kornelski / pngquant

Lossy PNG compressor — pngquant command based on libimagequant library
https://pngquant.org
Other
5.25k stars 486 forks source link

Cannot open file for reading if the filename contains japanese characters(non-unicode?) #376

Closed GustJc closed 3 years ago

GustJc commented 3 years ago
$ ./pngquant.exe イメージ.png
error: cannot open イメージ.png for reading

Not sure if this is a windows problem or pngquant one but when trying to unpack any images that contain japanese characters in its name it cannot read the file. The GUI tool works fine, only the cli tool doesn't in this case.

I've tried using the normal windows terminal, git bash and cygwin to see if there was any difference but the same thing happens on all of them. I can properly compress the image if I rename it to contain only ascii characters.

kornelski commented 3 years ago

This is a known problem of Microsoft's C standard library. Their implementation of fopen only supports old MS-DOS codepages, and doesn't support Unicode.

GustJc commented 3 years ago

I understand it now.

I've make a quick workaround for myself to enable the conversion of those japanese named files. This is very wrong, but it seems to work all right for me so far anyways. Here is what I did if anyone else wants to make a personal build of it.

I've basically forced the locale before opening the file and it properly openned and compacted the image. For reference:

#include <locale.h>

// Inside static pngquant_error read_image
setlocale(LC_ALL, ".65001");

Result:

$ ./pngquant.exe イメージ.png --verbose
イメージ.png:
  read 5KB file
  passing sRGB tag from the input
  made histogram...2 colors found
  eliminated opaque tRNS-chunk entries...0 entries transparent
  mapped image to new colors...MSE=0.000 (Q=100)
  writing 2-color image as イメージ-fs8.png
  copied 1KB of additional PNG metadata
Quantized 1 image.
kornelski commented 3 years ago

This is great! Thank you for the tip.

jibsen commented 3 years ago

I have no experience with this, but just wanted to note that the docs for setlocale has a section on UTF-8 support that mentions this was added in a 2018 update of Windows 10 and requires static linking to support older Windows versions.

kornelski commented 3 years ago

I've released 2.15.0 with this fix