0

I am getting a segmentation fault when I run this program, and am fairly sure that the problem is in the third for loop, where I try to copy the bytes from *pixel to *line, duplicating each byte 'resize' times. In the loop I have two lines of code that I tried to use to implement Zamyla's array method, but both resulted in seg faults. Any ideas on what's happening? Thanks.

// storage of bytes-to-write
RGBTRIPLE* line = malloc(bi.biWidth * sizeof(RGBTRIPLE));  

// iterate over infile's scanlines    
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight / resize; i++)
{
    // iterate over pixels in scanline       
    for (int j = 0; j < bi.biWidth / resize; j++)
    {         
        // read RGB triple from infile
        // storage of bytes read
        RGBTRIPLE* pixel = malloc(sizeof(RGBTRIPLE));
        fread(pixel, sizeof(RGBTRIPLE), 1, inptr);
        fseek(inptr, padding, SEEK_CUR);

        //copying bytes read (stored in *pixel) to bytes-to-write (stored in *line)
        for (int k = 0; k < resize; k++)
        {
            //memcpy(line + (j * resize + k) * sizeof(RGBTRIPLE), pixel, sizeof(RGBTRIPLE));
            //*(line + (j * resize + k) * sizeof(RGBTRIPLE)) = *pixel;
        }            

        free(pixel);
    }
  ...
2

Without seeing the rest of your code, it looks like you aren't allocating enough memory in line, here:

// storage of bytes-to-write
RGBTRIPLE* line = malloc(bi.biWidth * sizeof(RGBTRIPLE));  

In the example code, bi is the bitmap info header for the input file. Unless you changed things around a bit (which is also likely), your code would be allocating memory for line based on the input image size, and not the output image size.

Something that might make this easier, is to use local stack variables instead of using malloc to dynamically allocate memory on the heap.

For example:

// storage of bytes-to-write
// allocate a static array of the required size on the stack 
RGBTRIPLE line[bi.biWidth];  

// iterate over infile's scanlines    
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight / resize; i++)
{
    // iterate over pixels in scanline       
    for (int j = 0; j < bi.biWidth / resize; j++)
    {         
        // read RGB triple from infile
        // storage of bytes read
        RGBTRIPLE pixel;

        // note the use of the 'address of' operator
        // this basically gets a pointer to the pixel so that read can write to it
        fread(&pixel, sizeof(RGBTRIPLE), 1, inptr);

        fseek(inptr, padding, SEEK_CUR);

        //copying bytes read (stored in *pixel) to bytes-to-write (stored in *line)
        for (int k = 0; k < resize; k++)
        {
            // set the value in the line array
            // note each 'unit' is an RGBTRIPLE so we don't need to multiply by the size of the struct
            line[j * resize + k] = pixel
        }            

        // free() is not required because the data was not malloc'd
    }

    ...

You must log in to answer this question.

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