0

so i made changes you pointed to thank you.. but still there is a problem i could do it by miself but debug 50 doesnt work with (ERROR Child exited with status 1) so please point one last time with one word where i need seek for problems because i need help not the solution for example like (paddings... or cicles) i really appreciate youre help

#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 < bi.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

There are two significant problems. First, the padding for the output file has been calculated, but not for the input file. The padding for the two files are not necessarily the same, so both must be calculated. The output file padding can't be used for operations on the input file.

Second, like the padding, values like biWidth and biHeight for the output file can't be used to control movement within the input file. You need to preserve the original values to use in controlling your for loops.

There may be other issues, but this will get you going again. Other issues will be for new question.

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

1
  • updated question read pls
    – Andrew
    Dec 1 '16 at 20:39

You must log in to answer this question.

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