-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage.cpp
More file actions
178 lines (156 loc) · 5.29 KB
/
Copy pathimage.cpp
File metadata and controls
178 lines (156 loc) · 5.29 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
// Libpng wrapper
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <string.h> // for memcpy
#include "image.h"
// Reference:
// https://gist.github.com/mortennobel/5299151
/* load_png
*
* Loads a PNG image into a newly allocated GLubyte array
*
* Returns true on success, false on failure
*
* Sets outWidth and outHeight to image dimensions
* Allocates a GLubyte array, and sets outData to point to the newly created array
*/
bool load_png (const char* filename, int& outWidth, int& outHeight, bool &outHasAlpha, GLubyte **outData) {
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
int color_type, interlace_type;
FILE *fp;
if ((fp = fopen(filename, "rb")) == NULL) {
fprintf (stderr, "(File not found) Failed to open file %s\n", filename);
return false;
}
/* Create and initialize the png_struct
* with the desired error handler
* functions. If you want to use the
* default stderr and longjump method,
* you can supply NULL for the last
* three parameters. We also supply the
* the compiler header file version, so
* that we know if the application
* was compiled with a compatible version
* of the library. REQUIRED
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
return false;
}
/* Allocate/initialize the memory
* for image information. REQUIRED. */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
return false;
}
/* Set error handling if you are
* using the setjmp/longjmp method
* (this is the normal method of
* doing things with libpng).
* REQUIRED unless you set up
* your own error handlers in
* the png_create_read_struct()
* earlier.
*/
if (setjmp(png_jmpbuf(png_ptr))) {
/* Free all of the memory associated
* with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
/* If we get here, we had a
* problem reading the file */
return false;
}
/* Set up the output control if
* you are using standard C streams */
png_init_io(png_ptr, fp);
/* If we have already
* read some of the signature */
png_set_sig_bytes(png_ptr, sig_read);
/*
* If you have enough memory to read
* in the entire image at once, and
* you need to specify only
* transforms that can be controlled
* with one of the PNG_TRANSFORM_*
* bits (this presently excludes
* dithering, filling, setting
* background, and doing gamma
* adjustment), then you can read the
* entire image (including pixels)
* into the info structure with this
* call
*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to
* expand a palette into RGB
*/
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
png_uint_32 width, height;
int bit_depth;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
&interlace_type, NULL, NULL);
outWidth = width;
outHeight = height;
// Whether this is color or grayscale, if alpha channel exists, return true here:
outHasAlpha = color_type & (PNG_COLOR_MASK_ALPHA);
unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr);
*outData = (unsigned char*) malloc(row_bytes * outHeight);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < outHeight; i++) {
// note that png is ordered top to
// bottom, but OpenGL expect it bottom to top
// so the order or swapped
memcpy(*outData+(row_bytes * (outHeight-1-i)), row_pointers[i], row_bytes);
}
/* Clean up after the read,
* and free any memory allocated */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
/* Close the file */
fclose(fp);
/* That's it */
return true;
}
Image::Image() {
// Start out with empty values
this->filename = NULL;
this->width = 0;
this->height = 0;
this->hasAlpha = false;
this->texture = NULL;
}
Image::Image(const char* filename) : filename(filename) {
this->width = 0;
this->height = 0;
this->texture = NULL;
this->hasAlpha = false;
if (!load_png(filename, this->width, this->height, this->hasAlpha, &(this->texture))) {
// An error occurred, try to free the buffer if it was allocated,
// and set the image to be uninitialized
free(this->texture);
this->texture = NULL;
this->width = 0;
this->height = 0;
this->hasAlpha = false;
fprintf(stderr, "Error loading file %s\n", filename);
}
}
Image::~Image() {
// Try to free the allocated memory
if (texture != NULL)
free(texture);
// Set the image parameters such that any code using this object should become a NOP
this->texture = NULL;
this->width = 0;
this->height = 0;
this->hasAlpha = false;
}