1bf215546Sopenharmony_ciCOPYRIGHT = """\
2bf215546Sopenharmony_ci/*
3bf215546Sopenharmony_ci * Copyright 2017 Google
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
7bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
8bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
9bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
10bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
11bf215546Sopenharmony_ci * the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
14bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
15bf215546Sopenharmony_ci * of the Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci"""
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci# stdlib
28bf215546Sopenharmony_ciimport argparse
29bf215546Sopenharmony_cifrom sys import stdout
30bf215546Sopenharmony_cifrom mako.template import Template
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci# local
33bf215546Sopenharmony_ciimport format_parser
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cidef parse_args():
36bf215546Sopenharmony_ci    p = argparse.ArgumentParser()
37bf215546Sopenharmony_ci    p.add_argument("csv")
38bf215546Sopenharmony_ci    p.add_argument("out")
39bf215546Sopenharmony_ci    return p.parse_args()
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cidef get_unorm_to_srgb_map(formats):
42bf215546Sopenharmony_ci    names = set(fmt.name for fmt in formats)
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci    for fmt in formats:
45bf215546Sopenharmony_ci        if fmt.colorspace != 'srgb':
46bf215546Sopenharmony_ci            continue;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci        replacements = [
49bf215546Sopenharmony_ci            ('SRGB', 'RGB'),
50bf215546Sopenharmony_ci            ('SRGB', 'UNORM'),
51bf215546Sopenharmony_ci            ('SRGB8_ALPHA8', 'RGBA'),
52bf215546Sopenharmony_ci            ('SRGB8_ALPHA8', 'RGBA8'),
53bf215546Sopenharmony_ci            ('SRGB_ALPHA_UNORM', 'RGBA_UNORM'),
54bf215546Sopenharmony_ci        ]
55bf215546Sopenharmony_ci        found_unorm_name = False
56bf215546Sopenharmony_ci        for rep in replacements:
57bf215546Sopenharmony_ci            if fmt.name.find(rep[0]) == -1:
58bf215546Sopenharmony_ci                continue
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci            unorm_name = fmt.name.replace(rep[0], rep[1])
61bf215546Sopenharmony_ci            if unorm_name in names:
62bf215546Sopenharmony_ci                yield unorm_name, fmt.name
63bf215546Sopenharmony_ci                found_unorm_name = True
64bf215546Sopenharmony_ci                break
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci        # Every sRGB format MUST have a UNORM equivalent
67bf215546Sopenharmony_ci        assert found_unorm_name
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cidef get_intensity_to_red_map(formats):
70bf215546Sopenharmony_ci    names = set(fmt.name for fmt in formats)
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci    for fmt in formats:
73bf215546Sopenharmony_ci        if str(fmt.swizzle) != 'xxxx':
74bf215546Sopenharmony_ci            continue
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci        i_name = fmt.name
77bf215546Sopenharmony_ci        r_name = i_name.replace("_I_", "_R_")
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci        assert r_name in names
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci        yield i_name, r_name
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ciTEMPLATE = Template(COPYRIGHT + """
84bf215546Sopenharmony_ci#include "formats.h"
85bf215546Sopenharmony_ci#include "util/macros.h"
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci/**
88bf215546Sopenharmony_ci * For an sRGB format, return the corresponding linear color space format.
89bf215546Sopenharmony_ci * For non-sRGB formats, return the format as-is.
90bf215546Sopenharmony_ci */
91bf215546Sopenharmony_cimesa_format
92bf215546Sopenharmony_ci_mesa_get_srgb_format_linear(mesa_format format)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   switch (format) {
95bf215546Sopenharmony_ci%for unorm, srgb in unorm_to_srgb_map:
96bf215546Sopenharmony_ci   case ${srgb}:
97bf215546Sopenharmony_ci      return ${unorm};
98bf215546Sopenharmony_ci%endfor
99bf215546Sopenharmony_ci   default:
100bf215546Sopenharmony_ci      return format;
101bf215546Sopenharmony_ci   }
102bf215546Sopenharmony_ci}
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci/**
105bf215546Sopenharmony_ci * For an intensity format, return the corresponding red format.  For other
106bf215546Sopenharmony_ci * formats, return the format as-is.
107bf215546Sopenharmony_ci */
108bf215546Sopenharmony_cimesa_format
109bf215546Sopenharmony_ci_mesa_get_intensity_format_red(mesa_format format)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   switch (format) {
112bf215546Sopenharmony_ci%for i, r in intensity_to_red_map:
113bf215546Sopenharmony_ci   case ${i}:
114bf215546Sopenharmony_ci      return ${r};
115bf215546Sopenharmony_ci%endfor
116bf215546Sopenharmony_ci   default:
117bf215546Sopenharmony_ci      return format;
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci}
120bf215546Sopenharmony_ci""");
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_cidef main():
123bf215546Sopenharmony_ci    pargs = parse_args()
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci    formats = list(format_parser.parse(pargs.csv))
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci    template_env = {
128bf215546Sopenharmony_ci        'unorm_to_srgb_map': list(get_unorm_to_srgb_map(formats)),
129bf215546Sopenharmony_ci        'intensity_to_red_map': list(get_intensity_to_red_map(formats)),
130bf215546Sopenharmony_ci    }
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci    with open(pargs.out, 'w') as f:
133bf215546Sopenharmony_ci        f.write(TEMPLATE.render(**template_env))
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ciif __name__ == "__main__":
136bf215546Sopenharmony_ci    main()
137