本文整理匯總了Golang中image.Paletted.Palette方法的典型用法代碼示例。如果您正苦於以下問題:Golang Paletted.Palette方法的具體用法?Golang Paletted.Palette怎麽用?Golang Paletted.Palette使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類image.Paletted
的用法示例。
在下文中一共展示了Paletted.Palette方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Quantize
func (q *MedianCutQuantizer) Quantize(dst *image.Paletted, r image.Rectangle, src image.Image, sp image.Point) {
clip(dst, &r, src, &sp)
if r.Empty() {
return
}
points := make([]point, r.Dx()*r.Dy())
colorSet := make(map[uint32]color.Color, q.NumColor)
i := 0
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
c := src.At(x, y)
r, g, b, _ := c.RGBA()
colorSet[(r>>8)<<16|(g>>8)<<8|b>>8] = c
points[i][0] = int(r)
points[i][1] = int(g)
points[i][2] = int(b)
i++
}
}
if len(colorSet) <= q.NumColor {
// No need to quantize since the total number of colors
// fits within the palette.
dst.Palette = make(color.Palette, len(colorSet))
i := 0
for _, c := range colorSet {
dst.Palette[i] = c
i++
}
} else {
dst.Palette = q.medianCut(points)
}
for y := 0; y < r.Dy(); y++ {
for x := 0; x < r.Dx(); x++ {
// TODO: this should be done more efficiently.
dst.Set(sp.X+x, sp.Y+y, src.At(r.Min.X+x, r.Min.Y+y))
}
}
}
示例2: ripShinyPokemonBack
func ripShinyPokemonBack(rip *sprites.Ripper, number int, form string, outname string) error {
var m *image.Paletted
var err error
if number == 201 && form != "" {
m, err = rip.UnownBack(form)
} else {
m, err = rip.PokemonBack(number)
}
if err != nil {
return err
}
m.Palette = rip.ShinyPalette(number)
if m.Palette == nil {
return errors.New("couldn't get palette")
}
return write(m, outname)
}
示例3: Fusionne
// Merge les deux images de telle sorte qu'après l'opération les pixels de img1 :
// - soient inchangés là où l'index dans img2 est 0
// - prennent la valeur de img2 ailleurs
// Les deux images doivent être de mêmes dimensions exactement
// La palette de img1 est enrichie si nécessaire
func Fusionne(img1 *image.Paletted, img2 *image.Paletted) error {
r1 := img1.Bounds()
r2 := img2.Bounds()
if r1.Min.X != r2.Min.X || r1.Max.X != r2.Max.X || r1.Min.Y != r2.Min.Y || r1.Max.Y != r2.Max.Y {
return errors.New("image dimensions not compatible")
}
p1 := img1.Palette
p2 := img2.Palette
n1 := len(p1)
n2 := len(p2)
op := make([]uint8, n2) // tableau donnant l'index dans la palette de img1 d'une couleur de la palette de img2
for i2 := 0; i2 < n2; i2++ {
c := p2[i2].(color.RGBA)
if i2 < n1 && couleursEgales(c, p1[i2].(color.RGBA)) {
op[i2] = uint8(i2)
} else {
found := false
for i1 := 0; i1 < n1; i1++ {
if couleursEgales(c, p1[i1].(color.RGBA)) {
op[i2] = uint8(i1)
found = true
break
}
}
if !found {
op[i2] = uint8(len(p1))
p1 = append(p1, c)
img1.Palette = p1
}
}
}
l2 := len(img2.Pix)
for x := 0; x < l2; x++ {
if img2.Pix[x] != 0 {
img1.Pix[x] = op[img2.Pix[x]]
}
}
return nil
}
示例4: decode
// decode decodes the IDAT data into an image.
func (d *decoder) decode() (image.Image, error) {
r, err := zlib.NewReader(d)
if err != nil {
return nil, err
}
defer r.Close()
bitsPerPixel := 0
pixOffset := 0
var (
gray *image.Gray
rgba *image.RGBA
paletted *image.Paletted
nrgba *image.NRGBA
gray16 *image.Gray16
rgba64 *image.RGBA64
nrgba64 *image.NRGBA64
img image.Image
)
switch d.cb {
case cbG1, cbG2, cbG4, cbG8:
bitsPerPixel = d.depth
gray = image.NewGray(image.Rect(0, 0, d.width, d.height))
img = gray
case cbGA8:
bitsPerPixel = 16
nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height))
img = nrgba
case cbTC8:
bitsPerPixel = 24
rgba = image.NewRGBA(image.Rect(0, 0, d.width, d.height))
img = rgba
case cbP1, cbP2, cbP4, cbP8:
bitsPerPixel = d.depth
paletted = image.NewPaletted(image.Rect(0, 0, d.width, d.height), d.palette)
img = paletted
case cbTCA8:
bitsPerPixel = 32
nrgba = image.NewNRGBA(image.Rect(0, 0, d.width, d.height))
img = nrgba
case cbG16:
bitsPerPixel = 16
gray16 = image.NewGray16(image.Rect(0, 0, d.width, d.height))
img = gray16
case cbGA16:
bitsPerPixel = 32
nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height))
img = nrgba64
case cbTC16:
bitsPerPixel = 48
rgba64 = image.NewRGBA64(image.Rect(0, 0, d.width, d.height))
img = rgba64
case cbTCA16:
bitsPerPixel = 64
nrgba64 = image.NewNRGBA64(image.Rect(0, 0, d.width, d.height))
img = nrgba64
}
bytesPerPixel := (bitsPerPixel + 7) / 8
// cr and pr are the bytes for the current and previous row.
// The +1 is for the per-row filter type, which is at cr[0].
cr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8)
pr := make([]uint8, 1+(bitsPerPixel*d.width+7)/8)
for y := 0; y < d.height; y++ {
// Read the decompressed bytes.
_, err := io.ReadFull(r, cr)
if err != nil {
return nil, err
}
// Apply the filter.
cdat := cr[1:]
pdat := pr[1:]
switch cr[0] {
case ftNone:
// No-op.
case ftSub:
for i := bytesPerPixel; i < len(cdat); i++ {
cdat[i] += cdat[i-bytesPerPixel]
}
case ftUp:
for i, p := range pdat {
cdat[i] += p
}
case ftAverage:
for i := 0; i < bytesPerPixel; i++ {
cdat[i] += pdat[i] / 2
}
for i := bytesPerPixel; i < len(cdat); i++ {
cdat[i] += uint8((int(cdat[i-bytesPerPixel]) + int(pdat[i])) / 2)
}
case ftPaeth:
filterPaeth(cdat, pdat, bytesPerPixel)
default:
return nil, FormatError("bad filter type")
}
// Convert from bytes to colors.
switch d.cb {
//.........這裏部分代碼省略.........
示例5: decode
// decode reads a GIF image from r and stores the result in d.
func (d *decoder) decode(r io.Reader, configOnly bool) error {
// Add buffering if r does not provide ReadByte.
if rr, ok := r.(reader); ok {
d.r = rr
} else {
d.r = bufio.NewReader(r)
}
err := d.readHeaderAndScreenDescriptor()
if err != nil {
return err
}
if configOnly {
return nil
}
if d.headerFields&fColorMapFollows != 0 {
if d.globalColorMap, err = d.readColorMap(); err != nil {
return err
}
}
Loop:
for err == nil {
var c byte
c, err = d.r.ReadByte()
if err == io.EOF {
break
}
switch c {
case sExtension:
err = d.readExtension()
case sImageDescriptor:
var m *image.Paletted
m, err = d.newImageFromDescriptor()
if err != nil {
break
}
if d.imageFields&fColorMapFollows != 0 {
m.Palette, err = d.readColorMap()
if err != nil {
break
}
// TODO: do we set transparency in this map too? That would be
// d.setTransparency(m.Palette)
} else {
m.Palette = d.globalColorMap
}
var litWidth uint8
litWidth, err = d.r.ReadByte()
if err != nil {
return err
}
if litWidth < 2 || litWidth > 8 {
return fmt.Errorf("gif: pixel size in decode out of range: %d", litWidth)
}
// A wonderfully Go-like piece of magic.
lzwr := lzw.NewReader(&blockReader{r: d.r}, lzw.LSB, int(litWidth))
if _, err = io.ReadFull(lzwr, m.Pix); err != nil {
break
}
// There should be a "0" block remaining; drain that.
c, err = d.r.ReadByte()
if err != nil {
return err
}
if c != 0 {
return errors.New("gif: extra data after image")
}
// Undo the interlacing if necessary.
if d.imageFields&ifInterlace != 0 {
uninterlace(m)
}
d.image = append(d.image, m)
d.delay = append(d.delay, d.delayTime)
d.delayTime = 0 // TODO: is this correct, or should we hold on to the value?
case sTrailer:
break Loop
default:
err = fmt.Errorf("gif: unknown block type: 0x%.2x", c)
}
}
if err != nil {
return err
}
if len(d.image) == 0 {
return io.ErrUnexpectedEOF
}
return nil
}
示例6: readImagePass
// readImagePass reads a single image pass, sized according to the pass number.
func (d *decoder) readImagePass(r io.Reader, pass int, allocateOnly bool) (image.Image, error) {
var bitsPerPixel int = 0
pixOffset := 0
var (
gray *image.Gray
rgba *image.RGBA
paletted *image.Paletted
nrgba *image.NRGBA
gray16 *image.Gray16
rgba64 *image.RGBA64
nrgba64 *image.NRGBA64
img image.Image
)
width, height := d.width, d.height
if d.interlace == itAdam7 && !allocateOnly {
p := interlacing[pass]
// Add the multiplication factor and subtract one, effectively rounding up.
width = (width - p.xOffset + p.xFactor - 1) / p.xFactor
height = (height - p.yOffset + p.yFactor - 1) / p.yFactor
// A PNG image can't have zero width or height, but for an interlaced
// image, an individual pass might have zero width or height. If so, we
// shouldn't even read a per-row filter type byte, so return early.
if width == 0 || height == 0 {
return nil, nil
}
}
switch d.cb {
case cbG1, cbG2, cbG4, cbG8:
bitsPerPixel = d.depth
gray = image.NewGray(image.Rect(0, 0, width, height))
img = gray
case cbGA8:
bitsPerPixel = 16
nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
img = nrgba
case cbTC8:
bitsPerPixel = 24
rgba = image.NewRGBA(image.Rect(0, 0, width, height))
img = rgba
case cbP1, cbP2, cbP4, cbP8:
bitsPerPixel = d.depth
paletted = image.NewPaletted(image.Rect(0, 0, width, height), d.palette)
img = paletted
case cbTCA8:
bitsPerPixel = 32
nrgba = image.NewNRGBA(image.Rect(0, 0, width, height))
img = nrgba
case cbG16:
bitsPerPixel = 16
gray16 = image.NewGray16(image.Rect(0, 0, width, height))
img = gray16
case cbGA16:
bitsPerPixel = 32
nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
img = nrgba64
case cbTC16:
bitsPerPixel = 48
rgba64 = image.NewRGBA64(image.Rect(0, 0, width, height))
img = rgba64
case cbTCA16:
bitsPerPixel = 64
nrgba64 = image.NewNRGBA64(image.Rect(0, 0, width, height))
img = nrgba64
}
if allocateOnly {
return img, nil
}
bytesPerPixel := (bitsPerPixel + 7) / 8
// The +1 is for the per-row filter type, which is at cr[0].
rowSize := 1 + (bitsPerPixel*width+7)/8
// cr and pr are the bytes for the current and previous row.
cr := make([]uint8, rowSize)
pr := make([]uint8, rowSize)
for y := 0; y < height; y++ {
// Read the decompressed bytes.
_, err := io.ReadFull(r, cr)
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return nil, FormatError("not enough pixel data")
}
return nil, err
}
// Apply the filter.
cdat := cr[1:]
pdat := pr[1:]
switch cr[0] {
case ftNone:
// No-op.
case ftSub:
for i := bytesPerPixel; i < len(cdat); i++ {
cdat[i] += cdat[i-bytesPerPixel]
}
case ftUp:
for i, p := range pdat {
cdat[i] += p
}
//.........這裏部分代碼省略.........
示例7: dessine
// dessine la couche sur l'image qui peut avoir une palette différente de la palette standard.
func dessine(img *image.Paletted, couche *Couche) {
imgIndexes := make(map[string]uint8) // similaire à indexes (fond->index) mais relatif à la palette de l'image et non à la palette standard
caseAPalissade := make(map[int32]bool) // map suivant PosKey(x,y) : true ssi une palissade est en x,y
for _, p := range couche.Palissades {
caseAPalissade[PosKey(p.X, p.Y)] = true
}
imgPalette := img.Palette
déplacementsDansPalette := 0
ajoutsPalette := 0
n := len(imgPalette)
nbAbsences := make(map[string]uint) // je note les fonds manquants dans ma palette, ils peuvent correspondre à des évolutions du jeu Braldahim
for _, c := range couche.Cases {
x, y := int(c.X)+SEMI_LARGEUR, SEMI_HAUTEUR-int(c.Y)
key := c.Fond
if caseAPalissade[PosKey(c.X, c.Y)] {
key += ".p"
}
imgIndex, ok := imgIndexes[key] // index de la couleur du fond dans la palette de l'image
if !ok {
index, ok := indexes[key]
if ok {
c := palette[index].(color.RGBA)
if index < uint8(n) && couleursEgales(c, imgPalette[index].(color.RGBA)) { // test rapide : si la couleur est au même index dans imgPalette que dans la palette standard
imgIndex = index
imgIndexes[key] = imgIndex
} else {
found := false
for i := 0; i < n; i++ {
if couleursEgales(c, imgPalette[i].(color.RGBA)) {
found = true
imgIndex = uint8(i)
imgIndexes[key] = imgIndex
break
}
}
if found {
déplacementsDansPalette++
} else {
log.Printf(" couleur \"%s\" absente de la palette de l'image\n", key)
imgIndex = uint8(len(imgPalette))
imgIndexes[key] = imgIndex
img.Palette = append(img.Palette, c)
imgPalette = img.Palette
ajoutsPalette++
}
}
} else { // fond inconnu y compris pour la palette standard
nbAbsences[c.Fond] = nbAbsences[c.Fond] + 1
}
}
img.SetColorIndex(x, y, imgIndex) // si pas ok, ça doit passer transparent (imgIndex=0)
}
if ajoutsPalette+déplacementsDansPalette != 0 {
log.Println(" Transformations palette : ", déplacementsDansPalette, " déplacements et ", ajoutsPalette, "ajouts")
}
if len(nbAbsences) != 0 {
log.Println(" Fonds manquants :")
for fond, nb := range nbAbsences {
log.Println(" ", fond, " : ", nb)
}
}
}