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