Solved

Adjusting Brightness/contrast and RGB on DC

Posted on 1998-10-12
5
692 Views
Last Modified: 2013-11-20
In a view I display a bitmap, how can I adjust brightness
contrast and the RGB vales?  (like they do in paint programs)
0
Comment
Question by:dan_hed
[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
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:psdavis
ID: 1323209
You must either manipulate the palette (easiest) or manipulate the raw data itself.

If you want to manipulate the palette, look into the CPalette class.  Either way, it will also depend on the number of bits per pixel you are working in.  

Most likely, your paint programs are manipulating the image, and using RealizePalette to update the image's colors.  If you have more than 256 colors, you won't be able to use the palette though.

Phillip
0
 

Author Comment

by:dan_hed
ID: 1323210
I know I need use CPalette... HOW ?????
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1323211
// Here's mine.  The shadow and the background shorten the image to increase contrast and brightness...

void CMyCtrl::CreatePalette( CDC* pDC, short sShadow, short sBackground )
{
   // Now create the system identity palette
   
   struct
   {
      LOGPALETTE   lpPalette;
      PALETTEENTRY lpEntries[ 255 ];
   } pal;
   
   LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
   
   pLogPalette->palVersion    = 0x300;  
   pLogPalette->palNumEntries = 256;
   
   // Get the static colors from the system palette
   
   sShadow      = (short) max(  10, sShadow     );
   sBackground  = (short) min( 245, sBackground );
   
   // iNumberColors == 4096 if in 32K mode
   // iNumberColors ==   20 if in 256 mode                      
   
   int iNumberColors = pDC->GetDeviceCaps( NUMCOLORS );
   
   short sStaticColors = (short)( iNumberColors / 2 );
   
   if( iNumberColors > 20 )
      sStaticColors = 10;
   
   short sSizePalette  = (short)( min( sBackground - sShadow, 256 - ( sStaticColors * 2 )));
   short sUsableColors = (short)( 256 - sStaticColors );

   if( sSizePalette < 10 )
      sSizePalette = 10;
   
   GetSystemPaletteEntries( pDC->GetSafeHdc( ), 0,  sStaticColors, pLogPalette->palPalEntry );
   GetSystemPaletteEntries( pDC->GetSafeHdc( ), 256 - sStaticColors, sStaticColors, pLogPalette->palPalEntry + ( 256 - sStaticColors ));
   
   // Set the peFlags of the lower static colors to zero
   
   short sCounter;
   
   for( sCounter = 0; sCounter < sStaticColors; sCounter ++ )
      pLogPalette->palPalEntry[ sCounter ].peFlags = 0;
   
   // Fill in black spots until we reach our gray-palette          
   
   for( ; sCounter < sShadow; sCounter ++ )
   {  
      pLogPalette->palPalEntry[ sCounter ].peRed     = 0;
      pLogPalette->palPalEntry[ sCounter ].peGreen   = 0;
      pLogPalette->palPalEntry[ sCounter ].peBlue    = 0;
      pLogPalette->palPalEntry[ sCounter ].peFlags   = PC_RESERVED;
   }
   
   // Implement the gray-scale palette now
   
   float fColor     = 0.0f;
   float fIncrement = 255.0f / (float) sSizePalette;
   BYTE  byColor;
   
   for( ; sCounter <= sBackground; sCounter ++, fColor += fIncrement )
   {  
      byColor = (BYTE) fColor;
      pLogPalette->palPalEntry[ sCounter ].peRed     = byColor;
      pLogPalette->palPalEntry[ sCounter ].peGreen   = byColor;
      pLogPalette->palPalEntry[ sCounter ].peBlue    = byColor;
      pLogPalette->palPalEntry[ sCounter ].peFlags   = PC_RESERVED;
   }
   
   // Fill in the rest of the palette with white until we reach the end
   
   for( ; sCounter < sUsableColors; sCounter ++ )
   {  
      pLogPalette->palPalEntry[ sCounter ].peRed     = 255;
      pLogPalette->palPalEntry[ sCounter ].peGreen   = 255;
      pLogPalette->palPalEntry[ sCounter ].peBlue    = 255;
      pLogPalette->palPalEntry[ sCounter ].peFlags   = PC_RESERVED;
   }
   
   // Set the peFlags of the upper static colors to zero
   
   for( sCounter = (short)( 256 - sStaticColors ); sCounter < 256; sCounter ++ )
      pLogPalette->palPalEntry[ sCounter ].peFlags = 0;
   
   if( iNumberColors <= 20 )
   {
      if( m_pGrayPalette.m_hObject == NULL )
         m_pGrayPalette.CreatePalette( pLogPalette );
      else
         m_pGrayPalette.SetPaletteEntries( 10, 245, pLogPalette->palPalEntry );
   }
   
   m_bInitPalette = true;
}

Good Luck
Phillip
0
 

Author Comment

by:dan_hed
ID: 1323212
I tried:
CPalete m_pGrayPalette;
pDC->SelectObject(&m_pGrayPalette);
And some variations...... nothing happens!!!!
0
 
LVL 7

Accepted Solution

by:
psdavis earned 200 total points
ID: 1323213
// Don't forget to override the OnQueryNewPalette & OnPaletteChanged.  

int CAfixCtrl::OnQueryNewPalette()
{
   CClientDC dc( this );
   
   CPalette* pOldPalette = dc.SelectPalette( &m_pGrayPalette, false );
   
   UINT uiCount = dc.RealizePalette( );
   
   if( uiCount > 0 )
      Invalidate( );
   
   dc.SelectPalette( pOldPalette, false );
   
   return uiCount;
}

void CAfixCtrl::OnPaletteChanged( CWnd* pFocusWnd )
{
// COleControl::OnPaletteChanged( pFocusWnd );
   
   if( pFocusWnd != this )
   {
      CClientDC dc( this );
      CPalette* pOldPalette = dc.SelectPalette( &m_pGrayPalette, false );
     
      if( dc.RealizePalette( ))
         Invalidate( );
     
      dc.SelectPalette( pOldPalette, false );
   }
   
}

p.s.  Don't feel bad if this isn't working the way it should.  I've been doing them for a while and I'm still lost!
Phillip
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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