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()
Esempio n. 2
0
    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()
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):
        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),
    print