{
static const GLenum format[] =
{ 0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
+ static const GLenum type[] =
+ { 0, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
+ GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4 };
GLuint o = 0;
glTexImage2D(GL_TEXTURE_2D, 0,
format[b], W, H, 0,
- format[b], GL_UNSIGNED_BYTE, q ? q : p);
+ format[b], type[b], q ? q : p);
if (q) free(q);
return o;
}
+static void *image_to_16bpp(unsigned char *data, int w, int h, int b)
+{
+ unsigned char *p, *row;
+ int i, j;
+
+ if (b <= 2) return data;
+
+ if ((p = malloc(w * h * 2)))
+ {
+ for (i = 0; i < h; i++)
+ {
+ row = data + i * w * b;
+ for (j = 0; j < w; j++)
+ {
+ unsigned short packed, *pp;
+ if (b == 4)
+ packed =
+ (row[j * b] >> 4) << 12 |
+ (row[j * b + 1] >> 4) << 8 |
+ (row[j * b + 2] >> 4) << 4 |
+ row[j * b + 3] >> 4;
+ else
+ packed =
+ (row[j * b] >> 3) << 11 |
+ (row[j * b + 1] >> 2) << 5 |
+ (row[j * b + 2] >> 3);
+ pp = (unsigned short *)&p[i * w * 2 + j * 2];
+ *pp = packed;
+ }
+ }
+ }
+ free(data);
+ return p;
+}
+
/*
* Load an image from the named file. Return an OpenGL texture object.
*/
if ((p = image_load(filename, &w, &h, &b)))
{
+ p = image_to_16bpp(p, w, h, b);
o = make_texture(p, w, h, b);
free(p);
}
/* Create the OpenGL texture object. */
+ p = image_to_16bpp(p, w2, h2, b);
o = make_texture(p, w2, h2, b);
free(p);