Pixels
Utility class for manipulating raw RGBA pixel data.
Pixels provides low-level operations for working with pixel buffers in RGBA format. Each pixel consists of 4 bytes: Red, Green, Blue, and Alpha channels (0-255 each). This class is useful for:
- Image processing and filtering
- Procedural texture generation
- Pixel-perfect collision detection
- Screenshot capture and export
- Dynamic texture creation
Buffer format:
- Pixels are stored in row-major order (left to right, top to bottom)
- Each pixel uses 4 consecutive bytes: [R, G, B, A]
- Buffer index calculation: (y * width + x) * 4
// Create a 100x100 red image
var pixels = Pixels.create(100, 100, AlphaColor.RED);
// Set a single pixel
Pixels.set(pixels, 100, 50, 50, AlphaColor.BLUE);
// Copy a region
Pixels.copy(srcPixels, srcWidth, dstPixels, dstWidth,
0, 0, 50, 50, // Source region
25, 25); // Destination position
// Export to PNG
Pixels.pixelsToPng(100, 100, pixels, "output.png", () -> {
trace("PNG saved!");
});
Static Members
copy(srcBuffer: UInt8Array, srcBufferWidth: Int, dstBuffer: UInt8Array, dstBufferWidth: Int, srcX: Int, srcY: Int, srcWidth: Int, srcHeight: Int, dstX: Int, dstY: Int, ?copyRed: Bool = true, ?copyGreen: Bool = true, ?copyBlue: Bool = true, ?copyAlpha: Bool = true): VoidCopies a rectangular region of pixels from one buffer to another.
This method performs a pixel-by-pixel copy with optional channel filtering. It's useful for:
- Compositing multiple images
- Creating texture atlases
- Implementing copy/paste functionality
- Selective channel manipulation
The copy operation respects buffer boundaries and will not read/write outside the valid pixel ranges.
| Name | Type | Default | Description |
|---|---|---|---|
srcBuffer |
UInt8Array | Source pixel buffer in RGBA format | |
srcBufferWidth |
Int | Width of the source image in pixels | |
dstBuffer |
UInt8Array | Destination pixel buffer in RGBA format | |
dstBufferWidth |
Int | Width of the destination image in pixels | |
srcX |
Int | Starting X coordinate in source buffer | |
srcY |
Int | Starting Y coordinate in source buffer | |
srcWidth |
Int | Width of the region to copy | |
srcHeight |
Int | Height of the region to copy | |
dstX |
Int | Target X coordinate in destination buffer | |
dstY |
Int | Target Y coordinate in destination buffer | |
copyRed |
Bool | true |
Whether to copy the red channel (default: true) |
copyGreen |
Bool | true |
Whether to copy the green channel (default: true) |
copyBlue |
Bool | true |
Whether to copy the blue channel (default: true) |
copyAlpha |
Bool | true |
Whether to copy the alpha channel (default: true) * haxe // Copy entire image Pixels.copy(src, 100, dst, 200, 0, 0, 100, 100, 50, 50); * // Copy only RGB channels (preserve destination alpha) Pixels.copy(src, 100, dst, 200, 0, 0, 50, 50, 0, 0, true, true, true, false); |
create(width: Int, height: Int, fillColor: AlphaColor): UInt8ArrayCreates a new pixel buffer filled with the specified color.
Allocates a UInt8Array of size width × height × 4 bytes. All pixels are initialized to the same color value.
| Name | Type | Description |
|---|---|---|
width |
Int | Width of the image in pixels |
height |
Int | Height of the image in pixels |
fillColor |
AlphaColor | Initial color for all pixels (including alpha) |
| Returns | Description |
|---|---|
| UInt8Array | New pixel buffer in RGBA format * haxe // Create transparent image var pixels = Pixels.create(256, 256, AlphaColor.TRANSPARENT); * // Create opaque white background var bg = Pixels.create(800, 600, AlphaColor.WHITE); |
fromBytes(bytes: haxe.io.Bytes): UInt8ArrayCreates a pixel buffer from raw bytes in RGBA format.
Converts Haxe Bytes to a platform-specific UInt8Array. The bytes must already be in RGBA format with 4 bytes per pixel.
| Name | Type | Description |
|---|---|---|
bytes |
haxe.io.Bytes | Raw bytes containing RGBA pixel data |
| Returns | Description |
|---|---|
| UInt8Array | Pixel buffer suitable for use with other Pixels methods * haxe var bytes = File.getBytes("raw_image.data"); var pixels = Pixels.fromBytes(bytes); |
get(buffer: UInt8Array, bufferWidth: Int, x: Int, y: Int): AlphaColorGets a single pixel color at the specified coordinates.
Reads 4 bytes from the buffer and returns them as an AlphaColor. No bounds checking is performed for performance reasons.
| Name | Type | Description |
|---|---|---|
buffer |
UInt8Array | Pixel buffer to read from |
bufferWidth |
Int | Width of the image in pixels |
x |
Int | X coordinate (0 to width-1) |
y |
Int | Y coordinate (0 to height-1) |
| Returns | Description |
|---|---|
| AlphaColor | Color value at the specified position * haxe var color = Pixels.get(buffer, 100, 50, 25); trace('Pixel alpha: ' + color.alpha); |
set(buffer: UInt8Array, bufferWidth: Int, x: Int, y: Int, color: AlphaColor): VoidSets a single pixel color at the specified coordinates.
Writes 4 bytes to the buffer from the AlphaColor components. No bounds checking is performed for performance reasons.
| Name | Type | Description |
|---|---|---|
buffer |
UInt8Array | Pixel buffer to write to |
bufferWidth |
Int | Width of the image in pixels |
x |
Int | X coordinate (0 to width-1) |
y |
Int | Y coordinate (0 to height-1) |
color |
AlphaColor | Color value to set (including alpha) * haxe // Draw a red pixel Pixels.set(buffer, 100, 50, 25, AlphaColor.RED); * // Set semi-transparent green var color = AlphaColor.fromRGBA(0, 255, 0, 128); Pixels.set(buffer, 100, 10, 10, color); |
setRectangle(buffer: UInt8Array, bufferWidth: Int, x: Int, y: Int, width: Int, height: Int, color: AlphaColor): VoidFills a rectangular area with a solid color.
Sets all pixels within the specified rectangle to the same color. This is more efficient than setting pixels individually in a loop. No bounds checking is performed.
| Name | Type | Description |
|---|---|---|
buffer |
UInt8Array | Pixel buffer to write to |
bufferWidth |
Int | Width of the image in pixels |
x |
Int | Left edge of rectangle (0 to width-1) |
y |
Int | Top edge of rectangle (0 to height-1) |
width |
Int | Width of rectangle in pixels |
height |
Int | Height of rectangle in pixels |
color |
AlphaColor | Color to fill the rectangle with (including alpha) * haxe // Draw a blue square Pixels.setRectangle(buffer, 200, 50, 50, 100, 100, AlphaColor.BLUE); * // Clear a region to transparent Pixels.setRectangle(buffer, 200, 0, 0, 200, 50, AlphaColor.TRANSPARENT); |
pixelsToPng(width: Int, height: Int, pixels: UInt8Array, path: String, done: Function): VoidExports pixel data as a PNG file to the specified path.
Encodes the raw RGBA pixel buffer as PNG format and saves it to disk. The operation is asynchronous and calls the callback when complete.
| Name | Type | Description |
|---|---|---|
width |
Int | Width of the image in pixels |
height |
Int | Height of the image in pixels |
pixels |
UInt8Array | RGBA pixel buffer to encode |
path |
String | File path where to save the PNG (e.g., "/path/to/image.png") |
done |
Function | Callback invoked when the export is complete * haxe var screenshot = Pixels.create(800, 600, AlphaColor.BLACK); // ... draw to screenshot ... Pixels.pixelsToPng(800, 600, screenshot, "screenshot.png", () -> { trace("Screenshot saved!"); }); |
rgbaPixelsToRgbPixels(width: Int, height: Int, inPixels: UInt8Array, ?outPixels: Null<UInt8Array>): UInt8ArrayConverts RGBA pixel data to RGB format by stripping alpha channel.
Creates a new buffer with 3 bytes per pixel instead of 4. Useful for formats that don't support transparency or to reduce memory usage.
| Name | Type | Default | Description |
|---|---|---|---|
width |
Int | Width of the image in pixels | |
height |
Int | Height of the image in pixels | |
inPixels |
UInt8Array | Source RGBA pixel buffer (4 bytes per pixel) | |
outPixels |
Null<UInt8Array> | (optional) | Optional destination buffer to reuse. Must be exactly width × height × 3 bytes. If null or wrong size, a new buffer is created. |
| Returns | Description |
|---|---|
| UInt8Array | RGB pixel buffer (3 bytes per pixel) * haxe // Convert for JPEG encoding (no alpha support) var rgbPixels = Pixels.rgbaPixelsToRgbPixels(100, 100, rgbaPixels); |
rgbPixelsToRgbaPixels(width: Int, height: Int, ?alpha: Int = 255, inPixels: UInt8Array, ?outPixels: Null<UInt8Array>): UInt8ArrayConverts RGB pixel data to RGBA format by adding an alpha channel.
Expands the buffer from 3 bytes per pixel to 4 bytes per pixel. All pixels receive the same alpha value.
| Name | Type | Default | Description |
|---|---|---|---|
width |
Int | Width of the image in pixels | |
height |
Int | Height of the image in pixels | |
alpha |
Int | 255 |
Alpha value to add to all pixels (0-255, default: 255 for opaque) |
inPixels |
UInt8Array | Source RGB pixel buffer (3 bytes per pixel) | |
outPixels |
Null<UInt8Array> | (optional) | Optional destination buffer to reuse. Must be exactly width × height × 4 bytes. If null or wrong size, a new buffer is created. |
| Returns | Description |
|---|---|
| UInt8Array | RGBA pixel buffer (4 bytes per pixel) * haxe // Convert RGB to opaque RGBA var rgbaPixels = Pixels.rgbPixelsToRgbaPixels(100, 100, 255, rgbPixels); * // Convert with 50% transparency var semiTransparent = Pixels.rgbPixelsToRgbaPixels(100, 100, 128, rgbPixels); |
mixPixelsBuffers(inPixelsList: Array<UInt8Array>, ?middleFactor: Float = 1, ?outPixels: Null<UInt8Array>): UInt8ArrayBlends multiple pixel buffers into a single weighted average.
Combines multiple images using weighted averaging, with optional emphasis on middle buffers. This is useful for:
- Creating smooth transitions between frames
- Temporal anti-aliasing
- Motion blur effects
- Image stacking for noise reduction
The weight distribution forms a pyramid shape when middleFactor > 1:
- First and last buffers have weight 1
- Middle buffers have weight multiplied by middleFactor
| Name | Type | Default | Description |
|---|---|---|---|
inPixelsList |
Array<UInt8Array> | Array of pixel buffers to mix. All must have same dimensions. | |
middleFactor |
Float | 1 |
Weight multiplier for middle buffers. - 1.0: Equal weighting for all buffers - >1.0: Emphasize middle buffers - <1.0: Emphasize edge buffers |
outPixels |
Null<UInt8Array> | (optional) | Optional destination buffer to reuse. Must match size of input buffers. |
| Returns | Description |
|---|---|
| UInt8Array | Mixed pixel buffer with weighted average of inputs * |
flipY(buffer: UInt8Array, bufferWidth: Int): VoidFlips an image vertically (upside down) in-place.
Swaps pixel rows from top to bottom. The top row becomes the bottom row, the second row becomes the second-to-last row, etc.
This operation modifies the buffer directly without allocating new memory.
| Name | Type | Description |
|---|---|---|
buffer |
UInt8Array | Pixel buffer to flip. Modified in-place. |
bufferWidth |
Int | Width of the image in pixels. Height is calculated from buffer.length / (width × 4). * haxe // Flip image loaded from file (often needed for OpenGL) var pixels = loadImagePixels("texture.png"); Pixels.flipY(pixels, 256); |
flipX(buffer: UInt8Array, bufferWidth: Int): VoidFlips an image horizontally (mirror) in-place.
Swaps pixel columns from left to right. The leftmost column becomes the rightmost column, etc.
This operation modifies the buffer directly without allocating new memory.
| Name | Type | Description |
|---|---|---|
buffer |
UInt8Array | Pixel buffer to flip. Modified in-place. |
bufferWidth |
Int | Width of the image in pixels. Height is calculated from buffer.length / (width × 4). * haxe // Create mirror image var pixels = loadImagePixels("character.png"); Pixels.flipX(pixels, 64); // Character now faces opposite direction |