Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/CMYK/CMYKJPEGTest.java
66646 views
/*1* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25* This test verifies that using the built-in ImageI/O JPEG plugin that JPEG images26* that are in a CMYK ColorSpace can be read into a BufferedImage using the convemience27* APIS and that and the colours are properly interpreted.28* Since there is no standard JDK CMYK ColorSpace, this requires that either the image29* contain an ICC_Profile which can be used by the plugin to create an ICC_ColorSpace30* or that the plugin provides a suitable default CMYK ColorSpace instance by some other means.31*32* The test further verifies that the resultant BufferedImage will be re-written as a CMYK33* BufferedImage. It can do this so long as the BufferedImage has that CMYK ColorSpace34* used by its ColorModel.35*36* The verification requires re-reading again the re-written image and checking the37* re-read image still has a CMYK ColorSpace and the same colours.38*39* Optionally - not for use in the test harness - the test can be passed a parameter40* -display to create a UI which renders all the images the test is41* verifying so it can be manually verified42*/4344/*45* @test46* @bug 827473547* @summary Verify CMYK JPEGs can be read and written48*/4950import java.awt.Color;51import static java.awt.Color.*;52import java.awt.Dimension;53import java.awt.Graphics;54import java.awt.GridLayout;55import java.awt.image.BufferedImage;56import java.awt.image.ColorModel;57import java.io.ByteArrayInputStream;58import java.io.ByteArrayOutputStream;59import java.io.File;60import java.io.IOException;61import javax.imageio.ImageIO;62import javax.swing.JComponent;63import javax.swing.JFrame;64import javax.swing.JLabel;65import javax.swing.JPanel;66import javax.swing.SwingUtilities;6768public class CMYKJPEGTest {6970static String[] fileNames = {71"black_cmyk.jpg",72"white_cmyk.jpg",73"gray_cmyk.jpg",74"red_cmyk.jpg",75"blue_cmyk.jpg",76"green_cmyk.jpg",77"cyan_cmyk.jpg",78"magenta_cmyk.jpg",79"yellow_cmyk.jpg",80};8182static Color[] colors = {83black,84white,85gray,86red,87blue,88green,89cyan,90magenta,91yellow,92};9394static boolean display;9596static BufferedImage[] readImages;97static BufferedImage[] writtenImages;98static int imageIndex = 0;99100public static void main(String[] args) throws Exception {101102if (args.length > 0) {103display = "-display".equals(args[0]);104}105106String sep = System.getProperty("file.separator");107String dir = System.getProperty("test.src", ".");108String prefix = dir+sep;109110readImages = new BufferedImage[fileNames.length];111writtenImages = new BufferedImage[fileNames.length];112113for (String fileName : fileNames) {114String color = fileName.replace("_cmyk.jpg", "");115test(prefix+fileName, color, imageIndex++);116}117if (display) {118SwingUtilities.invokeAndWait(() -> createUI());119}120}121122static void test(String fileName, String color, int index)123throws IOException {124125readImages[index] = ImageIO.read(new File(fileName));126verify(readImages[index], color, colors[index]);127ByteArrayOutputStream baos = new ByteArrayOutputStream();128ImageIO.write(readImages[index], "jpg", baos);129ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());130writtenImages[index] = ImageIO.read(bais);131verify(writtenImages[index], color, colors[index]);132}133134static void verify(BufferedImage img, String colorName, Color c) {135ColorModel cm = img.getColorModel();136int tc = cm.getNumComponents();137int cc = cm.getNumColorComponents();138if (cc != 4 || tc != 4) {139throw new RuntimeException("Unexpected num comp for " + img);140}141142int rgb = img.getRGB(0,0);143int c_red = c.getRed();144int c_green = c.getGreen();145int c_blue = c.getBlue();146int i_red = (rgb & 0x0ff0000) >> 16;147int i_green = (rgb & 0x000ff00) >> 8;148int i_blue = (rgb & 0x00000ff);149int tol = 16;150if ((Math.abs(i_red - c_red) > tol) ||151(Math.abs(i_green - c_green) > tol) ||152(Math.abs(i_blue - c_blue) > tol))153{154System.err.println("red="+i_red+" green="+i_green+" blue="+i_blue);155throw new RuntimeException("Too different " + img + " " + colorName + " " + c);156}157}158159static class ImageComp extends JComponent {160161BufferedImage img;162163ImageComp(BufferedImage img) {164this.img = img;165}166167public Dimension getPreferredSize() {168return new Dimension(img.getWidth(), img.getHeight());169}170171public Dimension getMinimumSize() {172return getPreferredSize();173}174175public void paintComponent(Graphics g) {176super.paintComponent(g);177g.drawImage(img, 0, 0, null);178}179}180181static void createUI() {182JFrame f = new JFrame("CMYK JPEG Test");183JPanel p = new JPanel();184p.setLayout(new GridLayout(3, colors.length, 10, 10));185for (String s : fileNames) {186p.add(new JLabel(s.replace("_cmyk.jpg", "")));187}188for (BufferedImage i : readImages) {189p.add(new ImageComp(i));190}191for (BufferedImage i : writtenImages) {192p.add(new ImageComp(i));193}194f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);195f.add(p);196f.pack();197f.setVisible(true);198}199}200201202