blob: 4567ce635c0518e1ee08d44e631d7e9c025f0ac6 (
plain)
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
|
/**
* @module cpixelformat_BGR24
* @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
* @brief Implementation of CPixelFormat handling 24bit color Windows Bitmaps.
* @date 18.04.2009
*/
#include <boost/numeric/conversion/cast.hpp>
#include "cpixelformat_bgr24.h"
using namespace std;
/* TODO */
void CPixelFormat_BGR24::getPixel(uint32_t *pixel, uint32_t x, uint32_t y)
{
/*
* pixel[0] ... red
* pixel[1] ... green
* pixel[2] ... blue
*/
if (m_bitmap->getPixelData() == NULL)
throw PixelFormatError("No pixelbuffer allocated.");
/* calc rowsize - boundary is 32 */
uint32_t rowsize = 4 * static_cast<uint32_t>(
((getBitCount() * m_bitmap->getWidth()) + 31) / 32
);
/* if the y-coordinates are mirrored */
if (m_bitmap->isMirrored())
y = m_bitmap->getHeight() - y - 1;
uint32_t offset = y * rowsize + x * (4 * getBitCount() / 32);
/* boundary check */
if (offset + getBitCount()/8 > m_bitmap->getPixelDataSize())
throw PixelFormatError("Pixel position is out of range.");
/* get pixel */
try
{
pixel[0] = boost::numeric_cast<uint32_t>(*(m_bitmap->getPixelData() + offset + 2));
pixel[1] = boost::numeric_cast<uint32_t>(*(m_bitmap->getPixelData() + offset + 1));
pixel[2] = boost::numeric_cast<uint32_t>(*(m_bitmap->getPixelData() + offset));
}
catch(boost::numeric::bad_numeric_cast& ex)
{
throw PixelFormatError("Unable to convert pixelcolor to correct size: " + string(ex.what()));
}
}
void CPixelFormat_BGR24::setPixel(const uint32_t *pixel, uint32_t x, uint32_t y)
{
/*
* pixel[0] ... red
* pixel[1] ... green
* pixel[2] ... blue
*/
if (m_bitmap->getPixelData() == NULL)
throw PixelFormatError("No pixelbuffer allocated.");
/* calc rowsize - boundary is 32 */
uint32_t rowsize = 4 * static_cast<uint32_t>(
((getBitCount() * m_bitmap->getWidth()) + 31) / 32
);
/* if the y-coordinates are mirrored */
if (m_bitmap->isMirrored())
y = m_bitmap->getHeight() - y - 1;
uint32_t offset = y * rowsize + x * (4 * getBitCount() / 32);
/* boundary check */
if (offset + getBitCount()/8 > m_bitmap->getPixelDataSize())
throw PixelFormatError("Pixel position is out of range.");
/* convert color values to correct types */
uint8_t data[3];
try
{
data[0] = boost::numeric_cast<uint8_t>(pixel[2]);
data[1] = boost::numeric_cast<uint8_t>(pixel[1]);
data[2] = boost::numeric_cast<uint8_t>(pixel[0]);
}
catch(boost::numeric::bad_numeric_cast& ex)
{
throw PixelFormatError("Unable to convert pixelcolor to correct size: " + string(ex.what()));
}
copy(data, data + 3, m_bitmap->getPixelData() + offset);
}
/* vim: set et sw=2 ts=2: */
|