Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiff is saved with uncompleted tag content #77

Closed
JiangXL opened this issue Apr 24, 2022 · 4 comments
Closed

Tiff is saved with uncompleted tag content #77

JiangXL opened this issue Apr 24, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@JiangXL
Copy link

JiangXL commented Apr 24, 2022

Hi, I try to write below omexml to TiffImages.IMAGEDESCRIPTION tag and save tiff, but saved tiff's IMAGEDESCRIPTION tag always lose final 2 chars! It is so weird... For example, if the original is ...</OME>\n, saved tiff will be ...</OME(julia take \n as a char ). But the dump xml from OMETIFF.jl and simple strings don't show this issue. As the same time, exiftool write tag normally.

run(`exiftool -ImageDescription=$tiffxml $img_name -overwrite_original`

The final goal is to save OMETIFF. To do that, I have to add two more chars in original xml: ...</OME>\n => ...</OME>\n>\n. It will be convenient and excited to write OMETIFF, so I'm looking for your idea.

"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<OME xsi:schemaLocation=\"http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd\" UUID=\"urn:uuid:39163ae6-e2d4-4bd4-b45f-e29234238f3f\" Creato" ⋯ 579 bytes ⋯ "  </Channel>\n      <TiffData FirstT=\"0\" IFD=\"0\" FirstC=\"0\" PlaneCount=\"1\" FirstZ=\"0\">\n        <UUID FileName=\"4D-series.ome.tif\">urn:uuid:39163ae6-e2d4-4bd4-b45f-e29234238f3f</UUID>\n      </TiffData>\n    </Pixels>\n  </Image>\n</OME>\n"
@tlnagy tlnagy added the bug Something isn't working label Apr 25, 2022
@tlnagy
Copy link
Owner

tlnagy commented Apr 25, 2022

This is definitely a bug. My best guess is that somehow the length of the string is being computed incorrectly?

@tlnagy
Copy link
Owner

tlnagy commented Apr 25, 2022

So after digging into this a bit more, it looks like this is caused by the UTF-8 encoding of the OME-XML.

julia> using TiffImages

julia> tf = TiffImages.TiffFile(UInt32);

julia> tag = TiffImages.Tag(TiffImages.IMAGEDESCRIPTION, "αβγ") # extended characters
Tag(IMAGEDESCRIPTION, "αβγ")

julia> write(tf, tag) # this should be false, because extended chars take up more space
true

julia> seekstart(tf.io);

julia> read(tf, TiffImages.Tag) # the last character is missing!
Tag(IMAGEDESCRIPTION, "αβ")

julia> sizeof(tag) # this would be correct if characters were ascii
4

julia> sizeof(tag.data) # what the actual length is
6

The TIFF standard was written prior to UTF-8 so it doesn't seem like there's a standardized way to handle this. But given that Julia uses UTF-8 basically everywhere, we should do our best to encode it properly.

tlnagy added a commit that referenced this issue Apr 25, 2022
Using `ncodeunits` handles the variable byte lengths for UTF-8
strings properly.
@tlnagy tlnagy closed this as completed in facdf3d Apr 25, 2022
@tlnagy
Copy link
Owner

tlnagy commented Apr 25, 2022

This should be fixed by #78, could you please test the latest dev version and see if that fixed your problem?

Also, if you are working on a writer for OME-TIFF files, it would be fantastic if you could submit a PR over at OMETIFF.jl! I've been meaning to do that forever, and would very happy to review it.

EDIT: For at least three years 😅 tlnagy/OMETIFF.jl#30

@JiangXL
Copy link
Author

JiangXL commented Apr 26, 2022

Thanks, I'm working at adapting this code to generate omexml. Although this implementation is only subset of OMEXML, I think it may be very helpful to manage multi dimensions image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants