Oolite
Loading...
Searching...
No Matches
OOTextureScaling.h File Reference
import "OOPixMap.h"
Include dependency graph for OOTextureScaling.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

OOPixMap OOScalePixMap (OOPixMap srcPixMap, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
BOOL OOGenerateMipMaps (void *textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)

Function Documentation

◆ OOGenerateMipMaps()

BOOL OOGenerateMipMaps ( void * textureBytes,
OOPixMapDimension width,
OOPixMapDimension height,
OOPixMapFormat format )

Definition at line 336 of file OOTextureScaling.m.

337{
338 if (EXPECT_NOT(width != OORoundUpToPowerOf2_PixMap(width) || height != OORoundUpToPowerOf2_PixMap(height))) {
339 OOLog(kOOLogParameterError, @"Non-power-of-two dimensions (%ux%u) passed to %s() - ignoring, data will be junk.", width, height, __PRETTY_FUNCTION__);
340 return NO;
341 }
342 if (EXPECT_NOT(textureBytes == NULL)) {
343 OOLog(kOOLogParameterError, @"%@", @"NULL texture pointer passed to GenerateMipMaps().");
344 return NO;
345 }
346
347 switch (format) {
348 case kOOPixMapRGBA:
349 return GenerateMipMaps4(textureBytes, width, height);
350
352 return GenerateMipMaps1(textureBytes, width, height);
353
355 return GenerateMipMaps2(textureBytes, width, height);
356
358 break;
359 }
360
361 OOLog(kOOLogParameterError, @"%s(): bad pixmap format (%@) - ignoring, data will be junk.", __PRETTY_FUNCTION__, OOPixMapFormatName(format));
362 return NO;
363}
#define EXPECT_NOT(x)
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogParameterError
Definition OOLogging.m:596
NSString * OOPixMapFormatName(OOPixMapFormat format) PURE_FUNC
Definition OOPixMap.m:212
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:37
@ kOOPixMapGrayscale
Definition OOPixMap.h:38
@ kOOPixMapRGBA
Definition OOPixMap.h:40
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:39
#define OORoundUpToPowerOf2_PixMap
Definition OOPixMap.h:34
static BOOL GenerateMipMaps2(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static BOOL GenerateMipMaps4(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static BOOL GenerateMipMaps1(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC

References EXPECT_NOT, GenerateMipMaps1(), GenerateMipMaps2(), GenerateMipMaps4(), kOOLogParameterError, kOOPixMapGrayscale, kOOPixMapGrayscaleAlpha, kOOPixMapInvalidFormat, kOOPixMapRGBA, OOLog, OOPixMapFormatName(), and OORoundUpToPowerOf2_PixMap.

Referenced by OOTextureLoader(OOPrivate)::applySettings, OOTextureLoader::generateMipMapsForCubeMap, and OOPixMapTextureLoader::loadTexture.

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

◆ OOScalePixMap()

OOPixMap OOScalePixMap ( OOPixMap srcPixMap,
OOPixMapDimension dstWidth,
OOPixMapDimension dstHeight,
BOOL leaveSpaceForMipMaps )

Definition at line 244 of file OOTextureScaling.m.

245{
246 OOPixMap dstPx = { 0 }, sparePx = { 0 };
247 BOOL OK = YES;
248
249 // Sanity check.
250 if (EXPECT_NOT(!OOIsValidPixMap(srcPx))) {
252 free(srcPx.pixels);
253 return kOONullPixMap;
254 }
255
257 DUMP_SCALE_DUMP(srcPx, @"initial");
258
259 if (srcPx.height < dstHeight) {
260 // Stretch vertically. This requires a separate buffer.
261 size_t dstSize = srcPx.rowBytes * dstHeight;
262 if (leaveSpaceForMipMaps && dstWidth <= srcPx.width)
263 dstSize = dstSize * 4 / 3;
264
265 dstPx = OOAllocatePixMap(srcPx.width, dstHeight, srcPx.format, 0, dstSize);
266 if (EXPECT_NOT(!OOIsValidPixMap(dstPx))) {
267 OK = NO;
268 goto FAIL;
269 }
270
271 StretchVertically(srcPx, dstPx);
272 DUMP_SCALE_DUMP(dstPx, @"stretched vertically");
273
274 sparePx = srcPx;
275 srcPx = dstPx;
276 } else if (dstHeight < srcPx.height) {
277 // Squeeze vertically. This can be done in-place.
278 SqueezeVertically(srcPx, dstHeight);
279 srcPx.height = dstHeight;
280 DUMP_SCALE_DUMP(srcPx, @"squeezed vertically");
281 }
282
283 if (srcPx.width < dstWidth) {
284 // Stretch horizontally. This requires a separate buffer.
285 size_t dstSize = OOPixMapBytesPerPixel(srcPx) * dstWidth * srcPx.height;
286 if (leaveSpaceForMipMaps)
287 dstSize = dstSize * 4 / 3;
288
289 if (dstSize <= sparePx.bufferSize) {
290 dstPx = OOMakePixMap(sparePx.pixels, dstWidth, srcPx.height, srcPx.format, 0, sparePx.bufferSize);
291 sparePx = kOONullPixMap;
292 } else {
293 dstPx = OOAllocatePixMap(dstWidth, srcPx.height, srcPx.format, 0, dstSize);
294 }
295 if (EXPECT_NOT(!OOIsValidPixMap(dstPx))) {
296 OK = NO;
297 goto FAIL;
298 }
299
300 StretchHorizontally(srcPx, dstPx);
301 DUMP_SCALE_DUMP(dstPx, @"stretched horizontally");
302 } else if (dstWidth < srcPx.width) {
303 // Squeeze horizontally. This can be done in-place.
304 SqueezeHorizontally(srcPx, dstWidth);
305
306 dstPx = srcPx;
307 dstPx.width = dstWidth;
308 dstPx.rowBytes = dstPx.width * OOPixMapBytesPerPixel(dstPx);
309 DUMP_SCALE_DUMP(dstPx, @"squeezed horizontally");
310 } else {
311 // No horizontal scaling.
312 dstPx = srcPx;
313 }
314
315 // Avoid a potential double free (if the realloc in EnsureCorrectDataSize() relocates the block).
316 if (srcPx.pixels == dstPx.pixels)
317 srcPx.pixels = NULL;
318
319 // dstPx is now the result.
320 OK = EnsureCorrectDataSize(&dstPx, leaveSpaceForMipMaps);
321
322FAIL:
323 free(srcPx.pixels);
324 if (sparePx.pixels != dstPx.pixels && sparePx.pixels != srcPx.pixels) {
325 free(sparePx.pixels);
326 }
327 if (!OK) {
328 free(dstPx.pixels);
329 dstPx.pixels = NULL;
330 }
331
332 return OK ? dstPx : kOONullPixMap;
333}
#define FAIL(s)
#define OOLogGenericParameterError()
Definition OOLogging.h:137
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
OOPixMap OOAllocatePixMap(OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:66
OOINLINE unsigned short OOPixMapBytesPerPixel(OOPixMap pixMap)
Definition OOPixMap.h:118
OOPixMap OOMakePixMap(void *pixels, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, size_t rowBytes, size_t bufferSize)
Definition OOPixMap.m:44
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
#define DUMP_SCALE_PREPARE()
#define DUMP_SCALE_DUMP(PM, stage)
static BOOL EnsureCorrectDataSize(OOPixMap *pixMap, BOOL leaveSpaceForMipMaps) NONNULL_FUNC
OOINLINE void StretchVertically(OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
OOINLINE void StretchHorizontally(OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
OOINLINE void SqueezeVertically(OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
OOINLINE void SqueezeHorizontally(OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44
OOPixMapDimension width
Definition OOPixMap.h:45

References DUMP_SCALE_DUMP, DUMP_SCALE_PREPARE, EnsureCorrectDataSize(), EXPECT_NOT, FAIL, OOPixMap::format, OOPixMap::height, kOONullPixMap, OOAllocatePixMap(), OOIsValidPixMap(), OOLogGenericParameterError, OOMakePixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, SqueezeHorizontally(), SqueezeVertically(), StretchHorizontally(), StretchVertically(), and OOPixMap::width.

Referenced by OOTextureLoader(OOPrivate)::applySettings, OOConvertCubeMapToLatLong(), and ScaleToMatch().

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