| | 1 | | // <copyright file="WuAlphaColorQuantizer.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 | | namespace JeremyAnsel.ColorQuant |
| | 9 | | { |
| | 10 | | using System; |
| | 11 | | using System.Diagnostics.CodeAnalysis; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// A Wu's color quantizer with alpha channel. |
| | 15 | | /// </summary> |
| | 16 | | /// <remarks> |
| | 17 | | /// <para> |
| | 18 | | /// Based on C Implementation of Xiaolin Wu's Color Quantizer (v. 2) |
| | 19 | | /// (see Graphics Gems volume II, pages 126-133) |
| | 20 | | /// (<see href="http://www.ece.mcmaster.ca/~xwu/cq.c"/>). |
| | 21 | | /// </para> |
| | 22 | | /// <para> |
| | 23 | | /// Algorithm: Greedy orthogonal bipartition of RGB space for variance |
| | 24 | | /// minimization aided by inclusion-exclusion tricks. |
| | 25 | | /// For speed no nearest neighbor search is done. Slightly |
| | 26 | | /// better performance can be expected by more sophisticated |
| | 27 | | /// but more expensive versions. |
| | 28 | | /// </para> |
| | 29 | | /// </remarks> |
| | 30 | | [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Wu", Justification = " |
| | 31 | | public sealed class WuAlphaColorQuantizer : IColorQuantizer |
| | 32 | | { |
| | 33 | | /// <summary> |
| | 34 | | /// The index bits. |
| | 35 | | /// </summary> |
| | 36 | | private const int IndexBits = 6; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The index alpha bits. |
| | 40 | | /// </summary> |
| | 41 | | private const int IndexAlphaBits = 4; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The index count. |
| | 45 | | /// </summary> |
| | 46 | | private const int IndexCount = (1 << WuAlphaColorQuantizer.IndexBits) + 1; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// The index alpha count. |
| | 50 | | /// </summary> |
| | 51 | | private const int IndexAlphaCount = (1 << WuAlphaColorQuantizer.IndexAlphaBits) + 1; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The table length. |
| | 55 | | /// </summary> |
| | 56 | | private const int TableLength = WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexCount * WuAlphaCol |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Moment of <c>P(c)</c>. |
| | 60 | | /// </summary> |
| 3 | 61 | | private readonly long[] vwt = new long[WuAlphaColorQuantizer.TableLength]; |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Moment of <c>r*P(c)</c>. |
| | 65 | | /// </summary> |
| 3 | 66 | | private readonly long[] vmr = new long[WuAlphaColorQuantizer.TableLength]; |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Moment of <c>g*P(c)</c>. |
| | 70 | | /// </summary> |
| 3 | 71 | | private readonly long[] vmg = new long[WuAlphaColorQuantizer.TableLength]; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Moment of <c>b*P(c)</c>. |
| | 75 | | /// </summary> |
| 3 | 76 | | private readonly long[] vmb = new long[WuAlphaColorQuantizer.TableLength]; |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Moment of <c>a*P(c)</c>. |
| | 80 | | /// </summary> |
| 3 | 81 | | private readonly long[] vma = new long[WuAlphaColorQuantizer.TableLength]; |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Moment of <c>c^2*P(c)</c>. |
| | 85 | | /// </summary> |
| 3 | 86 | | private readonly double[] m2 = new double[WuAlphaColorQuantizer.TableLength]; |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Color space tag. |
| | 90 | | /// </summary> |
| 3 | 91 | | private readonly byte[] tag = new byte[WuAlphaColorQuantizer.TableLength]; |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Quantizes an image. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="image">The image (ARGB).</param> |
| | 97 | | /// <returns>The result.</returns> |
| | 98 | | public ColorQuantizerResult Quantize(byte[] image) |
| | 99 | | { |
| 27 | 100 | | return this.Quantize(image, 256); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Quantizes an image. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="image">The image (ARGB).</param> |
| | 107 | | /// <param name="colorCount">The color count.</param> |
| | 108 | | /// <returns>The result.</returns> |
| | 109 | | public ColorQuantizerResult Quantize(byte[] image, int colorCount) |
| | 110 | | { |
| 36 | 111 | | if (image == null) |
| | 112 | | { |
| 6 | 113 | | throw new ArgumentNullException(nameof(image)); |
| | 114 | | } |
| | 115 | |
|
| 30 | 116 | | if (colorCount < 1 || colorCount > 256) |
| | 117 | | { |
| 6 | 118 | | throw new ArgumentOutOfRangeException(nameof(colorCount)); |
| | 119 | | } |
| | 120 | |
|
| 24 | 121 | | this.Clear(); |
| | 122 | |
|
| 24 | 123 | | this.Build3DHistogram(image); |
| 24 | 124 | | this.Get3DMoments(); |
| | 125 | |
|
| | 126 | | Box[] cube; |
| 24 | 127 | | this.BuildCube(out cube, ref colorCount); |
| | 128 | |
|
| 24 | 129 | | return this.GenerateResult(image, colorCount, cube); |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <summary> |
| | 133 | | /// Gets an index. |
| | 134 | | /// </summary> |
| | 135 | | /// <param name="r">The red value.</param> |
| | 136 | | /// <param name="g">The green value.</param> |
| | 137 | | /// <param name="b">The blue value.</param> |
| | 138 | | /// <param name="a">The alpha value.</param> |
| | 139 | | /// <returns>The index.</returns> |
| | 140 | | private static int GetIndex(int r, int g, int b, int a) |
| | 141 | | { |
| 314579340 | 142 | | return (r << ((WuAlphaColorQuantizer.IndexBits * 2) + WuAlphaColorQuantizer.IndexAlphaBits)) |
| 314579340 | 143 | | + (r << (WuAlphaColorQuantizer.IndexBits + WuAlphaColorQuantizer.IndexAlphaBits + 1)) |
| 314579340 | 144 | | + (g << (WuAlphaColorQuantizer.IndexBits + WuAlphaColorQuantizer.IndexAlphaBits)) |
| 314579340 | 145 | | + (r << (WuAlphaColorQuantizer.IndexBits * 2)) |
| 314579340 | 146 | | + (r << (WuAlphaColorQuantizer.IndexBits + 1)) |
| 314579340 | 147 | | + (g << WuAlphaColorQuantizer.IndexBits) |
| 314579340 | 148 | | + ((r + g + b) << WuAlphaColorQuantizer.IndexAlphaBits) |
| 314579340 | 149 | | + r + g + b + a; |
| | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Computes sum over a box of any given statistic. |
| | 154 | | /// </summary> |
| | 155 | | /// <param name="cube">The cube.</param> |
| | 156 | | /// <param name="moment">The moment.</param> |
| | 157 | | /// <returns>The result.</returns> |
| | 158 | | private static double Volume(Box cube, long[] moment) |
| | 159 | | { |
| 35535 | 160 | | return moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, cube.A1)] |
| 35535 | 161 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, cube.A0)] |
| 35535 | 162 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A1)] |
| 35535 | 163 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A0)] |
| 35535 | 164 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A1)] |
| 35535 | 165 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A0)] |
| 35535 | 166 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A1)] |
| 35535 | 167 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A0)] |
| 35535 | 168 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A1)] |
| 35535 | 169 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A0)] |
| 35535 | 170 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A1)] |
| 35535 | 171 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A0)] |
| 35535 | 172 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A1)] |
| 35535 | 173 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A0)] |
| 35535 | 174 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A1)] |
| 35535 | 175 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary> |
| | 179 | | /// Computes part of Volume(cube, moment) that doesn't depend on r1, g1, or b1 (depending on direction). |
| | 180 | | /// </summary> |
| | 181 | | /// <param name="cube">The cube.</param> |
| | 182 | | /// <param name="direction">The direction.</param> |
| | 183 | | /// <param name="moment">The moment.</param> |
| | 184 | | /// <returns>The result.</returns> |
| | 185 | | private static long Bottom(Box cube, int direction, long[] moment) |
| | 186 | | { |
| | 187 | | switch (direction) |
| | 188 | | { |
| | 189 | | // Red |
| | 190 | | case 3: |
| 11940 | 191 | | return -moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A1)] |
| 11940 | 192 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A0)] |
| 11940 | 193 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A1)] |
| 11940 | 194 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A0)] |
| 11940 | 195 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A1)] |
| 11940 | 196 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A0)] |
| 11940 | 197 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A1)] |
| 11940 | 198 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 199 | |
|
| | 200 | | // Green |
| | 201 | | case 2: |
| 11940 | 202 | | return -moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A1)] |
| 11940 | 203 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A0)] |
| 11940 | 204 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A1)] |
| 11940 | 205 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A0)] |
| 11940 | 206 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A1)] |
| 11940 | 207 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A0)] |
| 11940 | 208 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A1)] |
| 11940 | 209 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 210 | |
|
| | 211 | | // Blue |
| | 212 | | case 1: |
| 11940 | 213 | | return -moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A1)] |
| 11940 | 214 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A0)] |
| 11940 | 215 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A1)] |
| 11940 | 216 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A0)] |
| 11940 | 217 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A1)] |
| 11940 | 218 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A0)] |
| 11940 | 219 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A1)] |
| 11940 | 220 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 221 | |
|
| | 222 | | // Alpha |
| | 223 | | case 0: |
| 11940 | 224 | | return -moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, cube.A0)] |
| 11940 | 225 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A0)] |
| 11940 | 226 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A0)] |
| 11940 | 227 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A0)] |
| 11940 | 228 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A0)] |
| 11940 | 229 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A0)] |
| 11940 | 230 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A0)] |
| 11940 | 231 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 232 | |
|
| | 233 | | default: |
| 0 | 234 | | throw new ArgumentOutOfRangeException(nameof(direction)); |
| | 235 | | } |
| | 236 | | } |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Computes remainder of Volume(cube, moment), substituting position for r1, g1, or b1 (depending on direction) |
| | 240 | | /// </summary> |
| | 241 | | /// <param name="cube">The cube.</param> |
| | 242 | | /// <param name="direction">The direction.</param> |
| | 243 | | /// <param name="position">The position.</param> |
| | 244 | | /// <param name="moment">The moment.</param> |
| | 245 | | /// <returns>The result.</returns> |
| | 246 | | private static long Top(Box cube, int direction, int position, long[] moment) |
| | 247 | | { |
| | 248 | | switch (direction) |
| | 249 | | { |
| | 250 | | // Red |
| | 251 | | case 3: |
| 346140 | 252 | | return moment[WuAlphaColorQuantizer.GetIndex(position, cube.G1, cube.B1, cube.A1)] |
| 346140 | 253 | | - moment[WuAlphaColorQuantizer.GetIndex(position, cube.G1, cube.B1, cube.A0)] |
| 346140 | 254 | | - moment[WuAlphaColorQuantizer.GetIndex(position, cube.G1, cube.B0, cube.A1)] |
| 346140 | 255 | | + moment[WuAlphaColorQuantizer.GetIndex(position, cube.G1, cube.B0, cube.A0)] |
| 346140 | 256 | | - moment[WuAlphaColorQuantizer.GetIndex(position, cube.G0, cube.B1, cube.A1)] |
| 346140 | 257 | | + moment[WuAlphaColorQuantizer.GetIndex(position, cube.G0, cube.B1, cube.A0)] |
| 346140 | 258 | | + moment[WuAlphaColorQuantizer.GetIndex(position, cube.G0, cube.B0, cube.A1)] |
| 346140 | 259 | | - moment[WuAlphaColorQuantizer.GetIndex(position, cube.G0, cube.B0, cube.A0)]; |
| | 260 | |
|
| | 261 | | // Green |
| | 262 | | case 2: |
| 469980 | 263 | | return moment[WuAlphaColorQuantizer.GetIndex(cube.R1, position, cube.B1, cube.A1)] |
| 469980 | 264 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, position, cube.B1, cube.A0)] |
| 469980 | 265 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, position, cube.B0, cube.A1)] |
| 469980 | 266 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, position, cube.B0, cube.A0)] |
| 469980 | 267 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, position, cube.B1, cube.A1)] |
| 469980 | 268 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, position, cube.B1, cube.A0)] |
| 469980 | 269 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, position, cube.B0, cube.A1)] |
| 469980 | 270 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, position, cube.B0, cube.A0)]; |
| | 271 | |
|
| | 272 | | // Blue |
| | 273 | | case 1: |
| 487260 | 274 | | return moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, position, cube.A1)] |
| 487260 | 275 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, position, cube.A0)] |
| 487260 | 276 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, position, cube.A1)] |
| 487260 | 277 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, position, cube.A0)] |
| 487260 | 278 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, position, cube.A1)] |
| 487260 | 279 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, position, cube.A0)] |
| 487260 | 280 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, position, cube.A1)] |
| 487260 | 281 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, position, cube.A0)]; |
| | 282 | |
|
| | 283 | | // Alpha |
| | 284 | | case 0: |
| 144060 | 285 | | return moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, position)] |
| 144060 | 286 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, position)] |
| 144060 | 287 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, position)] |
| 144060 | 288 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, position)] |
| 144060 | 289 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, position)] |
| 144060 | 290 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, position)] |
| 144060 | 291 | | + moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, position)] |
| 144060 | 292 | | - moment[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, position)]; |
| | 293 | |
|
| | 294 | | default: |
| 0 | 295 | | throw new ArgumentOutOfRangeException(nameof(direction)); |
| | 296 | | } |
| | 297 | | } |
| | 298 | |
|
| | 299 | | /// <summary> |
| | 300 | | /// Clears the tables. |
| | 301 | | /// </summary> |
| | 302 | | private void Clear() |
| | 303 | | { |
| 24 | 304 | | Array.Clear(this.vwt, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 305 | | Array.Clear(this.vmr, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 306 | | Array.Clear(this.vmg, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 307 | | Array.Clear(this.vmb, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 308 | | Array.Clear(this.vma, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 309 | | Array.Clear(this.m2, 0, WuAlphaColorQuantizer.TableLength); |
| | 310 | |
|
| 24 | 311 | | Array.Clear(this.tag, 0, WuAlphaColorQuantizer.TableLength); |
| 24 | 312 | | } |
| | 313 | |
|
| | 314 | | /// <summary> |
| | 315 | | /// Builds a 3-D color histogram of <c>counts, r/g/b, c^2</c>. |
| | 316 | | /// </summary> |
| | 317 | | /// <param name="image">The image.</param> |
| | 318 | | private void Build3DHistogram(byte[] image) |
| | 319 | | { |
| 9276 | 320 | | for (int i = 0; i < image.Length; i += 4) |
| | 321 | | { |
| 4614 | 322 | | int a = image[i + 3]; |
| 4614 | 323 | | int r = image[i + 2]; |
| 4614 | 324 | | int g = image[i + 1]; |
| 4614 | 325 | | int b = image[i]; |
| | 326 | |
|
| 4614 | 327 | | int inr = r >> (8 - WuAlphaColorQuantizer.IndexBits); |
| 4614 | 328 | | int ing = g >> (8 - WuAlphaColorQuantizer.IndexBits); |
| 4614 | 329 | | int inb = b >> (8 - WuAlphaColorQuantizer.IndexBits); |
| 4614 | 330 | | int ina = a >> (8 - WuAlphaColorQuantizer.IndexAlphaBits); |
| | 331 | |
|
| 4614 | 332 | | int ind = WuAlphaColorQuantizer.GetIndex(inr + 1, ing + 1, inb + 1, ina + 1); |
| | 333 | |
|
| 4614 | 334 | | this.vwt[ind]++; |
| 4614 | 335 | | this.vmr[ind] += r; |
| 4614 | 336 | | this.vmg[ind] += g; |
| 4614 | 337 | | this.vmb[ind] += b; |
| 4614 | 338 | | this.vma[ind] += a; |
| 4614 | 339 | | this.m2[ind] += (r * r) + (g * g) + (b * b) + (a * a); |
| | 340 | | } |
| 24 | 341 | | } |
| | 342 | |
|
| | 343 | | /// <summary> |
| | 344 | | /// Converts the histogram into moments so that we can rapidly calculate |
| | 345 | | /// the sums of the above quantities over any desired box. |
| | 346 | | /// </summary> |
| | 347 | | private void Get3DMoments() |
| | 348 | | { |
| 24 | 349 | | long[] volume = new long[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 350 | | long[] volumeR = new long[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 351 | | long[] volumeG = new long[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 352 | | long[] volumeB = new long[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 353 | | long[] volumeA = new long[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 354 | | double[] volume2 = new double[WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount]; |
| | 355 | |
|
| 24 | 356 | | long[] area = new long[WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 357 | | long[] areaR = new long[WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 358 | | long[] areaG = new long[WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 359 | | long[] areaB = new long[WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 360 | | long[] areaA = new long[WuAlphaColorQuantizer.IndexAlphaCount]; |
| 24 | 361 | | double[] area2 = new double[WuAlphaColorQuantizer.IndexAlphaCount]; |
| | 362 | |
|
| 3120 | 363 | | for (int r = 1; r < WuAlphaColorQuantizer.IndexCount; r++) |
| | 364 | | { |
| 1536 | 365 | | Array.Clear(volume, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| 1536 | 366 | | Array.Clear(volumeR, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| 1536 | 367 | | Array.Clear(volumeG, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| 1536 | 368 | | Array.Clear(volumeB, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| 1536 | 369 | | Array.Clear(volumeA, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| 1536 | 370 | | Array.Clear(volume2, 0, WuAlphaColorQuantizer.IndexCount * WuAlphaColorQuantizer.IndexAlphaCount); |
| | 371 | |
|
| 199680 | 372 | | for (int g = 1; g < WuAlphaColorQuantizer.IndexCount; g++) |
| | 373 | | { |
| 98304 | 374 | | Array.Clear(area, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| 98304 | 375 | | Array.Clear(areaR, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| 98304 | 376 | | Array.Clear(areaG, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| 98304 | 377 | | Array.Clear(areaB, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| 98304 | 378 | | Array.Clear(areaA, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| 98304 | 379 | | Array.Clear(area2, 0, WuAlphaColorQuantizer.IndexAlphaCount); |
| | 380 | |
|
| 12779520 | 381 | | for (int b = 1; b < WuAlphaColorQuantizer.IndexCount; b++) |
| | 382 | | { |
| 6291456 | 383 | | long line = 0; |
| 6291456 | 384 | | long lineR = 0; |
| 6291456 | 385 | | long lineG = 0; |
| 6291456 | 386 | | long lineB = 0; |
| 6291456 | 387 | | long lineA = 0; |
| 6291456 | 388 | | double line2 = 0; |
| | 389 | |
|
| 213909504 | 390 | | for (int a = 1; a < WuAlphaColorQuantizer.IndexAlphaCount; a++) |
| | 391 | | { |
| 100663296 | 392 | | int ind1 = WuAlphaColorQuantizer.GetIndex(r, g, b, a); |
| | 393 | |
|
| 100663296 | 394 | | line += this.vwt[ind1]; |
| 100663296 | 395 | | lineR += this.vmr[ind1]; |
| 100663296 | 396 | | lineG += this.vmg[ind1]; |
| 100663296 | 397 | | lineB += this.vmb[ind1]; |
| 100663296 | 398 | | lineA += this.vma[ind1]; |
| 100663296 | 399 | | line2 += this.m2[ind1]; |
| | 400 | |
|
| 100663296 | 401 | | area[a] += line; |
| 100663296 | 402 | | areaR[a] += lineR; |
| 100663296 | 403 | | areaG[a] += lineG; |
| 100663296 | 404 | | areaB[a] += lineB; |
| 100663296 | 405 | | areaA[a] += lineA; |
| 100663296 | 406 | | area2[a] += line2; |
| | 407 | |
|
| 100663296 | 408 | | int inv = (b * WuAlphaColorQuantizer.IndexAlphaCount) + a; |
| | 409 | |
|
| 100663296 | 410 | | volume[inv] += area[a]; |
| 100663296 | 411 | | volumeR[inv] += areaR[a]; |
| 100663296 | 412 | | volumeG[inv] += areaG[a]; |
| 100663296 | 413 | | volumeB[inv] += areaB[a]; |
| 100663296 | 414 | | volumeA[inv] += areaA[a]; |
| 100663296 | 415 | | volume2[inv] += area2[a]; |
| | 416 | |
|
| 100663296 | 417 | | int ind2 = ind1 - WuAlphaColorQuantizer.GetIndex(1, 0, 0, 0); |
| | 418 | |
|
| 100663296 | 419 | | this.vwt[ind1] = this.vwt[ind2] + volume[inv]; |
| 100663296 | 420 | | this.vmr[ind1] = this.vmr[ind2] + volumeR[inv]; |
| 100663296 | 421 | | this.vmg[ind1] = this.vmg[ind2] + volumeG[inv]; |
| 100663296 | 422 | | this.vmb[ind1] = this.vmb[ind2] + volumeB[inv]; |
| 100663296 | 423 | | this.vma[ind1] = this.vma[ind2] + volumeA[inv]; |
| 100663296 | 424 | | this.m2[ind1] = this.m2[ind2] + volume2[inv]; |
| | 425 | | } |
| | 426 | | } |
| | 427 | | } |
| | 428 | | } |
| 24 | 429 | | } |
| | 430 | |
|
| | 431 | | /// <summary> |
| | 432 | | /// Computes the weighted variance of a box. |
| | 433 | | /// </summary> |
| | 434 | | /// <param name="cube">The cube.</param> |
| | 435 | | /// <returns>The result.</returns> |
| | 436 | | private double Variance(Box cube) |
| | 437 | | { |
| 3129 | 438 | | double dr = WuAlphaColorQuantizer.Volume(cube, this.vmr); |
| 3129 | 439 | | double dg = WuAlphaColorQuantizer.Volume(cube, this.vmg); |
| 3129 | 440 | | double db = WuAlphaColorQuantizer.Volume(cube, this.vmb); |
| 3129 | 441 | | double da = WuAlphaColorQuantizer.Volume(cube, this.vma); |
| | 442 | |
|
| 3129 | 443 | | double xx = |
| 3129 | 444 | | this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, cube.A1)] |
| 3129 | 445 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B1, cube.A0)] |
| 3129 | 446 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A1)] |
| 3129 | 447 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G1, cube.B0, cube.A0)] |
| 3129 | 448 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A1)] |
| 3129 | 449 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B1, cube.A0)] |
| 3129 | 450 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A1)] |
| 3129 | 451 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R1, cube.G0, cube.B0, cube.A0)] |
| 3129 | 452 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A1)] |
| 3129 | 453 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B1, cube.A0)] |
| 3129 | 454 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A1)] |
| 3129 | 455 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G1, cube.B0, cube.A0)] |
| 3129 | 456 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A1)] |
| 3129 | 457 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B1, cube.A0)] |
| 3129 | 458 | | - this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A1)] |
| 3129 | 459 | | + this.m2[WuAlphaColorQuantizer.GetIndex(cube.R0, cube.G0, cube.B0, cube.A0)]; |
| | 460 | |
|
| 3129 | 461 | | return xx - (((dr * dr) + (dg * dg) + (db * db) + (da * da)) / WuAlphaColorQuantizer.Volume(cube, this.vwt)) |
| | 462 | | } |
| | 463 | |
|
| | 464 | | /// <summary> |
| | 465 | | /// We want to minimize the sum of the variances of two sub-boxes. |
| | 466 | | /// The sum(c^2) terms can be ignored since their sum over both sub-boxes |
| | 467 | | /// is the same (the sum for the whole box) no matter where we split. |
| | 468 | | /// The remaining terms have a minus sign in the variance formula, |
| | 469 | | /// so we drop the minus sign and maximize the sum of the two terms. |
| | 470 | | /// </summary> |
| | 471 | | /// <param name="cube">The cube.</param> |
| | 472 | | /// <param name="direction">The direction.</param> |
| | 473 | | /// <param name="first">The first position.</param> |
| | 474 | | /// <param name="last">The last position.</param> |
| | 475 | | /// <param name="cut">The cutting point.</param> |
| | 476 | | /// <param name="wholeR">The whole red.</param> |
| | 477 | | /// <param name="wholeG">The whole green.</param> |
| | 478 | | /// <param name="wholeB">The whole blue.</param> |
| | 479 | | /// <param name="wholeA">The whole alpha.</param> |
| | 480 | | /// <param name="wholeW">The whole weight.</param> |
| | 481 | | /// <returns>The result.</returns> |
| | 482 | | private double Maximize(Box cube, int direction, int first, int last, out int cut, double wholeR, double wholeG, |
| | 483 | | { |
| 9552 | 484 | | long baseR = WuAlphaColorQuantizer.Bottom(cube, direction, this.vmr); |
| 9552 | 485 | | long baseG = WuAlphaColorQuantizer.Bottom(cube, direction, this.vmg); |
| 9552 | 486 | | long baseB = WuAlphaColorQuantizer.Bottom(cube, direction, this.vmb); |
| 9552 | 487 | | long baseA = WuAlphaColorQuantizer.Bottom(cube, direction, this.vma); |
| 9552 | 488 | | long baseW = WuAlphaColorQuantizer.Bottom(cube, direction, this.vwt); |
| | 489 | |
|
| 9552 | 490 | | double max = 0.0; |
| 9552 | 491 | | cut = -1; |
| | 492 | |
|
| 598080 | 493 | | for (int i = first; i < last; i++) |
| | 494 | | { |
| 289488 | 495 | | double halfR = baseR + WuAlphaColorQuantizer.Top(cube, direction, i, this.vmr); |
| 289488 | 496 | | double halfG = baseG + WuAlphaColorQuantizer.Top(cube, direction, i, this.vmg); |
| 289488 | 497 | | double halfB = baseB + WuAlphaColorQuantizer.Top(cube, direction, i, this.vmb); |
| 289488 | 498 | | double halfA = baseA + WuAlphaColorQuantizer.Top(cube, direction, i, this.vma); |
| 289488 | 499 | | double halfW = baseW + WuAlphaColorQuantizer.Top(cube, direction, i, this.vwt); |
| | 500 | |
|
| 289488 | 501 | | if (halfW == 0) |
| | 502 | | { |
| | 503 | | continue; |
| | 504 | | } |
| | 505 | |
|
| 221991 | 506 | | double temp = ((halfR * halfR) + (halfG * halfG) + (halfB * halfB) + (halfA * halfA)) / halfW; |
| | 507 | |
|
| 221991 | 508 | | halfR = wholeR - halfR; |
| 221991 | 509 | | halfG = wholeG - halfG; |
| 221991 | 510 | | halfB = wholeB - halfB; |
| 221991 | 511 | | halfA = wholeA - halfA; |
| 221991 | 512 | | halfW = wholeW - halfW; |
| | 513 | |
|
| 221991 | 514 | | if (halfW == 0) |
| | 515 | | { |
| | 516 | | continue; |
| | 517 | | } |
| | 518 | |
|
| 25509 | 519 | | temp += ((halfR * halfR) + (halfG * halfG) + (halfB * halfB) + (halfA * halfA)) / halfW; |
| | 520 | |
|
| 25509 | 521 | | if (temp > max) |
| | 522 | | { |
| 5058 | 523 | | max = temp; |
| 5058 | 524 | | cut = i; |
| | 525 | | } |
| | 526 | | } |
| | 527 | |
|
| 9552 | 528 | | return max; |
| | 529 | | } |
| | 530 | |
|
| | 531 | | /// <summary> |
| | 532 | | /// Cuts a box. |
| | 533 | | /// </summary> |
| | 534 | | /// <param name="set1">The first set.</param> |
| | 535 | | /// <param name="set2">The second set.</param> |
| | 536 | | /// <returns>Returns a value indicating whether the box has been split.</returns> |
| | 537 | | private bool Cut(Box set1, Box set2) |
| | 538 | | { |
| 2388 | 539 | | double wholeR = WuAlphaColorQuantizer.Volume(set1, this.vmr); |
| 2388 | 540 | | double wholeG = WuAlphaColorQuantizer.Volume(set1, this.vmg); |
| 2388 | 541 | | double wholeB = WuAlphaColorQuantizer.Volume(set1, this.vmb); |
| 2388 | 542 | | double wholeA = WuAlphaColorQuantizer.Volume(set1, this.vma); |
| 2388 | 543 | | double wholeW = WuAlphaColorQuantizer.Volume(set1, this.vwt); |
| | 544 | |
|
| | 545 | | int cutr; |
| | 546 | | int cutg; |
| | 547 | | int cutb; |
| | 548 | | int cuta; |
| | 549 | |
|
| 2388 | 550 | | double maxr = this.Maximize(set1, 3, set1.R0 + 1, set1.R1, out cutr, wholeR, wholeG, wholeB, wholeA, wholeW) |
| 2388 | 551 | | double maxg = this.Maximize(set1, 2, set1.G0 + 1, set1.G1, out cutg, wholeR, wholeG, wholeB, wholeA, wholeW) |
| 2388 | 552 | | double maxb = this.Maximize(set1, 1, set1.B0 + 1, set1.B1, out cutb, wholeR, wholeG, wholeB, wholeA, wholeW) |
| 2388 | 553 | | double maxa = this.Maximize(set1, 0, set1.A0 + 1, set1.A1, out cuta, wholeR, wholeG, wholeB, wholeA, wholeW) |
| | 554 | |
|
| | 555 | | int dir; |
| | 556 | |
|
| 2388 | 557 | | if ((maxr >= maxg) && (maxr >= maxb) && (maxr >= maxa)) |
| | 558 | | { |
| 1251 | 559 | | dir = 3; |
| | 560 | |
|
| 1251 | 561 | | if (cutr < 0) |
| | 562 | | { |
| 822 | 563 | | return false; |
| | 564 | | } |
| | 565 | | } |
| 1137 | 566 | | else if ((maxg >= maxr) && (maxg >= maxb) && (maxg >= maxa)) |
| | 567 | | { |
| 291 | 568 | | dir = 2; |
| | 569 | | } |
| 846 | 570 | | else if ((maxb >= maxr) && (maxb >= maxg) && (maxb >= maxa)) |
| | 571 | | { |
| 393 | 572 | | dir = 1; |
| | 573 | | } |
| | 574 | | else |
| | 575 | | { |
| 453 | 576 | | dir = 0; |
| | 577 | | } |
| | 578 | |
|
| 1566 | 579 | | set2.R1 = set1.R1; |
| 1566 | 580 | | set2.G1 = set1.G1; |
| 1566 | 581 | | set2.B1 = set1.B1; |
| 1566 | 582 | | set2.A1 = set1.A1; |
| | 583 | |
|
| | 584 | | switch (dir) |
| | 585 | | { |
| | 586 | | // Red |
| | 587 | | case 3: |
| 429 | 588 | | set2.R0 = set1.R1 = cutr; |
| 429 | 589 | | set2.G0 = set1.G0; |
| 429 | 590 | | set2.B0 = set1.B0; |
| 429 | 591 | | set2.A0 = set1.A0; |
| 429 | 592 | | break; |
| | 593 | |
|
| | 594 | | // Green |
| | 595 | | case 2: |
| 291 | 596 | | set2.G0 = set1.G1 = cutg; |
| 291 | 597 | | set2.R0 = set1.R0; |
| 291 | 598 | | set2.B0 = set1.B0; |
| 291 | 599 | | set2.A0 = set1.A0; |
| 291 | 600 | | break; |
| | 601 | |
|
| | 602 | | // Blue |
| | 603 | | case 1: |
| 393 | 604 | | set2.B0 = set1.B1 = cutb; |
| 393 | 605 | | set2.R0 = set1.R0; |
| 393 | 606 | | set2.G0 = set1.G0; |
| 393 | 607 | | set2.A0 = set1.A0; |
| 393 | 608 | | break; |
| | 609 | |
|
| | 610 | | // Alpha |
| | 611 | | case 0: |
| 453 | 612 | | set2.A0 = set1.A1 = cuta; |
| 453 | 613 | | set2.R0 = set1.R0; |
| 453 | 614 | | set2.G0 = set1.G0; |
| 453 | 615 | | set2.B0 = set1.B0; |
| | 616 | | break; |
| | 617 | | } |
| | 618 | |
|
| 1566 | 619 | | set1.Volume = (set1.R1 - set1.R0) * (set1.G1 - set1.G0) * (set1.B1 - set1.B0) * (set1.A1 - set1.A0); |
| 1566 | 620 | | set2.Volume = (set2.R1 - set2.R0) * (set2.G1 - set2.G0) * (set2.B1 - set2.B0) * (set2.A1 - set2.A0); |
| | 621 | |
|
| 1566 | 622 | | return true; |
| | 623 | | } |
| | 624 | |
|
| | 625 | | /// <summary> |
| | 626 | | /// Marks a color space tag. |
| | 627 | | /// </summary> |
| | 628 | | /// <param name="cube">The cube.</param> |
| | 629 | | /// <param name="label">A label.</param> |
| | 630 | | private void Mark(Box cube, byte label) |
| | 631 | | { |
| 84588 | 632 | | for (int r = cube.R0 + 1; r <= cube.R1; r++) |
| | 633 | | { |
| 2563584 | 634 | | for (int g = cube.G0 + 1; g <= cube.G1; g++) |
| | 635 | | { |
| 43376640 | 636 | | for (int b = cube.B0 + 1; b <= cube.B1; b++) |
| | 637 | | { |
| 242221056 | 638 | | for (int a = cube.A0 + 1; a <= cube.A1; a++) |
| | 639 | | { |
| 100663296 | 640 | | this.tag[WuAlphaColorQuantizer.GetIndex(r, g, b, a)] = label; |
| | 641 | | } |
| | 642 | | } |
| | 643 | | } |
| | 644 | | } |
| 1590 | 645 | | } |
| | 646 | |
|
| | 647 | | /// <summary> |
| | 648 | | /// Builds the cube. |
| | 649 | | /// </summary> |
| | 650 | | /// <param name="cube">The cube.</param> |
| | 651 | | /// <param name="colorCount">The color count.</param> |
| | 652 | | private void BuildCube(out Box[] cube, ref int colorCount) |
| | 653 | | { |
| 24 | 654 | | cube = new Box[colorCount]; |
| 24 | 655 | | double[] vv = new double[colorCount]; |
| | 656 | |
|
| 12336 | 657 | | for (int i = 0; i < colorCount; i++) |
| | 658 | | { |
| 6144 | 659 | | cube[i] = new Box(); |
| | 660 | | } |
| | 661 | |
|
| 24 | 662 | | cube[0].R0 = cube[0].G0 = cube[0].B0 = cube[0].A0 = 0; |
| 24 | 663 | | cube[0].R1 = cube[0].G1 = cube[0].B1 = WuAlphaColorQuantizer.IndexCount - 1; |
| 24 | 664 | | cube[0].A1 = WuAlphaColorQuantizer.IndexAlphaCount - 1; |
| | 665 | |
|
| 24 | 666 | | int next = 0; |
| | 667 | |
|
| 4776 | 668 | | for (int i = 1; i < colorCount; i++) |
| | 669 | | { |
| 2388 | 670 | | if (this.Cut(cube[next], cube[i])) |
| | 671 | | { |
| 1566 | 672 | | vv[next] = cube[next].Volume > 1 ? this.Variance(cube[next]) : 0.0; |
| 1566 | 673 | | vv[i] = cube[i].Volume > 1 ? this.Variance(cube[i]) : 0.0; |
| | 674 | | } |
| | 675 | | else |
| | 676 | | { |
| 822 | 677 | | vv[next] = 0.0; |
| 822 | 678 | | i--; |
| | 679 | | } |
| | 680 | |
|
| 2388 | 681 | | next = 0; |
| | 682 | |
|
| 2388 | 683 | | double temp = vv[0]; |
| 347928 | 684 | | for (int k = 1; k <= i; k++) |
| | 685 | | { |
| 171576 | 686 | | if (vv[k] > temp) |
| | 687 | | { |
| 2259 | 688 | | temp = vv[k]; |
| 2259 | 689 | | next = k; |
| | 690 | | } |
| | 691 | | } |
| | 692 | |
|
| 2388 | 693 | | if (temp <= 0.0) |
| | 694 | | { |
| 24 | 695 | | colorCount = i + 1; |
| 24 | 696 | | break; |
| | 697 | | } |
| | 698 | | } |
| 0 | 699 | | } |
| | 700 | |
|
| | 701 | | /// <summary> |
| | 702 | | /// Generates the quantized result. |
| | 703 | | /// </summary> |
| | 704 | | /// <param name="image">The image.</param> |
| | 705 | | /// <param name="colorCount">The color count.</param> |
| | 706 | | /// <param name="cube">The cube.</param> |
| | 707 | | /// <returns>The result.</returns> |
| | 708 | | private ColorQuantizerResult GenerateResult(byte[] image, int colorCount, Box[] cube) |
| | 709 | | { |
| 24 | 710 | | var quantizedImage = new ColorQuantizerResult(image.Length / 4, colorCount); |
| | 711 | |
|
| 3228 | 712 | | for (int k = 0; k < colorCount; k++) |
| | 713 | | { |
| 1590 | 714 | | this.Mark(cube[k], (byte)k); |
| | 715 | |
|
| 1590 | 716 | | double weight = WuAlphaColorQuantizer.Volume(cube[k], this.vwt); |
| | 717 | |
|
| 1590 | 718 | | if (weight != 0) |
| | 719 | | { |
| 1590 | 720 | | quantizedImage.Palette[(k * 4) + 3] = (byte)(WuAlphaColorQuantizer.Volume(cube[k], this.vma) / weigh |
| 1590 | 721 | | quantizedImage.Palette[(k * 4) + 2] = (byte)(WuAlphaColorQuantizer.Volume(cube[k], this.vmr) / weigh |
| 1590 | 722 | | quantizedImage.Palette[(k * 4) + 1] = (byte)(WuAlphaColorQuantizer.Volume(cube[k], this.vmg) / weigh |
| 1590 | 723 | | quantizedImage.Palette[k * 4] = (byte)(WuAlphaColorQuantizer.Volume(cube[k], this.vmb) / weight); |
| | 724 | | } |
| | 725 | | else |
| | 726 | | { |
| 0 | 727 | | quantizedImage.Palette[(k * 4) + 3] = 0xff; |
| 0 | 728 | | quantizedImage.Palette[(k * 4) + 2] = 0; |
| 0 | 729 | | quantizedImage.Palette[(k * 4) + 1] = 0; |
| 0 | 730 | | quantizedImage.Palette[k * 4] = 0; |
| | 731 | | } |
| | 732 | | } |
| | 733 | |
|
| 9276 | 734 | | for (int i = 0; i < image.Length / 4; i++) |
| | 735 | | { |
| 4614 | 736 | | int a = image[(i * 4) + 3] >> (8 - WuAlphaColorQuantizer.IndexAlphaBits); |
| 4614 | 737 | | int r = image[(i * 4) + 2] >> (8 - WuAlphaColorQuantizer.IndexBits); |
| 4614 | 738 | | int g = image[(i * 4) + 1] >> (8 - WuAlphaColorQuantizer.IndexBits); |
| 4614 | 739 | | int b = image[i * 4] >> (8 - WuAlphaColorQuantizer.IndexBits); |
| | 740 | |
|
| 4614 | 741 | | int ind = WuAlphaColorQuantizer.GetIndex(r + 1, g + 1, b + 1, a + 1); |
| | 742 | |
|
| 4614 | 743 | | quantizedImage.Bytes[i] = this.tag[ind]; |
| | 744 | | } |
| | 745 | |
|
| 24 | 746 | | return quantizedImage; |
| | 747 | | } |
| | 748 | | } |
| | 749 | | } |
| | 750 | |
|