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.
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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
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
#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 < 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 = 0, psx_tim_t* p__root = 0);
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 = 0, psx_tim_t* p__root = 0);
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;
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_
// 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 = 0;
m_img = 0;
f_has_clut = false;
f_bpp = false;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
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 = new bitmap_t(m__io, this, m__root);
}
m_img = 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) {
if (m_clut) {
delete m_clut; m_clut = 0;
}
}
if (m_img) {
delete m_img; m_img = 0;
}
}
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;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
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;
}