image.js 18 KB

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