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 dprintf(bmp->outfd,"[bmp] magic identifier: %c%c\n",
34 bmp->hdr.identifier&0xff,bmp->hdr.identifier>>8);
36 if(bmp->info.compression!=0) {
37 dprintf(bmp->outfd,"[bmp] compression not supported\n");
41 if(bmp->info.bpp!=24) {
42 dprintf(bmp->outfd,"[bmp] only true color (24bpp) supported\n");
46 if(bmp->hdr.offset!=BMP_H_SIZE+BMP_I_SIZE) {
47 dprintf(bmp->outfd,"[bmp] files with %d bytes offset not supported\n",
52 if(bmp->info.size!=BMP_I_SIZE) {
53 dprintf(bmp->outfd,"[bmp] files with %d bytes info size not supported\n",
61 int bmp_alloc_map(t_bmp *bmp) {
65 size=bmp->width*bmp->height*3;
67 dprintf(bmp->outfd,"[bmp] alloc map memory (%d bytes)\n",size);
69 if((bmp->map=(t_pixel *)malloc(size))==NULL) {
70 dprintf(bmp->outfd,"[bmp] memory map alloc failed\n");
77 int bmp_write_file(t_bmp *bmp) {
85 if(!(bmp->mode&WRITE)) {
86 dprintf(bmp->outfd,"[bmp] write mode not specified\n");
92 size=(xsize+fill)*bmp->height;
95 bmp->hdr.identifier='B'|('M'<<8);
96 bmp->hdr.size=size+BMP_H_SIZE+BMP_I_SIZE;
97 bmp->hdr.offset=BMP_H_SIZE+BMP_I_SIZE;
98 bmp->info.size=BMP_I_SIZE;
99 bmp->info.width=bmp->width;
100 bmp->info.height=bmp->height;
103 bmp->info.imagesize=size;
104 if(bmp->info.xres==0) bmp->info.xres=2048;
105 if(bmp->info.yres==0) bmp->info.yres=2048;
110 if((bmp->fd=open(bmp->file,O_WRONLY|O_CREAT))<0) {
111 dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file);
115 if(write(bmp->fd,&(bmp->hdr),BMP_H_SIZE)<BMP_H_SIZE) {
116 dprintf(bmp->outfd,"[bmp] unable to write bmp header\n");
117 return B_E_WRITE_DATA;
120 if(write(bmp->fd,&(bmp->info),BMP_I_SIZE)<BMP_I_SIZE) {
121 dprintf(bmp->outfd,"[bmp] unable to write bmp info\n");
122 return B_E_WRITE_DATA;
125 for(y=0;y<bmp->height;y++) {
126 if(write(bmp->fd,bmp->map+y*bmp->width,xsize)<xsize) {
127 dprintf(bmp->outfd,"[bmp] unable to write image data line %d\n",y);
128 return B_E_WRITE_DATA;
130 if(write(bmp->fd,buf,fill)<fill) {
131 dprintf(bmp->outfd,"[bmp] unable to write fill bytes\n");
132 return B_E_WRITE_DATA;
141 int bmp_read_file(t_bmp *bmp) {
143 unsigned char buf[BMP_H_SIZE+BMP_I_SIZE];
147 if(!(bmp->mode&READ)) {
148 dprintf(bmp->outfd,"[bmp] read mode not specified\n");
152 if((bmp->fd=open(bmp->file,O_RDONLY))<0) {
153 dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file);
157 if(read(bmp->fd,buf,BMP_H_SIZE+BMP_I_SIZE)<BMP_H_SIZE+BMP_I_SIZE) {
158 dprintf(bmp->outfd,"[bmp] error reading bmp header & info\n");
162 memcpy(&(bmp->hdr),buf,BMP_H_SIZE);
163 memcpy(&(bmp->info),buf+BMP_H_SIZE,BMP_I_SIZE);
165 if(bmp_check_header_and_info(bmp)!=B_SUCCESS) {
166 dprintf(bmp->outfd,"[bmp] header/info check failed\n");
170 bmp->map=(t_pixel *)malloc(bmp->info.width*bmp->info.height*sizeof(t_pixel));
172 dprintf(bmp->outfd,"[bmp] malloc of map memory failed\n");
176 crop=(bmp->info.imagesize/bmp->info.height)%4;
177 xsize=(bmp->info.bpp/8)*bmp->info.width;
179 for(y=0;y<bmp->info.height;y++) {
180 if(read(bmp->fd,bmp->map+y*bmp->info.width,xsize)<xsize) {
181 dprintf(bmp->outfd,"[bmp] reading image data of line %d failed\n",y);
182 return B_E_READ_DATA;
184 if(read(bmp->fd,buf,crop)<crop) {
185 dprintf(bmp->outfd,"[bmp] failed reading rest of line\n");
186 return B_E_READ_DATA;