6 #include "s2tc_compressor.h"
7 #include "s2tc_common.h"
17 inline bool operator<(const color_t &a, const color_t &b)
29 // 16 differences must fit in int
30 // i.e. a difference must be lower than 2^27
32 // shift right, rounded
33 #define SHRR(a,n) (((a) + (1 << ((n)-1))) >> (n))
35 inline int color_dist_avg(const color_t &a, const color_t &b)
37 int dr = a.r - b.r; // multiplier: 31 (-1..1)
38 int dg = a.g - b.g; // multiplier: 63 (-1..1)
39 int db = a.b - b.b; // multiplier: 31 (-1..1)
40 return ((dr*dr) << 2) + dg*dg + ((db*db) << 2);
43 inline int color_dist_wavg(const color_t &a, const color_t &b)
45 int dr = a.r - b.r; // multiplier: 31 (-1..1)
46 int dg = a.g - b.g; // multiplier: 63 (-1..1)
47 int db = a.b - b.b; // multiplier: 31 (-1..1)
48 return ((dr*dr) << 2) + ((dg*dg) << 2) + (db*db);
52 inline int color_dist_yuv(const color_t &a, const color_t &b)
54 int dr = a.r - b.r; // multiplier: 31 (-1..1)
55 int dg = a.g - b.g; // multiplier: 63 (-1..1)
56 int db = a.b - b.b; // multiplier: 31 (-1..1)
57 int y = dr * 30*2 + dg * 59 + db * 11*2; // multiplier: 6259
58 int u = dr * 202 - y; // * 0.5 / (1 - 0.30)
59 int v = db * 202 - y; // * 0.5 / (1 - 0.11)
60 return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
61 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
62 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
65 inline int color_dist_rgb(const color_t &a, const color_t &b)
67 int dr = a.r - b.r; // multiplier: 31 (-1..1)
68 int dg = a.g - b.g; // multiplier: 63 (-1..1)
69 int db = a.b - b.b; // multiplier: 31 (-1..1)
70 int y = dr * 21*2 + dg * 72 + db * 7*2; // multiplier: 6272
71 int u = dr * 202 - y; // * 0.5 / (1 - 0.21)
72 int v = db * 202 - y; // * 0.5 / (1 - 0.07)
73 return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
74 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.21)) = 0.395
75 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.07)) = 0.328
78 inline int color_dist_srgb(const color_t &a, const color_t &b)
80 int dr = a.r * (int) a.r - b.r * (int) b.r; // multiplier: 31*31
81 int dg = a.g * (int) a.g - b.g * (int) b.g; // multiplier: 63*63
82 int db = a.b * (int) a.b - b.b * (int) b.b; // multiplier: 31*31
83 int y = dr * 21*2*2 + dg * 72 + db * 7*2*2; // multiplier: 393400
84 int u = dr * 409 - y; // * 0.5 / (1 - 0.30)
85 int v = db * 409 - y; // * 0.5 / (1 - 0.11)
86 int sy = SHRR(y, 3) * SHRR(y, 4);
87 int su = SHRR(u, 3) * SHRR(u, 4);
88 int sv = SHRR(v, 3) * SHRR(v, 4);
89 return SHRR(sy, 4) + SHRR(su, 8) + SHRR(sv, 9);
90 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
91 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
94 inline int srgb_get_y(const color_t &a)
97 int r = a.r * (int) a.r;
98 int g = a.g * (int) a.g;
99 int b = a.b * (int) a.b;
101 int y = 37 * (r * 21*2*2 + g * 72 + b * 7*2*2); // multiplier: 14555800
102 // square root it (!)
103 y = sqrtf(y) + 0.5f; // now in range 0 to 3815
107 inline int color_dist_srgb_mixed(const color_t &a, const color_t &b)
110 int ay = srgb_get_y(a);
111 int by = srgb_get_y(b);
113 int au = a.r * 191 - ay;
114 int av = a.b * 191 - ay;
115 int bu = b.r * 191 - by;
116 int bv = b.b * 191 - by;
121 return ((y*y) << 3) + SHRR(u*u, 1) + SHRR(v*v, 2);
126 // FIXME this is likely broken
127 inline int color_dist_lab_srgb(const color_t &a, const color_t &b)
130 float ar = powf(a.r / 31.0f, 2.4f);
131 float ag = powf(a.g / 63.0f, 2.4f);
132 float ab = powf(a.b / 31.0f, 2.4f);
133 float br = powf(b.r / 31.0f, 2.4f);
134 float bg = powf(b.g / 63.0f, 2.4f);
135 float bb = powf(b.b / 31.0f, 2.4f);
136 // convert to CIE XYZ
137 float aX = 0.4124f * ar + 0.3576f * ag + 0.1805f * ab;
138 float aY = 0.2126f * ar + 0.7152f * ag + 0.0722f * ab;
139 float aZ = 0.0193f * ar + 0.1192f * ag + 0.9505f * ab;
140 float bX = 0.4124f * br + 0.3576f * bg + 0.1805f * bb;
141 float bY = 0.2126f * br + 0.7152f * bg + 0.0722f * bb;
142 float bZ = 0.0193f * br + 0.1192f * bg + 0.9505f * bb;
143 // convert to CIE Lab
147 float aL = 116 * cbrtf(aY / Yn) - 16;
148 float aA = 500 * (cbrtf(aX / Xn) - cbrtf(aY / Yn));
149 float aB = 200 * (cbrtf(aY / Yn) - cbrtf(aZ / Zn));
150 float bL = 116 * cbrtf(bY / Yn) - 16;
151 float bA = 500 * (cbrtf(bX / Xn) - cbrtf(bY / Yn));
152 float bB = 200 * (cbrtf(bY / Yn) - cbrtf(bZ / Zn));
153 // euclidean distance, but moving weight away from A and B
154 return 1000 * ((aL - bL) * (aL - bL) + (aA - bA) * (aA - bA) + (aB - bB) * (aB - bB));
157 inline int color_dist_normalmap(const color_t &a, const color_t &b)
159 float ca[3], cb[3], n;
160 ca[0] = a.r / 31.0f * 2 - 1;
161 ca[1] = a.g / 63.0f * 2 - 1;
162 ca[2] = a.b / 31.0f * 2 - 1;
163 cb[0] = b.r / 31.0f * 2 - 1;
164 cb[1] = b.g / 63.0f * 2 - 1;
165 cb[2] = b.b / 31.0f * 2 - 1;
166 n = sqrt(ca[0] * ca[0] + ca[1] * ca[1] + ca[2] * ca[2]);
174 n = sqrt(cb[0] * cb[0] + cb[1] * cb[1] + cb[2] * cb[2]);
186 (cb[0] - ca[0]) * (cb[0] - ca[0])
188 (cb[1] - ca[1]) * (cb[1] - ca[1])
190 (cb[2] - ca[2]) * (cb[2] - ca[2])
193 // max value: 1000 * (4 + 4 + 4) = 6000
196 typedef int ColorDistFunc(const color_t &a, const color_t &b);
198 inline int alpha_dist(unsigned char a, unsigned char b)
200 return (a - (int) b) * (a - (int) b);
203 template <class T, class F>
205 // m: total color count (including non-counted inputs)
207 inline void reduce_colors_inplace(T *c, int n, int m, F dist)
215 for(i = 0; i < n; ++i)
218 for(j = i+1; j < n; ++j)
220 int d = dist(c[i], c[j]);
221 dists[i][j] = dists[j][i] = d;
227 for(j = 0; j < n; ++j)
229 int d = dist(c[i], c[j]);
233 for(i = 0; i < m; ++i)
234 for(j = i+1; j < m; ++j)
237 for(k = 0; k < n; ++k)
239 int di = dists[i][k];
240 int dj = dists[j][k];
244 if(bestsum < 0 || sum < bestsum)
256 template <class T, class F>
257 inline void reduce_colors_inplace_2fixpoints(T *c, int n, int m, F dist, const T &fix0, const T &fix1)
265 for(i = 0; i < n; ++i)
268 for(j = i+1; j < n; ++j)
270 int d = dist(c[i], c[j]);
271 dists[i][j] = dists[j][i] = d;
277 for(j = 0; j < n; ++j)
279 int d = dist(c[i], c[j]);
283 // then the two extra rows
284 for(j = 0; j < n; ++j)
286 int d = dist(fix0, c[j]);
289 for(j = 0; j < n; ++j)
291 int d = dist(fix1, c[j]);
294 for(i = 0; i < m; ++i)
295 for(j = i+1; j < m; ++j)
298 for(k = 0; k < n; ++k)
300 int di = dists[i][k];
301 int dj = dists[j][k];
302 int d0 = dists[m][k];
303 int d1 = dists[m+1][k];
304 int m = min(min(di, dj), min(d0, d1));
307 if(bestsum < 0 || sum < bestsum)
327 template<ColorDistFunc ColorDist> inline int refine_component_encode(int comp)
331 template<> inline int refine_component_encode<color_dist_srgb>(int comp)
335 template<> inline int refine_component_encode<color_dist_srgb_mixed>(int comp)
339 template<> inline int refine_component_encode<color_dist_lab_srgb>(int comp)
344 template<ColorDistFunc ColorDist> inline int refine_component_decode(int comp)
348 template<> inline int refine_component_decode<color_dist_srgb>(int comp)
350 return sqrtf(comp) + 0.5f;
352 template<> inline int refine_component_decode<color_dist_srgb_mixed>(int comp)
354 return sqrtf(comp) + 0.5f;
356 template<> inline int refine_component_decode<color_dist_lab_srgb>(int comp)
358 return sqrtf(comp) + 0.5f;
361 template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode, RefinementMode refine>
362 inline void s2tc_encode_block(unsigned char *out, const unsigned char *rgba, int iw, int w, int h, int nrandom)
364 color_t c[16 + (mode == MODE_RANDOM ? nrandom : 0)];
365 unsigned char ca[16 + (mode == MODE_RANDOM ? nrandom : 0)];
369 if(mode == MODE_FAST)
371 // FAST: trick from libtxc_dxtn: just get brightest and darkest colors, and encode using these
373 color_t c0 = {0, 0, 0};
379 int dmin = ColorDist(c[0], c0);
387 for(x = 0; x < w; ++x)
388 for(y = !x; y < h; ++y)
390 c[2].r = rgba[(x + y * iw) * 4 + 2];
391 c[2].g = rgba[(x + y * iw) * 4 + 1];
392 c[2].b = rgba[(x + y * iw) * 4 + 0];
394 int d = ColorDist(c[2], c0);
408 ca[2] = rgba[(x + y * iw) * 4 + 3];
420 for(x = 0; x < w; ++x)
421 for(y = 0; y < h; ++y)
423 c[n].r = rgba[(x + y * iw) * 4 + 2];
424 c[n].g = rgba[(x + y * iw) * 4 + 1];
425 c[n].b = rgba[(x + y * iw) * 4 + 0];
427 ca[n] = rgba[(x + y * iw) * 4 + 3];
432 if(mode == MODE_RANDOM)
436 unsigned char mina = (dxt == DXT5) ? ca[0] : 0;
437 unsigned char maxa = (dxt == DXT5) ? ca[0] : 0;
438 for(x = 1; x < n; ++x)
440 mins.r = min(mins.r, c[x].r);
441 mins.g = min(mins.g, c[x].g);
442 mins.b = min(mins.b, c[x].b);
443 maxs.r = max(maxs.r, c[x].r);
444 maxs.g = max(maxs.g, c[x].g);
445 maxs.b = max(maxs.b, c[x].b);
448 mina = min(mina, ca[x]);
449 maxa = max(maxa, ca[x]);
452 color_t len = { maxs.r - mins.r + 1, maxs.g - mins.g + 1, maxs.b - mins.b + 1 };
453 int lena = (dxt == DXT5) ? (maxa - (int) mina + 1) : 0;
454 for(x = 0; x < nrandom; ++x)
456 c[m].r = mins.r + rand() % len.r;
457 c[m].g = mins.g + rand() % len.g;
458 c[m].b = mins.b + rand() % len.b;
460 ca[m] = mina + rand() % lena;
466 // hack for last miplevel
474 reduce_colors_inplace(c, n, m, ColorDist);
476 reduce_colors_inplace_2fixpoints(ca, n, m, alpha_dist, (unsigned char) 0, (unsigned char) 255);
479 if(refine == REFINE_NEVER)
501 int nc0 = 0, na0 = 0, sc0r = 0, sc0g = 0, sc0b = 0, sa0 = 0;
502 int nc1 = 0, na1 = 0, sc1r = 0, sc1g = 0, sc1b = 0, sa1 = 0;
503 if(refine == REFINE_LOOP)
506 memset(out, 0, (dxt == DXT1) ? 8 : 16);
507 for(x = 0; x < w; ++x)
508 for(y = 0; y < h; ++y)
510 int pindex = (x+y*4);
511 c[2].r = rgba[(x + y * iw) * 4 + 2];
512 c[2].g = rgba[(x + y * iw) * 4 + 1];
513 c[2].b = rgba[(x + y * iw) * 4 + 0];
514 ca[2] = rgba[(x + y * iw) * 4 + 3];
520 int bitindex = pindex * 3;
521 da[0] = alpha_dist(ca[0], ca[2]);
522 da[1] = alpha_dist(ca[1], ca[2]);
523 da[2] = alpha_dist(0, ca[2]);
524 da[3] = alpha_dist(255, ca[2]);
525 if(da[2] <= da[0] && da[2] <= da[1] && da[2] <= da[3])
529 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
531 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
533 else if(da[3] <= da[0] && da[3] <= da[1])
536 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
538 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
540 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
542 else if(da[0] <= da[1])
545 if(refine != REFINE_NEVER)
554 out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
555 if(refine != REFINE_NEVER)
562 if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
564 int bitindex = pindex * 2;
565 out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
566 if(refine != REFINE_NEVER)
569 sc1r += refine_component_encode<ColorDist>(c[2].r);
570 sc1g += refine_component_encode<ColorDist>(c[2].g);
571 sc1b += refine_component_encode<ColorDist>(c[2].b);
576 if(refine != REFINE_NEVER)
579 sc0r += refine_component_encode<ColorDist>(c[2].r);
580 sc0g += refine_component_encode<ColorDist>(c[2].g);
581 sc0b += refine_component_encode<ColorDist>(c[2].b);
587 int bitindex = pindex * 4;
588 out[bitindex / 8 + 0] |= (ca[2] << (bitindex % 8));
590 if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
592 int bitindex = pindex * 2;
593 out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
594 if(refine != REFINE_NEVER)
597 sc1r += refine_component_encode<ColorDist>(c[2].r);
598 sc1g += refine_component_encode<ColorDist>(c[2].g);
599 sc1b += refine_component_encode<ColorDist>(c[2].b);
604 if(refine != REFINE_NEVER)
607 sc0r += refine_component_encode<ColorDist>(c[2].r);
608 sc0g += refine_component_encode<ColorDist>(c[2].g);
609 sc0b += refine_component_encode<ColorDist>(c[2].b);
615 int bitindex = pindex * 2;
617 out[bitindex / 8 + 4] |= (3 << (bitindex % 8));
618 else if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
620 out[bitindex / 8 + 4] |= (1 << (bitindex % 8));
621 if(refine != REFINE_NEVER)
624 sc1r += refine_component_encode<ColorDist>(c[2].r);
625 sc1g += refine_component_encode<ColorDist>(c[2].g);
626 sc1b += refine_component_encode<ColorDist>(c[2].b);
631 if(refine != REFINE_NEVER)
634 sc0r += refine_component_encode<ColorDist>(c[2].r);
635 sc0g += refine_component_encode<ColorDist>(c[2].g);
636 sc0b += refine_component_encode<ColorDist>(c[2].b);
643 if(refine != REFINE_NEVER)
645 // REFINEMENT: trick from libtxc_dxtn: reassign the colors to an average of the colors encoded with that value
650 ca[0] = (2 * sa0 + na0) / (2 * na0);
652 ca[1] = (2 * sa1 + na1) / (2 * na1);
654 if(refine == REFINE_CHECK || refine == REFINE_LOOP)
661 c[0].r = refine_component_decode<ColorDist>((2 * sc0r + nc0) / (2 * nc0));
662 c[0].g = refine_component_decode<ColorDist>((2 * sc0g + nc0) / (2 * nc0));
663 c[0].b = refine_component_decode<ColorDist>((2 * sc0b + nc0) / (2 * nc0));
667 c[1].r = refine_component_decode<ColorDist>((2 * sc1r + nc1) / (2 * nc1));
668 c[1].g = refine_component_decode<ColorDist>((2 * sc1g + nc1) / (2 * nc1));
669 c[1].b = refine_component_decode<ColorDist>((2 * sc1b + nc1) / (2 * nc1));
672 if(refine == REFINE_CHECK || refine == REFINE_LOOP)
676 for(x = 0; x < w; ++x)
677 for(y = 0; y < h; ++y)
679 int pindex = (x+y*4);
680 c[4].r = rgba[(x + y * iw) * 4 + 2];
681 c[4].g = rgba[(x + y * iw) * 4 + 1];
682 c[4].b = rgba[(x + y * iw) * 4 + 0];
683 ca[4] = rgba[(x + y * iw) * 4 + 3];
684 if(dxt == DXT1 && !ca[4])
686 int bitindex = pindex * 2;
687 if(out[bitindex / 8 + (dxt == DXT1 ? 4 : 12)] & (1 << (bitindex % 8)))
690 score_01 += ColorDist(c[1], c[4]);
691 score_23 += ColorDist(c[3], c[4]);
696 score_01 += ColorDist(c[0], c[4]);
697 score_23 += ColorDist(c[2], c[4]);
701 if(score_23 <= score_01)
703 // refinement was BAD
707 else if(refine == REFINE_LOOP)
710 // alpha refinement is always good and doesn't
711 // need to be checked because alpha is linear
722 for(int pindex = 0; pindex < 16; ++pindex)
724 int bitindex_set = pindex * 3;
725 int bitindex_test = bitindex_set + 2;
726 if(!(out[bitindex_test / 8 + 2] & (1 << (bitindex_test % 8))))
727 out[bitindex_set / 8 + 2] ^= (1 << (bitindex_set % 8));
739 out[4] ^= 0x55 & ~(out[4] >> 1);
740 out[5] ^= 0x55 & ~(out[5] >> 1);
741 out[6] ^= 0x55 & ~(out[6] >> 1);
742 out[7] ^= 0x55 & ~(out[7] >> 1);
746 out[12] ^= 0x55 & ~(out[12] >> 1);
747 out[13] ^= 0x55 & ~(out[13] >> 1);
748 out[14] ^= 0x55 & ~(out[14] >> 1);
749 out[15] ^= 0x55 & ~(out[15] >> 1);
754 while(refine == REFINE_LOOP && refined);
762 out[8] = ((c[0].g & 0x07) << 5) | c[0].b;
763 out[9] = (c[0].r << 3) | (c[0].g >> 3);
764 out[10] = ((c[1].g & 0x07) << 5) | c[1].b;
765 out[11] = (c[1].r << 3) | (c[1].g >> 3);
768 out[0] = ((c[0].g & 0x07) << 5) | c[0].b;
769 out[1] = (c[0].r << 3) | (c[0].g >> 3);
770 out[2] = ((c[1].g & 0x07) << 5) | c[1].b;
771 out[3] = (c[1].r << 3) | (c[1].g >> 3);
776 // compile time dispatch magic
777 template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode>
778 inline s2tc_encode_block_func_t s2tc_encode_block_func(RefinementMode refine)
783 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_NEVER>;
785 // these color dist functions do not need the refinement check, as they always improve the situation
786 if(ColorDist != color_dist_avg && ColorDist != color_dist_wavg)
787 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_CHECK>;
789 // these color dist functions do not need the refinement check, as they always improve the situation
790 if(ColorDist != color_dist_avg && ColorDist != color_dist_wavg)
791 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_CHECK>;
794 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_ALWAYS>;
798 template<DxtMode dxt, ColorDistFunc ColorDist>
799 inline s2tc_encode_block_func_t s2tc_encode_block_func(int nrandom, RefinementMode refine)
802 return s2tc_encode_block_func<dxt, ColorDist, MODE_RANDOM>(refine);
803 else if(nrandom == 0 || ColorDist == color_dist_normalmap) // MODE_FAST not supported for normalmaps, sorry
804 return s2tc_encode_block_func<dxt, ColorDist, MODE_NORMAL>(refine);
806 return s2tc_encode_block_func<dxt, ColorDist, MODE_FAST>(refine);
809 template<ColorDistFunc ColorDist>
810 inline s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, int nrandom, RefinementMode refine)
815 return s2tc_encode_block_func<DXT1, ColorDist>(nrandom, refine);
818 return s2tc_encode_block_func<DXT3, ColorDist>(nrandom, refine);
822 return s2tc_encode_block_func<DXT5, ColorDist>(nrandom, refine);
828 s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, ColorDistMode cd, int nrandom, RefinementMode refine)
833 return s2tc_encode_block_func<color_dist_rgb>(dxt, nrandom, refine);
836 return s2tc_encode_block_func<color_dist_yuv>(dxt, nrandom, refine);
839 return s2tc_encode_block_func<color_dist_srgb>(dxt, nrandom, refine);
842 return s2tc_encode_block_func<color_dist_srgb_mixed>(dxt, nrandom, refine);
845 return s2tc_encode_block_func<color_dist_lab_srgb>(dxt, nrandom, refine);
848 return s2tc_encode_block_func<color_dist_avg>(dxt, nrandom, refine);
852 return s2tc_encode_block_func<color_dist_wavg>(dxt, nrandom, refine);
855 return s2tc_encode_block_func<color_dist_normalmap>(dxt, nrandom, refine);
862 inline int diffuse(int *diff, int src, int shift)
864 int maxval = (1 << (8 - shift)) - 1;
866 int ret = max(0, min(src >> shift, maxval));
867 // simulate decoding ("loop filter")
868 int loop = (ret << shift) | (ret >> (8 - 2 * shift));
872 inline int diffuse1(int *diff, int src)
875 int ret = (src >= 128);
876 int loop = ret ? 255 : 0;
882 void rgb565_image(unsigned char *out, const unsigned char *rgba, int w, int h, int srccomps, int bgr, int alphabits)
891 for(y = 0; y < h; ++y)
892 for(x = 0; x < w; ++x)
894 out[(x + y * w) * 4 + 2] = diffuse(&diffuse_r, rgba[(x + y * w) * srccomps + 2], 3);
895 out[(x + y * w) * 4 + 1] = diffuse(&diffuse_g, rgba[(x + y * w) * srccomps + 1], 2);
896 out[(x + y * w) * 4 + 0] = diffuse(&diffuse_b, rgba[(x + y * w) * srccomps + 0], 3);
901 for(y = 0; y < h; ++y)
902 for(x = 0; x < w; ++x)
904 out[(x + y * w) * 4 + 2] = diffuse(&diffuse_r, rgba[(x + y * w) * srccomps + 0], 3);
905 out[(x + y * w) * 4 + 1] = diffuse(&diffuse_g, rgba[(x + y * w) * srccomps + 1], 2);
906 out[(x + y * w) * 4 + 0] = diffuse(&diffuse_b, rgba[(x + y * w) * srccomps + 2], 3);
913 for(y = 0; y < h; ++y)
914 for(x = 0; x < w; ++x)
915 out[(x + y * w) * 4 + 3] = diffuse1(&diffuse_a, rgba[(x + y * w) * srccomps + 3]);
917 else if(alphabits == 8)
919 for(y = 0; y < h; ++y)
920 for(x = 0; x < w; ++x)
921 out[(x + y * w) * 4 + 3] = rgba[(x + y * w) * srccomps + 3]; // no conversion
925 int alphadiffuse = 8 - alphabits;
926 for(y = 0; y < h; ++y)
927 for(x = 0; x < w; ++x)
928 out[(x + y * w) * 4 + 3] = diffuse(&diffuse_a, rgba[(x + y * w) * srccomps + 3], alphadiffuse);
933 int alpharange = (1 << alphabits) - 1;
934 for(y = 0; y < h; ++y)
935 for(x = 0; x < w; ++x)
936 out[(x + y * w) * 4 + 3] = alpharange;