From: Neszt Tibor Date: Tue, 11 Jun 2013 16:17:21 +0000 (+0200) Subject: styling fixes X-Git-Tag: v-0.1 X-Git-Url: http://git.neszt.hu/?a=commitdiff_plain;h=c182767348c721cbccb246665ee2c73a8a928a85;p=img-merge.git styling fixes --- diff --git a/img_merge.c b/img_merge.c index 0860bc4..6a5eb6a 100644 --- a/img_merge.c +++ b/img_merge.c @@ -6,10 +6,9 @@ #include "img_merge.h" #include "stdlib.h" -/* ----------------------------------------------- */ -void print_help(void) { +void print_help(void) {/*{{{*/ - printf("Usage: img_merge -p1 pic1 -c1 brightnes1 .. .. -s step -o outbasename\n"); + printf("Usage: img_merge -p1 pic1 -c1 brightnes1 .. .. -s step -o outbasename\n"); printf(" Usage: img_merge\n"); printf(" -n number of images to merge (1-999)\n"); @@ -33,307 +32,305 @@ void print_help(void) { printf("\n"); printf(" those p[n]s, what is not defined will be automatically\n"); printf(" set correspondingly as the last value. // later on!\n"); -} -/* ----------------------------------------------- */ -void scan_args(int argc, char **argv) { - int i, j, r, tmpint; +}/*}}}*/ - #if debug>1 - printf("* Debug * Arguments being processed\n"); - #endif - for ( i=1 ; i i + 1 && isdigit(argv[i+1][0]) ) { - if ( num ) { - printf("Only one -n parameter allowed!\n", num); - exit(0); - } - if ( !p_base[0] ) { - printf("You must give -p firstpic before -n!\n", num); - exit(0); - } - num = atoi(&argv[i+1][0]); - if ( num > maxn ) { - printf("Then given -n parameter is over the maximun: %d\n", maxn); - exit(0); - } - #if debug>1 - printf("* Debug * Found -n: %d\n", num); - #endif - i++; - for ( j = 0 ; j < num ; j++ ) { - sprintf(p[j+1], "%s", p_base); - increment_str(p_base); - } - } - break; + #if debug>1 + printf("* Debug * Arguments being processed\n"); + #endif + for ( i=1 ; i1 - printf("* Debug * Found -p%d : %s\n", j, p[j]); - #endif - } else if ( argc>i+1 ) { - sprintf(p_base, "%s", argv[i+1]); - #if debug>1 - printf("* Debug * Found -p global: %s\n", argv[i+1]); - #endif - } - break; + for ( i = 1 ; i < argc ; i++ ) { + switch( argv[i][0] ) { + case '-': + switch( argv[i][1] ) { + case 'n': + if ( argc > i + 1 && isdigit(argv[i+1][0]) ) { + if ( num ) { + printf("Only one -n parameter allowed!\n", num); + exit(0); + } + if ( !p_base[0] ) { + printf("You must give -p firstpic before -n!\n", num); + exit(0); + } + num = atoi(&argv[i+1][0]); + if ( num > maxn ) { + printf("Then given -n parameter is over the maximun: %d\n", maxn); + exit(0); + } + #if debug>1 + printf("* Debug * Found -n: %d\n", num); + #endif + i++; + for ( j = 0 ; j < num ; j++ ) { + sprintf(p[j+1], "%s", p_base); + increment_str(p_base); + } + } + break; - case 'c': - #if debug>1 - printf("* Debug * Found -c : "); - #endif - if ( isdigit(argv[i][2]) && argc>i+1 && isdigit(argv[i+1][0]) ) { - #if debug>1 - printf("* Debug * Found -c%d : %d\n", atoi(&argv[i][2]), atoi(&argv[i+1][0])); - #endif - c[ atoi(&argv[i][2]) ] = atoi(&argv[i+1][0]), i++; - } else if ( argc>i+1 && isdigit(argv[i+1][0]) ) { - r = atoi( &argv[i+1][0] ); - for ( j = 0 ; j < maxn ; j++ ) { c[ j ] = r; }; - i++; - #if debug>1 - printf("* Debug * Found -c for all numbers : %d\n", r); - #endif - } else if ( argc>i+1 && argv[i+1][0] == 'a' ) { - c[0] = -1; - i++; - } - break; + case 'p': + if ( isdigit(argv[i][2]) ) { + j = atoi(&argv[i][2]); + sprintf(p[j], "%s", argv[i+1]); + i++, num++; + #if debug>1 + printf("* Debug * Found -p%d : %s\n", j, p[j]); + #endif + } else if ( argc>i+1 ) { + sprintf(p_base, "%s", argv[i+1]); + #if debug>1 + printf("* Debug * Found -p global: %s\n", argv[i+1]); + #endif + } + break; - case 't': - #if debug>1 - printf("* Debug * Found -t\n"); - #endif - if ( argc>=i+1 ) { - if ( argv[i+1][0] == 'S' || argv[i+1][0] == 's' ) { - t = 0; - } else if ( argv[i+1][0] == 'X' || argv[i+1][0] == 'x' ) { - t = 1; - } else if ( argv[i+1][0] == 'N' || argv[i+1][0] == 'n' ) { - t = 2; - } else if ( argv[i+1][0] == 'A' || argv[i+1][0] == 'a' ) { - t = 3; - } else if ( argv[i+1][0] == 'D' || argv[i+1][0] == 'd' ) { - t = 4; - } else { - printf("Then given -t merge type (%s) is unknown!\n", &argv[i+1][0]); - exit(0); - } - #if debug>1 - printf("* Debug * Found -t : %c\n", argv[i+1][0]); - #endif - } - break; + case 'c': + #if debug>1 + printf("* Debug * Found -c : "); + #endif + if ( isdigit(argv[i][2]) && argc>i+1 && isdigit(argv[i+1][0]) ) { + #if debug>1 + printf("* Debug * Found -c%d : %d\n", atoi(&argv[i][2]), atoi(&argv[i+1][0])); + #endif + c[ atoi(&argv[i][2]) ] = atoi(&argv[i+1][0]), i++; + } else if ( argc>i+1 && isdigit(argv[i+1][0]) ) { + r = atoi( &argv[i+1][0] ); + for ( j = 0 ; j < maxn ; j++ ) { c[ j ] = r; }; + i++; + #if debug>1 + printf("* Debug * Found -c for all numbers : %d\n", r); + #endif + } else if ( argc>i+1 && argv[i+1][0] == 'a' ) { + c[0] = -1; + i++; + } + break; - case 's': - if ( isdigit(argv[i+1][0]) ) { - #if debug>1 - printf("* Debug * Found -s : %d\n", atoi(&argv[i+1][0])); - #endif - step = atoi(&argv[i+1][0]), i++; - } - break; + case 't': + #if debug>1 + printf("* Debug * Found -t\n"); + #endif + if ( argc>=i+1 ) { + if ( argv[i+1][0] == 'S' || argv[i+1][0] == 's' ) { + t = 0; + } else if ( argv[i+1][0] == 'X' || argv[i+1][0] == 'x' ) { + t = 1; + } else if ( argv[i+1][0] == 'N' || argv[i+1][0] == 'n' ) { + t = 2; + } else if ( argv[i+1][0] == 'A' || argv[i+1][0] == 'a' ) { + t = 3; + } else if ( argv[i+1][0] == 'D' || argv[i+1][0] == 'd' ) { + t = 4; + } else { + printf("Then given -t merge type (%s) is unknown!\n", &argv[i+1][0]); + exit(0); + } + #if debug>1 + printf("* Debug * Found -t : %c\n", argv[i+1][0]); + #endif + } + break; - case 'f': - if ( isdigit(argv[i+1][0]) ) { - #if debug>1 - printf("* Debug * Found -f : %d\n", atoi(&argv[i+1][0])); - #endif - framestep = atoi(&argv[i+1][0]), i++; - } - break; + case 's': + if ( isdigit(argv[i+1][0]) ) { + #if debug>1 + printf("* Debug * Found -s : %d\n", atoi(&argv[i+1][0])); + #endif + step = atoi(&argv[i+1][0]), i++; + } + break; - case 'o': - #if debug>1 - printf("* Debug * Found -o : %s\n",argv[i+1]); - #endif - sprintf(outbasename, "%s", argv[i+1]); - i++; - break; - } - break; - case '?': - print_help(); - break; - default: - break; - } - if ( c[0] == -1 && num > 0 ) { - for ( j = 0 ; j < maxn ; j++ ) { c[ j ] = 100/num; }; - } - } - if (num<2) { - print_help(); - exit(0); - } -} -/* ----------------------------------------- */ -void merge_begin(uint8_t *img, int c) { - int i,j,h3i,j3, pos; + case 'f': + if ( isdigit(argv[i+1][0]) ) { + #if debug>1 + printf("* Debug * Found -f : %d\n", atoi(&argv[i+1][0])); + #endif + framestep = atoi(&argv[i+1][0]), i++; + } + break; - for( i = 0 ; i < w ; i++ ) { - h3i = h * 3 * i; - for( j = 0 ; j < h ; j++ ) { - j3 = 3*j; - pos = h3i + j3; - outbuffer[pos+0] = c*img[pos+0]; - outbuffer[pos+1] = c*img[pos+1]; - outbuffer[pos+2] = c*img[pos+2]; - } - } -} -/* ----------------------------------------- */ -void merge(uint8_t *img, int c) { - int i,j,h3i,j3, pos; + case 'o': + #if debug>1 + printf("* Debug * Found -o : %s\n",argv[i+1]); + #endif + sprintf(outbasename, "%s", argv[i+1]); + i++; + break; + } + break; + case '?': + print_help(); + break; + default: + break; + } + if ( c[0] == -1 && num > 0 ) { + for ( j = 0 ; j < maxn ; j++ ) { c[ j ] = 100 / num; }; + } + } + if (num < 2) { + print_help(); + exit(0); + } +}/*}}}*/ - for( i = 0 ; i < w ; i++ ) { - h3i = h*3*i; - for( j = 0 ; j < h ; j++ ) { - j3 = 3 * j; - pos = h3i + j3; - if ( t == 1 ) { // max - if ( c*img[pos+0] > outbuffer[pos+0] ) { outbuffer[pos+0] = c*img[pos+0]; } - if ( c*img[pos+1] > outbuffer[pos+1] ) { outbuffer[pos+1] = c*img[pos+1]; } - if ( c*img[pos+2] > outbuffer[pos+2] ) { outbuffer[pos+2] = c*img[pos+2]; } - } else if ( t == 2 ) { // min - if ( c*img[pos+0] < outbuffer[pos+0] ) { outbuffer[pos+0] = c*img[pos+0]; } - if ( c*img[pos+1] < outbuffer[pos+1] ) { outbuffer[pos+1] = c*img[pos+1]; } - if ( c*img[pos+2] < outbuffer[pos+2] ) { outbuffer[pos+2] = c*img[pos+2]; } - } else if ( t == 4 ) { // diff - if ( c*img[pos+0] != outbuffer[pos+0] ) { outbuffer[pos+0] += c*img[pos+0]; } else { outbuffer[pos+0] = 0; } - if ( c*img[pos+1] != outbuffer[pos+1] ) { outbuffer[pos+1] += c*img[pos+1]; } else { outbuffer[pos+1] = 0; } - if ( c*img[pos+2] != outbuffer[pos+2] ) { outbuffer[pos+2] += c*img[pos+2]; } else { outbuffer[pos+2] = 0; } - } else { - outbuffer[pos+0] += c*img[pos+0]; - outbuffer[pos+1] += c*img[pos+1]; - outbuffer[pos+2] += c*img[pos+2]; - } - } - } -} -/* ----------------------------------------- */ -void merge_finish(void) { - int i,j,h3i,j3, pos; +void merge_begin(uint8_t *img, int c) {/*{{{*/ + int i,j,h3i,j3, pos; - for( i = 0 ; i < w ; i++ ) { - h3i = h*3*i; - for( j = 0 ; j < h ; j++ ) { - j3 = 3 * j; - pos = h3i + j3; - if ( t == 3 ) { // avg - outimage[pos+0] = (uint8_t) ( outbuffer[pos+2]/100/num <= 255 ? outbuffer[pos+2]/100/num : 255 ); - outimage[pos+1] = (uint8_t) ( outbuffer[pos+1]/100/num <= 255 ? outbuffer[pos+1]/100/num : 255 ); - outimage[pos+2] = (uint8_t) ( outbuffer[pos+0]/100/num <= 255 ? outbuffer[pos+0]/100/num : 255 ); - } else { - outimage[pos+0] = (uint8_t) ( outbuffer[pos+2]/100 <= 255 ? outbuffer[pos+2]/100 : 255 ); - outimage[pos+1] = (uint8_t) ( outbuffer[pos+1]/100 <= 255 ? outbuffer[pos+1]/100 : 255 ); - outimage[pos+2] = (uint8_t) ( outbuffer[pos+0]/100 <= 255 ? outbuffer[pos+0]/100 : 255 ); - } - } - } -} -/* ----------------------------------------------- */ -void increment_str( char *p ) { - int i; + for ( i = 0 ; i < w ; i++ ) { + h3i = h * 3 * i; + for ( j = 0 ; j < h ; j++ ) { + j3 = 3*j; + pos = h3i + j3; + outbuffer[pos+0] = c*img[pos+0]; + outbuffer[pos+1] = c*img[pos+1]; + outbuffer[pos+2] = c*img[pos+2]; + } + } +}/*}}}*/ - for ( i = 0 ; p[i] != '\0' ; i++ ); - i-=5; - while ( (++p[i]) == ('9'+1) ) { - p[i] = '0'; - i--; - } -} -/* ----------------------------------------------- */ -void increment(void) { - int i,j; +void merge(uint8_t *img, int c) {/*{{{*/ + int i, j, h3i, j3, pos; - for ( i = 1 ; i < num + 1 ; i++ ) { - for ( j = 0 ; p[i][j] != '\0' ; j++ ); - j -= 5; - while ( (++p[i][j]) == ('9'+1) ) { - p[i][j] = '0'; - j--; - } - } -} -/* ----------------------------------------- */ -/* ----------------------------------------- */ -/* ----------------------------------------- */ -int main(int argc, char **argv) { - tga_image img1; - tga_image img2; - tga_result result; - int i, j; + for ( i = 0 ; i < w ; i++ ) { + h3i = h * 3 * i; + for ( j = 0 ; j < h ; j++ ) { + j3 = 3 * j; + pos = h3i + j3; + if ( t == 1 ) { // max + if ( c*img[pos+0] > outbuffer[pos+0] ) { outbuffer[pos+0] = c*img[pos+0]; } + if ( c*img[pos+1] > outbuffer[pos+1] ) { outbuffer[pos+1] = c*img[pos+1]; } + if ( c*img[pos+2] > outbuffer[pos+2] ) { outbuffer[pos+2] = c*img[pos+2]; } + } else if ( t == 2 ) { // min + if ( c*img[pos+0] < outbuffer[pos+0] ) { outbuffer[pos+0] = c*img[pos+0]; } + if ( c*img[pos+1] < outbuffer[pos+1] ) { outbuffer[pos+1] = c*img[pos+1]; } + if ( c*img[pos+2] < outbuffer[pos+2] ) { outbuffer[pos+2] = c*img[pos+2]; } + } else if ( t == 4 ) { // diff + if ( c*img[pos+0] != outbuffer[pos+0] ) { outbuffer[pos+0] += c*img[pos+0]; } else { outbuffer[pos+0] = 0; } + if ( c*img[pos+1] != outbuffer[pos+1] ) { outbuffer[pos+1] += c*img[pos+1]; } else { outbuffer[pos+1] = 0; } + if ( c*img[pos+2] != outbuffer[pos+2] ) { outbuffer[pos+2] += c*img[pos+2]; } else { outbuffer[pos+2] = 0; } + } else { + outbuffer[pos+0] += c*img[pos+0]; + outbuffer[pos+1] += c*img[pos+1]; + outbuffer[pos+2] += c*img[pos+2]; + } + } + } +}/*}}}*/ - scan_args(argc, argv); +void merge_finish(void) {/*{{{*/ + int i, j, h3i, j3, pos; - #if debug>1 - printf("* Debug * Checking first file:%s\n",p[1]); - #endif - result = tga_read(&tga_images[1], p[1]); - if (result != TGA_NOERR) { - printf("Error opening first file!\n"); - exit(0); - } - if (!tga_is_top_to_bottom(&tga_images[1])) tga_flip_vert(&tga_images[1]); - w = tga_images[1].width; - h = tga_images[1].height; - #if debug>1 - printf("* Debug * Width:%d , Height:%d\n",w,h); - #endif - tga_free_buffers(&tga_images[1]); + for ( i = 0 ; i < w ; i++ ) { + h3i = h * 3 * i; + for ( j = 0 ; j < h ; j++ ) { + j3 = 3 * j; + pos = h3i + j3; + if ( t == 3 ) { // avg + outimage[pos+0] = (uint8_t) ( outbuffer[pos+2]/100/num <= 255 ? outbuffer[pos+2]/100/num : 255 ); + outimage[pos+1] = (uint8_t) ( outbuffer[pos+1]/100/num <= 255 ? outbuffer[pos+1]/100/num : 255 ); + outimage[pos+2] = (uint8_t) ( outbuffer[pos+0]/100/num <= 255 ? outbuffer[pos+0]/100/num : 255 ); + } else { + outimage[pos+0] = (uint8_t) ( outbuffer[pos+2]/100 <= 255 ? outbuffer[pos+2]/100 : 255 ); + outimage[pos+1] = (uint8_t) ( outbuffer[pos+1]/100 <= 255 ? outbuffer[pos+1]/100 : 255 ); + outimage[pos+2] = (uint8_t) ( outbuffer[pos+0]/100 <= 255 ? outbuffer[pos+0]/100 : 255 ); + } + } + } +}/*}}}*/ - outimage = (uint8_t*) malloc( w*h*3*sizeof(uint8_t) ); - outbuffer = (int*) malloc( w*h*3*sizeof(int) ); +void increment_str(char *p) {/*{{{*/ + int i; - while(step) { - for ( i = 1 ; i < num + 1 ; i += framestep ) { - result = tga_read(&tga_images[i], p[i]); - if (result == TGA_NOERR) { - if (!tga_is_top_to_bottom(&tga_images[i])) tga_flip_vert(&tga_images[i]); - if (!( tga_images[i].width==w && tga_images[i].height==h )) { - printf("Not identical dimensions!\n"); - exit(0); - } - image_datas[i] = tga_images[i].image_data; - if ( i == 1 ) { - merge_begin(image_datas[i], c[i]); - } else { - merge(image_datas[i], c[i]); - } - #if debug>1 - printf("* Debug * Image merged:%s - %d%%\n",p[i],c[i]); - #endif - tga_free_buffers(&tga_images[i]); - } else { - printf("Error opening image:%s!\n",p[i]); - exit(0); - } - } - merge_finish(); - sprintf(outname,"%s%05d.tga",outbasename,outindex++); - tga_write_rgb(outname, (uint8_t *) outimage, w, h, 24); - #if debug>1 - printf("* Debug * Image writen:%s\n",outname); - #endif - for ( j = 0 ; j < step ; j++ ) { increment(); } - } + for ( i = 0 ; p[i] != '\0' ; i++ ); + i -= 5; + while ( (++p[i]) == ('9'+1) ) { + p[i] = '0'; + i--; + } +}/*}}}*/ - free(outimage); - free(outbuffer); +void increment(void) {/*{{{*/ + int i, j; - return 0; -} + for ( i = 1 ; i < num + 1 ; i++ ) { + for ( j = 0 ; p[i][j] != '\0' ; j++ ); + j -= 5; + while ( (++p[i][j]) == ('9' + 1) ) { + p[i][j] = '0'; + j--; + } + } +}/*}}}*/ + +int main(int argc, char **argv) {/*{{{*/ + tga_image img1; + tga_image img2; + tga_result result; + int i, j; + + scan_args(argc, argv); + + #if debug>1 + printf("* Debug * Checking first file:%s\n",p[1]); + #endif + result = tga_read(&tga_images[1], p[1]); + if (result != TGA_NOERR) { + printf("Error opening first file!\n"); + exit(0); + } + if (!tga_is_top_to_bottom(&tga_images[1])) tga_flip_vert(&tga_images[1]); + w = tga_images[1].width; + h = tga_images[1].height; + #if debug>1 + printf("* Debug * Width:%d , Height:%d\n",w,h); + #endif + tga_free_buffers(&tga_images[1]); + + outimage = (uint8_t*) malloc( w*h*3*sizeof(uint8_t) ); + outbuffer = (int*) malloc( w*h*3*sizeof(int) ); + + while(step) { + for ( i = 1 ; i < num + 1 ; i += framestep ) { + result = tga_read(&tga_images[i], p[i]); + if (result == TGA_NOERR) { + if (!tga_is_top_to_bottom(&tga_images[i])) tga_flip_vert(&tga_images[i]); + if (!( tga_images[i].width==w && tga_images[i].height==h )) { + printf("Not identical dimensions!\n"); + exit(0); + } + image_datas[i] = tga_images[i].image_data; + if ( i == 1 ) { + merge_begin(image_datas[i], c[i]); + } else { + merge(image_datas[i], c[i]); + } + #if debug>1 + printf("* Debug * Image merged:%s - %d%%\n",p[i],c[i]); + #endif + tga_free_buffers(&tga_images[i]); + } else { + printf("Error opening image:%s!\n",p[i]); + exit(0); + } + } + merge_finish(); + sprintf(outname,"%s%05d.tga",outbasename,outindex++); + tga_write_rgb(outname, (uint8_t *) outimage, w, h, 24); + #if debug>1 + printf("* Debug * Image writen:%s\n",outname); + #endif + for ( j = 0 ; j < step ; j++ ) { increment(); } + } + + free(outimage); + free(outbuffer); + + return 0; +}/*}}}*/