]> Creatis software - clitk.git/blob - utilities/CxImage/ximaiter.h
cosmetic
[clitk.git] / utilities / CxImage / ximaiter.h
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 "ximadef.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!=NULL)&&(buf!=NULL)&&(n>0)) memcpy(IterImage,buf,n);
146 }
147 /////////////////////////////////////////////////////////////////////
148 inline void CImageIterator::GetRow(BYTE *buf, int n)
149 {
150         if ((IterImage!=NULL)&&(buf!=NULL)&&(n>0))
151                 memcpy(buf,IterImage,min(n,(int)ima->GetEffWidth()));
152 }
153 /////////////////////////////////////////////////////////////////////
154 inline BYTE* CImageIterator::GetRow()
155 {
156         return IterImage;
157 }
158 /////////////////////////////////////////////////////////////////////
159 inline BYTE* CImageIterator::GetRow(int n)
160 {
161         SetY(n);
162         return IterImage;
163 }
164 /////////////////////////////////////////////////////////////////////
165 inline BOOL CImageIterator::NextByte()
166 {
167         if (++Itx < (int)ima->GetEffWidth()) return 1;
168         else
169                 if (++Ity < (int)ima->GetHeight()){
170                         IterImage += ima->GetEffWidth();
171                         Itx = 0;
172                         return 1;
173                 } else
174                         return 0;
175 }
176 /////////////////////////////////////////////////////////////////////
177 inline BOOL CImageIterator::PrevByte()
178 {
179   if (--Itx >= 0) return 1;
180   else
181           if (--Ity >= 0){
182                   IterImage -= ima->GetEffWidth();
183                   Itx = 0;
184                   return 1;
185           } else
186                   return 0;
187 }
188 /////////////////////////////////////////////////////////////////////
189 inline BOOL CImageIterator::NextStep()
190 {
191         Itx += Stepx;
192         if (Itx < (int)ima->GetEffWidth()) return 1;
193         else {
194                 Ity += Stepy;
195                 if (Ity < (int)ima->GetHeight()){
196                         IterImage += ima->GetEffWidth();
197                         Itx = 0;
198                         return 1;
199                 } else
200                         return 0;
201         }
202 }
203 /////////////////////////////////////////////////////////////////////
204 inline BOOL CImageIterator::PrevStep()
205 {
206         Itx -= Stepx;
207         if (Itx >= 0) return 1;
208         else {       
209                 Ity -= Stepy;
210                 if (Ity >= 0 && Ity < (int)ima->GetHeight()) {
211                         IterImage -= ima->GetEffWidth();
212                         Itx = 0;
213                         return 1;
214                 } else
215                         return 0;
216         }
217 }
218 /////////////////////////////////////////////////////////////////////
219 inline BOOL CImageIterator::GetCol(BYTE* pCol, DWORD x)
220 {
221         if ((pCol==0)||(ima->GetBpp()<8)||(x>=ima->GetWidth()))
222                 return 0;
223         DWORD h = ima->GetHeight();
224         //DWORD line = ima->GetEffWidth();
225         BYTE bytes = (BYTE)(ima->GetBpp()>>3);
226         BYTE* pSrc;
227         for (DWORD y=0;y<h;y++){
228                 pSrc = ima->GetBits(y) + x*bytes;
229                 for (BYTE w=0;w<bytes;w++){
230                         *pCol++=*pSrc++;
231                 }
232         }
233         return 1;
234 }
235 /////////////////////////////////////////////////////////////////////
236 inline BOOL CImageIterator::SetCol(BYTE* pCol, DWORD x)
237 {
238         if ((pCol==0)||(ima->GetBpp()<8)||(x>=ima->GetWidth()))
239                 return 0;
240         DWORD h = ima->GetHeight();
241         //DWORD line = ima->GetEffWidth();
242         BYTE bytes = (BYTE)(ima->GetBpp()>>3);
243         BYTE* pSrc;
244         for (DWORD y=0;y<h;y++){
245                 pSrc = ima->GetBits(y) + x*bytes;
246                 for (BYTE w=0;w<bytes;w++){
247                         *pSrc++=*pCol++;
248                 }
249         }
250         return 1;
251 }
252 /////////////////////////////////////////////////////////////////////
253 #endif