1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci/**
27bf215546Sopenharmony_ci * \file texcompress_fxt1.c
28bf215546Sopenharmony_ci * GL_3DFX_texture_compression_FXT1 support.
29bf215546Sopenharmony_ci */
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "errors.h"
33bf215546Sopenharmony_ci#include "glheader.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "image.h"
36bf215546Sopenharmony_ci#include "macros.h"
37bf215546Sopenharmony_ci#include "mipmap.h"
38bf215546Sopenharmony_ci#include "texcompress.h"
39bf215546Sopenharmony_ci#include "texcompress_fxt1.h"
40bf215546Sopenharmony_ci#include "texstore.h"
41bf215546Sopenharmony_ci#include "mtypes.h"
42bf215546Sopenharmony_ci#include "util/format/u_format_fxt1.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci/**
45bf215546Sopenharmony_ci * Compress the user's image to either FXT1_RGB or FXT1_RGBA.
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_ciGLboolean
48bf215546Sopenharmony_ci_mesa_texstore_fxt1(TEXSTORE_PARAMS)
49bf215546Sopenharmony_ci{
50bf215546Sopenharmony_ci   const uint8_t *pixels;
51bf215546Sopenharmony_ci   int32_t srcRowStride;
52bf215546Sopenharmony_ci   uint8_t *dst;
53bf215546Sopenharmony_ci   const uint8_t *tempImage = NULL;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   assert(dstFormat == MESA_FORMAT_RGB_FXT1 || dstFormat == MESA_FORMAT_RGBA_FXT1);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   if (srcFormat != GL_RGBA ||
58bf215546Sopenharmony_ci       srcType != GL_UNSIGNED_BYTE ||
59bf215546Sopenharmony_ci       ctx->_ImageTransferState ||
60bf215546Sopenharmony_ci       srcPacking->SwapBytes) {
61bf215546Sopenharmony_ci      /* convert image to RGBA/uint8_t */
62bf215546Sopenharmony_ci      uint8_t *tempImageSlices[1];
63bf215546Sopenharmony_ci      int rgbaRowStride = 4 * srcWidth * sizeof(uint8_t);
64bf215546Sopenharmony_ci      tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(uint8_t));
65bf215546Sopenharmony_ci      if (!tempImage)
66bf215546Sopenharmony_ci         return GL_FALSE; /* out of memory */
67bf215546Sopenharmony_ci      tempImageSlices[0] = (uint8_t *) tempImage;
68bf215546Sopenharmony_ci      _mesa_texstore(ctx, dims,
69bf215546Sopenharmony_ci                     baseInternalFormat,
70bf215546Sopenharmony_ci                     MESA_FORMAT_RGBA_UNORM8,
71bf215546Sopenharmony_ci                     rgbaRowStride, tempImageSlices,
72bf215546Sopenharmony_ci                     srcWidth, srcHeight, srcDepth,
73bf215546Sopenharmony_ci                     srcFormat, srcType, srcAddr,
74bf215546Sopenharmony_ci                     srcPacking);
75bf215546Sopenharmony_ci      pixels = tempImage;
76bf215546Sopenharmony_ci      srcRowStride = 4 * srcWidth;
77bf215546Sopenharmony_ci      srcFormat = GL_RGBA;
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci   else {
80bf215546Sopenharmony_ci      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
81bf215546Sopenharmony_ci                                     srcFormat, srcType, 0, 0);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci      srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat,
84bf215546Sopenharmony_ci                                            srcType) / sizeof(uint8_t);
85bf215546Sopenharmony_ci   }
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   dst = dstSlices[0];
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   if (dstFormat == MESA_FORMAT_RGB_FXT1)
90bf215546Sopenharmony_ci      util_format_fxt1_rgb_pack_rgba_8unorm(dst, dstRowStride, pixels, srcRowStride, srcWidth, srcHeight);
91bf215546Sopenharmony_ci   else
92bf215546Sopenharmony_ci      util_format_fxt1_rgba_pack_rgba_8unorm(dst, dstRowStride, pixels, srcRowStride, srcWidth, srcHeight);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   free((void*) tempImage);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   return GL_TRUE;
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cistatic void
100bf215546Sopenharmony_cifetch_rgb_fxt1(const uint8_t *map,
101bf215546Sopenharmony_ci               int32_t rowStride, int32_t i, int32_t j, float *texel)
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   map += rowStride * (i / 8);
104bf215546Sopenharmony_ci   map += 16 * (j / 4);
105bf215546Sopenharmony_ci   util_format_fxt1_rgb_fetch_rgba(texel, map, i & 7, j & 3);
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cistatic void
110bf215546Sopenharmony_cifetch_rgba_fxt1(const uint8_t *map,
111bf215546Sopenharmony_ci                int32_t rowStride, int32_t i, int32_t j, float *texel)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   map += rowStride * (i / 8);
114bf215546Sopenharmony_ci   map += 16 * (j / 4);
115bf215546Sopenharmony_ci   util_format_fxt1_rgba_fetch_rgba(texel, map, i & 7, j & 3);
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_cicompressed_fetch_func
120bf215546Sopenharmony_ci_mesa_get_fxt_fetch_func(mesa_format format)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   switch (format) {
123bf215546Sopenharmony_ci   case MESA_FORMAT_RGB_FXT1:
124bf215546Sopenharmony_ci      return fetch_rgb_fxt1;
125bf215546Sopenharmony_ci   case MESA_FORMAT_RGBA_FXT1:
126bf215546Sopenharmony_ci      return fetch_rgba_fxt1;
127bf215546Sopenharmony_ci   default:
128bf215546Sopenharmony_ci      return NULL;
129bf215546Sopenharmony_ci   }
130bf215546Sopenharmony_ci}
131