Advertisement
here2share

graphics.py module ref

Mar 25th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # graphics.py -- pys60
  2. #
  3. # Copyright (c) 2005-2007 Nokia Corporation
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #     http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17.  
  18. import e32
  19. import _graphics
  20. import sysinfo
  21.  
  22. Draw=_graphics.Draw
  23.  
  24. def _revdict(d):
  25.     return dict([(d[k],k) for k in d.keys()])
  26.  
  27. SDK12=not hasattr(_graphics,'FLIP_LEFT_RIGHT')
  28.  
  29. if not SDK12:
  30.     FLIP_LEFT_RIGHT=_graphics.FLIP_LEFT_RIGHT
  31.     FLIP_TOP_BOTTOM=_graphics.FLIP_TOP_BOTTOM
  32.     ROTATE_90=_graphics.ROTATE_90
  33.     ROTATE_180=_graphics.ROTATE_180
  34.     ROTATE_270=_graphics.ROTATE_270
  35.  
  36. class Image(object):
  37.     _twips = sysinfo.display_twips()
  38.     _pixels = sysinfo.display_pixels()
  39.     _default_density = (float(_twips[0])/_pixels[0],
  40.                         float(_twips[1])/_pixels[1])
  41.     _modemap={
  42. '1': _graphics.EGray2,
  43.               'L': _graphics.EGray256,
  44.               'RGB12': _graphics.EColor4K,
  45.               'RGB16': _graphics.EColor64K,
  46.               'RGB': _graphics.EColor16M
  47. }
  48.     _moderevmap=_revdict(_modemap)
  49.     def __init__(self,img):
  50.         self._image=img
  51.         if img.twipsize == (0,0):
  52.             img.twipsize = (self._default_density[0]*img.size[0],self._default_density[1]*img.size[1])
  53.         self._drawapi=self._image._drawapi
  54.         self._draw=_graphics.Draw(self._image)
  55.         self._bitmapapi=self._image._bitmapapi
  56.         self.getpixel=self._image.getpixel
  57.         self._lock=None
  58.         self._waiting=0
  59.         self._resized_image=None
  60.         for k in _graphics._draw_methods:
  61.             setattr(self,k,getattr(self._draw,k))
  62.     size=property(lambda self:self._image.size)
  63.     mode=property(lambda self:self._moderevmap[self._image.mode])
  64.  
  65.     twipsize=property(lambda self: self._image.twipsize,
  66.                       lambda self, value: setattr(self._image,
  67.                                                   "twipsize",
  68.                                                   value))
  69.     def from_cfbsbitmap(bitmap):
  70.         return Image(_graphics.ImageFromCFbsBitmap(bitmap))
  71.     from_cfbsbitmap=staticmethod(from_cfbsbitmap)
  72.     def from_icon(filename,image_id,size):
  73.         if e32.s60_version_info>=(3,0):
  74.             return Image(_graphics.ImageFromIcon(filename,image_id,size[0],size[1]))
  75.         else:
  76.             raise RuntimeError('not supported')
  77.     from_icon=staticmethod(from_icon)
  78.     def new(size,mode='RGB16'):
  79.         if not Image._modemap.has_key(mode):
  80.             raise ValueError('invalid mode')
  81.         return Image(_graphics.ImageNew(size,Image._modemap[mode]))
  82.     new=staticmethod(new)
  83.     if not SDK12:
  84.         def open(filename):
  85.             def finish_load(errcode):
  86.                 img._errcode=errcode
  87.                 lock.signal()
  88.             lock=e32.Ao_lock()
  89.             img=Image(_graphics.ImageOpen(unicode(filename),finish_load))
  90.             lock.wait()        
  91.             if img._errcode != 0:
  92.                 raise SymbianError,(img._errcode,
  93.                                     "Error loading image:"+e32.strerror(img._errcode))
  94.             return img
  95.         open=staticmethod(open)
  96.         def inspect(filename):
  97.             (size,mode)=_graphics.ImageInspect(unicode(filename))
  98.             return {
  99. 'size': size
  100. }
  101.         inspect=staticmethod(inspect)
  102.         def load(self,filename,callback=None):
  103.             self._wait()
  104.             self._filename=unicode(filename)
  105.             self._usercallback=callback
  106.             self._lock=e32.Ao_lock()
  107.             self._image.load(self._filename,self._callback)
  108.             if callback is None:
  109.                 self._wait()
  110.                 if self._errcode != 0:
  111.                     err=self._errcode
  112.                     self._errcode=0
  113.                     raise SymbianError,(err, "Error loading image:"+e32.strerror(err))
  114.         def save(self,filename,callback=None,format=None,quality=75,bpp=24,compression='default'):
  115.             if format is None:
  116.                 if filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'):
  117.                     format='JPEG'
  118.                 elif filename.lower().endswith('.png'):
  119.                     format='PNG'
  120.                 else:
  121.                     raise ValueError('unrecognized suffix and format not specified')
  122.             self._wait()
  123.             lock=e32.Ao_lock()
  124.             self._image.save(unicode(filename),self._callback,format,quality,compression,bpp)
  125.             # If the code above didn't raise an exception, this method
  126.             # will succeed, so now it's safe to modify object state.
  127.             self._usercallback=callback
  128.             self._lock=lock
  129.             if callback is None:
  130.                 self._wait()
  131.                 if self._errcode != 0:
  132.                     err=self._errcode
  133.                     self._errcode=0
  134.                     raise SymbianError,(err, "Error loading image:"+e32.strerror(err))
  135.         def resize(self,size,callback=None,keepaspect=0):
  136.             self._wait()
  137.             newimage=Image.new(size,self.mode)
  138.             lock=e32.Ao_lock()
  139.             self._image.resize(newimage,keepaspect,self._callback)
  140.             # If the code above didn't raise an exception, this method
  141.             # will succeed, so now it's safe to modify object state.
  142.             self._lock=lock
  143.             self._usercallback=callback
  144.             self._resized_image=newimage
  145.             if callback is None:
  146.                 self._wait()
  147.                 if self._errcode != 0:
  148.                     err=self._errcode
  149.                     self._errcode=0
  150.                     raise SymbianError,(err, "Error resizing image:"+e32.strerror(err))
  151.                 t=self._resized_image
  152.                 self._resized_image=None
  153.                 return t
  154.         def transpose(self,direction,callback=None):
  155.             self._wait()
  156.             if direction == ROTATE_90 or direction == ROTATE_270:
  157.                 newsize=(self.size[1],self.size[0])
  158.             else:
  159.                 newsize=self.size
  160.             newimage=Image.new(newsize,self.mode)
  161.             lock=e32.Ao_lock()
  162.             self._image.transpose(newimage,direction,self._callback)
  163.             # If the code above didn't raise an exception, this method
  164.             # will succeed, so now it's safe to modify object state.
  165.             self._lock=lock
  166.             self._usercallback=callback
  167.             self._resized_image=newimage
  168.             if callback is None:
  169.                 self._wait()
  170.                 if self._errcode != 0:
  171.                     err=self._errcode
  172.                     self._errcode=0
  173.                     raise RuntimeError("Error resizing image:"+str(err))
  174.                 t=self._resized_image
  175.                 self._resized_image=None
  176.                 return t
  177.         def _callback(self, errcode):
  178.             self._errcode=errcode
  179.             if self._lock:
  180.                 self._lock.signal()
  181.             self._lock=None
  182.             if self._usercallback is not None:
  183.                 t=self._usercallback
  184.                 self._usercallback=None
  185.                 if self._resized_image is not None: # resize in progress
  186.                     if self._errcode == 0:
  187.                         newimage=self._resized_image
  188.                         self._resized_image=None
  189.                         t(newimage)
  190.                     else:
  191.                         t(None)
  192.                 else:
  193.                     t(self._errcode)
  194.         def _wait(self):
  195.             if self._lock:
  196.                 if self._waiting:
  197.                     raise RuntimeError("Image object busy.")
  198.                 self._waiting=1
  199.                 self._lock.wait()
  200.                 self._waiting=0
  201.         def stop(self):
  202.             self._image.stop()
  203.             if self._lock:
  204.                 self._errcode=0
  205.                 self._lock.signal()
  206.                 self._lock=None
  207.  
  208. def screenshot():
  209.     return Image(_graphics.screenshot())
  210.  
  211.  
  212. FONT_BOLD=1
  213. FONT_ITALIC=2
  214. FONT_SUBSCRIPT=4
  215. FONT_SUPERSCRIPT=8
  216. FONT_ANTIALIAS=16
  217. FONT_NO_ANTIALIAS=32
  218.  
  219. __all__=('Draw',
  220.          'Image',
  221.          'screenshot',
  222.          'FONT_BOLD',
  223.          'FONT_ITALIC',
  224.          'FONT_SUBSCRIPT',
  225.          'FONT_SUPERSCRIPT',
  226.          'FONT_ANTIALIAS',
  227.          'FONT_NO_ANTIALIAS')
  228.  
  229. if not SDK12:
  230.     __all__+=(('FLIP_LEFT_RIGHT',
  231.                'FLIP_TOP_BOTTOM',
  232.                'ROTATE_90',
  233.                'ROTATE_180',
  234.                'ROTATE_270'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement