Files
LCUI/test/graph2array.c
T

132 lines
3.1 KiB
C
Raw Normal View History

2013-01-06 17:58:24 +08:00
/*
* 此程序用于将图形数据转换成数组,以实现图形数据内置于程序
* */
#include <LCUI_Build.h>
#include LC_LCUI_H
#include LC_GRAPH_H
#include LC_DRAW_H
#define MAX_ROW_PIXEL_NUM 11
2013-11-14 17:18:59 +08:00
static char help_text[]={
2013-01-06 17:58:24 +08:00
"picture file to C code."
"usage:\n"
"%s image_file_path\n"
};
2013-11-14 17:18:59 +08:00
/** Converte image data to C code, and write to the source code file */
static int GraphConvertToCode( LCUI_Graph *buff, char *out_filepath )
2013-01-06 17:58:24 +08:00
{
int i;
FILE *fp;
2013-06-16 15:23:44 +08:00
char file[1024];
2013-06-11 21:07:47 +08:00
2013-06-16 15:23:44 +08:00
sprintf( file, "%s.c", out_filepath );
2013-01-06 17:58:24 +08:00
printf("output file: %s\n", file);
2013-11-14 17:18:59 +08:00
/* open output file */
2013-01-06 17:58:24 +08:00
fp = fopen( file, "w+" );
if( !fp ) {
return -1;
}
2013-06-16 15:23:44 +08:00
fprintf( fp, "/* this code generate from file %s */\n", out_filepath );
fprintf( fp,
"#include <LCUI_Build.h>\n"
"#include LC_LCUI_H\n"
"#include LC_GRAPH_H\n\n"
2013-01-06 17:58:24 +08:00
);
/* red channel */
2013-06-16 15:23:44 +08:00
fprintf( fp, "static unsigned char graph_red[] = {" );
2013-11-14 17:18:59 +08:00
for( i=0; i<buff->w*buff->h; ++i ) {
2013-01-06 17:58:24 +08:00
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n\t" );
2013-01-06 17:58:24 +08:00
}
fprintf( fp, "0x%02x,", buff->rgba[0][i] );
}
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n};\n\n" );
2013-06-11 21:07:47 +08:00
2013-01-06 17:58:24 +08:00
/* green channel */
2013-06-16 15:23:44 +08:00
fprintf( fp, "static unsigned char graph_green[] = {" );
2013-11-14 17:18:59 +08:00
for( i=0; i<buff->w*buff->h; ++i ) {
2013-01-06 17:58:24 +08:00
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n\t" );
2013-01-06 17:58:24 +08:00
}
fprintf( fp, "0x%02x,", buff->rgba[1][i] );
}
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n};\n\n" );
2013-06-11 21:07:47 +08:00
2013-01-06 17:58:24 +08:00
/* blue channel */
2013-06-16 15:23:44 +08:00
fprintf( fp, "static unsigned char graph_blue[] = {" );
2013-11-14 17:18:59 +08:00
for( i=0; i<buff->w*buff->h; ++i ) {
2013-01-06 17:58:24 +08:00
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n\t" );
2013-01-06 17:58:24 +08:00
}
fprintf( fp, "0x%02x,", buff->rgba[2][i] );
}
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n};\n\n" );
2013-06-11 21:07:47 +08:00
2013-01-06 17:58:24 +08:00
/* alpha channel */
2013-11-14 17:18:59 +08:00
if( buff->color_type == COLOR_TYPE_RGBA ) {
2013-06-16 15:23:44 +08:00
fprintf( fp, "static unsigned char graph_alpha[] = {" );
2013-11-14 17:18:59 +08:00
for( i=0; i<buff->w*buff->h; ++i ) {
2013-01-06 17:58:24 +08:00
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n\t" );
2013-01-06 17:58:24 +08:00
}
fprintf( fp, "0x%02x,", buff->rgba[3][i] );
}
2013-06-16 15:23:44 +08:00
fprintf( fp, "\n};\n\n" );
2013-01-06 17:58:24 +08:00
}
2013-06-11 21:07:47 +08:00
2013-01-06 17:58:24 +08:00
fprintf( fp,
2013-11-14 17:18:59 +08:00
"int InCoreIMG_LoadMyIIMG( LCUI_Graph *buff )\n"
2013-06-16 15:23:44 +08:00
"{\n"
" int ret;\n"
" if( Graph_IsValid( buff ) ) {\n"
2013-01-06 17:58:24 +08:00
" Graph_Free( buff );\n"
" }\n"
" Graph_Init( buff );\n"
2013-11-14 17:18:59 +08:00
" buff->color_type = %s;\n"
2013-01-06 17:58:24 +08:00
" buff->alpha = 255;\n"
" ret = Graph_Create( buff, %d, %d );\n"
" if( ret == 0 ) {\n"
" memcpy( buff->rgba[0], red, sizeof(red) );\n"
" memcpy( buff->rgba[1], green, sizeof(green) );\n"
2013-06-16 15:23:44 +08:00
" memcpy( buff->rgba[2], blue, sizeof(blue) );\n",
2013-11-14 17:18:59 +08:00
buff->color_type == COLOR_TYPE_RGBA? "COLOR_TYPE_RGBA":"COLOR_TYPE_RGB",
buff->w, buff->h
2013-01-06 17:58:24 +08:00
);
2013-11-14 17:18:59 +08:00
if( buff->color_type == COLOR_TYPE_RGBA ) {
2013-06-16 15:23:44 +08:00
fprintf( fp,
2013-11-14 17:18:59 +08:00
" memcpy( buff->rgba[3], alpha, sizeof(alpha) );\n"
2013-06-16 15:23:44 +08:00
);
}
fprintf( fp,
" }\n"
" return ret;\n"
"}\n"
);
2013-01-06 17:58:24 +08:00
fclose( fp );
return 0;
}
int main( int argc, char *argv[] )
{
2013-11-14 17:18:59 +08:00
int ret;
2013-01-06 17:58:24 +08:00
if( argc == 2 ) {
2013-11-14 17:18:59 +08:00
LCUI_Graph graph;
Graph_Init( &graph );
ret = Graph_LoadImage( argv[1], &graph );
if( ret != 0 ) {
return -1;
}
ret = GraphConvertToCode( &graph, argv[1] );
if( ret != 0 ) {
return -2;
}
Graph_Free( &graph );
2013-01-06 17:58:24 +08:00
} else {
2013-11-14 17:18:59 +08:00
printf( help_text, argv[0] );
2013-01-06 17:58:24 +08:00
}
return 0;
}