@@ -155,6 +155,7 @@ Datum image_square(PG_FUNCTION_ARGS);
155155Datum image_resize (PG_FUNCTION_ARGS );
156156Datum image_crop (PG_FUNCTION_ARGS );
157157Datum image_rotate (PG_FUNCTION_ARGS );
158+ Datum image_draw_text (PG_FUNCTION_ARGS );
158159
159160/*
160161 * Aggregate functions
@@ -168,6 +169,7 @@ PPImage * pp_init_image(Image * gimg);
168169// parsing and formatting
169170Timestamp pp_str2timestamp (const char * date );
170171char * pp_timestamp2str (Timestamp ts );
172+ char * pp_varchar2str (VarChar * text );
171173int pp_substr2int (char * str , int off , int len );
172174float4 pp_parse_float (const char * str );
173175int pp_parse_int (char * str );
@@ -464,13 +466,54 @@ Datum image_square(PG_FUNCTION_ARGS)
464466 PG_RETURN_POINTER (res );
465467}
466468
469+ PG_FUNCTION_INFO_V1 (image_draw_text );
470+ Datum image_draw_text (PG_FUNCTION_ARGS )
471+ {
472+ PPImage * img , * res ;
473+ Image * gimg ;
474+ DrawContext ctx ;
475+ VarChar * label , * f_family = NULL ;
476+ char * str ;
477+ int x = 20 , y = 20 , f_size = 10 ;
478+ unsigned int na ;
479+
480+ na = PG_NARGS ();
481+ img = PG_GETARG_IMAGE (0 );
482+ label = PG_GETARG_VARCHAR_P (1 );
483+ switch (na ) {
484+ case 6 : //font family and size
485+ f_family = PG_GETARG_VARCHAR_P (4 );
486+ f_size = PG_GETARG_INT32 (5 );
487+ /* fallthrough */
488+ case 4 : // x and y position
489+ x = PG_GETARG_INT32 (2 );
490+ y = PG_GETARG_INT32 (3 );
491+ }
492+ gimg = gm_image_from_bytea (& img -> imgdata );
493+
494+ ctx = DrawAllocateContext ((DrawInfo * )NULL , gimg );
495+ if (na == 6 ) {
496+ str = pp_varchar2str (f_family );
497+ DrawSetFontFamily (ctx , str );
498+ DrawSetFontSize (ctx , f_size );
499+ }
500+ str = pp_varchar2str (label );
501+ DrawAnnotation (ctx , x , y , (unsigned char * ) str );
502+ DrawRender (ctx );
503+ DrawDestroyContext (ctx );
504+ res = pp_init_image (gimg );
505+ gm_image_destroy (gimg );
506+
507+ PG_RETURN_POINTER (res );
508+ }
509+
467510PG_FUNCTION_INFO_V1 (image_index );
468511Datum image_index (PG_FUNCTION_ARGS )
469512{
470513 ArrayType * aimgs ;
471514 Image * gimg , * rimg ;
472- int4 tile , offset , sz ;
473- int nimgs , i , istart ;
515+ int4 tile , offset ;
516+ int sz , nimgs , i , istart ;
474517 ImageInfo iinfo ;
475518 MontageInfo minfo ;
476519 ExceptionInfo ex ;
@@ -502,10 +545,7 @@ Datum image_index(PG_FUNCTION_ARGS)
502545 str = palloc (2 * INTLEN );
503546 sprintf (str , "%dx%d" , tile , (nimgs % tile ? nimgs /tile + 1 : nimgs /tile ));
504547 minfo .tile = str ;
505- sz = VARSIZE (title ) - VARHDRSZ ;
506- str = palloc (sz + 1 );
507- strncpy (str , VARDATA (title ), sz );
508- str [sz ]= 0 ;
548+ str = pp_varchar2str (title );
509549 minfo .title = str ;
510550 minfo .shadow = 1 ;
511551 GetExceptionInfo (& ex );
@@ -743,6 +783,19 @@ char* pp_timestamp2str(Timestamp ts)
743783 return res ;
744784}
745785
786+ char * pp_varchar2str (VarChar * text )
787+ {
788+ char * str ;
789+ int sz ;
790+
791+ sz = VARSIZE (text ) - VARHDRSZ ;
792+ str = palloc (sz + 1 );
793+ strncpy (str , VARDATA (text ), sz );
794+ str [sz ]= 0 ;
795+
796+ return str ;
797+ }
798+
746799float4 pp_parse_float (const char * str )
747800{
748801 float s = -1 ,g = 1 ;
0 commit comments