How to use jpeg-js - 10 common examples

To help you get started, we’ve selected a few jpeg-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tensorflow / tfjs / tfjs-react-native / src / decode_image.ts View on Github external
export function decodeJpeg(
    contents: Uint8Array, channels: 0|1|3 = 3): Tensor3D {
  util.assert(
      getImageType(contents) === ImageType.JPEG,
      () => 'The passed contents are not a valid JPEG image');
  util.assert(
      channels === 3, () => 'Only 3 channels is supported at this time');
  const TO_UINT8ARRAY = true;
  const {width, height, data} = jpeg.decode(contents, TO_UINT8ARRAY);
  // Drop the alpha channel info because jpeg.decode always returns a typedArray
  // with 255
  const buffer = new Uint8Array(width * height * 3);
  let offset = 0;  // offset into original data
  for (let i = 0; i < buffer.length; i += 3) {
    buffer[i] = data[offset];
    buffer[i + 1] = data[offset + 1];
    buffer[i + 2] = data[offset + 2];

    offset += 4;
  }

  return tensor3d(buffer, [height, width, channels]);
}
github tensorflow / tfjs / tfjs-react-native / integration_rn59 / components / mobilenet_demo.tsx View on Github external
imageToTensor(rawImageData: ArrayBuffer): tf.Tensor3D {
    const TO_UINT8ARRAY = true;
    const { width, height, data } = jpeg.decode(rawImageData, TO_UINT8ARRAY);
    // Drop the alpha channel info for mobilenet
    const buffer = new Uint8Array(width * height * 3);
    let offset = 0; // offset into original data
    for (let i = 0; i < buffer.length; i += 3) {
      buffer[i] = data[offset];
      buffer[i + 1] = data[offset + 1];
      buffer[i + 2] = data[offset + 2];

      offset += 4;
    }

    return tf.tensor3d(buffer, [height, width, 3]);
  }
github microsoft / screenshots-diff-toolkit / screenshots-diff / diffImagesAsync.ts View on Github external
return new Promise(async resolve => {
    const imageAndTestInformation: ImageAndTestInformation = {};
    if (fs.existsSync(imagePath)) {
      const jsonPath = imagePath.replace(/\.[^.]+$/i, ".json");
      if (fs.existsSync(jsonPath)) {
        imageAndTestInformation.info = JSON.parse(
          fs.readFileSync(jsonPath, { encoding: "utf-8" })
        ) as TestInformation;
      }

      try {
        const isPNG = imagePath.endsWith(".png");
        imageAndTestInformation.image = isPNG
          ? await decodePngImage(imagePath)
          : decode(fs.readFileSync(imagePath));
      } catch (imageDecodeError) {
        // if the image is corrupted then PNG/JPG image package fails to decode and throws error
        imageAndTestInformation.image = undefined;
      }

      if (!imageAndTestInformation.info && imageAndTestInformation.image) {
        const imageFileName = getImageFileNameFromPath(imagePath);
        imageAndTestInformation.info = {
          name: imageFileName,
          viewport: {
            x: 0,
            y: 0,
            width: imageAndTestInformation.image.width,
            height: imageAndTestInformation.image.height,
            scale: 1
          }
github neuroanatomy / BrainBox / xt / app.js View on Github external
case 'axi':i=ya*brain.dim[1]*brain.dim[0]+ y*brain.dim[0]+x; break;
		}
		val=255*(brain.data[i]-brain.min)/(brain.max-brain.min);
		frameData[4*j+0] = val; // red
		frameData[4*j+1] = val; // green
		frameData[4*j+2] = val; // blue
		frameData[4*j+3] = 0xFF; // alpha - ignored in JPEGs
		j++;
	}

	var rawImageData = {
	  data: frameData,
	  width: brain_W,
	  height: brain_H
	};
	return jpeg.encode(rawImageData,99);
}
github oliver-moran / jimp / index.js View on Github external
filterType: this._filterType,
              colorType: (this._rgba) ? 6 : 2,
              inputHasAlpha: true
            });
            
            if (this._rgba) png.data = new Buffer(this.bitmap.data);
            else png.data = compositeBitmapOverBackground(this).data; // when PNG doesn't support alpha
            
            StreamToBuffer(png.pack(), function (err, buffer) {
                return cb.call(that, null, buffer);
            });
            break;

        case Jimp.MIME_JPEG:
            // composite onto a new image so that the background shows through alpha channels
            var jpeg = JPEG.encode(compositeBitmapOverBackground(this), this._quality);
            return cb.call(this, null, jpeg.data);

        case Jimp.MIME_BMP:
            // composite onto a new image so that the background shows through alpha channels
            var bmp = BMP.encode(compositeBitmapOverBackground(this));
            return cb.call(this, null, bmp.data);

        default:
            return cb.call(this, "Unsupported MIME type: " + mime);
    }

    return this;
};
github neuroanatomy / BrainBox / controller / atlasMakerServer / atlasMakerServer.js View on Github external
frameData[4*j + 0] = 255*val; // red
                        frameData[4*j + 1] = 255*val; // green
                        frameData[4*j + 2] = 255*val; // blue
                    }
                    frameData[4*j + 3] = 0xFF; // alpha - ignored in JPEGs
                    j += 1;
                }
            }

            var rawImageData = {
              data: frameData,
              width: brainWidth,
              height: brainHeight
            };

            return jpeg.encode(rawImageData, 99);
        },
github neuroanatomy / BrainBox / controller / atlasmakerServer / atlasmakerServer.js View on Github external
frameData[4*j + 0] = 255*val; // red
                        frameData[4*j + 1] = 255*val; // green
                        frameData[4*j + 2] = 255*val; // blue
                    }
                    frameData[4*j + 3] = 0xFF; // alpha - ignored in JPEGs
                    j += 1;
                }
            }

            var rawImageData = {
              data: frameData,
              width: brainWidth,
              height: brainHeight
            };

            return jpeg.encode(rawImageData, 99);
        },
github johansatge / jpeg-autorotate / test / helpers.js View on Github external
function comparePixels(originPathOrBuffer, transformedBuffer) {
  const targetBuffer = fs.readFileSync(originPathOrBuffer.replace('.jpg', '_dest.jpg'))
  const targetJpeg = jpegjs.decode(targetBuffer)
  const diffPng = new PNG({width: targetJpeg.width, height: targetJpeg.height})
  const diffPixels = pixelmatch(
    jpegjs.decode(transformedBuffer).data,
    targetJpeg.data,
    diffPng.data,
    targetJpeg.width,
    targetJpeg.height,
    {
      threshold: 0.25,
    }
  )
  const diffPath = path.join(
    path.join(__dirname, '.tmp'),
    path.parse(originPathOrBuffer).base.replace('.jpg', '.diff.png')
  )
  diffPng.pack().pipe(fs.createWriteStream(diffPath))
  return diffPixels === 0
}
github unbornchikken / NOOOCL / test / imageTests.js View on Github external
.then(function () {
                                    assert(out.ptr ? true : false);
                                    assert.equal(out.rowPitch, inputImage.width * 4);
                                    assert.equal(ref.address(out.ptr), ref.address(outputData));

                                    var outputImage = {
                                        width: inputImage.width,
                                        height: inputImage.height,
                                        data: outputData
                                    };

                                    var jpegData = jpeg.encode(outputImage, 85);

                                    return fs.writeFileAsync(path.join(cd, "out.jpg"), jpegData.data, "binary").finally(
                                        function () {
                                            return queue.waitable().enqueueUnmapMemory(dst, out.ptr).promise;
                                        });
                                });
                        });
github google / node-gles / tfjs-mobilenet.js View on Github external
path => {
      const buf = fs.readFileSync(path);
      const pixels = jpeg.decode(buf, true);
      return pixels;
    }

jpeg-js

A pure javascript JPEG encoder and decoder

BSD-3-Clause
Latest version published 2 years ago

Package Health Score

77 / 100
Full package analysis

Popular jpeg-js functions