83{
84 png_bytepp rows = NULL;
85 png_uint_32 pngWidth,
86 pngHeight;
87 int depth,
88 colorType;
89 uint32_t i;
90
91
93 if (png != NULL) pngInfo = png_create_info_struct(png);
94 if (pngInfo != NULL) pngEndInfo = png_create_info_struct(png);
95 if (pngEndInfo == NULL)
96 {
97 OOLog(
@"texture.load.png.setup.failed",
@"***** Error preparing to read %@.", _path);
99 }
100
102 {
103
104 if (_data)
105 {
106 free(_data);
107 _data = NULL;
108 }
110 }
111
112 png_set_read_fn(png,
self,
PNGRead);
113
114 png_read_info(png, pngInfo);
115
116 if (
EXPECT_NOT(!png_get_IHDR(png, pngInfo, &pngWidth, &pngHeight, &depth, &colorType, NULL, NULL, NULL)))
117 {
118 OOLog(
@"texture.load.png.failed",
@"Failed to get metadata from PNG %@", _path);
120 }
121 png_set_strip_16(png);
122 if (depth < 8 || colorType == PNG_COLOR_TYPE_PALETTE)
123 {
124 png_set_expand(png);
125 }
126
127 if (colorType == PNG_COLOR_TYPE_GRAY)
128 {
130 }
131 else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
132 {
134 }
135 else
136 {
138
139#if OOLITE_BIG_ENDIAN
140 png_set_bgr(png);
141 png_set_swap_alpha(png);
142 png_set_filler(png, 0xFF, PNG_FILLER_BEFORE);
143#elif OOLITE_LITTLE_ENDIAN
144 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
145#else
146#error Unknown handle byte order.
147#endif
148 }
149
150 png_read_update_info(png, pngInfo);
151 png_set_interlace_handling(png);
152
153
154 _width = pngWidth;
155 _height = pngHeight;
156 _rowBytes = png_get_rowbytes(png, pngInfo);
157
158
159 rows = malloc(sizeof *rows * _height);
160 _data = malloc(_rowBytes * _height);
161 if (
EXPECT_NOT(rows == NULL || _data == NULL))
162 {
163 if (rows != NULL)
164 {
165 free(rows);
166 rows = NULL;
167 }
168 if (_data != NULL)
169 {
170 free(_data);
171 _data = NULL;
172 }
175 }
176
177 for (i = 0; i != _height; ++i)
178 {
179 rows[i] = ((png_bytep)_data) + i * _rowBytes;
180 }
181 png_read_image(png, rows);
182 png_read_end(png, pngEndInfo);
183
185 free(rows);
186 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
187}
#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