0

Had to walk away from this but looking over it again and hitting a wall. Can someone take a look ??

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

int main (int argc, char *argv[])
{
    // Check for correct arguments

    if (argc != 4)
    {
        fprintf(stderr, " Usage: ./resize n infile outfile\n");
        return 1;
    }
    // Remember file names
    char *n = argv[1];
    char *infile = argv[2];
    char *outfile = argv[3];

    int number = atoi(n);

    if (number < 1 || number  > 100)
    {
        fprintf(stderr, " n must be a positive number between 1 and 100.\n");
        return 2;
    }
    // Open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, " Could not open %s.\n", infile);
        return 3;
    }
    // Open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, " Could not create %s.\n", outfile);
        return 4;
    }
    // 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 5;
    }

    // infile- remember width, height and padding

    int width_in = bi.biWidth;
    int height_in = abs(bi.biHeight);
    int padding_in = (4- (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // change the header properties to match the scaling

    bi.biWidth *= number;
    bi.biHeight *= number;
    int paddingnew = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + paddingnew) * 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; i < height_in ; i++)
    {
        // declare an array for storing row to be written

        RGBTRIPLE row[bi.biWidth];

        // iterate over pixels in scanline

        for  (int j = 0; j < width_in; j++)
        {
            //temp storage

            RGBTRIPLE triple;

            //read RGBTRIPLE from infile

            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

           // writing the pixel n times in an array

           for (int x = 0; x < number; x++)
            {
                row[x + ( number *j)] = triple;
           }
        }
        for (int j = 0; j < number; j++)
        {
            fwrite(row, sizeof(row), 1, outptr);

        }
    }

    // skip over padding if any

    fseek(inptr, padding_in, SEEK_CUR);

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    return 0;
}

check50 result:

) resize.c and bmp.h exist.
:) resize.c compiles.
:( doesn't resize small.bmp when n is 1
    Byte 29 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 2
    Byte 20 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes small.bmp correctly when n is 3
    Byte 29 of pixel data doesn't match. Expected 0x00, not 0xff
:( resizes small.bmp correctly when n is 4
    Byte 146 of pixel data doesn't match. Expected 0xff, not 0x00
:( resizes small.bmp correctly when n is 5
    Byte 47 of pixel data doesn't match. Expected 0x00, not 0xff
:) resizes large.bmp correctly when n is 2
:) resizes smiley.bmp correctly when n is 2
:) resizes smiley.bmp correctly when n is 3

See https://cs50.me/c

0

Let's look at exactly what the code is doing:

for (int i = 0; i < height_in ; i++)
{
    // process entire input file
    ...
}

// skip over padding if any

fseek(inptr, padding_in, SEEK_CUR);

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

The padding is being processed at the end of the file, not the end of each line as it should. The code needs to be reorganized a bit.

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

You must log in to answer this question.

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