1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2006 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
34 * Carl D. Worth <cworth@cworth.org>
39 typedef struct _lzw_buf
{
40 cairo_status_t status
;
46 unsigned int pending_bits
;
49 /* An lzw_buf_t is a simple, growable chunk of memory for holding
50 * variable-size objects of up to 16 bits each.
52 * Initialize an lzw_buf_t to the given size in bytes.
54 * To store objects into the lzw_buf_t, call _lzw_buf_store_bits and
55 * when finished, call _lzw_buf_store_pending, (which flushes out the
56 * last few bits that hadn't yet made a complete byte yet).
58 * Instead of returning failure from any functions, lzw_buf_t provides
59 * a status value that the caller can query, (and should query at
60 * least once when done with the object). The status value will be
61 * either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY;
64 _lzw_buf_init (lzw_buf_t
*buf
, int size
)
69 buf
->status
= CAIRO_STATUS_SUCCESS
;
70 buf
->data_size
= size
;
73 buf
->pending_bits
= 0;
75 buf
->data
= malloc (size
);
76 if (buf
->data
== NULL
) {
78 buf
->status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
83 /* Increase the buffer size by doubling.
85 * Returns %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
88 _lzw_buf_grow (lzw_buf_t
*buf
)
90 int new_size
= buf
->data_size
* 2;
91 unsigned char *new_data
;
97 /* check for integer overflow */
98 if (new_size
/ 2 == buf
->data_size
)
99 new_data
= realloc (buf
->data
, new_size
);
101 if (new_data
== NULL
) {
104 buf
->status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
108 buf
->data
= new_data
;
109 buf
->data_size
= new_size
;
111 return CAIRO_STATUS_SUCCESS
;
114 /* Store the lowest num_bits bits of values into buf.
116 * Note: The bits of value above size_in_bits must be 0, (so don't lie
119 * See also _lzw_buf_store_pending which must be called after the last
120 * call to _lzw_buf_store_bits.
122 * Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
125 _lzw_buf_store_bits (lzw_buf_t
*buf
, uint16_t value
, int num_bits
)
127 cairo_status_t status
;
129 assert (value
<= (1 << num_bits
) - 1);
134 buf
->pending
= (buf
->pending
<< num_bits
) | value
;
135 buf
->pending_bits
+= num_bits
;
137 while (buf
->pending_bits
>= 8) {
138 if (buf
->num_data
>= buf
->data_size
) {
139 status
= _lzw_buf_grow (buf
);
143 buf
->data
[buf
->num_data
++] = buf
->pending
>> (buf
->pending_bits
- 8);
144 buf
->pending_bits
-= 8;
148 /* Store the last remaining pending bits into the buffer.
150 * Note: This function must be called after the last call to
151 * _lzw_buf_store_bits.
153 * Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
156 _lzw_buf_store_pending (lzw_buf_t
*buf
)
158 cairo_status_t status
;
163 if (buf
->pending_bits
== 0)
166 assert (buf
->pending_bits
< 8);
168 if (buf
->num_data
>= buf
->data_size
) {
169 status
= _lzw_buf_grow (buf
);
174 buf
->data
[buf
->num_data
++] = buf
->pending
<< (8 - buf
->pending_bits
);
175 buf
->pending_bits
= 0;
178 /* LZW defines a few magic code values */
179 #define LZW_CODE_CLEAR_TABLE 256
180 #define LZW_CODE_EOD 257
181 #define LZW_CODE_FIRST 258
183 /* We pack three separate values into a symbol as follows:
185 * 12 bits (31 down to 20): CODE: code value used to represent this symbol
186 * 12 bits (19 down to 8): PREV: previous code value in chain
187 * 8 bits ( 7 down to 0): NEXT: next byte value in chain
189 typedef uint32_t lzw_symbol_t
;
191 #define LZW_SYMBOL_SET(sym, prev, next) ((sym) = ((prev) << 8)|(next))
192 #define LZW_SYMBOL_SET_CODE(sym, code, prev, next) ((sym) = ((code << 20)|(prev) << 8)|(next))
193 #define LZW_SYMBOL_GET_CODE(sym) (((sym) >> 20))
194 #define LZW_SYMBOL_GET_PREV(sym) (((sym) >> 8) & 0x7ff)
195 #define LZW_SYMBOL_GET_BYTE(sym) (((sym) >> 0) & 0x0ff)
197 /* The PREV+NEXT fields can be seen as the key used to fetch values
198 * from the hash table, while the code is the value fetched.
200 #define LZW_SYMBOL_KEY_MASK 0x000fffff
202 /* Since code values are only stored starting with 258 we can safely
203 * use a zero value to represent free slots in the hash table. */
204 #define LZW_SYMBOL_FREE 0x00000000
206 /* These really aren't very free for modifying. First, the PostScript
207 * specification sets the 9-12 bit range. Second, the encoding of
208 * lzw_symbol_t above also relies on 2 of LZW_BITS_MAX plus one byte
209 * fitting within 32 bits.
211 * But other than that, the LZW compression scheme could function with
212 * more bits per code.
214 #define LZW_BITS_MIN 9
215 #define LZW_BITS_MAX 12
216 #define LZW_BITS_BOUNDARY(bits) ((1<<(bits))-1)
217 #define LZW_MAX_SYMBOLS (1<<LZW_BITS_MAX)
219 #define LZW_SYMBOL_TABLE_SIZE 9013
220 #define LZW_SYMBOL_MOD1 LZW_SYMBOL_TABLE_SIZE
221 #define LZW_SYMBOL_MOD2 9011
223 typedef struct _lzw_symbol_table
{
224 lzw_symbol_t table
[LZW_SYMBOL_TABLE_SIZE
];
225 } lzw_symbol_table_t
;
227 /* Initialize the hash table to entirely empty */
229 _lzw_symbol_table_init (lzw_symbol_table_t
*table
)
231 memset (table
->table
, 0, LZW_SYMBOL_TABLE_SIZE
* sizeof (lzw_symbol_t
));
234 /* Lookup a symbol in the symbol table. The PREV and NEXT fields of
235 * symbol form the key for the lookup.
237 * If successful, then this function returns %TRUE and slot_ret will be
238 * left pointing at the result that will have the CODE field of
241 * If the lookup fails, then this function returns %FALSE and slot_ret
242 * will be pointing at the location in the table to which a new CODE
243 * value should be stored along with PREV and NEXT.
246 _lzw_symbol_table_lookup (lzw_symbol_table_t
*table
,
248 lzw_symbol_t
**slot_ret
)
250 /* The algorithm here is identical to that in cairo-hash.c. We
251 * copy it here to allow for a rather more efficient
252 * implementation due to several circumstances that do not apply
253 * to the more general case:
255 * 1) We have a known bound on the total number of symbols, so we
256 * have a fixed-size table without any copying when growing
258 * 2) We never delete any entries, so we don't need to
259 * support/check for DEAD entries during lookup.
261 * 3) The object fits in 32 bits so we store each object in its
262 * entirety within the table rather than storing objects
263 * externally and putting pointers in the table, (which here
264 * would just double the storage requirements and have negative
265 * impacts on memory locality).
267 int i
, idx
, step
, hash
= symbol
& LZW_SYMBOL_KEY_MASK
;
268 lzw_symbol_t candidate
;
270 idx
= hash
% LZW_SYMBOL_MOD1
;
274 for (i
= 0; i
< LZW_SYMBOL_TABLE_SIZE
; i
++)
276 candidate
= table
->table
[idx
];
277 if (candidate
== LZW_SYMBOL_FREE
)
279 *slot_ret
= &table
->table
[idx
];
282 else /* candidate is LIVE */
284 if ((candidate
& LZW_SYMBOL_KEY_MASK
) ==
285 (symbol
& LZW_SYMBOL_KEY_MASK
))
287 *slot_ret
= &table
->table
[idx
];
293 step
= hash
% LZW_SYMBOL_MOD2
;
299 if (idx
>= LZW_SYMBOL_TABLE_SIZE
)
300 idx
-= LZW_SYMBOL_TABLE_SIZE
;
306 /* Compress a bytestream using the LZW algorithm.
308 * This is an original implementation based on reading the
309 * specification of the LZWDecode filter in the PostScript Language
310 * Reference. The free parameters in the LZW algorithm are set to the
311 * values mandated by PostScript, (symbols encoded with widths from 9
314 * This function returns a pointer to a newly allocated buffer holding
315 * the compressed data, or %NULL if an out-of-memory situation
318 * Notice that any one of the _lzw_buf functions called here could
319 * trigger an out-of-memory condition. But lzw_buf_t uses cairo's
320 * shutdown-on-error idiom, so it's safe to continue to call into
321 * lzw_buf without having to check for errors, (until a final check at
325 _cairo_lzw_compress (unsigned char *data
, unsigned long *size_in_out
)
327 int bytes_remaining
= *size_in_out
;
329 lzw_symbol_table_t table
;
330 lzw_symbol_t symbol
, *slot
= NULL
; /* just to squelch a warning */
331 int code_next
= LZW_CODE_FIRST
;
332 int code_bits
= LZW_BITS_MIN
;
333 int prev
, next
= 0; /* just to squelch a warning */
335 if (*size_in_out
== 0)
338 _lzw_buf_init (&buf
, *size_in_out
);
340 _lzw_symbol_table_init (&table
);
342 /* The LZW header is a clear table code. */
343 _lzw_buf_store_bits (&buf
, LZW_CODE_CLEAR_TABLE
, code_bits
);
347 /* Find the longest existing code in the symbol table that
348 * matches the current input, if any. */
351 if (bytes_remaining
) {
356 LZW_SYMBOL_SET (symbol
, prev
, next
);
357 if (_lzw_symbol_table_lookup (&table
, symbol
, &slot
))
358 prev
= LZW_SYMBOL_GET_CODE (*slot
);
359 } while (bytes_remaining
&& *slot
!= LZW_SYMBOL_FREE
);
360 if (*slot
== LZW_SYMBOL_FREE
) {
366 /* Write the code into the output. This is either a byte read
367 * directly from the input, or a code from the last successful
369 _lzw_buf_store_bits (&buf
, prev
, code_bits
);
371 if (bytes_remaining
== 0)
374 LZW_SYMBOL_SET_CODE (*slot
, code_next
++, prev
, next
);
376 if (code_next
> LZW_BITS_BOUNDARY(code_bits
))
379 if (code_bits
> LZW_BITS_MAX
) {
380 _lzw_symbol_table_init (&table
);
381 _lzw_buf_store_bits (&buf
, LZW_CODE_CLEAR_TABLE
, code_bits
- 1);
382 code_bits
= LZW_BITS_MIN
;
383 code_next
= LZW_CODE_FIRST
;
388 /* The LZW footer is an end-of-data code. */
389 _lzw_buf_store_bits (&buf
, LZW_CODE_EOD
, code_bits
);
391 _lzw_buf_store_pending (&buf
);
393 /* See if we ever ran out of memory while writing to buf. */
394 if (buf
.status
== CAIRO_STATUS_NO_MEMORY
) {
399 assert (buf
.status
== CAIRO_STATUS_SUCCESS
);
401 *size_in_out
= buf
.num_data
;