// @(#)root/img_view:$Id$ // Author: Jerzy Tarasiuk 19/06/2009 { // see http://root.cern.ch/root/html/tutorials/proof/getProof.C.html // //_________________________________________________________________ // // img_view - view a graphical image as 2-dimensional histogram // // Reads a graphical image (allowed typed are: GIF, JPEG, PNG, TIFF) // puts its data in a histogram and shows it; // to be run as root session, with the image filename, // and other arguments passed as environment variables. // //#include "Getline.h" //#include "TEnv.h" //#include "TString.h" //#include "TSystem.h" gROOT->Reset(); // import sys; int ix, iy, iz, hx, hy, sc, ixm, iym, izm, hxm, hym; double ac,bc,ll,ul,dpi,z; char *img, *ixp, *ipn=NULL; const char *xan[]={ "a", "b", "ll", "ul", "img", "dpi", "sc", NULL }; // void *xav[]= { &ac, &bc, &ll, &ul, &img, &dpi, &sc }; const char xat[]="ddddsdi"; const char *itn[]={ "gif", "jpg", "png", "tif" }; const char *itp[]={ "gif", "jpeg", "png","tiff" }; FILE* pfd; char buf[1024],*cp,*ibf; int i,tc; long idc; // 1. process args (passed as environment variables) // 1.1. verify they all were specified for(i=0; xan[i]!=NULL; i++) { if((cp=gSystem->Getenv(xan[i]))==NULL) break; // switch(xat[i]) { // case's': *(char**)xav[i]=cp; break; // case'i': *(int*)(xav[i])=atol(cp); break; // sscanf(cp, "%d", xav[i]); break; // case'd': *(double*)(xav[i])=atof(cp); break; // sscanf(cp, "%lf", xav[i]); break; // } } if(xan[i]!=NULL) { cerr << "\nName not found: " << xan[i] << "\n\n"; cerr << "Usage: name1=value1 name2=value2 [...] root program.C\n\n"; cerr << "Required names: a, b, ll, ul, img, dpi, sc; where:\n"; cerr << "a and b are for data linear transformation: z=a*data+b\n"; cerr << "ll and ul are lower and upper limit for the data\n"; cerr << "img is input image, types handled: gif jpg png tif\n"; cerr << "sc is reduction factor (int); dpi is image dot density\n\n"; exit(-1); } // 1.2. get and convert their values ac=atof(gSystem->Getenv("a")); bc=atof(gSystem->Getenv("b")); ll=atof(gSystem->Getenv("ll")); ul=atof(gSystem->Getenv("ul")); img=gSystem->Getenv("img"); dpi=atof(gSystem->Getenv("dpi")); sc=atoi(gSystem->Getenv("sc")); // 1.3. show these args cout << "a=" << ac << ", b=" << bc; cout << ", ll=" << ll << ", ul=" << ul << "\n"; cout << "dpi=" << dpi; cout << ", sc=" << sc; cout << ", img=" << img << "\n"; // 1.4. are args values correct? if(sc<1) { cerr << "sc must be 1 or more\n"; exit(-1); } if(dpi<20) { cerr << "dpi unreasonably small\n"; exit(-1); } if((ixp=strrchr(img,'.'))!=NULL) ixp++; else { cerr << "img without extension??\n"; exit(-1); } cout << "ixp=" << ixp << "\n"; if(ixp!=NULL) for(i=0; i<4; i++) { if(strcmp(ixp, itn[i])==0 || strcmp(ixp, itp[i])==0) { ipn=itp[i]; break; } } if(ipn) cout << "Image processor: " << ipn << "topnm\n"; else { cerr << "No processor for such an image\n"; exit(-1); } // 2. get image header (will read image data later) sprintf(buf, "%stopnm %s | ppmtopgm", ipn, img); pfd=gSystem->OpenPipe(buf,"r"); for(i=0; i<3; i++) if(fgets(buf, sizeof(buf), pfd)!=NULL) switch(i) { case 0: if(strcmp(buf,"P5\n")) { cerr << "Bad image header type: " << buf; exit(-1); } break; case 1: if(sscanf(buf,"%d%d", &ixm, &iym)==2) break; cerr << "Image header error, buf=" << buf; exit(-1); case 2: if(sscanf(buf,"%d", &izm)==1) break; cerr << "Image header error, buf=" << buf; exit(-1); } hxm=ixm/sc; hym=iym/sc; cout << "ixm=" << ixm << ", iym=" << iym << ", izm=" << izm << "\n"; // 3. make the histogram // 3.1. histogram setup TCanvas *c1 = new TCanvas("c1"," ",1); TH2F *h2 = new TH2F("h2","cap",hxm,0,hxm*sc*25.4/dpi,hym,0,hym*sc*25.4/dpi); h2->SetXTitle(" X [mm]"); h2->SetYTitle(" Y [mm]"); h2->SetZTitle("Neutron dose relative units"); h2->GetZaxis()->SetTitleOffset(1.4); h2->GetXaxis()->SetTitleOffset(1.5); h2->GetYaxis()->SetTitleOffset(1.5); cout << "histogram setup done\n"; // 3.2. get image data and put it in the histogram if((ibf=malloc(ixm))==NULL) { cerr << "malloc() failed\n"; exit(-1); } for(idc=iy=0; iyul) z=ul; if(zSetBinContent(hx,hy,z/sc/sc +h2->GetBinContent(hx,hy)); // sum in histogram } } gSystem->ClosePipe(pfd); cout << idc << " data points used\n"; // 3.3. show the histogram gPad->SetLeftMargin(0.15); h2->Draw("lego"); } // http://root.cern.ch/root/html/tutorials/tree/basic.C.html