1 /* ft.c - fourier transformation on nlsop save files */
4 * author: frank.zirkelbach@physik.uni-augsburg.de
12 puts("ft -ifile <infile> -ofile <outfile> -itype <intype>");
13 puts(" -otype <outtype> -dim <dimensions>");
14 puts(" -geometry <geometry> -debug <debug file>");
16 puts("itype may be bmp or nlsop");
17 puts("otype may be bmp or plot");
18 puts("dimensions: 2 or 3");
19 puts("geometry in 2d: <resolution> <start>");
20 puts(" eg: 64 32 0 80 (64x32 big area starting from (0|80)");
21 puts("geometry in 3d: <resolution> <start>");
22 puts(" eg: 64 64 32 0 0 80 (the same in 3 dimensions ^)");
24 puts("you must set -dimensions before specifying -geometry");
25 puts("default: dimension = 2 , resolution = whole image");
26 puts("only 2 dimensions allowed for bmp");
30 int get_size_of_infile(int fd,int *res,unsigned char set) {
34 unsigned char buf[BMP_HEADER_LEN];
36 printf("reading bmp header ...");
37 read(fd,buf,BMP_HEADER_LEN);
38 res[0]=buf[18]|(buf[19]<<8);
39 res[1]=buf[22]|(buf[23]<<8);
42 return ((int)buf[10]);
48 puts("reading nlsop header ...");
49 d3l=(d3_lattice *)malloc(sizeof(d3_lattice));
50 read(fd,d3l,sizeof(d3_lattice));
55 } else res[1]=d3l->max_z;
57 return (sizeof(d3_lattice)+sizeof(info));
62 int get_infile_data(int fd,int offset,int *RES,int *res,int *x,unsigned char *orig,unsigned char set) {
67 test=lseek(fd,offset,SEEK_SET);
69 printf("getting data at offset %d ...",test);
74 if(w_len%4) w_len+=(4-(w_len%4));
75 lseek(fd,x[1]*w_len,SEEK_CUR);
76 buf=(unsigned char *)malloc(w_len);
77 for(j=0;j<res[1];j++) {
79 for(i=x[0];i<res[0];i++) if(buf[3*i]!=0) *(orig+j*res[0]+i-x[0])=1;
83 else if(set&SET_IN_NLSOP) {
85 w_len=RES[0]*RES[1]*RES[2];
87 buf=(unsigned char *)malloc(w_len);
89 for(k=0;k<res[2];k++) {
90 for(j=0;j<res[1];j++) {
91 for(i=0;i<res[0];i++) {
92 if(*(buf+x[0]+i+(x[1]+j)*RES[0]+(x[2]+k)*RES[0]*RES[1])!=0)
93 *(orig+i+j*RES[0]+k*RES[0]*RES[1])=1;
105 int write_bmp(int fd,unsigned char *orig,int *res) {
108 unsigned char buf[64];
110 size=res[0]*3+4-((res[0]*3)%4);
115 buf[0]='B'; /* std header start */
117 buf[2]=(size+0x36)&0xff; /* file size */
118 buf[3]=(size+0x36)>>8;
120 buf[10]=0x36; /* offset to data */
122 buf[14]=0x28; /* length of bmp info header */
124 buf[18]=res[0]&0xff; /* width and height */
130 buf[26]=1; /* # planes -> 1 */
132 buf[28]=24; /* bits per pixel -> 2^24 (true color) */
134 memset(buf+30,0,4); /* compression -> none */
135 buf[34]=size&0xff; /* data size */
138 buf[38]=0x12; /* res: pixel/meter */
144 memset(buf+46,0,8); /* no colors, no important colors */
148 for(j=0;j<res[1];j++) {
149 for(i=0;i<res[0];i++) {
151 if(*(orig+i+j*res[0])) memset(buf,0xff,3);
155 memset(buf,0,4-((res[0]*3)%4));
156 write(fd,buf,4-((res[0]*3)%4));
163 int write_debug_file(int fd,unsigned char *orig,int *res,unsigned char set) {
167 if(set&SET_OUT_PLOT) {
168 dprintf(fd,"# gnuplot debug file\n");
170 for(i=0;i<res[0];i++)
171 for(j=0;j<res[1];j++)
172 if(!*(orig+i+j*res[0])) dprintf(fd,"%d %d\n",i,j);
175 for(i=0;i<res[0];i++)
176 for(j=0;j<res[1];j++)
177 for(k=0;k<res[2];k++)
178 if(*(orig+i+j*res[0]+k*res[0]*res[1])) dprintf(fd,"%d %d %d\n",i,j,k);
181 else if(set&SET_OUT_BMP) write_bmp(fd,orig,res);
186 int main(int argc,char **argv) {
188 char infile[FILE_LEN]="",outfile[FILE_LEN]="",debugfile[FILE_LEN]="";
189 char itype[TYPE_LEN]="",otype[TYPE_LEN]="";
193 int i,j,k,size,offset;
195 unsigned char *orig,*image;
196 int RES[3]; /* resolution of the original image */
200 for(i=1;i<argc;i++) {
201 if(!strncmp(argv[i],"-ifile",6)) strncpy(infile,argv[++i],FILE_LEN);
202 else if(!strncmp(argv[i],"-ofile",6)) strncpy(outfile,argv[++i],FILE_LEN);
203 else if(!strncmp(argv[i],"-itype",6)) strncpy(itype,argv[++i],TYPE_LEN);
204 else if(!strncmp(argv[i],"-otype",6)) strncpy(otype,argv[++i],TYPE_LEN);
205 else if(!strncmp(argv[i],"-debug",6)) {
206 strncpy(debugfile,argv[++i],FILE_LEN);
209 else if(!strncmp(argv[i],"-dim",4)) {
212 if(dim==3) set|=SET_3D;
214 else if(!strncmp(argv[i],"-geometry",8)) {
216 res[0]=atoi(argv[++i]); res[1]=atoi(argv[++i]);
217 if(dim==3) res[2]=atoi(argv[++i]);
218 x[0]=atoi(argv[++i]); x[1]=atoi(argv[++i]);
219 if(dim==3) x[2]=atoi(argv[++i]);
227 if((ifd=open(infile,O_RDONLY))<0) {
228 perror("unable to open file readonly");
232 if((ofd=open(outfile,O_WRONLY|O_CREAT))<0) {
233 perror("unable to open file writeonly");
238 if((dfd=open(debugfile,O_WRONLY|O_CREAT))<0) {
239 perror("unable to open degugfile writeonly");
244 if(!strncmp(itype,"bmp",3)) {
247 puts("there is no 3 dimensional bmp file!");
251 else if(!strncmp(itype,"nlsop",5)) {
254 puts("there is no 2 dimensional nlsop file!");
260 if(!strncmp(otype,"bmp",3)) {
263 puts("cant write 3 dimensional bmp file!");
267 else if(!strncmp(otype,"plot",5)) set|=SET_OUT_PLOT;
270 offset=get_size_of_infile(ifd,RES,set);
278 printf("debug: %d %d %d %d\n",res[0],res[1],x[0],x[1]);
280 /* allocate buffer for original and transformed image */
282 if(dim==3) size*=res[2];
283 orig=(unsigned char *)malloc(size);
285 image=(unsigned char *)malloc(size);
287 get_infile_data(ifd,offset,RES,res,x,orig,set);
289 if(set&SET_DEBUG) write_debug_file(dfd,orig,res,set);
291 /* to be continued ... */