mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-24 11:55:40 +00:00
65aaa607ab
Change made by: - running "gvt fetch" on each of the packages mentioned in Godeps/Godeps.json - `rm -rf Godeps` - tweaking the build scripts to not mention Godeps - tweaking the build scripts to test `./lib/...`, `./cmd/...` explicitly (to avoid testing vendor) - tweaking the build scripts to not juggle GOPATH for Godeps and instead set GO15VENDOREXPERIMENT. This also results in some updated packages at the same time I bet. Building with Go 1.3 and 1.4 still *works* but won't use our vendored dependencies - the user needs to have the actual packages in their GOPATH then, which they'll get with a normal "go get". Building with Go 1.6+ will get our vendored dependencies by default even when not using our build script, which is nice. By doing this we gain some freedom in that we can pick and choose manually what to include in vendor, as it's not based on just dependency analysis of our own code. This is also a risk as we might pick up dependencies we are unaware of, as the build may work locally with those packages present in GOPATH. On the other hand the build server will detect this as it has no packages in it's GOPATH beyond what is included in the repo. Recommended tool to manage dependencies is github.com/FiloSottile/gvt.
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
package qr
|
|
|
|
import (
|
|
"errors"
|
|
"image"
|
|
"image/color"
|
|
|
|
"github.com/vitrun/qart/coding"
|
|
)
|
|
|
|
// A Level denotes a QR error correction level.
|
|
// From least to most tolerant of errors, they are L, M, Q, H.
|
|
type Level int
|
|
|
|
const (
|
|
L Level = iota // 20% redundant
|
|
M // 38% redundant
|
|
Q // 55% redundant
|
|
H // 65% redundant
|
|
)
|
|
|
|
// Encode returns an encoding of text at the given error correction level.
|
|
func Encode(text string, level Level) (*Code, error) {
|
|
// Pick data encoding, smallest first.
|
|
// We could split the string and use different encodings
|
|
// but that seems like overkill for now.
|
|
var enc coding.Encoding
|
|
switch {
|
|
case coding.Num(text).Check() == nil:
|
|
enc = coding.Num(text)
|
|
case coding.Alpha(text).Check() == nil:
|
|
enc = coding.Alpha(text)
|
|
default:
|
|
enc = coding.String(text)
|
|
}
|
|
|
|
// Pick size.
|
|
l := coding.Level(level)
|
|
var v coding.Version
|
|
for v = coding.MinVersion; ; v++ {
|
|
if v > coding.MaxVersion {
|
|
return nil, errors.New("text too long to encode as QR")
|
|
}
|
|
if enc.Bits(v) <= v.DataBytes(l)*8 {
|
|
break
|
|
}
|
|
}
|
|
|
|
// Build and execute plan.
|
|
p, err := coding.NewPlan(v, l, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cc, err := p.Encode(enc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO: Pick appropriate mask.
|
|
|
|
return &Code{cc.Bitmap, cc.Size, cc.Stride, 8}, nil
|
|
}
|
|
|
|
// A Code is a square pixel grid.
|
|
// It implements image.Image and direct PNG encoding.
|
|
type Code struct {
|
|
Bitmap []byte // 1 is black, 0 is white
|
|
Size int // number of pixels on a side
|
|
Stride int // number of bytes per row
|
|
Scale int // number of image pixels per QR pixel
|
|
}
|
|
|
|
// Black returns true if the pixel at (x,y) is black.
|
|
func (c *Code) Black(x, y int) bool {
|
|
return 0 <= x && x < c.Size && 0 <= y && y < c.Size &&
|
|
c.Bitmap[y*c.Stride+x/8]&(1<<uint(7-x&7)) != 0
|
|
}
|
|
|
|
// Image returns an Image displaying the code.
|
|
func (c *Code) Image() image.Image {
|
|
return &codeImage{c}
|
|
|
|
}
|
|
|
|
// codeImage implements image.Image
|
|
type codeImage struct {
|
|
*Code
|
|
}
|
|
|
|
var (
|
|
whiteColor color.Color = color.Gray{0xFF}
|
|
blackColor color.Color = color.Gray{0x00}
|
|
)
|
|
|
|
func (c *codeImage) Bounds() image.Rectangle {
|
|
d := (c.Size + 8) * c.Scale
|
|
return image.Rect(0, 0, d, d)
|
|
}
|
|
|
|
func (c *codeImage) At(x, y int) color.Color {
|
|
if c.Black(x, y) {
|
|
return blackColor
|
|
}
|
|
return whiteColor
|
|
}
|
|
|
|
func (c *codeImage) ColorModel() color.Model {
|
|
return color.GrayModel
|
|
}
|