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
|
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*-
*
* Pigment media rendering library
*
* Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author(s): Loïc Molinari <loic@fluendo.com>
* Julien Moutte <julien@fluendo.com>
*/
/**
* SECTION:pgmimage
* @short_description: An image drawable supporting various media.
* @see_also: #PgmDrawable, #PgmText, #PgmCanvas.
*
* <refsect2>
* <para>
* #PgmImage is a drawable displaying media. It supports various ways of loading
* images through buffers, file paths or videos through GStreamer.
* </para>
* <title>Loading image data</title>
* <para>
* Image data loading can happen with three different functions:
* <itemizedlist>
* <listitem>
* <para>
* pgm_image_set_from_buffer() takes a pre-loaded data buffer and sets it as
* the currently displayed image. This is useful when you want to use an image
* loading library (GdkPixbuf, FreeImage, etc) in your application and just
* provide the pixels to #PgmImage for display. The data buffer containing the
* pixels is copied internally, you can free the data buffer from the
* application side as soon as the function returns.
* </para>
* </listitem>
* <listitem>
* <para>
* pgm_image_set_from_gst_buffer() takes a GStreamer #GstBuffer and sets it as
* the currently displayed image. This is mostly used to do video rendering.
* There's no copying of the buffer data to optimize performances, indeed the
* reference count of the buffer is going to be increased to keep the buffer
* around while it's needed for rendering. When you call pgm_image_clear() the
* reference to the buffer will be decreased and the buffer can get freed. Note
* that this method is used by #PgmSink to render video frames directly in a
* #PgmImage when the pipeline is playing.
* </para>
* </listitem>
* <listitem>
* <para>
* pgm_image_set_from_file() takes a path to an image file delegating image
* loading to Pigment. Thus the loading is asynchronous and won't block the
* Pigment main loop.
* </para>
* </listitem>
* </itemizedlist>
* </para>
* <title>Sharing image data between #PgmImage objects</title>
* <para>
* pgm_image_set_from_image() is a convenient system to slave an image to
* another one. Indeed you might want to load an image data once and then use
* it in multiple image objects. In that case this image becomes a slave to the
* one that has the image data loaded internally and each time it needs to draw
* it will use that data.
* </para>
* <para>
* Layout settings of the drawable are independent from one image to another.
* That means that even if two image objects are using the same image, they can
* have different colors, different #PgmDrawableLayoutType or different
* #PgmDrawableAlignment.
* </para>
* <para>
* Each time a new image data buffer is loaded in the master image object, all
* the slave image objects are automatically updated. That means you can render
* a video clip in ten different drawables without doing anything else than
* slaving nine image objects to the one that's receiving the image data.
* </para>
* <title>Image data aspect ratio</title>
* <para>
* This rarely happens with normal images but video rendering often has non
* square pixels video frames coming out of the video decoders (DVD, DV cameras,
* etc). In that case a calculation has to be done when projecting to the
* viewport so that we put in adequation both the pixel aspect ratio and the
* source video aspect ratio. You can set the image aspect ratio using
* pgm_image_set_aspect_ratio() and be assured that Pigment is going to render
* that image correctly on the viewport.
* </para>
* <title>Benefitting from hardware acceleration</title>
* <para>
* Depending on the viewport implementation, some #PgmImagePixelFormat (color
* space) can be supported or not. When it comes to video rendering, hardware
* acceleration is very important and you need to know what kind of pixel
* formats are convenient for the rendering backend. you can get a list of
* supported (accelerated) pixel formats using
* pgm_viewport_get_pixel_formats().
* </para>
* </refsect2>
*
* Last reviewed on 2007-11-08 (0.3.2)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <glib/gstdio.h> /* g_fopen */
#include "pgmimage.h"
#include "pgmmarshal.h"
/* Number of bytes read at each callback */
#define ASYNC_LOADER_CHUNK_SIZE 1024
GST_DEBUG_CATEGORY_STATIC (pgm_image_debug);
#define GST_CAT_DEFAULT pgm_image_debug
/* Image signals */
enum {
PIXBUF_LOADED,
LAST_SIGNAL
};
/* Asynchronous pixbuf loader */
typedef struct {
PgmImage *image; /* Image object */
FILE *stream; /* File handle */
GdkPixbufLoader *loader; /* GDK Pixbuf loader */
GMutex *lock; /* Access lock: avoid holding both this and the
image lock. Only exception: you must have the
image lock held when calling
async_loader_free().
*/
gboolean loaded; /* Is the loading finished? */
guint max_size; /* Scaling threshold */
guint source; /* Pigment main loop source id */
} AsyncLoader;
static PgmDrawableClass *parent_class = NULL;
static guint pgm_image_signals[LAST_SIGNAL] = { 0 };
static void async_loader_free (AsyncLoader *async);
/* Private methods */
/* Update the ratio of the slaves.
* Called with Image LOCK. */
static void
update_slaves_ratio (PgmImage *image)
{
PgmImage *slave;
GList *walk;
walk = image->slaves;
while (walk)
{
slave = walk->data;
slave->par_n = image->par_n;
slave->par_d = image->par_d;
walk = walk->next;
}
}
/* Do the actual clearing of image.
* Called with Image LOCK. */
static PgmError
do_clear (PgmImage *image)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
switch (image->storage_type)
{
case PGM_IMAGE_BUFFER:
GST_DEBUG_OBJECT (image, "Clearing buffer image");
g_free (image->data.buffer.buffer);
break;
case PGM_IMAGE_GST_BUFFER:
GST_DEBUG_OBJECT (image, "Clearing GstBuffer image");
gst_buffer_unref (image->data.gst_buffer.gst_buffer);
break;
case PGM_IMAGE_PIXBUF:
GST_DEBUG_OBJECT (image, "Clearing GdkPixbuf image");
gdk_pixbuf_unref ((GdkPixbuf *) (image->data.pixbuf.pixbuf));
break;
case PGM_IMAGE_SYSTEM_BUFFER:
GST_DEBUG_OBJECT (image, "Clearing system buffer image");
image->data.system_buffer.system_buffer = NULL;
break;
case PGM_IMAGE_IMAGE:
GST_DEBUG_OBJECT (image, "Clearing slaved image");
if (image->master)
{
/* Remove ourself from the master's slave list. */
GST_OBJECT_LOCK (image->master);
image->master->slaves = g_list_remove (image->master->slaves, image);
GST_OBJECT_UNLOCK (image->master);
image->master = NULL;
}
break;
default:
break;
}
image->storage_type = PGM_IMAGE_EMPTY;
image->par_n = 0;
image->par_d = 1;
update_slaves_ratio (image);
return PGM_ERROR_OK;
}
/* Asynchronous loader chunk reader callback */
static gint
async_loader_read_chunk_cb (gpointer data)
{
AsyncLoader *async = (AsyncLoader*) data;
PgmImage *image = async->image;
gchar buffer[ASYNC_LOADER_CHUNK_SIZE];
gsize bytes_read;
GdkPixbuf *pixbuf;
/* we want to avoid a disposal of the image between here and when we use it */
gst_object_ref (image);
g_mutex_lock (async->lock);
/* the source may have been destroyed while we were waiting for the lock (e.g.
* by async_loader_free() which holds that lock) */
if (g_source_is_destroyed (g_main_current_source ()))
{
if (async->lock)
g_mutex_unlock (async->lock);
gst_object_unref (image);
return FALSE;
}
/* Read the chunk */
bytes_read = fread (buffer, 1, ASYNC_LOADER_CHUNK_SIZE, async->stream);
if (ferror (async->stream))
{
GST_WARNING_OBJECT (image, "cannot read from the file: %s",
g_strerror (errno));
goto error;
}
/* Write the chunk */
if (!gdk_pixbuf_loader_write (async->loader, (const guchar*) buffer,
bytes_read, NULL))
{
GST_WARNING_OBJECT (image, "GdkPixbufLoader cannot parse the buffer");
goto error;
}
/* Image data not fully loaded */
if (!feof (async->stream))
{
g_mutex_unlock (async->lock);
gst_object_unref (image);
return TRUE;
}
/* Image data fully loaded */
else
{
/* Closing makes the loader aware that its got the full file: without this
* it's buggy with small files */
gdk_pixbuf_loader_close (async->loader, NULL);
/* Get the pixbuf */
pixbuf = gdk_pixbuf_ref (gdk_pixbuf_loader_get_pixbuf (async->loader));
if (!pixbuf)
{
GST_WARNING_OBJECT (image, "GdkPixbufLoader cannot get the pixbuf");
goto error;
}
/* Now we don't need to access the async loader anymore, but we want to
* get a lock on the image, which we should not do with the async loader
* lock held */
g_mutex_unlock (async->lock);
/* Clean up the previous data */
pgm_image_clear (image);
GST_OBJECT_LOCK (image);
/* Fill the new storage informations */
image->storage_type = PGM_IMAGE_PIXBUF;
image->par_n = gdk_pixbuf_get_width (pixbuf);
image->par_d = gdk_pixbuf_get_height (pixbuf);
image->data.pixbuf.pixbuf = pixbuf;
update_slaves_ratio (image);
GST_OBJECT_UNLOCK (image);
/* And emit signals */
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_PIXBUF);
g_signal_emit (G_OBJECT (image), pgm_image_signals[PIXBUF_LOADED], 0);
/* We don't need the loader anymore */
GST_OBJECT_LOCK (image);
/* we free image->async_loader and not async since it may have been freed
* and NULL-ified since we unlocked it */
async_loader_free (image->async_loader);
image->async_loader = NULL;
GST_OBJECT_UNLOCK (image);
gst_object_unref (image);
return FALSE;
}
error:
fclose (async->stream);
async->stream = NULL;
gdk_pixbuf_loader_close (async->loader, NULL);
g_object_unref (async->loader);
async->loader = NULL;
async->loaded = TRUE;
g_mutex_unlock (async->lock);
gst_object_unref (image);
return FALSE;
}
/* GdkPixbufLoader handler called whenever loader's figured out the size */
static void
async_loader_size_prepared_cb (GdkPixbufLoader *loader,
gint width,
gint height,
gpointer data)
{
AsyncLoader *async = (AsyncLoader*) data;
gint max_size = (gint) async->max_size;
/* Request a scaling if the width or the height exceeds the maximum size */
if (width > max_size || height > max_size)
{
if (width >= height)
{
height = height * max_size / width;
width = max_size;
}
else
{
width = width * max_size / height;
height = max_size;
}
gdk_pixbuf_loader_set_size (loader, width, height);
}
}
/* Asynchronous loader creation */
static AsyncLoader*
async_loader_new (PgmImage *image,
FILE *stream,
guint max_size)
{
AsyncLoader *async;
async = g_slice_new0 (AsyncLoader);
if (G_UNLIKELY (!async))
{
GST_WARNING_OBJECT (image, "cannot create the asynchronous image loader");
return NULL;
}
async->image = image;
async->stream = stream;
async->max_size = max_size;
async->loaded = FALSE;
async->lock = g_mutex_new ();
async->loader = gdk_pixbuf_loader_new ();
/* Connect to 'size_prepared' signal to adapt image size as requested */
if (max_size != 0)
g_signal_connect (async->loader, "size_prepared",
G_CALLBACK (async_loader_size_prepared_cb), async);
/* Add the progressive loading source to the main Pigment loop */
async->source = g_idle_add (async_loader_read_chunk_cb, async);
return async;
}
/* Asynchronous loader destruction.
* Note: This function should be called with the lock on the corresponding image
* held.
*/
static void
async_loader_free (AsyncLoader *async)
{
if (async == NULL)
return;
if (!async->loaded)
{
g_source_remove (async->source);
/* from this point on, async_loader_read_chunk_cb() cannot be called, we
* lock in case it has been called before the source removal. */
g_mutex_lock (async->lock);
g_mutex_unlock (async->lock);
fclose (async->stream);
async->stream = NULL;
gdk_pixbuf_loader_close (async->loader, NULL);
g_object_unref (async->loader);
async->loader = NULL;
}
g_mutex_free (async->lock);
async->lock = NULL;
g_slice_free (AsyncLoader, async);
}
/* Gets the width and height in pixel of the original image buffer.
* Called with LOCK. */
static void
image_get_buffer_pixel_size (PgmImage *image,
guint *width,
guint *height)
{
switch (image->storage_type)
{
case PGM_IMAGE_BUFFER:
*width = image->data.buffer.width;
*height = image->data.buffer.height;
break;
case PGM_IMAGE_GST_BUFFER:
*width = image->data.gst_buffer.width;
*height = image->data.gst_buffer.height;
break;
case PGM_IMAGE_PIXBUF:
*width = gdk_pixbuf_get_width (image->data.pixbuf.pixbuf);
*height = gdk_pixbuf_get_height (image->data.pixbuf.pixbuf);
break;
case PGM_IMAGE_IMAGE:
image_get_buffer_pixel_size (image->master, width, height);
break;
case PGM_IMAGE_SYSTEM_BUFFER:
*width = image->data.system_buffer.width;
*height = image->data.system_buffer.height;
break;
default:
*width = *height = 0;
}
}
/* Gets the position of the upper left corner of the image in drawable
* coordinates (in x_offset and y_offset), and the resolution of the image, in
* pixel per canvas unit.
* Called with LOCK. */
static void
image_get_offset_and_resolution (PgmImage *image,
gfloat *x_offset,
gfloat *y_offset,
gfloat *x_resolution,
gfloat *y_resolution)
{
PgmDrawable *drawable = PGM_DRAWABLE (image);
guint image_width, image_height;
gboolean image_wider, fill_height;
gfloat image_aspect_ratio;
image_get_buffer_pixel_size (image, &image_width, &image_height);
if (image->layout == PGM_IMAGE_FILLED)
{
*x_offset = 0;
*y_offset = 0;
*x_resolution = image_width / drawable->width;
*y_resolution = image_height / drawable->height;
return;
}
image_aspect_ratio = ((gfloat) image->par_n) / image->par_d;
image_wider = image_aspect_ratio > drawable->width / drawable->height;
fill_height = (image->layout == PGM_IMAGE_SCALED && !image_wider)
|| (image->layout == PGM_IMAGE_ZOOMED && image_wider);
/* resolution */
if (fill_height)
{
*x_resolution = image_width / (image_aspect_ratio * drawable->height);
*y_resolution = image_height / drawable->height;
}
else /* fill width */
{
*x_resolution = image_width / drawable->width;
*y_resolution = image_height * image_aspect_ratio / drawable->width;
}
/* x_offset */
if (!fill_height || image->align & PGM_IMAGE_LEFT)
*x_offset = 0;
else if (image->align & PGM_IMAGE_RIGHT)
*x_offset = drawable->width - image_width / *x_resolution;
else /* PGM_IMAGE_CENTER */
*x_offset = 0.5f * (drawable->width - image_width / *x_resolution);
/* y_offset */
if (fill_height || image->align & PGM_IMAGE_TOP)
*y_offset = 0;
else if (image->align & PGM_IMAGE_BOTTOM)
*y_offset = drawable->height - image_height / *y_resolution;
else /* PGM_IMAGE_CENTER */
*y_offset = 0.5f * (drawable->height - image_height / *y_resolution);
}
/* GObject stuff */
G_DEFINE_TYPE (PgmImage, pgm_image, PGM_TYPE_DRAWABLE);
static void
pgm_image_dispose (GObject *object)
{
PgmImage *image = PGM_IMAGE (object);
GST_OBJECT_LOCK (image);
async_loader_free (image->async_loader);
image->async_loader = NULL;
GST_OBJECT_UNLOCK (image);
/* Image is a master */
if (image->slaves)
{
GList *copy = NULL, *walk = NULL;
/* We need a copy of the slaves list since it gets modified in
* pgm_image_clear */
copy = g_list_copy (image->slaves);
for (walk = copy; walk; walk = walk->next)
pgm_image_clear (walk->data);
g_list_free (copy);
if (image->slaves)
GST_DEBUG_OBJECT (image, "Slave list not completely cleared");
}
GST_OBJECT_LOCK (image);
do_clear (image);
GST_OBJECT_UNLOCK (image);
pgm_mat4x4_free (image->mapping_matrix);
image->mapping_matrix = NULL;
GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
}
static void
pgm_image_class_init (PgmImageClass *klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
parent_class = g_type_class_peek_parent (klass);
/**
* PgmImage::pixbuf-loaded:
* @image: the #PgmImage
*
* Will be emitted after @image has finished to load its data from the file
* path given in the pgm_image_set_from_file() method.
*/
pgm_image_signals[PIXBUF_LOADED] =
g_signal_new ("pixbuf-loaded", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (PgmImageClass, pixbuf_loaded),
NULL, NULL, pgm_marshal_VOID__VOID, G_TYPE_NONE, 0);
gobject_class->dispose = GST_DEBUG_FUNCPTR (pgm_image_dispose);
GST_DEBUG_CATEGORY_INIT (pgm_image_debug, "pgm_image", 0,
"Pigment Image object");
}
static void
pgm_image_init (PgmImage *image)
{
image->mapping_matrix = pgm_mat4x4_new_identity ();
image->storage_type = PGM_IMAGE_EMPTY;
image->par_n = 0;
image->par_d = 1;
image->align = PGM_IMAGE_CENTER;
image->layout = PGM_IMAGE_SCALED;
image->interp = PGM_IMAGE_BILINEAR;
image->master = NULL;
image->slaves = NULL;
image->border_width = 0.0f;
image->border_inner_r = 255;
image->border_inner_g = 255;
image->border_inner_b = 255;
image->border_inner_a = 255;
image->border_outer_r = 255;
image->border_outer_g = 255;
image->border_outer_b = 255;
image->border_outer_a = 255;
image->async_loader = NULL;
}
/* public methods */
/**
* pgm_image_new:
*
* Creates a new #PgmImage instance.
*
* MT safe.
*
* Returns: a new #PgmImage instance.
*/
PgmDrawable *
pgm_image_new (void)
{
PgmImage *image;
image = g_object_new (PGM_TYPE_IMAGE, NULL);
GST_DEBUG_OBJECT (image, "created new image");
return PGM_DRAWABLE (image);
}
/**
* pgm_image_new_from_file:
* @filename: the filename.
* @max_size: the maximum size of the image in pixels before loading it in the
* #PgmImage or 0 to not constrain the size.
*
* Creates a new #PgmImage instance loading progressively an image from the
* given @filename. It optionally pre-scales the image so that it has a maximum
* width and height of @max_size.
*
* MT safe.
*
* Returns: a new #PgmImage instance.
*/
PgmDrawable *
pgm_image_new_from_file (const gchar *filename,
guint max_size)
{
PgmImage *image;
PgmError ret = PGM_ERROR_OK;
image = g_object_new (PGM_TYPE_IMAGE, NULL);
GST_DEBUG_OBJECT (image, "created new image from file");
ret = pgm_image_set_from_file (image, filename, max_size);
if (PGM_ERROR_OK != ret)
{
gst_object_unref (image);
return NULL;
}
return PGM_DRAWABLE (image);
}
/**
* pgm_image_new_from_buffer:
* @format: the pixel format of the buffer.
* @width: the image width in pixels.
* @height: the image height in pixels.
* @stride: the image rowstride in bytes (number of bytes per line).
* @size: the buffer size in bytes.
* @data: a pointer to the data buffer.
*
* Creates a new #PgmImage instance with the image from the given buffer.
*
* MT safe.
*
* Returns: a new #PgmImage instance.
*/
PgmDrawable *
pgm_image_new_from_buffer (PgmImagePixelFormat format,
guint width,
guint height,
guint stride,
guint size,
gconstpointer data)
{
PgmImage *image;
image = g_object_new (PGM_TYPE_IMAGE, NULL);
GST_DEBUG_OBJECT (image, "created new image");
pgm_image_set_from_buffer (image, format, width, height, stride, size, data);
return PGM_DRAWABLE (image);
}
/**
* pgm_image_new_from_image:
* @src_image: a #PgmImage which will be used as the master image.
*
* Creates a new #PgmImage instance with an image slaved from the
* image of @src_image.
*
* MT safe.
*
* Returns: a new #PgmImage instance.
*/
PgmDrawable *
pgm_image_new_from_image (PgmImage *src_image)
{
PgmImage *image;
image = g_object_new (PGM_TYPE_IMAGE, NULL);
GST_DEBUG_OBJECT (image, "created new image");
pgm_image_set_from_image (image, src_image);
return PGM_DRAWABLE (image);
}
/**
* pgm_image_set_from_file:
* @image: a #PgmImage object.
* @filename: a filename.
* @max_size: the maximum size of the image in pixels before loading it in the
* #PgmImage or 0 to not constrain the size.
*
* Loads an image from the file @filename. It optionally pre-scales the image so
* it has a maximum width and height of @max_size.
*
* This function is meant to be asynchronous, it loads the image by small chunks
* of 1024 bytes using an idle source in the Pigment mainloop. The consequence
* being that the storage type of @image doesn't change immediately, but only
* once the whole image is loaded. You can connect a callback to the
* <link linkend="PgmImage-pixbuf-loaded">pixbuf-loaded</link> signal to know when
* the loading is done.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_from_file (PgmImage *image,
const gchar *filename,
guint max_size)
{
FILE *stream;
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
/* Get a file handle from the filename */
stream = g_fopen (filename, "rb");
if (!stream)
{
GST_WARNING_OBJECT (image, "cannot open %s: %s", filename,
g_strerror (errno));
return PGM_ERROR_X;
}
/* Free the current asynchronous loader and create the new one */
GST_OBJECT_LOCK (image);
async_loader_free ((AsyncLoader*) image->async_loader);
image->async_loader = (gpointer) async_loader_new (image, stream, max_size);
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_from_buffer:
* @image: a #PgmImage object.
* @format: the pixel format of the buffer.
* @width: the image width in pixels.
* @height: the image height in pixels.
* @stride: the rowstride of the image in bytes (number of bytes per line).
* @size: the buffer size in bytes.
* @data: a pointer to the data buffer.
*
* Loads an image in @image from an existing buffer using pixel format @format.
* If you don't know the rowstride of the image you can set stride to 0. @data
* is copied internally you can free it right after the function call returns.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_from_buffer (PgmImage *image,
PgmImagePixelFormat format,
guint width,
guint height,
guint stride,
guint size,
gconstpointer data)
{
gpointer _data = NULL;
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (data != NULL, PGM_ERROR_X);
/* Let's set the storage data */
GST_OBJECT_LOCK (image);
/* The buffer sent is not the first of this size, it's not needed to call
* a clear, just free the buffer. */
if (G_LIKELY (image->storage_type == PGM_IMAGE_BUFFER
&& image->data.buffer.width == width
&& image->data.buffer.height == height
&& image->data.buffer.format == format
&& image->data.buffer.stride == stride))
{
if (G_LIKELY (image->data.buffer.buffer))
g_free (image->data.buffer.buffer);
}
/* It's the first */
else
{
/* Let's clear if needed */
if (image->storage_type != PGM_IMAGE_EMPTY)
{
do_clear (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_EMPTY);
GST_OBJECT_LOCK (image);
}
/* Store buffer informations */
image->storage_type = PGM_IMAGE_BUFFER;
image->data.buffer.format = format;
image->data.buffer.width = width;
image->data.buffer.height = height;
image->data.buffer.stride = stride;
image->data.buffer.size = size;
/* Store ratio */
image->par_n = width;
image->par_d = height;
update_slaves_ratio (image);
}
/* Try to copy the given buffer */
_data = g_memdup (data, size);
image->data.buffer.buffer = (guint8 *) _data;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_BUFFER);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_from_gst_buffer:
* @image: a #PgmImage object.
* @format: the pixel format of the buffer.
* @width: the image width in pixels.
* @height: the image height in pixels.
* @stride: the rowstride of the image in bytes (number of bytes per line).
* @buffer: A #GstBuffer reference containing the video frame.
*
* Loads an image in @image from an existing #GstBuffer using the pixel format
* @format. If you don't know the rowstride of the image you can set stride
* to 0. @buffer will have its reference count increased by 1 and will not get
* freed until the drawable gets cleaned up or that a new buffer is loaded.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_from_gst_buffer (PgmImage *image,
PgmImagePixelFormat format,
guint width,
guint height,
guint stride,
GstBuffer *buffer)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (GST_IS_BUFFER (buffer), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
/* The GstBuffer sent is not the first, it's not needed to call a clear,
* just unref the GstBuffer. I mean that the implementation should not be
* signaled to clear its allocated size/format for the stream of buffers
* since it's really heavy to clear for each new one. */
if (G_LIKELY (image->storage_type == PGM_IMAGE_GST_BUFFER
&& image->data.gst_buffer.width == width
&& image->data.gst_buffer.height == height
&& image->data.gst_buffer.format == format
&& image->data.gst_buffer.stride == stride))
{
if (G_LIKELY (image->data.gst_buffer.gst_buffer))
gst_buffer_unref (image->data.gst_buffer.gst_buffer);
}
/* It's the first */
else
{
/* Let's clear if needed */
if (image->storage_type != PGM_IMAGE_EMPTY)
{
do_clear (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_EMPTY);
GST_OBJECT_LOCK (image);
}
/* Store buffer informations */
image->storage_type = PGM_IMAGE_GST_BUFFER;
image->data.gst_buffer.format = format;
image->data.gst_buffer.width = width;
image->data.gst_buffer.height = height;
image->data.gst_buffer.stride = stride;
/* Store ratio */
image->par_n = width;
image->par_d = height;
update_slaves_ratio (image);
}
/* Take a ref on the GstBuffer */
image->data.gst_buffer.gst_buffer = gst_buffer_ref (buffer);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_GST_BUFFER);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_from_system_buffer:
* @image: a #PgmImage object.
* @format: the pixel format of the buffer.
* @width: the image width in pixels.
* @height: the image height in pixels.
* @system_buffer: a pointer to the system buffer.
*
* Loads an image in @image from an existing system buffer.
*
* A system buffer depends on the platform, for instance on UNIX with an X11
* server running, @system_buffer can be set to an X11 Pixmap.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_from_system_buffer (PgmImage *image,
PgmImagePixelFormat format,
guint width,
guint height,
gconstpointer system_buffer)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (system_buffer != NULL, PGM_ERROR_X);
/* Let's set the storage data */
GST_OBJECT_LOCK (image);
/* Let's clear if needed */
if (image->storage_type != PGM_IMAGE_EMPTY)
{
do_clear (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_EMPTY);
GST_OBJECT_LOCK (image);
}
/* Store buffer informations */
image->storage_type = PGM_IMAGE_SYSTEM_BUFFER;
image->data.system_buffer.format = format;
image->data.system_buffer.width = width;
image->data.system_buffer.height = height;
image->data.system_buffer.system_buffer = system_buffer;
/* Store ratio */
image->par_n = width;
image->par_d = height;
update_slaves_ratio (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image),
PGM_IMAGE_DATA_SYSTEM_BUFFER);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_from_image:
* @image: a #PgmImage object.
* @src_image: the source #PgmImage object to use as a master.
*
* Slaves @image to @src_image. Every change to @src_image is reflected on
* @image until you remove @image from the canvas or you call pgm_image_clear()
* on @image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_from_image (PgmImage *image,
PgmImage *src_image)
{
PgmError ret = PGM_ERROR_OK;
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (PGM_IS_IMAGE (src_image), PGM_ERROR_X);
GST_DEBUG_OBJECT (image, "using image from %s", GST_OBJECT_NAME (src_image));
GST_OBJECT_LOCK (image);
/* Trying to deadlock us? :) */
if (G_UNLIKELY (image == src_image || image == src_image->master))
{
GST_WARNING_OBJECT (image, "trying to do a master/slave loop!");
GST_OBJECT_UNLOCK (image);
ret = PGM_ERROR_X;
goto beach;
}
do_clear (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_EMPTY);
GST_OBJECT_LOCK (image);
image->storage_type = PGM_IMAGE_IMAGE;
GST_OBJECT_LOCK (src_image);
/* The master requested is already a slave */
if (G_UNLIKELY (src_image->master))
{
GST_DEBUG_OBJECT (image, "%s is already a slave to %s, using its master",
GST_OBJECT_NAME (src_image),
GST_OBJECT_NAME (src_image->master));
GST_OBJECT_LOCK (src_image->master);
src_image->master->slaves = g_list_append (src_image->master->slaves,
image);
image->master = src_image->master;
GST_OBJECT_UNLOCK (src_image->master);
}
/* The master requested is not a slave */
else
{
/* Add ourself to the slave list with an increased reference */
src_image->slaves = g_list_append (src_image->slaves, image);
image->master = src_image;
}
image->par_n = src_image->par_n;
image->par_d = src_image->par_d;
GST_OBJECT_UNLOCK (src_image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_IMAGE);
beach:
return ret;
}
/**
* pgm_image_clear:
* @image: a #PgmImage object.
*
* Removes any image from @image. If @image had some image data loaded, it's
* cleared, if there was a #GstBuffer used, it's unreffed and if the @image was
* a slave to another it is not anymore. If @image has slave images they all
* get cleared but they still are slaves to @image. So if you load a new image
* to @image, all the slaves will load it too.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_clear (PgmImage *image)
{
PgmError ret = PGM_ERROR_OK;
GST_OBJECT_LOCK (image);
async_loader_free (image->async_loader);
image->async_loader = NULL;
ret = do_clear (image);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_DATA_EMPTY);
return ret;
}
/**
* pgm_image_get_storage_type:
* @image: a #PgmImage object.
* @storage: a #PgmImageStorageType where the storage type is going to be
* stored.
*
* Retrieves the type of representation being used by @image to store image
* data. If @image has no image data, the return value will be
* #PGM_IMAGE_EMPTY.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_storage_type (PgmImage *image,
PgmImageStorageType *storage)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (storage != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*storage = image->storage_type;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_system_buffer_lock:
* @image: a #PgmImage object.
*
* Lock the system buffer set as the image content, ensuring the image is not
* going to be drawn while the system buffer content changes. This function
* guaranties tear free system buffer updates.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_system_buffer_lock (PgmImage *image)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
/* FIXME: lock */
return PGM_ERROR_OK;
}
/**
* pgm_image_system_buffer_unlock:
* @image: a #PgmImage object.
*
* Unlock the system buffer set as the image content.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_system_buffer_unlock (PgmImage *image)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
/* FIXME: unlock */
_pgm_drawable_emit_changed (PGM_DRAWABLE (image),
PGM_IMAGE_SYSTEM_BUFFER_CONTENT);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_mapping_matrix:
* @image: a #PgmImage object.
* @mapping_matrix: a #PgmMat4x4 object.
*
* Defines the transformation to apply to the stored image when it is rendered.
* You can make the stored image slide over the drawable, rotate around it,
* stretch and shrink, or any combination of the three.
*
* Each point in an image can be defined by an (x, y) vector, which we call
* the source position, representing the horizontal (x) and vertical (y)
* positions (with values between 0 for left/top and 1 for right/bottom).
* When the image is drawn on a surface, each point (x, y) is drawn on the
* (x', y') coordinate of the surface. We call (x', y') the destination
* position. The default mapping matrix is the identity, you have
* (x', y') == (x, y). Once you have called the function, the destination
* position is calculated by multiplying @mapping_matrix with the source
* position vector. To reset the mapping matrix, just set the identity.
*
* @mapping_matrix is a 4x4 matrix since the source and destination positions
* can be represented as 4 coordinate vectors (x, y, z, w) and (x', y', z', w').
* Unless you know what you are doing, you should not worry about z and w, and
* arrange for your matrix to leave them unchanged.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_mapping_matrix (PgmImage *image,
PgmMat4x4 *mapping_matrix)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (mapping_matrix != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
pgm_mat4x4_set_from_mat4x4 (image->mapping_matrix, mapping_matrix);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_MAPPING_MATRIX);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_mapping_matrix:
* @image: a #PgmImage object.
* @mapping_matrix: a pointer to a #PgmMat4x4 pointer where the mapping matrix
* is going to be stored. pgm_mat4x4_free() after use.
*
* Retrieves in @matrix the current mapping matrix applied to @image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_mapping_matrix (PgmImage *image,
PgmMat4x4 **mapping_matrix)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (mapping_matrix != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*mapping_matrix = pgm_mat4x4_copy (image->mapping_matrix);
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_alignment:
* @image: a #PgmImage object.
* @align: a #PgmImageAlignment combination of flags.
*
* Defines the way @image aligns the stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_alignment (PgmImage *image,
PgmImageAlignment align)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image->align = align;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_ALIGNMENT);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_alignment:
* @image: a #PgmImage object.
* @align: a pointer to a #PgmImageAlignment where alignment flags are going
* to be stored.
*
* Retrieves in @align the way @image aligns the stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_alignment (PgmImage *image,
PgmImageAlignment *align)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (align != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*align = image->align;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_layout:
* @image: a #PgmImage object.
* @layout: a #PgmImageLayoutType layout type.
*
* Defines the way @image layouts its stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_layout (PgmImage *image,
PgmImageLayoutType layout)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image->layout = layout;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_LAYOUT);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_layout:
* @image: a #PgmImage object.
* @layout: a pointer to a #PgmImageLayoutType where the layout type is going
* to be stored.
*
* Retrieves in @layout the way @image layouts its its stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_layout (PgmImage *image,
PgmImageLayoutType *layout)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (layout != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*layout = image->layout;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_interp:
* @image: a #PgmImage object.
* @interp: the interpolation type.
*
* Defines that @image will be rendered using @interp as its interpolation
* type.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_interp (PgmImage *image,
PgmImageInterpType interp)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image->interp = interp;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_INTERP);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_interp:
* @image: a #PgmImage object.
* @interp: a pointer to a #PgmImageInterpType where the interpolation type
* is going to be stored.
*
* Retrieves in @interp the current interpolation type of @image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_interp (PgmImage *image,
PgmImageInterpType *interp)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (interp != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*interp = image->interp;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_aspect_ratio:
* @image: a #PgmImage object.
* @numerator: the numerator of the aspect ratio fraction.
* @denominator: the denominator of the aspect ratio fraction.
*
* Customizes the aspect ratio of the stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_aspect_ratio (PgmImage *image,
guint numerator,
guint denominator)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
if (numerator == image->par_n && denominator == image->par_d)
{
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
image->par_n = numerator;
image->par_d = MAX (denominator, 1);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_ASPECT_RATIO);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_aspect_ratio:
* @image: a #PgmImage object.
* @numerator: a pointer to a #guint where the numerator of the aspect ratio
* fraction will be stored.
* @denominator: a pointer to a #guint where the denominator of the aspect
* ratio fraction will be stored.
*
* Retrieves the aspect ratio of the stored image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_aspect_ratio (PgmImage *image,
guint *numerator,
guint *denominator)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (numerator != NULL, PGM_ERROR_X);
g_return_val_if_fail (denominator != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*numerator = image->par_n;
*denominator = image->par_d;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_border_width:
* @image: a #PgmImage object.
* @width: the border with. 0.0 by default.
*
* Defines the border width drawn around @image.
*
* Note that the border is drawn around the image, inside the drawable. When
* you change the size of the border, the image will be down-scaled.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_border_width (PgmImage *image,
gfloat width)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
/* Avoid further computations if the width doesn't change */
if (width == image->border_width)
{
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/* Clamp width to [0, MAX_FLOAT] */
image->border_width = MAX (width, 0);
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image), PGM_IMAGE_BORDER_WIDTH);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_border_width:
* @image: a #PgmImage object.
* @width: a pointer to a #gfloat where the border width will be stored.
*
* Retrieves the width of the border drawn around #image inside the drawable.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_border_width (PgmImage *image,
gfloat *width)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (width != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*width = image->border_width;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_border_inner_color:
* @image: a #PgmImage object.
* @red: the border inner red color. 255 by default.
* @green: the border inner green color. 255 by default.
* @blue: the border inner blue color. 255 by default.
* @alpha: the border inner alpha color. 255 by default.
*
* Defines the border inner color drawn around @image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_border_inner_color (PgmImage *image,
guchar red,
guchar green,
guchar blue,
guchar alpha)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image->border_inner_r = red;
image->border_inner_g = green;
image->border_inner_b = blue;
image->border_inner_a = alpha;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image),
PGM_IMAGE_BORDER_INNER_COLOR);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_border_inner_color:
* @image: a #PgmImage object.
* @red: a pointer to a #guchar where the border inner red color will be stored.
* @green: a pointer to a #guchar where the border inner green color will be
* stored.
* @blue: a pointer to a #guchar where the border inner blue color will be
* stored.
* @alpha: a pointer to a #guchar where the border inner alpha color will be
* stored.
*
* Retrieves the inner color of the border drawn around #image inside the
* drawable.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_border_inner_color (PgmImage *image,
guchar *red,
guchar *green,
guchar *blue,
guchar *alpha)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (red != NULL, PGM_ERROR_X);
g_return_val_if_fail (green != NULL, PGM_ERROR_X);
g_return_val_if_fail (blue != NULL, PGM_ERROR_X);
g_return_val_if_fail (alpha != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*red = image->border_inner_r;
*green = image->border_inner_g;
*blue = image->border_inner_b;
*alpha = image->border_inner_a;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_set_border_outer_color:
* @image: a #PgmImage object.
* @red: the border outer red color. 255 by default.
* @green: the border outer green color. 255 by default.
* @blue: the border outer blue color. 255 by default.
* @alpha: the border outer alpha color. 255 by default.
*
* Defines the border outer color drawn around @image.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_set_border_outer_color (PgmImage *image,
guchar red,
guchar green,
guchar blue,
guchar alpha)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image->border_outer_r = red;
image->border_outer_g = green;
image->border_outer_b = blue;
image->border_outer_a = alpha;
GST_OBJECT_UNLOCK (image);
_pgm_drawable_emit_changed (PGM_DRAWABLE (image),
PGM_IMAGE_BORDER_OUTER_COLOR);
return PGM_ERROR_OK;
}
/**
* pgm_image_get_border_outer_color:
* @image: a #PgmImage object.
* @red: a pointer to a #guchar where the border outer red color will be stored.
* @green: a pointer to a #guchar where the border outer green color will be
* stored.
* @blue: a pointer to a #guchar where the border outer blue color will be
* stored.
* @alpha: a pointer to a #guchar where the border outer alpha color will be
* stored.
*
* Retrieves the outer color of the border drawn around #image inside the
* drawable.
*
* MT safe.
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_get_border_outer_color (PgmImage *image,
guchar *red,
guchar *green,
guchar *blue,
guchar *alpha)
{
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (red != NULL, PGM_ERROR_X);
g_return_val_if_fail (green != NULL, PGM_ERROR_X);
g_return_val_if_fail (blue != NULL, PGM_ERROR_X);
g_return_val_if_fail (alpha != NULL, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
*red = image->border_outer_r;
*green = image->border_outer_g;
*blue = image->border_outer_b;
*alpha = image->border_outer_a;
GST_OBJECT_UNLOCK (image);
return PGM_ERROR_OK;
}
/**
* pgm_image_from_drawable:
* @image: a #PgmImage instance
* @x_image: where to return the x coordinate in the @image plane.
* @y_image: where to return the y coordinate in the @image plane.
* @x_drawable: the x coordinate in drawable space.
* @y_drawable: the y coordinate in drawable space.
*
* Converts a position in drawable space (canvas units, origin at top left of
* drawable) into @image space (image pixel units, origin at top left of image).
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_from_drawable (PgmImage *image,
gint *x_image,
gint *y_image,
gfloat x_drawable,
gfloat y_drawable)
{
gfloat x_offset, y_offset;
gfloat x_resolution, y_resolution;
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (NULL != x_image, PGM_ERROR_X);
g_return_val_if_fail (NULL != y_image, PGM_ERROR_X);
g_return_val_if_fail (image->layout != PGM_IMAGE_CENTERED
|| image->layout != PGM_IMAGE_TILED, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image_get_offset_and_resolution (image, &x_offset, &y_offset,
&x_resolution, &y_resolution);
GST_OBJECT_UNLOCK (image);
*x_image = (gint) ((x_drawable - x_offset) * x_resolution);
*y_image = (gint) ((y_drawable - y_offset) * y_resolution);
return PGM_ERROR_OK;
}
/**
* pgm_image_to_drawable:
* @image: a #PgmImage instance
* @x_drawable: where to return the x coordinate in the @drawable plane.
* @y_drawable: where to return the y coordinate in the @drawable plane.
* @x_image: the x coordinate in image space.
* @y_image: the y coordinate in image space.
*
* Converts a position in @image space (image pixel units, origin at top left of
* image) into drawable space (canvas units, origin at top left of drawable).
*
* Returns: a #PgmError indicating success/failure.
*/
PgmError
pgm_image_to_drawable (PgmImage *image,
gfloat *x_drawable,
gfloat *y_drawable,
gint x_image,
gint y_image)
{
gfloat x_offset, y_offset;
gfloat x_resolution, y_resolution;
g_return_val_if_fail (PGM_IS_IMAGE (image), PGM_ERROR_X);
g_return_val_if_fail (NULL != x_drawable, PGM_ERROR_X);
g_return_val_if_fail (NULL != y_drawable, PGM_ERROR_X);
g_return_val_if_fail (image->layout != PGM_IMAGE_CENTERED
|| image->layout != PGM_IMAGE_TILED, PGM_ERROR_X);
GST_OBJECT_LOCK (image);
image_get_offset_and_resolution (image, &x_offset, &y_offset,
&x_resolution, &y_resolution);
GST_OBJECT_UNLOCK (image);
*x_drawable = x_offset + x_image / x_resolution;
*y_drawable = y_offset + y_image / y_resolution;
return PGM_ERROR_OK;
}
|