Thread: Prob with padding in BMP image files

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    12

    Unhappy Prob with padding in BMP image files

    Hi!

    I've posted earlier about reading and writing to bitmap image files and I've been able to successfully work with the 32x32 bitmap files that I was interested in.

    Now, I want to deal with different sizes and ran into an issue with the padding from bitmap files.

    I created a 33x32 image using MS Paint and tried to read it. I can't seem to get the right image... it seems distorted and I'm unable to figure out where the bug is. Any idea on what I need to fix, people?

    Best Regards,
    Tin

    PS: I could post the whole code, if anyone wants that... I've traced the error to this section so I've only posted this bit.


    This code fragment reads the file and removes the padding.

    Code:
    	//Code fragment from trying to read a 33x32 image in 24-bit BMP format
    	//The approach works perfectly when there's no padding... I tried 32x32 and some other sizes with no padding
    	//I did use a #pragma pack(1)
    	
    	struct rgbQuad {
    		unsigned char blue;
    		unsigned char green;
    		unsigned char red;
    		unsigned char reserved;	
    	};
    	
    	
    	struct rgbQuad* bmpImgData2;
    	void* tempImgData;
    
    
    
    
    	padding = (width*3) % 4;
    	tempImgData = malloc((3*width*height)+(padding*height));
    
    //the header has previously been removed so the reading is fine too.
    //I also tried a rewind and fseek to the offset from the header - I get the same result
    	fread((void*) tempImgData, (3*width*height), 1, file1);
    
    	int n=0;
    	bmpImgData2 = (struct rgbQuad*) malloc(3*width*height);
    	char* tempDestPtr = (char*) bmpImgData2;
    	char* tempSrcPtr = (char*) tempImgData;
    
    	for (int p=0; p<height; p++) {
    			
    		memcpy(tempDestPtr, tempSrcPtr, sizeof(struct rgbQuad)*width);
    		printf("(R: %u G: %u B: %u)\n", ((struct rgbQuad*) tempDestPtr)->red, ((struct rgbQuad*) tempDestPtr)->green, ((struct rgbQuad*) tempDestPtr)->blue);
    		tempDestPtr += (sizeof(struct rgbQuad)*width);
    		tempSrcPtr += (sizeof(struct rgbQuad)*width+padding);
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > tempImgData = malloc((3*width*height)+(padding*height));
    How about
    tempImgData = malloc((3*(width+padding)*height));

    > fread((void*) tempImgData, (3*width*height), 1, file1);
    And also reading that amount of data as well.

    > tempSrcPtr += (sizeof(struct rgbQuad)*width+padding);
    More operator precedence
    tempSrcPtr += (sizeof(struct rgbQuad)*(width+padding));

    In fact, create another variable called paddedWidth

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't remember how I did this exactly but I think it's:

    Code:
    int iPadding=bmpInfo.bmWidth % 4;
    
    
    ...
    Then while displaying the bitmap you can check for the padding or you can load the bitmap and discard the padding thus having a normal bitmap in memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I can't make bmp image files show up in an SDL window
    By Lucas89 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2009, 01:04 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Help! (serial number, image files)
    By param in forum C Programming
    Replies: 1
    Last Post: 01-18-2003, 03:54 PM
  4. How to change a mouse cursor in console mode
    By GaPe in forum Windows Programming
    Replies: 10
    Last Post: 07-03-2002, 07:42 AM
  5. *question* Reading Image Files
    By madsmile in forum C Programming
    Replies: 5
    Last Post: 03-05-2002, 12:57 PM