16#define STBI_ASSERT(x) ((void)0)
17#define STBIW_ASSERT(x) ((void)0)
19#ifdef SCIMESH_STB_WRITE_IMPL
20#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
22#define STB_IMAGE_WRITE_IMPLEMENTATION
25#include "stb_image_write.h"
27#ifdef SCIMESH_STB_READ_IMPL
28#ifndef STB_IMAGE_IMPLEMENTATION
30#define STB_IMAGE_IMPLEMENTATION
71 clear(
static_cast<uint8_t>(std::clamp(r, 0.0f, 1.0f) * 255.0f),
72 static_cast<uint8_t>(std::clamp(g, 0.0f, 1.0f) * 255.0f),
73 static_cast<uint8_t>(std::clamp(b, 0.0f, 1.0f) * 255.0f),
74 static_cast<uint8_t>(std::clamp(a, 0.0f, 1.0f) * 255.0f));
111 if (
w <= 0 ||
h <= 0) {
116 int x0 = std::max(0,
x);
117 int y0 = std::max(0,
y);
144 if (
other.width <= 0 ||
other.height <= 0)
return;
186 for (
int y = 0;
y <
other.height; ++
y) {
281 auto is_bg = [&](
int x,
int y) {
353 u = std::max(0.0f, std::min(1.0f,
u));
354 v = std::max(0.0f, std::min(1.0f,
v));
357 int x0 =
static_cast<int>(
fx);
358 int y0 =
static_cast<int>(
fy);
380 if (contrast == 1.0f)
return;
386 pixels[
idx] =
static_cast<uint8_t>(std::clamp((r - 0.5f) * contrast + 0.5f, 0.0f, 1.0f) * 255.0f);
387 pixels[
idx + 1] =
static_cast<uint8_t>(std::clamp((g - 0.5f) * contrast + 0.5f, 0.0f, 1.0f) * 255.0f);
388 pixels[
idx + 2] =
static_cast<uint8_t>(std::clamp((b - 0.5f) * contrast + 0.5f, 0.0f, 1.0f) * 255.0f);
393 std::ofstream
f(
filename, std::ios::binary);
411 std::ofstream
f(
filename, std::ios::binary);
423 f.write(
reinterpret_cast<const char *
>(
fh), 14);
428 f.write(
reinterpret_cast<const char *
>(&
header_size), 4);
431 f.write(
reinterpret_cast<const char *
>(&
w), 4);
432 f.write(
reinterpret_cast<const char *
>(&
h), 4);
434 f.write(
reinterpret_cast<const char *
>(&
planes), 2);
436 f.write(
reinterpret_cast<const char *
>(&
bpp), 2);
438 f.write(
reinterpret_cast<const char *
>(&
compression), 4);
440 f.write(
reinterpret_cast<const char *
>(&
img_size), 4);
442 f.write(
reinterpret_cast<const char *
>(&
ppm), 4);
443 f.write(
reinterpret_cast<const char *
>(&
ppm), 4);
445 f.write(
reinterpret_cast<const char *
>(&
colors_used), 4);
453 f.write(
reinterpret_cast<const char *
>(&
r_mask), 4);
454 f.write(
reinterpret_cast<const char *
>(&
g_mask), 4);
455 f.write(
reinterpret_cast<const char *
>(&
b_mask), 4);
456 f.write(
reinterpret_cast<const char *
>(&
a_mask), 4);
477 std::ofstream
f(
filename, std::ios::binary);
497 f.write(
reinterpret_cast<const char *
>(
header),
sizeof(
header));
513 f.write(
reinterpret_cast<const char *
>(
row.data()),
514 static_cast<std::streamsize
>(
row.size()));
534 int w = 0,
h = 0,
n = 0;
536 if (!data)
return Image();
538 std::memcpy(
img.pixels.data(), data,
w *
h * 4);
566 int n =
static_cast<int>(
images.size());
570 ncol =
static_cast<int>(std::ceil(std::sqrt(
static_cast<double>(
n))));
572 }
else if (
ncol <= 0) {
574 }
else if (
nrow <= 0) {
592 std::vector<Image>
cells;
605 cells.push_back(std::move(
c));
619 cells.push_back(std::move(
c));
632 cells.push_back(std::move(
c));
644 std::vector<Image>
rows;
646 for (
int r = 0; r <
nrow; ++r) {
648 for (
int c = 1;
c <
ncol; ++
c) {
658 for (
int r = 1; r <
nrow; ++r) {
The Image — an RGBA pixel buffer with compositing and I/O operations.
CropContentDirection
Direction(s) for the crop_to_content() operation.
@ BOTTOM
Crop bottom edge only.
@ RIGHT
Crop right edge only.
@ VERTICAL
Crop both top and bottom edges.
@ ALL
Crop all four edges.
@ LEFT
Crop left edge only.
@ 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.
MergeDirection
Direction for the merge() operation.
@ BOTTOM
Attach other below.
@ RIGHT
Attach other to the right side.
@ LEFT
Attach other to the left side.
FitMode
Strategy for normalizing images to a common cell size in grid_arrange().
@ SCALE
Scale all images to match the largest cell dimensions.
An RGBA color with floating-point components.
void apply_contrast(float contrast)
Apply a contrast adjustment to the image.
int height
Image height in pixels.
bool write_tga(const std::string &filename, bool use24bit=false) const
Write the image as a TGA file (Truevision Targa).
bool write_ppm(const std::string &filename) const
Write the image as a PPM file (Portable Pixmap).
void scale(int new_width, int new_height)
Scale (resize) the image to new dimensions in-place.
int width
Image width in pixels.
void crop(int x, int y, int w, int h)
Crop the image to a sub-rectangle.
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.
std::vector< uint8_t > pixels
Raw pixel data: RGBA bytes, row-major, first row = top of the image.
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 clear_float(float r, float g, float b, float a)
Fill the entire image with an RGBA color (float values 0.0–1.0).
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 merge(const Image &other, MergeDirection direction)
Merge (concatenate) another image onto this one.
Image downsample_box(int factor) const
Downsample the image by a factor using box filtering.
void pad_to_size(int target_w, int target_h, const Color &background)
Pad the image to a target size, centering the content.
bool write_png(const std::string &filename) const
Write the image as a PNG file.
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 crop_to_content(CropContentDirection direction, const Color &background)
Crop away uniform borders of a given background color.
Color sample_bilinear(float u, float v) const
Sample the image at texture coordinates (u, v) using bilinear interpolation.
static Image read_image(const std::string &path)
Read an image from a file (PNG, BMP, TGA, JPEG, etc.).
bool write_bmp(const std::string &filename) const
Write the image as a BMP file (Windows Bitmap).
int y
Top edge of the bitmap, in image pixels.
int x
Left edge of the bitmap, in image pixels.