/*************************************************************************************** * convert_raw.c * * * * * * WRITTEN BY: Greg Porreca (Church Lab) 10-16-2005 * * * * * ***************************************************************************************/ #include #include #include #include #include #include #include #include #include using namespace std; //function prototypes int scandir_sel(struct dirent * ent); //global variables int STRIPSIZE = 8000; int STRIPMAX = 250; int main(int argc, char *argv[]){ FILE *infile; TIFF *outfile; struct dirent **namelist; char *tiff_img_pointer; char dirname[500]; char curr_raw_filename[500]; char curr_tif_filename[500]; int num_imgs, i, j; int imageOffset, result; short unsigned int curr_img[1000000]; if(argc != 2){ fprintf(stdout, "\n"); fprintf(stdout, "To use find_objects, execute as follows:\n"); fprintf(stdout, " ./convert_raw dirname/\n"); fprintf(stdout, "\n"); exit(0); } strcpy(dirname, ""); strcat(dirname, argv[1]); //turn annoying TIFF warnings off TIFFSetWarningHandler(0); num_imgs= scandir(dirname, &namelist, (int (*)(const struct dirent *))(scandir_sel), alphasort); for(i=0; id_name); strcpy(curr_tif_filename, ""); strncat(curr_tif_filename, curr_raw_filename, strlen(curr_raw_filename)-4); strcat(curr_tif_filename, ".tif"); fprintf(stdout, "%s -> %s\n", curr_raw_filename, curr_tif_filename); //open input and output files if((infile = fopen(curr_raw_filename, "r")) == NULL){ fprintf(stdout, "Could not open incoming raw file %s\n", curr_raw_filename); exit(42); } if((outfile = TIFFOpen(curr_tif_filename, "w")) == NULL){ fprintf(stdout, "Could not open outgoing image %s\n", curr_tif_filename); exit(42); } //load raw image data into memory for(j=0; j<1000000; j++){ if( (fread(&curr_img[j], sizeof(short unsigned int), 1, infile)) < 1){ fprintf(stdout, "ERROR reading from raw file %s; failed at pixel %d\n", curr_raw_filename, j); exit(42); } } fclose(infile); //set tiff image parameters TIFFSetField(outfile, TIFFTAG_IMAGEWIDTH, 1000); TIFFSetField(outfile, TIFFTAG_IMAGELENGTH, 1000); TIFFSetField(outfile, TIFFTAG_BITSPERSAMPLE, 16); TIFFSetField(outfile, TIFFTAG_ROWSPERSTRIP, 4); TIFFSetField(outfile, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); TIFFSetField(outfile, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(outfile, TIFFTAG_XRESOLUTION, 72.0); TIFFSetField(outfile, TIFFTAG_YRESOLUTION, 72.0); TIFFSetField(outfile, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH); // Write the data imageOffset = 0; tiff_img_pointer = (char*)curr_img; //TIFFWriteEncodedStrip takes a char*, not a short unsigned int* for (j = 0; j < STRIPMAX; j++){//iterate over the number of strips if((result = TIFFWriteEncodedStrip (outfile, j, tiff_img_pointer + imageOffset, STRIPSIZE)) == -1){ fprintf(stdout, "Write error on input strip number %d\n", j); exit(42); } imageOffset += result; } TIFFClose(outfile); } free(namelist); } //only returns files matching FILEMASK int scandir_sel(struct dirent * ent){ char* FILEMASK = "*.raw"; if(1) return(!fnmatch(FILEMASK, ent->d_name, 0)); return(0); }