Subversion Repositories filter_foundry

Rev

Rev 189 | Rev 194 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     This file is part of "Filter Foundry", a filter plugin for Adobe Photoshop
  3.     Copyright (C) 2003-2009 Toby Thain, toby@telegraphics.com.au
  4.     Copyright (C) 2018-2019 Daniel Marschall, ViaThinkSoft
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. /* Portions Copyright 1996 - 1999 Adobe Systems Incorporated                */
  22. /* All Rights Reserved.                                            */
  23.  
  24. //#include <stdio.h>
  25.  
  26. //#include "world.h" // must come before Photoshop includes
  27.  
  28. #include "ff.h"
  29.  
  30. #include "scripting.h"
  31. //#include "ui.h"
  32. #include "dbg.h"
  33.  
  34. //extern FilterRecordPtr gpb;
  35.  
  36. OSErr put_cstring(PIWriteDescriptor token,DescriptorKeyID key,char *s){
  37.         size_t n = strlen(s);
  38.         Ptr p;
  39.         Handle h = PINEWHANDLE((int32)n);
  40.         p = PILOCKHANDLE(h,false);
  41.         memcpy(p,s,n);
  42.         PIUNLOCKHANDLE(h);
  43.         return PIPutText(token,key,h);
  44.         /* FIXME: not sure if we are supposed to dispose of handle */
  45. }
  46.  
  47. char *get_cstring(PIReadDescriptor token){
  48.         int n;
  49.         Ptr p;
  50.         char *str = NULL;
  51.         Handle h;
  52.         OSErr e = PIGetText(token,&h);
  53.  
  54.         if(!e && h){
  55.                 n = PIGETHANDLESIZE(h);
  56.                 p = PILOCKHANDLE(h,false);
  57.                 //sprintf(str,"get_cstring: token=%#x s=%#x h=%#x p=%#x n=%d",token,s,h,p,n); dbg(str);
  58.                 if( (str = malloc(n+1)) ){
  59.                         memcpy(str,p,n);
  60.                         str[n] = 0;
  61.                 }
  62.                 PIUNLOCKHANDLE(h);
  63.                 /* FIXME: not sure if we are supposed to dispose of handle */
  64.         }
  65.         return str;
  66. }
  67.  
  68. /* return true if dialog should be shown */
  69.  
  70. Boolean ReadScriptParamsOnRead(void)
  71. {
  72.         PIReadDescriptor token;
  73.         DescriptorKeyID key;
  74.         DescriptorTypeID type;
  75.         DescriptorKeyIDArray array = { NULLID };
  76.         int32 flags;
  77.         OSErr stickyError;
  78.         int32 v;
  79.  
  80.         if (DescriptorAvailable(NULL)){ /* playing back.  Do our thing. */
  81.                 token = OpenReader(array);
  82.                 if (token){
  83.                         while (PIGetKey(token, &key, &type, &flags)){
  84.                                 switch (key){
  85.                                         case PARAM_R_KEY: expr[0] = get_cstring(token); break;
  86.                                         case PARAM_G_KEY: expr[1] = get_cstring(token); break;
  87.                                         case PARAM_B_KEY: expr[2] = get_cstring(token); break;
  88.                                         case PARAM_A_KEY: expr[3] = get_cstring(token); break;
  89.                                         default:
  90.                                                 if(key >= PARAM_CTL0_KEY && key <= PARAM_CTL7_KEY){
  91.                                                         PIGetInt(token,&v);
  92.                                                         slider[key - PARAM_CTL0_KEY] = v;
  93.                                                 }
  94.                                                 break;
  95.                                 }
  96.                         }
  97.  
  98.                         stickyError = CloseReader(&token); // closes & disposes.
  99.  
  100.                         // all Filter Foundry parameters are optional,
  101.                         // so we needn't worry if any are missing
  102.                 }
  103.  
  104.                 return gpb->descriptorParameters->playInfo == plugInDialogDisplay; /* TRUE if want to show our Dialog */
  105.         }
  106.  
  107.         return false;
  108. }
  109.  
  110. OSErr WriteScriptParamsOnRead(void)
  111. {
  112.         PIWriteDescriptor token;
  113.         OSErr gotErr = noErr;
  114.         extern int ctls[],maps[],nplanes;
  115.         int i,allctls;
  116.  
  117.         if (DescriptorAvailable(NULL)){ /* recording.  Do our thing. */
  118.                 token = OpenWriter();
  119.                 if (token){
  120.                         // write keys here
  121.                         if(!gdata->standalone){
  122.                                 if (nplanes > 0) put_cstring(token, PARAM_R_KEY, expr[0]);
  123.                                 if (nplanes > 1) put_cstring(token, PARAM_G_KEY, expr[1]);
  124.                                 if (nplanes > 2) put_cstring(token, PARAM_B_KEY, expr[2]);
  125.                                 if (nplanes > 3) put_cstring(token, PARAM_A_KEY, expr[3]);
  126.                         }
  127.  
  128.                         /* only write values for the sliders that are actually used! */
  129.                         allctls = checksliders(4,ctls,maps);
  130.                         for(i = 0; i < 8; ++i)
  131.                                 if(allctls || ctls[i])
  132.                                         PIPutInt(token, PARAM_CTL0_KEY+i, slider[i]);
  133.  
  134.                         gotErr = CloseWriter(&token); /* closes and sets dialog optional */
  135.                         /* done.  Now pass handle on to Photoshop */
  136.                 }
  137.         }
  138.         return gotErr;
  139. }
  140.  
  141.  
  142. //-------------------------------------------------------------------------------
  143. //
  144. //      HostDescriptorAvailable
  145. //
  146. //      Determines whether the PIDescriptorParameters callback is available.
  147. //
  148. //      Check for valid suite version, routine suite version, and routine count.
  149. //      Also check that the subset of routines we actually use is actually present.
  150. //
  151. //-------------------------------------------------------------------------------
  152.  
  153. Boolean HostDescriptorAvailable (PIDescriptorParameters *procs,
  154.                                                                  Boolean *outNewerVersion)
  155. {
  156.         if(outNewerVersion)
  157.                 *outNewerVersion = procs->descriptorParametersVersion > kCurrentDescriptorParametersVersion
  158.                         || procs->readDescriptorProcs->readDescriptorProcsVersion > kCurrentReadDescriptorProcsVersion
  159.                         || procs->writeDescriptorProcs->writeDescriptorProcsVersion > kCurrentWriteDescriptorProcsVersion ;
  160.  
  161.         return procs != NULL
  162.                 && procs->descriptorParametersVersion == kCurrentDescriptorParametersVersion
  163.  
  164.                 && procs->readDescriptorProcs != NULL
  165.                 && procs->readDescriptorProcs->readDescriptorProcsVersion == kCurrentReadDescriptorProcsVersion
  166.                 && procs->readDescriptorProcs->numReadDescriptorProcs >= kCurrentReadDescriptorProcsCount
  167.                 && procs->readDescriptorProcs->openReadDescriptorProc != NULL
  168.                 && procs->readDescriptorProcs->closeReadDescriptorProc != NULL
  169.                 && procs->readDescriptorProcs->getKeyProc != NULL
  170.                 && procs->readDescriptorProcs->getTextProc != NULL
  171.                 && procs->readDescriptorProcs->getIntegerProc != NULL
  172.  
  173.                 && procs->writeDescriptorProcs != NULL
  174.                 && procs->writeDescriptorProcs->writeDescriptorProcsVersion == kCurrentWriteDescriptorProcsVersion
  175.                 && procs->writeDescriptorProcs->numWriteDescriptorProcs >= kCurrentWriteDescriptorProcsCount
  176.                 && procs->writeDescriptorProcs->openWriteDescriptorProc != NULL
  177.                 && procs->writeDescriptorProcs->closeWriteDescriptorProc != NULL
  178.                 && procs->writeDescriptorProcs->putTextProc != NULL
  179.                 && procs->writeDescriptorProcs->putIntegerProc != NULL ;
  180. }
  181.  
  182.  
  183. //-------------------------------------------------------------------------------
  184. //
  185. //      HostCloseReader
  186. //
  187. //      Closes a read token, disposes its handle, sets the token to NULL, and
  188. //      sets the parameter blocks' descriptor to NULL.
  189. //
  190. //      The Descriptor Parameters suite are callbacks designed for
  191. //      scripting and automation.  See PIActions.h.
  192. //
  193. //      Inputs:
  194. //              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
  195. //
  196. //              HandleProcs *hProcs                             Pointer to HandleProcs callback.
  197. //
  198. //              PIReadDescriptor *token                 Pointer to token to close.
  199. //
  200. //              procs->descriptor                               Pointer to original read handle.
  201. //
  202. //      Outputs:
  203. //              PIReadDescriptor *token                 Set to NULL.
  204. //
  205. //              procs->descriptor                               Disposed then set to NULL.
  206. //
  207. //              returns OSErr                                   noErr or error if one occurred.
  208. //
  209. //-------------------------------------------------------------------------------
  210.  
  211. OSErr HostCloseReader (PIDescriptorParameters *procs,
  212.                                            HandleProcs *hProcs,
  213.                                            PIReadDescriptor *token)
  214. {
  215.         // Close token:
  216.         OSErr err = procs->readDescriptorProcs->closeReadDescriptorProc(*token);
  217.  
  218.         // Dispose the parameter block descriptor:
  219.         hProcs->disposeProc(procs->descriptor);
  220.  
  221.         // Set the descriptor and the read token to NULL:
  222.         procs->descriptor = NULL;
  223.         *token = NULL;
  224.  
  225.         return err;
  226.  
  227. } // end HostCloseReader
  228.  
  229. //-------------------------------------------------------------------------------
  230. //
  231. //      HostCloseWriter
  232. //
  233. //      Closes a write token, stores its handle in the global parameter block for
  234. //      the host to use, sets the token to NULL, and sets the recordInfo to
  235. //      plugInDialogOptional (the default).
  236. //
  237. //      The Descriptor Parameters suite are callbacks designed for
  238. //      scripting and automation.  See PIActions.h.
  239. //
  240. //      Inputs:
  241. //              PIDescriptorParameters *procs   Pointer to Descriptor Parameters suite.
  242. //
  243. //              HandleProcs *hProcs                             Pointer to HandleProcs callback.
  244. //
  245. //              PIWriteDescriptor *token                Pointer to token to close and pass on.
  246. //
  247. //              procs->descriptor                               Should be NULL.  If not, its contents
  248. //                                                                              will be disposed and replaced.
  249. //
  250. //      Outputs:
  251. //              PIWriteDescriptor *token                Set to NULL.
  252. //
  253. //              procs->descriptor                               Set to descriptor handle.
  254. //
  255. //              returns OSErr                                   noErr or error if one occurred.
  256. //
  257. //-------------------------------------------------------------------------------
  258.  
  259. OSErr   HostCloseWriter(PIDescriptorParameters *procs,
  260.                                                 HandleProcs *hProcs,
  261.                                                 PIWriteDescriptor *token)
  262. {
  263.         OSErr err = noErr; // assume no error
  264.         PIDescriptorHandle h = NULL;
  265.  
  266.         if (procs->descriptor != NULL) // don't need descriptor passed to us
  267.                 hProcs->disposeProc(procs->descriptor); // dispose.
  268.         procs->writeDescriptorProcs->closeWriteDescriptorProc(*token, &h);
  269.         procs->descriptor = h;
  270.  
  271.         // Set recordInfo to default.  Options are: plugInDialogOptional,
  272.         // plugInDialogRequire, plugInDialogNone:
  273.         procs->recordInfo = plugInDialogOptional;
  274.  
  275.         *token = NULL;
  276.  
  277.         return err;
  278.  
  279. } // end HostCloseWriter
  280.