Java BufferedImage Operation ensureIntRGB(final BufferedImage img)

Here you can find the source of ensureIntRGB(final BufferedImage img)

Description

Ensure that an RGB image is the same kind of RGB, so that the int arrays can be treated as storing the pixels as packed RGB values.

License

Open Source License

Parameter

Parameter Description
img a parameter

Declaration

public static BufferedImage ensureIntRGB(final BufferedImage img) 

Method Source Code


//package com.java2s;
/*-/*from  w  w w .j a  va2  s.  com*/
 * #%L
 * This file is part of QuPath.
 * %%
 * Copyright (C) 2014 - 2016 The Queen's University of Belfast, Northern Ireland
 * Contact: IP Management (ipmanagement@qub.ac.uk)
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * Ensure that an RGB image is the same kind of RGB, so that the int arrays can be treated as 
     * storing the pixels as packed RGB values.
     * 
     * Running this command results in byte array variations, or BGR images are converted to have BufferedImage.TYPE_INT_RGB.
     * 
     * Images that are already RGB, or RGBA are returned unchanged.
     * 
     * @param img
     * @return
     */
    public static BufferedImage ensureIntRGB(final BufferedImage img) {
        if (img == null)
            return null;
        switch (img.getType()) {
        case BufferedImage.TYPE_3BYTE_BGR:
        case BufferedImage.TYPE_4BYTE_ABGR:
        case BufferedImage.TYPE_4BYTE_ABGR_PRE:
        case BufferedImage.TYPE_INT_BGR:
            BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
            //         BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics2D g2d = img2.createGraphics();
            g2d.drawImage(img, 0, 0, null);
            g2d.dispose();
            return img2;
        }
        return img;
    }
}

Related

  1. doInGraphics(final BufferedImage image, final Consumer consumer)
  2. duplicate(BufferedImage image)
  3. DuplicateBufferedImage(BufferedImage bi)
  4. duplicateImage(BufferedImage image)
  5. dye(BufferedImage image, Color color)
  6. ensureRGBAImage(BufferedImage img)
  7. fadeImageByShape(BufferedImage bimg, Shape arbshape, double alpha, int rule)
  8. fadeImages(BufferedImage source1, BufferedImage source2, BufferedImage target, int relX, int targetX)
  9. fakeAOI(final BufferedImage pImage, final Rectangle pSourceRegion)