github.com/blackjack/webcam@v0.0.0-20230509180125-87693b3f29dc/formats.go (about)

     1  package webcam
     2  
     3  import "fmt"
     4  
     5  // Represents image format code used by V4L2 subsystem.
     6  // Number of formats can be different in various
     7  // Linux kernel versions
     8  // See /usr/include/linux/videodev2.h for full list
     9  // of supported image formats
    10  type PixelFormat uint32
    11  
    12  // Struct that describes frame size supported by a webcam
    13  // For fixed sizes min and max values will be the same and
    14  // step value will be equal to '0'
    15  type FrameSize struct {
    16  	MinWidth  uint32
    17  	MaxWidth  uint32
    18  	StepWidth uint32
    19  
    20  	MinHeight  uint32
    21  	MaxHeight  uint32
    22  	StepHeight uint32
    23  }
    24  
    25  // FrameRate represents all possible framerates supported by a webcam
    26  // for a given pixel format and frame size. For discrete returns min
    27  // and max values will be the same and step value will be equal to '0'
    28  // Stepwise returns are represented as a range of values with a step.
    29  type FrameRate struct {
    30  	MinNumerator  uint32
    31  	MaxNumerator  uint32
    32  	StepNumerator uint32
    33  
    34  	MinDenominator  uint32
    35  	MaxDenominator  uint32
    36  	StepDenominator uint32
    37  }
    38  
    39  // Return string representation of FrameRate struct. e.g.1/30 for
    40  // discrete framerates and [1-2;1]/[10-60;1] for stepwise framerates.
    41  func (f FrameRate) String() string {
    42  	if f.StepNumerator == 0 && f.StepDenominator == 0 {
    43  		return fmt.Sprintf("%d/%d", f.MinNumerator, f.MinDenominator)
    44  	} else {
    45  		return fmt.Sprintf("[%d-%d;%d]/[%d-%d;%d]", f.MinNumerator, f.MaxNumerator, f.StepNumerator, f.MinDenominator, f.MaxDenominator, f.StepDenominator)
    46  	}
    47  }
    48  
    49  // Returns string representation of frame size, e.g.
    50  // 1280x720 for fixed-size frames and
    51  // [320-640;160]x[240-480;160] for stepwise-sized frames
    52  func (s FrameSize) GetString() string {
    53  	if s.StepWidth == 0 && s.StepHeight == 0 {
    54  		return fmt.Sprintf("%dx%d", s.MaxWidth, s.MaxHeight)
    55  	} else {
    56  		return fmt.Sprintf("[%d-%d;%d]x[%d-%d;%d]", s.MinWidth, s.MaxWidth, s.StepWidth, s.MinHeight, s.MaxHeight, s.StepHeight)
    57  	}
    58  }