Skip to main content
added 1651 characters in body
Source Link

So, here are the header calculations:

// read infile's BITMAPFILEHEADER in two variables BITMAPFILEHEADER bf;

// bfOutput = the resized file's heade
BITMAPFILEHEADER bfOutput;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
 
// setting the cursor to the beginning of inputr, to read the same stuff
fseek(inptr, 0, SEEK_SET);
fread(&bfOutput, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
 
// same as above
BITMAPINFOHEADER biOutput;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
 
// setting the cursor after the fileheader
fseek(inptr, sizeof(BITMAPFILEHEADER), SEEK_SET);
fread(&biOutput, sizeof(BITMAPINFOHEADER), 1, inptr);
 
biOutput.biWidth = biOutput.biWidth * size;
biOutput.biHeight = biOutput.biHeight * size;


// determining the paddign for the large bmp
int paddingOutput =  (4 - (biOutput.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;  
 
//updating the file and image size info
biOutput.biSizeImage = (biOutput.biWidth * abs(biOutput.biHeight) * sizeof(RGBTRIPLE)) + (paddingOutput * abs(biOutput.biHeight));
bfOutput.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + biOutput.biSizeImage;



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

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

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

So, here are the header calculations:

// read infile's BITMAPFILEHEADER in two variables BITMAPFILEHEADER bf;

// bfOutput = the resized file's heade
BITMAPFILEHEADER bfOutput;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
 
// setting the cursor to the beginning of inputr, to read the same stuff
fseek(inptr, 0, SEEK_SET);
fread(&bfOutput, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
 
// same as above
BITMAPINFOHEADER biOutput;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
 
// setting the cursor after the fileheader
fseek(inptr, sizeof(BITMAPFILEHEADER), SEEK_SET);
fread(&biOutput, sizeof(BITMAPINFOHEADER), 1, inptr);
 
biOutput.biWidth = biOutput.biWidth * size;
biOutput.biHeight = biOutput.biHeight * size;


// determining the paddign for the large bmp
int paddingOutput =  (4 - (biOutput.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;  
 
//updating the file and image size info
biOutput.biSizeImage = (biOutput.biWidth * abs(biOutput.biHeight) * sizeof(RGBTRIPLE)) + (paddingOutput * abs(biOutput.biHeight));
bfOutput.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + biOutput.biSizeImage;



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

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

// determine padding for scanlines
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
added 1521 characters in body
Source Link

My code: (I've read the headers also in the biOutput and bfOutput, modified the relevant properties for the new file's header)

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(biOutput.biHeight); i < biHeight; i++)
{
     
    // loop for vertical stretching
    for (int n = 0; n < size; n++)
    {
     
        // 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);

            // write RGB triple to outfile n times
            for (int m = 0; m < size; m++)
            {
                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 < paddingOutput; k++)
        {
            fputc(0x00, outptr);
        }
         
        // resetting position for vertical resizing
        fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)  + padding), SEEK_CUR);
    }
    fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE) + padding, SEEK_CUR);
}

My code: (I've read the headers also in the biOutput and bfOutput, modified the relevant properties for the new file's header)

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(biOutput.biHeight); i < biHeight; i++)
{
     
    // loop for vertical stretching
    for (int n = 0; n < size; n++)
    {
     
        // 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);

            // write RGB triple to outfile n times
            for (int m = 0; m < size; m++)
            {
                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 < paddingOutput; k++)
        {
            fputc(0x00, outptr);
        }
         
        // resetting position for vertical resizing
        fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)  + padding), SEEK_CUR);
    }
    fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE) + padding, SEEK_CUR);
}
formatted code
Source Link
ChrisG
  • 7.4k
  • 2
  • 17
  • 26

My resize does the work pretty well with the smiley.bmp and small.bmp. Although it fails to pass check50. I've checked my program so many times, the headers are the same as the stuff implementation, so there is no bug.

:) resize.c and bmp.h exist :) resize.c compiles :) doesn't resize 1x1-pixel BMP when n is 1 :( resizes 1x1-pixel BMP to 2x2 correctly when n is 2 :( resizes 1x1-pixel BMP to 3x3 correctly when n is 3 :( resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :( resizes 1x1-pixel BMP to 5x5 correctly when n is 5 :( resizes 2x2-pixel BMP to 4x4 correctly when n is 2


:) resize.c and bmp.h exist
:) resize.c compiles
:) doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

I know it was asked so many times, but I still couldn't find the right answer. Getting desperate :(

My resize does the work pretty well with the smiley.bmp and small.bmp. Although it fails to pass check50. I've checked my program so many times, the headers are the same as the stuff implementation, so there is no bug.

:) resize.c and bmp.h exist :) resize.c compiles :) doesn't resize 1x1-pixel BMP when n is 1 :( resizes 1x1-pixel BMP to 2x2 correctly when n is 2 :( resizes 1x1-pixel BMP to 3x3 correctly when n is 3 :( resizes 1x1-pixel BMP to 4x4 correctly when n is 4 :( resizes 1x1-pixel BMP to 5x5 correctly when n is 5 :( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

I know it was asked so many times, but I still couldn't find the right answer. Getting desperate :(

My resize does the work pretty well with the smiley.bmp and small.bmp. Although it fails to pass check50. I've checked my program so many times, the headers are the same as the stuff implementation, so there is no bug.


:) resize.c and bmp.h exist
:) resize.c compiles
:) doesn't resize 1x1-pixel BMP when n is 1
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2

I know it was asked so many times, but I still couldn't find the right answer. Getting desperate :(

Source Link
Loading