Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am Trying to convert a RGB (color space) Picture to CMY (color space) picture. I can read a specific (RGB) Picture, but my problem is actually to write or save it as a CMY picture. The picture, what I want to convert is the following:

Picture

I had wrote the following code:

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);
    }
}

I get the rgb value as a single int-value in the method iterateThroughImage(BufferedImage image), where I store it in the int variable pixel. I get each Red, Green, Blue and Alpha value in the method getPixelARGB(int pixel).

My problem actually is that I don't know how to convert an given specific RGB picture to a CMY (color space) picture.

By the Way: the link in the answer of Pazhamalai G is not reachable, because the "under"page ist not available anymore.

I had found a quite similar question related to this question. I had postet there my question, but it was deleted (for reasons I don't know). Its the following question: Writing java program about RGB-CMY

I Hope you can help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
164 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is mightbe a solution:

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);
    }
}

Hopefully is helpful


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...