0

I've been struggling with this code for a while and cannot find a solution on my own or anywhere. Could anyone possibly explain what is wrong with my code or give me some sort of hint? I'm dying here lol. The image will resize horizontally just fine but I can't get it to resize vertically.

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

int scale = atoi(argv[1]);
if (scale < 0 || scale > 100)
{
    printf("positive ints only!\n");
    return 1;
}

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

// 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);

//stores original sizes
int infPad = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
/*int infSize = bf.bfSize;
int infSizeImage = bi.biSizeImage;*/
int infWidth = bi.biWidth;
int infHeight = bi.biHeight;

// 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;
}

// redefine width and height
bi.biWidth *= scale;
bi.biHeight *= scale;

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

// redefine image size and file size
bi.biSizeImage = abs(bi.biHeight) * ((bi.biWidth * sizeof (RGBTRIPLE)) + padding); 
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(infHeight); i < biHeight; i++)
{

        for (int r = 0; r < scale; r++)
        {
            // iterate over pixels in scanline
            for (int j = 0; j < infWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

                // write RGB triple to outfile

                for (int h = 0; h < scale; h++)
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }


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

            // return to beginning of scanline scale number of times
            if (r < scale)
                fseek (inptr, - infPad + (infWidth * sizeof (RGBTRIPLE)), SEEK_CUR);
        }

}
// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
1

There are a couple of problems. First, there's an issue with how you are moving around in the input file. At the end of each line, you need to skip over the input file's padding so that the pointer is properly positioned to go back to the beginning of the line to reprocess it, or to move to the next line and start working on it.

Second, there's an off by 1 error in your reprocessing of each line. The code REprocesses each line scale times. The problem is that you forget to account for the first time, so each line is being processed once too many times.

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

1
  • Thank you so much! I skipped over the padding and changed the if statement so that it reprocesses the correct number of times. After that, it still didn't work as expected and I was extremely worried until I realized the problem was simply the placement of some parentheses. Ugh. Anyways, thanks for your help :)
    – Daniella
    Sep 8 '16 at 23:38

You must log in to answer this question.

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