Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adjusting Brightness/contrast and RGB on DC

Posted on 1998-10-12
5
Medium Priority
?
694 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 400 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
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…

886 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