Added MP3::Info.pm inside script to make it standalone.
[kugel-rb.git] / apps / plugins / jpeg.c
blobc601712b111fa9243fdaee0d3175c6ab289b5983
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * JPEG image viewer
11 * (This is a real mess if it has to be coded in one single C file)
13 * Copyright (C) 2004 Jörg Hohensohn aka [IDC]Dragon
14 * Grayscale framework (c) 2004 Jens Arnold
15 * Heavily borrowed from the IJG implementation (c) Thomas G. Lane
16 * Small & fast downscaling IDCT (c) 2002 by Guido Vollbeding JPEGclub.org
18 * All files in this archive are subject to the GNU General Public License.
19 * See the file COPYING in the source tree root for full license agreement.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
26 #ifndef SIMULATOR /* not for simulator by now */
27 #include "plugin.h"
29 #ifdef HAVE_LCD_BITMAP /* and also not for the Player */
30 #include "gray.h"
32 /* variable button definitions */
33 #if CONFIG_KEYPAD == RECORDER_PAD
34 #define JPEG_ZOOM_IN BUTTON_PLAY
35 #define JPEG_ZOOM_OUT BUTTON_ON
37 #elif CONFIG_KEYPAD == ONDIO_PAD
38 #define JPEG_ZOOM_PRE BUTTON_MENU
39 #define JPEG_ZOOM_IN (BUTTON_MENU | BUTTON_REL)
40 #define JPEG_ZOOM_OUT (BUTTON_MENU | BUTTON_REPEAT)
42 #endif
44 /******************************* Globals ***********************************/
46 static struct plugin_api* rb;
48 /* for portability of below JPEG code */
49 #define MEMSET(p,v,c) rb->memset(p,v,c)
50 #define INLINE static inline
51 #define ENDIAN_SWAP16(n) n /* only for poor little endian machines */
55 /**************** begin JPEG code ********************/
57 /* LUT for IDCT, this could also be used for gamma correction */
58 const unsigned char range_limit[1024] =
60 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
61 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
62 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
63 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
64 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
65 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
66 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
67 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,
69 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
70 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
71 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
72 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
73 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
74 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
75 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
76 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
77 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
78 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
79 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
80 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
81 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
82 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
83 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
84 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
85 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
86 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
87 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
88 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
89 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
90 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
91 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
92 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
94 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
95 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
96 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
97 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
98 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
99 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
100 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
101 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
102 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
103 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
104 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
105 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
106 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
107 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
108 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
109 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
110 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
111 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
112 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
113 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
114 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
115 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
116 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
117 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
119 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
120 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
121 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
122 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
123 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
124 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
125 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
126 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
130 /* IDCT implementation */
133 #define CONST_BITS 13
134 #define PASS1_BITS 2
137 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
138 * causing a lot of useless floating-point operations at run time.
139 * To get around this we use the following pre-calculated constants.
140 * If you change CONST_BITS you may want to add appropriate values.
141 * (With a reasonable C compiler, you can just rely on the FIX() macro...)
143 #define FIX_0_298631336 2446 /* FIX(0.298631336) */
144 #define FIX_0_390180644 3196 /* FIX(0.390180644) */
145 #define FIX_0_541196100 4433 /* FIX(0.541196100) */
146 #define FIX_0_765366865 6270 /* FIX(0.765366865) */
147 #define FIX_0_899976223 7373 /* FIX(0.899976223) */
148 #define FIX_1_175875602 9633 /* FIX(1.175875602) */
149 #define FIX_1_501321110 12299 /* FIX(1.501321110) */
150 #define FIX_1_847759065 15137 /* FIX(1.847759065) */
151 #define FIX_1_961570560 16069 /* FIX(1.961570560) */
152 #define FIX_2_053119869 16819 /* FIX(2.053119869) */
153 #define FIX_2_562915447 20995 /* FIX(2.562915447) */
154 #define FIX_3_072711026 25172 /* FIX(3.072711026) */
158 /* Multiply an long variable by an long constant to yield an long result.
159 * For 8-bit samples with the recommended scaling, all the variable
160 * and constant values involved are no more than 16 bits wide, so a
161 * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
162 * For 12-bit samples, a full 32-bit multiplication will be needed.
164 #define MULTIPLY16(var,const) (((short) (var)) * ((short) (const)))
167 /* Dequantize a coefficient by multiplying it by the multiplier-table
168 * entry; produce an int result. In this module, both inputs and result
169 * are 16 bits or less, so either int or short multiply will work.
171 /* #define DEQUANTIZE(coef,quantval) (((int) (coef)) * (quantval)) */
172 #define DEQUANTIZE MULTIPLY16
174 /* Descale and correctly round an int value that's scaled by N bits.
175 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
176 * the fudge factor is correct for either sign of X.
178 #define DESCALE(x,n) (((x) + (1l << ((n)-1))) >> (n))
180 #define RANGE_MASK (255 * 4 + 3) /* 2 bits wider than legal samples */
185 * Perform dequantization and inverse DCT on one block of coefficients,
186 * producing a reduced-size 1x1 output block.
188 void idct1x1(unsigned char* p_byte, int* inptr, int* quantptr, int skip_line)
190 (void)skip_line; /* unused */
191 *p_byte = range_limit[(inptr[0] * quantptr[0] >> 3) & RANGE_MASK];
197 * Perform dequantization and inverse DCT on one block of coefficients,
198 * producing a reduced-size 2x2 output block.
200 void idct2x2(unsigned char* p_byte, int* inptr, int* quantptr, int skip_line)
202 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
203 unsigned char* outptr;
205 /* Pass 1: process columns from input, store into work array. */
207 /* Column 0 */
208 tmp4 = DEQUANTIZE(inptr[8*0], quantptr[8*0]);
209 tmp5 = DEQUANTIZE(inptr[8*1], quantptr[8*1]);
211 tmp0 = tmp4 + tmp5;
212 tmp2 = tmp4 - tmp5;
214 /* Column 1 */
215 tmp4 = DEQUANTIZE(inptr[8*0+1], quantptr[8*0+1]);
216 tmp5 = DEQUANTIZE(inptr[8*1+1], quantptr[8*1+1]);
218 tmp1 = tmp4 + tmp5;
219 tmp3 = tmp4 - tmp5;
221 /* Pass 2: process 2 rows, store into output array. */
223 /* Row 0 */
224 outptr = p_byte;
226 outptr[0] = range_limit[(int) DESCALE(tmp0 + tmp1, 3)
227 & RANGE_MASK];
228 outptr[1] = range_limit[(int) DESCALE(tmp0 - tmp1, 3)
229 & RANGE_MASK];
231 /* Row 1 */
232 outptr = p_byte + skip_line;
234 outptr[0] = range_limit[(int) DESCALE(tmp2 + tmp3, 3)
235 & RANGE_MASK];
236 outptr[1] = range_limit[(int) DESCALE(tmp2 - tmp3, 3)
237 & RANGE_MASK];
243 * Perform dequantization and inverse DCT on one block of coefficients,
244 * producing a reduced-size 4x4 output block.
246 void idct4x4(unsigned char* p_byte, int* inptr, int* quantptr, int skip_line)
248 int tmp0, tmp2, tmp10, tmp12;
249 int z1, z2, z3;
250 int * wsptr;
251 unsigned char* outptr;
252 int ctr;
253 int workspace[4*4]; /* buffers data between passes */
255 /* Pass 1: process columns from input, store into work array. */
257 wsptr = workspace;
258 for (ctr = 0; ctr < 4; ctr++, inptr++, quantptr++, wsptr++)
260 /* Even part */
262 tmp0 = DEQUANTIZE(inptr[8*0], quantptr[8*0]);
263 tmp2 = DEQUANTIZE(inptr[8*2], quantptr[8*2]);
265 tmp10 = (tmp0 + tmp2) << PASS1_BITS;
266 tmp12 = (tmp0 - tmp2) << PASS1_BITS;
268 /* Odd part */
269 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
271 z2 = DEQUANTIZE(inptr[8*1], quantptr[8*1]);
272 z3 = DEQUANTIZE(inptr[8*3], quantptr[8*3]);
274 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
275 tmp0 = DESCALE(z1 + MULTIPLY16(z3, - FIX_1_847759065), CONST_BITS-PASS1_BITS);
276 tmp2 = DESCALE(z1 + MULTIPLY16(z2, FIX_0_765366865), CONST_BITS-PASS1_BITS);
278 /* Final output stage */
280 wsptr[4*0] = (int) (tmp10 + tmp2);
281 wsptr[4*3] = (int) (tmp10 - tmp2);
282 wsptr[4*1] = (int) (tmp12 + tmp0);
283 wsptr[4*2] = (int) (tmp12 - tmp0);
286 /* Pass 2: process 4 rows from work array, store into output array. */
288 wsptr = workspace;
289 for (ctr = 0; ctr < 4; ctr++)
291 outptr = p_byte + (ctr*skip_line);
292 /* Even part */
294 tmp0 = (int) wsptr[0];
295 tmp2 = (int) wsptr[2];
297 tmp10 = (tmp0 + tmp2) << CONST_BITS;
298 tmp12 = (tmp0 - tmp2) << CONST_BITS;
300 /* Odd part */
301 /* Same rotation as in the even part of the 8x8 LL&M IDCT */
303 z2 = (int) wsptr[1];
304 z3 = (int) wsptr[3];
306 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
307 tmp0 = z1 + MULTIPLY16(z3, - FIX_1_847759065);
308 tmp2 = z1 + MULTIPLY16(z2, FIX_0_765366865);
310 /* Final output stage */
312 outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
313 CONST_BITS+PASS1_BITS+3)
314 & RANGE_MASK];
315 outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
316 CONST_BITS+PASS1_BITS+3)
317 & RANGE_MASK];
318 outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
319 CONST_BITS+PASS1_BITS+3)
320 & RANGE_MASK];
321 outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
322 CONST_BITS+PASS1_BITS+3)
323 & RANGE_MASK];
325 wsptr += 4; /* advance pointer to next row */
332 * Perform dequantization and inverse DCT on one block of coefficients.
334 void idct8x8(unsigned char* p_byte, int* inptr, int* quantptr, int skip_line)
336 long tmp0, tmp1, tmp2, tmp3;
337 long tmp10, tmp11, tmp12, tmp13;
338 long z1, z2, z3, z4, z5;
339 int * wsptr;
340 unsigned char* outptr;
341 int ctr;
342 static int workspace[64]; /* buffers data between passes */
344 /* Pass 1: process columns from input, store into work array. */
345 /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
346 /* furthermore, we scale the results by 2**PASS1_BITS. */
348 wsptr = workspace;
349 for (ctr = 8; ctr > 0; ctr--)
351 /* Due to quantization, we will usually find that many of the input
352 * coefficients are zero, especially the AC terms. We can exploit this
353 * by short-circuiting the IDCT calculation for any column in which all
354 * the AC terms are zero. In that case each output is equal to the
355 * DC coefficient (with scale factor as needed).
356 * With typical images and quantization tables, half or more of the
357 * column DCT calculations can be simplified this way.
360 if ((inptr[8*1] | inptr[8*2] | inptr[8*3]
361 | inptr[8*4] | inptr[8*5] | inptr[8*6] | inptr[8*7]) == 0)
363 /* AC terms all zero */
364 int dcval = DEQUANTIZE(inptr[8*0], quantptr[8*0]) << PASS1_BITS;
366 wsptr[8*0] = wsptr[8*1] = wsptr[8*2] = wsptr[8*3] = wsptr[8*4]
367 = wsptr[8*5] = wsptr[8*6] = wsptr[8*7] = dcval;
368 inptr++; /* advance pointers to next column */
369 quantptr++;
370 wsptr++;
371 continue;
374 /* Even part: reverse the even part of the forward DCT. */
375 /* The rotator is sqrt(2)*c(-6). */
377 z2 = DEQUANTIZE(inptr[8*2], quantptr[8*2]);
378 z3 = DEQUANTIZE(inptr[8*6], quantptr[8*6]);
380 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
381 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065);
382 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865);
384 z2 = DEQUANTIZE(inptr[8*0], quantptr[8*0]);
385 z3 = DEQUANTIZE(inptr[8*4], quantptr[8*4]);
387 tmp0 = (z2 + z3) << CONST_BITS;
388 tmp1 = (z2 - z3) << CONST_BITS;
390 tmp10 = tmp0 + tmp3;
391 tmp13 = tmp0 - tmp3;
392 tmp11 = tmp1 + tmp2;
393 tmp12 = tmp1 - tmp2;
395 /* Odd part per figure 8; the matrix is unitary and hence its
396 transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
398 tmp0 = DEQUANTIZE(inptr[8*7], quantptr[8*7]);
399 tmp1 = DEQUANTIZE(inptr[8*5], quantptr[8*5]);
400 tmp2 = DEQUANTIZE(inptr[8*3], quantptr[8*3]);
401 tmp3 = DEQUANTIZE(inptr[8*1], quantptr[8*1]);
403 z1 = tmp0 + tmp3;
404 z2 = tmp1 + tmp2;
405 z3 = tmp0 + tmp2;
406 z4 = tmp1 + tmp3;
407 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
409 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
410 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
411 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
412 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
413 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
414 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
415 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
416 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
418 z3 += z5;
419 z4 += z5;
421 tmp0 += z1 + z3;
422 tmp1 += z2 + z4;
423 tmp2 += z2 + z3;
424 tmp3 += z1 + z4;
426 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
428 wsptr[8*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
429 wsptr[8*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
430 wsptr[8*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
431 wsptr[8*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
432 wsptr[8*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
433 wsptr[8*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
434 wsptr[8*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
435 wsptr[8*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
437 inptr++; /* advance pointers to next column */
438 quantptr++;
439 wsptr++;
442 /* Pass 2: process rows from work array, store into output array. */
443 /* Note that we must descale the results by a factor of 8 == 2**3, */
444 /* and also undo the PASS1_BITS scaling. */
446 wsptr = workspace;
447 for (ctr = 0; ctr < 8; ctr++)
449 outptr = p_byte + (ctr*skip_line);
450 /* Rows of zeroes can be exploited in the same way as we did with columns.
451 * However, the column calculation has created many nonzero AC terms, so
452 * the simplification applies less often (typically 5% to 10% of the time).
453 * On machines with very fast multiplication, it's possible that the
454 * test takes more time than it's worth. In that case this section
455 * may be commented out.
458 #ifndef NO_ZERO_ROW_TEST
459 if ((wsptr[1] | wsptr[2] | wsptr[3]
460 | wsptr[4] | wsptr[5] | wsptr[6] | wsptr[7]) == 0)
462 /* AC terms all zero */
463 unsigned char dcval = range_limit[(int) DESCALE((long) wsptr[0],
464 PASS1_BITS+3) & RANGE_MASK];
466 outptr[0] = dcval;
467 outptr[1] = dcval;
468 outptr[2] = dcval;
469 outptr[3] = dcval;
470 outptr[4] = dcval;
471 outptr[5] = dcval;
472 outptr[6] = dcval;
473 outptr[7] = dcval;
475 wsptr += 8; /* advance pointer to next row */
476 continue;
478 #endif
480 /* Even part: reverse the even part of the forward DCT. */
481 /* The rotator is sqrt(2)*c(-6). */
483 z2 = (long) wsptr[2];
484 z3 = (long) wsptr[6];
486 z1 = MULTIPLY16(z2 + z3, FIX_0_541196100);
487 tmp2 = z1 + MULTIPLY16(z3, - FIX_1_847759065);
488 tmp3 = z1 + MULTIPLY16(z2, FIX_0_765366865);
490 tmp0 = ((long) wsptr[0] + (long) wsptr[4]) << CONST_BITS;
491 tmp1 = ((long) wsptr[0] - (long) wsptr[4]) << CONST_BITS;
493 tmp10 = tmp0 + tmp3;
494 tmp13 = tmp0 - tmp3;
495 tmp11 = tmp1 + tmp2;
496 tmp12 = tmp1 - tmp2;
498 /* Odd part per figure 8; the matrix is unitary and hence its
499 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively. */
501 tmp0 = (long) wsptr[7];
502 tmp1 = (long) wsptr[5];
503 tmp2 = (long) wsptr[3];
504 tmp3 = (long) wsptr[1];
506 z1 = tmp0 + tmp3;
507 z2 = tmp1 + tmp2;
508 z3 = tmp0 + tmp2;
509 z4 = tmp1 + tmp3;
510 z5 = MULTIPLY16(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
512 tmp0 = MULTIPLY16(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
513 tmp1 = MULTIPLY16(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
514 tmp2 = MULTIPLY16(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
515 tmp3 = MULTIPLY16(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
516 z1 = MULTIPLY16(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
517 z2 = MULTIPLY16(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
518 z3 = MULTIPLY16(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
519 z4 = MULTIPLY16(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
521 z3 += z5;
522 z4 += z5;
524 tmp0 += z1 + z3;
525 tmp1 += z2 + z4;
526 tmp2 += z2 + z3;
527 tmp3 += z1 + z4;
529 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
531 outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
532 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
533 outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
534 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
535 outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
536 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
537 outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
538 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
539 outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
540 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
541 outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
542 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
543 outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
544 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
545 outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
546 CONST_BITS+PASS1_BITS+3) & RANGE_MASK];
548 wsptr += 8; /* advance pointer to next row */
554 /* JPEG decoder implementation */
557 #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
559 struct derived_tbl
561 /* Basic tables: (element [0] of each array is unused) */
562 long mincode[17]; /* smallest code of length k */
563 long maxcode[18]; /* largest code of length k (-1 if none) */
564 /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
565 int valptr[17]; /* huffval[] index of 1st symbol of length k */
567 /* Back link to public Huffman table (needed only in slow_DECODE) */
568 int* pub;
570 /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
571 the input data stream. If the next Huffman code is no more
572 than HUFF_LOOKAHEAD bits long, we can obtain its length and
573 the corresponding symbol directly from these tables. */
574 int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
575 unsigned char look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
578 #define QUANT_TABLE_LENGTH 64
580 /* for type of Huffman table */
581 #define DC_LEN 28
582 #define AC_LEN 178
584 struct huffman_table
585 { /* length and code according to JFIF format */
586 int huffmancodes_dc[DC_LEN];
587 int huffmancodes_ac[AC_LEN];
590 struct frame_component
592 int ID;
593 int horizontal_sampling;
594 int vertical_sampling;
595 int quanttable_select;
598 struct scan_component
600 int ID;
601 int DC_select;
602 int AC_select;
605 struct bitstream
607 unsigned long get_buffer; /* current bit-extraction buffer */
608 int bits_left; /* # of unused bits in it */
609 unsigned char* next_input_byte;
610 unsigned char* input_end; /* upper limit +1 */
613 struct jpeg
615 int x_size, y_size; /* size of image (can be less than block boundary) */
616 int x_phys, y_phys; /* physical size, block aligned */
617 int x_mbl; /* x dimension of MBL */
618 int y_mbl; /* y dimension of MBL */
619 int blocks; /* blocks per MBL */
620 int restart_interval; /* number of MCUs between RSTm markers */
621 int store_pos[4]; /* for Y block ordering */
623 unsigned char* p_entropy_data;
624 unsigned char* p_entropy_end;
626 int quanttable[4][QUANT_TABLE_LENGTH]; /* raw quantization tables 0-3 */
627 int qt_idct[2][QUANT_TABLE_LENGTH]; /* quantization tables for IDCT */
629 struct huffman_table hufftable[2]; /* Huffman tables */
630 struct derived_tbl dc_derived_tbls[2]; /* Huffman-LUTs */
631 struct derived_tbl ac_derived_tbls[2];
633 struct frame_component frameheader[3]; /* Component descriptor */
634 struct scan_component scanheader[3]; /* currently not used */
636 int mcu_membership[6]; /* info per block */
637 int tab_membership[6];
641 /* possible return flags for process_markers() */
642 #define HUFFTAB 0x0001 /* with huffman table */
643 #define QUANTTAB 0x0002 /* with quantization table */
644 #define APP0_JFIF 0x0004 /* with APP0 segment following JFIF standard */
645 #define FILL_FF 0x0008 /* with 0xFF padding bytes at begin/end */
646 #define SOF0 0x0010 /* with SOF0-Segment */
647 #define DHT 0x0020 /* with Definition of huffman tables */
648 #define SOS 0x0040 /* with Start-of-Scan segment */
649 #define DQT 0x0080 /* with definition of quantization table */
651 /* Preprocess the JPEG JFIF file */
652 int process_markers(unsigned char* p_src, long size, struct jpeg* p_jpeg)
654 unsigned char* p_bytes = p_src;
655 int marker_size; /* variable length of marker segment */
656 int i, j, n;
657 int ret = 0; /* returned flags */
659 p_jpeg->p_entropy_end = p_src + size;
661 while (p_src < p_bytes + size)
663 if (*p_src++ != 0xFF) /* no marker? */
665 p_src--; /* it's image data, put it back */
666 p_jpeg->p_entropy_data = p_src;
667 break; /* exit marker processing */
670 switch (*p_src++)
672 case 0xFF: /* Fill byte */
673 ret |= FILL_FF;
674 case 0x00: /* Zero stuffed byte - entropy data */
675 p_src--; /* put it back */
676 continue;
678 case 0xC0: /* SOF Huff - Baseline DCT */
680 ret |= SOF0;
681 marker_size = *p_src++ << 8; /* Highbyte */
682 marker_size |= *p_src++; /* Lowbyte */
683 n = *p_src++; /* sample precision (= 8 or 12) */
684 if (n != 8)
686 return(-1); /* Unsupported sample precision */
688 p_jpeg->y_size = *p_src++ << 8; /* Highbyte */
689 p_jpeg->y_size |= *p_src++; /* Lowbyte */
690 p_jpeg->x_size = *p_src++ << 8; /* Highbyte */
691 p_jpeg->x_size |= *p_src++; /* Lowbyte */
693 n = (marker_size-2-6)/3;
694 if (*p_src++ != n || (n != 1 && n != 3))
696 return(-2); /* Unsupported SOF0 component specification */
698 for (i=0; i<n; i++)
700 p_jpeg->frameheader[i].ID = *p_src++; /* Component info */
701 p_jpeg->frameheader[i].horizontal_sampling = *p_src >> 4;
702 p_jpeg->frameheader[i].vertical_sampling = *p_src++ & 0x0F;
703 p_jpeg->frameheader[i].quanttable_select = *p_src++;
704 if (p_jpeg->frameheader[i].horizontal_sampling > 2
705 || p_jpeg->frameheader[i].vertical_sampling > 2)
706 return -3; /* Unsupported SOF0 subsampling */
708 p_jpeg->blocks = n;
710 break;
712 case 0xC1: /* SOF Huff - Extended sequential DCT*/
713 case 0xC2: /* SOF Huff - Progressive DCT*/
714 case 0xC3: /* SOF Huff - Spatial (sequential) lossless*/
715 case 0xC5: /* SOF Huff - Differential sequential DCT*/
716 case 0xC6: /* SOF Huff - Differential progressive DCT*/
717 case 0xC7: /* SOF Huff - Differential spatial*/
718 case 0xC8: /* SOF Arith - Reserved for JPEG extensions*/
719 case 0xC9: /* SOF Arith - Extended sequential DCT*/
720 case 0xCA: /* SOF Arith - Progressive DCT*/
721 case 0xCB: /* SOF Arith - Spatial (sequential) lossless*/
722 case 0xCD: /* SOF Arith - Differential sequential DCT*/
723 case 0xCE: /* SOF Arith - Differential progressive DCT*/
724 case 0xCF: /* SOF Arith - Differential spatial*/
726 return (-4); /* other DCT model than baseline not implemented */
729 case 0xC4: /* Define Huffman Table(s) */
731 unsigned char* p_temp;
733 ret |= DHT;
734 marker_size = *p_src++ << 8; /* Highbyte */
735 marker_size |= *p_src++; /* Lowbyte */
737 p_temp = p_src;
738 while (p_src < p_temp+marker_size-2-17) /* another table */
740 int sum = 0;
741 i = *p_src & 0x0F; /* table index */
742 if (i > 1)
744 return (-5); /* Huffman table index out of range */
746 else if (*p_src++ & 0xF0) /* AC table */
748 for (j=0; j<16; j++)
750 sum += *p_src;
751 p_jpeg->hufftable[i].huffmancodes_ac[j] = *p_src++;
753 if(16 + sum > AC_LEN)
754 return -10; /* longer than allowed */
756 for (; j < 16 + sum; j++)
757 p_jpeg->hufftable[i].huffmancodes_ac[j] = *p_src++;
759 else /* DC table */
761 for (j=0; j<16; j++)
763 sum += *p_src;
764 p_jpeg->hufftable[i].huffmancodes_dc[j] = *p_src++;
766 if(16 + sum > DC_LEN)
767 return -11; /* longer than allowed */
769 for (; j < 16 + sum; j++)
770 p_jpeg->hufftable[i].huffmancodes_dc[j] = *p_src++;
772 } /* while */
773 p_src = p_temp+marker_size - 2; // skip possible residue
775 break;
777 case 0xCC: /* Define Arithmetic coding conditioning(s) */
778 return(-6); /* Arithmetic coding not supported */
780 case 0xD8: /* Start of Image */
781 case 0xD9: /* End of Image */
782 case 0x01: /* for temp private use arith code */
783 break; /* skip parameterless marker */
786 case 0xDA: /* Start of Scan */
788 ret |= SOS;
789 marker_size = *p_src++ << 8; /* Highbyte */
790 marker_size |= *p_src++; /* Lowbyte */
792 n = (marker_size-2-1-3)/2;
793 if (*p_src++ != n || (n != 1 && n != 3))
795 return (-7); /* Unsupported SOS component specification */
797 for (i=0; i<n; i++)
799 p_jpeg->scanheader[i].ID = *p_src++;
800 p_jpeg->scanheader[i].DC_select = *p_src >> 4;
801 p_jpeg->scanheader[i].AC_select = *p_src++ & 0x0F;
803 p_src += 3; /* skip spectral information */
805 break;
807 case 0xDB: /* Define quantization Table(s) */
809 ret |= DQT;
810 marker_size = *p_src++ << 8; /* Highbyte */
811 marker_size |= *p_src++; /* Lowbyte */
812 n = (marker_size-2)/(QUANT_TABLE_LENGTH+1); /* # of tables */
813 for (i=0; i<n; i++)
815 int id = *p_src++; /* ID */
816 if (id >= 4)
818 return (-8); /* Unsupported quantization table */
820 /* Read Quantisation table: */
821 for (j=0; j<QUANT_TABLE_LENGTH; j++)
822 p_jpeg->quanttable[id][j] = *p_src++;
825 break;
827 case 0xDD: /* Define Restart Interval */
829 marker_size = *p_src++ << 8; /* Highbyte */
830 marker_size |= *p_src++; /* Lowbyte */
831 p_jpeg->restart_interval = *p_src++ << 8; /* Highbyte */
832 p_jpeg->restart_interval |= *p_src++; /* Lowbyte */
833 p_src += marker_size-4; /* skip segment */
835 break;
837 case 0xDC: /* Define Number of Lines */
838 case 0xDE: /* Define Hierarchical progression */
839 case 0xDF: /* Expand Reference Component(s) */
840 case 0xE0: /* Application Field 0*/
841 case 0xE1: /* Application Field 1*/
842 case 0xE2: /* Application Field 2*/
843 case 0xE3: /* Application Field 3*/
844 case 0xE4: /* Application Field 4*/
845 case 0xE5: /* Application Field 5*/
846 case 0xE6: /* Application Field 6*/
847 case 0xE7: /* Application Field 7*/
848 case 0xE8: /* Application Field 8*/
849 case 0xE9: /* Application Field 9*/
850 case 0xEA: /* Application Field 10*/
851 case 0xEB: /* Application Field 11*/
852 case 0xEC: /* Application Field 12*/
853 case 0xED: /* Application Field 13*/
854 case 0xEE: /* Application Field 14*/
855 case 0xEF: /* Application Field 15*/
856 case 0xFE: /* Comment */
858 marker_size = *p_src++ << 8; /* Highbyte */
859 marker_size |= *p_src++; /* Lowbyte */
860 p_src += marker_size-2; /* skip segment */
862 break;
864 case 0xF0: /* Reserved for JPEG extensions */
865 case 0xF1: /* Reserved for JPEG extensions */
866 case 0xF2: /* Reserved for JPEG extensions */
867 case 0xF3: /* Reserved for JPEG extensions */
868 case 0xF4: /* Reserved for JPEG extensions */
869 case 0xF5: /* Reserved for JPEG extensions */
870 case 0xF6: /* Reserved for JPEG extensions */
871 case 0xF7: /* Reserved for JPEG extensions */
872 case 0xF8: /* Reserved for JPEG extensions */
873 case 0xF9: /* Reserved for JPEG extensions */
874 case 0xFA: /* Reserved for JPEG extensions */
875 case 0xFB: /* Reserved for JPEG extensions */
876 case 0xFC: /* Reserved for JPEG extensions */
877 case 0xFD: /* Reserved for JPEG extensions */
878 case 0x02: /* Reserved */
879 default:
880 return (-9); /* Unknown marker */
881 } /* switch */
882 } /* while */
884 return (ret); /* return flags with seen markers */
888 void default_huff_tbl(struct jpeg* p_jpeg)
890 static const struct huffman_table luma_table =
893 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
894 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
897 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,
898 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,
899 0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,
900 0x24,0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,
901 0x29,0x2A,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,
902 0x4A,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
903 0x6A,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
904 0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
905 0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,
906 0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xE1,0xE2,
907 0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
908 0xF9,0xFA
912 static const struct huffman_table chroma_table =
915 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,
916 0x00,0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B
919 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,0x02,0x77,
920 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
921 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33,0x52,0xF0,
922 0x15,0x62,0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,
923 0x27,0x28,0x29,0x2A,0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,
924 0x49,0x4A,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,
925 0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,
926 0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0xA2,0xA3,0xA4,0xA5,
927 0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xC2,0xC3,
928 0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,
929 0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
930 0xF9,0xFA
934 p_jpeg->hufftable[0] = luma_table;
935 p_jpeg->hufftable[1] = chroma_table;
937 return;
940 /* Compute the derived values for a Huffman table */
941 void fix_huff_tbl(int* htbl, struct derived_tbl* dtbl)
943 int p, i, l, si;
944 int lookbits, ctr;
945 char huffsize[257];
946 unsigned int huffcode[257];
947 unsigned int code;
949 dtbl->pub = htbl; /* fill in back link */
951 /* Figure C.1: make table of Huffman code length for each symbol */
952 /* Note that this is in code-length order. */
954 p = 0;
955 for (l = 1; l <= 16; l++)
956 { /* all possible code length */
957 for (i = 1; i <= (int) htbl[l-1]; i++) /* all codes per length */
958 huffsize[p++] = (char) l;
960 huffsize[p] = 0;
962 /* Figure C.2: generate the codes themselves */
963 /* Note that this is in code-length order. */
965 code = 0;
966 si = huffsize[0];
967 p = 0;
968 while (huffsize[p])
970 while (((int) huffsize[p]) == si)
972 huffcode[p++] = code;
973 code++;
975 code <<= 1;
976 si++;
979 /* Figure F.15: generate decoding tables for bit-sequential decoding */
981 p = 0;
982 for (l = 1; l <= 16; l++)
984 if (htbl[l-1])
986 dtbl->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
987 dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
988 p += htbl[l-1];
989 dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
991 else
993 dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
996 dtbl->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
998 /* Compute lookahead tables to speed up decoding.
999 * First we set all the table entries to 0, indicating "too long";
1000 * then we iterate through the Huffman codes that are short enough and
1001 * fill in all the entries that correspond to bit sequences starting
1002 * with that code.
1005 MEMSET(dtbl->look_nbits, 0, sizeof(dtbl->look_nbits));
1007 p = 0;
1008 for (l = 1; l <= HUFF_LOOKAHEAD; l++)
1010 for (i = 1; i <= (int) htbl[l-1]; i++, p++)
1012 /* l = current code's length, p = its index in huffcode[] & huffval[]. */
1013 /* Generate left-justified code followed by all possible bit sequences */
1014 lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
1015 for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--)
1017 dtbl->look_nbits[lookbits] = l;
1018 dtbl->look_sym[lookbits] = htbl[16+p];
1019 lookbits++;
1026 /* zag[i] is the natural-order position of the i'th element of zigzag order.
1027 * If the incoming data is corrupted, decode_mcu could attempt to
1028 * reference values beyond the end of the array. To avoid a wild store,
1029 * we put some extra zeroes after the real entries.
1031 static const int zag[] =
1033 0, 1, 8, 16, 9, 2, 3, 10,
1034 17, 24, 32, 25, 18, 11, 4, 5,
1035 12, 19, 26, 33, 40, 48, 41, 34,
1036 27, 20, 13, 6, 7, 14, 21, 28,
1037 35, 42, 49, 56, 57, 50, 43, 36,
1038 29, 22, 15, 23, 30, 37, 44, 51,
1039 58, 59, 52, 45, 38, 31, 39, 46,
1040 53, 60, 61, 54, 47, 55, 62, 63,
1041 0, 0, 0, 0, 0, 0, 0, 0, /* extra entries in case k>63 below */
1042 0, 0, 0, 0, 0, 0, 0, 0
1045 void build_lut(struct jpeg* p_jpeg)
1047 int i;
1048 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_dc,
1049 &p_jpeg->dc_derived_tbls[0]);
1050 fix_huff_tbl(p_jpeg->hufftable[0].huffmancodes_ac,
1051 &p_jpeg->ac_derived_tbls[0]);
1052 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_dc,
1053 &p_jpeg->dc_derived_tbls[1]);
1054 fix_huff_tbl(p_jpeg->hufftable[1].huffmancodes_ac,
1055 &p_jpeg->ac_derived_tbls[1]);
1057 /* build the dequantization tables for the IDCT (De-ZiZagged) */
1058 for (i=0; i<64; i++)
1060 p_jpeg->qt_idct[0][zag[i]] = p_jpeg->quanttable[0][i];
1061 p_jpeg->qt_idct[1][zag[i]] = p_jpeg->quanttable[1][i];
1064 for (i=0; i<4; i++)
1065 p_jpeg->store_pos[i] = i; /* default ordering */
1067 /* assignments for the decoding of blocks */
1068 if (p_jpeg->frameheader[0].horizontal_sampling == 2
1069 && p_jpeg->frameheader[0].vertical_sampling == 1)
1070 { /* 4:2:2 */
1071 p_jpeg->blocks = 4;
1072 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16;
1073 p_jpeg->x_phys = p_jpeg->x_mbl * 16;
1074 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8;
1075 p_jpeg->y_phys = p_jpeg->y_mbl * 8;
1076 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=2, V=3 */
1077 p_jpeg->mcu_membership[1] = 0;
1078 p_jpeg->mcu_membership[2] = 2;
1079 p_jpeg->mcu_membership[3] = 3;
1080 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */
1081 p_jpeg->tab_membership[1] = 0;
1082 p_jpeg->tab_membership[2] = 1;
1083 p_jpeg->tab_membership[3] = 1;
1085 if (p_jpeg->frameheader[0].horizontal_sampling == 1
1086 && p_jpeg->frameheader[0].vertical_sampling == 2)
1087 { /* 4:2:2 vertically subsampled */
1088 p_jpeg->store_pos[1] = 2; /* block positions are mirrored */
1089 p_jpeg->store_pos[2] = 1;
1090 p_jpeg->blocks = 4;
1091 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8;
1092 p_jpeg->x_phys = p_jpeg->x_mbl * 8;
1093 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16;
1094 p_jpeg->y_phys = p_jpeg->y_mbl * 16;
1095 p_jpeg->mcu_membership[0] = 0; /* Y1=Y2=0, U=2, V=3 */
1096 p_jpeg->mcu_membership[1] = 0;
1097 p_jpeg->mcu_membership[2] = 2;
1098 p_jpeg->mcu_membership[3] = 3;
1099 p_jpeg->tab_membership[0] = 0; /* DC, DC, AC, AC */
1100 p_jpeg->tab_membership[1] = 0;
1101 p_jpeg->tab_membership[2] = 1;
1102 p_jpeg->tab_membership[3] = 1;
1104 else if (p_jpeg->frameheader[0].horizontal_sampling == 2
1105 && p_jpeg->frameheader[0].vertical_sampling == 2)
1106 { /* 4:2:0 */
1107 p_jpeg->blocks = 6;
1108 p_jpeg->x_mbl = (p_jpeg->x_size+15) / 16;
1109 p_jpeg->x_phys = p_jpeg->x_mbl * 16;
1110 p_jpeg->y_mbl = (p_jpeg->y_size+15) / 16;
1111 p_jpeg->y_phys = p_jpeg->y_mbl * 16;
1112 p_jpeg->mcu_membership[0] = 0;
1113 p_jpeg->mcu_membership[1] = 0;
1114 p_jpeg->mcu_membership[2] = 0;
1115 p_jpeg->mcu_membership[3] = 0;
1116 p_jpeg->mcu_membership[4] = 2;
1117 p_jpeg->mcu_membership[5] = 3;
1118 p_jpeg->tab_membership[0] = 0;
1119 p_jpeg->tab_membership[1] = 0;
1120 p_jpeg->tab_membership[2] = 0;
1121 p_jpeg->tab_membership[3] = 0;
1122 p_jpeg->tab_membership[4] = 1;
1123 p_jpeg->tab_membership[5] = 1;
1125 else if (p_jpeg->frameheader[0].horizontal_sampling == 1
1126 && p_jpeg->frameheader[0].vertical_sampling == 1)
1127 { /* 4:4:4 */
1128 /* don't overwrite p_jpeg->blocks */
1129 p_jpeg->x_mbl = (p_jpeg->x_size+7) / 8;
1130 p_jpeg->x_phys = p_jpeg->x_mbl * 8;
1131 p_jpeg->y_mbl = (p_jpeg->y_size+7) / 8;
1132 p_jpeg->y_phys = p_jpeg->y_mbl * 8;
1133 p_jpeg->mcu_membership[0] = 0;
1134 p_jpeg->mcu_membership[1] = 2;
1135 p_jpeg->mcu_membership[2] = 3;
1136 p_jpeg->tab_membership[0] = 0;
1137 p_jpeg->tab_membership[1] = 1;
1138 p_jpeg->tab_membership[2] = 1;
1145 * These functions/macros provide the in-line portion of bit fetching.
1146 * Use check_bit_buffer to ensure there are N bits in get_buffer
1147 * before using get_bits, peek_bits, or drop_bits.
1148 * check_bit_buffer(state,n,action);
1149 * Ensure there are N bits in get_buffer; if suspend, take action.
1150 * val = get_bits(n);
1151 * Fetch next N bits.
1152 * val = peek_bits(n);
1153 * Fetch next N bits without removing them from the buffer.
1154 * drop_bits(n);
1155 * Discard next N bits.
1156 * The value N should be a simple variable, not an expression, because it
1157 * is evaluated multiple times.
1160 INLINE void check_bit_buffer(struct bitstream* pb, int nbits)
1162 if (pb->bits_left < nbits)
1163 { /* nbits is <= 16, so I can always refill 2 bytes in this case */
1164 unsigned char byte;
1166 byte = *pb->next_input_byte++;
1167 if (byte == 0xFF) /* legal marker can be byte stuffing or RSTm */
1168 { /* simplification: just skip the (one-byte) marker code */
1169 pb->next_input_byte++;
1171 pb->get_buffer = (pb->get_buffer << 8) | byte;
1173 byte = *pb->next_input_byte++;
1174 if (byte == 0xFF) /* legal marker can be byte stuffing or RSTm */
1175 { /* simplification: just skip the (one-byte) marker code */
1176 pb->next_input_byte++;
1178 pb->get_buffer = (pb->get_buffer << 8) | byte;
1180 pb->bits_left += 16;
1184 INLINE int get_bits(struct bitstream* pb, int nbits)
1186 return ((int) (pb->get_buffer >> (pb->bits_left -= nbits))) & ((1<<nbits)-1);
1189 INLINE int peek_bits(struct bitstream* pb, int nbits)
1191 return ((int) (pb->get_buffer >> (pb->bits_left - nbits))) & ((1<<nbits)-1);
1194 INLINE void drop_bits(struct bitstream* pb, int nbits)
1196 pb->bits_left -= nbits;
1199 /* re-synchronize to entropy data (skip restart marker) */
1200 void search_restart(struct bitstream* pb)
1202 pb->next_input_byte--; /* we may have overread it, taking 2 bytes */
1203 /* search for a non-byte-padding marker, has to be RSTm or EOS */
1204 while (pb->next_input_byte < pb->input_end &&
1205 (pb->next_input_byte[-2] != 0xFF || pb->next_input_byte[-1] == 0x00))
1207 pb->next_input_byte++;
1209 pb->bits_left = 0;
1212 /* Figure F.12: extend sign bit. */
1213 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
1215 static const int extend_test[16] = /* entry n is 2**(n-1) */
1217 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
1218 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000
1221 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
1223 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
1224 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
1225 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
1226 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1
1229 /* Decode a single value */
1230 INLINE int huff_decode_dc(struct bitstream* bs, struct derived_tbl* tbl)
1232 int nb, look, s, r;
1234 check_bit_buffer(bs, HUFF_LOOKAHEAD);
1235 look = peek_bits(bs, HUFF_LOOKAHEAD);
1236 if ((nb = tbl->look_nbits[look]) != 0)
1238 drop_bits(bs, nb);
1239 s = tbl->look_sym[look];
1240 check_bit_buffer(bs, s);
1241 r = get_bits(bs, s);
1242 s = HUFF_EXTEND(r, s);
1244 else
1245 { /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */
1246 long code;
1247 nb=HUFF_LOOKAHEAD+1;
1248 check_bit_buffer(bs, nb);
1249 code = get_bits(bs, nb);
1250 while (code > tbl->maxcode[nb])
1252 code <<= 1;
1253 check_bit_buffer(bs, 1);
1254 code |= get_bits(bs, 1);
1255 nb++;
1257 if (nb > 16) /* error in Huffman */
1259 s=0; /* fake a zero, this is most safe */
1261 else
1263 s = tbl->pub[16 + tbl->valptr[nb] + ((int) (code - tbl->mincode[nb])) ];
1264 check_bit_buffer(bs, s);
1265 r = get_bits(bs, s);
1266 s = HUFF_EXTEND(r, s);
1268 } /* end slow decode */
1269 return s;
1272 INLINE int huff_decode_ac(struct bitstream* bs, struct derived_tbl* tbl)
1274 int nb, look, s;
1276 check_bit_buffer(bs, HUFF_LOOKAHEAD);
1277 look = peek_bits(bs, HUFF_LOOKAHEAD);
1278 if ((nb = tbl->look_nbits[look]) != 0)
1280 drop_bits(bs, nb);
1281 s = tbl->look_sym[look];
1283 else
1284 { /* slow_DECODE(s, HUFF_LOOKAHEAD+1)) < 0); */
1285 long code;
1286 nb=HUFF_LOOKAHEAD+1;
1287 check_bit_buffer(bs, nb);
1288 code = get_bits(bs, nb);
1289 while (code > tbl->maxcode[nb])
1291 code <<= 1;
1292 check_bit_buffer(bs, 1);
1293 code |= get_bits(bs, 1);
1294 nb++;
1296 if (nb > 16) /* error in Huffman */
1298 s=0; /* fake a zero, this is most safe */
1300 else
1302 s = tbl->pub[16 + tbl->valptr[nb] + ((int) (code - tbl->mincode[nb])) ];
1304 } /* end slow decode */
1305 return s;
1309 /* a JPEG decoder specialized in decoding only the luminance (b&w) */
1310 int jpeg_decode(struct jpeg* p_jpeg, unsigned char* p_pixel, int downscale,
1311 void (*pf_progress)(int current, int total))
1313 struct bitstream bs; /* bitstream "object" */
1314 static int block[64]; /* decoded DCT coefficients */
1316 int width, height;
1317 int skip_line; /* bytes from one line to the next (skip_line) */
1318 int skip_strip, skip_mcu; /* bytes to next DCT row / column */
1320 int x, y; /* loop counter */
1322 unsigned char* p_byte; /* bitmap pointer */
1324 void (*pf_idct)(unsigned char*, int*, int*, int); /* selected IDCT */
1325 int k_need; /* AC coefficients needed up to here */
1326 int zero_need; /* init the block with this many zeros */
1328 int last_dc_val = 0;
1329 int store_offs[4]; /* memory offsets: order of Y11 Y12 Y21 Y22 U V */
1330 int restart = p_jpeg->restart_interval; /* MCUs until restart marker */
1332 /* pick the IDCT we want, determine how to work with coefs */
1333 if (downscale == 1)
1335 pf_idct = idct8x8;
1336 k_need = 64; /* all */
1337 zero_need = 63; /* all */
1339 else if (downscale == 2)
1341 pf_idct = idct4x4;
1342 k_need = 25; /* this far in zig-zag to cover 4*4 */
1343 zero_need = 27; /* clear this far in linear order */
1345 else if (downscale == 4)
1347 pf_idct = idct2x2;
1348 k_need = 5; /* this far in zig-zag to cover 2*2 */
1349 zero_need = 9; /* clear this far in linear order */
1351 else if (downscale == 8)
1353 pf_idct = idct1x1;
1354 k_need = 0; /* no AC, not needed */
1355 zero_need = 0; /* no AC, not needed */
1357 else return -1; /* not supported */
1359 /* init bitstream, fake a restart to make it start */
1360 bs.next_input_byte = p_jpeg->p_entropy_data;
1361 bs.bits_left = 0;
1362 bs.input_end = p_jpeg->p_entropy_end;
1364 width = p_jpeg->x_phys / downscale;
1365 height = p_jpeg->y_phys / downscale;
1366 skip_line = width;
1367 skip_strip = skip_line * (height / p_jpeg->y_mbl);
1368 skip_mcu = (width/p_jpeg->x_mbl);
1370 /* prepare offsets about where to store the different blocks */
1371 store_offs[p_jpeg->store_pos[0]] = 0;
1372 store_offs[p_jpeg->store_pos[1]] = 8 / downscale; /* to the right */
1373 store_offs[p_jpeg->store_pos[2]] = width * 8 / downscale; /* below */
1374 store_offs[p_jpeg->store_pos[3]] = store_offs[1] + store_offs[2]; /* r+b */
1376 for(y=0; y<p_jpeg->y_mbl && bs.next_input_byte <= bs.input_end; y++)
1378 p_byte = p_pixel;
1379 p_pixel += skip_strip;
1380 for (x=0; x<p_jpeg->x_mbl; x++)
1382 int blkn;
1384 /* Outer loop handles each block in the MCU */
1385 for (blkn = 0; blkn < p_jpeg->blocks; blkn++)
1386 { /* Decode a single block's worth of coefficients */
1387 int k = 1; /* coefficient index */
1388 int s, r; /* huffman values */
1389 int ci = p_jpeg->mcu_membership[blkn]; /* component index */
1390 int ti = p_jpeg->tab_membership[blkn]; /* table index */
1391 struct derived_tbl* dctbl = &p_jpeg->dc_derived_tbls[ti];
1392 struct derived_tbl* actbl = &p_jpeg->ac_derived_tbls[ti];
1394 /* Section F.2.2.1: decode the DC coefficient difference */
1395 s = huff_decode_dc(&bs, dctbl);
1397 if (ci == 0) /* only for Y component */
1399 last_dc_val += s;
1400 block[0] = last_dc_val; /* output it (assumes zag[0] = 0) */
1402 /* coefficient buffer must be cleared */
1403 MEMSET(block+1, 0, zero_need*sizeof(block[0]));
1405 /* Section F.2.2.2: decode the AC coefficients */
1406 for (; k < k_need; k++)
1408 s = huff_decode_ac(&bs, actbl);
1409 r = s >> 4;
1410 s &= 15;
1412 if (s)
1414 k += r;
1415 check_bit_buffer(&bs, s);
1416 r = get_bits(&bs, s);
1417 block[zag[k]] = HUFF_EXTEND(r, s);
1419 else
1421 if (r != 15)
1423 k = 64;
1424 break;
1426 k += r;
1428 } /* for k */
1430 /* In this path we just discard the values */
1431 for (; k < 64; k++)
1433 s = huff_decode_ac(&bs, actbl);
1434 r = s >> 4;
1435 s &= 15;
1437 if (s)
1439 k += r;
1440 check_bit_buffer(&bs, s);
1441 drop_bits(&bs, s);
1443 else
1445 if (r != 15)
1446 break;
1447 k += r;
1449 } /* for k */
1451 if (ci == 0)
1452 { /* only for Y component */
1453 pf_idct(p_byte+store_offs[blkn], block, p_jpeg->qt_idct[ti],
1454 skip_line);
1456 } /* for blkn */
1457 p_byte += skip_mcu;
1458 if (p_jpeg->restart_interval && --restart == 0)
1459 { /* if a restart marker is due: */
1460 restart = p_jpeg->restart_interval; /* count again */
1461 search_restart(&bs); /* align the bitstream */
1462 last_dc_val = 0; /* reset decoder */
1464 } /* for x */
1465 if (pf_progress != NULL)
1466 pf_progress(y, p_jpeg->y_mbl-1); /* notify about decoding progress */
1467 } /* for y */
1469 return 0; /* success */
1473 /**************** end JPEG code ********************/
1477 /**************** begin Application ********************/
1480 /************************* Types ***************************/
1482 struct t_disp
1484 unsigned char* bitmap;
1485 int width;
1486 int height;
1487 int stride;
1488 int x, y;
1492 /************************* Globals ***************************/
1494 /* decompressed image in the possible sizes (1,2,4,8), wasting the other */
1495 struct t_disp disp[9];
1497 /* my memory pool (from the mp3 buffer) */
1498 char print[32]; /* use a common snprintf() buffer */
1499 unsigned char* buf; /* up to here currently used by image(s) */
1500 int buf_size;
1501 unsigned char* buf_root; /* the root of the images */
1502 int root_size;
1504 /************************* Implementation ***************************/
1506 #define ZOOM_IN 100 // return codes for below function
1507 #define ZOOM_OUT 101
1509 /* switch off overlay, for handling SYS_ events */
1510 void cleanup(void *parameter)
1512 (void)parameter;
1514 gray_show_display(false);
1517 /* interactively scroll around the image */
1518 int scroll_bmp(struct t_disp* pdisp)
1520 int lastbutton = 0;
1522 /*empty the button queue first, to avoid unwanted scrolling */
1523 while(rb->button_get(false) != BUTTON_NONE);
1525 while (true)
1527 int button;
1528 int move;
1530 button = rb->button_get(true);
1532 switch(button)
1534 case BUTTON_LEFT:
1535 case BUTTON_LEFT | BUTTON_REPEAT:
1536 move = MIN(10, pdisp->x);
1537 if (move > 0)
1539 gray_scroll_right(move, false); /* scroll right */
1540 pdisp->x -= move;
1541 gray_drawgraymap(
1542 pdisp->bitmap + pdisp->y * pdisp->stride + pdisp->x,
1543 0, MAX(0, (LCD_HEIGHT-pdisp->height)/2), // x, y
1544 move, MIN(LCD_HEIGHT, pdisp->height), // w, h
1545 pdisp->stride);
1547 break;
1549 case BUTTON_RIGHT:
1550 case BUTTON_RIGHT | BUTTON_REPEAT:
1551 move = MIN(10, pdisp->width - pdisp->x - LCD_WIDTH);
1552 if (move > 0)
1554 gray_scroll_left(move, false); /* scroll left */
1555 pdisp->x += move;
1556 gray_drawgraymap(
1557 pdisp->bitmap + pdisp->y * pdisp->stride + pdisp->x + LCD_WIDTH - move,
1558 LCD_WIDTH - move, MAX(0, (LCD_HEIGHT-pdisp->height)/2), /* x, y */
1559 move, MIN(LCD_HEIGHT, pdisp->height), /* w, h */
1560 pdisp->stride);
1562 break;
1564 case BUTTON_UP:
1565 case BUTTON_UP | BUTTON_REPEAT:
1566 move = MIN(8, pdisp->y);
1567 if (move > 0)
1569 if (move == 8)
1570 gray_scroll_down8(false); /* scroll down by 8 pixel */
1571 else
1572 gray_scroll_down(move, false); /* scroll down 1..7 pixel */
1573 pdisp->y -= move;
1574 gray_drawgraymap(
1575 pdisp->bitmap + pdisp->y * pdisp->stride + pdisp->x,
1576 MAX(0, (LCD_WIDTH-pdisp->width)/2), 0, /* x, y */
1577 MIN(LCD_WIDTH, pdisp->width), move, /* w, h */
1578 pdisp->stride);
1580 break;
1582 case BUTTON_DOWN:
1583 case BUTTON_DOWN | BUTTON_REPEAT:
1584 move = MIN(8, pdisp->height - pdisp->y - LCD_HEIGHT);
1585 if (move > 0)
1587 if (move == 8)
1588 gray_scroll_up8(false); /* scroll up by 8 pixel */
1589 else
1590 gray_scroll_up(move, false); /* scroll up 1..7 pixel */
1591 pdisp->y += move;
1592 gray_drawgraymap(
1593 pdisp->bitmap + (pdisp->y + LCD_HEIGHT - move) * pdisp->stride + pdisp->x,
1594 MAX(0, (LCD_WIDTH-pdisp->width)/2), LCD_HEIGHT - move, /* x, y */
1595 MIN(LCD_WIDTH, pdisp->width), move, /* w, h */
1596 pdisp->stride);
1598 break;
1600 case JPEG_ZOOM_IN:
1601 #ifdef JPEG_ZOOM_PRE
1602 if (lastbutton != JPEG_ZOOM_PRE)
1603 break;
1604 #endif
1605 return ZOOM_IN;
1606 break;
1608 case JPEG_ZOOM_OUT:
1609 #ifdef JPEG_ZOOM_PRE
1610 if (lastbutton != JPEG_ZOOM_PRE)
1611 break;
1612 #endif
1613 return ZOOM_OUT;
1614 break;
1616 case BUTTON_OFF:
1617 return PLUGIN_OK;
1619 default:
1620 if (rb->default_event_handler_ex(button, cleanup, NULL)
1621 == SYS_USB_CONNECTED)
1622 return PLUGIN_USB_CONNECTED;
1623 break;
1625 } /* switch */
1627 if (button != BUTTON_NONE)
1628 lastbutton = button;
1629 } /* while (true) */
1632 /********************* main function *************************/
1634 /* debug function */
1635 int wait_for_button(void)
1637 int button;
1641 button = rb->button_get(true);
1642 } while ((button & BUTTON_REL) && button != SYS_USB_CONNECTED);
1644 return button;
1647 /* callback updating a progress meter while JPEG decoding */
1648 void cb_progess(int current, int total)
1650 rb->yield(); /* be nice to the other threads */
1651 rb->scrollbar(0, LCD_HEIGHT-8, LCD_WIDTH, 8, total, 0,
1652 current, HORIZONTAL);
1653 rb->lcd_update_rect(0, LCD_HEIGHT-8, LCD_WIDTH, 8);
1656 /* helper to align a buffer to a given power of two */
1657 void align(unsigned char** ppbuf, int* plen, int align)
1659 unsigned int orig = (unsigned int)*ppbuf;
1660 unsigned int aligned = (orig + (align-1)) & ~(align-1);
1662 *plen -= aligned - orig;
1663 *ppbuf = (unsigned char*)aligned;
1667 /* how far can we zoom in without running out of memory */
1668 int min_downscale(int x, int y, int bufsize)
1670 int downscale = 8;
1672 if ((x/8) * (y/8) > bufsize)
1673 return 0; /* error, too large, even 1:8 doesn't fit */
1675 while ((x*2/downscale) * (y*2/downscale) < bufsize
1676 && downscale > 1)
1678 downscale /= 2;
1680 return downscale;
1684 /* how far can we zoom out, to fit image into the LCD */
1685 int max_downscale(int x, int y)
1687 int downscale = 1;
1689 while ((x/downscale > LCD_WIDTH || y/downscale > LCD_HEIGHT)
1690 && downscale < 8)
1692 downscale *= 2;
1695 return downscale;
1699 /* return decoded or cached image */
1700 struct t_disp* get_image(struct jpeg* p_jpg, int ds)
1702 int w, h; /* used to center output */
1703 int size; /* decompressed image size */
1704 long time; /* measured ticks */
1705 int status;
1707 struct t_disp* p_disp = &disp[ds]; /* short cut */
1709 if (p_disp->bitmap != NULL)
1711 return p_disp; /* we still have it */
1714 /* assign image buffer */
1716 /* physical size needed for decoding */
1717 size = (p_jpg->x_phys/ds) * (p_jpg->y_phys / ds);
1718 if (buf_size <= size)
1719 { /* have to discard the current */
1720 int i;
1721 for (i=1; i<=8; i++)
1722 disp[i].bitmap = NULL; /* invalidate all bitmaps */
1723 buf = buf_root; /* start again from the beginning of the buffer */
1724 buf_size = root_size;
1727 /* size may be less when decoded (if height is not block aligned) */
1728 size = (p_jpg->x_phys/ds) * (p_jpg->y_size / ds);
1729 p_disp->bitmap = buf;
1730 buf += size;
1731 buf_size -= size;
1733 rb->snprintf(print, sizeof(print), "decoding %d*%d",
1734 p_jpg->x_size/ds, p_jpg->y_size/ds);
1735 rb->lcd_puts(0, 3, print);
1736 rb->lcd_update();
1738 /* update image properties */
1739 p_disp->width = p_jpg->x_size/ds;
1740 p_disp->stride = p_jpg->x_phys / ds; /* use physical size for stride */
1741 p_disp->height = p_jpg->y_size/ds;
1743 /* the actual decoding */
1744 time = *rb->current_tick;
1745 status = jpeg_decode(p_jpg, p_disp->bitmap, ds, cb_progess);
1746 if (status)
1748 rb->splash(HZ*2, true, "decode error %d", status);
1749 return NULL;
1751 time = *rb->current_tick - time;
1752 rb->snprintf(print, sizeof(print), " %d.%02d sec ", time/HZ, time%HZ);
1753 rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */
1754 rb->lcd_putsxy((LCD_WIDTH - w)/2, LCD_HEIGHT - h, print);
1755 rb->lcd_update();
1757 return p_disp;
1761 /* set the view to the given center point, limit if necessary */
1762 void set_view (struct t_disp* p_disp, int cx, int cy)
1764 int x, y;
1766 /* plain center to available width/height */
1767 x = cx - MIN(LCD_WIDTH, p_disp->width) / 2;
1768 y = cy - MIN(LCD_HEIGHT, p_disp->height) / 2;
1770 /* limit against upper image size */
1771 x = MIN(p_disp->width - LCD_WIDTH, x);
1772 y = MIN(p_disp->height - LCD_HEIGHT, y);
1774 /* limit against negative side */
1775 x = MAX(0, x);
1776 y = MAX(0, y);
1778 p_disp->x = x; /* set the values */
1779 p_disp->y = y;
1783 /* calculate the view center based on the bitmap position */
1784 void get_view(struct t_disp* p_disp, int* p_cx, int* p_cy)
1786 *p_cx = p_disp->x + MIN(LCD_WIDTH, p_disp->width) / 2;
1787 *p_cy = p_disp->y + MIN(LCD_HEIGHT, p_disp->height) / 2;
1791 /* load, decode, display the image */
1792 int main(char* filename)
1794 int fd;
1795 int filesize;
1796 int grayscales;
1797 int graysize; // helper
1798 unsigned char* buf_jpeg; /* compressed JPEG image */
1799 static struct jpeg jpg; /* too large for stack */
1800 int status;
1801 int ds, ds_min, ds_max; /* scaling and limits */
1802 struct t_disp* p_disp; /* currenly displayed image */
1803 int cx, cy; /* view center */
1805 fd = rb->open(filename, O_RDONLY);
1806 if (fd < 0)
1808 rb->splash(HZ*2, true, "fopen err");
1809 return PLUGIN_ERROR;
1811 filesize = rb->filesize(fd);
1813 rb->memset(&disp, 0, sizeof(disp));
1815 buf = rb->plugin_get_mp3_buffer(&buf_size); /* start munching memory */
1818 /* initialize the grayscale buffer:
1819 * 112 pixels wide, 8 rows (64 pixels) high, (try to) reserve
1820 * 32 bitplanes for 33 shades of gray. (uses 28856 bytes)*/
1821 grayscales = gray_init_buffer(buf, buf_size, 112, 8, 32, &graysize) + 1;
1822 buf += graysize;
1823 buf_size -= graysize;
1824 if (grayscales < 33 || buf_size <= 0)
1826 rb->splash(HZ*2, true, "gray buf error");
1827 return PLUGIN_ERROR;
1831 /* allocate JPEG buffer */
1832 align(&buf, &buf_size, 2); /* 16 bit align */
1833 buf_jpeg = (unsigned char*)(((int)buf + 1) & ~1);
1834 buf += filesize;
1835 buf_size -= filesize;
1836 buf_root = buf; /* we can start the decompressed images behind it */
1837 root_size = buf_size;
1838 if (buf_size <= 0)
1840 rb->splash(HZ*2, true, "out of memory");
1841 rb->close(fd);
1842 return PLUGIN_ERROR;
1845 rb->snprintf(print, sizeof(print), "loading %d bytes", filesize);
1846 rb->lcd_puts(0, 0, print);
1847 rb->lcd_update();
1849 rb->read(fd, buf_jpeg, filesize);
1850 rb->close(fd);
1852 rb->snprintf(print, sizeof(print), "decoding markers");
1853 rb->lcd_puts(0, 1, print);
1854 rb->lcd_update();
1856 rb->memset(&jpg, 0, sizeof(jpg)); /* clear info struct */
1857 /* process markers, unstuffing */
1858 status = process_markers(buf_jpeg, filesize, &jpg);
1859 if (status < 0 || (status & (DQT | SOF0)) != (DQT | SOF0))
1860 { /* bad format or minimum components not contained */
1861 rb->splash(HZ*2, true, "unsupported %d", status);
1862 return PLUGIN_ERROR;
1864 if (!(status & DHT)) /* if no Huffman table present: */
1865 default_huff_tbl(&jpg); /* use default */
1866 build_lut(&jpg); /* derive Huffman and other lookup-tables */
1868 rb->snprintf(print, sizeof(print), "image %d*%d", jpg.x_size, jpg.y_size);
1869 rb->lcd_puts(0, 2, print);
1870 rb->lcd_update();
1872 /* check display constraint */
1873 ds_max = max_downscale(jpg.x_size, jpg.y_size);
1874 /* check memory constraint */
1875 ds_min = min_downscale(jpg.x_phys, jpg.y_phys, buf_size);
1876 if (ds_min == 0)
1878 rb->splash(HZ*2, true, "too large");
1879 return PLUGIN_ERROR;
1881 ds = ds_max; /* initials setting */
1882 cx = jpg.x_size/ds/2; /* center the view */
1883 cy = jpg.y_size/ds/2;
1885 do /* loop the image prepare and decoding when zoomed */
1887 p_disp = get_image(&jpg, ds); /* decode or fetch from cache */
1888 if (p_disp == NULL)
1889 return PLUGIN_ERROR;
1891 set_view(p_disp, cx, cy);
1893 rb->snprintf(print, sizeof(print), "showing %d*%d",
1894 p_disp->width, p_disp->height);
1895 rb->lcd_puts(0, 3, print);
1896 rb->lcd_update();
1898 gray_clear_display();
1899 gray_drawgraymap(
1900 p_disp->bitmap + p_disp->y * p_disp->stride + p_disp->x,
1901 MAX(0, (LCD_WIDTH - p_disp->width) / 2),
1902 MAX(0, (LCD_HEIGHT - p_disp->height) / 2),
1903 MIN(LCD_WIDTH, p_disp->width),
1904 MIN(LCD_HEIGHT, p_disp->height),
1905 p_disp->stride);
1907 gray_show_display(true); /* switch on grayscale overlay */
1909 /* drawing is now finished, play around with scrolling
1910 * until you press OFF or connect USB
1912 while (1)
1914 status = scroll_bmp(p_disp);
1915 if (status == ZOOM_IN)
1917 if (ds > ds_min)
1919 ds /= 2; /* reduce downscaling to zoom in */
1920 get_view(p_disp, &cx, &cy);
1921 cx *= 2; /* prepare the position in the new image */
1922 cy *= 2;
1924 else
1925 continue;
1928 if (status == ZOOM_OUT)
1930 if (ds < ds_max)
1932 ds *= 2; /* increase downscaling to zoom out */
1933 get_view(p_disp, &cx, &cy);
1934 cx /= 2; /* prepare the position in the new image */
1935 cy /= 2;
1937 else
1938 continue;
1940 break;
1943 gray_show_display(false); /* switch off overlay */
1946 while (status != PLUGIN_OK && status != PLUGIN_USB_CONNECTED);
1948 gray_release_buffer(); /* deinitialize */
1950 return status;
1953 /******************** Plugin entry point *********************/
1955 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
1957 /* this macro should be called as the first thing you do in the plugin.
1958 it test that the api version and model the plugin was compiled for
1959 matches the machine it is running on */
1960 TEST_PLUGIN_API(api);
1962 rb = api; /* copy to global api pointer */
1964 /* This plugin uses the grayscale framework, so initialize */
1965 gray_init(api);
1967 return main((char*)parameter);
1970 #endif /* #ifdef HAVE_LCD_BITMAP */
1971 #endif /* #ifndef SIMULATOR */