1 #ifndef _PixRaster_h_
2 #define _PixRaster_h_
3 
4 #include <Draw/Draw.h>
5 
6 #define Pix _Pix
7 #include "lib/allheaders.h"
8 #undef Pix
9 
10 #include "Marker.h"
11 
12 NAMESPACE_UPP
13 
14 ///////////////////////////////////////////////////////////////////////////////
15 // WARNING :
16 //
17 // ENDIANNESS MUST BE DEFINED AS L_BIG_ENDIAN or L_LITTLE_ENDIAN
18 // PIXEL ACCESSES DEPENDS ON IT ON LEPTONICA
19 ///////////////////////////////////////////////////////////////////////////////
20 #define L_LITTLE_ENDIAN
21 
22 // shortcut for current page
23 // set to a value which should never happen
24 #define PIXRASTER_INVALID_PAGE	0x8fffffff
25 #define PIXRASTER_CURPAGE		0x8ffffffe
26 #define PIXRASTER_LASTPAGE		0x8ffffffd
27 #define PIXRASTER_END			0x8ffffffc
28 #define PIXRASTER_BEGIN			0x00000000
29 
30 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
31 // base class for Pix and PixRaster -- used mainly to have a common base class for PixRasterCtrl
32 class PixBase : public Raster
33 {
34 	public:
35 		typedef PixBase CLASSNAME;
36 
SeekPage(int page)37 		virtual void SeekPage(int page) {};
GetPageCount()38 		virtual int GetPageCount() { return 1; }
GetActivePage()39 		virtual int GetActivePage() { return 0; }
40 
41 		// gets underlying PIX object
42 		// WARNING -- Pix has ownership of it, so DON'T free nor destroy it
43 		virtual PIX *GetPIX(int page = PIXRASTER_CURPAGE) = 0;
44 
45 		// standard Raster functions -- they operate on active page
GetSize()46 		virtual Size GetSize()					{ return GetSizeEx(PIXRASTER_CURPAGE); }
GetInfo()47 		virtual Raster::Info GetInfo()			{ return GetInfoEx(PIXRASTER_CURPAGE); }
GetLine(int line)48 		virtual Raster::Line GetLine(int line)	{ return GetLineEx(line, PIXRASTER_CURPAGE); }
GetPaletteCount()49 		virtual int GetPaletteCount()			{ return GetPaletteCountEx(PIXRASTER_CURPAGE); }
GetPalette()50 		virtual const RGBA *GetPalette()		{ return GetPaletteEx(PIXRASTER_CURPAGE); }
GetFormat()51 		virtual const RasterFormat *GetFormat() { return GetFormatEx(PIXRASTER_CURPAGE); }
GetWidth()52 		virtual int GetWidth() { return GetSize().cx; }
GetHeight()53 		virtual int GetHeight() { return GetSize().cy; }
GetMarkers()54 		virtual Markers *GetMarkers() { return GetMarkersEx(PIXRASTER_CURPAGE); }
55 
56 
57 		// extended Raster functions -- they allow to query
58 		// a given page inside raster
59 		virtual Size GetSizeEx(int page) = 0;
60 		virtual Info GetInfoEx(int page) = 0;
61 		virtual Line GetLineEx(int line, int page) = 0;
62 		virtual int GetPaletteCountEx(int page) = 0;
63 		virtual const RGBA *GetPaletteEx(int page) = 0;
64 		virtual const RasterFormat *GetFormatEx(int page) = 0;
GetWidthEx(int page)65 		virtual int GetWidthEx(int page)		{ return GetSizeEx(page).cx; }
GetHeightEx(int page)66 		virtual int GetHeightEx(int page)		{ return GetSizeEx(page).cy; }
67 		virtual Markers *GetMarkersEx(int page) = 0;
68 
69 		virtual bool IsEmpty() = 0;
70 		operator bool() { return !IsEmpty(); }
71 
72 		virtual void Clear() = 0;
73 
74 }; // END Class PixBase
75 
76 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 // class wrapping Leptonica PIX image -- it's a single page raster format
78 class PixRaster;
79 class Pix : public PixBase
80 {
81 	// give PixRaster access to private members
82 	friend class PixRaster;
83 
84 	public:
85 		typedef Pix CLASSNAME;
86 
87 	private:
88 
89 		// the Leptonica PIX
90 		PIX *pix;
91 
92 		// raster format
93 		RasterFormat *rasterFormat;
94 
95 		// local palette
96 		RGBA *localPalette;
97 
98 		// polygon markers
99 		Markers markers;
100 
101 	protected:
102 
103 		// internal destructor
104 		void Destroy();
105 
106 		// sets raster format depending on pix format
107 		void SetRasterFormat();
108 
109 		// synthesyze grayscale palettes
110 		void CreateGrayPalette2();
111 		void CreateGrayPalette4();
112 		void CreateGrayPalette16();
113 		void CreateGrayPalette256();
114 
115 		// convert PIX palette
116 		void ConvertPIXPalette(PIXCMAP *colormap);
117 
118 		// raster loading/conversion routines
119 		bool LoadRASTER_RGBA(Raster &raster);
120 
121 		bool LoadRASTER_1(Raster &raster);
122 		bool LoadRASTER_2(Raster &raster);
123 		bool LoadRASTER_4(Raster &raster);
124 		bool LoadRASTER_8(Raster &raster);
125 		bool LoadRASTER_8ALPHA(Raster &raster);
126 		bool LoadRASTER_16(Raster &raster);
127 		bool LoadRASTER_24(Raster &raster);
128 		bool LoadRASTER_32(Raster &raster);
129 
130 		bool LoadRASTER_1_MSBFIRST(Raster &raster);
131 		bool LoadRASTER_2_MSBFIRST(Raster &raster);
132 		bool LoadRASTER_4_MSBFIRST(Raster &raster);
133 		bool LoadRASTER_8_MSBFIRST(Raster &raster);
134 		bool LoadRASTER_8ALPHA_MSBFIRST(Raster &raster);
135 		bool LoadRASTER_16_MSBFIRST(Raster &raster);
136 		bool LoadRASTER_24_MSBFIRST(Raster &raster);
137 		bool LoadRASTER_32_MSBFIRST(Raster &raster);
138 
139 	public:
140 
141 		// gets underlying PIX object
142 		// WARNING -- Pix has ownership of it, so DON'T free nor destroy it
143 		virtual PIX *GetPIX(int page = PIXRASTER_CURPAGE) { return pix; }
144 		operator PIX*() { return GetPIX(PIXRASTER_CURPAGE); }
145 
146 		// bring-in color modes
147 		enum BringInModes { PIXRASTER_BRING_IN_WHITE = L_BRING_IN_WHITE, PIXRASTER_BRING_IN_BLACK = L_BRING_IN_BLACK };
148 
149 		// rotate modes
150 		enum RotateModes { PIXRASTER_ROTATE_AREA_MAP = L_ROTATE_AREA_MAP, PIXRASTER_ROTATE_SHEAR = L_ROTATE_SHEAR, PIXRASTER_ROTATE_SAMPLING = L_ROTATE_SAMPLING };
151 
152 		// scan direction modes
153 		enum ScanModes { PIXRASTER_FROM_LEFT = L_FROM_LEFT, PIXRASTER_FROM_RIGHT = L_FROM_RIGHT, PIXRASTER_FROM_TOP = L_FROM_TOP, PIXRASTER_FROM_BOTTOM = L_FROM_BOTTOM };
154 
155 		// loads Pix from another raster object
156 		void Load(Raster& raster, bool deepCopy = false, int page = PIXRASTER_CURPAGE);
157 
158 		// empty constructor -- sets NULL PIX
159 		Pix();
160 
161 		// creates a PIX in given format and size
162 		Pix(int width, int height, int depth, RGBA *colorTable = NULL);
163 
164 		// constructor -- takes a Leptonica PIX
165 		// TAKES OWNERSHIP of underlying PIX object which is NULLed
166 		Pix(PIX **_pix);
167 
168 		// copy constructor -- reference increments underlying PIX
169 		Pix(Pix const &_pix);
170 
171 		// deep copy constructor -- deep copy of underlying PIX
172 		Pix(Pix const &_pix, int i);
173 
174 		// creation of Pix from a raster object
175 		Pix(Raster &raster, bool deepCopy = false, int page = PIXRASTER_CURPAGE);
176 
177 		// creation of Pix from file/stream
178 		Pix(FileIn &fs, int page = 0);
179 		Pix(String const &fileName, int page = 0);
180 
181 		// destructor
182 		~Pix();
183 
184 		// assignment -- increment reference of underlying PIX
185 		Pix &operator=(Pix &pix);
186 		Pix const &operator=(Pix const &pix) { return operator=((Pix &)pix); }
187 
188 		// deep copy -- deep copies underlying PIX
189 		Pix &operator <<=(Pix &pix);
190 		Pix &DeepCopy(Pix &pix);
191 
192 		// checks wether Pix is empty
IsEmpty()193 		virtual bool IsEmpty() { return (pix == NULL); }
194 
195 		// empties the image
Clear()196 		virtual void Clear() { Destroy(); }
197 
198 		// extended Raster functions -- they allow to query
199 		// a given page inside raster
200 		virtual Size GetSizeEx(int page);
201 		virtual Info GetInfoEx(int page);
202 		virtual Line GetLineEx(int line, int page);
203 		virtual int GetPaletteCountEx(int page);
204 		virtual const RGBA *GetPaletteEx(int page);
205 		virtual const RasterFormat *GetFormatEx(int page);
206 
207 		// gets polygon markers
GetMarkers()208 		virtual Markers *GetMarkers() { return &markers; }
GetMarkersEx(int)209 		virtual Markers *GetMarkersEx(int) { return &markers; }
210 
211 		// file I/O
212 		bool Load(FileIn &fs, int page = 0);
213 		bool Load(String fileName, int page = 0);
214 		bool Save(String fileName, int quality = 75, bool progressive = false);
215 
216 		/////////////////////////////////////////////////////////////////////////////////////////////////////////
217 		///////////////////////////////////////// LEPTONICA OPERATIONS //////////////////////////////////////////
218 		/////////////////////////////////////////////////////////////////////////////////////////////////////////
219 
220 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 		// IMAGE THRESHOLDING //
222 
223 		Pix DitherToBinary();
224 		Pix DitherToBinarySpec(int lowerclip, int upperclip);
225 
226 		// threshold the image -- warning, operates ONLY on grayscale pixmaps
227 		Pix ThresholdToBinary(int threshold);
228 
229 		Pix VarThresholdToBinary(Pix &thresholdPix);
230 		Pix DitherToBinaryLUT(int lowerclip, int upperclip);
231 		Pix GenerateMaskByValue(int val, bool useCMap = false);
232 		Pix GenerateMaskByBand(int lower, int upper, int inband, bool useCMap = false);
233 		Pix DitherTo2bpp(int cmapflag);
234 		Pix DitherTo2bppSpec(int lowerclip, int upperclip, int cmapflag);
235 		Pix ThresholdTo2bpp(int nlevels, int cmapflag);
236 		Pix ThresholdTo4bpp(int nlevels, int cmapflag);
237 		Pix ThresholdOn8bpp(int nlevels, int cmapflag);
238 		Pix ThresholdGrayArb(const char  *edgevals, int outdepth, int use_average, int setblack, int setwhite);
239 		Buffer<int> MakeGrayQuantIndexTable(int nlevels);
240 		Buffer<int> MakeGrayQuantTargetTable(int nlevels, int depth);
241 		Pix GenerateMaskByBand32(unsigned refval, int delm, int delp);
242 		Pix GenerateMaskByDiscr32(unsigned refval1, unsigned refval2, int distflag);
243 		Pix GrayQuantFromHisto(Pix &mPix, double minfract, int maxsize);
244 
245 		Pix ThresholdToValue(int threshval, int setval);
246 
247 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
248 		// COLOR CONVERSION //
249 
250 		// converts pix to monochrome 1bpp
251 		Pix ConvertTo1(int threshold);
252 
253 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
254 		// SKEW REMOVAL //
255 
256 		// Simple top-level deskew interfaces
257 		Pix Deskew(int ReductionFactor = 1);
258 		Pix FindSkewAndDeskew(int ReductionFactor, double *skewAngle = NULL, double *confidenceFactor = NULL);
259 
260 		// Simple top-level skew angle finding interface
261 		bool FindSkew(double *pangle, double *pconf = NULL);
262 
263 		// Basic angle-finding functions with all parameters
264 		bool FindSkewSweep(double *pangle, int reduction, double sweeprange, double sweepdelta);
265 
266 		// Angle-finding functions with all parameters
267 		bool FindSkewSweepAndSearch(
268 			double *pangle, double *pconf,
269 			int redsweep, int redsearch,
270 			double sweeprange, double sweepdelta,
271 			double minbsdelta);
272 
273 		Pix DeskewLocal();
274 
275 		// NOTE : Leptonica has many more skew functions....
276 
277 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
278 		// ROTATION FUNCTIONS
279 
280  		// Rotates image (2-4-8 or 32 bpp) about center.
281 		Pix RotateAM(double angle, BringInModes incolor = PIXRASTER_BRING_IN_WHITE);
282 		Pix RotateAMColor(double angle, int colorval);
283 		Pix RotateAMGray(double angle, int grayval);
284 		Pix Rotate(double angle, int type = PIXRASTER_ROTATE_SHEAR, int incolor = PIXRASTER_BRING_IN_WHITE, int width = 0, int height = 0);
285 
286 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
287 		// MORPHING FUNCTIONS
288 
289 		Pix ErodeGray(int hsize, int vsize);
290 		Pix DilateGray(int hsize, int vsize);
291 		Pix OpenGray(int hsize, int vsize);
292 		Pix CloseGray(int hsize, int vsize);
293 
294 		Pix ErodeBrick(int hsize, int vsize);
295 		Pix DilateBrick(int hsize, int vsize);
296 		Pix OpenBrick(int hsize, int vsize);
297 		Pix CloseBrick(int hsize, int vsize);
298 
299 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
300 		// ARITHMETIC AND ALIKE FUNCTIONS
301 
302 		Pix Invert();
303 		Pix AddConstantGray(int val);
304 		Pix MultConstantGray(int val);
305 		Pix AddGray(Pix &pix2);
306 		Pix SubtractGray(Pix &pix2);
307 
308 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
309 		// BLENDING/COMBINING FUNCTIONS
310 
311 		Pix CombineMasked(Pix &aPix, Pix &maskPix);
312 
313 		// extracts a rectangular area from PIX
314 		Pix ClipRectangle(Rect r);
315 
316 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
317 		// SCALE/RESIZE FUNCTIONS
318 
319 		// toplevel scale function
320 		Pix Scale(double scalex, double scaley);
321 		Pix Fit(int width, int height, bool keepRatio = true);
322 
323 		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
324 		// DOCUMENT PAGE SEGMENTING FUNCTIONS
325 
326 		// top level page segmenting
327 		PixRaster GetRegionsBinary() ;
328 
329 		// text baseline finding routine
330 		Array<int> FindBaselines();
331 
332 		// foreground bounding box locating routine
333 		// scans from a given border through image center to locate
334 		// first non-background pixel.
335 		// scanFlag is one of PIXRASTER_FROM_LEFT, PIXRASTER_FROM_RIGHT, PIXRASTER_FROM_TOP, PIXRASTER_FROM_BOTTOM
336 		// returns true on success, false otherwise
337 		bool ScanForForeground(int &loc, enum ScanModes scanMode);
338 
339 }; // END Class Pix
340 
341 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
342 // raster class -- array of Pix objects
343 class PixRaster : public PixBase
344 {
345 	protected:
346 
347 		// array of Pixs
348 		Array<Pix>pages;
349 
350 		// currently active page for multipage images
351 		int curPage;
352 
353 		// gets real page
354 		int getTruePage(int page = PIXRASTER_CURPAGE);
355 
356 	public:
357 
358 		// gets PIX from PixRaster
359 		// WARNING -- Pix has ownership of it, so DON'T free nor destroy it
360 		PIX *GetPIX(int page = PIXRASTER_CURPAGE);
361 		operator PIX*() { return GetPIX(PIXRASTER_CURPAGE); }
362 
363 		// constructor -- empty raster
364 		PixRaster();
365 
366 		// constructor -- takes a Leptonica Pix
367 		// PixRaster TAKES OWNERSHIP of underlying PIX object which is NULLed
368 		PixRaster(PIX **pix);
369 
370 		// constructor -- takes an array of Leptonica Pix
371 		// PIXA gets destroyed and NULLed
372 		PixRaster(PIXA **pixa);
373 
374 		// creation of Pix from a raster object
375 		PixRaster(Raster &raster, bool deepCopy = false);
376 
377 		// destructor
378 		~PixRaster();
379 
380 		// loads Pix from another raster object
381 		void Load(Raster& raster, bool Append = false, bool deepCopy = false);
382 
383 		// sets/appends a source raster
384 		PixRaster &operator=(Raster &raster) { Load(raster, false, false); return *this; }
385 		PixRaster &operator+=(Raster &raster) { Load(raster, true, false); return *this; }
386 
387 		// deep copy of source raster
388 		PixRaster &operator <<=(Raster &raster) { Load(raster, false, true); return *this; }
389 
390 		// page handling
391 		virtual void SeekPage(int page);
392 		virtual int GetPageCount();
393 		virtual int GetActivePage();
394 
395 		// extended Raster functions -- they allow to query
396 		// a given page inside raster
397 		virtual Size GetSizeEx(int page);
398 		virtual Info GetInfoEx(int page);
399 		virtual Line GetLineEx(int line, int page);
400 		virtual int GetPaletteCountEx(int page);
401 		virtual const RGBA *GetPaletteEx(int page);
402 		virtual const RasterFormat *GetFormatEx(int page);
403 
404 		// gets polygon markers
GetMarkers()405 		virtual Markers *GetMarkers() { return GetMarkersEx(PIXRASTER_CURPAGE); }
406 		virtual Markers *GetMarkersEx(int page);
407 
408 		// check whether pixraster has images
IsEmpty(void)409 		bool IsEmpty(void) { return !pages.GetCount(); };
410 
411 		// sets Pix
412 		void Set(Pix &_pix, int page = PIXRASTER_CURPAGE, bool deepCopy = false);
413 
414 		// adds a Pix or a PixRaster
415 		void Add(Pix &pix, bool deepCopy = false);
416 		void Add(PixRaster &pix, bool deepCopy = false);
417 
418 		// inserts a PIX or a PIX array
419 		void Insert(Pix &pix, int where = PIXRASTER_CURPAGE, bool deepCopy = false);
420 		void Insert(PixRaster &pixR, int where = PIXRASTER_CURPAGE, bool deepCopy = false);
421 
422 		// removes a PIX or a series of PIX
423 		void Remove(int startPage = PIXRASTER_CURPAGE, int count = 1);
424 
425 		// duplicates a page at the end of array (useful for stack-like operations)
426 		// NOTE1: deep copy of underlying PIX
427 		// NOTE2: it seeks added page too
428 		Pix &Dup(int page = PIXRASTER_CURPAGE);
429 
430 		// Drops last page(s) of array (ditto)
431 		// NOTE: it seeks last page too
432 		void Drop(int count = 1);
433 
434 		// Pix access operator
435 		Pix &operator[](int page) { return At(page); }
436 
437 		// access current page
438 		operator Pix&() { return At(PIXRASTER_CURPAGE); }
439 
440 		// array access
441 		Pix &At(int page);
442 
443 		// empties the image
Clear()444 		virtual void Clear() { pages.Clear(); }
445 
446 		// file I/O
447 		bool Load(FileIn &fs, bool Append = false);
448 		bool Load(String fileName, bool Append = false);
449 		bool Save(String fileName, int page = PIXRASTER_CURPAGE); // @@ to do - add compression and type handling
450 		bool SaveAll(String fileName);
451 
452 		rval_default(PixRaster);
453 }; // END class PixRaster
454 
455 END_UPP_NAMESPACE
456 
457 #endif
458