int usage(void) {
puts("usage:");
- puts("ft -i <infile> -o <outfile> -itype <intype> -otype <outtype> -d <dimensions> -g <geometry> -debug <debug file>");
+ puts("ft -ifile <infile> -ofile <outfile> -itype <intype>");
+ puts(" -otype <outtype> -dim <dimensions>");
+ puts(" -geometry <geometry> -debug <debug file>");
puts("");
- puts("i/o type may be bmp or nlsop");
+ puts("itype may be bmp or nlsop");
+ puts("otype may be bmp or plot");
puts("dimensions: 2 or 3");
- puts("geometry in 2d: <resolution> <start> , eg: 64 32 0 80 (64x32 big area starting from (0|80)");
- puts("geometry in 3d: <resolution> <start> , eg: 64 64 32 0 0 80 (the same in 3 dimensions ^)");
+ puts("geometry in 2d: <resolution> <start>");
+ puts(" eg: 64 32 0 80 (64x32 big area starting from (0|80)");
+ puts("geometry in 3d: <resolution> <start>");
+ puts(" eg: 64 64 32 0 0 80 (the same in 3 dimensions ^)");
puts("");
puts("you must set -dimensions before specifying -geometry");
puts("default: dimension = 2 , resolution = whole image");
+ puts("only 2 dimensions allowed for bmp");
return 1;
}
-int get_size_of_infile(int fd,int *res,unigned char set) {
+int get_size_of_infile(int fd,int *res,unsigned char set) {
if(set&SET_IN_BMP) {
unsigned char buf[BMP_HEADER_LEN];
- puts("reading bmp header ...");
+ printf("reading bmp header ...");
read(fd,buf,BMP_HEADER_LEN);
res[0]=buf[18]|(buf[19]<<8);
res[1]=buf[22]|(buf[23]<<8);
+ printf(" done\n");
return ((int)buf[10]);
}
int get_infile_data(int fd,int offset,int *RES,int *res,int *x,unsigned char *orig,unsigned char set) {
- int i,j,k,w_len;
+ int i,j,k,w_len,test;
unsigned char *buf;
- lseek(fd,offset,SEEK_SET);
+ test=lseek(fd,offset,SEEK_SET);
+
+ printf("getting data at offset %d ...",test);
if(set&SET_IN_BMP) {
+
w_len=3*RES[1];
- w_len+=(4-w_len%4);
+ if(w_len%4) w_len+=(4-(w_len%4));
lseek(fd,x[1]*w_len,SEEK_CUR);
- buf=(unsigned char)malloc(w_len);
+ buf=(unsigned char *)malloc(w_len);
for(j=0;j<res[1];j++) {
read(fd,buf,w_len);
- for(i=x[0];i<res[0];i++) if(buf[3*i]!=0) orig+j*res[0]+i-x[0]=0xff;
+ for(i=x[0];i<res[0];i++) if(buf[3*i]!=0) *(orig+j*res[0]+i-x[0])=1;
}
free(buf);
}
else if(set&SET_IN_NLSOP) {
- puts("NOT SUPPORTED RIGT NOW!");
- return -1;
+
+ w_len=RES[0]*RES[1]*RES[2];
+
+ buf=(unsigned char *)malloc(w_len);
+ read(fd,buf,w_len);
+ for(k=0;k<res[2];k++) {
+ for(j=0;j<res[1];j++) {
+ for(i=0;i<res[0];i++) {
+ if(*(buf+x[0]+i+(x[1]+j)*RES[0]+(x[2]+k)*RES[0]*RES[1])!=0)
+ *(orig+i+j*RES[0]+k*RES[0]*RES[1])=1;
+ }
+ }
+ }
+ free(buf);
+ }
+
+ printf(" done\n");
+
+ return 1;
+}
+
+int write_bmp(int fd,unsigned char *orig,int *res) {
+
+ int size,i,j;
+ unsigned char buf[64];
+
+ size=res[0]*3+4-((res[0]*3)%4);
+ size*=res[1];
+ memset(buf,0,64);
+
+ /* bmpheader */
+ buf[0]='B'; /* std header start */
+ buf[1]='M';
+ buf[2]=(size+0x36)&0xff; /* file size */
+ buf[3]=(size+0x36)>>8;
+ memset(buf+4,0,6);
+ buf[10]=0x36; /* offset to data */
+ memset(buf+11,0,3);
+ buf[14]=0x28; /* length of bmp info header */
+ memset(buf+15,0,3);
+ buf[18]=res[0]&0xff; /* width and height */
+ buf[19]=res[0]>>8;
+ memset(buf+20,0,2);
+ buf[22]=res[1]&0xff;
+ buf[23]=res[1]>>8;
+ memset(buf+24,0,2);
+ buf[26]=1; /* # planes -> 1 */
+ buf[27]=0;
+ buf[28]=24; /* bits per pixel -> 2^24 (true color) */
+ buf[29]=0;
+ memset(buf+30,0,4); /* compression -> none */
+ buf[34]=size&0xff; /* data size */
+ buf[35]=size>>8;
+ memset(buf+36,0,2);
+ buf[38]=0x12; /* res: pixel/meter */
+ buf[39]=0x0b;
+ memset(buf+40,0,2);
+ buf[42]=0x12;
+ buf[43]=0x0b;
+ memset(buf+44,0,2);
+ memset(buf+46,0,8); /* no colors, no important colors */
+ /* write it */
+ write(fd,buf,54);
+
+ for(j=0;j<res[1];j++) {
+ for(i=0;i<res[0];i++) {
+ memset(buf,0,3);
+ if(*(orig+i+j*res[0])) memset(buf,0xff,3);
+ write(fd,buf,3);
+ }
+ if((res[0]*3)%4) {
+ memset(buf,0,4-((res[0]*3)%4));
+ write(fd,buf,4-((res[0]*3)%4));
+ }
}
return 1;
}
+int write_debug_file(int fd,unsigned char *orig,int *res,unsigned char set) {
+
+ int i,j,k;
+
+ if(set&SET_OUT_PLOT) {
+ dprintf(fd,"# gnuplot debug file\n");
+ if(!(set&SET_3D)) {
+ for(i=0;i<res[0];i++)
+ for(j=0;j<res[1];j++)
+ if(!*(orig+i+j*res[0])) dprintf(fd,"%d %d\n",i,j);
+ }
+ else {
+ for(i=0;i<res[0];i++)
+ for(j=0;j<res[1];j++)
+ for(k=0;k<res[2];k++)
+ if(*(orig+i+j*res[0]+k*res[0]*res[1])) dprintf(fd,"%d %d %d\n",i,j,k);
+ }
+ }
+ else if(set&SET_OUT_BMP) write_bmp(fd,orig,res);
+
+ return 1;
+}
+
int main(int argc,char **argv) {
char infile[FILE_LEN]="",outfile[FILE_LEN]="",debugfile[FILE_LEN]="";
/* parse argv */
for(i=1;i<argc;i++) {
- if(!strncmp(argv[i],"-i",2)) strncpy(infile,argv[++i],FILE_LEN);
- else if(!strncmp(argv[i],"-o",2)) strncpy(outfile,argv[++i],FILE_LEN);
+ if(!strncmp(argv[i],"-ifile",6)) strncpy(infile,argv[++i],FILE_LEN);
+ else if(!strncmp(argv[i],"-ofile",6)) strncpy(outfile,argv[++i],FILE_LEN);
else if(!strncmp(argv[i],"-itype",6)) strncpy(itype,argv[++i],TYPE_LEN);
else if(!strncmp(argv[i],"-otype",6)) strncpy(otype,argv[++i],TYPE_LEN);
- else if(!strncmp(argv[i],"-d",2)) {
+ else if(!strncmp(argv[i],"-debug",6)) {
strncpy(debugfile,argv[++i],FILE_LEN);
set|=SET_DEBUG;
}
- else if(!strncmp(argv[i],"-d",2)) {
+ else if(!strncmp(argv[i],"-dim",4)) {
dim=atoi(argv[++i]);
set|=SET_DIM;
if(dim==3) set|=SET_3D;
}
- else if(!strncmp(argv[i],"-g",2)) {
+ else if(!strncmp(argv[i],"-geometry",8)) {
set|=SET_GEO;
res[0]=atoi(argv[++i]); res[1]=atoi(argv[++i]);
if(dim==3) res[2]=atoi(argv[++i]);
x[0]=atoi(argv[++i]); x[1]=atoi(argv[++i]);
if(dim==3) x[2]=atoi(argv[++i]);
}
+ else {
usage();
return -1;
}
return -1;
}
- if((ofd=open(ofile,O_WRONLY))<0) {
+ if((ofd=open(outfile,O_WRONLY|O_CREAT))<0) {
perror("unable to open file writeonly");
return -1;
}
if(set&SET_DEBUG) {
- if((dfd=open(debugfile,O_WRONLY))<0) {
+ if((dfd=open(debugfile,O_WRONLY|O_CREAT))<0) {
perror("unable to open degugfile writeonly");
return -1;
}
return -1;
}
}
- else if(!strncmp(itype,"nlsop",5)) set|=SET_IN_NLSOP;
+ else if(!strncmp(itype,"nlsop",5)) {
+ set|=SET_IN_NLSOP;
+ if(dim==2) {
+ puts("there is no 2 dimensional nlsop file!");
+ return -1;
+ }
+ }
else usage();
if(!strncmp(otype,"bmp",3)) {
return -1;
}
}
- else if(!strncmp(otype,"nlsop",5)) set|=SET_OUT_NLSOP;
+ else if(!strncmp(otype,"plot",5)) set|=SET_OUT_PLOT;
else usage();
offset=get_size_of_infile(ifd,RES,set);
- if(!set&SET_GEO) {
+ if(!(set&SET_GEO)) {
for(i=0;i<3;i++) {
x[i]=0;
res[i]=RES[i];
}
}
+ printf("debug: %d %d %d %d\n",res[0],res[1],x[0],x[1]);
+
/* allocate buffer for original and transformed image */
size=res[0]*res[1];
if(dim==3) size*=res[2];
orig=(unsigned char *)malloc(size);
+ memset(orig,0,size);
image=(unsigned char *)malloc(size);
get_infile_data(ifd,offset,RES,res,x,orig,set);
+
+ if(set&SET_DEBUG) write_debug_file(dfd,orig,res,set);
/* to be continued ... */
+ free(orig);
+ free(image);
+
+ close(ifd);
+ close(ofd);
+ close(dfd);
return 1;
}