Search in sources :

Example 56 with ColorModel

use of java.awt.image.ColorModel in project imagej1 by imagej.

the class VirtualStack method getProcessor.

/**
 * Returns an ImageProcessor for the specified slice,
 *		were 1<=n<=nslices. Returns null if the stack is empty.
 */
public ImageProcessor getProcessor(int n) {
    if (path == null) {
        ImageProcessor ip = new ByteProcessor(getWidth(), getHeight());
        label(ip, "" + n, Color.white);
        return ip;
    }
    Opener opener = new Opener();
    opener.setSilentMode(true);
    IJ.redirectErrorMessages(true);
    ImagePlus imp = opener.openImage(path, names[n - 1]);
    IJ.redirectErrorMessages(false);
    ImageProcessor ip = null;
    int depthThisImage = 0;
    if (imp != null) {
        int w = imp.getWidth();
        int h = imp.getHeight();
        int type = imp.getType();
        ColorModel cm = imp.getProcessor().getColorModel();
        String info = (String) imp.getProperty("Info");
        if (info != null && !(info.startsWith("Software") || info.startsWith("ImageDescription")))
            labels[n - 1] = info;
        depthThisImage = imp.getBitDepth();
        ip = imp.getProcessor();
        ip.setOverlay(imp.getOverlay());
        properties = imp.getProperty("FHT") != null ? imp.getProperties() : null;
    } else {
        File f = new File(path, names[n - 1]);
        String msg = f.exists() ? "Error opening " : "File not found: ";
        ip = new ByteProcessor(getWidth(), getHeight());
        ip.invert();
        label(ip, msg + names[n - 1], Color.black);
        depthThisImage = 8;
    }
    if (depthThisImage != bitDepth) {
        switch(bitDepth) {
            case 8:
                ip = ip.convertToByte(true);
                break;
            case 16:
                ip = ip.convertToShort(true);
                break;
            case 24:
                ip = ip.convertToRGB();
                break;
            case 32:
                ip = ip.convertToFloat();
                break;
        }
    }
    if (ip.getWidth() != getWidth() || ip.getHeight() != getHeight()) {
        ImageProcessor ip2 = ip.createProcessor(getWidth(), getHeight());
        ip2.insert(ip, 0, 0);
        ip = ip2;
    }
    return ip;
}
Also used : ColorModel(java.awt.image.ColorModel)

Example 57 with ColorModel

use of java.awt.image.ColorModel in project UniversalMediaServer by UniversalMediaServer.

the class ImageIOTools method readImageInfo.

/**
 * Tries to gather the data needed to populate a {@link ImageInfo} instance
 * describing the input image.
 *
 * <p>
 * This method does not close {@code inputStream}.
 *
 * @param inputStream the image whose information to gather.
 * @param size the size of the image in bytes or
 *             {@link ImageInfo#SIZE_UNKNOWN} if it can't be determined.
 * @param metadata the {@link Metadata} instance to embed in the resulting
 *                 {@link ImageInfo} instance.
 * @param applyExifOrientation whether or not Exif orientation should be
 *            compensated for when setting width and height. This will also
 *            reset the Exif orientation information. <b>Changes will be
 *            applied to the {@code metadata} argument instance</b>.
 * @return An {@link ImageInfo} instance describing the input image.
 * @throws UnknownFormatException if the format could not be determined.
 * @throws IOException if an IO error occurred.
 */
