magnify.c 13.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * magnify.c : Magnify/Zoom interactive effect
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2005-2009 VLC authors and VideoLAN
Antoine Cellerier's avatar
Antoine Cellerier committed
5
 * $Id$
6 7 8
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program 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.1 of the License, or
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
31
#include <math.h>
32
#include <assert.h>
33

34 35 36 37
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_image.h>
#include <vlc_filter.h>
38 39
#include "filter_picture.h"

40
/*****************************************************************************
41
 * Module descriptor
42 43 44 45
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

46 47 48
vlc_module_begin ()
    set_description( N_("Magnify/Zoom interactive video filter") )
    set_shortname( N_( "Magnify" ))
49
    set_capability( "video filter", 0 )
50 51
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
52

53 54
    set_callbacks( Create, Destroy )
vlc_module_end ()
55

56 57 58 59

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
60 61
static picture_t *Filter( filter_t *, picture_t * );
static int Mouse( filter_t *, vlc_mouse_t *, const vlc_mouse_t *, const vlc_mouse_t * );
62

63
/* */
64 65 66 67 68
static void DrawZoomStatus( uint8_t *, int i_pitch, int i_width, int i_height,
                            int i_offset_x, int i_offset_y, bool b_visible );
static void DrawRectangle( uint8_t *, int i_pitch, int i_width, int i_height,
                           int x, int y, int i_w, int i_h );

69 70
/* */
struct filter_sys_t
71 72 73
{
    image_handler_t *p_image;

74 75
    int64_t i_hide_timeout;

76 77 78
    int i_zoom; /* zoom level in percent */
    int i_x, i_y; /* top left corner coordinates in original image */

79
    bool b_visible; /* is "interface" visible ? */
80 81

    int64_t i_last_activity;
82 83
};

84 85 86
#define VIS_ZOOM 4
#define ZOOM_FACTOR 8

87
/*****************************************************************************
88
 * Create:
89 90 91
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
92 93
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;
94

95 96 97 98 99 100 101
    /* */
    switch( p_filter->fmt_in.i_codec )
    {
    CASE_PLANAR_YUV
    case VLC_CODEC_GREY:
        break;
    default:
102
        msg_Err( p_filter, "Unsupported chroma %4.4s", (char *)&p_filter->fmt_in.i_codec );
103 104
        return VLC_EGENERIC;
    }
105
    if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) )
106
    {
107 108
        msg_Err( p_filter, "Input and output format does not match" );
        return VLC_EGENERIC;
109 110
    }

111
    /* Allocate structure */
112 113
    p_filter->p_sys = p_sys = malloc( sizeof( *p_sys ) );
    if( !p_filter->p_sys )
114 115
        return VLC_ENOMEM;

116 117
    p_sys->p_image = image_HandlerCreate( p_filter );
    if( !p_sys->p_image )
118
    {
119
        free( p_sys );
120 121 122
        return VLC_EGENERIC;
    }

123 124 125 126 127 128
    p_sys->i_x = 0;
    p_sys->i_y = 0;
    p_sys->i_zoom = 2*ZOOM_FACTOR;
    p_sys->b_visible = true;
    p_sys->i_last_activity = mdate();
    p_sys->i_hide_timeout = 1000 * var_CreateGetInteger( p_filter, "mouse-hide-timeout" ); /* FIXME */
129

130 131
    /* */
    p_filter->pf_video_filter = Filter;
132
    p_filter->pf_video_mouse = Mouse;
133 134 135 136
    return VLC_SUCCESS;
}

/*****************************************************************************
137
 * Destroy:
138 139 140
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
141 142
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;
143

144
    image_HandlerDelete( p_sys->p_image );
145

146
    free( p_sys );
147 148 149 150 151
}

/*****************************************************************************
 * Render: displays previously rendered output
 *****************************************************************************/
152
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
153
{
154
    filter_sys_t *p_sys = p_filter->p_sys;
155 156 157 158
    picture_t *p_outpic;

    int v_w, v_h;
    picture_t *p_converted;
159
    plane_t *p_oyp;
160

161 162
    p_outpic = filter_NewPicture( p_filter );
    if( !p_outpic )
163
    {
164 165
        picture_Release( p_pic );
        return NULL;
166 167
    }

168
    /* */
169 170 171 172
    const bool b_visible = p_sys->b_visible;
    const int o_x = p_sys->i_x;
    const int o_y = p_sys->i_y;
    const int o_zoom = p_sys->i_zoom;
173 174 175 176

    /* background magnified image */
    if( o_zoom != ZOOM_FACTOR )
    {
177 178 179 180 181
        video_format_t fmt_in;
        video_format_t fmt_out;
        picture_t crop;

        crop = *p_pic;
182
        for( int i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
183
        {
184 185 186
            const int o_yp = o_y * p_outpic->p[i_plane].i_lines / p_outpic->p[Y_PLANE].i_lines;
            const int o_xp = o_x * p_outpic->p[i_plane].i_pitch / p_outpic->p[Y_PLANE].i_pitch;

187
            crop.p[i_plane].p_pixels += o_yp * p_pic->p[i_plane].i_pitch + o_xp;
188
        }
189 190

        /* */
191
        fmt_in = p_filter->fmt_in.video;
192 193
        fmt_in.i_width  = fmt_in.i_visible_width  = (fmt_in.i_width  * ZOOM_FACTOR / o_zoom) & ~1;
        fmt_in.i_height = fmt_in.i_visible_height = (fmt_in.i_height * ZOOM_FACTOR / o_zoom) & ~1;
194 195

        /* */
196
        fmt_out = p_filter->fmt_out.video;
197 198 199 200 201 202

        p_converted = image_Convert( p_sys->p_image, &crop, &fmt_in, &fmt_out );

        picture_CopyPixels( p_outpic, p_converted );

        picture_Release( p_converted );
203 204 205
    }
    else
    {
206
        picture_CopyPixels( p_outpic, p_pic );
207 208
    }

209 210 211
    /* */
    p_oyp = &p_outpic->p[Y_PLANE];
    if( b_visible )
212
    {
213 214
        video_format_t fmt_out;

215
        /* image visualization */
216
        fmt_out = p_filter->fmt_out.video;
217 218
        fmt_out.i_width  = fmt_out.i_visible_width  = (fmt_out.i_width /VIS_ZOOM) & ~1;
        fmt_out.i_height = fmt_out.i_visible_height = (fmt_out.i_height/VIS_ZOOM) & ~1;
219 220
        p_converted = image_Convert( p_sys->p_image, p_pic,
                                     &p_pic->format, &fmt_out );
221 222 223 224

        /* It will put only what can be copied at the top left */
        picture_CopyPixels( p_outpic, p_converted );

225
        picture_Release( p_converted );
226 227

        /* white rectangle on visualization */
228 229 230 231 232 233 234
        v_w = __MIN( fmt_out.i_width  * ZOOM_FACTOR / o_zoom, fmt_out.i_width - 1 );
        v_h = __MIN( fmt_out.i_height * ZOOM_FACTOR / o_zoom, fmt_out.i_height - 1 );

        DrawRectangle( p_oyp->p_pixels, p_oyp->i_pitch,
                       p_oyp->i_pitch, p_oyp->i_lines,
                       o_x/VIS_ZOOM, o_y/VIS_ZOOM,
                       v_w, v_h );
235 236

        /* */
237
        v_h = fmt_out.i_height + 1;
238 239 240 241 242 243
    }
    else
    {
        v_h = 1;
    }

244
    /* print a small "VLC ZOOM" */
245 246

    if( b_visible || p_sys->i_last_activity + p_sys->i_hide_timeout > mdate() )
247 248
        DrawZoomStatus( p_oyp->p_pixels, p_oyp->i_pitch, p_oyp->i_pitch, p_oyp->i_lines,
                        1, v_h, b_visible );
249 250

    if( b_visible )
251 252
    {
        /* zoom gauge */
253 254
        memset( p_oyp->p_pixels + (v_h+9)*p_oyp->i_pitch, 0xff, 41 );
        for( int y = v_h + 10; y < v_h + 90; y++ )
255
        {
256 257
            int i_width = v_h + 90 - y;
            i_width = i_width * i_width / 160;
258
            if( (80 - y + v_h)*ZOOM_FACTOR/10 < o_zoom )
259
            {
260
                memset( p_oyp->p_pixels + y*p_oyp->i_pitch, 0xff, i_width );
261 262 263 264
            }
            else
            {
                p_oyp->p_pixels[y*p_oyp->i_pitch] = 0xff;
265
                p_oyp->p_pixels[y*p_oyp->i_pitch + i_width - 1] = 0xff;
266 267 268
            }
        }
    }
269

270
    return CopyInfoAndRelease( p_outpic, p_pic );
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

static void DrawZoomStatus( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
                            int i_offset_x, int i_offset_y, bool b_visible )
{
    static const char *p_hide =
        "X   X X      XXXX   XXXXX  XXX   XXX  XX XX   X   X XXXXX XXXX  XXXXXL"
        "X   X X     X          X  X   X X   X X X X   X   X   X   X   X X    L"
        " X X  X     X         X   X   X X   X X   X   XXXXX   X   X   X XXXX L"
        " X X  X     X        X    X   X X   X X   X   X   X   X   X   X X    L"
        "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   X   X XXXXX XXXX  XXXXXL";
    static const char *p_show = 
        "X   X X      XXXX   XXXXX  XXX   XXX  XX XX    XXXX X   X  XXX  X   XL"
        "X   X X     X          X  X   X X   X X X X   X     X   X X   X X   XL"
        " X X  X     X         X   X   X X   X X   X    XXX  XXXXX X   X X X XL"
        " X X  X     X        X    X   X X   X X   X       X X   X X   X X X XL"
        "  X   XXXXX  XXXX   XXXXX  XXX   XXX  X   X   XXXX  X   X  XXX   X X L";
    const char *p_draw = b_visible ? p_hide : p_show;

290
    for( int i = 0, x = i_offset_x, y = i_offset_y; p_draw[i] != '\0'; i++ )
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
    {
        if( p_draw[i] == 'X' )
        {
            if( x < i_width && y < i_height )
                pb_dst[y*i_pitch + x] = 0xff;
            x++;
        }
        else if( p_draw[i] == ' ' )
        {
            x++;
        }
        else if( p_draw[i] == 'L' )
        {
            x = i_offset_x;
            y++;
        }
    }
}
static void DrawRectangle( uint8_t *pb_dst, int i_pitch, int i_width, int i_height,
                           int x, int y, int i_w, int i_h )
{
    if( x + i_w > i_width || y + i_h > i_height )
        return;

    /* top line */
Rafaël Carré's avatar
Rafaël Carré committed
316
    memset( &pb_dst[y * i_pitch + x], 0xff, i_w );
317 318

    /* left and right */
319
    for( int dy = 1; dy < i_h-1; dy++ )
320 321 322 323 324 325
    {
        pb_dst[(y+dy) * i_pitch + x +     0] = 0xff;
        pb_dst[(y+dy) * i_pitch + x + i_w-1] = 0xff;
    }

    /* bottom line */
Rafaël Carré's avatar
Rafaël Carré committed
326
    memset( &pb_dst[(y+i_h-1) * i_pitch + x], 0xff, i_w );
327 328
}

329
static int Mouse( filter_t *p_filter, vlc_mouse_t *p_mouse, const vlc_mouse_t *p_old, const vlc_mouse_t *p_new )
330
{
331 332
    filter_sys_t *p_sys = p_filter->p_sys;
    const video_format_t *p_fmt = &p_filter->fmt_in.video;
333

334 335 336
    /* */
    const bool b_click = vlc_mouse_HasPressed( p_old, p_new, MOUSE_BUTTON_LEFT );
    const bool b_pressed = vlc_mouse_IsLeftPressed( p_new );
337

338
    bool b_grab = false;
339

340 341
    /* Find the mouse position */
    if( p_sys->b_visible )
342
    {
343 344 345 346 347
        const int i_visu_width  = p_fmt->i_width  / VIS_ZOOM;
        const int i_visu_height = p_fmt->i_height / VIS_ZOOM;

        if( p_new->i_x >= 0 && p_new->i_x < i_visu_width &&
            p_new->i_y >= 0 && p_new->i_y < i_visu_height )
348
        {
349 350
            /* Visualization */
            if( b_pressed )
351
            {
352 353 354
                const int v_w = p_fmt->i_width  * ZOOM_FACTOR / p_sys->i_zoom;
                const int v_h = p_fmt->i_height * ZOOM_FACTOR / p_sys->i_zoom;

355
                p_sys->i_x = VLC_CLIP( p_new->i_x * VIS_ZOOM - v_w/2, 0,
356
                                           (int)p_fmt->i_width  - v_w - 1);
357
                p_sys->i_y = VLC_CLIP( p_new->i_y * VIS_ZOOM - v_h/2, 0,
358 359 360
                                           (int)p_fmt->i_height - v_h - 1);

                b_grab = true;
361 362
            }
        }
363 364 365
        else if( p_new->i_x >= 0 && p_new->i_x < 80 &&
                 p_new->i_y >= i_visu_height &&
                 p_new->i_y <  i_visu_height + 9 )
366
        {
367 368
            /* Hide text */
            if( b_click )
369
            {
370 371
                p_sys->b_visible = false;
                b_grab = true;
372
            }
373 374 375 376 377 378 379 380 381
        }
        else if( p_new->i_x >= 0 &&
                 p_new->i_x <= ( i_visu_height + 90 - p_new->i_y ) *
                               ( i_visu_height + 90 - p_new->i_y ) / 160 &&
                 p_new->i_y >= i_visu_height + 9 &&
                 p_new->i_y <= i_visu_height + 90 )
        {
            /* Zoom gauge */
            if( b_pressed )
382
            {
383 384 385 386 387 388
                p_sys->i_zoom = __MAX( ZOOM_FACTOR,
                                       (80 + i_visu_height - p_new->i_y + 2) *
                                           ZOOM_FACTOR / 10 );

                const int v_w = p_fmt->i_width  * ZOOM_FACTOR / p_sys->i_zoom;
                const int v_h = p_fmt->i_height * ZOOM_FACTOR / p_sys->i_zoom;
389 390
                p_sys->i_x = VLC_CLIP( p_sys->i_x, 0, (int)p_fmt->i_width  - v_w - 1 );
                p_sys->i_y = VLC_CLIP( p_sys->i_y, 0, (int)p_fmt->i_height - v_h - 1 );
391 392

                b_grab = true;
393
            }
394 395 396 397 398 399 400 401 402
        }
    }
    else
    {
        if( p_new->i_x >= 0 && p_new->i_x <  80 &&
            p_new->i_y >= 0 && p_new->i_y <= 10 )
        {
            /* Show text */
            if( b_click )
403
            {
404 405
                p_sys->b_visible = true;
                b_grab = true;
406 407 408 409
            }
        }
    }

410 411
    if( vlc_mouse_HasMoved( p_old, p_new ) )
        p_sys->i_last_activity = mdate();
412

413 414
    if( b_grab )
        return VLC_EGENERIC;
415

416 417 418 419
    /* */
    *p_mouse = *p_new;
    p_mouse->i_x = p_sys->i_x + p_new->i_x * ZOOM_FACTOR / p_sys->i_zoom;
    p_mouse->i_y = p_sys->i_y + p_new->i_y * ZOOM_FACTOR / p_sys->i_zoom;
420 421
    return VLC_SUCCESS;
}
422