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