1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 
26 /**
27  * \file texcompress_fxt1.c
28  * GL_3DFX_texture_compression_FXT1 support.
29  */
30 
31 
32 #include "errors.h"
33 #include "glheader.h"
34 
35 #include "image.h"
36 #include "macros.h"
37 #include "mipmap.h"
38 #include "texcompress.h"
39 #include "texcompress_fxt1.h"
40 #include "texstore.h"
41 #include "mtypes.h"
42 #include "util/format/u_format_fxt1.h"
43 
44 /**
45  * Compress the user's image to either FXT1_RGB or FXT1_RGBA.
46  */
47 GLboolean
_mesa_texstore_fxt1(TEXSTORE_PARAMS)48 _mesa_texstore_fxt1(TEXSTORE_PARAMS)
49 {
50    const uint8_t *pixels;
51    int32_t srcRowStride;
52    uint8_t *dst;
53    const uint8_t *tempImage = NULL;
54 
55    assert(dstFormat == MESA_FORMAT_RGB_FXT1 || dstFormat == MESA_FORMAT_RGBA_FXT1);
56 
57    if (srcFormat != GL_RGBA ||
58        srcType != GL_UNSIGNED_BYTE ||
59        ctx->_ImageTransferState ||
60        srcPacking->SwapBytes) {
61       /* convert image to RGBA/uint8_t */
62       uint8_t *tempImageSlices[1];
63       int rgbaRowStride = 4 * srcWidth * sizeof(uint8_t);
64       tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(uint8_t));
65       if (!tempImage)
66          return GL_FALSE; /* out of memory */
67       tempImageSlices[0] = (uint8_t *) tempImage;
68       _mesa_texstore(ctx, dims,
69                      baseInternalFormat,
70                      MESA_FORMAT_RGBA_UNORM8,
71                      rgbaRowStride, tempImageSlices,
72                      srcWidth, srcHeight, srcDepth,
73                      srcFormat, srcType, srcAddr,
74                      srcPacking);
75       pixels = tempImage;
76       srcRowStride = 4 * srcWidth;
77       srcFormat = GL_RGBA;
78    }
79    else {
80       pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
81                                      srcFormat, srcType, 0, 0);
82 
83       srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
84                                             srcType) / sizeof(uint8_t);
85    }
86 
87    dst = dstSlices[0];
88 
89    if (dstFormat == MESA_FORMAT_RGB_FXT1)
90       util_format_fxt1_rgb_pack_rgba_8unorm(dst, dstRowStride, pixels, srcRowStride, srcWidth, srcHeight);
91    else
92       util_format_fxt1_rgba_pack_rgba_8unorm(dst, dstRowStride, pixels, srcRowStride, srcWidth, srcHeight);
93 
94    free((void*) tempImage);
95 
96    return GL_TRUE;
97 }
98 
99 static void
fetch_rgb_fxt1(const uint8_t * map,int32_t rowStride,int32_t i,int32_t j,float * texel)100 fetch_rgb_fxt1(const uint8_t *map,
101                int32_t rowStride, int32_t i, int32_t j, float *texel)
102 {
103    map += rowStride * (i / 8);
104    map += 16 * (j / 4);
105    util_format_fxt1_rgb_fetch_rgba(texel, map, i & 7, j & 3);
106 }
107 
108 
109 static void
fetch_rgba_fxt1(const uint8_t * map,int32_t rowStride,int32_t i,int32_t j,float * texel)110 fetch_rgba_fxt1(const uint8_t *map,
111                 int32_t rowStride, int32_t i, int32_t j, float *texel)
112 {
113    map += rowStride * (i / 8);
114    map += 16 * (j / 4);
115    util_format_fxt1_rgba_fetch_rgba(texel, map, i & 7, j & 3);
116 }
117 
118 
119 compressed_fetch_func
_mesa_get_fxt_fetch_func(mesa_format format)120 _mesa_get_fxt_fetch_func(mesa_format format)
121 {
122    switch (format) {
123    case MESA_FORMAT_RGB_FXT1:
124       return fetch_rgb_fxt1;
125    case MESA_FORMAT_RGBA_FXT1:
126       return fetch_rgba_fxt1;
127    default:
128       return NULL;
129    }
130 }
131