You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 8.9 KiB

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