0

I tried to use an array to store the original pixels, and then reprint them 'n' times in the fprint function.. I don't know where it's wrong, everything else looks good to me, the headers match the 'peek' comparisons with the staff's.. Please let me know where I'm going wrong here!

int main(int argc, char* argv[])
{
if (argc != 4)
{
    printf("Usage: ./copy n infile outfile\n");
    return 1;
}

if (atoi(argv[1]) <1 || atoi(argv[1]) > 100) {
    printf("n must be a positive integer less than or equal to 100.\n");
    return 1;
}

int n = atoi(argv[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;
BITMAPFILEHEADER bf_out;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bf_out = bf;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
BITMAPINFOHEADER bi_out;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bi_out = 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;
}


//change BITMAPFILEHEADER and BITMAPINFOHEADER's values

bi_out.biHeight = (bi_out.biHeight * n);
bi_out.biWidth = (bi_out.biWidth * n);

int origHeight = abs(bi.biHeight);
int origWidth = bi.biWidth;

int newPadding =  (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int origPadding = (4 - (origWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int a = bi.biSizeImage - (origPadding * origHeight);
bi_out.biSizeImage = (a * n * n) + newPadding * abs(bi_out.biHeight); 
bf_out.bfSize = bi_out.biSizeImage + 54;

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

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


// iterate over infile's scanlines
for (int i = 0; i < origHeight; i++)

{
        // set up temporary storage

        RGBTRIPLE triple;

        RGBTRIPLE tripleArray[origWidth];

        // write pixels to temp array

        for (int j = 0; j < origWidth; j++)
        {

        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        tripleArray[j] = triple;

        }

        fseek(inptr, origPadding, SEEK_CUR);


        // repeat n times per original scanline
        for (int x = 0; x < n; x++) {

            // write pixels from to real outfile and add padding

            for (int j = 0; j < origWidth; j++)
            {

                    fwrite(&tripleArray[j], sizeof(RGBTRIPLE), n, outptr);

            } 

            for (int k = 0; k < newPadding; k++)

                {
                    fputc(0x00, outptr);
                }


        }

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

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

Not so strange. It's a misunderstanding of how fwrite works. Look at your write statement:

fwrite(&tripleArray[j], sizeof(RGBTRIPLE), n, outptr);

The second parameter, sizeof(RGBTRIPLE) is the size of each element to be written and the third parameter, n, is the number of elements to be written. It is NOT how many times to write the same element. In other words, if n = 3, this write statement is going to go to tripleArray[j], grab the 3 consecutive triples that start there, and write them out.

So, say that you had letters instead of pixels. If the image were "abcd", and n=3, it would write "abcbcdcd?d??" (the ? would be replaced by whatever it found in memory) and not "aaabbbcccddd".

You need to actually write out each specific triple n times. There may be other issues, but this is the primary problem.

If that 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 .