0

Please, help me to understand where should i focus because I spent so many hours on this..... i would love to finish it :) here message from check50 :

:) 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
    resized image does not match expected image
:( 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
    resized image does not match expected image                             

Here the code :

    // data increase by the n value for enhance
    BITMAPFILEHEADER bf_n;
    BITMAPINFOHEADER bi_n;

    bf_n = bf;
    bi_n = 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;
    }

    // dimensions from enhance with n size for bitmapinfoheader
    bi_n.biWidth = n * bi.biWidth;
    bi_n.biHeight = n * bi.biHeight;


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


    //new dimension of biSizeImage
    //bi_n.biSizeImage = bi_n.biWidth * bi_n.biHeight;

    //new dimension of bfSize which is biSizeImage + bfOffBits // number of bytes required by the structure for the info header.
    //where bfOffbits = structure of the pixel data // format of the pixel data depends upon the number of bits per pixel.
    //where 8 Bits per Pixel. Each pixel in the row is represented 3 bytes ( 1 byte for each 3 colors) and 1byte for padding.

    //bf_n.bfSize = 54 + bi_n.biSizeImage + padding_new;
    bf_n.bfSize = 54 + (bi_n.biWidth * sizeof(RGBTRIPLE) + padding_new) * abs(bi_n.biHeight);
    bi_n.biSizeImage = bf_n.bfSize - 54;

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) // abs means positive value
    {
        // each row will be copy until n number enhance
        for (int j = 0; j < n; j++)
        {
            //iterate over pixel in row
            for (int g = 0; g < bi.biWidth; g++)
            {
                // temporary storage
                RGBTRIPLE triple;

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

                // each RGB triple copy until n number for enhance
                for (int z = 0; z < n; z++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

            // add new padding to outfile (to demonstrate how
            for (int k = 0; k < padding_new; k++)
            {
             fputc(0x00, outptr);
            }

             //return at the beginning of row
            if ( j < n - 1)
            {
              //long offset = bi.biWidth*sizeof(RGBTRIPLE);
              //seek back to the beggining of the row in input file
              fseek(inptr, - (long int)(bi.biWidth*sizeof(RGBTRIPLE) + padding), SEEK_CUR);
            }
        }
            //skip over padding if any
            fseek(inptr, padding, SEEK_CUR);
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

You read bi.biWidth pixels, each sizeof(RGBTRIPLE) bytes wide.

Then you rewind by bi.biWidth times sizeof(RGBTRIPLE) plus padding. So essentially you're rewinding too far.

2
  • i made it by switching the skip padding
    – Genevie
    Aug 26 '17 at 13:53
  • Great. With the skip part within the loop, movement forward and movement back match.
    – Blauelf
    Aug 28 '17 at 8:50

You must log in to answer this question.

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