channeltga

Targa image – damaged red and green channels


I've a problem with 16-bit targa file. I'm opening image and it colors are weird. It's problem with red and green channels – blue is fine.

How I can repair targa image, to look like on example? (first is original image, second is as must look.)

Image


Solution

  • EDIT: this answer gives results that look right, but won't be bit-identical to the original. Something weirder is going on.

    You can make your corrupted image out of your original image by subjecting the red and green channels to a function that doubles the value, subject to wraparound:

    F(r) = (r*2) % 255
    F(g) = (g*2) % 255
    

    These functions are not invertible, because more than one input value can map to the same output value. In particular,

    F^-1(r) = {r / 2, r / 2 + 128}
    

    But we can still try to recover the image if we're willing to tolerate some errors. We'll try to guess whether red (or green) should be high; if so, add 128.

    Two pieces of information can guide our guess:

    1. Is the blue channel high? Unless images contain strongly blue pixels, this is a hint that the red and green channels should be high, too.
    2. Is there an adjacent pixel whose red (or green) channel is high, while this pixel's red channel is very low (say, less than 64)? This may suggest that both pixels are relatively bright, but this pixel's red channel got wrapped.

    I recovered something very close to your original image using just the blue channel information to decide between r / 2 and r / 2 + 128, though it would probably be better using the neighboring pixels' red and green channels as well.


    As a side note, one way this sort of problem could occur is if there were originally (say) 6 bits of red information, but only the least significant 5 bits were retained when the file was written. It would be useful to look at how these images were acquired to make sure that you're not chopping off the most significant bits of your R and G channels somehow.