Example usage for java.awt.image ColorModel getComponentSize

List of usage examples for java.awt.image ColorModel getComponentSize

Introduction

In this page you can find the example usage for java.awt.image ColorModel getComponentSize.

Prototype

public int[] getComponentSize() 

Source Link

Document

Returns an array of the number of bits per color/alpha component.

Usage

From source file:lucee.runtime.img.Image.java

public void resize(int width, int height, int interpolation, double blurFactor) throws ExpressionException {

    ColorModel cm = image().getColorModel();

    if (interpolation == IP_HIGHESTPERFORMANCE) {
        interpolation = IPC_BICUBIC;/*from  ww w  . j  av a2s .  com*/
    }

    if (cm.getColorSpace().getType() == ColorSpace.TYPE_GRAY && cm.getComponentSize()[0] == 8) {
        if (interpolation == IP_HIGHESTQUALITY || interpolation == IP_HIGHPERFORMANCE
                || interpolation == IP_HIGHQUALITY || interpolation == IP_MEDIUMPERFORMANCE
                || interpolation == IP_MEDIUMQUALITY) {
            interpolation = IPC_BICUBIC;
        }
        if (interpolation != IPC_BICUBIC && interpolation != IPC_BILINEAR && interpolation != IPC_NEAREST) {
            throw new ExpressionException("invalid grayscale interpolation");
        }
    }

    if (interpolation <= IPC_MAX) {
        resizeImage(width, height, interpolation);
    } else {
        image(ImageResizer.resize(image(), width, height, interpolation, blurFactor));

    }
}

From source file:lucee.runtime.img.Image.java

public Struct info() throws ExpressionException {
    if (sctInfo != null)
        return sctInfo;

    Struct sctInfo = new StructImpl(), sct;

    sctInfo.setEL("height", new Double(getHeight()));
    sctInfo.setEL("width", new Double(getWidth()));
    sctInfo.setEL("source", source == null ? "" : source.getAbsolutePath());
    //sct.setEL("mime_type",getMimeType());

    ColorModel cm = image().getColorModel();
    sct = new StructImpl();
    sctInfo.setEL("colormodel", sct);

    sct.setEL("alpha_channel_support", Caster.toBoolean(cm.hasAlpha()));
    sct.setEL("alpha_premultiplied", Caster.toBoolean(cm.isAlphaPremultiplied()));
    sct.setEL("transparency", toStringTransparency(cm.getTransparency()));
    sct.setEL("pixel_size", Caster.toDouble(cm.getPixelSize()));
    sct.setEL("num_components", Caster.toDouble(cm.getNumComponents()));
    sct.setEL("num_color_components", Caster.toDouble(cm.getNumColorComponents()));
    sct.setEL("colorspace", toStringColorSpace(cm.getColorSpace()));

    //bits_component
    int[] bitspercomponent = cm.getComponentSize();
    Array arr = new ArrayImpl();
    Double value;/*w w w  .ja  v  a 2 s. c  o  m*/
    for (int i = 0; i < bitspercomponent.length; i++) {
        sct.setEL("bits_component_" + (i + 1), value = new Double(bitspercomponent[i]));
        arr.appendEL(value);
    }
    sct.setEL("bits_component", arr);

    // colormodel_type
    if (cm instanceof ComponentColorModel)
        sct.setEL("colormodel_type", "ComponentColorModel");
    else if (cm instanceof IndexColorModel)
        sct.setEL("colormodel_type", "IndexColorModel");
    else if (cm instanceof PackedColorModel)
        sct.setEL("colormodel_type", "PackedColorModel");
    else
        sct.setEL("colormodel_type", ListUtil.last(cm.getClass().getName(), '.'));

    getMetaData(sctInfo);

    ImageMeta.addInfo(format, source, sctInfo);

    this.sctInfo = sctInfo;
    return sctInfo;
}

From source file:org.geoserver.wps.gs.GeorectifyCoverage.java

@DescribeResults({
        @DescribeResult(name = "result", description = "Georectified raster", type = GridCoverage2D.class),
        @DescribeResult(name = "path", description = "Pathname of the generated raster on the server", type = String.class) })
public Map<String, Object> execute(
        @DescribeParameter(name = "data", description = "Input raster") GridCoverage2D coverage,
        @DescribeParameter(name = "gcp", description = "List of Ground control points.  Points are specified as [x,y] or [x,y,z].") String gcps,
        @DescribeParameter(name = "bbox", description = "Bounding box for output", min = 0) Envelope bbox,
        @DescribeParameter(name = "targetCRS", description = "Coordinate reference system to use for the output raster") CoordinateReferenceSystem crs,
        @DescribeParameter(name = "width", description = "Width of output raster in pixels", min = 0) Integer width,
        @DescribeParameter(name = "height", description = "Height of output raster in pixels", min = 0) Integer height,
        @DescribeParameter(name = "warpOrder", min = 0, description = "Order of the warping polynomial (1 to 3)") Integer warpOrder,
        @DescribeParameter(name = "transparent", min = 0, description = "Force output to have transparent background") Boolean transparent,
        @DescribeParameter(name = "store", min = 0, description = "Indicates whether to keep the output file after processing") Boolean store,
        @DescribeParameter(name = "outputPath", min = 0, description = "Pathname where the output file is stored") String outputPath)
        throws IOException {

    GeoTiffReader reader = null;/*from   w ww.j  a  va2 s.  com*/
    List<File> removeFiles = new ArrayList<File>();
    String location = null;
    try {
        File tempFolder = config.getTempFolder();
        File loggingFolder = config.getLoggingFolder();

        // do we have to add the alpha channel?
        boolean forceTransparent = false;
        if (transparent == null) {
            transparent = true;
        }
        ColorModel cm = coverage.getRenderedImage().getColorModel();
        if (cm.getTransparency() == Transparency.OPAQUE && transparent) {
            forceTransparent = true;
        }

        // //
        //
        // STEP 1: Getting the dataset to be georectified
        //
        // //
        final Object fileSource = coverage.getProperty(GridCoverage2DReader.FILE_SOURCE_PROPERTY);
        if (fileSource != null && fileSource instanceof String) {
            location = (String) fileSource;
        }
        if (location == null) {
            RenderedImage image = coverage.getRenderedImage();
            if (forceTransparent) {
                ImageWorker iw = new ImageWorker(image);
                iw.forceComponentColorModel();
                final ImageLayout tempLayout = new ImageLayout(image);
                tempLayout.unsetValid(ImageLayout.COLOR_MODEL_MASK).unsetValid(ImageLayout.SAMPLE_MODEL_MASK);
                RenderedImage alpha = ConstantDescriptor.create(Float.valueOf(image.getWidth()),
                        Float.valueOf(image.getHeight()), new Byte[] { Byte.valueOf((byte) 255) },
                        new RenderingHints(JAI.KEY_IMAGE_LAYOUT, tempLayout));
                iw.addBand(alpha, false);
                image = iw.getRenderedImage();
                cm = image.getColorModel();
            }
            File storedImageFile = storeImage(image, tempFolder);
            location = storedImageFile.getAbsolutePath();
            removeFiles.add(storedImageFile);
        }

        // //
        //
        // STEP 2: Adding Ground Control Points
        //
        // //
        final int gcpNum[] = new int[1];
        final String gcp = parseGcps(gcps, gcpNum);
        File vrtFile = addGroundControlPoints(location, gcp, config.getGdalTranslateParameters());
        if (vrtFile == null || !vrtFile.exists() || !vrtFile.canRead()) {
            throw new IOException("Unable to get a valid file with attached Ground Control Points");
        }
        removeFiles.add(vrtFile);

        // //
        //
        // STEP 3: Warping
        //
        // //
        File warpedFile = warpFile(vrtFile, bbox, crs, width, height, warpOrder, tempFolder, loggingFolder,
                config.getExecutionTimeout(), config.getGdalWarpingParameters());
        if (warpedFile == null || !warpedFile.exists() || !warpedFile.canRead()) {
            throw new IOException("Unable to get a valid georectified file");
        }

        boolean expand = false;
        if (cm instanceof IndexColorModel) {
            expand = true;
        } else if (cm instanceof ComponentColorModel && cm.getNumComponents() == 1
                && cm.getComponentSize()[0] == 1) {
            expand = true;
        }
        if (expand) {
            removeFiles.add(warpedFile);
            warpedFile = expandRgba(warpedFile.getAbsolutePath());
        }

        // if we have the output path move the final file there
        if (Boolean.TRUE.equals(store) && outputPath != null) {
            File output = new File(outputPath);
            if (output.exists()) {
                if (!output.delete()) {
                    throw new WPSException("Output file " + outputPath + " exists but cannot be overwritten");
                }
            } else {
                File parent = output.getParentFile();
                if (!parent.exists()) {
                    if (!parent.mkdirs()) {
                        throw new WPSException("Output file parent directory " + parent.getAbsolutePath()
                                + " does not exist and cannot be created");
                    }
                }
            }
            if (!warpedFile.renameTo(output)) {
                throw new WPSException("Could not move " + warpedFile.getAbsolutePath() + " to " + outputPath
                        + ", it's likely a permission issue");
            }
            warpedFile = output;
        }

        // mark the output file for deletion at the end of request
        if (resourceManager != null && !Boolean.TRUE.equals(store)) {
            resourceManager.addResource(new WPSFileResource(warpedFile));
        }

        // //
        //
        // FINAL STEP: Returning the warped gridcoverage
        //
        // //
        reader = new GeoTiffReader(warpedFile);
        GridCoverage2D cov = addLocationProperty(reader.read(null), warpedFile);

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("result", cov);
        result.put("path", warpedFile.getAbsolutePath());
        return result;
    } finally {
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
                // Does nothing
            }
        }

        for (File file : removeFiles) {
            deleteFile(file);
        }
    }
}

From source file:org.photovault.image.PhotovaultImage.java

/**
 Create a color mapping LUT based on current color channel mapping.
 @return the created LUT. If not channel mapping is specified, returns an
 identity mapping.//from  w w  w.ja va2s  . co  m
 */
private LookupTableJAI createColorMappingLUT() {
    ColorModel colorModel = this.getCorrectedImageColorModel();
    ColorSpace colorSpace = colorModel.getColorSpace();

    int[] componentSizes = colorModel.getComponentSize();
    ColorCurve valueCurve = null;
    ColorCurve[] componentCurves = new ColorCurve[componentSizes.length];
    boolean[] applyValueCurve = new boolean[componentSizes.length];
    for (int n = 0; n < componentSizes.length; n++) {
        applyValueCurve[n] = false;
    }

    if (channelMap != null) {
        valueCurve = channelMap.getChannelCurve("value");
        switch (colorSpace.getType()) {
        case ColorSpace.TYPE_GRAY:
            // Gray scale image - just apply value curve to 1st channel
            componentCurves[0] = valueCurve;
            break;
        case ColorSpace.TYPE_RGB:
            componentCurves[0] = channelMap.getChannelCurve("red");
            componentCurves[1] = channelMap.getChannelCurve("green");
            componentCurves[2] = channelMap.getChannelCurve("blue");
            applyValueCurve[0] = true;
            applyValueCurve[1] = true;
            applyValueCurve[2] = true;
            break;
        default:
            // Unsupported color format. Just return identity transform
            break;
        }
    }
    if (valueCurve == null) {
        // No color mapping found, use identity mapping
        valueCurve = new ColorCurve();
    }

    LookupTableJAI jailut = null;
    if (componentSizes[0] == 8) {
        byte[][] lut = new byte[componentSizes.length][256];
        double dx = 1.0 / 256.0;
        for (int band = 0; band < colorModel.getNumComponents(); band++) {
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band < componentCurves.length && componentCurves[band] != null) {
                    val = componentCurves[band].getValue(val);
                }
                if (band < applyValueCurve.length && applyValueCurve[band]) {
                    val = valueCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (byte) ((lut[band].length - 1) * val);
            }
        }
        jailut = new LookupTableJAI(lut);
    } else if (componentSizes[0] == 16) {
        short[][] lut = new short[componentSizes.length][0x10000];
        double dx = 1.0 / 65536.0;
        for (int band = 0; band < colorModel.getNumComponents(); band++) {
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band < componentCurves.length && componentCurves[band] != null) {
                    val = componentCurves[band].getValue(val);
                }
                if (band < applyValueCurve.length && applyValueCurve[band]) {
                    val = valueCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (short) ((lut[band].length - 1) * val);
            }
        }
        jailut = new LookupTableJAI(lut, true);
    } else {
        log.error("Unsupported data type with with = " + componentSizes[0]);
    }
    return jailut;
}

