/* subset_image.c ** This program is used to subset a raw image file ** (i.e. containing of no header) ** (sometimes such images also called generic binary image) ** */ #include #include #include main() { FILE *fin, *fout; unsigned int img_row; unsigned int img_col, step; int sub_col, sub_row, x_up, y_up, x_low, y_low; int count1, count2; unsigned char *store, *sub_row_start, in_file[50], out_file[50]; printf("\n====Subset an image====\n"); printf("Please input the number for image rows:"); scanf("%d",&img_row); printf("Please input the number for image columns:"); scanf("%d",&img_col); printf("Please input upper left co-ordinates for subset starting point:");// img_width = atoi(argv[4]); printf("x="); scanf("%d",&x_up); printf("y="); scanf("%d",&y_up); printf("Please input down right co-ordinates for subset ending point:");// img_width = atoi(argv[4]); printf("x="); scanf("%d",&x_low); printf("y="); scanf("%d",&y_low); printf("Input image file:"); scanf("%s",in_file); if((fin = fopen(in_file,"rb")) == NULL) { printf("Failed to open %s\n",in_file); exit(EXIT_FAILURE); } printf("Ouput image file:"); scanf("%s",out_file); if((fout = fopen(out_file,"a")) == NULL) { printf("Failed to open %s\n",out_file); exit(EXIT_FAILURE); } printf("Input completed, start calculating...\n"); sub_col = x_low - x_up + 1; sub_row = y_low - y_up + 1; store = (unsigned char *)malloc(img_col); sub_row_start = (unsigned char *)malloc(sub_col); for(count1 = 0; count1 < y_up; count1++) fseek(fin,img_col,1); if(x_up != 0) fseek(fin,x_up,1); step = img_col-sub_col; for(count2 = 0;count2 < sub_row; count2++) { fread(sub_row_start,1,sub_col,fin); fwrite(sub_row_start,1,sub_col,fout); fseek(fin,step,1); } fclose(fin); fclose(fout); free(store); free(sub_row_start); printf("\n===Running completed!\n"); printf("\nImage %s with row = %d, column = %d!\n",out_file, sub_row, sub_col); }