37static void PNGError(png_structp png, png_const_charp message);
38static void PNGWarning(png_structp png, png_const_charp message);
39static void PNGRead(png_structp png, png_bytep bytes, png_size_t
size);
42@interface OOPNGTextureLoader (OOPrivate)
45- (void)readBytes:(png_bytep)bytes count:(png_size_t)count;
55 fileData = [[NSData oo_dataWithOXZFile:_path] retain];
56 if (fileData ==
nil)
return;
57 length = [fileData length];
71 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
80@implementation OOPNGTextureLoader (OOPrivate)
84 png_bytepp rows = NULL;
93 if (png != NULL) pngInfo = png_create_info_struct(png);
94 if (pngInfo != NULL) pngEndInfo = png_create_info_struct(png);
95 if (pngEndInfo == NULL)
97 OOLog(
@"texture.load.png.setup.failed",
@"***** Error preparing to read %@.", _path);
112 png_set_read_fn(png,
self,
PNGRead);
114 png_read_info(png, pngInfo);
116 if (
EXPECT_NOT(!png_get_IHDR(png, pngInfo, &pngWidth, &pngHeight, &depth, &colorType, NULL, NULL, NULL)))
118 OOLog(
@"texture.load.png.failed",
@"Failed to get metadata from PNG %@", _path);
121 png_set_strip_16(png);
122 if (depth < 8 || colorType == PNG_COLOR_TYPE_PALETTE)
127 if (colorType == PNG_COLOR_TYPE_GRAY)
131 else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
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);
146#error Unknown handle byte order.
150 png_read_update_info(png, pngInfo);
151 png_set_interlace_handling(png);
156 _rowBytes = png_get_rowbytes(png, pngInfo);
159 rows = malloc(
sizeof *rows * _height);
160 _data = malloc(_rowBytes * _height);
161 if (
EXPECT_NOT(rows == NULL || _data == NULL))
177 for (i = 0; i != _height; ++i)
179 rows[i] = ((png_bytep)_data) + i * _rowBytes;
181 png_read_image(png, rows);
182 png_read_end(png, pngEndInfo);
186 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
190- (void)readBytes:(png_bytep)bytes count:(png_size_t)count
195 NSString *message = [NSString stringWithFormat:@"attempt to read beyond end of file (%@), file may be truncated.", _path];
196 png_error(png, [message UTF8String]);
199 assert(bytes != NULL);
213#if PNG_LIBPNG_VER >= 10400
214#define MSG_TERMINATOR "."
216#define MSG_TERMINATOR ""
220static void PNGError(png_structp png, png_const_charp message)
223 OOLog(
@"texture.load.png.error",
@"***** A PNG loading error occurred for %@: %s" MSG_TERMINATOR, [loader path], message);
225#if PNG_LIBPNG_VER >= 10500
228 longjmp(png_jmpbuf(png), 1);
233static void PNGWarning(png_structp png, png_const_charp message)
236 OOLog(
@"texture.load.png.warning",
@"----- A PNG loading warning occurred for %@: %s" MSG_TERMINATOR, [loader path], message);
240static void PNGRead(png_structp png, png_bytep bytes, png_size_t
size)
#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
void readBytes:count:(png_bytep bytes,[count] png_size_t count)