Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将像素从一个图像复制到另一个图像_Java_Swt - Fatal编程技术网

Java 将像素从一个图像复制到另一个图像

Java 将像素从一个图像复制到另一个图像,java,swt,Java,Swt,我正在SWT中创建一个图像操纵器框架。其中一部分是选择图像的特定部分并以某种方式对其进行操作。因为我使用的是第三方软件,所以我只能操作整个图像,并希望复制选择的像素 函数如下所示: public Image doMagic(Image input, Selection selection) { Image manipulatedImage = manipulateImage(input); int width = manipulatedImage.getBounds().widt

我正在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);
但这根本不起作用,我想是因为操纵时调色板发生了变化。在我的例子中,灰度缩放创建一个黄色的灰度图像

那么,我是否还遗漏了另一种可能性?在图像之间传输像素的推荐方法是什么?

对于图像数据,您需要使用调色板字段:

int inputPixel = inputData.getPixel(x,y);

RGB rgb = inputData.palette.getRGB(inputPixel);

int outputPixel = manipulatedImageData.palette.getPixel(rgb);

manipulatedImageData.setPixel(x, y, outputPixel);