NT-MDT palette format: C++98/STL parsing library

It is a color scheme for visualising SPM scans.

Application

["Nova", "Image Analysis", "NanoEducator"]

File extension

pal

KS implementation details

License: Unlicense

This page hosts a formal specification of NT-MDT palette format using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.pal", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    nt_mdt_pal_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.signature() // => get signature

C++98/STL source code to parse NT-MDT palette format

nt_mdt_pal.h

#ifndef NT_MDT_PAL_H_
#define NT_MDT_PAL_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * It is a color scheme for visualising SPM scans.
 */

class nt_mdt_pal_t : public kaitai::kstruct {

public:
    class meta_t;
    class color_t;
    class col_table_t;

    nt_mdt_pal_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, nt_mdt_pal_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~nt_mdt_pal_t();

    class meta_t : public kaitai::kstruct {

    public:

        meta_t(kaitai::kstream* p__io, nt_mdt_pal_t* p__parent = 0, nt_mdt_pal_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~meta_t();

    private:
        std::string m_unkn00;
        std::string m_unkn01;
        std::string m_unkn02;
        std::string m_unkn03;
        uint16_t m_colors_count;
        std::string m_unkn10;
        std::string m_unkn11;
        std::string m_unkn12;
        uint16_t m_name_size;
        nt_mdt_pal_t* m__root;
        nt_mdt_pal_t* m__parent;

    public:

        /**
         * usually 0s
         */
        std::string unkn00() const { return m_unkn00; }
        std::string unkn01() const { return m_unkn01; }
        std::string unkn02() const { return m_unkn02; }

        /**
         * usually 0s
         */
        std::string unkn03() const { return m_unkn03; }
        uint16_t colors_count() const { return m_colors_count; }

        /**
         * usually 0s
         */
        std::string unkn10() const { return m_unkn10; }

        /**
         * usually 4
         */
        std::string unkn11() const { return m_unkn11; }

        /**
         * usually 0s
         */
        std::string unkn12() const { return m_unkn12; }
        uint16_t name_size() const { return m_name_size; }
        nt_mdt_pal_t* _root() const { return m__root; }
        nt_mdt_pal_t* _parent() const { return m__parent; }
    };

    class color_t : public kaitai::kstruct {

    public:

        color_t(kaitai::kstream* p__io, nt_mdt_pal_t::col_table_t* p__parent = 0, nt_mdt_pal_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~color_t();

    private:
        uint8_t m_red;
        uint8_t m_unkn;
        uint8_t m_blue;
        uint8_t m_green;
        nt_mdt_pal_t* m__root;
        nt_mdt_pal_t::col_table_t* m__parent;

    public:
        uint8_t red() const { return m_red; }
        uint8_t unkn() const { return m_unkn; }
        uint8_t blue() const { return m_blue; }
        uint8_t green() const { return m_green; }
        nt_mdt_pal_t* _root() const { return m__root; }
        nt_mdt_pal_t::col_table_t* _parent() const { return m__parent; }
    };

    class col_table_t : public kaitai::kstruct {

    public:

        col_table_t(uint16_t p_index, kaitai::kstream* p__io, nt_mdt_pal_t* p__parent = 0, nt_mdt_pal_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~col_table_t();

    private:
        uint8_t m_size1;
        uint8_t m_unkn;
        std::string m_title;
        uint16_t m_unkn1;
        std::vector<color_t*>* m_colors;
        uint16_t m_index;
        nt_mdt_pal_t* m__root;
        nt_mdt_pal_t* m__parent;

    public:
        uint8_t size1() const { return m_size1; }
        uint8_t unkn() const { return m_unkn; }
        std::string title() const { return m_title; }
        uint16_t unkn1() const { return m_unkn1; }
        std::vector<color_t*>* colors() const { return m_colors; }
        uint16_t index() const { return m_index; }
        nt_mdt_pal_t* _root() const { return m__root; }
        nt_mdt_pal_t* _parent() const { return m__parent; }
    };

private:
    std::string m_signature;
    uint32_t m_count;
    std::vector<meta_t*>* m_meta;
    std::string m_something2;
    std::vector<col_table_t*>* m_tables;
    nt_mdt_pal_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string signature() const { return m_signature; }
    uint32_t count() const { return m_count; }
    std::vector<meta_t*>* meta() const { return m_meta; }
    std::string something2() const { return m_something2; }
    std::vector<col_table_t*>* tables() const { return m_tables; }
    nt_mdt_pal_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // NT_MDT_PAL_H_

nt_mdt_pal.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "nt_mdt_pal.h"
#include "kaitai/exceptions.h"

nt_mdt_pal_t::nt_mdt_pal_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nt_mdt_pal_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_meta = 0;
    m_tables = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nt_mdt_pal_t::_read() {
    m_signature = m__io->read_bytes(26);
    if (!(signature() == std::string("\x4E\x54\x2D\x4D\x44\x54\x20\x50\x61\x6C\x65\x74\x74\x65\x20\x46\x69\x6C\x65\x20\x20\x31\x2E\x30\x30\x21", 26))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x4E\x54\x2D\x4D\x44\x54\x20\x50\x61\x6C\x65\x74\x74\x65\x20\x46\x69\x6C\x65\x20\x20\x31\x2E\x30\x30\x21", 26), signature(), _io(), std::string("/seq/0"));
    }
    m_count = m__io->read_u4be();
    m_meta = new std::vector<meta_t*>();
    const int l_meta = count();
    for (int i = 0; i < l_meta; i++) {
        m_meta->push_back(new meta_t(m__io, this, m__root));
    }
    m_something2 = m__io->read_bytes(1);
    m_tables = new std::vector<col_table_t*>();
    const int l_tables = count();
    for (int i = 0; i < l_tables; i++) {
        m_tables->push_back(new col_table_t(i, m__io, this, m__root));
    }
}

nt_mdt_pal_t::~nt_mdt_pal_t() {
    _clean_up();
}

void nt_mdt_pal_t::_clean_up() {
    if (m_meta) {
        for (std::vector<meta_t*>::iterator it = m_meta->begin(); it != m_meta->end(); ++it) {
            delete *it;
        }
        delete m_meta; m_meta = 0;
    }
    if (m_tables) {
        for (std::vector<col_table_t*>::iterator it = m_tables->begin(); it != m_tables->end(); ++it) {
            delete *it;
        }
        delete m_tables; m_tables = 0;
    }
}

nt_mdt_pal_t::meta_t::meta_t(kaitai::kstream* p__io, nt_mdt_pal_t* p__parent, nt_mdt_pal_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nt_mdt_pal_t::meta_t::_read() {
    m_unkn00 = m__io->read_bytes(3);
    m_unkn01 = m__io->read_bytes(2);
    m_unkn02 = m__io->read_bytes(1);
    m_unkn03 = m__io->read_bytes(1);
    m_colors_count = m__io->read_u2le();
    m_unkn10 = m__io->read_bytes(2);
    m_unkn11 = m__io->read_bytes(1);
    m_unkn12 = m__io->read_bytes(2);
    m_name_size = m__io->read_u2be();
}

nt_mdt_pal_t::meta_t::~meta_t() {
    _clean_up();
}

void nt_mdt_pal_t::meta_t::_clean_up() {
}

nt_mdt_pal_t::color_t::color_t(kaitai::kstream* p__io, nt_mdt_pal_t::col_table_t* p__parent, nt_mdt_pal_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nt_mdt_pal_t::color_t::_read() {
    m_red = m__io->read_u1();
    m_unkn = m__io->read_u1();
    m_blue = m__io->read_u1();
    m_green = m__io->read_u1();
}

nt_mdt_pal_t::color_t::~color_t() {
    _clean_up();
}

void nt_mdt_pal_t::color_t::_clean_up() {
}

nt_mdt_pal_t::col_table_t::col_table_t(uint16_t p_index, kaitai::kstream* p__io, nt_mdt_pal_t* p__parent, nt_mdt_pal_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_index = p_index;
    m_colors = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nt_mdt_pal_t::col_table_t::_read() {
    m_size1 = m__io->read_u1();
    m_unkn = m__io->read_u1();
    m_title = kaitai::kstream::bytes_to_str(m__io->read_bytes(_root()->meta()->at(index())->name_size()), std::string("UTF-16LE"));
    m_unkn1 = m__io->read_u2be();
    m_colors = new std::vector<color_t*>();
    const int l_colors = (_root()->meta()->at(index())->colors_count() - 1);
    for (int i = 0; i < l_colors; i++) {
        m_colors->push_back(new color_t(m__io, this, m__root));
    }
}

nt_mdt_pal_t::col_table_t::~col_table_t() {
    _clean_up();
}

void nt_mdt_pal_t::col_table_t::_clean_up() {
    if (m_colors) {
        for (std::vector<color_t*>::iterator it = m_colors->begin(); it != m_colors->end(); ++it) {
            delete *it;
        }
        delete m_colors; m_colors = 0;
    }
}