I'm taking the XED course "CS50" and now i'm stuck at pset4.
So far i have managed to resize the images footprint and managed to resize one of the given images by a factor of 2 and 4, but with a factor of 3 it does not work. and i have yet to get the padding to work on images that need that. Here are some of the images of the results of the program
Under are my 2 source codes. Have written one using the recopy method and one using the rewrite method. but got the same problem in both programs.
Recopy method:
/*
"recopy"-method
CS50 2017 pset4 - resize less
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 1;
}
if (atoi(argv[1] < 0 || 100 < atoi(argv[1]))
{
fprintf(stderr,
"Please let the resize facor be between 0 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;
}
int n = atoi(argv[1]);
// 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;
}
//gives new width
bi.biWidth *= n;
//gives new height
bi.biHeight *= n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//gives new image size
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding)
* abs(bi.biHeight);
//gives new file size
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//Offset to move cursor in input file for reading. fseek();
long offset = bi.biWidth * sizeof(RGBTRIPLE) + padding;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
//iterate to print n-lines
for (int m = 0; m < n;m++ )
{
// 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 k = 0; k < n; k++)
{
// write RGB triple to outfile
fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}
}
//move cursor back to start of line as long as it is not the
//last run of writing the line.
if (m != n - 1)
{
//Moves the cursor "-offset" bytes back in the input
//file to read from the beginning of the line.
fseek(inptr, -offset, SEEK_CUR);
}
}
// skip over padding, if any
fseek(inptr, padding, 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;
}
Then the rewrite method:
/*
"rewrite" method
CS50 2017 pset4 - resize less
*/
#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: ./resize n infile outfile\n");
return 1;
}
if (atoi(argv[1] < 0 || 100 < atoi(argv[1]))
{
fprintf(stderr, "Please let the resize facor be
between 0 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;
}
int n = atoi(argv[1]);
// 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;
}
//gives new width
bi.biWidth *= n;
//gives new height
bi.biHeight *= n;
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
//gives new image size
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding)
* abs(bi.biHeight);
//gives new file size
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
//Initiates an array of triples with size bi.biwidth
* sizeof(RGBTRIPLE) bytes.
RGBTRIPLE array[bi.biWidth * sizeof(RGBTRIPLE)];
// iterate over infile's scanlines
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);
//Saves each pixel (3 bytes) in an array.
array[j] = triple;
}
//Iterate over how many lines to write
for (int l = 0; l < n; l++)
{
//Iterate over line to write the pixels
for (int m = 0; m < bi.biWidth; m++)
{
//write every pixel g times.
for (int g = 0; g < n; g++)
{
fwrite(&array[m], sizeof(RGBTRIPLE), 1, outptr);
}
}
}
// skip over padding, if any
fseek(inptr, padding, 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;
}
A lot(!) of code, i know, that's why i'm so frustrated when i got 2 methods working the same way with the same faults... I can provide the pictures in question if needed but the code should work on all 24-bit uncompressed BMP 4.0 images. resizing a 12*12px by a factor of 2 and 4 is OK, but 3 fails... Also an 3*3px image fails any factors and the smiley face of 8*8px also fails. Thank you in advance!