0

Please help resolve this issue.. Thank you so much!!

/**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * 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)
    {
        printf("Usage: ./copy infile outfile\n");
        return 1;
    }

    int n = atoi(argv[1]);

    if(0>n || n>100){

        printf("Please enter a value in range\n");
        return 1;
    }


    // remember filenames
    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)
    {
        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;
    }



    //Create BMP variables for output image
       BITMAPFILEHEADER bf_output = bf;
       BITMAPINFOHEADER bi_output = bi;

       //Keep track of out BMP variables seperently as suggested by Zamyla
       bi_output.biWidth = bi.biWidth * n;
       bi_output.biHeight = bi.biHeight * n;

       //Keep padding of both infile and outfile seperatly
       int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
       int out_padding = (4 - (bi_output.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;


       bi_output.biSizeImage = ((bi_output.biWidth * sizeof(RGBTRIPLE)) + out_padding) * abs(bi_output.biHeight);

       bf_output.bfSize = 54 + bi_output.biSizeImage;

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

       fwrite(&bi_output,sizeof(BITMAPINFOHEADER),1,outptr);

     // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for(int v=0; v<n-1;v++){

        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

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

            }
        }

        // then add it back (to demonstrate how)
        for (int k = 0; k < out_padding; k++)
        {
            fputc(0x00, outptr);
        }
            fseek(inptr,-bi.biWidth,SEEK_CUR);
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
8
  • "bogus header data" isn't an actual scientific way to describe a problem.Try again with more details, and what you did to resolve it, including also what your problem is exactly and what you get when you run your program.
    – ChrisG
    Aug 19 '15 at 9:02
  • @ChrisG when a run my program via the terminal the new image that appears aka the enlarged ones opens but instead of i see a black image with a red stop in the center that says BMP image has bogus header data . My problem is that I cant see my enlarged Image and instead I see this error message dsescribed above Aug 19 '15 at 9:14
  • Your headers look fine to me. What differences do you see when you compare them to the ones generated by the staff?
    – ChrisG
    Aug 19 '15 at 9:28
  • I cant seem to run peek. I ran xxd and I got even more confused :( Aug 19 '15 at 9:42
  • Why can't you execute peek?
    – ChrisG
    Aug 19 '15 at 10:58

You must log in to answer this question.

Browse other questions tagged .