0
/**
 * 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: ./copy n infile outfile\n");
        return 1;
    }
    //making sure n is an integer
    int n = atoi(argv[1]); // make so n is integer
    if(n > 100) // this is working but it passes out words still how to I fix?
    {
        fprintf(stderr, "Usage: require and integer less than 101\n");
        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;
    }
    // I believe this is where I can update the outfile bitmap headers as they have been read already
// why do i have to create new variables in a new header like out_bi instead of just updating just bi? Using header from infile to make out file bad? why?
    BITMAPFILEHEADER newbf=bf;
    BITMAPINFOHEADER newbi=bi;
    newbi.biWidth = bi.biWidth * n;
    newbi.biHeight = bi.biHeight * n;
    int newpadding = (4 - (newbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    newbi.biSizeImage = ((sizeof(RGBTRIPLE) * newbi.biWidth) + newpadding) * abs(newbi.biHeight);
    newbf.bfSize = newbi.biSizeImage + 54;// use 54 as it uses the bfoffbits look at https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx)
// creating the above new headers is needed as when you resize you create your map first then you fill in the blanks when reading the infiles scanlines
 // go to https://www.youtube.com/watch?v=AqscP7rc8_M for more information about resizing   
    // write outfile's BITMAPFILEHEADER
    fwrite(&newbf, sizeof(BITMAPFILEHEADER), 1, outptr);

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for(int vert = 0; vert < n; vert++)
        {

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

            // write RGB triple to outfile. Does it require for loop to go horizontally?
            for(int wub = 0; wub <n; wub++)
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); // each pixel is being written n times first
        }
        // moved putc to write new padding before skipping infile padding and it worked!
        for (int k = 0; k < newpadding; k++)
        {
            fputc(0x00, outptr);
        }
        fseek(inptr, -(bi.biWidth *3), SEEK_CUR);
        }//vert ends here
        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);
        //there used to be putc padding here but I moved it
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

I am having trouble implementing recopy method I thought I had it with this iteration but it is not working :( Is anyone able to give me a hint I think Im close but not sure

1

Here's 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

The vert loop more-or-less corresponds to "for n-1 times". Because it is executing n times, one problem comes from "send[ing] infile cursor back" an extra time.

1
  • so about that psuedocode it confuses me as does it mean I have to have an fwrite statement twice and it has write pixels twice? And okay ya ill try out this n-1 and thank you for your advice! – Roundabout Apr 24 '17 at 21:15

You must log in to answer this question.

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