0

// Copies a BMP file

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

    int main(int argc, char *argv[])
    {
        // ensure proper usage
        // I changed to argc eqal to four because it will be accepting
        // the name of the program, the number we want to increase the size by,
        if (argc != 4)
        {
            fprintf(stderr, "Usage: copy infile outfile\n");
            return 1;
        }

        // remember filenames
        int resize = atoi(argv[1]);
        char *infile = argv[2];
        char *outfile = argv[3];
        // I'm going to need to change this and add something about what
        // var resize is, as well as convert var resize from
        // an array of characters to an integer

        // 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;
        // the fread function inclues
        //fread (data, size, number, inptr)

        //data: pointing to a struct that will contain the bytes you're reading
        //size: size of each element to read
        //number: number of elements to read
        //inptr: FILE * to read from

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

        // write outfile's BITMAPFILEHEADER
        //fwrite (data, size, number, outptr);

        //data: pointer to the struct that contains the bytes you're writing
        //size
        //number
        //outptr: FILE * to write to

        // keep in mind that we are both storing the bytes
        //that we've read and the bytes that we've written at the same pointer

        fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

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

        // determine padding for scanlines
        int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
        // I'm not sure how I will account for the padding being different
        // for the infile and the outfile
        // I think I will be okay given that the copy.c updates headers automatically

        for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
        {
            // I would want to repeat the previous line n times
            for (int a = 0, b = resize; a < b, a++)

                // iterate over pixels in scanline
                for (int j = 0; j < bi.biWidth; j++)
                {
                    // I would want to repeat the previous line n times
                    for (int c = 0, d = resize, c < d, c++)

                    // temporary storage
                    RGBTRIPLE triple;

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

                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }

            // skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);
            //fseek (inptr, offset, from)
            //inptr FILE * to seek over
            //offset: number of bytes to move cursor
            //from what position
            // this makes sense because it's after you write each pixel

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

        // close infile
        fclose(inptr);

        // close outfile
        fclose(outptr);

        // success
        return 0;
    }

Overall, I'm very confused about what to do in resize. I don't understand why fread(&bf...) and fread (&bi...) have the same pointer as fwrite does. I'm also confused about what the exact purpose of reading and writing the files are and why I would make my changes to the code under fwrite instead of fread (as I made changes under fread in whodunit) as well as how I'm going to change the padding for outfile vs the infile. Thanks for helping.

1

I'm not sure why you have interpreted the instructions or code as you have. The fread statements read using the inptr pointer while the fwrite statements all write to the file attached to the outptr pointer. The input and output files are two distinct files.

This program is to enlarge (or possibly reduce, if doing more version) the image contained in the input file. There are certain values in the headers that have to be updated for the new size of the output file, as detailed in the pset instructions for resize. Also, the padding for the output file may or may not be the same as the input file padding. The padding should be calculated for each file. (There is a complicated way to calculate the new padding based on the scaling factor and other values, but its simpler to just do a straight calculation from the output file's values directly.)

I strongly recommend that you go back and review the resize spec for the pset as well as the walkthroughs until you understand what is needed.

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

| improve this answer | |
  • I took another look at the walkthrough and I posted a new question with revised code. – user21860 Jul 16 '18 at 16:08

You must log in to answer this question.

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