Skip to content

Commit fbef674

Browse files
committed
better rules for 16-bit heifsave
now parallel 16-bit PNG save see #3087
1 parent 7547476 commit fbef674

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
11/10/22 started 8.13.3
2+
- improve rules for 16-bit heifsave [johntrunc]
3+
14
5/9/22 started 8.13.2
25
- in dzsave, add add missing include directive for errno/EEXIST [kleisauke]
36
- fix 8 bit pallete PNG save [lovell]

libvips/foreign/heifsave.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* - rename "speed" as "effort" for consistency with other savers
1515
* 22/12/21
1616
* - add >8 bit support
17+
* 22/10/11
18+
* - improve rules for 16-bit write [johntrunc]
1719
*/
1820

1921
/*
@@ -48,21 +50,6 @@
4850
#define DEBUG
4951
*/
5052

51-
/*
52-
*
53-
54-
TODO:
55-
56-
what about a 16-bit PNG saved with bitdepth=8? does this work?
57-
58-
no!
59-
60-
what about a 8-bit PNG saved with bitdepth=12? does this work?
61-
62-
*
63-
*/
64-
65-
6653
#ifdef HAVE_CONFIG_H
6754
#include <config.h>
6855
#endif /*HAVE_CONFIG_H*/
@@ -309,7 +296,7 @@ vips_foreign_save_heif_pack( VipsForeignSaveHeif *heif,
309296

310297
if( heif->image->BandFmt == VIPS_FORMAT_UCHAR &&
311298
heif->bitdepth == 8 )
312-
/* Most common cases -- 8 bit to 8 bit.
299+
/* Most common case -- 8 bit to 8 bit.
313300
*/
314301
memcpy( q, p, ne );
315302
else if( heif->image->BandFmt == VIPS_FORMAT_UCHAR &&
@@ -330,8 +317,14 @@ vips_foreign_save_heif_pack( VipsForeignSaveHeif *heif,
330317
else if( heif->image->BandFmt == VIPS_FORMAT_USHORT &&
331318
heif->bitdepth <= 8 ) {
332319
/* 16-bit native byte order source, 8 bit write.
333-
*/
334-
int shift = 16 - heif->bitdepth;
320+
*
321+
* Pick the high or low bits of the source.
322+
*/
323+
int vips_bitdepth =
324+
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
325+
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
326+
16 : 8;
327+
int shift = vips_bitdepth - heif->bitdepth;
335328

336329
for( i = 0; i < ne; i++ ) {
337330
guint16 v = *((gushort *) p) >> shift;
@@ -345,7 +338,11 @@ vips_foreign_save_heif_pack( VipsForeignSaveHeif *heif,
345338
heif->bitdepth > 8 ) {
346339
/* 16-bit native byte order source, 16 bit bigendian write.
347340
*/
348-
int shift = 16 - heif->bitdepth;
341+
int vips_bitdepth =
342+
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
343+
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
344+
16 : 8;
345+
int shift = vips_bitdepth - heif->bitdepth;
349346

350347
for( i = 0; i < ne; i++ ) {
351348
guint16 v = *((gushort *) p) >> shift;
@@ -435,25 +432,27 @@ vips_foreign_save_heif_build( VipsObject *object )
435432
build( object ) )
436433
return( -1 );
437434

435+
/* Make a copy of the image in case we modify the metadata eg. for
436+
* exif_update.
437+
*/
438+
if( vips_copy( save->ready, &heif->image, NULL ) )
439+
return( -1 );
440+
438441
/* If the old, deprecated "speed" param is being used and the new
439442
* "effort" param is not, use speed to init effort.
440443
*/
441444
if( vips_object_argument_isset( object, "speed" ) &&
442445
!vips_object_argument_isset( object, "effort" ) )
443446
heif->effort = 9 - heif->speed;
444447

445-
/* Default 12 bit save for ushort. HEIC (for example) implements
448+
/* Default 12 bit save for 16-bit images. HEIC (for example) implements
446449
* 8 / 10 / 12.
447450
*/
448451
if( !vips_object_argument_isset( object, "bitdepth" ) )
449-
heif->bitdepth = save->ready->BandFmt == VIPS_FORMAT_UCHAR ?
450-
8 : 12;
451-
452-
/* Make a copy of the image in case we modify the metadata eg. for
453-
* exif_update.
454-
*/
455-
if( vips_copy( save->ready, &heif->image, NULL ) )
456-
return( -1 );
452+
heif->bitdepth =
453+
heif->image->Type == VIPS_INTERPRETATION_RGB16 ||
454+
heif->image->Type == VIPS_INTERPRETATION_GREY16 ?
455+
12 : 8;
457456

458457
error = heif_context_get_encoder_for_format( heif->ctx,
459458
(enum heif_compression_format) heif->compression,

0 commit comments

Comments
 (0)