63@interface OOTextureLoader (OOPrivate)
76+ (id)loaderWithPath:(NSString *)inPath options:(uint32_t)options
78 NSString *extension =
nil;
88 extension = [[inPath pathExtension] lowercaseString];
89 if ([extension isEqualToString:
@"png"])
91 result = [[[
OOPNGTextureLoader alloc] initWithPath:inPath options:options] autorelease];
95 OOLog(
@"texture.load.unknownType",
@"Can't use %@ as a texture - extension \"%@\
" does not identify a known type.", inPath, extension);
107+ (id)loaderWithTextureSpecifier:(
id)specifier extraOptions:(uint32_t)extraOptions folder:(NSString *)folder
109 NSString *name =
nil;
110 NSString *path =
nil;
111 uint32_t options = 0;
114 options |= extraOptions;
126 return [
self loaderWithPath:path options:options];
130- (id)initWithPath:(NSString *)inPath options:(uint32_t)options
133 if (
self ==
nil)
return nil;
135 _path = [inPath copy];
149 if (_avoidShrinking || _noScalingWhatsoever)
161#if OO_TEXTURE_CUBE_MAP
167 _extractChannel = YES;
171 _extractChannelIndex = 0;
175 _extractChannelIndex = 1;
179 _extractChannelIndex = 2;
183 _extractChannelIndex = 3;
187 OOLogERR(
@"texture.load.unknownExtractChannelMask",
@"Unknown texture extract channel mask (0x%.4X). This is an internal error, please report it.", options &
kOOTextureExtractChannelMask);
188 _extractChannel = NO;
207- (NSString *)descriptionComponents
209 NSString *state =
nil;
213 if (_data != NULL) state =
@"ready";
214 else state =
@"failed";
219#if INSTRUMENT_TEXTURE_LOADING
220 if (debugHasLoaded) state =
@"loaded";
224 return [NSString stringWithFormat:@"{%@ -- %@}", _path, state];
228- (NSString *)shortDescriptionComponents
230 return [_path lastPathComponent];
246- (BOOL) getResult:(
OOPixMap *)result
248 originalWidth:(uint32_t *)outWidth
249 originalHeight:(uint32_t *)outHeight
251 NSParameterAssert(result != NULL && outFormat != NULL);
259 if (_data == NULL) OK = NO;
265 *outFormat = _format;
267 if (outWidth != NULL) *outWidth = _originalWidth;
268 if (outHeight != NULL) *outHeight = _originalHeight;
281- (NSString *) cacheKey
283 return [NSString stringWithFormat:@"%@:0x%.4X", [[
self path] lastPathComponent], _options];
297 OOGL(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize));
299 OOLog(
@"texture.load.rescale.maxSize",
@"GL maximum texture size: %u",
sGLMaxSize);
302 sUserMaxSize = [[NSUserDefaults standardUserDefaults] oo_unsignedIntForKey:@"max-texture-size" defaultValue:0x80000000];
314- (void)performAsyncTask
318 OOLog(
@"texture.load.asyncLoad",
@"Loading texture %@", [_path lastPathComponent]);
325 OOLog(
@"texture.load.failed.internalError",
@"Texture loader internal error for %@: data is non-null but data format is invalid (%u).", _path, _format);
330 if (_data != NULL) [
self applySettings];
332 OOLog(
@"texture.load.asyncLoad.done",
@"%@",
@"Loading complete.");
334 @catch (NSException *exception)
336 OOLog(
@"texture.load.asyncLoad.exception",
@"***** Exception loading texture %@: %@ (%@).", _path, [exception name], [exception reason]);
345- (void) generateMipMapsForCubeMap
348 NSParameterAssert(_data != NULL);
351 size_t srcSideSize = _width * _width * components;
352 size_t newSideSize = srcSideSize * 4 / 3;
353 newSideSize = (newSideSize + 15) & ~15;
354 size_t newSize = newSideSize * 6;
356 void *newData = malloc(newSize);
359 _generateMipMaps = NO;
365 for (i = 0; i < 6; i++)
367 void *srcBytes = ((uint8_t *)_data) + srcSideSize * i;
368 void *dstBytes = ((uint8_t *)newData) + newSideSize * i;
370 memcpy(dstBytes, srcBytes, srcSideSize);
390 if (_originalWidth == 0) _originalWidth = _width;
391 if (_originalHeight == 0) _originalHeight = _height;
392 if (_rowBytes == 0) _rowBytes = _width * components;
394 pixMap =
OOMakePixMap(_data, _width, _height, components, _rowBytes, 0);
405 OOLogWARN(
@"texture.load.extractChannel.invalid",
@"Cannot extract channel from texture \"%@\
"", [_path lastPathComponent]);
409 [
self getDesiredWidth:&desiredWidth andHeight:&desiredHeight];
414 desiredHeight =
MIN(desiredWidth * 2, 512U);
416 desiredWidth = desiredHeight * 2;
423#if DUMP_CONVERTED_CUBE_MAPS
424 OODumpPixMap(pixMap, [NSString stringWithFormat:
@"converted cube map %@", [[_path lastPathComponent] stringByDeletingPathExtension]]);
429 rescale = (_width != desiredWidth || _height != desiredHeight);
432 BOOL leaveSpaceForMipMaps = _generateMipMaps;
433#if OO_TEXTURE_CUBE_MAP
434 if (_isCubeMap) leaveSpaceForMipMaps = NO;
437 OOLog(
@"texture.load.rescale",
@"Rescaling texture \"%@\
" from %u x %u to %u x %u.", [_path lastPathComponent], pixMap.
width, pixMap.
height, desiredWidth, desiredHeight);
439 pixMap =
OOScalePixMap(pixMap, desiredWidth, desiredHeight, leaveSpaceForMipMaps);
443 _width = pixMap.
width;
448#if OO_TEXTURE_CUBE_MAP
451 if (_generateMipMaps)
453 [
self generateMipMapsForCubeMap];
460 if (_generateMipMaps)
463 newSize = desiredWidth * components * desiredHeight;
464 newSize = (newSize * 4) / 3;
469 _width = pixMap.
width;
473 if (_generateMipMaps)
OOPixMap OOScalePixMap(OOPixMap srcPixMap, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
BOOL OOGenerateMipMaps(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)
uint8_t OOTextureComponentsForFormat(OOTextureDataFormat format)
BOOL OOInterpretTextureSpecifier(id specifier, NSString **outName, OOTextureFlags *outOptions, float *outAnisotropy, float *outLODBias, BOOL ignoreExtract)