0

I have an issue that I have been tearing my hair out for a week about. My code does not resize the image and it doesn't keep the image the same. What is the problem with my logic?

/**
 * 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)
    {
        fprintf(stderr, "Usage: ./resize n infile outfile\n");
        return 1;
    }
    int n = atoi(argv[1]);
    if ((n < 1) || (n > 100))
    {
        fprintf(stderr, "n must be a positive integer between 1 to 100");
        return 1;
    }

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

    // deternmine dimensions
    int oldwidth = bi.biWidth;
    int oldheight = bi.biHeight;
    bi.biWidth = oldwidth * n;
    bi.biHeight = oldheight * n;

    // determine padding for scanlines
    int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int oldpadding = (4 - (oldwidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(bi.biHeight);
    //addheaders
    bf.bfSize = (bi.biSizeImage) + 54;

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

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

    int posoldheight = abs(oldheight);
    // iterate over infile's scanlines
    for (int i = 0; i < posoldheight; i++)
    {
        for (int j = 0; j < n; j++)
        {
            fseek(inptr, (54 + (((oldwidth * 3) + oldpadding) * i)), SEEK_SET);
            // iterate over pixels in scanline
            for (int k = 0; k < oldwidth; k++)
            {
                // temporary storage
                RGBTRIPLE triple;]


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

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

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

You are writing your outfile's headers before you calculate their new values.

Also, given that you are fseeking to the beginning of each scanline using SEEK_SET, do you then also want to fseek forward by the padding amount? Won't that put you too far in the file?

5
  • Ok, I think I fixed a part of the issue, however, I believe I have an issue with the padding. I am uploading the new code now. – Human Aug 3 '17 at 21:32
  • @Human I edited my answer. – curiouskiwi Aug 4 '17 at 1:35
  • Ok thank you, I will try this out. – Human Aug 4 '17 at 4:00
  • I'm afraid I do not understand what exactly I am supposed to do. Can you please provide a little more detail on what I need to do to fix my code? – Human Aug 4 '17 at 4:47
  • What's the point of this? // skip over padding, if any fseek(inptr, newpadding, SEEK_CUR); Why are you moving your read cursor forward in the input file by the amount of the new padding? – curiouskiwi Aug 4 '17 at 4:48

You must log in to answer this question.

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