summaryrefslogtreecommitdiffstats
path: root/apps/plugins/imageviewer/imageviewer.c
blob: 9d5aea7f15658825814556f531dc1737650cbd95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * user intereface of image viewer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*
 * TODO:
 *   - check magick value in file header to determine image type.
 */
#include "plugin.h"
#include <lib/playback_control.h>
#include <lib/helper.h>
#include <lib/configfile.h>
#include "imageviewer.h"
#include "imageviewer_button.h"
#include "image_decoder.h"


#ifdef USEGSLIB
GREY_INFO_STRUCT
#endif

/* Headings */
#define DIR_PREV  1
#define DIR_NEXT -1
#define DIR_NONE  0

/******************************* Globals ***********************************/

/* Persistent configuration */
#define IMGVIEW_CONFIGFILE          "imageviewer.cfg"
#define IMGVIEW_SETTINGS_MINVERSION 1
#define IMGVIEW_SETTINGS_VERSION    2

/* Slideshow times */
#define SS_MIN_TIMEOUT      1
#define SS_MAX_TIMEOUT      20
#define SS_DEFAULT_TIMEOUT  5

#ifdef HAVE_LCD_COLOR
/* needed for value of settings */
#include "jpeg/yuv2rgb.h"
#endif

static struct imgview_settings settings =
{
#ifdef HAVE_LCD_COLOR
    COLOURMODE_COLOUR,
    DITHER_NONE,
#endif
    SS_DEFAULT_TIMEOUT
};
static struct imgview_settings old_settings;

static struct configdata config[] =
{
#ifdef HAVE_LCD_COLOR
    { TYPE_ENUM, 0, COLOUR_NUM_MODES, { .int_p = &settings.jpeg_colour_mode },
        "Colour Mode", (char *[]){ "Colour", "Grayscale" } },
    { TYPE_ENUM, 0, DITHER_NUM_MODES, { .int_p = &settings.jpeg_dither_mode },
        "Dither Mode", (char *[]){ "None", "Ordered", "Diffusion" } },
#endif
    { TYPE_INT, SS_MIN_TIMEOUT, SS_MAX_TIMEOUT,
        { .int_p = &settings.ss_timeout }, "Slideshow Time", NULL },
};

static void cb_progress(int current, int total);

static struct imgdec_api iv_api = {
    .settings = &settings,
    .slideshow_enabled = false,
    .running_slideshow = false,
#ifdef DISK_SPINDOWN
    .immediate_ata_off = false,
#endif
#ifdef USE_PLUG_BUF
    .plug_buf = true,
#endif

    .cb_progress = cb_progress,

#ifdef USEGSLIB
    .gray_bitmap_part = myxlcd_ub_(gray_bitmap_part),
#endif
};

/**************** begin Application ********************/


/************************* Globals ***************************/

#ifdef HAVE_LCD_COLOR
static fb_data rgb_linebuf[LCD_WIDTH];  /* Line buffer for scrolling when
                                           DITHER_DIFFUSION is set        */
#endif

/* buffer to load image decoder */
static unsigned char* decoder_buf;
static size_t decoder_buf_size;
/* the remaining free part of the buffer for loaded+resized images */
static unsigned char* buf;
static size_t buf_size;

static int ds, ds_min, ds_max; /* downscaling and limits */
static struct image_info image_info;

/* the current full file name */
static char np_file[MAX_PATH];
static int curfile = -1, direction = DIR_NEXT, entries = 0;

/* list of the supported image files */
static char **file_pt;

/* progress update tick */
static long next_progress_tick;

static const struct image_decoder *imgdec = NULL;
static enum image_type image_type = IMAGE_UNKNOWN;

/************************* Implementation ***************************/

/* Read directory contents for scrolling. */
static void get_pic_list(void)
{
    struct tree_context *tree = rb->tree_get_context();
    struct entry *dircache = rb->tree_get_entries(tree);
    int i;
    char *pname;

    file_pt = (char **) buf;

    /* Remove path and leave only the name.*/
    pname = rb->strrchr(np_file,'/');
    pname++;

    for (i = 0; i < tree->filesindir && buf_size > sizeof(char**); i++)
    {
        /* Add all files. Non-image files will be filtered out while loading. */
        if (!(dircache[i].attr & ATTR_DIRECTORY))
        {
            file_pt[entries] = dircache[i].name;
            /* Set Selected File. */
            if (!rb->strcmp(file_pt[entries], pname))
                curfile = entries;
            entries++;

            buf += (sizeof(char**));
            buf_size -= (sizeof(char**));
        }
    }
}

static int change_filename(int direct)
{
    bool file_erased = (file_pt[curfile] == NULL);
    direction = direct;

    curfile += (direct == DIR_PREV? entries - 1: 1);
    if (curfile >= entries)
        curfile -= entries;

    if (file_erased)
    {
        /* remove 'erased' file names from list. */
        int count, i;
        for (count = i = 0; i < entries; i++)
        {
            if (curfile == i)
                curfile = count;
            if (file_pt[i] != NULL)
                file_pt[count++] = file_pt[i];
        }
        entries = count;
    }

    if (entries == 0)
    {
        rb->splash(HZ, "No supported files");
        return PLUGIN_ERROR;
    }

    size_t np_file_length = rb->strlen(np_file);
    size_t np_file_name_length = rb->strlen(rb->strrchr(np_file, '/')+1);
    size_t avail_length = sizeof(np_file) - (np_file_length - np_file_name_length);

    rb->snprintf(rb->strrchr(np_file, '/')+1, avail_length, "%s", file_pt[curfile]);

    return PLUGIN_OTHER;
}

/* switch off overlay, for handling SYS_ events */
static void cleanup(void *parameter)
{
    (void)parameter;
#ifdef USEGSLIB
    grey_show(false);
#endif
}

#ifdef HAVE_LCD_COLOR
static bool set_option_grayscale(void)
{
    bool gray = settings.jpeg_colour_mode == COLOURMODE_GRAY;
    rb->set_bool("Grayscale (Jpeg)", &gray);
    settings.jpeg_colour_mode = gray ? COLOURMODE_GRAY : COLOURMODE_COLOUR;
    return false;
}

static bool set_option_dithering(void)
{
    static const struct opt_items dithering[DITHER_NUM_MODES] = {
        [DITHER_NONE]      = { STR(LANG_OFF) },
        [DITHER_ORDERED]   = { STR(LANG_ORDERED) },
        [DITHER_DIFFUSION] = { STR(LANG_DIFFUSION) },
    };

    rb->set_option(rb->str(LANG_DITHERING), &settings.jpeg_dither_mode, RB_INT,
                   dithering, DITHER_NUM_MODES, NULL);
    return false;
}

MENUITEM_FUNCTION(grayscale_item, 0, ID2P(LANG_GRAYSCALE),
                  set_option_grayscale, NULL, Icon_NOICON);
MENUITEM_FUNCTION(dithering_item, 0, ID2P(LANG_DITHERING),
                  set_option_dithering, NULL, Icon_NOICON);
MAKE_MENU(display_menu, "Display Options", NULL, Icon_NOICON,
            &grayscale_item, &dithering_item);

static void display_options(void)
{
    rb->do_menu(&display_menu, NULL, NULL, false);
}
#endif /* HAVE_LCD_COLOR */

static int show_menu(void) /* return 1 to quit */
{
    int result;

    enum menu_id
    {
        MIID_RETURN = 0,
        MIID_TOGGLE_SS_MODE,
        MIID_CHANGE_SS_MODE,
#ifdef USE_PLUG_BUF
        MIID_SHOW_PLAYBACK_MENU,
#endif
#ifdef HAVE_LCD_COLOR
        MIID_DISPLAY_OPTIONS,
#endif
        MIID_QUIT,
    };

    MENUITEM_STRINGLIST(menu, "Image Viewer Menu", NULL,
                        ID2P(LANG_RETURN),
                        ID2P(LANG_SLIDESHOW_MODE),
                        ID2P(LANG_SLIDESHOW_TIME),
#ifdef USE_PLUG_BUF
                        ID2P(LANG_PLAYBACK_CONTROL),
#endif
#ifdef HAVE_LCD_COLOR
                        ID2P(LANG_MENU_DISPLAY_OPTIONS),
#endif
                        ID2P(LANG_MENU_QUIT));

    static const struct opt_items slideshow[2] = {
        { STR(LANG_OFF) },
        { STR(LANG_ON) },
    };

    result=rb->do_menu(&menu, NULL, NULL, false);

    switch (result)
    {
        case MIID_RETURN:
            break;
        case MIID_TOGGLE_SS_MODE:
            rb->set_option(rb->str(LANG_SLIDESHOW_MODE), &iv_api.slideshow_enabled, RB_BOOL,
                           slideshow , 2, NULL);
            break;
        case MIID_CHANGE_SS_MODE:
            rb->set_int(rb->str(LANG_SLIDESHOW_TIME), "s", UNIT_SEC,
                        &settings.ss_timeout, NULL, 1,
                        SS_MIN_TIMEOUT, SS_MAX_TIMEOUT, NULL);
            break;

#ifdef USE_PLUG_BUF
        case MIID_SHOW_PLAYBACK_MENU:
            if (iv_api.plug_buf)
            {
                playback_control(NULL);
            }
            else
            {
                rb->splash(HZ, ID2P(LANG_CANNOT_RESTART_PLAYBACK));
            }
            break;
#endif
#ifdef HAVE_LCD_COLOR
        case MIID_DISPLAY_OPTIONS:
            display_options();
            break;
#endif
        case MIID_QUIT:
            return 1;
            break;
    }

#ifdef DISK_SPINDOWN
    /* change ata spindown time based on slideshow time setting */
    iv_api.immediate_ata_off = false;
    rb->storage_spindown(rb->global_settings->disk_spindown);

    if (iv_api.slideshow_enabled)
    {
        if(settings.ss_timeout < 10)
        {
            /* slideshow times < 10s keep disk spinning */
            rb->storage_spindown(0);
        }
        else if (!rb->pcm_is_playing())
        {
            /* slideshow times > 10s and not playing: ata_off after load */
            iv_api.immediate_ata_off = true;
        }
    }
#endif
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
    rb->lcd_set_foreground(LCD_WHITE);
    rb->lcd_set_background(LCD_BLACK);
#endif
    rb->lcd_clear_display();
    return 0;
}

#ifdef USE_PLUG_BUF
static int ask_and_get_audio_buffer(const char *filename)
{
    int button;
#if defined(IMGVIEW_ZOOM_PRE) || defined(IMGVIEW_QUIT_PRE)
    int lastbutton = BUTTON_NONE;
#endif
    rb->lcd_setfont(FONT_SYSFIXED);
    rb->lcd_clear_display();
    rb->lcd_puts(0, 0, rb->strrchr(filename,'/')+1);
    rb->lcd_puts(0, 1, "Not enough plugin memory!");
    rb->lcd_puts(0, 2, "Zoom In: Stop playback.");
    if(entries > 1)
        rb->lcd_puts(0, 3, "Left/Right: Skip File.");
    rb->lcd_puts(0, 4, "Show Menu: Quit.");
    rb->lcd_update();
    rb->lcd_setfont(FONT_UI);

    rb->button_clear_queue();

    while (1)
    {
        if (iv_api.slideshow_enabled)
            button = rb->button_get_w_tmo(settings.ss_timeout * HZ);
        else
            button = rb->button_get(true);

        switch(button)
        {
            case IMGVIEW_ZOOM_IN:
#ifdef IMGVIEW_ZOOM_PRE
                if (lastbutton != IMGVIEW_ZOOM_PRE)
                    break;
#endif
                iv_api.plug_buf = false;
                buf = rb->plugin_get_audio_buffer(&buf_size);
                /*try again this file, now using the audio buffer */
                return PLUGIN_OTHER;
#ifdef IMGVIEW_RC_MENU
            case IMGVIEW_RC_MENU:
#endif
#ifdef IMGVIEW_QUIT
            case IMGVIEW_QUIT:
#ifdef IMGVIEW_QUIT_PRE
            if (lastbutton != IMGVIEW_QUIT_PRE)
                break;
#endif
#endif
            case IMGVIEW_MENU:
                return PLUGIN_OK;

            case IMGVIEW_LEFT:
                if(entries>1)
                {
                    rb->lcd_clear_display();
                    return change_filename(DIR_PREV);
                }
                break;

            case IMGVIEW_RIGHT:
                if(entries>1)
                {
                    rb->lcd_clear_display();
                    return change_filename(DIR_NEXT);
                }
                break;
            case BUTTON_NONE:
                if(entries>1)
                {
                    rb->lcd_clear_display();
                    return change_filename(direction);
                }
                break;

            default:
                if(rb->default_event_handler_ex(button, cleanup, NULL)
                        == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
        }
#if defined(IMGVIEW_ZOOM_PRE) || defined(IMGVIEW_QUIT_PRE)
        if (button != BUTTON_NONE)
            lastbutton = button;
#endif
    }
}
#endif /* USE_PLUG_BUF */

/* callback updating a progress meter while image decoding */
static void cb_progress(int current, int total)
{
    /* do not yield or update the progress bar if we did so too recently */
    long now = *rb->current_tick;
    if(!TIME_AFTER(now, next_progress_tick))
        return;

    /* limit to 20fps */
    next_progress_tick = now + HZ/20;

#ifndef USEGSLIB
    /* in slideshow mode, keep gui interference to a minimum */
    const int size = (!iv_api.running_slideshow ? 8 : 4);
#else
    const int size = 8;
    if(!iv_api.running_slideshow)
#endif
    {
        rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],
                            0, LCD_HEIGHT-size, LCD_WIDTH, size,
                            total, 0, current, HORIZONTAL);
        rb->lcd_update_rect(0, LCD_HEIGHT-size, LCD_WIDTH, size);
    }

    rb->yield(); /* be nice to the other threads */
}

