Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ColorBlock.cpp « dds « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2e8e0b0313bb1f718cc14466ca0d1b812d3e17b (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup imbdds
 */

/*
 * This file is based on a similar file from the NVIDIA texture tools
 * (http://nvidia-texture-tools.googlecode.com/)
 *
 * Original license from NVIDIA follows.
 */

// This code is in the public domain -- castanyo@yahoo.es

#include <ColorBlock.h>
#include <Common.h>
#include <Image.h>

#if 0
// Get approximate luminance.
inline static uint colorLuminance(Color32 c)
{
  return c.r + c.g + c.b;
}

// Get the euclidean distance between the given colors.
inline static uint colorDistance(Color32 c0, Color32 c1)
{
  return (c0.r - c1.r) * (c0.r - c1.r) + (c0.g - c1.g) * (c0.g - c1.g) +
         (c0.b - c1.b) * (c0.b - c1.b);
}
#endif

/** Default constructor. */
ColorBlock::ColorBlock()
{
}

/** Init the color block from an array of colors. */
ColorBlock::ColorBlock(const uint *linearImage)
{
  for (uint i = 0; i < 16; i++) {
    color(i) = Color32(linearImage[i]);
  }
}

/** Init the color block with the contents of the given block. */
ColorBlock::ColorBlock(const ColorBlock &block)
{
  for (uint i = 0; i < 16; i++) {
    color(i) = block.color(i);
  }
}

/** Initialize this color block. */
ColorBlock::ColorBlock(const Image *img, uint x, uint y)
{
  init(img, x, y);
}

void ColorBlock::init(const Image *img, uint x, uint y)
{
  init(img->width(), img->height(), (const uint *)img->pixels(), x, y);
}

void ColorBlock::init(uint w, uint h, const uint *data, uint x, uint y)
{
  const uint bw = MIN(w - x, 4U);
  const uint bh = MIN(h - y, 4U);

  // Blocks that are smaller than 4x4 are handled by repeating the pixels.
  // @@ That's only correct when block size is 1, 2 or 4, but not with 3. :(
  // @@ Ideally we should zero the weights of the pixels out of range.

  for (uint i = 0; i < 4; i++) {
    const int by = i % bh;

    for (uint e = 0; e < 4; e++) {
      const int bx = e % bw;
      const uint idx = (y + by) * w + x + bx;

      color(e, i).u = data[idx];
    }
  }
}

void ColorBlock::init(uint w, uint h, const float *data, uint x, uint y)
{
  const uint bw = MIN(w - x, 4U);
  const uint bh = MIN(h - y, 4U);

  // Blocks that are smaller than 4x4 are handled by repeating the pixels.
  // @@ That's only correct when block size is 1, 2 or 4, but not with 3. :(
  // @@ Ideally we should zero the weights of the pixels out of range.

  uint srcPlane = w * h;

  for (uint i = 0; i < 4; i++) {
    const uint by = i % bh;

    for (uint e = 0; e < 4; e++) {
      const uint bx = e % bw;
      const uint idx = ((y + by) * w + x + bx);

      Color32 &c = color(e, i);
      c.r = uint8(255 * CLAMP(data[idx + 0 * srcPlane],
                              0.0f,
                              1.0f));  // @@ Is this the right way to quantize floats to bytes?
      c.g = uint8(255 * CLAMP(data[idx + 1 * srcPlane], 0.0f, 1.0f));
      c.b = uint8(255 * CLAMP(data[idx + 2 * srcPlane], 0.0f, 1.0f));
      c.a = uint8(255 * CLAMP(data[idx + 3 * srcPlane], 0.0f, 1.0f));
    }
  }
}

static inline uint8 component(Color32 c, uint i)
{
  if (i == 0) {
    return c.r;
  }
  if (i == 1) {
    return c.g;
  }
  if (i == 2) {
    return c.b;
  }
  if (i == 3) {
    return c.a;
  }
  if (i == 4) {
    return 0xFF;
  }
  return 0;
}

void ColorBlock::swizzle(uint x, uint y, uint z, uint w)
{
  for (int i = 0; i < 16; i++) {
    Color32 c = m_color[i];
    m_color[i].r = component(c, x);
    m_color[i].g = component(c, y);
    m_color[i].b = component(c, z);
    m_color[i].a = component(c, w);
  }
}

/** Returns true if the block has a single color. */
bool ColorBlock::isSingleColor(Color32 mask /*= Color32(0xFF, 0xFF, 0xFF, 0x00)*/) const
{
  uint u = m_color[0].u & mask.u;

  for (int i = 1; i < 16; i++) {
    if (u != (m_color[i].u & mask.u)) {
      return false;
    }
  }

  return true;
}

#if 0
/** Returns true if the block has a single color, ignoring transparent pixels. */
bool ColorBlock::isSingleColorNoAlpha() const
{
  Color32 c;
  int i;
  for (i = 0; i < 16; i++) {
    if (m_color[i].a != 0) {
      c = m_color[i];
    }
  }

  Color32 mask(0xFF, 0xFF, 0xFF, 0x00);
  uint u = c.u & mask.u;

  for (; i < 16; i++) {
    if (u != (m_color[i].u & mask.u)) {
      return false;
    }
  }

  return true;
}
#endif

#if 0
/** Count number of unique colors in this color block. */
uint ColorBlock::countUniqueColors() const
{
  uint count = 0;

  // @@ This does not have to be o(n^2)
  for (int i = 0; i < 16; i++) {
    bool unique = true;
    for (int j = 0; j < i; j++) {
      if (m_color[i] != m_color[j]) {
        unique = false;
      }
    }

    if (unique) {
      count++;
    }
  }

  return count;
}
#endif

#if 0
/** Get average color of the block. */
Color32 ColorBlock::averageColor() const
{
  uint r, g, b, a;
  r = g = b = a = 0;

  for (uint i = 0; i < 16; i++) {
    r += m_color[i].r;
    g += m_color[i].g;
    b += m_color[i].b;
    a += m_color[i].a;
  }

  return Color32(uint8(r / 16), uint8(g / 16), uint8(b / 16), uint8(a / 16));
}
#endif

/** Return true if the block is not fully opaque. */
bool ColorBlock::hasAlpha() const
{
  for (uint i = 0; i < 16; i++) {
    if (m_color[i].a != 255) {
      return true;
    }
  }
  return false;
}

#if 0

/** Get diameter color range. */
void ColorBlock::diameterRange(Color32 *start, Color32 *end) const
{
  Color32 c0, c1;
  uint best_dist = 0;

  for (int i = 0; i < 16; i++) {
    for (int j = i + 1; j < 16; j++) {
      uint dist = colorDistance(m_color[i], m_color[j]);
      if (dist > best_dist) {
        best_dist = dist;
        c0 = m_color[i];
        c1 = m_color[j];
      }
    }
  }

  *start = c0;
  *end = c1;
}

/** Get luminance color range. */
void ColorBlock::luminanceRange(Color32 *start, Color32 *end) const
{
  Color32 minColor, maxColor;
  uint minLuminance, maxLuminance;

  maxLuminance = minLuminance = colorLuminance(m_color[0]);

  for (uint i = 1; i < 16; i++) {
    uint luminance = colorLuminance(m_color[i]);

    if (luminance > maxLuminance) {
      maxLuminance = luminance;
      maxColor = m_color[i];
    }
    else if (luminance < minLuminance) {
      minLuminance = luminance;
      minColor = m_color[i];
    }
  }

  *start = minColor;
  *end = maxColor;
}

/** Get color range based on the bounding box. */
void ColorBlock::boundsRange(Color32 *start, Color32 *end) const
{
  Color32 minColor(255, 255, 255);
  Color32 maxColor(0, 0, 0);

  for (uint i = 0; i < 16; i++) {
    if (m_color[i].r < minColor.r) {
      minColor.r = m_color[i].r;
    }
    if (m_color[i].g < minColor.g) {
      minColor.g = m_color[i].g;
    }
    if (m_color[i].b < minColor.b) {
      minColor.b = m_color[i].b;
    }
    if (m_color[i].r > maxColor.r) {
      maxColor.r = m_color[i].r;
    }
    if (m_color[i].g > maxColor.g) {
      maxColor.g = m_color[i].g;
    }
    if (m_color[i].b > maxColor.b) {
      maxColor.b = m_color[i].b;
    }
  }

  // Offset range by 1/16 of the extents
  Color32 inset;
  inset.r = (maxColor.r - minColor.r) >> 4;
  inset.g = (maxColor.g - minColor.g) >> 4;
  inset.b = (maxColor.b - minColor.b) >> 4;

  minColor.r = (minColor.r + inset.r <= 255) ? minColor.r + inset.r : 255;
  minColor.g = (minColor.g + inset.g <= 255) ? minColor.g + inset.g : 255;
  minColor.b = (minColor.b + inset.b <= 255) ? minColor.b + inset.b : 255;

  maxColor.r = (maxColor.r >= inset.r) ? maxColor.r - inset.r : 0;
  maxColor.g = (maxColor.g >= inset.g) ? maxColor.g - inset.g : 0;
  maxColor.b = (maxColor.b >= inset.b) ? maxColor.b - inset.b : 0;

  *start = minColor;
  *end = maxColor;
}

/** Get color range based on the bounding box. */
void ColorBlock::boundsRangeAlpha(Color32 *start, Color32 *end) const
{
  Color32 minColor(255, 255, 255, 255);
  Color32 maxColor(0, 0, 0, 0);

  for (uint i = 0; i < 16; i++) {
    if (m_color[i].r < minColor.r) {
      minColor.r = m_color[i].r;
    }
    if (m_color[i].g < minColor.g) {
      minColor.g = m_color[i].g;
    }
    if (m_color[i].b < minColor.b) {
      minColor.b = m_color[i].b;
    }
    if (m_color[i].a < minColor.a) {
      minColor.a = m_color[i].a;
    }
    if (m_color[i].r > maxColor.r) {
      maxColor.r = m_color[i].r;
    }
    if (m_color[i].g > maxColor.g) {
      maxColor.g = m_color[i].g;
    }
    if (m_color[i].b > maxColor.b) {
      maxColor.b = m_color[i].b;
    }
    if (m_color[i].a > maxColor.a) {
      maxColor.a = m_color[i].a;
    }
  }

  // Offset range by 1/16 of the extents
  Color32 inset;
  inset.r = (maxColor.r - minColor.r) >> 4;
  inset.g = (maxColor.g - minColor.g) >> 4;
  inset.b = (maxColor.b - minColor.b) >> 4;
  inset.a = (maxColor.a - minColor.a) >> 4;

  minColor.r = (minColor.r + inset.r <= 255) ? minColor.r + inset.r : 255;
  minColor.g = (minColor.g + inset.g <= 255) ? minColor.g + inset.g : 255;
  minColor.b = (minColor.b + inset.b <= 255) ? minColor.b + inset.b : 255;
  minColor.a = (minColor.a + inset.a <= 255) ? minColor.a + inset.a : 255;

  maxColor.r = (maxColor.r >= inset.r) ? maxColor.r - inset.r : 0;
  maxColor.g = (maxColor.g >= inset.g) ? maxColor.g - inset.g : 0;
  maxColor.b = (maxColor.b >= inset.b) ? maxColor.b - inset.b : 0;
  maxColor.a = (maxColor.a >= inset.a) ? maxColor.a - inset.a : 0;

  *start = minColor;
  *end = maxColor;
}
#endif

#if 0
/** Sort colors by abosolute value in their 16 bit representation. */
void ColorBlock::sortColorsByAbsoluteValue()
{
  // Dummy selection sort.
  for (uint a = 0; a < 16; a++) {
    uint max = a;
    Color16 cmax(m_color[a]);

    for (uint b = a + 1; b < 16; b++) {
      Color16 cb(m_color[b]);

      if (cb.u > cmax.u) {
        max = b;
        cmax = cb;
      }
    }
    swap(m_color[a], m_color[max]);
  }
}
#endif

#if 0
/** Find extreme colors in the given axis. */
void ColorBlock::computeRange(Vector3::Arg axis, Color32 *start, Color32 *end) const
{

  int mini, maxi;
  mini = maxi = 0;

  float min, max;
  min = max = dot(Vector3(m_color[0].r, m_color[0].g, m_color[0].b), axis);

  for (uint i = 1; i < 16; i++) {
    const Vector3 vec(m_color[i].r, m_color[i].g, m_color[i].b);

    float val = dot(vec, axis);
    if (val < min) {
      mini = i;
      min = val;
    }
    else if (val > max) {
      maxi = i;
      max = val;
    }
  }

  *start = m_color[mini];
  *end = m_color[maxi];
}
#endif

#if 0
/** Sort colors in the given axis. */
void ColorBlock::sortColors(const Vector3 &axis)
{
  float luma_array[16];

  for (uint i = 0; i < 16; i++) {
    const Vector3 vec(m_color[i].r, m_color[i].g, m_color[i].b);
    luma_array[i] = dot(vec, axis);
  }

  // Dummy selection sort.
  for (uint a = 0; a < 16; a++) {
    uint min = a;
    for (uint b = a + 1; b < 16; b++) {
      if (luma_array[b] < luma_array[min]) {
        min = b;
      }
    }
    swap(luma_array[a], luma_array[min]);
    swap(m_color[a], m_color[min]);
  }
}
#endif

#if 0
/** Get the volume of the color block. */
float ColorBlock::volume() const
{
  Box bounds;
  bounds.clearBounds();

  for (int i = 0; i < 16; i++) {
    const Vector3 point(m_color[i].r, m_color[i].g, m_color[i].b);
    bounds.addPointToBounds(point);
  }

  return bounds.volume();
}
#endif