scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
scimesh::Image Struct Reference

A 2D RGBA image buffer. More...

#include <image.h>

Public Member Functions

 Image ()=default
 Construct an empty (0×0) image.
 
 Image (int w, int h)
 Construct an image of the given size, initialized to transparent black (0, 0, 0, 0).
 
void set_pixel (int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
 Set a single pixel's RGBA value.
 
void get_pixel (int x, int y, uint8_t &r, uint8_t &g, uint8_t &b, uint8_t &a) const
 Get a single pixel's RGBA value.
 
void clear (uint8_t r, uint8_t g, uint8_t b, uint8_t a)
 Fill the entire image with an RGBA color (byte values 0–255).
 
void clear_float (float r, float g, float b, float a)
 Fill the entire image with an RGBA color (float values 0.0–1.0).
 
Color sample_bilinear (float u, float v) const
 Sample the image at texture coordinates (u, v) using bilinear interpolation.
 
Image downsample_box (int factor) const
 Downsample the image by a factor using box filtering.
 
void crop (int x, int y, int w, int h)
 Crop the image to a sub-rectangle.
 
void merge (const Image &other, MergeDirection direction)
 Merge (concatenate) another image onto this one.
 
void grow (int top, int bottom, int left, int right, const Color &background)
 Grow (pad) the image by adding borders.
 
void rotate_90 (bool clockwise=true)
 Rotate the image by 90 degrees in-place.
 
void scale (int new_width, int new_height)
 Scale (resize) the image to new dimensions in-place.
 
void crop_to_content (CropContentDirection direction, const Color &background)
 Crop away uniform borders of a given background color.
 
void apply_contrast (float contrast)
 Apply a contrast adjustment to the image.
 
bool write_ppm (const std::string &filename) const
 Write the image as a PPM file (Portable Pixmap).
 
bool write_bmp (const std::string &filename) const
 Write the image as a BMP file (Windows Bitmap).
 
bool write_tga (const std::string &filename, bool use24bit=false) const
 Write the image as a TGA file (Truevision Targa).
 
bool write_png (const std::string &filename) const
 Write the image as a PNG file.
 
void pad_to_size (int target_w, int target_h, const Color &background)
 Pad the image to a target size, centering the content.
 

Static Public Member Functions

static Image read_image (const std::string &path)
 Read an image from a file (PNG, BMP, TGA, JPEG, etc.).
 

Public Attributes

int width = 0
 Image width in pixels.
 
int height = 0
 Image height in pixels.
 
std::vector< uint8_t > pixels
 Raw pixel data: RGBA bytes, row-major, bottom-left origin.
 

Detailed Description

A 2D RGBA image buffer.

Overview

The Image class stores pixels as 4 bytes per pixel (red, green, blue, alpha) in row-major order. Pixel (0, 0) is the bottom-left corner (matching OpenGL texture convention).

Images are used both as render targets (the output of the renderer) and as texture sources for textured meshes.

Construction

Image img(800, 600); // 800×600 blank (transparent black)
img.clear(1.0f, 1.0f, 1.0f, 1.0f); // fill with opaque white
A 2D RGBA image buffer.
Definition image.h:87

Saving

img.write_png("output.png"); // PNG (recommended)
img.write_ppm("output.ppm"); // PPM (simple text/binary format, for debugging)
img.write_bmp("output.bmp"); // BMP (for debugging)
img.write_tga("output.tga"); // TGA (uncompressed true-color, no dependencies)
See also
Mesh::texture, Renderer

Definition at line 87 of file image.h.

Constructor & Destructor Documentation

◆ Image() [1/2]

scimesh::Image::Image ( )
default

Construct an empty (0×0) image.

◆ Image() [2/2]

scimesh::Image::Image ( int  w,
int  h 
)

Construct an image of the given size, initialized to transparent black (0, 0, 0, 0).

Parameters
wWidth in pixels.
hHeight in pixels.

Definition at line 37 of file image.cpp.

Member Function Documentation

◆ apply_contrast()

void scimesh::Image::apply_contrast ( float  contrast)

Apply a contrast adjustment to the image.

A value of 1.0 means no change. Values > 1.0 increase contrast; values < 1.0 reduce it (0.0 = uniform gray).

Parameters
contrastContrast multiplier (1.0 = unchanged).
Example
img.apply_contrast(1.5f); // increase contrast

Definition at line 379 of file image.cpp.

◆ clear()

void scimesh::Image::clear ( uint8_t  r,
uint8_t  g,
uint8_t  b,
uint8_t  a 
)

Fill the entire image with an RGBA color (byte values 0–255).

Parameters
rRed channel.
gGreen channel.
bBlue channel.
aAlpha channel.

Definition at line 61 of file image.cpp.

◆ clear_float()

void scimesh::Image::clear_float ( float  r,
float  g,
float  b,
float  a 
)

Fill the entire image with an RGBA color (float values 0.0–1.0).

This is a convenience wrapper that converts floats to bytes internally.

Parameters
rRed channel (0.0–1.0).
gGreen channel (0.0–1.0).
bBlue channel (0.0–1.0).
aAlpha channel (0.0–1.0).

Definition at line 70 of file image.cpp.

◆ crop()

void scimesh::Image::crop ( int  x,
int  y,
int  w,
int  h 
)

Crop the image to a sub-rectangle.

The cropped region replaces the image in-place.

Parameters
xLeft edge of the crop region.
yBottom edge of the crop region.
wWidth of the crop region.
hHeight of the crop region.
Example
img.crop(100, 50, 400, 300); // keep a 400×300 region

Definition at line 110 of file image.cpp.

◆ crop_to_content()

void scimesh::Image::crop_to_content ( CropContentDirection  direction,
const Color background 
)

Crop away uniform borders of a given background color.

Removes rows/columns from the specified edges as long as every pixel in that row/column matches the background color (within tolerance).

Parameters
directionWhich edge(s) to crop.
backgroundThe color to treat as "empty" border.
Example
// removes all transparent borders from all edges
@ ALL
Crop all four edges.
constexpr Color TRANSPARENT_BLACK
A fully transparent black color (0, 0, 0, 0).
Definition types.h:150
See also
CropContentDirection

Definition at line 273 of file image.cpp.

◆ downsample_box()

Image scimesh::Image::downsample_box ( int  factor) const

Downsample the image by a factor using box filtering.

Reduces the image size. Each output pixel is the average of a factor × factor block of input pixels. The width and height must be divisible by factor.

Parameters
factorDownsampling factor (e.g., 2 halves both dimensions).
Returns
A new, smaller Image.
Example
Image small = big.downsample_box(4); // ¼ width, ¼ height
Image downsample_box(int factor) const
Downsample the image by a factor using box filtering.
Definition image.cpp:77

Definition at line 77 of file image.cpp.

◆ get_pixel()

void scimesh::Image::get_pixel ( int  x,
int  y,
uint8_t &  r,
uint8_t &  g,
uint8_t &  b,
uint8_t &  a 
) const

Get a single pixel's RGBA value.

Parameters
[in]xX coordinate (0 = left).
[in]yY coordinate (0 = bottom).
[out]rRed channel (0–255).
[out]gGreen channel (0–255).
[out]bBlue channel (0–255).
[out]aAlpha channel (0–255).

Definition at line 49 of file image.cpp.

◆ grow()

void scimesh::Image::grow ( int  top,
int  bottom,
int  left,
int  right,
const Color background 
)

Grow (pad) the image by adding borders.

Adds top, bottom, left, right rows/columns filled with the given background color.

Parameters
topPixels to add above the image.
bottomPixels to add below the image.
leftPixels to add left of the image.
rightPixels to add right of the image.
backgroundFill color for the new border.
Example
img.grow(10, 10, 20, 20, Color(1,1,1)); // add 10px top/bottom, 20px left/right
An RGBA color with floating-point components.
Definition types.h:88

Definition at line 197 of file image.cpp.

◆ merge()

void scimesh::Image::merge ( const Image other,
MergeDirection  direction 
)

Merge (concatenate) another image onto this one.

The two images must have compatible dimensions for the merge direction. For LEFT/RIGHT merges, both images must have the same height. For TOP/BOTTOM merges, both images must have the same width.

Parameters
otherThe image to attach.
directionWhich side to attach to.
Example
Image left(400, 300), right(400, 300);
left.merge(right, MergeDirection::RIGHT);
// left is now 800×300
@ RIGHT
Attach other to the right side.
See also
MergeDirection

Definition at line 143 of file image.cpp.

◆ pad_to_size()

void scimesh::Image::pad_to_size ( int  target_w,
int  target_h,
const Color background 
)

Pad the image to a target size, centering the content.

Adds borders filled with background so the image reaches target_w × target_h. Content stays pixel-perfect — no scaling. If the image is already at or larger than the target size, this is a no-op.

Parameters
target_wDesired width in pixels.
target_hDesired height in pixels.
backgroundFill color for the added borders.
Example
Image small(400, 300);
small.pad_to_size(800, 600, Color(1,1,1));
// small is now 800×600, original content centered
See also
grow(), scale()

Definition at line 547 of file image.cpp.

◆ read_image()

Image scimesh::Image::read_image ( const std::string &  path)
static

Read an image from a file (PNG, BMP, TGA, JPEG, etc.).

Uses stb_image internally, which auto-detects the format from the file header. Returns an empty (0×0) image on failure.

Parameters
pathPath to the image file.
Returns
The loaded Image, or empty Image on failure.
Example
Image img = Image::read_image("colorbar.png");
if (img.width == 0) { // handle error }
int width
Image width in pixels.
Definition image.h:89
static Image read_image(const std::string &path)
Read an image from a file (PNG, BMP, TGA, JPEG, etc.).
Definition image.cpp:533

Definition at line 533 of file image.cpp.

◆ rotate_90()

void scimesh::Image::rotate_90 ( bool  clockwise = true)

Rotate the image by 90 degrees in-place.

Parameters
clockwiseIf true (default), rotate clockwise. Otherwise, counter-clockwise.
Example
img.rotate_90(); // clockwise
img.rotate_90(false); // counter-clockwise

Definition at line 228 of file image.cpp.

◆ sample_bilinear()

Color scimesh::Image::sample_bilinear ( float  u,
float  v 
) const

Sample the image at texture coordinates (u, v) using bilinear interpolation.

Bilinear sampling blends the four nearest pixels, producing a smooth result when texture coordinates fall between pixel centers.

Parameters
uHorizontal texture coordinate (0.0–1.0, 0 = left).
vVertical texture coordinate (0.0–1.0, 0 = bottom).
Returns
The interpolated Color.
Example
Color c = img.sample_bilinear(0.5f, 0.5f); // center of texture

Definition at line 351 of file image.cpp.

◆ scale()

void scimesh::Image::scale ( int  new_width,
int  new_height 
)

Scale (resize) the image to new dimensions in-place.

Uses bilinear interpolation for smooth results.

Parameters
new_widthTarget width in pixels.
new_heightTarget height in pixels.
Example
img.scale(1600, 1200); // upscale to 1600×1200

Definition at line 251 of file image.cpp.

◆ set_pixel()

void scimesh::Image::set_pixel ( int  x,
int  y,
uint8_t  r,
uint8_t  g,
uint8_t  b,
uint8_t  a 
)

Set a single pixel's RGBA value.

Parameters
xX coordinate (0 = left).
yY coordinate (0 = bottom).
rRed channel (0–255).
gGreen channel (0–255).
bBlue channel (0–255).
aAlpha channel (0–255, 255 = fully opaque).

Definition at line 39 of file image.cpp.

◆ write_bmp()

bool scimesh::Image::write_bmp ( const std::string &  filename) const

Write the image as a BMP file (Windows Bitmap).

BMP is a simple, uncompressed format. No external libraries needed.

Parameters
filenameOutput file path (should end with .bmp).
Returns
true on success.
See also
write_png(), write_ppm()

Definition at line 405 of file image.cpp.

◆ write_png()

bool scimesh::Image::write_png ( const std::string &  filename) const

Write the image as a PNG file.

PNG is the recommended output format for publication-quality results. Uses stb_image_write internally.

Parameters
filenameOutput file path (should end with .png).
Returns
true on success.
Example
Image result = renderer.render_mesh(mesh, camera, opts);
result.write_png("rendering.png");
bool write_png(const std::string &filename) const
Write the image as a PNG file.
Definition image.cpp:521
See also
write_ppm(), write_bmp()

Definition at line 521 of file image.cpp.

◆ write_ppm()

bool scimesh::Image::write_ppm ( const std::string &  filename) const

Write the image as a PPM file (Portable Pixmap).

PPM is a simple, uncompressed format useful for debugging and testing. No external libraries are needed.

Parameters
filenameOutput file path (should end with .ppm).
Returns
true on success.
See also
write_png(), write_bmp()

Definition at line 392 of file image.cpp.

◆ write_tga()

bool scimesh::Image::write_tga ( const std::string &  filename,
bool  use24bit = false 
) const

Write the image as a TGA file (Truevision Targa).

Writes an uncompressed true-color TGA file (image type 2) using scimesh's own implementation (no external libraries). The header declares a top-left origin, matching the pixel layout produced by the renderer. Pixel data is stored BGR(A) per the TGA spec, so red and blue channels are swapped on write.

Parameters
filenameOutput file path (should end with .tga).
use24bitIf true, write 24-bit RGB (no alpha channel). Default false writes 32-bit RGBA.
Returns
true on success.
See also
write_png(), write_bmp()

Definition at line 471 of file image.cpp.

Member Data Documentation

◆ height

int scimesh::Image::height = 0

Image height in pixels.

Definition at line 92 of file image.h.

◆ pixels

std::vector<uint8_t> scimesh::Image::pixels

Raw pixel data: RGBA bytes, row-major, bottom-left origin.

Size is width * height * 4 bytes. Pixel at (x, y) is at offset (y * width + x) * 4.

Definition at line 98 of file image.h.

◆ width

int scimesh::Image::width = 0

Image width in pixels.

Definition at line 89 of file image.h.


The documentation for this struct was generated from the following files: