File: speck.c

package info (click to toggle)
libepsilon 0.9.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,192 kB
  • sloc: ansic: 11,329; sh: 9,321; perl: 936; xml: 86; makefile: 85
file content (1126 lines) | stat: -rw-r--r-- 31,508 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
/*
 * $Id: speck.c,v 1.62 2010/04/05 05:01:04 simakov Exp $
 *
 * EPSILON - wavelet image compression library.
 * Copyright (C) 2006,2007,2010 Alexander Simakov, <xander@entropyware.info>
 *
 * This file is part of EPSILON
 *
 * EPSILON is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EPSILON is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with EPSILON.  If not, see <http://www.gnu.org/licenses/>.
 *
 * http://epsilon-project.sourceforge.net
 */

#include <common.h>
#include <speck.h>
#include <mem_alloc.h>
#include <list.h>
#include <bit_io.h>
#include <filter.h>
#include <color.h>

/* Before you dive into the sources, note that
 * X and Y axes here are swapped. In other words,
 * X denotes vertical position while Y denotes
 * horizontal position. The origin is the top-left
 * point. Now you a warned. */

local int max_coeff(int **channel, int channel_size)
{
    int i, j, max = 0;

    for (i = 0; i < channel_size; i++) {
        for (j = 0; j < channel_size; j++) {
            if (ABS(channel[i][j]) > max) {
                max = ABS(channel[i][j]);
            }
        }
    }

    return max;
}

local int validate_set(pixel_set *set, int channel_size)
{
    int base_size = channel_size & (~1);

    /* Basic checks */
    if ((set->type != TYPE_POINT) && (set->type != TYPE_S) && (set->type != TYPE_I))
        return 0;

    if ((set->x < 0) || (set->x >= channel_size))
        return 0;

    if ((set->y < 0) || (set->y >= channel_size))
        return 0;

    if ((set->width <= 0) || (set->width > channel_size))
        return 0;

    if ((set->height <= 0) || (set->height > channel_size))
        return 0;

    if (set->x + set->height > channel_size)
        return 0;

    if (set->y + set->width > channel_size)
        return 0;

    switch (set->type) {
        case TYPE_POINT:
        {
            /* Single point */
            if ((set->width != 1) || (set->height != 1))
                return 0;

            return 1;
        }
        case TYPE_S:
        {
            /* Set of type 'S' */
            int scale;

            if ((set->width == 1) && (set->height == 1))
                return 0;

            if (channel_size == base_size) {
                /* Normal mode */
                if (set->width != set->height)
                    return 0;

                if (!is_power_of_two(set->width))
                    return 0;

                scale = number_of_bits(set->width) - 1;

                if ((set->x) & (~(~0 << scale)))
                    return 0;

                if ((set->y) & (~(~0 << scale)))
                    return 0;

                return 1;
            } else {
                /* OTLPF mode */
                if ((set->x == 0) && (set->y == 0)) {
                    /* Origin */
                    if (set->width != set->height)
                        return 0;

                    if (!is_power_of_two(set->width - 1))
                        return 0;

                    return 1;
                } else if ((set->x == 0) && (set->y > 1)) {
                    /* Top border */
                    if (set->height - set->width != 1)
                        return 0;

                    if (!is_power_of_two(set->width))
                        return 0;

                    scale = number_of_bits(set->width) - 1;

                    if ((set->y - 1) & (~(~0 << scale)))
                        return 0;

                    return 1;
                } else if ((set->x > 1) && (set->y == 0)) {
                    /* Left border */
                    if (set->width - set->height != 1)
                        return 0;

                    if (!is_power_of_two(set->height))
                        return 0;

                    scale = number_of_bits(set->height) - 1;

                    if ((set->x - 1) & (~(~0 << scale)))
                        return 0;

                    return 1;
                } else if ((set->x > 1) && (set->y > 1)) {
                    /* Internal */
                    if (set->width != set->height)
                        return 0;

                    if (!is_power_of_two(set->width))
                        return 0;

                    scale = number_of_bits(set->width) - 1;

                    if ((set->x - 1) & (~(~0 << scale)))
                        return 0;

                    if ((set->y - 1) & (~(~0 << scale)))
                        return 0;

                    return 1;
                } else {
                    return 0;
                }
            }
        }
        case TYPE_I:
        {
            /* Set of type 'I' */
            if (set->x != set->y)
                return 0;

            if (set->width != set->height)
                return 0;

            if (set->x + set->width != channel_size)
                return 0;

            if (channel_size == base_size) {
                /* Normal mode */
                if (!is_power_of_two(set->x))
                    return 0;
            } else {
                /* OTLPF mode */
                if (!is_power_of_two(set->x - 1))
                    return 0;
            }

            return 1;
        }
        default:
        {
            return 0;
        }
    }
}

local int significance_test(pixel_set *set, int threshold,
                            int **channel, int channel_size)
{
#ifdef ENABLE_SET_VALIDATION
    /* Ensure that the set is valid */
    assert(validate_set(set, channel_size));
#endif

    assert(threshold > 0);

    switch (set->type) {
        case TYPE_POINT:
        {
            /* Single point */
            return (ABS(channel[set->x][set->y]) >= threshold);
            break;
        }
        case TYPE_S:
        {
            /* Set of type 'S' */
            int x, y;

            for (x = set->x; x < set->x + set->height; x++) {
                for (y = set->y; y < set->y + set->width; y++) {
                    if (ABS(channel[x][y]) >= threshold) {
                        return 1;
                    }
                }
            }

            return 0;
            break;
        }
        case TYPE_I:
        {
            /* Set of type 'I' */
            int x, y;

            for (x = 0; x < channel_size; x++) {
                for (y = 0; y < channel_size; y++) {
                    if ((x >= set->x) || (y >= set->y)) {
                        if (ABS(channel[x][y]) >= threshold) {
                            return 1;
                        }
                    }
                }
            }

            return 0;
            break;
        }
        default:
        {
            assert(0);
            break;
        }
    }

    /* This code is unreachable, just to be on a safe side */
    assert(0);

    /* Fake return disables 'not all control paths return a value' warning */
    return -1;
}

local void select_part_type(pixel_set *set)
{
    /* Guess the set type after split operation */
    if ((set->width == 1) && (set->height == 1)) {
        set->type = TYPE_POINT;
    } else if ((set->width == 0) || (set->height == 0)) {
        set->type = TYPE_EMPTY;
    } else {
        set->type = TYPE_S;
    }
}

local void split_set(pixel_set *set, pixel_set *part1, pixel_set *part2,
                     pixel_set *part3, pixel_set *part4, int channel_size)
{
    int base_size = channel_size & (~1);

#ifdef ENABLE_SET_VALIDATION
    /* Ensure that the set is valid */
    assert(validate_set(set, channel_size));
#endif

    switch (set->type) {
        case TYPE_S:
        {
            /* Split parent set of type 'S' */
            part1->x = set->x;
            part1->y = set->y;
            part1->width = (set->width + 1) / 2;
            part1->height = (set->height + 1) / 2;
            select_part_type(part1);

            part2->x = set->x;
            part2->y = set->y + (set->width + 1) / 2;
            part2->width = set->width / 2;
            part2->height = (set->height + 1) / 2;
            select_part_type(part2);

            part3->x = set->x + (set->height + 1) / 2;
            part3->y = set->y;
            part3->width = (set->width + 1) / 2;
            part3->height = set->height / 2;
            select_part_type(part3);

            part4->x = set->x + (set->height + 1) / 2;
            part4->y = set->y + (set->width + 1) / 2;
            part4->width = set->width / 2;
            part4->height = set->height / 2;
            select_part_type(part4);

            break;
        }
        case TYPE_I:
        {
            /* Split parent set of type 'I' */
            int p0, p1;
            int scale;

            scale = number_of_bits(set->x - (channel_size != base_size));

            p0 = set->x;
            p1 = (1 << scale) + (channel_size != base_size);

            part1->x = part1->y = p1;
            part1->width = part1->height = channel_size - p1;
            part1->type = (p1 == channel_size) ? TYPE_EMPTY : TYPE_I;

            part2->x = 0;
            part2->y = p0;
            part2->width = p1 - p0;
            part2->height = p0;
            select_part_type(part2);

            part3->x = p0;
            part3->y = 0;
            part3->width = p0;
            part3->height = p1 - p0;
            select_part_type(part3);

            part4->x = part4->y = p0;
            part4->width = part4->height = p1 - p0;
            select_part_type(part4);

            break;
        }
        default:
        {
            assert(0);
            break;
        }
    }
}

local linked_list **alloc_LIS_slots(int channel_size)
{
    linked_list **LIS_slots;
    int n_slots;
    int i;

    /* Think of this structure as a list of lists. Splitting
     * entire list into several slots speed-ups algorithm:
     * one slot for each scale. */
    n_slots = number_of_bits(channel_size);
    LIS_slots = (linked_list **) xmalloc(n_slots * sizeof(linked_list *));

    for (i = 0; i < n_slots; i++) {
        LIS_slots[i] = alloc_linked_list();
    }

    return LIS_slots;
}

local void free_LIS_slots(linked_list **LIS_slots, int channel_size)
{
    int n_slots;
    int i;

    n_slots = number_of_bits(channel_size);

    for (i = 0; i < n_slots; i++) {
        free_linked_list(LIS_slots[i]);
    }

    free(LIS_slots);
}

local void assign_set(list_node *node, pixel_set *set)
{
    PIXEL_SET(node)->type = set->type;
    PIXEL_SET(node)->x = set->x;
    PIXEL_SET(node)->y = set->y;
    PIXEL_SET(node)->width = set->width;
    PIXEL_SET(node)->height = set->height;
}

local void zero_channel(int **channel, int channel_size)
{
    int i, j;

    /* Reset everything to zero */
    for (i = 0; i < channel_size; i++) {
        for (j = 0; j < channel_size; j++) {
            channel[i][j] = 0;
        }
    }
}

local int speck_encode_S(int **channel, int channel_size,
                         pixel_set *set, linked_list **LIS_slots,
                         linked_list *LSP, bit_buffer *bb,
                         int threshold)
{
    pixel_set new_sets[4];
    int result;
    int st[4];
    int flag;
    int i;

    /* Split parent set */
    split_set(set, &new_sets[0], &new_sets[1], &new_sets[2], &new_sets[3], channel_size);

    /* Test each set for significance skipping over empty sets */
    for (flag = 0, i = 3; i >= 0; i--) {
        if (new_sets[i].type == TYPE_EMPTY) {
            continue;
        }

        st[i] = significance_test(&new_sets[i], threshold, channel, channel_size);

        if (i) {
            flag |= st[i];
        }

        /* If parent set is significant, but first three
         * child sets are not, than undoubtedly fourth
         * child set is significant: there is no need
         * to code this explicitly. Using this trick
         * saves some bit-budget. */
        if (i || flag) {
            result = st[i] ? write_1(bb) : write_0(bb);
            RETURN_IF_OVERFLOW(result);
        }
    }

    /* Process non-empty sets using their significance information */
    for (i = 0; i < 4; i++) {
        if (new_sets[i].type == TYPE_EMPTY) {
            continue;
        }

        if (st[i]) {
            /* Significant set */
            if (new_sets[i].type == TYPE_POINT) {
                /* Single point */
                list_node *new_node;

                /* Encode coefficient sign */
                result = channel[new_sets[i].x][new_sets[i].y] > 0 ? write_0(bb) : write_1(bb);
                RETURN_IF_OVERFLOW(result);

                new_node = alloc_list_node(sizeof(pixel_set));
                assign_set(new_node, &new_sets[i]);
                append_list_node(LSP, new_node);
            } else {
                /* Encode set of type 'S' */
                result = speck_encode_S(channel, channel_size, &new_sets[i],
                                        LIS_slots, LSP, bb, threshold);
    
                RETURN_IF_OVERFLOW(result);
            }
        } else {
            /* Insignificant set */
            list_node *new_node = alloc_list_node(sizeof(pixel_set));
            assign_set(new_node, &new_sets[i]);
            prepend_list_node(LIS_slots[SLOT_INDEX((&new_sets[i]))], new_node);            
        }
    }

    return BIT_BUFFER_OK;
}

local int speck_process_S(int **channel, int channel_size,
                          list_node *node, linked_list *slot,
                          linked_list **LIS_slots, linked_list *LSP,
                          bit_buffer *bb, int threshold,
                          int coding_stage)
{
    pixel_set *set;
    int result;
    int st;

    /* Test the set for significance */
    set = PIXEL_SET(node);
    st = significance_test(set, threshold, channel, channel_size);

    result = st ? write_1(bb) : write_0(bb);
    RETURN_IF_OVERFLOW(result);

    if (st) {
        /* Significant set */
        if (set->type == TYPE_POINT) {
            /* Single point: encode coefficient sign */
            result = channel[set->x][set->y] > 0 ? write_0(bb) : write_1(bb);
            RETURN_IF_OVERFLOW(result);

            if (coding_stage == STAGE_S) {
                remove_list_node_link(slot, node);
            }

            append_list_node(LSP, node);
        } else {
            /* Encode set of type 'S' */
            result = speck_encode_S(channel, channel_size, set,
                                    LIS_slots, LSP, bb, threshold);

            RETURN_IF_OVERFLOW(result);

            if (coding_stage == STAGE_S) {
                remove_list_node(slot, node);
            } else {
                free_list_node(node);
            }
        }
    } else {
        /* Insignificant set */
        if (coding_stage == STAGE_I) {
            prepend_list_node(LIS_slots[SLOT_INDEX(set)], node);
        }
    }

    return BIT_BUFFER_OK;
}

local int speck_encode_I(int **channel, int channel_size, pixel_set *I,
                         linked_list **LIS_slots, linked_list *LSP,
                         bit_buffer *bb, int threshold)
{
    pixel_set new_sets[3];
    int result;
    int i;

    /* Split parent set */
    split_set(I, I, &new_sets[0], &new_sets[1], &new_sets[2], channel_size);

    /* Process child sets of type 'S' */
    for (i = 0; i < 3; i++) {
        list_node *node = alloc_list_node(sizeof(pixel_set));
        assign_set(node, &new_sets[i]);

        /* Process child set of type 'S' */
        result = speck_process_S(channel, channel_size, node,
                                 NULL, LIS_slots, LSP, bb,
                                 threshold, STAGE_I);

        if (result == BIT_BUFFER_OVERFLOW) {
            free_list_node(node);
            return result;
        }
    }

    /* Process child set of type 'I' */
    result = speck_process_I(channel, channel_size, I,
                             LIS_slots, LSP, bb, threshold);

    return result;
}

local int speck_process_I(int **channel, int channel_size, pixel_set *I,
                          linked_list **LIS_slots, linked_list *LSP,
                          bit_buffer *bb, int threshold)
{
    int result;
    int st;

    /* Skip over empty sets */
    if (I->type == TYPE_EMPTY) {
        return BIT_BUFFER_OK;
    }

    /* Test the set for significance */
    st = significance_test(I, threshold, channel, channel_size);

    result = st ? write_1(bb) : write_0(bb);
    RETURN_IF_OVERFLOW(result);

    if (st) {
        /* Encode set of type 'I' */
        result = speck_encode_I(channel, channel_size, I,
                                LIS_slots, LSP, bb, threshold);

        RETURN_IF_OVERFLOW(result);
    }

    return BIT_BUFFER_OK;
}

local int encode_sorting_pass(int **channel, int channel_size,
                              linked_list **LIS_slots, linked_list *LSP,
                              pixel_set *I, bit_buffer *bb,
                              int threshold)
{
    int n_slots;
    int result;
    int i;

    n_slots = number_of_bits(channel_size);

    /* Travels through all LIS slots */
    for (i = 0; i < n_slots; i++) {
        linked_list *cur_slot = LIS_slots[i];
        list_node *cur_node;

        /* Skip over empty slots */
        CONTINUE_IF_EMPTY(cur_slot);

        /* Get first slot node */
        cur_node = cur_slot->first;

        /* Process all nodes within this slot */
        while (cur_node) {
            list_node *next_node = cur_node->next;

            /* Process set of type 'S' */
            result = speck_process_S(channel, channel_size, cur_node,
                                     cur_slot, LIS_slots, LSP, bb,
                                     threshold, STAGE_S);

            RETURN_IF_OVERFLOW(result);

            /* Next node */
            cur_node = next_node;
        }
    }

    /* Process set of type 'I' */
    result = speck_process_I(channel, channel_size, I,
                             LIS_slots, LSP, bb, threshold);

    return result;
}

local int encode_refinement_pass(int **channel, linked_list *LSP,
                                 bit_buffer *bb, int threshold)
{
    list_node *node;
    int result;

    node = LSP->first;
    threshold <<= 1;

    /* Travels through all nodes in LSP */
    while (node) {
        pixel_set *set = PIXEL_SET(node);
        int coeff = ABS(channel[set->x][set->y]);

        /* Output next bit */
        if (coeff >= threshold) {
            result = coeff & (threshold >> 1) ? write_1(bb) : write_0(bb);
            RETURN_IF_OVERFLOW(result);
        }

        /* Next node */
        node = node->next;
    }

    return BIT_BUFFER_OK;
}

local int speck_decode_S(int **channel, int channel_size,
                         pixel_set *set, linked_list **LIS_slots,
                         linked_list *LSP, bit_buffer *bb,
                         int threshold)
{
    pixel_set new_sets[4];
    int result;
    int st[4];
    int flag;
    int i;

    /* Split parent set */
    split_set(set, &new_sets[0], &new_sets[1], &new_sets[2], &new_sets[3], channel_size);

    /* Test each set for significance skipping over empty sets */
    for (flag = 0, i = 3; i >= 0; i--) {
        if (new_sets[i].type == TYPE_EMPTY) {
            continue;
        }

        if (i) {
            result = read_bit(bb, &st[i]);
            RETURN_IF_UNDERFLOW(result);

            flag |= st[i];
        } else {
            if (flag) {
                result = read_bit(bb, &st[i]);
                RETURN_IF_UNDERFLOW(result);
            } else {
                /* Implicitly significant set */
                st[i] = 1;
            }
        }
    }

    /* Process non-empty sets using their significance information */
    for (i = 0; i < 4; i++) {
        if (new_sets[i].type == TYPE_EMPTY) {
            continue;
        }

        if (st[i]) {
            /* Significant set */
            if (new_sets[i].type == TYPE_POINT) {
                /* Single point */
                list_node *new_node;
                int sign = 0;

                result = read_bit(bb, &sign);
                RETURN_IF_UNDERFLOW(result);

                /* Decode coefficient sign */
                if (sign) {
                    channel[new_sets[i].x][new_sets[i].y] =
                        -(threshold + (threshold >> 1));
                } else {
                    channel[new_sets[i].x][new_sets[i].y] =
                         (threshold + (threshold >> 1));
                }

                new_node = alloc_list_node(sizeof(pixel_set));
                assign_set(new_node, &new_sets[i]);
                append_list_node(LSP, new_node);
            } else {
                /* Decode set of type 'S' */
                result = speck_decode_S(channel, channel_size, &new_sets[i],
                                        LIS_slots, LSP, bb, threshold);
    
                RETURN_IF_UNDERFLOW(result);
            }
        } else {
            /* Insignificant set */
            list_node *new_node = alloc_list_node(sizeof(pixel_set));
            assign_set(new_node, &new_sets[i]);
            prepend_list_node(LIS_slots[SLOT_INDEX((&new_sets[i]))], new_node);            
        }
    }

    return BIT_BUFFER_OK;
}

local int speck_unprocess_S(int **channel, int channel_size,
                            list_node *node, linked_list *slot,
                            linked_list **LIS_slots, linked_list *LSP,
                            bit_buffer *bb, int threshold,
                            int coding_stage)
{
    pixel_set *set;
    int result;
    int st;

    set = PIXEL_SET(node);

    /* Read set significance information */
    result = read_bit(bb, &st);
    RETURN_IF_UNDERFLOW(result);

    if (st) {
        /* Significant set */
        if (set->type == TYPE_POINT) {
            int sign = 0;

            /* Single point: read coefficient sign */
            result = read_bit(bb, &sign);
            RETURN_IF_UNDERFLOW(result);

            if (sign) {
                channel[set->x][set->y] = -(threshold + (threshold >> 1));
            } else {
                channel[set->x][set->y] =  (threshold + (threshold >> 1));
            }

            if (coding_stage == STAGE_S) {
                remove_list_node_link(slot, node);
            }

            append_list_node(LSP, node);
        } else {
            /* Decode set of type 'S' */
            result = speck_decode_S(channel, channel_size, set,
                                    LIS_slots, LSP, bb, threshold);

            RETURN_IF_UNDERFLOW(result);

            if (coding_stage == STAGE_S) {
                remove_list_node(slot, node);
            } else {
                free_list_node(node);
            }
        }
    } else {
        /* Insignificant set */
        if (coding_stage == STAGE_I) {
            prepend_list_node(LIS_slots[SLOT_INDEX(set)], node);
        }
    }

    return BIT_BUFFER_OK;
}

