fseek (f, sizeof(pcxbuf) - 4, SEEK_SET);
count = (pcx->xmax+1) * (pcx->ymax+1);
- image_rgba = malloc( count * 4);
+ image_rgba = qmalloc( count * 4);
for (y=0 ; y<=pcx->ymax ; y++)
{
rows = targa_header.height;
numPixels = columns * rows;
- image_rgba = malloc (numPixels*4);
+ image_rgba = qmalloc(numPixels*4);
if (targa_header.id_length != 0)
fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
if (matchheight && height != matchheight)
return NULL;
- image_rgba = malloc(width*height*4);
+ image_rgba = qmalloc(width*height*4);
fread(image_rgba + width*height*3, 1, width*height, f);
fclose(f);
{
FILE *f;
char basename[256], name[256];
- byte *image_rgba, *c;
+ byte *c;
Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
// replace *'s with #, so commandline utils don't get confused when dealing with the external files
for (c = basename;*c;c++)
COM_FOpenFile (name, &f, true);
if (f)
return LoadLMP (f, matchwidth, matchheight);
- if ((image_rgba = W_GetTexture(basename, matchwidth, matchheight)))
- return image_rgba;
- COM_StripExtension(filename, basename); // do it again with a * this time
- 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;
return data; // some transparency
else
{
- free(data);
+ qfree(data);
return NULL; // all opaque
}
}
-int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
+rtexture_t *loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
{
- int texnum;
byte *data;
+ rtexture_t *rt;
if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
return 0;
- texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
- free(data);
- return texnum;
+ rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
+ qfree(data);
+ return rt;
}
-int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
+rtexture_t *loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
{
- int texnum;
byte *data;
+ rtexture_t *rt;
if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
return 0;
- texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
- free(data);
- return texnum;
+ rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
+ qfree(data);
+ return rt;
}
-int image_masktexnum;
-int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap)
+rtexture_t *image_masktex;
+rtexture_t *loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
{
- int texnum, count;
+ int count;
byte *data;
char *filename2;
- image_masktexnum = 0;
+ rtexture_t *rt;
+ image_masktex = NULL;
if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
return 0;
- texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
+ rt = R_LoadTexture (filename, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
count = image_makemask(data, data, image_width * image_height);
if (count)
{
- filename2 = malloc(strlen(filename) + 6);
+ filename2 = qmalloc(strlen(filename) + 6);
sprintf(filename2, "%s_mask", filename);
- image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4);
- free(filename2);
+ image_masktex = R_LoadTexture (filename2, image_width, image_height, data, TEXF_ALPHA | TEXF_RGBA | (mipmap ? TEXF_MIPMAP : 0) | (mipmap ? TEXF_PRECACHE : 0));
+ qfree(filename2);
}
- free(data);
- return texnum;
+ qfree(data);
+ return rt;
+}
+
+void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *data)
+{
+ byte *buffer, *in, *out, *end;
+
+ buffer = qmalloc(width*height*3 + 18);
+
+ memset (buffer, 0, 18);
+ buffer[2] = 2; // uncompressed type
+ buffer[12] = (width >> 0) & 0xFF;
+ buffer[13] = (width >> 8) & 0xFF;
+ buffer[14] = (height >> 0) & 0xFF;
+ buffer[15] = (height >> 8) & 0xFF;
+ buffer[16] = 24; // pixel size
+
+ // swap rgb to bgr
+ in = data;
+ out = buffer + 18;
+ end = in + width*height*3;
+ for (;in < end;in += 3)
+ {
+ *out++ = in[2];
+ *out++ = in[1];
+ *out++ = in[0];
+ }
+ COM_WriteFile (filename, buffer, width*height*3 + 18 );
+
+ qfree(buffer);
}
void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
int y;
byte *buffer, *in, *out, *end;
- buffer = malloc(width*height*3 + 18);
+ buffer = qmalloc(width*height*3 + 18);
memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
}
COM_WriteFile (filename, buffer, width*height*3 + 18 );
- free(buffer);
+ qfree(buffer);
}
void Image_WriteTGARGBA (char *filename, int width, int height, byte *data)
int y;
byte *buffer, *in, *out, *end;
- buffer = malloc(width*height*4 + 18);
+ buffer = qmalloc(width*height*4 + 18);
memset (buffer, 0, 18);
buffer[2] = 2; // uncompressed type
}
COM_WriteFile (filename, buffer, width*height*4 + 18 );
- free(buffer);
+ qfree(buffer);
+}
+
+qboolean Image_CheckAlpha(byte *data, int size, qboolean rgba)
+{
+ byte *end;
+ if (rgba)
+ {
+ // check alpha bytes
+ for (end = data + size * 4, data += 3;data < end;data += 4)
+ if (*data < 255)
+ return 1;
+ }
+ else
+ {
+ // color 255 is transparent
+ for (end = data + size;data < end;data++)
+ if (*data == 255)
+ return 1;
+ }
+ return 0;
}