/************************************************************************************************/ /* */ /* Program Name : bmp_lanczos3_down_resize_ver**.c */ /* */ /* Date : 06.05.10 New Create */ /* */ /* Author : Takashi Nakajima */ /* */ /* Function : 任意倍率縮小 */ /* */ /* Limitation : bitmap 24bit true color only */ /* */ /* ver01 2007.03.06 : New Create */ /* */ /* ver03 2007.06.20 : いらない記述削除 */ /* */ /* ver04 2007.06.20 : 30tap固定をlanczos 係数0までにtap数を適応的に変化させる */ /* */ /* ver05 2011.01.17 : 鏡像処理を端の画素値で延長に変更、縮小率バグ修正 */ /* */ /************************************************************************************************/ #include #include #include #include #include #define uchar unsigned char #define ushort unsigned short #define uint unsigned int #define ulong unsigned long #define rchar register char #define rshort register short #define rint register int #define rlong register long #define ruchar register uchar #define rushort register ushort #define ruint register uint #define rulong register ulong #define schar static char #define sshhort static short #define sint static int #define slong static long #define suchar static uchar #define sushort static ushort #define suint static uint #define sulong static ulong #define ORG_IMAGE_X 720 #define ORG_IMAGE_Y 480 typedef struct control_data { uint sizex; uint sizey; uint startx; uint starty; char InputFileName[128]; char InputFileName2[128]; char OutputFileName[128]; uint hv; uint sm; uint smy; uint bunshi; uint bunbo; uint vbunshi; uint vbunbo; uint norm; uint mado; uint tapnum_y; uint tapnum_c; double Fs; } CNTL; char inputfile[50]; char inputfile2[50]; char outputfile[50]; int hsize; int vsize; int org_image_x; int org_image_y; void resize( int hsize, int vsize, int bunshi, int bunbo, int vbunshi, int vbunbo, unsigned char *vin_buffer , unsigned char *vout); void hresize( int hsize , int vsize, int bunshi , int bunbo , double *in , double *out); void vresize( int hsize , int vsize, int bunshi , int bunbo , double *in , double *out); void print_help( void ) { fprintf( stderr, "Usage: Lanczos3 Down Resize For Bitmap [options]\n" ); fprintf( stderr, "\toptions: \n" ); fprintf( stderr, "\t -help : help\n" ); fprintf( stderr, "\t -i : Input File Name(BMP24bit truecolor)\n" ); fprintf( stderr, "\t -o : Output File Name(BMP24bit truecolor .bmp extention is automatically added.\n" ); fprintf( stderr, "\t -bunshi : suihei syukusyouritsu bunshi(suihei syukusyou go no gasosuu)\n" ); fprintf( stderr, "\t -bunbo : suihei syukusyouritsu bunbo(suihei syukusyou mae no gasosuu\n" ); fprintf( stderr, "\t -vbunshi : suityoku syukusyouritsu bunshi(suityoku syukusyou go no gasosuu)\n" ); fprintf( stderr, "\t -vbunbo : suityoku syukusyouritsu bunbo(suityoku syukusyou mae no gasosuu)\n" ); } void option_set(short argc,char *argv[], CNTL *ptr) { short i; for(i=1;i < argc;i++) { if(argv[i][0] != '-') continue; if ( !strcmp( &argv[i][1], "h" ) ){ print_help(); exit(0); } else if ( !strcmp( &argv[i][1], "o" ) ){ strcpy(ptr->OutputFileName, argv[++i]); } else if ( !strcmp( &argv[i][1], "i" ) ){ strcpy(ptr->InputFileName, argv[++i]); } else if ( !strcmp( &argv[i][1], "bunshi" ) ){ ptr->bunshi = atoi(argv[++i]); } else if ( !strcmp( &argv[i][1], "bunbo" ) ){ ptr->bunbo = atoi(argv[++i]); } else if ( !strcmp( &argv[i][1], "vbunshi" ) ){ ptr->vbunshi = atoi(argv[++i]); } else if ( !strcmp( &argv[i][1], "vbunbo" ) ){ ptr->vbunbo = atoi(argv[++i]); } else if ( !strcmp( &argv[i][1], "orgx" ) ){ org_image_x = atoi(argv[++i]); } else if ( !strcmp( &argv[i][1], "orgy" ) ){ org_image_y = atoi(argv[++i]); } else { print_help(); exit(0); } } } int main(int argc, char *argv[]){ FILE *fpr1; FILE *fpr2; FILE *fpw; unsigned char *vin; unsigned char *vout; unsigned char *vin_buffer; int i,j,k,l,m,n,o ; CNTL *cnt; int center0; int sizeminus; int filesizebyte; unsigned char *B; unsigned char *G; unsigned char *R; unsigned char *BIN; unsigned char *GIN; unsigned char *RIN; unsigned char *RUPOUT; unsigned char *GUPOUT; unsigned char *BUPOUT; unsigned char temp; unsigned char temp2; unsigned int filesize; if( argc < 2 ) { print_help(); exit(0); } cnt = (CNTL *)malloc( sizeof(CNTL)*sizeof(uchar) ); option_set(argc, argv, cnt); strcpy(inputfile,cnt->InputFileName); strcpy(inputfile2,cnt->InputFileName2); strcpy(outputfile,cnt->OutputFileName); sizeminus = cnt->sm; /********************************/ /* */ /* MAIN ROUTINE */ /* */ /********************************/ if((fpr1 = fopen(inputfile,"rb"))==NULL){ exit(0); } /***************************************************/ /* picture size & file size get from bitmap header */ /***************************************************/ fseek(fpr1,18,SEEK_SET); fread(&hsize,sizeof(int),1,fpr1); fread(&vsize,sizeof(int),1,fpr1); fseek(fpr1,2,SEEK_SET); fread(&filesizebyte,sizeof(int),1,fpr1); /***************/ /* picture get */ /***************/ vin = (unsigned char *)malloc(sizeof(unsigned char)*(filesizebyte-54)); B = (unsigned char *)malloc(sizeof(unsigned char)*hsize*vsize); G = (unsigned char *)malloc(sizeof(unsigned char)*hsize*vsize); R = (unsigned char *)malloc(sizeof(unsigned char)*hsize*vsize); fseek(fpr1,54,SEEK_SET); if(fread(vin,sizeof(unsigned char),filesizebyte - 54,fpr1) != filesizebyte -54 ){ printf("input file size err!!\n"); exit(1); } fclose(fpr1); /***************************************/ /* set to B,G,R array with RGB PICTURE */ /***************************************/ for(i=0,k=0;ibunshi/cnt->bunbo, hsize, vsize*cnt->vbunshi/cnt->vbunbo, vsize, RIN , RUPOUT); resize( hsize, vsize, hsize*cnt->bunshi/cnt->bunbo, hsize, vsize*cnt->vbunshi/cnt->vbunbo, vsize, GIN , GUPOUT); resize( hsize, vsize, hsize*cnt->bunshi/cnt->bunbo, hsize, vsize*cnt->vbunshi/cnt->vbunbo, vsize, BIN , BUPOUT); sprintf(cnt->OutputFileName,"%s.bmp",outputfile); fpw = fopen(cnt->OutputFileName,"wb"); hsize = hsize*cnt->bunshi/cnt->bunbo ; vsize = vsize*cnt->vbunshi/cnt->vbunbo ; /************************/ /* HEADER 54 byte write */ /************************/ if(((hsize*3)%4) == 0){ filesize = 0x36 + hsize*vsize*3; } else { filesize = 0x36 + hsize*vsize*3+(4-(hsize*3)%4)*vsize; } temp = 0x42; //'B' fwrite(&temp,sizeof(unsigned char),1,fpw); temp = 0x4D; //'M' fwrite(&temp,sizeof(unsigned char),1,fpw); fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0; fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0x00000036; fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0x28; fwrite(&filesize,sizeof(unsigned int),1,fpw); fwrite(&hsize,sizeof(int),1,fpw); fwrite(&vsize,sizeof(int),1,fpw); filesize = 0x00180001; fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0; fwrite(&filesize,sizeof(unsigned int),1,fpw); fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0x00000EC4; fwrite(&filesize,sizeof(unsigned int),1,fpw); fwrite(&filesize,sizeof(unsigned int),1,fpw); filesize = 0; fwrite(&filesize,sizeof(unsigned int),1,fpw); fwrite(&filesize,sizeof(unsigned int),1,fpw); temp2 =0; for(i=0;i 255.0){ *(VOUTI+j*vsize*vbunshi/vbunbo+i) = 255.0; } *(vout+j*vsize*vbunshi/vbunbo+i) = (unsigned char)(*(VOUTI+j*vsize*vbunshi/vbunbo+i)); } } }// resize kansuu end void hresize( int hsize , int vsize, int bunshi , int bunbo , double *in , double *out) { int TAPNUM = 30; int i,j,k,l,m,n; double center_left_iti; double center_right_iti; double temp; double iti; double x; double y; int temp2; double *KEISUU; double KEISUU_GOUKEI; double PI = atan(1)*4.0; // if( ((int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1) % 2 == 1 ){ TAPNUM = (int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1+1; //debug start // printf("奇数\n"); //debug end // } else { // TAPNUM = (int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1; //debug start // printf("偶数\n"); //debug end // } //debug start // printf("TAPNUM=%d\n",TAPNUM); //debug end KEISUU = (double *)malloc(sizeof(double)*TAPNUM); for(i=0;i=-3.0) && (y <= 3.0)){ *(KEISUU+TAPNUM/2-1-k) = sin(x/3.0)/x*3.0*sin(x)/x; } else { *(KEISUU+TAPNUM/2-1-k) = 0.0; } } for(k=0;k=-3.0) && (y <= 3.0)){ *(KEISUU+k+TAPNUM/2) = sin(x/3.0)/x*3.0*sin(x)/x; } else { *(KEISUU+k+TAPNUM/2) = 0.0; } } KEISUU_GOUKEI = 0.0; for(k=0;k=hsize){ l= hsize-1/*2*(hsize-1) - l*/; } *(out+j*vsize+i) += (*(KEISUU+k))*(*(in+l*vsize+i)); } } } } void vresize( int hsize , int vsize, int bunshi , int bunbo , double *in , double *out) { int i,j,k,l,m,n; int TAPNUM = 30; double center_left_iti; double center_right_iti; double temp; double iti; double x; double y; int temp2; double *KEISUU; double KEISUU_GOUKEI; double PI = atan(1)*4.0; // if( ((int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1) % 2 == 1 ){ TAPNUM = (int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1+1; //debug start // printf("奇数\n"); //debug end // } else { // TAPNUM = (int)(3.0/ ((double)(bunshi)/(double)(bunbo)) +1.0)*2+1; //debug start // printf("偶数\n"); //debug end // } KEISUU = (double *)malloc(sizeof(double)*TAPNUM); for(i=0;i= -3.0) && (y <= 3.0)){ *(KEISUU+TAPNUM/2-1-k) = sin(x/3.0)/x*3.0*sin(x)/x; } else { *(KEISUU+TAPNUM/2-1-k) = 0.0; } } for(k=0;k= -3.0) && (y <= 3.0)) { *(KEISUU+k+TAPNUM/2) = sin(x/3.0)/x*3.0*sin(x)/x; } else { *(KEISUU+k+TAPNUM/2) = 0.0; } } KEISUU_GOUKEI = 0.0; for(k=0;k=vsize){ l= vsize-1/*2*(vsize-1) - l*/; } *(out+i*vsize*bunshi/bunbo+j) += (*(KEISUU+k))*(*(in+i*vsize+l)); } } } }