Oolite 1.91.0.7645-241119-222d325
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]
 
- Instance Methods inherited from <OOAsyncWorkTask>

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 34 of file OOPNGTextureLoader.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Reimplemented from OOTextureLoader.

Definition at line 240 of file OOPNGTextureLoader.m.

67{
68 [fileData release];
69 if (png != NULL)
70 {
71 png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
72 }
73
74 [super dealloc];
75}

References readBytes:count:.

+ Here is the call graph for this function:

◆ doLoadTexture

- (void) doLoadTexture
implementation

Provided by category OOPNGTextureLoader(OOPrivate).

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

◆ loadTexture

- (void) loadTexture
implementation

Reimplemented from OOTextureLoader.

Definition at line 240 of file OOPNGTextureLoader.m.

53{
54 // Get data from file
55 fileData = [[NSData oo_dataWithOXZFile:_path] retain];
56 if (fileData == nil) return;
57 length = [fileData length];
58
59 [self doLoadTexture];
60
61 [fileData release];
62 fileData = nil;
63}
return nil

◆ readBytes:count:

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

Provided by category OOPNGTextureLoader(OOPrivate).

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

Referenced by dealloc.

+ Here is the caller graph for this function:

Member Data Documentation

◆ fileData

- (NSData*) fileData
private

Definition at line 40 of file OOPNGTextureLoader.h.

◆ length

- (size_t) length
private

Definition at line 41 of file OOPNGTextureLoader.h.

◆ offset

- (size_t) offset
private

Definition at line 42 of file OOPNGTextureLoader.h.

◆ png

- (png_structp) png
private

Definition at line 37 of file OOPNGTextureLoader.h.

◆ pngEndInfo

- (png_infop) pngEndInfo
private

Definition at line 39 of file OOPNGTextureLoader.h.

◆ pngInfo

- (png_infop) pngInfo
private

Definition at line 38 of file OOPNGTextureLoader.h.


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