aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/Unpack.c
blob: 279bdcdc8ad80dbfe6b89a5efa50276062e119e1 (plain) (blame)
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
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
/*
 * The Python Imaging Library.
 * $Id$
 *
 * code to unpack raw data from various file formats
 *
 * history:
 * 1996-03-07 fl   Created (from various decoders)
 * 1996-04-19 fl   Added band unpackers
 * 1996-05-12 fl   Published RGB unpackers
 * 1996-05-27 fl   Added nibble unpacker
 * 1996-12-10 fl   Added complete set of PNG unpackers
 * 1996-12-29 fl   Set alpha byte in RGB unpackers
 * 1997-01-05 fl   Added remaining TGA unpackers
 * 1997-01-18 fl   Added inverting band unpackers
 * 1997-01-25 fl   Added FlashPix unpackers
 * 1997-05-31 fl   Added floating point unpackers
 * 1998-02-08 fl   Added I unpacker
 * 1998-07-01 fl   Added YCbCr unpacker
 * 1998-07-02 fl   Added full set of integer unpackers
 * 1998-12-29 fl   Added mode field, I;16 unpackers
 * 1998-12-30 fl   Added RGBX modes
 * 1999-02-04 fl   Fixed I;16 unpackers
 * 2003-05-13 fl   Added L/RGB reversed unpackers
 * 2003-09-26 fl   Added LA/PA and RGBa->RGB unpackers
 *
 * Copyright (c) 1997-2003 by Secret Labs AB.
 * Copyright (c) 1996-1997 by Fredrik Lundh.
 *
 * See the README file for information on usage and redistribution.
 */

#include "Imaging.h"
#include "Convert.h"

#define R 0
#define G 1
#define B 2
#define X 3

#define A 3

#define C 0
#define M 1
#define Y 2
#define K 3

/* byte-swapping macros */

#define C16N (tmp[0] = in[0], tmp[1] = in[1]);
#define C16S (tmp[1] = in[0], tmp[0] = in[1]);
#define C32N (tmp[0] = in[0], tmp[1] = in[1], tmp[2] = in[2], tmp[3] = in[3]);
#define C32S (tmp[3] = in[0], tmp[2] = in[1], tmp[1] = in[2], tmp[0] = in[3]);
#define C64N         \
    (tmp[0] = in[0], \
     tmp[1] = in[1], \
     tmp[2] = in[2], \
     tmp[3] = in[3], \
     tmp[4] = in[4], \
     tmp[5] = in[5], \
     tmp[6] = in[6], \
     tmp[7] = in[7]);
#define C64S         \
    (tmp[7] = in[0], \
     tmp[6] = in[1], \
     tmp[5] = in[2], \
     tmp[4] = in[3], \
     tmp[3] = in[4], \
     tmp[2] = in[5], \
     tmp[1] = in[6], \
     tmp[0] = in[7]);

#ifdef WORDS_BIGENDIAN
#define C16B C16N
#define C16L C16S
#define C32B C32N
#define C32L C32S
#define C64B C64N
#define C64L C64S
#else
#define C16B C16S
#define C16L C16N
#define C32B C32S
#define C32L C32N
#define C64B C64S
#define C64L C64N
#endif

/* bit-swapping */

static UINT8 BITFLIP[] = {
    0,  128, 64, 192, 32, 160, 96,  224, 16, 144, 80, 208, 48, 176, 112, 240,
    8,  136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
    4,  132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
    12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
    2,  130, 66, 194, 34, 162, 98,  226, 18, 146, 82, 210, 50, 178, 114, 242,
    10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
    6,  134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
    14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
    1,  129, 65, 193, 33, 161, 97,  225, 17, 145, 81, 209, 49, 177, 113, 241,
    9,  137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
    5,  133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
    13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
    3,  131, 67, 195, 35, 163, 99,  227, 19, 147, 83, 211, 51, 179, 115, 243,
    11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
    7,  135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
    15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255};

/* Unpack to "1" image */

static void
unpack1(UINT8 *out, const UINT8 *in, int pixels) {
    /* bits (msb first, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 7:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 6:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 5:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 4:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 3:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 2:
                *out++ = (byte & 128) ? 255 : 0;
                byte <<= 1;
            case 1:
                *out++ = (byte & 128) ? 255 : 0;
        }
        pixels -= 8;
    }
}

static void
unpack1I(UINT8 *out, const UINT8 *in, int pixels) {
    /* bits (msb first, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 7:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 6:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 5:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 4:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 3:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 2:
                *out++ = (byte & 128) ? 0 : 255;
                byte <<= 1;
            case 1:
                *out++ = (byte & 128) ? 0 : 255;
        }
        pixels -= 8;
    }
}

static void
unpack1R(UINT8 *out, const UINT8 *in, int pixels) {
    /* bits (lsb first, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 7:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 6:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 5:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 4:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 3:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 2:
                *out++ = (byte & 1) ? 255 : 0;
                byte >>= 1;
            case 1:
                *out++ = (byte & 1) ? 255 : 0;
        }
        pixels -= 8;
    }
}

static void
unpack1IR(UINT8 *out, const UINT8 *in, int pixels) {
    /* bits (lsb first, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 7:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 6:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 5:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 4:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 3:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 2:
                *out++ = (byte & 1) ? 0 : 255;
                byte >>= 1;
            case 1:
                *out++ = (byte & 1) ? 0 : 255;
        }
        pixels -= 8;
    }
}

static void
unpack18(UINT8 *out, const UINT8 *in, int pixels) {
    /* Unpack a '|b1' image, which is a numpy boolean.
       1 == true, 0==false, in bytes */

    int i;
    for (i = 0; i < pixels; i++) {
        out[i] = in[i] > 0 ? 255 : 0;
    }
}

/* Unpack to "L" image */

static void
unpackL2(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (msb first, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 3:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 2:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 1:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
        }
        pixels -= 4;
    }
}

static void
unpackL2I(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (msb first, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 3:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 2:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 1:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
        }
        pixels -= 4;
    }
}

static void
unpackL2R(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (bit order reversed, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        byte = BITFLIP[byte];
        switch (pixels) {
            default:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 3:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 2:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
                byte <<= 2;
            case 1:
                *out++ = ((byte >> 6) & 0x03U) * 0x55U;
        }
        pixels -= 4;
    }
}

static void
unpackL2IR(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (bit order reversed, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        byte = BITFLIP[byte];
        switch (pixels) {
            default:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 3:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 2:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
                byte <<= 2;
            case 1:
                *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
        }
        pixels -= 4;
    }
}

static void
unpackL4(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (msb first, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
                byte <<= 4;
            case 1:
                *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
        }
        pixels -= 2;
    }
}

static void
unpackL4I(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (msb first, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
                byte <<= 4;
            case 1:
                *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
        }
        pixels -= 2;
    }
}

static void
unpackL4R(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (bit order reversed, white is non-zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        byte = BITFLIP[byte];
        switch (pixels) {
            default:
                *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
                byte <<= 4;
            case 1:
                *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
        }
        pixels -= 2;
    }
}

static void
unpackL4IR(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles (bit order reversed, white is zero) */
    while (pixels > 0) {
        UINT8 byte = *in++;
        byte = BITFLIP[byte];
        switch (pixels) {
            default:
                *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
                byte <<= 4;
            case 1:
                *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
        }
        pixels -= 2;
    }
}

static void
unpackLA(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* LA, pixel interleaved */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[1]);
        memcpy(_out, &iv, sizeof(iv));
        in += 2;
        _out += 4;
    }
}

static void
unpackLAL(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* LA, line interleaved */
    for (i = 0; i < pixels; i++, _out += 4) {
        UINT32 iv = MAKE_UINT32(in[i], in[i], in[i], in[i + pixels]);
        memcpy(_out, &iv, sizeof(iv));
    }
}

static void
unpackLI(UINT8 *out, const UINT8 *in, int pixels) {
    /* negative */
    int i;
    for (i = 0; i < pixels; i++) {
        out[i] = ~in[i];
    }
}

static void
unpackLR(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, bit reversed */
    for (i = 0; i < pixels; i++) {
        out[i] = BITFLIP[in[i]];
    }
}

static void
unpackL16(UINT8 *out, const UINT8 *in, int pixels) {
    /* int16 (upper byte, little endian) */
    int i;
    for (i = 0; i < pixels; i++) {
        out[i] = in[1];
        in += 2;
    }
}

static void
unpackL16B(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* int16 (upper byte, big endian) */
    for (i = 0; i < pixels; i++) {
        out[i] = in[0];
        in += 2;
    }
}

/* Unpack to "P" image */

static void
unpackP1(UINT8 *out, const UINT8 *in, int pixels) {
    /* bits */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 7:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 6:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 5:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 4:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 3:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 2:
                *out++ = (byte >> 7) & 1;
                byte <<= 1;
            case 1:
                *out++ = (byte >> 7) & 1;
        }
        pixels -= 8;
    }
}

static void
unpackP2(UINT8 *out, const UINT8 *in, int pixels) {
    /* bit pairs */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte >> 6) & 3;
                byte <<= 2;
            case 3:
                *out++ = (byte >> 6) & 3;
                byte <<= 2;
            case 2:
                *out++ = (byte >> 6) & 3;
                byte <<= 2;
            case 1:
                *out++ = (byte >> 6) & 3;
        }
        pixels -= 4;
    }
}

static void
unpackP4(UINT8 *out, const UINT8 *in, int pixels) {
    /* nibbles */
    while (pixels > 0) {
        UINT8 byte = *in++;
        switch (pixels) {
            default:
                *out++ = (byte >> 4) & 15;
                byte <<= 4;
            case 1:
                *out++ = (byte >> 4) & 15;
        }
        pixels -= 2;
    }
}

static void
unpackP2L(UINT8 *out, const UINT8 *in, int pixels) {
    int i, j, m, s;
    /* bit layers */
    m = 128;
    s = (pixels + 7) / 8;
    for (i = j = 0; i < pixels; i++) {
        out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0);
        if ((m >>= 1) == 0) {
            m = 128;
            j++;
        }
    }
}

static void
unpackP4L(UINT8 *out, const UINT8 *in, int pixels) {
    int i, j, m, s;
    /* bit layers (trust the optimizer ;-) */
    m = 128;
    s = (pixels + 7) / 8;
    for (i = j = 0; i < pixels; i++) {
        out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0) +
                 ((in[j + 2 * s] & m) ? 4 : 0) + ((in[j + 3 * s] & m) ? 8 : 0);
        if ((m >>= 1) == 0) {
            m = 128;
            j++;
        }
    }
}

/* Unpack to "RGB" image */

void
ImagingUnpackRGB(UINT8 *_out, const UINT8 *in, int pixels) {
    int i = 0;
    /* RGB triplets */
    for (; i < pixels - 1; i++) {
        UINT32 iv;
        memcpy(&iv, in, sizeof(iv));
        iv |= MASK_UINT32_CHANNEL_3;
        memcpy(_out, &iv, sizeof(iv));
        in += 3;
        _out += 4;
    }
    for (; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[0], in[1], in[2], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 3;
        _out += 4;
    }
}

void
unpackRGB16L(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGB triplets, little-endian order */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[1], in[3], in[5], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 6;
        _out += 4;
    }
}

void
unpackRGB16B(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGB triplets, big-endian order */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[0], in[2], in[4], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 6;
        _out += 4;
    }
}

static void
unpackRGBL(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, line interleaved */
    for (i = 0; i < pixels; i++, _out += 4) {
        UINT32 iv = MAKE_UINT32(in[i], in[i + pixels], in[i + pixels + pixels], 255);
        memcpy(_out, &iv, sizeof(iv));
    }
}

static void
unpackRGBR(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, bit reversed */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(BITFLIP[in[0]], BITFLIP[in[1]], BITFLIP[in[2]], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 3;
        _out += 4;
    }
}

void
ImagingUnpackBGR(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, reversed bytes */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 3;
        _out += 4;
    }
}

void
ImagingUnpackRGB15(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, 5 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[R] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 31) * 255 / 31;
        out[B] = ((pixel >> 10) & 31) * 255 / 31;
        out[A] = 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackRGBA15(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, 5/5/5/1 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[R] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 31) * 255 / 31;
        out[B] = ((pixel >> 10) & 31) * 255 / 31;
        out[A] = (pixel >> 15) * 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackBGR15(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, reversed bytes, 5 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[B] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 31) * 255 / 31;
        out[R] = ((pixel >> 10) & 31) * 255 / 31;
        out[A] = 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackBGRA15(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, rearranged channels, 5/5/5/1 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[B] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 31) * 255 / 31;
        out[R] = ((pixel >> 10) & 31) * 255 / 31;
        out[A] = (pixel >> 15) * 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackRGB16(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, 5/6/5 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[R] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 63) * 255 / 63;
        out[B] = ((pixel >> 11) & 31) * 255 / 31;
        out[A] = 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackBGR16(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, reversed bytes, 5/6/5 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[B] = (pixel & 31) * 255 / 31;
        out[G] = ((pixel >> 5) & 63) * 255 / 63;
        out[R] = ((pixel >> 11) & 31) * 255 / 31;
        out[A] = 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackRGB4B(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGB, 4 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[R] = (pixel & 15) * 17;
        out[G] = ((pixel >> 4) & 15) * 17;
        out[B] = ((pixel >> 8) & 15) * 17;
        out[A] = 255;
        out += 4;
        in += 2;
    }
}

void
ImagingUnpackRGBA4B(UINT8 *out, const UINT8 *in, int pixels) {
    int i, pixel;
    /* RGBA, 4 bits per pixel */
    for (i = 0; i < pixels; i++) {
        pixel = in[0] + (in[1] << 8);
        out[R] = (pixel & 15) * 17;
        out[G] = ((pixel >> 4) & 15) * 17;
        out[B] = ((pixel >> 8) & 15) * 17;
        out[A] = ((pixel >> 12) & 15) * 17;
        out += 4;
        in += 2;
    }
}

static void
ImagingUnpackBGRX(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, reversed bytes with padding */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
ImagingUnpackXRGB(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, leading pad */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[1], in[2], in[3], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
ImagingUnpackXBGR(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGB, reversed bytes, leading pad */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[3], in[2], in[1], 255);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

/* Unpack to "RGBA" image */

static void
unpackRGBALA(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* greyscale with alpha */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[1]);
        memcpy(_out, &iv, sizeof(iv));
        in += 2;
        _out += 4;
    }
}

static void
unpackRGBALA16B(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit greyscale with alpha, big-endian */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[2]);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackRGBa16L(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* premultiplied 16-bit RGBA, little-endian */
    for (i = 0; i < pixels; i++) {
        int a = in[7];
        UINT32 iv;
        if (!a) {
            iv = 0;
        } else if (a == 255) {
            iv = MAKE_UINT32(in[1], in[3], in[5], a);
        } else {
            iv = MAKE_UINT32(
                CLIP8(in[1] * 255 / a),
                CLIP8(in[3] * 255 / a),
                CLIP8(in[5] * 255 / a),
                a);
        }
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
        _out += 4;
    }
}

static void
unpackRGBa16B(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* premultiplied 16-bit RGBA, big-endian */
    for (i = 0; i < pixels; i++) {
        int a = in[6];
        UINT32 iv;
        if (!a) {
            iv = 0;
        } else if (a == 255) {
            iv = MAKE_UINT32(in[0], in[2], in[4], a);
        } else {
            iv = MAKE_UINT32(
                CLIP8(in[0] * 255 / a),
                CLIP8(in[2] * 255 / a),
                CLIP8(in[4] * 255 / a),
                a);
        }
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
        _out += 4;
    }
}

static void
unpackRGBa(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* premultiplied RGBA */
    for (i = 0; i < pixels; i++) {
        int a = in[3];
        UINT32 iv;
        if (!a) {
            iv = 0;
        } else if (a == 255) {
            iv = MAKE_UINT32(in[0], in[1], in[2], a);
        } else {
            iv = MAKE_UINT32(
                CLIP8(in[0] * 255 / a),
                CLIP8(in[1] * 255 / a),
                CLIP8(in[2] * 255 / a),
                a);
        }
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackRGBaskip1(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    UINT32 *out = (UINT32 *)_out;
    /* premultiplied RGBA */
    for (i = 0; i < pixels; i++) {
        int a = in[3];
        if (!a) {
            out[i] = 0;
        } else if (a == 255) {
            out[i] = MAKE_UINT32(in[0], in[1], in[2], a);
        } else {
            out[i] = MAKE_UINT32(
                CLIP8(in[0] * 255 / a),
                CLIP8(in[1] * 255 / a),
                CLIP8(in[2] * 255 / a),
                a);
        }
        in += 5;
    }
}

static void
unpackRGBaskip2(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    UINT32 *out = (UINT32 *)_out;
    /* premultiplied RGBA */
    for (i = 0; i < pixels; i++) {
        int a = in[3];
        if (!a) {
            out[i] = 0;
        } else if (a == 255) {
            out[i] = MAKE_UINT32(in[0], in[1], in[2], a);
        } else {
            out[i] = MAKE_UINT32(
                CLIP8(in[0] * 255 / a),
                CLIP8(in[1] * 255 / a),
                CLIP8(in[2] * 255 / a),
                a);
        }
        in += 6;
    }
}

static void
unpackBGRa(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* premultiplied BGRA */
    for (i = 0; i < pixels; i++) {
        int a = in[3];
        UINT32 iv;
        if (!a) {
            iv = 0;
        } else if (a == 255) {
            iv = MAKE_UINT32(in[2], in[1], in[0], a);
        } else {
            iv = MAKE_UINT32(
                CLIP8(in[2] * 255 / a),
                CLIP8(in[1] * 255 / a),
                CLIP8(in[0] * 255 / a),
                a);
        }
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackRGBAI(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* RGBA, inverted RGB bytes (FlashPix) */
    for (i = 0; i < pixels; i++) {
        out[R] = ~in[0];
        out[G] = ~in[1];
        out[B] = ~in[2];
        out[A] = in[3];
        out += 4;
        in += 4;
    }
}

static void
unpackRGBAL(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGBA, line interleaved */
    for (i = 0; i < pixels; i++, _out += 4) {
        UINT32 iv = MAKE_UINT32(
            in[i],
            in[i + pixels],
            in[i + pixels + pixels],
            in[i + pixels + pixels + pixels]);
        memcpy(_out, &iv, sizeof(iv));
    }
}

void
unpackRGBA16L(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGBA, little-endian order */
    for (i = 0; i < pixels; i++, _out += 4) {
        UINT32 iv = MAKE_UINT32(in[1], in[3], in[5], in[7]);
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
    }
}

void
unpackRGBA16B(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGBA, big-endian order */
    for (i = 0; i < pixels; i++, _out += 4) {
        UINT32 iv = MAKE_UINT32(in[0], in[2], in[4], in[6]);
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
    }
}

static void
unpackARGB(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGBA, leading pad */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[1], in[2], in[3], in[0]);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackABGR(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGBA, reversed bytes */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[3], in[2], in[1], in[0]);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackBGRA(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* RGBA, rearranged channels */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], in[3]);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

static void
unpackBGRA16L(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGBA, little-endian order, rearranged channels */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[5], in[3], in[1], in[7]);
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
        _out += 4;
    }
}

static void
unpackBGRA16B(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* 16-bit RGBA, big-endian order, rearranged channels */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = MAKE_UINT32(in[4], in[2], in[0], in[6]);
        memcpy(_out, &iv, sizeof(iv));
        in += 8;
        _out += 4;
    }
}

/* Unpack to "CMYK" image */

static void
unpackCMYKI(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    /* CMYK, inverted bytes (Photoshop 2.5) */
    for (i = 0; i < pixels; i++) {
        UINT32 iv = ~MAKE_UINT32(in[0], in[1], in[2], in[3]);
        memcpy(_out, &iv, sizeof(iv));
        in += 4;
        _out += 4;
    }
}

/* Unpack to "LAB" image */
/* There are two representations of LAB images for whatever precision:
   L: Uint (in PS, it's 0-100)
   A: Int (in ps, -128 .. 128, or elsewhere 0..255, with 128 as middle.
           Channels in PS display a 0 value as middle grey,
           LCMS appears to use 128 as the 0 value for these channels)
   B: Int (as above)

   Since we don't have any signed ints, we're going with the shifted versions
   internally, and we'll unshift for saving and whatnot.
*/
void
ImagingUnpackLAB(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* LAB triplets */
    for (i = 0; i < pixels; i++) {
        out[0] = in[0];
        out[1] = in[1] ^ 128; /* signed in outside world */
        out[2] = in[2] ^ 128;
        out[3] = 255;
        out += 4;
        in += 3;
    }
}

static void
unpackI16N_I16B(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    UINT8 *tmp = (UINT8 *)out;
    for (i = 0; i < pixels; i++) {
        C16B;
        in += 2;
        tmp += 2;
    }
}
static void
unpackI16N_I16(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    UINT8 *tmp = (UINT8 *)out;
    for (i = 0; i < pixels; i++) {
        C16L;
        in += 2;
        tmp += 2;
    }
}
static void
unpackI16B_I16(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    for (i = 0; i < pixels; i++) {
        out[0] = in[1];
        out[1] = in[0];
        in += 2;
        out += 2;
    }
}
static void
unpackI16R_I16(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    for (i = 0; i < pixels; i++) {
        out[0] = BITFLIP[in[0]];
        out[1] = BITFLIP[in[1]];
        in += 2;
        out += 2;
    }
}

static void
unpackI12_I16(UINT8 *out, const UINT8 *in, int pixels) {
    /*  Fillorder 1/MSB -> LittleEndian, for 12bit integer greyscale tiffs.

        According to the TIFF spec:

        FillOrder = 2 should be used only when BitsPerSample = 1 and
        the data is either uncompressed or compressed using CCITT 1D
        or 2D compression, to avoid potentially ambiguous situations.

        Yeah. I thought so. We'll see how well people read the spec.
        We've got several fillorder=2 modes in TiffImagePlugin.py

        There's no spec I can find. It appears that the in storage
        layout is: 00 80 00 ... -> (128 , 0 ...). The samples are
        stored in a single big bitian 12bit block, but need to be
        pulled out to little endian format to be stored in a 2 byte
        int.
     */

    int i;
    UINT16 pixel;
#ifdef WORDS_BIGENDIAN
    UINT8 *tmp = (UINT8 *)&pixel;
#endif
    for (i = 0; i < pixels - 1; i += 2) {
        pixel = (((UINT16)in[0]) << 4) + (in[1] >> 4);
#ifdef WORDS_BIGENDIAN
        out[0] = tmp[1];
        out[1] = tmp[0];
#else
        memcpy(out, &pixel, sizeof(pixel));
#endif

        out += 2;
        pixel = (((UINT16)(in[1] & 0x0F)) << 8) + in[2];
#ifdef WORDS_BIGENDIAN
        out[0] = tmp[1];
        out[1] = tmp[0];
#else
        memcpy(out, &pixel, sizeof(pixel));
#endif

        in += 3;
        out += 2;
    }
    if (i == pixels - 1) {
        pixel = (((UINT16)in[0]) << 4) + (in[1] >> 4);
#ifdef WORDS_BIGENDIAN
        out[0] = tmp[1];
        out[1] = tmp[0];
#else
        memcpy(out, &pixel, sizeof(pixel));
#endif
    }
}

