简体   繁体   English

CS50 pset4 的棕褐色滤镜

[英]Sepia Filter of CS50 pset4

So I've tried writing code for sepia filter, but its not working properly for corner cases.所以我尝试为棕褐色过滤器编写代码,但它不适用于极端情况。 I dont find any issue.我没有发现任何问题。 But error is being prompted.但是提示错误。

The below image is the picture of my errors下图是我的错误图片

I tried running the program in VS code, it gives desired output.我尝试在 VS 代码中运行该程序,它给出了所需的 output。 But check50 prompts error message.但是check50提示错误信息。

void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for(int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float r = image[i][j].rgbtRed;
            float g = image[i][j].rgbtGreen;
            float b = image[i][j].rgbtBlue;

            image[i][j].rgbtRed = round(0.393*r + 0.769*g + 0.189*b);
            image[i][j].rgbtGreen = round(0.349*r + 0.686*g + 0.168*b);
            image[i][j].rgbtBlue = round(0.272*r + 0.534*g + 0.131*b);

            if (image[i][j].rgbtRed > 255)
            {
                image[i][j].rgbtRed = 255;
            }

            if (image[i][j].rgbtGreen > 255)
            {
                image[i][j].rgbtGreen = 255;
            }

            if (image[i][j].rgbtBlue > 255)
            {
                image[i][j].rgbtBlue = 255;
            }

        }
    }
    return;
}

Your sepia function is almost correct.您的sepia function 几乎是正确的。 You just missed that with你只是错过了

            image[i][j].rgbtRed = round(0.393*r + 0.769*g + 0.189*b);
            image[i][j].rgbtGreen = round(0.349*r + 0.686*g + 0.168*b);
            image[i][j].rgbtBlue = round(0.272*r + 0.534*g + 0.131*b);

the members rgbtRed , rgbtGreen and rgbtBlue are only 8 bits and the behavior of assigning floating type values from 256 on to them is undefined;成员rgbtRedrgbtGreenrgbtBlue只有 8 位,从 256 到它们分配浮点类型值的行为是未定义的; the following code to cap the values can't work.以下限制值的代码不起作用。 So cap the RGB values before assigning them:因此,在分配它们之前限制 RGB 值:

            image[i][j].rgbtRed   = fmin(round(0.393*r + 0.769*g + 0.189*b), 255);
            image[i][j].rgbtGreen = fmin(round(0.349*r + 0.686*g + 0.168*b), 255);
            image[i][j].rgbtBlue  = fmin(round(0.272*r + 0.534*g + 0.131*b), 255);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM