added 2d dft + bmp api (TESTING - may break!)
[my-code/api.git] / bmp / bmp.c
1 /* bmp.c -- bmp write/read api
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "bmp.h"
8
9 int bmp_init(t_bmp *bmp,int outfd) {
10
11   dprintf(outfd,"[bmp] initializing bmp api ...\n");
12
13   memset(bmp,0,sizeof(t_bmp));
14   bmp->outfd=outfd;
15
16   return B_SUCCESS;
17 }
18
19 int bmp_shutdown(t_bmp *bmp) {
20
21   if(bmp->map!=NULL) {
22     dprintf(bmp->outfd,"[bmp] free pixmap memory\n");
23     free(bmp->map);
24   }
25
26   dprintf(bmp->outfd,"[bmp] shutdown\n");
27
28   return B_SUCCESS;
29 }
30
31 int bmp_check_header_and_info(t_bmp *bmp) {
32
33   if(bmp->info.compression!=0) {
34     dprintf(bmp->outfd,"[bmp] compression not supported\n");
35     return B_NO_SUPPORT;
36   }
37   
38   if(bmp->info.bpp!=24) {
39     dprintf(bmp->outfd,"[bmp] only true color (24bpp) supported\n");
40     return B_NO_SUPPORT;
41   }
42     
43   if(bmp->hdr.offset!=B_H_SIZE+B_I_SIZE) {
44     dprintf(bmp->outfd,"[bmp] files with %d bytes offset not supported\n",
45             bmp->hdr-offset);
46     return B_NO_SUPPORT;
47   }
48
49   if(bmp->info.size!=BMP_I_SIZE) {
50     dprintf(bmp->outfd,"[bmp] files with %d bytes info size not supported\n",
51             bmp->info.size);
52     return B_NO_SUPPORT;
53   }
54     
55   return B_SUCCESS;
56 }
57
58 int bmp_write_file(t_bmp *bmp) {
59  
60   int fill,xsize,size;
61
62   xsize=bmp->width*3;
63   fill=(4-(xsize%4))%4;
64   size=(xsize+fill)*bmp->height;
65
66   /* construct it */
67   bmp->hdr.identifier='B'|('M'<<8);
68   bmp->hdr.size=size+BMP_H_SIZE+BMP_I_SIZE;
69   bmp->hdr.offset=BMP_H_SIZE+BMP_I_SIZE;
70   bmp->info.size=BMP_I_SIZE;
71   bmp->info.width=bmp->width;
72   bmp->info.height=bmp->height;
73   bmp->info.planes=1;
74   bmp->info.bpp=24;
75   bmp->info.imagesize=size;
76   if(bmp->info.xres==0) bmp->info.xres=2048;
77   if(bmp->info.yres==0) bmp->info.yres=2048;
78   bmp->info.noc=0;
79   bmp->info.ic=0;
80
81   /* write it */
82   ...
83
84
85   return B_SUCCESS;
86 }
87
88 int bmp_read_file(t_bmp *bmp) {
89
90   unsigned char buf[BMP_HI_SIZE];
91   int y,xsize;
92   int crop;
93   unsigned char *data;
94
95   if(!(bmp->mode&READ)) {
96     dprintf(bmp->outfd,"[bmp] read mode not specified");
97     return B_NO_READ_MODE;
98   }
99
100   if((bmp->fd=open(bmp->file,O_RDONLY))<0) {
101     dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file);
102     return B_NO_FILE;
103   }
104
105   if(read(bmp->fd,buf,BMP_H_SIZE+BMP_I_SIZE)<BMP_H_SIZE+BMP_I_SIZE) {
106     dprintf(bmp->outfd,"[bmp] error reading bmp header & info\n");
107     return B_NO_HI;
108   }
109
110   memcpy(&(bmp->hdr),buf,BMP_H_SIZE);
111   memcpy(&(bmp->info),buf+BMP_H_SIZE,BMP_I_SIZE);
112
113   if(bmp_check_header_and_info(bmp)!=B_SUCCESS) {
114     dprintf(bmp->outfd,"[bmp] header/info check failed\n");
115     return B_HI_FAIL;
116   }
117
118   bmp->map=(t_pixel *)malloc(bmp->info.width*bmp->info.height*sizeof(t_pixel));
119   if(bmp->map==NULL) {
120     dprintf(bmp->outfd,"[bmp] malloc of map memory failed\n");
121     return B_E_MEM;
122   }
123
124   crop=(bmp->info.imagesize/bmp->info.height)%4;
125   xsize=(bmp->info.bpp/8)*bmp->info.width;
126
127   for(y=0;y<bmp->info.height;y++) {
128     if(read(bmp->fd,bmp->map+y*bmp->info.width,xsize)<xsize) {
129       dprintf(bmp->outfd,"[bmp] reading image data of line %d failed\n",y);
130       return B_E_READ_DATA;
131     }
132     if(read(bmp->fd,buf,crop)<crop) {
133       dprintf(bmp->outfd,"[bmp] failed reading rest of line\n");
134       return B_E_READ_DATA;
135     }
136   }
137     
138   close(bmp->fd);
139
140   return B_SUCCESS;
141 }