MCPcopy Create free account
hub / github.com/32comic/image2pixel-web / extractBlockGridAverage

Function extractBlockGridAverage

web/js/algorithms.js:182–205  ·  view source on GitHub ↗
(imageData, blockSize)

Source from the content-addressed store, hash-verified

180// ============================================================
181
182function extractBlockGridAverage(imageData, blockSize) {
183 const { width, height } = imageData;
184 const d = imageData.data;
185 const cols = Math.ceil(width / blockSize);
186 const rows = Math.ceil(height / blockSize);
187 const grid = [];
188
189 for (let row = 0; row < rows; row++) {
190 grid[row] = [];
191 for (let col = 0; col < cols; col++) {
192 const x0 = col * blockSize, y0 = row * blockSize;
193 let r = 0, g = 0, b = 0, count = 0;
194 for (let dy = 0; dy < blockSize && y0 + dy < height; dy++) {
195 for (let dx = 0; dx < blockSize && x0 + dx < width; dx++) {
196 const idx = ((y0 + dy) * width + (x0 + dx)) * 4;
197 r += d[idx]; g += d[idx + 1]; b += d[idx + 2];
198 count++;
199 }
200 }
201 grid[row][col] = [Math.round(r / count), Math.round(g / count), Math.round(b / count)];
202 }
203 }
204 return { grid, cols, rows };
205}
206
207function extractBlockGridNearest(imageData, blockSize) {
208 const { width, height } = imageData;

Callers 2

processWithClassicLabFunction · 0.85
processWithLabDitherFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected