-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 715806f
Showing
48 changed files
with
2,900 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This is the official list of Graphics-Go authors for copyright purposes. | ||
# This file is distinct from the CONTRIBUTORS files. | ||
# See the latter for an explanation. | ||
|
||
# Names should be added to this file as | ||
# Name or Organization <email address> | ||
# The email address is not required for organizations. | ||
|
||
# Please keep the list sorted. | ||
|
||
Google Inc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This is the official list of people who can contribute | ||
# (and typically have contributed) code to the Graphics-Go repository. | ||
# The AUTHORS file lists the copyright holders; this file | ||
# lists people. For example, Google employees are listed here | ||
# but not in AUTHORS, because Google holds the copyright. | ||
# | ||
# The submission process automatically checks to make sure | ||
# that people submitting code are listed in this file (by email address). | ||
# | ||
# Names should be added to this file only after verifying that | ||
# the individual or the individual's organization has agreed to | ||
# the appropriate Contributor License Agreement, found here: | ||
# | ||
# http://code.google.com/legal/individual-cla-v1.0.html | ||
# http://code.google.com/legal/corporate-cla-v1.0.html | ||
# | ||
# The agreement for individuals can be filled out on the web. | ||
# | ||
# When adding J Random Contributor's name to this file, | ||
# either J's name or J's organization's name should be | ||
# added to the AUTHORS file, depending on whether the | ||
# individual or corporate CLA was used. | ||
|
||
# Names should be added to this file like so: | ||
# Name <email address> | ||
|
||
# Please keep the list sorted. | ||
|
||
David Crawshaw <[email protected]> | ||
Johan Euphrosine <[email protected]> | ||
Nigel Tao <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2011 The Graphics-Go Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2011 The Graphics-Go Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
include $(GOROOT)/src/Make.inc | ||
|
||
all: install | ||
|
||
# The order matters: earlier packages may not depend on later ones. | ||
DIRS=\ | ||
graphics | ||
|
||
test: | ||
cd graphics; GOMAXPROCS=2 gomake test | ||
|
||
bench: | ||
cd graphics; GOMAXPROCS=2 gomake bench | ||
|
||
install clean nuke: | ||
for dir in $(DIRS); do \ | ||
$(MAKE) -C $$dir $@ || exit 1; \ | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This is a Graphics library for the Go programming language. | ||
|
||
Unless otherwise noted, the graphics-go source files are distributed | ||
under the BSD-style license found in the LICENSE file. | ||
|
||
Contributions should follow the same procedure as for the Go project: | ||
http://golang.org/doc/contribute.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module code.google.com/p/graphics-go | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2011 The Graphics-Go Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
include $(GOROOT)/src/Make.inc | ||
|
||
TARG=code.google.com/p/graphics-go/graphics | ||
GOFILES=\ | ||
affine.go\ | ||
blur.go\ | ||
rotate.go\ | ||
scale.go\ | ||
thumbnail.go\ | ||
|
||
include $(GOROOT)/src/Make.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright 2011 The Graphics-Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package graphics | ||
|
||
import ( | ||
"code.google.com/p/graphics-go/graphics/interp" | ||
"errors" | ||
"image" | ||
"image/draw" | ||
"math" | ||
) | ||
|
||
// I is the identity Affine transform matrix. | ||
var I = Affine{ | ||
1, 0, 0, | ||
0, 1, 0, | ||
0, 0, 1, | ||
} | ||
|
||
// Affine is a 3x3 2D affine transform matrix. | ||
// M(i,j) is Affine[i*3+j]. | ||
type Affine [9]float64 | ||
|
||
// Mul returns the multiplication of two affine transform matrices. | ||
func (a Affine) Mul(b Affine) Affine { | ||
return Affine{ | ||
a[0]*b[0] + a[1]*b[3] + a[2]*b[6], | ||
a[0]*b[1] + a[1]*b[4] + a[2]*b[7], | ||
a[0]*b[2] + a[1]*b[5] + a[2]*b[8], | ||
a[3]*b[0] + a[4]*b[3] + a[5]*b[6], | ||
a[3]*b[1] + a[4]*b[4] + a[5]*b[7], | ||
a[3]*b[2] + a[4]*b[5] + a[5]*b[8], | ||
a[6]*b[0] + a[7]*b[3] + a[8]*b[6], | ||
a[6]*b[1] + a[7]*b[4] + a[8]*b[7], | ||
a[6]*b[2] + a[7]*b[5] + a[8]*b[8], | ||
} | ||
} | ||
|
||
func (a Affine) transformRGBA(dst *image.RGBA, src *image.RGBA, i interp.RGBA) error { | ||
srcb := src.Bounds() | ||
b := dst.Bounds() | ||
for y := b.Min.Y; y < b.Max.Y; y++ { | ||
for x := b.Min.X; x < b.Max.X; x++ { | ||
sx, sy := a.pt(x, y) | ||
if inBounds(srcb, sx, sy) { | ||
c := i.RGBA(src, sx, sy) | ||
off := (y-dst.Rect.Min.Y)*dst.Stride + (x-dst.Rect.Min.X)*4 | ||
dst.Pix[off+0] = c.R | ||
dst.Pix[off+1] = c.G | ||
dst.Pix[off+2] = c.B | ||
dst.Pix[off+3] = c.A | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Transform applies the affine transform to src and produces dst. | ||
func (a Affine) Transform(dst draw.Image, src image.Image, i interp.Interp) error { | ||
if dst == nil { | ||
return errors.New("graphics: dst is nil") | ||
} | ||
if src == nil { | ||
return errors.New("graphics: src is nil") | ||
} | ||
|
||
// RGBA fast path. | ||
dstRGBA, dstOk := dst.(*image.RGBA) | ||
srcRGBA, srcOk := src.(*image.RGBA) | ||
interpRGBA, interpOk := i.(interp.RGBA) | ||
if dstOk && srcOk && interpOk { | ||
return a.transformRGBA(dstRGBA, srcRGBA, interpRGBA) | ||
} | ||
|
||
srcb := src.Bounds() | ||
b := dst.Bounds() | ||
for y := b.Min.Y; y < b.Max.Y; y++ { | ||
for x := b.Min.X; x < b.Max.X; x++ { | ||
sx, sy := a.pt(x, y) | ||
if inBounds(srcb, sx, sy) { | ||
dst.Set(x, y, i.Interp(src, sx, sy)) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func inBounds(b image.Rectangle, x, y float64) bool { | ||
if x < float64(b.Min.X) || x >= float64(b.Max.X) { | ||
return false | ||
} | ||
if y < float64(b.Min.Y) || y >= float64(b.Max.Y) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (a Affine) pt(x0, y0 int) (x1, y1 float64) { | ||
fx := float64(x0) + 0.5 | ||
fy := float64(y0) + 0.5 | ||
x1 = fx*a[0] + fy*a[1] + a[2] | ||
y1 = fx*a[3] + fy*a[4] + a[5] | ||
return x1, y1 | ||
} | ||
|
||
// TransformCenter applies the affine transform to src and produces dst. | ||
// Equivalent to | ||
// a.CenterFit(dst, src).Transform(dst, src, i). | ||
func (a Affine) TransformCenter(dst draw.Image, src image.Image, i interp.Interp) error { | ||
if dst == nil { | ||
return errors.New("graphics: dst is nil") | ||
} | ||
if src == nil { | ||
return errors.New("graphics: src is nil") | ||
} | ||
|
||
return a.CenterFit(dst.Bounds(), src.Bounds()).Transform(dst, src, i) | ||
} | ||
|
||
// Scale produces a scaling transform of factors x and y. | ||
func (a Affine) Scale(x, y float64) Affine { | ||
return a.Mul(Affine{ | ||
1 / x, 0, 0, | ||
0, 1 / y, 0, | ||
0, 0, 1, | ||
}) | ||
} | ||
|
||
// Rotate produces a clockwise rotation transform of angle, in radians. | ||
func (a Affine) Rotate(angle float64) Affine { | ||
s, c := math.Sincos(angle) | ||
return a.Mul(Affine{ | ||
+c, +s, +0, | ||
-s, +c, +0, | ||
+0, +0, +1, | ||
}) | ||
} | ||
|
||
// Shear produces a shear transform by the slopes x and y. | ||
func (a Affine) Shear(x, y float64) Affine { | ||
d := 1 - x*y | ||
return a.Mul(Affine{ | ||
+1 / d, -x / d, 0, | ||
-y / d, +1 / d, 0, | ||
0, 0, 1, | ||
}) | ||
} | ||
|
||
// Translate produces a translation transform with pixel distances x and y. | ||
func (a Affine) Translate(x, y float64) Affine { | ||
return a.Mul(Affine{ | ||
1, 0, -x, | ||
0, 1, -y, | ||
0, 0, +1, | ||
}) | ||
} | ||
|
||
// Center produces the affine transform, centered around the provided point. | ||
func (a Affine) Center(x, y float64) Affine { | ||
return I.Translate(-x, -y).Mul(a).Translate(x, y) | ||
} | ||
|
||
// CenterFit produces the affine transform, centered around the rectangles. | ||
// It is equivalent to | ||
// I.Translate(-<center of src>).Mul(a).Translate(<center of dst>) | ||
func (a Affine) CenterFit(dst, src image.Rectangle) Affine { | ||
dx := float64(dst.Min.X) + float64(dst.Dx())/2 | ||
dy := float64(dst.Min.Y) + float64(dst.Dy())/2 | ||
sx := float64(src.Min.X) + float64(src.Dx())/2 | ||
sy := float64(src.Min.Y) + float64(src.Dy())/2 | ||
return I.Translate(-sx, -sy).Mul(a).Translate(dx, dy) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2011 The Graphics-Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package graphics | ||
|
||
import ( | ||
"code.google.com/p/graphics-go/graphics/convolve" | ||
"errors" | ||
"image" | ||
"image/draw" | ||
"math" | ||
) | ||
|
||
// DefaultStdDev is the default blurring parameter. | ||
var DefaultStdDev = 0.5 | ||
|
||
// BlurOptions are the blurring parameters. | ||
// StdDev is the standard deviation of the normal, higher is blurrier. | ||
// Size is the size of the kernel. If zero, it is set to Ceil(6 * StdDev). | ||
type BlurOptions struct { | ||
StdDev float64 | ||
Size int | ||
} | ||
|
||
// Blur produces a blurred version of the image, using a Gaussian blur. | ||
func Blur(dst draw.Image, src image.Image, opt *BlurOptions) error { | ||
if dst == nil { | ||
return errors.New("graphics: dst is nil") | ||
} | ||
if src == nil { | ||
return errors.New("graphics: src is nil") | ||
} | ||
|
||
sd := DefaultStdDev | ||
size := 0 | ||
|
||
if opt != nil { | ||
sd = opt.StdDev | ||
size = opt.Size | ||
} | ||
|
||
if size < 1 { | ||
size = int(math.Ceil(sd * 6)) | ||
} | ||
|
||
kernel := make([]float64, 2*size+1) | ||
for i := 0; i <= size; i++ { | ||
x := float64(i) / sd | ||
x = math.Pow(1/math.SqrtE, x*x) | ||
kernel[size-i] = x | ||
kernel[size+i] = x | ||
} | ||
|
||
// Normalize the weights to sum to 1.0. | ||
kSum := 0.0 | ||
for _, k := range kernel { | ||
kSum += k | ||
} | ||
for i, k := range kernel { | ||
kernel[i] = k / kSum | ||
} | ||
|
||
return convolve.Convolve(dst, src, &convolve.SeparableKernel{ | ||
X: kernel, | ||
Y: kernel, | ||
}) | ||
} |
Oops, something went wrong.