1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/*
* Copyright (C) 2011 Rudolf Polzer All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* RUDOLF POLZER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define S2TC_LICENSE_IDENTIFIER s2tc_libtxc_dxtn_license
#include "s2tc_license.h"
extern "C"
{
#include "txc_dxtn.h"
};
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "s2tc_algorithm.h"
#include "s2tc_common.h"
void fetch_2d_texel_rgb_dxt1(GLint srcRowStride, const GLubyte *pixdata,
GLint i, GLint j, GLvoid *texel)
{
// fetches a single texel (i,j) into pixdata (RGB)
GLubyte *t = (GLubyte *) texel;
const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
unsigned int c = blksrc[0] + 256*blksrc[1];
unsigned int c1 = blksrc[2] + 256*blksrc[3];
int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
case 1: c = c1; break;
case 3: if(c1 >= c) { c = 0; break; }
default: if((i^j) & 1) c = c1; break;
}
t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
t[1] = ((c >> 5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
t[2] = ((c ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
t[3] = 255;
}
void fetch_2d_texel_rgba_dxt1(GLint srcRowStride, const GLubyte *pixdata,
GLint i, GLint j, GLvoid *texel)
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
unsigned int c = blksrc[0] + 256*blksrc[1];
unsigned int c1 = blksrc[2] + 256*blksrc[3];
int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: t[3] = 255; break;
case 1: c = c1; t[3] = 255; break;
case 3: if(c1 >= c) { c = 0; t[3] = 0; break; }
default: if((i^j) & 1) c = c1; t[3] = 255; break;
}
t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
t[1] = ((c >> 5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
t[2] = ((c ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
}
void fetch_2d_texel_rgba_dxt3(GLint srcRowStride, const GLubyte *pixdata,
GLint i, GLint j, GLvoid *texel)
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
unsigned int c = blksrc[8] + 256*blksrc[9];
unsigned int c1 = blksrc[10] + 256*blksrc[11];
int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
case 1: c = c1; break;
default: if((i^j) & 1) c = c1; break;
}
t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
t[1] = ((c >> 5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
t[2] = ((c ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
int a = (blksrc[(j & 3) * 2 + ((i & 3) >> 1)] >> (4 * (i & 1))) & 0x0F;
t[3] = a | (a << 4);
}
void fetch_2d_texel_rgba_dxt5(GLint srcRowStride, const GLubyte *pixdata,
GLint i, GLint j, GLvoid *texel)
{
// fetches a single texel (i,j) into pixdata (RGBA)
GLubyte *t = (GLubyte *) texel;
const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
unsigned int c = blksrc[8] + 256*blksrc[9];
unsigned int c1 = blksrc[10] + 256*blksrc[11];
int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
switch(b)
{
case 0: break;
case 1: c = c1; break;
default: if((i^j) & 1) c = c1; break;
}
t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
t[1] = ((c >> 5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
t[2] = ((c ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
unsigned int a = blksrc[0];
unsigned int a1 = blksrc[1];
int abit = ((j & 3) * 4 + (i & 3)) * 3;
int ab = 0;
if(testbit(&blksrc[2], abit))
ab |= 1;
++abit;
if(testbit(&blksrc[2], abit))
ab |= 2;
++abit;
if(testbit(&blksrc[2], abit))
ab |= 4;
switch(ab)
{
case 0: break;
case 1: a = a1; break;
case 6: if(a1 >= a) { a = 0; break; }
case 7: if(a1 >= a) { a = 255; break; }
default: if((i^j) & 1) a = a1; break;
}
t[3] = a;
}
void tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
const GLubyte *srcPixData, GLenum destformat,
GLubyte *dest, GLint dstRowStride)
{
// compresses width*height pixels (RGB or RGBA depending on srccomps) at srcPixData (packed) to destformat (dest, dstRowStride)
GLubyte *blkaddr = dest;
GLint numxpixels, numypixels;
GLint i, j;
GLint dstRowDiff;
unsigned char *rgba = (unsigned char *) malloc(width * height * 4);
unsigned char *srcaddr;
DxtMode dxt;
ColorDistMode cd = WAVG;
int nrandom = -1;
RefinementMode refine = REFINE_ALWAYS;
DitherMode dither = DITHER_SIMPLE;
{
const char *v = getenv("S2TC_DITHER_MODE");
if(v)
{
if(!strcasecmp(v, "NONE"))
dither = DITHER_NONE;
else if(!strcasecmp(v, "SIMPLE"))
dither = DITHER_SIMPLE;
else if(!strcasecmp(v, "FLOYDSTEINBERG"))
dither = DITHER_FLOYDSTEINBERG;
else
fprintf(stderr, "Invalid dither mode: %s\n", v);
}
}
{
const char *v = getenv("S2TC_COLORDIST_MODE");
if(v)
{
if(!strcasecmp(v, "RGB"))
cd = RGB;
else if(!strcasecmp(v, "YUV"))
cd = YUV;
else if(!strcasecmp(v, "SRGB"))
cd = SRGB;
else if(!strcasecmp(v, "SRGB_MIXED"))
cd = SRGB_MIXED;
else if(!strcasecmp(v, "AVG"))
cd = AVG;
else if(!strcasecmp(v, "WAVG"))
cd = WAVG;
else if(!strcasecmp(v, "W0AVG"))
cd = W0AVG;
else if(!strcasecmp(v, "NORMALMAP"))
cd = NORMALMAP;
else
fprintf(stderr, "Invalid color dist mode: %s\n", v);
}
}
{
const char *v = getenv("S2TC_RANDOM_COLORS");
if(v)
nrandom = atoi(v);
}
{
const char *v = getenv("S2TC_REFINE_COLORS");
if(v)
{
if(!strcasecmp(v, "NEVER"))
refine = REFINE_NEVER;
else if(!strcasecmp(v, "ALWAYS"))
refine = REFINE_ALWAYS;
else if(!strcasecmp(v, "LOOP"))
refine = REFINE_LOOP;
else
fprintf(stderr, "Invalid refinement mode: %s\n", v);
}
}
switch (destformat) {
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
dxt = DXT1;
rgb565_image(rgba, srcPixData, width, height, srccomps, 1, dither);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
dxt = DXT3;
rgb565_image(rgba, srcPixData, width, height, srccomps, 4, dither);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
dxt = DXT5;
rgb565_image(rgba, srcPixData, width, height, srccomps, 8, dither);
break;
default:
free(rgba);
fprintf(stderr, "libdxtn: Bad dstFormat %d in tx_compress_dxtn\n", destformat);
return;
}
s2tc_encode_block_func_t encode_block = s2tc_encode_block_func(dxt, cd, nrandom, refine);
switch (destformat) {
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
/* hmm we used to get called without dstRowStride... */
dstRowDiff = dstRowStride >= (width * 2) ? dstRowStride - (((width + 3) & ~3) * 2) : 0;
/* fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */
for (j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4;
else numypixels = height - j;
srcaddr = rgba + j * width * 4;
for (i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4;
else numxpixels = width - i;
encode_block(blkaddr, srcaddr, width, numxpixels, numypixels, nrandom);
srcaddr += 4 * numxpixels;
blkaddr += 8;
}
blkaddr += dstRowDiff;
}
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
/* fprintf(stderr, "dxt3 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */
for (j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4;
else numypixels = height - j;
srcaddr = rgba + j * width * 4;
for (i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4;
else numxpixels = width - i;
encode_block(blkaddr, srcaddr, width, numxpixels, numypixels, nrandom);
srcaddr += 4 * numxpixels;
blkaddr += 16;
}
blkaddr += dstRowDiff;
}
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
/* fprintf(stderr, "dxt5 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */
for (j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4;
else numypixels = height - j;
srcaddr = rgba + j * width * 4;
for (i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4;
else numxpixels = width - i;
encode_block(blkaddr, srcaddr, width, numxpixels, numypixels, nrandom);
srcaddr += 4 * numxpixels;
blkaddr += 16;
}
blkaddr += dstRowDiff;
}
break;
}
free(rgba);
}
|