scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <scimesh/types.h>
11#include <vector>
12#include <cstdint>
13#include <string>
14
15namespace scimesh {
16
17// ---------------------------------------------------------------------------
18// Enums for image operations
19// ---------------------------------------------------------------------------
20
25enum class MergeDirection {
26 LEFT,
27 RIGHT,
28 TOP,
29 BOTTOM
30};
31
38 LEFT,
39 RIGHT,
41 TOP,
42 BOTTOM,
43 VERTICAL,
44 ALL
45};
46
50enum class FitMode {
51 PAD,
52 SCALE
53};
54
55// ---------------------------------------------------------------------------
56// Image
57// ---------------------------------------------------------------------------
58
87struct Image {
89 int width = 0;
90
92 int height = 0;
93
98 std::vector<uint8_t> pixels;
99
101 Image() = default;
102
107 Image(int w, int h);
108
116 void set_pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
117
125 void get_pixel(int x, int y, uint8_t &r, uint8_t &g, uint8_t &b, uint8_t &a) const;
126
132 void clear(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
133
142 void clear_float(float r, float g, float b, float a);
143
158 Color sample_bilinear(float u, float v) const;
159
173 Image downsample_box(int factor) const;
174
188 void crop(int x, int y, int w, int h);
189
207 void merge(const Image &other, MergeDirection direction);
208
224 void grow(int top, int bottom, int left, int right, const Color &background);
225
236 void rotate_90(bool clockwise = true);
237
249 void scale(int new_width, int new_height);
250
266 void crop_to_content(CropContentDirection direction, const Color &background);
267
279 void apply_contrast(float contrast);
280
281 // ------------------------------------------------------------------
282 // File output
283 // ------------------------------------------------------------------
284
294 bool write_ppm(const std::string &filename) const;
295
304 bool write_bmp(const std::string &filename) const;
305
320 bool write_tga(const std::string &filename, bool use24bit = false) const;
321
337 bool write_png(const std::string &filename) const;
338
339 // ------------------------------------------------------------------
340 // File input (stb_image)
341 // ------------------------------------------------------------------
342
356 static Image read_image(const std::string &path);
357
358 // ------------------------------------------------------------------
359 // Size normalization
360 // ------------------------------------------------------------------
361
381 void pad_to_size(int target_w, int target_h, const Color &background);
382};
383
384// ---------------------------------------------------------------------------
385// Free functions
386// ---------------------------------------------------------------------------
387
410Image grid_arrange(const std::vector<Image> &images,
411 int ncol = 0, int nrow = 0,
412 FitMode fit_mode = FitMode::PAD,
413 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f));
414
427inline Image stack_horizontal(const std::vector<Image> &images,
428 FitMode fit_mode = FitMode::PAD,
429 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f)) {
430 return grid_arrange(images, static_cast<int>(images.size()), 1,
431 fit_mode, background);
432}
433
446inline Image stack_vertical(const std::vector<Image> &images,
447 FitMode fit_mode = FitMode::PAD,
448 const Color &background = Color(1.0f, 1.0f, 1.0f, 1.0f)) {
449 return grid_arrange(images, 1, static_cast<int>(images.size()),
450 fit_mode, background);
451}
452
453} // namespace scimesh
CropContentDirection
Direction(s) for the crop_to_content() operation.
Definition image.h:37
@ 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:25
@ 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:427
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:446
FitMode
Strategy for normalizing images to a common cell size in grid_arrange().
Definition image.h:50
@ 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:87
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:92
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:89
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, bottom-left origin.
Definition image.h:98
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
Fundamental types used throughout the scimesh rendering engine.