forked from ssloy/tinyrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgaimage.h
57 lines (50 loc) · 1.65 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
#pragma once
#include <cstdint>
#include <fstream>
#include <vector>
#pragma pack(push,1)
struct TGAHeader {
std::uint8_t idlength{};
std::uint8_t colormaptype{};
std::uint8_t datatypecode{};
std::uint16_t colormaporigin{};
std::uint16_t colormaplength{};
std::uint8_t colormapdepth{};
std::uint16_t x_origin{};
std::uint16_t y_origin{};
std::uint16_t width{};
std::uint16_t height{};
std::uint8_t bitsperpixel{};
std::uint8_t imagedescriptor{};
};
#pragma pack(pop)
struct TGAColor {
std::uint8_t bgra[4] = {0,0,0,0};
std::uint8_t bytespp = {0};
TGAColor() = default;
TGAColor(const std::uint8_t R, const std::uint8_t G, const std::uint8_t B, const std::uint8_t A=255) : bgra{B,G,R,A}, bytespp(4) { }
TGAColor(const std::uint8_t *p, const std::uint8_t bpp) : bytespp(bpp) {
for (int i=bpp; i--; bgra[i] = p[i]);
}
std::uint8_t& operator[](const int i) { return bgra[i]; }
};
struct TGAImage {
enum Format { GRAYSCALE=1, RGB=3, RGBA=4 };
TGAImage() = default;
TGAImage(const int w, const int h, const int bpp);
bool read_tga_file(const std::string filename);
bool write_tga_file(const std::string filename, const bool vflip=true, const bool rle=true) const;
void flip_horizontally();
void flip_vertically();
TGAColor get(const int x, const int y) const;
void set(const int x, const int y, const TGAColor &c);
int width() const;
int height() const;
private:
bool load_rle_data(std::ifstream &in);
bool unload_rle_data(std::ofstream &out) const;
int w = 0;
int h = 0;
int bpp = 0;
std::vector<std::uint8_t> data = {};
};