-
Notifications
You must be signed in to change notification settings - Fork 1
/
image-character.cpp
155 lines (126 loc) · 4.13 KB
/
image-character.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
/**
* This file is part of HD OSD Font Tool.
* Written by Darren Lines (Mr. D RC)
*
* HD OSD Font Tool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. Please keep this header in the files.
*
* HD OSD Font Tool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HD OSD Font Tool. If not, see <http://www.gnu.org/licenses/>.
*/
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "image-character.h"
#include "stb_image.h"
#include "stb_image_write.h"
#include "stb_image_resize.h"
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
ImageCharacter::ImageCharacter() {
// Need a blank constructor, failing to compile without it.
}
ImageCharacter::~ImageCharacter() {
stbi_image_free(data);
}
ImageCharacter::ImageCharacter(const string imageFile) {
if (!readImage(imageFile)) {
cout << "Failed to load image " << imageFile << endl;
}
}
ImageCharacter::ImageCharacter(int newW, int newH) {
setWH(newW, newH);
}
ImageCharacter::ImageCharacter(const ImageCharacter &fromCharacter) {
copyImage(fromCharacter);
}
void ImageCharacter::copyImage(const ImageCharacter &fromCharacter) {
w = fromCharacter.w;
h = fromCharacter.h;
channels = fromCharacter.channels;
size = w * h * channels;
data = new uint8_t[size](0);
memcpy(data, fromCharacter.data, size);
}
bool ImageCharacter::readImage(const string imageFile) {
char filename[imageFile.length() + 1];
strcpy(filename, imageFile.c_str());
data = stbi_load(filename, &w, &h, &channels, 4);
if (data != NULL) {
size = w * h * channels;
} else {
size = 0;
}
return data != NULL;
}
void ImageCharacter::fillImage(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
for (int i = 0; i < (w * h * channels); i++) {
data[i++] = r;
data[i++] = g;
data[i++] = b;
data[i] = a;
}
}
void ImageCharacter::resizeImage(const string imageFile, int newW, int newH) {
if (!readImage(imageFile)) {
cout << "Failed to load image " << imageFile << endl;
}
uint8_t* resizedData = new uint8_t[(newW * newH * channels)](0);
stbir_resize_uint8( data, w, h, 0,
resizedData, newW, newH, 0, channels);
w = newW;
h = newH;
size = w * h * channels;
data = resizedData;
}
void ImageCharacter::setWH(int newW, int newH) {
w = newW;
h = newH;
size = w * h * channels;
data = new uint8_t[size](0);
// Convert to a transparent, white image
for (int i = 0; i < (w * h * channels); i++) {
if (i % 4 == 3) {
data[i] = 0;
} else {
data[i] = 255;
}
}
}
void ImageCharacter::convertRGBAtoRGB() {
if (channels == 4) {
uint8_t* noAlphaData = new uint8_t[(w * h * 3)](0);
int toPointer = 0;
for (int i = 0; i < (w * h * channels); i++) {
if (i % 4 != 3) {
memcpy(&noAlphaData[toPointer++], &data[i], 1);
}
}
channels = 3;
size = w * h * channels;
data = noAlphaData;
}
}
bool ImageCharacter::writeAvatarImage(const string fileName) {
stbi_write_png_compression_level = 10;
char filename[fileName.length() + 1];
strcpy(filename, fileName.c_str());
int success = stbi_write_png(filename, w, h, channels, data, (w * channels));
return (success != 0);
}
bool ImageCharacter::writeHDZeroImage(const string fileName) {
char filename[fileName.length() + 1];
strcpy(filename, fileName.c_str());
convertRGBAtoRGB();
int success = stbi_write_bmp(filename, w, h, channels, data);
return (success != 0);
}