public static ImageInfo readImageInfo(InputStream inputStream, long size, Metadata metadata, boolean applyExifOrientation) throws IOException {
    if (inputStream == null) {
        throw new IllegalArgumentException("input == null!");
    }
    try (ImageInputStream stream = createImageInputStream(inputStream)) {
        Iterator<?> iter = ImageIO.getImageReaders(stream);
        if (!iter.hasNext()) {
            throw new UnknownFormatException("Unable to find a suitable image reader");
        }
        ImageReader reader = (ImageReader) iter.next();
        try {
            int width = -1;
            int height = -1;
            ImageFormat format = ImageFormat.toImageFormat(reader.getFormatName());
            if (format == null) {
                throw new UnknownFormatException("Unable to determine image format");
            }
            ColorModel colorModel = null;
            try {
                reader.setInput(stream, true, true);
                Iterator<ImageTypeSpecifier> iterator = reader.getImageTypes(0);
                if (iterator.hasNext()) {
                    colorModel = iterator.next().getColorModel();
                }
                width = reader.getWidth(0);
                height = reader.getHeight(0);
            } catch (RuntimeException e) {
                throw new ImageIORuntimeException("Error reading image information: " + e.getMessage(), e);
            }
            boolean imageIOSupport;
            if (format == ImageFormat.TIFF) {
                // but fails when it actually tries, so we have to test it.
                try {
                    ImageReadParam param = reader.getDefaultReadParam();
                    param.setSourceRegion(new Rectangle(1, 1));
                    reader.read(0, param);
                    imageIOSupport = true;
                } catch (Exception e) {
                    // Catch anything here, we simply want to test if it fails.
                    imageIOSupport = false;
                }
            } else {
                imageIOSupport = true;
            }
            ImageInfo imageInfo = ImageInfo.create(width, height, format, size, colorModel, metadata, applyExifOrientation, imageIOSupport);
            return imageInfo;
        } finally {
            reader.dispose();
        }
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) Rectangle(java.awt.Rectangle) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IOException(java.io.IOException) IIOException(javax.imageio.IIOException) ImageIORuntimeException(net.pms.image.ImageIORuntimeException) UnknownFormatException(net.pms.util.UnknownFormatException) ImageIORuntimeException(net.pms.image.ImageIORuntimeException) ImageReadParam(javax.imageio.ImageReadParam) ImageIORuntimeException(net.pms.image.ImageIORuntimeException) ColorModel(java.awt.image.ColorModel) UnknownFormatException(net.pms.util.UnknownFormatException) ImageReader(javax.imageio.ImageReader)

Example 58 with ColorModel

use of java.awt.image.ColorModel in project com.revolsys.open by revolsys.

the class WholeImageFilter method filter.

@Override
public BufferedImage filter(final BufferedImage src, BufferedImage dst) {
    final int width = src.getWidth();
    final int height = src.getHeight();
    final int type = src.getType();
    final WritableRaster srcRaster = src.getRaster();
    this.originalSpace = new Rectangle(0, 0, width, height);
    this.transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(this.transformedSpace);
    if (dst == null) {
        final ColorModel dstCM = src.getColorModel();
        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(this.transformedSpace.width, this.transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
    }
    final WritableRaster dstRaster = dst.getRaster();
    int[] inPixels = getRGB(src, 0, 0, width, height, null);
    inPixels = filterPixels(width, height, inPixels, this.transformedSpace);
    setRGB(dst, 0, 0, this.transformedSpace.width, this.transformedSpace.height, inPixels);
    return dst;
}
Also used : WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage)

Example 59 with ColorModel

use of java.awt.image.ColorModel in project Purus-Pasta by puruscor.

the class TexI method detectfmt.

/* Java's image model is a little bit complex, so these may not be
     * entirely correct. They should be corrected if oddities are
     * detected. */
public static int detectfmt(BufferedImage img) {
    ColorModel cm = img.getColorModel();
    if (!(img.getSampleModel() instanceof PixelInterleavedSampleModel))
        return (-1);
    PixelInterleavedSampleModel sm = (PixelInterleavedSampleModel) img.getSampleModel();
    int[] cs = cm.getComponentSize();
    int[] off = sm.getBandOffsets();
    /*
    System.err.print(this + ": " + cm.getNumComponents() + ", (");
	for(int i = 0; i < off.length; i++)
	    System.err.print(((i > 0)?" ":"") + off[i]);
	System.err.print("), (");
	for(int i = 0; i < off.length; i++)
	    System.err.print(((i > 0)?" ":"") + cs[i]);
	System.err.print(")");
	System.err.println();
	*/
    if ((cm.getNumComponents() == 4) && (off.length == 4)) {
        if (((cs[0] == 8) && (cs[1] == 8) && (cs[2] == 8) && (cs[3] == 8)) && (cm.getTransferType() == DataBuffer.TYPE_BYTE) && (cm.getTransparency() == java.awt.Transparency.TRANSLUCENT)) {
            if ((off[0] == 0) && (off[1] == 1) && (off[2] == 2) && (off[3] == 3))
                return (GL.GL_RGBA);
            if ((off[0] == 2) && (off[1] == 1) && (off[2] == 0) && (off[3] == 3))
                return (GL.GL_BGRA);
        }
    } else if ((cm.getNumComponents() == 3) && (off.length == 3)) {
        if (((cs[0] == 8) && (cs[1] == 8) && (cs[2] == 8)) && (cm.getTransferType() == DataBuffer.TYPE_BYTE) && (cm.getTransparency() == java.awt.Transparency.OPAQUE)) {
            if ((off[0] == 0) && (off[1] == 1) && (off[2] == 2))
                return (GL.GL_RGB);
            if ((off[0] == 2) && (off[1] == 1) && (off[2] == 0))
                return (GL2.GL_BGR);
        }
    }
    return (-1);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel)

Example 60 with ColorModel

use of java.awt.image.ColorModel in project bioformats by openmicroscopy.

the class Colorizer method applyDisplayRanges.

// -- Helper methods --
private void applyDisplayRanges(ImagePlus imp, int series) {
    if (imp instanceof VirtualImagePlus) {
        // virtual stacks handle their own display ranges
        return;
    }
    final ImporterOptions options = process.getOptions();
    final ImageProcessorReader reader = process.getReader();
    final int pixelType = reader.getPixelType();
    final boolean autoscale = options.isAutoscale() || // always autoscale float data
    FormatTools.isFloatingPoint(pixelType);
    final int cSize = imp.getNChannels();
    final double[] cMin = new double[cSize];
    final double[] cMax = new double[cSize];
    Arrays.fill(cMin, Double.NaN);
    Arrays.fill(cMax, Double.NaN);
    if (autoscale) {
        // extract display ranges for autoscaling
        final MinMaxCalculator minMaxCalc = process.getMinMaxCalculator();
        final int cBegin = process.getCBegin(series);
        final int cStep = process.getCStep(series);
        for (int c = 0; c < cSize; c++) {
            final int cIndex = cBegin + c * cStep;
            Double cMinVal = null, cMaxVal = null;
            try {
                cMinVal = minMaxCalc.getChannelGlobalMinimum(cIndex);
                cMaxVal = minMaxCalc.getChannelGlobalMaximum(cIndex);
                if (cMinVal == null) {
                    cMinVal = minMaxCalc.getChannelKnownMinimum(cIndex);
                }
                if (cMaxVal == null) {
                    cMaxVal = minMaxCalc.getChannelKnownMaximum(cIndex);
                }
            } catch (FormatException exc) {
            } catch (IOException exc) {
            }
            if (cMinVal != null)
                cMin[c] = cMinVal;
            if (cMaxVal != null)
                cMax[c] = cMaxVal;
        }
    }
    // for calibrated data, the offset from zero
    final double zeroOffset = getZeroOffset(imp);
    // fill in default display ranges as appropriate
    final double min, max;
    if (FormatTools.isFloatingPoint(pixelType)) {
        // no defined min and max values for floating point data
        min = max = Double.NaN;
    } else {
        final int bitDepth = reader.getBitsPerPixel();
        final double halfPow = Math.pow(2, bitDepth - 1);
        final double fullPow = 2 * halfPow;
        final boolean signed = FormatTools.isSigned(pixelType);
        if (signed) {
            // signed data is centered at 0
            min = -halfPow;
            max = halfPow - 1;
        } else {
            // unsigned data begins at 0
            min = 0;
            max = fullPow - 1;
        }
        for (int c = 0; c < cSize; c++) {
            if (Double.isNaN(cMin[c]))
                cMin[c] = min;
            if (Double.isNaN(cMax[c]))
                cMax[c] = max;
        }
    }
    // apply display ranges
    if (imp instanceof CompositeImage) {
        // apply channel display ranges
        final CompositeImage compImage = (CompositeImage) imp;
        for (int c = 0; c < cSize; c++) {
            LUT lut = compImage.getChannelLut(c + 1);
            // NB: Uncalibrate values before assigning to LUT min/max.
            lut.min = cMin[c] - zeroOffset;
            lut.max = cMax[c] - zeroOffset;
        }
    } else {
        // compute global display range from channel display ranges
        double globalMin = Double.POSITIVE_INFINITY;
        double globalMax = Double.NEGATIVE_INFINITY;
        for (int c = 0; c < cSize; c++) {
            if (cMin[c] < globalMin)
                globalMin = cMin[c];
            if (cMax[c] > globalMax)
                globalMax = cMax[c];
        }
        // NB: Uncalibrate values before assigning to display range min/max.
        globalMin -= zeroOffset;
        globalMax -= zeroOffset;
        // apply global display range
        ImageProcessor proc = imp.getProcessor();
        if (proc instanceof ColorProcessor) {
            // NB: Should never occur. ;-)
            final ColorProcessor colorProc = (ColorProcessor) proc;
            colorProc.setMinAndMax(globalMin, globalMax, 3);
        } else {
            ColorModel model = proc.getColorModel();
            proc.setMinAndMax(globalMin, globalMax);
            proc.setColorModel(model);
            imp.setDisplayRange(globalMin, globalMax);
        }
    }
}
Also used : VirtualImagePlus(loci.plugins.util.VirtualImagePlus) ImageProcessorReader(loci.plugins.util.ImageProcessorReader) LUT(ij.process.LUT) IOException(java.io.IOException) FormatException(loci.formats.FormatException) ImageProcessor(ij.process.ImageProcessor) ColorProcessor(ij.process.ColorProcessor) CompositeImage(ij.CompositeImage) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) MinMaxCalculator(loci.formats.MinMaxCalculator)

Aggregations

ColorModel (java.awt.image.ColorModel)188 BufferedImage (java.awt.image.BufferedImage)92 IndexColorModel (java.awt.image.IndexColorModel)80 WritableRaster (java.awt.image.WritableRaster)57 ComponentColorModel (java.awt.image.ComponentColorModel)47 SampleModel (java.awt.image.SampleModel)40 DirectColorModel (java.awt.image.DirectColorModel)39 ColorSpace (java.awt.color.ColorSpace)28 Raster (java.awt.image.Raster)24 Rectangle (java.awt.Rectangle)21 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)20 Point (java.awt.Point)15 ComponentSampleModel (java.awt.image.ComponentSampleModel)15 Graphics2D (java.awt.Graphics2D)13 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)13 IOException (java.io.IOException)13 ColorConvertOp (java.awt.image.ColorConvertOp)12 DataBuffer (java.awt.image.DataBuffer)12 DataBufferInt (java.awt.image.DataBufferInt)11 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)11