.bmp file format of Heroes of Might and Magic: Nim parsing library

Application

Heroes of Might and Magic

File extension

bmp

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .bmp file format of Heroes of Might and Magic using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Nim source code to parse .bmp file format of Heroes of Might and Magic

heroes_of_might_and_magic_bmp.nim

import kaitai_struct_nim_runtime
import options

type
  HeroesOfMightAndMagicBmp* = ref object of KaitaiStruct
    `magic`*: uint16
    `width`*: uint16
    `height`*: uint16
    `data`*: seq[byte]
    `parent`*: KaitaiStruct

proc read*(_: typedesc[HeroesOfMightAndMagicBmp], io: KaitaiStream, root: KaitaiStruct, parent: KaitaiStruct): HeroesOfMightAndMagicBmp


proc read*(_: typedesc[HeroesOfMightAndMagicBmp], io: KaitaiStream, root: KaitaiStruct, parent: KaitaiStruct): HeroesOfMightAndMagicBmp =
  template this: untyped = result
  this = new(HeroesOfMightAndMagicBmp)
  let root = if root == nil: cast[HeroesOfMightAndMagicBmp](this) else: cast[HeroesOfMightAndMagicBmp](root)
  this.io = io
  this.root = root
  this.parent = parent

  let magicExpr = this.io.readU2le()
  this.magic = magicExpr
  let widthExpr = this.io.readU2le()
  this.width = widthExpr
  let heightExpr = this.io.readU2le()
  this.height = heightExpr
  let dataExpr = this.io.readBytes(int((this.width * this.height)))
  this.data = dataExpr

proc fromFile*(_: typedesc[HeroesOfMightAndMagicBmp], filename: string): HeroesOfMightAndMagicBmp =
  HeroesOfMightAndMagicBmp.read(newKaitaiFileStream(filename), nil, nil)