Decoding a .xwd image in Julia

Viewed 121

I am trying to write a decoder for a .xwd image (X Window Dump), since ImageMagick is quite slow. The only specifications I found are:

http://www.opensource.apple.com/source/X11/X11-0.40.80/xc/include/XWDFile.h?txt

https://formats.kaitai.io/xwd/index.html

From which I managed to read the header:

xwd_data = read(`xwd -id $id`)

function get_header(data)
    args = [reinterpret(Int32, reverse(data[4*i-3:4*i]))[1] for i in 1:25]
    xwd = XwdHeader(args...)
    return xwd
end

struct XwdHeader
    header_size::Int32
    file_version::Int32
    pixmap_format::Int32
    pixmap_depth::Int32
    pixmap_width::Int32
    pixmap_height::Int32
    xoffset::Int32
    byte_order::Int32
    bitmap_unit::Int32
    bitmap_bit_order::Int32
    bitmap_pad::Int32
    bits_per_pixel::Int32
    bytes_per_line::Int32
    visual_class::Int32
    red_mask::Int32
    green_mask::Int32
    blue_mask::Int32
    bits_per_rgb::Int32
    colormap_entries::Int32
    ncolors::Int32
    window_width::Int32
    window_height::Int32
    window_x::Int32
    window_y::Int32
    window_bdrwidth::Int32
end

and the colormap, which is stored in blocks of 12 bytes and in little-endian byte order:

function read_colormap_entry(n, data, header)
    offset = header.header_size + 1
    poff = 12*n
    px = Pixel(reinterpret(UInt32, reverse(data[offset+poff:offset+poff+3]))[1],
                reinterpret(UInt16, reverse(data[offset+poff+4:offset+poff+5]))[1],
                reinterpret(UInt16, reverse(data[offset+poff+6:offset+poff+7]))[1],
                reinterpret(UInt16, reverse(data[offset+poff+8:offset+poff+9]))[1],
                reinterpret(UInt8, data[offset+poff+10])[1],
                reinterpret(UInt8, data[offset+poff+11])[1])
    println("Pixel number ", px.entry_number >> 16)
    println("R ", px.red >> 8)
    println("G ", px.green >> 8)
    println("B ", px.blue >> 8)
    println("flags ", px.flags)
    println("padding ",px.padding)
end


struct Pixel
    entry_number::UInt32
    red::UInt16
    green::UInt16
    blue::UInt16
    flags::UInt8
    padding::UInt8
end
julia> read_colormap_entry(0, data, header)
Pixel number 0
R 0
G 0
B 0
flags 7
padding 0

julia> read_colormap_entry(1, data, header)
Pixel number 1
R 1
G 1
B 1
flags 7
padding 0

julia> read_colormap_entry(2, data, header)
Pixel number 2
R 2
G 2
B 2
flags 7
padding 0

Now I have the actual image data stored in 4 byte blocks per pixel in the "Direct Color" visual class. Does anybody know howto extract the RGB values from this ?

edit: By playing around with the data I found out how to extract the R and G values

function read_pixel(i, j, data, header::XwdHeader)
    w = header.window_width
    h = header.window_height
    offset = header.header_size + header.colormap_entries * 12 + 1
    poff = 4*((i-1)*w + (j-1))
    px = reinterpret(UInt32, reverse(data[offset+poff:offset+poff+3]))[1]
    println("Px value ", px)
    r = (px & xwd.red_mask) >> 16
    g = (px & xwd.green_mask) >> 8
    b = (px & xwd.blue_mask)
    println("r ", r)
    println("g ", g)
    println("b ", b)
end

which gives the correct R and G values, but the B value should be non zero.

julia> read_pixel(31, 31, data, xwd_header)
Px value 741685248
r 53
g 56
b 0

I basically have no idea what I am doing with the color masks and the bit-shifts. Can anyone explain this ? Thanks !

0 Answers
Related