1

I've been watching the walkthrough for 3 days now and can't understand the whole idea, i read the spec and wrote couple lines of code finally i decided to write the code with only psuedocode in the video for this proplem here's the code :

// Copies a BMP file

include

include

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];

int n = atoi(argv[1]);
if (n <= 0 || n >= 100)
{
    printf("scale Must be between 0 and 100\n");
    return 1;

}

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

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

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

int newbiwidth = bi.biWidth * n;
int newbiheight = bi.biHeight * n;

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

//creating new padding with new dimentions
int pading = (4 - (newbiwidth * sizeof(RGBTRIPLE)) % 4) % 4;

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{

    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        //iterate over scale to write it pixel by pixel n times
        for (int s = 0; s < n; s++)
        {
            // temporary storage
            RGBTRIPLE triple;
            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            fseek(inptr, *infile, SEEK_CUR);
        }
    }

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


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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

i need some enlightenment here please, thanks in advance.

0

As a start it doesn't look like you're setting the new parameters for BITMAPFILEHEADER and BITMAPINFOHEADER before writing these to the outfile

Go to 2:31 in Zaymyla's video to get a better idea..

4
  • first of all thanks for your help, i rewatched zamayla's video again and you're right i missed that one and tried to fix it so here couple lines i've added after the second padding equation, bi.biSizeImage = ((sizeof(RGBTRIPLE) * newbiwidth) + pading * abs(newbiheight)); bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); but i think i've missing something about returning the values back to the object struct am i right? – ox0o Sep 18 '19 at 3:51
  • Yes you also need to write the new height and width to BITMAPINFOHEADER. If you don't do this before writing the new values for bi.biSizeImage and bf.bfSize then these will be incorrect as they depend on the new height and width of the resized image – uroboros Sep 18 '19 at 17:13
  • Also keep in mind that you need to keep track of the original padding and padding for the resized image I probably can't assist much further on this one so in the words of Cliff B: If this answers your question, please click the check mark. Let's keep up on forum maintenance. ;-) Otherwise good luck :) – uroboros Sep 18 '19 at 17:20
  • i made some progress last night still have some issues but i think i got it, thanks dude – ox0o Sep 19 '19 at 7:51

You must log in to answer this question.

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