|
|
1.1 root 1: /******************************Module*Header*******************************\
2: * Module Name: dibmp.c
3: *
4: * Contains functions that manipulate the DIB color
5: *
6: * Created: 30-Jan-1992 21:21:05
7: * Author: Petrus Wong
8: *
9: * Copyright (c) 1990 Microsoft Corporation
10: *
11: * These functions will be called from the ChildWndProc() as it processes
12: * the corresponding WM_COMMAND message for changing the color of the bitmap.
13: *
14: * iCreatePenFrPal() is called by InitializeApp() for creating pens based on
15: * either system or custom palette.
16: *
17: * Dependencies:
18: * none
19: *
20: \**************************************************************************/
21: #include <windows.h>
22: #include <stdio.h>
23: #include "jtypes.h"
24: #include "dibmp.h"
25:
1.1.1.3 ! root 26: #define CYCLETHRD
1.1 root 27:
28: //#define DEBUG
29:
30: #ifndef DEBUG
31: #undef OutputDebugString
32: #define OutputDebugString(LPCSTR)
33: #endif
34:
1.1.1.3 ! root 35: extern HPALETTE ghPal, ghPalOld;
1.1 root 36: extern HWND ghwndMain;
37: extern HWND ghwndClient;
38: extern HANDLE hQuitEvent;
39: extern char gtext[256];
40: extern HANDLE ghAccel;
41: BOOL bValidMode(INT);
42: BOOL bColor2BW(PBITMAPINFO);
43: BOOL bChangeDIBColor(HDC, PINFO, INT);
44: BOOL bColorShift(PBITMAPINFO);
45: BOOL bColorReplace(PBITMAPINFO);
46: BOOL bColorCycle(HDC, RECT, PBYTE, PBITMAPINFO, PINFO);
47: BOOL bCycle(HWND);
48: BOOL bColor2Mono(HDC, PINFO, PBITMAPINFO);
1.1.1.3 ! root 49: INT iCreatePenFrPal(HDC, PVOID *, INT, HPALETTE *);
! 50: HPALETTE CopyPalette(HPALETTE);
1.1 root 51: extern PINFO pGetInfoData(HWND);
52: extern BOOL bReleaseInfoData(HWND);
53:
54: /******************************Public*Routine******************************\
55: *
56: * bChangeDIBColor
57: *
58: * Effects: Main entry point for the color changing operations. It
59: * 1. validates the operation
60: * 2. retrieves the bitmap info from the graphics engine
61: * 3. dispatches the call to the right function
62: * 4. saves the resulting bitmap
63: * 5. updates the screen
64: *
65: * Warnings:
66: *
67: * History:
68: * 30-Jan-1992 -by- Petrus Wong
69: * Wrote it.
70: \**************************************************************************/
71: BOOL bChangeDIBColor(HDC hDC, PINFO pInfo, INT iMode)
72: {
73: BOOL bSuccess;
74: HBITMAP hTmpBmp, hBmpOld;
75: PBITMAPINFO pbmi;
76: PBYTE pBits;
77: BITMAPINFO bmi;
78: PBYTE pjTmp, pjTmpBmi;
79: ULONG sizBMI;
80: RECT rc;
81: BOOL bCycle;
82:
83:
84: bSuccess = TRUE;
85: if (!pInfo->hBmpSaved) {
86: MessageBox(ghwndMain, "There's no Bitmap to change!", "Error", MB_OK);
87: return FALSE;
88: }
89:
90: if (!bValidMode(iMode)) {
91: MessageBox(ghwndMain, "bChangeDIBColor: invalid mode!", "Error", MB_OK);
92: return FALSE;
93: }
94:
1.1.1.3 ! root 95: if (pInfo->hPal) {
! 96: HPALETTE hPalOld;
! 97: int ii;
! 98:
! 99: hPalOld = SelectPalette(hDC, pInfo->hPal, FALSE);
! 100: ii = RealizePalette(hDC);
! 101:
! 102: if (ii){
! 103: UpdateColors (hDC);
! 104: }
! 105:
! 106: }
! 107:
1.1 root 108: //
109: // Let the graphics engine to retrieve the dimension of the bitmap for us
110: // GetDIBits uses the size to determine if it's BITMAPCOREINFO or BITMAPINFO
111: // if BitCount != 0, color table will be retrieved
112: //
113: bmi.bmiHeader.biSize = 0x28; // GDI need this to work
114: bmi.bmiHeader.biBitCount = 0; // don't get the color table
115: if ((GetDIBits(hDC, pInfo->hBmpSaved, 0, 0, (LPSTR)NULL, &bmi, DIB_RGB_COLORS)) == 0) {
116: MessageBox(ghwndMain, "GetDIBits failed!", "Error", MB_OK);
117: return FALSE;
118: }
119:
120: //
121: // Now that we know the size of the image, alloc enough memory to retrieve
122: // the actual bits
123: //
124: if ((pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
125: bmi.bmiHeader.biSizeImage)) == NULL) {
126: MessageBox(ghwndMain, "Failed in Memory Allocation for pBits!", "Error", MB_OK);
127: return FALSE;
128: }
129:
130: //
131: // Note: 24 bits per pixel has no color table. So, we don't have to
132: // allocate memory for retrieving that. Otherwise, we do.
133: //
134: pbmi = &bmi; // assume no color table
135: if (bmi.bmiHeader.biBitCount != 24) { // has color table
136: sizBMI = sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(1<<bmi.bmiHeader.biBitCount);
137: //
138: // I need more memory for the color table
139: //
140: if ((pbmi = (PBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizBMI )) == NULL) {
141: MessageBox(ghwndMain, "Failed in Memory Allocation for pbmi!", "Error", MB_OK);
142: bSuccess = FALSE;
143: goto ErrExit1;
144: }
145:
146: //
147: // Now that we've a bigger chunk of memory, let's copy the Bitmap
148: // info header data over
149: //
150: pjTmp = (PBYTE)pbmi;
151: pjTmpBmi = (PBYTE)&bmi;
152: sizBMI = sizeof(BITMAPINFOHEADER);
153:
154: while(sizBMI--)
155: {
156: *(((PBYTE)pjTmp)++) = *((pjTmpBmi)++);
157: }
158:
159: }
160:
161: //
162: // Bitmap can't be selected into a DC when calling GetDIBits
163: // Assume that the hDC is the DC where the bitmap would have been selected
164: // if indeed it has been selected
165: //
166: if (hTmpBmp = CreateCompatibleBitmap(hDC, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight)) {
167: hBmpOld = SelectObject(hDC, hTmpBmp);
168: if ((GetDIBits(hDC, pInfo->hBmpSaved, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS))==0){
169: MessageBox(ghwndMain, "Failed in GetDIBits!", "Error", MB_OK);
170: bSuccess = FALSE;
171: goto ErrExit4;
172: }
173: } else {
174: MessageBox(ghwndMain, "Failed in creating bitmap!", "Error", MB_OK);
175: bSuccess = FALSE;
176: goto ErrExit3;
177: }
178:
179: bCycle = FALSE;
180: switch (iMode) {
181: case MM_BW:
182: if ((!bColor2Mono(hDC, pInfo, pbmi)) ||
183: (pInfo->hBmpMono == NULL)) {
184: MessageBox(ghwndMain, "Failed in Color2Mono!", "Error", MB_OK);
185: bSuccess = FALSE;
186: goto ErrExit4;
187: }
188: pInfo->bUseMono = TRUE;
189: break;
190: case MM_SHIFT:
1.1.1.3 ! root 191:
1.1 root 192: if (!bColorShift(pbmi)) {
193: MessageBox(ghwndMain, "Failed in ColorShift!", "Error", MB_OK);
194: bSuccess = FALSE;
195: goto ErrExit4;
196: }
197: break;
198: case MM_CUSTOM:
199: bColorReplace(pbmi);
200: break;
201:
202: case MM_CYCLE:
203: bCycle = TRUE;
204: break;
205:
206: default:
207: bSuccess = FALSE;
208: break;
209: }
210:
211: if (SetDIBits(hDC, pInfo->hBmpSaved, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits,
212: pbmi, DIB_RGB_COLORS) == 0) {
213: MessageBox(ghwndMain, "Failed in SetDIBits!", "Error", MB_OK);
214: bSuccess = FALSE;
215: goto ErrExit4;
216: }
217:
218: if (bCycle)
219: pInfo->bSetDIBsToDevice = TRUE;
220:
1.1.1.3 ! root 221: #if 0
1.1 root 222: if (pInfo->bSetDIBsToDevice && !pInfo->bUseMono) {
223: //GetWindowRect(pInfo->hwnd, &rc);
224: GetClientRect(pInfo->hwnd, &rc);
225: //SetMapMode(hDC, MM_TEXT);
226:
1.1.1.3 ! root 227: //if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top,
! 228: // 0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS) == 0) {
! 229: // MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK);
! 230: // bSuccess = FALSE;
! 231: // goto ErrExit4;
! 232: //}
1.1 root 233: } else {
1.1.1.3 ! root 234: #endif
1.1 root 235: InvalidateRect(pInfo->hwnd, NULL, FALSE);
1.1.1.3 ! root 236: // }
1.1 root 237:
238: if (bCycle) {
1.1.1.3 ! root 239: //
! 240: // This is commented out because I don't want the system colors
! 241: // be affected by color cycling
! 242: //
1.1 root 243: //SetSystemPaletteUse(hDC, SYSPAL_NOSTATIC);
244: bColorCycle(hDC, rc, pBits, pbmi, pInfo);
245: //SetSystemPaletteUse(hDC, SYSPAL_STATIC);
246: }
247:
248: ErrExit4:
249: SelectObject(hDC, hBmpOld);
250: DeleteObject(hTmpBmp);
251: ErrExit3:
252: GlobalFree(pbmi);
253: ErrExit1:
254: GlobalFree(pBits);
255: return bSuccess;
256: }
257:
258:
259: /******************************Public*Routine******************************\
260: *
261: * bValidMode
262: *
263: * Effects: validates the change mode for ChangeDIBColor function
264: * returns TRUE if mode is valid. Otherwise, FALSE
265: *
266: * Warnings:
267: *
268: * History:
269: * 30-Jan-1992 -by- Petrus Wong
270: * Wrote it.
271: \**************************************************************************/
272: BOOL bValidMode(INT iMode)
273: {
274: if ((iMode == MM_BW) || (iMode == MM_SHIFT) ||
275: (iMode == MM_CUSTOM) || (iMode == MM_CYCLE))
276: return TRUE;
277: else
278: return FALSE;
279: }
280:
281: /******************************Public*Routine******************************\
282: *
283: * bColor2BW
284: *
285: * Effects: changes the color table in the bitmapinfo to black and white
286: *
287: * Warnings: doesn't check if pbmi is valid or not
288: *
289: * History:
290: * 30-Jan-1992 -by- Petrus Wong
291: * Wrote it.
292: \**************************************************************************/
293: BOOL bColor2BW(PBITMAPINFO pbmi)
294: {
295: INT iEntry;
296:
297: //
298: // CR! What's the right behavior? Maybe I should convert this to a
299: // monochrome bitmap. This will be very useful in MaskBlt().
300: //
301: iEntry = 1<<pbmi->bmiHeader.biBitCount;
302: while (--iEntry >= 0) {
303: if (iEntry <= iEntry / 2) {
304: pbmi->bmiColors[iEntry].rgbBlue = 0x00;
305: pbmi->bmiColors[iEntry].rgbGreen = 0x00;
306: pbmi->bmiColors[iEntry].rgbRed = 0x00;
307: pbmi->bmiColors[iEntry].rgbReserved = 0x00;
308: } else {
309: pbmi->bmiColors[iEntry].rgbBlue = 0xFF;
310: pbmi->bmiColors[iEntry].rgbGreen = 0xFF;
311: pbmi->bmiColors[iEntry].rgbRed = 0xFF;
312: pbmi->bmiColors[iEntry].rgbReserved = 0x00;
313: }
314: }
315: return TRUE;
316: }
317:
318:
319: /******************************Public*Routine******************************\
320: *
321: * bColorShift
322: *
323: * Effects: Shifting entries of the color table
324: *
325: * Warnings:
326: *
327: * History:
328: * 30-Jan-1992 -by- Petrus Wong
329: * Wrote it.
330: \**************************************************************************/
331:
332: BOOL bColorShift(PBITMAPINFO pbmi)
333: {
334: INT iEntry;
335: RGBQUAD Tmp;
336:
337: iEntry = 1<<pbmi->bmiHeader.biBitCount;
338:
339: Tmp.rgbBlue = pbmi->bmiColors[iEntry-1].rgbBlue;
340: Tmp.rgbGreen = pbmi->bmiColors[iEntry-1].rgbGreen;
341: Tmp.rgbRed = pbmi->bmiColors[iEntry-1].rgbRed;
342: Tmp.rgbReserved = pbmi->bmiColors[iEntry-1].rgbReserved;
343:
344: while (--iEntry > 0) {
345: pbmi->bmiColors[iEntry].rgbBlue =
346: pbmi->bmiColors[iEntry-1].rgbBlue;
347: pbmi->bmiColors[iEntry].rgbGreen =
348: pbmi->bmiColors[iEntry-1].rgbGreen;
349: pbmi->bmiColors[iEntry].rgbRed =
350: pbmi->bmiColors[iEntry-1].rgbRed;
351: pbmi->bmiColors[iEntry].rgbReserved =
352: pbmi->bmiColors[iEntry-1].rgbReserved;
353: }
354: pbmi->bmiColors[0].rgbBlue = Tmp.rgbBlue;
355: pbmi->bmiColors[0].rgbGreen = Tmp.rgbGreen;
356: pbmi->bmiColors[0].rgbRed = Tmp.rgbRed;
357: pbmi->bmiColors[0].rgbReserved = Tmp.rgbReserved;
358: return TRUE;
359:
360: }
361:
362: BOOL bColorReplace(PBITMAPINFO pbmi)
363: {
364: return FALSE;
365: UNREFERENCED_PARAMETER(pbmi);
366: }
367:
368: /******************************Public*Routine******************************\
369: *
370: * bColorCycle
371: *
372: * Effects: Create a logical palette with a good spread of color
373: * Animate the palette, shift the palette entries and animate
374: * again for 256 times altogether.
375: * This creates the color cycling effect.
376: *
377: * Warnings: Only works in device that support palette.
378: *
379: * History:
380: * 31-May-1992 Petrus Wong Check if drawing window is gone or not
381: * 24-Apr-1992 -by- Petrus Wong
382: * Wrote it.
383: \**************************************************************************/
384:
385: BOOL bColorCycle(HDC hDC, RECT rc, PBYTE pBits, PBITMAPINFO pbmi, PINFO pInfo)
386: {
1.1.1.3 ! root 387: INT iEntry, i, iNumClr;
1.1 root 388: PLOGPALETTE plogPat;
389: ULONG ulSize;
390: BOOL bSuccess;
391: HPALETTE hPal, hPalOld;
392: PALETTEENTRY peTemp;
1.1.1.3 ! root 393:
1.1 root 394: #ifdef CYCLETHRD
395: DWORD dwWait;
1.1.1.3 ! root 396: #else
1.1 root 397: MSG msg;
1.1.1.3 ! root 398: #endif
1.1 root 399: BOOL bQuit;
400:
1.1.1.3 ! root 401: //UINT uRC;
1.1 root 402:
403: bSuccess = TRUE;
404: iEntry = 1<<pbmi->bmiHeader.biBitCount;
1.1.1.3 ! root 405:
! 406: iNumClr = 236; // 256 minus 20 reserved colors
1.1 root 407: ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*256;
408:
409: if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) {
410: MessageBox(ghwndMain, "Failed in Memory Allocation for plogPat!", "Error", MB_OK);
411: bSuccess = FALSE;
412: goto ErrExit1;
413: }
414:
415: plogPat->palVersion = 0x300;
416: plogPat->palNumEntries = (WORD) 256;
1.1.1.3 ! root 417:
! 418: {
! 419: int iFirst, iSecond, iInc;
! 420:
! 421: iFirst = iNumClr/3;
! 422: iSecond = iFirst * 2;
! 423: iInc = (iNumClr <= 256) ? 3 : 1;
! 424:
! 425: for (i = 0; i < iNumClr; i++) {
! 426: if (i < iFirst) {
! 427: plogPat->palPalEntry[i+10].peBlue = 255-(i+1)*iInc;
! 428: plogPat->palPalEntry[i+10].peGreen = 0;
! 429: plogPat->palPalEntry[i+10].peRed = (i+1)*iInc;
! 430: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 431: continue;
! 432: }
! 433: if (i < iSecond) {
! 434: plogPat->palPalEntry[i+10].peBlue = 0;
! 435: plogPat->palPalEntry[i+10].peGreen = (i+1-iFirst)*iInc;
! 436: plogPat->palPalEntry[i+10].peRed = 255-(i+1-iFirst)*iInc;
! 437: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 438: continue;
! 439: }
! 440: plogPat->palPalEntry[i+10].peBlue = (i+1-iSecond)*iInc;
! 441: plogPat->palPalEntry[i+10].peGreen = 255-(i+1-iSecond)*iInc;
! 442: plogPat->palPalEntry[i+10].peRed = 0;
! 443: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 444: }
1.1 root 445: }
446:
447: if ((hPal = CreatePalette(plogPat)) == (HPALETTE) NULL) {
448: MessageBox(ghwndMain, "Failed in creating palette!", "Error", MB_OK);
449: bSuccess = FALSE;
450: goto ErrExit2;
451: }
452:
1.1.1.3 ! root 453: pInfo->hCyclePal = hPal;
! 454:
1.1 root 455: hPalOld = SelectPalette(hDC, hPal, FALSE);
456: RealizePalette(hDC);
457:
1.1.1.3 ! root 458:
1.1 root 459: #ifndef CYCLETHRD
460:
461: bQuit = FALSE;
462: while (TRUE && !bQuit) {
463:
464: if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
465: //sprintf( gtext,"msg = %lx\n", msg.message);
466: //OutputDebugString( gtext );
467:
468: if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) ||
469: ((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) {
470: sprintf( gtext,"msg = %lx, wPram = %lx\n", msg.message, msg.wParam);
471: OutputDebugString( gtext );
472: bQuit = TRUE;
473: PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
474: } else {
475: if (!TranslateAccelerator( ghwndMain, ghAccel, &msg) &&
476: !TranslateMDISysAccel( ghwndClient, &msg) ) {
477: TranslateMessage(&msg);
478: DispatchMessage(&msg);
479: }
480: }
481: }
482:
483: #else
484: bQuit = FALSE;
485: while (TRUE) {
486: //
487: // If parent gets a WM_CLOSE, we will return
488: //
489: dwWait = WaitForSingleObject(pInfo->hQuitEvent, 0);
490: if (dwWait == WAIT_TIMEOUT) {
491: MessageBox(ghwndMain, "Cycle Thread Quitting!", "CYCLE Thread", MB_OK);
492: break;
493: }
494: #endif
495: if (!pInfo->bClrCycle) {
496: sprintf( gtext,"NOT to animate palette\n");
497: OutputDebugString( gtext );
498: bQuit = TRUE;
499: }
500:
501: if (!bQuit) {
502: sprintf( gtext,"About to animate palette\n");
503: OutputDebugString( gtext );
504:
1.1.1.3 ! root 505: peTemp = plogPat->palPalEntry[10];
! 506: for (i = 0; i < iNumClr; i++) {
! 507: plogPat->palPalEntry[i+10] = plogPat->palPalEntry[i+10+1];
1.1 root 508: }
1.1.1.3 ! root 509: plogPat->palPalEntry[i-1+10] = peTemp;
1.1 root 510:
511: if (!AnimatePalette(hPal, 0, 256, plogPat->palPalEntry)) {
512: sprintf( gtext,"Anmiate palette failed\n");
513: OutputDebugString( gtext );
514: }
1.1.1.3 ! root 515: Sleep(50);
1.1 root 516:
1.1.1.3 ! root 517: //
! 518: // This is not needed anymore starting from build 475 14-Jun-1993
! 519: //
! 520: #if 0
! 521: //
! 522: // Not realizing stops color-cycling after moving back into
! 523: // foreground
! 524: //
! 525: if ((uRC = RealizePalette(hDC)) == GDI_ERROR) {
1.1 root 526: sprintf( gtext,"Realize palette failed\n");
527: OutputDebugString( gtext );
528: }
529:
530: if (!UpdateColors(hDC)) {
531: sprintf( gtext,"Update Colors failed\n");
532: OutputDebugString( gtext );
533: }
1.1.1.3 ! root 534: #endif
1.1 root 535: }
536:
537: }
538:
1.1.1.3 ! root 539: SelectPalette(hDC, hPalOld, 0);
1.1 root 540: DeleteObject(hPal);
541: ErrExit2:
542: GlobalFree(plogPat);
543: ErrExit1:
544: return bSuccess;
545:
546: }
547:
548:
549: /******************************Public*Routine******************************\
550: *
551: * bCycle
552: *
553: * Effects: Wrapper for doing color cycling in a separate thread
554: * Called from MM_CYCLE
555: *
556: * Warnings: presents problem when the MDI child is closed if this thread
557: * is not done yet. We will have to wait until DeleteDC is
558: * fully functional.
559: *
560: * History:
561: * 28-May-1992 -by- Petrus Wong
562: * Wrote it.
563: \**************************************************************************/
564: BOOL bCycle(HWND hwnd)
565: {
566: HDC hDC;
567: PINFO pInfo;
568:
569: if ((pInfo = pGetInfoData(hwnd)) == NULL) {
570: return 0L;
571: }
572:
573: hDC = GetDC(pInfo->hwnd);
574: bChangeDIBColor(hDC, pInfo, MM_CYCLE);
575: ReleaseDC(pInfo->hwnd, hDC);
576:
577: bReleaseInfoData(hwnd);
578:
579: ExitThread(0);
580: return TRUE;
581: }
582:
583:
584: /******************************Public*Routine******************************\
585: *
586: * bColor2Mono
587: *
588: * Effects: Create a monochrome bitmap out of the colored one
589: * Using a different a source background clr every time it is called
590: * Saving the Monochrome bitmap in pInfo
591: *
592: * Warnings: It's more straight forward to use GetDIBits than this but
593: * that means some big changes to the bChangeDIBColor.
594: *
595: * History:
596: * 31-May-1992 -by- Petrus Wong
597: * Wrote it.
598: \**************************************************************************/
599:
600: BOOL bColor2Mono(HDC hDC, PINFO pInfo, PBITMAPINFO pbmi) {
601: HDC hDCMemDest, hDCMemSrc;
602: HBITMAP hOldBmpDest, hOldBmpSrc;
603: INT iWidth, iHeight, iEntry;
604: static INT iCount=0;
605:
606:
607: iWidth = pbmi->bmiHeader.biWidth;
608: iHeight = pbmi->bmiHeader.biHeight;
609:
610: if (pInfo->hBmpMono)
611: DeleteObject(pInfo->hBmpMono);
612:
613: pInfo->hBmpMono = CreateBitmap(iWidth, iHeight, pbmi->bmiHeader.biPlanes, 1, NULL);
614: if (pInfo->hBmpMono == NULL) {
615: return(FALSE);
616: }
617:
618: hDCMemDest = CreateCompatibleDC(hDC);
619: hDCMemSrc = CreateCompatibleDC(hDC);
1.1.1.3 ! root 620:
! 621: if (pInfo->hPal) {
! 622: SelectPalette(hDCMemSrc, pInfo->hPal, FALSE);
! 623: RealizePalette(hDCMemSrc);
! 624: }
! 625:
1.1 root 626: iEntry = 1<<pbmi->bmiHeader.biBitCount;
627: SetBkColor(hDCMemSrc, RGB( pbmi->bmiColors[iCount].rgbRed,
628: pbmi->bmiColors[iCount].rgbGreen,
629: pbmi->bmiColors[iCount].rgbBlue) );
630: //SetBkColor(hDCMemDest, RGB( 0, 0, 0));
631: //SetTextColor(hDCMemDest, RGB( 255, 255, 255));
632:
633: iCount++;
634: if (iCount >= iEntry)
635: iCount = 0;
636:
637: hOldBmpDest = SelectObject(hDCMemDest, pInfo->hBmpMono);
638: hOldBmpSrc = SelectObject(hDCMemSrc, pInfo->hBmpSaved);
639: BitBlt(hDCMemDest, 0, 0, iWidth, iHeight, hDCMemSrc, 0, 0, SRCCOPY);
640:
1.1.1.3 ! root 641: pInfo->hBmpMono = SelectObject(hDCMemDest, hOldBmpDest);
1.1 root 642: SelectObject(hDCMemSrc, hOldBmpSrc);
643:
644: DeleteDC(hDCMemDest);
645: DeleteDC(hDCMemSrc);
646:
647: return(TRUE);
648:
649: }
650:
651:
652: /******************************Public*Routine******************************\
653: *
654: * iCreatePenFrPal
655: *
656: * Effects: Create an array of pens from palette.
657: * If device supports palette, then first creates a logical palette
658: * with a good spread of color. Then select the logical palette into
659: * the DC. Create pen that corresponds to each palette entry.
660: * If system does not support palette, then use the system palette.
661: *
662: * prghPen pointer to an array of hPen
663: * If this is NULL, the required size of the array is
664: * returned. If this is not NULL, the array will be filled
665: * with hPens.
666: *
667: * returns the number of hPens created.
668: *
669: * Warnings:
670: *
671: * History:
672: * 13-Jun-1992 -by- Petrus Wong
673: * Wrote it.
674: \**************************************************************************/
1.1.1.3 ! root 675: INT iCreatePenFrPal(HDC hDC, PVOID *prghPen, INT iWidth, HPALETTE *phPal)
1.1 root 676: {
1.1.1.3 ! root 677: INT iNumClr, iResult, i, iEntry;
1.1 root 678: PLOGPALETTE plogPat;
679: ULONG ulSize;
680:
681: iResult = 0;
682:
683: if (!((GetDeviceCaps(hDC, RASTERCAPS)) & RC_PALETTE)) {
684:
1.1.1.3 ! root 685: if (iNumClr = GetSystemPaletteEntries(hDC, 0, 0, NULL)) {
! 686: if (prghPen == NULL) {
! 687: return (iNumClr);
! 688: }
! 689:
! 690: if (iWidth < 0) {
! 691: MessageBox(GetFocus(), "Invalid pen width!", "Error", MB_OK);
! 692: return 0L;
! 693: }
! 694:
! 695: for (i = 0; i < iNumClr; i++) {
! 696: prghPen[i] =
! 697: (PVOID) CreatePen(PS_SOLID, iWidth, PALETTEINDEX(i));
! 698: iResult = i;
! 699: #if 0
! 700: SelectObject(hDC, (HPEN)prghPen[i]);
! 701: MoveToEx(hDC, i*iWidth, 0, NULL);
! 702: LineTo(hDC, i*iWidth, 300);
! 703: #endif
! 704: }
! 705: return iResult;
! 706: }
! 707:
! 708: // GetPaletteEntries returns 0
! 709: iNumClr = 1 << GetDeviceCaps(hDC, BITSPIXEL);
! 710:
! 711: if (iNumClr <= 16) {
! 712: sprintf( gtext,"Assertion failed: iCreatePenFrPal() \n");
! 713: OutputDebugString( gtext);
! 714: sprintf( gtext,"iNumClr <= 16! iNumClr = %d\n", iNumClr);
! 715: OutputDebugString( gtext);
! 716: return (iNumClr);
! 717: }
! 718:
! 719: // Assuming at least 16 bit device
! 720: //
! 721: iNumClr = 768; // COLORREF format: 0x00bbggrr r,g,b=[0,255]
! 722:
1.1 root 723: if (prghPen == NULL) {
724: return (iNumClr);
725: }
726:
1.1.1.3 ! root 727: if (iWidth < 0) {
! 728: MessageBox(GetFocus(), "Invalid pen width!", "Error", MB_OK);
! 729: return 0L;
! 730: }
! 731:
1.1 root 732: for (i = 0; i < iNumClr; i++) {
1.1.1.3 ! root 733: if (i < 256) {
! 734: prghPen[i] =
! 735: (PVOID) CreatePen(PS_SOLID, iWidth, RGB(i,0,255-i));
! 736: iResult = i;
! 737: #if 0
! 738: SelectObject(hDC, (HPEN)prghPen[i]);
! 739: MoveToEx(hDC, i*iWidth, 0, NULL);
! 740: LineTo(hDC, i*iWidth, 300);
! 741: #endif
! 742: continue;
! 743: }
! 744:
! 745: if (i < 512) {
! 746: prghPen[i] =
! 747: (PVOID) CreatePen(PS_SOLID, iWidth, RGB(511-i,i-256,0));
! 748: iResult = i;
! 749: #if 0
! 750: SelectObject(hDC, (HPEN)prghPen[i]);
! 751: MoveToEx(hDC, i*iWidth, 0, NULL);
! 752: LineTo(hDC, i*iWidth, 300);
! 753: #endif
! 754: continue;
! 755: }
! 756:
! 757:
! 758: prghPen[i] =
! 759: (PVOID) CreatePen(PS_SOLID, iWidth, RGB(0,767-i,i-512));
1.1 root 760: iResult = i;
1.1.1.3 ! root 761: #if 0
! 762: SelectObject(hDC, (HPEN)prghPen[i]);
! 763: MoveToEx(hDC, i*iWidth, 0, NULL);
! 764: LineTo(hDC, i*iWidth, 300);
! 765: #endif
1.1 root 766: }
767:
768: return iResult;
769: }
770:
1.1.1.3 ! root 771: //
! 772: // Palette managed device
! 773: //
! 774: iEntry = iNumClr = 1 << GetDeviceCaps(hDC, BITSPIXEL);
1.1 root 775:
1.1.1.3 ! root 776: if (iNumClr > 256) {
! 777: sprintf( gtext,"Assertion failed: iCreatePenFrPal() \n");
! 778: OutputDebugString( gtext);
! 779: sprintf( gtext,"iNumClr > 256! iNumClr = %d\n", iNumClr);
! 780: OutputDebugString( gtext);
! 781: iEntry=iNumClr = 256;
! 782: }
1.1 root 783:
784: ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*iNumClr;
785:
1.1.1.3 ! root 786: iNumClr -= GetDeviceCaps(hDC, NUMRESERVED);
! 787:
! 788: if (prghPen == NULL)
! 789: return iNumClr;
! 790:
1.1 root 791: if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) {
1.1.1.3 ! root 792: MessageBox(GetFocus(), "Failed in Memory Allocation for plogPat!", "Error", MB_OK);
1.1 root 793: goto ErrExit1;
794: }
795:
796: plogPat->palVersion = 0x300;
1.1.1.3 ! root 797: plogPat->palNumEntries = (WORD) iEntry;
! 798:
! 799: {
! 800: int iFirst, iSecond, iInc;
! 801:
! 802: iFirst = iNumClr/3;
! 803: iSecond = iFirst * 2;
! 804: iInc = (iNumClr <= 256) ? 3 : 1;
! 805:
! 806: for (i = 0; i < iNumClr; i++) {
! 807: if (i < iFirst) {
! 808: plogPat->palPalEntry[i+10].peBlue = 255-(i+1)*iInc;
! 809: plogPat->palPalEntry[i+10].peGreen = 0;
! 810: plogPat->palPalEntry[i+10].peRed = (i+1)*iInc;
! 811: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 812: continue;
! 813: }
! 814: if (i < iSecond) {
! 815: plogPat->palPalEntry[i+10].peBlue = 0;
! 816: plogPat->palPalEntry[i+10].peGreen = (i+1-iFirst)*iInc;
! 817: plogPat->palPalEntry[i+10].peRed = 255-(i+1-iFirst)*iInc;
! 818: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 819: continue;
! 820: }
! 821: plogPat->palPalEntry[i+10].peBlue = (i+1-iSecond)*iInc;
! 822: plogPat->palPalEntry[i+10].peGreen = 255-(i+1-iSecond)*iInc;
! 823: plogPat->palPalEntry[i+10].peRed = 0;
! 824: plogPat->palPalEntry[i+10].peFlags = PC_RESERVED;
! 825: }
1.1 root 826: }
827:
1.1.1.3 ! root 828: DeleteObject(*phPal);
! 829:
! 830: if ((*phPal = CreatePalette(plogPat)) == (HPALETTE) NULL) {
! 831: MessageBox(GetFocus(), "Failed in creating palette!", "Error", MB_OK);
1.1 root 832: goto ErrExit2;
833: }
1.1.1.3 ! root 834: if (*phPal) {
! 835: SelectPalette(hDC, *phPal, FALSE);
! 836: RealizePalette(hDC);
! 837: }
1.1 root 838:
839: for (i = 0; i < iNumClr; i++) {
1.1.1.3 ! root 840: HPEN hPen;
! 841:
! 842: hPen = CreatePen(PS_SOLID, iWidth, PALETTEINDEX(i+10));
! 843: prghPen[i] = (PVOID) hPen;
1.1 root 844: iResult = i;
1.1.1.3 ! root 845: #if 0
! 846: SelectObject(hDC, hPen);
! 847: MoveToEx(hDC, i*iWidth, 0, NULL);
! 848: LineTo(hDC, i*iWidth, 300);
! 849: #endif
1.1 root 850: }
851:
852: ErrExit2:
853: GlobalFree(plogPat);
854: ErrExit1:
855: return iResult;
856:
857: }
1.1.1.3 ! root 858:
! 859:
! 860: /******************************Public*Routine******************************\
! 861: *
! 862: * HPALETTE CopyPalette
! 863: *
! 864: * Effects:
! 865: *
! 866: * Warnings:
! 867: *
! 868: * History:
! 869: * 18-Sep-1992 -by- Petrus Wong
! 870: * Wrote it.
! 871: \**************************************************************************/
! 872:
! 873: HPALETTE CopyPalette(HPALETTE hPalSrc)
! 874: {
! 875: PLOGPALETTE plogPal;
! 876: PBYTE pjTmp;
! 877: int iNumEntries=0;
! 878: HPALETTE hPal;
! 879:
! 880: if ((iNumEntries = GetPaletteEntries(hPalSrc, 0, iNumEntries, NULL)) == 0) {
! 881: MessageBox(ghwndMain, "No entry in palette to copy!", "Error", MB_OK);
! 882: return (HPALETTE) NULL;
! 883: }
! 884:
! 885: if ((plogPal = (PLOGPALETTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
! 886: sizeof(DWORD) + sizeof(PALETTEENTRY)*iNumEntries )) == NULL) {
! 887: MessageBox(ghwndMain, "Failed in CopyPalette!", "Error", MB_OK);
! 888: return (HPALETTE) NULL;
! 889: }
! 890:
! 891: plogPal->palVersion = 0x300;
! 892: plogPal->palNumEntries = (WORD) iNumEntries;
! 893: pjTmp = (PBYTE) plogPal;
! 894: pjTmp += 8;
! 895: GetPaletteEntries(hPalSrc, 0, iNumEntries, (PPALETTEENTRY)pjTmp);
! 896: hPal = CreatePalette(plogPal);
! 897:
! 898: GlobalFree(plogPal);
! 899:
! 900: return hPal;
! 901: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.