File: __magick_read__.cc

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (2394 lines) | stat: -rw-r--r-- 87,141 bytes parent folder | download
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
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "file-stat.h"
#include "oct-env.h"
#include "oct-time.h"

#include "defun.h"
#include "error.h"
#include "ov-struct.h"

#include "errwarn.h"

#if defined (HAVE_MAGICK)

#include <Magick++.h>
#include <clocale>

// In theory, it should be enough to check the class:
// Magick::ClassType
// PseudoClass:
// Image is composed of pixels which specify an index in a color palette.
// DirectClass:
// Image is composed of pixels which represent literal color values.
//
//  GraphicsMagick does not really distinguishes between indexed and
//  normal images.  After reading a file, it decides itself the optimal
//  way to store the image in memory, independently of the how the
//  image was stored in the file.  That's what ClassType returns.  While
//  it seems to match the original file most of the times, this is
//  not necessarily true all the times.  See
//    https://sourceforge.net/mailarchive/message.php?msg_id=31180507
//  In addition to the ClassType, there is also ImageType which has a
//  type for indexed images (PaletteType and PaletteMatteType).  However,
//  they also don't represent the original image.  Not only does DirectClass
//  can have a PaletteType, but also does a PseudoClass have non Palette
//  types.
//
//        We can't do better without having format specific code which is
//        what we are trying to avoid by using a library such as GM.  We at
//        least create workarounds for the most common problems.
//
// 1) A grayscale jpeg image can report being indexed even though the
//    JPEG format has no support for indexed images.  We can at least
//    fix this one.
// 2) A PNG file is only an indexed image if color type orig is 3 (value comes
//    from libpng)
static bool
is_indexed (const Magick::Image& img)
{
  bool indexed = (img.classType () == Magick::PseudoClass);
  // Our problem until now is non-indexed images, being represented as indexed
  // by GM.  The following attempts educated guesses to undo this optimization.
  if (indexed)
    {
      const std::string fmt = img.magick ();
      if (fmt == "JPEG")
        // The JPEG format does not support indexed images, but GM sometimes
        // reports grayscale JPEG as indexed.  Always false for JPEG.
        indexed = false;
      else if (fmt == "PNG")
        {
          // Newer versions of GM (at least does not happens with 1.3.16) will
          // store values from the underlying library as image attributes.  In
          // the case of PNG files, this is libpng where an indexed image will
          // always have a value of 3 for "color-type-orig".  This property
          // always has a value in libpng so if we get nothing, we assume this
          // GM version does not store them and we have to go with whatever
          // GM PseudoClass says.
          const std::string color_type
            = const_cast<Magick::Image&> (img).attribute ("PNG:IHDR.color-type-orig");
          if (! color_type.empty () && color_type != "3")
            indexed = false;
        }
    }
  return indexed;
}

//  The depth from depth() is not always correct for us but seems to be the
//  best value we can get.  For example, a grayscale png image with 1 bit
//  per channel should return a depth of 1 but instead we get 8.
//  We could check channelDepth() but then, which channel has the data
//  is not straightforward.  So we'd have to check all
//  the channels and select the highest value.  But then, I also
//  have a 16bit TIFF whose depth returns 16 (correct), but all of the
//  channels gives 8 (wrong).  No idea why, maybe a bug in GM?
//  Anyway, using depth() seems that only causes problems for binary
//  images, and the problem with channelDepth() is not making set them
//  all to 1.  So we will guess that if all channels have depth of 1,
//  then we must have a binary image.
//  Note that we can't use AllChannels it doesn't work for this.
//  We also can't check only one from RGB, one from CMYK, and grayscale
// and transparency, we really need to check all of the channels (bug #41584).
static octave_idx_type
get_depth (Magick::Image& img)
{
  octave_idx_type depth = img.depth ();
  if (depth == 8
      && img.channelDepth (Magick::RedChannel)     == 1
      && img.channelDepth (Magick::GreenChannel)   == 1
      && img.channelDepth (Magick::BlueChannel)    == 1
      && img.channelDepth (Magick::CyanChannel)    == 1
      && img.channelDepth (Magick::MagentaChannel) == 1
      && img.channelDepth (Magick::YellowChannel)  == 1
      && img.channelDepth (Magick::BlackChannel)   == 1
      && img.channelDepth (Magick::OpacityChannel) == 1
      && img.channelDepth (Magick::GrayChannel)    == 1)
    depth = 1;

  return depth;
}

// We need this in case one of the sides of the image being read has
// width 1.  In those cases, the type will come as scalar instead of range
// since that's the behavior of the colon operator (1:1:1 will be a scalar,
// not a range).
static Range
get_region_range (const octave_value& region)
{
  Range output;

  if (region.is_range ())
    output = region.range_value ();
  else if (region.is_scalar_type ())
    {
      double value = region.scalar_value ();
      output = Range (value, value);
    }
  else if (region.is_matrix_type ())
    {
      NDArray array = region.array_value ();
      double base = array(0);
      double limit = array(array.numel () - 1);
      double incr = array(1) - base;
      output = Range (base, limit, incr);
    }
  else
    error ("__magick_read__: unknown datatype for Region option");

  return output;
}

class
image_region
{
public:

  image_region (const octave_scalar_map& options)
  {
    // FIXME: should we have better checking on the input map and values
    // or is that expected to be done elsewhere?

    const Cell pixel_region = options.getfield ("region").cell_value ();

    // Subtract 1 to account for 0 indexing.

    const Range rows = get_region_range (pixel_region (0));
    const Range cols = get_region_range (pixel_region (1));

    m_row_start = rows.base () - 1;
    m_col_start = cols.base () - 1;
    m_row_end = rows.max () - 1;
    m_col_end = cols.max () - 1;

    m_row_cache = m_row_end - m_row_start + 1;
    m_col_cache = m_col_end - m_col_start + 1;

    m_row_shift = m_col_cache * rows.inc ();
    m_col_shift = m_col_cache * (m_row_cache + rows.inc () - 1) - cols.inc ();

    m_row_out = rows.numel ();
    m_col_out = cols.numel ();
  }

  // Default copy, move, and delete methods are all OK for this class.

  image_region (const image_region&) = default;
  image_region (image_region&&) = default;

  image_region& operator = (const image_region&) = default;
  image_region& operator = (image_region&&) = default;

  ~image_region (void) = default;

  octave_idx_type row_start (void) const { return m_row_start; }
  octave_idx_type col_start (void) const { return m_col_start; }
  octave_idx_type row_end (void) const { return m_row_end; }
  octave_idx_type col_end (void) const { return m_col_end; }

  // Length of the area to load into the Image Pixel Cache.  We use max and
  // min to account for cases where last element of range is the range limit.

  octave_idx_type row_cache (void) const { return m_row_cache; }
  octave_idx_type col_cache (void) const { return m_col_cache; }

  // How much we have to shift in the memory when doing the loops.

  octave_idx_type row_shift (void) const { return m_row_shift; }
  octave_idx_type col_shift (void) const { return m_col_shift; }

  // The actual height and width of the output image

  octave_idx_type row_out (void) const { return m_row_out; }
  octave_idx_type col_out (void) const { return m_col_out; }

private:

  octave_idx_type m_row_start;
  octave_idx_type m_col_start;
  octave_idx_type m_row_end;
  octave_idx_type m_col_end;

  // Length of the area to load into the Image Pixel Cache.  We use max and
  // min to account for cases where last element of range is the range limit.

  octave_idx_type m_row_cache;
  octave_idx_type m_col_cache;

  // How much we have to shift in the memory when doing the loops.

  octave_idx_type m_row_shift;
  octave_idx_type m_col_shift;

  // The actual height and width of the output image

  octave_idx_type m_row_out;
  octave_idx_type m_col_out;
};

static octave_value_list
read_maps (Magick::Image& img)
{
  // can't call colorMapSize on const Magick::Image
  const octave_idx_type mapsize = img.colorMapSize ();
  Matrix cmap                   = Matrix (mapsize, 3); // colormap
  ColumnVector amap             = ColumnVector (mapsize); // alpha map
  for (octave_idx_type i = 0; i < mapsize; i++)
    {
      const Magick::ColorRGB c = img.colorMap (i);
      cmap(i,0) = c.red   ();
      cmap(i,1) = c.green ();
      cmap(i,2) = c.blue  ();
      amap(i)   = c.alpha ();
    }
  octave_value_list maps;
  maps(0) = cmap;
  maps(1) = amap;
  return maps;
}

template <typename T>
static octave_value_list
read_indexed_images (const std::vector<Magick::Image>& imvec,
                     const Array<octave_idx_type>& frameidx,
                     const octave_idx_type& nargout,
                     const octave_scalar_map& options)
{
  typedef typename T::element_type P;

  octave_value_list retval (1);

  image_region region (options);

  const octave_idx_type nFrames = frameidx.numel ();
  const octave_idx_type nRows = region.row_out ();
  const octave_idx_type nCols = region.col_out ();

  // imvec has all of the pages of a file, even the ones we are not
  // interested in.  We will use the first image that we will be actually
  // reading to get information about the image.
  const octave_idx_type def_elem = frameidx(0);

  T img       = T (dim_vector (nRows, nCols, 1, nFrames));
  P *img_fvec = img.fortran_vec ();

  const octave_idx_type row_start = region.row_start ();
  const octave_idx_type col_start = region.col_start ();
  const octave_idx_type row_shift = region.row_shift ();
  const octave_idx_type col_shift = region.col_shift ();
  const octave_idx_type row_cache = region.row_cache ();
  const octave_idx_type col_cache = region.col_cache ();

  // When reading PixelPackets from the Image Pixel Cache, they come in
  // row major order.  So we keep moving back and forth there so we can
  // write the image in column major order.
  octave_idx_type idx = 0;
  for (octave_idx_type frame = 0; frame < nFrames; frame++)
    {
      octave_quit ();

      imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                             col_cache, row_cache);

      const Magick::IndexPacket *pix
        = imvec[frameidx(frame)].getConstIndexes ();

      for (octave_idx_type col = 0; col < nCols; col++)
        {
          for (octave_idx_type row = 0; row < nRows; row++)
            {
              img_fvec[idx++] = static_cast<P> (*pix);
              pix += row_shift;
            }
          pix -= col_shift;
        }
    }
  retval(0) = octave_value (img);

  // Only bother reading the colormap if it was requested as output.
  if (nargout > 1)
    {
      // In theory, it should be possible for each frame of an image to
      // have different colormaps but for Matlab compatibility, we only
      // return the colormap of the first frame.  To obtain the colormaps
      // of different frames, one needs can either use imfinfo or a for
      // loop around imread.
      const octave_value_list maps
        = read_maps (const_cast<Magick::Image&> (imvec[frameidx(def_elem)]));

      retval(1) = maps(0);

      // only interpret alpha channel if it exists and was requested as output
      if (imvec[def_elem].matte () && nargout >= 3)
        {
          const Matrix amap = maps(1).matrix_value ();
          const double *amap_fvec = amap.fortran_vec ();

          NDArray alpha (dim_vector (nRows, nCols, 1, nFrames));
          double *alpha_fvec = alpha.fortran_vec ();

          // GraphicsMagick stores the alpha values inverted, i.e.,
          // 1 for transparent and 0 for opaque so we fix that here.
          const octave_idx_type nPixels = alpha.numel ();
          for (octave_idx_type pix = 0; pix < nPixels; pix++)
            alpha_fvec[pix] = 1 - amap_fvec[static_cast<int> (img_fvec[3])];

          retval(2) = alpha;
        }
    }

  return retval;
}

// This function is highly repetitive, a bunch of for loops that are
// very similar to account for different image types.  They are different
// enough that trying to reduce the copy and paste would decrease its
// readability too much.
template <typename T>
octave_value_list
read_images (std::vector<Magick::Image>& imvec,
             const Array<octave_idx_type>& frameidx,
             const octave_idx_type& nargout,
             const octave_scalar_map& options)
{
  typedef typename T::element_type P;

  octave_value_list retval (3, Matrix ());

  image_region region (options);

  const octave_idx_type nFrames = frameidx.numel ();
  const octave_idx_type nRows = region.row_out ();
  const octave_idx_type nCols = region.col_out ();
  T img;

  // imvec has all of the pages of a file, even the ones we are not
  // interested in.  We will use the first image that we will be actually
  // reading to get information about the image.
  const octave_idx_type def_elem = frameidx(0);

  const octave_idx_type row_start = region.row_start ();
  const octave_idx_type col_start = region.col_start ();
  const octave_idx_type row_shift = region.row_shift ();
  const octave_idx_type col_shift = region.col_shift ();
  const octave_idx_type row_cache = region.row_cache ();
  const octave_idx_type col_cache = region.col_cache ();

  // GraphicsMagick (GM) keeps the image values in memory using whatever
  // QuantumDepth it was built with independently of the original image
  // bitdepth.  Basically this means that if GM was built with quantum 16
  // all values are scaled in the uint16 range.  If the original image
  // had an 8 bit depth, we need to rescale it for that range.
  // However, if the image had a bitdepth of 32, then we will be returning
  // a floating point image.  In this case, the values need to be rescaled
  // for the range [0 1] (this is what Matlab has documented on the page
  // about image types but in some cases seems to be doing something else.
  // See bug #39249).
  // Finally, we must do the division ourselves (set a divisor) instead of
  // using quantumOperator for the cases where we will be returning floating
  // point and want things in the range [0 1].  This is the same reason why
  // the divisor is of type double.
  // uint64_t is used in expression because default 32-bit value overflows
  // when depth() is 32.
  // FIXME: in the next release of GraphicsMagick, MaxRGB should be replaced
  //        with QuantumRange since MaxRGB is already deprecated in ImageMagick.
  double divisor;
  if (imvec[def_elem].depth () == 32)
    divisor = std::numeric_limits<uint32_t>::max ();
  else
    divisor = MaxRGB / ((uint64_t (1) << imvec[def_elem].depth ()) - 1);

  // FIXME: this workaround should probably be fixed in GM by creating a
  //        new ImageType BilevelMatteType
  // Despite what GM documentation claims, opacity is not only on the types
  // with Matte on the name.  It is possible that an image is completely
  // black (1 color), and have a second channel set for transparency (2nd
  // color).  Its type will be bilevel since there is no BilevelMatte.  The
  // only way to check for this seems to be by checking matte ().
  Magick::ImageType type = imvec[def_elem].type ();
  if (type == Magick::BilevelType && imvec[def_elem].matte ())
    type = Magick::GrayscaleMatteType;

  // FIXME: ImageType is the type being used to represent the image in memory
  // by GM.  The real type may be different (see among others bug #36820).  For
  // example, a png file where all channels are equal may report being
  // grayscale or even bilevel.  But we must always return the real image in
  // file.  In some cases, the original image attributes are stored in the
  // attributes but this is undocumented.  This should be fixed in GM so that
  // a method such as original_type returns an actual Magick::ImageType
  if (imvec[0].magick () == "PNG")
    {
      // These values come from libpng, not GM:
      //      Grayscale         = 0
      //      Palette           = 2 + 1
      //      RGB               = 2
      //      RGB + Alpha       = 2 + 4
      //      Grayscale + Alpha = 4
      // We won't bother with case 3 (palette) since those should be
      // read by the function to read indexed images
      const std::string type_str
        = imvec[0].attribute ("PNG:IHDR.color-type-orig");

      if (type_str == "0")
        type = Magick::GrayscaleType;
      else if (type_str == "2")
        type = Magick::TrueColorType;
      else if (type_str == "6")
        type = Magick::TrueColorMatteType;
      else if (type_str == "4")
        type = Magick::GrayscaleMatteType;
      // Color types 0, 2, and 3 can also have alpha channel, conveyed
      // via the "tRNS" chunk.  For 0 and 2, it's limited to GIF-style
      // binary transparency, while 3 can have any level of alpha per
      // palette entry.  We thus must check matte() to see if the image
      // really doesn't have an alpha channel.
      if (imvec[0].matte ())
        {
          if (type == Magick::GrayscaleType)
            type = Magick::GrayscaleMatteType;
          else if (type == Magick::TrueColorType)
            type = Magick::TrueColorMatteType;
        }
    }

  // If the alpha channel was not requested, treat images as if
  // it doesn't exist.
  if (nargout < 3)
    {
      switch (type)
        {
        case Magick::GrayscaleMatteType:
          type = Magick::GrayscaleType;
          break;

        case Magick::PaletteMatteType:
          type = Magick::PaletteType;
          break;

        case Magick::TrueColorMatteType:
          type = Magick::TrueColorType;
          break;

        case Magick::ColorSeparationMatteType:
          type = Magick::ColorSeparationType;
          break;

        default:
          // Do nothing other than silencing warnings about enumeration
          // values not being handled in switch.
          ;
        }
    }

  const octave_idx_type color_stride = nRows * nCols;
  switch (type)
    {
    case Magick::BilevelType:           // Monochrome bi-level image
    case Magick::GrayscaleType:         // Grayscale image
      {
        img = T (dim_vector (nRows, nCols, 1, nFrames));
        P *img_fvec = img.fortran_vec ();

        octave_idx_type idx = 0;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    img_fvec[idx++] = pix->red / divisor;
                    pix += row_shift;
                  }
                pix -= col_shift;
              }
          }
        break;
      }

    case Magick::GrayscaleMatteType:    // Grayscale image with opacity
      {
        img = T (dim_vector (nRows, nCols, 1, nFrames));
        T alpha (dim_vector (nRows, nCols, 1, nFrames));
        P *img_fvec = img.fortran_vec ();
        P *a_fvec   = alpha.fortran_vec ();

        octave_idx_type idx = 0;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    img_fvec[idx] = pix->red / divisor;
                    a_fvec[idx]   = (MaxRGB - pix->opacity) / divisor;
                    pix += row_shift;
                    idx++;
                  }
                pix -= col_shift;
              }
          }
        retval(2) = alpha;
        break;
      }

    case Magick::PaletteType:           // Indexed color (palette) image
    case Magick::TrueColorType:         // Truecolor image
      {
        img = T (dim_vector (nRows, nCols, 3, nFrames));
        P *img_fvec = img.fortran_vec ();

        const octave_idx_type frame_stride = color_stride * 3;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);

            octave_idx_type idx = 0;
            P *rbuf = img_fvec;
            P *gbuf = img_fvec + color_stride;
            P *bbuf = img_fvec + color_stride * 2;

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    rbuf[idx] = pix->red   / divisor;
                    gbuf[idx] = pix->green / divisor;
                    bbuf[idx] = pix->blue  / divisor;
                    pix += row_shift;
                    idx++;
                  }
                pix -= col_shift;
              }
            img_fvec += frame_stride;
          }
        break;
      }

    case Magick::PaletteMatteType:    // Indexed color image with opacity
    case Magick::TrueColorMatteType:  // Truecolor image with opacity
      {
        img = T (dim_vector (nRows, nCols, 3, nFrames));
        T alpha (dim_vector (nRows, nCols, 1, nFrames));
        P *img_fvec = img.fortran_vec ();
        P *a_fvec   = alpha.fortran_vec ();

        const octave_idx_type frame_stride = color_stride * 3;

        // Unlike the index for the other channels, this one won't need
        // to be reset on each frame since it's a separate matrix.
        octave_idx_type a_idx = 0;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);

            octave_idx_type idx = 0;
            P *rbuf = img_fvec;
            P *gbuf = img_fvec + color_stride;
            P *bbuf = img_fvec + color_stride * 2;

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    rbuf[idx]     = pix->red     / divisor;
                    gbuf[idx]     = pix->green   / divisor;
                    bbuf[idx]     = pix->blue    / divisor;
                    a_fvec[a_idx++] = (MaxRGB - pix->opacity) / divisor;
                    pix += row_shift;
                    idx++;
                  }
                pix -= col_shift;
              }
            img_fvec += frame_stride;
          }
        retval(2) = alpha;
        break;
      }

    case Magick::ColorSeparationType:  // Cyan/Magenta/Yellow/Black (CMYK) image
      {
        img = T (dim_vector (nRows, nCols, 4, nFrames));
        P *img_fvec = img.fortran_vec ();

        const octave_idx_type frame_stride = color_stride * 4;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);

            octave_idx_type idx = 0;
            P *cbuf = img_fvec;
            P *mbuf = img_fvec + color_stride;
            P *ybuf = img_fvec + color_stride * 2;
            P *kbuf = img_fvec + color_stride * 3;

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    cbuf[idx] = pix->red     / divisor;
                    mbuf[idx] = pix->green   / divisor;
                    ybuf[idx] = pix->blue    / divisor;
                    kbuf[idx] = pix->opacity / divisor;
                    pix += row_shift;
                    idx++;
                  }
                pix -= col_shift;
              }
            img_fvec += frame_stride;
          }
        break;
      }

    // Cyan, magenta, yellow, and black with alpha (opacity) channel
    case Magick::ColorSeparationMatteType:
      {
        img = T (dim_vector (nRows, nCols, 4, nFrames));
        T alpha (dim_vector (nRows, nCols, 1, nFrames));
        P *img_fvec = img.fortran_vec ();
        P *a_fvec   = alpha.fortran_vec ();

        const octave_idx_type frame_stride = color_stride * 4;

        // Unlike the index for the other channels, this one won't need
        // to be reset on each frame since it's a separate matrix.
        octave_idx_type a_idx = 0;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            const Magick::PixelPacket *pix
              = imvec[frameidx(frame)].getConstPixels (col_start, row_start,
                                                       col_cache, row_cache);
            // Note that for CMYKColorspace + matte (CMYKA), the opacity is
            // stored in the associated IndexPacket.
            const Magick::IndexPacket *apix
              = imvec[frameidx(frame)].getConstIndexes ();

            octave_idx_type idx = 0;
            P *cbuf = img_fvec;
            P *mbuf = img_fvec + color_stride;
            P *ybuf = img_fvec + color_stride * 2;
            P *kbuf = img_fvec + color_stride * 3;

            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    cbuf[idx]     = pix->red     / divisor;
                    mbuf[idx]     = pix->green   / divisor;
                    ybuf[idx]     = pix->blue    / divisor;
                    kbuf[idx]     = pix->opacity / divisor;
                    a_fvec[a_idx++] = (MaxRGB - *apix) / divisor;
                    pix += row_shift;
                    idx++;
                  }
                pix -= col_shift;
              }
            img_fvec += frame_stride;
          }
        retval(2) = alpha;
        break;
      }

    default:
      error ("__magick_read__: unknown Magick++ image type");
    }

  retval(0) = img;

  return retval;
}

// Read a file into vector of image objects.
void static
read_file (const std::string& filename, std::vector<Magick::Image>& imvec)
{
  try
    {
      Magick::readImages (&imvec, filename);
    }
  catch (Magick::Warning& w)
    {
      warning ("Magick++ warning: %s", w.what ());
    }
  catch (Magick::Exception& e)
    {
      error ("Magick++ exception: %s", e.what ());
    }
}

static void
maybe_initialize_magick (void)
{
  static bool initialized = false;

  if (! initialized)
    {
      // Save locale as GraphicsMagick might change this (fixed in
      // GraphicsMagick since version 1.3.13 released on December 24, 2011)
      const char *static_locale = setlocale (LC_ALL, nullptr);
      const std::string locale = (static_locale ? static_locale : "");

      const std::string program_name
        = octave::sys::env::get_program_invocation_name ();
      Magick::InitializeMagick (program_name.c_str ());

      // Restore locale from before GraphicsMagick initialisation
      setlocale (LC_ALL, locale.c_str ());

      // Why should we give a warning?
      // Magick does not tell us the real bitdepth of the image in file.
      // The best we can have is the minimum between the bitdepth of the
      // file and the quantum depth.  So we never know if the file will
      // actually be read correctly so we warn the user that it might
      // be limited.
      //
      // Why we warn if < 16 instead of < 32 ?
      // The reasons for < 32 is simply that it's the maximum quantum
      // depth they support.  However, very few people would actually
      // need such support while being a major inconvenience to anyone
      // else (8 bit images suddenly taking 4x more space will be
      // critical for multi page images).  It would also suggests that
      // it covers all images which does not (it still does not support
      // float point and signed integer images).
      // On the other hand, 16bit images are much more common.  If quantum
      // depth is 8, there's a good chance that we will be limited.  It
      // is also the GraphicsMagick recommended setting and the default
      // for ImageMagick.
      if (QuantumDepth < 16)
        warning_with_id ("Octave:GraphicsMagick-Quantum-Depth",
                         "your version of %s limits images to %d bits per pixel\n",
                         MagickPackageName, QuantumDepth);

      initialized = true;
    }
}

#endif

DEFUN (__magick_read__, args, nargout,
       doc: /* -*- texinfo -*-
@deftypefn {} {[@var{img}, @var{map}, @var{alpha}] =} __magick_read__ (@var{fname}, @var{options})
Read image with GraphicsMagick or ImageMagick.

This is a private internal function not intended for direct use.
Use @code{imread} instead.

@seealso{imfinfo, imformats, imread, imwrite}
@end deftypefn */)
{
#if defined (HAVE_MAGICK)

  if (args.length () != 2 || ! args(0).is_string ())
    print_usage ();

  maybe_initialize_magick ();

  const octave_scalar_map options
    = args(1).xscalar_map_value ("__magick_read__: OPTIONS must be a struct");

  octave_value_list output;

  std::vector<Magick::Image> imvec;
  read_file (args(0).string_value (), imvec);

  // Prepare an Array with the indexes for the requested frames.
  const octave_idx_type nFrames = imvec.size ();
  Array<octave_idx_type> frameidx;
  const octave_value indexes = options.getfield ("index");
  if (indexes.is_string () && indexes.string_value () == "all")
    {
      frameidx.resize (dim_vector (1, nFrames));
      for (octave_idx_type i = 0; i < nFrames; i++)
        frameidx(i) = i;
    }
  else
    {
      frameidx = indexes.xint_vector_value ("__magick_read__: invalid value for Index/Frame");

      // Fix indexes from base 1 to base 0, and at the same time, make
      // sure none of the indexes is outside the range of image number.
      const octave_idx_type n = frameidx.numel ();
      for (octave_idx_type i = 0; i < n; i++)
        {
          frameidx(i)--;
          if (frameidx(i) < 0 || frameidx(i) > nFrames - 1)
            {
              // We do this check inside the loop because frameidx does not
              // need to be ordered (this is a feature and even allows for
              // some frames to be read multiple times).
              error ("imread: index/frames specified are outside the number of images");
            }
        }
    }

  // Check that all frames have the same size.  We don't do this at the same
  // time we decode the image because that's done in many different places,
  // to cover the different types of images which would lead to a lot of
  // copy and paste.
  {
    const unsigned int nRows = imvec[frameidx(0)].rows ();
    const unsigned int nCols = imvec[frameidx(0)].columns ();
    const octave_idx_type n = frameidx.numel ();
    for (octave_idx_type frame = 0; frame < n; frame++)
      {
        if (nRows != imvec[frameidx(frame)].rows ()
            || nCols != imvec[frameidx(frame)].columns ())
          {
            error ("imread: all frames must have the same size but frame "
                   "%" OCTAVE_IDX_TYPE_FORMAT " is different",
                   frameidx(frame) +1);
          }
      }
  }

  const octave_idx_type depth = get_depth (imvec[frameidx(0)]);
  if (is_indexed (imvec[frameidx(0)]))
    {
      if (depth <= 1)
        output = read_indexed_images<boolNDArray>   (imvec, frameidx,
                                                     nargout, options);
      else if (depth <= 8)
        output = read_indexed_images<uint8NDArray>  (imvec, frameidx,
                                                     nargout, options);
      else if (depth <= 16)
        output = read_indexed_images<uint16NDArray> (imvec, frameidx,
                                                     nargout, options);
      else
        error ("imread: indexed images with depths greater than 16-bit are not supported");
    }

  else
    {
      if (depth <= 1)
        output = read_images<boolNDArray>   (imvec, frameidx, nargout, options);
      else if (depth <= 8)
        output = read_images<uint8NDArray>  (imvec, frameidx, nargout, options);
      else if (depth <= 16)
        output = read_images<uint16NDArray> (imvec, frameidx, nargout, options);
      else if (depth <= 32)
        output = read_images<FloatNDArray>  (imvec, frameidx, nargout, options);
      else
        error ("imread: reading of images with %" OCTAVE_IDX_TYPE_FORMAT
               "-bit depth is not supported", depth);
    }

  return output;

#else

  octave_unused_parameter (args);
  octave_unused_parameter (nargout);

  err_disabled_feature ("imread", "Image IO");

#endif
}

/*
## No test needed for internal helper function.
%!assert (1)
*/

#if defined (HAVE_MAGICK)

template <typename T>
static uint32NDArray
img_float2uint (const T& img)
{
  typedef typename T::element_type P;
  uint32NDArray out (img.dims ());

  octave_uint32 *out_fvec = out.fortran_vec ();
  const P       *img_fvec = img.fortran_vec ();

  const octave_uint32 max = octave_uint32::max ();
  const octave_idx_type numel = img.numel ();
  for (octave_idx_type idx = 0; idx < numel; idx++)
    out_fvec[idx] = img_fvec[idx] * max;

  return out;
}

// Gets the bitdepth to be used for an Octave class, i.e, returns 8 for
// uint8, 16 for uint16, and 32 for uint32
template <typename T>
static octave_idx_type
bitdepth_from_class ()
{
  typedef typename T::element_type P;
  const octave_idx_type bitdepth
    = sizeof (P) * std::numeric_limits<unsigned char>::digits;
  return bitdepth;
}

static Magick::Image
init_enconde_image (const octave_idx_type& nCols, const octave_idx_type& nRows,
                    const octave_idx_type& bitdepth,
                    const Magick::ImageType& type,
                    const Magick::ClassType& klass)
{
  Magick::Image img (Magick::Geometry (nCols, nRows), "black");
  // Ensure that there are no other references to this image.
  img.modifyImage ();

  img.classType (klass);
  img.type (type);
  // FIXME: for some reason, setting bitdepth doesn't seem to work for
  //        indexed images.
  img.depth (bitdepth);
  switch (type)
    {
    case Magick::GrayscaleMatteType:
    case Magick::TrueColorMatteType:
    case Magick::ColorSeparationMatteType:
    case Magick::PaletteMatteType:
      img.matte (true);
      break;

    default:
      img.matte (false);
    }

  return img;
}

template <typename T>
static void
encode_indexed_images (std::vector<Magick::Image>& imvec,
                       const T& img,
                       const Matrix& cmap)
{
  typedef typename T::element_type P;
  const octave_idx_type nFrames   = (img.ndims () < 4 ? 1 : img.dims ()(3));
  const octave_idx_type nRows     = img.rows ();
  const octave_idx_type nCols     = img.columns ();
  const octave_idx_type cmap_size = cmap.rows ();
  const octave_idx_type bitdepth  = bitdepth_from_class<T> ();

  // There is no colormap object, we need to build a new one for each frame,
  // even if it's always the same.  We can least get a vector for the Colors.
  std::vector<Magick::ColorRGB> colormap;
  {
    const double *cmap_fvec = cmap.fortran_vec ();
    const octave_idx_type G_offset = cmap_size;
    const octave_idx_type B_offset = cmap_size * 2;
    for (octave_idx_type map_idx = 0; map_idx < cmap_size; map_idx++)
      colormap.push_back (Magick::ColorRGB (cmap_fvec[map_idx],
                                            cmap_fvec[map_idx + G_offset],
                                            cmap_fvec[map_idx + B_offset]));
  }

  for (octave_idx_type frame = 0; frame < nFrames; frame++)
    {
      octave_quit ();

      Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                Magick::PaletteType,
                                                Magick::PseudoClass);

      // Insert colormap.
      m_img.colorMapSize (cmap_size);
      for (octave_idx_type map_idx = 0; map_idx < cmap_size; map_idx++)
        m_img.colorMap (map_idx, colormap[map_idx]);

      // Why are we also setting the pixel values instead of only the
      // index values? We don't know if a file format supports indexed
      // images.  If we only set the indexes and then try to save the
      // image as JPEG for example, the indexed values get discarded,
      // there is no conversion from the indexes, it's the initial values
      // that get used.  An alternative would be to only set the pixel
      // values (no indexes), then set the image as PseudoClass and GM
      // would create a colormap for us.  However, we wouldn't have control
      // over the order of that colormap.  And that's why we set both.
      Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
      Magick::IndexPacket *ind = m_img.getIndexes ();
      const P *img_fvec        = img.fortran_vec ();

      octave_idx_type GM_idx = 0;
      for (octave_idx_type column = 0; column < nCols; column++)
        {
          for (octave_idx_type row = 0; row < nRows; row++)
            {
              ind[GM_idx] = double (*img_fvec);
              pix[GM_idx] = m_img.colorMap (double (*img_fvec));
              img_fvec++;
              GM_idx += nCols;
            }
          GM_idx -= nCols * nRows - 1;
        }

      // Save changes to underlying image.
      m_img.syncPixels ();
      imvec.push_back (m_img);
    }
}

static void
encode_bool_image (std::vector<Magick::Image>& imvec, const boolNDArray& img)
{
  const octave_idx_type nFrames = (img.ndims () < 4 ? 1 : img.dims ()(3));
  const octave_idx_type nRows   = img.rows ();
  const octave_idx_type nCols   = img.columns ();

  // The initialized image will be black, this is for the other pixels
  const Magick::Color white ("white");

  const bool *img_fvec = img.fortran_vec ();
  octave_idx_type img_idx = 0;
  for (octave_idx_type frame = 0; frame < nFrames; frame++)
    {
      octave_quit ();

      // For some reason, we can't set the type to Magick::BilevelType or
      // the output image will be black, changing to white has no effect.
      // However, this will still work fine and a binary image will be
      // saved because we are setting the bitdepth to 1.
      Magick::Image m_img = init_enconde_image (nCols, nRows, 1,
                                                Magick::GrayscaleType,
                                                Magick::DirectClass);

      Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
      octave_idx_type GM_idx = 0;
      for (octave_idx_type col = 0; col < nCols; col++)
        {
          for (octave_idx_type row = 0; row < nRows; row++)
            {
              if (img_fvec[img_idx])
                pix[GM_idx] = white;

              img_idx++;
              GM_idx += nCols;
            }
          GM_idx -= nCols * nRows - 1;
        }
      // Save changes to underlying image.
      m_img.syncPixels ();
      // While we could not set it to Bilevel at the start, we can do it
      // here otherwise some coders won't save it as binary.
      m_img.type (Magick::BilevelType);
      imvec.push_back (m_img);
    }
}

template <typename T>
static void
encode_uint_image (std::vector<Magick::Image>& imvec,
                   const T& img, const T& alpha)
{
  typedef typename T::element_type P;
  const octave_idx_type channels = (img.ndims () < 3 ? 1 : img.dims ()(2));
  const octave_idx_type nFrames  = (img.ndims () < 4 ? 1 : img.dims ()(3));
  const octave_idx_type nRows    = img.rows ();
  const octave_idx_type nCols    = img.columns ();
  const octave_idx_type bitdepth = bitdepth_from_class<T> ();

  Magick::ImageType type;
  const bool has_alpha = ! alpha.isempty ();
  switch (channels)
    {
    case 1:
      if (has_alpha)
        type = Magick::GrayscaleMatteType;
      else
        type = Magick::GrayscaleType;
      break;

    case 3:
      if (has_alpha)
        type = Magick::TrueColorMatteType;
      else
        type = Magick::TrueColorType;
      break;

    case 4:
      if (has_alpha)
        type = Magick::ColorSeparationMatteType;
      else
        type = Magick::ColorSeparationType;
      break;

    default:
      // __imwrite should have already filtered this cases
      error ("__magick_write__: wrong size on 3rd dimension");
    }

  // We will be passing the values as integers with depth as specified
  // by QuantumDepth (maximum value specified by MaxRGB).  This is independent
  // of the actual depth of the image.  GM will then convert the values but
  // while in memory, it always keeps the values as specified by QuantumDepth.
  // From GM documentation:
  //  Color arguments are must be scaled to fit the Quantum size according to
  //  the range of MaxRGB
  const double divisor = static_cast<double> ((uint64_t (1) << bitdepth) - 1)
                         / MaxRGB;

  const P *img_fvec = img.fortran_vec ();
  const P *a_fvec   = alpha.fortran_vec ();
  switch (type)
    {
    case Magick::GrayscaleType:
      {
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    const double grey = octave::math::round (double (*img_fvec) / divisor);
                    Magick::Color c (grey, grey, grey);
                    pix[GM_idx] = c;
                    img_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
          }
        break;
      }

    case Magick::GrayscaleMatteType:
      {
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    double grey = octave::math::round (double (*img_fvec) / divisor);
                    Magick::Color c (grey, grey, grey,
                                     MaxRGB - octave::math::round (double (*a_fvec) / divisor));
                    pix[GM_idx] = c;
                    img_fvec++;
                    a_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
          }
        break;
      }

    case Magick::TrueColorType:
      {
        // The fortran_vec offset for the green and blue channels
        const octave_idx_type G_offset = nCols * nRows;
        const octave_idx_type B_offset = nCols * nRows * 2;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    Magick::Color c (octave::math::round (double (*img_fvec)          / divisor),
                                     octave::math::round (double (img_fvec[G_offset]) / divisor),
                                     octave::math::round (double (img_fvec[B_offset]) / divisor));
                    pix[GM_idx] = c;
                    img_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
            img_fvec += B_offset;
          }
        break;
      }

    case Magick::TrueColorMatteType:
      {
        // The fortran_vec offset for the green and blue channels
        const octave_idx_type G_offset = nCols * nRows;
        const octave_idx_type B_offset = nCols * nRows * 2;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    Magick::Color c (octave::math::round (double (*img_fvec)          / divisor),
                                     octave::math::round (double (img_fvec[G_offset]) / divisor),
                                     octave::math::round (double (img_fvec[B_offset]) / divisor),
                                     MaxRGB - octave::math::round (double (*a_fvec) / divisor));
                    pix[GM_idx] = c;
                    img_fvec++;
                    a_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
            img_fvec += B_offset;
          }
        break;
      }

    case Magick::ColorSeparationType:
      {
        // The fortran_vec offset for the Magenta, Yellow, and blacK channels
        const octave_idx_type M_offset = nCols * nRows;
        const octave_idx_type Y_offset = nCols * nRows * 2;
        const octave_idx_type K_offset = nCols * nRows * 3;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    Magick::Color c (octave::math::round (double (*img_fvec)          / divisor),
                                     octave::math::round (double (img_fvec[M_offset]) / divisor),
                                     octave::math::round (double (img_fvec[Y_offset]) / divisor),
                                     octave::math::round (double (img_fvec[K_offset]) / divisor));
                    pix[GM_idx] = c;
                    img_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
            img_fvec += K_offset;
          }
        break;
      }

    case Magick::ColorSeparationMatteType:
      {
        // The fortran_vec offset for the Magenta, Yellow, and blacK channels
        const octave_idx_type M_offset = nCols * nRows;
        const octave_idx_type Y_offset = nCols * nRows * 2;
        const octave_idx_type K_offset = nCols * nRows * 3;
        for (octave_idx_type frame = 0; frame < nFrames; frame++)
          {
            octave_quit ();

            Magick::Image m_img = init_enconde_image (nCols, nRows, bitdepth,
                                                      type,
                                                      Magick::DirectClass);

            Magick::PixelPacket *pix = m_img.getPixels (0, 0, nCols, nRows);
            Magick::IndexPacket *ind = m_img.getIndexes ();
            octave_idx_type GM_idx = 0;
            for (octave_idx_type col = 0; col < nCols; col++)
              {
                for (octave_idx_type row = 0; row < nRows; row++)
                  {
                    Magick::Color c (octave::math::round (double (*img_fvec)          / divisor),
                                     octave::math::round (double (img_fvec[M_offset]) / divisor),
                                     octave::math::round (double (img_fvec[Y_offset]) / divisor),
                                     octave::math::round (double (img_fvec[K_offset]) / divisor));
                    pix[GM_idx] = c;
                    ind[GM_idx] = MaxRGB - octave::math::round (double (*a_fvec) / divisor);
                    img_fvec++;
                    a_fvec++;
                    GM_idx += nCols;
                  }
                GM_idx -= nCols * nRows - 1;
              }
            // Save changes to underlying image.
            m_img.syncPixels ();
            imvec.push_back (m_img);
            img_fvec += K_offset;
          }
        break;
      }

    default:
      error ("__magick_write__: unrecognized Magick::ImageType");
    }

  return;
}

// Meant to be shared with both imfinfo and imwrite.
static std::map<octave_idx_type, std::string>
init_disposal_methods ()
{
  //  GIF Specifications:
  //
  // Disposal Method - Indicates the way in which the graphic is to
  //                    be treated after being displayed.
  //
  //  0 -   No disposal specified.  The decoder is
  //        not required to take any action.
  //  1 -   Do not dispose.  The graphic is to be left
  //        in place.
  //  2 -   Restore to background color.  The area used by the
  //        graphic must be restored to the background color.
  //  3 -   Restore to previous.  The decoder is required to
  //        restore the area overwritten by the graphic with
  //        what was there prior to rendering the graphic.
  //  4-7 - To be defined.
  static std::map<octave_idx_type, std::string> methods;
  if (methods.empty ())
    {
      methods[0] = "doNotSpecify";
      methods[1] = "leaveInPlace";
      methods[2] = "restoreBG";
      methods[3] = "restorePrevious";
    }
  return methods;
}
static std::map<std::string, octave_idx_type>
init_reverse_disposal_methods ()
{
  static std::map<std::string, octave_idx_type> methods;
  if (methods.empty ())
    {
      methods["donotspecify"]     = 0;
      methods["leaveinplace"]     = 1;
      methods["restorebg"]        = 2;
      methods["restoreprevious"]  = 3;
    }
  return methods;
}

void static
write_file (const std::string& filename,
            const std::string& ext,
            std::vector<Magick::Image>& imvec)
{
  try
    {
      Magick::writeImages (imvec.begin (), imvec.end (), ext + ':' + filename);
    }
  catch (Magick::Warning& w)
    {
      warning ("Magick++ warning: %s", w.what ());
    }
  catch (Magick::ErrorCoder& e)
    {
      warning ("Magick++ coder error: %s", e.what ());
    }
  catch (Magick::Exception& e)
    {
      error ("Magick++ exception: %s", e.what ());
    }
}

#endif

DEFUN (__magick_write__, args, ,
       doc: /* -*- texinfo -*-
@deftypefn {} {} __magick_write__ (@var{fname}, @var{fmt}, @var{img}, @var{map}, @var{options})
Write image with GraphicsMagick or ImageMagick.

This is a private internal function not intended for direct use.
Use @code{imwrite} instead.

@seealso{imfinfo, imformats, imread, imwrite}
@end deftypefn */)
{
#if defined (HAVE_MAGICK)

  if (args.length () != 5 || ! args(0).is_string () || ! args(1).is_string ())
    print_usage ();

  maybe_initialize_magick ();

  const std::string filename = args(0).string_value ();
  const std::string ext = args(1).string_value ();

  const octave_scalar_map options
    = args(4).xscalar_map_value ("__magick_write__: OPTIONS must be a struct");

  const octave_value img = args(2);
  const Matrix cmap = args(3).xmatrix_value ("__magick_write__: invalid MAP");

  std::vector<Magick::Image> imvec;

  if (cmap.isempty ())
    {
      const octave_value alpha = options.getfield ("alpha");
      if (img.islogical ())
        encode_bool_image (imvec, img.bool_array_value ());
      else if (img.is_uint8_type ())
        encode_uint_image<uint8NDArray>  (imvec, img.uint8_array_value (),
                                          alpha.uint8_array_value ());
      else if (img.is_uint16_type ())
        encode_uint_image<uint16NDArray> (imvec, img.uint16_array_value (),
                                          alpha.uint16_array_value ());
      else if (img.is_uint32_type ())
        encode_uint_image<uint32NDArray> (imvec, img.uint32_array_value (),
                                          alpha.uint32_array_value ());
      else if (img.isfloat ())
        {
          // For image formats that support floating point values, we write
          // the actual values.  For those who don't, we only use the values
          // on the range [0 1] and save integer values.
          // But here, even for formats that would support floating point
          // values, GM seems unable to do that so we at least make them uint32.
          uint32NDArray clip_img;
          uint32NDArray clip_alpha;
          if (img.is_single_type ())
            {
              clip_img   = img_float2uint<FloatNDArray>
                             (img.float_array_value ());
              clip_alpha = img_float2uint<FloatNDArray>
                             (alpha.float_array_value ());
            }
          else
            {
              clip_img   = img_float2uint<NDArray> (img.array_value ());
              clip_alpha = img_float2uint<NDArray> (alpha.array_value ());
            }
          encode_uint_image<uint32NDArray> (imvec, clip_img, clip_alpha);
        }
      else
        error ("__magick_write__: image type not supported");
    }
  else
    {
      // We should not get floating point indexed images here because we
      // converted them in __imwrite__.m.  We should probably do it here
      // but it would look much messier.
      if (img.is_uint8_type ())
        encode_indexed_images<uint8NDArray>  (imvec, img.uint8_array_value (),
                                              cmap);
      else if (img.is_uint16_type ())
        encode_indexed_images<uint16NDArray> (imvec, img.uint16_array_value (),
                                              cmap);
      else
        error ("__magick_write__: indexed image must be uint8, uint16 or float.");
    }
  static std::map<std::string, octave_idx_type> disposal_methods
    = init_reverse_disposal_methods ();

  const octave_idx_type nFrames = imvec.size ();

  const octave_idx_type quality = options.getfield ("quality").int_value ();
  const ColumnVector delaytime
    = options.getfield ("delaytime").column_vector_value ();
  const Array<std::string> disposalmethod
    = options.getfield ("disposalmethod").cellstr_value ();
  for (octave_idx_type i = 0; i < nFrames; i++)
    {
      imvec[i].quality (quality);
      imvec[i].animationDelay (delaytime(i));
      imvec[i].gifDisposeMethod (disposal_methods[disposalmethod(i)]);
    }

  // If writemode is set to append, read the image and append to it.  Even
  // if set to append, make sure that something was read at all.
  const std::string writemode = options.getfield ("writemode").string_value ();
  if (writemode == "append" && octave::sys::file_stat (filename).exists ())
    {
      std::vector<Magick::Image> ini_imvec;
      read_file (filename, ini_imvec);

      if (ini_imvec.size () > 0)
        {
          ini_imvec.insert (ini_imvec.end (), imvec.begin (), imvec.end ());
          ini_imvec.swap (imvec);
        }
    }

  // FIXME: LoopCount or animationIterations
  //  How it should work:
  //
  // This value is only set for the first image in the sequence.  Trying
  // to set this value with the append mode should have no effect, the
  // value used with the first image is the one that counts (that would
  // also be Matlab compatible).  Thus, the right way to do this would be
  // to have an else block on the condition above, and set this only
  // when creating a new file.  Since Matlab does not interpret a 4D
  // matrix as sequence of images to write, its users need to use a for
  // loop and set LoopCount only on the first iteration (it actually
  // throws warnings otherwise)
  //
  //  Why is this not done the right way:
  //
  // When GM saves a single image, it discards the value if there is only
  // a single image and sets it to "no loop".  Since our default is an
  // infinite loop, if the user tries to do it the Matlab way (setting
  // LoopCount only on the first image) that value will go nowhere.
  // See https://sourceforge.net/p/graphicsmagick/bugs/248/
  // Because of this, we document to set LoopCount on every iteration
  // (in Matlab will cause a lot of warnings), or pass a 4D matrix with
  // all frames (won't work in Matlab at all).
  // Note that this only needs to be set on the first frame
  imvec[0].animationIterations (options.getfield ("loopcount").uint_value ());

  const std::string compression
    = options.getfield ("compression").string_value ();

#define COMPRESS_MAGICK_IMAGE_VECTOR(GM_TYPE)                           \
  for (std::vector<Magick::Image>::size_type i = 0; i < imvec.size (); i++) \
    imvec[i].compressType (GM_TYPE)

  if (compression == "none")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::NoCompression);
  else if (compression == "bzip")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::BZipCompression);
  else if (compression == "fax3")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::FaxCompression);
  else if (compression == "fax4")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::Group4Compression);
  else if (compression == "jpeg")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::JPEGCompression);
  else if (compression == "lzw")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::LZWCompression);
  else if (compression == "rle")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::RLECompression);
  else if (compression == "deflate")
    COMPRESS_MAGICK_IMAGE_VECTOR (Magick::ZipCompression);

#undef COMPRESS_MAGICK_IMAGE_VECTOR

  write_file (filename, ext, imvec);

  return ovl ();

#else

  octave_unused_parameter (args);

  err_disabled_feature ("imwrite", "Image IO");

#endif
}

/*
## No test needed for internal helper function.
%!assert (1)
*/

// Gets the minimum information from images such as its size and format.  Much
// faster than using imfinfo, which slows down a lot since.  Note than without
// this, we need to read the image once for imfinfo to set defaults (which is
// done in Octave language), and then again for the actual reading.
DEFUN (__magick_ping__, args, ,
       doc: /* -*- texinfo -*-
@deftypefn {} {} __magick_ping__ (@var{fname}, @var{idx})
Ping image information with GraphicsMagick or ImageMagick.

This is a private internal function not intended for direct use.

@seealso{imfinfo}
@end deftypefn */)
{
#if defined (HAVE_MAGICK)

  if (args.length () < 1 || ! args(0).is_string ())
    print_usage ();

  maybe_initialize_magick ();

  const std::string filename = args(0).string_value ();

  int idx;
  if (args.length () > 1)
    idx = args(1).int_value () -1;
  else
    idx = 0;

  Magick::Image img;
  img.subImage (idx); // start ping from this image (in case of multi-page)
  img.subRange (1);   // ping only one of them
  try
    {
      img.ping (filename);
    }
  catch (Magick::Warning& w)
    {
      warning ("Magick++ warning: %s", w.what ());
    }
  catch (Magick::Exception& e)
    {
      error ("Magick++ exception: %s", e.what ());
    }

  static const char *fields[] = {"rows", "columns", "format", nullptr};
  octave_scalar_map ping = octave_scalar_map (string_vector (fields));
  ping.setfield ("rows",    octave_value (img.rows ()));
  ping.setfield ("columns", octave_value (img.columns ()));
  ping.setfield ("format",  octave_value (img.magick ()));

  return ovl (ping);

#else

  octave_unused_parameter (args);

  err_disabled_feature ("imfinfo", "Image IO");

#endif
}

#if defined (HAVE_MAGICK)

static octave_value
magick_to_octave_value (const Magick::CompressionType& magick)
{
  switch (magick)
    {
    case Magick::NoCompression:
      return octave_value ("none");
    case Magick::BZipCompression:
      return octave_value ("bzip");
    case Magick::FaxCompression:
      return octave_value ("fax3");
    case Magick::Group4Compression:
      return octave_value ("fax4");
    case Magick::JPEGCompression:
      return octave_value ("jpeg");
    case Magick::LZWCompression:
      return octave_value ("lzw");
    case Magick::RLECompression:
      // This is named "rle" for the HDF, but the same thing is named
      // "ccitt" and "PackBits" for binary and non-binary images in TIFF.
      return octave_value ("rle");
    case Magick::ZipCompression:
      return octave_value ("deflate");

    // The following are present only in recent versions of GraphicsMagick.
    // At the moment the only use of this would be to have imfinfo report
    // the compression method.  In the future, someone could implement
    // the Compression option for imwrite in which case a macro in
    // configure.ac will have to check for their presence of this.
    // See bug #39913
    //      case Magick::LZMACompression:
    //        return octave_value ("lzma");
    //      case Magick::JPEG2000Compression:
    //        return octave_value ("jpeg2000");
    //      case Magick::JBIG1Compression:
    //        return octave_value ("jbig1");
    //      case Magick::JBIG2Compression:
    //        return octave_value ("jbig2");

    default:
      return octave_value ("undefined");
    }
}

static octave_value
magick_to_octave_value (const Magick::EndianType& magick)
{
  switch (magick)
    {
    case Magick::LSBEndian:
      return octave_value ("little-endian");
    case Magick::MSBEndian:
      return octave_value ("big-endian");
    default:
      return octave_value ("undefined");
    }
}

static octave_value
magick_to_octave_value (const Magick::OrientationType& magick)
{
  switch (magick)
    {
    // Values come from the TIFF6 spec
    case Magick::TopLeftOrientation:
      return octave_value (1);
    case Magick::TopRightOrientation:
      return octave_value (2);
    case Magick::BottomRightOrientation:
      return octave_value (3);
    case Magick::BottomLeftOrientation:
      return octave_value (4);
    case Magick::LeftTopOrientation:
      return octave_value (5);
    case Magick::RightTopOrientation:
      return octave_value (6);
    case Magick::RightBottomOrientation:
      return octave_value (7);
    case Magick::LeftBottomOrientation:
      return octave_value (8);
    default:
      return octave_value (1);
    }
}

static octave_value
magick_to_octave_value (const Magick::ResolutionType& magick)
{
  switch (magick)
    {
    case Magick::PixelsPerInchResolution:
      return octave_value ("Inch");
    case Magick::PixelsPerCentimeterResolution:
      return octave_value ("Centimeter");
    default:
      return octave_value ("undefined");
    }
}

static bool
is_valid_exif (const std::string& val)
{
  // Sometimes GM will return the string "unknown" instead of empty
  // for an empty value.
  return (! val.empty () && val != "unknown");
}

static void
fill_exif (octave_scalar_map& map, Magick::Image& img,
           const std::string& key)
{
  const std::string attr = img.attribute ("EXIF:" + key);
  if (is_valid_exif (attr))
    map.setfield (key, octave_value (attr));
  return;
}

static void
fill_exif_ints (octave_scalar_map& map, Magick::Image& img,
                const std::string& key)
{
  const std::string attr = img.attribute ("EXIF:" + key);
  if (is_valid_exif (attr))
    {
      // string of the type "float,float,float....."
      float number;
      ColumnVector values (std::count (attr.begin (), attr.end (), ',') +1);
      std::string sub;
      std::istringstream sstream (attr);
      octave_idx_type n = 0;
      while (std::getline (sstream, sub, char (',')))
        {
          sscanf (sub.c_str (), "%f", &number);
          values(n++) = number;
        }
      map.setfield (key, octave_value (values));
    }
  return;
}

static void
fill_exif_floats (octave_scalar_map& map, Magick::Image& img,
                  const std::string& key)
{
  const std::string attr = img.attribute ("EXIF:" + key);
  if (is_valid_exif (attr))
    {
      // string of the type "int/int,int/int,int/int....."
      int numerator;
      int denominator;
      ColumnVector values (std::count (attr.begin (), attr.end (), ',') +1);
      std::string sub;
      std::istringstream sstream (attr);
      octave_idx_type n = 0;
      while (std::getline (sstream, sub, ','))
        {
          sscanf (sub.c_str (), "%i/%i", &numerator, &denominator);
          values(n++) = double (numerator) / double (denominator);
        }
      map.setfield (key, octave_value (values));
    }
  return;
}

#endif

DEFUN (__magick_finfo__, args, ,
       doc: /* -*- texinfo -*-
@deftypefn {} {} __magick_finfo__ (@var{fname})
Read image information with GraphicsMagick or ImageMagick.

This is a private internal function not intended for direct use.
Use @code{imfinfo} instead.

@seealso{imfinfo, imformats, imread, imwrite}
@end deftypefn */)
{
#if defined (HAVE_MAGICK)

  if (args.length () < 1 || ! args(0).is_string ())
    print_usage ();

  maybe_initialize_magick ();

  const std::string filename = args(0).string_value ();

  std::vector<Magick::Image> imvec;
  read_file (filename, imvec);

  const octave_idx_type nFrames = imvec.size ();
  const std::string format = imvec[0].magick ();

  // Here's how this function works.  We need to return a struct array, one
  // struct for each image in the file (remember, there are image
  // that allow for multiple images in the same file).  Now, Matlab seems
  // to have format specific code so the fields on the struct are different
  // for each format.  It only has a small subset that is common to all
  // of them, the others are undocumented.  Because we try to abstract from
  // the formats we always return the same list of fields (note that with
  // GM we support more than 88 formats.  That's way more than Matlab, and
  // I don't want to write specific code for each of them).
  //
  // So what we do is we create an octave_scalar_map, fill it with the
  // information for that image, and then insert it into an octave_map.
  // Because in the same file, different images may have values for
  // different fields, we can't create a field only if there's a value.
  // Bad things happen if we merge octave_scalar_maps with different
  // fields from the others (suppose for example a TIFF file with 4 images,
  // where only the third image has a colormap.

  static const char *fields[] =
  {
    // These are fields that must always appear for Matlab.
    "Filename",
    "FileModDate",
    "FileSize",
    "Format",
    "FormatVersion",
    "Width",
    "Height",
    "BitDepth",
    "ColorType",

    // These are format specific or not existent in Matlab.  The most
    // annoying thing is that Matlab may have different names for the
    // same thing in different formats.
    "DelayTime",
    "DisposalMethod",
    "LoopCount",
    "ByteOrder",
    "Gamma",
    "Chromaticities",
    "Comment",
    "Quality",
    "Compression",        // same as CompressionType
    "Colormap",           // same as ColorTable (in PNG)
    "Orientation",
    "ResolutionUnit",
    "XResolution",
    "YResolution",
    "Software",           // sometimes is an Exif tag
    "Make",               // actually an Exif tag
    "Model",              // actually an Exif tag
    "DateTime",           // actually an Exif tag
    "ImageDescription",   // actually an Exif tag
    "Artist",             // actually an Exif tag
    "Copyright",          // actually an Exif tag
    "DigitalCamera",
    "GPSInfo",
    // Notes for the future: GM allows one to get many attributes, and even has
    // attribute() to obtain arbitrary ones, that may exist in only some
    // cases.  The following is a list of some methods and into what possible
    // Matlab compatible values they may be converted.
    //
    //  colorSpace()      -> PhotometricInterpretation
    //  backgroundColor() -> BackgroundColor
    //  interlaceType()   -> Interlaced, InterlaceType, and PlanarConfiguration
    //  label()           -> Title
    nullptr
  };

  // The one we will return at the end
  octave_map info (dim_vector (nFrames, 1), string_vector (fields));

  // Some of the fields in the struct are about file information and will be
  // the same for all images in the file.  So we create a template, fill in
  // those values, and make a copy of the template for each image.
  octave_scalar_map template_info = (string_vector (fields));

  template_info.setfield ("Format", octave_value (format));
  // We can't actually get FormatVersion but even Matlab sometimes can't.
  template_info.setfield ("FormatVersion", octave_value (""));

  const octave::sys::file_stat fs (filename);
  if (! fs)
    error ("imfinfo: error reading '%s': %s", filename.c_str (),
           fs.error ().c_str ());

  const octave::sys::localtime mtime (fs.mtime ());
  const std::string filetime = mtime.strftime ("%e-%b-%Y %H:%M:%S");
  template_info.setfield ("Filename",    octave_value (filename));
  template_info.setfield ("FileModDate", octave_value (filetime));
  template_info.setfield ("FileSize",    octave_value (fs.size ()));

  for (octave_idx_type frame = 0; frame < nFrames; frame++)
    {
      octave_quit ();

      octave_scalar_map info_frame (template_info);
      const Magick::Image img = imvec[frame];

      info_frame.setfield ("Width",  octave_value (img.columns ()));
      info_frame.setfield ("Height", octave_value (img.rows ()));
      info_frame.setfield ("BitDepth",
                           octave_value (get_depth (const_cast<Magick::Image&> (img))));

      // Stuff related to colormap, image class and type
      // Because GM is too smart for us...  Read the comments in is_indexed()
      {
        std::string color_type;
        Matrix cmap;
        if (is_indexed (img))
          {
            color_type = "indexed";
            cmap = read_maps (const_cast<Magick::Image&> (img))(0).matrix_value ();
          }
        else
          {
            switch (img.type ())
              {
              case Magick::BilevelType:
              case Magick::GrayscaleType:
              case Magick::GrayscaleMatteType:
                color_type = "grayscale";
                break;

              case Magick::TrueColorType:
              case Magick::TrueColorMatteType:
                color_type = "truecolor";
                break;

              case Magick::PaletteType:
              case Magick::PaletteMatteType:
                // we should never get here or is_indexed needs to be fixed
                color_type = "indexed";
                break;

              case Magick::ColorSeparationType:
              case Magick::ColorSeparationMatteType:
                color_type = "CMYK";
                break;

              default:
                color_type = "undefined";
              }
          }
        info_frame.setfield ("ColorType", octave_value (color_type));
        info_frame.setfield ("Colormap",  octave_value (cmap));
      }

      {
        // Not all images have chroma values.  In such cases, they'll
        // be all zeros.  So rather than send a matrix of zeros, we will
        // check for that, and send an empty vector instead.
        RowVector chromaticities (8);
        double *chroma_fvec = chromaticities.fortran_vec ();
        img.chromaWhitePoint    (&chroma_fvec[0], &chroma_fvec[1]);
        img.chromaRedPrimary    (&chroma_fvec[2], &chroma_fvec[3]);
        img.chromaGreenPrimary  (&chroma_fvec[4], &chroma_fvec[5]);
        img.chromaBluePrimary   (&chroma_fvec[6], &chroma_fvec[7]);
        if (chromaticities.nnz () == 0)
          chromaticities = RowVector (0);
        info_frame.setfield ("Chromaticities", octave_value (chromaticities));
      }

      info_frame.setfield ("Gamma",       octave_value (img.gamma ()));
      info_frame.setfield ("XResolution", octave_value (img.xResolution ()));
      info_frame.setfield ("YResolution", octave_value (img.yResolution ()));
      info_frame.setfield ("DelayTime",   octave_value (img.animationDelay ()));
      info_frame.setfield ("LoopCount",
                           octave_value (img.animationIterations ()));
      info_frame.setfield ("Quality",     octave_value (img.quality ()));
      info_frame.setfield ("Comment",     octave_value (img.comment ()));

      info_frame.setfield ("Compression",
                           magick_to_octave_value (img.compressType ()));
      info_frame.setfield ("Orientation",
                           magick_to_octave_value (img.orientation ()));
      info_frame.setfield ("ResolutionUnit",
                           magick_to_octave_value (img.resolutionUnits ()));
      info_frame.setfield ("ByteOrder",
                           magick_to_octave_value (img.endian ()));

      // It is not possible to know if there's an Exif field so we just
      // check for the Exif Version value.  If it does exists, then we
      // bother about looking for specific fields.
      {
        Magick::Image& cimg = const_cast<Magick::Image&> (img);

        // These will be in Exif tags but must appear as fields in the
        // base struct array, not as another struct in one of its fields.
        // This is likely because they belong to the Baseline TIFF specs
        // and may appear out of the Exif tag.  So first we check if it
        // exists outside the Exif tag.
        // See Section 4.6.4, table 4, page 28 of Exif specs version 2.3
        // (CIPA DC- 008-Translation- 2010)
        static const char *base_exif_str_fields[] =
        {
          "DateTime",
          "ImageDescription",
          "Make",
          "Model",
          "Software",
          "Artist",
          "Copyright",
          nullptr,
        };
        static const string_vector base_exif_str (base_exif_str_fields);
        static const octave_idx_type n_base_exif_str = base_exif_str.numel ();
        for (octave_idx_type field = 0; field < n_base_exif_str; field++)
          {
            info_frame.setfield (base_exif_str[field],
                                 octave_value (cimg.attribute (base_exif_str[field])));
            fill_exif (info_frame, cimg, base_exif_str[field]);
          }

        octave_scalar_map camera;
        octave_scalar_map gps;
        if (! cimg.attribute ("EXIF:ExifVersion").empty ())
          {
            // See Section 4.6.5, table 7 and 8, over pages page 42 to 43
            // of Exif specs version 2.3 (CIPA DC- 008-Translation- 2010)

            // Listed on the Exif specs as being of type ASCII.
            static const char *exif_str_fields[] =
            {
              "RelatedSoundFile",
              "DateTimeOriginal",
              "DateTimeDigitized",
              "SubSecTime",
              "DateTimeOriginal",
              "SubSecTimeOriginal",
              "SubSecTimeDigitized",
              "ImageUniqueID",
              "CameraOwnerName",
              "BodySerialNumber",
              "LensMake",
              "LensModel",
              "LensSerialNumber",
              "SpectralSensitivity",
              // These last two are of type undefined but most likely will
              // be strings.  Even if they're not GM returns a string anyway.
              "UserComment",
              "MakerComment",
              nullptr
            };
            static const string_vector exif_str (exif_str_fields);
            static const octave_idx_type n_exif_str = exif_str.numel ();
            for (octave_idx_type field = 0; field < n_exif_str; field++)
              fill_exif (camera, cimg, exif_str[field]);

            // Listed on the Exif specs as being of type SHORT or LONG.
            static const char *exif_int_fields[] =
            {
              "ColorSpace",
              "ExifImageWidth",  // PixelXDimension (CPixelXDimension in Matlab)
              "ExifImageHeight", // PixelYDimension (CPixelYDimension in Matlab)
              "PhotographicSensitivity",
              "StandardOutputSensitivity",
              "RecommendedExposureIndex",
              "ISOSpeed",
              "ISOSpeedLatitudeyyy",
              "ISOSpeedLatitudezzz",
              "FocalPlaneResolutionUnit",
              "FocalLengthIn35mmFilm",
              // Listed as SHORT or LONG but with more than 1 count.
              "SubjectArea",
              "SubjectLocation",
              // While the following are an integer, their value have a meaning
              // that must be represented as a string for Matlab compatibility.
              // For example, a 3 on ExposureProgram, would return
              // "Aperture priority" as defined on the Exif specs.
              "ExposureProgram",
              "SensitivityType",
              "MeteringMode",
              "LightSource",
              "Flash",
              "SensingMethod",
              "FileSource",
              "CustomRendered",
              "ExposureMode",
              "WhiteBalance",
              "SceneCaptureType",
              "GainControl",
              "Contrast",
              "Saturation",
              "Sharpness",
              "SubjectDistanceRange",
              nullptr
            };
            static const string_vector exif_int (exif_int_fields);
            static const octave_idx_type n_exif_int = exif_int.numel ();
            for (octave_idx_type field = 0; field < n_exif_int; field++)
              fill_exif_ints (camera, cimg, exif_int[field]);

            // Listed as RATIONAL or SRATIONAL
            static const char *exif_float_fields[] =
            {
              "Gamma",
              "CompressedBitsPerPixel",
              "ExposureTime",
              "FNumber",
              "ShutterSpeedValue",  // SRATIONAL
              "ApertureValue",
              "BrightnessValue",    // SRATIONAL
              "ExposureBiasValue",  // SRATIONAL
              "MaxApertureValue",
              "SubjectDistance",
              "FocalLength",
              "FlashEnergy",
              "FocalPlaneXResolution",
              "FocalPlaneYResolution",
              "ExposureIndex",
              "DigitalZoomRatio",
              // Listed as RATIONAL or SRATIONAL with more than 1 count.
              "LensSpecification",
              nullptr
            };
            static const string_vector exif_float (exif_float_fields);
            static const octave_idx_type n_exif_float = exif_float.numel ();
            for (octave_idx_type field = 0; field < n_exif_float; field++)
              fill_exif_floats (camera, cimg, exif_float[field]);

            // Inside a Exif field, it is possible that there is also a
            // GPS field.  This is not the same as ExifVersion but seems
            // to be how we have to check for it.
            if (cimg.attribute ("EXIF:GPSInfo") != "unknown")
              {
                // The story here is the same as with Exif.
                // See Section 4.6.6, table 15 on page 68 of Exif specs
                // version 2.3 (CIPA DC- 008-Translation- 2010)

                static const char *gps_str_fields[] =
                {
                  "GPSLatitudeRef",
                  "GPSLongitudeRef",
                  "GPSAltitudeRef",
                  "GPSSatellites",
                  "GPSStatus",
                  "GPSMeasureMode",
                  "GPSSpeedRef",
                  "GPSTrackRef",
                  "GPSImgDirectionRef",
                  "GPSMapDatum",
                  "GPSDestLatitudeRef",
                  "GPSDestLongitudeRef",
                  "GPSDestBearingRef",
                  "GPSDestDistanceRef",
                  "GPSDateStamp",
                  nullptr
                };
                static const string_vector gps_str (gps_str_fields);
                static const octave_idx_type n_gps_str = gps_str.numel ();
                for (octave_idx_type field = 0; field < n_gps_str; field++)
                  fill_exif (gps, cimg, gps_str[field]);

                static const char *gps_int_fields[] =
                {
                  "GPSDifferential",
                  nullptr
                };
                static const string_vector gps_int (gps_int_fields);
                static const octave_idx_type n_gps_int = gps_int.numel ();
                for (octave_idx_type field = 0; field < n_gps_int; field++)
                  fill_exif_ints (gps, cimg, gps_int[field]);

                static const char *gps_float_fields[] =
                {
                  "GPSAltitude",
                  "GPSDOP",
                  "GPSSpeed",
                  "GPSTrack",
                  "GPSImgDirection",
                  "GPSDestBearing",
                  "GPSDestDistance",
                  "GPSHPositioningError",
                  // Listed as RATIONAL or SRATIONAL with more than 1 count.
                  "GPSLatitude",
                  "GPSLongitude",
                  "GPSTimeStamp",
                  "GPSDestLatitude",
                  "GPSDestLongitude",
                  nullptr
                };
                static const string_vector gps_float (gps_float_fields);
                static const octave_idx_type n_gps_float = gps_float.numel ();
                for (octave_idx_type field = 0; field < n_gps_float; field++)
                  fill_exif_floats (gps, cimg, gps_float[field]);

              }
          }
        info_frame.setfield ("DigitalCamera", octave_value (camera));
        info_frame.setfield ("GPSInfo",       octave_value (gps));
      }

      info.fast_elem_insert (frame, info_frame);
    }

  if (format == "GIF")
    {
      static std::map<octave_idx_type, std::string> disposal_methods
        = init_disposal_methods ();
      string_vector methods (nFrames);
      for (octave_idx_type frame = 0; frame < nFrames; frame++)
        methods[frame] = disposal_methods[imvec[frame].gifDisposeMethod ()];
      info.setfield ("DisposalMethod", Cell (methods));
    }
  else
    info.setfield ("DisposalMethod",
                   Cell (dim_vector (nFrames, 1), octave_value ("")));

  return ovl (info);

#else

  octave_unused_parameter (args);

  err_disabled_feature ("imfinfo", "Image IO");

#endif
}

/*
## No test needed for internal helper function.
%!assert (1)
*/

DEFUN (__magick_formats__, args, ,
       doc: /* -*- texinfo -*-
@deftypefn {} {} __magick_imformats__ (@var{formats})
Fill formats info with GraphicsMagick CoderInfo.

@seealso{imfinfo, imformats, imread, imwrite}
@end deftypefn */)
{
  if (args.length () != 1 || ! args(0).isstruct ())
    print_usage ();

  octave_map formats = args(0).map_value ();

#if defined (HAVE_MAGICK)

  maybe_initialize_magick ();

  for (octave_idx_type idx = 0; idx < formats.numel (); idx++)
    {
      try
        {
          octave_scalar_map fmt = formats.checkelem (idx);
          Magick::CoderInfo coder (fmt.getfield ("coder").string_value ());

          fmt.setfield ("description", octave_value (coder.description ()));
          fmt.setfield ("multipage", coder.isMultiFrame () ? true : false);
          // default for read and write is a function handle.  If we can't
          // read or write them, them set it to an empty value
          if (! coder.isReadable ())
            fmt.setfield ("read",  Matrix ());
          if (! coder.isWritable ())
            fmt.setfield ("write", Matrix ());
          formats.fast_elem_insert (idx, fmt);
        }
      catch (Magick::Exception& e)
        {
          // Exception here are missing formats.  So we remove the format
          // from the structure and reduce idx.
          formats.delete_elements (idx);
          idx--;
        }
    }

#else

  formats = octave_map (dim_vector (1, 0), formats.fieldnames ());

#endif

  return ovl (formats);
}

/*
## No test needed for internal helper function.
%!assert (1)
*/