Browse Source

fix docs; rotation is ccw

disintegration 11 năm trước cách đây
mục cha
commit
272634379a
3 tập tin đã thay đổi với 40 bổ sung33 xóa
  1. 1 1
      LICENSE
  2. 25 18
      README.md
  3. 14 14
      imaging.go

+ 1 - 1
LICENSE

@@ -1,6 +1,6 @@
 The MIT License (MIT)
 
-Copyright (c) 2012 Grigory Dryapak
+Copyright (c) 2012-2014 Grigory Dryapak
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal

+ 25 - 18
README.md

@@ -27,15 +27,19 @@ import (
 )
 
 func main() {
-    src, _ := imaging.Open("src.png") // load an image from file (returns image.Image interface)
+    src, err := imaging.Open("src.png") // load an image from file (returns image.Image interface)
+    if err != nil {
+        panic(err)
+    }
+
     var dst *image.NRGBA
-    
+
     dst = imaging.New(800, 600, color.NRGBA{255, 0, 0, 255}) // create a new 800x600px image filled with red color
-    dst = imaging.Clone(src) // make a copy of the image
-    
-    dst = imaging.Rotate90(src) // rotate 90 degrees clockwise 
-    dst = imaging.Rotate180(src) // rotate 180 degrees clockwise
-    dst = imaging.Rotate270(src) // rotate 270 degrees clockwise
+    dst = imaging.Clone(src)                                 // make a copy of the image
+
+    dst = imaging.Rotate90(src)  // rotate 90 degrees counterclockwiseclockwise
+    dst = imaging.Rotate180(src) // rotate 180 degrees counterclockwiseclockwise
+    dst = imaging.Rotate270(src) // rotate 270 degrees counterclockwiseclockwise
 
     dst = imaging.FlipH(src) // flip horizontally (from left to right)
     dst = imaging.FlipV(src) // flip vertically (from top to bottom)
@@ -45,20 +49,23 @@ func main() {
     // CatmullRom, BSpline, Gaussian, Lanczos, Hann, Hamming, Blackman, Bartlett, Welch, Cosine.
 
     dst = imaging.Resize(src, 600, 400, imaging.CatmullRom) // resize to 600x400 px using CatmullRom cubic filter
-    dst = imaging.Resize(src, 600, 0, imaging.CatmullRom) // resize to width = 600, preserve the image aspect ratio
-    dst = imaging.Resize(src, 0, 400, imaging.CatmullRom) // resize to height = 400, preserve the image aspect ratio
-    
-    dst = imaging.Fit(src, 800, 600, imaging.CatmullRom) // scale down the image to fit the given maximum width and height
+    dst = imaging.Resize(src, 600, 0, imaging.CatmullRom)   // resize to width = 600, preserve the image aspect ratio
+    dst = imaging.Resize(src, 0, 400, imaging.CatmullRom)   // resize to height = 400, preserve the image aspect ratio
+
+    dst = imaging.Fit(src, 800, 600, imaging.CatmullRom)       // scale down the image to fit the given maximum width and height
     dst = imaging.Thumbnail(src, 100, 100, imaging.CatmullRom) // resize and crop the image to make a 100x100 thumbnail
-    
+
     dst = imaging.Crop(src, image.Rect(50, 50, 100, 100)) // cut out a rectangular region from the image
-    dst = imaging.CropCenter(src, 200, 100) // cut out a 200x100 px region from the center of the image
-    dst = imaging.Paste(dst, src, image.Pt(50, 50)) // paste the src image to the dst image at the given position
-    dst = imaging.PasteCenter(dst, src) // paste the src image to the center of the dst image
-    
+    dst = imaging.CropCenter(src, 200, 100)               // cut out a 200x100 px region from the center of the image
+    dst = imaging.Paste(dst, src, image.Pt(50, 50))       // paste the src image to the dst image at the given position
+    dst = imaging.PasteCenter(dst, src)                   // paste the src image to the center of the dst image
+
     // draw one image over another at the given position and with the given opacity (from 0.0 to 1.0)
     dst = imaging.Overlay(dst, src, image.Pt(50, 30), 1.0)
-    
-    imaging.Save(dst, "dst.jpg") // save the image to file
+
+    err = imaging.Save(dst, "dst.jpg") // save the image to file
+    if err != nil {
+        panic(err)
+    }
 }
 ```

+ 14 - 14
imaging.go

@@ -1,9 +1,9 @@
 // Package imaging provides basic image manipulation functions
 // (resize, rotate, flip, crop, etc.) as well as simplified image loading and saving.
-// 
-// This package is based on the standard Go image package. All the image 
-// manipulation functions provided by the package take any image type that 
-// implements image.Image interface, and return a new image of 
+//
+// This package is based on the standard Go image package. All the image
+// manipulation functions provided by the package take any image type that
+// implements image.Image interface, and return a new image of
 // *image.NRGBA type (32 bit RGBA colors, not premultiplied by alpha).
 //
 package imaging
@@ -33,7 +33,7 @@ func Open(filename string) (img image.Image, err error) {
 	return
 }
 
-// Save saves the image to file with the specified filename. 
+// Save saves the image to file with the specified filename.
 // The format is determined from the filename extension, "jpg" (or "jpeg") and "png" are supported.
 func Save(img image.Image, filename string) (err error) {
 	format := strings.ToLower(filepath.Ext(filename))
@@ -72,7 +72,7 @@ func Save(img image.Image, filename string) (err error) {
 	return
 }
 
-// New creates a new image with the specified width and height, and fills it with the specified color. 
+// New creates a new image with the specified width and height, and fills it with the specified color.
 func New(width, height int, fillColor color.Color) *image.NRGBA {
 	dst := image.NewNRGBA(image.Rect(0, 0, width, height))
 	c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
@@ -421,7 +421,7 @@ func Overlay(background, source image.Image, pos image.Point, opacity float64) *
 	return dst
 }
 
-// Rotate90 rotates the image 90 degrees clockwise and returns the transformed image.
+// Rotate90 rotates the image 90 degrees counterclockwise and returns the transformed image.
 func Rotate90(img image.Image) *image.NRGBA {
 	src := convertToNRGBA(img)
 	srcBounds := src.Bounds()
@@ -451,7 +451,7 @@ func Rotate90(img image.Image) *image.NRGBA {
 	return dst
 }
 
-// Rotate180 rotates the image 180 degrees clockwise and returns the transformed image.
+// Rotate180 rotates the image 180 degrees counterclockwise and returns the transformed image.
 func Rotate180(img image.Image) *image.NRGBA {
 	src := convertToNRGBA(img)
 	srcBounds := src.Bounds()
@@ -481,7 +481,7 @@ func Rotate180(img image.Image) *image.NRGBA {
 	return dst
 }
 
-// Rotate270 rotates the image 270 degrees clockwise and returns the transformed image.
+// Rotate270 rotates the image 270 degrees counterclockwise and returns the transformed image.
 func Rotate270(img image.Image) *image.NRGBA {
 	src := convertToNRGBA(img)
 	srcBounds := src.Bounds()
@@ -604,7 +604,7 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
 		return &image.NRGBA{}
 	}
 
-	// if new width or height is 0 then preserve aspect ratio, minimum 1px  
+	// if new width or height is 0 then preserve aspect ratio, minimum 1px
 	if dstW == 0 {
 		tmpW := float64(dstH) * float64(srcW) / float64(srcH)
 		dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
@@ -617,7 +617,7 @@ func Resize(img image.Image, width, height int, filter ResampleFilter) *image.NR
 	src := convertToNRGBA(img)
 	var tmp, dst *image.NRGBA
 
-	// two-pass resize 
+	// two-pass resize
 	if srcW != dstW {
 		tmp = resizeHorizontal(src, dstW, filter)
 	} else {
@@ -790,7 +790,7 @@ func resizeNearest(img image.Image, width, height int) *image.NRGBA {
 		return &image.NRGBA{}
 	}
 
-	// if new width or height is 0 then preserve aspect ratio, minimum 1px  
+	// if new width or height is 0 then preserve aspect ratio, minimum 1px
 	if dstW == 0 {
 		tmpW := float64(dstH) * float64(srcW) / float64(srcH)
 		dstW = int(math.Max(1.0, math.Floor(tmpW+0.5)))
@@ -828,7 +828,7 @@ func resizeNearest(img image.Image, width, height int) *image.NRGBA {
 	return dst
 }
 
-// Fit scales down the image using the specified resample filter to fit the specified 
+// Fit scales down the image using the specified resample filter to fit the specified
 // maximum width and height and returns the transformed image.
 //
 // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,
@@ -872,7 +872,7 @@ func Fit(img image.Image, width, height int, filter ResampleFilter) *image.NRGBA
 	return Resize(img, newW, newH, filter)
 }
 
-// Thumbnail scales the image up or down using the specified resample filter, crops it 
+// Thumbnail scales the image up or down using the specified resample filter, crops it
 // to the specified width and hight and returns the transformed image.
 //
 // Supported resample filters: NearestNeighbor, Box, Linear, Hermite, MitchellNetravali,