parser-sync.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. let hasSyncZlib = true;
  3. let zlib = require("zlib");
  4. let inflateSync = require("./sync-inflate");
  5. if (!zlib.deflateSync) {
  6. hasSyncZlib = false;
  7. }
  8. let SyncReader = require("./sync-reader");
  9. let FilterSync = require("./filter-parse-sync");
  10. let Parser = require("./parser");
  11. let bitmapper = require("./bitmapper");
  12. let formatNormaliser = require("./format-normaliser");
  13. module.exports = function (buffer, options) {
  14. if (!hasSyncZlib) {
  15. throw new Error(
  16. "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
  17. );
  18. }
  19. let err;
  20. function handleError(_err_) {
  21. err = _err_;
  22. }
  23. let metaData;
  24. function handleMetaData(_metaData_) {
  25. metaData = _metaData_;
  26. }
  27. function handleTransColor(transColor) {
  28. metaData.transColor = transColor;
  29. }
  30. function handlePalette(palette) {
  31. metaData.palette = palette;
  32. }
  33. function handleSimpleTransparency() {
  34. metaData.alpha = true;
  35. }
  36. let gamma;
  37. function handleGamma(_gamma_) {
  38. gamma = _gamma_;
  39. }
  40. let inflateDataList = [];
  41. function handleInflateData(inflatedData) {
  42. inflateDataList.push(inflatedData);
  43. }
  44. let reader = new SyncReader(buffer);
  45. let parser = new Parser(options, {
  46. read: reader.read.bind(reader),
  47. error: handleError,
  48. metadata: handleMetaData,
  49. gamma: handleGamma,
  50. palette: handlePalette,
  51. transColor: handleTransColor,
  52. inflateData: handleInflateData,
  53. simpleTransparency: handleSimpleTransparency,
  54. });
  55. parser.start();
  56. reader.process();
  57. if (err) {
  58. throw err;
  59. }
  60. //join together the inflate datas
  61. let inflateData = Buffer.concat(inflateDataList);
  62. inflateDataList.length = 0;
  63. let inflatedData;
  64. if (metaData.interlace) {
  65. inflatedData = zlib.inflateSync(inflateData);
  66. } else {
  67. let rowSize =
  68. ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
  69. let imageSize = rowSize * metaData.height;
  70. inflatedData = inflateSync(inflateData, {
  71. chunkSize: imageSize,
  72. maxLength: imageSize,
  73. });
  74. }
  75. inflateData = null;
  76. if (!inflatedData || !inflatedData.length) {
  77. throw new Error("bad png - invalid inflate data response");
  78. }
  79. let unfilteredData = FilterSync.process(inflatedData, metaData);
  80. inflateData = null;
  81. let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
  82. unfilteredData = null;
  83. let normalisedBitmapData = formatNormaliser(bitmapData, metaData);
  84. metaData.data = normalisedBitmapData;
  85. metaData.gamma = gamma || 0;
  86. return metaData;
  87. };