Pset4 (более удобный) - проблема размытия - PullRequest
0 голосов
/ 04 августа 2020

У меня возникли проблемы с компиляцией кода из-за ошибки сегментации. Я считаю, что это как-то связано с тем, как я пытаюсь освободить изображение из кучи и переназначить его на среднее значение. Я бы подумал, что это будет нормально, поскольку изображение снова будет освобождено в фильтрах. c на основе нового адреса, на который оно указывает.

Я выполнил debug50 и прошел через свой код. Я понял, что при переназначении изображения, когда изображение было указано на среднее значение, оно не проявляется после размытия функции стека. Адрес возвращается к исходному адресу изображения. Есть ли способ обойти это?

Вот мой код:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
 
    // Create a of the image array to store the average of the pixel
    RGBTRIPLE (*average)[width] = calloc(height, width * sizeof(RGBTRIPLE));
 
    // Iterate through each row
    for (int i = 0; i < height; i++)
    {
        // Iterate through each column
        // Also declare the sub row and col that will be determined based on current i,j
        for (int j = 0, x = 0, y = 0; j < width; j++)
        {
            // Declare variable parameters to determine sub for loop width and height
            // Count is used to track division after sum is taken from surrounding pixels
            float count = 0.0;
            int boxh, boxw;
 
            // ====HORIZONTAL COMPONENT====
            // Check if current row at the first row. Set x = 0 and box height i + 1
            if ((i - 1) < 0)
            {
                x = i;
                boxh = i + 1;
            }
 
            // If current row is not at the boundary, set x to 1 row before and box height + 1
            else if ((i + 1) == height)
            {
                x = i - 1;
                boxh = i;
            }
 
            // If current row is the last row, set x to 1 row before and box height = i
            else
            {
                x = i - 1;
                boxh = i + 1;
            }
 
            // ====VERTICAL COMPONENT====
            // Check if current col at the first col. Set y = 0 and box height j + 1
            if ((j - 1) < 0)
            {
                y = j;
                boxw = j + 1;
 
            }
            // If current col is not at the boundary, set y to 1 col before and box height + 1
            else if ((j + 1) == width)
            {
                y = j - 1;
                boxw = j;
            }
 
            // If current col is the last col, set y to 1 col before and box height = j
            else
            {
                y = j - 1;
                boxw = j + 1;
            }
 
            // Iterate through subset box based on parameters determined above
            for (; x <= boxh; x++)
            {
                for (; y <= boxw; y++)
                {
                    // Take summation of each RBG value and store it in the current pixel on average array
                    average[i][j].rgbtRed += image[x][y].rgbtRed;
                    average[i][j].rgbtGreen += image[x][y].rgbtGreen;
                    average[i][j].rgbtBlue += image[x][y].rgbtBlue;
 
                    // Count number of pixels used
                    count++;
                }
            }
 
            // After summation is complete, divide sum of each RBG value and divide by count
            average[i][j].rgbtRed = round(average[i][j].rgbtRed / count);
            average[i][j].rgbtGreen = round(average[i][j].rgbtGreen / count);
            average[i][j].rgbtBlue = round(average[i][j].rgbtBlue / count);
 
        }
    }
 
    // Free current image array from heap
    free(image);
 
    // Set image pointer to average array
    image = average;
 
    return;
}

Вот ошибка, которую я получаю:

~/pset4/filter/ $ ./filter -b images/yard.bmp out.bmp 
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==661==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fd256615010 (pc 0x7fd255674c3c bp 0x000000000708 sp 0x7ffc319b62e8 T661)
==661==The signal is caused by a READ memory access.
    #0 0x7fd255674c3b  (/lib/x86_64-linux-gnu/libc.so.6+0x18ec3b)
    #1 0x7fd255571993  (/lib/x86_64-linux-gnu/libc.so.6+0x8b993)
    #2 0x7fd255565976  (/lib/x86_64-linux-gnu/libc.so.6+0x7f976)
    #3 0x423456  (/home/ubuntu/pset4/filter/filter+0x423456)
    #4 0x7fd255507b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #5 0x402dd9  (/home/ubuntu/pset4/filter/filter+0x402dd9)
 
UndefinedBehaviorSanitizer can not provide additional info.
==661==ABORTING

Заранее спасибо!

...