summaryrefslogtreecommitdiff
path: root/test/stream/stream_colorspace.cpp
blob: 1b7afe65fbfd87a2b7aa5368e2ca9bf709f5aeea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2022, Ideas on Board Oy.
 *
 * stream_colorspace.cpp - Stream colorspace adjustment test
 */

#include <iostream>

#include <libcamera/camera.h>
#include <libcamera/formats.h>
#include <libcamera/stream.h>

#include "test.h"

using namespace libcamera;
using namespace std;

class TestCameraConfiguration : public CameraConfiguration
{
public:
	TestCameraConfiguration()
		: CameraConfiguration()
	{
	}

	Status validate() override
	{
		return validateColorSpaces();
	}
};

class StreamColorSpaceTest : public Test
{
protected:
	int run()
	{
		TestCameraConfiguration config;

		StreamConfiguration cfg;
		cfg.size = { 640, 320 };
		cfg.pixelFormat = formats::YUV422;
		cfg.colorSpace = ColorSpace::Srgb;
		config.addConfiguration(cfg);

		StreamConfiguration &streamCfg = config.at(0);

		/*
		 * YUV pixelformat with sRGB colorspace should have Y'CbCr encoding
		 * adjusted.
		 */
		config.validate();
		if (streamCfg.colorSpace->ycbcrEncoding == ColorSpace::YcbcrEncoding::None) {
			cerr << "YUV format must have YCbCr encoding" << endl;
			return TestFail;
		}

		/*
		 * For YUV pixelFormat, encoding should be picked up according
		 * to primaries and transfer function, if 'None' is specified.
		 */
		streamCfg.pixelFormat = formats::YUV422;
		streamCfg.colorSpace = ColorSpace(ColorSpace::Primaries::Rec2020,
						  ColorSpace::TransferFunction::Rec709,
						  ColorSpace::YcbcrEncoding::None,
						  ColorSpace::Range::Limited);
		config.validate();
		if (streamCfg.colorSpace->ycbcrEncoding != ColorSpace::YcbcrEncoding::Rec2020) {
			cerr << "Failed to adjust colorspace Y'CbCr encoding according"
			     << " to primaries and transfer function" << endl;
			return TestFail;
		}

		/* For RGB pixelFormat, Sycc colorspace should get adjusted to sRGB. */
		streamCfg.pixelFormat = formats::RGB888;
		streamCfg.colorSpace = ColorSpace::Sycc;
		config.validate();
		if (streamCfg.colorSpace != ColorSpace::Srgb) {
			cerr << "RGB format's colorspace should be set to Srgb" << endl;
			return TestFail;
		}

		/* Raw formats should always set colorspace to ColorSpace::Raw. */
		streamCfg.pixelFormat = formats::SBGGR8;
		streamCfg.colorSpace = ColorSpace::Rec709;
		config.validate();
		if (streamCfg.colorSpace != ColorSpace::Raw) {
			cerr << "Raw format must always have Raw colorspace" << endl;
			return TestFail;
		}

		return TestPass;
	}
};

TEST_REGISTER(StreamColorSpaceTest)