0

I have worked on this so many hours. Thought I had fixed things up but still not resizing correctly. Can someone check my code and let me know where I am messing up ??

#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 padding = (4 - (bi.biWidth * 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; 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, outptr);

           // 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< padding; k++ )
    {
        fputc(0x00, outptr);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    return 0;
}

1 Answer 1

0

There are multiple issues with your code, and I'm sure that you've probably been working around many of them - probably would have fixed them, except for the one underlying problem that's messing everything up. I suspect that even though the headers are right, the image data always comes up as 000000.

So here's the baseline problem. Look at this line of code:

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

Wouldn't it work better if the code read from the input file instead? Once this is fixed, resolving the remaining issues should be much easier. ;-)

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

1
  • Thanks ! OMG - I can't believe I missed this !! I wrote it correctly on my pseudo code and then copied it incorrectly. Apr 3, 2018 at 14:49

You must log in to answer this question.

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