1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2011 LunarG, Inc. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/* 25bf215546Sopenharmony_ci * Included by texcompress_etc1 and gallium to define ETC1 decoding routines. 26bf215546Sopenharmony_ci */ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_cistruct TAG(etc1_block) { 29bf215546Sopenharmony_ci uint32_t pixel_indices; 30bf215546Sopenharmony_ci int flipped; 31bf215546Sopenharmony_ci const int *modifier_tables[2]; 32bf215546Sopenharmony_ci UINT8_TYPE base_colors[2][3]; 33bf215546Sopenharmony_ci}; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistatic UINT8_TYPE 36bf215546Sopenharmony_ciTAG(etc1_base_color_diff_hi)(UINT8_TYPE in) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci return (in & 0xf8) | (in >> 5); 39bf215546Sopenharmony_ci} 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_cistatic UINT8_TYPE 42bf215546Sopenharmony_ciTAG(etc1_base_color_diff_lo)(UINT8_TYPE in) 43bf215546Sopenharmony_ci{ 44bf215546Sopenharmony_ci static const int lookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 }; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci in = (in >> 3) + lookup[in & 0x7]; 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci return (in << 3) | (in >> 2); 49bf215546Sopenharmony_ci} 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_cistatic UINT8_TYPE 52bf215546Sopenharmony_ciTAG(etc1_base_color_ind_hi)(UINT8_TYPE in) 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci return (in & 0xf0) | ((in & 0xf0) >> 4); 55bf215546Sopenharmony_ci} 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_cistatic UINT8_TYPE 58bf215546Sopenharmony_ciTAG(etc1_base_color_ind_lo)(UINT8_TYPE in) 59bf215546Sopenharmony_ci{ 60bf215546Sopenharmony_ci return ((in & 0xf) << 4) | (in & 0xf); 61bf215546Sopenharmony_ci} 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistatic UINT8_TYPE 64bf215546Sopenharmony_ciTAG(etc1_clamp)(UINT8_TYPE base, int modifier) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci int tmp = (int) base + modifier; 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci /* CLAMP(tmp, 0, 255) */ 69bf215546Sopenharmony_ci return (UINT8_TYPE) ((tmp < 0) ? 0 : ((tmp > 255) ? 255 : tmp)); 70bf215546Sopenharmony_ci} 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_cistatic const int TAG(etc1_modifier_tables)[8][4] = { 73bf215546Sopenharmony_ci { 2, 8, -2, -8}, 74bf215546Sopenharmony_ci { 5, 17, -5, -17}, 75bf215546Sopenharmony_ci { 9, 29, -9, -29}, 76bf215546Sopenharmony_ci { 13, 42, -13, -42}, 77bf215546Sopenharmony_ci { 18, 60, -18, -60}, 78bf215546Sopenharmony_ci { 24, 80, -24, -80}, 79bf215546Sopenharmony_ci { 33, 106, -33, -106}, 80bf215546Sopenharmony_ci { 47, 183, -47, -183} 81bf215546Sopenharmony_ci}; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_cistatic void 84bf215546Sopenharmony_ciTAG(etc1_parse_block)(struct TAG(etc1_block) *block, const UINT8_TYPE *src) 85bf215546Sopenharmony_ci{ 86bf215546Sopenharmony_ci if (src[3] & 0x2) { 87bf215546Sopenharmony_ci /* differential mode */ 88bf215546Sopenharmony_ci block->base_colors[0][0] = (int) TAG(etc1_base_color_diff_hi)(src[0]); 89bf215546Sopenharmony_ci block->base_colors[1][0] = (int) TAG(etc1_base_color_diff_lo)(src[0]); 90bf215546Sopenharmony_ci block->base_colors[0][1] = (int) TAG(etc1_base_color_diff_hi)(src[1]); 91bf215546Sopenharmony_ci block->base_colors[1][1] = (int) TAG(etc1_base_color_diff_lo)(src[1]); 92bf215546Sopenharmony_ci block->base_colors[0][2] = (int) TAG(etc1_base_color_diff_hi)(src[2]); 93bf215546Sopenharmony_ci block->base_colors[1][2] = (int) TAG(etc1_base_color_diff_lo)(src[2]); 94bf215546Sopenharmony_ci } 95bf215546Sopenharmony_ci else { 96bf215546Sopenharmony_ci /* individual mode */ 97bf215546Sopenharmony_ci block->base_colors[0][0] = (int) TAG(etc1_base_color_ind_hi)(src[0]); 98bf215546Sopenharmony_ci block->base_colors[1][0] = (int) TAG(etc1_base_color_ind_lo)(src[0]); 99bf215546Sopenharmony_ci block->base_colors[0][1] = (int) TAG(etc1_base_color_ind_hi)(src[1]); 100bf215546Sopenharmony_ci block->base_colors[1][1] = (int) TAG(etc1_base_color_ind_lo)(src[1]); 101bf215546Sopenharmony_ci block->base_colors[0][2] = (int) TAG(etc1_base_color_ind_hi)(src[2]); 102bf215546Sopenharmony_ci block->base_colors[1][2] = (int) TAG(etc1_base_color_ind_lo)(src[2]); 103bf215546Sopenharmony_ci } 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci /* pick modifier tables */ 106bf215546Sopenharmony_ci block->modifier_tables[0] = TAG(etc1_modifier_tables)[(src[3] >> 5) & 0x7]; 107bf215546Sopenharmony_ci block->modifier_tables[1] = TAG(etc1_modifier_tables)[(src[3] >> 2) & 0x7]; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci block->flipped = (src[3] & 0x1); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci block->pixel_indices = 112bf215546Sopenharmony_ci (src[4] << 24) | (src[5] << 16) | (src[6] << 8) | src[7]; 113bf215546Sopenharmony_ci} 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_cistatic void 116bf215546Sopenharmony_ciTAG(etc1_fetch_texel)(const struct TAG(etc1_block) *block, 117bf215546Sopenharmony_ci int x, int y, UINT8_TYPE *dst) 118bf215546Sopenharmony_ci{ 119bf215546Sopenharmony_ci const UINT8_TYPE *base_color; 120bf215546Sopenharmony_ci int modifier, bit, idx, blk; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci /* get pixel index */ 123bf215546Sopenharmony_ci bit = y + x * 4; 124bf215546Sopenharmony_ci idx = ((block->pixel_indices >> (15 + bit)) & 0x2) | 125bf215546Sopenharmony_ci ((block->pixel_indices >> (bit)) & 0x1); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci /* get subblock */ 128bf215546Sopenharmony_ci blk = (block->flipped) ? (y >= 2) : (x >= 2); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci base_color = block->base_colors[blk]; 131bf215546Sopenharmony_ci modifier = block->modifier_tables[blk][idx]; 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci dst[0] = TAG(etc1_clamp)(base_color[0], modifier); 134bf215546Sopenharmony_ci dst[1] = TAG(etc1_clamp)(base_color[1], modifier); 135bf215546Sopenharmony_ci dst[2] = TAG(etc1_clamp)(base_color[2], modifier); 136bf215546Sopenharmony_ci} 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_cistatic void 139bf215546Sopenharmony_cietc1_unpack_rgba8888(uint8_t *dst_row, 140bf215546Sopenharmony_ci unsigned dst_stride, 141bf215546Sopenharmony_ci const uint8_t *src_row, 142bf215546Sopenharmony_ci unsigned src_stride, 143bf215546Sopenharmony_ci unsigned width, 144bf215546Sopenharmony_ci unsigned height) 145bf215546Sopenharmony_ci{ 146bf215546Sopenharmony_ci const unsigned bw = 4, bh = 4, bs = 8, comps = 4; 147bf215546Sopenharmony_ci struct etc1_block block; 148bf215546Sopenharmony_ci unsigned x, y, i, j; 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci for (y = 0; y < height; y += bh) { 151bf215546Sopenharmony_ci const uint8_t *src = src_row; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci for (x = 0; x < width; x+= bw) { 154bf215546Sopenharmony_ci etc1_parse_block(&block, src); 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci for (j = 0; j < MIN2(bh, height - y); j++) { 157bf215546Sopenharmony_ci uint8_t *dst = dst_row + (y + j) * dst_stride + x * comps; 158bf215546Sopenharmony_ci for (i = 0; i < MIN2(bw, width - x); i++) { 159bf215546Sopenharmony_ci etc1_fetch_texel(&block, i, j, dst); 160bf215546Sopenharmony_ci dst[3] = 255; 161bf215546Sopenharmony_ci dst += comps; 162bf215546Sopenharmony_ci } 163bf215546Sopenharmony_ci } 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci src += bs; 166bf215546Sopenharmony_ci } 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci src_row += src_stride; 169bf215546Sopenharmony_ci } 170bf215546Sopenharmony_ci} 171