簡體   English   中英

將像素從一個圖像復制到另一個

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM