Shifting color channels without drawing each channel separately?

286 Views Asked by At

I want to draw with Direct2D frames which color channels are shifted on x-axis. I know I could set the composition mode to D2D1_COMPOSITE_MODE_PLUS and draw each color channel separately so I can shift them manually. But I want to know if there is another (maybe more efficient) way of drawing shapes with shifted color channels?

I attached an image which shows what I mean. (I suggest to open this image in a new tab and zoom in to see the effect better) enter image description here

1

There are 1 best solutions below

1
On BEST ANSWER

The way this is typically done is to sample 3 pixels from the input image at a time, each separated by some amount in the x direction, and combine the red from one, the green from another, and the blue from the third. Unfortunately, I don't know DX2D at all, so I don't know the specifics of how it works there. But if you have a bitmap and a pointer to the pixels, you can simply subtract one (or more) pixels from that pointer, and add one or more pixels to the that pointer and read from those memory locations (being careful to account for image edges). Then pull the channels from the values you've read. For example:

RGBA8* pixel = baseAddressOfImage;
RGBA8* pixelMinus1 = pixel - 1;
RGBA8* pixelPuls1 = pixel + 1;
for each pixel in the output
{
    result.red = pixelMinus1->red;
    result.green = pixel->green;
    result.blue = pixelPlus1->blue;
    pixelMinus1++;
    pixel++;
    pixelPlus1++;
}

Note that you can add or subtract more than 1, but as mentioned above, you have to handle what happens at the edges in those cases.