spillerrec / imgviewer

Customizable image viewer with minimalistic interface
2 stars 1 forks source link

Improve casting of types in Reader* code #42

Closed spillerrec closed 8 years ago

spillerrec commented 8 years ago

There is a lot of casting from char* to unsigned char* and vice-versa when dealing with binary data from files. This requires a reinterpret_cast, but since most of the code uses C-style casts, this has gone unnoticed. This mostly appears to be caused by inattentiveness, the casts should be where they are needed and not hidden away in strange places.

We should try to pick the type which will require us to use the fewest amount of casts. It cannot be avoided everywhere as it is not consistent in the libraries which is used. For example Qt uses char while libexif uses unsigned char. unsigned char appears to be the better choice, but it should be more clear when starting to look deeper into the code.

Checklist:

libpng is a bit tricky. png_byte is defined to be unsigned char in png_conf.h, but this could potentally change, as png_conf.h states:

The typedefs should be at least as large as the numbers suggest (a png_uint_32 must be at least 32 bits long), but they don't have to be exactly that size.

So someone could potentially change it to a larger type, and the cast would be wrong. We should have a static_assert to check that the sizes are the same.

spillerrec commented 8 years ago

That should be all of it, png/jpeg was fixed in https://github.com/spillerrec/imgviewer/commit/cee4c66b10872c8c278cfcbb7df081f8f49b4b6f, forgot to tag the commit