1 /**************************************************************************
3 * Copyright (C) 2011 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
23 **************************************************************************/
27 #include "u_format_rgtc.h"
28 #include "util/u_math.h"
29 #include "util/rgtc.h"
32 util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
34 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
41 util_format_rgtc1_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
43 const unsigned bw
= 4, bh
= 4, comps
= 4;
45 unsigned block_size
= 8;
47 for(y
= 0; y
< height
; y
+= bh
) {
48 const uint8_t *src
= src_row
;
49 for(x
= 0; x
< width
; x
+= bw
) {
50 for(j
= 0; j
< bh
; ++j
) {
51 for(i
= 0; i
< bw
; ++i
) {
52 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
53 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 1);
61 src_row
+= src_stride
;
66 util_format_rgtc1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
,
67 unsigned src_stride
, unsigned width
, unsigned height
)
69 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
72 for(y
= 0; y
< height
; y
+= bh
) {
73 uint8_t *dst
= dst_row
;
74 for(x
= 0; x
< width
; x
+= bw
) {
75 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
76 for(j
= 0; j
< bh
; ++j
) {
77 for(i
= 0; i
< bw
; ++i
) {
78 tmp
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
81 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
82 dst
+= bytes_per_block
;
84 dst_row
+= dst_stride
/ sizeof(*dst_row
);
89 util_format_rgtc1_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
93 for(y
= 0; y
< height
; y
+= 4) {
94 const uint8_t *src
= src_row
;
95 for(x
= 0; x
< width
; x
+= 4) {
96 for(j
= 0; j
< 4; ++j
) {
97 for(i
= 0; i
< 4; ++i
) {
98 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
100 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
101 dst
[0] = ubyte_to_float(tmp_r
);
109 src_row
+= src_stride
;
114 util_format_rgtc1_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
116 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
119 for(y
= 0; y
< height
; y
+= bh
) {
120 uint8_t *dst
= dst_row
;
121 for(x
= 0; x
< width
; x
+= bw
) {
122 uint8_t tmp
[4][4]; /* [bh][bw][comps] */
123 for(j
= 0; j
< bh
; ++j
) {
124 for(i
= 0; i
< bw
; ++i
) {
125 tmp
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
128 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
129 dst
+= bytes_per_block
;
131 dst_row
+= dst_stride
/ sizeof(*dst_row
);
136 util_format_rgtc1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
139 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
140 dst
[0] = ubyte_to_float(tmp_r
);
147 util_format_rgtc1_snorm_fetch_rgba_8unorm(UNUSED
uint8_t *dst
, UNUSED
const uint8_t *src
,
148 UNUSED
unsigned i
, UNUSED
unsigned j
)
150 fprintf(stderr
,"%s\n", __func__
);
154 util_format_rgtc1_snorm_unpack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
155 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
156 UNUSED
unsigned width
, UNUSED
unsigned height
)
158 fprintf(stderr
,"%s\n", __func__
);
162 util_format_rgtc1_snorm_pack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
163 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
164 UNUSED
unsigned width
, UNUSED
unsigned height
)
166 fprintf(stderr
,"%s\n", __func__
);
170 util_format_rgtc1_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
172 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
175 for(y
= 0; y
< height
; y
+= bh
) {
176 int8_t *dst
= (int8_t *)dst_row
;
177 for(x
= 0; x
< width
; x
+= bw
) {
178 int8_t tmp
[4][4]; /* [bh][bw][comps] */
179 for(j
= 0; j
< bh
; ++j
) {
180 for(i
= 0; i
< bw
; ++i
) {
181 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
184 util_format_signed_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
185 dst
+= bytes_per_block
;
187 dst_row
+= dst_stride
/ sizeof(*dst_row
);
192 util_format_rgtc1_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
196 for(y
= 0; y
< height
; y
+= 4) {
197 const int8_t *src
= (int8_t *)src_row
;
198 for(x
= 0; x
< width
; x
+= 4) {
199 for(j
= 0; j
< 4; ++j
) {
200 for(i
= 0; i
< 4; ++i
) {
201 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
203 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
204 dst
[0] = byte_to_float_tex(tmp_r
);
212 src_row
+= src_stride
;
217 util_format_rgtc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
220 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
221 dst
[0] = byte_to_float_tex(tmp_r
);
229 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
231 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
232 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
238 util_format_rgtc2_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
240 const unsigned bw
= 4, bh
= 4, comps
= 4;
242 unsigned block_size
= 16;
244 for(y
= 0; y
< height
; y
+= bh
) {
245 const uint8_t *src
= src_row
;
246 for(x
= 0; x
< width
; x
+= bw
) {
247 for(j
= 0; j
< bh
; ++j
) {
248 for(i
= 0; i
< bw
; ++i
) {
249 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
250 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
251 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
258 src_row
+= src_stride
;
263 util_format_rgtc2_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
265 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
268 for(y
= 0; y
< height
; y
+= bh
) {
269 uint8_t *dst
= dst_row
;
270 for(x
= 0; x
< width
; x
+= bw
) {
271 uint8_t tmp_r
[4][4]; /* [bh][bw] */
272 uint8_t tmp_g
[4][4]; /* [bh][bw] */
273 for(j
= 0; j
< bh
; ++j
) {
274 for(i
= 0; i
< bw
; ++i
) {
275 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
276 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
279 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
280 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
281 dst
+= bytes_per_block
;
283 dst_row
+= dst_stride
/ sizeof(*dst_row
);
288 util_format_rxtc2_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
, unsigned chan2off
)
290 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
293 for(y
= 0; y
< height
; y
+= bh
) {
294 uint8_t *dst
= dst_row
;
295 for(x
= 0; x
< width
; x
+= bw
) {
296 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
297 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
298 for(j
= 0; j
< bh
; ++j
) {
299 for(i
= 0; i
< bw
; ++i
) {
300 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
301 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
304 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
305 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
306 dst
+= bytes_per_block
;
308 dst_row
+= dst_stride
/ sizeof(*dst_row
);
313 util_format_rgtc2_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
315 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
319 util_format_rgtc2_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
323 for(y
= 0; y
< height
; y
+= 4) {
324 const uint8_t *src
= src_row
;
325 for(x
= 0; x
< width
; x
+= 4) {
326 for(j
= 0; j
< 4; ++j
) {
327 for(i
= 0; i
< 4; ++i
) {
328 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
329 uint8_t tmp_r
, tmp_g
;
330 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
331 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
332 dst
[0] = ubyte_to_float(tmp_r
);
333 dst
[1] = ubyte_to_float(tmp_g
);
340 src_row
+= src_stride
;
345 util_format_rgtc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
347 uint8_t tmp_r
, tmp_g
;
348 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
349 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
350 dst
[0] = ubyte_to_float(tmp_r
);
351 dst
[1] = ubyte_to_float(tmp_g
);
358 util_format_rgtc2_snorm_fetch_rgba_8unorm(UNUSED
uint8_t *dst
, UNUSED
const uint8_t *src
,
359 UNUSED
unsigned i
, UNUSED
unsigned j
)
361 fprintf(stderr
,"%s\n", __func__
);
365 util_format_rgtc2_snorm_unpack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
366 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
367 UNUSED
unsigned width
, UNUSED
unsigned height
)
369 fprintf(stderr
,"%s\n", __func__
);
373 util_format_rgtc2_snorm_pack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
374 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
375 UNUSED
unsigned width
, UNUSED
unsigned height
)
377 fprintf(stderr
,"%s\n", __func__
);
381 util_format_rgtc2_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
385 for(y
= 0; y
< height
; y
+= 4) {
386 const int8_t *src
= (int8_t *)src_row
;
387 for(x
= 0; x
< width
; x
+= 4) {
388 for(j
= 0; j
< 4; ++j
) {
389 for(i
= 0; i
< 4; ++i
) {
390 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
392 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
393 util_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
394 dst
[0] = byte_to_float_tex(tmp_r
);
395 dst
[1] = byte_to_float_tex(tmp_g
);
402 src_row
+= src_stride
;
407 util_format_rxtc2_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
, unsigned chan2off
)
409 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
412 for(y
= 0; y
< height
; y
+= bh
) {
413 int8_t *dst
= (int8_t *)dst_row
;
414 for(x
= 0; x
< width
; x
+= bw
) {
415 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
416 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
417 for(j
= 0; j
< bh
; ++j
) {
418 for(i
= 0; i
< bw
; ++i
) {
419 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
420 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
423 util_format_signed_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
424 util_format_signed_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
425 dst
+= bytes_per_block
;
427 dst_row
+= dst_stride
/ sizeof(*dst_row
);
432 util_format_rgtc2_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
, const float *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
434 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
438 util_format_rgtc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
441 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
442 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
443 dst
[0] = byte_to_float_tex(tmp_r
);
444 dst
[1] = byte_to_float_tex(tmp_g
);