0

I have been working on resize for some time now and continue to get random colours in my output. I have tried multiple variations of both the rewrite and recopy methods to no avail. I am currently most confused about where to put the fseek function in my code, however it is very possible that there aare other errors. the pseudocode I have been working with (provided in the resize video) is for each row for n-1 times write pixels, padding to outfile

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

int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        printf("Usage: ./copy infile outfile\n");
        return 1;
    }

    // remember filenames
    int n = atoi(argv[1]);
    char* infile = argv[2];
    char* outfile = argv[3];

    // open input file 
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("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);

    bi.biWidth *=n;
    bi.biHeight *=n;

    int outpadding=  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int inpadding = (4 - (bi.biWidth/n * sizeof(RGBTRIPLE)) % 4) % 4;

    bi.biSizeImage= ((sizeof(RGBTRIPLE) *bi.biWidth) + outpadding) * abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    long offset = bi.biWidth * sizeof(RGBTRIPLE);

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

    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for (int j = 0; j < bi.biWidth; j++)
        {   
            RGBTRIPLE triple;
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            for (int a = 0; a <n-1; a++)
            {                   
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                fseek(inptr, -offset, SEEK_CUR);
            }          

            for (int k = 0; k < outpadding; k++)
            {
                fputc(0x00, outptr);
            }
            fseek(inptr, inpadding, SEEK_CUR);            
        }
    }
        // skip over padding, if any

        // then add it back (to demonstrate how)

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
1

I see several problems. First, the code uses bi.biWidth and bi.biHeight for the output file to control the loops. The values for the input file should be used. Next, look at the following:

        for (int a = 0; a <n-1; a++)
        {                   
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            fseek(inptr, -offset, SEEK_CUR);
        } 

This code is supposed to write each pixel n times to the output file. In reality it will write n-1 copies of each pixel. What happens when n=1? Also, the fseek will back up in the input file one line, less the padding for each and every pixel written to the output file. This is the wrong approach.

There may be more issues, but this should get you going.

Here's some advice. Work through the code in steps. First, start with n=1. Make sure the header is right. Then start working on the image. Once the image and the header are right, move on to n=2. Get the header right first and then, finally, work on the image.

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

You must log in to answer this question.

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