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

Application

Sony PlayStation (PSX) typical image format

File extension

tim

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

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 a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.tim", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #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++/STL source code to parse .tim file format of Sony PlayStation (PSX) typical image format

psx_tim.h

#ifndef PSX_TIM_H_
#define PSX_TIM_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>

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

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 = 0, psx_tim_t* p__root = 0);

private:
    void _read();

public:
    ~psx_tim_t();

    class bitmap_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    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;
    bitmap_t* m_clut;
    bool n_clut;

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

private:
    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; }
    bitmap_t* img() const { return m_img; }
    psx_tim_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // PSX_TIM_H_

psx_tim.cpp

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

#include "psx_tim.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;
    f_has_clut = false;
    f_bpp = false;
    _read();
}

void psx_tim_t::_read() {
    m_magic = m__io->ensure_fixed_contents(std::string("\x10\x00\x00\x00", 4));
    m_flags = m__io->read_u4le();
    n_clut = true;
    if (has_clut()) {
        n_clut = false;
        m_clut = new bitmap_t(m__io, this, m__root);
    }
    m_img = new bitmap_t(m__io, this, m__root);
}

psx_tim_t::~psx_tim_t() {
    if (!n_clut) {
        delete m_clut;
    }
    delete m_img;
}

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() {
}

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;
}