[英]Copy Pixels From One Image To Another
我正在用SWT创建图像操纵器框架。 它的一部分是选择图像的特定部分并以某种方式对其进行操作。 由于我使用第三方软件,因此我只能操作整个图像,并且想要复制选择的像素。
该函数如下所示:
public Image doMagic(Image input, Selection selection) {
Image manipulatedImage = manipulateImage(input);
int width = manipulatedImage.getBounds().width;
int height = manipulatedImage.getBounds().height;
GC gc = new GC(manipulatedImage);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (!selection.containsPixel(x, y)) {
// we should not have transformed this pixel
//-> we set it back to the original one
gc.drawImage(input, x, y, 1, 1, x, y, 1, 1);
}
}
}
gc.dispose();
return manipulatedImage;
}
现在可以使用,但是很慢。 可能是因为整个图片用于绘制单个像素。
第二种可能性是:
ImageData inputData = input.getImageData();
ImageData manipulatedImageData = manipulatedImage.getImageData();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (!selection.containsPixel(x, y)) {
manipulatedImageData.setPixel(x, y, inputData.getPixel(x,y));
}
}
}
return new Image(image.getDevice(), manipulatedImageData);
但这根本不起作用,我猜是因为在操作时调色板发生了变化。 在我的情况下,灰度会产生黄色的灰度图像。
那么,我还有其他可能吗? 在图像之间传输像素的推荐方法是什么?
对于ImageData
您需要使用palette
字段:
int inputPixel = inputData.getPixel(x,y);
RGB rgb = inputData.palette.getRGB(inputPixel);
int outputPixel = manipulatedImageData.palette.getPixel(rgb);
manipulatedImageData.setPixel(x, y, outputPixel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.