IMAGE Class Reference

#include <img.h>

List of all members.


Detailed Description

Encapsulated image class.

Definition at line 58 of file img.h.

Public Member Functions

Private Member Functions

Private Attributes

Friends


Constructor & Destructor Documentation

IMAGE::IMAGE (  ) 

Contructor for an IMAGE class.

Makes the image definitely illegal ??

Definition at line 66 of file imgs.cpp.

References bpp, fd, image, NULL, photo_interp, and res.

00066              {  //construct an image
00067   bpp = 0;                       //all illegal
00068   fd = -1;
00069   image = NULL;
00070   photo_interp = 1;
00071   res = image_default_resolution;
00072 }

IMAGE::~IMAGE (  )  [inline]

Definition at line 63 of file img.h.

References destroy().

00063               {                  //destructor
00064       destroy();  //free memory
00065     }


Member Function Documentation

INT8 IMAGE::bufread ( INT32  y  )  [private]

Read a bit more of an image into the buffer.

Definition at line 226 of file imgio.cpp.

References bufheight, copy_sub_image, ERRCODE::error(), FALSE, fd, image, lineskip, LOG, NULL, read(), READFAILED, TRUE, xdim, xsize, ymax, and ymin.

Referenced by check_legal_access().

00228                      {
00229   INT32 readtop;                 //no of lines copied
00230   INT32 linestoread;             //no of lines to read
00231   INT32 row;                     //row to read
00232   BOOL8 failed;                  //read failed
00233 
00234                                  //copy needed?
00235   if (y + bufheight / 2 >= ymin) {
00236                                  //no of lines to move
00237     readtop = y + bufheight / 2 - ymin + 1;
00238                                  //copy inside it
00239     copy_sub_image (this, 0, ymin, xsize, readtop, this,
00240       0, ymax - readtop, TRUE);
00241   }
00242   else
00243     readtop = 0;
00244   ymax = y + bufheight / 2;      //new top of image
00245   ymin = ymax - bufheight;       //possible bottom
00246   if (ymin < 0)
00247     ymin = 0;                    //clip to image size
00248   linestoread = ymax - ymin - readtop;
00249   if (lineskip == 0)
00250     failed =::read (fd, (char *) (image + xdim * readtop),
00251       (size_t) (xdim * linestoread)) != xdim * linestoread;
00252   else {
00253     for (failed = FALSE, row = 0; row < linestoread && !failed; row++) {
00254       failed =::read (fd, (char *) (image + (readtop + row) * xdim),
00255         (size_t) xdim) != xdim;
00256       failed |= lseek (fd, lineskip, SEEK_CUR) < 0;
00257     }
00258   }
00259   if (failed) {
00260     READFAILED.error ("IMAGE::bufread", LOG, NULL);
00261     return -1;                   //read failed
00262   }
00263   if (ymin <= 0) {
00264     close(fd);  //finished reading
00265     fd = -1;                     //not open now
00266   }
00267   return 0;                      //success
00268 }

INT8 IMAGE::capture ( UINT8 pixels,
INT32  x,
INT32  y,
INT8  bits_per_pixel 
)

Assign a given memory area to an image to use as an image of given size and bpp.

Definition at line 154 of file imgs.cpp.

References bpp, bps, bufheight, bytespp, captured, check_legal_image_size(), destroy(), image, photo_interp, res, TRUE, xdim, xsize, ymax, ymin, and ysize.

Referenced by TessBaseAPI::CopyBinaryRect(), create(), def_show_sub_image(), and read_tif_image().

00159                      {
00160   destroy(); 
00161   xdim = check_legal_image_size (x, y, bits_per_pixel);
00162   if (xdim < 0)
00163     return -1;
00164   xsize = x;
00165   ysize = y;
00166   bufheight = y;
00167   bpp = bits_per_pixel;
00168   bps = bpp == 24 ? 8 : bpp;
00169   photo_interp = 1;
00170   bytespp = (bpp + 7) / 8;
00171   image = pixels;                //assign image area
00172   ymin = 0;
00173   ymax = bufheight;              //read it all
00174   captured = TRUE;
00175   res = image_default_resolution;
00176   return 0;                      //success
00177 }

void IMAGE::check_legal_access ( INT32  x,
INT32  y,
INT32  xext 
)

Check that x,y are within the bounds of the image.

Call bufread if necessary to get the image into memory.

Definition at line 1443 of file imgs.cpp.

References ABORT, BADIMAGECOORDS, BADIMAGESEEK, bufread(), ERRCODE::error(), xsize, ymax, ymin, and ysize.

Referenced by bias_sub_image(), copy_sub_image(), enlarge_sub_image(), fast_get_line(), fast_reduce_sub_image(), get_column(), get_line(), pixel(), put_column(), put_line(), reduce_sub_image(), and starbase_to_normal().

01447                                 {
01448   if (x < 0 || x >= xsize || y < 0 || y >= ysize || x + xext > xsize)
01449     BADIMAGECOORDS.error ("IMAGE::check_legal_access",
01450       ABORT, "(%d+%d,%d)", x, xext, y);
01451   if (y >= ymax)
01452     BADIMAGESEEK.error ("IMAGE::check_legal_access", ABORT, "(%d,%d)", x, y);
01453   if (y < ymin)
01454     bufread(y);  //read some more
01455 }

void IMAGE::convolver ( INT32  win_width,
INT32  win_height,
void(*)(UINT8 **pixels, UINT8 bytespp, INT32 win_wd, INT32 win_ht, UINT8 ret_white_value, UINT8 *result)  convolve 
)

Calls the specified function for each pixel in the image, passing in an m x n window of the image, centred on the pixel.

The convolution function returns a new value for the pixel, based on the window.

At the edges of the image, the window is padded to white pixels.

Definition at line 1468 of file imgs.cpp.

References ABORT, alloc_mem(), BADWINDOW, bpp, ERRCODE::error(), get_line(), get_white_level(), IMAGELINE::init(), max, IMAGELINE::pixels, put_line(), IMAGELINE::set_bpp(), xsize, and ysize.

01478   {
01479   IMAGELINE new_row;             //Replacement pixels
01480   IMAGELINE *old_rows;           //Rows being processed
01481   INT32 oldest_imline;           //Next imline to replace
01482   UINT8 **window;                //ptrs to pixel rows
01483   UINT8 **winmax;                //ptrs to pixel rows
01484   UINT8 **win;                   //ptrs to pixel rows
01485   INT32 current_row;             //Row being calculated
01486   INT32 current_col;             //Col being calculated
01487   INT32 row = 0;                 //Next row to get
01488 
01489   INT32 i, j;
01490   UINT8 *pix;
01491   UINT8 *max;
01492   INT32 xmargin = win_width / 2;
01493   INT32 ymargin = win_height / 2;
01494   UINT8 white = get_white_level ();
01495   const UINT8 max_white = 255;
01496   float white_scale = (float) 255 / get_white_level ();
01497 
01498   if (((win_width % 2) == 0) ||
01499     ((win_height % 2) == 0) ||
01500     (win_height < 3) ||
01501     (win_width < 3) || (win_height > ysize / 2) || (win_width > xsize / 2))
01502     BADWINDOW.error ("IMAGE::convolver",
01503       ABORT, "(%d x %d)", win_width, win_height);
01504 
01505   new_row.init (xsize * bytespp);
01506   new_row.set_bpp (bpp);
01507   old_rows = new IMAGELINE[win_height];
01508   for (i = 0; i < win_height; i++) {
01509     old_rows[i].init ((xsize + 2 * xmargin) * bytespp);
01510     old_rows[i].set_bpp (bpp);
01511   }
01512 
01513   window = (UINT8 **) alloc_mem (win_height * sizeof (UINT8 *));
01514   winmax = window + win_height;
01515 
01516   /* Make bottom border */
01517   for (oldest_imline = 0; oldest_imline < ymargin; oldest_imline++) {
01518     pix = old_rows[oldest_imline].pixels;
01519     max = pix + (xsize + 2 * xmargin) * bytespp;
01520     while (pix < max)
01521       *pix++ = max_white;
01522   }
01523   /* Initialise remaining rows but one*/
01524   for (; oldest_imline < win_height - 1; oldest_imline++) {
01525     get_line (0, row++, xsize, &old_rows[oldest_imline], xmargin);
01526     if (max_white != white) {
01527       pix = old_rows[oldest_imline].pixels;
01528       max = pix + (xsize + 2 * xmargin) * bytespp;
01529       while (pix < max) {
01530         *pix = (UINT8) (*pix * white_scale);
01531         ++pix;
01532       }
01533     }
01534   }
01535 
01536   /* Image Processing */
01537 
01538   for (current_row = 0; current_row < ysize;) {
01539     /* Get next row and re-initialise window array */
01540     if (row < ysize) {
01541       get_line (0, row++, xsize, &old_rows[oldest_imline], xmargin);
01542       if (max_white != white) {
01543         pix = old_rows[oldest_imline].pixels;
01544         max = pix + (xsize + 2 * xmargin) * bytespp;
01545         while (pix < max) {
01546           *pix = (UINT8) (*pix * white_scale);
01547           ++pix;
01548         }
01549       }
01550     }
01551     else {
01552       pix = old_rows[oldest_imline].pixels;
01553       max = pix + (xsize + 2 * xmargin) * bytespp;
01554       while (pix < max)
01555         *pix++ = max_white;
01556     }
01557     oldest_imline++;
01558     if (oldest_imline >= win_height)
01559       oldest_imline = 0;
01560 
01561     /* Process line */
01562     pix = new_row.pixels;
01563     for (current_col = 0; current_col < xsize;) {
01564       /* Set up window ptrs */
01565       if (current_col == 0) {
01566         j = oldest_imline;
01567         for (i = 0; i < win_height; i++) {
01568           window[i] = old_rows[j++].pixels;
01569           if (j >= win_height)
01570             j = 0;
01571         }
01572       }
01573       else {
01574         for (win = window; win < winmax; (*win++) += bytespp);
01575         //Move along rows
01576       }
01577 
01578       convolve(window, bytespp, win_width, win_height, white, pix); 
01579       pix += bytespp;
01580       current_col++;
01581     }
01582 
01583     put_line (0, current_row, xsize, &new_row, 0);
01584     new_row.init ();
01585     new_row.set_bpp (bpp);
01586     current_row++;
01587   }
01588 }

INT8 IMAGE::create ( INT32  x,
INT32  y,
INT8  bits_per_pixel 
)

Create an image (allocate memory) of a specific size and bpp.

Definition at line 111 of file imgs.cpp.

References ABORT, alloc_big_zeros(), capture(), captured, check_legal_image_size(), ERRCODE::error(), FALSE, MEMORY_OUT, NULL, res, and xdim.

Referenced by clip_sample(), TessBaseAPI::CopyBinaryRect(), CHAR_PROTO::make_image(), match1(), scale_image(), and TessBaseAPI::ThresholdRect().

00115                     {
00116   UINT8 *pixels;                 //memory for image
00117 
00118   xdim = check_legal_image_size (x, y, bits_per_pixel);
00119   if (xdim < 0)
00120     return -1;
00121   pixels = (UINT8 *) alloc_big_zeros ((size_t) (xdim * y * sizeof (UINT8)));
00122   if (pixels == NULL) {
00123     MEMORY_OUT.error ("IMAGE::create", ABORT, "Size=(%d,%d)", xdim, y);
00124     return -1;
00125   }
00126                                  //allocate to image
00127   this->capture (pixels, x, y, bits_per_pixel);
00128   captured = FALSE;
00129   res = image_default_resolution;
00130   return 0;                      //success
00131 }

void IMAGE::destroy (  ) 

Destroy an image, freeing memory and closing any open file.

Definition at line 137 of file imgs.cpp.

References bpp, captured, fd, free_big_mem(), image, and NULL.

Referenced by capture(), operator=(), read(), and read_header().

00137                     {  //get rid of image
00138   if (image != NULL && !captured) {
00139     free_big_mem(image); 
00140   }
00141   image = NULL;
00142   if (fd >= 0) {
00143     close(fd); 
00144     fd = -1;
00145   }
00146   bpp = 0;
00147 }

void IMAGE::fast_get_line ( INT32  x,
INT32  y,
INT32  width,
IMAGELINE linebuf 
)

Get a line of image into the supplied image line buffer.

The image is converted to 8bpp by simple assignment.

If the image is aleady 8 or 6bpp, no copy is done and a pointer to the correct image section is put in the line buffer.

Definition at line 948 of file imgs.cpp.

References IMAGELINE::bpp, bpp, bytespp, check_legal_access(), get_line(), image, IMAGELINE::pixels, xdim, and ymax.

Referenced by CHAR_PROTO::add_sample(), bias_sub_image(), CHAR_PROTO::CHAR_PROTO(), copy_sub_image(), enlarge_sub_image(), fast_reduce_sub_image(), generate_imlines(), invert_image(), CHAR_PROTO::make_image(), match1(), reduce_sub_image(), and scale_image().

00953                            {
00954   if (width > 0 && bpp > 4) {
00955     check_legal_access(x, y, width); 
00956                                  //get pointer only
00957     linebuf->pixels = image + xdim * (ymax - 1 - y) + x * bytespp;
00958   }
00959   else
00960                                  //just copy it
00961     this->get_line (x, y, width, linebuf, 0);
00962   linebuf->bpp = bpp;
00963 }

void IMAGE::fast_put_line ( INT32  x,
INT32  y,
INT32  width,
IMAGELINE linebuf 
)

Put a line buffer back into the image.

If the line buffer merely points back into the image, nothing is done. Otherwise, put_line is used to copy the line back.

Definition at line 1168 of file imgs.cpp.

References IMAGELINE::line, and put_line().

Referenced by bias_sub_image(), invert_image(), CHAR_PROTO::make_image(), and match1().

01173                            {
01174   if (width > 0 && (bpp <= 4 || linebuf->pixels == linebuf->line))
01175                                  //just copy it
01176     put_line (x, y, width, linebuf, 0);
01177 }

INT8 IMAGE::get_bpp (  )  [inline]

Definition at line 100 of file img.h.

Referenced by bias_sub_image(), def_show_sub_image(), edges_and_textord(), invert_image(), main(), matrix_match(), scale_image(), and starbase_to_normal().

00100                    { 
00101       return bpp;
00102     }                            //access function

INT8 IMAGE::get_bps (  )  [inline]

Definition at line 103 of file img.h.

00103                    { 
00104       return bps;
00105     }                            //bits per sample

UINT8* IMAGE::get_buffer (  )  [inline]

Definition at line 119 of file img.h.

Referenced by main().

00119                         { 
00120       return image;
00121     }

void IMAGE::get_column ( INT32  x,
INT32  y,
INT32  height,
IMAGELINE linebuf,
INT32  margins 
)

Get a column of image into the supplied image line buffer.

The image is converted to 8bpp by simple assignment.

Definition at line 1072 of file imgs.cpp.

References bpp, bpp1table, bpp2table, bpp4table, bytespp, check_legal_access(), image, IMAGELINE::init(), IMAGELINE::line, pixel(), IMAGELINE::pixels, xdim, ymax, and ysize.

Referenced by scale_image().

