Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello and Merry Christmas ! I've made a program that overwrites all the colors from a 24bit bitmap (except for white) with a given one (it is said that the background is white) . The problem is that i can't remove the padding . I've read bunch of tutorials and questions and still can't manage to solve the problem . I'm asked to overwrite the padding with 0 (I don't know why , in my opinion it will make the padding black) but i'm getting in the upper-right corner always a black row of one third the size of the width..

This is the input image
http://i.stack.imgur.com/jAsW6.png[^]
This is the desired output
http://i.stack.imgur.com/aAgix.png[^]
And this is my result
http://i.stack.imgur.com/yNvqq.png[^]

Now i've seen that not even the color is right..
And here is the code :

C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp_header.h"

int main(void)
{
    FILE *f;
    f = fopen("captcha.bmp","rb");
    fread(&BMP_header,sizeof(BMP_header),1,f);
    fread(&BMP_info_header,sizeof(BMP_info_header),1,f);
    fseek(f,BMP_header.imageDataOffset,SEEK_SET);
    int rowSize = (BMP_info_header.bitPix * BMP_info_header.width + 31 ) /32 * 4 ;
    unsigned char *PixelArray = (unsigned char *)malloc(rowSize * abs(BMP_info_header.height));
    fread(PixelArray,sizeof(char),BMP_info_header.biSizeImage,f);
    int i,j,k,addPadding;
    int padding = 4-((BMP_info_header.width*3)%4);

    for( i = 0; i<abs(BMP_info_header.height); i++)

    {
                   for(j = 0 ; j<rowSize-padding; j+= 3)
                  {
                        int offset = i * (rowSize-padding) + j;
                        if((PixelArray[offset] != 255 ) && (PixelArray[offset+1] != 255) && (PixelArray[offset+2] != 255))
                       {    //it is asked to use this RGB value (69,106,671)
                            PixelArray[offset] = 69;
                            PixelArray[offset+1] = 106;
                            PixelArray[offset+2] = 671;
                       }
                  }

                  for (addPadding = 0 ; addPadding < padding ; addPadding ++)

                        PixelArray[ i * (rowSize+padding) + j  ] = 255;

    }



    f = fopen("result.bmp","wb");
    fwrite(&BMP_header,sizeof(BMP_header),1,f);
    fwrite(&BMP_info_header,sizeof(BMP_info_header),1,f);
    fwrite(PixelArray,rowSize * abs(BMP_info_header.height),1,f);
    fclose(f);
    free(PixelArray);


    return 0;
}


Here is "bmp_header.h" 's content :
C++
#pragma pack(1)

struct bmp_fileheader
{
    unsigned char  fileMarker1; /* 'B' */
    unsigned char  fileMarker2; /* 'M' */
    unsigned int   bfSize; /* File's size */
    unsigned short unused1;
    unsigned short unused2;
    unsigned int   imageDataOffset; /* Offset to the start of image data */
}BMP_header,BMP_header_out;

struct bmp_infoheader
{
    unsigned int   biSize; /* Size of the info header - 40 bytes */
    signed int     width; /* Width of the image */
    signed int     height; /* Height of the image */
    unsigned short planes;
    unsigned short bitPix;
    unsigned int   biCompression;
    unsigned int   biSizeImage; /* Size of the image data */
    int            biXPelsPerMeter;
    int            biYPelsPerMeter;
    unsigned int   biClrUsed;
    unsigned int   biClrImportant;
}BMP_info_header,BMP_info_header_out;

#pragma pack()

Any help would be very much appreciated :)
Posted
Comments
[no name] 26-Dec-15 8:30am    
Can you please supply the source Image as bmp?
Member 12229315 26-Dec-15 10:54am    
Yes , here http://www.filedropper.com/captcha9
KarstenK 30-Dec-15 12:14pm    
the rowsize in the data is WITH padding. Do not subtract it.

Tip: use a simple rectangular test bmp to find your bug. ;-)

1 solution

your main and obvious bug is a typo in the inner loop:
C++
if((PixelArray[offset] != 255 ) && (PixelArray[offset+1] != 255) && (PixelArray[offset+2] != 255))
{    //it is asked to use this RGB value (69,106,671) // 671 WTF???
    PixelArray[offset] = 69;
    PixelArray[offset+1] = 106;
    PixelArray[offset+2] = 61;//AND NOT 671 it is ONLY a bit!!!
}

The black belt on the top I cant reproduce, but I think it is some problem in writing.

You must take care on the bit depth of your source bmp. Depending you have a 24 bit or 32 bit (with alpha channel) bitmap your code and the padding may fail.

By the way: you must close the input file before writing
C++
fclose(f);
f = fopen("result.bmp","wb");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900