我正在用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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句