0

I am on pset4 and working on resize, less comfortable. I chose to follow the "rewrite" method but it seems that I am going wrong somewhere and I have no idea what to do. Any advice or help is most needed and welcome.

This is the loop part of my code. I have not edited my comments, so please ignore those. -

int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

LONG newWidth = bi.biWidth * number;
LONG newHeight = abs(bi.biHeight) * number;
int newPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bii.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + newPadding) * abs(newHeight);
bff.bfSize = bii.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    RGBTRIPLE triples[newWidth];

    // 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 m = 0; m < number; m++)
        {
            triples[m + (j * number)] = triple;
        }

    }

    for (int l = 0; l < number; l++)
    {
        // write RGB triple to outfile
        fwrite(triples, sizeof(triples), 1, outptr);

        for (int k = 0; k < newPadding; k++)
        {
            // then add padding back (to demonstrate how)
            fputc(0x00, outptr);
        }
    }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}

EDIT My outptr keeps giving me an "invalid or unsupported image format" result. Is there something wrong with my header edits? -

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bff;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi, bii;
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;
}

// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) * abs(bi.biHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

LONG newWidth = bi.biWidth * number;
LONG newHeight = abs(bi.biHeight) * number;
int newPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bii.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + newPadding) * abs(newHeight);
bff.bfSize = bii.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

// write outfile's BITMAPINFOHEADER
fwrite(&bii, sizeof(BITMAPINFOHEADER), 1, outptr);
2
  • Please edit the question and add all the code from the initial header read forward.
    – Cliff B
    Commented Apr 28, 2018 at 10:38
  • @CliffB I have done it. Does that help?
    – Naomi
    Commented Apr 28, 2018 at 10:47

1 Answer 1

1

I do not see where you are writing the new BITMAPFILEHEADER or BITMAPINFOHEADER to the outptr with fwrite(...) this is what caused me issue at first because I wasn't writing it properly.

These things have to be written to the file first as this is what tells the computer that it is a bitmap image. I nested all 3 loops and wrote the triples as I went, but your storage of the RGBTRIPLES in triples looks sound.

2
  • I tried following your suggestion; however, now, my outptr is giving a result of invalid or unsupported image format.
    – Naomi
    Commented Apr 28, 2018 at 19:09
  • I have added my edit to the question. Is there something wrong with the way I edited it?
    – Naomi
    Commented Apr 28, 2018 at 19:15

You must log in to answer this question.

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