Search in sources :

Example 11 with AffineTransform

use of java.awt.geom.AffineTransform in project OpenRefine by OpenRefine.

the class ScatterplotFacet method createRotationMatrix.

public static AffineTransform createRotationMatrix(int rotation, double l) {
    if (rotation == ScatterplotFacet.ROTATE_CW) {
        AffineTransform t = AffineTransform.getTranslateInstance(0, l / 2);
        t.scale(s_rotateScale, s_rotateScale);
        t.rotate(-Math.PI / 4);
        return t;
    } else if (rotation == ScatterplotFacet.ROTATE_CCW) {
        AffineTransform t = AffineTransform.getTranslateInstance(l / 2, 0);
        t.scale(s_rotateScale, s_rotateScale);
        t.rotate(Math.PI / 4);
        return t;
    } else {
        return null;
    }
}
Also used : AffineTransform(java.awt.geom.AffineTransform)

Example 12 with AffineTransform

use of java.awt.geom.AffineTransform in project jmonkeyengine by jMonkeyEngine.

the class ImageUtils method resizeTo.

/**
     * Resizes the image to the given width and height.
     * @param source
     *            the source image (this remains untouched, the new image instance is created)
     * @param width
     *            the target image width
     * @param height
     *            the target image height
     * @return the resized image
     */
public static Image resizeTo(Image source, int width, int height) {
    BufferedImage sourceImage = ImageToAwt.convert(source, false, false, 0);
    double scaleX = width / (double) sourceImage.getWidth();
    double scaleY = height / (double) sourceImage.getHeight();
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage scaledImage = bilinearScaleOp.filter(sourceImage, new BufferedImage(width, height, sourceImage.getType()));
    return ImageUtils.toJmeImage(scaledImage, source.getFormat());
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp)

Example 13 with AffineTransform

use of java.awt.geom.AffineTransform in project AozoraEpub3 by hmdev.

the class ImageUtils method writeImage.

/** 大きすぎる画像は縮小して出力
	 * @param is 画像の入力ストリーム srcImageがあれば利用しないのでnull
	 * @param srcImage 読み込み済の場合は画像をこちらに設定 nullならisから読み込む
	 * @param zos 出力先Zipストリーム
	 * @param imageInfo 画像情報
	 * @param jpegQuality jpeg画質 (低画質 0.0-1.0 高画質)
	 * @param maxImagePixels 縮小する画素数
	 * @param maxImageW 縮小する画像幅
	 * @param maxImageH 縮小する画像高さ
	 * @param dispW 画面幅 余白除去後の縦横比補正用
	 * @param dispH 画面高さ 余白除去後の縦横比補正用
	 * @param autoMarginLimitH 余白除去 最大%
	 * @param autoMarginLimitV 余白除去 最大%
	 * @param autoMarginWhiteLevel 白画素として判別する白さ 100が白
	 * @param autoMarginPadding 余白除去後に追加するマージン */
