Detect image type from buffer

As discussed in EyalAr/lwip#87 .
Please let me know if you feel like some parts need improvement. I'll be happy to make the needed adjustments.