beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-lzw.c
blobde7f999837635673933c7731938d5b4b56654320
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, 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
31 * California.
33 * Contributor(s):
34 * Carl D. Worth <cworth@cworth.org>
37 #include "cairoint.h"
38 #include "cairo-error-private.h"
40 typedef struct _lzw_buf {
41 cairo_status_t status;
43 unsigned char *data;
44 int data_size;
45 int num_data;
46 uint32_t pending;
47 unsigned int pending_bits;
48 } lzw_buf_t;
50 /* An lzw_buf_t is a simple, growable chunk of memory for holding
51 * variable-size objects of up to 16 bits each.
53 * Initialize an lzw_buf_t to the given size in bytes.
55 * To store objects into the lzw_buf_t, call _lzw_buf_store_bits and
56 * when finished, call _lzw_buf_store_pending, (which flushes out the
57 * last few bits that hadn't yet made a complete byte yet).
59 * Instead of returning failure from any functions, lzw_buf_t provides
60 * a status value that the caller can query, (and should query at
61 * least once when done with the object). The status value will be
62 * either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY;
64 static void
65 _lzw_buf_init (lzw_buf_t *buf, int size)
67 if (size == 0)
68 size = 16;
70 buf->status = CAIRO_STATUS_SUCCESS;
71 buf->data_size = size;
72 buf->num_data = 0;
73 buf->pending = 0;
74 buf->pending_bits = 0;
76 buf->data = malloc (size);
77 if (unlikely (buf->data == NULL)) {
78 buf->data_size = 0;
79 buf->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
80 return;
84 /* Increase the buffer size by doubling.
86 * Returns %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
88 static cairo_status_t
89 _lzw_buf_grow (lzw_buf_t *buf)
91 int new_size = buf->data_size * 2;
92 unsigned char *new_data;
94 if (buf->status)
95 return buf->status;
97 new_data = NULL;
98 /* check for integer overflow */
99 if (new_size / 2 == buf->data_size)
100 new_data = realloc (buf->data, new_size);
102 if (unlikely (new_data == NULL)) {
103 free (buf->data);
104 buf->data_size = 0;
105 buf->status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
106 return buf->status;
109 buf->data = new_data;
110 buf->data_size = new_size;
112 return CAIRO_STATUS_SUCCESS;
115 /* Store the lowest num_bits bits of values into buf.
117 * Note: The bits of value above size_in_bits must be 0, (so don't lie
118 * about the size).
120 * See also _lzw_buf_store_pending which must be called after the last
121 * call to _lzw_buf_store_bits.
123 * Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
125 static void
126 _lzw_buf_store_bits (lzw_buf_t *buf, uint16_t value, int num_bits)
128 cairo_status_t status;
130 assert (value <= (1 << num_bits) - 1);
132 if (buf->status)
133 return;
135 buf->pending = (buf->pending << num_bits) | value;
136 buf->pending_bits += num_bits;
138 while (buf->pending_bits >= 8) {
139 if (buf->num_data >= buf->data_size) {
140 status = _lzw_buf_grow (buf);
141 if (unlikely (status))
142 return;
144 buf->data[buf->num_data++] = buf->pending >> (buf->pending_bits - 8);
145 buf->pending_bits -= 8;
149 /* Store the last remaining pending bits into the buffer.
151 * Note: This function must be called after the last call to
152 * _lzw_buf_store_bits.
154 * Sets buf->status to either %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY.
156 static void
157 _lzw_buf_store_pending (lzw_buf_t *buf)
159 cairo_status_t status;
161 if (buf->status)
162 return;
164 if (buf->pending_bits == 0)
165 return;
167 assert (buf->pending_bits < 8);
169 if (buf->num_data >= buf->data_size) {
170 status = _lzw_buf_grow (buf);
171 if (unlikely (status))
172 return;
175 buf->data[buf->num_data++] = buf->pending << (8 - buf->pending_bits);
176 buf->pending_bits = 0;
179 /* LZW defines a few magic code values */
180 #define LZW_CODE_CLEAR_TABLE 256
181 #define LZW_CODE_EOD 257
182 #define LZW_CODE_FIRST 258
184 /* We pack three separate values into a symbol as follows:
186 * 12 bits (31 down to 20): CODE: code value used to represent this symbol
187 * 12 bits (19 down to 8): PREV: previous code value in chain
188 * 8 bits ( 7 down to 0): NEXT: next byte value in chain
190 typedef uint32_t lzw_symbol_t;
192 #define LZW_SYMBOL_SET(sym, prev, next) ((sym) = ((prev) << 8)|(next))
193 #define LZW_SYMBOL_SET_CODE(sym, code, prev, next) ((sym) = ((code << 20)|(prev) << 8)|(next))
194 #define LZW_SYMBOL_GET_CODE(sym) (((sym) >> 20))
195 #define LZW_SYMBOL_GET_PREV(sym) (((sym) >> 8) & 0x7ff)
196 #define LZW_SYMBOL_GET_BYTE(sym) (((sym) >> 0) & 0x0ff)
198 /* The PREV+NEXT fields can be seen as the key used to fetch values
199 * from the hash table, while the code is the value fetched.
201 #define LZW_SYMBOL_KEY_MASK 0x000fffff
203 /* Since code values are only stored starting with 258 we can safely
204 * use a zero value to represent free slots in the hash table. */
205 #define LZW_SYMBOL_FREE 0x00000000
207 /* These really aren't very free for modifying. First, the PostScript
208 * specification sets the 9-12 bit range. Second, the encoding of
209 * lzw_symbol_t above also relies on 2 of LZW_BITS_MAX plus one byte
210 * fitting within 32 bits.
212 * But other than that, the LZW compression scheme could function with
213 * more bits per code.
215 #define LZW_BITS_MIN 9
216 #define LZW_BITS_MAX 12
217 #define LZW_BITS_BOUNDARY(bits) ((1<<(bits))-1)
218 #define LZW_MAX_SYMBOLS (1<<LZW_BITS_MAX)
220 #define LZW_SYMBOL_TABLE_SIZE 9013
221 #define LZW_SYMBOL_MOD1 LZW_SYMBOL_TABLE_SIZE
222 #define LZW_SYMBOL_MOD2 9011
224 typedef struct _lzw_symbol_table {
225 lzw_symbol_t table[LZW_SYMBOL_TABLE_SIZE];
226 } lzw_symbol_table_t;
228 /* Initialize the hash table to entirely empty */
229 static void
230 _lzw_symbol_table_init (lzw_symbol_table_t *table)
232 memset (table->table, 0, LZW_SYMBOL_TABLE_SIZE * sizeof (lzw_symbol_t));
235 /* Lookup a symbol in the symbol table. The PREV and NEXT fields of
236 * symbol form the key for the lookup.
238 * If successful, then this function returns %TRUE and slot_ret will be
239 * left pointing at the result that will have the CODE field of
240 * interest.
242 * If the lookup fails, then this function returns %FALSE and slot_ret
243 * will be pointing at the location in the table to which a new CODE
244 * value should be stored along with PREV and NEXT.
246 static cairo_bool_t
247 _lzw_symbol_table_lookup (lzw_symbol_table_t *table,
248 lzw_symbol_t symbol,
249 lzw_symbol_t **slot_ret)
251 /* The algorithm here is identical to that in cairo-hash.c. We
252 * copy it here to allow for a rather more efficient
253 * implementation due to several circumstances that do not apply
254 * to the more general case:
256 * 1) We have a known bound on the total number of symbols, so we
257 * have a fixed-size table without any copying when growing
259 * 2) We never delete any entries, so we don't need to
260 * support/check for DEAD entries during lookup.
262 * 3) The object fits in 32 bits so we store each object in its
263 * entirety within the table rather than storing objects
264 * externally and putting pointers in the table, (which here
265 * would just double the storage requirements and have negative
266 * impacts on memory locality).
268 int i, idx, step, hash = symbol & LZW_SYMBOL_KEY_MASK;
269 lzw_symbol_t candidate;
271 idx = hash % LZW_SYMBOL_MOD1;
272 step = 0;
274 *slot_ret = NULL;
275 for (i = 0; i < LZW_SYMBOL_TABLE_SIZE; i++)
277 candidate = table->table[idx];
278 if (candidate == LZW_SYMBOL_FREE)
280 *slot_ret = &table->table[idx];
281 return FALSE;
283 else /* candidate is LIVE */
285 if ((candidate & LZW_SYMBOL_KEY_MASK) ==
286 (symbol & LZW_SYMBOL_KEY_MASK))
288 *slot_ret = &table->table[idx];
289 return TRUE;
293 if (step == 0) {
294 step = hash % LZW_SYMBOL_MOD2;
295 if (step == 0)
296 step = 1;
299 idx += step;
300 if (idx >= LZW_SYMBOL_TABLE_SIZE)
301 idx -= LZW_SYMBOL_TABLE_SIZE;
304 return FALSE;
307 /* Compress a bytestream using the LZW algorithm.
309 * This is an original implementation based on reading the
310 * specification of the LZWDecode filter in the PostScript Language
311 * Reference. The free parameters in the LZW algorithm are set to the
312 * values mandated by PostScript, (symbols encoded with widths from 9
313 * to 12 bits).
315 * This function returns a pointer to a newly allocated buffer holding
316 * the compressed data, or %NULL if an out-of-memory situation
317 * occurs.
319 * Notice that any one of the _lzw_buf functions called here could
320 * trigger an out-of-memory condition. But lzw_buf_t uses cairo's
321 * shutdown-on-error idiom, so it's safe to continue to call into
322 * lzw_buf without having to check for errors, (until a final check at
323 * the end).
325 unsigned char *
326 _cairo_lzw_compress (unsigned char *data, unsigned long *size_in_out)
328 int bytes_remaining = *size_in_out;
329 lzw_buf_t buf;
330 lzw_symbol_table_t table;
331 lzw_symbol_t symbol, *slot = NULL; /* just to squelch a warning */
332 int code_next = LZW_CODE_FIRST;
333 int code_bits = LZW_BITS_MIN;
334 int prev, next = 0; /* just to squelch a warning */
336 if (*size_in_out == 0)
337 return NULL;
339 _lzw_buf_init (&buf, *size_in_out);
341 _lzw_symbol_table_init (&table);
343 /* The LZW header is a clear table code. */
344 _lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits);
346 while (1) {
348 /* Find the longest existing code in the symbol table that
349 * matches the current input, if any. */
350 prev = *data++;
351 bytes_remaining--;
352 if (bytes_remaining) {
355 next = *data++;
356 bytes_remaining--;
357 LZW_SYMBOL_SET (symbol, prev, next);
358 if (_lzw_symbol_table_lookup (&table, symbol, &slot))
359 prev = LZW_SYMBOL_GET_CODE (*slot);
360 } while (bytes_remaining && *slot != LZW_SYMBOL_FREE);
361 if (*slot == LZW_SYMBOL_FREE) {
362 data--;
363 bytes_remaining++;
367 /* Write the code into the output. This is either a byte read
368 * directly from the input, or a code from the last successful
369 * lookup. */
370 _lzw_buf_store_bits (&buf, prev, code_bits);
372 if (bytes_remaining == 0)
373 break;
375 LZW_SYMBOL_SET_CODE (*slot, code_next++, prev, next);
377 if (code_next > LZW_BITS_BOUNDARY(code_bits))
379 code_bits++;
380 if (code_bits > LZW_BITS_MAX) {
381 _lzw_symbol_table_init (&table);
382 _lzw_buf_store_bits (&buf, LZW_CODE_CLEAR_TABLE, code_bits - 1);
383 code_bits = LZW_BITS_MIN;
384 code_next = LZW_CODE_FIRST;
389 /* The LZW footer is an end-of-data code. */
390 _lzw_buf_store_bits (&buf, LZW_CODE_EOD, code_bits);
392 _lzw_buf_store_pending (&buf);
394 /* See if we ever ran out of memory while writing to buf. */
395 if (buf.status == CAIRO_STATUS_NO_MEMORY) {
396 *size_in_out = 0;
397 return NULL;
400 assert (buf.status == CAIRO_STATUS_SUCCESS);
402 *size_in_out = buf.num_data;
403 return buf.data;