Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

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