@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 @@
1
+ {"version":3,"file":"legacy.d.ts","sourceRoot":"","sources":["../../src/legacy.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAgC,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACzE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAI/C,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGtD,OAAO,KAAK,EACV,UAAU,EACV,YAAY,EACZ,kBAAkB,EAClB,OAAO,EACP,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB,6DAA6D;AAC7D,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAEhD,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC/B;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAClC;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACnC;AAED,MAAM,WAAW,aAAa;IAC5B,+CAA+C;IAC/C,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,2EAA2E;IAC3E,eAAe,CAAC,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AW,GAAG,MAAM,EACxC,OAAO,GAAE,aAAkB,GAC1B,eAAe,CA2BjB;AAED,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,UAAU,GAAG,WAAW,CAAC;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AA4ED,gEAAgE;AAChE,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB;;;;;;mBAuDC,SAAS;;;;;YAK/B,GAAG;YACH,KAAK;YACL,IAAI;;;;;QAII,KAAK;;EAEpB;AAED,wBAAgB,WAAW,CACzB,KAAK,EAAE,WAAW,EAClB,OAAO,GAAE,aAAkB,GAC1B,UAAU,CAIZ"}
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMetadata = getMetadata;
4
+ exports.legacyRead = legacyRead;
5
+ exports.prepareLegacyWrite = prepareLegacyWrite;
6
+ exports.legacyWrite = legacyWrite;
7
+ const buffer_js_1 = require("./buffer.js");
8
+ const constants_js_1 = require("./constants.js");
9
+ const decode_js_1 = require("./decode.js");
10
+ const encode_js_1 = require("./encode.js");
11
+ const pack_js_1 = require("./pack.js");
12
+ const pixels_js_1 = require("./pixels.js");
13
+ const read_js_1 = require("./read.js");
14
+ function getMetadata(header, hasTransparency) {
15
+ const { colorType } = header;
16
+ return {
17
+ width: header.width,
18
+ height: header.height,
19
+ depth: header.depth,
20
+ interlace: header.interlace,
21
+ palette: colorType === constants_js_1.ColorType.PALETTE,
22
+ color: (colorType & 2) !== 0,
23
+ alpha: (colorType & 4) !== 0 || hasTransparency,
24
+ bpp: constants_js_1.CHANNELS[colorType],
25
+ colorType,
26
+ };
27
+ }
28
+ function legacyRead(input, options = {}) {
29
+ const bytes = typeof input === "string"
30
+ ? new TextEncoder().encode(input)
31
+ : (0, buffer_js_1.toUint8Array)(input);
32
+ const file = (0, read_js_1.readPng)(bytes, { checkCRC: options.checkCRC !== false });
33
+ const indexed = options.keepIndexed && file.header.colorType === constants_js_1.ColorType.PALETTE;
34
+ const data = (0, pixels_js_1.decodePixels)({
35
+ ...file.header,
36
+ palette: file.palette,
37
+ transparentColor: file.transparentColor,
38
+ }, file.idat, indexed ? "indexed" : options.skipRescale ? "raw" : 8);
39
+ const info = (0, decode_js_1.getDecodedInfo)(file);
40
+ return {
41
+ ...info,
42
+ ...getMetadata(file.header, file.trns !== undefined),
43
+ data: (data instanceof Uint8Array ? (0, buffer_js_1.toBuffer)(data) : data),
44
+ gamma: file.gamma ?? 0,
45
+ palette: file.palette ?? false,
46
+ transColor: file.transparentColor,
47
+ };
48
+ }
49
+ const INPUT_COLOR_TYPES = [0, 2, 4, 6];
50
+ const OUTPUT_COLOR_TYPES = [0, 2, 3, 4, 6];
51
+ /** Reads 16-bit samples in the platform's byte order */
52
+ function asUint16Array(data) {
53
+ if (data instanceof Uint16Array) {
54
+ return data;
55
+ }
56
+ // A Buffer from Node's pool may start at an odd offset, so copy it then
57
+ if (data.byteOffset % 2 === 0) {
58
+ return new Uint16Array(data.buffer, data.byteOffset, data.byteLength >> 1);
59
+ }
60
+ return new Uint16Array(data.slice().buffer, 0, data.byteLength >> 1);
61
+ }
62
+ /**
63
+ * Converts the input pixels to RGBA, which the encoder takes. pngjs only
64
+ * reads input as RGBA unless `inputColorType` or `inputHasAlpha` says
65
+ * otherwise.
66
+ */
67
+ function toRgba(image, inputColorType, inputHasAlpha, sixteenBit) {
68
+ const { width, height } = image;
69
+ const input = sixteenBit ? asUint16Array(image.data) : image.data;
70
+ const max = sixteenBit ? 65535 : 255;
71
+ let channels = constants_js_1.CHANNELS[inputColorType];
72
+ // pngjs: RGBA input without alpha is RGB
73
+ if (inputColorType === 6 && !inputHasAlpha) {
74
+ channels = 3;
75
+ }
76
+ if (input.length < width * height * channels) {
77
+ throw new Error(`Not enough pixel data: a ${width}x${height} image needs ${width * height * channels} samples, got ${input.length}`);
78
+ }
79
+ if (channels === 4) {
80
+ return { width, height, data: input, depth: sixteenBit ? 16 : 8 };
81
+ }
82
+ const length = width * height;
83
+ const data = sixteenBit
84
+ ? new Uint16Array(length * 4)
85
+ : new Uint8Array(length * 4);
86
+ for (let p = 0, i = 0, o = 0; p < length; p++, i += channels, o += 4) {
87
+ switch (channels) {
88
+ case 1:
89
+ data[o] = data[o + 1] = data[o + 2] = input[i];
90
+ data[o + 3] = max;
91
+ break;
92
+ case 2:
93
+ data[o] = data[o + 1] = data[o + 2] = input[i];
94
+ data[o + 3] = input[i + 1];
95
+ break;
96
+ default:
97
+ data[o] = input[i];
98
+ data[o + 1] = input[i + 1];
99
+ data[o + 2] = input[i + 2];
100
+ data[o + 3] = max;
101
+ }
102
+ }
103
+ return { width, height, data, depth: sixteenBit ? 16 : 8 };
104
+ }
105
+ /** Everything needed to write a PNG, short of compressing it */
106
+ function prepareLegacyWrite(image, options = {}) {
107
+ const { colorType = 6, inputColorType = 6, inputHasAlpha = true, bitDepth = 8, } = options;
108
+ if (!OUTPUT_COLOR_TYPES.includes(colorType)) {
109
+ throw new Error(`option color type:${colorType} is not supported at present`);
110
+ }
111
+ if (!INPUT_COLOR_TYPES.includes(inputColorType)) {
112
+ throw new Error(`option input color type:${inputColorType} is not supported at present`);
113
+ }
114
+ if (![1, 2, 4, 8, 16].includes(bitDepth)) {
115
+ throw new Error(`option bit depth:${bitDepth} is not supported at present`);
116
+ }
117
+ const sixteenBit = bitDepth === 16;
118
+ const source = toRgba(image, inputColorType, inputHasAlpha, sixteenBit);
119
+ (0, encode_js_1.validateImage)(source);
120
+ let depth = bitDepth;
121
+ let palette;
122
+ if (colorType === constants_js_1.ColorType.PALETTE) {
123
+ const stats = (0, pack_js_1.getPixelStats)(source);
124
+ // Without a bitDepth, use the smallest that indexes every colour
125
+ depth = (0, pack_js_1.chooseFormat)(source, stats, colorType, options.bitDepth).depth;
126
+ palette = (0, pack_js_1.buildPalette)(stats.colors);
127
+ }
128
+ else if (!constants_js_1.ALLOWED_BIT_DEPTHS[colorType].includes(bitDepth)) {
129
+ throw new Error(`option bit depth:${bitDepth} is not supported for color type ${colorType}`);
130
+ }
131
+ const max = sixteenBit ? 65535 : 255;
132
+ const bg = options.bgColor ?? {};
133
+ return {
134
+ source,
135
+ header: {
136
+ width: image.width,
137
+ height: image.height,
138
+ depth,
139
+ colorType: colorType,
140
+ interlace: false,
141
+ },
142
+ pack: {
143
+ background: {
144
+ red: bg.red ?? max,
145
+ green: bg.green ?? max,
146
+ blue: bg.blue ?? max,
147
+ },
148
+ palette,
149
+ },
150
+ metadata: { gamma: image.gamma || undefined },
151
+ };
152
+ }
153
+ function legacyWrite(image, options = {}) {
154
+ const { source, header, pack, metadata } = prepareLegacyWrite(image, options);
155
+ return (0, buffer_js_1.toBuffer)((0, encode_js_1.writePng)(source, header, pack, metadata, options));
156
+ }
157
+ //# sourceMappingURL=legacy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"legacy.js","sourceRoot":"","sources":["../../src/legacy.ts"],"names":[],"mappings":";;;;;;AAEA,2CAAqD;AACrD,iDAAyE;AAEzE,2CAA6C;AAC7C,2CAAsD;AACtD,uCAAsE;AAEtE,2CAA2C;AAC3C,uCAAoC;AAyHpC,qBACE,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,wBAAS,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,uB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G,IAAI,GAAG;gBAClB,KAAK,EAAE,EAAE,CAAC,KAAK,IAAI,GAAG;gBACtB,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG;aACrB;YACD,OAAO;SACR;QACD,QAAQ,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,SAAS,EAAE;KAC9C,CAAC;AACJ,CAAC;AAED,qBACE,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,IAAA,oBAAQ,EAAC,IAAA,oBAAQ,EAAC,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,348 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPixelStats = getPixelStats;
4
+ exports.chooseFormat = chooseFormat;
5
+ exports.buildPalette = buildPalette;
6
+ exports.packPass = packPass;
7
+ const constants_js_1 = require("./constants.js");
8
+ const interlace_js_1 = require("./interlace.js");
9
+ /** Scans the pixels, stopping once nothing more can be learned */
10
+ function getPixelStats(source) {
11
+ const { data, depth } = source;
12
+ const length = source.width * source.height * 4;
13
+ let gray = true;
14
+ let opaque = true;
15
+ let fits8 = depth === 8;
16
+ // Bit 1, 2 and 4 are set while those gray depths are lossless
17
+ let grayDepths = 1 | 2 | 4;
18
+ let colors = new Map();
19
+ let lastKey = -1;
20
+ const max = depth === 16 ? 65535 : 255;
21
+ if (depth === 16) {
22
+ fits8 = true;
23
+ for (let i = 0; i < length; i++) {
24
+ if (data[i] % 257 !== 0) {
25
+ fits8 = false;
26
+ break;
27
+ }
28
+ }
29
+ }
30
+ // Colours are counted at 8 bits, which is only lossless if fits8
31
+ const shift = depth === 16 ? 8 : 0;
32
+ if (depth === 16 && !fits8) {
33
+ colors = undefined;
34
+ }
35
+ for (let i = 0; i < length; i += 4) {
36
+ const r = data[i];
37
+ const g = data[i + 1];
38
+ const b = data[i + 2];
39
+ const a = data[i + 3];
40
+ if (a !== max) {
41
+ opaque = false;
42
+ }
43
+ if (gray) {
44
+ if (r !== g || r !== b) {
45
+ gray = false;
46
+ }
47
+ else if (grayDepths && depth === 8) {
48
+ if (r % 17 !== 0) {
49
+ grayDepths = 0;
50
+ }
51
+ else if (r % 85 !== 0) {
52
+ grayDepths &= 4;
53
+ }
54
+ else if (r !== 0 && r !== 255) {
55
+ grayDepths &= 2 | 4;
56
+ }
57
+ }
58
+ }
59
+ if (colors) {
60
+ const key = (((r >> shift) << 24) |
61
+ ((g >> shift) << 16) |
62
+ ((b >> shift) << 8) |
63
+ (a >> shift)) >>>
64
+ 0;
65
+ if (key !== lastKey) {
66
+ lastKey = key;
67
+ if (!colors.has(key)) {
68
+ if (colors.size === 256) {
69
+ colors = undefined;
70
+ }
71
+ else {
72
+ colors.set(key, colors.size);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ if (!gray && !opaque && !colors) {
78
+ break;
79
+ }
80
+ }
81
+ let grayDepth = depth === 16 && !fits8 ? 16 : 8;
82
+ if (depth === 8 || fits8) {
83
+ if (grayDepths & 1) {
84
+ grayDepth = 1;
85
+ }
86
+ else if (grayDepths & 2) {
87
+ grayDepth = 2;
88
+ }
89
+ else if (grayDepths & 4) {
90
+ grayDepth = 4;
91
+ }
92
+ }
93
+ // The 16-bit gray check above only ran for 8-bit data
94
+ if (depth === 16 && fits8 && gray) {
95
+ grayDepth = getGrayDepth16(data, length);
96
+ }
97
+ return { gray, opaque, fits8, grayDepth, colors };
98
+ }
99
+ function getGrayDepth16(data, length) {
100
+ let grayDepths = 1 | 2 | 4;
101
+ for (let i = 0; i < length && grayDepths; i += 4) {
102
+ const v = data[i] / 257;
103
+ if (v % 17 !== 0) {
104
+ grayDepths = 0;
105
+ }
106
+ else if (v % 85 !== 0) {
107
+ grayDepths &= 4;
108
+ }
109
+ else if (v !== 0 && v !== 255) {
110
+ grayDepths &= 2 | 4;
111
+ }
112
+ }
113
+ return grayDepths & 1 ? 1 : grayDepths & 2 ? 2 : grayDepths & 4 ? 4 : 8;
114
+ }
115
+ function getPaletteDepth(count) {
116
+ return count <= 2 ? 1 : count <= 4 ? 2 : count <= 16 ? 4 : 8;
117
+ }
118
+ /** Picks the colour type and bit depth to write */
119
+ function chooseFormat(source, stats, colorType, bitDepth) {
120
+ const { gray, opaque, colors } = stats;
121
+ const wide = source.depth === 16 && !stats.fits8;
122
+ if (colorType === undefined) {
123
+ const direct = gray
124
+ ? opaque
125
+ ? constants_js_1.ColorType.GRAYSCALE
126
+ : constants_js_1.ColorType.GRAYSCALE_ALPHA
127
+ : opaque
128
+ ? constants_js_1.ColorType.COLOR
129
+ : constants_js_1.ColorType.COLOR_ALPHA;
130
+ if (bitDepth === 16 || wide) {
131
+ colorType = direct;
132
+ }
133
+ else if (gray && opaque && stats.grayDepth <= (bitDepth ?? 8)) {
134
+ colorType = constants_js_1.ColorType.GRAYSCALE;
135
+ }
136
+ else if (colors && colors.size <= 2 ** (bitDepth ?? 8)) {
137
+ colorType = constants_js_1.ColorType.PALETTE;
138
+ }
139
+ else if (bitDepth === undefined || bitDepth === 8) {
140
+ colorType = direct;
141
+ }
142
+ else {
143
+ throw new Error(`The image can't be stored with a bit depth of ${bitDepth} without loss. Set colorType to convert it.`);
144
+ }
145
+ }
146
+ if (bitDepth === undefined) {
147
+ if (colorType === constants_js_1.ColorType.PALETTE) {
148
+ if (!colors) {
149
+ throw new Error("The image has more than 256 colors, so it can't be written as a palette image");
150
+ }
151
+ bitDepth = getPaletteDepth(colors.size);
152
+ }
153
+ else if (wide) {
154
+ bitDepth = 16;
155
+ }
156
+ else if (colorType === constants_js_1.ColorType.GRAYSCALE && gray && opaque) {
157
+ bitDepth = stats.grayDepth;
158
+ }
159
+ else {
160
+ bitDepth = 8;
161
+ }
162
+ }
163
+ if (!constants_js_1.ALLOWED_BIT_DEPTHS[colorType]?.includes(bitDepth)) {
164
+ throw new Error(`Bit depth ${bitDepth} isn't allowed for color type ${colorType}`);
165
+ }
166
+ if (colorType === constants_js_1.ColorType.PALETTE &&
167
+ (!colors || colors.size > 2 ** bitDepth)) {
168
+ 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}`);
169
+ }
170
+ return { colorType, depth: bitDepth };
171
+ }
172
+ /**
173
+ * Orders the palette with transparent colours first, so the tRNS chunk can
174
+ * stop at the last of them. Otherwise colours keep the order they appear in.
175
+ */
176
+ function buildPalette(colors) {
177
+ const keys = [...colors.keys()];
178
+ const sorted = [
179
+ ...keys.filter((key) => (key & 0xff) !== 0xff),
180
+ ...keys.filter((key) => (key & 0xff) === 0xff),
181
+ ];
182
+ const plte = new Uint8Array(sorted.length * 3);
183
+ const alpha = [];
184
+ const indices = new Map();
185
+ sorted.forEach((key, i) => {
186
+ plte[i * 3] = key >>> 24;
187
+ plte[i * 3 + 1] = (key >>> 16) & 0xff;
188
+ plte[i * 3 + 2] = (key >>> 8) & 0xff;
189
+ if ((key & 0xff) !== 0xff) {
190
+ alpha.push(key & 0xff);
191
+ }
192
+ indices.set(key, i);
193
+ });
194
+ return {
195
+ plte,
196
+ trns: alpha.length ? new Uint8Array(alpha) : undefined,
197
+ indices,
198
+ };
199
+ }
200
+ function defaultGray(r, g, b) {
201
+ return Math.round((r + g + b) / 3);
202
+ }
203
+ /**
204
+ * Converts RGBA pixels to the lines of a pass, packed in the PNG's format,
205
+ * without filter bytes.
206
+ */
207
+ function packPass(source, header, pass, options) {
208
+ const { data, width } = source;
209
+ const { colorType, depth } = header;
210
+ const lineBytes = (0, interlace_js_1.getLineBytes)(header, pass.width);
211
+ const step = pass.xStep * 4;
212
+ // RGBA at 8 bits, not interlaced: the source is already in the format
213
+ if (colorType === constants_js_1.ColorType.COLOR_ALPHA &&
214
+ depth === 8 &&
215
+ source.depth === 8 &&
216
+ pass.xStep === 1 &&
217
+ pass.yStep === 1) {
218
+ return data.subarray(0, lineBytes * pass.height);
219
+ }
220
+ const out = new Uint8Array(lineBytes * pass.height);
221
+ // Samples are converted to the working depth, 8 or 16 bits, then to the
222
+ // output depth if that's lower
223
+ const working = depth === 16 ? 16 : 8;
224
+ const max = working === 16 ? 65535 : 255;
225
+ const fromSource = source.depth === working
226
+ ? undefined
227
+ : source.depth === 8
228
+ ? (v) => v * 257
229
+ : (v) => ((v + 128) / 257) | 0;
230
+ const bg = options.background;
231
+ const hasAlpha = colorType === constants_js_1.ColorType.COLOR_ALPHA ||
232
+ colorType === constants_js_1.ColorType.GRAYSCALE_ALPHA;
233
+ const toGray = options.toGray ?? defaultGray;
234
+ const indices = options.palette?.indices;
235
+ const lowMax = (1 << depth) - 1;
236
+ let o = 0;
237
+ for (let y = 0; y < pass.height; y++) {
238
+ let i = ((pass.y + y * pass.yStep) * width + pass.x) * 4;
239
+ // For bit depths under 8: the bits waiting to be written
240
+ let byte = 0;
241
+ let bits = 0;
242
+ for (let x = 0; x < pass.width; x++, i += step) {
243
+ if (indices) {
244
+ const shift = source.depth === 16 ? 8 : 0;
245
+ const key = (((data[i] >> shift) << 24) |
246
+ ((data[i + 1] >> shift) << 16) |
247
+ ((data[i + 2] >> shift) << 8) |
248
+ (data[i + 3] >> shift)) >>>
249
+ 0;
250
+ const index = indices.get(key);
251
+ if (index === undefined) {
252
+ throw new Error("The palette doesn't have every color of the image");
253
+ }
254
+ if (depth === 8) {
255
+ out[o++] = index;
256
+ }
257
+ else {
258
+ byte = (byte << depth) | index;
259
+ bits += depth;
260
+ if (bits === 8) {
261
+ out[o++] = byte;
262
+ byte = 0;
263
+ bits = 0;
264
+ }
265
+ }
266
+ continue;
267
+ }
268
+ let r = data[i];
269
+ let g = data[i + 1];
270
+ let b = data[i + 2];
271
+ let a = data[i + 3];
272
+ if (fromSource) {
273
+ r = fromSource(r);
274
+ g = fromSource(g);
275
+ b = fromSource(b);
276
+ a = fromSource(a);
277
+ }
278
+ if (!hasAlpha && a !== max) {
279
+ const f = a / max;
280
+ r = Math.round((1 - f) * bg.red + f * r);
281
+ g = Math.round((1 - f) * bg.green + f * g);
282
+ b = Math.round((1 - f) * bg.blue + f * b);
283
+ }
284
+ switch (colorType) {
285
+ case constants_js_1.ColorType.GRAYSCALE:
286
+ case constants_js_1.ColorType.GRAYSCALE_ALPHA: {
287
+ let v = r === g && r === b ? r : toGray(r, g, b);
288
+ if (v < 0) {
289
+ v = 0;
290
+ }
291
+ else if (v > max) {
292
+ v = max;
293
+ }
294
+ if (depth < 8) {
295
+ byte = (byte << depth) | Math.round((v * lowMax) / 255);
296
+ bits += depth;
297
+ if (bits === 8) {
298
+ out[o++] = byte;
299
+ byte = 0;
300
+ bits = 0;
301
+ }
302
+ }
303
+ else if (depth === 8) {
304
+ out[o++] = v;
305
+ if (hasAlpha) {
306
+ out[o++] = a;
307
+ }
308
+ }
309
+ else {
310
+ out[o++] = v >> 8;
311
+ out[o++] = v & 0xff;
312
+ if (hasAlpha) {
313
+ out[o++] = a >> 8;
314
+ out[o++] = a & 0xff;
315
+ }
316
+ }
317
+ break;
318
+ }
319
+ default:
320
+ if (depth === 8) {
321
+ out[o++] = r;
322
+ out[o++] = g;
323
+ out[o++] = b;
324
+ if (hasAlpha) {
325
+ out[o++] = a;
326
+ }
327
+ }
328
+ else {
329
+ out[o++] = r >> 8;
330
+ out[o++] = r & 0xff;
331
+ out[o++] = g >> 8;
332
+ out[o++] = g & 0xff;
333
+ out[o++] = b >> 8;
334
+ out[o++] = b & 0xff;
335
+ if (hasAlpha) {
336
+ out[o++] = a >> 8;
337
+ out[o++] = a & 0xff;
338
+ }
339
+ }
340
+ }
341
+ }
342
+ if (bits > 0) {
343
+ out[o++] = byte << (8 - bits);
344
+ }
345
+ }
346
+ return out;
347
+ }
348
+ //# sourceMappingURL=pack.js.map