1 package org.nightlabs.editor2d.viewer.j2dswt; 2 3 import java.awt.AlphaComposite ; 4 import java.awt.BasicStroke ; 5 import java.awt.Color ; 6 import java.awt.Composite ; 7 import java.awt.Font ; 8 import java.awt.FontMetrics ; 9 import java.awt.GradientPaint ; 10 import java.awt.Graphics ; 11 import java.awt.Graphics2D ; 12 import java.awt.GraphicsConfiguration ; 13 import java.awt.Image ; 14 import java.awt.Paint ; 15 import java.awt.Rectangle ; 16 import java.awt.RenderingHints ; 17 import java.awt.Shape ; 18 import java.awt.Stroke ; 19 import java.awt.TexturePaint ; 20 import java.awt.Toolkit ; 21 import java.awt.RenderingHints.Key; 22 import java.awt.font.FontRenderContext ; 23 import java.awt.font.GlyphVector ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.PathIterator ; 26 import java.awt.geom.Point2D ; 27 import java.awt.image.BufferedImage ; 28 import java.awt.image.BufferedImageOp ; 29 import java.awt.image.ColorModel ; 30 import java.awt.image.DirectColorModel ; 31 import java.awt.image.ImageObserver ; 32 import java.awt.image.IndexColorModel ; 33 import java.awt.image.RenderedImage ; 34 import java.awt.image.WritableRaster ; 35 import java.awt.image.renderable.RenderableImage ; 36 import java.text.AttributedCharacterIterator ; 37 import java.util.Map ; 38 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.graphics.Device; 41 import org.eclipse.swt.graphics.FontData; 42 import org.eclipse.swt.graphics.GC; 43 import org.eclipse.swt.graphics.ImageData; 44 import org.eclipse.swt.graphics.PaletteData; 45 import org.eclipse.swt.graphics.Path; 46 import org.eclipse.swt.graphics.Pattern; 47 import org.eclipse.swt.graphics.RGB; 48 import org.eclipse.swt.graphics.Transform; 49 import org.eclipse.swt.widgets.Display; 50 51 65 public class SWTGraphics2D 66 extends Graphics2D 67 { 68 private final static AffineTransform IDENTITY = new AffineTransform (); 69 70 74 private RenderingHints hints = new RenderingHints (null); 75 76 81 private org.eclipse.swt.graphics.Color oldBg; 82 83 88 private org.eclipse.swt.graphics.Color oldFg; 89 90 96 private boolean needSwap = true; 97 98 private GC _gc; 99 100 private Device _dev; 101 102 public SWTGraphics2D(GC gc, Device dev) { 103 super(); 104 _gc = gc; 105 _dev = (dev == null) ? Display.getDefault() : dev; 106 switch (_gc.getAntialias()) { 107 case SWT.OFF : 108 hints.put(RenderingHints.KEY_ANTIALIASING, 109 RenderingHints.VALUE_ANTIALIAS_OFF); 110 break; 111 case SWT.ON : 112 hints.put(RenderingHints.KEY_ANTIALIASING, 113 RenderingHints.VALUE_ANTIALIAS_ON); 114 break; 115 case SWT.DEFAULT : 116 hints.put(RenderingHints.KEY_ANTIALIASING, 117 RenderingHints.VALUE_ANTIALIAS_DEFAULT); 118 break; 119 } 120 switch (_gc.getTextAntialias()) { 121 case SWT.OFF : 122 hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, 123 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 124 break; 125 case SWT.ON : 126 hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, 127 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 128 break; 129 case SWT.DEFAULT : 130 hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, 131 RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT); 132 break; 133 } 134 } 135 136 public SWTGraphics2D(GC gc) { 137 this(gc, null); 138 } 139 140 147 private void swapColors() { 148 if (needSwap) { 149 oldBg = _gc.getBackground(); 150 oldFg = _gc.getForeground(); 151 _gc.setBackground(oldFg); 152 _gc.setForeground(oldBg); 153 } 154 } 155 156 170 public Transform toSWTTransform(AffineTransform t) { 171 double[] m = new double[6]; 172 t.getMatrix(m); 173 return new Transform (_dev, (float) m[0], (float) m[1], (float) m[2], 174 (float) m[3], (float) m[4], (float) m[5]); 175 } 176 177 182 public AffineTransform toAWTTransform(Transform t) { 183 float[] m = new float[6]; 184 t.getElements(m); 185 return new AffineTransform (m); 186 } 187 188 199 public Color toAWTColor(org.eclipse.swt.graphics.Color rgb) { 200 return new Color (rgb.getRed(), rgb.getGreen(), rgb.getBlue(), _gc.getAlpha()); 201 } 202 203 219 public org.eclipse.swt.graphics.Color toSWTColor(Color rgba) { 220 return new org.eclipse.swt.graphics.Color(_dev, rgba.getRed(), rgba.getGreen(), 221 rgba.getBlue()); 222 } 223 224 245 public Path toPath(Shape s) { 246 float[] coords = new float[6]; 247 Path p = new Path(_dev); 248 PathIterator it = s.getPathIterator(IDENTITY); 249 while (!it.isDone()) { 250 int type = it.currentSegment(coords); 251 switch (type) { 252 case PathIterator.SEG_MOVETO : 253 p.moveTo(coords[0], coords[1]); 254 break; 255 case PathIterator.SEG_LINETO : 256 p.lineTo(coords[0], coords[1]); 257 break; 258 case PathIterator.SEG_QUADTO : 259 p.quadTo(coords[0], coords[1], coords[2], coords[3]); 260 break; 261 case PathIterator.SEG_CUBICTO : 262 p.cubicTo(coords[0], coords[1], coords[2], coords[3], coords[4], 263 coords[5]); 264 break; 265 case PathIterator.SEG_CLOSE : 268 p.close(); 269 break; 270 } 271 it.next(); 272 } 273 return p; 274 } 275 276 public void addRenderingHints(Map <?, ?> map) { 277 setRenderingHints(map); 278 } 279 280 public void clip(Shape s) { 281 Path p = toPath(s); 282 _gc.setClipping(p); 283 p.dispose(); 284 } 285 286 public void draw(Shape s) { 287 Path p = toPath(s); 288 _gc.drawPath(p); 289 p.dispose(); 290 } 291 292 public void drawGlyphVector(GlyphVector g, float x, float y) { 293 295 } 296 297 public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) { 298 BufferedImage bi = op.createCompatibleDestImage(img, null); 299 org.eclipse.swt.graphics.Image swtImage = toSWT(bi); 300 _gc.drawImage(swtImage, x, y); 301 } 302 303 public static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB; 304 public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) 305 { 306 if (img instanceof BufferedImage ) { 307 BufferedImage bi = (BufferedImage ) img; 308 Transform oldTransform = new Transform (_dev); 309 _gc.getTransform(oldTransform); 310 _gc.setTransform(toSWTTransform(xform)); 311 org.eclipse.swt.graphics.Image swtImage = toSWT(bi); 312 _gc.drawImage(swtImage, (int)xform.getTranslateX(), (int)xform.getTranslateY()); 313 return true; 314 } 315 return false; 316 } 317 318 public void drawRenderableImage(RenderableImage img, AffineTransform xform) { 319 321 } 322 323 public void drawRenderedImage(RenderedImage img, AffineTransform xform) { 324 326 } 327 328 public void drawString(AttributedCharacterIterator iterator, float x, float y) { 329 } 331 332 public void drawString(AttributedCharacterIterator iterator, int x, int y) { 333 } 335 336 public void drawString(String s, float x, float y) { 337 drawString(s, (int) x, (int) y); 338 } 339 340 public void drawString(String str, int x, int y) { 341 int fh = _gc.getFontMetrics().getAscent(); 342 _gc.drawString(str, x, y - fh, true); 343 } 344 345 public void fill(Shape s) { 346 Path p = toPath(s); 347 swapColors(); 348 _gc.fillPath(p); 349 swapColors(); 350 p.dispose(); 351 } 352 353 public Color getBackground() { 354 return toAWTColor(_gc.getBackground()); 355 } 356 357 360 public Composite getComposite() { 361 return AlphaComposite.Src; 362 } 363 364 public GraphicsConfiguration getDeviceConfiguration() { 365 return null; 367 } 368 369 public FontRenderContext getFontRenderContext() { 370 return null; 372 } 373 374 public Paint getPaint() { 375 return toAWTColor(_gc.getBackground()); 376 } 377 378 public Object getRenderingHint(Key hintKey) { 379 return hints.get(hintKey); 380 } 381 382 public RenderingHints getRenderingHints() { 383 return hints; 384 } 385 386 public Stroke getStroke() { 387 return null; 389 } 390 391 public AffineTransform getTransform() 392 { 393 Transform t = new Transform (_dev); 394 _gc.getTransform(t); 395 AffineTransform at = toAWTTransform(t); 396 t.dispose(); 397 return at; 398 } 399 400 public boolean hit(Rectangle rect, Shape s, boolean onStroke) { 401 return false; 403 } 404 405 public void rotate(double theta, double x, double y) { 406 408 } 409 410 public void rotate(double theta) { 411 Transform t = new Transform (_dev); 412 _gc.getTransform(t); 413 t.rotate((float) Math.toDegrees(theta)); 414 _gc.setTransform(t); 415 t.dispose(); 416 } 417 418 public void scale(double sx, double sy) { 419 Transform t = new Transform (_dev); 420 _gc.getTransform(t); 421 t.scale((float) sx, (float) sy); 422 _gc.setTransform(t); 423 t.dispose(); 424 } 425 426 public void setBackground(Color color) { 427 _gc.setBackground(toSWTColor(color)); 428 _gc.setAlpha(color.getAlpha()); 429 } 430 431 434 public void setComposite(Composite comp) { 435 } 436 437 442 public void setPaint(Paint paint) { 443 needSwap = true; 444 if (paint instanceof Color ) { 445 setColor((Color ) paint); 446 } else if (paint instanceof GradientPaint ) { 447 GradientPaint gp = (GradientPaint ) paint; 448 Point2D p1 = gp.getPoint1(); 449 Point2D p2 = gp.getPoint2(); 450 org.eclipse.swt.graphics.Color c1 = toSWTColor(gp.getColor1()); 451 org.eclipse.swt.graphics.Color c2 = toSWTColor((gp.getColor2())); 452 Pattern p = new Pattern(_dev, (float) p1.getX(), (float) p1.getY(), 453 (float) p2.getX(), (float) p2.getY(), c1, c2); 454 _gc.setBackgroundPattern(p); 455 c1.dispose(); 456 c2.dispose(); 457 needSwap = false; 458 } else if (paint instanceof TexturePaint ) { 459 TexturePaint tp = (TexturePaint ) paint; 460 BufferedImage awtImg = tp.getImage(); 461 org.eclipse.swt.graphics.Image swtImg = new org.eclipse.swt.graphics.Image( 462 _dev, awtImg.getWidth(), awtImg.getHeight()); 463 Pattern p = new Pattern(_dev, swtImg); 465 _gc.setForegroundPattern(p); 466 swtImg.dispose(); 467 } 468 } 469 470 473 public void setRenderingHint(Key hintKey, Object hintValue) { 474 if (hintKey == RenderingHints.KEY_ANTIALIASING) { 476 if (hintValue == RenderingHints.VALUE_ANTIALIAS_OFF) { 477 _gc.setAntialias(SWT.OFF); 478 } else if (hintValue == RenderingHints.VALUE_ANTIALIAS_ON) { 479 _gc.setAntialias(SWT.ON); 480 } else { 481 _gc.setAntialias(SWT.DEFAULT); 482 } 483 hints.put(hintKey, hintValue); 484 } 485 if (hintKey == RenderingHints.KEY_TEXT_ANTIALIASING) { 486 if (hintValue == RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) { 487 _gc.setTextAntialias(SWT.OFF); 488 } else if (hintValue == RenderingHints.VALUE_TEXT_ANTIALIAS_ON) { 489 _gc.setTextAntialias(SWT.ON); 490 } else { 491 _gc.setTextAntialias(SWT.DEFAULT); 492 } 493 hints.put(hintKey, hintValue); 494 } 495 if (hintKey == RenderingHints.KEY_INTERPOLATION) { 496 if (hintValue == RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) { 497 _gc.setInterpolation(SWT.LOW); 498 } else if ((hintValue == RenderingHints.VALUE_INTERPOLATION_BILINEAR) 499 || (hintValue == RenderingHints.VALUE_INTERPOLATION_BICUBIC)) { 500 _gc.setTextAntialias(SWT.HIGH); 501 } else { 502 _gc.setTextAntialias(SWT.DEFAULT); 503 } 504 hints.put(hintKey, hintValue); 505 } 506 } 507 508 public void setRenderingHints(Map <?, ?> hints) { 509 for (Object hint : hints.keySet()) { 510 setRenderingHint((Key) hint, hints.get(hint)); 511 } 512 } 513 514 527 public void setStroke(Stroke s) { 528 if (s instanceof BasicStroke ) { 530 BasicStroke bs = (BasicStroke ) s; 531 532 _gc.setLineWidth((int) bs.getLineWidth()); 534 535 switch (bs.getLineJoin()) { 537 case BasicStroke.JOIN_BEVEL : 538 _gc.setLineJoin(SWT.JOIN_BEVEL); 539 break; 540 case BasicStroke.JOIN_MITER : 541 _gc.setLineJoin(SWT.JOIN_MITER); 542 break; 543 case BasicStroke.JOIN_ROUND : 544 _gc.setLineJoin(SWT.JOIN_ROUND); 545 break; 546 } 547 548 switch (bs.getEndCap()) { 550 case BasicStroke.CAP_BUTT : 551 _gc.setLineCap(SWT.CAP_FLAT); 552 break; 553 case BasicStroke.CAP_ROUND : 554 _gc.setLineCap(SWT.CAP_ROUND); 555 break; 556 case BasicStroke.CAP_SQUARE : 557 _gc.setLineCap(SWT.CAP_SQUARE); 558 break; 559 } 560 561 _gc.setLineStyle(SWT.LINE_SOLID); 563 564 float[] dashes = bs.getDashArray(); 566 if (dashes != null) { 567 int[] a = new int[dashes.length]; 570 for (int i = 0; i < a.length; i++) { 571 a[i] = (int) dashes[i]; 572 } 573 _gc.setLineDash(a); 575 a = null; 576 dashes = null; 577 } 578 } 579 } 580 581 public void setTransform(AffineTransform tx) { 582 Transform t = toSWTTransform(tx); 583 _gc.setTransform(t); 584 t.dispose(); 585 } 586 587 590 public void shear(double shx, double shy) { 591 Transform t = new Transform (_dev); 592 _gc.getTransform(t); 593 Transform shear = new Transform (_dev, 1f, (float) shx, (float) shy, 1f, 0, 0); 594 t.multiply(shear); 595 _gc.setTransform(t); 596 t.dispose(); 597 } 598 599 600 public void transform(AffineTransform Tx) { 601 Transform t = new Transform (_dev); 602 _gc.getTransform(t); 603 t.multiply(toSWTTransform(Tx)); 604 _gc.setTransform(t); 605 t.dispose(); 606 } 607 608 public void translate(double tx, double ty) { 609 Transform t = new Transform (_dev); 610 _gc.getTransform(t); 611 t.translate((float) tx, (float) ty); 612 _gc.setTransform(t); 613 t.dispose(); 614 } 615 616 public void translate(int x, int y) { 617 translate((double) x, (double) y); 618 } 619 620 public void clearRect(int x, int y, int width, int height) { 621 int alpha = _gc.getAlpha(); 622 _gc.setAlpha(0); 623 _gc.fillRectangle(x, y, width, height); 625 _gc.setAlpha(alpha); 626 } 627 628 632 public void clipRect(int x, int y, int width, int height) { 633 org.eclipse.swt.graphics.Rectangle clip = _gc.getClipping(); 634 clip.intersect(new org.eclipse.swt.graphics.Rectangle(x, y, width, height)); 635 _gc.setClipping(clip); 636 } 637 638 public void copyArea(int x, int y, int width, int height, int dx, int dy) { 639 _gc.copyArea(x, y, width, height, dx, dy); 640 } 641 642 public Graphics create() { 643 return new SWTGraphics2D(_gc, _dev); 644 } 645 646 public void dispose() { 647 _gc = null; 648 _dev = null; 649 hints.clear(); 650 hints = null; 651 } 652 653 public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) { 654 _gc.drawArc(x, y, width, height, startAngle, arcAngle); 655 } 656 657 public boolean drawImage(Image img, int x, int y, Color bgcolor, 658 ImageObserver observer) { 659 return false; 661 } 662 663 public boolean drawImage(Image img, int x, int y, ImageObserver observer) { 664 return false; 666 } 667 668 public boolean drawImage(Image img, int x, int y, int width, int height, 669 Color bgcolor, ImageObserver observer) { 670 return false; 672 } 673 674 public boolean drawImage(Image img, int x, int y, int width, int height, 675 ImageObserver observer) { 676 return false; 678 } 679 680 public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, 681 int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) { 682 return false; 684 } 685 686 public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, 687 int sy1, int sx2, int sy2, ImageObserver observer) { 688 return false; 690 } 691 692 public void drawLine(int x1, int y1, int x2, int y2) { 693 _gc.drawLine(x1, y1, x2, y2); 694 } 695 696 public void drawOval(int x, int y, int width, int height) { 697 _gc.drawOval(x, y, width, height); 698 } 699 700 public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) { 701 int[] buf = new int[nPoints * 2]; 702 int j = 0; 703 for (int i = 0; i < nPoints; i++) { 704 buf[j++] = xPoints[i]; 705 buf[j++] = yPoints[i]; 706 } 707 _gc.drawPolygon(buf); 708 buf = null; 709 } 710 711 public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) { 712 int[] buf = new int[nPoints * 2]; 713 int j = 0; 714 for (int i = 0; i < nPoints; i++) { 715 buf[j++] = xPoints[i]; 716 buf[j++] = yPoints[i]; 717 } 718 _gc.drawPolyline(buf); 719 buf = null; 720 } 721 722 public void drawRoundRect(int x, int y, int width, int height, int arcWidth, 723 int arcHeight) { 724 _gc.drawRoundRectangle(x, y, width, height, arcWidth, arcHeight); 725 } 726 727 731 public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) { 732 swapColors(); 733 _gc.fillArc(x, y, width, height, startAngle, arcAngle); 734 swapColors(); 735 } 736 737 741 public void fillOval(int x, int y, int width, int height) { 742 swapColors(); 743 _gc.fillOval(x, y, width, height); 744 swapColors(); 745 } 746 747 751 public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) { 752 int[] buf = new int[nPoints * 2]; 753 int j = 0; 754 for (int i = 0; i < nPoints; i++) { 755 buf[j++] = xPoints[i]; 756 buf[j++] = yPoints[i]; 757 } 758 swapColors(); 759 _gc.fillPolygon(buf); 760 swapColors(); 761 buf = null; 762 } 763 764 768 public void fillRect(int x, int y, int width, int height) { 769 swapColors(); 770 _gc.fillRectangle(x, y, width, height); 771 swapColors(); 772 } 773 774 778 public void fillRoundRect(int x, int y, int width, int height, int arcWidth, 779 int arcHeight) { 780 swapColors(); 781 _gc.fillRoundRectangle(x, y, width, height, arcWidth, arcHeight); 782 swapColors(); 783 } 784 785 790 public Shape getClip() { 791 return getClipBounds(); 792 } 793 794 public Rectangle getClipBounds() { 795 org.eclipse.swt.graphics.Rectangle r = _gc.getClipping(); 796 return new Rectangle (r.x, r.y, r.width, r.height); 797 } 798 799 public Color getColor() { 800 return toAWTColor(_gc.getForeground()); 801 } 802 803 810 public Font getFont() { 811 FontData f = _gc.getFont().getFontData()[0]; 812 int style = Font.PLAIN; 813 if ((f.getStyle() & SWT.BOLD) != 0) { 814 style = Font.BOLD; 815 } 816 if ((f.getStyle() & SWT.ITALIC) != 0) { 817 style |= Font.ITALIC; 818 } 819 int pixels = (int) (f.getHeight() * _dev.getDPI().x / 72.0); 821 return new Font (f.getName(), style, pixels); 822 } 823 824 public FontMetrics getFontMetrics(Font f) { 825 return Toolkit.getDefaultToolkit().getFontMetrics(f); 826 } 827 828 public void setClip(int x, int y, int width, int height) { 829 _gc.setClipping(x, y, width, height); 830 } 831 832 public void setClip(Shape clip) { 833 Path p = toPath(clip); 834 _gc.setClipping(p); 835 p.dispose(); 836 } 837 838 public void setColor(Color c) { 839 _gc.setForeground(toSWTColor(c)); 840 _gc.setAlpha(c.getAlpha()); 841 needSwap = true; 842 } 843 844 851 public void setFont(Font font) { 852 int style = SWT.NORMAL; 853 if (font.isBold()) { 854 style |= SWT.BOLD; 855 } 856 if (font.isItalic()) { 857 style |= SWT.ITALIC; 858 } 859 int points = (int) (font.getSize2D() * 72.0 / _dev.getDPI().x); 861 862 org.eclipse.swt.graphics.Font f = new org.eclipse.swt.graphics.Font(_dev, font 863 .getFamily(), points, style); 864 _gc.setFont(f); 865 f.dispose(); 866 } 867 868 public void setPaintMode() { 869 _gc.setXORMode(false); 870 } 871 872 public void setXORMode(Color c1) { 873 _gc.setXORMode(true); 875 } 876 877 884 public BufferedImage toAWT(ImageData data) 885 { 886 ColorModel colorModel = null; 887 PaletteData palette = data.palette; 888 if (palette.isDirect) { 889 colorModel = new DirectColorModel (data.depth, palette.redMask, palette.greenMask, palette.blueMask); 890 BufferedImage bufferedImage = new BufferedImage (colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); 891 WritableRaster raster = bufferedImage.getRaster(); 892 int[] pixelArray = new int[3]; 893 for (int y = 0; y < data.height; y++) { 894 for (int x = 0; x < data.width; x++) { 895 int pixel = data.getPixel(x, y); 896 RGB rgb = palette.getRGB(pixel); 897 pixelArray[0] = rgb.red; 898 pixelArray[1] = rgb.green; 899 pixelArray[2] = rgb.blue; 900 raster.setPixels(x, y, 1, 1, pixelArray); 901 } 902 } 903 return bufferedImage; 904 } else { 905 RGB[] rgbs = palette.getRGBs(); 906 byte[] red = new byte[rgbs.length]; 907 byte[] green = new byte[rgbs.length]; 908 byte[] blue = new byte[rgbs.length]; 909 for (int i = 0; i < rgbs.length; i++) { 910 RGB rgb = rgbs[i]; 911 red[i] = (byte)rgb.red; 912 green[i] = (byte)rgb.green; 913 blue[i] = (byte)rgb.blue; 914 } 915 if (data.transparentPixel != -1) { 916 colorModel = new IndexColorModel (data.depth, rgbs.length, red, green, blue, data.transparentPixel); 917 } else { 918 colorModel = new IndexColorModel (data.depth, rgbs.length, red, green, blue); 919 } 920 BufferedImage bufferedImage = new BufferedImage (colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); 921 WritableRaster raster = bufferedImage.getRaster(); 922 int[] pixelArray = new int[1]; 923 for (int y = 0; y < data.height; y++) { 924 for (int x = 0; x < data.width; x++) { 925 int pixel = data.getPixel(x, y); 926 pixelArray[0] = pixel; 927 raster.setPixel(x, y, pixelArray); 928 } 929 } 930 return bufferedImage; 931 } 932 } 933 934 941 public ImageData toSWTImageData(BufferedImage bufferedImage) 942 { 943 if (bufferedImage.getColorModel() instanceof DirectColorModel ) { 944 DirectColorModel colorModel = (DirectColorModel )bufferedImage.getColorModel(); 945 PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask()); 946 ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); 947 WritableRaster raster = bufferedImage.getRaster(); 948 int[] pixelArray = new int[3]; 949 for (int y = 0; y < data.height; y++) { 950 for (int x = 0; x < data.width; x++) { 951 raster.getPixel(x, y, pixelArray); 952 int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); 953 data.setPixel(x, y, pixel); 954 } 955 } 956 return data; 957 } else if (bufferedImage.getColorModel() instanceof IndexColorModel ) { 958 IndexColorModel colorModel = (IndexColorModel )bufferedImage.getColorModel(); 959 int size = colorModel.getMapSize(); 960 byte[] reds = new byte[size]; 961 byte[] greens = new byte[size]; 962 byte[] blues = new byte[size]; 963 colorModel.getReds(reds); 964 colorModel.getGreens(greens); 965 colorModel.getBlues(blues); 966 RGB[] rgbs = new RGB[size]; 967 for (int i = 0; i < rgbs.length; i++) { 968 rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); 969 } 970 PaletteData palette = new PaletteData(rgbs); 971 ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); 972 data.transparentPixel = colorModel.getTransparentPixel(); 973 WritableRaster raster = bufferedImage.getRaster(); 974 int[] pixelArray = new int[1]; 975 for (int y = 0; y < data.height; y++) { 976 for (int x = 0; x < data.width; x++) { 977 raster.getPixel(x, y, pixelArray); 978 data.setPixel(x, y, pixelArray[0]); 979 } 980 } 981 return data; 982 } 983 return null; 984 } 985 986 public org.eclipse.swt.graphics.Image toSWT(BufferedImage img) 987 { 988 return new org.eclipse.swt.graphics.Image(_dev, toSWTImageData(img)); 989 } 990 } 991 | Popular Tags |