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;
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;
}
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");
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;
}
return B_E_WRITE_DATA;
}
+ for(y=0;y<bmp->height;y++) {
+ if(write(bmp->fd,bmp->map+y*bmp->width,xsize)<xsize) {
+ dprintf(bmp->outfd,"[bmp] unable to write image data line %d\n",y);
+ return B_E_WRITE_DATA;
+ }
+ if(write(bmp->fd,buf,fill)<fill) {
+ dprintf(bmp->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;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
/* defines */
#define B_SUCCESS 1
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 */
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;
} 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
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));
for(i=0;i<fourier->data_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;
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;v<Y;v++) {
- off=v*X;
+ /* stupid way */
+
+ // for(v=0;v<Y;v++) {
+ // off_f=v*X;
+ // for(u=0;u<X;u++) {
+ // dprintf(fourier->outfd,"[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;y<Y;y++) {
+ // off_r=y*X;
+ // for(x=0;x<X;x++) {
+ // arg=-2.0*M_PI*((1.0*u*x)/X+(1.0*v*y)/Y);
+ // fourier->ftdata[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;y<Y;y++) {
for(u=0;u<X;u++) {
- fourier->ftdata[off+u].r=0;
- fourier->ftdata[off+u].i=0;
+ //dprintf(fourier->outfd,"[fourier] (u=%d,v=%d)\n",u,y);
+ for(x=0;x<X;x++) {
+ arg=-2.0*M_PI*u*x/X;
+ off_r=off_f+x;
+ data[off_f+u].r+=(cos(arg)*fourier->data[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;x<X;x++) {
+ for(v=0;v<Y;v++) {
+ //dprintf(fourier->outfd,"[fourier] (u=%d,v=%d)\n",v,x);
for(y=0;y<Y;y++) {
- off_r=y*X;
- for(x=0;x<X;x++) {
- arg=-2.0*M_PI*((1.0*u*x)/X+(1.0*v*y)/Y);
- fourier->ftdata[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;
}