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 300
|
/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
** FULL TEXT OF THE NON-WARRANTY PROVISIONS.
**
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
** THE UNITED STATES.
**
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
**
** $Revision: 1.2 $
** $Date: 2000/06/15 00:11:40 $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "texusint.h"
// Error diffusion is implemented here.
static int ErrR[MAX_TEXWIDTH], ErrG[MAX_TEXWIDTH], ErrB[MAX_TEXWIDTH];
#if FAST_DIFFUSION
int nsorted;
FxU32 sortR[256], sortG[256], sortB[256];
FxU8 bestR[256], bestG[256], bestB[256];
static int
_txAscendingR(const void *a, const void *b)
{
return ((*(FxI32 *)a) & 0x00FF0000) - ((*(FxI32 *)b) & 0x00FF0000);
}
static int
_txAscendingG(const void *a, const void *b)
{
return ((*(FxI32 *)a) & 0x0000ff00) - ((*(FxI32 *)b) & 0x0000ff00);
}
static int
_txAscendingB(const void *a, const void *b)
{
return ((*(FxI32 *)a) & 0x000000ff) - ((*(FxI32 *)b) & 0x000000ff);
}
static void
_txMakeRange(const FxU32 *palette, int ncolors)
{
int i, j, mindist, minpos, d;
FxU32 *pal;
for (i=0; i<ncolors; i++) {
sortR[i] = (palette[i] & 0x00ffffff) | (i<<24);
sortG[i] = (palette[i] & 0x00ffffff) | (i<<24);
sortB[i] = (palette[i] & 0x00ffffff) | (i<<24);
}
qsort(sortR, ncolors, sizeof(FxU32), _txAscendingR);
qsort(sortG, ncolors, sizeof(FxU32), _txAscendingG);
qsort(sortB, ncolors, sizeof(FxU32), _txAscendingB);
nsorted = ncolors;
#if 0
for (i=0; i<ncolors; i++)
printf("[%3d] = R%.08x G%.08x B%.08x\n",
i, sortR[i], sortG[i], sortB[i]);
#endif
for (i=0; i<256; i++) {
/* Find index of best matching Red component given r=i */
pal = sortR;
minpos = mindist = 0x7fffffff;
for (j=0; j < ncolors; j++) {
d = DISTANCE(i, 0, 0, (pal[j]>>16) & 0xff, 0, 0);
if (d < mindist) { mindist = d; minpos = j; }
}
bestR[i] = minpos;
/* Find index of best matching Grn component given g=i */
pal = sortG;
minpos = mindist = 0x7fffffff;
for (j=0; j < ncolors; j++) {
d = DISTANCE(0, i, 0, 0, (pal[j]>>8) & 0xff, 0);
if (d < mindist) { mindist = d; minpos = j; }
}
bestG[i] = minpos;
/* Find index of best matching Blu component given b=i */
pal = sortB;
minpos = mindist = 0x7fffffff;
for (j=0; j < ncolors; j++) {
d = DISTANCE(0, 0, i, 0, 0, (pal[j]) & 0xff);
if (d < mindist) { mindist = d; minpos = j; }
}
bestB[i] = minpos;
}
}
static int
_txFastMatch(int r, int g, int b)
{
int minpos, mindist, i, d, persist;
FxU32 *pal;
minpos = mindist = 0x7fffffff;
/* Walk backwards from bestR, tracking best index */
pal = sortR;
persist = 0;
for (i=bestR[r]; i>=0; i--) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i] >> 24) & 0xff; }
else if (++persist > 3) break;
}
/* Walk forwards from bestR+1, tracking best index */
persist = 0;
for (i=bestR[r]+1; i < nsorted; i++) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i] >> 24) & 0xff; }
else if (++persist > 3) break;
}
/* Walk backwards from bestG, tracking best index */
pal = sortG;
persist = 0;
for (i=bestG[g]; i>=0; i--) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i]>>24) & 0xff; }
else if (++persist > 3) break;
}
/* Walk forwards from bestG+1, tracking best index */
persist = 0;
for (i=bestG[g]+1; i < nsorted; i++) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i]>>24) & 0xff; }
else if (++persist > 3) break;
}
/* Walk backwards from bestB, tracking best index */
pal = sortB;
persist = 0;
for (i=bestB[b]; i>=0; i--) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i]>>24) & 0xff; }
else if (++persist > 3) break;
}
/* Walk forwards from bestB+1, tracking best index */
persist = 0;
for (i=bestB[b]+1; i < nsorted; i++) {
d = DISTANCE(r, g, b,
((pal[i] >> 16) & 0xff), ((pal[i] >> 8) & 0xff), ((pal[i]) & 0xff));
if (d < mindist) { mindist = d; minpos = (pal[i]>>24) & 0xff; }
else if (++persist > 3) break;
}
return minpos;
}
#endif // FAST_DIFFUSION
static void
_txToDiffuseIndex (FxU8 *opixels, int pixsize, const FxU32 *pal, int ncolors,
const FxU32 *ipixels, int width, int height)
{
int y, x, i;
int qr, qg, qb;
for (y=0; y<height; y++) {
if( txVerbose )
{
if (y == (3*height)/4) { printf("."); fflush(stdout);}
if (y == (2*height)/4) { printf("."); fflush(stdout);}
if (y == (1*height)/4) { printf("."); fflush(stdout);}
if (y == (0*height)/4) { printf("."); fflush(stdout);}
}
qr = qg = qb = 0;
for (i=0; i<=width; i++) ErrR[i] = ErrG[i] = ErrB[i] = 0;
for (x=0; x<width; x++) {
int ia, ir, ig, ib, c;
// The input color, and add diffused errors to these.
ia = (*ipixels >> 24) & 0xFF;
ir = (*ipixels >> 16) & 0xFF;
ig = (*ipixels >> 8) & 0xFF;
ib = (*ipixels ) & 0xFF;
ipixels ++;
ir += qr + ErrR[x];
ig += qg + ErrG[x];
ib += qb + ErrB[x];
qr = ir; // quantized pixel values.
qg = ig; // qR is error from pixel to left, errR is
qb = ib; // error from pixel to the top & top left.
if (qr < 0) qr = 0; if (qr > 255) qr = 255; // clamp.
if (qg < 0) qg = 0; if (qg > 255) qg = 255;
if (qb < 0) qb = 0; if (qb > 255) qb = 255;
// Find closest color in the tables, and quantize.
c = txNearestColor(qr, qg, qb, pal, ncolors);
#if FAST_DIFFUSION
// 3 times faster, but not as good quality.
c = _txFastMatch(qr, qg, qb);
#endif
qr = (pal[c] & 0x00ff0000) >> 16;
qg = (pal[c] & 0x0000ff00) >> 8;
qb = (pal[c] & 0x000000ff) ;
// Diffuse the errors.
qr = ir - qr;
qg = ig - qg;
qb = ib - qb;
// 3/8 (=0.375) to the EAST, 3/8 to the SOUTH,
// 1/4 (0.25) to the SOUTH-EAST.
ErrR[x] = ((x == 0) ? 0 : ErrR[x]) + ((int) (qr * 0.375f));
ErrG[x] = ((x == 0) ? 0 : ErrG[x]) + ((int) (qg * 0.375f));
ErrB[x] = ((x == 0) ? 0 : ErrB[x]) + ((int) (qb * 0.375f));
ErrR[x+1] = (int) (qr * 0.250f);
ErrG[x+1] = (int) (qg * 0.250f);
ErrB[x+1] = (int) (qb * 0.250f);
qr = (int) (qr * 0.375f); // Carried to the right.
qg = (int) (qg * 0.375f);
qb = (int) (qb * 0.375f);
if (pixsize == 2) {
*(FxU16 *) opixels = (ia << 8) | c;
opixels += pixsize;
} else {
*opixels++ = (FxU8) c;
}
}
}
}
void
txDiffuseIndex(TxMip *pxMip, TxMip *txMip, int pixsize, const FxU32 *palette,
int ncolors)
{
int i, w, h;
if( txVerbose )
{
printf("EDiffusion:..."); fflush(stdout);
}
#if FAST_DIFFUSION
_txMakeRange(palette, ncolors);
#endif
/* Translate image to an indexed image using error diffusion */
w = txMip->width;
h = txMip->height;
for (i=0; i<txMip->depth; i++) {
_txToDiffuseIndex(pxMip->data[i], pixsize, palette, ncolors,
txMip->data[i], w, h);
if (w > 1) w >>= 1;
if (h > 1) h >>= 1;
}
if( txVerbose )
{
printf("done\n");
}
}
|