-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cpp
301 lines (269 loc) · 14.1 KB
/
image.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//Copyright © 2023 Charles Kerr. All rights reserved.
#include "image.hpp"
#include <iostream>
#include <algorithm>
#include <stdexcept>
using namespace std::string_literals;
namespace util {
//=================================================================================
namespace img {
//============================================================================
struct bmpsig{
constexpr static auto size = 14 ;
std::uint16_t signature ;
std::uint32_t filesize ;
std::uint32_t placeholder;
std::uint32_t offset ;
[[maybe_unused]] auto load(std::ifstream &input) ->bmpsig& {
input.read(reinterpret_cast<char*>(&signature),sizeof(signature));
input.read(reinterpret_cast<char*>(&filesize),sizeof(filesize));
input.read(reinterpret_cast<char*>(&placeholder),sizeof(placeholder));
input.read(reinterpret_cast<char*>(&offset),sizeof(offset));
return *this;
}
bmpsig():filesize(14+40),placeholder(0),offset(14+40),signature(0x4d42){
}
bmpsig(std::ifstream &input){
load(input) ;
}
auto save(std::ofstream &output) ->void {
output.write(reinterpret_cast<char*>(&signature),sizeof(signature));
output.write(reinterpret_cast<char*>(&filesize),sizeof(filesize));
output.write(reinterpret_cast<char*>(&placeholder),sizeof(placeholder));
output.write(reinterpret_cast<char*>(&offset),sizeof(offset));
}
};
//============================================================================
struct dibheader{
constexpr static auto size = 40 ;
std::uint32_t header_size ;
std::int32_t width ;
std::int32_t height ;
std::uint16_t num_planes ;
std::uint16_t bits_pixel ;
std::uint32_t compression ;
std::uint32_t image_size ;
std::uint32_t horiz_resolution ;
std::uint32_t vert_resolution ;
std::uint32_t color_count ;
std::uint32_t important_count ;
[[maybe_unused]] auto load(std::ifstream &input) ->dibheader& {
input.read(reinterpret_cast<char*>(&header_size),sizeof(header_size));
if (header_size < dibheader::size){
throw std::runtime_error("Unsupported dib header format");
}
input.read(reinterpret_cast<char*>(&width),sizeof(width));
input.read(reinterpret_cast<char*>(&height),sizeof(height));
input.read(reinterpret_cast<char*>(&num_planes),sizeof(num_planes));
input.read(reinterpret_cast<char*>(&bits_pixel),sizeof(bits_pixel));
input.read(reinterpret_cast<char*>(&compression),sizeof(compression));
input.read(reinterpret_cast<char*>(&image_size),sizeof(image_size));
input.read(reinterpret_cast<char*>(&horiz_resolution),sizeof(horiz_resolution));
input.read(reinterpret_cast<char*>(&vert_resolution),sizeof(vert_resolution));
input.read(reinterpret_cast<char*>(&color_count),sizeof(color_count));
input.read(reinterpret_cast<char*>(&important_count),sizeof(important_count));
return *this;
}
auto save(std::ofstream &output) ->void {
output.write(reinterpret_cast<char*>(&header_size),sizeof(header_size));
output.write(reinterpret_cast<char*>(&width),sizeof(width));
output.write(reinterpret_cast<char*>(&height),sizeof(height));
output.write(reinterpret_cast<char*>(&num_planes),sizeof(num_planes));
output.write(reinterpret_cast<char*>(&bits_pixel),sizeof(bits_pixel));
output.write(reinterpret_cast<char*>(&compression),sizeof(compression));
output.write(reinterpret_cast<char*>(&image_size),sizeof(image_size));
output.write(reinterpret_cast<char*>(&horiz_resolution),sizeof(horiz_resolution));
output.write(reinterpret_cast<char*>(&vert_resolution),sizeof(vert_resolution));
output.write(reinterpret_cast<char*>(&color_count),sizeof(color_count));
output.write(reinterpret_cast<char*>(&important_count),sizeof(important_count));
}
//Print resolution of the image,
//72 DPI × 39.3701 inches per metre yields 2834.6472
dibheader():header_size(size),width(0),height(0),num_planes(1),bits_pixel(32),compression(0),image_size(0),horiz_resolution(0xb13),vert_resolution(0xb13),color_count(0),important_count(0) {
}
dibheader(std::ifstream &input){
load(input) ;
}
};
//=================================================================================
image_t::image_t(std::size_t height,std::size_t width,const color_t &fillcolor){
auto data = std::vector(height,std::vector(width,fillcolor));
}
//=================================================================================
auto image_t::empty() const ->bool{
return data.empty();
}
//=================================================================================
auto image_t::size() const ->std::pair<std::size_t,std::size_t> {
auto height = static_cast<std::size_t>(data.size());
auto width = std::size_t(0);
if (height>0){
width = static_cast<std::size_t>(data.at(0).size());
}
return std::make_pair(height,width);
}
//=================================================================================
auto image_t::size(std::size_t height, std::size_t width,const color_t &fillcolor) ->image_t&{
data.resize(height,std::vector<color_t>(width,fillcolor));
std::for_each(data.begin(),data.end(),[width,&fillcolor](std::vector<color_t> &row){
row.resize(width,fillcolor);
});
return *this ;
}
//=================================================================================
auto image_t::color(std::size_t row,std::size_t col) const ->const color_t& {
return data.at(row).at(col);
}
//=================================================================================
auto image_t::color(std::size_t row,std::size_t col) -> color_t& {
return data.at(row).at(col);
}
//=================================================================================
auto image_t::invert() ->image_t& {
std::reverse(data.begin(), data.end());
return *this;
}
//=================================================================================
auto image_t::flip() ->image_t& {
std::for_each(data.begin(),data.end(),[](std::vector<color_t> &row){
std::reverse(row.begin(), row.end());
});
return *this;
}
//=================================================================================
auto image_t::copy(const image_t &image, std::size_t offrow,std::size_t offcol,std::size_t height,std::size_t width, std::size_t destrow,std::size_t destcol,bool transparent) ->image_t& {
auto [myheight,mywidth] = this->size();
auto [sheight, swidth] = image.size();
for (std::size_t row = 0;row < height;row++){
if ((row+destrow) < myheight){
if (row+offrow < sheight){
for (std::size_t col =0;col < width;col++){
if (col+destcol < width){
if (col + offcol < swidth){
auto color = image.color(offrow=row, offcol+col);
if (!color.transparent() || transparent ){
this->color(destrow+row, destcol+col) = color;
}
}
}
}
}
}
}
return *this;
}
//=================================================================================
auto image_t::palette() const ->std::set<color_t> {
auto rvalue = std::set<color_t>();
for (const auto &row:data){
for (const auto &color:row){
rvalue.insert(color);
}
}
return rvalue ;
}
//=================================================================================
auto image_t::save(std::ofstream &output) ->void {
auto header = dibheader() ;
auto [height,width] = this->size();
header.width = static_cast<std::int32_t>(width) ;
header.height = static_cast<std::int32_t>(height) ;
header.bits_pixel = 32 ;
header.image_size = static_cast<std::uint32_t>(height*width*4);
auto sig = bmpsig() ;
sig.filesize = static_cast<std::uint32_t>(bmpsig::size + dibheader::size + header.image_size) ;
sig.save(output);
header.save(output);
for (auto iter = data.rbegin();iter != data.rend();iter++){
for (const auto &color:*iter){
auto value = color.value<std::uint32_t>();
output.write(reinterpret_cast<const char*>(&value),sizeof(value));
}
}
}
//=================================================================================
auto image_t::save(const std::filesystem::path &outpath) ->bool{
auto output = std::ofstream(outpath.string(),std::ios::binary);
auto rvalue = false ;
if (output.is_open()){
rvalue = true ;
save(output);
}
return rvalue ;
}
//=================================================================================
auto image_t::load(const std::filesystem::path &bmppath) ->image_t& {
auto input = std::ifstream(bmppath.string(),std::ios::binary);
if (!input.is_open()){
throw std::runtime_error("Unable to open: "s + bmppath.string());
}
return load(input);
}
//=================================================================================
auto image_t::load(std::ifstream &input) ->image_t& {
auto sig = bmpsig(input);
auto current = static_cast<std::uint32_t>(input.tellg());
auto header = dibheader(input);
if (header.compression != 0){
throw std::runtime_error("Unsupported bmp file compression method");
}
if (header.bits_pixel<8){
throw std::runtime_error("Unsupported bmp pixel size");
}
// Does this have a palette?
auto headerend = current + header.header_size;
auto palettesize = header.color_count ;
if (palettesize == 0){
palettesize = sig.offset - headerend ;
}
input.seekg(headerend,std::ios::beg);
auto count = palettesize/4 ;
auto palette_data = std::array<std::uint8_t,4>() ;
auto palette = std::vector<color_t>() ;
for (std::uint32_t j= 0 ; j<count;j++){
input.read(reinterpret_cast<char*>(palette_data.data()),palette_data.size());
palette.push_back(color_t(palette_data[2],palette_data[1],palette_data[0],palette_data[3]));
}
input.seekg(sig.offset,std::ios::beg);
data = std::vector<std::vector<color_t>>(header.height,std::vector<color_t>(header.width,color_t()));
auto temp =(header.width * (header.bits_pixel/8)) ;
auto pad = (temp%4==0?0:4-temp%4);
auto padvalue = std::uint32_t(0) ;
auto colvalue = std::uint32_t(0);
auto color = color_t() ;
for (std::int32_t row = 0 ; row < header.height;row++){
for (std::int32_t col = 0 ; col < header.width;col++){
switch (header.bits_pixel){
case 8: {
input.read(reinterpret_cast<char*>(&colvalue),1);
color = palette.at(colvalue);
colvalue = 0 ;
break;
}
case 16: {
input.read(reinterpret_cast<char*>(&colvalue),1);
colvalue |= 0x8000;
color = static_cast<std::uint16_t>(colvalue);
colvalue = 0 ;
break;
}
case 24: {
input.read(reinterpret_cast<char*>(palette_data.data()),3); // we are reusing this varable
color = color_t(palette_data.at(0),palette_data.at(1),palette_data.at(2),255);
break;
}
case 32: {
input.read(reinterpret_cast<char*>(&colvalue),1);
color = colvalue;
colvalue = 0 ;
break;
}
}
data.at(data.size()-row -1).at(col) = color ;
}
input.read(reinterpret_cast<char*>(&padvalue),pad);
}
return *this;
}
}
}