Home | History | Annotate | Download | only in main
      1 /*
      2  * Mesa 3-D graphics library
      3  *
      4  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included
     14  * in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     20  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  */
     23 
     24 /**
     25  * Code to convert compressed/paletted texture images to ordinary images.
     26  * See the GL_OES_compressed_paletted_texture spec at
     27  * http://khronos.org/registry/gles/extensions/OES/OES_compressed_paletted_texture.txt
     28  *
     29  * XXX this makes it impossible to add hardware support...
     30  */
     31 
     32 
     33 #include "glheader.h"
     34 #include "compiler.h" /* for ASSERT */
     35 #include "context.h"
     36 #include "mfeatures.h"
     37 #include "mtypes.h"
     38 #include "imports.h"
     39 #include "pixelstore.h"
     40 #include "texcompress_cpal.h"
     41 #include "teximage.h"
     42 
     43 #if FEATURE_ES
     44 
     45 
     46 static const struct cpal_format_info {
     47    GLenum cpal_format;
     48    GLenum format;
     49    GLenum type;
     50    GLuint palette_size;
     51    GLuint size;
     52 } formats[] = {
     53    { GL_PALETTE4_RGB8_OES,     GL_RGB,  GL_UNSIGNED_BYTE,           16, 3 },
     54    { GL_PALETTE4_RGBA8_OES,    GL_RGBA, GL_UNSIGNED_BYTE,           16, 4 },
     55    { GL_PALETTE4_R5_G6_B5_OES, GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,    16, 2 },
     56    { GL_PALETTE4_RGBA4_OES,    GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,  16, 2 },
     57    { GL_PALETTE4_RGB5_A1_OES,  GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1,  16, 2 },
     58    { GL_PALETTE8_RGB8_OES,     GL_RGB,  GL_UNSIGNED_BYTE,          256, 3 },
     59    { GL_PALETTE8_RGBA8_OES,    GL_RGBA, GL_UNSIGNED_BYTE,          256, 4 },
     60    { GL_PALETTE8_R5_G6_B5_OES, GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,   256, 2 },
     61    { GL_PALETTE8_RGBA4_OES,    GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 256, 2 },
     62    { GL_PALETTE8_RGB5_A1_OES,  GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 256, 2 }
     63 };
     64 
     65 
     66 /**
     67  * Get a color/entry from the palette.
     68  */
     69 static GLuint
     70 get_palette_entry(const struct cpal_format_info *info, const GLubyte *palette,
     71                   GLuint index, GLubyte *pixel)
     72 {
     73    memcpy(pixel, palette + info->size * index, info->size);
     74    return info->size;
     75 }
     76 
     77 
     78 /**
     79  * Convert paletted texture to color texture.
     80  */
     81 static void
     82 paletted_to_color(const struct cpal_format_info *info, const GLubyte *palette,
     83                   const void *indices, GLuint num_pixels, GLubyte *image)
     84 {
     85    GLubyte *pix = image;
     86    GLuint remain, i;
     87 
     88    if (info->palette_size == 16) {
     89       /* 4 bits per index */
     90       const GLubyte *ind = (const GLubyte *) indices;
     91 
     92       /* two pixels per iteration */
     93       remain = num_pixels % 2;
     94       for (i = 0; i < num_pixels / 2; i++) {
     95          pix += get_palette_entry(info, palette, (ind[i] >> 4) & 0xf, pix);
     96          pix += get_palette_entry(info, palette, ind[i] & 0xf, pix);
     97       }
     98       if (remain) {
     99          get_palette_entry(info, palette, (ind[i] >> 4) & 0xf, pix);
    100       }
    101    }
    102    else {
    103       /* 8 bits per index */
    104       const GLubyte *ind = (const GLubyte *) indices;
    105       for (i = 0; i < num_pixels; i++)
    106          pix += get_palette_entry(info, palette, ind[i], pix);
    107    }
    108 }
    109 
    110 unsigned
    111 _mesa_cpal_compressed_size(int level, GLenum internalFormat,
    112 			   unsigned width, unsigned height)
    113 {
    114    const struct cpal_format_info *info;
    115    const int num_levels = -level + 1;
    116    int lvl;
    117    unsigned w, h, expect_size;
    118 
    119    if (internalFormat < GL_PALETTE4_RGB8_OES
    120        || internalFormat > GL_PALETTE8_RGB5_A1_OES) {
    121       return 0;
    122    }
    123 
    124    info = &formats[internalFormat - GL_PALETTE4_RGB8_OES];
    125    ASSERT(info->cpal_format == internalFormat);
    126 
    127    expect_size = info->palette_size * info->size;
    128    for (lvl = 0; lvl < num_levels; lvl++) {
    129       w = width >> lvl;
    130       if (!w)
    131          w = 1;
    132       h = height >> lvl;
    133       if (!h)
    134          h = 1;
    135 
    136       if (info->palette_size == 16)
    137          expect_size += (w * h  + 1) / 2;
    138       else
    139          expect_size += w * h;
    140    }
    141 
    142    return expect_size;
    143 }
    144 
    145 void
    146 _mesa_cpal_compressed_format_type(GLenum internalFormat, GLenum *format,
    147 				  GLenum *type)
    148 {
    149    const struct cpal_format_info *info;
    150 
    151    if (internalFormat < GL_PALETTE4_RGB8_OES
    152        || internalFormat > GL_PALETTE8_RGB5_A1_OES) {
    153       return;
    154    }
    155 
    156    info = &formats[internalFormat - GL_PALETTE4_RGB8_OES];
    157    *format = info->format;
    158    *type = info->type;
    159 }
    160 
    161 /**
    162  * Convert a call to glCompressedTexImage2D() where internalFormat is a
    163  *  compressed palette format into a regular GLubyte/RGBA glTexImage2D() call.
    164  */
    165 void
    166 _mesa_cpal_compressed_teximage2d(GLenum target, GLint level,
    167 				 GLenum internalFormat,
    168 				 GLsizei width, GLsizei height,
    169 				 GLsizei imageSize, const void *palette)
    170 {
    171    const struct cpal_format_info *info;
    172    GLint lvl, num_levels;
    173    const GLubyte *indices;
    174    GLint saved_align, align;
    175    GET_CURRENT_CONTEXT(ctx);
    176 
    177    /* By this point, the internalFormat should have been validated.
    178     */
    179    assert(internalFormat >= GL_PALETTE4_RGB8_OES
    180 	  && internalFormat <= GL_PALETTE8_RGB5_A1_OES);
    181 
    182    info = &formats[internalFormat - GL_PALETTE4_RGB8_OES];
    183 
    184    num_levels = -level + 1;
    185 
    186    /* first image follows the palette */
    187    indices = (const GLubyte *) palette + info->palette_size * info->size;
    188 
    189    saved_align = ctx->Unpack.Alignment;
    190    align = saved_align;
    191 
    192    for (lvl = 0; lvl < num_levels; lvl++) {
    193       GLsizei w, h;
    194       GLuint num_texels;
    195       GLubyte *image = NULL;
    196 
    197       w = width >> lvl;
    198       if (!w)
    199          w = 1;
    200       h = height >> lvl;
    201       if (!h)
    202          h = 1;
    203       num_texels = w * h;
    204       if (w * info->size % align) {
    205          _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 1);
    206          align = 1;
    207       }
    208 
    209       /* allocate and fill dest image buffer */
    210       if (palette) {
    211          image = (GLubyte *) malloc(num_texels * info->size);
    212          paletted_to_color(info, palette, indices, num_texels, image);
    213       }
    214 
    215       _mesa_TexImage2D(target, lvl, info->format, w, h, 0,
    216                        info->format, info->type, image);
    217       if (image)
    218          free(image);
    219 
    220       /* advance index pointer to point to next src mipmap */
    221       if (info->palette_size == 16)
    222          indices += (num_texels + 1) / 2;
    223       else
    224          indices += num_texels;
    225    }
    226 
    227    if (saved_align != align)
    228       _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, saved_align);
    229 }
    230 
    231 #endif
    232