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.

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