0

I'm working on the resize (less version) problem, but the output image is a messed up block of color boxes. Is there something wrong while I'm iterating over the lines? Thanks!

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

int main(int argc, char *argv[])
{
//Usage
if (argc != 4)
{
    fprintf(stderr, "Usage: resize infile outfile\n");
    return 1;
}

int n = atoi(argv[1]);

if (n < 1 || n > 100)
{
    printf("Usage: resize input\n");
    return 2;
}

// 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_in;
fread(&bf_in, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi_in;
fread(&bi_in, sizeof(BITMAPINFOHEADER), 1, inptr);

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf_in.bfType != 0x4d42 || bf_in.bfOffBits != 54 || bi_in.biSize != 40 ||
    bi_in.biBitCount != 24 || bi_in.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}


// create outfile's BITMAPFILEHEADER
BITMAPFILEHEADER bf_out = bf_in;

// create outfile's BITMAPINFOHEADER
BITMAPINFOHEADER bi_out = bi_in;

//CHANGE BITMAPINFOHEADER
bi_out.biWidth = n * bi_in.biWidth;
bi_out.biHeight = n * bi_in.biHeight;

// determine padding for scanlines
int padding_in = (4 - (bi_in.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int padding_out = (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//CHANGE BITMAPINFOHEADER SizeImage
bi_out.biSizeImage = ((sizeof(RGBTRIPLE) * bi_out.biWidth) + padding_out) * abs(bi_out.biHeight);

//CHANGE BITMAPFILEHEADER
bf_out.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

//WRITE TO OUT_BF & BI
fwrite(&bf_out, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi_out, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterate over infile's scanlines
for (int i = 0, bi_inHeight = abs(bi_in.biHeight); i < bi_inHeight; i++)
{
    // iterate over pixels in scanline
    for (int j = 0; j < bi_in.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

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

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

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

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

    //MOVE CURSER BACK TO BEGINNING OF ROW
    fseek(outptr, -n*bi_out.biWidth , SEEK_END);

    // RESIZE VERTICALLY
    RGBTRIPLE triplerow;
    fread(&triplerow, sizeof(RGBTRIPLE), bi_out.biWidth, outptr);
    fwrite(&triplerow, sizeof(RGBTRIPLE), bi_out.biWidth, outptr);

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1 Answer 1

0

Well I see quite a few issues, first of all im nit sure what this is doing there.

// RESIZE VERTICALLY
RGBTRIPLE triplerow;
fread(&triplerow, sizeof(RGBTRIPLE), bi_out.biWidth, outptr);
fwrite(&triplerow, sizeof(RGBTRIPLE), bi_out.biWidth, outptr);

Next you did not add the {} on this line of code

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

There is also a loop missing and an issue with this

    //MOVE CURSER BACK TO BEGINNING OF ROW
fseek(outptr, -n*bi_out.biWidth , SEEK_END);

If you move it to the beginning of the line and you will have to reset it afterwords so the code can keep running.

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

1
  • Thanks! All cleared.
    – AYA
    Nov 3, 2019 at 2:24

You must log in to answer this question.

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