0

Can anyone help me with my code, please? I get this result, and don't know how to fix this:

enter image description here

My code resizes images horizontally, but does not do the right thing with height:

// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(argv[1]);

if(n < 0 || n > 100){
    fprintf(stderr, "%i must be positive integer smaller than 100", n);
    return 1;
}

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "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;
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 originalPadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
LONG originalBiWidth = bi.biWidth;
LONG originalBiHeight = bi.biHeight;

bi.biWidth*=n;
bi.biHeight*=n;

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

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

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(originalBiHeight); i < biHeight; i++)
{
    for(int m = 0; m<n; m++){

        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }

        fseek(inptr, -(originalBiWidth * sizeof(RGBTRIPLE)), SEEK_CUR);

        // skip over padding, if any
        fseek(inptr, originalPadding, SEEK_CUR);
    }
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}

1

You use bi.biWidth, not originalBiWidth for the loop.

After writing a line, you jump backwards by (originalBiWidth * sizeof(RGBTRIPLE)), which can be correct. But then you additionally jump forward by originalPadding, which certainly isn't. Instead, outside this loop, jump forward by (originalBiWidth * sizeof(RGBTRIPLE)) + originalPadding to progress to the next input line.

0

omg, that was such a stupid mistake that I didn't track/check the cursor position inside file. That worked:

for (int i = 0, biHeight = abs(originalBiHeight); i < biHeight; i++) { for(int m = 0; m

        for (int j = 0; j < originalBiWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
        fseek(inptr, -(originalBiWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
    }
    // skip over padding, if any
    fseek(inptr, originalBiWidth * sizeof(RGBTRIPLE) + originalPadding, SEEK_CUR);
}

thank you

You must log in to answer this question.

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