-
Notifications
You must be signed in to change notification settings - Fork 9
/
image_iterator.cpp
56 lines (48 loc) · 1.1 KB
/
image_iterator.cpp
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
// Aseprite TGA Library
// Copyright (C) 2020-2022 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "tga.h"
namespace tga {
namespace details {
ImageIterator::ImageIterator()
: m_image(nullptr)
{
}
ImageIterator::ImageIterator(const Header& header, Image& image)
: m_image(&image)
, m_x(header.leftToRight() ? 0: header.width-1)
, m_y(header.topToBottom() ? 0: header.height-1)
, m_w(header.width)
, m_h(header.height)
, m_dx(header.leftToRight() ? +1: -1)
, m_dy(header.topToBottom() ? +1: -1)
{
calcPtr();
}
bool ImageIterator::advance()
{
m_x += m_dx;
m_ptr += int(m_dx*m_image->bytesPerPixel);
if ((m_dx < 0 && m_x < 0) ||
(m_dx > 0 && m_x == m_w)) {
m_x = (m_dx > 0 ? 0: m_w-1);
m_y += m_dy;
if ((m_dy < 0 && m_y < 0) ||
(m_dy > 0 && m_y == m_h)) {
return true;
}
calcPtr();
}
return false;
}
void ImageIterator::calcPtr()
{
m_ptr =
m_image->pixels
+ m_image->rowstride*m_y
+ m_image->bytesPerPixel*m_x;
}
} // namespace details
} // namespace tga