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;
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;
}
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");
+ return B_WRONG_MODE;
+ }
xsize=bmp->width*3;
fill=(4-(xsize%4))%4;
bmp->info.ic=0;
/* write it */
- ...
+ 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;
+ }
+ if(write(bmp->fd,&(bmp->hdr),BMP_H_SIZE)<BMP_H_SIZE) {
+ dprintf(bmp->outfd,"[bmp] unable to write bmp header\n");
+ return B_E_WRITE_DATA;
+ }
+
+ if(write(bmp->fd,&(bmp->info),BMP_I_SIZE)<BMP_I_SIZE) {
+ dprintf(bmp->outfd,"[bmp] unable to write bmp info\n");
+ return B_E_WRITE_DATA;
+ }
+
+ for(y=0;y<bmp->height;y++) {
+ if(write(bmp->fd,bmp->map+y*bmp->width,xsize)<xsize) {
+ dprintf(bmp->outfd,"[bmp] unable to write image data line %d\n",y);
+ return B_E_WRITE_DATA;
+ }
+ if(write(bmp->fd,buf,fill)<fill) {
+ dprintf(bmp->outfd,"[bmp] unable to write fill bytes\n");
+ return B_E_WRITE_DATA;
+ }
+ }
+
+ close(bmp->fd);
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");
- return B_NO_READ_MODE;
+ dprintf(bmp->outfd,"[bmp] read mode not specified\n");
+ return B_WRONG_MODE;
}
if((bmp->fd=open(bmp->file,O_RDONLY))<0) {