MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / decompress

Method decompress

modules/commodetto/commodettoReadPNG.js:59–160  ·  view source on GitHub ↗
(data, pixelFormat, alpha)

Source from the content-addressed store, hash-verified

57// get palette()
58
59 static decompress(data, pixelFormat, alpha) {
60 let png = new PNG(data);
61 let width = png.width, height = png.height, channels = png.channels, depth = png.depth, palette = png.palette ? new Uint8Array(png.palette) : undefined;
62
63 if ((8 == depth) && (((3 == channels) || (4 == channels)) || ((1 == channels) && palette))) { // RGB (24) or RGBA (32) or indexed (8)
64 let image = new BufferOut({width, height, pixelFormat});
65 image.begin(0, 0, width, height);
66 let scanOut = new ArrayBuffer((width * Bitmap.depth(pixelFormat)) >> 3);
67 let convert = new Converter((3 == channels) ? Bitmap.RGB24 : Bitmap.RGBA32, pixelFormat);
68
69 let mask, scanAlpha;
70 if (alpha && ((4 == channels) || palette)) {
71 mask = new BufferOut({width: width, height: height, pixelFormat: alpha}); // only works when alpha is Bitmap.Gray16
72 mask.begin(0, 0, width, height);
73 scanAlpha = new Uint8Array(scanOut);
74 }
75
76 for (let i = 0; i < height; i++) {
77 let scanLine = png.read();
78
79 if (palette) {
80//@@ need a converter!
81 for (let j = 0; j < width; j++) {
82 let offset = scanLine[j] * 4;
83 let r = palette[offset ];
84 let g = palette[offset + 1];
85 let b = palette[offset + 2];
86 scanOut[j] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
87 }
88 }
89 else
90 convert.process(scanLine.buffer, scanOut);
91 image.send(scanOut);
92
93 if (mask) {
94//@ converters!
95 if (palette) {
96 for (let j = 0; j < width; j += 2) {
97 let a0 = palette[(scanLine[j] * 4) + 3];
98 let a1 = palette[(scanLine[j + 1] * 4) + 3];
99 scanAlpha[j >> 1] = ~((a0 & 0xf0) | (a1 >> 4));
100 }
101 }
102 else {
103 for (let j = 0, offset = 3; j < (width >> 1); j++, offset += (channels << 1)) {
104 let a0 = scanLine[offset];
105 let a1 = scanLine[offset + channels];
106 scanAlpha[j] = ~((a0 & 0xf0) | (a1 >> 4));
107 }
108 }
109 mask.send(scanAlpha.buffer, 0, width >> 1);
110 }
111 }
112
113 image.end();
114 image = image.bitmap;
115 if (mask) {
116 mask.end();

Callers

nothing calls this directly

Calls 9

beginMethod · 0.95
readMethod · 0.95
sendMethod · 0.95
endMethod · 0.95
beginMethod · 0.65
endMethod · 0.65
depthMethod · 0.45
processMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected