Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
pixel_mode.c

Understanding pixel formats and RGB packing.

Understanding pixel formats and RGB packing.Pixel formats define how RGB color values are packed into integers. Different formats trade off memory usage vs color precision. This example demonstrates how the same RGB values produce different packed integers depending on the pixel format.

#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/pixel.h>
void display_value(bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue) {
// bj_get_pixel_value() converts 8-bit RGB components (0-255) into a packed
// integer value according to the specified pixel format. The packing order
// and bit depth vary by format.
const uint32_t val = bj_get_pixel_value(mode, red, green, blue);
// Convert the packed value to binary string for visualization of bit layout.
char binarystr[33] = {0};
for(size_t b = 0 ; b < 32 ; ++b) {
binarystr[31-b] = '0' + ((val >> b) & 0x01);
}
bj_info("R:%d, G:%d, B:%d -[0x%08x]--> %ld\t0x%08x\t0b%s",
red, green, blue, mode, 0, val, binarystr
);
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
// RGB565: 16-bit format with 5 bits red, 6 bits green, 5 bits blue.
// Uses less memory (2 bytes per pixel) but lower color precision.
// Green gets 6 bits because human eyes are most sensitive to green.
// XRGB1555: 16-bit format with 1 unused bit, then 5 bits each for RGB.
// Also 2 bytes per pixel but with equal precision across all channels.
// XRGB8888: 32-bit format with 8 bits per channel (full precision).
// Uses 4 bytes per pixel but retains all 8 bits of input color data.
// The 'X' byte is unused padding for alignment.
return 0;
}
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:105
uint32_t bj_get_pixel_value(enum bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel color, given its RGB composition.
bj_pixel_mode
Representation of a pixel encoding.
Definition pixel.h:16
@ BJ_PIXEL_MODE_RGB565
16bpp 565-RGB
Definition pixel.h:23
@ BJ_PIXEL_MODE_XRGB8888
32bpp RGB
Definition pixel.h:24
@ BJ_PIXEL_MODE_XRGB1555
16bpp 555-RGB
Definition pixel.h:22
Logging utility functions.
Portable main substitution and application callback facilities.
Header file for general pixel manipulation facilities.
void display_value(bj_pixel_mode mode, uint8_t red, uint8_t green, uint8_t blue)
Definition pixel_mode.c:14