Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ImageOSG.h
1 //
2 // Copyright (c) 2001-2011 Virtual Terrain Project
3 // Free for all uses, see license.txt for details.
4 //
5 
6 #ifndef VTOSG_IMAGEH
7 #define VTOSG_IMAGEH
8 
9 #include "vtdata/vtDIB.h"
10 #include "vtdata/Projections.h"
11 
12 #include <osg/Image>
13 #include <osgDB/ReadFile>
14 
25 class vtImage: public vtBitmapBase, public osg::Image
26 {
27 public:
28  vtImage();
29  vtImage(class vtDIB *pDIB);
30  vtImage(vtImage *copyfrom);
31 
32  bool Create(int width, int height, int bitdepth, bool create_palette = false);
33  bool WritePNG(const char *fname, bool progress_callback(int) = NULL);
34  bool WriteJPEG(const char *fname, int quality = 99, bool progress_callback(int) = NULL);
35  bool HasData() { return valid() && data() != NULL; }
36  void Scale(int w, int h);
37 
39  std::string GetFilename() const { return getFileName(); }
40 
41  // Provide vtBitmapBase methods
42  uchar GetPixel8(int x, int y) const;
43  void GetPixel24(int x, int y, RGBi &rgb) const;
44  void GetPixel32(int x, int y, RGBAi &rgba) const;
45 
46  void SetPixel8(int x, int y, uchar color);
47  void SetPixel24(int x, int y, const RGBi &rgb);
48  void SetPixel32(int x, int y, const RGBAi &rgba);
49 
50  uint GetWidth() const;
51  uint GetHeight() const;
52  uint GetDepth() const;
53 
54  uchar *GetData() { return data(); }
55  uchar *GetRowData(int row) { return data(0, row); }
56 
57 protected:
58 // bool _Read(const char *fname, bool bAllowCache = true, bool progress_callback(int) = NULL);
59  void _BasicInit();
60  void _CreateFromDIB(vtDIB *pDIB, bool b16bit = false);
61  bool _ReadPNG(const char *filename);
62 };
63 typedef osg::ref_ptr<vtImage> vtImagePtr;
64 typedef osg::ref_ptr<osg::Image> ImagePtr;
65 
66 
67 /*
68  When you want to operate on an osg::Image with vtdata's Bitmap routines, it
69  can be wrapped in this class.
70  */
72 {
73 public:
74  vtImageWrapper(osg::Image *image) { m_image = image; }
75 
76  // Provide vtBitmapBase methods
77  uchar GetPixel8(int x, int y) const;
78  void GetPixel24(int x, int y, RGBi &rgb) const;
79  void GetPixel32(int x, int y, RGBAi &rgba) const;
80 
81  void SetPixel8(int x, int y, uchar color);
82  void SetPixel24(int x, int y, const RGBi &rgb);
83  void SetPixel32(int x, int y, const RGBAi &rgba);
84 
85  uint GetWidth() const { return m_image->s(); }
86  uint GetHeight() const { return m_image->t(); }
87  uint GetDepth() const { return m_image->getPixelSizeInBits(); }
88 
89  uchar *GetData() { return m_image->data(); }
90  uchar *GetRowData(int row) { return m_image->data(0, row); }
91 
92  osg::Image *m_image;
93 };
94 
95 // To ease the transition from vtImage to osg::Image, some helpers:
96 uchar GetPixel8(const osg::Image *image, int x, int y);
97 void GetPixel24(const osg::Image *image, int x, int y, RGBi &rgb);
98 void GetPixel32(const osg::Image *image, int x, int y, RGBAi &rgba);
99 void SetPixel8(osg::Image *image, int x, int y, uchar color);
100 void SetPixel24(osg::Image *image, int x, int y, const RGBi &rgb);
101 void SetPixel32(osg::Image *image, int x, int y, const RGBAi &rgba);
102 uint GetWidth(const osg::Image *image);
103 uint GetHeight(const osg::Image *image);
104 uint GetDepth(const osg::Image *image);
105 void Set16BitInternal(osg::Image *image, bool bFlag);
106 
107 
108 class vtImageGeo : public vtImage
109 {
110 public:
111  vtImageGeo();
112  vtImageGeo(const vtImageGeo *copyfrom);
113 
114  bool ReadTIF(const char *filename, bool progress_callback(int) = NULL);
115  void ReadExtents(const char *filename);
116 
117  // In case the image was loaded from a georeferenced format (such as
118  // GeoTIFF), provide access to the georef
119  vtProjection &GetProjection() { return m_proj; }
120  DRECT &GetExtents() { return m_extents; }
121 
122 protected:
123  // These two fields are rarely used
124  vtProjection m_proj;
125  DRECT m_extents;
126 };
127 typedef osg::ref_ptr<vtImageGeo> vtImageGeoPtr;
128 
129 bool vtImageInfo(const char *filename, int &width, int &height, int &depth);
130 
131 #endif // VTOSG_IMAGEH
132