Add stubs for Direct3D9 backend.
[cairo/gpu.git] / src / cairo-deflate-stream.c
blob863189f456535949a560456801995c86aff13ff5
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2006 Adrian Johnson
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Adrian Johnson.
33 * Author(s):
34 * Adrian Johnson <ajohnson@redneon.com>
37 #include "cairoint.h"
38 #include "cairo-output-stream-private.h"
39 #include <zlib.h>
41 #define BUFFER_SIZE 16384
43 typedef struct _cairo_deflate_stream {
44 cairo_output_stream_t base;
45 cairo_output_stream_t *output;
46 z_stream zlib_stream;
47 unsigned char input_buf[BUFFER_SIZE];
48 unsigned char output_buf[BUFFER_SIZE];
49 } cairo_deflate_stream_t;
51 static void
52 cairo_deflate_stream_deflate (cairo_deflate_stream_t *stream, cairo_bool_t flush)
54 int ret;
55 cairo_bool_t finished;
57 do {
58 ret = deflate (&stream->zlib_stream, flush ? Z_FINISH : Z_NO_FLUSH);
59 if (flush || stream->zlib_stream.avail_out == 0)
61 _cairo_output_stream_write (stream->output,
62 stream->output_buf,
63 BUFFER_SIZE - stream->zlib_stream.avail_out);
64 stream->zlib_stream.next_out = stream->output_buf;
65 stream->zlib_stream.avail_out = BUFFER_SIZE;
68 finished = TRUE;
69 if (stream->zlib_stream.avail_in != 0)
70 finished = FALSE;
71 if (flush && ret != Z_STREAM_END)
72 finished = FALSE;
74 } while (!finished);
76 stream->zlib_stream.next_in = stream->input_buf;
79 static cairo_status_t
80 _cairo_deflate_stream_write (cairo_output_stream_t *base,
81 const unsigned char *data,
82 unsigned int length)
84 cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base;
85 unsigned int count;
86 const unsigned char *p = data;
88 while (length) {
89 count = length;
90 if (count > BUFFER_SIZE - stream->zlib_stream.avail_in)
91 count = BUFFER_SIZE - stream->zlib_stream.avail_in;
92 memcpy (stream->input_buf + stream->zlib_stream.avail_in, p, count);
93 p += count;
94 stream->zlib_stream.avail_in += count;
95 length -= count;
97 if (stream->zlib_stream.avail_in == BUFFER_SIZE)
98 cairo_deflate_stream_deflate (stream, FALSE);
101 return _cairo_output_stream_get_status (stream->output);
104 static cairo_status_t
105 _cairo_deflate_stream_close (cairo_output_stream_t *base)
107 cairo_deflate_stream_t *stream = (cairo_deflate_stream_t *) base;
109 cairo_deflate_stream_deflate (stream, TRUE);
110 deflateEnd (&stream->zlib_stream);
112 return _cairo_output_stream_get_status (stream->output);
115 cairo_output_stream_t *
116 _cairo_deflate_stream_create (cairo_output_stream_t *output)
118 cairo_deflate_stream_t *stream;
120 if (output->status)
121 return _cairo_output_stream_create_in_error (output->status);
123 stream = malloc (sizeof (cairo_deflate_stream_t));
124 if (unlikely (stream == NULL)) {
125 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
126 return (cairo_output_stream_t *) &_cairo_output_stream_nil;
129 _cairo_output_stream_init (&stream->base,
130 _cairo_deflate_stream_write,
131 NULL,
132 _cairo_deflate_stream_close);
133 stream->output = output;
135 stream->zlib_stream.zalloc = Z_NULL;
136 stream->zlib_stream.zfree = Z_NULL;
137 stream->zlib_stream.opaque = Z_NULL;
139 if (deflateInit (&stream->zlib_stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
140 free (stream);
141 return (cairo_output_stream_t *) &_cairo_output_stream_nil;
144 stream->zlib_stream.next_in = stream->input_buf;
145 stream->zlib_stream.avail_in = 0;
146 stream->zlib_stream.next_out = stream->output_buf;
147 stream->zlib_stream.avail_out = BUFFER_SIZE;
149 return &stream->base;