2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* Copyright (C) 2007 Hiroshi Morii <koolsmoky(at)users.sourceforge.net>
21 * Modified for the Texture Filtering library
25 #include "TextureFilters.h"
27 /************************************************************************/
29 /************************************************************************/
31 #define DWORD_MAKE(r, g, b, a) ((uint32) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)))
32 #define WORD_MAKE(r, g, b, a) ((uint16) (((a) << 12) | ((r) << 8) | ((g) << 4) | (b)))
34 // Basic 2x R8G8B8A8 filter with interpolation
36 void Texture2x_32(uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height)
38 uint32 *pDst1, *pDst2;
40 uint32 nWidth = width;
41 uint32 nHeight = height;
63 for (ySrc = 0; ySrc < nHeight; ySrc++)
65 pSrc = (uint32*)(((uint8*)srcPtr)+ySrc*srcPitch);
66 pSrc2 = (uint32*)(((uint8*)srcPtr)+(ySrc+1)*srcPitch);
67 pDst1 = (uint32*)(((uint8*)dstPtr)+(ySrc*2)*dstPitch);
68 pDst2 = (uint32*)(((uint8*)dstPtr)+(ySrc*2+1)*dstPitch);
70 for (xSrc = 0; xSrc < nWidth; xSrc++)
72 b1 = (pSrc[xSrc]>>0)&0xFF;
73 g1 = (pSrc[xSrc]>>8)&0xFF;
74 r1 = (pSrc[xSrc]>>16)&0xFF;
75 a1 = (pSrc[xSrc]>>24)&0xFF;
78 pDst1[xSrc*2] = pSrc[xSrc];
83 b2 = (pSrc[xSrc+1]>>0)&0xFF;
84 g2 = (pSrc[xSrc+1]>>8)&0xFF;
85 r2 = (pSrc[xSrc+1]>>16)&0xFF;
86 a2 = (pSrc[xSrc+1]>>24)&0xFF;
87 pDst1[xSrc*2+1] = DWORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
90 pDst1[xSrc*2+1] = pSrc[xSrc];
95 b3 = (pSrc2[xSrc]>>0)&0xFF;
96 g3 = (pSrc2[xSrc]>>8)&0xFF;
97 r3 = (pSrc2[xSrc]>>16)&0xFF;
98 a3 = (pSrc2[xSrc]>>24)&0xFF;
99 pDst2[xSrc*2] = DWORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
102 b4 = (pSrc2[xSrc+1]>>0)&0xFF;
103 g4 = (pSrc2[xSrc+1]>>8)&0xFF;
104 r4 = (pSrc2[xSrc+1]>>16)&0xFF;
105 a4 = (pSrc2[xSrc+1]>>24)&0xFF;
107 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r2+r3+r4)/4, (g1+g2+g3+g4)/4, (b1+b2+b3+b4)/4, (a1+a2+a3+a4)/4);
112 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
118 pDst2[xSrc*2] = pSrc[xSrc];
122 pDst2[xSrc*2+1] = DWORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
126 pDst2[xSrc*2+1] = pSrc[xSrc];
134 // Basic 2x R4G4B4A4 filter with interpolation
135 void Texture2x_16(uint8 *srcPtr, uint32 srcPitch, uint8 *dstPtr, uint32 dstPitch, int width, int height)
137 uint16 *pDst1, *pDst2;
138 uint16 *pSrc, *pSrc2;
139 uint32 nWidth = width;
140 uint32 nHeight = height;
162 for (ySrc = 0; ySrc < nHeight; ySrc++)
164 pSrc = (uint16*)(((uint8*)srcPtr)+ySrc*srcPitch);
165 pSrc2 = (uint16*)(((uint8*)srcPtr)+(ySrc+1)*srcPitch);
166 pDst1 = (uint16*)(((uint8*)dstPtr)+(ySrc*2)*dstPitch);
167 pDst2 = (uint16*)(((uint8*)dstPtr)+(ySrc*2+1)*dstPitch);
169 for (xSrc = 0; xSrc < nWidth; xSrc++)
171 b1 = (pSrc[xSrc]>> 0)&0xF;
172 g1 = (pSrc[xSrc]>> 4)&0xF;
173 r1 = (pSrc[xSrc]>> 8)&0xF;
174 a1 = (pSrc[xSrc]>>12)&0xF;
178 b2 = (pSrc[xSrc+1]>> 0)&0xF;
179 g2 = (pSrc[xSrc+1]>> 4)&0xF;
180 r2 = (pSrc[xSrc+1]>> 8)&0xF;
181 a2 = (pSrc[xSrc+1]>>12)&0xF;
186 b3 = (pSrc2[xSrc]>> 0)&0xF;
187 g3 = (pSrc2[xSrc]>> 4)&0xF;
188 r3 = (pSrc2[xSrc]>> 8)&0xF;
189 a3 = (pSrc2[xSrc]>>12)&0xF;
192 b4 = (pSrc2[xSrc+1]>> 0)&0xF;
193 g4 = (pSrc2[xSrc+1]>> 4)&0xF;
194 r4 = (pSrc2[xSrc+1]>> 8)&0xF;
195 a4 = (pSrc2[xSrc+1]>>12)&0xF;
200 pDst1[xSrc*2] = pSrc[xSrc];
205 pDst1[xSrc*2+1] = WORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
208 pDst1[xSrc*2+1] = pSrc[xSrc];
214 pDst2[xSrc*2] = WORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
217 pDst2[xSrc*2] = pSrc[xSrc];
224 pDst2[xSrc*2+1] = WORD_MAKE((r1+r2+r3+r4)/4, (g1+g2+g3+g4)/4, (b1+b2+b3+b4)/4, (a1+a2+a3+a4)/4);
228 pDst2[xSrc*2+1] = WORD_MAKE((r1+r2)/2, (g1+g2)/2, (b1+b2)/2, (a1+a2)/2);
235 pDst2[xSrc*2+1] = WORD_MAKE((r1+r3)/2, (g1+g3)/2, (b1+b3)/2, (a1+a3)/2);
238 pDst2[xSrc*2+1] = pSrc[xSrc];
243 #endif /* !_16BPP_HACK */
247 * Hiroshi Morii <koolsmoky@users.sourceforge.net>
249 void SharpFilter_8888(uint32 *src, uint32 srcwidth, uint32 srcheight, uint32 *dest, uint32 filter)
251 // NOTE: for now we get away with copying the boundaries
252 // filter the boundaries if we face problems
254 uint32 mul1, mul2, mul3, shift4;
257 uint32 *_src1, *_src2, *_src3, *_dest;
259 uint32 t1,t2,t3,t4,t5,t6,t7,t8,t9;
280 _src2 = _src1 + srcwidth;
281 _src3 = _src2 + srcwidth;
284 // copy the first row
285 memcpy(_dest, _src1, (srcwidth << 2));
287 // filter 2nd row to 1 row before the last
288 for (y = 1; y < srcheight-1; y++) {
289 // copy the first pixel
291 // filter 2nd pixel to 1 pixel before last
292 for (x = 1; x < srcwidth-1; x++) {
293 for (z=0; z<4; z++) {
294 t1 = *((uint8*)(_src1+x-1)+z);
295 t2 = *((uint8*)(_src1+x )+z);
296 t3 = *((uint8*)(_src1+x+1)+z);
297 t4 = *((uint8*)(_src2+x-1)+z);
298 t5 = *((uint8*)(_src2+x )+z);
299 t6 = *((uint8*)(_src2+x+1)+z);
300 t7 = *((uint8*)(_src3+x-1)+z);
301 t8 = *((uint8*)(_src3+x )+z);
302 t9 = *((uint8*)(_src3+x+1)+z);
304 if( (t5*mul2) > (t1+t3+t7+t9+t2+t4+t6+t8)*mul1 ) {
305 val[z]= ((t5*mul3) - (t1+t3+t7+t9+t2+t4+t6+t8)*mul1)>>shift4;
306 if (val[z] > 0xFF) val[z] = 0xFF;
311 _dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
313 // copy the ending pixel
314 _dest[srcwidth-1] = *(_src3 - 1);
322 memcpy(_dest, _src2, (srcwidth << 2));
326 void SharpFilter_4444(uint16 *src, uint32 srcwidth, uint32 srcheight, uint16 *dest, uint32 filter)
328 // NOTE: for now we get away with copying the boundaries
329 // filter the boundaries if we face problems
331 uint16 mul1, mul2, mul3, shift4;
334 uint16 *_src1, *_src2, *_src3, *_dest;
336 uint16 t1,t2,t3,t4,t5,t6,t7,t8,t9;
356 _src2 = _src1 + srcwidth;
357 _src3 = _src2 + srcwidth;
360 // copy the first row
361 memcpy(_dest, _src1, (srcwidth << 1));
363 // filter 2nd row to 1 row before the last
364 for( y = 1; y < srcheight - 1; y++) {
365 // copy the first pixel
367 // filter 2nd pixel to 1 pixel before last
368 for( x = 1; x < srcwidth - 1; x++) {
369 for( z = 0; z < 4; z++ ) {
370 /* Hiroshi Morii <koolsmoky@users.sourceforge.net>
371 * Read the entire 16bit pixel and then extract the A,R,G,B components.
373 uint32 shift = z << 2;
374 t1 = ((*((uint16*)(_src1+x-1))) >> shift) & 0xF;
375 t2 = ((*((uint16*)(_src1+x ))) >> shift) & 0xF;
376 t3 = ((*((uint16*)(_src1+x+1))) >> shift) & 0xF;
377 t4 = ((*((uint16*)(_src2+x-1))) >> shift) & 0xF;
378 t5 = ((*((uint16*)(_src2+x ))) >> shift) & 0xF;
379 t6 = ((*((uint16*)(_src2+x+1))) >> shift) & 0xF;
380 t7 = ((*((uint16*)(_src3+x-1))) >> shift) & 0xF;
381 t8 = ((*((uint16*)(_src3+x ))) >> shift) & 0xF;
382 t9 = ((*((uint16*)(_src3+x+1))) >> shift) & 0xF;
384 if( (t5*mul2) > (t1+t3+t7+t9+t2+t4+t6+t8)*mul1 ) {
385 val[z] = ((t5*mul3) - (t1+t3+t7+t9+t2+t4+t6+t8)*mul1)>>shift4;
386 if (val[z] > 0xF) val[z] = 0xF;
391 _dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
393 // copy the ending pixel
394 _dest[srcwidth-1] = *(_src3 - 1);
402 memcpy(_dest, _src2, (srcwidth << 1));
404 #endif /* !_16BPP_HACK */
408 * Hiroshi Morii <koolsmoky@users.sourceforge.net>
410 void SmoothFilter_8888(uint32 *src, uint32 srcwidth, uint32 srcheight, uint32 *dest, uint32 filter)
412 // NOTE: for now we get away with copying the boundaries
413 // filter the boundaries if we face problems
415 uint32 mul1, mul2, mul3, shift4;
418 uint32 *_src1, *_src2, *_src3, *_dest;
420 uint32 t1,t2,t3,t4,t5,t6,t7,t8,t9;
423 case SMOOTH_FILTER_4:
429 case SMOOTH_FILTER_3:
435 case SMOOTH_FILTER_2:
441 case SMOOTH_FILTER_1:
451 case SMOOTH_FILTER_3:
452 case SMOOTH_FILTER_4:
455 _src2 = _src1 + srcwidth;
456 _src3 = _src2 + srcwidth;
458 // copy the first row
459 memcpy(_dest, _src1, (srcwidth << 2));
461 // filter 2nd row to 1 row before the last
462 for (y = 1; y < srcheight - 1; y++){
463 // copy the first pixel
465 // filter 2nd pixel to 1 pixel before last
466 for (x = 1; x < srcwidth - 1; x++) {
467 for (z = 0; z < 4; z++ ) {
468 t1 = *((uint8*)(_src1+x-1)+z);
469 t2 = *((uint8*)(_src1+x )+z);
470 t3 = *((uint8*)(_src1+x+1)+z);
471 t4 = *((uint8*)(_src2+x-1)+z);
472 t5 = *((uint8*)(_src2+x )+z);
473 t6 = *((uint8*)(_src2+x+1)+z);
474 t7 = *((uint8*)(_src3+x-1)+z);
475 t8 = *((uint8*)(_src3+x )+z);
476 t9 = *((uint8*)(_src3+x+1)+z);
477 /* the component value must not overflow 0xFF */
478 val[z] = ((t1+t3+t7+t9)*mul1+((t2+t4+t6+t8)*mul2)+(t5*mul3))>>shift4;
479 if (val[z] > 0xFF) val[z] = 0xFF;
481 _dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
483 // copy the ending pixel
484 _dest[srcwidth-1] = *(_src3 - 1);
492 memcpy(_dest, _src2, (srcwidth << 2));
494 case SMOOTH_FILTER_1:
495 case SMOOTH_FILTER_2:
499 _src2 = _src1 + srcwidth;
500 _src3 = _src2 + srcwidth;
502 // copy the first row
503 memcpy(_dest, _src1, (srcwidth << 2));
505 // filter 2nd row to 1 row before the last
506 for (y = 1; y < srcheight - 1; y++) {
507 // filter 1st pixel to the last
509 for( x = 0; x < srcwidth; x++) {
510 for( z = 0; z < 4; z++ ) {
511 t2 = *((uint8*)(_src1+x )+z);
512 t5 = *((uint8*)(_src2+x )+z);
513 t8 = *((uint8*)(_src3+x )+z);
514 /* the component value must not overflow 0xFF */
515 val[z] = ((t2+t8)*mul2+(t5*mul3))>>shift4;
516 if (val[z] > 0xFF) val[z] = 0xFF;
518 _dest[x] = val[0]|(val[1]<<8)|(val[2]<<16)|(val[3]<<24);
521 memcpy(_dest, _src2, (srcwidth << 2));
530 memcpy(_dest, _src2, (srcwidth << 2));
536 void SmoothFilter_4444(uint16 *src, uint32 srcwidth, uint32 srcheight, uint16 *dest, uint32 filter)
538 // NOTE: for now we get away with copying the boundaries
539 // filter the boundaries if we face problems
541 uint16 mul1, mul2, mul3, shift4;
544 uint16 *_src1, *_src2, *_src3, *_dest;
546 uint16 t1,t2,t3,t4,t5,t6,t7,t8,t9;
549 case SMOOTH_FILTER_4:
555 case SMOOTH_FILTER_3:
561 case SMOOTH_FILTER_2:
567 case SMOOTH_FILTER_1:
577 case SMOOTH_FILTER_3:
578 case SMOOTH_FILTER_4:
581 _src2 = _src1 + srcwidth;
582 _src3 = _src2 + srcwidth;
584 // copy the first row
585 memcpy(_dest, _src1, (srcwidth << 1));
587 // filter 2nd row to 1 row before the last
588 for (y = 1; y < srcheight - 1; y++) {
589 // copy the first pixel
591 // filter 2nd pixel to 1 pixel before last
592 for (x = 1; x < srcwidth - 1; x++) {
593 for (z = 0; z < 4; z++ ) {
594 /* Read the entire 16bit pixel and then extract the A,R,G,B components. */
595 uint32 shift = z << 2;
596 t1 = ((*(uint16*)(_src1+x-1)) >> shift) & 0xF;
597 t2 = ((*(uint16*)(_src1+x )) >> shift) & 0xF;
598 t3 = ((*(uint16*)(_src1+x+1)) >> shift) & 0xF;
599 t4 = ((*(uint16*)(_src2+x-1)) >> shift) & 0xF;
600 t5 = ((*(uint16*)(_src2+x )) >> shift) & 0xF;
601 t6 = ((*(uint16*)(_src2+x+1)) >> shift) & 0xF;
602 t7 = ((*(uint16*)(_src3+x-1)) >> shift) & 0xF;
603 t8 = ((*(uint16*)(_src3+x )) >> shift) & 0xF;
604 t9 = ((*(uint16*)(_src3+x+1)) >> shift) & 0xF;
605 /* the component value must not overflow 0xF */
606 val[z] = ((t1+t3+t7+t9)*mul1+((t2+t4+t6+t8)*mul2)+(t5*mul3))>>shift4;
607 if (val[z] > 0xF) val[z] = 0xF;
609 _dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
611 // copy the ending pixel
612 _dest[srcwidth-1] = *(_src3 - 1);
620 memcpy(_dest, _src2, (srcwidth << 1));
622 case SMOOTH_FILTER_1:
623 case SMOOTH_FILTER_2:
627 _src2 = _src1 + srcwidth;
628 _src3 = _src2 + srcwidth;
630 // copy the first row
631 memcpy(_dest, _src1, (srcwidth << 1));
633 // filter 2nd row to 1 row before the last
634 for( y = 1; y < srcheight - 1; y++) {
636 for( x = 0; x < srcwidth; x++) {
637 for( z = 0; z < 4; z++ ) {
638 /* Read the entire 16bit pixel and then extract the A,R,G,B components. */
639 uint32 shift = z << 2;
640 t2 = ((*(uint16*)(_src1+x)) >> shift) & 0xF;
641 t5 = ((*(uint16*)(_src2+x)) >> shift) & 0xF;
642 t8 = ((*(uint16*)(_src3+x)) >> shift) & 0xF;
643 /* the component value must not overflow 0xF */
644 val[z] = ((t2+t8)*mul2+(t5*mul3))>>shift4;
645 if (val[z] > 0xF) val[z] = 0xF;
647 _dest[x] = val[0]|(val[1]<<4)|(val[2]<<8)|(val[3]<<12);
650 memcpy(_dest, _src2, (srcwidth << 1));
659 memcpy(_dest, _src2, (srcwidth << 1));
663 #endif /* !_16BPP_HACK */
665 void filter_8888(uint32 *src, uint32 srcwidth, uint32 srcheight, uint32 *dest, uint32 filter) {
666 switch (filter & ENHANCEMENT_MASK) {
667 case HQ4X_ENHANCEMENT:
668 hq4x_8888((uint8*)src, (uint8*)dest, srcwidth, srcheight, srcwidth, (srcwidth << 4));
670 case HQ2X_ENHANCEMENT:
671 hq2x_32((uint8*)src, (srcwidth << 2), (uint8*)dest, (srcwidth << 3), srcwidth, srcheight);
673 case HQ2XS_ENHANCEMENT:
674 hq2xS_32((uint8*)src, (srcwidth << 2), (uint8*)dest, (srcwidth << 3), srcwidth, srcheight);
676 case LQ2X_ENHANCEMENT:
677 lq2x_32((uint8*)src, (srcwidth << 2), (uint8*)dest, (srcwidth << 3), srcwidth, srcheight);
679 case LQ2XS_ENHANCEMENT:
680 lq2xS_32((uint8*)src, (srcwidth << 2), (uint8*)dest, (srcwidth << 3), srcwidth, srcheight);
682 case X2SAI_ENHANCEMENT:
683 Super2xSaI_8888((uint32*)src, (uint32*)dest, srcwidth, srcheight, srcwidth);
686 Texture2x_32((uint8*)src, (srcwidth << 2), (uint8*)dest, (srcwidth << 3), srcwidth, srcheight);
690 switch (filter & (SMOOTH_FILTER_MASK|SHARP_FILTER_MASK)) {
691 case SMOOTH_FILTER_1:
692 case SMOOTH_FILTER_2:
693 case SMOOTH_FILTER_3:
694 case SMOOTH_FILTER_4:
695 SmoothFilter_8888((uint32*)src, srcwidth, srcheight, (uint32*)dest, (filter & SMOOTH_FILTER_MASK));
699 SharpFilter_8888((uint32*)src, srcwidth, srcheight, (uint32*)dest, (filter & SHARP_FILTER_MASK));