01078                         {
01079   UINT8 *src;                    //source pointer
01080   UINT8 *dest;                   //destination pointer
01081   INT8 bit;                      //bit index
01082   INT8 pixperbyte;               //pixels per byte
01083   UINT8 white;                   //white colour
01084   INT32 pixel;                   //pixel index
01085 
01086                                  //test coords
01087   this->check_legal_access (x, y, 1);
01088                                  //test coords
01089   this->check_legal_access (x, y + height - 1, 1);
01090   if (height > ysize - y)
01091     height = ysize - y;          //clip to image
01092   linebuf->init (height * bytespp + margins * bytespp * 2);
01093                                  //start of line
01094   src = image + xdim * (ymax - 1 - y);
01095   dest = linebuf->line;          //destination line
01096   linebuf->pixels = dest;
01097   white = (1 << bpp) - 1;        //max value of pixel
01098   for (pixel = margins * bytespp; pixel > 0; pixel--) {
01099     *dest++ = white;             //margins are white
01100   }
01101   if (height > 0) {
01102     if (bpp == 24) {
01103       src += x * bytespp;        //offset
01104       for (; height > 0; --height) {
01105         *dest++ = *src;          //copy bytes
01106         *dest++ = *(src + 1);
01107         *dest++ = *(src + 2);
01108         src -= xdim;
01109       }
01110     }
01111     else if (bpp > 4) {
01112       src += x;
01113       for (; height > 0; --height) {
01114         *dest++ = *src;          //copy bytes
01115         src -= xdim;
01116       }
01117     }
01118     else if (bpp == 4) {
01119       src += x / 2;              //offset on line
01120       if (x & 1) {
01121         for (; height > 0; --height) {
01122                                  //get coded nibble
01123           *dest++ = bpp4table[*src][1];
01124           src -= xdim;
01125         }
01126       }
01127       else {
01128         for (; height > 0; --height) {
01129                                  //get coded nibble
01130           *dest++ = bpp4table[*src][0];
01131           src -= xdim;
01132         }
01133       }
01134     }
01135     else if (bpp == 2) {
01136       pixperbyte = 4;
01137       src += x / 4;              //offset on line
01138       bit = (INT8) (x % 4);      //offset in byte
01139       for (; height > 0; --height) {
01140                                  //get coded bits
01141         *dest++ = bpp2table[*src][bit];
01142         src -= xdim;
01143       }
01144     }
01145     else {
01146       pixperbyte = 8;
01147       src += x / 8;              //offset on line
01148       bit = (INT8) (x % 8);      //offset in byte
01149       for (; height > 0; --height) {
01150                                  //get coded bits
01151         *dest++ = bpp1table[*src][bit];
01152         src -= xdim;
01153       }
01154     }
01155   }
01156   for (pixel = margins * bytespp; pixel > 0; pixel--) {
01157     *dest++ = white;             //margins are white
01158   }
01159 }

void IMAGE::get_line ( INT32  x,
INT32  y,
INT32  width,
IMAGELINE linebuf,
INT32  margins 
)

Get a line of image into the supplied image line buffer.

The image is converted to 8bpp by simple assignment.

Definition at line 971 of file imgs.cpp.

References bpp, IMAGELINE::bpp, bpp1table, bpp2table, bpp4table, bytespp, check_legal_access(), image, IMAGELINE::init(), IMAGELINE::line, pixel(), IMAGELINE::pixels, xdim, xsize, and ymax.

Referenced by block_edges(), convolver(), copy_sub_image(), TessBaseAPI::DumpPGM(), fast_get_line(), starbase_to_normal(), and whiteout_block().

00977                       {
00978   UINT8 *src;                    //source pointer
00979   UINT8 *dest;                   //destination pointer
00980   UINT8 *unpacksrc;              //unpacking pointer
00981   INT8 bit;                      //bit index
00982   INT8 pixperbyte;               //pixels per byte
00983   UINT8 white;                   //white colour
00984   INT32 pixel;                   //pixel index
00985 
00986                                  //test coords
00987   this->check_legal_access (x, y, width);
00988   if (width > xsize - x)
00989     width = xsize - x;           //clip to image
00990   width *= bytespp;
00991   linebuf->init (width + margins * bytespp * 2);
00992   linebuf->bpp = bpp;
00993                                  //start of line
00994   src = image + xdim * (ymax - 1 - y);
00995   dest = linebuf->line;          //destination line
00996   linebuf->pixels = dest;
00997   white = (1 << bpp) - 1;        //max value of pixel
00998   for (pixel = margins * bytespp; pixel > 0; pixel--) {
00999     *dest++ = white;             //margins are white
01000   }
01001   if (width > 0) {
01002     if (bpp > 4) {
01003       src += x;                  //offset
01004                                  //easy way
01005       memmove (dest, src, (unsigned) width);
01006     }
01007     else if (bpp == 4) {
01008       src += x / 2;              //offset on line
01009       if (x & 1) {
01010                                  //get coded nibble
01011         *dest++ = bpp4table[*src++][1];
01012         width--;
01013       }
01014       while (width >= 2) {
01015                                  //get coded bits
01016         unpacksrc = bpp4table[*src++];
01017         *dest++ = *unpacksrc++;
01018         *dest++ = *unpacksrc++;  //copy nibbles
01019         width -= 2;
01020       }
01021       if (width) {
01022                                  //get coded nibble
01023         *dest++ = bpp4table[*src++][0];
01024       }
01025     }
01026     else if (bpp == 2) {
01027       pixperbyte = 4;
01028       src += x / 4;              //offset on line
01029       bit = (INT8) (x % 4);      //offset in byte
01030       width += bit;
01031       while (width > 0) {        //until all done
01032         if (width < pixperbyte)
01033                                  //less on last byte
01034           pixperbyte = (INT8) width;
01035                                  //get coded bits
01036         unpacksrc = &bpp2table[*src++][bit];
01037         for (; bit < pixperbyte; bit++)
01038           *dest++ = *unpacksrc++;//copy bytes
01039         width -= pixperbyte;
01040         bit = 0;
01041       }
01042     }
01043     else {
01044       pixperbyte = 8;
01045       src += x / 8;              //offset on line
01046       bit = (INT8) (x % 8);      //offset in byte
01047       width += bit;
01048       while (width > 0) {        //until all done
01049         if (width < pixperbyte)
01050                                  //less on last byte
01051           pixperbyte = (INT8) width;
01052                                  //get coded bits
01053         unpacksrc = &bpp1table[*src++][bit];
01054         for (; bit < pixperbyte; bit++)
01055           *dest++ = *unpacksrc++;//copy bytes
01056         width -= pixperbyte;
01057         bit = 0;
01058       }
01059     }
01060   }
01061   for (pixel = margins * bytespp; pixel > 0; pixel--) {
01062     *dest++ = white;             //margins are white
01063   }
01064 }

INT32 IMAGE::get_res (  )  [inline]

Definition at line 112 of file img.h.

Referenced by adapt_to_good_ems(), adapt_to_good_samples(), clip_sample(), collect_characters_for_adaption(), and collect_ems_for_adaption().

00112                     { 
00113       return res;
00114     }                            //access function

UINT8 IMAGE::get_white_level (  )  [inline]

Definition at line 109 of file img.h.

Referenced by convolver(), and starbase_to_normal().

00109                             {  //access function
00110       return (1 << bpp) - 1;
00111     }

INT32 IMAGE::get_xsize (  )  [inline]

Definition at line 92 of file img.h.

Referenced by CHAR_PROTO::add_sample(), CHAR_SAMPLES::add_sample_to_prototype(), bias_sub_image(), block_edges(), CHAR_SAMPLES::build_prototype(), PIXROW::char_clip_image(), char_clip_word(), CHAR_PROTO::CHAR_PROTO(), clip_sample(), display_clip_image(), display_image(), display_images(), do_re_display(), TessBaseAPI::DumpPGM(), edges_and_textord(), invert_image(), main(), match1(), matrix_match(), scale_image(), scale_image_cop_out(), smd_cmd(), and starbase_to_normal().

00092                       { 
00093       return xsize;
00094     }

INT32 IMAGE::get_ysize (  )  [inline]

Definition at line 96 of file img.h.

Referenced by CHAR_PROTO::add_sample(), CHAR_SAMPLES::add_sample_to_prototype(), bias_sub_image(), CHAR_SAMPLES::build_prototype(), PIXROW::char_clip_image(), char_clip_word(), CHAR_PROTO::CHAR_PROTO(), clip_sample(), display_clip_image(), display_image(), display_images(), TessBaseAPI::DumpPGM(), edges_and_textord(), invert_image(), main(), match1(), scale_image(), scale_image_cop_out(), smd_cmd(), starbase_to_normal(), write_cooked_text(), and write_shm_text().

00096                       { 
00097       return ysize;
00098     }

IMAGE & IMAGE::operator= ( IMAGE source  ) 

Assign an IMAGE to another

The dest becomes the owner of the memory.

Definition at line 80 of file imgs.cpp.

References bpp, bps, bufheight, bytespp, captured, destroy(), fd, image, lineskip, photo_interp, reader, res, TRUE, xdim, xsize, ymax, ymin, and ysize.

00082   {
00083   destroy(); 
00084   bpp = source.bpp;
00085   photo_interp = source.photo_interp;
00086   bps = source.bps;
00087   bytespp = (bpp + 7) / 8;
00088   lineskip = source.lineskip;    //copy everything
00089   captured = source.captured;
00090   xsize = source.xsize;
00091   ysize = source.ysize;
00092   res = source.res;
00093   image = source.image;
00094   xdim = source.xdim;
00095   bufheight = source.bufheight;
00096   fd = source.fd;
00097   reader = source.reader;
00098   ymin = source.ymin;
00099   ymax = source.ymax;
00100 
00101   source.captured = TRUE;        //source now captured
00102   source.fd = -1;
00103 
00104   return *this;
00105 }

