def dumppattern(self, argv): if len(argv) < 1: raise ArgumentsException() result = Result() if len(argv) == 2: patt = int(argv[1]) else: patt = 0 bf = brother.brotherFile(argv[0]) if patt == 0: result.patterns = bf.getPatterns() if DEBUG: print "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" print "Data file" print "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" # first dump the 99 'pattern id' blocks for i in range(99): print "program entry", i # each block is 7 bytes bytenum = i * 7 pattused = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex(pattused), if (pattused == 1): print "\t(used)" else: print "\t(unused)" #print "\t-skipped-" #continue bytenum += 1 unk1 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex(unk1), "\t(unknown)" bytenum += 1 rows100 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex( rows100), "\t(rows = ", (rows100 >> 4) * 100, " + ", ( rows100 & 0xF) * 10 bytenum += 1 rows1 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex(rows1), "\t\t+ ", ( rows1 >> 4), " stiches = ", (rows1 & 0xF) * 100, "+" bytenum += 1 stitches10 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex( stitches10), "\t\t+ ", (stitches10 >> 4) * 10, " +", ( stitches10 & 0xF), ")" bytenum += 1 prog100 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex( prog100), "\t(unknown , prog# = ", (prog100 & 0xF) * 100, "+" bytenum += 1 prog10 = bf.getIndexedByte(bytenum) print "\t", hex(bytenum), ": ", hex(prog10), "\t\t + ", ( prog10 >> 4) * 10, "+", (prog10 & 0xF), ")" bytenum += 1 print "============================================" print "Program memory grows -up-" # now we're onto data data # dump the first program pointer = 0x6DF # this is the 'bottom' of the memory for i in range(99): # of course, not all patterns will get dumped pattused = bf.getIndexedByte(i * 7) if (pattused != 1): # :( break # otherwise its a valid pattern print "pattern bank #", i # calc pattern size rows100 = bf.getIndexedByte(i * 7 + 2) rows1 = bf.getIndexedByte(i * 7 + 3) stitches10 = bf.getIndexedByte(i * 7 + 4) rows = (rows100 >> 4) * 100 + (rows100 & 0xF) * 10 + ( rows1 >> 4) stitches = (rows1 & 0xF) * 100 + (stitches10 >> 4) * 10 + ( stitches10 & 0xF) print "rows = ", rows, "stitches = ", stitches # print "total nibs per row = ", nibblesPerRow(stitches) # dump the memo data print "memo length =", bytesForMemo(rows) for i in range(bytesForMemo(rows)): b = pointer - i print "\t", hex(b), ": ", hex(bf.getIndexedByte(b)) pointer -= bytesForMemo(rows) print "pattern length = ", bytesPerPattern(stitches, rows) for i in range(bytesPerPattern(stitches, rows)): b = pointer - i print "\t", hex(b), ": ", hex(bf.getIndexedByte(b)), for j in range(8): if (bf.getIndexedByte(b) & (1 << j)): print "*", else: print " ", print "" # print it out in nibbles per row? for row in range(rows): for nibs in range(nibblesPerRow(stitches)): n = bf.getIndexedNibble( pointer, nibblesPerRow(stitches) * row + nibs) print hex(n), for j in range(8): if (n & (1 << j)): print "*", else: print " ", print "" pointer -= bytesPerPattern(stitches, rows) #for i in range (0x06DF, 99*7, -1): # print "\t",hex(i),": ",hex(bf.getIndexedByte(i)) else: self.printInfoCallback('Searching for pattern number %d' % patt) pats = bf.getPatterns(patt) if len(pats) == 0: raise PatternNotFoundException(patt) else: stitches = pats[0]["stitches"] rows = pats[0]["rows"] self.printInfoCallback('%3d Stitches, %3d Rows' % (stitches, rows)) result.pattern = bf.getPattern(patt) return result
def insertPattern(self, oldbrotherfile, pattnum, imgfile, newbrotherfile): bf = brother.brotherFile(oldbrotherfile) pats = bf.getPatterns() # ok got a bank, now lets figure out how big this thing we want to insert is TheImage = Image.open(imgfile) TheImage.load() im_size = TheImage.size width = im_size[0] self.printInfoCallback("width:" + str(width)) height = im_size[1] self.printInfoCallback("height:" + str(height)) # find the program entry thePattern = None for pat in pats: if (int(pat["number"]) == int(pattnum)): #print "found it!" thePattern = pat if (thePattern == None): raise PatternNotFoundException(pattnum) if (height != thePattern["rows"] or width != thePattern["stitches"]): raise InserterException("Pattern is the wrong size, the BMP is ", height, "x", width, "and the pattern is ", thePattern["rows"], "x", thePattern["stitches"]) # debugging stuff here x = 0 y = 0 x = width - 1 for y in xrange(height): for x in xrange(width): value = TheImage.getpixel((x, y)) if value: self.printPattern('* ') else: self.printPattern(' ') print " " # debugging stuff done # now to make the actual, yknow memo+pattern data # the memo seems to be always blank. i have no idea really memoentry = [] for i in range(bytesForMemo(height)): memoentry.append(0x0) # now for actual real live pattern data! pattmemnibs = [] for r in range(height): row = [] # we'll chunk in bits and then put em into nibbles for s in range(width): x = s if methodWithPointers else width - s - 1 value = TheImage.getpixel((x, height - r - 1)) isBlack = (value == 0) if methodWithPointers else (value != 0) if (isBlack): row.append(1) else: row.append(0) #print row # turn it into nibz for s in range(roundfour(width) / 4): n = 0 for nibs in range(4): #print "row size = ", len(row), "index = ",s*4+nibs if (len(row) == (s * 4 + nibs)): break # padding! if (row[s * 4 + nibs]): n |= 1 << nibs pattmemnibs.append(n) #print hex(n), if (len(pattmemnibs) % 2): # odd nibbles, buffer to a byte pattmemnibs.append(0x0) #print len(pattmemnibs), "nibbles of data" # turn into bytes pattmem = [] for i in range(len(pattmemnibs) / 2): pattmem.append(pattmemnibs[i * 2] | (pattmemnibs[i * 2 + 1] << 4)) #print map(hex, pattmem) # whew. # now to insert this data into the file # now we have to figure out the -end- of the last pattern is endaddr = 0x6df beginaddr = thePattern["pattend"] endaddr = beginaddr + bytesForMemo(height) + len(pattmem) self.printInfoCallback("beginning will be at " + str(hex(beginaddr)) + ", end at " + str(hex(endaddr))) # Note - It's note certain that in all cases this collision test is needed. What's happening # when you write below this address (as the pattern grows downward in memory) in that you begin # to overwrite the pattern index data that starts at low memory. Since you overwrite the info # for highest memory numbers first, you may be able to get away with it as long as you don't # attempt to use higher memories. # Steve if beginaddr <= 0x2B8: self.printErrorCallback( "Sorry, this will collide with the pattern entry data since %s is <= 0x2B8!" % hex(beginaddr)) #exit # write the memo and pattern entry from the -end- to the -beginning- (up!) for i in range(len(memoentry)): bf.setIndexedByte(endaddr, 0) endaddr -= 1 for i in range(len(pattmem)): bf.setIndexedByte(endaddr, pattmem[i]) endaddr -= 1 # push the data to a file outfile = open(newbrotherfile, 'wb') d = bf.getFullData() outfile.write(d) outfile.close()
def dumppattern(self,argv): if len(argv) < 1: raise ArgumentsException() result = Result() if len(argv) == 2: patt = int(argv[1]) else: patt = 0 bf = brother.brotherFile(argv[0]) if patt == 0: result.patterns = bf.getPatterns() if DEBUG: print "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" print "Data file" print "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+" # first dump the 99 'pattern id' blocks for i in range(99): print "program entry",i # each block is 7 bytes bytenum = i*7 pattused = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(pattused), if (pattused == 1): print "\t(used)" else: print "\t(unused)" #print "\t-skipped-" #continue bytenum += 1 unk1 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(unk1),"\t(unknown)" bytenum += 1 rows100 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(rows100),"\t(rows = ", (rows100 >> 4)*100, " + ", (rows100 & 0xF)*10 bytenum += 1 rows1 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(rows1),"\t\t+ ", (rows1 >> 4), " stiches = ", (rows1 & 0xF)*100,"+" bytenum += 1 stitches10 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(stitches10),"\t\t+ ", (stitches10 >> 4)*10, " +", (stitches10 & 0xF),")" bytenum += 1 prog100 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(prog100),"\t(unknown , prog# = ", (prog100&0xF) * 100,"+" bytenum += 1 prog10 = bf.getIndexedByte(bytenum) print "\t",hex(bytenum),": ",hex(prog10),"\t\t + ", (prog10>>4) * 10,"+",(prog10&0xF),")" bytenum += 1 print "============================================" print "Program memory grows -up-" # now we're onto data data # dump the first program pointer = 0x6DF # this is the 'bottom' of the memory for i in range(99): # of course, not all patterns will get dumped pattused = bf.getIndexedByte(i*7) if (pattused != 1): # :( break # otherwise its a valid pattern print "pattern bank #", i # calc pattern size rows100 = bf.getIndexedByte(i*7 + 2) rows1 = bf.getIndexedByte(i*7 + 3) stitches10 = bf.getIndexedByte(i*7 + 4) rows = (rows100 >> 4)*100 + (rows100 & 0xF)*10 + (rows1 >> 4); stitches = (rows1 & 0xF)*100 + (stitches10 >> 4)*10 + (stitches10 & 0xF) print "rows = ", rows, "stitches = ", stitches # print "total nibs per row = ", nibblesPerRow(stitches) # dump the memo data print "memo length =",bytesForMemo(rows) for i in range (bytesForMemo(rows)): b = pointer - i print "\t",hex(b),": ",hex(bf.getIndexedByte(b)) pointer -= bytesForMemo(rows) print "pattern length = ", bytesPerPattern(stitches, rows) for i in range (bytesPerPattern(stitches, rows)): b = pointer - i print "\t",hex(b),": ",hex(bf.getIndexedByte(b)), for j in range(8): if (bf.getIndexedByte(b) & (1<<j)): print "*", else: print " ", print "" # print it out in nibbles per row? for row in range(rows): for nibs in range(nibblesPerRow(stitches)): n = bf.getIndexedNibble(pointer, nibblesPerRow(stitches)*row + nibs) print hex(n), for j in range(8): if (n & (1<<j)): print "*", else: print " ", print "" pointer -= bytesPerPattern(stitches, rows) #for i in range (0x06DF, 99*7, -1): # print "\t",hex(i),": ",hex(bf.getIndexedByte(i)) else: self.printInfoCallback( 'Searching for pattern number %d' % patt) pats = bf.getPatterns(patt) if len(pats) == 0: raise PatternNotFoundException(patt) else: stitches = pats[0]["stitches"] rows = pats[0]["rows"] self.printInfoCallback( '%3d Stitches, %3d Rows' % (stitches, rows)) result.pattern = bf.getPattern(patt) return result
def insertPattern(self, oldbrotherfile, pattnum, imgfile, newbrotherfile): bf = brother.brotherFile(oldbrotherfile) pats = bf.getPatterns() # ok got a bank, now lets figure out how big this thing we want to insert is TheImage = Image.open(imgfile) TheImage.load() im_size = TheImage.size width = im_size[0] self.printInfoCallback( "width:" + str(width)) height = im_size[1] self.printInfoCallback( "height:" + str(height)) # find the program entry thePattern = None for pat in pats: if (int(pat["number"]) == int(pattnum)): #print "found it!" thePattern = pat if (thePattern == None): raise PatternNotFoundException(pattnum) if (height != thePattern["rows"] or width != thePattern["stitches"]): raise InserterException("Pattern is the wrong size, the BMP is ",height,"x",width,"and the pattern is ",thePattern["rows"], "x", thePattern["stitches"]) # debugging stuff here x = 0 y = 0 x = width - 1 for y in xrange(height): for x in xrange(width): value = TheImage.getpixel((x,y)) if value: self.printPattern('* ') else: self.printPattern(' ') print " " # debugging stuff done # now to make the actual, yknow memo+pattern data # the memo seems to be always blank. i have no idea really memoentry = [] for i in range(bytesForMemo(height)): memoentry.append(0x0) # now for actual real live pattern data! pattmemnibs = [] for r in range(height): row = [] # we'll chunk in bits and then put em into nibbles for s in range(width): x = s if methodWithPointers else width-s-1 value = TheImage.getpixel((x,height-r-1)) isBlack = (value == 0) if methodWithPointers else (value != 0) if (isBlack): row.append(1) else: row.append(0) #print row # turn it into nibz for s in range(roundfour(width) / 4): n = 0 for nibs in range(4): #print "row size = ", len(row), "index = ",s*4+nibs if (len(row) == (s*4+nibs)): break # padding! if (row[s*4 + nibs]): n |= 1 << nibs pattmemnibs.append(n) #print hex(n), if (len(pattmemnibs) % 2): # odd nibbles, buffer to a byte pattmemnibs.append(0x0) #print len(pattmemnibs), "nibbles of data" # turn into bytes pattmem = [] for i in range (len(pattmemnibs) / 2): pattmem.append( pattmemnibs[i*2] | (pattmemnibs[i*2 + 1] << 4)) #print map(hex, pattmem) # whew. # now to insert this data into the file # now we have to figure out the -end- of the last pattern is endaddr = 0x6df beginaddr = thePattern["pattend"] endaddr = beginaddr + bytesForMemo(height) + len(pattmem) self.printInfoCallback("beginning will be at " + str(hex(beginaddr)) + ", end at " + str(hex(endaddr))) # Note - It's note certain that in all cases this collision test is needed. What's happening # when you write below this address (as the pattern grows downward in memory) in that you begin # to overwrite the pattern index data that starts at low memory. Since you overwrite the info # for highest memory numbers first, you may be able to get away with it as long as you don't # attempt to use higher memories. # Steve if beginaddr <= 0x2B8: self.printErrorCallback("Sorry, this will collide with the pattern entry data since %s is <= 0x2B8!" % hex(beginaddr)) #exit # write the memo and pattern entry from the -end- to the -beginning- (up!) for i in range(len(memoentry)): bf.setIndexedByte(endaddr, 0) endaddr -= 1 for i in range(len(pattmem)): bf.setIndexedByte(endaddr, pattmem[i]) endaddr -= 1 # push the data to a file outfile = open(newbrotherfile, 'wb') d = bf.getFullData() outfile.write(d) outfile.close()
break # otherwise its a valid pattern print "pattern bank #", i # calc pattern size rows100 = bf.getIndexedByte(i*7 + 2) rows1 = bf.getIndexedByte(i*7 + 3) stitches10 = bf.getIndexedByte(i*7 + 4) rows = (rows100 >> 4)*100 + (rows100 & 0xF)*10 + (rows1 >> 4); stitches = (rows1 & 0xF)*100 + (stitches10 >> 4)*10 + (stitches10 & 0xF) print "rows = ", rows, "stitches = ", stitches # print "total nibs per row = ", nibblesPerRow(stitches) # dump the memo data print "memo length =",bytesForMemo(rows) for i in range (bytesForMemo(rows)): b = pointer - i print "\t",hex(b),": ",hex(bf.getIndexedByte(b)) pointer -= bytesForMemo(rows) print "pattern length = ", bytesPerPattern(stitches, rows) for i in range (bytesPerPattern(stitches, rows)): b = pointer - i print "\t",hex(b),": ",hex(bf.getIndexedByte(b)), for j in range(8): if (bf.getIndexedByte(b) & (1<<j)): print "*", else: print " ", print ""
patternnum = 901 # start with 901? i dunno for pat in pats: if (pat["number"] >= patternnum): patternnum = pat["number"] + 1 #print patternnum progentry.append(int(patternnum / 100) & 0xF) progentry.append( (int((patternnum % 100)/10) << 4) | (int(patternnum % 10) & 0xF) ) print "Program entry: ",map(hex, progentry) # now to make the actual, yknow memo+pattern data # the memo seems to be always blank. i have no idea really memoentry = [] for i in range(bytesForMemo(height)): memoentry.append(0x0) # now for actual real live pattern data! pattmemnibs = [] for r in range(height): row = [] # we'll chunk in bits and then put em into nibbles for s in range(width): value = TheImage.getpixel((width-s-1,height-r-1)) if (value != 0): row.append(1) else: row.append(0) #print row # turn it into nibz for s in range(roundfour(width) / 4):
sys.stdout.write(' ') #sys.stdout.write(str(value)) x = x-1 if x < 0: #did we hit the end of the line? y = y+1 x = width - 1 print " " if y == height: break # debugging stuff done # now to make the actual, yknow memo+pattern data # the memo seems to be always blank. i have no idea really memoentry = [] for i in range(bytesForMemo(height)): memoentry.append(0x0) # now for actual real live pattern data! pattmem = brother.image2nibblestream(TheImage) #print map(hex, pattmem) # whew. # now to insert this data into the file # now we have to figure out the -end- of the last pattern is endaddr = 0x6df beginaddr = thePattern["pattend"]