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
31#include <string>
32
33namespace vcl {
34
43class Image
44{
45 Array2<uint> mImg;
46
47public:
48 Image() {}
49
62 const void* data,
63 uint w,
64 uint h,
65 bool yFlip = false,
66 Color::Format format = Color::Format::ABGR)
67 {
68 if (data) {
69 mImg.resize(h, w);
70 std::size_t size = w * h;
71
72 auto* cdata = reinterpret_cast<const uint32_t*>(data);
73
74 if (format == Color::Format::ABGR) {
75 std::copy(cdata, cdata + size, mImg.data());
76 }
77 else {
78 for (uint i = 0; i < h; i++) {
79 for (uint j = 0; j < w; j++) {
80 Color c(cdata[i * w + j], format);
81 mImg(i, j) = c.abgr();
82 }
83 }
84 }
85
86 if (yFlip) {
87 mirror();
88 }
89 }
90 }
91
92 Image(const Array2<uint>& img) : mImg(img) {}
93
94 Image(Array2<uint>&& img) : mImg(std::move(img)) {}
95
96 bool isNull() const { return mImg.empty(); }
97
98 int height() const { return mImg.rows(); }
99
100 int width() const { return mImg.cols(); }
101
102 std::size_t sizeInBytes() const { return mImg.rows() * mImg.cols() * 4; }
103
104 Color pixel(uint i, uint j) const
105 {
106 return Color(static_cast<Color::ColorABGR>(mImg(i, j)));
107 }
108
109 const unsigned char* data() const
110 {
111 return reinterpret_cast<const unsigned char*>(mImg.data());
112 }
113
114 void mirror(bool horizontal = false, bool vertical = true)
115 {
116 if (horizontal) {
117 for (uint i = 0; i < mImg.rows(); i++) {
118 std::reverse(mImg.data(i), mImg.data(i) + mImg.cols());
119 }
120 }
121 if (vertical) {
122 for (uint i = 0; i < mImg.rows() / 2; i++) {
123 uint mir = mImg.rows() - i - 1;
124 std::swap_ranges(
125 mImg.data(i), mImg.data(i) + mImg.cols(), mImg.data(mir));
126 }
127 }
128 }
129
130 void serialize(std::ostream& os) const { mImg.serialize(os); }
131
132 void deserialize(std::istream& is) { mImg.deserialize(is); }
133};
134
135/* Concepts */
136
147template<typename T>
148concept ImageConcept = std::derived_from<std::remove_cvref_t<T>, Image>;
149
150} // namespace vcl
151
152#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
The Image class stores an Image in 4 bytes RGBA format.
Definition image.h:44
Image(const void *data, uint w, uint h, bool yFlip=false, Color::Format format=Color::Format::ABGR)
Construct an Image from a raw buffer, which is assumed to be in the given format (default: ABGR).
Definition image.h:61
A concept representing an Image.
Definition image.h:148