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