qt: playlist: use item title if available
[vlc.git] / modules / codec / xwd.c
blobf75c1d513b284d1124214c7ff8ba423dd19e6558
1 /*****************************************************************************
2 * xwd.c: X Window system raster image dump pseudo-decoder
3 *****************************************************************************
4 * Copyright (C) 2012 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <assert.h>
26 #ifdef HAVE_ARPA_INET_H
27 #include <arpa/inet.h>
28 #endif
29 #include <X11/XWDFile.h>
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 static int Decode (decoder_t *dec, block_t *block)
37 picture_t *pic = NULL;
39 if (block == NULL) /* No Drain */
40 return VLCDEC_SUCCESS;
42 if (block->i_pts == VLC_TICK_INVALID)
43 goto drop; /* undated block, should never happen */
44 if (block->i_buffer < sz_XWDheader)
45 goto drop;
47 /* Skip XWD header */
48 const XWDFileHeader *hdr = (const void *)block->p_buffer;
49 uint32_t hdrlen = ntohl(hdr->header_size);
50 if (hdrlen < sz_XWDheader
51 || ntohl(hdr->file_version) < XWD_FILE_VERSION
52 || ntohl(hdr->pixmap_format) != 2 /* ZPixmap */)
53 goto drop;
55 hdrlen += ntohl(hdr->ncolors) * sz_XWDColor;
56 if (hdrlen > block->i_buffer)
57 goto drop;
58 block->p_buffer += hdrlen;
59 block->i_buffer -= hdrlen;
61 /* Parse XWD header */
62 vlc_fourcc_t chroma = 0;
63 switch (ntohl(hdr->pixmap_depth))
65 case 8:
66 if (ntohl(hdr->bits_per_pixel) == 8)
67 chroma = VLC_CODEC_RGB8;
68 break;
69 case 15:
70 if (ntohl(hdr->bits_per_pixel) == 16)
71 chroma = VLC_CODEC_RGB15;
72 break;
73 case 16:
74 if (ntohl(hdr->bits_per_pixel) == 16)
75 chroma = VLC_CODEC_RGB16;
76 break;
77 case 24:
78 switch (ntohl(hdr->bits_per_pixel))
80 case 32: chroma = VLC_CODEC_RGB32; break;
81 case 24: chroma = VLC_CODEC_RGB24; break;
83 break;
84 case 32:
85 if (ntohl(hdr->bits_per_pixel) == 32)
86 chroma = VLC_CODEC_ARGB;
87 break;
89 /* TODO: check image endianess, set RGB mask */
90 if (!chroma)
91 goto drop;
93 video_format_Setup(&dec->fmt_out.video, chroma,
94 ntohl(hdr->pixmap_width), ntohl(hdr->pixmap_height),
95 ntohl(hdr->pixmap_width), ntohl(hdr->pixmap_height),
96 dec->fmt_in.video.i_sar_num,
97 dec->fmt_in.video.i_sar_den);
99 const size_t copy = dec->fmt_out.video.i_width
100 * (dec->fmt_out.video.i_bits_per_pixel / 8);
101 const uint32_t pitch = ntohl(hdr->bytes_per_line);
103 /* Build picture */
104 if (pitch < copy
105 || (block->i_buffer / pitch) < dec->fmt_out.video.i_height)
106 goto drop;
108 if (decoder_UpdateVideoFormat(dec))
109 goto drop;
110 pic = decoder_NewPicture(dec);
111 if (pic == NULL)
112 goto drop;
114 const uint8_t *in = block->p_buffer;
115 uint8_t *out = pic->p->p_pixels;
116 for (unsigned i = 0; i < dec->fmt_out.video.i_height; i++)
118 memcpy(out, in, copy);
119 in += pitch;
120 out += pic->p->i_pitch;
122 pic->date = block->i_pts;
123 pic->b_progressive = true;
125 drop:
126 block_Release(block);
127 decoder_QueueVideo(dec, pic);
128 return VLCDEC_SUCCESS;
131 static int Open(vlc_object_t *obj)
133 decoder_t *dec = (decoder_t *)obj;
135 if (dec->fmt_in.i_codec != VLC_CODEC_XWD)
136 return VLC_EGENERIC;
138 dec->pf_decode = Decode;
139 es_format_Copy(&dec->fmt_out, &dec->fmt_in);
140 dec->fmt_out.i_codec = VLC_CODEC_RGB32;
141 return VLC_SUCCESS;
144 vlc_module_begin()
145 set_description(N_("XWD image decoder"))
146 set_capability("video decoder", 50)
147 set_category(CAT_INPUT)
148 set_subcategory(SUBCAT_INPUT_VCODEC)
149 set_callback(Open)
150 vlc_module_end()