25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 9.1 KiB

21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
21 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "version.h"
  32. extern getpixelrow(float *pixelv);
  33. #define CHA_OFFSET 86
  34. #define CHB_OFFSET 1126
  35. #define CH_WIDTH 909
  36. #define IMG_WIDTH 2080
  37. static SNDFILE *inwav;
  38. static int initsnd(char *filename)
  39. {
  40. SF_INFO infwav;
  41. /* open wav input file */
  42. infwav.format=0;
  43. inwav=sf_open(filename,SFM_READ,&infwav);
  44. if (inwav==NULL) {
  45. fprintf(stderr,"could not open %s\n",filename);
  46. return(1);
  47. }
  48. if(infwav.samplerate !=11025) {
  49. fprintf(stderr,"Bad Input File sample rate: %d. Must be 11025\n",infwav.samplerate);
  50. return(1);
  51. }
  52. if(infwav.channels !=1) {
  53. fprintf(stderr,"Too many channels in input file : %d\n",infwav.channels);
  54. return(1);
  55. }
  56. return(0);
  57. }
  58. int getsample(float *sample,int nb)
  59. {
  60. return(sf_read_float(inwav,sample,nb));
  61. }
  62. png_text text_ptr[]={
  63. { PNG_TEXT_COMPRESSION_NONE, "Software", version ,sizeof(version) },
  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);fflush(stdout);
  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);fflush(stdout);
  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);fflush(stdout);
  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 <dir>\tDestination directory\n-i [a|b|c|t]\tOutput image type r: Raw a: A chan. b: B chan. c: False color t: Temperature\n-c <file>\tFalse color config file\n-p\t\t16bits output\n-s [0|1]\tSatellite number\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. printf("%s\n",version);
  236. opterr=0;
  237. while ((c=getopt(argc,argv,"c:d:i:ps:"))!=EOF) {
  238. switch(c) {
  239. case 'd':
  240. strcpy(pngdirname,optarg);
  241. break;
  242. case 'c':
  243. readfconf(optarg);
  244. break;
  245. case 'i':
  246. strcpy(imgopt,optarg);
  247. break;
  248. case 'p':
  249. depth=16;
  250. break;
  251. case 's':
  252. satnum=atoi(optarg);
  253. if(satnum<0 || satnum > 1) {
  254. fprintf(stderr,"invalid satellite\n");
  255. exit(1);
  256. }
  257. break;
  258. default:
  259. usage();
  260. }
  261. }
  262. for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
  263. for (;optind<argc;optind++) {
  264. int a=0,b=0;
  265. strcpy(pngfilename,argv[optind]);
  266. strcpy(name,basename(pngfilename));
  267. strtok(name,".");
  268. if(pngdirname[0]=='\0') {
  269. strcpy(pngfilename,argv[optind]);
  270. strcpy(pngdirname,dirname(pngfilename));
  271. }
  272. /* open snd input */
  273. if(initsnd(argv[optind]))
  274. exit(1);
  275. /* main loop */
  276. printf("Decoding: %s \n",argv[optind]);
  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. printf("%d\r",nrow);fflush(stdout);
  282. }
  283. printf("\nDone\n",nrow);
  284. sf_close(inwav);
  285. /* raw image */
  286. if(strchr(imgopt,(int)'r')!=NULL){
  287. sprintf(pngfilename,"%s/%s-r.png",pngdirname,name);
  288. ImageOut(pngfilename,"raw",prow,nrow,depth,IMG_WIDTH,0);
  289. }
  290. /* Channel A */
  291. if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'d')!=NULL))) {
  292. ch=Calibrate(prow,nrow,CHA_OFFSET);
  293. if(ch>0) {
  294. a=1;
  295. if(strchr(imgopt,(int)'a')!=NULL) {
  296. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  297. ImageOut(pngfilename,chid[ch],prow,nrow,depth,CH_WIDTH,CHA_OFFSET);
  298. }
  299. }
  300. }
  301. /* Channel B */
  302. if((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'t')!=NULL)) {
  303. ch=Calibrate(prow,nrow,CHB_OFFSET);
  304. if(ch>0) {
  305. if(strchr(imgopt,(int)'b')!=NULL) {
  306. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  307. ImageOut(pngfilename,chid[ch],prow,nrow,depth,CH_WIDTH,CHB_OFFSET);
  308. }
  309. }
  310. if(ch>2) {
  311. b=1;
  312. Temperature(prow,nrow,ch,CHB_OFFSET);
  313. if(strchr(imgopt,(int)'t')!=NULL) {
  314. sprintf(pngfilename,"%s/%s-t.png",pngdirname,name);
  315. ImageOut(pngfilename,"Temperature", prow,nrow,depth,CH_WIDTH,CHB_OFFSET);
  316. }
  317. }
  318. }
  319. /* distribution */
  320. if(a && b && strchr(imgopt,(int)'d')!=NULL){
  321. sprintf(pngfilename,"%s/%s-d.pnm",pngdirname,name);
  322. Distrib(pngfilename,prow,nrow);
  323. }
  324. /* color image */
  325. if(a && b && strchr(imgopt,(int)'c')!=NULL){
  326. sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
  327. ImageColorOut(pngfilename,prow,nrow);
  328. }
  329. }
  330. exit (0);
  331. }