.tim file format of Sony PlayStation (PSX) typical image format: JavaScript parsing library

Application

Sony PlayStation (PSX) typical image format

File extension

tim

KS implementation details

License: CC0-1.0

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 JavaScript generated by Kaitai Struct depends on the JavaScript runtime library. You have to install it before you can parse data.

The JavaScript runtime library is available at npm:

npm install kaitai-struct

Code

See the usage examples in the JavaScript notes.

Parse structure from an ArrayBuffer:

var arrayBuffer = ...;
var data = new PsxTim(new KaitaiStream(arrayBuffer));

After that, one can get various attributes from the structure by accessing fields or properties like:

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

JavaScript source code to parse .tim file format of Sony PlayStation (PSX) typical image format

PsxTim.js

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

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'));
  } else {
    root.PsxTim = factory(root.KaitaiStream);
  }
}(this, function (KaitaiStream) {
var PsxTim = (function() {
  PsxTim.BppType = Object.freeze({
    BPP_4: 0,
    BPP_8: 1,
    BPP_16: 2,
    BPP_24: 3,

    0: "BPP_4",
    1: "BPP_8",
    2: "BPP_16",
    3: "BPP_24",
  });

  function PsxTim(_io, _parent, _root) {
    this._io = _io;
    this._parent = _parent;
    this._root = _root || this;

    this._read();
  }
  PsxTim.prototype._read = function() {
    this.magic = this._io.readBytes(4);
    if (!((KaitaiStream.byteArrayCompare(this.magic, [16, 0, 0, 0]) == 0))) {
      throw new KaitaiStream.ValidationNotEqualError([16, 0, 0, 0], this.magic, this._io, "/seq/0");
    }
    this.flags = this._io.readU4le();
    if (this.hasClut) {
      this.clut = new Bitmap(this._io, this, this._root);
    }
    this.img = new Bitmap(this._io, this, this._root);
  }

  var Bitmap = PsxTim.Bitmap = (function() {
    function Bitmap(_io, _parent, _root) {
      this._io = _io;
      this._parent = _parent;
      this._root = _root || this;

      this._read();
    }
    Bitmap.prototype._read = function() {
      this.len = this._io.readU4le();
      this.originX = this._io.readU2le();
      this.originY = this._io.readU2le();
      this.width = this._io.readU2le();
      this.height = this._io.readU2le();
      this.body = this._io.readBytes((this.len - 12));
    }

    return Bitmap;
  })();
  Object.defineProperty(PsxTim.prototype, 'hasClut', {
    get: function() {
      if (this._m_hasClut !== undefined)
        return this._m_hasClut;
      this._m_hasClut = (this.flags & 8) != 0;
      return this._m_hasClut;
    }
  });
  Object.defineProperty(PsxTim.prototype, 'bpp', {
    get: function() {
      if (this._m_bpp !== undefined)
        return this._m_bpp;
      this._m_bpp = (this.flags & 3);
      return this._m_bpp;
    }
  });

  /**
   * Encodes bits-per-pixel and whether CLUT is present in a file or not
   */

  /**
   * CLUT (Color LookUp Table), one or several palettes for indexed color image, represented as a
   */

  return PsxTim;
})();
return PsxTim;
}));