public static void writeImage(InputStream is, BufferedImage srcImage, ZipArchiveOutputStream zos, ImageInfo imageInfo, float jpegQuality, LookupOp gammaOp, int maxImagePixels, int maxImageW, int maxImageH, int dispW, int dispH, int autoMarginLimitH, int autoMarginLimitV, int autoMarginWhiteLevel, float autoMarginPadding, int autoMarginNombre, float nombreSize) throws IOException {
    try {
        String ext = imageInfo.getExt();
        int imgW = imageInfo.getWidth();
        int imgH = imageInfo.getHeight();
        int w = imgW;
        int h = imgH;
        imageInfo.setOutWidth(imgW);
        imageInfo.setOutHeight(imgH);
        //余白チェック時に読み込んだ画像のバッファ
        byte[] imgBuf = null;
        //回転とコントラスト調整なら読み込んでおく
        if (srcImage == null && (imageInfo.rotateAngle != 0 || gammaOp != null))
            srcImage = readImage(ext, is);
        int[] margin = null;
        if (autoMarginLimitH > 0 || autoMarginLimitV > 0) {
            //1%
            int startPixel = (int) (w * 0.01);
            //3%
            int ignoreEdge = (int) (w * 0.03);
            //1%
            int dustSize = (int) (w * 0.01);
            //画像がなければ読み込み 変更なしの時にそのまま出力できるように一旦バッファに読み込む
            if (srcImage == null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(is, baos);
                imgBuf = baos.toByteArray();
                ByteArrayInputStream bais = new ByteArrayInputStream(imgBuf);
                try {
                    srcImage = readImage(ext, bais);
                } finally {
                    bais.close();
                }
            }
            margin = getPlainMargin(srcImage, autoMarginLimitH / 100f, autoMarginLimitV / 100f, autoMarginWhiteLevel / 100f, autoMarginPadding / 100f, startPixel, ignoreEdge, dustSize, autoMarginNombre, nombreSize);
            if (margin[0] == 0 && margin[1] == 0 && margin[2] == 0 && margin[3] == 0)
                margin = null;
            if (margin != null) {
                //元画像が幅か高さかチェック
                int mw = w - margin[0] - margin[2];
                int mh = h - margin[1] - margin[3];
                double dWH = dispW / (double) dispH;
                double mWH = mw / (double) mh;
                //縦横比で画面の横か縦に合わせる方向が変わらないようにマージンを調整する
                if (w / (double) h < dWH) {
                    //元が縦
                    if (mWH > dWH && mw > dispW) {
                        //余白除去で横にはみ出す
                        mh = (int) (mw / dWH);
                        //下マージンを伸ばす
                        margin[3] = h - margin[1] - mh;
                        if (margin[3] < 0) {
                            margin[3] = 0;
                            margin[1] = h - mh;
                        }
                    }
                } else {
                    //元が横
                    if (mWH < dWH && mh > dispH) {
                        //余白除去で縦にはみ出す
                        mw = (int) (mh * dWH);
                        double mLR = margin[0] + margin[2];
                        margin[0] = (int) ((w - mw) * margin[0] / mLR);
                        margin[2] = (int) ((w - mw) * margin[2] / mLR);
                    }
                }
                w = mw;
                h = mh;
            }
        }
        //倍率取得
        double scale = 1;
        //最大画素数指定
        if (maxImagePixels >= 10000)
            scale = Math.sqrt((double) maxImagePixels / (w * h));
        //最大幅指定
        if (maxImageW > 0)
            scale = Math.min(scale, (double) maxImageW / w);
        //最大高さ指定
        if (maxImageH > 0)
            scale = Math.min(scale, (double) maxImageH / h);
        if (scale >= 1 && (gammaOp == null || srcImage.getType() == BufferedImage.TYPE_INT_RGB)) {
            if (srcImage == null) {
                //変更なしならそのままファイル出力
                IOUtils.copy(is, zos);
            } else {
                if (margin == null && imgBuf != null && imageInfo.rotateAngle == 0) {
                    //余白除去が無く画像も編集されていなければバッファからそのまま出力
                    ByteArrayInputStream bais = new ByteArrayInputStream(imgBuf);
                    try {
                        IOUtils.copy(bais, zos);
                    } finally {
                        bais.close();
                    }
                } else {
                    //編集済の画像なら同じ画像形式で書き出し 余白があれば切り取る
                    if (imageInfo.rotateAngle != 0) {
                        BufferedImage outImage = new BufferedImage(h, w, srcImage.getType());
                        Graphics2D g = outImage.createGraphics();
                        try {
                            g.setColor(Color.WHITE);
                            g.fillRect(0, 0, h, w);
                            g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                            g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                            int x = 0;
                            int y = 0;
                            AffineTransform at;
                            if (imageInfo.rotateAngle == 90) {
                                at = AffineTransform.getQuadrantRotateInstance(1, 0, 0);
                                at.translate(0, -imgH);
                                if (margin != null) {
                                    x = -margin[3];
                                    y = -margin[0];
                                }
                            } else {
                                at = AffineTransform.getQuadrantRotateInstance(-1, 0, 0);
                                at.translate(-imgW, 0);
                                if (margin != null) {
                                    x = -margin[1];
                                    y = -margin[2];
                                }
                            }
                            AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
                            g.drawImage(srcImage, ato, x, y);
                        } finally {
                            g.dispose();
                        }
                        //入れ替え
                        srcImage = outImage;
                    } else if (margin != null)
                        srcImage = srcImage.getSubimage(margin[0], margin[1], srcImage.getWidth() - margin[2] - margin[0], srcImage.getHeight() - margin[3] - margin[1]);
                    if (gammaOp != null) {
                        BufferedImage filterdImage = new BufferedImage(srcImage.getWidth(), srcImage.getHeight(), BufferedImage.TYPE_INT_RGB);
                        srcImage = gammaOp.filter(srcImage, filterdImage);
                        srcImage = filterdImage;
                    }
                    _writeImage(zos, srcImage, ext, jpegQuality);
                    imageInfo.setOutWidth(srcImage.getWidth());
                    imageInfo.setOutHeight(srcImage.getHeight());
                    if (imageInfo.rotateAngle != 0)
                        LogAppender.println("画像回転" + ": " + imageInfo.getOutFileName() + " (" + h + "," + w + ")");
                }
            }
        } else {
            //縮小
            int scaledW = (int) (w * scale + 0.5);
            int scaledH = (int) (h * scale + 0.5);
            if (imageInfo.rotateAngle != 0) {
                scaledW = (int) (h * scale + 0.5);
                scaledH = (int) (w * scale + 0.5);
            }
            //画像がなければ読み込み
            if (srcImage == null)
                srcImage = readImage(ext, is);
            int imageType = srcImage.getType();
            BufferedImage outImage;
            ColorModel colorModel;
            WritableRaster raster;
            switch(gammaOp == null ? imageType : BufferedImage.TYPE_INT_RGB) {
                case BufferedImage.TYPE_BYTE_BINARY:
                    colorModel = srcImage.getColorModel();
                    colorModel = getGray16ColorModel();
                    raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                    outImage = new BufferedImage(colorModel, raster, true, null);
                    break;
                case BufferedImage.TYPE_BYTE_INDEXED:
                    colorModel = srcImage.getColorModel();
                    raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                    outImage = new BufferedImage(colorModel, raster, true, null);
                    break;
                /*case BufferedImage.TYPE_BYTE_GRAY:
				//PngEncoderのGRAYが薄くなるのでindexにする
				colorModel = srcImage.getColorModel();
				if (colorModel.getPixelSize() <= 4) colorModel = getGray16ColorModel();
				else colorModel = getGray256ColorModel();
				raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
				outImage = new BufferedImage(colorModel, raster, true, null);
				break;*/
                case BufferedImage.TYPE_BYTE_GRAY:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_BYTE_GRAY);
                    break;
                case BufferedImage.TYPE_USHORT_GRAY:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_USHORT_GRAY);
                    break;
                default:
                    outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
            }
            Graphics2D g = outImage.createGraphics();
            try {
                if (imageType == BufferedImage.TYPE_BYTE_BINARY && imageType == BufferedImage.TYPE_BYTE_INDEXED && imageType == BufferedImage.TYPE_INT_ARGB) {
                    g.setColor(Color.WHITE);
                    g.fillRect(0, 0, scaledW, scaledH);
                }
                g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
                int x = 0;
                int y = 0;
                if (imageInfo.rotateAngle == 0) {
                    if (margin != null) {
                        x = (int) (-margin[0] * scale + 0.5);
                        y = (int) (-margin[1] * scale + 0.5);
                    }
                } else if (imageInfo.rotateAngle == 90) {
                    at.rotate(Math.toRadians(imageInfo.rotateAngle), 0, 0);
                    at.translate(0, -imgH);
                    if (margin != null) {
                        x = (int) (-margin[3] * scale + 0.5);
                        y = (int) (-margin[0] * scale + 0.5);
                    }
                } else {
                    at.quadrantRotate(-1, 0, 0);
                    at.translate(-imgW, 0);
                    if (margin != null) {
                        x = (int) (-margin[1] * scale + 0.5);
                        y = (int) (-margin[2] * scale + 0.5);
                    }
                }
                AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
                g.drawImage(srcImage, ato, x, y);
            } finally {
                g.dispose();
            }
            //コントラスト調整
            if (gammaOp != null) {
                BufferedImage filterdImage = new BufferedImage(outImage.getWidth(), outImage.getHeight(), BufferedImage.TYPE_INT_RGB);
                outImage = gammaOp.filter(outImage, filterdImage);
                outImage = filterdImage;
                filterdImage = null;
                //インデックス化
                switch(imageType) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        colorModel = srcImage.getColorModel();
                        colorModel = getGray16ColorModel();
                        raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                        filterdImage = new BufferedImage(colorModel, raster, true, null);
                        break;
                    case BufferedImage.TYPE_BYTE_INDEXED:
                        colorModel = srcImage.getColorModel();
                        raster = colorModel.createCompatibleWritableRaster(scaledW, scaledH);
                        filterdImage = new BufferedImage(colorModel, raster, true, null);
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        filterdImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_BYTE_GRAY);
                        break;
                    case BufferedImage.TYPE_USHORT_GRAY:
                        filterdImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_USHORT_GRAY);
                        break;
                }
                if (filterdImage != null) {
                    g = filterdImage.createGraphics();
                    try {
                        g.drawImage(outImage, 0, 0, null);
                    } finally {
                        g.dispose();
                    }
                    outImage = filterdImage;
                }
            }
            _writeImage(zos, outImage, ext, jpegQuality);
            imageInfo.setOutWidth(outImage.getWidth());
            imageInfo.setOutHeight(outImage.getHeight());
            if (scale < 1) {
                LogAppender.append("画像縮小");
                if (imageInfo.rotateAngle != 0)
                    LogAppender.append("回転");
                LogAppender.println(": " + imageInfo.getOutFileName() + " (" + w + "," + h + ")→(" + scaledW + "," + scaledH + ")");
            }
            zos.flush();
        }
    } catch (Exception e) {
        LogAppender.println("画像読み込みエラー: " + imageInfo.getOutFileName());
        e.printStackTrace();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) AffineTransformOp(java.awt.image.AffineTransformOp) ByteArrayInputStream(java.io.ByteArrayInputStream) IndexColorModel(java.awt.image.IndexColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) AffineTransform(java.awt.geom.AffineTransform)

