0

I have tried many times to find the error but have failed to do so ... here is my resize/less 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: ./resize n infile outfile\n");
        return 1;
    }
    //the first agrv n where 0<n<=100
    int n=atoi(argv[1]);
     if(n<0 || n>100){
         printf("n must be a positive integer less than or equal to 100\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;
    }

    //old padding
     int padding = (4-(bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    //old width and height
     LONG o_width=bi.biWidth;
     LONG  o_height=abs(bi.biHeight);
    //n times i.e., new width and height
     bi.biWidth *= n;
     bi.biHeight*= n;
    // new padding
     int n_padding = (4-((bi.biWidth)* sizeof(RGBTRIPLE)) % 4) % 4;

     //new sizeimage and size
     bi.biSizeImage = (sizeof(RGBTRIPLE)*bi.biWidth + n_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);

    //resize 
    // iterate over infile's scanlines
    for (int i = 0, height = abs(o_height); i < height; i++)
    {
        //n time iteration
        for (int a = 0; a < n; a++)
        {  
            // iterate over pixels
            for (int j = 0; j < o_width; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

                 for (int m=0; m < n; m++)
                 {
                    // read RGB triple from outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                 }
            }   

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

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

             //skip if changing to new line
              if(a+1<n)
              fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (i * (o_width * sizeof(RGBTRIPLE) + n_padding)), SEEK_SET);
        }
    }
    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

the check50 shows the following result

:) resize.c and bmp.h exist.
:) resize.c compiles.
:) doesn't resize 3x3-pixel BMP when n is 1
:( resizes 3x3-pixel BMP to 6x6 correctly when n is 2
    resized image does not match expected image
:( resizes 3x3-pixel BMP to 9x9 correctly when n is 3
    resized image does not match expected image
:( resizes 3x3-pixel BMP to 12x12 correctly when n is 4
    resized image does not match expected image
:) resizes 3x3-pixel BMP to 15x15 correctly when n is 5
:( resizes 6x6-pixel BMP to 12x12 correctly when n is 2
    resized image does not match expected image
:( resizes 9x9-pixel BMP to 18x18 correctly when n is 2
    resized image does not match expected image
:) resizes 6x6-pixel BMP to 18x18 correctly when n is 3

please someone help me find my mistake or give me some hints.Thank you.

0

The line

fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (i * (o_width * sizeof(RGBTRIPLE) + n_padding)), SEEK_SET);

should use original dimensions, even for padding (use padding instead of n_padding).

2
  • Thank you so much it worked!
    – shaistha
    Sep 25 '17 at 13:40
  • Had to be a padding issue as there are happy smileys for all the cases where the padding stays the same between input and output file.
    – Blauelf
    Sep 25 '17 at 13:42

You must log in to answer this question.

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