0

I have been struggling for a couple of days on the resize less confortable problem.

So far my resized image looked enlarged by the right coefficient but the white section remains too small compared to the green one. (see picture attached) enter image description here

I think I am also having issues with the fwrite functions on lines 99 and 102 (the result looks better with having them commented otherwise the colors are all mixed up) . I think this creates me a problem or the padding written afterwards in my outfile.

Some hints will be much appreciated :)

Thanks !

My code below :

/**
 * Copies a BMP piece by piece, just because.
 */

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

#include "bmp.h"

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

    // ensure proper usage
    if (argc != 4)
    {
        printf("wrong command line\n");
        return 1;
    }

    // TO DO ensure first argument is an int, less than or equal to 100

    // remember the multipliying coefficient

    int coef = atoi(argv[1]);

    // check that coef is alnum @todo

    if (coef<0 || coef>100)
    {
        printf("argv1 must be between 1 and 100\n");
    }

    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[3];

    // 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;
    BITMAPFILEHEADER bf_new;


    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    //fread(&bf_new, sizeof(BITMAPFILEHEADER), 1, inptr);
    bf_new = bf;

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPINFOHEADER bi_new;
    // bi_new = bi;



    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    //fread(&bi_new, sizeof(BITMAPINFOHEADER), 1, inptr);
    bi_new = 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;
    }


    //Update
    //compute new size with coef
    bi_new.biHeight = bi.biHeight * coef;
    bi_new.biWidth = bi.biWidth * coef;

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

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

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

    bi_new.biSizeImage = ((sizeof(RGBTRIPLE)*bi_new.biWidth) + padding_new ) *abs(bi_new.biHeight);
    // fwrite(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);

    bf_new.bfSize = bi_new.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) ;
    // fwrite(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);

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

        for (int coef_k=0;coef_k<coef;coef_k++)

        {
            // 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 to outfile n times
                    for (int coef_j=0; coef_j <coef; coef_j ++)
                    {
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

                    }

            }

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

             }

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

        }


    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    //success
    return 0;
}
1

From one line of input data, you need to write coef lines of output data. Either remember the line using an array and write it multiple times, or jump back (using fseek) and re-read the input line.

2
  • Thanks ! I finally managed to use fseek and also realized I had some issues in my header's declaration. I changed the variable computations and fwrite functions orders and it now works fine.
    – lele701
    Feb 14 '18 at 18:45
  • Missed that one with the order of writing/manipulation of the header. Glad you found it :)
    – Blauelf
    Feb 14 '18 at 22:57

You must log in to answer this question.

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