image.js 18 KB

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