Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsimage.h
Go to the documentation of this file.
1 #ifndef __VSIMAGE__H
2 #define __VSIMAGE__H
3 
4 #include "vsfilesystem.h"
5 
6 extern int PNG_HAS_PALETTE;
7 extern int PNG_HAS_COLOR;
8 extern int PNG_HAS_ALPHA;
9 
10 #ifndef WIN32
11 typedef unsigned int DWORD;
12 typedef int LONG;
13 typedef unsigned short WORD;
14 typedef unsigned char BYTE;
20 typedef struct
21 {
22  DWORD biSize;
23  LONG biWidth;
24  LONG biHeight;
25  WORD biPlanes;
26  WORD biBitCount;
27  DWORD biCompression;
28  DWORD biSizeImage;
29  LONG biXPelsPerMeter;
30  LONG biYPelsPerMeter;
31  DWORD biClrUsed;
32  DWORD biClrImportant;
34 
39 typedef struct
40 {
41  WORD bfType;
42  DWORD bfSize;
43  WORD bfReserved1;
44  WORD bfReserved2;
45  DWORD bfOffBits;
47 
51 typedef struct
52 {
53  BYTE rgbBlue;
54  BYTE rgbGreen;
55  BYTE rgbRed;
56  BYTE rgbReserved;
57 } RGBQUAD;
58 #else
59 #ifndef NOMINMAX
60 #define NOMINMAX
61 #endif //tells VCC not to generate min/max macros
62 #include <windows.h>
63 #include <wingdi.h>
64 #endif
65 
66 /*
67  * Standard DDS formats.
68  */
69 
70 #ifndef GL_COMPRESSED_RGB_S3TC_DXT1_EXT
71 #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
72 #endif
73 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
74 #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
75 #endif
76 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
77 #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
78 #endif
79 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
80 #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
81 #endif
82 #ifndef GL_TEXTURE_COMPRESSION_HINT_ARB
83 #define GL_TEXTURE_COMPRESSION_HINT_ARB 0x84EF
84 #endif
85 
86 /*
87  * DDS header structs
88  * Some DDS files may not fill in all data members.
89  */
90 typedef struct
91 {
92  char fourcc[4];
93  int bpp;
94 } pxlformat;
95 
96 typedef struct
97 {
98  int size;
99  int flags;
100  int height;
101  int width;
102  int linsize;
103  int depth;
104  int nmips;
106  int dcaps1;
107  int dcaps2;
108 } ddsHeader;
109 //End DDS header
110 
111 typedef struct
112 {
113  char *Buffer;
114  int Pos;
116 
117 typedef unsigned char* (textureTransform)( int &bpp, int &color_type, unsigned long &width, unsigned long &height,
118  unsigned char **row_pointers );
122 
123 void png_write( const char *myfile, unsigned char *data, unsigned int width, unsigned int height, bool alpha, char bpp );
124 
126 
127 /*
128  * VSImage is a container and low level api to texture input data.
129  * Image loading is initiated with a call to VSImage->ReadImage().
130  * ReadImage then calls CheckFormat to see if the image file supplied is a valid format.
131  * img_type is set to the detected format.
132  * Depending on the format found, it calls the correct ReadType function.
133  * The ReadType function reads in data with img_file->Read and converts the data into
134  * a format that GL understands, either uncompressed image data or S3TC compressed data.
135  * ReadType will also set img_depth,sizeX,sizeY,img_alpha,type,mode.
136  * That data is then returned to ReadImage which then returns that data to the caller.
137  */
138 class VSImage
139 {
140 private:
141  VSFileSystem::VSFile *img_file;
142  VSFileSystem::VSFile *img_file2;
143  textureTransform *tt;
144  VSImageType img_type;
145 
146  int img_depth;
147  int img_color_type;
148  bool img_alpha;
149  bool strip_16;
150  bool flip;
151 
152 protected:
153 
155  {
157  SIDE_POS_X =0x01,
158  SIDE_NEG_X =0x02,
159  SIDE_POS_Y =0x04,
160  SIDE_NEG_Y =0x08,
161  SIDE_POS_Z =0x10,
163  };
164 
165  char img_sides;
167 
168 private:
169 
170  void Init();
171  void Init( VSFileSystem::VSFile *f, textureTransform *t = NULL, bool strip = false, VSFileSystem::VSFile *f2 = NULL );
172 
173  VSFileSystem::VSError CheckPNGSignature( VSFileSystem::VSFile *file );
174  VSFileSystem::VSError CheckJPEGSignature( VSFileSystem::VSFile *file );
175  VSFileSystem::VSError CheckBMPSignature( VSFileSystem::VSFile *file );
176  VSFileSystem::VSError CheckDDSSignature( VSFileSystem::VSFile *file );
177 
178 /*
179  * Calls above Check methods to determine if mime type matches supported format.
180  * Sets img_type to correct type.
181  */
182  void CheckFormat( VSFileSystem::VSFile *file );
183 
184 /*
185  * The following are format specific read functions called by ReadImage().
186  * They are responsible for returning usable image data, either
187  * an uncompressed data of supported depth/type or DDS
188  */
189  unsigned char * ReadPNG();
190  unsigned char * ReadJPEG();
191  unsigned char * ReadBMP();
192  unsigned char * ReadDDS();
193 
194  VSFileSystem::VSError WritePNG( unsigned char *data );
195  VSFileSystem::VSError WriteJPEG( unsigned char *data );
196  VSFileSystem::VSError WriteBMP( unsigned char *data );
197 
198  void AllocatePalette();
199 
200 public: VSImage();
201 //f2 is needed for bmp loading
202  VSImage( VSFileSystem::VSFile *f, textureTransform *t = NULL, bool strip = false, VSFileSystem::VSFile *f2 = NULL );
203  ~VSImage();
204 
205 //if we statically allocate it, then gl_texture will kill it when destructor is called...and if we delete this texture we be messed
206  unsigned char *palette;
207 
208 /*
209  * Position 3 and greater of VSImageMode are helper modes to differentiate the correct modes of DDS and PNG files.
210  * DDS files are assumed to be 24 or 32 bit.
211  */
213 
215  unsigned long sizeX;
216  unsigned long sizeY;
217 
218 //Defined for gcc which pads the size of structs
219 //const static int SIZEOF_BITMAPFILEHEADER;
220  LOCALCONST_DECL( int, SIZEOF_BITMAPFILEHEADER, sizeof (WORD)+sizeof (DWORD)+sizeof (WORD)+sizeof (WORD)+sizeof (DWORD) )
221 //Defined for gcc which pads the size of structs
224  *sizeof (LONG)
225  +2*sizeof (DWORD) )
226 //const static int SIZEOF_BITMAPINFOHEADER;
227 //Defined for gcc which pads size of structs (not entirely necessary)
228 //const static int SIZEOF_RGBQUAD;
230 
231 //f2 is needed for bmp loading
232  unsigned char*ReadImage( VSFileSystem::VSFile*f,
233  textureTransform*t = NULL,
234  bool strip = false,
235  VSFileSystem::VSFile*f2 = NULL );
236 
237  VSFileSystem::VSError WriteImage( char *filename,
238  unsigned char *data,
239  VSImageType type,
240  unsigned int width,
241  unsigned int height,
242  bool alpha = 1,
243  char bpp = 16,
244  VSFileSystem::VSFileType ft = VSFileSystem::UnknownFile,
245  bool flip = false );
246  VSFileSystem::VSError WriteImage( VSFileSystem::VSFile *pf,
247  unsigned char *data,
248  VSImageType type,
249  unsigned int width,
250  unsigned int height,
251  bool alpha = 1,
252  char bpp = 16,
253  bool flip = false );
254 
255  int Depth() const
256  {
257  return this->img_depth;
258  }
259  int Format() const
260  {
261  return this->img_color_type;
262  }
263  char Sides() const
264  {
265  return this->img_sides;
266  }
267  bool isCube() const
268  {
269  return this->img_sides != SIDE_SINGLE;
270  }
271 };
272 
273 #endif
274