Created
March 3, 2020 09:52
-
-
Save jcupitt/fea1f7c6b1b6d7375b18bc0867acd242 to your computer and use it in GitHub Desktop.
render dithered text with libvips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* watermark a fax image with vips8 C | |
* | |
* compile with | |
gcc -g -Wall watermark_fax.c `pkg-config vips --cflags --libs` | |
*/ | |
#include <vips/vips.h> | |
int | |
main( int argc, char **argv ) | |
{ | |
VipsImage *base; | |
VipsImage **t; | |
VipsImage *in; | |
VipsImage *dither; | |
VipsImage *text; | |
VipsImage *overlay; | |
if( VIPS_INIT( argv[0] ) ) | |
vips_error_exit( NULL ); | |
if( argc != 4 ) | |
vips_error_exit( "usage: %s in-file out-file \"message\"", | |
argv[0] ); | |
base = vips_image_new(); | |
t = (VipsImage **) vips_object_local_array( VIPS_OBJECT( base ), 12 ); | |
if( !(t[0] = vips_image_new_from_file( argv[1], | |
"access", VIPS_ACCESS_SEQUENTIAL, | |
NULL )) ) { | |
g_object_unref( base ); | |
vips_error_exit( NULL ); | |
} | |
in = t[0]; | |
if( !(t[1] = vips_image_new_matrixv( 6, 6, | |
255.0, 0.0, 0.0, 0.0, 255.0, 0.0, | |
0.0, 255.0, 0.0, 255.0, 0.0, 0.0, | |
0.0, 0.0, 255.0, 0.0, 0.0, 0.0, | |
0.0, 255.0, 0.0, 255.0, 0.0, 0.0, | |
255.0, 0.0, 0.0, 0.0, 255.0, 0.0, | |
0.0, 0.0, 0.0, 0.0, 0.0, 255.0 )) || | |
vips_cast( t[1], &t[2], VIPS_FORMAT_UCHAR, NULL ) ) { | |
g_object_unref( base ); | |
vips_error_exit( NULL ); | |
} | |
dither = t[2]; | |
if( vips_text( &t[3], argv[3], | |
"width", 500, | |
"dpi", 300, | |
NULL ) || | |
vips_replicate( dither, &t[4], | |
1 + t[3]->Xsize / dither->Xsize, | |
1 + t[3]->Ysize / dither->Ysize, NULL ) || | |
vips_crop( t[4], &t[5], 0, 0, | |
t[3]->Xsize, t[3]->Ysize, NULL ) || | |
vips_andimage( t[5], t[3], &t[6], NULL ) ) { | |
g_object_unref( base ); | |
vips_error_exit( NULL ); | |
} | |
text = t[6]; | |
if( vips_embed( text, &t[7], 100, 100, | |
text->Xsize + 200, text->Ysize + 200, NULL ) || | |
vips_replicate( t[7], &t[8], | |
1 + in->Xsize / t[7]->Xsize, | |
1 + in->Ysize / t[7]->Ysize, NULL ) || | |
vips_crop( t[8], &t[9], 0, 0, | |
in->Xsize, in->Ysize, NULL ) ) { | |
g_object_unref( base ); | |
vips_error_exit( NULL ); | |
} | |
overlay = t[9]; | |
/* EOR the image and overlay and write to output. | |
*/ | |
if( vips_eorimage( in, overlay, &t[10], NULL ) || | |
vips_image_write_to_file( t[10], argv[2], NULL ) ) { | |
g_object_unref( base ); | |
vips_error_exit( NULL ); | |
} | |
g_object_unref( base ); | |
return( 0 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment