Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1084 lines
28 KiB

  1. /****************************************************************************
  2. *
  3. * dibmap.c
  4. *
  5. * Histrogram and optimal palette processing module.
  6. *
  7. * Microsoft Video for Windows Sample Capture Class
  8. *
  9. * Copyright (c) 1992, 1993 Microsoft Corporation. All Rights Reserved.
  10. *
  11. * You have a royalty-free right to use, modify, reproduce and
  12. * distribute the Sample Files (and/or any modified version) in
  13. * any way you find useful, provided that you agree that
  14. * Microsoft has no warranty obligations or liability for any
  15. * Sample Application Files which are modified.
  16. *
  17. ***************************************************************************/
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include <win32.h>
  21. #include "dibmap.h"
  22. #ifndef WIN32
  23. extern NEAR PASCAL MemCopy(LPVOID,LPVOID,DWORD);
  24. #else
  25. #define HUGE
  26. #endif
  27. extern NEAR PASCAL MemFill(LPVOID,DWORD,BYTE);
  28. void Histogram24(BYTE huge *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram);
  29. void Histogram16(BYTE huge *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram);
  30. void Histogram8(BYTE huge *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors);
  31. void Histogram4(BYTE huge *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors);
  32. void Histogram1(BYTE huge *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors);
  33. void Reduce24(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp16to8);
  34. void Reduce16(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp16to8);
  35. void Reduce8(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp8to8);
  36. void Reduce4(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp8to8);
  37. void Reduce1(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp8to8);
  38. //
  39. // InitHistogram
  40. //
  41. // create a zero'ed histogram table
  42. //
  43. LPHISTOGRAM InitHistogram(LPHISTOGRAM lpHistogram)
  44. {
  45. if (lpHistogram == NULL)
  46. lpHistogram = (LPVOID)GlobalLock(GlobalAlloc(GPTR,32768l*sizeof(DWORD)));
  47. #if 0
  48. if (lpHistogram)
  49. MemFill(lpHistogram, 32768l * sizeof(DWORD), 0);
  50. #endif
  51. return lpHistogram;
  52. }
  53. //
  54. // FreeHistogram
  55. //
  56. // free a histogram table
  57. //
  58. void FreeHistogram(LPHISTOGRAM lpHistogram)
  59. {
  60. GlobalFree(GlobalHandle(lpHistogram));
  61. }
  62. //
  63. // DibHistogram
  64. //
  65. // take all colors in a dib and increment its entry in the Histogram table
  66. //
  67. // supports the following DIB formats: 1,4,8,16,24
  68. //
  69. BOOL DibHistogram(LPBITMAPINFOHEADER lpbi, LPBYTE lpBits, int x, int y, int dx, int dy, LPHISTOGRAM lpHistogram)
  70. {
  71. int i;
  72. WORD WidthBytes;
  73. RGBQUAD FAR * prgbq;
  74. WORD argb16[256];
  75. if (lpbi == NULL || lpHistogram == NULL)
  76. return FALSE;
  77. if (lpbi->biClrUsed == 0 && lpbi->biBitCount <= 8)
  78. lpbi->biClrUsed = (1 << (int)lpbi->biBitCount);
  79. if (lpBits == NULL)
  80. lpBits = (LPBYTE)lpbi + (int)lpbi->biSize + (int)lpbi->biClrUsed*sizeof(RGBQUAD);
  81. WidthBytes = (WORD)((lpbi->biBitCount * lpbi->biWidth + 7) / 8 + 3) & ~3;
  82. ((BYTE huge *)lpBits) += (DWORD)y*WidthBytes + ((x*(int)lpbi->biBitCount)/8);
  83. if (dx < 0 || dx > (int)lpbi->biWidth)
  84. dx = (int)lpbi->biWidth;
  85. if (dy < 0 || dy > (int)lpbi->biHeight)
  86. dy = (int)lpbi->biHeight;
  87. if ((int)lpbi->biBitCount <= 8)
  88. {
  89. prgbq = (LPVOID)((LPBYTE)lpbi + lpbi->biSize);
  90. for (i=0; i<(int)lpbi->biClrUsed; i++)
  91. {
  92. argb16[i] = RGB16(prgbq[i].rgbRed,prgbq[i].rgbGreen,prgbq[i].rgbBlue);
  93. }
  94. for (i=(int)lpbi->biClrUsed; i<256; i++)
  95. {
  96. argb16[i] = 0x0000; // just in case!
  97. }
  98. }
  99. switch ((int)lpbi->biBitCount)
  100. {
  101. case 24:
  102. Histogram24(lpBits, dx, dy, WidthBytes, lpHistogram);
  103. break;
  104. case 16:
  105. Histogram16(lpBits, dx, dy, WidthBytes, lpHistogram);
  106. break;
  107. case 8:
  108. Histogram8(lpBits, dx, dy, WidthBytes, lpHistogram, argb16);
  109. break;
  110. case 4:
  111. Histogram4(lpBits, dx, dy, WidthBytes, lpHistogram, argb16);
  112. break;
  113. case 1:
  114. Histogram1(lpBits, dx, dy, WidthBytes, lpHistogram, argb16);
  115. break;
  116. }
  117. }
  118. //
  119. // will convert the given DIB to a 8bit DIB with the specifed palette
  120. //
  121. HANDLE DibReduce(LPBITMAPINFOHEADER lpbiIn, LPBYTE pbIn, HPALETTE hpal, LPBYTE lp16to8)
  122. {
  123. HANDLE hdib;
  124. short nPalColors;
  125. int nDibColors;
  126. WORD cbOut;
  127. WORD cbIn;
  128. BYTE xlat[256];
  129. BYTE HUGE * pbOut;
  130. RGBQUAD FAR * prgb;
  131. DWORD dwSize;
  132. int i;
  133. int dx;
  134. int dy;
  135. PALETTEENTRY pe;
  136. LPBITMAPINFOHEADER lpbiOut;
  137. dx = (int)lpbiIn->biWidth;
  138. dy = (int)lpbiIn->biHeight;
  139. cbIn = ((lpbiIn->biBitCount*dx+7)/8+3)&~3;
  140. cbOut = (dx+3)&~3;
  141. //
  142. // careful with GetObject in Win32: this (counter-intuitively) writes
  143. // a short not an INT for the number of colours
  144. //
  145. GetObject(hpal, sizeof(short), (LPVOID)&nPalColors);
  146. nDibColors = (int)lpbiIn->biClrUsed;
  147. if (nDibColors == 0 && lpbiIn->biBitCount <= 8)
  148. nDibColors = (1 << (int)lpbiIn->biBitCount);
  149. if (pbIn == NULL)
  150. pbIn = (LPBYTE)lpbiIn + (int)lpbiIn->biSize + nDibColors*sizeof(RGBQUAD);
  151. dwSize = (DWORD)cbOut * dy;
  152. hdib = GlobalAlloc(GMEM_MOVEABLE,sizeof(BITMAPINFOHEADER)
  153. + nPalColors*sizeof(RGBQUAD) + dwSize);
  154. if (!hdib)
  155. return NULL;
  156. lpbiOut = (LPVOID)GlobalLock(hdib);
  157. lpbiOut->biSize = sizeof(BITMAPINFOHEADER);
  158. lpbiOut->biWidth = lpbiIn->biWidth;
  159. lpbiOut->biHeight = lpbiIn->biHeight;
  160. lpbiOut->biPlanes = 1;
  161. lpbiOut->biBitCount = 8;
  162. lpbiOut->biCompression = BI_RGB;
  163. lpbiOut->biSizeImage = dwSize;
  164. lpbiOut->biXPelsPerMeter= 0;
  165. lpbiOut->biYPelsPerMeter= 0;
  166. lpbiOut->biClrUsed = nPalColors;
  167. lpbiOut->biClrImportant = 0;
  168. pbOut = (LPBYTE)lpbiOut + (int)lpbiOut->biSize + nPalColors*sizeof(RGBQUAD);
  169. prgb = (LPVOID)((LPBYTE)lpbiOut + (int)lpbiOut->biSize);
  170. for (i=0; i<nPalColors; i++)
  171. {
  172. GetPaletteEntries(hpal, i, 1, &pe);
  173. prgb[i].rgbRed = pe.peRed;
  174. prgb[i].rgbGreen = pe.peGreen;
  175. prgb[i].rgbBlue = pe.peBlue;
  176. prgb[i].rgbReserved = 0;
  177. }
  178. if ((int)lpbiIn->biBitCount <= 8)
  179. {
  180. prgb = (LPVOID)((LPBYTE)lpbiIn + lpbiIn->biSize);
  181. for (i=0; i<nDibColors; i++)
  182. xlat[i] = lp16to8[RGB16(prgb[i].rgbRed,prgb[i].rgbGreen,prgb[i].rgbBlue)];
  183. for (; i<256; i++)
  184. xlat[i] = 0;
  185. }
  186. switch ((int)lpbiIn->biBitCount)
  187. {
  188. case 24:
  189. Reduce24(pbIn, dx, dy, cbIn, pbOut, cbOut, lp16to8);
  190. break;
  191. case 16:
  192. Reduce16(pbIn, dx, dy, cbIn, pbOut, cbOut, lp16to8);
  193. break;
  194. case 8:
  195. Reduce8(pbIn, dx, dy, cbIn, pbOut, cbOut, xlat);
  196. break;
  197. case 4:
  198. Reduce4(pbIn, dx, dy, cbIn, pbOut, cbOut, xlat);
  199. break;
  200. case 1:
  201. Reduce1(pbIn, dx, dy, cbIn, pbOut, cbOut, xlat);
  202. break;
  203. }
  204. return hdib;
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. // cluster.c
  208. ///////////////////////////////////////////////////////////////////////////////
  209. #define IN_DEPTH 5 // # bits/component kept from input
  210. #define IN_SIZE (1 << IN_DEPTH) // max value of a color component
  211. typedef enum { red, green, blue } color;
  212. typedef struct tagCut {
  213. long lvariance; // for int version
  214. int cutpoint;
  215. unsigned long rem; // for experimental fixed point
  216. color cutaxis;
  217. long w1, w2;
  218. double variance;
  219. } Cut;
  220. typedef struct tagColorBox { // from cluster.c
  221. struct tagColorBox *next; /* pointer to next box */
  222. int rmin, rmax, gmin, gmax, bmin, bmax; /* bounding box */
  223. long variance, wt; /* weighted variance */
  224. long sum[3]; /* sum of values */
  225. } ColorBox;
  226. static int InitBoxes(int nBoxes);
  227. static void DeleteBoxes(void);
  228. static int SplitBoxAxis(ColorBox *box, Cut cutaxis);
  229. static void ShrinkBox(ColorBox *box);
  230. static int ComputePalette(LPHISTOGRAM lpHistogram, LPBYTE lp16to8, LPPALETTEENTRY palette);
  231. static COLORREF DetermineRepresentative(ColorBox *box, int palIndex);
  232. static Cut FindSplitAxis(ColorBox *box);
  233. static void SplitBox(ColorBox *box);
  234. static void SortBoxes(void);
  235. HANDLE hBoxes;
  236. ColorBox *UsedBoxes;
  237. ColorBox *FreeBoxes;
  238. LPBYTE glp16to8;
  239. #ifdef WIN32
  240. /*
  241. * to avoid all this 16 bit assembler with minimal changes to the
  242. * rest of the code the Win32 version will use a global pointer set by
  243. * UseHistogram and accessed by the hist() and IncHistogram macros.
  244. */
  245. DWORD HUGE* glpHistogram;
  246. #define UseHistogram(p) (glpHistogram = (p))
  247. #define hist(r,g,b) ((DWORD HUGE *)glpHistogram)[(WORD)(b) | ((WORD)(g)<<IN_DEPTH) | ((WORD)(r)<<(IN_DEPTH*2))]
  248. #define IncHistogram(w) if (lpHistogram[(WORD)(w)] < 0xFFFFFFFF) { \
  249. lpHistogram[(WORD)(w)]++;\
  250. }
  251. #else
  252. #define hist(r,g,b) GetHistogram((BYTE)(r),(BYTE)(g),(BYTE)(b))
  253. #pragma optimize ("", off)
  254. //
  255. // set FS == lpHistogram.sel, so we can get at it quickly!
  256. //
  257. void NEAR PASCAL UseHistogram(LPHISTOGRAM lpHistogram)
  258. {
  259. _asm {
  260. mov ax,word ptr lpHistogram[2]
  261. _emit 08Eh ; mov fs,ax
  262. _emit 0E0h
  263. }
  264. }
  265. //
  266. // get the DWORD histogram count of a RGB
  267. //
  268. DWORD NEAR _FASTCALL GetHistogram(BYTE r, BYTE g, BYTE b)
  269. {
  270. if (0) // avoid compiler warning NO RETURN VALUE
  271. return 0;
  272. _asm {
  273. ;
  274. ; on entry al=r, dl=g, bl=b [0-31]
  275. ;
  276. ; map to a RGB16
  277. ;
  278. xor ah,ah
  279. shl ax,5
  280. or al,dl
  281. shl ax,5
  282. or al,bl
  283. ; now ax = RGB16
  284. _emit 66h _asm xor bx,bx ; xor ebx,ebx
  285. _asm mov bx,ax ; mov bx,ax
  286. _emit 66h _asm shl bx,2 ; shl ebx,2
  287. _emit 64h _asm _emit 67h ; mov dx,fs:[ebx][2]
  288. _emit 8Bh _asm _emit 53h
  289. _emit 02h
  290. _emit 64h _asm _emit 67h ; mov ax,fs:[ebx][0]
  291. _emit 8Bh _asm _emit 03h
  292. }
  293. }
  294. //
  295. // increment the histogram count of a RGB16
  296. //
  297. //
  298. // #define IncHistogram(w) if (lpHistogram[(WORD)(w)] < 0xFFFFFFFF)
  299. // lpHistogram[(WORD)(w)]++;
  300. //
  301. void NEAR _FASTCALL IncHistogram(WORD rgb16)
  302. {
  303. _asm {
  304. ;
  305. ; on entry ax = rgb16
  306. ;
  307. _emit 66h _asm xor bx,bx ; xor ebx,ebx
  308. _asm mov bx,ax ; mov bx,ax
  309. _emit 66h _asm shl bx,2 ; shl ebx,2
  310. _emit 64h _asm _emit 67h ; cmp dword ptr fs:[ebx], -1
  311. _emit 66h _asm _emit 83h
  312. _emit 3Bh _asm _emit 0FFh
  313. _emit 74h _asm _emit 05h ; je short @f
  314. _emit 64h _asm _emit 67h ; inc dword ptr fs:[ebx]
  315. _emit 66h _asm _emit 0FFh
  316. _emit 03h
  317. }
  318. }
  319. #pragma optimize ("", on)
  320. // !!! C8 generates a Jump into the middle of a 2 byte instruction
  321. //
  322. #pragma optimize ("", off)
  323. #endif //WIN32
  324. //
  325. // HistogramPalette
  326. //
  327. // given a histogram, will reduce it to 'nColors' number of colors.
  328. // returns a optimal palette. if specifed lp16to8 will contain the
  329. // translate table from RGB16 to the palette index.
  330. //
  331. // you can specify lpHistogram as lp16to8
  332. //
  333. HPALETTE HistogramPalette(LPHISTOGRAM lpHistogram, LPBYTE lp16to8, int nColors)
  334. {
  335. WORD w;
  336. DWORD dwMax;
  337. COLORREF rgb;
  338. ColorBox *box;
  339. int i;
  340. // Had to make this global to prevent VB 2.0 stack explosion
  341. static struct {
  342. WORD palVersion;
  343. WORD palNumEntries;
  344. PALETTEENTRY palPalEntry[256];
  345. } pal;
  346. //
  347. // the 'C' code cant handle >64k histogram counts.
  348. // !!!fix this
  349. //
  350. for (dwMax=0,w=0; w<0x8000; w++)
  351. dwMax = max(dwMax,lpHistogram[w]);
  352. while (dwMax > 0xFFFFl)
  353. {
  354. for (w=0; w<0x8000; w++)
  355. lpHistogram[w] /= 2;
  356. dwMax /= 2;
  357. }
  358. if (!InitBoxes(min(nColors, 236)))
  359. return NULL;
  360. UseHistogram(lpHistogram);
  361. glp16to8 = lp16to8;
  362. /* while there are free boxes left, split the largest */
  363. i = 0;
  364. do {
  365. i++;
  366. SplitBox(UsedBoxes);
  367. }
  368. while (FreeBoxes && UsedBoxes->variance);
  369. SortBoxes();
  370. i=0;
  371. //
  372. // add some standard colors to the histogram
  373. //
  374. if (nColors > 236)
  375. {
  376. HDC hdc;
  377. HPALETTE hpal;
  378. hdc = GetDC(NULL);
  379. if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  380. {
  381. GetSystemPaletteEntries(hdc, 0, 10, &pal.palPalEntry[0]);
  382. GetSystemPaletteEntries(hdc, 246, 10, &pal.palPalEntry[246]);
  383. i = 10;
  384. } else { // we're a true colour device, so get the system
  385. // colours from the default palette.
  386. hpal = GetStockObject(DEFAULT_PALETTE);
  387. GetPaletteEntries(hpal, 0, 10, &pal.palPalEntry[0]);
  388. GetPaletteEntries(hpal, 10, 10, &pal.palPalEntry[246]);
  389. i = 10;
  390. }
  391. ReleaseDC(NULL, hdc);
  392. }
  393. /* Generate the representitives and the associated Palette mapping */
  394. /* NOTE: Might loop less than nColors times. */
  395. for (box = UsedBoxes; box; box = box->next, i++)
  396. {
  397. rgb = DetermineRepresentative(box, i);
  398. pal.palPalEntry[i].peRed = GetRValue(rgb);
  399. pal.palPalEntry[i].peGreen = GetGValue(rgb);
  400. pal.palPalEntry[i].peBlue = GetBValue(rgb);
  401. pal.palPalEntry[i].peFlags = 0;
  402. }
  403. DeleteBoxes();
  404. if (nColors > 236)
  405. {
  406. for (; i<246; i++)
  407. {
  408. pal.palPalEntry[i].peRed = 0;
  409. pal.palPalEntry[i].peGreen = 0;
  410. pal.palPalEntry[i].peBlue = 0;
  411. pal.palPalEntry[i].peFlags = 0;
  412. }
  413. i = 256;
  414. }
  415. glp16to8 = NULL;
  416. pal.palVersion = 0x300;
  417. pal.palNumEntries = i;
  418. return CreatePalette((LPLOGPALETTE)&pal);
  419. }
  420. #pragma optimize ("", on)
  421. static void SortBoxes()
  422. {
  423. ColorBox *box;
  424. ColorBox *newList;
  425. ColorBox *insBox;
  426. ColorBox *nextBox;
  427. newList = UsedBoxes;
  428. nextBox = newList->next;
  429. newList->next = NULL;
  430. for (box = nextBox; box; box = nextBox) { // just an insertion sort...
  431. nextBox = box->next;
  432. if (box->wt > newList->wt) {
  433. box->next = newList;
  434. newList = box;
  435. } else {
  436. for (insBox = newList;
  437. insBox->next && (box->wt < insBox->next->wt);
  438. insBox = insBox->next) ;
  439. box->next = insBox->next;
  440. insBox->next = box;
  441. }
  442. }
  443. UsedBoxes = newList;
  444. }
  445. /*
  446. allocate space for nBoxes boxes, set up links. On exit UsedBoxes
  447. points to one box, FreeBoxes points to remaining (nBoxes-1) boxes.
  448. return 0 if successful.
  449. */
  450. static BOOL InitBoxes(int nBoxes)
  451. {
  452. int i;
  453. hBoxes = LocalAlloc(LHND, nBoxes*sizeof(ColorBox));
  454. if (!hBoxes)
  455. return FALSE;
  456. UsedBoxes = (ColorBox*)LocalLock(hBoxes);
  457. FreeBoxes = UsedBoxes + 1;
  458. UsedBoxes->next = NULL;
  459. for (i = 0; i < nBoxes - 1; ++i)
  460. {
  461. FreeBoxes[i].next = FreeBoxes + i + 1;
  462. }
  463. FreeBoxes[nBoxes-2].next = NULL;
  464. /* save the bounding box */
  465. UsedBoxes->rmin = UsedBoxes->gmin = UsedBoxes->bmin = 0;
  466. UsedBoxes->rmax = UsedBoxes->gmax = UsedBoxes->bmax = IN_SIZE - 1;
  467. UsedBoxes->variance = 9999999; /* arbitrary large # */
  468. return TRUE;
  469. }
  470. static void DeleteBoxes()
  471. {
  472. LocalUnlock(hBoxes);
  473. LocalFree(hBoxes);
  474. hBoxes = NULL;
  475. }
  476. static void SplitBox(ColorBox *box)
  477. {
  478. /*
  479. split box into two roughly equal halves and update the data structures
  480. appropriately.
  481. */
  482. Cut cutaxis;
  483. ColorBox *temp, *temp2, *prev;
  484. cutaxis = FindSplitAxis(box);
  485. /* split the box along that axis. If rc != 0 then the box contains
  486. one color, and should not be split */
  487. if (SplitBoxAxis(box, cutaxis))
  488. return;
  489. /* shrink each of the boxes to fit the points they enclose */
  490. ShrinkBox(box);
  491. ShrinkBox(FreeBoxes);
  492. /* move old box down in list, if necessary */
  493. if (box->next && box->variance < box->next->variance)
  494. {
  495. UsedBoxes = box->next;
  496. temp = box;
  497. do {
  498. prev = temp;
  499. temp = temp->next;
  500. } while (temp && temp->variance > box->variance);
  501. box->next = temp;
  502. prev->next = box;
  503. }
  504. /* insert the new box in sorted order (descending), removing it
  505. from the free list. */
  506. if (FreeBoxes->variance >= UsedBoxes->variance)
  507. {
  508. temp = FreeBoxes;
  509. FreeBoxes = FreeBoxes->next;
  510. temp->next = UsedBoxes;
  511. UsedBoxes = temp;
  512. }
  513. else
  514. {
  515. temp = UsedBoxes;
  516. do {
  517. prev = temp;
  518. temp = temp->next;
  519. } while (temp && temp->variance > FreeBoxes->variance);
  520. temp2 = FreeBoxes->next;
  521. FreeBoxes->next = temp;
  522. prev->next = FreeBoxes;
  523. FreeBoxes = temp2;
  524. }
  525. }
  526. static Cut FindSplitAxis(ColorBox *box)
  527. {
  528. unsigned long proj_r[IN_SIZE],proj_g[IN_SIZE],proj_b[IN_SIZE];
  529. unsigned long f;
  530. double currentMax,mean;
  531. unsigned long w,w1,m,m1;
  532. short r,g,b;
  533. short bestCut;
  534. color bestAxis;
  535. Cut cutRet;
  536. double temp1,temp2;
  537. for (r = 0; r < IN_SIZE; r++) {
  538. proj_r[r] = proj_g[r] = proj_b[r] = 0;
  539. }
  540. w = 0;
  541. // Project contents of box down onto axes
  542. for (r = box->rmin; r <= box->rmax; r++) {
  543. for (g = box->gmin; g <= box->gmax; ++g) {
  544. for (b = box->bmin; b <= box->bmax; ++b) {
  545. f = hist(r,g,b);
  546. proj_r[r] += f;
  547. proj_g[g] += f;
  548. proj_b[b] += f;
  549. }
  550. }
  551. w += proj_r[r];
  552. }
  553. currentMax = 0.0f;
  554. #define Check_Axis(l,color) \
  555. m = 0; \
  556. for (l = box->l##min; l <= box->l##max; (l)++) { \
  557. m += l * proj_##l[l]; \
  558. } \
  559. mean = ((double) m) / ((double) w); \
  560. \
  561. w1 = 0; \
  562. m1 = 0; \
  563. for (l = box->l##min; l <= box->l##max; l++) { \
  564. w1 += proj_##l[l]; \
  565. if (w1 == 0) \
  566. continue; \
  567. if (w1 == w) \
  568. break; \
  569. m1 += l * proj_##l[l]; \
  570. temp1 = mean - (((double) m1) / ((double) w1)); \
  571. temp2 = (((double) w1) / ((double) (w-w1))) * temp1 * temp1; \
  572. if (temp2 > currentMax) { \
  573. bestCut = l; \
  574. bestAxis = color; \
  575. currentMax = temp2; \
  576. } \
  577. }
  578. Check_Axis(r,red);
  579. Check_Axis(g,green);
  580. Check_Axis(b,blue);
  581. cutRet.cutaxis = bestAxis;
  582. cutRet.cutpoint = bestCut;
  583. return cutRet;
  584. }
  585. static int SplitBoxAxis(ColorBox *box, Cut cutaxis)
  586. {
  587. /*
  588. Split box along splitaxis into two boxes, one of which is placed
  589. back in box, the other going in the first free box (FreeBoxes)
  590. If the box only contains one color, return non-zero, else return 0.
  591. */
  592. ColorBox *next;
  593. if ( box->variance == 0)
  594. return 1;
  595. /* copy all non-link information to new box */
  596. next = FreeBoxes->next;
  597. *FreeBoxes = *box;
  598. FreeBoxes->next = next;
  599. switch (cutaxis.cutaxis)
  600. {
  601. case red:
  602. box->rmax = cutaxis.cutpoint;
  603. FreeBoxes->rmin = cutaxis.cutpoint+1;
  604. break;
  605. case green:
  606. box->gmax = cutaxis.cutpoint;
  607. FreeBoxes->gmin = cutaxis.cutpoint+1;
  608. break;
  609. case blue:
  610. box->bmax = cutaxis.cutpoint;
  611. FreeBoxes->bmin = cutaxis.cutpoint+1;
  612. break;
  613. }
  614. return 0;
  615. }
  616. static void ShrinkBox(ColorBox *box)
  617. {
  618. unsigned long n, sxx, sx2, var, quotient, remainder;
  619. int r,g,b;
  620. unsigned long f;
  621. unsigned long proj_r[IN_SIZE],proj_g[IN_SIZE],proj_b[IN_SIZE];
  622. n = 0;
  623. for (r = 0; r < IN_SIZE; r++) {
  624. proj_r[r] = proj_g[r] = proj_b[r] = 0;
  625. }
  626. // Project contents of box down onto axes
  627. for (r = box->rmin; r <= box->rmax; r++) {
  628. for (g = box->gmin; g <= box->gmax; ++g) {
  629. for (b = box->bmin; b <= box->bmax; ++b) {
  630. f = hist(r,g,b);
  631. proj_r[r] += f;
  632. proj_g[g] += f;
  633. proj_b[b] += f;
  634. }
  635. }
  636. n += proj_r[r];
  637. }
  638. box->wt = n;
  639. var = 0;
  640. #define AddAxisVariance(c) \
  641. sxx = 0; sx2 = 0; \
  642. for (c = box->c##min; c <= box->c##max; c++) { \
  643. sxx += proj_##c[c] * c * c; \
  644. sx2 += proj_##c[c] * c; \
  645. } \
  646. quotient = sx2 / n; /* This stuff avoids overflow */ \
  647. remainder = sx2 % n; \
  648. var += sxx - quotient * sx2 - ((remainder * sx2)/n);
  649. AddAxisVariance(r);
  650. AddAxisVariance(g);
  651. AddAxisVariance(b);
  652. box->variance = var;
  653. }
  654. static COLORREF DetermineRepresentative(ColorBox *box, int palIndex)
  655. {
  656. /*
  657. determines the rgb value to represent the pixels contained in
  658. box. nbits is the # bits/component we're allowed to return.
  659. */
  660. long f;
  661. long Rval, Gval, Bval;
  662. unsigned long total;
  663. int r, g, b;
  664. WORD w;
  665. /* compute the weighted sum of the elements in the box */
  666. Rval = Gval = Bval = total = 0;
  667. for (r = box->rmin; r <= box->rmax; ++r)
  668. {
  669. for (g = box->gmin; g <= box->gmax; ++g)
  670. {
  671. for (b = box->bmin; b <= box->bmax; ++b)
  672. {
  673. if (glp16to8)
  674. {
  675. w = (WORD)(b) | ((WORD)(g)<<IN_DEPTH) | ((WORD)(r)<<(IN_DEPTH*2));
  676. glp16to8[w] = (BYTE)palIndex;
  677. }
  678. f = hist(r,g,b);
  679. if (f == 0L)
  680. continue;
  681. Rval += f * (long) r;
  682. Gval += f * (long) g;
  683. Bval += f * (long) b;
  684. total += f;
  685. }
  686. }
  687. }
  688. /* Bias the sum so that we round up at .5 */
  689. Rval += total / 2;
  690. Gval += total / 2;
  691. Bval += total / 2;
  692. return RGB(Rval*255/total/IN_SIZE, Gval*255/total/IN_SIZE, Bval*255/total/IN_SIZE);
  693. }
  694. ///////////////////////////////////////////////////////////////////////////////
  695. //
  696. ///////////////////////////////////////////////////////////////////////////////
  697. ///////////////////////////////////////////////////////////////////////////////
  698. //
  699. // write this stuff in ASM!
  700. //
  701. ///////////////////////////////////////////////////////////////////////////////
  702. void Histogram24(BYTE HUGE *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram)
  703. {
  704. int x,y;
  705. BYTE r,g,b;
  706. WORD w;
  707. UseHistogram(lpHistogram);
  708. WidthBytes -= dx*3;
  709. for (y=0; y<dy; y++)
  710. {
  711. for (x=0; x<dx; x++)
  712. {
  713. b = *pb++;
  714. g = *pb++;
  715. r = *pb++;
  716. w = RGB16(r,g,b);
  717. IncHistogram(w);
  718. }
  719. pb += WidthBytes;
  720. }
  721. }
  722. void Histogram16(BYTE HUGE *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram)
  723. {
  724. int x,y;
  725. WORD w;
  726. UseHistogram(lpHistogram);
  727. WidthBytes -= dx*2;
  728. for (y=0; y<dy; y++)
  729. {
  730. for (x=0; x<dx; x++)
  731. {
  732. w = *((WORD HUGE *)pb)++;
  733. w &= 0x7FFF;
  734. IncHistogram(w);
  735. }
  736. pb += WidthBytes;
  737. }
  738. }
  739. void Histogram8(BYTE HUGE *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors)
  740. {
  741. int x,y;
  742. WORD w;
  743. UseHistogram(lpHistogram);
  744. WidthBytes -= dx;
  745. for (y=0; y<dy; y++)
  746. {
  747. for (x=0; x<dx; x++)
  748. {
  749. w = lpColors[*pb++];
  750. IncHistogram(w);
  751. }
  752. pb += WidthBytes;
  753. }
  754. }
  755. void Histogram4(BYTE HUGE *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors)
  756. {
  757. int x,y;
  758. BYTE b;
  759. WORD w;
  760. UseHistogram(lpHistogram);
  761. WidthBytes -= (dx+1)/2;
  762. for (y=0; y<dy; y++)
  763. {
  764. for (x=0; x<(dx+1)/2; x++)
  765. {
  766. b = *pb++;
  767. w = lpColors[b>>4];
  768. IncHistogram(w);
  769. w = lpColors[b&0x0F];
  770. IncHistogram(w);
  771. }
  772. pb += WidthBytes;
  773. }
  774. }
  775. void Histogram1(BYTE HUGE *pb, int dx, int dy, WORD WidthBytes, LPHISTOGRAM lpHistogram, LPWORD lpColors)
  776. {
  777. int x,y,i;
  778. BYTE b;
  779. WORD w;
  780. UseHistogram(lpHistogram);
  781. WidthBytes -= (dx+7)/8;
  782. for (y=0; y<dy; y++)
  783. {
  784. for (x=0; x<(dx+7)/8; x++)
  785. {
  786. b = *pb++;
  787. for (i=0; i<8; i++)
  788. {
  789. w = lpColors[b>>7];
  790. IncHistogram(w);
  791. b<<=1;
  792. }
  793. }
  794. pb += WidthBytes;
  795. }
  796. }
  797. ///////////////////////////////////////////////////////////////////////////////
  798. //
  799. // write this stuff in ASM! too
  800. // -- if you do - please leave the C version #ifdef WIN32
  801. //
  802. ///////////////////////////////////////////////////////////////////////////////
  803. void Reduce24(BYTE HUGE *pbIn, int dx, int dy, WORD cbIn, BYTE HUGE *pbOut, WORD cbOut, LPBYTE lp16to8)
  804. {
  805. int x,y;
  806. BYTE r,g,b;
  807. cbOut -= dx;
  808. cbIn -= dx*3;
  809. for (y=0; y<dy; y++)
  810. {
  811. for (x=0; x<dx; x++)
  812. {
  813. b = *pbIn++;
  814. g = *pbIn++;
  815. r = *pbIn++;
  816. *pbOut++ = lp16to8[RGB16(r,g,b)];
  817. }
  818. pbIn += cbIn;
  819. pbOut+= cbOut;
  820. }
  821. }
  822. void Reduce16(BYTE huge *pbIn, int dx, int dy, WORD cbIn, BYTE huge *pbOut, WORD cbOut, LPBYTE lp16to8)
  823. {
  824. int x,y;
  825. WORD w;
  826. cbOut -= dx;
  827. cbIn -= dx*2;
  828. for (y=0; y<dy; y++)
  829. {
  830. for (x=0; x<dx; x++)
  831. {
  832. w = *((WORD HUGE *)pbIn)++;
  833. *pbOut++ = lp16to8[w&0x7FFF];
  834. }
  835. pbIn += cbIn;
  836. pbOut+= cbOut;
  837. }
  838. }
  839. void Reduce8(BYTE HUGE *pbIn, int dx, int dy, WORD cbIn, BYTE HUGE *pbOut, WORD cbOut, LPBYTE lp8to8)
  840. {
  841. int x,y;
  842. cbIn -= dx;
  843. cbOut -= dx;
  844. for (y=0; y<dy; y++)
  845. {
  846. for (x=0; x<dx; x++)
  847. {
  848. *pbOut++ = lp8to8[*pbIn++];
  849. }
  850. pbIn += cbIn;
  851. pbOut += cbOut;
  852. }
  853. }
  854. void Reduce4(BYTE HUGE *pbIn, int dx, int dy, WORD cbIn, BYTE HUGE *pbOut, WORD cbOut, LPBYTE lp8to8)
  855. {
  856. int x,y;
  857. BYTE b;
  858. cbIn -= (dx+1)/2;
  859. cbOut -= (dx+1)&~1;
  860. for (y=0; y<dy; y++)
  861. {
  862. for (x=0; x<(dx+1)/2; x++)
  863. {
  864. b = *pbIn++;
  865. *pbOut++ = lp8to8[b>>4];
  866. *pbOut++ = lp8to8[b&0x0F];
  867. }
  868. pbIn += cbIn;
  869. pbOut += cbOut;
  870. }
  871. }
  872. void Reduce1(BYTE HUGE *pbIn, int dx, int dy, WORD cbIn, BYTE HUGE *pbOut, WORD cbOut, LPBYTE lp8to8)
  873. {
  874. int x,y;
  875. BYTE b;
  876. cbIn -= (dx+7)/8;
  877. cbOut -= dx;
  878. for (y=0; y<dy; y++)
  879. {
  880. for (x=0; x<dx; x++)
  881. {
  882. if (x%8 == 0)
  883. b = *pbIn++;
  884. *pbOut++ = lp8to8[b>>7];
  885. b<<=1;
  886. }
  887. pbIn += cbIn;
  888. pbOut += cbOut;
  889. }
  890. }