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 **************************************************************************/
26 #include "util/format/u_format.h"
27 #include "util/format/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(void *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
= (float *)((uint8_t *)dst_row
+ (y
+ j
)*dst_stride
+ (x
+ i
)*16);
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(void *in_dst
, const uint8_t *src
, unsigned i
, unsigned j
)
140 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
141 dst
[0] = ubyte_to_float(tmp_r
);
148 util_format_rgtc1_snorm_fetch_rgba_8unorm(UNUSED
uint8_t *dst
, UNUSED
const uint8_t *src
,
149 UNUSED
unsigned i
, UNUSED
unsigned j
)
151 fprintf(stderr
,"%s\n", __func__
);
155 util_format_rgtc1_snorm_unpack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
156 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
157 UNUSED
unsigned width
, UNUSED
unsigned height
)
159 fprintf(stderr
,"%s\n", __func__
);
163 util_format_rgtc1_snorm_pack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
164 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
165 UNUSED
unsigned width
, UNUSED
unsigned height
)
167 fprintf(stderr
,"%s\n", __func__
);
171 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
)
173 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
176 for(y
= 0; y
< height
; y
+= bh
) {
177 int8_t *dst
= (int8_t *)dst_row
;
178 for(x
= 0; x
< width
; x
+= bw
) {
179 int8_t tmp
[4][4]; /* [bh][bw][comps] */
180 for(j
= 0; j
< bh
; ++j
) {
181 for(i
= 0; i
< bw
; ++i
) {
182 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
185 util_format_signed_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
186 dst
+= bytes_per_block
;
188 dst_row
+= dst_stride
/ sizeof(*dst_row
);
193 util_format_rgtc1_snorm_unpack_rgba_float(void *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
197 for(y
= 0; y
< height
; y
+= 4) {
198 const int8_t *src
= (int8_t *)src_row
;
199 for(x
= 0; x
< width
; x
+= 4) {
200 for(j
= 0; j
< 4; ++j
) {
201 for(i
= 0; i
< 4; ++i
) {
202 float *dst
= (float *)((uint8_t *)dst_row
+ (y
+ j
)*dst_stride
+ (x
+ i
)*16);
204 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
205 dst
[0] = byte_to_float_tex(tmp_r
);
213 src_row
+= src_stride
;
218 util_format_rgtc1_snorm_fetch_rgba(void *in_dst
, const uint8_t *src
, unsigned i
, unsigned j
)
222 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
223 dst
[0] = byte_to_float_tex(tmp_r
);
231 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
233 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
234 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
240 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
)
242 const unsigned bw
= 4, bh
= 4, comps
= 4;
244 unsigned block_size
= 16;
246 for(y
= 0; y
< height
; y
+= bh
) {
247 const uint8_t *src
= src_row
;
248 for(x
= 0; x
< width
; x
+= bw
) {
249 for(j
= 0; j
< bh
; ++j
) {
250 for(i
= 0; i
< bw
; ++i
) {
251 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
252 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
253 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
260 src_row
+= src_stride
;
265 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
)
267 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
270 for(y
= 0; y
< height
; y
+= bh
) {
271 uint8_t *dst
= dst_row
;
272 for(x
= 0; x
< width
; x
+= bw
) {
273 uint8_t tmp_r
[4][4]; /* [bh][bw] */
274 uint8_t tmp_g
[4][4]; /* [bh][bw] */
275 for(j
= 0; j
< bh
; ++j
) {
276 for(i
= 0; i
< bw
; ++i
) {
277 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
278 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
281 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
282 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
283 dst
+= bytes_per_block
;
285 dst_row
+= dst_stride
/ sizeof(*dst_row
);
290 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
)
292 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
295 for(y
= 0; y
< height
; y
+= bh
) {
296 uint8_t *dst
= dst_row
;
297 for(x
= 0; x
< width
; x
+= bw
) {
298 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
299 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
300 for(j
= 0; j
< bh
; ++j
) {
301 for(i
= 0; i
< bw
; ++i
) {
302 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
303 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
306 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
307 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
308 dst
+= bytes_per_block
;
310 dst_row
+= dst_stride
/ sizeof(*dst_row
);
315 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
)
317 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
321 util_format_rgtc2_unorm_unpack_rgba_float(void *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
325 for(y
= 0; y
< height
; y
+= 4) {
326 const uint8_t *src
= src_row
;
327 for(x
= 0; x
< width
; x
+= 4) {
328 for(j
= 0; j
< 4; ++j
) {
329 for(i
= 0; i
< 4; ++i
) {
330 float *dst
= (float *)((uint8_t *)dst_row
+ (y
+ j
)*dst_stride
+ (x
+ i
)*16);
331 uint8_t tmp_r
, tmp_g
;
332 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
333 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
334 dst
[0] = ubyte_to_float(tmp_r
);
335 dst
[1] = ubyte_to_float(tmp_g
);
342 src_row
+= src_stride
;
347 util_format_rgtc2_unorm_fetch_rgba(void *in_dst
, const uint8_t *src
, unsigned i
, unsigned j
)
350 uint8_t tmp_r
, tmp_g
;
351 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
352 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
353 dst
[0] = ubyte_to_float(tmp_r
);
354 dst
[1] = ubyte_to_float(tmp_g
);
361 util_format_rgtc2_snorm_fetch_rgba_8unorm(UNUSED
uint8_t *dst
, UNUSED
const uint8_t *src
,
362 UNUSED
unsigned i
, UNUSED
unsigned j
)
364 fprintf(stderr
,"%s\n", __func__
);
368 util_format_rgtc2_snorm_unpack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
369 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
370 UNUSED
unsigned width
, UNUSED
unsigned height
)
372 fprintf(stderr
,"%s\n", __func__
);
376 util_format_rgtc2_snorm_pack_rgba_8unorm(UNUSED
uint8_t *dst_row
, UNUSED
unsigned dst_stride
,
377 UNUSED
const uint8_t *src_row
, UNUSED
unsigned src_stride
,
378 UNUSED
unsigned width
, UNUSED
unsigned height
)
380 fprintf(stderr
,"%s\n", __func__
);
384 util_format_rgtc2_snorm_unpack_rgba_float(void *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
388 for(y
= 0; y
< height
; y
+= 4) {
389 const int8_t *src
= (int8_t *)src_row
;
390 for(x
= 0; x
< width
; x
+= 4) {
391 for(j
= 0; j
< 4; ++j
) {
392 for(i
= 0; i
< 4; ++i
) {
393 float *dst
= (float *)((uint8_t *)dst_row
+ (y
+ j
)*dst_stride
+ (x
+ i
)*16);
395 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
396 util_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
397 dst
[0] = byte_to_float_tex(tmp_r
);
398 dst
[1] = byte_to_float_tex(tmp_g
);
405 src_row
+= src_stride
;
410 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
)
412 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
415 for(y
= 0; y
< height
; y
+= bh
) {
416 int8_t *dst
= (int8_t *)dst_row
;
417 for(x
= 0; x
< width
; x
+= bw
) {
418 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
419 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
420 for(j
= 0; j
< bh
; ++j
) {
421 for(i
= 0; i
< bw
; ++i
) {
422 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
423 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
426 util_format_signed_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
427 util_format_signed_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
428 dst
+= bytes_per_block
;
430 dst_row
+= dst_stride
/ sizeof(*dst_row
);
435 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
)
437 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
441 util_format_rgtc2_snorm_fetch_rgba(void *in_dst
, const uint8_t *src
, unsigned i
, unsigned j
)
445 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
446 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
447 dst
[0] = byte_to_float_tex(tmp_r
);
448 dst
[1] = byte_to_float_tex(tmp_g
);