#include "quakedef.h" int image_width; int image_height; /* ================================================================= PCX Loading ================================================================= */ 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 data; // unbounded } pcx_t; /* ============ LoadPCX ============ */ byte* LoadPCX (FILE *f, int matchwidth, int matchheight) { pcx_t *pcx, pcxbuf; byte palette[768]; byte *pix, *image_rgba; int x, y; int dataByte, runLength; int count; // // parse the PCX file // fread (&pcxbuf, 1, sizeof(pcxbuf), f); pcx = &pcxbuf; if (pcx->manufacturer != 0x0a || pcx->version != 5 || pcx->encoding != 1 || pcx->bits_per_pixel != 8 || pcx->xmax >= 320 || pcx->ymax >= 256) { Con_Printf ("Bad pcx file\n"); return NULL; } if (matchwidth && (pcx->xmax+1) != matchwidth) return NULL; if (matchheight && (pcx->ymax+1) != matchheight) return NULL; // seek to palette fseek (f, -768, SEEK_END); fread (palette, 1, 768, f); fseek (f, sizeof(pcxbuf) - 4, SEEK_SET); count = (pcx->xmax+1) * (pcx->ymax+1); image_rgba = malloc( count * 4); for (y=0 ; y<=pcx->ymax ; y++) { pix = image_rgba + 4*y*(pcx->xmax+1); for (x=0 ; x<=pcx->xmax ; ) { dataByte = fgetc(f); if((dataByte & 0xC0) == 0xC0) { runLength = dataByte & 0x3F; dataByte = fgetc(f); } else runLength = 1; while(runLength-- > 0) { pix[0] = palette[dataByte*3]; pix[1] = palette[dataByte*3+1]; pix[2] = palette[dataByte*3+2]; pix[3] = 255; pix += 4; x++; } } } image_width = pcx->xmax+1; image_height = pcx->ymax+1; return image_rgba; } /* ========================================================= TARGA LOADING ========================================================= */ typedef struct _TargaHeader { unsigned char id_length, colormap_type, image_type; unsigned short colormap_index, colormap_length; unsigned char colormap_size; unsigned short x_origin, y_origin, width, height; unsigned char pixel_size, attributes; } TargaHeader; TargaHeader targa_header; int fgetLittleShort (FILE *f) { byte b1, b2; b1 = fgetc(f); b2 = fgetc(f); return (short)(b1 + b2*256); } int fgetLittleLong (FILE *f) { byte b1, b2, b3, b4; b1 = fgetc(f); b2 = fgetc(f); b3 = fgetc(f); b4 = fgetc(f); return b1 + (b2<<8) + (b3<<16) + (b4<<24); } /* ============= LoadTGA ============= */ byte* LoadTGA (FILE *fin, int matchwidth, int matchheight) { int columns, rows, numPixels; byte *pixbuf; int row, column; byte *image_rgba; targa_header.id_length = fgetc(fin); targa_header.colormap_type = fgetc(fin); targa_header.image_type = fgetc(fin); targa_header.colormap_index = fgetLittleShort(fin); targa_header.colormap_length = fgetLittleShort(fin); targa_header.colormap_size = fgetc(fin); targa_header.x_origin = fgetLittleShort(fin); targa_header.y_origin = fgetLittleShort(fin); targa_header.width = fgetLittleShort(fin); targa_header.height = fgetLittleShort(fin); if (matchwidth && targa_header.width != matchwidth) return NULL; if (matchheight && targa_header.height != matchheight) return NULL; targa_header.pixel_size = fgetc(fin); targa_header.attributes = fgetc(fin); if (targa_header.image_type!=2 && targa_header.image_type!=10) Sys_Error ("LoadTGA: Only type 2 and 10 targa RGB images supported\n"); if (targa_header.colormap_type !=0 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24)) Sys_Error ("Texture_LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n"); columns = targa_header.width; rows = targa_header.height; numPixels = columns * rows; image_rgba = malloc (numPixels*4); if (targa_header.id_length != 0) fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment if (targa_header.image_type==2) { // Uncompressed, RGB images for(row=rows-1; row>=0; row--) { pixbuf = image_rgba + row*columns*4; for(column=0; column=0; row--) { pixbuf = image_rgba + row*columns*4; for(column=0; column0) row--; else goto breakOut; pixbuf = image_rgba + row*columns*4; } } } else { // non run-length packet for(j=0;j0) row--; else goto breakOut; pixbuf = image_rgba + row*columns*4; } } } } breakOut:; } } fclose(fin); image_width = columns; image_height = rows; return image_rgba; } byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int matchheight) { FILE *f; char basename[128], name[128]; byte *image_rgba; COM_StripExtension(filename, basename); // strip the extension to allow TGA skins on Q2 models despite the .pcx in the skin name sprintf (name, "textures/%s.tga", basename); COM_FOpenFile (name, &f, true); if (f) return LoadTGA (f, matchwidth, matchheight); sprintf (name, "textures/%s.pcx", basename); COM_FOpenFile (name, &f, true); if (f) return LoadPCX (f, matchwidth, matchheight); sprintf (name, "%s.tga", basename); COM_FOpenFile (name, &f, true); if (f) return LoadTGA (f, matchwidth, matchheight); sprintf (name, "%s.pcx", basename); COM_FOpenFile (name, &f, true); if (f) return LoadPCX (f, matchwidth, matchheight); if (image_rgba = W_GetTexture(basename, matchwidth, matchheight)) return image_rgba; if (complain) Con_Printf ("Couldn't load %s.tga or .pcx\n", filename); return NULL; } int loadtextureimage (int texnum, char* filename, qboolean complain, int matchwidth, int matchheight) { byte *data; if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight))) return 0; if (texnum >= 0) // specific texnum, not cached { glBindTexture(GL_TEXTURE_2D, texnum); GL_Upload32 (data, image_width, image_height, true, true); free(data); return texnum; } else // any texnum, cached { texnum = GL_LoadTexture (filename, image_width, image_height, data, true, true, 4); free(data); return texnum; } }