Tesseract
3.02
|
00001 /********************************************************************** 00002 * File: bmp_8.h 00003 * Description: Declaration of an 8-bit Bitmap class 00004 * Author: Ahmad Abdulkader 00005 * Created: 2007 00006 * 00007 * (C) Copyright 2008, Google Inc. 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 BMP8_H 00021 #define BMP8_H 00022 00023 // The Bmp8 class is an 8-bit bitmap that represents images of 00024 // words, characters and segments throughout Cube 00025 // It is meant to provide fast access to the bitmap bits and provide 00026 // fast scaling, cropping, deslanting, connected components detection, 00027 // loading and saving functionality 00028 00029 #include <stdlib.h> 00030 #include <stdio.h> 00031 #include "con_comp.h" 00032 #include "cached_file.h" 00033 00034 namespace tesseract { 00035 00036 // Non-integral deslanting parameters. 00037 static const float kMinDeslantAngle = -30.0f; 00038 static const float kMaxDeslantAngle = 30.0f; 00039 static const float kDeslantAngleDelta = 0.5f; 00040 00041 class Bmp8 { 00042 public: 00043 Bmp8(unsigned short wid, unsigned short hgt); 00044 ~Bmp8(); 00045 // Clears the bitmap 00046 bool Clear(); 00047 // accessors to bitmap dimensions 00048 inline unsigned short Width() const { return wid_; } 00049 inline unsigned short Stride() const { return stride_; } 00050 inline unsigned short Height() const { return hgt_; } 00051 inline unsigned char *RawData() const { 00052 return (line_buff_ == NULL ? NULL : line_buff_[0]); 00053 } 00054 // creates a scaled version of the specified bitmap 00055 // Optionally, scaling can be isotropic (preserving aspect ratio) or not 00056 bool ScaleFrom(Bmp8 *bmp, bool isotropic = true); 00057 // Deslant the bitmap vertically 00058 bool Deslant(); 00059 // Deslant the bitmap horizontally 00060 bool HorizontalDeslant(double *deslant_angle); 00061 // Create a bitmap object from a file 00062 static Bmp8 *FromCharDumpFile(CachedFile *fp); 00063 static Bmp8 *FromCharDumpFile(FILE *fp); 00064 // are two bitmaps identical 00065 bool IsIdentical(Bmp8 *pBmp) const; 00066 // Detect connected components 00067 ConComp ** FindConComps(int *concomp_cnt, int min_size) const; 00068 // compute the foreground ratio 00069 float ForegroundRatio() const; 00070 // returns the mean horizontal histogram entropy of the bitmap 00071 float MeanHorizontalHistogramEntropy() const; 00072 // returns the horizontal histogram of the bitmap 00073 int *HorizontalHistogram() const; 00074 00075 private: 00076 // Compute a look up tan table that will be used for fast slant computation 00077 static bool ComputeTanTable(); 00078 // create a bitmap buffer (two flavors char & int) and init contents 00079 unsigned char ** CreateBmpBuffer(unsigned char init_val = 0xff); 00080 static unsigned int ** CreateBmpBuffer(int wid, int hgt, 00081 unsigned char init_val = 0xff); 00082 // Free a bitmap buffer 00083 static void FreeBmpBuffer(unsigned char **buff); 00084 static void FreeBmpBuffer(unsigned int **buff); 00085 00086 // a static array that holds the tan lookup table 00087 static float *tan_table_; 00088 // bitmap 32-bit-aligned stride 00089 unsigned short stride_; 00090 // Bmp8 magic number used to validate saved bitmaps 00091 static const unsigned int kMagicNumber = 0xdeadbeef; 00092 00093 protected: 00094 // bitmap dimensions 00095 unsigned short wid_; 00096 unsigned short hgt_; 00097 // bitmap contents 00098 unsigned char **line_buff_; 00099 // deslanting parameters 00100 static const int kConCompAllocChunk = 16; 00101 static const int kDeslantAngleCount; 00102 00103 // Load dimensions & contents of bitmap from file 00104 bool LoadFromCharDumpFile(CachedFile *fp); 00105 bool LoadFromCharDumpFile(FILE *fp); 00106 // Load dimensions & contents of bitmap from raw data 00107 bool LoadFromCharDumpFile(unsigned char **raw_data); 00108 // Load contents of bitmap from raw data 00109 bool LoadFromRawData(unsigned char *data); 00110 // save bitmap to a file 00111 bool SaveBmp2CharDumpFile(FILE *fp) const; 00112 // checks if a row or a column are entirely blank 00113 bool IsBlankColumn(int x) const; 00114 bool IsBlankRow(int y) const; 00115 // crop the bitmap returning new dimensions 00116 void Crop(int *xst_src, int *yst_src, int *wid, int *hgt); 00117 // copy part of the specified bitmap 00118 void Copy(int x, int y, int wid, int hgt, Bmp8 *bmp_dest) const; 00119 }; 00120 } 00121 00122 #endif // BMP8_H