ag-psd 30.2.0 → 31.0.1

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 (252) hide show
  1. package/.v8-cache/v24.13.1-x64-cf738c9d/01e5e61b +0 -0
  2. package/.v8-cache/v24.13.1-x64-cf738c9d/05135617 +0 -0
  3. package/.v8-cache/v24.13.1-x64-cf738c9d/06423db9 +0 -0
  4. package/.v8-cache/v24.13.1-x64-cf738c9d/06e765df +0 -0
  5. package/.v8-cache/v24.13.1-x64-cf738c9d/0930679d +0 -0
  6. package/.v8-cache/v24.13.1-x64-cf738c9d/09519a62 +0 -0
  7. package/.v8-cache/v24.13.1-x64-cf738c9d/0a6607b0 +0 -0
  8. package/.v8-cache/v24.13.1-x64-cf738c9d/0ad8fa6e +0 -0
  9. package/.v8-cache/v24.13.1-x64-cf738c9d/0d390e10 +0 -0
  10. package/.v8-cache/v24.13.1-x64-cf738c9d/0dfa7b16 +0 -0
  11. package/.v8-cache/v24.13.1-x64-cf738c9d/0e9e09db +0 -0
  12. package/.v8-cache/v24.13.1-x64-cf738c9d/1130885f +0 -0
  13. package/.v8-cache/v24.13.1-x64-cf738c9d/1222cb8c +0 -0
  14. package/.v8-cache/v24.13.1-x64-cf738c9d/15e2c039 +0 -0
  15. package/.v8-cache/v24.13.1-x64-cf738c9d/1a2d469d +0 -0
  16. package/.v8-cache/v24.13.1-x64-cf738c9d/1b300c82 +0 -0
  17. package/.v8-cache/v24.13.1-x64-cf738c9d/1bc3d370 +0 -0
  18. package/.v8-cache/v24.13.1-x64-cf738c9d/1e44e5af +0 -0
  19. package/.v8-cache/v24.13.1-x64-cf738c9d/1efa10a1 +0 -0
  20. package/.v8-cache/v24.13.1-x64-cf738c9d/1f6c8743 +0 -0
  21. package/.v8-cache/v24.13.1-x64-cf738c9d/20b27e40 +0 -0
  22. package/.v8-cache/v24.13.1-x64-cf738c9d/257c62ba +0 -0
  23. package/.v8-cache/v24.13.1-x64-cf738c9d/25d60154 +0 -0
  24. package/.v8-cache/v24.13.1-x64-cf738c9d/26f822f3 +0 -0
  25. package/.v8-cache/v24.13.1-x64-cf738c9d/2a1e2a8d +0 -0
  26. package/.v8-cache/v24.13.1-x64-cf738c9d/2b02fded +0 -0
  27. package/.v8-cache/v24.13.1-x64-cf738c9d/2c76dc0d +0 -0
  28. package/.v8-cache/v24.13.1-x64-cf738c9d/2deb286a +0 -0
  29. package/.v8-cache/v24.13.1-x64-cf738c9d/32018e44 +0 -0
  30. package/.v8-cache/v24.13.1-x64-cf738c9d/341402c3 +0 -0
  31. package/.v8-cache/v24.13.1-x64-cf738c9d/34e0fa2e +0 -0
  32. package/.v8-cache/v24.13.1-x64-cf738c9d/3bbf643e +0 -0
  33. package/.v8-cache/v24.13.1-x64-cf738c9d/3ceff89e +0 -0
  34. package/.v8-cache/v24.13.1-x64-cf738c9d/404f5cdc +0 -0
  35. package/.v8-cache/v24.13.1-x64-cf738c9d/4087aa2b +0 -0
  36. package/.v8-cache/v24.13.1-x64-cf738c9d/4376c1f9 +0 -0
  37. package/.v8-cache/v24.13.1-x64-cf738c9d/44903df4 +0 -0
  38. package/.v8-cache/v24.13.1-x64-cf738c9d/44d58dc8 +0 -0
  39. package/.v8-cache/v24.13.1-x64-cf738c9d/4511bacf +0 -0
  40. package/.v8-cache/v24.13.1-x64-cf738c9d/47da0ce8 +0 -0
  41. package/.v8-cache/v24.13.1-x64-cf738c9d/48cd54a9 +0 -0
  42. package/.v8-cache/v24.13.1-x64-cf738c9d/4d938476 +0 -0
  43. package/.v8-cache/v24.13.1-x64-cf738c9d/4ec6d390 +0 -0
  44. package/.v8-cache/v24.13.1-x64-cf738c9d/4f5702bd +0 -0
  45. package/.v8-cache/v24.13.1-x64-cf738c9d/514cc428 +0 -0
  46. package/.v8-cache/v24.13.1-x64-cf738c9d/557d3b42 +0 -0
  47. package/.v8-cache/v24.13.1-x64-cf738c9d/56abbff7 +0 -0
  48. package/.v8-cache/v24.13.1-x64-cf738c9d/57b540ee +0 -0
  49. package/.v8-cache/v24.13.1-x64-cf738c9d/5e20c081 +0 -0
  50. package/.v8-cache/v24.13.1-x64-cf738c9d/651c4ada +0 -0
  51. package/.v8-cache/v24.13.1-x64-cf738c9d/652a0d59 +0 -0
  52. package/.v8-cache/v24.13.1-x64-cf738c9d/6a0e89ee +0 -0
  53. package/.v8-cache/v24.13.1-x64-cf738c9d/6af33b91 +0 -0
  54. package/.v8-cache/v24.13.1-x64-cf738c9d/6c2562d2 +0 -0
  55. package/.v8-cache/v24.13.1-x64-cf738c9d/6dae0cfa +0 -0
  56. package/.v8-cache/v24.13.1-x64-cf738c9d/6e2529ce +0 -0
  57. package/.v8-cache/v24.13.1-x64-cf738c9d/71df9cb3 +0 -0
  58. package/.v8-cache/v24.13.1-x64-cf738c9d/72e1d543 +0 -0
  59. package/.v8-cache/v24.13.1-x64-cf738c9d/75519c80 +0 -0
  60. package/.v8-cache/v24.13.1-x64-cf738c9d/79bf115c +0 -0
  61. package/.v8-cache/v24.13.1-x64-cf738c9d/7a39347c +0 -0
  62. package/.v8-cache/v24.13.1-x64-cf738c9d/7b83e3d3 +0 -0
  63. package/.v8-cache/v24.13.1-x64-cf738c9d/7cd71bc5 +0 -0
  64. package/.v8-cache/v24.13.1-x64-cf738c9d/7da78512 +0 -0
  65. package/.v8-cache/v24.13.1-x64-cf738c9d/7ea06cea +0 -0
  66. package/.v8-cache/v24.13.1-x64-cf738c9d/82c3b573 +0 -0
  67. package/.v8-cache/v24.13.1-x64-cf738c9d/84c186e4 +0 -0
  68. package/.v8-cache/v24.13.1-x64-cf738c9d/8607bd36 +0 -0
  69. package/.v8-cache/v24.13.1-x64-cf738c9d/87372817 +0 -0
  70. package/.v8-cache/v24.13.1-x64-cf738c9d/87cf2209 +0 -0
  71. package/.v8-cache/v24.13.1-x64-cf738c9d/882aaa05 +0 -0
  72. package/.v8-cache/v24.13.1-x64-cf738c9d/8a4d3de8 +0 -0
  73. package/.v8-cache/v24.13.1-x64-cf738c9d/8e223473 +0 -0
  74. package/.v8-cache/v24.13.1-x64-cf738c9d/92b7aefd +0 -0
  75. package/.v8-cache/v24.13.1-x64-cf738c9d/932f0770 +0 -0
  76. package/.v8-cache/v24.13.1-x64-cf738c9d/937c03a1 +0 -0
  77. package/.v8-cache/v24.13.1-x64-cf738c9d/991e4758 +0 -0
  78. package/.v8-cache/v24.13.1-x64-cf738c9d/9af658fb +0 -0
  79. package/.v8-cache/v24.13.1-x64-cf738c9d/9ed80ba5 +0 -0
  80. package/.v8-cache/v24.13.1-x64-cf738c9d/9f0dc993 +0 -0
  81. package/.v8-cache/v24.13.1-x64-cf738c9d/a19c0c4f +0 -0
  82. package/.v8-cache/v24.13.1-x64-cf738c9d/a6409d95 +0 -0
  83. package/.v8-cache/v24.13.1-x64-cf738c9d/a7e80865 +0 -0
  84. package/.v8-cache/v24.13.1-x64-cf738c9d/ac9802c6 +0 -0
  85. package/.v8-cache/v24.13.1-x64-cf738c9d/acc36e66 +0 -0
  86. package/.v8-cache/v24.13.1-x64-cf738c9d/ad11e2dc +0 -0
  87. package/.v8-cache/v24.13.1-x64-cf738c9d/b0c8fcd8 +0 -0
  88. package/.v8-cache/v24.13.1-x64-cf738c9d/b1762958 +0 -0
  89. package/.v8-cache/v24.13.1-x64-cf738c9d/b3c2fab7 +0 -0
  90. package/.v8-cache/v24.13.1-x64-cf738c9d/b5b3329b +0 -0
  91. package/.v8-cache/v24.13.1-x64-cf738c9d/b806679e +0 -0
  92. package/.v8-cache/v24.13.1-x64-cf738c9d/bc1cdd48 +0 -0
  93. package/.v8-cache/v24.13.1-x64-cf738c9d/bc7ebd1d +0 -0
  94. package/.v8-cache/v24.13.1-x64-cf738c9d/c0b789d2 +0 -0
  95. package/.v8-cache/v24.13.1-x64-cf738c9d/c314aece +0 -0
  96. package/.v8-cache/v24.13.1-x64-cf738c9d/c399a5dc +0 -0
  97. package/.v8-cache/v24.13.1-x64-cf738c9d/c681cbc8 +0 -0
  98. package/.v8-cache/v24.13.1-x64-cf738c9d/c6834ffc +0 -0
  99. package/.v8-cache/v24.13.1-x64-cf738c9d/c6f12ed5 +0 -0
  100. package/.v8-cache/v24.13.1-x64-cf738c9d/cb0f0a3d +0 -0
  101. package/.v8-cache/v24.13.1-x64-cf738c9d/cbcb7c95 +0 -0
  102. package/.v8-cache/v24.13.1-x64-cf738c9d/cdd9107c +0 -0
  103. package/.v8-cache/v24.13.1-x64-cf738c9d/cdf2c4a3 +0 -0
  104. package/.v8-cache/v24.13.1-x64-cf738c9d/cfc49f4f +0 -0
  105. package/.v8-cache/v24.13.1-x64-cf738c9d/cff9434a +0 -0
  106. package/.v8-cache/v24.13.1-x64-cf738c9d/d02dd6f7 +0 -0
  107. package/.v8-cache/v24.13.1-x64-cf738c9d/d0507bf3 +0 -0
  108. package/.v8-cache/v24.13.1-x64-cf738c9d/d60fce19 +0 -0
  109. package/.v8-cache/v24.13.1-x64-cf738c9d/d733d619 +0 -0
  110. package/.v8-cache/v24.13.1-x64-cf738c9d/d75b7803 +0 -0
  111. package/.v8-cache/v24.13.1-x64-cf738c9d/dc86f000 +0 -0
  112. package/.v8-cache/v24.13.1-x64-cf738c9d/defa4f7f +0 -0
  113. package/.v8-cache/v24.13.1-x64-cf738c9d/e03e2acd +0 -0
  114. package/.v8-cache/v24.13.1-x64-cf738c9d/e0e3f52c +0 -0
  115. package/.v8-cache/v24.13.1-x64-cf738c9d/e158d332 +0 -0
  116. package/.v8-cache/v24.13.1-x64-cf738c9d/e6735171 +0 -0
  117. package/.v8-cache/v24.13.1-x64-cf738c9d/e6745c2b +0 -0
  118. package/.v8-cache/v24.13.1-x64-cf738c9d/e6b99945 +0 -0
  119. package/.v8-cache/v24.13.1-x64-cf738c9d/e6d8ceb8 +0 -0
  120. package/.v8-cache/v24.13.1-x64-cf738c9d/e7ffec37 +0 -0
  121. package/.v8-cache/v24.13.1-x64-cf738c9d/ea33b6de +0 -0
  122. package/.v8-cache/v24.13.1-x64-cf738c9d/eaafa3c4 +0 -0
  123. package/.v8-cache/v24.13.1-x64-cf738c9d/ebb50fca +0 -0
  124. package/.v8-cache/v24.13.1-x64-cf738c9d/ec805f2e +0 -0
  125. package/.v8-cache/v24.13.1-x64-cf738c9d/edaef8f1 +0 -0
  126. package/.v8-cache/v24.13.1-x64-cf738c9d/ee4401c8 +0 -0
  127. package/.v8-cache/v24.13.1-x64-cf738c9d/eeeebdfe +0 -0
  128. package/.v8-cache/v24.13.1-x64-cf738c9d/f3d51676 +0 -0
  129. package/.v8-cache/v24.13.1-x64-cf738c9d/f7b50a75 +0 -0
  130. package/.v8-cache/v24.13.1-x64-cf738c9d/fa58ca72 +0 -0
  131. package/.v8-cache/v24.13.1-x64-cf738c9d/fc97d72d +0 -0
  132. package/.v8-cache/v24.13.1-x64-cf738c9d/fd0a2987 +0 -0
  133. package/CHANGELOG.md +9 -0
  134. package/README.md +64 -0
  135. package/TODO.md +2 -0
  136. package/dist/abr.d.ts +196 -196
  137. package/dist/abr.js +364 -365
  138. package/dist/abr.js.map +1 -1
  139. package/dist/additionalInfo.d.ts +26 -26
  140. package/dist/additionalInfo.js +3478 -3478
  141. package/dist/additionalInfo.js.map +1 -1
  142. package/dist/ase.d.ts +32 -32
  143. package/dist/ase.js +81 -82
  144. package/dist/ase.js.map +1 -1
  145. package/dist/bundle.js +12380 -12259
  146. package/dist/csh.d.ts +10 -10
  147. package/dist/csh.js +35 -36
  148. package/dist/csh.js.map +1 -1
  149. package/dist/descriptor.d.ts +601 -601
  150. package/dist/descriptor.js +2003 -2003
  151. package/dist/descriptor.js.map +1 -1
  152. package/dist/effectsHelpers.d.ts +5 -5
  153. package/dist/effectsHelpers.js +286 -287
  154. package/dist/effectsHelpers.js.map +1 -1
  155. package/dist/engineData.d.ts +2 -2
  156. package/dist/engineData.js +334 -335
  157. package/dist/engineData.js.map +1 -1
  158. package/dist/engineData2.d.ts +2 -2
  159. package/dist/engineData2.js +409 -410
  160. package/dist/engineData2.js.map +1 -1
  161. package/dist/helpers.d.ts +74 -74
  162. package/dist/helpers.js +353 -342
  163. package/dist/helpers.js.map +1 -1
  164. package/dist/imageResources.d.ts +17 -17
  165. package/dist/imageResources.js +1040 -1040
  166. package/dist/imageResources.js.map +1 -1
  167. package/dist/index.d.ts +20 -22
  168. package/dist/index.js +61 -56
  169. package/dist/index.js.map +1 -1
  170. package/dist/initializeCanvas.d.ts +1 -1
  171. package/dist/initializeCanvas.js +9 -10
  172. package/dist/initializeCanvas.js.map +1 -1
  173. package/dist/jpeg.d.ts +1 -1
  174. package/dist/jpeg.js +1020 -1021
  175. package/dist/jpeg.js.map +1 -1
  176. package/dist/psd.d.ts +1877 -1873
  177. package/dist/psd.js +45 -45
  178. package/dist/psd.js.map +1 -1
  179. package/dist/psdReader.d.ts +52 -45
  180. package/dist/psdReader.js +1385 -1282
  181. package/dist/psdReader.js.map +1 -1
  182. package/dist/psdWriter.d.ts +34 -34
  183. package/dist/psdWriter.js +834 -824
  184. package/dist/psdWriter.js.map +1 -1
  185. package/dist/text.d.ts +179 -179
  186. package/dist/text.js +552 -553
  187. package/dist/text.js.map +1 -1
  188. package/dist/utf8.d.ts +4 -6
  189. package/dist/utf8.js +156 -157
  190. package/dist/utf8.js.map +1 -1
  191. package/dist-es/abr.d.ts +196 -196
  192. package/dist-es/abr.js +364 -361
  193. package/dist-es/abr.js.map +1 -1
  194. package/dist-es/additionalInfo.d.ts +26 -26
  195. package/dist-es/additionalInfo.js +3478 -3472
  196. package/dist-es/additionalInfo.js.map +1 -1
  197. package/dist-es/ase.d.ts +32 -32
  198. package/dist-es/ase.js +81 -78
  199. package/dist-es/ase.js.map +1 -1
  200. package/dist-es/csh.d.ts +10 -10
  201. package/dist-es/csh.js +35 -32
  202. package/dist-es/csh.js.map +1 -1
  203. package/dist-es/descriptor.d.ts +601 -601
  204. package/dist-es/descriptor.js +2003 -1970
  205. package/dist-es/descriptor.js.map +1 -1
  206. package/dist-es/effectsHelpers.d.ts +5 -5
  207. package/dist-es/effectsHelpers.js +286 -282
  208. package/dist-es/effectsHelpers.js.map +1 -1
  209. package/dist-es/engineData.d.ts +2 -2
  210. package/dist-es/engineData.js +334 -330
  211. package/dist-es/engineData.js.map +1 -1
  212. package/dist-es/engineData2.d.ts +2 -2
  213. package/dist-es/engineData2.js +409 -406
  214. package/dist-es/engineData2.js.map +1 -1
  215. package/dist-es/helpers.d.ts +74 -74
  216. package/dist-es/helpers.js +353 -324
  217. package/dist-es/helpers.js.map +1 -1
  218. package/dist-es/imageResources.d.ts +17 -17
  219. package/dist-es/imageResources.js +1040 -1037
  220. package/dist-es/imageResources.js.map +1 -1
  221. package/dist-es/index.d.ts +20 -22
  222. package/dist-es/index.js +61 -33
  223. package/dist-es/index.js.map +1 -1
  224. package/dist-es/initializeCanvas.d.ts +1 -1
  225. package/dist-es/initializeCanvas.js +9 -6
  226. package/dist-es/initializeCanvas.js.map +1 -1
  227. package/dist-es/jpeg.d.ts +1 -1
  228. package/dist-es/jpeg.js +1020 -1017
  229. package/dist-es/jpeg.js.map +1 -1
  230. package/dist-es/psd.d.ts +1877 -1873
  231. package/dist-es/psd.js +45 -42
  232. package/dist-es/psd.js.map +1 -1
  233. package/dist-es/psdReader.d.ts +52 -45
  234. package/dist-es/psdReader.js +1385 -1244
  235. package/dist-es/psdReader.js.map +1 -1
  236. package/dist-es/psdWriter.d.ts +34 -34
  237. package/dist-es/psdWriter.js +834 -794
  238. package/dist-es/psdWriter.js.map +1 -1
  239. package/dist-es/text.d.ts +179 -179
  240. package/dist-es/text.js +552 -548
  241. package/dist-es/text.js.map +1 -1
  242. package/dist-es/utf8.d.ts +4 -6
  243. package/dist-es/utf8.js +156 -150
  244. package/dist-es/utf8.js.map +1 -1
  245. package/package.json +4 -3
  246. package/src/helpers.ts +13 -3
  247. package/src/index.ts +4 -6
  248. package/src/psd.ts +4 -0
  249. package/src/psdReader.ts +136 -43
  250. package/src/psdWriter.ts +42 -23
  251. package/tsconfig-es6.json +2 -1
  252. package/tsconfig.json +1 -0
