OME Files in C++: Invalid OME-XML document


I am working on a C++ library that integrates the ome-files library.
In order to write my pixel data to an ome.tiff file, I’m closely following the example code from the tutorial:

Reading the tiff files works fine, and writing them also results in a correct ome.tiff file which I can open and view with ImageJ. However, when closing the writer, I get the following error message: Caught exception: Invalid OME-XML document. Also, when I use ImageJ to see the metadata, the DimensionOrder is messed up.

Any help on this would be very much appreciated!


Hi @luco, is your code available anywhere to view?
If you run the xmlvalid tool ( what validation errors are you seeing?

Hi @dgault. Thanks for your response!
I cannot share the full git repo ATM, but let me show you what my code looks like:

shared_ptr<OMEXMLMetadata> createMetadata(HistData &data){

    auto meta = make_shared<OMEXMLMetadata>();
    std::vector<shared_ptr<CoreMetadata>> seriesList;
    shared_ptr<CoreMetadata> core(make_shared<CoreMetadata>());


    core->sizeX = sqrt(data.dataRows);
    core->sizeY = sqrt(data.dataRows);
    core->sizeZ = 1U;
    core->sizeC.clear(); // defaults to 1 channel with 1 subchannel; clear this
    core->sizeC.push_back(1U); // replace with single black/white channel
    core->sizeT = data.dataCols;
    core->pixelType = ome::xml::model::enums::PixelType::UINT16;
    core->interleaved = false;
    core->bitsPerPixel = 12U;
    core->dimensionOrder = DimensionOrder::XYZTC;


    fillMetadata(*meta, seriesList);
    return meta;

void writePixelData(FormatWriter& writer, std::ostream& stream, HistData &data){
    // Total number of images (series)
    dimension_size_type ic = writer.getMetadataRetrieve()->getImageCount();
    stream << "Image count: " << ic << '\n';

    // Loop over images
    for (dimension_size_type i = 0 ; i < ic; ++i)
        // Change the current series to this index

        // Get meta data and print to console
        int resX = writer.getMetadataRetrieve()->getPixelsSizeX(i);
        int resY = writer.getMetadataRetrieve()->getPixelsSizeY(i);
        int resT = writer.getMetadataRetrieve()->getPixelsSizeT(i);
        std::cout << "\tSizeX = " << resX << std::endl;
        std::cout << "\tSizeY = " << resY << std::endl;
        std::cout << "\tSizeZ = " <<  writer.getMetadataRetrieve()->getPixelsSizeZ(i) << std::endl;
        std::cout << "\tSizeT = " <<  writer.getMetadataRetrieve()->getPixelsSizeT(i) << std::endl;
        std::cout << "\tChannelCount = " <<  writer.getMetadataRetrieve()->getChannelCount(i) << std::endl;

          // Total number of planes.
        dimension_size_type pc = 1U;
        pc *= writer.getMetadataRetrieve()->getPixelsSizeZ(i);
        pc *= writer.getMetadataRetrieve()->getPixelsSizeT(i);
        pc *= writer.getMetadataRetrieve()->getChannelCount(i);
        stream << "\tPlane count: " << pc << '\n';

        // Loop over planes (for this image index)
        for (dimension_size_type p = 0 ; p < pc; ++p)
            // Pixel buffer
                      PixelType::UINT16, ome::files::ENDIAN_LITTLE,
                      PixelBufferBase::make_storage_order(DimensionOrder::XYZTC, false)));

            // Fill pixel buffer with pixel data from HistData object
            for (dimension_size_type x = 0; x < resX; ++x)
                for (dimension_size_type y = 0; y < resY; ++y)
                    dimension_size_type idx_data = x + y*resX;

                    PixelBufferBase::indices_type idx;
                    std::fill(idx.begin(), idx.end(), 0);
                    idx[DIM_SPATIAL_X] = x;
                    idx[DIM_SPATIAL_Y] = y;
                    buffer->at(idx) =[idx_data][p];

          VariantPixelBuffer varBuffer(buffer);
            stream << "PixelBuffer PixelType is " << buffer->pixelType() << '\n';
            stream << "VariantPixelBuffer PixelType is " << varBuffer.pixelType() << '\n';
            stream << std::flush;

            // Write the the entire pixel buffer to the plane.
            writer.saveBytes(p, varBuffer);
            stream << "Wrote " << buffer->num_elements() << ' '
                   << buffer->pixelType() << " pixels in plane number " << p << "\n";

in the file calling these functions:

void histWriteOMETiff_0_2(HistData &data, FILE *file, const char *filepath)

    try {
        // Portable path
        path filename(filepath);

        // Create minimal metadata for the file to be written.
        auto meta = createMetadata(data);


        // Create TIFF writer
        auto writer = make_shared<OMETIFFWriter>();

        // Set writer options before opening a file
        auto retrieve = static_pointer_cast<MetadataRetrieve>(meta);

        // Open the file

        // Write pixel data
        writePixelData(*writer, std::cout, data);

        // Explicitly close writer
    catch (const std::exception& e)
        std::cerr << "Caught exception: " << e.what() << '\n';
    catch (...)
        std::cerr << "Caught unknown exception\n";

The output from xmlvalid:
Parsing schema path

Error parsing schema path from /Users/lukascodispoti/Documents/UZH/flim-control/buffers/test5_v0-2_Circular.ome.tiff

org.xml.sax.SAXParseException: Content is not allowed in prolog.

at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source) ~[bioformats_package.jar:6.5.1]

at java.xml/javax.xml.parsers.SAXParser.parse( ~[na:na]

at loci.common.xml.XMLTools.validateXML( ~[bioformats_package.jar:6.5.1]

at loci.common.xml.XMLTools.validateXML( ~[bioformats_package.jar:6.5.1]

at ~[bioformats_package.jar:6.5.1]

at ~[bioformats_package.jar:6.5.1]

at ~[bioformats_package.jar:6.5.1]

Thanks again!

Thanks for code snippets. The exception from the xmlvalid tool is an odd one, it leads me to think it may be due to and encoding issue or the xml somehow becoming malformed rather than missing metadata.

If you run the bfconvert tool like below is it able to complete or does it just give you the same stack trace as xmlvalid?
bfconvert -nopix -omexml path/to/myFile.ome.tiff

If it does not provide the XML can you open a small sample file and copy the first few lines of the XML from within the file, i suspect the first 2 lines as below may be enough:

<?xml version="1.0" encoding="UTF-8"?>

<OME xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Hi @dgault. Thanks for the support! It’s good to know that there is someone out there.

I believe that there was something wrong with the build that I was using.

After downloading the 0.6.0 release and building it myself, my code works like a charm.


1 Like