X-Git-Url: https://www.hackdaworld.org/gitweb/?a=blobdiff_plain;f=bmp%2Fbmp.c;h=cc53c7cc6161463be4d96f3b471b32dfdf8fd2ea;hb=HEAD;hp=4d8c7acbc3a93c9cca03c3ab92cfd072669ca816;hpb=a54cc5ef402ca4ba6202f13ee65e5f2fdcfd10c1;p=my-code%2Fapi.git diff --git a/bmp/bmp.c b/bmp/bmp.c index 4d8c7ac..cc53c7c 100644 --- a/bmp/bmp.c +++ b/bmp/bmp.c @@ -30,6 +30,9 @@ int bmp_shutdown(t_bmp *bmp) { 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; @@ -40,9 +43,9 @@ int bmp_check_header_and_info(t_bmp *bmp) { 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; } @@ -55,9 +58,29 @@ int bmp_check_header_and_info(t_bmp *bmp) { 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"); @@ -84,7 +107,7 @@ int bmp_write_file(t_bmp *bmp) { 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; } @@ -99,19 +122,53 @@ int bmp_write_file(t_bmp *bmp) { return B_E_WRITE_DATA; } + for(y=0;yheight;y++) { + if(write(bmp->fd,bmp->map+y*bmp->width,xsize)outfd,"[bmp] unable to write image data line %d\n",y); + return B_E_WRITE_DATA; + } + if(write(bmp->fd,buf,fill)outfd,"[bmp] unable to write fill bytes\n"); + return B_E_WRITE_DATA; + } + } + + close(bmp->fd); + + return B_SUCCESS; +} + +int bmp_cut_grab_bottom(t_bmp *dst,t_bmp *src,int dz,unsigned char m) { + + int off; + + dst->width=src->width; + dst->height=dz; + + if(dz>src->height) { + dprintf(src->outfd,"[bmp] cut region greater than image height\n"); + return B_E_GEOMETRY; + } + + if(bmp_alloc_map(dst)!=B_SUCCESS) { + dprintf(dst->outfd,"[bmp] no map memory\n"); + return B_E_MEM; + } + + off=(m==GRAB)?0:(src->height-dz)*src->width; + memcpy(dst->map,src->map+off,dz*src->width*sizeof(t_pixel)); 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; } @@ -133,17 +190,20 @@ int bmp_read_file(t_bmp *bmp) { return B_HI_FAIL; } - bmp->map=(t_pixel *)malloc(bmp->info.width*bmp->info.height*sizeof(t_pixel)); + bmp->width=bmp->info.width; + bmp->height=bmp->info.height; + + bmp->map=(t_pixel *)malloc(bmp->width*bmp->height*sizeof(t_pixel)); if(bmp->map==NULL) { dprintf(bmp->outfd,"[bmp] malloc of map memory failed\n"); return B_E_MEM; } - crop=(bmp->info.imagesize/bmp->info.height)%4; - xsize=(bmp->info.bpp/8)*bmp->info.width; + crop=bmp->info.imagesize/bmp->height-bmp->width*(bmp->info.bpp/8); + xsize=(bmp->info.bpp/8)*bmp->width; - for(y=0;yinfo.height;y++) { - if(read(bmp->fd,bmp->map+y*bmp->info.width,xsize)height;y++) { + if(read(bmp->fd,bmp->map+y*bmp->width,xsize)outfd,"[bmp] reading image data of line %d failed\n",y); return B_E_READ_DATA; }