github.com/aamcrae/webcam@v0.0.0-20210915060337-934acc13bdc3/frame/rgb.go (about)

     1  // Copyright 2019 Google LLC
     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  //     https://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 frame
    16  
    17  import (
    18  	"fmt"
    19  	"image"
    20  	"image/color"
    21  	"runtime"
    22  )
    23  
    24  type fRGB struct {
    25  	model   color.Model
    26  	b       image.Rectangle
    27  	stride  int
    28  	roffs   int
    29  	goffs   int
    30  	boffs   int
    31  	frame   []byte
    32  	release func()
    33  }
    34  
    35  // Register framers for these formats.
    36  func init() {
    37  	RegisterFramer("RGB3", newFramerRGB3)
    38  	RegisterFramer("BGR3", newFramerBGR3)
    39  }
    40  
    41  // Return a function that is used as a framer for RGB3.
    42  func newFramerRGB3(w, h, stride, size int) func([]byte, func()) (Frame, error) {
    43  	return newRGBFramer(w, h, stride, size, 0, 1, 2)
    44  }
    45  
    46  // Return a function that is used as a framer for BGR3.
    47  func newFramerBGR3(w, h, stride, size int) func([]byte, func()) (Frame, error) {
    48  	return newRGBFramer(w, h, stride, size, 2, 1, 0)
    49  }
    50  
    51  // Return a function that is used as a generic RGB framer.
    52  func newRGBFramer(w, h, stride, size, r, g, b int) func([]byte, func()) (Frame, error) {
    53  	return func(buf []byte, rel func()) (Frame, error) {
    54  		return frameRGB(size, stride, w, h, r, g, b, buf, rel)
    55  	}
    56  }
    57  
    58  // Wrap a raw webcam frame in a Frame so that it can be used as an image.
    59  func frameRGB(size, stride, w, h, rof, gof, bof int, b []byte, rel func()) (Frame, error) {
    60  	if len(b) != size {
    61  		if rel != nil {
    62  			defer rel()
    63  		}
    64  		return nil, fmt.Errorf("Wrong frame length (exp: %d, read %d)", size, len(b))
    65  	}
    66  	f := &fRGB{model: color.RGBAModel, b: image.Rect(0, 0, w, h), stride: stride,
    67  		roffs: rof, goffs: gof, boffs: bof, frame: b, release: rel}
    68  	runtime.SetFinalizer(f, func(obj Frame) {
    69  		obj.Release()
    70  	})
    71  	return f, nil
    72  }
    73  
    74  func (f *fRGB) ColorModel() color.Model {
    75  	return f.model
    76  }
    77  
    78  func (f *fRGB) Bounds() image.Rectangle {
    79  	return f.b
    80  }
    81  
    82  func (f *fRGB) At(x, y int) color.Color {
    83  	i := f.stride*y + x*3
    84  	return color.RGBA{f.frame[i+f.roffs], f.frame[i+f.goffs], f.frame[i+f.boffs], 0xFF}
    85  }
    86  
    87  // Done with frame, release back to camera (if required).
    88  func (f *fRGB) Release() {
    89  	if f.release != nil {
    90  		f.release()
    91  		// Make sure it only gets called once.
    92  		f.release = nil
    93  	}
    94  }