Oolite
Loading...
Searching...
No Matches
OOTextureScaling.m
Go to the documentation of this file.
1/*
2
3OOTextureScaling.m
4
5Copyright (C) 2007-2013 Jens Ayton
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#import "OOTextureScaling.h"
28#import "OOCPUInfo.h"
30#import "OOLogging.h"
31#import "OOMaths.h"
32#include <stdlib.h>
33
34#define DUMP_MIP_MAPS 0
35#define DUMP_SCALE 0
36
37/* Internal function declarations.
38
39 NOTE: the function definitions are grouped together for best code cache
40 coherence rather than the order listed here.
41 */
42static BOOL GenerateMipMaps1(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC;
43static BOOL GenerateMipMaps2(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC;
44static BOOL GenerateMipMaps4(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC;
45
46/* ScaleToHalf_P_xN functions
47 These scale a texture with P planes (components) to half its size in each
48 dimension, handling N pixels at a time. srcWidth must be a multiple of N.
49 Parameters are not validated -- bad parameters will lead to bad data or a
50 crash.
51
52 Scaling is an unweighted average. 8 bits per channel assumed.
53 It is safe and meaningful for srcBytes == dstBytes.
54*/
55static void ScaleToHalf_1_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
56static void ScaleToHalf_2_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
57static void ScaleToHalf_4_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
58
59#if OOLITE_NATIVE_64_BIT
60static void ScaleToHalf_1_x8(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
61// static void ScaleToHalf_2_x4(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
62static void ScaleToHalf_4_x2(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
63#else
64static void ScaleToHalf_1_x4(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
65// static void ScaleToHalf_2_x2(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC;
66#endif
67
72
73static void StretchVerticallyN_x1(OOPixMap srcPx, OOPixMap dstPx);
74
75static void SqueezeVertically1(OOPixMap srcPx, OOPixMapDimension dstHeight);
76static void SqueezeVertically2(OOPixMap srcPx, OOPixMapDimension dstHeight);
77static void SqueezeVertically4(OOPixMap srcPx, OOPixMapDimension dstHeight);
78static void StretchHorizontally1(OOPixMap srcPx, OOPixMap dstPx);
79static void StretchHorizontally2(OOPixMap srcPx, OOPixMap dstPx);
80static void StretchHorizontally4(OOPixMap srcPx, OOPixMap dstPx);
81static void SqueezeHorizontally1(OOPixMap srcPx, OOPixMapDimension dstWidth);
82static void SqueezeHorizontally2(OOPixMap srcPx, OOPixMapDimension dstWidth);
83static void SqueezeHorizontally4(OOPixMap srcPx, OOPixMapDimension dstWidth);
84
85static BOOL EnsureCorrectDataSize(OOPixMap* pixMap, BOOL leaveSpaceForMipMaps) NONNULL_FUNC;
86
87#if !OOLITE_NATIVE_64_BIT
88
89static void StretchVerticallyN_x4(OOPixMap srcPx, OOPixMap dstPx);
90
92{
93 if (!((srcPx.rowBytes) & 3)) {
94 StretchVerticallyN_x4(srcPx, dstPx);
95 } else {
96 StretchVerticallyN_x1(srcPx, dstPx);
97 }
98}
99
100#else // OOLITE_NATIVE_64_BIT
101
102static void StretchVerticallyN_x8(OOPixMap srcPx, OOPixMap dstPx);
103
105{
106 if (!((srcPx.rowBytes) & 7)) {
107 StretchVerticallyN_x8(srcPx, dstPx);
108 } else {
109 StretchVerticallyN_x1(srcPx, dstPx);
110 }
111}
112
113#endif
114
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}
141
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}
170
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}
197
198#if DUMP_MIP_MAPS || DUMP_SCALE
199// NOTE: currently only works on OS X because of OSAtomicAdd32() (used to increment ID counter in thread-safe way). A simple increment would be sufficient if limited to a single thread (in OOTextureLoader).
200volatile int32_t sPreviousDumpID = 0;
201int32_t OSAtomicAdd32(int32_t __theAmount, volatile int32_t* __theValue);
202
203#endif
204
205#if DUMP_MIP_MAPS
206#define DUMP_CHANNELS -1 // Bitmap of channel counts - -1 for all dumps
207
208#define DUMP_MIP_MAP_PREPARE(pl) \
209 uint32_t dumpPlanes = pl; \
210 uint32_t dumpLevel = 0; \
211 BOOL dumpThis = (dumpPlanes & DUMP_CHANNELS) != 0; \
212 SInt32 dumpID = dumpThis ? OSAtomicAdd32(1, &sPreviousDumpID) : 0;
213#define DUMP_MIP_MAP_DUMP(px, w, h) \
214 if (dumpThis) \
215 DumpMipMap(px, w, h, dumpPlanes, dumpID, dumpLevel++);
216static void DumpMipMap(void* data, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, SInt32 ID, uint32_t level);
217#else
218#define DUMP_MIP_MAP_PREPARE(pl) \
219 do { \
220 (void)pl; \
221 } while (0)
222#define DUMP_MIP_MAP_DUMP(px, w, h) \
223 do { \
224 (void)px; \
225 (void)w; \
226 (void)h; \
227 } while (0)
228#endif
229
230#if DUMP_SCALE
231#define DUMP_SCALE_PREPARE() SInt32 dumpID = OSAtomicAdd32(1, &sPreviousDumpID), dumpCount = 0;
232#define DUMP_SCALE_DUMP(PM, stage) \
233 do { \
234 OOPixMap* pm = &(PM); \
235 OODumpPixMap(*pm, [NSString stringWithFormat:@"scaling dump ID %u stage %u-%@ %ux%u", dumpID, dumpCount++, stage, pm->width, pm->height]); \
236 } while (0)
237#else
238#define DUMP_SCALE_PREPARE()
239#define DUMP_SCALE_DUMP(PM, stage) \
240 do { \
241 } while (0)
242#endif
243
244OOPixMap OOScalePixMap(OOPixMap srcPx, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
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}
334
335// FIXME: should take an OOPixMap.
336BOOL OOGenerateMipMaps(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)
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}
364
365static BOOL GenerateMipMaps1(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height)
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}
413
414static void ScaleToHalf_1_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
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}
450
451#if !OOLITE_NATIVE_64_BIT
452
453static void ScaleToHalf_1_x4(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
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}
499
500#else // OOLITE_NATIVE_64_BIT
501
502static void ScaleToHalf_1_x8(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
503{
505 uint64_t *src0, *src1;
506 uint64_t* dst;
507 uint_fast64_t px00, px01, px10, px11;
508 uint_fast64_t sum0, sum1;
509
510 srcWidth >>= 3; // Eight (output) pixels at a time
511 src0 = srcBytes;
512 src1 = src0 + srcWidth;
513 dst = dstBytes;
514
515 y = srcHeight >> 1;
516 do {
517 x = srcWidth >> 1;
518 do {
519 // Read 16 pixels in an 8x2 rectangle...
520 px00 = *src0++;
521 px01 = *src0++;
522 px10 = *src1++;
523 px11 = *src1++;
524
525 // ...add them together...
526 sum0 = ((px00 & 0x00FF00FF00FF00FFULL)) + ((px10 & 0x00FF00FF00FF00FFULL)) + ((px00 & 0xFF00FF00FF00FF00ULL) >> 8) + ((px10 & 0xFF00FF00FF00FF00ULL) >> 8);
527 sum1 = ((px01 & 0x00FF00FF00FF00FFULL)) + ((px11 & 0x00FF00FF00FF00FFULL)) + ((px01 & 0xFF00FF00FF00FF00ULL) >> 8) + ((px11 & 0xFF00FF00FF00FF00ULL) >> 8);
528
529 // ...swizzle the sums around...
530#if OOLITE_BIG_ENDIAN
531 sum0 = ((sum0 << 06) & 0xFF00000000000000ULL) | ((sum0 << 14) & 0x00FF000000000000ULL) | ((sum0 << 22) & 0x0000FF0000000000ULL) | ((sum0 << 30) & 0x000000FF00000000ULL);
532 sum1 = ((sum1 >> 26) & 0x00000000FF000000ULL) | ((sum1 >> 18) & 0x0000000000FF0000ULL) | ((sum1 >> 10) & 0x000000000000FF00ULL) | ((sum1 >> 02) & 0x00000000000000FFULL);
533#elif OOLITE_LITTLE_ENDIAN
534 sum0 = ((sum0 >> 26) & 0x00000000FF000000ULL) | ((sum0 >> 18) & 0x0000000000FF0000ULL) | ((sum0 >> 10) & 0x000000000000FF00ULL) | ((sum0 >> 02) & 0x00000000000000FFULL);
535 sum1 = ((sum1 << 06) & 0xFF00000000000000ULL) | ((sum1 << 14) & 0x00FF000000000000ULL) | ((sum1 << 22) & 0x0000FF0000000000ULL) | ((sum1 << 30) & 0x000000FF00000000ULL);
536#else
537#error Neither OOLITE_BIG_ENDIAN nor OOLITE_LITTLE_ENDIAN is defined as nonzero!
538#endif
539 // ...and write output pixel.
540 *dst++ = sum0 | sum1;
541 } while (--x);
542
543 // Skip a row for each source row
544 src0 = src1;
545 src1 += srcWidth;
546 } while (--y);
547}
548
549#endif
550
551static BOOL GenerateMipMaps2(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height)
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}
604
605static void ScaleToHalf_2_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
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}
642
643static BOOL GenerateMipMaps4(void* textureBytes, OOPixMapDimension width, OOPixMapDimension height)
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}
689
690static void ScaleToHalf_4_x1(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
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}
740
741#if OOLITE_NATIVE_64_BIT
742
743static void ScaleToHalf_4_x2(void* srcBytes, void* dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight)
744{
746 uint_fast64_t *src0, *src1, *dst;
747 uint_fast64_t px00, px01, px10, px11;
748
749 /* We treat channel layout as ABGR -- actual layout doesn't matter since
750 each channel is handled the same. We use two accumulators, with
751 alternating channels, so overflow doesn't cross channel boundaries,
752 while having less overhead than one accumulator per channel.
753 */
754 uint_fast64_t ag0, ag1, br0, br1;
755
756 srcWidth >>= 1; // Two bytes at a time
757 src0 = srcBytes;
758 src1 = src0 + srcWidth;
759 dst = dstBytes;
760
761 y = srcHeight >> 1;
762 do {
763 x = srcWidth >> 1;
764 do {
765 // Read eight pixels (4x2)...
766 px00 = *src0++;
767 px01 = *src0++;
768 px10 = *src1++;
769 px11 = *src1++;
770
771 // ...and add them together, channel by channel.
772 ag0 = (px00 & 0xFF00FF00FF00FF00ULL) >> 8;
773 br0 = (px00 & 0x00FF00FF00FF00FFULL);
774 ag0 += (px10 & 0xFF00FF00FF00FF00ULL) >> 8;
775 br0 += (px10 & 0x00FF00FF00FF00FFULL);
776 ag1 = (px01 & 0xFF00FF00FF00FF00ULL) >> 8;
777 br1 = (px01 & 0x00FF00FF00FF00FFULL);
778 ag1 += (px11 & 0xFF00FF00FF00FF00ULL) >> 8;
779 br1 += (px11 & 0x00FF00FF00FF00FFULL);
780
781#if OOLITE_BIG_ENDIAN
782 // Shift and add some more...
783 ag0 = ag0 + (ag0 << 32);
784 br0 = br0 + (br0 << 32);
785 ag1 = ag1 + (ag1 >> 32);
786 br1 = br1 + (br1 >> 32);
787
788 // ...merge and shift some more...
789 ag0 = ((ag0 & 0x03FC03FC00000000ULL) | (ag1 & 0x0000000003FC03FCULL)) << 6;
790 br0 = ((br0 & 0x03FC03FC00000000ULL) | (br1 & 0x0000000003FC03FCULL)) >> 2;
791#elif OOLITE_LITTLE_ENDIAN
792 // Shift and add some more...
793 ag0 = ag0 + (ag0 >> 32);
794 br0 = br0 + (br0 >> 32);
795 ag1 = ag1 + (ag1 << 32);
796 br1 = br1 + (br1 << 32);
797
798 // ...merge and shift some more...
799 ag0 = ((ag0 & 0x0000000003FC03FCULL) | (ag1 & 0x03FC03FC00000000ULL)) << 6;
800 br0 = ((br0 & 0x0000000003FC03FCULL) | (br1 & 0x03FC03FC00000000ULL)) >> 2;
801#else
802#error Unknown architecture.
803#endif
804
805 // ...and write output pixel.
806 *dst++ = ag0 | br0;
807 } while (--x);
808
809 // Skip a row for each source row
810 src0 = src1;
811 src1 += srcWidth;
812 } while (--y);
813}
814
815#endif
816
817#if DUMP_MIP_MAPS
818static void DumpMipMap(void* data, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format, SInt32 ID, uint32_t level)
819{
820 OOPixMap pixMap = OOMakePixMap(data, width, height, format, 0, 0);
821 OODumpPixMap(pixMap, [NSString stringWithFormat:@"mipmap dump ID %u lv%u %@ %ux%u", ID, level, OOPixMapFormatName(format), width, height]);
822}
823#endif
824
825static void StretchVerticallyN_x1(OOPixMap srcPx, OOPixMap dstPx)
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}
866
867#if !OOLITE_NATIVE_64_BIT
868
869static void StretchVerticallyN_x4(OOPixMap srcPx, OOPixMap dstPx)
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}
914
915#else // OOLITE_NATIVE_64_BIT
916
917static void StretchVerticallyN_x8(OOPixMap srcPx, OOPixMap dstPx)
918{
919 uint8_t* src;
920 uint64_t *src0, *src1, *prev, *dst;
921 uint64_t px0, px1, agag, brbr;
922 uint32_t x, y, xCount;
923 size_t srcRowBytes;
924 uint16_t weight0, weight1;
925 uint32_t fractY; // Y coordinate, fixed-point (24.8)
926
927 src = srcPx.pixels;
928 srcRowBytes = srcPx.rowBytes;
929 dst = dstPx.pixels; // Assumes dstPx.width == dstPx.rowBytes.
930
931 src0 = prev = (uint64_t*)src;
932
933 xCount = (srcPx.width * OOPixMapBytesPerPixel(srcPx)) >> 3;
934
935 for (y = 1; y != dstPx.height; ++y) {
936 fractY = ((srcPx.height * y) << 8) / dstPx.height;
937
938 src0 = prev;
939 prev = src1 = (uint64_t*)(src + srcRowBytes * (fractY >> 8));
940
941 weight1 = fractY & 0xFF;
942 weight0 = 0x100 - weight1;
943
944 x = xCount;
945 while (x--) {
946 px0 = *src0++;
947 px1 = *src1++;
948
949 agag = ((px0 & 0xFF00FF00FF00FF00ULL) >> 8) * weight0 + ((px1 & 0xFF00FF00FF00FF00ULL) >> 8) * weight1;
950 brbr = (px0 & 0x00FF00FF00FF00FFULL) * weight0 + (px1 & 0x00FF00FF00FF00FFULL) * weight1;
951
952 *dst++ = (agag & 0xFF00FF00FF00FF00ULL) | ((brbr >> 8) & 0x00FF00FF00FF00FFULL);
953 }
954 }
955
956 // Copy last row (without referring to the last-plus-oneth row)
957 x = xCount;
958 while (x--) {
959 *dst++ = *src0++;
960 }
961}
962#endif
963
964static void StretchHorizontally1(OOPixMap srcPx, OOPixMap dstPx)
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}
1018
1019static void StretchHorizontally2(OOPixMap srcPx, OOPixMap dstPx)
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}
1080
1081static void StretchHorizontally4(OOPixMap srcPx, OOPixMap dstPx)
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}
1143
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}
1201
1202static void SqueezeVertically1(OOPixMap srcPx, OOPixMapDimension dstHeight)
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}
1258
1259/* Macros to manage 2-channel accumulators in 2-channel squeeze scalers.
1260 accumHi is the sum of weighted high-channel pixels, shifted left 8 bits.
1261 accumLo is the sum of weighted low-channel pixels.
1262 weight is the sum of all pixel weights.
1263*/
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)
1272
1273#define CLEAR_ACCUM2() \
1274 do { \
1275 accumHi = 0; \
1276 accumLo = 0; \
1277 weight = 0; \
1278 } while (0)
1279
1280#define ACCUM2TOPX() ( \
1281 ((accumHi / weight) & 0xFF00) | ((accumLo / weight) & 0x00FF))
1282
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}
1339
1340static void SqueezeVertically2(OOPixMap srcPx, OOPixMapDimension dstHeight)
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}
1396
1397/* Macros to manage 4-channel accumulators in 4-channel squeeze scalers.
1398 The approach is similar to the ACCUM2 family above, except that the wt
1399 multiplication works on two channels at a time before splitting into four
1400 accumulators, all of which are shifted to the low end of the value.
1401*/
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)
1414
1415#define CLEAR_ACCUM4() \
1416 do { \
1417 accum1 = 0; \
1418 accum2 = 0; \
1419 accum3 = 0; \
1420 accum4 = 0; \
1421 weight = 0; \
1422 } while (0)
1423
1424/* These integer divisions cause a stall -- this is the biggest
1425 bottleneck in this file. Unrolling the loop might help on PPC.
1426 Linear interpolation instead of box filtering would help, with
1427 a quality hit. Given that scaling doesn't happen very often,
1428 I think I'll leave it this way. -- Ahruman
1429*/
1430#define ACCUM4TOPX() ( \
1431 (((accum1 / weight) & 0xFF) << 24) | (((accum3 / weight) & 0xFF) << 8) | (((accum2 / weight) & 0xFF) << 16) | ((accum4 / weight) & 0xFF))
1432
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}
1489
1490static void SqueezeVertically4(OOPixMap srcPx, OOPixMapDimension dstHeight)
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}
1547
1548static BOOL EnsureCorrectDataSize(OOPixMap* pixMap, BOOL leaveSpaceForMipMaps)
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 ALWAYS_INLINE_FUNC
#define NONNULL_FUNC
#define OOINLINE
#define EXPECT(x)
#define FAIL(s)
#define OOLogGenericParameterError()
Definition OOLogging.h:137
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogParameterError
Definition OOLogging.m:596
uint32_t OOPixMapDimension
Definition OOPixMap.h:32
NSString * OOPixMapFormatName(OOPixMapFormat format) PURE_FUNC
Definition OOPixMap.m:212
void OODumpPixMap(OOPixMap pixMap, NSString *name)
Definition OOPixMap.m:140
OOPixMapFormat
Definition OOPixMap.h:36
@ kOOPixMapInvalidFormat
Definition OOPixMap.h:37
@ kOOPixMapGrayscale
Definition OOPixMap.h:38
@ kOOPixMapRGBA
Definition OOPixMap.h:40
@ kOOPixMapGrayscaleAlpha
Definition OOPixMap.h:39
const OOPixMap kOONullPixMap
Definition OOPixMap.m:30
#define OORoundUpToPowerOf2_PixMap
Definition OOPixMap.h:34
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
float y
float x
static BOOL GenerateMipMaps2(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static void SqueezeVertically1(OOPixMap srcPx, OOPixMapDimension dstHeight)
#define DUMP_MIP_MAP_DUMP(px, w, h)
static void ScaleToHalf_4_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
#define ACCUM2(PX, WT)
static void ScaleToHalf_2_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
#define DUMP_SCALE_PREPARE()
static void StretchHorizontally2(OOPixMap srcPx, OOPixMap dstPx)
#define DUMP_SCALE_DUMP(PM, stage)
#define ACCUM2TOPX()
static BOOL GenerateMipMaps4(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
static BOOL EnsureCorrectDataSize(OOPixMap *pixMap, BOOL leaveSpaceForMipMaps) NONNULL_FUNC
OOPixMap OOScalePixMap(OOPixMap srcPx, OOPixMapDimension dstWidth, OOPixMapDimension dstHeight, BOOL leaveSpaceForMipMaps)
#define DUMP_MIP_MAP_PREPARE(pl)
OOINLINE void StretchVertically(OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
static void SqueezeHorizontally2(OOPixMap srcPx, OOPixMapDimension dstWidth)
static BOOL GenerateMipMaps1(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height) NONNULL_FUNC
OOINLINE void StretchHorizontally(OOPixMap srcPx, OOPixMap dstPx) ALWAYS_INLINE_FUNC
static void SqueezeVertically4(OOPixMap srcPx, OOPixMapDimension dstHeight)
#define ACCUM4(PX, WT)
#define ACCUM4TOPX()
OOINLINE void SqueezeVertically(OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
static void ScaleToHalf_1_x1(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
OOINLINE void SqueezeHorizontally(OOPixMap pixMap, OOPixMapDimension dstHeight) ALWAYS_INLINE_FUNC
static void SqueezeHorizontally1(OOPixMap srcPx, OOPixMapDimension dstWidth)
static void SqueezeHorizontally4(OOPixMap srcPx, OOPixMapDimension dstWidth)
BOOL OOGenerateMipMaps(void *textureBytes, OOPixMapDimension width, OOPixMapDimension height, OOPixMapFormat format)
static void StretchHorizontally1(OOPixMap srcPx, OOPixMap dstPx)
static void StretchVerticallyN_x1(OOPixMap srcPx, OOPixMap dstPx)
static void ScaleToHalf_1_x4(void *srcBytes, void *dstBytes, OOPixMapDimension srcWidth, OOPixMapDimension srcHeight) NONNULL_FUNC
static void StretchHorizontally4(OOPixMap srcPx, OOPixMap dstPx)
#define CLEAR_ACCUM4()
#define CLEAR_ACCUM2()
static void SqueezeVertically2(OOPixMap srcPx, OOPixMapDimension dstHeight)
static void StretchVerticallyN_x4(OOPixMap srcPx, OOPixMap dstPx)
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
OOPixMapDimension width
Definition OOPixMap.h:45
OOPixMapFormat format
Definition OOPixMap.h:46