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
|
/* array type
*
* Unlike GArray, this has fixed length, tracks a GType for emements, and has
* a per-element free function.
*
* 27/10/11
* - from header.c
*/
/*
This file is part of VIPS.
VIPS 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 program 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 program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define VIPS_DEBUG
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
/* A very simple boxed type for testing. Just an int.
*/
/**
* vips_thing_new:
* @n:
*
* Returns: (transfer full): a new #VipsThing.
*/
VipsThing *
vips_thing_new( int i )
{
VipsThing *thing;
thing = g_new( VipsThing, 1 );
thing->i = i;
printf( "vips_thing_new: %d %p\n", i, thing );
return( thing );
}
static VipsThing *
vips_thing_copy( VipsThing *thing )
{
VipsThing *thing2;
thing2 = vips_thing_new( thing->i );
printf( "vips_thing_copy: %d %p = %p\n", thing->i, thing2, thing );
return( thing2 );
}
static void
vips_thing_free( VipsThing *thing )
{
printf( "vips_thing_free: %d %p\n", thing->i, thing );
g_free( thing );
}
int
vips_thing_get_i( VipsThing *thing )
{
printf( "vips_thing_get_i: %d %p\n", thing->i, thing );
return( thing->i );
}
G_DEFINE_BOXED_TYPE( VipsThing, vips_thing,
(GBoxedCopyFunc) vips_thing_copy,
(GBoxedFreeFunc) vips_thing_free );
/**
* SECTION: VipsArea
* @short_description: an area of memory
* @stability: Stable
* @see_also: <link linkend="libvips-meta">header</link>,
* @include: vips/vips.h
*
* A VipsArea wraps a chunk of memory. It adds reference counting and a free
* function. It also keeps a count and a GType, so the area can be an array.
*
* This type is used for things like passing an array of double or an array of
* VipsObject pointers to operations, and for reference-countred immutable
* strings.
*/
#ifdef DEBUG
static int vips_area_number = 0;
#endif /*DEBUG*/
VipsArea *
vips_area_copy( VipsArea *area )
{
g_assert( area->count >= 0 );
area->count += 1;
#ifdef DEBUG
printf( "vips_area_copy: %p count = %d\n", area, area->count );
#endif /*DEBUG*/
return( area );
}
void
vips_area_unref( VipsArea *area )
{
g_assert( area->count > 0 );
area->count -= 1;
#ifdef DEBUG
printf( "vips_area_unref: %p count = %d\n", area, area->count );
#endif /*DEBUG*/
if( area->count == 0 ) {
if( area->free_fn && area->data ) {
area->free_fn( area->data, area );
area->data = NULL;
area->free_fn = NULL;
}
g_free( area );
#ifdef DEBUG
vips_area_number -= 1;
printf( "vips_area_unref: free .. total = %d\n",
vips_area_number );
#endif /*DEBUG*/
}
}
/**
* vips_area_new:
* @free_fn: (scope async): @data will be freed with this function
* @data: data will be freed with this function
*
* An area of memory with a free function. (eg. \0-terminated string, or a
* struct). Inital count == 1, so _unref() after attaching somewhere.
*
* See also: vips_area_unref().
*
* Returns: (transfer full): the new #VipsArea.
*/
VipsArea *
vips_area_new( VipsCallbackFn free_fn, void *data )
{
VipsArea *area;
area = g_new( VipsArea, 1 );
area->count = 1;
area->length = 0;
area->data = data;
area->free_fn = free_fn;
area->type = 0;
area->sizeof_type = 0;
#ifdef DEBUG
vips_area_number += 1;
printf( "vips_area_new: %p count = %d (%d in total)\n",
area, area->count, vips_area_number );
#endif /*DEBUG*/
return( area );
}
/**
* vips_area_new_blob:
* @free_fn: (scope async): @data will be freed with this function
* @data: data will be freed with this function
* @length: number of bytes in @data
*
* Like vips_area_new(), but track a length as well.
*
* An area of mem with a free func and a length (some sort of binary object,
* like an ICC profile).
*
* See also: vips_area_unref().
*
* Returns: (transfer full): the new #VipsArea.
*/
VipsArea *
vips_area_new_blob( VipsCallbackFn free_fn, void *data, size_t length )
{
VipsArea *area;
area = vips_area_new( free_fn, data );
area->length = length;
return( area );
}
/**
* vips_area_new_array:
* @type: %GType of elements to store
* @sizeof_type: sizeof() an element in the array
* @n: number of elements in the array
*
* An area which holds an array of elements of some GType. To set values for
* the elements, get the pointer and write.
*
* See also: vips_area_unref().
*
* Returns: (transfer full): the new #VipsArea.
*/
VipsArea *
vips_area_new_array( GType type, size_t sizeof_type, int n )
{
VipsArea *area;
void *array;
array = g_malloc( n * sizeof_type );
area = vips_area_new( (VipsCallbackFn) g_free, array );
area->n = n;
area->length = n * sizeof_type;
area->type = type;
area->sizeof_type = sizeof_type;
return( area );
}
static void
vips_area_free_array_object( GObject **array, VipsArea *area )
{
int i;
for( i = 0; i < area->n; i++ )
VIPS_FREEF( g_object_unref, array[i] );
area->n = 0;
}
/**
* vips_area_new_array_object:
* @n: number of elements in the array
*
* An area which holds an array of GObjects. See vips_area_new_array(). When
* the area is freed, each %GObject will be unreffed.
*
* See also: vips_area_unref().
*
* Returns: (transfer full): the new #VipsArea.
*/
VipsArea *
vips_area_new_array_object( int n )
{
GObject **array;
VipsArea *area;
array = g_new0( GObject *, n );
area = vips_area_new( (VipsCallbackFn) vips_area_free_array_object,
array );
area->n = n;
area->length = n * sizeof( GObject * );
area->type = G_TYPE_OBJECT;
area->sizeof_type = sizeof( GObject * );
return( area );
}
/**
* vips_area_get_data:
* @area: #VipsArea to fetch from
* @length: (allow-none): optionally return length in bytes here
* @n: (allow-none): optionally return number of elements here
* @type: (allow-none): optionally return element type here
* @sizeof_type: (allow-none): optionally return sizeof() element type here
*
* Return the data pointer plus optionally the length in bytes of an area,
* the number of elements, the %GType of each element and the sizeof() each
* element.
*
* Returns: (transfer none): The pointer held by @area.
*/
void *
vips_area_get_data( VipsArea *area,
size_t *length, int *n, GType *type, size_t *sizeof_type )
{
if( length )
*length = area->length;
if( n )
*n = area->n;
if( type )
*type = area->type;
if( sizeof_type )
*sizeof_type = area->sizeof_type;
return( area->data );
}
/* Transform an area to a G_TYPE_STRING.
*/
static void
transform_area_g_string( const GValue *src_value, GValue *dest_value )
{
VipsArea *area;
char buf[256];
area = g_value_get_boxed( src_value );
vips_snprintf( buf, 256, "VIPS_TYPE_AREA, count = %d, data = %p",
area->count, area->data );
g_value_set_string( dest_value, buf );
}
GType
vips_area_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsArea",
(GBoxedCopyFunc) vips_area_copy,
(GBoxedFreeFunc) vips_area_unref );
g_value_register_transform_func( type, G_TYPE_STRING,
transform_area_g_string );
}
return( type );
}
/* Transform funcs for builtin types to SAVE_STRING.
*/
static void
transform_int_save_string( const GValue *src_value, GValue *dest_value )
{
vips_value_set_save_stringf( dest_value,
"%d", g_value_get_int( src_value ) );
}
static void
transform_save_string_int( const GValue *src_value, GValue *dest_value )
{
g_value_set_int( dest_value,
atoi( vips_value_get_save_string( src_value ) ) );
}
static void
transform_double_save_string( const GValue *src_value, GValue *dest_value )
{
char buf[G_ASCII_DTOSTR_BUF_SIZE];
/* Need to be locale independent.
*/
g_ascii_dtostr( buf, G_ASCII_DTOSTR_BUF_SIZE,
g_value_get_double( src_value ) );
vips_value_set_save_string( dest_value, buf );
}
static void
transform_save_string_double( const GValue *src_value, GValue *dest_value )
{
g_value_set_double( dest_value,
g_ascii_strtod( vips_value_get_save_string( src_value ),
NULL ) );
}
/* Save meta fields to the header. We have a new string type for header fields
* to save to XML and define transform functions to go from our meta types to
* this string type.
*/
GType
vips_save_string_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsSaveString",
(GBoxedCopyFunc) g_strdup,
(GBoxedFreeFunc) g_free );
}
return( type );
}
/* Transform a refstring to a G_TYPE_STRING and back.
*/
static void
transform_ref_string_g_string( const GValue *src_value, GValue *dest_value )
{
g_value_set_string( dest_value,
vips_value_get_ref_string( src_value, NULL ) );
}
static void
transform_g_string_ref_string( const GValue *src_value, GValue *dest_value )
{
vips_value_set_ref_string( dest_value,
g_value_get_string( src_value ) );
}
/* To a save string.
*/
static void
transform_ref_string_save_string( const GValue *src_value, GValue *dest_value )
{
vips_value_set_save_stringf( dest_value,
"%s", vips_value_get_ref_string( src_value, NULL ) );
}
static void
transform_save_string_ref_string( const GValue *src_value, GValue *dest_value )
{
vips_value_set_ref_string( dest_value,
vips_value_get_save_string( src_value ) );
}
GType
vips_ref_string_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsRefString",
(GBoxedCopyFunc) vips_area_copy,
(GBoxedFreeFunc) vips_area_unref );
g_value_register_transform_func( type, G_TYPE_STRING,
transform_ref_string_g_string );
g_value_register_transform_func( G_TYPE_STRING, type,
transform_g_string_ref_string );
g_value_register_transform_func( type, VIPS_TYPE_SAVE_STRING,
transform_ref_string_save_string );
g_value_register_transform_func( VIPS_TYPE_SAVE_STRING, type,
transform_save_string_ref_string );
}
return( type );
}
/* Transform a blob to a G_TYPE_STRING.
*/
static void
transform_blob_g_string( const GValue *src_value, GValue *dest_value )
{
void *blob;
size_t blob_length;
char buf[256];
blob = vips_value_get_blob( src_value, &blob_length );
vips_snprintf( buf, 256, "VIPS_TYPE_BLOB, data = %p, length = %zd",
blob, blob_length );
g_value_set_string( dest_value, buf );
}
/* Transform a blob to a save string and back.
*/
static void
transform_blob_save_string( const GValue *src_value, GValue *dest_value )
{
void *blob;
size_t blob_length;
char *b64;
blob = vips_value_get_blob( src_value, &blob_length );
if( (b64 = vips__b64_encode( blob, blob_length )) ) {
vips_value_set_save_string( dest_value, b64 );
vips_free( b64 );
}
}
static void
transform_save_string_blob( const GValue *src_value, GValue *dest_value )
{
const char *b64;
void *blob;
size_t blob_length;
b64 = vips_value_get_save_string( src_value );
if( (blob = vips__b64_decode( b64, &blob_length )) )
vips_value_set_blob( dest_value,
(VipsCallbackFn) vips_free, blob, blob_length );
}
GType
vips_blob_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsBlob",
(GBoxedCopyFunc) vips_area_copy,
(GBoxedFreeFunc) vips_area_unref );
g_value_register_transform_func( type, G_TYPE_STRING,
transform_blob_g_string );
g_value_register_transform_func( type, VIPS_TYPE_SAVE_STRING,
transform_blob_save_string );
g_value_register_transform_func( VIPS_TYPE_SAVE_STRING, type,
transform_save_string_blob );
}
return( type );
}
static void
transform_array_double_g_string( const GValue *src_value, GValue *dest_value )
{
int n;
double *array = vips_value_get_array_double( src_value, &n );
char txt[1024];
VipsBuf buf = VIPS_BUF_STATIC( txt );
int i;
for( i = 0; i < n; i++ )
/* Use space as a separator since ',' may be a decimal point
* in this locale.
*/
vips_buf_appendf( &buf, "%g ", array[i] );
g_value_set_string( dest_value, vips_buf_all( &buf ) );
}
/* It'd be great to be able to write a generic string->array function, but
* it doesn't seem possible.
*/
static void
transform_g_string_array_double( const GValue *src_value, GValue *dest_value )
{
char *str;
int n;
char *p, *q;
int i;
double *array;
/* Walk the string to get the number of elements.
* We need a copy of the string, since we insert \0 during
* scan.
*
* We can't allow ',' as a separator, since some locales use it as a
* decimal point.
*/
str = g_value_dup_string( src_value );
n = 0;
for( p = str; (q = vips_break_token( p, "\t; " )); p = q )
n += 1;
g_free( str );
vips_value_set_array( dest_value, n, G_TYPE_DOUBLE, sizeof( double ) );
array = (double *) vips_value_get_array( dest_value, NULL, NULL, NULL );
str = g_value_dup_string( src_value );
i = 0;
for( p = str; (q = vips_break_token( p, "\t; " )); p = q )
array[i++] = atof( p );
g_free( str );
}
GType
vips_array_double_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsArrayDouble",
(GBoxedCopyFunc) vips_area_copy,
(GBoxedFreeFunc) vips_area_unref );
g_value_register_transform_func( type, G_TYPE_STRING,
transform_array_double_g_string );
g_value_register_transform_func( G_TYPE_STRING, type,
transform_g_string_array_double );
}
return( type );
}
static void
transform_g_string_array_image( const GValue *src_value, GValue *dest_value )
{
char *str;
int n;
char *p, *q;
int i;
GObject **array;
/* We need a copy of the string, since we insert \0 during
* scan.
*/
str = g_value_dup_string( src_value );
n = 0;
for( p = str; (q = vips_break_token( p, " " )); p = q )
n += 1;
g_free( str );
vips_value_set_array_object( dest_value, n );
array = vips_value_get_array_object( dest_value, NULL );
str = g_value_dup_string( src_value );
for( i = 0, p = str; (q = vips_break_token( p, " " )); i++, p = q )
if( !(array[i] = G_OBJECT( vips_image_new_from_file( p ) )) ) {
/* Set the dest to length zero to indicate error.
*/
vips_value_set_array_object( dest_value, 0 );
g_free( str );
return;
}
g_free( str );
}
GType
vips_array_image_get_type( void )
{
static GType type = 0;
if( !type ) {
type = g_boxed_type_register_static( "VipsArrayImage",
(GBoxedCopyFunc) vips_area_copy,
(GBoxedFreeFunc) vips_area_unref );
g_value_register_transform_func( G_TYPE_STRING, type,
transform_g_string_array_image );
}
return( type );
}
/**
* vips_value_set_area:
* @value: (out): set this value
* @free_fn: (scope async): data will be freed with this function
* @data: set @value to track this pointer
*
* Set value to be a ref-counted area of memory with a free function.
*/
void
vips_value_set_area( GValue *value, VipsCallbackFn free_fn, void *data )
{
VipsArea *area;
area = vips_area_new( free_fn, data );
g_value_init( value, VIPS_TYPE_AREA );
g_value_set_boxed( value, area );
vips_area_unref( area );
}
/**
* vips_value_get_area:
* @value: get from this value
* @length: (allow-none): optionally return length here
*
* Get the pointer from an area. Don't touch count (area is static).
*
* Returns: (transfer none): The pointer held by @value.
*/
void *
vips_value_get_area( const GValue *value, size_t *length )
{
VipsArea *area;
area = g_value_get_boxed( value );
return( vips_area_get_data( area, length, NULL, NULL, NULL ) );
}
/**
* vips_value_get_save_string:
* @value: GValue to get from
*
* Get the C string held internally by the GValue.
*
* Returns: (transfer none): The C string held by @value.
*/
const char *
vips_value_get_save_string( const GValue *value )
{
return( (char *) g_value_get_boxed( value ) );
}
/**
* vips_value_set_save_string:
* @value: (out): GValue to set
* @str: C string to copy into the GValue
*
* Copies the C string into @value.
*/
void
vips_value_set_save_string( GValue *value, const char *str )
{
g_assert( G_VALUE_TYPE( value ) == VIPS_TYPE_SAVE_STRING );
g_value_set_boxed( value, str );
}
/**
* vips_value_set_save_stringf:
* @value: (out): GValue to set
* @fmt: printf()-style format string
* @Varargs: arguments to printf()-formatted @fmt
*
* Generates a string and copies it into @value.
*/
void
vips_value_set_save_stringf( GValue *value, const char *fmt, ... )
{
va_list ap;
char *str;
g_assert( G_VALUE_TYPE( value ) == VIPS_TYPE_SAVE_STRING );
va_start( ap, fmt );
str = g_strdup_vprintf( fmt, ap );
va_end( ap );
vips_value_set_save_string( value, str );
g_free( str );
}
/**
* vips_value_get_ref_string:
* @value: %GValue to get from
* @length: (allow-none): return length here, optionally
*
* Get the C string held internally by the GValue.
*
* Returns: (transfer none): The C string held by @value.
*/
const char *
vips_value_get_ref_string( const GValue *value, size_t *length )
{
return( vips_value_get_area( value, length ) );
}
/**
* vips_value_set_ref_string:
* @value: (out): GValue to set
* @str: C string to copy into the GValue
*
* Copies the C string @str into @value.
*
* vips_ref_string are immutable C strings that are copied between images by
* copying reference-counted pointers, making the much more efficient than
* regular GValue strings.
*
* Returns: 0 on success, -1 otherwise.
*/
int
vips_value_set_ref_string( GValue *value, const char *str )
{
VipsArea *area;
char *str_copy;
g_assert( G_VALUE_TYPE( value ) == VIPS_TYPE_REF_STRING );
str_copy = g_strdup( str );
area = vips_area_new( (VipsCallbackFn) vips_free, str_copy );
/* Handy place to cache this.
*/
area->length = strlen( str );
g_value_set_boxed( value, area );
vips_area_unref( area );
return( 0 );
}
/**
* vips_value_set_blob:
* @value: (out): GValue to set
* @free_fn: (scope async): free function for @data
* @data: pointer to area of memory
* @length: length of memory area
*
* Sets @value to hold a @data. When @value is freed, @data will be
* freed with @free_fn. @value also holds a note of the length of the memory
* area.
*
* blobs are things like ICC profiles or EXIF data. They are relocatable, and
* are saved to VIPS files for you coded as base64 inside the XML. They are
* copied by copying reference-counted pointers.
*
* See also: vips_value_get_blob()
*/
void
vips_value_set_blob( GValue *value,
VipsCallbackFn free_fn, void *data, size_t length )
{
VipsArea *area;
g_assert( G_VALUE_TYPE( value ) == VIPS_TYPE_BLOB );
area = vips_area_new_blob( free_fn, data, length );
g_value_set_boxed( value, area );
vips_area_unref( area );
}
/**
* vips_value_get_blob:
* @value: GValue to set
* @length: (allow-none): optionally return length of memory area
*
* Returns the data pointer from a blob. Optionally returns the length too.
*
* blobs are things like ICC profiles or EXIF data. They are relocatable, and
* are saved to VIPS files for you coded as base64 inside the XML. They are
* copied by copying reference-counted pointers.
*
* See also: vips_value_set_blob()
*
* Returns: (transfer none): The pointer held by @value.
*/
void *
vips_value_get_blob( const GValue *value, size_t *length )
{
return( vips_value_get_area( value, length ) );
}
/**
* vips_value_set_array:
* @value: (out): %GValue to set
* @n: number of elements
* @type: the type of each element
* @sizeof_type: the sizeof each element
*
* Set @value to be an array of things.
*
* This allocates memory but does not
* initialise the contents: get the pointer and write instead.
*/
void
vips_value_set_array( GValue *value, int n, GType type, size_t sizeof_type )
{
VipsArea *area;
area = vips_area_new_array( type, sizeof_type, n );
g_value_set_boxed( value, area );
vips_area_unref( area );
}
/**
* vips_value_get_array:
* @value: %GValue to get from
* @n: (allow-none): return the number of elements here, optionally
* @type: (allow-none): return the type of each element here, optionally
* @sizeof_type: (allow-none): return the sizeof each element here, optionally
*
* Return the pointer to the array held by @value.
* Optionally return the other properties of the array in @n, @type,
* @sizeof_type.
*
* See also: vips_value_set_array().
*
* Returns: (transfer none): The array address.
*/
void *
vips_value_get_array( const GValue *value,
int *n, GType *type, size_t *sizeof_type )
{
VipsArea *area;
/* Can't check value type, because we may get called from
* vips_*_get_type().
*/
area = g_value_get_boxed( value );
if( n )
*n = area->n;
if( type )
*type = area->type;
if( sizeof_type )
*sizeof_type = area->sizeof_type;
return( area->data );
}
/**
* vips_array_double_new:
* @array: (array length=n): array of double
* @n: number of doubles
*
* Allocate a new array of doubles and copy @array into it. Free with
* vips_area_unref().
*
* See also: #VipsArea.
*
* Returns: (transfer full): A new #VipsArrayDouble.
*/
VipsArrayDouble *
vips_array_double_new( const double *array, int n )
{
VipsArea *area;
double *array_copy;
printf( "hello, world!\n" );
area = vips_area_new_array( G_TYPE_DOUBLE, sizeof( double ), n );
array_copy = vips_area_get_data( area, NULL, NULL, NULL, NULL );
memcpy( array_copy, array, n * sizeof( double ) );
return( area );
}
/**
* vips_value_get_array_double:
* @value: %GValue to get from
* @n: (allow-none): return the number of elements here, optionally
*
* Return the start of the array of doubles held by @value.
* optionally return the number of elements in @n.
*
* See also: vips_array_double_set().
*
* Returns: (transfer none): The array address.
*/
double *
vips_value_get_array_double( const GValue *value, int *n )
{
return( vips_value_get_array( value, n, NULL, NULL ) );
}
/**
* vips_value_set_array_double:
* @value: (out): %GValue to get from
* @array: (array length=n): array of doubles
* @n: the number of elements
*
* Set @value to hold a copy of @array. Pass in the array length in @n.
*
* See also: vips_array_double_get().
*
* Returns: 0 on success, -1 otherwise.
*/
int
vips_value_set_array_double( GValue *value, const double *array, int n )
{
double *array_copy;
g_value_init( value, VIPS_TYPE_ARRAY_DOUBLE );
vips_value_set_array( value, n, G_TYPE_DOUBLE, sizeof( double ) );
array_copy = vips_value_get_array_double( value, NULL );
memcpy( array_copy, array, n * sizeof( double ) );
return( 0 );
}
/**
* vips_value_get_array_object: (skip)
* @value: %GValue to get from
* @n: (allow-none): return the number of elements here, optionally
*
* Return the start of the array of %GObject held by @value.
* optionally return the number of elements in @n.
*
* See also: vips_array_object_set().
*
* Returns: (transfer none): The array address.
*/
GObject **
vips_value_get_array_object( const GValue *value, int *n )
{
return( vips_value_get_array( value, n, NULL, NULL ) );
}
/**
* vips_array_object_set:
* @value: (out): %GValue to set
* @n: the number of elements
*
* Set @value to hold an array of GObject. Pass in the array length in @n.
*
* See also: vips_array_object_get().
*
* Returns: 0 on success, -1 otherwise.
*/
int
vips_value_set_array_object( GValue *value, int n )
{
VipsArea *area;
if( !(area = vips_area_new_array_object( n )) )
return( -1 );
g_value_set_boxed( value, area );
vips_area_unref( area );
return( 0 );
}
/* Make the types we need for basic functioning. Called from init_world().
*/
void
vips__meta_init_types( void )
{
(void) vips_thing_get_type();
(void) vips_save_string_get_type();
(void) vips_area_get_type();
(void) vips_ref_string_get_type();
(void) vips_blob_get_type();
(void) vips_array_double_get_type();
(void) vips_array_image_get_type();
/* Register transform functions to go from built-in saveable types to
* a save string. Transform functions for our own types are set
* during type creation.
*/
g_value_register_transform_func( G_TYPE_INT, VIPS_TYPE_SAVE_STRING,
transform_int_save_string );
g_value_register_transform_func( VIPS_TYPE_SAVE_STRING, G_TYPE_INT,
transform_save_string_int );
g_value_register_transform_func( G_TYPE_DOUBLE, VIPS_TYPE_SAVE_STRING,
transform_double_save_string );
g_value_register_transform_func( VIPS_TYPE_SAVE_STRING, G_TYPE_DOUBLE,
transform_save_string_double );
}
|