home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 September / PCWorld_2004-09_cd.bin / software / temacd / paintshop / psp810entr.exe / Data1.cab / JascUtils.py < prev    next >
Encoding:
Python Source  |  2003-10-02  |  10.1 KB  |  244 lines

  1. from JascApp import *
  2.  
  3. # Copyright 2003 Jasc Software Inc., all rights reserved
  4. # This file contains utility routines provided by Jasc Software.
  5.  
  6. class SaveSelection:
  7.     ''' define a helper class that can save any active selection to the alpha
  8.         channel and restore it later
  9.     '''
  10.     def __init__( self, Environment, Doc ):
  11.         ''' at init time we save the environment variable provided by PSP,
  12.             and if a selection exists we save it to an alpha channel
  13.         '''
  14.         self.Env = Environment
  15.         self.SavedSelection =  '__$TempSavedSelection$__'
  16.         self.IsSaved = 0
  17.         self.SavedOnDoc = Doc
  18.         
  19.         SelResult = App.Do( self.Env, 'GetRasterSelectionRect' )
  20.         if SelResult[ 'Type' ] != App.Constants.SelectionType.None:
  21.             # if there is a selection save it to the alpha channel
  22.             App.Do( self.Env, 'SelectSaveDisk', {
  23.                 'FileName': self.SavedSelection, 
  24.                 'Overwrite': App.Constants.Boolean.true, 
  25.                 'GeneralSettings': {
  26.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  27.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  28.                     }
  29.                 }, Doc)
  30.             self.IsSaved = 1    # set this so we know we saved one
  31.             
  32.             if SelResult[ 'Type' ] == App.Constants.SelectionType.Floating:
  33.                 # if the selection is floating promote it to a layer
  34.                 App.Do( self.Env, 'SelectPromote', {
  35.                         'KeepSelection': App.Constants.Boolean.false, 
  36.                         'LayerName': 'Selection promoted via script', 
  37.                         'GeneralSettings': {
  38.                             'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  39.                             'AutoActionMode': App.Constants.AutoActionMode.Match
  40.                             }
  41.                         }, Doc)
  42.             else:
  43.                 App.Do( self.Env, 'SelectNone' )
  44.            
  45.             
  46.     def RestoreSelection( self ):
  47.         ''' if we have saved a selection, restore it now.  If we promoted
  48.             a floating selection to a layer we don't restore the selection
  49.             but don't attempt to mess with the layer in any way
  50.         '''
  51.         if self.IsSaved == 1:
  52.             # load the selection back from disk - this will replace any existing selection
  53.             App.Do( self.Env, 'SelectLoadDisk', {
  54.                     'FileName': self.SavedSelection, 
  55.                     'Operation': App.Constants.SelectionOperation.Replace, 
  56.                     'UpperLeft': App.Constants.Boolean.false, 
  57.                     'ClipToCanvas': App.Constants.Boolean.false, 
  58.                     'GreyMethod': App.Constants.CreateMaskFrom.Luminance, 
  59.                     'Invert': App.Constants.Boolean.false, 
  60.                     'GeneralSettings': {
  61.                         'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  62.                         'AutoActionMode': App.Constants.AutoActionMode.Match
  63.                         }
  64.                     }, self.SavedOnDoc)
  65.  
  66.     # end class SaveSelection
  67.  
  68. def NameFromMaterial( Material, Delimiter=' ', IncludeTexture=1 ):
  69.     ''' Given a material repository, return a name that describes it.
  70.         By default the name is delimited with space, but the delimiter
  71.         parameter can be used to override it.
  72.         By default textures are included in the name, but can be omitted
  73.         by setting the IncludeTexture parameter to 0
  74.     '''
  75.     TextureName = None
  76.     TypeName = ''
  77.     if Material is None:
  78.         CoreName = 'Null'
  79.     else:
  80.         if Material[ 'Pattern' ] and \
  81.            (Material[ 'Pattern' ][ 'Name' ] or Material[ 'Pattern' ][ 'Image' ] ):
  82.             TypeName = 'Pattern'
  83.             if Material[ 'Pattern' ][ 'Name' ]:
  84.                 CoreName = Material[ 'Pattern'  ][ 'Name' ]
  85.             else:
  86.                 CoreName = 'Inline'
  87.         elif Material[ 'Gradient' ] and Material[ 'Gradient' ][ 'Name' ]:
  88.             TypeName = 'Gradient'
  89.             GradType = {}
  90.             GradType[ App.Constants.GradientType.Linear ] = 'Linear'
  91.             GradType[ App.Constants.GradientType.Rectangular ] = 'Rectangular'
  92.             GradType[ App.Constants.GradientType.Radial ] = 'Radial'
  93.             GradType[ App.Constants.GradientType.Angular ] = 'Sunburst'
  94.             
  95.             CoreName = '%s%s%s' % ( Material[ 'Gradient' ][ 'Name' ], Delimiter, 
  96.                                     GradType[ Material[ 'Gradient' ][ 'GradientType' ] ] )
  97.         else:
  98.             TypeName = 'Solid'
  99.             CoreName = '%02x%02x%02x' % Material[ 'Color' ]
  100.  
  101.         if Material[ 'Texture' ] and Material[ 'Texture' ][ 'Name' ]:
  102.             TextureName = Material[ 'Texture' ]['Name']
  103.  
  104.     if TextureName is not None and IncludeTexture != 0:
  105.         MaterialName = '%s%s%s%s%s' % ( TypeName, Delimiter, CoreName, Delimiter, TextureName )
  106.     else:
  107.         MaterialName = '%s%s%s' % ( TypeName, Delimiter, CoreName )
  108.  
  109.     return MaterialName
  110.  
  111. def IsNullMaterial( Material ):
  112.     ' check if the passed in material is none.  Returns true if null, false if non-null'
  113.     if Material is None:    
  114.         return App.Constants.Boolean.true   # material might be entirely none
  115.  
  116.     ColorIsNone = 0
  117.     GradientIsNone = 0
  118.     PatternIsNone = 0
  119.     
  120.     if Material['Color'] is None:
  121.         ColorIsNone = 1
  122.  
  123.     if Material['Gradient'] is None or Material['Gradient']['Name'] is None:
  124.         GradientIsNone = 1
  125.  
  126.     if Material['Pattern'] is None or \
  127.        (Material['Pattern']['Name'] is None and Material['Pattern']['Image'] is None):
  128.         PatternIsNone = 1
  129.  
  130.     if ColorIsNone and GradientIsNone and PatternIsNone:
  131.         return App.Constants.Boolean.true   #it works out to none
  132.     else:
  133.         return App.Constants.Boolean.false
  134.  
  135.  
  136. def IsFlatImage( Environment, Doc ):
  137.     'Determine if Doc consists of a single background layer.  True if flat, false if not'
  138.     ImageInfo = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  139.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  140.  
  141.     if ImageInfo[ 'LayerNum' ] == 1 and LayerInfo[ 'IsBackground' ] == App.Constants.Boolean.true:
  142.         return App.Constants.Boolean.true
  143.     else:
  144.         return App.Constants.Boolean.false
  145.  
  146. def IsPaletted( Environment, Doc ):
  147.     '''Determine if the current image is paletted.  Greyscale is not counted as paletted
  148.        Returns true on paletted, false if not
  149.     '''
  150.     # these are all the paletted pixel formats
  151.     TargetFormats = [ App.Constants.PixelFormat.Index1, App.Constants.PixelFormat.Index4,
  152.                       App.Constants.PixelFormat.Index8 ]
  153.     
  154.     # are we paletted?
  155.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  156.  
  157.     if Info['PixelFormat'] in TargetFormats:
  158.         return App.Constants.Boolean.true
  159.     else:
  160.         return App.Constants.Boolean.false
  161.  
  162. def IsTrueColor( Environment, Doc ):
  163.     ''' Determine if the current image is true color.  Greyscale does not count
  164.         Returns true for true color, false for all others
  165.     '''
  166.     TargetFormats = [ App.Constants.PixelFormat.BGR, App.Constants.PixelFormat.BGRA ]
  167.     
  168.     # are we true color?
  169.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  170.     if Info['PixelFormat'] in TargetFormats:
  171.         return App.Constants.Boolean.true
  172.     else:
  173.         return App.Constants.Boolean.false
  174.  
  175. def IsGreyScale( Environment, Doc ):
  176.     ' Determine if the current image (not layer) is greyscale. True if it is, false otherwise'
  177.     TargetFormats = [ App.Constants.PixelFormat.Grey, App.Constants.PixelFormat.GreyA ]
  178.     
  179.     # are we true color?
  180.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  181.     if Info['PixelFormat'] in TargetFormats:
  182.         return App.Constants.Boolean.true
  183.     else:
  184.         return App.Constants.Boolean.false
  185.  
  186. def LayerIsRaster( Environment, Doc ):
  187.     'Returns true if the current layer is a raster layer'
  188.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  189.     if LayerInfo[ 'LayerType' ] == App.Constants.LayerType.Raster:
  190.         return App.Constants.Boolean.true
  191.     else:
  192.         return App.Constants.Boolean.false
  193.  
  194.  
  195. def LayerIsVector( Environment, Doc ):
  196.     'Returns true if the current layer is a vector layer'
  197.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  198.     if LayerInfo[ 'LayerType' ] == App.Constants.LayerType.Vector:
  199.         return App.Constants.Boolean.true
  200.     else:
  201.         return App.Constants.Boolean.false
  202.  
  203. def LayerIsBackground( Environment, Doc ):
  204.     'Returns true if the current layer is the background layer'
  205.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  206.     if LayerInfo[ 'IsBackground' ] == App.Constants.LayerType.Vector:
  207.         return App.Constants.Boolean.true
  208.     else:
  209.         return App.Constants.Boolean.false
  210.     
  211.  
  212. def GetLayerCount( Environment, Doc ):
  213.     'Returns number of layers in Doc'
  214.     ImageInfo = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  215.     return ImageInfo[ 'LayerNum' ]    
  216.  
  217. def GetCurrentLayerName( Environment, Doc ):
  218.     'Returns the name of the current layer in Doc'
  219.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  220.     return LayerInfo[ 'General' ][ 'Name' ]
  221.  
  222. def PromoteToTrueColor( Environment, Doc ):
  223.     'If the current image type is paletted, promote it to true color.  Greyscale is left alone'
  224.     if IsPaletted( Environment, Doc ):
  225.         App.Do( Environment, 'IncreaseColorsTo16Million', {}, Doc )
  226.     return
  227.  
  228. def RequireADoc( Environment ):
  229.     '''Test that we actually have a target document, and put up a message box if we dont
  230.        Returns true if we have an open doc, false otherwise
  231.     '''
  232.     if App.TargetDocument is None:
  233.         App.Do( Environment, 'MsgBox', {
  234.                 'Buttons': App.Constants.MsgButtons.OK, 
  235.                 'Icon': App.Constants.MsgIcons.Stop, 
  236.                 'Text': 'This script requires an open image.', 
  237.                 })
  238.         return App.Constants.Boolean.false
  239.     else:
  240.         return App.Constants.Boolean.true
  241.  
  242.     
  243.         
  244.