Oolite
Loading...
Searching...
No Matches
OOPNGTextureLoader Class Reference

#include <OOPNGTextureLoader.h>

Inheritance diagram for OOPNGTextureLoader:
Collaboration diagram for OOPNGTextureLoader:

Instance Methods

(void) - loadTexture [implementation]
(void) - dealloc [implementation]
(void) - doLoadTexture [implementation]
(void) - readBytes:count: [implementation]
Instance Methods inherited from OOTextureLoader
(BOOL) - isReady
(BOOL) - getResult:format:originalWidth:originalHeight:
(NSString *) - cacheKey
(id) - initWithPath:options:
(NSString *) - path
(NSString *) - descriptionComponents [implementation]
(NSString *) - shortDescriptionComponents [implementation]
(void) - performAsyncTask [implementation]
(void) - generateMipMapsForCubeMap [implementation]
(void) - applySettings [implementation]
(void) - getDesiredWidth:andHeight: [implementation]
(void) - completeAsyncTask [implementation]

Private Attributes

png_structp png
png_infop pngInfo
png_infop pngEndInfo
NSData * fileData
size_t length
size_t offset

Additional Inherited Members

Class Methods inherited from OOTextureLoader
(id) + loaderWithPath:options:
(id) + loaderWithTextureSpecifier:extraOptions:folder:
(void) + setUp [implementation]
Protected Attributes inherited from OOTextureLoader
NSString * _path
OOTextureFlags _options
uint8_t _generateMipMaps: 1
uint8_t _scaleAsNormalMap: 1
uint8_t _avoidShrinking: 1
uint8_t _noScalingWhatsoever: 1
uint8_t _extractChannel: 1
uint8_t _allowCubeMap: 1
uint8_t _isCubeMap: 1
uint8_t _ready: 1
uint8_t _extractChannelIndex
OOTextureDataFormat _format
void * _data
uint32_t _width
uint32_t _height
uint32_t _originalWidth
uint32_t _originalHeight
uint32_t _shrinkThreshold
uint32_t _maxSize
size_t _rowBytes

Detailed Description

Definition at line 33 of file OOPNGTextureLoader.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Reimplemented from OOTextureLoader.

Definition at line 63 of file OOPNGTextureLoader.m.

64{
65 [fileData release];
66 if (png != NULL) {
67 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
68 }
69
70 [super dealloc];
71}

References dealloc, OOTextureLoader::dealloc, png, pngEndInfo, and pngInfo.

Referenced by dealloc.

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

◆ doLoadTexture

- (void) doLoadTexture
implementation

Provided by category OOPNGTextureLoader(OOPrivate).

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) {
121 } else if (colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
123 } else {
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
OOTextureDataFormat _format

Referenced by loadTexture.

Here is the caller graph for this function:

◆ loadTexture

- (void) loadTexture
implementation

Reimplemented from OOTextureLoader.

Definition at line 49 of file OOPNGTextureLoader.m.

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}
return nil

References doLoadTexture, fileData, length, loadTexture, and nil.

Referenced by loadTexture.

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 
implementation

Provided by category OOPNGTextureLoader(OOPrivate).

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

Referenced by PNGRead().

Here is the caller graph for this function:

Member Data Documentation

◆ fileData

- (NSData*) fileData
private

Definition at line 38 of file OOPNGTextureLoader.h.

Referenced by loadTexture.

◆ length

- (size_t) length
private

Definition at line 39 of file OOPNGTextureLoader.h.

Referenced by loadTexture.

◆ offset

- (size_t) offset
private

Definition at line 40 of file OOPNGTextureLoader.h.

◆ png

- (png_structp) png
private

Definition at line 35 of file OOPNGTextureLoader.h.

Referenced by dealloc.

◆ pngEndInfo

- (png_infop) pngEndInfo
private

Definition at line 37 of file OOPNGTextureLoader.h.

Referenced by dealloc.

◆ pngInfo

- (png_infop) pngInfo
private

Definition at line 36 of file OOPNGTextureLoader.h.

Referenced by dealloc.


The documentation for this class was generated from the following files: