Я написал код для функции размытия фильтра pset. Однако всякий раз, когда я пытаюсь скомпилировать код, я получаю сообщение об ошибке. Может кто-то увидеть, где лежит моя ошибка?
Код:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int count = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int red = 0;
int green = 0;
int blue = 0;
if (j - 1 >= 0 && j - 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255)
{
red += image[i][j + 1].rgbtRed;
green += image[i][j + 1].rgbtGreen;
blue += image[i][j + 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 <= 255 && i - 1 >= 0 && i - 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (i - 1 >= 0 && i - 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255 && i - 1 >= 0 && i - 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 <= 255 && i + 1 >= 0 && i + 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (i + 1 >= 0 && i + 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255 && i + 1 >= 0 && i + 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
image[i][j].rgbtRed = (red / count);
image[i][j].rgbtGreen = (green / count);
image[i][j].rgbtBlue = (blue / count);
}
}
return;
}
Сообщение об ошибке:
helpers.c:170:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:171:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:172:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:178:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:179:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:180:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:146:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:147:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:148:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:154:24: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:155:26: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:156:25: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
Мой ввод:
./filter -b stadium.bmp outfile.bmp
Если кому-то нужна дополнительная информация о pset, перейдите на собственную страницу CS50:
https://cs50.harvard.edu/x/2020/psets/4/filter/less/
обновленный код:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
int count = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int red = 0;
int green = 0;
int blue = 0;
if (j - 1 >= 0 & j - 1 <= 255)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255)
{
red += image[i][j + 1].rgbtRed;
green += image[i][j + 1].rgbtGreen;
blue += image[i][j + 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 <= 255 && i - 1 >= 0 && i - 1 <= 255)
{
red += image[i - 1][j - 1].rgbtRed;
green += image[i - 1][j - 1].rgbtGreen;
blue += image[i - 1][j - 1].rgbtBlue;
count++;
}
if (i - 1 >= 0 && i - 1 <= 255)
{
red += image[i - 1][j].rgbtRed;
green += image[i - 1][j].rgbtGreen;
blue += image[i - 1][j].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255 && i - 1 >= 0 && i - 1 <= 255)
{
red += image[i - 1][j + 1].rgbtRed;
green += image[i- 1][j + 1].rgbtGreen;
blue += image[i- 1][j + 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 <= 255 && i + 1 >= 0 && i + 1 <= 255)
{
red += image[i + 1][j - 1].rgbtRed;
green += image[i + 1] [j - 1].rgbtGreen;
blue += image[i + 1][j - 1].rgbtBlue;
count++;
}
if (i + 1 >= 0 && i + 1 <= 255)
{
red += image[i+ 1][j ].rgbtRed;
green += image[i+ 1][j].rgbtGreen;
blue += image[i+ 1][j].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 <= 255 && i + 1 >= 0 && i + 1 <= 255)
{
red += image[i + 1][j + 1].rgbtRed;
green += image[i+ 1][j + 1].rgbtGreen;
blue += image[i+ 1][j + 1].rgbtBlue;
count++;
}
copy[i][j].rgbtRed = round(red / count);
copy[i][j].rgbtGreen = round(green / count);
copy[i][j].rgbtBlue = round(blue / count);
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = copy[i][j].rgbtRed;
image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
}
}
return;
}
обновление 2:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float red = 0;
float green = 0;
float blue = 0;
int count = 0;
if (j >= 0 )
{
red += image[i][j].rgbtRed;
green += image[i][j].rgbtGreen;
blue += image[i][j].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 < width- 1)
{
red += image[i][j - 1].rgbtRed;
green += image[i][j - 1].rgbtGreen;
blue += image[i][j - 1].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 < width - 1)
{
red += image[i][j + 1].rgbtRed;
green += image[i][j + 1].rgbtGreen;
blue += image[i][j + 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 < width - 1 && i - 1 >= 0 && i - 1 < height - 1)
{
red += image[i - 1][j - 1].rgbtRed;
green += image[i - 1][j - 1].rgbtGreen;
blue += image[i - 1][j - 1].rgbtBlue;
count++;
}
if (i - 1 >= 0 && i - 1 < height - 1)
{
red += image[i - 1][j].rgbtRed;
green += image[i - 1][j].rgbtGreen;
blue += image[i - 1][j].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 < width - 1 && i - 1 >= 0 && i - 1 < height - 1)
{
red += image[i - 1][j + 1].rgbtRed;
green += image[i- 1][j + 1].rgbtGreen;
blue += image[i- 1][j + 1].rgbtBlue;
count++;
}
if (j - 1 >= 0 && j - 1 < width - 1 && i + 1 >= 0 && i + 1 < height - 1)
{
red += image[i + 1][j - 1].rgbtRed;
green += image[i + 1] [j - 1].rgbtGreen;
blue += image[i + 1][j - 1].rgbtBlue;
count++;
}
if (i + 1 >= 0 && i + 1 < height - 1)
{
red += image[i+ 1][j ].rgbtRed;
green += image[i+ 1][j].rgbtGreen;
blue += image[i+ 1][j].rgbtBlue;
count++;
}
if (j + 1 >= 0 && j + 1 < width- 1 && i + 1 >= 0 && i + 1 < height - 1)
{
red += image[i + 1][j + 1].rgbtRed;
green += image[i+ 1][j + 1].rgbtGreen;
blue += image[i+ 1][j + 1].rgbtBlue;
count++;
}
copy[i][j].rgbtRed = round(red / count);
copy[i][j].rgbtGreen = round(green / count);
copy[i][j].rgbtBlue = round(blue / count);
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = copy[i][j].rgbtRed;
image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
}
}
return;
}
сообщение об ошибке после запуска тестового кода:
:( blur correctly filters middle pixel
expected "127 140 149\n", not "70 85 95\n"
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "81 101 65\n"
:( blur correctly filters pixel in corner
expected "70 85 95\n", not "45 72 71\n"
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "49 57 71\n61 6..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "46 60 79\n85 8..."