static void
copy1(UINT8 *out, const UINT8 *in, int pixels) {
    /* L, P */
    memcpy(out, in, pixels);
}

static void
copy2(UINT8 *out, const UINT8 *in, int pixels) {
    /* I;16 */
    memcpy(out, in, pixels * 2);
}

static void
copy3(UINT8 *out, const UINT8 *in, int pixels) {
    /* BGR;24 */
    memcpy(out, in, pixels * 3);
}

static void
copy4(UINT8 *out, const UINT8 *in, int pixels) {
    /* RGBA, CMYK quadruples */
    memcpy(out, in, 4 * pixels);
}

static void
copy4skip1(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    for (i = 0; i < pixels; i++) {
        memcpy(_out, in, 4);
        in += 5;
        _out += 4;
    }
}

static void
copy4skip2(UINT8 *_out, const UINT8 *in, int pixels) {
    int i;
    for (i = 0; i < pixels; i++) {
        memcpy(_out, in, 4);
        in += 6;
        _out += 4;
    }
}

/* Unpack to "I" and "F" images */

#define UNPACK_RAW(NAME, GET, INTYPE, OUTTYPE)                   \
    static void NAME(UINT8 *out_, const UINT8 *in, int pixels) { \
        int i;                                                   \
        OUTTYPE *out = (OUTTYPE *)out_;                          \
        for (i = 0; i < pixels; i++, in += sizeof(INTYPE)) {     \
            out[i] = (OUTTYPE)((INTYPE)GET);                     \
        }                                                        \
    }

#define UNPACK(NAME, COPY, INTYPE, OUTTYPE)                      \
    static void NAME(UINT8 *out_, const UINT8 *in, int pixels) { \
        int i;                                                   \
        OUTTYPE *out = (OUTTYPE *)out_;                          \
        INTYPE tmp_;                                             \
        UINT8 *tmp = (UINT8 *)&tmp_;                             \
        for (i = 0; i < pixels; i++, in += sizeof(INTYPE)) {     \
            COPY;                                                \
            out[i] = (OUTTYPE)tmp_;                              \
        }                                                        \
    }

UNPACK_RAW(unpackI8, in[0], UINT8, INT32)
UNPACK_RAW(unpackI8S, in[0], INT8, INT32)
UNPACK(unpackI16, C16L, UINT16, INT32)
UNPACK(unpackI16S, C16L, INT16, INT32)
UNPACK(unpackI16B, C16B, UINT16, INT32)
UNPACK(unpackI16BS, C16B, INT16, INT32)
UNPACK(unpackI16N, C16N, UINT16, INT32)
UNPACK(unpackI16NS, C16N, INT16, INT32)
UNPACK(unpackI32, C32L, UINT32, INT32)
UNPACK(unpackI32S, C32L, INT32, INT32)
UNPACK(unpackI32B, C32B, UINT32, INT32)
UNPACK(unpackI32BS, C32B, INT32, INT32)
UNPACK(unpackI32N, C32N, UINT32, INT32)
UNPACK(unpackI32NS, C32N, INT32, INT32)

UNPACK_RAW(unpackF8, in[0], UINT8, FLOAT32)
UNPACK_RAW(unpackF8S, in[0], INT8, FLOAT32)
UNPACK(unpackF16, C16L, UINT16, FLOAT32)
UNPACK(unpackF16S, C16L, INT16, FLOAT32)
UNPACK(unpackF16B, C16B, UINT16, FLOAT32)
UNPACK(unpackF16BS, C16B, INT16, FLOAT32)
UNPACK(unpackF16N, C16N, UINT16, FLOAT32)
UNPACK(unpackF16NS, C16N, INT16, FLOAT32)
UNPACK(unpackF32, C32L, UINT32, FLOAT32)
UNPACK(unpackF32S, C32L, INT32, FLOAT32)
UNPACK(unpackF32B, C32B, UINT32, FLOAT32)
UNPACK(unpackF32BS, C32B, INT32, FLOAT32)
UNPACK(unpackF32N, C32N, UINT32, FLOAT32)
UNPACK(unpackF32NS, C32N, INT32, FLOAT32)
UNPACK(unpackF32F, C32L, FLOAT32, FLOAT32)
UNPACK(unpackF32BF, C32B, FLOAT32, FLOAT32)
UNPACK(unpackF32NF, C32N, FLOAT32, FLOAT32)
#ifdef FLOAT64
UNPACK(unpackF64F, C64L, FLOAT64, FLOAT32)
UNPACK(unpackF64BF, C64B, FLOAT64, FLOAT32)
UNPACK(unpackF64NF, C64N, FLOAT64, FLOAT32)
#endif

/* Misc. unpackers */

static void
band0(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 0 only */
    for (i = 0; i < pixels; i++) {
        out[0] = in[i];
        out += 4;
    }
}

static void
band1(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 1 only */
    for (i = 0; i < pixels; i++) {
        out[1] = in[i];
        out += 4;
    }
}

static void
band2(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 2 only */
    for (i = 0; i < pixels; i++) {
        out[2] = in[i];
        out += 4;
    }
}

static void
band3(UINT8 *out, const UINT8 *in, int pixels) {
    /* band 3 only */
    int i;
    for (i = 0; i < pixels; i++) {
        out[3] = in[i];
        out += 4;
    }
}

