Merge branch 'master' of github.com:libvips/libvips

This commit is contained in:
John Cupitt 2021-05-01 14:48:25 +01:00
commit 33812d79ad
15 changed files with 1013 additions and 318 deletions

View File

@ -21,7 +21,8 @@
- png and gif load note background colour as metadata [781545872] - png and gif load note background colour as metadata [781545872]
- add vips_image_[set|get]_array_double() - add vips_image_[set|get]_array_double()
- add GIF load with libnsgif - add GIF load with libnsgif
- add JPEG2000 load and save - add jp2kload, jp2ksave
- add jp2k compression to tiff load and save
- add JPEG-XL load and save - add JPEG-XL load and save
- add black_point_compensation flag for icc transforms - add black_point_compensation flag for icc transforms
- add "rgba" flag to vips_text() to enable full colour text rendering - add "rgba" flag to vips_text() to enable full colour text rendering

View File

@ -836,10 +836,11 @@ AC_ARG_WITH([libopenjp2],
AS_HELP_STRING([--without-libopenjp2], AS_HELP_STRING([--without-libopenjp2],
[build without libopenjp2 (default: test)])) [build without libopenjp2 (default: test)]))
# 2.4 is the first one to have working threading and tiling
if test x"$with_libopenjp2" != x"no"; then if test x"$with_libopenjp2" != x"no"; then
PKG_CHECK_MODULES(LIBOPENJP2, libopenjp2 >= 2.3, PKG_CHECK_MODULES(LIBOPENJP2, libopenjp2 >= 2.4,
[AC_DEFINE(HAVE_LIBOPENJP2,1, [AC_DEFINE(HAVE_LIBOPENJP2,1,
[define if you have libopenjp2 >=2.2 installed.]) [define if you have libopenjp2 >=2.4 installed.])
with_libopenjp2=yes with_libopenjp2=yes
PACKAGES_USED="$PACKAGES_USED libopenjp2" PACKAGES_USED="$PACKAGES_USED libopenjp2"
], ],
@ -847,14 +848,6 @@ if test x"$with_libopenjp2" != x"no"; then
with_libopenjp2=no with_libopenjp2=no
] ]
) )
# 2.3 and earlier have threading problems
PKG_CHECK_MODULES(LIBOPENJP2_THREADING, libopenjp2 >= 2.4,
[AC_DEFINE(HAVE_LIBOPENJP2_THREADING,1,[define if your libopenjp2 threading works.])
],
[:
]
)
fi fi
VIPS_CFLAGS="$VIPS_CFLAGS $LIBOPENJP2_CFLAGS" VIPS_CFLAGS="$VIPS_CFLAGS $LIBOPENJP2_CFLAGS"
@ -1501,6 +1494,7 @@ AC_CONFIG_FILES([
libvips/histogram/Makefile libvips/histogram/Makefile
libvips/draw/Makefile libvips/draw/Makefile
libvips/iofuncs/Makefile libvips/iofuncs/Makefile
libvips/module/Makefile
libvips/morphology/Makefile libvips/morphology/Makefile
libvips/mosaicing/Makefile libvips/mosaicing/Makefile
libvips/create/Makefile libvips/create/Makefile
@ -1555,7 +1549,7 @@ EXIF metadata support with libexif: $with_libexif
JPEG load/save with libjpeg: $with_jpeg JPEG load/save with libjpeg: $with_jpeg
JXL load/save with libjxl: $with_libjxl JXL load/save with libjxl: $with_libjxl
JPEG2000 load/save with libopenjp2: $with_libopenjp2 JPEG2000 load/save with libopenjp2: $with_libopenjp2
(requires libopenjp2 2.2 or later) (requires libopenjp2 2.4 or later)
PNG load with libspng: $with_libspng PNG load with libspng: $with_libspng
(requires libspng-0.6 or later) (requires libspng-0.6 or later)
PNG load/save with libpng: $with_png PNG load/save with libpng: $with_png
@ -1574,8 +1568,6 @@ PDF load with poppler-glib: $with_poppler (dynamic module: $with_pop
SVG load with librsvg-2.0: $with_rsvg SVG load with librsvg-2.0: $with_rsvg
(requires librsvg-2.0 2.34.0 or later) (requires librsvg-2.0 2.34.0 or later)
EXR load with OpenEXR: $with_OpenEXR EXR load with OpenEXR: $with_OpenEXR
JPEG2000 load/save with libopenjp2: $with_libopenjp2
(requires libopenjp2 2.2 or later)
slide load with OpenSlide: $with_openslide (dynamic module: $with_openslide_module) slide load with OpenSlide: $with_openslide (dynamic module: $with_openslide_module)
(requires openslide-3.3.0 or later) (requires openslide-3.3.0 or later)
Matlab load with matio: $with_matio Matlab load with matio: $with_matio

View File

@ -27,6 +27,7 @@ SUBDIRS = \
draw \ draw \
iofuncs \ iofuncs \
morphology \ morphology \
module \
mosaicing \ mosaicing \
create create
@ -61,7 +62,6 @@ libvips_la_LDFLAGS = \
-version-info @LIBRARY_CURRENT@:@LIBRARY_REVISION@:@LIBRARY_AGE@ -version-info @LIBRARY_CURRENT@:@LIBRARY_REVISION@:@LIBRARY_AGE@
EXTRA_DIST = \ EXTRA_DIST = \
module \
$(OPTIONAL_DIST_DIR) $(OPTIONAL_DIST_DIR)
CLEANFILES = CLEANFILES =

View File

@ -1,6 +1,4 @@
if ENABLE_NSGIF
SUBDIRS = libnsgif SUBDIRS = libnsgif
endif
noinst_LTLIBRARIES = libforeign.la noinst_LTLIBRARIES = libforeign.la
@ -94,7 +92,5 @@ if !OPENSLIDE_MODULE
libforeign_la_SOURCES += openslide2vips.c libforeign_la_SOURCES += openslide2vips.c
endif # !OPENSLIDE_MODULE endif # !OPENSLIDE_MODULE
EXTRA_DIST = libnsgif
AM_CPPFLAGS = -I${top_srcdir}/libvips/include @VIPS_CFLAGS@ @VIPS_INCLUDES@ AM_CPPFLAGS = -I${top_srcdir}/libvips/include @VIPS_CFLAGS@ @VIPS_INCLUDES@

View File

@ -274,10 +274,6 @@ vips_foreign_load_jp2k_error_callback( const char *msg, void *client )
vips_error( class->nickname, "%s", msg ); vips_error( class->nickname, "%s", msg );
jp2k->n_errors += 1; jp2k->n_errors += 1;
#ifdef DEBUG
printf( "%s: error %s", class->nickname, msg );
#endif /*DEBUG*/
} }
/* The openjpeg info and warning callbacks are incredibly chatty. /* The openjpeg info and warning callbacks are incredibly chatty.
@ -500,12 +496,7 @@ vips_foreign_load_jp2k_header( VipsForeignLoad *load )
if( !opj_setup_decoder( jp2k->codec, &jp2k->parameters ) ) if( !opj_setup_decoder( jp2k->codec, &jp2k->parameters ) )
return( -1 ); return( -1 );
#ifdef HAVE_LIBOPENJP2_THREADING
/* Use eg. VIPS_CONCURRENCY etc. to set n-cpus, if this openjpeg has
* stable support.
*/
opj_codec_set_threads( jp2k->codec, vips_concurrency_get() ); opj_codec_set_threads( jp2k->codec, vips_concurrency_get() );
#endif /*HAVE_LIBOPENJP2_THREADING*/
if( !opj_read_header( jp2k->stream, jp2k->codec, &jp2k->image ) ) if( !opj_read_header( jp2k->stream, jp2k->codec, &jp2k->image ) )
return( -1 ); return( -1 );
@ -586,7 +577,7 @@ vips_foreign_load_jp2k_header( VipsForeignLoad *load )
\ \
for( x = 0; x < length; x++ ) { \ for( x = 0; x < length; x++ ) { \
for( i = 0; i < b; i++ ) { \ for( i = 0; i < b; i++ ) { \
int dx = jp2k->image->comps[i].dx; \ int dx = image->comps[i].dx; \
int pixel = planes[i][x / dx]; \ int pixel = planes[i][x / dx]; \
\ \
tq[i] = pixel; \ tq[i] = pixel; \
@ -596,28 +587,37 @@ vips_foreign_load_jp2k_header( VipsForeignLoad *load )
} \ } \
} }
/* Pack the set of openjpeg components into a libvips region. left/top are the /* Pack a line of openjpeg pixels into libvips format. left/top are the
* offsets into the tile in pixel coordinates where we should start reading. * offsets into the opj image in pixel coordinates where we should start
* reading.
*
* Set upsample if any opj component is subsampled.
*/ */
static void static void
vips_foreign_load_jp2k_pack( VipsForeignLoadJp2k *jp2k, vips_foreign_load_jp2k_pack( gboolean upsample,
VipsImage *image, VipsPel *q, opj_image_t *image, VipsImage *im,
int left, int top, int length ) VipsPel *q, int left, int top, int length )
{ {
int *planes[MAX_BANDS]; int *planes[MAX_BANDS];
int b = jp2k->image->numcomps; int b = image->numcomps;
int x, i; int x, i;
#ifdef DEBUG_VERBOSE
printf( "vips_foreign_load_jp2k_pack: "
"upsample = %d, left = %d, top = %d, length = %d\n",
upsample, left, top, length );
#endif /*DEBUG_VERBOSE*/
for( i = 0; i < b; i++ ) { for( i = 0; i < b; i++ ) {
opj_image_comp_t *comp = &jp2k->image->comps[i]; opj_image_comp_t *comp = &image->comps[i];
planes[i] = comp->data + (top / comp->dy) * comp->w + planes[i] = comp->data + (top / comp->dy) * comp->w +
(left / comp->dx); (left / comp->dx);
} }
if( jp2k->upsample ) if( upsample )
switch( image->BandFmt ) { switch( im->BandFmt ) {
case VIPS_FORMAT_CHAR: case VIPS_FORMAT_CHAR:
case VIPS_FORMAT_UCHAR: case VIPS_FORMAT_UCHAR:
PACK_UPSAMPLE( unsigned char ); PACK_UPSAMPLE( unsigned char );
@ -640,7 +640,7 @@ vips_foreign_load_jp2k_pack( VipsForeignLoadJp2k *jp2k,
else else
/* Fast no-upsample path. /* Fast no-upsample path.
*/ */
switch( image->BandFmt ) { switch( im->BandFmt ) {
case VIPS_FORMAT_CHAR: case VIPS_FORMAT_CHAR:
case VIPS_FORMAT_UCHAR: case VIPS_FORMAT_UCHAR:
PACK( unsigned char ); PACK( unsigned char );
@ -692,17 +692,16 @@ vips_foreign_load_jp2k_pack( VipsForeignLoadJp2k *jp2k,
/* YCC->RGB for a line of pels. /* YCC->RGB for a line of pels.
*/ */
static void static void
vips_foreign_load_jp2k_ycc_to_rgb( VipsForeignLoadJp2k *jp2k, vips_foreign_load_jp2k_ycc_to_rgb( opj_image_t *image, VipsImage *im,
VipsPel *q, int length ) VipsPel *q, int length )
{ {
VipsForeignLoad *load = (VipsForeignLoad *) jp2k; int prec = image->comps[0].prec;
int prec = jp2k->image->comps[0].prec;
int offset = 1 << (prec - 1); int offset = 1 << (prec - 1);
int upb = (1 << prec) - 1; int upb = (1 << prec) - 1;
int x; int x;
switch( load->out->BandFmt ) { switch( im->BandFmt ) {
case VIPS_FORMAT_CHAR: case VIPS_FORMAT_CHAR:
case VIPS_FORMAT_UCHAR: case VIPS_FORMAT_UCHAR:
YCC_TO_RGB( unsigned char ); YCC_TO_RGB( unsigned char );
@ -806,15 +805,16 @@ vips_foreign_load_jp2k_generate( VipsRegion *out,
VipsPel *q = VIPS_REGION_ADDR( out, VipsPel *q = VIPS_REGION_ADDR( out,
hit.left, hit.top + z ); hit.left, hit.top + z );
vips_foreign_load_jp2k_pack( jp2k, vips_foreign_load_jp2k_pack( jp2k->upsample,
out->im, q, jp2k->image, out->im, q,
hit.left - tile.left, hit.left - tile.left,
hit.top - tile.top + z, hit.top - tile.top + z,
hit.width ); hit.width );
if( jp2k->ycc_to_rgb ) if( jp2k->ycc_to_rgb )
vips_foreign_load_jp2k_ycc_to_rgb( jp2k, vips_foreign_load_jp2k_ycc_to_rgb(
q, hit.width ); jp2k->image, out->im, q,
hit.width );
} }
x += hit.width; x += hit.width;
@ -1132,6 +1132,142 @@ vips_foreign_load_jp2k_source_init(
{ {
} }
static void
warning_callback( const char *msg G_GNUC_UNUSED, void *data G_GNUC_UNUSED )
{
/* There are a lot of warnings ...
*/
}
static void
error_callback( const char *msg, void *data )
{
printf( "OpenJPEG: %s", msg );
vips_error( "OpenJPEG", "%s", msg );
}
typedef struct _TileDecompress {
VipsSource *source;
opj_stream_t *stream;
opj_codec_t *codec;
opj_image_t *image;
} TileDecompress;
static void
vips__foreign_load_jp2k_decompress_free( TileDecompress *decompress )
{
VIPS_FREEF( opj_destroy_codec, decompress->codec );
VIPS_FREEF( opj_image_destroy, decompress->image );
VIPS_FREEF( opj_stream_destroy, decompress->stream );
VIPS_UNREF( decompress->source );
}
/* Called from tiff2vips to decode a jp2k-compressed tile.
*
* width/height is the tile size. If this is an edge tile, and smaller than
* this, we still write a full-size tile and our caller will clip.
*/
int
vips__foreign_load_jp2k_decompress( VipsImage *out,
int width, int height, gboolean ycc_to_rgb,
void *from, size_t from_length,
void *to, size_t to_length )
{
size_t pel_size = VIPS_IMAGE_SIZEOF_PEL( out );
size_t line_size = pel_size * width;
TileDecompress decompress = { 0 };
opj_dparameters_t parameters;
int i;
gboolean upsample;
VipsPel *q;
int y;
#ifdef DEBUG
printf( "vips__foreign_load_jp2k_decompress: width = %d, height = %d, "
"ycc_to_rgb = %d, from_length = %zd, to_length = %zd\n",
width, height, ycc_to_rgb, from_length, to_length );
#endif /*DEBUG*/
/* Our ycc->rgb only works for exactly 3 bands.
*/
ycc_to_rgb = ycc_to_rgb && out->Bands == 3;
decompress.codec = opj_create_decompress( OPJ_CODEC_J2K );
opj_set_default_decoder_parameters( &parameters );
opj_setup_decoder( decompress.codec, &parameters );
opj_set_warning_handler( decompress.codec, warning_callback, NULL );
opj_set_error_handler( decompress.codec, error_callback, NULL );
decompress.source = vips_source_new_from_memory( from, from_length );
decompress.stream = vips_foreign_load_jp2k_stream( decompress.source );
if( !opj_read_header( decompress.stream,
decompress.codec, &decompress.image ) ) {
vips_error( "jp2kload", "%s", ( "header error" ) );
vips__foreign_load_jp2k_decompress_free( &decompress );
return( -1 );
}
if( decompress.image->x1 > width ||
decompress.image->y1 > height ||
line_size * height > to_length ) {
vips_error( "jp2kload", "%s", ( "bad dimensions" ) );
vips__foreign_load_jp2k_decompress_free( &decompress );
return( -1 );
}
if( !opj_decode( decompress.codec,
decompress.stream, decompress.image ) ) {
vips_error( "jp2kload", "%s", ( "decode error" ) );
vips__foreign_load_jp2k_decompress_free( &decompress );
return( -1 );
}
/* Do any components need upsampling?
*/
upsample = FALSE;
for( i = 0; i < decompress.image->numcomps; i++ ) {
opj_image_comp_t *this = &decompress.image->comps[i];
if( this->dx > 1 ||
this->dy > 1 )
upsample = TRUE;
}
/* Unpack hit pixels to buffer in vips layout.
*/
q = to;
for( y = 0; y < height; y++ ) {
vips_foreign_load_jp2k_pack( upsample,
decompress.image, out, q,
0, y, width );
if( ycc_to_rgb )
vips_foreign_load_jp2k_ycc_to_rgb(
decompress.image, out, q,
width );
q += line_size;
}
vips__foreign_load_jp2k_decompress_free( &decompress );
return( 0 );
}
#else /*!HAVE_LIBOPENJP2*/
int
vips__foreign_load_jp2k_decompress( VipsImage *out,
int width, int height, gboolean ycc_to_rgb,
void *from, size_t from_length,
void *to, size_t to_length )
{
vips_error( "jp2k",
"%s", _( "libvips built without JPEG2000 support" ) );
return( -1 );
}
#endif /*HAVE_LIBOPENJP2*/ #endif /*HAVE_LIBOPENJP2*/
/** /**

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,15 @@
noinst_LTLIBRARIES = libnsgif.la
MY_SOURCES = \
libnsgif.h \
libnsgif.c \
lzw.c \
lzw.h
if ENABLE_NSGIF
libnsgif_la_SOURCES = $(MY_SOURCES)
endif
EXTRA_DIST = \ EXTRA_DIST = \
README-ns \ README-ns \
README.md \ README.md \
@ -5,11 +17,7 @@ EXTRA_DIST = \
update.sh \ update.sh \
utils utils
noinst_LTLIBRARIES = libnsgif.la if !ENABLE_NSGIF
EXTRA_DIST += \
libnsgif_la_SOURCES = \ $(MY_SOURCES)
libnsgif.h \ endif
libnsgif.c \
lzw.c \
lzw.h

View File

@ -237,6 +237,14 @@ struct heif_image;
void vips__heif_image_print( struct heif_image *img ); void vips__heif_image_print( struct heif_image *img );
extern const char *vips__jp2k_suffs[]; extern const char *vips__jp2k_suffs[];
int vips__foreign_load_jp2k_decompress( VipsImage *out,
int width, int height, gboolean ycc_to_rgb,
void *from, size_t from_length,
void *to, size_t to_length );
int vips__foreign_load_jp2k_compress( VipsRegion *region,
VipsRect *tile, VipsTarget *target,
int tile_width, int tile_height,
gboolean save_as_ycc, gboolean subsample, gboolean lossless, int Q );
extern const char *vips__jxl_suffs[]; extern const char *vips__jxl_suffs[];

View File

@ -201,6 +201,8 @@
* - add subifd * - add subifd
* 6/6/20 MathemanFlo * 6/6/20 MathemanFlo
* - support 2 and 4 bit greyscale load * - support 2 and 4 bit greyscale load
* 27/3/21
* - add jp2k decompresion
*/ */
/* /*
@ -253,6 +255,23 @@
#include "pforeign.h" #include "pforeign.h"
#include "tiff.h" #include "tiff.h"
/* Aperio TIFFs (svs) use these compression types for jp2k-compressed tiles.
*/
#define JP2K_YCC 33003
#define JP2K_RGB 33005
/* Bioformats uses this tag for jp2k compressed tiles.
*/
#define JP2K_LOSSY 33004
/* Compression types we handle ourselves.
*/
static int rtiff_we_decompress[] = {
JP2K_YCC,
JP2K_RGB,
JP2K_LOSSY
};
/* What we read from the tiff dir to set our read strategy. For multipage /* What we read from the tiff dir to set our read strategy. For multipage
* read, we need to read and compare lots of these, so it needs to be broken * read, we need to read and compare lots of these, so it needs to be broken
* out as a separate thing. * out as a separate thing.
@ -316,6 +335,11 @@ typedef struct _RtiffHeader {
*/ */
char *image_description; char *image_description;
/* TRUE if the compression type is not supported by libtiff directly
* and we must read the raw data and decompress ourselves.
*/
gboolean we_decompress;
} RtiffHeader; } RtiffHeader;
/* Scanline-type process function. /* Scanline-type process function.
@ -371,6 +395,12 @@ typedef struct _Rtiff {
*/ */
tdata_t contig_buf; tdata_t contig_buf;
/* If we are decompressing, we need a buffer to read the raw tile to
* before running the decompressor.
*/
tdata_t compressed_buf;
tsize_t compressed_buf_length;
/* The Y we are reading at. Used to verify strip read is sequential. /* The Y we are reading at. Used to verify strip read is sequential.
*/ */
int y_pos; int y_pos;
@ -1720,12 +1750,47 @@ static int
rtiff_read_tile( Rtiff *rtiff, tdata_t *buf, int x, int y ) rtiff_read_tile( Rtiff *rtiff, tdata_t *buf, int x, int y )
{ {
#ifdef DEBUG_VERBOSE #ifdef DEBUG_VERBOSE
printf( "rtiff_read_tile: x = %d, y = %d\n", x, y ); printf( "rtiff_read_tile: x = %d, y = %d, we_decompress = %d\n",
x, y, rtiff->header.we_decompress );
#endif /*DEBUG_VERBOSE*/ #endif /*DEBUG_VERBOSE*/
if( TIFFReadTile( rtiff->tiff, buf, x, y, 0, 0 ) < 0 ) { if( rtiff->header.we_decompress ) {
vips_foreign_load_invalidate( rtiff->out ); ttile_t tile_no = TIFFComputeTile( rtiff->tiff, x, y, 0, 0 );
return( -1 );
tsize_t size;
size = TIFFReadRawTile( rtiff->tiff, tile_no,
rtiff->compressed_buf, rtiff->compressed_buf_length );
if( size <= 0 ) {
vips_foreign_load_invalidate( rtiff->out );
return( -1 );
}
switch( rtiff->header.compression ) {
case JP2K_YCC:
case JP2K_RGB:
case JP2K_LOSSY:
if( vips__foreign_load_jp2k_decompress(
rtiff->out,
rtiff->header.tile_width,
rtiff->header.tile_height,
TRUE,
rtiff->compressed_buf, size,
buf, rtiff->header.tile_size ) )
return( -1 );
break;
default:
g_assert_not_reached();
break;
}
}
else {
if( TIFFReadTile( rtiff->tiff, buf, x, y, 0, 0 ) < 0 ) {
vips_foreign_load_invalidate( rtiff->out );
return( -1 );
}
} }
return( 0 ); return( 0 );
@ -1982,6 +2047,20 @@ rtiff_read_tilewise( Rtiff *rtiff, VipsImage *out )
return( -1 ); return( -1 );
} }
/* If we will be decompressing, we need a buffer large enough to hold
* the largest compressed tile in any page.
*
* Allocate a buffer 2x the uncompressed tile size ... much simpler
* than searching every page for the largest tile with
* TIFFTAG_TILEBYTECOUNTS.
*/
if( rtiff->header.we_decompress ) {
rtiff->compressed_buf_length = 2 * rtiff->header.tile_size;
if( !(rtiff->compressed_buf = vips_malloc( VIPS_OBJECT( out ),
rtiff->compressed_buf_length )) )
return( -1 );
}
/* Read to this image, then cache to out, see below. /* Read to this image, then cache to out, see below.
*/ */
t[0] = vips_image_new(); t[0] = vips_image_new();
@ -2355,6 +2434,7 @@ rtiff_read_stripwise( Rtiff *rtiff, VipsImage *out )
static int static int
rtiff_header_read( Rtiff *rtiff, RtiffHeader *header ) rtiff_header_read( Rtiff *rtiff, RtiffHeader *header )
{ {
int i;
uint16 extra_samples_count; uint16 extra_samples_count;
uint16 *extra_samples_types; uint16 *extra_samples_types;
toff_t *subifd_offsets; toff_t *subifd_offsets;
@ -2377,6 +2457,19 @@ rtiff_header_read( Rtiff *rtiff, RtiffHeader *header )
TIFFGetFieldDefaulted( rtiff->tiff, TIFFGetFieldDefaulted( rtiff->tiff,
TIFFTAG_COMPRESSION, &header->compression ); TIFFTAG_COMPRESSION, &header->compression );
/* One of the types we decompress?
*/
for( i = 0; i < VIPS_NUMBER( rtiff_we_decompress ); i++ )
if( header->compression == rtiff_we_decompress[i] ) {
#ifdef DEBUG
printf( "rtiff_header_read: "
"compression %d handled by us\n",
header->compression );
#endif /*DEBUG*/
header->we_decompress = TRUE;
break;
}
/* We must set this here since it'll change the value of scanline_size. /* We must set this here since it'll change the value of scanline_size.
*/ */
rtiff_set_decode_format( rtiff ); rtiff_set_decode_format( rtiff );
@ -2471,6 +2564,8 @@ rtiff_header_read( Rtiff *rtiff, RtiffHeader *header )
*/ */
header->tiled = TIFFIsTiled( rtiff->tiff ); header->tiled = TIFFIsTiled( rtiff->tiff );
#ifdef DEBUG #ifdef DEBUG
printf( "rtiff_header_read: header.width = %d\n", printf( "rtiff_header_read: header.width = %d\n",
header->width ); header->width );
@ -2652,6 +2747,7 @@ rtiff_header_equal( RtiffHeader *h1, RtiffHeader *h2 )
h1->photometric_interpretation != h1->photometric_interpretation !=
h2->photometric_interpretation || h2->photometric_interpretation ||
h1->sample_format != h2->sample_format || h1->sample_format != h2->sample_format ||
h1->compression != h2->compression ||
h1->separate != h2->separate || h1->separate != h2->separate ||
h1->tiled != h2->tiled || h1->tiled != h2->tiled ||
h1->orientation != h2->orientation ) h1->orientation != h2->orientation )

View File

@ -270,6 +270,16 @@
*/ */
#define MAX_ALPHA (64) #define MAX_ALPHA (64)
/* Bioformats uses this tag for lossy jp2k compressed tiles.
*/
#define JP2K_LOSSY 33004
/* Compression types we handle ourselves.
*/
static int wtiff_we_compress[] = {
JP2K_LOSSY
};
typedef struct _Layer Layer; typedef struct _Layer Layer;
typedef struct _Wtiff Wtiff; typedef struct _Wtiff Wtiff;
@ -351,7 +361,7 @@ struct _Wtiff {
int strip; /* Don't write metadata */ int strip; /* Don't write metadata */
VipsRegionShrink region_shrink; /* How to shrink regions */ VipsRegionShrink region_shrink; /* How to shrink regions */
int level; /* zstd compression level */ int level; /* zstd compression level */
gboolean lossless; /* webp lossless mode */ gboolean lossless; /* lossless mode */
VipsForeignDzDepth depth; /* Pyr depth */ VipsForeignDzDepth depth; /* Pyr depth */
gboolean subifd; /* Write pyr layers into subifds */ gboolean subifd; /* Write pyr layers into subifds */
@ -368,6 +378,16 @@ struct _Wtiff {
* roll mode. * roll mode.
*/ */
int image_height; int image_height;
/* TRUE if the compression type is not supported by libtiff directly
* and we must compress ourselves.
*/
gboolean we_compress;
/* If we are copying, we need a buffer to read the compressed tile to.
*/
tdata_t compressed_buf;
tsize_t compressed_buf_length;
}; };
/* Write an ICC Profile from a file into the JPEG stream. /* Write an ICC Profile from a file into the JPEG stream.
@ -636,6 +656,7 @@ wtiff_write_header( Wtiff *wtiff, Layer *layer )
{ {
TIFF *tif = layer->tif; TIFF *tif = layer->tif;
int i;
int orientation; int orientation;
#ifdef DEBUG #ifdef DEBUG
@ -672,6 +693,12 @@ wtiff_write_header( Wtiff *wtiff, Layer *layer )
wtiff->predictor != VIPS_FOREIGN_TIFF_PREDICTOR_NONE ) wtiff->predictor != VIPS_FOREIGN_TIFF_PREDICTOR_NONE )
TIFFSetField( tif, TIFFTAG_PREDICTOR, wtiff->predictor ); TIFFSetField( tif, TIFFTAG_PREDICTOR, wtiff->predictor );
for( i = 0; i < VIPS_NUMBER( wtiff_we_compress ); i++ )
if( wtiff->compression == wtiff_we_compress[i] ) {
wtiff->we_compress = TRUE;
break;
}
/* Don't write mad resolutions (eg. zero), it confuses some programs. /* Don't write mad resolutions (eg. zero), it confuses some programs.
*/ */
TIFFSetField( tif, TIFFTAG_RESOLUTIONUNIT, wtiff->resunit ); TIFFSetField( tif, TIFFTAG_RESOLUTIONUNIT, wtiff->resunit );
@ -926,6 +953,20 @@ wtiff_allocate_layers( Wtiff *wtiff )
return( -1 ); return( -1 );
} }
/* If we will be copying layers we need a buffer large enough to hold
* the largest compressed tile in any page.
*
* Allocate a buffer 2x the uncompressed tile size ... much simpler
* than searching every page for the largest tile with
* TIFFTAG_TILEBYTECOUNTS.
*/
if( wtiff->pyramid ) {
wtiff->compressed_buf_length = 2 * wtiff->tls * wtiff->tileh;
if( !(wtiff->compressed_buf = vips_malloc( NULL,
wtiff->compressed_buf_length )) )
return( -1 );
}
return( 0 ); return( 0 );
} }
@ -981,11 +1022,11 @@ static void
wtiff_free( Wtiff *wtiff ) wtiff_free( Wtiff *wtiff )
{ {
wtiff_delete_temps( wtiff ); wtiff_delete_temps( wtiff );
VIPS_UNREF( wtiff->ready ); VIPS_UNREF( wtiff->ready );
VIPS_FREE( wtiff->tbuf ); VIPS_FREE( wtiff->tbuf );
VIPS_FREEF( layer_free_all, wtiff->layer ); VIPS_FREEF( layer_free_all, wtiff->layer );
VIPS_FREE( wtiff->filename ); VIPS_FREE( wtiff->filename );
VIPS_FREE( wtiff->compressed_buf );
VIPS_FREE( wtiff ); VIPS_FREE( wtiff );
} }
@ -1011,6 +1052,8 @@ get_compression( VipsForeignTiffCompression compression )
case VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD: case VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD:
return( COMPRESSION_ZSTD ); return( COMPRESSION_ZSTD );
#endif /*HAVE_TIFF_COMPRESSION_WEBP*/ #endif /*HAVE_TIFF_COMPRESSION_WEBP*/
case VIPS_FOREIGN_TIFF_COMPRESSION_JP2K:
return( JP2K_LOSSY );
default: default:
return( COMPRESSION_NONE ); return( COMPRESSION_NONE );
@ -1237,21 +1280,6 @@ wtiff_new( VipsImage *input, const char *filename,
wtiff->miniswhite = FALSE; wtiff->miniswhite = FALSE;
} }
/* lossless is for webp only.
*/
#ifdef HAVE_TIFF_COMPRESSION_WEBP
if( wtiff->lossless ) {
if( wtiff->compression == COMPRESSION_NONE )
wtiff->compression = COMPRESSION_WEBP;
if( wtiff->compression != COMPRESSION_WEBP ) {
g_warning( "%s",
_( "lossless is for WEBP compression only" ) );
wtiff->lossless = FALSE;
}
}
#endif /*HAVE_TIFF_COMPRESSION_WEBP*/
/* Sizeof a line of bytes in the TIFF tile. /* Sizeof a line of bytes in the TIFF tile.
*/ */
if( wtiff->ready->Coding == VIPS_CODING_LABQ ) if( wtiff->ready->Coding == VIPS_CODING_LABQ )
@ -1489,7 +1517,7 @@ wtiff_pack2tiff( Wtiff *wtiff, Layer *layer,
/* Write a set of tiles across the strip. /* Write a set of tiles across the strip.
*/ */
static int static int
wtiff_layer_write_tile( Wtiff *wtiff, Layer *layer, VipsRegion *strip ) wtiff_layer_write_tiles( Wtiff *wtiff, Layer *layer, VipsRegion *strip )
{ {
VipsImage *im = layer->image; VipsImage *im = layer->image;
VipsRect *area = &strip->valid; VipsRect *area = &strip->valid;
@ -1511,21 +1539,88 @@ wtiff_layer_write_tile( Wtiff *wtiff, Layer *layer, VipsRegion *strip )
tile.height = wtiff->tileh; tile.height = wtiff->tileh;
vips_rect_intersectrect( &tile, &image, &tile ); vips_rect_intersectrect( &tile, &image, &tile );
/* Have to repack pixels.
*/
wtiff_pack2tiff( wtiff, layer, strip, &tile, wtiff->tbuf );
#ifdef DEBUG_VERBOSE #ifdef DEBUG_VERBOSE
printf( "Writing %dx%d tile at position %dx%d to image %s\n", printf( "Writing %dx%d tile at position %dx%d to image %s\n",
tile.width, tile.height, tile.left, tile.top, tile.width, tile.height, tile.left, tile.top,
TIFFFileName( layer->tif ) ); TIFFFileName( layer->tif ) );
#endif /*DEBUG_VERBOSE*/ #endif /*DEBUG_VERBOSE*/
if( TIFFWriteTile( layer->tif, wtiff->tbuf, if( wtiff->we_compress ) {
tile.left, tile.top, 0, 0 ) < 0 ) { ttile_t tile_no = TIFFComputeTile( layer->tif,
vips_error( "vips2tiff", tile.left, tile.top, 0, 0 );
"%s", _( "TIFF write tile failed" ) );
return( -1 ); VipsTarget *target;
int result;
unsigned char *buffer;
size_t length;
target = vips_target_new_to_memory();
switch( wtiff->compression ) {
case JP2K_LOSSY:
/* Sadly chroma subsample seems not to work
* for edge tiles in tiff with jp2k
* compression, so we always pass FALSE
* instead of:
*
* !wtiff->rgbjpeg && wtiff->Q < 90,
*
* I've verified that the libvips jp2k
* encode and decode subsample operations fill
* the comps[i].data arrays correctly, so it
* seems to be a openjpeg bug.
*
* FIXME ... try again with openjpeg 2.5,
* when that comes.
*/
result = vips__foreign_load_jp2k_compress(
strip, &tile, target,
wtiff->tilew, wtiff->tileh,
!wtiff->rgbjpeg,
// !wtiff->rgbjpeg && wtiff->Q < 90,
FALSE,
wtiff->lossless,
wtiff->Q );
break;
default:
result = -1;
g_assert_not_reached();
break;
}
if( result ) {
g_object_unref( target );
return( -1 );
}
buffer = vips_target_steal( target, &length );
g_object_unref( target );
result = TIFFWriteRawTile( layer->tif, tile_no,
buffer, length );
g_free( buffer );
if( result < 0 ) {
vips_error( "vips2tiff",
"%s", _( "TIFF write tile failed" ) );
return( -1 );
}
}
else {
/* Have to repack pixels for libtiff.
*/
wtiff_pack2tiff( wtiff,
layer, strip, &tile, wtiff->tbuf );
if( TIFFWriteTile( layer->tif, wtiff->tbuf,
tile.left, tile.top, 0, 0 ) < 0 ) {
vips_error( "vips2tiff",
"%s", _( "TIFF write tile failed" ) );
return( -1 );
}
} }
} }
@ -1672,7 +1767,7 @@ layer_strip_arrived( Layer *layer )
VipsRect image_area; VipsRect image_area;
if( wtiff->tile ) if( wtiff->tile )
result = wtiff_layer_write_tile( wtiff, layer, layer->strip ); result = wtiff_layer_write_tiles( wtiff, layer, layer->strip );
else else
result = wtiff_layer_write_strip( wtiff, layer, layer->strip ); result = wtiff_layer_write_strip( wtiff, layer, layer->strip );
if( result ) if( result )
@ -1803,7 +1898,7 @@ wtiff_copy_tiff( Wtiff *wtiff, TIFF *out, TIFF *in )
uint16 ui16_2; uint16 ui16_2;
float f; float f;
tdata_t buf; tdata_t buf;
ttile_t tile; ttile_t tile_no;
ttile_t n; ttile_t n;
uint16 *a; uint16 *a;
@ -1891,19 +1986,15 @@ wtiff_copy_tiff( Wtiff *wtiff, TIFF *out, TIFF *in )
buf = vips_malloc( NULL, TIFFTileSize( in ) ); buf = vips_malloc( NULL, TIFFTileSize( in ) );
n = TIFFNumberOfTiles( in ); n = TIFFNumberOfTiles( in );
for( tile = 0; tile < n; tile++ ) { for( tile_no = 0; tile_no < n; tile_no++ ) {
tsize_t len; tsize_t len;
/* It'd be good to use TIFFReadRawTile()/TIFFWtiffRawTile() len = TIFFReadRawTile( in, tile_no,
* here to save compression/decompression, but sadly it seems wtiff->compressed_buf, wtiff->compressed_buf_length );
* not to work :-( investigate at some point. if( len <= 0 ||
*/ TIFFWriteRawTile( out, tile_no,
len = TIFFReadEncodedTile( in, tile, buf, -1 ); wtiff->compressed_buf, len ) < 0 )
if( len < 0 ||
TIFFWriteEncodedTile( out, tile, buf, len ) < 0 ) {
g_free( buf );
return( -1 ); return( -1 );
}
} }
g_free( buf ); g_free( buf );

View File

@ -465,6 +465,7 @@ int vips_webpsave_mime( VipsImage *in, ... )
* @VIPS_FOREIGN_TIFF_COMPRESSION_LZW: LZW compression * @VIPS_FOREIGN_TIFF_COMPRESSION_LZW: LZW compression
* @VIPS_FOREIGN_TIFF_COMPRESSION_WEBP: WEBP compression * @VIPS_FOREIGN_TIFF_COMPRESSION_WEBP: WEBP compression
* @VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD: ZSTD compression * @VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD: ZSTD compression
* @VIPS_FOREIGN_TIFF_COMPRESSION_JP2K: JP2K compression
* *
* The compression types supported by the tiff writer. * The compression types supported by the tiff writer.
* *
@ -485,6 +486,7 @@ typedef enum {
VIPS_FOREIGN_TIFF_COMPRESSION_LZW, VIPS_FOREIGN_TIFF_COMPRESSION_LZW,
VIPS_FOREIGN_TIFF_COMPRESSION_WEBP, VIPS_FOREIGN_TIFF_COMPRESSION_WEBP,
VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD, VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD,
VIPS_FOREIGN_TIFF_COMPRESSION_JP2K,
VIPS_FOREIGN_TIFF_COMPRESSION_LAST VIPS_FOREIGN_TIFF_COMPRESSION_LAST
} VipsForeignTiffCompression; } VipsForeignTiffCompression;

View File

@ -165,18 +165,16 @@ void vips_region_invalidate( VipsRegion *reg );
#define VIPS_COUNT_PIXELS( R, N ) #define VIPS_COUNT_PIXELS( R, N )
#endif /*DEBUG_LEAK*/ #endif /*DEBUG_LEAK*/
/* Macros on VipsRegion.
* VIPS_REGION_LSKIP() add to move down line
* VIPS_REGION_N_ELEMENTS() number of elements across region
* VIPS_REGION_SIZEOF_LINE() sizeof width of region
* VIPS_REGION_ADDR() address of pixel in region
*/
#define VIPS_REGION_LSKIP( R ) \ #define VIPS_REGION_LSKIP( R ) \
((size_t)((R)->bpl)) ((size_t)((R)->bpl))
#define VIPS_REGION_N_ELEMENTS( R ) \ #define VIPS_REGION_N_ELEMENTS( R ) \
((size_t)((R)->valid.width * (R)->im->Bands)) ((size_t)((R)->valid.width * (R)->im->Bands))
#define VIPS_REGION_SIZEOF_ELEMENT( R ) \
(VIPS_IMAGE_SIZEOF_ELEMENT( (R)->im ))
#define VIPS_REGION_SIZEOF_PEL( R ) \
(VIPS_IMAGE_SIZEOF_PEL( (R)->im ))
#define VIPS_REGION_SIZEOF_LINE( R ) \ #define VIPS_REGION_SIZEOF_LINE( R ) \
((size_t)((R)->valid.width * VIPS_IMAGE_SIZEOF_PEL( (R)->im) )) ((size_t)((R)->valid.width * VIPS_REGION_SIZEOF_PEL( R )))
/* If DEBUG is defined, add bounds checking. /* If DEBUG is defined, add bounds checking.
*/ */
@ -184,7 +182,7 @@ void vips_region_invalidate( VipsRegion *reg );
#define VIPS_REGION_ADDR( R, X, Y ) \ #define VIPS_REGION_ADDR( R, X, Y ) \
( (vips_rect_includespoint( &(R)->valid, (X), (Y) ))? \ ( (vips_rect_includespoint( &(R)->valid, (X), (Y) ))? \
((R)->data + ((Y) - (R)->valid.top) * VIPS_REGION_LSKIP(R) + \ ((R)->data + ((Y) - (R)->valid.top) * VIPS_REGION_LSKIP(R) + \
((X) - (R)->valid.left) * VIPS_IMAGE_SIZEOF_PEL((R)->im)): \ ((X) - (R)->valid.left) * VIPS_REGION_SIZEOF_PEL( R )): \
(fprintf( stderr, \ (fprintf( stderr, \
"VIPS_REGION_ADDR: point out of bounds, " \ "VIPS_REGION_ADDR: point out of bounds, " \
"file \"%s\", line %d\n" \ "file \"%s\", line %d\n" \
@ -202,7 +200,7 @@ void vips_region_invalidate( VipsRegion *reg );
#define VIPS_REGION_ADDR( R, X, Y ) \ #define VIPS_REGION_ADDR( R, X, Y ) \
((R)->data + \ ((R)->data + \
((Y)-(R)->valid.top) * VIPS_REGION_LSKIP( R ) + \ ((Y)-(R)->valid.top) * VIPS_REGION_LSKIP( R ) + \
((X)-(R)->valid.left) * VIPS_IMAGE_SIZEOF_PEL( (R)->im )) ((X)-(R)->valid.left) * VIPS_REGION_SIZEOF_PEL( R ))
#endif /*DEBUG*/ #endif /*DEBUG*/
#define VIPS_REGION_ADDR_TOPLEFT( R ) ((R)->data) #define VIPS_REGION_ADDR_TOPLEFT( R ) ((R)->data)

View File

@ -574,6 +574,7 @@ vips_foreign_tiff_compression_get_type( void )
{VIPS_FOREIGN_TIFF_COMPRESSION_LZW, "VIPS_FOREIGN_TIFF_COMPRESSION_LZW", "lzw"}, {VIPS_FOREIGN_TIFF_COMPRESSION_LZW, "VIPS_FOREIGN_TIFF_COMPRESSION_LZW", "lzw"},
{VIPS_FOREIGN_TIFF_COMPRESSION_WEBP, "VIPS_FOREIGN_TIFF_COMPRESSION_WEBP", "webp"}, {VIPS_FOREIGN_TIFF_COMPRESSION_WEBP, "VIPS_FOREIGN_TIFF_COMPRESSION_WEBP", "webp"},
{VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD, "VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD", "zstd"}, {VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD, "VIPS_FOREIGN_TIFF_COMPRESSION_ZSTD", "zstd"},
{VIPS_FOREIGN_TIFF_COMPRESSION_JP2K, "VIPS_FOREIGN_TIFF_COMPRESSION_JP2K", "jp2k"},
{VIPS_FOREIGN_TIFF_COMPRESSION_LAST, "VIPS_FOREIGN_TIFF_COMPRESSION_LAST", "last"}, {VIPS_FOREIGN_TIFF_COMPRESSION_LAST, "VIPS_FOREIGN_TIFF_COMPRESSION_LAST", "last"},
{0, NULL, NULL} {0, NULL, NULL}
}; };

View File

@ -0,0 +1,5 @@
EXTRA_DIST = \
heif.c \
magick.c \
openslide.c \
poppler.c

View File

@ -525,6 +525,16 @@ class TestForeign:
z = y.hist_find(band=0) z = y.hist_find(band=0)
assert z(0, 0)[0] + z(255, 0)[0] == y.width * y.height assert z(0, 0)[0] + z(255, 0)[0] == y.width * y.height
@skip_if_no("jp2kload")
@skip_if_no("tiffload")
def test_tiffjp2k(self):
self.save_load_file(".tif", "[tile,compression=jp2k]", self.colour, 80)
self.save_load_file(".tif",
"[tile,pyramid,compression=jp2k]", self.colour, 80)
self.save_load_file(".tif",
"[tile,pyramid,subifd,compression=jp2k]",
self.colour, 80)
@skip_if_no("magickload") @skip_if_no("magickload")
def test_magickload(self): def test_magickload(self):
def bmp_valid(im): def bmp_valid(im):