Content of file ConvertColorProfileJ2DOp.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>ConvertColorProfileJ2DOp.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Mistral Examples CustomOperation</a> &gt; <a href="../index.html" class="el_bundle">image-operations</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.image.java2d</a> &gt; <span class="el_source">ConvertColorProfileJ2DOp.java</span></div><h1>ConvertColorProfileJ2DOp.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * Mistral: open source imaging engine
 * http://tidalwave.it/projects/mistral
 *
 * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/mistral-src
 * git clone https://github.com/tidalwave-it/mistral-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.image.java2d;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Collections;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DirectColorModel;
import java.awt.image.Raster;
import it.tidalwave.image.EditableImage;
import it.tidalwave.image.ImageUtils;
import it.tidalwave.image.op.ConvertColorProfileOp;
import it.tidalwave.image.op.OperationImplementation;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L53">@Immutable @Slf4j</span>
<span class="nc" id="L54">public class ConvertColorProfileJ2DOp extends OperationImplementation&lt;ConvertColorProfileOp, BufferedImage&gt;</span>
  {
    @Override @Nonnull
    protected BufferedImage execute (@Nonnull final ConvertColorProfileOp operation,
                                     @Nonnull final EditableImage image,
                                     @Nonnull final BufferedImage bufferedImage)
      {
<span class="nc" id="L61">        final var targetProfile = operation.getIccProfile();</span>
<span class="nc" id="L62">        log.debug(&quot;convertColorProfile({})&quot;, ImageUtils.getICCProfileName(targetProfile));</span>
<span class="nc" id="L63">        Java2DUtils.logImage(log, &quot;&gt;&gt;&gt;&gt; source bufferedImage&quot;, bufferedImage);</span>

<span class="nc" id="L65">        final var sourceProfile = ImageUtils.getICCProfile(bufferedImage);</span>
<span class="nc" id="L66">        final var sourceProfileName = ImageUtils.getICCProfileName(sourceProfile);</span>
<span class="nc" id="L67">        log.debug(&quot;&gt;&gt;&gt;&gt; Converting profile from {}  to {}&quot;,</span>
                  sourceProfileName,
<span class="nc" id="L69">                  ImageUtils.getICCProfileName(targetProfile));</span>

<span class="nc" id="L71">        final var hints = new RenderingHints(Collections.emptyMap());</span>
<span class="nc" id="L72">        hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);</span>
<span class="nc" id="L73">        final var ccOp = new ColorConvertOp(new ICC_Profile[]{targetProfile}, hints);</span>

        // Strategy 1: it would be the best, but reduces depth to 8 bit and converts to PixelInterleavedRaster, which
        // is SLOW!
        //        image = ccOp.filter(image, null); // - produce PixelInt BYTE
        // Strategy 2: create a dest image which is already an 8-bit packed raster
        // FIXME: this reduces the depth to 8 bit, which we don't want!
this reduces the depth to 8 bit, which we don't want!
<span class="nc" id="L80"> log.warn(&quot;&gt;&gt;&gt;&gt; **** WARNING: convertColorProfile() is reducing depth to 8 bit!&quot;);</span> <span class="nc" id="L82"> final var raster = Raster.createPackedRaster(DataBuffer.TYPE_INT,</span> <span class="nc" id="L83"> bufferedImage.getWidth(),</span> <span class="nc" id="L84"> bufferedImage.getHeight(),</span> 3, 8, null); <span class="nc" id="L88"> final ColorSpace colorSpace = new ICC_ColorSpace(targetProfile);</span> <span class="nc" id="L89"> final ColorModel colorModel =</span> new DirectColorModel(colorSpace, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0, false, DataBuffer.TYPE_INT); <span class="nc" id="L92"> final var image2 =</span> <span class="nc" id="L93"> new BufferedImage(colorModel, raster, false, Java2DUtils.getProperties(bufferedImage));</span> <span class="nc" id="L94"> final var result = ccOp.filter(bufferedImage, image2);</span> // Strategy 3: work in place. It would be the best, but it does not work: produces a dark image. // ccOp.filter(image, image); // Strategy 4: create an explicity destination image with the same SampleModel as the source, but produces a dark image // BufferedImage image2 = createCopy2(false); // - stessa conseguenza dell'in place // image = ccOp.filter(image, image2); <span class="nc" id="L101"> Java2DUtils.logImage(log, &quot;&gt;&gt;&gt;&gt; &gt;&gt;&gt;&gt; convertColorProfile() returning &quot;, result);</span> <span class="nc" id="L103"> return result;</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>