.dcx file format: Python parsing library

DCX is a simple extension of PCX image format allowing to bundle many PCX images (typically, pages of a document) in one file. It saw some limited use in DOS-era fax software, but was largely superseded with multi-page TIFFs and PDFs since then.

File extension

dcx

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of .dcx file 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 Python generated by Kaitai Struct depends on the Python runtime library. You have to install it before you can parse data.

The Python runtime library can be installed from PyPI:

python3 -m pip install kaitaistruct

Code

Parse a local file and get structure in memory:

data = PcxDcx.from_file("path/to/local/file.dcx")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

raw = b"\x00\x01\x02..."
data = PcxDcx(KaitaiStream(BytesIO(raw)))

After that, one can get various attributes from the structure by invoking getter methods like:

data.magic # => get magic

Python source code to parse .dcx file format

pcx_dcx.py

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

import kaitaistruct
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO


if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
    raise Exception("Incompatible Kaitai Struct Python API: 0.9 or later is required, but you have %s" % (kaitaistruct.__version__))

import pcx
class PcxDcx(KaitaiStruct):
    """DCX is a simple extension of PCX image format allowing to bundle
    many PCX images (typically, pages of a document) in one file. It saw
    some limited use in DOS-era fax software, but was largely
    superseded with multi-page TIFFs and PDFs since then.
    """
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._read()

    def _read(self):
        self.magic = self._io.read_bytes(4)
        if not self.magic == b"\xB1\x68\xDE\x3A":
            raise kaitaistruct.ValidationNotEqualError(b"\xB1\x68\xDE\x3A", self.magic, self._io, u"/seq/0")
        self.files = []
        i = 0
        while True:
            _ = PcxDcx.PcxOffset(self._io, self, self._root)
            self.files.append(_)
            if _.ofs_body == 0:
                break
            i += 1

    class PcxOffset(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.ofs_body = self._io.read_u4le()

        @property
        def body(self):
            if hasattr(self, '_m_body'):
                return self._m_body

            if self.ofs_body != 0:
                _pos = self._io.pos()
                self._io.seek(self.ofs_body)
                self._m_body = pcx.Pcx(self._io)
                self._io.seek(_pos)

            return getattr(self, '_m_body', None)