/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); 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 "AS IS" 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.
 */
package org.apache.pdfbox.pdmodel.graphics.color;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.IOException;

import org.apache.pdfbox.cos.COSName;

/**
 * Colours in the DeviceRGB colour space are specified according to the additive
 * RGB (red-green-blue) colour model.
 *
 * @author Ben Litchfield
 * @author John Hewson
 */
public final class PDDeviceRGB extends PDDeviceColorSpace
{
    /**  This is the single instance of this class. */
    public static final PDDeviceRGB INSTANCE = new PDDeviceRGB();
    
    private final PDColor initialColor = new PDColor(new float[] { 0, 0, 0 }, this);
    private volatile boolean initDone = false;

    private PDDeviceRGB()
    {
    }

    /**
     * Lazy setting of the AWT color space due to JDK race condition.
     */
    private void init()
    {
        // no need to synchronize this check as it is atomic
        if (initDone)
        {
            return;
        }

        synchronized (this)
        {
            // we might have been waiting for another thread, so check again
            if (initDone)
            {
                return;
            }

            // there is a JVM bug which results in a CMMException which appears to be a race
            // condition caused by lazy initialization of the color transform, so we perform
            // an initial color conversion while we're still synchronized, see PDFBOX-2184
            ColorSpace.getInstance(ColorSpace.CS_sRGB).toRGB(new float[] { 0, 0, 0, 0 });

            // This volatile write must be the LAST statement in the synchronized block!
            initDone = true;
        }
    }
    
    @Override
    public String getName()
    {
        return COSName.DEVICERGB.getName();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getNumberOfComponents()
    {
        return 3;
    }

    @Override
    public float[] getDefaultDecode(int bitsPerComponent)
    {
        return new float[] { 0, 1, 0, 1, 0, 1 };
    }

    @Override
    public PDColor getInitialColor()
    {
        return initialColor;
    }

    @Override
    public float[] toRGB(float[] value)
    {
        return value;
    }

    @Override
    public BufferedImage toRGBImage(WritableRaster raster) throws IOException
    {
        init();

        //
        // WARNING: this method is performance sensitive, modify with care!
        //
        // Please read PDFBOX-3854 and PDFBOX-2092 and look at the related commits first.
        // The current code returns TYPE_INT_RGB images which prevents slowness due to threads
        // blocking each other when TYPE_CUSTOM images are used.
        BufferedImage image = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_RGB);
        image.setData(raster);
        return image;
    }

    @Override
    public BufferedImage toRawImage(WritableRaster raster) throws IOException
    {
        // Device RGB is not specified, as its the colors of whatever device you use. The user
        // should use the toRGBImage().
        return null;
    }
}

@@ -1478,1035 +1478,47 @@
 s);%0A
-    private volatile boolean initDone = false;%0A%0A    private PDDeviceRGB()%0A    %7B%0A    %7D%0A%0A    /**%0A     * Lazy setting of the AWT color space due to JDK race condition.%0A     */%0A    private void init()%0A    %7B%0A        // no need to synchronize this check as it is atomic%0A        if (initDone)%0A        %7B%0A            return;%0A        %7D%0A%0A        synchronized (this)%0A        %7B%0A            // we might have been waiting for another thread, so check again%0A            if (initDone)%0A            %7B%0A                return;%0A            %7D%0A%0A            // there is a JVM bug which results in a CMMException which appears to be a race%0A            // condition caused by lazy initialization of the color transform, so we perform%0A            // an initial color conversion while we're still synchronized, see PDFBOX-2184%0A            ColorSpace.getInstance(ColorSpace.CS_sRGB).toRGB(new float%5B%5D %7B 0, 0, 0, 0 %7D);%0A%0A            // This volatile write must be the LAST statement in the synchronized block!%0A            initDone = true;%0A        %7D%0A    %7D%0A    
+%0A    private PDDeviceRGB()%0A    %7B%0A    %7D%0A
 %0A   
@@ -2152,25 +2152,8 @@
   %7B%0A
-        init();%0A%0A
     

remove JVM bug related workaround that is no longer needed, as suggested by Andreas Lehmkühler