让 Eclipse 插件程序具有二维作图能力(2)

发表于:2007-07-14来源:作者:点击数: 标签:
· 创建 SWT 图像 SWT Image 类似于 AWT BufferedImage,因为它的像素数据可以有直接读或者写操作访问。这意味着可以通过直接读取或者修改图像的数据,来设置或者取得图像中任何像素或者任何一组像素的颜色值。不过, SWT API 与相应的 AWT API 有很大不同,
· 创建 SWT 图像

SWT Image 类似于 AWT BufferedImage,因为它的像素数据可以有直接读或者写操作访问。这意味着可以通过直接读取或者修改图像的数据,来设置或者取得图像中任何像素或者任何一组像素的颜色值。不过, SWT API 与相应的 AWT API 有很大不同,并且更容易使用。

SWT 的 Image 类提供了几个构造函数,可以完成以下任务:

通过将一个文件名或者一个 InputStream 作为参数传递给构造函数装载一个现有的图像。图像的格式必须是所支持的格式之一:BMP、GIF、JPG、PNG、Windows ICO 等。

构造一个指定大小的空图像。可以通过修改其像素值或者向它拷贝一个 SWT 图形上下文的内容 (GC) 来绘制该图像。构造一个用像素值的现有缓冲区进行初始化的图像。您将使用第三个构造函数创建一个 SWT 图像,它是所绘制的 AWT 图像的副本。

· 关于 ImageData 类

有关图像的像素数据的信息包含在它的 ImageData 中。ImageData 是一个包含有关图像大小、调色板、颜色值和透明度信息的类。应当特别关注以下 ImageData 字段:

width 和 height 指定图像的大小。
depth 指定图像的颜色深度。可能的值为 1、2、4、8、16、24 或者 32,指定编码每一个像素的值所使用的位数。
palette 包含一个 PaletteData 对象,它存储有关图像的颜色模型的信息。与 AWT 一样,SWT 的颜色模型可以是索引或者直接的。如果颜色模型是索引的,那么 PaletteData 包含颜色索引。如果它是直接的,那么它包含转换(shift)信息,表明应当如何从像素的整数值中提取出颜色的 RGB 成分。

data 包含包含有像素值的字节缓冲区。与 AWT 缓冲区不同,SWT 缓冲区不是包含每一个像素的一种颜色值的整数数组。相反,它包含字节值。字节编码的方法取决于所使用的颜色深度。对于一个 8 位的图像,数组中的一个字节正好表示图像中一个像素的值。对于 16 位图像,每一个像素值编码为缓冲区中的两个字节。这两个字节以最低有效字节顺序存储。对于 24 或者 32 位图像,每一个像素值以最高有效位字节顺序编码为缓冲区中的三个或者四个字节。

bytesPerLine 表明缓冲区中有多少字节用于编写图像中一行像素的所有像素值。transparentPixel 定义用于图像中透明度的像素值。我们将使用带有一个透明度颜色信道的 24 位图像。图像中的每一个像素都编码为数组中的三个字节,顺序为红、绿和蓝成分。

· 转换图像

知道了图像数据就可以容易地将 AWT 图像转换为 SWT 图像。只要将(由 AWT 图像利用 getRGB(...) 返回的)整数缓冲区转换为 SWT 图像所使用的字节缓冲。图 3 显示了在 SWT 图像的缓冲区中这些值是如何存储的。

点击放大
图3. 将像素值写入 SWT 图像(图片较大,请放大查看)

和图 2 中一样,上图中下面的部分显示了图像缓冲区的内部表示。括号中的数字显示在缓冲区中表示其颜色值的那个像素的坐标。尽管每一个像素都用三个字节编码,但是对于 24 位图像,缓冲区中一行像素的大小并不总是 3*width。缓冲区中两行像素之间可能有一些索引未使用。要知道图像中每一行像素真正使用了多少字节(这样就可知道缓冲区中下一行从哪个索引位置开始),必须使用 ImageData 字段的 bytesPerLine 值。

· SWT 到 Java 2D 渲染器

清单 1 显示实现了屏外图像技术的一般性渲染器(renderer)的源代码。这个渲染器可以在 SWT 组件或者 Draw2D 图像上绘制时透明地使用 Java 2D 例程。

清单 1. SWT/Draw2D Java 2D renderer

package swtgraphics2d;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;

/**
* Helper class allowing the use of Java 2D on SWT or Draw2D graphical
* context.
* @author Yannick Saillet
*/
public class Graphics2DRenderer {
private static final PaletteData PALETTE_DATA =
new PaletteData(0xFF0000, 0xFF00, 0xFF);

private BufferedImage awtImage;
private Image swtImage;
private ImageData swtImageData;
private int[] awtPixels;

/** RGB value to use as transparent color */
private static final int TRANSPARENT_COLOR = 0x123456;

/**
* Prepare to render on a SWT graphics context.
*/
public void prepareRendering(GC gc) {
org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
prepareRendering(clip.x, clip.y, clip.width, clip.height);
}

/**
* Prepare to render on a Draw2D graphics context.
*/
public void prepareRendering(org.eclipse.draw2d.Graphics graphics) {
org.eclipse.draw2d.geometry.Rectangle clip =
graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle());
prepareRendering(clip.x, clip.y, clip.width, clip.height);
}

/**
* Prepare the AWT offscreen image for the rendering of the rectangular
* region given as parameter.
*/
private void prepareRendering(int clipX, int clipY, int clipW, int clipH) {
// check that the offscreen images are initialized and large enough
checkOffScreenImages(clipW, clipH);
// fill the region in the AWT image with the transparent color
java.awt.Graphics awtGraphics = awtImage.getGraphics();
awtGraphics.setColor(new java.awt.Color(TRANSPARENT_COLOR));
awtGraphics.fillRect(clipX, clipY, clipW, clipH);
}

/**
* Returns the Graphics2D context to use.
*/
public Graphics2D getGraphics2D() {
if (awtImage == null) return null;
return (Graphics2D) awtImage.getGraphics();
}

/**
* Complete the rendering by flushing the 2D renderer on a SWT graphical
* context.
*/
public void render(GC gc) {
if (awtImage == null) return;

org.eclipse.swt.graphics.Rectangle clip = gc.getClipping();
transferPixels(clip.x, clip.y, clip.width, clip.height);
gc.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height,
clip.x, clip.y, clip.width, clip.height);
}

/**
* Complete the rendering by flushing the 2D renderer on a Draw2D
* graphical context.
*/
public void render(org.eclipse.draw2d.Graphics graphics) {
if (awtImage == null) return;

org.eclipse.draw2d.geometry.Rectangle clip =
graphics.getClip(new org.eclipse.draw2d.geometry.Rectangle());
transferPixels(clip.x, clip.y, clip.width, clip.height);
graphics.drawImage(swtImage, clip.x, clip.y, clip.width, clip.height,
clip.x, clip.y, clip.width, clip.height);
}

/**
* Transfer a rectangular region from the AWT image to the SWT image.
*/
private void transferPixels(int clipX, int clipY, int clipW, int clipH) {
int step = swtImageData.depth / 8;
byte[] data = swtImageData.data;
awtImage.getRGB(clipX, clipY, clipW, clipH, awtPixels, 0, clipW);
for (int i = 0; i < clipH; i++) {
int idx = (clipY + i) * swtImageData.bytesPerLine + clipX * step;
for (int j = 0; j < clipW; j++) {
int rgb = awtPixels[j + i * clipW];
for (int k = swtImageData.depth - 8; k >= 0; k -= 8) {
data[idx++] = (byte) ((rgb >> k) & 0xFF);
}
}
}
if (swtImage != null) swtImage.dispose();
swtImage = new Image(Display.getDefault(), swtImageData);
}

/**
* Dispose the resources attached to this 2D renderer.
*/
public void dispose() {
if (awtImage != null) awtImage.flush();
if (swtImage != null) swtImage.dispose();
awtImage = null;
swtImageData = null;
awtPixels = null;
}

/**
* Ensure that the offscreen images are initialized and are at least
* as large as the size given as parameter.
*/
private void checkOffScreenImages(int width, int height) {
int currentImageWidth = 0;
int currentImageHeight = 0;
if (swtImage != null) {
currentImageWidth = swtImage.getImageData().width;
currentImageHeight = swtImage.getImageData().height;
}

// if the offscreen images are too small, recreate them
if (width > currentImageWidth || height > currentImageHeight) {
dispose();
width = Math.max(width, currentImageWidth);
height = Math.max(height, currentImageHeight);
awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
swtImageData = new ImageData(width, height, 24, PALETTE_DATA);
swtImageData.transparentPixel = TRANSPARENT_COLOR;
awtPixels = new int[width * height];
}
}
}



这个渲染器包含在一个实用程序类中。这个类包含并管理屏外图像技术所需要的 AWT 和 SWT 屏外图像的引用。还要注意:

字段 swtImageData 和 awtPixels 分别是在像素转移时包含 SWT 图像的像素值的缓冲区和用于包含 AWT 图像的像素值的缓冲区。

常量 TRANSPARENT_COLOR 包含一个作为 SWT 图像中透明颜色的 RGB 值。因为必须定义作为透明度信道的颜色以绘制背景,所以必须为此保留一个颜色值。在代码中我使用了随机值 0x123456。所有使用这个颜色值的像素都按透明处理。如果这个值所表示的颜色有可能在绘制操作中用到,可以用另一个值表示透明度。

原文转自:http://www.ltesting.net