libtcoddocumentation

5.2. Reading data from a TCODImage

Getting the size of an image

You can read the size of an image in pixels with this function.

void TCODImage::getSize(int *w,int *h) const

void TCOD_image_get_size(TCOD_image_t image, int *w,int *h)

image_get_size(image)

void TCODImage::getSize(out int w, out int h)

ParameterDescription
imageIn the C version, the image handler, obtained with the load function.
w,hWhen the function returns, those variables contain the size of the image.
Example:

TCODImage *pix = new TCODImage(80,50);
int
w,h;
pix->getSize(&w,&h);

TCOD_image_t pix = TCOD_image_new(80,50);
int
w,h;
TCOD_image_get_size(pix,&w,&h);

pix = libtcod.image_new(80,50)
w,h=libtcod.image_get_size(pix)


Getting the color of a pixel

You can read the colors from an image with this function.

TCODColor TCODImage::getPixel(int x, int y) const

TCOD_color_t TCOD_image_get_pixel(TCOD_image_t image,int x, int y)

image_get_pixel(image, x, y)

TCODColor TCODImage::getPixel(int x, int y)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height
Example:

TCODImage *pix = new TCODImage(80,50);
TCODColor col=pix->getPixel(40,25);

TCOD_image_t pix = TCOD_image_new(80,50);
TCOD_color_t
col=TCOD_image_get_pixel(pix,40,25);

pix = litbcod.image_new(80,50)
col=litbcod.image_get_pixel(pix,40,25)


Getting the alpha value of a pixel

If you have set a key color for this image with setKeyColor, or if this image was created from a 32 bits PNG file (with alpha layer), you can get the pixel transparency with this function. This function returns a value between 0 (transparent pixel) and 255 (opaque pixel).

int TCODImage::getAlpha(int x, int y) const

int TCOD_image_get_alpha(TCOD_image_t image, int x, int y)

image_get_alpha(image, x, y)

int TCODImage::getAlpha(int x, int y)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height

Checking if a pixel is transparent

You can use this simpler version (for images with alpha layer, returns true only if alpha == 0) :

bool TCODImage::isPixelTransparent(int x,int y) const

bool TCOD_image_is_pixel_transparent(TCOD_image_t image,int x, int y)

image_is_pixel_transparent(image, x, y)

bool TCODImage::isPixelTransparent(int x,int y)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
x,yThe pixel coordinates inside the image.
0 <= x < width
0 <= y < height

Getting the average color of a part of the image

This method uses mipmaps to get the average color of an arbitrary rectangular region of the image.
It can be used to draw a scaled-down version of the image. It's used by libtcod's blitting functions.

TCODColor TCODImage::getMipmapPixel(float x0,float y0, float x1, float y1)

TCOD_color_t TCOD_image_get_mipmap_pixel(TCOD_image_t image,float x0,float y0, float x1, float y1)

image_get_mipmap_pixel(image,x0,y0, x1, y1)

TCODColor TCODImage::getMipmapPixel(float x0,float y0, float x1, float y1)

ParameterDescription
imageIn the C version, the image handler, obtained with the load function.
x0,y0Coordinates in pixels of the upper-left corner of the region.
0.0 <= x0 < x1
0.0 <= y0 < y1
x1,y1Coordinates in pixels of the lower-right corner of the region.
x0 < x1 < width
y0 < y1 < height
Example:

// Get the average color of a 5x5 "superpixel" in the center of the image.
TCODImage *pix = new TCODImage(80,50);
TCODColor col=pix->getMipMapPixel(37.5f, 22.5f, 42.5f, 28.5f);

TCOD_image_t pix = TCOD_image_new(80,50);
TCOD_color_t
col=TCOD_image_get_mipmap_pixel(pix,37.5f, 22.5f, 42.5f, 28.5f);

pix = libtcod.image_new(80,50)
col=libtcod.image_get_mipmap_pixel(pix,37.5, 22.5, 42.5, 28.5)