Visual Computing Library  devel
Loading...
Searching...
No Matches
image.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_IMAGE_H
24#define VCL_SPACE_CORE_IMAGE_H
25
26#include "array.h"
27#include "color.h"
28
29#include <vclib/base.h>
30
31namespace vcl {
32
47class Image
48{
49public:
50 enum class ColorSpace { UNKNOWN, LINEAR, SRGB };
51
52private:
53 Array2<uint> mImg;
54
55 ColorSpace mColorSpace = ColorSpace::UNKNOWN;
56
57public:
61 Image() {}
62
80 const void* data,
81 uint w,
82 uint h,
83 bool yFlip = false,
84 Color::Format format = Color::Format::ABGR)
85 {
86 if (data) {
87 mImg.resize(h, w);
88 std::size_t size = w * h;
89
90 auto* cdata = reinterpret_cast<const uint32_t*>(data);
91
92 if (format == Color::Format::ABGR) {
93 std::copy(cdata, cdata + size, mImg.data());
94 }
95 else {
96 for (uint i = 0; i < h; i++) {
97 for (uint j = 0; j < w; j++) {
98 Color c(cdata[i * w + j], format);
99 mImg(i, j) = c.abgr();
100 }
101 }
102 }
103
104 if (yFlip) {
105 mirror();
106 }
107 }
108 }
109
114 Image(const Array2<uint>& img) : mImg(img) {}
115
120 Image(Array2<uint>&& img) : mImg(std::move(img)) {}
121
127 bool isNull() const { return mImg.empty(); }
128
133 int height() const { return mImg.rows(); }
134
139 int width() const { return mImg.cols(); }
140
145 std::size_t sizeInBytes() const { return mImg.rows() * mImg.cols() * 4; }
146
151 ColorSpace colorSpace() const { return mColorSpace; }
152
157 ColorSpace& colorSpace() { return mColorSpace; }
158
166 Color pixel(uint i, uint j) const
167 {
168 return Color(static_cast<Color::ColorABGR>(mImg(i, j)));
169 }
170
177 const unsigned char* data() const
178 {
179 return reinterpret_cast<const unsigned char*>(mImg.data());
180 }
181
189 void mirror(bool horizontal = false, bool vertical = true)
190 {
191 if (horizontal) {
192 for (uint i = 0; i < mImg.rows(); i++) {
193 std::reverse(mImg.data(i), mImg.data(i) + mImg.cols());
194 }
195 }
196 if (vertical) {
197 for (uint i = 0; i < mImg.rows() / 2; i++) {
198 uint mir = mImg.rows() - i - 1;
199 std::swap_ranges(
200 mImg.data(i), mImg.data(i) + mImg.cols(), mImg.data(mir));
201 }
202 }
203 }
204
209 void serialize(std::ostream& os) const
210 {
211 mImg.serialize(os);
212 vcl::serialize(os, mColorSpace);
213 }
214
219 void deserialize(std::istream& is)
220 {
221 mImg.deserialize(is);
222 vcl::deserialize(is, mColorSpace);
223 }
224};
225
226/* Concepts */
227
234template<typename T>
235concept ImageConcept = std::derived_from<std::remove_cvref_t<T>, Image>;
236
237} // namespace vcl
238
239#endif // VCL_SPACE_CORE_IMAGE_H
A class representing a box in N-dimensional space.
Definition box.h:46
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:476
void serialize(std::ostream &os) const
Serializes the box to the given output stream.
Definition box.h:466
The Color class represents a 32 bit color.
Definition color.h:48
ColorABGR
ABGR enum with some standard colors.
Definition color.h:84
Format
Color format enumeration.
Definition color.h:77
A class for representing and manipulating 2D images.
Definition image.h:48
void serialize(std::ostream &os) const
Serializes the image data to an output stream.
Definition image.h:209
ColorSpace & colorSpace()
Gets a reference to the image's color space property.
Definition image.h:157
const unsigned char * data() const
Provides direct, read-only access to the raw pixel data.
Definition image.h:177
Image(const Array2< uint > &img)
Constructs an Image by copying an existing Array2 of pixels.
Definition image.h:114
int width() const
Gets the width of the image in pixels.
Definition image.h:139
Color pixel(uint i, uint j) const
Retrieves the color of a specific pixel.
Definition image.h:166
void deserialize(std::istream &is)
Deserializes image data from an input stream.
Definition image.h:219
std::size_t sizeInBytes() const
Calculates the total size of the image data in bytes.
Definition image.h:145
int height() const
Gets the height of the image in pixels.
Definition image.h:133
void mirror(bool horizontal=false, bool vertical=true)
Flips the image horizontally and/or vertically in-place.
Definition image.h:189
ColorSpace colorSpace() const
Gets the color space of the image.
Definition image.h:151
bool isNull() const
Checks if the image is null or empty.
Definition image.h:127
Image()
Default constructor. Creates an empty, null image.
Definition image.h:61
Image(const void *data, uint w, uint h, bool yFlip=false, Color::Format format=Color::Format::ABGR)
Constructs an Image from a raw pixel buffer.
Definition image.h:79
Image(Array2< uint > &&img)
Constructs an Image by moving an existing Array2 of pixels.
Definition image.h:120
A concept that is satisfied if a type T is or derives from vcl::Image.
Definition image.h:235