opengl: apply subpictures alpha
[vlc.git] / lib / picture.c
blobfc8b769b2a85e07f2e600efa0727e0cac50ff13e
1 /*****************************************************************************
2 * picture.c: libvlc API picture management
3 *****************************************************************************
4 * Copyright (C) 2018 VLC authors and VideoLAN
6 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc/libvlc.h>
28 #include <vlc/libvlc_picture.h>
30 #include <vlc_atomic.h>
31 #include <vlc_picture.h>
32 #include <vlc_block.h>
33 #include <vlc_fs.h>
35 #include "picture_internal.h"
37 struct libvlc_picture_t
39 vlc_atomic_rc_t rc;
40 libvlc_picture_type_t type;
41 block_t* converted;
42 video_format_t fmt;
43 libvlc_time_t time;
46 libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
47 libvlc_picture_type_t type,
48 unsigned int width, unsigned int height,
49 bool crop )
51 libvlc_picture_t *pic = malloc( sizeof( *pic ) );
52 if ( unlikely( pic == NULL ) )
53 return NULL;
54 vlc_atomic_rc_init( &pic->rc );
55 pic->type = type;
56 pic->time = MS_FROM_VLC_TICK( input->date );
57 vlc_fourcc_t format;
58 switch ( type )
60 case libvlc_picture_Argb:
61 format = VLC_CODEC_ARGB;
62 break;
63 case libvlc_picture_Jpg:
64 format = VLC_CODEC_JPEG;
65 break;
66 case libvlc_picture_Png:
67 format = VLC_CODEC_PNG;
68 break;
69 default:
70 vlc_assert_unreachable();
72 if ( picture_Export( p_obj, &pic->converted, &pic->fmt,
73 input, format, width, height, crop ) != VLC_SUCCESS )
75 free( pic );
76 return NULL;
79 return pic;
82 void libvlc_picture_retain( libvlc_picture_t* pic )
84 vlc_atomic_rc_inc( &pic->rc );
87 void libvlc_picture_release( libvlc_picture_t* pic )
89 if ( vlc_atomic_rc_dec( &pic->rc ) == false )
90 return;
91 video_format_Clean( &pic->fmt );
92 if ( pic->converted )
93 block_Release( pic->converted );
94 free( pic );
97 int libvlc_picture_save( const libvlc_picture_t* pic, const char* path )
99 FILE* file = vlc_fopen( path, "wb" );
100 if ( !file )
101 return -1;
102 size_t res = fwrite( pic->converted->p_buffer,
103 pic->converted->i_buffer, 1, file );
104 fclose( file );
105 return res == 1 ? 0 : -1;
108 const unsigned char* libvlc_picture_get_buffer( const libvlc_picture_t* pic,
109 size_t *size )
111 assert( size != NULL );
112 *size = pic->converted->i_buffer;
113 return pic->converted->p_buffer;
116 libvlc_picture_type_t libvlc_picture_type( const libvlc_picture_t* pic )
118 return pic->type;
121 unsigned int libvlc_picture_get_stride( const libvlc_picture_t *pic )
123 assert( pic->type == libvlc_picture_Argb );
124 return pic->fmt.i_width * pic->fmt.i_bits_per_pixel / 8;
127 unsigned int libvlc_picture_get_width( const libvlc_picture_t* pic )
129 return pic->fmt.i_visible_width;
132 unsigned int libvlc_picture_get_height( const libvlc_picture_t* pic )
134 return pic->fmt.i_visible_height;
137 libvlc_time_t libvlc_picture_get_time( const libvlc_picture_t* pic )
139 return pic->time;