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.
 
 
 
 
 

391 lines
8.5 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 (Thierry Leconte)",35 }
  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*255.0);
  99. break;
  100. case 16 :
  101. ((unsigned short*)pixel)[i]=htons((unsigned short)(pv*65535.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]*255.0);
  150. x=(int)(pixelv[i+1125]*255.0);
  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. #ifdef DEBUG
  210. unsigned int distrib[256][256];
  211. int Distrib(char *filename,float **prow,int nrow)
  212. {
  213. int n;
  214. int x,y;
  215. int max=0;
  216. FILE *df;
  217. for(y=0;y<256;y++)
  218. for(x=0;x<256;x++)
  219. distrib[y][x]=0;
  220. for(n=0;n<nrow;n++) {
  221. float *pixelv;
  222. png_color pixel[909];
  223. int i;
  224. pixelv=prow[n];
  225. for(i=0;i<909;i++) {
  226. y=(int)(pixelv[i+85]*255.0);
  227. x=(int)(pixelv[i+1125]*255.0);
  228. if(x>255) x=255;
  229. if(x<0) x=0;
  230. if(y>255) y=255;
  231. if(y<0) y=0;
  232. distrib[y][x]+=1;
  233. if(distrib[y][x]> max) max=distrib[y][x];
  234. }
  235. }
  236. df=fopen(filename,"w");
  237. fprintf(df,"P2\n#max %d\n",max);
  238. fprintf(df,"256 256\n255\n");
  239. for(y=0;y<256;y++)
  240. for(x=0;x<256;x++)
  241. fprintf(df,"%d\n",(int)((255.0*(double)(distrib[y][x]))/(double)max));
  242. fclose(df);
  243. }
  244. #endif
  245. extern int Calibrate(float **prow,int nrow,int offset);
  246. extern int optind,opterr;
  247. extern char *optarg;
  248. main(int argc, char **argv)
  249. {
  250. char pngfilename[1024];
  251. char name[1024];
  252. char *pngdirname=NULL;
  253. char imgopt[20]="abc";
  254. float *prow[3000];
  255. const char *chid[6]={ "1","2","3A","4","5","3B"};
  256. int depth=16;
  257. int n,nrow;
  258. int ch;
  259. int c;
  260. opterr=0;
  261. while ((c=getopt(argc,argv,"c:d:i:s"))!=EOF) {
  262. switch(c) {
  263. case 'd':
  264. pngdirname=optarg;
  265. break;
  266. case 'c':
  267. readcmap(optarg);
  268. break;
  269. case 'i':
  270. strcpy(imgopt,optarg);
  271. break;
  272. case 's':
  273. depth=8;
  274. break;
  275. }
  276. }
  277. for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
  278. for (;optind<argc;optind++) {
  279. int a=0,b=0;
  280. strcpy(pngfilename,argv[optind]);
  281. strcpy(name,basename(pngfilename));
  282. strtok(name,".");
  283. if (pngdirname==NULL) {
  284. strcpy(pngfilename,argv[optind]);
  285. pngdirname=dirname(pngfilename);
  286. }
  287. /* open snd input */
  288. if(initsnd(argv[optind]))
  289. exit(1);
  290. /* main loop */
  291. printf("Decoding: %s ...",argv[optind]);
  292. fflush(stdout);
  293. for(nrow=0;nrow<3000;nrow++) {
  294. if(prow[nrow]==NULL) prow[nrow]=(float*)malloc(sizeof(float)*2150);
  295. if(getpixelrow(prow[nrow])==0)
  296. break;
  297. }
  298. printf("Done : %d lines\n",nrow);
  299. sf_close(inwav);
  300. /* raw image */
  301. if(strchr(imgopt,(int)'r')!=NULL){
  302. sprintf(pngfilename,"%s/%s.png",pngdirname,name);
  303. ImageOut(pngfilename,prow,nrow,depth,2080,0);
  304. }
  305. /* Channel A */
  306. if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,(int)'c')!=NULL))) {
  307. ch=Calibrate(prow,nrow,85);
  308. if(ch>0) {
  309. a=1;
  310. if(strchr(imgopt,(int)'a')!=NULL) {
  311. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  312. ImageOut(pngfilename,prow,nrow,depth,954,85);
  313. }
  314. }
  315. }
  316. /* Channel B */
  317. if(((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,(int)'c')!=NULL))) {
  318. ch=Calibrate(prow,nrow,1125);
  319. if(ch>0) {
  320. b=1;
  321. if(strchr(imgopt,(int)'b')!=NULL) {
  322. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  323. ImageOut(pngfilename,prow,nrow,depth,954,1125);
  324. }
  325. }
  326. }
  327. #ifdef DEBUG
  328. sprintf(pngfilename,"%s/%s-d.pnm",pngdirname,name);
  329. Distrib(pngfilename,prow,nrow);
  330. #endif
  331. /* color image */
  332. if(a && b && strchr(imgopt,(int)'c')!=NULL){
  333. sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
  334. ImageColorOut(pngfilename,prow,nrow);
  335. }
  336. }
  337. exit (0);
  338. }