简体   繁体   English

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

[英]Copy Pixels From One Image To Another

I'm in the process of creating an image manipulator framework in SWT. 我正在用SWT创建图像操纵器框架。 One part of it is selecting a specific part of the image and manipulating it somehow. 它的一部分是选择图像的特定部分并以某种方式对其进行操作。 Since I use third party software, I can only manipulate the entire image and want to copy the pixels of the selection over. 由于我使用第三方软件,因此我只能操作整个图像,并且想要复制选择的像素。

The function looks like this: 该函数如下所示:

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;
}

Now this works, but is slow. 现在可以使用,但是很慢。 Probably because the entire picture is used to draw a single pixel. 可能是因为整个图片用于绘制单个像素。

A second possibility is: 第二种可能性是:

    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);

But this doesn't work at all, I guess because the color palette changed while manipulating. 但这根本不起作用,我猜是因为在操作时调色板发生了变化。 In my case, grayscaling creates a yellowy grayscaled image. 在我的情况下,灰度会产生黄色的灰度图像。

So is there another possibility I'm missing? 那么,我还有其他可能吗? What is the recommended way for transfering pixels between images? 在图像之间传输像素的推荐方法是什么?

For ImageData you need to use the palette field: 对于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM