Example usage for java.awt.image BufferedImage getRGB

List of usage examples for java.awt.image BufferedImage getRGB

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getRGB.

Prototype

public int getRGB(int x, int y) 

Source Link

Document

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.

Usage

From source file:preprocessing.Utils.java

public static BufferedImage limpiarBordeImagen(BufferedImage numero1, int proporcionX, int proporcionY) {
    BufferedImage nuevo = numero1;
    if (proporcionX == -1)
        proporcionX = 1;//from  w w w.  j av  a2s  .c  o m
    if (proporcionY == -1)
        proporcionY = 1;
    int inicioY, inicioX, finY, finX;
    inicioY = 0;
    for (int i = 0; i < numero1.getHeight() / proporcionY; i++) {
        int[] linea = new int[numero1.getWidth()];
        for (int j = 0; j < numero1.getWidth(); j++)
            linea[j] = numero1.getRGB(j, i);
        if (esLineaNegra(linea)) {
            inicioY = i + 1;
        }
    }

    finY = numero1.getHeight() - 1;
    for (int i = numero1.getHeight() - 1; i > (numero1.getHeight()
            - (numero1.getHeight() / proporcionY)); i--) {
        int[] linea = new int[numero1.getWidth()];
        for (int j = 0; j < numero1.getWidth(); j++)
            linea[j] = numero1.getRGB(j, i);
        if (esLineaNegra(linea)) {
            finY = i - 1;
        }
    }

    inicioX = 0;
    for (int i = 0; i < numero1.getWidth() / proporcionX; i++) {
        int[] linea = new int[numero1.getHeight()];
        for (int j = 0; j < numero1.getHeight(); j++)
            linea[j] = numero1.getRGB(i, j);
        if (esLineaNegra(linea)) {
            inicioX = i + 1;
        }
    }

    finX = numero1.getWidth() - 1;
    for (int i = numero1.getWidth() - 1; i > (numero1.getWidth() - (numero1.getWidth() / proporcionX)); i--) {
        int[] linea = new int[numero1.getHeight()];
        for (int j = 0; j < numero1.getHeight(); j++)
            linea[j] = numero1.getRGB(i, j);
        if (esLineaNegra(linea)) {
            finX = i - 1;
        }
    }
    return numero1.getSubimage(inicioX, inicioY, (finX - inicioX), (finY - inicioY));
}

From source file:cn.z.Ocr5.java

public static List<BufferedImage> splitImage(BufferedImage img, String filename) throws Exception {
    final List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final List<Integer> weightlist = new ArrayList<Integer>();
    for (int x = 0; x < width; ++x) {
        int count = 0;
        for (int y = 0; y < height; ++y) {
            if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from ww  w. jav  a2s  .  c  o m*/
            }
        }
        weightlist.add(count);
    }
    for (int i = 0; i < weightlist.size(); i++) {
        int length = 0;
        while (i < weightlist.size() && weightlist.get(i) > 0) {
            i++;
            length++;
        }
        if (length > 18) {
            subImgs.add(CommonUtil.removeBlank(img.getSubimage(i - length, 0, length / 2, height),
                    whiteThreshold, 0));
            subImgs.add(CommonUtil.removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height),
                    whiteThreshold, 0));
        } else if (length > 5) {
            subImgs.add(
                    CommonUtil.removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    //        for(int i = 0; i < subImgs.size(); i++) {
    //            FileOutputStream fos = new FileOutputStream("D:\\test\\img" + filename + i + ".jpg");
    //            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
    //            encoder.encode(subImgs.get(i));
    //            fos.close();
    //        }
    return subImgs;
}

From source file:de.fhg.igd.swingrcp.SwingRCPUtilities.java

/**
 * Convert a {@link BufferedImage} to a SWT Image.
 * //from   ww  w .ja v a  2 s  . co  m
 * {@link "http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java?view=co"}
 * 
 * @param bufferedImage the AWT {@link BufferedImage}
 * @return the SWT {@link ImageData}
 */
public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
                colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
                // also set the alpha value (ST)
                data.setAlpha(x, y, colorModel.getAlpha(rgb));
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(),
                colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        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++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

private static List<RectangularRegion> findSubImageInImage(BufferedImage subImage, BufferedImage image,
        int max) {
    Map<Integer, List<Point>> rgb2offsets = new HashMap<Integer, List<Point>>();
    int sw = subImage.getWidth();
    int sh = subImage.getHeight();
    for (int x = 0; x < sw; ++x) {
        for (int y = 0; y < sh; ++y) {
            int argb = subImage.getRGB(x, y);
            int a = argb >>> 24;
            if (a == 255) {
                Integer rgb = argb & 0xFFFFFF;
                List<Point> offsets = rgb2offsets.get(rgb);
                if (offsets == null) {
                    offsets = new ArrayList<Point>();
                    rgb2offsets.put(rgb, offsets);
                }//from   w  ww .ja  va2  s .co  m
                offsets.add(new Point(x, y));
            }
        }
    }
    List<RectangularRegion> result = new ArrayList<RectangularRegion>();
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] p = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                p[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
    } else if (raster.getTransferType() == DataBuffer.TYPE_INT) {
        for (int x = 0; x < w; ++x) {
            p[x] = (int[]) raster.getDataElements(x, 0, 1, h, null);
        }
    } else {
        throw new RuntimeException("findSubImageInImage not implemented for image transfer type "
                + raster.getTransferType() + " yet.");
    }
    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            Iterator<Map.Entry<Integer, List<Point>>> i = rgb2offsets.entrySet().iterator();
            compareWithSubImageLoop: while (i.hasNext()) {
                Map.Entry<Integer, List<Point>> mapEntry = i.next();
                int expectedRgb = mapEntry.getKey();
                for (Point offset : mapEntry.getValue()) {
                    int xx = x + offset.x;
                    int yy = y + offset.y;
                    if (xx >= w || yy >= h || expectedRgb != p[xx][yy]) {
                        break compareWithSubImageLoop;
                    }
                }
                if (!i.hasNext()) {
                    result.add(new RectangularRegion(x, y, x + (sw - 1), y + (sh - 1)));
                    if (result.size() == max) {
                        return result;
                    }
                }
            }
        }
    }
    return result;
}

From source file:de.darkblue.bongloader2.utils.ToolBox.java

public static BufferedImage grayScaleAlpha(BufferedImage original) {

    int alpha, red, green, blue;
    int newPixel;

    BufferedImage avg_gray = new BufferedImage(original.getWidth(), original.getHeight(), original.getType());
    int[] avgLUT = new int[766];
    for (int i = 0; i < avgLUT.length; i++) {
        avgLUT[i] = (int) (i / 3);
    }//from   ww w .  j av a2s. c o  m

    for (int x = 0; x < original.getWidth(); x++) {
        for (int y = 0; y < original.getHeight(); y++) {

            // Get pixels by R, G, B
            int color = original.getRGB(x, y);
            alpha = color & 0xFF000000;

            red = (color >> 16) & 0xFF;
            green = (color >> 8) & 0xFF;
            blue = color & 0xFF;

            newPixel = red + green + blue;
            newPixel = avgLUT[newPixel];
            // Return back to original format
            newPixel = newPixel | (newPixel << 8) | (newPixel << 16) | alpha;

            // Write pixels into image
            avg_gray.setRGB(x, y, newPixel);

        }
    }

    return avg_gray;

}

From source file:net.rptools.lib.image.ImageUtil.java

public static int pickBestTransparency(BufferedImage image) {
    // See if we can short circuit
    ColorModel colorModel = image.getColorModel();
    if (colorModel.getTransparency() == Transparency.OPAQUE) {
        return Transparency.OPAQUE;
    }//  ww w . j a  v a 2  s.c  o m
    // Get the pixels
    int width = image.getWidth();
    int height = image.getHeight();

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }
            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }
    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:com.t3.image.ImageUtil.java

public static int pickBestTransparency(BufferedImage image) {

    // See if we can short circuit
    ColorModel colorModel = image.getColorModel();
    if (colorModel.getTransparency() == Transparency.OPAQUE) {
        return Transparency.OPAQUE;
    }//  w w  w .ja v  a  2 s.  c o m

    // Get the pixels
    int width = image.getWidth();
    int height = image.getHeight();

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = image.getRGB(x, y);
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }

            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }

    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:cn.z.Ocr5.java

