Pixel Utility Functions

0.9.0 · active · verified Sun Apr 19

pixel-utils is a JavaScript/TypeScript library providing minimalist utility functions for pixel manipulation. As of version 0.9.0, it offers conversions between raw, RGB, and RGBA pixel formats, functions for handling 'no data' values, pixel iteration, selection, and transparency adjustments. The library is optimized for speed and supports optional mutability for memory-preserving operations. A key differentiator is its ability to handle raw image data (not limited to 8-bit 0-255 RGBA) and arbitrary image layouts, thanks to integration with the xdim library. While actively developed with frequent minor updates, the project maintains a 'beta' status, indicating that function signatures are subject to change without strict adherence to semantic versioning for breaking changes. Core assumptions involve pixel formats as arrays of numbers (0-255 for RGB/RGBA, arbitrary for raw) and specific conventions for alpha and 'no data' values.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting raw, unscaled pixel data into RGBA format and then iterating through the resulting RGBA pixel array.

import { rawToRgba, eachPixel } from 'pixel-utils';

// Example raw pixel data (e.g., from a sensor, could be any number)
const rawImageBuffer = [
  1000, 2000, 3000, // Pixel 1 data
  4000, 5000, 6000, // Pixel 2 data
  7000, 8000, 9000  // Pixel 3 data
];

// Define image dimensions and no-data value for raw conversion
const width = 3;
const height = 1;
const bands = 3;
const noDataValue = 0; // Assuming 0 for simplicity, actual would depend on sensor

// Convert raw data to RGBA (0-255 range)
// This example assumes a simple scaling for demonstration.
// In a real scenario, you'd likely map sensor values to a visual range.
const rgbaPixels = rawToRgba({
  data: rawImageBuffer,
  width,
  height,
  bands,
  noData: noDataValue,
  scale: (value: number) => Math.min(255, Math.max(0, value / 40))
});

console.log('Converted RGBA Pixels (first 4 values):', rgbaPixels.data.slice(0, 4));

// Iterate over the RGBA pixels
let pixelCount = 0;
eachPixel({
  data: rgbaPixels.data,
  width: rgbaPixels.width,
  height: rgbaPixels.height,
  bands: 4 // RGBA has 4 bands
}, (pixel: number[], x: number, y: number) => {
  console.log(`Pixel at (${x}, ${y}): [${pixel.join(', ')}]`);
  pixelCount++;
});

console.log(`Processed ${pixelCount} pixels.`);

view raw JSON →