Fix Jpeg error handling
Add fileio criticalsection and some inital check

FileRead_* functions are thread-safe from this commit