image.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.PDFImage = void 0;
  27. var _util = require("../shared/util");
  28. var _primitives = require("./primitives");
  29. var _colorspace = require("./colorspace");
  30. var _stream = require("./stream");
  31. var _jpeg_stream = require("./jpeg_stream");
  32. var _jpx = require("./jpx");
  33. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  34. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  35. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  36. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  37. var PDFImage = function PDFImageClosure() {
  38. function handleImageData(image, nativeDecoder) {
  39. if (nativeDecoder && nativeDecoder.canDecode(image)) {
  40. return nativeDecoder.decode(image)["catch"](function (reason) {
  41. (0, _util.warn)('Native image decoding failed -- trying to recover: ' + (reason && reason.message));
  42. return image;
  43. });
  44. }
  45. return Promise.resolve(image);
  46. }
  47. function decodeAndClamp(value, addend, coefficient, max) {
  48. value = addend + value * coefficient;
  49. return value < 0 ? 0 : value > max ? max : value;
  50. }
  51. function resizeImageMask(src, bpc, w1, h1, w2, h2) {
  52. var length = w2 * h2;
  53. var dest = bpc <= 8 ? new Uint8Array(length) : bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
  54. var xRatio = w1 / w2;
  55. var yRatio = h1 / h2;
  56. var i,
  57. j,
  58. py,
  59. newIndex = 0,
  60. oldIndex;
  61. var xScaled = new Uint16Array(w2);
  62. var w1Scanline = w1;
  63. for (i = 0; i < w2; i++) {
  64. xScaled[i] = Math.floor(i * xRatio);
  65. }
  66. for (i = 0; i < h2; i++) {
  67. py = Math.floor(i * yRatio) * w1Scanline;
  68. for (j = 0; j < w2; j++) {
  69. oldIndex = py + xScaled[j];
  70. dest[newIndex++] = src[oldIndex];
  71. }
  72. }
  73. return dest;
  74. }
  75. function PDFImage(_ref) {
  76. var xref = _ref.xref,
  77. res = _ref.res,
  78. image = _ref.image,
  79. _ref$isInline = _ref.isInline,
  80. isInline = _ref$isInline === void 0 ? false : _ref$isInline,
  81. _ref$smask = _ref.smask,
  82. smask = _ref$smask === void 0 ? null : _ref$smask,
  83. _ref$mask = _ref.mask,
  84. mask = _ref$mask === void 0 ? null : _ref$mask,
  85. _ref$isMask = _ref.isMask,
  86. isMask = _ref$isMask === void 0 ? false : _ref$isMask,
  87. pdfFunctionFactory = _ref.pdfFunctionFactory;
  88. this.image = image;
  89. var dict = image.dict;
  90. var filter = dict.get('Filter');
  91. if ((0, _primitives.isName)(filter)) {
  92. switch (filter.name) {
  93. case 'JPXDecode':
  94. var jpxImage = new _jpx.JpxImage();
  95. jpxImage.parseImageProperties(image.stream);
  96. image.stream.reset();
  97. image.width = jpxImage.width;
  98. image.height = jpxImage.height;
  99. image.bitsPerComponent = jpxImage.bitsPerComponent;
  100. image.numComps = jpxImage.componentsCount;
  101. break;
  102. case 'JBIG2Decode':
  103. image.bitsPerComponent = 1;
  104. image.numComps = 1;
  105. break;
  106. }
  107. }
  108. var width = dict.get('Width', 'W');
  109. var height = dict.get('Height', 'H');
  110. if (Number.isInteger(image.width) && image.width > 0 && Number.isInteger(image.height) && image.height > 0 && (image.width !== width || image.height !== height)) {
  111. (0, _util.warn)('PDFImage - using the Width/Height of the image data, ' + 'rather than the image dictionary.');
  112. width = image.width;
  113. height = image.height;
  114. }
  115. if (width < 1 || height < 1) {
  116. throw new _util.FormatError("Invalid image width: ".concat(width, " or ") + "height: ".concat(height));
  117. }
  118. this.width = width;
  119. this.height = height;
  120. this.interpolate = dict.get('Interpolate', 'I') || false;
  121. this.imageMask = dict.get('ImageMask', 'IM') || false;
  122. this.matte = dict.get('Matte') || false;
  123. var bitsPerComponent = image.bitsPerComponent;
  124. if (!bitsPerComponent) {
  125. bitsPerComponent = dict.get('BitsPerComponent', 'BPC');
  126. if (!bitsPerComponent) {
  127. if (this.imageMask) {
  128. bitsPerComponent = 1;
  129. } else {
  130. throw new _util.FormatError("Bits per component missing in image: ".concat(this.imageMask));
  131. }
  132. }
  133. }
  134. this.bpc = bitsPerComponent;
  135. if (!this.imageMask) {
  136. var colorSpace = dict.get('ColorSpace', 'CS');
  137. if (!colorSpace) {
  138. (0, _util.info)('JPX images (which do not require color spaces)');
  139. switch (image.numComps) {
  140. case 1:
  141. colorSpace = _primitives.Name.get('DeviceGray');
  142. break;
  143. case 3:
  144. colorSpace = _primitives.Name.get('DeviceRGB');
  145. break;
  146. case 4:
  147. colorSpace = _primitives.Name.get('DeviceCMYK');
  148. break;
  149. default:
  150. throw new Error("JPX images with ".concat(image.numComps, " ") + 'color components not supported.');
  151. }
  152. }
  153. var resources = isInline ? res : null;
  154. this.colorSpace = _colorspace.ColorSpace.parse(colorSpace, xref, resources, pdfFunctionFactory);
  155. this.numComps = this.colorSpace.numComps;
  156. }
  157. this.decode = dict.getArray('Decode', 'D');
  158. this.needsDecode = false;
  159. if (this.decode && (this.colorSpace && !this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent) || isMask && !_colorspace.ColorSpace.isDefaultDecode(this.decode, 1))) {
  160. this.needsDecode = true;
  161. var max = (1 << bitsPerComponent) - 1;
  162. this.decodeCoefficients = [];
  163. this.decodeAddends = [];
  164. var isIndexed = this.colorSpace && this.colorSpace.name === 'Indexed';
  165. for (var i = 0, j = 0; i < this.decode.length; i += 2, ++j) {
  166. var dmin = this.decode[i];
  167. var dmax = this.decode[i + 1];
  168. this.decodeCoefficients[j] = isIndexed ? (dmax - dmin) / max : dmax - dmin;
  169. this.decodeAddends[j] = isIndexed ? dmin : max * dmin;
  170. }
  171. }
  172. if (smask) {
  173. this.smask = new PDFImage({
  174. xref: xref,
  175. res: res,
  176. image: smask,
  177. isInline: isInline,
  178. pdfFunctionFactory: pdfFunctionFactory
  179. });
  180. } else if (mask) {
  181. if ((0, _primitives.isStream)(mask)) {
  182. var maskDict = mask.dict,
  183. imageMask = maskDict.get('ImageMask', 'IM');
  184. if (!imageMask) {
  185. (0, _util.warn)('Ignoring /Mask in image without /ImageMask.');
  186. } else {
  187. this.mask = new PDFImage({
  188. xref: xref,
  189. res: res,
  190. image: mask,
  191. isInline: isInline,
  192. isMask: true,
  193. pdfFunctionFactory: pdfFunctionFactory
  194. });
  195. }
  196. } else {
  197. this.mask = mask;
  198. }
  199. }
  200. }
  201. PDFImage.buildImage = function (_ref2) {
  202. var handler = _ref2.handler,
  203. xref = _ref2.xref,
  204. res = _ref2.res,
  205. image = _ref2.image,
  206. _ref2$isInline = _ref2.isInline,
  207. isInline = _ref2$isInline === void 0 ? false : _ref2$isInline,
  208. _ref2$nativeDecoder = _ref2.nativeDecoder,
  209. nativeDecoder = _ref2$nativeDecoder === void 0 ? null : _ref2$nativeDecoder,
  210. pdfFunctionFactory = _ref2.pdfFunctionFactory;
  211. var imagePromise = handleImageData(image, nativeDecoder);
  212. var smaskPromise;
  213. var maskPromise;
  214. var smask = image.dict.get('SMask');
  215. var mask = image.dict.get('Mask');
  216. if (smask) {
  217. smaskPromise = handleImageData(smask, nativeDecoder);
  218. maskPromise = Promise.resolve(null);
  219. } else {
  220. smaskPromise = Promise.resolve(null);
  221. if (mask) {
  222. if ((0, _primitives.isStream)(mask)) {
  223. maskPromise = handleImageData(mask, nativeDecoder);
  224. } else if (Array.isArray(mask)) {
  225. maskPromise = Promise.resolve(mask);
  226. } else {
  227. (0, _util.warn)('Unsupported mask format.');
  228. maskPromise = Promise.resolve(null);
  229. }
  230. } else {
  231. maskPromise = Promise.resolve(null);
  232. }
  233. }
  234. return Promise.all([imagePromise, smaskPromise, maskPromise]).then(function (_ref3) {
  235. var _ref4 = _slicedToArray(_ref3, 3),
  236. imageData = _ref4[0],
  237. smaskData = _ref4[1],
  238. maskData = _ref4[2];
  239. return new PDFImage({
  240. xref: xref,
  241. res: res,
  242. image: imageData,
  243. isInline: isInline,
  244. smask: smaskData,
  245. mask: maskData,
  246. pdfFunctionFactory: pdfFunctionFactory
  247. });
  248. });
  249. };
  250. PDFImage.createMask = function (_ref5) {
  251. var imgArray = _ref5.imgArray,
  252. width = _ref5.width,
  253. height = _ref5.height,
  254. imageIsFromDecodeStream = _ref5.imageIsFromDecodeStream,
  255. inverseDecode = _ref5.inverseDecode;
  256. var computedLength = (width + 7 >> 3) * height;
  257. var actualLength = imgArray.byteLength;
  258. var haveFullData = computedLength === actualLength;
  259. var data, i;
  260. if (imageIsFromDecodeStream && (!inverseDecode || haveFullData)) {
  261. data = imgArray;
  262. } else if (!inverseDecode) {
  263. data = new Uint8ClampedArray(actualLength);
  264. data.set(imgArray);
  265. } else {
  266. data = new Uint8ClampedArray(computedLength);
  267. data.set(imgArray);
  268. for (i = actualLength; i < computedLength; i++) {
  269. data[i] = 0xff;
  270. }
  271. }
  272. if (inverseDecode) {
  273. for (i = 0; i < actualLength; i++) {
  274. data[i] ^= 0xFF;
  275. }
  276. }
  277. return {
  278. data: data,
  279. width: width,
  280. height: height
  281. };
  282. };
  283. PDFImage.prototype = {
  284. get drawWidth() {
  285. return Math.max(this.width, this.smask && this.smask.width || 0, this.mask && this.mask.width || 0);
  286. },
  287. get drawHeight() {
  288. return Math.max(this.height, this.smask && this.smask.height || 0, this.mask && this.mask.height || 0);
  289. },
  290. decodeBuffer: function decodeBuffer(buffer) {
  291. var bpc = this.bpc;
  292. var numComps = this.numComps;
  293. var decodeAddends = this.decodeAddends;
  294. var decodeCoefficients = this.decodeCoefficients;
  295. var max = (1 << bpc) - 1;
  296. var i, ii;
  297. if (bpc === 1) {
  298. for (i = 0, ii = buffer.length; i < ii; i++) {
  299. buffer[i] = +!buffer[i];
  300. }
  301. return;
  302. }
  303. var index = 0;
  304. for (i = 0, ii = this.width * this.height; i < ii; i++) {
  305. for (var j = 0; j < numComps; j++) {
  306. buffer[index] = decodeAndClamp(buffer[index], decodeAddends[j], decodeCoefficients[j], max);
  307. index++;
  308. }
  309. }
  310. },
  311. getComponents: function getComponents(buffer) {
  312. var bpc = this.bpc;
  313. if (bpc === 8) {
  314. return buffer;
  315. }
  316. var width = this.width;
  317. var height = this.height;
  318. var numComps = this.numComps;
  319. var length = width * height * numComps;
  320. var bufferPos = 0;
  321. var output = bpc <= 8 ? new Uint8Array(length) : bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length);
  322. var rowComps = width * numComps;
  323. var max = (1 << bpc) - 1;
  324. var i = 0,
  325. ii,
  326. buf;
  327. if (bpc === 1) {
  328. var mask, loop1End, loop2End;
  329. for (var j = 0; j < height; j++) {
  330. loop1End = i + (rowComps & ~7);
  331. loop2End = i + rowComps;
  332. while (i < loop1End) {
  333. buf = buffer[bufferPos++];
  334. output[i] = buf >> 7 & 1;
  335. output[i + 1] = buf >> 6 & 1;
  336. output[i + 2] = buf >> 5 & 1;
  337. output[i + 3] = buf >> 4 & 1;
  338. output[i + 4] = buf >> 3 & 1;
  339. output[i + 5] = buf >> 2 & 1;
  340. output[i + 6] = buf >> 1 & 1;
  341. output[i + 7] = buf & 1;
  342. i += 8;
  343. }
  344. if (i < loop2End) {
  345. buf = buffer[bufferPos++];
  346. mask = 128;
  347. while (i < loop2End) {
  348. output[i++] = +!!(buf & mask);
  349. mask >>= 1;
  350. }
  351. }
  352. }
  353. } else {
  354. var bits = 0;
  355. buf = 0;
  356. for (i = 0, ii = length; i < ii; ++i) {
  357. if (i % rowComps === 0) {
  358. buf = 0;
  359. bits = 0;
  360. }
  361. while (bits < bpc) {
  362. buf = buf << 8 | buffer[bufferPos++];
  363. bits += 8;
  364. }
  365. var remainingBits = bits - bpc;
  366. var value = buf >> remainingBits;
  367. output[i] = value < 0 ? 0 : value > max ? max : value;
  368. buf = buf & (1 << remainingBits) - 1;
  369. bits = remainingBits;
  370. }
  371. }
  372. return output;
  373. },
  374. fillOpacity: function fillOpacity(rgbaBuf, width, height, actualHeight, image) {
  375. var smask = this.smask;
  376. var mask = this.mask;
  377. var alphaBuf, sw, sh, i, ii, j;
  378. if (smask) {
  379. sw = smask.width;
  380. sh = smask.height;
  381. alphaBuf = new Uint8ClampedArray(sw * sh);
  382. smask.fillGrayBuffer(alphaBuf);
  383. if (sw !== width || sh !== height) {
  384. alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh, width, height);
  385. }
  386. } else if (mask) {
  387. if (mask instanceof PDFImage) {
  388. sw = mask.width;
  389. sh = mask.height;
  390. alphaBuf = new Uint8ClampedArray(sw * sh);
  391. mask.numComps = 1;
  392. mask.fillGrayBuffer(alphaBuf);
  393. for (i = 0, ii = sw * sh; i < ii; ++i) {
  394. alphaBuf[i] = 255 - alphaBuf[i];
  395. }
  396. if (sw !== width || sh !== height) {
  397. alphaBuf = resizeImageMask(alphaBuf, mask.bpc, sw, sh, width, height);
  398. }
  399. } else if (Array.isArray(mask)) {
  400. alphaBuf = new Uint8ClampedArray(width * height);
  401. var numComps = this.numComps;
  402. for (i = 0, ii = width * height; i < ii; ++i) {
  403. var opacity = 0;
  404. var imageOffset = i * numComps;
  405. for (j = 0; j < numComps; ++j) {
  406. var color = image[imageOffset + j];
  407. var maskOffset = j * 2;
  408. if (color < mask[maskOffset] || color > mask[maskOffset + 1]) {
  409. opacity = 255;
  410. break;
  411. }
  412. }
  413. alphaBuf[i] = opacity;
  414. }
  415. } else {
  416. throw new _util.FormatError('Unknown mask format.');
  417. }
  418. }
  419. if (alphaBuf) {
  420. for (i = 0, j = 3, ii = width * actualHeight; i < ii; ++i, j += 4) {
  421. rgbaBuf[j] = alphaBuf[i];
  422. }
  423. } else {
  424. for (i = 0, j = 3, ii = width * actualHeight; i < ii; ++i, j += 4) {
  425. rgbaBuf[j] = 255;
  426. }
  427. }
  428. },
  429. undoPreblend: function undoPreblend(buffer, width, height) {
  430. var matte = this.smask && this.smask.matte;
  431. if (!matte) {
  432. return;
  433. }
  434. var matteRgb = this.colorSpace.getRgb(matte, 0);
  435. var matteR = matteRgb[0];
  436. var matteG = matteRgb[1];
  437. var matteB = matteRgb[2];
  438. var length = width * height * 4;
  439. for (var i = 0; i < length; i += 4) {
  440. var alpha = buffer[i + 3];
  441. if (alpha === 0) {
  442. buffer[i] = 255;
  443. buffer[i + 1] = 255;
  444. buffer[i + 2] = 255;
  445. continue;
  446. }
  447. var k = 255 / alpha;
  448. buffer[i] = (buffer[i] - matteR) * k + matteR;
  449. buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG;
  450. buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB;
  451. }
  452. },
  453. createImageData: function createImageData() {
  454. var forceRGBA = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  455. var drawWidth = this.drawWidth;
  456. var drawHeight = this.drawHeight;
  457. var imgData = {
  458. width: drawWidth,
  459. height: drawHeight,
  460. kind: 0,
  461. data: null
  462. };
  463. var numComps = this.numComps;
  464. var originalWidth = this.width;
  465. var originalHeight = this.height;
  466. var bpc = this.bpc;
  467. var rowBytes = originalWidth * numComps * bpc + 7 >> 3;
  468. var imgArray;
  469. if (!forceRGBA) {
  470. var kind;
  471. if (this.colorSpace.name === 'DeviceGray' && bpc === 1) {
  472. kind = _util.ImageKind.GRAYSCALE_1BPP;
  473. } else if (this.colorSpace.name === 'DeviceRGB' && bpc === 8 && !this.needsDecode) {
  474. kind = _util.ImageKind.RGB_24BPP;
  475. }
  476. if (kind && !this.smask && !this.mask && drawWidth === originalWidth && drawHeight === originalHeight) {
  477. imgData.kind = kind;
  478. imgArray = this.getImageBytes(originalHeight * rowBytes);
  479. if (this.image instanceof _stream.DecodeStream) {
  480. imgData.data = imgArray;
  481. } else {
  482. var newArray = new Uint8ClampedArray(imgArray.length);
  483. newArray.set(imgArray);
  484. imgData.data = newArray;
  485. }
  486. if (this.needsDecode) {
  487. (0, _util.assert)(kind === _util.ImageKind.GRAYSCALE_1BPP, 'PDFImage.createImageData: The image must be grayscale.');
  488. var buffer = imgData.data;
  489. for (var i = 0, ii = buffer.length; i < ii; i++) {
  490. buffer[i] ^= 0xff;
  491. }
  492. }
  493. return imgData;
  494. }
  495. if (this.image instanceof _jpeg_stream.JpegStream && !this.smask && !this.mask) {
  496. var imageLength = originalHeight * rowBytes;
  497. switch (this.colorSpace.name) {
  498. case 'DeviceGray':
  499. imageLength *= 3;
  500. case 'DeviceRGB':
  501. case 'DeviceCMYK':
  502. imgData.kind = _util.ImageKind.RGB_24BPP;
  503. imgData.data = this.getImageBytes(imageLength, drawWidth, drawHeight, true);
  504. return imgData;
  505. }
  506. }
  507. }
  508. imgArray = this.getImageBytes(originalHeight * rowBytes);
  509. var actualHeight = 0 | imgArray.length / rowBytes * drawHeight / originalHeight;
  510. var comps = this.getComponents(imgArray);
  511. var alpha01, maybeUndoPreblend;
  512. if (!forceRGBA && !this.smask && !this.mask) {
  513. imgData.kind = _util.ImageKind.RGB_24BPP;
  514. imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3);
  515. alpha01 = 0;
  516. maybeUndoPreblend = false;
  517. } else {
  518. imgData.kind = _util.ImageKind.RGBA_32BPP;
  519. imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 4);
  520. alpha01 = 1;
  521. maybeUndoPreblend = true;
  522. this.fillOpacity(imgData.data, drawWidth, drawHeight, actualHeight, comps);
  523. }
  524. if (this.needsDecode) {
  525. this.decodeBuffer(comps);
  526. }
  527. this.colorSpace.fillRgb(imgData.data, originalWidth, originalHeight, drawWidth, drawHeight, actualHeight, bpc, comps, alpha01);
  528. if (maybeUndoPreblend) {
  529. this.undoPreblend(imgData.data, drawWidth, actualHeight);
  530. }
  531. return imgData;
  532. },
  533. fillGrayBuffer: function fillGrayBuffer(buffer) {
  534. var numComps = this.numComps;
  535. if (numComps !== 1) {
  536. throw new _util.FormatError("Reading gray scale from a color image: ".concat(numComps));
  537. }
  538. var width = this.width;
  539. var height = this.height;
  540. var bpc = this.bpc;
  541. var rowBytes = width * numComps * bpc + 7 >> 3;
  542. var imgArray = this.getImageBytes(height * rowBytes);
  543. var comps = this.getComponents(imgArray);
  544. var i, length;
  545. if (bpc === 1) {
  546. length = width * height;
  547. if (this.needsDecode) {
  548. for (i = 0; i < length; ++i) {
  549. buffer[i] = comps[i] - 1 & 255;
  550. }
  551. } else {
  552. for (i = 0; i < length; ++i) {
  553. buffer[i] = -comps[i] & 255;
  554. }
  555. }
  556. return;
  557. }
  558. if (this.needsDecode) {
  559. this.decodeBuffer(comps);
  560. }
  561. length = width * height;
  562. var scale = 255 / ((1 << bpc) - 1);
  563. for (i = 0; i < length; ++i) {
  564. buffer[i] = scale * comps[i];
  565. }
  566. },
  567. getImageBytes: function getImageBytes(length, drawWidth, drawHeight) {
  568. var forceRGB = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  569. this.image.reset();
  570. this.image.drawWidth = drawWidth || this.width;
  571. this.image.drawHeight = drawHeight || this.height;
  572. this.image.forceRGB = !!forceRGB;
  573. return this.image.getBytes(length, true);
  574. }
  575. };
  576. return PDFImage;
  577. }();
  578. exports.PDFImage = PDFImage;