.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

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.

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.ensureFixedContents([16, 0, 0, 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;
}));