0

resize.c compiles and runs, but produces an image with a lot of black space. A fresh set of eyes on my code is welcome to see where I have gone wrong. I assume it has to do with padding but I can't figure it out.

int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage: ./copy infile outfile\n"); return 1; }

// remember filenames
//convert scale to int
int n = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];

if(n < 1)
{
    printf("A number greater than 0 must be used.");
    return 1;    
}



// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", infile);
    return 2;
}

// open output file
FILE* outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 3;
}

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
    bi.biBitCount != 24 || bi.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

// determine padding for scanlines
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;


//update outfile's file header and infoheader
bi.biWidth = bi.biWidth*n;
bi.biHeight = bi.biHeight*n;

int newpadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biSizeImage = (bi.biWidth+newpadding)*(bi.biHeight)*(sizeof(RGBTRIPLE));
bf.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bi.biSizeImage;



// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

//space for info header and file header
int allowance = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        // write RGB triple to outfile n times
        for(int k = 0; k < n; k++)
        {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }

    // skip over ALLOWANCE, if any
    fseek(inptr, padding+allowance, SEEK_CUR);


    // then add it back (to demonstrate how)
    for (int l = 0; l < newpadding; l++)
    {
        fputc(0x00, outptr);
    }


}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

1 Answer 1

0

You need to keep track of infile's oldwidth and oldheight. And read - iterate over infile with those values.

4
  • Why? If I iterate over those wouldn't I get the same sized image?
    – Kim
    Commented Nov 27, 2014 at 14:25
  • No, iterate over infile with own values and write out file with new - multiplied values. Commented Nov 28, 2014 at 21:30
  • You use the calculation bi.biSizeImage = (bi.biWidth+newpadding)*(bi.biHeight)*(sizeof(RGBTRIPLE)); I used something like bi.biSizeImage = (bi.biWidth*(sizeof(RGBTRIPLE))+newpadding)*(bi.biHeight); I don't multiply sizeof(RGBTRIPLE) by new padding because newpadding is already in bytes. Commented Nov 30, 2014 at 2:53
  • Also, should you be changing the size of bf.bfsize? I thought that was the size of the header which did not change. Commented Nov 30, 2014 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .