(imageData, blockSize)
| 180 | // ============================================================ |
| 181 | |
| 182 | function 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 | |
| 207 | function extractBlockGridNearest(imageData, blockSize) { |
| 208 | const { width, height } = imageData; |
no outgoing calls
no test coverage detected