-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.h
More file actions
317 lines (276 loc) · 8.84 KB
/
Image.h
File metadata and controls
317 lines (276 loc) · 8.84 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#pragma once
#include <stdint.h>
#include <stdlib.h>
struct Image
{
uint8_t* pixels;
uint32_t width;
uint32_t height;
};
struct ImageMetadata
{
uint32_t width;
uint32_t height;
};
/* Returns an Image with sRGBA 8-bits-per-color pixels (4 bytes per pixel).
* When bytes is an invalid or unrecognized image, or when byteCount is zero,
* pixels, width, and height are set to zero. */
Image decodeImage(const uint8_t* bytes, size_t byteCount);
/* Decodes only metadata without decoding pixels.
* When bytes is an invalid or unrecognized image, or when byteCount is zero,
* width and height are set to zero. */
ImageMetadata decodeImageMetadata(const uint8_t* bytes, size_t byteCount);
/* Free memory for pixels.
* Fine to call on a zeroed image or an image that failed to decode. */
void destroyImage(Image* image);
/* Mostly for internal use, but implementations are platform independent. */
uint8_t* allocateImageMemory(size_t byteCount);
bool isPng(const uint8_t* bytes, size_t byteCount);
bool isJpeg(const uint8_t* bytes, size_t byteCount);
////////// Implementation //////////
#include <string.h>
void destroyImage(Image* image)
{
free(image->pixels);
}
uint8_t* allocateImageMemory(size_t byteCount)
{
if (byteCount == 0) return NULL;
return (uint8_t*)malloc(byteCount);
}
bool isPng(const uint8_t* bytes, size_t byteCount)
{
uint8_t pngMagicNumber[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
return byteCount > sizeof(pngMagicNumber)
&& memcmp(pngMagicNumber, bytes, sizeof(pngMagicNumber)) == 0;
}
bool isJpeg(const uint8_t* bytes, size_t byteCount)
{
uint8_t jpegMagicNumber[] = {0xFF, 0xD8, 0xFF};
return byteCount > sizeof(jpegMagicNumber)
&& memcmp(jpegMagicNumber, bytes, sizeof(jpegMagicNumber)) == 0;
}
bool isWebp(const uint8_t* bytes, size_t byteCount)
{
return byteCount > 12
&& memcmp("RIFF", bytes, 4) == 0
&& memcmp("WEBP", bytes+8, 4) == 0;
}
#ifdef _WIN32
#include <wincodec.h>
#include <shlwapi.h>
Image decodeImage(const uint8_t* bytes, size_t byteCount)
{
CoInitialize(NULL);
Image result = {0};
IStream* stream = SHCreateMemStream(bytes, (UINT)byteCount);
IWICImagingFactory* imagingFactory = NULL;
CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void**)&imagingFactory);
IWICBitmapDecoder* decoder = NULL;
IWICBitmapFrameDecode* frame = NULL;
imagingFactory->CreateDecoderFromStream(stream, NULL, WICDecodeMetadataCacheOnLoad, &decoder);
if (decoder) decoder->GetFrame(0, &frame);
IWICFormatConverter* converter = NULL;
uint32_t width = 0;
uint32_t height = 0;
if (frame) {
imagingFactory->CreateFormatConverter(&converter);
converter->Initialize(frame, GUID_WICPixelFormat32bppRGBA, WICBitmapDitherTypeNone, 0, 0, WICBitmapPaletteTypeCustom);
converter->GetSize(&width, &height);
}
UINT imageByteCount = width * height * 4;
result.pixels = allocateImageMemory(imageByteCount);
if (result.pixels) {
result.width = width;
result.height = height;
UINT stride = width * 4;
converter->CopyPixels(NULL, stride, imageByteCount, result.pixels);
}
if (frame) frame->Release();
if (decoder) decoder->Release();
if (converter) converter->Release();
imagingFactory->Release();
stream->Release();
return result;
}
ImageMetadata decodeImageMetadata(const uint8_t* bytes, size_t byteCount)
{
CoInitialize(NULL);
ImageMetadata result = {0};
IStream* stream = SHCreateMemStream(bytes, (UINT)byteCount);
IWICImagingFactory* imagingFactory = NULL;
CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void**)&imagingFactory);
IWICBitmapDecoder* decoder = NULL;
IWICBitmapFrameDecode* frame = NULL;
imagingFactory->CreateDecoderFromStream(stream, NULL, WICDecodeMetadataCacheOnLoad, &decoder);
if (decoder) decoder->GetFrame(0, &frame);
if (frame) {
frame->GetSize(&result.width, &result.height);
frame->Release();
}
if (decoder) decoder->Release();
imagingFactory->Release();
stream->Release();
return result;
}
#endif // _WIN32
#ifdef __APPLE__
#include <CoreImage/CoreImage.h>
Image decodeImage(const uint8_t* bytes, size_t byteCount)
{
Image result = {0};
CFDataRef data = CFDataCreate(0, bytes, byteCount);
CGImageSourceRef imageSource = CGImageSourceCreateWithData(data, NULL);
CGImage* image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
uint32_t width = CGImageGetWidth(image);
uint32_t height = CGImageGetHeight(image);
size_t imageByteCount = width * height * 4;
result.pixels = allocateImageMemory(imageByteCount);
if (result.pixels) {
result.width = width;
result.height = height;
memset(result.pixels, 0, imageByteCount);
CGColorSpace* colorSpace = CGColorSpaceCreateDeviceRGB();
CGContext* cgContext = CGBitmapContextCreate(result.pixels, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast);
CGRect rect = CGRectMake(0, 0, width, height);
CGContextDrawImage(cgContext, rect, image);
CGColorSpaceRelease(colorSpace);
CGContextRelease(cgContext);
}
CGImageRelease(image);
CFRelease(imageSource);
CFRelease(data);
return result;
}
ImageMetadata decodeImageMetadata(const uint8_t* bytes, size_t byteCount)
{
ImageMetadata result = {0};
CFDataRef data = CFDataCreate(0, bytes, byteCount);
CGImageSourceRef imageSource = CGImageSourceCreateWithData(data, NULL);
CGImage* image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
result.width = CGImageGetWidth(image);
result.height = CGImageGetHeight(image);
CGImageRelease(image);
CFRelease(imageSource);
CFRelease(data);
return result;
}
#endif // __APPLE__
#if defined(__linux__) && !defined(__ANDROID__)
#include <turbojpeg.h>
#include <png.h>
#include <webp/decode.h>
static Image decodePng(const uint8_t* bytes, size_t byteCount)
{
Image result = {0};
png_image png = {0};
png.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_memory(&png, bytes, byteCount) != 0) {
png.format = PNG_FORMAT_RGBA;
result.pixels = allocateImageMemory(PNG_IMAGE_SIZE(png));
}
if (result.pixels) {
result.width = png.width;
result.height = png.height;
png_image_finish_read(&png, NULL, result.pixels, 0, 0);
}
png_image_free(&png);
return result;
}
static Image decodeJpeg(const uint8_t* bytes, size_t byteCount)
{
Image result = {0};
tjhandle turbojpeg = tj3Init(TJINIT_DECOMPRESS);
uint32_t width = 0;
uint32_t height = 0;
if (tj3DecompressHeader(turbojpeg, bytes, byteCount) == 0) {
width = tj3Get(turbojpeg, TJPARAM_JPEGWIDTH);
height = tj3Get(turbojpeg, TJPARAM_JPEGHEIGHT);
result.pixels = allocateImageMemory(width * height * 4);
}
if (result.pixels) {
result.width = width;
result.height = height;
tj3Decompress8(turbojpeg, bytes, byteCount, result.pixels, 0, TJPF_RGBA);
}
tjDestroy(turbojpeg);
return result;
}
static Image decodeWebp(const uint8_t* bytes, size_t byteCount)
{
Image result = {0};
int width, height;
uint8_t* pixels = WebPDecodeRGBA(bytes, byteCount, &width, &height);
if (pixels) {
size_t pixelByteCount = width * height * 4;
result.pixels = (uint8_t*)malloc(pixelByteCount);
if (result.pixels) {
memcpy(result.pixels, pixels, pixelByteCount);
result.width = width;
result.height = height;
}
WebPFree(pixels);
}
return result;
}
Image decodeImage(const uint8_t* bytes, size_t byteCount)
{
Image result = {0};
if (byteCount > 0) {
if (isJpeg(bytes, byteCount)) {
result = decodeJpeg(bytes, byteCount);
} else if (isPng(bytes, byteCount)) {
result = decodePng(bytes, byteCount);
} else if (isWebp(bytes, byteCount)) {
result = decodeWebp(bytes, byteCount);
}
}
return result;
}
static ImageMetadata decodeJpegMetadata(const uint8_t* bytes, size_t byteCount)
{
ImageMetadata result = {0};
tjhandle turbojpeg = tj3Init(TJINIT_DECOMPRESS);
if (tj3DecompressHeader(turbojpeg, bytes, byteCount) == 0) {
result.width = tj3Get(turbojpeg, TJPARAM_JPEGWIDTH);
result.height = tj3Get(turbojpeg, TJPARAM_JPEGHEIGHT);
}
tjDestroy(turbojpeg);
return result;
}
static ImageMetadata decodePngMetadata(const uint8_t* bytes, size_t byteCount)
{
ImageMetadata result = {0};
png_image png = {.version = PNG_IMAGE_VERSION};
if (png_image_begin_read_from_memory(&png, bytes, byteCount) != 0) {
result.width = png.width;
result.height = png.height;
}
png_image_free(&png);
return result;
}
static ImageMetadata decodeWebpMetadata(const uint8_t* bytes, size_t byteCount)
{
ImageMetadata result = {0};
int width, height;
if (WebPGetInfo(bytes, byteCount, &width, &height)) {
result.width = width;
result.height = height;
}
return result;
}
ImageMetadata decodeImageMetadata(const uint8_t* bytes, size_t byteCount)
{
ImageMetadata result = {0};
if (byteCount > 0) {
if (isJpeg(bytes, byteCount)) {
result = decodeJpegMetadata(bytes, byteCount);
} else if (isPng(bytes, byteCount)) {
result = decodePngMetadata(bytes, byteCount);
} else if (isWebp(bytes, byteCount)) {
result = decodeWebpMetadata(bytes, byteCount);
}
}
return result;
}
#endif // Linux