xwd (X Window Dump) bitmap image: Python parsing library

xwd is a file format written by eponymous X11 screen capture application (xwd stands for "X Window Dump"). Typically, an average user transforms xwd format into something more widespread by any of xwdtopnm and pnmto... utilities right away.

xwd format itself provides a raw uncompressed bitmap with some metainformation, like pixel format, width, height, bit depth, etc. Note that technically format includes machine-dependent fields and thus is probably a poor choice for true cross-platform usage.

Application

xwd

KS implementation details

License: CC0-1.0

This page hosts a formal specification of xwd (X Window Dump) bitmap image using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

data = Xwd.from_file("path/to/local/file.xwd (X Window Dump) bitmap image")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

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

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

data.header_size # => Size of the header in bytes

Python source code to parse xwd (X Window Dump) bitmap image

xwd.py

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

from pkg_resources import parse_version
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class Xwd(KaitaiStruct):
    """xwd is a file format written by eponymous X11 screen capture
    application (xwd stands for "X Window Dump"). Typically, an average
    user transforms xwd format into something more widespread by any of
    `xwdtopnm` and `pnmto...` utilities right away.
    
    xwd format itself provides a raw uncompressed bitmap with some
    metainformation, like pixel format, width, height, bit depth,
    etc. Note that technically format includes machine-dependent fields
    and thus is probably a poor choice for true cross-platform usage.
    """

    class PixmapFormat(Enum):
        x_y_bitmap = 0
        x_y_pixmap = 1
        z_pixmap = 2

    class ByteOrder(Enum):
        le = 0
        be = 1

    class VisualClass(Enum):
        static_gray = 0
        gray_scale = 1
        static_color = 2
        pseudo_color = 3
        true_color = 4
        direct_color = 5
    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.header_size = self._io.read_u4be()
        self._raw_hdr = self._io.read_bytes((self.header_size - 4))
        io = KaitaiStream(BytesIO(self._raw_hdr))
        self.hdr = self._root.Header(io, self, self._root)
        self._raw_color_map = [None] * (self.hdr.color_map_entries)
        self.color_map = [None] * (self.hdr.color_map_entries)
        for i in range(self.hdr.color_map_entries):
            self._raw_color_map[i] = self._io.read_bytes(12)
            io = KaitaiStream(BytesIO(self._raw_color_map[i]))
            self.color_map[i] = self._root.ColorMapEntry(io, self, self._root)


    class Header(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.file_version = self._io.read_u4be()
            self.pixmap_format = self._root.PixmapFormat(self._io.read_u4be())
            self.pixmap_depth = self._io.read_u4be()
            self.pixmap_width = self._io.read_u4be()
            self.pixmap_height = self._io.read_u4be()
            self.x_offset = self._io.read_u4be()
            self.byte_order = self._root.ByteOrder(self._io.read_u4be())
            self.bitmap_unit = self._io.read_u4be()
            self.bitmap_bit_order = self._io.read_u4be()
            self.bitmap_pad = self._io.read_u4be()
            self.bits_per_pixel = self._io.read_u4be()
            self.bytes_per_line = self._io.read_u4be()
            self.visual_class = self._root.VisualClass(self._io.read_u4be())
            self.red_mask = self._io.read_u4be()
            self.green_mask = self._io.read_u4be()
            self.blue_mask = self._io.read_u4be()
            self.bits_per_rgb = self._io.read_u4be()
            self.number_of_colors = self._io.read_u4be()
            self.color_map_entries = self._io.read_u4be()
            self.window_width = self._io.read_u4be()
            self.window_height = self._io.read_u4be()
            self.window_x = self._io.read_s4be()
            self.window_y = self._io.read_s4be()
            self.window_border_width = self._io.read_u4be()
            self.creator = (self._io.read_bytes_term(0, False, True, True)).decode(u"UTF-8")


    class ColorMapEntry(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.entry_number = self._io.read_u4be()
            self.red = self._io.read_u2be()
            self.green = self._io.read_u2be()
            self.blue = self._io.read_u2be()
            self.flags = self._io.read_u1()
            self.padding = self._io.read_u1()