+ Con_Printf ("Image_Resample: 3D resampling not supported\n");
+ if (*width > destwidth)
+ *width >>= 1;
+ if (*height > destheight)
+ *height >>= 1;
+ if (*depth > destdepth)
+ *depth >>= 1;
+ return;
+ }
+ // note: if given odd width/height this discards the last row/column of
+ // pixels, rather than doing a proper box-filter scale down
+ inrow = in;
+ nextrow = *width * bytesperpixel;
+ if (*width > destwidth)
+ {
+ *width >>= 1;
+ if (*height > destheight)
+ {
+ // reduce both
+ *height >>= 1;
+ if (bytesperpixel == 4)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow * 2)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
+ out[1] = (unsigned char) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
+ out[2] = (unsigned char) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
+ out[3] = (unsigned char) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
+ out += 4;
+ in += 8;
+ }
+ }
+ }
+ else if (bytesperpixel == 3)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow * 2)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
+ out[1] = (unsigned char) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
+ out[2] = (unsigned char) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
+ out += 3;
+ in += 6;
+ }
+ }
+ }
+ else
+ Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+ }
+ else
+ {
+ // reduce width
+ if (bytesperpixel == 4)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[4]) >> 1);
+ out[1] = (unsigned char) ((in[1] + in[5]) >> 1);
+ out[2] = (unsigned char) ((in[2] + in[6]) >> 1);
+ out[3] = (unsigned char) ((in[3] + in[7]) >> 1);
+ out += 4;
+ in += 8;
+ }
+ }
+ }
+ else if (bytesperpixel == 3)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[3]) >> 1);
+ out[1] = (unsigned char) ((in[1] + in[4]) >> 1);
+ out[2] = (unsigned char) ((in[2] + in[5]) >> 1);
+ out += 3;
+ in += 6;
+ }
+ }
+ }
+ else
+ Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+ }
+ }
+ else
+ {
+ if (*height > destheight)
+ {
+ // reduce height
+ *height >>= 1;
+ if (bytesperpixel == 4)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow * 2)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
+ out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
+ out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
+ out[3] = (unsigned char) ((in[3] + in[nextrow+3]) >> 1);
+ out += 4;
+ in += 4;
+ }
+ }
+ }
+ else if (bytesperpixel == 3)
+ {
+ for (y = 0;y < *height;y++, inrow += nextrow * 2)
+ {
+ for (in = inrow, x = 0;x < *width;x++)
+ {
+ out[0] = (unsigned char) ((in[0] + in[nextrow ]) >> 1);
+ out[1] = (unsigned char) ((in[1] + in[nextrow+1]) >> 1);
+ out[2] = (unsigned char) ((in[2] + in[nextrow+2]) >> 1);
+ out += 3;
+ in += 3;
+ }
+ }
+ }
+ else
+ Con_Printf ("Image_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
+ }
+ else
+ Con_Printf ("Image_MipReduce: desired size already achieved\n");
+ }
+}
+
+void Image_HeightmapToNormalmap(const unsigned char *inpixels, unsigned char *outpixels, int width, int height, int clamp, float bumpscale)
+{
+ int x, y, x1, x2, y1, y2;
+ const unsigned char *b, *row[3];
+ int p[5];
+ unsigned char *out;
+ float iwidth, iheight, ibumpscale, n[3];
+ iwidth = 1.0f / width;
+ iheight = 1.0f / height;
+ ibumpscale = (255.0f * 6.0f) / bumpscale;
+ out = outpixels;
+ for (y = 0, y1 = height-1;y < height;y1 = y, y++)
+ {
+ y2 = y + 1;if (y2 >= height) y2 = 0;
+ row[0] = inpixels + (y1 * width) * 4;
+ row[1] = inpixels + (y * width) * 4;
+ row[2] = inpixels + (y2 * width) * 4;
+ for (x = 0, x1 = width-1;x < width;x1 = x, x++)
+ {
+ x2 = x + 1;if (x2 >= width) x2 = 0;
+ // left, right
+ b = row[1] + x1 * 4;p[0] = (b[0] + b[1] + b[2]);
+ b = row[1] + x2 * 4;p[1] = (b[0] + b[1] + b[2]);
+ // above, below
+ b = row[0] + x * 4;p[2] = (b[0] + b[1] + b[2]);
+ b = row[2] + x * 4;p[3] = (b[0] + b[1] + b[2]);
+ // center
+ b = row[1] + x * 4;p[4] = (b[0] + b[1] + b[2]);
+ // calculate a normal from the slopes
+ n[0] = p[0] - p[1];
+ n[1] = p[3] - p[2];
+ n[2] = ibumpscale;
+ VectorNormalize(n);
+ // turn it into a dot3 rgb vector texture
+ out[0] = (int)(128.0f + n[0] * 127.0f);
+ out[1] = (int)(128.0f + n[1] * 127.0f);
+ out[2] = (int)(128.0f + n[2] * 127.0f);
+ out[3] = (p[4]) / 3;
+ out += 4;
+ }