local int speck_decode_I(int **channel, int channel_size, pixel_set *I,
                         linked_list **LIS_slots, linked_list *LSP,
                         bit_buffer *bb, int threshold)
{
    pixel_set new_sets[3];
    int result;
    int i;

    /* Split parent set */
    split_set(I, I, &new_sets[0], &new_sets[1], &new_sets[2], channel_size);

    /* Unprocess sets of type 'S' */
    for (i = 0; i < 3; i++) {
        list_node *node = alloc_list_node(sizeof(pixel_set));
        assign_set(node, &new_sets[i]);

        result = speck_unprocess_S(channel, channel_size, node,
                                   NULL, LIS_slots, LSP, bb,
                                   threshold, STAGE_I);

        if (result == BIT_BUFFER_UNDERFLOW) {
            free_list_node(node);
            return result;
        }
    }

    /* Unprocess set of type 'I' */
    result = speck_unprocess_I(channel, channel_size, I,
                               LIS_slots, LSP, bb, threshold);

    return result;
}

local int speck_unprocess_I(int **channel, int channel_size,
                            pixel_set *I, linked_list **LIS_slots,
                            linked_list *LSP, bit_buffer *bb,
                            int threshold)
{
    int result;
    int st;

    /* Skip over empty sets */
    if (I->type == TYPE_EMPTY) {
        return BIT_BUFFER_OK;
    }

    /* Read significance information */
    result = read_bit(bb, &st);
    RETURN_IF_UNDERFLOW(result);

    if (st) {
        result = speck_decode_I(channel, channel_size, I,
                                LIS_slots, LSP, bb, threshold);

        RETURN_IF_UNDERFLOW(result);
    }

    return BIT_BUFFER_OK;
}

local int decode_sorting_pass(int **channel, int channel_size,
                              linked_list **LIS_slots, linked_list *LSP,
                              pixel_set *I, bit_buffer *bb,
                              int threshold)
{
    int n_slots;
    int result;
    int i;

    n_slots = number_of_bits(channel_size);

    /* Travels through all LIS slots */
    for (i = 0; i < n_slots; i++) {
        linked_list *cur_slot = LIS_slots[i];
        list_node *cur_node;

        /* Skip over empty slots */
        CONTINUE_IF_EMPTY(cur_slot);

        /* Get first node */
        cur_node = cur_slot->first;

        /* Process all nodes within this slot */
        while (cur_node) {
            list_node *next_node = cur_node->next;

            /* Unprocess set of type 'S' */
            result = speck_unprocess_S(channel, channel_size, cur_node,
                                       cur_slot, LIS_slots, LSP,
                                       bb, threshold, STAGE_S);

            RETURN_IF_UNDERFLOW(result);

            /* Next node */
            cur_node = next_node;
        }
    }

    /* Unprocess set of type 'I' */
    result = speck_unprocess_I(channel, channel_size, I,
                               LIS_slots, LSP, bb, threshold);

    return result;
}

local int decode_refinement_pass(int **channel, linked_list *LSP,
                                 bit_buffer *bb, int threshold)
{
    list_node *node;
    int result;
    int mask;

    node = LSP->first;

    mask = threshold;
    threshold <<= 1;

    /* Travels through all nodes in LSP */
    while (node) {
        pixel_set *set = PIXEL_SET(node);

        int coeff = ABS(channel[set->x][set->y]);
        int sign = channel[set->x][set->y] < 0;

        if (coeff >= threshold) {
            int bit = 0;

            /* Read and shift-in next bit */
            result = read_bit(bb, &bit);
            RETURN_IF_OVERFLOW(result);

            if (bit) {
                coeff |= mask;
            } else {
                coeff &= ~mask;
            }

            coeff |= (mask >> 1);
            channel[set->x][set->y] = sign ? -coeff : coeff;
        }

        /* Next node */
        node = node->next;
    }

    return BIT_BUFFER_OK;
}

local void speck_init(linked_list **LIS_slots, pixel_set *I,
                      int channel_size, int mode)
{
    list_node *root;

    root = alloc_list_node(sizeof(pixel_set));

    /* Setup root node */
    if (mode == MODE_NORMAL) {
        PIXEL_SET(root)->type = TYPE_POINT;
        PIXEL_SET(root)->x = PIXEL_SET(root)->y = 0;
        PIXEL_SET(root)->width = PIXEL_SET(root)->height = 1;

        I->type = TYPE_I;
        I->x = I->y = 1;
        I->width = I->height = channel_size - 1;

        prepend_list_node(LIS_slots[0], root);
    } else {
        PIXEL_SET(root)->type = TYPE_S;
        PIXEL_SET(root)->x = PIXEL_SET(root)->y = 0;
        PIXEL_SET(root)->width = PIXEL_SET(root)->height = 2;

        I->type = TYPE_I;
        I->x = I->y = 2;
        I->width = I->height = channel_size - 2;

        prepend_list_node(LIS_slots[1], root);
    }
}

int speck_encode(int **channel, int channel_size,
                 unsigned char *buf, int buf_size)
{
    int threshold_bits;
    int threshold;
    int result;
    int mode;
    int n_bytes;

    linked_list **LIS_slots;
    linked_list *LSP;
    pixel_set *I;

    bit_buffer *bb;

    mode = channel_size & 1;

    /* Sanity checks */
    assert(buf_size >= MIN_SPECK_BUF_SIZE);
    assert(channel_size >= 2);

    /* Allocate list of significant pixels (LSP),
     * list of lists of insignificant sets (LIS_slots),
     * and set of type 'I' */
    LSP = alloc_linked_list();
    LIS_slots = alloc_LIS_slots(channel_size);
    I = (pixel_set *) xmalloc(sizeof(pixel_set));

    /* Setup initial encoding threshold */
    threshold_bits = number_of_bits(max_coeff(channel, channel_size));
    threshold = threshold_bits ? (1 << (threshold_bits - 1)) : 0;

    /* Allocate bit-buffer */
    bb = (bit_buffer *) xmalloc(sizeof(bit_buffer));

    /* Initialize bit-buffer */
    init_bits(bb, buf, buf_size);
    write_bits(bb, threshold_bits, THRESHOLD_BITS);

    /* Setup encoder */
    speck_init(LIS_slots, I, channel_size, mode);

    /* Travels through all bit planes */
    while (threshold > 0) {
        /* Sorting pass */
        result = encode_sorting_pass(channel, channel_size, LIS_slots, LSP, I, bb, threshold);
        BREAK_IF_OVERFLOW(result);

        /* Refinement pass */
        result = encode_refinement_pass(channel, LSP, bb, threshold);
        BREAK_IF_OVERFLOW(result);

        /* Proceed to the next bit plane */
        threshold >>= 1;
    }

    /* Flush bit-buffer */
    flush_bits(bb);
    n_bytes = bb->next - bb->start;

    free(bb);
    free(I);
    free_LIS_slots(LIS_slots, channel_size);
    free_linked_list(LSP);

    return n_bytes;
}

void speck_decode(unsigned char *buf, int buf_size,
                  int **channel, int channel_size)
{
    int threshold_bits;
    int threshold;
    int result;
    int mode;

    linked_list **LIS_slots;
    linked_list *LSP;
    pixel_set *I;

    bit_buffer *bb;

    mode = channel_size & 1;

    /* Sanity checks */
    assert(buf_size >= MIN_SPECK_BUF_SIZE);
    assert(channel_size >= 2);

    /* Reset output channel */
    zero_channel(channel, channel_size);

    /* Allocate list of significant pixels (LSP),
     * list of lists of insignificant sets (LIS_slots),
     * and set of type 'I' */
    LSP = alloc_linked_list();
    LIS_slots = alloc_LIS_slots(channel_size);
    I = (pixel_set *) xmalloc(sizeof(pixel_set));

    /* Allocate bit-buffer */
    bb = (bit_buffer *) xmalloc(sizeof(bit_buffer));

    /* Initialize bit-buffer */
    init_bits(bb, buf, buf_size);
    read_bits(bb, &threshold_bits, THRESHOLD_BITS);

    /* Read encoding threshold */
    threshold = threshold_bits ? (1 << (threshold_bits - 1)) : 0;
    speck_init(LIS_slots, I, channel_size, mode);

    /* Travels through all bit planes */
    while (threshold > 0) {
        /* Decode sorting pass */
        result = decode_sorting_pass(channel, channel_size, LIS_slots, LSP, I, bb, threshold);
        BREAK_IF_UNDERFLOW(result);

        /* Decode refinement pass */
        result = decode_refinement_pass(channel, LSP, bb, threshold);
        BREAK_IF_UNDERFLOW(result);

        /* Proceed to the next bit plane */
        threshold >>= 1;
    }

    free(bb);
    free(I);
    free_LIS_slots(LIS_slots, channel_size);
    free_linked_list(LSP);
}