home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / common / SplitImage2.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  12.1 KB  |  351 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import wx
  5. from util.primitives.funcs import do
  6. from util.primitives.mapping import to_storage, Storage
  7. from gui.toolbox import get_wxColor
  8. from gui import skin
  9.  
  10. class SplitImage2(object):
  11.     
  12.     def __init__(self, image_dict):
  13.         imgs = self.image_dictionary = to_storage(image_dict)
  14.         image = skin.load_image(imgs.source)
  15.         self.cache = Storage()
  16.         clipcolor = imgs.get('clipcolor', None)
  17.         if clipcolor is not None:
  18.             self.cache.clipcolor = get_wxColor(imgs.clipcolor)
  19.             image.SetMask(wx.Mask(image, self.cache.clipcolor))
  20.         
  21.         imgw = image.GetWidth()
  22.         self.imgw = imgw
  23.         imgh = image.GetHeight()
  24.         self.imgh = imgh
  25.         self.draw_commands = []
  26.         if imgs.corners:
  27.             self.regions_to_draw = None
  28.             cornw = imgs.cornw = imgs.corners.size[0]
  29.             cornh = imgs.cornh = imgs.corners.size[1]
  30.             if imgs.corners.side == 'left':
  31.                 self.corners_to_draw = [
  32.                     'top_left',
  33.                     'bottom_left']
  34.                 if imgw - cornw > 0:
  35.                     self.cache.large_right = image.GetSubBitmap(wx.Rect(cornw, 0, imgw - cornw, imgh))
  36.                     self.regions_to_draw = [
  37.                         'left',
  38.                         'large_right']
  39.                 else:
  40.                     self.regions_to_draw = [
  41.                         'left']
  42.             elif imgs.corners.side == 'right':
  43.                 self.corners_to_draw = [
  44.                     'top_right',
  45.                     'bottom_right']
  46.                 if imgw - cornw > 0:
  47.                     self.cache.large_left = image.GetSubBitmap(wx.Rect(0, 0, imgw - cornw, imgh))
  48.                     self.regions_to_draw = [
  49.                         'large_left',
  50.                         'right']
  51.                 else:
  52.                     self.regions_to_draw = [
  53.                         'right']
  54.             elif imgs.corners.side == 'top':
  55.                 self.corners_to_draw = [
  56.                     'top_left',
  57.                     'top_right']
  58.                 if imgh - cornh > 0:
  59.                     self.cache.large_bottom = image.GetSubBitmap(wx.Rect(0, cornh, imgw, imgh - cornh))
  60.                     self.regions_to_draw = [
  61.                         'top',
  62.                         'large_bottom']
  63.                 else:
  64.                     self.regions_to_draw = [
  65.                         'top']
  66.             elif imgs.corners.side == 'bottom':
  67.                 self.corners_to_draw = [
  68.                     'bottom_left',
  69.                     'bottom_right']
  70.                 if imgh - cornh > 0:
  71.                     self.cache.large_top = image.GetSubBitmap(wx.Rect(0, 0, imgw, imgh - cornh))
  72.                     self.regions_to_draw = [
  73.                         'large_top',
  74.                         'bottom']
  75.                 else:
  76.                     self.regions_to_draw = [
  77.                         'bottom']
  78.             else:
  79.                 self.corners_to_draw = [
  80.                     'top_left',
  81.                     'top_right',
  82.                     'bottom_left',
  83.                     'bottom_right']
  84.                 self.regions_to_draw = [
  85.                     'left',
  86.                     'right',
  87.                     'top',
  88.                     'bottom',
  89.                     'center']
  90.             [ self.draw_commands.append((getattr(self, 'draw_' + corner + '_corner'), None)) for corner in self.corners_to_draw ]
  91.             if 'top_right' in self.corners_to_draw:
  92.                 self.cache.top_right = image.GetSubBitmap(wx.Rect(imgw - cornw, 0, cornw, cornh))
  93.             
  94.             if 'bottom_left' in self.corners_to_draw:
  95.                 self.cache.bottom_left = image.GetSubBitmap(wx.Rect(0, imgh - cornh, cornw, cornh))
  96.             
  97.             if 'bottom_right' in self.corners_to_draw:
  98.                 self.cache.bottom_right = image.GetSubBitmap(wx.Rect(imgw - cornw, imgh - cornh, cornw, cornh))
  99.             
  100.             if 'left' in self.regions_to_draw:
  101.                 self.cache.left = image.GetSubBitmap(wx.Rect(0, cornh, cornw, imgh - cornh * 2))
  102.             
  103.             if 'right' in self.regions_to_draw:
  104.                 self.cache.right = image.GetSubBitmap(wx.Rect(imgw - cornw, cornh, cornw, imgh - cornh * 2))
  105.             
  106.             if 'top' in self.regions_to_draw:
  107.                 self.cache.top = image.GetSubBitmap(wx.Rect(cornw, 0, imgw - cornw * 2, cornh))
  108.             
  109.             if 'bottom' in self.regions_to_draw:
  110.                 self.cache.bottom = image.GetSubBitmap(wx.Rect(cornw, imgh - cornh, imgw - cornw * 2, cornh))
  111.             
  112.             if 'center' in self.regions_to_draw:
  113.                 self.cache.center = image.GetSubBitmap(wx.Rect(cornw, cornh, imgw - cornw * 2, imgh - cornh * 2))
  114.             
  115.         else:
  116.             self.cache.total = image
  117.             self.cache['totalch'] = 'left'
  118.             self.cache['totalcv'] = 'top'
  119.             self.cache['totalco'] = [
  120.                 0,
  121.                 0]
  122.             self.regions_to_draw = [
  123.                 'total']
  124.         for region in self.regions_to_draw:
  125.             if region in self.image_dictionary.regions:
  126.                 region_dict = self.image_dictionary.regions[region]
  127.                 style = region_dict['style']
  128.                 self.draw_commands.append((getattr(self, 'draw_' + region), style))
  129.                 if style == 'static' or self.image_dictionary.style == 'static':
  130.                     color = get_wxColor(region_dict['color'])
  131.                     if color is not None:
  132.                         self.cache[region + 'cb'] = wx.Brush(color)
  133.                     else:
  134.                         self.cache[region + 'cb'] = None
  135.                     self.cache[region + 'ch'] = region_dict['halign']
  136.                     self.cache[region + 'cv'] = region_dict['valign']
  137.                     self.cache[region + 'co'] = region_dict['offset']
  138.                 
  139.             self.image_dictionary.style == 'static'
  140.             self.draw_commands.append((getattr(self, 'draw_' + region), self.image_dictionary['style']))
  141.         
  142.  
  143.     
  144.     def get_num_anchors_to(self):
  145.         return len(self.get_anchors_to())
  146.  
  147.     
  148.     def get_anchors_to(self):
  149.         return _[1]
  150.  
  151.     
  152.     def draw_total(self, type):
  153.         args = ('total', 0, 0, self.rect.width, self.rect.height)
  154.         getattr(self, 'draw_region_' + type)(*args)
  155.  
  156.     
  157.     def draw_top_left_corner(self, unused):
  158.         self.dc.DrawBitmap(self.cache.top_left, self.rect.x, self.rect.y, True)
  159.  
  160.     
  161.     def draw_top_right_corner(self, unused):
  162.         self.dc.DrawBitmap(self.cache.top_right, self.rect.x + self.rect.width - self.cache.top_right.GetWidth(), self.rect.y, True)
  163.  
  164.     
  165.     def draw_bottom_left_corner(self, unused):
  166.         self.dc.DrawBitmap(self.cache.bottom_left, self.rect.x, self.rect.y + self.rect.height - self.cache.bottom_left.GetHeight(), True)
  167.  
  168.     
  169.     def draw_bottom_right_corner(self, unused):
  170.         self.dc.DrawBitmap(self.cache.bottom_right, self.rect.x + self.rect.width - self.cache.bottom_right.GetWidth(), self.rect.y + self.rect.height - self.cache.bottom_right.GetHeight(), True)
  171.  
  172.     
  173.     def draw_left(self, type):
  174.         args = ('left', 0, self.image_dictionary.cornh, self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh * 2)
  175.         getattr(self, 'draw_region_' + type)(*args)
  176.  
  177.     
  178.     def draw_right(self, type):
  179.         args = ('right', self.rect.width - self.image_dictionary.cornw, self.image_dictionary.cornh, self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh * 2)
  180.         getattr(self, 'draw_region_' + type)(*args)
  181.  
  182.     
  183.     def draw_top(self, type):
  184.         args = ('top', self.image_dictionary.cornw, 0, self.rect.width - self.image_dictionary.cornw * 2, self.image_dictionary.cornh)
  185.         getattr(self, 'draw_region_' + type)(*args)
  186.  
  187.     
  188.     def draw_bottom(self, type):
  189.         args = ('bottom', self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh, self.rect.width - self.image_dictionary.cornw * 2, self.image_dictionary.cornh)
  190.         getattr(self, 'draw_region_' + type)(*args)
  191.  
  192.     
  193.     def draw_center(self, type):
  194.         args = ('center', self.image_dictionary.cornw, self.image_dictionary.cornh, self.rect.width - self.image_dictionary.cornw * 2, self.rect.height - self.image_dictionary.cornh * 2)
  195.         getattr(self, 'draw_region_' + type)(*args)
  196.  
  197.     
  198.     def draw_large_left(self, type):
  199.         args = ('large_left', 0, 0, self.rect.width - self.image_dictionary.cornw, self.rect.height)
  200.         getattr(self, 'draw_region_' + type)(*args)
  201.  
  202.     
  203.     def draw_large_right(self, type):
  204.         args = ('large_right', self.image_dictionary.cornw, 0, self.rect.width - self.image_dictionary.cornw, self.rect.height)
  205.         getattr(self, 'draw_region_' + type)(*args)
  206.  
  207.     
  208.     def draw_large_top(self, type):
  209.         args = ('large_top', 0, 0, self.rect.width, self.rect.height - self.image_dictionary.cornh)
  210.         getattr(self, 'draw_region_' + type)(*args)
  211.  
  212.     
  213.     def draw_large_bottom(self, type):
  214.         args = ('large_bottom', 0, self.image_dictionary.cornh, self.rect.width, self.rect.height - self.image_dictionary.cornh)
  215.         getattr(self, 'draw_region_' + type)(*args)
  216.  
  217.     
  218.     def draw_region_stretch(self, img_string, dest_x, dest_y, dest_w, dest_h):
  219.         if dest_w > 0 and dest_h > 0:
  220.             myimg = self.cache[img_string]
  221.             if img_string + 's' not in self.cache and self.cache[img_string + 's'].GetWidth() != dest_w or self.cache[img_string + 's'].GetHeight() != dest_h:
  222.                 temp = myimg.ConvertToImage()
  223.                 temp.Rescale(dest_w, dest_h)
  224.                 self.cache[img_string + 's'] = wx.BitmapFromImage(temp)
  225.                 self.cache[img_string + 'sr'] = wx.RegionFromBitmap(self.cache[img_string + 's'])
  226.                 if not self.cache[img_string + 'sr'].IsEmpty():
  227.                     self.cache[img_string + 'sr'].Offset(self.rect.x + dest_x, self.rect.y + dest_y)
  228.                 
  229.             
  230.             self.dc.DrawBitmap(self.cache[img_string + 's'], self.rect.x + dest_x, self.rect.y + dest_y, True)
  231.             self.rs.append(self.cache[img_string + 'sr'])
  232.         
  233.  
  234.     
  235.     def draw_region_tile(self, img_string, dest_x, dest_y, dest_w, dest_h):
  236.         if dest_w > 0 and dest_h > 0:
  237.             if img_string + 't' not in self.cache:
  238.                 self.cache[img_string + 't'] = wx.Brush(wx.RED)
  239.                 self.cache[img_string + 't'].SetStipple(self.cache[img_string])
  240.                 if 'clipcolor' in self.image_dictionary:
  241.                     self.cache[img_string + 't'].SetStyle(wx.STIPPLE)
  242.                 
  243.             
  244.             r = wx.Region(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  245.             self.rs.append(r)
  246.             if 'clipcolor' in self.image_dictionary:
  247.                 bmp = wx.EmptyBitmap(dest_w, dest_h, 32)
  248.                 memdc = wx.MemoryDC()
  249.                 memdc.SelectObject(bmp)
  250.                 memdc.SetBrush(self.cache[img_string + 't'])
  251.                 memdc.DrawRectangle(0, 0, dest_w, dest_h)
  252.                 memdc.SelectObject(wx.NullBitmap)
  253.                 memdc.SetBrush(wx.NullBrush)
  254.                 bmp.SetMask(wx.Mask(bmp, self.cache.clipcolor))
  255.                 self.dc.DrawBitmap(bmp, self.rect.x + dest_x, self.rect.y + dest_y)
  256.             else:
  257.                 self.dc.SetBrush(self.cache[img_string + 't'])
  258.                 self.dc.SetPen(wx.TRANSPARENT_PEN)
  259.                 self.dc.DrawRectangle(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  260.                 self.dc.SetPen(wx.NullPen)
  261.                 self.dc.SetBrush(wx.NullBrush)
  262.         
  263.  
  264.     
  265.     def draw_region_static(self, img_string, dest_x, dest_y, dest_w, dest_h):
  266.         if dest_w > 0 and dest_h > 0:
  267.             myimg = self.cache[img_string]
  268.             halign = self.cache[img_string + 'ch']
  269.             valign = self.cache[img_string + 'cv']
  270.             offset = self.cache[img_string + 'co']
  271.             if halign == 'left':
  272.                 x = 0
  273.             elif halign == 'right':
  274.                 x = dest_w - myimg.GetWidth()
  275.             else:
  276.                 x = dest_w / 2 - myimg.GetWidth() / 2
  277.             x = x + dest_x + self.rect.x + offset[0]
  278.             if valign == 'top':
  279.                 y = 0
  280.             elif valign == 'bottom':
  281.                 y = dest_h - myimg.GetHeight()
  282.             else:
  283.                 y = dest_h / 2 - myimg.GetHeight() / 2
  284.             y = y + dest_y + self.rect.y + offset[1]
  285.             self.dc.SetClippingRegion(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  286.             if img_string + 'cb' in self.cache and self.cache[img_string + 'cb'] is not None:
  287.                 self.dc.SetBrush(self.cache[img_string + 'cb'])
  288.                 self.dc.SetPen(wx.TRANSPARENT_PEN)
  289.                 self.dc.DrawRectangle(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  290.                 self.dc.SetPen(wx.NullPen)
  291.                 self.dc.SetBrush(wx.NullBrush)
  292.             
  293.             self.dc.DrawBitmap(myimg, x, y, True)
  294.             if img_string + 'r' not in self.cache:
  295.                 new = (wx.RegionFromBitmap(myimg), x, y)
  296.                 self.cache[img_string + 'r'] = new
  297.                 if not new[0].IsEmpty():
  298.                     new[0].Offset(x, y)
  299.                 
  300.             else:
  301.                 (old, oldx, oldy) = self.cache[img_string + 'r']
  302.                 if not old.IsEmpty():
  303.                     old.Offset(x - oldx, y - oldy)
  304.                 
  305.                 self.cache[img_string + 'r'] = (old, x, y)
  306.             self.rs.append(self.cache[img_string + 'r'][0])
  307.             self.dc.DestroyClippingRegion()
  308.         
  309.  
  310.     
  311.     def draw(self, dc, rect):
  312.         self.dc = dc
  313.         self.rect = rect
  314.         self.rs = []
  315.         do((lambda .0: for func, args in .0:
  316. func(args))(self.draw_commands))
  317.         self.region = wx.Region(0, 0, 0, 0)
  318.         for region in self.rs:
  319.             if not region.IsEmpty():
  320.                 self.region.UnionRegion(region)
  321.                 continue
  322.         
  323.         self.dc = None
  324.         self.rect = None
  325.  
  326.  
  327. if __name__ == '__main__':
  328.     from skins import images, skins
  329.     app = wx.PySimpleApp()
  330.     image = Storage()
  331.     image['style'] = 'static'
  332.     image['source'] = 'skins/default/checkerboard9.png'
  333.     image['corners'] = { }
  334.     image['halign'] = 'left'
  335.     image['valign'] = 'top'
  336.     image['offset'] = [
  337.         50,
  338.         50]
  339.     image['regions'] = dict(center = dict(style = 'static', color = 'red', valign = 'center', halign = 'center', offset = [
  340.         50,
  341.         50]))
  342.     skins.res_path = 'res/'
  343.     destbitmap = images.get('skins/default/blue-flower.jpg')
  344.     temp_dc = wx.MemoryDC()
  345.     temp_dc.SelectObject(destbitmap)
  346.     splitimg = SplitImage2(image)
  347.     splitimg.draw(temp_dc, wx.Rect(50, 50, 200, 200))
  348.     temp_dc.SelectObject(wx.NullBitmap)
  349.     destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
  350.  
  351.