Oolite
Loading...
Searching...
No Matches
OOPNGTextureLoader(OOPrivate) Category Reference

Instance Methods

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

Detailed Description

Definition at line 40 of file OOPNGTextureLoader.m.

Method Documentation

◆ doLoadTexture

- (void) doLoadTexture

Extends class OOPNGTextureLoader.

Definition at line 77 of file OOPNGTextureLoader.m.

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}
#define EXPECT_NOT(x)
#define FAIL(s)
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogAllocationFailure
Definition OOLogging.m:598
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:105
@ kOOTextureDataGrayscale
Definition OOTexture.h:104
@ kOOTextureDataRGBA
Definition OOTexture.h:103

References doLoadTexture, EXPECT_NOT, FAIL, kOOLogAllocationFailure, kOOTextureDataGrayscale, kOOTextureDataGrayscaleAlpha, kOOTextureDataRGBA, OOLog, PNGError(), PNGRead(), and PNGWarning().

Referenced by doLoadTexture.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBytes:count:

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

Extends class OOPNGTextureLoader.

Definition at line 172 of file OOPNGTextureLoader.m.

172 :(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}
unsigned count
voidpf uLong offset
Definition ioapi.h:132

References count, and EXPECT_NOT.


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