copy/paste caused some issues here
[swfdec.git] / libswfdec / swfdec_codec_vp6_alpha.c
blob2d6ca9de6228d98681ad93d0dbccef537c7150a0
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <string.h>
25 #include <zlib.h>
26 #include <liboil/liboil.h>
28 #include "swfdec_codec_video.h"
29 #include "swfdec_bits.h"
30 #include "swfdec_debug.h"
31 #include "swfdec_internal.h"
33 typedef struct _SwfdecCodecVp6Alpha SwfdecCodecVp6Alpha;
35 struct _SwfdecCodecVp6Alpha {
36 SwfdecVideoDecoder decoder; /* the decoder */
37 SwfdecVideoDecoder * image; /* the image decoder */
38 SwfdecVideoDecoder * alpha; /* the alpha decoder */
41 static gboolean
42 swfdec_video_decoder_vp6_alpha_decode (SwfdecVideoDecoder *dec, SwfdecBuffer *buffer,
43 SwfdecVideoImage *image)
45 SwfdecCodecVp6Alpha *vp6 = (SwfdecCodecVp6Alpha *) dec;
46 SwfdecBuffer *tmp;
47 SwfdecVideoImage alpha;
48 SwfdecBits bits;
49 guint size;
51 swfdec_bits_init (&bits, buffer);
52 size = swfdec_bits_get_bu24 (&bits);
53 tmp = swfdec_bits_get_buffer (&bits, size);
54 if (tmp == NULL)
55 return FALSE;
56 if (!vp6->image->decode (vp6->image, tmp, image)) {
57 swfdec_buffer_unref (tmp);
58 return FALSE;
60 swfdec_buffer_unref (tmp);
61 tmp = swfdec_bits_get_buffer (&bits, -1);
62 if (tmp == NULL)
63 return FALSE;
64 if (!vp6->alpha->decode (vp6->alpha, tmp, &alpha)) {
65 swfdec_buffer_unref (tmp);
66 return FALSE;
68 swfdec_buffer_unref (tmp);
69 if (alpha.width != image->width || alpha.height != image->height) {
70 SWFDEC_ERROR ("size of mask doesn't match image: %ux%u vs %ux%u",
71 alpha.width, alpha.height, image->width, image->height);
72 return FALSE;
74 image->mask = alpha.plane[0];
75 image->mask_rowstride = alpha.rowstride[0];
76 return TRUE;
79 static void
80 swfdec_video_decoder_vp6_alpha_free (SwfdecVideoDecoder *dec)
82 SwfdecCodecVp6Alpha *vp6 = (SwfdecCodecVp6Alpha *) dec;
84 if (vp6->image)
85 swfdec_video_decoder_free (vp6->image);
86 if (vp6->alpha)
87 swfdec_video_decoder_free (vp6->alpha);
88 g_free (vp6);
91 SwfdecVideoDecoder *
92 swfdec_video_decoder_vp6_alpha_new (SwfdecVideoCodec type)
94 SwfdecCodecVp6Alpha *vp6;
96 if (type != SWFDEC_VIDEO_CODEC_VP6_ALPHA)
97 return NULL;
99 vp6 = g_new0 (SwfdecCodecVp6Alpha, 1);
100 vp6->decoder.decode = swfdec_video_decoder_vp6_alpha_decode;
101 vp6->decoder.free = swfdec_video_decoder_vp6_alpha_free;
102 vp6->image = swfdec_video_decoder_new (SWFDEC_VIDEO_CODEC_VP6);
103 vp6->alpha = swfdec_video_decoder_new (SWFDEC_VIDEO_CODEC_VP6);
104 if (vp6->alpha == NULL || vp6->image == NULL) {
105 swfdec_video_decoder_vp6_alpha_free (&vp6->decoder);
106 return NULL;
109 return &vp6->decoder;