[英]RGB to CMYK and back algorithm
我正在嘗試實現一種解決方案,用於計算RGB和CMYK之間的轉換,反之亦然。 這是我到目前為止的內容:
public static int[] rgbToCmyk(int red, int green, int blue)
{
int black = Math.min(Math.min(255 - red, 255 - green), 255 - blue);
if (black!=255) {
int cyan = (255-red-black)/(255-black);
int magenta = (255-green-black)/(255-black);
int yellow = (255-blue-black)/(255-black);
return new int[] {cyan,magenta,yellow,black};
} else {
int cyan = 255 - red;
int magenta = 255 - green;
int yellow = 255 - blue;
return new int[] {cyan,magenta,yellow,black};
}
}
public static int[] cmykToRgb(int cyan, int magenta, int yellow, int black)
{
if (black!=255) {
int R = ((255-cyan) * (255-black)) / 255;
int G = ((255-magenta) * (255-black)) / 255;
int B = ((255-yellow) * (255-black)) / 255;
return new int[] {R,G,B};
} else {
int R = 255 - cyan;
int G = 255 - magenta;
int B = 255 - yellow;
return new int[] {R,G,B};
}
}
正如Lea Verou所說,您應該利用色彩空間信息,因為沒有從RGB映射到CMYK的算法。 Adobe有一些可供下載的ICC顏色配置文件1 ,但我不確定它們如何獲得許可。
獲得顏色配置文件后,將執行以下操作:
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.IOException;
import java.util.Arrays;
public class ColorConv {
final static String pathToCMYKProfile = "C:\\UncoatedFOGRA29.icc";
public static float[] rgbToCmyk(float... rgb) throws IOException {
if (rgb.length != 3) {
throw new IllegalArgumentException();
}
ColorSpace instance = new ICC_ColorSpace(ICC_Profile.getInstance(pathToCMYKProfile));
float[] fromRGB = instance.fromRGB(rgb);
return fromRGB;
}
public static float[] cmykToRgb(float... cmyk) throws IOException {
if (cmyk.length != 4) {
throw new IllegalArgumentException();
}
ColorSpace instance = new ICC_ColorSpace(ICC_Profile.getInstance(pathToCMYKProfile));
float[] fromRGB = instance.toRGB(cmyk);
return fromRGB;
}
public static void main(String... args) {
try {
float[] rgbToCmyk = rgbToCmyk(1.0f, 1.0f, 1.0f);
System.out.println(Arrays.toString(rgbToCmyk));
System.out.println(Arrays.toString(cmykToRgb(rgbToCmyk[0], rgbToCmyk[1], rgbToCmyk[2], rgbToCmyk[3])));
} catch (IOException e) {
e.printStackTrace();
}
}
}
要將Photoshop的值准確地從RGB轉換為CMYK,反之亦然,您需要使用ICC顏色配置文件。 您會在互連網上找到所有簡單的算法解決方案(例如上面發布的解決方案),並且產生的顏色超出CMYK色域(例如,它們將CMYK(100,0,0,0)轉換為rgb(0 ,255、255),這顯然是錯誤的,因為rgb(0,255,255)無法用CMYK復制)。 查看java.awt.color.ICC_ColorSpace和java.awt.color.ICC_Profile類,以使用ICC顏色配置文件轉換顏色。 至於顏色配置文件本身,Adobe會免費分發。
更好的方法是:
try {
// The "from" CMYK colorspace
ColorSpace cmykColorspace = new ICC_ColorSpace(ICC_Profile.getInstance("icc/CoatedFOGRA27.icc"));
// The "to" RGB colorspace
ColorSpace rgbColorspace = new ICC_ColorSpace(ICC_Profile.getInstance("icc/AdobeRGB1998.icc"));
// Bring in to CIEXYZ colorspace (refer to Java documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/color/ColorSpace.html)
float[] ciexyz = cmykColorspace.toCIEXYZ(cmyk);
float[] thisColorspace = rgbColorspace.fromCIEXYZ(ciexyz);
float[] rgb = thisColorspace;
Color c = new Color(rgb[0], rgb[1], rgb[2]);
// Format RGB as Hex and return
return String.format("#%06x", c.getRGB() & 0xFFFFFF);
} catch (IOException e) { e.printStackTrace(); }
public static String makeCMYKString(int color) {
double red = Color.red(color);
double green = Color.green(color);
double blue = Color.blue(color);
double red1 = red / 255;
double green1 = green / 255;
double blue1 = blue / 255;
double max = (Math.max(Math.max(red1, green1), blue1));
double K = 1 - max;
double C = (1 - red1 - K) / (1 - K);
double M = (1 - green1 - K) / (1 - K);
double Y = (1 - blue1 - K) / (1 - K);
double CMYK[] = {C, M, Y, K};
String cmyk = "CMYK = (" + Math.round(C * 100) + " , " + Math.round(M * 100) + " , " + Math.round(Y * 100) + " , " + Math.round(K * 100) + ")";
return cmyk;
}
為了正確顯示,CMYK圖像應包含色彩空間信息作為ICC配置文件。 因此,最好的方法是使用可以通過Sanselan輕松提取的ICC配置文件:
ICC_Profile iccProfile = Sanselan.getICCProfile(new File("filename.jpg"));
ColorSpace cs = new ICC_ColorSpace(iccProfile);
如果沒有附加到圖像的ICC配置文件,我將使用Adobe配置文件作為默認設置。
現在的問題是,您不能僅使用ImageIO加載具有自定義色彩空間的JPEG文件,因為它會引發異常,抱怨它不支持某些色彩空間或類似的東西。 因此,您將不得不使用柵格:
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(data));
Raster srcRaster = decoder.decodeAsRaster();
BufferedImage result = new BufferedImage(srcRaster.getWidth(), srcRaster.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster resultRaster = result.getRaster();
ColorConvertOp cmykToRgb = new ColorConvertOp(cs, result.getColorModel().getColorSpace(), null);
cmykToRgb.filter(srcRaster, resultRaster);
然后,您可以在任何需要的地方使用result
,它將轉換為顏色。
但是實際上,我遇到了一些圖像(用相機拍攝並用Photoshop處理),這些圖像具有某種程度上的顏色值倒置,因此最終得到的圖像總是倒置,即使再次反轉后它們也太亮了。 盡管我仍然不知道如何找出何時正確使用它(當我需要反轉像素值時),但是我有一種算法可以校正這些值並逐像素轉換顏色:
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new ByteArrayInputStream(data));
Raster srcRaster = decoder.decodeAsRaster();
BufferedImage ret = new BufferedImage(srcRaster.getWidth(), srcRaster.getHeight(), BufferedImage.TYPE_INT_RGB);
WritableRaster resultRaster = ret.getRaster();
for (int x = srcRaster.getMinX(); x < srcRaster.getWidth(); ++x)
for (int y = srcRaster.getMinY(); y < srcRaster.getHeight(); ++y) {
float[] p = srcRaster.getPixel(x, y, (float[])null);
for (int i = 0; i < p.length; ++i)
p[i] = 1 - p[i] / 255f;
p = cs.toRGB(p);
for (int i = 0; i < p.length; ++i)
p[i] = p[i] * 255f;
resultRaster.setPixel(x, y, p);
}
我很確定RasterOp或ColorConvertOp可以用來使對話更加有效,但這對我來說已經足夠了。
認真地講,不需要使用這些簡化的CMYK到RGB轉換算法,因為您可以使用嵌入到圖像中的ICC配置文件,也可以從Adobe免費獲得。 如果效果不理想(帶有嵌入式配置文件),生成的圖像將看起來更好。
這是我的方式。 請記住,我將RGB顏色從原始顏色重新轉換了。
public static String getCMYK(int c){
float computedC = 0;
float computedM = 0;
float computedY = 0;
float computedK = 0;
int r = (c >> 16) & 0xFF;
int g = (c >> 8) & 0xFF;
int b = (c >> 0) & 0xFF;
// BLACK
if (r==0 && g==0 && b==0) {
computedK = 1;
return "0 0 0 100";
}
computedC = 1 - (r/255f);
computedM = 1 - (g/255f);
computedY = 1 - (b/255f);
float minCMY = Math.min(computedC,Math.min(computedM,computedY));
if (1 - minCMY != 0){
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
}
computedK = minCMY;
return (int)(computedC*100f) + " " + (int)(computedM*100f) + " " + (int)(computedY*100f) + " " + (int)(computedK*100f);
}
這是與您相同的問題
這是該頁面的副本/部分:
/** CMYK to RGB conversion */
/* Adobe PhotoShop algorithm */
cyan = Math.min(255, cyan + black); //black is from K
magenta = Math.min(255, magenta + black);
yellow = Math.min(255, yellow + black);
rgb[0] = 255 - cyan;
rgb[1] = 255 - magenta;
rgb[2] = 255 - yellow;
/* GNU Ghostscript algorithm -- this is better*/
int colors = 255 - black;
rgb[0] = colors * (255 - cyan)/255;
rgb[1] = colors * (255 - magenta)/255;
rgb[2] = colors * (255 - yellow)/255;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.