Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
vtImage.h
1 //
2 // vtImage.h
3 //
4 // Copyright (c) 2002-2008 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTIMAGE_H
9 #define VTIMAGE_H
10 
11 #include "wx/image.h"
12 #include "TilingOptions.h"
13 #include "vtdata/Projections.h"
14 
15 class vtBitmap;
16 class GDALDataset;
17 class GDALRasterBand;
18 class GDALColorTable;
19 class BuilderView;
20 class ImageGLCanvas;
21 
22 // The following mechanism is for a small buffer, consisting of a small
23 // number of scanlines, to cache the results of accessing large image
24 // files out of memory (direct from disk).
25 struct Scanline
26 {
27  RGBi *m_data;
28  int m_y;
29  int m_overview; // 0 is the base image, 1 2 3.. are the overviews
30 };
31 #define BUF_SCANLINES 4
32 
34 {
35 public:
37  ~LineBufferGDAL() { Cleanup(); }
38 
39  void Setup(GDALDataset *pDataset);
40  void Cleanup();
41 
42  void ReadScanline(int y, int bufrow, int overview);
43  RGBi *GetScanlineFromBuffer(int y, int overview);
44  void FindMaxBlockSize(GDALDataset *pDataset);
45 
46  int m_iXSize;
47  int m_iYSize;
48 
49  int m_iRasterCount;
50  uchar *m_pBlock;
51  uchar *m_pRedBlock;
52  uchar *m_pGreenBlock;
53  uchar *m_pBlueBlock;
54  GDALRasterBand *m_pBand;
55  GDALRasterBand *m_pRed;
56  GDALRasterBand *m_pGreen;
57  GDALRasterBand *m_pBlue;
58  GDALColorTable *m_pTable;
59  int m_MaxBlockSize;
60 
61  // Total views, including Overviews
62  int m_iViewCount;
63 
64  Scanline m_row[BUF_SCANLINES];
65  int m_use_next;
66  int m_found_last;
67 
68  // Statistics
69  int m_linereads;
70  int m_blockreads;
71 };
72 
74 {
75 public:
76  BitmapInfo() { m_pBitmap = NULL; m_bOnDisk = false; }
77 
78  int number; // 0, 1, 2..
79  vtBitmap *m_pBitmap; // non-NULL if in memory
80  bool m_bOnDisk; // true if GDAL overview exists on disk
81  IPoint2 m_Size; // size in pixels
82  DPoint2 m_Spacing; // spatial resolution in earth units/pixel
83 };
84 
86 
87 class vtImage
88 {
89 public:
90  vtImage();
91  vtImage(const DRECT &area, int xsize, int ysize,
92  const vtProjection &proj);
93  virtual ~vtImage();
94 
95  bool GetExtent(DRECT &rect) const;
96  void SetExtent(const DRECT &rect);
97  void DrawToView(wxDC *pDC, vtScaledView *pView);
98  bool ConvertProjection(vtImage *input, vtProjection &proj_new,
99  int iSampleN, bool progress_callback(int) = NULL);
100 
101  DPoint2 GetSpacing(int bitmap = 0) const;
102  vtBitmap *GetBitmap() { return m_Bitmaps[0].m_pBitmap; }
103 
104  void GetProjection(vtProjection &proj) const;
105  vtProjection &GetAtProjection() { return m_proj; }
106  void SetProjection(const vtProjection &proj);
107  bool ReprojectExtents(const vtProjection &proj_new);
108 
109  void GetDimensions(int &xsize, int &ysize) const
110  {
111  xsize = m_Bitmaps[0].m_Size.x;
112  ysize = m_Bitmaps[0].m_Size.y;
113  }
114  IPoint2 GetDimensions() const
115  {
116  return m_Bitmaps[0].m_Size;
117  }
118  bool GetColorSolid(const DPoint2 &p, RGBi &rgb, double dRes = 0.0);
119  bool GetMultiSample(const DPoint2 &p, const DLine2 &offsets, RGBi &rgb, double dRes = 0.0);
120  void GetRGB(int x, int y, RGBi &rgb, double dRes = 0.0);
121  void SetRGB(int x, int y, uchar r, uchar g, uchar b);
122  void SetRGB(int x, int y, const RGBi &rgb);
123  void ReplaceColor(const RGBi &rgb1, const RGBi &rgb2);
124  void SetupBitmapInfo(int iXSize, int iYSize);
125 
126  // File IO
127  bool ReadPPM(const char *fname, bool progress_callback(int) = NULL);
128  bool WritePPM(const char *fname) const;
129  bool SaveToFile(const char *fname) const;
130  bool ReadPNGFromMemory(uchar *buf, int len);
131  bool LoadFromGDAL(const char *fname);
132  bool CreateOverviews();
133 
134  bool ReadFeaturesFromTerraserver(const DRECT &area, int iTheme,
135  int iMetersPerPixel, int iUTMZone, const char *filename);
136  bool WriteGridOfTilePyramids(TilingOptions &opts, BuilderView *pView);
137  bool WriteTile(const TilingOptions &opts, BuilderView *pView, vtString &dirname,
138  DRECT &tile_area, DPoint2 &tile_dim, int col, int row, int lod);
139 
140  // global switch
141  static bool bTreatBlackAsTransparent;
142 
143  // used when reading from a file with GDAL
144  GDALDataset *m_pDataset;
145  LineBufferGDAL m_linebuf;
146 
147  size_t NumBitmaps() const { return m_Bitmaps.size(); }
148  BitmapInfo &GetBitmapInfo(size_t i) { return m_Bitmaps[i]; }
149  int NumBitmapsInMemory();
150  int NumBitmapsOnDisk();
151 
152  void AllocMipMaps();
153  void DrawMipMaps();
154  void FreeMipMaps();
155 
156 protected:
157  void SetDefaults();
158  void CleanupGDALUsage();
159 
160  vtProjection m_proj;
161 
162  DRECT m_Extents;
163 
164  std::vector<BitmapInfo> m_Bitmaps;
165 
166  // Used during writing of tilesets
167  int m_iTotal, m_iCompleted;
168  ImageGLCanvas *m_pCanvas;
169 };
170 
171 // Helpers
172 int GetBitDepthUsingGDAL(const char *fname);
173 void MakeSampleOffsets(const DPoint2 cellsize, uint N, DLine2 &offsets);
174 void SampleMipLevel(vtBitmap *bigger, vtBitmap *smaller);
175 
176 #endif // VTIMAGE_H