Quantizer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include "stdafx.h"
  2. #include "Quantizer.h"
  3. #include <MATH.H>
  4. /////////////////////////////////////////////////////////////////////////////
  5. CQuantizer::CQuantizer (UINT nMaxColors, UINT nColorBits)
  6. {
  7. m_nColorBits = nColorBits < 8 ? nColorBits : 8;
  8. m_pTree = NULL;
  9. m_nLeafCount = 0;
  10. for (int i=0; i<=(int) m_nColorBits; i++)
  11. m_pReducibleNodes[i] = NULL;
  12. m_nMaxColors = nMaxColors;
  13. }
  14. /////////////////////////////////////////////////////////////////////////////
  15. CQuantizer::~CQuantizer ()
  16. {
  17. if (m_pTree != NULL)
  18. DeleteTree (&m_pTree);
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. BOOL CQuantizer::ProcessImage (HANDLE hImage)
  22. {
  23. BYTE r, g, b;
  24. int i, j;
  25. BITMAPINFOHEADER ds;
  26. memcpy(&ds,hImage, sizeof(ds));
  27. int effwdt = ((((ds.biBitCount * ds.biWidth ) + 31) / 32) * 4);
  28. int nPad = effwdt - (((ds.biWidth * ds.biBitCount) + 7) / 8);
  29. BYTE* pbBits = (BYTE*)hImage + *(LPDWORD)hImage;
  30. switch (ds.biBitCount) {
  31. case 1: // 1-bit DIB
  32. case 4: // 4-bit DIB
  33. case 8: // 8-bit DIB
  34. for (i=0; i<ds.biHeight; i++) {
  35. for (j=0; j<ds.biWidth; j++) {
  36. BYTE idx=GetPixelIndex(j,i,ds.biBitCount,effwdt,pbBits);
  37. BYTE* pal = (BYTE*)(hImage) + sizeof(BITMAPINFOHEADER);
  38. long ldx = idx*sizeof(RGBQUAD);
  39. b = pal[ldx++];
  40. g = pal[ldx++];
  41. r = pal[ldx];
  42. AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
  43. m_pReducibleNodes);
  44. while (m_nLeafCount > m_nMaxColors)
  45. ReduceTree (m_nColorBits, &m_nLeafCount,
  46. m_pReducibleNodes);
  47. }
  48. }
  49. break;
  50. case 15:
  51. case 16://16bit
  52. for (i=0; i<ds.biHeight; i++)
  53. {
  54. for (j=0; j<ds.biWidth; j++)
  55. {
  56. b = (*pbBits)&0x1F;
  57. g = (*pbBits++)>>5;
  58. g |= ((*pbBits)&0x03)<<3;
  59. r = ((*pbBits++)>>2)&0x1F;
  60. r *= 8;
  61. b *= 8;
  62. g *= 8;
  63. AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
  64. m_pReducibleNodes);
  65. while (m_nLeafCount > m_nMaxColors)
  66. ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
  67. }
  68. pbBits += nPad;
  69. }
  70. break;
  71. case 24: // 24-bit DIB
  72. for (i=0; i<ds.biHeight; i++) {
  73. for (j=0; j<ds.biWidth; j++) {
  74. b = *pbBits++;
  75. g = *pbBits++;
  76. r = *pbBits++;
  77. AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
  78. m_pReducibleNodes);
  79. while (m_nLeafCount > m_nMaxColors)
  80. ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
  81. }
  82. pbBits += nPad;
  83. }
  84. break;
  85. case 32: // 32-bit DIB
  86. for (i=0; i<ds.biHeight; i++) {
  87. for (j=0; j<ds.biWidth; j++) {
  88. b = *pbBits++;
  89. g = *pbBits++;
  90. r = *pbBits++;
  91. pbBits++;
  92. AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
  93. m_pReducibleNodes);
  94. while (m_nLeafCount > m_nMaxColors)
  95. ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
  96. }
  97. pbBits += nPad;
  98. }
  99. break;
  100. default: // Unrecognized color format
  101. return FALSE;
  102. }
  103. return TRUE;
  104. }
  105. /////////////////////////////////////////////////////////////////////////////
  106. void CQuantizer::AddColor (NODE** ppNode, BYTE r, BYTE g, BYTE b,
  107. UINT nColorBits, UINT nLevel, UINT* pLeafCount, NODE** pReducibleNodes)
  108. {
  109. static BYTE mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  110. // If the node doesn't exist, create it.
  111. if (*ppNode == NULL)
  112. *ppNode = (NODE*)CreateNode (nLevel, nColorBits, pLeafCount, pReducibleNodes);
  113. // Update color information if it's a leaf node.
  114. if ((*ppNode)->bIsLeaf)
  115. {
  116. (*ppNode)->nPixelCount++;
  117. (*ppNode)->nRedSum += r;
  118. (*ppNode)->nGreenSum += g;
  119. (*ppNode)->nBlueSum += b;
  120. } else
  121. { // Recurse a level deeper if the node is not a leaf.
  122. int shift = 7 - nLevel;
  123. int nIndex =(((r & mask[nLevel]) >> shift) << 2) |
  124. (((g & mask[nLevel]) >> shift) << 1) |
  125. (( b & mask[nLevel]) >> shift);
  126. AddColor(&((*ppNode)->pChild[nIndex]), r, g, b, nColorBits,
  127. nLevel + 1, pLeafCount, pReducibleNodes);
  128. }
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. void* CQuantizer::CreateNode (UINT nLevel, UINT nColorBits, UINT* pLeafCount,
  132. NODE** pReducibleNodes)
  133. {
  134. NODE* pNode = (NODE*)calloc(1,sizeof(NODE));
  135. if (pNode== NULL) return NULL;
  136. pNode->bIsLeaf = (nLevel == nColorBits) ? TRUE : FALSE;
  137. if (pNode->bIsLeaf) (*pLeafCount)++;
  138. else {
  139. pNode->pNext = pReducibleNodes[nLevel];
  140. pReducibleNodes[nLevel] = pNode;
  141. }
  142. return pNode;
  143. }
  144. /////////////////////////////////////////////////////////////////////////////
  145. void CQuantizer::ReduceTree (UINT nColorBits, UINT* pLeafCount,
  146. NODE** pReducibleNodes)
  147. {
  148. int i;
  149. // Find the deepest level containing at least one reducible node.
  150. for (i=nColorBits - 1; (i>0) && (pReducibleNodes[i] == NULL); i--);
  151. // Reduce the node most recently added to the list at level i.
  152. NODE* pNode = pReducibleNodes[i];
  153. pReducibleNodes[i] = pNode->pNext;
  154. NODE* pNodeTmp = pNode;
  155. NODE* pNeedReduceNode = NULL,*pPreNode = NULL,*pPreNodeTmp = NULL;
  156. long nMinPixelCount = 0;
  157. while(pNodeTmp)
  158. {
  159. long nPixelCount = 0;
  160. for (int m=0; m<8; m++)
  161. {
  162. if (pNodeTmp->pChild[m] != NULL)
  163. {
  164. nPixelCount += pNodeTmp->pChild[m]->nPixelCount;
  165. }
  166. }
  167. if(nMinPixelCount == 0)
  168. {
  169. nMinPixelCount = nPixelCount;
  170. pNeedReduceNode = pNodeTmp;
  171. }
  172. else if(nPixelCount < nMinPixelCount)
  173. {
  174. nMinPixelCount = nPixelCount;
  175. pNeedReduceNode = pNodeTmp;
  176. pPreNode = pPreNodeTmp;
  177. }
  178. pPreNodeTmp = pNodeTmp;
  179. pNodeTmp = pNodeTmp->pNext;
  180. }
  181. if(pPreNode)
  182. {
  183. pPreNode->pNext = pNeedReduceNode->pNext;
  184. pReducibleNodes[i] = pNode;
  185. }
  186. pNode = pNeedReduceNode;
  187. UINT nRedSum = 0;
  188. UINT nGreenSum = 0;
  189. UINT nBlueSum = 0;
  190. UINT nChildren = 0;
  191. for (i=0; i<8; i++)
  192. {
  193. if (pNode->pChild[i] != NULL)
  194. {
  195. nRedSum += pNode->pChild[i]->nRedSum;
  196. nGreenSum += pNode->pChild[i]->nGreenSum;
  197. nBlueSum += pNode->pChild[i]->nBlueSum;
  198. pNode->nPixelCount += pNode->pChild[i]->nPixelCount;
  199. free(pNode->pChild[i]);
  200. pNode->pChild[i] = NULL;
  201. nChildren++;
  202. }
  203. }
  204. pNode->bIsLeaf = TRUE;
  205. pNode->nRedSum = nRedSum;
  206. pNode->nGreenSum = nGreenSum;
  207. pNode->nBlueSum = nBlueSum;
  208. *pLeafCount -= (nChildren - 1);
  209. }
  210. /////////////////////////////////////////////////////////////////////////////
  211. void CQuantizer::DeleteTree (NODE** ppNode)
  212. {
  213. for (int i=0; i<8; i++) {
  214. if ((*ppNode)->pChild[i] != NULL) DeleteTree (&((*ppNode)->pChild[i]));
  215. }
  216. free(*ppNode);
  217. *ppNode = NULL;
  218. }
  219. BOOL CQuantizer::FindColorIndex(NODE *pNode,BYTE r,BYTE g,BYTE b,int nLevel,UINT *pColorIndex)
  220. {
  221. static BYTE mask[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  222. // If the node doesn't exist, create it.
  223. if (pNode == NULL)
  224. {
  225. return FALSE;
  226. }
  227. // Update color information if it's a leaf node.
  228. if (pNode->bIsLeaf)
  229. {
  230. *pColorIndex = pNode->nColorIndex;
  231. return TRUE;
  232. }
  233. // Recurse a level deeper if the node is not a leaf.
  234. int shift = 7 - nLevel;
  235. int nIndex =(((r & mask[nLevel]) >> shift) << 2) |
  236. (((g & mask[nLevel]) >> shift) << 1) |
  237. (( b & mask[nLevel]) >> shift);
  238. return FindColorIndex(pNode->pChild[nIndex], r, g, b, nLevel+1, pColorIndex);
  239. }
  240. BOOL CQuantizer::GetColorIndex(BYTE r,BYTE g,BYTE b,UINT *pColorIndex)
  241. {
  242. return FindColorIndex(m_pTree,r,g,b,0,pColorIndex);
  243. }
  244. /////////////////////////////////////////////////////////////////////////////
  245. void CQuantizer::GetPaletteColors (NODE* pTree, RGBQUAD* prgb, UINT* pIndex)
  246. {
  247. if (pTree){
  248. if (pTree->bIsLeaf) {
  249. prgb[*pIndex].rgbRed = (BYTE)((pTree->nRedSum)/(pTree->nPixelCount));
  250. prgb[*pIndex].rgbGreen = (BYTE)((pTree->nGreenSum)/(pTree->nPixelCount));
  251. prgb[*pIndex].rgbBlue = (BYTE)((pTree->nBlueSum)/(pTree->nPixelCount));
  252. prgb[*pIndex].rgbReserved = 0;
  253. pTree->nColorIndex = *pIndex;
  254. (*pIndex)++;
  255. } else {
  256. for (int i=0; i<8; i++) {
  257. if (pTree->pChild[i] != NULL)
  258. GetPaletteColors (pTree->pChild[i], prgb, pIndex);
  259. }
  260. }
  261. }
  262. }
  263. /////////////////////////////////////////////////////////////////////////////
  264. UINT CQuantizer::GetColorCount ()
  265. {
  266. return m_nLeafCount;
  267. }
  268. /////////////////////////////////////////////////////////////////////////////
  269. void CQuantizer::SetColorTable (RGBQUAD* prgb)
  270. {
  271. UINT nIndex = 0;
  272. GetPaletteColors (m_pTree, prgb, &nIndex);
  273. }
  274. /////////////////////////////////////////////////////////////////////////////
  275. BYTE CQuantizer::GetPixelIndex(long x, long y, int nbit, long effwdt, BYTE *pimage)
  276. {
  277. if (nbit==8)
  278. {
  279. return pimage[y*effwdt + x];
  280. } else
  281. {
  282. BYTE pos;
  283. BYTE iDst= pimage[y*effwdt + (x*nbit >> 3)];
  284. if (nbit==4)
  285. {
  286. pos = 4*(1-x%2);
  287. iDst &= (0x0F<<pos);
  288. return iDst >> pos;
  289. } else if (nbit==1)
  290. {
  291. pos = 7-x%8;
  292. iDst &= (0x01<<pos);
  293. return iDst >> pos;
  294. }
  295. }
  296. return 0;
  297. }
  298. CRGBQuantizer::CRGBQuantizer(UINT nMaxColors, UINT nColorBits)
  299. :CQuantizer(nMaxColors,nColorBits)
  300. {
  301. }
  302. BOOL CRGBQuantizer::ProcessImageRGB(BYTE *pRGBData,UINT nWidth,UINT nHeight)
  303. {
  304. BYTE r,g,b;
  305. for (int i=0; i<nHeight; i++)
  306. {
  307. for (int j=0; j<nWidth; j++)
  308. {
  309. r = *pRGBData++;
  310. g = *pRGBData++;
  311. b = *pRGBData++;
  312. AddColor (&m_pTree, r, g, b, m_nColorBits, 0, &m_nLeafCount,
  313. m_pReducibleNodes);
  314. while (m_nLeafCount > m_nMaxColors)
  315. ReduceTree (m_nColorBits, &m_nLeafCount, m_pReducibleNodes);
  316. }
  317. }
  318. return TRUE;
  319. }
  320. CBitmapQuantizer::CBitmapQuantizer(UINT nMaxColors, UINT nColorBits)
  321. :CQuantizer(nMaxColors,nColorBits)
  322. {
  323. }
  324. BOOL CBitmapQuantizer::ProcessImageBitmap (HBITMAP hBmp)
  325. {
  326. BITMAP bm;
  327. PBITMAPINFO bmpInf;
  328. if(GetObject(hBmp,sizeof(bm),&bm)==0)
  329. return FALSE;
  330. int nPaletteSize=0;
  331. if(bm.bmBitsPixel<16)
  332. nPaletteSize=(int)pow(2.0,bm.bmBitsPixel);
  333. bmpInf=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
  334. sizeof(RGBQUAD)*nPaletteSize+
  335. (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel);
  336. BYTE* buf = ((BYTE*)bmpInf)+
  337. sizeof(BITMAPINFOHEADER)+
  338. sizeof(RGBQUAD)*nPaletteSize;
  339. //-----------------------------------------------
  340. bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  341. bmpInf->bmiHeader.biWidth = bm.bmWidth;
  342. bmpInf->bmiHeader.biHeight = bm.bmHeight;
  343. bmpInf->bmiHeader.biPlanes = bm.bmPlanes;
  344. bmpInf->bmiHeader.biBitCount = bm.bmBitsPixel;
  345. bmpInf->bmiHeader.biCompression = BI_RGB;
  346. bmpInf->bmiHeader.biSizeImage = (bm.bmWidth+7)/8*bm.bmHeight*bm.bmBitsPixel;
  347. bmpInf->bmiHeader.biXPelsPerMeter = 0;
  348. bmpInf->bmiHeader.biYPelsPerMeter = 0;
  349. bmpInf->bmiHeader.biClrUsed = 0;
  350. bmpInf->bmiHeader.biClrImportant = 0;
  351. //-----------------------------------------------
  352. HDC hDC = ::GetWindowDC(NULL);
  353. if(!::GetDIBits(hDC,hBmp,0,(UINT)bm.bmHeight,buf,bmpInf,DIB_RGB_COLORS))
  354. {
  355. ::ReleaseDC(NULL,hDC);
  356. LocalFree(bmpInf);
  357. return FALSE;
  358. }
  359. ::ReleaseDC(NULL,hDC);
  360. bmpInf->bmiHeader.biSize = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*nPaletteSize;
  361. BOOL bRet = ProcessImage(bmpInf);
  362. LocalFree(bmpInf);
  363. return bRet;
  364. }