001: /*******************************************************************************
002: * Copyright (c) 2007-2008 Kirill Grouchnikov and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *******************************************************************************/package test;
008:
009: import java.awt.*;
010: import java.awt.Color;
011: import java.awt.Font;
012: import java.awt.image.*;
013: import java.io.IOException;
014: import java.util.Map;
015:
016: import javax.imageio.ImageIO;
017: import javax.swing.*;
018:
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.graphics.*;
021: import org.eclipse.swt.graphics.Image;
022: import org.eclipse.swt.widgets.Display;
023:
024: public class MainBackground extends JFrame {
025: private BufferedImage bi;
026:
027: protected class SwtPanel extends JPanel {
028: @Override
029: protected void paintComponent(Graphics g) {
030: g.drawImage(bi, 0, 0, null);
031: }
032: }
033:
034: public MainBackground() throws IOException {
035: this .setLayout(new BorderLayout());
036: SwtPanel panel = new SwtPanel();
037: panel.setOpaque(true);
038: this .add(panel, BorderLayout.CENTER);
039:
040: // Load AWT image
041: BufferedImage awtImage = ImageIO.read(MainBackground.class
042: .getClassLoader().getResourceAsStream(
043: "test/background.PNG"));
044: int width = awtImage.getWidth();
045: int height = awtImage.getHeight();
046:
047: this .setSize(width, height);
048: this .setLocationRelativeTo(null);
049: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
050:
051: Display display = Display.getDefault();
052:
053: long time = System.nanoTime();
054: PaletteData PALETTE_DATA = new PaletteData(0xFF0000, 0xFF00,
055: 0xFF);
056: ImageData swtImageData = new ImageData(width, height, 24,
057: PALETTE_DATA);
058: // int step = swtImageData.depth / 8;
059: byte[] data = swtImageData.data;
060: int[] awtPixels = null;
061: awtPixels = awtImage.getRGB(0, 0, width, height, awtPixels, 0,
062: width);
063: for (int i = 0; i < height; i++) {
064: int idx = i * swtImageData.bytesPerLine;
065: for (int j = 0; j < width; j++) {
066: int rgb = awtPixels[j + i * width];
067: for (int k = swtImageData.depth - 8; k >= 0; k -= 8) {
068: data[idx++] = (byte) ((rgb >> k) & 0xFF);
069: }
070: }
071: }
072: System.out.println("AWT to SWT " + (System.nanoTime() - time));
073: time = System.nanoTime();
074:
075: // Create SWT image and render some text in it
076: Image swtImage = new Image(display, swtImageData);
077: GC gc = new GC(swtImage);
078: gc.setTextAntialias(SWT.DEFAULT);
079: gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
080:
081: TextLayout layout = new TextLayout(display);
082: org.eclipse.swt.graphics.Font swtFont = new org.eclipse.swt.graphics.Font(
083: display, "Segoe UI", 9, SWT.BOLD);
084: layout.setFont(swtFont);
085: layout
086: .setText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
087: layout.draw(gc, 10, 10);
088: layout.setText("1234567890!@#$%^&*()-=_+~`[]\\;',./{}|:\"<>?");
089: layout.draw(gc, 10, 50);
090:
091: org.eclipse.swt.graphics.Point swtStartMnemonicPoint = layout
092: .getLocation(22, false);
093: org.eclipse.swt.graphics.Point swtEndMnemonicPoint = layout
094: .getLocation(22, true);
095: int underlineY = 10 + swtStartMnemonicPoint.y
096: + layout.getLineMetrics(0).getHeight()
097: - layout.getLineMetrics(0).getDescent() + 1;
098: int start = 10 + swtStartMnemonicPoint.x;
099: int end = 10 + swtEndMnemonicPoint.x;
100: gc.drawLine(start, underlineY, end, underlineY);
101:
102: // gc.drawText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
103: // 10,
104: // 10, true);// ,
105: System.out.println("SWT text rendering "
106: + (System.nanoTime() - time));
107: time = System.nanoTime();
108:
109: // System.out.println(gc.getFont().getFontData()[0].getName());
110:
111: ImageData imageData = swtImage.getImageData();
112: this .bi = new BufferedImage(width, height,
113: BufferedImage.TYPE_INT_ARGB);
114:
115: int redMask = imageData.palette.redMask;
116: int redShift = imageData.palette.redShift;
117: int greenMask = imageData.palette.greenMask;
118: int greenShift = imageData.palette.greenShift;
119: int blueShift = imageData.palette.blueShift;
120: int blueMask = imageData.palette.blueMask;
121:
122: int[] lineData = new int[width];
123:
124: // Get the raw data buffer and write directly to it.
125: // Compared to BufferedImage.setRGB this speeds up the
126: // image creation by the factor of three.
127: WritableRaster srcRaster = this .bi.getRaster();
128: DataBufferInt dataBuffer = (DataBufferInt) srcRaster
129: .getDataBuffer();
130: int[] rawData = dataBuffer.getData();
131:
132: for (int y = 0; y < height; y++) {
133: imageData.getPixels(0, y, width, lineData, 0);
134: // Analyze each pixel value in the line
135: for (int x = 0; x < lineData.length; x++) {
136: // Extract the red, green and blue component -
137: // see org/eclipse/swt/internal/image/PngEncoder.java
138: int pixelValue = lineData[x];
139: int alpha = imageData.getAlpha(x, y);
140:
141: int r = pixelValue & redMask;
142: r = (redShift < 0) ? r >>> -redShift : r << redShift;
143: int g = pixelValue & greenMask;
144: g = (greenShift < 0) ? g >>> -greenShift
145: : g << greenShift;
146: int b = pixelValue & blueMask;
147: b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
148:
149: rawData[x + y * width] = alpha << 24 | r << 16 | g << 8
150: | b;
151: }
152: }
153: System.out.println("SWT to AWT " + (System.nanoTime() - time));
154: time = System.nanoTime();
155:
156: swtImage.dispose();
157: layout.dispose();
158: gc.dispose();
159: swtFont.dispose();
160:
161: Graphics2D g2d = (Graphics2D) bi.getGraphics();
162: Toolkit tk = Toolkit.getDefaultToolkit();
163: Map desktopHints = (Map) (tk
164: .getDesktopProperty("awt.font.desktophints"));
165: if (desktopHints != null) {
166: g2d.addRenderingHints(desktopHints);
167: }
168: Font font = new Font("Tahoma", Font.PLAIN, 11);
169: g2d.setFont(font);
170: g2d.setColor(Color.black);
171: g2d.drawString(
172: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
173: 10, 40);
174: g2d.drawString("1234567890!@#$%^&*()-=_+~`[]\\;',./{}|:\"<>?",
175: 10, 100);
176:
177: }
178:
179: public static void main(String[] args) {
180: SwingUtilities.invokeLater(new Runnable() {
181: public void run() {
182: try {
183: new MainBackground().setVisible(true);
184: } catch (IOException ioe) {
185: ioe.printStackTrace();
186: }
187: }
188: });
189: }
190: }
|