-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgaimage.h
102 lines (85 loc) · 2.21 KB
/
tgaimage.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef __IMAGE_H__
#define __IMAGE_H__
#include <fstream>
#include <stdexcept>
#pragma pack(push,1)
struct TGA_Header {
char idlength;
char colormaptype;
char datatypecode;
short colormaporigin;
short colormaplength;
char colormapdepth;
short x_origin;
short y_origin;
short width;
short height;
char bitsperpixel;
char imagedescriptor;
};
#pragma pack(pop)
struct TGAColor {
union {
struct {
uint8_t b, g, r, a;
} parts;
uint8_t raw[4];
uint32_t val;
};
int bytespp;
TGAColor() : val(0), bytespp(1) {
}
TGAColor(unsigned char R, unsigned char G, unsigned char B, unsigned char A = 255) : parts{B, G, R, A}, bytespp(4) {
}
TGAColor(int v, int bpp) : val(v), bytespp(bpp) {
}
TGAColor(const TGAColor &c) : val(c.val), bytespp(c.bytespp) {
}
TGAColor(const unsigned char *p, int bpp) : val(0), bytespp(bpp) {
for (int i=0; i<bpp; i++) {
raw[i] = p[i];
}
if (bpp < 1 || bpp > 4)
throw std::logic_error("bpp can be only 1, 2, 3 or 4");
}
TGAColor & operator =(const TGAColor &c) {
if (this != &c) {
bytespp = c.bytespp;
val = c.val;
}
return *this;
}
};
class TGAImage {
protected:
int width;
int height;
int bytespp;
uint8_t* data;
bool load_rle_data(std::ifstream &in);
bool unload_rle_data(std::ofstream &out);
public:
enum Format {
GRAYSCALE=1, RGB=3, RGBA=4
};
TGAImage(int w, int h, int bpp);
TGAImage(const TGAImage &img);
bool read_tga_file(const char *filename);
bool write_tga_file(const char *filename, bool rle=true);
void flip_horizontally();
void flip_vertically();
bool scale(int w, int h);
TGAColor safeGet(int x, int y);
bool safeSet(int x, int y, const TGAColor& c);
void set1(int x, int y, const TGAColor& c);
void set2(int x, int y, const TGAColor& c);
void set4(int x, int y, const TGAColor& c);
~TGAImage();
TGAImage & operator =(const TGAImage &img);
int get_width();
int get_height();
int get_bytespp();
uint8_t *buffer();
void clear();
};
#endif //__IMAGE_H__