package psd import ( "image" "image/color" psdColor "github.com/oov/psd/color" ) type picker interface { image.Image setSource(rect image.Rectangle, src ...[]byte) } func findPicker(depth int, colorMode ColorMode, hasAlpha bool) picker { switch colorMode { case ColorModeBitmap, ColorModeGrayscale: return findNGrayPicker(depth, hasAlpha) case ColorModeRGB: return findNRGBAPicker(depth, hasAlpha) case ColorModeCMYK: return findNCMYKAPicker(depth, hasAlpha) } return nil } func findGrayPicker(depth int) picker { switch depth { case 1: return &pickerGray1{} case 8: return &pickerGray8{} case 16: return &pickerGray16{} case 32: return &pickerGray32{} } return nil } func findNGrayPicker(depth int, hasAlpha bool) picker { switch depth { case 8: if hasAlpha { return &pickerNGrayA8{} } return &pickerNGray8{} case 16: if hasAlpha { return &pickerNGrayA16{} } return &pickerNGray16{} case 32: if hasAlpha { return &pickerNGrayA32{} } return &pickerNGray32{} } return nil } func findNRGBAPicker(depth int, hasAlpha bool) picker { switch depth { case 8: if hasAlpha { return &pickerNRGBA8{} } return &pickerNRGB8{} case 16: if hasAlpha { return &pickerNRGBA16{} } return &pickerNRGB16{} case 32: if hasAlpha { return &pickerNRGBA32{} } return &pickerNRGB32{} } return nil } func findNCMYKAPicker(depth int, hasAlpha bool) picker { switch depth { case 8: if hasAlpha { return &pickerNCMYKA8{} } return &pickerNCMYK8{} case 16: if hasAlpha { return &pickerNCMYKA16{} } return &pickerNCMYK16{} } return nil } type pickerPalette struct { Rect image.Rectangle Src []byte Palette color.Palette } func (p *pickerPalette) setSource(rect image.Rectangle, src ...[]byte) { p.Rect, p.Src = rect, src[0] } func (p *pickerPalette) ColorModel() color.Model { return p.Palette } func (p *pickerPalette) Bounds() image.Rectangle { return p.Rect } func (p *pickerPalette) At(x, y int) color.Color { pos := (y-p.Rect.Min.Y)*p.Rect.Dx() + x - p.Rect.Min.X return p.Palette[p.Src[pos]] } type pickerGray1 struct { Rect image.Rectangle Y []byte } func (p *pickerGray1) setSource(rect image.Rectangle, src ...[]byte) { p.Rect, p.Y = rect, src[0] } func (p *pickerGray1) ColorModel() color.Model { return psdColor.Gray1Model } func (p *pickerGray1) Bounds() image.Rectangle { return p.Rect } func (p *pickerGray1) At(x, y int) color.Color { xx := x - p.Rect.Min.X pos := (p.Rect.Dx()+7)>>3*(y-p.Rect.Min.Y) + xx>>3 return psdColor.Gray1{Y: p.Y[pos]&(1<