mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Add script/draw-with-mask.go
This commit is contained in:
1 parent
c6e6dd7023
commit
621bd9505d
5 files changed
+93
No files matched your search
@@ -4,5 +4,6 @@ go 1.12
|
||||
|
||||
require (
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
|
||||
gopkg.in/russross/blackfriday.v2 v2.0.0 // indirect
|
||||
)
|
||||
@@ -1,4 +1,7 @@
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 h1:5h3ngYt7+vXCDZCup/HkCQgW5XwmSvR/nA2JmJ0RErg=
|
||||
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA=
|
||||
gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI=
|
||||
@@ -0,0 +1,89 @@
|
||||
// Copyright 2020 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
// draw-with-mask.go draws one image using a second image as an alpha mask
|
||||
// (scaled to the size fo the first image). The resultant image is PNG-encoded
|
||||
// to stdout.
|
||||
//
|
||||
// The result isn't necessarily beautiful, but it is an easy way to generate
|
||||
// test images containing an alpha channel.
|
||||
//
|
||||
// Usage: go run draw-with-mask.go color.png alpha.png > foo.png
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"image"
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
"golang.org/x/image/draw"
|
||||
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := main1(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func main1() error {
|
||||
if len(os.Args) != 3 {
|
||||
return errors.New(`bad arguments: usage: "go draw-with-mask.go color.png alpha.png"`)
|
||||
}
|
||||
color, err := decode(os.Args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
alpha, err := decode(os.Args[2])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := color.Bounds()
|
||||
mask := makeMask(b, alpha)
|
||||
|
||||
dst := image.NewRGBA(b)
|
||||
draw.DrawMask(dst, b, color, b.Min, mask, mask.Bounds().Min, draw.Src)
|
||||
|
||||
w := bufio.NewWriter(os.Stdout)
|
||||
defer w.Flush()
|
||||
return png.Encode(w, dst)
|
||||
}
|
||||
|
||||
func decode(filename string) (image.Image, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
m, _, err := image.Decode(f)
|
||||
return m, err
|
||||
}
|
||||
|
||||
func makeMask(bounds image.Rectangle, src image.Image) *image.Alpha {
|
||||
dst := image.NewGray(bounds)
|
||||
draw.CatmullRom.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Src, nil)
|
||||
return &image.Alpha{
|
||||
Pix: dst.Pix,
|
||||
Stride: dst.Stride,
|
||||
Rect: dst.Rect,
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
Reference in new issue
Block a user