Commit | Line | Data |
---|---|---|
6762286d SC |
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; | |
5c6eb3a8 | 16 | #endif |
6762286d SC |
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 | char 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 | ||
49 | void *lpbi; | |
50 | int bgindex; | |
51 | wxPalette* m_palette; | |
52 | bool imageOK; | |
53 | friend class wxPNGReaderIter; | |
54 | public: | |
55 | wxPNGReader(void); | |
56 | wxPNGReader (char* ImageFileName); // Read an image file | |
57 | virtual ~wxPNGReader (); | |
58 | ||
59 | void Create(int width, int height, int deep, int colortype=-1); | |
60 | ||
61 | bool ReadFile( char* ImageFileName=0 ); | |
62 | bool SaveFile( char* ImageFileName=0 ); | |
63 | bool SaveXPM(char *filename, char *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 m_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() const { return IsOk(); } | |
92 | inline bool IsOk(void) { return imageOK; } | |
93 | }; | |
94 | ||
95 | class wxPNGReaderIter | |
96 | { | |
97 | protected: | |
98 | int Itx, Ity; // Counters | |
99 | int Stepx, Stepy; | |
100 | ImagePointerType IterImage; // Image pointer | |
101 | wxPNGReader *ima; | |
102 | public: | |
103 | // Constructors | |
104 | wxPNGReaderIter ( void ); | |
105 | wxPNGReaderIter ( wxPNGReader *imax ); | |
106 | operator wxPNGReader* (); | |
107 | ||
108 | // Iterators | |
109 | bool ItOK (); | |
110 | void reset (); | |
111 | void upset (); | |
112 | void SetRow(byte *buf, int n); | |
113 | void GetRow(byte *buf, int n); | |
114 | byte GetByte( ) { return IterImage[Itx]; } | |
115 | void SetByte(byte b) { IterImage[Itx] = b; } | |
116 | ImagePointerType GetRow(void); | |
117 | bool NextRow(); | |
118 | bool PrevRow(); | |
119 | bool NextByte(); | |
120 | bool PrevByte(); | |
121 | ||
122 | void SetSteps(int x, int y=0) { Stepx = x; Stepy = y; } | |
123 | void GetSteps(int *x, int *y) { *x = Stepx; *y = Stepy; } | |
124 | bool NextStep(); | |
125 | bool PrevStep(); | |
126 | ||
127 | ////////////////////////// AD - for interlace /////////////////////////////// | |
128 | void SetY(int y); | |
129 | ///////////////////////////////////////////////////////////////////////////// | |
130 | }; | |
131 | ||
132 | ||
133 | inline | |
134 | wxPNGReaderIter::wxPNGReaderIter(void) | |
135 | { | |
136 | ima = 0; | |
137 | IterImage = 0; | |
138 | Itx = Ity = 0; | |
139 | Stepx = Stepy = 0; | |
140 | } | |
141 | ||
142 | inline | |
143 | wxPNGReaderIter::wxPNGReaderIter(wxPNGReader *imax): ima(imax) | |
144 | { | |
145 | if (ima) | |
146 | IterImage = ima->RawImage; | |
147 | Itx = Ity = 0; | |
148 | Stepx = Stepy = 0; | |
149 | } | |
150 | ||
151 | inline | |
152 | wxPNGReaderIter::operator wxPNGReader* () | |
153 | { | |
154 | return ima; | |
155 | } | |
156 | ||
157 | inline | |
158 | bool wxPNGReaderIter::ItOK () | |
159 | { | |
160 | if (ima) | |
161 | return ima->Inside(Itx, Ity); | |
162 | else | |
163 | return FALSE; | |
164 | } | |
165 | ||
166 | ||
167 | inline void wxPNGReaderIter::reset() | |
168 | { | |
169 | IterImage = ima->RawImage; | |
170 | Itx = Ity = 0; | |
171 | } | |
172 | ||
173 | inline void wxPNGReaderIter::upset() | |
174 | { | |
175 | Itx = 0; | |
176 | Ity = ima->Height-1; | |
177 | IterImage = ima->RawImage + ima->EfeWidth*(ima->Height-1); | |
178 | } | |
179 | ||
180 | inline bool wxPNGReaderIter::NextRow() | |
181 | { | |
182 | if (++Ity >= ima->Height) return 0; | |
183 | IterImage += ima->EfeWidth; | |
184 | return 1; | |
185 | } | |
186 | ||
187 | inline bool wxPNGReaderIter::PrevRow() | |
188 | { | |
189 | if (--Ity < 0) return 0; | |
190 | IterImage -= ima->EfeWidth; | |
191 | return 1; | |
192 | } | |
193 | ||
194 | ////////////////////////// AD - for interlace /////////////////////////////// | |
195 | inline void wxPNGReaderIter::SetY(int y) | |
196 | { | |
197 | if ((y < 0) || (y > ima->Height)) return; | |
198 | Ity = y; | |
199 | IterImage = ima->RawImage + ima->EfeWidth*y; | |
200 | } | |
201 | ||
202 | ///////////////////////////////////////////////////////////////////////////// | |
203 | ||
204 | inline void wxPNGReaderIter::SetRow(byte *buf, int n) | |
205 | { | |
206 | // Here should be bcopy or memcpy | |
207 | //_fmemcpy(IterImage, (void far *)buf, n); | |
208 | if (n<0) | |
209 | n = ima->GetWidth(); | |
210 | ||
211 | for (int i=0; i<n; i++) IterImage[i] = buf[i]; | |
212 | } | |
213 | ||
214 | inline void wxPNGReaderIter::GetRow(byte *buf, int n) | |
215 | { | |
216 | for (int i=0; i<n; i++) buf[i] = IterImage[i]; | |
217 | } | |
218 | ||
219 | inline ImagePointerType wxPNGReaderIter::GetRow() | |
220 | { | |
221 | return IterImage; | |
222 | } | |
223 | ||
224 | inline bool wxPNGReaderIter::NextByte() | |
225 | { | |
226 | if (++Itx < ima->EfeWidth) | |
227 | return 1; | |
228 | else | |
229 | if (++Ity < ima->Height) | |
230 | { | |
231 | IterImage += ima->EfeWidth; | |
232 | Itx = 0; | |
233 | return 1; | |
234 | } else | |
235 | return 0; | |
236 | } | |
237 | ||
238 | inline bool wxPNGReaderIter::PrevByte() | |
239 | { | |
240 | if (--Itx >= 0) | |
241 | return 1; | |
242 | else | |
243 | if (--Ity >= 0) | |
244 | { | |
245 | IterImage -= ima->EfeWidth; | |
246 | Itx = 0; | |
247 | return 1; | |
248 | } else | |
249 | return 0; | |
250 | } | |
251 | ||
252 | inline bool wxPNGReaderIter::NextStep() | |
253 | { | |
254 | Itx += Stepx; | |
255 | if (Itx < ima->EfeWidth) | |
256 | return 1; | |
257 | else { | |
258 | Ity += Stepy; | |
259 | if (Ity < ima->Height) | |
260 | { | |
261 | IterImage += ima->EfeWidth; | |
262 | Itx = 0; | |
263 | return 1; | |
264 | } else | |
265 | return 0; | |
266 | } | |
267 | } | |
268 | ||
269 | inline bool wxPNGReaderIter::PrevStep() | |
270 | { | |
271 | Itx -= Stepx; | |
272 | if (Itx >= 0) | |
273 | return 1; | |
274 | else { | |
275 | Ity -= Stepy; | |
276 | if (Ity >= 0 && Ity < ima->Height) | |
277 | { | |
278 | IterImage -= ima->EfeWidth; | |
279 | Itx = 0; | |
280 | return 1; | |
281 | } else | |
282 | return 0; | |
283 | } | |
284 | } | |
285 | ||
286 | #endif | |
287 |