Skip to content

FreeImage identifies images written via PPM-FI as PBM-FI. #677

Closed

Description

@FirefoxMetzger

I found a strange behavior in freeimage. When I write an image using PPM-FI then FreeImage identifies the written file as an image in PBM format instead of PPM format. Here is the snippet to show the failure

Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import imageio as iio
>>> from imageio.core.request import Request
>>> fname = "test.ppm"
>>> im = iio.imread("imageio:chelsea.png")
>>> iio.imwrite(fname, im, format="PPM-FI")
>>> test_req = Request(fname, "ri")
>>> assert iio.formats["PBM-FI"].can_read(test_req) 
>>> assert iio.formats["PPM-FI"].can_read(test_req) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

Are PPM and PBM equivalent on a file-level? The following suggests that they are likely very similar:

>>> im1 = iio.imread(fname, format="PPM-FI")
>>> im2 = iio.imread(fname, format="PBM-FI") 
>>> import numpy as np
>>> np.allclose(im1, im2)
True

I have traced the source code for writing up to the point we hand the image over to FreeImage and confirmed that it is indeed PPM all the way. Later on, we read the file's header and identify the format based on that using fi.getFIF(request.filename, "r", request.firstbytes). This then re-identifies the file as PBM which is treated as incompatible with PPM.

@almarklein I am a bit lost, because I am only loosely familiar with FreeImage. I discovered this while refactoring imopen (more on that in the PR, in short: I want to configure the plugin manager using imageio.config and add lazy-loading of plugins upon first use.) and it would be important here that PPM-FI correctly reports that it can read PPM-FI.

Activity

  1. almarklein commented on Nov 5, 2021

    @almarklein
    Member

    This is the code path deeper in freeimage. You could see which step eventually sets ftype. It might be a bug in freeimage.

    if mode == "r":
    if bb is not None:
    fimemory = lib.FreeImage_OpenMemory(ctypes.c_char_p(bb), len(bb))
    ftype = lib.FreeImage_GetFileTypeFromMemory(
    ctypes.c_void_p(fimemory), len(bb)
    )
    lib.FreeImage_CloseMemory(ctypes.c_void_p(fimemory))
    if (ftype == -1) and os.path.isfile(filename):
    ftype = lib.FreeImage_GetFileType(efn(filename), 0)

  2. FirefoxMetzger commented on Nov 5, 2021

    @FirefoxMetzger
    ContributorAuthor

    @almarklein This could be indeed inside freeimage. We associate ftype==14 in our wrapper here:

    ("PPM", 14, "Portable Pixelmap (ASCII)", "ppm"),

    and this is what we eventually pass to freeimage when writing. However when we read the buffer here

    ftype = lib.FreeImage_GetFileTypeFromMemory(
    ctypes.c_void_p(fimemory), len(bb)
    )

    the returned fytpe is 7 which we associate with PBM:

    ("PBM", 7, "Portable Bitmap (ASCII)", "pbm"),

  3. almarklein commented on Nov 5, 2021

    @almarklein
    Member

    We could create a workaround. Something like if FreeImage_GetFileTypeFromMemory returns 7, also check the file extension.

  4. FirefoxMetzger commented on Nov 5, 2021

    @FirefoxMetzger
    ContributorAuthor

    I'm currently doing this in my feature branch

    if request._fif == self.fif:
            return True
        elif request._fif == 7 and self.fif == 14:
            # PPM gets identified as PBM and PPM can read PBM
            # see: https://github.com/imageio/imageio/issues/677
            return True

    which fixes the symptom. Not sure if we should go more low-level though. Does FreeImage have an issue tracker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions