0

/** * Copies a BMP piece by piece, and increase by factor of n */

include

include

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

int n =atoi (argv[1]);
{
    if (n<1 ||n>100)
    {
        fprintf(stderr, "n has to be a number between 0-100\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 1;
}

// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 1;
}

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

//replace with new height and width nbi
bi.biWidth *=n;
bi.biHeight *=n;

// determine padding for scanlines

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

bi.biSizeImage=(bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(bi.biHeight);
bf.bfSize=sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) +bi.biSizeImage;


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

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

// read by iterating over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight/n); i < (biHeight); i++)
{
    //repeat each scanline n times
    for (int m=0; m<n; m++)
    {
        // iterate over infile pixels in scanline
        for (int j = 0; j < (bi.biWidth/n); j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
            {
                // write RGB triple to outfile
                for (int l=0; l<n;l++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

        }

        //  add new_padding
        for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }

            //newline
            fputc('\n', outptr);

        //m is number of times the infile seek-cur reverse to start of line
        if (m<(n-1))
            {
                fseek(inptr, -(bi.biWidth/n), SEEK_CUR);
            }
        else
        {
             // skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);
        }

    }

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

return 0;

}

3
  • Hi. Naked code drops are against the rules of this forum. Please review the information at the following two sites, particularly the sections on code drops and on formatting code. meta.stackexchange.com/questions/22186/… and meta.cs50.stackexchange.com/questions/63/… After reviewing these two sites, please come back and edit your question, adding details about what is giving you problems, examples of output, and what you think is the issue and we'll be happy to help you then.
    – Cliff B
    Dec 1 '17 at 10:24
  • Again, this is a code drop. Can you please add a description of the problem to the question. What is it doing, what do you think is the problem, along with any other insight you have.
    – Cliff B
    Dec 1 '17 at 22:01
  • I finally get how to modify and write the file headers but my problem is that i am not able to expand vertically. I used the /n function to get a new line but i am not getting a new line and instead the colour of the next line changes. please advice if the code to get to next line is correct and why the next line changes colour
    – Winy
    Apr 9 '18 at 10:19

You must log in to answer this question.

Browse other questions tagged .