3 * Purpose: PNG file reader
4 * Author: Alejandro Aguilar Sierra/Julian Smart
6 * Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
15 #pragma interface "pngread.h"
19 typedef unsigned char byte
;
22 #define WXIMA_COLORS DIB_PAL_COLORS
25 typedef byte
* ImagePointerType
;
27 typedef byte huge
* ImagePointerType
;
38 #define COLORTYPE_PALETTE 1
39 #define COLORTYPE_COLOR 2
40 #define COLORTYPE_ALPHA 4
47 ImagePointerType RawImage
; // Image data
49 int Width
, Height
; // Dimensions
50 int Depth
; // (bits x pixel)
51 int ColorType
; // Bit 1 = Palette used
55 long EfeWidth
; // Efective Width
57 LPBITMAPINFOHEADER lpbi
;
61 friend class wxPNGReaderIter
;
64 wxPNGReader (char* ImageFileName
); // Read an image file
67 void Create(int width
, int height
, int deep
, int colortype
=-1);
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
; };
77 int GetIndex(int x
, int y
);
78 bool GetRGB(int x
, int y
, byte
* r
, byte
* g
, byte
* b
);
80 bool SetIndex(int x
, int y
, int index
);
81 bool SetRGB(int x
, int y
, byte r
, byte g
, byte b
);
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
; }
90 inline int GetBGIndex(void) { return bgindex
; }
92 inline bool Inside(int x
, int y
)
93 { return (0<=y
&& y
<Height
&& 0<=x
&& x
<Width
); }
95 virtual wxBitmap
*GetBitmap(void);
96 virtual bool InstantiateBitmap(wxBitmap
*bitmap
);
97 wxMask
*CreateMask(void);
99 inline bool Ok(void) { return imageOK
; }
102 class wxPNGReaderIter
105 int Itx
, Ity
; // Counters
107 ImagePointerType IterImage
; // Image pointer
111 wxPNGReaderIter ( void );
112 wxPNGReaderIter ( wxPNGReader
*imax
);
113 operator wxPNGReader
* ();
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);
129 void SetSteps(int x
, int y
=0) { Stepx
= x
; Stepy
= y
; }
130 void GetSteps(int *x
, int *y
) { *x
= Stepx
; *y
= Stepy
; }
134 ////////////////////////// AD - for interlace ///////////////////////////////
136 /////////////////////////////////////////////////////////////////////////////
141 wxPNGReaderIter::wxPNGReaderIter(void)
150 wxPNGReaderIter::wxPNGReaderIter(wxPNGReader
*imax
): ima(imax
)
153 IterImage
= ima
->RawImage
;
159 wxPNGReaderIter::operator wxPNGReader
* ()
165 bool wxPNGReaderIter::ItOK ()
168 return ima
->Inside(Itx
, Ity
);
174 inline void wxPNGReaderIter::reset()
176 IterImage
= ima
->RawImage
;
180 inline void wxPNGReaderIter::upset()
184 IterImage
= ima
->RawImage
+ ima
->EfeWidth
*(ima
->Height
-1);
187 inline bool wxPNGReaderIter::NextRow()
189 if (++Ity
>= ima
->Height
) return 0;
190 IterImage
+= ima
->EfeWidth
;
194 inline bool wxPNGReaderIter::PrevRow()
196 if (--Ity
< 0) return 0;
197 IterImage
-= ima
->EfeWidth
;
201 ////////////////////////// AD - for interlace ///////////////////////////////
202 inline void wxPNGReaderIter::SetY(int y
)
204 if ((y
< 0) || (y
> ima
->Height
)) return;
206 IterImage
= ima
->RawImage
+ ima
->EfeWidth
*y
;
209 /////////////////////////////////////////////////////////////////////////////
211 inline void wxPNGReaderIter::SetRow(byte
*buf
, int n
)
213 // Here should be bcopy or memcpy
214 //_fmemcpy(IterImage, (void far *)buf, n);
218 for (int i
=0; i
<n
; i
++) IterImage
[i
] = buf
[i
];
221 inline void wxPNGReaderIter::GetRow(byte
*buf
, int n
)
223 for (int i
=0; i
<n
; i
++) buf
[i
] = IterImage
[i
];
226 inline ImagePointerType
wxPNGReaderIter::GetRow()
231 inline bool wxPNGReaderIter::NextByte()
233 if (++Itx
< ima
->EfeWidth
)
236 if (++Ity
< ima
->Height
)
238 IterImage
+= ima
->EfeWidth
;
245 inline bool wxPNGReaderIter::PrevByte()
252 IterImage
-= ima
->EfeWidth
;
259 inline bool wxPNGReaderIter::NextStep()
262 if (Itx
< ima
->EfeWidth
)
266 if (Ity
< ima
->Height
)
268 IterImage
+= ima
->EfeWidth
;
276 inline bool wxPNGReaderIter::PrevStep()
283 if (Ity
>= 0 && Ity
< ima
->Height
)
285 IterImage
-= ima
->EfeWidth
;