#define VSCROLL (LCD_HEIGHT/8)
#define HSCROLL (LCD_WIDTH/10)

/* Pan the viewing window right - move image to the left and fill in
   the right-hand side */
static void pan_view_right(struct image_info *info)
{
    int move;

    move = MIN(HSCROLL, info->width - info->x - LCD_WIDTH);
    if (move > 0)
    {
        mylcd_ub_scroll_left(move); /* scroll left */
        info->x += move;
        imgdec->draw_image_rect(info, LCD_WIDTH - move, 0,
                                move, info->height-info->y);
        mylcd_ub_update();
    }
}

/* Pan the viewing window left - move image to the right and fill in
   the left-hand side */
static void pan_view_left(struct image_info *info)
{
    int move;

    move = MIN(HSCROLL, info->x);
    if (move > 0)
    {
        mylcd_ub_scroll_right(move); /* scroll right */
        info->x -= move;
        imgdec->draw_image_rect(info, 0, 0, move, info->height-info->y);
        mylcd_ub_update();
    }
}

/* Pan the viewing window up - move image down and fill in
   the top */
static void pan_view_up(struct image_info *info)
{
    int move;

    move = MIN(VSCROLL, info->y);
    if (move > 0)
    {
        mylcd_ub_scroll_down(move); /* scroll down */
        info->y -= move;
#ifdef HAVE_LCD_COLOR
        if (image_type == IMAGE_JPEG
         && settings.jpeg_dither_mode == DITHER_DIFFUSION)
        {
            /* Draw over the band at the top of the last update
               caused by lack of error history on line zero. */
            move = MIN(move + 1, info->y + info->height);
        }
#endif
        imgdec->draw_image_rect(info, 0, 0, info->width-info->x, move);
        mylcd_ub_update();
    }
}

/* Pan the viewing window down - move image up and fill in
   the bottom */
static void pan_view_down(struct image_info *info)
{
    static fb_data *lcd_fb = NULL;
    if (!lcd_fb)
    {
        struct viewport *vp_main = *(rb->screens[SCREEN_MAIN]->current_viewport);
        lcd_fb = vp_main->buffer->fb_ptr;
    }

    int move;

    move = MIN(VSCROLL, info->height - info->y - LCD_HEIGHT);
    if (move > 0)
    {
        mylcd_ub_scroll_up(move); /* scroll up */
        info->y += move;
#ifdef HAVE_LCD_COLOR
        if (image_type == IMAGE_JPEG
         && settings.jpeg_dither_mode == DITHER_DIFFUSION)
        {
            /* Save the line that was on the last line of the display
               and draw one extra line above then recover the line with
               image data that had an error history when it was drawn.
             */
            move++, info->y--;
            rb->memcpy(rgb_linebuf,
                    lcd_fb + (LCD_HEIGHT - move)*LCD_WIDTH,
                    LCD_WIDTH*sizeof (fb_data));
        }
#endif

        imgdec->draw_image_rect(info, 0, LCD_HEIGHT - move,
                                info->width-info->x, move);

#ifdef HAVE_LCD_COLOR
        if (image_type == IMAGE_JPEG
         && settings.jpeg_dither_mode == DITHER_DIFFUSION)
        {
            /* Cover the first row drawn with previous image data. */
            rb->memcpy(lcd_fb + (LCD_HEIGHT - move)*LCD_WIDTH,
                        rgb_linebuf, LCD_WIDTH*sizeof (fb_data));
            info->y++;
        }
#endif
        mylcd_ub_update();
    }
}

/* interactively scroll around the image */
static int scroll_bmp(struct image_info *info, bool initial_frame)
{
    static long ss_timeout = 0;

    int button;
#if defined(IMGVIEW_ZOOM_PRE) || defined(IMGVIEW_MENU_PRE) \
    || defined(IMGVIEW_SLIDE_SHOW_PRE) || defined(IMGVIEW_QUIT_PRE)
    static int lastbutton;
    if (initial_frame)
        lastbutton = BUTTON_NONE;

#else
    (void) initial_frame;
#endif

    if (!ss_timeout && iv_api.slideshow_enabled)
        ss_timeout = *rb->current_tick + settings.ss_timeout * HZ;

    while (true)
    {
        if (iv_api.slideshow_enabled)
        {
            if (info->frames_count > 1 && info->delay &&
                settings.ss_timeout * HZ > info->delay)
            {
                /* animated content and delay between subsequent frames
                 * is shorter then slideshow delay
                 */
                button = rb->button_get_w_tmo(info->delay);
            }
            else
                button = rb->button_get_w_tmo(settings.ss_timeout * HZ);
        }
        else
        {
            if (info->frames_count > 1 && info->delay)
                button = rb->button_get_w_tmo(info->delay);
            else
                button = rb->button_get(true);
        }

        iv_api.running_slideshow = false;

        switch(button)
        {
        case IMGVIEW_LEFT:
            if (entries > 1 && info->width <= LCD_WIDTH
                            && info->height <= LCD_HEIGHT)
                return change_filename(DIR_PREV);
            /* fallthrough */
        case IMGVIEW_LEFT | BUTTON_REPEAT:
            pan_view_left(info);
            break;

        case IMGVIEW_RIGHT:
            if (entries > 1 && info->width <= LCD_WIDTH
                            && info->height <= LCD_HEIGHT)
                return change_filename(DIR_NEXT);
            /* fallthrough */
        case IMGVIEW_RIGHT | BUTTON_REPEAT:
            pan_view_right(info);
            break;

        case IMGVIEW_UP:
        case IMGVIEW_UP | BUTTON_REPEAT:
#ifdef IMGVIEW_SCROLL_UP
        case IMGVIEW_SCROLL_UP:
        case IMGVIEW_SCROLL_UP | BUTTON_REPEAT:
#endif
            pan_view_up(info);
            break;

        case IMGVIEW_DOWN:
        case IMGVIEW_DOWN | BUTTON_REPEAT:
#ifdef IMGVIEW_SCROLL_DOWN
        case IMGVIEW_SCROLL_DOWN:
        case IMGVIEW_SCROLL_DOWN | BUTTON_REPEAT:
#endif
            pan_view_down(info);
            break;

        case BUTTON_NONE:
            if (iv_api.slideshow_enabled && entries > 1)
            {
                if (info->frames_count > 1)
                {
                    /* animations */
                    if (TIME_AFTER(*rb->current_tick, ss_timeout))
                    {
                        iv_api.running_slideshow = true;
                        ss_timeout = 0;
                        return change_filename(DIR_NEXT);
                    }
                    else
                        return NEXT_FRAME;
                }
                else
                {
                    /* still picture */
                    iv_api.running_slideshow = true;
                    return change_filename(DIR_NEXT);
                }
            }
            else
                return NEXT_FRAME;

            break;

#ifdef IMGVIEW_SLIDE_SHOW
        case IMGVIEW_SLIDE_SHOW:
#ifdef IMGVIEW_SLIDE_SHOW_PRE
            if (lastbutton != IMGVIEW_SLIDE_SHOW_PRE)
                break;
#endif
#ifdef IMGVIEW_SLIDE_SHOW2
        case IMGVIEW_SLIDE_SHOW2:
#endif
            iv_api.slideshow_enabled = !iv_api.slideshow_enabled;
            break;
#endif

#ifdef IMGVIEW_NEXT_REPEAT
        case IMGVIEW_NEXT_REPEAT:
#endif
        case IMGVIEW_NEXT:
            if (entries > 1)
                return change_filename(DIR_NEXT);
            break;

#ifdef IMGVIEW_PREVIOUS_REPEAT
        case IMGVIEW_PREVIOUS_REPEAT:
#endif
        case IMGVIEW_PREVIOUS:
            if (entries > 1)
                return change_filename(DIR_PREV);
            break;

        case IMGVIEW_ZOOM_IN:
#ifdef IMGVIEW_ZOOM_PRE
            if (lastbutton != IMGVIEW_ZOOM_PRE)
                break;
#endif
            return ZOOM_IN;
            break;

        case IMGVIEW_ZOOM_OUT:
#ifdef IMGVIEW_ZOOM_PRE
            if (lastbutton != IMGVIEW_ZOOM_PRE)
                break;
#endif
            return ZOOM_OUT;
            break;

#ifdef IMGVIEW_RC_MENU
        case IMGVIEW_RC_MENU:
#endif
        case IMGVIEW_MENU:
#ifdef IMGVIEW_MENU_PRE
            if (lastbutton != IMGVIEW_MENU_PRE)
                break;
#endif
#ifdef USEGSLIB
            grey_show(false); /* switch off greyscale overlay */
#endif
            if (show_menu() == 1)
                return PLUGIN_OK;

#ifdef USEGSLIB
            grey_show(true); /* switch on greyscale overlay */
#else
            imgdec->draw_image_rect(info, 0, 0,
                            info->width-info->x, info->height-info->y);
            mylcd_ub_update();
#endif
            break;

#ifdef IMGVIEW_QUIT
            case IMGVIEW_QUIT:
#ifdef IMGVIEW_QUIT_PRE
            if (lastbutton != IMGVIEW_QUIT_PRE)
                break;
#endif
            return PLUGIN_OK;
            break;
#endif

        default:
            if (rb->default_event_handler_ex(button, cleanup, NULL)
                == SYS_USB_CONNECTED)
                return PLUGIN_USB_CONNECTED;
            break;

        } /* switch */
#if defined(IMGVIEW_ZOOM_PRE) || defined(IMGVIEW_MENU_PRE) ||\
    defined(IMGVIEW_SLIDE_SHOW_PRE) || defined(IMGVIEW_QUIT_PRE)
        if (button != BUTTON_NONE)
            lastbutton = button;
#endif
    } /* while (true) */
}

