78{
79 png_bytepp rows = NULL;
80 png_uint_32 pngWidth,
81 pngHeight;
82 int depth,
83 colorType;
84 uint32_t i;
85
86
88 if (png != NULL)
89 pngInfo = png_create_info_struct(png);
90 if (pngInfo != NULL)
91 pngEndInfo = png_create_info_struct(png);
92 if (pngEndInfo == NULL) {
93 OOLog(
@"texture.load.png.setup.failed",
@"***** Error preparing to read %@.", _path);
95 }
96
98
99 if (_data) {
100 free(_data);
101 _data = NULL;
102 }
104 }
105
106 png_set_read_fn(png,
self,
PNGRead);
107
108 png_read_info(png, pngInfo);
109
110 if (
EXPECT_NOT(!png_get_IHDR(png, pngInfo, &pngWidth, &pngHeight, &depth, &colorType, NULL, NULL, NULL))) {
111 OOLog(
@"texture.load.png.failed",
@"Failed to get metadata from PNG %@", _path);
113 }
114 png_set_strip_16(png);
115 if (depth < 8 || colorType == PNG_COLOR_TYPE_PALETTE) {
116 png_set_expand(png);
117 }
118
119 if (colorType == PNG_COLOR_TYPE_GRAY) {
121 } else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
123 } else {
125
126#if OOLITE_BIG_ENDIAN
127 png_set_bgr(png);
128 png_set_swap_alpha(png);
129 png_set_filler(png, 0xFF, PNG_FILLER_BEFORE);
130#elif OOLITE_LITTLE_ENDIAN
131 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
132#else
133#error Unknown handle byte order.
134#endif
135 }
136
137 png_read_update_info(png, pngInfo);
138 png_set_interlace_handling(png);
139
140
141 _width = pngWidth;
142 _height = pngHeight;
143 _rowBytes = png_get_rowbytes(png, pngInfo);
144
145
146 rows = malloc(sizeof *rows * _height);
147 _data = malloc(_rowBytes * _height);
148 if (
EXPECT_NOT(rows == NULL || _data == NULL)) {
149 if (rows != NULL) {
150 free(rows);
151 rows = NULL;
152 }
153 if (_data != NULL) {
154 free(_data);
155 _data = NULL;
156 }
159 }
160
161 for (i = 0; i != _height; ++i) {
162 rows[i] = ((png_bytep)_data) + i * _rowBytes;
163 }
164 png_read_image(png, rows);
165 png_read_end(png, pngEndInfo);
166
168 free(rows);
169 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
170}
#define OOLog(class, format,...)
NSString *const kOOLogAllocationFailure
static void PNGRead(png_structp png, png_bytep bytes, png_size_t size)
static void PNGError(png_structp png, png_const_charp message)
static void PNGWarning(png_structp png, png_const_charp message)
@ kOOTextureDataGrayscaleAlpha
@ kOOTextureDataGrayscale