PageRenderTime 24ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17/* smart sharpen
 18 * PIX *edgemask = pixThreshedSobelEdgeFilter(pix8, 16, 2, L_ALL_EDGES);
 19 * PIX *enhanced = pixUnsharpMasking(pix8, UNSHARP_HALFWIDTH, UNSHARP_FRACTION);
 20 * PIX *blended = pixBlendWithGrayMask(normalized, enhanced, edgemask, 0, 0);
 21 */
 22
 23#include "leptonica.h"
 24#include "utilities.h"
 25
 26void numaJoin(NUMA *dst, NUMA *src) {
 27  l_int32 count = numaGetCount(src);
 28  l_float32 temp;
 29
 30  for (int i = 0; i < count; i++) {
 31    numaGetFValue(src, i, &temp);
 32    numaAddNumber(dst, temp);
 33  }
 34}
 35
 36PIX *pixaDisplayRandomCmapFiltered(PIXA *pixa, l_int32 w, l_int32 h, l_uint8 *filter) {
 37  l_int32 i, n, d, index, xb, yb, wb, hb;
 38  BOXA *boxa;
 39  PIX *pixs, *pixt, *pixd;
 40  PIXCMAP *cmap;
 41
 42  PROCNAME("pixaDisplayRandomCmapFiltered");
 43
 44  if (!pixa)
 45    return (PIX *) ERROR_PTR("pixa not defined", procName, NULL);
 46  n = pixaGetCount(pixa);
 47  if (n == 0)
 48    return (PIX *) ERROR_PTR("no components", procName, NULL);
 49
 50  /* Use the first pix in pixa to verify depth is 1 bpp  */
 51  pixs = pixaGetPix(pixa, 0, L_CLONE);
 52  d = pixGetDepth(pixs);
 53  pixDestroy(&pixs);
 54  if (d != 1)
 55    return (PIX *) ERROR_PTR("components not 1 bpp", procName, NULL);
 56
 57  /* If w and h not input, determine the minimum size required
 58   * to contain the origin and all c.c. */
 59  if (w == 0 || h == 0) {
 60    boxa = pixaGetBoxa(pixa, L_CLONE);
 61    boxaGetExtent(boxa, &w, &h, NULL);
 62    boxaDestroy(&boxa);
 63  }
 64
 65  /* Set up an 8 bpp dest pix, with a colormap with 254 random colors */
 66  if ((pixd = pixCreate(w, h, 8)) == NULL)
 67    return (PIX *) ERROR_PTR("pixd not made", procName, NULL);
 68  cmap = pixcmapCreateRandom(8, 1, 1);
 69  pixSetColormap(pixd, cmap);
 70
 71  /* Color each component and blit it in */
 72  for (i = 0; i < n; i++) {
 73    if (filter[i])
 74      continue;
 75    index = 1 + (i % 254);
 76    pixaGetBoxGeometry(pixa, i, &xb, &yb, &wb, &hb);
 77    pixs = pixaGetPix(pixa, i, L_CLONE);
 78    pixt = pixConvert1To8(NULL, pixs, 0, index);
 79    pixRasterop(pixd, xb, yb, wb, hb, PIX_PAINT, pixt, 0, 0);
 80    pixDestroy(&pixs);
 81    pixDestroy(&pixt);
 82  }
 83
 84  return pixd;
 85}
 86
 87/*!
 88 *  pixcmapCreateHeatmap()
 89 *
 90 *      Input:  d (depth of pix for this colormap; 1, 2, 4 or 8)
 91 *      Return: cmap, or null on error
 92 *
 93 *  Notes:
 94 *      (1) Colormap shows 0 as black, 1-MAX as a range from violet to red.
 95 */
 96PIXCMAP *
 97pixcmapCreateHeatmap(l_int32  d)
 98{
 99l_int32   nlevels, i, h, s, v;
100PIXCMAP  *cmap;
101
102    PROCNAME("pixcmapCreateHeatmap");
103
104    if (d != 1 && d != 2 && d !=4 && d != 8)
105        return (PIXCMAP *)ERROR_PTR("d not in {1, 2, 4, 8}", procName, NULL);
106    nlevels = 1 << d;
107
108    cmap = pixcmapCreate(d);
109
110    nlevels--;
111    pixcmapAddColor(cmap, 0, 0, 0);
112
113    for (i = nlevels; i > 0; i--) {
114        h = 170 * i / nlevels;
115        s = 255;
116        v = 255;
117        pixcmapAddColor(cmap, h, s, v);
118    }
119
120    pixcmapConvertHSVToRGB(cmap);
121
122    return cmap;
123}
124
125PIX *pixaDisplayHeatmap(PIXA *pixa, l_int32 w, l_int32 h, NUMA *confs) {
126  l_int32 i, n, d, val, xb, yb, wb, hb;
127  l_float32 maxconf, minconf, confrange, conf;
128  BOXA *boxa;
129  PIX *pixs, *pixt, *pixd;
130  PIXCMAP *cmap;
131
132  PROCNAME("pixaDisplayHeatmap");
133
134  if (!pixa)
135    return (PIX *) ERROR_PTR("pixa not defined", procName, NULL);
136  n = pixaGetCount(pixa);
137  if (n == 0)
138    return (PIX *) ERROR_PTR("no components", procName, NULL);
139
140  /* Use the first pix in pixa to verify depth is 1 bpp  */
141  pixs = pixaGetPix(pixa, 0, L_CLONE);
142  d = pixGetDepth(pixs);
143  pixDestroy(&pixs);
144  if (d != 1)
145    return (PIX *) ERROR_PTR("components not 1 bpp", procName, NULL);
146
147  /* If w and h not input, determine the minimum size required
148   * to contain the origin and all c.c. */
149  if (w == 0 || h == 0) {
150    boxa = pixaGetBoxa(pixa, L_CLONE);
151    boxaGetExtent(boxa, &w, &h, NULL);
152    boxaDestroy(&boxa);
153  }
154
155  /* Determine the confidence range */
156  numaGetMin(confs, &minconf, NULL);
157  numaGetMax(confs, &maxconf, NULL);
158  confrange = maxconf - minconf;
159
160  /* Set up an 8 bpp dest pix, with a colormap with 254 random colors */
161  if ((pixd = pixCreate(w, h, 8)) == NULL)
162    return (PIX *) ERROR_PTR("pixd not made", procName, NULL);
163  cmap = pixcmapCreateHeatmap(8);
164  pixSetColormap(pixd, cmap);
165
166  /* Color each component and blit it in */
167  for (i = 0; i < n; i++) {
168    numaGetFValue(confs, i, &conf);
169    val = (l_int32) ((conf - minconf) / confrange * 254) + 1;
170    pixaGetBoxGeometry(pixa, i, &xb, &yb, &wb, &hb);
171    pixs = pixaGetPix(pixa, i, L_CLONE);
172    pixt = pixConvert1To8(NULL, pixs, 0, val);
173    pixRasterop(pixd, xb, yb, wb, hb, PIX_PAINT, pixt, 0, 0);
174    pixDestroy(&pixs);
175    pixDestroy(&pixt);
176  }
177
178  return pixd;
179}