UINT8 IMAGE::pixel ( INT32  x,
INT32  y 
)

Get a single pixel out of the image.

Definition at line 183 of file imgs.cpp.

References bpp, bpp1table, bpp2table, bpp4table, check_legal_access(), image, tprintf(), xdim, xsize, ymax, and ysize.

Referenced by get_column(), get_line(), put_column(), and put_line().

00186                     {
00187   if (x < 0)
00188     x = 0;                       //silently clip
00189   else if (x >= xsize)
00190     x = xsize - 1;
00191   if (y < 0)
00192     y = 0;
00193   else if (y >= ysize)
00194     y = ysize - 1;
00195   check_legal_access (x, y, 1);
00196   switch (bpp) {
00197     case 5:
00198     case 6:
00199     case 8:
00200       return image[(ymax - 1 - y) * xdim + x];
00201     case 4:
00202       return bpp4table[image[(ymax - 1 - y) * xdim + x / 2]][x & 1];
00203     case 2:
00204       return bpp2table[image[(ymax - 1 - y) * xdim + x / 4]][x & 3];
00205     case 1:
00206       return bpp1table[image[(ymax - 1 - y) * xdim + x / 8]][x & 7];
00207     default:
00208       tprintf ("Unexpected bits per pixel %d\n", bpp);
00209       return 0;
00210   }
00211 }

void IMAGE::put_column ( INT32  x,
INT32  y,
INT32  height,
IMAGELINE linebuf,
INT32  margins 
)

Put the supplied column buffer into the image.

The image is converted from 8bpp by simple assignment.

Definition at line 1332 of file imgs.cpp.

References bpp, IMAGELINE::bpp, check_legal_access(), image, pixel(), IMAGELINE::pixels, xdim, ymax, and ysize.

Referenced by scale_image().

01338                         {
01339   UINT8 *src;                    //source pointer
01340   UINT8 *dest;                   //destination pointer
01341   INT8 bit;                      //bit index
01342   UINT8 pixel;                   //collected bits
01343   INT8 bytesperpix;              //in source
01344 
01345   this->check_legal_access (x, y, 1);
01346   this->check_legal_access (x, y + height - 1, 1);
01347   if (height > ysize - y)
01348     height = ysize - y;          //clip to image
01349   if (height <= 0)
01350     return;                      //nothing to do
01351                                  //source line
01352   src = linebuf->pixels + margins;
01353                                  //start of line
01354   dest = image + xdim * (ymax - 1 - y);
01355 
01356   if (linebuf->bpp == 24) {
01357     src++;
01358     bytesperpix = 3;
01359   }
01360   else
01361     bytesperpix = 1;
01362 
01363   if (bpp == 24 && linebuf->bpp == 24) {
01364     dest += x * bytesperpix;
01365     src--;
01366     for (; height > 0; --height) {
01367       *dest = *src++;
01368       *(dest + 1) = *src++;
01369       *(dest + 2) = *src++;
01370       dest -= xdim;
01371     }
01372   }
01373   else if (bpp == 24) {
01374     src--;
01375     dest += x * bytesperpix;
01376     for (; height > 0; --height) {
01377       pixel = *src++;
01378       *dest = pixel;
01379       *(dest + 1) = pixel;
01380       *(dest + 2) = pixel;
01381       dest -= xdim;
01382     }
01383   }
01384   else if (bpp > 4) {
01385     dest += x;                   //offset
01386     for (; height > 0; --height) {
01387       *dest = *src;
01388       src += bytesperpix;
01389       dest -= xdim;
01390     }
01391   }
01392   else if (bpp == 4) {
01393     dest += x / 2;               //offset on line
01394     if (x & 1) {
01395       for (; height > 0; --height) {
01396         *dest &= 0xf0;           //clean odd byte
01397         *dest |= *src & 0x0f;    //and copy it
01398         src += bytesperpix;
01399         dest -= xdim;
01400       }
01401     }
01402     else {
01403       for (; height > 0; --height) {
01404         *dest &= 0x0f;           //clean odd byte
01405         *dest |= *src << 4;
01406         src += bytesperpix;
01407         dest -= xdim;
01408       }
01409     }
01410   }
01411   else if (bpp == 2) {
01412     dest += x / 4;               //offset on line
01413     bit = (INT8) (x % 4);        //offset in byte
01414     bit = 6 - bit - bit;         //bit shift
01415     pixel = ~(3 << bit);         //mask
01416     for (; height > 0; --height) {
01417                                  //change 2 bits
01418       *dest = (*dest & pixel) | ((*src & 3) << bit);
01419       src += bytesperpix;
01420       dest -= xdim;
01421     }
01422   }
01423   else {
01424     dest += x / 8;               //offset on line
01425     bit = (INT8) (x % 8);        //offset in byte
01426     bit = 7 - bit;
01427     pixel = ~(1 << bit);
01428     for (; height > 0; --height) {
01429                                  //change 1 bit
01430       *dest = (*dest & pixel) | ((*src & 1) << bit);
01431       src += bytesperpix;
01432       dest -= xdim;
01433     }
01434   }
01435 }

void IMAGE::put_line ( INT32  x,
INT32  y,
INT32  width,
IMAGELINE linebuf,
INT32  margins 
)

Put the supplied line buffer into the image.

The image is converted from 8bpp by simple assignment.

Definition at line 1185 of file imgs.cpp.

References bpp, IMAGELINE::bpp, bytespp, check_legal_access(), image, pixel(), IMAGELINE::pixels, xdim, xsize, and ymax.

Referenced by PIXROW::char_clip_image(), convolver(), copy_sub_image(), enlarge_sub_image(), fast_put_line(), fast_reduce_sub_image(), read_tif_image(), reduce_sub_image(), scale_image(), starbase_to_normal(), TessBaseAPI::ThresholdRect(), and whiteout_block().

01191                       {
01192   UINT8 *src;                    //source pointer
01193   UINT8 *dest;                   //destination pointer
01194   INT8 bit;                      //bit index
01195   UINT8 pixel;                   //collected bits
01196   INT8 pixperbyte;               //pixels in a byte
01197   INT8 bytesperpix;              //in source
01198 
01199   this->check_legal_access (x, y, width);
01200   if (width > xsize - x)
01201     width = xsize - x;           //clip to image
01202   if (width <= 0)
01203     return;                      //nothing to do
01204                                  //source line
01205   src = linebuf->pixels + margins;
01206                                  //start of line
01207   dest = image + xdim * (ymax - 1 - y);
01208 
01209   if (linebuf->bpp == 24) {
01210     src++;
01211     bytesperpix = 3;
01212   }
01213   else
01214     bytesperpix = 1;
01215   if (bpp == 24 && linebuf->bpp == 24) {
01216     dest += x * bytespp;
01217     width *= bytespp;
01218     memmove (dest, src - 1, (unsigned) width);
01219   }
01220   else if (bpp == 24) {
01221     src--;
01222     dest += x * bytespp;
01223     while (width > 0) {
01224       pixel = *src++;
01225       *dest++ = pixel;
01226       *dest++ = pixel;
01227       *dest++ = pixel;
01228       width--;
01229     }
01230   }
01231   else if (bpp > 4) {
01232     dest += x;                   //offset
01233     if (linebuf->bpp == 24) {
01234       while (width > 0) {
01235         *dest++ = *src;
01236         src += 3;
01237         width--;
01238       }
01239     }
01240     else
01241                                  //easy way
01242       memmove (dest, src, (unsigned) width);
01243   }
01244   else if (bpp == 4) {
01245     dest += x / 2;               //offset on line
01246     if (x & 1) {
01247       *dest &= 0xf0;             //clean odd byte
01248       *dest++ |= *src & 0x0f;    //and copy it
01249       src += bytesperpix;
01250       width--;
01251     }
01252     while (width >= 2) {
01253       pixel = *src << 4;         //left pixel
01254       src += bytesperpix;
01255       pixel |= *src & 0x0f;      //right pixel
01256       src += bytesperpix;
01257       *dest++ = pixel;
01258       width -= 2;
01259     }
01260     if (width) {
01261       *dest &= 0x0f;             //clean odd byte
01262       *dest |= *src << 4;
01263     }
01264   }
01265   else if (bpp == 2) {
01266     pixperbyte = 4;
01267     dest += x / 4;               //offset on line
01268     bit = (INT8) (x % 4);        //offset in byte
01269     width += bit;
01270     pixel = *dest >> (8 - bit - bit);
01271     while (width >= 4) {         //until all done
01272       for (; bit < 4; bit++) {
01273         pixel <<= 2;             //make space for new one
01274         pixel |= *src & 3;
01275         src += bytesperpix;
01276       }
01277       *dest++ = pixel;           //new pixel
01278       width -= 4;
01279       bit = 0;
01280     }
01281     if (width > 0) {             //until all done
01282       for (bit = 0; bit < width; bit++) {
01283         pixel <<= 2;             //make space for new one
01284         pixel |= *src & 3;
01285         src += bytesperpix;
01286       }
01287       pixel <<= (8 - bit - bit); //shift rest
01288                                  //keep trainling bits
01289       pixel |= *dest & ((1 << (8 - bit - bit)) - 1);
01290       *dest++ = pixel;           //new pixel
01291     }
01292   }
01293   else {
01294     pixperbyte = 8;
01295     dest += x / 8;               //offset on line
01296     bit = (INT8) (x % 8);        //offset in byte
01297     width += bit;
01298     pixel = *dest >> (8 - bit);
01299     while (width >= 8) {         //until all done
01300       for (; bit < 8; bit++) {
01301         pixel <<= 1;             //make space for new one
01302         pixel |= *src & 1;
01303         src += bytesperpix;
01304       }
01305       *dest++ = pixel;           //new pixel
01306       width -= 8;
01307       bit = 0;
01308     }
01309     width -= bit;
01310     if (width > 0) {             //until all done
01311       while (width > 0) {
01312         pixel <<= 1;             //make space for new one
01313         pixel |= *src & 1;
01314         src += bytesperpix;
01315         bit++;
01316         width--;
01317       }
01318       pixel <<= (8 - bit);       //shift rest
01319                                  //keep trainling bits
01320       pixel |= *dest & ((1 << (8 - bit)) - 1);
01321       *dest++ = pixel;           //new pixel
01322     }
01323   }
01324 }

