Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

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