static void
band0I(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 0 only */
    for (i = 0; i < pixels; i++) {
        out[0] = ~in[i];
        out += 4;
    }
}

static void
band1I(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 1 only */
    for (i = 0; i < pixels; i++) {
        out[1] = ~in[i];
        out += 4;
    }
}

static void
band2I(UINT8 *out, const UINT8 *in, int pixels) {
    int i;
    /* band 2 only */
    for (i = 0; i < pixels; i++) {
        out[2] = ~in[i];
        out += 4;
    }
}

static void
band3I(UINT8 *out, const UINT8 *in, int pixels) {
    /* band 3 only */
    int i;
    for (i = 0; i < pixels; i++) {
        out[3] = ~in[i];
        out += 4;
    }
}

static void
band016B(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 0 only, big endian */
    for (i = 0; i < pixels; i++) {
        out[0] = in[0];
        out += 4; in += 2;
    }
}

static void
band116B(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 1 only, big endian */
    for (i = 0; i < pixels; i++) {
        out[1] = in[0];
        out += 4; in += 2;
    }
}

static void
band216B(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 2 only, big endian */
    for (i = 0; i < pixels; i++) {
        out[2] = in[0];
        out += 4; in += 2;
    }
}

static void
band316B(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 3 only, big endian */
    for (i = 0; i < pixels; i++) {
        out[3] = in[0];
        out += 4; in += 2;
    }
}

static void
band016L(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 0 only, little endian */
    for (i = 0; i < pixels; i++) {
        out[0] = in[1];
        out += 4; in += 2;
    }
}

static void
band116L(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 1 only, little endian */
    for (i = 0; i < pixels; i++) {
        out[1] = in[1];
        out += 4; in += 2;
    }
}

static void
band216L(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 2 only, little endian */
    for (i = 0; i < pixels; i++) {
        out[2] = in[1];
        out += 4; in += 2;
    }
}

static void
band316L(UINT8* out, const UINT8* in, int pixels)
{
    int i;
    /* band 3 only, little endian */
    for (i = 0; i < pixels; i++) {
        out[3] = in[1];
        out += 4; in += 2;
    }
}

