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