Debian Codegraph @

src/effect/PixelIterator.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "PixelIterator.h"
10
11 #include "SurfaceLock.h"
12 #include "PixelTool.h"
13
14 #include <assert.h>
15
16 //-----------------------------------------------------------------
17 /**
18 * Creates new pixel iterator and locks the surface.
19 */
20 PixelIterator::PixelIterator(SDL_Surface *surface)
21 {
22 m_surface = surface;
23 m_lock = new SurfaceLock(m_surface);
24 m_p = static_cast<Uint8*>(surface->pixels);
25 m_end = m_p + m_surface->h * m_surface->pitch;
26 m_bpp = surface->format->BytesPerPixel;
27 }
28 //-----------------------------------------------------------------
29 /**
30 * Unlocks surface.
31 */
32 PixelIterator::~PixelIterator()
33 {
34 delete m_lock;
35 }
36 //-----------------------------------------------------------------
37 void
38 PixelIterator::setPos(const V2 &pos)
39 {
40 int x = pos.getX();
41 int y = pos.getY();
42 assert((0 <= x && x < m_surface->w) && (0 <= y && y < m_surface->h));
43
44 m_p = static_cast<Uint8*>(m_surface->pixels) +
45 y * m_surface->pitch +
46 x * m_bpp;
47 }
48 //-----------------------------------------------------------------
49 bool
50 PixelIterator::isTransparent() const
51 {
52 return getPixel() == m_surface->format->colorkey;
53 }
54 //-----------------------------------------------------------------
55 SDL_Color
56 PixelIterator::getColor() const
57 {
58 SDL_Color color;
59 SDL_GetRGBA(getPixel(), m_surface->format,
60 &color.r, &color.g, &color.b, &color.unused);
61 return color;
62 }
63 //-----------------------------------------------------------------
64 Uint32
65 PixelIterator::getPixel() const
66 {
67 return PixelTool::unpackPixel(m_bpp, m_p);
68 }
69 //-----------------------------------------------------------------
70 void
71 PixelIterator::putColor(const SDL_Color &color)
72 {
73 Uint32 pixel = SDL_MapRGBA(m_surface->format,
74 color.r, color.g, color.b, color.unused);
75 putPixel(pixel);
76 }
77 //-----------------------------------------------------------------
78 void
79 PixelIterator::putPixel(Uint32 pixel)
80 {
81 PixelTool::packPixel(m_bpp, m_p, pixel);
82 }
83