Mercurial > vba-clojure
diff src/filters/pixel.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/filters/pixel.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,196 @@ 1.4 +#include "../Port.h" 1.5 + 1.6 +extern u32 RGB_LOW_BITS_MASK; 1.7 + 1.8 +void Pixelate2x16(u8 *srcPtr, u32 srcPitch, u8 *deltaPtr, 1.9 + u8 *dstPtr, u32 dstPitch, int width, int height) 1.10 +{ 1.11 + u8 *nextLine, *finish; 1.12 + u32 colorMask = ~(RGB_LOW_BITS_MASK | (RGB_LOW_BITS_MASK << 16)); 1.13 + colorMask = (colorMask >> 2) & (colorMask >> 1); 1.14 + 1.15 + nextLine = dstPtr + dstPitch; 1.16 + 1.17 + do 1.18 + { 1.19 + u32 *bP = (u32 *) srcPtr; 1.20 + u32 *xP = (u32 *) deltaPtr; 1.21 + u32 *dP = (u32 *) dstPtr; 1.22 + u32 *nL = (u32 *) nextLine; 1.23 + u32 currentPixel; 1.24 + u32 nextPixel; 1.25 + u32 currentDelta; 1.26 + u32 nextDelta; 1.27 + 1.28 + finish = (u8 *) bP + ((width+2) << 1); 1.29 + nextPixel = *bP++; 1.30 + nextDelta = *xP++; 1.31 + 1.32 + do 1.33 + { 1.34 + currentPixel = nextPixel; 1.35 + currentDelta = nextDelta; 1.36 + nextPixel = *bP++; 1.37 + nextDelta = *xP++; 1.38 + 1.39 + if ((nextPixel != nextDelta) || (currentPixel != currentDelta)) 1.40 + { 1.41 + u32 colorA, colorB, product; 1.42 + 1.43 + *(xP - 2) = currentPixel; 1.44 +#ifdef WORDS_BIGENDIAN 1.45 + colorA = currentPixel >> 16; 1.46 + colorB = currentPixel & 0xffff; 1.47 +#else 1.48 + colorA = currentPixel & 0xffff; 1.49 + colorB = currentPixel >> 16; 1.50 +#endif 1.51 + product = (colorA >> 2) & colorMask; 1.52 + 1.53 +#ifdef WORDS_BIGENDIAN 1.54 + *(nL) = (product << 16) | (product); 1.55 + *(dP) = (colorA << 16) | product; 1.56 +#else 1.57 + *(nL) = product | (product << 16); 1.58 + *(dP) = colorA | (product << 16); 1.59 +#endif 1.60 + 1.61 +#ifdef WORDS_BIGENDIAN 1.62 + colorA = nextPixel >> 16; 1.63 +#else 1.64 + colorA = nextPixel & 0xffff; 1.65 +#endif 1.66 + product = (colorB >> 2) & colorMask; 1.67 +#ifdef WORDS_BIGENDIAN 1.68 + *(nL + 1) = (product << 16) | (product); 1.69 + *(dP + 1) = (colorB << 16) | (product); 1.70 +#else 1.71 + *(nL + 1) = (product) | (product << 16); 1.72 + *(dP + 1) = (colorB) | (product << 16); 1.73 +#endif 1.74 + } 1.75 + 1.76 + dP += 2; 1.77 + nL += 2; 1.78 + } 1.79 + while ((u8 *) bP < finish); 1.80 + 1.81 + deltaPtr += srcPitch; 1.82 + srcPtr += srcPitch; 1.83 + dstPtr += dstPitch << 1; 1.84 + nextLine += dstPitch << 1; 1.85 + } 1.86 + while (--height); 1.87 +} 1.88 + 1.89 +void Pixelate2x32(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, 1.90 + u8 *dstPtr, u32 dstPitch, int width, int height) 1.91 +{ 1.92 + u8 *nextLine, *finish; 1.93 + u32 colorMask = ((u32)~RGB_LOW_BITS_MASK >> 2) & ((u32)~RGB_LOW_BITS_MASK >> 1); 1.94 + 1.95 + nextLine = dstPtr + dstPitch; 1.96 + 1.97 + do 1.98 + { 1.99 + u32 *bP = (u32 *) srcPtr; 1.100 + // u32 *xP = (u32 *) deltaPtr; 1.101 + u32 *dP = (u32 *) dstPtr; 1.102 + u32 *nL = (u32 *) nextLine; 1.103 + u32 currentPixel; 1.104 + u32 nextPixel; 1.105 + 1.106 + finish = (u8 *) bP + ((width+1) << 2); 1.107 + nextPixel = *bP++; 1.108 + 1.109 + do 1.110 + { 1.111 + u32 product; 1.112 + 1.113 + currentPixel = nextPixel; 1.114 + nextPixel = *bP++; 1.115 + product = (currentPixel >> 2) & colorMask; 1.116 + *(nL) = product; 1.117 + *(nL+1) = product; 1.118 + *(dP) = currentPixel; 1.119 + *(dP+1) = product; 1.120 + 1.121 + currentPixel = nextPixel; 1.122 + nextPixel = *bP++; 1.123 + product = (currentPixel >> 2) & colorMask; 1.124 + *(nL + 2) = product; 1.125 + *(nL + 3) = product; 1.126 + *(dP + 2) = currentPixel; 1.127 + *(dP + 3) = product; 1.128 + 1.129 + dP += 4; 1.130 + nL += 4; 1.131 + } 1.132 + while ((u8 *) bP < finish); 1.133 + 1.134 + srcPtr += srcPitch; 1.135 + dstPtr += dstPitch << 1; 1.136 + nextLine += dstPitch << 1; 1.137 + } 1.138 + while (--height); 1.139 +} 1.140 + 1.141 +// generic Pixelate Nx magnification filter 1.142 +template <int magnification, typename ColorType> 1.143 +void PixelateNx(u8 *srcPtr, u32 srcPitch, u8 * /* deltaPtr */, 1.144 + u8 *dstPtr, u32 dstPitch, int width, int height) 1.145 +{ 1.146 + ColorType colorMask = ((ColorType)~RGB_LOW_BITS_MASK >> 2) & ((ColorType)~RGB_LOW_BITS_MASK >> 1); 1.147 + 1.148 + srcPitch = srcPitch / sizeof(ColorType) - width; 1.149 + u32 dstNextP = dstPitch / sizeof(ColorType); 1.150 + u32 dstNextL = (dstNextP - width) * magnification; // skip to the next magnificated 'line' 1.151 + dstNextP -= magnification; 1.152 + 1.153 + u32 offset = (dstPitch + sizeof(ColorType)) * magnification - dstPitch; 1.154 + 1.155 + ColorType *src = (ColorType *)srcPtr; 1.156 + ColorType *dst = (ColorType *)dstPtr; 1.157 + 1.158 + do // per src line 1.159 + { 1.160 + u8 *finishP = (u8 *)dst + offset; 1.161 + for (int x = 0; x < width; ++x) // per pixel in line 1.162 + { 1.163 + ColorType col = *src; 1.164 + ColorType *dst2 = dst; 1.165 + u8 *finishM = (u8 *)(dst + magnification); 1.166 + 1.167 + ColorType product = (col >> 2) & colorMask; 1.168 + do 1.169 + { 1.170 + *dst2 = product; 1.171 + } while ((u8 *)++dst2 < finishM); 1.172 + dst2 += dstNextP; 1.173 + finishM += dstPitch; 1.174 + do // dst magnificated pixel 1.175 + { 1.176 + *dst2++ = product; 1.177 + do 1.178 + { 1.179 + *dst2 = col; 1.180 + } while ((u8 *)++dst2 < finishM); 1.181 + dst2 += dstNextP; 1.182 + finishM += dstPitch; 1.183 + } while ((u8 *)dst2 < finishP); 1.184 + 1.185 + ++src; 1.186 + dst += magnification; 1.187 + finishP += magnification * sizeof(ColorType); 1.188 + } 1.189 + src += srcPitch; 1.190 + dst += dstNextL; 1.191 + } while (--height); 1.192 +} 1.193 + 1.194 +typedef void (*PixelateNxFP)(u8*, u32, u8*, u8*, u32, int, int); 1.195 + 1.196 +PixelateNxFP Pixelate3x16 = PixelateNx<3, u16>; 1.197 +PixelateNxFP Pixelate3x32 = PixelateNx<3, u32>; 1.198 +PixelateNxFP Pixelate4x16 = PixelateNx<4, u16>; 1.199 +PixelateNxFP Pixelate4x32 = PixelateNx<4, u32>;