1// Aseprite TGA Library
2// Copyright (C) 2020-2022 Igara Studio S.A.
3//
4// This file is released under the terms of the MIT license.
5// Read LICENSE.txt for more information.
6
7#include "tga.h"
8
9namespace tga {
10namespace details {
11
12ImageIterator::ImageIterator()
13 : m_image(nullptr)
14{
15}
16
17ImageIterator::ImageIterator(const Header& header, Image& image)
18 : m_image(&image)
19 , m_x(header.leftToRight() ? 0: header.width-1)
20 , m_y(header.topToBottom() ? 0: header.height-1)
21 , m_w(header.width)
22 , m_h(header.height)
23 , m_dx(header.leftToRight() ? +1: -1)
24 , m_dy(header.topToBottom() ? +1: -1)
25{
26 calcPtr();
27}
28
29bool ImageIterator::advance()
30{
31 m_x += m_dx;
32 m_ptr += int(m_dx*m_image->bytesPerPixel);
33
34 if ((m_dx < 0 && m_x < 0) ||
35 (m_dx > 0 && m_x == m_w)) {
36 m_x = (m_dx > 0 ? 0: m_w-1);
37 m_y += m_dy;
38 if ((m_dy < 0 && m_y < 0) ||
39 (m_dy > 0 && m_y == m_h)) {
40 return true;
41 }
42 calcPtr();
43 }
44 return false;
45}
46
47void ImageIterator::calcPtr()
48{
49 m_ptr =
50 m_image->pixels
51 + m_image->rowstride*m_y
52 + m_image->bytesPerPixel*m_x;
53}
54
55} // namespace details
56} // namespace tga
57