MCPcopy Create free account
hub / github.com/ActiveState/code / loadImage

Method loadImage

recipes/Python/577674_Bitmap_Maker/recipe-577674.py:606–656  ·  view source on GitHub ↗
(self, image)

Source from the content-addressed store, hash-verified

604 self.font = font_data
605
606 def loadImage(self, image):
607
608 width = stringToLong(image, 0x12)
609 height = stringToLong(image, 0x16)
610 self.wd = width
611 self.ht = height
612 self.bgcolor = 0
613 self.fgcolor = 0
614 self.palette = []
615 self.currentPen = 0
616
617 bitarray = []
618 bitarray.append([])
619
620 row_idx = 0
621 col_idx = 0
622 idx_offset = stringToLong(image, 0xa)
623 idx = idx_offset
624 line_padding = (4 - ( width % 4 ) ) % 4
625 bytes_in_row = width*3 + line_padding
626
627 while (idx+3) <= len(image):
628 if col_idx >= width:
629 # end of row, dismiss padding
630 row_idx += 1
631 idx += line_padding
632 col_idx = 0
633 if idx + 3 > len(image):
634 break
635 # add new row to image
636 bitarray.append([])
637
638 c = Color(ord(image[idx+2]), ord(image[idx+1]), ord(image[idx]))
639 # register palette
640 colorNum = c.toLong()
641 try:
642 self.currentPen = self.palette.index(colorNum)
643 except ValueError:
644 if len( self.palette ) < 256 :
645 self.palette.append(colorNum)
646 self.currentPen = len( self.palette ) - 1
647 else:
648 self.currentPen = self.fgcolor
649
650 bitarray[row_idx].append(self.currentPen)
651 idx += 3
652 col_idx += 1
653
654 # this is it
655 bitarray.reverse()
656 self.bitarray = bitarray
657
658################################################################################
659

Callers

nothing calls this directly

Calls 6

toLongMethod · 0.95
stringToLongFunction · 0.85
ColorClass · 0.70
appendMethod · 0.45
indexMethod · 0.45
reverseMethod · 0.45

Tested by

no test coverage detected