forked from ssloy/tinyrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgaimage.h
51 lines (45 loc) · 1.39 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
#pragma once
#include <cstdint>
#include <fstream>
#include <vector>
#pragma pack(push,1)
struct TGAHeader {
std::uint8_t idlength = 0;
std::uint8_t colormaptype = 0;
std::uint8_t datatypecode = 0;
std::uint16_t colormaporigin = 0;
std::uint16_t colormaplength = 0;
std::uint8_t colormapdepth = 0;
std::uint16_t x_origin = 0;
std::uint16_t y_origin = 0;
std::uint16_t width = 0;
std::uint16_t height = 0;
std::uint8_t bitsperpixel = 0;
std::uint8_t imagedescriptor = 0;
};
#pragma pack(pop)
struct TGAColor {
std::uint8_t bgra[4] = {0,0,0,0};
std::uint8_t bytespp = 4;
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;
std::uint8_t bpp = 0;
std::vector<std::uint8_t> data = {};
};