简体   繁体   中英

Copy Pixels From One Image To Another

I'm in the process of creating an image manipulator framework in 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:

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

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

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

manipulatedImageData.setPixel(x, y, outputPixel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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