#include <SDL3/SDL_endian.h>
#include <SDL3_image/SDL_image.h>
#ifdef LOAD_PCX
struct PCXheader {
Uint8 Manufacturer;
Uint8 Version;
Uint8 Encoding;
Uint8 BitsPerPixel;
Sint16 Xmin, Ymin, Xmax, Ymax;
Sint16 HDpi, VDpi;
Uint8 Colormap[48];
Uint8 Reserved;
Uint8 NPlanes;
Sint16 BytesPerLine;
Sint16 PaletteInfo;
Sint16 HscreenSize;
Sint16 VscreenSize;
Uint8 Filler[54];
};
bool IMG_isPCX(SDL_IOStream *src)
{
Sint64 start;
bool is_PCX;
const int ZSoft_Manufacturer = 10;
const int PC_Paintbrush_Version = 5;
const int PCX_Uncompressed_Encoding = 0;
const int PCX_RunLength_Encoding = 1;
struct PCXheader pcxh;
if (!src) {
return false;
}
start = SDL_TellIO(src);
is_PCX = false;
if (SDL_ReadIO(src, &pcxh, sizeof(pcxh)) == sizeof(pcxh) ) {
if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
(pcxh.Version == PC_Paintbrush_Version) &&
(pcxh.Encoding == PCX_RunLength_Encoding ||
pcxh.Encoding == PCX_Uncompressed_Encoding) ) {
is_PCX = true;
}
}
SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
return is_PCX;
}
SDL_Surface *IMG_LoadPCX_IO(SDL_IOStream *src)
{
Sint64 start;
struct PCXheader pcxh;
SDL_Surface *surface = NULL;
int width, height;
int y;
size_t bpl;
Uint8 *row, *buf = NULL;
char *error = NULL;
int bits, src_bits;
int count = 0;
Uint8 ch;
Uint32 format;
if ( !src ) {
return NULL;
}
start = SDL_TellIO(src);
if (SDL_ReadIO(src, &pcxh, sizeof(pcxh)) != sizeof(pcxh) ) {
error = "file truncated";
goto done;
}
pcxh.Xmin = SDL_Swap16LE(pcxh.Xmin);
pcxh.Ymin = SDL_Swap16LE(pcxh.Ymin);
pcxh.Xmax = SDL_Swap16LE(pcxh.Xmax);
pcxh.Ymax = SDL_Swap16LE(pcxh.Ymax);
pcxh.BytesPerLine = SDL_Swap16LE(pcxh.BytesPerLine);
#if 0#endif
width = (pcxh.Xmax - pcxh.Xmin) + 1;
height = (pcxh.Ymax - pcxh.Ymin) + 1;
src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
|| (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
bits = 8;
format = SDL_PIXELFORMAT_INDEX8;
} else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
bits = 24;
format = SDL_PIXELFORMAT_RGB24;
} else {
error = "unsupported PCX format";
goto done;
}
surface = SDL_CreateSurface(width, height, format);
if ( surface == NULL ) {
goto done;
}
bpl = pcxh.NPlanes * pcxh.BytesPerLine;
buf = (Uint8 *)SDL_calloc(bpl, 1);
if ( !buf ) {
error = "Out of memory";
goto done;
}
row = (Uint8 *)surface->pixels;
for ( y=0; y<surface->h; ++y ) {
size_t i;
if ( pcxh.Encoding == 0 ) {
if (SDL_ReadIO(src, buf, bpl) != bpl ) {
error = "file truncated";
goto done;
}
} else {
for ( i = 0; i < bpl; i++ ) {
if ( !count ) {
if (SDL_ReadIO(src, &ch, 1) != 1 ) {
error = "file truncated";
goto done;
}
if ( ch < 0xc0 ) {
count = 1;
} else {
count = ch - 0xc0;
if (SDL_ReadIO(src, &ch, 1) != 1 ) {
error = "file truncated";
goto done;
}
}
}
buf[i] = ch;
count--;
}
}
if ( src_bits <= 4 ) {
Uint8 *innerSrc = buf;
int plane;
for ( plane = 0; plane < pcxh.NPlanes; plane++ ) {
int j, k, x = 0;
for( j = 0; j < pcxh.BytesPerLine; j++ ) {
Uint8 byte = *innerSrc++;
for( k = 7; k >= 0; k-- ) {
unsigned bit = (byte >> k) & 1;
if (j * 8 + k >= width)
continue;
row[x++] |= bit << plane;
}
}
}
} else if ( src_bits == 8 ) {
SDL_memcpy(row, buf, SDL_min((size_t)width, bpl));
} else if ( src_bits == 24 ) {
Uint8 *innerSrc = buf;
Uint8 *end1 = buf+bpl;
int plane;
for ( plane = 0; plane < pcxh.NPlanes; plane++ ) {
int x;
Uint8 *dst = row + plane;
Uint8 *end2= row + surface->pitch;
for ( x = 0; x < width; x++ ) {
if ( (innerSrc + x) >= end1 || dst >= end2 ) {
error = "decoding out of bounds (corrupt?)";
goto done;
}
*dst = innerSrc[x];
dst += pcxh.NPlanes;
}
innerSrc += pcxh.BytesPerLine;
}
}
row += surface->pitch;
}
if ( bits == 8 ) {
int nc = 1 << src_bits;
SDL_Palette *palette;
int i;
palette = SDL_CreateSurfacePalette(surface);
if (!palette) {
error = "Couldn't create palette";
goto done;
}
if (nc > palette->ncolors) {
nc = palette->ncolors;
}
palette->ncolors = nc;
if ( src_bits == 8 ) {
Uint8 pch;
Uint8 colormap[768];
do {
if (SDL_ReadIO(src, &pch, 1) != 1 ) {
SDL_SeekIO(src, -768, SDL_IO_SEEK_END);
break;
}
} while ( pch != 12 );
if (SDL_ReadIO(src, colormap, sizeof(colormap)) != sizeof(colormap) ) {
error = "file truncated";
goto done;
}
for ( i = 0; i < 256; i++ ) {
palette->colors[i].r = colormap[i * 3 + 0];
palette->colors[i].g = colormap[i * 3 + 1];
palette->colors[i].b = colormap[i * 3 + 2];
}
} else {
for ( i = 0; i < nc; i++ ) {
palette->colors[i].r = pcxh.Colormap[i * 3 + 0];
palette->colors[i].g = pcxh.Colormap[i * 3 + 1];
palette->colors[i].b = pcxh.Colormap[i * 3 + 2];
}
}
}
done:
SDL_free(buf);
if ( error ) {
SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
if ( surface ) {
SDL_DestroySurface(surface);
surface = NULL;
}
SDL_SetError("%s", error);
}
return surface;
}
#else
bool IMG_isPCX(SDL_IOStream *src)
{
return false;
}
SDL_Surface *IMG_LoadPCX_IO(SDL_IOStream *src)
{
SDL_SetError("SDL_image built without PCX support");
return NULL;
}
#endif