mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08:00
refactor(vips.h): watermark replicate
This commit is contained in:
parent
51c320fab7
commit
b065e902ed
2 changed files with 69 additions and 19 deletions
|
|
@ -134,6 +134,37 @@ func TestImageWatermark(t *testing.T) {
|
|||
Write("fixtures/test_watermark_out.jpg", buf)
|
||||
}
|
||||
|
||||
func TestImageWatermarkNoReplicate(t *testing.T) {
|
||||
image := initImage("test.jpg")
|
||||
_, err := image.Crop(800, 600, NORTH)
|
||||
if err != nil {
|
||||
t.Errorf("Cannot process the image: %#v", err)
|
||||
}
|
||||
|
||||
buf, err := image.Watermark(Watermark{
|
||||
Text: "Copy me if you can",
|
||||
Opacity: 0.5,
|
||||
Width: 200,
|
||||
DPI: 100,
|
||||
NoReplicate: true,
|
||||
Background: Color{255, 255, 255},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = assertSize(buf, 800, 600)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if DetermineImageType(buf) != JPEG {
|
||||
t.Fatal("Image is not jpeg")
|
||||
}
|
||||
|
||||
Write("fixtures/test_watermark_replicate_out.jpg", buf)
|
||||
}
|
||||
|
||||
func TestImageZoom(t *testing.T) {
|
||||
buf, err := initImage("test.jpg").Zoom(1)
|
||||
if err != nil {
|
||||
|
|
|
|||
57
vips.h
57
vips.h
|
|
@ -168,13 +168,31 @@ vips_init_image(void *buf, size_t len, int imageType, VipsImage **out) {
|
|||
return code;
|
||||
};
|
||||
|
||||
int
|
||||
vips_watermark_replicate(VipsImage *orig, VipsImage *in, VipsImage **out)
|
||||
{
|
||||
VipsImage *cache = vips_image_new();
|
||||
|
||||
if (
|
||||
vips_replicate(in, &cache,
|
||||
1 + orig->Xsize / in->Xsize,
|
||||
1 + orig->Ysize / in->Ysize, NULL) ||
|
||||
vips_crop(cache, out, 0, 0, orig->Xsize, orig->Ysize, NULL)
|
||||
) {
|
||||
g_object_unref(cache);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
||||
int
|
||||
vips_watermark(VipsImage *in, VipsImage **out, watermarkTextOptions *to, watermarkOptions *o)
|
||||
{
|
||||
double ones[3] = { 1, 1, 1 };
|
||||
|
||||
VipsImage *base = vips_image_new();
|
||||
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 12);
|
||||
VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
|
||||
t[0] = in;
|
||||
|
||||
// Make the mask.
|
||||
|
|
@ -189,38 +207,39 @@ vips_watermark(VipsImage *in, VipsImage **out, watermarkTextOptions *to, waterma
|
|||
vips_embed(t[3], &t[4], 100, 100, t[3]->Xsize + o->Margin, t[3]->Ysize + o->Margin, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Replicate if necessary
|
||||
if (o->NoReplicate != 1 && (
|
||||
vips_replicate(t[4], &t[5],
|
||||
1 + t[0]->Xsize / t[4]->Xsize,
|
||||
1 + t[0]->Ysize / t[4]->Ysize, NULL) ||
|
||||
vips_crop(t[5], &t[6], 0, 0, t[0]->Xsize, t[0]->Ysize, NULL))
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return (1);
|
||||
if (o->NoReplicate != 1) {
|
||||
VipsImage *cache = vips_image_new();
|
||||
if (vips_watermark_replicate(t[0], t[4], &cache)) {
|
||||
g_object_unref(cache);
|
||||
g_object_unref(base);
|
||||
return 1;
|
||||
}
|
||||
g_object_unref(t[4]);
|
||||
t[4] = cache;
|
||||
}
|
||||
|
||||
// Make the constant image to paint the text with.
|
||||
if (
|
||||
vips_black(&t[7], 1, 1, NULL) ||
|
||||
vips_linear( t[7], &t[8], ones, o->Background, 3, NULL) ||
|
||||
vips_cast(t[8], &t[9], VIPS_FORMAT_UCHAR, NULL) ||
|
||||
vips_copy(t[9], &t[10], "interpretation", t[0]->Type, NULL) ||
|
||||
vips_embed(t[10], &t[11], 0, 0, t[0]->Xsize, t[0]->Ysize, "extend", VIPS_EXTEND_COPY, NULL)
|
||||
vips_black(&t[5], 1, 1, NULL) ||
|
||||
vips_linear(t[5], &t[6], ones, o->Background, 3, NULL) ||
|
||||
vips_cast(t[6], &t[7], VIPS_FORMAT_UCHAR, NULL) ||
|
||||
vips_copy(t[7], &t[8], "interpretation", t[0]->Type, NULL) ||
|
||||
vips_embed(t[8], &t[9], 0, 0, t[0]->Xsize, t[0]->Ysize, "extend", VIPS_EXTEND_COPY, NULL)
|
||||
) {
|
||||
g_object_unref(base);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Blend the mask and text and write to output.
|
||||
if (vips_ifthenelse(t[6], t[11], t[0], out, "blend", TRUE, NULL)) {
|
||||
if (vips_ifthenelse(t[4], t[9], t[0], out, "blend", TRUE, NULL)) {
|
||||
g_object_unref(base);
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_object_unref(base);
|
||||
return (0);
|
||||
return 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue