pixel-match.js 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. /*
  2. Stencil Screenshot Pixel Match v4.20.0 | MIT Licensed | https://stenciljs.com
  3. */
  4. "use strict";
  5. var __create = Object.create;
  6. var __defProp = Object.defineProperty;
  7. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  8. var __getOwnPropNames = Object.getOwnPropertyNames;
  9. var __getProtoOf = Object.getPrototypeOf;
  10. var __hasOwnProp = Object.prototype.hasOwnProperty;
  11. var __commonJS = (cb, mod) => function __require() {
  12. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  13. };
  14. var __copyProps = (to, from, except, desc) => {
  15. if (from && typeof from === "object" || typeof from === "function") {
  16. for (let key of __getOwnPropNames(from))
  17. if (!__hasOwnProp.call(to, key) && key !== except)
  18. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  19. }
  20. return to;
  21. };
  22. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  23. // If the importer is in node compatibility mode or this is not an ESM
  24. // file that has been converted to a CommonJS file using a Babel-
  25. // compatible transform (i.e. "__esModule" has not been set), then set
  26. // "default" to the CommonJS "module.exports" for node compatibility.
  27. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  28. mod
  29. ));
  30. // node_modules/pixelmatch/index.js
  31. var require_pixelmatch = __commonJS({
  32. "node_modules/pixelmatch/index.js"(exports2, module2) {
  33. "use strict";
  34. module2.exports = pixelmatch2;
  35. var defaultOptions = {
  36. threshold: 0.1,
  37. // matching threshold (0 to 1); smaller is more sensitive
  38. includeAA: false,
  39. // whether to skip anti-aliasing detection
  40. alpha: 0.1,
  41. // opacity of original image in diff output
  42. aaColor: [255, 255, 0],
  43. // color of anti-aliased pixels in diff output
  44. diffColor: [255, 0, 0],
  45. // color of different pixels in diff output
  46. diffColorAlt: null,
  47. // whether to detect dark on light differences between img1 and img2 and set an alternative color to differentiate between the two
  48. diffMask: false
  49. // draw the diff over a transparent background (a mask)
  50. };
  51. function pixelmatch2(img1, img2, output, width, height, options) {
  52. if (!isPixelData(img1) || !isPixelData(img2) || output && !isPixelData(output))
  53. throw new Error("Image data: Uint8Array, Uint8ClampedArray or Buffer expected.");
  54. if (img1.length !== img2.length || output && output.length !== img1.length)
  55. throw new Error("Image sizes do not match.");
  56. if (img1.length !== width * height * 4) throw new Error("Image data size does not match width/height.");
  57. options = Object.assign({}, defaultOptions, options);
  58. const len = width * height;
  59. const a32 = new Uint32Array(img1.buffer, img1.byteOffset, len);
  60. const b32 = new Uint32Array(img2.buffer, img2.byteOffset, len);
  61. let identical = true;
  62. for (let i = 0; i < len; i++) {
  63. if (a32[i] !== b32[i]) {
  64. identical = false;
  65. break;
  66. }
  67. }
  68. if (identical) {
  69. if (output && !options.diffMask) {
  70. for (let i = 0; i < len; i++) drawGrayPixel(img1, 4 * i, options.alpha, output);
  71. }
  72. return 0;
  73. }
  74. const maxDelta = 35215 * options.threshold * options.threshold;
  75. let diff = 0;
  76. for (let y = 0; y < height; y++) {
  77. for (let x = 0; x < width; x++) {
  78. const pos = (y * width + x) * 4;
  79. const delta = colorDelta(img1, img2, pos, pos);
  80. if (Math.abs(delta) > maxDelta) {
  81. if (!options.includeAA && (antialiased(img1, x, y, width, height, img2) || antialiased(img2, x, y, width, height, img1))) {
  82. if (output && !options.diffMask) drawPixel(output, pos, ...options.aaColor);
  83. } else {
  84. if (output) {
  85. drawPixel(output, pos, ...delta < 0 && options.diffColorAlt || options.diffColor);
  86. }
  87. diff++;
  88. }
  89. } else if (output) {
  90. if (!options.diffMask) drawGrayPixel(img1, pos, options.alpha, output);
  91. }
  92. }
  93. }
  94. return diff;
  95. }
  96. function isPixelData(arr) {
  97. return ArrayBuffer.isView(arr) && arr.constructor.BYTES_PER_ELEMENT === 1;
  98. }
  99. function antialiased(img, x1, y1, width, height, img2) {
  100. const x0 = Math.max(x1 - 1, 0);
  101. const y0 = Math.max(y1 - 1, 0);
  102. const x2 = Math.min(x1 + 1, width - 1);
  103. const y2 = Math.min(y1 + 1, height - 1);
  104. const pos = (y1 * width + x1) * 4;
  105. let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;
  106. let min = 0;
  107. let max = 0;
  108. let minX, minY, maxX, maxY;
  109. for (let x = x0; x <= x2; x++) {
  110. for (let y = y0; y <= y2; y++) {
  111. if (x === x1 && y === y1) continue;
  112. const delta = colorDelta(img, img, pos, (y * width + x) * 4, true);
  113. if (delta === 0) {
  114. zeroes++;
  115. if (zeroes > 2) return false;
  116. } else if (delta < min) {
  117. min = delta;
  118. minX = x;
  119. minY = y;
  120. } else if (delta > max) {
  121. max = delta;
  122. maxX = x;
  123. maxY = y;
  124. }
  125. }
  126. }
  127. if (min === 0 || max === 0) return false;
  128. return hasManySiblings(img, minX, minY, width, height) && hasManySiblings(img2, minX, minY, width, height) || hasManySiblings(img, maxX, maxY, width, height) && hasManySiblings(img2, maxX, maxY, width, height);
  129. }
  130. function hasManySiblings(img, x1, y1, width, height) {
  131. const x0 = Math.max(x1 - 1, 0);
  132. const y0 = Math.max(y1 - 1, 0);
  133. const x2 = Math.min(x1 + 1, width - 1);
  134. const y2 = Math.min(y1 + 1, height - 1);
  135. const pos = (y1 * width + x1) * 4;
  136. let zeroes = x1 === x0 || x1 === x2 || y1 === y0 || y1 === y2 ? 1 : 0;
  137. for (let x = x0; x <= x2; x++) {
  138. for (let y = y0; y <= y2; y++) {
  139. if (x === x1 && y === y1) continue;
  140. const pos2 = (y * width + x) * 4;
  141. if (img[pos] === img[pos2] && img[pos + 1] === img[pos2 + 1] && img[pos + 2] === img[pos2 + 2] && img[pos + 3] === img[pos2 + 3]) zeroes++;
  142. if (zeroes > 2) return true;
  143. }
  144. }
  145. return false;
  146. }
  147. function colorDelta(img1, img2, k, m, yOnly) {
  148. let r1 = img1[k + 0];
  149. let g1 = img1[k + 1];
  150. let b1 = img1[k + 2];
  151. let a1 = img1[k + 3];
  152. let r2 = img2[m + 0];
  153. let g2 = img2[m + 1];
  154. let b2 = img2[m + 2];
  155. let a2 = img2[m + 3];
  156. if (a1 === a2 && r1 === r2 && g1 === g2 && b1 === b2) return 0;
  157. if (a1 < 255) {
  158. a1 /= 255;
  159. r1 = blend(r1, a1);
  160. g1 = blend(g1, a1);
  161. b1 = blend(b1, a1);
  162. }
  163. if (a2 < 255) {
  164. a2 /= 255;
  165. r2 = blend(r2, a2);
  166. g2 = blend(g2, a2);
  167. b2 = blend(b2, a2);
  168. }
  169. const y1 = rgb2y(r1, g1, b1);
  170. const y2 = rgb2y(r2, g2, b2);
  171. const y = y1 - y2;
  172. if (yOnly) return y;
  173. const i = rgb2i(r1, g1, b1) - rgb2i(r2, g2, b2);
  174. const q = rgb2q(r1, g1, b1) - rgb2q(r2, g2, b2);
  175. const delta = 0.5053 * y * y + 0.299 * i * i + 0.1957 * q * q;
  176. return y1 > y2 ? -delta : delta;
  177. }
  178. function rgb2y(r, g, b) {
  179. return r * 0.29889531 + g * 0.58662247 + b * 0.11448223;
  180. }
  181. function rgb2i(r, g, b) {
  182. return r * 0.59597799 - g * 0.2741761 - b * 0.32180189;
  183. }
  184. function rgb2q(r, g, b) {
  185. return r * 0.21147017 - g * 0.52261711 + b * 0.31114694;
  186. }
  187. function blend(c, a) {
  188. return 255 + (c - 255) * a;
  189. }
  190. function drawPixel(output, pos, r, g, b) {
  191. output[pos + 0] = r;
  192. output[pos + 1] = g;
  193. output[pos + 2] = b;
  194. output[pos + 3] = 255;
  195. }
  196. function drawGrayPixel(img, i, alpha, output) {
  197. const r = img[i + 0];
  198. const g = img[i + 1];
  199. const b = img[i + 2];
  200. const val = blend(rgb2y(r, g, b), alpha * img[i + 3] / 255);
  201. drawPixel(output, i, val, val, val);
  202. }
  203. }
  204. });
  205. // node_modules/pngjs/lib/chunkstream.js
  206. var require_chunkstream = __commonJS({
  207. "node_modules/pngjs/lib/chunkstream.js"(exports2, module2) {
  208. "use strict";
  209. var util = require("util");
  210. var Stream = require("stream");
  211. var ChunkStream = module2.exports = function() {
  212. Stream.call(this);
  213. this._buffers = [];
  214. this._buffered = 0;
  215. this._reads = [];
  216. this._paused = false;
  217. this._encoding = "utf8";
  218. this.writable = true;
  219. };
  220. util.inherits(ChunkStream, Stream);
  221. ChunkStream.prototype.read = function(length, callback) {
  222. this._reads.push({
  223. length: Math.abs(length),
  224. // if length < 0 then at most this length
  225. allowLess: length < 0,
  226. func: callback
  227. });
  228. process.nextTick(
  229. (function() {
  230. this._process();
  231. if (this._paused && this._reads && this._reads.length > 0) {
  232. this._paused = false;
  233. this.emit("drain");
  234. }
  235. }).bind(this)
  236. );
  237. };
  238. ChunkStream.prototype.write = function(data, encoding) {
  239. if (!this.writable) {
  240. this.emit("error", new Error("Stream not writable"));
  241. return false;
  242. }
  243. let dataBuffer;
  244. if (Buffer.isBuffer(data)) {
  245. dataBuffer = data;
  246. } else {
  247. dataBuffer = Buffer.from(data, encoding || this._encoding);
  248. }
  249. this._buffers.push(dataBuffer);
  250. this._buffered += dataBuffer.length;
  251. this._process();
  252. if (this._reads && this._reads.length === 0) {
  253. this._paused = true;
  254. }
  255. return this.writable && !this._paused;
  256. };
  257. ChunkStream.prototype.end = function(data, encoding) {
  258. if (data) {
  259. this.write(data, encoding);
  260. }
  261. this.writable = false;
  262. if (!this._buffers) {
  263. return;
  264. }
  265. if (this._buffers.length === 0) {
  266. this._end();
  267. } else {
  268. this._buffers.push(null);
  269. this._process();
  270. }
  271. };
  272. ChunkStream.prototype.destroySoon = ChunkStream.prototype.end;
  273. ChunkStream.prototype._end = function() {
  274. if (this._reads.length > 0) {
  275. this.emit("error", new Error("Unexpected end of input"));
  276. }
  277. this.destroy();
  278. };
  279. ChunkStream.prototype.destroy = function() {
  280. if (!this._buffers) {
  281. return;
  282. }
  283. this.writable = false;
  284. this._reads = null;
  285. this._buffers = null;
  286. this.emit("close");
  287. };
  288. ChunkStream.prototype._processReadAllowingLess = function(read) {
  289. this._reads.shift();
  290. let smallerBuf = this._buffers[0];
  291. if (smallerBuf.length > read.length) {
  292. this._buffered -= read.length;
  293. this._buffers[0] = smallerBuf.slice(read.length);
  294. read.func.call(this, smallerBuf.slice(0, read.length));
  295. } else {
  296. this._buffered -= smallerBuf.length;
  297. this._buffers.shift();
  298. read.func.call(this, smallerBuf);
  299. }
  300. };
  301. ChunkStream.prototype._processRead = function(read) {
  302. this._reads.shift();
  303. let pos = 0;
  304. let count = 0;
  305. let data = Buffer.alloc(read.length);
  306. while (pos < read.length) {
  307. let buf = this._buffers[count++];
  308. let len = Math.min(buf.length, read.length - pos);
  309. buf.copy(data, pos, 0, len);
  310. pos += len;
  311. if (len !== buf.length) {
  312. this._buffers[--count] = buf.slice(len);
  313. }
  314. }
  315. if (count > 0) {
  316. this._buffers.splice(0, count);
  317. }
  318. this._buffered -= read.length;
  319. read.func.call(this, data);
  320. };
  321. ChunkStream.prototype._process = function() {
  322. try {
  323. while (this._buffered > 0 && this._reads && this._reads.length > 0) {
  324. let read = this._reads[0];
  325. if (read.allowLess) {
  326. this._processReadAllowingLess(read);
  327. } else if (this._buffered >= read.length) {
  328. this._processRead(read);
  329. } else {
  330. break;
  331. }
  332. }
  333. if (this._buffers && !this.writable) {
  334. this._end();
  335. }
  336. } catch (ex) {
  337. this.emit("error", ex);
  338. }
  339. };
  340. }
  341. });
  342. // node_modules/pngjs/lib/interlace.js
  343. var require_interlace = __commonJS({
  344. "node_modules/pngjs/lib/interlace.js"(exports2) {
  345. "use strict";
  346. var imagePasses = [
  347. {
  348. // pass 1 - 1px
  349. x: [0],
  350. y: [0]
  351. },
  352. {
  353. // pass 2 - 1px
  354. x: [4],
  355. y: [0]
  356. },
  357. {
  358. // pass 3 - 2px
  359. x: [0, 4],
  360. y: [4]
  361. },
  362. {
  363. // pass 4 - 4px
  364. x: [2, 6],
  365. y: [0, 4]
  366. },
  367. {
  368. // pass 5 - 8px
  369. x: [0, 2, 4, 6],
  370. y: [2, 6]
  371. },
  372. {
  373. // pass 6 - 16px
  374. x: [1, 3, 5, 7],
  375. y: [0, 2, 4, 6]
  376. },
  377. {
  378. // pass 7 - 32px
  379. x: [0, 1, 2, 3, 4, 5, 6, 7],
  380. y: [1, 3, 5, 7]
  381. }
  382. ];
  383. exports2.getImagePasses = function(width, height) {
  384. let images = [];
  385. let xLeftOver = width % 8;
  386. let yLeftOver = height % 8;
  387. let xRepeats = (width - xLeftOver) / 8;
  388. let yRepeats = (height - yLeftOver) / 8;
  389. for (let i = 0; i < imagePasses.length; i++) {
  390. let pass = imagePasses[i];
  391. let passWidth = xRepeats * pass.x.length;
  392. let passHeight = yRepeats * pass.y.length;
  393. for (let j = 0; j < pass.x.length; j++) {
  394. if (pass.x[j] < xLeftOver) {
  395. passWidth++;
  396. } else {
  397. break;
  398. }
  399. }
  400. for (let j = 0; j < pass.y.length; j++) {
  401. if (pass.y[j] < yLeftOver) {
  402. passHeight++;
  403. } else {
  404. break;
  405. }
  406. }
  407. if (passWidth > 0 && passHeight > 0) {
  408. images.push({ width: passWidth, height: passHeight, index: i });
  409. }
  410. }
  411. return images;
  412. };
  413. exports2.getInterlaceIterator = function(width) {
  414. return function(x, y, pass) {
  415. let outerXLeftOver = x % imagePasses[pass].x.length;
  416. let outerX = (x - outerXLeftOver) / imagePasses[pass].x.length * 8 + imagePasses[pass].x[outerXLeftOver];
  417. let outerYLeftOver = y % imagePasses[pass].y.length;
  418. let outerY = (y - outerYLeftOver) / imagePasses[pass].y.length * 8 + imagePasses[pass].y[outerYLeftOver];
  419. return outerX * 4 + outerY * width * 4;
  420. };
  421. };
  422. }
  423. });
  424. // node_modules/pngjs/lib/paeth-predictor.js
  425. var require_paeth_predictor = __commonJS({
  426. "node_modules/pngjs/lib/paeth-predictor.js"(exports2, module2) {
  427. "use strict";
  428. module2.exports = function paethPredictor(left, above, upLeft) {
  429. let paeth = left + above - upLeft;
  430. let pLeft = Math.abs(paeth - left);
  431. let pAbove = Math.abs(paeth - above);
  432. let pUpLeft = Math.abs(paeth - upLeft);
  433. if (pLeft <= pAbove && pLeft <= pUpLeft) {
  434. return left;
  435. }
  436. if (pAbove <= pUpLeft) {
  437. return above;
  438. }
  439. return upLeft;
  440. };
  441. }
  442. });
  443. // node_modules/pngjs/lib/filter-parse.js
  444. var require_filter_parse = __commonJS({
  445. "node_modules/pngjs/lib/filter-parse.js"(exports2, module2) {
  446. "use strict";
  447. var interlaceUtils = require_interlace();
  448. var paethPredictor = require_paeth_predictor();
  449. function getByteWidth(width, bpp, depth) {
  450. let byteWidth = width * bpp;
  451. if (depth !== 8) {
  452. byteWidth = Math.ceil(byteWidth / (8 / depth));
  453. }
  454. return byteWidth;
  455. }
  456. var Filter = module2.exports = function(bitmapInfo, dependencies) {
  457. let width = bitmapInfo.width;
  458. let height = bitmapInfo.height;
  459. let interlace = bitmapInfo.interlace;
  460. let bpp = bitmapInfo.bpp;
  461. let depth = bitmapInfo.depth;
  462. this.read = dependencies.read;
  463. this.write = dependencies.write;
  464. this.complete = dependencies.complete;
  465. this._imageIndex = 0;
  466. this._images = [];
  467. if (interlace) {
  468. let passes = interlaceUtils.getImagePasses(width, height);
  469. for (let i = 0; i < passes.length; i++) {
  470. this._images.push({
  471. byteWidth: getByteWidth(passes[i].width, bpp, depth),
  472. height: passes[i].height,
  473. lineIndex: 0
  474. });
  475. }
  476. } else {
  477. this._images.push({
  478. byteWidth: getByteWidth(width, bpp, depth),
  479. height,
  480. lineIndex: 0
  481. });
  482. }
  483. if (depth === 8) {
  484. this._xComparison = bpp;
  485. } else if (depth === 16) {
  486. this._xComparison = bpp * 2;
  487. } else {
  488. this._xComparison = 1;
  489. }
  490. };
  491. Filter.prototype.start = function() {
  492. this.read(
  493. this._images[this._imageIndex].byteWidth + 1,
  494. this._reverseFilterLine.bind(this)
  495. );
  496. };
  497. Filter.prototype._unFilterType1 = function(rawData, unfilteredLine, byteWidth) {
  498. let xComparison = this._xComparison;
  499. let xBiggerThan = xComparison - 1;
  500. for (let x = 0; x < byteWidth; x++) {
  501. let rawByte = rawData[1 + x];
  502. let f1Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
  503. unfilteredLine[x] = rawByte + f1Left;
  504. }
  505. };
  506. Filter.prototype._unFilterType2 = function(rawData, unfilteredLine, byteWidth) {
  507. let lastLine = this._lastLine;
  508. for (let x = 0; x < byteWidth; x++) {
  509. let rawByte = rawData[1 + x];
  510. let f2Up = lastLine ? lastLine[x] : 0;
  511. unfilteredLine[x] = rawByte + f2Up;
  512. }
  513. };
  514. Filter.prototype._unFilterType3 = function(rawData, unfilteredLine, byteWidth) {
  515. let xComparison = this._xComparison;
  516. let xBiggerThan = xComparison - 1;
  517. let lastLine = this._lastLine;
  518. for (let x = 0; x < byteWidth; x++) {
  519. let rawByte = rawData[1 + x];
  520. let f3Up = lastLine ? lastLine[x] : 0;
  521. let f3Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
  522. let f3Add = Math.floor((f3Left + f3Up) / 2);
  523. unfilteredLine[x] = rawByte + f3Add;
  524. }
  525. };
  526. Filter.prototype._unFilterType4 = function(rawData, unfilteredLine, byteWidth) {
  527. let xComparison = this._xComparison;
  528. let xBiggerThan = xComparison - 1;
  529. let lastLine = this._lastLine;
  530. for (let x = 0; x < byteWidth; x++) {
  531. let rawByte = rawData[1 + x];
  532. let f4Up = lastLine ? lastLine[x] : 0;
  533. let f4Left = x > xBiggerThan ? unfilteredLine[x - xComparison] : 0;
  534. let f4UpLeft = x > xBiggerThan && lastLine ? lastLine[x - xComparison] : 0;
  535. let f4Add = paethPredictor(f4Left, f4Up, f4UpLeft);
  536. unfilteredLine[x] = rawByte + f4Add;
  537. }
  538. };
  539. Filter.prototype._reverseFilterLine = function(rawData) {
  540. let filter = rawData[0];
  541. let unfilteredLine;
  542. let currentImage = this._images[this._imageIndex];
  543. let byteWidth = currentImage.byteWidth;
  544. if (filter === 0) {
  545. unfilteredLine = rawData.slice(1, byteWidth + 1);
  546. } else {
  547. unfilteredLine = Buffer.alloc(byteWidth);
  548. switch (filter) {
  549. case 1:
  550. this._unFilterType1(rawData, unfilteredLine, byteWidth);
  551. break;
  552. case 2:
  553. this._unFilterType2(rawData, unfilteredLine, byteWidth);
  554. break;
  555. case 3:
  556. this._unFilterType3(rawData, unfilteredLine, byteWidth);
  557. break;
  558. case 4:
  559. this._unFilterType4(rawData, unfilteredLine, byteWidth);
  560. break;
  561. default:
  562. throw new Error("Unrecognised filter type - " + filter);
  563. }
  564. }
  565. this.write(unfilteredLine);
  566. currentImage.lineIndex++;
  567. if (currentImage.lineIndex >= currentImage.height) {
  568. this._lastLine = null;
  569. this._imageIndex++;
  570. currentImage = this._images[this._imageIndex];
  571. } else {
  572. this._lastLine = unfilteredLine;
  573. }
  574. if (currentImage) {
  575. this.read(currentImage.byteWidth + 1, this._reverseFilterLine.bind(this));
  576. } else {
  577. this._lastLine = null;
  578. this.complete();
  579. }
  580. };
  581. }
  582. });
  583. // node_modules/pngjs/lib/filter-parse-async.js
  584. var require_filter_parse_async = __commonJS({
  585. "node_modules/pngjs/lib/filter-parse-async.js"(exports2, module2) {
  586. "use strict";
  587. var util = require("util");
  588. var ChunkStream = require_chunkstream();
  589. var Filter = require_filter_parse();
  590. var FilterAsync = module2.exports = function(bitmapInfo) {
  591. ChunkStream.call(this);
  592. let buffers = [];
  593. let that = this;
  594. this._filter = new Filter(bitmapInfo, {
  595. read: this.read.bind(this),
  596. write: function(buffer) {
  597. buffers.push(buffer);
  598. },
  599. complete: function() {
  600. that.emit("complete", Buffer.concat(buffers));
  601. }
  602. });
  603. this._filter.start();
  604. };
  605. util.inherits(FilterAsync, ChunkStream);
  606. }
  607. });
  608. // node_modules/pngjs/lib/constants.js
  609. var require_constants = __commonJS({
  610. "node_modules/pngjs/lib/constants.js"(exports2, module2) {
  611. "use strict";
  612. module2.exports = {
  613. PNG_SIGNATURE: [137, 80, 78, 71, 13, 10, 26, 10],
  614. TYPE_IHDR: 1229472850,
  615. TYPE_IEND: 1229278788,
  616. TYPE_IDAT: 1229209940,
  617. TYPE_PLTE: 1347179589,
  618. TYPE_tRNS: 1951551059,
  619. // eslint-disable-line camelcase
  620. TYPE_gAMA: 1732332865,
  621. // eslint-disable-line camelcase
  622. // color-type bits
  623. COLORTYPE_GRAYSCALE: 0,
  624. COLORTYPE_PALETTE: 1,
  625. COLORTYPE_COLOR: 2,
  626. COLORTYPE_ALPHA: 4,
  627. // e.g. grayscale and alpha
  628. // color-type combinations
  629. COLORTYPE_PALETTE_COLOR: 3,
  630. COLORTYPE_COLOR_ALPHA: 6,
  631. COLORTYPE_TO_BPP_MAP: {
  632. 0: 1,
  633. 2: 3,
  634. 3: 1,
  635. 4: 2,
  636. 6: 4
  637. },
  638. GAMMA_DIVISION: 1e5
  639. };
  640. }
  641. });
  642. // node_modules/pngjs/lib/crc.js
  643. var require_crc = __commonJS({
  644. "node_modules/pngjs/lib/crc.js"(exports2, module2) {
  645. "use strict";
  646. var crcTable = [];
  647. (function() {
  648. for (let i = 0; i < 256; i++) {
  649. let currentCrc = i;
  650. for (let j = 0; j < 8; j++) {
  651. if (currentCrc & 1) {
  652. currentCrc = 3988292384 ^ currentCrc >>> 1;
  653. } else {
  654. currentCrc = currentCrc >>> 1;
  655. }
  656. }
  657. crcTable[i] = currentCrc;
  658. }
  659. })();
  660. var CrcCalculator = module2.exports = function() {
  661. this._crc = -1;
  662. };
  663. CrcCalculator.prototype.write = function(data) {
  664. for (let i = 0; i < data.length; i++) {
  665. this._crc = crcTable[(this._crc ^ data[i]) & 255] ^ this._crc >>> 8;
  666. }
  667. return true;
  668. };
  669. CrcCalculator.prototype.crc32 = function() {
  670. return this._crc ^ -1;
  671. };
  672. CrcCalculator.crc32 = function(buf) {
  673. let crc = -1;
  674. for (let i = 0; i < buf.length; i++) {
  675. crc = crcTable[(crc ^ buf[i]) & 255] ^ crc >>> 8;
  676. }
  677. return crc ^ -1;
  678. };
  679. }
  680. });
  681. // node_modules/pngjs/lib/parser.js
  682. var require_parser = __commonJS({
  683. "node_modules/pngjs/lib/parser.js"(exports2, module2) {
  684. "use strict";
  685. var constants = require_constants();
  686. var CrcCalculator = require_crc();
  687. var Parser = module2.exports = function(options, dependencies) {
  688. this._options = options;
  689. options.checkCRC = options.checkCRC !== false;
  690. this._hasIHDR = false;
  691. this._hasIEND = false;
  692. this._emittedHeadersFinished = false;
  693. this._palette = [];
  694. this._colorType = 0;
  695. this._chunks = {};
  696. this._chunks[constants.TYPE_IHDR] = this._handleIHDR.bind(this);
  697. this._chunks[constants.TYPE_IEND] = this._handleIEND.bind(this);
  698. this._chunks[constants.TYPE_IDAT] = this._handleIDAT.bind(this);
  699. this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this);
  700. this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this);
  701. this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this);
  702. this.read = dependencies.read;
  703. this.error = dependencies.error;
  704. this.metadata = dependencies.metadata;
  705. this.gamma = dependencies.gamma;
  706. this.transColor = dependencies.transColor;
  707. this.palette = dependencies.palette;
  708. this.parsed = dependencies.parsed;
  709. this.inflateData = dependencies.inflateData;
  710. this.finished = dependencies.finished;
  711. this.simpleTransparency = dependencies.simpleTransparency;
  712. this.headersFinished = dependencies.headersFinished || function() {
  713. };
  714. };
  715. Parser.prototype.start = function() {
  716. this.read(constants.PNG_SIGNATURE.length, this._parseSignature.bind(this));
  717. };
  718. Parser.prototype._parseSignature = function(data) {
  719. let signature = constants.PNG_SIGNATURE;
  720. for (let i = 0; i < signature.length; i++) {
  721. if (data[i] !== signature[i]) {
  722. this.error(new Error("Invalid file signature"));
  723. return;
  724. }
  725. }
  726. this.read(8, this._parseChunkBegin.bind(this));
  727. };
  728. Parser.prototype._parseChunkBegin = function(data) {
  729. let length = data.readUInt32BE(0);
  730. let type = data.readUInt32BE(4);
  731. let name = "";
  732. for (let i = 4; i < 8; i++) {
  733. name += String.fromCharCode(data[i]);
  734. }
  735. let ancillary = Boolean(data[4] & 32);
  736. if (!this._hasIHDR && type !== constants.TYPE_IHDR) {
  737. this.error(new Error("Expected IHDR on beggining"));
  738. return;
  739. }
  740. this._crc = new CrcCalculator();
  741. this._crc.write(Buffer.from(name));
  742. if (this._chunks[type]) {
  743. return this._chunks[type](length);
  744. }
  745. if (!ancillary) {
  746. this.error(new Error("Unsupported critical chunk type " + name));
  747. return;
  748. }
  749. this.read(length + 4, this._skipChunk.bind(this));
  750. };
  751. Parser.prototype._skipChunk = function() {
  752. this.read(8, this._parseChunkBegin.bind(this));
  753. };
  754. Parser.prototype._handleChunkEnd = function() {
  755. this.read(4, this._parseChunkEnd.bind(this));
  756. };
  757. Parser.prototype._parseChunkEnd = function(data) {
  758. let fileCrc = data.readInt32BE(0);
  759. let calcCrc = this._crc.crc32();
  760. if (this._options.checkCRC && calcCrc !== fileCrc) {
  761. this.error(new Error("Crc error - " + fileCrc + " - " + calcCrc));
  762. return;
  763. }
  764. if (!this._hasIEND) {
  765. this.read(8, this._parseChunkBegin.bind(this));
  766. }
  767. };
  768. Parser.prototype._handleIHDR = function(length) {
  769. this.read(length, this._parseIHDR.bind(this));
  770. };
  771. Parser.prototype._parseIHDR = function(data) {
  772. this._crc.write(data);
  773. let width = data.readUInt32BE(0);
  774. let height = data.readUInt32BE(4);
  775. let depth = data[8];
  776. let colorType = data[9];
  777. let compr = data[10];
  778. let filter = data[11];
  779. let interlace = data[12];
  780. if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) {
  781. this.error(new Error("Unsupported bit depth " + depth));
  782. return;
  783. }
  784. if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) {
  785. this.error(new Error("Unsupported color type"));
  786. return;
  787. }
  788. if (compr !== 0) {
  789. this.error(new Error("Unsupported compression method"));
  790. return;
  791. }
  792. if (filter !== 0) {
  793. this.error(new Error("Unsupported filter method"));
  794. return;
  795. }
  796. if (interlace !== 0 && interlace !== 1) {
  797. this.error(new Error("Unsupported interlace method"));
  798. return;
  799. }
  800. this._colorType = colorType;
  801. let bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType];
  802. this._hasIHDR = true;
  803. this.metadata({
  804. width,
  805. height,
  806. depth,
  807. interlace: Boolean(interlace),
  808. palette: Boolean(colorType & constants.COLORTYPE_PALETTE),
  809. color: Boolean(colorType & constants.COLORTYPE_COLOR),
  810. alpha: Boolean(colorType & constants.COLORTYPE_ALPHA),
  811. bpp,
  812. colorType
  813. });
  814. this._handleChunkEnd();
  815. };
  816. Parser.prototype._handlePLTE = function(length) {
  817. this.read(length, this._parsePLTE.bind(this));
  818. };
  819. Parser.prototype._parsePLTE = function(data) {
  820. this._crc.write(data);
  821. let entries = Math.floor(data.length / 3);
  822. for (let i = 0; i < entries; i++) {
  823. this._palette.push([data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 255]);
  824. }
  825. this.palette(this._palette);
  826. this._handleChunkEnd();
  827. };
  828. Parser.prototype._handleTRNS = function(length) {
  829. this.simpleTransparency();
  830. this.read(length, this._parseTRNS.bind(this));
  831. };
  832. Parser.prototype._parseTRNS = function(data) {
  833. this._crc.write(data);
  834. if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) {
  835. if (this._palette.length === 0) {
  836. this.error(new Error("Transparency chunk must be after palette"));
  837. return;
  838. }
  839. if (data.length > this._palette.length) {
  840. this.error(new Error("More transparent colors than palette size"));
  841. return;
  842. }
  843. for (let i = 0; i < data.length; i++) {
  844. this._palette[i][3] = data[i];
  845. }
  846. this.palette(this._palette);
  847. }
  848. if (this._colorType === constants.COLORTYPE_GRAYSCALE) {
  849. this.transColor([data.readUInt16BE(0)]);
  850. }
  851. if (this._colorType === constants.COLORTYPE_COLOR) {
  852. this.transColor([
  853. data.readUInt16BE(0),
  854. data.readUInt16BE(2),
  855. data.readUInt16BE(4)
  856. ]);
  857. }
  858. this._handleChunkEnd();
  859. };
  860. Parser.prototype._handleGAMA = function(length) {
  861. this.read(length, this._parseGAMA.bind(this));
  862. };
  863. Parser.prototype._parseGAMA = function(data) {
  864. this._crc.write(data);
  865. this.gamma(data.readUInt32BE(0) / constants.GAMMA_DIVISION);
  866. this._handleChunkEnd();
  867. };
  868. Parser.prototype._handleIDAT = function(length) {
  869. if (!this._emittedHeadersFinished) {
  870. this._emittedHeadersFinished = true;
  871. this.headersFinished();
  872. }
  873. this.read(-length, this._parseIDAT.bind(this, length));
  874. };
  875. Parser.prototype._parseIDAT = function(length, data) {
  876. this._crc.write(data);
  877. if (this._colorType === constants.COLORTYPE_PALETTE_COLOR && this._palette.length === 0) {
  878. throw new Error("Expected palette not found");
  879. }
  880. this.inflateData(data);
  881. let leftOverLength = length - data.length;
  882. if (leftOverLength > 0) {
  883. this._handleIDAT(leftOverLength);
  884. } else {
  885. this._handleChunkEnd();
  886. }
  887. };
  888. Parser.prototype._handleIEND = function(length) {
  889. this.read(length, this._parseIEND.bind(this));
  890. };
  891. Parser.prototype._parseIEND = function(data) {
  892. this._crc.write(data);
  893. this._hasIEND = true;
  894. this._handleChunkEnd();
  895. if (this.finished) {
  896. this.finished();
  897. }
  898. };
  899. }
  900. });
  901. // node_modules/pngjs/lib/bitmapper.js
  902. var require_bitmapper = __commonJS({
  903. "node_modules/pngjs/lib/bitmapper.js"(exports2) {
  904. "use strict";
  905. var interlaceUtils = require_interlace();
  906. var pixelBppMapper = [
  907. // 0 - dummy entry
  908. function() {
  909. },
  910. // 1 - L
  911. // 0: 0, 1: 0, 2: 0, 3: 0xff
  912. function(pxData, data, pxPos, rawPos) {
  913. if (rawPos === data.length) {
  914. throw new Error("Ran out of data");
  915. }
  916. let pixel = data[rawPos];
  917. pxData[pxPos] = pixel;
  918. pxData[pxPos + 1] = pixel;
  919. pxData[pxPos + 2] = pixel;
  920. pxData[pxPos + 3] = 255;
  921. },
  922. // 2 - LA
  923. // 0: 0, 1: 0, 2: 0, 3: 1
  924. function(pxData, data, pxPos, rawPos) {
  925. if (rawPos + 1 >= data.length) {
  926. throw new Error("Ran out of data");
  927. }
  928. let pixel = data[rawPos];
  929. pxData[pxPos] = pixel;
  930. pxData[pxPos + 1] = pixel;
  931. pxData[pxPos + 2] = pixel;
  932. pxData[pxPos + 3] = data[rawPos + 1];
  933. },
  934. // 3 - RGB
  935. // 0: 0, 1: 1, 2: 2, 3: 0xff
  936. function(pxData, data, pxPos, rawPos) {
  937. if (rawPos + 2 >= data.length) {
  938. throw new Error("Ran out of data");
  939. }
  940. pxData[pxPos] = data[rawPos];
  941. pxData[pxPos + 1] = data[rawPos + 1];
  942. pxData[pxPos + 2] = data[rawPos + 2];
  943. pxData[pxPos + 3] = 255;
  944. },
  945. // 4 - RGBA
  946. // 0: 0, 1: 1, 2: 2, 3: 3
  947. function(pxData, data, pxPos, rawPos) {
  948. if (rawPos + 3 >= data.length) {
  949. throw new Error("Ran out of data");
  950. }
  951. pxData[pxPos] = data[rawPos];
  952. pxData[pxPos + 1] = data[rawPos + 1];
  953. pxData[pxPos + 2] = data[rawPos + 2];
  954. pxData[pxPos + 3] = data[rawPos + 3];
  955. }
  956. ];
  957. var pixelBppCustomMapper = [
  958. // 0 - dummy entry
  959. function() {
  960. },
  961. // 1 - L
  962. // 0: 0, 1: 0, 2: 0, 3: 0xff
  963. function(pxData, pixelData, pxPos, maxBit) {
  964. let pixel = pixelData[0];
  965. pxData[pxPos] = pixel;
  966. pxData[pxPos + 1] = pixel;
  967. pxData[pxPos + 2] = pixel;
  968. pxData[pxPos + 3] = maxBit;
  969. },
  970. // 2 - LA
  971. // 0: 0, 1: 0, 2: 0, 3: 1
  972. function(pxData, pixelData, pxPos) {
  973. let pixel = pixelData[0];
  974. pxData[pxPos] = pixel;
  975. pxData[pxPos + 1] = pixel;
  976. pxData[pxPos + 2] = pixel;
  977. pxData[pxPos + 3] = pixelData[1];
  978. },
  979. // 3 - RGB
  980. // 0: 0, 1: 1, 2: 2, 3: 0xff
  981. function(pxData, pixelData, pxPos, maxBit) {
  982. pxData[pxPos] = pixelData[0];
  983. pxData[pxPos + 1] = pixelData[1];
  984. pxData[pxPos + 2] = pixelData[2];
  985. pxData[pxPos + 3] = maxBit;
  986. },
  987. // 4 - RGBA
  988. // 0: 0, 1: 1, 2: 2, 3: 3
  989. function(pxData, pixelData, pxPos) {
  990. pxData[pxPos] = pixelData[0];
  991. pxData[pxPos + 1] = pixelData[1];
  992. pxData[pxPos + 2] = pixelData[2];
  993. pxData[pxPos + 3] = pixelData[3];
  994. }
  995. ];
  996. function bitRetriever(data, depth) {
  997. let leftOver = [];
  998. let i = 0;
  999. function split() {
  1000. if (i === data.length) {
  1001. throw new Error("Ran out of data");
  1002. }
  1003. let byte = data[i];
  1004. i++;
  1005. let byte8, byte7, byte6, byte5, byte4, byte3, byte2, byte1;
  1006. switch (depth) {
  1007. default:
  1008. throw new Error("unrecognised depth");
  1009. case 16:
  1010. byte2 = data[i];
  1011. i++;
  1012. leftOver.push((byte << 8) + byte2);
  1013. break;
  1014. case 4:
  1015. byte2 = byte & 15;
  1016. byte1 = byte >> 4;
  1017. leftOver.push(byte1, byte2);
  1018. break;
  1019. case 2:
  1020. byte4 = byte & 3;
  1021. byte3 = byte >> 2 & 3;
  1022. byte2 = byte >> 4 & 3;
  1023. byte1 = byte >> 6 & 3;
  1024. leftOver.push(byte1, byte2, byte3, byte4);
  1025. break;
  1026. case 1:
  1027. byte8 = byte & 1;
  1028. byte7 = byte >> 1 & 1;
  1029. byte6 = byte >> 2 & 1;
  1030. byte5 = byte >> 3 & 1;
  1031. byte4 = byte >> 4 & 1;
  1032. byte3 = byte >> 5 & 1;
  1033. byte2 = byte >> 6 & 1;
  1034. byte1 = byte >> 7 & 1;
  1035. leftOver.push(byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8);
  1036. break;
  1037. }
  1038. }
  1039. return {
  1040. get: function(count) {
  1041. while (leftOver.length < count) {
  1042. split();
  1043. }
  1044. let returner = leftOver.slice(0, count);
  1045. leftOver = leftOver.slice(count);
  1046. return returner;
  1047. },
  1048. resetAfterLine: function() {
  1049. leftOver.length = 0;
  1050. },
  1051. end: function() {
  1052. if (i !== data.length) {
  1053. throw new Error("extra data found");
  1054. }
  1055. }
  1056. };
  1057. }
  1058. function mapImage8Bit(image, pxData, getPxPos, bpp, data, rawPos) {
  1059. let imageWidth = image.width;
  1060. let imageHeight = image.height;
  1061. let imagePass = image.index;
  1062. for (let y = 0; y < imageHeight; y++) {
  1063. for (let x = 0; x < imageWidth; x++) {
  1064. let pxPos = getPxPos(x, y, imagePass);
  1065. pixelBppMapper[bpp](pxData, data, pxPos, rawPos);
  1066. rawPos += bpp;
  1067. }
  1068. }
  1069. return rawPos;
  1070. }
  1071. function mapImageCustomBit(image, pxData, getPxPos, bpp, bits, maxBit) {
  1072. let imageWidth = image.width;
  1073. let imageHeight = image.height;
  1074. let imagePass = image.index;
  1075. for (let y = 0; y < imageHeight; y++) {
  1076. for (let x = 0; x < imageWidth; x++) {
  1077. let pixelData = bits.get(bpp);
  1078. let pxPos = getPxPos(x, y, imagePass);
  1079. pixelBppCustomMapper[bpp](pxData, pixelData, pxPos, maxBit);
  1080. }
  1081. bits.resetAfterLine();
  1082. }
  1083. }
  1084. exports2.dataToBitMap = function(data, bitmapInfo) {
  1085. let width = bitmapInfo.width;
  1086. let height = bitmapInfo.height;
  1087. let depth = bitmapInfo.depth;
  1088. let bpp = bitmapInfo.bpp;
  1089. let interlace = bitmapInfo.interlace;
  1090. let bits;
  1091. if (depth !== 8) {
  1092. bits = bitRetriever(data, depth);
  1093. }
  1094. let pxData;
  1095. if (depth <= 8) {
  1096. pxData = Buffer.alloc(width * height * 4);
  1097. } else {
  1098. pxData = new Uint16Array(width * height * 4);
  1099. }
  1100. let maxBit = Math.pow(2, depth) - 1;
  1101. let rawPos = 0;
  1102. let images;
  1103. let getPxPos;
  1104. if (interlace) {
  1105. images = interlaceUtils.getImagePasses(width, height);
  1106. getPxPos = interlaceUtils.getInterlaceIterator(width, height);
  1107. } else {
  1108. let nonInterlacedPxPos = 0;
  1109. getPxPos = function() {
  1110. let returner = nonInterlacedPxPos;
  1111. nonInterlacedPxPos += 4;
  1112. return returner;
  1113. };
  1114. images = [{ width, height }];
  1115. }
  1116. for (let imageIndex = 0; imageIndex < images.length; imageIndex++) {
  1117. if (depth === 8) {
  1118. rawPos = mapImage8Bit(
  1119. images[imageIndex],
  1120. pxData,
  1121. getPxPos,
  1122. bpp,
  1123. data,
  1124. rawPos
  1125. );
  1126. } else {
  1127. mapImageCustomBit(
  1128. images[imageIndex],
  1129. pxData,
  1130. getPxPos,
  1131. bpp,
  1132. bits,
  1133. maxBit
  1134. );
  1135. }
  1136. }
  1137. if (depth === 8) {
  1138. if (rawPos !== data.length) {
  1139. throw new Error("extra data found");
  1140. }
  1141. } else {
  1142. bits.end();
  1143. }
  1144. return pxData;
  1145. };
  1146. }
  1147. });
  1148. // node_modules/pngjs/lib/format-normaliser.js
  1149. var require_format_normaliser = __commonJS({
  1150. "node_modules/pngjs/lib/format-normaliser.js"(exports2, module2) {
  1151. "use strict";
  1152. function dePalette(indata, outdata, width, height, palette) {
  1153. let pxPos = 0;
  1154. for (let y = 0; y < height; y++) {
  1155. for (let x = 0; x < width; x++) {
  1156. let color = palette[indata[pxPos]];
  1157. if (!color) {
  1158. throw new Error("index " + indata[pxPos] + " not in palette");
  1159. }
  1160. for (let i = 0; i < 4; i++) {
  1161. outdata[pxPos + i] = color[i];
  1162. }
  1163. pxPos += 4;
  1164. }
  1165. }
  1166. }
  1167. function replaceTransparentColor(indata, outdata, width, height, transColor) {
  1168. let pxPos = 0;
  1169. for (let y = 0; y < height; y++) {
  1170. for (let x = 0; x < width; x++) {
  1171. let makeTrans = false;
  1172. if (transColor.length === 1) {
  1173. if (transColor[0] === indata[pxPos]) {
  1174. makeTrans = true;
  1175. }
  1176. } else if (transColor[0] === indata[pxPos] && transColor[1] === indata[pxPos + 1] && transColor[2] === indata[pxPos + 2]) {
  1177. makeTrans = true;
  1178. }
  1179. if (makeTrans) {
  1180. for (let i = 0; i < 4; i++) {
  1181. outdata[pxPos + i] = 0;
  1182. }
  1183. }
  1184. pxPos += 4;
  1185. }
  1186. }
  1187. }
  1188. function scaleDepth(indata, outdata, width, height, depth) {
  1189. let maxOutSample = 255;
  1190. let maxInSample = Math.pow(2, depth) - 1;
  1191. let pxPos = 0;
  1192. for (let y = 0; y < height; y++) {
  1193. for (let x = 0; x < width; x++) {
  1194. for (let i = 0; i < 4; i++) {
  1195. outdata[pxPos + i] = Math.floor(
  1196. indata[pxPos + i] * maxOutSample / maxInSample + 0.5
  1197. );
  1198. }
  1199. pxPos += 4;
  1200. }
  1201. }
  1202. }
  1203. module2.exports = function(indata, imageData, skipRescale = false) {
  1204. let depth = imageData.depth;
  1205. let width = imageData.width;
  1206. let height = imageData.height;
  1207. let colorType = imageData.colorType;
  1208. let transColor = imageData.transColor;
  1209. let palette = imageData.palette;
  1210. let outdata = indata;
  1211. if (colorType === 3) {
  1212. dePalette(indata, outdata, width, height, palette);
  1213. } else {
  1214. if (transColor) {
  1215. replaceTransparentColor(indata, outdata, width, height, transColor);
  1216. }
  1217. if (depth !== 8 && !skipRescale) {
  1218. if (depth === 16) {
  1219. outdata = Buffer.alloc(width * height * 4);
  1220. }
  1221. scaleDepth(indata, outdata, width, height, depth);
  1222. }
  1223. }
  1224. return outdata;
  1225. };
  1226. }
  1227. });
  1228. // node_modules/pngjs/lib/parser-async.js
  1229. var require_parser_async = __commonJS({
  1230. "node_modules/pngjs/lib/parser-async.js"(exports2, module2) {
  1231. "use strict";
  1232. var util = require("util");
  1233. var zlib = require("zlib");
  1234. var ChunkStream = require_chunkstream();
  1235. var FilterAsync = require_filter_parse_async();
  1236. var Parser = require_parser();
  1237. var bitmapper = require_bitmapper();
  1238. var formatNormaliser = require_format_normaliser();
  1239. var ParserAsync = module2.exports = function(options) {
  1240. ChunkStream.call(this);
  1241. this._parser = new Parser(options, {
  1242. read: this.read.bind(this),
  1243. error: this._handleError.bind(this),
  1244. metadata: this._handleMetaData.bind(this),
  1245. gamma: this.emit.bind(this, "gamma"),
  1246. palette: this._handlePalette.bind(this),
  1247. transColor: this._handleTransColor.bind(this),
  1248. finished: this._finished.bind(this),
  1249. inflateData: this._inflateData.bind(this),
  1250. simpleTransparency: this._simpleTransparency.bind(this),
  1251. headersFinished: this._headersFinished.bind(this)
  1252. });
  1253. this._options = options;
  1254. this.writable = true;
  1255. this._parser.start();
  1256. };
  1257. util.inherits(ParserAsync, ChunkStream);
  1258. ParserAsync.prototype._handleError = function(err) {
  1259. this.emit("error", err);
  1260. this.writable = false;
  1261. this.destroy();
  1262. if (this._inflate && this._inflate.destroy) {
  1263. this._inflate.destroy();
  1264. }
  1265. if (this._filter) {
  1266. this._filter.destroy();
  1267. this._filter.on("error", function() {
  1268. });
  1269. }
  1270. this.errord = true;
  1271. };
  1272. ParserAsync.prototype._inflateData = function(data) {
  1273. if (!this._inflate) {
  1274. if (this._bitmapInfo.interlace) {
  1275. this._inflate = zlib.createInflate();
  1276. this._inflate.on("error", this.emit.bind(this, "error"));
  1277. this._filter.on("complete", this._complete.bind(this));
  1278. this._inflate.pipe(this._filter);
  1279. } else {
  1280. let rowSize = (this._bitmapInfo.width * this._bitmapInfo.bpp * this._bitmapInfo.depth + 7 >> 3) + 1;
  1281. let imageSize = rowSize * this._bitmapInfo.height;
  1282. let chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK);
  1283. this._inflate = zlib.createInflate({ chunkSize });
  1284. let leftToInflate = imageSize;
  1285. let emitError = this.emit.bind(this, "error");
  1286. this._inflate.on("error", function(err) {
  1287. if (!leftToInflate) {
  1288. return;
  1289. }
  1290. emitError(err);
  1291. });
  1292. this._filter.on("complete", this._complete.bind(this));
  1293. let filterWrite = this._filter.write.bind(this._filter);
  1294. this._inflate.on("data", function(chunk) {
  1295. if (!leftToInflate) {
  1296. return;
  1297. }
  1298. if (chunk.length > leftToInflate) {
  1299. chunk = chunk.slice(0, leftToInflate);
  1300. }
  1301. leftToInflate -= chunk.length;
  1302. filterWrite(chunk);
  1303. });
  1304. this._inflate.on("end", this._filter.end.bind(this._filter));
  1305. }
  1306. }
  1307. this._inflate.write(data);
  1308. };
  1309. ParserAsync.prototype._handleMetaData = function(metaData) {
  1310. this._metaData = metaData;
  1311. this._bitmapInfo = Object.create(metaData);
  1312. this._filter = new FilterAsync(this._bitmapInfo);
  1313. };
  1314. ParserAsync.prototype._handleTransColor = function(transColor) {
  1315. this._bitmapInfo.transColor = transColor;
  1316. };
  1317. ParserAsync.prototype._handlePalette = function(palette) {
  1318. this._bitmapInfo.palette = palette;
  1319. };
  1320. ParserAsync.prototype._simpleTransparency = function() {
  1321. this._metaData.alpha = true;
  1322. };
  1323. ParserAsync.prototype._headersFinished = function() {
  1324. this.emit("metadata", this._metaData);
  1325. };
  1326. ParserAsync.prototype._finished = function() {
  1327. if (this.errord) {
  1328. return;
  1329. }
  1330. if (!this._inflate) {
  1331. this.emit("error", "No Inflate block");
  1332. } else {
  1333. this._inflate.end();
  1334. }
  1335. };
  1336. ParserAsync.prototype._complete = function(filteredData) {
  1337. if (this.errord) {
  1338. return;
  1339. }
  1340. let normalisedBitmapData;
  1341. try {
  1342. let bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);
  1343. normalisedBitmapData = formatNormaliser(
  1344. bitmapData,
  1345. this._bitmapInfo,
  1346. this._options.skipRescale
  1347. );
  1348. bitmapData = null;
  1349. } catch (ex) {
  1350. this._handleError(ex);
  1351. return;
  1352. }
  1353. this.emit("parsed", normalisedBitmapData);
  1354. };
  1355. }
  1356. });
  1357. // node_modules/pngjs/lib/bitpacker.js
  1358. var require_bitpacker = __commonJS({
  1359. "node_modules/pngjs/lib/bitpacker.js"(exports2, module2) {
  1360. "use strict";
  1361. var constants = require_constants();
  1362. module2.exports = function(dataIn, width, height, options) {
  1363. let outHasAlpha = [constants.COLORTYPE_COLOR_ALPHA, constants.COLORTYPE_ALPHA].indexOf(
  1364. options.colorType
  1365. ) !== -1;
  1366. if (options.colorType === options.inputColorType) {
  1367. let bigEndian = function() {
  1368. let buffer = new ArrayBuffer(2);
  1369. new DataView(buffer).setInt16(
  1370. 0,
  1371. 256,
  1372. true
  1373. /* littleEndian */
  1374. );
  1375. return new Int16Array(buffer)[0] !== 256;
  1376. }();
  1377. if (options.bitDepth === 8 || options.bitDepth === 16 && bigEndian) {
  1378. return dataIn;
  1379. }
  1380. }
  1381. let data = options.bitDepth !== 16 ? dataIn : new Uint16Array(dataIn.buffer);
  1382. let maxValue = 255;
  1383. let inBpp = constants.COLORTYPE_TO_BPP_MAP[options.inputColorType];
  1384. if (inBpp === 4 && !options.inputHasAlpha) {
  1385. inBpp = 3;
  1386. }
  1387. let outBpp = constants.COLORTYPE_TO_BPP_MAP[options.colorType];
  1388. if (options.bitDepth === 16) {
  1389. maxValue = 65535;
  1390. outBpp *= 2;
  1391. }
  1392. let outData = Buffer.alloc(width * height * outBpp);
  1393. let inIndex = 0;
  1394. let outIndex = 0;
  1395. let bgColor = options.bgColor || {};
  1396. if (bgColor.red === void 0) {
  1397. bgColor.red = maxValue;
  1398. }
  1399. if (bgColor.green === void 0) {
  1400. bgColor.green = maxValue;
  1401. }
  1402. if (bgColor.blue === void 0) {
  1403. bgColor.blue = maxValue;
  1404. }
  1405. function getRGBA() {
  1406. let red;
  1407. let green;
  1408. let blue;
  1409. let alpha = maxValue;
  1410. switch (options.inputColorType) {
  1411. case constants.COLORTYPE_COLOR_ALPHA:
  1412. alpha = data[inIndex + 3];
  1413. red = data[inIndex];
  1414. green = data[inIndex + 1];
  1415. blue = data[inIndex + 2];
  1416. break;
  1417. case constants.COLORTYPE_COLOR:
  1418. red = data[inIndex];
  1419. green = data[inIndex + 1];
  1420. blue = data[inIndex + 2];
  1421. break;
  1422. case constants.COLORTYPE_ALPHA:
  1423. alpha = data[inIndex + 1];
  1424. red = data[inIndex];
  1425. green = red;
  1426. blue = red;
  1427. break;
  1428. case constants.COLORTYPE_GRAYSCALE:
  1429. red = data[inIndex];
  1430. green = red;
  1431. blue = red;
  1432. break;
  1433. default:
  1434. throw new Error(
  1435. "input color type:" + options.inputColorType + " is not supported at present"
  1436. );
  1437. }
  1438. if (options.inputHasAlpha) {
  1439. if (!outHasAlpha) {
  1440. alpha /= maxValue;
  1441. red = Math.min(
  1442. Math.max(Math.round((1 - alpha) * bgColor.red + alpha * red), 0),
  1443. maxValue
  1444. );
  1445. green = Math.min(
  1446. Math.max(Math.round((1 - alpha) * bgColor.green + alpha * green), 0),
  1447. maxValue
  1448. );
  1449. blue = Math.min(
  1450. Math.max(Math.round((1 - alpha) * bgColor.blue + alpha * blue), 0),
  1451. maxValue
  1452. );
  1453. }
  1454. }
  1455. return { red, green, blue, alpha };
  1456. }
  1457. for (let y = 0; y < height; y++) {
  1458. for (let x = 0; x < width; x++) {
  1459. let rgba = getRGBA(data, inIndex);
  1460. switch (options.colorType) {
  1461. case constants.COLORTYPE_COLOR_ALPHA:
  1462. case constants.COLORTYPE_COLOR:
  1463. if (options.bitDepth === 8) {
  1464. outData[outIndex] = rgba.red;
  1465. outData[outIndex + 1] = rgba.green;
  1466. outData[outIndex + 2] = rgba.blue;
  1467. if (outHasAlpha) {
  1468. outData[outIndex + 3] = rgba.alpha;
  1469. }
  1470. } else {
  1471. outData.writeUInt16BE(rgba.red, outIndex);
  1472. outData.writeUInt16BE(rgba.green, outIndex + 2);
  1473. outData.writeUInt16BE(rgba.blue, outIndex + 4);
  1474. if (outHasAlpha) {
  1475. outData.writeUInt16BE(rgba.alpha, outIndex + 6);
  1476. }
  1477. }
  1478. break;
  1479. case constants.COLORTYPE_ALPHA:
  1480. case constants.COLORTYPE_GRAYSCALE: {
  1481. let grayscale = (rgba.red + rgba.green + rgba.blue) / 3;
  1482. if (options.bitDepth === 8) {
  1483. outData[outIndex] = grayscale;
  1484. if (outHasAlpha) {
  1485. outData[outIndex + 1] = rgba.alpha;
  1486. }
  1487. } else {
  1488. outData.writeUInt16BE(grayscale, outIndex);
  1489. if (outHasAlpha) {
  1490. outData.writeUInt16BE(rgba.alpha, outIndex + 2);
  1491. }
  1492. }
  1493. break;
  1494. }
  1495. default:
  1496. throw new Error("unrecognised color Type " + options.colorType);
  1497. }
  1498. inIndex += inBpp;
  1499. outIndex += outBpp;
  1500. }
  1501. }
  1502. return outData;
  1503. };
  1504. }
  1505. });
  1506. // node_modules/pngjs/lib/filter-pack.js
  1507. var require_filter_pack = __commonJS({
  1508. "node_modules/pngjs/lib/filter-pack.js"(exports2, module2) {
  1509. "use strict";
  1510. var paethPredictor = require_paeth_predictor();
  1511. function filterNone(pxData, pxPos, byteWidth, rawData, rawPos) {
  1512. for (let x = 0; x < byteWidth; x++) {
  1513. rawData[rawPos + x] = pxData[pxPos + x];
  1514. }
  1515. }
  1516. function filterSumNone(pxData, pxPos, byteWidth) {
  1517. let sum = 0;
  1518. let length = pxPos + byteWidth;
  1519. for (let i = pxPos; i < length; i++) {
  1520. sum += Math.abs(pxData[i]);
  1521. }
  1522. return sum;
  1523. }
  1524. function filterSub(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
  1525. for (let x = 0; x < byteWidth; x++) {
  1526. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1527. let val = pxData[pxPos + x] - left;
  1528. rawData[rawPos + x] = val;
  1529. }
  1530. }
  1531. function filterSumSub(pxData, pxPos, byteWidth, bpp) {
  1532. let sum = 0;
  1533. for (let x = 0; x < byteWidth; x++) {
  1534. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1535. let val = pxData[pxPos + x] - left;
  1536. sum += Math.abs(val);
  1537. }
  1538. return sum;
  1539. }
  1540. function filterUp(pxData, pxPos, byteWidth, rawData, rawPos) {
  1541. for (let x = 0; x < byteWidth; x++) {
  1542. let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
  1543. let val = pxData[pxPos + x] - up;
  1544. rawData[rawPos + x] = val;
  1545. }
  1546. }
  1547. function filterSumUp(pxData, pxPos, byteWidth) {
  1548. let sum = 0;
  1549. let length = pxPos + byteWidth;
  1550. for (let x = pxPos; x < length; x++) {
  1551. let up = pxPos > 0 ? pxData[x - byteWidth] : 0;
  1552. let val = pxData[x] - up;
  1553. sum += Math.abs(val);
  1554. }
  1555. return sum;
  1556. }
  1557. function filterAvg(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
  1558. for (let x = 0; x < byteWidth; x++) {
  1559. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1560. let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
  1561. let val = pxData[pxPos + x] - (left + up >> 1);
  1562. rawData[rawPos + x] = val;
  1563. }
  1564. }
  1565. function filterSumAvg(pxData, pxPos, byteWidth, bpp) {
  1566. let sum = 0;
  1567. for (let x = 0; x < byteWidth; x++) {
  1568. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1569. let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
  1570. let val = pxData[pxPos + x] - (left + up >> 1);
  1571. sum += Math.abs(val);
  1572. }
  1573. return sum;
  1574. }
  1575. function filterPaeth(pxData, pxPos, byteWidth, rawData, rawPos, bpp) {
  1576. for (let x = 0; x < byteWidth; x++) {
  1577. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1578. let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
  1579. let upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0;
  1580. let val = pxData[pxPos + x] - paethPredictor(left, up, upleft);
  1581. rawData[rawPos + x] = val;
  1582. }
  1583. }
  1584. function filterSumPaeth(pxData, pxPos, byteWidth, bpp) {
  1585. let sum = 0;
  1586. for (let x = 0; x < byteWidth; x++) {
  1587. let left = x >= bpp ? pxData[pxPos + x - bpp] : 0;
  1588. let up = pxPos > 0 ? pxData[pxPos + x - byteWidth] : 0;
  1589. let upleft = pxPos > 0 && x >= bpp ? pxData[pxPos + x - (byteWidth + bpp)] : 0;
  1590. let val = pxData[pxPos + x] - paethPredictor(left, up, upleft);
  1591. sum += Math.abs(val);
  1592. }
  1593. return sum;
  1594. }
  1595. var filters = {
  1596. 0: filterNone,
  1597. 1: filterSub,
  1598. 2: filterUp,
  1599. 3: filterAvg,
  1600. 4: filterPaeth
  1601. };
  1602. var filterSums = {
  1603. 0: filterSumNone,
  1604. 1: filterSumSub,
  1605. 2: filterSumUp,
  1606. 3: filterSumAvg,
  1607. 4: filterSumPaeth
  1608. };
  1609. module2.exports = function(pxData, width, height, options, bpp) {
  1610. let filterTypes;
  1611. if (!("filterType" in options) || options.filterType === -1) {
  1612. filterTypes = [0, 1, 2, 3, 4];
  1613. } else if (typeof options.filterType === "number") {
  1614. filterTypes = [options.filterType];
  1615. } else {
  1616. throw new Error("unrecognised filter types");
  1617. }
  1618. if (options.bitDepth === 16) {
  1619. bpp *= 2;
  1620. }
  1621. let byteWidth = width * bpp;
  1622. let rawPos = 0;
  1623. let pxPos = 0;
  1624. let rawData = Buffer.alloc((byteWidth + 1) * height);
  1625. let sel = filterTypes[0];
  1626. for (let y = 0; y < height; y++) {
  1627. if (filterTypes.length > 1) {
  1628. let min = Infinity;
  1629. for (let i = 0; i < filterTypes.length; i++) {
  1630. let sum = filterSums[filterTypes[i]](pxData, pxPos, byteWidth, bpp);
  1631. if (sum < min) {
  1632. sel = filterTypes[i];
  1633. min = sum;
  1634. }
  1635. }
  1636. }
  1637. rawData[rawPos] = sel;
  1638. rawPos++;
  1639. filters[sel](pxData, pxPos, byteWidth, rawData, rawPos, bpp);
  1640. rawPos += byteWidth;
  1641. pxPos += byteWidth;
  1642. }
  1643. return rawData;
  1644. };
  1645. }
  1646. });
  1647. // node_modules/pngjs/lib/packer.js
  1648. var require_packer = __commonJS({
  1649. "node_modules/pngjs/lib/packer.js"(exports2, module2) {
  1650. "use strict";
  1651. var constants = require_constants();
  1652. var CrcStream = require_crc();
  1653. var bitPacker = require_bitpacker();
  1654. var filter = require_filter_pack();
  1655. var zlib = require("zlib");
  1656. var Packer = module2.exports = function(options) {
  1657. this._options = options;
  1658. options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
  1659. options.deflateLevel = options.deflateLevel != null ? options.deflateLevel : 9;
  1660. options.deflateStrategy = options.deflateStrategy != null ? options.deflateStrategy : 3;
  1661. options.inputHasAlpha = options.inputHasAlpha != null ? options.inputHasAlpha : true;
  1662. options.deflateFactory = options.deflateFactory || zlib.createDeflate;
  1663. options.bitDepth = options.bitDepth || 8;
  1664. options.colorType = typeof options.colorType === "number" ? options.colorType : constants.COLORTYPE_COLOR_ALPHA;
  1665. options.inputColorType = typeof options.inputColorType === "number" ? options.inputColorType : constants.COLORTYPE_COLOR_ALPHA;
  1666. if ([
  1667. constants.COLORTYPE_GRAYSCALE,
  1668. constants.COLORTYPE_COLOR,
  1669. constants.COLORTYPE_COLOR_ALPHA,
  1670. constants.COLORTYPE_ALPHA
  1671. ].indexOf(options.colorType) === -1) {
  1672. throw new Error(
  1673. "option color type:" + options.colorType + " is not supported at present"
  1674. );
  1675. }
  1676. if ([
  1677. constants.COLORTYPE_GRAYSCALE,
  1678. constants.COLORTYPE_COLOR,
  1679. constants.COLORTYPE_COLOR_ALPHA,
  1680. constants.COLORTYPE_ALPHA
  1681. ].indexOf(options.inputColorType) === -1) {
  1682. throw new Error(
  1683. "option input color type:" + options.inputColorType + " is not supported at present"
  1684. );
  1685. }
  1686. if (options.bitDepth !== 8 && options.bitDepth !== 16) {
  1687. throw new Error(
  1688. "option bit depth:" + options.bitDepth + " is not supported at present"
  1689. );
  1690. }
  1691. };
  1692. Packer.prototype.getDeflateOptions = function() {
  1693. return {
  1694. chunkSize: this._options.deflateChunkSize,
  1695. level: this._options.deflateLevel,
  1696. strategy: this._options.deflateStrategy
  1697. };
  1698. };
  1699. Packer.prototype.createDeflate = function() {
  1700. return this._options.deflateFactory(this.getDeflateOptions());
  1701. };
  1702. Packer.prototype.filterData = function(data, width, height) {
  1703. let packedData = bitPacker(data, width, height, this._options);
  1704. let bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType];
  1705. let filteredData = filter(packedData, width, height, this._options, bpp);
  1706. return filteredData;
  1707. };
  1708. Packer.prototype._packChunk = function(type, data) {
  1709. let len = data ? data.length : 0;
  1710. let buf = Buffer.alloc(len + 12);
  1711. buf.writeUInt32BE(len, 0);
  1712. buf.writeUInt32BE(type, 4);
  1713. if (data) {
  1714. data.copy(buf, 8);
  1715. }
  1716. buf.writeInt32BE(
  1717. CrcStream.crc32(buf.slice(4, buf.length - 4)),
  1718. buf.length - 4
  1719. );
  1720. return buf;
  1721. };
  1722. Packer.prototype.packGAMA = function(gamma) {
  1723. let buf = Buffer.alloc(4);
  1724. buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0);
  1725. return this._packChunk(constants.TYPE_gAMA, buf);
  1726. };
  1727. Packer.prototype.packIHDR = function(width, height) {
  1728. let buf = Buffer.alloc(13);
  1729. buf.writeUInt32BE(width, 0);
  1730. buf.writeUInt32BE(height, 4);
  1731. buf[8] = this._options.bitDepth;
  1732. buf[9] = this._options.colorType;
  1733. buf[10] = 0;
  1734. buf[11] = 0;
  1735. buf[12] = 0;
  1736. return this._packChunk(constants.TYPE_IHDR, buf);
  1737. };
  1738. Packer.prototype.packIDAT = function(data) {
  1739. return this._packChunk(constants.TYPE_IDAT, data);
  1740. };
  1741. Packer.prototype.packIEND = function() {
  1742. return this._packChunk(constants.TYPE_IEND, null);
  1743. };
  1744. }
  1745. });
  1746. // node_modules/pngjs/lib/packer-async.js
  1747. var require_packer_async = __commonJS({
  1748. "node_modules/pngjs/lib/packer-async.js"(exports2, module2) {
  1749. "use strict";
  1750. var util = require("util");
  1751. var Stream = require("stream");
  1752. var constants = require_constants();
  1753. var Packer = require_packer();
  1754. var PackerAsync = module2.exports = function(opt) {
  1755. Stream.call(this);
  1756. let options = opt || {};
  1757. this._packer = new Packer(options);
  1758. this._deflate = this._packer.createDeflate();
  1759. this.readable = true;
  1760. };
  1761. util.inherits(PackerAsync, Stream);
  1762. PackerAsync.prototype.pack = function(data, width, height, gamma) {
  1763. this.emit("data", Buffer.from(constants.PNG_SIGNATURE));
  1764. this.emit("data", this._packer.packIHDR(width, height));
  1765. if (gamma) {
  1766. this.emit("data", this._packer.packGAMA(gamma));
  1767. }
  1768. let filteredData = this._packer.filterData(data, width, height);
  1769. this._deflate.on("error", this.emit.bind(this, "error"));
  1770. this._deflate.on(
  1771. "data",
  1772. (function(compressedData) {
  1773. this.emit("data", this._packer.packIDAT(compressedData));
  1774. }).bind(this)
  1775. );
  1776. this._deflate.on(
  1777. "end",
  1778. (function() {
  1779. this.emit("data", this._packer.packIEND());
  1780. this.emit("end");
  1781. }).bind(this)
  1782. );
  1783. this._deflate.end(filteredData);
  1784. };
  1785. }
  1786. });
  1787. // node_modules/pngjs/lib/sync-inflate.js
  1788. var require_sync_inflate = __commonJS({
  1789. "node_modules/pngjs/lib/sync-inflate.js"(exports2, module2) {
  1790. "use strict";
  1791. var assert = require("assert").ok;
  1792. var zlib = require("zlib");
  1793. var util = require("util");
  1794. var kMaxLength = require("buffer").kMaxLength;
  1795. function Inflate(opts) {
  1796. if (!(this instanceof Inflate)) {
  1797. return new Inflate(opts);
  1798. }
  1799. if (opts && opts.chunkSize < zlib.Z_MIN_CHUNK) {
  1800. opts.chunkSize = zlib.Z_MIN_CHUNK;
  1801. }
  1802. zlib.Inflate.call(this, opts);
  1803. this._offset = this._offset === void 0 ? this._outOffset : this._offset;
  1804. this._buffer = this._buffer || this._outBuffer;
  1805. if (opts && opts.maxLength != null) {
  1806. this._maxLength = opts.maxLength;
  1807. }
  1808. }
  1809. function createInflate(opts) {
  1810. return new Inflate(opts);
  1811. }
  1812. function _close(engine, callback) {
  1813. if (callback) {
  1814. process.nextTick(callback);
  1815. }
  1816. if (!engine._handle) {
  1817. return;
  1818. }
  1819. engine._handle.close();
  1820. engine._handle = null;
  1821. }
  1822. Inflate.prototype._processChunk = function(chunk, flushFlag, asyncCb) {
  1823. if (typeof asyncCb === "function") {
  1824. return zlib.Inflate._processChunk.call(this, chunk, flushFlag, asyncCb);
  1825. }
  1826. let self = this;
  1827. let availInBefore = chunk && chunk.length;
  1828. let availOutBefore = this._chunkSize - this._offset;
  1829. let leftToInflate = this._maxLength;
  1830. let inOff = 0;
  1831. let buffers = [];
  1832. let nread = 0;
  1833. let error;
  1834. this.on("error", function(err) {
  1835. error = err;
  1836. });
  1837. function handleChunk(availInAfter, availOutAfter) {
  1838. if (self._hadError) {
  1839. return;
  1840. }
  1841. let have = availOutBefore - availOutAfter;
  1842. assert(have >= 0, "have should not go down");
  1843. if (have > 0) {
  1844. let out = self._buffer.slice(self._offset, self._offset + have);
  1845. self._offset += have;
  1846. if (out.length > leftToInflate) {
  1847. out = out.slice(0, leftToInflate);
  1848. }
  1849. buffers.push(out);
  1850. nread += out.length;
  1851. leftToInflate -= out.length;
  1852. if (leftToInflate === 0) {
  1853. return false;
  1854. }
  1855. }
  1856. if (availOutAfter === 0 || self._offset >= self._chunkSize) {
  1857. availOutBefore = self._chunkSize;
  1858. self._offset = 0;
  1859. self._buffer = Buffer.allocUnsafe(self._chunkSize);
  1860. }
  1861. if (availOutAfter === 0) {
  1862. inOff += availInBefore - availInAfter;
  1863. availInBefore = availInAfter;
  1864. return true;
  1865. }
  1866. return false;
  1867. }
  1868. assert(this._handle, "zlib binding closed");
  1869. let res;
  1870. do {
  1871. res = this._handle.writeSync(
  1872. flushFlag,
  1873. chunk,
  1874. // in
  1875. inOff,
  1876. // in_off
  1877. availInBefore,
  1878. // in_len
  1879. this._buffer,
  1880. // out
  1881. this._offset,
  1882. //out_off
  1883. availOutBefore
  1884. );
  1885. res = res || this._writeState;
  1886. } while (!this._hadError && handleChunk(res[0], res[1]));
  1887. if (this._hadError) {
  1888. throw error;
  1889. }
  1890. if (nread >= kMaxLength) {
  1891. _close(this);
  1892. throw new RangeError(
  1893. "Cannot create final Buffer. It would be larger than 0x" + kMaxLength.toString(16) + " bytes"
  1894. );
  1895. }
  1896. let buf = Buffer.concat(buffers, nread);
  1897. _close(this);
  1898. return buf;
  1899. };
  1900. util.inherits(Inflate, zlib.Inflate);
  1901. function zlibBufferSync(engine, buffer) {
  1902. if (typeof buffer === "string") {
  1903. buffer = Buffer.from(buffer);
  1904. }
  1905. if (!(buffer instanceof Buffer)) {
  1906. throw new TypeError("Not a string or buffer");
  1907. }
  1908. let flushFlag = engine._finishFlushFlag;
  1909. if (flushFlag == null) {
  1910. flushFlag = zlib.Z_FINISH;
  1911. }
  1912. return engine._processChunk(buffer, flushFlag);
  1913. }
  1914. function inflateSync(buffer, opts) {
  1915. return zlibBufferSync(new Inflate(opts), buffer);
  1916. }
  1917. module2.exports = exports2 = inflateSync;
  1918. exports2.Inflate = Inflate;
  1919. exports2.createInflate = createInflate;
  1920. exports2.inflateSync = inflateSync;
  1921. }
  1922. });
  1923. // node_modules/pngjs/lib/sync-reader.js
  1924. var require_sync_reader = __commonJS({
  1925. "node_modules/pngjs/lib/sync-reader.js"(exports2, module2) {
  1926. "use strict";
  1927. var SyncReader = module2.exports = function(buffer) {
  1928. this._buffer = buffer;
  1929. this._reads = [];
  1930. };
  1931. SyncReader.prototype.read = function(length, callback) {
  1932. this._reads.push({
  1933. length: Math.abs(length),
  1934. // if length < 0 then at most this length
  1935. allowLess: length < 0,
  1936. func: callback
  1937. });
  1938. };
  1939. SyncReader.prototype.process = function() {
  1940. while (this._reads.length > 0 && this._buffer.length) {
  1941. let read = this._reads[0];
  1942. if (this._buffer.length && (this._buffer.length >= read.length || read.allowLess)) {
  1943. this._reads.shift();
  1944. let buf = this._buffer;
  1945. this._buffer = buf.slice(read.length);
  1946. read.func.call(this, buf.slice(0, read.length));
  1947. } else {
  1948. break;
  1949. }
  1950. }
  1951. if (this._reads.length > 0) {
  1952. throw new Error("There are some read requests waitng on finished stream");
  1953. }
  1954. if (this._buffer.length > 0) {
  1955. throw new Error("unrecognised content at end of stream");
  1956. }
  1957. };
  1958. }
  1959. });
  1960. // node_modules/pngjs/lib/filter-parse-sync.js
  1961. var require_filter_parse_sync = __commonJS({
  1962. "node_modules/pngjs/lib/filter-parse-sync.js"(exports2) {
  1963. "use strict";
  1964. var SyncReader = require_sync_reader();
  1965. var Filter = require_filter_parse();
  1966. exports2.process = function(inBuffer, bitmapInfo) {
  1967. let outBuffers = [];
  1968. let reader = new SyncReader(inBuffer);
  1969. let filter = new Filter(bitmapInfo, {
  1970. read: reader.read.bind(reader),
  1971. write: function(bufferPart) {
  1972. outBuffers.push(bufferPart);
  1973. },
  1974. complete: function() {
  1975. }
  1976. });
  1977. filter.start();
  1978. reader.process();
  1979. return Buffer.concat(outBuffers);
  1980. };
  1981. }
  1982. });
  1983. // node_modules/pngjs/lib/parser-sync.js
  1984. var require_parser_sync = __commonJS({
  1985. "node_modules/pngjs/lib/parser-sync.js"(exports2, module2) {
  1986. "use strict";
  1987. var hasSyncZlib = true;
  1988. var zlib = require("zlib");
  1989. var inflateSync = require_sync_inflate();
  1990. if (!zlib.deflateSync) {
  1991. hasSyncZlib = false;
  1992. }
  1993. var SyncReader = require_sync_reader();
  1994. var FilterSync = require_filter_parse_sync();
  1995. var Parser = require_parser();
  1996. var bitmapper = require_bitmapper();
  1997. var formatNormaliser = require_format_normaliser();
  1998. module2.exports = function(buffer, options) {
  1999. if (!hasSyncZlib) {
  2000. throw new Error(
  2001. "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
  2002. );
  2003. }
  2004. let err;
  2005. function handleError(_err_) {
  2006. err = _err_;
  2007. }
  2008. let metaData;
  2009. function handleMetaData(_metaData_) {
  2010. metaData = _metaData_;
  2011. }
  2012. function handleTransColor(transColor) {
  2013. metaData.transColor = transColor;
  2014. }
  2015. function handlePalette(palette) {
  2016. metaData.palette = palette;
  2017. }
  2018. function handleSimpleTransparency() {
  2019. metaData.alpha = true;
  2020. }
  2021. let gamma;
  2022. function handleGamma(_gamma_) {
  2023. gamma = _gamma_;
  2024. }
  2025. let inflateDataList = [];
  2026. function handleInflateData(inflatedData2) {
  2027. inflateDataList.push(inflatedData2);
  2028. }
  2029. let reader = new SyncReader(buffer);
  2030. let parser = new Parser(options, {
  2031. read: reader.read.bind(reader),
  2032. error: handleError,
  2033. metadata: handleMetaData,
  2034. gamma: handleGamma,
  2035. palette: handlePalette,
  2036. transColor: handleTransColor,
  2037. inflateData: handleInflateData,
  2038. simpleTransparency: handleSimpleTransparency
  2039. });
  2040. parser.start();
  2041. reader.process();
  2042. if (err) {
  2043. throw err;
  2044. }
  2045. let inflateData = Buffer.concat(inflateDataList);
  2046. inflateDataList.length = 0;
  2047. let inflatedData;
  2048. if (metaData.interlace) {
  2049. inflatedData = zlib.inflateSync(inflateData);
  2050. } else {
  2051. let rowSize = (metaData.width * metaData.bpp * metaData.depth + 7 >> 3) + 1;
  2052. let imageSize = rowSize * metaData.height;
  2053. inflatedData = inflateSync(inflateData, {
  2054. chunkSize: imageSize,
  2055. maxLength: imageSize
  2056. });
  2057. }
  2058. inflateData = null;
  2059. if (!inflatedData || !inflatedData.length) {
  2060. throw new Error("bad png - invalid inflate data response");
  2061. }
  2062. let unfilteredData = FilterSync.process(inflatedData, metaData);
  2063. inflateData = null;
  2064. let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
  2065. unfilteredData = null;
  2066. let normalisedBitmapData = formatNormaliser(
  2067. bitmapData,
  2068. metaData,
  2069. options.skipRescale
  2070. );
  2071. metaData.data = normalisedBitmapData;
  2072. metaData.gamma = gamma || 0;
  2073. return metaData;
  2074. };
  2075. }
  2076. });
  2077. // node_modules/pngjs/lib/packer-sync.js
  2078. var require_packer_sync = __commonJS({
  2079. "node_modules/pngjs/lib/packer-sync.js"(exports2, module2) {
  2080. "use strict";
  2081. var hasSyncZlib = true;
  2082. var zlib = require("zlib");
  2083. if (!zlib.deflateSync) {
  2084. hasSyncZlib = false;
  2085. }
  2086. var constants = require_constants();
  2087. var Packer = require_packer();
  2088. module2.exports = function(metaData, opt) {
  2089. if (!hasSyncZlib) {
  2090. throw new Error(
  2091. "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
  2092. );
  2093. }
  2094. let options = opt || {};
  2095. let packer = new Packer(options);
  2096. let chunks = [];
  2097. chunks.push(Buffer.from(constants.PNG_SIGNATURE));
  2098. chunks.push(packer.packIHDR(metaData.width, metaData.height));
  2099. if (metaData.gamma) {
  2100. chunks.push(packer.packGAMA(metaData.gamma));
  2101. }
  2102. let filteredData = packer.filterData(
  2103. metaData.data,
  2104. metaData.width,
  2105. metaData.height
  2106. );
  2107. let compressedData = zlib.deflateSync(
  2108. filteredData,
  2109. packer.getDeflateOptions()
  2110. );
  2111. filteredData = null;
  2112. if (!compressedData || !compressedData.length) {
  2113. throw new Error("bad png - invalid compressed data response");
  2114. }
  2115. chunks.push(packer.packIDAT(compressedData));
  2116. chunks.push(packer.packIEND());
  2117. return Buffer.concat(chunks);
  2118. };
  2119. }
  2120. });
  2121. // node_modules/pngjs/lib/png-sync.js
  2122. var require_png_sync = __commonJS({
  2123. "node_modules/pngjs/lib/png-sync.js"(exports2) {
  2124. "use strict";
  2125. var parse = require_parser_sync();
  2126. var pack = require_packer_sync();
  2127. exports2.read = function(buffer, options) {
  2128. return parse(buffer, options || {});
  2129. };
  2130. exports2.write = function(png, options) {
  2131. return pack(png, options);
  2132. };
  2133. }
  2134. });
  2135. // node_modules/pngjs/lib/png.js
  2136. var require_png = __commonJS({
  2137. "node_modules/pngjs/lib/png.js"(exports2) {
  2138. "use strict";
  2139. var util = require("util");
  2140. var Stream = require("stream");
  2141. var Parser = require_parser_async();
  2142. var Packer = require_packer_async();
  2143. var PNGSync = require_png_sync();
  2144. var PNG2 = exports2.PNG = function(options) {
  2145. Stream.call(this);
  2146. options = options || {};
  2147. this.width = options.width | 0;
  2148. this.height = options.height | 0;
  2149. this.data = this.width > 0 && this.height > 0 ? Buffer.alloc(4 * this.width * this.height) : null;
  2150. if (options.fill && this.data) {
  2151. this.data.fill(0);
  2152. }
  2153. this.gamma = 0;
  2154. this.readable = this.writable = true;
  2155. this._parser = new Parser(options);
  2156. this._parser.on("error", this.emit.bind(this, "error"));
  2157. this._parser.on("close", this._handleClose.bind(this));
  2158. this._parser.on("metadata", this._metadata.bind(this));
  2159. this._parser.on("gamma", this._gamma.bind(this));
  2160. this._parser.on(
  2161. "parsed",
  2162. (function(data) {
  2163. this.data = data;
  2164. this.emit("parsed", data);
  2165. }).bind(this)
  2166. );
  2167. this._packer = new Packer(options);
  2168. this._packer.on("data", this.emit.bind(this, "data"));
  2169. this._packer.on("end", this.emit.bind(this, "end"));
  2170. this._parser.on("close", this._handleClose.bind(this));
  2171. this._packer.on("error", this.emit.bind(this, "error"));
  2172. };
  2173. util.inherits(PNG2, Stream);
  2174. PNG2.sync = PNGSync;
  2175. PNG2.prototype.pack = function() {
  2176. if (!this.data || !this.data.length) {
  2177. this.emit("error", "No data provided");
  2178. return this;
  2179. }
  2180. process.nextTick(
  2181. (function() {
  2182. this._packer.pack(this.data, this.width, this.height, this.gamma);
  2183. }).bind(this)
  2184. );
  2185. return this;
  2186. };
  2187. PNG2.prototype.parse = function(data, callback) {
  2188. if (callback) {
  2189. let onParsed, onError;
  2190. onParsed = (function(parsedData) {
  2191. this.removeListener("error", onError);
  2192. this.data = parsedData;
  2193. callback(null, this);
  2194. }).bind(this);
  2195. onError = (function(err) {
  2196. this.removeListener("parsed", onParsed);
  2197. callback(err, null);
  2198. }).bind(this);
  2199. this.once("parsed", onParsed);
  2200. this.once("error", onError);
  2201. }
  2202. this.end(data);
  2203. return this;
  2204. };
  2205. PNG2.prototype.write = function(data) {
  2206. this._parser.write(data);
  2207. return true;
  2208. };
  2209. PNG2.prototype.end = function(data) {
  2210. this._parser.end(data);
  2211. };
  2212. PNG2.prototype._metadata = function(metadata) {
  2213. this.width = metadata.width;
  2214. this.height = metadata.height;
  2215. this.emit("metadata", metadata);
  2216. };
  2217. PNG2.prototype._gamma = function(gamma) {
  2218. this.gamma = gamma;
  2219. };
  2220. PNG2.prototype._handleClose = function() {
  2221. if (!this._parser.writable && !this._packer.readable) {
  2222. this.emit("close");
  2223. }
  2224. };
  2225. PNG2.bitblt = function(src, dst, srcX, srcY, width, height, deltaX, deltaY) {
  2226. srcX |= 0;
  2227. srcY |= 0;
  2228. width |= 0;
  2229. height |= 0;
  2230. deltaX |= 0;
  2231. deltaY |= 0;
  2232. if (srcX > src.width || srcY > src.height || srcX + width > src.width || srcY + height > src.height) {
  2233. throw new Error("bitblt reading outside image");
  2234. }
  2235. if (deltaX > dst.width || deltaY > dst.height || deltaX + width > dst.width || deltaY + height > dst.height) {
  2236. throw new Error("bitblt writing outside image");
  2237. }
  2238. for (let y = 0; y < height; y++) {
  2239. src.data.copy(
  2240. dst.data,
  2241. (deltaY + y) * dst.width + deltaX << 2,
  2242. (srcY + y) * src.width + srcX << 2,
  2243. (srcY + y) * src.width + srcX + width << 2
  2244. );
  2245. }
  2246. };
  2247. PNG2.prototype.bitblt = function(dst, srcX, srcY, width, height, deltaX, deltaY) {
  2248. PNG2.bitblt(this, dst, srcX, srcY, width, height, deltaX, deltaY);
  2249. return this;
  2250. };
  2251. PNG2.adjustGamma = function(src) {
  2252. if (src.gamma) {
  2253. for (let y = 0; y < src.height; y++) {
  2254. for (let x = 0; x < src.width; x++) {
  2255. let idx = src.width * y + x << 2;
  2256. for (let i = 0; i < 3; i++) {
  2257. let sample = src.data[idx + i] / 255;
  2258. sample = Math.pow(sample, 1 / 2.2 / src.gamma);
  2259. src.data[idx + i] = Math.round(sample * 255);
  2260. }
  2261. }
  2262. }
  2263. src.gamma = 0;
  2264. }
  2265. };
  2266. PNG2.prototype.adjustGamma = function() {
  2267. PNG2.adjustGamma(this);
  2268. };
  2269. }
  2270. });
  2271. // src/screenshot/pixel-match.ts
  2272. var import_fs = __toESM(require("fs"));
  2273. var import_pixelmatch = __toESM(require_pixelmatch());
  2274. var import_pngjs = __toESM(require_png());
  2275. function getMismatchedPixels(pixelMatchInput) {
  2276. const imgA = import_fs.default.createReadStream(pixelMatchInput.imageAPath).pipe(new import_pngjs.PNG()).on("parsed", doneReading);
  2277. const imgB = import_fs.default.createReadStream(pixelMatchInput.imageBPath).pipe(new import_pngjs.PNG()).on("parsed", doneReading);
  2278. let filesRead = 0;
  2279. function doneReading() {
  2280. if (++filesRead < 2) return;
  2281. const mismatchedPixels = (0, import_pixelmatch.default)(imgA.data, imgB.data, null, pixelMatchInput.width, pixelMatchInput.height, {
  2282. threshold: pixelMatchInput.pixelmatchThreshold,
  2283. includeAA: false
  2284. });
  2285. if (typeof process.send !== "function") {
  2286. throw new Error("`getMismatchedPixels` must be run in a child process.");
  2287. }
  2288. process.send(mismatchedPixels);
  2289. }
  2290. }
  2291. process.on("message", getMismatchedPixels);