File: vips2webp.c

package info (click to toggle)
vips 8.10.5-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 40,952 kB
  • sloc: ansic: 201,201; cpp: 9,766; sh: 5,031; xml: 4,191; python: 3,869; makefile: 1,033; perl: 40
file content (584 lines) | stat: -rw-r--r-- 13,524 bytes parent folder | download
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
/* wrap libwebp libray for write
 *
 * 6/8/13
 * 	- from vips2jpeg.c
 * 31/5/16
 * 	- buffer write ignored lossless, thanks aaron42net
 * 2/5/16 Felix Bünemann
 * 	- used advanced encoding API, expose controls 
 * 8/11/16
 * 	- add metadata write
 * 29/10/18
 * 	- target libwebp 0.5+ and remove some ifdefs
 * 	- add animated webp write
 * 	- use libwebpmux instead of our own thing, phew
 * 6/7/19 [deftomat]
 * 	- support array of delays 
 * 8/7/19
 * 	- set loop even if we strip
 * 14/10/19
 * 	- revise for target IO
 * 18/7/20
 * 	- add @profile param to match tiff, jpg, etc.
 */

/*

    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>

#ifdef HAVE_LIBWEBP

#include <stdlib.h>
#include <string.h>

#include <vips/vips.h>
#include <vips/internal.h>

#include "pforeign.h"

#include <webp/encode.h>
#include <webp/types.h>
#include <webp/mux.h>

typedef int (*webp_import)( WebPPicture *picture,
	const uint8_t *rgb, int stride );

typedef struct {
	VipsImage *image;

	int Q;
	gboolean lossless;
	VipsForeignWebpPreset preset;
	gboolean smart_subsample;
	gboolean near_lossless;
	int alpha_q;
	int reduction_effort;
	gboolean min_size;
	int kmin;
	int kmax;
	gboolean strip;
	const char *profile;

	WebPConfig config;

	/* Output is written here. We can only support memory write, since we
	 * handle metadata.
	 */
	WebPMemoryWriter memory_writer;

	/* Write animated webp here.
	 */
	WebPAnimEncoder *enc;

	/* Add metadata with this.
	 */
	WebPMux *mux;
} VipsWebPWrite;

static WebPPreset
get_preset( VipsForeignWebpPreset preset )
{
	switch( preset ) {
	case VIPS_FOREIGN_WEBP_PRESET_DEFAULT:
		return( WEBP_PRESET_DEFAULT );
	case VIPS_FOREIGN_WEBP_PRESET_PICTURE:
		return( WEBP_PRESET_PICTURE );
	case VIPS_FOREIGN_WEBP_PRESET_PHOTO:
		return( WEBP_PRESET_PHOTO );
	case VIPS_FOREIGN_WEBP_PRESET_DRAWING:
		return( WEBP_PRESET_DRAWING );
	case VIPS_FOREIGN_WEBP_PRESET_ICON:
		return( WEBP_PRESET_ICON );
	case VIPS_FOREIGN_WEBP_PRESET_TEXT:
		return( WEBP_PRESET_TEXT );

	default:
		g_assert_not_reached();
	}

	/* Keep -Wall happy.
	 */
	return( -1 );
}

static void
vips_webp_write_unset( VipsWebPWrite *write )
{
	WebPMemoryWriterClear( &write->memory_writer );
	VIPS_FREEF( WebPAnimEncoderDelete, write->enc );
	VIPS_FREEF( WebPMuxDelete, write->mux );
	VIPS_UNREF( write->image );
}

static int
vips_webp_write_init( VipsWebPWrite *write, VipsImage *image,
	int Q, gboolean lossless, VipsForeignWebpPreset preset,
	gboolean smart_subsample, gboolean near_lossless,
	int alpha_q, int reduction_effort,
	gboolean min_size, int kmin, int kmax,
	gboolean strip, const char *profile )
{
	write->image = NULL;
	write->Q = Q;
	write->lossless = lossless;
	write->preset = preset;
	write->smart_subsample = smart_subsample;
	write->near_lossless = near_lossless;
	write->alpha_q = alpha_q;
	write->reduction_effort = reduction_effort;
	write->min_size = min_size;
	write->kmin = kmin;
	write->kmax = kmax;
	write->strip = strip;
	write->profile = profile;
	WebPMemoryWriterInit( &write->memory_writer );
	write->enc = NULL;
	write->mux = NULL;

	/* We need a copy of the input image in case we change the metadata
	 * eg. in vips__exif_update().
	 */
	if( vips_copy( image, &write->image, NULL ) ) {
		vips_webp_write_unset( write );
		return( -1 );
	}

	if( !WebPConfigInit( &write->config ) ) {
		vips_webp_write_unset( write );
		vips_error( "vips2webp",
			"%s", _( "config version error" ) );
		return( -1 );
	}

	/* These presets are only for lossy compression. There seems to be
	 * separate API for lossless or near-lossless, see
	 * WebPConfigLosslessPreset().
	 */
	if( !(lossless || near_lossless) &&
		!WebPConfigPreset( &write->config, get_preset( preset ), Q ) ) {
		vips_webp_write_unset( write );
		vips_error( "vips2webp", "%s", _( "config version error" ) );
		return( -1 );
	}

	write->config.lossless = lossless || near_lossless;
	write->config.alpha_quality = alpha_q;
	write->config.method = reduction_effort;

	if( lossless )
		write->config.quality = Q;
	if( near_lossless )
		write->config.near_lossless = Q;
	if( smart_subsample )
		write->config.use_sharp_yuv = 1;

	if( !WebPValidateConfig( &write->config ) ) {
		vips_webp_write_unset( write );
		vips_error( "vips2webp", "%s", _( "invalid configuration" ) );
		return( -1 );
	}

	return( 0 );
}

static gboolean
vips_webp_pic_init( VipsWebPWrite *write, WebPPicture *pic )
{
	if( !WebPPictureInit( pic ) ) {
		vips_error( "vips2webp", "%s", _( "picture version error" ) );
		return( FALSE );
	}
	pic->writer = WebPMemoryWrite;
	pic->custom_ptr = (void *) &write->memory_writer;

	/* Smart subsampling needs use_argb because it is applied during 
	 * RGB to YUV conversion.
	 */
	pic->use_argb = write->lossless || 
		write->near_lossless || 
		write->smart_subsample;

	return( TRUE );
}

/* Write a VipsImage into an unintialised pic.
 */
static int
write_webp_image( VipsWebPWrite *write, VipsImage *image, WebPPicture *pic ) 
{
	VipsImage *memory;
	webp_import import;

	if( !vips_webp_pic_init( write, pic ) ) 
		return( -1 );

	if( !(memory = vips_image_copy_memory( image )) ) {
		WebPPictureFree( pic );
		return( -1 );
	}

	pic->width = memory->Xsize;
	pic->height = memory->Ysize;

	if( memory->Bands == 4 )
		import = WebPPictureImportRGBA;
	else
		import = WebPPictureImportRGB;

	if( !import( pic, VIPS_IMAGE_ADDR( memory, 0, 0 ),
		VIPS_IMAGE_SIZEOF_LINE( memory ) ) ) {
		VIPS_UNREF( memory );
		WebPPictureFree( pic );
		vips_error( "vips2webp", "%s", _( "picture memory error" ) );
		return( -1 );
	}

	VIPS_UNREF( memory );

	return( 0 );
}

/* Write a single image into write->memory_writer.
 */
static int
write_webp_single( VipsWebPWrite *write, VipsImage *image )
{
	WebPPicture pic;

	if( write_webp_image( write, image, &pic ) ) { 
		WebPPictureFree( &pic );
		return( -1 );
	}

	if( !WebPEncode( &write->config, &pic ) ) {
		WebPPictureFree( &pic );
		vips_error( "vips2webp", "%s", _( "unable to encode" ) );
		return( -1 );
	}

	WebPPictureFree( &pic );

	return( 0 );
}

/* Write a set of animated frames into write->memory_writer.
 */
