summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lib/playergfx.c
blob: c332c027114e2309d54a2f91d724272366285d64 (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
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Bitmap graphics on player LCD!
*
* Copyright (C) 2005 Jens Arnold
*
* 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.
*
****************************************************************************/

#include "plugin.h"

#ifdef HAVE_LCD_CHARCELLS /* Player only :) */
#include "playergfx.h"

/*** globals ***/

static const struct plugin_api *pgfx_rb = NULL; /* global api struct pointer */
static int char_width;
static int char_height;
static int pixel_height;
static int pixel_width;
static unsigned long gfx_chars[8];
static unsigned char gfx_buffer[56];
static int drawmode = DRMODE_SOLID;

/*** Special functions ***/

/* library init */
bool pgfx_init(const struct plugin_api* newrb, int cwidth, int cheight)
{
    int i;

    if (((unsigned) cwidth * (unsigned) cheight) > 8 || (unsigned) cheight > 2)
        return false;

    pgfx_rb = newrb;
    char_width = cwidth;
    char_height = cheight;
    pixel_height = 7 * char_height;
    pixel_width = 5 * char_width;

    for (i = 0; i < cwidth * cheight; i++)
    {
        if ((gfx_chars[i] = pgfx_rb->lcd_get_locked_pattern()) == 0)
        {
            pgfx_release();
            return false;
        }
    }

    return true;
}

/* library deinit */
void pgfx_release(void)
{
    int i;
    
    for (i = 0; i < 8; i++)
        if (gfx_chars[i])
            pgfx_rb->lcd_unlock_pattern(gfx_chars[i]);
}

/* place the display */
void pgfx_display(int cx, int cy)
{
    int i, j;
    int width = MIN(char_width, 11 - cx);
    int height = MIN(char_height, 2 - cy);

    for (i = 0; i < width; i++)
        for (j = 0; j < height; j++)
            pgfx_rb->lcd_putc(cx + i, cy + j, gfx_chars[char_height * i + j]);
}

void pgfx_display_block(int cx, int cy, int x, int y)
{
    pgfx_rb->lcd_putc(cx, cy, gfx_chars[char_height * x + y]);
}


/*** Update functions ***/

void pgfx_update(void)
{
    int i;

    for (i = 0; i < char_width * char_height; i++)
        pgfx_rb->lcd_define_pattern(gfx_chars[i], gfx_buffer + 7 * i);
    
    pgfx_rb->lcd_update();
}

/*** Parameter handling ***/

void pgfx_set_drawmode(int mode)
{
    drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
}

int pgfx_get_drawmode(void)
{
    return drawmode;
}

/*** Low-level drawing functions ***/

static void setpixel(int x, int y)
{
    gfx_buffer[pixel_height * (x/5) + y] |= 0x10 >> (x%5);
}

static void clearpixel(int x, int y)
{
    gfx_buffer[pixel_height * (x/5) + y] &= ~(0x10 >> (x%5));
}

static void flippixel(int x, int y)
{
    gfx_buffer[pixel_height * (x/5) + y] ^= 0x10 >> (x%5);
}

static void nopixel(int x, int y)
{
    (void)x;
    (void)y;
}

lcd_pixelfunc_type* pgfx_pixelfuncs[8] = {
    flippixel, nopixel, setpixel, setpixel,
    nopixel, clearpixel, nopixel, clearpixel
};
                               
static void flipblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address ^= (bits & mask);
}

static void bgblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address &= (bits | ~mask);
}

static void fgblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address |= (bits & mask);
}

static void solidblock(unsigned char *address, unsigned mask, unsigned bits)
{
    unsigned data = *(char *)address;
    
    bits    ^= data;
    *address = data ^ (bits & mask);
}

static void flipinvblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address ^= (~bits & mask);
}

static void bginvblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address &= ~(bits & mask);
}

static void fginvblock(unsigned char *address, unsigned mask, unsigned bits)
{
    *address |= (~bits & mask);
}

static void solidinvblock(unsigned char *address, unsigned mask, unsigned bits)
{
    unsigned data = *(char *)address;
    
    bits     = ~bits ^ data;
    *address = data ^ (bits & mask);
}

lcd_blockfunc_type* pgfx_blockfuncs[8] = {
    flipblock, bgblock, fgblock, solidblock,
    flipinvblock, bginvblock, fginvblock, solidinvblock
};

/*** Drawing functions ***/

/* Clear the whole display */
void pgfx_clear_display(void)
{
    unsigned bits = (drawmode & DRMODE_INVERSEVID) ? 0x1F : 0;

    pgfx_rb->memset(gfx_buffer, bits, char_width * pixel_height);
}

