소스 검색

Correct image extending

DarthSim 5 년 전
부모
커밋
7d92cdd66d
4개의 변경된 파일31개의 추가작업 그리고 18개의 파일을 삭제
  1. 6 12
      process.go
  2. 10 2
      vips.c
  3. 14 3
      vips.go
  4. 1 1
      vips.h

+ 6 - 12
process.go

@@ -311,18 +311,6 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
 		}
 	}
 
-	if po.Expand && (po.Width > img.Width() || po.Height > img.Height()) {
-		if err = img.EnsureAlpha(); err != nil {
-			return err
-		}
-
-		hasAlpha = true
-
-		if err = img.Embed(gravityCenter, po.Width, po.Height, 0, 0); err != nil {
-			return err
-		}
-	}
-
 	if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) {
 		if err = img.Flatten(po.Background); err != nil {
 			return err
@@ -341,6 +329,12 @@ func transformImage(ctx context.Context, img *vipsImage, data []byte, po *proces
 		}
 	}
 
+	if po.Expand && (po.Width > img.Width() || po.Height > img.Height()) {
+		if err = img.Embed(gravityCenter, po.Width, po.Height, 0, 0, po.Background); err != nil {
+			return err
+		}
+	}
+
 	checkTimeout(ctx)
 
 	if po.Watermark.Enabled {

+ 10 - 2
vips.c

@@ -364,8 +364,16 @@ vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
 }
 
 int
-vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
-  return vips_embed(in, out, x, y, width, height, NULL);
+vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
+  VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
+  int ret = vips_embed(
+    in, out, x, y, width, height,
+    "extend", VIPS_EXTEND_BACKGROUND,
+    "background", bga,
+    NULL
+  );
+  vips_area_unref((VipsArea *)bga);
+  return ret;
 }
 
 int

+ 14 - 3
vips.go

@@ -575,7 +575,7 @@ func (img *vipsImage) Replicate(width, height int) error {
 	return nil
 }
 
-func (img *vipsImage) Embed(gravity gravityType, width, height int, offX, offY int) error {
+func (img *vipsImage) Embed(gravity gravityType, width, height int, offX, offY int, bg rgbColor) error {
 	wmWidth := img.Width()
 	wmHeight := img.Height()
 
@@ -610,8 +610,19 @@ func (img *vipsImage) Embed(gravity gravityType, width, height int, offX, offY i
 		top = 0
 	}
 
+	if err := img.RgbColourspace(); err != nil {
+		return err
+	}
+
+	var bgc []C.double
+	if img.HasAlpha() {
+		bgc = []C.double{C.double(0)}
+	} else {
+		bgc = []C.double{C.double(bg.R), C.double(bg.G), C.double(bg.B)}
+	}
+
 	var tmp *C.VipsImage
-	if C.vips_embed_go(img.VipsImage, &tmp, C.int(left), C.int(top), C.int(width), C.int(height)) != 0 {
+	if C.vips_embed_go(img.VipsImage, &tmp, C.int(left), C.int(top), C.int(width), C.int(height), &bgc[0], C.int(len(bgc))) != 0 {
 		return vipsError()
 	}
 	C.swap_and_clear(&img.VipsImage, tmp)
@@ -655,7 +666,7 @@ func (img *vipsImage) ApplyWatermark(opts *watermarkOptions) error {
 			return err
 		}
 	} else {
-		if err = wm.Embed(opts.Gravity, imgW, imgH, opts.OffsetX, opts.OffsetY); err != nil {
+		if err = wm.Embed(opts.Gravity, imgW, imgH, opts.OffsetX, opts.OffsetY, rgbColor{0, 0, 0}); err != nil {
 			return err
 		}
 	}

+ 1 - 1
vips.h

@@ -68,7 +68,7 @@ int vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma);
 int vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b);
 
 int vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down);
-int vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height);
+int vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn);
 
 int vips_ensure_alpha(VipsImage *in, VipsImage **out);
 int vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity);