summaryrefslogtreecommitdiff
path: root/apps/recorder/resize.c
blob: e41501032a690e6448164bda4deabff78f4dd3d6 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Akio Idehara, Andrew Mahone
 *
 * 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.
 *
 ****************************************************************************/

/*
 * Implementation of area average and linear row and vertical scalers, and
 * nearest-neighbor grey scaler (C) 2008 Andrew Mahone
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <general.h>
#include "inttypes.h"
#include "debug.h"
#include "lcd.h"
#include "file.h"
#ifdef HAVE_REMOTE_LCD
#include "lcd-remote.h"
#endif
#ifdef ROCKBOX_DEBUG_SCALERS
#define SDEBUGF DEBUGF
#else
#define SDEBUGF(...)
#endif
#ifndef __PCTOOL__
#include "config.h"
#include "system.h"
#include "bmp.h"
#include "resize.h"
#include "resize.h"
#include "debug.h"
#else
#undef DEBUGF
#define DEBUGF(...)
#endif

/* All of these scalers use variations of Bresenham's algorithm to convert from
   their input to output coordinates. The color scalers have the error value
   shifted so that it is a useful input to the scaling algorithm.
*/

#ifdef HAVE_LCD_COLOR
/* dither + pack on channel of RGB565, R an B share a packing macro */
#define PACKRB(v, delta)  ((31 * v + (v >> 3) + delta) >> 8)
#define PACKG(g, delta) ((63 * g + (g >> 2) + delta) >> 8)

/* read new img_part unconditionally, return false on failure */
#define FILL_BUF_INIT(img_part, store_part, args) { \
    img_part = store_part(args); \
    if (img_part == NULL) \
        return false; \
}

/* read new img_part if current one is empty, return false on failure */
#define FILL_BUF(img_part, store_part, args) { \
    if (img_part->len == 0) \
        img_part = store_part(args); \
    if (img_part == NULL) \
        return false; \
}

struct uint32_rgb {
    uint32_t r;
    uint32_t g;
    uint32_t b;
};

struct scaler_context {
    uint32_t divmul;
    uint32_t round;
    struct img_part* (*store_part)(void *);
    long last_tick;
    unsigned char *buf;
    int len;
    void *args;
};

/* Set up rounding and scale factors for horizontal area scaler */
static void scale_h_area_setup(struct bitmap *bm, struct dim *src,
                               struct scaler_context *ctx)
{
    (void) bm;
/* sum is output value * src->width */
    ctx->divmul = ((src->width - 1 + 0x80000000U) / src->width) << 1;
    ctx->round = (src->width + 1) >> 1;
}

/* horizontal area average scaler */
static bool scale_h_area(struct bitmap *bm, struct dim *src,
                         struct uint32_rgb *out_line,
                         struct scaler_context *ctx, bool accum)
{
    SDEBUGF("scale_h_area\n");
    unsigned int ix, ox, oxe, mul;
    struct uint32_rgb rgbvalacc = { 0, 0, 0 }, 
                      rgbvaltmp = { 0, 0, 0 };
    struct img_part *part;
    FILL_BUF_INIT(part,ctx->store_part,ctx->args);
    ox = 0;
    oxe = 0;
    mul = 0;
    for (ix = 0; ix < (unsigned int)src->width; ix++)
    {
        oxe += bm->width;
        /* end of current area has been reached */
        if (oxe >= (unsigned int)src->width)
        {
            /* yield if we haven't since last tick */
            if (ctx->last_tick != current_tick)
            {
                yield();
                ctx->last_tick = current_tick;
            }
            /* "reset" error, which now represents partial coverage of next
               pixel by the next area
            */
            oxe -= src->width;
            /* add saved partial pixel from start of area */
            rgbvalacc.r = rgbvalacc.r * bm->width + rgbvaltmp.r * mul;
            rgbvalacc.g = rgbvalacc.g * bm->width + rgbvaltmp.g * mul;
            rgbvalacc.b = rgbvalacc.b * bm->width + rgbvaltmp.b * mul;
            /* fill buffer if needed */
            FILL_BUF(part,ctx->store_part,ctx->args);
            /* get new pixel , then add its partial coverage to this area */
            rgbvaltmp.r = part->buf->red;
            rgbvaltmp.g = part->buf->green;
            rgbvaltmp.b = part->buf->blue;
            part->buf++;
            part->len--;
            mul = bm->width - oxe;
            rgbvalacc.r += rgbvaltmp.r * mul;
            rgbvalacc.g += rgbvaltmp.g * mul;
            rgbvalacc.b += rgbvaltmp.b * mul;
            /* round, divide, and either store or accumulate to output row */
            out_line[ox].r = (accum ? out_line[ox].r : 0) +
                             ((rgbvalacc.r + ctx->round) *
                             (uint64_t)ctx->divmul >> 32);
            out_line[ox].g = (accum ? out_line[ox].g : 0) +
                             ((rgbvalacc.g + ctx->round) *
                             (uint64_t)ctx->divmul >> 32);
            out_line[ox].b = (accum ? out_line[ox].b : 0) +
                             ((rgbvalacc.b + ctx->round) *
                             (uint64_t)ctx->divmul >> 32);
            /* reset accumulator */
            rgbvalacc.r = 0;
            rgbvalacc.g = 0;
            rgbvalacc.b = 0;
            mul = bm->width - mul;
            ox += 1;
        /* inside an area */
        } else {
            /* fill buffer if needed */
            FILL_BUF(part,ctx->store_part,ctx->args);
            /* add pixel value to accumulator */
            rgbvalacc.r += part->buf->red;
            rgbvalacc.g += part->buf->green;
            rgbvalacc.b += part->buf->blue;
            part->buf++;
            part->len--;
        }
    }
    return true;
}

/* vertical area average scaler */
static bool scale_v_area(struct bitmap *bm, bool dither, struct dim *src,
                         struct rowset *rset,
                         bool (*h_scaler)(struct bitmap*, struct dim*,
                                          struct uint32_rgb*,
                                          struct scaler_context*, bool),
                         struct scaler_context *ctx)
{
    uint32_t mul, divmul, x, oy, iy, oye, round;
    int delta = 127, r, g, b;
    fb_data *row, *pix;

    /* Set up rounding and scale factors */
    divmul = ((src->height - 1 + 0x80000000U) / src->height) << 1;
    round = (src->height + 1) >> 1;
    mul = 0;
    oy = 0;
    oye = 0;
    struct uint32_rgb *rowacc = (struct uint32_rgb *)(ctx->buf),
                      *rowtmp = rowacc + bm->width;

    SDEBUGF("scale_v_area\n");
    /* zero the accumulator and temp rows */
    memset((void *)ctx->buf, 0, bm->width * 2 * sizeof(struct uint32_rgb));
    row = (fb_data *)(bm->data) + bm->width * rset->rowstart;
    for (iy = 0; iy < (unsigned int)src->height; iy++)
    {
        oye += bm->height;
        /* end of current area has been reached */
        if (oye >= (unsigned int)src->height)
        {
            /* "reset" error, which now represents partial coverage of the next
               row by the next area
            */
            oye -= src->height;
            /* add stored partial row to accumulator */
            for (x = 0; x < 3 *(unsigned int)bm->width; x++)
                ((uint32_t*)rowacc)[x] = ((uint32_t*)rowacc)[x] *
                                        bm->height + mul *
                                        ((uint32_t*)rowtmp)[x];
            /* store new scaled row in temp row */
            if(!h_scaler(bm, src, rowtmp, ctx, false))
                return false;
            /* add partial coverage by new row to this area, then round and
               scale to final value
            */
            mul = bm->height - oye;
            for (x = 0; x < 3 *(unsigned int)bm->width; x++)
            {
                ((uint32_t*)rowacc)[x] += mul * ((uint32_t*)rowtmp)[x];
                ((uint32_t*)rowacc)[x] = (round +
                                        ((uint32_t*)rowacc)[x]) *
                                        (uint64_t)divmul >> 32;
            }
            /* convert to RGB565 in output bitmap */
            pix = row;
            for (x = 0; x < (unsigned int)bm->width; x++)
            {
                if (dither)
                    delta = dither_mat(x & 0xf, oy & 0xf);
                r = PACKRB(rowacc[x].r,delta);
                g = PACKG(rowacc[x].g,delta);
                b = PACKRB(rowacc[x].b,delta);
                *pix++ = LCD_RGBPACK_LCD(r, g, b);
            }
            /* clear accumulator row, store partial coverage for next row */
            memset((void *)rowacc, 0, bm->width * sizeof(struct uint32_rgb));
            mul = oye;
            row += bm->width * rset->rowstep;
            oy += 1;
        /* inside an area */
        } else {
            /* accumulate new scaled row to rowacc */
            if (!h_scaler(bm, src, rowacc, ctx, true))
                return false;
        }
    }
    return true;
}

#ifdef HAVE_UPSCALER
/* Set up rounding and scale factors for the horizontal scaler. The divisor
   is bm->width - 1, so that the first and last pixels in the row align
   exactly between input and output
*/
static void scale_h_linear_setup(struct bitmap *bm, struct dim *src,
                                   struct scaler_context *ctx)
{
    (void) src;
    ctx->divmul = ((bm->width - 2 + 0x80000000U) / (bm->width - 1)) << 1;
    ctx->round = bm->width >> 1;
}

/* horizontal linear scaler */
static bool scale_h_linear(struct bitmap *bm, struct dim *src,
                             struct uint32_rgb *out_line,
                             struct scaler_context *ctx, bool accum) 
{
    unsigned int ix, ox, ixe;
    /* type x = x is an ugly hack for hiding an unitialized data warning. The
       values are conditionally initialized before use, but other values are
       set such that this will occur before these are used.
    */
    struct uint32_rgb rgbval=rgbval, rgbinc=rgbinc;
    struct img_part *part;
    SDEBUGF("scale_h_linear\n");
    FILL_BUF_INIT(part,ctx->store_part,ctx->args);
    ix = 0;
    /* The error is set so that values are initialized on the first pass. */
    ixe = bm->width - 1;
    for (ox = 0; ox < (uint32_t)bm->width; ox++) {
        if (ixe >= ((uint32_t)bm->width - 1))
        {
            /* yield once each tick */
            if (ctx->last_tick != current_tick)
            {
                yield();
                ctx->last_tick = current_tick;
            }
            /* Store the new "current" pixel value in rgbval, and the color 
               step value in rgbinc.
            */
            ixe -= (bm->width - 1);
            rgbinc.r = -(part->buf->red);
            rgbinc.g = -(part->buf->green);
            rgbinc.b = -(part->buf->blue);
            rgbval.r = (part->buf->red) * (bm->width - 1);
            rgbval.g = (part->buf->green) * (bm->width - 1);
            rgbval.b = (part->buf->blue) * (bm->width - 1);
            ix += 1;
            /* If this wasn't the last pixel, add the next one to rgbinc. */
            if (ix < (uint32_t)src->width) {
                part->buf++;
                part->len--;
                /* Fetch new pixels if needed */
                FILL_BUF(part,ctx->store_part,ctx->args);
                rgbinc.r += part->buf->red;
                rgbinc.g += part->buf->green;
                rgbinc.b += part->buf->blue;
                /* Add a partial step to rgbval, in this pixel isn't precisely
                   aligned with the new source pixel
                */
                rgbval.r += rgbinc.r * ixe;
                rgbval.g += rgbinc.g * ixe;
                rgbval.b += rgbinc.b * ixe;
            }
            /* Now multiple the color increment to its proper value */
            rgbinc.r *= src->width - 1;
            rgbinc.g *= src->width - 1;
            rgbinc.b *= src->width - 1;
        }
        /* round and scale values, and accumulate or store to output */
        out_line[ox].r = (accum ? out_line[ox].r : 0) +
                         ((rgbval.r + ctx->round) *
                         (uint64_t)ctx->divmul >> 32);
        out_line[ox].g = (accum ? out_line[ox].g : 0) +
                         ((rgbval.g + ctx->round) *
                         (uint64_t)ctx->divmul >> 32);
        out_line[ox].b = (accum ? out_line[ox].b : 0) +
                         ((rgbval.b + ctx->round) *
                         (uint64_t)ctx->divmul >> 32);
        rgbval.r += rgbinc.r;
        rgbval.g += rgbinc.g;
        rgbval.b += rgbinc.b;
        ixe += src->width - 1;
    }
    return true;
}

/* vertical linear scaler */
static bool scale_v_linear(struct bitmap *bm, bool dither, struct dim *src,
                           struct rowset *rset,
                           bool (*h_scaler)(struct bitmap*, struct dim*,
                                            struct uint32_rgb*,
                                            struct scaler_context*, bool),
                           struct scaler_context *ctx)
{
    uint32_t mul, divmul, x, oy, iy, iye, round;
    int delta = 127;
    struct uint32_rgb p;
    fb_data *row, *pix;
    /* Set up scale and rounding factors, the divisor is bm->height - 1 */
    divmul = ((bm->height - 2 + 0x80000000U) / (bm->height - 1)) << 1;
    round = bm->height >> 1; 
    mul = 0;
    iy = 0;
    iye = bm->height - 1;
    /* Set up our two temp buffers. The names are generic because they'll be
       swapped each time a new input row is read
    */
    struct uint32_rgb *crow1 = (struct uint32_rgb *)(ctx->buf),
                      *crow2 = crow1 + bm->width,
                      *t;

    SDEBUGF("scale_v_linear\n");
    row = (fb_data *)(bm->data) + bm->width * rset->rowstart;
    /* get first scaled row in crow2 */
    if(!h_scaler(bm, src, crow2, ctx, false))
        return false;
    for (oy = 0; oy < (uint32_t)bm->height; oy++)
    {
        if (iye >= (uint32_t)bm->height - 1)
        {
            /* swap temp rows, then read another row into crow2 */
            t = crow2;
            crow2 = crow1;
            crow1 = t;
            iye -= bm->height - 1;
            iy += 1;
            if (iy < (uint32_t)src->height)
            {
                if (!h_scaler(bm, src, crow2, ctx, false))
                    return false;
            }
        }
        pix = row;
        for (x = 0; x < (uint32_t)bm->width; x++)
        {
            /* iye and bm-height - 1 - iye represent the contribution of each
               row to the output. Calculate their weighted sum, then round and
               scale it.
            */
            p.r = (crow1[x].r * (bm->height - 1 - iye) +
                  crow2[x].r * iye + round) * (uint64_t)divmul >> 32;
            p.g = (crow1[x].g * (bm->height - 1 - iye) +
                  crow2[x].g * iye + round) * (uint64_t)divmul >> 32;
            p.b = (crow1[x].b * (bm->height - 1 - iye) +
                  crow2[x].b * iye + round) * (uint64_t)divmul >> 32;
            /* dither and pack pixels to output */
            if (dither)
                delta = dither_mat(x & 0xf, oy & 0xf);
            p.r = PACKRB(p.r,delta);
            p.g = PACKG(p.g,delta);
            p.b = PACKRB(p.b,delta);
            *pix++ = LCD_RGBPACK_LCD(p.r, p.g, p.b);
        }
        row += bm->width * rset->rowstep;
        iye += src->height - 1;
    }
    return true;
}
#endif /* HAVE_UPSCALER */
#endif /* HAVE_LCD_COLOR */