static int
write_webp_anim( VipsWebPWrite *write, VipsImage *image, int page_height )
{
	WebPAnimEncoderOptions anim_config;
	WebPData webp_data;
	int gif_delay;
	int *delay;
	int delay_length;
	int top;
	int timestamp_ms;

	if( !WebPAnimEncoderOptionsInit( &anim_config ) ) {
		vips_error( "vips2webp",
			"%s", _( "config version error" ) );
		return( -1 );
	}

	anim_config.minimize_size = write->min_size;
	anim_config.allow_mixed = write->min_size;
	anim_config.kmin = write->kmin;
	anim_config.kmax = write->kmax;

	write->enc = WebPAnimEncoderNew( image->Xsize, page_height, 
		&anim_config );
	if( !write->enc ) {
		vips_error( "vips2webp", 
			"%s", _( "unable to init animation" ) );
		return( -1 );
	}

	/* There might just be the old gif-delay field. This is centiseconds.
	 */
	gif_delay = 4;
	if( vips_image_get_typeof( image, "gif-delay" ) &&
		vips_image_get_int( image, "gif-delay", &gif_delay ) )
		return( -1 );

	/* New images have an array of ints instead.
	 */
	delay = NULL;
	if( vips_image_get_typeof( image, "delay" ) &&
		vips_image_get_array_int( image, "delay", 
			&delay, &delay_length ) )
		return( -1 );

	timestamp_ms = 0;
	for( top = 0; top < image->Ysize; top += page_height ) {
		VipsImage *x;
		WebPPicture pic;
		int page_index;

		if( vips_crop( image, &x, 
			0, top, image->Xsize, page_height, NULL ) )
			return( -1 );

		if( write_webp_image( write, x, &pic ) ) {
			VIPS_UNREF( x ); 
			return( -1 );
		}

		VIPS_UNREF( x ); 

		if( !WebPAnimEncoderAdd( write->enc, 
			&pic, timestamp_ms, &write->config ) ) {
			WebPPictureFree( &pic );
			vips_error( "vips2webp",
				"%s", _( "anim add error" ) );
			return( -1 );
		}

		WebPPictureFree( &pic );

		page_index = top / page_height;
		if( delay &&
			page_index < delay_length )
			timestamp_ms += delay[page_index];
		else 
			timestamp_ms += gif_delay * 10;
	}

	/* Closes encoder and adds last frame delay.
	 */
	if( !WebPAnimEncoderAdd( write->enc, 
		NULL, timestamp_ms, NULL ) ) {
		vips_error( "vips2webp",
			"%s", _( "anim close error" ) );
		return( -1 );
	}

	if( !WebPAnimEncoderAssemble( write->enc, &webp_data ) ) {
		vips_error( "vips2webp",
			"%s", _( "anim build error" ) );
		return( -1 );
	}

	/* Terrible. This will only work if the output buffer is currently
	 * empty. 
	 */
	if( write->memory_writer.mem != NULL ) {
		vips_error( "vips2webp", "%s", _( "internal error" ) );
		return( -1 );
	}
	write->memory_writer.mem = (uint8_t *) webp_data.bytes;
	write->memory_writer.size = webp_data.size;

	return( 0 );
}

static int
write_webp( VipsWebPWrite *write )
{
	int page_height = vips_image_get_page_height( write->image ); 

	if( page_height < write->image->Ysize )
		return( write_webp_anim( write, write->image, page_height ) );
	else
		return( write_webp_single( write, write->image ) );
}

static void
vips_webp_set_count( VipsWebPWrite *write, int loop_count )
{
	uint32_t features;

	if( WebPMuxGetFeatures( write->mux, &features ) == WEBP_MUX_OK &&
		(features & ANIMATION_FLAG) ) {
		WebPMuxAnimParams params;

		if( WebPMuxGetAnimationParams( write->mux, &params ) == 
			WEBP_MUX_OK ) {
			params.loop_count = loop_count;
			WebPMuxSetAnimationParams( write->mux, &params );
		}
	}
}

static int
vips_webp_set_chunk( VipsWebPWrite *write, 
	const char *webp_name, const void *data, size_t length )
{
	WebPData chunk;

	chunk.bytes = data;
	chunk.size = length;

	if( WebPMuxSetChunk( write->mux, webp_name, &chunk, 1 ) != 
		WEBP_MUX_OK ) { 
		vips_error( "vips2webp", 
			"%s", _( "chunk add error" ) );
		return( -1 );
	}

	return( 0 );
}

static int 
vips_webp_add_chunks( VipsWebPWrite *write )
{
	int i;

	for( i = 0; i < vips__n_webp_names; i++ ) { 
		const char *vips_name = vips__webp_names[i].vips;
		const char *webp_name = vips__webp_names[i].webp;

		if( vips_image_get_typeof( write->image, vips_name ) ) {
			const void *data;
			size_t length;

			if( vips_image_get_blob( write->image, 
				vips_name, &data, &length ) ||
				vips_webp_set_chunk( write, 
					webp_name, data, length ) )
				return( -1 ); 
		}
	}

	return( 0 );
}

static int 
vips_webp_add_metadata( VipsWebPWrite *write )
{
	WebPData data;

	data.bytes = write->memory_writer.mem;
	data.size = write->memory_writer.size;

	/* Parse what we have.
	 */
	if( !(write->mux = WebPMuxCreate( &data, 1 )) ) {
		vips_error( "vips2webp", "%s", _( "mux error" ) );
		return( -1 );
	}

	if( vips_image_get_typeof( write->image, "loop" ) ) {
		int loop;

		if( vips_image_get_int( write->image, "loop", &loop ) )
			return( -1 );

		vips_webp_set_count( write, loop );
	}
	/* DEPRECATED "gif-loop"
	 */
	else if ( vips_image_get_typeof( write->image, "gif-loop" ) ) {
		int gif_loop;

		if( vips_image_get_int( write->image, "gif-loop", &gif_loop ) )
			return( -1 );

		vips_webp_set_count( write, gif_loop == 0 ? 0 : gif_loop + 1 );
	}

	/* Add extra metadata.
	 */
	if( !write->strip ) {
		/* We need to rebuild exif from the other image tags before
		 * writing the metadata.
		 */
		if( vips__exif_update( write->image ) )
			return( -1 );

		/* Override profile.
		 */
		if( write->profile &&
			vips__profile_set( write->image, write->profile ) )
			return( -1 );

		if( vips_webp_add_chunks( write ) ) 
			return( -1 );
	}

	if( WebPMuxAssemble( write->mux, &data ) != WEBP_MUX_OK ) {
		vips_error( "vips2webp", "%s", _( "mux error" ) );
		return( -1 );
	}

	/* Free old stuff, reinit with new stuff.
	 */
	WebPMemoryWriterClear( &write->memory_writer );
	write->memory_writer.mem = (uint8_t *) data.bytes;
	write->memory_writer.size = data.size;
  
	return( 0 );
}

int
vips__webp_write_target( VipsImage *image, VipsTarget *target,
	int Q, gboolean lossless, VipsForeignWebpPreset preset,
	gboolean smart_subsample, gboolean near_lossless,
	int alpha_q, int reduction_effort,
	gboolean min_size, int kmin, int kmax,
	gboolean strip, const char *profile )
{
	VipsWebPWrite write;

	if( vips_webp_write_init( &write, image,
		Q, lossless, preset, smart_subsample, near_lossless,
		alpha_q, reduction_effort, min_size, kmin, kmax, strip,
		profile ) )
		return( -1 );

	if( write_webp( &write ) ) {
		vips_webp_write_unset( &write );
		return( -1 );
	}

	if( vips_webp_add_metadata( &write ) ) {
		vips_webp_write_unset( &write );
		return( -1 );
	}

	if( vips_target_write( target, 
		write.memory_writer.mem, write.memory_writer.size ) ) {
		vips_webp_write_unset( &write );
		return( -1 );
	}

	vips_target_finish( target );

	vips_webp_write_unset( &write );

	return( 0 );
}

#endif /*HAVE_LIBWEBP*/