-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpng_reader.h
More file actions
61 lines (49 loc) · 1.92 KB
/
png_reader.h
File metadata and controls
61 lines (49 loc) · 1.92 KB
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
#pragma once
#include <fstream>
#include <iostream>
#include <zlib.h>
class PNGReader {
public:
std::ifstream input_file;
uint64_t signature = 0;
struct IHDRChunk {
uint32_t width; // Image width (big-endian)
uint32_t height; // Image height (big-endian)
uint8_t bit_depth; // Bit depth
uint8_t color_type; // Color type
uint8_t compression_method; // Compression method
uint8_t filter_method; // Filter method
uint8_t interlace_method; // Interlace method
} ihdr_chunk = {};
uint8_t* _color_buffer = nullptr;
explicit PNGReader(const std::string& filename) : input_file(filename, std::ios::binary) {
if (!input_file) {
throw std::runtime_error("Error opening file");
}
verify_signature();
read_ihdr_chunk();
read_idat_chunk();
input_file.close();
}
~PNGReader() {
delete[] _color_buffer;
}
void verify_signature();
uint32_t get_width() const { return ihdr_chunk.width; }
uint32_t get_height() const { return ihdr_chunk.width; }
uint8_t get_bit_depth() const { return ihdr_chunk.bit_depth; }
uint8_t get_color_type() const { return ihdr_chunk.color_type; }
uint8_t get_compression_method() const { return ihdr_chunk.compression_method; }
uint8_t get_filter_method() const { return ihdr_chunk.filter_method; }
uint8_t get_interlace_method() const { return ihdr_chunk.interlace_method; }
uint8_t* get_color_buffer() const { return _color_buffer; }
uint8_t get_bytes_per_pixel() const;
uint64_t get_scanline_length() const;
uint64_t get_reconstructed_scanline_length() const;
std::vector<int> get_pixel_rgba(int x, int y) const;
private:
static uint8_t paeth_predictor(uint8_t, uint8_t, uint8_t);
uint8_t* reconstruct_scanline(const uint8_t*) const;
void read_ihdr_chunk();
void read_idat_chunk();
};