Oolite 1.91.0.7644-241112-7f5034b
Loading...
Searching...
No Matches
OOPNGTextureLoader(OOPrivate) Category Reference

Instance Methods

(void) - doLoadTexture
 
(void) - readBytes:count:
 

Detailed Description

Definition at line 42 of file OOPNGTextureLoader.m.

Method Documentation

◆ doLoadTexture

- (void) doLoadTexture

Extends class OOPNGTextureLoader.

Definition at line 240 of file OOPNGTextureLoader.m.

83{
84 png_bytepp rows = NULL;
85 png_uint_32 pngWidth,
86 pngHeight;
87 int depth,
88 colorType;
89 uint32_t i;
90
91 // Set up PNG decoding
92 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, self, PNGError, PNGWarning);
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);
98 goto FAIL;
99 }
100
101 if (EXPECT_NOT(setjmp(png_jmpbuf(png))))
102 {
103 // libpng will jump here on error.
104 if (_data)
105 {
106 free(_data);
107 _data = NULL;
108 }
109 goto FAIL;
110 }
111
112 png_set_read_fn(png, self, PNGRead);
113
114 png_read_info(png, pngInfo);
115 // Read header, get format info and check that it meets our expectations.
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);
119 goto FAIL;
120 }
121 png_set_strip_16(png); // 16 bits per channel -> 8 bpc
122 if (depth < 8 || colorType == PNG_COLOR_TYPE_PALETTE)
123 {
124 png_set_expand(png); // Paletted -> RGB, greyscale -> 8 bpc
125 }
126
127 if (colorType == PNG_COLOR_TYPE_GRAY)
128 {
129 _format = kOOTextureDataGrayscale;
130 }
131 else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
132 {
134 }
135 else
136 {
137 _format = kOOTextureDataRGBA;
138
139#if OOLITE_BIG_ENDIAN
140 png_set_bgr(png);
141 png_set_swap_alpha(png); // RGBA->ARGB
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 // Metadata is acceptable; load data.
154 _width = pngWidth;
155 _height = pngHeight;
156 _rowBytes = png_get_rowbytes(png, pngInfo);
157
158 // png_read_png
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 }
173 OOLog(kOOLogAllocationFailure, @"Failed to allocate space (%zu bytes) for texture %@", _rowBytes * _height, _path);
174 goto FAIL;
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
184FAIL:
185 free(rows);
186 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
187}
#define EXPECT_NOT(x)
#define FAIL(s)
#define OOLog(class, format,...)
Definition OOLogging.h:88
NSString *const kOOLogAllocationFailure
Definition OOLogging.m:649
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
Definition OOTexture.h:111
@ kOOTextureDataGrayscale
Definition OOTexture.h:110
@ kOOTextureDataRGBA
Definition OOTexture.h:109

◆ readBytes:count:

- (void) readBytes: (png_bytep) bytes
count: (png_size_t) count 

Extends class OOPNGTextureLoader.

Definition at line 240 of file OOPNGTextureLoader.m.

190 :(png_bytep)bytes count:(png_size_t)count
191{
192 // Check that we're within the file's bounds
193 if (EXPECT_NOT(length - offset < count))
194 {
195 NSString *message = [NSString stringWithFormat:@"attempt to read beyond end of file (%@), file may be truncated.", _path];
196 png_error(png, [message UTF8String]); // Will not return
197 }
198
199 assert(bytes != NULL);
200
201 // Copy bytes
202 memcpy(bytes, [fileData bytes] + offset, count);
203 offset += count;
204}
unsigned count
voidpf uLong offset
Definition ioapi.h:140

The documentation for this category was generated from the following file: