0

So i think i finished resizing horizontally but when i try to open the output image to check i get an error saying "Invalid or unsupported Image Format", I read other similar questions here and i came to the conclusion that the issue is the headers? This is my code:

    // Copies a BMP file

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


#include "bmp.h"

int main(int argc, char *argv[])
{
    // TEST - ENSURE PROPER USAGE:

    if (argc != 4)
    {
        fprintf(stderr, "Usage: resize n infile outfile\n");
        return 1;
    }


    // Turn the argv[1] into an int that tells me how many times to multiply 
    // the image by
    int n = atoi(argv[1]);
    printf("n = %i\n", n);

    // TEST - CONTINUE "ENSURE PROPER USAGE":
    if (n <= 0 || n >= 100)
    {
        fprintf(stderr, "Usage: resize n infile outfile\n");
        return 1;
    }
    printf("### Ensure proper usage - DONE ###\n");


    // REMEMBER FILENAMES:
    char *infile = argv[2];
    char *outfile = argv[3];


    // 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);


    // TEST - 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;
    }


    // DETERMINE PADDING FOR INFILE'S SCANLINES: 
    int sorgbt = sizeof(RGBTRIPLE);
    int small_padding = (4 - (bi.biWidth * sorgbt) % 4) % 4;

    // obi / obf are copies of the infile headers
    BITMAPFILEHEADER obf = bf; 
    BITMAPINFOHEADER obi = bi;
    obi.biWidth = bi.biWidth * n;
    obi.biHeight = bi.biHeight * n;


    // DETERMINE PADDING FOR OUTFILE'S SCANLINES: 
    sorgbt = sizeof(RGBTRIPLE);
    int padding = (4 - (obi.biWidth * sorgbt) % 4) % 4;


    // WRITE OUTFILE'S INFO HEADER (BMIH):
    obi.biSizeImage = (bi.biSizeImage - (small_padding * 3)) * (n * n) + (padding * abs(obi.biHeight)) ;
    obf.bfSize = 54 + obi.biSizeImage;
    fwrite(&obi, sizeof(BITMAPINFOHEADER), 1, outptr);


    // WRITE OUTFILES'S BITMAPFILEHEADER:
    fwrite(&obf, sizeof(BITMAPFILEHEADER), 1, outptr);


    // ITERATE OVER INFILE'S SCANLINES:
    bi.biHeight = abs(bi.biHeight);
    for (int i = 0; i < bi.biHeight; i++)
    {
        for (int p = 0; p < n; p++)
           {
            // iterate over pixels in scanline
            // for each pixel its done n times
            for (int j = 0; j <  bi.biWidth; j++)
            {
                // temporary storage
                RGBTRIPLE triple;

                // read RGB triple from infile
                fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                // write RGB triple to outfile
                for (int k = 0; k < n; k++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    printf("DONE \n");
    return 0;
}

These are the values in the headers with n = 2: enter image description here enter image description here

I would also be thankful for some tips about resizing vertically whats keeping me for succeeding in doing so :) Thanks.

1 Answer 1

1

Have you actually looked at the raw hex data contained in the program's output files? Notice anything interesting? Where is the bmp signature that's supposed to be in the first 2 bytes?

Next question: Which header is supposed to be printed out first, bi or bf? ;-)

1
  • ohh right sh*t... Thanks :) Commented Jul 1, 2019 at 5:42

You must log in to answer this question.

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