harvard-lts / kakadu-vips

Kakadu JP2 reader and writer for libvips
Apache License 2.0
3 stars 0 forks source link

Cannot process 32-bit floating point image #12

Closed scossu closed 3 months ago

scossu commented 5 months ago

First try:

>>> img = Image.new_from_file("sample-32bit_float.tif")
>>> img.kakadusave("sample-32bit_float_vips.jph", options="Cmodes=HT Creversible=yes")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/stefano/code/huit/imgconv/venv/lib/python3.11/site-packages/pyvips/vimage.py", line 1347, in call_function
    return pyvips.Operation.call(name, self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stefano/code/huit/imgconv/venv/lib/python3.11/site-packages/pyvips/voperation.py", line 305, in call
    raise Error('unable to call {0}'.format(operation_name))
pyvips.error.Error: unable to call kakadusave
  kakadusave: not an integer format

This seems to be a Kakadu limitation.

But then:

>>> from pyvips.enums import BandFormat
>>> img2 = img.cast(BandFormat.UINT)
>>> img
<pyvips.Image 720x479 float, 3 bands, scrgb>
>>> img2
<pyvips.Image 720x479 uint, 3 bands, scrgb>
>>> out = img2.kakadusave_buffer( options="Cmodes=HT Creversible=yes")
Segmentation fault (core dumped)

Not sure what's happening here. The converted image (img2) is all black if I save it as TIFF. Same for float to uchar. Is this the right conversion process?

jcupitt commented 5 months ago

Hiya,

Yes, it looks like we should only expect 8 and 16-bit images to work. A simple test is:

$ vips colourspace nina.jpg x.v rgb16

ie. write x.v, a vips image, as 16-bit RGB. This will take care of left justifying the bits (so the numeric range is 0-65535) and do any gamma adjustments. vips format is a very simple dump of the pixel array, so you can use it for any image type. The vips image viewer is handy for checking things:

https://github.com/jcupitt/vipsdisp/releases/tag/v3.0.4

(linux and windows binary)

If you turn on the info bar (burger menu / view / info bar) you can check exact pixel values:

image

Then save with kakadu:

$ vips kakadusave x.v x.jp2 

And view with vipsdisp (this will load with openjpeg):

image

... haha there seems to be some problem here. I'll have a look.

jcupitt commented 5 months ago

I fixed 16-bit support and added float load and save to git main. Float load and save still has some issues around gamma and clipping, but does mostly work.