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.
 
 
 
 
 

330 line
7.3 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", "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. x=(int)(pixelv[i+85]*255.0);
  143. y=(int)(pixelv[i+1125]*255.0);
  144. pixel[i]=cmap[x][y];
  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. extern int Calibrate(float **prow,int nrow,int offset);
  203. extern int optind,opterr;
  204. extern char *optarg;
  205. main(int argc, char **argv)
  206. {
  207. char pngfilename[1024];
  208. char name[1024];
  209. char *pngdirname=NULL;
  210. char imgopt[20]="abc";
  211. float *prow[3000];
  212. const char *chid[6]={ "1","2","3A","4","5","3B"};
  213. int n,nrow;
  214. int ch;
  215. int c;
  216. opterr=0;
  217. while ((c=getopt(argc,argv,"c:d:i:"))!=EOF) {
  218. switch(c) {
  219. case 'd':
  220. pngdirname=optarg;
  221. break;
  222. case 'c':
  223. readcmap(optarg);
  224. break;
  225. case 'i':
  226. strcpy(imgopt,optarg);
  227. break;
  228. }
  229. }
  230. for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
  231. for (;optind<argc;optind++) {
  232. int a=0,b=0;
  233. strcpy(pngfilename,argv[optind]);
  234. strcpy(name,basename(pngfilename));
  235. strtok(name,".");
  236. if (pngdirname==NULL) {
  237. strcpy(pngfilename,argv[optind]);
  238. pngdirname=dirname(pngfilename);
  239. }
  240. /* open snd input */
  241. if(initsnd(argv[optind]))
  242. exit(1);
  243. /* main loop */
  244. printf("Decoding: %s ...",argv[optind]);
  245. fflush(stdout);
  246. for(nrow=0;nrow<3000;nrow++) {
  247. if(prow[nrow]==NULL) prow[nrow]=(float*)malloc(sizeof(float)*2150);
  248. if(getpixelrow(prow[nrow])==0)
  249. break;
  250. }
  251. printf("Done : %d lines\n",nrow);
  252. sf_close(inwav);
  253. /* raw image */
  254. if(strchr(imgopt,(int)'r')!=NULL){
  255. sprintf(pngfilename,"%s/%s.png",pngdirname,name);
  256. ImageOut(pngfilename,prow,nrow,2080,0);
  257. }
  258. /* Channel A */
  259. ch=Calibrate(prow,nrow,85);
  260. if(ch>0) {
  261. a=1;
  262. if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,ch)!=NULL))) {
  263. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  264. ImageOut(pngfilename,prow,nrow,909,85);
  265. }
  266. }
  267. /* Channel B */
  268. ch=Calibrate(prow,nrow,1125);
  269. if(ch>0) {
  270. b=1;
  271. if(((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,ch)!=NULL))) {
  272. sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
  273. ImageOut(pngfilename,prow,nrow,909,1125);
  274. }
  275. }
  276. /* color image */
  277. if(a && b && strchr(imgopt,(int)'c')!=NULL){
  278. sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
  279. ImageColorOut(pngfilename,prow,nrow);
  280. }
  281. }
  282. exit (0);
  283. }