Oolite
Loading...
Searching...
No Matches
OOPNGTextureLoader.m
Go to the documentation of this file.
1/*
2
3OOPNGTextureLoader.m
4
5
6Copyright (C) 2007-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
30#import "OOCPUInfo.h"
32#import "OOLogging.h"
33
34// void png_error(png_structp, png_const_charp) NO_RETURN_FUNC;
35
36static void PNGError(png_structp png, png_const_charp message);
37static void PNGWarning(png_structp png, png_const_charp message);
38static void PNGRead(png_structp png, png_bytep bytes, png_size_t size);
39
40@interface OOPNGTextureLoader (OOPrivate)
41
42- (void)doLoadTexture;
43- (void)readBytes:(png_bytep)bytes count:(png_size_t)count;
44
45@end
46
47@implementation OOPNGTextureLoader
48
50{
51 // Get data from file
52 fileData = [[NSData oo_dataWithOXZFile:_path] retain];
53 if (fileData == nil)
54 return;
55 length = [fileData length];
56
57 [self doLoadTexture];
58
59 [fileData release];
60 fileData = nil;
61}
62
63- (void)dealloc
64{
65 [fileData release];
66 if (png != NULL) {
67 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
68 }
69
70 [super dealloc];
71}
72
73@end
74
75@implementation OOPNGTextureLoader (OOPrivate)
76
78{
79 png_bytepp rows = NULL;
80 png_uint_32 pngWidth,
81 pngHeight;
82 int depth,
83 colorType;
84 uint32_t i;
85
86 // Set up PNG decoding
87 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, self, PNGError, PNGWarning);
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);
94 goto FAIL;
95 }
96
97 if (EXPECT_NOT(setjmp(png_jmpbuf(png)))) {
98 // libpng will jump here on error.
99 if (_data) {
100 free(_data);
101 _data = NULL;
102 }
103 goto FAIL;
104 }
105
106 png_set_read_fn(png, self, PNGRead);
107
108 png_read_info(png, pngInfo);
109 // Read header, get format info and check that it meets our expectations.
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);
112 goto FAIL;
113 }
114 png_set_strip_16(png); // 16 bits per channel -> 8 bpc
115 if (depth < 8 || colorType == PNG_COLOR_TYPE_PALETTE) {
116 png_set_expand(png); // Paletted -> RGB, greyscale -> 8 bpc
117 }
118
119 if (colorType == PNG_COLOR_TYPE_GRAY) {
120 _format = kOOTextureDataGrayscale;
121 } else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
123 } else {
124 _format = kOOTextureDataRGBA;
125
126#if OOLITE_BIG_ENDIAN
127 png_set_bgr(png);
128 png_set_swap_alpha(png); // RGBA->ARGB
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 // Metadata is acceptable; load data.
141 _width = pngWidth;
142 _height = pngHeight;
143 _rowBytes = png_get_rowbytes(png, pngInfo);
144
145 // png_read_png
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 }
157 OOLog(kOOLogAllocationFailure, @"Failed to allocate space (%zu bytes) for texture %@", _rowBytes * _height, _path);
158 goto FAIL;
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
167FAIL:
168 free(rows);
169 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
170}
171
172- (void)readBytes:(png_bytep)bytes count:(png_size_t)count
173{
174 // Check that we're within the file's bounds
175 if (EXPECT_NOT(length - offset < count)) {
176 NSString* message = [NSString stringWithFormat:@"attempt to read beyond end of file (%@), file may be truncated.", _path];
177 png_error(png, [message UTF8String]); // Will not return
178 }
179
180 assert(bytes != NULL);
181
182 // Copy bytes
183 memcpy(bytes, [fileData bytes] + offset, count);
184 offset += count;
185}
186
187@end
188
189/* Minor detail: libpng 1.4.0 removed trailing .s from error and warning
190 messages. It's also binary-incompatible with 1.2 and earlier, so it's
191 reasonable to make this test at build time.
192*/
193#if PNG_LIBPNG_VER >= 10400
194#define MSG_TERMINATOR "."
195#else
196#define MSG_TERMINATOR ""
197#endif
198
199static void PNGError(png_structp png, png_const_charp message)
200{
201 OOPNGTextureLoader* loader = png_get_io_ptr(png);
202 OOLog(@"texture.load.png.error", @"***** A PNG loading error occurred for %@: %s" MSG_TERMINATOR, [loader path], message);
203
204#if PNG_LIBPNG_VER >= 10500
205 png_longjmp(png, 1);
206#else
207 longjmp(png_jmpbuf(png), 1);
208#endif
209}
210
211static void PNGWarning(png_structp png, png_const_charp message)
212{
213 OOPNGTextureLoader* loader = png_get_io_ptr(png);
214 OOLog(@"texture.load.png.warning", @"----- A PNG loading warning occurred for %@: %s" MSG_TERMINATOR, [loader path], message);
215}
216
217static void PNGRead(png_structp png, png_bytep bytes, png_size_t size)
218{
219 OOPNGTextureLoader* loader = png_get_io_ptr(png);
220 [loader readBytes:bytes count:size];
221}
#define EXPECT_NOT(x)
#define FAIL(s)
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogAllocationFailure
Definition OOLogging.m:598
#define MSG_TERMINATOR
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)
unsigned count
return nil
@ kOOTextureDataGrayscaleAlpha
Definition OOTexture.h:105
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
@ kOOTextureDataRGBA
Definition OOTexture.h:103
void readBytes:count:(png_bytep bytes,[count] png_size_t count)
voidpf void uLong size
Definition ioapi.h:126
voidpf uLong offset
Definition ioapi.h:132