/** * Create the BMP object with specified width and height and depth. */ bmpfile_t * bmp_create(uint32_t width, uint32_t height, uint32_t depth) { bmpfile_t *result; double bytes_per_pixel; uint32_t bytes_per_line; uint32_t palette_size; if (depth != 1 && depth != 4 && depth != 8 && depth != 16 && depth != 24 && depth != 32) return NULL; result = malloc(sizeof(bmpfile_t)); memset(result, 0, sizeof(bmpfile_t)); result->header.magic[0] = 'B'; result->header.magic[1] = 'M'; result->dib.header_sz = 40; result->dib.width = width; result->dib.height = height; result->dib.nplanes = 1; result->dib.depth = depth; result->dib.hres = DEFAULT_DPI_X; result->dib.vres = DEFAULT_DPI_Y; if (depth == 16) result->dib.compress_type = BI_BITFIELDS; else result->dib.compress_type = BI_RGB; bmp_malloc_pixels(result); bmp_malloc_colors(result); /* Calculate the field value of header and DIB */ bytes_per_pixel = (result->dib.depth * 1.0) / 8.0; bytes_per_line = (int)ceil(bytes_per_pixel * result->dib.width); if (bytes_per_line % 4 != 0) bytes_per_line += 4 - bytes_per_line % 4; result->dib.bmp_bytesz = bytes_per_line * result->dib.height; palette_size = 0; if (depth == 1 || depth == 4 || depth == 8) palette_size = uint32_pow(2, result->dib.depth) * 4; else if (result->dib.depth == 16) palette_size = 3 * 4; result->header.offset = 14 + result->dib.header_sz + palette_size; result->header.filesz = result->header.offset + result->dib.bmp_bytesz; return result; }
bmp_structp bmp_create_bitmap(uint32_t width, uint32_t height, uint16_t depth) { bmp_structp result; double bpp; uint32_t bpl; uint32_t palette_size; if (!bmp_validate_bit_depth(depth)) return NULL; result = (bmp_structp)malloc(sizeof(bmp_struct)); memset(result, 0, sizeof(bmp_struct)); result->header.magic[0] = 'B'; result->header.magic[1] = 'M'; result->dib.header_sz = 40; // ? sizeof(bmp_header_t) ? result->dib.width = width; result->dib.height = height; result->dib.nplanes = 1; result->dib.depth = depth; result->dib.hres = DEFAULT_DPI_X; result->dib.vres = DEFAULT_DPI_Y; // COMPRESS TYPE bmp_malloc_pixels(result); bmp_malloc_colors(result); // calculate the number bytes in the bitmap. bpp = result->dib.depth / 8.0; bpl = (int)ceil(bpp * result->dib.width); bpl += bpl % 4; // add padding if necessary. result->dib.bmp_bytesz = bpl * result->dib.height; // calculate the palette size given specified depth. palette_size = 0; if (depth <= 8) palette_size = (1 << result->dib.depth) * 4; else if (depth == 16) palette_size = 12; result->header.offset = 14 + result->dib.header_sz + palette_size; result->header.filesz = result->header.offset + result->dib.bmp_bytesz; rgb_pixel_t* test = (rgb_pixel_t*)(&result->header + result->header.offset); return result; }
bmp_t bmp_create (uint32_t width, uint32_t height, uint32_t depth) { bmp_t *result = new bmp_t; result->magic[0] = 'B'; result->magic[1] = 'M'; result->dib.header_size = 40; result->dib.width = width; result->dib.height = height; result->dib.nplanes = 1; result->dib.depth = depth; result->dib.compress_type = 0; result->dib.bmp_byte_size = result->dib.hres = default_dpi_x; result->dib.vres = default_dpi_y; bmp_malloc_pixels(result); bmp_malloc_colors(result); return result; }
/** * Create the BMP from file. * Written by MewCatcher 2013/5/3 */ bmpfile_t * bmp_create_from_file( const char *filename ) /* This function is written by MewCatcher(xiayuanzhong) */ { FILE *fp; unsigned char *buf; /* temporary */ unsigned long len; /* Open the file */ if ( ( fp = fopen( filename, "rb" ) ) == NULL ) return FALSE; /* Get file Size */ fseek( fp, 0, SEEK_END ); len = ftell( fp ); fseek( fp, 0, SEEK_BEG ); if( len <= 40 ) return FALSE; /* Read the file */ buf = (unsigned char*) malloc( len * sizeof( unsigned char ) ); fread( buf, sizeof( unsigned char ), len, fp ); fclose( fp ); /* Don't need to use again, so close it. */ bmp_header_t *BMP_HEADER_TEMP = (bmp_header_t *)buf; bmp_dib_v3_header_t *BMP_DIB_V3_HEADER_TEMP = (bmp_dib_v3_header_t *)( BMP_HEADER_TEMP + 1 ); /* Prepare */ bmpfile_t *result; result = malloc( sizeof( bmpfile_t ) ); /* Don't need to set zero */ /* Copy to result -> header */ result -> header.magic[ 0 ] = BMP_HEADER_TEMP -> magic[ 0 ]; result -> header.magic[ 1 ] = BMP_HEADER_TEMP -> magic[ 0 ]; result -> header.filesz = BMP_HEADER_TEMP -> filesz; result -> header.creator1 = BMP_HEADER_TEMP -> creator1; result -> header.creator2 = BMP_HEADER_TEMP -> creator2; result -> header.offset = BMP_HEADER_TEMP -> offset; /* Copy to result -> dib */ result -> dib.header_sz = BMP_DIB_V3_HEADER_TEMP -> header_sz; result -> dib.width = BMP_DIB_V3_HEADER_TEMP -> width; result -> dib.height = BMP_DIB_V3_HEADER_TEMP -> height; result -> dib.nplanes = BMP_DIB_V3_HEADER_TEMP -> nplanes; result -> dib.depth = BMP_DIB_V3_HEADER_TEMP -> depth; result -> dib.compress_ty = BMP_DIB_V3_HEADER_TEMP -> compress_ty; result -> dib.bmp_bytesz = BMP_DIB_V3_HEADER_TEMP -> bmp_bytesz; result -> dib.hres = BMP_DIB_V3_HEADER_TEMP -> hres; result -> dib.vres = BMP_DIB_V3_HEADER_TEMP -> vres; result -> dib.ncolors = BMP_DIB_V3_HEADER_TEMP -> ncolors; result -> dib.nimpcolors = BMP_DIB_V3_HEADER_TEMP -> nimpcolors; /* Set **pixels & *colors*/ int i; bmp_malloc_pixels( result ); if ( result -> dib.depth == 1 || result -> dib.depth == 4 || bmp -> result.depth == 8 ) { result -> colors = malloc( sizeof( rgb_pixel_t ) * result -> dib.ncolors ); /* Here need to load color table */ unsigned char *t = (unsigned char*)( BMP_DIB_V3_HEADER_TEMP + 1 ) for( i = 0; i < result -> dib.ncolors; i ++ ) { result -> colors[ i ].blue = t[ 0 + 4 * i ]; result -> colors[ i ].green = t[ 1 + 4 * i ]; result -> colors[ i ].red = t[ 2 + 4 * i ]; result -> colors[ i ].alpha = t[ 3 + 4 * i ]; } }