0

Seems to be working on the first line of resizing, but then something is missing and not copying the correct RGB Triples - the color scheme gets completely off... Have tried playing with all the different file info and padding, and during debugging seems like I'm getting the right values. Any ideas what I'm missing?

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

#include "bmp.h"

int main (int argc, char *argv[]) {
if (argc != 4) {
    printf("wrong number of arguments");
    return 1;
}
//declare values
int n = atoi(argv[1]);
char *infile = argv[2];
char *outfile = argv[3];
//check values
if (n <= 0 || n >= 100) {
    printf("sorry, value must be between 0 - 100");
    return 1;
}
//open files to read and write
FILE *inptr = fopen(infile, "r");
if (inptr == NULL) {
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

FILE *outptr = fopen(outfile, "w");
if (outptr == NULL) {
    fprintf(stderr, "Could not open %s.\n", outfile);
    return 2;
}

//read header files
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
//infile padding for later usage
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

//create new bi/bf values for the output file
int width = bi.biWidth;
bi.biWidth *= n;
bi.biHeight *= n;
int newpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) %4;
bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + 54;

//write header files
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

//write image. for each scan line, write each rgbtriple pixel, add padding
//for each scanline
for (int i = 0; i < abs(bi.biHeight); i++) {

    for (int m = 0; m < n; m++) {

        //for each pixel in scanline
        for (int j=0; j < bi.biWidth; j++) {

            RGBTRIPLE triple;

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

            for (int k = 0; k < n; k++) {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }//finished pixels in scanline

        //add padding at end of outptr line
        for (int pad = 0; pad < newpadding; pad++) {
            fputc(0x00, outptr);
        }
        if (m < n-1) {
            fseek(inptr, -width * sizeof(RGBTRIPLE), SEEK_CUR);
        }
    }

        // skip over padding in inptr file, if any, so that next i goes to next line.
        fseek(inptr, padding, SEEK_CUR);
}


fclose(inptr);
fclose(outptr);
return 0;

}

0
            for (int j=0; j < bi.biWidth; j++) {

should use width, not bi.biWidth (error by factor n).

Similar problem for outermost loop for height.

You must log in to answer this question.

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