-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCImageIterator.h
More file actions
382 lines (342 loc) · 8.98 KB
/
Copy pathCImageIterator.h
File metadata and controls
382 lines (342 loc) · 8.98 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* File: CImageIterator.h
* Purpose: Declaration of the Platform Independent Image Base Class
* Author: Alejandro Aguilar Sierra
* Created: 1995
* Copyright: (c) 1995, Alejandro Aguilar Sierra <asierra(at)servidor(dot)unam(dot)mx>
*
* 07/08/2001 Davide Pizzolato - www.xdp.it
* - removed slow loops
* - added safe checks
*
* Permission is given by the author to freely redistribute and include
* this code in any program as int32_t as this credit is given where due.
*
* COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
* THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
* OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
* CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
* THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
* SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
* PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
* THIS DISCLAIMER.
*
* Use at your own risk!
* ==========================================================
*/
#pragma once
#include "CxImage.h"
#include "CxDefines.h"
namespace CppImage
{
//////////////////////////////////////////////////////////////////////////
class CImageIterator
{
friend class CxImage;
protected:
int32_t Itx;
int32_t Ity; // Counters
int32_t Stepx;
int32_t Stepy;
uint8_t* IterImage; // Image pointer
CxImage* mImage;
public:
// Constructors
CImageIterator();
CImageIterator(CxImage* inImage);
// Operators
operator CxImage* ();
// Iterators
bool ItOK();
void Reset();
void Upset();
void GetRow(uint8_t* buf, int32_t n);
uint8_t* GetRow();
uint8_t* GetRow(int32_t n);
void SetRow(uint8_t* buf, int32_t n);
uint8_t GetByte();
void SetByte(uint8_t b);
bool NextRow();
bool PrevRow();
bool NextByte();
bool PrevByte();
void GetSteps(int32_t* x, int32_t* y);
void SetSteps(int32_t x, int32_t y = 0);
bool NextStep();
bool PrevStep();
int32_t GetY();
void SetY(int32_t y); /* AD - for interlace */
bool GetCol(uint8_t* outCol, uint32_t inColIndex);
bool SetCol(uint8_t* inCol, uint32_t inColIndex);
};
}
namespace CppImage
{
//////////////////////////////////////////////////////////////////////////
inline void CImageIterator::SetByte(uint8_t b)
{
IterImage[Itx] = b;
}
//////////////////////////////////////////////////////////////////////////
inline uint8_t CImageIterator::GetByte()
{
return IterImage[Itx];
}
//////////////////////////////////////////////////////////////////////////
inline void CImageIterator::SetSteps(int32_t x, int32_t y)
{
Stepx = x;
Stepy = y;
}
//////////////////////////////////////////////////////////////////////////
inline void CImageIterator::GetSteps(int32_t* x, int32_t* y)
{
*x = Stepx;
*y = Stepy;
}
//////////////////////////////////////////////////////////////////////////
inline int32_t CImageIterator::GetY()
{
return Ity;
}
//////////////////////////////////////////////////////////////////////////
inline CImageIterator::CImageIterator(void)
{
mImage = NULL;
IterImage = 0;
Itx = 0;
Ity = 0;
Stepx = 0;
Stepy = 0;
}
/////////////////////////////////////////////////////////////////////
inline CImageIterator::CImageIterator(CxImage* inImage)
: mImage(inImage)
{
if (mImage != NULL)
IterImage = mImage->GetBits();
Itx = 0;
Ity = 0;
Stepx = 0;
Stepy = 0;
}
/////////////////////////////////////////////////////////////////////
inline CImageIterator::operator CxImage* ()
{
return mImage;
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::ItOK()
{
return (mImage != NULL) && mImage->IsInside(Itx, Ity);
}
/////////////////////////////////////////////////////////////////////
inline void CImageIterator::Reset()
{
if (mImage != NULL)
IterImage = mImage->GetBits();
else
IterImage = 0;
Itx = 0;
Ity = 0;
}
/////////////////////////////////////////////////////////////////////
inline void CImageIterator::Upset()
{
Itx = 0;
Ity = mImage->GetHeight() - 1;
IterImage = mImage->GetBits() + mImage->GetEffWidth() * (mImage->GetHeight() - 1);
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::NextRow()
{
++Ity;
if (Ity >= static_cast<int32_t>(mImage->GetHeight()))
return 0;
IterImage += mImage->GetEffWidth();
return 1;
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::PrevRow()
{
--Ity;
if (Ity < 0)
return false;
IterImage -= mImage->GetEffWidth();
return true;
}
//////////////////////////////////////////////////////////////////////////
/* AD - for interlace */
inline void CImageIterator::SetY(int32_t y)
{
if ((y < 0) || (y > static_cast<int32_t>(mImage->GetHeight())))
return;
Ity = y;
IterImage = mImage->GetBits() + mImage->GetEffWidth() * y;
}
/////////////////////////////////////////////////////////////////////
inline void CImageIterator::SetRow(uint8_t *buf, int32_t n)
{
int32_t effWidth = static_cast<int32_t>(mImage->GetEffWidth());
n = (n < 0) ? effWidth : min(n, effWidth);
if ((IterImage != NULL) && (buf != NULL) && (n > 0))
{
memcpy(IterImage, buf, n);
}
}
/////////////////////////////////////////////////////////////////////
inline void CImageIterator::GetRow(uint8_t *buf, int32_t n)
{
if ((IterImage != NULL) && (buf != NULL) && (n > 0))
{
memcpy(buf, IterImage, min(n, static_cast<int32_t>(mImage->GetEffWidth())));
}
}
/////////////////////////////////////////////////////////////////////
inline uint8_t* CImageIterator::GetRow()
{
return IterImage;
}
/////////////////////////////////////////////////////////////////////
inline uint8_t* CImageIterator::GetRow(int32_t n)
{
SetY(n);
return IterImage;
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::NextByte()
{
++Itx;
if (Itx < static_cast<int32_t>(mImage->GetEffWidth()))
{
return 1;
}
else
{
if (++Ity < static_cast<int32_t>(mImage->GetHeight()))
{
IterImage += mImage->GetEffWidth();
Itx = 0;
return 1;
}
else
{
return 0;
}
}
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::PrevByte()
{
if (--Itx >= 0)
{
return 1;
}
else
{
if (--Ity >= 0)
{
IterImage -= mImage->GetEffWidth();
Itx = 0;
return 1;
}
else
{
return 0;
}
}
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::NextStep()
{
Itx += Stepx;
if (Itx < static_cast<int32_t>(mImage->GetEffWidth()))
{
return 1;
}
else
{
Ity += Stepy;
if (Ity < static_cast<int32_t>(mImage->GetHeight()))
{
IterImage += mImage->GetEffWidth();
Itx = 0;
return 1;
}
else
{
return 0;
}
}
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::PrevStep()
{
Itx -= Stepx;
if (Itx >= 0)
{
return 1;
}
else
{
Ity -= Stepy;
if (Ity >= 0 && Ity < static_cast<int32_t>(mImage->GetHeight()))
{
IterImage -= mImage->GetEffWidth();
Itx = 0;
return 1;
}
else
{
return 0;
}
}
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::GetCol(uint8_t* outCol, uint32_t inColIndex)
{
if ((outCol == NULL) ||
(mImage->GetBpp() < 8) ||
(inColIndex >= mImage->GetWidth()))
{
return 0;
}
uint32_t h = mImage->GetHeight();
//uint32_t line = mImage->GetEffWidth();
uint8_t bytes = static_cast<uint8_t>(mImage->GetBpp() >> 3);
uint8_t* pSrc;
for (uint32_t y = 0; y < h; y++)
{
pSrc = mImage->GetBits(y) + inColIndex * bytes;
for (uint8_t w = 0; w < bytes; w++)
{
*outCol++=*pSrc++;
}
}
return 1;
}
/////////////////////////////////////////////////////////////////////
inline bool CImageIterator::SetCol(uint8_t* inCol, uint32_t inColIndex)
{
if ((inCol == 0) ||
(mImage->GetBpp() < 8) ||
(inColIndex >= mImage->GetWidth()))
{
return 0;
}
uint32_t h = mImage->GetHeight();
//uint32_t line = mImage->GetEffWidth();
uint8_t bytes = (uint8_t)(mImage->GetBpp() >> 3);
uint8_t* pSrc;
for (uint32_t y = 0; y < h; y++)
{
pSrc = mImage->GetBits(y) + inColIndex * bytes;
for (uint8_t w = 0; w < bytes; w++)
{
*pSrc++=*inCol++;
}
}
return 1;
}
}