Skip to content

Commit 46203fa

Browse files
committed
Support choosing color in text annotations.
Note: the color should be expressed in the same colorspace as the image.
1 parent 355624b commit 46203fa

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

server/postpic.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,13 @@ float4 pp_parse_float(const char * str);
175175
int pp_parse_int(char * str);
176176
Oid pp_parse_cstype(const ColorspaceType t);
177177
void pp_parse_color(const char * str, PPColor * color);
178-
// image (GraphicsMagick) building and destroying
178+
// To deal easily with GraphicsMagick objects
179179
Image * gm_image_from_lob(Oid loid);
180180
Image * gm_image_from_bytea(bytea * imgdata);
181181
char * gm_image_getattr(Image * img, const char * attr);
182182
void * gm_image_to_blob(Image * timg, size_t * blen, ExceptionInfo * ex);
183183
void gm_image_destroy(Image *);
184+
PixelPacket * gm_ppacket_from_color(const PPColor * color);
184185
// large objects processing
185186
void * lo_readblob(Oid loid, int * len);
186187
int lo_size(int32 fd);
@@ -470,6 +471,7 @@ PG_FUNCTION_INFO_V1(image_draw_text);
470471
Datum image_draw_text(PG_FUNCTION_ARGS)
471472
{
472473
PPImage * img, * res;
474+
PPColor * color = NULL;
473475
Image * gimg;
474476
DrawContext ctx;
475477
VarChar * label, * f_family = NULL;
@@ -481,6 +483,9 @@ Datum image_draw_text(PG_FUNCTION_ARGS)
481483
img = PG_GETARG_IMAGE(0);
482484
label = PG_GETARG_VARCHAR_P(1);
483485
switch(na) {
486+
case 7: //color
487+
color = (PPColor*) PG_GETARG_POINTER(6);
488+
/* fallthrough */
484489
case 6: //font family and size
485490
f_family = PG_GETARG_VARCHAR_P(4);
486491
f_size = PG_GETARG_INT32(5);
@@ -492,10 +497,11 @@ Datum image_draw_text(PG_FUNCTION_ARGS)
492497
gimg = gm_image_from_bytea(&img->imgdata);
493498

494499
ctx = DrawAllocateContext((DrawInfo*)NULL, gimg);
495-
if (na==6) {
500+
if (na>=6) {
496501
str = pp_varchar2str(f_family);
497502
DrawSetFontFamily(ctx, str);
498503
DrawSetFontSize(ctx, f_size);
504+
if (na>=7) DrawSetFillColor(ctx, gm_ppacket_from_color(color));
499505
}
500506
str = pp_varchar2str(label);
501507
DrawAnnotation(ctx, x, y, (unsigned char*) str);
@@ -735,6 +741,19 @@ char * gm_image_getattr(Image * img, const char * attr)
735741
return pstrdup(attrs->value);
736742
}
737743

744+
PixelPacket * gm_ppacket_from_color(const PPColor * color)
745+
{
746+
PixelPacket * pp = (PixelPacket *)palloc(sizeof(PixelPacket));
747+
unsigned int data = ntohl(color->cd);
748+
749+
if ((color->cs == CS_RGB.oid) || (color->cs == CS_sRGB.oid)) data = (data << 8);
750+
pp->red = (data >> 24) & 0xFF;
751+
pp->green = (data >> 16 ) & 0xFF;
752+
pp->blue = (data >> 8 ) & 0xFF;
753+
pp->opacity = data & 0xFF;
754+
return pp;
755+
}
756+
738757
int pp_substr2int(char * str, int off, int len)
739758
{
740759
str[off+len] = 0;

server/postpic.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ CREATE FUNCTION draw_text ( image, VARCHAR, INT, INT, VARCHAR, INT )
144144
AS '$libdir/postpic', 'image_draw_text'
145145
LANGUAGE C IMMUTABLE STRICT;
146146

147+
-- Image, text, x, y, font family, font size, color
148+
CREATE FUNCTION draw_text ( image, VARCHAR, INT, INT, VARCHAR, INT, color )
149+
RETURNS image
150+
AS '$libdir/postpic', 'image_draw_text'
151+
LANGUAGE C IMMUTABLE STRICT;
152+
147153
CREATE FUNCTION resize ( image, INT, INT )
148154
RETURNS image
149155
AS '$libdir/postpic', 'image_resize'

0 commit comments

Comments
 (0)