List of usage examples for java.awt.image FilteredImageSource FilteredImageSource
public FilteredImageSource(ImageProducer orig, ImageFilter imgf)
From source file:com.chiorichan.factory.event.PostImageProcessor.java
@EventHandler() public void onEvent(PostEvalEvent event) { try {/*from w ww. java 2 s . c o m*/ if (event.context().contentType() == null || !event.context().contentType().toLowerCase().startsWith("image")) return; float x = -1; float y = -1; boolean cacheEnabled = AppConfig.get().getBoolean("advanced.processors.imageProcessorCache", true); boolean grayscale = false; ScriptingContext context = event.context(); HttpRequestWrapper request = context.request(); Map<String, String> rewrite = request.getRewriteMap(); if (rewrite != null) { if (rewrite.get("serverSideOptions") != null) { String[] params = rewrite.get("serverSideOptions").trim().split("_"); for (String p : params) if (p.toLowerCase().startsWith("width") && x < 0) x = Integer.parseInt(p.substring(5)); else if ((p.toLowerCase().startsWith("x") || p.toLowerCase().startsWith("w")) && p.length() > 1 && x < 0) x = Integer.parseInt(p.substring(1)); else if (p.toLowerCase().startsWith("height") && y < 0) y = Integer.parseInt(p.substring(6)); else if ((p.toLowerCase().startsWith("y") || p.toLowerCase().startsWith("h")) && p.length() > 1 && y < 0) y = Integer.parseInt(p.substring(1)); else if (p.toLowerCase().equals("thumb")) { x = 150; y = 0; break; } else if (p.toLowerCase().equals("bw") || p.toLowerCase().equals("grayscale")) grayscale = true; } if (request.getArgument("width") != null && request.getArgument("width").length() > 0) x = request.getArgumentInt("width"); if (request.getArgument("height") != null && request.getArgument("height").length() > 0) y = request.getArgumentInt("height"); if (request.getArgument("w") != null && request.getArgument("w").length() > 0) x = request.getArgumentInt("w"); if (request.getArgument("h") != null && request.getArgument("h").length() > 0) y = request.getArgumentInt("h"); if (request.getArgument("thumb") != null) { x = 150; y = 0; } if (request.hasArgument("bw") || request.hasArgument("grayscale")) grayscale = true; } // Tests if our Post Processor can process the current image. List<String> readerFormats = Arrays.asList(ImageIO.getReaderFormatNames()); List<String> writerFormats = Arrays.asList(ImageIO.getWriterFormatNames()); if (context.contentType() != null && !readerFormats.contains(context.contentType().split("/")[1].toLowerCase())) return; int inx = event.context().buffer().readerIndex(); BufferedImage img = ImageIO.read(new ByteBufInputStream(event.context().buffer())); event.context().buffer().readerIndex(inx); if (img == null) return; float w = img.getWidth(); float h = img.getHeight(); float w1 = w; float h1 = h; if (x < 1 && y < 1) { x = w; y = h; } else if (x > 0 && y < 1) { w1 = x; h1 = x * (h / w); } else if (y > 0 && x < 1) { w1 = y * (w / h); h1 = y; } else if (x > 0 && y > 0) { w1 = x; h1 = y; } boolean resize = w1 > 0 && h1 > 0 && w1 != w && h1 != h; boolean argb = request.hasArgument("argb") && request.getArgument("argb").length() == 8; if (!resize && !argb && !grayscale) return; // Produce a unique encapsulated id based on this image processing request String encapId = SecureFunc.md5(context.filename() + w1 + h1 + request.getArgument("argb") + grayscale); File tmp = context.site() == null ? AppConfig.get().getDirectoryCache() : context.site().directoryTemp(); File file = new File(tmp, encapId + "_" + new File(context.filename()).getName()); if (cacheEnabled && file.exists()) { event.context().resetAndWrite(FileUtils.readFileToByteArray(file)); return; } Image image = resize ? img.getScaledInstance(Math.round(w1), Math.round(h1), AppConfig.get().getBoolean("advanced.processors.useFastGraphics", true) ? Image.SCALE_FAST : Image.SCALE_SMOOTH) : img; // TODO Report malformed parameters to user if (argb) { FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), new RGBColorFilter((int) Long.parseLong(request.getArgument("argb"), 16))); image = Toolkit.getDefaultToolkit().createImage(filteredSrc); } BufferedImage rtn = new BufferedImage(Math.round(w1), Math.round(h1), img.getType()); Graphics2D graphics = rtn.createGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); if (grayscale) { ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); op.filter(rtn, rtn); } if (resize) Log.get().info(EnumColor.GRAY + "Resized image from " + Math.round(w) + "px by " + Math.round(h) + "px to " + Math.round(w1) + "px by " + Math.round(h1) + "px"); if (rtn != null) { ByteArrayOutputStream bs = new ByteArrayOutputStream(); if (context.contentType() != null && writerFormats.contains(context.contentType().split("/")[1].toLowerCase())) ImageIO.write(rtn, context.contentType().split("/")[1].toLowerCase(), bs); else ImageIO.write(rtn, "png", bs); if (cacheEnabled && !file.exists()) FileUtils.writeByteArrayToFile(file, bs.toByteArray()); event.context().resetAndWrite(bs.toByteArray()); } } catch (Throwable e) { e.printStackTrace(); } return; }
From source file:com.piaoyou.util.ImageUtil.java
/** * This method returns a fit-sized image for a source image, * this method retains the ratio of the source image *//* ww w. jav a2 s. c om*/ public static Image getFitSizeImage(Image srcImage, int fitWidth, int fitHeight) throws IOException { if ((fitWidth < 100) || (fitHeight < 100)) throw new IllegalArgumentException("Cannot accept values < 100"); int srcWidth = srcImage.getWidth(null);//xem lai cho nay vi neu dung BufferedImage thi khong can null int srcHeight = srcImage.getHeight(null); //log.trace("src w = " + srcWidth + " h = " + srcHeight); // don't need any transforms if ((srcWidth == fitWidth) && (srcHeight == fitHeight)) return srcImage; int newWidth = srcWidth; int newHeight = srcHeight; double fitRatio = (double) fitWidth / fitHeight; double srcRatio = (double) srcWidth / srcHeight; if (srcRatio > fitRatio) {// must cut the width of the source image newWidth = (int) (srcHeight * fitRatio); } else {// must cut the height of the source image newHeight = (int) (srcWidth / fitRatio); } //log.trace("new w = " + newWidth + " h = " + newHeight); ImageFilter cropFilter = new CropImageFilter((srcWidth - newWidth) / 2, (srcHeight - newHeight) / 2, newWidth, newHeight); ImageProducer cropProd = new FilteredImageSource(srcImage.getSource(), cropFilter); Image cropImage = Toolkit.getDefaultToolkit().createImage(cropProd); Image retImage = new ImageIcon(getScaledInstance(cropImage, fitWidth, fitHeight)).getImage(); return retImage; }
From source file:com.krawler.esp.handlers.genericFileUpload.java
private BufferedImage scaleImage(Image sourceImage, int width, int height) { ImageFilter filter = new ReplicateScaleFilter(width, height); ImageProducer producer = new FilteredImageSource(sourceImage.getSource(), filter); Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer); return this.toBufferedImage(resizedImage); }
From source file:com.krawler.esp.handlers.genericFileUpload.java
private BufferedImage scaleCompanyImage(Image sourceImage, int width, int height) { ImageFilter filter = new ReplicateScaleFilter(width, height); ImageProducer producer = new FilteredImageSource(sourceImage.getSource(), filter); Image resizedImage = Toolkit.getDefaultToolkit().createImage(producer); return this.toBufferedCompanyImage(resizedImage); }
From source file:com.sketchy.image.ImageProcessingThread.java
public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight, CenterOption centerOption, ScaleOption scaleOption) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); double widthScale = drawingWidth / (double) imageWidth; double heightScale = drawingHeight / (double) imageHeight; double scale = Math.min(widthScale, heightScale); int scaleWidth = (int) (imageWidth * scale); int scaleHeight = (int) (imageHeight * scale); BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); if (scaleOption == ScaleOption.SCALE_AREA_AVERAGING) { ImageProducer prod = new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(scaleWidth, scaleHeight)); Image img = Toolkit.getDefaultToolkit().createImage(prod); g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled } else {/* w w w .j a v a 2 s.c o m*/ if (scaleOption == ScaleOption.SCALE_NEAREST_NEIGHBOR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else if (scaleOption == ScaleOption.SCALE_BICUBIC) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else if (scaleOption == ScaleOption.SCALE_BILINEAR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null); } g.dispose(); int cropWidth = (int) Math.min(scaleWidth, drawingWidth); int cropHeight = (int) Math.min(scaleHeight, drawingHeight); int drawingLeft = 0; int drawingTop = 0; if ((centerOption == CenterOption.CENTER_HORIZONTAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingLeft = (int) ((drawingWidth - cropWidth) / 2.0); } if ((centerOption == CenterOption.CENTER_VERTICAL) || (centerOption == CenterOption.CENTER_BOTH)) { drawingTop = (int) ((drawingHeight - cropHeight) / 2.0); } BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight); resizedImage = null; BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB); g = drawingImage.createGraphics(); g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft + cropWidth, drawingTop + cropHeight, 0, 0, cropWidth, cropHeight, null); g.dispose(); croppedImage = null; return drawingImage; }
From source file:com.piaoyou.util.ImageUtil.java
public static Image getScaledInstance(Image srcImage, int width, int height) { boolean useSun = false; ImageFilter filter;// ww w . j a v a 2 s .co m if (useSun) { //log.trace("use sun scalefilter"); filter = new java.awt.image.AreaAveragingScaleFilter(width, height); } else { //log.trace("use nguoimau scalefilter"); filter = new AreaAveragingScaleFilter(width, height); // filter = new java.awt.image.AreaAveragingScaleFilter(width, height); } ImageProducer prod = new FilteredImageSource(srcImage.getSource(), filter); Image newImage = Toolkit.getDefaultToolkit().createImage(prod); ImageIcon imageIcon = new ImageIcon(newImage); return imageIcon.getImage(); }
From source file:GifEncoder.java
/** Constructor from Image with interlace setting. * @param img The image to encode.// w w w. ja va 2 s . c o m * @param out The stream to write the GIF to. * @param interlace Whether to interlace. * @param transparentColor The color to use for transparency */ public GifEncoder(Image img, OutputStream out, boolean interlace, Color transparentColor) throws IOException { RGBImageFilter f = new TransparentFilter(transparentColor); this.producer = new FilteredImageSource(img.getSource(), f); this.out = out; this.interlace = interlace; }
From source file:com.jcraft.weirdx.DDXWindowImp.java
public Image getImage(GC gc, int x, int y, int w, int h) { Image i = getImage();//from w ww .j a v a2 s. c om if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) { TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask())); i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf)); } return i; }
From source file:com.jcraft.weirdx.XPixmap.java
void reset() { if (fgImg != null) { fgImg.flush();/*ww w . j a v a2 s . c o m*/ } fbFilter = new FBFilter(); fgImg = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(), fbFilter)); }
From source file:com.jcraft.weirdx.XPixmap.java
Image getImage(GC gc, int x, int y, int w, int h) { if (data != null && time < colormap.icmtime) { mkMIS();/* w w w. j a v a2s . c o m*/ Image dataImg = Toolkit.getDefaultToolkit().createImage(mis); time = System.currentTimeMillis(); imgg.drawImage(dataImg, 0, 0, Screen.screen[0].root.ddxwindow); //?? dataImg.flush(); } Image i = getImage(); if (gc != null && gc.clip_mask != null && gc.clip_mask instanceof ClipPixmap) { TransparentFilter tf = new TransparentFilter(0, 0, (XPixmap) (gc.clip_mask.getMask())); i = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(i.getSource(), tf)); } return i; }