basic load works

no metadata, no options
This commit is contained in:
John Cupitt 2019-01-20 11:33:12 +00:00
parent dcba364efa
commit 33fd2a48f0
7 changed files with 353 additions and 4 deletions

View File

@ -19,6 +19,7 @@
- add vips_profile_load() and use it everywhere
- fix race in temp filename creation [lhecker]
- add @reduction_effort param to webpsave [lovell]
- add vips_heifload()
4/1/19 started 8.7.4
- magickload with magick6 API did not chain exceptions correctly causing a

View File

@ -889,6 +889,22 @@ if test x"$with_nifti" != x"no"; then
)
fi
# libheif
AC_ARG_WITH([heif],
AS_HELP_STRING([--without-heif], [build without libheif (default: test)]))
if test x"$with_heif" != x"no"; then
PKG_CHECK_MODULES(HEIF, libheif,
[AC_DEFINE(HAVE_HEIF,1,[define if you have libheif installed.])
with_heif=yes
PACKAGES_USED="$PACKAGES_USED libheif"
],
[AC_MSG_WARN([libheif not found; disabling HEIF support])
with_heif=no
]
)
fi
# pdfium
AC_ARG_WITH([pdfium],
AS_HELP_STRING([--without-pdfium], [build without pdfium (default: test)]))
@ -1304,14 +1320,14 @@ fi
# Gather all up for VIPS_CFLAGS, VIPS_INCLUDES, VIPS_LIBS
# sort includes to get longer, more specific dirs first
# helps, for example, selecting graphicsmagick over imagemagick
VIPS_CFLAGS=`for i in $VIPS_CFLAGS $GTHREAD_CFLAGS $REQUIRED_CFLAGS $EXPAT_CFLAGS $ZLIB_CFLAGS $PANGOFT2_CFLAGS $GSF_CFLAGS $FFTW_CFLAGS $MAGICK_CFLAGS $JPEG_CFLAGS $PNG_CFLAGS $IMAGEQUANT_CFLAGS $EXIF_CFLAGS $MATIO_CFLAGS $CFITSIO_CFLAGS $LIBWEBP_CFLAGS $LIBWEBPMUX_CFLAGS $GIFLIB_INCLUDES $RSVG_CFLAGS $PDFIUM_INCLUDES $POPPLER_CFLAGS $OPENEXR_CFLAGS $OPENSLIDE_CFLAGS $ORC_CFLAGS $TIFF_CFLAGS $LCMS_CFLAGS
VIPS_CFLAGS=`for i in $VIPS_CFLAGS $GTHREAD_CFLAGS $REQUIRED_CFLAGS $EXPAT_CFLAGS $ZLIB_CFLAGS $PANGOFT2_CFLAGS $GSF_CFLAGS $FFTW_CFLAGS $MAGICK_CFLAGS $JPEG_CFLAGS $PNG_CFLAGS $IMAGEQUANT_CFLAGS $EXIF_CFLAGS $MATIO_CFLAGS $CFITSIO_CFLAGS $LIBWEBP_CFLAGS $LIBWEBPMUX_CFLAGS $GIFLIB_INCLUDES $RSVG_CFLAGS $PDFIUM_INCLUDES $POPPLER_CFLAGS $OPENEXR_CFLAGS $OPENSLIDE_CFLAGS $ORC_CFLAGS $TIFF_CFLAGS $LCMS_CFLAGS $HEIF_CFLAGS
do
echo $i
done | sort -ru`
VIPS_CFLAGS=`echo $VIPS_CFLAGS`
VIPS_CFLAGS="$VIPS_DEBUG_FLAGS $VIPS_CFLAGS"
VIPS_INCLUDES="$ZLIB_INCLUDES $PNG_INCLUDES $TIFF_INCLUDES $JPEG_INCLUDES $NIFTI_INCLUDES"
VIPS_LIBS="$ZLIB_LIBS $MAGICK_LIBS $PNG_LIBS $IMAGEQUANT_LIBS $TIFF_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $EXPAT_LIBS $PANGOFT2_LIBS $GSF_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $GIFLIB_LIBS $RSVG_LIBS $NIFTI_LIBS $PDFIUM_LIBS $POPPLER_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $LIBWEBP_LIBS $LIBWEBPMUX_LIBS $MATIO_LIBS $EXIF_LIBS -lm"
VIPS_LIBS="$ZLIB_LIBS $HEIF_LIBS $MAGICK_LIBS $PNG_LIBS $IMAGEQUANT_LIBS $TIFF_LIBS $JPEG_LIBS $GTHREAD_LIBS $REQUIRED_LIBS $EXPAT_LIBS $PANGOFT2_LIBS $GSF_LIBS $FFTW_LIBS $ORC_LIBS $LCMS_LIBS $GIFLIB_LIBS $RSVG_LIBS $NIFTI_LIBS $PDFIUM_LIBS $POPPLER_LIBS $OPENEXR_LIBS $OPENSLIDE_LIBS $CFITSIO_LIBS $LIBWEBP_LIBS $LIBWEBPMUX_LIBS $MATIO_LIBS $EXIF_LIBS -lm"
AC_SUBST(VIPS_LIBDIR)
@ -1404,6 +1420,7 @@ accelerate loops with orc: $with_orc
(requires orc-0.4.11 or later)
ICC profile support with lcms: $with_lcms
file import with niftiio: $with_nifti
file import with libheif: $with_heif
file import with OpenEXR: $with_OpenEXR
file import with OpenSlide: $with_openslide
(requires openslide-3.3.0 or later)

View File

@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libforeign.la
libforeign_la_SOURCES = \
pforeign.h \
heifload.c \
niftiload.c \
niftisave.c \
quantise.c \

View File

@ -1827,6 +1827,7 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_load_svg_get_type( void );
extern GType vips_foreign_load_svg_file_get_type( void );
extern GType vips_foreign_load_svg_buffer_get_type( void );
extern GType vips_foreign_load_heif_get_type( void );
extern GType vips_foreign_load_nifti_get_type( void );
extern GType vips_foreign_save_nifti_get_type( void );
extern GType vips_foreign_load_gif_get_type( void );
@ -1956,6 +1957,10 @@ vips_foreign_operation_init( void )
vips_foreign_save_nifti_get_type();
#endif /*HAVE_NIFTI*/
#ifdef HAVE_HEIF
vips_foreign_load_heif_get_type();
#endif /*HAVE_HEIF*/
vips__foreign_load_operation =
g_quark_from_static_string( "vips-foreign-load-operation" );
}

322
libvips/foreign/heifload.c Normal file
View File

@ -0,0 +1,322 @@
/* load heif images with libheif
*
* 19/1/19
* - from heifload.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., 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
#define VIPS_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/debug.h>
#include <vips/internal.h>
#ifdef HAVE_HEIF
#include <libheif/heif.h>
#include "pforeign.h"
typedef struct _VipsForeignLoadHeif {
VipsForeignLoad parent_object;
/* Filename for load.
*/
char *filename;
struct heif_context *ctx;
struct heif_image_handle *handle;
struct heif_image *img;
/* Valid until img is released.
*/
int stride;
const uint8_t *data;
/* Our intermediate image.
*/
VipsImage *memory;
} VipsForeignLoadHeif;
typedef VipsForeignLoadClass VipsForeignLoadHeifClass;
G_DEFINE_TYPE( VipsForeignLoadHeif, vips_foreign_load_heif,
VIPS_TYPE_FOREIGN_LOAD );
static void
vips_foreign_load_heif_dispose( GObject *gobject )
{
VipsForeignLoadHeif *heif = (VipsForeignLoadHeif *) gobject;
VIPS_FREEF( heif_image_release, heif->img );
VIPS_FREEF( heif_image_handle_release, heif->handle );
VIPS_FREEF( heif_context_free, heif->ctx );
VIPS_UNREF( heif->memory );
G_OBJECT_CLASS( vips_foreign_load_heif_parent_class )->
dispose( gobject );
}
static void
vips_heif_error( struct heif_error error )
{
if( error.code )
vips_error( "heifload", "%s", error.message );
}
/* More recent libheif have this in the API, but we need to work with older
* versions too.
*/
static const char *vips_foreign_load_heif_magic[] = {
"ftypheic",
"ftypheix",
"ftyphevc",
"ftypheim",
"ftypheis",
"ftyphevm",
"ftyphevs"
};
static int
vips_foreign_load_heif_is_a( const char *filename )
{
unsigned char buf[15];
int i;
if( vips__get_bytes( filename, buf, 15 ) != 15 )
return( 0 );
for( i = 0; i < VIPS_NUMBER( vips_foreign_load_heif_magic ); i++ )
if( strncmp( (char *) buf + 4,
vips_foreign_load_heif_magic[i], 8 ) == 0 )
return( 1 );
return( 0 );
}
/* Read the primary image header into @out.
*/
static int
vips_foreign_load_heif_set_header( VipsForeignLoadHeif *heif, VipsImage *out )
{
guint width;
guint height;
gboolean has_alpha;
guint bands;
width = heif_image_handle_get_width( heif->handle );
height = heif_image_handle_get_height( heif->handle );
has_alpha = heif_image_handle_has_alpha_channel( heif->handle );
bands = has_alpha ? 4 : 3;
vips_image_pipelinev( out, VIPS_DEMAND_STYLE_SMALLTILE, NULL );
vips_image_init_fields( out,
width, height, bands, VIPS_FORMAT_UCHAR,
VIPS_CODING_NONE, VIPS_INTERPRETATION_sRGB,
1.0, 1.0 );
return( 0 );
}
static int
vips_foreign_load_heif_header( VipsForeignLoad *load )
{
VipsForeignLoadHeif *heif = (VipsForeignLoadHeif *) load;
struct heif_error error;
error = heif_context_read_from_file( heif->ctx, heif->filename, NULL );
if( error.code ) {
vips_heif_error( error );
return( -1 );
}
error = heif_context_get_primary_image_handle( heif->ctx,
&heif->handle );
if( error.code ) {
vips_heif_error( error );
return( -1 );
}
if( vips_foreign_load_heif_set_header( heif, load->out ) )
return( -1 );
VIPS_SETSTR( load->out->filename, heif->filename );
return( 0 );
}
static int
vips_foreign_load_heif_load( VipsForeignLoad *load )
{
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( load );
VipsForeignLoadHeif *heif = (VipsForeignLoadHeif *) load;
struct heif_error error;
#ifdef DEBUG
printf( "vips_foreign_load_heif_load: loading image\n" );
#endif /*DEBUG*/
/* Decode the image and convert colorspace to RGB, saved as 24bit
* interleaved.
*
* FIXME What will this do for RGBA?
*/
error = heif_decode_image( heif->handle, &heif->img,
heif_colorspace_RGB, heif_chroma_interleaved_24bit, NULL );
if( error.code ) {
vips_heif_error( error );
return( -1 );
}
if( !(heif->data = heif_image_get_plane_readonly( heif->img,
heif_channel_interleaved, &heif->stride )) ) {
vips_error( class->nickname,
"%s", _( "unable to get image data" ) );
return( -1 );
}
if( VIPS_IMAGE_SIZEOF_LINE( load->out ) == heif->stride ) {
printf( "heifload: copying pointer .. \n" );
/* libheif has decoded to a contigious memory area. We can
* just wrap an image around it.
*/
if( !(heif->memory = vips_image_new_from_memory(
heif->data, VIPS_IMAGE_SIZEOF_IMAGE( load->out ),
load->out->Xsize, load->out->Ysize,
load->out->Bands, load->out->BandFmt )) )
return( -1 );
}
else {
/* Non-contigious memory area. We must copy the data,
*/
int y;
printf( "heifload: copying data .. \n" );
printf( " stride = %d, sizeof_line = %zd\n",
heif->stride, VIPS_IMAGE_SIZEOF_LINE( load->out ) );
heif->memory = vips_image_new_memory();
if( vips_foreign_load_heif_set_header( heif, heif->memory ) ||
vips_image_write_prepare( heif->memory ) )
return( -1 );
for( y = 0; y < heif->memory->Ysize; y++ )
memcpy( VIPS_IMAGE_ADDR( heif->memory, 0, y ),
heif->data + heif->stride * y,
VIPS_IMAGE_SIZEOF_LINE( heif->memory ) );
}
if( vips_image_write( heif->memory, load->real ) )
return( -1 );
return( 0 );
}
const char *vips__heif_suffs[] = {
".heic",
NULL
};
static void
vips_foreign_load_heif_class_init( VipsForeignLoadHeifClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
gobject_class->dispose = vips_foreign_load_heif_dispose;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "heifload";
object_class->description = _( "load a HEIF image" );
foreign_class->suffs = vips__heif_suffs;
load_class->is_a = vips_foreign_load_heif_is_a;
load_class->header = vips_foreign_load_heif_header;
load_class->load = vips_foreign_load_heif_load;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to load from" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignLoadHeif, filename ),
NULL );
}
static void
vips_foreign_load_heif_init( VipsForeignLoadHeif *heif )
{
heif->ctx = heif_context_alloc();
}
#endif /*HAVE_HEIF*/
/**
* vips_heifload:
* @filename: file to load
* @out: (out): decompressed image
* @...: %NULL-terminated list of optional named arguments
*
* Read a HEIF image file into a VIPS image.
*
* See also: vips_image_new_from_file().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_heifload( const char *filename, VipsImage **out, ... )
{
va_list ap;
int result;
va_start( ap, out );
result = vips_call_split( "heifload", ap, filename, out );
va_end( ap );
return( result );
}

View File

@ -565,7 +565,7 @@ vips_foreign_load_nifti_load( VipsForeignLoad *load )
load->out->Bands, load->out->BandFmt )) )
return( -1 );
if( vips_copy( nifti->memory, &load->real, NULL ) )
if( vips_image_write( nifti->memory, load->real ) )
return( -1 );
return( 0 );
@ -617,7 +617,7 @@ vips_foreign_load_nifti_init( VipsForeignLoadNifti *nifti )
{
}
#endif /*HAVE_CFITSIO*/
#endif /*HAVE_NIFTI*/
/**
* vips_niftiload:

View File

@ -560,6 +560,9 @@ int vips_gifload( const char *filename, VipsImage **out, ... )
int vips_gifload_buffer( void *buf, size_t len, VipsImage **out, ... )
__attribute__((sentinel));
int vips_heifload( const char *filename, VipsImage **out, ... )
__attribute__((sentinel));
int vips_niftiload( const char *filename, VipsImage **out, ... )
__attribute__((sentinel));
int vips_niftisave( VipsImage *in, const char *filename, ... )