/
png_load.cpp
113 lines (98 loc) · 3 KB
/
png_load.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef USING_QT_UI
#include <QtGui/QImage>
#else
#include <png.h>
#endif
#include "png_load.h"
#include "base/logging.h"
// *image_data_ptr should be deleted with free()
// return value of 1 == success.
int pngLoad(const char *file, int *pwidth, int *pheight, unsigned char **image_data_ptr, bool flip) {
#ifdef USING_QT_UI
QImage image(file, "PNG");
if (image.isNull()) {
ELOG("pngLoad: Error loading image %s", file);
return 0;
}
if (flip)
image = image.mirrored();
*pwidth = image.width();
*pheight = image.height();
image = image.convertToFormat(QImage::Format_ARGB32);
*image_data_ptr = (unsigned char *)malloc(image.byteCount());
uint32_t *src = (uint32_t*) image.bits();
uint32_t *dest = (uint32_t*) *image_data_ptr;
// Qt4 does not support RGBA
for (size_t sz = 0; sz < (size_t)image.byteCount(); sz+=4, ++src, ++dest) {
const uint32_t v = *src;
*dest = (v & 0xFF00FF00) | ((v & 0xFF) << 16) | (( v >> 16 ) & 0xFF); // ARGB -> RGBA
}
#else
if (flip)
ELOG("pngLoad: flip flag not supported, image will be loaded upside down");
png_image png;
memset(&png, 0, sizeof(png));
png.version = PNG_IMAGE_VERSION;
png_image_begin_read_from_file(&png, file);
if (PNG_IMAGE_FAILED(png))
{
ELOG("pngLoad: %s", png.message);
return 0;
}
*pwidth = png.width;
*pheight = png.height;
png.format = PNG_FORMAT_RGBA;
int stride = PNG_IMAGE_ROW_STRIDE(png);
*image_data_ptr = (unsigned char *)malloc(PNG_IMAGE_SIZE(png));
png_image_finish_read(&png, NULL, *image_data_ptr, stride, NULL);
#endif
return 1;
}
int pngLoadPtr(const unsigned char *input_ptr, size_t input_len, int *pwidth, int *pheight, unsigned char **image_data_ptr,
bool flip) {
#ifdef USING_QT_UI
QImage image;
if (!image.loadFromData(input_ptr, input_len, "PNG")) {
ELOG("pngLoad: Error loading image");
return 0;
}
if (flip)
image = image.mirrored();
*pwidth = image.width();
*pheight = image.height();
image = image.convertToFormat(QImage::Format_ARGB32);
*image_data_ptr = (unsigned char *)malloc(image.byteCount());
uint32_t *src = (uint32_t*) image.bits();
uint32_t *dest = (uint32_t*) *image_data_ptr;
// Qt4 does not support RGBA
for (size_t sz = 0; sz < (size_t)image.byteCount(); sz+=4, ++src, ++dest) {
const uint32_t v = *src;
*dest = (v & 0xFF00FF00) | ((v & 0xFF) << 16) | (( v >> 16 ) & 0xFF); // convert it!
}
#else
if (flip)
ELOG("pngLoad: flip flag not supported, image will be loaded upside down");
png_image png{};
png.version = PNG_IMAGE_VERSION;
png_image_begin_read_from_memory(&png, input_ptr, input_len);
if (PNG_IMAGE_FAILED(png)) {
ELOG("pngLoad: %s", png.message);
return 0;
}
*pwidth = png.width;
*pheight = png.height;
png.format = PNG_FORMAT_RGBA;
int stride = PNG_IMAGE_ROW_STRIDE(png);
size_t size = PNG_IMAGE_SIZE(png);
if (!size) {
ELOG("pngLoad: empty image");
return 0;
}
*image_data_ptr = (unsigned char *)malloc(size);
png_image_finish_read(&png, NULL, *image_data_ptr, stride, NULL);
#endif
return 1;
}