File: test-palette.py

package info (click to toggle)
gimp 3.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 210,076 kB
  • sloc: ansic: 842,287; lisp: 10,761; python: 10,318; cpp: 7,238; perl: 4,355; sh: 1,043; xml: 963; yacc: 609; lex: 348; javascript: 150; makefile: 43
file content (120 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3

import struct

GIMP_TEST_PALETTE      = "Bears"
GIMP_TEST_PALETTE_SIZE = 256
GIMP_TEST_COLOR_IDX    = 3
GIMP_TEST_COLOR_FORMAT = "R'G'B' u8"
GIMP_TEST_COLOR_R_U8   = 72
GIMP_TEST_COLOR_G_U8   = 56
GIMP_TEST_COLOR_B_U8   = 56
EPSILON                = 1e-6

pal = Gimp.Palette.get_by_name(GIMP_TEST_PALETTE)
gimp_assert('gimp_palette_get_by_name()', type(pal) == Gimp.Palette)

pal2 = Gimp.Palette.get_by_name(GIMP_TEST_PALETTE)
gimp_assert('gimp_palette_get_by_name() is unique', pal == pal2)

n_colors = pal.get_color_count()
gimp_assert('gimp_palette_get_color_count()', GIMP_TEST_PALETTE_SIZE == n_colors)

colors = pal.get_colors()
gimp_assert('gimp_palette_get_colors()',
            len(colors) == GIMP_TEST_PALETTE_SIZE and type(colors[0]) == Gegl.Color)

f = colors[GIMP_TEST_COLOR_IDX].get_format()
gimp_assert("Checking fourth palette color's format",
            f == Babl.format (GIMP_TEST_COLOR_FORMAT))

b = colors[GIMP_TEST_COLOR_IDX].get_bytes(f)
rgb = b.get_data()
gimp_assert("Checking fourth palette color's RGB components",
            int(rgb[0]) == GIMP_TEST_COLOR_R_U8 and int(rgb[1]) == GIMP_TEST_COLOR_G_U8 and int(rgb[2]) == GIMP_TEST_COLOR_B_U8)

colormap, n_colormap_colors = pal.get_colormap (f)
format_bpp = Babl.format_get_bytes_per_pixel (f);
gimp_assert("gimp_palette_get_colormap()",
            colormap is not None and n_colormap_colors == n_colors and
            n_colormap_colors * format_bpp == len(colormap))

gimp_assert("Comparing fourth palette color's RGB components from colormap",
            colormap[format_bpp * GIMP_TEST_COLOR_IDX] == GIMP_TEST_COLOR_R_U8     and
            colormap[format_bpp * GIMP_TEST_COLOR_IDX + 1] == GIMP_TEST_COLOR_G_U8 and
            colormap[format_bpp * GIMP_TEST_COLOR_IDX + 2]== GIMP_TEST_COLOR_B_U8)

f2 = Babl.format('RGBA float')
colormap, n_colormap_colors = pal.get_colormap (f2)
f2_bpp = Babl.format_get_bytes_per_pixel (f2);
gimp_assert('gimp_palette_get_colormap() in "RGBA float"',
            colormap is not None and n_colormap_colors == n_colors and
            n_colormap_colors * f2_bpp == len(colormap))


start = f2_bpp * GIMP_TEST_COLOR_IDX
colormap_r = struct.unpack('f', colormap[start:start + 4])[0]
colormap_g = struct.unpack('f', colormap[start + 4:start + 8])[0]
colormap_b = struct.unpack('f', colormap[start + 8:start + 12])[0]

rgb_bytes = colors[GIMP_TEST_COLOR_IDX].get_bytes(f2)
rgb = rgb_bytes.get_data()
palette_r = struct.unpack('f', rgb[:4])[0]
palette_g = struct.unpack('f', rgb[4:8])[0]
palette_b = struct.unpack('f', rgb[8:12])[0]
gimp_assert("Comparing fourth palette color's RGB components from colormap in float format",
            abs(colormap_r - palette_r) < EPSILON and
            abs(colormap_g - palette_g) < EPSILON and
            abs(colormap_b - palette_b) < EPSILON)

# Run the same tests through PDB:

proc   = Gimp.get_pdb().lookup_procedure('gimp-palette-get-by-name')
config = proc.create_config()
config.set_property('name', GIMP_TEST_PALETTE)
result = proc.run(config)
status = result.index(0)
gimp_assert('gimp-palette-get-by-name', status == Gimp.PDBStatusType.SUCCESS)

pal2 = result.index(1)
gimp_assert('gimp-palette-get-by-name and gimp_palette_get_by_name() get identical result', pal == pal2)

proc   = Gimp.get_pdb().lookup_procedure('gimp-palette-get-color-count')
config = proc.create_config()
config.set_property('palette', pal)
result = proc.run(config)
status = result.index(0)
gimp_assert('gimp-palette-get-color-count', status == Gimp.PDBStatusType.SUCCESS)

n_colors = result.index(1)
gimp_assert('gimp_palette_get_color_count()', GIMP_TEST_PALETTE_SIZE == n_colors)

proc   = Gimp.get_pdb().lookup_procedure('gimp-palette-get-colors')
config = proc.create_config()
config.set_property('palette', pal)
result = proc.run(config)
status = result.index(0)
gimp_assert('gimp-palette-get-colors', status == Gimp.PDBStatusType.SUCCESS)

colors = result.index(1)
# XXX This test is actually what happens right now, but not what should happen.
# See: https://gitlab.gnome.org/GNOME/gimp/-/issues/10885#note_2030308
# And: https://gitlab.gnome.org/GNOME/gobject-introspection/-/issues/492
# If some day this test fails, and in particular if it can return a list of
# GeglColor, then we should remove the test and deprecate
# gimp_value_array_get_color_array() in favor of the generic
# gimp_value_array_index().
gimp_assert('gimp-palette-get-colors', type(colors) == GObject.GBoxed)

colors = result.get_color_array(1)
gimp_assert('gimp_palette_get_colors()',
            type(colors) == list and len(colors) == GIMP_TEST_PALETTE_SIZE and type(colors[0]) == Gegl.Color)

f = colors[3].get_format()
gimp_assert ("Checking fourth palette color's format",
             f == Babl.format (GIMP_TEST_COLOR_FORMAT))

b = colors[3].get_bytes(f)
rgb = b.get_data()
gimp_assert ("Checking fourth palette color's RGB components",
             int(rgb[0]) == GIMP_TEST_COLOR_R_U8 and int(rgb[1]) == GIMP_TEST_COLOR_G_U8 and int(rgb[2]) == GIMP_TEST_COLOR_B_U8)