MCPcopy Create free account
hub / github.com/PacktPublishing/3D-Graphics-Rendering-Cookbook / Bitmap

Class Bitmap

shared/Bitmap.h:21–121  ·  view source on GitHub ↗

R/RG/RGB/RGBA bitmaps

Source from the content-addressed store, hash-verified

19
20/// R/RG/RGB/RGBA bitmaps
21struct Bitmap
22{
23 Bitmap() = default;
24 Bitmap(int w, int h, int comp, eBitmapFormat fmt)
25 :w_(w), h_(h), comp_(comp), fmt_(fmt), data_(w * h * comp * getBytesPerComponent(fmt))
26 {
27 initGetSetFuncs();
28 }
29 Bitmap(int w, int h, int d, int comp, eBitmapFormat fmt)
30 :w_(w), h_(h), d_(d), comp_(comp), fmt_(fmt), data_(w * h * d * comp * getBytesPerComponent(fmt))
31 {
32 initGetSetFuncs();
33 }
34 Bitmap(int w, int h, int comp, eBitmapFormat fmt, const void* ptr)
35 :w_(w), h_(h), comp_(comp), fmt_(fmt), data_(w * h * comp * getBytesPerComponent(fmt))
36 {
37 initGetSetFuncs();
38 memcpy(data_.data(), ptr, data_.size());
39 }
40 int w_ = 0;
41 int h_ = 0;
42 int d_ = 1;
43 int comp_ = 3;
44 eBitmapFormat fmt_ = eBitmapFormat_UnsignedByte;
45 eBitmapType type_ = eBitmapType_2D;
46 std::vector<uint8_t> data_;
47
48 static int getBytesPerComponent(eBitmapFormat fmt)
49 {
50 if (fmt == eBitmapFormat_UnsignedByte) return 1;
51 if (fmt == eBitmapFormat_Float) return 4;
52 return 0;
53 }
54
55 void setPixel(int x, int y, const glm::vec4& c)
56 {
57 (*this.*setPixelFunc)(x, y, c);
58 }
59 glm::vec4 getPixel(int x, int y) const
60 {
61 return ((*this.*getPixelFunc)(x, y));
62 }
63private:
64 using setPixel_t = void(Bitmap::*)(int, int, const glm::vec4&);
65 using getPixel_t = glm::vec4(Bitmap::*)(int, int) const;
66 setPixel_t setPixelFunc = &Bitmap::setPixelUnsignedByte;
67 getPixel_t getPixelFunc = &Bitmap::getPixelUnsignedByte;
68
69 void initGetSetFuncs()
70 {
71 switch (fmt_)
72 {
73 case eBitmapFormat_UnsignedByte:
74 setPixelFunc = &Bitmap::setPixelUnsignedByte;
75 getPixelFunc = &Bitmap::getPixelUnsignedByte;
76 break;
77 case eBitmapFormat_Float:
78 setPixelFunc = &Bitmap::setPixelFloat;

Calls

no outgoing calls

Tested by

no test coverage detected