0

I have been stuck on this problem for over a week now. I can not seem to get the inner for loops to correctly copy and place the pixels from the infile.bmp to the outfile.bmp. I am able to created the headers correctly I believe. I think it lies in the fseek() usage but I cant seem to figure it out on my own. Anyone have any suggestions ?

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

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

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

    int n = atoi(factor);
    // 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;
    }

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


    //create new outfile bi header
    BITMAPINFOHEADER newbi = bi;
    newbi.biWidth *= n;
    //determine outfile padding for scanlines
    int outpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    newbi.biHeight =abs(bi.biHeight) * n;
    newbi.biSizeImage = ((sizeof(RGBTRIPLE)*newbi.biWidth)+outpadding) *abs(newbi.biHeight);
    //create new outfile bf header
    BITMAPFILEHEADER newbf = bf;
    newbf.bfSize = newbi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

    // write outfile's BITMAPINFOHEADER
    fwrite(&newbi, sizeof(BITMAPINFOHEADER), 1, outptr);
    long offset = bi.biWidth * sizeof(RGBTRIPLE);
    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for(int eachrow=0;eachrow<n;eachrow++){
             // 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);
                for(int eachpixel=0;eachpixel<n; eachpixel++){
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            // add padding
            for (int k = 0; k < outpadding; k++)
            {
                fputc(0x00, outptr);
            }
            // skip over padding, if any
            fseek(inptr, -offset, SEEK_CUR);

        }

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);
    printf("%d",bi.biWidth);
    // success
    return 0;
}
1

A quick way to format code properly: select the code and click the {} in the format bar, to-da. This can be done on initial post or you can edit the question and fix the format.

Here is the pseudo code from the walkthrough:

psuedocode: "recopy" method

for each row
     for n-1 times
          write pixels, padding to outfile
          send infile cursor back
     write pixels, padding to outfile
     skip over infile padding

for each row corresponds to the i loop. for n-1 times corresponds to the eachrow loop. Uh oh, how many iterations of eachrow are there? Therein lies the problem.

You must log in to answer this question.

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