2
0

image.js 19 KB

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