29 #include "../idlib/precompiled.h"
59 va_start (argptr,fmt);
70 va_start (argptr,fmt);
88 int bufferSize = width*height*4 + 18;
92 memset( buffer, 0, 18 );
94 buffer[12] = width&255;
95 buffer[13] = width>>8;
96 buffer[14] = height&255;
97 buffer[15] = height>>8;
99 if ( !flipVertical ) {
104 for ( i=imgStart ; i<bufferSize ; i+=4 ) {
105 buffer[
i] = data[i-imgStart+2];
106 buffer[i+1] = data[i-imgStart+1];
107 buffer[i+2] = data[i-imgStart+0];
108 buffer[i+3] = data[i-imgStart+3];
125 int bufferSize = (width *
height) + (256 * 3) + 18;
127 int imgStart = 18 + (256 * 3);
130 memset( buffer, 0, 18 );
136 buffer[12] = width&255;
137 buffer[13] = width>>8;
138 buffer[14] = height&255;
139 buffer[15] = height>>8;
141 if ( !flipVertical ) {
146 for ( i=palStart ; i<imgStart ; i+=3 ) {
147 buffer[
i] = palette[i-palStart+2];
148 buffer[i+1] = palette[i-palStart+1];
149 buffer[i+2] = palette[i-palStart+0];
153 for ( i=imgStart ; i<bufferSize ; i++ ) {
154 buffer[
i] = data[i-imgStart];
163 static void LoadBMP(
const char *
name,
byte **pic,
int *
width,
int *
height, ID_TIME_T *timestamp );
164 static void LoadTGA(
const char *
name,
byte **pic,
int *
width,
int *
height, ID_TIME_T *timestamp );
181 unsigned short xmin,
ymin,xmax,ymax;
183 unsigned char palette[48];
235 unsigned char palette[256][4];
243 static void LoadBMP(
const char *
name,
byte **pic,
int *
width,
int *
height, ID_TIME_T *timestamp )
245 int columns, rows, numPixels;
271 bmpHeader.
id[0] = *buf_p++;
272 bmpHeader.
id[1] = *buf_p++;
307 if ( bmpHeader.
id[0] !=
'B' && bmpHeader.
id[1] !=
'M' )
309 common->
Error(
"LoadBMP: only Windows-style BMP files supported (%s)\n", name );
313 common->
Error(
"LoadBMP: header size does not match file size (%lu vs. %d) (%s)\n", bmpHeader.
fileSize, length, name );
317 common->
Error(
"LoadBMP: only uncompressed BMP files supported (%s)\n", name );
321 common->
Error(
"LoadBMP: monochrome and 4-bit BMP files not supported (%s)\n", name );
324 columns = bmpHeader.
width;
328 numPixels = columns * rows;
339 for ( row = rows-1; row >= 0; row-- )
341 pixbuf = bmpRGBA + row*columns*4;
343 for ( column = 0; column < columns; column++ )
347 unsigned short shortPixel;
353 *pixbuf++ = bmpHeader.
palette[palIndex][2];
354 *pixbuf++ = bmpHeader.
palette[palIndex][1];
355 *pixbuf++ = bmpHeader.
palette[palIndex][0];
359 shortPixel = * (
unsigned short * ) pixbuf;
361 *pixbuf++ = ( shortPixel & ( 31 << 10 ) ) >> 7;
362 *pixbuf++ = ( shortPixel & ( 31 << 5 ) ) >> 2;
363 *pixbuf++ = ( shortPixel & ( 31 ) ) << 3;
412 static void LoadPCX (
const char *filename,
byte **pic,
byte **palette,
int *width,
int *height,
413 ID_TIME_T *timestamp ) {
418 int dataByte, runLength;
454 common->
Printf(
"Bad pcx file %s (%i x %i) (%i x %i)\n", filename, xmax+1, ymax+1, pcx->
xmax, pcx->
ymax);
467 memcpy (*palette, (
byte *)pcx + len - 768, 768);
476 for (y=0 ; y<=ymax ; y++, pix += xmax+1)
478 for (x=0 ; x<=xmax ; )
482 if((dataByte & 0xC0) == 0xC0)
484 runLength = dataByte & 0x3F;
490 while(runLength-- > 0)
496 if ( raw - (
byte *)pcx > len)
498 common->
Printf(
"PCX file %s was malformed", filename );
512 static void LoadPCX32 (
const char *filename,
byte **pic,
int *width,
int *height, ID_TIME_T *timestamp) {
522 LoadPCX (filename, &pic8, &palette, width, height, timestamp);
528 c = (*width) * (*height);
530 for (i = 0 ; i <
c ; i++) {
532 pic32[0] = palette[p*3];
533 pic32[1] = palette[p*3 + 1];
534 pic32[2] = palette[p*3 + 2];
556 static void LoadTGA(
const char *name,
byte **pic,
int *width,
int *height, ID_TIME_T *timestamp ) {
557 int columns, rows, numPixels, fileSize, numBytes;
603 common->
Error(
"LoadTGA( %s ): Only type 2 (RGB), 3 (gray), and 10 (RGB) TGA images supported\n", name );
607 common->
Error(
"LoadTGA( %s ): colormaps not supported\n", name );
611 common->
Error(
"LoadTGA( %s ): Only 32 or 24 bit images supported (no colormaps)\n", name );
616 if ( numBytes > fileSize - 18 - targa_header.
id_length ) {
617 common->
Error(
"LoadTGA( %s ): incomplete file\n", name );
621 columns = targa_header.
width;
622 rows = targa_header.
height;
623 numPixels = columns * rows;
642 for( row = rows - 1; row >= 0; row-- )
644 pixbuf = targa_rgba + row*columns*4;
645 for( column = 0; column < columns; column++)
674 alphabyte = *buf_p++;
678 *pixbuf++ = alphabyte;
688 unsigned char red,
green,
blue,alphabyte,packetHeader,packetSize,
j;
695 for( row = rows - 1; row >= 0; row-- ) {
696 pixbuf = targa_rgba + row*columns*4;
697 for( column = 0; column < columns; ) {
698 packetHeader= *buf_p++;
699 packetSize = 1 + (packetHeader & 0x7f);
700 if ( packetHeader & 0x80 ) {
712 alphabyte = *buf_p++;
719 for( j = 0; j < packetSize; j++ ) {
725 if ( column == columns ) {
733 pixbuf = targa_rgba + row*columns*4;
738 for( j = 0; j < packetSize; j++ ) {
753 alphabyte = *buf_p++;
757 *pixbuf++ = alphabyte;
764 if ( column == columns ) {
772 pixbuf = targa_rgba + row*columns*4;
802 static void LoadJPG(
const char *filename,
unsigned char **pic,
int *width,
int *height, ID_TIME_T *timestamp ) {
855 f->
Read( fbuffer, len );
905 row_stride = cinfo.output_width * cinfo.output_components;
907 if (cinfo.output_components!=4) {
909 filename, cinfo.output_components);
914 *width = cinfo.output_width;
915 *height = cinfo.output_height;
923 while (cinfo.output_scanline < cinfo.output_height) {
928 bbuf = ((out+(row_stride*cinfo.output_scanline)));
940 j = cinfo.output_width * cinfo.output_height * 4;
941 for ( i = 3 ; i <
j ; i+=4 ) {
998 void R_LoadImage(
const char *cname,
byte **pic,
int *width,
int *height, ID_TIME_T *timestamp,
bool makePowerOf2 ) {
1005 *timestamp = 0xFFFFFFFF;
1024 if ( ext ==
"tga" ) {
1026 if ( ( pic && *pic == 0 ) || ( timestamp && *timestamp == -1 ) ) {
1031 }
else if ( ext ==
"pcx" ) {
1033 }
else if ( ext ==
"bmp" ) {
1035 }
else if ( ext ==
"jpg" ) {
1039 if ( ( width && *width < 1 ) || ( height && *height < 1 ) ) {
1040 if ( pic && *pic ) {
1049 if ( pic && *pic && makePowerOf2 ) {
1051 int scaled_width, scaled_height;
1052 byte *resampledBuffer;
1057 for (scaled_width = 1 ; scaled_width <
w ; scaled_width<<=1)
1059 for (scaled_height = 1 ; scaled_height < h ; scaled_height<<=1)
1062 if ( scaled_width != w || scaled_height != h ) {
1067 scaled_height >>= 1;
1072 *pic = resampledBuffer;
1073 *width = scaled_width;
1074 *height = scaled_height;
1089 char *cameraSides[6] = {
"_forward.tga",
"_back.tga",
"_left.tga",
"_right.tga",
1090 "_up.tga",
"_down.tga" };
1091 char *axisSides[6] = {
"_px.tga",
"_nx.tga",
"_py.tga",
"_ny.tga",
1092 "_pz.tga",
"_nz.tga" };
1098 sides = cameraSides;
1105 memset( pics, 0, 6*
sizeof(pics[0]) );
1111 for ( i = 0 ; i < 6 ; i++ ) {
1112 idStr::snPrintf( fullName,
sizeof( fullName ),
"%s%s", imgName, sides[i] );
1121 if ( thisTime == FILE_NOT_FOUND_TIMESTAMP ) {
1127 if ( width != size || height != size ) {
1128 common->
Warning(
"Mismatched sizes on cube map '%s'", imgName );
1132 if ( thisTime > *timestamp ) {
1133 *timestamp = thisTime;
1136 if ( pics && extensions ==
CF_CAMERA ) {
1166 for ( j = 0 ; j <
i ; j++ ) {
bool R_LoadCubeImages(const char *imgName, cubeFiles_t extensions, byte *pics[6], int *outSize, ID_TIME_T *timestamp)
void jpg_Printf(const char *fmt,...)
void R_LoadImage(const char *cname, byte **pic, int *width, int *height, ID_TIME_T *timestamp, bool makePowerOf2)
unsigned short palette_type
virtual idFile * OpenFileRead(const char *relativePath, bool allowCopyFiles=true, const char *gamedir=NULL)=0
static int snPrintf(char *dest, int size, const char *fmt,...) id_attribute((format(printf
void R_WritePalTGA(const char *filename, const byte *data, const byte *palette, int width, int height, bool flipVertical)
GLOBAL boolean jpeg_start_decompress(j_decompress_ptr cinfo)
virtual int ReadFile(const char *relativePath, void **buffer, ID_TIME_T *timestamp=NULL)=0
int LoadJPG(const char *filename, unsigned char **pic, int *width, int *height)
idFileSystem * fileSystem
GLclampf GLclampf GLclampf alpha
idStr & DefaultFileExtension(const char *extension)
void R_LoadImageProgram(const char *name, byte **pic, int *width, int *height, ID_TIME_T *timestamp, textureDepth_t *depth=NULL)
virtual void FreeFile(void *buffer)=0
struct jpeg_error_mgr jerr
virtual int WriteFile(const char *relativePath, const void *buffer, int size, const char *basePath="fs_savepath")=0
GLOBAL void jpeg_create_decompress(j_decompress_ptr cinfo)
GLOBAL JDIMENSION jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines, JDIMENSION max_lines)
GLubyte GLubyte GLubyte GLubyte w
idStr & StripFileExtension(void)
virtual void virtual void virtual void DWarning(const char *fmt,...) id_attribute((format(printf
byte * R_ResampleTexture(const byte *in, int inwidth, int inheight, int outwidth, int outheight)
GLsizei GLsizei GLenum GLenum const GLvoid * data
idImageManager * globalImages
void R_HorizontalFlip(byte *data, int width, int height)
virtual void virtual void FatalError(const char *fmt,...) id_attribute((format(printf
virtual int Read(void *buffer, int len)
unsigned short bytes_per_line
virtual void Printf(const char *fmt,...) id_attribute((format(printf
GLOBAL boolean jpeg_finish_decompress(j_decompress_ptr cinfo)
virtual ID_TIME_T Timestamp(void)
GLenum GLsizei GLsizei height
static idCVar image_roundDown
void R_RotatePic(byte *data, int width)
short LittleShort(short l)
GLOBAL void jpeg_destroy_decompress(j_decompress_ptr cinfo)
GLenum GLenum GLvoid * row
void R_VerticalFlip(byte *data, int width, int height)
void * R_StaticAlloc(int bytes)
void R_WriteTGA(const char *filename, const byte *data, int width, int height, bool flipVertical)
struct _TargaHeader TargaHeader
GLsizei const GLcharARB const GLint * length
GLOBAL struct jpeg_error_mgr * jpeg_std_error(struct jpeg_error_mgr *err)
int vsprintf(idStr &string, const char *fmt, va_list argptr)
void * Mem_ClearedAlloc(const int size)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const char * c_str(void) const
void * Mem_Alloc(const int size)
void jpg_Error(const char *fmt,...)
void ExtractFileExtension(idStr &dest) const
virtual void CloseFile(idFile *f)=0
GLenum GLenum GLvoid GLvoid * column
void R_StaticFree(void *data)
GLOBAL int jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
virtual void Error(const char *fmt,...) id_attribute((format(printf
virtual void virtual void Warning(const char *fmt,...) id_attribute((format(printf
GLOBAL void jpeg_stdio_src(j_decompress_ptr cinfo, unsigned char *infile)