Thread: [C++/WinAPI] Changing bitmap contrast

  1. #1
    Registered User jagi's Avatar
    Join Date
    Mar 2005
    Location
    Poland
    Posts
    21

    [C++/WinAPI] Changing bitmap contrast

    It's a code of function changing kontrast of bitmap. But it don't work that I want. Maybe someone saw how loooks changing contrast in PhotoShop. I want to reach the same effect... In attaching image I show how it works in PS (right) in my function (center). On the left is source image... Any ideas how to reach effect like in PhotoShop ?

    Code:
    #include <windows.h>
    #include "epfEngine.h"
    #include <complex>
    
    static unsigned char TransLut[256];
    
    void build_lookup_table ( int contr )
    {
    	float step,step_value;
    
    	for (int i=0; i < 256; i++)
    		TransLut[i] = i;
    
    	if (contr > 0)
    	{
    		unsigned int MinBin = contr;
    		unsigned int MaxBin = 255 - contr;
    
    		step = sqrt((double)contr)/contr;
    		step_value = 0.0;
    
    		for ( i = 0; i < MinBin; i++)
    		{
    			TransLut[i] = (unsigned char)step_value;
    			step_value += step;
    		}
    
    		step = 256.0f / (float)(MaxBin-MinBin);
    
    		for ( i = MinBin; i <= MaxBin; i++)
    		{
    
    		if (step_value > 255.0f)
    		{
    			step_value = 255.0f;
    			step = 0.0f;
    		}
    
    		TransLut[i] = (unsigned char)step_value;
    		step_value += step;
    		}
    
    		for ( i = MaxBin + 1; i < 256; i++)
    			TransLut[i] = 255;
    
    	}
    	else
    	{
    		if (contr<0)
    		{
    			step = (256.0+(float)(contr*2))/256.0;
    			step_value = (float)contr * -1.0;
    
    			for (i = 0;i < 256; i++)
    			{
    				TransLut[i] = (unsigned char)step_value;
    				step_value += step;
    			}
    		}
    	}
    }
    
    BOOL epfChangeBitmapContrast(HBITMAP * phBitmap, int iCount)
    {
    	BITMAP    bm;
    	BYTE    * pBits;
    	RGBQUAD * pRgb;
    	WORD      wByteCount;
    	int       i, iPixels, gray;
    
    	build_lookup_table(iCount);
    
    	// Take BITMAP structure from HBITMAP
    
    	GetObject(*phBitmap, sizeof(BITMAP), &bm);
    
    	// Calculate bytes to read
    
    	wByteCount = bm.bmHeight * (2 * ((bm.bmWidth * bm.bmBitsPixel + 15) / 16));
    
    	// Alocate momory for bits od pixels and get pointers
    
    	pBits = (BYTE *) malloc(wByteCount);
    	GetBitmapBits(*phBitmap, wByteCount, pBits);
    
    	// Convert pointer to byte to pointer to RGBQUAD
    
    	pRgb = (RGBQUAD *) pBits;
    
    	// Operate on pixel's colors
    
    	iPixels = wByteCount / (bm.bmBitsPixel / 8);
    
    	for(i = 0; i < iPixels; i++, pRgb++)
    	{
    		gray = (pRgb->rgbRed + pRgb->rgbGreen + pRgb->rgbBlue) / 3;
    		int k = TransLut[gray]-gray;
    
    		pRgb->rgbRed   = min(pRgb->rgbRed   + k, 255);
    		pRgb->rgbGreen = min(pRgb->rgbGreen + k, 255);
    		pRgb->rgbBlue  = min(pRgb->rgbBlue  + k, 255);
    	}
    
    	SetBitmapBits(*phBitmap, wByteCount, pBits);
    
    	return TRUE;
    }
    Last edited by jagi; 03-28-2005 at 02:10 AM.
    [I'm from Poland]
    [Sorry for my English, because I just learn]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM
  5. texture is all white in opengl!
    By Crossbow in forum Game Programming
    Replies: 7
    Last Post: 03-31-2002, 11:54 AM