Encoding RGB image

It is not as simple as it seems.

Definitely read through this thread, which exposes a lot of the challenges involved in this problem:

Another relevant thread: