Oolite 1.91.0.7645-241119-222d325
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 343 of file OOTextureScaling.m.

344{
345 if (EXPECT_NOT(width != OORoundUpToPowerOf2_PixMap(width) || height != OORoundUpToPowerOf2_PixMap(height)))
346 {
347 OOLog(kOOLogParameterError, @"Non-power-of-two dimensions (%ux%u) passed to %s() - ignoring, data will be junk.", width, height, __PRETTY_FUNCTION__);
348 return NO;
349 }
350 if (EXPECT_NOT(textureBytes == NULL))
351 {
352 OOLog(kOOLogParameterError, @"%@", @"NULL texture pointer passed to GenerateMipMaps().");
353 return NO;
354 }
355
356 switch (format)
357 {
358 case kOOPixMapRGBA:
359 return GenerateMipMaps4(textureBytes, width, height);
360
362 return GenerateMipMaps1(textureBytes, width, height);
363
365 return GenerateMipMaps2(textureBytes, width, height);
366
368 break;
369 }
370
371
372 OOLog(kOOLogParameterError, @"%s(): bad pixmap format (%@) - ignoring, data will be junk.", __PRETTY_FUNCTION__, OOPixMapFormatName(format));
373 return NO;
374}
#define EXPECT_NOT(x)
#define OOLog(class, format,...)
Definition OOLogging.h:88
NSString *const kOOLogParameterError
Definition OOLogging.m:647
NSString * OOPixMapFormatName(OOPixMapFormat format) PURE_FUNC
Definition OOPixMap.m:217
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:40
@ kOOPixMapGrayscale
Definition OOPixMap.h:41
@ kOOPixMapRGBA
Definition OOPixMap.h:43
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:42
#define OORoundUpToPowerOf2_PixMap
Definition OOPixMap.h:35
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.

+ Here is the call graph for this function:

◆ OOScalePixMap()

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

Definition at line 245 of file OOTextureScaling.m.

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

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 OOCombinedEmissionMapGenerator::anisotropy, and OOConvertCubeMapToLatLong().

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