Search in sources :

Example 21 with ColorSpace

use of java.awt.color.ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.

the class CMYKJPEGImageReader method createRGBImageFromInvertedYCCK.

/**
 * Creates a buffered image from a raster in the inverted YCCK color space,
 * converting the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster A raster with (at least) 4 bands of samples.
 * @param cmykProfile An ICC_Profile for conversion from the CMYK color space
 * to the RGB color space. If this parameter is null, a default profile is used.
 * @return a BufferedImage in the RGB color space.
 */
public static BufferedImage createRGBImageFromInvertedYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertInvertedYCCKToCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        PixelInterleavedSampleModel pix;
        // if (Adobe_APP14 and transform==2) then YCCK else CMYK
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = 255 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 22 with ColorSpace

use of java.awt.color.ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.

the class CMYKJPEGImageReader method createRGBImageFromYCCK.

/**
 * Creates a buffered image from a raster in the YCCK color space, converting
 * the colors to RGB using the provided CMYK ICC_Profile.
 *
 * @param ycckRaster A raster with (at least) 4 bands of samples.
 * @param cmykProfile An ICC_Profile for conversion from the CMYK color space
 * to the RGB color space. If this parameter is null, a default profile is used.
 * @return a BufferedImage in the RGB color space.
 * @throws NullPointerException.
 */
public static BufferedImage createRGBImageFromYCCK(Raster ycckRaster, ICC_Profile cmykProfile) {
    BufferedImage image;
    if (cmykProfile != null) {
        ycckRaster = convertYCCKtoCMYK(ycckRaster);
        image = createRGBImageFromCMYK(ycckRaster, cmykProfile);
    } else {
        int w = ycckRaster.getWidth(), h = ycckRaster.getHeight();
        int[] rgb = new int[w * h];
        int[] Y = ycckRaster.getSamples(0, 0, w, h, 0, (int[]) null);
        int[] Cb = ycckRaster.getSamples(0, 0, w, h, 1, (int[]) null);
        int[] Cr = ycckRaster.getSamples(0, 0, w, h, 2, (int[]) null);
        int[] K = ycckRaster.getSamples(0, 0, w, h, 3, (int[]) null);
        float vr, vg, vb;
        for (int i = 0, imax = Y.length; i < imax; i++) {
            float k = K[i], y = Y[i], cb = Cb[i], cr = Cr[i];
            vr = y + 1.402f * (cr - 128) - k;
            vg = y - 0.34414f * (cb - 128) - 0.71414f * (cr - 128) - k;
            vb = y + 1.772f * (cb - 128) - k;
            rgb[i] = (0xff & (vr < 0.0f ? 0 : vr > 255.0f ? 0xff : (int) (vr + 0.5f))) << 16 | (0xff & (vg < 0.0f ? 0 : vg > 255.0f ? 0xff : (int) (vg + 0.5f))) << 8 | (0xff & (vb < 0.0f ? 0 : vb > 255.0f ? 0xff : (int) (vb + 0.5f)));
        }
        Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
    }
    return image;
}
Also used : ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace)

Example 23 with ColorSpace

use of java.awt.color.ColorSpace in project GMM by Katharsas.

the class TGAImageReader method getImageTypes.

// =========================================================================
// Required ImageReader methods
/**
 * @see javax.imageio.ImageReader#getImageTypes(int)
 */