public static BufferedImage removeBackgroud(File picFile) throws Exception {

    BufferedImage img = ImageIO.read(picFile);
    final int width = img.getWidth();
    final int height = img.getHeight();

    // int blackThreshold = 300;

    // img.getMinX() img.getMinY()
    for (int x = img.getMinX(); x < width; x++) {
        for (int y = img.getMinY(); y < height; y++) {

            Color color = new Color(img.getRGB(x, y));
            if ((color.getBlue() < 120) || ((color.getRed() + color.getGreen() + color.getBlue()) < 50)) { //
                img.setRGB(x, y, Color.WHITE.getRGB());
            } else if ((color.getRed() + color.getGreen() + color.getBlue()) < 400) {
                img.setRGB(x, y, Color.BLACK.getRGB());
            }/*  w ww. j  a  va2 s. c  o m*/

            int nearly = 0;
            int vertical = 0;
            int horizontal = 0;

            if (x > 0) {
                Color leftColor = new Color(img.getRGB(x - 1, y));
                if ((leftColor.getRed() + leftColor.getGreen() + leftColor.getBlue()) < 400) {
                    nearly++;
                    horizontal++;
                }
            }
            if (x < width - 1) {
                Color rightColor = new Color(img.getRGB(x + 1, y));
                if ((rightColor.getRed() + rightColor.getGreen() + rightColor.getBlue()) < 400) {
                    nearly++;
                    horizontal++;
                }
            }
            if (y > 0) {
                Color topColor = new Color(img.getRGB(x, y - 1));
                if ((topColor.getRed() + topColor.getGreen() + topColor.getBlue()) < 400) {
                    nearly++;
                    vertical++;
                }
            }
            if (y < height - 1) {
                Color bottomColor = new Color(img.getRGB(x, y + 1));
                if ((bottomColor.getRed() + bottomColor.getGreen() + bottomColor.getBlue()) < 400) {
                    nearly++;
                    vertical++;
                }
            }

            if (x > 0 && y > 0) {
                Color leftTopColor = new Color(img.getRGB(x - 1, y - 1));
                if ((leftTopColor.getRed() + leftTopColor.getGreen() + leftTopColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x < width - 1 && y < height - 1) {
                Color rightBottomColor = new Color(img.getRGB(x + 1, y + 1));
                if ((rightBottomColor.getRed() + rightBottomColor.getGreen()
                        + rightBottomColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x < width - 1 && y > 0) {
                Color rightTopColor = new Color(img.getRGB(x + 1, y - 1));
                if ((rightTopColor.getRed() + rightTopColor.getGreen() + rightTopColor.getBlue()) < 400) {
                    nearly++;
                }
            }
            if (x > 0 && y < height - 1) {
                Color leftBottomColor = new Color(img.getRGB(x - 1, y + 1));
                if ((leftBottomColor.getRed() + leftBottomColor.getGreen() + leftBottomColor.getBlue()) < 400) {
                    nearly++;
                }
            }

            if (nearly < 2) {
                img.setRGB(x, y, Color.WHITE.getRGB());
            }
            /*
            if (horizontal < 1 && vertical > 0) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            if (horizontal > 0 && vertical < 1) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 1) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            } else {
            img.setRGB(x, y, Color.BLACK.getRGB());
            }
                    
                    
            if (getColorBright(img.getRGB(x, y)) < 100) {
            int count = isBlack(img.getRGB(x - 1, y), blackThreshold) + isBlack(img.getRGB(x + 1, y), blackThreshold) + isBlack(img.getRGB(x, y - 1), blackThreshold) + isBlack(img.getRGB(x, y + 1), blackThreshold) + isBlack(img.getRGB(x + 1, y + 1), blackThreshold) + isBlack(img.getRGB(x - 1, y - 1), blackThreshold) + isBlack(img.getRGB(x + 1, y -1 ), blackThreshold) + isBlack(img.getRGB(x - 1, y + 1), blackThreshold);
            System.out.println(count);
            if (count < 2) {
                img.setRGB(x, y, Color.WHITE.getRGB());
            }
            //     img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            //                if(getColorBright(img.getRGB(x, y)) > 600) {
            //                    img.setRGB(x, y, Color.WHITE.getRGB());
            //                } else {
            //                    /*
            //                    // ?Graphics2D
            //                    Graphics2D g2d = img.createGraphics();
            //                    // ?
            //                    // 1.0f? 0-1.0????
            //                    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            //                    // 
            //                    g2d.setColor(new Color(255,0,0));
            //                    g2d.setStroke(new BasicStroke(1));
            //                    // g2d.draw
            //                    // 
            //                    // ? ?
            //                    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            //                    g2d.dispose();
            //                    */
            //
            //                    img.setRGB(x, y, Color.BLACK.getRGB());
            //                    /*
            //                    System.out.println(getColorBright(img.getRGB(x, y)) + ":");
            //                    System.out.println(getColorBright(img.getRGB(x + 1, y)) + "-" + getColorBright(img.getRGB(x + 1, y + 1)) + "-" + getColorBright(img.getRGB(x, y + 1)));
            //                    System.out.println(getColorBright(img.getRGB(x - 1, y)) + "-" + getColorBright(img.getRGB(x - 1, y - 1)) + "-" + getColorBright(img.getRGB(x, y - 1)));
            //                    System.out.println(getColorBright(img.getRGB(x - 1, y + 1)) + "-" + getColorBright(img.getRGB(x + 1, y - 1)));
            //                    */
            //
            //                    /*
            //                    int i = 0;
            //                    i = ((x < width - 1) && getColorBright(img.getRGB(x + 1, y)) < 30)? i + 1 : i;
            //                    i = ((x < width - 1) && (y < height - 1) && getColorBright(img.getRGB(x + 1, y + 1)) < 30)? i + 1 : i;
            //                    i = ((y < height - 1) && getColorBright(img.getRGB(x, y + 1)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && getColorBright(img.getRGB(x - 1, y)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && (y > 0) && getColorBright(img.getRGB(x - 1, y - 1)) < 30)? i + 1 : i;
            //                    i = ((y > 0) && getColorBright(img.getRGB(x, y - 1)) < 30)? i + 1 : i;
            //                    i = ((x < width - 1) && (y > 0) && getColorBright(img.getRGB(x + 1, y - 1)) < 30)? i + 1 : i;
            //                    i = ((x > 0) && (y < height - 1) && getColorBright(img.getRGB(x - 1, y + 1)) < 30)? i + 1 : i;
            //
            //                    if(i > 1) {
            //                        img.setRGB(x, y, Color.BLACK.getRGB());
            //                    } else {
            //                        img.setRGB(x, y, Color.WHITE.getRGB());
            //                    }
            //                    */
            //                }

            /*
            int i = 0;
            i = (getColorBright(img.getRGB(x + 1, y)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x + 1, y + 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x, y + 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x - 1, y)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x - 1, y - 1)) == 0)? i + 1 : i;
            i = (getColorBright(img.getRGB(x, y - 1)) == 0)? i + 1 : i;
                    
            System.out.println(getColorBright(img.getRGB(x, y)) + ":");
            System.out.println(getColorBright(img.getRGB(x + 1, y)) + "-" + getColorBright(img.getRGB(x + 1, y + 1)) + "-" + getColorBright(img.getRGB(x, y + 1)));
            System.out.println(getColorBright(img.getRGB(x - 1, y)) + "-" + getColorBright(img.getRGB(x - 1, y - 1)) + "-" + getColorBright(img.getRGB(x, y - 1)));
            System.out.println(getColorBright(img.getRGB(x - 1, y + 1)) + "-" + getColorBright(img.getRGB(x + 1, y - 1)));
            if(getColorBright(img.getRGB(x, y)) == 0 &&  i < 3) {
            img.setRGB(x, y, Color.WHITE.getRGB());
            }
            */

            /*
            // ?for????
            // ??object
            Object data = img.getRaster().getDataElements(x, y, null);
            int red = img.getColorModel().getRed(data);
            int blue = img.getColorModel().getBlue(data);
            int green = img.getColorModel().getGreen(data);
            System.out.println((red + blue + green) + "-" + getColorBright(img.getRGB(x, y)));
            red = (red * 3 + green * 6 + blue * 1)/10;
            green = red;
            blue = green;
                    
            // r?g?b?rgbbufferedImage????rgbrgb8388608?255*255*255?16777216
            int rgb = (red * 256 + green) * 256 + blue;
            if(rgb > 8388608) {
            rgb = rgb - 16777216;
            }
            // rgb
            img.setRGB(x, y, rgb);
            */

        }
    }
    // img = img.getSubimage(1, 1, img.getWidth() - 2, img.getHeight() - 2);
    return img;
}

From source file:net.cloudkit.relaxation.VerifyImage.java

public static List<BufferedImage> splitImage(BufferedImage img) throws Exception {
    final List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final List<Integer> weightList = new ArrayList<Integer>();
    for (int x = 0; x < width; ++x) {
        int count = 0;
        for (int y = 0; y < height; ++y) {
            if (isWhite(img.getRGB(x, y), whiteThreshold) == 0) {
                count++;/*from w w  w  .  j  a  v  a  2s .c o m*/
            }
        }
        weightList.add(count);
    }
    for (int i = 0; i < weightList.size(); i++) {
        int length = 0;
        while (i < weightList.size() && weightList.get(i) > 0) {
            i++;
            length++;
        }
        if (length > 18) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length / 2, height), whiteThreshold, 0));
            subImgs.add(removeBlank(img.getSubimage(i - length / 2, 0, length / 2, height), whiteThreshold, 0));
        } else if (length > 5) {
            subImgs.add(removeBlank(img.getSubimage(i - length, 0, length, height), whiteThreshold, 0));
        }
    }

    return subImgs;
}

From source file:nl.b3p.kaartenbalie.service.KBImageTool.java

/** Reads an image from an http input stream.
 *
 * @param is Inputstream// w w  w .  j  a v  a 2 s.c  om
 * @param mime String representing the mime type of the image.
 *
 * @return BufferedImage
 *
 * @throws Exception
 */
// <editor-fold defaultstate="" desc="readImage(GetMethod method, String mime) method.">
public static BufferedImage readImage(InputStream is, String mime, ServiceProviderRequest wmsRequest)
        throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead = 0;
    byte[] buffer = new byte[2048];
    while (bytesRead != -1) {
        bytesRead = is.read(buffer, 0, buffer.length);
        if (bytesRead > 0)
            baos.write(buffer, 0, bytesRead);
    }

    if (mime.indexOf(";") != -1) {
        mime = mime.substring(0, mime.indexOf(";"));
    }
    String mimeType = getMimeType(mime);
    if (mimeType == null) {
        String message = baos.toString();
        message = message.replaceAll("(\\r|\\n)", "");
        log.error("Response from server not understood (mime = " + mime + "): " + message);
        throw new Exception("Response from server not understood (mime = " + mime + "): " + message);
    }

    ImageReader ir = getReader(mimeType);
    if (ir == null) {
        log.error("no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1));
        throw new Exception(
                "no reader available for imageformat: " + mimeType.substring(mimeType.lastIndexOf("/") + 1));
    }

    //TODO Make smarter.. Possibly faster... But keep reporting!
    wmsRequest.setBytesReceived(new Long(baos.size()));
    ImageInputStream stream = ImageIO.createImageInputStream(new ByteArrayInputStream(baos.toByteArray()));
    ir.setInput(stream, true);
    try {
        //if image is a png, has no alpha and has a tRNS then make that color transparent.
        BufferedImage i = ir.read(0);
        if (!i.getColorModel().hasAlpha() && ir.getImageMetadata(0) instanceof PNGMetadata) {
            PNGMetadata metadata = (PNGMetadata) ir.getImageMetadata(0);
            if (metadata.tRNS_present) {
                int alphaPix = (metadata.tRNS_red << 16) | (metadata.tRNS_green << 8) | (metadata.tRNS_blue);
                BufferedImage tmp = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB);
                for (int x = 0; x < i.getWidth(); x++) {
                    for (int y = 0; y < i.getHeight(); y++) {
                        int rgb = i.getRGB(x, y);
                        rgb = (rgb & 0xFFFFFF) == alphaPix ? alphaPix : rgb;
                        tmp.setRGB(x, y, rgb);
                    }
                }
                i = tmp;
            }
        }
        return i;
    } finally {
        ir.dispose();
    }
}