0

hi guys cant see whats wrong help please

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

#include "bmp.h"

int main(int argc, char* argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        printf("Usage: ./copy factor infile outfile\n");
        return 1;
    }
    int n = atoi(argv[1]);
    if((n < 1)||(n > 100))
    {
        printf("ERROR factor cannot be less then 1 and more then 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)
    {
        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,bf2;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    bf2 = bf;

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi,bi2;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    bi2 = bi;

    // 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;
    }
    //changing dementsions
    bi2.biWidth = bi.biWidth * n;
    bi2.biHeight = bi.biHeight * n;
    //adding paddings
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding2 = (4 - (bi2.biWidth * sizeof(RGBTRIPLE) % 4) % 4);
    //new sizes
    bi2.biSizeImage = ((sizeof(RGBTRIPLE) * bi2.biWidth) + padding2) * abs(bi2.biHeight);
    bf2.bfSize = bi2.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
                for(int z = 0;z < n;z++)
                {

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

            // add padding
            for (int k = 0; k < padding2; k++)
            {
                fputc(0x00, outptr);
            }
            if (j < n - 1)
            fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
            // skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);
        }    
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
1

First, the code is still using output header values to control processing of the input file since the last question.

Next, there are issues with reprocessing the same line in the input file n times. Look at the following:

        if (j < n - 1)
        fseek(inptr, bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

The first fseek() is supposed to move backwards in the file, yet it moves forward. The combination of these two fseeks is also problematic. They move over the padding in one of them, but doesn't account for padding in the other.

Think carefully about when the input pointer is being moved and where it is moving to.

Again, this will get you going. If there are any new issues, please ask a new question instead of substantially altering the current one. We like to preserve the questions so that they match the answers.

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

You must log in to answer this question.

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