public ImagePanel(Kdu_coords viewSize) throws KduException { int width = Math.min(viewSize.Get_x(), 1600); int height = Math.min(viewSize.Get_y(), 1200); setPreferredSize(new Dimension(width, height)); getInputMap().put(KeyStroke.getKeyStroke("UP"), "pressedUpArrow"); getActionMap() .put( "pressedUpArrow", new AbstractAction() { public void actionPerformed(ActionEvent evt) { // placeholder } }); getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "pressedDownArrow"); getActionMap() .put( "pressedDownArrow", new AbstractAction() { public void actionPerformed(ActionEvent evt) { // placeholder } }); }
/** * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>, * <code>nohref</code> Attribute for the specified figure and ellipse. * * @return Returns true, if the circle is inside of the image bounds. */ private boolean writeCircleAttributes(IXMLElement elem, SVGFigure f, Ellipse2D.Double ellipse) { AffineTransform t = TRANSFORM.getClone(f); if (t == null) { t = drawingTransform; } else { t.preConcatenate(drawingTransform); } if ((t.getType() & (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION)) == t.getType() && ellipse.width == ellipse.height ) { Point2D.Double start = new Point2D.Double(ellipse.x, ellipse.y); Point2D.Double end = new Point2D.Double(ellipse.x + ellipse.width, ellipse.y + ellipse.height); t.transform(start, start); t.transform(end, end); ellipse.x = Math.min(start.x, end.x); ellipse.y = Math.min(start.y, end.y); ellipse.width = Math.abs(start.x - end.x); ellipse.height = Math.abs(start.y - end.y); elem.setAttribute("shape", "circle"); elem.setAttribute("coords", (int) (ellipse.x + ellipse.width / 2d)+","+ (int) (ellipse.y + ellipse.height / 2d)+","+ (int) (ellipse.width / 2d) ); writeHrefAttribute(elem, f); return bounds.intersects(ellipse.getBounds()); } else { return writePolyAttributes(elem, f, (Shape) ellipse); } }
public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); g2.translate(getWidth() / 2 - size / 2, getHeight() / 2 - size / 2); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Shape shape; if (mode == ColorPicker.SAT || mode == ColorPicker.BRI) { shape = new Ellipse2D.Float(0, 0, size, size); } else { Rectangle r = new Rectangle(0, 0, size, size); shape = r; } if (hasFocus()) { PaintUtils.paintFocus(g2, shape, 5); } if (!(shape instanceof Rectangle)) { // paint a circular shadow g2.translate(2, 2); g2.setColor(new Color(0, 0, 0, 20)); g2.fill(new Ellipse2D.Float(-2, -2, size + 4, size + 4)); g2.setColor(new Color(0, 0, 0, 40)); g2.fill(new Ellipse2D.Float(-1, -1, size + 2, size + 2)); g2.setColor(new Color(0, 0, 0, 80)); g2.fill(new Ellipse2D.Float(0, 0, size, size)); g2.translate(-2, -2); } g2.drawImage(image, 0, 0, size, size, 0, 0, size, size, null); if (shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; PaintUtils.drawBevel(g2, r); } else { g2.setColor(new Color(0, 0, 0, 120)); g2.draw(shape); } g2.setColor(Color.white); g2.setStroke(new BasicStroke(1)); g2.draw(new Ellipse2D.Float(point.x - 3, point.y - 3, 6, 6)); g2.setColor(Color.black); g2.draw(new Ellipse2D.Float(point.x - 4, point.y - 4, 8, 8)); g.translate(-imagePadding.left, -imagePadding.top); }
public void mousePressed(MouseEvent e) { requestFocus(); Point p = e.getPoint(); int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); p.translate(-(getWidth() / 2 - size / 2), -(getHeight() / 2 - size / 2)); if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) { // the two circular views: double radius = ((double) size) / 2.0; double x = p.getX() - size / 2.0; double y = p.getY() - size / 2.0; double r = Math.sqrt(x * x + y * y) / radius; double theta = Math.atan2(y, x) / (Math.PI * 2.0); if (r > 1) r = 1; if (mode == ColorPicker.BRI) { setHSB((float) (theta + .25f), (float) (r), bri); } else { setHSB((float) (theta + .25f), sat, (float) (r)); } } else if (mode == ColorPicker.HUE) { float s = ((float) p.x) / ((float) size); float b = ((float) p.y) / ((float) size); if (s < 0) s = 0; if (s > 1) s = 1; if (b < 0) b = 0; if (b > 1) b = 1; setHSB(hue, s, b); } else { int x2 = p.x * 255 / size; int y2 = p.y * 255 / size; if (x2 < 0) x2 = 0; if (x2 > 255) x2 = 255; if (y2 < 0) y2 = 0; if (y2 > 255) y2 = 255; if (mode == ColorPicker.RED) { setRGB(red, x2, y2); } else if (mode == ColorPicker.GREEN) { setRGB(x2, green, y2); } else { setRGB(x2, y2, blue); } } }
public static WritableRaster glowmask(Raster img) { Coord sz = imgsz(img); int nb = img.getNumBands(); WritableRaster ret = alpharaster(sz); float[] hsv = new float[3]; float max = 0; for (int y = 0; y < sz.y; y++) { for (int x = 0; x < sz.x; x++) { Color.RGBtoHSB(img.getSample(x, y, 0), img.getSample(x, y, 1), img.getSample(x, y, 2), hsv); float a = (nb > 3) ? (img.getSample(x, y, 3) / 255f) : 1f; float val = ((1f - hsv[1]) * hsv[2]) * a; max = Math.max(max, val); } } float imax = 1f / max; for (int y = 0; y < sz.y; y++) { for (int x = 0; x < sz.x; x++) { Color.RGBtoHSB(img.getSample(x, y, 0), img.getSample(x, y, 1), img.getSample(x, y, 2), hsv); float a = (nb > 3) ? (img.getSample(x, y, 3) / 255f) : 1f; float val = ((1f - hsv[1]) * hsv[2]) * a; ret.setSample(x, y, 0, Math.min(Math.max((int) (Math.sqrt(val * imax) * 255), 0), 255)); } } return (ret); }
@Override public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } int i = pf.getOrientation(); // get the size of the page double pageWidth = pf.getImageableWidth(); double pageHeight = pf.getImageableHeight(); double myWidth = this.getWidth(); // - borderWidth * 2; double myHeight = this.getHeight(); // - borderWidth * 2; double scaleX = pageWidth / myWidth; double scaleY = pageHeight / myHeight; double minScale = Math.min(scaleX, scaleY); Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.scale(minScale, minScale); drawPlot(g); return PAGE_EXISTS; }
public BufferedImage filter(BufferedImage src, BufferedImage dst) { icentreX = src.getWidth() * centreX; icentreY = src.getHeight() * centreY; if (radius == 0) radius = Math.min(icentreX, icentreY); radius2 = radius * radius; return super.filter(src, dst); }
protected void drawPointLegend( VPFSymbolAttributes attr, Graphics2D g2, int width, int height, int margin) { if (attr.getIconImageSource() == null) return; BufferedImage icon = getImage(attr.getIconImageSource()); if (icon != null) { // icon width / height int iw = icon.getWidth(); int ih = icon.getHeight(); // draw area width / height int dw = width - margin * 2; int dh = height - margin * 2; // draw scale to fit icon inside draw area float sx = iw > dw ? (float) dw / iw : 1f; // shrink only float sy = ih > dh ? (float) dh / ih : 1f; float scale = Math.min(sx, sy); iw = (int) ((float) iw * scale); ih = (int) ((float) ih * scale); // Center image and draw int x1 = iw < dw ? margin + (dw - iw) / 2 : margin; int y1 = ih < dh ? margin + (dh - ih) / 2 : margin; g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(icon, x1, y1, iw, ih, null); } }
public Point componentToImage(Point p) { float r = Math.min(getWidth() / (float) width, getHeight() / (float) height); int x = (int) (p.getX() / r); int y = (int) (p.getY() / r); if ((x > width - 1) || (y > height - 1)) return new Point(-1, -1); return new Point(x, y); }
protected void paintImage( Component c, Graphics g, int x, int y, int imageW, int imageH, Image image, Object[] args) { boolean isVertical = ((Boolean) args[1]).booleanValue(); // Render to the screen g.translate(x, y); if (isVertical) { for (int counter = 0; counter < w; counter += IMAGE_SIZE) { int tileSize = Math.min(IMAGE_SIZE, w - counter); g.drawImage(image, counter, 0, counter + tileSize, h, 0, 0, tileSize, h, null); } } else { for (int counter = 0; counter < h; counter += IMAGE_SIZE) { int tileSize = Math.min(IMAGE_SIZE, h - counter); g.drawImage(image, 0, counter, w, counter + tileSize, 0, 0, w, tileSize, null); } } g.translate(-x, -y); }
static int getSelectorAsInt(final String fromString) { final byte[] b = fromString.getBytes(); final int len = Math.min(b.length, 4); int result = 0; for (int i = 0; i < len; i++) { if (i > 0) result <<= 8; result |= (b[i] & 0xff); } return result; }
/** * Gets the dimension of the lattice in pixel units. * * @param panel * @return the dimension */ public Dimension getInterior(DrawingPanel panel) { float availableWidth = panel.getWidth() - panel.getLeftGutter() - panel.getRightGutter() - 1; float availableHeight = panel.getHeight() - panel.getTopGutter() - panel.getBottomGutter() - 1; scaleFactor = Math.min(availableWidth / dimension.width, availableHeight / dimension.height); if (scaleFactor > 1) { scaleFactor = 1; return dimension; } return new Dimension((int) (scaleFactor * ncol), (int) (scaleFactor * nrow)); }
public static BufferedImage monochromize(BufferedImage img, Color col) { Coord sz = Utils.imgsz(img); BufferedImage ret = TexI.mkbuf(sz); Raster src = img.getRaster(); WritableRaster dst = ret.getRaster(); boolean hasalpha = (src.getNumBands() == 4); for (int y = 0; y < sz.y; y++) { for (int x = 0; x < sz.x; x++) { int r = src.getSample(x, y, 0), g = src.getSample(x, y, 1), b = src.getSample(x, y, 2); int a = hasalpha ? src.getSample(x, y, 3) : 255; int max = Math.max(r, Math.max(g, b)), min = Math.min(r, Math.min(g, b)); int val = (max + min) / 2; dst.setSample(x, y, 0, (col.getRed() * val) / 255); dst.setSample(x, y, 1, (col.getGreen() * val) / 255); dst.setSample(x, y, 2, (col.getBlue() * val) / 255); dst.setSample(x, y, 3, (col.getAlpha() * a) / 255); } } return (ret); }
public static WritableRaster imggrow(WritableRaster img, int rad) { int h = img.getHeight(), w = img.getWidth(); int[] buf = new int[w * h]; int o = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { int m = 0; int u = Math.max(0, y - rad), b = Math.min(h - 1, y + rad); int l = Math.max(0, x - rad), r = Math.min(w - 1, x + rad); for (int y2 = u; y2 <= b; y2++) { for (int x2 = l; x2 <= r; x2++) { m = Math.max(m, img.getSample(x2, y2, 0)); } } buf[o++] = m; } } img.setSamples(0, 0, w, h, 0, buf); return (img); }
private static Color getColorFromPallet(int[] pallet, float x) { if (x < 0f || x > 1f) { throw new IllegalArgumentException("Parameter outside of expected range"); } int i = (int) (pallet.length * x); int max = pallet.length - 1; int index = Math.min(Math.max(i, 0), max); return new Color(pallet[index] & 0x00FFFFFF); // translucent // int pix = pallet[index] & 0x00FFFFFF | (0x64 << 24); // return new Color(pix), true); }
public void keyPressed(KeyEvent e) { int dx = 0; int dy = 0; if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dx = 1; } else if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { dy = 1; } int multiplier = 1; if (e.isShiftDown() && e.isAltDown()) { multiplier = 10; } else if (e.isShiftDown() || e.isAltDown()) { multiplier = 5; } if (dx != 0 || dy != 0) { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); int offsetX = getWidth() / 2 - size / 2; int offsetY = getHeight() / 2 - size / 2; mouseListener.mousePressed( new MouseEvent( ColorPickerPanel.this, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), 0, point.x + multiplier * dx + offsetX, point.y + multiplier * dy + offsetY, 1, false)); } }
public static WritableRaster gayblit( WritableRaster dst, int dband, Coord doff, Raster src, int sband, Coord soff) { if (doff.x < 0) { soff = soff.add(-doff.x, 0); doff = doff.add(-doff.x, 0); } if (doff.y < 0) { soff = soff.add(0, -doff.x); doff = doff.add(0, -doff.x); } int w = Math.min(src.getWidth() - soff.x, dst.getWidth() - doff.x), h = Math.min(src.getHeight() - soff.y, dst.getHeight() - doff.y); for (int y = 0; y < h; y++) { int sy = y + soff.y, dy = y + doff.y; for (int x = 0; x < w; x++) { int sx = x + soff.x, dx = x + doff.x; dst.setSample( dx, dy, dband, (dst.getSample(dx, dy, dband) * src.getSample(sx, sy, sband)) / 255); } } return (dst); }
/** * All other write methods delegate their work to here. */ public void write(OutputStream out, java.util.List<Figure> figures) throws IOException { Rectangle2D.Double drawingRect = null; for (Figure f : figures) { if (drawingRect == null) { drawingRect = f.getBounds(); } else { drawingRect.add(f.getBounds()); } } AffineTransform drawingTransform = new AffineTransform(); drawingTransform.translate( -Math.min(0, drawingRect.x), -Math.min(0, drawingRect.y) ); write(out, figures, drawingTransform, new Dimension( (int) (Math.abs(drawingRect.x) + drawingRect.width), (int) (Math.abs(drawingRect.y) + drawingRect.height)) ); }
public static WritableRaster imgblur(WritableRaster img, int rad, double var) { int h = img.getHeight(), w = img.getWidth(); double[] gk = new double[(rad * 2) + 1]; for (int i = 0; i <= rad; i++) gk[rad + i] = gk[rad - i] = Math.exp(-0.5 * Math.pow(i / var, 2.0)); double s = 0; for (double cw : gk) s += cw; s = 1.0 / s; for (int i = 0; i <= rad * 2; i++) gk[i] *= s; int[] buf = new int[w * h]; for (int band = 0; band < img.getNumBands(); band++) { int o; o = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double v = 0; int l = Math.max(0, x - rad), r = Math.min(w - 1, x + rad); for (int x2 = l, ks = l - (x - rad); x2 <= r; x2++, ks++) v += img.getSample(x2, y, band) * gk[ks]; buf[o++] = (int) v; } } img.setSamples(0, 0, w, h, band, buf); o = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double v = 0; int u = Math.max(0, y - rad), b = Math.min(h - 1, y + rad); for (int y2 = u, ks = u - (y - rad); y2 <= b; y2++, ks++) v += img.getSample(x, y2, band) * gk[ks]; buf[o++] = (int) v; } } img.setSamples(0, 0, w, h, band, buf); } return (img); }
/** * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>, * <code>nohref</code> Attribute for the specified figure and rectangle. * * @return Returns true, if the rect is inside of the image bounds. */ private boolean writeRectAttributes(IXMLElement elem, SVGFigure f, Rectangle2D.Double rect) { AffineTransform t = TRANSFORM.getClone(f); if (t == null) { t = drawingTransform; } else { t.preConcatenate(drawingTransform); } if ((t.getType() & (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION)) == t.getType() ) { Point2D.Double start = new Point2D.Double(rect.x, rect.y); Point2D.Double end = new Point2D.Double(rect.x + rect.width, rect.y + rect.height); t.transform(start, start); t.transform(end, end); Rectangle r = new Rectangle( (int) Math.min(start.x, end.x), (int) Math.min(start.y, end.y), (int) Math.abs(start.x - end.x), (int) Math.abs(start.y - end.y) ); elem.setAttribute("shape", "rect"); elem.setAttribute("coords", r.x + ","+ r.y + ","+ (r.x + r.width) + ","+ (r.y + r.height) ); writeHrefAttribute(elem, f); return bounds.intersects(r); } else { return writePolyAttributes(elem, f, (Shape) rect); } }
public int filterRGB(int x, int y, int rgb) { // Coefficients are from the sRGB color space: int gray = Math.min( 255, (int) (((0.2125f * ((rgb >> 16) & 0xFF)) + (0.7154f * ((rgb >> 8) & 0xFF)) + (0.0721f * (rgb & 0xFF)) + .5f) * factor + min)); return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | (gray << 0); }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { if (image == null) { image = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB); double coef = Math.min((double) width / (double) 68, (double) height / (double) 81); Graphics2D g2d = image.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.scale(coef, coef); paint(g2d); g2d.dispose(); } g.drawImage(image, x, y, null); }
/** Recalculates the (x,y) point used to indicate the selected color. */ private void regeneratePoint() { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); if (mode == ColorPicker.HUE || mode == ColorPicker.SAT || mode == ColorPicker.BRI) { if (mode == ColorPicker.HUE) { point = new Point((int) (sat * size), (int) (bri * size)); } else if (mode == ColorPicker.SAT) { double theta = hue * 2 * Math.PI - Math.PI / 2; if (theta < 0) theta += 2 * Math.PI; double r = bri * size / 2; point = new Point( (int) (r * Math.cos(theta) + .5 + size / 2.0), (int) (r * Math.sin(theta) + .5 + size / 2.0)); } else if (mode == ColorPicker.BRI) { double theta = hue * 2 * Math.PI - Math.PI / 2; if (theta < 0) theta += 2 * Math.PI; double r = sat * size / 2; point = new Point( (int) (r * Math.cos(theta) + .5 + size / 2.0), (int) (r * Math.sin(theta) + .5 + size / 2.0)); } } else if (mode == ColorPicker.RED) { point = new Point((int) (green * size / 255f + .49f), (int) (blue * size / 255f + .49f)); } else if (mode == ColorPicker.GREEN) { point = new Point((int) (red * size / 255f + .49f), (int) (blue * size / 255f + .49f)); } else if (mode == ColorPicker.BLUE) { point = new Point((int) (red * size / 255f + .49f), (int) (green * size / 255f + .49f)); } }
private Path2D.Double makeStar(int r1, int r2, int vc) { int or = Math.max(r1, r2); int ir = Math.min(r1, r2); double agl = 0d; double add = 2 * Math.PI / (vc * 2); Path2D.Double p = new Path2D.Double(); p.moveTo(or * 1, or * 0); for (int i = 0; i < vc * 2 - 1; i++) { agl += add; int r = i % 2 == 0 ? ir : or; p.lineTo(r * Math.cos(agl), r * Math.sin(agl)); } p.closePath(); AffineTransform at = AffineTransform.getRotateInstance(-Math.PI / 2, or, 0); return new Path2D.Double(p, at); }
public void fitToWindow() { ImageWindow win = imp.getWindow(); if (win == null) return; Rectangle bounds = win.getBounds(); Insets insets = win.getInsets(); int sliderHeight = (win instanceof StackWindow) ? 20 : 0; double xmag = (double) (bounds.width - 10) / srcRect.width; double ymag = (double) (bounds.height - (10 + insets.top + sliderHeight)) / srcRect.height; setMagnification(Math.min(xmag, ymag)); int width = (int) (imageWidth * magnification); int height = (int) (imageHeight * magnification); if (width == dstWidth && height == dstHeight) return; srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize(width, height); getParent().doLayout(); }
public synchronized void paint(Graphics gin) { Graphics2D g = (Graphics2D) gin; if (im == null) return; int height = getHeight(); int width = getWidth(); if (fit) { t = new AffineTransform(); double scale = Math.min(((double) width) / im.getWidth(), ((double) height) / im.getHeight()); // we'll re-center the transform in a moment. t.scale(scale, scale); } // if the image (in either X or Y) is smaller than the view port, then center // the image with respect to that axis. double mwidth = im.getWidth() * t.getScaleX(); double mheight = im.getHeight() * t.getScaleY(); if (mwidth < width) t.preConcatenate( AffineTransform.getTranslateInstance((width - mwidth) / 2.0 - t.getTranslateX(), 0)); if (mheight < height) t.preConcatenate( AffineTransform.getTranslateInstance(0, (height - mheight) / 2.0 - t.getTranslateY())); // if we're allowing panning (because only a portion of the image is visible), // don't allow translations that show less information that is possible. Point2D topleft = t.transform(new Point2D.Double(0, 0), null); Point2D bottomright = t.transform(new Point2D.Double(im.getWidth(), im.getHeight()), null); if (mwidth > width) { if (topleft.getX() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(-topleft.getX(), 0)); if (bottomright.getX() < width) t.preConcatenate(AffineTransform.getTranslateInstance(width - bottomright.getX(), 0)); // t.translate(width-bottomright.getX(), 0); } if (mheight > height) { if (topleft.getY() > 0) t.preConcatenate(AffineTransform.getTranslateInstance(0, -topleft.getY())); if (bottomright.getY() < height) t.preConcatenate(AffineTransform.getTranslateInstance(0, height - bottomright.getY())); } g.drawImage(im, t, null); }
/* * (non-Javadoc) * * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, * int, int) */ @Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.translate(x, y); Area clip = new Area(new Rectangle(0, 0, this.width, this.height)); if (g2d.getClip() != null) clip.intersect(new Area(g2d.getClip())); g2d.setClip(clip); double coef1 = (double) this.width / (double) getOrigWidth(); double coef2 = (double) this.height / (double) getOrigHeight(); double coef = Math.min(coef1, coef2); g2d.scale(coef, coef); paint(g2d); g2d.dispose(); }
@Override public void draw(Graphics2D g) { double opacity = get(OPACITY); opacity = Math.min(Math.max(0d, opacity), 1d); if (opacity != 0d) { if (opacity != 1d) { Rectangle2D.Double drawingArea = getDrawingArea(); Rectangle2D clipBounds = g.getClipBounds(); if (clipBounds != null) { Rectangle2D.intersect(drawingArea, clipBounds, drawingArea); } if (!drawingArea.isEmpty()) { BufferedImage buf = new BufferedImage( (int) ((2 + drawingArea.width) * g.getTransform().getScaleX()), (int) ((2 + drawingArea.height) * g.getTransform().getScaleY()), BufferedImage.TYPE_INT_ARGB); Graphics2D gr = buf.createGraphics(); gr.scale(g.getTransform().getScaleX(), g.getTransform().getScaleY()); gr.translate((int) -drawingArea.x, (int) -drawingArea.y); gr.setRenderingHints(g.getRenderingHints()); drawFigure(gr); gr.dispose(); Composite savedComposite = g.getComposite(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity)); g.drawImage( buf, (int) drawingArea.x, (int) drawingArea.y, 2 + (int) drawingArea.width, 2 + (int) drawingArea.height, null); g.setComposite(savedComposite); } } else { drawFigure(g); } } }
private BufferedImage cropScaleGrayscale(Rectangle visibleRect, RenderedImage image) { int minX = image.getMinX(); int minY = image.getMinY(); int width = image.getWidth(); int height = image.getHeight(); Rectangle bounds = new Rectangle(minX, minY, width, height); visibleRect = bounds.intersection(visibleRect); if (bounds.contains(visibleRect)) { ParameterBlock pb = new ParameterBlock(); pb.addSource(image); pb.add((float) visibleRect.x); pb.add((float) visibleRect.y); pb.add((float) visibleRect.width); pb.add((float) visibleRect.height); image = JAI.create("Crop", pb, JAIContext.noCacheHint); } Dimension previewSize = getSize(); if ((visibleRect.width > previewSize.width) || (visibleRect.height > previewSize.height)) { float scale = Math.min( previewSize.width / (float) visibleRect.width, previewSize.height / (float) visibleRect.height); image = ConvolveDescriptor.create(image, Functions.getGaussKernel(.25 / scale), null); ParameterBlock pb = new ParameterBlock(); pb.addSource(image); pb.add(scale); pb.add(scale); image = JAI.create("Scale", pb, JAIContext.noCacheHint); } image = Functions.toColorSpace(image, JAIContext.systemColorSpace, null); if (image.getSampleModel().getDataType() == DataBuffer.TYPE_USHORT) { image = Functions.fromUShortToByte(image, null); } return Functions.toFastBufferedImage(image); }
/** * Paint the image onto a Graphics object. The painting is performed tile-by-tile, and includes a * grey region covering the unused portion of image tiles as well as the general background. At * this point the image must be byte data. */ public synchronized void paintComponent(Graphics g) { Graphics2D g2D = null; if (g instanceof Graphics2D) { g2D = (Graphics2D) g; } else { return; } // if source is null, it's just a component if (source == null) { g2D.setColor(getBackground()); g2D.fillRect(0, 0, componentWidth, componentHeight); return; } int transX = -originX; int transY = -originY; // Get the clipping rectangle and translate it into image coordinates. Rectangle clipBounds = g.getClipBounds(); if (clipBounds == null) { clipBounds = new Rectangle(0, 0, componentWidth, componentHeight); } // clear the background (clip it) [minimal optimization here] if (transX > 0 || transY > 0 || transX < (componentWidth - source.getWidth()) || transY < (componentHeight - source.getHeight())) { g2D.setColor(getBackground()); g2D.fillRect(0, 0, componentWidth, componentHeight); } clipBounds.translate(-transX, -transY); // Determine the extent of the clipping region in tile coordinates. int txmin, txmax, tymin, tymax; int ti, tj; txmin = XtoTileX(clipBounds.x); txmin = Math.max(txmin, minTileX); txmin = Math.min(txmin, maxTileX); txmax = XtoTileX(clipBounds.x + clipBounds.width - 1); txmax = Math.max(txmax, minTileX); txmax = Math.min(txmax, maxTileX); tymin = YtoTileY(clipBounds.y); tymin = Math.max(tymin, minTileY); tymin = Math.min(tymin, maxTileY); tymax = YtoTileY(clipBounds.y + clipBounds.height - 1); tymax = Math.max(tymax, minTileY); tymax = Math.min(tymax, maxTileY); Insets insets = getInsets(); // Loop over tiles within the clipping region for (tj = tymin; tj <= tymax; tj++) { for (ti = txmin; ti <= txmax; ti++) { int tx = TileXtoX(ti); int ty = TileYtoY(tj); Raster tile = source.getTile(ti, tj); if (tile != null) { DataBuffer dataBuffer = tile.getDataBuffer(); WritableRaster wr = tile.createWritableRaster(sampleModel, dataBuffer, null); BufferedImage bi = new BufferedImage(colorModel, wr, colorModel.isAlphaPremultiplied(), null); // correctly handles band offsets if (brightnessEnabled == true) { SampleModel sm = sampleModel.createCompatibleSampleModel(tile.getWidth(), tile.getHeight()); WritableRaster raster = RasterFactory.createWritableRaster(sm, null); BufferedImage bimg = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); // don't move this code ByteLookupTable lutTable = new ByteLookupTable(0, lutData); LookupOp lookup = new LookupOp(lutTable, null); lookup.filter(bi, bimg); g2D.drawImage(bimg, biop, tx + transX + insets.left, ty + transY + insets.top); } else { AffineTransform transform; transform = AffineTransform.getTranslateInstance( tx + transX + insets.left, ty + transY + insets.top); g2D.drawRenderedImage(bi, transform); } } } } }