func vipsAffine(input *C.struct__VipsImage, residual float64, i Interpolator) (*C.struct__VipsImage, error) { var image *C.struct__VipsImage cstring := C.CString(i.String()) interpolator := C.vips_interpolate_new(cstring) defer C.free(unsafe.Pointer(cstring)) defer C.g_object_unref(C.gpointer(interpolator)) err := C.vips_affine_interpolator(input, &image, C.double(residual), 0, 0, C.double(residual), interpolator) if err != 0 { return nil, catchVipsError() } return image, nil }
func Resize(buf []byte, o Options) ([]byte, error) { debug("%#+v", o) // detect (if possible) the file type typ := UNKNOWN switch { case bytes.Equal(buf[:2], MARKER_JPEG): typ = JPEG case bytes.Equal(buf[:2], MARKER_PNG): typ = PNG default: return nil, errors.New("unknown image format") } // create an image instance var image, tmpImage *C.struct__VipsImage // feed it switch typ { case JPEG: C.vips_jpegload_buffer_seq(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &image) case PNG: C.vips_pngload_buffer_seq(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &image) } // cleanup defer func() { C.vips_thread_shutdown() C.vips_error_clear() }() // defaults if o.Quality == 0 { o.Quality = 100 } // get WxH inWidth := int(image.Xsize) inHeight := int(image.Ysize) // prepare for factor factor := 0.0 // image calculations switch { // Fixed width and height case o.Width > 0 && o.Height > 0: xf := float64(inWidth) / float64(o.Width) yf := float64(inHeight) / float64(o.Height) if o.Crop { factor = math.Min(xf, yf) } else { factor = math.Max(xf, yf) } // Fixed width, auto height case o.Width > 0: factor = float64(inWidth) / float64(o.Width) o.Height = int(math.Floor(float64(inHeight) / factor)) // Fixed height, auto width case o.Height > 0: factor = float64(inHeight) / float64(o.Height) o.Width = int(math.Floor(float64(inWidth) / factor)) // Identity transform default: factor = 1 o.Width = inWidth o.Height = inHeight } debug("transform from %dx%d to %dx%d", inWidth, inHeight, o.Width, o.Height) // shrink shrink := int(math.Floor(factor)) if shrink < 1 { shrink = 1 } // residual residual := float64(shrink) / factor // Do not enlarge the output if the input width *or* height are already less than the required dimensions if !o.Enlarge { if inWidth < o.Width && inHeight < o.Height { factor = 1 shrink = 1 residual = 0 o.Width = inWidth o.Height = inHeight } } debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual) // Try to use libjpeg shrink-on-load shrinkOnLoad := 1 if typ == JPEG && shrink >= 2 { switch { case shrink >= 8: factor = factor / 8 shrinkOnLoad = 8 case shrink >= 4: factor = factor / 4 shrinkOnLoad = 4 case shrink >= 2: factor = factor / 2 shrinkOnLoad = 2 } } if shrinkOnLoad > 1 { debug("shrink on load %d", shrinkOnLoad) // Recalculate integral shrink and double residual factor = math.Max(factor, 1.0) shrink = int(math.Floor(factor)) residual = float64(shrink) / factor // Reload input using shrink-on-load err := C.vips_jpegload_buffer_shrink(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &tmpImage, C.int(shrinkOnLoad)) C.g_object_unref(C.gpointer(image)) image = tmpImage if err != 0 { return nil, resizeError() } } if shrink > 1 { debug("shrink %d", shrink) // Use vips_shrink with the integral reduction err := C.vips_shrink_0(image, &tmpImage, C.double(float64(shrink)), C.double(float64(shrink))) C.g_object_unref(C.gpointer(image)) image = tmpImage if err != 0 { return nil, resizeError() } // Recalculate residual float based on dimensions of required vs shrunk images shrunkWidth := int(image.Xsize) shrunkHeight := int(image.Ysize) residualx := float64(o.Width) / float64(shrunkWidth) residualy := float64(o.Height) / float64(shrunkHeight) if o.Crop { residual = math.Max(residualx, residualy) } else { residual = math.Min(residualx, residualy) } } // Use vips_affine with the remaining float part debug("residual: %v", residual) if residual != 0 { debug("residual %.2f", residual) // Create interpolator - "bilinear" (default), "bicubic" or "nohalo" is := C.CString(o.Interpolator.String()) interpolator := C.vips_interpolate_new(is) // Perform affine transformation err := C.vips_affine_interpolator(image, &tmpImage, C.double(residual), 0, 0, C.double(residual), interpolator) C.g_object_unref(C.gpointer(image)) image = tmpImage C.free(unsafe.Pointer(is)) C.g_object_unref(C.gpointer(interpolator)) if err != 0 { return nil, resizeError() } } // Crop/embed affinedWidth := int(image.Xsize) affinedHeight := int(image.Ysize) if affinedWidth != o.Width || affinedHeight != o.Height { if o.Crop { // Crop debug("cropping") left, top := sharpCalcCrop(affinedWidth, affinedHeight, o.Width, o.Height, o.Gravity) o.Width = int(math.Min(float64(affinedWidth), float64(o.Width))) o.Height = int(math.Min(float64(affinedHeight), float64(o.Height))) err := C.vips_extract_area_0(image, &tmpImage, C.int(left), C.int(top), C.int(o.Width), C.int(o.Height)) C.g_object_unref(C.gpointer(image)) image = tmpImage if err != 0 { return nil, resizeError() } } else if o.Embed { debug("embedding with extend %d", o.Extend) left := (o.Width - affinedWidth) / 2 top := (o.Height - affinedHeight) / 2 err := C.vips_embed_extend(image, &tmpImage, C.int(left), C.int(top), C.int(o.Width), C.int(o.Height), C.int(o.Extend)) C.g_object_unref(C.gpointer(image)) image = tmpImage if err != 0 { return nil, resizeError() } } } else { debug("canvased same as affined") } // Always convert to sRGB colour space C.vips_colourspace_0(image, &tmpImage, C.VIPS_INTERPRETATION_sRGB) C.g_object_unref(C.gpointer(image)) image = tmpImage // Finally save length := C.size_t(0) var ptr unsafe.Pointer C.vips_jpegsave_custom(image, &ptr, &length, 1, C.int(o.Quality), 0) C.g_object_unref(C.gpointer(image)) // get back the buffer buf = C.GoBytes(ptr, C.int(length)) C.g_free(C.gpointer(ptr)) return buf, nil }