Reputation: 25
I figured out how to make an image bigger, but for the life of me I cant figure out how to make it smaller. The current code will make a bitmap the right size but fill it with random colors. The current code makes sense to me, i think the problem might be with the headers or the padding. Please Help! Original vs output
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize f infile outfile\n");
return 1;
}
/* GET FACTOR */
float f = atof( argv[1]);
if (f < 0.0 || f > 100.0)
{
fprintf(stderr, "The factor is out of bounds\n");
return 5;
}
//check if we are making it bigger
bool big = true;
int factor = 0;
if ( f <= 1.0)
{
big = false;
factor = floor(1/f);
}
else
{
big = true;
factor = floor(f);
}
/**/
// 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, bfn;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfn=bf;
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,bin;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bin=bi;
// 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;
}
/*Original pad*/
int oldpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
/**/
/*CHANGE THE INFOHEADER W&H SIZE*/
bin.biWidth= floor(bi.biWidth * f);
bin.biHeight= floor(bi.biHeight * f);
/*NEW PADDING*/
int padding = (4 - (bin.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
/*GET NEW SIZE*/
bin.biSizeImage = ((sizeof(RGBTRIPLE) * bin.biWidth) + padding) * abs(bin.biHeight);
bfn.bfSize = bin.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
/**/
// write outfile's BITMAPFILEHEADER
fwrite(&bfn, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bin, sizeof(BITMAPINFOHEADER), 1, outptr);
if(big)
{
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
for(int w = 0; w < factor; w++)
{
// temporary storage
RGBTRIPLE triple;
// iterate over pixels in scanline
for (int j = 0; j < bi.biWidth; j++)
{
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
for(int x = 0 ; x < factor ; x++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
fseek(inptr, -1*sizeof(RGBTRIPLE)*bi.biWidth, SEEK_CUR);
}
// skip over padding, if any
fseek(inptr, sizeof(RGBTRIPLE)*bi.biWidth+oldpadding, SEEK_CUR);
}
}
else
{
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
// 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);
if(i%factor==0&&j%factor==0)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
// skip over padding, if any
fseek(inptr, oldpadding, 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);
// success
return 0;
}
`
Upvotes: 0
Views: 1382
Reputation: 4925
The issue I found with your code was that iterating over the source width/height and trying to read/write as you go would end up writing more data than it should, and potentially getting the file pointers out of sync.
As an example, with a 512x512 source image and a factor of 3 you end up writing extra pixels per line, both because 512 * 0.33 is not the same as 512 / 3, and 512 % 3 != 0;
The way I would normally do it is to load the data into memory and iterate over the destination width/height assigning the proper pixels from the source.
I don't have bmp.h so I included Windows.h instead, and I switched to opening the files in binary mode, since you don't want any line endings getting translated. I'm also compiling in C++ mode, so there's a few casts you may not need if you're compiling as C.
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
float f = 0.33f;
int factor = 1 / f;
FILE *inptr = fopen("infile.bmp", "rb");
FILE *outptr = fopen("outfile.bmp", "wb");
BITMAPFILEHEADER bf, bfn;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bfn = bf;
BITMAPINFOHEADER bi, bin;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
bin = bi;
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;
}
bin.biWidth *= f;
bin.biHeight *= f;
int srcStride = (bi.biWidth * sizeof(RGBTRIPLE) + 3) & ~3;
int dstStride = (bin.biWidth * sizeof(RGBTRIPLE) + 3) & ~3;
bin.biSizeImage = dstStride * abs(bin.biHeight);
bfn.bfSize = bin.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fwrite(&bfn, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bin, sizeof(BITMAPINFOHEADER), 1, outptr);
char* srcData = (char*)calloc(1, bi.biSizeImage);
fread(srcData, bi.biSizeImage, 1, inptr);
char* dstData = (char*)calloc(1, bin.biSizeImage);
for (int i = 0, binHeight = abs(bin.biHeight); i < binHeight; i++)
{
RGBTRIPLE* src = (RGBTRIPLE*)&srcData[srcStride * i * factor];
RGBTRIPLE* dst = (RGBTRIPLE*)&dstData[dstStride * i];
for (int j = 0; j < bin.biWidth; j++)
{
dst[j] = src[j * factor];
}
}
fwrite(dstData, bin.biSizeImage, 1, outptr);
fclose(inptr);
fclose(outptr);
free(srcData);
free(dstData);
return 0;
}
Upvotes: 1