Library of Assembled Shared Sources
image.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::io::Image
46 * @brief image thing?
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_IO_IMAGE_H
51#define LASS_GUARDIAN_OF_INCLUSION_IO_IMAGE_H
52
53#include "io_common.h"
54
55#include "../num/num_common.h"
56#include "../num/endianness.h"
57#include "../prim/color_rgba.h"
58#include "../prim/point_2d.h"
60
61#if LASS_HAVE_STD_FILESYSTEM
62# include <filesystem>
63#endif
64
65namespace lass
66{
67
68// new interfaces
69
70namespace io
71{
72
73class BinaryIStream;
74class BinaryOStream;
75
77{
78public:
79
80 typedef num::NumTraits<size_t>::signedType TSignedSize;
81
82 typedef prim::ColorRGBA TPixel;
83 typedef std::vector<TPixel> TRaster;
84
85 typedef prim::ColorRGBA::TValue TValue;
86 typedef prim::ColorRGBA::TParam TParam;
87 typedef prim::ColorRGBA::TNumTraits TNumTraits;
88
89 typedef TValue (*TFilterFunction)(TValue);
90
91 typedef prim::Point2D<num::Tfloat32> TChromaticity;
92 enum { numChromaticities = 4 };
93 struct ColorSpace
94 {
95 TChromaticity red;
96 TChromaticity green;
97 TChromaticity blue;
98 TChromaticity white;
99 num::Tfloat32 gamma;
100 bool isFromFile;
101 const TChromaticity& operator[](size_t index) const { LASS_ASSERT(index < numChromaticities); return (&red)[index]; }
102 TChromaticity& operator[](size_t index) { LASS_ASSERT(index < numChromaticities); return (&red)[index]; }
103 bool operator==(const ColorSpace& other) const
104 {
105 return red == other.red && green == other.green && blue == other.blue && white == other.white && gamma == other.gamma;
106 }
107 bool operator!=(const ColorSpace& other) const { return !(*this == other); }
108 };
109
110 class BadFormat: public util::ExceptionMixin<BadFormat>
111 {
112 public:
113 BadFormat(std::string msg, std::string loc): util::ExceptionMixin<BadFormat>(std::move(msg), std::move(loc)) {}
114 ~BadFormat() noexcept {}
115 };
116
117 // STRUCTORS
118
119 Image();
120 Image(size_t rows, size_t cols);
121 Image(const std::string& path);
122#if LASS_HAVE_WCHAR_SUPPORT
123 Image(const std::wstring& path);
124#endif
125#if LASS_HAVE_STD_FILESYSTEM
126 Image(const std::filesystem::path& path);
127#endif
128 Image(const Image& other);
129 ~Image();
130
131
132 // METHODS
133
134 void reset();
135 void reset(size_t rows, size_t cols);
136 void reset(const std::string& path);
137#if LASS_HAVE_WCHAR_SUPPORT
138 void reset(const std::wstring& path);
139#endif
140#if LASS_HAVE_STD_FILESYSTEM
141 void reset(const std::filesystem::path& path);
142#endif
143 void reset(const Image& other);
144
145 void open(const std::string& path);
146 void open(BinaryIStream& stream, const std::string& formatTag);
147 void save(const std::string& path);
148 void save(BinaryOStream& stream, const std::string& formatTag);
149#if LASS_HAVE_WCHAR_SUPPORT
150 void open(const std::wstring& path);
151 void save(const std::wstring& path);
152#endif
153#if LASS_HAVE_STD_FILESYSTEM
154 void open(const std::filesystem::path& path);
155 void save(const std::filesystem::path& path);
156#endif
157
158 Image& operator=(const Image& other);
159 void swap(Image& other);
160
161 const TPixel& operator[](size_t flatIndex) const { return raster_[flatIndex]; }
162 TPixel& operator[](size_t flatIndex) { return raster_[flatIndex]; }
163
164 const TPixel& operator()(size_t row, size_t col) const;
165 TPixel& operator()(size_t row, size_t col);
166
167 const TPixel& at(TSignedSize row, TSignedSize col) const;
168 TPixel& at(TSignedSize row, TSignedSize col);
169
170 const TPixel* data() const;
171 TPixel* data();
172
173 const ColorSpace& colorSpace() const;
174 ColorSpace& colorSpace();
175 void transformColors(const ColorSpace& newColorSpace);
176
177 size_t rows() const;
178 size_t cols() const;
179 bool isEmpty() const;
180
181 // OPERATIONS
182
183 void over(const Image& other);
184 void in(const Image& other);
185 void out(const Image& other);
186 void atop(const Image& other);
187 void through(const Image& other);
188 void rover(const Image& other);
189 void rin(const Image& other);
190 void rout(const Image& other);
191 void ratop(const Image& other);
192 void rthrough(const Image& other);
193 void plus(const Image& other);
194
195 void clampNegatives();
196
197 // FILTERS
198
199 void filterMedian(size_t boxSize);
200 void filterGamma(TParam gammaExponent);
201 void filterExposure(TParam exposureTime);
202 void filterInverseExposure(TParam exposureTime);
203 void filter(TFilterFunction function);
204
205private:
206
207 struct HeaderLass
208 {
209 num::Tuint32 lass;
210 num::Tuint32 version;
211 num::Tuint32 rows;
212 num::Tuint32 cols;
213
214 void readFrom(BinaryIStream& stream);
215 void writeTo(BinaryOStream& stream);
216 };
217
218 struct HeaderTarga
219 {
220 num::Tuint8 idLength;
221 num::Tuint8 colorMapType;
222 num::Tuint8 imageType;
223 num::Tuint16 colorMapOrigin;
224 num::Tuint16 colorMapLength;
225 num::Tuint8 colorMapEntrySize;
226 num::Tuint16 imageXorigin;
227 num::Tuint16 imageYorigin;
228 num::Tuint16 imageWidth;
229 num::Tuint16 imageHeight;
230 num::Tuint8 imagePixelSize;
231 num::Tuint8 imageDescriptor;
232
233 unsigned numAttributeBits() const { return imageDescriptor & 0x0F; }
234 bool flipHorizontalFlag() const { return ((imageDescriptor >> 4) & 0x01) == 0x01; }
235 bool flipVerticalFlag() const { return ((imageDescriptor >> 5) & 0x01) == 0x01; }
236 bool interleavingFlag() const { return ((imageDescriptor >> 6) & 0x01) == 0x01; }
237
238 void readFrom(BinaryIStream& stream);
239 void writeTo(BinaryOStream& stream);
240 };
241
242 struct HeaderRadianceHdr
243 {
244 enum
245 {
246 sizeColorCorr = 3,
247 sizePrimaries = 8
248 };
249
250 float exposure;
251 float colorCorr[sizeColorCorr];
252 float primaries[sizePrimaries];
253 size_t height;
254 size_t width;
255 bool yIncreasing;
256 bool xIncreasing;
257 bool isRgb;
258 bool isDefaultPrimaries;
259
260 HeaderRadianceHdr();
261 void readFrom(BinaryIStream& stream);
262 void writeTo(BinaryOStream& stream);
263 };
264
265 struct HeaderPfm
266 {
267 size_t width;
268 size_t height;
269 float aspect;
270 num::Endianness endianness;
271 bool isGrey;
272
273 HeaderPfm();
274 void readFrom(BinaryIStream& stream);
275 void writeTo(BinaryOStream& stream);
276 };
277
278 struct HeaderIgi
279 {
280 num::Tint32 magic;
281 num::Tint32 version;
282 num::Tfloat64 numSamples;
283 num::Tuint32 width;
284 num::Tuint32 height;
285 num::Tuint32 superSampling;
286 num::Tint32 zipped;
287 num::Tint32 dataSize;
288 num::Tuint32 rgb;
289 // num::Tfloat64 render_time; // we don't care!
290 enum { padding = 5000 };
291
292 void readFrom(BinaryIStream& stream);
293 void writeTo(BinaryOStream& stream);
294 };
295
296 typedef BinaryIStream& (Image::*TFileOpener)(BinaryIStream&);
297 typedef BinaryOStream& (Image::*TFileSaver)(BinaryOStream&) const;
298
299 struct FileFormat
300 {
301 TFileOpener open;
302 TFileSaver save;
303 FileFormat(TFileOpener iOpen, TFileSaver iSave): open(iOpen), save(iSave) {}
304 FileFormat(): open(0), save(0) {}
305 };
306
307 typedef std::map<std::string, FileFormat> TFileFormats;
308
309 size_t resize(size_t rows, size_t cols);
310 size_t flatIndex(size_t rows, size_t cols) const
311 {
312 return rows * cols_ + cols;
313 }
314
315 BinaryIStream& openLass(BinaryIStream& stream);
316 BinaryIStream& openTarga(BinaryIStream& stream);
317 BinaryIStream& openTargaTrueColor(BinaryIStream& stream, const HeaderTarga& iHeader);
318 BinaryIStream& openRadianceHdr(BinaryIStream& stream);
319 BinaryIStream& openPfm(BinaryIStream& stream);
320 BinaryIStream& openIgi(BinaryIStream& stream);
321
322 BinaryOStream& saveLass(BinaryOStream& stream) const;
323 BinaryOStream& saveTarga(BinaryOStream& stream) const;
324 BinaryOStream& saveRadianceHdr(BinaryOStream& stream) const;
325 BinaryOStream& savePfm(BinaryOStream& stream) const;
326 BinaryOStream& saveIgi(BinaryOStream& stream) const;
327
328 FileFormat findFormat(const std::string& formatTag);
329 std::string readRadianceHdrString(BinaryIStream& stream) const;
330
331 static BinaryIStream& readLine(BinaryIStream& stream, std::string& line);
332 static BinaryOStream& writeLine(BinaryOStream& stream, const std::string& line);
333
334 static TFileFormats fillFileFormats();
335
336 static const ColorSpace defaultColorSpace();
337 static const ColorSpace xyzColorSpace();
338
339 ColorSpace colorSpace_;
340 size_t rows_;
341 size_t cols_;
342 TRaster raster_;
343
344 static TFileFormats fileFormats_;
345 static num::Tuint32 magicLass_;
346 static std::string magicRadiance_;
347 static num::Tint32 magicIgi_;
348};
349
350
351
352}
353
354}
355
356#endif
357
358// EOF
base class of binary input streams.
base class of binary output streams.
void rout(const Image &other)
this = other out this
Definition image.cpp:697
void filterInverseExposure(TParam exposureTime)
apply exposure to image
Definition image.cpp:873
void filterMedian(size_t boxSize)
Apply a median filter on image.
Definition image.cpp:760
const TPixel * data() const
Return const data block.
Definition image.cpp:486
void transformColors(const ColorSpace &newColorSpace)
Transform the colors from the current color spacer to destColorSpace.
Definition image.cpp:538
void atop(const Image &other)
this = this atop other
Definition image.cpp:657
void swap(Image &other)
swap two images
Definition image.cpp:424
size_t cols() const
Return width of image.
Definition image.cpp:609
void clampNegatives()
clamp all negative pixel components to zero.
Definition image.cpp:737
void ratop(const Image &other)
this = other atop this
Definition image.cpp:707
void out(const Image &other)
this = this out other
Definition image.cpp:647
void filterGamma(TParam gammaExponent)
apply gamma correction to image
Definition image.cpp:846
void through(const Image &other)
this = this through other
Definition image.cpp:667
size_t rows() const
Return height of image.
Definition image.cpp:600
const TPixel & operator()(size_t row, size_t col) const
Return const pixel at position row, col.
Definition image.cpp:438
bool isEmpty() const
Return true if image is empty (no data)
Definition image.cpp:618
Image()
Default constructor.
Definition image.cpp:98
void open(const std::string &path)
Open image from file.
Definition image.cpp:262
void rthrough(const Image &other)
this = other through this
Definition image.cpp:717
void plus(const Image &other)
this = this plus other = other plus this
Definition image.cpp:727
void rover(const Image &other)
this = other over this
Definition image.cpp:677
void save(const std::string &path)
Save image to file.
Definition image.cpp:305
void filterExposure(TParam exposureTime)
apply exposure to image
Definition image.cpp:860
void in(const Image &other)
this = this in other
Definition image.cpp:637
void reset()
Reset image to empty image.
Definition image.cpp:194
void over(const Image &other)
this = this over other
Definition image.cpp:627
Image & operator=(const Image &other)
Copy other into this image.
Definition image.cpp:413
void rin(const Image &other)
this = other in this
Definition image.cpp:687
const TPixel & at(TSignedSize row, TSignedSize col) const
Return const pixel at position row, col.
Definition image.cpp:462
const ColorSpace & colorSpace() const
Return colorSpace of image data.
Definition image.cpp:504
an [0, 1] floating point RGB colour with Alpha channel.
Definition color_rgba.h:62
#define LASS_DLL
DLL interface: import or export symbols?
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53