-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgfx2packer.pas
157 lines (147 loc) · 5.98 KB
/
gfx2packer.pas
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
type
T_PackBits_data = Record
f : file;
output_count : integer;
list_size : byte;
repetition_mode : byte;
list : array[0..129] of byte;
end;
procedure PackBits_pack_init(var data : T_PackBits_data;var f : FILE);
begin
Fillchar(data, 0, sizeof(data));
data.f := f;
end;
function PackBits_pack_add(var data : T_PackBits_data; b : byte) : integer;
begin
case data.list_size of 0:begin // First color
data.list[0] := b;
data.list_size := 1;
break;
end;
1:begin // second color
data.repetition_mode := (data.list[0] = b);
data.list[1] := b;
data.list_size := 2;
break;
end;
else
begin // next colors
if (data.list[data.list_size - 1] = b) then // repeat detected
begin
if (NOT data.repetition_mode AND data.list_size >= 127) then
begin
// diff mode with 126 bytes then 2 identical bytes
dec(data.list_size);
if (PackBits_pack_flush(data) < 0) then
begin
PackBits_pack_add:=-1;
exit;
end;
data.list[0] := b;
data.list[1] := b;
data.list_size := 2;
data.repetition_mode := 1;
end
else if ((data.repetition_mode) OR (data.list[data.list_size - 2] <> b)) then
begin
// same mode is kept
if (data.list_size = 128) then
begin
if (PackBits_pack_flush(data) < 0) then
begin
PackBits_pack_add:=-1;
exit;
end;
end;
inc(data.list_size);
data.list[data.list_size] := b;
end
else
begin
// diff mode and 3 identical bytes
data.list_size:=data.list_size - 2;
if (PackBits_pack_flush(data) < 0) then
begin
PackBits_pack_add:=-1;
exit;
end;
data.list[0] := b;
data.list[1] := b;
data.list[2] := b;
data.list_size := 3;
data.repetition_mode := 1;
end;
end
else // the color is different from the previous one
begin
if (NOT data.repetition_mode) then // keep mode
begin
if (data.list_size = 128) then
begin
if (PackBits_pack_flush(data) < 0) then
begin
PackBits_pack_add:=-1;
exit;
end;
end;
inc(data.list_size);
data.list[data.list_size] := b;
end
else // change mode
begin
if (PackBits_pack_flush(data) < 0) then
begin
PackBits_pack_add:=-1;
exit;
end;
inc(data.list_size);
data.list[data.list_size] := b;
end;
end;
end;
PackBits_pack_add:=0; // OK
end;
int PackBits_pack_flush(T_PackBits_data * data)
{
if (data->list_size > 0)
{
if (data->list_size > 128)
{
GFX2_Log(GFX2_ERROR, "PackBits_pack_flush() list_size=%d !\n", data->list_size);
}
if (data->repetition_mode)
{
if (data->f != NULL)
{
if (!Write_byte(data->f, 257 - data->list_size) ||
!Write_byte(data->f, data->list[0]))
return -1;
}
data->output_count += 2;
}
else
{
if (data->f != NULL)
{
if (!Write_byte(data->f, data->list_size - 1) ||
!Write_bytes(data->f, data->list, data->list_size))
return -1;
}
data->output_count += 1 + data->list_size;
}
data->list_size = 0;
data->repetition_mode = 0;
}
return data->output_count;
}
int PackBits_pack_buffer(FILE * f, const byte * buffer, size_t size)
{
T_PackBits_data pb_data;
PackBits_pack_init(&pb_data, f);
while (size-- > 0)
{
if (PackBits_pack_add(&pb_data, *buffer++))
return -1;
}
return PackBits_pack_flush(&pb_data);
}