0

I've been trying with this problem for more than a week now .. I guess I've figured out fseek() .. and the code works fine with files without padding, but it doesn't with ones that have padding ... so I guess the problem is somehow related to padding, but I just can't put my hands on it. so any help where exactly is the problem?

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

int factor = atoi(argv[1]);
if (factor < 1 || factor > 100){
    printf("enlargement factor should be between 1 and 100\n");
    return 2;
}

// remember filenames & enlarge factor

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

// store old height and width and padding
int opadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int owidth = bi.biWidth;
int oheight = bi.biHeight;


// change image height and width according to the enlarge factor
bi.biWidth *= factor;
bi.biHeight *= factor;

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

// change biSizeImage and bfSize based on the new height and width
bi.biSizeImage = ((bi.biWidth * sizeof(RGBTRIPLE)) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + 54;

// 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 < abs(oheight); i++)
{

    for(int x = 0; x < factor; x++){

        fseek(inptr,54 + (i * ((owidth * sizeof(RGBTRIPLE))) + opadding) ,SEEK_SET);

        // iterate over pixels in scanline
        for (int j = 0; j < owidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

            for(int m = 0; m<factor;m++)
            {
            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }


        // then add padding
        for (int k = 0; k < padding; k++)
        {
        fputc(0x00, outptr);
        }


    }


}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);


// that's all folks
return 0;
}
0

The problem lies in the following line:

fseek(inptr,54 + (i * ((owidth * sizeof(RGBTRIPLE))) + opadding) ,SEEK_SET);

Look carefully at how it is calculated. How many times will the padding be added to 54?

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

1
  • Thank you so much Cliff ... for some reason I didn't think that this line might be the problem. It was merely a fault of misplacing parentheses .. Thank you again :D . Sep 16 '16 at 7:04

You must log in to answer this question.

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