1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Akio Idehara, Andrew Mahone
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 * Implementation of area average and linear row and vertical scalers, and
24 * nearest-neighbor grey scaler (C) 2008 Andrew Mahone
26 * All files in this archive are subject to the GNU General Public License.
27 * See the file COPYING in the source tree root for full license agreement.
29 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
30 * KIND, either express or implied.
32 ****************************************************************************/
42 #ifdef HAVE_REMOTE_LCD
43 #include "lcd-remote.h"
45 #ifdef ROCKBOX_DEBUG_SCALERS
46 #define SDEBUGF DEBUGF
62 /* calculate the maximum dimensions which will preserve the aspect ration of
63 src while fitting in the constraints passed in dst, and store result in dst,
64 returning 0 if rounding and 1 if not rounding.
66 int recalc_dimension(struct dim
*dst
, struct dim
*src
)
70 dst
->width
= LCD_WIDTH
;
72 dst
->height
= LCD_HEIGHT
;
74 if (dst
->width
> src
->width
|| dst
->height
> src
->height
)
76 dst
->width
= src
->width
;
77 dst
->height
= src
->height
;
79 if (src
->width
== dst
->width
&& src
->height
== dst
->height
)
82 tmp
= (src
->width
* dst
->height
+ (src
->height
>> 1)) / src
->height
;
84 dst
->height
= (src
->height
* dst
->width
+ (src
->width
>> 1))
88 return src
->width
== dst
->width
&& src
->height
== dst
->height
;
91 /* All of these scalers use variations of Bresenham's algorithm to convert from
92 their input to output coordinates. The error value is shifted from the
93 "classic" version such that it is a useful input to the scaling calculation.
97 /* dither + pack on channel of RGB565, R an B share a packing macro */
98 #define PACKRB(v, delta) ((31 * v + (v >> 3) + delta) >> 8)
99 #define PACKG(g, delta) ((63 * g + (g >> 2) + delta) >> 8)
102 /* read new img_part unconditionally, return false on failure */
103 #define FILL_BUF_INIT(img_part, store_part, args) { \
104 img_part = store_part(args); \
105 if (img_part == NULL) \
109 /* read new img_part if current one is empty, return false on failure */
110 #define FILL_BUF(img_part, store_part, args) { \
111 if (img_part->len == 0) \
112 img_part = store_part(args); \
113 if (img_part == NULL) \
117 /* struct which containers various parameters shared between vertical scaler,
118 horizontal scaler, and row output
120 struct scaler_context
{
129 struct img_part
* (*store_part
)(void *);
130 void (*output_row
)(uint32_t,void*,struct scaler_context
*);
131 bool (*h_scaler
)(void*,struct scaler_context
*, bool);
134 /* Set up rounding and scale factors for horizontal area scaler */
135 static inline void scale_h_area_setup(struct scaler_context
*ctx
)
137 /* sum is output value * src->width */
138 SDEBUGF("scale_h_area_setup\n");
139 ctx
->divisor
= ctx
->src
->width
;
142 /* horizontal area average scaler */
143 static bool scale_h_area(void *out_line_ptr
,
144 struct scaler_context
*ctx
, bool accum
)
146 SDEBUGF("scale_h_area\n");
147 unsigned int ix
, ox
, oxe
, mul
;
148 #ifdef HAVE_LCD_COLOR
149 struct uint32_rgb rgbvalacc
= { 0, 0, 0 },
150 rgbvaltmp
= { 0, 0, 0 },
151 *out_line
= (struct uint32_rgb
*)out_line_ptr
;
153 uint32_t acc
= 0, tmp
= 0, *out_line
= (uint32_t*)out_line_ptr
;
155 struct img_part
*part
;
156 FILL_BUF_INIT(part
,ctx
->store_part
,ctx
->args
);
160 /* give other tasks a chance to run */
162 for (ix
= 0; ix
< (unsigned int)ctx
->src
->width
; ix
++)
164 oxe
+= ctx
->bm
->width
;
165 /* end of current area has been reached */
166 /* fill buffer if needed */
167 FILL_BUF(part
,ctx
->store_part
,ctx
->args
);
168 #ifdef HAVE_LCD_COLOR
169 if (oxe
>= (unsigned int)ctx
->src
->width
)
171 /* "reset" error, which now represents partial coverage of next
172 pixel by the next area
174 oxe
-= ctx
->src
->width
;
176 /* add saved partial pixel from start of area */
177 rgbvalacc
.r
= rgbvalacc
.r
* ctx
->bm
->width
+ rgbvaltmp
.r
* mul
;
178 rgbvalacc
.g
= rgbvalacc
.g
* ctx
->bm
->width
+ rgbvaltmp
.g
* mul
;
179 rgbvalacc
.b
= rgbvalacc
.b
* ctx
->bm
->width
+ rgbvaltmp
.b
* mul
;
181 /* get new pixel , then add its partial coverage to this area */
182 rgbvaltmp
.r
= part
->buf
->red
;
183 rgbvaltmp
.g
= part
->buf
->green
;
184 rgbvaltmp
.b
= part
->buf
->blue
;
185 mul
= ctx
->bm
->width
- oxe
;
186 rgbvalacc
.r
+= rgbvaltmp
.r
* mul
;
187 rgbvalacc
.g
+= rgbvaltmp
.g
* mul
;
188 rgbvalacc
.b
+= rgbvaltmp
.b
* mul
;
189 /* store or accumulate to output row */
192 rgbvalacc
.r
+= out_line
[ox
].r
;
193 rgbvalacc
.g
+= out_line
[ox
].g
;
194 rgbvalacc
.b
+= out_line
[ox
].b
;
196 out_line
[ox
].r
= rgbvalacc
.r
;
197 out_line
[ox
].g
= rgbvalacc
.g
;
198 out_line
[ox
].b
= rgbvalacc
.b
;
199 /* reset accumulator */
203 mul
= ctx
->bm
->width
- mul
;
207 /* add pixel value to accumulator */
208 rgbvalacc
.r
+= part
->buf
->red
;
209 rgbvalacc
.g
+= part
->buf
->green
;
210 rgbvalacc
.b
+= part
->buf
->blue
;
213 if (oxe
>= (unsigned int)ctx
->src
->width
)
215 /* "reset" error, which now represents partial coverage of next
216 pixel by the next area
218 oxe
-= ctx
->src
->width
;
220 /* add saved partial pixel from start of area */
221 acc
= acc
* ctx
->bm
->width
+ tmp
* mul
;
223 /* get new pixel , then add its partial coverage to this area */
225 mul
= ctx
->bm
->width
- oxe
;
227 /* round, divide, and either store or accumulate to output row */
233 /* reset accumulator */
235 mul
= ctx
->bm
->width
- mul
;
239 /* add pixel value to accumulator */
249 /* vertical area average scaler */
250 static inline bool scale_v_area(struct rowset
*rset
, struct scaler_context
*ctx
)
252 uint32_t mul
, x
, oy
, iy
, oye
;
254 /* Set up rounding and scale factors */
255 ctx
->divisor
*= ctx
->src
->height
;
256 ctx
->round
= ctx
->divisor
>> 1;
257 ctx
->divisor
= ((ctx
->divisor
- 1 + 0x80000000U
) / ctx
->divisor
) << 1;
261 #ifdef HAVE_LCD_COLOR
262 uint32_t *rowacc
= (uint32_t *) ctx
->buf
,
263 *rowtmp
= rowacc
+ 3 * ctx
->bm
->width
;
264 memset((void *)ctx
->buf
, 0, ctx
->bm
->width
* 2 * sizeof(struct uint32_rgb
));
266 uint32_t *rowacc
= (uint32_t *) ctx
->buf
,
267 *rowtmp
= rowacc
+ ctx
->bm
->width
;
268 memset((void *)ctx
->buf
, 0, ctx
->bm
->width
* 2 * sizeof(uint32_t));
270 SDEBUGF("scale_v_area\n");
271 /* zero the accumulator and temp rows */
272 for (iy
= 0; iy
< (unsigned int)ctx
->src
->height
; iy
++)
274 oye
+= ctx
->bm
->height
;
275 /* end of current area has been reached */
276 if (oye
>= (unsigned int)ctx
->src
->height
)
278 /* "reset" error, which now represents partial coverage of the next
281 oye
-= ctx
->src
->height
;
282 /* add stored partial row to accumulator */
283 #ifdef HAVE_LCD_COLOR
284 for (x
= 0; x
< 3 * (unsigned int)ctx
->bm
->width
; x
++)
286 for (x
= 0; x
< (unsigned int)ctx
->bm
->width
; x
++)
288 rowacc
[x
] = rowacc
[x
] * ctx
->bm
->height
+ mul
* rowtmp
[x
];
289 /* store new scaled row in temp row */
290 if(!ctx
->h_scaler(rowtmp
, ctx
, false))
292 /* add partial coverage by new row to this area, then round and
295 mul
= ctx
->bm
->height
- oye
;
296 #ifdef HAVE_LCD_COLOR
297 for (x
= 0; x
< 3 * (unsigned int)ctx
->bm
->width
; x
++)
299 for (x
= 0; x
< (unsigned int)ctx
->bm
->width
; x
++)
301 rowacc
[x
] += mul
* rowtmp
[x
];
302 ctx
->output_row(oy
, (void*)rowacc
, ctx
);
303 /* clear accumulator row, store partial coverage for next row */
304 #ifdef HAVE_LCD_COLOR
305 memset((void *)rowacc
, 0, ctx
->bm
->width
* sizeof(uint32_t) * 3);
307 memset((void *)rowacc
, 0, ctx
->bm
->width
* sizeof(uint32_t));
313 /* accumulate new scaled row to rowacc */
314 if (!ctx
->h_scaler(rowacc
, ctx
, true))
322 /* Set up rounding and scale factors for the horizontal scaler. The divisor
323 is bm->width - 1, so that the first and last pixels in the row align
324 exactly between input and output
326 static inline void scale_h_linear_setup(struct scaler_context
*ctx
)
328 ctx
->divisor
= ctx
->bm
->width
- 1;
331 /* horizontal linear scaler */
332 static bool scale_h_linear(void *out_line_ptr
, struct scaler_context
*ctx
,
335 unsigned int ix
, ox
, ixe
;
336 /* type x = x is an ugly hack for hiding an unitialized data warning. The
337 values are conditionally initialized before use, but other values are
338 set such that this will occur before these are used.
340 #ifdef HAVE_LCD_COLOR
341 struct uint32_rgb rgbval
=rgbval
, rgbinc
=rgbinc
,
342 *out_line
= (struct uint32_rgb
*)out_line_ptr
;
344 uint32_t val
=val
, inc
=inc
, *out_line
= (uint32_t*)out_line_ptr
;
346 struct img_part
*part
;
347 SDEBUGF("scale_h_linear\n");
348 FILL_BUF_INIT(part
,ctx
->store_part
,ctx
->args
);
350 /* The error is set so that values are initialized on the first pass. */
351 ixe
= ctx
->bm
->width
- 1;
352 /* give other tasks a chance to run */
354 for (ox
= 0; ox
< (uint32_t)ctx
->bm
->width
; ox
++)
356 #ifdef HAVE_LCD_COLOR
357 if (ixe
>= ((uint32_t)ctx
->bm
->width
- 1))
359 /* Store the new "current" pixel value in rgbval, and the color
360 step value in rgbinc.
362 ixe
-= (ctx
->bm
->width
- 1);
363 rgbinc
.r
= -(part
->buf
->red
);
364 rgbinc
.g
= -(part
->buf
->green
);
365 rgbinc
.b
= -(part
->buf
->blue
);
366 rgbval
.r
= (part
->buf
->red
) * (ctx
->bm
->width
- 1);
367 rgbval
.g
= (part
->buf
->green
) * (ctx
->bm
->width
- 1);
368 rgbval
.b
= (part
->buf
->blue
) * (ctx
->bm
->width
- 1);
370 /* If this wasn't the last pixel, add the next one to rgbinc. */
371 if (ix
< (uint32_t)ctx
->src
->width
) {
374 /* Fetch new pixels if needed */
375 FILL_BUF(part
,ctx
->store_part
,ctx
->args
);
376 rgbinc
.r
+= part
->buf
->red
;
377 rgbinc
.g
+= part
->buf
->green
;
378 rgbinc
.b
+= part
->buf
->blue
;
379 /* Add a partial step to rgbval, in this pixel isn't precisely
380 aligned with the new source pixel
382 rgbval
.r
+= rgbinc
.r
* ixe
;
383 rgbval
.g
+= rgbinc
.g
* ixe
;
384 rgbval
.b
+= rgbinc
.b
* ixe
;
386 /* Now multiple the color increment to its proper value */
387 rgbinc
.r
*= ctx
->src
->width
- 1;
388 rgbinc
.g
*= ctx
->src
->width
- 1;
389 rgbinc
.b
*= ctx
->src
->width
- 1;
391 rgbval
.r
+= rgbinc
.r
;
392 rgbval
.g
+= rgbinc
.g
;
393 rgbval
.b
+= rgbinc
.b
;
395 /* round and scale values, and accumulate or store to output */
398 out_line
[ox
].r
+= rgbval
.r
;
399 out_line
[ox
].g
+= rgbval
.g
;
400 out_line
[ox
].b
+= rgbval
.b
;
402 out_line
[ox
].r
= rgbval
.r
;
403 out_line
[ox
].g
= rgbval
.g
;
404 out_line
[ox
].b
= rgbval
.b
;
407 if (ixe
>= ((uint32_t)ctx
->bm
->width
- 1))
409 /* Store the new "current" pixel value in rgbval, and the color
410 step value in rgbinc.
412 ixe
-= (ctx
->bm
->width
- 1);
415 val
*= (ctx
->bm
->width
- 1);
417 /* If this wasn't the last pixel, add the next one to rgbinc. */
418 if (ix
< (uint32_t)ctx
->src
->width
) {
421 /* Fetch new pixels if needed */
422 FILL_BUF(part
,ctx
->store_part
,ctx
->args
);
424 /* Add a partial step to rgbval, in this pixel isn't precisely
425 aligned with the new source pixel
429 /* Now multiply the color increment to its proper value */
430 inc
*= ctx
->src
->width
- 1;
433 /* round and scale values, and accumulate or store to output */
441 ixe
+= ctx
->src
->width
- 1;
446 /* vertical linear scaler */
447 static inline bool scale_v_linear(struct rowset
*rset
,
448 struct scaler_context
*ctx
)
450 uint32_t mul
, x
, iy
, iye
;
452 /* Set up scale and rounding factors, the divisor is bm->height - 1 */
453 ctx
->divisor
*= (ctx
->bm
->height
- 1);
454 ctx
->round
= ctx
->divisor
>> 1;
455 ctx
->divisor
= ((ctx
->divisor
- 1 + 0x80000000U
) / ctx
->divisor
) << 1;
456 /* Set up our two temp buffers. The names are generic because they'll be
457 swapped each time a new input row is read
459 #ifdef HAVE_LCD_COLOR
460 uint32_t *rowinc
= (uint32_t *)(ctx
->buf
),
461 *rowval
= rowinc
+ 3 * ctx
->bm
->width
,
462 *rowtmp
= rowval
+ 3 * ctx
->bm
->width
;
464 uint32_t *rowinc
= (uint32_t *)(ctx
->buf
),
465 *rowval
= rowinc
+ ctx
->bm
->width
,
466 *rowtmp
= rowval
+ ctx
->bm
->width
;
469 SDEBUGF("scale_v_linear\n");
472 iye
= ctx
->bm
->height
- 1;
473 /* get first scaled row in rowtmp */
474 if(!ctx
->h_scaler((void*)rowtmp
, ctx
, false))
476 for (oy
= rset
->rowstart
; oy
!= rset
->rowstop
; oy
+= rset
->rowstep
)
478 if (iye
>= (uint32_t)ctx
->bm
->height
- 1)
480 iye
-= ctx
->bm
->height
- 1;
482 #ifdef HAVE_LCD_COLOR
483 for (x
= 0; x
< 3 * (uint32_t)ctx
->bm
->width
; x
++)
485 for (x
= 0; x
< (uint32_t)ctx
->bm
->width
; x
++)
488 rowinc
[x
] = -rowtmp
[x
];
489 rowval
[x
] = rowtmp
[x
] * (ctx
->bm
->height
- 1);
491 if (iy
< (uint32_t)ctx
->src
->height
)
493 if (!ctx
->h_scaler((void*)rowtmp
, ctx
, false))
495 #ifdef HAVE_LCD_COLOR
496 for (x
= 0; x
< 3 * (uint32_t)ctx
->bm
->width
; x
++)
498 for (x
= 0; x
< (uint32_t)ctx
->bm
->width
; x
++)
501 rowinc
[x
] += rowtmp
[x
];
502 rowval
[x
] += rowinc
[x
] * iye
;
503 rowinc
[x
] *= ctx
->src
->height
- 1;
507 #ifdef HAVE_LCD_COLOR
508 for (x
= 0; x
< 3 * (uint32_t)ctx
->bm
->width
; x
++)
510 for (x
= 0; x
< (uint32_t)ctx
->bm
->width
; x
++)
512 rowval
[x
] += rowinc
[x
];
513 ctx
->output_row(oy
, (void*)rowval
, ctx
);
514 iye
+= ctx
->src
->height
- 1;
518 #endif /* HAVE_UPSCALER */
520 void output_row_native(uint32_t row
, void * row_in
, struct scaler_context
*ctx
)
523 int fb_width
= BM_WIDTH(ctx
->bm
->width
,FORMAT_NATIVE
,0);
524 uint8_t dy
= DITHERY(row
);
525 #ifdef HAVE_LCD_COLOR
526 struct uint32_rgb
*qp
= (struct uint32_rgb
*)row_in
;
528 uint32_t *qp
= (uint32_t*)row_in
;
530 SDEBUGF("output_row: y: %d in: %p\n",row
, row_in
);
532 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
533 /* greyscale iPods */
534 fb_data
*dest
= (fb_data
*)ctx
->bm
->data
+ fb_width
* row
;
540 for (col
= 0; col
< ctx
->bm
->width
; col
++) {
542 delta
= DITHERXDY(col
,dy
);
543 bright
= ((*qp
++) + ctx
->round
) *
544 (uint64_t)ctx
->divisor
>> 32;
545 bright
= (3 * bright
+ (bright
>> 6) + delta
) >> 8;
546 data
|= (~bright
& 3) << shift
;
556 #elif LCD_PIXELFORMAT == VERTICAL_PACKING
558 fb_data
*dest
= (fb_data
*)ctx
->bm
->data
+ fb_width
*
560 int shift
= 2 * (row
& 3);
564 for (col
= 0; col
< ctx
->bm
->width
; col
++) {
566 delta
= DITHERXDY(col
,dy
);
567 bright
= ((*qp
++) + ctx
->round
) *
568 (uint64_t)ctx
->divisor
>> 32;
569 bright
= (3 * bright
+ (bright
>> 6) + delta
) >> 8;
570 *dest
++ |= (~bright
& 3) << shift
;
572 #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
574 fb_data
*dest
= (fb_data
*)ctx
->bm
->data
+ fb_width
*
580 for (col
= 0; col
< ctx
->bm
->width
; col
++) {
582 delta
= DITHERXDY(col
,dy
);
583 bright
= ((*qp
++) + ctx
->round
) *
584 (uint64_t)ctx
->divisor
>> 32;
585 bright
= (3 * bright
+ (bright
>> 6) + delta
) >> 8;
586 *dest
++ |= vi_pattern
[bright
] << shift
;
588 #endif /* LCD_PIXELFORMAT */
589 #elif LCD_DEPTH == 16
590 /* iriver h300, colour iPods, X5 */
591 fb_data
*dest
= (fb_data
*)ctx
->bm
->data
+ fb_width
* row
;
594 struct uint32_rgb q0
;
596 for (col
= 0; col
< ctx
->bm
->width
; col
++) {
598 delta
= DITHERXDY(col
,dy
);
600 r
= (q0
.r
+ ctx
->round
) * (uint64_t)ctx
->divisor
>> 32;
601 g
= (q0
.g
+ ctx
->round
) * (uint64_t)ctx
->divisor
>> 32;
602 b
= (q0
.b
+ ctx
->round
) * (uint64_t)ctx
->divisor
>> 32;
603 r
= (31 * r
+ (r
>> 3) + delta
) >> 8;
604 g
= (63 * g
+ (g
>> 2) + delta
) >> 8;
605 b
= (31 * b
+ (b
>> 3) + delta
) >> 8;
606 *dest
++ = LCD_RGBPACK_LCD(r
, g
, b
);
608 #endif /* LCD_DEPTH */
611 int resize_on_load(struct bitmap
*bm
, bool dither
, struct dim
*src
,
612 struct rowset
*rset
, unsigned char *buf
, unsigned int len
,
613 struct img_part
* (*store_part
)(void *args
),
618 const int sw
= src
->width
;
619 const int sh
= src
->height
;
620 const int dw
= bm
->width
;
621 const int dh
= bm
->height
;
624 #ifdef HAVE_LCD_COLOR
625 unsigned int needed
= sizeof(struct uint32_rgb
) * 3 * bm
->width
;
627 unsigned int needed
= sizeof(uint32_t) * 3 * bm
->width
;
629 #if MAX_SC_STACK_ALLOC
630 uint8_t sc_buf
[(needed
<= len
|| needed
> MAX_SC_STACK_ALLOC
) ?
633 len
= (unsigned int)align_buffer(PUN_PTR(void**, &buf
), len
,
637 #if MAX_SC_STACK_ALLOC
638 if (needed
> MAX_SC_STACK_ALLOC
)
640 DEBUGF("unable to allocate required buffer: %d needed, "
641 "%d available, %d permitted from stack\n",
642 needed
, len
, MAX_SC_STACK_ALLOC
);
645 if (sizeof(sc_buf
) < needed
)
647 DEBUGF("failed to allocate large enough buffer on stack: "
648 "%d needed, only got %d",
649 needed
, MAX_SC_STACK_ALLOC
);
653 DEBUGF("unable to allocate required buffer: %d needed, "
654 "%d available\n", needed
, len
);
659 struct scaler_context ctx
;
661 ctx
.store_part
= store_part
;
663 #if MAX_SC_STACK_ALLOC
664 ctx
.buf
= needed
> len
? sc_buf
: buf
;
672 ctx
.output_row
= output_row_native
;
677 ctx
.h_scaler
= scale_h_area
;
678 scale_h_area_setup(&ctx
);
681 ctx
.h_scaler
= scale_h_linear
;
682 scale_h_linear_setup(&ctx
);
688 ret
= scale_v_area(rset
, &ctx
);
691 ret
= scale_v_linear(rset
, &ctx
);
696 return BM_SIZE(bm
->width
,bm
->height
,bm
->format
,0);