Example 14 with AffineTransform

use of java.awt.geom.AffineTransform in project openblocks by mikaelhg.

the class BlockShapeUtil method printPath.

/** Prints out a GeneralPath.  Used for debugging only */
public static void printPath(GeneralPath gp) {
    if (gp == null) {
        System.out.println("(null path)");
        return;
    }
    int type;
    float[] segment = new float[6];
    PathIterator i = gp.getPathIterator(new AffineTransform());
    while (!i.isDone()) {
        type = i.currentSegment(segment);
        if (type == PathIterator.SEG_MOVETO) {
            System.out.println("m: (" + segment[0] + ", " + segment[1] + ")");
        } else if (type == PathIterator.SEG_LINETO) {
            System.out.println("l: (" + segment[0] + ", " + segment[1] + ")");
        } else if (type == PathIterator.SEG_QUADTO) {
            System.out.println("q: (" + segment[0] + ", " + segment[1] + "), (" + segment[2] + ", " + segment[3] + ")");
        } else if (type == PathIterator.SEG_CUBICTO) {
            System.out.println("c: (" + segment[0] + ", " + segment[1] + "), (" + segment[2] + ", " + segment[3] + "), (" + segment[4] + ", " + segment[5] + ")");
        }
        i.next();
    }
}
Also used : PathIterator(java.awt.geom.PathIterator) AffineTransform(java.awt.geom.AffineTransform)

Example 15 with AffineTransform

use of java.awt.geom.AffineTransform in project nutz by nutzam.

the class Images method rotate.

/**
     * 对一个图像进行旋转
     * 
     * @param image
     *            图像
     * @param degree
     *            旋转角度, 90 为顺时针九十度, -90 为逆时针九十度
     * @return 旋转后得图像对象
     */
public static BufferedImage rotate(BufferedImage image, int degree) {
    // 原始图象的宽度
    int iw = image.getWidth();
    // 原始图象的高度
    int ih = image.getHeight();
    int w = 0;
    int h = 0;
    int x = 0;
    int y = 0;
    degree = degree % 360;
    if (degree < 0)
        // 将角度转换到0-360度之间
        degree = 360 + degree;
    // 将角度转为弧度
    double ang = degree * 0.0174532925;
    if (degree == 180 || degree == 0 || degree == 360) {
        w = iw;
        h = ih;
    } else if (degree == 90 || degree == 270) {
        w = ih;
        h = iw;
    } else {
        int d = iw + ih;
        w = (int) (d * Math.abs(Math.cos(ang)));
        h = (int) (d * Math.abs(Math.sin(ang)));
    }
    // 确定原点坐标
    x = (w / 2) - (iw / 2);
    y = (h / 2) - (ih / 2);
    BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
    Graphics2D gs = rotatedImage.createGraphics();
    // 以给定颜色绘制旋转后图片的背景
    gs.fillRect(0, 0, w, h);
    AffineTransform at = new AffineTransform();
    // 旋转图象
    at.rotate(ang, w / 2, h / 2);
    at.translate(x, y);
    AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    op.filter(image, rotatedImage);
    image = rotatedImage;
    return image;
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) AffineTransformOp(java.awt.image.AffineTransformOp)

Aggregations

AffineTransform (java.awt.geom.AffineTransform)335 BufferedImage (java.awt.image.BufferedImage)55 Graphics2D (java.awt.Graphics2D)43 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)42 Rectangle2D (java.awt.geom.Rectangle2D)38 Point2D (java.awt.geom.Point2D)26 Paint (java.awt.Paint)22 Font (java.awt.Font)21 Shape (java.awt.Shape)21 GcSnapshot (com.android.layoutlib.bridge.impl.GcSnapshot)20 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)17 PathIterator (java.awt.geom.PathIterator)16 ArrayList (java.util.ArrayList)16 FontRenderContext (java.awt.font.FontRenderContext)15 Color (java.awt.Color)14 Area (java.awt.geom.Area)14 GeneralPath (java.awt.geom.GeneralPath)14 Rectangle (java.awt.Rectangle)11 Path2D (java.awt.geom.Path2D)11 Test (org.junit.Test)11