Skip to main content
added 96 characters in body
Source Link
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int factor;

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

factor = atoi(argv[1]);
if (factor<0 || factor >100)
{
    fprintf(stderr, "Provide an integer between 1 and 100.\n");
    return 1;
}

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

// 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 old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPFILEHEADER new_bf;
new_bf = bf;

BITMAPINFOHEADER new_bi;
new_bi = bi;

new_bi.biHeight *= factor;
new_bi.biWidth *= factor;
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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



// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE array[new_bi.biWidth];
    int counter = 0;
    // iterate over pixels in scanline
    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 m = 0; m<factor; m++)
        {
            array[counter] = triple;
            counter++;
        }
        
        
        for (int p = 0; p < factor; p++)
        {
            fwritefor (array,int l = 0; l < sizeof(new_bi.biWidth); l++)
            {
                fwrite(&array[l], sizeof(array), 1, outptr);
            //}
 then add it back (to demonstrate how)     
            
            for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }
            
        }
    }

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

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int factor;

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

factor = atoi(argv[1]);
if (factor<0 || factor >100)
{
    fprintf(stderr, "Provide an integer between 1 and 100.\n");
    return 1;
}

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

// 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 old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPFILEHEADER new_bf;
new_bf = bf;

BITMAPINFOHEADER new_bi;
new_bi = bi;

new_bi.biHeight *= factor;
new_bi.biWidth *= factor;
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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



// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE array[new_bi.biWidth];
    int counter = 0;
    // iterate over pixels in scanline
    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 m = 0; m<factor; m++)
        {
            array[counter] = triple;
            counter++;
        }
        
        
        for (int p = 0; p < factor; p++)
        {
            fwrite(array, sizeof(new_bi.biWidth), 1, outptr);
            // then add it back (to demonstrate how)
            for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }
            
        }
    }

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

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int factor;

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

factor = atoi(argv[1]);
if (factor<0 || factor >100)
{
    fprintf(stderr, "Provide an integer between 1 and 100.\n");
    return 1;
}

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

// 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 old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPFILEHEADER new_bf;
new_bf = bf;

BITMAPINFOHEADER new_bi;
new_bi = bi;

new_bi.biHeight *= factor;
new_bi.biWidth *= factor;
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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



// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE array[new_bi.biWidth];
    int counter = 0;
    // iterate over pixels in scanline
    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 m = 0; m<factor; m++)
        {
            array[counter] = triple;
            counter++;
        }
        
        
        for (int p = 0; p < factor; p++)
        {
            for (int l = 0; l < sizeof(new_bi.biWidth); l++)
            {
                fwrite(&array[l], sizeof(array), 1, outptr);
            }
            
            
            for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }
            
        }
    }

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

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
Source Link

PSET4 - Resize (less)

I am unable to figure out what I am lacking or doing wrong in my code. The output image large.bmp becomes just some random colors. Hopefully, someone can guide me here.

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

#include "bmp.h"

int factor;

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

factor = atoi(argv[1]);
if (factor<0 || factor >100)
{
    fprintf(stderr, "Provide an integer between 1 and 100.\n");
    return 1;
}

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

// 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 old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPFILEHEADER new_bf;
new_bf = bf;

BITMAPINFOHEADER new_bi;
new_bi = bi;

new_bi.biHeight *= factor;
new_bi.biWidth *= factor;
int new_padding = (4 - (new_bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

new_bi.biSizeImage = ((sizeof(RGBTRIPLE) * new_bi.biWidth) + new_padding) * abs(new_bi.biHeight);
new_bf.bfSize = new_bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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



// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE array[new_bi.biWidth];
    int counter = 0;
    // iterate over pixels in scanline
    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 m = 0; m<factor; m++)
        {
            array[counter] = triple;
            counter++;
        }
        
        
        for (int p = 0; p < factor; p++)
        {
            fwrite(array, sizeof(new_bi.biWidth), 1, outptr);
            // then add it back (to demonstrate how)
            for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }
            
        }
    }

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

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;

}