0

My code compiles, but when I execute this I get: runtime error: index 4 out of bounds for type 'RGBTRIPLE [newbi.biWidth]'

Where does 4 come from? Is it the number of arguments?? Thanks for any help.

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char* argv[])
{
    // set arg 1 as number by which to multiply
    int factor = atoi(argv[1]);

    // remember filenames
    char* infile = argv[2];
    char* outfile = argv[3];

    // ensure proper usage
    if (argc != 4 || factor < 1 || factor > 100)
    {
        fprintf(stderr, "Usage: ./resize number infile outfile\n");
        return 1;
    }

    // open input file
    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;
    }


    //initialize old and new file header variables
    BITMAPFILEHEADER bf, newbf;
    // read infile's BITMAPFILEHEADER
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    //initialize old and new info header variables
    BITMAPINFOHEADER bi, newbi;
    // read infile's BITMAPINFOHEADER
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // Get Original file's padding
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    //save the old file header and info header as new
    newbf = bf;
    newbi = bi;

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (newbf.bfType != 0x4d42 || newbf.bfOffBits != 54 || newbi.biSize != 40 ||
        newbi.biBitCount != 24 || newbi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // Update width, and height in Bitmap info header
    int width = bi.biWidth * factor;
    int height = bi.biHeight * factor;

    // determine padding for scanlines
    int newPadding =  (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;

    // Update size in Bitmap info header
    // out_bi.biSizeImage = out_bi.biWidth * abs(out_bi.biHeight) *
    //     sizeof(RGBTRIPLE) + newPadding * abs(out_bi.biHeight);
    newbi.biSizeImage = width * abs(height) *
        sizeof(RGBTRIPLE) + newPadding * abs(height);

    // Update Size in Bitmap File Header
    newbf.bfSize = newbf.bfOffBits + newbi.biSizeImage;

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

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

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(newbi.biHeight); i < biHeight; i++)
    {
        // Set up 'pixel' array to store pixels in
        RGBTRIPLE row[newbi.biWidth];
        int pixel = 0;

    //     for (int j = 0; j < bin.biWidth; j ++)
    // {
    //     fwrite(array[j], sizeof(RGBTRIPLE), 1, outptr);
    // }

        // iterate over pixels in scanline
        for (int k = 0; k < bi.biWidth; k++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // Repeat each pixel n times
            for (int p = 0; p < factor; p++, pixel++)
            {
                row[pixel] = triple;
            }
        }

        // repeat each row n times
        for (int r = 0; r < factor; r++)
        {
            // Write row to out file
            fwrite(&row, sizeof(row), 1, outptr);

            // Add padding to new file
            for (int p = 0; p < newPadding; p++)
            {
                fputc(0x00, outptr);
            }
        }

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // that's all folks
    return 0;
}
0

The 4 is the value in newbi.biWidth.

Looking over your code, something struck me. Why are newbi.biWidth and newbi.biHeight unchanged from the originals?

Also, have you used peek and xxd to look at raw file data for both the input and output files to verify the headers are correct when factor = 2? What does the output image data look like compared to the input?

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

1
  • Thank you. I changed those and I'm not getting an error message any more. But it still doesn't resize correctly. The image changes and it does resize but it doesn't change to size. What am I looking for in peek? I didn't see this in the lecture video. The header files look the same for large.bmp and small.bmp in peek. What am I looking for in xxd? – Marie Urbina May 29 '18 at 17:24

You must log in to answer this question.

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