更新图像转代码的程序代码

This commit is contained in:
Liu Chao
2013-06-16 15:23:44 +08:00
parent 95ec1638ca
commit 4f9581d08e
+37 -44
View File
@@ -15,86 +15,73 @@ char help_text[]={
"%s image_file_path\n"
};
static void
get_filename(char *filepath, char *out_name)
/* 根据文件路径,获取文件名 */
{
int m,n = 0;
char *p;
for(m=0; m<strlen(filepath); ++m) {
if(filepath[m]=='/')
n = m+1;
}
p = filepath + n;
strcpy(out_name, p);
}
static int
graph_conv( LCUI_Graph *buff, char *out_filename )
graph_conv( LCUI_Graph *buff, char *out_filepath )
{
int i;
FILE *fp;
char file[1024], name[1024];
char file[1024];
get_filename( out_filename, name );
sprintf( file, "%s.c", name );
sprintf( file, "%s.c", out_filepath );
printf("output file: %s\n", file);
fp = fopen( file, "w+" );
if( !fp ) {
return -1;
}
fprintf( fp,
"/* this code generate from file %s */\n"
"int load_my_img ( LCUI_Graph *buff )\n"
"{\n"
" int ret;\n",
name
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"
);
/* red channel */
fprintf( fp, " unsigned char red[] = {" );
fprintf( fp, "static unsigned char graph_red[] = {" );
for( i=0; i<buff->width*buff->height; ++i ) {
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
fprintf( fp, "\n " );
fprintf( fp, "\n\t" );
}
fprintf( fp, "0x%02x,", buff->rgba[0][i] );
}
fprintf( fp, "\n };\n" );
fprintf( fp, "\n};\n\n" );
/* green channel */
fprintf( fp, " unsigned char green[] = {" );
fprintf( fp, "static unsigned char graph_green[] = {" );
for( i=0; i<buff->width*buff->height; ++i ) {
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
fprintf( fp, "\n " );
fprintf( fp, "\n\t" );
}
fprintf( fp, "0x%02x,", buff->rgba[1][i] );
}
fprintf( fp, "\n };\n" );
fprintf( fp, "\n};\n\n" );
/* blue channel */
fprintf( fp, " unsigned char blue[] = {" );
fprintf( fp, "static unsigned char graph_blue[] = {" );
for( i=0; i<buff->width*buff->height; ++i ) {
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
fprintf( fp, "\n " );
fprintf( fp, "\n\t" );
}
fprintf( fp, "0x%02x,", buff->rgba[2][i] );
}
fprintf( fp, "\n };\n" );
fprintf( fp, "\n};\n\n" );
/* alpha channel */
if( buff->have_alpha ) {
fprintf( fp, " unsigned char alpha[] = {" );
fprintf( fp, "static unsigned char graph_alpha[] = {" );
for( i=0; i<buff->width*buff->height; ++i ) {
if( i%MAX_ROW_PIXEL_NUM == 0 ) {
fprintf( fp, "\n " );
fprintf( fp, "\n\t" );
}
fprintf( fp, "0x%02x,", buff->rgba[3][i] );
}
fprintf( fp, "\n };\n" );
fprintf( fp, "\n};\n\n" );
}
fprintf( fp,
" if( Graph_Valid( buff ) ) {\n"
"int InCoreIMG_LoadMyIIMG ( LCUI_Graph *buff )\n"
"{\n"
" int ret;\n"
" if( Graph_IsValid( buff ) ) {\n"
" Graph_Free( buff );\n"
" }\n"
" Graph_Init( buff );\n"
@@ -105,16 +92,22 @@ graph_conv( LCUI_Graph *buff, char *out_filename )
" if( ret == 0 ) {\n"
" memcpy( buff->rgba[0], red, sizeof(red) );\n"
" memcpy( buff->rgba[1], green, sizeof(green) );\n"
" memcpy( buff->rgba[2], blue, sizeof(blue) );\n"
" if( buff->have_alpha ) {\n"
" memcpy( buff->rgba[3], alpha, sizeof(alpha) );\n"
" }\n"
" }\n"
" return ret;\n"
"}\n",
" memcpy( buff->rgba[2], blue, sizeof(blue) );\n",
buff->have_alpha?"TRUE":"FALSE",
buff->type, buff->width, buff->height
);
if( buff->have_alpha ) {
fprintf( fp,
" if( buff->have_alpha ) {\n"
" memcpy( buff->rgba[3], alpha, sizeof(alpha) );\n"
" }\n"
);
}
fprintf( fp,
" }\n"
" return ret;\n"
"}\n"
);
fclose( fp );
return 0;
}