-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_writer.c
228 lines (175 loc) · 6.08 KB
/
image_writer.c
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
#include "image_writer.h"
#include "image_helper.h"
#include "io_helper.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
//#define STB_IMAGE_WRITE_STATIC
#include "msf_gif.h"
#include "stb_image_write.h"
const char* getExtensionForOutputFormat(ImageOutputFormat format) {
switch (format) {
case PNG:
case APNG:
return ".png";
case JPG:
return ".jpg";
case TGA:
return ".tga";
case BMP:
return ".bmp";
case GIF:
return ".gif";
default:
return ".raw";
}
}
bool writeImageToFile(const char* fileOutName, ImageOutputFormat format, int width, int height, int channels, char* imageData, bool overwrite) {
if (!overwrite && doesFileExist(fileOutName)) {
return false;
}
switch (format) {
case GIF: {
MsfGifState gifState = {};
if (!msf_gif_begin(&gifState, width, height)) {
return false;
}
uint8_t* gifBuffer;
if (channels != 4) {
gifBuffer = (uint8_t*) malloc(width * height * 4);
if (gifBuffer == NULL) {
return false;
}
convertRGB888ToRGBA8888((const unsigned char*) imageData, (unsigned char*) gifBuffer, width * height);
} else {
gifBuffer = (uint8_t*) imageData;
}
bool returnVal = msf_gif_frame(&gifState, gifBuffer, 0, 16, width * 4);
if ((char*) gifBuffer != imageData) {
free(gifBuffer);
}
if (returnVal) {
MsfGifResult result = msf_gif_end(&gifState);
returnVal = result.data && writeBytesToFile(fileOutName, (char*) result.data, result.dataSize, true);
msf_gif_free(result);
}
return returnVal;
}
case PNG:
case APNG:
return stbi_write_png(fileOutName, width, height, channels, imageData, width * channels);
case JPG:
return stbi_write_jpg(fileOutName, width, height, channels, imageData, 90);
case TGA:
return stbi_write_tga(fileOutName, width, height, channels, imageData);
case BMP:
return stbi_write_bmp(fileOutName, width, height, channels, imageData);
default:
return writeBytesToFile(fileOutName, imageData, width * height * channels, true);
}
}
typedef struct {
bool error;
size_t buff_size;
char* buff;
} mem_image;
static void write_to_mem(void* context, void* data, int size) {
mem_image* image = (mem_image*) context;
if (image->error) {
return;
}
char* reallocBuf = (char*) realloc(image->buff, image->buff_size + size);
if (reallocBuf == NULL) {
image->error = true;
if (image->buff != NULL) {
free(image->buff);
image->buff = NULL;
}
return;
}
image->buff = reallocBuf;
memcpy(image->buff + image->buff_size, data, size);
image->buff_size += size;
}
char* writeImageToBytes(size_t* outsize, ImageOutputFormat format, int width, int height, int channels, char* imageData) {
switch (format) {
case GIF: {
char* gifOutput = NULL;
MsfGifState gifState = {};
if (!msf_gif_begin(&gifState, width, height)) {
return NULL;
}
uint8_t* gifBuffer;
if (channels != 4) {
gifBuffer = (uint8_t*) malloc(width * height * 4);
if (gifBuffer == NULL) {
return NULL;
}
convertRGB888ToRGBA8888((const unsigned char*) imageData, (unsigned char*) gifBuffer, width * height);
} else {
gifBuffer = (uint8_t*) imageData;
}
bool didEncode = msf_gif_frame(&gifState, gifBuffer, 0, 16, width * 4);
if ((char*) gifBuffer != imageData) {
free(gifBuffer);
}
if (didEncode) {
MsfGifResult result = msf_gif_end(&gifState);
if (result.data) {
gifOutput = (char*) malloc(result.dataSize);
if (gifOutput != NULL) {
*outsize = result.dataSize;
memcpy(gifOutput, result.data, result.dataSize);
}
}
msf_gif_free(result);
}
return gifOutput;
}
case PNG:
case APNG: {
mem_image image = {};
if (stbi_write_png_to_func(write_to_mem, &image, width, height, channels, imageData, width * channels) && !image.error) {
*outsize = image.buff_size;
return image.buff;
}
if (image.buff != NULL) {
free(image.buff);
}
return NULL;
}
case JPG: {
mem_image image = {};
if (stbi_write_jpg_to_func(write_to_mem, &image, width, height, channels, imageData, 90) && !image.error) {
*outsize = image.buff_size;
return image.buff;
}
if (image.buff != NULL) {
free(image.buff);
}
return NULL;
}
case TGA: {
mem_image image = {};
if (stbi_write_tga_to_func(write_to_mem, &image, width, height, channels, imageData) && !image.error) {
*outsize = image.buff_size;
return image.buff;
}
if (image.buff != NULL) {
free(image.buff);
}
return NULL;
}
case BMP: {
mem_image image = {};
if (stbi_write_bmp_to_func(write_to_mem, &image, width, height, channels, imageData) && !image.error) {
*outsize = image.buff_size;
return image.buff;
}
if (image.buff != NULL) {
free(image.buff);
}
return NULL;
}
default:
return NULL;
}
}