Huw Davies | 6e5ab45 | 2003-12-08 21:40:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * PostScript filter functions |
| 3 | * |
| 4 | * Copyright 2003 Huw D M Davies |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <ctype.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "psdrv.h" |
| 25 | #include "wine/debug.h" |
| 26 | |
| 27 | WINE_DEFAULT_DEBUG_CHANNEL(psdrv); |
| 28 | |
| 29 | DWORD RLE_encode(BYTE *in_buf, DWORD len, BYTE *out_buf) |
| 30 | { |
| 31 | BYTE *next_in = in_buf, *next_out = out_buf; |
| 32 | DWORD rl; |
| 33 | |
| 34 | while(next_in < in_buf + len) { |
| 35 | if(next_in + 1 < in_buf + len) { |
| 36 | if(*next_in == *(next_in + 1)) { |
| 37 | next_in += 2; |
| 38 | rl = 2; |
| 39 | while(next_in < in_buf + len && *next_in == *(next_in - 1) && rl < 128) { |
| 40 | next_in++; |
| 41 | rl++; |
| 42 | } |
| 43 | *next_out++ = 257 - rl; |
| 44 | *next_out++ = *(next_in - 1); |
| 45 | } else { |
| 46 | rl = 0; |
| 47 | next_out++; |
| 48 | while(next_in < in_buf + len && rl < 128) { |
| 49 | if(next_in + 2 < in_buf + len && *next_in == *(next_in + 1)) { |
| 50 | if(rl == 127 || *next_in == *(next_in + 2)) |
| 51 | break; |
| 52 | } |
| 53 | *next_out++ = *next_in++; |
| 54 | rl++; |
| 55 | } |
| 56 | *(next_out - rl - 1) = rl - 1; |
| 57 | } |
| 58 | } else { /* special case: begin of run on last byte */ |
| 59 | *next_out++ = 0; |
| 60 | *next_out++ = *next_in; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | *next_out++ = 128; |
| 65 | return next_out - out_buf; |
| 66 | } |
| 67 | |
| 68 | DWORD ASCII85_encode(BYTE *in_buf, DWORD len, BYTE *out_buf) |
| 69 | { |
| 70 | DWORD number; |
| 71 | BYTE *next_in = in_buf, *next_out = out_buf; |
| 72 | int i; |
| 73 | |
| 74 | while(next_in + 3 < in_buf + len) { |
| 75 | number = (next_in[0] << 24) | |
| 76 | (next_in[1] << 16) | |
| 77 | (next_in[2] << 8) | |
| 78 | (next_in[3] ); |
| 79 | next_in += 4; |
| 80 | if(number == 0) |
| 81 | *next_out++ = 'z'; |
| 82 | else { |
| 83 | for(i = 4; i >= 0; i--) { |
| 84 | next_out[i] = number % 85 + '!'; |
| 85 | number /= 85; |
| 86 | } |
| 87 | next_out += 5; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if(next_in != in_buf + len) { |
| 92 | number = (*next_in++ << 24) & 0xff000000; |
| 93 | if(next_in < in_buf + len) |
| 94 | number |= ((*next_in++ << 16) & 0x00ff0000); |
| 95 | if(next_in < in_buf + len) |
| 96 | number |= ((*next_in++ << 8) & 0x0000ff00); |
| 97 | |
| 98 | for(i = len % 4 + 1; i < 5; i++) |
| 99 | number /= 85; |
| 100 | |
| 101 | for(i = len % 4; i >= 0; i--) { |
| 102 | next_out[i] = number % 85 + '!'; |
| 103 | number /= 85; |
| 104 | } |
| 105 | next_out += len % 4 + 1; |
| 106 | } |
| 107 | |
| 108 | return next_out - out_buf; |
| 109 | } |