Ejemplo n.º 1
0
export function decodeJpeg(buffer: Uint8Array): Image {
  const jpeg = decode(buffer, true);
  return new Image(jpeg.width, jpeg.height, {
    data: jpeg.data,
    kind: ImageKind.RGBA
  });
}
Ejemplo n.º 2
0
        fs.readFile(file, function(error: any, data: any) {
          if (error) {
            callback.call(
              thisArg,
              new Error("FS: Can't read file " + file + ', error: ' + error),
              null,
            );
            return;
          }

          // read jpeg
          let textureJpeg;
          try {
            textureJpeg = jpegEngine.decode(data);
          } catch (e) {
            callback.call(
              thisArg,
              new Error("JPG: Can't decode file " + file + ', error: ' + e),
              null,
            );
            return;
          }

          // create png
          const texturePng = new pngEngine({
            filterType: 0,
            width: textureJpeg.width,
            height: textureJpeg.height,
          });

          // convert data from jpg_plugin (rgb) to png_plugin (rgb)
          for (let i = 0; i < textureJpeg.data.length; i += 4) {
            texturePng.data[i] = textureJpeg.data[i];
            texturePng.data[i + 1] = textureJpeg.data[i + 1];
            texturePng.data[i + 2] = textureJpeg.data[i + 2];
            texturePng.data[i + 3] = textureJpeg.data[i + 3];
          }
          callback.call(thisArg, null, texturePng);
        });