Thanks for the help it is resizing the picture correctly now I think but my coparison with staff version gives me lots of fails. :
:) resize.c and bmp.h exist
:) resize.c compiles
:( doesn't resize 1x1-pixel BMP when n is 1
\ expected an exit code of 0, not output of "\n3\n\n3\n\n3\n90"
:( resizes 1x1-pixel BMP to 2x2 correctly when n is 2
\ expected an exit code of 0, not output of "\n3\n\n2\n\n3\n174"
:( resizes 1x1-pixel BMP to 3x3 correctly when n is 3
\ expected an exit code of 0, not output of "\n3\n\n1\n\n3\n306"
:( resizes 1x1-pixel BMP to 4x4 correctly when n is 4
\ expected an exit code of 0, not output of "\n3\n\n0\n\n3\n486"
:( resizes 1x1-pixel BMP to 5x5 correctly when n is 5
\ expected an exit code of 0, not output of "\n3\n\n3\n\n3\n774"
:( resizes 2x2-pixel BMP to 4x4 correctly when n is 2
\ expected an exit code of 0, not output of "\n2\n\n0\n\n6\n486"
No idea why :(
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmp.h"
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 4 )
{
printf("Usage: ./copy resize infile outfile\n");
return 1;
}
// remember filenames
char* resizeBy = argv[1];
int counter = 0;
char* infile = argv[2];
char* outfile = argv[3];
// char array[3];
int d;
for(int i = 0; resizeBy[i] > 0; i++){
counter++;
}
d = atoi(argv[1]);
if(d < 0 || d > 100)
{
printf("Try 1 - 100\n");
return 6;
}
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;
//int
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
int oriWidth = bi.biWidth;
int oriHeight = bi.biHeight;
bi.biWidth *= d;
bi.biHeight *= d;
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((bi.biWidth ) * ((abs(bi.biHeight))) * sizeof(RGBTRIPLE)) + padding * abs(bi.biHeight);
int oPadding = (4 - (oriWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bf.bfSize = 54 + bi.biSizeImage;
// 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;
}
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// determine padding for scanlines
printf("\n%d\n", oPadding);
printf("\n%d\n", padding);
printf("\n%d\n", oriWidth);
printf("%d",bf.bfSize);
// printf("%d", bi.biHeight);
// iterate over infile's scanlines
for (int i = 0, oHeight = abs(oriHeight); i < oHeight; i++)
{
for(int k = 0; k < d ; k++)
{
//get back to the beginning of the file pointer
fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + i *(oriWidth * sizeof(RGBTRIPLE) + oPadding ), SEEK_SET);
// iterate over pixels in scanline
for (int j = 0; j < oriWidth ; j++)
{
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// write RGB triple to outfile
for(int f = 0; f < d; f++)
{
fwrite(&triple, sizeof(RGBTRIPLE) ,1,outptr);
}
}
for(int t = 0; t < padding ; t++){
fputc(0x00, outptr);
}
}
fseek(inptr,padding,SEEK_CUR);
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// that's all folks
return 0;
}