38 #include "rotmatrix.h" 41 #include "paramfile.h" 42 #include "levels_facilities.h" 43 #include "lsconstants.h" 45 #include "string_utils.h" 51 void histo_eq (arr2<float> &img,
float &minv,
float &maxv, arr<double> &newpos)
54 arr<int> bincnt (nbins);
58 double fact = 1./(maxv-minv);
59 for (tsize i=0; i<img.size1(); ++i)
60 for (tsize j=0; j<img.size2(); ++j)
63 img[i][j] = float((img[i][j]-minv)*fact);
64 int idx = int(img[i][j]*nbins);
65 idx=max(0,min(idx,nbins-1));
70 newpos.alloc(nbins+1);
72 for (
int m=0; m<nbins; ++m)
74 newpos[m] = double(accu)/pixels;
79 for (tsize i=0; i<img.size1(); ++i)
80 for (tsize j=0; j<img.size2(); ++j)
83 int idx = int(img[i][j]*nbins);
84 idx=max(0,min(idx,nbins-1));
85 double frac = nbins*img[i][j] - idx;
86 img[i][j] = float((1-frac)*newpos[idx] + frac*newpos[idx+1]);
87 img[i][j] = float(minv+(maxv-minv)*img[i][j]);
92 int xsize, arr2<float> &img,
float &minv,
float &maxv,
bool smooth)
95 img.alloc(xsize,ysize);
97 double xc=(xsize-1)/2., yc=(ysize-1)/2.;
100 rot.Make_CPAC_Euler_Matrix(lon0,-lat0,0);
104 for (tsize i=0; i<img.size1(); ++i)
105 for (tsize j=0; j<img.size2(); ++j)
107 double u = 2*(i-xc)/(xc/1.02);
108 double v = (j-yc)/(yc/1.02);
109 bool mask = ((u*u/4 + v*v) <= 1);
112 pointing ptg (halfpi-(asin(2/pi*(asin(v) + v*sqrt((1-v)*(1+v))))),
113 -halfpi*u/max(sqrt((1-v)*(1+v)),1e-6));
114 vec3 pnt = rot.Transform(ptg.to_vec3());
118 img[i][j] = map[map.
ang2pix(pnt)];
121 if (img[i][j]<minv) minv=img[i][j];
122 if (img[i][j]>maxv) maxv=img[i][j];
129 int xsize,
double delta, arr2<float> &img,
float &minv,
float &maxv,
133 rot.Make_CPAC_Euler_Matrix(lon0,-lat0,0);
135 double start=-(xsize/2.)*delta;
136 img.alloc(xsize,xsize);
139 for (tsize i=0; i<img.size1(); ++i)
140 for (tsize j=0; j<img.size2(); ++j)
142 vec3 pnt (1,-(start+i*delta), start+j*delta);
143 pnt = rot.Transform(pnt);
147 img[i][j] = map[map.
ang2pix(pnt)];
150 if (img[i][j]<minv) minv=img[i][j];
151 if (img[i][j]>maxv) maxv=img[i][j];
156 void colorbar (LS_Image &img,
const Palette &pal,
int xmin,
int xmax,
157 int ymin,
int ymax,
bool flippal,
const arr<double> &newpos)
159 int nbins = newpos.size()-1;
160 for (
int i=xmin; i<=xmax; ++i)
162 double val = (double(i)-xmin)/(xmax-xmin);
165 int idx = int(val*nbins);
166 idx=max(0,min(idx,nbins-1));
167 double frac = nbins*val - idx;
168 val = (1-frac)*newpos[idx] + frac*newpos[idx+1];
170 if (flippal) val=1-val;
171 Colour c = pal.Get_Colour(
float(val));
172 for (
int j=ymin; j<=ymax; ++j)
173 img.put_pixel(i,j,c);
177 string conv (
double val)
180 if (abs(val)>100 || abs(val)<0.01)
182 os << setw(10) << setprecision(3) << scientific << val;
185 os << setw(10) << setprecision(6) << fixed << val;
186 return trim(os.str());
191 int map2tga_module (
int argc,
const char **argv)
193 module_startup (
"map2tga",argc>=2,
195 " map2tga <init object>\n\n" 197 " map2tga <input file> <output file> [-sig <int>] [-pal <int>]\n" 198 " [-xsz <int>] [-bar] [-log] [-asinh] [-lon <float>] [-lat <float>]\n" 199 " [-mul <float>] [-add <float>] [-min <float>] [-max <float>]\n" 200 " [-res <float>] [-title <string>] [-flippal] [-gnomonic]\n" 201 " [-interpol] [-equalize] [-viewer <viewer>]\n\n");
203 unique_ptr<paramfile> params;
206 vector<string> leading;
207 map<string,string> dict;
208 leading.push_back(
"infile");
209 leading.push_back(
"outfile");
210 parse_cmdline_classic (argc,argv,leading,dict);
211 if (dict.find(
"gnomonic")!=dict.end())
214 dict.erase(
"gnomonic");
216 params.reset(
new paramfile(dict,
false));
219 params.reset(
new paramfile(argv[1]));
221 string infile = params->find<
string>(
"infile");
222 string outfile = params->find<
string>(
"outfile");
223 int colnum = params->find<
int>(
"sig",1);
224 int palnr = params->find<
int>(
"pal",4);
225 bool flippal = params->find<
bool>(
"flippal",
false);
226 int xres = params->find<
int>(
"xsz",1024);
227 bool bar = params->find<
bool>(
"bar",
false);
228 bool logflag = params->find<
bool>(
"log",
false);
229 bool eqflag = params->find<
bool>(
"equalize",
false);
230 bool asinhflag = params->find<
bool>(
"asinh",
false);
231 double lon0 = degr2rad*params->find<
double>(
"lon",0);
232 double lat0 = degr2rad*params->find<
double>(
"lat",0);
233 double factor = params->find<
float>(
"mul",1);
234 double offset = params->find<
float>(
"add",0);
235 float usermin = params->find<
float>(
"min", -1e30);
236 bool min_supplied = (usermin>-1e29);
237 float usermax = params->find<
float>(
"max", 1e30);
238 bool max_supplied = (usermax<1e29);
239 double res = arcmin2rad*params->find<
double>(
"res",1);
240 string title = params->find<
string>(
"title",
"");
241 string viewer = params->find<
string>(
"viewer",
"");
242 bool mollpro = (params->find<
string>(
"pro",
"")!=
"gno");
243 bool interpol = params->find<
bool>(
"interpol",
false);
247 for (
int m=0; m<map.
Npix(); ++m)
251 map[m] = float((map[m]+offset)*factor);
254 : float(log(
double(map[m]))/ln10);
256 map[m] = (map[m]>=0) ?
257 float( log(
double( map[m]+sqrt(map[m]*map[m]+1)))) :
258 float(-log(
double(-map[m]+sqrt(map[m]*map[m]+1))));
259 if (min_supplied)
if (map[m] < usermin) map[m] = usermin;
260 if (max_supplied)
if (map[m] > usermax) map[m] = usermax;
266 mollpro ? pro_mollw (map,lon0,lat0,xres,imgarr,minv,maxv,interpol) :
267 pro_gno (map,lon0,lat0,xres,res,imgarr,minv,maxv,interpol);
270 if (eqflag) histo_eq(imgarr,minv,maxv,newpos);
272 if (min_supplied) minv = usermin;
273 if (max_supplied) maxv = usermax;
274 if (maxv==minv) maxv=minv+1e-10f;
276 int xsz=imgarr.size1();
277 int ysz=imgarr.size2();
279 int scale = max(1,xsz/800);
280 if (bar) ysz+=60*scale;
281 if (title !=
"") { ysz+=50*scale; yofs+=50*scale; }
282 LS_Image img(xsz,ysz);
283 img.fill(Colour(1,1,1));
284 img.set_font (giant_font);
286 pal.setPredefined(palnr);
288 img.annotate_centered(xsz/2,25*scale,Colour(0,0,0),title,scale);
289 for (tsize i=0; i<imgarr.size1(); ++i)
290 for (tsize j=0; j<imgarr.size2(); ++j)
292 if (imgarr[i][j]>-1e32)
294 Colour c(0.5,0.5,0.5);
297 int col = int((imgarr[i][j]-minv)/(maxv-minv)*256);
298 col = min(255,max(col,0));
299 float colfrac = (imgarr[i][j]-minv)/(maxv-minv);
300 if (flippal) colfrac = 1-colfrac;
301 c = pal.Get_Colour(colfrac);
303 img.put_pixel(i,yofs-j-1,c);
309 colorbar (img,pal,xsz/10,(xsz*9)/10,ysz-40*scale,ysz-20*scale,flippal,
311 img.set_font (medium_bold_font);
312 img.annotate_centered (xsz/20,ysz-30*scale,Colour(0,0,0),conv(minv),scale);
313 img.annotate_centered ((xsz*19)/20,ysz-30*scale,Colour(0,0,0),conv(maxv),
316 img.write_TGA(outfile);
320 int retcode = system((viewer+
" "+outfile).c_str());
322 cout <<
"Warning: viewer return code was " << retcode << endl;
I ang2pix(const pointing &ang) const
void read_Healpix_map_from_fits(fitshandle &inp, Healpix_Map< T > &map, int colnum)
T interpolated_value(const pointing &ptg) const
const double Healpix_undef
Healpix value representing "undefined".