home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / win32 / include / cximage / ximaiter.h < prev    next >
C/C++ Source or Header  |  2004-02-29  |  7KB  |  253 lines

  1. /*
  2.  * File:    ImaIter.h
  3.  * Purpose:    Declaration of the Platform Independent Image Base Class
  4.  * Author:    Alejandro Aguilar Sierra
  5.  * Created:    1995
  6.  * Copyright:    (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
  7.  *
  8.  * 07/08/2001 Davide Pizzolato - www.xdp.it
  9.  * - removed slow loops
  10.  * - added safe checks
  11.  *
  12.  * Permission is given by the author to freely redistribute and include
  13.  * this code in any program as long as this credit is given where due.
  14.  *
  15.  * COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
  16.  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
  17.  * THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
  18.  * OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
  19.  * CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
  20.  * THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
  21.  * SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
  22.  * PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  23.  * THIS DISCLAIMER.
  24.  *
  25.  * Use at your own risk!
  26.  * ==========================================================
  27.  */
  28.  
  29. #if !defined(__ImaIter_h)
  30. #define __ImaIter_h
  31.  
  32. #include "ximage.h"
  33. #include "ximadefs.h"
  34.  
  35. class CImageIterator
  36. {
  37. friend class CxImage;
  38. protected:
  39.     int Itx, Ity;        // Counters
  40.     int Stepx, Stepy;
  41.     BYTE* IterImage;    //  Image pointer
  42.     CxImage *ima;
  43. public:
  44.     // Constructors
  45.     CImageIterator ( void );
  46.     CImageIterator ( CxImage *image );
  47.     operator CxImage* ();
  48.  
  49.     // Iterators
  50.     BOOL ItOK ();
  51.     void Reset ();
  52.     void Upset ();
  53.     void SetRow(BYTE *buf, int n);
  54.     void GetRow(BYTE *buf, int n);
  55.     BYTE GetByte( ) { return IterImage[Itx]; }
  56.     void SetByte(BYTE b) { IterImage[Itx] = b; }
  57.     BYTE* GetRow(void);
  58.     BYTE* GetRow(int n);
  59.     BOOL NextRow();
  60.     BOOL PrevRow();
  61.     BOOL NextByte();
  62.     BOOL PrevByte();
  63.  
  64.     void SetSteps(int x, int y=0) {  Stepx = x; Stepy = y; }
  65.     void GetSteps(int *x, int *y) {  *x = Stepx; *y = Stepy; }
  66.     BOOL NextStep();
  67.     BOOL PrevStep();
  68.  
  69.     void SetY(int y);    /* AD - for interlace */
  70.     int  GetY() {return Ity;}
  71.     BOOL GetCol(BYTE* pCol, DWORD x);
  72.     BOOL SetCol(BYTE* pCol, DWORD x);
  73. };
  74.  
  75. /////////////////////////////////////////////////////////////////////
  76. inline
  77. CImageIterator::CImageIterator(void)
  78. {
  79.     ima = 0;
  80.     IterImage = 0;
  81.     Itx = Ity = 0;
  82.     Stepx = Stepy = 0;
  83. }
  84. /////////////////////////////////////////////////////////////////////
  85. inline
  86. CImageIterator::CImageIterator(CxImage *imageImpl): ima(imageImpl)
  87. {
  88.     if (ima) IterImage = ima->GetBits();
  89.     Itx = Ity = 0;
  90.     Stepx = Stepy = 0;
  91. }
  92. /////////////////////////////////////////////////////////////////////
  93. inline
  94. CImageIterator::operator CxImage* ()
  95. {
  96.     return ima;
  97. }
  98. /////////////////////////////////////////////////////////////////////
  99. inline BOOL CImageIterator::ItOK ()
  100. {
  101.     if (ima) return ima->IsInside(Itx, Ity);
  102.     else     return FALSE;
  103. }
  104. /////////////////////////////////////////////////////////////////////
  105. inline void CImageIterator::Reset()
  106. {
  107.     if (ima) IterImage = ima->GetBits();
  108.     else     IterImage=0;
  109.     Itx = Ity = 0;
  110. }
  111. /////////////////////////////////////////////////////////////////////
  112. inline void CImageIterator::Upset()
  113. {
  114.     Itx = 0;
  115.     Ity = ima->GetHeight()-1;
  116.     IterImage = ima->GetBits() + ima->GetEffWidth()*(ima->GetHeight()-1);
  117. }
  118. /////////////////////////////////////////////////////////////////////
  119. inline BOOL CImageIterator::NextRow()
  120. {
  121.     if (++Ity >= (int)ima->GetHeight()) return 0;
  122.     IterImage += ima->GetEffWidth();
  123.     return 1;
  124. }
  125. /////////////////////////////////////////////////////////////////////
  126. inline BOOL CImageIterator::PrevRow()
  127. {
  128.     if (--Ity < 0) return 0;
  129.     IterImage -= ima->GetEffWidth();
  130.     return 1;
  131. }
  132. /* AD - for interlace */
  133. inline void CImageIterator::SetY(int y)
  134. {
  135.     if ((y < 0) || (y > (int)ima->GetHeight())) return;
  136.     Ity = y;
  137.     IterImage = ima->GetBits() + ima->GetEffWidth()*y;
  138. }
  139. /////////////////////////////////////////////////////////////////////
  140. inline void CImageIterator::SetRow(BYTE *buf, int n)
  141. {
  142.     if (n<0) n = (int)ima->GetEffWidth();
  143.     else n = min(n,(int)ima->GetEffWidth());
  144.  
  145.     if (IterImage) memcpy(IterImage,buf,n);
  146. }
  147. /////////////////////////////////////////////////////////////////////
  148. inline void CImageIterator::GetRow(BYTE *buf, int n)
  149. {
  150.     if ((buf!=NULL)&&(n>0)) memcpy(buf,IterImage,n);
  151. }
  152. /////////////////////////////////////////////////////////////////////
  153. inline BYTE* CImageIterator::GetRow()
  154. {
  155.     return IterImage;
  156. }
  157. /////////////////////////////////////////////////////////////////////
  158. inline BYTE* CImageIterator::GetRow(int n)
  159. {
  160.     SetY(n);
  161.     return IterImage;
  162. }
  163. /////////////////////////////////////////////////////////////////////
  164. inline BOOL CImageIterator::NextByte()
  165. {
  166.     if (++Itx < (int)ima->GetEffWidth()) return 1;
  167.     else
  168.         if (++Ity < (int)ima->GetHeight()){
  169.             IterImage += ima->GetEffWidth();
  170.             Itx = 0;
  171.             return 1;
  172.         } else
  173.             return 0;
  174. }
  175. /////////////////////////////////////////////////////////////////////
  176. inline BOOL CImageIterator::PrevByte()
  177. {
  178.   if (--Itx >= 0) return 1;
  179.   else
  180.       if (--Ity >= 0){
  181.           IterImage -= ima->GetEffWidth();
  182.           Itx = 0;
  183.           return 1;
  184.       } else
  185.           return 0;
  186. }
  187. /////////////////////////////////////////////////////////////////////
  188. inline BOOL CImageIterator::NextStep()
  189. {
  190.     Itx += Stepx;
  191.     if (Itx < (int)ima->GetEffWidth()) return 1;
  192.     else {
  193.         Ity += Stepy;
  194.         if (Ity < (int)ima->GetHeight()){
  195.             IterImage += ima->GetEffWidth();
  196.             Itx = 0;
  197.             return 1;
  198.         } else
  199.             return 0;
  200.     }
  201. }
  202. /////////////////////////////////////////////////////////////////////
  203. inline BOOL CImageIterator::PrevStep()
  204. {
  205.     Itx -= Stepx;
  206.     if (Itx >= 0) return 1;
  207.     else {       
  208.         Ity -= Stepy;
  209.         if (Ity >= 0 && Ity < (int)ima->GetHeight()) {
  210.             IterImage -= ima->GetEffWidth();
  211.             Itx = 0;
  212.             return 1;
  213.         } else
  214.             return 0;
  215.     }
  216. }
  217. /////////////////////////////////////////////////////////////////////
  218. inline BOOL CImageIterator::GetCol(BYTE* pCol, DWORD x)
  219. {
  220.     if ((pCol==0)||(ima->GetBpp()<8)||(x>=ima->GetWidth()))
  221.         return 0;
  222.     DWORD h = ima->GetHeight();
  223.     DWORD line = ima->GetEffWidth();
  224.     BYTE bytes = ima->GetBpp()>>3;
  225.     BYTE* pSrc;
  226.     for (DWORD y=0;y<h;y++){
  227.         pSrc = ima->GetBits(y) + x*bytes;
  228.         for (BYTE w=0;w<bytes;w++){
  229.             *pCol++=*pSrc++;
  230.         }
  231.     }
  232.     return 1;
  233. }
  234. /////////////////////////////////////////////////////////////////////
  235. inline BOOL CImageIterator::SetCol(BYTE* pCol, DWORD x)
  236. {
  237.     if ((pCol==0)||(ima->GetBpp()<8)||(x>=ima->GetWidth()))
  238.         return 0;
  239.     DWORD h = ima->GetHeight();
  240.     DWORD line = ima->GetEffWidth();
  241.     BYTE bytes = ima->GetBpp()>>3;
  242.     BYTE* pSrc;
  243.     for (DWORD y=0;y<h;y++){
  244.         pSrc = ima->GetBits(y) + x*bytes;
  245.         for (BYTE w=0;w<bytes;w++){
  246.             *pSrc++=*pCol++;
  247.         }
  248.     }
  249.     return 1;
  250. }
  251. /////////////////////////////////////////////////////////////////////
  252. #endif
  253.