From 4946235d4934a3c994a4bb36031c6fac1ec516f7 Mon Sep 17 00:00:00 2001 From: aap Date: Thu, 23 Jul 2020 12:36:43 +0200 Subject: [PATCH] fixed image problems --- src/image.cpp | 3 ++- src/tga.cpp | 33 ++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/image.cpp b/src/image.cpp index 9e695d0..4ddf82f 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -411,6 +411,7 @@ Image::convertTo32(void) } newpixels = rwNewT(uint8, newstride*this->height, MEMDUR_EVENT | ID_IMAGE); + uint8 *pixels32 = newpixels; for(int y = 0; y < this->height; y++){ uint8 *line = pixels; uint8 *newline = newpixels; @@ -429,7 +430,7 @@ Image::convertTo32(void) this->stride = newstride; this->pixels = nil; this->palette = nil; - this->setPixels(newpixels); + this->setPixels(pixels32); } void diff --git a/src/tga.cpp b/src/tga.cpp index 9d761d1..e3f4b6f 100644 --- a/src/tga.cpp +++ b/src/tga.cpp @@ -94,7 +94,7 @@ readTGA(const char *filename) assert(palDepth == 24 || palDepth == 32); }else{ depth = header.depth; - assert(depth == 24 || depth == 32); + assert(depth == 16 || depth == 24 || depth == 32); } image = Image::create(header.width, header.height, depth); @@ -125,16 +125,28 @@ readTGA(const char *filename) for(int y = 0; y < image->height; y++){ uint8 *line = pixels; for(int x = 0; x < image->width; x++){ - if(palette) - *line++ = file.readU8(); - else{ + switch(image->depth){ + case 4: + case 8: + line[0] = file.readU8(); + break; + case 16: + line[0] = file.readU8(); + line[1] = file.readU8(); + break; + case 24: line[2] = file.readU8(); line[1] = file.readU8(); line[0] = file.readU8(); - line += 3; - if(depth == 32) - *line++ = file.readU8(); + break; + case 32: + line[2] = file.readU8(); + line[1] = file.readU8(); + line[0] = file.readU8(); + line[3] = file.readU8(); + break; } + line += image->bpp; } pixels += (header.descriptor&0x20) ? image->stride : -image->stride; @@ -187,26 +199,25 @@ writeTGA(Image *image, const char *filename) switch(image->depth){ case 4: case 8: - file.writeU8(*p++); + file.writeU8(p[0]); break; case 16: file.writeU8(p[0]); file.writeU8(p[1]); - p += 2; break; case 24: file.writeU8(p[2]); file.writeU8(p[1]); file.writeU8(p[0]); - p += 3; break; case 32: file.writeU8(p[2]); file.writeU8(p[1]); file.writeU8(p[0]); file.writeU8(p[3]); - p += 4; + break; } + p += image->bpp; } line += image->stride; }