Solved

Adjusting Brightness/contrast and RGB on DC

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 46
if loop in java 3 163
noX challenge 17 134
wordcount challenge 11 146
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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