Visual Computing Library
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 <vclib/io/image.h>
27#include <vclib/io/serialization.h>
28#include <vclib/space/core/array.h>
29#include <vclib/space/core/color.h>
30
31#include <string>
32
33namespace vcl {
34
43class Image
44{
46
47public:
48 Image() {}
49
55 Image(const std::string& filename) { load(filename); }
56
69 const void* data,
70 uint w,
71 uint h,
72 bool yFlip = false,
73 Color::Format format = Color::Format::ABGR)
74 {
75 if (data) {
76 mImg.resize(h, w);
77 std::size_t size = w * h;
78
79 auto* cdata = reinterpret_cast<const uint32_t*>(data);
80
81 if (format == Color::Format::ABGR) {
82 std::copy(cdata, cdata + size, mImg.data());
83 }
84 else {
85 for (uint i = 0; i < h; i++) {
86 for (uint j = 0; j < w; j++) {
87 Color c(cdata[i * w + j], format);
88 mImg(i, j) = c.abgr();
89 }
90 }
91 }
92
93 if (yFlip) {
94 mirror();
95 }
96 }
97 }
98
99 bool isNull() const { return mImg.empty(); }
100
101 int height() const { return mImg.rows(); }
102
103 int width() const { return mImg.cols(); }
104
105 std::size_t sizeInBytes() const { return mImg.rows() * mImg.cols() * 4; }
106
107 Color pixel(uint i, uint j) const
108 {
109 return Color(static_cast<Color::ColorABGR>(mImg(i, j)));
110 }
111
112 const unsigned char* data() const
113 {
114 return reinterpret_cast<const unsigned char*>(mImg.data());
115 }
116
117 bool load(const std::string& filename)
118 {
119 int w, h;
120 // we first load the data, then we copy it into our array2d, and then we
121 // free it.
122 std::shared_ptr<unsigned char> tmp = loadImageData(filename, w, h);
123 if (tmp) {
124 std::size_t size = w * h * 4;
125
126 mImg.resize(w, h);
127 std::copy(
128 tmp.get(),
129 tmp.get() + size,
130 reinterpret_cast<unsigned char*>(mImg.data()));
131 return true;
132 }
133 else {
134 return false;
135 }
136 }
137
138 void save(const std::string& filename, uint quality = 90) const
139 {
140 auto* data = reinterpret_cast<const unsigned char*>(mImg.data());
141 saveImageData(filename, mImg.cols(), mImg.rows(), data, quality);
142 }
143
144 void mirror(bool horizontal = false, bool vertical = true)
145 {
146 if (horizontal) {
147 for (uint i = 0; i < mImg.rows(); i++) {
148 std::reverse(mImg.data(i), mImg.data(i) + mImg.cols());
149 }
150 }
151 if (vertical) {
152 for (uint i = 0; i < mImg.rows() / 2; i++) {
153 uint mir = mImg.rows() - i - 1;
154 std::swap_ranges(
155 mImg.data(i), mImg.data(i) + mImg.cols(), mImg.data(mir));
156 }
157 }
158 }
159
160 void serialize(std::ostream& os) const { mImg.serialize(os); }
161
162 void deserialize(std::istream& is) { mImg.deserialize(is); }
163};
164
165} // namespace vcl
166
167#endif // VCL_SPACE_CORE_IMAGE_H
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 std::string &filename)
Load an image from a file.
Definition image.h:55
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:68
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43