summaryrefslogtreecommitdiffstats
path: root/apps/plugins/test_resize.c
blob: b0ef787d364284731bf76ec2f21ddd2af07b1236 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Jonas Hurrelmann
 *
 * 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.
 *
 ****************************************************************************/

/* Resizing test plugin. Loads /test.bmp (max 100x100) and displays a resized
 * version. Use the scrollwheel or the left/right keys to change the size of
 * the resizded version.
 */

#include "plugin.h"
#include "lib/pluginlib_actions.h"
#include "lib/bmp.h"

PLUGIN_HEADER

static const struct plugin_api* rb;

const struct button_mapping *plugin_contexts[]
= {generic_actions, generic_directions};

#define NB_ACTION_CONTEXTS sizeof(plugin_contexts)/sizeof(plugin_contexts[0])

/* Key assignement */
#define SIZE_INCREASE           PLA_UP
#define SIZE_INCREASE_REPEAT    PLA_UP_REPEAT
#define SIZE_DECREASE           PLA_DOWN
#define SIZE_DECREASE_REPEAT    PLA_DOWN_REPEAT

#define WIDTH_INCREASE           PLA_RIGHT
#define WIDTH_INCREASE_REPEAT    PLA_RIGHT_REPEAT
#define WIDTH_DECREASE           PLA_LEFT
#define WIDTH_DECREASE_REPEAT    PLA_LEFT_REPEAT

#define BUTTON_QUIT             PLA_QUIT
#define CHANGE_MODE             PLA_MENU

#define MAX_OUTPUT_WIDTH    LCD_WIDTH
#define MAX_OUTPUT_HEIGHT   LCD_HEIGHT

static fb_data *b;

static struct bitmap input_bmp;
static struct bitmap output_bmp;

static fb_data input_bmp_data[200*200];
static fb_data output_bmp_data[MAX_OUTPUT_WIDTH*MAX_OUTPUT_HEIGHT];


/* this is the plugin entry point */
enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
{
    (void)parameter;

    rb = api;
    b = rb->lcd_framebuffer;

    rb->lcd_set_background(LCD_RGBPACK(0,0,0));
    rb->lcd_clear_display(); // TODO: Optimizes this by e.g. invalidating rects

    input_bmp.data = (char*)input_bmp_data;
    output_bmp.data = (char*)output_bmp_data;

    int ret = rb->read_bmp_file("/test.bmp", &input_bmp, sizeof(input_bmp_data),
                                FORMAT_NATIVE);

    if (ret < 0) {
        rb->splash(HZ, "Could not load /test.bmp");
        return PLUGIN_ERROR;
    }

    int button;
    output_bmp.width = 50;
    output_bmp.height = 50;

    DEBUGF("input_bmp_data starts at %p\n", input_bmp_data);
    DEBUGF("output_bmp_data starts at %p\n", output_bmp_data);

    int scale_algorithm = 0;

    while(1) {
        rb->lcd_clear_display();
        rb->lcd_bitmap(input_bmp_data, 0, 0, input_bmp.width, input_bmp.height);

        switch ( scale_algorithm ) {
            case 0:
                smooth_resize_bitmap(&input_bmp, &output_bmp);
                rb->lcd_putsxy(0,0,"smooth_resize_bitmap");
                break;
            case 1:
                simple_resize_bitmap(&input_bmp, &output_bmp);
                rb->lcd_putsxy(0,0,"simple_resize_bitmap");
                break;
        }

        rb->lcd_bitmap(output_bmp_data, 0, 100, output_bmp.width,
                       output_bmp.height);

        rb->lcd_update();
        button = pluginlib_getaction(rb, HZ,
                                     plugin_contexts, NB_ACTION_CONTEXTS);
        switch (button) {
            case BUTTON_QUIT:
                return PLUGIN_OK;
            case SIZE_INCREASE:
            case SIZE_INCREASE_REPEAT:
                if (output_bmp.width < MAX_OUTPUT_WIDTH - 2)
                    output_bmp.width += 2;
                if (output_bmp.height < MAX_OUTPUT_HEIGHT - 2)
                    output_bmp.height += 2;
                break;

            case SIZE_DECREASE:
            case SIZE_DECREASE_REPEAT:
                if (output_bmp.width > 2) output_bmp.width -= 2;
                if (output_bmp.height > 2) output_bmp.height -= 2;
                break;

            case WIDTH_INCREASE:
            case WIDTH_INCREASE_REPEAT:
                if (output_bmp.width < MAX_OUTPUT_WIDTH - 2)
                    output_bmp.width += 2;
                break;

            case WIDTH_DECREASE:
            case WIDTH_DECREASE_REPEAT:
                if (output_bmp.width > 2) output_bmp.width -= 2;
                break;

            case CHANGE_MODE:
                scale_algorithm = (scale_algorithm+1)%2;
                break;
        }
    }
    return PLUGIN_OK;
}