summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/gdk-pixbuf-buffer-queue.c
blob: 1c4f007f904f35d914e63196286c8ac05930c77f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* GdkPixbuf library
 * Copyright (C) 2003-2006 David Schleef <ds@schleef.org>
 *		 2005-2006 Eric Anholt <eric@anholt.net>
 *		 2006-2007 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gdk-pixbuf-buffer-queue-private.h"

#include <string.h>

struct _GdkPixbufBufferQueue
{
  GSList *	first_buffer;		/* pointer to first buffer */
  GSList *	last_buffer;		/* pointer to last buffer (for fast appending) */
  gsize		size;			/* amount of bytes in the queue */
  gsize		offset;			/* amount of data already flushed out of the queue */
  
  int		ref_count;
};

/**
 * GdkPixbufBufferQueue:
 *
 * A #GdkPixbufBufferQueue is a queue of continuous buffers that allows reading
 * its data in chunks of pre-defined sizes. It is used to transform a data 
 * stream that was provided by buffers of random sizes to buffers of the right
 * size.
 */

/**
 * gdk_pixbuf_buffer_queue_new:
 *
 * Creates a new empty buffer queue.
 *
 * Returns: a new buffer queue. Use gdk_pixbuf_buffer_queue_unref () to free it.
 **/
GdkPixbufBufferQueue *
gdk_pixbuf_buffer_queue_new (void)
{
  GdkPixbufBufferQueue *buffer_queue;

  buffer_queue = g_new0 (GdkPixbufBufferQueue, 1);
  buffer_queue->ref_count = 1;

  return buffer_queue;
}

/**
 * gdk_pixbuf_buffer_queue_get_size:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Returns the number of bytes currently in @queue.
 *
 * Returns: amount of bytes in @queue.
 **/
gsize
gdk_pixbuf_buffer_queue_get_size (GdkPixbufBufferQueue *queue)
{
  g_return_val_if_fail (queue != NULL, 0);

  return queue->size;
}

/**
 * gdk_pixbuf_buffer_queue_get_offset:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Queries the amount of bytes that has already been pulled out of
 * @queue using functions like gdk_pixbuf_buffer_queue_pull().
 *
 * Returns: Number of bytes that were already pulled from this queue.
 **/
gsize
gdk_pixbuf_buffer_queue_get_offset (GdkPixbufBufferQueue * queue)
{
  g_return_val_if_fail (queue != NULL, 0);

  return queue->offset;
}

/**
 * gdk_pixbuf_buffer_queue_flush:
 * @queue: a #GdkPixbufBufferQueue
 * @n_bytes: amount of bytes to flush from the queue
 *
 * Removes the first @n_bytes bytes from the queue.
 */
void
gdk_pixbuf_buffer_queue_flush (GdkPixbufBufferQueue *queue, gsize n_bytes)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (n_bytes <= queue->size);

  queue->size -= n_bytes;
  queue->offset += n_bytes;

  while (n_bytes > 0)
    {
      GBytes *bytes;
      gsize size;
      
      bytes = queue->first_buffer->data;
      size = g_bytes_get_size (bytes);

      if (size <= n_bytes)
        {
          n_bytes -= size;
          queue->first_buffer = g_slist_remove (queue->first_buffer, bytes);
        }
      else
        {
          queue->first_buffer->data = g_bytes_new_from_bytes (bytes,
	                                                      n_bytes,
                                                              size - n_bytes);
          n_bytes = 0;
        }
      g_bytes_unref (bytes);
    }

  if (queue->first_buffer == NULL)
    queue->last_buffer = NULL;
}

/**
 * gdk_pixbuf_buffer_queue_clear:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Resets @queue into to initial state. All buffers it contains will be 
 * released and the offset will be reset to 0.
 **/
void
gdk_pixbuf_buffer_queue_clear (GdkPixbufBufferQueue *queue)
{
  g_return_if_fail (queue != NULL);

  g_slist_free_full (queue->first_buffer, (GDestroyNotify) g_bytes_unref);
  queue->first_buffer = NULL;
  queue->last_buffer = NULL;
  queue->size = 0;
  queue->offset = 0;
}

/**
 * gdk_pixbuf_buffer_queue_push:
 * @queue: a #GdkPixbufBufferQueue
 * @bytes: #GBytes to append to @queue
 *
 * Appends the given @bytes to the buffers already in @queue. This function
 * will take ownership of the given @buffer. Use g_bytes_ref () before
 * calling this function to keep a reference.
 **/
void
gdk_pixbuf_buffer_queue_push (GdkPixbufBufferQueue *queue,
                              GBytes               *bytes)
{
  gsize size;

  g_return_if_fail (queue != NULL);
  g_return_if_fail (bytes != NULL);

  size = g_bytes_get_size (bytes);
  if (size == 0)
    {
      g_bytes_unref (bytes);
      return;
    }

  queue->last_buffer = g_slist_append (queue->last_buffer, bytes);
  if (queue->first_buffer == NULL)
    queue->first_buffer = queue->last_buffer;
  else
    queue->last_buffer = queue->last_buffer->next;

  queue->size += size;
}

/**
 * gdk_pixbuf_buffer_queue_peek:
 * @queue: a #GdkPixbufBufferQueue to read from
 * @length: amount of bytes to peek
 *
 * Creates a new buffer with the first @length bytes from @queue, but unlike 
 * gdk_pixbuf_buffer_queue_pull(), does not remove them from @queue.
 *
 * Returns: NULL if the requested amount of data wasn't available or a new 
 *          #GBytes. Use g_bytes_unref() after use.
 **/
GBytes *
gdk_pixbuf_buffer_queue_peek (GdkPixbufBufferQueue *queue,
                              gsize                 length)
{
  GSList *g;
  GBytes *result, *bytes;

  g_return_val_if_fail (queue != NULL, NULL);

  if (queue->size < length)
    return NULL;

  /* need to special case here, because the queue may be empty */
  if (length == 0)
    return g_bytes_new (NULL, 0);

  g = queue->first_buffer;
  bytes = g->data;
  if (g_bytes_get_size (bytes) == length)
    {
      result = g_bytes_ref (bytes);
    }
  else if (g_bytes_get_size (bytes) > length)
    {
      result = g_bytes_new_from_bytes (bytes, 0, length);
    }
  else
    {
      guchar *data;
      gsize amount, offset;

      data = g_malloc (length);
      
      for (offset = 0; offset < length; offset += amount)
        {
          bytes = g->data;
          amount = MIN (length - offset, g_bytes_get_size (bytes));
          memcpy (data + offset, g_bytes_get_data (bytes, NULL), amount);
          g = g->next;
        }

      result = g_bytes_new_take (data, length);
    }

  return result;
}

/**
 * gdk_pixbuf_buffer_queue_pull:
 * @queue: a #GdkPixbufBufferQueue
 * @length: amount of bytes to pull
 *
 * If enough data is still available in @queue, the first @length bytes are 
 * put into a new buffer and that buffer is returned. The @length bytes are
 * removed from the head of the queue. If not enough data is available, %NULL
 * is returned.
 *
 * Returns: a new #GBytes or %NULL
 **/
GBytes *
gdk_pixbuf_buffer_queue_pull (GdkPixbufBufferQueue * queue, gsize length)
{
  GBytes *result;

  g_return_val_if_fail (queue != NULL, NULL);

  result = gdk_pixbuf_buffer_queue_peek (queue, length);
  if (result == NULL)
    return NULL;

  gdk_pixbuf_buffer_queue_flush (queue, length);

  return result;
}

/**
 * gdk_pixbuf_buffer_queue_peek_buffer:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Gets the first buffer out of @queue and returns it. This function is 
 * equivalent to calling gdk_pixbuf_buffer_queue_peek() with the size of the
 * first buffer in it.
 *
 * Returns: The first buffer in @queue or %NULL if @queue is empty. Use 
 *          g_bytes_unref() after use.
 **/
GBytes *
gdk_pixbuf_buffer_queue_peek_buffer (GdkPixbufBufferQueue * queue)
{
  GBytes *bytes;

  g_return_val_if_fail (queue != NULL, NULL);

  if (queue->first_buffer == NULL)
    return NULL;

  bytes = queue->first_buffer->data;

  return g_bytes_ref (bytes);
}

/**
 * gdk_pixbuf_buffer_queue_pull_buffer:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Pulls the first buffer out of @queue and returns it. This function is 
 * equivalent to calling gdk_pixbuf_buffer_queue_pull() with the size of the
 * first buffer in it.
 *
 * Returns: The first buffer in @queue or %NULL if @queue is empty.
 **/
GBytes *
gdk_pixbuf_buffer_queue_pull_buffer (GdkPixbufBufferQueue *queue)
{
  GBytes *bytes;

  g_return_val_if_fail (queue != NULL, NULL);

  bytes = gdk_pixbuf_buffer_queue_peek_buffer (queue);
  if (bytes)
    gdk_pixbuf_buffer_queue_flush (queue, g_bytes_get_size (bytes));

  return bytes;
}

/**
 * gdk_pixbuf_buffer_queue_ref:
 * @queue: a #GdkPixbufBufferQueue
 *
 * increases the reference count of @queue by one.
 *
 * Returns: The passed in @queue.
 **/
GdkPixbufBufferQueue *
gdk_pixbuf_buffer_queue_ref (GdkPixbufBufferQueue * queue)
{
  g_return_val_if_fail (queue != NULL, NULL);
  g_return_val_if_fail (queue->ref_count > 0, NULL);

  queue->ref_count++;
  return queue;
}

/**
 * gdk_pixbuf_buffer_queue_unref:
 * @queue: a #GdkPixbufBufferQueue
 *
 * Decreases the reference count of @queue by one. If no reference 
 * to this buffer exists anymore, the buffer and the memory 
 * it manages are freed.
 **/
void
gdk_pixbuf_buffer_queue_unref (GdkPixbufBufferQueue * queue)
{
  g_return_if_fail (queue != NULL);
  g_return_if_fail (queue->ref_count > 0);

  queue->ref_count--;
  if (queue->ref_count > 0)
    return;

  gdk_pixbuf_buffer_queue_clear (queue);
  g_free (queue);
}