@jimp/pngjs 0.0.0-stage → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +335 -2
  3. package/dist/browser/apng.d.ts +38 -0
  4. package/dist/browser/apng.d.ts.map +1 -0
  5. package/dist/browser/apng.js +109 -0
  6. package/dist/browser/apng.js.map +1 -0
  7. package/dist/browser/buffer-browser.d.mts.map +1 -0
  8. package/dist/browser/buffer-browser.mjs.map +1 -0
  9. package/dist/browser/buffer.d.ts +5 -0
  10. package/dist/browser/buffer.js +8 -0
  11. package/dist/browser/constants.d.ts +39 -0
  12. package/dist/browser/constants.d.ts.map +1 -0
  13. package/dist/browser/constants.js +55 -0
  14. package/dist/browser/constants.js.map +1 -0
  15. package/dist/browser/crc.d.ts +3 -0
  16. package/dist/browser/crc.d.ts.map +1 -0
  17. package/dist/browser/crc.js +24 -0
  18. package/dist/browser/crc.js.map +1 -0
  19. package/dist/browser/decode.d.ts +49 -0
  20. package/dist/browser/decode.d.ts.map +1 -0
  21. package/dist/browser/decode.js +49 -0
  22. package/dist/browser/decode.js.map +1 -0
  23. package/dist/browser/encode.d.ts +37 -0
  24. package/dist/browser/encode.d.ts.map +1 -0
  25. package/dist/browser/encode.js +252 -0
  26. package/dist/browser/encode.js.map +1 -0
  27. package/dist/browser/filter.d.ts +17 -0
  28. package/dist/browser/filter.d.ts.map +1 -0
  29. package/dist/browser/filter.js +180 -0
  30. package/dist/browser/filter.js.map +1 -0
  31. package/dist/browser/gamma.d.ts +22 -0
  32. package/dist/browser/gamma.d.ts.map +1 -0
  33. package/dist/browser/gamma.js +34 -0
  34. package/dist/browser/gamma.js.map +1 -0
  35. package/dist/browser/index.d.ts +14 -0
  36. package/dist/browser/index.d.ts.map +1 -0
  37. package/dist/browser/index.js +8 -0
  38. package/dist/browser/index.js.map +1 -0
  39. package/dist/browser/inflate-js.d.ts +10 -0
  40. package/dist/browser/inflate-js.d.ts.map +1 -0
  41. package/dist/browser/inflate-js.js +48 -0
  42. package/dist/browser/inflate-js.js.map +1 -0
  43. package/dist/browser/interlace.d.ts +22 -0
  44. package/dist/browser/interlace.d.ts.map +1 -0
  45. package/dist/browser/interlace.js +42 -0
  46. package/dist/browser/interlace.js.map +1 -0
  47. package/dist/browser/legacy.d.ts +153 -0
  48. package/dist/browser/legacy.d.ts.map +1 -0
  49. package/dist/browser/legacy.js +151 -0
  50. package/dist/browser/legacy.js.map +1 -0
  51. package/dist/browser/pack.d.ts +68 -0
  52. package/dist/browser/pack.d.ts.map +1 -0
  53. package/dist/browser/pack.js +342 -0
  54. package/dist/browser/pack.js.map +1 -0
  55. package/dist/browser/package.json +3 -0
  56. package/dist/browser/pixels.d.ts +18 -0
  57. package/dist/browser/pixels.d.ts.map +1 -0
  58. package/dist/browser/pixels.js +251 -0
  59. package/dist/browser/pixels.js.map +1 -0
  60. package/dist/browser/png.d.ts +87 -0
  61. package/dist/browser/png.d.ts.map +1 -0
  62. package/dist/browser/png.js +341 -0
  63. package/dist/browser/png.js.map +1 -0
  64. package/dist/browser/read.d.ts +71 -0
  65. package/dist/browser/read.d.ts.map +1 -0
  66. package/dist/browser/read.js +404 -0
  67. package/dist/browser/read.js.map +1 -0
  68. package/dist/browser/stream-base-browser.d.mts.map +1 -0
  69. package/dist/browser/stream-base-browser.mjs.map +1 -0
  70. package/dist/browser/stream-base.d.ts +21 -0
  71. package/dist/browser/stream-base.js +71 -0
  72. package/dist/browser/text.d.ts +10 -0
  73. package/dist/browser/text.d.ts.map +1 -0
  74. package/dist/browser/text.js +32 -0
  75. package/dist/browser/text.js.map +1 -0
  76. package/dist/browser/types.d.ts +147 -0
  77. package/dist/browser/types.d.ts.map +1 -0
  78. package/dist/browser/types.js +2 -0
  79. package/dist/browser/types.js.map +1 -0
  80. package/dist/browser/zlib-browser.d.mts.map +1 -0
  81. package/dist/browser/zlib-browser.mjs.map +1 -0
  82. package/dist/browser/zlib-stream.d.ts +9 -0
  83. package/dist/browser/zlib-stream.d.ts.map +1 -0
  84. package/dist/browser/zlib-stream.js +31 -0
  85. package/dist/browser/zlib-stream.js.map +1 -0
  86. package/dist/browser/zlib.d.ts +14 -0
  87. package/dist/browser/zlib.js +20 -0
  88. package/dist/commonjs/apng.d.ts +38 -0
  89. package/dist/commonjs/apng.d.ts.map +1 -0
  90. package/dist/commonjs/apng.js +112 -0
  91. package/dist/commonjs/apng.js.map +1 -0
  92. package/dist/commonjs/buffer.d.ts +7 -0
  93. package/dist/commonjs/buffer.d.ts.map +1 -0
  94. package/dist/commonjs/buffer.js +16 -0
  95. package/dist/commonjs/buffer.js.map +1 -0
  96. package/dist/commonjs/constants.d.ts +39 -0
  97. package/dist/commonjs/constants.d.ts.map +1 -0
  98. package/dist/commonjs/constants.js +59 -0
  99. package/dist/commonjs/constants.js.map +1 -0
  100. package/dist/commonjs/crc.d.ts +3 -0
  101. package/dist/commonjs/crc.d.ts.map +1 -0
  102. package/dist/commonjs/crc.js +27 -0
  103. package/dist/commonjs/crc.js.map +1 -0
  104. package/dist/commonjs/decode.d.ts +49 -0
  105. package/dist/commonjs/decode.d.ts.map +1 -0
  106. package/dist/commonjs/decode.js +55 -0
  107. package/dist/commonjs/decode.js.map +1 -0
  108. package/dist/commonjs/encode.d.ts +37 -0
  109. package/dist/commonjs/encode.d.ts.map +1 -0
  110. package/dist/commonjs/encode.js +262 -0
  111. package/dist/commonjs/encode.js.map +1 -0
  112. package/dist/commonjs/filter.d.ts +17 -0
  113. package/dist/commonjs/filter.d.ts.map +1 -0
  114. package/dist/commonjs/filter.js +184 -0
  115. package/dist/commonjs/filter.js.map +1 -0
  116. package/dist/commonjs/gamma.d.ts +22 -0
  117. package/dist/commonjs/gamma.d.ts.map +1 -0
  118. package/dist/commonjs/gamma.js +37 -0
  119. package/dist/commonjs/gamma.js.map +1 -0
  120. package/dist/commonjs/index.d.ts +14 -0
  121. package/dist/commonjs/index.d.ts.map +1 -0
  122. package/dist/commonjs/index.js +19 -0
  123. package/dist/commonjs/index.js.map +1 -0
  124. package/dist/commonjs/inflate-js.d.ts +10 -0
  125. package/dist/commonjs/inflate-js.d.ts.map +1 -0
  126. package/dist/commonjs/inflate-js.js +51 -0
  127. package/dist/commonjs/inflate-js.js.map +1 -0
  128. package/dist/commonjs/interlace.d.ts +22 -0
  129. package/dist/commonjs/interlace.d.ts.map +1 -0
  130. package/dist/commonjs/interlace.js +48 -0
  131. package/dist/commonjs/interlace.js.map +1 -0
  132. package/dist/commonjs/legacy.d.ts +153 -0
  133. package/dist/commonjs/legacy.d.ts.map +1 -0
  134. package/dist/commonjs/legacy.js +157 -0
  135. package/dist/commonjs/legacy.js.map +1 -0
  136. package/dist/commonjs/pack.d.ts +68 -0
  137. package/dist/commonjs/pack.d.ts.map +1 -0
  138. package/dist/commonjs/pack.js +348 -0
  139. package/dist/commonjs/pack.js.map +1 -0
  140. package/dist/commonjs/package.json +3 -0
  141. package/dist/commonjs/pixels.d.ts +18 -0
  142. package/dist/commonjs/pixels.d.ts.map +1 -0
  143. package/dist/commonjs/pixels.js +255 -0
  144. package/dist/commonjs/pixels.js.map +1 -0
  145. package/dist/commonjs/png.d.ts +87 -0
  146. package/dist/commonjs/png.d.ts.map +1 -0
  147. package/dist/commonjs/png.js +345 -0
  148. package/dist/commonjs/png.js.map +1 -0
  149. package/dist/commonjs/read.d.ts +71 -0
  150. package/dist/commonjs/read.d.ts.map +1 -0
  151. package/dist/commonjs/read.js +409 -0
  152. package/dist/commonjs/read.js.map +1 -0
  153. package/dist/commonjs/stream-base.d.ts +2 -0
  154. package/dist/commonjs/stream-base.d.ts.map +1 -0
  155. package/dist/commonjs/stream-base.js +8 -0
  156. package/dist/commonjs/stream-base.js.map +1 -0
  157. package/dist/commonjs/text.d.ts +10 -0
  158. package/dist/commonjs/text.d.ts.map +1 -0
  159. package/dist/commonjs/text.js +38 -0
  160. package/dist/commonjs/text.js.map +1 -0
  161. package/dist/commonjs/types.d.ts +147 -0
  162. package/dist/commonjs/types.d.ts.map +1 -0
  163. package/dist/commonjs/types.js +3 -0
  164. package/dist/commonjs/types.js.map +1 -0
  165. package/dist/commonjs/zlib-stream.d.ts +9 -0
  166. package/dist/commonjs/zlib-stream.d.ts.map +1 -0
  167. package/dist/commonjs/zlib-stream.js +35 -0
  168. package/dist/commonjs/zlib-stream.js.map +1 -0
  169. package/dist/commonjs/zlib.d.ts +20 -0
  170. package/dist/commonjs/zlib.d.ts.map +1 -0
  171. package/dist/commonjs/zlib.js +77 -0
  172. package/dist/commonjs/zlib.js.map +1 -0
  173. package/dist/esm/apng.d.ts +38 -0
  174. package/dist/esm/apng.d.ts.map +1 -0
  175. package/dist/esm/apng.js +109 -0
  176. package/dist/esm/apng.js.map +1 -0
  177. package/dist/esm/buffer.d.ts +7 -0
  178. package/dist/esm/buffer.d.ts.map +1 -0
  179. package/dist/esm/buffer.js +12 -0
  180. package/dist/esm/buffer.js.map +1 -0
  181. package/dist/esm/constants.d.ts +39 -0
  182. package/dist/esm/constants.d.ts.map +1 -0
  183. package/dist/esm/constants.js +55 -0
  184. package/dist/esm/constants.js.map +1 -0
  185. package/dist/esm/crc.d.ts +3 -0
  186. package/dist/esm/crc.d.ts.map +1 -0
  187. package/dist/esm/crc.js +24 -0
  188. package/dist/esm/crc.js.map +1 -0
  189. package/dist/esm/decode.d.ts +49 -0
  190. package/dist/esm/decode.d.ts.map +1 -0
  191. package/dist/esm/decode.js +49 -0
  192. package/dist/esm/decode.js.map +1 -0
  193. package/dist/esm/encode.d.ts +37 -0
  194. package/dist/esm/encode.d.ts.map +1 -0
  195. package/dist/esm/encode.js +252 -0
  196. package/dist/esm/encode.js.map +1 -0
  197. package/dist/esm/filter.d.ts +17 -0
  198. package/dist/esm/filter.d.ts.map +1 -0
  199. package/dist/esm/filter.js +180 -0
  200. package/dist/esm/filter.js.map +1 -0
  201. package/dist/esm/gamma.d.ts +22 -0
  202. package/dist/esm/gamma.d.ts.map +1 -0
  203. package/dist/esm/gamma.js +34 -0
  204. package/dist/esm/gamma.js.map +1 -0
  205. package/dist/esm/index.d.ts +14 -0
  206. package/dist/esm/index.d.ts.map +1 -0
  207. package/dist/esm/index.js +8 -0
  208. package/dist/esm/index.js.map +1 -0
  209. package/dist/esm/inflate-js.d.ts +10 -0
  210. package/dist/esm/inflate-js.d.ts.map +1 -0
  211. package/dist/esm/inflate-js.js +48 -0
  212. package/dist/esm/inflate-js.js.map +1 -0
  213. package/dist/esm/interlace.d.ts +22 -0
  214. package/dist/esm/interlace.d.ts.map +1 -0
  215. package/dist/esm/interlace.js +42 -0
  216. package/dist/esm/interlace.js.map +1 -0
  217. package/dist/esm/legacy.d.ts +153 -0
  218. package/dist/esm/legacy.d.ts.map +1 -0
  219. package/dist/esm/legacy.js +151 -0
  220. package/dist/esm/legacy.js.map +1 -0
  221. package/dist/esm/pack.d.ts +68 -0
  222. package/dist/esm/pack.d.ts.map +1 -0
  223. package/dist/esm/pack.js +342 -0
  224. package/dist/esm/pack.js.map +1 -0
  225. package/dist/esm/package.json +3 -0
  226. package/dist/esm/pixels.d.ts +18 -0
  227. package/dist/esm/pixels.d.ts.map +1 -0
  228. package/dist/esm/pixels.js +251 -0
  229. package/dist/esm/pixels.js.map +1 -0
  230. package/dist/esm/png.d.ts +87 -0
  231. package/dist/esm/png.d.ts.map +1 -0
  232. package/dist/esm/png.js +341 -0
  233. package/dist/esm/png.js.map +1 -0
  234. package/dist/esm/read.d.ts +71 -0
  235. package/dist/esm/read.d.ts.map +1 -0
  236. package/dist/esm/read.js +404 -0
  237. package/dist/esm/read.js.map +1 -0
  238. package/dist/esm/stream-base.d.ts +2 -0
  239. package/dist/esm/stream-base.d.ts.map +1 -0
  240. package/dist/esm/stream-base.js +4 -0
  241. package/dist/esm/stream-base.js.map +1 -0
  242. package/dist/esm/text.d.ts +10 -0
  243. package/dist/esm/text.d.ts.map +1 -0
  244. package/dist/esm/text.js +32 -0
  245. package/dist/esm/text.js.map +1 -0
  246. package/dist/esm/types.d.ts +147 -0
  247. package/dist/esm/types.d.ts.map +1 -0
  248. package/dist/esm/types.js +2 -0
  249. package/dist/esm/types.js.map +1 -0
  250. package/dist/esm/zlib-stream.d.ts +9 -0
  251. package/dist/esm/zlib-stream.d.ts.map +1 -0
  252. package/dist/esm/zlib-stream.js +31 -0
  253. package/dist/esm/zlib-stream.js.map +1 -0
  254. package/dist/esm/zlib.d.ts +20 -0
  255. package/dist/esm/zlib.d.ts.map +1 -0
  256. package/dist/esm/zlib.js +39 -0
  257. package/dist/esm/zlib.js.map +1 -0
  258. package/package.json +95 -4
@@ -0,0 +1,151 @@
1
+ import { toBuffer, toUint8Array } from "./buffer.js";
2
+ import { ALLOWED_BIT_DEPTHS, CHANNELS, ColorType } from "./constants.js";
3
+ import { getDecodedInfo } from "./decode.js";
4
+ import { validateImage, writePng } from "./encode.js";
5
+ import { buildPalette, chooseFormat, getPixelStats } from "./pack.js";
6
+ import { decodePixels } from "./pixels.js";
7
+ import { readPng } from "./read.js";
8
+ export function getMetadata(header, hasTransparency) {
9
+ const { colorType } = header;
10
+ return {
11
+ width: header.width,
12
+ height: header.height,
13
+ depth: header.depth,
14
+ interlace: header.interlace,
15
+ palette: colorType === ColorType.PALETTE,
16
+ color: (colorType & 2) !== 0,
17
+ alpha: (colorType & 4) !== 0 || hasTransparency,
18
+ bpp: CHANNELS[colorType],
19
+ colorType,
20
+ };
21
+ }
22
+ export function legacyRead(input, options = {}) {
23
+ const bytes = typeof input === "string"
24
+ ? new TextEncoder().encode(input)
25
+ : toUint8Array(input);
26
+ const file = readPng(bytes, { checkCRC: options.checkCRC !== false });
27
+ const indexed = options.keepIndexed && file.header.colorType === ColorType.PALETTE;
28
+ const data = decodePixels({
29
+ ...file.header,
30
+ palette: file.palette,
31
+ transparentColor: file.transparentColor,
32
+ }, file.idat, indexed ? "indexed" : options.skipRescale ? "raw" : 8);
33
+ const info = getDecodedInfo(file);
34
+ return {
35
+ ...info,
36
+ ...getMetadata(file.header, file.trns !== undefined),
37
+ data: (data instanceof Uint8Array ? toBuffer(data) : data),
38
+ gamma: file.gamma ?? 0,
39
+ palette: file.palette ?? false,
40
+ transColor: file.transparentColor,
41
+ };
42
+ }
43
+ const INPUT_COLOR_TYPES = [0, 2, 4, 6];
44
+ const OUTPUT_COLOR_TYPES = [0, 2, 3, 4, 6];
45
+ /** Reads 16-bit samples in the platform's byte order */
46
+ function asUint16Array(data) {
47
+ if (data instanceof Uint16Array) {
48
+ return data;
49
+ }
50
+ // A Buffer from Node's pool may start at an odd offset, so copy it then
51
+ if (data.byteOffset % 2 === 0) {
52
+ return new Uint16Array(data.buffer, data.byteOffset, data.byteLength >> 1);
53
+ }
54
+ return new Uint16Array(data.slice().buffer, 0, data.byteLength >> 1);
55
+ }
56
+ /**
57
+ * Converts the input pixels to RGBA, which the encoder takes. pngjs only
58
+ * reads input as RGBA unless `inputColorType` or `inputHasAlpha` says
59
+ * otherwise.
60
+ */
61
+ function toRgba(image, inputColorType, inputHasAlpha, sixteenBit) {
62
+ const { width, height } = image;
63
+ const input = sixteenBit ? asUint16Array(image.data) : image.data;
64
+ const max = sixteenBit ? 65535 : 255;
65
+ let channels = CHANNELS[inputColorType];
66
+ // pngjs: RGBA input without alpha is RGB
67
+ if (inputColorType === 6 && !inputHasAlpha) {
68
+ channels = 3;
69
+ }
70
+ if (input.length < width * height * channels) {
71
+ throw new Error(`Not enough pixel data: a ${width}x${height} image needs ${width * height * channels} samples, got ${input.length}`);
72
+ }
73
+ if (channels === 4) {
74
+ return { width, height, data: input, depth: sixteenBit ? 16 : 8 };
75
+ }
76
+ const length = width * height;
77
+ const data = sixteenBit
78
+ ? new Uint16Array(length * 4)
79
+ : new Uint8Array(length * 4);
80
+ for (let p = 0, i = 0, o = 0; p < length; p++, i += channels, o += 4) {
81
+ switch (channels) {
82
+ case 1:
83
+ data[o] = data[o + 1] = data[o + 2] = input[i];
84
+ data[o + 3] = max;
85
+ break;
86
+ case 2:
87
+ data[o] = data[o + 1] = data[o + 2] = input[i];
88
+ data[o + 3] = input[i + 1];
89
+ break;
90
+ default:
91
+ data[o] = input[i];
92
+ data[o + 1] = input[i + 1];
93
+ data[o + 2] = input[i + 2];
94
+ data[o + 3] = max;
95
+ }
96
+ }
97
+ return { width, height, data, depth: sixteenBit ? 16 : 8 };
98
+ }
99
+ /** Everything needed to write a PNG, short of compressing it */
100
+ export function prepareLegacyWrite(image, options = {}) {
101
+ const { colorType = 6, inputColorType = 6, inputHasAlpha = true, bitDepth = 8, } = options;
102
+ if (!OUTPUT_COLOR_TYPES.includes(colorType)) {
103
+ throw new Error(`option color type:${colorType} is not supported at present`);
104
+ }
105
+ if (!INPUT_COLOR_TYPES.includes(inputColorType)) {
106
+ throw new Error(`option input color type:${inputColorType} is not supported at present`);
107
+ }
108
+ if (![1, 2, 4, 8, 16].includes(bitDepth)) {
109
+ throw new Error(`option bit depth:${bitDepth} is not supported at present`);
110
+ }
111
+ const sixteenBit = bitDepth === 16;
112
+ const source = toRgba(image, inputColorType, inputHasAlpha, sixteenBit);
113
+ validateImage(source);
114
+ let depth = bitDepth;
115
+ let palette;
116
+ if (colorType === ColorType.PALETTE) {
117
+ const stats = getPixelStats(source);
118
+ // Without a bitDepth, use the smallest that indexes every colour
119
+ depth = chooseFormat(source, stats, colorType, options.bitDepth).depth;
120
+ palette = buildPalette(stats.colors);
121
+ }
122
+ else if (!ALLOWED_BIT_DEPTHS[colorType].includes(bitDepth)) {
123
+ throw new Error(`option bit depth:${bitDepth} is not supported for color type ${colorType}`);
124
+ }
125
+ const max = sixteenBit ? 65535 : 255;
126
+ const bg = options.bgColor ?? {};
127
+ return {
128
+ source,
129
+ header: {
130
+ width: image.width,
131
+ height: image.height,
132
+ depth,
133
+ colorType: colorType,
134
+ interlace: false,
135
+ },
136
+ pack: {
137
+ background: {
138
+ red: bg.red ?? max,
139
+ green: bg.green ?? max,
140
+ blue: bg.blue ?? max,
141
+ },
142
+ palette,
143
+ },
144
+ metadata: { gamma: image.gamma || undefined },
145
+ };
146
+ }
147
+ export function legacyWrite(image, options = {}) {
148
+ const { source, header, pack, metadata } = prepareLegacyWrite(image, options);
149
+ return toBuffer(writePng(source, header, pack, metadata, options));
150
+ }
151
+ //# sourceMappingURL=legacy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacy.js","sourceRoot":"","sources":["../../src/legacy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEzE,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAyHpC,MAAM,UAAU,WAAW,CACzB,MAMC,EACD,eAAwB;IAExB,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;IAE7B,OAAO;QACL,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,OAAO,EAAE,SAAS,KAAK,SAAS,CAAC,OAAO;QACxC,KAAK,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC;QAC5B,KAAK,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,eAAe;QAC/C,GAAG,EAAE,QAAQ,CAAC,SAAS,CAAoB;QAC3C,SAAS;KACV,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CACxB,KAAwC,EACxC,OAAO,GAAkB,EA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AE,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,SAAS,EAAE;KAC9C,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,KAAkB,EAClB,OAAO,GAAkB,EAAE;IAE3B,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,kBAAkB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAE9E,OAAO,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;AACrE,CAAC","sourcesContent":["// The pngjs-compatible sync API: PNG.sync.read and PNG.sync.write\nimport type { BufferLike } from \"./buffer.js\";\nimport { toBuffer, toUint8Array } from \"./buffer.js\";\nimport { ALLOWED_BIT_DEPTHS, CHANNELS, ColorType } from \"./constants.js\";\nimport type { BitDepth } from \"./constants.js\";\nimport { getDecodedInfo } from \"./decode.js\";\nimport { validateImage, writePng } from \"./encode.js\";\nimport { buildPalette, chooseFormat, getPixelStats } from \"./pack.js\";\nimport type { Palette, PixelSource } from \"./pack.js\";\nimport { decodePixels } from \"./pixels.js\";\nimport { readPng } from \"./read.js\";\nimport type {\n IccProfile,\n PaletteColor,\n PhysicalDimensions,\n PngText,\n RenderingIntent,\n} from \"./types.js\";\n\n/** The color types pngjs accepts for its input and output */\nexport type LegacyColorType = 0 | 2 | 3 | 4 | 6;\n\nexport interface ParserOptions {\n /**\n * Check the CRC of each chunk\n * @default true\n */\n checkCRC?: boolean | undefined;\n /**\n * Keep the samples at the image's bit depth instead of scaling them to 8\n * bits. 16-bit images decode to a Uint16Array.\n */\n skipRescale?: boolean | undefined;\n /**\n * Decode palette images to one palette index per pixel instead of RGBA.\n * The palette is in `palette`.\n */\n keepIndexed?: boolean | undefined;\n}\n\nexport interface PackerOptions {\n /** zlib's compression level, 0-9 @default 9 */\n deflateLevel?: number | undefined;\n /** zlib's strategy, e.g. 3 (Z_RLE) or 0 (Z_DEFAULT_STRATEGY) @default 3 */\n deflateStrategy?: number | undefined;\n /** Split the image data into IDAT chunks of at most this many bytes */\n idatChunkSize?: number | undefined;\n /**\n * The background that transparent pixels are blended with when the output\n * has no alpha channel, at the output bit depth\n * @default white\n */\n bgColor?: { red: number; green: number; blue: number } | undefined;\n /**\n * Bits per sample of the input and output. 16-bit input has one sample\n * per 2 bytes, in the platform's byte order (a Uint16Array's bytes).\n * Palette (3) and grayscale (0) output can also be 1, 2 or 4 bits.\n * @default 8\n */\n bitDepth?: BitDepth | undefined;\n /**\n * The output colour type. 3 writes a palette, which needs 256 colours or\n * fewer.\n * @default 6\n */\n colorType?: LegacyColorType | undefined;\n /** The input colour type. @default 6 */\n inputColorType?: Exclude<LegacyColorType, 3> | undefined;\n /**\n * With `inputColorType` 6: whether the input has 4 samples per pixel\n * (RGBA) or 3 (RGB).\n * @default true\n */\n inputHasAlpha?: boolean | undefined;\n /** Size of the chunks the deflate stream emits @default 32768 */\n deflateChunkSize?: number | undefined;\n /**\n * Creates the deflate stream that `pack()` compresses with, e.g.\n * `zlib.createDeflate`. By default `pack()` compresses synchronously, with\n * zlib in Node.js and pako in browsers.\n */\n deflateFactory?: DeflateFactory | undefined;\n /** -1 for automatic, a filter type 0-4, or a list to choose from @default -1 */\n filterType?: number | number[] | undefined;\n}\n\n/** A Node.js-style deflate stream, as zlib.createDeflate makes */\nexport interface DeflateStream {\n on(event: \"data\", listener: (chunk: Uint8Array) => void): unknown;\n on(event: \"end\", listener: () => void): unknown;\n on(event: \"error\", listener: (error: Error) => void): unknown;\n end(data: Uint8Array): unknown;\n}\n\nexport type DeflateFactory = (options: {\n chunkSize?: number;\n level?: number;\n strategy?: number;\n}) => DeflateStream;\n\nexport interface Metadata {\n width: number;\n height: number;\n depth: BitDepth;\n interlace: boolean;\n /** Whether the image is a palette image */\n palette: boolean;\n /** Whether the image has colour */\n color: boolean;\n /** Whether the image has an alpha channel or tRNS transparency */\n alpha: boolean;\n /** Samples per pixel */\n bpp: 1 | 2 | 3 | 4;\n colorType: LegacyColorType;\n}\n\nexport interface PNGWithMetadata extends Omit<Metadata, \"palette\"> {\n data: BufferLike;\n /** The gAMA chunk's gamma, 0 if there isn't one */\n gamma: number;\n /** The palette of palette images, `false` for other images */\n palette: PaletteColor[] | false;\n /** The tRNS colour of grayscale and colour images */\n transColor?: number[];\n srgb?: RenderingIntent;\n iccProfile?: IccProfile;\n physical?: PhysicalDimensions;\n text: PngText[];\n exif?: Uint8Array;\n}\n\nexport function getMetadata(\n header: {\n width: number;\n height: number;\n depth: BitDepth;\n colorType: ColorType;\n interlace: boolean;\n },\n hasTransparency: boolean,\n): Metadata {\n const { colorType } = header;\n\n return {\n width: header.width,\n height: header.height,\n depth: header.depth,\n interlace: header.interlace,\n palette: colorType === ColorType.PALETTE,\n color: (colorType & 2) !== 0,\n alpha: (colorType & 4) !== 0 || hasTransparency,\n bpp: CHANNELS[colorType] as Metadata[\"bpp\"],\n colorType,\n };\n}\n\nexport function legacyRead(\n input: Uint8Array | ArrayBuffer | string,\n options: ParserOptions = {},\n): PNGWithMetadata {\n const bytes =\n typeof input === \"string\"\n ? new TextEncoder().encode(input)\n : toUint8Array(input);\n const file = readPng(bytes, { checkCRC: options.checkCRC !== false });\n const indexed =\n options.keepIndexed && file.header.colorType === ColorType.PALETTE;\n const data = decodePixels(\n {\n ...file.header,\n palette: file.palette,\n transparentColor: file.transparentColor,\n },\n file.idat,\n indexed ? \"indexed\" : options.skipRescale ? \"raw\" : 8,\n );\n const info = getDecodedInfo(file);\n\n return {\n ...info,\n ...getMetadata(file.header, file.trns !== undefined),\n data: (data instanceof Uint8Array ? toBuffer(data) : data) as BufferLike,\n gamma: file.gamma ?? 0,\n palette: file.palette ?? false,\n transColor: file.transparentColor,\n };\n}\n\n/** An image to write: what a PNG instance or PNG.sync.read result has */\nexport interface LegacyImage {\n width: number;\n height: number;\n data: Uint8Array | Uint16Array;\n gamma?: number;\n}\n\nconst INPUT_COLOR_TYPES = [0, 2, 4, 6];\nconst OUTPUT_COLOR_TYPES = [0, 2, 3, 4, 6];\n\n/** Reads 16-bit samples in the platform's byte order */\nfunction asUint16Array(data: Uint8Array | Uint16Array) {\n if (data instanceof Uint16Array) {\n return data;\n }\n\n // A Buffer from Node's pool may start at an odd offset, so copy it then\n if (data.byteOffset % 2 === 0) {\n return new Uint16Array(data.buffer, data.byteOffset, data.byteLength >> 1);\n }\n\n return new Uint16Array(data.slice().buffer, 0, data.byteLength >> 1);\n}\n\n/**\n * Converts the input pixels to RGBA, which the encoder takes. pngjs only\n * reads input as RGBA unless `inputColorType` or `inputHasAlpha` says\n * otherwise.\n */\nfunction toRgba(\n image: LegacyImage,\n inputColorType: number,\n inputHasAlpha: boolean,\n sixteenBit: boolean,\n): PixelSource {\n const { width, height } = image;\n const input = sixteenBit ? asUint16Array(image.data) : image.data;\n const max = sixteenBit ? 65535 : 255;\n let channels = CHANNELS[inputColorType as ColorType];\n\n // pngjs: RGBA input without alpha is RGB\n if (inputColorType === 6 && !inputHasAlpha) {\n channels = 3;\n }\n\n if (input.length < width * height * channels) {\n throw new Error(\n `Not enough pixel data: a ${width}x${height} image needs ${width * height * channels} samples, got ${input.length}`,\n );\n }\n\n if (channels === 4) {\n return { width, height, data: input, depth: sixteenBit ? 16 : 8 };\n }\n\n const length = width * height;\n const data = sixteenBit\n ? new Uint16Array(length * 4)\n : new Uint8Array(length * 4);\n\n for (let p = 0, i = 0, o = 0; p < length; p++, i += channels, o += 4) {\n switch (channels) {\n case 1:\n data[o] = data[o + 1] = data[o + 2] = input[i]!;\n data[o + 3] = max;\n break;\n case 2:\n data[o] = data[o + 1] = data[o + 2] = input[i]!;\n data[o + 3] = input[i + 1]!;\n break;\n default:\n data[o] = input[i]!;\n data[o + 1] = input[i + 1]!;\n data[o + 2] = input[i + 2]!;\n data[o + 3] = max;\n }\n }\n\n return { width, height, data, depth: sixteenBit ? 16 : 8 };\n}\n\n/** Everything needed to write a PNG, short of compressing it */\nexport function prepareLegacyWrite(\n image: LegacyImage,\n options: PackerOptions = {},\n) {\n const {\n colorType = 6,\n inputColorType = 6,\n inputHasAlpha = true,\n bitDepth = 8,\n } = options;\n\n if (!OUTPUT_COLOR_TYPES.includes(colorType)) {\n throw new Error(\n `option color type:${colorType} is not supported at present`,\n );\n }\n\n if (!INPUT_COLOR_TYPES.includes(inputColorType)) {\n throw new Error(\n `option input color type:${inputColorType} is not supported at present`,\n );\n }\n\n if (![1, 2, 4, 8, 16].includes(bitDepth)) {\n throw new Error(`option bit depth:${bitDepth} is not supported at present`);\n }\n\n const sixteenBit = bitDepth === 16;\n const source = toRgba(image, inputColorType, inputHasAlpha, sixteenBit);\n\n validateImage(source);\n\n let depth: BitDepth = bitDepth;\n let palette: Palette | undefined;\n\n if (colorType === ColorType.PALETTE) {\n const stats = getPixelStats(source);\n\n // Without a bitDepth, use the smallest that indexes every colour\n depth = chooseFormat(source, stats, colorType, options.bitDepth).depth;\n palette = buildPalette(stats.colors!);\n } else if (!ALLOWED_BIT_DEPTHS[colorType as ColorType].includes(bitDepth)) {\n throw new Error(\n `option bit depth:${bitDepth} is not supported for color type ${colorType}`,\n );\n }\n\n const max = sixteenBit ? 65535 : 255;\n const bg: { red?: number; green?: number; blue?: number } =\n options.bgColor ?? {};\n\n return {\n source,\n header: {\n width: image.width,\n height: image.height,\n depth,\n colorType: colorType as ColorType,\n interlace: false,\n },\n pack: {\n background: {\n red: bg.red ?? max,\n green: bg.green ?? max,\n blue: bg.blue ?? max,\n },\n palette,\n },\n metadata: { gamma: image.gamma || undefined },\n };\n}\n\nexport function legacyWrite(\n image: LegacyImage,\n options: PackerOptions = {},\n): BufferLike {\n const { source, header, pack, metadata } = prepareLegacyWrite(image, options);\n\n return toBuffer(writePng(source, header, pack, metadata, options));\n}\n"]}
@@ -0,0 +1,68 @@
1
+ import { ColorType } from "./constants.js";
2
+ import type { BitDepth } from "./constants.js";
3
+ import type { Pass } from "./interlace.js";
4
+ import type { PngHeader } from "./types.js";
5
+ /** RGBA samples, 8 or 16 bits each */
6
+ export interface PixelSource {
7
+ width: number;
8
+ height: number;
9
+ data: Uint8Array | Uint16Array;
10
+ depth: 8 | 16;
11
+ }
12
+ /** What encode() needs to know about the pixels to pick a format */
13
+ export interface PixelStats {
14
+ /** Every pixel has R = G = B */
15
+ gray: boolean;
16
+ /** Every pixel has full alpha */
17
+ opaque: boolean;
18
+ /** The 16-bit samples all survive a trip through 8 bits */
19
+ fits8: boolean;
20
+ /**
21
+ * The smallest bit depth that stores the gray levels without loss, for
22
+ * opaque gray images
23
+ */
24
+ grayDepth: BitDepth;
25
+ /**
26
+ * The distinct colours, as 0xRRGGBBAA at 8 bits, mapped to the order they
27
+ * appear in. Undefined if there are more than 256.
28
+ */
29
+ colors?: Map<number, number>;
30
+ }
31
+ /** Scans the pixels, stopping once nothing more can be learned */
32
+ export declare function getPixelStats(source: PixelSource): PixelStats;
33
+ /** Picks the colour type and bit depth to write */
34
+ export declare function chooseFormat(source: PixelSource, stats: PixelStats, colorType?: ColorType, bitDepth?: BitDepth): {
35
+ colorType: ColorType;
36
+ depth: BitDepth;
37
+ };
38
+ /** The PLTE and tRNS data, and each colour's index */
39
+ export interface Palette {
40
+ plte: Uint8Array;
41
+ trns?: Uint8Array;
42
+ indices: Map<number, number>;
43
+ }
44
+ /**
45
+ * Orders the palette with transparent colours first, so the tRNS chunk can
46
+ * stop at the last of them. Otherwise colours keep the order they appear in.
47
+ */
48
+ export declare function buildPalette(colors: Map<number, number>): Palette;
49
+ export interface PackOptions {
50
+ /** Blends transparent pixels with this when there's no alpha channel */
51
+ background: {
52
+ red: number;
53
+ green: number;
54
+ blue: number;
55
+ };
56
+ palette?: Palette;
57
+ /**
58
+ * Converts colour to gray, on the working scale (8 or 16 bits). Defaults
59
+ * to the mean of R, G and B.
60
+ */
61
+ toGray?: (r: number, g: number, b: number) => number;
62
+ }
63
+ /**
64
+ * Converts RGBA pixels to the lines of a pass, packed in the PNG's format,
65
+ * without filter bytes.
66
+ */
67
+ export declare function packPass(source: PixelSource, header: PngHeader, pass: Pass, options: PackOptions): Uint8Array<ArrayBufferLike>;
68
+ //# sourceMappingURL=pack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pack.d.ts","sourceRoot":"","sources":["../../src/pack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,sCAAsC;AACtC,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,UAAU,GAAG,WAAW,CAAC;IAC/B,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC;CACf;AAED,oEAAoE;AACpE,MAAM,WAAW,UAAU;IACzB,gCAAgC;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,iCAAiC;IACjC,MAAM,EAAE,OAAO,CAAC;IAChB,2DAA2D;IAC3D,KAAK,EAAE,OAAO,CAAC;IACf;;;OAGG;IACH,SAAS,EAAE,QAAQ,CAAC;IACpB;;;OAGG;IACH,MAAM,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9B;AAED,kEAAkE;AAClE,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAkG7D;AAwBD,mDAAmD;AACnD,wBAAgB,YAAY,CAC1B,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,UAAU,EACjB,SAAS,CAAC,EAAE,SAAS,EACrB,QAAQ,CAAC,EAAE,QAAQ,GAClB;IAAE,SAAS,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,QAAQ,CAAA;CAAE,CA8D3C;AAED,sDAAsD;AACtD,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC9B;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CA2BjE;AAED,MAAM,WAAW,WAAW;IAC1B,wEAAwE;IACxE,UAAU,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACzD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;CACtD;AAMD;;;GAGG;AACH,wBAAgB,QAAQ,CACtB,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,WAAW,+BAqKrB"}
@@ -0,0 +1,342 @@
1
+ import { ALLOWED_BIT_DEPTHS, ColorType } from "./constants.js";
2
+ import { getLineBytes } from "./interlace.js";
3
+ /** Scans the pixels, stopping once nothing more can be learned */
4
+ export function getPixelStats(source) {
5
+ const { data, depth } = source;
6
+ const length = source.width * source.height * 4;
7
+ let gray = true;
8
+ let opaque = true;
9
+ let fits8 = depth === 8;
10
+ // Bit 1, 2 and 4 are set while those gray depths are lossless
11
+ let grayDepths = 1 | 2 | 4;
12
+ let colors = new Map();
13
+ let lastKey = -1;
14
+ const max = depth === 16 ? 65535 : 255;
15
+ if (depth === 16) {
16
+ fits8 = true;
17
+ for (let i = 0; i < length; i++) {
18
+ if (data[i] % 257 !== 0) {
19
+ fits8 = false;
20
+ break;
21
+ }
22
+ }
23
+ }
24
+ // Colours are counted at 8 bits, which is only lossless if fits8
25
+ const shift = depth === 16 ? 8 : 0;
26
+ if (depth === 16 && !fits8) {
27
+ colors = undefined;
28
+ }
29
+ for (let i = 0; i < length; i += 4) {
30
+ const r = data[i];
31
+ const g = data[i + 1];
32
+ const b = data[i + 2];
33
+ const a = data[i + 3];
34
+ if (a !== max) {
35
+ opaque = false;
36
+ }
37
+ if (gray) {
38
+ if (r !== g || r !== b) {
39
+ gray = false;
40
+ }
41
+ else if (grayDepths && depth === 8) {
42
+ if (r % 17 !== 0) {
43
+ grayDepths = 0;
44
+ }
45
+ else if (r % 85 !== 0) {
46
+ grayDepths &= 4;
47
+ }
48
+ else if (r !== 0 && r !== 255) {
49
+ grayDepths &= 2 | 4;
50
+ }
51
+ }
52
+ }
53
+ if (colors) {
54
+ const key = (((r >> shift) << 24) |
55
+ ((g >> shift) << 16) |
56
+ ((b >> shift) << 8) |
57
+ (a >> shift)) >>>
58
+ 0;
59
+ if (key !== lastKey) {
60
+ lastKey = key;
61
+ if (!colors.has(key)) {
62
+ if (colors.size === 256) {
63
+ colors = undefined;
64
+ }
65
+ else {
66
+ colors.set(key, colors.size);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ if (!gray && !opaque && !colors) {
72
+ break;
73
+ }
74
+ }
75
+ let grayDepth = depth === 16 && !fits8 ? 16 : 8;
76
+ if (depth === 8 || fits8) {
77
+ if (grayDepths & 1) {
78
+ grayDepth = 1;
79
+ }
80
+ else if (grayDepths & 2) {
81
+ grayDepth = 2;
82
+ }
83
+ else if (grayDepths & 4) {
84
+ grayDepth = 4;
85
+ }
86
+ }
87
+ // The 16-bit gray check above only ran for 8-bit data
88
+ if (depth === 16 && fits8 && gray) {
89
+ grayDepth = getGrayDepth16(data, length);
90
+ }
91
+ return { gray, opaque, fits8, grayDepth, colors };
92
+ }
93
+ function getGrayDepth16(data, length) {
94
+ let grayDepths = 1 | 2 | 4;
95
+ for (let i = 0; i < length && grayDepths; i += 4) {
96
+ const v = data[i] / 257;
97
+ if (v % 17 !== 0) {
98
+ grayDepths = 0;
99
+ }
100
+ else if (v % 85 !== 0) {
101
+ grayDepths &= 4;
102
+ }
103
+ else if (v !== 0 && v !== 255) {
104
+ grayDepths &= 2 | 4;
105
+ }
106
+ }
107
+ return grayDepths & 1 ? 1 : grayDepths & 2 ? 2 : grayDepths & 4 ? 4 : 8;
108
+ }
109
+ function getPaletteDepth(count) {
110
+ return count <= 2 ? 1 : count <= 4 ? 2 : count <= 16 ? 4 : 8;
111
+ }
112
+ /** Picks the colour type and bit depth to write */
113
+ export function chooseFormat(source, stats, colorType, bitDepth) {
114
+ const { gray, opaque, colors } = stats;
115
+ const wide = source.depth === 16 && !stats.fits8;
116
+ if (colorType === undefined) {
117
+ const direct = gray
118
+ ? opaque
119
+ ? ColorType.GRAYSCALE
120
+ : ColorType.GRAYSCALE_ALPHA
121
+ : opaque
122
+ ? ColorType.COLOR
123
+ : ColorType.COLOR_ALPHA;
124
+ if (bitDepth === 16 || wide) {
125
+ colorType = direct;
126
+ }
127
+ else if (gray && opaque && stats.grayDepth <= (bitDepth ?? 8)) {
128
+ colorType = ColorType.GRAYSCALE;
129
+ }
130
+ else if (colors && colors.size <= 2 ** (bitDepth ?? 8)) {
131
+ colorType = ColorType.PALETTE;
132
+ }
133
+ else if (bitDepth === undefined || bitDepth === 8) {
134
+ colorType = direct;
135
+ }
136
+ else {
137
+ throw new Error(`The image can't be stored with a bit depth of ${bitDepth} without loss. Set colorType to convert it.`);
138
+ }
139
+ }
140
+ if (bitDepth === undefined) {
141
+ if (colorType === ColorType.PALETTE) {
142
+ if (!colors) {
143
+ throw new Error("The image has more than 256 colors, so it can't be written as a palette image");
144
+ }
145
+ bitDepth = getPaletteDepth(colors.size);
146
+ }
147
+ else if (wide) {
148
+ bitDepth = 16;
149
+ }
150
+ else if (colorType === ColorType.GRAYSCALE && gray && opaque) {
151
+ bitDepth = stats.grayDepth;
152
+ }
153
+ else {
154
+ bitDepth = 8;
155
+ }
156
+ }
157
+ if (!ALLOWED_BIT_DEPTHS[colorType]?.includes(bitDepth)) {
158
+ throw new Error(`Bit depth ${bitDepth} isn't allowed for color type ${colorType}`);
159
+ }
160
+ if (colorType === ColorType.PALETTE &&
161
+ (!colors || colors.size > 2 ** bitDepth)) {
162
+ throw new Error(`The image has more than ${2 ** bitDepth} colors, so it can't be written as a palette image with a bit depth of ${bitDepth}`);
163
+ }
164
+ return { colorType, depth: bitDepth };
165
+ }
166
+ /**
167
+ * Orders the palette with transparent colours first, so the tRNS chunk can
168
+ * stop at the last of them. Otherwise colours keep the order they appear in.
169
+ */
170
+ export function buildPalette(colors) {
171
+ const keys = [...colors.keys()];
172
+ const sorted = [
173
+ ...keys.filter((key) => (key & 0xff) !== 0xff),
174
+ ...keys.filter((key) => (key & 0xff) === 0xff),
175
+ ];
176
+ const plte = new Uint8Array(sorted.length * 3);
177
+ const alpha = [];
178
+ const indices = new Map();
179
+ sorted.forEach((key, i) => {
180
+ plte[i * 3] = key >>> 24;
181
+ plte[i * 3 + 1] = (key >>> 16) & 0xff;
182
+ plte[i * 3 + 2] = (key >>> 8) & 0xff;
183
+ if ((key & 0xff) !== 0xff) {
184
+ alpha.push(key & 0xff);
185
+ }
186
+ indices.set(key, i);
187
+ });
188
+ return {
189
+ plte,
190
+ trns: alpha.length ? new Uint8Array(alpha) : undefined,
191
+ indices,
192
+ };
193
+ }
194
+ function defaultGray(r, g, b) {
195
+ return Math.round((r + g + b) / 3);
196
+ }
197
+ /**
198
+ * Converts RGBA pixels to the lines of a pass, packed in the PNG's format,
199
+ * without filter bytes.
200
+ */
201
+ export function packPass(source, header, pass, options) {
202
+ const { data, width } = source;
203
+ const { colorType, depth } = header;
204
+ const lineBytes = getLineBytes(header, pass.width);
205
+ const step = pass.xStep * 4;
206
+ // RGBA at 8 bits, not interlaced: the source is already in the format
207
+ if (colorType === ColorType.COLOR_ALPHA &&
208
+ depth === 8 &&
209
+ source.depth === 8 &&
210
+ pass.xStep === 1 &&
211
+ pass.yStep === 1) {
212
+ return data.subarray(0, lineBytes * pass.height);
213
+ }
214
+ const out = new Uint8Array(lineBytes * pass.height);
215
+ // Samples are converted to the working depth, 8 or 16 bits, then to the
216
+ // output depth if that's lower
217
+ const working = depth === 16 ? 16 : 8;
218
+ const max = working === 16 ? 65535 : 255;
219
+ const fromSource = source.depth === working
220
+ ? undefined
221
+ : source.depth === 8
222
+ ? (v) => v * 257
223
+ : (v) => ((v + 128) / 257) | 0;
224
+ const bg = options.background;
225
+ const hasAlpha = colorType === ColorType.COLOR_ALPHA ||
226
+ colorType === ColorType.GRAYSCALE_ALPHA;
227
+ const toGray = options.toGray ?? defaultGray;
228
+ const indices = options.palette?.indices;
229
+ const lowMax = (1 << depth) - 1;
230
+ let o = 0;
231
+ for (let y = 0; y < pass.height; y++) {
232
+ let i = ((pass.y + y * pass.yStep) * width + pass.x) * 4;
233
+ // For bit depths under 8: the bits waiting to be written
234
+ let byte = 0;
235
+ let bits = 0;
236
+ for (let x = 0; x < pass.width; x++, i += step) {
237
+ if (indices) {
238
+ const shift = source.depth === 16 ? 8 : 0;
239
+ const key = (((data[i] >> shift) << 24) |
240
+ ((data[i + 1] >> shift) << 16) |
241
+ ((data[i + 2] >> shift) << 8) |
242
+ (data[i + 3] >> shift)) >>>
243
+ 0;
244
+ const index = indices.get(key);
245
+ if (index === undefined) {
246
+ throw new Error("The palette doesn't have every color of the image");
247
+ }
248
+ if (depth === 8) {
249
+ out[o++] = index;
250
+ }
251
+ else {
252
+ byte = (byte << depth) | index;
253
+ bits += depth;
254
+ if (bits === 8) {
255
+ out[o++] = byte;
256
+ byte = 0;
257
+ bits = 0;
258
+ }
259
+ }
260
+ continue;
261
+ }
262
+ let r = data[i];
263
+ let g = data[i + 1];
264
+ let b = data[i + 2];
265
+ let a = data[i + 3];
266
+ if (fromSource) {
267
+ r = fromSource(r);
268
+ g = fromSource(g);
269
+ b = fromSource(b);
270
+ a = fromSource(a);
271
+ }
272
+ if (!hasAlpha && a !== max) {
273
+ const f = a / max;
274
+ r = Math.round((1 - f) * bg.red + f * r);
275
+ g = Math.round((1 - f) * bg.green + f * g);
276
+ b = Math.round((1 - f) * bg.blue + f * b);
277
+ }
278
+ switch (colorType) {
279
+ case ColorType.GRAYSCALE:
280
+ case ColorType.GRAYSCALE_ALPHA: {
281
+ let v = r === g && r === b ? r : toGray(r, g, b);
282
+ if (v < 0) {
283
+ v = 0;
284
+ }
285
+ else if (v > max) {
286
+ v = max;
287
+ }
288
+ if (depth < 8) {
289
+ byte = (byte << depth) | Math.round((v * lowMax) / 255);
290
+ bits += depth;
291
+ if (bits === 8) {
292
+ out[o++] = byte;
293
+ byte = 0;
294
+ bits = 0;
295
+ }
296
+ }
297
+ else if (depth === 8) {
298
+ out[o++] = v;
299
+ if (hasAlpha) {
300
+ out[o++] = a;
301
+ }
302
+ }
303
+ else {
304
+ out[o++] = v >> 8;
305
+ out[o++] = v & 0xff;
306
+ if (hasAlpha) {
307
+ out[o++] = a >> 8;
308
+ out[o++] = a & 0xff;
309
+ }
310
+ }
311
+ break;
312
+ }
313
+ default:
314
+ if (depth === 8) {
315
+ out[o++] = r;
316
+ out[o++] = g;
317
+ out[o++] = b;
318
+ if (hasAlpha) {
319
+ out[o++] = a;
320
+ }
321
+ }
322
+ else {
323
+ out[o++] = r >> 8;
324
+ out[o++] = r & 0xff;
325
+ out[o++] = g >> 8;
326
+ out[o++] = g & 0xff;
327
+ out[o++] = b >> 8;
328
+ out[o++] = b & 0xff;
329
+ if (hasAlpha) {
330
+ out[o++] = a >> 8;
331
+ out[o++] = a & 0xff;
332
+ }
333
+ }
334
+ }
335
+ }
336
+ if (bits > 0) {
337
+ out[o++] = byte << (8 - bits);
338
+ }
339
+ }
340
+ return out;
341
+ }
342
+ //# sourceMappingURL=pack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pack.js","sourceRoot":"","sources":["../../src/pack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM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AC,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;4BAClB,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;wBACtB,CAAC;oBACH,CAAC;YACL,CAAC;QACH,CAAC;QAED,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACb,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { ALLOWED_BIT_DEPTHS, ColorType } from \"./constants.js\";\nimport type { BitDepth } from \"./constants.js\";\nimport type { Pass } from \"./interlace.js\";\nimport { getLineBytes } from \"./interlace.js\";\nimport type { PngHeader } from \"./types.js\";\n\n/** RGBA samples, 8 or 16 bits each */\nexport interface PixelSource {\n width: number;\n height: number;\n data: Uint8Array | Uint16Array;\n depth: 8 | 16;\n}\n\n/** What encode() needs to know about the pixels to pick a format */\nexport interface PixelStats {\n /** Every pixel has R = G = B */\n gray: boolean;\n /** Every pixel has full alpha */\n opaque: boolean;\n /** The 16-bit samples all survive a trip through 8 bits */\n fits8: boolean;\n /**\n * The smallest bit depth that stores the gray levels without loss, for\n * opaque gray images\n */\n grayDepth: BitDepth;\n /**\n * The distinct colours, as 0xRRGGBBAA at 8 bits, mapped to the order they\n * appear in. Undefined if there are more than 256.\n */\n colors?: Map<number, number>;\n}\n\n/** Scans the pixels, stopping once nothing more can be learned */\nexport function getPixelStats(source: PixelSource): PixelStats {\n const { data, depth } = source;\n const length = source.width * source.height * 4;\n let gray = true;\n let opaque = true;\n let fits8 = depth === 8;\n // Bit 1, 2 and 4 are set while those gray depths are lossless\n let grayDepths = 1 | 2 | 4;\n let colors: Map<number, number> | undefined = new Map();\n let lastKey = -1;\n const max = depth === 16 ? 65535 : 255;\n\n if (depth === 16) {\n fits8 = true;\n\n for (let i = 0; i < length; i++) {\n if (data[i]! % 257 !== 0) {\n fits8 = false;\n break;\n }\n }\n }\n\n // Colours are counted at 8 bits, which is only lossless if fits8\n const shift = depth === 16 ? 8 : 0;\n\n if (depth === 16 && !fits8) {\n colors = undefined;\n }\n\n for (let i = 0; i < length; i += 4) {\n const r = data[i]!;\n const g = data[i + 1]!;\n const b = data[i + 2]!;\n const a = data[i + 3]!;\n\n if (a !== max) {\n opaque = false;\n }\n\n if (gray) {\n if (r !== g || r !== b) {\n gray = false;\n } else if (grayDepths && depth === 8) {\n if (r % 17 !== 0) {\n grayDepths = 0;\n } else if (r % 85 !== 0) {\n grayDepths &= 4;\n } else if (r !== 0 && r !== 255) {\n grayDepths &= 2 | 4;\n }\n }\n }\n\n if (colors) {\n const key =\n (((r >> shift) << 24) |\n ((g >> shift) << 16) |\n ((b >> shift) << 8) |\n (a >> shift)) >>>\n 0;\n\n if (key !== lastKey) {\n lastKey = key;\n\n if (!colors.has(key)) {\n if (colors.size === 256) {\n colors = undefined;\n } else {\n colors.set(key, colors.size);\n }\n }\n }\n }\n\n if (!gray && !opaque && !colors) {\n break;\n }\n }\n\n let grayDepth: BitDepth = depth === 16 && !fits8 ? 16 : 8;\n\n if (depth === 8 || fits8) {\n if (grayDepths & 1) {\n grayDepth = 1;\n } else if (grayDepths & 2) {\n grayDepth = 2;\n } else if (grayDepths & 4) {\n grayDepth = 4;\n }\n }\n\n // The 16-bit gray check above only ran for 8-bit data\n if (depth === 16 && fits8 && gray) {\n grayDepth = getGrayDepth16(data, length);\n }\n\n return { gray, opaque, fits8, grayDepth, colors };\n}\n\nfunction getGrayDepth16(data: Uint8Array | Uint16Array, length: number) {\n let grayDepths = 1 | 2 | 4;\n\n for (let i = 0; i < length && grayDepths; i += 4) {\n const v = data[i]! / 257;\n\n if (v % 17 !== 0) {\n grayDepths = 0;\n } else if (v % 85 !== 0) {\n grayDepths &= 4;\n } else if (v !== 0 && v !== 255) {\n grayDepths &= 2 | 4;\n }\n }\n\n return grayDepths & 1 ? 1 : grayDepths & 2 ? 2 : grayDepths & 4 ? 4 : 8;\n}\n\nfunction getPaletteDepth(count: number): BitDepth {\n return count <= 2 ? 1 : count <= 4 ? 2 : count <= 16 ? 4 : 8;\n}\n\n/** Picks the colour type and bit depth to write */\nexport function chooseFormat(\n source: PixelSource,\n stats: PixelStats,\n colorType?: ColorType,\n bitDepth?: BitDepth,\n): { colorType: ColorType; depth: BitDepth } {\n const { gray, opaque, colors } = stats;\n const wide = source.depth === 16 && !stats.fits8;\n\n if (colorType === undefined) {\n const direct = gray\n ? opaque\n ? ColorType.GRAYSCALE\n : ColorType.GRAYSCALE_ALPHA\n : opaque\n ? ColorType.COLOR\n : ColorType.COLOR_ALPHA;\n\n if (bitDepth === 16 || wide) {\n colorType = direct;\n } else if (gray && opaque && stats.grayDepth <= (bitDepth ?? 8)) {\n colorType = ColorType.GRAYSCALE;\n } else if (colors && colors.size <= 2 ** (bitDepth ?? 8)) {\n colorType = ColorType.PALETTE;\n } else if (bitDepth === undefined || bitDepth === 8) {\n colorType = direct;\n } else {\n throw new Error(\n `The image can't be stored with a bit depth of ${bitDepth} without loss. Set colorType to convert it.`,\n );\n }\n }\n\n if (bitDepth === undefined) {\n if (colorType === ColorType.PALETTE) {\n if (!colors) {\n throw new Error(\n \"The image has more than 256 colors, so it can't be written as a palette image\",\n );\n }\n\n bitDepth = getPaletteDepth(colors.size);\n } else if (wide) {\n bitDepth = 16;\n } else if (colorType === ColorType.GRAYSCALE && gray && opaque) {\n bitDepth = stats.grayDepth;\n } else {\n bitDepth = 8;\n }\n }\n\n if (!ALLOWED_BIT_DEPTHS[colorType]?.includes(bitDepth)) {\n throw new Error(\n `Bit depth ${bitDepth} isn't allowed for color type ${colorType}`,\n );\n }\n\n if (\n colorType === ColorType.PALETTE &&\n (!colors || colors.size > 2 ** bitDepth)\n ) {\n throw new Error(\n `The image has more than ${2 ** bitDepth} colors, so it can't be written as a palette image with a bit depth of ${bitDepth}`,\n );\n }\n\n return { colorType, depth: bitDepth };\n}\n\n/** The PLTE and tRNS data, and each colour's index */\nexport interface Palette {\n plte: Uint8Array;\n trns?: Uint8Array;\n indices: Map<number, number>;\n}\n\n/**\n * Orders the palette with transparent colours first, so the tRNS chunk can\n * stop at the last of them. Otherwise colours keep the order they appear in.\n */\nexport function buildPalette(colors: Map<number, number>): Palette {\n const keys = [...colors.keys()];\n const sorted = [\n ...keys.filter((key) => (key & 0xff) !== 0xff),\n ...keys.filter((key) => (key & 0xff) === 0xff),\n ];\n const plte = new Uint8Array(sorted.length * 3);\n const alpha: number[] = [];\n const indices = new Map<number, number>();\n\n sorted.forEach((key, i) => {\n plte[i * 3] = key >>> 24;\n plte[i * 3 + 1] = (key >>> 16) & 0xff;\n plte[i * 3 + 2] = (key >>> 8) & 0xff;\n\n if ((key & 0xff) !== 0xff) {\n alpha.push(key & 0xff);\n }\n\n indices.set(key, i);\n });\n\n return {\n plte,\n trns: alpha.length ? new Uint8Array(alpha) : undefined,\n indices,\n };\n}\n\nexport interface PackOptions {\n /** Blends transparent pixels with this when there's no alpha channel */\n background: { red: number; green: number; blue: number };\n palette?: Palette;\n /**\n * Converts colour to gray, on the working scale (8 or 16 bits). Defaults\n * to the mean of R, G and B.\n */\n toGray?: (r: number, g: number, b: number) => number;\n}\n\nfunction defaultGray(r: number, g: number, b: number) {\n return Math.round((r + g + b) / 3);\n}\n\n/**\n * Converts RGBA pixels to the lines of a pass, packed in the PNG's format,\n * without filter bytes.\n */\nexport function packPass(\n source: PixelSource,\n header: PngHeader,\n pass: Pass,\n options: PackOptions,\n) {\n const { data, width } = source;\n const { colorType, depth } = header;\n const lineBytes = getLineBytes(header, pass.width);\n const step = pass.xStep * 4;\n\n // RGBA at 8 bits, not interlaced: the source is already in the format\n if (\n colorType === ColorType.COLOR_ALPHA &&\n depth === 8 &&\n source.depth === 8 &&\n pass.xStep === 1 &&\n pass.yStep === 1\n ) {\n return (data as Uint8Array).subarray(0, lineBytes * pass.height);\n }\n\n const out = new Uint8Array(lineBytes * pass.height);\n\n // Samples are converted to the working depth, 8 or 16 bits, then to the\n // output depth if that's lower\n const working = depth === 16 ? 16 : 8;\n const max = working === 16 ? 65535 : 255;\n const fromSource =\n source.depth === working\n ? undefined\n : source.depth === 8\n ? (v: number) => v * 257\n : (v: number) => ((v + 128) / 257) | 0;\n const bg = options.background;\n const hasAlpha =\n colorType === ColorType.COLOR_ALPHA ||\n colorType === ColorType.GRAYSCALE_ALPHA;\n const toGray = options.toGray ?? defaultGray;\n const indices = options.palette?.indices;\n const lowMax = (1 << depth) - 1;\n let o = 0;\n\n for (let y = 0; y < pass.height; y++) {\n let i = ((pass.y + y * pass.yStep) * width + pass.x) * 4;\n // For bit depths under 8: the bits waiting to be written\n let byte = 0;\n let bits = 0;\n\n for (let x = 0; x < pass.width; x++, i += step) {\n if (indices) {\n const shift = source.depth === 16 ? 8 : 0;\n const key =\n (((data[i]! >> shift) << 24) |\n ((data[i + 1]! >> shift) << 16) |\n ((data[i + 2]! >> shift) << 8) |\n (data[i + 3]! >> shift)) >>>\n 0;\n const index = indices.get(key);\n\n if (index === undefined) {\n throw new Error(\"The palette doesn't have every color of the image\");\n }\n\n if (depth === 8) {\n out[o++] = index;\n } else {\n byte = (byte << depth) | index;\n bits += depth;\n\n if (bits === 8) {\n out[o++] = byte;\n byte = 0;\n bits = 0;\n }\n }\n\n continue;\n }\n\n let r = data[i]!;\n let g = data[i + 1]!;\n let b = data[i + 2]!;\n let a = data[i + 3]!;\n\n if (fromSource) {\n r = fromSource(r);\n g = fromSource(g);\n b = fromSource(b);\n a = fromSource(a);\n }\n\n if (!hasAlpha && a !== max) {\n const f = a / max;\n\n r = Math.round((1 - f) * bg.red + f * r);\n g = Math.round((1 - f) * bg.green + f * g);\n b = Math.round((1 - f) * bg.blue + f * b);\n }\n\n switch (colorType) {\n case ColorType.GRAYSCALE:\n case ColorType.GRAYSCALE_ALPHA: {\n let v = r === g && r === b ? r : toGray(r, g, b);\n\n if (v < 0) {\n v = 0;\n } else if (v > max) {\n v = max;\n }\n\n if (depth < 8) {\n byte = (byte << depth) | Math.round((v * lowMax) / 255);\n bits += depth;\n\n if (bits === 8) {\n out[o++] = byte;\n byte = 0;\n bits = 0;\n }\n } else if (depth === 8) {\n out[o++] = v;\n\n if (hasAlpha) {\n out[o++] = a;\n }\n } else {\n out[o++] = v >> 8;\n out[o++] = v & 0xff;\n\n if (hasAlpha) {\n out[o++] = a >> 8;\n out[o++] = a & 0xff;\n }\n }\n\n break;\n }\n default:\n if (depth === 8) {\n out[o++] = r;\n out[o++] = g;\n out[o++] = b;\n\n if (hasAlpha) {\n out[o++] = a;\n }\n } else {\n out[o++] = r >> 8;\n out[o++] = r & 0xff;\n out[o++] = g >> 8;\n out[o++] = g & 0xff;\n out[o++] = b >> 8;\n out[o++] = b & 0xff;\n\n if (hasAlpha) {\n out[o++] = a >> 8;\n out[o++] = a & 0xff;\n }\n }\n }\n }\n\n if (bits > 0) {\n out[o++] = byte << (8 - bits);\n }\n }\n\n return out;\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }