1
int pastwidth = bi.biWidth;
int pastHeight = bi.biHeight;
int padding =  (4 - (SCALE * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biWidth = SCALE;
bi.biHeight = SCALE;
bi.biSizeImage = ((SCALE+padding*SCALE+padding)*24);
bf.bfSize = bi.biSizeImage + 54;

// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// determine padding for scanlines
bi.biWidth = pastwidth;
bi.biHeight = pastHeight;
// iterate over infile's scanlines
for (int i = 0; i < abs(newHeight); i++)
{
    for(int o=0;o<SCALE;o++)
    {

fseek(inptr, 54 + bi.biWidth * i, SEEK_SET);
    // iterate over pixels in scanline
    for (int j = 0; j < newWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
    for(int a=0;a<SCALE;a++)
    {
        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }
    }
    }

    // skip over padding, if any
    fseek(inptr, padding, SEEK_SET);

    // 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);

// that's all folks
return 0;
}

AlRIGHT ALMOST EVERYTHING IS CHANGED

Hello cs50 people. I'm pretty close to the real solution, I feel it. So It produces SOLID GREEN BOX(with no space in it). I believe it's about fseek but dont know how to correct it :[.

1 Answer 1

2

I think you need to spend some time to go back and review the pset instructions, shorts videos and class material before refining your code. While doing so, here are some suggestions.

Headers: While it's possible to work with a single set of headers (bf and bi), it's much safer to make two sets - one to preserve the input file's headers and a second set to change/update vars in the headers for the output file. You will also need certain variables from both the old and the new headers to process the image.

Padding: The size of the padding used in the input file and the output file have no simple relationship. Simply put, you will need to calculate the padding for both separately. You can't just set one equal to the other or multiply the input padding by the scale to get the output padding.

Scaling: You seem to have taken the approach of coding everything by multiplying by 3, perhaps with the intent of replacing 3 with the scaling factor later. This is not a good approach. For one thing, you're just hard coding 3, everywhere. It will be easy to miss one or more of the 3's when you update later. At the very least, you should have used a definition statement like #declare SCALE 3 so that it could be universally updated simply by changing the 3 in one place. However, it would make much more sense to go ahead and code for a variable that would get the multiplier from the parameter list at execution time.

Use of intermediate vars unnecessary: Look at the following code:

int newWidth = bi.biWidth * 3;
...
bi.biWidth = newWidth;

This needlessly creates an extra var, newWidth, to multiply bi.biWidth by 3. Programming is not like math. The equals sign is an assignment operator, not an equality operator. Instead, you could simply do this:

bi.biWidth= bi.biWidth * 3;

It does the assignment in one step and eliminates the creation of an unnecessary var.

There may well be other issues, but this will get you going again. If you get stuck again, try a new question with the updated code posted there.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • How's the new code ? :}
    – log101
    Commented Aug 3, 2016 at 15:58

You must log in to answer this question.

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