]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
3D attenuation texture works now
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index 1c414dd75b268146e3cbc717480a18107bb5fa18..d78d1f5e04fdd52232215a243a3185b9d66dc26a 100644 (file)
--- a/image.c
+++ b/image.c
@@ -159,6 +159,7 @@ qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight)
                        else
                                a[x++] = dataByte;
                }
+               fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
                while(x < image_width)
                        a[x++] = 0;
        }
@@ -206,7 +207,8 @@ LoadTGA
 */
 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
 {
-       int columns, rows, row, column;
+       int x, y, row_inc;
+       unsigned char red, green, blue, alpha, run, runlen;
        qbyte *pixbuf, *image_rgba, *fin, *enddata;
 
        if (loadsize < 18+3)
@@ -243,13 +245,13 @@ qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
 
        enddata = f + loadsize;
 
-       columns = targa_header.width;
-       rows = targa_header.height;
+       image_width = targa_header.width;
+       image_height = targa_header.height;
 
-       image_rgba = Mem_Alloc(tempmempool, columns * rows * 4);
+       image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
        if (!image_rgba)
        {
-               Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", columns, rows);
+               Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", image_width, image_height);
                return NULL;
        }
 
@@ -257,34 +259,54 @@ qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
        if (targa_header.id_length != 0)
                fin += targa_header.id_length;  // skip TARGA image comment
 
+       // If bit 5 of attributes isn't set, the image has been stored from bottom to top
+       if ((targa_header.attributes & 0x20) == 0)
+       {
+               pixbuf = image_rgba + (image_height - 1)*image_width*4;
+               row_inc = -image_width*4*2;
+       }
+       else
+       {
+               pixbuf = image_rgba;
+               row_inc = 0;
+       }
+
        if (targa_header.image_type == 2)
        {
                // Uncompressed, RGB images
-               for(row = rows - 1;row >= 0;row--)
+               if (targa_header.pixel_size == 24)
                {
-                       pixbuf = image_rgba + row*columns*4;
-                       for(column = 0;column < columns;column++)
+                       if (fin + image_width * image_height * 3 <= enddata)
                        {
-                               switch (targa_header.pixel_size)
+                               for(y = 0;y < image_height;y++)
                                {
-                               case 24:
-                                       if (fin + 3 > enddata)
-                                               break;
-                                       *pixbuf++ = fin[2];
-                                       *pixbuf++ = fin[1];
-                                       *pixbuf++ = fin[0];
-                                       *pixbuf++ = 255;
-                                       fin += 3;
-                                       break;
-                               case 32:
-                                       if (fin + 4 > enddata)
-                                               break;
-                                       *pixbuf++ = fin[2];
-                                       *pixbuf++ = fin[1];
-                                       *pixbuf++ = fin[0];
-                                       *pixbuf++ = fin[3];
-                                       fin += 4;
-                                       break;
+                                       for(x = 0;x < image_width;x++)
+                                       {
+                                               *pixbuf++ = fin[2];
+                                               *pixbuf++ = fin[1];
+                                               *pixbuf++ = fin[0];
+                                               *pixbuf++ = 255;
+                                               fin += 3;
+                                       }
+                                       pixbuf += row_inc;
+                               }
+                       }
+               }
+               else
+               {
+                       if (fin + image_width * image_height * 4 <= enddata)
+                       {
+                               for(y = 0;y < image_height;y++)
+                               {
+                                       for(x = 0;x < image_width;x++)
+                                       {
+                                               *pixbuf++ = fin[2];
+                                               *pixbuf++ = fin[1];
+                                               *pixbuf++ = fin[0];
+                                               *pixbuf++ = fin[3];
+                                               fin += 4;
+                                       }
+                                       pixbuf += row_inc;
                                }
                        }
                }
@@ -292,105 +314,105 @@ qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
        else if (targa_header.image_type==10)
        {
                // Runlength encoded RGB images
-               unsigned char red = 0, green = 0, blue = 0, alphabyte = 0, packetHeader, packetSize, j;
-               for(row = rows - 1;row >= 0;row--)
+               x = 0;
+               y = 0;
+               while (y < image_height && fin < enddata)
                {
-                       pixbuf = image_rgba + row * columns * 4;
-                       for(column = 0;column < columns;)
+                       runlen = *fin++;
+                       if (runlen & 0x80)
                        {
-                               if (fin >= enddata)
-                                       goto outofdata;
-                               packetHeader = *fin++;
-                               packetSize = 1 + (packetHeader & 0x7f);
-                               if (packetHeader & 0x80)
+                               // RLE compressed run
+                               runlen = 1 + (runlen & 0x7f);
+                               if (targa_header.pixel_size == 24)
                                {
-                                       // run-length packet
-                                       switch (targa_header.pixel_size)
-                                       {
-                                       case 24:
-                                               if (fin + 3 > enddata)
-                                                       goto outofdata;
-                                               blue = *fin++;
-                                               green = *fin++;
-                                               red = *fin++;
-                                               alphabyte = 255;
+                                       if (fin + 3 > enddata)
                                                break;
-                                       case 32:
-                                               if (fin + 4 > enddata)
-                                                       goto outofdata;
-                                               blue = *fin++;
-                                               green = *fin++;
-                                               red = *fin++;
-                                               alphabyte = *fin++;
+                                       blue = *fin++;
+                                       green = *fin++;
+                                       red = *fin++;
+                                       alpha = 255;
+                               }
+                               else
+                               {
+                                       if (fin + 4 > enddata)
                                                break;
-                                       }
+                                       blue = *fin++;
+                                       green = *fin++;
+                                       red = *fin++;
+                                       alpha = *fin++;
+                               }
 
-                                       for(j = 0;j < packetSize;j++)
+                               while (runlen && y < image_height)
+                               {
+                                       run = runlen;
+                                       if (run > image_width - x)
+                                               run = image_width - x;
+                                       x += run;
+                                       runlen -= run;
+                                       while(run--)
                                        {
                                                *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 = image_rgba + row * columns * 4;
-                                               }
+                                               *pixbuf++ = alpha;
+                                       }
+                                       if (x == image_width)
+                                       {
+                                               // end of line, advance to next
+                                               x = 0;
+                                               y++;
+                                               pixbuf += row_inc;
                                        }
                                }
-                               else
+                       }
+                       else
+                       {
+                               // RLE uncompressed run
+                               runlen = 1 + (runlen & 0x7f);
+                               while (runlen && y < image_height)
                                {
-                                       // non run-length packet
-                                       for(j = 0;j < packetSize;j++)
+                                       run = runlen;
+                                       if (run > image_width - x)
+                                               run = image_width - x;
+                                       x += run;
+                                       runlen -= run;
+                                       if (targa_header.pixel_size == 24)
                                        {
-                                               switch (targa_header.pixel_size)
+                                               if (fin + run * 3 > enddata)
+                                                       break;
+                                               while(run--)
                                                {
-                                               case 24:
-                                                       if (fin + 3 > enddata)
-                                                               goto outofdata;
                                                        *pixbuf++ = fin[2];
                                                        *pixbuf++ = fin[1];
                                                        *pixbuf++ = fin[0];
                                                        *pixbuf++ = 255;
                                                        fin += 3;
+                                               }
+                                       }
+                                       else
+                                       {
+                                               if (fin + run * 4 > enddata)
                                                        break;
-                                               case 32:
-                                                       if (fin + 4 > enddata)
-                                                               goto outofdata;
+                                               while(run--)
+                                               {
                                                        *pixbuf++ = fin[2];
                                                        *pixbuf++ = fin[1];
                                                        *pixbuf++ = fin[0];
                                                        *pixbuf++ = fin[3];
                                                        fin += 4;
-                                                       break;
-                                               }
-                                               column++;
-                                               if (column == columns)
-                                               {
-                                                       // pixel packet run spans across rows
-                                                       column = 0;
-                                                       if (row > 0)
-                                                               row--;
-                                                       else
-                                                               goto breakOut;
-                                                       pixbuf = image_rgba + row * columns * 4;
                                                }
                                        }
+                                       if (x == image_width)
+                                       {
+                                               // end of line, advance to next
+                                               x = 0;
+                                               y++;
+                                               pixbuf += row_inc;
+                                       }
                                }
                        }
-                       breakOut:;
                }
        }
-outofdata:;
-
-       image_width = columns;
-       image_height = rows;
        return image_rgba;
 }
 