From source file:org.photovault.image.PhotovaultImage.java

/**
 Create lookup table for saturation correction. The operation is done 
 in IHS color space, so the lookup table will contain identity mapping
 for intensity & hue channels and map based "saturation" curve from channelMap
 for saturation. If "saturation curve is nonexistent, use identity mapping for 
 saturation as well.//from  w w w  .ja v  a  2  s .  c  o  m
         
 */
private LookupTableJAI createSaturationMappingLUT() {
    ColorModel colorModel = this.getCorrectedImageColorModel();

    int[] componentSizes = colorModel.getComponentSize();
    ColorCurve satCurve = null;
    if (channelMap != null) {
        satCurve = channelMap.getChannelCurve("saturation");
    }
    if (satCurve == null) {
        satCurve = new ColorCurve();
    }

    LookupTableJAI jailut = null;
    if (componentSizes[0] == 8) {
        byte[][] lut = new byte[componentSizes.length][256];
        double dx = 1.0 / 256.0;
        for (int band = 0; band < componentSizes.length; band++) {
            // Saturation
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band == 2) {
                    val = satCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (byte) ((lut[band].length - 1) * val);
            }
        }

        jailut = new LookupTableJAI(lut);
    } else if (componentSizes[0] == 16) {
        short[][] lut = new short[componentSizes.length][0x10000];
        double dx = 1.0 / 65536.0;
        for (int band = 0; band < componentSizes.length; band++) {
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band == 2) {
                    val = satCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (short) ((lut[band].length - 1) * val);
            }
        }
        jailut = new LookupTableJAI(lut, true);
    } else {
        log.error("Unsupported data type with with = " + componentSizes[0]);
    }
    return jailut;
}

From source file:org.photovault.image.PhotovaultImage.java

/**
 Add saturation mapping into from of the image processing pipeline.
 @param src The image to which saturation correction is applied
 @return Saturation change operator.//ww w  .j  av a  2  s . c om
 */
protected RenderableOp getSaturated(RenderableOp src) {
    IHSColorSpace ihs = IHSColorSpace.getInstance();
    ColorModel srcCm = getCorrectedImageColorModel();
    int[] componentSizes = srcCm.getComponentSize();
    if (componentSizes.length != 3) {
        // This is not an RGB image
        // TODO: handle also images with alpha channel
        return null;
    }
    ColorModel ihsColorModel = new ComponentColorModel(ihs, componentSizes, false, false, Transparency.OPAQUE,
            srcCm.getTransferType());

    // Create a ParameterBlock for the conversion.
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(src);
    pb.add(ihsColorModel);
    // Do the conversion.
    RenderableOp ihsImage = JAI.createRenderable("colorconvert", pb);
    ihsImage.setProperty("org.photovault.opname", "color_corrected_ihs_image");

    //        saturatedIhsImage =
    //                MultiplyConstDescriptor.createRenderable( ihsImage, new double[] {1.0, 1.0, saturation}, null );
    LookupTableJAI jailut = createSaturationMappingLUT();
    saturatedIhsImage = LookupDescriptor.createRenderable(ihsImage, jailut, null);
    saturatedIhsImage.setProperty("org.photovault.opname", "saturated_ihs_image");
    pb = new ParameterBlock();
    pb.addSource(saturatedIhsImage);
    ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel srgbColorModel = new ComponentColorModel(sRGB, componentSizes, false, false, Transparency.OPAQUE,
            srcCm.getTransferType());
    pb.add(srgbColorModel); // RGB color model!        
    RenderableOp saturatedImage = JAI.createRenderable("colorconvert", pb);
    saturatedImage.setProperty("org.photovault.opname", "saturated_image");

    return saturatedImage;
}