Package com.google.code.appengine.awt.image

Examples of com.google.code.appengine.awt.image.BufferedImage


     */
    protected void fill(Shape shape, Paint paint) {
        Rectangle2D bounds = shape.getBounds2D();

        // create image
        BufferedImage image = new BufferedImage(
            (int)Math.ceil(bounds.getWidth()) + 1,
            (int)Math.ceil(bounds.getHeight()) + 1,
            BufferedImage.TYPE_INT_ARGB);

        // fill background
        Graphics2D graphics = image.createGraphics();
        graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
        graphics.fill(graphics.getDeviceConfiguration().getBounds());
        graphics.setComposite(AlphaComposite.SrcOver);

        // draw paint
View Full Code Here


*/
public final class BitmapPainter implements ImagePainter {
    protected POILogger logger = POILogFactory.getLogger(this.getClass());

    public void paint(Graphics2D graphics, PictureData pict, Picture parent) {
        BufferedImage img;
        try {
               img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
        }
        catch (Exception e){
            logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType());
View Full Code Here

    public byte[] getData(){
         byte[] data = super.getData();
          try {
              //PNG created on MAC may have a 16-byte prefix which prevents successful reading.
              //Just cut it off!.
              BufferedImage bi = ImageIO.read(new ByteArrayInputStream(data));
              if (bi == null){
                  byte[] png = new byte[data.length-16];
                  System.arraycopy(data, 16, png, 0, png.length);
                  data = png;
              }
View Full Code Here

        AffineTransform imageTransform = new AffineTransform(
            1.0, 0.0, 0.0, -1.0, 0.0, image.getHeight());
        imageTransform.preConcatenate(xform);
        writeTransform(imageTransform);

        BufferedImage bufferedImage = ImageUtilities.createBufferedImage(
            image, null, null);
        AlphaBlend alphaBlend = new AlphaBlend(
            imageBounds,
            toUnit(0),
            toUnit(0),
View Full Code Here

    }

    public static RenderedImage createRenderedImage(Image image, ImageObserver observer, Color bkg) {
        if ((bkg == null) && (image instanceof RenderedImage)) return (RenderedImage)image;

        BufferedImage bufferedImage = new BufferedImage(
            image.getWidth(observer),
            image.getHeight(observer),
            (bkg == null) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.getGraphics();
        if (bkg == null) {
            g.drawImage(image, 0, 0, observer);
        } else {
            g.drawImage(image, 0, 0, bkg, observer);
        }
View Full Code Here

    }

    public static RenderedImage createRenderedImage(RenderedImage image, Color bkg) {
        if (bkg == null) return image;

        BufferedImage bufferedImage = new BufferedImage(
            image.getWidth(),
            image.getHeight(),
            BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D)bufferedImage.getGraphics();
        g.setBackground(bkg);
        g.clearRect(0, 0, image.getWidth(), image.getHeight());
        g.drawRenderedImage(image, new AffineTransform());
        return bufferedImage;
    }
View Full Code Here

                graphics.fill(anchor);
                break;
            case Fill.FILL_PICTURE:
                PictureData data = f.getPictureData();
                if (data instanceof Bitmap) {
                    BufferedImage img = null;
                    try {
                        img = ImageIO.read(new ByteArrayInputStream(data.getData()));
                    } catch (Exception e) {
                        logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + data.getType());
                        return;
                    }
                    Image scaledImg = img.getScaledInstance(anchor.width, anchor.height, Image.SCALE_SMOOTH);
                    graphics.drawImage(scaledImg, anchor.x, anchor.y, null);

                }
                break;
            default:
View Full Code Here

        // NOTE: special case for WBMP which only
        // supports on color band with sample size 1
        // (which means black / white with no gray scale)
        if (ImageConstants.WBMP.equalsIgnoreCase(format)) {
            return new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
        }

        // NOTE: special case for JPEG which has no Alpha
        if (ImageConstants.JPG.equalsIgnoreCase(format)) {
            return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }

        // NOTE: special case for BMP which has no Alpha
        if (ImageConstants.BMP.equalsIgnoreCase(format)) {
            return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }

        return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }
View Full Code Here

        }
        ImageReader reader = (ImageReader) iterator.next();

        ImageInputStream iis = ImageIO.createImageInputStream(is);
        reader.setInput(iis, true);
        BufferedImage image = reader.read(0);
        reader.dispose();
        iis.close();
        return image;
    }
View Full Code Here

            red = emf.readUnsignedByte();
            /*unused = */ emf.readUnsignedByte();

            int color2 = new Color(red, green, blue).getRGB();

            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            int[] data = emf.readUnsignedByte(len - 8);

            // TODO: this is highly experimental and does
            // not work for the tested examples
            int strangeOffset = width % 8;
            if (strangeOffset != 0) {
                strangeOffset = 8 - strangeOffset;
            }

            // iterator for pixel data
            int pixel = 0;

            // mask for getting the bits from a pixel data byte
            int[] mask = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

            // image data are swapped compared to java standard
            for (int y = height - 1; y > -1; y--) {
                for (int x = 0; x < width; x++) {
                    int pixelDataGroup = data[pixel / 8];
                    int pixelData = pixelDataGroup & mask[pixel % 8];
                    pixel ++;

                    if (pixelData > 0) {
                        result.setRGB(x, y, color2);
                    } else {
                        result.setRGB(x, y, color1);
                    }
                }
                // add the extra width
                pixel = pixel + strangeOffset;
            }

            /* for debugging: shows every loaded image
            javax.swing.JFrame f = new javax.swing.JFrame("test");
            f.getContentPane().setBackground(Color.green);
            f.getContentPane().setLayout(
                new com.google.code.appengine.awt.BorderLayout(0, 0));
            f.getContentPane().add(
                com.google.code.appengine.awt.BorderLayout.CENTER,
                new javax.swing.JLabel(
                    new javax.swing.ImageIcon(result)));
            f.setSize(new com.google.code.appengine.awt.Dimension(width + 20, height + 20));
            f.setVisible(true);*/

            return result;

        } else if ((bmi.getBitCount() == 8) &&
            (bmi.getCompression() == EMFConstants.BI_RGB)) {
            // 8   The bitmap has a maximum of 256 colors, and the bmiColors member
            // of BITMAPINFO contains up to 256 entries. In this case, each byte in
            // the array represents a single pixel.

            // TODO has to be done in BitMapInfoHeader?
            // read the color table
            int colorsUsed = bmi.getClrUsed();

            // typedef struct tagRGBQUAD {
            //   BYTE    rgbBlue;
            //   BYTE    rgbGreen;
            //   BYTE    rgbRed;
            //   BYTE    rgbReserved;
            // } RGBQUAD;
            int[] colors = emf.readUnsignedByte(colorsUsed * 4);

            // data a indexes to a certain color in the colortable.
            // Each byte represents a pixel
            int[] data = emf.readUnsignedByte(len - (colorsUsed * 4));

            // convert it to a color table
            int[] colorTable = new int[256];
            // iterator for color data
            int color = 0;
            for (int i = 0; i < colorsUsed; i++, color = i * 4) {
                colorTable[i] = new Color(
                    colors[color + 2],
                    colors[color + 1],
                    colors[color]).getRGB();
            }

            // fill with black to avoid ArrayIndexOutOfBoundExceptions;
            // somme images seem to use more colors than stored in ClrUsed
            if (colorsUsed < 256) {
                Arrays.fill(colorTable, colorsUsed, 256, 0);
            }

            // don't know why, but the width has to be adjusted ...
            // it took more than an hour to determine the strangeOffset
            int strangeOffset = width % 4;
            if (strangeOffset != 0) {
                strangeOffset = 4 - strangeOffset;
            }

            // create the image
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // iterator for pixel data
            int pixel = 0;

            // image data are swapped compared to java standard
            for (int y = height - 1; y > -1; y--) {
                for (int x = 0; x < width; x++) {
                    result.setRGB(x, y, colorTable[data[pixel++]]);
                }
                // add the extra width
                pixel = pixel + strangeOffset;
            }

            return result;
        }

        // The bitmap has a maximum of 2^16 colors. If the biCompression member
        // of the BITMAPINFOHEADER is BI_RGB, the bmiColors member of BITMAPINFO is
        // NULL.
        else if ((bmi.getBitCount() == 16) &&
            (bmi.getCompression() == EMFConstants.BI_RGB)) {

            // Each WORD in the bitmap array represents a single pixel. The
            // relative intensities of red, green, and blue are represented with
            // five bits for each color component. The value for blue is in the least
            // significant five bits, followed by five bits each for green and red.
            // The most significant bit is not used. The bmiColors color table is used
            // for optimizing colors used on palette-based devices, and must contain
            // the number of entries specified by the biClrUsed member of the
            // BITMAPINFOHEADER.
            int[] data = emf.readDWORD(len / 4);

            // don't know why, by the width has to be the half ...
            // maybe that has something to do with sie HALFTONE rendering setting.
            width = (width + (width % 2)) / 2;
            // to avoid ArrayIndexOutOfBoundExcesptions
            height = data.length / width / 2;

            // create a non transparent image
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // found no sample and color model to mak this work
            // tag.image.setRGB(0, 0, tag.widthSrc, tag.heightSrc, data, 0, 0);

            // used in the loop
            int off = 0;
            int pixel, neighbor;

            // image data are swapped compared to java standard
            for (int y = height - 1; y > -1; y--, off = off + width) {
                for (int x = 0; x < width; x++) {
                    neighbor = data[off + width];
                    pixel = data[off++];

                    // compute the average of the pixel and it's neighbor
                    // and set the reulting color values
                    result.setRGB(x, y, new Color(
                        // 0xF800 = 2 * 0x7C00
                        (float)((pixel & 0x7C00) + (neighbor & 0x7C00)) / 0xF800,
                        (float)((pixel & 0x3E0) + (neighbor & 0x3E0)) / 0x7C0,
                        (float)((pixel & 0x1F) + (neighbor & 0x1F)) / 0x3E).getRGB());
                }
            }

            /* for debugging: shows every loaded image
            javax.swing.JFrame f = new javax.swing.JFrame("test");
            f.getContentPane().setBackground(Color.green);
            f.getContentPane().setLayout(
                new com.google.code.appengine.awt.BorderLayout(0, 0));
            f.getContentPane().add(
                com.google.code.appengine.awt.BorderLayout.CENTER,
                new javax.swing.JLabel(
                    new javax.swing.ImageIcon(result)));
            f.pack();
            f.setVisible(true);*/

            return result;
        }
        // The bitmap has a maximum of 2^32 colors. If the biCompression member of the
        // BITMAPINFOHEADER is BI_RGB, the bmiColors member of BITMAPINFO is NULL.
        else if ((bmi.getBitCount() == 32) &&
            (bmi.getCompression() == EMFConstants.BI_RGB)) {
            // Each DWORD in the bitmap array represents the relative intensities of blue,
            // green, and red, respectively, for a pixel. The high byte in each DWORD is not
            // used. The bmiColors color table is used for optimizing colors used on
            // palette-based devices, and must contain the number of entries specified
            // by the biClrUsed member of the BITMAPINFOHEADER.

            width = (width + (width % 20)) / 20;
            height = (height + (height % 20)) / 20;

            // create a transparent image
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            // read the image data
            int[] data = emf.readDWORD(len / 4);

            // used to iterate the pixels later
            int off = 0;
            int pixel;
            int alpha;

            // The SourceConstantaAlpha member of BLENDFUNCTION specifies an alpha transparency
            // value to be used on the entire source bitmap. The SourceConstantAlpha value is
            // combined with any per-pixel alpha values. If SourceConstantAlpha is 0, it is
            // assumed that the image is transparent. Set the SourceConstantAlpha value to 255
            // (which indicates that the image is opaque) when you only want to use per-pixel
            // alpha values.
            int sourceConstantAlpha = blendFunction.getSourceConstantAlpha();

            if (blendFunction.getAlphaFormat() != EMFConstants.AC_SRC_ALPHA) {
                // If the source bitmap has no per-pixel alpha value (that is, AC_SRC_ALPHA is not
                // set), the SourceConstantAlpha value determines the blend of the source and
                // destination bitmaps, as shown in the following table. Note that SCA is used
                // for SourceConstantAlpha here. Also, SCA is divided by 255 because it has a
                // value that ranges from 0 to 255.

                // Dst.Red   = Src.Red * (SCA/255.0)   + Dst.Red * (1.0 - (SCA/255.0))
                // Dst.Green   = Src.Green * (SCA/255.0)   + Dst.Green * (1.0 - (SCA/255.0))
                // Dst.Blue   = Src.Blue * (SCA/255.0)   + Dst.Blue * (1.0 - (SCA/255.0))

                // If the destination bitmap has an alpha channel, then the blend is as follows.
                // Dst.Alpha   = Src.Alpha * (SCA/255.0)   + Dst.Alpha * (1.0 - (SCA/255.0))

                for (int y = height - 1; y > -1 && off < data.length; y--) {
                    for (int x = 0; x < width && off < data.length; x++) {
                        pixel = data[off++];

                        result.setRGB(x, y, new Color(
                            (pixel & 0xFF0000) >> 16,
                            (pixel & 0xFF00) >> 8,
                            (pixel & 0xFF),
                            // TODO not tested
                            sourceConstantAlpha
                        ).getRGB());
                    }
                }
            }
            // When the BlendOp parameter is AC_SRC_OVER , the source bitmap is placed over
            // the destination bitmap based on the alpha values of the source pixels.
            else {
                // If the source bitmap does not use SourceConstantAlpha (that is, it equals
                // 0xFF), the per-pixel alpha determines the blend of the source and destination
                // bitmaps, as shown in the following table.
                if (sourceConstantAlpha == 0xFF) {
                    // Dst.Red   = Src.Red   + (1 - Src.Alpha) * Dst.Red
                    // Dst.Green   = Src.Green   + (1 - Src.Alpha) * Dst.Green
                    // Dst.Blue   = Src.Blue   + (1 - Src.Alpha) * Dst.Blue

                    // If the destination bitmap has an alpha channel, then the blend is as follows.
                    // Dest.alpha   = Src.Alpha   + (1 - SrcAlpha) * Dst.Alpha

                    // image data are swapped compared to java standard
                    for (int y = height - 1; y > -1 && off < data.length; y--) {
                        for (int x = 0; x < width && off < data.length; x++) {
                            pixel = data[off++];
                            alpha = (pixel & 0xFF000000) >> 24;
                            if (alpha == -1) {
                                alpha = 0xFF;
                            }

                            result.setRGB(x, y, new Color(
                                (pixel & 0xFF0000) >> 16,
                                (pixel & 0xFF00) >> 8,
                                (pixel & 0xFF),
                                alpha
                            ).getRGB());
                        }
                    }
                }

                // If the source has both the SourceConstantAlpha (that is, it is not 0xFF)
                // and per-pixel alpha, the source is pre-multiplied by the SourceConstantAlpha
                // and then the blend is based on the per-pixel alpha. The following tables show
                // this. Note that SourceConstantAlpha is divided by 255 because it has a value
                // that ranges from 0 to 255.
                else {
                    // Src.Red   = Src.Red   * SourceConstantAlpha / 255.0;
                    // Src.Green   = Src.Green   * SourceConstantAlpha / 255.0;
                    // Src.Blue   = Src.Blue   * SourceConstantAlpha / 255.0;
                    // Src.Alpha   = Src.Alpha   * SourceConstantAlpha / 255.0;

                    // Dst.Red   = Src.Red   + (1 - Src.Alpha) * Dst.Red
                    // Dst.Green   = Src.Green   + (1 - Src.Alpha) * Dst.Green
                    // Dst.Blue   = Src.Blue   + (1 - Src.Alpha) * Dst.Blue
                    // Dst.Alpha   = Src.Alpha   + (1 - Src.Alpha) * Dst.Alpha

                    for (int y = height - 1; y > -1 && off < data.length; y--) {
                        for (int x = 0; x < width && off < data.length; x++) {
                            pixel = data[off++];

                            alpha = (pixel & 0xFF000000) >> 24;
                            if (alpha == -1) {
                                alpha = 0xFF;
                            }

                            // TODO not tested
                            alpha = alpha * sourceConstantAlpha / 0xFF;

                            result.setRGB(x, y, new Color(
                                (pixel & 0xFF0000) >> 16,
                                (pixel & 0xFF00) >> 8,
                                (pixel & 0xFF),
                                alpha
                            ).getRGB());
View Full Code Here

TOP

Related Classes of com.google.code.appengine.awt.image.BufferedImage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.