CMSC23700 Common Code Library
Support code for CS23700 programming projects
Loading...
Searching...
No Matches
cs237-image.hpp
Go to the documentation of this file.
1
8/*
9 * COPYRIGHT (c) 2023 John Reppy (http://cs.uchicago.edu/~jhr)
10 * All rights reserved.
11 */
12
13#ifndef _CS237_IMAGE_HPP_
14#define _CS237_IMAGE_HPP_
15
16#ifndef _CS237_HPP_
17#error "cs237-image.hpp should not be included directly"
18#endif
19
20#include <fstream>
21
22namespace cs237 {
23
25enum class Channels {
26 UNKNOWN,
27 R,
28 RG,
29 RGB,
30 BGR,
31 RGBA,
32 BGRA
33};
34
36std::string to_string (Channels ch);
37
39enum class ChannelTy {
40 UNKNOWN,
41 U8,
42 S8,
43 U16,
44 S16,
45 U32,
46 S32,
47 F32
48};
49
51std::string to_string (ChannelTy ty);
52
53namespace __detail {
54
56 vk::Format toVkFormat (Channels chans, ChannelTy ty, bool sRGB);
57
58 class ImageBase {
59 public:
61 uint32_t nDims () const { return this->_nDims; }
63 Channels channels () const { return this->_chans; }
65 ChannelTy type () const { return this->_type; }
67 vk::Format format () const { return toVkFormat(this->_chans, this->_type, this->_sRGB); }
69 void *data () const { return this->_data; }
71 size_t nBytes () const { return this->_nBytes; }
72
74 unsigned int nChannels () const;
75
77 size_t nBytesPerPixel () const;
78
86
87 protected:
91 bool _sRGB;
92 size_t _nBytes;
93 void *_data;
94
104
105 virtual ~ImageBase ();
106
107 };
108
109} /* namespace __detail */
110
111/* 1D images */
113 public:
118 Image1D (uint32_t wid, Channels chans, ChannelTy ty);
119
122 Image1D (std::string const &file);
123
125 size_t width () const { return this->_wid; }
126
130 bool write (const char *file);
131
132 private:
133 uint32_t _wid;
134};
135
140 public:
146 Image2D (uint32_t wid, uint32_t ht, Channels chans, ChannelTy ty);
147
152 Image2D (std::string const &file, bool flip = true);
153
158 Image2D (std::ifstream &inS, bool flip = true);
159
161 size_t width () const { return this->_wid; }
162
164 size_t height () const { return this->_ht; }
165
174 bool write (const char *file, bool flip = true);
175
184 bool write (std::ofstream &outS, bool flip = true);
185
193 void bitblt (Image2D const &src, uint32_t row, uint32_t col);
194
195 protected:
196 uint32_t _wid;
197 uint32_t _ht;
198};
199
201class DataImage2D : public Image2D {
202 public:
208 DataImage2D (uint32_t wid, uint32_t ht, Channels chans, ChannelTy ty)
209 : Image2D (wid, ht, chans, ty)
210 {
211 this->_sRGB = false;
212 }
213
218 DataImage2D (std::string const &file, bool flip = true)
219 : Image2D (file, flip)
220 {
221 this->_sRGB = false;
222 }
223
228 DataImage2D (std::ifstream &inS, bool flip = true)
229 : Image2D (inS, flip)
230 {
231 this->_sRGB = false;
232 }
233
234};
235
236} /* namespace cs237 */
237
238#endif /* !_CS237_IMAGE_HPP_ */
Definition cs237-image.hpp:58
ImageBase(uint32_t nd)
Definition cs237-image.hpp:99
ImageBase(uint32_t nd, Channels chans, ChannelTy ty, size_t nPixels)
ImageBase()
Definition cs237-image.hpp:95
Channels channels() const
return the format of the pixels.
Definition cs237-image.hpp:63
size_t nBytes() const
the total number of bytes of image data
Definition cs237-image.hpp:71
ChannelTy _type
the representation type of the data
Definition cs237-image.hpp:90
void * _data
the raw image data
Definition cs237-image.hpp:93
bool _sRGB
should the image be interpreted as an sRGB encoded image?
Definition cs237-image.hpp:91
size_t nBytesPerPixel() const
the number of bytes per pixel
ChannelTy type() const
returns the type of the channels
Definition cs237-image.hpp:65
vk::Format format() const
return the vulkan format of the image data
Definition cs237-image.hpp:67
Channels _chans
the texture format
Definition cs237-image.hpp:89
size_t _nBytes
size in bytes of image data
Definition cs237-image.hpp:92
unsigned int nChannels() const
the number of channels (1, 2, 3, or 4)
void * data() const
the data pointer
Definition cs237-image.hpp:69
uint32_t _nDims
the number of dimensions (1 or 2)
Definition cs237-image.hpp:88
uint32_t nDims() const
the number of dimensions (1, 2, or 3)
Definition cs237-image.hpp:61
A 2D Image used to store 2D data, such as a normal map.
Definition cs237-image.hpp:201
DataImage2D(std::ifstream &inS, bool flip=true)
Definition cs237-image.hpp:228
DataImage2D(std::string const &file, bool flip=true)
Definition cs237-image.hpp:218
DataImage2D(uint32_t wid, uint32_t ht, Channels chans, ChannelTy ty)
Definition cs237-image.hpp:208
Definition cs237-image.hpp:112
Image1D(std::string const &file)
Image1D(uint32_t wid, Channels chans, ChannelTy ty)
bool write(const char *file)
size_t width() const
return the width of the image
Definition cs237-image.hpp:125
Definition cs237-image.hpp:139
uint32_t _ht
the height of the image in pixels
Definition cs237-image.hpp:197
bool write(const char *file, bool flip=true)
void bitblt(Image2D const &src, uint32_t row, uint32_t col)
size_t height() const
return the height of the image
Definition cs237-image.hpp:164
Image2D(std::ifstream &inS, bool flip=true)
bool write(std::ofstream &outS, bool flip=true)
uint32_t _wid
the width of the image in pixels
Definition cs237-image.hpp:196
size_t width() const
return the width of the image
Definition cs237-image.hpp:161
Image2D(std::string const &file, bool flip=true)
Image2D(uint32_t wid, uint32_t ht, Channels chans, ChannelTy ty)
vk::Format toVkFormat(Channels chans, ChannelTy ty, bool sRGB)
convert an image format and channel type to a Vulkan image format
Definition cs237-aabb.hpp:22
ChannelTy
the type used to represent the channels
Definition cs237-image.hpp:39
@ F32
signed 32-bit float
@ U8
unsigned byte
@ S16
signed 16-bit int
@ S32
signed 32-bit int
@ S8
signed byte
@ U32
unsigned 32-bit int
@ U16
unsigned 16-bit int
Channels
the channels of an image
Definition cs237-image.hpp:25
@ BGR
three-channel image in blue-green-red order
@ BGRA
four-channel image in blue-green-red-alpha order
@ UNKNOWN
unknown
@ RGB
three-channel image in red-green-blue order
@ RG
two-channel image
@ R
single-channel image
@ RGBA
four-channel image in red-green-n-blue-alpha order
std::string to_string(Channels ch)
convert a Channels value to a printable string
Axis-Aligned Bounding Box parameterized over the scalar type.
Definition cs237-aabb.hpp:28