0

Please help me find out the bug in my code my resize.c does not print any error but after execution results into a file which is unsupported

/**
 * Copies a BMP piece by piece, just because.
 */

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

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

if(atoi(argv[1])<1 || atoi(argv[1])>100)
{
    fprintf(stderr, "Factor must be between 1 and 100\n");
}


// remember filenames
char *infile = argv[2];
char *outfile = argv[3];

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "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,bf2;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

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

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

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



// 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 padding1 = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

int padding2 = (4 - (atoi(argv[1])*bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;







bi2.biHeight=atoi(argv[1])*bi.biHeight;
bi2.biWidth=atoi(argv[1])*bi.biWidth;
bi2.biSizeImage=((sizeof(RGBTRIPLE)*bi2.biWidth)+padding2)*abs(bi2.biHeight);
bf2.bfSize=bi2.biSizeImage + sizeof(BITMAPINFOHEADER) +sizeof(BITMAPFILEHEADER);



// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    //iterate to resize the infile scanlines
    for(int k=0;k<atoi(argv[1]);k++)
    {
        // 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 while doing horizontal resizing
        fwrite(&triple, sizeof(RGBTRIPLE), atoi(argv[1]), outptr);



        }

        //moving back the pointer of the infile to the start of the scanfile
        fseek(inptr, -bi.biWidth*3, SEEK_CUR);

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


    }


    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
        fwrite(&triple, sizeof(RGBTRIPLE), atoi(argv[1]), outptr);

        }

    // skip over padding, if any
    fseek(inptr, padding1, SEEK_CUR);

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}
1

The output headers bi2 and bf2 are written out to the output file before they've even been initialized and before any of the various header fields have been changed. In short, no headers.

Have you used the peek and xxd tools to look at the input and output file contents?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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