Thread: [C++/WinAPI] Resizing image

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

    [C++/WinAPI] Resizing image

    Hi. I'm trying to write function to resizing images. Below is function which do that but only in x axis and scale = 50%, 25%, 12,5% etc. I need function resizing image in x and y axis with any scale. How to do that ? Thank for any help...

    RGBA is my own structure
    pBits - pointer to (oryginal) image's bits
    pBitsNew - pointer to (resized) image's bits

    Code:
    BOOL epfBitmap::SetSize (int xSize, int ySize)
    {
    	BYTE * pBitsNew ;
    
    	// Calculate bytes to allocate
    
    	int iByteCount = ySize * (((xSize * iBitsPixel + 15) & ~15) >> 3) ;
    
    	// Alocate momory for bits of new bitmap
    
    	pBitsNew = (BYTE *) malloc (iByteCount) ;
    
    	if (pBitsNew == NULL)
    	{
    		MessageBox (NULL, TEXT("Can't allocate memory!"), NULL, MB_OK | MB_ICONERROR) ;
    		return FALSE ;
    	}
    
    	// Create RGBA pointers
    
    	RGBA * pRGBANew = (RGBA *) pBitsNew ;
    	RGBA * pRGBA    = (RGBA *) pBits ;
    
    	// Resizing bitmap
    
    	float fxScale = (float) cx / xSize ;
    	float fyScale = (float) cy / ySize ;
    
    	float fxScale2 = fxScale ;
    	float fyScale2 = fyScale ;
    
    	int iSumRed   = 0 ;
    	int iSumGreen = 0 ;
    	int iSumBlue  = 0 ;
    	int iSumAlpha = 0 ;
    
    	for (int y = 0; y < cy; y++)
    	{
    		for (int x = 0; x < cx; x++, pRGBA++)
    		{
    			if(fxScale2 <= 0)
    			{
    				fxScale2 = fxScale ;
    
    				pRGBANew->rgbRed   = (int) ((float)iSumRed   / fxScale) ;
    				pRGBANew->rgbGreen = (int) ((float)iSumGreen / fxScale) ;
    				pRGBANew->rgbBlue  = (int) ((float)iSumBlue  / fxScale) ;
    				pRGBANew->rgbAlpha = (int) ((float)iSumAlpha / fxScale) ;
    
    				iSumRed   = 0 ;
    				iSumGreen = 0 ;
    				iSumBlue  = 0 ;
    				iSumAlpha = 0 ;
    
    				pRGBANew++ ;
    			}
    			
    			fxScale2-- ;
    			iSumRed   += pRGBA->rgbRed ;
    			iSumGreen += pRGBA->rgbGreen ;
    			iSumBlue  += pRGBA->rgbBlue ;
    			iSumAlpha += pRGBA->rgbAlpha ;
    		}
    	}
    
    	cx = xSize ;
    	cy = ySize ;
    
    	pBits = (BYTE *) realloc (pBits, iByteCount) ;
    
    	pBits = pBitsNew ;
    
    	return TRUE ;
    }
    [I'm from Poland]
    [Sorry for my English, because I just learn]

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Depending on how you want to use the image...

    You could create a compatible DC and compatible bitmap then StretchBlt() the image on to the new bitmap. This function is slow and the results are not 100% satisfactory (unless using int mult of original size).

    Then use GetDIBits() to get the new image's data.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User jagi's Avatar
    Join Date
    Mar 2005
    Location
    Poland
    Posts
    21
    I know that but isn't good. I must do it myself
    [I'm from Poland]
    [Sorry for my English, because I just learn]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. image resizing algorithms.....
    By bhupesh.kec in forum C Programming
    Replies: 4
    Last Post: 09-18-2008, 01:20 PM
  3. image resizing
    By eth0 in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2004, 01:08 AM
  4. Resizing an Image to Cover Top
    By Speedy5 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 06-11-2003, 01:34 PM
  5. Image resizing
    By Jock in forum Linux Programming
    Replies: 3
    Last Post: 02-20-2002, 01:48 PM