-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gid-color_tables.adb
68 lines (64 loc) · 2.23 KB
/
gid-color_tables.adb
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
with GID.Buffering;
package body GID.Color_tables is
use Interfaces;
procedure Convert (c, d : in U8; rgb : out RGB_Color_8_Bit) is
begin
rgb.red := (d and 127) / 4;
rgb.green := (d and 3) * 8 + c / 32;
rgb.blue := c and 31;
--
rgb.red := U8 ((U16 (rgb.red) * 255) / 31);
rgb.green := U8 ((U16 (rgb.green) * 255) / 31);
rgb.blue := U8 ((U16 (rgb.blue) * 255) / 31);
end Convert;
procedure Load_palette (image : in out Image_Descriptor) is
c, d : U8;
use GID.Buffering;
begin
if image.palette = null then
return;
end if;
declare
palette : Color_Table renames image.palette.all;
begin
for i in palette'Range loop
case image.format is
when BMP =>
-- order is BGRx
U8'Read (image.stream, palette (i).blue);
U8'Read (image.stream, palette (i).green);
U8'Read (image.stream, palette (i).red);
U8'Read (image.stream, c);
-- x discarded
when GIF | PNG =>
-- buffered; order is RGB
Get_Byte (image.buffer, palette (i).red);
Get_Byte (image.buffer, palette (i).green);
Get_Byte (image.buffer, palette (i).blue);
when TGA =>
case image.subformat_id is -- = palette's bit depth
when 8 => -- Grey
U8'Read (image.stream, c);
palette (i).red := c;
palette (i).green := c;
palette (i).blue := c;
when 15 | 16 => -- RGB, 5 bit per channel
U8'Read (image.stream, c);
U8'Read (image.stream, d);
Convert (c, d, palette (i));
when 24 | 32 => -- RGB | RGBA, 8 bit per channel
U8'Read (image.stream, palette (i).blue);
U8'Read (image.stream, palette (i).green);
U8'Read (image.stream, palette (i).red);
when others =>
null;
end case;
when others =>
raise unsupported_image_subformat with
"Palette loading not implemented for " &
Image_Format_Type'Image (image.format);
end case;
end loop;
end;
end Load_palette;
end GID.Color_tables;