predictor_stream.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PredictorStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. var _primitives = require("./primitives.js");
  29. var _util = require("../shared/util.js");
  30. class PredictorStream extends _decode_stream.DecodeStream {
  31. constructor(str, maybeLength, params) {
  32. super(maybeLength);
  33. if (!(params instanceof _primitives.Dict)) {
  34. return str;
  35. }
  36. const predictor = this.predictor = params.get("Predictor") || 1;
  37. if (predictor <= 1) {
  38. return str;
  39. }
  40. if (predictor !== 2 && (predictor < 10 || predictor > 15)) {
  41. throw new _util.FormatError(`Unsupported predictor: ${predictor}`);
  42. }
  43. if (predictor === 2) {
  44. this.readBlock = this.readBlockTiff;
  45. } else {
  46. this.readBlock = this.readBlockPng;
  47. }
  48. this.str = str;
  49. this.dict = str.dict;
  50. const colors = this.colors = params.get("Colors") || 1;
  51. const bits = this.bits = params.get("BPC", "BitsPerComponent") || 8;
  52. const columns = this.columns = params.get("Columns") || 1;
  53. this.pixBytes = colors * bits + 7 >> 3;
  54. this.rowBytes = columns * colors * bits + 7 >> 3;
  55. return this;
  56. }
  57. readBlockTiff() {
  58. const rowBytes = this.rowBytes;
  59. const bufferLength = this.bufferLength;
  60. const buffer = this.ensureBuffer(bufferLength + rowBytes);
  61. const bits = this.bits;
  62. const colors = this.colors;
  63. const rawBytes = this.str.getBytes(rowBytes);
  64. this.eof = !rawBytes.length;
  65. if (this.eof) {
  66. return;
  67. }
  68. let inbuf = 0,
  69. outbuf = 0;
  70. let inbits = 0,
  71. outbits = 0;
  72. let pos = bufferLength;
  73. let i;
  74. if (bits === 1 && colors === 1) {
  75. for (i = 0; i < rowBytes; ++i) {
  76. let c = rawBytes[i] ^ inbuf;
  77. c ^= c >> 1;
  78. c ^= c >> 2;
  79. c ^= c >> 4;
  80. inbuf = (c & 1) << 7;
  81. buffer[pos++] = c;
  82. }
  83. } else if (bits === 8) {
  84. for (i = 0; i < colors; ++i) {
  85. buffer[pos++] = rawBytes[i];
  86. }
  87. for (; i < rowBytes; ++i) {
  88. buffer[pos] = buffer[pos - colors] + rawBytes[i];
  89. pos++;
  90. }
  91. } else if (bits === 16) {
  92. const bytesPerPixel = colors * 2;
  93. for (i = 0; i < bytesPerPixel; ++i) {
  94. buffer[pos++] = rawBytes[i];
  95. }
  96. for (; i < rowBytes; i += 2) {
  97. const sum = ((rawBytes[i] & 0xff) << 8) + (rawBytes[i + 1] & 0xff) + ((buffer[pos - bytesPerPixel] & 0xff) << 8) + (buffer[pos - bytesPerPixel + 1] & 0xff);
  98. buffer[pos++] = sum >> 8 & 0xff;
  99. buffer[pos++] = sum & 0xff;
  100. }
  101. } else {
  102. const compArray = new Uint8Array(colors + 1);
  103. const bitMask = (1 << bits) - 1;
  104. let j = 0,
  105. k = bufferLength;
  106. const columns = this.columns;
  107. for (i = 0; i < columns; ++i) {
  108. for (let kk = 0; kk < colors; ++kk) {
  109. if (inbits < bits) {
  110. inbuf = inbuf << 8 | rawBytes[j++] & 0xff;
  111. inbits += 8;
  112. }
  113. compArray[kk] = compArray[kk] + (inbuf >> inbits - bits) & bitMask;
  114. inbits -= bits;
  115. outbuf = outbuf << bits | compArray[kk];
  116. outbits += bits;
  117. if (outbits >= 8) {
  118. buffer[k++] = outbuf >> outbits - 8 & 0xff;
  119. outbits -= 8;
  120. }
  121. }
  122. }
  123. if (outbits > 0) {
  124. buffer[k++] = (outbuf << 8 - outbits) + (inbuf & (1 << 8 - outbits) - 1);
  125. }
  126. }
  127. this.bufferLength += rowBytes;
  128. }
  129. readBlockPng() {
  130. const rowBytes = this.rowBytes;
  131. const pixBytes = this.pixBytes;
  132. const predictor = this.str.getByte();
  133. const rawBytes = this.str.getBytes(rowBytes);
  134. this.eof = !rawBytes.length;
  135. if (this.eof) {
  136. return;
  137. }
  138. const bufferLength = this.bufferLength;
  139. const buffer = this.ensureBuffer(bufferLength + rowBytes);
  140. let prevRow = buffer.subarray(bufferLength - rowBytes, bufferLength);
  141. if (prevRow.length === 0) {
  142. prevRow = new Uint8Array(rowBytes);
  143. }
  144. let i,
  145. j = bufferLength,
  146. up,
  147. c;
  148. switch (predictor) {
  149. case 0:
  150. for (i = 0; i < rowBytes; ++i) {
  151. buffer[j++] = rawBytes[i];
  152. }
  153. break;
  154. case 1:
  155. for (i = 0; i < pixBytes; ++i) {
  156. buffer[j++] = rawBytes[i];
  157. }
  158. for (; i < rowBytes; ++i) {
  159. buffer[j] = buffer[j - pixBytes] + rawBytes[i] & 0xff;
  160. j++;
  161. }
  162. break;
  163. case 2:
  164. for (i = 0; i < rowBytes; ++i) {
  165. buffer[j++] = prevRow[i] + rawBytes[i] & 0xff;
  166. }
  167. break;
  168. case 3:
  169. for (i = 0; i < pixBytes; ++i) {
  170. buffer[j++] = (prevRow[i] >> 1) + rawBytes[i];
  171. }
  172. for (; i < rowBytes; ++i) {
  173. buffer[j] = (prevRow[i] + buffer[j - pixBytes] >> 1) + rawBytes[i] & 0xff;
  174. j++;
  175. }
  176. break;
  177. case 4:
  178. for (i = 0; i < pixBytes; ++i) {
  179. up = prevRow[i];
  180. c = rawBytes[i];
  181. buffer[j++] = up + c;
  182. }
  183. for (; i < rowBytes; ++i) {
  184. up = prevRow[i];
  185. const upLeft = prevRow[i - pixBytes];
  186. const left = buffer[j - pixBytes];
  187. const p = left + up - upLeft;
  188. let pa = p - left;
  189. if (pa < 0) {
  190. pa = -pa;
  191. }
  192. let pb = p - up;
  193. if (pb < 0) {
  194. pb = -pb;
  195. }
  196. let pc = p - upLeft;
  197. if (pc < 0) {
  198. pc = -pc;
  199. }
  200. c = rawBytes[i];
  201. if (pa <= pb && pa <= pc) {
  202. buffer[j++] = left + c;
  203. } else if (pb <= pc) {
  204. buffer[j++] = up + c;
  205. } else {
  206. buffer[j++] = upLeft + c;
  207. }
  208. }
  209. break;
  210. default:
  211. throw new _util.FormatError(`Unsupported predictor: ${predictor}`);
  212. }
  213. this.bufferLength += rowBytes;
  214. }
  215. }
  216. exports.PredictorStream = PredictorStream;