[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Convering Colored Bitmap (palette mode) to Monochrom Bitmap

Posted on 1998-11-15
5
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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