Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scaleimg.cpp File Reference
#include "mfcpch.h"
#include <stdlib.h>
#include <string.h>
#include "fileerr.h"
#include "tprintf.h"
#include "img.h"
#include "imgscale.h"
#include "scaleimg.h"

Go to the source code of this file.

Functions

void scale_image (IMAGE &image, IMAGE &target_image)
 
void scale_image_cop_out (IMAGE &image, IMAGE &target_image, float factor, int *hires, int *lores, int *oldhires, int *oldlores)
 

Function Documentation

void scale_image ( IMAGE image,
IMAGE target_image 
)

Definition at line 42 of file scaleimg.cpp.

45  {
46  inT32 xsize, ysize, new_xsize, new_ysize;
47  IMAGELINE line, new_line;
48  int *hires, *lores, *oldhires, *oldlores;
49  int i, j, n, oldn, row, col;
50  int offset = 0; //not used here
51  float factor;
52  uinT8 curr_colour, new_colour;
53  int dummy = -1;
54  IMAGE image2; //horiz scaled image
55 
56  xsize = image.get_xsize ();
57  ysize = image.get_ysize ();
58  new_xsize = target_image.get_xsize ();
59  new_ysize = target_image.get_ysize ();
60  if (new_ysize > new_xsize)
61  new_line.init (new_ysize);
62  else
63  new_line.init (new_xsize);
64 
65  factor = (float) xsize / (float) new_xsize;
66 
67  hires = (int *) calloc (xsize, sizeof (int));
68  lores = (int *) calloc (new_xsize, sizeof (int));
69  oldhires = (int *) calloc (xsize, sizeof (int));
70  oldlores = (int *) calloc (new_xsize, sizeof (int));
71  if ((hires == NULL) || (lores == NULL) || (oldhires == NULL)
72  || (oldlores == NULL)) {
73  fprintf (stderr, "Calloc error in scale_image\n");
74  err_exit();
75  }
76 
77  image2.create (new_xsize, ysize, image.get_bpp ());
78 
79  oldn = 0;
80  /* do first row separately because hires[col-1] doesn't make sense here */
81  image.fast_get_line (0, 0, xsize, &line);
82  /* each line nominally begins with white */
83  curr_colour = 1;
84  n = 0;
85  for (i = 0; i < xsize; i++) {
86  new_colour = *(line.pixels + i);
87  if (new_colour != curr_colour) {
88  hires[n] = i;
89  n++;
90  curr_colour = new_colour;
91  }
92  }
93  if (offset != 0)
94  for (i = 0; i < n; i++)
95  hires[i] += offset;
96 
97  if (n > new_xsize) {
98  tprintf ("Too many transitions (%d) on line 0\n", n);
99  scale_image_cop_out(image,
100  target_image,
101  factor,
102  hires,
103  lores,
104  oldhires,
105  oldlores);
106  return;
107  }
108  else if (n > 0)
109  dyn_prog (n, hires, lores, new_xsize, &dummy, &dummy, 0, factor);
110  else
111  lores[0] = new_xsize;
112 
113  curr_colour = 1;
114  j = 0;
115  for (i = 0; i < new_xsize; i++) {
116  if (lores[j] == i) {
117  curr_colour = 1 - curr_colour;
118  j++;
119  }
120  *(new_line.pixels + i) = curr_colour;
121  }
122  image2.put_line (0, 0, new_xsize, &new_line, 0);
123 
124  for (i = 0; i < n; i++) {
125  oldhires[i] = hires[i];
126  oldlores[i] = lores[i];
127  }
128 
129  for (i = n; i < oldn; i++) {
130  oldhires[i] = 0;
131  oldlores[i] = 0;
132  }
133  oldn = n;
134 
135  for (row = 1; row < ysize; row++) {
136  image.fast_get_line (0, row, xsize, &line);
137  /* each line nominally begins with white */
138  curr_colour = 1;
139  n = 0;
140  for (i = 0; i < xsize; i++) {
141  new_colour = *(line.pixels + i);
142  if (new_colour != curr_colour) {
143  hires[n] = i;
144  n++;
145  curr_colour = new_colour;
146  }
147  }
148  for (i = n; i < oldn; i++) {
149  hires[i] = 0;
150  lores[i] = 0;
151  }
152  if (offset != 0)
153  for (i = 0; i < n; i++)
154  hires[i] += offset;
155 
156  if (n > new_xsize) {
157  tprintf ("Too many transitions (%d) on line %d\n", n, row);
158  scale_image_cop_out(image,
159  target_image,
160  factor,
161  hires,
162  lores,
163  oldhires,
164  oldlores);
165  return;
166  }
167  else if (n > 0)
168  dyn_prog(n, hires, lores, new_xsize, oldhires, oldlores, oldn, factor);
169  else
170  lores[0] = new_xsize;
171 
172  curr_colour = 1;
173  j = 0;
174  for (i = 0; i < new_xsize; i++) {
175  if (lores[j] == i) {
176  curr_colour = 1 - curr_colour;
177  j++;
178  }
179  *(new_line.pixels + i) = curr_colour;
180  }
181  image2.put_line (0, row, new_xsize, &new_line, 0);
182 
183  for (i = 0; i < n; i++) {
184  oldhires[i] = hires[i];
185  oldlores[i] = lores[i];
186  }
187  for (i = n; i < oldn; i++) {
188  oldhires[i] = 0;
189  oldlores[i] = 0;
190  }
191  oldn = n;
192  }
193 
194  free(hires);
195  free(lores);
196  free(oldhires);
197  free(oldlores);
198 
199  /* NOW DO THE VERTICAL SCALING from image2 to target_image*/
200 
201  xsize = new_xsize;
202  factor = (float) ysize / (float) new_ysize;
203  offset = 0;
204 
205  hires = (int *) calloc (ysize, sizeof (int));
206  lores = (int *) calloc (new_ysize, sizeof (int));
207  oldhires = (int *) calloc (ysize, sizeof (int));
208  oldlores = (int *) calloc (new_ysize, sizeof (int));
209  if ((hires == NULL) || (lores == NULL) || (oldhires == NULL)
210  || (oldlores == NULL)) {
211  fprintf (stderr, "Calloc error in scale_image (vert)\n");
212  err_exit();
213  }
214 
215  oldn = 0;
216  /* do first col separately because hires[col-1] doesn't make sense here */
217  image2.get_column (0, 0, ysize, &line, 0);
218  /* each line nominally begins with white */
219  curr_colour = 1;
220  n = 0;
221  for (i = 0; i < ysize; i++) {
222  new_colour = *(line.pixels + i);
223  if (new_colour != curr_colour) {
224  hires[n] = i;
225  n++;
226  curr_colour = new_colour;
227  }
228  }
229 
230  if (offset != 0)
231  for (i = 0; i < n; i++)
232  hires[i] += offset;
233 
234  if (n > new_ysize) {
235  tprintf ("Too many transitions (%d) on column 0\n", n);
236  scale_image_cop_out(image,
237  target_image,
238  factor,
239  hires,
240  lores,
241  oldhires,
242  oldlores);
243  return;
244  }
245  else if (n > 0)
246  dyn_prog (n, hires, lores, new_ysize, &dummy, &dummy, 0, factor);
247  else
248  lores[0] = new_ysize;
249 
250  curr_colour = 1;
251  j = 0;
252  for (i = 0; i < new_ysize; i++) {
253  if (lores[j] == i) {
254  curr_colour = 1 - curr_colour;
255  j++;
256  }
257  *(new_line.pixels + i) = curr_colour;
258  }
259  target_image.put_column (0, 0, new_ysize, &new_line, 0);
260 
261  for (i = 0; i < n; i++) {
262  oldhires[i] = hires[i];
263  oldlores[i] = lores[i];
264  }
265  for (i = n; i < oldn; i++) {
266  oldhires[i] = 0;
267  oldlores[i] = 0;
268  }
269  oldn = n;
270 
271  for (col = 1; col < xsize; col++) {
272  image2.get_column (col, 0, ysize, &line, 0);
273  /* each line nominally begins with white */
274  curr_colour = 1;
275  n = 0;
276  for (i = 0; i < ysize; i++) {
277  new_colour = *(line.pixels + i);
278  if (new_colour != curr_colour) {
279  hires[n] = i;
280  n++;
281  curr_colour = new_colour;
282  }
283  }
284  for (i = n; i < oldn; i++) {
285  hires[i] = 0;
286  lores[i] = 0;
287  }
288 
289  if (offset != 0)
290  for (i = 0; i < n; i++)
291  hires[i] += offset;
292 
293  if (n > new_ysize) {
294  tprintf ("Too many transitions (%d) on column %d\n", n, col);
295  scale_image_cop_out(image,
296  target_image,
297  factor,
298  hires,
299  lores,
300  oldhires,
301  oldlores);
302  return;
303  }
304  else if (n > 0)
305  dyn_prog(n, hires, lores, new_ysize, oldhires, oldlores, oldn, factor);
306  else
307  lores[0] = new_ysize;
308 
309  curr_colour = 1;
310  j = 0;
311  for (i = 0; i < new_ysize; i++) {
312  if (lores[j] == i) {
313  curr_colour = 1 - curr_colour;
314  j++;
315  }
316  *(new_line.pixels + i) = curr_colour;
317  }
318  target_image.put_column (col, 0, new_ysize, &new_line, 0);
319 
320  for (i = 0; i < n; i++) {
321  oldhires[i] = hires[i];
322  oldlores[i] = lores[i];
323  }
324  for (i = n; i < oldn; i++) {
325  oldhires[i] = 0;
326  oldlores[i] = 0;
327  }
328  oldn = n;
329  }
330  free(hires);
331  free(lores);
332  free(oldhires);
333  free(oldlores);
334 }
void err_exit()
Definition: globaloc.cpp:73
Definition: img.h:325
#define NULL
Definition: host.h:144
void init(inT32 width)
Definition: img.h:343
int inT32
Definition: host.h:102
void dyn_prog(int n, int *x, int *y, int ymax, int *oldx, int *oldy, int oldn, float factor)
Definition: imgscale.cpp:50
void put_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1222
inT32 get_ysize()
Definition: img.h:99
void put_column(inT32 x, inT32 y, inT32 height, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1371
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
uinT8 * pixels
image pixels
Definition: img.h:328
void scale_image_cop_out(IMAGE &image, IMAGE &target_image, float factor, int *hires, int *lores, int *oldhires, int *oldlores)
Definition: scaleimg.cpp:343
inT8 get_bpp()
Definition: img.h:106
void get_column(inT32 x, inT32 y, inT32 height, IMAGELINE *linebuf, inT32 margins)
Definition: imgs.cpp:1105
unsigned char uinT8
Definition: host.h:99
inT32 get_xsize()
Definition: img.h:92
#define new_line()
Definition: cutil.h:83
Definition: img.h:51
void fast_get_line(inT32 x, inT32 y, inT32 width, IMAGELINE *linebuf)
Definition: imgs.cpp:978
inT8 create(inT32 x, inT32 y, inT8 bits_per_pixel)
Definition: imgs.cpp:121
void scale_image_cop_out ( IMAGE image,
IMAGE target_image,
float  factor,
int *  hires,
int *  lores,
int *  oldhires,
int *  oldlores 
)

Definition at line 343 of file scaleimg.cpp.

350  {
351  inT32 xsize, ysize, new_xsize, new_ysize;
352 
353  xsize = image.get_xsize ();
354  ysize = image.get_ysize ();
355  new_xsize = target_image.get_xsize ();
356  new_ysize = target_image.get_ysize ();
357 
358  if (factor <= 0.5)
359  reduce_sub_image (&image, 0, 0, xsize, ysize,
360  &target_image, 0, 0, (inT32) (1.0 / factor), FALSE);
361  else if (factor >= 2)
362  enlarge_sub_image (&image, 0, 0, &target_image,
363  0, 0, new_xsize, new_ysize, (inT32) factor, FALSE);
364  else
365  copy_sub_image (&image, 0, 0, xsize, ysize, &target_image, 0, 0, FALSE);
366  free(hires);
367  free(lores);
368  free(oldhires);
369  free(oldlores);
370 }
int inT32
Definition: host.h:102
#define FALSE
Definition: capi.h:28
DLLSYM void enlarge_sub_image(IMAGE *source, inT32 xstart, inT32 ystart, IMAGE *dest, inT32 xdest, inT32 ydest, inT32 xext, inT32 yext, inT32 scale, BOOL8 adjust_grey)
Definition: imgs.cpp:398
DLLSYM void copy_sub_image(IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, BOOL8 adjust_grey)
Definition: imgs.cpp:270
inT32 get_ysize()
Definition: img.h:99
DLLSYM void reduce_sub_image(IMAGE *source, inT32 xstart, inT32 ystart, inT32 xext, inT32 yext, IMAGE *dest, inT32 xdest, inT32 ydest, inT32 scale, BOOL8 adjust_grey)
Definition: imgs.cpp:658
inT32 get_xsize()
Definition: img.h:92