/* docs for this are still TODO, but it's Bresenham's again, used to skip or
   repeat input pixels, and with the *ls values being used for "long steps"
   that skip all the way, or nearly all the way, to the next transition of
   the associated value.
*/
#if LCD_DEPTH < 8 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH < 8)
/* nearest-neighbor up/down/non-scaler */
static inline bool scale_nearest(struct bitmap *bm,
                                struct dim *src,
                                struct rowset *rset,
                                bool remote, bool dither,
                                struct img_part* (*store_part)(void *args),
                                bool (*skip_lines)(void *args, unsigned int),
                                void *args)
{
    const int sw = src->width;
    const int sh = src->height;
    const int dw = bm->width;
    const int dh = bm->height;
    unsigned char *bitmap = bm->data;
    const int rowstep = rset->rowstep;
    const int rowstart = rset->rowstart;
    const int rowstop = rset->rowstop;
    const int fb_width = get_fb_width(bm, false);
    long last_tick = current_tick;
    /* yet/oyt will always be initialized before use, since they are set in the
       inside loop, and not used elsewhere until the end of the outside loop.
    */
    int ix, ox, lx, xe, iy, oy, ly, ye, yet = yet, oyt = oyt;
    int xelim, ixls, xels, yelim, iyls, yels, p;
    struct img_part *cur_part;
#ifndef HAVE_LCD_COLOR
    fb_data *dest = dest, *dest_t;
#endif
#ifdef HAVE_REMOTE_LCD
    fb_remote_data *rdest = rdest, *rdest_t;
#endif

    SDEBUGF("scale_nearest sw=%d sh=%d dw=%d dh=%d remote=%d\n", sw, sh, dw,
            dh, remote);
    ly = 0;
    iy = 0;
    ye = 0;
    xelim = sw == dw - 1 ? dw : dw - 1;
    ixls = xelim ? sw / xelim : 1;
    xels = sw - ixls * (xelim ? xelim : 1);
    yelim = sh == dh - 1 ? dh : dh - 1;
    iyls = yelim ? sh / yelim : 1;
    yels = iyls * (yelim ? yelim : 1);
    int delta = 127;
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING || \
    (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_PIXELFORMAT == HORIZONTAL_PACKING)
    uint8_t buf[4];
    int data, oxt;
#endif
#if LCD_PIXELFORMAT == VERTICAL_PACKING || \
    LCD_PIXELFORMAT == VERTICAL_INTERLEAVED || \
    (defined(HAVE_REMOTE_LCD) && \
    (LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED || \
    LCD_REMOTE_PIXELFORMAT == VERTICAL_PACKING))
    int bright, shift;
#endif
    for (oy = rowstart; oy != rowstop;) {
        SDEBUGF("oy=%d iy=%d\n", oy, iy);
        if (last_tick != current_tick)
        {
            yield();
            last_tick = current_tick;
        }
        if (iy > ly && !skip_lines(args, iy - ly - 1))
            return false;
        ly = iy;

        cur_part = store_part(args);
        if (cur_part == NULL)
            return false;

        lx = 0;
        ix = 0;
        xe = 0;
#if defined(HAVE_REMOTE_LCD) && !defined(HAVE_LCD_COLOR) 
        if(!remote)
#else
        (void)remote;
#endif
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
            dest = (fb_data *)bitmap + fb_width * oy;
#elif LCD_PIXELFORMAT == VERTICAL_PACKING
            dest = (fb_data *)bitmap + fb_width * (oy >> 2);
#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
            dest = (fb_data *)bitmap + fb_width * (oy >> 3);
#endif
#ifdef HAVE_REMOTE_LCD
#ifndef HAVE_LCD_COLOR 
        else
#endif
            rdest = (fb_remote_data *)bitmap + fb_width * (oy >> 3);
#endif
        for (ox = 0; ox < dw; ox++) {
            while (cur_part->len <= ix - lx)
            {
                lx += cur_part->len;
                cur_part = store_part(args);
                if (cur_part == NULL)
                    return false;
            }
            cur_part->len -= ix - lx;
            cur_part->buf += ix - lx;
            lx = ix;
#if defined(HAVE_REMOTE_LCD) && !defined(HAVE_LCD_COLOR) 
            if(!remote)
            {
#endif
#if LCD_PIXELFORMAT == HORIZONTAL_PACKING
                /* greyscale iPods */
                buf[ox & 3] = brightness(*(cur_part->buf));
                if ((ox & 3) == 3 || ox == dw - 1)
                {
                    dest_t = dest++;
                    oyt = oy;
                    yet = ye;
                    int xo = ox & ~3;
                    while(yet < dh)
                    {
                        data = 0;
                        for (oxt = 0; oxt < (ox & 3) + 1; oxt++)
                        {
                            if (dither)
                                delta = dither_mat(oyt & 0xf, (xo + oxt) & 0xf);
                            p = (3 * buf[oxt] + (buf[oxt] >> 6) + delta) >> 8;
                            data |= (~p & 3) << ((3 - oxt) << 1);
                        }
                        *dest_t = data;
                        dest_t += rowstep * fb_width;
                        yet += sh;
                        oyt += rowstep;
                    }
                }
#elif LCD_PIXELFORMAT == VERTICAL_PACKING
                /* iriver H1x0 */
                bright = brightness(*(cur_part->buf));
                dest_t = dest++;
                oyt = oy;
                yet = ye;
                while(yet < dh)
                {
                    shift = (oyt & 3) << 1;
                    if (dither)
                        delta = dither_mat(oyt & 0xf, ox & 0xf);

                    p = (3 * bright + (bright >> 6) + delta) >> 8;
                    *dest_t |= (~p & 3) << shift;
                    if ((rowstep > 0 && shift == 6) || shift == 0)
                        dest_t += rowstep * fb_width;
                    yet += sh;
                    oyt += rowstep;
                }
#elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
                bright = brightness(*(cur_part->buf));
                dest_t = dest++;
                oyt = oy;
                yet = ye;
                while(yet < dh)
                {
                    shift = oyt & 7;
                    if (dither)
                        delta = dither_mat(oyt & 0xf, ox & 0xf);

                    p = (3 * bright + (bright >> 6) + delta) >> 8;
                    *dest_t |= vi_pat(p) << shift;
                    if ((rowstep > 0 && shift == 7) || shift == 0)
                        dest_t += rowstep * fb_width;
                    yet += sh;
                    oyt += rowstep;
                }
#endif /* LCD_PIXELFORMAT */
#ifdef HAVE_REMOTE_LCD
#ifndef HAVE_LCD_COLOR
            } else
#endif
            {
#if LCD_REMOTE_PIXELFORMAT == VERTICAL_INTERLEAVED
                bright = brightness(*(cur_part->buf));
                rdest_t = rdest++;
                oyt = oy;
                yet = ye;
                while(yet < dh)
                {
                    shift = oyt & 7;
                    if (dither)
                        delta = dither_mat(oyt & 0xf, ox & 0xf);

                    p = (3 * bright + (bright >> 6) + delta) >> 8;
                    *rdest_t |= vi_pat(p) << shift;
                    if ((rowstep > 0 && shift == 7) || shift == 0)
                        rdest_t += rowstep * fb_width;
                    yet += sh;
                    oyt += rowstep;
                }
#else
                bright = brightness(*(cur_part->buf));
                rdest_t = rdest++;
                oyt = oy;
                yet = ye;
                while(yet < dh)
                {
                    shift = oyt & 7;
                    if (dither)
                        delta = dither_mat(oyt & 0xf, ox & 0xf);
                    p = (bright + delta) >> 8;
                    *rdest_t |= (~p & 1) << shift;
                    if ((rowstep > 0 && shift == 7) || shift == 0)
                        rdest_t += rowstep * fb_width;
                    yet += sh;
                    oyt += rowstep;
                }
#endif
            }
#endif
            xe += xels;
            ix += ixls;
            while (xe > xelim)
            {
                xe -= xelim;
                ix += 1;
            }
        }
        oy = oyt;
        ye = yet - yels;
        iy += iyls;
        while (ye > yelim)
        {
            ye -= yelim;
            iy += 1;
        }
    }
    return true;
}
#endif

int resize_on_load(struct bitmap *bm, bool dither, struct dim *src,
                   struct rowset *rset, bool remote,
#ifdef HAVE_LCD_COLOR
                   unsigned char *buf, unsigned int len,
#endif
                   struct img_part* (*store_part)(void *args),
                   bool (*skip_lines)(void *args, unsigned int lines), 
                   void *args)
{

#if defined(HAVE_LCD_COLOR) && !defined(HAVE_REMOTE_LCD)
    (void)skip_lines;
#endif
#ifdef HAVE_LCD_COLOR
#ifdef HAVE_REMOTE_LCD
        if (!remote)
#endif
        {
#ifdef HAVE_UPSCALER
            const int sw = src->width;
            const int sh = src->height;
            const int dw = bm->width;
            const int dh = bm->height;
#endif
            int ret;
            unsigned int needed = sizeof(struct uint32_rgb) * 2 * bm->width;
#if MAX_SC_STACK_ALLOC
            uint8_t sc_buf[(needed <= len  || needed > MAX_SC_STACK_ALLOC) ?
                           0 : needed];
            if (len && buf)
#endif
                len = (unsigned int)align_buffer(PUN_PTR(void**, &buf), len,
                                                 sizeof(uint32_t));
            if (needed > len)
            {
#if MAX_SC_STACK_ALLOC
                if (needed > MAX_SC_STACK_ALLOC)
                {
                    DEBUGF("unable to allocate required buffer: %d needed, "
                           "%d available, %d permitted from stack\n",
                           needed, len, MAX_SC_STACK_ALLOC);
                    return 0;
                }
                if (sizeof(sc_buf) < needed)
                {
                    DEBUGF("failed to allocate large enough buffer on stack: "
                           "%d needed, only got %d",
                           needed, MAX_SC_STACK_ALLOC);
                    return 0;
                }
#else
                DEBUGF("unable to allocate required buffer: %d needed, "
                       "%d available\n", needed, len);
                return 0;
#endif
            }

            bool (*h_scaler)(struct bitmap*, struct dim*,
                             struct uint32_rgb*,
                             struct scaler_context*, bool);
            struct scaler_context ctx;
            ctx.last_tick = current_tick;
            cpu_boost(true);
#ifdef HAVE_UPSCALER
            if (sw > dw)
            {
#endif
                h_scaler = scale_h_area;
                scale_h_area_setup(bm, src, &ctx); 
#ifdef HAVE_UPSCALER
            } else {
                h_scaler = scale_h_linear;
                scale_h_linear_setup(bm, src, &ctx);
            }
#endif
            ctx.store_part = store_part;
            ctx.args = args;
#if MAX_SC_STACK_ALLOC
            ctx.buf = needed > len ? sc_buf : buf;
#else
            ctx.buf = buf;
#endif
            ctx.len = len;
#ifdef HAVE_UPSCALER
            if (sh > dh)
#endif
                ret = scale_v_area(bm, dither, src, rset, h_scaler, &ctx);
#ifdef HAVE_UPSCALER
            else
                ret = scale_v_linear(bm, dither, src, rset, h_scaler, &ctx);
#endif
            cpu_boost(false);
            if (!ret)
                return 0;
        }
#ifdef HAVE_REMOTE_LCD
        else
#endif
#endif /* HAVE_LCD_COLOR */
#if !defined(HAVE_LCD_COLOR) || defined(HAVE_REMOTE_LCD)
        {
            if (!scale_nearest(bm, src, rset, remote, dither, store_part,
                skip_lines, args))
                return 0;
        }
#endif /* !HAVE_LCD_COLOR || HAVE_REMOTE_LCD*/
    return get_totalsize(bm, remote);
}