rastertosag-gdi-python3
Didier Raboud
6 years ago
0 | #!/usr/bin/python -u | |
0 | #!/usr/bin/python3 -u | |
1 | 1 | #coding=utf8 |
2 | 2 | |
3 | 3 | # CUPS raster filter for Ricoh Aficio SP1000s |
25 | 25 | if len(sys.argv)>6: |
26 | 26 | filename=sys.argv[6] |
27 | 27 | if filename=='.' or filename=='-': |
28 | input=sys.stdin | |
28 | input=sys.stdin.buffer | |
29 | 29 | else: |
30 | input = open(sys.argv[6],'r') | |
31 | output = sys.stdout | |
30 | input = open(sys.argv[6],'rb') | |
31 | output = sys.stdout.buffer | |
32 | 32 | |
33 | 33 | def print_stderr(s,newline=True): |
34 | 34 | sys.stderr.write(str(s)+('\n' if newline else '')) |
38 | 38 | cups = input.read() # Read all bytes from stdin |
39 | 39 | |
40 | 40 | sync_word=cups[0:4] # length of sync-word is 4 bytes: "3SaR" |
41 | version=int(cups[0]) | |
41 | version=cups[0]-48 | |
42 | 42 | cups=cups[4:] |
43 | 43 | |
44 | 44 | dtype={ |
54 | 54 | } |
55 | 55 | |
56 | 56 | def make_unpack_format(format,begin,end,split_count=1): |
57 | return '<'+(str((end-begin+1)/dtype[format]['size']/split_count)+dtype[format]['b'])*split_count | |
57 | return '<'+(str((end-begin+1)//dtype[format]['size']//split_count)+dtype[format]['b'])*split_count | |
58 | 58 | |
59 | 59 | hdr={ |
60 | 60 | 1: {}, # version 1 header |
158 | 158 | line=unpack('<'+str(numbytes)+'B',page_data[y*numbytes:(y+1)*numbytes]) |
159 | 159 | ret=[] |
160 | 160 | extend=ret.extend |
161 | for x in range(0,w/8): | |
161 | for x in range(0,w//8): | |
162 | 162 | extend(bytes[line[x]]) |
163 | 163 | if w%8: |
164 | extend(bytes[line[w/8]][:w%8]) | |
164 | extend(bytes[line[w//8]][:w%8]) | |
165 | 165 | return ret |
166 | 166 | |
167 | 167 | FORMAT_WIDTH=0 |
184 | 184 | def begin_document(): |
185 | 185 | output.write(pack( |
186 | 186 | '>76sbbHHI', |
187 | ') SAG-GDI RL;0;0;Comment Copyright Sagem Communication 2005. Version 1.0.0.0', | |
187 | b') SAG-GDI RL;0;0;Comment Copyright Sagem Communication 2005. Version 1.0.0.0', | |
188 | 188 | 0x0D,0x0A, |
189 | 189 | 0x1000,0x0200, |
190 | 190 | 0 |
216 | 216 | current_line_length=0 |
217 | 217 | def begin_block(): |
218 | 218 | global current_block_data |
219 | current_block_data='' | |
219 | current_block_data=b'' | |
220 | 220 | |
221 | 221 | def get_block_size(): |
222 | 222 | return len(current_block_data) |
231 | 231 | color = 1 if col else 0 |
232 | 232 | color_bit = color << 6 |
233 | 233 | first_byte = length%64 |
234 | second_byte = length/64 | |
234 | second_byte = length//64 | |
235 | 235 | two_bytes_bit = 0b10000000 if second_byte else 0b00000000 |
236 | 236 | |
237 | 237 | px_data=pack('>B', two_bytes_bit | color_bit | first_byte) |
256 | 256 | w,h=min(hdr[1]['cupsWidth'],formats[format][FORMAT_WIDTH]),min(hdr[1]['cupsHeight'],formats[format][FORMAT_HEIGHT]) |
257 | 257 | t=time() |
258 | 258 | for y in range(h): |
259 | if y%(h/15)==0: | |
259 | if y%(h//15)==0: | |
260 | 260 | print_stderr('%d%% '%(int(float(y)/h*100),),False) |
261 | 261 | |
262 | 262 | yline=get_cups_line(y) |