<p>I have a need to convert images from CMYK to RGB - not necessarily back again, but hey, if it can be done...</p>

<p>With the release of ColdFusion 8, we got the <a href="http://cfquickdocs.com/cf8/?getDoc=cfimage" rel="nofollow noreferrer">CFImage</a> tag, but it doesn't support this conversion; and nor does <a href="http://x.com" rel="nofollow noreferrer">Image.cfc</a>, or <a href="http://x.com" rel="nofollow noreferrer">Alagad's Image Component</a>.</p>

<p>However, it should be possible in Java; which we can leverage through CF. For example, here's how you might create a Java thread to sleep a process:</p>

<pre><code>&lt;cfset jthread = createObject("java", "java.lang.Thread")/&gt;
&lt;cfset jthread.sleep(5000)/&gt;
</code></pre>

<p>I would guess a similar method could be used to leverage java to do this image conversion, but not being a Java developer, I don't have a clue where to start. Can anyone lend a hand here?</p>

[{'answer_id': 23577, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 4, 'selected': False, 'text': "<p>A very simple formula for converting from CMYK to RGB ignoring all color profiles is:</p>\n\n<pre>\n    R = ( (255-C)*(255-K) ) / 255;\n    G = ( (255-M)*(255-K) ) / 255;\n    B = ( (255-Y)*(255-K) ) / 255;\n</pre>\n\n<p>This code requires CMYK values to be in rage of 0-255. If you have 0 to 100 or 0.0 to 1.0 you'll have to convert the values.</p>\n\n<p>Hope this will get you started.</p>\n\n<p>As for the java and ColdFusion interfacing, I'm sorry, but I have no idea how to do that.</p>\n"}, {'answer_id': 275661, 'author': 'Jas Panesar', 'author_id': 35886, 'author_profile': 'https://Stackoverflow.com/users/35886', 'pm_score': 1, 'selected': False, 'text': "<p>The tag cfx_image may be of use to you.  I haven't used it in a while but I remember it had a ton of features.</p>\n\n<p>Alternatively, you might be able to script a windows app such as Irfanview (via commandline using cfexecute) to process images.</p>\n\n<p>Hope that helps</p>\n"}, {'answer_id': 848533, 'author': 'Randy Stegbauer', 'author_id': 34301, 'author_profile': 'https://Stackoverflow.com/users/34301', 'pm_score': 4, 'selected': True, 'text': '<p>I use the Java ImageIO libraries (<a href="https://jai-imageio.dev.java.net" rel="noreferrer">https://jai-imageio.dev.java.net</a>).  They aren\'t perfect, but can be simple and get the job done.  As far as converting from CMYK to RGB, here is the best I have been able to come up with.</p>\n\n<p>Download and install the ImageIO JARs and native libraries for your platform.  The native libraries are essential.  Without them the ImageIO JAR files will not be able to detect the CMYK images.  Originally, I was under the impression that the native libraries would improve performance but was not required for any functionality. I was wrong.</p>\n\n<p>The only other thing that I noticed is that the converted RGB images are sometimes much lighter than the CMYK images. If anyone knows how to solve that problem, I would be appreciative.</p>\n\n<p>Below is some code to convert a CMYK image into an RGB image of any supported format.</p>\n\n<p>Thank you,<br>\nRandy Stegbauer</p>\n\n<pre><code>package cmyk;\n\nimport java.awt.color.ColorSpace;\nimport java.awt.image.BufferedImage;\nimport java.awt.image.ColorConvertOp;\nimport java.io.File;\nimport java.io.IOException;\n\nimport javax.imageio.ImageIO;\n\nimport org.apache.commons.lang.StringUtils;\n\npublic class Main\n{\n\n    /**\n     * Creates new RGB images from all the CMYK images passed\n     * in on the command line.\n     * The new filename generated is, for example "GIF_original_filename.gif".\n     *\n     */\n    public static void main(String[] args)\n    {\n        for (int ii = 0; ii &lt; args.length; ii++)\n        {\n            String filename = args[ii];\n            boolean cmyk = isCMYK(filename);\n            System.out.println(cmyk + ": " + filename);\n            if (cmyk)\n            {\n                try\n                {\n                    String rgbFile = cmyk2rgb(filename);\n                    System.out.println(isCMYK(rgbFile) + ": " + rgbFile);\n                }\n                catch (IOException e)\n                {\n                    System.out.println(e.getMessage());\n                }\n            }\n        }\n    }\n\n    /**\n     * If \'filename\' is a CMYK file, then convert the image into RGB,\n     * store it into a JPEG file, and return the new filename.\n     *\n     * @param filename\n     */\n    private static String cmyk2rgb(String filename) throws IOException\n    {\n        // Change this format into any ImageIO supported format.\n        String format = "gif";\n        File imageFile = new File(filename);\n        String rgbFilename = filename;\n        BufferedImage image = ImageIO.read(imageFile);\n        if (image != null)\n        {\n            int colorSpaceType = image.getColorModel().getColorSpace().getType();\n            if (colorSpaceType == ColorSpace.TYPE_CMYK)\n            {\n                BufferedImage rgbImage =\n                    new BufferedImage(\n                        image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);\n                ColorConvertOp op = new ColorConvertOp(null);\n                op.filter(image, rgbImage);\n\n                rgbFilename = changeExtension(imageFile.getName(), format);\n                rgbFilename = new File(imageFile.getParent(), format + "_" + rgbFilename).getPath();\n                ImageIO.write(rgbImage, format, new File(rgbFilename));\n            }\n        }\n        return rgbFilename;\n    }\n\n    /**\n     * Change the extension of \'filename\' to \'newExtension\'.\n     *\n     * @param filename\n     * @param newExtension\n     * @return filename with new extension\n     */\n    private static String changeExtension(String filename, String newExtension)\n    {\n        String result = filename;\n        if (filename != null &amp;&amp; newExtension != null &amp;&amp; newExtension.length() != 0);\n        {\n            int dot = filename.lastIndexOf(\'.\');\n            if (dot != -1)\n            {\n                result = filename.substring(0, dot) + \'.\' + newExtension;\n            }\n        }\n        return result;\n    }\n\n    private static boolean isCMYK(String filename)\n    {\n        boolean result = false;\n        BufferedImage img = null;\n        try\n        {\n            img = ImageIO.read(new File(filename));\n        }\n        catch (IOException e)\n        {\n            System.out.println(e.getMessage() + ": " + filename);\n        }\n        if (img != null)\n        {\n            int colorSpaceType = img.getColorModel().getColorSpace().getType();\n            result = colorSpaceType == ColorSpace.TYPE_CMYK;\n        }\n\n        return result;\n    }\n}\n</code></pre>\n'}, {'answer_id': 17498325, 'author': 'James Moberg', 'author_id': 693068, 'author_profile': 'https://Stackoverflow.com/users/693068', 'pm_score': 0, 'selected': False, 'text': '<p>I know that this question is old, but I still encounter problems with CMYK images &amp; ColdFusion.  However, I just read a CMYK JPEG image using ColdFusion 10 and resaved it.  The saved image was able to to be read using ColdFusion 9 (which is only capable of reading RGB JPEGs.)  I\'m not sure if this conversion is intentional or not and I don\'t currently have any way of identifying whether the source image\'s color profile is CMYK or not as the saved color profile still appears to be the same.</p>\n\n<pre><code>&lt;cfset imgData = ImageRead(expandPath("./CMYK_image.jpg"))&gt;\n&lt;cfset ImageWrite(imgData, expandPath("./Saved_image.jpg"))&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/22409', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/751/']