paul_bitmap.h
IntroductionBitmap manipulation library for C/C++. DiscussionImplementation is included when PAUL_BITMAP_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.
Functions
bitmap_clipClips a rectangular region from an image in-place. bool bitmap_clip( bitmap_t *src, int rx, int ry, int rw, int rh); ParametersReturn Valuetrue if successful, false if region is invalid or image is invalid. DiscussionExtracts a rectangular region from the image, making it the new image content. The original image is replaced with just the clipped region. bitmap_clipped(bitmap_t, int, int, int, int)Creates a new image from a rectangular region of another image. bitmap_t bitmap_clipped( bitmap_t src, int rx, int ry, int rw, int rh); ParametersReturn ValueA new image containing the clipped region, or NULL on allocation failure. DiscussionAllocates a new image containing only the specified rectangular region from the source image. The original image remains unchanged. bitmap_clipped(bitmap_t, int, int, int, int)Creates a new image from a rectangular region of another image. bitmap_t bitmap_clipped( bitmap_t src, int rx, int ry, int rw, int rh); ParametersReturn ValueA new image containing the clipped region, or NULL on allocation failure. DiscussionAllocates a new image containing only the specified rectangular region from the source image. The original image remains unchanged. bitmap_clipped_pastePastes a rectangular region from one image to another. bool bitmap_clipped_paste( bitmap_t dst, const bitmap_t src, int x, int y, int rx, int ry, int rw, int rh); Parameters
Return Valuetrue if successful, false if images are invalid or operation would exceed bounds. DiscussionCopies a rectangular region (rx, ry, rw, rh) from the source image onto the destination image at position (x, y). Only the specified region of the source is copied. bitmap_destroyFrees the memory associated with an image. void bitmap_destroy( bitmap_t img); ParametersDiscussionReleases all memory allocated for the image. The image pointer becomes invalid after this call and should not be used. bitmap_dominant_colorFinds the most frequently occurring color in the image. color_t bitmap_dominant_color( const bitmap_t img); ParametersReturn ValueThe dominant color in the image. DiscussionAnalyzes all pixels in the image and returns the color that appears most often. Useful for generating color palettes or determining the primary color of an image. bitmap_draw_circleDraws a circle or filled circle. bool bitmap_draw_circle( bitmap_t img, int xc, int yc, int r, color_t color, int fill); ParametersReturn Valuetrue if successful, false if the image is invalid. DiscussionDraws a circle centered at (xc, yc) with radius r. If fill is non-zero, the circle is filled; otherwise only the outline is drawn. bitmap_draw_lineDraws a line between two points. bool bitmap_draw_line( bitmap_t img, int x0, int y0, int x1, int y1, color_t color); ParametersReturn Valuetrue if successful, false if the image is invalid. DiscussionDraws a straight line from (x0, y0) to (x1, y1) using the specified color. Uses Bresenham's line algorithm for efficient rasterization. bitmap_draw_rectangleDraws a rectangle or filled rectangle. bool bitmap_draw_rectangle( bitmap_t img, int x, int y, int w, int h, color_t color, int fill); ParametersReturn Valuetrue if successful, false if the image is invalid. DiscussionDraws a rectangle with top-left corner at (x, y) and dimensions w by h. If fill is non-zero, the rectangle is filled; otherwise only the outline is drawn. bitmap_draw_triangleDraws a triangle or filled triangle. bool bitmap_draw_triangle( bitmap_t img, int x0, int y0, int x1, int y1, int x2, int y2, color_t color, int fill); Parameters
Return Valuetrue if successful, false if the image is invalid. DiscussionDraws a triangle defined by three vertices (x0,y0), (x1,y1), (x2,y2). If fill is non-zero, the triangle is filled; otherwise only the outline is drawn. bitmap_dupeCreates a duplicate copy of an image. bitmap_t bitmap_dupe( bitmap_t src); ParametersReturn ValueA new image that is a copy of the source, or NULL on allocation failure. DiscussionAllocates a new image with the same dimensions and pixel data as the source image. The returned image is independent and must be freed with bitmap_destroy() when no longer needed. bitmap_emptyCreates a new empty image filled with a specified color. bitmap_t bitmap_empty( unsigned int w, unsigned int h, color_t color); ParametersReturn ValueA pointer to the new image, or NULL on allocation failure. DiscussionAllocates memory for a new image of the specified width and height, initializing all pixels to the given color. The returned image must be freed with bitmap_destroy() when no longer needed. bitmap_fillFills the entire image with a solid color. bool bitmap_fill( bitmap_t img, color_t color); ParametersReturn Valuetrue if successful, false if the image is invalid. DiscussionSets all pixels in the image to the specified color, effectively clearing the image to a uniform color. bitmap_flippedCreates a flipped copy of an image. bitmap_t bitmap_flipped( bitmap_t src, int horizontal, int vertical); ParametersReturn ValueA new flipped image, or NULL on allocation failure. DiscussionAllocates a new image that is a horizontally and/or vertically flipped version of the source image. The original image remains unchanged. bitmap_floodPerforms a flood fill starting from the specified coordinates. bool bitmap_flood( bitmap_t img, int x, int y, color_t color); ParametersReturn Valuetrue if successful, false if coordinates are out of bounds or image is invalid. DiscussionFills a contiguous region of pixels with the same color as the starting pixel with a new color. Uses 4-way connectivity (up, down, left, right). bitmap_heightGets the height of an image in pixels. int bitmap_height( const bitmap_t img); ParametersReturn ValueThe height of the image in pixels. DiscussionReturns the height dimension of the image. bitmap_histogramGenerates a histogram of color frequencies in the image. int* bitmap_histogram( const bitmap_t img); ParametersReturn ValueAn array of integers representing color frequencies, or NULL on failure. DiscussionCreates an array representing the frequency distribution of colors in the image. The returned array must be freed by the caller when no longer needed. bitmap_loadCreates an image from raw pixel data in the specified format. bitmap_t bitmap_load( const void *data, int width, int height, bitmap_format_t format); ParametersReturn ValueA new image created from the pixel data, or NULL on failure. DiscussionLoads pixel data from a raw byte array and creates a new image. The data must be in the specified format and contain width * height * bytes_per_pixel bytes. The returned image must be freed with bitmap_destroy() when no longer needed. bitmap_paletteExtracts a color palette from the image. color_t* bitmap_palette( const bitmap_t img, int count); ParametersReturn ValueAn array of colors representing the extracted palette, or NULL on failure. DiscussionAnalyzes the image and extracts the specified number of most representative colors. The returned array contains the palette colors and must be freed by the caller. bitmap_pastePastes one image onto another at the specified position. bool bitmap_paste( bitmap_t dst, const bitmap_t src, int x, int y); ParametersReturn Valuetrue if successful, false if images are invalid or operation would exceed bounds. DiscussionCopies all pixels from the source image onto the destination image starting at position (x, y). The source image is pasted over the destination without any alpha blending. bitmap_pgetGets the color of a pixel at the specified coordinates. color_t bitmap_pget( const bitmap_t img, int x, int y); ParametersReturn ValueThe color of the pixel, or a default color if coordinates are out of bounds. DiscussionRetrieves the color of the pixel at position (x, y). Coordinates are zero-based, with (0,0) being the top-left corner. If coordinates are out of bounds, returns a default color. bitmap_psetSets the color of a pixel at the specified coordinates. bool bitmap_pset( bitmap_t img, int x, int y, color_t color); ParametersReturn Valuetrue if successful, false if coordinates are out of bounds or image is invalid. DiscussionChanges the color of the pixel at position (x, y). Coordinates are zero-based, with (0,0) being the top-left corner. If coordinates are out of bounds, the operation fails. bitmap_resizeResizes an image in-place to new dimensions. bool bitmap_resize( bitmap_t *src, int nw, int nh); ParametersReturn Valuetrue if successful, false if allocation fails or image is invalid. DiscussionChanges the size of the image to the specified width and height. The original image data is lost and replaced with resized content. Uses nearest-neighbor interpolation. bitmap_resizedCreates a resized copy of an image. bitmap_t bitmap_resized( bitmap_t src, int nw, int nh); ParametersReturn ValueA new resized image, or NULL on allocation failure. DiscussionAllocates a new image with the specified dimensions and copies the source image content resized to fit. Uses nearest-neighbor interpolation. The original image remains unchanged. bitmap_rotateRotates an image in-place by the specified angle. bool bitmap_rotate( bitmap_t *src, float angle); ParametersReturn Valuetrue if successful, false if allocation fails or image is invalid. DiscussionRotates the image around its center by the given angle in degrees. Positive angles rotate clockwise. The image dimensions may change to accommodate the rotated content. bitmap_rotatedCreates a rotated copy of an image. bitmap_t bitmap_rotated( bitmap_t src, float angle); ParametersReturn ValueA new rotated image, or NULL on allocation failure. DiscussionAllocates a new image and copies the source image content rotated by the specified angle. The original image remains unchanged. bitmap_sizeGets both width and height of an image. bool bitmap_size( const bitmap_t img, int *w, int *h); ParametersReturn Valuetrue if successful, false if the image is invalid. DiscussionRetrieves the dimensions of the image, storing them in the provided pointers. This is more efficient than calling bitmap_width() and bitmap_height() separately. bitmap_widthGets the width of an image in pixels. int bitmap_width( const bitmap_t img); ParametersReturn ValueThe width of the image in pixels. DiscussionReturns the width dimension of the image. Typedefs
bitmap_format_tEnumeration of supported pixel data formats for image loading. typedef enum { BITMAP_FORMAT_RGBA, // 4 bytes per pixel: R, G, B, A BITMAP_FORMAT_RGB, // 3 bytes per pixel: R, G, B (alpha = 255) BITMAP_FORMAT_BGRA, // 4 bytes per pixel: B, G, R, A BITMAP_FORMAT_BGR, // 3 bytes per pixel: B, G, R (alpha = 255) BITMAP_FORMAT_ARGB, // 4 bytes per pixel: A, R, G, B BITMAP_FORMAT_ABGR, // 4 bytes per pixel: A, B, G, R BITMAP_FORMAT_GRAY, // 1 byte per pixel: grayscale (alpha = 255) BITMAP_FORMAT_GRAY_ALPHA, // 2 bytes per pixel: grayscale, alpha BITMAP_FORMAT_RGB565, // 2 bytes per pixel: 5-bit R, 6-bit G, 5-bit B (alpha = 255) BITMAP_FORMAT_RGB555, // 2 bytes per pixel: 5-bit R, 5-bit G, 5-bit B (alpha = 255) BITMAP_FORMAT_ARGB1555 // 2 bytes per pixel: 1-bit A, 5-bit R, 5-bit G, 5-bit B } bitmap_format_t; Fields
DiscussionDefines the various pixel formats that can be used when loading image data from raw pixel arrays. Each format specifies the byte layout and channel count of the pixel data. bitmap_tRepresents an RGBA color with 8 bits per channel. typedef color_t* bitmap_t; color_tRepresents a color with red, green, blue, and alpha components. typedef union color_t { struct { uint8_t r, g, b, a; }; uint32_t rgba; } color_t; Fields |