📄 sma_bmp.c
字号:
// Get pointers to color table and image data
color_table = bmp_get_color_table (bmp_data);
image_start = (INT_32) bmp_get_image_data (bmp_data);
image_data = (UNS_8 *) image_start;
// Clear initial pixel mask
pixel_mask = 0x0;
// Convert Y axis first
y = bmp_data->biheight - 1;
while (y >= 0)
{
// Reset buf to start from left to right
fbuf = buf + (bmp_data->biwidth * y);
// Render a single line
x = bmp_data->biwidth - 1;
while (x >= 0)
{
// A byte contains data for up to 8 pixels, once the
// pixel mask has reached 0x0, load a new byte and reset
// the mask
if (pixel_mask == 0x0)
{
// Reset pixel mask and shift value
pixel_mask = pmask;
pixel_shift = pshift;
mask_shift = md_shift;
// get a new byte and increment image pointer
pixel_data = *image_data;
image_data++;
}
// Determine index into color table
table_index = (INT_32)
((pixel_data & pixel_mask) >> mask_shift);
// Use the appropriate index in the color table
*fbuf = bmp_convert_color (&color_table [table_index]);
fbuf++;
// Update x coordinate
x--;
// Shift pixel mask to next bit
pixel_mask = pixel_mask >> pixel_shift;
mask_shift = mask_shift - pshift;
}
y--;
// Since a horizontal line is 'padded' to end on a 32-bit
// boundary, the image pointer may need to be adjusted for the
// next line
x = ((INT_32) image_data - image_start) / 4;
xdif = x * 4;
image_data = image_data + (((INT_32) image_data -
image_start) - xdif);
// Clear pixel_mask to reset pixel mask and shift values
pixel_mask = 0x0;
}
}
/***********************************************************************
*
* Function: bmp_render24
*
* Purpose:
* Converts a raw 24-bit RGB image to a color_type raw image.
*
* Processing:
* See function.
*
* Parameters:
* bmp_data : pointer to BMP data structure
* buf : pointer of where to place converted image
*
* Outputs:
* The buffer 'buf' will be updated with the raw image data in the
* native color format.
*
* Returns:
* Nothing
*
* Notes:
* This function is private to this module.
*
**********************************************************************/
void bmp_render24 (bmp_type *bmp_data, color_type *buf)
{
bmp24_color_table_type *image_data;
INT_32 x, y;
color_type *fbuf;
// Get pointer to image data - each pixel entry can be considered
// a color table / palette entry
image_data =
(bmp24_color_table_type *) bmp_get_image_data (bmp_data);
// Convert Y axis first
y = bmp_data->biheight - 1;
while (y >= 0)
{
// Reset buf to start from left to right
fbuf = buf + (bmp_data->biwidth * y);
// Render a single line
x = bmp_data->biwidth - 1;
while (x >= 0)
{
// Use the appropriate index in the color table
*fbuf = bmp_convert_color (
(bmp_color_table_type *) image_data);
// Update source to next pixel data set
image_data++;
fbuf++;
// Update x coordinate
x--;
}
// Since a horizontal line is 'padded' to end on a 32-bit
// boundary, the image pointer may need to be adjusted for the
// next line
x = bmp_data->biwidth / 4;
x = x * 4;
image_data = (bmp24_color_table_type *) ((INT_32) image_data +
(bmp_data->biwidth - x));
y--;
}
}
/***********************************************************************
*
* Function: bmp_convert_image
*
* Purpose:
* Convert a BMP image to a color_type image
*
* Processing:
* See function.
*
* Parameters:
* bmp_data : pointer to a BMP data structure
* xsize : Pointer to place the horizontal size of the image
* ysize : Pointer to place the vertical size of the image
* bufout : Pointer to where to place the converted image
*
* Outputs:
* The values of xsize, ysize, and bufout will be updated.
*
* Returns:
* Nothing
*
* Notes:
* Only uncompressed 1, 4, 8, and 24 bit per pixel formats are
* supported. Before converting, be sure that the target buffer,
* bufout, is large enough for the converted image.
*
**********************************************************************/
bmp_storage_type bmp_convert_image (bmp_type *bmp_data, INT_16 *xsize,
INT_16 *ysize, color_type *bufout)
{
bmp_storage_type bmpcolors;
// Get BMP image type
bmpcolors = bmp_is_header_valid (bmp_data);
if (bmpcolors != INVALID_BMP)
{
// Save horizontal and vertical sizes
*xsize = bmp_data->biwidth;
*ysize = bmp_data->biheight;
// Good table entry
switch (bmpcolors)
{
case BPP1:
// 1 bit per pixel, call function
bmp_render_w_palette (bmp_data, bufout, 0x80, 7, 1);
break;
case BPP4:
// 4 bits per pixel, call function
bmp_render_w_palette (bmp_data, bufout, 0xF0, 4, 4);
break;
case BPP8:
// 8 bits per pixel, call function
bmp_render_w_palette (bmp_data, bufout, 0xFF, 0, 8);
break;
case BPP24:
// 24 bits per pixel, call function
bmp_render24 (bmp_data, bufout);
break;
default:
// Invalid or unsupported BMP image
*xsize = 0;
*ysize = 0;
break;
}
}
else
{
// Invalid or unsupported BMP image
*xsize = 0;
*ysize = 0;
}
return bmpcolors;
}
/***********************************************************************
*
* Function: bmp_convert_image
*
* Purpose:
* Allocates storage for a new BMP file structure.
*
* Processing:
* This function computes the required size needed for the BMP header,
* color table, and image data, based on the color depth. Memory
* for an image (with header and color table) is allocated and the
* pointer returned to the caller.
*
* Parameters:
* xsize : Horizontal size of the image storage space
* ysize : Vertical size of the image storage space
* bits_per_pixel : number of bits per pixel, used to set the size of
* the buffer and color table (enumerator)
*
* Outputs:
* Nothing
*
* Returns:
* A pointer to a new allocated BMP structure, or NULL if an error
* occurred.
*
* Notes:
* The bits_per_pixel parameter is important for optimal memory usage.
* Setting this value will 'adjust' the sizing of the allocated BMP
* structure, modifying the sizes of the color table and data area.
* If unsure of the bits per pixel, use BPP24 - extra memory will be
* allocated for BPP24, but no memory allocation problems will occur.
*
**********************************************************************/
bmp_type *bmp_allocate_structure (INT_32 xsize, INT_32 ysize,
bmp_storage_type bits_per_pixel)
{
INT_32 alloc_size, ctable_size, dentry_size;
// Need room for the basic structure size
alloc_size = sizeof (bmp_type);
// Select size of color table and data area based on bits per pixel
switch (bits_per_pixel)
{
case BPP1:
ctable_size = 2;
dentry_size = 1;
break;
case BPP4:
ctable_size = 16;
dentry_size = 4;
break;
case BPP8:
ctable_size = 256;
dentry_size = 8;
break;
case BPP24:
default:
ctable_size = 0; // No color table
dentry_size = 24;
break;
}
// Add in data area size, - size based on bits per pixel (8 extra
// bytes are included at the end of the allocated memory for
// safety)
alloc_size = alloc_size +
(ctable_size * sizeof (bmp_color_table_type)) +
(xsize * ysize * dentry_size / 8) + 8;
// Return pointer to allocated structure
return (bmp_type *) malloc (alloc_size);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -