-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathImage.cpp
More file actions
138 lines (126 loc) · 3.7 KB
/
Image.cpp
File metadata and controls
138 lines (126 loc) · 3.7 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
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
#include "Image.h"
#include <cassert>
#include <cstring>
#include <squish.h>
#include <stb_image.h>
#include <unordered_map>
Image Image::convertToRGBA32() const
{
if (bpp == 32)
return Image(*this);
Image img;
img.width = width;
img.height = height;
img.bpp = 32;
img.pitch = width * 4;
img.pixels.resize(img.pitch * img.height);
if (bpp <= 8) {
assert(width == pitch);
uint8_t* oldpix = (uint8_t*)pixels.data();
uint32_t* newpix = (uint32_t*)img.pixels.data();
size_t oldsize = pitch * height;
for (size_t i = 0; i < oldsize; i++)
newpix[i] = palette[oldpix[i]];
}
else if (bpp == Format::ImageFormat_DXT1) {
squish::DecompressImage(img.pixels.data(), width, height, pixels.data(), squish::kDxt1);
}
else if (bpp == Format::ImageFormat_DXT2) {
squish::DecompressImage(img.pixels.data(), width, height, pixels.data(), squish::kDxt3);
}
else if (bpp == Format::ImageFormat_DXT4) {
squish::DecompressImage(img.pixels.data(), width, height, pixels.data(), squish::kDxt5);
}
else {
assert(false && "unsupported format for RGBA32 conversion");
}
return img;
}
std::optional<Image> Image::palettize() const
{
// convert image to 32bpp if it was in another format
std::optional<Image> cvtImage;
if (this->bpp != Format::ImageFormat_RGBA8888) {
cvtImage = this->convertToRGBA32();
}
const Image& image32 = cvtImage ? *cvtImage : *this;
int numUniqueColors = 0;
std::unordered_map<uint32_t, uint8_t> colorIndexMap;
const size_t numPixels = image32.width * image32.height;
DynArray<uint8_t> palPixels(numPixels);
for (size_t i = 0; i < numPixels; ++i) {
uint32_t color = *(uint32_t*)(image32.pixels.data() + 4 * i);
auto [iter, inserted] = colorIndexMap.try_emplace(color, (uint8_t)numUniqueColors);
if (inserted) {
numUniqueColors += 1;
if (numUniqueColors > 256)
return std::nullopt;
}
palPixels[i] = iter->second;
}
DynArray<uint32_t> palette((numUniqueColors <= 16) ? 16 : 256);
for (auto [color, index] : colorIndexMap) {
palette[index] = color;
}
std::fill(palette.begin() + numUniqueColors, palette.end(), 0);
Image image8;
image8.width = image32.width;
image8.height = image32.height;
image8.bpp = (numUniqueColors <= 16) ? 4 : 8;
image8.pitch = image8.width;
image8.pixels = std::move(palPixels);
image8.palette = std::move(palette);
return image8;
}
Image Image::loadFromFile(const char* filename)
{
Image img;
int sizx, sizy, origBpp;
void* pix = stbi_load(filename, &sizx, &sizy, &origBpp, 4);
assert(pix && "Failed to load image file\n");
img.width = sizx;
img.height = sizy;
img.bpp = 32;
img.pitch = img.width * 4;
img.pixels.resize(img.pitch * img.height);
memcpy(img.pixels.data(), pix, img.pixels.size());
stbi_image_free(pix);
return img;
}
Image Image::loadFromFile(const wchar_t* filename)
{
FILE* file;
_wfopen_s(&file, filename, L"rb");
Image img = loadFromFile(file);
fclose(file);
return img;
}
Image Image::loadFromFile(FILE* file)
{
Image img;
int sizx, sizy, origBpp;
void* pix = stbi_load_from_file(file, &sizx, &sizy, &origBpp, 4);
assert(pix && "Failed to load image file\n");
img.width = sizx;
img.height = sizy;
img.bpp = 32;
img.pitch = img.width * 4;
img.pixels.resize(img.pitch * img.height);
memcpy(img.pixels.data(), pix, img.pixels.size());
stbi_image_free(pix);
return img;
}
Image Image::loadFromMemory(void* ptr, size_t len) {
Image img;
int sizx, sizy, origBpp;
void* pix = stbi_load_from_memory((uint8_t*)ptr, (int)len, &sizx, &sizy, &origBpp, 4);
assert(pix && "Failed to load image from memory\n");
img.width = sizx;
img.height = sizy;
img.bpp = 32;
img.pitch = img.width * 4;
img.pixels.resize(img.pitch * img.height);
memcpy(img.pixels.data(), pix, img.pixels.size());
stbi_image_free(pix);
return img;
}