.tim file format of Sony PlayStation (PSX) typical image format: C++11/STL parsing library

Application

Sony PlayStation (PSX) typical image format

File extension

tim

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

References

This page hosts a formal specification of .tim file format of Sony PlayStation (PSX) typical image 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++11/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.tim", 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:
    psx_tim_t data(&ks);
    

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

data.flags() // => Encodes bits-per-pixel and whether CLUT is present in a file or not

C++11/STL source code to parse .tim file format of Sony PlayStation (PSX) typical image format

psx_tim.h

#pragma once

// 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 <memory>

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

/**
 * \sa http://fileformats.archiveteam.org/wiki/TIM_(PlayStation_graphics) Source
 * \sa https://mrclick.zophar.net/TilEd/download/timgfx.txt Source
 * \sa https://www.romhacking.net/documents/31/ Source
 */

class psx_tim_t : public kaitai::kstruct {

public:
    class bitmap_t;

    enum bpp_type_t {
        BPP_TYPE_BPP_4 = 0,
        BPP_TYPE_BPP_8 = 1,
        BPP_TYPE_BPP_16 = 2,
        BPP_TYPE_BPP_24 = 3
    };

    psx_tim_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, psx_tim_t* p__root = nullptr);

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

public:
    ~psx_tim_t();

    class bitmap_t : public kaitai::kstruct {

    public:

        bitmap_t(kaitai::kstream* p__io, psx_tim_t* p__parent = nullptr, psx_tim_t* p__root = nullptr);

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

    public:
        ~bitmap_t();

    private:
        uint32_t m_len;
        uint16_t m_origin_x;
        uint16_t m_origin_y;
        uint16_t m_width;
        uint16_t m_height;
        std::string m_body;
        psx_tim_t* m__root;
        psx_tim_t* m__parent;

    public:
        uint32_t len() const { return m_len; }
        uint16_t origin_x() const { return m_origin_x; }
        uint16_t origin_y() const { return m_origin_y; }
        uint16_t width() const { return m_width; }
        uint16_t height() const { return m_height; }
        std::string body() const { return m_body; }
        psx_tim_t* _root() const { return m__root; }
        psx_tim_t* _parent() const { return m__parent; }
    };

private:
    bool f_has_clut;
    bool m_has_clut;

public:
    bool has_clut();

private:
    bool f_bpp;
    int32_t m_bpp;

public:
    int32_t bpp();

private:
    std::string m_magic;
    uint32_t m_flags;
    std::unique_ptr<bitmap_t> m_clut;
    bool n_clut;

public:
    bool _is_null_clut() { clut(); return n_clut; };

private:
    std::unique_ptr<bitmap_t> m_img;
    psx_tim_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }

    /**
     * Encodes bits-per-pixel and whether CLUT is present in a file or not
     */
    uint32_t flags() const { return m_flags; }

    /**
     * CLUT (Color LookUp Table), one or several palettes for indexed color image, represented as a
     */
    bitmap_t* clut() const { return m_clut.get(); }
    bitmap_t* img() const { return m_img.get(); }
    psx_tim_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

psx_tim.cpp

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

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

psx_tim_t::psx_tim_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, psx_tim_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_clut = nullptr;
    m_img = nullptr;
    f_has_clut = false;
    f_bpp = false;
    _read();
}

void psx_tim_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x10\x00\x00\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x10\x00\x00\x00", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_flags = m__io->read_u4le();
    n_clut = true;
    if (has_clut()) {
        n_clut = false;
        m_clut = std::unique_ptr<bitmap_t>(new bitmap_t(m__io, this, m__root));
    }
    m_img = std::unique_ptr<bitmap_t>(new bitmap_t(m__io, this, m__root));
}

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

void psx_tim_t::_clean_up() {
    if (!n_clut) {
    }
}

psx_tim_t::bitmap_t::bitmap_t(kaitai::kstream* p__io, psx_tim_t* p__parent, psx_tim_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void psx_tim_t::bitmap_t::_read() {
    m_len = m__io->read_u4le();
    m_origin_x = m__io->read_u2le();
    m_origin_y = m__io->read_u2le();
    m_width = m__io->read_u2le();
    m_height = m__io->read_u2le();
    m_body = m__io->read_bytes((len() - 12));
}

psx_tim_t::bitmap_t::~bitmap_t() {
    _clean_up();
}

void psx_tim_t::bitmap_t::_clean_up() {
}

bool psx_tim_t::has_clut() {
    if (f_has_clut)
        return m_has_clut;
    m_has_clut = (flags() & 8) != 0;
    f_has_clut = true;
    return m_has_clut;
}

int32_t psx_tim_t::bpp() {
    if (f_bpp)
        return m_bpp;
    m_bpp = (flags() & 3);
    f_bpp = true;
    return m_bpp;
}