diff --git a/src/textures/pcxtexture.cpp b/src/textures/pcxtexture.cpp index 76d933611..4d7e9d94f 100644 --- a/src/textures/pcxtexture.cpp +++ b/src/textures/pcxtexture.cpp @@ -111,6 +111,7 @@ FTexture * PCXTexture_TryCreate(FileReader & file, int lumpnum) { PCXHeader hdr; + file.Seek(0, FileReader::SeekSet); if (file.Read(&hdr, sizeof(hdr)) != sizeof(hdr)) { @@ -123,8 +124,9 @@ FTexture * PCXTexture_TryCreate(FileReader & file, int lumpnum) if (hdr.manufacturer != 10 || hdr.encoding != 1) return NULL; if (hdr.version != 0 && hdr.version != 2 && hdr.version != 3 && hdr.version != 4 && hdr.version != 5) return NULL; - if (hdr.bitsPerPixel != 1 && hdr.bitsPerPixel != 8) return NULL; + if (hdr.bitsPerPixel != 1 && hdr.bitsPerPixel != 8 && hdr.bitsPerPixel != 4) return NULL; if (hdr.bitsPerPixel == 1 && hdr.numColorPlanes !=1 && hdr.numColorPlanes != 4) return NULL; + if (hdr.bitsPerPixel == 4 && hdr.numColorPlanes != 1) return NULL; if (hdr.bitsPerPixel == 8 && hdr.bytesPerScanLine != ((hdr.xmax - hdr.xmin + 2)&~1)) return NULL; for (int i = 0; i < 54; i++) @@ -205,7 +207,9 @@ void FPCXTexture::ReadPCX1bit (uint8_t *dst, FileReader & lump, PCXHeader *hdr) for (i = 7; i >= 0; --i, ptr ++) { - *ptr = ((rle_value & (1 << i)) > 0); + // This can overflow for the last byte if not checked. + if (ptr < dst+Width*Height) + *ptr = ((rle_value & (1 << i)) > 0); } } } @@ -259,13 +263,13 @@ void FPCXTexture::ReadPCX4bits (uint8_t *dst, FileReader & lump, PCXHeader *hdr) rle_count--; *(pLine++) = rle_value; } + } - /* compute line's color indexes */ - for (x = 0; x < Width; ++x) - { - if (line[x / 8] & (128 >> (x % 8))) - ptr[x] += (1 << c); - } + /* compute line's color indexes */ + for (x = 0; x < Width; ++x) + { + if (line[x / 8] & (128 >> (x % 8))) + ptr[x] += (1 << c); } } @@ -479,19 +483,20 @@ int FPCXTexture::CopyTrueColorPixels(FBitmap *bmp, int x, int y, int rotate, FCo Pixels = new uint8_t[Width*Height]; if (bitcount < 8) { - for (int i=0;i<16;i++) - { - pe[i] = PalEntry(header.palette[i*3],header.palette[i*3+1],header.palette[i*3+2]); - } - switch (bitcount) { default: case 1: + pe[0] = PalEntry(255, 0, 0, 0); + pe[1] = PalEntry(255, 255, 255, 255); ReadPCX1bit (Pixels, lump, &header); break; case 4: + for (int i = 0; i<16; i++) + { + pe[i] = PalEntry(255, header.palette[i * 3], header.palette[i * 3 + 1], header.palette[i * 3 + 2]); + } ReadPCX4bits (Pixels, lump, &header); break; }