/********************* main function *************************/

/* how far can we zoom in without running out of memory */
static int min_downscale(int bufsize)
{
    int downscale = 8;

    if (imgdec->img_mem(8) > bufsize)
        return 0; /* error, too large, even 1:8 doesn't fit */

    while (downscale > 1 && imgdec->img_mem(downscale/2) <= bufsize)
        downscale /= 2;

    return downscale;
}

/* how far can we zoom out, to fit image into the LCD */
static int max_downscale(struct image_info *info)
{
    int downscale = 1;

    while (downscale < 8 && (info->x_size/downscale > LCD_WIDTH
                          || info->y_size/downscale > LCD_HEIGHT))
    {
        downscale *= 2;
    }

    return downscale;
}

/* set the view to the given center point, limit if necessary */
static void set_view(struct image_info *info, int cx, int cy)
{
    int x, y;

    /* plain center to available width/height */
    x = cx - MIN(LCD_WIDTH, info->width) / 2;
    y = cy - MIN(LCD_HEIGHT, info->height) / 2;

    /* limit against upper image size */
    x = MIN(info->width - LCD_WIDTH, x);
    y = MIN(info->height - LCD_HEIGHT, y);

    /* limit against negative side */
    x = MAX(0, x);
    y = MAX(0, y);

    info->x = x; /* set the values */
    info->y = y;
}

/* calculate the view center based on the bitmap position */
static void get_view(struct image_info *info, int *p_cx, int *p_cy)
{
    *p_cx = info->x + MIN(LCD_WIDTH, info->width) / 2;
    *p_cy = info->y + MIN(LCD_HEIGHT, info->height) / 2;
}

/* load, decode, display the image */
static int load_and_show(char* filename, struct image_info *info)
{
    int status;
    int cx, cy;
    ssize_t remaining;

    rb->lcd_clear_display();

    /* suppress warning while running slideshow */
    status = get_image_type(filename, iv_api.running_slideshow);
    if (status == IMAGE_UNKNOWN) {
        /* file isn't supported image file, skip this. */
        file_pt[curfile] = NULL;
        return change_filename(direction);
    }
    if (image_type != status) /* type of image is changed, load decoder. */
    {
        struct loader_info loader_info = {
            status, &iv_api, decoder_buf, decoder_buf_size,
        };
        image_type = status;
        imgdec = load_decoder(&loader_info);
        if (imgdec == NULL)
        {
            /* something is wrong */
            return PLUGIN_ERROR;
        }
#ifdef USE_PLUG_BUF
        if(iv_api.plug_buf)
        {
            buf = loader_info.buffer;
            buf_size = loader_info.size;
        }
#endif
    }
    rb->memset(info, 0, sizeof(*info));
    remaining = buf_size;

    if (rb->button_get(false) == IMGVIEW_MENU)
        status = PLUGIN_ABORT;
    else
        status = imgdec->load_image(filename, info, buf, &remaining);

    if (status == PLUGIN_OUTOFMEM)
    {
#ifdef USE_PLUG_BUF
        if(iv_api.plug_buf)
        {
            return ask_and_get_audio_buffer(filename);
        }
        else
#endif
        {
            rb->splash(HZ, "Out of Memory");
            file_pt[curfile] = NULL;
            return change_filename(direction);
        }
    }
    else if (status == PLUGIN_ERROR)
    {
        file_pt[curfile] = NULL;
        return change_filename(direction);
    }
    else if (status == PLUGIN_ABORT) {
        rb->splash(HZ, "Aborted");
        return PLUGIN_OK;
    }

    ds_max = max_downscale(info);       /* check display constraint */
    ds_min = min_downscale(remaining);  /* check memory constraint */
    if (ds_min == 0)
    {
        if (imgdec->unscaled_avail)
        {
            /* Can not resize the image but original one is available, so use it. */
            ds_min = ds_max = 1;
        }
        else
#ifdef USE_PLUG_BUF
        if (iv_api.plug_buf)
        {
            return ask_and_get_audio_buffer(filename);
        }
        else
#endif
        {
            rb->splash(HZ, "Too large");
            file_pt[curfile] = NULL;
            return change_filename(direction);
        }
    }
    else if (ds_max < ds_min)
        ds_max = ds_min;

    ds = ds_max; /* initialize setting */
    cx = info->x_size/ds/2; /* center the view */
    cy = info->y_size/ds/2;

    /* used to loop through subimages in animated gifs */
    int frame = 0;
    bool initial_frame = true;
    do  /* loop the image prepare and decoding when zoomed */
    {
        status = imgdec->get_image(info, frame, ds); /* decode or fetch from cache */
        if (status == PLUGIN_ERROR)
        {
            file_pt[curfile] = NULL;
            return change_filename(direction);
        }

        set_view(info, cx, cy);

        if(!iv_api.running_slideshow && (info->frames_count == 1))
        {
            rb->lcd_putsf(0, 3, "showing %dx%d", info->width, info->height);
            rb->lcd_update();
        }

        if (frame == 0)
            mylcd_ub_clear_display();
        imgdec->draw_image_rect(info, 0, 0,
                        info->width-info->x, info->height-info->y);
        mylcd_ub_update();

#ifdef USEGSLIB
        grey_show(true); /* switch on greyscale overlay */
#endif

        /* drawing is now finished, play around with scrolling
         * until you press OFF or connect USB
         */
        while (1)
        {
            status = scroll_bmp(info, initial_frame);
            initial_frame = false;

            if (status == ZOOM_IN)
            {
                if (ds > ds_min || (imgdec->unscaled_avail && ds > 1))
                {
                    /* if 1/1 is always available, jump ds from ds_min to 1. */
                    int zoom = (ds == ds_min)? ds_min: 2;
                    ds /= zoom; /* reduce downscaling to zoom in */
                    get_view(info, &cx, &cy);
                    cx *= zoom; /* prepare the position in the new image */
                    cy *= zoom;
                }
                else
                    continue;
            }

            if (status == ZOOM_OUT)
            {
                if (ds < ds_max)
                {
                    /* if ds is 1 and ds_min is > 1, jump ds to ds_min. */
                    int zoom = (ds < ds_min)? ds_min: 2;
                    ds *= zoom; /* increase downscaling to zoom out */
                    get_view(info, &cx, &cy);
                    cx /= zoom; /* prepare the position in the new image */
                    cy /= zoom;
                    mylcd_ub_clear_display();
                }
                else
                    continue;
            }

            /* next frame in animated content */
            if (status == NEXT_FRAME)
                frame = (frame + 1)%info->frames_count;

            break;
        }

#ifdef USEGSLIB
        if (info->frames_count <= 1)
            grey_show(false); /* switch off overlay */
#endif
        rb->lcd_clear_display();
    }
    while (status > PLUGIN_OTHER);
#ifdef USEGSLIB
    grey_show(false); /* switch off overlay */
    rb->lcd_update();
#endif
    return status;
}

