Java RGB转CMY图片

Java Converting RGB to CMY picture

我正在尝试将 RGB(颜色 space)图片转换为 CMY(颜色 space)图片。我可以读取特定的 (RGB) 图片,但我的问题实际上是将其写入或保存为 CMY 图片。图片,我要转换的是下面的:

Picture

我写了下面的代码:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Aufgabe2c {

    public static void main(String[] args) {
        try {
            BufferedImage image = ImageIO.read(new File("blumen.bmp"));
            iterateThroughImage(image);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void iterateThroughImage(BufferedImage image) {
        int w = image.getWidth();
        int h = image.getHeight();
        System.out.println("width, height: " + w + ", " + h);

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                System.out.println("x,y: " + j + ", " + i);
                int pixel = image.getRGB(j, i);
                getPixelARGB(pixel);
                System.out.println("");
            }
        }
    }

    public static void getPixelARGB(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
    }

    public static void convertRGBToCMY(int red, int green, int blue) {
        int cyan = 255 - red;
        int magenta =  255 - green;
        int yellow =  255 - blue;

        System.out.println("argb: "+ red + ", " + green + ", " + blue);
    }
}

我在方法 iterateThroughImage(BufferedImage image) 中将 rgb 值作为单个 int 值获取,我将其存储在 int 变量 pixel 中。我在方法 getPixelARGB(int pixel).

中得到每个红色、绿色、蓝色和 Alpha 值

我的问题实际上是我不知道如何将给定的特定 RGB 图片转换为 CMY(颜色 space)图片。

顺便说一句:Pazhamalai G 的回答中的 link 无法访问,因为 "under" 页面不再可用。

我发现了一个与此问题相关的非常相似的问题。我在那里发布了我的问题,但它被删除了(出于我不知道的原因)。它的以下问题: Writing java program about RGB-CMY

我希望你能帮助我。

这是一个示例解决方案:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Aufgabe2c {

    public static void main(String[] args) {
        try {
            BufferedImage image = ImageIO.read(new File("blumen.bmp"));
            iterateThroughImageToGetPixel(image);
            
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void iterateThroughImageToGetPixel(BufferedImage image) {
        try {
            BufferedImage cmyImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            
            System.out.println("width, height: " + image.getWidth() + ", " + image.getHeight());
            
            for (int column = 0; column < image.getHeight(); column++) {
                for (int row = 0; row < image.getWidth(); row++) {
                    System.out.println("x,y: " + row + ", " + column);
                    int pixel = image.getRGB(row, column);
//                  getPixelCMYValuesFromARGBValuesPerPixel(pixel).getRGB();
                    cmyImage.setRGB(row, column, getPixelCMYValuesFromARGBValuesPerPixel(image.getRGB(row, column)).getRGB());              
                    
                    System.out.println("");
                    System.out.println("----------------------------------------------------------------------------");
                }
            }
            System.out.println("#####################################################");
            ImageIO.write(cmyImage, "bmp", new File("blumen_cmy.bmp") );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // Save as BMP
    }
    /*
     * Quelle: https://alvinalexander.com/blog/post/java/getting-rgb-values-for-each-pixel-in-image-using-java-bufferedi
     *         http://openbook.rheinwerk-verlag.de/javainsel9/javainsel_20_006.htm#mj4c12381d5bacf8fb6ee31448d26890bb
     */
    public static Color getPixelCMYValuesFromARGBValuesPerPixel(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;

        System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
        
        return convertRGBToCMY(red, green, blue);
    }
    
    public static Color convertRGBToCMY(int red, int green, int blue) {
        int[] cmyArray = new int[3];
        
        //cyan
        int cyan = 255 - red;
        //magenta
        int magenta = 255 - green;
        //yellow
        int yellow = 255 - blue;
        return new Color(cyan, magenta, yellow);
    }
}

希望对您有所帮助