Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]);
}
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]);
}
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
}
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
}
path => {
const buf = fs.readFileSync(path);
const pixels = jpeg.decode(buf, true);
return pixels;
}
function handleJPEG(data, cb) {
var jpegData
try {
jpegData = jpeg.decode(data)
}
catch(e) {
cb(e)
return
}
if(!jpegData) {
cb(new Error("Error decoding jpeg"))
return
}
var nshape = [ jpegData.height, jpegData.width, 4 ]
var result = ndarray(jpegData.data, nshape)
cb(null, result.transpose(1,0))
}
readImage( path ) {
const buf = fs.readFileSync( path )
const pixels = jpeg.decode( buf, true )
return pixels
}
function parseJpg(buffer, callback) {
var image
try {
image = jpeg.decode(buffer)
} catch (e) {
return callback(e)
}
var img = new Image(image.height, image.width)
img.addFrame(image.data)
return callback(null, img)
}