CGVIS Utils
Loading...
Searching...
No Matches
Image.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <vector>
5#include <string>
6
7#include "Vec4.h"
8
9class Grid2D;
10
22class Image {
23public:
26 uint32_t width;
27 uint32_t height;
29 std::vector<uint8_t> data;
31
36 Image(const Vec4& color);
37
45 Image(uint32_t width = 100,
46 uint32_t height = 100,
47 uint8_t componentCount = 4);
48
56 Image(uint32_t width,
57 uint32_t height,
58 uint8_t componentCount,
59 std::vector<uint8_t> data);
60
67 void multiply(const Vec4& color);
68
75 void generateAlpha(uint8_t alpha=255);
76
83
91 size_t computeIndex(uint32_t x, uint32_t y, uint8_t component) const;
92
100 uint8_t getValue(uint32_t x, uint32_t y, uint8_t component) const;
101
109 uint8_t sample(float x, float y, uint8_t component) const;
110
116 uint8_t getLumiValue(uint32_t x, uint32_t y) const;
117
124 void setValue(uint32_t x, uint32_t y, uint8_t component, uint8_t value);
125
131 void setValue(uint32_t x, uint32_t y, uint8_t value);
132
138 void setNormalizedValue(uint32_t x, uint32_t y, float value);
139
146 void setNormalizedValue(uint32_t x, uint32_t y, uint8_t component, float value);
147
154 std::string toCode(const std::string& varName="myImage", bool padding=false) const;
155
161 std::string toACIIArt(bool bSmallTable=true) const;
162
168 Image filter(const Grid2D& filter) const;
169
174 Image toGrayscale() const;
175
182 static Image genTestImage(uint32_t width,
183 uint32_t height);
184
191 Image crop(uint32_t blX, uint32_t blY, uint32_t trX, uint32_t trY) const;
192
198 Image resample(uint32_t newWidth) const;
199
206 Image cropToAspectAndResample(uint32_t newWidth, uint32_t newHeight) const;
207
212 Image flipHorizontal() const;
213
218 Image flipVertical() const;
219
220private:
228 uint8_t linear(uint8_t a, uint8_t b, float alpha) const;
229};
4D vector template with arithmetic, length helpers, and utilities.
Vec4t< float > Vec4
4D float vector (x,y,z,w).
Definition Vec4.h:337
Definition Grid2D.h:27
uint8_t componentCount
Number of components per pixel (1–4).
Definition Image.h:28
uint32_t width
Image width in pixels.
Definition Image.h:26
Image flipHorizontal() const
Flip rows (vertical mirror around horizontal axis).
Definition Image.cpp:322
void generateAlpha(uint8_t alpha=255)
Ensure an alpha channel exists and set it to alpha for all pixels.
Definition Image.cpp:347
Image flipVertical() const
Flip columns (horizontal mirror around vertical axis).
Definition Image.cpp:334
void setNormalizedValue(uint32_t x, uint32_t y, float value)
Set RGB at (x,y) from a normalized scalar in [0,1].
Definition Image.cpp:98
void setValue(uint32_t x, uint32_t y, uint8_t component, uint8_t value)
Write a single component at (x,y).
Definition Image.cpp:87
Image toGrayscale() const
Convert to single‑channel grayscale using luminance.
Definition Image.cpp:185
uint8_t getValue(uint32_t x, uint32_t y, uint8_t component) const
Read a component value at (x,y).
Definition Image.cpp:83
std::vector< uint8_t > data
Interleaved pixel data of size width*height*componentCount.
Definition Image.h:29
size_t computeIndex(uint32_t x, uint32_t y, uint8_t component) const
Compute flat buffer index for a pixel component.
Definition Image.cpp:79
Image crop(uint32_t blX, uint32_t blY, uint32_t trX, uint32_t trY) const
Crop a rectangle.
Definition Image.cpp:308
void multiply(const Vec4 &color)
Component‑wise multiply by a color; upgrades RGB→RGBA if needed.
Definition Image.cpp:36
static Image genTestImage(uint32_t width, uint32_t height)
Generate a colored test pattern (RGB bands + luminance ramp).
Definition Image.cpp:195
void generateAlphaFromLuminance()
Create or overwrite alpha from luminance of RGB.
Definition Image.cpp:59
std::string toCode(const std::string &varName="myImage", bool padding=false) const
Emit C++ code that reconstructs this image.
Definition Image.cpp:111
uint32_t height
Image height in pixels.
Definition Image.h:27
std::string toACIIArt(bool bSmallTable=true) const
Render a coarse ASCII art representation of the image.
Definition Image.cpp:135
Image cropToAspectAndResample(uint32_t newWidth, uint32_t newHeight) const
Crop to match target aspect, then downsample to target size.
Definition Image.cpp:271
uint8_t getLumiValue(uint32_t x, uint32_t y) const
Luminance from components at (x,y).
Definition Image.cpp:151
Image resample(uint32_t newWidth) const
Aspect‑preserving resample to a new width.
Definition Image.cpp:256
uint8_t sample(float x, float y, uint8_t component) const
Bilinear sample at normalized coordinates in [0,1].
Definition Image.cpp:234
Image(const Vec4 &color)
Construct a 1×1 RGBA image initialized from a color in [0,1].
Definition Image.cpp:7
Image filter(const Grid2D &filter) const
Convolve image with a small 2D filter kernel (no borders).
Definition Image.cpp:161