Search in sources :

Example 86 with ColorModel

use of java.awt.image.ColorModel in project robolectric by robolectric.

the class ShadowBitmap method copy.

@Implementation
protected Bitmap copy(Bitmap.Config config, boolean isMutable) {
    Bitmap newBitmap = ReflectionHelpers.callConstructor(Bitmap.class);
    ShadowBitmap shadowBitmap = Shadow.extract(newBitmap);
    shadowBitmap.createdFromBitmap = realBitmap;
    shadowBitmap.config = config;
    shadowBitmap.mutable = isMutable;
    shadowBitmap.height = getHeight();
    shadowBitmap.width = getWidth();
    if (bufferedImage != null) {
        ColorModel cm = bufferedImage.getColorModel();
        WritableRaster raster = bufferedImage.copyData(bufferedImage.getRaster().createCompatibleWritableRaster());
        shadowBitmap.bufferedImage = new BufferedImage(cm, raster, false, null);
    }
    return newBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage) Implementation(org.robolectric.annotation.Implementation)

Example 87 with ColorModel

use of java.awt.image.ColorModel in project scriptographer by scriptographer.

the class Raster method createCompatibleImage.

/**
 * @jshide
 */
public BufferedImage createCompatibleImage(int width, int height) {
    ColorModel cm = getColorModel();
    WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
    return new BufferedImage(cm, raster, false, null);
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) WritableRaster(java.awt.image.WritableRaster) BufferedImage(java.awt.image.BufferedImage)

Example 88 with ColorModel

use of java.awt.image.ColorModel in project scriptographer by scriptographer.

the class Raster method getCompatibleType.

/**
 * @jshide
 */
public static ColorType getCompatibleType(Image image) {
    if (image instanceof BufferedImage) {
        ColorModel cm = ((BufferedImage) image).getColorModel();
        int type = cm.getColorSpace().getType();
        boolean alpha = cm.hasAlpha();
        if (type == ColorSpace.TYPE_RGB) {
            return alpha ? ColorType.ARGB : ColorType.RGB;
        } else if (type == ColorSpace.TYPE_CMYK) {
            return alpha ? ColorType.ACMYK : ColorType.CMYK;
        }
        if (type == ColorSpace.TYPE_GRAY) {
            return alpha ? ColorType.AGRAY : ColorType.GRAY;
        }
    }
    return null;
}
Also used : ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) BufferedImage(java.awt.image.BufferedImage)

Example 89 with ColorModel

use of java.awt.image.ColorModel 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 90 with ColorModel

use of java.awt.image.ColorModel in project Smack by igniterealtime.

the class WholeImageFilter method filter.

@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();
    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);
    if (dst == null) {
        ColorModel dstCM = src.getColorModel();
        dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
    }
    WritableRaster dstRaster = dst.getRaster();
    int[] inPixels = getRGB(src, 0, 0, width, height, null);
    inPixels = filterPixels(width, height, inPixels, transformedSpace);
    setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);
    return dst;
}
Also used : WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel) Rectangle(java.awt.Rectangle) BufferedImage(java.awt.image.BufferedImage)

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