Initial implementation of jpeg encoder

currently only supports encoding 8 bit RGB images in 4:4:4 ratio
Small fix
Small fixes and rewrite write_bits
Unify some code