Imagelib
Imagelib
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// imagelib.c
#include "cmdlib.h"
#include "imagelib.h"
b1 = fgetc(f);
b2 = fgetc(f);
b1 = fgetc(f);
b2 = fgetc(f);
b3 = fgetc(f);
b4 = fgetc(f);
/*
============================================================================
============================================================================
*/
typedef enum
{
ms_none,
ms_mask,
ms_transcolor,
ms_lasso
} mask_t;
typedef enum
{
cm_none,
cm_rle1
} compress_t;
typedef struct
{
UWORD w,h;
short x,y;
UBYTE nPlanes;
UBYTE masking;
UBYTE compression;
UBYTE pad1;
UWORD transparentColor;
UBYTE xAspect,yAspect;
short pageWidth,pageHeight;
} bmhd_t;
bmhd_t bmhd;
/*
================
LBMRLEdecompress
count = 0;
do
{
rept = *source++;
count += rept;
} while (count<bpwidth);
if (count>bpwidth)
Error ("Decompression exceeded width!\n");
return source;
}
int formtype,formlength;
int chunktype,chunklength;
//
// load the LBM
//
LoadFile (filename, (void **)&LBMbuffer);
//
// parse the LBM header
//
LBM_P = LBMbuffer;
if ( *(int *)LBMbuffer != LittleLong(FORMID) )
Error ("No FORM ID at start of file!\n");
LBM_P += 4;
formlength = BigLong( *(int *)LBM_P );
LBM_P += 4;
LBMEND_P = LBM_P + Align(formlength);
LBM_P += 4;
//
// parse chunks
//
switch ( chunktype )
{
case BMHDID:
memcpy (&bmhd,LBM_P,sizeof(bmhd));
bmhd.w = BigShort(bmhd.w);
bmhd.h = BigShort(bmhd.h);
bmhd.x = BigShort(bmhd.x);
bmhd.y = BigShort(bmhd.y);
bmhd.pageWidth = BigShort(bmhd.pageWidth);
bmhd.pageHeight = BigShort(bmhd.pageHeight);
break;
case CMAPID:
cmapbuffer = malloc (768);
memset (cmapbuffer, 0, 768);
memcpy (cmapbuffer, LBM_P, chunklength);
break;
case BODYID:
body_p = LBM_P;
}
else
{
//
// unpack ILBM
//
Error ("%s is an interlaced LBM, not packed", filename);
}
break;
}
LBM_P += Align(chunklength);
free (LBMbuffer);
*picture = picbuffer;
if (palette)
*palette = cmapbuffer;
}
/*
============================================================================
WRITE LBM
============================================================================
*/
/*
==============
WriteLBMfile
==============
*/
void WriteLBMfile (const char *filename, byte *data,
int width, int height, byte *palette)
{
byte *lbm, *lbmptr;
int *formlength, *bmhdlength, *cmaplength, *bodylength;
int length;
bmhd_t basebmhd;
//
// start FORM
//
*lbmptr++ = 'F';
*lbmptr++ = 'O';
*lbmptr++ = 'R';
*lbmptr++ = 'M';
formlength = (int*)lbmptr;
lbmptr+=4; // leave space for length
*lbmptr++ = 'P';
*lbmptr++ = 'B';
*lbmptr++ = 'M';
*lbmptr++ = ' ';
//
// write BMHD
//
*lbmptr++ = 'B';
memset (&basebmhd,0,sizeof(basebmhd));
basebmhd.w = BigShort((short)width);
basebmhd.h = BigShort((short)height);
basebmhd.nPlanes = BigShort(8);
basebmhd.xAspect = BigShort(5);
basebmhd.yAspect = BigShort(6);
basebmhd.pageWidth = BigShort((short)width);
basebmhd.pageHeight = BigShort((short)height);
memcpy (lbmptr,&basebmhd,sizeof(basebmhd));
lbmptr += sizeof(basebmhd);
//
// write CMAP
//
*lbmptr++ = 'C';
*lbmptr++ = 'M';
*lbmptr++ = 'A';
*lbmptr++ = 'P';
memcpy (lbmptr,palette,768);
lbmptr += 768;
//
// write BODY
//
*lbmptr++ = 'B';
*lbmptr++ = 'O';
*lbmptr++ = 'D';
*lbmptr++ = 'Y';
//
// done
//
length = lbmptr-(byte *)formlength-4;
*formlength = BigLong(length);
if (length&1)
*lbmptr++ = 0; // pad chunk to even offset
//
// write output file
//
SaveFile (filename, lbm, lbmptr-lbm);
free (lbm);
}
/*
============================================================================
LOAD PCX
============================================================================
*/
typedef struct
{
char manufacturer;
char version;
char encoding;
char bits_per_pixel;
unsigned short xmin,ymin,xmax,ymax;
unsigned short hres,vres;
unsigned char palette[48];
char reserved;
char color_planes;
unsigned short bytes_per_line;
unsigned short palette_type;
char filler[58];
unsigned char data; // unbounded
} pcx_t;
/*
==============
LoadPCX
==============
//
// load the file
//
len = LoadFile (filename, (void **)&raw);
//
// parse the PCX file
//
pcx = (pcx_t *)raw;
raw = &pcx->data;
pcx->xmin = LittleShort(pcx->xmin);
pcx->ymin = LittleShort(pcx->ymin);
pcx->xmax = LittleShort(pcx->xmax);
pcx->ymax = LittleShort(pcx->ymax);
pcx->hres = LittleShort(pcx->hres);
pcx->vres = LittleShort(pcx->vres);
pcx->bytes_per_line = LittleShort(pcx->bytes_per_line);
pcx->palette_type = LittleShort(pcx->palette_type);
if (pcx->manufacturer != 0x0a
|| pcx->version != 5
|| pcx->encoding != 1
|| pcx->bits_per_pixel != 8
|| pcx->xmax >= 640
|| pcx->ymax >= 480)
Error ("Bad pcx file %s", filename);
if (palette)
{
*palette = malloc(768);
memcpy (*palette, (byte *)pcx + len - 768, 768);
}
if (width)
*width = pcx->xmax+1;
if (height)
*height = pcx->ymax+1;
if (!pic)
return;
*pic = out;
pix = out;
// FIXME: this shouldn't happen, but it does. Are we decoding the file wrong?
// Truncate runLength so we don't overrun the end of the buffer
if ( ( y == pcx->ymax ) && ( x + runLength > pcx->xmax + 1 ) ) {
runLength = pcx->xmax - x + 1;
}
while(runLength-- > 0)
pix[x++] = dataByte;
}
free (pcx);
}
/*
==============
WritePCXfile
==============
*/
void WritePCXfile (const char *filename, byte *data,
int width, int height, byte *palette)
{
int i, j, length;
pcx_t *pcx;
byte *pack;
free (pcx);
}
/*
============================================================================
LOAD BMP
============================================================================
*/
/*
*/
/*
==============
LoadBMP
==============
*/
void LoadBMP (const char *filename, byte **pic, byte **palette, int *width, int *height)
{
byte *out;
FILE *fin;
i = fgetLittleShort (fin);
if (i != 'B' + ('M'<<8) ) {
Error ("%s is not a bmp file", filename);
}
if (palette) {
fread (bcPalette, 1, 1024, fin);
*palette = malloc(768);
if (bcPlanes != 1) {
Error ("%s was not a single plane image", filename);
}
if (bcBitCount != 8) {
Error ("%s was not an 8 bit image", filename);
}
if (bcHeight < 0) {
bcHeight = -bcHeight;
flipped = qtrue;
} else {
flipped = qfalse;
}
if (width)
*width = bcWidth;
if (height)
*height = bcHeight;
if (!pic) {
fclose (fin);
return;
}
if (flipped) {
for (i = 0 ; i < bcHeight ; i++) {
fread (out + bcWidth * (bcHeight - 1 - i), 1, bcWidth, fin);
}
} else {
fread (out, 1, bcWidth*bcHeight, fin);
}
fclose (fin);
}
LOAD IMAGE
============================================================================
*/
/*
==============
Load256Image
/*
==============
Save256Image
/*
============================================================================
TARGA IMAGE
============================================================================
*/
/*
=============
LoadTGABuffer
=============
*/
void LoadTGABuffer ( byte *buffer, byte **pic, int *width, int *height)
{
int columns, rows, numPixels;
byte *pixbuf;
int row, column;
byte *buf_p;
TargaHeader targa_header;
byte *targa_rgba;
*pic = NULL;
buf_p = buffer;
targa_header.id_length = *buf_p++;
if (targa_header.image_type!=2
&& targa_header.image_type!=10
&& targa_header.image_type != 3 )
{
Error("LoadTGA: Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n");
}
if ( targa_header.colormap_type != 0 )
{
Error("LoadTGA: colormaps not supported\n" );
}
columns = targa_header.width;
rows = targa_header.height;
numPixels = columns * rows;
if (width)
*width = columns;
if (height)
*height = rows;
if (targa_header.id_length != 0)
buf_p += targa_header.id_length; // skip TARGA image comment
if ( targa_header.image_type==2 || targa_header.image_type == 3 )
{
// Uncompressed RGB or gray scale image
case 24:
blue = *buf_p++;
green = *buf_p++;
red = *buf_p++;
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = *buf_p++;
green = *buf_p++;
red = *buf_p++;
alphabyte = *buf_p++;
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
break;
default:
//Error("LoadTGA: illegal pixel_size '%d' in file '%s'\n", targa_header.pixel_size, name );
break;
}
}
}
}
else if (targa_header.image_type==10) { // Runlength encoded RGB images
unsigned char red,green,blue,alphabyte,packetHeader,packetSize,j;
red = 0;
green = 0;
blue = 0;
alphabyte = 0xff;
for(j=0;j<packetSize;j++) {
*pixbuf++=red;
*pixbuf++=green;
*pixbuf++=blue;
*pixbuf++=alphabyte;
column++;
if (column==columns) { // run spans across rows
column=0;
if (row>0)
row--;
else
goto breakOut;
pixbuf = targa_rgba + row*columns*4;
}
}
}
else { // non run-length packet
for(j=0;j<packetSize;j++) {
switch (targa_header.pixel_size) {
case 24:
blue = *buf_p++;
green = *buf_p++;
red = *buf_p++;
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = *buf_p++;
//free(buffer);
}
/*
=============
LoadTGA
=============
*/
void LoadTGA (const char *name, byte **pixels, int *width, int *height)
{
byte *buffer;
int nLen;
//
// load the file
//
nLen = LoadFile ( ( char * ) name, (void **)&buffer);
if (nLen == -1)
{
Error ("Couldn't read %s", name);
}
/*
================
WriteTGA
================
*/
void WriteTGA (const char *filename, byte *data, int width, int height) {
byte *buffer;
int i;
int c;
FILE *f;
free (buffer);
}
/*
============================================================================
LOAD32BITIMAGE
============================================================================
*/
/*
==============
Load32BitImage
Any of the return pointers can be NULL if you don't want them.
==============