1/**************************************************************************
2 *
3 * Copyright (C) 2011 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 **************************************************************************/
23
24#include <stdio.h>
25#include "u_math.h"
26#include "u_format.h"
27#include "u_format_rgtc.h"
28#include "u_format_latc.h"
29
30static void u_format_unsigned_encode_rgtc_ubyte(uint8_t *blkaddr, uint8_t srccolors[4][4],
31					       int numxpixels, int numypixels);
32
33static void u_format_unsigned_fetch_texel_rgtc(unsigned srcRowStride, const uint8_t *pixdata,
34					       unsigned i, unsigned j, uint8_t *value, unsigned comps);
35
36static void u_format_signed_encode_rgtc_ubyte(int8_t *blkaddr, int8_t srccolors[4][4],
37					     int numxpixels, int numypixels);
38
39static void u_format_signed_fetch_texel_rgtc(unsigned srcRowStride, const int8_t *pixdata,
40					       unsigned i, unsigned j, int8_t *value, unsigned comps);
41
42void
43util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
44{
45   /* Fix warnings here: */
46   (void) u_format_unsigned_encode_rgtc_ubyte;
47   (void) u_format_signed_encode_rgtc_ubyte;
48
49   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);
50   dst[1] = dst[0];
51   dst[2] = dst[0];
52   dst[3] = 255;
53}
54
55void
56util_format_latc1_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
57{
58   util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
59}
60
61void
62util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row,
63					 unsigned src_stride, unsigned width, unsigned height)
64{
65   util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
66}
67
68void
69util_format_latc1_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
70{
71   unsigned x, y, i, j;
72   int block_size = 8;
73
74   for(y = 0; y < height; y += 4) {
75      const uint8_t *src = src_row;
76      for(x = 0; x < width; x += 4) {
77         for(j = 0; j < 4; ++j) {
78            for(i = 0; i < 4; ++i) {
79               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
80               uint8_t tmp_r;
81               u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
82               dst[0] =
83               dst[1] =
84               dst[2] = ubyte_to_float(tmp_r);
85               dst[3] = 1.0;
86            }
87         }
88         src += block_size;
89      }
90      src_row += src_stride;
91   }
92}
93
94void
95util_format_latc1_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
96{
97   util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
98}
99
100void
101util_format_latc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
102{
103   uint8_t tmp_r;
104
105   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
106   dst[0] =
107   dst[1] =
108   dst[2] = ubyte_to_float(tmp_r);
109   dst[3] = 1.0;
110}
111
112void
113util_format_latc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
114{
115   fprintf(stderr,"%s\n", __func__);
116}
117
118void
119util_format_latc1_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
120{
121   fprintf(stderr,"%s\n", __func__);
122}
123
124void
125util_format_latc1_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
126{
127   fprintf(stderr,"%s\n", __func__);
128}
129
130void
131util_format_latc1_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
132{
133   util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
134}
135
136void
137util_format_latc1_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
138{
139   unsigned x, y, i, j;
140   int block_size = 8;
141
142   for(y = 0; y < height; y += 4) {
143      const int8_t *src = (int8_t *)src_row;
144      for(x = 0; x < width; x += 4) {
145         for(j = 0; j < 4; ++j) {
146            for(i = 0; i < 4; ++i) {
147               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
148               int8_t tmp_r;
149               u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
150               dst[0] =
151               dst[1] =
152               dst[2] = byte_to_float_tex(tmp_r);
153               dst[3] = 1.0;
154            }
155         }
156         src += block_size;
157      }
158      src_row += src_stride;
159   }
160}
161
162void
163util_format_latc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
164{
165   int8_t tmp_r;
166
167   u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);
168   dst[0] =
169   dst[1] =
170   dst[2] = byte_to_float_tex(tmp_r);
171   dst[3] = 1.0;
172}
173
174
175void
176util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
177{
178   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
179   dst[1] = dst[0];
180   dst[2] = dst[0];
181   u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);
182}
183
184void
185util_format_latc2_unorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
186{
187   util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
188}
189
190void
191util_format_latc2_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
192{
193   util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
194}
195
196void
197util_format_latc2_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
198{
199   util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
200}
201
202void
203util_format_latc2_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
204{
205   unsigned x, y, i, j;
206   int block_size = 16;
207
208   for(y = 0; y < height; y += 4) {
209      const uint8_t *src = src_row;
210      for(x = 0; x < width; x += 4) {
211         for(j = 0; j < 4; ++j) {
212            for(i = 0; i < 4; ++i) {
213               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
214               uint8_t tmp_r, tmp_g;
215               u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
216               u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
217               dst[0] =
218               dst[1] =
219               dst[2] = ubyte_to_float(tmp_r);
220               dst[3] = ubyte_to_float(tmp_g);
221            }
222         }
223         src += block_size;
224      }
225      src_row += src_stride;
226   }
227}
228
229void
230util_format_latc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
231{
232   uint8_t tmp_r, tmp_g;
233
234   u_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
235   u_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
236   dst[0] =
237   dst[1] =
238   dst[2] = ubyte_to_float(tmp_r);
239   dst[3] = ubyte_to_float(tmp_g);
240}
241
242
243void
244util_format_latc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
245{
246   fprintf(stderr,"%s\n", __func__);
247}
248
249void
250util_format_latc2_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
251{
252   fprintf(stderr,"%s\n", __func__);
253}
254
255void
256util_format_latc2_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
257{
258   fprintf(stderr,"%s\n", __func__);
259}
260
261void
262util_format_latc2_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
263{
264   unsigned x, y, i, j;
265   int block_size = 16;
266
267   for(y = 0; y < height; y += 4) {
268      const int8_t *src = (int8_t *)src_row;
269      for(x = 0; x < width; x += 4) {
270         for(j = 0; j < 4; ++j) {
271            for(i = 0; i < 4; ++i) {
272               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
273               int8_t tmp_r, tmp_g;
274               u_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
275               u_format_signed_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
276               dst[0] =
277               dst[1] =
278               dst[2] = byte_to_float_tex(tmp_r);
279               dst[3] = byte_to_float_tex(tmp_g);
280            }
281         }
282         src += block_size;
283      }
284      src_row += src_stride;
285   }
286}
287
288void
289util_format_latc2_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
290{
291   util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
292}
293
294void
295util_format_latc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
296{
297   int8_t tmp_r, tmp_g;
298
299   u_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);
300   u_format_signed_fetch_texel_rgtc(0, (int8_t *)src + 8, i, j, &tmp_g, 2);
301   dst[0] =
302   dst[1] =
303   dst[2] = byte_to_float_tex(tmp_r);
304   dst[3] = byte_to_float_tex(tmp_g);
305}
306
307
308#define TAG(x) u_format_unsigned_##x
309#define TYPE uint8_t
310#define T_MIN 0
311#define T_MAX 255
312
313#include "../../../mesa/main/texcompress_rgtc_tmp.h"
314
315#undef TYPE
316#undef TAG
317#undef T_MIN
318#undef T_MAX
319
320
321#define TAG(x) u_format_signed_##x
322#define TYPE int8_t
323#define T_MIN (int8_t)-128
324#define T_MAX (int8_t)127
325
326#include "../../../mesa/main/texcompress_rgtc_tmp.h"
327
328#undef TYPE
329#undef TAG
330#undef T_MIN
331#undef T_MAX
332