source: mainline/uspace/dist/src/bithenge/gif/gif.bh

Last change on this file was f1380b7, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, remaining files.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#
2# Copyright (c) 2012 Vojtech Horky
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# - Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# - Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# - The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28
29#
30# Graphics Interchange Format (image/gif)
31#
32
33
34transform chars(len) = ascii <- known_length(len);
35transform word = uint16le;
36
37
38
39transform gif_signature = chars(3);
40
41transform gif_version = chars(3);
42
43transform gif_color_map_entry = struct {
44 .red <- uint8;
45 .green <- uint8;
46 .blue <- uint8;
47};
48
49transform gif_color_map(length) = repeat(length) {
50 gif_color_map_entry
51};
52
53transform gif_color_map_from_bits_per_pixel(bits_per_pixel) = struct {
54 # We need to emulate missing << operator
55 switch (bits_per_pixel) {
56 1: { <- gif_color_map(2); };
57 2: { <- gif_color_map(4); };
58 3: { <- gif_color_map(8); };
59 4: { <- gif_color_map(16); };
60 5: { <- gif_color_map(32); };
61 6: { <- gif_color_map(64); };
62 7: { <- gif_color_map(128); };
63 8: { <- gif_color_map(256); };
64 }
65};
66
67
68# Process GIF data block:
69# - first byte denotes block size (.length)
70# - followed by .length bytes of the actual data (not processed here)
71# - followed by next block or terminator (zero)
72transform generic_data_block = do {
73 struct {
74 .length <- uint8;
75 .has_next <- nonzero_boolean <- (.length);
76 if (.has_next) {
77 .data <- known_length(.length);
78 }
79 }
80} while (.has_next);
81
82
83transform gif_image_block = struct {
84 .left <- word;
85 .top <- word;
86 .width <- word;
87 .height <- word;
88
89 <- struct {
90 .use_local_color_map <- bit;
91 .interlacing <- bit;
92 .reserved_bits <- uint_be(3);
93 .bits_per_pixel <- (in + 1) <- uint_be(3);
94 } <- bits_be <- known_length(1);
95
96 if (.use_local_color_map) {
97 .local_color_map <- gif_color_map_from_bits_per_pixel(.bits_per_pixel);
98 }
99
100 .lzw_initial_size <- uint8;
101 .lzw_data <- generic_data_block;
102};
103
104# TODO: interpret known extensions
105transform gif_extension = struct {
106 .function <- uint8;
107 .data <- generic_data_block;
108};
109
110# Switch over known blocks (image, extensions, end)
111transform gif_block = struct {
112 .kind <- uint8;
113 switch (.kind) {
114 33: { # exclamation mark -> extension block
115 .extension <- gif_extension;
116 };
117 44: { # comma -> image
118 .image <- gif_image_block;
119 };
120 59: { # semicolon -> terminator
121 .after_terminator <- repeat { uint8 };
122 };
123 else: {
124 .unknown <- repeat { uint8 };
125 };
126 }
127};
128
129transform main = struct {
130 .signature <- gif_signature;
131 .version <- gif_version;
132 .width <- word;
133 .height <- word;
134 <- struct {
135 .global_color_map_exists <- bit;
136 .color_resolution <- uint_be(3);
137 .reserved_bit <- uint_be(1);
138 .bits_per_pixel <- (in + 1) <- uint_be(3);
139 } <- bits_be <- known_length(1);
140 .background_color_index <- uint8;
141 .reserved <- uint8;
142 if (.global_color_map_exists) {
143 .global_color_map <- gif_color_map_from_bits_per_pixel(.bits_per_pixel);
144 }
145 .blocks <- repeat {
146 gif_block
147 };
148};
Note: See TracBrowser for help on using the repository browser.