codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
//hq4x filter demo program //---------------------------------------------------------- //Copyright (C) 2003 MaxSt ( maxst@hiend3d.com ) //This program is free software; you can redistribute it and/or //modify it under the terms of the GNU Lesser General Public //License as published by the Free Software Foundation; either //version 2.1 of the License, or (at your option) any later version. // //This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //Lesser General Public License for more details. // //You should have received a copy of the GNU Lesser General Public //License along with this program; if not, write to the Free Software //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include "Image.h" static uint32_t RGBtoYUV[1 << 24]; static uint32_t YUV1, YUV2; const uint32_t Ymask = 0x00FF0000; const uint32_t Umask = 0x0000FF00; const uint32_t Vmask = 0x000000FF; const uint32_t trY = 0x00300000; const uint32_t trU = 0x00000700; const uint32_t trV = 0x00000006; inline void Interp1(uint32_t * pc, uint32_t c1, uint32_t c2) { *pc = (c1*3+c2) >> 2; } inline void Interp2(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) { *pc = (c1*2+c2+c3) >> 2; } inline void Interp3(uint32_t * pc, uint32_t c1, uint32_t c2) { //*pc = (c1*7+c2)/8; *pc = ((((c1 & 0x00FF00)*7 + (c2 & 0x00FF00) ) & 0x0007F800) + (((c1 & 0xFF00FF)*7 + (c2 & 0xFF00FF) ) & 0x07F807F8)) >> 3; } inline void Interp5(uint32_t * pc, uint32_t c1, uint32_t c2) { *pc = (c1+c2) >> 1; } inline void Interp6(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) { //*pc = (c1*5+c2*2+c3)/8; *pc = ((((c1 & 0x00FF00)*5 + (c2 & 0x00FF00)*2 + (c3 & 0x00FF00) ) & 0x0007F800) + (((c1 & 0xFF00FF)*5 + (c2 & 0xFF00FF)*2 + (c3 & 0xFF00FF) ) & 0x07F807F8)) >> 3; } inline void Interp7(uint32_t * pc, uint32_t c1, uint32_t c2, uint32_t c3) { //*pc = (c1*6+c2+c3)/8; *pc = ((((c1 & 0x00FF00)*6 + (c2 & 0x00FF00) + (c3 & 0x00FF00) ) & 0x0007F800) + (((c1 & 0xFF00FF)*6 + (c2 & 0xFF00FF) + (c3 & 0xFF00FF) ) & 0x07F807F8)) >> 3; } inline void Interp8(uint32_t * pc, uint32_t c1, uint32_t c2) { //*pc = (c1*5+c2*3)/8; *pc = ((((c1 & 0x00FF00)*5 + (c2 & 0x00FF00)*3 ) & 0x0007F800) + (((c1 & 0xFF00FF)*5 + (c2 & 0xFF00FF)*3 ) & 0x07F807F8)) >> 3; } #define PIXEL00_0 *dp = w[5]; #define PIXEL00_11 Interp1(dp, w[5], w[4]); #define PIXEL00_12 Interp1(dp, w[5], w[2]); #define PIXEL00_20 Interp2(dp, w[5], w[2], w[4]); #define PIXEL00_50 Interp5(dp, w[2], w[4]); #define PIXEL00_80 Interp8(dp, w[5], w[1]); #define PIXEL00_81 Interp8(dp, w[5], w[4]); #define PIXEL00_82 Interp8(dp, w[5], w[2]); #define PIXEL01_0 *(dp+1) = w[5]; #define PIXEL01_10 Interp1(dp+1, w[5], w[1]); #define PIXEL01_12 Interp1(dp+1, w[5], w[2]); #define PIXEL01_14 Interp1(dp+1, w[2], w[5]); #define PIXEL01_21 Interp2(dp+1, w[2], w[5], w[4]); #define PIXEL01_31 Interp3(dp+1, w[5], w[4]); #define PIXEL01_50 Interp5(dp+1, w[2], w[5]); #define PIXEL01_60 Interp6(dp+1, w[5], w[2], w[4]); #define PIXEL01_61 Interp6(dp+1, w[5], w[2], w[1]); #define PIXEL01_82 Interp8(dp+1, w[5], w[2]); #define PIXEL01_83 Interp8(dp+1, w[2], w[4]); #define PIXEL02_0 *(dp+2) = w[5]; #define PIXEL02_10 Interp1(dp+2, w[5], w[3]); #define PIXEL02_11 Interp1(dp+2, w[5], w[2]); #define PIXEL02_13 Interp1(dp+2, w[2], w[5]); #define PIXEL02_21 Interp2(dp+2, w[2], w[5], w[6]); #define PIXEL02_32 Interp3(dp+2, w[5], w[6]); #define PIXEL02_50 Interp5(dp+2, w[2], w[5]); #define PIXEL02_60 Interp6(dp+2, w[5], w[2], w[6]); #define PIXEL02_61 Interp6(dp+2, w[5], w[2], w[3]); #define PIXEL02_81 Interp8(dp+2, w[5], w[2]); #define PIXEL02_83 Interp8(dp+2, w[2], w[6]); #define PIXEL03_0 *(dp+3) = w[5]; #define PIXEL03_11 Interp1(dp+3, w[5], w[2]); #define PIXEL03_12 Interp1(dp+3, w[5], w[6]); #define PIXEL03_20 Interp2(dp+3, w[5], w[2], w[6]); #define PIXEL03_50 Interp5(dp+3, w[2], w[6]); #define PIXEL03_80 Interp8(dp+3, w[5], w[3]); #define PIXEL03_81 Interp8(dp+3, w[5], w[2]); #define PIXEL03_82 Interp8(dp+3, w[5], w[6]); #define PIXEL10_0 *(dp+dpL) = w[5]; #define PIXEL10_10 Interp1(dp+dpL, w[5], w[1]); #define PIXEL10_11 Interp1(dp+dpL, w[5], w[4]); #define PIXEL10_13 Interp1(dp+dpL, w[4], w[5]); #define PIXEL10_21 Interp2(dp+dpL, w[4], w[5], w[2]); #define PIXEL10_32 Interp3(dp+dpL, w[5], w[2]); #define PIXEL10_50 Interp5(dp+dpL, w[4], w[5]); #define PIXEL10_60 Interp6(dp+dpL, w[5], w[4], w[2]); #define PIXEL10_61 Interp6(dp+dpL, w[5], w[4], w[1]); #define PIXEL10_81 Interp8(dp+dpL, w[5], w[4]); #define PIXEL10_83 Interp8(dp+dpL, w[4], w[2]); #define PIXEL11_0 *(dp+dpL+1) = w[5]; #define PIXEL11_30 Interp3(dp+dpL+1, w[5], w[1]); #define PIXEL11_31 Interp3(dp+dpL+1, w[5], w[4]); #define PIXEL11_32 Interp3(dp+dpL+1, w[5], w[2]); #define PIXEL11_70 Interp7(dp+dpL+1, w[5], w[4], w[2]); #define PIXEL12_0 *(dp+dpL+2) = w[5]; #define PIXEL12_30 Interp3(dp+dpL+2, w[5], w[3]); #define PIXEL12_31 Interp3(dp+dpL+2, w[5], w[2]); #define PIXEL12_32 Interp3(dp+dpL+2, w[5], w[6]); #define PIXEL12_70 Interp7(dp+dpL+2, w[5], w[6], w[2]); #define PIXEL13_0 *(dp+dpL+3) = w[5]; #define PIXEL13_10 Interp1(dp+dpL+3, w[5], w[3]); #define PIXEL13_12 Interp1(dp+dpL+3, w[5], w[6]); #define PIXEL13_14 Interp1(dp+dpL+3, w[6], w[5]); #define PIXEL13_21 Interp2(dp+dpL+3, w[6], w[5], w[2]); #define PIXEL13_31 Interp3(dp+dpL+3, w[5], w[2]); #define PIXEL13_50 Interp5(dp+dpL+3, w[6], w[5]); #define PIXEL13_60 Interp6(dp+dpL+3, w[5], w[6], w[2]); #define PIXEL13_61 Interp6(dp+dpL+3, w[5], w[6], w[3]); #define PIXEL13_82 Interp8(dp+dpL+3, w[5], w[6]); #define PIXEL13_83 Interp8(dp+dpL+3, w[6], w[2]); #define PIXEL20_0 *(dp+dpL+dpL) = w[5]; #define PIXEL20_10 Interp1(dp+dpL+dpL, w[5], w[7]); #define PIXEL20_12 Interp1(dp+dpL+dpL, w[5], w[4]); #define PIXEL20_14 Interp1(dp+dpL+dpL, w[4], w[5]); #define PIXEL20_21 Interp2(dp+dpL+dpL, w[4], w[5], w[8]); #define PIXEL20_31 Interp3(dp+dpL+dpL, w[5], w[8]); #define PIXEL20_50 Interp5(dp+dpL+dpL, w[4], w[5]); #define PIXEL20_60 Interp6(dp+dpL+dpL, w[5], w[4], w[8]); #define PIXEL20_61 Interp6(dp+dpL+dpL, w[5], w[4], w[7]); #define PIXEL20_82 Interp8(dp+dpL+dpL, w[5], w[4]); #define PIXEL20_83 Interp8(dp+dpL+dpL, w[4], w[8]); #define PIXEL21_0 *(dp+dpL+dpL+1) = w[5]; #define PIXEL21_30 Interp3(dp+dpL+dpL+1, w[5], w[7]); #define PIXEL21_31 Interp3(dp+dpL+dpL+1, w[5], w[8]); #define PIXEL21_32 Interp3(dp+dpL+dpL+1, w[5], w[4]); #define PIXEL21_70 Interp7(dp+dpL+dpL+1, w[5], w[4], w[8]); #define PIXEL22_0 *(dp+dpL+dpL+2) = w[5]; #define PIXEL22_30 Interp3(dp+dpL+dpL+2, w[5], w[9]); #define PIXEL22_31 Interp3(dp+dpL+dpL+2, w[5], w[6]); #define PIXEL22_32 Interp3(dp+dpL+dpL+2, w[5], w[8]); #define PIXEL22_70 Interp7(dp+dpL+dpL+2, w[5], w[6], w[8]); #define PIXEL23_0 *(dp+dpL+dpL+3) = w[5]; #define PIXEL23_10 Interp1(dp+dpL+dpL+3, w[5], w[9]); #define PIXEL23_11 Interp1(dp+dpL+dpL+3, w[5], w[6]); #define PIXEL23_13 Interp1(dp+dpL+dpL+3, w[6], w[5]); #define PIXEL23_21 Interp2(dp+dpL+dpL+3, w[6], w[5], w[8]); #define PIXEL23_32 Interp3(dp+dpL+dpL+3, w[5], w[8]); #define PIXEL23_50 Interp5(dp+dpL+dpL+3, w[6], w[5]); #define PIXEL23_60 Interp6(dp+dpL+dpL+3, w[5], w[6], w[8]); #define PIXEL23_61 Interp6(dp+dpL+dpL+3, w[5], w[6], w[9]); #define PIXEL23_81 Interp8(dp+dpL+dpL+3, w[5], w[6]); #define PIXEL23_83 Interp8(dp+dpL+dpL+3, w[6], w[8]); #define PIXEL30_0 *(dp+dpL+dpL+dpL) = w[5]; #define PIXEL30_11 Interp1(dp+dpL+dpL+dpL, w[5], w[8]); #define PIXEL30_12 Interp1(dp+dpL+dpL+dpL, w[5], w[4]); #define PIXEL30_20 Interp2(dp+dpL+dpL+dpL, w[5], w[8], w[4]); #define PIXEL30_50 Interp5(dp+dpL+dpL+dpL, w[8], w[4]); #define PIXEL30_80 Interp8(dp+dpL+dpL+dpL, w[5], w[7]); #define PIXEL30_81 Interp8(dp+dpL+dpL+dpL, w[5], w[8]); #define PIXEL30_82 Interp8(dp+dpL+dpL+dpL, w[5], w[4]); #define PIXEL31_0 *(dp+dpL+dpL+dpL+1) = w[5]; #define PIXEL31_10 Interp1(dp+dpL+dpL+dpL+1, w[5], w[7]); #define PIXEL31_11 Interp1(dp+dpL+dpL+dpL+1, w[5], w[8]); #define PIXEL31_13 Interp1(dp+dpL+dpL+dpL+1, w[8], w[5]); #define PIXEL31_21 Interp2(dp+dpL+dpL+dpL+1, w[8], w[5], w[4]); #define PIXEL31_32 Interp3(dp+dpL+dpL+dpL+1, w[5], w[4]); #define PIXEL31_50 Interp5(dp+dpL+dpL+dpL+1, w[8], w[5]); #define PIXEL31_60 Interp6(dp+dpL+dpL+dpL+1, w[5], w[8], w[4]); #define PIXEL31_61 Interp6(dp+dpL+dpL+dpL+1, w[5], w[8], w[7]); #define PIXEL31_81 Interp8(dp+dpL+dpL+dpL+1, w[5], w[8]); #define PIXEL31_83 Interp8(dp+dpL+dpL+dpL+1, w[8], w[4]); #define PIXEL32_0 *(dp+dpL+dpL+dpL+2) = w[5]; #define PIXEL32_10 Interp1(dp+dpL+dpL+dpL+2, w[5], w[9]); #define PIXEL32_12 Interp1(dp+dpL+dpL+dpL+2, w[5], w[8]); #define PIXEL32_14 Interp1(dp+dpL+dpL+dpL+2, w[8], w[5]); #define PIXEL32_21 Interp2(dp+dpL+dpL+dpL+2, w[8], w[5], w[6]); #define PIXEL32_31 Interp3(dp+dpL+dpL+dpL+2, w[5], w[6]); #define PIXEL32_50 Interp5(dp+dpL+dpL+dpL+2, w[8], w[5]); #define PIXEL32_60 Interp6(dp+dpL+dpL+dpL+2, w[5], w[8], w[6]); #define PIXEL32_61 Interp6(dp+dpL+dpL+dpL+2, w[5], w[8], w[9]); #define PIXEL32_82 Interp8(dp+dpL+dpL+dpL+2, w[5], w[8]); #define PIXEL32_83 Interp8(dp+dpL+dpL+dpL+2, w[8], w[6]); #define PIXEL33_0 *(dp+dpL+dpL+dpL+3) = w[5]; #define PIXEL33_11 Interp1(dp+dpL+dpL+dpL+3, w[5], w[6]); #define PIXEL33_12 Interp1(dp+dpL+dpL+dpL+3, w[5], w[8]); #define PIXEL33_20 Interp2(dp+dpL+dpL+dpL+3, w[5], w[8], w[6]); #define PIXEL33_50 Interp5(dp+dpL+dpL+dpL+3, w[8], w[6]); #define PIXEL33_80 Interp8(dp+dpL+dpL+dpL+3, w[5], w[9]); #define PIXEL33_81 Interp8(dp+dpL+dpL+dpL+3, w[5], w[6]); #define PIXEL33_82 Interp8(dp+dpL+dpL+dpL+3, w[5], w[8]); inline bool Diff(uint32_t w1, uint32_t w2) { YUV1 = RGBtoYUV[w1]; YUV2 = RGBtoYUV[w2]; return ( ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) || ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) || ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) ); } void hq4x_32( uint8_t * pIn, uint8_t * pOut, int Xres, int Yres, int dpL ) { int i, j, k; int prevline, nextline; int w[10]; uint32_t *sp = (uint32_t *) pIn; uint32_t *dp = (uint32_t *) pOut; // +----+----+----+ // | | | | // | w1 | w2 | w3 | // +----+----+----+ // | | | | // | w4 | w5 | w6 | // +----+----+----+ // | | | | // | w7 | w8 | w9 | // +----+----+----+ for (j=0; j<Yres; j++) { if (j>0) prevline = -Xres; else prevline = 0; if (j<Yres-1) nextline = Xres; else nextline = 0; for (i=0; i<Xres; i++) { w[2] = *(sp + prevline); w[5] = *sp; w[8] = *(sp + nextline); if (i>0) { w[1] = *(sp + prevline - 1); w[4] = *(sp - 1); w[7] = *(sp + nextline - 1); } else { w[1] = w[2]; w[4] = w[5]; w[7] = w[8]; } if (i<Xres-1) { w[3] = *(sp + prevline + 1); w[6] = *(sp + 1); w[9] = *(sp + nextline + 1); } else { w[3] = w[2]; w[6] = w[5]; w[9] = w[8]; } int pattern = 0; int flag = 1; YUV1 = RGBtoYUV[w[5]]; for (k=1; k<=9; k++) { if (k==5) continue; if ( w[k] != w[5] ) { YUV2 = RGBtoYUV[w[k]]; if ( ( abs((YUV1 & Ymask) - (YUV2 & Ymask)) > trY ) || ( abs((YUV1 & Umask) - (YUV2 & Umask)) > trU ) || ( abs((YUV1 & Vmask) - (YUV2 & Vmask)) > trV ) ) pattern |= flag; } flag <<= 1; } switch (pattern) { // omitted for brevity } sp++; dp += 4; } dp += (dpL * 3); } } void InitLUTs(void) { int r, g, b, Y, u, v; for (r = 0; r < 256; r++) { for (g = 0; g < 256; g++) { for (b = 0; b < 256; b++) { Y = (int) (0.299*r + 0.587*g + 0.114*b); u = (int) (128 - 0.169*r - 0.331*g + 0.5*b); // cb v = (int) (128 + 0.5*r - 0.419*g - 0.081*b); // cr //Y = (int)( 0.256788f*r + 0.504129f*g + 0.097906f*b + 0.5f) + 16; //u = (int)(-0.148223f*r - 0.290993f*g + 0.439216f*b + 0.5f) + 128; //v = (int)( 0.439216f*r - 0.367788f*g - 0.071427f*b + 0.5f) + 128; RGBtoYUV[ (r << 16) + (g << 8) + b ] = (Y << 16) + (u << 8) + v; } } } } int main(int argc, char* argv[]) { int nRes; CImage ImageIn; CImage ImageOut; char * szFilenameIn; char * szFilenameOut; if (argc <= 2) { printf("\nUsage: hq4x.exe input.bmp output.bmp\n"); printf("supports .bmp and .tga formats\n"); return 1; } szFilenameIn = argv[1]; szFilenameOut = argv[2]; struct stat st; if ( stat( szFilenameIn, &st ) == -1 ) { printf( "ERROR: file '%s'\n not found", szFilenameIn ); return 1; } if ( ImageIn.Load( szFilenameIn ) != 0 ) { printf( "ERROR: can't load '%s'\n", szFilenameIn ); return 1; } printf( "\n%s is %ix%ix%i\n", szFilenameIn, ImageIn.m_Xres, ImageIn.m_Yres, ImageIn.m_BitPerPixel ); if ( ImageIn.m_BitPerPixel != 32 ) { if ( ImageIn.ConvertTo32() != 0 ) { printf( "ERROR: '%s' conversion to 16 bit failed\n", szFilenameIn ); return 1; } } if ( ImageOut.Init( ImageIn.m_Xres*4, ImageIn.m_Yres*4, 32 ) != 0 ) { printf( "ERROR: ImageOut.Init()\n" ); return 1; }; InitLUTs(); hq4x_32( ImageIn.m_pBitmap, ImageOut.m_pBitmap, ImageIn.m_Xres, ImageIn.m_Yres, ImageOut.m_Xres ); nRes = ImageOut.Save( szFilenameOut ); if ( nRes != 0 ) { printf( "ERROR %i: ImageOut.Save(\"%s\")\n", nRes, szFilenameOut ); return nRes; } printf( "%s is %ix%ix%i\n", szFilenameOut, ImageOut.m_Xres, ImageOut.m_Yres, ImageOut.m_BitPerPixel ); printf( "\nOK\n" ); return 0; }
Private
[
?
]
Run code
Submit