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 **************************************************************************/
28 #include "u_format_rgtc.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(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
149 fprintf(stderr
,"%s\n", __func__
);
153 util_format_rgtc1_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
155 fprintf(stderr
,"%s\n", __func__
);
159 util_format_rgtc1_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
161 fprintf(stderr
,"%s\n", __func__
);
165 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
)
167 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 8;
170 for(y
= 0; y
< height
; y
+= bh
) {
171 int8_t *dst
= (int8_t *)dst_row
;
172 for(x
= 0; x
< width
; x
+= bw
) {
173 int8_t tmp
[4][4]; /* [bh][bw][comps] */
174 for(j
= 0; j
< bh
; ++j
) {
175 for(i
= 0; i
< bw
; ++i
) {
176 tmp
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
179 util_format_signed_encode_rgtc_ubyte(dst
, tmp
, 4, 4);
180 dst
+= bytes_per_block
;
182 dst_row
+= dst_stride
/ sizeof(*dst_row
);
187 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
)
191 for(y
= 0; y
< height
; y
+= 4) {
192 const int8_t *src
= (int8_t *)src_row
;
193 for(x
= 0; x
< width
; x
+= 4) {
194 for(j
= 0; j
< 4; ++j
) {
195 for(i
= 0; i
< 4; ++i
) {
196 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
198 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 1);
199 dst
[0] = byte_to_float_tex(tmp_r
);
207 src_row
+= src_stride
;
212 util_format_rgtc1_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
215 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 1);
216 dst
[0] = byte_to_float_tex(tmp_r
);
224 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
226 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
227 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
233 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
)
235 const unsigned bw
= 4, bh
= 4, comps
= 4;
237 unsigned block_size
= 16;
239 for(y
= 0; y
< height
; y
+= bh
) {
240 const uint8_t *src
= src_row
;
241 for(x
= 0; x
< width
; x
+= bw
) {
242 for(j
= 0; j
< bh
; ++j
) {
243 for(i
= 0; i
< bw
; ++i
) {
244 uint8_t *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*comps
;
245 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, dst
, 2);
246 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, dst
+ 1, 2);
253 src_row
+= src_stride
;
258 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
)
260 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
263 for(y
= 0; y
< height
; y
+= bh
) {
264 uint8_t *dst
= dst_row
;
265 for(x
= 0; x
< width
; x
+= bw
) {
266 uint8_t tmp_r
[4][4]; /* [bh][bw] */
267 uint8_t tmp_g
[4][4]; /* [bh][bw] */
268 for(j
= 0; j
< bh
; ++j
) {
269 for(i
= 0; i
< bw
; ++i
) {
270 tmp_r
[j
][i
] = src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4];
271 tmp_g
[j
][i
] = src_row
[((y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4) + 1];
274 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
275 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
276 dst
+= bytes_per_block
;
278 dst_row
+= dst_stride
/ sizeof(*dst_row
);
283 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
)
285 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
288 for(y
= 0; y
< height
; y
+= bh
) {
289 uint8_t *dst
= dst_row
;
290 for(x
= 0; x
< width
; x
+= bw
) {
291 uint8_t tmp_r
[4][4]; /* [bh][bw][comps] */
292 uint8_t tmp_g
[4][4]; /* [bh][bw][comps] */
293 for(j
= 0; j
< bh
; ++j
) {
294 for(i
= 0; i
< bw
; ++i
) {
295 tmp_r
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
296 tmp_g
[j
][i
] = float_to_ubyte(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
299 util_format_unsigned_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
300 util_format_unsigned_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
301 dst
+= bytes_per_block
;
303 dst_row
+= dst_stride
/ sizeof(*dst_row
);
308 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
)
310 util_format_rxtc2_unorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
314 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
)
318 for(y
= 0; y
< height
; y
+= 4) {
319 const uint8_t *src
= src_row
;
320 for(x
= 0; x
< width
; x
+= 4) {
321 for(j
= 0; j
< 4; ++j
) {
322 for(i
= 0; i
< 4; ++i
) {
323 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
324 uint8_t tmp_r
, tmp_g
;
325 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
326 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
327 dst
[0] = ubyte_to_float(tmp_r
);
328 dst
[1] = ubyte_to_float(tmp_g
);
335 src_row
+= src_stride
;
340 util_format_rgtc2_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
342 uint8_t tmp_r
, tmp_g
;
343 util_format_unsigned_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
344 util_format_unsigned_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
345 dst
[0] = ubyte_to_float(tmp_r
);
346 dst
[1] = ubyte_to_float(tmp_g
);
353 util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
355 fprintf(stderr
,"%s\n", __func__
);
359 util_format_rgtc2_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
361 fprintf(stderr
,"%s\n", __func__
);
365 util_format_rgtc2_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
, const uint8_t *src_row
, unsigned src_stride
, unsigned width
, unsigned height
)
367 fprintf(stderr
,"%s\n", __func__
);
371 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
)
375 for(y
= 0; y
< height
; y
+= 4) {
376 const int8_t *src
= (int8_t *)src_row
;
377 for(x
= 0; x
< width
; x
+= 4) {
378 for(j
= 0; j
< 4; ++j
) {
379 for(i
= 0; i
< 4; ++i
) {
380 float *dst
= dst_row
+ (y
+ j
)*dst_stride
/sizeof(*dst_row
) + (x
+ i
)*4;
382 util_format_signed_fetch_texel_rgtc(0, src
, i
, j
, &tmp_r
, 2);
383 util_format_signed_fetch_texel_rgtc(0, src
+ 8, i
, j
, &tmp_g
, 2);
384 dst
[0] = byte_to_float_tex(tmp_r
);
385 dst
[1] = byte_to_float_tex(tmp_g
);
392 src_row
+= src_stride
;
397 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
)
399 const unsigned bw
= 4, bh
= 4, bytes_per_block
= 16;
402 for(y
= 0; y
< height
; y
+= bh
) {
403 int8_t *dst
= (int8_t *)dst_row
;
404 for(x
= 0; x
< width
; x
+= bw
) {
405 int8_t tmp_r
[4][4]; /* [bh][bw][comps] */
406 int8_t tmp_g
[4][4]; /* [bh][bw][comps] */
407 for(j
= 0; j
< bh
; ++j
) {
408 for(i
= 0; i
< bw
; ++i
) {
409 tmp_r
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4]);
410 tmp_g
[j
][i
] = float_to_byte_tex(src_row
[(y
+ j
)*src_stride
/sizeof(*src_row
) + (x
+ i
)*4 + chan2off
]);
413 util_format_signed_encode_rgtc_ubyte(dst
, tmp_r
, 4, 4);
414 util_format_signed_encode_rgtc_ubyte(dst
+ 8, tmp_g
, 4, 4);
415 dst
+= bytes_per_block
;
417 dst_row
+= dst_stride
/ sizeof(*dst_row
);
422 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
)
424 util_format_rxtc2_snorm_pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, width
, height
, 1);
428 util_format_rgtc2_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
, unsigned i
, unsigned j
)
431 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
, i
, j
, &tmp_r
, 2);
432 util_format_signed_fetch_texel_rgtc(0, (int8_t *)src
+ 8, i
, j
, &tmp_g
, 2);
433 dst
[0] = byte_to_float_tex(tmp_r
);
434 dst
[1] = byte_to_float_tex(tmp_g
);