選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

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