CMYK转RGB

扫描仪的颜色空间一般为CMYK,从扫描仪上得到的图片如果是自己移植图片解码器, 一般需要将CMYK转换至RGB888。相关解释,参考国外资料:
To understand the CMYK color model, it is best to start with an understanding of RGB color.

The RGB color model is made up of red, green and blue. It is used on your computer monitor and is what you will view your projects in while still on the screen. RGB is retained for projects that are designed to stay on screen (websites, pdfs, and other web graphics, for instance).

These colors, however, can only be viewed with natural or produced light, such as in the computer monitor, and not on a printed page. This is where CMYK comes in.

When two RGB colors are mixed equally they produce the colors of the CMYK model, which are known as subtractive primaries.

Green and blue create cyan (C).
Red and blue create magenta (M).
Red and green create yellow (Y).
Black is added to the model because it cannot be created with the 3 subtractive primaries (when combined they create a dark brown). The K, or “key,” stands for black.

转换公式如下:
R = C* (255 - cyan)/255;
G = C* (255 - magenta)/255;
B = C* (255 - yellow)/255;

C实现代码:

uint8_t* CMYKToRGB24(uint32_t* cmyk, int width, int height)
{
    uint32_t off;
    uint32_t color;
    int i,j;
    uint32_t row_bytes;
    uint8_t* rgb;
    uint8_t* ptr;

    uint8_t c,m,y,k;
    int tmp;
    row_bytes = (width * 3 + 3) & ~3;
    rgb = (uint8_t*)malloc(row_bytes * height);
    if(!rgb)
        return NULL;
    off = 0;
    for (i = 0; i < height; i++)
    {   
        ptr = rgb + off;
        for(j = 0; j < width; j++)
        {
            color = *cmyk++;
            c = color & 0x000000FF;
            m = (color >> 8) & 0x000000FF;
            y = (color >> 16) & 0x000000FF;
            k = (color >> 24) & 0x000000FF;

            *ptr++ = k * c/255;
            *ptr++ = k * m/255;
            *ptr++ = k * y/255;
        }
        off += row_bytes;
    }
    return rgb;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值