-
Notifications
You must be signed in to change notification settings - Fork 0
/
images2onefile.h
101 lines (81 loc) · 3.39 KB
/
images2onefile.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
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
#pragma once
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stb_image.h>
#include <stb_image_write.h>
#include <zlib.h>
struct ImageData {
std::string name;
int width, height, channels;
std::vector<unsigned char> data;
};
static bool load_image(__in const std::string& path, __in const char* name, __out ImageData& img) {
img.name = name;
int width, height, channels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
if (data != nullptr) {
img.width = width;
img.height = height;
img.channels = channels;
img.data = std::vector<unsigned char>(data, data + width * height * channels);
stbi_image_free(data);
return true;
}
return false;
}
static bool compress_images(__in const std::vector<ImageData>& images, __in const std::string& outputPath) {
std::ofstream file(outputPath, std::ios::binary);
if (!file.is_open()) {
std::cout << "file not exist\n";
return false;
}
for (const auto& img : images) {
size_t name_length = img.name.size();
file.write(reinterpret_cast<const char*>(&name_length), sizeof(name_length));
file.write(img.name.c_str(), name_length);
file.write(reinterpret_cast<const char*>(&img.width), sizeof(img.width));
file.write(reinterpret_cast<const char*>(&img.height), sizeof(img.height));
file.write(reinterpret_cast<const char*>(&img.channels), sizeof(img.channels));
uLongf compressed_size = compressBound(img.data.size());
std::vector<unsigned char> compressed_data(compressed_size);
if (compress2(compressed_data.data(), &compressed_size, img.data.data(), img.data.size(), Z_BEST_COMPRESSION) != Z_OK) {
return false;
}
file.write(reinterpret_cast<const char*>(&compressed_size), sizeof(compressed_size));
file.write(reinterpret_cast<const char*>(compressed_data.data()), compressed_size);
}
file.close();
return true;
}
static bool decompress_images(__in const std::string& inputPath, __out std::vector<ImageData>& images) {
std::ifstream file(inputPath, std::ios::binary);
if (!file.is_open()) {
return false;
}
while (!file.eof()) {
ImageData img;
size_t name_length;
file.read(reinterpret_cast<char*>(&name_length), sizeof(name_length));
img.name.resize(name_length);
file.read(&img.name[0], name_length);
file.read(reinterpret_cast<char*>(&img.width), sizeof(img.width));
file.read(reinterpret_cast<char*>(&img.height), sizeof(img.height));
file.read(reinterpret_cast<char*>(&img.channels), sizeof(img.channels));
uLongf compressed_size;
file.read(reinterpret_cast<char*>(&compressed_size), sizeof(compressed_size));
std::vector<unsigned char> compressed_data(compressed_size);
file.read(reinterpret_cast<char*>(compressed_data.data()), compressed_size);
uLongf decompressed_size = img.width * img.height * img.channels;
img.data.resize(decompressed_size);
uncompress(img.data.data(), &decompressed_size, compressed_data.data(), compressed_size);
images.push_back(img);
}
images.erase(images.end() - 1);
return true;
}