I have similar question as there was asked, but can't find answer.
Here is my code:
/**
* copy.c
*
* Computer Science 50
* Problem Set 4
*
* Copies 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) // change for 4 command-line arguments
{
printf("Usage: ./resize n infile outfile\n");
return 1;
}
// remember filenames
int sf = atoi(argv[1]); // size factor for resizing
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;
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;
}
// create structs that will be written in output file
BITMAPFILEHEADER bf_out;
bf_out = bf;
BITMAPINFOHEADER bi_out;
bi_out = bi;
bi_out.biWidth = bi.biWidth * sf;
bi_out.biHeight = bi.biHeight * sf;
// determine padding for scanlines
int padding = (4 - (bi_out.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// determine padding for scanlines for input file
int padding_inp = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
/**
* See http://cs50.stackexchange.com/questions/14016/pset4-resize-c-bfsize-bisizeimage-formulas for understand of values in BITMAPINFOHEADER
* */
bi_out.biSizeImage = bi_out.biWidth * abs(bi_out.biHeight) * sizeof(RGBTRIPLE) + bi_out.biHeight * padding * sizeof(BYTE);
bf_out.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf_out, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi_out, sizeof(BITMAPINFOHEADER), 1, outptr);
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
//iterate sf times through scanlines
for (int r = 0; r < sf; r++ )
{
// 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);
// iterate sf times through pixels
for (int p = 0; p < sf; p++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, -( bi.biWidth * sizeof(RGBTRIPLE) + padding_inp) , 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;
}
As a result of this programm I get improper img and error:
resize.c:124:26: runtime error: negation of 12 cannot be represented in type 'unsigned long'
Could someone help me, since I don't have any idea what to do next.