我正在编写一个c程序,现在已经调试了几天了。我想我已经找到了问题所在。
下面是一段代码和生成的输出:
// now setting the new RGB values
int blue_val = (long) round(sqrt(((gx_b * gx_b) + (gy_b * gy_b))));
temp[pixel_i][pixel_j].rgbtBlue = blue_val;
printf("%i %in", blue_val, temp[pixel_i][pixel_j].rgbtBlue);
这是输出:
322 66
396 140
322 66
262 6
60 60
295 39
322 66
385 129
322 66
注意:我还设置了绿色和红色值,它们在结构中写得很好。它只是我在这里打印的蓝色值由于某种原因没有写出来。为了更清楚地了解情况,以下是后面的其余代码:
temp[pixel_i][pixel_j].rgbtGreen = (long) round(sqrt(((gx_g * gx_g) + (gy_g * gy_g))));
temp[pixel_i][pixel_j].rgbtRed = (long) round(sqrt(((gx_r * gx_r) + (gy_r * gy_r))));
if (temp[pixel_i][pixel_j].rgbtBlue > 255)
temp[pixel_i][pixel_j].rgbtBlue = 255;
if (temp[pixel_i][pixel_j].rgbtGreen > 255)
temp[pixel_i][pixel_j].rgbtGreen = 255;
if (temp[pixel_i][pixel_j].rgbtRed > 255)
temp[pixel_i][pixel_j].rgbtRed = 255;
// printf("R:%i G:%i B:%in", temp[pixel_i][pixel_j].rgbtRed, temp[pixel_i][pixel_j].rgbtGreen, temp[pixel_i][pixel_j].rgbtBlue);
return;
问题是结构 rgbtBlue 中的变量是 BYTE 类型,因此不允许大于 255 的赋值。
溶液:
// now setting the new RGB values taking care to assign 255 for values greater than 255
int bval = (int) round(sqrt(((gx_b * gx_b) + (gy_b * gy_b))));
temp[pixel_i][pixel_j].rgbtBlue = (bval > 255) ? 255 : bval;
int gval = (int) round(sqrt(((gx_g * gx_g) + (gy_g * gy_g))));
temp[pixel_i][pixel_j].rgbtGreen = (gval > 255) ? 255 : gval;
int rval = (int) round(sqrt(((gx_r * gx_r) + (gy_r * gy_r))));
temp[pixel_i][pixel_j].rgbtRed = (rval > 255) ? 255 : rval;
发布后几秒钟就想通了。.