]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image.c
Remove alsa 0.6 (for some random date of cvs alsa) support, never officially released...
[xonotic/darkplaces.git] / image.c
diff --git a/image.c b/image.c
index e3b62d4bd2166835603824926a277193480564c4..d12ed7123b58572197d17e6a757787e95a0180bd 100644 (file)
--- a/image.c
+++ b/image.c
@@ -130,7 +130,7 @@ byte* LoadPCX (FILE *f, int matchwidth, int matchheight)
        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++)
        {
@@ -258,7 +258,7 @@ byte* LoadTGA (FILE *fin, int matchwidth, int matchheight)
        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
@@ -397,7 +397,7 @@ byte* LoadLMP (FILE *f, int matchwidth, int matchheight)
        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);
 
@@ -409,12 +409,13 @@ byte* LoadLMP (FILE *f, int matchwidth, int matchheight)
 
 void Image_StripImageExtension (char *in, char *out)
 {
-       byte *end;
+       char *end, *temp;
        end = in + strlen(in);
        if ((end - in) >= 4)
        {
-               if (strcmp(end - 4, ".tga") == 0 || strcmp(end - 4, ".pcx") == 0 || strcmp(end - 4, ".lmp") == 0)
-                       end -= 4;
+               temp = end - 4;
+               if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
+                       end = temp;
                while (in < end)
                        *out++ = *in++;
                *out++ = 0;
@@ -442,15 +443,9 @@ byte* loadimagepixels (char* filename, qboolean complain, int matchwidth, int ma
        if (f)
                return LoadPCX (f, matchwidth, matchheight);
        sprintf (name, "%s.tga", basename);
-       Con_Printf("name = %s : ", name);
        COM_FOpenFile (name, &f, true);
        if (f)
-       {
-               Con_Printf("succeeded\n");
                return LoadTGA (f, matchwidth, matchheight);
-       }
-       else
-               Con_Printf("failed\n");
        sprintf (name, "%s.pcx", basename);
        COM_FOpenFile (name, &f, true);
        if (f)
@@ -495,7 +490,7 @@ byte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, in
                return data; // some transparency
        else
        {
-               free(data);
+               qfree(data);
                return NULL; // all opaque
        }
 }
@@ -507,7 +502,7 @@ int loadtextureimage (char* filename, int matchwidth, int matchheight, qboolean
        if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
                return 0;
        texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
-       free(data);
+       qfree(data);
        return texnum;
 }
 
@@ -518,7 +513,7 @@ int loadtextureimagemask (char* filename, int matchwidth, int matchheight, qbool
        if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
                return 0;
        texnum = GL_LoadTexture (filename, image_width, image_height, data, mipmap, true, 4);
-       free(data);
+       qfree(data);
        return texnum;
 }
 
@@ -535,20 +530,20 @@ int loadtextureimagewithmask (char* filename, int matchwidth, int matchheight, q
        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);
+               qfree(filename2);
        }
-       free(data);
+       qfree(data);
        return texnum;
 }
 
-void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
+void Image_WriteTGARGB_preflipped (char *filename, int width, int height, byte *data)
 {
        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
@@ -560,15 +555,82 @@ void Image_WriteTGARGB (char *filename, int width, int height, byte *data)
 
        // swap rgb to bgr
        in = data;
-       end = in + width*height*3;
        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, glwidth*glheight*3 + 18 );
+       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 = 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 and flip upside down
+       out = buffer + 18;
+       for (y = height - 1;y >= 0;y--)
+       {
+               in = data + y * width * 3;
+               end = in + width * 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_WriteTGARGBA (char *filename, int width, int height, byte *data)
+{
+       int y;
+       byte *buffer, *in, *out, *end;
+
+       buffer = qmalloc(width*height*4 + 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] = 32;        // pixel size
+
+       // swap rgba to bgra and flip upside down
+       out = buffer + 18;
+       for (y = height - 1;y >= 0;y--)
+       {
+               in = data + y * width * 4;
+               end = in + width * 4;
+               for (;in < end;in += 4)
+               {
+                       *out++ = in[2];
+                       *out++ = in[1];
+                       *out++ = in[0];
+                       *out++ = in[3];
+               }
+       }
+       COM_WriteFile (filename, buffer, width*height*4 + 18 );
 
-       free(buffer);
+       qfree(buffer);
 }