/ocr/ocrservice/jni/hydrogen/src/utilities.cpp

http://eyes-free.googlecode.com/ · C++ · 179 lines · 110 code · 29 blank · 40 comment · 34 complexity · 3bcfbb85837178ed5c0e2c89a081dcda MD5 · raw file

  1. /*
  2. * Copyright 2011, Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* smart sharpen
  17. * PIX *edgemask = pixThreshedSobelEdgeFilter(pix8, 16, 2, L_ALL_EDGES);
  18. * PIX *enhanced = pixUnsharpMasking(pix8, UNSHARP_HALFWIDTH, UNSHARP_FRACTION);
  19. * PIX *blended = pixBlendWithGrayMask(normalized, enhanced, edgemask, 0, 0);
  20. */
  21. #include "leptonica.h"
  22. #include "utilities.h"
  23. void numaJoin(NUMA *dst, NUMA *src) {
  24. l_int32 count = numaGetCount(src);
  25. l_float32 temp;
  26. for (int i = 0; i < count; i++) {
  27. numaGetFValue(src, i, &temp);
  28. numaAddNumber(dst, temp);
  29. }
  30. }
  31. PIX *pixaDisplayRandomCmapFiltered(PIXA *pixa, l_int32 w, l_int32 h, l_uint8 *filter) {
  32. l_int32 i, n, d, index, xb, yb, wb, hb;
  33. BOXA *boxa;
  34. PIX *pixs, *pixt, *pixd;
  35. PIXCMAP *cmap;
  36. PROCNAME("pixaDisplayRandomCmapFiltered");
  37. if (!pixa)
  38. return (PIX *) ERROR_PTR("pixa not defined", procName, NULL);
  39. n = pixaGetCount(pixa);
  40. if (n == 0)
  41. return (PIX *) ERROR_PTR("no components", procName, NULL);
  42. /* Use the first pix in pixa to verify depth is 1 bpp */
  43. pixs = pixaGetPix(pixa, 0, L_CLONE);
  44. d = pixGetDepth(pixs);
  45. pixDestroy(&pixs);
  46. if (d != 1)
  47. return (PIX *) ERROR_PTR("components not 1 bpp", procName, NULL);
  48. /* If w and h not input, determine the minimum size required
  49. * to contain the origin and all c.c. */
  50. if (w == 0 || h == 0) {
  51. boxa = pixaGetBoxa(pixa, L_CLONE);
  52. boxaGetExtent(boxa, &w, &h, NULL);
  53. boxaDestroy(&boxa);
  54. }
  55. /* Set up an 8 bpp dest pix, with a colormap with 254 random colors */
  56. if ((pixd = pixCreate(w, h, 8)) == NULL)
  57. return (PIX *) ERROR_PTR("pixd not made", procName, NULL);
  58. cmap = pixcmapCreateRandom(8, 1, 1);
  59. pixSetColormap(pixd, cmap);
  60. /* Color each component and blit it in */
  61. for (i = 0; i < n; i++) {
  62. if (filter[i])
  63. continue;
  64. index = 1 + (i % 254);
  65. pixaGetBoxGeometry(pixa, i, &xb, &yb, &wb, &hb);
  66. pixs = pixaGetPix(pixa, i, L_CLONE);
  67. pixt = pixConvert1To8(NULL, pixs, 0, index);
  68. pixRasterop(pixd, xb, yb, wb, hb, PIX_PAINT, pixt, 0, 0);
  69. pixDestroy(&pixs);
  70. pixDestroy(&pixt);
  71. }
  72. return pixd;
  73. }
  74. /*!
  75. * pixcmapCreateHeatmap()
  76. *
  77. * Input: d (depth of pix for this colormap; 1, 2, 4 or 8)
  78. * Return: cmap, or null on error
  79. *
  80. * Notes:
  81. * (1) Colormap shows 0 as black, 1-MAX as a range from violet to red.
  82. */
  83. PIXCMAP *
  84. pixcmapCreateHeatmap(l_int32 d)
  85. {
  86. l_int32 nlevels, i, h, s, v;
  87. PIXCMAP *cmap;
  88. PROCNAME("pixcmapCreateHeatmap");
  89. if (d != 1 && d != 2 && d !=4 && d != 8)
  90. return (PIXCMAP *)ERROR_PTR("d not in {1, 2, 4, 8}", procName, NULL);
  91. nlevels = 1 << d;
  92. cmap = pixcmapCreate(d);
  93. nlevels--;
  94. pixcmapAddColor(cmap, 0, 0, 0);
  95. for (i = nlevels; i > 0; i--) {
  96. h = 170 * i / nlevels;
  97. s = 255;
  98. v = 255;
  99. pixcmapAddColor(cmap, h, s, v);
  100. }
  101. pixcmapConvertHSVToRGB(cmap);
  102. return cmap;
  103. }
  104. PIX *pixaDisplayHeatmap(PIXA *pixa, l_int32 w, l_int32 h, NUMA *confs) {
  105. l_int32 i, n, d, val, xb, yb, wb, hb;
  106. l_float32 maxconf, minconf, confrange, conf;
  107. BOXA *boxa;
  108. PIX *pixs, *pixt, *pixd;
  109. PIXCMAP *cmap;
  110. PROCNAME("pixaDisplayHeatmap");
  111. if (!pixa)
  112. return (PIX *) ERROR_PTR("pixa not defined", procName, NULL);
  113. n = pixaGetCount(pixa);
  114. if (n == 0)
  115. return (PIX *) ERROR_PTR("no components", procName, NULL);
  116. /* Use the first pix in pixa to verify depth is 1 bpp */
  117. pixs = pixaGetPix(pixa, 0, L_CLONE);
  118. d = pixGetDepth(pixs);
  119. pixDestroy(&pixs);
  120. if (d != 1)
  121. return (PIX *) ERROR_PTR("components not 1 bpp", procName, NULL);
  122. /* If w and h not input, determine the minimum size required
  123. * to contain the origin and all c.c. */
  124. if (w == 0 || h == 0) {
  125. boxa = pixaGetBoxa(pixa, L_CLONE);
  126. boxaGetExtent(boxa, &w, &h, NULL);
  127. boxaDestroy(&boxa);
  128. }
  129. /* Determine the confidence range */
  130. numaGetMin(confs, &minconf, NULL);
  131. numaGetMax(confs, &maxconf, NULL);
  132. confrange = maxconf - minconf;
  133. /* Set up an 8 bpp dest pix, with a colormap with 254 random colors */
  134. if ((pixd = pixCreate(w, h, 8)) == NULL)
  135. return (PIX *) ERROR_PTR("pixd not made", procName, NULL);
  136. cmap = pixcmapCreateHeatmap(8);
  137. pixSetColormap(pixd, cmap);
  138. /* Color each component and blit it in */
  139. for (i = 0; i < n; i++) {
  140. numaGetFValue(confs, i, &conf);
  141. val = (l_int32) ((conf - minconf) / confrange * 254) + 1;
  142. pixaGetBoxGeometry(pixa, i, &xb, &yb, &wb, &hb);
  143. pixs = pixaGetPix(pixa, i, L_CLONE);
  144. pixt = pixConvert1To8(NULL, pixs, 0, val);
  145. pixRasterop(pixd, xb, yb, wb, hb, PIX_PAINT, pixt, 0, 0);
  146. pixDestroy(&pixs);
  147. pixDestroy(&pixt);
  148. }
  149. return pixd;
  150. }