C/C++ RGB与CMYK数据相互转换

#ifndef COLORHELPER_H
#define COLORHELPER_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>

#define mine_max(a,b)    (((a) > (b)) ? (a) : (b))
#define mine_min(a,b)    (((a) < (b)) ? (a) : (b))

bool mine_rgb2cmyk(unsigned char * rgbBuffer,int rgbLength,unsigned char ** cmykBuffer, int * cmykLength){
    int pixelNum = 0;
    int index = 0;
    unsigned char * tempBuffer = NULL;
    if(rgbBuffer == NULL || cmykBuffer == NULL){
        return false;
    }

    pixelNum = rgbLength / 3;
    *cmykBuffer = (unsigned char *)malloc(pixelNum * 4);
    if(*cmykBuffer == NULL){
        return false;
    }
    tempBuffer = *cmykBuffer;
    *cmykLength = pixelNum * 4;
    for(index = 0;index < pixelNum;index++){
        unsigned char C = 0;
        unsigned char M = 0;
        unsigned char Y = 0;

        unsigned char c = 255 - rgbBuffer[3 * index + 0];
        unsigned char m = 255 - rgbBuffer[3 * index + 1];
        unsigned char y = 255 - rgbBuffer[3 * index + 2];

        unsigned char K = mine_min(mine_min(c, m), y);

        if (K == 255)
        {
            C = 0;
            M = 0;
            Y = 0;
        }
        else
        {
            C = (unsigned char)((c - K)*255.0 / (255 - K));
            M = (unsigned char)((m - K)*255.0 / (255 - K));
            Y = (unsigned char)((y - K)*255.0 / (255 - K));
        }

        tempBuffer[4 * index + 0] = C;
        tempBuffer[4 * index + 1] = M;
        tempBuffer[4 * index + 2] = Y;
        tempBuffer[4 * index + 3] = K;
    }
    return true;
}

bool mine_cmyk2rgb(unsigned char *cmykBuffer, int cmykLength,unsigned char ** rgbBuffer,int *rgbLength){
    int pixelNum = 0;
    int index = 0;
    unsigned char * tempBuffer = NULL;
    if(rgbBuffer == NULL || cmykBuffer == NULL){
        return false;
    }

    pixelNum = cmykLength / 4;
    *rgbBuffer = (unsigned char *)malloc(pixelNum * 3);
    if(*rgbBuffer == NULL){
        return false;
    }
    tempBuffer = *rgbBuffer;
    *rgbLength = pixelNum * 3;
    for(index = 0;index < pixelNum;index++){
        unsigned char c = 0;
        unsigned char m = 0;
        unsigned char y = 0;

        unsigned char C = cmykBuffer[4 * index + 0];
        unsigned char M = cmykBuffer[4 * index + 1];
        unsigned char Y = cmykBuffer[4 * index + 2];
        unsigned char K = cmykBuffer[4 * index + 3];

        c = C * (255 - K) / 255 + K;
        m = M * (255 - K) / 255 + K;
        y = Y * (255 - K) / 255 + K;
        tempBuffer[3 * index + 0] = 255 - c;
        tempBuffer[3 * index + 1] = 255 - m;
        tempBuffer[3 * index + 2] = 255 - y;
    }
    return true;
}

bool mine_rgb2cmyk2rgb(unsigned char * rgbBuffer,int rgbLength){
    int pixelNum = 0;
    int index = 0;
    if(rgbBuffer == NULL ){
        return false;
    }

    pixelNum = rgbLength / 3;
    for(index = 0;index < pixelNum;index++){
        unsigned char C = 0;
        unsigned char M = 0;
        unsigned char Y = 0;

        unsigned char c = 255 - rgbBuffer[3 * index + 0];
        unsigned char m = 255 - rgbBuffer[3 * index + 1];
        unsigned char y = 255 - rgbBuffer[3 * index + 2];

        unsigned char K = mine_min(mine_min(c, m), y);

        if (K == 255)
        {
            C = 0;
            M = 0;
            Y = 0;
        }
        else
        {
            C = (unsigned char)((c - K)*255.0 / (255 - K));
            M = (unsigned char)((m - K)*255.0 / (255 - K));
            Y = (unsigned char)((y - K)*255.0 / (255 - K));
        }

        c = C * (255 - K) / 255 + K;
        m = M * (255 - K) / 255 + K;
        y = Y * (255 - K) / 255 + K;

        rgbBuffer[3 * index + 0] = 255 - c;
        rgbBuffer[3 * index + 1] = 255 - m;
        rgbBuffer[3 * index + 2] = 255 - y;
    }
    return true;
}

#endif // COLORHELPER_H

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×