/* Set a single pixel */
void pgfx_drawpixel(int x, int y)
{
    if (((unsigned)x < (unsigned)pixel_width) 
        && ((unsigned)y < (unsigned)pixel_height))
        pgfx_pixelfuncs[drawmode](x, y);
}

/* Draw a line */
void pgfx_drawline(int x1, int y1, int x2, int y2)
{
    int numpixels;
    int i;
    int deltax, deltay;
    int d, dinc1, dinc2;
    int x, xinc1, xinc2;
    int y, yinc1, yinc2;
    lcd_pixelfunc_type *pfunc = pgfx_pixelfuncs[drawmode];

    deltax = abs(x2 - x1);
    deltay = abs(y2 - y1);
    xinc2 = 1;
    yinc2 = 1;

    if (deltax >= deltay)
    {
        numpixels = deltax;
        d = 2 * deltay - deltax;
        dinc1 = deltay * 2;
        dinc2 = (deltay - deltax) * 2;
        xinc1 = 1;
        yinc1 = 0;
    }
    else
    {
        numpixels = deltay;
        d = 2 * deltax - deltay;
        dinc1 = deltax * 2;
        dinc2 = (deltax - deltay) * 2;
        xinc1 = 0;
        yinc1 = 1;
    }
    numpixels++; /* include endpoints */

    if (x1 > x2)
    {
        xinc1 = -xinc1;
        xinc2 = -xinc2;
    }

    if (y1 > y2)
    {
        yinc1 = -yinc1;
        yinc2 = -yinc2;
    }

    x = x1;
    y = y1;

    for (i = 0; i < numpixels; i++)
    {
        if (((unsigned)x < (unsigned)pixel_width)
            && ((unsigned)y < (unsigned)pixel_height))
            pfunc(x, y);

        if (d < 0)
        {
            d += dinc1;
            x += xinc1;
            y += yinc1;
        }
        else
        {
            d += dinc2;
            x += xinc2;
            y += yinc2;
        }
    }
}

/* Draw a horizontal line (optimised) */
void pgfx_hline(int x1, int x2, int y)
{
    int nx;
    unsigned char *dst;
    unsigned mask, mask_right;
    lcd_blockfunc_type *bfunc;

    /* direction flip */
    if (x2 < x1)
    {
        nx = x1;
        x1 = x2;
        x2 = nx;
    }

    /* nothing to draw? */
    if (((unsigned)y >= (unsigned)pixel_height) || (x1 >= pixel_width) 
        || (x2 < 0))
        return;  
    
    /* clipping */
    if (x1 < 0)
        x1 = 0;
    if (x2 >= pixel_width)
        x2 = pixel_width - 1;
        
    bfunc = pgfx_blockfuncs[drawmode];
    dst   = &gfx_buffer[pixel_height * (x1/5) + y];
    nx    = x2 - (x1 - (x1 % 5));
    mask  = 0x1F >> (x1 % 5);
    mask_right = 0x1F0 >> (nx % 5);

    for (; nx >= 5; nx -= 5)
    {
        bfunc(dst, mask, 0xFFu);
        dst += pixel_height;
        mask = 0x1F;
    }
    mask &= mask_right;
    bfunc(dst, mask, 0x1F);
}

/* Draw a vertical line (optimised) */
void pgfx_vline(int x, int y1, int y2)
{
    int y;
    unsigned char *dst, *dst_end;
    unsigned mask;
    lcd_blockfunc_type *bfunc;

    /* direction flip */
    if (y2 < y1)
    {
        y = y1;
        y1 = y2;
        y2 = y;
    }
    
    /* nothing to draw? */
    if (((unsigned)x >= (unsigned)pixel_width) || (y1 >= pixel_height) 
        || (y2 < 0))
        return;  
    
    /* clipping */
    if (y1 < 0)
        y1 = 0;
    if (y2 >= pixel_height)
        y2 = pixel_height - 1;
        
    bfunc = pgfx_blockfuncs[drawmode];
    dst   = &gfx_buffer[pixel_height * (x/5) + y1];
    mask  = 0x10 >> (x % 5);

    dst_end = dst + y2 - y1;
    do
        bfunc(dst++, mask, 0x1F);
    while (dst <= dst_end);
}

/* Draw a rectangular box */
void pgfx_drawrect(int x, int y, int width, int height)
{
    if ((width <= 0) || (height <= 0))
        return;

    int x2 = x + width - 1;
    int y2 = y + height - 1;

    pgfx_vline(x, y, y2);
    pgfx_vline(x2, y, y2);
    pgfx_hline(x, x2, y);
    pgfx_hline(x, x2, y2);
}

/* Fill a rectangular area */
void pgfx_fillrect(int x, int y, int width, int height)
{
    int nx;
    unsigned char *dst, *dst_end;
    unsigned mask, mask_right;
    lcd_blockfunc_type *bfunc;

    /* nothing to draw? */
    if ((width <= 0) || (height <= 0) || (x >= pixel_width)
        || (y >= pixel_height) || (x + width <= 0) || (y + height <= 0))
        return;

    /* clipping */
    if (x < 0)
    {
        width += x;
        x = 0;
    }
    if (y < 0)
    {
        height += y;
        y = 0;
    }
    if (x + width > pixel_width)
        width = pixel_width - x;
    if (y + height > pixel_height)
        height = pixel_height - y;
    
    bfunc = pgfx_blockfuncs[drawmode];
    dst   = &gfx_buffer[pixel_height * (x/5) + y];
    nx    = width - 1 + (x % 5);
    mask  = 0x1F >> (x % 5);
    mask_right = 0x1F0 >> (nx % 5);

    for (; nx >= 5; nx -= 5)
    {
        unsigned char *dst_col = dst;

        dst_end = dst_col + height;
        do
            bfunc(dst_col++, mask, 0x1F);
        while (dst_col < dst_end);

        dst += pixel_height;
        mask = 0x1F;
    }
    mask &= mask_right;

    dst_end = dst + height;
    do
        bfunc(dst++, mask, 0x1F);
    while (dst < dst_end);
}

/* About PlayerGFX internal bitmap format:
 *
 * A bitmap contains one bit for every pixel that defines if that pixel is
 * black (1) or white (0). Bits within a byte are arranged horizontally, 
 * MSB at the left.
 * The bytes are stored in row-major order, with byte 0 being top left,
 * byte 1 2nd from left etc. Each row of bytes defines one pixel row.
 *
 * This approximates the (even more strange) internal hardware format. */

/* Draw a partial bitmap. stride is given in pixels */
void pgfx_bitmap_part(const unsigned char *src, int src_x, int src_y,
                      int stride, int x, int y, int width, int height)
{
    int nx, shift;
    unsigned char *dst, *dst_end;
    unsigned mask, mask_right;
    lcd_blockfunc_type *bfunc;

    /* nothing to draw? */
    if ((width <= 0) || (height <= 0) || (x >= pixel_width) 
        || (y >= pixel_height) || (x + width <= 0) || (y + height <= 0))
        return;
        
    /* clipping */
    if (x < 0)
    {
        width += x;
        src_x -= x;
        x = 0;
    }
    if (y < 0)
    {
        height += y;
        src_y -= y;
        y = 0;
    }
    if (x + width > pixel_width)
        width = pixel_width - x;
    if (y + height > pixel_height)
        height = pixel_height - y;
        
    stride = (stride + 7) >> 3; /* convert to no. of bytes */

    src   += stride * src_y + (src_x >> 3); /* move starting point */  
    dst   = &gfx_buffer[pixel_height * (x/5) + y];
    shift = 3 + (x % 5) - (src_x & 7);
    nx    = width - 1 + (x % 5);

    bfunc  = pgfx_blockfuncs[drawmode];
    mask  = 0x1F >> (x % 5);
    mask_right = 0x1F0 >> (nx % 5);
    
    dst_end = dst + height;
    do
    {
        const unsigned char *src_row = src;
        unsigned char *dst_row = dst++;
        unsigned mask_row = mask;
        unsigned data = *src_row++;
        int extrabits = shift;

        for (x = nx; x >= 5; x -= 5)
        {
            if (extrabits < 0)
            {
                data = (data << 8) | *src_row++;
                extrabits += 8;
            }
            bfunc(dst_row, mask_row, data >> extrabits);
            extrabits -= 5;
            dst_row += pixel_height;
            mask_row = 0x1F;
        }
        if (extrabits < 0)
        {
            data = (data << 8) | *src_row;
            extrabits += 8;
        }
        bfunc(dst_row, mask_row & mask_right, data >> extrabits);

        src += stride;
    }
    while (dst < dst_end);
}

/* Draw a full bitmap */
void pgfx_bitmap(const unsigned char *src, int x, int y, int width, int height)
{
    pgfx_bitmap_part(src, 0, 0, width, x, y, width, height);
}

#endif /* HAVE_LCD_CHARCELLS */