📄 sma_bmp.c
字号:
/***********************************************************************
* $Workfile: SMA_bmp.c $
* $Revision: 1.0 $
* $Author: WellsK $
* $Date: Aug 27 2002 08:37:34 $
*
* Project: BMP file structures
*
* Description:
* See the bmp.h header file for a description of this package.
*
* This package uses *malloc*. If you want to use this package, you
* should replace malloc with your own dynamic allocation call if
* malloc is an invalid function.
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/CHIPS/archives/SOC/Source/Graphics/Utilities/BMP conversion/SMA_bmp.c-arc $
*
* Rev 1.0 Aug 27 2002 08:37:34 WellsK
* Initial revision.
*
*
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
* COPYRIGHT (C) 2002 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
**********************************************************************/
#include "SMA_bmp.h"
#include <stdlib.h>
//**********************************************************************
// Functions
//**********************************************************************
/***********************************************************************
*
* Function: bmp_is_header_valid
*
* Purpose:
* Determine if the structure is a BMP structure
*
* Processing:
* The header type (bftype) is examined to match 'BM'. If it matches
* and the file type is uncompressed, then the color depth is examined
* and the return value set to the appropriate color depth enumeration.
* If an unsupported type is found, type INVALID_BMP will be returned.
*
* Parameters:
* bmp_data : Pointer to a BMP data structure.
*
* Outputs:
* None
*
* Returns:
* Enumeration that defines the BMP color depth, or INVALID_BMP if
* the BMP type is unsupported.
*
* Notes:
* None
*
**********************************************************************/
bmp_storage_type bmp_is_header_valid (bmp_type *bmp_data)
{
bmp_storage_type retval = INVALID_BMP;
// Does the structure have the bitmap identifier?
if ((bmp_data->bftype [0] == BMP_ID0) &&
(bmp_data->bftype [1] == BMP_ID1))
{
// Compression field must be empty!
if ((bmp_data->bicompressn == BI_RGB) ||
(bmp_data->bicompressn == BI_RGBA))
{
switch (bmp_data->bibitcount)
{
case 1:
// 1 bit per pixel
retval = BPP1;
break;
case 4:
// 4 bits per pixel
retval = BPP4;
break;
case 8:
// 8 bits per pixel
retval = BPP8;
break;
case 24:
// 24 bits per pixel
retval = BPP24;
break;
default:
// No other modes are valid, keep invalid flag
break;
}
}
}
return retval;
}
/***********************************************************************
*
* Function: bmp_get_color_table
*
* Purpose:
* Returns a pointer to the color table (or NULL if it doesn't exist).
*
* Processing:
* A call to bmp_is_header_valid is performed to determine the BMP
* file type. If the BMP file type is BPP1, BPP4, or BPP8, then the
* color table is assigned a pointer after the BMP header information.
*
* Parameters:
* bmp_data : Pointer to a BMP data structure.
*
* Outputs:
* Nothing
*
* Returns:
* A pointer to the color table, or NULL if one does not exist.
*
* Notes:
* 1, 4, and 8 bit per pixel BMP images have color tables.
*
**********************************************************************/
bmp_color_table_type *bmp_get_color_table (bmp_type *bmp_data)
{
bmp_storage_type bmp_colors;
bmp_color_table_type *table_ptr = (bmp_color_table_type *) NULL;
// Get BMP structure type
bmp_colors = bmp_is_header_valid (bmp_data);
// Does a color table exist?
if ((bmp_colors >= BPP1) && (bmp_colors <= BPP8))
{
// Color table exists, return pointer
table_ptr = (bmp_color_table_type *) &bmp_data->ct_data;
}
return table_ptr;
}
/***********************************************************************
*
* Function: bmp_get_image_data
*
* Purpose:
* Returns a pointer to the BMP image data.
*
* Processing:
* A call to bmp_is_header_valid is performed to determine the BMP
* file type. Based on the BMP file type, the number of entries in
* the color table is computed. The pointer to the image data is
* computed at the end of the header plus an offset for the color
* table.
*
* Parameters:
* bmp_data : Pointer to a BMP data structure.
*
* Outputs:
* Nothing
*
* Returns:
* A pointer to the BMP image data.
*
* Notes:
* None
*
**********************************************************************/
void *bmp_get_image_data (bmp_type *bmp_data)
{
INT_32 ctsize;
// Skip past color table
switch (bmp_is_header_valid (bmp_data))
{
case BPP1:
// Color table size is 2 entries
ctsize = 2;
break;
case BPP4:
// Color table size is 16 entries
ctsize = 16;
break;
case BPP8:
// Color table size is 256 entries
ctsize = 256;
break;
default:
// No color table
ctsize = 0;
}
return (void *) ((UNS_32) &bmp_data->ct_data +
ctsize * sizeof (bmp_color_table_type));
}
/***********************************************************************
*
* Function: bmp_convert_color
*
* Purpose:
* Converts a BMP color table entry to a color_type color.
*
* Processing:
* A color table entry (or raw 24-bit entry) is converted into the
* native (compiled) color type by masking and shifting the red,
* green, and blue components of color and computing the closest color
* in the native format (either 233, 555, or 565).
*
* Parameters:
* color_entry : Color table entry pointer
*
* Outputs:
* None
*
* Returns:
* A converted color_type entry from the color data.
*
* Notes:
* Not valid for 16-bit or 32-bit color formats.
*
**********************************************************************/
color_type bmp_convert_color (bmp_color_table_type *color_entry)
{
color_type cv_color, r, g, b;
// Scale colors to color_type color
r = (color_type)
((UNS_32) color_entry->red * (REDMASK >> REDSHIFT) / 256);
g = (color_type)
((UNS_32) color_entry->green * (GREENMASK >> GREENSHIFT) / 256);
b = (color_type)
((UNS_32) color_entry->blue * (BLUEMASK >> BLUESHIFT) / 256);
cv_color = (b << BLUESHIFT) + (g << GREENSHIFT) + (r << REDSHIFT);
return cv_color;
}
/***********************************************************************
*
* Function: bmp_render_w_palette
*
* Purpose:
* Converts a pelletized (color table) BMP image into a color_type
* image in raw format.
*
* Processing:
* See function.
*
* Parameters:
* bmp_data : pointer to BMP data structure
* buf : pointer of where to place converted image
* pmask : Color table entry mask
* md_shift : incremental mask shift value
* pshift : initial mask shift value
*
* 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_render_w_palette (bmp_type *bmp_data, color_type *buf,
UNS_8 pmask, UNS_8 md_shift, UNS_8 pshift)
{
bmp_color_table_type *color_table;
UNS_8 *image_data;
INT_32 x, xdif, y;
UNS_8 pixel_data, pixel_mask, pixel_shift;
INT_32 table_index, image_start;
UNS_8 mask_shift;
color_type *fbuf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -