Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
gif.writeHeader()
sharp.cache(false) // Reduce memory usage
const totalFrames = 2 * FRAMES - 1
for (let i = 0; i < totalFrames; i++) {
// Render frame
const y = i < FRAMES ? i / FRAMES : 2 - i / FRAMES
timePiece.renderFrame(.5, y)
// Get frame data
let pixels = new Uint8Array(WIDTH_S * HEIGHT_S * 4)
gl.readPixels(0, 0, WIDTH_S, HEIGHT_S, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
// Resize and flip (otherwise gifs come out upside-down!?)
let framePixels = await sharp(Buffer.from(pixels.buffer), {
raw: {
width: WIDTH_S,
height: HEIGHT_S,
channels: 4,
}
})
.resize(WIDTH, HEIGHT)
.flip()
.toBuffer()
// Add to GIF (slow!)
gif.setDelay(i === 0 ? PAUSE : DURATION / FRAMES)
gif.addFrame(framePixels)
// This seems to help keep memory usage down.
pixels = null
format: false, // png / jpg / webp
// Force upscale
upscale: false,
// User parameters
...parameters
}
if (!params.width || !params.height)
return reject('Size not provided');
// Load sharp
let s = false;
try {
s = sharp().on('error', err => { return reject(err); });
}
catch (e) {
return reject(e)
}
if (!s)
return reject(e)
// Resize parameters
const opt = {
...((params.upscale) ? { withoutEnlargement: !!params.upscale } : {})
}
// Resize based on width
try {
if (params.minSize === 1)
s.resize(params.width, null, opt);
export async function forExport({frame, connection}) {
let {img} = frame;
// FIXME : CPU issues
img = Buffer.from(img, 'base64');
if ( ! connection.isSafari ) {
img = await sharp(img).webp(WEBP_OPTS).toBuffer();
}
img = img.toString('base64');
frame.img = img;
return frame;
}
async transformWrite(fileRecord) {
if (!transform) return null;
const { size, mod, format, type } = transform;
// Need to update the content type and extension of the file info, too.
// The new size gets set correctly automatically by FileCollections package.
fileRecord.type(type, { store: name });
fileRecord.extension(format, { store: name });
// resizing image, adding mod, setting output format
return sharp().resize(size, size)[mod]().toFormat(format);
}
})
let url = `https://s3.amazonaws.com/${organizationId}/${imageId}/original`
if (process.env.FILE_STORAGE === "offline") {
url = `http://localhost:5000/static/${imageId}/original.jpeg`
}
const response = await fetch(
url, {
method: 'GET'
})
const buffer = await response.buffer()
let meta = await sharp(buffer).metadata()
const info = {
"@context": "http://iiif.io/api/image/2/context.json",
"@id": `http://localhost:3000/iiif/${imageId}`,
"protocal": "http://iiif.io/api/image",
width: meta.width,
height: meta.height,
profile: [ "http://iiif.io/api/image/2/level2.json" ]
}
res.send(info)
} catch (ex) {
console.error(ex)
}
}
}
})
const tile = tiles[tileIdx]
tiles = tiles.filter((_, idx) => idx !== tileIdx)
for (let i = 0; i < tileHeight; i++) {
for (let j = 0; j < tileWidth; j++) {
const rightIdx = (tileWidth * i + j) * 3
const leftIdx = (width * (y * tileHeight + i) + x * tileWidth + j) * 3 ;
newPngData[leftIdx] = tile.buffer[rightIdx]
newPngData[leftIdx + 1] = tile.buffer[rightIdx + 1]
newPngData[leftIdx + 2] = tile.buffer[rightIdx + 2]
}
}
})
await sharp(Buffer.from(newPngData), { raw: { width, height, channels: 3 }})
.toFile(path.resolve(projectPath, 'dist', `${mosaic.split('.')[0]}-mosaic.png`))
}
async store(req, res) {
const { originalname: name, filename, path } = req.file;
const buffer = await sharp(path)
.resize(350, 350)
.toBuffer();
fs.writeFile(path, buffer, err => {
if (err) throw err;
});
const file = await File.create({
name,
path: filename,
});
return res.json(file);
}
}
async displayBlackWhiteImage(buffer, positionX, positionY, width){
//debug(url);
const image = sharp(buffer);
image.metadata()
.then((metadata) => {
image
.rotate((metadata.width>=metadata.height)?0:90)
.resize(width)
.extractChannel(1)
//.toFile(__dirname+"/../1.min.png");
.raw()
.toBuffer(async (err, data, info)=> {
debug(err);
debug(data.length);
debug(data);
debug(info);
//debug("fill", info.width, info.height, "0");
export async function readSourceImage(platform: Platform, type: ResourceType, src: string, errstream?: NodeJS.WritableStream): Promise {
const image = sharp(await readFile(src));
const metadata = await RASTER_RESOURCE_VALIDATORS[type](src, image);
debug('Source image for %s: %O', type, metadata);
return {
platform,
resource: type,
type: SourceType.RASTER,
src,
image: { src, pipeline: image, metadata },
};
}
async save(file: FileUpload, cb: Callback) {
await this.setFileProps(file);
const finalPath = path.join(this.destination, this.fileName);
const original = { fileName: this.fileName, width: 0, height: 0, fileSize: 0 };
const imageMeta = sharp().on('info', info => {
original.width = info.width;
original.height = info.height;
original.fileSize = info.size;
});
const outStream = fs.createWriteStream(finalPath);
outStream.on('error', cb);
outStream.on('finish', async () => {
const sizes = this.settings.crops.map(({ width, height }) => {
if (width < original.width && height > original.height) {
return [width];
} else if (height < original.height && width > original.width) {
return [null, height];
}
return [width, height];
});