Search in sources :

Example 51 with ColorModel

use of java.awt.image.ColorModel in project vcell by virtualcell.

the class GeometryClassLegendShape method getSubvolumeColor.

private Color getSubvolumeColor(SubVolume subVolume) {
    ColorModel colorModel = DisplayAdapterService.getHandleColorMap();
    int handle = subVolume.getHandle();
    return new Color(colorModel.getRGB(handle));
}
Also used : ColorModel(java.awt.image.ColorModel) Color(java.awt.Color) Point(java.awt.Point)

Example 52 with ColorModel

use of java.awt.image.ColorModel in project yamcs-studio by yamcs.

the class AWT2SWTImageConverter method convertToAWT.

static BufferedImage convertToAWT(ImageData data) {
    ColorModel colorModel = null;
    PaletteData palette = data.palette;
    if (palette.isDirect) {
        colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask);
        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                RGB rgb = palette.getRGB(pixel);
                bufferedImage.setRGB(x, y, rgb.red << 16 | rgb.green << 8 | rgb.blue);
            }
        }
        return bufferedImage;
    } else {
        RGB[] rgbs = palette.getRGBs();
        byte[] red = new byte[rgbs.length];
        byte[] green = new byte[rgbs.length];
        byte[] blue = new byte[rgbs.length];
        for (int i = 0; i < rgbs.length; i++) {
            RGB rgb = rgbs[i];
            red[i] = (byte) rgb.red;
            green[i] = (byte) rgb.green;
            blue[i] = (byte) rgb.blue;
        }
        if (data.transparentPixel != -1) {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel);
        } else {
            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
        }
        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int pixel = data.getPixel(x, y);
                pixelArray[0] = pixel;
                raster.setPixel(x, y, pixelArray);
            }
        }
        return bufferedImage;
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ColorModel(java.awt.image.ColorModel) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) RGB(org.eclipse.swt.graphics.RGB) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 53 with ColorModel

use of java.awt.image.ColorModel in project Terasology by MovingBlocks.

the class FieldFacetLayer method render.

@Override
public void render(BufferedImage img, Region region) {
    FieldFacet2D facet = region.getFacet(clazz);
    int width = img.getWidth();
    int height = img.getHeight();
    ColorModel colorModel = img.getColorModel();
    ColorBlender blender = ColorBlenders.forColorModel(ColorModels.RGBA, colorModel);
    DataBufferInt dataBuffer = (DataBufferInt) img.getRaster().getDataBuffer();
    for (int z = 0; z < height; z++) {
        for (int x = 0; x < width; x++) {
            Color col = getColor(facet, x, z);
            int src = col.rgba();
            int dst = dataBuffer.getElem(z * width + x);
            int mix = blender.add(src, dst);
            dataBuffer.setElem(z * width + x, mix);
        }
    }
}
Also used : FieldFacet2D(org.terasology.world.generation.facets.base.FieldFacet2D) ColorBlender(org.terasology.world.viewer.color.ColorBlender) ColorModel(java.awt.image.ColorModel) Color(org.terasology.rendering.nui.Color) DataBufferInt(java.awt.image.DataBufferInt)

Example 54 with ColorModel

use of java.awt.image.ColorModel in project Terasology by MovingBlocks.

the class NominalFacetLayer method render.

@Override
public void render(BufferedImage img, Region region) {
    ObjectFacet2D<E> facet = region.getFacet(facetClass);
    int width = img.getWidth();
    int height = img.getHeight();
    ColorModel colorModel = img.getColorModel();
    ColorBlender blender = ColorBlenders.forColorModel(ColorModels.RGBA, colorModel);
    DataBufferInt dataBuffer = (DataBufferInt) img.getRaster().getDataBuffer();
    for (int z = 0; z < height; z++) {
        for (int x = 0; x < width; x++) {
            Color src = getColor(facet, x, z);
            if (src != null) {
                int blend = blender.get(src.rgba());
                dataBuffer.setElem(z * width + x, blend);
            }
        }
    }
}
Also used : ColorBlender(org.terasology.world.viewer.color.ColorBlender) ColorModel(java.awt.image.ColorModel) Color(org.terasology.rendering.nui.Color) DataBufferInt(java.awt.image.DataBufferInt)

Example 55 with ColorModel

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

the class FolderOpener method run.

public void run(String arg) {
    boolean isMacro = Macro.getOptions() != null;
    String directory = null;
    if (arg != null && !arg.equals("")) {
        directory = arg;
    } else {
        if (!isMacro) {
            sortFileNames = staticSortFileNames;
            openAsVirtualStack = staticOpenAsVirtualStack;
        }
        arg = null;
        String title = "Open Image Sequence...";
        String macroOptions = Macro.getOptions();
        if (macroOptions != null) {
            directory = Macro.getValue(macroOptions, title, null);
            if (directory != null) {
                directory = OpenDialog.lookupPathVariable(directory);
                File f = new File(directory);
                if (!f.isDirectory() && (f.exists() || directory.lastIndexOf(".") > directory.length() - 5))
                    directory = f.getParent();
            }
            legacyRegex = Macro.getValue(macroOptions, "or", "");
            if (legacyRegex.equals(""))
                legacyRegex = null;
        }
        if (directory == null) {
            if (Prefs.useFileChooser && !IJ.isMacOSX()) {
                OpenDialog od = new OpenDialog(title, arg);
                directory = od.getDirectory();
                String name = od.getFileName();
                if (name == null)
                    return;
            } else
                directory = IJ.getDirectory(title);
        }
    }
    if (directory == null)
        return;
    String[] list = (new File(directory)).list();
    if (list == null)
        return;
    String title = directory;
    if (title.endsWith(File.separator) || title.endsWith("/"))
        title = title.substring(0, title.length() - 1);
    int index = title.lastIndexOf(File.separatorChar);
    if (index != -1)
        title = title.substring(index + 1);
    if (title.endsWith(":"))
        title = title.substring(0, title.length() - 1);
    IJ.register(FolderOpener.class);
    list = trimFileList(list);
    if (list == null)
        return;
    if (IJ.debugMode)
        IJ.log("FolderOpener: " + directory + " (" + list.length + " files)");
    int width = 0, height = 0, stackSize = 1, bitDepth = 0;
    ImageStack stack = null;
    double min = Double.MAX_VALUE;
    double max = -Double.MAX_VALUE;
    Calibration cal = null;
    boolean allSameCalibration = true;
    IJ.resetEscape();
    Overlay overlay = null;
    n = list.length;
    start = 1;
    increment = 1;
    try {
        if (isMacro) {
            if (!showDialog(null, list))
                return;
        } else {
            for (int i = 0; i < list.length; i++) {
                Opener opener = new Opener();
                opener.setSilentMode(true);
                IJ.redirectErrorMessages(true);
                ImagePlus imp = opener.openImage(directory, list[i]);
                IJ.redirectErrorMessages(false);
                if (imp != null) {
                    width = imp.getWidth();
                    height = imp.getHeight();
                    bitDepth = imp.getBitDepth();
                    if (arg == null) {
                        if (!showDialog(imp, list))
                            return;
                    }
                    break;
                }
            }
            if (width == 0) {
                IJ.error("Sequence Reader", "This folder does not appear to contain\n" + "any TIFF, JPEG, BMP, DICOM, GIF, FITS or PGM files.\n \n" + "   \"" + directory + "\"");
                return;
            }
        }
        String pluginName = "Sequence Reader";
        if (legacyRegex != null)
            pluginName += "(legacy)";
        list = getFilteredList(list, filter, pluginName);
        if (list == null)
            return;
        IJ.showStatus("");
        t0 = System.currentTimeMillis();
        if (sortFileNames)
            list = StringSorter.sortNumerically(list);
        if (n < 1)
            n = list.length;
        if (start < 1 || start > list.length)
            start = 1;
        if (start + n - 1 > list.length)
            n = list.length - start + 1;
        int count = 0;
        int counter = 0;
        ImagePlus imp = null;
        boolean firstMessage = true;
        boolean fileInfoStack = false;
        for (int i = start - 1; i < list.length; i++) {
            if ((counter++ % increment) != 0)
                continue;
            Opener opener = new Opener();
            opener.setSilentMode(true);
            IJ.redirectErrorMessages(true);
            if ("RoiSet.zip".equals(list[i])) {
                IJ.open(directory + list[i]);
                imp = null;
            } else if (!openAsVirtualStack || stack == null) {
                imp = opener.openImage(directory, list[i]);
                stackSize = imp != null ? imp.getStackSize() : 1;
            }
            IJ.redirectErrorMessages(false);
            if (imp != null && stack == null) {
                width = imp.getWidth();
                height = imp.getHeight();
                bitDepth = imp.getBitDepth();
                fi = imp.getOriginalFileInfo();
                ImageProcessor ip = imp.getProcessor();
                min = ip.getMin();
                max = ip.getMax();
                cal = imp.getCalibration();
                if (convertToRGB)
                    bitDepth = 24;
                ColorModel cm = imp.getProcessor().getColorModel();
                if (openAsVirtualStack) {
                    if (stackSize > 1) {
                        stack = new FileInfoVirtualStack();
                        fileInfoStack = true;
                    } else
                        stack = new VirtualStack(width, height, cm, directory);
                    ((VirtualStack) stack).setBitDepth(bitDepth);
                } else if (scale < 100.0)
                    stack = new ImageStack((int) (width * scale / 100.0), (int) (height * scale / 100.0), cm);
                else
                    stack = new ImageStack(width, height, cm);
                info1 = (String) imp.getProperty("Info");
            }
            if (imp == null)
                continue;
            if (imp.getWidth() != width || imp.getHeight() != height) {
                IJ.log(list[i] + ": wrong size; " + width + "x" + height + " expected, " + imp.getWidth() + "x" + imp.getHeight() + " found");
                continue;
            }
            String label = imp.getTitle();
            if (stackSize == 1) {
                String info = (String) imp.getProperty("Info");
                if (info != null)
                    label += "\n" + info;
            }
            if (Math.abs(imp.getCalibration().pixelWidth - cal.pixelWidth) > 0.0000000001)
                allSameCalibration = false;
            ImageStack inputStack = imp.getStack();
            Overlay overlay2 = imp.getOverlay();
            if (overlay2 != null && !openAsVirtualStack) {
                if (overlay == null)
                    overlay = new Overlay();
                for (int j = 0; j < overlay2.size(); j++) {
                    Roi roi = overlay2.get(j);
                    int position = roi.getPosition();
                    if (position == 0)
                        roi.setPosition(count + 1);
                    overlay.add(roi);
                }
            }
            if (openAsVirtualStack) {
                if (fileInfoStack)
                    openAsFileInfoStack((FileInfoVirtualStack) stack, directory + list[i]);
                else
                    ((VirtualStack) stack).addSlice(list[i]);
            } else {
                for (int slice = 1; slice <= stackSize; slice++) {
                    int bitDepth2 = imp.getBitDepth();
                    String label2 = label;
                    ImageProcessor ip = null;
                    if (stackSize > 1) {
                        String sliceLabel = inputStack.getSliceLabel(slice);
                        if (sliceLabel != null)
                            label2 = sliceLabel;
                        else if (label2 != null && !label2.equals(""))
                            label2 += ":" + slice;
                    }
                    ip = inputStack.getProcessor(slice);
                    if (convertToRGB) {
                        ip = ip.convertToRGB();
                        bitDepth2 = 24;
                    }
                    if (bitDepth2 != bitDepth) {
                        if (bitDepth == 8 && bitDepth2 == 24) {
                            ip = ip.convertToByte(true);
                            bitDepth2 = 8;
                        } else if (bitDepth == 32) {
                            ip = ip.convertToFloat();
                            bitDepth2 = 32;
                        } else if (bitDepth == 24) {
                            ip = ip.convertToRGB();
                            bitDepth2 = 24;
                        }
                    }
                    if (bitDepth2 != bitDepth) {
                        IJ.log(list[i] + ": wrong bit depth; " + bitDepth + " expected, " + bitDepth2 + " found");
                        break;
                    }
                    if (scale < 100.0)
                        ip = ip.resize((int) (width * scale / 100.0), (int) (height * scale / 100.0));
                    if (ip.getMin() < min)
                        min = ip.getMin();
                    if (ip.getMax() > max)
                        max = ip.getMax();
                    stack.addSlice(label2, ip);
                }
            }
            count++;
            IJ.showStatus(count + "/" + n);
            IJ.showProgress(count, n);
            if (count >= n)
                break;
            if (IJ.escapePressed()) {
                IJ.beep();
                break;
            }
        }
    } catch (OutOfMemoryError e) {
        IJ.outOfMemory("FolderOpener");
        if (stack != null)
            stack.trim();
    }
    if (stack != null && stack.getSize() > 0) {
        ImagePlus imp2 = new ImagePlus(title, stack);
        if (imp2.getType() == ImagePlus.GRAY16 || imp2.getType() == ImagePlus.GRAY32)
            imp2.getProcessor().setMinAndMax(min, max);
        if (fi == null)
            fi = new FileInfo();
        fi.fileFormat = FileInfo.UNKNOWN;
        fi.fileName = "";
        fi.directory = directory;
        // saves FileInfo of the first image
        imp2.setFileInfo(fi);
        imp2.setOverlay(overlay);
        if (stack instanceof VirtualStack) {
            Properties props = ((VirtualStack) stack).getProperties();
            if (props != null)
                imp2.setProperty("FHT", props.get("FHT"));
        }
        if (allSameCalibration) {
            // use calibration from first image
            if (scale != 100.0 && cal.scaled()) {
                cal.pixelWidth /= scale / 100.0;
                cal.pixelHeight /= scale / 100.0;
            }
            if (cal.pixelWidth != 1.0 && cal.pixelDepth == 1.0)
                cal.pixelDepth = cal.pixelWidth;
            imp2.setCalibration(cal);
        }
        if (info1 != null && info1.lastIndexOf("7FE0,0010") > 0) {
            stack = DicomTools.sort(stack);
            imp2.setStack(stack);
            double voxelDepth = DicomTools.getVoxelDepth(stack);
            if (voxelDepth > 0.0) {
                if (IJ.debugMode)
                    IJ.log("DICOM voxel depth set to " + voxelDepth + " (" + cal.pixelDepth + ")");
                cal.pixelDepth = voxelDepth;
                imp2.setCalibration(cal);
            }
        }
        if (imp2.getStackSize() == 1) {
            imp2.setProperty("Label", list[0]);
            if (info1 != null)
                imp2.setProperty("Info", info1);
        }
        if (arg == null && !saveImage) {
            String time = (System.currentTimeMillis() - t0) / 1000.0 + " seconds";
            imp2.show(time);
            if (stack.isVirtual()) {
                overlay = stack.getProcessor(1).getOverlay();
                if (overlay != null)
                    imp2.setOverlay(overlay);
            }
        }
        if (saveImage)
            image = imp2;
    }
    IJ.showProgress(1.0);
}
Also used : Calibration(ij.measure.Calibration) Properties(java.util.Properties) ColorModel(java.awt.image.ColorModel)

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