static struct {
    const char *mode;
    const char *rawmode;
    int bits;
    ImagingShuffler unpack;
} unpackers[] = {

    /* raw mode syntax is "<mode>;<bits><flags>" where "bits" defaults
       depending on mode (1 for "1", 8 for "P" and "L", etc), and
       "flags" should be given in alphabetical order.  if both bits
       and flags have their default values, the ; should be left out */

    /* flags: "I" inverted data; "R" reversed bit order; "B" big
       endian byte order (default is little endian); "L" line
       interleave, "S" signed, "F" floating point */

    /* exception: rawmodes "I" and "F" are always native endian byte order */

    /* bilevel */
    {"1", "1", 1, unpack1},
    {"1", "1;I", 1, unpack1I},
    {"1", "1;R", 1, unpack1R},
    {"1", "1;IR", 1, unpack1IR},
    {"1", "1;8", 8, unpack18},

    /* greyscale */
    {"L", "L;2", 2, unpackL2},
    {"L", "L;2I", 2, unpackL2I},
    {"L", "L;2R", 2, unpackL2R},
    {"L", "L;2IR", 2, unpackL2IR},

    {"L", "L;4", 4, unpackL4},
    {"L", "L;4I", 4, unpackL4I},
    {"L", "L;4R", 4, unpackL4R},
    {"L", "L;4IR", 4, unpackL4IR},

    {"L", "L", 8, copy1},
    {"L", "L;I", 8, unpackLI},
    {"L", "L;R", 8, unpackLR},
    {"L", "L;16", 16, unpackL16},
    {"L", "L;16B", 16, unpackL16B},

    /* greyscale w. alpha */
    {"LA", "LA", 16, unpackLA},
    {"LA", "LA;L", 16, unpackLAL},

    /* greyscale w. alpha premultiplied */
    {"La", "La", 16, unpackLA},

    /* palette */
    {"P", "P;1", 1, unpackP1},
    {"P", "P;2", 2, unpackP2},
    {"P", "P;2L", 2, unpackP2L},
    {"P", "P;4", 4, unpackP4},
    {"P", "P;4L", 4, unpackP4L},
    {"P", "P", 8, copy1},
    {"P", "P;R", 8, unpackLR},
    {"P", "L", 8, copy1},

    /* palette w. alpha */
    {"PA", "PA", 16, unpackLA},
    {"PA", "PA;L", 16, unpackLAL},
    {"PA", "LA", 16, unpackLA},

    /* true colour */
    {"RGB", "RGB", 24, ImagingUnpackRGB},
    {"RGB", "RGB;L", 24, unpackRGBL},
    {"RGB", "RGB;R", 24, unpackRGBR},
    {"RGB", "RGB;16L", 48, unpackRGB16L},
    {"RGB", "RGB;16B", 48, unpackRGB16B},
    {"RGB", "BGR", 24, ImagingUnpackBGR},
    {"RGB", "RGB;15", 16, ImagingUnpackRGB15},
    {"RGB", "BGR;15", 16, ImagingUnpackBGR15},
    {"RGB", "RGB;16", 16, ImagingUnpackRGB16},
    {"RGB", "BGR;16", 16, ImagingUnpackBGR16},
    {"RGB", "RGB;4B", 16, ImagingUnpackRGB4B},
    {"RGB", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */
    {"RGB", "RGBX", 32, copy4},
    {"RGB", "RGBX;L", 32, unpackRGBAL},
    {"RGB", "RGBA;L", 32, unpackRGBAL},
    {"RGB", "RGBA;15", 16, ImagingUnpackRGBA15},
    {"RGB", "BGRX", 32, ImagingUnpackBGRX},
    {"RGB", "XRGB", 32, ImagingUnpackXRGB},
    {"RGB", "XBGR", 32, ImagingUnpackXBGR},
    {"RGB", "YCC;P", 24, ImagingUnpackYCC},
    {"RGB", "R", 8, band0},
    {"RGB", "G", 8, band1},
    {"RGB", "B", 8, band2},
    {"RGB", "R;16L", 16, band016L},
    {"RGB", "G;16L", 16, band116L},
    {"RGB", "B;16L", 16, band216L},
    {"RGB", "R;16B", 16, band016B},
    {"RGB", "G;16B", 16, band116B},
    {"RGB", "B;16B", 16, band216B},
    {"RGB", "CMYK", 32, cmyk2rgb},

    {"BGR;15", "BGR;15", 16, copy2},
    {"BGR;16", "BGR;16", 16, copy2},
    {"BGR;24", "BGR;24", 24, copy3},

    /* true colour w. alpha */
    {"RGBA", "LA", 16, unpackRGBALA},
    {"RGBA", "LA;16B", 32, unpackRGBALA16B},
    {"RGBA", "RGBA", 32, copy4},
    {"RGBA", "RGBAX", 40, copy4skip1},
    {"RGBA", "RGBAXX", 48, copy4skip2},
    {"RGBA", "RGBa", 32, unpackRGBa},
    {"RGBA", "RGBaX", 40, unpackRGBaskip1},
    {"RGBA", "RGBaXX", 48, unpackRGBaskip2},
    {"RGBA", "RGBa;16L", 64, unpackRGBa16L},
    {"RGBA", "RGBa;16B", 64, unpackRGBa16B},
    {"RGBA", "BGRa", 32, unpackBGRa},
    {"RGBA", "RGBA;I", 32, unpackRGBAI},
    {"RGBA", "RGBA;L", 32, unpackRGBAL},
    {"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15},
    {"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15},
    {"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B},
    {"RGBA", "RGBA;16L", 64, unpackRGBA16L},
    {"RGBA", "RGBA;16B", 64, unpackRGBA16B},
    {"RGBA", "BGRA", 32, unpackBGRA},
    {"RGBA", "BGRA;16L", 64, unpackBGRA16L},
    {"RGBA", "BGRA;16B", 64, unpackBGRA16B},
    {"RGBA", "ARGB", 32, unpackARGB},
    {"RGBA", "ABGR", 32, unpackABGR},
    {"RGBA", "YCCA;P", 32, ImagingUnpackYCCA},
    {"RGBA", "R", 8, band0},
    {"RGBA", "G", 8, band1},
    {"RGBA", "B", 8, band2},
    {"RGBA", "A", 8, band3},
    {"RGBA", "R;16L", 16, band016L},
    {"RGBA", "G;16L", 16, band116L},
    {"RGBA", "B;16L", 16, band216L},
    {"RGBA", "A;16L", 16, band316L},
    {"RGBA", "R;16B", 16, band016B},
    {"RGBA", "G;16B", 16, band116B},
    {"RGBA", "B;16B", 16, band216B},
    {"RGBA", "A;16B", 16, band316B},

#ifdef WORDS_BIGENDIAN
    {"RGB", "RGB;16N", 48, unpackRGB16B},
    {"RGBA", "RGBa;16N", 64, unpackRGBa16B},
    {"RGBA", "RGBA;16N", 64, unpackRGBA16B},
    {"RGBX", "RGBX;16N", 64, unpackRGBA16B},
    {"RGB", "R;16N", 16, band016B},
    {"RGB", "G;16N", 16, band116B},
    {"RGB", "B;16N", 16, band216B},

    {"RGBA", "R;16N", 16, band016B},
    {"RGBA", "G;16N", 16, band116B},
    {"RGBA", "B;16N", 16, band216B},
    {"RGBA", "A;16N", 16, band316B},
#else
    {"RGB", "RGB;16N", 48, unpackRGB16L},
    {"RGBA", "RGBa;16N", 64, unpackRGBa16L},
    {"RGBA", "RGBA;16N", 64, unpackRGBA16L},
    {"RGBX", "RGBX;16N", 64, unpackRGBA16L},
    {"RGB", "R;16N", 16, band016L},
    {"RGB", "G;16N", 16, band116L},
    {"RGB", "B;16N", 16, band216L},


    {"RGBA", "R;16N", 16, band016L},
    {"RGBA", "G;16N", 16, band116L},
    {"RGBA", "B;16N", 16, band216L},
    {"RGBA", "A;16N", 16, band316L},
#endif

    /* true colour w. alpha premultiplied */
    {"RGBa", "RGBa", 32, copy4},
    {"RGBa", "BGRa", 32, unpackBGRA},
    {"RGBa", "aRGB", 32, unpackARGB},
    {"RGBa", "aBGR", 32, unpackABGR},

    /* true colour w. padding */
    {"RGBX", "RGB", 24, ImagingUnpackRGB},
    {"RGBX", "RGB;L", 24, unpackRGBL},
    {"RGBX", "RGB;16B", 48, unpackRGB16B},
    {"RGBX", "BGR", 24, ImagingUnpackBGR},
    {"RGBX", "RGB;15", 16, ImagingUnpackRGB15},
    {"RGBX", "BGR;15", 16, ImagingUnpackBGR15},
    {"RGBX", "RGB;4B", 16, ImagingUnpackRGB4B},
    {"RGBX", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */
    {"RGBX", "RGBX", 32, copy4},
    {"RGBX", "RGBXX", 40, copy4skip1},
    {"RGBX", "RGBXXX", 48, copy4skip2},
    {"RGBX", "RGBX;L", 32, unpackRGBAL},
    {"RGBX", "RGBX;16L", 64, unpackRGBA16L},
    {"RGBX", "RGBX;16B", 64, unpackRGBA16B},
    {"RGBX", "BGRX", 32, ImagingUnpackBGRX},
    {"RGBX", "XRGB", 32, ImagingUnpackXRGB},
    {"RGBX", "XBGR", 32, ImagingUnpackXBGR},
    {"RGBX", "YCC;P", 24, ImagingUnpackYCC},
    {"RGBX", "R", 8, band0},
    {"RGBX", "G", 8, band1},
    {"RGBX", "B", 8, band2},
    {"RGBX", "X", 8, band3},

    /* colour separation */
    {"CMYK", "CMYK", 32, copy4},
    {"CMYK", "CMYKX", 40, copy4skip1},
    {"CMYK", "CMYKXX", 48, copy4skip2},
    {"CMYK", "CMYK;I", 32, unpackCMYKI},
    {"CMYK", "CMYK;L", 32, unpackRGBAL},
    {"CMYK", "CMYK;16L", 64, unpackRGBA16L},
    {"CMYK", "CMYK;16B", 64, unpackRGBA16B},
    {"CMYK", "C", 8, band0},
    {"CMYK", "M", 8, band1},
    {"CMYK", "Y", 8, band2},
    {"CMYK", "K", 8, band3},
    {"CMYK", "C;I", 8, band0I},
    {"CMYK", "M;I", 8, band1I},
    {"CMYK", "Y;I", 8, band2I},
    {"CMYK", "K;I", 8, band3I},

#ifdef WORDS_BIGENDIAN
    {"CMYK", "CMYK;16N", 64, unpackRGBA16B},
#else
    {"CMYK", "CMYK;16N", 64, unpackRGBA16L},
#endif

    /* video (YCbCr) */
    {"YCbCr", "YCbCr", 24, ImagingUnpackRGB},
    {"YCbCr", "YCbCr;L", 24, unpackRGBL},
    {"YCbCr", "YCbCrX", 32, copy4},
    {"YCbCr", "YCbCrK", 32, copy4},

    /* LAB Color */
    {"LAB", "LAB", 24, ImagingUnpackLAB},
    {"LAB", "L", 8, band0},
    {"LAB", "A", 8, band1},
    {"LAB", "B", 8, band2},

    /* HSV Color */
    {"HSV", "HSV", 24, ImagingUnpackRGB},
    {"HSV", "H", 8, band0},
    {"HSV", "S", 8, band1},
    {"HSV", "V", 8, band2},

    /* integer variations */
    {"I", "I", 32, copy4},
    {"I", "I;8", 8, unpackI8},
    {"I", "I;8S", 8, unpackI8S},
    {"I", "I;16", 16, unpackI16},
    {"I", "I;16S", 16, unpackI16S},
    {"I", "I;16B", 16, unpackI16B},
    {"I", "I;16BS", 16, unpackI16BS},
    {"I", "I;16N", 16, unpackI16N},
    {"I", "I;16NS", 16, unpackI16NS},
    {"I", "I;32", 32, unpackI32},
    {"I", "I;32S", 32, unpackI32S},
    {"I", "I;32B", 32, unpackI32B},
    {"I", "I;32BS", 32, unpackI32BS},
    {"I", "I;32N", 32, unpackI32N},
    {"I", "I;32NS", 32, unpackI32NS},

    /* floating point variations */
    {"F", "F", 32, copy4},
    {"F", "F;8", 8, unpackF8},
    {"F", "F;8S", 8, unpackF8S},
    {"F", "F;16", 16, unpackF16},
    {"F", "F;16S", 16, unpackF16S},
    {"F", "F;16B", 16, unpackF16B},
    {"F", "F;16BS", 16, unpackF16BS},
    {"F", "F;16N", 16, unpackF16N},
    {"F", "F;16NS", 16, unpackF16NS},
    {"F", "F;32", 32, unpackF32},
    {"F", "F;32S", 32, unpackF32S},
    {"F", "F;32B", 32, unpackF32B},
    {"F", "F;32BS", 32, unpackF32BS},
    {"F", "F;32N", 32, unpackF32N},
    {"F", "F;32NS", 32, unpackF32NS},
    {"F", "F;32F", 32, unpackF32F},
    {"F", "F;32BF", 32, unpackF32BF},
    {"F", "F;32NF", 32, unpackF32NF},
#ifdef FLOAT64
    {"F", "F;64F", 64, unpackF64F},
    {"F", "F;64BF", 64, unpackF64BF},
    {"F", "F;64NF", 64, unpackF64NF},
#endif

    /* storage modes */
    {"I;16", "I;16", 16, copy2},
    {"I;16B", "I;16B", 16, copy2},
    {"I;16L", "I;16L", 16, copy2},
    {"I;16N", "I;16N", 16, copy2},

    {"I;16", "I;16B", 16, unpackI16B_I16},
    {"I;16", "I;16N", 16, unpackI16N_I16},   // LibTiff native->image endian.
    {"I;16L", "I;16N", 16, unpackI16N_I16},  // LibTiff native->image endian.
    {"I;16B", "I;16N", 16, unpackI16N_I16B},

    {"I;16", "I;16R", 16, unpackI16R_I16},

    {"I;16", "I;12", 12, unpackI12_I16},  // 12 bit Tiffs stored in 16bits.

    {NULL} /* sentinel */
};

ImagingShuffler
ImagingFindUnpacker(const char *mode, const char *rawmode, int *bits_out) {
    int i;

    /* find a suitable pixel unpacker */
    for (i = 0; unpackers[i].rawmode; i++) {
        if (strcmp(unpackers[i].mode, mode) == 0 &&
            strcmp(unpackers[i].rawmode, rawmode) == 0) {
            if (bits_out) {
                *bits_out = unpackers[i].bits;
            }
            return unpackers[i].unpack;
        }
    }

    /* FIXME: configure a general unpacker based on the type codes... */

    return NULL;
}