summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lib/grey_draw.c
blob: 298ffbe16ef68b14dcd3b878c2f948cd269ffe1c (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
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* New greyscale framework
* Drawing functions
*
* This is a generic framework to display 129 shades of grey on low-depth
* bitmap LCDs (Archos b&w, Iriver & Ipod 4-grey) within plugins.
*
* Copyright (C) 2008 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"
#include "grey.h"

extern struct viewport _grey_default_vp;

/*** low-level drawing functions ***/

static void setpixel(unsigned char *address)
{
    *address = _grey_info.vp->fg_pattern;
}

static void clearpixel(unsigned char *address)
{
    *address = _grey_info.vp->bg_pattern;
}

static void flippixel(unsigned char *address)
{
    *address = ~(*address);
}

static void nopixel(unsigned char *address)
{
    (void)address;
}

void (* const _grey_pixelfuncs[8])(unsigned char *address) = {
    flippixel, nopixel, setpixel, setpixel,
    nopixel, clearpixel, nopixel, clearpixel
};

/*** Drawing functions ***/

/* Clear the current viewport */
void grey_clear_viewport(void)
{
    struct viewport *vp = _grey_info.vp;
    int drawmode = vp->drawmode;
    vp->drawmode = DRMODE_SOLID | DRMODE_INVERSEVID;
    grey_fillrect(0, 0, vp->width, vp->height);
    vp->drawmode = drawmode;
}

/* Clear the whole display */
void grey_clear_display(void)
{
    struct viewport *vp = &_grey_default_vp;

    int value = (vp->drawmode & DRMODE_INVERSEVID) ?
                 vp->fg_pattern : vp->bg_pattern;

    rb->memset(_grey_info.curbuffer, value,
               _GREY_MULUQ(_grey_info.cb_width, _grey_info.cb_height));
}

/* Set a single pixel */
void grey_drawpixel(int x, int y)
{
    if (x >= _grey_info.clip_l && x < _grey_info.clip_r &&
        y >= _grey_info.clip_t && y < _grey_info.clip_b)
    {
        int dst_stride = _grey_info.cb_width;
        struct viewport *vp = _grey_info.vp;
        _grey_pixelfuncs[vp->drawmode](
                &_grey_info.curbuffer[
                    _GREY_MULUQ(dst_stride, vp->y - _grey_info.cb_y + y) +
                    vp->x - _grey_info.cb_x + x]);
    }
}

/* Draw a line */
void grey_drawline(int x1, int y1, int x2, int y2)
{
    struct viewport *vp = _grey_info.vp;
    int numpixels;
    int i;
    int deltax, deltay;
    int d, dinc1, dinc2;
    int x, xinc1, xinc2;
    int y, yinc1, yinc2;
    void (*pfunc)(unsigned char *address) = _grey_pixelfuncs[vp->drawmode];
    int dwidth;
    int xoffs, yoffs;

    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;

    dwidth = _grey_info.cb_width;
    xoffs  = vp->x - _grey_info.cb_x;
    yoffs  = vp->y - _grey_info.cb_y;

    for (i = 0; i < numpixels; i++)
    {
        if (x >= _grey_info.clip_l && x < _grey_info.clip_r &&
            y >= _grey_info.clip_t && y < _grey_info.clip_b)
        {
            pfunc(&_grey_info.curbuffer[_GREY_MULUQ(dwidth, yoffs + y) +
                                        xoffs + x]);
        }

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

/* Draw a horizontal line (optimised) */
void grey_hline(int x1, int x2, int y)
{
    struct viewport *vp = _grey_info.vp;
    int x;
    int value = 0;
    unsigned char *dst;
    bool fillopt = false;
    int dwidth;

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

    /* nothing to draw? */
    if (y < _grey_info.clip_t || y >= _grey_info.clip_b ||
        x1 >= _grey_info.clip_r || x2 < _grey_info.clip_l)
        return;  
    
    /* drawmode and optimisation */
    if (vp->drawmode & DRMODE_INVERSEVID)
    {
        if (vp->drawmode & DRMODE_BG)
        {
            fillopt = true;
            value = vp->bg_pattern;
        }
    }
    else
    {
        if (vp->drawmode & DRMODE_FG)
        {
            fillopt = true;
            value = vp->fg_pattern;
        }
    }
    if (!fillopt && vp->drawmode != DRMODE_COMPLEMENT)
        return;

    /* clipping */
    if (x1 < _grey_info.clip_l)
        x1 = _grey_info.clip_l;
    if (x2 >= _grey_info.clip_r)
        x2 = _grey_info.clip_r - 1;

    dwidth = _grey_info.cb_width;
    dst    = &_grey_info.curbuffer[
                    _GREY_MULUQ(dwidth, vp->y - _grey_info.cb_y + y) +
                    vp->x - _grey_info.cb_x + x1];

    if (fillopt)
        rb->memset(dst, value, x2 - x1 + 1);
    else  /* DRMODE_COMPLEMENT */
    {
        unsigned char *dst_end = dst + x2 - x1;
        do
            *dst = ~(*dst);
        while (++dst <= dst_end);
    }
}

/* Draw a vertical line (optimised) */
void grey_vline(int x, int y1, int y2)
{
    struct viewport *vp = _grey_info.vp;
    int y;
    unsigned char *dst, *dst_end;
    void (*pfunc)(unsigned char *address);
    int dwidth;
    
    /* direction flip */
    if (y2 < y1)
    {
        y = y1;
        y1 = y2;
        y2 = y;
    }

    /* nothing to draw? */
    if (x < _grey_info.clip_l || x >= _grey_info.clip_r ||
        y1 >= _grey_info.clip_b || y2 < _grey_info.clip_t)
        return;
    
    /* clipping */
    if (y1 < _grey_info.clip_t)
        y1 = _grey_info.clip_t;
    if (y2 >= _grey_info.clip_b)
        y2 = _grey_info.clip_b - 1;

    dwidth = _grey_info.cb_width;
    pfunc  = _grey_pixelfuncs[vp->drawmode];
    dst    = &_grey_info.curbuffer[
                    _GREY_MULUQ(dwidth, vp->y - _grey_info.cb_y + y1) +
                                vp->x - _grey_info.cb_x + x];

    dst_end = dst + _GREY_MULUQ(dwidth, y2 - y1);
    do
    {
        pfunc(dst);
        dst += dwidth;
    }
    while (dst <= dst_end);
}

/* Draw a filled triangle */
void grey_filltriangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
    int x, y;
    long fp_x1, fp_x2, fp_dx1, fp_dx2;

    /* sort vertices by increasing y value */
    if (y1 > y3)
    {
        if (y2 < y3)       /* y2 < y3 < y1 */
        {
            x = x1; x1 = x2; x2 = x3; x3 = x;
            y = y1; y1 = y2; y2 = y3; y3 = y;
        }
        else if (y2 > y1)  /* y3 < y1 < y2 */
        {
            x = x1; x1 = x3; x3 = x2; x2 = x;
            y = y1; y1 = y3; y3 = y2; y2 = y;
        }
        else               /* y3 <= y2 <= y1 */
        {
            x = x1; x1 = x3; x3 = x;
            y = y1; y1 = y3; y3 = y;
        }
    }
    else
    {
        if (y2 < y1)       /* y2 < y1 <= y3 */
        {
            x = x1; x1 = x2; x2 = x;
            y = y1; y1 = y2; y2 = y;
        }
        else if (y2 > y3)  /* y1 <= y3 < y2 */
        {
            x = x2; x2 = x3; x3 = x;
            y = y2; y2 = y3; y3 = y;
        }
        /* else already sorted */
    }

    if (y1 < y3)  /* draw */
    {
        fp_dx1 = ((x3 - x1) << 16) / (y3 - y1);
        fp_x1  = (x1 << 16) + (1<<15) + (fp_dx1 >> 1);

        if (y1 < y2)  /* first part */
        {
            fp_dx2 = ((x2 - x1) << 16) / (y2 - y1);
            fp_x2  = (x1 << 16) + (1<<15) + (fp_dx2 >> 1);
            for (y = y1; y < y2; y++)
            {
                grey_hline(fp_x1 >> 16, fp_x2 >> 16, y);
                fp_x1 += fp_dx1;
                fp_x2 += fp_dx2;
            }
        }
        if (y2 < y3)  /* second part */
        {
            fp_dx2 = ((x3 - x2) << 16) / (y3 - y2);
            fp_x2 = (x2 << 16) + (1<<15) + (fp_dx2 >> 1);
            for (y = y2; y < y3; y++)
            {
                grey_hline(fp_x1 >> 16, fp_x2 >> 16, y);
                fp_x1 += fp_dx1;
                fp_x2 += fp_dx2;
            }
        }
    }
}

/* Draw a rectangular box */
void grey_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;

    grey_vline(x, y, y2);
    grey_vline(x2, y, y2);
    grey_hline(x, x2, y);
    grey_hline(x, x2, y2);
}

/* Fill a rectangular area */
void grey_fillrect(int x, int y, int width, int height)
{
    struct viewport *vp = _grey_info.vp;
    int value = 0;
    unsigned char *dst, *dst_end;
    bool fillopt = false;
    int dwidth;

    /* drawmode and optimisation */
    if (vp->drawmode & DRMODE_INVERSEVID)
    {
        if (vp->drawmode & DRMODE_BG)
        {
            fillopt = true;
            value = vp->bg_pattern;
        }
    }
    else
    {
        if (vp->drawmode & DRMODE_FG)
        {
            fillopt = true;
            value = vp->fg_pattern;

        }
    }
    if (!fillopt && vp->drawmode != DRMODE_COMPLEMENT)
        return;

    /* clipping */
    if (x < _grey_info.clip_l)
    {
        width += x - _grey_info.clip_l;
        x = _grey_info.clip_l;
    }

    if (x + width > _grey_info.clip_r)
        width = _grey_info.clip_r - x;

    if (width <= 0)
        return;

    if (y < _grey_info.clip_t)
    {
        height += y - _grey_info.clip_t;
        y = _grey_info.clip_t;
    }

    if (y + height > _grey_info.clip_b)
        height = _grey_info.clip_b - y;

    if (height <= 0)
        return;
    
    dwidth  = _grey_info.cb_width;
    dst     = &_grey_info.curbuffer[
                _GREY_MULUQ(dwidth, _grey_info.vp->y - _grey_info.cb_y + y) +
                _grey_info.vp->x - _grey_info.cb_x + x];
    dst_end = dst + _GREY_MULUQ(dwidth, height);

    do
    {
        if (fillopt)
            rb->memset(dst, value, width);
        else  /* DRMODE_COMPLEMENT */
        {
            unsigned char *dst_row = dst;
            unsigned char *row_end = dst_row + width;

            do
                *dst_row = ~(*dst_row);
            while (++dst_row < row_end);
        }
        dst += dwidth;
    }
    while (dst < dst_end);
}

/* About Rockbox' internal monochrome bitmap format:
 *
 * A bitmap contains one bit for every pixel that defines if that pixel is
 * foreground (1) or background (0). Bits within a byte are arranged
 * vertically, LSB at top.
 * The bytes are stored in row-major order, with byte 0 being top left,
 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
 * 0..7, the second row defines pixel row 8..15 etc. */

/* Draw a partial monochrome bitmap */
void grey_mono_bitmap_part(const unsigned char *src, int src_x, int src_y,
                           int stride, int x, int y, int width, int height)
{
    struct viewport *vp = _grey_info.vp;
    const unsigned char *src_end;
    unsigned char *dst, *dst_end;
    unsigned dmask = 0x100; /* bit 8 == sentinel */
    int drmode = vp->drawmode;
    int dwidth;

    /* clipping */
    if (x < _grey_info.clip_l)
    {
        int dx = x - _grey_info.clip_l;
        width += dx;
        src_x -= dx;
        x = _grey_info.clip_l;
    }

    if (x + width > _grey_info.clip_r)
        width = _grey_info.clip_r - x;

    if (width <= 0)
        return;

    if (y < _grey_info.clip_t)
    {
        int dy = y - _grey_info.clip_t;
        height += dy;
        src_y  += dy;
        y = _grey_info.clip_t;
    }

    if (y + height > _grey_info.clip_b)
        height = _grey_info.clip_b - y;

    if (height <= 0)
        return;

    src    += _GREY_MULUQ(stride, src_y >> 3) + src_x; /* move starting point */
    src_y  &= 7;
    src_end = src + width;
    dwidth  = _grey_info.cb_width;
    dst     = &_grey_info.curbuffer[
                        _GREY_MULUQ(dwidth, vp->y - _grey_info.cb_y + y) +
                                    vp->x - _grey_info.cb_x + x];
    dst_end = dst + _GREY_MULUQ(dwidth, height);

    if (drmode & DRMODE_INVERSEVID)
    {
        dmask = 0x1ff;          /* bit 8 == sentinel */
        drmode &= DRMODE_SOLID; /* mask out inversevid */
    }

    do
    {
        const unsigned char *src_col = src++;
        unsigned char *dst_col = dst++;
        unsigned data = (*src_col ^ dmask) >> src_y;
        int fg, bg;

#define UPDATE_SRC  do {                  \
            data >>= 1;                   \
            if (data == 0x001) {          \
                src_col += stride;        \
                data = *src_col ^ dmask;  \
            }                             \
        } while (0)

        switch (drmode)
        {
          case DRMODE_COMPLEMENT:
            do
            {
                if (data & 0x01)
                    *dst_col = ~(*dst_col);

                dst_col += dwidth;
                UPDATE_SRC;
            }
            while (dst_col < dst_end);
            break;

          case DRMODE_BG:
            bg = vp->bg_pattern;
            do
            {
                if (!(data & 0x01))
                    *dst_col = bg;

                dst_col += dwidth;
                UPDATE_SRC;
            }
            while (dst_col < dst_end);
            break;

          case DRMODE_FG:
            fg = vp->fg_pattern;
            do
            {
                if (data & 0x01)
                    *dst_col = fg;

                dst_col += dwidth;
                UPDATE_SRC;
            }
            while (dst_col < dst_end);
            break;

          case DRMODE_SOLID:
            fg = vp->fg_pattern;
            bg = vp->bg_pattern;
            do
            {
                *dst_col = (data & 0x01) ? fg : bg;
                dst_col += dwidth;
                UPDATE_SRC;
            }
            while (dst_col < dst_end);
            break;
        }
    }
    while (src < src_end);
}

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

/* Draw a partial greyscale bitmap, canonical format */
void grey_gray_bitmap_part(const unsigned char *src, int src_x, int src_y,
                           int stride, int x, int y, int width, int height)
{
    unsigned char *dst, *dst_end;
    int dwidth;

    /* clipping */
    if (x < _grey_info.clip_l)
    {
        int dx = x - _grey_info.clip_l;
        width += dx;
        src_x -= dx;
        x = _grey_info.clip_l;
    }

    if (x + width > _grey_info.clip_r)
        width = _grey_info.clip_r - x;

    if (width <= 0)
        return;

    if (y < 0)
    {
        int dy = y - _grey_info.clip_t;
        height += dy;
        src_y -= dy;
        y = _grey_info.clip_t;
    }

    if (y + height > _grey_info.clip_b)
        height = _grey_info.clip_b - y;

    if (height <= 0)
        return;

    dwidth = _grey_info.cb_width;
    src   += _GREY_MULUQ(stride, src_y) + src_x; /* move starting point */
    dst    = &_grey_info.curbuffer[
                _GREY_MULUQ(dwidth, _grey_info.vp->y - _grey_info.cb_y + y) +
                _grey_info.vp->x - _grey_info.cb_x + x];
    dst_end = dst + _GREY_MULUQ(dwidth, height);

    do
    {
        rb->memcpy(dst, src, width);
        dst += dwidth;
        src += stride;
    }
    while (dst < dst_end);
}

/* Draw a full greyscale bitmap, canonical format */
void grey_gray_bitmap(const unsigned char *src, int x, int y, int width,
                      int height)
{
    grey_gray_bitmap_part(src, 0, 0, width, x, y, width, height);
}

/* Put a string at a given pixel position, skipping first ofs pixel columns */
void grey_putsxyofs(int x, int y, int ofs, const unsigned char *str)
{
    int ch;
    unsigned short *ucs;
    struct font* pf;

    if (_grey_info.clip_b <= _grey_info.clip_t)
        return;

    pf = rb->font_get(_grey_info.vp->font);
    ucs = rb->bidi_l2v(str, 1);

    while ((ch = *ucs++) != 0 && x < _grey_info.clip_r)
    {
        int width;
        const unsigned char *bits;

        /* get proportional width and glyph bits */
        width = rb->font_get_width(pf, ch);

        if (ofs > width)
        {
            ofs -= width;
            continue;
        }

        bits = rb->font_get_bits(pf, ch);

        grey_mono_bitmap_part(bits, ofs, 0, width, x, y, width - ofs, pf->height);
        
        x += width - ofs;
        ofs = 0;
    }
}

/* Put a string at a given pixel position */
void grey_putsxy(int x, int y, const unsigned char *str)
{
    grey_putsxyofs(x, y, 0, str);
}

