1 /* bmp.c -- bmp write/read api
3 * author: hackbard@hackdaworld.dyndns.org
9 int bmp_init(t_bmp *bmp,int outfd) {
11 dprintf(outfd,"[bmp] initializing bmp api ...\n");
13 memset(bmp,0,sizeof(t_bmp));
19 int bmp_shutdown(t_bmp *bmp) {
22 dprintf(bmp->outfd,"[bmp] free pixmap memory\n");
26 dprintf(bmp->outfd,"[bmp] shutdown\n");
31 int bmp_check_header_and_info(t_bmp *bmp) {
33 if(bmp->info.compression!=0) {
34 dprintf(bmp->outfd,"[bmp] compression not supported\n");
38 if(bmp->info.bpp!=24) {
39 dprintf(bmp->outfd,"[bmp] only true color (24bpp) supported\n");
43 if(bmp->hdr.offset!=B_H_SIZE+B_I_SIZE) {
44 dprintf(bmp->outfd,"[bmp] files with %d bytes offset not supported\n",
49 if(bmp->info.size!=BMP_I_SIZE) {
50 dprintf(bmp->outfd,"[bmp] files with %d bytes info size not supported\n",
58 int bmp_write_file(t_bmp *bmp) {
64 size=(xsize+fill)*bmp->height;
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;
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;
88 int bmp_read_file(t_bmp *bmp) {
90 unsigned char buf[BMP_HI_SIZE];
95 if(!(bmp->mode&READ)) {
96 dprintf(bmp->outfd,"[bmp] read mode not specified");
97 return B_NO_READ_MODE;
100 if((bmp->fd=open(bmp->file,O_RDONLY))<0) {
101 dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file);
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");
110 memcpy(&(bmp->hdr),buf,BMP_H_SIZE);
111 memcpy(&(bmp->info),buf+BMP_H_SIZE,BMP_I_SIZE);
113 if(bmp_check_header_and_info(bmp)!=B_SUCCESS) {
114 dprintf(bmp->outfd,"[bmp] header/info check failed\n");
118 bmp->map=(t_pixel *)malloc(bmp->info.width*bmp->info.height*sizeof(t_pixel));
120 dprintf(bmp->outfd,"[bmp] malloc of map memory failed\n");
124 crop=(bmp->info.imagesize/bmp->info.height)%4;
125 xsize=(bmp->info.bpp/8)*bmp->info.width;
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;
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;