codecs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.codecs = exports.preprocessors = void 0;
  6. var _fs = require("fs");
  7. var path = _interopRequireWildcard(require("path"));
  8. var _emscriptenUtilsJs = require("./emscripten-utils.js");
  9. var _mozjpegNodeEncJs = _interopRequireDefault(require("./mozjpeg/mozjpeg_node_enc.js"));
  10. var _mozjpegNodeDecJs = _interopRequireDefault(require("./mozjpeg/mozjpeg_node_dec.js"));
  11. var _webpNodeEncJs = _interopRequireDefault(require("./webp/webp_node_enc.js"));
  12. var _webpNodeDecJs = _interopRequireDefault(require("./webp/webp_node_dec.js"));
  13. var _avifNodeEncJs = _interopRequireDefault(require("./avif/avif_node_enc.js"));
  14. var _avifNodeDecJs = _interopRequireDefault(require("./avif/avif_node_dec.js"));
  15. var pngEncDec = _interopRequireWildcard(require("./png/squoosh_png.js"));
  16. var oxipng = _interopRequireWildcard(require("./png/squoosh_oxipng.js"));
  17. var resize = _interopRequireWildcard(require("./resize/squoosh_resize.js"));
  18. var _imageData = _interopRequireDefault(require("./image_data"));
  19. function _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {
  21. default: obj
  22. };
  23. }
  24. function _getRequireWildcardCache() {
  25. if (typeof WeakMap !== "function") return null;
  26. var cache = new WeakMap();
  27. _getRequireWildcardCache = function() {
  28. return cache;
  29. };
  30. return cache;
  31. }
  32. function _interopRequireWildcard(obj) {
  33. if (obj && obj.__esModule) {
  34. return obj;
  35. }
  36. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  37. return {
  38. default: obj
  39. };
  40. }
  41. var cache = _getRequireWildcardCache();
  42. if (cache && cache.has(obj)) {
  43. return cache.get(obj);
  44. }
  45. var newObj = {};
  46. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  47. for(var key in obj){
  48. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  49. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  50. if (desc && (desc.get || desc.set)) {
  51. Object.defineProperty(newObj, key, desc);
  52. } else {
  53. newObj[key] = obj[key];
  54. }
  55. }
  56. }
  57. newObj.default = obj;
  58. if (cache) {
  59. cache.set(obj, newObj);
  60. }
  61. return newObj;
  62. }
  63. const mozEncWasm = path.resolve(__dirname, "./mozjpeg/mozjpeg_node_enc.wasm");
  64. const mozDecWasm = path.resolve(__dirname, "./mozjpeg/mozjpeg_node_dec.wasm");
  65. const webpEncWasm = path.resolve(__dirname, "./webp/webp_node_enc.wasm");
  66. const webpDecWasm = path.resolve(__dirname, "./webp/webp_node_dec.wasm");
  67. const avifEncWasm = path.resolve(__dirname, "./avif/avif_node_enc.wasm");
  68. const avifDecWasm = path.resolve(__dirname, "./avif/avif_node_dec.wasm");
  69. const pngEncDecWasm = path.resolve(__dirname, "./png/squoosh_png_bg.wasm");
  70. const pngEncDecInit = ()=>pngEncDec.default(_fs.promises.readFile((0, _emscriptenUtilsJs).pathify(pngEncDecWasm)));
  71. const oxipngWasm = path.resolve(__dirname, "./png/squoosh_oxipng_bg.wasm");
  72. const oxipngInit = ()=>oxipng.default(_fs.promises.readFile((0, _emscriptenUtilsJs).pathify(oxipngWasm)));
  73. const resizeWasm = path.resolve(__dirname, "./resize/squoosh_resize_bg.wasm");
  74. const resizeInit = ()=>resize.default(_fs.promises.readFile((0, _emscriptenUtilsJs).pathify(resizeWasm)));
  75. // rotate
  76. const rotateWasm = path.resolve(__dirname, "./rotate/rotate.wasm");
  77. global.ImageData = _imageData.default;
  78. function resizeNameToIndex(name) {
  79. switch(name){
  80. case "triangle":
  81. return 0;
  82. case "catrom":
  83. return 1;
  84. case "mitchell":
  85. return 2;
  86. case "lanczos3":
  87. return 3;
  88. default:
  89. throw Error(`Unknown resize algorithm "${name}"`);
  90. }
  91. }
  92. function resizeWithAspect({ input_width , input_height , target_width , target_height }) {
  93. if (!target_width && !target_height) {
  94. throw Error("Need to specify at least width or height when resizing");
  95. }
  96. if (target_width && target_height) {
  97. return {
  98. width: target_width,
  99. height: target_height
  100. };
  101. }
  102. if (!target_width) {
  103. return {
  104. width: Math.round(input_width / input_height * target_height),
  105. height: target_height
  106. };
  107. }
  108. return {
  109. width: target_width,
  110. height: Math.round(input_height / input_width * target_width)
  111. };
  112. }
  113. const preprocessors = {
  114. resize: {
  115. name: "Resize",
  116. description: "Resize the image before compressing",
  117. instantiate: async ()=>{
  118. await resizeInit();
  119. return (buffer, input_width, input_height, { width , height , method , premultiply , linearRGB })=>{
  120. ({ width , height } = resizeWithAspect({
  121. input_width,
  122. input_height,
  123. target_width: width,
  124. target_height: height
  125. }));
  126. const imageData = new _imageData.default(resize.resize(buffer, input_width, input_height, width, height, resizeNameToIndex(method), premultiply, linearRGB), width, height);
  127. resize.cleanup();
  128. return imageData;
  129. };
  130. },
  131. defaultOptions: {
  132. method: "lanczos3",
  133. fitMethod: "stretch",
  134. premultiply: true,
  135. linearRGB: true
  136. }
  137. },
  138. rotate: {
  139. name: "Rotate",
  140. description: "Rotate image",
  141. instantiate: async ()=>{
  142. return async (buffer, width, height, { numRotations })=>{
  143. const degrees = numRotations * 90 % 360;
  144. const sameDimensions = degrees === 0 || degrees === 180;
  145. const size = width * height * 4;
  146. const instance = (await WebAssembly.instantiate(await _fs.promises.readFile((0, _emscriptenUtilsJs).pathify(rotateWasm)))).instance;
  147. const { memory } = instance.exports;
  148. const additionalPagesNeeded = Math.ceil((size * 2 - memory.buffer.byteLength + 8) / (64 * 1024));
  149. if (additionalPagesNeeded > 0) {
  150. memory.grow(additionalPagesNeeded);
  151. }
  152. const view = new Uint8ClampedArray(memory.buffer);
  153. view.set(buffer, 8);
  154. instance.exports.rotate(width, height, degrees);
  155. return new _imageData.default(view.slice(size + 8, size * 2 + 8), sameDimensions ? width : height, sameDimensions ? height : width);
  156. };
  157. },
  158. defaultOptions: {
  159. numRotations: 0
  160. }
  161. }
  162. };
  163. exports.preprocessors = preprocessors;
  164. const codecs = {
  165. mozjpeg: {
  166. name: "MozJPEG",
  167. extension: "jpg",
  168. detectors: [
  169. /^\xFF\xD8\xFF/
  170. ],
  171. dec: ()=>(0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_mozjpegNodeDecJs.default, mozDecWasm),
  172. enc: ()=>(0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_mozjpegNodeEncJs.default, mozEncWasm),
  173. defaultEncoderOptions: {
  174. quality: 75,
  175. baseline: false,
  176. arithmetic: false,
  177. progressive: true,
  178. optimize_coding: true,
  179. smoothing: 0,
  180. color_space: 3 /*YCbCr*/ ,
  181. quant_table: 3,
  182. trellis_multipass: false,
  183. trellis_opt_zero: false,
  184. trellis_opt_table: false,
  185. trellis_loops: 1,
  186. auto_subsample: true,
  187. chroma_subsample: 2,
  188. separate_chroma_quality: false,
  189. chroma_quality: 75
  190. },
  191. autoOptimize: {
  192. option: "quality",
  193. min: 0,
  194. max: 100
  195. }
  196. },
  197. webp: {
  198. name: "WebP",
  199. extension: "webp",
  200. detectors: [
  201. /^RIFF....WEBPVP8[LX ]/s
  202. ],
  203. dec: ()=>(0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_webpNodeDecJs.default, webpDecWasm),
  204. enc: ()=>(0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_webpNodeEncJs.default, webpEncWasm),
  205. defaultEncoderOptions: {
  206. quality: 75,
  207. target_size: 0,
  208. target_PSNR: 0,
  209. method: 4,
  210. sns_strength: 50,
  211. filter_strength: 60,
  212. filter_sharpness: 0,
  213. filter_type: 1,
  214. partitions: 0,
  215. segments: 4,
  216. pass: 1,
  217. show_compressed: 0,
  218. preprocessing: 0,
  219. autofilter: 0,
  220. partition_limit: 0,
  221. alpha_compression: 1,
  222. alpha_filtering: 1,
  223. alpha_quality: 100,
  224. lossless: 0,
  225. exact: 0,
  226. image_hint: 0,
  227. emulate_jpeg_size: 0,
  228. thread_level: 0,
  229. low_memory: 0,
  230. near_lossless: 100,
  231. use_delta_palette: 0,
  232. use_sharp_yuv: 0
  233. },
  234. autoOptimize: {
  235. option: "quality",
  236. min: 0,
  237. max: 100
  238. }
  239. },
  240. avif: {
  241. name: "AVIF",
  242. extension: "avif",
  243. // eslint-disable-next-line no-control-regex
  244. detectors: [
  245. /^\x00\x00\x00 ftypavif\x00\x00\x00\x00/
  246. ],
  247. dec: ()=>(0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_avifNodeDecJs.default, avifDecWasm),
  248. enc: async ()=>{
  249. return (0, _emscriptenUtilsJs).instantiateEmscriptenWasm(_avifNodeEncJs.default, avifEncWasm);
  250. },
  251. defaultEncoderOptions: {
  252. cqLevel: 33,
  253. cqAlphaLevel: -1,
  254. denoiseLevel: 0,
  255. tileColsLog2: 0,
  256. tileRowsLog2: 0,
  257. speed: 6,
  258. subsample: 1,
  259. chromaDeltaQ: false,
  260. sharpness: 0,
  261. tune: 0 /* AVIFTune.auto */
  262. },
  263. autoOptimize: {
  264. option: "cqLevel",
  265. min: 62,
  266. max: 0
  267. }
  268. },
  269. oxipng: {
  270. name: "OxiPNG",
  271. extension: "png",
  272. // eslint-disable-next-line no-control-regex
  273. detectors: [
  274. /^\x89PNG\x0D\x0A\x1A\x0A/
  275. ],
  276. dec: async ()=>{
  277. await pngEncDecInit();
  278. return {
  279. decode: (buffer)=>{
  280. const imageData = pngEncDec.decode(buffer);
  281. pngEncDec.cleanup();
  282. return imageData;
  283. }
  284. };
  285. },
  286. enc: async ()=>{
  287. await pngEncDecInit();
  288. await oxipngInit();
  289. return {
  290. encode: (buffer, width, height, opts)=>{
  291. const simplePng = pngEncDec.encode(new Uint8Array(buffer), width, height);
  292. const imageData = oxipng.optimise(simplePng, opts.level, false);
  293. oxipng.cleanup();
  294. return imageData;
  295. }
  296. };
  297. },
  298. defaultEncoderOptions: {
  299. level: 2
  300. },
  301. autoOptimize: {
  302. option: "level",
  303. min: 6,
  304. max: 1
  305. }
  306. }
  307. };
  308. exports.codecs = codecs;
  309. //# sourceMappingURL=codecs.js.map