2012-06-04 54 views
0

预先感谢您提供给我的任何帮助,并对我的英文不好。将四个CMYK图像合并为一个RGB图像Java

我知道有很多关于这个主题的问题,但我已经看过了很多的所有互联网(和StackOverflow上太)上,但我还没有发现这方面的任何答复......

我有四个图片;它们中的每一个都在TYPE_BYTE_GRAY颜色模型中。我已加载使用此代码这四个图片:

int numElems = 4; 
BufferedImage[] img = new BufferedImage[numElems]; 
for(int i=0;i<numElems;i++){ 
    FileInputStream in = new FileInputStream(args[i]); 
    img[i] = ImageIO.read(in); 
    in.close(); 
} 

刚读ImageIO的...我需要“合并”四象成一个RGB图像...图像的每一个都是从CMYK一个通道图片。所有这些图像具有相同的尺寸。我已经使用这个代码转换的四象一个CMYK图像:

for(int j=0;j<img[0].getHeight();j++){ 
    //Read current point color... 
    for(int k=0;k<numElems;k++){ 
     colPunto[k] = (img[k].getRGB(i, j) & 0xFF); 
    } 

    int colorPunto = convertComponentsRGB(colPunto); 

    //Now, I set the point... 
    out.setRGB(i, j, colorPunto); 
    } 
} 

此功能“convertComponentsRGB”是很自然的数学到CMYK颜色转换为RGB颜色...

function convertComponentsRGB(int[] pointColor){ 
float cyan = (float)pointColor[0]/(float)255; 
float magenta = (float)pointColor[1]/(float)255; 
float yellow = (float)pointColor[2]/(float)255; 
float black = (float)pointColor[3]/(float)255; 

float c = min(1f,cyan * (1f - black) + black); //minimum value 
float m = min(1f,magenta * (1f - black) + black); //minimum value 
float y = min(1f, yellow * (1f - black) + black); //minimum value 

result[0] = Math.round(255f*(1f - c)); 
result[1] = Math.round(255f*(1f - m)); 
result[2] = Math.round(255f*(1f - y)); 

    return (result[0]<<16) | (result[1]<<8) | result[2]; 
} 

这里的问题是......速度。处理一张图像需要12秒,因为我们必须读取每个像素并写入每个像素,而且我认为“getRGB”和“setRGB”函数不是很快(或者它只是实现此目的的最佳方式) 。

¿我该如何做到这一点?我已经阅读了很多关于ColorModel的过滤器,但我仍然不明白如何在更好的时间实现这一点。

+0

你可以计算1/255F一次为所有图像和乘法使用它,而不是作出分裂。尝试一下并回报。 :) –

+0

好的...这比10%更好......但事情是,我想知道是否有一种方法可以使用光栅图像,滤镜或类似的方法实现此目的... Pure Java2D – epilefreyes

+0

您不需要甚至需要漂浮物。如果你只用整数来做这件事,我想这可能会快10倍。 –

回答

0

您可以使用getDatasetData加快对像素的访问速度,而对getRGBsetRGB可以加速。

没有必要到CMYK转换为浮点和背部,你可以用像素值直接工作:

function convertComponentsRGB(int[] pointColor){ 
    int r = max(0, 255 - (pointColor[0] + pointColor[3])); 
    int g = max(0, 255 - (pointColor[1] + pointColor[3])); 
    int b = max(0, 255 - (pointColor[2] + pointColor[3])); 

    return (r<<16) | (g<<8) | b; 
} 
相关问题