public Iterator<ImageTypeSpecifier> getImageTypes(final int imageIndex) throws IOException {
    // validate the imageIndex (this will throw if invalid)
    checkImageIndex(imageIndex);
    // read / get the header
    final TGAHeader header = getHeader();
    // get the ImageTypeSpecifier for the image type
    // FIXME:  finish
    final ImageTypeSpecifier imageTypeSpecifier;
    switch(header.getImageType()) {
        case TGAConstants.COLOR_MAP:
        case TGAConstants.RLE_COLOR_MAP:
        case TGAConstants.TRUE_COLOR:
        case TGAConstants.RLE_TRUE_COLOR:
            {
                // determine if there is an alpha mask based on the number of
                // samples per pixel
                final int alphaMask;
                if (header.getSamplesPerPixel() == 4)
                    alphaMask = 0xFF000000;
                else
                    /* no alpha channel (less than 32 bits or 4 samples) */
                    alphaMask = 0;
                // packed RGB(A) pixel data (more specifically (A)BGR)
                // TODO:  split on 16, 24, and 32 bit images otherwise there
                // will be wasted space
                final ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                imageTypeSpecifier = ImageTypeSpecifier.createPacked(rgb, 0x000000FF, 0x0000FF00, 0x00FF0000, alphaMask, DataBuffer.TYPE_INT, false);
                break;
            }
        case TGAConstants.MONO:
        case TGAConstants.RLE_MONO:
            throw new IllegalArgumentException("Monochrome image type not supported.");
        case TGAConstants.NO_IMAGE:
        default:
            // FIXME:  localize
            throw new IllegalArgumentException("The image type is not known.");
    }
    // create a list and add the ImageTypeSpecifier to it
    final List<ImageTypeSpecifier> imageSpecifiers = new ArrayList<>();
    imageSpecifiers.add(imageTypeSpecifier);
    return imageSpecifiers.iterator();
}
Also used : ColorSpace(java.awt.color.ColorSpace) ArrayList(java.util.ArrayList) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 24 with ColorSpace

use of java.awt.color.ColorSpace in project WorldPainter by Captain-Chaos.

the class DumpImageInfo method dumpInfo.

private static void dumpInfo(BufferedImage image) throws IOException {
    System.out.print("  Image type: ");
    switch(image.getType()) {
        case BufferedImage.TYPE_3BYTE_BGR:
            System.out.println("3BYTE_BGR");
            break;
        case BufferedImage.TYPE_4BYTE_ABGR:
            System.out.println("4BYTE_ABGR");
            break;
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
            System.out.println("4BYTE_ABGR_PRE");
            break;
        case BufferedImage.TYPE_BYTE_BINARY:
            System.out.println("BYTE_BINARY");
            break;
        case BufferedImage.TYPE_BYTE_GRAY:
            System.out.println("BYTE_GRAY");
            break;
        case BufferedImage.TYPE_BYTE_INDEXED:
            System.out.println("BYTE_INDEXED");
            break;
        case BufferedImage.TYPE_CUSTOM:
            System.out.println("CUSTOM");
            break;
        case BufferedImage.TYPE_INT_ARGB:
            System.out.println("INT_ARGB");
            break;
        case BufferedImage.TYPE_INT_ARGB_PRE:
            System.out.println("INT_ARGB_PRE");
            break;
        case BufferedImage.TYPE_INT_BGR:
            System.out.println("INT_BGR");
            break;
        case BufferedImage.TYPE_INT_RGB:
            System.out.println("INT_RGB");
            break;
        case BufferedImage.TYPE_USHORT_555_RGB:
            System.out.println("USHORT_555_RGB");
            break;
        case BufferedImage.TYPE_USHORT_565_RGB:
            System.out.println("USHORT_565_RGB");
            break;
        case BufferedImage.TYPE_USHORT_GRAY:
            System.out.println("USHORT_GRAY");
            break;
        default:
            System.out.println("unknown (" + image.getType() + ")");
    }
    System.out.println("  Width: " + image.getWidth());
    System.out.println("  Height: " + image.getWidth());
    SampleModel sampleModel = image.getSampleModel();
    System.out.print("  Sample model data type: ");
    switch(sampleModel.getDataType()) {
        case DataBuffer.TYPE_BYTE:
            System.out.println("byte");
            break;
        case DataBuffer.TYPE_DOUBLE:
            System.out.println("double");
            break;
        case DataBuffer.TYPE_FLOAT:
            System.out.println("float");
            break;
        case DataBuffer.TYPE_INT:
            System.out.println("int");
            break;
        case DataBuffer.TYPE_SHORT:
            System.out.println("short");
            break;
        case DataBuffer.TYPE_UNDEFINED:
            System.out.println("undefined");
            break;
        case DataBuffer.TYPE_USHORT:
            System.out.println("unsigned short");
            break;
        default:
            System.out.println("unknown (" + sampleModel.getDataType() + ")");
            break;
    }
    System.out.println("  Sample model number of bands: " + sampleModel.getNumBands());
    for (int i = 0; i < sampleModel.getNumBands(); i++) {
        System.out.println("    Band " + i + ": " + sampleModel.getSampleSize(i) + " bits");
    }
    ColorModel colorModel = image.getColorModel();
    if (colorModel instanceof IndexColorModel) {
        System.out.println("  Color model is indexed");
        IndexColorModel indexColorModel = (IndexColorModel) colorModel;
        System.out.println("    Palette size: " + indexColorModel.getMapSize());
        System.out.println("    Palette:");
        for (int i = 0; i < indexColorModel.getMapSize(); i++) {
            System.out.printf("      Index %2d: 0x%8x%n", i, indexColorModel.getRGB(i));
        }
    } else {
        System.out.println("  Color model is not indexed");
    }
    ColorSpace colorSpace = colorModel.getColorSpace();
    System.out.print("  Color space type: ");
    switch(colorSpace.getType()) {
        case ColorSpace.TYPE_2CLR:
        case ColorSpace.TYPE_3CLR:
        case ColorSpace.TYPE_4CLR:
        case ColorSpace.TYPE_5CLR:
        case ColorSpace.TYPE_6CLR:
        case ColorSpace.TYPE_7CLR:
        case ColorSpace.TYPE_8CLR:
        case ColorSpace.TYPE_9CLR:
        case ColorSpace.TYPE_ACLR:
        case ColorSpace.TYPE_BCLR:
        case ColorSpace.TYPE_CCLR:
        case ColorSpace.TYPE_DCLR:
        case ColorSpace.TYPE_ECLR:
        case ColorSpace.TYPE_FCLR:
            System.out.println("generic");
            break;
        case ColorSpace.TYPE_CMY:
            System.out.println("CMY");
            break;
        case ColorSpace.TYPE_CMYK:
            System.out.println("CMYK");
            break;
        case ColorSpace.TYPE_GRAY:
            System.out.println("grayscale");
            break;
        case ColorSpace.TYPE_HLS:
            System.out.println("HLS");
            break;
        case ColorSpace.TYPE_HSV:
            System.out.println("HSV");
            break;
        case ColorSpace.TYPE_Lab:
            System.out.println("Lab");
            break;
        case ColorSpace.TYPE_Luv:
            System.out.println("Luv");
            break;
        case ColorSpace.TYPE_RGB:
            System.out.println("RGB");
            break;
        case ColorSpace.TYPE_XYZ:
            System.out.println("XYZ");
            break;
        case ColorSpace.TYPE_YCbCr:
            System.out.println("YCbCr");
            break;
        case ColorSpace.TYPE_Yxy:
            System.out.println("Yxy");
            break;
        default:
            System.out.println("unknown (" + colorSpace.getType() + ")");
            break;
    }
    System.out.println("  Color space components: " + colorSpace.getNumComponents());
    for (int i = 0; i < colorSpace.getNumComponents(); i++) {
        System.out.println("    Component " + i + ": " + colorSpace.getName(i));
        System.out.println("      Minimum value: " + colorSpace.getMinValue(i));
        System.out.println("      Maximum value: " + colorSpace.getMaxValue(i));
    }
    System.out.println("  Color model components: " + colorModel.getNumComponents());
    for (int i = 0; i < colorModel.getNumComponents(); i++) {
        System.out.println("    Component " + i + ": " + colorModel.getComponentSize(i) + " bits");
    }
    System.out.println("  Color model pixel size: " + colorModel.getPixelSize() + " bits");
    System.out.print("  Color model transparency: ");
    switch(colorModel.getTransparency()) {
        case Transparency.BITMASK:
            System.out.println("bitmask");
            break;
        case Transparency.OPAQUE:
            System.out.println("opaque");
            break;
        case Transparency.TRANSLUCENT:
            System.out.println("translucent");
            break;
        default:
            System.out.println("unknown (" + colorModel.getTransparency() + ")");
            break;
    }
    System.out.println("  Color model has alpha: " + colorModel.hasAlpha());
    if (colorModel.hasAlpha()) {
        System.out.println("    Premultiplied: " + colorModel.isAlphaPremultiplied());
    }
    System.out.println("[ R][ G][ B][ A][smpl]");
    for (int x = 0; x < 16; x++) {
        int rgba = image.getRGB(x, 0);
        System.out.printf("[%2x][%2x][%2x][%2x][%4x]%n", rgba & 0x000000ff, (rgba & 0x0000ff00) >> 8, (rgba & 0x00ff0000) >> 16, (rgba & 0xff000000) >>> 24, image.getRaster().getSample(x, 0, 0));
    }
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Example 25 with ColorSpace

use of java.awt.color.ColorSpace in project imageio-ext by geosolutions-it.

the class MrSIDTest method subBandsRead.

/**
 * Test read exploiting the setSourceBands and setDestinationType on
 * imageReadParam
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void subBandsRead() throws IOException {
    if (!isMrSidAvailable) {
        return;
    }
    try {
        ImageReader reader = new MrSIDImageReaderSpi().createReaderInstance();
        final File file = TestData.file(this, fileName);
        reader.setInput(file);
        // //
        // 
        // Getting image properties
        // 
        // //
        ImageTypeSpecifier spec = (ImageTypeSpecifier) reader.getImageTypes(0).next();
        SampleModel sm = spec.getSampleModel();
        final int width = reader.getWidth(0);
        final int height = reader.getHeight(0);
        // //
        // 
        // Setting a ColorModel
        // 
        // //
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorModel cm = RasterFactory.createComponentColorModel(sm.getDataType(), // color space
        cs, // has alpha
        false, // is alphaPremultiplied
        false, // transparency
        Transparency.OPAQUE);
        // //
        // 
        // Setting Image Read Parameters
        // 
        // //
        final ImageReadParam param = new ImageReadParam();
        final int ssx = 2;
        final int ssy = 2;
        param.setSourceSubsampling(ssx, ssy, 0, 0);
        final Rectangle sourceRegion = new Rectangle(50, 50, 300, 300);
        param.setSourceRegion(sourceRegion);
        param.setSourceBands(new int[] { 0 });
        Rectangle intersRegion = new Rectangle(0, 0, width, height);
        intersRegion = intersRegion.intersection(sourceRegion);
        int subsampledWidth = (intersRegion.width + ssx - 1) / ssx;
        int subsampledHeight = (intersRegion.height + ssy - 1) / ssy;
        param.setDestinationType(new ImageTypeSpecifier(cm, sm.createCompatibleSampleModel(subsampledWidth, subsampledHeight).createSubsetSampleModel(new int[] { 0 })));
        // //
        // 
        // Preparing the ImageRead operation
        // 
        // //
        ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
        pbjImageRead.setParameter("Input", file);
        pbjImageRead.setParameter("readParam", param);
        pbjImageRead.setParameter("reader", reader);
        // //
        // 
        // Setting a Layout
        // 
        // //
        final ImageLayout l = new ImageLayout();
        l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
        RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
        if (TestData.isInteractiveTest())
            Viewer.visualizeAllInformation(image, "SourceBand selection");
        else {
            Assert.assertNotNull(image.getTiles());
            ImageIOUtilities.disposeImage(image);
        }
    } catch (FileNotFoundException fnfe) {
        warningMessage();
    }
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ColorSpace(java.awt.color.ColorSpace) Rectangle(java.awt.Rectangle) FileNotFoundException(java.io.FileNotFoundException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Point(java.awt.Point) RenderingHints(java.awt.RenderingHints) ImageReadParam(javax.imageio.ImageReadParam) SampleModel(java.awt.image.SampleModel) RenderedOp(javax.media.jai.RenderedOp) ColorModel(java.awt.image.ColorModel) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest) Test(org.junit.Test)

Aggregations

ColorSpace (java.awt.color.ColorSpace)74 BufferedImage (java.awt.image.BufferedImage)28 ColorModel (java.awt.image.ColorModel)28 ComponentColorModel (java.awt.image.ComponentColorModel)25 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)22 SampleModel (java.awt.image.SampleModel)15 IndexColorModel (java.awt.image.IndexColorModel)13 WritableRaster (java.awt.image.WritableRaster)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)8 DataBuffer (java.awt.image.DataBuffer)8 DirectColorModel (java.awt.image.DirectColorModel)8 Point (java.awt.Point)7 DataBufferByte (java.awt.image.DataBufferByte)6 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)4 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)4