pngread.h 5.8 KB

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