Search in sources :

Example 81 with ColorSpace

use of java.awt.color.ColorSpace in project digilib by robcast.

the class ImageLoaderDocuImage method loadSubimage.

/* 
     * Load an image file into the Object.
     * 
     * @see digilib.image.DocuImageImpl#loadSubimage(digilib.io.ImageInput, java.awt.Rectangle, int)
     */
public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException {
    logger.debug("loadSubimage");
    this.input = ii;
    // ImageReader reader = null;
    try {
        reader = getReader(ii);
        /*
             * set up reader parameters
             */
        ImageReadParam readParam = reader.getDefaultReadParam();
        readParam.setSourceRegion(region);
        if (prescale > 1) {
            readParam.setSourceSubsampling(prescale, prescale, 0, 0);
        }
        if (imageHacks.get(Hacks.setDestSrgb)) {
            /*
				 * try to set target color space to sRGB
				 */
            for (Iterator<ImageTypeSpecifier> i = reader.getImageTypes(0); i.hasNext(); ) {
                ImageTypeSpecifier type = (ImageTypeSpecifier) i.next();
                ColorModel cm = type.getColorModel();
                ColorSpace cs = cm.getColorSpace();
                logger.debug("loadSubimage: possible color model:" + cm + " color space:" + cs);
                if (cs.getNumComponents() < 3 && !imageHacks.get(Hacks.setDestSrgbForNonRgb)) {
                    // if the first type is not RGB do nothing
                    logger.debug("loadSubimage: image is not RGB " + type);
                    break;
                }
                if (cs.isCS_sRGB()) {
                    logger.debug("loadSubimage: substituted sRGB destination type " + type);
                    readParam.setDestinationType(type);
                    break;
                }
            }
        }
        /*
             * read image
             */
        logger.debug("loadSubimage: loading..");
        img = reader.read(0, readParam);
        logger.debug("loadSubimage: loaded " + img);
        // invalidate image size if it was set
        imageSize = null;
        /*
             * downconvert highcolor images
             */
        if (img.getColorModel().getComponentSize(0) > 8) {
            logger.debug("loadSubimage: converting to 8bit");
            int type = BufferedImage.TYPE_INT_RGB;
            if (img.getColorModel().hasAlpha()) {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            BufferedImage lcImg = new BufferedImage(img.getWidth(), img.getHeight(), type);
            lcImg.createGraphics().drawImage(img, null, 0, 0);
            img = lcImg;
        }
    } catch (IOException e) {
        throw new FileOpException("Unable to load File!", e);
    } finally {
        if (!reuseReader && reader != null) {
            reader.dispose();
        }
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) ColorSpace(java.awt.color.ColorSpace) FileOpException(digilib.io.FileOpException) IOException(java.io.IOException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage)

Example 82 with ColorSpace

use of java.awt.color.ColorSpace in project pdfbox by apache.

the class PDDeviceCMYK method toRGBImageAWT.

@Override
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace) {
    if (usePureJavaCMYKConversion) {
        BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_RGB);
        ColorSpace destCS = dest.getColorModel().getColorSpace();
        WritableRaster destRaster = dest.getRaster();
        float[] srcValues = new float[4];
        float[] lastValues = new float[] { -1.0f, -1.0f, -1.0f, -1.0f };
        float[] destValues = new float[3];
        int startX = raster.getMinX();
        int startY = raster.getMinY();
        int endX = raster.getWidth() + startX;
        int endY = raster.getHeight() + startY;
        for (int x = startX; x < endX; x++) {
            for (int y = startY; y < endY; y++) {
                raster.getPixel(x, y, srcValues);
                // check if the last value can be reused
                if (!Arrays.equals(lastValues, srcValues)) {
                    lastValues[0] = srcValues[0];
                    srcValues[0] = srcValues[0] / 255f;
                    lastValues[1] = srcValues[1];
                    srcValues[1] = srcValues[1] / 255f;
                    lastValues[2] = srcValues[2];
                    srcValues[2] = srcValues[2] / 255f;
                    lastValues[3] = srcValues[3];
                    srcValues[3] = srcValues[3] / 255f;
                    // use CIEXYZ as intermediate format to optimize the color conversion
                    destValues = destCS.fromCIEXYZ(colorSpace.toCIEXYZ(srcValues));
                    for (int k = 0; k < destValues.length; k++) {
                        destValues[k] = destValues[k] * 255f;
                    }
                }
                destRaster.setPixel(x, y, destValues);
            }
        }
        return dest;
    } else {
        return super.toRGBImageAWT(raster, colorSpace);
    }
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorSpace(java.awt.color.ColorSpace) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Example 83 with ColorSpace

use of java.awt.color.ColorSpace in project pdfbox by apache.

the class LosslessFactoryTest method testCreateLosslessFrom16Bit.

@Test
void testCreateLosslessFrom16Bit() throws IOException {
    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
    ColorSpace targetCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int dataBufferType = DataBuffer.TYPE_USHORT;
    final ColorModel colorModel = new ComponentColorModel(targetCS, false, false, ColorModel.OPAQUE, dataBufferType);
    WritableRaster targetRaster = Raster.createInterleavedRaster(dataBufferType, image.getWidth(), image.getHeight(), targetCS.getNumComponents(), new Point(0, 0));
    BufferedImage img16Bit = new BufferedImage(colorModel, targetRaster, false, new Hashtable<>());
    ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
    op.filter(image, img16Bit);
    PDImageXObject ximage = LosslessFactory.createFromImage(document, img16Bit);
    validate(ximage, 16, img16Bit.getWidth(), img16Bit.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
    checkIdent(image, ximage.getImage());
    doWritePDF(document, ximage, TESTRESULTSDIR, "misc-16bit.pdf");
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) WritableRaster(java.awt.image.WritableRaster) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) Test(org.junit.jupiter.api.Test)

Example 84 with ColorSpace

use of java.awt.color.ColorSpace in project pdfbox by apache.

the class LosslessFactoryTest method testCreateLosslessFromImageCMYK.

/**
 * Test lossless encoding of CMYK images
 */
@Test
void testCreateLosslessFromImageCMYK() throws IOException {
    PDDocument document = new PDDocument();
    BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
    final ColorSpace targetCS = new ICC_ColorSpace(ICC_Profile.getInstance(this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc")));
    ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
    BufferedImage imageCMYK = op.filter(image, null);
    PDImageXObject ximage = LosslessFactory.createFromImage(document, imageCMYK);
    validate(ximage, 8, imageCMYK.getWidth(), imageCMYK.getHeight(), "png", "ICCBased");
    doWritePDF(document, ximage, TESTRESULTSDIR, "cmyk.pdf");
// still slight difference of 1 color level
// checkIdent(imageCMYK, ximage.getImage());
}
Also used : ICC_ColorSpace(java.awt.color.ICC_ColorSpace) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Example 85 with ColorSpace

use of java.awt.color.ColorSpace in project sis by apache.

the class Colorizer method initialize.

/**
 * Uses the given color model for mapping range of values to new colors. The colors in the given color model
 * are ignored (because they will be replaced by colors specified by this {@code Colorizer}); only the range
 * of values will be fetched, if such range exists.
 *
 * @param  source  the color model from which to get a range of values, or {@code null}.
 * @return {@code true} on success, or {@code false} if no range of values has been found.
 * @throws IllegalStateException if a sample dimension is already defined on this colorizer.
 */
public boolean initialize(final ColorModel source) {
    checkInitializationStatus(false);
    if (source != null) {
        final ColorSpace cs = source.getColorSpace();
        if (cs instanceof ScaledColorSpace) {
            final ScaledColorSpace scs = (ScaledColorSpace) cs;
            initialize(scs.offset, scs.maximum);
            return true;
        }
    }
    return false;
}
Also used : ColorSpace(java.awt.color.ColorSpace)

Aggregations

ColorSpace (java.awt.color.ColorSpace)86 ColorModel (java.awt.image.ColorModel)33 BufferedImage (java.awt.image.BufferedImage)30 ComponentColorModel (java.awt.image.ComponentColorModel)29 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)24 IndexColorModel (java.awt.image.IndexColorModel)17 SampleModel (java.awt.image.SampleModel)17 WritableRaster (java.awt.image.WritableRaster)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ColorConvertOp (java.awt.image.ColorConvertOp)10 DirectColorModel (java.awt.image.DirectColorModel)10 Point (java.awt.Point)8 DataBuffer (java.awt.image.DataBuffer)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)8 DataBufferByte (java.awt.image.DataBufferByte)6 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 IIOException (javax.imageio.IIOException)5 Test (org.junit.Test)5 Rectangle (java.awt.Rectangle)4 ComponentSampleModel (java.awt.image.ComponentSampleModel)4