Oolite
Loading...
Searching...
No Matches
OOTextureScaling.m File Reference
import "OOTextureScaling.h"
import "OOCPUInfo.h"
import "OOFunctionAttributes.h"
import "OOLogging.h"
import "OOMaths.h"
#include <stdlib.h>
Include dependency graph for OOTextureScaling.m:

Go to the source code of this file.

Macros

#define DUMP_MIP_MAPS   0
#define DUMP_SCALE   0
#define DUMP_MIP_MAP_PREPARE(pl)
#define DUMP_MIP_MAP_DUMP(px, w, h)
#define DUMP_SCALE_PREPARE()
#define DUMP_SCALE_DUMP(PM, stage)
#define ACCUM2(PX, WT)
#define CLEAR_ACCUM2()
#define ACCUM2TOPX()
#define ACCUM4(PX, WT)
#define CLEAR_ACCUM4()
#define ACCUM4TOPX()

Functions

static BOOL GenerateMipMaps1 (void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static BOOL GenerateMipMaps2 (void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static BOOL GenerateMipMaps4 (void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static void ScaleToHalf_1_x1 (void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
static void ScaleToHalf_2_x1 (void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
static void ScaleToHalf_4_x1 (void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
static void ScaleToHalf_1_x4 (void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
OOINLINE void StretchVertically (OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
OOINLINE void SqueezeVertically (OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
OOINLINE void StretchHorizontally (OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
OOINLINE void SqueezeHorizontally (OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
static void StretchVerticallyN_x1 (OOPixMap srcPx, OOPixMap dstPx)
static void SqueezeVertically1 (OOPixMap srcPx, OOPixMapDimension dstHeight)
static void SqueezeVertically2 (OOPixMap srcPx, OOPixMapDimension dstHeight)
static void SqueezeVertically4 (OOPixMap srcPx, OOPixMapDimension dstHeight)
static void StretchHorizontally1 (OOPixMap srcPx, OOPixMap dstPx)
static void StretchHorizontally2 (OOPixMap srcPx, OOPixMap dstPx)
static void StretchHorizontally4 (OOPixMap srcPx, OOPixMap dstPx)
static void SqueezeHorizontally1 (OOPixMap srcPx, OOPixMapDimension dstWidth)
static void SqueezeHorizontally2 (OOPixMap srcPx, OOPixMapDimension dstWidth)
static void SqueezeHorizontally4 (OOPixMap srcPx, OOPixMapDimension dstWidth)
static BOOL EnsureCorrectDataSize (OOPixMap *pixMap, BOOL leaveSpaceForMipMaps) NONNULL_FUNC
static void StretchVerticallyN_x4 (OOPixMap srcPx, OOPixMap dstPx)
OOPixMap OOScalePixMap (OOPixMap srcPx, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
BOOL OOGenerateMipMaps (void *textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)

Macro Definition Documentation

◆ ACCUM2

#define ACCUM2 ( PX,
WT )
Value:
do { \
uint16_t px = PX; \
uint32_t wt = WT; \
accumHi += (px & 0xFF00) * wt; \
accumLo += (px & 0x00FF) * wt; \
weight += wt; \
} while (0)

Definition at line 1264 of file OOTextureScaling.m.

1264#define ACCUM2(PX, WT) \
1265 do { \
1266 uint16_t px = PX; \
1267 uint32_t wt = WT; \
1268 accumHi += (px & 0xFF00) * wt; \
1269 accumLo += (px & 0x00FF) * wt; \
1270 weight += wt; \
1271 } while (0)

Referenced by SqueezeHorizontally2(), and SqueezeVertically2().

◆ ACCUM2TOPX

#define ACCUM2TOPX ( )
Value:
( \
((accumHi / weight) & 0xFF00) | ((accumLo / weight) & 0x00FF))

Definition at line 1280 of file OOTextureScaling.m.

1280#define ACCUM2TOPX() ( \
1281 ((accumHi / weight) & 0xFF00) | ((accumLo / weight) & 0x00FF))

Referenced by SqueezeHorizontally2(), and SqueezeVertically2().

◆ ACCUM4

#define ACCUM4 ( PX,
WT )
Value:
do { \
uint32_t px = PX; \
uint32_t wt = WT; \
ag = ((px & 0xFF00FF00) >> 8) * wt; \
br = (px & 0x00FF00FF) * wt; \
accum1 += ag >> 16; \
accum2 += br >> 16; \
accum3 += ag & 0xFFFF; \
accum4 += br & 0xFFFF; \
weight += wt; \
} while (0)

Definition at line 1402 of file OOTextureScaling.m.

1402#define ACCUM4(PX, WT) \
1403 do { \
1404 uint32_t px = PX; \
1405 uint32_t wt = WT; \
1406 ag = ((px & 0xFF00FF00) >> 8) * wt; \
1407 br = (px & 0x00FF00FF) * wt; \
1408 accum1 += ag >> 16; \
1409 accum2 += br >> 16; \
1410 accum3 += ag & 0xFFFF; \
1411 accum4 += br & 0xFFFF; \
1412 weight += wt; \
1413 } while (0)

Referenced by SqueezeHorizontally4(), and SqueezeVertically4().

◆ ACCUM4TOPX

#define ACCUM4TOPX ( )
Value:
( \
(((accum1 / weight) & 0xFF) << 24) | (((accum3 / weight) & 0xFF) << 8) | (((accum2 / weight) & 0xFF) << 16) | ((accum4 / weight) & 0xFF))

Definition at line 1430 of file OOTextureScaling.m.

1430#define ACCUM4TOPX() ( \
1431 (((accum1 / weight) & 0xFF) << 24) | (((accum3 / weight) & 0xFF) << 8) | (((accum2 / weight) & 0xFF) << 16) | ((accum4 / weight) & 0xFF))

Referenced by SqueezeHorizontally4(), and SqueezeVertically4().

◆ CLEAR_ACCUM2

#define CLEAR_ACCUM2 ( )
Value:
do { \
accumHi = 0; \
accumLo = 0; \
weight = 0; \
} while (0)

Definition at line 1273 of file OOTextureScaling.m.

1273#define CLEAR_ACCUM2() \
1274 do { \
1275 accumHi = 0; \
1276 accumLo = 0; \
1277 weight = 0; \
1278 } while (0)

Referenced by SqueezeHorizontally2(), and SqueezeVertically2().

◆ CLEAR_ACCUM4

#define CLEAR_ACCUM4 ( )
Value:
do { \
accum1 = 0; \
accum2 = 0; \
accum3 = 0; \
accum4 = 0; \
weight = 0; \
} while (0)

Definition at line 1415 of file OOTextureScaling.m.

1415#define CLEAR_ACCUM4() \
1416 do { \
1417 accum1 = 0; \
1418 accum2 = 0; \
1419 accum3 = 0; \
1420 accum4 = 0; \
1421 weight = 0; \
1422 } while (0)

Referenced by SqueezeHorizontally4(), and SqueezeVertically4().

◆ DUMP_MIP_MAP_DUMP

#define DUMP_MIP_MAP_DUMP ( px,
w,
h )
Value:
do { \
(void)px; \
(void)w; \
(void)h; \
} while (0)

Definition at line 222 of file OOTextureScaling.m.

222#define DUMP_MIP_MAP_DUMP(px, w, h) \
223 do { \
224 (void)px; \
225 (void)w; \
226 (void)h; \
227 } while (0)

Referenced by GenerateMipMaps1(), GenerateMipMaps2(), and GenerateMipMaps4().

◆ DUMP_MIP_MAP_PREPARE

#define DUMP_MIP_MAP_PREPARE ( pl)
Value:
do { \
(void)pl; \
} while (0)

Definition at line 218 of file OOTextureScaling.m.

218#define DUMP_MIP_MAP_PREPARE(pl) \
219 do { \
220 (void)pl; \
221 } while (0)

Referenced by GenerateMipMaps1(), GenerateMipMaps2(), and GenerateMipMaps4().

◆ DUMP_MIP_MAPS

#define DUMP_MIP_MAPS   0

Definition at line 34 of file OOTextureScaling.m.

◆ DUMP_SCALE

#define DUMP_SCALE   0

Definition at line 35 of file OOTextureScaling.m.

◆ DUMP_SCALE_DUMP

#define DUMP_SCALE_DUMP ( PM,
stage )
Value:
do { \
} while (0)

Definition at line 239 of file OOTextureScaling.m.

239#define DUMP_SCALE_DUMP(PM, stage) \
240 do { \
241 } while (0)

Referenced by OOScalePixMap().

◆ DUMP_SCALE_PREPARE

#define DUMP_SCALE_PREPARE ( )

Definition at line 238 of file OOTextureScaling.m.

Referenced by OOScalePixMap().

Function Documentation

◆ EnsureCorrectDataSize()

BOOL EnsureCorrectDataSize ( OOPixMap * pixMap,
BOOL leaveSpaceForMipMaps )
static

Definition at line 1548 of file OOTextureScaling.m.

1549{
1550 size_t correctSize;
1551 void* bytes = NULL;
1552
1553 correctSize = pixMap->rowBytes * pixMap->height;
1554
1555 // correctSize > 0 check is redundant, but static analyzer (checker-262) doesn't know that. -- Ahruman 2012-03-17
1556 NSCParameterAssert(OOIsValidPixMap(*pixMap) && correctSize > 0);
1557
1558 /* Ensure that the block is not too small. This needs to be done before
1559 adding the mip-map space, as the texture may have been shrunk in place
1560 without being grown for mip-maps.
1561 */
1562 if (EXPECT_NOT(pixMap->bufferSize < correctSize)) {
1564 return NO;
1565 }
1566
1567 if (leaveSpaceForMipMaps)
1568 correctSize = correctSize * 4 / 3;
1569 if (correctSize != pixMap->bufferSize) {
1570 bytes = realloc(pixMap->pixels, correctSize);
1571 if (EXPECT_NOT(bytes == NULL))
1572 free(pixMap->pixels);
1573 pixMap->pixels = bytes;
1574 pixMap->bufferSize = correctSize;
1575 }
1576
1577 return YES;
1578}
#define EXPECT_NOT(x)
#define OOLogGenericParameterError()
Definition OOLogging.h:137
BOOL OOIsValidPixMap(OOPixMap pixMap)
Definition OOPixMap.m:39
OOPixMapDimension height
Definition OOPixMap.h:45
size_t bufferSize
Definition OOPixMap.h:48
size_t rowBytes
Definition OOPixMap.h:47
void * pixels
Definition OOPixMap.h:44

References OOPixMap::bufferSize, EXPECT_NOT, OOPixMap::height, OOIsValidPixMap(), OOLogGenericParameterError, OOPixMap::pixels, and OOPixMap::rowBytes.

Referenced by OOScalePixMap().

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

◆ GenerateMipMaps1()

BOOL GenerateMipMaps1 ( void * textureBytes,
OOPixMapDimension width,
OOPixMapDimension height )
static

Definition at line 365 of file OOTextureScaling.m.

366{
367 OOPixMapDimension w = width, h = height;
368 uint8_t *curr, *next;
369
371 curr = textureBytes;
372
373#if OOLITE_NATIVE_64_BIT
374 while (8 < w && 1 < h) {
375 DUMP_MIP_MAP_DUMP(curr, w, h);
376
377 next = curr + w * h;
378 ScaleToHalf_1_x8(curr, next, w, h);
379
380 w >>= 1;
381 h >>= 1;
382 curr = next;
383 }
384#else
385 while (4 < w && 1 < h) {
386 DUMP_MIP_MAP_DUMP(curr, w, h);
387
388 next = curr + w * h;
389 ScaleToHalf_1_x4(curr, next, w, h);
390
391 w >>= 1;
392 h >>= 1;
393 curr = next;
394 }
395#endif
396
397 while (1 < w && 1 < h) {
398 DUMP_MIP_MAP_DUMP(curr, w, h);
399
400 next = curr + w * h;
401 ScaleToHalf_1_x1(curr, next, w, h);
402
403 w >>= 1;
404 h >>= 1;
405 curr = next;
406 }
407
408 DUMP_MIP_MAP_DUMP(curr, w, h);
409
410 // TODO: handle residual 1xN/Nx1 mips. For now, we just limit maximum mip level for non-square textures.
411 return YES;
412}
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
#define DUMP_MIP_MAP_DUMP(px, w, h)
#define DUMP_MIP_MAP_PREPARE(pl)
static void ScaleToHalf_1_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
static void ScaleToHalf_1_x4(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC

References DUMP_MIP_MAP_DUMP, DUMP_MIP_MAP_PREPARE, ScaleToHalf_1_x1(), and ScaleToHalf_1_x4().

Referenced by OOGenerateMipMaps().

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

◆ GenerateMipMaps2()

BOOL GenerateMipMaps2 ( void * textureBytes,
OOPixMapDimension width,
OOPixMapDimension height )
static

Definition at line 551 of file OOTextureScaling.m.

552{
553 OOPixMapDimension w = width, h = height;
554 uint16_t *curr, *next;
555
557 curr = textureBytes;
558
559 // TODO: multiple pixel two-plane scalers.
560#if 0
561#if OOLITE_NATIVE_64_BIT
562 while (4 < w && 1 < h)
563 {
564 DUMP_MIP_MAP_DUMP(curr, w, h);
565
566 next = curr + w * h;
567 ScaleToHalf_2_x4(curr, next, w, h);
568
569 w >>= 1;
570 h >>= 1;
571 curr = next;
572 }
573#else
574 while (2 < w && 1 < h)
575 {
576 DUMP_MIP_MAP_DUMP(curr, w, h);
577
578 next = curr + w * h;
579 ScaleToHalf_2_x2(curr, next, w, h);
580
581 w >>= 1;
582 h >>= 1;
583 curr = next;
584 }
585#endif
586#endif
587
588 while (1 < w && 1 < h) {
589 DUMP_MIP_MAP_DUMP(curr, w, h);
590
591 next = curr + w * h;
592 ScaleToHalf_2_x1(curr, next, w, h);
593
594 w >>= 1;
595 h >>= 1;
596 curr = next;
597 }
598
599 DUMP_MIP_MAP_DUMP(curr, w, h);
600
601 // TODO: handle residual 1xN/Nx1 mips. For now, we just limit maximum mip level for non-square textures.
602 return YES;
603}
static void ScaleToHalf_2_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC

References DUMP_MIP_MAP_DUMP, DUMP_MIP_MAP_PREPARE, and ScaleToHalf_2_x1().

Referenced by OOGenerateMipMaps().

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

◆ GenerateMipMaps4()

BOOL GenerateMipMaps4 ( void * textureBytes,
OOPixMapDimension width,
OOPixMapDimension height )
static

Definition at line 643 of file OOTextureScaling.m.

644{
645 OOPixMapDimension w = width, h = height;
646 uint32_t *curr, *next;
647
649 curr = textureBytes;
650
651#if OOLITE_NATIVE_64_BIT
652 while (2 < w && 1 < h) {
653 DUMP_MIP_MAP_DUMP(curr, w, h);
654
655 next = curr + w * h;
656 ScaleToHalf_4_x2(curr, next, w, h);
657
658 w >>= 1;
659 h >>= 1;
660 curr = next;
661 }
662 if (EXPECT(1 < w && 1 < h)) {
663 DUMP_MIP_MAP_DUMP(curr, w, h);
664
665 next = curr + w * h;
666 ScaleToHalf_4_x1(curr, next, w, h);
667
668 w >>= 1;
669 h >>= 1;
670 }
671#else
672 while (1 < w && 1 < h) {
673 DUMP_MIP_MAP_DUMP(curr, w, h);
674
675 next = curr + w * h;
676 ScaleToHalf_4_x1(curr, next, w, h);
677
678 w >>= 1;
679 h >>= 1;
680 curr = next;
681 }
682#endif
683
684 DUMP_MIP_MAP_DUMP(curr, w, h);
685
686 // TODO: handle residual 1xN/Nx1 mips. For now, we just limit maximum mip level for non-square textures.
687 return YES;
688}
#define EXPECT(x)
static void ScaleToHalf_4_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC

References DUMP_MIP_MAP_DUMP, DUMP_MIP_MAP_PREPARE, EXPECT, and ScaleToHalf_4_x1().

Referenced by OOGenerateMipMaps().

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

◆ 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 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 srcPx,
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)
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
#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
OOPixMapDimension width
Definition OOPixMap.h:45
OOPixMapFormat format
Definition OOPixMap.h:46

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:

◆ ScaleToHalf_1_x1()

void ScaleToHalf_1_x1 ( void * srcBytes,
void * dstBytes,
OOPixMapDimension srcWidth,
OOPixMapDimension srcHeight )
static

Definition at line 414 of file OOTextureScaling.m.

415{
417 uint8_t *src0, *src1, *dst;
418 uint8_t px00, px01, px10, px11;
419 uint16_t sum;
420
421 src0 = srcBytes;
422 src1 = src0 + srcWidth;
423 dst = dstBytes;
424
425 y = srcHeight >> 1;
426 do {
427 x = srcWidth >> 1;
428 do {
429 // Read four pixels in a square...
430 px00 = *src0++;
431 px01 = *src0++;
432 px10 = *src1++;
433 px11 = *src1++;
434
435 // ...add them together...
436 sum = px00 + px01 + px10 + px11;
437
438 // ...shift the sum into place...
439 sum >>= 2;
440
441 // ...and write output pixel.
442 *dst++ = sum;
443 } while (--x);
444
445 // Skip a row for each source row
446 src0 = src1;
447 src1 += srcWidth;
448 } while (--y);
449}
float y
float x

References x, and y.

Referenced by GenerateMipMaps1().

Here is the caller graph for this function:

◆ ScaleToHalf_1_x4()

void ScaleToHalf_1_x4 ( void * srcBytes,
void * dstBytes,
OOPixMapDimension srcWidth,
OOPixMapDimension srcHeight )
static

Definition at line 453 of file OOTextureScaling.m.

454{
456 uint32_t *src0, *src1, *dst;
457 uint32_t px00, px01, px10, px11;
458 uint32_t sum0, sum1;
459
460 srcWidth >>= 2; // Four (output) pixels at a time
461 src0 = srcBytes;
462 src1 = src0 + srcWidth;
463 dst = dstBytes;
464
465 y = srcHeight >> 1;
466 do {
467 x = srcWidth >> 1;
468 do {
469 // Read 8 pixels in a 4x2 rectangle...
470 px00 = *src0++;
471 px01 = *src0++;
472 px10 = *src1++;
473 px11 = *src1++;
474
475 // ...add them together.
476 sum0 = (px00 & 0x00FF00FF) + (px10 & 0x00FF00FF) + ((px00 & 0xFF00FF00) >> 8) + ((px10 & 0xFF00FF00) >> 8);
477 sum1 = (px01 & 0x00FF00FF) + (px11 & 0x00FF00FF) + ((px01 & 0xFF00FF00) >> 8) + ((px11 & 0xFF00FF00) >> 8);
478
479 // ...swizzle the sums around...
480#if OOLITE_BIG_ENDIAN
481 sum0 = ((sum0 << 6) & 0xFF000000) | ((sum0 << 14) & 0x00FF0000);
482 sum1 = ((sum1 >> 10) & 0x0000FF00) | ((sum1 >> 2) & 0x000000FF);
483#elif OOLITE_LITTLE_ENDIAN
484 sum0 = ((sum0 >> 10) & 0x0000FF00) | ((sum0 >> 2) & 0x000000FF);
485 sum1 = ((sum1 << 6) & 0xFF000000) | ((sum1 << 14) & 0x00FF0000);
486#else
487#error Neither OOLITE_BIG_ENDIAN nor OOLITE_LITTLE_ENDIAN is defined as nonzero!
488#endif
489
490 // ...and write output pixel.
491 *dst++ = sum0 | sum1;
492 } while (--x);
493
494 // Skip a row for each source row
495 src0 = src1;
496 src1 += srcWidth;
497 } while (--y);
498}

References x, and y.

Referenced by GenerateMipMaps1().

Here is the caller graph for this function:

◆ ScaleToHalf_2_x1()

void ScaleToHalf_2_x1 ( void * srcBytes,
void * dstBytes,
OOPixMapDimension srcWidth,
OOPixMapDimension srcHeight )
static

Definition at line 605 of file OOTextureScaling.m.

606{
608 uint16_t *src0, *src1, *dst;
609 uint16_t px00, px01, px10, px11;
610 uint32_t sumHi, sumLo;
611
612 src0 = srcBytes;
613 src1 = src0 + srcWidth;
614 dst = dstBytes;
615
616 y = srcHeight >> 1;
617 do {
618 x = srcWidth >> 1;
619 do {
620 // Read four pixels in a square...
621 px00 = *src0++;
622 px01 = *src0++;
623 px10 = *src1++;
624 px11 = *src1++;
625
626 // ...add them together...
627 sumHi = (px00 & 0xFF00) + (px01 & 0xFF00) + (px10 & 0xFF00) + (px11 & 0xFF00);
628 sumLo = (px00 & 0x00FF) + (px01 & 0x00FF) + (px10 & 0x00FF) + (px11 & 0x00FF);
629
630 // ...merge and shift the sum into place...
631 sumLo = ((sumHi & 0x3FC00) | sumLo) >> 2;
632
633 // ...and write output pixel.
634 *dst++ = sumLo;
635 } while (--x);
636
637 // Skip a row for each source row
638 src0 = src1;
639 src1 += srcWidth;
640 } while (--y);
641}

References x, and y.

Referenced by GenerateMipMaps2().

Here is the caller graph for this function:

◆ ScaleToHalf_4_x1()

void ScaleToHalf_4_x1 ( void * srcBytes,
void * dstBytes,
OOPixMapDimension srcWidth,
OOPixMapDimension srcHeight )
static

Definition at line 690 of file OOTextureScaling.m.

691{
693 uint32_t *src0, *src1, *dst;
694 uint32_t px00, px01, px10, px11;
695
696 /* We treat channel layout as ABGR -- actual layout doesn't matter since
697 each channel is handled the same. We use two accumulators, with
698 alternating channels, so overflow doesn't cross channel boundaries,
699 while having less overhead than one accumulator per channel.
700 */
701 uint32_t ag, br;
702
703 src0 = srcBytes;
704 src1 = src0 + srcWidth;
705 dst = dstBytes;
706
707 y = srcHeight >> 1;
708 do {
709 x = srcWidth >> 1;
710 do {
711 // Read four pixels in a square...
712 px00 = *src0++;
713 px01 = *src0++;
714 px10 = *src1++;
715 px11 = *src1++;
716
717 // ...and add them together, channel by channel.
718 ag = (px00 & 0xFF00FF00) >> 8;
719 br = (px00 & 0x00FF00FF);
720 ag += (px01 & 0xFF00FF00) >> 8;
721 br += (px01 & 0x00FF00FF);
722 ag += (px10 & 0xFF00FF00) >> 8;
723 br += (px10 & 0x00FF00FF);
724 ag += (px11 & 0xFF00FF00) >> 8;
725 br += (px11 & 0x00FF00FF);
726
727 // Shift the sums into place...
728 ag <<= 6;
729 br >>= 2;
730
731 // ...and write output pixel.
732 *dst++ = (ag & 0xFF00FF00) | (br & 0x00FF00FF);
733 } while (--x);
734
735 // Skip a row for each source row
736 src0 = src1;
737 src1 += srcWidth;
738 } while (--y);
739}

References x, and y.

Referenced by GenerateMipMaps4().

Here is the caller graph for this function:

◆ SqueezeHorizontally()

OOINLINE void SqueezeHorizontally ( OOPixMap pixMap,
OOPixMapDimension dstHeight )

Definition at line 171 of file OOTextureScaling.m.

172{
173 switch (pixMap.format) {
174 case kOOPixMapRGBA:
175 SqueezeHorizontally4(pixMap, dstHeight);
176 return;
177
179 SqueezeHorizontally1(pixMap, dstHeight);
180 return;
181
183 SqueezeHorizontally2(pixMap, dstHeight);
184 return;
185
187 break;
188 }
189
190#ifndef NDEBUG
191 [NSException raise:NSInternalInconsistencyException
192 format:@"Unsupported pixmap format in scaler: %@", OOPixMapFormatName(pixMap.format)];
193#else
194 abort();
195#endif
196}
static void SqueezeHorizontally2(OOPixMap srcPx, OOPixMapDimension dstWidth)
static void SqueezeHorizontally1(OOPixMap srcPx, OOPixMapDimension dstWidth)
static void SqueezeHorizontally4(OOPixMap srcPx, OOPixMapDimension dstWidth)

References OOPixMap::format, kOOPixMapGrayscale, kOOPixMapGrayscaleAlpha, kOOPixMapInvalidFormat, kOOPixMapRGBA, OOINLINE, SqueezeHorizontally1(), SqueezeHorizontally2(), and SqueezeHorizontally4().

Referenced by OOScalePixMap().

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

◆ SqueezeHorizontally1()

void SqueezeHorizontally1 ( OOPixMap srcPx,
OOPixMapDimension dstWidth )
static

Definition at line 1144 of file OOTextureScaling.m.

1145{
1146 uint8_t *src, *srcStart, *dst;
1147 uint8_t borderPx;
1148 uint32_t x, y, xCount, endX;
1149 size_t srcRowBytes;
1150 uint32_t endFractX, deltaX;
1151 uint32_t accum, weight;
1152 uint8_t borderWeight;
1153
1154 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 1);
1155
1156 srcStart = srcPx.pixels;
1157 dst = srcStart; // Output is placed in same buffer, without line padding.
1158 srcRowBytes = srcPx.rowBytes;
1159
1160 deltaX = (srcPx.width << 12) / dstWidth;
1161
1162 for (y = 0; y != srcPx.height; ++y) {
1163 borderPx = *srcStart;
1164 endFractX = 0;
1165 borderWeight = 0;
1166
1167 src = srcStart;
1168
1169 x = 0;
1170 xCount = dstWidth;
1171 while (xCount--) {
1172 endFractX += deltaX;
1173 endX = endFractX >> 12;
1174
1175 borderWeight = 0xFF - borderWeight;
1176 accum = borderPx * borderWeight;
1177 weight = borderWeight;
1178
1179 borderWeight = (endFractX >> 4) & 0xFF;
1180 weight += borderWeight;
1181
1182 for (;;) {
1183 ++x;
1184 if (EXPECT(x == endX)) {
1185 if (EXPECT(xCount))
1186 borderPx = *++src;
1187 accum += borderPx * borderWeight;
1188 break;
1189 } else {
1190 accum += *++src * 0xFF;
1191 weight += 0xFF;
1192 }
1193 }
1194
1195 *dst++ = accum / weight;
1196 }
1197
1198 srcStart = (uint8_t*)((char*)srcStart + srcRowBytes);
1199 }
1200}

References EXPECT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeHorizontally().

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

◆ SqueezeHorizontally2()

void SqueezeHorizontally2 ( OOPixMap srcPx,
OOPixMapDimension dstWidth )
static

Definition at line 1283 of file OOTextureScaling.m.

1284{
1285 uint16_t *src, *srcStart, *dst;
1286 uint16_t borderPx;
1287 uint32_t x, y, xCount, endX;
1288 size_t srcRowBytes;
1289 uint32_t endFractX, deltaX;
1290 uint32_t accumHi, accumLo, weight;
1291 uint8_t borderWeight;
1292
1293 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 2);
1294
1295 srcStart = srcPx.pixels;
1296 dst = srcStart; // Output is placed in same buffer, without line padding.
1297 srcRowBytes = srcPx.rowBytes;
1298
1299 deltaX = (srcPx.width << 12) / dstWidth;
1300
1301 for (y = 0; y != srcPx.height; ++y) {
1302 borderPx = *srcStart;
1303 endFractX = 0;
1304 borderWeight = 0;
1305
1306 src = srcStart;
1307
1308 x = 0;
1309 xCount = dstWidth;
1310 while (xCount--) {
1311 endFractX += deltaX;
1312 endX = endFractX >> 12;
1313
1314 CLEAR_ACCUM2();
1315
1316 borderWeight = 0xFF - borderWeight;
1317 ACCUM2(borderPx, borderWeight);
1318
1319 borderWeight = (endFractX >> 4) & 0xFF;
1320
1321 for (;;) {
1322 ++x;
1323 if (EXPECT(x == endX)) {
1324 if (EXPECT(xCount))
1325 borderPx = *++src;
1326 ACCUM2(borderPx, borderWeight);
1327 break;
1328 } else {
1329 ACCUM2(*++src, 0xFF);
1330 }
1331 }
1332
1333 *dst++ = ACCUM2TOPX();
1334 }
1335
1336 srcStart = (uint16_t*)((char*)srcStart + srcRowBytes);
1337 }
1338}
#define ACCUM2(PX, WT)
#define ACCUM2TOPX()
#define CLEAR_ACCUM2()

References ACCUM2, ACCUM2TOPX, CLEAR_ACCUM2, EXPECT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeHorizontally().

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

◆ SqueezeHorizontally4()

void SqueezeHorizontally4 ( OOPixMap srcPx,
OOPixMapDimension dstWidth )
static

Definition at line 1433 of file OOTextureScaling.m.

1434{
1435 uint32_t *src, *srcStart, *dst;
1436 uint32_t borderPx, ag, br;
1437 uint32_t x, y, xCount, endX;
1438 size_t srcRowBytes;
1439 uint32_t endFractX, deltaX;
1440 uint32_t accum1, accum2, accum3, accum4, weight;
1441 uint8_t borderWeight;
1442
1443 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 4);
1444
1445 srcStart = srcPx.pixels;
1446 dst = srcStart; // Output is placed in same buffer, without line padding.
1447 srcRowBytes = srcPx.rowBytes;
1448
1449 deltaX = (srcPx.width << 12) / dstWidth;
1450
1451 for (y = 0; y != srcPx.height; ++y) {
1452 borderPx = *srcStart;
1453 endFractX = 0;
1454 borderWeight = 0;
1455
1456 src = srcStart;
1457
1458 x = 0;
1459 xCount = dstWidth;
1460 while (xCount--) {
1461 endFractX += deltaX;
1462 endX = endFractX >> 12;
1463
1464 CLEAR_ACCUM4();
1465
1466 borderWeight = 0xFF - borderWeight;
1467 ACCUM4(borderPx, borderWeight);
1468
1469 borderWeight = (endFractX >> 4) & 0xFF;
1470
1471 for (;;) {
1472 ++x;
1473 if (EXPECT(x == endX)) {
1474 if (EXPECT(xCount))
1475 borderPx = *++src;
1476 ACCUM4(borderPx, borderWeight);
1477 break;
1478 } else {
1479 ACCUM4(*++src, 0xFF);
1480 }
1481 }
1482
1483 *dst++ = ACCUM4TOPX();
1484 }
1485
1486 srcStart = (uint32_t*)((char*)srcStart + srcRowBytes);
1487 }
1488}
#define ACCUM4(PX, WT)
#define ACCUM4TOPX()
#define CLEAR_ACCUM4()

References ACCUM4, ACCUM4TOPX, CLEAR_ACCUM4, EXPECT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeHorizontally().

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

◆ SqueezeVertically()

OOINLINE void SqueezeVertically ( OOPixMap pixMap,
OOPixMapDimension dstHeight )

Definition at line 115 of file OOTextureScaling.m.

116{
117 switch (pixMap.format) {
118 case kOOPixMapRGBA:
119 SqueezeVertically4(pixMap, dstHeight);
120 return;
121
123 SqueezeVertically1(pixMap, dstHeight);
124 return;
125
127 SqueezeVertically2(pixMap, dstHeight);
128 return;
129
131 break;
132 }
133
134#ifndef NDEBUG
135 [NSException raise:NSInternalInconsistencyException
136 format:@"Unsupported pixmap format in scaler: %@", OOPixMapFormatName(pixMap.format)];
137#else
138 abort();
139#endif
140}
static void SqueezeVertically1(OOPixMap srcPx, OOPixMapDimension dstHeight)
static void SqueezeVertically4(OOPixMap srcPx, OOPixMapDimension dstHeight)
static void SqueezeVertically2(OOPixMap srcPx, OOPixMapDimension dstHeight)

References OOPixMap::format, kOOPixMapGrayscale, kOOPixMapGrayscaleAlpha, kOOPixMapInvalidFormat, kOOPixMapRGBA, OOINLINE, SqueezeVertically1(), SqueezeVertically2(), and SqueezeVertically4().

Referenced by OOScalePixMap().

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

◆ SqueezeVertically1()

void SqueezeVertically1 ( OOPixMap srcPx,
OOPixMapDimension dstHeight )
static

Definition at line 1202 of file OOTextureScaling.m.

1203{
1204 uint8_t *src, *srcStart, *dst;
1205 uint32_t x, y, xCount, startY, endY, lastRow;
1206 size_t srcRowBytes;
1207 uint32_t endFractY, deltaY;
1208 uint32_t accum, weight;
1209 uint8_t startWeight, endWeight;
1210
1211 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 1);
1212
1213 dst = srcPx.pixels; // Output is placed in same buffer, without line padding.
1214 srcRowBytes = srcPx.rowBytes;
1215 xCount = srcPx.width;
1216
1217 deltaY = (srcPx.height << 12) / dstHeight;
1218 endFractY = 0;
1219
1220 endWeight = 0;
1221 endY = 0;
1222
1223 lastRow = srcPx.height - 1;
1224
1225 while (endY < lastRow) {
1226 endFractY += deltaY;
1227 startY = endY;
1228 endY = endFractY >> 12;
1229
1230 startWeight = 0xFF - endWeight;
1231 endWeight = (endFractY >> 4) & 0xFF;
1232
1233 srcStart = (uint8_t*)((char*)srcPx.pixels + srcRowBytes * startY);
1234
1235 for (x = 0; x != xCount; ++x) {
1236 src = srcStart++;
1237 accum = startWeight * *src;
1238 weight = startWeight + endWeight;
1239
1240 y = startY;
1241 for (;;) {
1242 ++y;
1243 src = (uint8_t*)((char*)src + srcRowBytes);
1244 if (EXPECT_NOT(y == endY)) {
1245 if (EXPECT(endY < lastRow))
1246 accum += *src * endWeight;
1247 break;
1248 } else {
1249 accum += *src * 0xFF;
1250 weight += 0xFF;
1251 }
1252 }
1253
1254 *dst++ = accum / weight;
1255 }
1256 }
1257}

References EXPECT, EXPECT_NOT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeVertically().

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

◆ SqueezeVertically2()

void SqueezeVertically2 ( OOPixMap srcPx,
OOPixMapDimension dstHeight )
static

Definition at line 1340 of file OOTextureScaling.m.

1341{
1342 uint16_t *src, *srcStart, *dst;
1343 uint32_t x, y, xCount, startY, endY, lastRow;
1344 size_t srcRowBytes;
1345 uint32_t endFractY, deltaY;
1346 uint32_t accumHi, accumLo, weight;
1347 uint8_t startWeight, endWeight;
1348
1349 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 2);
1350
1351 dst = srcPx.pixels; // Output is placed in same buffer, without line padding.
1352 srcRowBytes = srcPx.rowBytes;
1353 xCount = srcPx.width;
1354
1355 deltaY = (srcPx.height << 12) / dstHeight;
1356 endFractY = 0;
1357
1358 endWeight = 0;
1359 endY = 0;
1360
1361 lastRow = srcPx.height - 1;
1362
1363 while (endY < lastRow) {
1364 endFractY += deltaY;
1365 startY = endY;
1366 endY = endFractY >> 12;
1367
1368 startWeight = 0xFF - endWeight;
1369 endWeight = (endFractY >> 4) & 0xFF;
1370
1371 srcStart = (uint16_t*)((char*)srcPx.pixels + srcRowBytes * startY);
1372
1373 for (x = 0; x != xCount; ++x) {
1374 src = srcStart++;
1375
1376 CLEAR_ACCUM2();
1377 ACCUM2(*src, startWeight);
1378
1379 y = startY;
1380 for (;;) {
1381 ++y;
1382 src = (uint16_t*)((char*)src + srcRowBytes);
1383 if (EXPECT_NOT(y == endY)) {
1384 if (EXPECT(endY <= lastRow))
1385 ACCUM2(*src, endWeight);
1386 break;
1387 } else {
1388 ACCUM2(*src, 0xFF);
1389 }
1390 }
1391
1392 *dst++ = ACCUM2TOPX();
1393 }
1394 }
1395}

References ACCUM2, ACCUM2TOPX, CLEAR_ACCUM2, EXPECT, EXPECT_NOT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeVertically().

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

◆ SqueezeVertically4()

void SqueezeVertically4 ( OOPixMap srcPx,
OOPixMapDimension dstHeight )
static

Definition at line 1490 of file OOTextureScaling.m.

1491{
1492 uint32_t *src, *srcStart, *dst;
1493 uint32_t x, y, xCount, startY, endY, lastRow;
1494 size_t srcRowBytes;
1495 uint32_t ag, br;
1496 uint32_t endFractY, deltaY;
1497 uint32_t accum1, accum2, accum3, accum4, weight;
1498 uint8_t startWeight, endWeight;
1499
1500 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 4);
1501
1502 dst = srcPx.pixels; // Output is placed in same buffer, without line padding.
1503 srcRowBytes = srcPx.rowBytes;
1504 xCount = srcPx.width;
1505
1506 deltaY = (srcPx.height << 12) / dstHeight;
1507 endFractY = 0;
1508
1509 endWeight = 0;
1510 endY = 0;
1511
1512 lastRow = srcPx.height - 1;
1513
1514 while (endY < lastRow) {
1515 endFractY += deltaY;
1516 startY = endY;
1517 endY = endFractY >> 12;
1518
1519 startWeight = 0xFF - endWeight;
1520 endWeight = (endFractY >> 4) & 0xFF;
1521
1522 srcStart = (uint32_t*)((char*)srcPx.pixels + srcRowBytes * startY);
1523
1524 for (x = 0; x != xCount; ++x) {
1525 src = srcStart++;
1526
1527 CLEAR_ACCUM4();
1528 ACCUM4(*src, startWeight);
1529
1530 y = startY;
1531 for (;;) {
1532 ++y;
1533 src = (uint32_t*)((char*)src + srcRowBytes);
1534 if (EXPECT_NOT(y == endY)) {
1535 if (EXPECT(endY <= lastRow))
1536 ACCUM4(*src, endWeight);
1537 break;
1538 } else {
1539 ACCUM4(*src, 0xFF);
1540 }
1541 }
1542
1543 *dst++ = ACCUM4TOPX();
1544 }
1545 }
1546}

References ACCUM4, ACCUM4TOPX, CLEAR_ACCUM4, EXPECT, EXPECT_NOT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by SqueezeVertically().

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

◆ StretchHorizontally()

OOINLINE void StretchHorizontally ( OOPixMap srcPx,
OOPixMap dstPx )

Definition at line 142 of file OOTextureScaling.m.

143{
144 NSCParameterAssert(srcPx.format == dstPx.format);
145
146 switch (srcPx.format) {
147 case kOOPixMapRGBA:
148 StretchHorizontally4(srcPx, dstPx);
149 return;
150
152 StretchHorizontally1(srcPx, dstPx);
153 return;
154
156 StretchHorizontally2(srcPx, dstPx);
157 return;
158
160 break;
161 }
162
163#ifndef NDEBUG
164 [NSException raise:NSInternalInconsistencyException
165 format:@"Unsupported pixmap format in scaler: %@", OOPixMapFormatName(srcPx.format)];
166#else
167 abort();
168#endif
169}
static void StretchHorizontally2(OOPixMap srcPx, OOPixMap dstPx)
static void StretchHorizontally1(OOPixMap srcPx, OOPixMap dstPx)
static void StretchHorizontally4(OOPixMap srcPx, OOPixMap dstPx)

References OOPixMap::format, kOOPixMapGrayscale, kOOPixMapGrayscaleAlpha, kOOPixMapInvalidFormat, kOOPixMapRGBA, OOINLINE, StretchHorizontally1(), StretchHorizontally2(), and StretchHorizontally4().

Referenced by OOScalePixMap().

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

◆ StretchHorizontally1()

void StretchHorizontally1 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 964 of file OOTextureScaling.m.

965{
966 uint8_t *src, *srcStart, *dst;
967 uint8_t px0, px1;
968 uint32_t x, y, xCount;
969 size_t srcRowBytes;
970 uint16_t weight0, weight1;
971 uint32_t fractX, deltaX; // X coordinate, fixed-point (20.12), allowing widths up to 1 mebipixel
972
973 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 1 && OOIsValidPixMap(dstPx) && OOPixMapBytesPerPixel(dstPx) == 1);
974
975 srcStart = srcPx.pixels;
976 srcRowBytes = srcPx.rowBytes;
977 xCount = dstPx.width;
978 dst = dstPx.pixels; // Assumes no row padding
979
980 deltaX = (srcPx.width << 12) / dstPx.width;
981 px1 = *srcStart;
982
983 for (y = 0; y < dstPx.height - 1; ++y) {
984 fractX = 0;
985
986 for (x = 0; x != xCount; ++x) {
987 fractX += deltaX;
988
989 weight1 = (fractX >> 4) & 0xFF;
990 weight0 = 0x100 - weight1;
991
992 px0 = px1;
993 src = srcStart + (fractX >> 12);
994 px1 = *src;
995
996 *dst++ = (px0 * weight0 + px1 * weight1) >> 8;
997 }
998
999 srcStart = (uint8_t*)((char*)srcStart + srcRowBytes);
1000 px1 = *srcStart;
1001 }
1002
1003 // Copy last row without reading off end of buffer
1004 fractX = 0;
1005 for (x = 0; x != xCount; ++x) {
1006 fractX += deltaX;
1007
1008 weight1 = (fractX >> 4) & 0xFF;
1009 weight0 = 0x100 - weight1;
1010
1011 px0 = px1;
1012 src = srcStart + (fractX >> 12);
1013 px1 = *src;
1014
1015 *dst++ = (px0 * weight0 + px1 * weight1) >> 8;
1016 }
1017}

References OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by StretchHorizontally().

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

◆ StretchHorizontally2()

void StretchHorizontally2 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 1019 of file OOTextureScaling.m.

1020{
1021 uint16_t *src, *srcStart, *dst;
1022 uint16_t px0, px1;
1023 uint32_t hi, lo;
1024 uint32_t x, y, xCount;
1025 size_t srcRowBytes;
1026 uint16_t weight0, weight1;
1027 uint32_t fractX, deltaX; // X coordinate, fixed-point (20.12), allowing widths up to 1 mebipixel
1028
1029 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 2 && OOIsValidPixMap(dstPx) && OOPixMapBytesPerPixel(dstPx) == 2);
1030
1031 srcStart = srcPx.pixels;
1032 srcRowBytes = srcPx.rowBytes;
1033 xCount = dstPx.width;
1034 dst = dstPx.pixels; // Assumes no row padding
1035
1036 deltaX = (srcPx.width << 12) / dstPx.width;
1037 px1 = *srcStart;
1038
1039 for (y = 0; y < dstPx.height - 1; ++y) {
1040 fractX = 0;
1041
1042 for (x = 0; x != xCount; ++x) {
1043 fractX += deltaX;
1044
1045 weight1 = (fractX >> 4) & 0xFF;
1046 weight0 = 0x100 - weight1;
1047
1048 px0 = px1;
1049 src = srcStart + (fractX >> 12);
1050 px1 = *src;
1051
1052 hi = (px0 & 0xFF00) * weight0 + (px1 & 0xFF00) * weight1;
1053 lo = (px0 & 0x00FF) * weight0 + (px1 & 0x00FF) * weight1;
1054
1055 *dst++ = ((hi & 0xFF0000) | (lo & 0x00FF00)) >> 8;
1056 }
1057
1058 srcStart = (uint16_t*)((char*)srcStart + srcRowBytes);
1059 px1 = *srcStart;
1060 }
1061
1062 // Copy last row without reading off end of buffer
1063 fractX = 0;
1064 for (x = 0; x != xCount; ++x) {
1065 fractX += deltaX;
1066
1067 weight1 = (fractX >> 4) & 0xFF;
1068 weight0 = 0x100 - weight1;
1069
1070 px0 = px1;
1071 src = srcStart + (fractX >> 12);
1072 px1 = *src;
1073
1074 hi = (px0 & 0xFF00) * weight0 + (px1 & 0xFF00) * weight1;
1075 lo = (px0 & 0x00FF) * weight0 + (px1 & 0x00FF) * weight1;
1076
1077 *dst++ = ((hi & 0xFF0000) | (lo & 0x00FF00)) >> 8;
1078 }
1079}

References OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by StretchHorizontally().

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

◆ StretchHorizontally4()

void StretchHorizontally4 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 1081 of file OOTextureScaling.m.

1082{
1083 uint32_t *src, *srcStart, *dst;
1084 uint32_t px0, px1;
1085 uint32_t ag, br;
1086 uint32_t x, y, xCount;
1087 size_t srcRowBytes;
1088 uint16_t weight0, weight1;
1089 uint32_t fractX, deltaX; // X coordinate, fixed-point (20.12), allowing widths up to 1 mebipixel
1090
1091 NSCParameterAssert(OOIsValidPixMap(srcPx) && OOPixMapBytesPerPixel(srcPx) == 4 && OOIsValidPixMap(dstPx) && OOPixMapBytesPerPixel(dstPx) == 4);
1092
1093 srcStart = srcPx.pixels;
1094 srcRowBytes = srcPx.rowBytes;
1095 xCount = dstPx.width;
1096 dst = dstPx.pixels; // Assumes no row padding
1097
1098 deltaX = (srcPx.width << 12) / dstPx.width;
1099 px1 = *srcStart;
1100
1101 for (y = 0; y < dstPx.height - 1; ++y) {
1102 fractX = 0;
1103
1104 for (x = 0; x != xCount; ++x) {
1105 fractX += deltaX;
1106
1107 weight1 = (fractX >> 4) & 0xFF;
1108 weight0 = 0x100 - weight1;
1109
1110 px0 = px1;
1111 src = srcStart + (fractX >> 12);
1112 px1 = *src;
1113
1114 ag = ((px0 & 0xFF00FF00) >> 8) * weight0 + ((px1 & 0xFF00FF00) >> 8) * weight1;
1115 br = (px0 & 0x00FF00FF) * weight0 + (px1 & 0x00FF00FF) * weight1;
1116
1117 *dst++ = (ag & 0xFF00FF00) | ((br & 0xFF00FF00) >> 8);
1118 }
1119
1120 srcStart = (uint32_t*)((char*)srcStart + srcRowBytes);
1121 px1 = *srcStart;
1122 }
1123
1124 // Copy last row without reading off end of buffer
1125 fractX = 0;
1126 for (x = 0; x != xCount; ++x) {
1127 fractX += deltaX;
1128
1129 weight1 = (fractX >> 4) & 0xFF;
1130 weight0 = 0x100 - weight1;
1131
1132 px0 = px1;
1133 src = srcStart + (fractX >> 12);
1134 if (EXPECT(x < xCount - 1))
1135 px1 = *src;
1136
1137 ag = ((px0 & 0xFF00FF00) >> 8) * weight0 + ((px1 & 0xFF00FF00) >> 8) * weight1;
1138 br = (px0 & 0x00FF00FF) * weight0 + (px1 & 0x00FF00FF) * weight1;
1139
1140 *dst++ = (ag & 0xFF00FF00) | ((br & 0xFF00FF00) >> 8);
1141 }
1142}

References EXPECT, OOPixMap::height, OOIsValidPixMap(), OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by StretchHorizontally().

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

◆ StretchVertically()

OOINLINE void StretchVertically ( OOPixMap srcPx,
OOPixMap dstPx )

Definition at line 91 of file OOTextureScaling.m.

92{
93 if (!((srcPx.rowBytes) & 3)) {
94 StretchVerticallyN_x4(srcPx, dstPx);
95 } else {
96 StretchVerticallyN_x1(srcPx, dstPx);
97 }
98}
static void StretchVerticallyN_x1(OOPixMap srcPx, OOPixMap dstPx)
static void StretchVerticallyN_x4(OOPixMap srcPx, OOPixMap dstPx)

References OOINLINE, OOPixMap::rowBytes, StretchVerticallyN_x1(), and StretchVerticallyN_x4().

Referenced by OOScalePixMap().

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

◆ StretchVerticallyN_x1()

void StretchVerticallyN_x1 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 825 of file OOTextureScaling.m.

826{
827 uint8_t *src, *src0, *src1, *prev, *dst;
828 uint8_t px0, px1;
829 uint32_t x, y, xCount;
830 size_t srcRowBytes;
831 uint16_t weight0, weight1;
832 uint32_t fractY; // Y coordinate, fixed-point (24.8)
833
834 src = srcPx.pixels;
835 srcRowBytes = srcPx.rowBytes;
836 dst = dstPx.pixels; // Assumes dstPx.width == dstPx.rowBytes.
837
838 src0 = prev = src;
839
840 xCount = srcPx.width * OOPixMapBytesPerPixel(srcPx);
841
842 for (y = 1; y != dstPx.height; ++y) {
843 fractY = ((srcPx.height * y) << 8) / dstPx.height;
844
845 src0 = prev;
846 prev = src1 = src + srcRowBytes * (fractY >> 8);
847
848 weight1 = fractY & 0xFF;
849 weight0 = 0x100 - weight1;
850
851 x = xCount;
852 while (x--) {
853 px0 = *src0++;
854 px1 = *src1++;
855
856 *dst++ = (px0 * weight0 + px1 * weight1) >> 8;
857 }
858 }
859
860 // Copy last row (without referring to the last-plus-oneth row)
861 x = xCount;
862 while (x--) {
863 *dst++ = *src0++;
864 }
865}

References OOPixMap::height, OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by StretchVertically().

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

◆ StretchVerticallyN_x4()

void StretchVerticallyN_x4 ( OOPixMap srcPx,
OOPixMap dstPx )
static

Definition at line 869 of file OOTextureScaling.m.

870{
871 uint8_t* src;
872 uint32_t *src0, *src1, *prev, *dst;
873 uint32_t px0, px1, ag, br;
874 uint32_t x, y, xCount;
875 size_t srcRowBytes;
876 uint16_t weight0, weight1;
877 uint32_t fractY; // Y coordinate, fixed-point (24.8)
878
879 src = srcPx.pixels;
880 srcRowBytes = srcPx.rowBytes;
881 dst = dstPx.pixels; // Assumes no row padding.
882
883 src0 = prev = (uint32_t*)src;
884
885 xCount = (srcPx.width * OOPixMapBytesPerPixel(srcPx)) >> 2;
886
887 for (y = 1; y != dstPx.height; ++y) {
888 fractY = ((srcPx.height * y) << 8) / dstPx.height;
889
890 src0 = prev;
891 prev = src1 = (uint32_t*)(src + srcRowBytes * (fractY >> 8));
892
893 weight1 = fractY & 0xFF;
894 weight0 = 0x100 - weight1;
895
896 x = xCount;
897 while (x--) {
898 px0 = *src0++;
899 px1 = *src1++;
900
901 ag = ((px0 & 0xFF00FF00) >> 8) * weight0 + ((px1 & 0xFF00FF00) >> 8) * weight1;
902 br = (px0 & 0x00FF00FF) * weight0 + (px1 & 0x00FF00FF) * weight1;
903
904 *dst++ = (ag & 0xFF00FF00) | ((br >> 8) & 0x00FF00FF);
905 }
906 }
907
908 // Copy last row (without referring to the last-plus-oneth row)
909 x = xCount;
910 while (x--) {
911 *dst++ = *src0++;
912 }
913}

References OOPixMap::height, OOPixMapBytesPerPixel(), OOPixMap::pixels, OOPixMap::rowBytes, OOPixMap::width, x, and y.

Referenced by StretchVertically().

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