use of java.awt.image.AffineTransformOp 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();
}
}
use of java.awt.image.AffineTransformOp in project vcell by virtualcell.
the class ClientRequestManager method resizeImage.
private static void resizeImage(FieldDataFileOperationSpec fdfos, ISize newImagesISize, int imageType) throws Exception {
final int ORIG_XYSIZE = fdfos.isize.getX() * fdfos.isize.getY();
try {
int xsize = newImagesISize.getX();
int ysize = newImagesISize.getY();
double scaleFactor = (double) newImagesISize.getX() / (double) fdfos.isize.getX();
if (xsize != fdfos.isize.getX() || ysize != fdfos.isize.getY()) {
// this normally contains different variables but is
int numChannels = fdfos.shortSpecData[0].length;
// used for channels here
// resize each z section to xsize,ysize
AffineTransform scaleAffineTransform = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
AffineTransformOp scaleAffineTransformOp = new AffineTransformOp(scaleAffineTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
short[][][] resizeData = new short[1][numChannels][fdfos.isize.getZ() * xsize * ysize];
for (int c = 0; c < numChannels; c++) {
BufferedImage originalImage = new BufferedImage(fdfos.isize.getX(), fdfos.isize.getY(), BufferedImage.TYPE_USHORT_GRAY);
BufferedImage scaledImage = new BufferedImage(xsize, ysize, BufferedImage.TYPE_USHORT_GRAY);
for (int z = 0; z < fdfos.isize.getZ(); z++) {
short[] originalImageBuffer = ((DataBufferUShort) (originalImage.getRaster().getDataBuffer())).getData();
System.arraycopy(fdfos.shortSpecData[0][c], z * ORIG_XYSIZE, originalImageBuffer, 0, ORIG_XYSIZE);
scaleAffineTransformOp.filter(originalImage, scaledImage);
short[] scaledImageBuffer = ((DataBufferUShort) (scaledImage.getRaster().getDataBuffer())).getData();
System.arraycopy(scaledImageBuffer, 0, resizeData[0][c], z * xsize * ysize, xsize * ysize);
}
}
fdfos.isize = new ISize(xsize, ysize, fdfos.isize.getZ());
fdfos.shortSpecData = resizeData;
}
} catch (Exception e) {
throw new Exception("Error scaling imported image:\n" + e.getMessage());
}
}
use of java.awt.image.AffineTransformOp in project vcell by virtualcell.
the class RasterExporter method flipPNG.
private static byte[] flipPNG(BufferedImage image) throws IOException {
// flip image vertical
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -image.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
// write out
image = op.filter(image, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
return baos.toByteArray();
}
use of java.awt.image.AffineTransformOp in project jmonkeyengine by jMonkeyEngine.
the class JmeDesktopSystem method verticalFlip.
private static BufferedImage verticalFlip(BufferedImage original) {
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -original.getHeight());
AffineTransformOp transformOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage awtImage = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics2D g2d = awtImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2d.drawImage(original, transformOp, 0, 0);
g2d.dispose();
return awtImage;
}
use of java.awt.image.AffineTransformOp in project jmonkeyengine by jMonkeyEngine.
the class AwtPanel method reshapeInThread.
private void reshapeInThread(int width, int height) {
byteBuf = BufferUtils.ensureLargeEnough(byteBuf, width * height * 4);
intBuf = byteBuf.asIntBuffer();
if (fb != null) {
fb.dispose();
fb = null;
}
fb = new FrameBuffer(width, height, 1);
fb.setDepthBuffer(Format.Depth);
fb.setColorBuffer(Format.RGB8);
fb.setSrgb(srgb);
if (attachAsMain) {
rm.getRenderer().setMainFrameBufferOverride(fb);
}
synchronized (lock) {
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
}
// synchronized (lock){
// img = (BufferedImage) getGraphicsConfiguration().createCompatibleImage(width, height);
// }
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -img.getHeight());
transformOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
for (ViewPort vp : viewPorts) {
if (!attachAsMain) {
vp.setOutputFrameBuffer(fb);
}
vp.getCamera().resize(width, height, true);
// Main framebuffer should use RenderManager.notifyReshape().
for (SceneProcessor sp : vp.getProcessors()) {
sp.reshape(vp, width, height);
}
}
}
Aggregations