twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

rect.go (2493B)


      1 // Copyright 2019 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package restorable
     16 
     17 import (
     18 	"fmt"
     19 	"image"
     20 
     21 	"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
     22 )
     23 
     24 type rectToPixels struct {
     25 	m map[image.Rectangle][]byte
     26 
     27 	lastR   image.Rectangle
     28 	lastPix []byte
     29 }
     30 
     31 func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) {
     32 	if len(pixels) != 4*width*height {
     33 		panic(fmt.Sprintf("restorable: len(pixels) must be %d but %d", 4*width*height, len(pixels)))
     34 	}
     35 
     36 	if rtp.m == nil {
     37 		rtp.m = map[image.Rectangle][]byte{}
     38 	}
     39 
     40 	newr := image.Rect(x, y, x+width, y+height)
     41 	for r := range rtp.m {
     42 		if r == newr {
     43 			// Replace the region.
     44 			rtp.m[r] = pixels
     45 			if r == rtp.lastR {
     46 				rtp.lastPix = pixels
     47 			}
     48 			return
     49 		}
     50 		if r.Overlaps(newr) {
     51 			panic(fmt.Sprintf("restorable: region (%#v) conflicted with the other region (%#v)", newr, r))
     52 		}
     53 	}
     54 
     55 	// Add the region.
     56 	rtp.m[newr] = pixels
     57 	if newr == rtp.lastR {
     58 		rtp.lastPix = pixels
     59 	}
     60 }
     61 
     62 func (rtp *rectToPixels) remove(x, y, width, height int) {
     63 	if rtp.m == nil {
     64 		return
     65 	}
     66 
     67 	newr := image.Rect(x, y, x+width, y+height)
     68 	for r := range rtp.m {
     69 		if r == newr {
     70 			delete(rtp.m, r)
     71 			return
     72 		}
     73 	}
     74 }
     75 
     76 func (rtp *rectToPixels) at(i, j int) (byte, byte, byte, byte, bool) {
     77 	if rtp.m == nil {
     78 		return 0, 0, 0, 0, false
     79 	}
     80 
     81 	var pix []byte
     82 
     83 	var r *image.Rectangle
     84 	if pt := image.Pt(i, j); pt.In(rtp.lastR) {
     85 		r = &rtp.lastR
     86 		pix = rtp.lastPix
     87 	} else {
     88 		for rr := range rtp.m {
     89 			if pt.In(rr) {
     90 				r = &rr
     91 				rtp.lastR = rr
     92 				pix = rtp.m[rr]
     93 				rtp.lastPix = pix
     94 				break
     95 			}
     96 		}
     97 	}
     98 
     99 	if r == nil {
    100 		return 0, 0, 0, 0, false
    101 	}
    102 
    103 	idx := 4 * ((j-r.Min.Y)*r.Dx() + (i - r.Min.X))
    104 	return pix[idx], pix[idx+1], pix[idx+2], pix[idx+3], true
    105 }
    106 
    107 func (rtp *rectToPixels) apply(img *graphicscommand.Image) {
    108 	// TODO: Isn't this too heavy? Can we merge the operations?
    109 	for r, pix := range rtp.m {
    110 		img.ReplacePixels(pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy())
    111 	}
    112 }