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
|
/**************************************************************************//**
* Utility functions for RMagick.
*
* Copyright © 2002 - 2009 by Timothy P. Hunter
*
* Changes since Nov. 2009 copyright © by Benjamin Thomas and Omer Bar-or
*
* @file rmutil.cpp
* @version $Id: rmutil.cpp,v 1.182 2009/12/21 10:34:58 baror Exp $
* @author Tim Hunter
******************************************************************************/
#include "rmagick.h"
#include <errno.h>
#if defined(_WIN32)
#include <Windows.h>
#else
#include <pthread.h>
#endif
static VALUE rescue_not_str(VALUE, VALUE ATTRIBUTE_UNUSED) ATTRIBUTE_NORETURN;
static void handle_exception(ExceptionInfo *, Image *, ErrorRetention);
#if defined(IMAGEMAGICK_7)
DEFINE_GVL_STUB3(TransformImageColorspace, Image *, const ColorspaceType, ExceptionInfo *);
#else
DEFINE_GVL_STUB2(TransformImageColorspace, Image *, const ColorspaceType);
#endif
DEFINE_GVL_STUB5(CloneImage, const Image *, const size_t, const size_t, const MagickBooleanType, ExceptionInfo *);
/**
* ImageMagick safe version of malloc.
*
* No Ruby usage (internal function)
*
* Notes:
* - Use when managing memory that ImageMagick may have allocated or may free.
* - If malloc fails, it raises an exception.
* - magick_safe_malloc and magick_safe_realloc prevent exceptions caused by
* integer overflow. Added in 6.3.5-9 but backwards compatible with prior
* releases.
*
* @param count the number of quantum elements to allocate
* @param quantum the number of bytes in each quantum
* @return a pointer to a block of memory that is at least count*quantum
*/
void *
magick_safe_malloc(const size_t count, const size_t quantum)
{
void *ptr;
ptr = AcquireQuantumMemory(count, quantum);
if (!ptr)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return ptr;
}
/**
* ImageMagick version of malloc.
*
* No Ruby usage (internal function)
*
* @param size the size of memory to allocate
* @return pointer to a block of memory
*/
void *
magick_malloc(const size_t size)
{
void *ptr;
ptr = AcquireMagickMemory(size);
if (!ptr)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return ptr;
}
/**
* ImageMagick version of free.
*
* No Ruby usage (internal function)
*
* @param ptr pointer to the existing block of memory
*/
void
magick_free(void *ptr)
{
RelinquishMagickMemory(ptr);
}
/**
* ImageMagick safe version of realloc.
*
* No Ruby usage (internal function)
*
* Notes:
* - Use when managing memory that ImageMagick may have allocated or may free.
* - If malloc fails, it raises an exception.
* - magick_safe_malloc and magick_safe_realloc prevent exceptions caused by
* integer overflow. Added in 6.3.5-9 but backwards compatible with prior
* releases.
*
* @param memory the existing block of memory
* @param count the number of quantum elements to allocate
* @param quantum the number of bytes in each quantum
* @return a pointer to a block of memory that is at least count*quantum in size
*/
void *
magick_safe_realloc(void *memory, const size_t count, const size_t quantum)
{
void *v;
v = ResizeQuantumMemory(memory, count, quantum);
if (!v)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
return v;
}
/**
* Make a copy of a string in malloc'd memory.
*
* No Ruby usage (internal function)
*
* Notes:
* - Any existing string pointed to by *new_str is freed.
* - CloneString asserts if no memory. No need to check its return value.
*
* @param new_str pointer to the new string
* @param str the string to copy
*/
void
magick_clone_string(char **new_str, const char *str)
{
CloneString(new_str, str);
}
/**
* Compare s1 and s2 ignoring case.
*
* No Ruby usage (internal function)
*
* @param s1 the first string
* @param s2 the second string
* @return same as strcmp(3)
*/
int
rm_strcasecmp(const char *s1, const char *s2)
{
while (*s1 && *s2)
{
if (toupper(*s1) != toupper(*s2))
{
break;
}
s1 += 1;
s2 += 1;
}
return (int)(*s1 - *s2);
}
/**
* Compare s1 and s2 ignoring case.
*
* No Ruby usage (internal function)
*
* @param s1 the first string
* @param s2 the second string
* @param n number of characters to compare
* @return same as strcmp(3)
*/
int
rm_strncasecmp(const char *s1, const char *s2, size_t n)
{
if (n == 0)
{
return 0;
}
while (toupper(*s1) == toupper(*s2))
{
if (--n == 0 || *s1 == '\0')
{
return 0;
}
s1 += 1;
s2 += 1;
}
return (int)(*s1 - *s2);
}
/**
* Get string length.
*
* No Ruby usage (internal function)
*
* @param str the string
* @param strsz the maximum number of characters
* @return same as strnlen_s()
*/
size_t
rm_strnlen_s(const char *str, size_t strsz)
{
size_t length = 0;
while(*str && length < strsz)
{
str++;
length++;
}
return length;
}
/**
* Raise exception if array too short.
*
* No Ruby usage (internal function)
*
* @param ary the array
* @param len the minimum length
* @throw IndexError
*/
void
rm_check_ary_len(VALUE ary, long len)
{
if (RARRAY_LEN(ary) < len)
{
rb_raise(rb_eIndexError, "not enough elements in array - expecting %ld, got %ld",
len, (long)RARRAY_LEN(ary));
}
}
/**
* Raise exception if ary argument was invalid type
*
* No Ruby usage (internal function)
*
* @param ary the array
* @return the array that is converted type of argument object if needed
* @throw TypeError
*/
VALUE
rm_check_ary_type(VALUE ary)
{
VALUE checked = rb_check_array_type(ary);
if (NIL_P(checked))
{
rb_raise(rb_eTypeError, "wrong argument type %" RMIsVALUE " was given. (must respond to :to_ary)", rb_obj_class(ary));
}
return checked;
}
/**
* Raise an error if the image has been destroyed.
*
* No Ruby usage (internal function)
*
* @param obj the image
* @return the C image structure for the image
* @throw DestroyedImageError
*/
Image *
rm_check_destroyed(VALUE obj)
{
Image *image;
TypedData_Get_Struct(obj, Image, &rm_image_data_type, image);
if (!image)
{
rb_raise(Class_DestroyedImageError, "destroyed image");
}
return image;
}
/**
* Raise an error if the image has been destroyed or is frozen.
*
* No Ruby usage (internal function)
*
* @param obj the image
* @return the C image structure for the image
*/
Image *
rm_check_frozen(VALUE obj)
{
Image *image = rm_check_destroyed(obj);
rb_check_frozen(obj);
return image;
}
/**
* Overrides freeze in classes that can't be frozen.
*
* No Ruby usage (internal function)
*
* @raise [TypeError]
*/
VALUE
rm_no_freeze(VALUE obj)
{
rb_raise(rb_eTypeError, "can't freeze %s", rb_class2name(CLASS_OF(obj)));
}
/**
* Supply our own version of the "obsolete" rb_str2cstr.
*
* No Ruby usage (internal function)
*
* @param str the Ruby string
* @param len pointer to a size_t in which to store the number of characters
* @return a C string version of str
*/
char *
rm_str2cstr(VALUE str, size_t *len)
{
StringValue(str);
if (len)
{
*len = (size_t)RSTRING_LEN(str);
}
return RSTRING_PTR(str);
}
/**
* Called when `rb_str_to_str' raises an exception.
*
* No Ruby usage (internal function)
*
* @param arg the argument
* @return 0
* @throw TypeError
*/
static VALUE
rescue_not_str(VALUE arg, VALUE raised_exc ATTRIBUTE_UNUSED)
{
rb_raise(rb_eTypeError, "argument must be a number or a string in the form 'NN%%' (%s given)",
rb_class2name(CLASS_OF(arg)));
}
/**
* Return a double between 0.0 and max (the second argument), inclusive. If the
* argument is a number convert to a Float object, otherwise it's supposed to be
* a string in the form * "NN%". Convert to a number and then to a Float.
*
* No Ruby usage (internal function)
*
* @param arg the argument
* @param max the maximum allowed value
* @param only_positive Accept whether only positive numbers?
* @return a double
*/
double
rm_percentage2(VALUE arg, double max, bool only_positive)
{
double pct;
if (!rm_check_num2dbl(arg))
{
pct = rm_str_to_pct(arg, only_positive) * max;
}
else
{
pct = NUM2DBL(arg);
if (pct < 0.0 && only_positive)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%g')", pct);
}
}
return pct;
}
/**
* Return a double between 0.0 and max (the second argument), inclusive. If the
* argument is a number convert to a Float object, otherwise it's supposed to be
* a string in the form * "NN%". Convert to a number and then to a Float.
*
* No Ruby usage (internal function)
*
* @param arg the argument
* @param max the maximum allowed value
* @return a double
*/
double
rm_percentage(VALUE arg, double max)
{
return rm_percentage2(arg, max, true);
}
/**
* Return 0 if rb_num2dbl doesn't raise an exception.
*
* No Ruby usage (internal function)
*
* @param obj the object to convert to a double
* @return 0
*/
static VALUE
check_num2dbl(VALUE obj)
{
rb_num2dbl(obj);
return INT2FIX(1);
}
/**
* Called if rb_num2dbl raises an exception.
*
* No Ruby usage (internal function)
*
* @param ignored a Ruby object (unused)
* @return 0
*/
static VALUE
rescue_not_dbl(VALUE ignored ATTRIBUTE_UNUSED, VALUE raised_exc ATTRIBUTE_UNUSED)
{
return INT2FIX(0);
}
/**
* Return 1 if the object can be converted to a double, 0 otherwise.
*
* No Ruby usage (internal function)
*
* @param obj the object
* @return 1 or 0
*/
int
rm_check_num2dbl(VALUE obj)
{
return FIX2INT(rb_rescue(RESCUE_FUNC(check_num2dbl), obj, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_dbl), (VALUE)0));
}
/**
* Given a string in the form NN% return the corresponding double.
*
* No Ruby usage (internal function)
*
* @param str the string
* @param only_positive Accept whether only positive numbers?
* @return a double
*/
double
rm_str_to_pct(VALUE str, bool only_positive)
{
long pct;
char *pct_str, *end;
str = rb_rescue(RESCUE_FUNC(rb_str_to_str), str, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_str), str);
pct_str = StringValueCStr(str);
errno = 0;
pct = strtol(pct_str, &end, 10);
if (errno == ERANGE)
{
rb_raise(rb_eRangeError, "`%s' out of range", pct_str);
}
if (*end != '%')
{
rb_raise(rb_eArgError, "expected percentage, got `%s'", pct_str);
}
if (pct < 0L && only_positive)
{
rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", pct_str);
}
return pct / 100.0;
}
/**
* If the argument is a number, convert it to a double. Otherwise it's supposed
* to be a string in the form 'NN%'. Return a percentage of QuantumRange.
*
* No Ruby usage (internal function)
*
* @param fuzz_arg the fuzz argument
* @return a double
* @see Image_fuzz
* @see Image_fuzz_eq
*/
double
rm_fuzz_to_dbl(VALUE fuzz_arg)
{
return rm_percentage(fuzz_arg, QuantumRange);
}
/**
* Convert a application-supplied number to a Quantum. If the object is a Float,
* truncate it before converting.
*
* No Ruby usage (internal function)
*
* Notes:
* - Ruby says that 2147483647.5 doesn't fit into an unsigned long. If you
* truncate it, it works.
* - Should use this only when the input value is possibly subject to this
* problem.
*
* @param obj the application-supplied number
* @return a Quantum
*/
Quantum
rm_app2quantum(VALUE obj)
{
VALUE v = obj;
if (TYPE(obj) == T_FLOAT)
{
v = rb_Integer(obj);
}
return NUM2QUANTUM(v);
}
/**
* Returns a pointer to an image structure initialized to default values
*
* No Ruby usage (internal function)
*
* @param info the info
* @return the created image
*/
Image *
rm_acquire_image(ImageInfo *info)
{
#if defined(IMAGEMAGICK_7)
Image *new_image;
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
new_image = AcquireImage(info, exception);
CHECK_EXCEPTION();
DestroyExceptionInfo(exception);
return new_image;
#else
return AcquireImage(info);
#endif
}
/**
* Send the "cur_image" method to the object. If 'img' is an ImageList, then
* cur_image is self[\@scene]. If 'img' is an image, then cur_image is simply
* 'self'.
*
* No Ruby usage (internal function)
*
* @param img the object
* @return the return value from "cur_image"
*/
VALUE
rm_cur_image(VALUE img)
{
return rb_funcall(img, rm_ID_cur_image, 0);
}
/**
* Map the color intensity to a named color.
*
* No Ruby usage (internal function)
*
* @param image the image
* @param color the color intensity as a PixelColor
* @return the named color as a String
* @see rm_pixelcolor_to_color_name_info
*/
VALUE
rm_pixelcolor_to_color_name(Image *image, PixelColor *color)
{
MagickPixel mpp;
char tuple[MaxTextExtent];
#if defined(IMAGEMAGICK_7)
mpp = *color;
mpp.alpha_trait = BlendPixelTrait;
mpp.colorspace = image->colorspace;
#else
rm_init_magickpixel(image, &mpp);
mpp.red = GetPixelRed(color);
mpp.green = GetPixelGreen(color);
mpp.blue = GetPixelBlue(color);
mpp.opacity = GetPixelOpacity(color);
mpp.index = (MagickRealType) 0.0;
mpp.matte = MagickTrue;
#endif
mpp.depth = image->depth;
GetColorTuple(&mpp, MagickTrue, tuple);
return rb_str_new2(tuple);
}
/**
* Map the color intensity to a named color.
*
* No Ruby usage (internal function)
*
* Notes:
* - Simply create an Image from the Info, call QueryColorname, and then
* destroy the Image.
* - The default depth is always used, and the matte value is set to False,
* which means "don't use the alpha channel".
*
* @param info the info
* @param color the color intensity as a PixelColor
* @return the named color as a String
* @see rm_pixelcolor_to_color_name
*/
VALUE
rm_pixelcolor_to_color_name_info(Info *info, PixelColor *color)
{
Image *image;
VALUE color_name;
image = rm_acquire_image(info);
if (!image)
{
rb_raise(rb_eNoMemError, "not enough memory to continue.");
}
#if defined(IMAGEMAGICK_7)
image->alpha_trait = UndefinedPixelTrait;
#else
image->matte = MagickFalse;
#endif
color_name = rm_pixelcolor_to_color_name(image, color);
DestroyImage(image);
return color_name;
}
/**
* Initializes the MagickPixel structure.
*
* No Ruby usage (internal function)
*
* @param image the image
* @param pp the MagickPixel
*/
void
rm_init_magickpixel(const Image *image, MagickPixel *pp)
{
#if defined(IMAGEMAGICK_7)
GetPixelInfo(image, pp);
#else
GetMagickPixelPacket(image, pp);
#endif
}
/**
* Initializes the MagickPixel structure to the specified color.
*
* No Ruby usage (internal function)
*
* @param pp the MagickPixel
* @param color the color
*/
void
rm_set_magickpixel(MagickPixel *pp, const char *color)
{
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
#if defined(IMAGEMAGICK_7)
QueryColorCompliance(color, AllCompliance, pp, exception);
#else
QueryMagickColor(color, pp, exception);
#endif
// This exception is ignored because the color comes from places where we control
// the value and it is very unlikely that an exception will be thrown.
DestroyExceptionInfo(exception);
}
/**
* Write a temporary copy of the image to the IM registry.
*
* No Ruby usage (internal function)
*
* Notes:
* - The `temp_name' argument must point to an char array of size
* MaxTextExtent.
*
* @param image the image
* @param temp_name the temporary name to use
* @param temp_name_l the length of temp_name
* @return the "filename" of the registered image
*/
void
rm_write_temp_image(Image *image, char *temp_name, size_t temp_name_l)
{
#define TMPNAM_CLASS_VAR "@@_tmpnam_"
MagickBooleanType okay;
ExceptionInfo *exception;
VALUE id_value;
int id;
exception = AcquireExceptionInfo();
// 'id' is always the value of its previous use
if (rb_cvar_defined(Module_Magick, rb_intern(TMPNAM_CLASS_VAR)) == Qtrue)
{
id_value = rb_cv_get(Module_Magick, TMPNAM_CLASS_VAR);
id = FIX2INT(id_value);
}
else
{
id = 0;
rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
}
id += 1;
rb_cv_set(Module_Magick, TMPNAM_CLASS_VAR, INT2FIX(id));
snprintf(temp_name, temp_name_l, "mpri:%d", id);
// Omit "mpri:" from filename to form the key
okay = (MagickBooleanType)SetImageRegistry(ImageRegistryType, temp_name+5, image, exception);
CHECK_EXCEPTION();
DestroyExceptionInfo(exception);
if (!okay)
{
rb_raise(rb_eRuntimeError, "SetImageRegistry failed.");
}
RB_GC_GUARD(id_value);
}
/**
* Delete the temporary image from the registry.
*
* No Ruby usage (internal function)
*
* @param temp_name the name of temporary image in the registry.
*/
void
rm_delete_temp_image(char *temp_name)
{
MagickBooleanType okay = DeleteImageRegistry(temp_name+5);
if (!okay)
{
rb_warn("DeleteImageRegistry failed for `%s'", temp_name);
}
}
/**
* Raise NotImplementedError.
*
* No Ruby usage (internal function)
*
* Notes:
* - Called when a xMagick API is not available.
* - Replaces Ruby's rb_notimplement function.
*
* @throw NotImpError
*/
void
rm_not_implemented(void)
{
rb_raise(rb_eNotImpError, "the `%s' method is not supported by ImageMagick "
MagickLibVersionText, rb_id2name(rb_frame_this_func()));
}
/**
* Create a new ImageMagickError object and raise an exception.
*
* No Ruby usage (internal function)
*
* Notes:
* - This funky technique allows me to safely add additional information to
* the ImageMagickError object in both 1.6.8 and 1.8.0.
*
* @param msg the error mesage
* @throw ImageMagickError
* @see www.ruby_talk.org/36408.
*/
void
rm_magick_error(const char *msg)
{
VALUE exc, mesg;
mesg = rb_str_new2(msg);
exc = rb_funcall(Class_ImageMagickError, rm_ID_new, 2, mesg, Qnil);
rb_funcall(rb_cObject, rb_intern("raise"), 1, exc);
RB_GC_GUARD(exc);
RB_GC_GUARD(mesg);
}
#if defined(IMAGEMAGICK_7)
/**
* Sets the alpha channel of a pixel color
*
* No Ruby usage (internal function)
*
* @param pixel the Pixel
* @param value the value
*/
void
rm_set_pixelinfo_alpha(PixelInfo *pixel, const MagickRealType value)
{
pixel->alpha = value;
if (value != (MagickRealType) OpaqueAlpha)
{
pixel->alpha_trait = BlendPixelTrait;
}
}
#endif
/**
* Initialize a new ImageMagickError object - store the "loc" string in the
* magick_location instance variable.
*
* @overload initialize(msg, loc = nil)
* @param msg [String] the exception message
* @param loc [String] the location stored in the magick_location instance variable
* @return [Magick::ImageMagickError] self
*/
VALUE
ImageMagickError_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE super_argv[1] = {(VALUE)0};
int super_argc = 0;
VALUE extra = Qnil;
switch(argc)
{
case 2:
extra = argv[1];
case 1:
super_argv[0] = argv[0];
super_argc = 1;
case 0:
break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 2)", argc);
}
rb_call_super(super_argc, (const VALUE *)super_argv);
rb_iv_set(self, "@" MAGICK_LOC, extra);
RB_GC_GUARD(extra);
return self;
}
/**
* Backport GetImageProperty for pre-6.3.1 versions of ImageMagick.
*
* No Ruby usage (internal function)
*
* @param img the image
* @param property the property name
* @return the property value
*/
const char *
rm_get_property(const Image *img, const char *property)
{
#if defined(IMAGEMAGICK_7)
const char *result;
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
result = GetImageProperty(img, property, exception);
CHECK_EXCEPTION();
DestroyExceptionInfo(exception);
return result;
#else
return GetImageProperty(img, property);
#endif
}
/**
* Backport SetImageProperty for pre-6.3.1 versions of ImageMagick.
*
* No Ruby usage (internal function)
*
* @param image the image
* @param property the property name
* @param value the property value
* @return true if successful, otherwise false
*/
MagickBooleanType
rm_set_property(Image *image, const char *property, const char *value)
{
#if defined(IMAGEMAGICK_7)
ExceptionInfo *exception;
MagickBooleanType okay;
exception = AcquireExceptionInfo();
okay = SetImageProperty(image, property, value, exception);
CHECK_EXCEPTION();
DestroyExceptionInfo(exception);
return okay;
#else
return SetImageProperty(image, property, value);
#endif
}
/**
* If a "user" option is present in the Info, assign its value to a "user"
* artifact in each image.
*
* No Ruby usage (internal function)
*
* @param images a list of images
* @param info the info
*/
void rm_set_user_artifact(Image *images, Info *info)
{
const char *value;
value = GetImageOption(info, "user");
if (value)
{
Image *image;
image = GetFirstImageInList(images);
while (image)
{
SetImageArtifact(image, "user", value);
image = GetNextImageInList(image);
}
}
}
/**
* Collect optional method arguments via Magick::OptionalMethodArguments.
*
* No Ruby usage (internal function)
*
* Notes:
* - Creates an instance of Magick::OptionalMethodArguments, then yields to a
* block in the context of the instance.
*
* @param img the image
*/
void
rm_get_optional_arguments(VALUE img)
{
VALUE optional_method_arguments;
VALUE opt_args;
VALUE argv[1];
// opt_args = Magick::OptionalMethodArguments.new(img)
// opt_args.instance_eval { block }
if (rb_block_given_p())
{
optional_method_arguments = rb_const_get_from(Module_Magick, rb_intern("OptionalMethodArguments"));
argv[0] = img;
opt_args = rb_class_new_instance(1, argv, optional_method_arguments);
rb_yield(opt_args);
}
RB_GC_GUARD(optional_method_arguments);
RB_GC_GUARD(opt_args);
return;
}
/**
* Copy image options from the Info structure to the Image structure.
*
* No Ruby usage (internal function)
*
* @param image the Image structure to modify
* @param info the Info structure
*/
static void copy_options(Image *image, Info *info)
{
char property[MaxTextExtent];
const char *option;
ResetImageOptionIterator(info);
for (option = GetNextImageOption(info); option; option = GetNextImageOption(info))
{
const char *value;
value = GetImageOption(info, option);
if (value)
{
strlcpy(property, value, sizeof(property));
SetImageArtifact(image, property, value);
}
}
}
/**
* Propagate ImageInfo values to the Image
*
* No Ruby usage (internal function)
*
* @param image the Image structure to modify
* @param info the Info structure
* @see SyncImageSettings in mogrify.c in ImageMagick
*/
void rm_sync_image_options(Image *image, Info *info)
{
MagickStatusType flags;
GeometryInfo geometry_info;
const char *option;
#if defined(IMAGEMAGICK_7)
ExceptionInfo *exception;
#endif
// The option strings will be set only when their attribute values were
// set in the optional argument block.
option = GetImageOption(info, "background");
if (option)
{
image->background_color = info->background_color;
}
option = GetImageOption(info, "bordercolor");
if (option)
{
image->border_color = info->border_color;
}
if (info->colorspace != UndefinedColorspace)
{
#if defined(IMAGEMAGICK_7)
exception = AcquireExceptionInfo();
GVL_STRUCT_TYPE(TransformImageColorspace) args = { image, info->colorspace, exception };
CALL_FUNC_WITHOUT_GVL(GVL_FUNC(TransformImageColorspace), &args);
CHECK_EXCEPTION();
DestroyExceptionInfo(exception);
#else
GVL_STRUCT_TYPE(TransformImageColorspace) args = { image, info->colorspace };
CALL_FUNC_WITHOUT_GVL(GVL_FUNC(TransformImageColorspace), &args);
rm_check_image_exception(image, RetainOnError);
#endif
}
if (info->compression != UndefinedCompression)
{
image->compression = info->compression;
}
option = GetImageOption(info, "delay");
if (option)
{
image->delay = strtoul(option, NULL, 0);
}
if (info->density)
{
flags = ParseGeometry(info->density, &geometry_info);
#if defined(IMAGEMAGICK_7)
image->resolution.x = geometry_info.rho;
image->resolution.y = geometry_info.sigma;
#else
image->x_resolution = geometry_info.rho;
image->y_resolution = geometry_info.sigma;
#endif
if ((flags & SigmaValue) == 0)
{
#if defined(IMAGEMAGICK_7)
image->resolution.y = image->resolution.x;
#else
image->y_resolution = image->x_resolution;
#endif
}
}
if (info->depth != 0)
{
image->depth = info->depth;
}
option = GetImageOption(info, "dispose");
if (option)
{
image->dispose = rm_dispose_to_enum(option);
}
if (info->extract)
{
ParseAbsoluteGeometry(info->extract, &image->extract_info);
}
if (info->fuzz != 0.0)
{
image->fuzz = info->fuzz;
}
option = GetImageOption(info, "gravity");
if (option)
{
image->gravity = rm_gravity_to_enum(option);
}
if (info->interlace != NoInterlace)
{
image->interlace = info->interlace;
}
option = GetImageOption(info, "mattecolor");
if (option)
{
image->matte_color = info->matte_color;
}
if (info->orientation != UndefinedOrientation)
{
image->orientation = info->orientation;
}
if (info->page)
{
ParseAbsoluteGeometry(info->page, &image->page);
}
if (info->quality != 0UL)
{
image->quality = info->quality;
}
option = GetImageOption(info, "scene");
if (option)
{
image->scene = info->scene;
}
option = GetImageOption(info, "tile-offset");
if (option)
{
ParseAbsoluteGeometry(option, &image->tile_offset);
}
option = GetImageOption(info, "transparent");
if (option)
{
image->transparent_color = info->transparent_color;
}
if (info->type != UndefinedType)
{
image->type = info->type;
}
if (info->units != UndefinedResolution)
{
if (image->units != info->units)
{
switch (image->units)
{
case PixelsPerInchResolution:
{
if (info->units == PixelsPerCentimeterResolution)
{
#if defined(IMAGEMAGICK_7)
image->resolution.x /= 2.54;
image->resolution.y /= 2.54;
#else
image->x_resolution /= 2.54;
image->y_resolution /= 2.54;
#endif
}
break;
}
case PixelsPerCentimeterResolution:
{
if (info->units == PixelsPerInchResolution)
{
#if defined(IMAGEMAGICK_7)
image->resolution.x *= 2.54;
image->resolution.y *= 2.54;
#else
image->x_resolution *= 2.54;
image->y_resolution *= 2.54;
#endif
}
break;
}
default:
break;
}
}
image->units = info->units;
}
copy_options(image, info);
#if defined(IMAGEMAGICK_7)
// The value of info->compression is referenced when saving the image in ImageMagick 7.
info->compression = image->compression;
#endif
}
/**
* Replicate old (ImageMagick < 6.3.2) EXIF:* functionality using
* GetImageProperty by returning the exif entries as a single string, separated
* by \n's. Do this so that RMagick works no matter which version of
* ImageMagick is in use.
*
* No Ruby usage (internal function)
*
* @param image the image
* @return string representation of exif properties
* @see magick/identify.c in ImageMagick
*/
VALUE
rm_exif_by_entry(Image *image)
{
const char *property, *value;
char *str;
size_t len = 0, property_l, value_l;
VALUE v;
#if defined(IMAGEMAGICK_7)
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
GetImageProperty(image, "exif:*", exception);
CHECK_EXCEPTION();
#else
GetImageProperty(image, "exif:*");
#endif
ResetImagePropertyIterator(image);
property = GetNextImageProperty(image);
// Measure the exif properties and values
while (property)
{
// ignore properties that don't start with "exif:"
property_l = rm_strnlen_s(property, MaxTextExtent);
if (property_l > 5 && rm_strncasecmp(property, "exif:", 5) == 0)
{
if (len > 0)
{
len += 1; // there will be a \n between property=value entries
}
len += property_l - 5;
#if defined(IMAGEMAGICK_7)
value = GetImageProperty(image, property, exception);
CHECK_EXCEPTION();
#else
value = GetImageProperty(image, property);
#endif
if (value)
{
// add 1 for the = between property and value
len += 1 + rm_strnlen_s(value, MaxTextExtent);
}
}
property = GetNextImageProperty(image);
}
if (len == 0)
{
#if defined(IMAGEMAGICK_7)
DestroyExceptionInfo(exception);
#endif
return Qnil;
}
str = (char *)xmalloc(len);
len = 0;
// Copy the exif properties and values into the string.
ResetImagePropertyIterator(image);
property = GetNextImageProperty(image);
while (property)
{
property_l = rm_strnlen_s(property, MaxTextExtent);
if (property_l > 5 && rm_strncasecmp(property, "exif:", 5) == 0)
{
if (len > 0)
{
str[len++] = '\n';
}
memcpy(str+len, property+5, property_l-5);
len += property_l - 5;
#if defined(IMAGEMAGICK_7)
value = GetImageProperty(image, property, exception);
if (rm_should_raise_exception(exception, RetainExceptionRetention))
{
xfree(str);
rm_raise_exception(exception);
}
#else
value = GetImageProperty(image, property);
#endif
if (value)
{
value_l = rm_strnlen_s(value, MaxTextExtent);
str[len++] = '=';
memcpy(str+len, value, value_l);
len += value_l;
}
}
property = GetNextImageProperty(image);
}
#if defined(IMAGEMAGICK_7)
DestroyExceptionInfo(exception);
#endif
v = rb_str_new(str, len);
xfree(str);
RB_GC_GUARD(v);
return v;
}
/**
* Replicate old (ImageMagick < 6.3.2) EXIF:! functionality using
* GetImageProperty by returning the exif entries as a single string, separated
* by \n's. Do this so that RMagick works no matter which version of
* ImageMagick is in use.
*
* No Ruby usage (internal function)
*
* @param image the image
* @return string representation of exif properties
* @see magick/identify.c in ImageMagick
*/
VALUE
rm_exif_by_number(Image *image)
{
const char *property, *value;
char *str;
size_t len = 0, property_l, value_l;
VALUE v;
#if defined(IMAGEMAGICK_7)
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
GetImageProperty(image, "exif:!", exception);
CHECK_EXCEPTION();
#else
GetImageProperty(image, "exif:!");
#endif
ResetImagePropertyIterator(image);
property = GetNextImageProperty(image);
// Measure the exif properties and values
while (property)
{
// ignore properties that don't start with "#"
property_l = rm_strnlen_s(property, MaxTextExtent);
if (property_l > 1 && property[0] == '#')
{
if (len > 0)
{
len += 1; // there will be a \n between property=value entries
}
len += property_l;
#if defined(IMAGEMAGICK_7)
value = GetImageProperty(image, property, exception);
CHECK_EXCEPTION();
#else
value = GetImageProperty(image, property);
#endif
if (value)
{
// add 1 for the = between property and value
len += 1 + rm_strnlen_s(value, MaxTextExtent);
}
}
property = GetNextImageProperty(image);
}
if (len == 0)
{
#if defined(IMAGEMAGICK_7)
DestroyExceptionInfo(exception);
#endif
return Qnil;
}
str = (char *)xmalloc(len);
len = 0;
// Copy the exif properties and values into the string.
ResetImagePropertyIterator(image);
property = GetNextImageProperty(image);
while (property)
{
property_l = rm_strnlen_s(property, MaxTextExtent);
if (property_l > 1 && property[0] == '#')
{
if (len > 0)
{
str[len++] = '\n';
}
memcpy(str+len, property, property_l);
len += property_l;
#if defined(IMAGEMAGICK_7)
value = GetImageProperty(image, property, exception);
if (rm_should_raise_exception(exception, RetainExceptionRetention))
{
xfree(str);
rm_raise_exception(exception);
}
#else
value = GetImageProperty(image, property);
#endif
if (value)
{
value_l = rm_strnlen_s(value, MaxTextExtent);
str[len++] = '=';
memcpy(str+len, value, value_l);
len += value_l;
}
}
property = GetNextImageProperty(image);
}
#if defined(IMAGEMAGICK_7)
DestroyExceptionInfo(exception);
#endif
v = rb_str_new(str, len);
xfree(str);
RB_GC_GUARD(v);
return v;
}
/**
* Clone an image, handle errors.
*
* No Ruby usage (internal function)
*
* Notes:
* - Don't trace creation - the clone may not be used as an Image object. Let
* the caller do the trace if desired.
*
* @param image the image to clone
* @return the cloned image
*/
Image *
rm_clone_image(Image *image)
{
Image *clone;
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
GVL_STRUCT_TYPE(CloneImage) args = { image, 0, 0, MagickTrue, exception };
clone = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(CloneImage), &args);
if (!clone)
{
rb_raise(rb_eNoMemError, "not enough memory to continue");
}
rm_check_exception(exception, clone, DestroyOnError);
DestroyExceptionInfo(exception);
return clone;
}
/**
* Remove the ImageMagick links between images in an scene sequence.
*
* No Ruby usage (internal function)
*
* Notes:
* - The images remain grouped via the ImageList
*
* @param image the image
*/
void
rm_split(Image *image)
{
if (!image)
{
rb_bug("RMagick FATAL: split called with NULL argument.");
}
while (image)
{
RemoveFirstImageFromList(&image);
}
}
#if defined(IMAGEMAGICK_6)
/**
* If an ExceptionInfo struct in a list of images indicates a warning, issue a
* warning message. If an ExceptionInfo struct indicates an error, raise an
* exception and optionally destroy the images.
*
* No Ruby usage (internal function)
*
* @param imglist the list of images
* @param retention retention strategy in case of an error (either RetainOnError
* or DestroyOnError)
*/
void
rm_check_image_exception(Image *imglist, ErrorRetention retention)
{
ExceptionInfo *exception;
Image *badboy = NULL;
Image *image;
if (imglist == NULL)
{
return;
}
exception = AcquireExceptionInfo();
// Find the image with the highest severity
image = GetFirstImageInList(imglist);
while (image)
{
if (image->exception.severity != UndefinedException)
{
if (!badboy || image->exception.severity > badboy->exception.severity)
{
badboy = image;
InheritException(exception, &badboy->exception);
}
ClearMagickException(&image->exception);
}
image = GetNextImageInList(image);
}
if (badboy)
{
rm_check_exception(exception, imglist, retention);
}
DestroyExceptionInfo(exception);
}
#endif
#define ERROR_MSG_SIZE 1024
/**
* Formats the exception into the message buffer
*
* No Ruby usage (internal function)
*
* @param severity information about the severity of the error
* @param reason the reason for the error
* @param description description of the error
* @param msg the buffer where the exception message should be formated in
*/
static void
format_exception(const ExceptionType severity, const char *reason, const char *description, char *msg)
{
int len;
memset(msg, 0, ERROR_MSG_SIZE);
len = snprintf(msg, ERROR_MSG_SIZE, "%s%s%s",
GetLocaleExceptionMessage(severity, reason),
description ? ": " : "",
description ? GetLocaleExceptionMessage(severity, description) : "");
msg[len] = '\0';
}
/**
* Call handle_exception if there is an exception to handle.
*
* No Ruby usage (internal function)
*
* @param exception information about the exception
* @param imglist the images that caused the exception
* @param retention retention strategy in case of an error (either RetainOnError
* or DestroyOnError)
*/
void
rm_check_exception(ExceptionInfo *exception, Image *imglist, ErrorRetention retention)
{
if (exception->severity == UndefinedException)
{
return;
}
handle_exception(exception, imglist, retention);
}
/**
* Called from ImageMagick for a warning.
*
* No Ruby usage (internal function)
*
* @param severity information about the severity of the warning (ignored)
* @param reason the reason for the warning
* @param description description of the warning
*/
void
rm_warning_handler(const ExceptionType severity, const char *reason, const char *description)
{
rb_warning("RMagick: %s%s%s",
GetLocaleExceptionMessage(severity, reason),
description ? ": " : "",
description ? GetLocaleExceptionMessage(severity, description) : "");
}
/**
* Called from ImageMagick for a error.
*
* No Ruby usage (internal function)
*
* @param severity information about the severity of the error
* @param reason the reason for the error
* @param description description of the error
*/
void
rm_error_handler(const ExceptionType severity, const char *reason, const char *description)
{
char msg[ERROR_MSG_SIZE];
format_exception(severity, reason, description, msg);
rm_magick_error(msg);
}
/**
* Called from ImageMagick for a fatal error.
*
* No Ruby usage (internal function)
*
* @param severity information about the severity of the error
* @param reason the reason for the error
* @param description description of the error
* @throw FatalImageMagickError
*/
void
rm_fatal_error_handler(const ExceptionType severity, const char *reason, const char *description)
{
rb_raise(Class_FatalImageMagickError, "%s%s%s",
GetLocaleExceptionMessage(severity, reason),
description ? ": " : "",
description ? GetLocaleExceptionMessage(severity, description) : "");
}
/**
* Called when rm_check_exception determines that we need to either issue a
* warning message or raise an exception. This function allocates a bunch of
* stack so we don't call it unless we have to.
*
* No Ruby usage (internal function)
*
* @param exception information about the exception
* @param imglist the images that caused the exception
* @param retention retention strategy in case of an error (either RetainOnError
* or DestroyOnError)
*/
static void
handle_exception(ExceptionInfo *exception, Image *imglist, ErrorRetention retention)
{
char msg[ERROR_MSG_SIZE];
// Handle simple warning
if (exception->severity < ErrorException)
{
rm_warning_handler(exception->severity, exception->reason, exception->description);
// Caller deletes ExceptionInfo...
return;
}
// Raise an exception. We're not coming back...
// Newly-created images should be destroyed, images that are part
// of image objects should be retained but split.
if (imglist)
{
if (retention == DestroyOnError)
{
DestroyImageList(imglist);
imglist = NULL;
}
else
{
rm_split(imglist);
}
}
format_exception(exception->severity, exception->reason, exception->description, msg);
DestroyExceptionInfo(exception);
rm_magick_error(msg);
}
/**
* RMagick expected a result. If it got NULL instead raise an exception.
*
* No Ruby usage (internal function)
*
* @param image the expected result
* @throw RuntimeError
*/
void
rm_ensure_result(Image *image)
{
if (!image)
{
rb_raise(rb_eRuntimeError, MagickPackageName " library function failed to return a result.");
}
}
/**
* Checks if an error should be raised for the exception.
*
* No Ruby usage (internal function)
*
* @param exception information about the exception
* @param retention retention strategy for the exception in case there was no error
*/
MagickBooleanType
rm_should_raise_exception(ExceptionInfo *exception, const ExceptionRetention retention)
{
if (exception->severity < ErrorException)
{
if (exception->severity != UndefinedException)
{
rm_warning_handler(exception->severity, exception->reason, exception->description);
}
if (retention == DestroyExceptionRetention)
{
DestroyExceptionInfo(exception);
}
return MagickFalse;
}
return MagickTrue;
}
/**
* Raises an exception.
*
* No Ruby usage (internal function)
*
* @param exception information about the exception
*/
void
rm_raise_exception(ExceptionInfo *exception)
{
char msg[ERROR_MSG_SIZE];
format_exception(exception->severity, exception->reason, exception->description, msg);
DestroyExceptionInfo(exception);
rm_magick_error(msg);
}
/**
* Get IO path.
*
* No Ruby usage (internal function)
*
* @param io An IO object
* @return string of the path
*/
VALUE
rm_io_path(VALUE io)
{
#ifdef HAVE_RB_IO_PATH
return rb_io_path(io);
#else
rb_io_t *fptr;
GetOpenFile(io, fptr);
return fptr->pathv;
#endif
}
|