scimesh 0.3.4
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <scimesh/types.h>
12#include <vector>
13#include <cstdint>
14#include <string>
15
16namespace scimesh {
17
18// ---------------------------------------------------------------------------
19// Enums for image operations
20// ---------------------------------------------------------------------------
21
26enum class MergeDirection {
27 LEFT,
28 RIGHT,
29 TOP,
30 BOTTOM
31};
32
39 LEFT,
40 RIGHT,
42 TOP,
43 BOTTOM,
44 VERTICAL,
45 ALL
46};
47
51enum class FitMode {
52 PAD,
53 SCALE
54};
55
56// ---------------------------------------------------------------------------
57// Image
58// ---------------------------------------------------------------------------
59
92struct Image {
94 int width = 0;
95
97 int height = 0;
98
103 std::vector<uint8_t> pixels;
104
106 Image() = default;
107
112 Image(int w, int h);
113
121 void set_pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
122
130 void get_pixel(int x, int y, uint8_t &r, uint8_t &g, uint8_t &b, uint8_t &a) const;
131
137 void clear(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
138
147 void clear_float(float r, float g, float b, float a);
148
170 Color sample_bilinear(float u, float v) const;
171
185 Image downsample_box(int factor) const;
186
200 void crop(int x, int y, int w, int h);
201
220
236 void grow(int top, int bottom, int left, int right, const Color &background);
237
248 void rotate_90(bool clockwise = true);
249
261 void scale(int new_width, int new_height);
262
279
291 void apply_contrast(float contrast);
292
293 // ------------------------------------------------------------------
294 // File output
295 // ------------------------------------------------------------------
296
306 bool write_ppm(const std::string &filename) const;
307
316 bool write_bmp(const std::string &filename) const;
317
332 bool write_tga(const std::string &filename, bool use24bit = false) const;
333
349 bool write_png(const std::string &filename) const;
350
351 // ------------------------------------------------------------------
352 // File input (stb_image)
353 // ------------------------------------------------------------------
354
368 static Image read_image(const std::string &path);
369
370 // ------------------------------------------------------------------
371 // Size normalization
372 // ------------------------------------------------------------------
373
393 void pad_to_size(int target_w, int target_h, const Color &background);
394};
395
396// ---------------------------------------------------------------------------
397// Free functions
398// ---------------------------------------------------------------------------
399
422Image grid_arrange(const std::vector<Image> &images,
423 int ncol = 0, int nrow = 0,
425 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f));
426
439inline Image stack_horizontal(const std::vector<Image> &images,
441 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f)) {
442 return grid_arrange(images, static_cast<int>(images.size()), 1,
444}
445
458inline Image stack_vertical(const std::vector<Image> &images,
460 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f)) {
461 return grid_arrange(images, 1, static_cast<int>(images.size()),
463}
464
465} // namespace scimesh
CropContentDirection
Direction(s) for the crop_to_content() operation.
Definition image.h:38
@ VERTICAL
Crop both top and bottom edges.
@ ALL
Crop all four edges.
@ HORIZONTAL
Crop both left and right edges.
Image grid_arrange(const std::vector< Image > &images, int ncol, int nrow, FitMode fit_mode, const Color &background)
Arrange a list of images into a grid layout.
Definition image.cpp:560
MergeDirection
Direction for the merge() operation.
Definition image.h:26
@ BOTTOM
Attach other below.
@ RIGHT
Attach other to the right side.
@ TOP
Attach other above.
@ LEFT
Attach other to the left side.
Image stack_horizontal(const std::vector< Image > &images, FitMode fit_mode=FitMode::PAD, const Color &background=Color(1.0f, 1.0f, 1.0f, 1.0f))
Stack images horizontally in a single row.
Definition image.h:439
Image stack_vertical(const std::vector< Image > &images, FitMode fit_mode=FitMode::PAD, const Color &background=Color(1.0f, 1.0f, 1.0f, 1.0f))
Stack images vertically in a single column.
Definition image.h:458
FitMode
Strategy for normalizing images to a common cell size in grid_arrange().
Definition image.h:51
@ SCALE
Scale all images to match the largest cell dimensions.
@ PAD
Pad smaller images with background color (content stays pixel-perfect).
An RGBA color with floating-point components.
Definition types.h:88
A 2D RGBA image buffer.
Definition image.h:92
void apply_contrast(float contrast)
Apply a contrast adjustment to the image.
Definition image.cpp:379
int height
Image height in pixels.
Definition image.h:97
bool write_tga(const std::string &filename, bool use24bit=false) const
Write the image as a TGA file (Truevision Targa).
Definition image.cpp:471
bool write_ppm(const std::string &filename) const
Write the image as a PPM file (Portable Pixmap).
Definition image.cpp:392
void scale(int new_width, int new_height)
Scale (resize) the image to new dimensions in-place.
Definition image.cpp:251
int width
Image width in pixels.
Definition image.h:94
void crop(int x, int y, int w, int h)
Crop the image to a sub-rectangle.
Definition image.cpp:110
Image()=default
Construct an empty (0×0) image.
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.
Definition image.cpp:39
std::vector< uint8_t > pixels
Raw pixel data: RGBA bytes, row-major, first row = top of the image.
Definition image.h:103
void grow(int top, int bottom, int left, int right, const Color &background)
Grow (pad) the image by adding borders.
Definition image.cpp:197
void rotate_90(bool clockwise=true)
Rotate the image by 90 degrees in-place.
Definition image.cpp:228
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).
Definition image.cpp:70
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.
Definition image.cpp:49
void merge(const Image &other, MergeDirection direction)
Merge (concatenate) another image onto this one.
Definition image.cpp:143
Image downsample_box(int factor) const
Downsample the image by a factor using box filtering.
Definition image.cpp:77
void pad_to_size(int target_w, int target_h, const Color &background)
Pad the image to a target size, centering the content.
Definition image.cpp:547
bool write_png(const std::string &filename) const
Write the image as a PNG file.
Definition image.cpp:521
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).
Definition image.cpp:61
void crop_to_content(CropContentDirection direction, const Color &background)
Crop away uniform borders of a given background color.
Definition image.cpp:273
Color sample_bilinear(float u, float v) const
Sample the image at texture coordinates (u, v) using bilinear interpolation.
Definition image.cpp:351
static Image read_image(const std::string &path)
Read an image from a file (PNG, BMP, TGA, JPEG, etc.).
Definition image.cpp:533
bool write_bmp(const std::string &filename) const
Write the image as a BMP file (Windows Bitmap).
Definition image.cpp:405
int y
Top edge of the bitmap, in image pixels.
Definition text.cpp:224
int x
Left edge of the bitmap, in image pixels.
Definition text.cpp:223
Fundamental types used throughout the scimesh rendering engine.