0

I'm still so very stuck on this one. Below I have a cobbled together "solution" that at least increases the size of the image.

I have no clue what I'm doing when it comes to copying the value of each pixel n number of times, nor how that would translate to the extra rows of of pixels.

Question wise: what do I need to do next?

I need a nudge, I've been stuck on this for weeks now.

// Copies a BMP file

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

#include "bmp.h"

// BITMAPFILEHEADER and BITMAPINFOHEADER
int main(int argc, char *argv[])
{
    int n = 2;
    // ensure proper usage
    if (argc != 3)
    {
        fprintf(stderr, "Usage: copy infile outfile\n");
        return 1;
    }

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

    // 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;
    }
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // output
    BITMAPFILEHEADER out_bf = bf;
    BITMAPINFOHEADER out_bi = bi;
    out_bi.biWidth = bi.biWidth * n;
    out_bi.biHeight = bi.biHeight * n;
    int out_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    out_bi.biSizeImage = (sizeof(RGBTRIPLE) * out_bi.biWidth + out_padding) * abs(out_bi.biHeight);
    out_bf.bfSize = out_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

    // write outfile's BITMAPINFOHEADER
    fwrite(&out_bi, sizeof(BITMAPINFOHEADER), 1, outptr);
    // tempstorage again
    RGBTRIPLE scanline[out_bi.biWidth];

    // iterate over outfile's scanlines
    // rows
    // for (int i = 0, biHeight = abs(out_bi.biHeight); i < biHeight; i++)
        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)

    {
        // pixel
        // for (int j = 0; j < out_bi.biWidth; j++)
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;
            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
            // fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

            for (int k = 0; k <= n; k++)
                {
                    scanline[k] = triple;
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                    // x++;
                }

            // for(int k = 0; k < n; k++)
            // {
            //     fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            //     fseek(outptr, sizeof(RGBTRIPLE), SEEK_CUR);
            // }
        }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

k <= n should be k < n.

The line scanline[k] = triple; should include some j (each input pixel would have to advance the index by n).

I wouldn't fwrite while reading, but do that separately, in a for loop after your fseek, after you've read the whole input line.

| improve this answer | |

You must log in to answer this question.

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