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

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

unix.superglobalmegacorp.com

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