Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 205 lines (158 sloc) 5.264 kb
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
1 #!/usr/bin/env python
2
3 # Copyright 2009 Steve Conklin
4 # steve at conklinhouse dot com
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program 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
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 import sys
21 import brother
22 import Image
23 import array
24
25 TheImage = None
26
27 ##################
28
29 def roundeven(val):
30 return (val+(val%2))
31
32 def roundeight(val):
33 if val % 8:
34 return val + (8-(val%8))
35 else:
36 return val
37
38 def roundfour(val):
39 if val % 4:
40 return val + (4-(val%4))
41 else:
42 return val
43
44 def nibblesPerRow(stitches):
45 # there are four stitches per nibble
46 # each row is nibble aligned
47 return(roundfour(stitches)/4)
48
49 def bytesPerPattern(stitches, rows):
50 nibbs = rows * nibblesPerRow(stitches)
51 bytes = roundeven(nibbs)/2
52 return bytes
53
54 def bytesForMemo(rows):
55 bytes = roundeven(rows)/2
56 return bytes
57
58 ##############
59
60
61 version = '1.0'
62
87dd8178 » ladyada
2010-10-26 does some sanity checking on BMP vs pattern entry size
63 if len(sys.argv) < 5:
64 print 'Usage: %s oldbrotherfile pattern# image.bmp newbrotherfile' % sys.argv[0]
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
65 sys.exit()
66
67
68 bf = brother.brotherFile(sys.argv[1])
87dd8178 » ladyada
2010-10-26 does some sanity checking on BMP vs pattern entry size
69 pattnum = sys.argv[2]
70 imgfile = sys.argv[3]
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
71
72
73 pats = bf.getPatterns()
74
75 # ok got a bank, now lets figure out how big this thing we want to insert is
76 TheImage = Image.open(imgfile)
77 TheImage.load()
78
79 im_size = TheImage.size
80 width = im_size[0]
81 print "width:",width
82 height = im_size[1]
83 print "height:", height
84
25601075 » ladyada
2010-11-01 less debugging output
85
86
87 # find the program entry
88 thePattern = None
89
90 for pat in pats:
91 if (int(pat["number"]) == int(pattnum)):
92 #print "found it!"
93 thePattern = pat
94 if (thePattern == None):
95 print "Pattern #",pattnum,"not found!"
96 exit(0)
97
5d34c060 » ladyada
2010-11-01 fixed non-square inserts
98 if (height != thePattern["rows"] or width != thePattern["stitches"]):
99 print "Pattern is the wrong size, the BMP is ",height,"x",width,"and the pattern is ",thePattern["rows"], "x", thePattern["stitches"]
25601075 » ladyada
2010-11-01 less debugging output
100 exit(0)
101
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
102 # debugging stuff here
103 x = 0
104 y = 0
105
106 x = width - 1
107 while x > 0:
108 value = TheImage.getpixel((x,y))
109 if value:
110 sys.stdout.write('* ')
111 else:
112 sys.stdout.write(' ')
113 #sys.stdout.write(str(value))
114 x = x-1
115 if x == 0: #did we hit the end of the line?
116 y = y+1
117 x = width - 1
118 print " "
119 if y == height:
120 break
121 # debugging stuff done
122
123 # now to make the actual, yknow memo+pattern data
124
125 # the memo seems to be always blank. i have no idea really
126 memoentry = []
127 for i in range(bytesForMemo(height)):
128 memoentry.append(0x0)
129
130 # now for actual real live pattern data!
131 pattmemnibs = []
132 for r in range(height):
133 row = [] # we'll chunk in bits and then put em into nibbles
134 for s in range(width):
135 value = TheImage.getpixel((width-s-1,height-r-1))
136 if (value != 0):
137 row.append(1)
138 else:
139 row.append(0)
140 #print row
141 # turn it into nibz
142 for s in range(roundfour(width) / 4):
143 n = 0
144 for nibs in range(4):
145 #print "row size = ", len(row), "index = ",s*4+nibs
146
147 if (len(row) == (s*4+nibs)):
148 break # padding!
149
150 if (row[s*4 + nibs]):
151 n |= 1 << nibs
152 pattmemnibs.append(n)
25601075 » ladyada
2010-11-01 less debugging output
153 #print hex(n),
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
154
155
156 if (len(pattmemnibs) % 2):
157 # odd nibbles, buffer to a byte
158 pattmemnibs.append(0x0)
159
25601075 » ladyada
2010-11-01 less debugging output
160 #print len(pattmemnibs), "nibbles of data"
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
161
162 # turn into bytes
163 pattmem = []
164 for i in range (len(pattmemnibs) / 2):
165 pattmem.append( pattmemnibs[i*2] | (pattmemnibs[i*2 + 1] << 4))
166
25601075 » ladyada
2010-11-01 less debugging output
167 #print map(hex, pattmem)
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
168 # whew.
169
170
171 # now to insert this data into the file
172
173 # now we have to figure out the -end- of the last pattern is
174 endaddr = 0x6df
175
81869e49 » ladyada
2010-10-26 Ready for testing!
176 beginaddr = thePattern["pattend"]
177 endaddr = beginaddr + bytesForMemo(height) + len(pattmem)
269de61d » Steve Conklin
2012-02-27 minor output tweaks, no functional changes
178 print "beginning will be at ", hex(beginaddr), "end at", hex(endaddr)
179
180 # Note - It's note certain that in all cases this collision test is needed. What's happening
181 # when you write below this address (as the pattern grows downward in memory) in that you begin
182 # to overwrite the pattern index data that starts at low memory. Since you overwrite the info
183 # for highest memory numbers first, you may be able to get away with it as long as you don't
184 # attempt to use higher memories.
185 # Steve
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
186
187 if beginaddr <= 0x2B8:
269de61d » Steve Conklin
2012-02-27 minor output tweaks, no functional changes
188 print "sorry, this will collide with the pattern entry data since %s is <= 0x2B8!" % hex(beginaddr)
189 #exit
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
190
191 # write the memo and pattern entry from the -end- to the -beginning- (up!)
192 for i in range(len(memoentry)):
193 bf.setIndexedByte(endaddr, 0)
194 endaddr -= 1
195
196 for i in range(len(pattmem)):
197 bf.setIndexedByte(endaddr, pattmem[i])
198 endaddr -= 1
199
200 # push the data to a file
87dd8178 » ladyada
2010-10-26 does some sanity checking on BMP vs pattern entry size
201 outfile = open(sys.argv[4], 'wb')
bd4288e6 » ladyada
2010-10-26 gonna rewrite this for inserting into a 'blank' pattern
202
203 d = bf.getFullData()
204 outfile.write(d)
205 outfile.close()
Something went wrong with that request. Please try again.