/*** Unbuffered drawing functions ***/

/* Clear the greyscale display (sets all pixels to white) */
void grey_ub_clear_display(void)
{
    struct viewport *vp = &_grey_default_vp;
    int value = _grey_info.gvalue[(vp->drawmode & DRMODE_INVERSEVID) ?
                                   vp->fg_pattern : vp->bg_pattern];

    rb->memset(_grey_info.values, value,
                          _GREY_MULUQ(_grey_info.width, _grey_info.height));
#ifdef SIMULATOR
    rb->sim_lcd_ex_update_rect(_grey_info.x, _grey_info.y,
                                          _grey_info.width, _grey_info.height);
#endif
}

/* Assembler optimised helper function for copying a single line to the 
 * greyvalue buffer. */
void _grey_line1(int width, unsigned char *dst, const unsigned char *src,
                 const unsigned char *lut);

/* Draw a partial greyscale bitmap, canonical format */
void grey_ub_gray_bitmap_part(const unsigned char *src, int src_x, int src_y,
                              int stride, int x, int y, int width, int height)
{
    int yc, ye;
    unsigned char *dst;

    /* nothing to draw? */
    if ((width <= 0) || (height <= 0) || (x >= _grey_info.width)
        || (y >= _grey_info.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 > _grey_info.width)
        width = _grey_info.width - x;
    if (y + height > _grey_info.height)
        height = _grey_info.height - y;

    src += _GREY_MULUQ(stride, src_y) + src_x; /* move starting point */ 
    yc = y;
    ye = y + height;
    dst = _grey_info.values + (x << _GREY_BSHIFT);

    do
    {
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
        int idx = _GREY_MULUQ(_grey_info.width, yc);
#else /* vertical packing or vertical interleaved */
        int idx = _GREY_MULUQ(_grey_info.width, yc & ~_GREY_BMASK)
                + (~yc & _GREY_BMASK);
#endif /* LCD_PIXELFORMAT */

#if ((LCD_PIXELFORMAT == VERTICAL_PACKING) && (LCD_DEPTH == 2) && defined(CPU_COLDFIRE)) \
 || ((LCD_PIXELFORMAT == VERTICAL_INTERLEAVED) && defined(CPU_COLDFIRE))
        _grey_line1(width, dst + idx, src, _grey_info.gvalue);
#else
        unsigned char *dst_row = dst + idx;
        const unsigned char *src_row = src;
        const unsigned char *src_end = src + width;

        do
        {
            *dst_row = _grey_info.gvalue[*src_row++];
            dst_row += _GREY_BSIZE;
        }
        while (src_row < src_end);
#endif
        
        src += stride;
    }
    while (++yc < ye);
#ifdef SIMULATOR
    rb->sim_lcd_ex_update_rect(_grey_info.x + x, _grey_info.y + y,
                                          width, height);
#endif
}

/* Draw a full greyscale bitmap, canonical format */
void grey_ub_gray_bitmap(const unsigned char *src, int x, int y, int width,
                         int height)
{
    grey_ub_gray_bitmap_part(src, 0, 0, width, x, y, width, height);
}

static void output_row_grey_8(uint32_t row, void * row_in,
                              struct scaler_context *ctx)
{
    uint8_t *dest = (uint8_t*)ctx->bm->data + ctx->bm->width * row;
    rb->memcpy(dest, row_in, ctx->bm->width);
}

static void output_row_grey_32(uint32_t row, void * row_in,
                               struct scaler_context *ctx)
{
    int col;
    uint32_t *qp = (uint32_t*)row_in;
    uint8_t *dest = (uint8_t*)ctx->bm->data + ctx->bm->width * row;
    for (col = 0; col < ctx->bm->width; col++)
        *dest++ = SC_OUT(*qp++, ctx);
}

static unsigned int get_size_grey(struct bitmap *bm)
{
    return bm->width * bm->height;
}

const struct custom_format format_grey = {
    .output_row_8 = output_row_grey_8,
    .output_row_32 = output_row_grey_32,
    .get_size = get_size_grey
};