Any Ideas as to what might be wrong???
Printable View
Any Ideas as to what might be wrong???
Whoops, forgot to add that on the last post... here it is:
Code:/****************************************************************************
* resize.c
*
* Computer Science 50
* Problem Set 4
*
* Resizes 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: copy N infile outfile\n");
return 1;
}
// remember filenames
char* nchar = argv[1];
char* infile = argv[2];
char* outfile = argv[3];
int timeswriten = 0;
//convert nchar to an int
int n = atoi (nchar);
if (n < 1 || n > 100)
{
printf("Please try again with a positive number less then 100\n");
return 1;
}
// 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;
}
//save original image headder
int oldheight = bi.biHeight;
int oldwidth = bi.biWidth;
int oldpadding = (4-(oldwidth*sizeof(RGBTRIPLE)) % 4) % 4;
bi.biHeight = oldheight *n;
bi.biWidth = oldwidth *n;
int padding = (4 -(bi.biWidth * sizeof(RGBTRIPLE))%4)%4;
bi.biSizeImage = (bi.biWidth * sizeof(RGBTRIPLE) + padding) * abs(bi.biHeight);
bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bi.biSizeImage;
// write outfile's BITMAPFILEHEADER and BITMAPINFOHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
RGBTRIPLE *buffer = malloc(sizeof(RGBTRIPLE) * bi.biWidth);
// iterate over infile's scanlines
for (int i = 0; i < abs(oldheight); i++)
{
// iterate over pixels in scanline
for (int j = 0; j < oldwidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile and store in buffer
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//iterate over each pixel factor times
for (int k = 0; k < n; k++)
{
buffer[timeswriten++] = triple;
}
timeswriten = 0;
}
// skip over any input padding
fseek(inptr, oldpadding, SEEK_CUR);
// print each row from buffer factor times
for (int r = 0; r < n; r++)
{
// write RGB triple to outfile
fwrite(buffer, sizeof(RGBTRIPLE), bi.biWidth, outptr);
// write padding to outfile
for (int p = 0; p < padding; p++)
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}
You read one pixel, fill up the buffer and then reset the buffer counter to zero, thus overwriting the buffer at the next iteration.Code:// iterate over pixels in scanline
for (int j = 0; j < oldwidth; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile and store in buffer
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//iterate over each pixel factor times
for (int k = 0; k < n; k++)
{
buffer[timeswriten++] = triple;
}
timeswriten = 0;
}
Bye, Andreas
Oh.... I shouldn't be resetting the buffer counter should I..?..?... I have removed the reset, and it is even closer to working (so I assume I made the correct change)
Now when I input a very small image of a red smiley face and give it a resize factor of 100, It outputs a red band down the middle of a white canvas, although It is definitely a LOT larger, as it is supposed to be. It is not a smiley face however, so there is still something not quite correct..... Again, any help would be GREAT!
NOTE: I have not reattached my code as all I changed was removing the "timeswriten = 0" line
You have to reset the counter but you did it at the wrong place. The indentation of that line is right but it should be outside the for-loop. IMHO the best place would be before the for-loop instead of after it. (I'm talking about the outer for-loop with the "j" counter in case that's not clear).
Bye, Andreas
IT WORKS!!!! PUTTING THE RESET OUTSIDE OF THE LOOP MADE IT WORK!!!!!!!!!!!!!!!!!!!!!!!
Thanks, everyone so much for all the help you have given me!!!
Now, If you could take a look at my other thread, there is another program I am working on :wink:: http://cboard.cprogramming.com/c-pro...-raw-file.html