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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 **************************************************************************/
24
25#include <stdio.h>
26#include "u_format.h"
27#include "u_format_rgtc.h"
28#include "u_format_latc.h"
29#include "util/rgtc.h"
30#include "util/u_math.h"
31
32void
33util_format_latc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
34{
35   /* Fix warnings here: */
36   (void) util_format_unsigned_encode_rgtc_ubyte;
37   (void) util_format_signed_encode_rgtc_ubyte;
38
39   util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1);
40   dst[1] = dst[0];
41   dst[2] = dst[0];
42   dst[3] = 255;
43}
44
45void
46util_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)
47{
48   util_format_rgtc1_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
49}
50
51void
52util_format_latc1_unorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride, const uint8_t *src_row,
53					 unsigned src_stride, unsigned width, unsigned height)
54{
55   util_format_rgtc1_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
56}
57
58void
59util_format_latc1_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
60{
61   unsigned x, y, i, j;
62   int block_size = 8;
63
64   for(y = 0; y < height; y += 4) {
65      const uint8_t *src = src_row;
66      for(x = 0; x < width; x += 4) {
67         for(j = 0; j < 4; ++j) {
68            for(i = 0; i < 4; ++i) {
69               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
70               uint8_t tmp_r;
71               util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
72               dst[0] =
73               dst[1] =
74               dst[2] = ubyte_to_float(tmp_r);
75               dst[3] = 1.0;
76            }
77         }
78         src += block_size;
79      }
80      src_row += src_stride;
81   }
82}
83
84void
85util_format_latc1_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
86{
87   util_format_rgtc1_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
88}
89
90void
91util_format_latc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
92{
93   uint8_t tmp_r;
94
95   util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
96   dst[0] =
97   dst[1] =
98   dst[2] = ubyte_to_float(tmp_r);
99   dst[3] = 1.0;
100}
101
102void
103util_format_latc1_snorm_fetch_rgba_8unorm(UNUSED uint8_t *dst, UNUSED const uint8_t *src,
104                                          UNUSED unsigned i, UNUSED unsigned j)
105{
106   fprintf(stderr,"%s\n", __func__);
107}
108
109void
110util_format_latc1_snorm_unpack_rgba_8unorm(UNUSED uint8_t *dst_row, UNUSED unsigned dst_stride,
111                                           UNUSED const uint8_t *src_row, UNUSED unsigned src_stride,
112                                           UNUSED unsigned width, UNUSED unsigned height)
113{
114   fprintf(stderr,"%s\n", __func__);
115}
116
117void
118util_format_latc1_snorm_pack_rgba_8unorm(UNUSED uint8_t *dst_row, UNUSED unsigned dst_stride,
119                                         UNUSED const uint8_t *src_row, UNUSED unsigned src_stride,
120                                         UNUSED unsigned width, UNUSED unsigned height)
121{
122   fprintf(stderr,"%s\n", __func__);
123}
124
125void
126util_format_latc1_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
127{
128   util_format_rgtc1_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height);
129}
130
131void
132util_format_latc1_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
133{
134   unsigned x, y, i, j;
135   int block_size = 8;
136
137   for(y = 0; y < height; y += 4) {
138      const int8_t *src = (int8_t *)src_row;
139      for(x = 0; x < width; x += 4) {
140         for(j = 0; j < 4; ++j) {
141            for(i = 0; i < 4; ++i) {
142               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
143               int8_t tmp_r;
144               util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1);
145               dst[0] =
146               dst[1] =
147               dst[2] = byte_to_float_tex(tmp_r);
148               dst[3] = 1.0;
149            }
150         }
151         src += block_size;
152      }
153      src_row += src_stride;
154   }
155}
156
157void
158util_format_latc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
159{
160   int8_t tmp_r;
161
162   util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 1);
163   dst[0] =
164   dst[1] =
165   dst[2] = byte_to_float_tex(tmp_r);
166   dst[3] = 1.0;
167}
168
169
170void
171util_format_latc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
172{
173   util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 2);
174   dst[1] = dst[0];
175   dst[2] = dst[0];
176   util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, dst + 3, 2);
177}
178
179void
180util_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)
181{
182   util_format_rgtc2_unorm_unpack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
183}
184
185void
186util_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)
187{
188   util_format_rgtc2_unorm_pack_rgba_8unorm(dst_row, dst_stride, src_row, src_stride, width, height);
189}
190
191void
192util_format_latc2_unorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride, const float *src_row, unsigned src_stride, unsigned width, unsigned height)
193{
194   util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 3);
195}
196
197void
198util_format_latc2_unorm_unpack_rgba_float(float *dst_row, unsigned dst_stride, const uint8_t *src_row, unsigned src_stride, unsigned width, unsigned height)
199{
200   unsigned x, y, i, j;
201   int block_size = 16;
202
203   for(y = 0; y < height; y += 4) {
204      const uint8_t *src = src_row;
205      for(x = 0; x < width; x += 4) {
206         for(j = 0; j < 4; ++j) {
207            for(i = 0; i < 4; ++i) {
208               float *dst = dst_row + (y + j)*dst_stride/sizeof(*dst_row) + (x + i)*4;
209               uint8_t tmp_r, tmp_g;
210               util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
211               util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
212               dst[0] =
213               dst[1] =
214               dst[2] = ubyte_to_float(tmp_r);
215               dst[3] = ubyte_to_float(tmp_g);
216            }
217         }
218         src += block_size;
219      }
220      src_row += src_stride;
221   }
222}
223
224void
225util_format_latc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j)
226{
227   uint8_t tmp_r, tmp_g;
228
229   util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
230   util_format_unsigned_fetch_texel_rgtc(0, src + 8, i, j, &tmp_g, 2);
231   dst[0] =
232   dst[1] =
233   dst[2] = ubyte_to_float(tmp_r);
234   dst[3] = ubyte_to_float(tmp_g);
235}
236
237
238void
239util_format_latc2_snorm_fetch_rgba_8unorm(UNUSED uint8_t *dst, UNUSED const uint8_t *src,
240                                          UNUSED unsigned i, UNUSED unsigned j)
241{
242   fprintf(stderr,"%s\n", __func__);
243}
244
245void
246util_format_latc2_snorm_unpack_rgba_8unorm(UNUSED uint8_t *dst_row, UNUSED unsigned dst_stride,
247                                           UNUSED const uint8_t *src_row, UNUSED unsigned src_stride,
248                                           UNUSED unsigned width, UNUSED unsigned height)
249{
250   fprintf(stderr,"%s\n", __func__);
251}
252
253void
254util_format_latc2_snorm_pack_rgba_8unorm(UNUSED uint8_t *dst_row, UNUSED unsigned dst_stride,
255                                         UNUSED const uint8_t *src_row, UNUSED unsigned src_stride,
256                                         UNUSED unsigned width, UNUSED 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               util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 2);
275               util_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   util_format_signed_fetch_texel_rgtc(0, (int8_t *)src, i, j, &tmp_r, 2);
300   util_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