diff --git a/xraysim/xraysim.cxx b/xraysim/xraysim.cxx new file mode 100644 index 0000000..2e9a265 --- /dev/null +++ b/xraysim/xraysim.cxx @@ -0,0 +1,472 @@ +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include "SimpleGVXR.h" + +#include +#include + +#include + +float version = 3.14; + +// for synthetic noise +#include +std::mt19937 generator; +double mean = 0.0; +double stddev = 1.0; +std::normal_distribution normal(mean, stddev); + +void usage () { + printf("xraysim v%f\n",version); + printf("usage: xraysim [-s(ourcePos) x y z] [-d(etPos) x y z] [-D(detUpVec) x y z]\n"); + printf(" [-e(nergy) MeV] [-S(caleIntensity) min max range] [-n addgaussnoiseperc]\n"); + printf(" [-r(otate-z) degree] [-R(otate) x y z]\n"); + printf(" [-ct(-rotate-z-step) delta_degree] [-cr(-rotate-z-range) degree]\n"); + printf(" [-w pixels] [-h pixels] [-p pixelsize] [-g openglwinsize]\n"); + printf(" [-show(_scene)] [-soft(ware_cpu_renderer)] [-o out[%%04d].tif/pgm]\n"); + printf(" [-u density g/cm3] file.stl [-u density file2.stl]\n"); +} + +long int millitime() { + struct timeval tp; + gettimeofday(&tp, NULL); + long int ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; + return ms; +} + +int savePGM(std::string output,std::vector> image, + float scaleMin, float scaleMax, float scaleRange){ + int iwidth=image.at(0).size(), + iheight=image.size(); + // Save the image into a image file + printf("Saving Xray Image to file %s (Scale min=%f max=%f range=%f)\n", + output.c_str(),scaleMin,scaleMax,scaleRange); + // normalize to X bits / scaleRange + FILE *fd = fopen(output.c_str(),"w+"); + fprintf(fd,"P2\n"); + fprintf(fd,"%d %d\n",image.at(0).size(),image.size()); + fprintf(fd,"%d\n",(int)scaleRange); + int k=0,length=image.size()*image.at(0).size(); + for(int i=0;i> image, + float scaleMin, float scaleMax){ + float scaleRange=65535; + int iwidth=image.at(0).size(), + iheight=image.size(); + printf("Saving Xray Image to file %s (Scale min=%f max=%f range=%f)\n", + output.c_str(),scaleMin,scaleMax,scaleRange); + // Write 16 bit tiff + // Open the TIFF file + TIFF *output_image; + if((output_image = TIFFOpen(output.c_str(), "w")) == NULL){ + std::cerr << "Unable to write tif file: " << output << std::endl; + } + TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, iwidth); + TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, iheight); + TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 1); + TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 16); + TIFFSetField(output_image, TIFFTAG_ROWSPERSTRIP, iheight); + TIFFSetField(output_image, TIFFTAG_ORIENTATION, (int)ORIENTATION_TOPLEFT); + TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); + TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE); + TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); + + tsize_t image_s; + short int *data=(short int*)malloc(iwidth*iheight*2); + int k=0; + for(int i=0;i> image=getLastXRayImage(); + + if (noise!=0.0) { + // add relative gaussian noise (rough approximation of xray noise) + for(int i=0;i