Can someone please help me figure out what I am doing wrong here? My program outputs a transparent image, but the image is the correct size.
(For example: input image is 4x4 output image is 8x8 but completely transparent)

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

#include "bmp.h"

int main(int argc, char* argv[])
    // ensure proper usage
    if (argc != 4)
        printf("Usage: ./resize n 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)
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;

    // read infile's BITMAPFILEHEADER
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

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

    int padding =  (4 - (bi.biWidth * n * sizeof(RGBTRIPLE)) % 4) % 4;

    // Store width and height
    int width = bi.biWidth;
    int height = bi.biHeight;

    // Calculate new size of image
    bi.biSizeImage = width*height+padding*width;

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

    // 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)
        fprintf(stderr, "Unsupported file format.\n");
        return 4;

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

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

    int length = 0;
    fpos_t lineStart;
    // iterate over infile's scanlines
    for (int i = 0; i < height; i++)
        fgetpos(inptr, &lineStart);
        for (int m = 0; m < n; m++)
            length = 0;
            // iterate over pixels in scanline
            for (int j = 0; j < width; j++)
                // temporary storage
                RGBTRIPLE triple;

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

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



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

            // then add it back (to demonstrate how)
            for (int l = 0; l < padding; l++)
                fputc(0x00, outptr);
        fseek(inptr, padding, SEEK_CUR);

    // close infile

    // close outfile

    // that's all folks
    return 0;

I have been stuck on this problem set for a while now, please help!


It isn't that the image is transparent, the image data doesn't exist. Because of a lack of understanding of the values being handled, the image data is not being processed.

There are a number of issues, some of which are:

At least one of the header fields is not being calculated correctly. Padding is only being calculated once. Padding needs to be calculated for both the input and output files separately.

There may be other issues, but this will get you started and give you more experience debugging, which is of more value.

You should start by testing with ./ressize 1 small.bmp s2.bmp This command should produce a copy of the input file. First get the header calculations right. Then get the image right. Once the whole file is copied correctly, change 1 to 2 and repeat the process.

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

| improve this answer | |

You must log in to answer this question.

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