ufo-kit / ufo-filters

Common plugin suite for the ufo-core processing framework
GNU Lesser General Public License v3.0
10 stars 14 forks source link

memory-in task not properly working with images of bitwidths 8 and 16 #230

Closed jonakopmann closed 1 year ago

jonakopmann commented 1 year ago

https://github.com/ufo-kit/ufo-filters/blob/82ab847a1d4158e4f7b66cc3fba423db02797dba/src/ufo-memory-in-task.c#L122

If the pointer passed into the task is e.g. a guint8 pointer the task tries to get the offset of one image as if it would have the bitwidth of 32, since it doesnt take into account the bytes_per_pixel.

Unless this is supposed to be like this, changing _UfoMemoryInTaskPrivate.pointer to a guint8* and changing the memcopy line to:

memcpy(data, &priv->pointer[priv->read * priv->width * priv->height * priv->bytes_per_pixel], priv->width * priv->height * priv->bytes_per_pixel);

should solve this.

tfarago commented 1 year ago

Please check out the branch in #231. For this python program, np.uint8, np.uint16, and np.float32 work correctly:

import gi
import numpy as np
import tifffile
gi.require_version("Ufo", "0.0")
from gi.repository import Ufo

pm = Ufo.PluginManager()
sched = Ufo.FixedScheduler()
graph = Ufo.TaskGraph()

mem_in = pm.get_task("memory-in")
writer = pm.get_task("write")

n = 128
number = 5
mem_in.props.width = n
mem_in.props.height = n
mem_in.props.bitdepth = 8  # Or 16, or 32
mem_in.props.number = number

writer.props.filename = "/home/tomas/data/out.tif"
writer.props.tiff_bigtiff = False

ref = np.arange(number * n ** 2, dtype=np.uint8)  # or np.uint16, or np.float32
# mem_in.props.memory_location = "host"
mem_in.props.pointer = ref.__array_interface__["data"][0]

graph.connect_nodes(mem_in, writer)
sched.run(graph)

tifffile.imwrite("/home/tomas/data/out-gt.tif", ref.reshape(number, n, n))
out = tifffile.imread("/home/tomas/data/out.tif").flatten()
assert np.sum(out - ref) == 0.0
jonakopmann commented 1 year ago

Yeah I can test that. I already tested the changes on the last release commit, so I dont have to compile ufo-core, with my c++ program for 8 bit images and that worked fine

jonakopmann commented 1 year ago

Just tested it with the python script you send and it works fine now for all 3 bitdepths, thank you.