INT8 IMAGE::read ( INT32  buflines  ) 

Read a previously opened image file into memory.

If buflines is 0, the whole image is read in one go. If buflines>0, memory space is reserved for reading just that many lines at once.

As soon as a request is made to get a line past the end of the buffer, the buffer is reread with a 50% overlap. Backward seeks are not allowed. Read returns \-1 in case of failure or 0 if successful.

Definition at line 174 of file imgio.cpp.

References ABORT, alloc_big_mem(), bpp, bufheight, captured, destroy(), ERRCODE::error(), FALSE, fd, image, IMAGEUNDEFINED, lineskip, LOG, MEMORY_OUT, NULL, reader, READFAILED, xdim, xsize, ymax, ymin, and ysize.

Referenced by bufread(), edges_and_textord(), and main().

00176                   {
00177   INT32 row;                     //image row
00178   BOOL8 failed;                  //read failed
00179 
00180   if (fd < 0 || image != NULL)
00181     IMAGEUNDEFINED.error ("IMAGE::read", ABORT, NULL);
00182 
00183   if (buflines <= 0 || buflines > ysize || reader == NULL)
00184     buflines = ysize;            //default to all
00185   bufheight = buflines;
00186   image =
00187     (UINT8 *) alloc_big_mem ((size_t) (xdim * bufheight * sizeof (UINT8)));
00188   if (image == NULL) {
00189     MEMORY_OUT.error ("IMAGE::read", LOG, NULL);
00190     destroy(); 
00191     return -1;
00192   }
00193   captured = FALSE;
00194   ymax = ysize;
00195   ymin = ysize - buflines;       //amount of image read
00196   if (reader != NULL && lineskip < 0)
00197     failed = (*reader) (fd, image, xsize, ysize, bpp, xdim) < 0;
00198   else {
00199     if (lineskip == 0)
00200       failed =::read (fd, (char *) image,
00201         (size_t) (xdim * bufheight)) != xdim * bufheight;
00202     else {
00203       for (failed = FALSE, row = 0; row < bufheight && !failed; row++) {
00204         failed =::read (fd, (char *) image + row * xdim,
00205           (size_t) xdim) != xdim;
00206         failed |= lseek (fd, lineskip, SEEK_CUR) < 0;
00207       }
00208     }
00209   }
00210   if (failed) {
00211     READFAILED.error ("IMAGE::read", LOG, NULL);
00212     destroy(); 
00213     return -1;                   //read failed
00214   }
00215   if (ymin <= 0) {
00216     close(fd);  //finished reading
00217     fd = -1;                     //not open now
00218   }
00219   return 0;                      //success
00220 }

INT8 IMAGE::read_header ( const char *  name  ) 

Read the header of an image, typed according to the extension of the name.

Returns:
Return is 0 for success, \-1 for failure.

Definition at line 117 of file imgio.cpp.

References bpp, bps, bytespp, CANTOPENFILE, CANTREADIMAGETYPE, COMPUTE_IMAGE_XDIM, destroy(), ERRCODE::error(), fd, imagetypes, lineskip, LOG, name_to_image_type(), NULL, IMAGETYPE::opener, photo_interp, IMAGETYPE::reader, reader, res, xdim, xsize, and ysize.

Referenced by edges_and_textord(), and main().

00119                          {
00120   INT8 type;                     //image type
00121 
00122   destroy();  //destroy old image
00123                                  //get type
00124   type = name_to_image_type (name);
00125   if (type < 0 || imagetypes[type].opener == NULL) {
00126     CANTREADIMAGETYPE.error ("IMAGE::read_header", LOG, name);
00127     return -1;                   //read not supported
00128   }
00129   #ifdef __UNIX__
00130   if ((fd = open (name, O_RDONLY)) < 0)
00131   #endif
00132   #if defined (__MSW32__) || defined (__MAC__)
00133     if ((fd = open (name, O_RDONLY | O_BINARY)) < 0)
00134   #endif
00135   {
00136     CANTOPENFILE.error ("IMAGE::read_header", LOG, name);
00137     return -1;                   //failed
00138   }
00139   lineskip =
00140     (*imagetypes[type].opener) (fd, &xsize, &ysize, &bpp, &photo_interp,
00141     &res);
00142   if (lineskip == -1) {
00143                                  //get header
00144     bpp = 0;                     //still empty
00145     close(fd); 
00146     fd = -1;
00147     return -1;                   //failed
00148   }
00149   if (res <= 0)
00150     res = image_default_resolution;
00151   //      fprintf(stderr,"Image size=(%d,%d), bpp=%d\n",
00152   //              xsize,ysize,bpp);
00153                                  //bytes per line
00154   xdim = COMPUTE_IMAGE_XDIM (xsize, bpp);
00155   bps = bpp == 24 ? 8 : bpp;
00156   bytespp = (bpp + 7) / 8;
00157                                  //funtion to read with
00158   reader = imagetypes[type].reader;
00159   return 0;                      //success
00160 }

void IMAGE::set_res ( INT32  resolution  )  [inline]

Definition at line 115 of file img.h.

00116                                    {
00117       res = resolution;
00118     }

BOOL8 IMAGE::white_high (  )  [inline]

Definition at line 106 of file img.h.

Referenced by edges_and_textord().

00106                        {  //photo interp
00107       return photo_interp;
00108     }

INT8 IMAGE::write ( const char *  name  ) 

Write an image to a file in a format determined by the name.

Definition at line 274 of file imgio.cpp.

References ABORT, bpp, bufheight, CANTCREATEFILE, CANTWRITEIMAGETYPE, creat(), ERRCODE::error(), fd, image, imagetypes, IMAGEUNDEFINED, LOG, name_to_image_type(), NULL, photo_interp, res, WRITEFAILED, xsize, and ysize.

00276                    {
00277   INT8 type;                     //type of image
00278 
00279   if (bpp == 0 || image == NULL || bufheight != ysize)
00280     IMAGEUNDEFINED.error ("IMAGE::write", ABORT, NULL);
00281   if (fd >= 0) {
00282     close(fd);  //close old file
00283     fd = -1;                     //no longer open
00284   }
00285                                  //get image type
00286   type = name_to_image_type (name);
00287   if (type < 0 || imagetypes[type].writer == NULL) {
00288     CANTWRITEIMAGETYPE.error ("IMAGE::write", LOG, name);
00289     return -1;                   //write not supported
00290   }
00291   #ifdef __UNIX__
00292   if ((fd = creat (name, 0666)) < 0)
00293   #endif
00294   #ifdef __MSW32__
00295     if ((fd = open (name, _O_CREAT | _O_WRONLY | _O_BINARY, _S_IWRITE)) < 0)
00296   #endif
00297   #ifdef __MAC__
00298       if ((fd = creat (name, O_WRONLY | O_BINARY)) < 0)
00299   #endif
00300   {
00301     CANTCREATEFILE.error ("IMAGE::write", LOG, name);
00302     return -1;                   //failed
00303   }
00304   if (res <= 0)
00305     res = image_default_resolution;
00306   if ((*imagetypes[type].writer) (fd, image, xsize, ysize, bpp, photo_interp,
00307   res) < 0) {
00308                                  //get header
00309                                  //write failed
00310     WRITEFAILED.error ("IMAGE::write", LOG, name);
00311     close(fd); 
00312     fd = -1;
00313     return -1;                   //failed
00314   }
00315   return 0;                      //success
00316 }


Friends And Related Function Documentation

DLLSYM void copy_sub_image ( IMAGE source,
INT32  xstart,
INT32  ystart,
INT32  xext,
INT32  yext,
IMAGE dest,
INT32  xdest,
INT32  ydest,
BOOL8  adjust_grey 
) [friend]

Copy a portion of one image to a portion of another image.

If the bpps are different, the position of the most significant bit is preserved.

Definition at line 245 of file imgs.cpp.

Referenced by bufread().

00255                             {
00256   IMAGELINE copyline;            //copy of line
00257   UINT8 *copy;                   //source pointer
00258   INT8 shift;                    //shift factor
00259   INT32 pixel;                   //pixel index
00260   INT32 y;                       //line index
00261   INT32 yoffset;                 //current adjusted offset
00262   INT32 bytesize;                //no of bytes to copy
00263   INT32 srcppb;                  //pixels per byte
00264   BOOL8 aligned;
00265 
00266   if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
00267     return;
00268   if (xext <= 0)
00269     xext = source->xsize;        //default to all
00270   if (xext > source->xsize - xstart)
00271                                  //clip to smallest
00272       xext = source->xsize - xstart;
00273   if (xext > dest->xsize - xdest)
00274     xext = dest->xsize - xdest;
00275   if (yext <= 0)
00276     yext = source->ysize;        //default to all
00277   if (yext > source->ysize - ystart)
00278                                  //clip to smallest
00279       yext = source->ysize - ystart;
00280   if (yext > dest->ysize - ydest)
00281     yext = dest->ysize - ydest;
00282   if (xext <= 0 || yext <= 0)
00283     return;                      //nothing to do
00284 
00285   srcppb = 8 / source->bpp;      //pixels per byte
00286   if (source->bpp == dest->bpp || !adjust_grey)
00287     shift = 0;                   //no adjustment
00288   else {
00289     shift = source->bps - dest->bps;
00290     if (shift < 0)
00291       shift = -shift;            //keep positive
00292   }
00293   aligned = source->bpp == dest->bpp;
00294   if (aligned && srcppb != 0) {
00295     aligned = xstart % srcppb == 0
00296       && xdest % srcppb == 0
00297       && (xext % srcppb == 0 || xdest + xext == dest->xsize);
00298   }
00299   for (y = 0; y < yext; y++) {
00300     if (ystart >= ydest)
00301       yoffset = y;               //top down
00302     else
00303       yoffset = yext - y - 1;    //bottom up
00304     source->check_legal_access (xstart, ystart + yoffset, xext);
00305     dest->check_legal_access (xdest, ydest + yoffset, xext);
00306     if (aligned) {
00307       bytesize = COMPUTE_IMAGE_XDIM (xext, source->bpp);
00308       //get bytes per line
00309       if (srcppb == 0)
00310                                  //do cheap move
00311         memmove (dest->image + (dest->ymax - 1 - ydest - yoffset)
00312          * dest->xdim + xdest * 3, source->image 
00313          + (source->ymax - 1 - ystart - yoffset) 
00314          * source->xdim + xstart * 3, (unsigned) bytesize);
00315       else
00316                                  //do cheap move
00317         memmove (dest->image
00318          + (dest->ymax - 1 - ydest - yoffset)
00319          * dest->xdim + xdest / srcppb, source->image 
00320          + (source->ymax - 1 - ystart - yoffset) 
00321          * source->xdim + xstart / srcppb, (unsigned) bytesize);
00322     }
00323     else {
00324       if (shift == 0) {
00325         source->fast_get_line (xstart, ystart + yoffset, xext,
00326           &copyline);
00327       }
00328       else if (source->bpp < dest->bpp) {
00329         source->get_line (xstart, ystart + yoffset, xext, &copyline, 0);
00330         if (source->bpp <= shift
00331         && (source->bpp == 1 || source->bpp == 4)) {
00332           if (source->bpp == 1) {
00333             for (pixel = 0, copy = copyline.pixels; pixel < xext;
00334               pixel++, copy++)
00335             if (*copy)
00336               *copy = 0xff;
00337           }
00338           else {
00339             for (pixel = 0, copy = copyline.pixels; pixel < xext;
00340               pixel++, copy++)
00341                                  //scale up
00342             *copy = (*copy << shift) | *copy;
00343           }
00344         }
00345         else {
00346           for (pixel = 0, copy = copyline.pixels; pixel < xext;
00347             pixel++)
00348           *copy++ <<= shift;     //scale up
00349         }
00350       }
00351       else {
00352         source->get_line (xstart, ystart + yoffset, xext, &copyline, 0);
00353         if (source->bpp == 24) {
00354           for (pixel = 0, copy = copyline.pixels + 1; pixel < xext;
00355           pixel++) {
00356             *copy >>= shift;
00357             copy += 3;
00358           }
00359         }
00360         else {
00361           for (pixel = 0, copy = copyline.pixels; pixel < xext;
00362             pixel++)
00363           *copy++ >>= shift;     //scale down
00364         }
00365       }
00366       dest->put_line (xdest, ydest + yoffset, xext, &copyline, 0);
00367     }
00368   }
00369 }

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 
) [friend]

Enlarge rectangle

Enlarge a portion of one image to a portion of another image. If the bpps are different, the position of the most significant bit is preserved.

Definition at line 379 of file imgs.cpp.

00390                                {
00391   INT8 shift;                    //shift factor
00392   UINT8 pixel;                   //current pixel
00393   INT32 srcext;                  //source extent
00394   INT32 xoffset;                 //column index
00395   INT32 yoffset;                 //line index
00396   INT32 xindex, yindex;          //index in super pixel
00397   INT32 startxindex;             //initial x index
00398   INT32 xscale;                  //x scale factor
00399   UINT8 *src;                    //source pixels
00400   UINT8 *destpix;                //dest pixels
00401   IMAGELINE copyline;            //copy of line
00402   IMAGELINE bigline;             //expanded line
00403 
00404   if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
00405     return;
00406 
00407   if (xext <= 0)
00408     xext = dest->xsize;          //default to all
00409   if (xext > source->xsize * scale - xstart)
00410                                  //clip to smallest
00411     xext = source->xsize * scale - xstart;
00412   if (xext > dest->xsize - xdest)
00413     xext = dest->xsize - xdest;
00414   if (yext <= 0)
00415     yext = dest->ysize;          //default to all
00416   if (yext > source->ysize * scale - ystart)
00417     yext = source->ysize * scale - ystart;
00418   if (yext > dest->ysize - ydest)
00419     yext = dest->ysize - ydest;
00420   if (xext <= 0 || yext <= 0)
00421     return;                      //nothing to do
00422 
00423   xindex = xstart % scale;       //offset in super pixel
00424   startxindex = xindex;
00425   yindex = ystart % scale;
00426                                  //no of source pixels
00427   srcext = (xext + xindex + scale - 1) / scale;
00428   xstart /= scale;               //actual start
00429   ystart /= scale;
00430   if (adjust_grey) {
00431     shift = dest->bps - source->bps;
00432   }
00433   else
00434     shift = 0;                   //no adjustment
00435   bigline.init (xext * 3);
00436   bigline.bpp = dest->bpp == 24 ? source->bpp : dest->bpp;
00437 
00438   for (yoffset = 0; yoffset < yext; ystart++) {
00439     source->check_legal_access (xstart, ystart, srcext);
00440     dest->check_legal_access (xdest, ydest + yoffset, xext);
00441     source->fast_get_line (xstart, ystart, srcext, &copyline);
00442     src = copyline.pixels;
00443     destpix = bigline.pixels;
00444     xscale = scale;              //enlargement factor
00445     if (source->bpp == 24 && dest->bpp == 24) {
00446       for (xoffset = 0, xindex = startxindex; xoffset < xext;
00447       src += source->bytespp) {
00448         xoffset += xscale - xindex;
00449         if (xoffset > xext)
00450           xscale -= xoffset - xext;
00451         for (; xindex < xscale; xindex++) {
00452           *destpix++ = *src;
00453           *destpix++ = *(src + 1);
00454           *destpix++ = *(src + 2);
00455         }
00456         xindex = 0;
00457       }
00458     }
00459     else {
00460       if (source->bpp == 24)
00461         src++;
00462       for (xoffset = 0, xindex = startxindex; xoffset < xext;
00463       src += source->bytespp) {
00464         xoffset += xscale - xindex;
00465         if (xoffset > xext)
00466                                  //clip to dest limit
00467             xscale -= xoffset - xext;
00468         if (shift == 0)
00469           pixel = *src;
00470         else if (shift > 0)
00471           pixel = *src << shift;
00472         else
00473           pixel = *src >> (-shift);
00474         for (; xindex < xscale; xindex++)
00475           *destpix++ = pixel;    //duplicate pixel
00476         xindex = 0;
00477       }
00478     }
00479     for (; yoffset < yext && yindex < scale; yindex++, yoffset++) {
00480       dest->put_line (xdest, ydest + yoffset, xext, &bigline, 0);
00481     }
00482     yindex = 0;
00483   }
00484 }

DLLSYM void fast_reduce_sub_image ( IMAGE source,
INT32  xstart,
INT32  ystart,
INT32  xext,
INT32  yext,
IMAGE dest,
INT32  xdest,
INT32  ydest,
INT32  scale,
BOOL8  adjust_grey 
) [friend]

Reduce a portion of one image to a portion of another image.

If the bpps are different, the position of the most significant bit is preserved.

This is a fast but dirty version, which simply sub-samples. It does not smooth as it reduces.

Definition at line 496 of file imgs.cpp.

00507                                    {
00508   INT8 shift;                    //shift factor
00509   INT32 xfactor;                 //run on x coord
00510   INT32 divisor;                 //total cell area
00511   INT32 xindex, yindex;          //into averaging square
00512   INT32 xcoord;                  //current x coord
00513   INT32 destext;                 //destination size
00514   INT32 yoffset;                 //current adjusted offset
00515   UINT8 *pixel;                  //ptr to source pixels
00516   INT32 *sums;                   //ptr to sums array
00517   IMAGELINE copyline;            //copy of line
00518   INT32 *linesums;               //averaging sums
00519 
00520   if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
00521     return;
00522   if (xext <= 0)
00523     xext = source->xsize;        //default to all
00524   if (xext > source->xsize - xstart)
00525                                  //clip to smallest
00526       xext = source->xsize - xstart;
00527   if (xext > (dest->xsize - xdest) * scale)
00528     xext = (dest->xsize - xdest) * scale;
00529   if (yext <= 0)
00530     yext = source->ysize;        //default to all
00531   if (yext > source->ysize - ystart)
00532                                  //clip to smallest
00533       yext = source->ysize - ystart;
00534   if (yext > (dest->ysize - ydest) * scale)
00535     yext = (dest->ysize - ydest) * scale;
00536   if (xext <= 0 || yext <= 0)
00537     return;                      //nothing to do
00538 
00539   xfactor = xext % scale;        //left overs
00540   if (xfactor == 0)
00541     xfactor = scale;
00542                                  //destination pixels
00543   destext = (xext + scale - 1) / scale;
00544   if (adjust_grey)
00545                                  //shift factor
00546     shift = dest->bps - source->bps;
00547   else
00548     shift = 0;                   //no adjustment
00549   linesums = new INT32[destext * source->bytespp];
00550 
00551   for (yoffset = 0; yoffset < yext; ydest++) {
00552     source->check_legal_access (xstart, ystart + yoffset, xext);
00553     dest->check_legal_access (xdest, ydest, destext);
00554     for (xindex = destext * source->bytespp - 1; xindex >= 0; xindex--)
00555       linesums[xindex] = 0;      //zero sums
00556     for (yindex = 0; yindex < scale
00557     && ystart + yoffset < source->ysize; yindex += 3) {
00558       source->fast_get_line (xstart, ystart + yoffset, xext, &copyline);
00559       pixel = copyline.pixels;   //start of line
00560       if (source->bpp == 24) {
00561         for (xcoord = 1, sums = linesums; xcoord < destext;
00562         xcoord++, sums += 3) {
00563           for (xindex = 0; xindex < scale; xindex += 2) {
00564             *sums += *pixel++;
00565             *(sums + 1) += *pixel++;
00566             *(sums + 2) += *pixel++;
00567             pixel += 3;
00568           }
00569           if (scale & 1)
00570             pixel -= 3;          //correct position
00571         }
00572         for (xindex = 0; xindex < xfactor; xindex += 2) {
00573           *sums += *pixel++;
00574           *(sums + 1) += *pixel++;
00575           *(sums + 2) += *pixel++;
00576           pixel += 3;
00577         }
00578       }
00579       else {
00580         for (xcoord = 1, sums = linesums; xcoord < destext;
00581         xcoord++, sums++) {
00582           for (xindex = 0; xindex < scale; xindex += 2) {
00583             *sums += *pixel;
00584             pixel += 2;
00585           }
00586           if (scale & 1)
00587             pixel--;             //correct position
00588         }
00589         for (xindex = 0; xindex < xfactor; xindex += 2) {
00590           *sums += *pixel;
00591           pixel += 2;
00592         }
00593       }
00594       yoffset += 3;              //every 3 lines
00595     }
00596     if (yindex > scale)
00597       yoffset -= yindex - scale; //back on right scale
00598     copyline.init ();            //set pixels back to array
00599     copyline.bpp = source->bpp;
00600     pixel = copyline.pixels;
00601                                  //pixels in block
00602     divisor = ((yindex + 2) / 3) * ((scale + 1) / 2);
00603     if (shift <= 0) {
00604       divisor <<= (-shift);      //do greyscale correction
00605       for (sums = linesums, xindex = (destext - 1) * source->bytespp;
00606         xindex > 0; xindex--)
00607                                  //turn to destination value
00608       *pixel++ = (UINT8) (*sums++ / divisor);
00609       for (xindex = source->bytespp; xindex > 0; xindex--)
00610         *pixel++ = *sums++
00611           / (((yindex + 2) / 3) * ((xfactor + 1) / 2) << (-shift));
00612       //lastone different
00613     }
00614     else {
00615       for (sums = linesums, xindex = (destext - 1) * source->bytespp;
00616         xindex > 0; xindex--)
00617       *pixel++ = (UINT8) ((*sums++ << shift) / divisor);
00618       //destination value
00619       for (xindex = source->bytespp; xindex > 0; xindex--)
00620                                  //last one different
00621         *pixel++ = (*(sums++) << shift) /
00622          (((yindex + 2) / 3) * ((xfactor + 1) / 2));
00623     }
00624                                  //put in destination
00625     dest->put_line (xdest, ydest, destext, &copyline, 0);
00626   }
00627   delete linesums;
00628 }

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 
) [friend]

Reduce a portion of one image to a portion of another image.

If the bpps are different, the position of the most significant bit is preserved.

Definition at line 637 of file imgs.cpp.

00648                               {
00649   INT8 shift;                    //shift factor
00650   INT32 xfactor;                 //run on x coord
00651   INT32 divisor;                 //total cell area
00652   INT32 div2;                    //total cell area divided by 2
00653   INT32 xindex, yindex;          //into averaging square
00654   INT32 xcoord;                  //current x coord
00655   INT32 destext;                 //destination size
00656   INT32 yoffset;                 //current adjusted offset
00657   UINT8 *pixel;                  //ptr to source pixels
00658   INT32 *sums;                   //ptr to sums array
00659   IMAGELINE copyline;            //copy of line
00660   INT32 *linesums;               //averaging sums
00661 
00662   if (xstart < 0 || ystart < 0 || xdest < 0 || ydest < 0)
00663     return;
00664   if (xext <= 0)
00665     xext = source->xsize;        //default to all
00666   if (xext > source->xsize - xstart)
00667                                  //clip to smallest
00668       xext = source->xsize - xstart;
00669   if (xext > (dest->xsize - xdest) * scale)
00670     xext = (dest->xsize - xdest) * scale;
00671   if (yext <= 0)
00672     yext = source->ysize;        //default to all
00673   if (yext > source->ysize - ystart)
00674                                  //clip to smallest
00675       yext = source->ysize - ystart;
00676   if (yext > (dest->ysize - ydest) * scale)
00677     yext = (dest->ysize - ydest) * scale;
00678   if (xext <= 0 || yext <= 0)
00679     return;                      //nothing to do
00680 
00681   xfactor = xext % scale;        //left overs
00682   if (xfactor == 0)
00683     xfactor = scale;
00684                                  //destination pixels
00685   destext = (xext + scale - 1) / scale;
00686   if (adjust_grey)
00687                                  //shift factor
00688     shift = dest->bps - source->bps;
00689   else
00690     shift = 0;                   //no adjustment
00691   linesums = new INT32[destext * source->bytespp];
00692 
00693   for (yoffset = 0; yoffset < yext; ydest++) {
00694     source->check_legal_access (xstart, ystart + yoffset, xext);
00695     dest->check_legal_access (xdest, ydest, destext);
00696     for (xindex = 0; xindex < (destext) * source->bytespp; xindex++)
00697       linesums[xindex] = 0;      //zero sums
00698     for (yindex = 0; yindex < scale && ystart + yoffset < source->ysize;
00699     yindex++) {
00700       source->fast_get_line (xstart, ystart + yoffset, xext, &copyline);
00701       pixel = copyline.pixels;   //start of line
00702       if (source->bpp == 24) {
00703         for (xcoord = 1, sums = linesums; xcoord < destext;
00704         xcoord++, sums += 3) {
00705           for (xindex = 0; xindex < scale; xindex++) {
00706             *sums += *pixel++;
00707             *(sums + 1) += *pixel++;
00708             *(sums + 2) += *pixel++;
00709           }
00710         }
00711         for (xindex = 0; xindex < xfactor; xindex++) {
00712           *sums += *pixel++;
00713           *(sums + 1) += *pixel++;
00714           *(sums + 2) += *pixel++;
00715         }
00716       }
00717       else {
00718         for (xcoord = 1, sums = linesums; xcoord < destext;
00719         xcoord++, sums++) {
00720           for (xindex = 0; xindex < scale; xindex++)
00721             *sums += *pixel++;
00722         }
00723         for (xindex = 0; xindex < xfactor; xindex++)
00724           *sums += *pixel++;
00725       }
00726       yoffset++;                 //next line
00727     }
00728     copyline.init ();            //set pixels back to array
00729     copyline.set_bpp (source->bpp);
00730     pixel = copyline.pixels;
00731     divisor = yindex * scale;
00732     if (divisor == 0) {
00733       tprintf
00734         ("Impossible:divisor=0!, yindex=%d, scale=%d, yoffset=%d,yext=%d\n",
00735         yindex, scale, yoffset, yext);
00736       break;
00737     }
00738     if (shift <= 0) {
00739       divisor <<= (-shift);      //do greyscale correction
00740       div2 = divisor / 2;
00741       for (sums = linesums, xindex = (destext - 1) * source->bytespp;
00742         xindex > 0; xindex--)
00743       *pixel++ = (UINT8) ((div2 + *sums++) / divisor);
00744       //turn to destination value
00745       div2 = (yindex * xfactor << (-shift)) / 2;
00746       for (xindex = source->bytespp; xindex > 0; xindex--)
00747         *pixel++ =
00748           (UINT8) ((div2 + *sums++) / (yindex * xfactor << (-shift)));
00749       //lastone different
00750     }
00751     else {
00752       div2 = divisor / 2;
00753       for (sums = linesums, xindex = (destext - 1) * source->bytespp;
00754         xindex > 0; xindex--)
00755       *pixel++ = (UINT8) ((div2 + (*sums++ << shift)) / divisor);
00756       //destination value
00757       div2 = (yindex * xfactor) / 2;
00758       for (xindex = source->bytespp; xindex > 0; xindex--)
00759         *pixel++ =
00760           (UINT8) ((div2 + (*sums++ << shift)) / (yindex * xfactor));
00761       //last one different
00762     }
00763                                  //put in destination
00764     dest->put_line (xdest, ydest, destext, &copyline, 0);
00765   }
00766   delete linesums;
00767 }


Member Data Documentation

INT8 IMAGE::bpp [private]

Definition at line 230 of file img.h.

Referenced by capture(), convolver(), copy_sub_image(), destroy(), enlarge_sub_image(), fast_get_line(), fast_reduce_sub_image(), get_column(), get_line(), IMAGE(), operator=(), pixel(), put_column(), put_line(), read(), read_header(), reduce_sub_image(), and write().

INT8 IMAGE::bps [private]

Definition at line 231 of file img.h.

Referenced by capture(), copy_sub_image(), enlarge_sub_image(), fast_reduce_sub_image(), operator=(), read_header(), and reduce_sub_image().

INT32 IMAGE::bufheight [private]

Definition at line 240 of file img.h.

Referenced by bufread(), capture(), operator=(), read(), and write().

INT8 IMAGE::bytespp [private]

Definition at line 232 of file img.h.

Referenced by capture(), enlarge_sub_image(), fast_get_line(), fast_reduce_sub_image(), get_column(), get_line(), operator=(), put_line(), read_header(), and reduce_sub_image().

BOOL8 IMAGE::captured [private]

Definition at line 234 of file img.h.

Referenced by capture(), create(), destroy(), operator=(), and read().

int IMAGE::fd [private]

Definition at line 241 of file img.h.

Referenced by bufread(), destroy(), IMAGE(), operator=(), read(), read_header(), and write().

UINT8* IMAGE::image [private]

Definition at line 238 of file img.h.

Referenced by bufread(), capture(), copy_sub_image(), destroy(), fast_get_line(), get_column(), get_line(), IMAGE(), operator=(), pixel(), put_column(), put_line(), read(), and write().

INT8 IMAGE::lineskip [private]

Definition at line 233 of file img.h.

Referenced by bufread(), operator=(), read(), and read_header().

INT8 IMAGE::photo_interp [private]

Definition at line 235 of file img.h.

Referenced by capture(), IMAGE(), operator=(), read_header(), and write().

IMAGE_READER IMAGE::reader [private]

Definition at line 242 of file img.h.

Referenced by operator=(), read(), and read_header().

INT32 IMAGE::res [private]

Definition at line 237 of file img.h.

Referenced by capture(), create(), IMAGE(), operator=(), read_header(), and write().

INT32 IMAGE::xdim [private]

Definition at line 239 of file img.h.

Referenced by bufread(), capture(), copy_sub_image(), create(), fast_get_line(), get_column(), get_line(), operator=(), pixel(), put_column(), put_line(), read(), and read_header().

INT32 IMAGE::xsize [private]

Definition at line 236 of file img.h.

Referenced by bufread(), capture(), check_legal_access(), convolver(), copy_sub_image(), enlarge_sub_image(), fast_reduce_sub_image(), get_line(), operator=(), pixel(), put_line(), read(), read_header(), reduce_sub_image(), and write().

INT32 IMAGE::ymax [private]

Definition at line 244 of file img.h.

Referenced by bufread(), capture(), check_legal_access(), copy_sub_image(), fast_get_line(), get_column(), get_line(), operator=(), pixel(), put_column(), put_line(), and read().

INT32 IMAGE::ymin [private]

Definition at line 243 of file img.h.

Referenced by bufread(), capture(), check_legal_access(), operator=(), and read().

INT32 IMAGE::ysize [private]

Definition at line 236 of file img.h.

Referenced by capture(), check_legal_access(), convolver(), copy_sub_image(), enlarge_sub_image(), fast_reduce_sub_image(), get_column(), operator=(), pixel(), put_column(), read(), read_header(), reduce_sub_image(), and write().


The documentation for this class was generated from the following files:
Generated on Wed Feb 28 19:49:32 2007 for Tesseract by  doxygen 1.5.1