BOOL fipImage::splitCMYK(fipImage& newc, fipImage& newm, fipImage& newy, fipImage& newk){
if (_dib){
if(!FreeImage_HasPixels(_dib)) return FALSE;
// src image should be CMYK
FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(_dib);
if (color_type !=FIC_CMYK){
return FALSE;
}
FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(_dib);
unsigned bpp = FreeImage_GetBPP(_dib);
// allocate a 8-bit dib
unsigned width = FreeImage_GetWidth(_dib);
unsigned height = FreeImage_GetHeight(_dib);
FIBITMAP *dst[4] = {NULL, NULL, NULL, NULL};
for (int i = 0; i < 4; ++i){
dst[i] = FreeImage_Allocate(width, height, 8);
if(!dst) continue;
// build a greyscale palette
RGBQUAD *pal = FreeImage_GetPalette(dst[i]);
for(int i = 0; i < 256; i++) {
pal[i].rgbBlue = pal[i].rgbGreen = pal[i].rgbRed = (BYTE)i;
}
}
// perform extraction
int bytespp = bpp / 8; // bytes / pixel
for(unsigned y = 0; y < height; y++) {
BYTE *src_bits = FreeImage_GetScanLine(_dib, y);
BYTE *c_bits = FreeImage_GetScanLine(dst[0], y);
BYTE *m_bits = FreeImage_GetScanLine(dst[1], y);
BYTE *y_bits = FreeImage_GetScanLine(dst[2], y);
BYTE *k_bits = FreeImage_GetScanLine(dst[3], y);
for(unsigned x = 0; x < width; x++) {
c_bits[x] = src_bits[0];
m_bits[x] = src_bits[1];
y_bits[x] = src_bits[2];
k_bits[x] = src_bits[3];
src_bits += bytespp;
}
}
//
newc = dst[0];
newm = dst[1];
newy = dst[2];
newk = dst[3];
return (newc.isValid() && newm.isValid() && newy.isValid() && newk.isValid());
}
return FALSE;
}