0

I'm not sure where I went wrong that is causing it to add too many bytes. When I test the program, it seems to resize everything correctly.

Code:

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

#include "bmp.h"

int main(int argc, char* argv[])
{


// ensure proper usage
if (argc != 4)
{
    printf("Usage: copy infile outfile\n");
    return 1;
}

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

//make sure n <=100
if (n>100 || n<1 ) {
    printf("value outside bounds (1-100\n)");
    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;
}


BITMAPINFOHEADER new_bi = bi;


//update width and height
new_bi.biWidth = new_bi.biWidth * n;
new_bi.biHeight = new_bi.biHeight * n;
// determine newPadding for scanlines
int newPadding =  (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int oldPadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//new sizeImage
new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + newPadding)* abs(new_bi.biHeight);

//new bfSize
bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) +
                                sizeof(BITMAPINFOHEADER);

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

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(new_bi.biHeight); i < biHeight; i++)
{
    int counter = 0;
    //scanline
    RGBTRIPLE scanLine[(new_bi.biWidth)];


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

        for (int k = 0; k < n; k++)
        {
            scanLine[counter] = triple;
            counter++;

        }

    }


    for (int l = 0; l < n; l++)
    {
        // write scanline
         fwrite(&scanLine[0], sizeof(RGBTRIPLE), (new_bi.biWidth), outptr);

         for (int m = 0; m < newPadding; m++)
         {
             fputc(0x00, outptr);
         }
    }

    // skip over Padding, if any
    fseek(inptr, oldPadding, SEEK_CUR);

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

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

Your outer for loop needs to use the input height, not output height.

1
  • Thank you! Fixed it.
    – Jgoren
    Mar 6 '19 at 22:12

You must log in to answer this question.

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