@@ -440,6 +462,52 @@ qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
        return image_rgba;
 }
 
+/*
+============
+LoadLMP
+============
+*/
+qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
+{
+       qbyte *image_8bit;
+       int width, height;
+
+       if (loadsize < 9)
+       {
+               Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
+               return NULL;
+       }
+
+       // parse the very complicated header *chuckle*
+       width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
+       height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
+       if ((unsigned) width > 4096 || (unsigned) height > 4096)
+       {
+               Con_Printf("LoadLMPAs8Bit: invalid size\n");
+               return NULL;
+       }
+       if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
+               return NULL;
+
+       if (loadsize < 8 + width * height)
+       {
+               Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
+               return NULL;
+       }
+
+       image_width = width;
+       image_height = height;
+
+       image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
+       if (!image_8bit)
+       {
+               Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
+               return NULL;
+       }
+       memcpy(image_8bit, f + 8, image_width * image_height);
+       return image_8bit;
+}
+
 void Image_StripImageExtension (char *in, char *out)
 {
        char *end, *temp;
@@ -587,8 +655,9 @@ rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, char* filename, int
        return rt;
 }
 
-void Image_WriteTGARGB_preflipped (char *filename, int width, int height, qbyte *data)
+qboolean Image_WriteTGARGB_preflipped (char *filename, int width, int height, qbyte *data)
 {
+       qboolean ret;
        qbyte *buffer, *in, *out, *end;
 
        buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
@@ -611,9 +680,10 @@ void Image_WriteTGARGB_preflipped (char *filename, int width, int height, qbyte
                *out++ = in[1];
                *out++ = in[0];
        }
-       COM_WriteFile (filename, buffer, width*height*3 + 18 );
+       ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
 
        Mem_Free(buffer);
+       return ret;
 }
 
 void Image_WriteTGARGB (char *filename, int width, int height, qbyte *data)
@@ -1027,8 +1097,10 @@ void Image_Resample24Nolerp(void *indata, int inwidth, int inheight, void *outda
 Image_Resample
 ================
 */
-void Image_Resample (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight, int bytesperpixel, int quality)
+void Image_Resample (void *indata, int inwidth, int inheight, int indepth, void *outdata, int outwidth, int outheight, int outdepth, int bytesperpixel, int quality)
 {
+       if (indepth != 1 || outdepth != 1)
+               Sys_Error("Image_Resample: 3D resampling not supported\n");
        if (resamplerowsize < outwidth*4)
        {
                if (resamplerow1)
@@ -1058,9 +1130,11 @@ void Image_Resample (void *indata, int inwidth, int inheight, void *outdata, int
 }
 
 // in can be the same as out
-void Image_MipReduce(qbyte *in, qbyte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
+void Image_MipReduce(qbyte *in, qbyte *out, int *width, int *height, int *depth, int destwidth, int destheight, int destdepth, int bytesperpixel)
 {
        int x, y, nextrow;
+       if (*depth != 1 || destdepth != 1)
+               Sys_Error("Image_Resample: 3D resampling not supported\n");
        nextrow = *width * bytesperpixel;
        if (*width > destwidth)
        {
@@ -1183,3 +1257,4 @@ void Image_MipReduce(qbyte *in, qbyte *out, int *width, int *height, int destwid
                        Sys_Error("Image_MipReduce: desired size already achieved\n");
        }
 }
+