浏览代码

two new functions: Invert, Grayscale

disintegration 11 年之前
父节点
当前提交
9f428d15b4
共有 2 个文件被更改,包括 71 次插入3 次删除
  1. 15 3
      README.md
  2. 56 0
      colormods.go

+ 15 - 3
README.md

@@ -3,7 +3,7 @@
 Package imaging provides basic image manipulation functions (resize, rotate, flip, crop, etc.). 
 This package is based on the standard Go image package and works best along with it. 
 
-###Installation
+### Installation
 
     go get -u github.com/disintegration/imaging
     
@@ -11,7 +11,7 @@ This package is based on the standard Go image package and works best along with
 
 http://godoc.org/github.com/disintegration/imaging
 
-###Overview
+### Overview
 
 Image manipulation functions provided by the package take any image type 
 that implements `image.Image` interface as an input, and return a new image of 
@@ -106,7 +106,7 @@ dstImage = imaging.PasteCenter(backgroundImage, srcImage)
 // draw the srcImage over the backgroundImage at the (50, 50) position with opacity=0.5
 dstImage = imaging.Overlay(backgroundImage, srcImage, image.Pt(50, 50), 0.5)
 ```
-##### Blur and Sharpen
+##### Blur & Sharpen
 
 `Blur` produces a blurred version of the image.
 
@@ -122,6 +122,18 @@ dstImage = imaging.Blur(srcImage, 4.5)
 dstImage = imaging.Sharpen(srcImage, 3.0)
 ```
 
+##### Color modifications
+
+`Grayscale` produces grayscale version of the image.
+
+`Invert` produces inverted (negated) version of the image.
+
+Examples:
+```go
+dstImage = imaging.Grayscale(srcImage)
+dstImage = imaging.Invert(srcImage)
+```
+
 ##### Load, Save, New, Clone
 
 Imaging package provides useful shortcuts for image loading, saving, creation and copying.

+ 56 - 0
colormods.go

@@ -0,0 +1,56 @@
+package imaging
+
+import (
+	"image"
+)
+
+// Grayscale produces grayscale version of the image.
+func Grayscale(img image.Image) *image.NRGBA {
+	src := toNRGBA(img)
+	width := src.Bounds().Max.X
+	height := src.Bounds().Max.Y
+	dst := image.NewNRGBA(image.Rect(0, 0, width, height))
+
+	parallel(height, func(partStart, partEnd int) {
+		for y := partStart; y < partEnd; y++ {
+			for x := 0; x < width; x++ {
+				i := y*src.Stride + x*4
+				j := y*dst.Stride + x*4
+				r := float64(src.Pix[i+0])
+				g := float64(src.Pix[i+1])
+				b := float64(src.Pix[i+2])
+				f := 0.299*r + 0.587*g + 0.114*b
+				c := uint8(f + 0.5)
+				dst.Pix[j+0] = c
+				dst.Pix[j+1] = c
+				dst.Pix[j+2] = c
+				dst.Pix[j+3] = src.Pix[i+3]
+			}
+		}
+	})
+
+	return dst
+}
+
+// Invert produces inverted (negated) version of the image.
+func Invert(img image.Image) *image.NRGBA {
+	src := toNRGBA(img)
+	width := src.Bounds().Max.X
+	height := src.Bounds().Max.Y
+	dst := image.NewNRGBA(image.Rect(0, 0, width, height))
+
+	parallel(height, func(partStart, partEnd int) {
+		for y := partStart; y < partEnd; y++ {
+			for x := 0; x < width; x++ {
+				i := y*src.Stride + x*4
+				j := y*dst.Stride + x*4
+				dst.Pix[j+0] = 255 - src.Pix[i+0]
+				dst.Pix[j+1] = 255 - src.Pix[i+1]
+				dst.Pix[j+2] = 255 - src.Pix[i+2]
+				dst.Pix[j+3] = src.Pix[i+3]
+			}
+		}
+	})
+
+	return dst
+}