| | 1 | | // <copyright file="WuColorQuantizer.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2014-2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | #if !NET8_0_OR_GREATER |
| | 9 | | namespace JeremyAnsel.ColorQuant |
| | 10 | | { |
| | 11 | | using System; |
| | 12 | | using System.Diagnostics.CodeAnalysis; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// A Wu's color quantizer. |
| | 16 | | /// </summary> |
| | 17 | | /// <remarks> |
| | 18 | | /// <para> |
| | 19 | | /// Based on C Implementation of Xiaolin Wu's Color Quantizer (v. 2) |
| | 20 | | /// (see Graphics Gems volume II, pages 126-133) |
| | 21 | | /// (<see href="http://www.ece.mcmaster.ca/~xwu/cq.c"/>). |
| | 22 | | /// </para> |
| | 23 | | /// <para> |
| | 24 | | /// Algorithm: Greedy orthogonal bipartition of RGB space for variance |
| | 25 | | /// minimization aided by inclusion-exclusion tricks. |
| | 26 | | /// For speed no nearest neighbor search is done. Slightly |
| | 27 | | /// better performance can be expected by more sophisticated |
| | 28 | | /// but more expensive versions. |
| | 29 | | /// </para> |
| | 30 | | /// </remarks> |
| | 31 | | [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Wu", Justification = " |
| | 32 | | public sealed class WuColorQuantizer : IColorQuantizer |
| | 33 | | { |
| | 34 | | /// <summary> |
| | 35 | | /// The maximum color count for the quantize. |
| | 36 | | /// </summary> |
| | 37 | | public const int MaxColors = 256; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// The index bits. |
| | 41 | | /// </summary> |
| | 42 | | private const int IndexBits = 7; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The index count. |
| | 46 | | /// </summary> |
| | 47 | | private const int IndexCount = (1 << IndexBits) + 1; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// The table length. |
| | 51 | | /// </summary> |
| | 52 | | private const int TableLength = IndexCount * IndexCount * IndexCount; |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Moment of <c>P(c)</c>. |
| | 56 | | /// </summary> |
| 1 | 57 | | private readonly long[] vwt = new long[TableLength]; |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Moment of <c>r*P(c)</c>. |
| | 61 | | /// </summary> |
| 1 | 62 | | private readonly long[] vmr = new long[TableLength]; |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Moment of <c>g*P(c)</c>. |
| | 66 | | /// </summary> |
| 1 | 67 | | private readonly long[] vmg = new long[TableLength]; |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Moment of <c>b*P(c)</c>. |
| | 71 | | /// </summary> |
| 1 | 72 | | private readonly long[] vmb = new long[TableLength]; |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Moment of <c>c^2*P(c)</c>. |
| | 76 | | /// </summary> |
| 1 | 77 | | private readonly double[] m2 = new double[TableLength]; |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Color space tag. |
| | 81 | | /// </summary> |
| 1 | 82 | | private readonly byte[] tag = new byte[TableLength]; |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Quantizes an image. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="image">The image (XRGB).</param> |
| | 88 | | /// <returns>The result.</returns> |
| | 89 | | public ColorQuantizerResult Quantize(byte[] image) |
| | 90 | | { |
| 11 | 91 | | return this.Quantize(image, 256); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Quantizes an image. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="image">The image (XRGB).</param> |
| | 98 | | /// <param name="colorCount">The color count.</param> |
| | 99 | | /// <returns>The result.</returns> |
| | 100 | | public ColorQuantizerResult Quantize(byte[] image, int colorCount) |
| | 101 | | { |
| 14 | 102 | | if (image == null) |
| | 103 | | { |
| 2 | 104 | | throw new ArgumentNullException(nameof(image)); |
| | 105 | | } |
| | 106 | |
|
| 12 | 107 | | if (colorCount < 1 || colorCount > 256) |
| | 108 | | { |
| 2 | 109 | | throw new ArgumentOutOfRangeException(nameof(colorCount)); |
| | 110 | | } |
| | 111 | |
|
| 10 | 112 | | this.Clear(); |
| | 113 | |
|
| 10 | 114 | | this.Build3DHistogram(image); |
| 10 | 115 | | this.Get3DMoments(); |
| | 116 | |
|
| | 117 | | Box[] cube; |
| 10 | 118 | | this.BuildCube(out cube, ref colorCount); |
| | 119 | |
|
| 10 | 120 | | return this.GenerateResult(image, colorCount, cube); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Gets an index. |
| | 125 | | /// </summary> |
| | 126 | | /// <param name="r">The red value.</param> |
| | 127 | | /// <param name="g">The green value.</param> |
| | 128 | | /// <param name="b">The blue value.</param> |
| | 129 | | /// <returns>The index.</returns> |
| | 130 | | private static int GetIndex(int r, int g, int b) |
| | 131 | | { |
| 199588134 | 132 | | return (r << (IndexBits * 2)) + (r << (IndexBits + 1)) + (g << IndexBits) + r + g + b; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Computes sum over a box of any given statistic. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="cube">The cube.</param> |
| | 139 | | /// <param name="moment">The moment.</param> |
| | 140 | | /// <returns>The result.</returns> |
| | 141 | | private static double Volume(Box cube, long[] moment) |
| | 142 | | { |
| 24472 | 143 | | return moment[cube.Index111] |
| 24472 | 144 | | - moment[cube.Index110] |
| 24472 | 145 | | - moment[cube.Index101] |
| 24472 | 146 | | + moment[cube.Index100] |
| 24472 | 147 | | - moment[cube.Index011] |
| 24472 | 148 | | + moment[cube.Index010] |
| 24472 | 149 | | + moment[cube.Index001] |
| 24472 | 150 | | - moment[cube.Index000]; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | /// <summary> |
| | 154 | | /// Computes part of Volume(cube, moment) that doesn't depend on r1, g1, or b1 (depending on direction). |
| | 155 | | /// </summary> |
| | 156 | | /// <param name="cube">The cube.</param> |
| | 157 | | /// <param name="direction">The direction.</param> |
| | 158 | | /// <param name="moment">The moment.</param> |
| | 159 | | /// <returns>The result.</returns> |
| | 160 | | private static long Bottom(Box cube, int direction, long[] moment) |
| | 161 | | { |
| | 162 | | switch (direction) |
| | 163 | | { |
| | 164 | | // Red |
| | 165 | | case 2: |
| 9184 | 166 | | return -moment[cube.Index011] |
| 9184 | 167 | | + moment[cube.Index010] |
| 9184 | 168 | | + moment[cube.Index001] |
| 9184 | 169 | | - moment[cube.Index000]; |
| | 170 | |
|
| | 171 | | // Green |
| | 172 | | case 1: |
| 9184 | 173 | | return -moment[cube.Index101] |
| 9184 | 174 | | + moment[cube.Index100] |
| 9184 | 175 | | + moment[cube.Index001] |
| 9184 | 176 | | - moment[cube.Index000]; |
| | 177 | |
|
| | 178 | | // Blue |
| | 179 | | case 0: |
| 9184 | 180 | | return -moment[cube.Index110] |
| 9184 | 181 | | + moment[cube.Index100] |
| 9184 | 182 | | + moment[cube.Index010] |
| 9184 | 183 | | - moment[cube.Index000]; |
| | 184 | |
|
| | 185 | | default: |
| 0 | 186 | | throw new ArgumentOutOfRangeException(nameof(direction)); |
| | 187 | | } |
| | 188 | | } |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Computes remainder of Volume(cube, moment), substituting position for r1, g1, or b1 (depending on direction) |
| | 192 | | /// </summary> |
| | 193 | | /// <param name="cube">The cube.</param> |
| | 194 | | /// <param name="direction">The direction.</param> |
| | 195 | | /// <param name="position">The position.</param> |
| | 196 | | /// <param name="moment">The moment.</param> |
| | 197 | | /// <returns>The result.</returns> |
| | 198 | | private static long Top(Box cube, int direction, int position, long[] moment) |
| | 199 | | { |
| | 200 | | switch (direction) |
| | 201 | | { |
| | 202 | | // Red |
| | 203 | | case 2: |
| 159866 | 204 | | return moment[GetIndex(position, cube.G1, cube.B1)] |
| 159866 | 205 | | - moment[GetIndex(position, cube.G1, cube.B0)] |
| 159866 | 206 | | - moment[GetIndex(position, cube.G0, cube.B1)] |
| 159866 | 207 | | + moment[GetIndex(position, cube.G0, cube.B0)]; |
| | 208 | |
|
| | 209 | | // Green |
| | 210 | | case 1: |
| 215797 | 211 | | return moment[GetIndex(cube.R1, position, cube.B1)] |
| 215797 | 212 | | - moment[GetIndex(cube.R1, position, cube.B0)] |
| 215797 | 213 | | - moment[GetIndex(cube.R0, position, cube.B1)] |
| 215797 | 214 | | + moment[GetIndex(cube.R0, position, cube.B0)]; |
| | 215 | |
|
| | 216 | | // Blue |
| | 217 | | case 0: |
| 237658 | 218 | | return moment[GetIndex(cube.R1, cube.G1, position)] |
| 237658 | 219 | | - moment[GetIndex(cube.R1, cube.G0, position)] |
| 237658 | 220 | | - moment[GetIndex(cube.R0, cube.G1, position)] |
| 237658 | 221 | | + moment[GetIndex(cube.R0, cube.G0, position)]; |
| | 222 | |
|
| | 223 | | default: |
| 0 | 224 | | throw new ArgumentOutOfRangeException(nameof(direction)); |
| | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| | 228 | | /// <summary> |
| | 229 | | /// Clears the tables. |
| | 230 | | /// </summary> |
| | 231 | | private void Clear() |
| | 232 | | { |
| 10 | 233 | | this.vwt.AsSpan().Clear(); |
| 10 | 234 | | this.vmr.AsSpan().Clear(); |
| 10 | 235 | | this.vmg.AsSpan().Clear(); |
| 10 | 236 | | this.vmb.AsSpan().Clear(); |
| 10 | 237 | | this.m2.AsSpan().Clear(); |
| 10 | 238 | | this.tag.AsSpan().Clear(); |
| 10 | 239 | | } |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Builds a 3-D color histogram of <c>counts, r/g/b, c^2</c>. |
| | 243 | | /// </summary> |
| | 244 | | /// <param name="image">The image.</param> |
| | 245 | | private void Build3DHistogram(byte[] image) |
| | 246 | | { |
| 134220310 | 247 | | for (int i = 0; i < image.Length; i += 4) |
| | 248 | | { |
| 67110145 | 249 | | int r = image[i + 2]; |
| 67110145 | 250 | | int g = image[i + 1]; |
| 67110145 | 251 | | int b = image[i]; |
| | 252 | |
|
| 67110145 | 253 | | int inr = r >> (8 - IndexBits); |
| 67110145 | 254 | | int ing = g >> (8 - IndexBits); |
| 67110145 | 255 | | int inb = b >> (8 - IndexBits); |
| | 256 | |
|
| 67110145 | 257 | | int ind = GetIndex(inr + 1, ing + 1, inb + 1); |
| | 258 | |
|
| 67110145 | 259 | | this.vwt[ind]++; |
| 67110145 | 260 | | this.vmr[ind] += r; |
| 67110145 | 261 | | this.vmg[ind] += g; |
| 67110145 | 262 | | this.vmb[ind] += b; |
| 67110145 | 263 | | this.m2[ind] += (r * r) + (g * g) + (b * b); |
| | 264 | | } |
| 10 | 265 | | } |
| | 266 | |
|
| | 267 | | /// <summary> |
| | 268 | | /// Converts the histogram into moments so that we can rapidly calculate |
| | 269 | | /// the sums of the above quantities over any desired box. |
| | 270 | | /// </summary> |
| | 271 | | private void Get3DMoments() |
| | 272 | | { |
| 10 | 273 | | Span<long> area = stackalloc long[IndexCount]; |
| 10 | 274 | | Span<long> areaR = stackalloc long[IndexCount]; |
| 10 | 275 | | Span<long> areaG = stackalloc long[IndexCount]; |
| 10 | 276 | | Span<long> areaB = stackalloc long[IndexCount]; |
| 10 | 277 | | Span<double> area2 = stackalloc double[IndexCount]; |
| | 278 | |
|
| 2580 | 279 | | for (int r = 1; r < IndexCount; r++) |
| | 280 | | { |
| 1280 | 281 | | area.Clear(); |
| 1280 | 282 | | areaR.Clear(); |
| 1280 | 283 | | areaG.Clear(); |
| 1280 | 284 | | areaB.Clear(); |
| 1280 | 285 | | area2.Clear(); |
| | 286 | |
|
| 330240 | 287 | | for (int g = 1; g < IndexCount; g++) |
| | 288 | | { |
| 163840 | 289 | | long line = 0; |
| 163840 | 290 | | long lineR = 0; |
| 163840 | 291 | | long lineG = 0; |
| 163840 | 292 | | long lineB = 0; |
| 163840 | 293 | | double line2 = 0; |
| | 294 | |
|
| 42270720 | 295 | | for (int b = 1; b < IndexCount; b++) |
| | 296 | | { |
| 20971520 | 297 | | int ind1 = GetIndex(r, g, b); |
| | 298 | |
|
| 20971520 | 299 | | line += this.vwt[ind1]; |
| 20971520 | 300 | | lineR += this.vmr[ind1]; |
| 20971520 | 301 | | lineG += this.vmg[ind1]; |
| 20971520 | 302 | | lineB += this.vmb[ind1]; |
| 20971520 | 303 | | line2 += this.m2[ind1]; |
| | 304 | |
|
| 20971520 | 305 | | area[b] += line; |
| 20971520 | 306 | | areaR[b] += lineR; |
| 20971520 | 307 | | areaG[b] += lineG; |
| 20971520 | 308 | | areaB[b] += lineB; |
| 20971520 | 309 | | area2[b] += line2; |
| | 310 | |
|
| 20971520 | 311 | | int ind2 = ind1 - GetIndex(1, 0, 0); |
| | 312 | |
|
| 20971520 | 313 | | this.vwt[ind1] = this.vwt[ind2] + area[b]; |
| 20971520 | 314 | | this.vmr[ind1] = this.vmr[ind2] + areaR[b]; |
| 20971520 | 315 | | this.vmg[ind1] = this.vmg[ind2] + areaG[b]; |
| 20971520 | 316 | | this.vmb[ind1] = this.vmb[ind2] + areaB[b]; |
| 20971520 | 317 | | this.m2[ind1] = this.m2[ind2] + area2[b]; |
| | 318 | | } |
| | 319 | | } |
| | 320 | | } |
| 10 | 321 | | } |
| | 322 | |
|
| | 323 | | /// <summary> |
| | 324 | | /// Computes the weighted variance of a box. |
| | 325 | | /// </summary> |
| | 326 | | /// <param name="cube">The cube.</param> |
| | 327 | | /// <returns>The result.</returns> |
| | 328 | | private double Variance(Box cube) |
| | 329 | | { |
| 2541 | 330 | | double dr = Volume(cube, this.vmr); |
| 2541 | 331 | | double dg = Volume(cube, this.vmg); |
| 2541 | 332 | | double db = Volume(cube, this.vmb); |
| | 333 | |
|
| 2541 | 334 | | double xx = |
| 2541 | 335 | | this.m2[cube.Index111] |
| 2541 | 336 | | - this.m2[cube.Index110] |
| 2541 | 337 | | - this.m2[cube.Index101] |
| 2541 | 338 | | + this.m2[cube.Index100] |
| 2541 | 339 | | - this.m2[cube.Index011] |
| 2541 | 340 | | + this.m2[cube.Index010] |
| 2541 | 341 | | + this.m2[cube.Index001] |
| 2541 | 342 | | - this.m2[cube.Index000]; |
| | 343 | |
|
| 2541 | 344 | | return xx - (((dr * dr) + (dg * dg) + (db * db)) / Volume(cube, this.vwt)); |
| | 345 | | } |
| | 346 | |
|
| | 347 | | /// <summary> |
| | 348 | | /// We want to minimize the sum of the variances of two sub-boxes. |
| | 349 | | /// The sum(c^2) terms can be ignored since their sum over both sub-boxes |
| | 350 | | /// is the same (the sum for the whole box) no matter where we split. |
| | 351 | | /// The remaining terms have a minus sign in the variance formula, |
| | 352 | | /// so we drop the minus sign and maximize the sum of the two terms. |
| | 353 | | /// </summary> |
| | 354 | | /// <param name="cube">The cube.</param> |
| | 355 | | /// <param name="direction">The direction.</param> |
| | 356 | | /// <param name="first">The first position.</param> |
| | 357 | | /// <param name="last">The last position.</param> |
| | 358 | | /// <param name="cut">The cutting point.</param> |
| | 359 | | /// <param name="wholeR">The whole red.</param> |
| | 360 | | /// <param name="wholeG">The whole green.</param> |
| | 361 | | /// <param name="wholeB">The whole blue.</param> |
| | 362 | | /// <param name="wholeW">The whole weight.</param> |
| | 363 | | /// <returns>The result.</returns> |
| | 364 | | private double Maximize(Box cube, int direction, int first, int last, out int cut, double wholeR, double wholeG, |
| | 365 | | { |
| 6888 | 366 | | long baseR = Bottom(cube, direction, this.vmr); |
| 6888 | 367 | | long baseG = Bottom(cube, direction, this.vmg); |
| 6888 | 368 | | long baseB = Bottom(cube, direction, this.vmb); |
| 6888 | 369 | | long baseW = Bottom(cube, direction, this.vwt); |
| | 370 | |
|
| 6888 | 371 | | double max = 0.0; |
| 6888 | 372 | | cut = -1; |
| | 373 | |
|
| 1114880 | 374 | | for (int i = first; i < last; i++) |
| | 375 | | { |
| 550552 | 376 | | double halfW1 = baseW + Top(cube, direction, i, this.vwt); |
| | 377 | |
|
| 550552 | 378 | | if (halfW1 == 0) |
| | 379 | | { |
| | 380 | | continue; |
| | 381 | | } |
| | 382 | |
|
| 473296 | 383 | | double halfW2 = wholeW - halfW1; |
| | 384 | |
|
| 473296 | 385 | | if (halfW2 == 0) |
| | 386 | | { |
| | 387 | | continue; |
| | 388 | | } |
| | 389 | |
|
| 20923 | 390 | | double halfR1 = baseR + Top(cube, direction, i, this.vmr); |
| 20923 | 391 | | double halfG1 = baseG + Top(cube, direction, i, this.vmg); |
| 20923 | 392 | | double halfB1 = baseB + Top(cube, direction, i, this.vmb); |
| | 393 | |
|
| 20923 | 394 | | double temp = ((halfR1 * halfR1) + (halfG1 * halfG1) + (halfB1 * halfB1)) / halfW1; |
| | 395 | |
|
| 20923 | 396 | | double halfR2 = wholeR - halfR1; |
| 20923 | 397 | | double halfG2 = wholeG - halfG1; |
| 20923 | 398 | | double halfB2 = wholeB - halfB1; |
| | 399 | |
|
| 20923 | 400 | | temp += ((halfR2 * halfR2) + (halfG2 * halfG2) + (halfB2 * halfB2)) / halfW2; |
| | 401 | |
|
| 20923 | 402 | | if (temp > max) |
| | 403 | | { |
| 5888 | 404 | | max = temp; |
| 5888 | 405 | | cut = i; |
| | 406 | | } |
| | 407 | | } |
| | 408 | |
|
| 6888 | 409 | | return max; |
| | 410 | | } |
| | 411 | |
|
| | 412 | | /// <summary> |
| | 413 | | /// Cuts a box. |
| | 414 | | /// </summary> |
| | 415 | | /// <param name="set1">The first set.</param> |
| | 416 | | /// <param name="set2">The second set.</param> |
| | 417 | | /// <returns>Returns a value indicating whether the box has been split.</returns> |
| | 418 | | private bool Cut(Box set1, Box set2) |
| | 419 | | { |
| 2296 | 420 | | double wholeR = Volume(set1, this.vmr); |
| 2296 | 421 | | double wholeG = Volume(set1, this.vmg); |
| 2296 | 422 | | double wholeB = Volume(set1, this.vmb); |
| 2296 | 423 | | double wholeW = Volume(set1, this.vwt); |
| | 424 | |
|
| | 425 | | int cutr; |
| | 426 | | int cutg; |
| | 427 | | int cutb; |
| | 428 | |
|
| 2296 | 429 | | double maxr = this.Maximize(set1, 2, set1.R0 + 1, set1.R1, out cutr, wholeR, wholeG, wholeB, wholeW); |
| 2296 | 430 | | double maxg = this.Maximize(set1, 1, set1.G0 + 1, set1.G1, out cutg, wholeR, wholeG, wholeB, wholeW); |
| 2296 | 431 | | double maxb = this.Maximize(set1, 0, set1.B0 + 1, set1.B1, out cutb, wholeR, wholeG, wholeB, wholeW); |
| | 432 | |
|
| | 433 | | int dir; |
| | 434 | |
|
| 2296 | 435 | | if ((maxr >= maxg) && (maxr >= maxb)) |
| | 436 | | { |
| 1615 | 437 | | dir = 2; |
| | 438 | |
|
| 1615 | 439 | | if (cutr < 0) |
| | 440 | | { |
| 1025 | 441 | | return false; |
| | 442 | | } |
| | 443 | | } |
| 681 | 444 | | else if ((maxg >= maxr) && (maxg >= maxb)) |
| | 445 | | { |
| 263 | 446 | | dir = 1; |
| | 447 | | } |
| | 448 | | else |
| | 449 | | { |
| 418 | 450 | | dir = 0; |
| | 451 | | } |
| | 452 | |
|
| 1271 | 453 | | set2.R1 = set1.R1; |
| 1271 | 454 | | set2.G1 = set1.G1; |
| 1271 | 455 | | set2.B1 = set1.B1; |
| | 456 | |
|
| | 457 | | switch (dir) |
| | 458 | | { |
| | 459 | | // Red |
| | 460 | | case 2: |
| 590 | 461 | | set2.R0 = set1.R1 = cutr; |
| 590 | 462 | | set2.G0 = set1.G0; |
| 590 | 463 | | set2.B0 = set1.B0; |
| 590 | 464 | | break; |
| | 465 | |
|
| | 466 | | // Green |
| | 467 | | case 1: |
| 263 | 468 | | set2.G0 = set1.G1 = cutg; |
| 263 | 469 | | set2.R0 = set1.R0; |
| 263 | 470 | | set2.B0 = set1.B0; |
| 263 | 471 | | break; |
| | 472 | |
|
| | 473 | | // Blue |
| | 474 | | case 0: |
| 418 | 475 | | set2.B0 = set1.B1 = cutb; |
| 418 | 476 | | set2.R0 = set1.R0; |
| 418 | 477 | | set2.G0 = set1.G0; |
| | 478 | | break; |
| | 479 | | } |
| | 480 | |
|
| 1271 | 481 | | set1.Update(); |
| 1271 | 482 | | set2.Update(); |
| | 483 | |
|
| 1271 | 484 | | return true; |
| | 485 | | } |
| | 486 | |
|
| | 487 | | /// <summary> |
| | 488 | | /// Marks a color space tag. |
| | 489 | | /// </summary> |
| | 490 | | /// <param name="cube">The cube.</param> |
| | 491 | | /// <param name="label">A label.</param> |
| | 492 | | private void Mark(Box cube, byte label) |
| | 493 | | { |
| 143106 | 494 | | for (int r = cube.R0 + 1; r <= cube.R1; r++) |
| | 495 | | { |
| 9020672 | 496 | | for (int g = cube.G0 + 1; g <= cube.G1; g++) |
| | 497 | | { |
| 50823168 | 498 | | for (int b = cube.B0 + 1; b <= cube.B1; b++) |
| | 499 | | { |
| 20971520 | 500 | | this.tag[GetIndex(r, g, b)] = label; |
| | 501 | | } |
| | 502 | | } |
| | 503 | | } |
| 1281 | 504 | | } |
| | 505 | |
|
| | 506 | | /// <summary> |
| | 507 | | /// Builds the cube. |
| | 508 | | /// </summary> |
| | 509 | | /// <param name="cube">The cube.</param> |
| | 510 | | /// <param name="colorCount">The color count.</param> |
| | 511 | | private void BuildCube(out Box[] cube, ref int colorCount) |
| | 512 | | { |
| 10 | 513 | | cube = new Box[colorCount]; |
| 10 | 514 | | double[] vv = new double[colorCount]; |
| | 515 | |
|
| 5140 | 516 | | for (int i = 0; i < colorCount; i++) |
| | 517 | | { |
| 2560 | 518 | | cube[i] = new Box(); |
| | 519 | | } |
| | 520 | |
|
| 10 | 521 | | cube[0].R0 = cube[0].G0 = cube[0].B0 = 0; |
| 10 | 522 | | cube[0].R1 = cube[0].G1 = cube[0].B1 = IndexCount - 1; |
| 10 | 523 | | cube[0].Update(); |
| | 524 | |
|
| 10 | 525 | | int next = 0; |
| | 526 | |
|
| 4592 | 527 | | for (int i = 1; i < colorCount; i++) |
| | 528 | | { |
| 2296 | 529 | | if (this.Cut(cube[next], cube[i])) |
| | 530 | | { |
| 1271 | 531 | | vv[next] = cube[next].Volume > 1 ? this.Variance(cube[next]) : 0.0; |
| 1271 | 532 | | vv[i] = cube[i].Volume > 1 ? this.Variance(cube[i]) : 0.0; |
| | 533 | | } |
| | 534 | | else |
| | 535 | | { |
| 1025 | 536 | | vv[next] = 0.0; |
| 1025 | 537 | | i--; |
| | 538 | | } |
| | 539 | |
|
| 2296 | 540 | | next = 0; |
| | 541 | |
|
| 2296 | 542 | | double temp = vv[0]; |
| 460016 | 543 | | for (int k = 1; k <= i; k++) |
| | 544 | | { |
| 227712 | 545 | | if (vv[k] > temp) |
| | 546 | | { |
| 2223 | 547 | | temp = vv[k]; |
| 2223 | 548 | | next = k; |
| | 549 | | } |
| | 550 | | } |
| | 551 | |
|
| 2296 | 552 | | if (temp <= 0.0) |
| | 553 | | { |
| 10 | 554 | | colorCount = i + 1; |
| 10 | 555 | | break; |
| | 556 | | } |
| | 557 | | } |
| 0 | 558 | | } |
| | 559 | |
|
| | 560 | | /// <summary> |
| | 561 | | /// Generates the quantized result. |
| | 562 | | /// </summary> |
| | 563 | | /// <param name="image">The image.</param> |
| | 564 | | /// <param name="colorCount">The color count.</param> |
| | 565 | | /// <param name="cube">The cube.</param> |
| | 566 | | /// <returns>The result.</returns> |
| | 567 | | private ColorQuantizerResult GenerateResult(byte[] image, int colorCount, Box[] cube) |
| | 568 | | { |
| 10 | 569 | | var quantizedImage = new ColorQuantizerResult(image.Length / 4, colorCount); |
| | 570 | |
|
| 2582 | 571 | | for (int k = 0; k < colorCount; k++) |
| | 572 | | { |
| 1281 | 573 | | this.Mark(cube[k], (byte)k); |
| | 574 | |
|
| 1281 | 575 | | double weight = Volume(cube[k], this.vwt); |
| | 576 | |
|
| 1281 | 577 | | if (weight != 0) |
| | 578 | | { |
| 1281 | 579 | | quantizedImage.Palette[(k * 4) + 3] = 0xff; |
| 1281 | 580 | | quantizedImage.Palette[(k * 4) + 2] = (byte)(Volume(cube[k], this.vmr) / weight); |
| 1281 | 581 | | quantizedImage.Palette[(k * 4) + 1] = (byte)(Volume(cube[k], this.vmg) / weight); |
| 1281 | 582 | | quantizedImage.Palette[k * 4] = (byte)(Volume(cube[k], this.vmb) / weight); |
| | 583 | | } |
| | 584 | | else |
| | 585 | | { |
| 0 | 586 | | quantizedImage.Palette[(k * 4) + 3] = 0xff; |
| 0 | 587 | | quantizedImage.Palette[(k * 4) + 2] = 0; |
| 0 | 588 | | quantizedImage.Palette[(k * 4) + 1] = 0; |
| 0 | 589 | | quantizedImage.Palette[k * 4] = 0; |
| | 590 | | } |
| | 591 | | } |
| | 592 | |
|
| 134220310 | 593 | | for (int i = 0; i < image.Length / 4; i++) |
| | 594 | | { |
| 67110145 | 595 | | int r = image[(i * 4) + 2] >> (8 - IndexBits); |
| 67110145 | 596 | | int g = image[(i * 4) + 1] >> (8 - IndexBits); |
| 67110145 | 597 | | int b = image[i * 4] >> (8 - IndexBits); |
| | 598 | |
|
| 67110145 | 599 | | int ind = GetIndex(r + 1, g + 1, b + 1); |
| | 600 | |
|
| 67110145 | 601 | | quantizedImage.Bytes[i] = this.tag[ind]; |
| | 602 | | } |
| | 603 | |
|
| 10 | 604 | | return quantizedImage; |
| | 605 | | } |
| | 606 | | } |
| | 607 | | } |
| | 608 | | #endif |
| | 609 | |
|