From: hackbard Date: Thu, 7 Oct 2004 15:00:50 +0000 (+0000) Subject: fixed bmp api, improved dft_2d ... though still seems to be broken X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=515c7fc6da820714686f9bfc87ccb9743c57d692;p=my-code%2Fapi.git fixed bmp api, improved dft_2d ... though still seems to be broken --- diff --git a/bmp/bmp.c b/bmp/bmp.c index 4d8c7ac..94e11d1 100644 --- a/bmp/bmp.c +++ b/bmp/bmp.c @@ -30,6 +30,9 @@ int bmp_shutdown(t_bmp *bmp) { int bmp_check_header_and_info(t_bmp *bmp) { + dprintf(bmp->outfd,"[bmp] magic identifier: %c%c\n", + bmp->hdr.identifier&0xff,bmp->hdr.identifier>>8); + if(bmp->info.compression!=0) { dprintf(bmp->outfd,"[bmp] compression not supported\n"); return B_NO_SUPPORT; @@ -40,9 +43,9 @@ int bmp_check_header_and_info(t_bmp *bmp) { return B_NO_SUPPORT; } - if(bmp->hdr.offset!=B_H_SIZE+B_I_SIZE) { + if(bmp->hdr.offset!=BMP_H_SIZE+BMP_I_SIZE) { dprintf(bmp->outfd,"[bmp] files with %d bytes offset not supported\n", - bmp->hdr-offset); + bmp->hdr.offset); return B_NO_SUPPORT; } @@ -55,9 +58,29 @@ int bmp_check_header_and_info(t_bmp *bmp) { return B_SUCCESS; } +int bmp_alloc_map(t_bmp *bmp) { + + int size; + + size=bmp->width*bmp->height*3; + + dprintf(bmp->outfd,"[bmp] alloc map memory (%d bytes)\n",size); + + if((bmp->map=(t_pixel *)malloc(size))==NULL) { + dprintf(bmp->outfd,"[bmp] memory map alloc failed\n"); + return B_E_MEM; + } + + return B_SUCCESS; +} + int bmp_write_file(t_bmp *bmp) { int fill,xsize,size; + int y; + unsigned char buf[3]; + + memset(buf,0,3); if(!(bmp->mode&WRITE)) { dprintf(bmp->outfd,"[bmp] write mode not specified\n"); @@ -84,7 +107,7 @@ int bmp_write_file(t_bmp *bmp) { bmp->info.ic=0; /* write it */ - if((bmp->fd=open(bmp->file,O_WRONLY)<0) { + if((bmp->fd=open(bmp->file,O_WRONLY|O_CREAT))<0) { dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file); return B_NO_FILE; } @@ -99,19 +122,30 @@ int bmp_write_file(t_bmp *bmp) { return B_E_WRITE_DATA; } + for(y=0;yheight;y++) { + if(write(bmp->fd,bmp->map+y*bmp->width,xsize)outfd,"[bmp] unable to write image data line %d\n",y); + return B_E_WRITE_DATA; + } + if(write(bmp->fd,buf,fill)outfd,"[bmp] unable to write fill bytes\n"); + return B_E_WRITE_DATA; + } + } + + close(bmp->fd); return B_SUCCESS; } int bmp_read_file(t_bmp *bmp) { - unsigned char buf[BMP_HI_SIZE]; + unsigned char buf[BMP_H_SIZE+BMP_I_SIZE]; int y,xsize; int crop; - unsigned char *data; if(!(bmp->mode&READ)) { - dprintf(bmp->outfd,"[bmp] read mode not specified"); + dprintf(bmp->outfd,"[bmp] read mode not specified\n"); return B_WRONG_MODE; } diff --git a/bmp/bmp.h b/bmp/bmp.h index f25007d..4dd47f5 100644 --- a/bmp/bmp.h +++ b/bmp/bmp.h @@ -8,6 +8,10 @@ #include #include #include +#include +#include +#include +#include /* defines */ #define B_SUCCESS 1 @@ -31,7 +35,7 @@ typedef struct s_bmp_hdr { unsigned short int reserved1; unsigned short int reserved2; unsigned int offset; /* <- 14 + 40 bytes = 0x36 */ -} t_bmp_hdr; /* 14 bytes */ +} __attribute__ ((packed)) t_bmp_hdr; /* 14 bytes */ typedef struct s_bmp_info { unsigned int size; /* 40 bytes = 0x28 */ @@ -45,13 +49,13 @@ typedef struct s_bmp_info { unsigned int yres; unsigned int noc; unsigned int ic; -} t_bmp_info; /* 40 bytes */ +} __attribute__ ((packed)) t_bmp_info; /* 40 bytes */ typedef struct s_pixel { unsigned char r; unsigned char g; unsigned char b; -} t_pixel; +} __attribute__ ((packed)) t_pixel; typedef struct s_bmp { int outfd; @@ -68,7 +72,11 @@ typedef struct s_bmp { } t_bmp; /* function prototypes */ -int bmp_init(t_bmp *bmp); +int bmp_init(t_bmp *bmp,int outfd); int bmp_shutdown(t_bmp *bmp); +int bmp_check_header_and_info(t_bmp *bmp); +int bmp_alloc_map(t_bmp *bmp); +int bmp_write_file(t_bmp *bmp); +int bmp_read_file(t_bmp *bmp); #endif diff --git a/fourier/fourier.c b/fourier/fourier.c index d322bc0..4f395f5 100644 --- a/fourier/fourier.c +++ b/fourier/fourier.c @@ -103,7 +103,7 @@ int fourier_fft_1d_shutdown(t_fourier *fourier) { int fourier_fft_1d(t_fourier *fourier) { - int i,j; + int i; /* copy src to destination, destination is modified in place */ memcpy(fourier->ftdata,fourier->data,fourier->data_len[0]*sizeof(t_complex)); @@ -117,7 +117,7 @@ int fourier_fft_1d(t_fourier *fourier) { for(i=0;idata_len[0];i++) dprintf(fourier->outfd,"%f %f\n", - fourier->ftdata[i]->r,fourier->revdata[i]->i); + fourier->ftdata[i].r,fourier->revdata[i]->r); return F_NOT_SUPPORTED; @@ -163,32 +163,82 @@ int fourier_dft_2d(t_fourier *fourier) { int off_f,off_r; int u,v,x,y; int X,Y; + int size; double arg; + t_complex *data; X=fourier->data_len[0]; Y=fourier->data_len[1]; + size=X*Y; + + data=(t_complex *)malloc(size*sizeof(t_complex)); + if(data==NULL) { + dprintf(fourier->outfd, + "[fourier] malloc of %d bytes of temp data failed\n",size); + return F_ALLOC_FAIL; + } + memset(data,0,size*sizeof(t_complex)); + if(fourier->type&BWD) return F_NOT_SUPPORTED; - /* stupid way - actually you would do: ft_on_index_1(ft_on_index_2(f(x,y))) */ - for(v=0;voutfd,"[fourier] (u=%d,v=%d)\n",u,v); + // fourier->ftdata[off_f+u].r=0; + // fourier->ftdata[off_f+u].i=0; + // for(y=0;yftdata[off_f+u].r+=(cos(arg)*fourier->data[off_r+x].r-sin(arg)*fourier->data[off_r+x].i); + // fourier->ftdata[off_f+u].i+=(sin(arg)*fourier->data[off_r+x].r+cos(arg)*fourier->data[off_r+x].i); + // } + // } + // fourier->ftdata[off_f+u].r/=(X*Y); + // fourier->ftdata[off_f+u].i/=(X*Y); + // } + // } + + /* the more clever way ... */ + // dft on index 1 + off_f=0; + for(y=0;yftdata[off+u].r=0; - fourier->ftdata[off+u].i=0; + //dprintf(fourier->outfd,"[fourier] (u=%d,v=%d)\n",u,y); + for(x=0;xdata[off_r].r-sin(arg)*fourier->data[off_r].i); + data[off_f+u].r+=(sin(arg)*fourier->data[off_r].r+cos(arg)*fourier->data[off_r].i); + } + data[off_f+u].r/=X; + data[off_f+u].i/=X; + } + off_f+=X; + } + // dft on index 2 of 'index 1 transformed data' + off_f=0; + for(x=0;xoutfd,"[fourier] (u=%d,v=%d)\n",v,x); for(y=0;yftdata[off+u].r+=(cos(arg)*fourier->data[off_r+x].r-sin(arg)*fourier->data[off_r+x].i); - fourier->ftdata[off+u].i+=(sin(arg)*fourier->data[off_r+x].r+cos(arg)*fourier->data[off_r+x].i); - } + arg=-2.0*M_PI*v*y/Y; + off_r=off_f+y; + fourier->ftdata[off_f+v].r+=(cos(arg)*data[off_r].r-sin(arg)*data[off_r].i); + fourier->ftdata[off_f+v].r+=(sin(arg)*data[off_r].r+cos(arg)*data[off_r].i); } - fourier->ftdata[off_f+u].r/=(X*Y); - fourier->ftdata[off_f+u].i/=(X*Y); + fourier->ftdata[off_f+v].r/=Y; + fourier->ftdata[off_f+v].i/=Y; } + off_f+=Y; } + free(data); + return F_SUCCESS; }