package/dist/jpeg.js CHANGED
@@ -1,1022 +1,1021 @@
1
- "use strict";
2
- // based on https://github.com/jpeg-js/jpeg-js
3
- /*
4
- Copyright 2011 notmasteryet
5
-
6
- Licensed under the Apache License, Version 2.0 (the "License");
7
- you may not use this file except in compliance with the License.
8
- You may obtain a copy of the License at
9
-
10
- http://www.apache.org/licenses/LICENSE-2.0
11
-
12
- Unless required by applicable law or agreed to in writing, software
13
- distributed under the License is distributed on an "AS IS" BASIS,
14
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- See the License for the specific language governing permissions and
16
- limitations under the License.
17
- */
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.decodeJpeg = void 0;
20
- const dctZigZag = new Int32Array([
21
- 0,
22
- 1, 8,
23
- 16, 9, 2,
24
- 3, 10, 17, 24,
25
- 32, 25, 18, 11, 4,
26
- 5, 12, 19, 26, 33, 40,
27
- 48, 41, 34, 27, 20, 13, 6,
28
- 7, 14, 21, 28, 35, 42, 49, 56,
29
- 57, 50, 43, 36, 29, 22, 15,
30
- 23, 30, 37, 44, 51, 58,
31
- 59, 52, 45, 38, 31,
32
- 39, 46, 53, 60,
33
- 61, 54, 47,
34
- 55, 62,
35
- 63
36
- ]);
37
- const dctCos1 = 4017; // cos(pi/16)
38
- const dctSin1 = 799; // sin(pi/16)
39
- const dctCos3 = 3406; // cos(3*pi/16)
40
- const dctSin3 = 2276; // sin(3*pi/16)
41
- const dctCos6 = 1567; // cos(6*pi/16)
42
- const dctSin6 = 3784; // sin(6*pi/16)
43
- const dctSqrt2 = 5793; // sqrt(2)
44
- const dctSqrt1d2 = 2896; // sqrt(2) / 2
45
- const maxResolutionInMP = 100; // Don't decode more than 100 megapixels
46
- const maxMemoryUsageBytes = 64 * 1024 * 1024; // Don't decode if memory footprint is more than 64MB
47
- let totalBytesAllocated = 0; // avoid unexpected OOMs from untrusted content.
48
- function requestMemoryAllocation(increaseAmount) {
49
- const totalMemoryImpactBytes = totalBytesAllocated + increaseAmount;
50
- if (totalMemoryImpactBytes > maxMemoryUsageBytes) {
51
- const exceededAmount = Math.ceil((totalMemoryImpactBytes - maxMemoryUsageBytes) / 1024 / 1024);
52
- throw new Error(`Max memory limit exceeded by at least ${exceededAmount}MB`);
53
- }
54
- totalBytesAllocated = totalMemoryImpactBytes;
55
- }
56
- function buildHuffmanTable(codeLengths, values) {
57
- let length = 16;
58
- while (length > 0 && !codeLengths[length - 1])
59
- length--;
60
- const code = [{ children: [], index: 0 }];
61
- let k = 0;
62
- let p = code[0];
63
- for (let i = 0; i < length; i++) {
64
- for (let j = 0; j < codeLengths[i]; j++) {
65
- p = code.pop();
66
- p.children[p.index] = values[k];
67
- while (p.index > 0) {
68
- if (code.length === 0)
69
- throw new Error('Could not recreate Huffman Table');
70
- p = code.pop();
71
- }
72
- p.index++;
73
- code.push(p);
74
- while (code.length <= i) {
75
- const q = { children: [], index: 0 };
76
- code.push(q);
77
- p.children[p.index] = q.children;
78
- p = q;
79
- }
80
- k++;
81
- }
82
- if (i + 1 < length) {
83
- // p here points to last code
84
- const q = { children: [], index: 0 };
85
- code.push(q);
86
- p.children[p.index] = q.children;
87
- p = q;
88
- }
89
- }
90
- return code[0].children;
91
- }
92
- function decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successivePrev, successive) {
93
- const mcusPerLine = frame.mcusPerLine;
94
- const progressive = frame.progressive;
95
- const startOffset = offset;
96
- let bitsData = 0;
97
- let bitsCount = 0;
98
- function readBit() {
99
- if (bitsCount > 0) {
100
- bitsCount--;
101
- return (bitsData >> bitsCount) & 1;
102
- }
103
- bitsData = data[offset++];
104
- if (bitsData == 0xFF) {
105
- const nextByte = data[offset++];
106
- if (nextByte)
107
- throw new Error(`unexpected marker: ${((bitsData << 8) | nextByte).toString(16)}`);
108
- // unstuff 0
109
- }
110
- bitsCount = 7;
111
- return bitsData >>> 7;
112
- }
113
- function decodeHuffman(tree) {
114
- let node = tree;
115
- while (true) {
116
- node = node[readBit()];
117
- if (typeof node === 'number')
118
- return node;
119
- if (node === undefined)
120
- throw new Error('invalid huffman sequence');
121
- }
122
- }
123
- function receive(length) {
124
- let n = 0;
125
- while (length > 0) {
126
- n = (n << 1) | readBit();
127
- length--;
128
- }
129
- return n;
130
- }
131
- function receiveAndExtend(length) {
132
- let n = receive(length);
133
- if (n >= 1 << (length - 1))
134
- return n;
135
- return n + (-1 << length) + 1;
136
- }
137
- function decodeBaseline(component, zz) {
138
- const t = decodeHuffman(component.huffmanTableDC);
139
- const diff = t === 0 ? 0 : receiveAndExtend(t);
140
- zz[0] = (component.pred += diff);
141
- let k = 1;
142
- while (k < 64) {
143
- const rs = decodeHuffman(component.huffmanTableAC);
144
- const s = rs & 15;
145
- const r = rs >> 4;
146
- if (s === 0) {
147
- if (r < 15)
148
- break;
149
- k += 16;
150
- continue;
151
- }
152
- k += r;
153
- const z = dctZigZag[k];
154
- zz[z] = receiveAndExtend(s);
155
- k++;
156
- }
157
- }
158
- function decodeDCFirst(component, zz) {
159
- const t = decodeHuffman(component.huffmanTableDC);
160
- const diff = t === 0 ? 0 : (receiveAndExtend(t) << successive);
161
- zz[0] = (component.pred += diff);
162
- }
163
- function decodeDCSuccessive(_component, zz) {
164
- zz[0] |= readBit() << successive;
165
- }
166
- let eobrun = 0;
167
- function decodeACFirst(component, zz) {
168
- if (eobrun > 0) {
169
- eobrun--;
170
- return;
171
- }
172
- let k = spectralStart, e = spectralEnd;
173
- while (k <= e) {
174
- const rs = decodeHuffman(component.huffmanTableAC);
175
- const s = rs & 15;
176
- const r = rs >> 4;
177
- if (s === 0) {
178
- if (r < 15) {
179
- eobrun = receive(r) + (1 << r) - 1;
180
- break;
181
- }
182
- k += 16;
183
- continue;
184
- }
185
- k += r;
186
- const z = dctZigZag[k];
187
- zz[z] = receiveAndExtend(s) * (1 << successive);
188
- k++;
189
- }
190
- }
191
- let successiveACState = 0;
192
- let successiveACNextValue = 0;
193
- function decodeACSuccessive(component, zz) {
194
- let k = spectralStart;
195
- let e = spectralEnd;
196
- let r = 0;
197
- while (k <= e) {
198
- const z = dctZigZag[k];
199
- const direction = zz[z] < 0 ? -1 : 1;
200
- switch (successiveACState) {
201
- case 0: // initial state
202
- const rs = decodeHuffman(component.huffmanTableAC);
203
- const s = rs & 15;
204
- r = rs >> 4; // this was new variable in old code
205
- if (s === 0) {
206
- if (r < 15) {
207
- eobrun = receive(r) + (1 << r);
208
- successiveACState = 4;
209
- }
210
- else {
211
- r = 16;
212
- successiveACState = 1;
213
- }
214
- }
215
- else {
216
- if (s !== 1)
217
- throw new Error('invalid ACn encoding');
218
- successiveACNextValue = receiveAndExtend(s);
219
- successiveACState = r ? 2 : 3;
220
- }
221
- continue;
222
- case 1: // skipping r zero items
223
- case 2:
224
- if (zz[z]) {
225
- zz[z] += (readBit() << successive) * direction;
226
- }
227
- else {
228
- r--;
229
- if (r === 0)
230
- successiveACState = successiveACState == 2 ? 3 : 0;
231
- }
232
- break;
233
- case 3: // set value for a zero item
234
- if (zz[z]) {
235
- zz[z] += (readBit() << successive) * direction;
236
- }
237
- else {
238
- zz[z] = successiveACNextValue << successive;
239
- successiveACState = 0;
240
- }
241
- break;
242
- case 4: // eob
243
- if (zz[z]) {
244
- zz[z] += (readBit() << successive) * direction;
245
- }
246
- break;
247
- }
248
- k++;
249
- }
250
- if (successiveACState === 4) {
251
- eobrun--;
252
- if (eobrun === 0)
253
- successiveACState = 0;
254
- }
255
- }
256
- function decodeMcu(component, decode, mcu, row, col) {
257
- const mcuRow = (mcu / mcusPerLine) | 0;
258
- const mcuCol = mcu % mcusPerLine;
259
- const blockRow = mcuRow * component.v + row;
260
- const blockCol = mcuCol * component.h + col;
261
- // If the block is missing, just skip it.
262
- if (component.blocks[blockRow] === undefined)
263
- return;
264
- decode(component, component.blocks[blockRow][blockCol]);
265
- }
266
- function decodeBlock(component, decode, mcu) {
267
- const blockRow = (mcu / component.blocksPerLine) | 0;
268
- const blockCol = mcu % component.blocksPerLine;
269
- // If the block is missing, just skip it.
270
- if (component.blocks[blockRow] === undefined)
271
- return;
272
- decode(component, component.blocks[blockRow][blockCol]);
273
- }
274
- const componentsLength = components.length;
275
- let component;
276
- let decodeFn;
277
- if (progressive) {
278
- if (spectralStart === 0) {
279
- decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
280
- }
281
- else {
282
- decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
283
- }
284
- }
285
- else {
286
- decodeFn = decodeBaseline;
287
- }
288
- let mcu = 0;
289
- let mcuExpected;
290
- if (componentsLength == 1) {
291
- mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
292
- }
293
- else {
294
- mcuExpected = mcusPerLine * frame.mcusPerColumn;
295
- }
296
- if (!resetInterval)
297
- resetInterval = mcuExpected;
298
- let h;
299
- let v;
300
- let marker;
301
- while (mcu < mcuExpected) {
302
- // reset interval stuff
303
- for (let i = 0; i < componentsLength; i++)
304
- components[i].pred = 0;
305
- eobrun = 0;
306
- if (componentsLength == 1) {
307
- component = components[0];
308
- for (let n = 0; n < resetInterval; n++) {
309
- decodeBlock(component, decodeFn, mcu);
310
- mcu++;
311
- }
312
- }
313
- else {
314
- for (let n = 0; n < resetInterval; n++) {
315
- for (let i = 0; i < componentsLength; i++) {
316
- component = components[i];
317
- h = component.h;
318
- v = component.v;
319
- for (let j = 0; j < v; j++) {
320
- for (let k = 0; k < h; k++) {
321
- decodeMcu(component, decodeFn, mcu, j, k);
322
- }
323
- }
324
- }
325
- mcu++;
326
- // If we've reached our expected MCU's, stop decoding
327
- if (mcu === mcuExpected)
328
- break;
329
- }
330
- }
331
- if (mcu === mcuExpected) {
332
- // Skip trailing bytes at the end of the scan - until we reach the next marker
333
- do {
334
- if (data[offset] === 0xFF) {
335
- if (data[offset + 1] !== 0x00) {
336
- break;
337
- }
338
- }
339
- offset += 1;
340
- } while (offset < data.length - 2);
341
- }
342
- // find marker
343
- bitsCount = 0;
344
- marker = (data[offset] << 8) | data[offset + 1];
345
- if (marker < 0xFF00)
346
- throw new Error('marker was not found');
347
- if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
348
- offset += 2;
349
- }
350
- else {
351
- break;
352
- }
353
- }
354
- return offset - startOffset;
355
- }
356
- function buildComponentData(component) {
357
- const lines = [];
358
- const blocksPerLine = component.blocksPerLine;
359
- const blocksPerColumn = component.blocksPerColumn;
360
- const samplesPerLine = blocksPerLine << 3;
361
- // Only 1 used per invocation of this function and garbage collected after invocation, so no need to account for its memory footprint.
362
- const R = new Int32Array(64);
363
- const r = new Uint8Array(64);
364
- // A port of poppler's IDCT method which in turn is taken from:
365
- // Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
366
- // "Practical Fast 1-D DCT Algorithms with 11 Multiplications",
367
- // IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
368
- // 988-991.
369
- function quantizeAndInverse(zz, dataOut, dataIn) {
370
- const qt = component.quantizationTable;
371
- const p = dataIn;
372
- // dequant
373
- for (let i = 0; i < 64; i++) {
374
- p[i] = zz[i] * qt[i];
375
- }
376
- // inverse DCT on rows
377
- for (let i = 0; i < 8; ++i) {
378
- const row = 8 * i;
379
- // check for all-zero AC coefficients
380
- if (p[1 + row] == 0 && p[2 + row] == 0 && p[3 + row] == 0 &&
381
- p[4 + row] == 0 && p[5 + row] == 0 && p[6 + row] == 0 &&
382
- p[7 + row] == 0) {
383
- const t = (dctSqrt2 * p[0 + row] + 512) >> 10;
384
- p[0 + row] = t;
385
- p[1 + row] = t;
386
- p[2 + row] = t;
387
- p[3 + row] = t;
388
- p[4 + row] = t;
389
- p[5 + row] = t;
390
- p[6 + row] = t;
391
- p[7 + row] = t;
392
- continue;
393
- }
394
- // stage 4
395
- let v0 = (dctSqrt2 * p[0 + row] + 128) >> 8;
396
- let v1 = (dctSqrt2 * p[4 + row] + 128) >> 8;
397
- let v2 = p[2 + row];
398
- let v3 = p[6 + row];
399
- let v4 = (dctSqrt1d2 * (p[1 + row] - p[7 + row]) + 128) >> 8;
400
- let v7 = (dctSqrt1d2 * (p[1 + row] + p[7 + row]) + 128) >> 8;
401
- let v5 = p[3 + row] << 4;
402
- let v6 = p[5 + row] << 4;
403
- // stage 3
404
- let t = (v0 - v1 + 1) >> 1;
405
- v0 = (v0 + v1 + 1) >> 1;
406
- v1 = t;
407
- t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
408
- v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
409
- v3 = t;
410
- t = (v4 - v6 + 1) >> 1;
411
- v4 = (v4 + v6 + 1) >> 1;
412
- v6 = t;
413
- t = (v7 + v5 + 1) >> 1;
414
- v5 = (v7 - v5 + 1) >> 1;
415
- v7 = t;
416
- // stage 2
417
- t = (v0 - v3 + 1) >> 1;
418
- v0 = (v0 + v3 + 1) >> 1;
419
- v3 = t;
420
- t = (v1 - v2 + 1) >> 1;
421
- v1 = (v1 + v2 + 1) >> 1;
422
- v2 = t;
423
- t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
424
- v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
425
- v7 = t;
426
- t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
427
- v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
428
- v6 = t;
429
- // stage 1
430
- p[0 + row] = v0 + v7;
431
- p[7 + row] = v0 - v7;
432
- p[1 + row] = v1 + v6;
433
- p[6 + row] = v1 - v6;
434
- p[2 + row] = v2 + v5;
435
- p[5 + row] = v2 - v5;
436
- p[3 + row] = v3 + v4;
437
- p[4 + row] = v3 - v4;
438
- }
439
- // inverse DCT on columns
440
- for (let i = 0; i < 8; ++i) {
441
- const col = i;
442
- // check for all-zero AC coefficients
443
- if (p[1 * 8 + col] == 0 && p[2 * 8 + col] == 0 && p[3 * 8 + col] == 0 &&
444
- p[4 * 8 + col] == 0 && p[5 * 8 + col] == 0 && p[6 * 8 + col] == 0 &&
445
- p[7 * 8 + col] == 0) {
446
- const t = (dctSqrt2 * dataIn[i + 0] + 8192) >> 14;
447
- p[0 * 8 + col] = t;
448
- p[1 * 8 + col] = t;
449
- p[2 * 8 + col] = t;
450
- p[3 * 8 + col] = t;
451
- p[4 * 8 + col] = t;
452
- p[5 * 8 + col] = t;
453
- p[6 * 8 + col] = t;
454
- p[7 * 8 + col] = t;
455
- continue;
456
- }
457
- // stage 4
458
- let v0 = (dctSqrt2 * p[0 * 8 + col] + 2048) >> 12;
459
- let v1 = (dctSqrt2 * p[4 * 8 + col] + 2048) >> 12;
460
- let v2 = p[2 * 8 + col];
461
- let v3 = p[6 * 8 + col];
462
- let v4 = (dctSqrt1d2 * (p[1 * 8 + col] - p[7 * 8 + col]) + 2048) >> 12;
463
- let v7 = (dctSqrt1d2 * (p[1 * 8 + col] + p[7 * 8 + col]) + 2048) >> 12;
464
- let v5 = p[3 * 8 + col];
465
- let v6 = p[5 * 8 + col];
466
- // stage 3
467
- let t = (v0 - v1 + 1) >> 1;
468
- v0 = (v0 + v1 + 1) >> 1;
469
- v1 = t;
470
- t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
471
- v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
472
- v3 = t;
473
- t = (v4 - v6 + 1) >> 1;
474
- v4 = (v4 + v6 + 1) >> 1;
475
- v6 = t;
476
- t = (v7 + v5 + 1) >> 1;
477
- v5 = (v7 - v5 + 1) >> 1;
478
- v7 = t;
479
- // stage 2
480
- t = (v0 - v3 + 1) >> 1;
481
- v0 = (v0 + v3 + 1) >> 1;
482
- v3 = t;
483
- t = (v1 - v2 + 1) >> 1;
484
- v1 = (v1 + v2 + 1) >> 1;
485
- v2 = t;
486
- t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
487
- v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
488
- v7 = t;
489
- t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
490
- v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
491
- v6 = t;
492
- // stage 1
493
- p[0 * 8 + col] = v0 + v7;
494
- p[7 * 8 + col] = v0 - v7;
495
- p[1 * 8 + col] = v1 + v6;
496
- p[6 * 8 + col] = v1 - v6;
497
- p[2 * 8 + col] = v2 + v5;
498
- p[5 * 8 + col] = v2 - v5;
499
- p[3 * 8 + col] = v3 + v4;
500
- p[4 * 8 + col] = v3 - v4;
501
- }
502
- // convert to 8-bit integers
503
- for (let i = 0; i < 64; ++i) {
504
- const sample = 128 + ((p[i] + 8) >> 4);
505
- dataOut[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
506
- }
507
- }
508
- requestMemoryAllocation(samplesPerLine * blocksPerColumn * 8);
509
- for (let blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
510
- const scanLine = blockRow << 3;
511
- for (let i = 0; i < 8; i++)
512
- lines.push(new Uint8Array(samplesPerLine));
513
- for (let blockCol = 0; blockCol < blocksPerLine; blockCol++) {
514
- quantizeAndInverse(component.blocks[blockRow][blockCol], r, R);
515
- let offset = 0;
516
- const sample = blockCol << 3;
517
- for (let j = 0; j < 8; j++) {
518
- const line = lines[scanLine + j];
519
- for (let i = 0; i < 8; i++)
520
- line[sample + i] = r[offset++];
521
- }
522
- }
523
- }
524
- return lines;
525
- }
526
- function clampTo8bit(a) {
527
- return a < 0 ? 0 : a > 255 ? 255 : a;
528
- }
529
- function parse(data) {
530
- const self = {
531
- width: 0,
532
- height: 0,
533
- comments: [],
534
- adobe: undefined,
535
- components: [],
536
- exifBuffer: undefined,
537
- jfif: undefined,
538
- };
539
- const maxResolutionInPixels = maxResolutionInMP * 1000 * 1000;
540
- let offset = 0;
541
- function readUint16() {
542
- const value = (data[offset] << 8) | data[offset + 1];
543
- offset += 2;
544
- return value;
545
- }
546
- function readDataBlock() {
547
- const length = readUint16();
548
- const array = data.subarray(offset, offset + length - 2);
549
- offset += array.length;
550
- return array;
551
- }
552
- function prepareComponents(frame) {
553
- let maxH = 0, maxV = 0;
554
- for (let componentId in frame.components) {
555
- if (frame.components.hasOwnProperty(componentId)) {
556
- const component = frame.components[componentId];
557
- if (maxH < component.h)
558
- maxH = component.h;
559
- if (maxV < component.v)
560
- maxV = component.v;
561
- }
562
- }
563
- const mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / maxH);
564
- const mcusPerColumn = Math.ceil(frame.scanLines / 8 / maxV);
565
- for (let componentId in frame.components) {
566
- if (frame.components.hasOwnProperty(componentId)) {
567
- const component = frame.components[componentId];
568
- const blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / maxH);
569
- const blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / maxV);
570
- const blocksPerLineForMcu = mcusPerLine * component.h;
571
- const blocksPerColumnForMcu = mcusPerColumn * component.v;
572
- const blocksToAllocate = blocksPerColumnForMcu * blocksPerLineForMcu;
573
- const blocks = [];
574
- // Each block is a Int32Array of length 64 (4 x 64 = 256 bytes)
575
- requestMemoryAllocation(blocksToAllocate * 256);
576
- for (let i = 0; i < blocksPerColumnForMcu; i++) {
577
- const row = [];
578
- for (let j = 0; j < blocksPerLineForMcu; j++) {
579
- row.push(new Int32Array(64));
580
- }
581
- blocks.push(row);
582
- }
583
- component.blocksPerLine = blocksPerLine;
584
- component.blocksPerColumn = blocksPerColumn;
585
- component.blocks = blocks;
586
- }
587
- }
588
- frame.maxH = maxH;
589
- frame.maxV = maxV;
590
- frame.mcusPerLine = mcusPerLine;
591
- frame.mcusPerColumn = mcusPerColumn;
592
- }
593
- let jfif = null;
594
- let adobe = null;
595
- let frame = undefined;
596
- let resetInterval = 0;
597
- let quantizationTables = [];
598
- let frames = [];
599
- let huffmanTablesAC = [];
600
- let huffmanTablesDC = [];
601
- let fileMarker = readUint16();
602
- let malformedDataOffset = -1;
603
- if (fileMarker != 0xFFD8) { // SOI (Start of Image)
604
- throw new Error('SOI not found');
605
- }
606
- fileMarker = readUint16();
607
- while (fileMarker != 0xFFD9) { // EOI (End of image)
608
- switch (fileMarker) {
609
- case 0xFF00: break;
610
- case 0xFFE0: // APP0 (Application Specific)
611
- case 0xFFE1: // APP1
612
- case 0xFFE2: // APP2
613
- case 0xFFE3: // APP3
614
- case 0xFFE4: // APP4
615
- case 0xFFE5: // APP5
616
- case 0xFFE6: // APP6
617
- case 0xFFE7: // APP7
618
- case 0xFFE8: // APP8
619
- case 0xFFE9: // APP9
620
- case 0xFFEA: // APP10
621
- case 0xFFEB: // APP11
622
- case 0xFFEC: // APP12
623
- case 0xFFED: // APP13
624
- case 0xFFEE: // APP14
625
- case 0xFFEF: // APP15
626
- case 0xFFFE: { // COM (Comment)
627
- const appData = readDataBlock();
628
- if (fileMarker === 0xFFFE) {
629
- let comment = '';
630
- for (let ii = 0; ii < appData.byteLength; ii++) {
631
- comment += String.fromCharCode(appData[ii]);
632
- }
633
- self.comments.push(comment);
634
- }
635
- if (fileMarker === 0xFFE0) {
636
- if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
637
- appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
638
- jfif = {
639
- version: { major: appData[5], minor: appData[6] },
640
- densityUnits: appData[7],
641
- xDensity: (appData[8] << 8) | appData[9],
642
- yDensity: (appData[10] << 8) | appData[11],
643
- thumbWidth: appData[12],
644
- thumbHeight: appData[13],
645
- thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
646
- };
647
- }
648
- }
649
- // TODO APP1 - Exif
650
- if (fileMarker === 0xFFE1) {
651
- if (appData[0] === 0x45 &&
652
- appData[1] === 0x78 &&
653
- appData[2] === 0x69 &&
654
- appData[3] === 0x66 &&
655
- appData[4] === 0) { // 'EXIF\x00'
656
- self.exifBuffer = appData.subarray(5, appData.length);
657
- }
658
- }
659
- if (fileMarker === 0xFFEE) {
660
- if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
661
- appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
662
- adobe = {
663
- version: appData[6],
664
- flags0: (appData[7] << 8) | appData[8],
665
- flags1: (appData[9] << 8) | appData[10],
666
- transformCode: appData[11]
667
- };
668
- }
669
- }
670
- break;
671
- }
672
- case 0xFFDB: { // DQT (Define Quantization Tables)
673
- const quantizationTablesLength = readUint16();
674
- const quantizationTablesEnd = quantizationTablesLength + offset - 2;
675
- while (offset < quantizationTablesEnd) {
676
- const quantizationTableSpec = data[offset++];
677
- requestMemoryAllocation(64 * 4);
678
- const tableData = new Int32Array(64);
679
- if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
680
- for (let j = 0; j < 64; j++) {
681
- const z = dctZigZag[j];
682
- tableData[z] = data[offset++];
683
- }
684
- }
685
- else if ((quantizationTableSpec >> 4) === 1) { //16 bit
686
- for (let j = 0; j < 64; j++) {
687
- const z = dctZigZag[j];
688
- tableData[z] = readUint16();
689
- }
690
- }
691
- else
692
- throw new Error('DQT: invalid table spec');
693
- quantizationTables[quantizationTableSpec & 15] = tableData;
694
- }
695
- break;
696
- }
697
- case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
698
- case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
699
- case 0xFFC2: { // SOF2 (Start of Frame, Progressive DCT)
700
- readUint16(); // skip data length
701
- frame = {
702
- extended: (fileMarker === 0xFFC1),
703
- progressive: (fileMarker === 0xFFC2),
704
- precision: data[offset++],
705
- scanLines: readUint16(),
706
- samplesPerLine: readUint16(),
707
- components: {},
708
- componentsOrder: [],
709
- maxH: 0,
710
- maxV: 0,
711
- mcusPerLine: 0,
712
- mcusPerColumn: 0,
713
- };
714
- const pixelsInFrame = frame.scanLines * frame.samplesPerLine;
715
- if (pixelsInFrame > maxResolutionInPixels) {
716
- const exceededAmount = Math.ceil((pixelsInFrame - maxResolutionInPixels) / 1e6);
717
- throw new Error(`maxResolutionInMP limit exceeded by ${exceededAmount}MP`);
718
- }
719
- const componentsCount = data[offset++];
720
- for (let i = 0; i < componentsCount; i++) {
721
- const componentId = data[offset];
722
- const h = data[offset + 1] >> 4;
723
- const v = data[offset + 1] & 15;
724
- const qId = data[offset + 2];
725
- frame.componentsOrder.push(componentId);
726
- frame.components[componentId] = {
727
- h: h,
728
- v: v,
729
- quantizationIdx: qId,
730
- blocksPerColumn: 0,
731
- blocksPerLine: 0,
732
- blocks: [],
733
- pred: 0,
734
- };
735
- offset += 3;
736
- }
737
- prepareComponents(frame);
738
- frames.push(frame);
739
- break;
740
- }
741
- case 0xFFC4: { // DHT (Define Huffman Tables)
742
- const huffmanLength = readUint16();
743
- for (let i = 2; i < huffmanLength;) {
744
- const huffmanTableSpec = data[offset++];
745
- const codeLengths = new Uint8Array(16);
746
- let codeLengthSum = 0;
747
- for (let j = 0; j < 16; j++, offset++) {
748
- codeLengthSum += (codeLengths[j] = data[offset]);
749
- }
750
- requestMemoryAllocation(16 + codeLengthSum);
751
- const huffmanValues = new Uint8Array(codeLengthSum);
752
- for (let j = 0; j < codeLengthSum; j++, offset++) {
753
- huffmanValues[j] = data[offset];
754
- }
755
- i += 17 + codeLengthSum;
756
- const index = huffmanTableSpec & 15;
757
- const table = (huffmanTableSpec >> 4) === 0 ? huffmanTablesDC : huffmanTablesAC;
758
- table[index] = buildHuffmanTable(codeLengths, huffmanValues);
759
- }
760
- break;
761
- }
762
- case 0xFFDD: // DRI (Define Restart Interval)
763
- readUint16(); // skip data length
764
- resetInterval = readUint16();
765
- break;
766
- case 0xFFDC: // Number of Lines marker
767
- readUint16(); // skip data length
768
- readUint16(); // Ignore this data since it represents the image height
769
- break;
770
- case 0xFFDA: { // SOS (Start of Scan)
771
- readUint16(); // skip data length
772
- const selectorsCount = data[offset++];
773
- const components = [];
774
- for (let i = 0; i < selectorsCount; i++) {
775
- const component = frame.components[data[offset++]];
776
- const tableSpec = data[offset++];
777
- component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
778
- component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
779
- components.push(component);
780
- }
781
- const spectralStart = data[offset++];
782
- const spectralEnd = data[offset++];
783
- const successiveApproximation = data[offset++];
784
- const processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15);
785
- offset += processed;
786
- break;
787
- }
788
- case 0xFFFF: // Fill bytes
789
- if (data[offset] !== 0xFF) { // Avoid skipping a valid marker.
790
- offset--;
791
- }
792
- break;
793
- default: {
794
- if (data[offset - 3] == 0xFF && data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
795
- // could be incorrect encoding -- last 0xFF byte of the previous
796
- // block was eaten by the encoder
797
- offset -= 3;
798
- break;
799
- }
800
- else if (fileMarker === 0xE0 || fileMarker == 0xE1) {
801
- // Recover from malformed APP1 markers popular in some phone models.
802
- // See https://github.com/eugeneware/jpeg-js/issues/82
803
- if (malformedDataOffset !== -1) {
804
- throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
805
- }
806
- malformedDataOffset = offset - 1;
807
- const nextOffset = readUint16();
808
- if (data[offset + nextOffset - 2] === 0xFF) {
809
- offset += nextOffset - 2;
810
- break;
811
- }
812
- }
813
- throw new Error('unknown JPEG marker ' + fileMarker.toString(16));
814
- }
815
- }
816
- fileMarker = readUint16();
817
- }
818
- if (frames.length != 1)
819
- throw new Error('only single frame JPEGs supported');
820
- // set each frame's components quantization table
821
- for (let i = 0; i < frames.length; i++) {
822
- const cp = frames[i].components;
823
- for (let j in cp) { // TODO: don't use `in`
824
- cp[j].quantizationTable = quantizationTables[cp[j].quantizationIdx];
825
- delete cp[j].quantizationIdx; // TODO: why ???
826
- }
827
- }
828
- self.width = frame.samplesPerLine;
829
- self.height = frame.scanLines;
830
- self.jfif = jfif;
831
- self.adobe = adobe;
832
- self.components = [];
833
- for (let i = 0; i < frame.componentsOrder.length; i++) {
834
- const component = frame.components[frame.componentsOrder[i]];
835
- self.components.push({
836
- lines: buildComponentData(component),
837
- scaleX: component.h / frame.maxH,
838
- scaleY: component.v / frame.maxV
839
- });
840
- }
841
- return self;
842
- }
843
- function getData(decoded) {
844
- let offset = 0;
845
- let colorTransform = false;
846
- const width = decoded.width;
847
- const height = decoded.height;
848
- const dataLength = width * height * decoded.components.length;
849
- requestMemoryAllocation(dataLength);
850
- const data = new Uint8Array(dataLength);
851
- switch (decoded.components.length) {
852
- case 1: {
853
- const component1 = decoded.components[0];
854
- for (let y = 0; y < height; y++) {
855
- const component1Line = component1.lines[0 | (y * component1.scaleY)];
856
- for (let x = 0; x < width; x++) {
857
- const Y = component1Line[0 | (x * component1.scaleX)];
858
- data[offset++] = Y;
859
- }
860
- }
861
- break;
862
- }
863
- case 2: {
864
- // PDF might compress two component data in custom colorspace
865
- const component1 = decoded.components[0];
866
- const component2 = decoded.components[1];
867
- for (let y = 0; y < height; y++) {
868
- const component1Line = component1.lines[0 | (y * component1.scaleY)];
869
- const component2Line = component2.lines[0 | (y * component2.scaleY)];
870
- for (let x = 0; x < width; x++) {
871
- const Y1 = component1Line[0 | (x * component1.scaleX)];
872
- data[offset++] = Y1;
873
- const Y2 = component2Line[0 | (x * component2.scaleX)];
874
- data[offset++] = Y2;
875
- }
876
- }
877
- break;
878
- }
879
- case 3: {
880
- // The default transform for three components is true
881
- colorTransform = true;
882
- // The adobe transform marker overrides any previous setting
883
- if (decoded.adobe && decoded.adobe.transformCode)
884
- colorTransform = true;
885
- const component1 = decoded.components[0];
886
- const component2 = decoded.components[1];
887
- const component3 = decoded.components[2];
888
- for (let y = 0; y < height; y++) {
889
- const component1Line = component1.lines[0 | (y * component1.scaleY)];
890
- const component2Line = component2.lines[0 | (y * component2.scaleY)];
891
- const component3Line = component3.lines[0 | (y * component3.scaleY)];
892
- for (let x = 0; x < width; x++) {
893
- let Y, Cb, Cr, R, G, B;
894
- if (!colorTransform) {
895
- R = component1Line[0 | (x * component1.scaleX)];
896
- G = component2Line[0 | (x * component2.scaleX)];
897
- B = component3Line[0 | (x * component3.scaleX)];
898
- }
899
- else {
900
- Y = component1Line[0 | (x * component1.scaleX)];
901
- Cb = component2Line[0 | (x * component2.scaleX)];
902
- Cr = component3Line[0 | (x * component3.scaleX)];
903
- R = clampTo8bit(Y + 1.402 * (Cr - 128));
904
- G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
905
- B = clampTo8bit(Y + 1.772 * (Cb - 128));
906
- }
907
- data[offset++] = R;
908
- data[offset++] = G;
909
- data[offset++] = B;
910
- }
911
- }
912
- break;
913
- }
914
- case 4: {
915
- if (!decoded.adobe)
916
- throw new Error('Unsupported color mode (4 components)');
917
- // The default transform for four components is false
918
- colorTransform = false;
919
- // The adobe transform marker overrides any previous setting
920
- if (decoded.adobe && decoded.adobe.transformCode)
921
- colorTransform = true;
922
- const component1 = decoded.components[0];
923
- const component2 = decoded.components[1];
924
- const component3 = decoded.components[2];
925
- const component4 = decoded.components[3];
926
- for (let y = 0; y < height; y++) {
927
- const component1Line = component1.lines[0 | (y * component1.scaleY)];
928
- const component2Line = component2.lines[0 | (y * component2.scaleY)];
929
- const component3Line = component3.lines[0 | (y * component3.scaleY)];
930
- const component4Line = component4.lines[0 | (y * component4.scaleY)];
931
- for (let x = 0; x < width; x++) {
932
- let Y, Cb, Cr, K, C, M, Ye;
933
- if (!colorTransform) {
934
- C = component1Line[0 | (x * component1.scaleX)];
935
- M = component2Line[0 | (x * component2.scaleX)];
936
- Ye = component3Line[0 | (x * component3.scaleX)];
937
- K = component4Line[0 | (x * component4.scaleX)];
938
- }
939
- else {
940
- Y = component1Line[0 | (x * component1.scaleX)];
941
- Cb = component2Line[0 | (x * component2.scaleX)];
942
- Cr = component3Line[0 | (x * component3.scaleX)];
943
- K = component4Line[0 | (x * component4.scaleX)];
944
- C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
945
- M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
946
- Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
947
- }
948
- data[offset++] = 255 - C;
949
- data[offset++] = 255 - M;
950
- data[offset++] = 255 - Ye;
951
- data[offset++] = 255 - K;
952
- }
953
- }
954
- break;
955
- }
956
- default:
957
- throw new Error('Unsupported color mode');
958
- }
959
- return data;
960
- }
961
- function decodeJpeg(encoded, createImageData) {
962
- totalBytesAllocated = 0;
963
- if (encoded.length === 0)
964
- throw new Error('Empty jpeg buffer');
965
- const decoded = parse(encoded);
966
- requestMemoryAllocation(decoded.width * decoded.height * 4);
967
- const data = getData(decoded);
968
- const imageData = createImageData(decoded.width, decoded.height);
969
- const width = imageData.width;
970
- const height = imageData.height;
971
- const imageDataArray = imageData.data;
972
- let i = 0;
973
- let j = 0;
974
- switch (decoded.components.length) {
975
- case 1:
976
- for (let y = 0; y < height; y++) {
977
- for (let x = 0; x < width; x++) {
978
- const Y = data[i++];
979
- imageDataArray[j++] = Y;
980
- imageDataArray[j++] = Y;
981
- imageDataArray[j++] = Y;
982
- imageDataArray[j++] = 255;
983
- }
984
- }
985
- break;
986
- case 3:
987
- for (let y = 0; y < height; y++) {
988
- for (let x = 0; x < width; x++) {
989
- const R = data[i++];
990
- const G = data[i++];
991
- const B = data[i++];
992
- imageDataArray[j++] = R;
993
- imageDataArray[j++] = G;
994
- imageDataArray[j++] = B;
995
- imageDataArray[j++] = 255;
996
- }
997
- }
998
- break;
999
- case 4:
1000
- for (let y = 0; y < height; y++) {
1001
- for (let x = 0; x < width; x++) {
1002
- const C = data[i++];
1003
- const M = data[i++];
1004
- const Y = data[i++];
1005
- const K = data[i++];
1006
- const R = 255 - clampTo8bit(C * (1 - K / 255) + K);
1007
- const G = 255 - clampTo8bit(M * (1 - K / 255) + K);
1008
- const B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
1009
- imageDataArray[j++] = R;
1010
- imageDataArray[j++] = G;
1011
- imageDataArray[j++] = B;
1012
- imageDataArray[j++] = 255;
1013
- }
1014
- }
1015
- break;
1016
- default:
1017
- throw new Error('Unsupported color mode');
1018
- }
1019
- return imageData;
1020
- }
1021
- exports.decodeJpeg = decodeJpeg;
1
+ "use strict";
2
+ // based on https://github.com/jpeg-js/jpeg-js
3
+ /*
4
+ Copyright 2011 notmasteryet
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.decodeJpeg = decodeJpeg;
20
+ const dctZigZag = new Int32Array([
21
+ 0,
22
+ 1, 8,
23
+ 16, 9, 2,
24
+ 3, 10, 17, 24,
25
+ 32, 25, 18, 11, 4,
26
+ 5, 12, 19, 26, 33, 40,
27
+ 48, 41, 34, 27, 20, 13, 6,
28
+ 7, 14, 21, 28, 35, 42, 49, 56,
29
+ 57, 50, 43, 36, 29, 22, 15,
30
+ 23, 30, 37, 44, 51, 58,
31
+ 59, 52, 45, 38, 31,
32
+ 39, 46, 53, 60,
33
+ 61, 54, 47,
34
+ 55, 62,
35
+ 63
36
+ ]);
37
+ const dctCos1 = 4017; // cos(pi/16)
38
+ const dctSin1 = 799; // sin(pi/16)
39
+ const dctCos3 = 3406; // cos(3*pi/16)
40
+ const dctSin3 = 2276; // sin(3*pi/16)
41
+ const dctCos6 = 1567; // cos(6*pi/16)
42
+ const dctSin6 = 3784; // sin(6*pi/16)
43
+ const dctSqrt2 = 5793; // sqrt(2)
44
+ const dctSqrt1d2 = 2896; // sqrt(2) / 2
45
+ const maxResolutionInMP = 100; // Don't decode more than 100 megapixels
46
+ const maxMemoryUsageBytes = 64 * 1024 * 1024; // Don't decode if memory footprint is more than 64MB
47
+ let totalBytesAllocated = 0; // avoid unexpected OOMs from untrusted content.
48
+ function requestMemoryAllocation(increaseAmount) {
49
+ const totalMemoryImpactBytes = totalBytesAllocated + increaseAmount;
50
+ if (totalMemoryImpactBytes > maxMemoryUsageBytes) {
51
+ const exceededAmount = Math.ceil((totalMemoryImpactBytes - maxMemoryUsageBytes) / 1024 / 1024);
52
+ throw new Error(`Max memory limit exceeded by at least ${exceededAmount}MB`);
53
+ }
54
+ totalBytesAllocated = totalMemoryImpactBytes;
55
+ }
56
+ function buildHuffmanTable(codeLengths, values) {
57
+ let length = 16;
58
+ while (length > 0 && !codeLengths[length - 1])
59
+ length--;
60
+ const code = [{ children: [], index: 0 }];
61
+ let k = 0;
62
+ let p = code[0];
63
+ for (let i = 0; i < length; i++) {
64
+ for (let j = 0; j < codeLengths[i]; j++) {
65
+ p = code.pop();
66
+ p.children[p.index] = values[k];
67
+ while (p.index > 0) {
68
+ if (code.length === 0)
69
+ throw new Error('Could not recreate Huffman Table');
70
+ p = code.pop();
71
+ }
72
+ p.index++;
73
+ code.push(p);
74
+ while (code.length <= i) {
75
+ const q = { children: [], index: 0 };
76
+ code.push(q);
77
+ p.children[p.index] = q.children;
78
+ p = q;
79
+ }
80
+ k++;
81
+ }
82
+ if (i + 1 < length) {
83
+ // p here points to last code
84
+ const q = { children: [], index: 0 };
85
+ code.push(q);
86
+ p.children[p.index] = q.children;
87
+ p = q;
88
+ }
89
+ }
90
+ return code[0].children;
91
+ }
92
+ function decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successivePrev, successive) {
93
+ const mcusPerLine = frame.mcusPerLine;
94
+ const progressive = frame.progressive;
95
+ const startOffset = offset;
96
+ let bitsData = 0;
97
+ let bitsCount = 0;
98
+ function readBit() {
99
+ if (bitsCount > 0) {
100
+ bitsCount--;
101
+ return (bitsData >> bitsCount) & 1;
102
+ }
103
+ bitsData = data[offset++];
104
+ if (bitsData == 0xFF) {
105
+ const nextByte = data[offset++];
106
+ if (nextByte)
107
+ throw new Error(`unexpected marker: ${((bitsData << 8) | nextByte).toString(16)}`);
108
+ // unstuff 0
109
+ }
110
+ bitsCount = 7;
111
+ return bitsData >>> 7;
112
+ }
113
+ function decodeHuffman(tree) {
114
+ let node = tree;
115
+ while (true) {
116
+ node = node[readBit()];
117
+ if (typeof node === 'number')
118
+ return node;
119
+ if (node === undefined)
120
+ throw new Error('invalid huffman sequence');
121
+ }
122
+ }
123
+ function receive(length) {
124
+ let n = 0;
125
+ while (length > 0) {
126
+ n = (n << 1) | readBit();
127
+ length--;
128
+ }
129
+ return n;
130
+ }
131
+ function receiveAndExtend(length) {
132
+ let n = receive(length);
133
+ if (n >= 1 << (length - 1))
134
+ return n;
135
+ return n + (-1 << length) + 1;
136
+ }
137
+ function decodeBaseline(component, zz) {
138
+ const t = decodeHuffman(component.huffmanTableDC);
139
+ const diff = t === 0 ? 0 : receiveAndExtend(t);
140
+ zz[0] = (component.pred += diff);
141
+ let k = 1;
142
+ while (k < 64) {
143
+ const rs = decodeHuffman(component.huffmanTableAC);
144
+ const s = rs & 15;
145
+ const r = rs >> 4;
146
+ if (s === 0) {
147
+ if (r < 15)
148
+ break;
149
+ k += 16;
150
+ continue;
151
+ }
152
+ k += r;
153
+ const z = dctZigZag[k];
154
+ zz[z] = receiveAndExtend(s);
155
+ k++;
156
+ }
157
+ }
158
+ function decodeDCFirst(component, zz) {
159
+ const t = decodeHuffman(component.huffmanTableDC);
160
+ const diff = t === 0 ? 0 : (receiveAndExtend(t) << successive);
161
+ zz[0] = (component.pred += diff);
162
+ }
163
+ function decodeDCSuccessive(_component, zz) {
164
+ zz[0] |= readBit() << successive;
165
+ }
166
+ let eobrun = 0;
167
+ function decodeACFirst(component, zz) {
168
+ if (eobrun > 0) {
169
+ eobrun--;
170
+ return;
171
+ }
172
+ let k = spectralStart, e = spectralEnd;
173
+ while (k <= e) {
174
+ const rs = decodeHuffman(component.huffmanTableAC);
175
+ const s = rs & 15;
176
+ const r = rs >> 4;
177
+ if (s === 0) {
178
+ if (r < 15) {
179
+ eobrun = receive(r) + (1 << r) - 1;
180
+ break;
181
+ }
182
+ k += 16;
183
+ continue;
184
+ }
185
+ k += r;
186
+ const z = dctZigZag[k];
187
+ zz[z] = receiveAndExtend(s) * (1 << successive);
188
+ k++;
189
+ }
190
+ }
191
+ let successiveACState = 0;
192
+ let successiveACNextValue = 0;
193
+ function decodeACSuccessive(component, zz) {
194
+ let k = spectralStart;
195
+ let e = spectralEnd;
196
+ let r = 0;
197
+ while (k <= e) {
198
+ const z = dctZigZag[k];
199
+ const direction = zz[z] < 0 ? -1 : 1;
200
+ switch (successiveACState) {
201
+ case 0: // initial state
202
+ const rs = decodeHuffman(component.huffmanTableAC);
203
+ const s = rs & 15;
204
+ r = rs >> 4; // this was new variable in old code
205
+ if (s === 0) {
206
+ if (r < 15) {
207
+ eobrun = receive(r) + (1 << r);
208
+ successiveACState = 4;
209
+ }
210
+ else {
211
+ r = 16;
212
+ successiveACState = 1;
213
+ }
214
+ }
215
+ else {
216
+ if (s !== 1)
217
+ throw new Error('invalid ACn encoding');
218
+ successiveACNextValue = receiveAndExtend(s);
219
+ successiveACState = r ? 2 : 3;
220
+ }
221
+ continue;
222
+ case 1: // skipping r zero items
223
+ case 2:
224
+ if (zz[z]) {
225
+ zz[z] += (readBit() << successive) * direction;
226
+ }
227
+ else {
228
+ r--;
229
+ if (r === 0)
230
+ successiveACState = successiveACState == 2 ? 3 : 0;
231
+ }
232
+ break;
233
+ case 3: // set value for a zero item
234
+ if (zz[z]) {
235
+ zz[z] += (readBit() << successive) * direction;
236
+ }
237
+ else {
238
+ zz[z] = successiveACNextValue << successive;
239
+ successiveACState = 0;
240
+ }
241
+ break;
242
+ case 4: // eob
243
+ if (zz[z]) {
244
+ zz[z] += (readBit() << successive) * direction;
245
+ }
246
+ break;
247
+ }
248
+ k++;
249
+ }
250
+ if (successiveACState === 4) {
251
+ eobrun--;
252
+ if (eobrun === 0)
253
+ successiveACState = 0;
254
+ }
255
+ }
256
+ function decodeMcu(component, decode, mcu, row, col) {
257
+ const mcuRow = (mcu / mcusPerLine) | 0;
258
+ const mcuCol = mcu % mcusPerLine;
259
+ const blockRow = mcuRow * component.v + row;
260
+ const blockCol = mcuCol * component.h + col;
261
+ // If the block is missing, just skip it.
262
+ if (component.blocks[blockRow] === undefined)
263
+ return;
264
+ decode(component, component.blocks[blockRow][blockCol]);
265
+ }
266
+ function decodeBlock(component, decode, mcu) {
267
+ const blockRow = (mcu / component.blocksPerLine) | 0;
268
+ const blockCol = mcu % component.blocksPerLine;
269
+ // If the block is missing, just skip it.
270
+ if (component.blocks[blockRow] === undefined)
271
+ return;
272
+ decode(component, component.blocks[blockRow][blockCol]);
273
+ }
274
+ const componentsLength = components.length;
275
+ let component;
276
+ let decodeFn;
277
+ if (progressive) {
278
+ if (spectralStart === 0) {
279
+ decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
280
+ }
281
+ else {
282
+ decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
283
+ }
284
+ }
285
+ else {
286
+ decodeFn = decodeBaseline;
287
+ }
288
+ let mcu = 0;
289
+ let mcuExpected;
290
+ if (componentsLength == 1) {
291
+ mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
292
+ }
293
+ else {
294
+ mcuExpected = mcusPerLine * frame.mcusPerColumn;
295
+ }
296
+ if (!resetInterval)
297
+ resetInterval = mcuExpected;
298
+ let h;
299
+ let v;
300
+ let marker;
301
+ while (mcu < mcuExpected) {
302
+ // reset interval stuff
303
+ for (let i = 0; i < componentsLength; i++)
304
+ components[i].pred = 0;
305
+ eobrun = 0;
306
+ if (componentsLength == 1) {
307
+ component = components[0];
308
+ for (let n = 0; n < resetInterval; n++) {
309
+ decodeBlock(component, decodeFn, mcu);
310
+ mcu++;
311
+ }
312
+ }
313
+ else {
314
+ for (let n = 0; n < resetInterval; n++) {
315
+ for (let i = 0; i < componentsLength; i++) {
316
+ component = components[i];
317
+ h = component.h;
318
+ v = component.v;
319
+ for (let j = 0; j < v; j++) {
320
+ for (let k = 0; k < h; k++) {
321
+ decodeMcu(component, decodeFn, mcu, j, k);
322
+ }
323
+ }
324
+ }
325
+ mcu++;
326
+ // If we've reached our expected MCU's, stop decoding
327
+ if (mcu === mcuExpected)
328
+ break;
329
+ }
330
+ }
331
+ if (mcu === mcuExpected) {
332
+ // Skip trailing bytes at the end of the scan - until we reach the next marker
333
+ do {
334
+ if (data[offset] === 0xFF) {
335
+ if (data[offset + 1] !== 0x00) {
336
+ break;
337
+ }
338
+ }
339
+ offset += 1;
340
+ } while (offset < data.length - 2);
341
+ }
342
+ // find marker
343
+ bitsCount = 0;
344
+ marker = (data[offset] << 8) | data[offset + 1];
345
+ if (marker < 0xFF00)
346
+ throw new Error('marker was not found');
347
+ if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
348
+ offset += 2;
349
+ }
350
+ else {
351
+ break;
352
+ }
353
+ }
354
+ return offset - startOffset;
355
+ }
356
+ function buildComponentData(component) {
357
+ const lines = [];
358
+ const blocksPerLine = component.blocksPerLine;
359
+ const blocksPerColumn = component.blocksPerColumn;
360
+ const samplesPerLine = blocksPerLine << 3;
361
+ // Only 1 used per invocation of this function and garbage collected after invocation, so no need to account for its memory footprint.
362
+ const R = new Int32Array(64);
363
+ const r = new Uint8Array(64);
364
+ // A port of poppler's IDCT method which in turn is taken from:
365
+ // Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
366
+ // "Practical Fast 1-D DCT Algorithms with 11 Multiplications",
367
+ // IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
368
+ // 988-991.
369
+ function quantizeAndInverse(zz, dataOut, dataIn) {
370
+ const qt = component.quantizationTable;
371
+ const p = dataIn;
372
+ // dequant
373
+ for (let i = 0; i < 64; i++) {
374
+ p[i] = zz[i] * qt[i];
375
+ }
376
+ // inverse DCT on rows
377
+ for (let i = 0; i < 8; ++i) {
378
+ const row = 8 * i;
379
+ // check for all-zero AC coefficients
380
+ if (p[1 + row] == 0 && p[2 + row] == 0 && p[3 + row] == 0 &&
381
+ p[4 + row] == 0 && p[5 + row] == 0 && p[6 + row] == 0 &&
382
+ p[7 + row] == 0) {
383
+ const t = (dctSqrt2 * p[0 + row] + 512) >> 10;
384
+ p[0 + row] = t;
385
+ p[1 + row] = t;
386
+ p[2 + row] = t;
387
+ p[3 + row] = t;
388
+ p[4 + row] = t;
389
+ p[5 + row] = t;
390
+ p[6 + row] = t;
391
+ p[7 + row] = t;
392
+ continue;
393
+ }
394
+ // stage 4
395
+ let v0 = (dctSqrt2 * p[0 + row] + 128) >> 8;
396
+ let v1 = (dctSqrt2 * p[4 + row] + 128) >> 8;
397
+ let v2 = p[2 + row];
398
+ let v3 = p[6 + row];
399
+ let v4 = (dctSqrt1d2 * (p[1 + row] - p[7 + row]) + 128) >> 8;
400
+ let v7 = (dctSqrt1d2 * (p[1 + row] + p[7 + row]) + 128) >> 8;
401
+ let v5 = p[3 + row] << 4;
402
+ let v6 = p[5 + row] << 4;
403
+ // stage 3
404
+ let t = (v0 - v1 + 1) >> 1;
405
+ v0 = (v0 + v1 + 1) >> 1;
406
+ v1 = t;
407
+ t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
408
+ v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
409
+ v3 = t;
410
+ t = (v4 - v6 + 1) >> 1;
411
+ v4 = (v4 + v6 + 1) >> 1;
412
+ v6 = t;
413
+ t = (v7 + v5 + 1) >> 1;
414
+ v5 = (v7 - v5 + 1) >> 1;
415
+ v7 = t;
416
+ // stage 2
417
+ t = (v0 - v3 + 1) >> 1;
418
+ v0 = (v0 + v3 + 1) >> 1;
419
+ v3 = t;
420
+ t = (v1 - v2 + 1) >> 1;
421
+ v1 = (v1 + v2 + 1) >> 1;
422
+ v2 = t;
423
+ t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
424
+ v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
425
+ v7 = t;
426
+ t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
427
+ v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
428
+ v6 = t;
429
+ // stage 1
430
+ p[0 + row] = v0 + v7;
431
+ p[7 + row] = v0 - v7;
432
+ p[1 + row] = v1 + v6;
433
+ p[6 + row] = v1 - v6;
434
+ p[2 + row] = v2 + v5;
435
+ p[5 + row] = v2 - v5;
436
+ p[3 + row] = v3 + v4;
437
+ p[4 + row] = v3 - v4;
438
+ }
439
+ // inverse DCT on columns
440
+ for (let i = 0; i < 8; ++i) {
441
+ const col = i;
442
+ // check for all-zero AC coefficients
443
+ if (p[1 * 8 + col] == 0 && p[2 * 8 + col] == 0 && p[3 * 8 + col] == 0 &&
444
+ p[4 * 8 + col] == 0 && p[5 * 8 + col] == 0 && p[6 * 8 + col] == 0 &&
445
+ p[7 * 8 + col] == 0) {
446
+ const t = (dctSqrt2 * dataIn[i + 0] + 8192) >> 14;
447
+ p[0 * 8 + col] = t;
448
+ p[1 * 8 + col] = t;
449
+ p[2 * 8 + col] = t;
450
+ p[3 * 8 + col] = t;
451
+ p[4 * 8 + col] = t;
452
+ p[5 * 8 + col] = t;
453
+ p[6 * 8 + col] = t;
454
+ p[7 * 8 + col] = t;
455
+ continue;
456
+ }
457
+ // stage 4
458
+ let v0 = (dctSqrt2 * p[0 * 8 + col] + 2048) >> 12;
459
+ let v1 = (dctSqrt2 * p[4 * 8 + col] + 2048) >> 12;
460
+ let v2 = p[2 * 8 + col];
461
+ let v3 = p[6 * 8 + col];
462
+ let v4 = (dctSqrt1d2 * (p[1 * 8 + col] - p[7 * 8 + col]) + 2048) >> 12;
463
+ let v7 = (dctSqrt1d2 * (p[1 * 8 + col] + p[7 * 8 + col]) + 2048) >> 12;
464
+ let v5 = p[3 * 8 + col];
465
+ let v6 = p[5 * 8 + col];
466
+ // stage 3
467
+ let t = (v0 - v1 + 1) >> 1;
468
+ v0 = (v0 + v1 + 1) >> 1;
469
+ v1 = t;
470
+ t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
471
+ v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
472
+ v3 = t;
473
+ t = (v4 - v6 + 1) >> 1;
474
+ v4 = (v4 + v6 + 1) >> 1;
475
+ v6 = t;
476
+ t = (v7 + v5 + 1) >> 1;
477
+ v5 = (v7 - v5 + 1) >> 1;
478
+ v7 = t;
479
+ // stage 2
480
+ t = (v0 - v3 + 1) >> 1;
481
+ v0 = (v0 + v3 + 1) >> 1;
482
+ v3 = t;
483
+ t = (v1 - v2 + 1) >> 1;
484
+ v1 = (v1 + v2 + 1) >> 1;
485
+ v2 = t;
486
+ t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
487
+ v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
488
+ v7 = t;
489
+ t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
490
+ v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
491
+ v6 = t;
492
+ // stage 1
493
+ p[0 * 8 + col] = v0 + v7;
494
+ p[7 * 8 + col] = v0 - v7;
495
+ p[1 * 8 + col] = v1 + v6;
496
+ p[6 * 8 + col] = v1 - v6;
497
+ p[2 * 8 + col] = v2 + v5;
498
+ p[5 * 8 + col] = v2 - v5;
499
+ p[3 * 8 + col] = v3 + v4;
500
+ p[4 * 8 + col] = v3 - v4;
501
+ }
502
+ // convert to 8-bit integers
503
+ for (let i = 0; i < 64; ++i) {
504
+ const sample = 128 + ((p[i] + 8) >> 4);
505
+ dataOut[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
506
+ }
507
+ }
508
+ requestMemoryAllocation(samplesPerLine * blocksPerColumn * 8);
509
+ for (let blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
510
+ const scanLine = blockRow << 3;
511
+ for (let i = 0; i < 8; i++)
512
+ lines.push(new Uint8Array(samplesPerLine));
513
+ for (let blockCol = 0; blockCol < blocksPerLine; blockCol++) {
514
+ quantizeAndInverse(component.blocks[blockRow][blockCol], r, R);
515
+ let offset = 0;
516
+ const sample = blockCol << 3;
517
+ for (let j = 0; j < 8; j++) {
518
+ const line = lines[scanLine + j];
519
+ for (let i = 0; i < 8; i++)
520
+ line[sample + i] = r[offset++];
521
+ }
522
+ }
523
+ }
524
+ return lines;
525
+ }
526
+ function clampTo8bit(a) {
527
+ return a < 0 ? 0 : a > 255 ? 255 : a;
528
+ }
529
+ function parse(data) {
530
+ const self = {
531
+ width: 0,
532
+ height: 0,
533
+ comments: [],
534
+ adobe: undefined,
535
+ components: [],
536
+ exifBuffer: undefined,
537
+ jfif: undefined,
538
+ };
539
+ const maxResolutionInPixels = maxResolutionInMP * 1000 * 1000;
540
+ let offset = 0;
541
+ function readUint16() {
542
+ const value = (data[offset] << 8) | data[offset + 1];
543
+ offset += 2;
544
+ return value;
545
+ }
546
+ function readDataBlock() {
547
+ const length = readUint16();
548
+ const array = data.subarray(offset, offset + length - 2);
549
+ offset += array.length;
550
+ return array;
551
+ }
552
+ function prepareComponents(frame) {
553
+ let maxH = 0, maxV = 0;
554
+ for (let componentId in frame.components) {
555
+ if (frame.components.hasOwnProperty(componentId)) {
556
+ const component = frame.components[componentId];
557
+ if (maxH < component.h)
558
+ maxH = component.h;
559
+ if (maxV < component.v)
560
+ maxV = component.v;
561
+ }
562
+ }
563
+ const mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / maxH);
564
+ const mcusPerColumn = Math.ceil(frame.scanLines / 8 / maxV);
565
+ for (let componentId in frame.components) {
566
+ if (frame.components.hasOwnProperty(componentId)) {
567
+ const component = frame.components[componentId];
568
+ const blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / maxH);
569
+ const blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / maxV);
570
+ const blocksPerLineForMcu = mcusPerLine * component.h;
571
+ const blocksPerColumnForMcu = mcusPerColumn * component.v;
572
+ const blocksToAllocate = blocksPerColumnForMcu * blocksPerLineForMcu;
573
+ const blocks = [];
574
+ // Each block is a Int32Array of length 64 (4 x 64 = 256 bytes)
575
+ requestMemoryAllocation(blocksToAllocate * 256);
576
+ for (let i = 0; i < blocksPerColumnForMcu; i++) {
577
+ const row = [];
578
+ for (let j = 0; j < blocksPerLineForMcu; j++) {
579
+ row.push(new Int32Array(64));
580
+ }
581
+ blocks.push(row);
582
+ }
583
+ component.blocksPerLine = blocksPerLine;
584
+ component.blocksPerColumn = blocksPerColumn;
585
+ component.blocks = blocks;
586
+ }
587
+ }
588
+ frame.maxH = maxH;
589
+ frame.maxV = maxV;
590
+ frame.mcusPerLine = mcusPerLine;
591
+ frame.mcusPerColumn = mcusPerColumn;
592
+ }
593
+ let jfif = null;
594
+ let adobe = null;
595
+ let frame = undefined;
596
+ let resetInterval = 0;
597
+ let quantizationTables = [];
598
+ let frames = [];
599
+ let huffmanTablesAC = [];
600
+ let huffmanTablesDC = [];
601
+ let fileMarker = readUint16();
602
+ let malformedDataOffset = -1;
603
+ if (fileMarker != 0xFFD8) { // SOI (Start of Image)
604
+ throw new Error('SOI not found');
605
+ }
606
+ fileMarker = readUint16();
607
+ while (fileMarker != 0xFFD9) { // EOI (End of image)
608
+ switch (fileMarker) {
609
+ case 0xFF00: break;
610
+ case 0xFFE0: // APP0 (Application Specific)
611
+ case 0xFFE1: // APP1
612
+ case 0xFFE2: // APP2
613
+ case 0xFFE3: // APP3
614
+ case 0xFFE4: // APP4
615
+ case 0xFFE5: // APP5
616
+ case 0xFFE6: // APP6
617
+ case 0xFFE7: // APP7
618
+ case 0xFFE8: // APP8
619
+ case 0xFFE9: // APP9
620
+ case 0xFFEA: // APP10
621
+ case 0xFFEB: // APP11
622
+ case 0xFFEC: // APP12
623
+ case 0xFFED: // APP13
624
+ case 0xFFEE: // APP14
625
+ case 0xFFEF: // APP15
626
+ case 0xFFFE: { // COM (Comment)
627
+ const appData = readDataBlock();
628
+ if (fileMarker === 0xFFFE) {
629
+ let comment = '';
630
+ for (let ii = 0; ii < appData.byteLength; ii++) {
631
+ comment += String.fromCharCode(appData[ii]);
632
+ }
633
+ self.comments.push(comment);
634
+ }
635
+ if (fileMarker === 0xFFE0) {
636
+ if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
637
+ appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
638
+ jfif = {
639
+ version: { major: appData[5], minor: appData[6] },
640
+ densityUnits: appData[7],
641
+ xDensity: (appData[8] << 8) | appData[9],
642
+ yDensity: (appData[10] << 8) | appData[11],
643
+ thumbWidth: appData[12],
644
+ thumbHeight: appData[13],
645
+ thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
646
+ };
647
+ }
648
+ }
649
+ // TODO APP1 - Exif
650
+ if (fileMarker === 0xFFE1) {
651
+ if (appData[0] === 0x45 &&
652
+ appData[1] === 0x78 &&
653
+ appData[2] === 0x69 &&
654
+ appData[3] === 0x66 &&
655
+ appData[4] === 0) { // 'EXIF\x00'
656
+ self.exifBuffer = appData.subarray(5, appData.length);
657
+ }
658
+ }
659
+ if (fileMarker === 0xFFEE) {
660
+ if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
661
+ appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
662
+ adobe = {
663
+ version: appData[6],
664
+ flags0: (appData[7] << 8) | appData[8],
665
+ flags1: (appData[9] << 8) | appData[10],
666
+ transformCode: appData[11]
667
+ };
668
+ }
669
+ }
670
+ break;
671
+ }
672
+ case 0xFFDB: { // DQT (Define Quantization Tables)
673
+ const quantizationTablesLength = readUint16();
674
+ const quantizationTablesEnd = quantizationTablesLength + offset - 2;
675
+ while (offset < quantizationTablesEnd) {
676
+ const quantizationTableSpec = data[offset++];
677
+ requestMemoryAllocation(64 * 4);
678
+ const tableData = new Int32Array(64);
679
+ if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
680
+ for (let j = 0; j < 64; j++) {
681
+ const z = dctZigZag[j];
682
+ tableData[z] = data[offset++];
683
+ }
684
+ }
685
+ else if ((quantizationTableSpec >> 4) === 1) { //16 bit
686
+ for (let j = 0; j < 64; j++) {
687
+ const z = dctZigZag[j];
688
+ tableData[z] = readUint16();
689
+ }
690
+ }
691
+ else
692
+ throw new Error('DQT: invalid table spec');
693
+ quantizationTables[quantizationTableSpec & 15] = tableData;
694
+ }
695
+ break;
696
+ }
697
+ case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
698
+ case 0xFFC1: // SOF1 (Start of Frame, Extended DCT)
699
+ case 0xFFC2: { // SOF2 (Start of Frame, Progressive DCT)
700
+ readUint16(); // skip data length
701
+ frame = {
702
+ extended: (fileMarker === 0xFFC1),
703
+ progressive: (fileMarker === 0xFFC2),
704
+ precision: data[offset++],
705
+ scanLines: readUint16(),
706
+ samplesPerLine: readUint16(),
707
+ components: {},
708
+ componentsOrder: [],
709
+ maxH: 0,
710
+ maxV: 0,
711
+ mcusPerLine: 0,
712
+ mcusPerColumn: 0,
713
+ };
714
+ const pixelsInFrame = frame.scanLines * frame.samplesPerLine;
715
+ if (pixelsInFrame > maxResolutionInPixels) {
716
+ const exceededAmount = Math.ceil((pixelsInFrame - maxResolutionInPixels) / 1e6);
717
+ throw new Error(`maxResolutionInMP limit exceeded by ${exceededAmount}MP`);
718
+ }
719
+ const componentsCount = data[offset++];
720
+ for (let i = 0; i < componentsCount; i++) {
721
+ const componentId = data[offset];
722
+ const h = data[offset + 1] >> 4;
723
+ const v = data[offset + 1] & 15;
724
+ const qId = data[offset + 2];
725
+ frame.componentsOrder.push(componentId);
726
+ frame.components[componentId] = {
727
+ h: h,
728
+ v: v,
729
+ quantizationIdx: qId,
730
+ blocksPerColumn: 0,
731
+ blocksPerLine: 0,
732
+ blocks: [],
733
+ pred: 0,
734
+ };
735
+ offset += 3;
736
+ }
737
+ prepareComponents(frame);
738
+ frames.push(frame);
739
+ break;
740
+ }
741
+ case 0xFFC4: { // DHT (Define Huffman Tables)
742
+ const huffmanLength = readUint16();
743
+ for (let i = 2; i < huffmanLength;) {
744
+ const huffmanTableSpec = data[offset++];
745
+ const codeLengths = new Uint8Array(16);
746
+ let codeLengthSum = 0;
747
+ for (let j = 0; j < 16; j++, offset++) {
748
+ codeLengthSum += (codeLengths[j] = data[offset]);
749
+ }
750
+ requestMemoryAllocation(16 + codeLengthSum);
751
+ const huffmanValues = new Uint8Array(codeLengthSum);
752
+ for (let j = 0; j < codeLengthSum; j++, offset++) {
753
+ huffmanValues[j] = data[offset];
754
+ }
755
+ i += 17 + codeLengthSum;
756
+ const index = huffmanTableSpec & 15;
757
+ const table = (huffmanTableSpec >> 4) === 0 ? huffmanTablesDC : huffmanTablesAC;
758
+ table[index] = buildHuffmanTable(codeLengths, huffmanValues);
759
+ }
760
+ break;
761
+ }
762
+ case 0xFFDD: // DRI (Define Restart Interval)
763
+ readUint16(); // skip data length
764
+ resetInterval = readUint16();
765
+ break;
766
+ case 0xFFDC: // Number of Lines marker
767
+ readUint16(); // skip data length
768
+ readUint16(); // Ignore this data since it represents the image height
769
+ break;
770
+ case 0xFFDA: { // SOS (Start of Scan)
771
+ readUint16(); // skip data length
772
+ const selectorsCount = data[offset++];
773
+ const components = [];
774
+ for (let i = 0; i < selectorsCount; i++) {
775
+ const component = frame.components[data[offset++]];
776
+ const tableSpec = data[offset++];
777
+ component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
778
+ component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
779
+ components.push(component);
780
+ }
781
+ const spectralStart = data[offset++];
782
+ const spectralEnd = data[offset++];
783
+ const successiveApproximation = data[offset++];
784
+ const processed = decodeScan(data, offset, frame, components, resetInterval, spectralStart, spectralEnd, successiveApproximation >> 4, successiveApproximation & 15);
785
+ offset += processed;
786
+ break;
787
+ }
788
+ case 0xFFFF: // Fill bytes
789
+ if (data[offset] !== 0xFF) { // Avoid skipping a valid marker.
790
+ offset--;
791
+ }
792
+ break;
793
+ default: {
794
+ if (data[offset - 3] == 0xFF && data[offset - 2] >= 0xC0 && data[offset - 2] <= 0xFE) {
795
+ // could be incorrect encoding -- last 0xFF byte of the previous
796
+ // block was eaten by the encoder
797
+ offset -= 3;
798
+ break;
799
+ }
800
+ else if (fileMarker === 0xE0 || fileMarker == 0xE1) {
801
+ // Recover from malformed APP1 markers popular in some phone models.
802
+ // See https://github.com/eugeneware/jpeg-js/issues/82
803
+ if (malformedDataOffset !== -1) {
804
+ throw new Error(`first unknown JPEG marker at offset ${malformedDataOffset.toString(16)}, second unknown JPEG marker ${fileMarker.toString(16)} at offset ${(offset - 1).toString(16)}`);
805
+ }
806
+ malformedDataOffset = offset - 1;
807
+ const nextOffset = readUint16();
808
+ if (data[offset + nextOffset - 2] === 0xFF) {
809
+ offset += nextOffset - 2;
810
+ break;
811
+ }
812
+ }
813
+ throw new Error('unknown JPEG marker ' + fileMarker.toString(16));
814
+ }
815
+ }
816
+ fileMarker = readUint16();
817
+ }
818
+ if (frames.length != 1)
819
+ throw new Error('only single frame JPEGs supported');
820
+ // set each frame's components quantization table
821
+ for (let i = 0; i < frames.length; i++) {
822
+ const cp = frames[i].components;
823
+ for (let j in cp) { // TODO: don't use `in`
824
+ cp[j].quantizationTable = quantizationTables[cp[j].quantizationIdx];
825
+ delete cp[j].quantizationIdx; // TODO: why ???
826
+ }
827
+ }
828
+ self.width = frame.samplesPerLine;
829
+ self.height = frame.scanLines;
830
+ self.jfif = jfif;
831
+ self.adobe = adobe;
832
+ self.components = [];
833
+ for (let i = 0; i < frame.componentsOrder.length; i++) {
834
+ const component = frame.components[frame.componentsOrder[i]];
835
+ self.components.push({
836
+ lines: buildComponentData(component),
837
+ scaleX: component.h / frame.maxH,
838
+ scaleY: component.v / frame.maxV
839
+ });
840
+ }
841
+ return self;
842
+ }
843
+ function getData(decoded) {
844
+ let offset = 0;
845
+ let colorTransform = false;
846
+ const width = decoded.width;
847
+ const height = decoded.height;
848
+ const dataLength = width * height * decoded.components.length;
849
+ requestMemoryAllocation(dataLength);
850
+ const data = new Uint8Array(dataLength);
851
+ switch (decoded.components.length) {
852
+ case 1: {
853
+ const component1 = decoded.components[0];
854
+ for (let y = 0; y < height; y++) {
855
+ const component1Line = component1.lines[0 | (y * component1.scaleY)];
856
+ for (let x = 0; x < width; x++) {
857
+ const Y = component1Line[0 | (x * component1.scaleX)];
858
+ data[offset++] = Y;
859
+ }
860
+ }
861
+ break;
862
+ }
863
+ case 2: {
864
+ // PDF might compress two component data in custom colorspace
865
+ const component1 = decoded.components[0];
866
+ const component2 = decoded.components[1];
867
+ for (let y = 0; y < height; y++) {
868
+ const component1Line = component1.lines[0 | (y * component1.scaleY)];
869
+ const component2Line = component2.lines[0 | (y * component2.scaleY)];
870
+ for (let x = 0; x < width; x++) {
871
+ const Y1 = component1Line[0 | (x * component1.scaleX)];
872
+ data[offset++] = Y1;
873
+ const Y2 = component2Line[0 | (x * component2.scaleX)];
874
+ data[offset++] = Y2;
875
+ }
876
+ }
877
+ break;
878
+ }
879
+ case 3: {
880
+ // The default transform for three components is true
881
+ colorTransform = true;
882
+ // The adobe transform marker overrides any previous setting
883
+ if (decoded.adobe && decoded.adobe.transformCode)
884
+ colorTransform = true;
885
+ const component1 = decoded.components[0];
886
+ const component2 = decoded.components[1];
887
+ const component3 = decoded.components[2];
888
+ for (let y = 0; y < height; y++) {
889
+ const component1Line = component1.lines[0 | (y * component1.scaleY)];
890
+ const component2Line = component2.lines[0 | (y * component2.scaleY)];
891
+ const component3Line = component3.lines[0 | (y * component3.scaleY)];
892
+ for (let x = 0; x < width; x++) {
893
+ let Y, Cb, Cr, R, G, B;
894
+ if (!colorTransform) {
895
+ R = component1Line[0 | (x * component1.scaleX)];
896
+ G = component2Line[0 | (x * component2.scaleX)];
897
+ B = component3Line[0 | (x * component3.scaleX)];
898
+ }
899
+ else {
900
+ Y = component1Line[0 | (x * component1.scaleX)];
901
+ Cb = component2Line[0 | (x * component2.scaleX)];
902
+ Cr = component3Line[0 | (x * component3.scaleX)];
903
+ R = clampTo8bit(Y + 1.402 * (Cr - 128));
904
+ G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
905
+ B = clampTo8bit(Y + 1.772 * (Cb - 128));
906
+ }
907
+ data[offset++] = R;
908
+ data[offset++] = G;
909
+ data[offset++] = B;
910
+ }
911
+ }
912
+ break;
913
+ }
914
+ case 4: {
915
+ if (!decoded.adobe)
916
+ throw new Error('Unsupported color mode (4 components)');
917
+ // The default transform for four components is false
918
+ colorTransform = false;
919
+ // The adobe transform marker overrides any previous setting
920
+ if (decoded.adobe && decoded.adobe.transformCode)
921
+ colorTransform = true;
922
+ const component1 = decoded.components[0];
923
+ const component2 = decoded.components[1];
924
+ const component3 = decoded.components[2];
925
+ const component4 = decoded.components[3];
926
+ for (let y = 0; y < height; y++) {
927
+ const component1Line = component1.lines[0 | (y * component1.scaleY)];
928
+ const component2Line = component2.lines[0 | (y * component2.scaleY)];
929
+ const component3Line = component3.lines[0 | (y * component3.scaleY)];
930
+ const component4Line = component4.lines[0 | (y * component4.scaleY)];
931
+ for (let x = 0; x < width; x++) {
932
+ let Y, Cb, Cr, K, C, M, Ye;
933
+ if (!colorTransform) {
934
+ C = component1Line[0 | (x * component1.scaleX)];
935
+ M = component2Line[0 | (x * component2.scaleX)];
936
+ Ye = component3Line[0 | (x * component3.scaleX)];
937
+ K = component4Line[0 | (x * component4.scaleX)];
938
+ }
939
+ else {
940
+ Y = component1Line[0 | (x * component1.scaleX)];
941
+ Cb = component2Line[0 | (x * component2.scaleX)];
942
+ Cr = component3Line[0 | (x * component3.scaleX)];
943
+ K = component4Line[0 | (x * component4.scaleX)];
944
+ C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
945
+ M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
946
+ Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
947
+ }
948
+ data[offset++] = 255 - C;
949
+ data[offset++] = 255 - M;
950
+ data[offset++] = 255 - Ye;
951
+ data[offset++] = 255 - K;
952
+ }
953
+ }
954
+ break;
955
+ }
956
+ default:
957
+ throw new Error('Unsupported color mode');
958
+ }
959
+ return data;
960
+ }
961
+ function decodeJpeg(encoded, createImageData) {
962
+ totalBytesAllocated = 0;
963
+ if (encoded.length === 0)
964
+ throw new Error('Empty jpeg buffer');
965
+ const decoded = parse(encoded);
966
+ requestMemoryAllocation(decoded.width * decoded.height * 4);
967
+ const data = getData(decoded);
968
+ const imageData = createImageData(decoded.width, decoded.height);
969
+ const width = imageData.width;
970
+ const height = imageData.height;
971
+ const imageDataArray = imageData.data;
972
+ let i = 0;
973
+ let j = 0;
974
+ switch (decoded.components.length) {
975
+ case 1:
976
+ for (let y = 0; y < height; y++) {
977
+ for (let x = 0; x < width; x++) {
978
+ const Y = data[i++];
979
+ imageDataArray[j++] = Y;
980
+ imageDataArray[j++] = Y;
981
+ imageDataArray[j++] = Y;
982
+ imageDataArray[j++] = 255;
983
+ }
984
+ }
985
+ break;
986
+ case 3:
987
+ for (let y = 0; y < height; y++) {
988
+ for (let x = 0; x < width; x++) {
989
+ const R = data[i++];
990
+ const G = data[i++];
991
+ const B = data[i++];
992
+ imageDataArray[j++] = R;
993
+ imageDataArray[j++] = G;
994
+ imageDataArray[j++] = B;
995
+ imageDataArray[j++] = 255;
996
+ }
997
+ }
998
+ break;
999
+ case 4:
1000
+ for (let y = 0; y < height; y++) {
1001
+ for (let x = 0; x < width; x++) {
1002
+ const C = data[i++];
1003
+ const M = data[i++];
1004
+ const Y = data[i++];
1005
+ const K = data[i++];
1006
+ const R = 255 - clampTo8bit(C * (1 - K / 255) + K);
1007
+ const G = 255 - clampTo8bit(M * (1 - K / 255) + K);
1008
+ const B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
1009
+ imageDataArray[j++] = R;
1010
+ imageDataArray[j++] = G;
1011
+ imageDataArray[j++] = B;
1012
+ imageDataArray[j++] = 255;
1013
+ }
1014
+ }
1015
+ break;
1016
+ default:
1017
+ throw new Error('Unsupported color mode');
1018
+ }
1019
+ return imageData;
1020
+ }
1022
1021
  //# sourceMappingURL=jpeg.js.map