Skip to content

Commit 355624b

Browse files
committed
Added functions to annotate images with text
1 parent 1208bb5 commit 355624b

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

server/postpic.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Datum image_square(PG_FUNCTION_ARGS);
155155
Datum image_resize(PG_FUNCTION_ARGS);
156156
Datum image_crop(PG_FUNCTION_ARGS);
157157
Datum 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
169170
Timestamp pp_str2timestamp(const char * date);
170171
char * pp_timestamp2str(Timestamp ts);
172+
char * pp_varchar2str(VarChar * text);
171173
int pp_substr2int(char * str, int off, int len);
172174
float4 pp_parse_float(const char * str);
173175
int 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+
467510
PG_FUNCTION_INFO_V1(image_index);
468511
Datum 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+
746799
float4 pp_parse_float(const char * str)
747800
{
748801
float s=-1,g=1;

server/postpic.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ CREATE FUNCTION square ( image, INT )
127127
AS '$libdir/postpic', 'image_square'
128128
LANGUAGE C IMMUTABLE STRICT;
129129

130+
CREATE FUNCTION draw_text ( image, VARCHAR )
131+
RETURNS image
132+
AS '$libdir/postpic', 'image_draw_text'
133+
LANGUAGE C IMMUTABLE STRICT;
134+
135+
-- Image, text, x, y
136+
CREATE FUNCTION draw_text ( image, VARCHAR, INT, INT )
137+
RETURNS image
138+
AS '$libdir/postpic', 'image_draw_text'
139+
LANGUAGE C IMMUTABLE STRICT;
140+
141+
-- Image, text, x, y, font family, font size
142+
CREATE FUNCTION draw_text ( image, VARCHAR, INT, INT, VARCHAR, INT )
143+
RETURNS image
144+
AS '$libdir/postpic', 'image_draw_text'
145+
LANGUAGE C IMMUTABLE STRICT;
146+
130147
CREATE FUNCTION resize ( image, INT, INT )
131148
RETURNS image
132149
AS '$libdir/postpic', 'image_resize'

0 commit comments

Comments
 (0)