X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=image.c;h=368bb1f802d79135c4b44c4a3eb00ec000f50107;hp=6740daca29e9b710f26bbf149d9e84054bec25f6;hb=da455986a856db0801c7e4bcfc0f65cebb4ee200;hpb=855932aeb5707c5efb2858c3e51b913d8203ebbe diff --git a/image.c b/image.c index 6740daca..368bb1f8 100644 --- a/image.c +++ b/image.c @@ -437,6 +437,37 @@ byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int ma return NULL; } +int image_makemask (byte *in, byte *out, int size) +{ + int i, count; + count = 0; + for (i = 0;i < size;i++) + { + out[0] = out[1] = out[2] = 255; + out[3] = in[3]; + if (in[3] != 255) + count++; + in += 4; + out += 4; + } + return count; +} + +byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight) +{ + byte *in, *data; + in = data = loadimagepixels(filename, complain, matchwidth, matchheight); + if (!data) + return NULL; + if (image_makemask(data, data, image_width * image_height)) + return data; // some transparency + else + { + free(data); + return NULL; // all opaque + } +} + int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap) { int texnum; @@ -446,19 +477,66 @@ int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4); free(data); return texnum; - /* - if (texnum >= 0) // specific texnum, not cached +} + +int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap) +{ + int texnum; + byte *data; + 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; +} + +int image_masktexnum; +int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap) +{ + int texnum, count; + byte *data; + char *filename2; + image_masktexnum = 0; + if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight))) + return 0; + texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4); + count = image_makemask(data, data, image_width * image_height); + if (count) { - glBindTexture(GL_TEXTURE_2D, texnum); - GL_Upload32 (data, image_width, image_height, mipmap, true); - free(data); - return texnum; + filename2 = malloc(strlen(filename) + 6); + sprintf(filename2, "%s_mask", filename); + image_masktexnum = GL_LoadTexture (filename2, image_width, image_height, data, mipmap, true, 4); + free(filename2); } - else // any texnum, cached + free(data); + return texnum; +} + +void Image_WriteTGARGB (char *filename, int width, int height, byte *data) +{ + byte *buffer, *in, *out, *end; + + buffer = malloc(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; + end = in + width*height*3; + out = buffer + 18; + for (;in < end;in += 3) { - texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4); - free(data); - return texnum; + *out++ = in[2]; + *out++ = in[1]; + *out++ = in[0]; } - */ + COM_WriteFile (filename, buffer, glwidth*glheight*3 + 18 ); + + free(buffer); }