All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bmp_8.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: bmp_8.h
3  * Description: Declaration of an 8-bit Bitmap class
4  * Author: Ahmad Abdulkader
5  * Created: 2007
6  *
7  * (C) Copyright 2008, Google Inc.
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 BMP8_H
21 #define BMP8_H
22 
23 // The Bmp8 class is an 8-bit bitmap that represents images of
24 // words, characters and segments throughout Cube
25 // It is meant to provide fast access to the bitmap bits and provide
26 // fast scaling, cropping, deslanting, connected components detection,
27 // loading and saving functionality
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include "con_comp.h"
32 #include "cached_file.h"
33 
34 namespace tesseract {
35 
36 // Non-integral deslanting parameters.
37 static const float kMinDeslantAngle = -30.0f;
38 static const float kMaxDeslantAngle = 30.0f;
39 static const float kDeslantAngleDelta = 0.5f;
40 
41 class Bmp8 {
42  public:
43  Bmp8(unsigned short wid, unsigned short hgt);
44  ~Bmp8();
45  // Clears the bitmap
46  bool Clear();
47  // accessors to bitmap dimensions
48  inline unsigned short Width() const { return wid_; }
49  inline unsigned short Stride() const { return stride_; }
50  inline unsigned short Height() const { return hgt_; }
51  inline unsigned char *RawData() const {
52  return (line_buff_ == NULL ? NULL : line_buff_[0]);
53  }
54  // creates a scaled version of the specified bitmap
55  // Optionally, scaling can be isotropic (preserving aspect ratio) or not
56  bool ScaleFrom(Bmp8 *bmp, bool isotropic = true);
57  // Deslant the bitmap vertically
58  bool Deslant();
59  // Deslant the bitmap horizontally
60  bool HorizontalDeslant(double *deslant_angle);
61  // Create a bitmap object from a file
62  static Bmp8 *FromCharDumpFile(CachedFile *fp);
63  static Bmp8 *FromCharDumpFile(FILE *fp);
64  // are two bitmaps identical
65  bool IsIdentical(Bmp8 *pBmp) const;
66  // Detect connected components
67  ConComp ** FindConComps(int *concomp_cnt, int min_size) const;
68  // compute the foreground ratio
69  float ForegroundRatio() const;
70  // returns the mean horizontal histogram entropy of the bitmap
71  float MeanHorizontalHistogramEntropy() const;
72  // returns the horizontal histogram of the bitmap
73  int *HorizontalHistogram() const;
74 
75  private:
76  // Compute a look up tan table that will be used for fast slant computation
77  static bool ComputeTanTable();
78  // create a bitmap buffer (two flavors char & int) and init contents
79  unsigned char ** CreateBmpBuffer(unsigned char init_val = 0xff);
80  static unsigned int ** CreateBmpBuffer(int wid, int hgt,
81  unsigned char init_val = 0xff);
82  // Free a bitmap buffer
83  static void FreeBmpBuffer(unsigned char **buff);
84  static void FreeBmpBuffer(unsigned int **buff);
85 
86  // a static array that holds the tan lookup table
87  static float *tan_table_;
88  // bitmap 32-bit-aligned stride
89  unsigned short stride_;
90  // Bmp8 magic number used to validate saved bitmaps
91  static const unsigned int kMagicNumber = 0xdeadbeef;
92 
93  protected:
94  // bitmap dimensions
95  unsigned short wid_;
96  unsigned short hgt_;
97  // bitmap contents
98  unsigned char **line_buff_;
99  // deslanting parameters
100  static const int kConCompAllocChunk = 16;
101  static const int kDeslantAngleCount;
102 
103  // Load dimensions & contents of bitmap from file
105  bool LoadFromCharDumpFile(FILE *fp);
106  // Load dimensions & contents of bitmap from raw data
107  bool LoadFromCharDumpFile(unsigned char **raw_data);
108  // Load contents of bitmap from raw data
109  bool LoadFromRawData(unsigned char *data);
110  // save bitmap to a file
111  bool SaveBmp2CharDumpFile(FILE *fp) const;
112  // checks if a row or a column are entirely blank
113  bool IsBlankColumn(int x) const;
114  bool IsBlankRow(int y) const;
115  // crop the bitmap returning new dimensions
116  void Crop(int *xst_src, int *yst_src, int *wid, int *hgt);
117  // copy part of the specified bitmap
118  void Copy(int x, int y, int wid, int hgt, Bmp8 *bmp_dest) const;
119 };
120 }
121 
122 #endif // BMP8_H
bool LoadFromCharDumpFile(CachedFile *fp)
Definition: bmp_8.cpp:137
void Copy(int x, int y, int wid, int hgt, Bmp8 *bmp_dest) const
Definition: bmp_8.cpp:578
static const int kConCompAllocChunk
Definition: bmp_8.h:100
bool IsBlankRow(int y) const
Definition: bmp_8.cpp:337
unsigned char * RawData() const
Definition: bmp_8.h:51
bool LoadFromRawData(unsigned char *data)
Definition: bmp_8.cpp:504
unsigned short Width() const
Definition: bmp_8.h:48
ConComp ** FindConComps(int *concomp_cnt, int min_size) const
Definition: bmp_8.cpp:605
static Bmp8 * FromCharDumpFile(CachedFile *fp)
Definition: bmp_8.cpp:217
void Crop(int *xst_src, int *yst_src, int *wid, int *hgt)
Definition: bmp_8.cpp:348
Bmp8(unsigned short wid, unsigned short hgt)
Definition: bmp_8.cpp:38
unsigned short hgt_
Definition: bmp_8.h:96
bool SaveBmp2CharDumpFile(FILE *fp) const
Definition: bmp_8.cpp:515
static const int kDeslantAngleCount
Definition: bmp_8.h:101
bool IsBlankColumn(int x) const
Definition: bmp_8.cpp:327
unsigned short wid_
Definition: bmp_8.h:95
int * HorizontalHistogram() const
Definition: bmp_8.cpp:1123
unsigned short Stride() const
Definition: bmp_8.h:49
unsigned char ** line_buff_
Definition: bmp_8.h:98
float ForegroundRatio() const
Definition: bmp_8.cpp:970
bool ScaleFrom(Bmp8 *bmp, bool isotropic=true)
Definition: bmp_8.cpp:393
unsigned short Height() const
Definition: bmp_8.h:50
bool Deslant()
Definition: bmp_8.cpp:795
bool Clear()
Definition: bmp_8.cpp:128
#define NULL
Definition: host.h:144
bool IsIdentical(Bmp8 *pBmp) const
Definition: bmp_8.cpp:590
float MeanHorizontalHistogramEntropy() const
Definition: bmp_8.cpp:1100
bool HorizontalDeslant(double *deslant_angle)
Definition: bmp_8.cpp:987