After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 566862 - pixbuf_new_from_file does not autodetect format
pixbuf_new_from_file does not autodetect format
Status: RESOLVED FIXED
Product: gdk-pixbuf
Classification: Platform
Component: general
git master
Other All
: Normal normal
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2009-01-07 04:52 UTC by Scott Horowitz
Modified: 2010-07-10 04:08 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
A test program to obtain the mime type of a file (517 bytes, text/plain)
2009-01-07 15:58 UTC, Hagen Schink
Details

Description Scott Horowitz 2009-01-07 04:52:47 UTC
Please describe the problem:
If I take a png file and rename it to *.jpg, pixbuf_new_from_file tries to load the image as a jpeg image instead of autodetecting that it is a png file.

Using pygtk:
>>> import gtk
>>> f = gtk.gdk.pixbuf_new_from_file("/home/stonecrest/images/archlinux.png")
>>> f = gtk.gdk.pixbuf_new_from_file("/home/stonecrest/images/archlinux.png.jpg")
Traceback (most recent call last):
  • File "<stdin>", line 1 in <module>
glib.GError: Failed to load image '/home/stonecrest/images/archlinux.png.jpg': Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)
>>>

I'm quite certain this has worked correctly in previous gtk versions, although I'm not sure when it broke. Furthermore, the gtk documentation explicitly states that this function should detect the function. From http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-loading.html#gdk-pixbuf-new-from-file:
"The file format is detected automatically."

Steps to reproduce:


Actual results:


Expected results:


Does this happen every time?
Yes

Other information:
Tested using pygtk 2.13.0 on ArchLinux.
Comment 1 Scott Horowitz 2009-01-07 04:56:57 UTC
"this function should detect the function" - sorry, that should read "detect the format."

And just for clarification
$ file ~/images/archlinux.png.jpg 
/home/stonecrest/images/archlinux.png.jpg: PNG image, 1024 x 768, 8-bit grayscale, non-interlaced
Comment 2 Hagen Schink 2009-01-07 15:57:01 UTC
Hello there,

I had a look on gdk-pixbuf-io.c and especially the function _gdk_pixbuf_get_module (). This function uses g_content_type_guess () to determine the mime type of a file.

I started the python interpreter and tested the function gio.content_type_guess () and it turned out that it uses the ending to determine the mime type. This is also true for the function mimetypes.guess_type ().

This behaviour is also true for the following test program in C (attached).

So maybe we should pass this bug to GIO or clarify the documentation.
Comment 3 Hagen Schink 2009-01-07 15:58:39 UTC
Created attachment 125934 [details]
A test program to obtain the mime type of a file

This program uses g_content_type_guess () to get the mime type.
Comment 4 Matthias Clasen 2009-01-07 16:01:27 UTC
2009-01-07  Matthias Clasen  <mclasen@redhat.com>

        Bug 566862 – pixbuf_new_from_file does not autodetect format

        * gdk-pixbuf-io.c (_gdk_pixbuf_get_module): Go back to sniffing
        without looking at the filename, to avoid breaking expected
        functionality.
Comment 5 Daniel Macks 2009-02-06 22:12:55 UTC
SVN revision #22073 causes regression/breakage...see Bug #569412. I'd agree with Comment #2: let g_content_type_guess() have whatever data it can accept (i.e., "if there's a filename parameter, pass the filename"). If it should only look at content not name per its documentation, then that's an internal bug *there* not for the caller to work around. The work-around makes things worse too, because error messages don't have the filename so it's hard to figure out which file can't be loaded. Regarding how it *should* work, I don't see anything in the documentation statement "Creates a new pixbuf by loading an image from a file. The file format is detected automatically." that would restrict detection to the file's contents only (cf. filename). Conversely, g_content_type_guess() documentation is quite explicit that filename *is* usable for content-type analysis: "Guesses the content type based on example data. If the function is uncertain, result_uncertain will be set to TRUE. Either filename or data may be NULL, in which case the guess will be based solely on the other argument." I don't know the original intended behavior of pixbuf_new_from_file(), but it seems inconsistent to ignore filename in general, and especially given that another part explicitly uses filename.