Annotation of mstools/samples/mandel/dibmp.c, revision 1.1.1.2

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: \**************************************************************************/
1.1.1.2 ! root       21: //#define  STRICT
1.1       root       22: #include <windows.h>
                     23: #include <stdio.h>
                     24: #include "jtypes.h"
                     25: #include "dibmp.h"
                     26: 
                     27: //#define CYCLETHRD
                     28: 
                     29: //#define DEBUG
                     30: 
                     31: #ifndef DEBUG
                     32:    #undef OutputDebugString
                     33:    #define OutputDebugString(LPCSTR)
                     34: #endif
                     35: 
                     36: 
                     37: extern HWND ghwndMain;
                     38: extern HWND ghwndClient;
                     39: extern HANDLE hQuitEvent;
                     40: extern char   gtext[256];
                     41: extern HANDLE ghAccel;
                     42: BOOL bValidMode(INT);
                     43: BOOL bColor2BW(PBITMAPINFO);
                     44: BOOL bChangeDIBColor(HDC, PINFO, INT);
                     45: BOOL bColorShift(PBITMAPINFO);
                     46: BOOL bColorReplace(PBITMAPINFO);
                     47: BOOL bColorCycle(HDC, RECT, PBYTE, PBITMAPINFO, PINFO);
                     48: BOOL bCycle(HWND);
                     49: BOOL bColor2Mono(HDC, PINFO, PBITMAPINFO);
                     50: INT  iCreatePenFrPal(HDC, PVOID *);
                     51: extern PINFO pGetInfoData(HWND);
                     52: extern BOOL bReleaseInfoData(HWND);
                     53: 
                     54: 
                     55: /******************************Public*Routine******************************\
                     56: *
                     57: * bChangeDIBColor
                     58: *
                     59: * Effects: Main entry point for the color changing operations.  It
                     60: *          1. validates the operation
                     61: *          2. retrieves the bitmap info from the graphics engine
                     62: *          3. dispatches the call to the right function
                     63: *          4. saves the resulting bitmap
                     64: *          5. updates the screen
                     65: *
                     66: * Warnings:
                     67: *
                     68: * History:
                     69: *  30-Jan-1992 -by- Petrus Wong
                     70: * Wrote it.
                     71: \**************************************************************************/
                     72: BOOL bChangeDIBColor(HDC hDC, PINFO pInfo, INT iMode)
                     73: {
                     74:     BOOL                 bSuccess;
                     75:     HBITMAP              hTmpBmp, hBmpOld;
                     76:     PBITMAPINFO          pbmi;
                     77:     PBYTE                pBits;
                     78:     BITMAPINFO           bmi;
                     79:     PBYTE                pjTmp, pjTmpBmi;
                     80:     ULONG                sizBMI;
                     81:     RECT                 rc;
                     82:     BOOL                 bCycle;
                     83: 
                     84: 
                     85:     bSuccess = TRUE;
                     86:     if (!pInfo->hBmpSaved) {
                     87:         MessageBox(ghwndMain, "There's no Bitmap to change!", "Error", MB_OK);
                     88:         return FALSE;
                     89:     }
                     90: 
                     91:     if (!bValidMode(iMode)) {
                     92:         MessageBox(ghwndMain, "bChangeDIBColor: invalid mode!", "Error", MB_OK);
                     93:         return FALSE;
                     94:     }
                     95: 
                     96:     //
                     97:     // Let the graphics engine to retrieve the dimension of the bitmap for us
                     98:     // GetDIBits uses the size to determine if it's BITMAPCOREINFO or BITMAPINFO
                     99:     // if BitCount != 0, color table will be retrieved
                    100:     //
                    101:     bmi.bmiHeader.biSize = 0x28;              // GDI need this to work
                    102:     bmi.bmiHeader.biBitCount = 0;             // don't get the color table
                    103:     if ((GetDIBits(hDC, pInfo->hBmpSaved, 0, 0, (LPSTR)NULL, &bmi, DIB_RGB_COLORS)) == 0) {
                    104:         MessageBox(ghwndMain, "GetDIBits failed!", "Error", MB_OK);
                    105:         return FALSE;
                    106:     }
                    107: 
                    108:     //
                    109:     // Now that we know the size of the image, alloc enough memory to retrieve
                    110:     // the actual bits
                    111:     //
                    112:     if ((pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,
                    113:                 bmi.bmiHeader.biSizeImage)) == NULL) {
                    114:         MessageBox(ghwndMain, "Failed in Memory Allocation for pBits!", "Error", MB_OK);
                    115:         return FALSE;
                    116:     }
                    117: 
                    118:     //
                    119:     // Note: 24 bits per pixel has no color table.  So, we don't have to
                    120:     // allocate memory for retrieving that.  Otherwise, we do.
                    121:     //
                    122:     pbmi = &bmi;                                      // assume no color table
                    123:     if (bmi.bmiHeader.biBitCount != 24) {             // has color table
                    124:         sizBMI = sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(1<<bmi.bmiHeader.biBitCount);
                    125:         //
                    126:         // I need more memory for the color table
                    127:         //
                    128:         if ((pbmi = (PBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizBMI )) == NULL) {
                    129:             MessageBox(ghwndMain, "Failed in Memory Allocation for pbmi!", "Error", MB_OK);
                    130:             bSuccess = FALSE;
                    131:             goto ErrExit1;
                    132:         }
                    133: 
                    134:         //
                    135:         // Now that we've a bigger chunk of memory, let's copy the Bitmap
                    136:         // info header data over
                    137:         //
                    138:         pjTmp = (PBYTE)pbmi;
                    139:         pjTmpBmi = (PBYTE)&bmi;
                    140:         sizBMI = sizeof(BITMAPINFOHEADER);
                    141: 
                    142:         while(sizBMI--)
                    143:         {
                    144:             *(((PBYTE)pjTmp)++) = *((pjTmpBmi)++);
                    145:         }
                    146: 
                    147:     }
                    148: 
                    149:     //
                    150:     // Bitmap can't be selected into a DC when calling GetDIBits
                    151:     // Assume that the hDC is the DC where the bitmap would have been selected
                    152:     // if indeed it has been selected
                    153:     //
                    154:     if (hTmpBmp = CreateCompatibleBitmap(hDC, pbmi->bmiHeader.biWidth, pbmi->bmiHeader.biHeight)) {
                    155:         hBmpOld = SelectObject(hDC, hTmpBmp);
                    156:         if ((GetDIBits(hDC, pInfo->hBmpSaved, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS))==0){
                    157:             MessageBox(ghwndMain, "Failed in GetDIBits!", "Error", MB_OK);
                    158:             bSuccess = FALSE;
                    159:             goto ErrExit4;
                    160:         }
                    161:     } else {
                    162:         MessageBox(ghwndMain, "Failed in creating bitmap!", "Error", MB_OK);
                    163:         bSuccess = FALSE;
                    164:         goto ErrExit3;
                    165:     }
                    166: 
                    167:     bCycle = FALSE;
                    168:     switch (iMode) {
                    169:         case MM_BW:
                    170:             if ((!bColor2Mono(hDC, pInfo, pbmi)) ||
                    171:                 (pInfo->hBmpMono == NULL)) {
                    172:                 MessageBox(ghwndMain, "Failed in Color2Mono!", "Error", MB_OK);
                    173:                 bSuccess = FALSE;
                    174:                 goto ErrExit4;
                    175:             }
                    176:             pInfo->bUseMono = TRUE;
                    177:             break;
                    178:         case MM_SHIFT:
                    179:             if (!bColorShift(pbmi)) {
                    180:                 MessageBox(ghwndMain, "Failed in ColorShift!", "Error", MB_OK);
                    181:                 bSuccess = FALSE;
                    182:                 goto ErrExit4;
                    183:             }
                    184:             break;
                    185:         case MM_CUSTOM:
                    186:             bColorReplace(pbmi);
                    187:             break;
                    188: 
                    189:         case MM_CYCLE:
                    190:             bCycle = TRUE;
                    191:             break;
                    192: 
                    193:         default:
                    194:             bSuccess = FALSE;
                    195:             break;
                    196:     }
                    197: 
                    198:     if (SetDIBits(hDC, pInfo->hBmpSaved, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits,
                    199:             pbmi, DIB_RGB_COLORS) == 0) {
                    200:         MessageBox(ghwndMain, "Failed in SetDIBits!", "Error", MB_OK);
                    201:         bSuccess = FALSE;
                    202:         goto ErrExit4;
                    203:     }
                    204: 
                    205:     if (bCycle)
                    206:         pInfo->bSetDIBsToDevice = TRUE;
                    207: 
                    208:     if (pInfo->bSetDIBsToDevice && !pInfo->bUseMono) {
                    209:         //GetWindowRect(pInfo->hwnd, &rc);
                    210:         GetClientRect(pInfo->hwnd, &rc);
                    211:         //SetMapMode(hDC, MM_TEXT);
                    212: 
                    213:         if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top,
                    214:              0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_RGB_COLORS) == 0) {
                    215:             MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK);
                    216:             bSuccess = FALSE;
                    217:             goto ErrExit4;
                    218:         }
                    219:     } else {
                    220:         InvalidateRect(pInfo->hwnd, NULL, FALSE);
                    221: 
                    222:     }
                    223: 
                    224:     if (bCycle) {
                    225:         //SetSystemPaletteUse(hDC, SYSPAL_NOSTATIC);
                    226:         bColorCycle(hDC, rc, pBits, pbmi, pInfo);
                    227:         //SetSystemPaletteUse(hDC, SYSPAL_STATIC);
                    228:     }
                    229: 
                    230: ErrExit4:
                    231:     SelectObject(hDC, hBmpOld);
                    232:     DeleteObject(hTmpBmp);
                    233: ErrExit3:
                    234:     GlobalFree(pbmi);
                    235: ErrExit1:
                    236:     GlobalFree(pBits);
                    237:     return bSuccess;
                    238: }
                    239: 
                    240: 
                    241: /******************************Public*Routine******************************\
                    242: *
                    243: * bValidMode
                    244: *
                    245: * Effects: validates the change mode for ChangeDIBColor function
                    246: *          returns TRUE if mode is valid.  Otherwise, FALSE
                    247: *
                    248: * Warnings:
                    249: *
                    250: * History:
                    251: *  30-Jan-1992 -by- Petrus Wong
                    252: * Wrote it.
                    253: \**************************************************************************/
                    254: BOOL bValidMode(INT iMode)
                    255: {
                    256:     if ((iMode == MM_BW)     || (iMode == MM_SHIFT) ||
                    257:         (iMode == MM_CUSTOM) || (iMode == MM_CYCLE))
                    258:         return TRUE;
                    259:     else
                    260:         return FALSE;
                    261: }
                    262: 
                    263: /******************************Public*Routine******************************\
                    264: *
                    265: * bColor2BW
                    266: *
                    267: * Effects: changes the color table in the bitmapinfo to black and white
                    268: *
                    269: * Warnings: doesn't check if pbmi is valid or not
                    270: *
                    271: * History:
                    272: *  30-Jan-1992 -by- Petrus Wong
                    273: * Wrote it.
                    274: \**************************************************************************/
                    275: BOOL bColor2BW(PBITMAPINFO pbmi)
                    276: {
                    277:     INT iEntry;
                    278: 
                    279:     //
                    280:     // CR! What's the right behavior?  Maybe I should convert this to a
                    281:     // monochrome bitmap.  This will be very useful in MaskBlt().
                    282:     //
                    283:     iEntry = 1<<pbmi->bmiHeader.biBitCount;
                    284:     while (--iEntry >= 0) {
                    285:         if (iEntry <= iEntry / 2) {
                    286:                 pbmi->bmiColors[iEntry].rgbBlue     = 0x00;
                    287:                 pbmi->bmiColors[iEntry].rgbGreen    = 0x00;
                    288:                 pbmi->bmiColors[iEntry].rgbRed      = 0x00;
                    289:                 pbmi->bmiColors[iEntry].rgbReserved = 0x00;
                    290:          } else {
                    291:                 pbmi->bmiColors[iEntry].rgbBlue     = 0xFF;
                    292:                 pbmi->bmiColors[iEntry].rgbGreen    = 0xFF;
                    293:                 pbmi->bmiColors[iEntry].rgbRed      = 0xFF;
                    294:                 pbmi->bmiColors[iEntry].rgbReserved = 0x00;
                    295:         }
                    296:     }
                    297:     return TRUE;
                    298: }
                    299: 
                    300: 
                    301: /******************************Public*Routine******************************\
                    302: *
                    303: * bColorShift
                    304: *
                    305: * Effects: Shifting entries of the color table
                    306: *
                    307: * Warnings:
                    308: *
                    309: * History:
                    310: *  30-Jan-1992 -by- Petrus Wong
                    311: * Wrote it.
                    312: \**************************************************************************/
                    313: 
                    314: BOOL bColorShift(PBITMAPINFO pbmi)
                    315: {
                    316:     INT iEntry;
                    317:     RGBQUAD     Tmp;
                    318: 
                    319:     iEntry = 1<<pbmi->bmiHeader.biBitCount;
                    320: 
                    321:     Tmp.rgbBlue     = pbmi->bmiColors[iEntry-1].rgbBlue;
                    322:     Tmp.rgbGreen    = pbmi->bmiColors[iEntry-1].rgbGreen;
                    323:     Tmp.rgbRed      = pbmi->bmiColors[iEntry-1].rgbRed;
                    324:     Tmp.rgbReserved = pbmi->bmiColors[iEntry-1].rgbReserved;
                    325: 
                    326:     while (--iEntry > 0) {
                    327:          pbmi->bmiColors[iEntry].rgbBlue     =
                    328:             pbmi->bmiColors[iEntry-1].rgbBlue;
                    329:          pbmi->bmiColors[iEntry].rgbGreen    =
                    330:             pbmi->bmiColors[iEntry-1].rgbGreen;
                    331:          pbmi->bmiColors[iEntry].rgbRed      =
                    332:             pbmi->bmiColors[iEntry-1].rgbRed;
                    333:          pbmi->bmiColors[iEntry].rgbReserved =
                    334:             pbmi->bmiColors[iEntry-1].rgbReserved;
                    335:     }
                    336:     pbmi->bmiColors[0].rgbBlue     = Tmp.rgbBlue;
                    337:     pbmi->bmiColors[0].rgbGreen    = Tmp.rgbGreen;
                    338:     pbmi->bmiColors[0].rgbRed      = Tmp.rgbRed;
                    339:     pbmi->bmiColors[0].rgbReserved = Tmp.rgbReserved;
                    340:     return TRUE;
                    341: 
                    342: }
                    343: 
                    344: BOOL bColorReplace(PBITMAPINFO pbmi)
                    345: {
                    346:     return FALSE;
                    347:     UNREFERENCED_PARAMETER(pbmi);
                    348: }
                    349: 
                    350: /******************************Public*Routine******************************\
                    351: *
                    352: * bColorCycle
                    353: *
                    354: * Effects:  Create a logical palette with a good spread of color
                    355: *           Animate the palette, shift the palette entries and animate
                    356: *           again for 256 times altogether.
                    357: *           This creates the color cycling effect.
                    358: *
                    359: * Warnings: Only works in device that support palette.
                    360: *
                    361: * History:
                    362: *  31-May-1992      Petrus Wong     Check if drawing window is gone or not
                    363: *  24-Apr-1992 -by- Petrus Wong
                    364: * Wrote it.
                    365: \**************************************************************************/
                    366: 
                    367: BOOL bColorCycle(HDC hDC, RECT rc, PBYTE pBits, PBITMAPINFO pbmi, PINFO pInfo)
                    368: {
                    369:     INT             iEntry, i;
                    370:     PLOGPALETTE     plogPat;
                    371:     ULONG           ulSize;
                    372:     BOOL            bSuccess;
                    373:     HPALETTE        hPal, hPalOld;
                    374:     PALETTEENTRY    peTemp;
                    375:     BYTE            red, green, blue;
                    376: #ifdef CYCLETHRD
                    377:     DWORD           dwWait;
                    378: #endif
                    379:     MSG             msg;
                    380:     BOOL            bQuit;
                    381: 
                    382: UINT uRC;
                    383: 
                    384:     bSuccess = TRUE;
                    385:     iEntry = 1<<pbmi->bmiHeader.biBitCount;
                    386:     ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*256;
                    387: 
                    388:     if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) {
                    389:         MessageBox(ghwndMain, "Failed in Memory Allocation for plogPat!", "Error", MB_OK);
                    390:         bSuccess = FALSE;
                    391:         goto ErrExit1;
                    392:     }
                    393: 
                    394:     plogPat->palVersion = 0x300;
                    395:     plogPat->palNumEntries = (WORD) 256;
                    396: #if 0
                    397:     for (i = 0; i < iEntry; i++) {
                    398:         plogPat->palPalEntry[i].peBlue   = pbmi->bmiColors[i].rgbBlue;
                    399:         plogPat->palPalEntry[i].peGreen  = pbmi->bmiColors[i].rgbGreen;
                    400:         plogPat->palPalEntry[i].peRed    = pbmi->bmiColors[i].rgbRed;
                    401:         plogPat->palPalEntry[i].peFlags   = PC_RESERVED;
                    402:     }
                    403: #endif
                    404:     for (i = 0; i < 256; i++) {
                    405:         plogPat->palPalEntry[i].peBlue   = blue;
                    406:         plogPat->palPalEntry[i].peGreen  = green;
                    407:         plogPat->palPalEntry[i].peRed    = red;
                    408:         plogPat->palPalEntry[i].peFlags   = PC_RESERVED;
                    409: 
                    410:         if (!(red += 32))
                    411:             if (!(green += 32))
                    412:                 blue += 64;
                    413:     }
                    414: 
                    415:     if ((hPal = CreatePalette(plogPat)) == (HPALETTE) NULL) {
                    416:         MessageBox(ghwndMain, "Failed in creating palette!", "Error", MB_OK);
                    417:         bSuccess = FALSE;
                    418:         goto ErrExit2;
                    419:     }
                    420: 
                    421:     hPalOld = SelectPalette(hDC, hPal, FALSE);
                    422:     RealizePalette(hDC);
                    423: 
                    424: #ifndef CYCLETHRD
                    425: 
                    426:     bQuit = FALSE;
                    427:     while (TRUE && !bQuit) {
                    428: 
                    429:         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
                    430:             //sprintf( gtext,"msg = %lx\n", msg.message);
                    431:             //OutputDebugString( gtext );
                    432: 
                    433:             if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) ||
                    434:                 ((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) {
                    435:                 sprintf( gtext,"msg = %lx, wPram = %lx\n", msg.message, msg.wParam);
                    436:                 OutputDebugString( gtext );
                    437:                 bQuit = TRUE;
                    438:                 PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
                    439:             } else {
                    440:                 if (!TranslateAccelerator( ghwndMain, ghAccel, &msg) &&
                    441:                     !TranslateMDISysAccel(  ghwndClient, &msg)          ) {
                    442:                     TranslateMessage(&msg);
                    443:                     DispatchMessage(&msg);
                    444:                 }
                    445:            }
                    446:         }
                    447: 
                    448: #else
                    449:     bQuit = FALSE;
                    450:     while (TRUE) {
                    451:         //
                    452:         // If parent gets a WM_CLOSE, we will return
                    453:         //
                    454:         dwWait = WaitForSingleObject(pInfo->hQuitEvent, 0);
                    455:         if (dwWait == WAIT_TIMEOUT) {
                    456:             MessageBox(ghwndMain, "Cycle Thread Quitting!", "CYCLE Thread", MB_OK);
                    457:             break;
                    458:         }
                    459: #endif
                    460:         if (!pInfo->bClrCycle) {
                    461:             sprintf( gtext,"NOT to animate palette\n");
                    462:             OutputDebugString( gtext );
                    463:             bQuit = TRUE;
                    464:         }
                    465: 
                    466:         if (!bQuit) {
                    467:             sprintf( gtext,"About to animate palette\n");
                    468:             OutputDebugString( gtext );
                    469: 
                    470: 
                    471:             peTemp = plogPat->palPalEntry[0];
                    472:             for (i = 0; i < 255; i++) {
                    473:                 plogPat->palPalEntry[i] = plogPat->palPalEntry[i+1];
                    474:             }
                    475:             plogPat->palPalEntry[i] = peTemp;
                    476: 
                    477:             if (!AnimatePalette(hPal, 0, 256, plogPat->palPalEntry)) {
                    478:                 sprintf( gtext,"Anmiate palette failed\n");
                    479:                 OutputDebugString( gtext );
                    480:             }
                    481: 
                    482:             if ((uRC = RealizePalette(hDC)) == -1) {
                    483:                 sprintf( gtext,"Realize palette failed\n");
                    484:                 OutputDebugString( gtext );
                    485:             }
                    486:             sprintf( gtext,"Realize palette = %d\n", uRC);
                    487:             OutputDebugString( gtext );
                    488: 
                    489:             if (!UpdateColors(hDC)) {
                    490:                 sprintf( gtext,"Update Colors failed\n");
                    491:                 OutputDebugString( gtext );
                    492:             }
                    493: 
                    494:             if (SetDIBitsToDevice(hDC, 0, 0, rc.right-rc.left, rc.bottom-rc.top,
                    495:                  0, 0, 0, pbmi->bmiHeader.biHeight, (LPSTR)pBits, pbmi, DIB_PAL_COLORS) == 0) {
                    496:                 MessageBox(ghwndMain, "Failed in SetDIBitsToDevice!", "Error", MB_OK);
                    497:             }
                    498:         }
                    499: 
                    500:     }
                    501: 
