Solved

Adjusting Brightness/contrast and RGB on DC

Posted on 1998-10-12
5
686 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 78
ODBC Connection Logging, ADO.NET 6 49
notReplace  challenge 53 102
either24  challenge 19 84
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
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…

759 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