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
|
/* save to heif
*
* 5/7/18
* - from niftisave.c
* 3/7/19 [lovell]
* - add "compression" option
* 1/9/19 [meyermarcel]
* - save alpha when necessary
* 15/3/20
* - revise for new VipsTarget API
* 14/2/21 kleisauke
* - move GObject part to vips2heif.c
* 30/7/21
* - rename "speed" as "effort" for consistency with other savers
* 22/12/21
* - add >8 bit support
* 22/10/11
* - improve rules for 16-bit write [johntrunc]
*/
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG_VERBOSE
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#ifdef HAVE_HEIF_ENCODER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include "pforeign.h"
#include <libheif/heif.h>
typedef struct _VipsForeignSaveHeif {
VipsForeignSave parent_object;
/* Where to write (set by subclasses).
*/
VipsTarget *target;
/* Coding quality factor (1 - 100).
*/
int Q;
/* bitdepth to save at for >8 bit images.
*/
int bitdepth;
/* Lossless compression.
*/
gboolean lossless;
/* Compression format
*/
VipsForeignHeifCompression compression;
/* CPU effort (0 - 9).
*/
int effort;
/* Chroma subsampling.
*/
VipsForeignSubsample subsample_mode;
/* Encoder to use. For instance: aom, svt etc.
*/
VipsForeignHeifEncoder selected_encoder;
/* The image we save. This is a copy of save->ready since we need to
* be able to update the metadata.
*/
VipsImage *image;
int page_width;
int page_height;
int n_pages;
struct heif_context *ctx;
struct heif_encoder *encoder;
/* The current page we are writing.
*/
struct heif_image_handle *handle;
/* The current page in memory which we build as we scan down the
* image.
*/
struct heif_image *img;
/* The libheif memory area we fill with pixels from the libvips
* pipe.
*/
uint8_t *data;
int stride;
/* Deprecated ... this is now called effort for consistency with the
* other encoders.
*/
int speed;
} VipsForeignSaveHeif;
typedef VipsForeignSaveClass VipsForeignSaveHeifClass;
/* Defined in heifload.c
*/
void vips__heif_image_print( struct heif_image *img );
void vips__heif_error( struct heif_error *error );
G_DEFINE_ABSTRACT_TYPE( VipsForeignSaveHeif, vips_foreign_save_heif,
VIPS_TYPE_FOREIGN_SAVE );
static void
vips_foreign_save_heif_dispose( GObject *gobject )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) gobject;
VIPS_UNREF( heif->target );
VIPS_UNREF( heif->image );
VIPS_FREEF( heif_image_release, heif->img );
VIPS_FREEF( heif_image_handle_release, heif->handle );
VIPS_FREEF( heif_encoder_release, heif->encoder );
VIPS_FREEF( heif_context_free, heif->ctx );
G_OBJECT_CLASS( vips_foreign_save_heif_parent_class )->
dispose( gobject );
}
typedef struct heif_error (*libheif_metadata_fn)( struct heif_context *,
const struct heif_image_handle *,
const void *, int );
/* String-based metadata fields we add.
*/
typedef struct _VipsForeignSaveHeifMetadata {
const char *name; /* as understood by libvips */
libheif_metadata_fn saver; /* as understood by libheif */
} VipsForeignSaveHeifMetadata;
static VipsForeignSaveHeifMetadata libheif_metadata[] = {
{ VIPS_META_EXIF_NAME, heif_context_add_exif_metadata },
{ VIPS_META_XMP_NAME, heif_context_add_XMP_metadata }
};
static int
vips_foreign_save_heif_write_metadata( VipsForeignSaveHeif *heif )
{
int i;
struct heif_error error;
/* Rebuild exif from tags, if we'll be saving it.
*/
if( vips__exif_update( heif->image ) )
return( -1 );
for( i = 0; i < VIPS_NUMBER( libheif_metadata ); i++ )
if( vips_image_get_typeof( heif->image,
libheif_metadata[i].name ) ) {
const void *data;
size_t length;
#ifdef DEBUG
printf( "attaching %s ..\n",
libheif_metadata[i].name );
#endif /*DEBUG*/
if( vips_image_get_blob( heif->image,
libheif_metadata[i].name, &data, &length ) )
return( -1 );
error = libheif_metadata[i].saver( heif->ctx,
heif->handle, data, length );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
}
return( 0 );
}
static int
vips_foreign_save_heif_write_page( VipsForeignSaveHeif *heif, int page )
{
VipsForeignSave *save = (VipsForeignSave *) heif;
struct heif_error error;
struct heif_encoding_options *options;
#ifdef HAVE_HEIF_COLOR_PROFILE
if( !save->strip &&
vips_image_get_typeof( heif->image, VIPS_META_ICC_NAME ) ) {
const void *data;
size_t length;
#ifdef DEBUG
printf( "attaching profile ..\n" );
#endif /*DEBUG*/
if( vips_image_get_blob( heif->image,
VIPS_META_ICC_NAME, &data, &length ) )
return( -1 );
/* FIXME .. also see heif_image_set_nclx_color_profile()
*/
error = heif_image_set_raw_color_profile( heif->img,
"rICC", data, length );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
}
#endif /*HAVE_HEIF_COLOR_PROFILE*/
options = heif_encoding_options_alloc();
options->save_alpha_channel = heif->image->Bands > 3;
#ifdef DEBUG
{
GTimer *timer = g_timer_new();
printf( "calling heif_context_encode_image() ...\n" );
#endif /*DEBUG*/
error = heif_context_encode_image( heif->ctx,
heif->img, heif->encoder, options, &heif->handle );
#ifdef DEBUG
printf( "... libheif took %.2g seconds\n",
g_timer_elapsed( timer, NULL ) );
g_timer_destroy( timer );
}
#endif /*DEBUG*/
heif_encoding_options_free( options );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
if( vips_image_get_typeof( heif->image, "heif-primary" ) ) {
int primary;
if( vips_image_get_int( heif->image,
"heif-primary", &primary ) )
return( -1 );
if( page == primary ) {
error = heif_context_set_primary_image( heif->ctx,
heif->handle );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
}
}
if( !save->strip &&
vips_foreign_save_heif_write_metadata( heif ) )
return( -1 );
VIPS_FREEF( heif_image_handle_release, heif->handle );
return( 0 );
}
static int
vips_foreign_save_heif_pack( VipsForeignSaveHeif *heif,
VipsPel *q, VipsPel *p, int ne )
{
int i;
if( heif->image->BandFmt == VIPS_FORMAT_UCHAR &&
heif->bitdepth == 8 )
/* Most common case -- 8 bit to 8 bit.
*/
memcpy( q, p, ne );
else if( heif->image->BandFmt == VIPS_FORMAT_UCHAR &&
heif->bitdepth > 8 ) {
/* 8-bit source, write a bigendian short, shifted up.
*/
int shift = heif->bitdepth - 8;
for( i = 0; i < ne; i++ ) {
guint16 v = p[i] << shift;
q[0] = v >> 8;
q[1] = v;
q += 2;
}
}
else if( heif->image->BandFmt == VIPS_FORMAT_USHORT &&
heif->bitdepth <= 8 ) {
/* 16-bit native byte order source, 8 bit write.
*
* Pick the high or low bits of the source.
*/
int vips_bitdepth =
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
16 : 8;
int shift = vips_bitdepth - heif->bitdepth;
for( i = 0; i < ne; i++ ) {
guint16 v = *((gushort *) p) >> shift;
q[i] = v;
p += 2;
}
}
else if( heif->image->BandFmt == VIPS_FORMAT_USHORT &&
heif->bitdepth > 8 ) {
/* 16-bit native byte order source, 16 bit bigendian write.
*/
int vips_bitdepth =
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
16 : 8;
int shift = vips_bitdepth - heif->bitdepth;
for( i = 0; i < ne; i++ ) {
guint16 v = *((gushort *) p) >> shift;
q[0] = v >> 8;
q[1] = v;
p += 2;
q += 2;
}
}
else {
VipsObjectClass *class = VIPS_OBJECT_CLASS( heif );
vips_error( class->nickname,
"%s", _( "unimplemeted format conversion" ) );
return( -1 );
}
return( 0 );
}
static int
vips_foreign_save_heif_write_block( VipsRegion *region, VipsRect *area,
void *a )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) a;
int y;
#ifdef DEBUG
printf( "vips_foreign_save_heif_write_block: y = %d\n", area->top );
#endif /*DEBUG*/
/* Copy a line at a time into our output image, write each time the
* image fills.
*/
for( y = 0; y < area->height; y++ ) {
/* Y in page.
*/
int page = (area->top + y) / heif->page_height;
int line = (area->top + y) % heif->page_height;
VipsPel *p = VIPS_REGION_ADDR( region, 0, area->top + y );
VipsPel *q = heif->data + line * heif->stride;
if( vips_foreign_save_heif_pack( heif,
q, p, VIPS_REGION_N_ELEMENTS( region ) ) )
return( -1 );
/* Did we just write the final line? Write as a new page
* into the output.
*/
if( line == heif->page_height - 1 )
if( vips_foreign_save_heif_write_page( heif, page ) )
return( -1 );
}
return( 0 );
}
struct heif_error
vips_foreign_save_heif_write( struct heif_context *ctx,
const void *data, size_t length, void *userdata )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) userdata;
struct heif_error error;
error.code = 0;
if( vips_target_write( heif->target, data, length ) )
error.code = -1;
return( error );
}
static int
vips_foreign_save_heif_build( VipsObject *object )
{
VipsForeignSave *save = (VipsForeignSave *) object;
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) object;
struct heif_error error;
struct heif_writer writer;
char *chroma;
const struct heif_encoder_descriptor *out_encoder;
gboolean has_alpha;
if( VIPS_OBJECT_CLASS( vips_foreign_save_heif_parent_class )->
build( object ) )
return( -1 );
/* Make a copy of the image in case we modify the metadata eg. for
* exif_update.
*/
if( vips_copy( save->ready, &heif->image, NULL ) )
return( -1 );
/* If the old, deprecated "speed" param is being used and the new
* "effort" param is not, use speed to init effort.
*/
if( vips_object_argument_isset( object, "speed" ) &&
!vips_object_argument_isset( object, "effort" ) )
heif->effort = 9 - heif->speed;
/* Default 12 bit save for 16-bit images. HEIC (for example) implements
* 8 / 10 / 12.
*/
if( !vips_object_argument_isset( object, "bitdepth" ) )
heif->bitdepth =
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
12 : 8;
/* Try to find the selected encoder.
*/
if( heif->selected_encoder != VIPS_FOREIGN_HEIF_ENCODER_AUTO ) {
const int count = heif_context_get_encoder_descriptors(
heif->ctx,
(enum heif_compression_format) heif->compression,
vips_enum_nick( VIPS_TYPE_FOREIGN_HEIF_ENCODER,
heif->selected_encoder ),
&out_encoder, 1 );
if( count > 0 )
error = heif_context_get_encoder( heif->ctx,
out_encoder, &heif->encoder );
else
g_warning( "heifsave: could not find %s",
vips_enum_nick( VIPS_TYPE_FOREIGN_HEIF_ENCODER,
heif->selected_encoder ) );
}
/* Fallback to default encoder.
*/
if( !heif->encoder )
error = heif_context_get_encoder_for_format( heif->ctx,
(enum heif_compression_format) heif->compression,
&heif->encoder );
if( error.code ) {
if( error.code == heif_error_Unsupported_filetype )
vips_error( "heifsave",
"%s", _( "Unsupported compression" ) );
else
vips__heif_error( &error );
return( -1 );
}
error = heif_encoder_set_lossy_quality( heif->encoder, heif->Q );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
error = heif_encoder_set_lossless( heif->encoder, heif->lossless );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
error = heif_encoder_set_parameter_integer( heif->encoder,
"speed", 9 - heif->effort );
if( error.code &&
error.subcode != heif_suberror_Unsupported_parameter ) {
vips__heif_error( &error );
return( -1 );
}
chroma = heif->subsample_mode == VIPS_FOREIGN_SUBSAMPLE_OFF ||
(heif->subsample_mode == VIPS_FOREIGN_SUBSAMPLE_AUTO &&
heif->Q >= 90) ? "444" : "420";
error = heif_encoder_set_parameter_string( heif->encoder,
"chroma", chroma );
if( error.code &&
error.subcode != heif_suberror_Unsupported_parameter ) {
vips__heif_error( &error );
return( -1 );
}
/* TODO .. support extra per-encoder params with
* heif_encoder_list_parameters().
*/
heif->page_width = heif->image->Xsize;
heif->page_height = vips_image_get_page_height( heif->image );
heif->n_pages = heif->image->Ysize / heif->page_height;
has_alpha = heif->image->Bands > 3;
/* Make a heif image the size of a page. We send sink_disc() output
* here and write a frame each time it fills.
*/
#ifdef DEBUG
printf( "vips_foreign_save_heif_build:\n" );
printf( "\twidth = %d\n", heif->page_width );
printf( "\theight = %d\n", heif->page_height );
printf( "\talpha = %d\n", has_alpha );
#endif /*DEBUG*/
error = heif_image_create( heif->page_width, heif->page_height,
heif_colorspace_RGB,
vips__heif_chroma( heif->bitdepth, has_alpha ),
&heif->img );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
error = heif_image_add_plane( heif->img, heif_channel_interleaved,
heif->page_width, heif->page_height,
heif->bitdepth );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
#ifdef DEBUG
vips__heif_image_print( heif->img );
#endif /*DEBUG*/
heif->data = heif_image_get_plane( heif->img,
heif_channel_interleaved, &heif->stride );
/* Write data.
*/
if( vips_sink_disc( heif->image,
vips_foreign_save_heif_write_block, heif ) )
return( -1 );
/* This has to come right at the end :-( so there's no support for
* incremental writes.
*/
writer.writer_api_version = 1;
writer.write = vips_foreign_save_heif_write;
error = heif_context_write( heif->ctx, &writer, heif );
if( error.code ) {
vips__heif_error( &error );
return( -1 );
}
if( vips_target_end( heif->target ) )
return( -1 );
return( 0 );
}
#define UC VIPS_FORMAT_UCHAR
#define US VIPS_FORMAT_USHORT
/* Except for 8-bit inputs, we send everything else to 16. We decide on 8-bit
* vs. 12 bit save based on Type in_build(), see above.
*/
static VipsBandFormat vips_heif_bandfmt[10] = {
/* Band format: UC C US S UI I F X D DX */
/* Promotion: */ UC, UC, US, US, US, US, US, US, US, US
};
static void
vips_foreign_save_heif_class_init( VipsForeignSaveHeifClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class;
gobject_class->dispose = vips_foreign_save_heif_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "heifsave_base";
object_class->description = _( "save image in HEIF format" );
object_class->build = vips_foreign_save_heif_build;
save_class->saveable = VIPS_SAVEABLE_RGBA_ONLY;
save_class->format_table = vips_heif_bandfmt;
VIPS_ARG_INT( class, "Q", 10,
_( "Q" ),
_( "Q factor" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, Q ),
1, 100, 50 );
VIPS_ARG_INT( class, "bitdepth", 11,
_( "Bit depth" ),
_( "Number of bits per pixel" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, bitdepth ),
1, 16, 12 );
VIPS_ARG_BOOL( class, "lossless", 13,
_( "Lossless" ),
_( "Enable lossless compression" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, lossless ),
FALSE );
VIPS_ARG_ENUM( class, "compression", 14,
_( "Compression" ),
_( "Compression format" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, compression ),
VIPS_TYPE_FOREIGN_HEIF_COMPRESSION,
VIPS_FOREIGN_HEIF_COMPRESSION_HEVC );
VIPS_ARG_INT( class, "effort", 15,
_( "Effort" ),
_( "CPU effort" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, effort ),
0, 9, 4 );
VIPS_ARG_ENUM( class, "subsample_mode", 16,
_( "Subsample mode" ),
_( "Select chroma subsample operation mode" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, subsample_mode ),
VIPS_TYPE_FOREIGN_SUBSAMPLE,
VIPS_FOREIGN_SUBSAMPLE_AUTO );
VIPS_ARG_INT( class, "speed", 17,
_( "Speed" ),
_( "CPU effort" ),
VIPS_ARGUMENT_OPTIONAL_INPUT | VIPS_ARGUMENT_DEPRECATED,
G_STRUCT_OFFSET( VipsForeignSaveHeif, speed ),
0, 9, 5 );
VIPS_ARG_ENUM( class, "encoder", 18,
_( "Encoder" ),
_( "Select encoder to use" ),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeif, selected_encoder ),
VIPS_TYPE_FOREIGN_HEIF_ENCODER,
VIPS_FOREIGN_HEIF_ENCODER_AUTO );
}
static void
vips_foreign_save_heif_init( VipsForeignSaveHeif *heif )
{
heif->ctx = heif_context_alloc();
heif->Q = 50;
heif->bitdepth = 12;
heif->compression = VIPS_FOREIGN_HEIF_COMPRESSION_HEVC;
heif->effort = 4;
heif->subsample_mode = VIPS_FOREIGN_SUBSAMPLE_AUTO;
/* Deprecated.
*/
heif->speed = 5;
}
typedef struct _VipsForeignSaveHeifFile {
VipsForeignSaveHeif parent_object;
/* Filename for save.
*/
char *filename;
} VipsForeignSaveHeifFile;
typedef VipsForeignSaveHeifClass VipsForeignSaveHeifFileClass;
G_DEFINE_TYPE( VipsForeignSaveHeifFile, vips_foreign_save_heif_file,
vips_foreign_save_heif_get_type() );
static int
vips_foreign_save_heif_file_build( VipsObject *object )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) object;
VipsForeignSaveHeifFile *file = (VipsForeignSaveHeifFile *) object;
if( !(heif->target = vips_target_new_to_file( file->filename )) )
return( -1 );
if( vips_iscasepostfix( file->filename, ".avif" ) )
heif->compression = VIPS_FOREIGN_HEIF_COMPRESSION_AV1;
if( VIPS_OBJECT_CLASS( vips_foreign_save_heif_file_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_heif_file_class_init( VipsForeignSaveHeifFileClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "heifsave";
object_class->build = vips_foreign_save_heif_file_build;
foreign_class->suffs = vips__heif_suffs;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeifFile, filename ),
NULL );
}
static void
vips_foreign_save_heif_file_init( VipsForeignSaveHeifFile *file )
{
}
typedef struct _VipsForeignSaveHeifBuffer {
VipsForeignSaveHeif parent_object;
/* Save to a buffer.
*/
VipsArea *buf;
} VipsForeignSaveHeifBuffer;
typedef VipsForeignSaveHeifClass VipsForeignSaveHeifBufferClass;
G_DEFINE_TYPE( VipsForeignSaveHeifBuffer, vips_foreign_save_heif_buffer,
vips_foreign_save_heif_get_type() );
static int
vips_foreign_save_heif_buffer_build( VipsObject *object )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) object;
VipsForeignSaveHeifBuffer *buffer =
(VipsForeignSaveHeifBuffer *) object;
VipsBlob *blob;
if( !(heif->target = vips_target_new_to_memory()) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_foreign_save_heif_buffer_parent_class )->
build( object ) )
return( -1 );
g_object_get( heif->target, "blob", &blob, NULL );
g_object_set( buffer, "buffer", blob, NULL );
vips_area_unref( VIPS_AREA( blob ) );
return( 0 );
}
static void
vips_foreign_save_heif_buffer_class_init(
VipsForeignSaveHeifBufferClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "heifsave_buffer";
object_class->build = vips_foreign_save_heif_buffer_build;
foreign_class->suffs = vips__heic_suffs;
VIPS_ARG_BOXED( class, "buffer", 1,
_( "Buffer" ),
_( "Buffer to save to" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeifBuffer, buf ),
VIPS_TYPE_BLOB );
}
static void
vips_foreign_save_heif_buffer_init( VipsForeignSaveHeifBuffer *buffer )
{
}
typedef struct _VipsForeignSaveHeifTarget {
VipsForeignSaveHeif parent_object;
VipsTarget *target;
} VipsForeignSaveHeifTarget;
typedef VipsForeignSaveHeifClass VipsForeignSaveHeifTargetClass;
G_DEFINE_TYPE( VipsForeignSaveHeifTarget, vips_foreign_save_heif_target,
vips_foreign_save_heif_get_type() );
static int
vips_foreign_save_heif_target_build( VipsObject *object )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) object;
VipsForeignSaveHeifTarget *target =
(VipsForeignSaveHeifTarget *) object;
if( target->target ) {
heif->target = target->target;
g_object_ref( heif->target );
}
if( VIPS_OBJECT_CLASS( vips_foreign_save_heif_target_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_heif_target_class_init(
VipsForeignSaveHeifTargetClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "heifsave_target";
object_class->build = vips_foreign_save_heif_target_build;
foreign_class->suffs = vips__heic_suffs;
VIPS_ARG_OBJECT( class, "target", 1,
_( "Target" ),
_( "Target to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveHeifTarget, target ),
VIPS_TYPE_TARGET );
}
static void
vips_foreign_save_heif_target_init( VipsForeignSaveHeifTarget *target )
{
}
typedef VipsForeignSaveHeifTarget VipsForeignSaveAvifTarget;
typedef VipsForeignSaveHeifTargetClass VipsForeignSaveAvifTargetClass;
G_DEFINE_TYPE( VipsForeignSaveAvifTarget, vips_foreign_save_avif_target,
vips_foreign_save_heif_target_get_type() );
static void
vips_foreign_save_avif_target_class_init(
VipsForeignSaveAvifTargetClass *class )
{
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsOperationClass *operation_class = (VipsOperationClass *) class;
object_class->nickname = "avifsave_target";
object_class->description = _( "save image in AVIF format" );
foreign_class->suffs = vips__avif_suffs;
/* Hide from UI.
*/
operation_class->flags |= VIPS_OPERATION_DEPRECATED;
}
static void
vips_foreign_save_avif_target_init( VipsForeignSaveAvifTarget *target )
{
VipsForeignSaveHeif *heif = (VipsForeignSaveHeif *) target;
heif->compression = VIPS_FOREIGN_HEIF_COMPRESSION_AV1;
}
#endif /*HAVE_HEIF_ENCODER*/
/* The C API wrappers are defined in foreign.c.
*/
|