I've been able to resize images correctly (I checked several sizes and images and apparently everything works), but check50 complains saying that it doesn't resize correctly. I think the problem is in writing the headers, but I'm not sure what's wrong with the code.
Here's my code:
int main(int argc, char* argv[])
{ // ensure proper usage if (argc != 4) { printf("Usage: ./copy new_size infile outfile\n"); return 1; }
// store new size in int
int new_size = atoi(argv[1]);
// check if the value is between 1 and 100
if (new_size < 1 || new_size > 100)
{
printf("Resize value should be 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)
{
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;
}
// Store previous width and height
int original_width = bi.biWidth;
int original_height = abs(bi.biHeight);
// Determine the new width and height
bi.biWidth = bi.biWidth * new_size;
bi.biHeight = bi.biHeight * new_size;
// determine padding for scanlines
int padding = (4 - (original_width * sizeof(RGBTRIPLE)) % 4) % 4;
int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + 54;
// Determine the new size of the new image
bi.biSizeImage = bf.bfSize - 54;
// 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; i < original_height; i++)
{
// To know where to come back
int current_position = ftell(inptr);
for (int j = 0; j < new_size; j++)
{
// Detect current position so we can go back to resize the height
fseek(inptr, current_position, SEEK_SET);
// iterate over pixels in scanline
for (int k = 0; k < original_width; k++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for (int l = 0; l < new_size; l++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// Add new padding if necessary
for (int k = 0; k < new_padding; k++)
{
fputc(0x00, outptr);
}
}
// skip over padding, if any
fseek(inptr, padding, SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}
I have also tried with:
// Determine new size of the resized bitmap file
bf.bfSize = ((bi.biWidth * new_size) * (abs(bi.biHeight) * new_size) * 3) + new_padding + 54;
which should be more correct actually because that why I'm including the new padding, but it doesn't work either.
Any ideas? Thanks a lot!
Pablo
peek
,xxd
, anddiff
as suggested by the specs?