imagedata
imagedata
¶
CPU pixel buffers you can read, write and build by hand, then upload.
An ImageData holds tightly packed RGBA8 pixels (four bytes per pixel, row
by row). You can make one blank, filled with a color, or loaded from a file,
read and write single pixels as a Color, run a proc over every pixel, save
it to PNG, and upload it to a drawable image. The byte order lines up with
SDL_PIXELFORMAT_RGBA32 and the GPU's RGBA8 textures, so the common path
needs no conversion. This assumes a little-endian host, which covers the
platforms nim2d targets.
ImageData
¶
type ImageData = ref object
A CPU pixel buffer in RGBA8 you can read and write one pixel at a time,
then upload to a drawable image.
Fields
-
pixelsseq[uint8] -
widthint32 -
heightint32
newImageData ¶
proc newImageData(width, height: int32): ImageData
Create a blank (fully transparent) ImageData of the given size.
Parameters
-
width(int32) -
height(int32)
Returns
ImageData
newImageData ¶
proc newImageData(width, height: int32; fill: Color): ImageData
Create an ImageData filled with a single color.
Parameters
-
width(int32) -
height(int32) -
fill(Color)
Returns
ImageData
newImageData ¶
proc newImageData(filename: string): ImageData
Load an image file into a CPU pixel buffer, converting to RGBA8 if needed.
Parameters
-
filename(string)
Returns
ImageData
getWidth ¶
proc getWidth(d: ImageData): int32
The width of the pixel buffer.
Parameters
-
d(ImageData)
Returns
int32
getHeight ¶
proc getHeight(d: ImageData): int32
The height of the pixel buffer.
Parameters
-
d(ImageData)
Returns
int32
getDimensions ¶
proc getDimensions(d: ImageData): tuple[w, h: int32]
The width and height of the pixel buffer.
Parameters
-
d(ImageData)
Returns
tuple[w, h: int32]
getPixel ¶
proc getPixel(d: ImageData; x, y: int32): Color
The color at (x, y). Raises IndexDefect when out of range.
Parameters
-
d(ImageData) -
x(int32) -
y(int32)
Returns
Color
setPixel ¶
proc setPixel(d: ImageData; x, y: int32; color: Color)
Set the color at (x, y). Raises IndexDefect when out of range.
Parameters
-
d(ImageData) -
x(int32) -
y(int32) -
color(Color)
mapPixel ¶
proc mapPixel(d: ImageData; fn: proc (x, y: int32; c: Color): Color)
Apply fn to every pixel, replacing each with the color it returns.
Parameters
-
d(ImageData) -
fn(proc (x, y: int32; c: Color): Color)
encode ¶
proc encode(d: ImageData; filename: string)
Save the pixel buffer to a PNG file. Raises IOError on failure.
Parameters
-
d(ImageData) -
filename(string)
save ¶
proc save(d: ImageData; filename: string)
Alias for encode: save the pixel buffer to a PNG file.
Parameters
-
d(ImageData) -
filename(string)
newImage ¶
proc newImage(nim2d: Nim2d; data: ImageData; mipmaps = false): Image
Upload a CPU pixel buffer to a drawable GPU image.
Parameters
-
nim2d(Nim2d) -
data(ImageData) -
mipmaps(auto)
Returns
Image