PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/DXGifRenderWP8/quantize.cpp

https://github.com/hippiehunter/Baconography
C++ | 309 lines | 216 code | 37 blank | 56 comment | 36 complexity | ccdb34c972d363a9b51a58016da3f29a MD5 | raw file
  1. /*****************************************************************************
  2. quantize.c - quantize a high resolution image into lower one
  3. Based on: "Color Image Quantization for frame buffer Display", by
  4. Paul Heckbert SIGGRAPH 1982 page 297-307.
  5. This doesn't really belong in the core library, was undocumented,
  6. and was removed in 4.2. Then it turned out some client apps were
  7. actually using it, so it was restored in 5.0.
  8. ******************************************************************************/
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "gif_lib.h"
  12. #include "gif_lib_private.h"
  13. #define ABS(x) ((x) > 0 ? (x) : (-(x)))
  14. #define COLOR_ARRAY_SIZE 32768
  15. #define BITS_PER_PRIM_COLOR 5
  16. #define MAX_PRIM_COLOR 0x1f
  17. static int SortRGBAxis;
  18. typedef struct QuantizedColorType {
  19. GifByteType RGB[3];
  20. GifByteType NewColorIndex;
  21. long Count;
  22. struct QuantizedColorType *Pnext;
  23. } QuantizedColorType;
  24. typedef struct NewColorMapType {
  25. GifByteType RGBMin[3], RGBWidth[3];
  26. unsigned int NumEntries; /* # of QuantizedColorType in linked list below */
  27. unsigned long Count; /* Total number of pixels in all the entries */
  28. QuantizedColorType *QuantizedColors;
  29. } NewColorMapType;
  30. static int SubdivColorMap(NewColorMapType * NewColorSubdiv,
  31. unsigned int ColorMapSize,
  32. unsigned int *NewColorMapSize);
  33. static int SortCmpRtn(const void *Entry1, const void *Entry2);
  34. /******************************************************************************
  35. Quantize high resolution image into lower one. Input image consists of a
  36. 2D array for each of the RGB colors with size Width by Height. There is no
  37. Color map for the input. Output is a quantized image with 2D array of
  38. indexes into the output color map.
  39. Note input image can be 24 bits at the most (8 for red/green/blue) and
  40. the output has 256 colors at the most (256 entries in the color map.).
  41. ColorMapSize specifies size of color map up to 256 and will be updated to
  42. real size before returning.
  43. Also non of the parameter are allocated by this routine.
  44. This function returns GIF_OK if successful, GIF_ERROR otherwise.
  45. ******************************************************************************/
  46. int
  47. GifQuantizeBuffer(unsigned int Width,
  48. unsigned int Height,
  49. int *ColorMapSize,
  50. GifByteType * RedInput,
  51. GifByteType * GreenInput,
  52. GifByteType * BlueInput,
  53. GifByteType * OutputBuffer,
  54. GifColorType * OutputColorMap) {
  55. unsigned int Index, NumOfEntries;
  56. int i, j, MaxRGBError[3];
  57. unsigned int NewColorMapSize;
  58. long Red, Green, Blue;
  59. NewColorMapType NewColorSubdiv[256];
  60. QuantizedColorType *ColorArrayEntries, *QuantizedColor;
  61. ColorArrayEntries = (QuantizedColorType *)malloc(
  62. sizeof(QuantizedColorType) * COLOR_ARRAY_SIZE);
  63. if (ColorArrayEntries == NULL) {
  64. return GIF_ERROR;
  65. }
  66. for (i = 0; i < COLOR_ARRAY_SIZE; i++) {
  67. ColorArrayEntries[i].RGB[0] = i >> (2 * BITS_PER_PRIM_COLOR);
  68. ColorArrayEntries[i].RGB[1] = (i >> BITS_PER_PRIM_COLOR) &
  69. MAX_PRIM_COLOR;
  70. ColorArrayEntries[i].RGB[2] = i & MAX_PRIM_COLOR;
  71. ColorArrayEntries[i].Count = 0;
  72. }
  73. /* Sample the colors and their distribution: */
  74. for (i = 0; i < (int)(Width * Height); i++) {
  75. Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
  76. (2 * BITS_PER_PRIM_COLOR)) +
  77. ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
  78. BITS_PER_PRIM_COLOR) +
  79. (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
  80. ColorArrayEntries[Index].Count++;
  81. }
  82. /* Put all the colors in the first entry of the color map, and call the
  83. * recursive subdivision process. */
  84. for (i = 0; i < 256; i++) {
  85. NewColorSubdiv[i].QuantizedColors = NULL;
  86. NewColorSubdiv[i].Count = NewColorSubdiv[i].NumEntries = 0;
  87. for (j = 0; j < 3; j++) {
  88. NewColorSubdiv[i].RGBMin[j] = 0;
  89. NewColorSubdiv[i].RGBWidth[j] = 255;
  90. }
  91. }
  92. /* Find the non empty entries in the color table and chain them: */
  93. for (i = 0; i < COLOR_ARRAY_SIZE; i++)
  94. if (ColorArrayEntries[i].Count > 0)
  95. break;
  96. QuantizedColor = NewColorSubdiv[0].QuantizedColors = &ColorArrayEntries[i];
  97. NumOfEntries = 1;
  98. while (++i < COLOR_ARRAY_SIZE)
  99. if (ColorArrayEntries[i].Count > 0) {
  100. QuantizedColor->Pnext = &ColorArrayEntries[i];
  101. QuantizedColor = &ColorArrayEntries[i];
  102. NumOfEntries++;
  103. }
  104. QuantizedColor->Pnext = NULL;
  105. NewColorSubdiv[0].NumEntries = NumOfEntries; /* Different sampled colors */
  106. NewColorSubdiv[0].Count = ((long)Width) * Height; /* Pixels */
  107. NewColorMapSize = 1;
  108. if (SubdivColorMap(NewColorSubdiv, *ColorMapSize, &NewColorMapSize) !=
  109. GIF_OK) {
  110. free((char *)ColorArrayEntries);
  111. return GIF_ERROR;
  112. }
  113. if (NewColorMapSize < *ColorMapSize) {
  114. /* And clear rest of color map: */
  115. for (i = NewColorMapSize; i < *ColorMapSize; i++)
  116. OutputColorMap[i].Red = OutputColorMap[i].Green =
  117. OutputColorMap[i].Blue = 0;
  118. }
  119. /* Average the colors in each entry to be the color to be used in the
  120. * output color map, and plug it into the output color map itself. */
  121. for (i = 0; i < NewColorMapSize; i++) {
  122. if ((j = NewColorSubdiv[i].NumEntries) > 0) {
  123. QuantizedColor = NewColorSubdiv[i].QuantizedColors;
  124. Red = Green = Blue = 0;
  125. while (QuantizedColor) {
  126. QuantizedColor->NewColorIndex = i;
  127. Red += QuantizedColor->RGB[0];
  128. Green += QuantizedColor->RGB[1];
  129. Blue += QuantizedColor->RGB[2];
  130. QuantizedColor = QuantizedColor->Pnext;
  131. }
  132. OutputColorMap[i].Red = (Red << (8 - BITS_PER_PRIM_COLOR)) / j;
  133. OutputColorMap[i].Green = (Green << (8 - BITS_PER_PRIM_COLOR)) / j;
  134. OutputColorMap[i].Blue = (Blue << (8 - BITS_PER_PRIM_COLOR)) / j;
  135. }
  136. }
  137. /* Finally scan the input buffer again and put the mapped index in the
  138. * output buffer. */
  139. MaxRGBError[0] = MaxRGBError[1] = MaxRGBError[2] = 0;
  140. for (i = 0; i < (int)(Width * Height); i++) {
  141. Index = ((RedInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
  142. (2 * BITS_PER_PRIM_COLOR)) +
  143. ((GreenInput[i] >> (8 - BITS_PER_PRIM_COLOR)) <<
  144. BITS_PER_PRIM_COLOR) +
  145. (BlueInput[i] >> (8 - BITS_PER_PRIM_COLOR));
  146. Index = ColorArrayEntries[Index].NewColorIndex;
  147. OutputBuffer[i] = Index;
  148. if (MaxRGBError[0] < ABS(OutputColorMap[Index].Red - RedInput[i]))
  149. MaxRGBError[0] = ABS(OutputColorMap[Index].Red - RedInput[i]);
  150. if (MaxRGBError[1] < ABS(OutputColorMap[Index].Green - GreenInput[i]))
  151. MaxRGBError[1] = ABS(OutputColorMap[Index].Green - GreenInput[i]);
  152. if (MaxRGBError[2] < ABS(OutputColorMap[Index].Blue - BlueInput[i]))
  153. MaxRGBError[2] = ABS(OutputColorMap[Index].Blue - BlueInput[i]);
  154. }
  155. #ifdef DEBUG
  156. fprintf(stderr,
  157. "Quantization L(0) errors: Red = %d, Green = %d, Blue = %d.\n",
  158. MaxRGBError[0], MaxRGBError[1], MaxRGBError[2]);
  159. #endif /* DEBUG */
  160. free((char *)ColorArrayEntries);
  161. *ColorMapSize = NewColorMapSize;
  162. return GIF_OK;
  163. }
  164. /******************************************************************************
  165. Routine to subdivide the RGB space recursively using median cut in each
  166. axes alternatingly until ColorMapSize different cubes exists.
  167. The biggest cube in one dimension is subdivide unless it has only one entry.
  168. Returns GIF_ERROR if failed, otherwise GIF_OK.
  169. *******************************************************************************/
  170. static int
  171. SubdivColorMap(NewColorMapType * NewColorSubdiv,
  172. unsigned int ColorMapSize,
  173. unsigned int *NewColorMapSize) {
  174. int MaxSize;
  175. unsigned int i, j, Index = 0, NumEntries, MinColor, MaxColor;
  176. long Sum, Count;
  177. QuantizedColorType *QuantizedColor, **SortArray;
  178. while (ColorMapSize > *NewColorMapSize) {
  179. /* Find candidate for subdivision: */
  180. MaxSize = -1;
  181. for (i = 0; i < *NewColorMapSize; i++) {
  182. for (j = 0; j < 3; j++) {
  183. if ((((int)NewColorSubdiv[i].RGBWidth[j]) > MaxSize) &&
  184. (NewColorSubdiv[i].NumEntries > 1)) {
  185. MaxSize = NewColorSubdiv[i].RGBWidth[j];
  186. Index = i;
  187. SortRGBAxis = j;
  188. }
  189. }
  190. }
  191. if (MaxSize == -1)
  192. return GIF_OK;
  193. /* Split the entry Index into two along the axis SortRGBAxis: */
  194. /* Sort all elements in that entry along the given axis and split at
  195. * the median. */
  196. SortArray = (QuantizedColorType **)malloc(
  197. sizeof(QuantizedColorType *) *
  198. NewColorSubdiv[Index].NumEntries);
  199. if (SortArray == NULL)
  200. return GIF_ERROR;
  201. for (j = 0, QuantizedColor = NewColorSubdiv[Index].QuantizedColors;
  202. j < NewColorSubdiv[Index].NumEntries && QuantizedColor != NULL;
  203. j++, QuantizedColor = QuantizedColor->Pnext)
  204. SortArray[j] = QuantizedColor;
  205. qsort(SortArray, NewColorSubdiv[Index].NumEntries,
  206. sizeof(QuantizedColorType *), SortCmpRtn);
  207. /* Relink the sorted list into one: */
  208. for (j = 0; j < NewColorSubdiv[Index].NumEntries - 1; j++)
  209. SortArray[j]->Pnext = SortArray[j + 1];
  210. SortArray[NewColorSubdiv[Index].NumEntries - 1]->Pnext = NULL;
  211. NewColorSubdiv[Index].QuantizedColors = QuantizedColor = SortArray[0];
  212. free((char *)SortArray);
  213. /* Now simply add the Counts until we have half of the Count: */
  214. Sum = NewColorSubdiv[Index].Count / 2 - QuantizedColor->Count;
  215. NumEntries = 1;
  216. Count = QuantizedColor->Count;
  217. while (QuantizedColor->Pnext != NULL &&
  218. (Sum -= QuantizedColor->Pnext->Count) >= 0 &&
  219. QuantizedColor->Pnext->Pnext != NULL) {
  220. QuantizedColor = QuantizedColor->Pnext;
  221. NumEntries++;
  222. Count += QuantizedColor->Count;
  223. }
  224. /* Save the values of the last color of the first half, and first
  225. * of the second half so we can update the Bounding Boxes later.
  226. * Also as the colors are quantized and the BBoxes are full 0..255,
  227. * they need to be rescaled.
  228. */
  229. MaxColor = QuantizedColor->RGB[SortRGBAxis]; /* Max. of first half */
  230. /* coverity[var_deref_op] */
  231. MinColor = QuantizedColor->Pnext->RGB[SortRGBAxis]; /* of second */
  232. MaxColor <<= (8 - BITS_PER_PRIM_COLOR);
  233. MinColor <<= (8 - BITS_PER_PRIM_COLOR);
  234. /* Partition right here: */
  235. NewColorSubdiv[*NewColorMapSize].QuantizedColors =
  236. QuantizedColor->Pnext;
  237. QuantizedColor->Pnext = NULL;
  238. NewColorSubdiv[*NewColorMapSize].Count = Count;
  239. NewColorSubdiv[Index].Count -= Count;
  240. NewColorSubdiv[*NewColorMapSize].NumEntries =
  241. NewColorSubdiv[Index].NumEntries - NumEntries;
  242. NewColorSubdiv[Index].NumEntries = NumEntries;
  243. for (j = 0; j < 3; j++) {
  244. NewColorSubdiv[*NewColorMapSize].RGBMin[j] =
  245. NewColorSubdiv[Index].RGBMin[j];
  246. NewColorSubdiv[*NewColorMapSize].RGBWidth[j] =
  247. NewColorSubdiv[Index].RGBWidth[j];
  248. }
  249. NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] =
  250. NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] +
  251. NewColorSubdiv[*NewColorMapSize].RGBWidth[SortRGBAxis] - MinColor;
  252. NewColorSubdiv[*NewColorMapSize].RGBMin[SortRGBAxis] = MinColor;
  253. NewColorSubdiv[Index].RGBWidth[SortRGBAxis] =
  254. MaxColor - NewColorSubdiv[Index].RGBMin[SortRGBAxis];
  255. (*NewColorMapSize)++;
  256. }
  257. return GIF_OK;
  258. }
  259. /****************************************************************************
  260. Routine called by qsort to compare two entries.
  261. *****************************************************************************/
  262. static int
  263. SortCmpRtn(const void *Entry1,
  264. const void *Entry2) {
  265. return (*((QuantizedColorType **) Entry1))->RGB[SortRGBAxis] -
  266. (*((QuantizedColorType **) Entry2))->RGB[SortRGBAxis];
  267. }
  268. /* end */