-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.cpp
151 lines (120 loc) · 4.07 KB
/
texture.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "texture.h"
#include "parameters.h"
#include <QFileInfo>
#include <QImageReader>
#include <iostream>
#ifdef HAS_JPEG
#include <jerror.h>
#include <jpeglib.h>
#endif
#ifdef HAS_PNG
#include <png.h>
#endif
namespace Mpcv {
#ifdef HAS_JPEG
JpegTexture::JpegTexture(const std::string& filename) {
FILE* file = fopen(filename.c_str(), "rb");
jpeg_error_mgr err;
jpeg_decompress_struct info;
info.err = jpeg_std_error(&err);
jpeg_create_decompress(&info);
if (!file) {
throw std::runtime_error("Error reading JPEG image '" + filename + "'");
}
jpeg_stdio_src(&info, file);
jpeg_read_header(&info, TRUE);
jpeg_start_decompress(&info);
width_ = info.output_width;
height_ = info.output_height;
channels_ = info.num_components;
std::cout << "Loading JPEG image of size " << width_ << "x" << height_ << std::endl;
data_ = (uint8_t*)malloc(width_ * height_ * channels_);
uint8_t* rowptr[1];
while (info.output_scanline < info.output_height) {
rowptr[0] = (uint8_t*)data_ + channels_ * info.output_width * info.output_scanline;
jpeg_read_scanlines(&info, rowptr, 1);
}
jpeg_finish_decompress(&info);
jpeg_destroy_decompress(&info);
fclose(file);
std::cout << "row = " << int(data_[0]) << "," << int(data_[1]) << "," << int(data_[2]) << ","
<< int(data_[3]) << "," << int(data_[4]) << "," << int(data_[5]) << "," << int(data_[6])
<< std::endl;
}
JpegTexture::~JpegTexture() {
free(data_);
}
Pvl::Vec2i JpegTexture::size() const {
return Pvl::Vec2i(width_, height_);
}
ImageFormat JpegTexture::format() const {
return channels_ == 4 ? ImageFormat::RGBA : ImageFormat::RGB;
}
uint8_t* JpegTexture::data() {
return data_;
}
#endif
#ifdef HAS_PNG
PngTexture::PngTexture(const std::string& filename) {
png_image image;
memset(&image, 0, sizeof(image));
image.version = PNG_IMAGE_VERSION;
if (!png_image_begin_read_from_file(&image, filename.c_str())) {
throw std::runtime_error("Failed to read PNG file '" + filename + "'");
}
image.format = PNG_FORMAT_RGB;
data_ = (uint8_t*)malloc(PNG_IMAGE_SIZE(image));
if (!png_image_finish_read(&image, nullptr, data_, 0, nullptr)) {
throw std::runtime_error("Failed to read PNG file '" + filename + "'");
}
width_ = image.width;
height_ = image.height;
channels_ = PNG_IMAGE_SAMPLE_CHANNELS(image.format);
}
PngTexture::~PngTexture() {
free(data_);
}
Pvl::Vec2i PngTexture::size() const {
return Pvl::Vec2i(width_, height_);
}
ImageFormat PngTexture::format() const {
return channels_ == 4 ? ImageFormat::RGBA : ImageFormat::RGB;
}
uint8_t* PngTexture::data() {
return data_;
}
#endif
std::unique_ptr<ITexture> makeTexture(const QString& filename) {
if (!QFileInfo(filename).exists()) {
throw std::runtime_error("Texture image '" + filename.toStdString() + "' does not exist");
}
QString ext = QFileInfo(filename).suffix();
QImageReader reader(filename);
QSize size = reader.size();
float scale = Mpcv::Parameters::global().textureScale;
if (scale < 1.f) {
size *= scale;
reader.setScaledSize(size);
}
const int maxQtSize = (1 << 15) - 1;
if (size.width() <= maxQtSize && size.height() <= maxQtSize) {
std::cout << "Loading image '" + filename.toStdString() + "' using Qt reader" << std::endl;
return std::make_unique<QtTexture>(reader.read());
} else
#ifdef HAS_JPEG
if (ext == "jpg" || ext == "jpeg") {
std::cout << "Loading image '" + filename.toStdString() + "' using libjpeg reader" << std::endl;
return std::make_unique<JpegTexture>(filename.toStdString());
} else
#endif
#ifdef HAS_PNG
if (ext == "png") {
std::cout << "Loading image '" + filename.toStdString() + "' using libpng reader" << std::endl;
return std::make_unique<PngTexture>(filename.toStdString());
} else
#endif
{
throw std::runtime_error("Cannot read texture '" + filename.toStdString() + "', image too large");
}
}
} // namespace Mpcv