@@ -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) {
0 commit comments