0

when I ran it with 1 it won't open small.bmp When I check it with: check50 2015.fall.pset4.resize bmp.h resize.c I give me almost all red but at the same time seems it resize as needed, what Am I missing here?

:) resize.c and bmp.h exist
:) resize.c compiles
:( doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:) resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

https://sandbox.cs50.net/checks/bb8d8f84ad4b434ca1ad03522450ef63

my 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
    char* infile = argv[2];
    char* outfile = argv[3];

    // number to enlarge the file by
    int n = atoi(argv[1]);

    if (n < 1 || n > 100)
    {
       printf("The value on %i must be btw 1 and 100 (inclusive)\n", n);
        return 5; 
    }

    // 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;
    }
    BITMAPFILEHEADER bfTemp = bf;
    BITMAPINFOHEADER biTemp = bi;

    bfTemp.bfSize = (bf.bfSize - 54) * n * 3 + 54;
    biTemp.biSizeImage = bi.biSizeImage * n * 3;
    biTemp.biWidth = bi.biWidth * n;
    biTemp.biHeight = bi.biHeight * n;

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

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            for (int i2 = 0; i2 < n; i2++)
            {
                // 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 n times
                    for (int i1 = 0; i1 < n; i1++)
                    {
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);    
                    }
                }
                // then add padding 
                for (int k = 0; k < paddingNew; k++)
                {
                    fputc(0x00, outptr);
                }
                 fseek(inptr, -(3 * bi.biWidth), SEEK_CUR);
            }        
                // skip over padding, if any
                fseek(inptr, (padding + 3 * bi.biWidth), SEEK_CUR);
        }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

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

Have you determined what is wrong in the output? Using the peek tool, have you checked the headers of an output file against an input file using a scale of 1? Have you compared the image data as well, using the xxd tool?

When you do, you'll find that you have some header issues. Simply put, the errors are due to incorrect calculation of these header values. They are dependent on correctly calculating the padding for the output file and incorporating that into the results. The bad values can't be calculated before calculating the padding! Frankly, the bad calculations weren't even close. You should review how those values are supposed to be calculated.

I'm being somewhat cryptic to force you to troubleshoot the errors. This is a much more valuable skill to have than just spelling it out.

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

1
  • It did, my mistake was that because the resized bmp look as expected, I didn't consider that maybe I have a header problem. Thanks!
    – ShaLuz
    Nov 28 '16 at 9:59

You must log in to answer this question.

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