]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * File: pngread.h | |
3 | * Purpose: PNG file reader | |
4 | * Author: Alejandro Aguilar Sierra/Julian Smart | |
5 | * Created: 1995 | |
6 | * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra@servidor.unam.mx> | |
7 | * | |
8 | * | |
9 | */ | |
10 | ||
11 | #ifndef __PNGREAD__ | |
12 | #define __PNGREAD__ | |
13 | ||
14 | #ifdef __GNUG__ | |
15 | #pragma interface "pngread.h" | |
16 | #endif | |
17 | ||
18 | #ifndef byte | |
19 | typedef unsigned char byte; | |
20 | #endif | |
21 | ||
22 | #define WXIMA_COLORS DIB_PAL_COLORS | |
23 | ||
24 | #ifdef __WIN32__ | |
25 | typedef byte* ImagePointerType; | |
26 | #else | |
27 | typedef byte huge* ImagePointerType; | |
28 | #endif | |
29 | ||
30 | typedef struct | |
31 | { | |
32 | byte red; | |
33 | byte green; | |
34 | byte blue; | |
35 | } rgb_color_struct; | |
36 | ||
37 | ||
38 | #define COLORTYPE_PALETTE 1 | |
39 | #define COLORTYPE_COLOR 2 | |
40 | #define COLORTYPE_ALPHA 4 | |
41 | ||
42 | class wxPNGReader | |
43 | { | |
44 | protected: | |
45 | int filetype; | |
46 | char filename[255]; | |
47 | ImagePointerType RawImage; // Image data | |
48 | ||
49 | int Width, Height; // Dimensions | |
50 | int Depth; // (bits x pixel) | |
51 | int ColorType; // Bit 1 = Palette used | |
52 | // Bit 2 = Color used | |
53 | // Bit 3 = Alpha used | |
54 | ||
55 | long EfeWidth; // Efective Width | |
56 | ||
57 | LPBITMAPINFOHEADER lpbi; | |
58 | int bgindex; | |
59 | wxPalette* Palette; | |
60 | bool imageOK; | |
61 | friend class wxPNGReaderIter; | |
62 | public: | |
63 | wxPNGReader(void); | |
64 | wxPNGReader (char* ImageFileName); // Read an image file | |
65 | ~wxPNGReader (); | |
66 | ||
67 | void Create(int width, int height, int deep, int colortype=-1); | |
68 | ||
69 | bool ReadFile( char* ImageFileName=0 ); | |
70 | bool SaveFile( char* ImageFileName=0 ); | |
71 | bool SaveXPM(char *filename, char *name = 0); | |
72 | int GetWidth( void ) const { return Width; }; | |
73 | int GetHeight( void ) const { return Height; }; | |
74 | int GetDepth( void ) const { return Depth; }; | |
75 | int GetColorType( void ) const { return ColorType; }; | |
76 | ||
77 | int GetIndex(int x, int y); | |
78 | bool GetRGB(int x, int y, byte* r, byte* g, byte* b); | |
79 | ||
80 | bool SetIndex(int x, int y, int index); | |
81 | bool SetRGB(int x, int y, byte r, byte g, byte b); | |
82 | ||
83 | // ColorMap settings | |
84 | bool SetPalette(wxPalette* colourmap); | |
85 | bool SetPalette(int n, rgb_color_struct *rgb_struct); | |
86 | bool SetPalette(int n, byte *r, byte *g=0, byte *b=0); | |
87 | wxPalette* GetPalette() const { return Palette; } | |
88 | ||
89 | void NullData(); | |
90 | inline int GetBGIndex(void) { return bgindex; } | |
91 | ||
92 | inline bool Inside(int x, int y) | |
93 | { return (0<=y && y<Height && 0<=x && x<Width); } | |
94 | ||
95 | virtual wxBitmap *GetBitmap(void); | |
96 | virtual bool InstantiateBitmap(wxBitmap *bitmap); | |
97 | wxMask *CreateMask(void); | |
98 | ||
99 | inline bool Ok(void) { return imageOK; } | |
100 | }; | |
101 | ||
102 | class wxPNGReaderIter | |
103 | { | |
104 | protected: | |
105 | int Itx, Ity; // Counters | |
106 | int Stepx, Stepy; | |
107 | ImagePointerType IterImage; // Image pointer | |
108 | wxPNGReader *ima; | |
109 | public: | |
110 | // Constructors | |
111 | wxPNGReaderIter ( void ); | |
112 | wxPNGReaderIter ( wxPNGReader *imax ); | |
113 | operator wxPNGReader* (); | |
114 | ||
115 | // Iterators | |
116 | bool ItOK (); | |
117 | void reset (); | |
118 | void upset (); | |
119 | void SetRow(byte *buf, int n); | |
120 | void GetRow(byte *buf, int n); | |
121 | byte GetByte( ) { return IterImage[Itx]; } | |
122 | void SetByte(byte b) { IterImage[Itx] = b; } | |
123 | ImagePointerType GetRow(void); | |
124 | bool NextRow(); | |
125 | bool PrevRow(); | |
126 | bool NextByte(); | |
127 | bool PrevByte(); | |
128 | ||
129 | void SetSteps(int x, int y=0) { Stepx = x; Stepy = y; } | |
130 | void GetSteps(int *x, int *y) { *x = Stepx; *y = Stepy; } | |
131 | bool NextStep(); | |
132 | bool PrevStep(); | |
133 | ||
134 | ////////////////////////// AD - for interlace /////////////////////////////// | |
135 | void SetY(int y); | |
136 | ///////////////////////////////////////////////////////////////////////////// | |
137 | }; | |
138 | ||
139 | ||
140 | inline | |
141 | wxPNGReaderIter::wxPNGReaderIter(void) | |
142 | { | |
143 | ima = 0; | |
144 | IterImage = 0; | |
145 | Itx = Ity = 0; | |
146 | Stepx = Stepy = 0; | |
147 | } | |
148 | ||
149 | inline | |
150 | wxPNGReaderIter::wxPNGReaderIter(wxPNGReader *imax): ima(imax) | |
151 | { | |
152 | if (ima) | |
153 | IterImage = ima->RawImage; | |
154 | Itx = Ity = 0; | |
155 | Stepx = Stepy = 0; | |
156 | } | |
157 | ||
158 | inline | |
159 | wxPNGReaderIter::operator wxPNGReader* () | |
160 | { | |
161 | return ima; | |
162 | } | |
163 | ||
164 | inline | |
165 | bool wxPNGReaderIter::ItOK () | |
166 | { | |
167 | if (ima) | |
168 | return ima->Inside(Itx, Ity); | |
169 | else | |
170 | return FALSE; | |
171 | } | |
172 | ||
173 | ||
174 | inline void wxPNGReaderIter::reset() | |
175 | { | |
176 | IterImage = ima->RawImage; | |
177 | Itx = Ity = 0; | |
178 | } | |
179 | ||
180 | inline void wxPNGReaderIter::upset() | |
181 | { | |
182 | Itx = 0; | |
183 | Ity = ima->Height-1; | |
184 | IterImage = ima->RawImage + ima->EfeWidth*(ima->Height-1); | |
185 | } | |
186 | ||
187 | inline bool wxPNGReaderIter::NextRow() | |
188 | { | |
189 | if (++Ity >= ima->Height) return 0; | |
190 | IterImage += ima->EfeWidth; | |
191 | return 1; | |
192 | } | |
193 | ||
194 | inline bool wxPNGReaderIter::PrevRow() | |
195 | { | |
196 | if (--Ity < 0) return 0; | |
197 | IterImage -= ima->EfeWidth; | |
198 | return 1; | |
199 | } | |
200 | ||
201 | ////////////////////////// AD - for interlace /////////////////////////////// | |
202 | inline void wxPNGReaderIter::SetY(int y) | |
203 | { | |
204 | if ((y < 0) || (y > ima->Height)) return; | |
205 | Ity = y; | |
206 | IterImage = ima->RawImage + ima->EfeWidth*y; | |
207 | } | |
208 | ||
209 | ///////////////////////////////////////////////////////////////////////////// | |
210 | ||
211 | inline void wxPNGReaderIter::SetRow(byte *buf, int n) | |
212 | { | |
213 | // Here should be bcopy or memcpy | |
214 | //_fmemcpy(IterImage, (void far *)buf, n); | |
215 | if (n<0) | |
216 | n = ima->GetWidth(); | |
217 | ||
218 | for (int i=0; i<n; i++) IterImage[i] = buf[i]; | |
219 | } | |
220 | ||
221 | inline void wxPNGReaderIter::GetRow(byte *buf, int n) | |
222 | { | |
223 | for (int i=0; i<n; i++) buf[i] = IterImage[i]; | |
224 | } | |
225 | ||
226 | inline ImagePointerType wxPNGReaderIter::GetRow() | |
227 | { | |
228 | return IterImage; | |
229 | } | |
230 | ||
231 | inline bool wxPNGReaderIter::NextByte() | |
232 | { | |
233 | if (++Itx < ima->EfeWidth) | |
234 | return 1; | |
235 | else | |
236 | if (++Ity < ima->Height) | |
237 | { | |
238 | IterImage += ima->EfeWidth; | |
239 | Itx = 0; | |
240 | return 1; | |
241 | } else | |
242 | return 0; | |
243 | } | |
244 | ||
245 | inline bool wxPNGReaderIter::PrevByte() | |
246 | { | |
247 | if (--Itx >= 0) | |
248 | return 1; | |
249 | else | |
250 | if (--Ity >= 0) | |
251 | { | |
252 | IterImage -= ima->EfeWidth; | |
253 | Itx = 0; | |
254 | return 1; | |
255 | } else | |
256 | return 0; | |
257 | } | |
258 | ||
259 | inline bool wxPNGReaderIter::NextStep() | |
260 | { | |
261 | Itx += Stepx; | |
262 | if (Itx < ima->EfeWidth) | |
263 | return 1; | |
264 | else { | |
265 | Ity += Stepy; | |
266 | if (Ity < ima->Height) | |
267 | { | |
268 | IterImage += ima->EfeWidth; | |
269 | Itx = 0; | |
270 | return 1; | |
271 | } else | |
272 | return 0; | |
273 | } | |
274 | } | |
275 | ||
276 | inline bool wxPNGReaderIter::PrevStep() | |
277 | { | |
278 | Itx -= Stepx; | |
279 | if (Itx >= 0) | |
280 | return 1; | |
281 | else { | |
282 | Ity -= Stepy; | |
283 | if (Ity >= 0 && Ity < ima->Height) | |
284 | { | |
285 | IterImage -= ima->EfeWidth; | |
286 | Itx = 0; | |
287 | return 1; | |
288 | } else | |
289 | return 0; | |
290 | } | |
291 | } | |
292 | ||
293 | #endif | |
294 |