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_cut_grab_bottom(t_bmp *dst,t_bmp *src,int dz,unsigned char m) {
145 dst->width=src->width;
149 dprintf(src->outfd,"[bmp] cut region greater than image height\n");
153 if(bmp_alloc_map(dst)!=B_SUCCESS) {
154 dprintf(dst->outfd,"[bmp] no map memory\n");
158 off=(m==GRAB)?0:(src->height-dz)*src->width;
159 memcpy(dst->map,src->map+off,dz*src->width*sizeof(t_pixel));
164 int bmp_read_file(t_bmp *bmp) {
166 unsigned char buf[BMP_H_SIZE+BMP_I_SIZE];
170 if(!(bmp->mode&READ)) {
171 dprintf(bmp->outfd,"[bmp] read mode not specified\n");
175 if((bmp->fd=open(bmp->file,O_RDONLY))<0) {
176 dprintf(bmp->outfd,"[bmp] unable to open file %s\n",bmp->file);
180 if(read(bmp->fd,buf,BMP_H_SIZE+BMP_I_SIZE)<BMP_H_SIZE+BMP_I_SIZE) {
181 dprintf(bmp->outfd,"[bmp] error reading bmp header & info\n");
185 memcpy(&(bmp->hdr),buf,BMP_H_SIZE);
186 memcpy(&(bmp->info),buf+BMP_H_SIZE,BMP_I_SIZE);
188 if(bmp_check_header_and_info(bmp)!=B_SUCCESS) {
189 dprintf(bmp->outfd,"[bmp] header/info check failed\n");
193 bmp->width=bmp->info.width;
194 bmp->height=bmp->info.height;
196 bmp->map=(t_pixel *)malloc(bmp->width*bmp->height*sizeof(t_pixel));
198 dprintf(bmp->outfd,"[bmp] malloc of map memory failed\n");
202 crop=bmp->info.imagesize/bmp->height-bmp->width*(bmp->info.bpp/8);
203 xsize=(bmp->info.bpp/8)*bmp->width;
205 for(y=0;y<bmp->height;y++) {
206 if(read(bmp->fd,bmp->map+y*bmp->width,xsize)<xsize) {
207 dprintf(bmp->outfd,"[bmp] reading image data of line %d failed\n",y);
208 return B_E_READ_DATA;
210 if(read(bmp->fd,buf,crop)<crop) {
211 dprintf(bmp->outfd,"[bmp] failed reading rest of line\n");
212 return B_E_READ_DATA;