7 void Image_GammaRemapRGB(qbyte *in, qbyte *out, int pixels, qbyte *gammar, qbyte *gammag, qbyte *gammab)
11 out[0] = gammar[in[0]];
12 out[1] = gammag[in[1]];
13 out[2] = gammab[in[2]];
19 // note: pal must be 32bit color
20 void Image_Copy8bitRGBA(qbyte *in, qbyte *out, int pixels, int *pal)
22 int *iout = (void *)out;
58 =================================================================
62 =================================================================
71 unsigned short xmin,ymin,xmax,ymax;
72 unsigned short hres,vres;
73 unsigned char palette[48];
76 unsigned short bytes_per_line;
77 unsigned short palette_type;
86 qbyte* LoadPCX (qbyte *f, int matchwidth, int matchheight)
89 qbyte *palette, *a, *b, *image_rgba, *fin, *pbuf, *enddata;
90 int x, y, x2, dataByte;
92 if (loadsize < sizeof(pcx) + 768)
94 Con_Printf ("Bad pcx file\n");
100 memcpy(&pcx, fin, sizeof(pcx));
103 // LordHavoc: big-endian support ported from QF newtree
104 pcx.xmax = LittleShort (pcx.xmax);
105 pcx.xmin = LittleShort (pcx.xmin);
106 pcx.ymax = LittleShort (pcx.ymax);
107 pcx.ymin = LittleShort (pcx.ymin);
108 pcx.hres = LittleShort (pcx.hres);
109 pcx.vres = LittleShort (pcx.vres);
110 pcx.bytes_per_line = LittleShort (pcx.bytes_per_line);
111 pcx.palette_type = LittleShort (pcx.palette_type);
113 if (pcx.manufacturer != 0x0a || pcx.version != 5 || pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.xmax > 320 || pcx.ymax > 256)
115 Con_Printf ("Bad pcx file\n");
119 if (matchwidth && (pcx.xmax+1) != matchwidth)
123 if (matchheight && (pcx.ymax+1) != matchheight)
128 image_width = pcx.xmax+1;
129 image_height = pcx.ymax+1;
131 palette = f + loadsize - 768;
133 image_rgba = Mem_Alloc(tempmempool, image_width*image_height*4);
136 Con_Printf("LoadPCX: not enough memory for %i by %i image\n", image_width, image_height);
139 pbuf = image_rgba + image_width*image_height*3;
142 for (y = 0;y < image_height && fin < enddata;y++)
144 a = pbuf + y * image_width;
145 for (x = 0;x < image_width && fin < enddata;)
152 x2 = x + (dataByte & 0x3F);
154 if (x2 > image_width)
155 x2 = image_width; // technically an error
162 fin += pcx.bytes_per_line - image_width; // the number of bytes per line is always forced to an even number
163 while(x < image_width)
170 for(x = 0;x < image_width*image_height;x++)
183 =========================================================
187 =========================================================
190 typedef struct _TargaHeader
192 unsigned char id_length, colormap_type, image_type;
193 unsigned short colormap_index, colormap_length;
194 unsigned char colormap_size;
195 unsigned short x_origin, y_origin, width, height;
196 unsigned char pixel_size, attributes;
200 TargaHeader targa_header;
208 qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight)
210 int columns, rows, row, column, row_inc;
211 qbyte *pixbuf, *image_rgba, *fin, *enddata;
215 targa_header.id_length = f[0];
216 targa_header.colormap_type = f[1];
217 targa_header.image_type = f[2];
219 targa_header.colormap_index = f[3] + f[4] * 256;
220 targa_header.colormap_length = f[5] + f[6] * 256;
221 targa_header.colormap_size = f[7];
222 targa_header.x_origin = f[8] + f[9] * 256;
223 targa_header.y_origin = f[10] + f[11] * 256;
224 targa_header.width = f[12] + f[13] * 256;
225 targa_header.height = f[14] + f[15] * 256;
226 if (matchwidth && targa_header.width != matchwidth)
228 if (matchheight && targa_header.height != matchheight)
230 targa_header.pixel_size = f[16];
231 targa_header.attributes = f[17];
233 if (targa_header.image_type != 2 && targa_header.image_type != 10)
235 Con_Printf ("LoadTGA: Only type 2 and 10 targa RGB images supported\n");
239 if (targa_header.colormap_type != 0 || (targa_header.pixel_size != 32 && targa_header.pixel_size != 24))
241 Con_Printf ("LoadTGA: Only 32 or 24 bit images supported (no colormaps)\n");
245 enddata = f + loadsize;
247 columns = targa_header.width;
248 rows = targa_header.height;
250 image_rgba = Mem_Alloc(tempmempool, columns * rows * 4);
253 Con_Printf ("LoadTGA: not enough memory for %i by %i image\n", columns, rows);
258 if (targa_header.id_length != 0)
259 fin += targa_header.id_length; // skip TARGA image comment
261 // If bit 5 of attributes isn't set, the image has been stored from bottom to top
262 if ((targa_header.attributes & 0x20) == 0)
264 pixbuf = image_rgba + (rows - 1)*columns*4;
265 row_inc = -columns*4*2;
273 if (targa_header.image_type == 2)
275 // Uncompressed, RGB images
276 for(row = 0;row < rows;row++)
278 for(column = 0;column < columns;column++)
280 switch (targa_header.pixel_size)
283 if (fin + 3 > enddata)
292 if (fin + 4 > enddata)
305 else if (targa_header.image_type==10)
307 // Runlength encoded RGB images
308 unsigned char red = 0, green = 0, blue = 0, alphabyte = 0, packetHeader, packetSize, j;
309 for(row = 0;row < rows;row++)
311 for(column = 0;column < columns;)
315 packetHeader = *fin++;
316 packetSize = 1 + (packetHeader & 0x7f);
317 if (packetHeader & 0x80)
320 switch (targa_header.pixel_size)
323 if (fin + 3 > enddata)
331 if (fin + 4 > enddata)
340 for(j = 0;j < packetSize;j++)
345 *pixbuf++ = alphabyte;
347 if (column == columns)
349 // run spans across rows
361 // non run-length packet
362 for(j = 0;j < packetSize;j++)
364 switch (targa_header.pixel_size)
367 if (fin + 3 > enddata)
376 if (fin + 4 > enddata)
386 if (column == columns)
388 // pixel packet run spans across rows
405 image_width = columns;
415 qbyte *LoadLMP (qbyte *f, int matchwidth, int matchheight)
422 Con_Printf("LoadLMP: invalid LMP file\n");
426 // parse the very complicated header *chuckle*
427 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
428 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
429 if ((unsigned) width > 4096 || (unsigned) height > 4096)
431 Con_Printf("LoadLMP: invalid size\n");
434 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
437 if (loadsize < 8 + width * height)
439 Con_Printf("LoadLMP: invalid LMP file\n");
444 image_height = height;
446 image_rgba = Mem_Alloc(tempmempool, image_width * image_height * 4);
449 Con_Printf("LoadLMP: not enough memory for %i by %i image\n", image_width, image_height);
452 Image_Copy8bitRGBA(f + 8, image_rgba, image_width * image_height, d_8to24table);
461 qbyte *LoadLMPAs8Bit (qbyte *f, int matchwidth, int matchheight)
468 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
472 // parse the very complicated header *chuckle*
473 width = f[0] + f[1] * 256 + f[2] * 65536 + f[3] * 16777216;
474 height = f[4] + f[5] * 256 + f[6] * 65536 + f[7] * 16777216;
475 if ((unsigned) width > 4096 || (unsigned) height > 4096)
477 Con_Printf("LoadLMPAs8Bit: invalid size\n");
480 if ((matchwidth && width != matchwidth) || (matchheight && height != matchheight))
483 if (loadsize < 8 + width * height)
485 Con_Printf("LoadLMPAs8Bit: invalid LMP file\n");
490 image_height = height;
492 image_8bit = Mem_Alloc(tempmempool, image_width * image_height);
495 Con_Printf("LoadLMPAs8Bit: not enough memory for %i by %i image\n", image_width, image_height);
498 memcpy(image_8bit, f + 8, image_width * image_height);
502 void Image_StripImageExtension (char *in, char *out)
505 end = in + strlen(in);
509 if (strcmp(temp, ".tga") == 0 || strcmp(temp, ".pcx") == 0 || strcmp(temp, ".lmp") == 0)
519 qbyte *loadimagepixels (char *filename, qboolean complain, int matchwidth, int matchheight)
522 char basename[256], name[256], *c;
523 Image_StripImageExtension(filename, basename); // strip .tga, .pcx and .lmp extensions to allow replacement by other types
524 // replace *'s with #, so commandline utils don't get confused when dealing with the external files
525 for (c = basename;*c;c++)
528 sprintf (name, "textures/%s.tga", basename);
529 f = COM_LoadFile(name, true);
532 data = LoadTGA (f, matchwidth, matchheight);
536 sprintf (name, "textures/%s.pcx", basename);
537 f = COM_LoadFile(name, true);
540 data = LoadPCX (f, matchwidth, matchheight);
544 sprintf (name, "%s.tga", basename);
545 f = COM_LoadFile(name, true);
548 data = LoadTGA (f, matchwidth, matchheight);
552 sprintf (name, "%s.pcx", basename);
553 f = COM_LoadFile(name, true);
556 data = LoadPCX (f, matchwidth, matchheight);
560 sprintf (name, "%s.lmp", basename);
561 f = COM_LoadFile(name, true);
564 data = LoadLMP (f, matchwidth, matchheight);
569 Con_Printf ("Couldn't load %s.tga, .pcx, .lmp\n", filename);
573 int image_makemask (qbyte *in, qbyte *out, int size)
577 for (i = 0;i < size;i++)
579 out[0] = out[1] = out[2] = 255;
589 qbyte* loadimagepixelsmask (char* filename, qboolean complain, int matchwidth, int matchheight)
592 in = data = loadimagepixels(filename, complain, matchwidth, matchheight);
595 if (image_makemask(data, data, image_width * image_height))
596 return data; // some transparency
600 return NULL; // all opaque
604 rtexture_t *loadtextureimage (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
608 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
610 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
615 rtexture_t *loadtextureimagemask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
619 if (!(data = loadimagepixelsmask (filename, complain, matchwidth, matchheight)))
621 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
626 rtexture_t *image_masktex;
627 rtexture_t *loadtextureimagewithmask (rtexturepool_t *pool, char* filename, int matchwidth, int matchheight, qboolean complain, qboolean mipmap, qboolean precache)
633 image_masktex = NULL;
634 if (!(data = loadimagepixels (filename, complain, matchwidth, matchheight)))
636 rt = R_LoadTexture (pool, filename, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
637 count = image_makemask(data, data, image_width * image_height);
640 filename2 = Mem_Alloc(tempmempool, strlen(filename) + 6);
641 sprintf(filename2, "%s_mask", filename);
642 image_masktex = R_LoadTexture (pool, filename2, image_width, image_height, data, TEXTYPE_RGBA, TEXF_ALPHA | (mipmap ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
649 qboolean Image_WriteTGARGB_preflipped (char *filename, int width, int height, qbyte *data)
652 qbyte *buffer, *in, *out, *end;
654 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
656 memset (buffer, 0, 18);
657 buffer[2] = 2; // uncompressed type
658 buffer[12] = (width >> 0) & 0xFF;
659 buffer[13] = (width >> 8) & 0xFF;
660 buffer[14] = (height >> 0) & 0xFF;
661 buffer[15] = (height >> 8) & 0xFF;
662 buffer[16] = 24; // pixel size
667 end = in + width*height*3;
668 for (;in < end;in += 3)
674 ret = COM_WriteFile (filename, buffer, width*height*3 + 18 );
680 void Image_WriteTGARGB (char *filename, int width, int height, qbyte *data)
683 qbyte *buffer, *in, *out, *end;
685 buffer = Mem_Alloc(tempmempool, width*height*3 + 18);
687 memset (buffer, 0, 18);
688 buffer[2] = 2; // uncompressed type
689 buffer[12] = (width >> 0) & 0xFF;
690 buffer[13] = (width >> 8) & 0xFF;
691 buffer[14] = (height >> 0) & 0xFF;
692 buffer[15] = (height >> 8) & 0xFF;
693 buffer[16] = 24; // pixel size
695 // swap rgb to bgr and flip upside down
697 for (y = height - 1;y >= 0;y--)
699 in = data + y * width * 3;
700 end = in + width * 3;
701 for (;in < end;in += 3)
708 COM_WriteFile (filename, buffer, width*height*3 + 18 );
713 void Image_WriteTGARGBA (char *filename, int width, int height, qbyte *data)
716 qbyte *buffer, *in, *out, *end;
718 buffer = Mem_Alloc(tempmempool, width*height*4 + 18);
720 memset (buffer, 0, 18);
721 buffer[2] = 2; // uncompressed type
722 buffer[12] = (width >> 0) & 0xFF;
723 buffer[13] = (width >> 8) & 0xFF;
724 buffer[14] = (height >> 0) & 0xFF;
725 buffer[15] = (height >> 8) & 0xFF;
726 buffer[16] = 32; // pixel size
728 // swap rgba to bgra and flip upside down
730 for (y = height - 1;y >= 0;y--)
732 in = data + y * width * 4;
733 end = in + width * 4;
734 for (;in < end;in += 4)
742 COM_WriteFile (filename, buffer, width*height*4 + 18 );
747 qboolean Image_CheckAlpha(qbyte *data, int size, qboolean rgba)
753 for (end = data + size * 4, data += 3;data < end;data += 4)
759 // color 255 is transparent
760 for (end = data + size;data < end;data++)
767 static void Image_Resample32LerpLine (qbyte *in, qbyte *out, int inwidth, int outwidth)
769 int j, xi, oldx = 0, f, fstep, endx, lerp;
770 fstep = (int) (inwidth*65536.0f/outwidth);
772 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
777 in += (xi - oldx) * 4;
783 *out++ = (qbyte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
784 *out++ = (qbyte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
785 *out++ = (qbyte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
786 *out++ = (qbyte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
788 else // last pixel of the line has no pixel to lerp to
798 static void Image_Resample24LerpLine (qbyte *in, qbyte *out, int inwidth, int outwidth)
800 int j, xi, oldx = 0, f, fstep, endx, lerp;
801 fstep = (int) (inwidth*65536.0f/outwidth);
803 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
808 in += (xi - oldx) * 3;
814 *out++ = (qbyte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
815 *out++ = (qbyte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
816 *out++ = (qbyte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
818 else // last pixel of the line has no pixel to lerp to
827 int resamplerowsize = 0;
828 qbyte *resamplerow1 = NULL;
829 qbyte *resamplerow2 = NULL;
830 mempool_t *resamplemempool = NULL;
832 #define LERPBYTE(i) r = resamplerow1[i];out[i] = (qbyte) ((((resamplerow2[i] - r) * lerp) >> 16) + r)
833 void Image_Resample32Lerp(void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
835 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
838 fstep = (int) (inheight*65536.0f/outheight);
842 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
843 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
844 for (i = 0, f = 0;i < outheight;i++,f += fstep)
852 inrow = (qbyte *)indata + inwidth4*yi;
854 memcpy(resamplerow1, resamplerow2, outwidth4);
856 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
857 Image_Resample32LerpLine (inrow + inwidth4, resamplerow2, inwidth, outwidth);
908 resamplerow1 -= outwidth4;
909 resamplerow2 -= outwidth4;
915 inrow = (qbyte *)indata + inwidth4*yi;
917 memcpy(resamplerow1, resamplerow2, outwidth4);
919 Image_Resample32LerpLine (inrow, resamplerow1, inwidth, outwidth);
922 memcpy(out, resamplerow1, outwidth4);
927 void Image_Resample32Nearest(void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
930 unsigned frac, fracstep;
931 // relies on int being 4 bytes
935 fracstep = inwidth*0x10000/outwidth;
936 for (i = 0;i < outheight;i++)
938 inrow = (int *)indata + inwidth*(i*inheight/outheight);
939 frac = fracstep >> 1;
943 out[0] = inrow[frac >> 16];frac += fracstep;
944 out[1] = inrow[frac >> 16];frac += fracstep;
945 out[2] = inrow[frac >> 16];frac += fracstep;
946 out[3] = inrow[frac >> 16];frac += fracstep;
952 out[0] = inrow[frac >> 16];frac += fracstep;
953 out[1] = inrow[frac >> 16];frac += fracstep;
958 out[0] = inrow[frac >> 16];frac += fracstep;
964 void Image_Resample24Lerp(void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
966 int i, j, r, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
969 fstep = (int) (inheight*65536.0f/outheight);
973 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
974 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
975 for (i = 0, f = 0;i < outheight;i++,f += fstep)
983 inrow = (qbyte *)indata + inwidth3*yi;
985 memcpy(resamplerow1, resamplerow2, outwidth3);
987 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
988 Image_Resample24LerpLine (inrow + inwidth3, resamplerow2, inwidth, outwidth);
1032 resamplerow1 -= outwidth3;
1033 resamplerow2 -= outwidth3;
1039 inrow = (qbyte *)indata + inwidth3*yi;
1041 memcpy(resamplerow1, resamplerow2, outwidth3);
1043 Image_Resample24LerpLine (inrow, resamplerow1, inwidth, outwidth);
1046 memcpy(out, resamplerow1, outwidth3);
1051 void Image_Resample24Nolerp(void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
1053 int i, j, f, inwidth3 = inwidth * 3;
1054 unsigned frac, fracstep;
1058 fracstep = inwidth*0x10000/outwidth;
1059 for (i = 0;i < outheight;i++)
1061 inrow = (qbyte *)indata + inwidth3*(i*inheight/outheight);
1062 frac = fracstep >> 1;
1066 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1067 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1068 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1069 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1074 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1075 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1080 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
1091 void Image_Resample (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight, int bytesperpixel, int quality)
1093 if (resamplerowsize < outwidth*4)
1096 Mem_Free(resamplerow1);
1097 resamplerowsize = outwidth*4;
1098 if (!resamplemempool)
1099 resamplemempool = Mem_AllocPool("Image Scaling Buffer");
1100 resamplerow1 = Mem_Alloc(resamplemempool, resamplerowsize*2);
1101 resamplerow2 = resamplerow1 + resamplerowsize;
1103 if (bytesperpixel == 4)
1106 Image_Resample32Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1108 Image_Resample32Nearest(indata, inwidth, inheight, outdata, outwidth, outheight);
1110 else if (bytesperpixel == 3)
1113 Image_Resample24Lerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1115 Image_Resample24Nolerp(indata, inwidth, inheight, outdata, outwidth, outheight);
1118 Sys_Error("Image_Resample: unsupported bytesperpixel %i\n", bytesperpixel);
1121 // in can be the same as out
1122 void Image_MipReduce(qbyte *in, qbyte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
1125 nextrow = *width * bytesperpixel;
1126 if (*width > destwidth)
1129 if (*height > destheight)
1133 if (bytesperpixel == 4)
1135 for (y = 0;y < *height;y++)
1137 for (x = 0;x < *width;x++)
1139 out[0] = (qbyte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
1140 out[1] = (qbyte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
1141 out[2] = (qbyte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
1142 out[3] = (qbyte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
1146 in += nextrow; // skip a line
1149 else if (bytesperpixel == 3)
1151 for (y = 0;y < *height;y++)
1153 for (x = 0;x < *width;x++)
1155 out[0] = (qbyte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
1156 out[1] = (qbyte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
1157 out[2] = (qbyte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
1161 in += nextrow; // skip a line
1165 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1170 if (bytesperpixel == 4)
1172 for (y = 0;y < *height;y++)
1174 for (x = 0;x < *width;x++)
1176 out[0] = (qbyte) ((in[0] + in[4]) >> 1);
1177 out[1] = (qbyte) ((in[1] + in[5]) >> 1);
1178 out[2] = (qbyte) ((in[2] + in[6]) >> 1);
1179 out[3] = (qbyte) ((in[3] + in[7]) >> 1);
1185 else if (bytesperpixel == 3)
1187 for (y = 0;y < *height;y++)
1189 for (x = 0;x < *width;x++)
1191 out[0] = (qbyte) ((in[0] + in[3]) >> 1);
1192 out[1] = (qbyte) ((in[1] + in[4]) >> 1);
1193 out[2] = (qbyte) ((in[2] + in[5]) >> 1);
1200 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1205 if (*height > destheight)
1209 if (bytesperpixel == 4)
1211 for (y = 0;y < *height;y++)
1213 for (x = 0;x < *width;x++)
1215 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1216 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1217 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1218 out[3] = (qbyte) ((in[3] + in[nextrow+3]) >> 1);
1222 in += nextrow; // skip a line
1225 else if (bytesperpixel == 3)
1227 for (y = 0;y < *height;y++)
1229 for (x = 0;x < *width;x++)
1231 out[0] = (qbyte) ((in[0] + in[nextrow ]) >> 1);
1232 out[1] = (qbyte) ((in[1] + in[nextrow+1]) >> 1);
1233 out[2] = (qbyte) ((in[2] + in[nextrow+2]) >> 1);
1237 in += nextrow; // skip a line
1241 Sys_Error("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
1244 Sys_Error("Image_MipReduce: desired size already achieved\n");