📄 bmp2c.cpp
字号:
/**********************************************************************
* $Workfile: bmp2c.cpp $
* $Revision: 1.1 $
* $Author: StokerD $
* $Date: Aug 31 2001 18:45:08 $
*
* Project: LH79791 EVB
*
* Usage:
* bmp2c infile[.bmp] [-m]
* if the .bmp extension is omitted, it will be automaticlly supplied.
* if the -m option is specified, then a bit mask array is created with
* a minimum stride of eight bits.
*
* Description:
* This program reads an 8-bit, 256 color Microsoft Windows(TM)
* .BMP file and converts it into a C structure with enough information to
* create a UGL bitmap: the bitmap width, the bitmap height, the 256 color
* RGB palette, and an array of 8 bit indices into the palette, one per pixel
* If the -m option is specified, then a bit mask array is generated. The
* following macro will create a UGL bitmap that corresponds to the infile
* name without the extension and set d to the UGL_BITMAP_ID value. It is
* assumed that the indicies allocated colors are stored in the color_remap[]
* array. The bitmap uses no mask.
* #define MAKE_BITMAP(d,n)\
* for (i = 0; i < n##_h * n##_w; i++)\
* {\
* color_data[i] = color_remap##[ n[i]];\
* }\
*\
* d = uglBitmapCreate(gc,\
* n##_w,\
* n##_h,\
* n##_w,\
* color_data,\
* 0, \
* 0);
* the next macro is the same as MAKE_BITMAP except that the black areas of the
* bitmap will be drawn transparent.
* #define MAKE_SPRITE(d,n)\
* for (i = 0; i < n##_h * n##_w; i++)\
* {\
* color_data[i] = color_remap##[ n[i]];\
* }\
* \
* d = uglBitmapCreate(gc,\
* n##_w,\
* n##_h,\
* n##_w,\
* color_data,\
* max( (n##_w),8), \
* n##_mask);
*
* If the bitmap file is 24-bit color, the program converts it into a C
* structure with enough information to create a UGL bitmap: the bitmap
* width, the bitmap height, the pixel array encoded into 16 bits.
*
*
*
* Portability issues:
* This file relies on the _splitpath library function in stdlib.h
* _splitpath is available on Microsoft and Borland PC-based compilers
* It may not be available in other environments.
*
* Revision History:
* $Log: P:/PVCS6_6/archives/Tools/bmp2c.cpp-arc $
*
* Rev 1.1 Aug 31 2001 18:45:08 StokerD
* Merged with Jun Li's bmp2c.cpp file
* Added ability to handle 4 bit bmp file
*
* Rev 1.0 10 May 2001 15:41:38 KovitzP
* Initial revision.
//
// Rev 1.8 Sep 22 2000 17:41:54 KovitzP
// palette variable is also there
//
// Rev 1.7 Sep 22 2000 13:07:00 KovitzP
// Fixed the BPP #define to be correcto for 16 BPP bitmaps
//
// Rev 1.6 Sep 21 2000 11:48:26 KovitzP
// adds define constant to extract bits per pixel.
//
// Rev 1.5 Sep 20 2000 13:13:48 KovitzP
// create header file for 16-bit bitmaps
//
// Rev 1.4 Sep 19 2000 07:52:22 KovitzP
// Declarations are generic for ARM and independent of UGL
//
// Rev 1.3 Jun 07 2000 14:55:24 KovitzP
// added comments about macros and
// the minimum mask stride of 8
//
// Rev 1.2 May 25 2000 14:09:18 KovitzP
// Added mask bit option and made UGL_INT8
// variables UGL_UINT8.
//
// Rev 1.1 May 23 2000 16:29:18 KovitzP
// Added support for 256 color bitmaps and
// tightened binding to UGL. Changed usage
// so that only an input file name is required.
// 24-bit bitmaps not tested.
//
// Rev 1.0 May 04 2000 14:47:36 KovitzP
// Initial Version seems to work. Tested with
// 640 x 480 24-bit .BMP file
*
*
* COPYRIGHT (C) 2000 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ms_bmp.h"
/*
this union defines a 16-bit pixel that allocates 5 bits each to
red, greed and blue and keeps one pixel 0
*/
typedef union tagLCD555
{
struct
{
WORD blue:5;
WORD green:5;
WORD red:5;
WORD unused:1;
} color;
WORD pixel;
} LCD555;
/* from Borland C++ help file */
typedef struct tagRGBTRIPLE
{ // rgbt
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} RGBTRIPLE;
/* from ugltype.h */
/* RGB colors */
typedef unsigned long UGL_ARGB;
typedef UGL_ARGB UGL_RGB;
#define UGL_RGB_RED(rgb) (UGL_RGB)(((rgb) >> 16) & 0xFF)
#define UGL_RGB_GREEN(rgb) (UGL_RGB)(((rgb) >> 8) & 0xFF)
#define UGL_RGB_BLUE(rgb) (UGL_RGB)((rgb) & 0xFF)
#define UGL_MAKE_ARGB(alpha, red, green, blue) \
(((UGL_ARGB)(alpha) << 24) \
| ((UGL_ARGB)(red) << 16) \
| ((UGL_ARGB)(green) << 8) \
| (UGL_ARGB)(blue))
#define UGL_MAKE_RGB(red, green, blue) \
UGL_MAKE_ARGB (0xff, (red), (green), (blue))
#define UGL_COLOR_TRANSPARENT ((UGL_COLOR)0x00010101L)
/**********************************************************************
*
* Function: main()
*
* Purpose:
* See file header, above
*
* Processing:
* Extract bitmap file name from argv[1]
* Open the bitmap file
* Read the bitmap header information and pixel data
* Close bitmap file
* Extract the output file name from the bitmap file name and change ext to .c
* Open C file for writing
* Write bitmap information to C file
* Write a C header file from the bitmap file name .h
*
* Parameters:
* argc--must be 3
* argv[0]--function name (supplied by OS)
* argv[1]--bitmap input file name
* argv[2]--optional -m flag causes mask array to be generated
* Outputs:
* C-formatted bitmap data to specified file name
* Errors to stderr
*
* Returns:
*
* 0 normally
* -1 if there is a problem
*
* Notes:
* The function "init_spi_slave" must be called to obtain a
* slave ID prior to calling this function.
*
**********************************************************************/
int main(int argc, char * argv[])
{
BITMAPFILEHEADER bf;
BITMAPINFO bmi;
FILE * infile, * outfile;
DWORD bytes_read=0;
DWORD bmpsize;
BYTE *bmp,dummy;
DWORD bytes_per_line;
BYTE *next_color;
int i,j,k;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char file[_MAX_FNAME];
char ext[_MAX_EXT];
char of_name[_MAX_FNAME+_MAX_EXT];
if ( !((argc == 2) || (argc == 3 && (strcmp(argv[2],"-m") == 0))) )
{
fprintf(stderr,"%s: usage:\n\tbmp2c.exe infile[.bmp] [-m]\n"
"where\n\tinfile is a 256 color Microsoft(tm) BMP file\n"
"\t-m will cause generation of a mask array for black bits\n",
argv[0]);
exit(-1);
}
/* split the string to separate elementss */
_splitpath(argv[1],drive,dir,file,ext);
strcpy(file,argv[1]);
if (strlen(ext) == 0)
{
strcat(file,".bmp");
}
infile = fopen(file, "rb");
if (infile)
{
if (fread(&bf,sizeof(bf),1,infile) == 1)
bytes_read += sizeof(bf);
if (fread(&bmi,sizeof(bmi.bmiHeader),1,infile) == 1)
bytes_read += sizeof(bmi.bmiHeader);
bmpsize = bf.bfSize - bf.bfOffBits;
bmp = (BYTE *)malloc(bmpsize);
if (bmi.bmiHeader.biBitCount == 24)
{
/* Read the 24-bit bitmap file and write the C output */
LCD555 *pixel_line;
pixel_line = (LCD555 *)malloc(bmi.bmiHeader.biWidth
*sizeof(pixel_line[0]));
if (bmp == NULL)
{
fprintf(stderr,"%s: cannot allocate memory for bitmap\n",argv[0]);
fclose(infile);
free(bmp);
free(pixel_line);
exit(-1);
}
else
{
/* seek to the beginning of the data */
while (bytes_read < bf.bfOffBits)
{
if (fread(&dummy,sizeof(dummy),1,infile)!=1)
{
fprintf(stderr,"%s: error seeking to bitmap data\n",argv[0]);
fclose(infile);
free(bmp);
free(pixel_line);
exit(-1);
}
++bytes_read;
}
bytes_read = fread(bmp,sizeof(bmp[0]),bmpsize,infile);
if (bytes_read != bmpsize)
{
fprintf(stderr,"%s: cannot read bitmap data\n",argv[0]);
fclose(infile);
free(bmp);
free(pixel_line);
exit(-1);
}
}
/*bitmap is read*/
fclose(infile);
_splitpath(argv[1],drive,dir,file,ext);
strcpy(of_name,file);
strcat(of_name,".c");
outfile = fopen(of_name,"w");
/* write bitmap RGB data out in 5 5 5 (RGB) encoding*/
/*
compute bytes_per_line so that each line ends on an aligned,
4-byte boundary.
*/
bytes_per_line = (((DWORD)bmi.bmiHeader.biWidth *
(DWORD)bmi.bmiHeader.biBitCount + 31L) / 32L) * 4L;
fprintf (outfile,"/*\nThis file, %s, was automatically generated from"
" file\n %s \nusing utility\n %s $Revision: 1.1 $.\n"
"Please do not edit it.\n*/\n",of_name,argv[1],argv[0]);
_splitpath(argv[1],drive,dir,file,ext);
fprintf (outfile,
"\n\n/*\Below is a set of three variables: %s_w, the width,\n"
"%s_h, the height, and the array %s[], an array of\n"
" 16-bit 555-encoded RGB pixel values.\n"
"Bits 4-0 encode blue intensity.\n"
"Bits 9-5 encode green intensity.\n"
"Bits 14-10 encode green intensity.\n"
"Bit 15 is always 0\n*/\n",file,file,file);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -