Solved

Convering Colored Bitmap (palette mode) to Monochrom Bitmap

Posted on 1998-11-15
5
356 Views
Last Modified: 2013-12-03
I tried to convert 256 colored bitmap to monochrom bitmap.
I used the SetBkColor to set which color goes to white,
but in the result, more colors went to white, not only the specified in the SetBkColor.
Thanks.
0
Comment
Question by:bugsi
5 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 1416075
There are some ways. What do you want to convert - file, bitmap object (HBITMAP), device-independed bitmap (DIBSECTION) ?
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1416076
And which way you trying to do this?
0
 
LVL 2

Expert Comment

by:cramer091498
ID: 1416077
Have a look at
http://www.codeguru.com/bitmap/index.shtml

This has code to alter the bitmap palette to make it grayscale
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 50 total points
ID: 1416078
Hi, i send you main Programm, that create palette
for Transformation, but function YouCondition: you choice:
you must decide, wich color must goto BLACK, wich: to WHITE:
..
HANDLE hImage = LoadImage (NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0,
            LR_LOADFROMFILE | LR_CREATEDIBSECTION);
/////////////////////////////////////////////////////////////////////////////
// Palette generation routines and helpers

HPALETTE CreateExactPalette (HANDLE hImage)
{
    DIBSECTION ds;
    UINT nColors, i;
    RGBQUAD* prgb;
    LOGPALETTE* plp;
    HDC hdc, hdcMem;
    HBITMAP hOldBitmap;
    HPALETTE hPalette;
    DWORD dwSize;

    // Get the number of colors in the image
    GetObject (hImage, sizeof (ds), &ds);
    if (ds.dsBmih.biClrUsed != 0)
        nColors = ds.dsBmih.biClrUsed;
    else
        nColors = 1 << ds.dsBmih.biBitCount;

    if (nColors > 256) // Sanity check
        return NULL;

    // Retrieve the image's color table
    if ((prgb = (RGBQUAD*) HeapAlloc (GetProcessHeap (), 0,
        nColors * sizeof (RGBQUAD))) == NULL)
        return NULL;

    hdc = GetDC (NULL);
    hdcMem = CreateCompatibleDC (hdc);
    hOldBitmap = SelectObject (hdcMem, hImage);
    nColors = min (nColors, GetDIBColorTable (hdcMem, 0, nColors, prgb));
    SelectObject (hdcMem, hOldBitmap);
    DeleteDC (hdcMem);
    ReleaseDC (NULL, hdc);

    if (nColors == 0) { // Another sanity check
        HeapFree (GetProcessHeap (), 0, prgb);
        return NULL;
    }

    // Create a logical palette from the colors in the color table
    dwSize = sizeof (LOGPALETTE) + ((nColors - 1) * sizeof (PALETTEENTRY));
    if ((plp = (LOGPALETTE*) HeapAlloc (GetProcessHeap (), 0,
        dwSize)) == NULL) {
        HeapFree (GetProcessHeap (), 0, prgb);
        return NULL;
    }

    plp->palVersion = 0x300;
    plp->palNumEntries = (WORD) nColors;
                  //it is main place!!!!!
    for (i=0; i<nColors; i++) {
       if (YouConditon(prgb[i])
         prgb[i].rgbRed = prgb[i].rgbGreen = prgb[i].rgbBlue = 255;
       else
         prgb[i].rgbRed = prgb[i].rgbGreen = prgb[i].rgbBlue = 0;
    }

    for (i=0; i<nColors; i++) {
        plp->palPalEntry[i].peRed   = prgb[i].rgbRed;
        plp->palPalEntry[i].peGreen = prgb[i].rgbGreen;
        plp->palPalEntry[i].peBlue  = prgb[i].rgbBlue;
        plp->palPalEntry[i].peFlags = 0;
    }

    hPalette = CreatePalette (plp);

    HeapFree (GetProcessHeap (), 0, plp);
    HeapFree (GetProcessHeap (), 0, prgb);
    return hPalette;
}
.
After that you must Realize and Actice Palette as

void DisplayDIBSection (HDC hdc, HANDLE hImage, HPALETTE hPalette)
{
    HDC hdcMem;
    HBITMAP hOldBitmap;
    DIBSECTION ds;
    int cx, cy;

    hdcMem = CreateCompatibleDC (hdc);
    if (hPalette != NULL) {
        SelectPalette (hdcMem, hPalette, FALSE);
        RealizePalette (hdcMem);
        SelectPalette (hdc, hPalette, FALSE);
        RealizePalette (hdc);
    }

    GetObject (g_hImage, sizeof (ds), &ds);
    cx = ds.dsBmih.biWidth;
    cy = ds.dsBmih.biHeight;

    hOldBitmap = SelectObject (hdcMem, hImage);
    BitBlt (hdc, 0, 0, cx, cy, hdcMem, 0, 0, SRCCOPY);

    SelectObject (hdcMem, hOldBitmap);
    DeleteDC (hdcMem);
}
Regards, Alex
0
 

Author Comment

by:bugsi
ID: 1416079
Thank you. I will check it but it seems to be what I need.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now