File: argyll_cgats.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (648 lines) | stat: -rw-r--r-- 22,701 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# -*- coding: utf-8 -*-

# import decimal
# Decimal = decimal.Decimal
import decimal
from decimal import Decimal
import os
import traceback
from io import BytesIO
from time import strftime

from DisplayCAL import colormath
from DisplayCAL import localization as lang
from DisplayCAL.cgats import (
    CGATS,
    CGATSError,
    CGATSInvalidError,
    CGATSInvalidOperationError,
    CGATSKeyError,
    CGATSTypeError,
    CGATSValueError,
)
from DisplayCAL.icc_profile import (
    ICCProfile,
    ICCProfileInvalidError,
    Text,
    TextDescriptionType,
    VideoCardGammaTableType,
    VideoCardGammaType,
    WcsProfilesTagType,
)
from DisplayCAL.debughelpers import Error
from DisplayCAL.options import debug

cals = {}


def quote_nonoption_args(args):
    """Put quotes around all arguments which are not options.

    (ie. which do not start with a hyphen '-')
    """
    args = list(args)
    for i, arg in enumerate(args):
        # first convert everything to bytes
        if not isinstance(arg, bytes):
            arg = bytes(str(arg), "utf-8")
        args[i] = b'"%s"' % arg if arg[:1] != b"-" else arg
    return args


def add_dispcal_options_to_cal(cal, options_dispcal):
    # Add dispcal options to cal
    options_dispcal = quote_nonoption_args(options_dispcal)
    try:
        cgats = CGATS(cal)
        cgats[0].add_section("ARGYLL_DISPCAL_ARGS", b" ".join(options_dispcal))
        return cgats
    except Exception:
        print(traceback.format_exc())


def add_options_to_ti3(ti3, options_dispcal=None, options_colprof=None):
    # Add dispcal and colprof options to ti3
    try:
        cgats = CGATS(ti3)
        if options_colprof:
            options_colprof = quote_nonoption_args(options_colprof)
            cgats[0].add_section(
                "ARGYLL_COLPROF_ARGS",
                b" ".join(options_colprof),
            )

        if options_dispcal and 1 in cgats:
            options_dispcal = quote_nonoption_args(options_dispcal)
            cgats[1].add_section(
                "ARGYLL_DISPCAL_ARGS",
                b" ".join(options_dispcal),
            )
        return cgats
    except BaseException:
        print(traceback.format_exc())


def cal_to_fake_profile(cal):
    """Create and return a 'fake' ICCProfile with just a vcgt tag.

    cal must refer to a valid Argyll CAL file and can be a CGATS instance
    or a filename.
    """
    vcgt, cal = cal_to_vcgt(cal, True)
    if not vcgt:
        return
    profile = ICCProfile()
    profile.fileName = cal.filename
    profile._data = b"\0" * 128
    profile._tags.desc = TextDescriptionType(b"", "desc")
    profile._tags.desc.ASCII = str(os.path.basename(cal.filename)).encode(
        "ascii", "asciize"
    )
    profile._tags.desc.Unicode = str(os.path.basename(cal.filename))
    profile._tags.vcgt = vcgt
    profile.size = len(profile.data)
    profile.is_loaded = True
    return profile


def cal_to_vcgt(cal, return_cgats=False):
    """Create a vcgt tag from calibration data.

    cal must refer to a valid Argyll CAL file and can be a CGATS instance
    or a filename.
    """
    if not isinstance(cal, CGATS):
        try:
            cal = CGATS(cal)
        except (
            IOError,
            CGATSInvalidError,
            CGATSInvalidOperationError,
            CGATSKeyError,
            CGATSTypeError,
            CGATSValueError,
        ) as exception:
            print(f"Warning - couldn't process CGATS file '{cal}': {exception}")
            return None
    required_fields = ("RGB_I", "RGB_R", "RGB_G", "RGB_B")
    if data_format := cal.queryv1("DATA_FORMAT"):
        for field in required_fields:
            if field.encode("utf-8") not in list(data_format.values()):
                if debug:
                    print(f"[D] Missing required field: {field}")
                return None
        for field in list(data_format.values()):
            if field.decode("utf-8") not in required_fields:
                if debug:
                    print(f"[D] Unknown field: {field}")
                return None
    entries = cal.queryv(required_fields)
    if len(entries) < 1:
        if debug:
            print(f"[D] No entries found in calibration {cal.filename}")
        return None
    vcgt = VideoCardGammaTableType(b"", "vcgt")
    vcgt.update(
        {
            "channels": 3,
            "entryCount": len(entries),
            "entrySize": 2,
            "data": [[], [], []],
        }
    )
    for n in entries:
        for i in range(3):
            vcgt.data[i].append(entries[n][i + 1] * 65535.0)
    if return_cgats:
        return vcgt, cal
    return vcgt


def can_update_cal(path):
    """Check if cal can be updated by checking for required fields."""
    try:
        calstat = os.stat(path)
    except Exception as exception:
        print(f"Warning - os.stat('{path}') failed: {exception}")
        return False
    if path not in cals or cals[path].mtime != calstat.st_mtime:
        try:
            cal = CGATS(path)
        except (
            IOError,
            CGATSInvalidError,
            CGATSInvalidOperationError,
            CGATSKeyError,
            CGATSTypeError,
            CGATSValueError,
        ) as exception:
            if path in cals:
                del cals[path]
            print(f"Warning - couldn't process CGATS file '{path}': {exception}")
        else:
            if cal.queryv1("DEVICE_CLASS") == "DISPLAY" and None not in (
                cal.queryv1("TARGET_WHITE_XYZ"),
                cal.queryv1("TARGET_GAMMA"),
                cal.queryv1("BLACK_POINT_CORRECTION"),
                cal.queryv1("QUALITY"),
            ):
                cals[path] = cal
    return path in cals and cals[path].mtime == calstat.st_mtime


def extract_cal_from_profile(
    profile, out_cal_path=None, raise_on_missing_cal=True, prefer_cal=False
):
    """Extract calibration from 'targ' tag in profile or vcgt as fallback"""
    white = False

    # Check if calibration is included in TI3
    targ = profile.tags.get("targ", profile.tags.get("CIED"))
    if isinstance(targ, Text):
        cal = extract_cal_from_ti3(targ)
        if cal:
            check = cal
            get_cgats = CGATS
            arg = cal
    else:
        cal = None
    if not cal:
        # Convert calibration information from embedded WCS profile
        # (if present) to VideCardFormulaType if the latter is not present
        if (
            isinstance(profile.tags.get("MS00"), WcsProfilesTagType)
            and "vcgt" not in profile.tags
        ):
            profile.tags["vcgt"] = profile.tags["MS00"].get_vcgt()

        # Get the calibration from profile vcgt
        check = isinstance(profile.tags.get("vcgt"), VideoCardGammaType)
        get_cgats = vcgt_to_cal
        arg = profile

    if check:
        try:
            cgats = get_cgats(arg)
        except (IOError, CGATSError) as e:
            traceback.print_exc()
            raise Error(lang.getstr("cal_extraction_failed")) from e
    elif raise_on_missing_cal:
        raise Error(lang.getstr("profile.no_vcgt"))
    else:
        return False
    if (
        cal
        and not prefer_cal
        and isinstance(profile.tags.get("vcgt"), VideoCardGammaType)
    ):
        # When vcgt is nonlinear, prefer it
        # Check for video levels encoding
        if cgats.queryv1("TV_OUTPUT_ENCODING") == b"YES":
            black, white = (16, 235)
        elif output_enc := cgats.queryv1("OUTPUT_ENCODING"):
            try:
                black, white = (float(v) for v in output_enc.split())
            except (TypeError, ValueError):
                white = False
        cgats = vcgt_to_cal(profile)
        if white and (black, white) != (0, 255):
            print(f"Need to un-scale vcgt from video levels ({black}..{white})")
            # Need to un-scale video levels
            if data := cgats.queryv1("DATA"):
                print(f"Un-scaling vcgt from video levels ({black}..{white})")
                encoding_mismatch = False
                # For video encoding the extra bits of
                # precision are created by bit shifting rather
                # than scaling, so we need to scale the fp
                # value to account for this
                oldmin = (black / 256.0) * (65536 / 65535.0)
                oldmax = (white / 256.0) * (65536 / 65535.0)
                for entry in data.values():
                    for column in "RGB":
                        v_old = entry[f"RGB_{column}"]
                        lvl = round(v_old * (65535 / 65536.0) * 256, 2)
                        if lvl < round(black, 2) or lvl > round(white, 2):
                            # Can't be right. Metadata says it's video encoded,
                            # but clearly exceeds the encoding range.
                            print(
                                f"Warning: Metadata claims video levels ("
                                f"{round(black, 2)}..{round(white, 2)}) but "
                                f"vcgt value {lvl} exceeds encoding range. "
                                f"Using values as-is."
                            )
                            encoding_mismatch = True
                            break
                        v_new = colormath.convert_range(v_old, oldmin, oldmax, 0, 1)
                        entry[f"RGB_{column}"] = min(max(v_new, 0), 1)
                    if encoding_mismatch:
                        break
                if encoding_mismatch:
                    cgats = vcgt_to_cal(profile)
                # Add video levels hint to CGATS
                elif (black, white) == (16, 235):
                    cgats[0].add_keyword("TV_OUTPUT_ENCODING", "YES")
                else:
                    cgats[0].add_keyword(
                        "OUTPUT_ENCODING",
                        b" ".join(bytes(str(v), "utf-8") for v in (black, white)),
                    )
            else:
                print("Warning - no un-scaling applied - no calibration data!")
    if out_cal_path:
        cgats.write(out_cal_path)
    return cgats


def extract_cal_from_ti3(ti3):
    """Extract and return the CAL section of a TI3.

    ti3 can be a file object or a string holding the data.
    """
    if isinstance(ti3, CGATS):
        ti3 = bytes(ti3)
    if isinstance(ti3, bytes):
        ti3 = BytesIO(ti3)
    cal = False
    cal_lines = []
    for line in ti3:
        line = line.strip()
        if line == b"CAL":
            line = b"CAL    "  # Make sure CGATS file identifiers are
            # always a minimum of 7 characters
            cal = True
        if cal:
            cal_lines.append(line)
            if line == b"END_DATA":
                break
    try:
        ti3.close()
    except AttributeError:
        pass

    return b"\n".join(cal_lines)


def extract_fix_copy_cal(source_filename, target_filename=None):
    """Return the CAL section from a profile's embedded measurement data.

    Try to 'fix it' (add information needed to make the resulting .cal file
    'updatable') and optionally copy it to target_filename.
    """
    from DisplayCAL.worker import get_options_from_profile

    try:
        profile = ICCProfile(source_filename)
    except (IOError, ICCProfileInvalidError) as exception:
        return exception
    if "CIED" not in profile.tags and "targ" not in profile.tags:
        return None
    cal_lines = []
    ti3 = BytesIO(profile.tags.get("CIED", b"") or profile.tags.get("targ", b""))
    ti3_lines = [line.strip() for line in ti3]
    ti3.close()
    cal_found = False
    for line in ti3_lines:
        line = line.strip()
        if line == b"CAL":
            line = b"CAL    "  # Make sure CGATS file identifiers are always a minimum of 7 characters
            cal_found = True
        if not cal_found:
            continue
        cal_lines.append(line)
        if line != b'DEVICE_CLASS "DISPLAY"':
            continue
        options_dispcal = get_options_from_profile(profile)[0]
        if not options_dispcal:
            continue
        whitepoint = False
        # b = profile.tags.lumi.Y
        for o in options_dispcal:
            if o[0] == b"y":
                cal_lines.append(b'KEYWORD "DEVICE_TYPE"')
                if o[1] == b"c":
                    cal_lines.append(b'DEVICE_TYPE "CRT"')
                else:
                    cal_lines.append(b'DEVICE_TYPE "LCD"')
                continue
            if o[0] in (b"t", b"T"):
                continue
            if o[0] == b"w":
                continue
            if o[0] in (b"g", b"G"):
                if o[1:] == b"240":
                    trc = b"SMPTE240M"
                elif o[1:] == b"709":
                    trc = b"REC709"
                elif o[1:] == b"l":
                    trc = b"L_STAR"
                elif o[1:] == b"s":
                    trc = b"sRGB"
                else:
                    trc = o[1:]
                    if o[0] == b"G":
                        try:
                            trc = 0 - Decimal(trc)
                        except decimal.InvalidOperation:
                            continue
                cal_lines.extend(
                    (b'KEYWORD "TARGET_GAMMA"', b'TARGET_GAMMA "%s"' % trc)
                )
                continue
            if o[0] == b"f":
                cal_lines.extend(
                    (
                        b'KEYWORD "DEGREE_OF_BLACK_OUTPUT_OFFSET"',
                        b'DEGREE_OF_BLACK_OUTPUT_OFFSET "%s"' % o[1:],
                    )
                )

                continue
            if o[0] == b"k":
                cal_lines.extend(
                    (
                        b'KEYWORD "BLACK_POINT_CORRECTION"',
                        b'BLACK_POINT_CORRECTION "%s"' % o[1:],
                    )
                )

                continue
            if o[0] == b"B":
                cal_lines.extend(
                    (
                        b'KEYWORD "TARGET_BLACK_BRIGHTNESS"',
                        b'TARGET_BLACK_BRIGHTNESS "%s"' % o[1:],
                    )
                )

                continue
            if o[0] == b"q":
                if o[1] == b"l":
                    q = b"low"
                elif o[1] == b"m":
                    q = b"medium"
                else:
                    q = b"high"
                cal_lines.extend(
                    (b'KEYWORD "QUALITY"', b'QUALITY "%s"' % q)
                )
        if not whitepoint:
            cal_lines.extend(
                (
                    b'KEYWORD "NATIVE_TARGET_WHITE"',
                    b'NATIVE_TARGET_WHITE ""',
                )
            )
    if cal_lines:
        if target_filename:
            try:
                with open(target_filename, "wb") as f:
                    f.write(b"\n".join(cal_lines))
            except Exception as exception:
                return exception
        return cal_lines


def extract_device_gray_primaries(
    ti3, gray=True, logfn=None, include_neutrals=False, neutrals_ab_threshold=0.1
):
    """Extract gray or primaries into new TI3

    Return extracted ti3, extracted RGB to XYZ mapping and remaining RGB to XYZ
    """
    filename = ti3.filename
    ti3 = ti3.queryi1("DATA")
    ti3.filename = filename
    ti3_extracted = CGATS(
        b"""CTI3
DEVICE_CLASS "DISPLAY"
COLOR_REP "RGB_XYZ"
BEGIN_DATA_FORMAT
END_DATA_FORMAT
BEGIN_DATA
END_DATA"""
    )[0]
    ti3_extracted.DATA_FORMAT.update(ti3.DATA_FORMAT)
    subset = [(100.0, 100.0, 100.0), (0.0, 0.0, 0.0)]
    if not gray:
        subset.extend(
            [
                (100.0, 0.0, 0.0),
                (0.0, 100.0, 0.0),
                (0.0, 0.0, 100.0),
                (50.0, 50.0, 50.0),
            ]
        )
        if logfn:
            logfn(f"Extracting neutrals and primaries from {ti3.filename}")
    elif logfn:
        logfn(f"Extracting neutrals from {ti3.filename}")
    RGB_XYZ_extracted = {}
    RGB_XYZ_remaining = {}
    dupes = {}
    if include_neutrals:
        white = ti3.get_white_cie("XYZ")
        str_thresh = str(neutrals_ab_threshold)
        round_digits = len(str_thresh[str_thresh.find(".") + 1 :])
    for i in ti3.DATA:
        item = ti3.DATA[i]
        if not i:
            # Check if fields are missing
            for prefix in ("RGB", "XYZ"):
                for suffix in prefix:
                    key = f"{prefix}_{suffix}"
                    if key not in item:
                        raise Error(
                            lang.getstr(
                                "error.testchart.missing_fields", (ti3.filename, key)
                            )
                        )
        RGB = (item["RGB_R"], item["RGB_G"], item["RGB_B"])
        XYZ = (item["XYZ_X"], item["XYZ_Y"], item["XYZ_Z"])
        for RGB_XYZ in (RGB_XYZ_extracted, RGB_XYZ_remaining):
            if RGB not in RGB_XYZ:
                continue
            if RGB != (100.0, 100.0, 100.0):
                # Add to existing values for averaging later
                # if it's not white (all other readings are scaled to the
                # white Y by dispread, so we don't alter it. Note that it's
                # always the first encountered white that will have Y = 100,
                # even if subsequent white readings may be higher)
                XYZ = tuple(RGB_XYZ[RGB][i] + XYZ[i] for i in range(3))
                if RGB not in dupes:
                    dupes[RGB] = 1.0
                dupes[RGB] += 1.0
            elif RGB in subset:
                # We have white already, remove it from the subset so any
                # additional white readings we encounter are ignored
                subset.remove(RGB)
        if (
            gray
            and (
                item["RGB_R"] == item["RGB_G"] == item["RGB_B"]
                or (
                    include_neutrals
                    and all(
                        round(abs(v), round_digits) <= neutrals_ab_threshold
                        for v in colormath.XYZ2Lab(
                            item["XYZ_X"],
                            item["XYZ_Y"],
                            item["XYZ_Z"],
                            whitepoint=white,
                        )[1:]
                    )
                )
            )
            and RGB not in [(100.0, 100.0, 100.0), (0.0, 0.0, 0.0)]
        ) or RGB in subset:
            ti3_extracted.DATA.add_data(item)
            RGB_XYZ_extracted[RGB] = XYZ
        elif RGB not in [(100.0, 100.0, 100.0), (0.0, 0.0, 0.0)]:
            RGB_XYZ_remaining[RGB] = XYZ
    for RGB, count in dupes.items():
        for RGB_XYZ in (RGB_XYZ_extracted, RGB_XYZ_remaining):
            if RGB in RGB_XYZ:
                # Average values
                XYZ = tuple(RGB_XYZ[RGB][i] / count for i in range(3))
                RGB_XYZ[RGB] = XYZ
    return ti3_extracted, RGB_XYZ_extracted, RGB_XYZ_remaining


