📄 bmploader.cpp
字号:
}
/*****************************************************************************
* FUNCTION
* BMP_load_info_header
* DESCRIPTION
*
* PARAMETERS
* h [?]
* file [?]
* RETURNS
* void
*****************************************************************************/
void BMP_load_info_header(bitmap_info_header *h, bytestream *file)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
h->header_size = bytestream_fgetdword(file);
h->width = bytestream_fgetdword(file);
h->height = bytestream_fgetdword(file);
h->number_of_colors = bytestream_fgetword(file);
h->bits_per_pixel = bytestream_fgetword(file);
h->compression = bytestream_fgetdword(file);
h->bitmap_size = bytestream_fgetdword(file);
h->device_width = bytestream_fgetdword(file);
h->device_height = bytestream_fgetdword(file);
h->number_of_colors = bytestream_fgetdword(file);
h->number_of_important_colors = bytestream_fgetdword(file);
}
/*****************************************************************************
* FUNCTION
* BMP_load
* DESCRIPTION
*
* PARAMETERS
* file [?] input bitmap file
* b [?] output bitmap
* color_depth [IN] output bitmap color depth
* RETURNS
*
*****************************************************************************/
U8 BMP_load(bytestream *file, bitmap *b, S8 color_depth)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
bitmap_file_header file_header;
bitmap_info_header info_header;
S32 ncolors, i, j, width, height;
S32 bitmap_size, row_bytes, used_row_bytes, total_bits;
S32 dest_row_bytes = 0;
U8 *buffer;
U8 palette[256][3];
U32 image_static_row_data[(IMAGE_STATIC_ROW_DATA_SIZE + 3) / 4]; /* Made image_static_row_data as automatic data *///070306 Alpha layer
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
BMP_load_file_header(&file_header, file);
if (((file_header.file_type & 0xff) != 'B') &&
((file_header.file_type >> 8) != 'M'))
{
return (0);
}
BMP_load_info_header(&info_header, file);
/* info_header.bits_per_pixel; */
width = info_header.width;
height = info_header.height;
ncolors = 1 << info_header.bits_per_pixel;
if (info_header.bits_per_pixel == 24)
{
used_row_bytes = width * 3;
}
else if (info_header.bits_per_pixel == 32)
{
used_row_bytes = width * 4;
}
else if (info_header.bits_per_pixel == 16)
{
used_row_bytes = width * 2;
}
else if (info_header.bits_per_pixel <= 8)
{
total_bits = width * info_header.bits_per_pixel;
used_row_bytes = total_bits >> 3; /* total_bits/8 */
if ((total_bits % 8) > 0)
{
used_row_bytes += 1; /* total_bits%8 */
}
}
else
{
return (0);
}
if ((used_row_bytes % 4) > 0)
{
row_bytes = used_row_bytes + (4 - (used_row_bytes % 4));
}
else
{
row_bytes = used_row_bytes;
}
if (color_depth <= 8)
{
dest_row_bytes = width / (8 / color_depth);
if ((width % (8 / color_depth)) > 0)
{
dest_row_bytes++;
}
}
else
{
if (color_depth == 24)
{
dest_row_bytes = width * 3;
}
else if (color_depth == 32)
{
dest_row_bytes = width * 4;
}
else if ((color_depth == 12) || (color_depth == 16))
{
dest_row_bytes = width * 2;
}
}
bitmap_size = dest_row_bytes * height;
b->xsize = width;
b->ysize = height;
b->color_depth = color_depth;
b->row_bytes = dest_row_bytes;
if (color_depth <= 8)
{
b->palette = (U8*) image_static_palette;
}
else
{
b->palette = NULL;
}
b->data = (U8*) image_static_buffer;
if (info_header.bits_per_pixel <= 8)
{
for (i = 0; i < ncolors; i++)
{
palette[i][2] = bytestream_fgetbyte(file);
palette[i][1] = bytestream_fgetbyte(file);
palette[i][0] = bytestream_fgetbyte(file);
bytestream_fgetbyte(file);
}
}
bytestream_fseek(file, file_header.bitmap_offset, SEEK_SET);
buffer = (U8*) image_static_row_data;
for (j = height - 1; j >= 0; j--)
{
for (i = 0; i < row_bytes; i++)
{
buffer[i] = bytestream_fgetbyte(file);
}
BMP_write_line(b, buffer, row_bytes, (S8) info_header.bits_per_pixel, color_depth, width, j, palette);
}
return (1);
}
/*****************************************************************************
* FUNCTION
* BMPLoader
* DESCRIPTION
*
* PARAMETERS
* in_filename [?]
* out_filename [?]
* RETURNS
*
*****************************************************************************/
U8 BMPLoader(char *in_filename, char *out_filename, S8 color_depth)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
bitmap outimage;
FILE *infile;
FILE *outfile;
bytestream inimage;
S32 file_length;
U8 *buffer;
S32 i, bitmap_size, total_data_size;
U8 ret;
U32 width, height, width_height;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
infile = fopen(in_filename, "rb");
if (infile == NULL)
{
printf("\nError opening %s", in_filename);
return 0;
}
outfile = fopen(out_filename, "wb+");
if (outfile == NULL)
{
printf("\nError creating %s", out_filename);
fclose(infile);
return 0;
}
fseek(infile, 0, SEEK_END);
file_length = ftell(infile);
fseek(infile, 0, SEEK_SET);
buffer = (U8*) malloc(file_length);
if (buffer == NULL)
{
printf("\nError allocating memory of size %d bytes", file_length);
fclose(infile);
fclose(outfile);
return 0;
}
fread(buffer, 1, file_length, infile);
fclose(infile);
bytestream_initialize(&inimage, buffer, file_length);
ret = BMP_load(&inimage, &outimage, color_depth);
if (ret == 0)
{
printf("\nFormat not supported %s", in_filename);
free(buffer);
fclose(outfile);
return 0;
}
bitmap_size = outimage.row_bytes * outimage.ysize;
total_data_size = bitmap_size + 13;
if ((total_data_size >> 24) > 0)
{
fprintf(stderr, "image is too big:%s\n", in_filename);
}
if (Image_Get_Dimensions(in_filename, &width, &height) >= 0)
{
width_height = ((width & 0XFFF) << 12) | (height & 0XFFF);
}
else
{
printf("\nGet dimensions failed:\t%s", in_filename);
return 0;
}
fprintf(outfile, "{\n\t");
/* resource header */
fprintf(outfile, "0x%02X, ", 0x04);
fprintf(outfile, "0x%02X, ", 0x01);
total_data_size = bitmap_size + 13;
fprintf(
outfile,
"0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X,\n\t",
(total_data_size & 0x000000ff),
(total_data_size & 0x0000ff00) >> 8,
(total_data_size & 0x00ff0000) >> 16,
(width_height & 0x000000ff),
(width_height & 0x0000ff00) >> 8,
(width_height & 0x00ff0000) >> 16);
/* BMP resource header */
fprintf(
outfile,
"0x%02X, 0x%02X, 0x%02X, 0x%02X, ",
(outimage.xsize & 0x000000ff),
(outimage.xsize & 0x0000ff00) >> 8,
(outimage.xsize & 0x00ff0000) >> 16,
(outimage.xsize & 0xff000000) >> 24);
fprintf(
outfile,
"0x%02X, 0x%02X, 0x%02X, 0x%02X, ",
(outimage.ysize & 0x000000ff),
(outimage.ysize & 0x0000ff00) >> 8,
(outimage.ysize & 0x00ff0000) >> 16,
(outimage.ysize & 0xff000000) >> 24);
fprintf(outfile, "0x%02X, ", outimage.color_depth);
fprintf(
outfile,
"0x%02X, 0x%02X, 0x%02X, 0x%02X, ",
(outimage.row_bytes & 0x000000ff),
(outimage.row_bytes & 0x0000ff00) >> 8,
(outimage.row_bytes & 0x00ff0000) >> 16,
(outimage.row_bytes & 0xff000000) >> 24);
for (i = 0; i < bitmap_size; i++)
{
if ((i % 16) == 0)
{
fprintf(outfile, "\n\t");
}
fprintf(outfile, "0x%02X, ", outimage.data[i]);
}
fprintf(outfile, "\n};");
fclose(outfile);
free(buffer);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -