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 <vlc_mouse.h>
#include <vlc_picture.h>
40 41
#include "filter_picture.h"

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

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

55 56
    set_callbacks( Create, Destroy )
vlc_module_end ()
57

58 59 60 61

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

65
/* */
66 67 68 69 70
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 );

71 72
/* */
struct filter_sys_t
73 74 75
{
    image_handler_t *p_image;

76 77
    int64_t i_hide_timeout;

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

81
    bool b_visible; /* is "interface" visible ? */
82 83

    int64_t i_last_activity;
84 85
};

86 87 88
#define VIS_ZOOM 4
#define ZOOM_FACTOR 8

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

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

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

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

125 126 127 128 129 130
    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 */
131

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

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

146
    image_HandlerDelete( p_sys->p_image );
147

148
    free( p_sys );
149 150 151 152 153
}

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

    int v_w, v_h;
    picture_t *p_converted;
161
    plane_t *p_oyp;
162

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

170
    /* */
171 172 173 174
    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;
175 176 177 178

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

        crop = *p_pic;
184
        for( int i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
185
        {
186 187 188
            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;

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

        /* */
193
        fmt_in = p_filter->fmt_in.video;
194 195
        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;
196 197

        /* */
198
        fmt_out = p_filter->fmt_out.video;
199 200 201 202 203 204

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

        picture_CopyPixels( p_outpic, p_converted );

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

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

217
        /* image visualization */
218
        fmt_out = p_filter->fmt_out.video;
219 220
        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;
221 222
        p_converted = image_Convert( p_sys->p_image, p_pic,
                                     &p_pic->format, &fmt_out );
223 224 225 226

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

227
        picture_Release( p_converted );
228 229

        /* white rectangle on visualization */
230 231 232 233 234 235 236
        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 );
237 238

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

246
    /* print a small "VLC ZOOM" */
247 248

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

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

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

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;

292
    for( int i = 0, x = i_offset_x, y = i_offset_y; p_draw[i] != '\0'; i++ )
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
    {
        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
318
    memset( &pb_dst[y * i_pitch + x], 0xff, i_w );
319 320

    /* left and right */
321
    for( int dy = 1; dy < i_h-1; dy++ )
322 323 324 325 326 327
    {
        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
328
    memset( &pb_dst[(y+i_h-1) * i_pitch + x], 0xff, i_w );
329 330
}

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

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

340
    bool b_grab = false;
341

342 343
    /* Find the mouse position */
    if( p_sys->b_visible )
344
    {
345 346 347 348 349
        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 )
350
        {
351 352
            /* Visualization */
            if( b_pressed )
353
            {
354 355 356
                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;

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

                b_grab = true;
363 364
            }
        }
365 366 367
        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 )
368
        {
369 370
            /* Hide text */
            if( b_click )
371
            {
372 373
                p_sys->b_visible = false;
                b_grab = true;
374
            }
375 376 377 378 379 380 381 382 383
        }
        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 )
384
            {
385 386 387 388 389 390
                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;
391 392
                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 );
393 394

                b_grab = true;
395
            }
396 397 398 399 400 401 402 403 404
        }
    }
    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 )
405
            {
406 407
                p_sys->b_visible = true;
                b_grab = true;
408 409 410 411
            }
        }
    }

412 413
    if( vlc_mouse_HasMoved( p_old, p_new ) )
        p_sys->i_last_activity = mdate();
414

415 416
    if( b_grab )
        return VLC_EGENERIC;
417

418 419 420 421
    /* */
    *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;
422 423
    return VLC_SUCCESS;
}
424