0

I have asked before but no one seems to have an explanation for what is happenning because it seems as if it were ok , could someone please help me and tell me why this is happening, this is the code

./**
 * copy.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * rezises a BMP piece by piece, just because.
 */

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

#include "bmp.h"

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

    // remember filenames and key to resize

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

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPINFOHEADER bic;
    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;
    }
    //makes a copy of the header files
    bfc = bf;
    bic = bi;

    bic.biWidth = bi.biWidth * key; 
    bic.biHeight = bi.biWidth * key;

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

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

    // Padddings
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) %4) % 4;
    int paddingc = (4 - (bic.biWidth * sizeof(RGBTRIPLE)) %4) % 4;

    // New seize
    bic.biSizeImage = (bic.biWidth * sizeof(RGBTRIPLE) + paddingc) * abs(bic.biHeight);
    bfc.bfSize = bf.bfSize - bi.biSizeImage + bic.biSizeImage;

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        //to ultiply times the hight
        for (int j = 0; j < key; j++)
        {
            // iterate over pixels in scanline
           for(int b = 0; b < bi.biWidth; b++ )
            {
                RGBTRIPLE triple;

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

                // multiplies times the width
                for(int y = 0; y < key; y++)
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            } 
            // Add a new padding
            for (int f = 0; f < paddingc; f++)
                fputc(0x00, outptr);

            // Back to the begining
            if (j < key - 1)
                fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);      
        }

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

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

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

I think I found your problem: bic.biHeight = bi.biWidth * key;

If it isn't immediately clear what is wrong, read it very, very carefully.

I'm guessing that you lucked out partially because the input files are square, so the horizontal and vertical dimensions are the same absolute value.

The reason it was upside-down is this: the sign of bi.biHeight determines whether the file is painted top-down (negative) or bottom-up (positive). In our cases, it's supposed to be negative.

Also, I think you have a second issue with padding. Since you didn't ask about it, I'll just say that you need to look at ALL of your code and it should jump out at you. If you don't find it, you can always post another question.

You must log in to answer this question.

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