Skip to content

Commit 9689326

Browse files
committed
Soon RGB support
1 parent df9dd0f commit 9689326

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
lines changed

bin/debug/VividImage.exe

207 Bytes
Binary file not shown.

image.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@ namespace vivid { namespace util {
3030
std::ifstream stream(path, std::ifstream::in | std::ifstream::ate | std::ifstream::binary);
3131
auto size = (unsigned int) stream.tellg();
3232
if (!stream.good()) {
33-
pixels = new Pixel(0, 0, 0, 0);
33+
data = new unsigned char[3] {0, 0, 0};
3434
format.width = 1;
3535
format.height = 1;
3636
format.bitDepth = 8;
37-
format.colorFormat = VIVID_IMAGE_FORMAT_RGBA;
37+
format.colorFormat = VIVID_IMAGE_FORMAT_RGB;
38+
return;
3839
}
3940
stream.seekg(0, std::ifstream::beg);
4041

41-
unsigned char data[size];
42+
unsigned char fileData[size];
4243
unsigned int position = 0;
4344
char in;
4445
while (stream.get(in))
45-
data[position++] = (unsigned char) in;
46+
fileData[position++] = (unsigned char) in;
4647

4748
stream.close();
4849

49-
loadChunks(chunks, data, size);
50+
loadChunks(chunks, fileData, size);
5051

5152
// Sets the format of the image
5253
std::vector<unsigned char>& dataIDHR = chunks[0].data; // the IDAT chunk
@@ -55,11 +56,12 @@ namespace vivid { namespace util {
5556
format.bitDepth = dataIDHR[8];
5657
format.colorFormat = dataIDHR[9];
5758
if (format.colorFormat == 3) {
58-
pixels = new Pixel(255, 0, 0, 0);
59+
data = new unsigned char[3] {0, 0, 0};
5960
format.width = 1;
6061
format.height = 1;
6162
format.bitDepth = 8;
62-
format.colorFormat = VIVID_IMAGE_FORMAT_RGBA;
63+
format.colorFormat = VIVID_IMAGE_FORMAT_RGB;
64+
return;
6365
}
6466
}
6567

@@ -258,29 +260,27 @@ namespace vivid { namespace util {
258260
}
259261

260262
// todo: add pixel creation for types 0, 2 and 4
261-
if (format.colorFormat != 6) {
262-
std::cout << "ABORT!" << std::endl;
263-
}
264-
265-
pixels = new Pixel[format.width * format.height];
266-
267-
unsigned int actualWidth = 1 + format.width * 4;
268-
for (unsigned int y = 0; y < format.height; y++) {
269-
for (unsigned int x = 0; x < format.width; x++) {
270-
unsigned int i = 1 + 4 * x + y * actualWidth;
271-
pixels[x + y * format.width].r = (unsigned char) dataStream[i + 0];
272-
pixels[x + y * format.width].g = (unsigned char) dataStream[i + 1];
273-
pixels[x + y * format.width].b = (unsigned char) dataStream[i + 2];
274-
pixels[x + y * format.width].a = (unsigned char) dataStream[i + 3];
263+
if (format.colorFormat == 6) {
264+
PixelRGBA* pixels = new PixelRGBA[format.width * format.height];
265+
266+
unsigned int actualWidth = 1 + format.width * 4;
267+
for (unsigned int y = 0; y < format.height; y++) {
268+
for (unsigned int x = 0; x < format.width; x++) {
269+
unsigned int i = 1 + 4 * x + y * actualWidth;
270+
pixels[x + y * format.width].r = (unsigned char) dataStream[i + 0];
271+
pixels[x + y * format.width].g = (unsigned char) dataStream[i + 1];
272+
pixels[x + y * format.width].b = (unsigned char) dataStream[i + 2];
273+
pixels[x + y * format.width].a = (unsigned char) dataStream[i + 3];
274+
}
275275
}
276+
277+
data = (unsigned char*) pixels;
278+
format.colorFormat = VIVID_IMAGE_FORMAT_RGBA;
276279
}
277-
278-
// it always returns to RGBA
279-
format.colorFormat = VIVID_IMAGE_FORMAT_RGBA;
280280
}
281281

282282
Image::~Image() {
283-
delete[] pixels;
283+
delete[] data;
284284
}
285285

286286
void Image::loadChunks(std::vector<Chunk>& chunks, const unsigned char* data, unsigned int size) {

image.h

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <vector>
1010
#include <utility>
1111

12-
#define LOG(x) (std::cout << x << std::endl)
13-
1412
#ifdef VIVID_OPENGL
1513
#define VIVID_IMAGE_FORMAT_RGB 0x1907
1614
#define VIVID_IMAGE_FORMAT_RGBA 0x1908
@@ -31,7 +29,25 @@ namespace vivid { namespace util {
3129
: length(length), type(std::move(type)), data(data), crc(crc) {}
3230
};
3331

34-
struct Pixel {
32+
struct PixelRGB {
33+
union {
34+
struct {
35+
unsigned char red, green, blue;
36+
};
37+
struct {
38+
unsigned char r{}, g{}, b{};
39+
};
40+
};
41+
42+
PixelRGB(unsigned char red, unsigned char green, unsigned char blue)
43+
: red(red), green(green), blue(blue) {}
44+
PixelRGB()
45+
: red(0), green(0), blue(0) {}
46+
47+
unsigned int color() const { return ((unsigned int) 0xFF) << 24 | r << 16 | g << 8 | b; }
48+
};
49+
50+
struct PixelRGBA {
3551
union {
3652
struct {
3753
unsigned char red, green, blue, alpha;
@@ -41,10 +57,12 @@ namespace vivid { namespace util {
4157
};
4258
};
4359

44-
Pixel(unsigned char alpha, unsigned char red, unsigned char green, unsigned char blue)
60+
PixelRGBA(unsigned char alpha, unsigned char red, unsigned char green, unsigned char blue)
4561
: alpha(alpha), red(red), green(green), blue(blue) {}
46-
Pixel()
62+
PixelRGBA()
4763
: alpha(0), red(0), green(0), blue(0) {}
64+
PixelRGBA(const PixelRGB& pixelRGB)
65+
: alpha(255), red(pixelRGB.red), green(pixelRGB.green), blue(pixelRGB.blue) {}
4866

4967
unsigned int color() const { return a << 24 | r << 16 | g << 8 | b; }
5068
};
@@ -53,7 +71,7 @@ namespace vivid { namespace util {
5371
unsigned int width;
5472
unsigned int height;
5573
unsigned char bitDepth;
56-
unsigned short colorFormat;
74+
unsigned int colorFormat;
5775

5876
ImageFormat()
5977
: width(0), height(0), bitDepth(0), colorFormat(0) {}
@@ -62,17 +80,22 @@ namespace vivid { namespace util {
6280
class Image {
6381
private:
6482
ImageFormat format;
65-
Pixel* pixels;
83+
unsigned char* data;
6684
public:
6785
explicit Image(const std::string& path);
6886
~Image();
6987

70-
inline const Pixel& getPixel(unsigned int& x, unsigned int& y) { return pixels[x + y * format.width]; }
71-
inline const Pixel* const getPixels() const { return pixels; }
88+
inline const PixelRGBA getPixel(unsigned int& x, unsigned int& y) {
89+
if (getColorFormat() == VIVID_IMAGE_FORMAT_RGBA)
90+
return ((PixelRGBA*) data)[x + y * format.width];
91+
if (getColorFormat() == VIVID_IMAGE_FORMAT_RGB)
92+
return PixelRGBA(((PixelRGB*) data)[x + y * format.width]);
93+
}
94+
inline const unsigned char* const getData() const { return data; }
7295
inline const unsigned int& getWidth() const { return format.width; }
7396
inline const unsigned int& getHeight() const { return format.height; }
7497
inline const unsigned char& getBitDepth() const { return format.bitDepth; }
75-
inline const unsigned short& getColorFormat() const { return format.colorFormat; }
98+
inline const unsigned int& getColorFormat() const { return format.colorFormat; }
7699

77100
inline const ImageFormat& getFormat() const { return format; }
78101
private:

main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include "image.h"
22

3+
#define LOG(x) std::cout << x << std::endl;
4+
35
int main() {
46
using namespace vivid;
57
using namespace util;
68

79
Image image("res/cartoon_goat.png");
810
LOG("Size: " << image.getWidth() << "x" << image.getHeight());
9-
const Pixel* pixels = image.getPixels();
11+
const PixelRGBA* pixels = (PixelRGBA*) image.getData();
1012

1113
int x = 0;
1214
int y = 0;
13-
Pixel pixel = pixels[x + y * image.getWidth()];
15+
PixelRGBA pixel = pixels[x + y * image.getWidth()];
1416
std::printf("%08X\n", pixel.color());
1517

1618
return 0;

0 commit comments

Comments
 (0)