0

Now the simley image gets resized properly....but the small.bmp does not...i checked the header information via specification...but both turn out be correct....here is the code..

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

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: copy infile outfile\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;
    }

    int toResize = atoi(argv[1]);

    if (toResize > 100 || toResize < 1)
    {
        fprintf(stderr, "Usage: second arguent should be  greater than 1 or less than 100\n");
        return 1;
    }

    // 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;
    }

    long originalHeight = bi.biHeight;
    long originalWidth = bi.biWidth;

    bi.biHeight = bi.biHeight * toResize;
    bi.biWidth = bi.biWidth * toResize;

    // calculates padding and biSizeImage and bfSize
    int padding1 = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding1 ) * 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);

    // determine padding for scanlines
    int padding = (4 - (originalWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    originalHeight = labs(originalHeight);

    // iterate over infile's scanlines
    for(int i = 0; i < originalHeight; i++) {

    // temporary storage
    RGBTRIPLE triple;

        for(int a = 0; a < toResize; a++) 
        {
            for(int j = 0; j < originalWidth; j++) 
            {
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                for(int l = 0; l < toResize; l++) 
                {
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
                //writes padding to outfile
                for (int m = 0; m < padding1; m++)
                {
                    fputc(0x00, outptr);
                }
                //skips padding
                fseek(inptr, padding,  SEEK_CUR);
            }
            //sends infile cursor back
            fseek(inptr, -(originalWidth * sizeof(RGBTRIPLE) + padding1), SEEK_CUR);
        }
        fseek(inptr, originalWidth * sizeof(RGBTRIPLE) + padding1, SEEK_CUR);

    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

Thanks again for the help :)

1

Did you look at the image output data using xxd? Here's a hint: There's padding after every pixel. The padding should only be added at the end of each line.

But there are other issues. Why does your first/outer loop only run toResize times? How many times should it execute to read all of the input file's lines of pixels?

There are probably more issues, but you need the chance to find them. ;-)

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

2
  • Thanks once again :) – Mark Mascarenhas Apr 23 '18 at 8:36
  • I finally did it, I passed all test cases, I learnt a lot from this Pset, Thanks once again Cliff :) – Mark Mascarenhas Apr 25 '18 at 19:45

You must log in to answer this question.

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