Improved test.
[ocaml-bitstring.git] / examples / gif.ml
1 (* GIF header parser.
2  * $Id$
3  *)
4
5 open Printf
6
7 let () =
8   if Array.length Sys.argv <= 1 then
9     failwith "usage: gif input.gif";
10   let filename = Sys.argv.(1) in
11   let bits = Bitmatch.bitstring_of_file filename in
12
13   bitmatch bits with
14   | { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
15       width : 16 : littleendian;
16       height : 16 : littleendian;
17       colormap : 1;                     (* Has colormap? *)
18       colorbits : 3;                    (* Color res = colorbits+1 *)
19       sortflag : 1;
20       bps : 3;                          (* Bits/pixel = bps+1 *)
21       bg : 8;                           (* Background colour. *)
22       aspectratio : 8 } ->
23       printf "%s: GIF image:\n" filename;
24       printf "  size %d %d\n" width height;
25       printf "  has global colormap? %b\n" colormap;
26       printf "  colorbits %d\n" (colorbits+1);
27       printf "  global colormap is sorted? %b\n" sortflag;
28       printf "  bits/pixel %d\n" (bps+1);
29       printf "  background color index %d\n" bg;
30       printf "  aspect ratio %d\n" aspectratio
31
32   | { _ } ->
33       eprintf "%s: Not a GIF image\n" filename