Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

373 wiersze
8.7 KiB

  1. /*
  2. * Atpdec
  3. * Copyright (c) 2003 by Thierry Leconte (F4DWV)
  4. *
  5. * $Id$
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Library General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <libgen.h>
  26. #include <sndfile.h>
  27. #include <png.h>
  28. extern getpixelrow(float *pixelv);
  29. static SNDFILE *inwav;
  30. static int initsnd(char *filename)
  31. {
  32. SF_INFO infwav;
  33. /* open wav input file */
  34. infwav.format=0;
  35. inwav=sf_open(filename,SFM_READ,&infwav);
  36. if (inwav==NULL) {
  37. fprintf(stderr,"could not open %s\n",filename);
  38. return(1);
  39. }
  40. if(infwav.samplerate !=11025) {
  41. fprintf(stderr,"Bad Input File sample rate: %d. Must be 11025\n",infwav.samplerate);
  42. return(1);
  43. }
  44. if(infwav.channels !=1) {
  45. fprintf(stderr,"Too many channels in input file : %d\n",infwav.channels);
  46. return(1);
  47. }
  48. return(0);
  49. }
  50. int getsample(float *sample,int nb)
  51. {
  52. return(sf_read_float(inwav,sample,nb));
  53. }
  54. char signature[]="atpdec 1.3 (c) Thierry Leconte 2003";
  55. png_text text_ptr[]={
  56. { PNG_TEXT_COMPRESSION_NONE, "Software", signature ,sizeof(signature) },
  57. { PNG_TEXT_COMPRESSION_NONE, "Channel", NULL ,0 },
  58. { PNG_TEXT_COMPRESSION_NONE, "Description", "NOAA POES satellite Image" ,25 }
  59. };
  60. static int ImageOut(char *filename,const char* chid,float **prow,int nrow,int depth,int width,int offset)
  61. {
  62. FILE *pngfile;
  63. png_infop info_ptr;
  64. png_structp png_ptr;
  65. int n;
  66. /* init png lib */
  67. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  68. if (!png_ptr) {
  69. fprintf(stderr,"could not open create png_ptr\n");
  70. return(1);
  71. }
  72. info_ptr = png_create_info_struct(png_ptr);
  73. if (!info_ptr) {
  74. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  75. fprintf(stderr,"could not open create info_ptr\n");
  76. return(1);
  77. }
  78. png_set_IHDR(png_ptr, info_ptr, width, nrow,
  79. depth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
  80. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  81. text_ptr[1].text=chid;
  82. text_ptr[1].text_length=strlen(chid);
  83. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  84. png_set_pHYs(png_ptr, info_ptr, 4000,4000,PNG_RESOLUTION_METER);
  85. printf("Writing %s ... ",filename);
  86. pngfile=fopen(filename,"w");
  87. if (pngfile==NULL) {
  88. fprintf(stderr,"could not open %s\n",filename);
  89. return(1);
  90. }
  91. png_init_io(png_ptr,pngfile);
  92. png_write_info(png_ptr,info_ptr);
  93. for(n=0;n<nrow;n++) {
  94. float *pixelv;
  95. png_byte pixel[2*2080];
  96. int i;
  97. pixelv=prow[n];
  98. for(i=0;i<width;i++) {
  99. float pv;
  100. pv=pixelv[i+offset];
  101. switch(depth) {
  102. case 8 :
  103. pixel[i]=(png_byte)pv;
  104. break;
  105. case 16 :
  106. ((unsigned short*)pixel)[i]=htons((unsigned short)(pv*255.0));
  107. break;
  108. }
  109. }
  110. png_write_row(png_ptr,pixel);
  111. }
  112. png_write_end(png_ptr,info_ptr);
  113. fclose(pngfile);
  114. printf("Done\n");
  115. png_destroy_write_struct(&png_ptr,&info_ptr);
  116. return(0);
  117. }
  118. int ImageColorOut(char *filename,float **prow,int nrow)
  119. {
  120. FILE *pngfile;
  121. png_infop info_ptr;
  122. png_structp png_ptr;
  123. int n;
  124. extern void false_color(double v, double t, float *r, float *g, float *b);
  125. /* init png lib */
  126. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  127. if (!png_ptr) {
  128. fprintf(stderr,"could not open create png_ptr\n");
  129. return(1);
  130. }
  131. info_ptr = png_create_info_struct(png_ptr);
  132. if (!info_ptr) {
  133. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  134. fprintf(stderr,"could not open create info_ptr\n");
  135. return(1);
  136. }
  137. png_set_IHDR(png_ptr, info_ptr, 909, nrow,
  138. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  139. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  140. text_ptr[1].text="False Colors";
  141. text_ptr[1].text_length=strlen(text_ptr[1].text);
  142. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  143. png_set_pHYs(png_ptr, info_ptr, 4000,4000,PNG_RESOLUTION_METER);
  144. printf("Writing %s ...",filename);
  145. pngfile=fopen(filename,"w");
  146. if (pngfile==NULL) {
  147. fprintf(stderr,"could not open %s\n",filename);
  148. return(1);
  149. }
  150. png_init_io(png_ptr,pngfile);
  151. png_write_info(png_ptr,info_ptr);
  152. for(n=0;n<nrow;n++) {
  153. float *pixelv;
  154. png_color pixel[909];
  155. int i;
  156. pixelv=prow[n];
  157. for(i=0;i<909;i++) {
  158. double v,t;
  159. float r,g,b;
  160. v=pixelv[i+85];
  161. t=pixelv[i+1125];
  162. falsecolor(v,t,&r,&g,&b);
  163. pixel[i].red=( unsigned int)(255.0*r);
  164. pixel[i].green=( unsigned int)(255.0*g);
  165. pixel[i].blue=( unsigned int)(255.0*b);
  166. }
  167. png_write_row(png_ptr,(png_bytep)pixel);
  168. }
  169. png_write_end(png_ptr,info_ptr);
  170. fclose(pngfile);
  171. printf("Done\n");
  172. png_destroy_write_struct(&png_ptr,&info_ptr);
  173. return(0);
  174. }
  175. static int Distrib(char *filename,float **prow,int nrow)
  176. {
  177. unsigned int distrib[256][256];
  178. int n;
  179. int x,y;
  180. int max=0;
  181. FILE *df;
  182. for(y=0;y<256;y++)
  183. for(x=0;x<256;x++)
  184. distrib[y][x]=0;
  185. for(n=0;n<nrow;n++) {
  186. float *pixelv;
  187. int i;
  188. pixelv=prow[n];
  189. for(i=0;i<909;i++) {
  190. y=(int)(pixelv[i+85]);
  191. x=(int)(pixelv[i+1125]);
  192. distrib[y][x]+=1;
  193. if(distrib[y][x]> max) max=distrib[y][x];
  194. }
  195. }
  196. df=fopen(filename,"w");
  197. printf("Writing %s\n",filename);
  198. fprintf(df,"P2\n#max %d\n",max);
  199. fprintf(df,"256 256\n255\n");
  200. for(y=0;y<256;y++)
  201. for(x=0;x<256;x++)
  202. fprintf(df,"%d\n",(int)((255.0*(double)(distrib[y][x]))/(double)max));
  203. fclose(df);
  204. }
  205. extern int Calibrate(float **prow,int nrow,int offset);
  206. extern int Temperature(float **prow,int nrow,int ch, int offset);
  207. extern void readfconf(char *file);
  208. extern int optind,opterr;
  209. extern char *optarg;
  210. int satnum=1;
  211. static void usage(void)
  212. {
  213. fprintf(stderr,"atpdec [options] soundfiles ...\n");
  214. fprintf(stderr,"options:\n-d directory\n-i [a|b|c|t|d]\n-c cmap.png\n-p\n-s satnumber\n");
  215. }
  216. main(int argc, char **argv)
  217. {
  218. char pngfilename[1024];
  219. char name[1024];
  220. char pngdirname[1024]="";
  221. char imgopt[20]="ac";
  222. float *prow[3000];
  223. const char *chid[6]={ "1","2","3A","4","5","3B"};
  224. int depth=8;
  225. int n,nrow;
  226. int ch;
  227. int c;
  228. opterr=0;
  229. while ((c=getopt(argc,argv,"c:d:i:ps:"))!=EOF) {
  230. switch(c) {
  231. case 'd':
  232. strcpy(pngdirname,optarg);
  233. break;
  234. case 'c':
  235. readfconf(optarg);
  236. break;
  237. case 'i':
  238. strcpy(imgopt,optarg);
  239. break;
  240. case 'p':
  241. depth=16;
  242. break;
  243. case 's':
  244. satnum=atoi(optarg);
  245. if(satnum<0 || satnum > 1) {
  246. fprintf(stderr,"invalid satellite\n");
  247. exit(1);
  248. }
  249. break;
  250. default:
  251. usage();
  252. }
  253. }
  254. for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
  255. for (;optind<argc;optind++) {
  256. int a=0,b=0;
  257. strcpy(pngfilename,argv[optind]);
  258. strcpy(name,basename(pngfilename));
  259. strtok(name,".");
  260. if(pngdirname[0]=='\0') {
  261. strcpy(pngfilename,argv[optind]);
  262. strcpy(pngdirname,dirname(pngfilename));
  263. }
  264. /* open snd input */
  265. if(initsnd(argv[optind]))
  266. exit(1);
  267. /* main loop */
  268. printf("Decoding: %s ...",argv[optind]);
  269. fflush(stdout);
  270. for(nrow=0;nrow<3000;nrow++) {
  271. if(prow[nrow]==NULL) prow[nrow]=(float*)malloc(sizeof(float)*2150);
  272. if(getpixelrow(prow[nrow])==0)
  273. break;
  274. }
  275. printf("Done : %d lines\n",nrow);
  276. sf_close(inwav);
  277. /* raw image */
  278. if(strchr(imgopt,(int)'r')!=NULL){
  279. sprintf(pngfilename,"%s/%s-r.png",pngdirname,name);
  280. ImageOut(pngfilename,"raw",prow,nrow,depth,2080,0);
  281. }
  282. /* Channel A */
  283. if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'d')!=NULL))) {
  284. ch=Calibrate(prow,nrow,85);
  285. if(ch>0) {
  286. a=1;
  287. if(strchr(imgopt,(int)'a')!=NULL) {
  288. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  289. ImageOut(pngfilename,chid[ch],prow,nrow,depth,954,85);
  290. }
  291. }
  292. }
  293. /* Channel B */
  294. if((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'t')!=NULL)) {
  295. ch=Calibrate(prow,nrow,1125);
  296. if(ch>0) {
  297. if(strchr(imgopt,(int)'b')!=NULL) {
  298. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  299. ImageOut(pngfilename,chid[ch],prow,nrow,depth,954,1125);
  300. }
  301. }
  302. if(ch>2) {
  303. b=1;
  304. Temperature(prow,nrow,ch,1125);
  305. if(strchr(imgopt,(int)'t')!=NULL) {
  306. sprintf(pngfilename,"%s/%s-t.png",pngdirname,name);
  307. ImageOut(pngfilename,"Temperature", prow,nrow,depth,954,1125);
  308. }
  309. }
  310. }
  311. /* distribution */
  312. if(a && b && strchr(imgopt,(int)'d')!=NULL){
  313. sprintf(pngfilename,"%s/%s-d.pnm",pngdirname,name);
  314. Distrib(pngfilename,prow,nrow);
  315. }
  316. /* color image */
  317. if(a && b && strchr(imgopt,(int)'c')!=NULL){
  318. sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
  319. ImageColorOut(pngfilename,prow,nrow);
  320. }
  321. }
  322. exit (0);
  323. }