Solved

Adjusting Brightness/contrast and RGB on DC

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 50
Window placement 17 82
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 253
Excel file not created as expected 7 72
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‚Ķ
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

773 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