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