25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

main.c 8.3 KiB

21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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", "Created by Thierry Leconte's atpdec",35 }
  57. };
  58. static int ImageOut(char *filename,float **prow,int nrow,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. 8, 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[2080];
  91. int i;
  92. pixelv=prow[n];
  93. for(i=0;i<width;i++) {
  94. float pv;
  95. pv=pixelv[i+offset];
  96. pixel[i]=(png_byte)(pv*255.0);
  97. }
  98. png_write_row(png_ptr,pixel);
  99. }
  100. png_write_end(png_ptr,info_ptr);
  101. fclose(pngfile);
  102. png_destroy_write_struct(&png_ptr,&info_ptr);
  103. return(0);
  104. }
  105. int ImageColorOut(char *filename,float **prow,int nrow)
  106. {
  107. FILE *pngfile;
  108. png_infop info_ptr;
  109. png_structp png_ptr;
  110. int n;
  111. pngfile=fopen(filename,"w");
  112. if (pngfile==NULL) {
  113. fprintf(stderr,"could not open %s\n",filename);
  114. return(1);
  115. }
  116. /* init png lib */
  117. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  118. if (!png_ptr) {
  119. fprintf(stderr,"could not open create png_ptr\n");
  120. return(1);
  121. }
  122. info_ptr = png_create_info_struct(png_ptr);
  123. if (!info_ptr) {
  124. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  125. fprintf(stderr,"could not open create info_ptr\n");
  126. return(1);
  127. }
  128. png_init_io(png_ptr,pngfile);
  129. png_set_IHDR(png_ptr, info_ptr, 909, nrow,
  130. 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  131. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  132. png_set_text(png_ptr, info_ptr, text_ptr, 1);
  133. printf("Writing %s\n",filename);
  134. png_write_info(png_ptr,info_ptr);
  135. for(n=0;n<nrow;n++) {
  136. float *pixelv;
  137. png_color pixel[909];
  138. int i;
  139. pixelv=prow[n];
  140. for(i=0;i<909;i++) {
  141. int x,y;
  142. y=(int)(pixelv[i+85]*255.0);
  143. x=(int)(pixelv[i+1125]*255.0);
  144. pixel[i]=cmap[y][x];
  145. }
  146. png_write_row(png_ptr,(png_bytep)pixel);
  147. }
  148. png_write_end(png_ptr,info_ptr);
  149. fclose(pngfile);
  150. png_destroy_write_struct(&png_ptr,&info_ptr);
  151. return(0);
  152. }
  153. readcmap(char *filename)
  154. {
  155. FILE *pngfile;
  156. png_infop info_ptr;
  157. png_structp png_ptr;
  158. int n;
  159. png_bytep prow[256];
  160. pngfile=fopen(filename,"r");
  161. if (pngfile==NULL) {
  162. fprintf(stderr,"could not open %s\n",filename);
  163. return(1);
  164. }
  165. /* init png lib */
  166. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  167. if (!png_ptr) {
  168. fprintf(stderr,"could not open create png_ptr\n");
  169. return(1);
  170. }
  171. info_ptr = png_create_info_struct(png_ptr);
  172. if (!info_ptr) {
  173. png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
  174. fprintf(stderr,"could not open create info_ptr\n");
  175. return(1);
  176. }
  177. png_init_io(png_ptr,pngfile);
  178. for(n=0;n<256;n++)
  179. prow[n]=(png_bytep)&(cmap[n][0]);
  180. png_read_info(png_ptr, info_ptr);
  181. if(png_get_image_width(png_ptr,info_ptr)!=256) {
  182. fprintf(stderr,"Invalid cmap width\n");
  183. exit(1);
  184. }
  185. if(png_get_image_height(png_ptr,info_ptr)!=256) {
  186. fprintf(stderr,"Invalid cmap height\n");
  187. exit(1);
  188. }
  189. if(png_get_bit_depth(png_ptr,info_ptr)!=8) {
  190. fprintf(stderr,"Invalid cmap depth\n");
  191. exit(1);
  192. }
  193. if(png_get_color_type(png_ptr,info_ptr)!=PNG_COLOR_TYPE_RGB ){
  194. fprintf(stderr,"Invalid cmap color type\n");
  195. exit(1);
  196. }
  197. png_read_image(png_ptr,prow);
  198. fclose(pngfile);
  199. png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
  200. return(0);
  201. }
  202. #ifdef DEBUG
  203. unsigned int distrib[256][256];
  204. int Distrib(char *filename,float **prow,int nrow)
  205. {
  206. int n;
  207. int x,y;
  208. int max=0;
  209. FILE *df;
  210. for(y=0;y<256;y++)
  211. for(x=0;x<256;x++)
  212. distrib[y][x]=0;
  213. for(n=0;n<nrow;n++) {
  214. float *pixelv;
  215. png_color pixel[909];
  216. int i;
  217. pixelv=prow[n];
  218. for(i=0;i<909;i++) {
  219. y=(int)(pixelv[i+85]*255.0);
  220. x=(int)(pixelv[i+1125]*255.0);
  221. if(x>255) x=255;
  222. if(x<0) x=0;
  223. if(y>255) y=255;
  224. if(y<0) y=0;
  225. distrib[y][x]+=1;
  226. if(distrib[y][x]> max) max=distrib[y][x];
  227. }
  228. }
  229. df=fopen(filename,"w");
  230. fprintf(df,"P2\n#max %d\n",max);
  231. fprintf(df,"256 256\n255\n");
  232. for(y=0;y<256;y++)
  233. for(x=0;x<256;x++)
  234. fprintf(df,"%d\n",(int)((255.0*(double)(distrib[y][x]))/(double)max));
  235. fclose(df);
  236. }
  237. #endif
  238. extern int Calibrate(float **prow,int nrow,int offset);
  239. extern int optind,opterr;
  240. extern char *optarg;
  241. main(int argc, char **argv)
  242. {
  243. char pngfilename[1024];
  244. char name[1024];
  245. char *pngdirname=NULL;
  246. char imgopt[20]="abc";
  247. float *prow[3000];
  248. const char *chid[6]={ "1","2","3A","4","5","3B"};
  249. int n,nrow;
  250. int ch;
  251. int c;
  252. opterr=0;
  253. while ((c=getopt(argc,argv,"c:d:i:"))!=EOF) {
  254. switch(c) {
  255. case 'd':
  256. pngdirname=optarg;
  257. break;
  258. case 'c':
  259. readcmap(optarg);
  260. break;
  261. case 'i':
  262. strcpy(imgopt,optarg);
  263. break;
  264. }
  265. }
  266. for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
  267. for (;optind<argc;optind++) {
  268. int a=0,b=0;
  269. strcpy(pngfilename,argv[optind]);
  270. strcpy(name,basename(pngfilename));
  271. strtok(name,".");
  272. if (pngdirname==NULL) {
  273. strcpy(pngfilename,argv[optind]);
  274. pngdirname=dirname(pngfilename);
  275. }
  276. /* open snd input */
  277. if(initsnd(argv[optind]))
  278. exit(1);
  279. /* main loop */
  280. printf("Decoding: %s ...",argv[optind]);
  281. fflush(stdout);
  282. for(nrow=0;nrow<3000;nrow++) {
  283. if(prow[nrow]==NULL) prow[nrow]=(float*)malloc(sizeof(float)*2150);
  284. if(getpixelrow(prow[nrow])==0)
  285. break;
  286. }
  287. printf("Done : %d lines\n",nrow);
  288. sf_close(inwav);
  289. /* raw image */
  290. if(strchr(imgopt,(int)'r')!=NULL){
  291. sprintf(pngfilename,"%s/%s.png",pngdirname,name);
  292. ImageOut(pngfilename,prow,nrow,2080,0);
  293. }
  294. /* Channel A */
  295. if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,(int)'c')!=NULL))) {
  296. ch=Calibrate(prow,nrow,85);
  297. if(ch>0) {
  298. a=1;
  299. if(strchr(imgopt,(int)'a')!=NULL) {
  300. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  301. ImageOut(pngfilename,prow,nrow,954,85);
  302. }
  303. }
  304. }
  305. /* Channel B */
  306. if(((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,(int)'c')!=NULL))) {
  307. ch=Calibrate(prow,nrow,1125);
  308. if(ch>0) {
  309. b=1;
  310. if(strchr(imgopt,(int)'b')!=NULL) {
  311. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  312. ImageOut(pngfilename,prow,nrow,954,1125);
  313. }
  314. }
  315. }
  316. #ifdef DEBUG
  317. sprintf(pngfilename,"%s/%s-d.pnm",pngdirname,name);
  318. Distrib(pngfilename,prow,nrow);
  319. #endif
  320. /* color image */
  321. if(a && b && strchr(imgopt,(int)'c')!=NULL){
  322. sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
  323. ImageColorOut(pngfilename,prow,nrow);
  324. }
  325. }
  326. exit (0);
  327. }