非零解
游戏性注入者
级别: 火花会员
编号: 95772
精华: 0
发帖: 940
威望: 0 点
配偶: 单身
火 花 币: 18424 HHB
注册时间:2008-12-29
最后登陆:2024-08-21
|
女神啊出现吧markII---闲的蛋疼编的一个压缩颜色的程序。
因为没地放程序,所以直接贴代码。会c编程的自己编译一下就行。原理很简单,用调色板里最相近的颜色代替原图的颜色。 输入文件两个,input.bmp,原图,24位色和32位色都行,貌似索引色也可以。 color.txt,需要自己写的调色板文件,调色板用gba的格式,也就是16个5位色,每个颜色是4位16进制数,所以从vba上尽管抄好了。每个颜色之间用空格隔开。 输出文件是用了那个调色板的16色索引位图。 调色板选得好的话,效果还是不错的。 #include<stdio.h> #include<malloc.h> #include<math.h> struct picture { int x; int y; int bit; int headlong; unsigned char *head; unsigned char *data;
}pic; void main() { void read(FILE *in); void write(FILE *out,unsigned char *box,unsigned char *index); FILE *in,*out; unsigned char *p,*q,*index,*boxindex; unsigned char box[16][4]; int i,color,j,min1,min2,size,patch1,patch2,k; float fangcha,t; in=fopen("color.txt","r"); boxindex=(unsigned char*)malloc(64); q=boxindex; for(i=0;i<16;i++) { fscanf(in,"%x",&color); box[0]=8*((color&64512)/1024);*q++=box[0]; box[1]=8*((color&992)/32);*q++=box[1]; box[2]=8*(color&31);*q++=box[2]; box[3]=0;*q++=box[3]; } fclose(in); in=fopen("input.bmp","r+b"); out=fopen("output.bmp","wb"); read(in); p=pic.data; patch1=(4-pic.x%4)%4; patch2=(4-((pic.x+1)/2)%4)%4; size=((pic.x+1)/2+patch2)*pic.y; index=(unsigned char*)malloc(size); q=index; for(j=0;j<pic.y;j++) { for(k=0;k<pic.x/2;k++) { fangcha=195075;min1=0; for(i=0;i<16;i++) { t=(box[0]-*p)*(box[0]-*p)+(box[1]-*(p+1))*(box[1]-*(p+1))+(box[2]-*(p+2))*(box[2]-*(p+2)); if(t<fangcha) { fangcha=t; min1=i; } } p=p+pic.bit; fangcha=195075;min2=0; for(i=0;i<16;i++) { t=(box[0]-*p)*(box[0]-*p)+(box[1]-*(p+1))*(box[1]-*(p+1))+(box[2]-*(p+2))*(box[2]-*(p+2)); if(t<fangcha) { fangcha=t; min2=i; } } *q++=min1*16+min2; p=p+pic.bit; } if(pic.x%2==1) { fangcha=195075;min1=0; for(i=0;i<16;i++) { t=(box[0]-*p)*(box[0]-*p)+(box[1]-*(p+1))*(box[1]-*(p+1))+(box[2]-*(p+2))*(box[2]-*(p+2)); if(t<fangcha) { fangcha=t; min1=i; } } *q++=min1*16; p=p+pic.bit; } for(k=0;k<patch2;k++) *q++=0; } write(out,boxindex,index); //free(index); //free(boxindex); fclose(in); fclose(out); return; } void read(FILE *in) { int *x,*y; int size,patch1; unsigned short *type; unsigned* away; fseek(in,10,SEEK_SET); away=(unsigned* )malloc(4); x=(int *)malloc(4); y=(int *)malloc(4); type=(unsigned short *)malloc(2); fread(away,4,1,in); fseek(in,18,SEEK_SET); fread(x,4,1,in); fseek(in,22,SEEK_SET); fread(y,4,1,in); fseek(in,28,SEEK_SET); fread(type,2,1,in); pic.x=*x; pic.y=*y; pic.bit=*type/8; pic.headlong=*away; patch1=(4-*x%4)%4; *x=*x+patch1; *x=pic.bit*(*x); size=*x * (*y); pic.data=(unsigned char*)malloc(size); pic.head=(unsigned char*)malloc(*away); if(pic.data==NULL||pic.head==NULL) { printf("???????????"); return; } fseek(in,0,SEEK_SET); fread(pic.head,1,*away,in); fseek(in,*away,SEEK_SET); fread(pic.data,1,size,in); free(x); free(y); free(away); free(type); } void write(FILE *out,unsigned char *box,unsigned char *index) {
int i; int datalong,size,patch2; unsigned char *all,*p; patch2=(4-((pic.x+1)/2)%4)%4; datalong=((pic.x+1)/2+patch2)*pic.y; size=54+64+datalong; p=pic.head; p=p+2; *p=size%256; size=size/256; p++; *p=size%256; size=size/256; p++; *p=size%256; size=size/256; p++; *p=size%256; size=size/256; p++; p=p+4; *p=54+64; p=pic.head+28; *p=4; p=pic.head+34; *p=datalong%256; p++; *p=(datalong/256)%256; p++; *p=(datalong/256/256)%256; p++; *p=(datalong/256/256/256)%256; fwrite(pic.head,1,pic.headlong,out); fwrite(box,1,64,out); fwrite(index,1,datalong,out); }
|
|
[楼 主]
|
Posted:2011-04-02 21:10| |
顶端
| |