Kaynağa Gözat

Standartize saving functions

DarthSim 1 hafta önce
ebeveyn
işleme
cc535a5214
11 değiştirilmiş dosya ile 95 ekleme ve 56 silme
  1. 2 2
      processing/processing.go
  2. 3 1
      processing/save_fit_bytes.go
  3. 1 1
      vips/bmp.h
  4. 1 1
      vips/bmpsave.c
  5. 1 1
      vips/ico.h
  6. 1 1
      vips/icosave.c
  7. 24 0
      vips/options.go
  8. 16 0
      vips/options.h
  9. 19 19
      vips/vips.c
  10. 18 21
      vips/vips.go
  11. 9 9
      vips/vips.h

+ 2 - 2
processing/processing.go

@@ -543,9 +543,9 @@ func (p *Processor) saveImage(
 	// If we want and can fit the image into the specified number of bytes,
 	// let's do it.
 	if maxBytes := po.MaxBytes(); maxBytes > 0 && outFormat.SupportsQuality() {
-		return saveImageToFitBytes(ctx, img, outFormat, quality, maxBytes)
+		return saveImageToFitBytes(ctx, img, outFormat, quality, maxBytes, po.Options)
 	}
 
 	// Otherwise, just save the image with the specified quality.
-	return img.Save(outFormat, quality)
+	return img.Save(outFormat, quality, po.Options)
 }

+ 3 - 1
processing/save_fit_bytes.go

@@ -6,6 +6,7 @@ import (
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
 	"github.com/imgproxy/imgproxy/v3/imath"
+	"github.com/imgproxy/imgproxy/v3/options"
 	"github.com/imgproxy/imgproxy/v3/server"
 	"github.com/imgproxy/imgproxy/v3/vips"
 )
@@ -19,6 +20,7 @@ func saveImageToFitBytes(
 	format imagetype.Type,
 	startQuality int,
 	target int,
+	o *options.Options,
 ) (imagedata.ImageData, error) {
 	var newQuality int
 
@@ -38,7 +40,7 @@ func saveImageToFitBytes(
 			return nil, err
 		}
 
-		imgdata, err := img.Save(format, quality)
+		imgdata, err := img.Save(format, quality, o)
 		if err != nil {
 			return nil, err
 		}

+ 1 - 1
vips/bmp.h

@@ -30,6 +30,6 @@ int
 vips_bmpload_buffer(void *buf, size_t len, VipsImage **out, ...);
 
 // defined in bmpsave.c
-int vips_bmpsave_target_go(VipsImage *in, VipsTarget *target);
+int vips_bmpsave_target_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts);
 
 #endif

+ 1 - 1
vips/bmpsave.c

@@ -303,7 +303,7 @@ vips_bmpsave_target(VipsImage *in, VipsTarget *target, ...)
 
 // wrapper function which hides varargs (...) from CGo
 int
-vips_bmpsave_target_go(VipsImage *in, VipsTarget *target)
+vips_bmpsave_target_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts)
 {
   return vips_bmpsave_target(in, VIPS_TARGET(target), NULL);
 }

+ 1 - 1
vips/ico.h

@@ -29,7 +29,7 @@ typedef struct __attribute__((packed)) _ICONDIRENTRY_IcoHeader {
 } ICONDIRENTRY_IcoHeader;
 
 // defined in icosave.c
-int vips_icosave_target_go(VipsImage *in, VipsTarget *target);
+int vips_icosave_target_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts);
 
 // defined in icoload.c
 VIPS_API

+ 1 - 1
vips/icosave.c

@@ -243,7 +243,7 @@ vips_icosave_target(VipsImage *in, VipsTarget *target, ...)
 
 // wrapper function which hides varargs (...) from CGo
 int
-vips_icosave_target_go(VipsImage *in, VipsTarget *target)
+vips_icosave_target_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts)
 {
   return vips_icosave_target(in, VIPS_TARGET(target), NULL);
 }

+ 24 - 0
vips/options.go

@@ -0,0 +1,24 @@
+package vips
+
+/*
+#include "options.h"
+*/
+import "C"
+import "github.com/imgproxy/imgproxy/v3/options"
+
+func newSaveOptions(_ *options.Options) C.ImgproxySaveOptions {
+	return C.ImgproxySaveOptions{
+		JpegProgressive: gbool(config.JpegProgressive),
+
+		PngInterlaced:         gbool(config.PngInterlaced),
+		PngQuantize:           gbool(config.PngQuantize),
+		PngQuantizationColors: C.int(config.PngQuantizationColors),
+
+		WebpPreset: C.VipsForeignWebpPreset(config.WebpPreset),
+		WebpEffort: C.int(config.WebpEffort),
+
+		AvifSpeed: C.int(config.AvifSpeed),
+
+		JxlEffort: C.int(config.JxlEffort),
+	}
+}

