Tesseract 3.01
/data/source/tesseract-ocr/image/img.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        img.h  (Formerly image.h)
00003  * Description: Class definition for the IMAGE class.
00004  * Author:      Ray Smith
00005  * Created:     Thu Jun 07 13:42:37 BST 1990
00006  *
00007  * (C) Copyright 1990, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef           IMG_H
00021 #define           IMG_H
00022 
00023 #include          "memry.h"
00024 
00025 struct Pix;
00026 
00028 #define MAXIMAGEWIDTH   (900*14) 
00029 
00031 #define MAXIMAGEHEIGHT    (900*14)
00032 
00033 #define COMPUTE_IMAGE_XDIM(xsize,bpp) ((bpp)>8 ? ((xsize)*(bpp)+7)/8 :((xsize)+8/(bpp)-1)/(8/(bpp)))
00034 
00035 typedef inT8 (*IMAGE_OPENER) (int, inT32 *, inT32 *, inT8 *, inT8 *, inT32 *);
00036 typedef inT8 (*IMAGE_READER) (int, uinT8 *, inT32, inT32, inT8, inT32);
00037 typedef inT8 (*IMAGE_WRITER) (int, uinT8 *, inT32, inT32, inT8, inT8, inT32);
00038 
00040 typedef uinT8 *COLOUR_PIX;
00041 enum COLOUR_PIX_NAME
00042 {
00043   RED_PIX,
00044   GREEN_PIX,
00045   BLUE_PIX
00046 };
00047 
00048 class DLLSYM IMAGELINE;
00049 
00051 class DLLSYM IMAGE
00052 {
00053   public:
00054     IMAGE();  //constructor
00055 
00056     ~IMAGE () {                  //destructor
00057       destroy();  //free memory
00058     }
00059 
00060     IMAGE & operator= (          //assignment
00061       IMAGE & source);
00062 
00069     inT8 create(inT32 x,
00070                 inT32 y,
00071                 inT8 bits_per_pixel);
00072 
00080     inT8 capture(uinT8 *pixels,
00081                  inT32 x,
00082                  inT32 y,
00083                  inT8 bits_per_pixel);
00084 
00086     void destroy();
00087 
00092     inT32 get_xsize() {
00093       return xsize;
00094     }
00099     inT32 get_ysize() {
00100       return ysize;
00101     }
00106     inT8 get_bpp() {
00107       return bpp;
00108     }
00113     inT8 get_bps() {
00114       return bps;
00115     }
00117     BOOL8 white_high() {
00118       return photo_interp;
00119     }
00121     uinT8 get_white_level() {
00122       return (1 << bpp) - 1;
00123     }
00125     inT32 get_res() {
00126       return res;
00127     }
00129     void set_res(inT32 resolution) {
00130       res = resolution;
00131     }
00132     uinT8 *get_buffer() {
00133       return image;
00134     }
00135 
00141     uinT8 pixel(inT32 x,
00142                 inT32 y);
00143 
00151     void fast_get_line(inT32 x,
00152                        inT32 y,
00153                        inT32 width,
00154                        IMAGELINE *linebuf);
00155 
00164     void get_line(inT32 x,
00165                   inT32 y,
00166                   inT32 width,
00167                   IMAGELINE *linebuf,
00168                   inT32 margins);
00177     void get_column(inT32 x,
00178                     inT32 y,
00179                     inT32 height,
00180                     IMAGELINE *linebuf,
00181                     inT32 margins);
00182 
00190     void fast_put_line(inT32 x,
00191                        inT32 y,
00192                        inT32 width,
00193                        IMAGELINE *linebuf);
00194 
00203     void put_line(inT32 x,
00204                   inT32 y,
00205                   inT32 width,
00206                   IMAGELINE *linebuf,
00207                   inT32 margins);
00216     void put_column(inT32 x,
00217                     inT32 y,
00218                     inT32 height,
00219                     IMAGELINE *linebuf,
00220                     inT32 margins);
00221 
00227     void check_legal_access(inT32 x,
00228                             inT32 y,
00229                             inT32 xext);
00230 
00231 
00233     Pix* ToPix();
00234     void FromPix(const Pix* src_pix);
00235 
00242     void convolver (
00243       inT32 win_width,
00244       inT32 win_height,
00245       void (*convolve) (
00246       uinT8 ** pixels,           
00247       uinT8 bytespp,             
00248       inT32 win_wd,              
00249       inT32 win_ht,              
00250       uinT8 ret_white_value,     
00251       uinT8 * result             
00252       ));
00253 
00254                                  //copy rectangle
00255     friend DLLSYM void copy_sub_image(IMAGE *source,  //source image
00256                                       inT32 xstart,   //start coords
00257                                       inT32 ystart,
00258                                       inT32 xext,     //extent to copy
00259                                       inT32 yext,
00260                                       IMAGE *dest,    //destination image
00261                                       inT32 xdest,    //destination coords //shift to match bpp
00262                                       inT32 ydest,
00263                                       BOOL8 adjust_grey);
00264 
00265                                  //enlarge rectangle
00266     friend DLLSYM void enlarge_sub_image(IMAGE *source,       //source image
00267                                          inT32 xstart,        //scaled coords
00268                                          inT32 ystart,
00269                                          IMAGE *dest,         //destination image
00270                                          inT32 xdest,         //destination coords
00271                                          inT32 ydest,
00272                                          inT32 xext,          //extent to copy
00273                                          inT32 yext,
00274                                          inT32 scale,         //scale factor
00275                                          BOOL8 adjust_grey);  //shift to match bpp
00276 
00277                                  //reduce rectangle
00278     friend DLLSYM void fast_reduce_sub_image(IMAGE *source,       //source image
00279                                              inT32 xstart,        //start coords
00280                                              inT32 ystart,
00281                                              inT32 xext,          //extent to copy
00282                                              inT32 yext,
00283                                              IMAGE *dest,         //destination image
00284                                              inT32 xdest,         //destination coords
00285                                              inT32 ydest,
00286                                              inT32 scale,         //scale factor
00287                                              BOOL8 adjust_grey);  //shift to match bpp
00288 
00289                                  //reduce rectangle
00290     friend DLLSYM void reduce_sub_image(IMAGE *source,       //source image
00291                                         inT32 xstart,        //start coords
00292                                         inT32 ystart,
00293                                         inT32 xext,          //extent to copy
00294                                         inT32 yext,
00295                                         IMAGE *dest,         //destination image
00296                                         inT32 xdest,         //destination coords
00297                                         inT32 ydest,
00298                                         inT32 scale,         //scale factor
00299                                         BOOL8 adjust_grey);  //shift to match bpp
00300 
00301   private:
00302     inT8 bpp;                    
00303     inT8 bps;                    
00304     inT8 bytespp;                
00305     inT8 lineskip;               
00306     BOOL8 captured;              
00307     inT8 photo_interp;           
00308     inT32 xsize, ysize;          
00309     inT32 res;                   
00310     uinT8 *image;                
00311     inT32 xdim;                  
00312     inT32 bufheight;             
00313     int fd;                      
00314     IMAGE_READER reader;         
00315     inT32 ymin;                  
00316     inT32 ymax;                  
00317 
00321     inT8 bufread(inT32 y);
00322 };
00323 
00324 class DLLSYM IMAGELINE           //one line of image
00325 {
00326   public:
00327     uinT8 * pixels;              
00328     inT8 bpp;                    
00329 
00330     COLOUR_PIX operator[] (
00331     inT32 index) {
00332       return &pixels[index * 3]; //coercion access op
00333     }
00334 
00336     IMAGELINE() {
00337       linewidth = 0;
00338       line = NULL;
00339       pixels = line;
00340       bpp = 8;
00341     }
00342     void init(                //setup size
00343               inT32 width) {  //size of line
00344       if (width <= 0)
00345         width = MAXIMAGEWIDTH;
00346       if (width > linewidth) {
00347         if (line != NULL)
00348           free_mem(line);
00349         linewidth = width;
00350         line = (uinT8 *) alloc_mem (linewidth * sizeof (uinT8));
00351       }
00352       pixels = line;
00353       bpp = 8;
00354     }
00355     ~IMAGELINE () {              //destructor
00356       if (line != NULL)
00357         free_mem(line);
00358     }
00359 
00361     void set_bpp(inT8 new_bpp) {
00362       if (new_bpp <= 8)
00363         bpp = 8;
00364       else
00365         bpp = 24;
00366     }
00367 
00368     void init() {
00369       if (line == NULL)
00370         init (0);
00371       else {
00372         pixels = line;
00373         bpp = 8;
00374       }
00375     }
00376 
00385     friend void IMAGE::get_line(inT32 x,
00386                                 inT32 y,
00387                                 inT32 width,
00388                                 IMAGELINE *linebuf,
00389                                 inT32 margins);
00398     friend void IMAGE::get_column(inT32 x,
00399                                   inT32 y,
00400                                   inT32 height,
00401                                   IMAGELINE *linebuf,
00402                                   inT32 margins);
00403 
00412     friend void IMAGE::put_line(inT32 x,
00413                                 inT32 y,
00414                                 inT32 width,
00415                                 IMAGELINE *linebuf,
00416                                 inT32 margins);
00417 
00426     friend void IMAGE::put_column(inT32 x,
00427                                   inT32 y,
00428                                   inT32 height,
00429                                   IMAGELINE *linebuf,
00430                                   inT32 margins);
00431 
00439     friend void IMAGE::fast_get_line(inT32 x,
00440                                      inT32 y,
00441                                      inT32 width,
00442                                      IMAGELINE *linebuf);
00443 
00451     friend void IMAGE::fast_put_line(inT32 x,
00452                                      inT32 y,
00453                                      inT32 width,
00454                                      IMAGELINE *linebuf);
00455 
00456   private:
00457     uinT8 * line;                
00458     inT32 linewidth;             
00459 };
00460 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines