1 /********************************************************************** 2 * File: img.h (Formerly image.h) 3 * Description: Class definition for the IMAGE class. 4 * Author: Ray Smith 5 * Created: Thu Jun 07 13:42:37 BST 1990 6 * 7 * (C) Copyright 1990, Hewlett-Packard Ltd. 8 ** Licensed under the Apache License, Version 2.0 (the "License"); 9 ** you may not use this file except in compliance with the License. 10 ** You may obtain a copy of the License at 11 ** http://www.apache.org/licenses/LICENSE-2.0 12 ** Unless required by applicable law or agreed to in writing, software 13 ** distributed under the License is distributed on an "AS IS" BASIS, 14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 ** See the License for the specific language governing permissions and 16 ** limitations under the License. 17 * 18 **********************************************************************/ 19 20 #ifndef IMG_H 21 #define IMG_H 22 23 #include "memry.h" 24 25 struct Pix; 26 27 #define MAXIMAGEWIDTH (900*14) /*14inch * 400dpi */ 28 /*14inch * 400dpi */ 29 #define MAXIMAGEHEIGHT (900*14) 30 31 #define COMPUTE_IMAGE_XDIM(xsize,bpp) ((bpp)>8 ? ((xsize)*(bpp)+7)/8 :((xsize)+8/(bpp)-1)/(8/(bpp))) 32 33 typedef inT8 (*IMAGE_OPENER) (int, inT32 *, inT32 *, inT8 *, inT8 *, inT32 *); 34 typedef inT8 (*IMAGE_READER) (int, uinT8 *, inT32, inT32, inT8, inT32); 35 typedef inT8 (*IMAGE_WRITER) (int, uinT8 *, inT32, inT32, inT8, inT8, inT32); 36 37 typedef uinT8 *COLOUR_PIX; //array of colours 38 enum COLOUR_PIX_NAME 39 { 40 RED_PIX, 41 GREEN_PIX, 42 BLUE_PIX 43 }; 44 45 class DLLSYM IMAGELINE; 46 47 class DLLSYM IMAGE //encapsulated image 48 { 49 public: 50 IMAGE(); //constructor 51 ~IMAGE()52 ~IMAGE () { //destructor 53 destroy(); //free memory 54 } 55 56 IMAGE & operator= ( //assignment 57 IMAGE & source); 58 59 inT8 read_header( //get file header 60 const char *name); //name of image 61 62 inT8 read( //get rest of image 63 inT32 buflines); //size of buffer 64 65 inT8 write( //write image 66 const char *name); //name to write 67 68 inT8 create( //create blank image 69 inT32 x, //x size required 70 inT32 y, //ysize required 71 inT8 bits_per_pixel); //bpp required 72 73 inT8 capture( //capture raw image 74 uinT8 *pixels, //pixels to capture 75 inT32 x, //x size required 76 inT32 y, //ysize required 77 inT8 bits_per_pixel); //bpp required 78 79 void destroy(); //destroy image 80 get_xsize()81 inT32 get_xsize() { 82 return xsize; 83 } 84 //access function get_ysize()85 inT32 get_ysize() { 86 return ysize; 87 } 88 //access function get_bpp()89 inT8 get_bpp() { 90 return bpp; 91 } //access function get_bps()92 inT8 get_bps() { 93 return bps; 94 } //bits per sample white_high()95 BOOL8 white_high() { //photo interp 96 return photo_interp; 97 } get_white_level()98 uinT8 get_white_level() { //access function 99 return (1 << bpp) - 1; 100 } get_res()101 inT32 get_res() { 102 return res; 103 } //access function set_res(inT32 resolution)104 void set_res( //set resolution 105 inT32 resolution) { 106 res = resolution; 107 } get_buffer()108 uinT8 *get_buffer() { 109 return image; 110 } 111 //access function 112 113 uinT8 pixel( //access pixel 114 inT32 x, //x coord 115 inT32 y); //y coord 116 117 void fast_get_line( //get image line 118 inT32 x, //coord to start at 119 inT32 y, //line to get 120 inT32 width, //no of pixels to get 121 IMAGELINE *linebuf); //line to copy to 122 123 void get_line( //get image line 124 inT32 x, //coord to start at 125 inT32 y, //line to get 126 inT32 width, //no of pixels to get 127 IMAGELINE *linebuf, //line to copy to 128 inT32 margins); //size of margins 129 void get_column( //get image column 130 inT32 x, //coord to start at 131 inT32 y, //line to get 132 inT32 height, //no of pixels to get 133 IMAGELINE *linebuf, //line to copy to 134 inT32 margins); //size of margins 135 136 void fast_put_line( //put image line 137 inT32 x, //coord to start at 138 inT32 y, //line to put 139 inT32 width, //no of pixels to put 140 IMAGELINE *linebuf); //line to copy from 141 142 void put_line( //put image line 143 inT32 x, //coord to start at 144 inT32 y, //line to put 145 inT32 width, //no of pixels to put 146 IMAGELINE *linebuf, //line to copy from 147 inT32 margins); //size of margins 148 void put_column( //put image column 149 inT32 x, //coord to start at 150 inT32 y, //line to put 151 inT32 height, //no of pixels to put 152 IMAGELINE *linebuf, //line to copy to 153 inT32 margins); //size of margins 154 155 void check_legal_access( //check coords 156 inT32 x, //xcoord to check 157 inT32 y, 158 inT32 xext); //ycoord to check 159 160 161 // Methods to convert image types. Only available if Leptonica is available. 162 Pix* ToPix(); 163 void FromPix(const Pix* src_pix); 164 165 void convolver ( //Map fn over window 166 inT32 win_width, //Window width 167 inT32 win_height, //Window height 168 void (*convolve) ( //Conv Function 169 uinT8 ** pixels, //Of window 170 uinT8 bytespp, //1 or 3 for colour 171 inT32 win_wd, //Window width 172 inT32 win_ht, //Window height 173 uinT8 ret_white_value, //White value to RETURN 174 uinT8 * result)); //Result pixel(s) 175 176 //copy rectangle 177 friend DLLSYM void copy_sub_image(IMAGE *source, //source image 178 inT32 xstart, //start coords 179 inT32 ystart, 180 inT32 xext, //extent to copy 181 inT32 yext, 182 IMAGE *dest, //destination image 183 inT32 xdest, //destination coords //shift to match bpp 184 inT32 ydest, 185 BOOL8 adjust_grey); 186 187 //enlarge rectangle 188 friend DLLSYM void enlarge_sub_image(IMAGE *source, //source image 189 inT32 xstart, //scaled coords 190 inT32 ystart, 191 IMAGE *dest, //destination image 192 inT32 xdest, //destination coords 193 inT32 ydest, 194 inT32 xext, //extent to copy 195 inT32 yext, 196 inT32 scale, //scale factor 197 BOOL8 adjust_grey); //shift to match bpp 198 199 //reduce rectangle 200 friend DLLSYM void fast_reduce_sub_image(IMAGE *source, //source image 201 inT32 xstart, //start coords 202 inT32 ystart, 203 inT32 xext, //extent to copy 204 inT32 yext, 205 IMAGE *dest, //destination image 206 inT32 xdest, //destination coords 207 inT32 ydest, 208 inT32 scale, //scale factor 209 BOOL8 adjust_grey); //shift to match bpp 210 211 //reduce rectangle 212 friend DLLSYM void reduce_sub_image(IMAGE *source, //source image 213 inT32 xstart, //start coords 214 inT32 ystart, 215 inT32 xext, //extent to copy 216 inT32 yext, 217 IMAGE *dest, //destination image 218 inT32 xdest, //destination coords 219 inT32 ydest, 220 inT32 scale, //scale factor 221 BOOL8 adjust_grey); //shift to match bpp 222 223 private: 224 inT8 bpp; //bits per pixel 225 inT8 bps; //bits per sample 226 inT8 bytespp; //per pixel 227 inT8 lineskip; //waste bytes on line 228 BOOL8 captured; //true if buffer captured 229 inT8 photo_interp; //interpretation 230 inT32 xsize, ysize; //size of image 231 inT32 res; //resolution 232 uinT8 *image; //the actual image 233 inT32 xdim; //bytes per line 234 inT32 bufheight; //height of buffer 235 int fd; //open file descriptor 236 IMAGE_READER reader; //reading function 237 inT32 ymin; //bottom line in mem 238 inT32 ymax; //top line in mem+1 239 inT8 bufread( //read some more 240 inT32 y); //ycoord required 241 }; 242 243 class DLLSYM IMAGELINE //one line of image 244 { 245 public: 246 uinT8 * pixels; //image pixels 247 inT8 bpp; //bits per pixel 248 COLOUR_PIX operator[] ( //colour pixels 249 inT32 index) { 250 return &pixels[index * 3]; //coercion access op 251 } 252 IMAGELINE()253 IMAGELINE() { //default constructor 254 linewidth = 0; 255 line = NULL; 256 pixels = line; 257 bpp = 8; 258 } init(inT32 width)259 void init( //setup size 260 inT32 width) { //size of line 261 if (width <= 0) 262 width = MAXIMAGEWIDTH; 263 if (width > linewidth) { 264 if (line != NULL) 265 free_mem(line); 266 linewidth = width; 267 line = (uinT8 *) alloc_mem (linewidth * sizeof (uinT8)); 268 } 269 pixels = line; 270 bpp = 8; 271 } ~IMAGELINE()272 ~IMAGELINE () { //destructor 273 if (line != NULL) 274 free_mem(line); 275 } 276 set_bpp(inT8 new_bpp)277 void set_bpp( //For colour 278 inT8 new_bpp) { 279 if (new_bpp <= 8) 280 bpp = 8; 281 else 282 bpp = 24; 283 } 284 init()285 void init() { 286 if (line == NULL) 287 init (0); 288 else { 289 pixels = line; 290 bpp = 8; 291 } 292 } 293 294 friend void IMAGE::get_line( //copies a line 295 inT32 x, //coord to start at 296 inT32 y, //line to get 297 inT32 width, //no of pixels to get 298 IMAGELINE *linebuf, //line to copy to 299 inT32 margins); //size of margins 300 //copies a column 301 friend void IMAGE::get_column(inT32 x, //coord to start at 302 inT32 y, //line to get 303 inT32 height, //no of pixels to get 304 IMAGELINE *linebuf, //line to copy to 305 inT32 margins); //size of margins 306 307 friend void IMAGE::put_line( //writes a line 308 inT32 x, //coord to start at 309 inT32 y, //line to put 310 inT32 width, //no of pixels to put 311 IMAGELINE *linebuf, //line to copy from 312 inT32 margins); //size of margins 313 //writes a column 314 friend void IMAGE::put_column(inT32 x, //coord to start at 315 inT32 y, //line to put 316 inT32 height, //no of pixels to put 317 IMAGELINE *linebuf, //line to copy from 318 inT32 margins); //size of margins 319 320 //may just change pointer 321 friend void IMAGE::fast_get_line(inT32 x, //coord to start at 322 inT32 y, //line to get 323 inT32 width, //no of pixels to get 324 IMAGELINE *linebuf); //line to copy to 325 326 //may just change pointer 327 friend void IMAGE::fast_put_line(inT32 x, //coord to start at 328 inT32 y, //line to get 329 inT32 width, //no of pixels to get 330 IMAGELINE *linebuf); //line to copy to 331 332 private: 333 uinT8 * line; //local buffer 334 inT32 linewidth; //width of buffer 335 }; 336 #endif 337