1.1.1.2 ! root      502:     //SelectPalette(hDC, hPalOld, 0);
1.1       root      503:     DeleteObject(hPal);
                    504: ErrExit2:
                    505:     GlobalFree(plogPat);
                    506: ErrExit1:
                    507:     return bSuccess;
                    508: 
                    509: }
                    510: 
                    511: 
                    512: /******************************Public*Routine******************************\
                    513: *
                    514: * bCycle
                    515: *
                    516: * Effects: Wrapper for doing color cycling in a separate thread
                    517: *          Called from MM_CYCLE
                    518: *
                    519: * Warnings: presents problem when the MDI child is closed if this thread
                    520: *           is not done yet.  We will have to wait until DeleteDC is
                    521: *           fully functional.
                    522: *
                    523: * History:
                    524: *  28-May-1992 -by- Petrus Wong
                    525: * Wrote it.
                    526: \**************************************************************************/
                    527: BOOL bCycle(HWND hwnd)
                    528: {
                    529:     HDC     hDC;
                    530:     PINFO   pInfo;
                    531: 
                    532:     if ((pInfo = pGetInfoData(hwnd)) == NULL) {
                    533:         return 0L;
                    534:     }
                    535: 
                    536:     hDC = GetDC(pInfo->hwnd);
                    537:     bChangeDIBColor(hDC, pInfo, MM_CYCLE);
                    538:     ReleaseDC(pInfo->hwnd, hDC);
                    539: 
                    540:     bReleaseInfoData(hwnd);
                    541: 
                    542:     ExitThread(0);
                    543:     return TRUE;
                    544: }
                    545: 
                    546: 
                    547: /******************************Public*Routine******************************\
                    548: *
                    549: * bColor2Mono
                    550: *
                    551: * Effects: Create a monochrome bitmap out of the colored one
                    552: *          Using a different a source background clr every time it is called
                    553: *          Saving the Monochrome bitmap in pInfo
                    554: *
                    555: * Warnings: It's more straight forward to use GetDIBits than this but
                    556: *           that means some big changes to the bChangeDIBColor.
                    557: *
                    558: * History:
                    559: *  31-May-1992 -by- Petrus Wong
                    560: * Wrote it.
                    561: \**************************************************************************/
                    562: 
                    563: BOOL bColor2Mono(HDC hDC, PINFO pInfo, PBITMAPINFO pbmi) {
                    564:     HDC     hDCMemDest, hDCMemSrc;
                    565:     HBITMAP hOldBmpDest, hOldBmpSrc;
                    566:     INT     iWidth, iHeight, iEntry;
                    567:     static INT iCount=0;
                    568: 
                    569: 
                    570:     iWidth = pbmi->bmiHeader.biWidth;
                    571:     iHeight = pbmi->bmiHeader.biHeight;
                    572: 
                    573:     if (pInfo->hBmpMono)
                    574:         DeleteObject(pInfo->hBmpMono);
                    575: 
                    576:     pInfo->hBmpMono = CreateBitmap(iWidth, iHeight, pbmi->bmiHeader.biPlanes, 1, NULL);
                    577:     if (pInfo->hBmpMono == NULL) {
                    578:         return(FALSE);
                    579:     }
                    580: 
                    581:     hDCMemDest = CreateCompatibleDC(hDC);
                    582:     hDCMemSrc  = CreateCompatibleDC(hDC);
                    583:     iEntry = 1<<pbmi->bmiHeader.biBitCount;
                    584:     SetBkColor(hDCMemSrc, RGB( pbmi->bmiColors[iCount].rgbRed,
                    585:                                pbmi->bmiColors[iCount].rgbGreen,
                    586:                                pbmi->bmiColors[iCount].rgbBlue) );
                    587:     //SetBkColor(hDCMemDest, RGB( 0, 0, 0));
                    588:     //SetTextColor(hDCMemDest, RGB( 255, 255, 255));
                    589: 
                    590:     iCount++;
                    591:     if (iCount >= iEntry)
                    592:         iCount = 0;
                    593: 
                    594:     hOldBmpDest = SelectObject(hDCMemDest, pInfo->hBmpMono);
                    595:     hOldBmpSrc  = SelectObject(hDCMemSrc,  pInfo->hBmpSaved);
                    596:     BitBlt(hDCMemDest, 0, 0, iWidth, iHeight, hDCMemSrc, 0, 0, SRCCOPY);
                    597: 
                    598:     SelectObject(hDCMemDest, hOldBmpDest);
                    599:     SelectObject(hDCMemSrc, hOldBmpSrc);
                    600: 
                    601:     DeleteDC(hDCMemDest);
                    602:     DeleteDC(hDCMemSrc);
                    603: 
                    604:     return(TRUE);
                    605: 
                    606: }
                    607: 
                    608: 
                    609: /******************************Public*Routine******************************\
                    610: *
                    611: * iCreatePenFrPal
                    612: *
                    613: * Effects: Create an array of pens from palette.
                    614: *          If device supports palette, then first creates a logical palette
                    615: *          with a good spread of color. Then select the logical palette into
                    616: *          the DC. Create pen that corresponds to each palette entry.
                    617: *          If system does not support palette, then use the system palette.
                    618: *
                    619: *          prghPen pointer to an array of hPen
                    620: *                  If this is NULL, the required size of the array is
                    621: *                  returned. If this is not NULL, the array will be filled
                    622: *                  with hPens.
                    623: *
                    624: *          returns the number of hPens created.
                    625: *
                    626: * Warnings:
                    627: *
                    628: * History:
                    629: *  13-Jun-1992 -by- Petrus Wong
                    630: * Wrote it.
                    631: \**************************************************************************/
                    632: 
                    633: INT iCreatePenFrPal(HDC hDC, PVOID *prghPen)
                    634: {
                    635:     INT             iNumClr, iResult, i;
                    636:     PLOGPALETTE     plogPat;
                    637:     ULONG           ulSize;
                    638:     HPALETTE        hPal, hPalOld;
                    639:     BYTE            red, green, blue;
                    640: 
                    641:     iResult = 0;
                    642: 
                    643:     if (!((GetDeviceCaps(hDC, RASTERCAPS)) & RC_PALETTE)) {
                    644: 
                    645:         iNumClr = GetSystemPaletteEntries(hDC, 0, 0, NULL);
                    646:         if (prghPen == NULL) {
                    647:             return (iNumClr);
                    648:         }
                    649: 
                    650:         for (i = 0; i < iNumClr; i++) {
1.1.1.2 ! root      651:             prghPen[i] = (PVOID)CreatePen(PS_SOLID, 0, PALETTEINDEX(i));
1.1       root      652:             iResult = i;
                    653:         }
                    654: 
                    655:         return iResult;
                    656:     }
                    657: 
                    658:     iNumClr = GetDeviceCaps(hDC, NUMCOLORS);
                    659: 
                    660:     if (prghPen == NULL)
                    661:         return iNumClr;
                    662: 
                    663:     ulSize = sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*iNumClr;
                    664: 
                    665:     if ((plogPat = (PLOGPALETTE) GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, ulSize)) == NULL) {
                    666:         MessageBox(ghwndMain, "Failed in Memory Allocation for plogPat!", "Error", MB_OK);
                    667:         goto ErrExit1;
                    668:     }
                    669: 
                    670:     plogPat->palVersion = 0x300;
                    671:     plogPat->palNumEntries = (WORD) iNumClr;
                    672:     for (i = 0; i < iNumClr; i++) {
                    673:         plogPat->palPalEntry[i].peBlue   = blue;
                    674:         plogPat->palPalEntry[i].peGreen  = green;
                    675:         plogPat->palPalEntry[i].peRed    = red;
                    676:         plogPat->palPalEntry[i].peFlags   = PC_RESERVED;
                    677: 
                    678:         if (!(red += 32))
                    679:             if (!(green += 32))
                    680:                 blue += 64;
                    681:     }
                    682: 
                    683:     if ((hPal = CreatePalette(plogPat)) == (HPALETTE) NULL) {
                    684:         MessageBox(ghwndMain, "Failed in creating palette!", "Error", MB_OK);
                    685:         goto ErrExit2;
                    686:     }
                    687: 
                    688:     hPalOld = SelectPalette(hDC, hPal, FALSE);
                    689:     RealizePalette(hDC);
                    690: 
                    691:     for (i = 0; i < iNumClr; i++) {
1.1.1.2 ! root      692:         prghPen[i] = (PVOID)CreatePen(PS_SOLID, 0, PALETTEINDEX(i));
1.1       root      693:         iResult = i;
                    694:     }
                    695: 
                    696:     SelectPalette(hDC, hPalOld, 0);
                    697:     DeleteObject(hPal);
                    698: ErrExit2:
                    699:     GlobalFree(plogPat);
                    700: ErrExit1:
                    701:     return iResult;
                    702: 
                    703: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.