📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(this->OpenPictureDialog1->Execute())
{
this->Image1->Picture->LoadFromFile(
this->OpenPictureDialog1->FileName);
m_bmp->LoadFromFile(
this->OpenPictureDialog1->FileName);
if(m_bmp->PixelFormat != pf8bit)
{
ShowMessage("提示:只能转换256色BMP");
}
if(m_bmp->Width < 32)
{
ShowMessage("提示:图片宽度太小");
}
if(m_bmp->Height < 24)
{
ShowMessage("提示:图片高度太小");
}
if(m_bmp->Width > 320)
{
ShowMessage("提示:图片宽度太大");
}
if(m_bmp->Height > 240)
{
ShowMessage("提示:图片高度太大");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
m_bmp = new Graphics::TBitmap;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete m_bmp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(m_bmp->PixelFormat != pf8bit)
{
ShowMessage("提示:只能转换256色");
return;
}
if(m_bmp->Width < 32)
{
ShowMessage("提示:图片宽度太小");
return;
}
if(m_bmp->Height < 24)
{
ShowMessage("提示:图片高度太小");
return;
}
if(m_bmp->Width > 320)
{
ShowMessage("提示:图片宽度太大");
return;
}
if(m_bmp->Height > 240)
{
ShowMessage("提示:图片高度太大");
return;
}
memset(&(m_color_c[0][0]),0,sizeof(m_color_c));
memset(&(m_pixel_a[0][0]),0,sizeof(m_pixel_a));
int y;
int x;
for(y = 0; y < m_bmp->Height; y ++)
{
memcpy(m_pixel_a[y],
m_bmp->ScanLine[y],m_bmp->Width);
for(x = 0; x < m_bmp->Width; x ++)
{
m_color_c[m_pixel_a[y][x]][0] ++;
}
}
tagPALETTEENTRY t_pes[256];
int j = GetPaletteEntries(
m_bmp->Palette,0,256,t_pes);
int i;
for(i = 0; i < j; i ++)
{
m_color_c[i][1] = t_pes[i].peRed & 0x00ff;
m_color_c[i][2] = t_pes[i].peGreen & 0x00ff;
m_color_c[i][3] = t_pes[i].peBlue & 0x00ff;
}
int c = 0;
for(i = 0; i < j; i ++)
{
if(m_color_c[i][0] != 0)
{
m_color_c[i][4] = c ++;
}
}
ShowMessage("提示:色彩实际使用" + IntToStr(c) + "个");
if(this->SaveDialog1->Execute())
{
AnsiString astr = this->SaveDialog1->FileName;
FILE * fp;
if((fp = fopen(astr.c_str(),"w+")) != NULL)
{
//image ?;
fprintf(fp,"int color_count = %d;\n",c);
fprintf(fp,"int image_width = %d;\n",m_bmp->Width );
fprintf(fp,"int image_height= %d;\n",m_bmp->Height);
//color r;
fprintf(fp,"\nunsigned char linux_logo_red[] __initdata =\n{\t");
for(i = 0,c = 0; i < j; i ++)
{
if(m_color_c[i][0] != 0)
{
fprintf(fp,"0x%02x,",m_color_c[i][1]);
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
c ++;
}
}
for(;c < 256; c ++)
{
fprintf(fp,"0x00,");
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
}
fprintf(fp,"\n};\n");
//color g;
fprintf(fp,"\nunsigned char linux_logo_green[] __initdata =\n{\t");
for(i = 0,c = 0; i < j; i ++)
{
if(m_color_c[i][0] != 0)
{
fprintf(fp,"0x%02x,",m_color_c[i][2]);
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
c ++;
}
}
for(;c < 256; c ++)
{
fprintf(fp,"0x00,");
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
}
fprintf(fp,"\n};\n");
//color b;
fprintf(fp,"\nunsigned char linux_logo_blue[] __initdata =\n{\t");
for(i = 0,c = 0; i < j; i ++)
{
if(m_color_c[i][0] != 0)
{
fprintf(fp,"0x%02x,",m_color_c[i][3]);
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
c ++;
}
}
for(;c < 256; c ++)
{
fprintf(fp,"0x00,");
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
}
fprintf(fp,"\n};\n\n");
//image p;
fprintf(fp,"unsigned char linux_logo[] __initdata =\n{\t");
c = 0;
for(y = 0; y < m_bmp->Height; y ++)
for(x = 0; x < m_bmp->Width ; x ++)
{
fprintf(fp,"0x%02x,",m_color_c[m_pixel_a[y][x]][4] + 0x20);
if((c & 7) == 7)
{
fprintf(fp,"\n\t");
}
c ++;
}
fprintf(fp,"\n};\n\n");
fclose(fp);
}
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -