Solved

Convering Colored Bitmap (palette mode) to Monochrom Bitmap

Posted on 1998-11-15
5
359 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

863 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

24 Experts available now in Live!

Get 1:1 Help Now