2 * PostScript filter functions
4 * Copyright 2003 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 DWORD
RLE_encode(BYTE
*in_buf
, DWORD len
, BYTE
*out_buf
)
28 BYTE
*next_in
= in_buf
, *next_out
= out_buf
;
31 while(next_in
< in_buf
+ len
) {
32 if(next_in
+ 1 < in_buf
+ len
) {
33 if(*next_in
== *(next_in
+ 1)) {
36 while(next_in
< in_buf
+ len
&& *next_in
== *(next_in
- 1) && rl
< 128) {
40 *next_out
++ = 257 - rl
;
41 *next_out
++ = *(next_in
- 1);
45 while(next_in
< in_buf
+ len
&& rl
< 128) {
46 if(next_in
+ 2 < in_buf
+ len
&& *next_in
== *(next_in
+ 1)) {
47 if(rl
== 127 || *next_in
== *(next_in
+ 2))
50 *next_out
++ = *next_in
++;
53 *(next_out
- rl
- 1) = rl
- 1;
55 } else { /* special case: begin of run on last byte */
57 *next_out
++ = *next_in
;
62 return next_out
- out_buf
;
65 DWORD
ASCII85_encode(BYTE
*in_buf
, DWORD len
, BYTE
*out_buf
)
68 BYTE
*next_in
= in_buf
, *next_out
= out_buf
;
71 while(next_in
+ 3 < in_buf
+ len
) {
72 number
= (next_in
[0] << 24) |
80 for(i
= 4; i
>= 0; i
--) {
81 next_out
[i
] = number
% 85 + '!';
88 if(next_in
!= in_buf
+ len
) {
89 number
= (*next_in
++ << 24) & 0xff000000;
90 if(next_in
< in_buf
+ len
)
91 number
|= ((*next_in
++ << 16) & 0x00ff0000);
92 if(next_in
< in_buf
+ len
)
93 number
|= ((*next_in
++ << 8) & 0x0000ff00);
95 for(i
= len
% 4 + 1; i
< 5; i
++)
98 for(i
= len
% 4; i
>= 0; i
--) {
99 next_out
[i
] = number
% 85 + '!';
102 next_out
+= len
% 4 + 1;
105 return next_out
- out_buf
;