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.
 
 
 
 
 

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