0

Have been trying to figure out what's wrong with my code, but having issues with it. Can someone please point me in the right direction?

What I'm getting with check50

:) resize.c and bmp.h exist.

:) resize.c compiles.

:) doesn't resize small.bmp when n is 1

:( resizes small.bmp correctly when n is 2 Byte 27 of pixel data doesn't match. Expected 0x00, not 0xff

:( resizes small.bmp correctly when n is 3 Byte 38 of pixel data doesn't match. Expected 0x00, not 0xff

:( resizes small.bmp correctly when n is 4 Byte 49 of pixel data doesn't match. Expected 0x00, not 0xff

:( resizes small.bmp correctly when n is 5 Byte 64 of pixel data doesn't match. Expected 0x00, not 0xff

:( resizes large.bmp correctly when n is 2 Byte 313 of pixel data doesn't match. Expected 0x00, not 0xff

:( resizes smiley.bmp correctly when n is 2 Byte 55 of pixel data doesn't match. Expected 0xff, not 0x00

:( resizes smiley.bmp correctly when n is 3 Byte 82 of pixel data doesn't match. Expected 0xff, not 0x00

My code

// Copies a BMP file

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

#include "bmp.h"

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

int n = atoi(argv[1]); // resize factor

if  (n >= 1 || n <= 100 );
// resize factor has to be higher than 1

else
{
    printf("MAKE SURE RESIZE FACTOR IS EQUAL TO (OR GREATER THAN) ONE\n");
    return 1;
}

// 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;
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 oldheight = bi.biHeight;
int oldwidth = bi.biWidth;

bi.biWidth *= n;
bi.biHeight *= n;

int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int oldpadding = (4 - (oldwidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biSizeImage = (((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight));

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(oldheight); i < biHeight; i++)
{
    for (int p = 0; p < n; p ++)  //interating up to n factor
    {
        // iterate over pixels in scanline
        for (int j = 0; j < oldwidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            // write RGB triple to outfile * n to enlarge
            for (int out = 0; out < n; out ++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // adding padding to outfile
        for (int k = 0; k < padding; k ++)
        {
            fputc(0x00, outptr);
        }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

The output I get is this. There's a weird line of white pixels in the middle and can't figure out how to fix it. Been looking at this for hours.

enter image description here

Thanks for the help!

1

The problem is in vertical scaling - the code isn't complete.

The code is scaling horizontally just fine. It also looks like it's set up to repeat processing each line for vertical replication, but it doesn't go back to the beginning of the line before trying to reprocess it.

The net effect is that the code is correctly scaling horizontally, but vertically is a different story. Each input line is output only once. When the last line has been processed, nothing further is read in, and the contents of the buffer from the final read is repeated over and over until the output file has had enough lines read in.

Hint: what is the fseek() function?

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

1
  • Thank for you for the suggestions! The fseek() function is used set the file position, so I would have to figure out a way to back and over the lines and scale vertically? – q0w9e8r7t6y5 Oct 2 '17 at 3:37

You must log in to answer this question.

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