CImg
CImg copied to clipboard
Problem opening TIFF file with Cyrillic filename on Windows (with fix)
I recently found I couldn't open a file with a Cyrillic filename using libtiff via load_tiff. It's because the routine calls TIFFOpen and on Windows it should really call TIFFOpenW with a wchar_t argument. The fix is just a few lines of code:
#if cimg_OS==2 // cimg_OS is set to 2 on windows systems
int sizeRequired = MultiByteToWideChar(CP_UTF8, 0, filename, -1, nullptr, 0);
auto output = std::make_unique<wchar_t[]>(sizeRequired + 1);
MultiByteToWideChar(CP_UTF8, 0, filename, -1, output.get(), sizeRequired);
TIFF *tif = TIFFOpenW(output.get(), "r");
// wchar_t *output = new wchar_t[size_t(sizeRequired) + 1];
// MultiByteToWideChar(CP_UTF8, 0, filename, -1, output, sizeRequired);
// TIFF *tif = TIFFOpenW(output, "r");
// delete [] output;
#else
TIFF *tif = TIFFOpen(filename,"r");
#endif
I used std::make_unique because I'm not sure whether TIFFOpenW and MultiByteToWideChar can cause an exception but if they don't then the new, delete option is a perfectly good alternative and might be more portable. This allows UTF-8 encoded paths to work under Windows. They work fine on Unix and MacOS in any case.
Cheers Bill