image.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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") || false;
  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. }) {
  245. const computedLength = (width + 7 >> 3) * height;
  246. const actualLength = imgArray.byteLength;
  247. const haveFullData = computedLength === actualLength;
  248. let data, i;
  249. if (imageIsFromDecodeStream && (!inverseDecode || haveFullData)) {
  250. data = imgArray;
  251. } else if (!inverseDecode) {
  252. data = new Uint8ClampedArray(actualLength);
  253. data.set(imgArray);
  254. } else {
  255. data = new Uint8ClampedArray(computedLength);
  256. data.set(imgArray);
  257. for (i = actualLength; i < computedLength; i++) {
  258. data[i] = 0xff;
  259. }
  260. }
  261. if (inverseDecode) {
  262. for (i = 0; i < actualLength; i++) {
  263. data[i] ^= 0xff;
  264. }
  265. }
  266. return {
  267. data,
  268. width,
  269. height
  270. };
  271. }
  272. get drawWidth() {
  273. return Math.max(this.width, this.smask && this.smask.width || 0, this.mask && this.mask.width || 0);
  274. }
  275. get drawHeight() {
  276. return Math.max(this.height, this.smask && this.smask.height || 0, this.mask && this.mask.height || 0);
  277. }
  278. decodeBuffer(buffer) {
  279. const bpc = this.bpc;
  280. const numComps = this.numComps;
  281. const decodeAddends = this.decodeAddends;
  282. const decodeCoefficients = this.decodeCoefficients;
  283. const max = (1 << bpc) - 1;
  284. let i, ii;
  285. if (bpc === 1) {
  286. for (i = 0, ii = buffer.length; i < ii; i++) {
  287. buffer[i] = +!buffer[i];
  288. }
  289. return;
  290. }
  291. let index = 0;
  292. for (i = 0, ii = this.width * this.height; i < ii; i++) {
  293. for (let j = 0; j < numComps; j++) {
  294. buffer[index] = decodeAndClamp(buffer[index], decodeAddends[j], decodeCoefficients[j], max);
  295. index++;
  296. }
  297. }
  298. }
  299. getComponents(buffer) {
  300. const bpc = this.bpc;
  301. if (bpc === 8) {
  302. return buffer;
  303. }
  304. const width = this.width;
  305. const height = this.height;
  306. const numComps = this.numComps;
  307. const length = width * height * numComps;
  308. let bufferPos = 0;
  309. let output;
  310. if (bpc <= 8) {
  311. output = new Uint8Array(length);
  312. } else if (bpc <= 16) {
  313. output = new Uint16Array(length);
  314. } else {
  315. output = new Uint32Array(length);
  316. }
  317. const rowComps = width * numComps;
  318. const max = (1 << bpc) - 1;
  319. let i = 0,
  320. ii,
  321. buf;
  322. if (bpc === 1) {
  323. let mask, loop1End, loop2End;
  324. for (let j = 0; j < height; j++) {
  325. loop1End = i + (rowComps & ~7);
  326. loop2End = i + rowComps;
  327. while (i < loop1End) {
  328. buf = buffer[bufferPos++];
  329. output[i] = buf >> 7 & 1;
  330. output[i + 1] = buf >> 6 & 1;
  331. output[i + 2] = buf >> 5 & 1;
  332. output[i + 3] = buf >> 4 & 1;
  333. output[i + 4] = buf >> 3 & 1;
  334. output[i + 5] = buf >> 2 & 1;
  335. output[i + 6] = buf >> 1 & 1;
  336. output[i + 7] = buf & 1;
  337. i += 8;
  338. }
  339. if (i < loop2End) {
  340. buf = buffer[bufferPos++];
  341. mask = 128;
  342. while (i < loop2End) {
  343. output[i++] = +!!(buf & mask);
  344. mask >>= 1;
  345. }
  346. }
  347. }
  348. } else {
  349. let bits = 0;
  350. buf = 0;
  351. for (i = 0, ii = length; i < ii; ++i) {
  352. if (i % rowComps === 0) {
  353. buf = 0;
  354. bits = 0;
  355. }
  356. while (bits < bpc) {
  357. buf = buf << 8 | buffer[bufferPos++];
  358. bits += 8;
  359. }
  360. const remainingBits = bits - bpc;
  361. let value = buf >> remainingBits;
  362. if (value < 0) {
  363. value = 0;
  364. } else if (value > max) {
  365. value = max;
  366. }
  367. output[i] = value;
  368. buf = buf & (1 << remainingBits) - 1;
  369. bits = remainingBits;
  370. }
  371. }
  372. return output;
  373. }
  374. fillOpacity(rgbaBuf, width, height, actualHeight, image) {
  375. const smask = this.smask;
  376. const mask = this.mask;
  377. let 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. const numComps = this.numComps;
  402. for (i = 0, ii = width * height; i < ii; ++i) {
  403. let opacity = 0;
  404. const imageOffset = i * numComps;
  405. for (j = 0; j < numComps; ++j) {
  406. const color = image[imageOffset + j];
  407. const 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(buffer, width, height) {
  430. const matte = this.smask && this.smask.matte;
  431. if (!matte) {
  432. return;
  433. }
  434. const matteRgb = this.colorSpace.getRgb(matte, 0);
  435. const matteR = matteRgb[0];
  436. const matteG = matteRgb[1];
  437. const matteB = matteRgb[2];
  438. const length = width * height * 4;
  439. for (let i = 0; i < length; i += 4) {
  440. const 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. const 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(forceRGBA = false) {
  454. const drawWidth = this.drawWidth;
  455. const drawHeight = this.drawHeight;
  456. const imgData = {
  457. width: drawWidth,
  458. height: drawHeight,
  459. kind: 0,
  460. data: null
  461. };
  462. const numComps = this.numComps;
  463. const originalWidth = this.width;
  464. const originalHeight = this.height;
  465. const bpc = this.bpc;
  466. const rowBytes = originalWidth * numComps * bpc + 7 >> 3;
  467. let imgArray;
  468. if (!forceRGBA) {
  469. let kind;
  470. if (this.colorSpace.name === "DeviceGray" && bpc === 1) {
  471. kind = _util.ImageKind.GRAYSCALE_1BPP;
  472. } else if (this.colorSpace.name === "DeviceRGB" && bpc === 8 && !this.needsDecode) {
  473. kind = _util.ImageKind.RGB_24BPP;
  474. }
  475. if (kind && !this.smask && !this.mask && drawWidth === originalWidth && drawHeight === originalHeight) {
  476. imgData.kind = kind;
  477. imgArray = this.getImageBytes(originalHeight * rowBytes);
  478. if (this.image instanceof _decode_stream.DecodeStream) {
  479. imgData.data = imgArray;
  480. } else {
  481. const newArray = new Uint8ClampedArray(imgArray.length);
  482. newArray.set(imgArray);
  483. imgData.data = newArray;
  484. }
  485. if (this.needsDecode) {
  486. (0, _util.assert)(kind === _util.ImageKind.GRAYSCALE_1BPP, "PDFImage.createImageData: The image must be grayscale.");
  487. const buffer = imgData.data;
  488. for (let i = 0, ii = buffer.length; i < ii; i++) {
  489. buffer[i] ^= 0xff;
  490. }
  491. }
  492. return imgData;
  493. }
  494. if (this.image instanceof _jpeg_stream.JpegStream && !this.smask && !this.mask) {
  495. let imageLength = originalHeight * rowBytes;
  496. switch (this.colorSpace.name) {
  497. case "DeviceGray":
  498. imageLength *= 3;
  499. case "DeviceRGB":
  500. case "DeviceCMYK":
  501. imgData.kind = _util.ImageKind.RGB_24BPP;
  502. imgData.data = this.getImageBytes(imageLength, drawWidth, drawHeight, true);
  503. return imgData;
  504. }
  505. }
  506. }
  507. imgArray = this.getImageBytes(originalHeight * rowBytes);
  508. const actualHeight = 0 | imgArray.length / rowBytes * drawHeight / originalHeight;
  509. const comps = this.getComponents(imgArray);
  510. let alpha01, maybeUndoPreblend;
  511. if (!forceRGBA && !this.smask && !this.mask) {
  512. imgData.kind = _util.ImageKind.RGB_24BPP;
  513. imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3);
  514. alpha01 = 0;
  515. maybeUndoPreblend = false;
  516. } else {
  517. imgData.kind = _util.ImageKind.RGBA_32BPP;
  518. imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 4);
  519. alpha01 = 1;
  520. maybeUndoPreblend = true;
  521. this.fillOpacity(imgData.data, drawWidth, drawHeight, actualHeight, comps);
  522. }
  523. if (this.needsDecode) {
  524. this.decodeBuffer(comps);
  525. }
  526. this.colorSpace.fillRgb(imgData.data, originalWidth, originalHeight, drawWidth, drawHeight, actualHeight, bpc, comps, alpha01);
  527. if (maybeUndoPreblend) {
  528. this.undoPreblend(imgData.data, drawWidth, actualHeight);
  529. }
  530. return imgData;
  531. }
  532. fillGrayBuffer(buffer) {
  533. const numComps = this.numComps;
  534. if (numComps !== 1) {
  535. throw new _util.FormatError(`Reading gray scale from a color image: ${numComps}`);
  536. }
  537. const width = this.width;
  538. const height = this.height;
  539. const bpc = this.bpc;
  540. const rowBytes = width * numComps * bpc + 7 >> 3;
  541. const imgArray = this.getImageBytes(height * rowBytes);
  542. const comps = this.getComponents(imgArray);
  543. let i, length;
  544. if (bpc === 1) {
  545. length = width * height;
  546. if (this.needsDecode) {
  547. for (i = 0; i < length; ++i) {
  548. buffer[i] = comps[i] - 1 & 255;
  549. }
  550. } else {
  551. for (i = 0; i < length; ++i) {
  552. buffer[i] = -comps[i] & 255;
  553. }
  554. }
  555. return;
  556. }
  557. if (this.needsDecode) {
  558. this.decodeBuffer(comps);
  559. }
  560. length = width * height;
  561. const scale = 255 / ((1 << bpc) - 1);
  562. for (i = 0; i < length; ++i) {
  563. buffer[i] = scale * comps[i];
  564. }
  565. }
  566. getImageBytes(length, drawWidth, drawHeight, forceRGB = false) {
  567. this.image.reset();
  568. this.image.drawWidth = drawWidth || this.width;
  569. this.image.drawHeight = drawHeight || this.height;
  570. this.image.forceRGB = !!forceRGB;
  571. return this.image.getBytes(length, true);
  572. }
  573. }
  574. exports.PDFImage = PDFImage;