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_math.h" 27 #include "u_format.h" 28 #include "u_format_rgtc.h" 29 #include "util/rgtc.h" 30 31 void 32 util_format_rgtc1_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 33 { 34 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, dst, 1); 35 dst[1] = 0; 36 dst[2] = 0; 37 dst[3] = 255; 38 } 39 40 void 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) 42 { 43 const unsigned bw = 4, bh = 4, comps = 4; 44 unsigned x, y, i, j; 45 unsigned block_size = 8; 46 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); 54 dst[1] = 0; 55 dst[2] = 0; 56 dst[3] = 255; 57 } 58 } 59 src += block_size; 60 } 61 src_row += src_stride; 62 } 63 } 64 65 void 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) 68 { 69 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 70 unsigned x, y, i, j; 71 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]; 79 } 80 } 81 util_format_unsigned_encode_rgtc_ubyte(dst, tmp, 4, 4); 82 dst += bytes_per_block; 83 } 84 dst_row += dst_stride / sizeof(*dst_row); 85 } 86 } 87 88 void 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) 90 { 91 unsigned x, y, i, j; 92 int block_size = 8; 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; 99 uint8_t tmp_r; 100 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 101 dst[0] = ubyte_to_float(tmp_r); 102 dst[1] = 0.0; 103 dst[2] = 0.0; 104 dst[3] = 1.0; 105 } 106 } 107 src += block_size; 108 } 109 src_row += src_stride; 110 } 111 } 112 113 void 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) 115 { 116 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 117 unsigned x, y, i, j; 118 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]); 126 } 127 } 128 util_format_unsigned_encode_rgtc_ubyte(dst, tmp, 4, 4); 129 dst += bytes_per_block; 130 } 131 dst_row += dst_stride / sizeof(*dst_row); 132 } 133 } 134 135 void 136 util_format_rgtc1_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 137 { 138 uint8_t tmp_r; 139 util_format_unsigned_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 140 dst[0] = ubyte_to_float(tmp_r); 141 dst[1] = 0.0; 142 dst[2] = 0.0; 143 dst[3] = 1.0; 144 } 145 146 void 147 util_format_rgtc1_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 148 { 149 fprintf(stderr,"%s\n", __func__); 150 } 151 152 void 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) 154 { 155 fprintf(stderr,"%s\n", __func__); 156 } 157 158 void 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) 160 { 161 fprintf(stderr,"%s\n", __func__); 162 } 163 164 void 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) 166 { 167 const unsigned bw = 4, bh = 4, bytes_per_block = 8; 168 unsigned x, y, i, j; 169 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]); 177 } 178 } 179 util_format_signed_encode_rgtc_ubyte(dst, tmp, 4, 4); 180 dst += bytes_per_block; 181 } 182 dst_row += dst_stride / sizeof(*dst_row); 183 } 184 } 185 186 void 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) 188 { 189 unsigned x, y, i, j; 190 int block_size = 8; 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; 197 int8_t tmp_r; 198 util_format_signed_fetch_texel_rgtc(0, src, i, j, &tmp_r, 1); 199 dst[0] = byte_to_float_tex(tmp_r); 200 dst[1] = 0.0; 201 dst[2] = 0.0; 202 dst[3] = 1.0; 203 } 204 } 205 src += block_size; 206 } 207 src_row += src_stride; 208 } 209 } 210 211 void 212 util_format_rgtc1_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 213 { 214 int8_t tmp_r; 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); 217 dst[1] = 0.0; 218 dst[2] = 0.0; 219 dst[3] = 1.0; 220 } 221 222 223 void 224 util_format_rgtc2_unorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 225 { 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); 228 dst[2] = 0; 229 dst[3] = 255; 230 } 231 232 void 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) 234 { 235 const unsigned bw = 4, bh = 4, comps = 4; 236 unsigned x, y, i, j; 237 unsigned block_size = 16; 238 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); 247 dst[2] = 0; 248 dst[3] = 255; 249 } 250 } 251 src += block_size; 252 } 253 src_row += src_stride; 254 } 255 } 256 257 void 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) 259 { 260 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 261 unsigned x, y, i, j; 262 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]; 272 } 273 } 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; 277 } 278 dst_row += dst_stride / sizeof(*dst_row); 279 } 280 } 281 282 void 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) 284 { 285 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 286 unsigned x, y, i, j; 287 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]); 297 } 298 } 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; 302 } 303 dst_row += dst_stride / sizeof(*dst_row); 304 } 305 } 306 307 void 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) 309 { 310 util_format_rxtc2_unorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 1); 311 } 312 313 void 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) 315 { 316 unsigned x, y, i, j; 317 int block_size = 16; 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); 329 dst[2] = 0.0; 330 dst[3] = 1.0; 331 } 332 } 333 src += block_size; 334 } 335 src_row += src_stride; 336 } 337 } 338 339 void 340 util_format_rgtc2_unorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 341 { 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); 347 dst[2] = 0.0; 348 dst[3] = 1.0; 349 } 350 351 352 void 353 util_format_rgtc2_snorm_fetch_rgba_8unorm(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j) 354 { 355 fprintf(stderr,"%s\n", __func__); 356 } 357 358 void 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) 360 { 361 fprintf(stderr,"%s\n", __func__); 362 } 363 364 void 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) 366 { 367 fprintf(stderr,"%s\n", __func__); 368 } 369 370 void 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) 372 { 373 unsigned x, y, i, j; 374 int block_size = 16; 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; 381 int8_t tmp_r, tmp_g; 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); 386 dst[2] = 0.0; 387 dst[3] = 1.0; 388 } 389 } 390 src += block_size; 391 } 392 src_row += src_stride; 393 } 394 } 395 396 void 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) 398 { 399 const unsigned bw = 4, bh = 4, bytes_per_block = 16; 400 unsigned x, y, i, j; 401 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]); 411 } 412 } 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; 416 } 417 dst_row += dst_stride / sizeof(*dst_row); 418 } 419 } 420 421 void 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) 423 { 424 util_format_rxtc2_snorm_pack_rgba_float(dst_row, dst_stride, src_row, src_stride, width, height, 1); 425 } 426 427 void 428 util_format_rgtc2_snorm_fetch_rgba_float(float *dst, const uint8_t *src, unsigned i, unsigned j) 429 { 430 int8_t tmp_r, tmp_g; 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); 435 dst[2] = 0.0; 436 dst[3] = 1.0; 437 } 438 439