GNOME Bugzilla – Bug 569671
gdk_pixbuf_new_from_file() can't open image/x-portable-pixmap
Last modified: 2010-07-10 04:09:21 UTC
Please describe the problem: If gtk2 is compiled with GDK_PIXBUF_USE_GIO_MIME set, gdk_pixbuf_new_from_file can't open pixmap files such as .ppm. This breaks functionality in some existing programs, e.g. Xournal loses the ability to annotate PDF files. The issue is that gdk_pixbuf_new_from_file() ultimately tries to determine the mime type by reading the file into memory and passing it to gio. However, the header for these image types is so rudimentary (e.g. "P6") that g_content_type_guess() can't determine the mime type without knowing the file's extension, and reports it as application/octet-stream. Compiling without the GDK_PIXBUF_USE_GIO_MIME makes everything work correctly. It seems like the simplest fix would be to change _gdk_pixbuf_get_module() to actually pass its "filename" variable to g_content_type_guess(). I have not tested this. Steps to reproduce: 1. Compile a problem with this main code: pixbuf = gdk_pixbuf_new_from_file(argv[1],&gerror); if(!originalpb) { printf("error message: %s\n", gerror->message); exit(1); } gdk_pixbuf_unref(originalpb); 2. Try to run it with a ppm file as argument. Actual results: An error is output: error message: Couldn't recognize the image file format for file test.ppm Expected results: No output. Does this happen every time? Yes. Other information:
Created attachment 127458 [details] [review] Proposed fix by passing filename to gio
The above patch is the one I mentioned in the bugreport. It fixes the problem on my machine.
We did pass the filename initially. That was removed when people started to complain that they could no longer name there png files foo.gif and still have eog load them... If g_content_type_guess can't correctly sniff ppm files, thats something that should be addressed in shared-mime-info
Well, now that's just silly. OK. If that needs to be worked around, then so be it. It is now reported to shared-mime-info <a href="http://bugs.freedesktop.org/show_bug.cgi?id=19811">here</a>.
shared-mime-info has marked this as WONTFIX because the magic for a PPM header is only 2 characters. I'm attaching another proposed fix for this problem. It checks if g_content_type_guess() returned an "uncertain" result, and if so then it checks based on the filename. This hopefully will satisfy everyone except those who want to load misnamed files with malformed headers.
Created attachment 128312 [details] [review] Proposed fix by rechecking when mime sniffer is uncertain
Sounds like an acceptable compromise to me.
OK. I don't have access to commit this, can someone else?
* gdk-pixbuf-io.c (_gdk_pixbuf_get_module): If pure sniffing yields uncertain results, try again with the filename. Patch by Tyler Lawson
This bug still breaks things such as xscreensaver's webcollage that name various images without an extension. Perhaps instead of using filename in a case of uncertainty, it could check for the PPM header. It should only get called when it's expected to be an image and if it fails, it won't be anything that would have worked without this tweak. This could also be a last resort after the filename fix when that doesn't work either.