0

I'm Samuel and I have been working on Problem Set 4 and I have been working a long time on resize. I have refined it several times but can't find what is wrong with my code. Could you please help me?

Thank You!

    int main(int argc, char *argv[])
   {
        if (argc != 4)
        {
    fprintf(stderr, "Usage: ./copy infile outfile\n");
    return 1;
}

int n = argv[1];

if (n > 0 || n < 101)
{
    printf("The scale must be a positive integer no larger than 100.");
    return 2;
}


char *infile = argv[2];
char *outfile = argv[3];


FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 3;
}


FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
    fclose(inptr);
    fprintf(stderr, "Could not create %s.\n", outfile);
    return 4;
}


BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);


BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

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 5;
}


int Width_0 = bi.biWidth;
int Height_0 = bi.biHeight;
int Width_1 = bi.biWidth;
int Height_1 = bi.biHeight;

int Width_1 *= n
int Height_1 *= n

int Padding_0 = (4 - (Width_0 * sizeof(RGBTRIPLE)) % 4) % 4;
int Padding_1 = (4 - (Width_1 * sizeof(RGBTRIPLE)) % 4) % 4;


bi.biSizeImage = abs(bi.biHeight) * ((bi.biWidth * sizeof (RGBTRIPLE)) 
 + padding);


bf.bfSize = bi.biSizeImage + sizeof (BITMAPFILEHEADER) + sizeof 
 (BITMAPINFOHEADER); 


fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);


fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);


RGBTRIPLE *temp = malloc(sizeof(RGBTRIPLE) * (bi.biWidth));


for (int i = 0, Height_1 = abs(Height_0); i < Height_1; i++)
{


    int counter = 0


    for (int j = 0; j < Width_0; j++)

    RGBTRIPLE triple;

    {

        RGBTRIPLE triple;


        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

         for(int count = 0; count < scale; count++)
        {
            *(temp + (counter)) = triple;
            counter++;


        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }


    fseek(inptr, padding, SEEK_CUR);



    for(int b = 0; b < scale; b++)
       {
            fwrite((temp), sizeof(RGBTRIPLE), bi.biWidth, outptr);
            for (int k = 0; k < padding; k++)
            {
                fputc(0x00, outptr);
            }
        }

    }


fclose(inptr);


fclose(outptr);

free(temp);
return 0;
}
0

argv[1] is a string. You need to parse it first, using for example atoi ("ASCII to integer").

Also, you seem to re-define Width_1 and Height_1. Declare variables only once. You need to update bi.biHeight and bi.biWidth, not just some other variables, before writing the headers.

Your code is not consistent. You use different variable names in all places.

1
  • Thank you very much, my code compiles now! :) – Samuel Hanssen Sep 29 '17 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .