forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
png_decoder.h
76 lines (70 loc) · 1.5 KB
/
png_decoder.h
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
#ifndef PNG_DECODER_H_INCLUDED
#define PNG_DECODER_H_INCLUDED
#include <string>
#include <cstdint>
#include <stdexcept>
#include "stream.h"
/**************
*Description:
*Input:
*Output:
**************/
using namespace std;
class PngLoadError final : public IOException
{
public:
explicit PngLoadError(const string & arg)
: IOException(arg)
{
}
};
/** read and decode png files<br/>
bytes in RGBA format
*/
class PngDecoder final
{
private:
unsigned w, h;
uint8_t * data;
PngDecoder(const PngDecoder &) = delete;
const PngDecoder &operator =(const PngDecoder &) = delete;
public:
explicit PngDecoder(Reader & reader);
PngDecoder(PngDecoder && rt)
{
w = rt.w;
h = rt.h;
data = rt.data;
rt.data = nullptr;
}
~PngDecoder()
{
delete []data;
}
uint8_t operator()(int x, int y, int byteNum) const
{
if(x < 0 || (unsigned)x >= w || y < 0 || (unsigned)y >= h || byteNum < 0 || byteNum >= 4)
throw range_error("index out of range in PngDecoder::operator()(int x, int y, int byteNum) const");
size_t index = y;
index *= w;
index += x;
index *= 4;
index += byteNum;
return data[index];
}
int width() const
{
return w;
}
int height() const
{
return h;
}
uint8_t * removeData()
{
uint8_t * retval = data;
data = nullptr;
return retval;
}
};
#endif // PNG_DECODER_H_INCLUDED