Skip to content

Commit

Permalink
[wpiutil,cscore] Move VideoMode.PixelFormat to wpiutil (#6097)
Browse files Browse the repository at this point in the history
This tracks the RawFrame move.
  • Loading branch information
PeterJohnson authored Dec 26, 2023
1 parent ab78b93 commit 5550870
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import edu.wpi.first.cscore.VideoException;
import edu.wpi.first.cscore.VideoListener;
import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.cscore.VideoMode.PixelFormat;
import edu.wpi.first.cscore.VideoSink;
import edu.wpi.first.cscore.VideoSource;
import edu.wpi.first.networktables.BooleanEntry;
Expand All @@ -27,6 +26,7 @@
import edu.wpi.first.networktables.StringArrayTopic;
import edu.wpi.first.networktables.StringEntry;
import edu.wpi.first.networktables.StringPublisher;
import edu.wpi.first.util.PixelFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -686,7 +686,7 @@ public static AxisCamera addAxisCamera(String name, String[] hosts) {
*/
public static MjpegServer addSwitchedCamera(String name) {
// create a dummy CvSource
CvSource source = new CvSource(name, VideoMode.PixelFormat.kMJPEG, 160, 120, 30);
CvSource source = new CvSource(name, PixelFormat.kMJPEG, 160, 120, 30);
MjpegServer server = startAutomaticCapture(source);
synchronized (CameraServer.class) {
m_fixedSources.put(server.getHandle(), source.getHandle());
Expand Down Expand Up @@ -801,7 +801,7 @@ public static CvSink getVideo(String name) {
* @return OpenCV source for the MJPEG stream
*/
public static CvSource putVideo(String name, int width, int height) {
CvSource source = new CvSource(name, VideoMode.PixelFormat.kMJPEG, width, height, 30);
CvSource source = new CvSource(name, PixelFormat.kMJPEG, width, height, 30);
startAutomaticCapture(source);
return source;
}
Expand Down
2 changes: 1 addition & 1 deletion cscore/src/main/java/edu/wpi/first/cscore/CvSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package edu.wpi.first.cscore;

import edu.wpi.first.cscore.VideoMode.PixelFormat;
import edu.wpi.first.util.PixelFormat;
import org.opencv.core.Mat;

/**
Expand Down
3 changes: 2 additions & 1 deletion cscore/src/main/java/edu/wpi/first/cscore/CvSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package edu.wpi.first.cscore;

import edu.wpi.first.util.PixelFormat;
import org.opencv.core.Mat;

/**
Expand Down Expand Up @@ -32,7 +33,7 @@ public CvSource(String name, VideoMode mode) {
* @param height height
* @param fps fps
*/
public CvSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
public CvSource(String name, PixelFormat pixelFormat, int width, int height, int fps) {
super(CameraServerCvJNI.createCvSource(name, pixelFormat.getValue(), width, height, fps));
}

Expand Down
30 changes: 2 additions & 28 deletions cscore/src/main/java/edu/wpi/first/cscore/VideoMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@

package edu.wpi.first.cscore;

import edu.wpi.first.util.PixelFormat;
import java.util.Objects;

/** Video mode. */
@SuppressWarnings("MemberName")
public class VideoMode {
public enum PixelFormat {
kUnknown(0),
kMJPEG(1),
kYUYV(2),
kRGB565(3),
kBGR(4),
kGray(5),
kY16(6),
kUYVY(7);

private final int value;

PixelFormat(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

private static final PixelFormat[] m_pixelFormatValues = PixelFormat.values();

public static PixelFormat getPixelFormatFromInt(int pixelFormat) {
return m_pixelFormatValues[pixelFormat];
}

/**
* Create a new video mode.
*
Expand All @@ -45,7 +19,7 @@ public static PixelFormat getPixelFormatFromInt(int pixelFormat) {
* @param fps The camera's frames per second.
*/
public VideoMode(int pixelFormat, int width, int height, int fps) {
this.pixelFormat = getPixelFormatFromInt(pixelFormat);
this.pixelFormat = PixelFormat.getFromInt(pixelFormat);
this.width = width;
this.height = height;
this.fps = fps;
Expand Down
6 changes: 4 additions & 2 deletions cscore/src/main/java/edu/wpi/first/cscore/VideoSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package edu.wpi.first.cscore;

import edu.wpi.first.util.PixelFormat;

/**
* A source for video that provides a sequence of frames. Each frame may consist of multiple images
* (e.g. from a stereo or depth camera); these are called channels.
Expand Down Expand Up @@ -235,7 +237,7 @@ public boolean setVideoMode(VideoMode mode) {
* @param fps desired FPS
* @return True if set successfully
*/
public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
public boolean setVideoMode(PixelFormat pixelFormat, int width, int height, int fps) {
return CameraServerJNI.setSourceVideoMode(m_handle, pixelFormat.getValue(), width, height, fps);
}

Expand All @@ -245,7 +247,7 @@ public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int he
* @param pixelFormat desired pixel format
* @return True if set successfully
*/
public boolean setPixelFormat(VideoMode.PixelFormat pixelFormat) {
public boolean setPixelFormat(PixelFormat pixelFormat) {
return CameraServerJNI.setSourcePixelFormat(m_handle, pixelFormat.getValue());
}

Expand Down
5 changes: 3 additions & 2 deletions cscore/src/main/java/edu/wpi/first/cscore/raw/RawSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.cscore.ImageSource;
import edu.wpi.first.cscore.VideoMode;
import edu.wpi.first.util.PixelFormat;
import edu.wpi.first.util.RawFrame;

/**
Expand Down Expand Up @@ -36,7 +37,7 @@ public RawSource(String name, VideoMode mode) {
* @param height height
* @param fps fps
*/
public RawSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
public RawSource(String name, PixelFormat pixelFormat, int width, int height, int fps) {
super(CameraServerJNI.createRawSource(name, pixelFormat.getValue(), width, height, fps));
}

Expand Down Expand Up @@ -72,7 +73,7 @@ protected void putFrame(long data, int width, int height, int pixelFormat, int t
* @param totalData length of data in total
*/
protected void putFrame(
long data, int width, int height, VideoMode.PixelFormat pixelFormat, int totalData) {
long data, int width, int height, PixelFormat pixelFormat, int totalData) {
CameraServerJNI.putRawSourceFrame(
m_handle, data, width, height, pixelFormat.getValue(), totalData);
}
Expand Down
5 changes: 3 additions & 2 deletions cscore/src/test/java/edu/wpi/first/cscore/VideoModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import edu.wpi.first.util.PixelFormat;
import org.junit.jupiter.api.Test;

class VideoModeTest {
@Test
void equalityTest() {
VideoMode a = new VideoMode(VideoMode.PixelFormat.kMJPEG, 1920, 1080, 30);
VideoMode b = new VideoMode(VideoMode.PixelFormat.kMJPEG, 1920, 1080, 30);
VideoMode a = new VideoMode(PixelFormat.kMJPEG, 1920, 1080, 30);
VideoMode b = new VideoMode(PixelFormat.kMJPEG, 1920, 1080, 30);

assertEquals(a, b);
assertNotEquals(a, null);
Expand Down
52 changes: 52 additions & 0 deletions wpiutil/src/main/java/edu/wpi/first/util/PixelFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package edu.wpi.first.util;

/** Image pixel format. */
public enum PixelFormat {
/** Unknown format. */
kUnknown(0),
/** Motion-JPEG (compressed image data). */
kMJPEG(1),
/** YUY 4:2:2, 16 bpp. */
kYUYV(2),
/** RGB 5-6-5, 16 bpp. */
kRGB565(3),
/** BGR 8-8-8, 24 bpp. */
kBGR(4),
/** Grayscale, 8 bpp. */
kGray(5),
/** Grayscale, 16 bpp. */
kY16(6),
/** YUV 4:2:2, 16 bpp. */
kUYVY(7);

private final int value;

PixelFormat(int value) {
this.value = value;
}

/**
* Gets the integer value of the pixel format.
*
* @return Integer value
*/
public int getValue() {
return value;
}

private static final PixelFormat[] s_values = values();

/**
* Gets a PixelFormat enum value from its integer value.
*
* @param pixelFormat integer value
* @return Enum value
*/
public static PixelFormat getFromInt(int pixelFormat) {
return s_values[pixelFormat];
}
}

0 comments on commit 5550870

Please sign in to comment.