Intento implementar cultivos múltiples con raw. porque, las formas tradicionales de extraer y luego guardar en el búfer son demasiado lentas. para mi tarea, una imagen toma 700ms. Quiero reducir la llamada de extracto con manipular datos sin procesar. por lo que puede acelerar 10x - 20x. mi problema en extraer marco en tamaño de bloque secuencial. digamos 16x16 desde la parte superior izquierda hasta la parte inferior derecha.
const sharp = require('sharp'); const benchmark = () => { return new Promise((resolve) => { const currentTime = Date.now(); resolve(currentTime) }) }; const encode = async (absolutePathFile) => { const start_time = await benchmark(); const image = await sharp(absolutePathFile); const { data, info } = await image.ensureAlpha(1).raw().toBuffer({ resolveWithObject: true }); const { width, height, channels } = info; const colors = new Uint8ClampedArray(data.buffer); const pixels = []; const frame = []; const frameBlocks = []; // Convert colors to RGBA pixel for (let i=0; i < colors.length; i += channels) { const pixel = []; for (let j=i; j < i + channels; j++) { pixel.push(colors[j]) }; pixels.push(pixel) } // Convert pixels to frame let index = 0; for (let i=0; i < width; i++) { frame[i] = []; for (let j=0; j < height; j++) { frame[i][j] = pixels[index]; index += 1; } }; // Crop frame to piece of block size from left top to right bottom. const wBlockSize = 16; const hBlockSize = 16; const wBlockLength = Math.floor(width/wBlockSize); const hBlockLength = Math.floor(height/hBlockSize); for (let i=0; i < wBlockLength; i++) { for (let j=0; j < hBlockLength; j++) { const block = []; for (let k=i; k < i+wBlockSize; k++) { for (let l=j; l+hBlockSize; l++) { block.push(frame[k][l]) } }; frameBlocks.push(block) } }; const end_time = await benchmark(); const finish_time = end_time - start_time; console.log(finish_time); console.log(frameBlocks.length) }; encode(`${__dirname}/public/image.jpg`)