+ 16 - 0
vips/options.h

@@ -0,0 +1,16 @@
+#include <vips/vips.h>
+
+typedef struct _ImgproxySaveOptions {
+  gboolean JpegProgressive; // Whether to save JPEG as progressive.
+
+  gboolean PngInterlaced;    // Whether to save PNG as interlaced.
+  gboolean PngQuantize;      // Whether to quantize PNG (save with palette).
+  int PngQuantizationColors; // Number of colors to use in PNG quantization.
+
+  VipsForeignWebpPreset WebpPreset; // WebP preset to use.
+  int WebpEffort;                   // WebP encoding effort level.
+
+  int AvifSpeed; // AVIF encoding speed.
+
+  int JxlEffort; // JPEG XL encoding effort.
+} ImgproxySaveOptions;

+ 19 - 19
vips/vips.c

@@ -1025,38 +1025,39 @@ vips_strip_all(VipsImage *in, VipsImage **out)
 }
 
 int
-vips_jpegsave_go(VipsImage *in, VipsTarget *target, int quality, int interlace)
+vips_jpegsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_jpegsave_target(
       in, target,
       "Q", quality,
       "optimize_coding", TRUE,
-      "interlace", interlace,
+      "interlace", opts.JpegProgressive,
       NULL);
 }
 
 int
-vips_jxlsave_go(VipsImage *in, VipsTarget *target, int quality, int effort)
+vips_jxlsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_jxlsave_target(
       in, target,
       "Q", quality,
-      "effort", effort,
+      "effort", opts.JxlEffort,
       NULL);
 }
 
 int
-vips_pngsave_go(VipsImage *in, VipsTarget *target, int interlace, int quantize, int colors)
+vips_pngsave_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts)
 {
+  int quantize = opts.PngQuantize;
   int bitdepth;
 
   if (quantize) {
     bitdepth = 1;
-    if (colors > 16)
+    if (opts.PngQuantizationColors > 16)
       bitdepth = 8;
-    else if (colors > 4)
+    else if (opts.PngQuantizationColors > 4)
       bitdepth = 4;
-    else if (colors > 2)
+    else if (opts.PngQuantizationColors > 2)
       bitdepth = 2;
   }
   else {
@@ -1067,7 +1068,6 @@ vips_pngsave_go(VipsImage *in, VipsTarget *target, int interlace, int quantize,
       else if (bitdepth > 2)
         bitdepth = 4;
       quantize = 1;
-      colors = 1 << bitdepth;
     }
   }
 
@@ -1075,31 +1075,31 @@ vips_pngsave_go(VipsImage *in, VipsTarget *target, int interlace, int quantize,
     return vips_pngsave_target(
         in, target,
         "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
-        "interlace", interlace,
+        "interlace", opts.PngInterlaced,
         NULL);
 
   return vips_pngsave_target(
       in, target,
       "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
-      "interlace", interlace,
+      "interlace", opts.PngInterlaced,
       "palette", quantize,
       "bitdepth", bitdepth,
       NULL);
 }
 
 int
-vips_webpsave_go(VipsImage *in, VipsTarget *target, int quality, int effort, VipsForeignWebpPreset preset)
+vips_webpsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_webpsave_target(
       in, target,
       "Q", quality,
-      "effort", effort,
-      "preset", preset,
+      "effort", opts.WebpEffort,
+      "preset", opts.WebpPreset,
       NULL);
 }
 
 int
-vips_gifsave_go(VipsImage *in, VipsTarget *target)
+vips_gifsave_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts)
 {
   int bitdepth = vips_get_palette_bit_depth(in);
   if (bitdepth <= 0 || bitdepth > 8)
@@ -1108,13 +1108,13 @@ vips_gifsave_go(VipsImage *in, VipsTarget *target)
 }
 
 int
-vips_tiffsave_go(VipsImage *in, VipsTarget *target, int quality)
+vips_tiffsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_tiffsave_target(in, target, "Q", quality, NULL);
 }
 
 int
-vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality)
+vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_heifsave_target(
       in, target,
@@ -1124,13 +1124,13 @@ vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality)
 }
 
 int
-vips_avifsave_go(VipsImage *in, VipsTarget *target, int quality, int speed)
+vips_avifsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts)
 {
   return vips_heifsave_target(
       in, target,
       "Q", quality,
       "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
-      "effort", 9 - speed,
+      "effort", 9 - opts.AvifSpeed,
       NULL);
 }
 

+ 18 - 21
vips/vips.go

@@ -25,6 +25,7 @@ import (
 	"github.com/imgproxy/imgproxy/v3/ierrors"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
+	"github.com/imgproxy/imgproxy/v3/options"
 	"github.com/imgproxy/imgproxy/v3/vips/color"
 )
 
@@ -368,47 +369,43 @@ func (img *Image) LoadThumbnail(imgdata imagedata.ImageData) error {
 	return nil
 }
 
-func (img *Image) Save(imgtype imagetype.Type, quality int) (imagedata.ImageData, error) {
+func (img *Image) Save(
+	imgtype imagetype.Type,
+	quality int,
+	o *options.Options,
+) (imagedata.ImageData, error) {
 	target := C.vips_target_new_to_memory()
 
 	cancel := func() {
 		C.vips_unref_target(target)
 	}
 
+	so := newSaveOptions(o)
+
 	err := C.int(0)
 	imgsize := C.size_t(0)
 
 	switch imgtype {
 	case imagetype.JPEG:
-		err = C.vips_jpegsave_go(img.VipsImage, target, C.int(quality), gbool(config.JpegProgressive))
+		err = C.vips_jpegsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.JXL:
-		err = C.vips_jxlsave_go(img.VipsImage, target, C.int(quality), C.int(config.JxlEffort))
+		err = C.vips_jxlsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.PNG:
-		err = C.vips_pngsave_go(
-			img.VipsImage, target,
-			gbool(config.PngInterlaced),
-			gbool(config.PngQuantize),
-			C.int(config.PngQuantizationColors),
-		)
+		err = C.vips_pngsave_go(img.VipsImage, target, so)
 	case imagetype.WEBP:
-		err = C.vips_webpsave_go(
-			img.VipsImage, target,
-			C.int(quality),
-			C.int(config.WebpEffort),
-			config.WebpPreset.C(),
-		)
+		err = C.vips_webpsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.GIF:
-		err = C.vips_gifsave_go(img.VipsImage, target)
+		err = C.vips_gifsave_go(img.VipsImage, target, so)
 	case imagetype.HEIC:
-		err = C.vips_heifsave_go(img.VipsImage, target, C.int(quality))
+		err = C.vips_heifsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.AVIF:
-		err = C.vips_avifsave_go(img.VipsImage, target, C.int(quality), C.int(config.AvifSpeed))
+		err = C.vips_avifsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.TIFF:
-		err = C.vips_tiffsave_go(img.VipsImage, target, C.int(quality))
+		err = C.vips_tiffsave_go(img.VipsImage, target, C.int(quality), so)
 	case imagetype.BMP:
-		err = C.vips_bmpsave_target_go(img.VipsImage, target)
+		err = C.vips_bmpsave_target_go(img.VipsImage, target, so)
 	case imagetype.ICO:
-		err = C.vips_icosave_target_go(img.VipsImage, target)
+		err = C.vips_icosave_target_go(img.VipsImage, target, so)
 	default:
 		// NOTE: probably, it would be better to use defer unref + additionally ref the target
 		// before passing it to the imagedata.ImageData

+ 9 - 9
vips/vips.h

@@ -6,6 +6,7 @@
 #include <vips/vector.h>
 #include <vips/foreign.h>
 
+#include "options.h"
 #include "source.h"
 #include "bmp.h"
 #include "ico.h"
@@ -93,15 +94,14 @@ int vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n);
 int vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright);
 int vips_strip_all(VipsImage *in, VipsImage **out);
 
-int vips_jpegsave_go(VipsImage *in, VipsTarget *target, int quality, int interlace);
-int vips_jxlsave_go(VipsImage *in, VipsTarget *target, int quality, int effort);
-int vips_pngsave_go(VipsImage *in, VipsTarget *target, int interlace, int quantize,
-    int colors);
-int vips_webpsave_go(VipsImage *in, VipsTarget *target, int quality, int effort, VipsForeignWebpPreset preset);
-int vips_gifsave_go(VipsImage *in, VipsTarget *target);
-int vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality);
-int vips_avifsave_go(VipsImage *in, VipsTarget *target, int quality, int speed);
-int vips_tiffsave_go(VipsImage *in, VipsTarget *target, int quality);
+int vips_jpegsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
+int vips_jxlsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
+int vips_pngsave_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts);
+int vips_webpsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
+int vips_gifsave_go(VipsImage *in, VipsTarget *target, ImgproxySaveOptions opts);
+int vips_heifsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
+int vips_avifsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
+int vips_tiffsave_go(VipsImage *in, VipsTarget *target, int quality, ImgproxySaveOptions opts);
 
 void vips_cleanup();