def ti3_to_ti1(ti3_data):
    """Create and return TI1 data converted from TI3.

    ti3_data can be a file object, a list of strings or a string holding the data.
    """
    ti3 = CGATS(ti3_data)
    if not ti3:
        return ""
    ti3[0].type = b"CTI1"
    ti3[0].DESCRIPTOR = b"Argyll Calibration Target chart information 1"
    ti3[0].ORIGINATOR = b"Argyll targen"
    if hasattr(ti3[0], "COLOR_REP"):
        color_rep = ti3[0].COLOR_REP.split(b"_")[0]
    else:
        color_rep = b"RGB"
    ti3[0].add_keyword("COLOR_REP", color_rep)
    ti3[0].remove_keyword("DEVICE_CLASS")
    if hasattr(ti3[0], "LUMINANCE_XYZ_CDM2"):
        ti3[0].remove_keyword("LUMINANCE_XYZ_CDM2")
    if hasattr(ti3[0], "ARGYLL_COLPROF_ARGS"):
        del ti3[0].ARGYLL_COLPROF_ARGS
    return bytes(ti3[0])


def vcgt_to_cal(profile):
    """Return a CAL (CGATS instance) from vcgt."""
    cgats = CGATS(file_identifier=b"CAL")
    context = cgats.add_data({"DESCRIPTOR": b"Argyll Device Calibration State"})
    context.add_data({"ORIGINATOR": b"vcgt"})
    context.add_data(
        {
            "CREATED": bytes(
                strftime("%a %b %d %H:%M:%S %Y", profile.dateTime.timetuple()),
                "utf-8",
                "replace",
            )
        }
    )
    context.add_keyword("DEVICE_CLASS", b"DISPLAY")
    context.add_keyword("COLOR_REP", b"RGB")
    context.add_keyword("RGB_I")
    key = "DATA_FORMAT"
    context[key] = CGATS()
    context[key].key = key
    context[key].parent = context
    context[key].root = cgats
    context[key].type = key.encode("utf-8")
    context[key].add_data((b"RGB_I", b"RGB_R", b"RGB_G", b"RGB_B"))
    key = "DATA"
    context[key] = CGATS()
    context[key].key = key
    context[key].parent = context
    context[key].root = cgats
    context[key].type = key.encode("utf-8")
    values = profile.tags.vcgt.getNormalizedValues()
    for i, triplet in enumerate(values):
        context[key].add_data((b"%.7f" % (i / float(len(values) - 1)),) + triplet)
    return cgats


def verify_cgats(cgats, required, ignore_unknown=True):
    """Verify and return a CGATS instance or None on failure.

    Verify if a CGATS instance has a section with all required fields.
    Return the section as CGATS instance on success, None on failure.

    If ignore_unknown evaluates to True, ignore fields which are not required.
    Otherwise, the CGATS data must contain only the required fields, no more,
    no less.
    """
    cgats_1 = cgats.queryi1(required)
    if not cgats_1 or not cgats_1.parent or not cgats_1.parent.parent:
        raise CGATSKeyError(f"Missing required fields: {', '.join(required)}")
    cgats_1 = cgats_1.parent.parent
    if not cgats_1.queryv1("NUMBER_OF_SETS"):
        raise CGATSInvalidError("Missing NUMBER_OF_SETS")
    if not cgats_1.queryv1("DATA_FORMAT"):
        raise CGATSInvalidError("Missing DATA_FORMAT")
    for field in required:
        if field.encode("utf-8") not in list(cgats_1.queryv1("DATA_FORMAT").values()):
            raise CGATSKeyError(f"Missing required field: {field}")
    if not ignore_unknown:
        for field in list(cgats_1.queryv1("DATA_FORMAT").values()):
            if field not in required:
                raise CGATSError(f"Unknown field: {field}")
    modified = cgats_1.modified
    cgats_1.filename = cgats.filename
    cgats_1.modified = modified
    return cgats_1


def verify_ti1_rgb_xyz(cgats):
    """Verify and return a CGATS instance or None on failure.

    Verify if a CGATS instance has a TI1 section with all required fields
    for RGB devices. Return the TI1 section as CGATS instance on success,
    None on failure.
    """
    return verify_cgats(cgats, ("RGB_R", "RGB_B", "RGB_G", "XYZ_X", "XYZ_Y", "XYZ_Z"))