資源簡介
網上大部分的轉換代碼都沒有考慮對齊問題,好不容易找到這個,沒有問題。
但是在批量處理圖片時,需要修改兩個地方
int usedTimes[4096] = {0};//12b
int miniColor[4096];
要改new出來,并把usedTimes初始化
在Transfer函數的最后要delete []usedTimes和delete []miniColor,
不然的話,批量處理堆棧會溢出。

代碼片段和文件信息
//原文出處:
//http://members.easyshag.com/femaledesperation/cg01.html
//http://members.easyshag.com/femaledesperation/gillian22.html#top
//問題:沒考慮對齊!
/*?原理:
The?usual?approach?is?to?create?an?array?representing?a?histogram?of?colors?
and?color?frequencies?sort?the?array?in?order?of?descending?frequencies
and?copy?the?first?236?colors?in?the?array?to?the?palette.
To?keep?the?array?size?manageable?the?least?significant?3?or?4?bits?of?each?8-bit?color
component?are?normally?discarded.?A?popularity?palette?generally?produces?better?output?
than?a?halftone?palette?but?infrequently?appearing?colors?that?are?nonetheless?important
to?the?eye?may?be?omitted.*/?
/***************
bmpTest.cpp
****************/
#include?“Test.h“
//?計算平P方F差C的函數
int?PFC(int?color1?int?color2)
{
int?xyz;
x?=?(color1?&?0xf)?-?(color2?&?0xf);
y?=?((color1>>4)?&?0xf)?-?((color2>>4)?&?0xf);
z?=?((color1>>8)?&?0xf)?-?((color2>>8)?&?0xf);
return?(x*x?+?y*y?+?z*z);
};
//?直接插入排序
int?Sort1(int?*src?int?*attach?int?n)
{
int?cur?cur1;
int?ijk=0;
for?(i?=?1;?i? {
cur=?src[i];
cur1?=?attach[i];
for?(j?=?i?-?1;?j?>=?0;?j--)
{
if?(cur?>?src[j])
{
src[j+1]=?src[j];
attach[j+1]?=?attach[j];
}
else
{
break;
}
}
src[j+1]=?cur;
attach[j+1]?=?cur1;
}
return?0;
}
//?快速排序
int?Sort2(int?*src?int?*attach?int?n)
{
if?(n?<=?12)?return?Sort1(src?attach?n);
int?low?=?1?high?=?n?-?1;
int?tmp;
while?(low?<=?high)
{
while?(src[low]?>=?src[0])
{
if?(++low?>?(n?-?1))?break;
}
while?(src[high]? {
if?(--high?1)?break;
}
if?(low?>?high)?break;
//
tmp=?src[low];
src[low]=?src[high];
src[high]=?tmp;
tmp=?attach[low];
attach[low]=?attach[high];
attach[high]=?tmp;
low++;
high--;
}
//
tmp=?src[low?-?1];
src[low?-?1]=?src[0];
src[0]=?tmp;
tmp=?attach[low?-?1];
attach[low?-?1]=?attach[0];
attach[0]=?tmp;
//
if?(low?>?1)?Sort2(src?attach?low?-?1);
if?(low?//
return?0;
}
//?將?24?bit?的象素顏色數據轉換為?256?色圖的圖像數據(即索引值)
int?Transfer(WORD?*shortColor?int?BytesPerRow?int?bmWidth?int?bmHeight?BYTE?*out8Dib?RGBQUAD?*mainColor)
{//?BytesPerRow?may?not?equel?bmWidth?!
int?usedTimes[4096]?=?{0};//12b
int?miniColor[4096];
int?ij;
for?(i?=?0;?i?4096;?i++)?miniColor[i]?=?i;
//
for?(i?=?0;?i? {
usedTimes[shortColor[i]]?++;//?get?frequency
}
int?numberOfColors?=?0;
for?(i?=?0;?i?4096;?i++)
{
if?(usedTimes[i]?>?0)?numberOfColors++;
}
//?對usedTimes進行排序,排序過程中minColor數組(保存了顏色值)也作與useTimes
//?數組相似的交換
Sort2(usedTimes?miniColor?4096);
//?usedTimes數組中是各顏色使用頻率,從高到低排列,顯然第numberOfColor個之后的都為0
//?miniColor數組中是相應的顏色數據
//?將前256個顏色數據保存到256色位圖的調色盤中
for?(i?=?0;?i?256;?i++)
{
mainColor[i].rgbBlue=?(BYTE)((miniColor[i]>>8)<<4);
mainColor[i].rgbGreen=?(BYTE)(((miniColor[i]>>4)?&?0xf)<<4);
mai
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????8086??2016-11-23?12:26??真彩24位轉256色\BMP_ReadWrite.cpp
?????文件???????3548??2011-12-07?14:44??真彩24位轉256色\BMP_ReadWrite.dsp
?????文件????????551??2011-12-05?11:52??真彩24位轉256色\BMP_ReadWrite.dsw
?????文件??????58368??2016-11-23?14:30??真彩24位轉256色\BMP_ReadWrite.ncb
?????文件?????136704??2016-11-23?14:30??真彩24位轉256色\BMP_ReadWrite.opt
?????文件???????1412??2016-11-23?12:26??真彩24位轉256色\BMP_ReadWrite.plg
?????文件????????240??2016-11-23?14:30??真彩24位轉256色\BMP_ReadWrite.positions
?????文件????1934336??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.bsc
?????文件?????176239??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.exe
?????文件?????207732??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.ilk
?????文件??????25533??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.obj
????I.A....???4494808??2016-11-23?12:02??真彩24位轉256色\Debug\BMP_ReadWrite.pch
?????文件?????500736??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.pdb
?????文件??????????0??2016-11-23?12:26??真彩24位轉256色\Debug\BMP_ReadWrite.sbr
?????文件?????132096??2016-11-23?12:26??真彩24位轉256色\Debug\vc60.idb
?????文件??????77824??2016-11-23?12:26??真彩24位轉256色\Debug\vc60.pdb
?????文件???????3630??2011-12-07?13:40??真彩24位轉256色\test.h
?????文件???????1582??2016-11-23?12:26??真彩24位轉256色\testout.bmp
?????文件??????10341??2011-12-14?11:18??真彩24位轉256色\讀寫BMP示例.txt
?????目錄??????????0??2016-11-23?12:26??真彩24位轉256色\Debug
?????目錄??????????0??2016-11-23?14:30??真彩24位轉256色
-----------?---------??----------?-----??----
??????????????7773766????????????????????21
評論
共有 條評論