|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- /*
- * Atpdec
- * Copyright (c) 2003 by Thierry Leconte (F4DWV)
- *
- * $Id$
- *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Library General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- */
-
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <libgen.h>
- #include <sndfile.h>
- #include <png.h>
- #include <cmap.h>
-
- extern getpixelrow(float *pixelv);
-
-
- static SNDFILE *inwav;
- static int initsnd(char *filename)
- {
- SF_INFO infwav;
-
- /* open wav input file */
- infwav.format=0;
- inwav=sf_open(filename,SFM_READ,&infwav);
- if (inwav==NULL) {
- fprintf(stderr,"could not open %s\n",filename);
- return(1);
- }
- if(infwav.samplerate !=11025) {
- fprintf(stderr,"Bad Input File sample rate: %d. Must be 11025\n",infwav.samplerate);
- return(1);
- }
- if(infwav.channels !=1) {
- fprintf(stderr,"Too many channels in input file : %d\n",infwav.channels);
- return(1);
- }
-
- return(0);
- }
-
- int getsample(float *sample,int nb)
- {
- return(sf_read_float(inwav,sample,nb));
- }
-
- png_text text_ptr[2]={
- { PNG_TEXT_COMPRESSION_NONE, "Software", "atpdec (c) Thierry Leconte 2003",32 }
- };
- static int ImageOut(char *filename,float **prow,int nrow,int depth,int width,int offset)
- {
- FILE *pngfile;
- png_infop info_ptr;
- png_structp png_ptr;
- int n;
-
- pngfile=fopen(filename,"w");
- if (pngfile==NULL) {
- fprintf(stderr,"could not open %s\n",filename);
- return(1);
- }
- /* init png lib */
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- fprintf(stderr,"could not open create png_ptr\n");
- return(1);
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- fprintf(stderr,"could not open create info_ptr\n");
- return(1);
- }
-
- png_init_io(png_ptr,pngfile);
- png_set_IHDR(png_ptr, info_ptr, width, nrow,
- depth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-
- png_set_text(png_ptr, info_ptr, text_ptr, 1);
-
- printf("Writing %s\n",filename);
- png_write_info(png_ptr,info_ptr);
- for(n=0;n<nrow;n++) {
- float *pixelv;
- png_byte pixel[2*2080];
- int i;
-
- pixelv=prow[n];
- for(i=0;i<width;i++) {
- float pv;
-
- pv=pixelv[i+offset];
- switch(depth) {
- case 8 :
- pixel[i]=(png_byte)pv;
- break;
- case 16 :
- ((unsigned short*)pixel)[i]=htons((unsigned short)(pv*255.0));
- break;
- }
- }
- png_write_row(png_ptr,pixel);
- }
- png_write_end(png_ptr,info_ptr);
- fclose(pngfile);
- png_destroy_write_struct(&png_ptr,&info_ptr);
- return(0);
- }
-
- int ImageColorOut(char *filename,float **prow,int nrow)
- {
- FILE *pngfile;
- png_infop info_ptr;
- png_structp png_ptr;
- int n;
-
- pngfile=fopen(filename,"w");
- if (pngfile==NULL) {
- fprintf(stderr,"could not open %s\n",filename);
- return(1);
- }
- /* init png lib */
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- fprintf(stderr,"could not open create png_ptr\n");
- return(1);
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- fprintf(stderr,"could not open create info_ptr\n");
- return(1);
- }
-
- png_init_io(png_ptr,pngfile);
- png_set_IHDR(png_ptr, info_ptr, 909, nrow,
- 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
- PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-
- png_set_text(png_ptr, info_ptr, text_ptr, 1);
-
- printf("Writing %s\n",filename);
- png_write_info(png_ptr,info_ptr);
-
- for(n=0;n<nrow;n++) {
- float *pixelv;
- png_color pixel[909];
- int i;
-
- pixelv=prow[n];
- for(i=0;i<909;i++) {
- int x,y;
-
- y=(int)(pixelv[i+85]);
- x=(int)(pixelv[i+1125]);
- pixel[i]=cmap[y][x];
- }
- png_write_row(png_ptr,(png_bytep)pixel);
- }
- png_write_end(png_ptr,info_ptr);
- fclose(pngfile);
- png_destroy_write_struct(&png_ptr,&info_ptr);
- return(0);
- }
-
- readcmap(char *filename)
- {
- FILE *pngfile;
- png_infop info_ptr;
- png_structp png_ptr;
- int n;
- png_bytep prow[256];
-
-
- pngfile=fopen(filename,"r");
- if (pngfile==NULL) {
- fprintf(stderr,"could not open %s\n",filename);
- return(1);
- }
- /* init png lib */
- png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- fprintf(stderr,"could not open create png_ptr\n");
- return(1);
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
- fprintf(stderr,"could not open create info_ptr\n");
- return(1);
- }
- png_init_io(png_ptr,pngfile);
-
- for(n=0;n<256;n++)
- prow[n]=(png_bytep)&(cmap[n][0]);
-
- png_read_info(png_ptr, info_ptr);
-
- if(png_get_image_width(png_ptr,info_ptr)!=256) {
- fprintf(stderr,"Invalid cmap width\n");
- exit(1);
- }
- if(png_get_image_height(png_ptr,info_ptr)!=256) {
- fprintf(stderr,"Invalid cmap height\n");
- exit(1);
- }
- if(png_get_bit_depth(png_ptr,info_ptr)!=8) {
- fprintf(stderr,"Invalid cmap depth\n");
- exit(1);
- }
- if(png_get_color_type(png_ptr,info_ptr)!=PNG_COLOR_TYPE_RGB ){
- fprintf(stderr,"Invalid cmap color type\n");
- exit(1);
- }
-
- png_read_image(png_ptr,prow);
- fclose(pngfile);
- png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
- return(0);
- }
-
- static int Distrib(char *filename,float **prow,int nrow)
- {
- unsigned int distrib[256][256];
- int n;
- int x,y;
- int max=0;
- FILE *df;
-
- for(y=0;y<256;y++)
- for(x=0;x<256;x++)
- distrib[y][x]=0;
-
- for(n=0;n<nrow;n++) {
- float *pixelv;
- int i;
-
- pixelv=prow[n];
- for(i=0;i<909;i++) {
-
- y=(int)(pixelv[i+85]);
- x=(int)(pixelv[i+1125]);
- distrib[y][x]+=1;
- if(distrib[y][x]> max) max=distrib[y][x];
- }
- }
- df=fopen(filename,"w");
-
- printf("Writing %s\n",filename);
-
- fprintf(df,"P2\n#max %d\n",max);
- fprintf(df,"256 256\n255\n");
- for(y=0;y<256;y++)
- for(x=0;x<256;x++)
- fprintf(df,"%d\n",(int)((255.0*(double)(distrib[y][x]))/(double)max));
- fclose(df);
- }
-
- extern int Calibrate(float **prow,int nrow,int offset);
- extern int Temperature(float **prow,int nrow,int ch, int offset);
- extern int optind,opterr;
- extern char *optarg;
- int satnum=1;
-
- static void usage(void)
- {
- fprintf(stderr,"atpdec [options] soundfiles ...\n");
- fprintf(stderr,"options:\n-d directory\n-i [a|b|c|t|d]\n-c cmap.png\n-p\n-s satnumber\n");
- }
-
- main(int argc, char **argv)
- {
- char pngfilename[1024];
- char name[1024];
- char pngdirname[1024]="";
- char imgopt[20]="ac";
- float *prow[3000];
- const char *chid[6]={ "1","2","3A","4","5","3B"};
- int depth=8;
- int n,nrow;
- int ch;
- int c;
-
-
- opterr=0;
- while ((c=getopt(argc,argv,"c:d:i:ps:"))!=EOF) {
- switch(c) {
- case 'd':
- strcpy(pngdirname,optarg);
- break;
- case 'c':
- readcmap(optarg);
- break;
- case 'i':
- strcpy(imgopt,optarg);
- break;
- case 'p':
- depth=16;
- break;
- case 's':
- satnum=atoi(optarg);
- if(satnum<0 || satnum > 1) {
- fprintf(stderr,"invalid satellite\n");
- exit(1);
- }
- break;
- default:
- usage();
- }
- }
-
- for(nrow=0;nrow<3000;nrow++) prow[nrow]=NULL;
-
- for (;optind<argc;optind++) {
- int a=0,b=0;
-
- strcpy(pngfilename,argv[optind]);
- strcpy(name,basename(pngfilename));
- strtok(name,".");
- if(pngdirname[0]=='\0') {
- strcpy(pngfilename,argv[optind]);
- strcpy(pngdirname,dirname(pngfilename));
- }
-
- /* open snd input */
- if(initsnd(argv[optind]))
- exit(1);
-
- /* main loop */
- printf("Decoding: %s ...",argv[optind]);
- fflush(stdout);
- for(nrow=0;nrow<3000;nrow++) {
- if(prow[nrow]==NULL) prow[nrow]=(float*)malloc(sizeof(float)*2150);
- if(getpixelrow(prow[nrow])==0)
- break;
- }
- printf("Done : %d lines\n",nrow);
- sf_close(inwav);
-
- /* raw image */
- if(strchr(imgopt,(int)'r')!=NULL){
- sprintf(pngfilename,"%s/%s-r.png",pngdirname,name);
- ImageOut(pngfilename,prow,nrow,depth,2080,0);
- }
-
- /* Channel A */
- if(((strchr(imgopt,(int)'a')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'d')!=NULL))) {
- ch=Calibrate(prow,nrow,85);
- if(ch>0) {
- a=1;
- if(strchr(imgopt,(int)'a')!=NULL) {
- sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
- ImageOut(pngfilename,prow,nrow,depth,954,85);
- }
- }
- }
-
- /* Channel B */
- if((strchr(imgopt,(int)'b')!=NULL) || (strchr(imgopt,(int)'c')!=NULL) || (strchr(imgopt,(int)'t')!=NULL)) {
- ch=Calibrate(prow,nrow,1125);
- if(ch>0) {
- if(strchr(imgopt,(int)'b')!=NULL) {
- sprintf(pngfilename,"%s/%s-%s.png",pngdirname,name,chid[ch]);
- ImageOut(pngfilename,prow,nrow,depth,954,1125);
- }
- }
- if(ch>2) {
- b=1;
- Temperature(prow,nrow,ch,1125);
- if(strchr(imgopt,(int)'t')!=NULL) {
- sprintf(pngfilename,"%s/%s-t.png",pngdirname,name);
- ImageOut(pngfilename,prow,nrow,depth,954,1125);
- }
- }
- }
-
- /* distribution */
- if(a && b && strchr(imgopt,(int)'d')!=NULL){
- sprintf(pngfilename,"%s/%s-d.pnm",pngdirname,name);
- Distrib(pngfilename,prow,nrow);
- }
-
- /* color image */
- if(a && b && strchr(imgopt,(int)'c')!=NULL){
- sprintf(pngfilename,"%s/%s-c.png",pngdirname,name);
- ImageColorOut(pngfilename,prow,nrow);
- }
-
- }
- exit (0);
- }
|