Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

425 linhas
11 KiB

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