1 /* Spca561decoder (C) 2005 Andrzej Szombierski [qq@kuku.eu.org] */
2 /*==============================================================*/
3 /* This program is distributed under the terms of the GNU */
4 /* General Public License, version 2. You may use, modify, */
5 /* and redistribute it under the terms of this license. A */
6 /* copy should be included with this source. */
9 * Decoder for compressed spca561 images
10 * It was developed for "Labtec WebCam Elch 2(SPCA561A)" (046d:0929)
11 * but it might work with other spca561 cameras
22 /*fixme: not reentrant */
23 static unsigned int bit_bucket;
24 static const unsigned char *input_ptr;
26 static void refill(int *bitfill)
29 bit_bucket = (bit_bucket << 8) | *(input_ptr++);
34 static int nbits(int *bitfill, int n)
36 bit_bucket = (bit_bucket << 8) | *(input_ptr++);
38 return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1);
41 static int _nbits(int *bitfill, int n)
44 return (bit_bucket >> (*bitfill & 0xff)) & ((1 << n) - 1);
47 static int fun_A(int *bitfill)
51 12, 13, 14, 15, 16, 17, 18, 19, -12, -13, -14, -15,
52 -16, -17, -18, -19, -19
55 ret = tab[nbits(bitfill, 4)];
60 static int fun_B(int *bitfill)
63 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31, 31,
64 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
67 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
70 { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -5,
71 -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17,
76 tmp = nbits(bitfill, 7) - 68;
80 return tab[tab1[tmp]];
82 static int fun_C(int *bitfill, int gkw)
85 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23, 23, 23, 23, 23, 23,
86 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
89 15, 16, 17, 18, 19, 20, 21, 22
92 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -9, -10, -11,
93 -12, -13, -14, -15, -16, -17, -18, -19
98 if (nbits(bitfill, 1) == 0)
107 tmp = nbits(bitfill, 7) - 72;
112 return tab[tab1[tmp]];
114 static int fun_D(int *bitfill, int gkw)
117 if (nbits(bitfill, 1) == 0)
123 if (nbits(bitfill, 1) == 0)
129 switch (nbits(bitfill, 2)) {
142 switch (nbits(bitfill, 3)) {
152 return _nbits(bitfill, 1) ? 0xed : 0x12;
162 static int fun_E(int cur_byte, int *bitfill)
164 static int tab0[] = { 0, -1, 1, -2, 2, -3, 3, -4 };
165 static int tab1[] = { 4, -5, 5, -6, 6, -7, 7, -8 };
166 static int tab2[] = { 8, -9, 9, -10, 10, -11, 11, -12 };
167 static int tab3[] = { 12, -13, 13, -14, 14, -15, 15, -16 };
168 static int tab4[] = { 16, -17, 17, -18, 18, -19, 19, -19 };
170 if ((cur_byte & 0xf0) >= 0x80) {
172 return tab0[(cur_byte >> 4) & 7];
174 if ((cur_byte & 0xc0) == 0x40) {
176 return tab1[(cur_byte >> 3) & 7];
179 if ((cur_byte & 0xe0) == 0x20) {
181 return tab2[(cur_byte >> 2) & 7];
184 if ((cur_byte & 0xf0) == 0x10) {
186 return tab3[(cur_byte >> 1) & 7];
189 if ((cur_byte & 0xf8) == 8) {
191 return tab4[cur_byte & 7];
196 static int fun_F(int cur_byte, int *bitfill)
199 switch (cur_byte & 0xf8) {
235 switch (cur_byte & 0xfc) {
271 switch (cur_byte & 0xfe) {
292 static int spca561_decode(int width, int height,
293 const unsigned char *inbuf,
294 unsigned char *outbuf)
297 static int accum[8 * 8 * 8];
298 static int i_hits[8 * 8 * 8];
300 const int nbits_A[] =
301 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
302 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
305 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
363 const int nbits_B[] =
364 { 0, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
365 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
368 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2,
371 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
374 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
377 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
380 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
383 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
386 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
389 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
392 { 0xff, -4, 3, 3, -3, -3, -3, -3, 2, 2, 2, 2, 2, 2, 2, 2, -2,
393 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
396 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
399 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
402 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
405 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
408 -1, -1, -1, -1, -1, -1, -1, 0, 0,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, 2, 2, 2, 2, 2, 2, 2, -3, -3, -3, -3, -3, -3, -3, -3,
462 -3, -3, -3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
465 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2,
468 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
471 -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
474 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
477 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1,
480 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
483 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
486 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
490 const int nbits_D[] =
491 { 0, 0, 0, 0, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6,
492 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5,
495 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4,
498 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
501 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
504 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
507 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
510 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
513 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
516 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
519 { 0xff, 0xfe, 0xfd, 0xfc, 10, -11, 11, -12, 8, 8, -9, -9, 9, 9,
520 -10, -10, 6, 6, 6, 6, -7, -7, -7, -7, 7, 7, 7, 7, -8, -8,
523 4, 4, 4, 4, -5, -5, -5, -5, -5, -5, -5, -5, 5, 5, 5, 5, 5,
526 -6, -6, -6, -6, -6, -6, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
529 -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
532 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -4, -4, -4, -4, -4, -4, -4,
535 -4, -4, -4, -4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1,
541 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
544 -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
547 1, 1, 1, 1, 1, 1, 1, 1, 1, -2, -2, -2, -2, -2, -2, -2, -2,
550 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
555 /* a_curve[19 + i] = ... [-19..19] => [-160..160] */
556 const int a_curve[] =
557 { -160, -144, -128, -112, -98, -88, -80, -72, -64, -56, -48,
558 -40, -32, -24, -18, -12, -8, -5, -2, 0, 2, 5, 8, 12, 18,
561 72, 80, 88, 98, 112, 128, 144, 160
563 /* clamp0_255[256 + i] = min(max(i,255),0) */
564 const unsigned char clamp0_255[] =
565 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
568 0, 0, 0, 0, 0, 0, 0, 0, 0,
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
571 0, 0, 0, 0, 0, 0, 0, 0, 0,
572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
574 0, 0, 0, 0, 0, 0, 0, 0, 0,
575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
577 0, 0, 0, 0, 0, 0, 0, 0, 0,
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
580 0, 0, 0, 0, 0, 0, 0, 0, 0,
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
583 0, 0, 0, 0, 0, 0, 0, 0, 0,
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
586 0, 0, 0, 0, 0, 0, 0, 0, 0,
587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
589 3, 4, 5, 6, 7, 8, 9, 10,
590 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
592 28, 29, 30, 31, 32, 33,
593 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
595 51, 52, 53, 54, 55, 56,
596 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
598 74, 75, 76, 77, 78, 79,
599 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
601 97, 98, 99, 100, 101,
602 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
604 115, 116, 117, 118, 119,
605 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
607 133, 134, 135, 136, 137,
608 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
610 151, 152, 153, 154, 155,
611 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
613 169, 170, 171, 172, 173,
614 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185,
616 187, 188, 189, 190, 191,
617 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203,
619 205, 206, 207, 208, 209,
620 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
622 223, 224, 225, 226, 227,
623 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
625 241, 242, 243, 244, 245,
626 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 255, 255,
628 255, 255, 255, 255, 255,
629 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
631 255, 255, 255, 255, 255,
632 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
634 255, 255, 255, 255, 255,
635 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
637 255, 255, 255, 255, 255,
638 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
640 255, 255, 255, 255, 255,
641 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
643 255, 255, 255, 255, 255,
644 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
646 255, 255, 255, 255, 255,
647 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
649 255, 255, 255, 255, 255,
650 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
652 255, 255, 255, 255, 255,
653 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
655 255, 255, 255, 255, 255,
656 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
658 255, 255, 255, 255, 255,
659 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
661 255, 255, 255, 255, 255,
662 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
664 255, 255, 255, 255, 255,
665 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
667 255, 255, 255, 255, 255,
668 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
672 /* abs_clamp15[19 + i] = min(abs(i), 15) */
673 const int abs_clamp15[] =
674 { 15, 15, 15, 15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3,
675 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
679 /* diff_encoding[256 + i] = ... */
680 const int diff_encoding[] =
681 { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
682 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
685 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
688 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
691 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
694 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
697 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
700 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
703 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 5,
706 3, 3, 1, 1, 0, 2, 2, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6,
709 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
712 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
715 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
718 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
721 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
724 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
727 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
730 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
737 int xwidth = width + 6;
738 int off_up_right = 2 - 2 * xwidth;
739 int off_up_left = -2 - 2 * xwidth;
740 int pixel_U = 0, saved_pixel_UR = 0;
741 int pixel_x = 0, pixel_y = 2;
742 unsigned char *output_ptr = outbuf;
744 memset(i_hits, 0, sizeof(i_hits));
745 memset(accum, 0, sizeof(accum));
747 memcpy(outbuf + xwidth * 2 + 3, inbuf + 0x14, width);
748 memcpy(outbuf + xwidth * 3 + 3, inbuf + 0x14 + width, width);
750 input_ptr = inbuf + 0x14 + width * 2;
751 output_ptr = outbuf + (xwidth) * 4 + 3;
755 for (block = 0; block < ((height - 2) * width) / 32; ++block) {
761 cur_byte = (bit_bucket >> (bitfill & 7)) & 0xff;
763 if ((cur_byte & 0x80) == 0) {
766 } else if ((cur_byte & 0xC0) == 0x80) {
769 } else if ((cur_byte & 0xc0) == 0xc0) {
774 for (b_it = 0; b_it < 32; b_it++) {
776 int pixel_L, pixel_UR, pixel_UL;
779 int gkw; /* God knows what */
782 cur_byte = bit_bucket >> (bitfill & 7) & 0xff;
784 pixel_L = output_ptr[-2];
785 pixel_UR = output_ptr[off_up_right];
786 pixel_UL = output_ptr[off_up_left];
788 dL = diff_encoding[0x100 + pixel_UL - pixel_L];
789 dC = diff_encoding[0x100 + pixel_U - pixel_UL];
790 dR = diff_encoding[0x100 + pixel_UR - pixel_U];
793 pixel_L = pixel_UL = pixel_U =
794 output_ptr[-xwidth * 2];
795 pixel_UR = output_ptr[off_up_right];
797 dR = diff_encoding[0x100 + pixel_UR -
799 } else if (pixel_x > width - 3)
803 index = dR + dC * 8 + dL * 64;
805 if (pixel_L + pixel_U * 2 <= 144
806 && (pixel_y & 1) == 0
807 && (b_it & 3) == 0 && (dR < 5) && (dC < 5)
810 } else if (pixel_L <= 48
811 && dL <= 4 && dC <= 4 && dL >= 1
814 } else if (var_7 == 1) {
816 } else if (dC + dL >= 11 || var_7 == 2) {
820 if (i_hits[index] < 7) {
821 bitfill -= nbits_A[cur_byte];
822 gkw = tab_A[cur_byte];
824 gkw = fun_A(&bitfill);
825 } else if (i_hits[index] >= accum[index]) {
826 bitfill -= nbits_B[cur_byte];
827 gkw = tab_B[cur_byte];
829 gkw = fun_B(&bitfill);
830 } else if (i_hits[index] * 2 >= accum[index]) {
831 bitfill -= nbits_C[cur_byte];
832 gkw = tab_C[cur_byte];
834 gkw = fun_C(&bitfill, gkw);
835 } else if (i_hits[index] * 4 >= accum[index]) {
836 bitfill -= nbits_D[cur_byte];
837 gkw = tab_D[cur_byte];
839 gkw = fun_D(&bitfill, gkw);
840 } else if (i_hits[index] * 8 >= accum[index]) {
841 gkw = fun_E(cur_byte, &bitfill);
843 gkw = fun_F(cur_byte, &bitfill);
853 (pixel_U + pixel_L) * 3 - pixel_UL * 2;
854 tmp1 += (tmp1 < 0) ? 3 : 0;
855 tmp2 = a_curve[19 + gkw] * multiplier;
856 tmp2 += (tmp2 < 0) ? 1 : 0;
859 clamp0_255[0x100 + (tmp1 >> 2) -
862 pixel_U = saved_pixel_UR;
863 saved_pixel_UR = pixel_UR;
865 if (++pixel_x == width) {
871 accum[index] += abs_clamp15[19 + gkw];
873 if (i_hits[index]++ == 15) {
882 /* FIXME, change spca561_decode not to need the extra border
883 around its dest buffer */
884 int fswc_add_image_s561(avgbmp_t *dst, uint8_t *img, uint32_t length, uint32_t width, uint32_t height, int palette)
888 unsigned char tmpimg[650 * 490];
890 if(spca561_decode(width, height, img, tmpimg) != 0)
895 /* Remove buffer border */
897 s = tmpimg + 2 * (width + 6) + 3;
898 for(y = 0; y < height; y++)
900 for(x = 0; x < width; x++) *(d++) = *(s++);
904 fswc_add_image_bayer(dst, tmpimg, width * height, width, height, SRC_PAL_SGBRG8);