0

Alright. This code is performing VERY spastically- sometimes passing peek, never passing check50, not always changing height and width properly, and never changing coloration properly. I know it has a LOT of problems based upon its inconsistency, but can someone point a few out to get me started in the right direction? Thanks in advance.

/**
* resize.c
*
* Computer Science 50
* Problem Set 4
*
* Resizes a bmp.
*/

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

 #include "bmp.h"

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

// remember filenames
int user = atoi(argv[1]);
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);

// 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;
}
int newwidth = bi.biWidth * user;
int newheight = bi.biHeight * user;
int newpadding =  (4 - (newwidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding;
bi.biHeight = newheight + newpadding;
bi.biWidth = newwidth;
bi.biSizeImage = (bi.biWidth * sizeof (RGBTRIPLE) + newpadding) * abs(bi.biHeight);
bf.bfSize = (bi.biSizeImage) + 54;
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
 // iterate over infile's scanlines
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// determine padding for scanlines
padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// 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
        for (int z = 0; z < user; z++)
        {
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }
    }

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

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


// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
1
  • Looks like you are only resizing it horizontally and not vertically. You should use the user variable in at least one more for Loop In order to resize vertically. Also you need fseek to help you do that so that the cursor can go back to the beginning of the line. I think there is the issue of output padding as well which needs to be calculated separately if I'm not mistaken.
    – Evan
    Jun 17 '16 at 20:40
0

I have finished the problem, and I am not going to go into detail here about what I did, (it was a LONG day of bug fixing), just posting an answer so I can accept the answer to keep up on forum maintenance.

You must log in to answer this question.

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