/******************** Plugin entry point *********************/

enum plugin_status plugin_start(const void* parameter)
{
    int condition;
#ifdef USEGSLIB
    long greysize; /* helper */
#endif

    if(!parameter) {rb->splash(HZ*2, "No file"); return PLUGIN_ERROR; }

    rb->strcpy(np_file, parameter);
    if (get_image_type(np_file, false) == IMAGE_UNKNOWN)
    {
        rb->splash(HZ*2, "Unsupported file");
        return PLUGIN_ERROR;
    }

#ifdef USE_PLUG_BUF
    buf = rb->plugin_get_buffer(&buf_size);
#else
    decoder_buf = rb->plugin_get_buffer(&decoder_buf_size);
    buf = rb->plugin_get_audio_buffer(&buf_size);
#endif

    get_pic_list();

#ifdef USEGSLIB
    if (!grey_init(buf, buf_size, GREY_ON_COP,
                   LCD_WIDTH, LCD_HEIGHT, &greysize))
    {
        rb->splash(HZ, "grey buf error");
        return PLUGIN_ERROR;
    }
    buf += greysize;
    buf_size -= greysize;
#endif

#ifdef USE_PLUG_BUF
    decoder_buf = buf;
    decoder_buf_size = buf_size;
    if(!rb->audio_status())
    {
        iv_api.plug_buf = false;
        buf = rb->plugin_get_audio_buffer(&buf_size);
    }
#endif

    /* should be ok to just load settings since the plugin itself has
       just been loaded from disk and the drive should be spinning */
    configfile_load(IMGVIEW_CONFIGFILE, config,
                    ARRAYLEN(config), IMGVIEW_SETTINGS_MINVERSION);
    rb->memcpy(&old_settings, &settings, sizeof (settings));

    /* Turn off backlight timeout */
    backlight_ignore_timeout();

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
    rb->lcd_set_foreground(LCD_WHITE);
    rb->lcd_set_background(LCD_BLACK);
#endif

    do
    {
        condition = load_and_show(np_file, &image_info);
    } while (condition >= PLUGIN_OTHER);
    release_decoder();

    if (rb->memcmp(&settings, &old_settings, sizeof (settings)))
    {
        /* Just in case drive has to spin, keep it from looking locked */
        rb->splash(0, "Saving Settings");
        configfile_save(IMGVIEW_CONFIGFILE, config,
                        ARRAYLEN(config), IMGVIEW_SETTINGS_VERSION);
    }

#ifdef DISK_SPINDOWN
    /* set back ata spindown time in case we changed it */
    rb->storage_spindown(rb->global_settings->disk_spindown);
#endif

    /* Turn on backlight timeout (revert to settings) */
    backlight_use_settings();

#ifdef USEGSLIB
    grey_release(); /* deinitialize */
#endif

    return condition;
}