0

I am able to resize the image for a factor of 1 correctly , but do not understand why it is failing for n > 1 The output for a factor of 4 is included. Could someone pls provide a few hints ? enter image description here

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

 int main(int argc, char* argv[])

{

       // ensure proper usage
         if (argc != 4)
{
    printf("Usage: ./resize n infile outfile\n");
    return 1;
}

// remember filenames
int n = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];

// 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,bfo;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfo = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,bio;    // bfo and bio are variables for storing outfile header info 
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bio = bi;

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

// change width, height for  outfile header 

bio.biWidth = bi.biWidth * n; 
bio.biHeight = bi.biHeight * n;

// padding new 

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

// determine padding for oufile 
int paddingo = (4 - (bio.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; 
 // paddingo is outfile padding 


// change size of outfile header 
bio.biSizeImage = (bio.biWidth * sizeof(RGBTRIPLE)  + paddingo) * abs(bio.biHeight); 
bfo.bfSize = bfo.bfSize - bi.biSizeImage + bio.biSizeImage;     // Calculate actual image size 

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

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

// I moved the padding line from 79-80 to padding new heading 

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    if (i % n != 0)
    {
    fseek(inptr, -((bio.biWidth* sizeof(RGBTRIPLE)*3) + paddingo),SEEK_CUR);
    } 


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



        for (int k = 0; k < n; k++)
            {

                // write RGB triple to outfile
                 fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

            }



    } // pixel j horizontal 

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

            // Add a new padding
        for (int l = 0; l < paddingo; l++)
            fputc(0x00, outptr);


} // main loop for Height

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

1

While writing bytes to your new file, you are not expanding the image vertically. Observe your code. You need to repeat writing a particular row from input file to the output file n times (however, you are doing it only one time for every row).

If you are relying on that "if statement" at the top of your loop for repeating every row n times, then it is flawed.

I suppose it should be,

if (i % n != 0)
{
fseek(inptr, -((bio.biWidth* sizeof(RGBTRIPLE)) + paddingo),SEEK_CUR);
i--;
} 
  1. Got rid of *3
  2. Added i--;

I am not sure whether this is the perfect code. But, it should take you closer to it. `

You must log in to answer this question.

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