Subversion Repositories filter_foundry

Rev

Rev 192 | Rev 203 | 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. //#include <stdio.h>
  22. //#include <sound.h>
  23.  
  24. #include "ff.h"
  25.  
  26. #include "node.h"
  27. #include "funcs.h"
  28. #include "y.tab.h"
  29. #include "scripting.h"
  30. #include <math.h>
  31.  
  32. struct node *tree[4];
  33. char *err[4];
  34. int errpos[4],errstart[4],nplanes,cnvused,chunksize,toprow;
  35. value_type slider[8],cell[NUM_CELLS],map[4][0x100];
  36. char *expr[4];
  37. // long maxSpace;
  38. globals_t *gdata;
  39. FilterRecordPtr gpb;
  40.  
  41. #ifdef MAC_ENV
  42.         #define hDllInstance NULL /* fake this Windows-only global */
  43. #endif
  44.  
  45. #ifdef WIN_ENV
  46. #include "manifest.h"
  47. #endif
  48.  
  49. extern struct sym_rec predefs[];
  50. extern int nplanes,varused[];
  51.  
  52. int checkandinitparams(Handle params);
  53.  
  54. // MPW MrC requires prototype
  55. DLLEXPORT MACPASCAL
  56. void ENTRYPOINT(short selector,FilterRecordPtr pb,intptr_t *data,short *result);
  57.  
  58. DLLEXPORT MACPASCAL
  59. void ENTRYPOINT(short selector, FilterRecordPtr pb, intptr_t *data, short *result){
  60.         static Boolean wantdialog = false;
  61.         OSErr e = noErr;
  62.         char *reason;
  63.  
  64. #ifdef WIN_ENV
  65.         // For Windows, we use an activation context to enforce that our Manifest resource will
  66.         // be used. This allows us to use Visual Styles, even if the host application does not
  67.         // support it.
  68.         ManifestActivationCtx manifestVars;
  69.         BOOL activationContextUsed;
  70.  
  71.         activationContextUsed = ActivateManifest((HMODULE)hDllInstance, 1, &manifestVars);
  72. #endif
  73.  
  74.         if(selector != filterSelectorAbout && !*data){
  75.                 BufferID tempId;
  76.                 if( (*result = PS_BUFFER_ALLOC(sizeof(globals_t), &tempId)) ) {
  77. #ifdef WIN_ENV
  78.                         if (activationContextUsed) DeactivateManifest(&manifestVars);
  79. #endif
  80.                         return;
  81.                 }
  82.                 *data = (intptr_t)PS_BUFFER_LOCK(tempId, true);
  83.                 gdata = (globals_t*)*data;
  84.                 gdata->standalone = gdata->parmloaded = false;
  85.         }else
  86.                 gdata = (globals_t*)*data;
  87.  
  88.         EnterCodeResource();
  89.  
  90.         gpb = pb;
  91.  
  92.         nplanes = MIN(pb->planes,4);
  93.  
  94.         switch (selector){
  95.         case filterSelectorAbout:
  96.                 if(gdata && !gdata->parmloaded)
  97.                         gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1);
  98.                 DoAbout((AboutRecordPtr)pb);
  99.                 break;
  100.         case filterSelectorParameters:
  101.                 wantdialog = true;
  102.                 break;
  103.         case filterSelectorPrepare:
  104.                 DoPrepare(pb);
  105.                 init_symtab(predefs); // ready for parser calls
  106.                 init_trigtab();
  107.                 break;
  108.         case filterSelectorStart:
  109.                 /* initialise the parameter handle that Photoshop keeps for us */
  110.                 if(!pb->parameters)
  111.                         pb->parameters = PINEWHANDLE(1); // don't set initial size to 0, since some hosts (e.g. GIMP/PSPI) are incompatible with that.
  112.  
  113.                 wantdialog |= checkandinitparams(pb->parameters);
  114.  
  115.                 /* wantdialog = false means that we never got a Parameters call, so we're not supposed to ask user */
  116.                 if( wantdialog && (!gdata->standalone || gdata->parm.popDialog) ){
  117.                         if( maindialog(pb) ){
  118.                                 if (!host_preserves_parameters()) {
  119.                                         /* Workaround for GIMP/PSPI, to avoid that formulas vanish when you re-open the main window.
  120.                                            The reason is a bug in PSPI: The host should preserve the value of pb->parameters, which PSPI does not do.
  121.                                            Also, all global variables are unloaded, so the plugin cannot preserve any data.
  122.                                            Workaround in FF 1.7: If the host GIMP is detected, then a special mode will be activated.
  123.                                            This mode saves the filter data into a temporary file "FilterFoundry.afs" and loads it
  124.                                            when the window is opened again. */
  125.                                         // Workaround: Save settings in "FilterFoundry.afs" if the host does not preserve pb->parameters
  126.                                         char outfilename[255];
  127.                                         char* tempdir;
  128.                                         StandardFileReply sfr;
  129.                                         sfr.sfGood = true;
  130.                                         sfr.sfReplacing = true;
  131.                                         sfr.sfType = PS_FILTER_FILETYPE;
  132.  
  133.                                         tempdir = getenv("TMP");
  134.                                         #ifdef WIN_ENV
  135.                                         if (strlen(tempdir) > 0) strcat(tempdir, "\\");
  136.                                         #else
  137.                                         if (strlen(tempdir) > 0) strcat(tempdir, "/");
  138.                                         #endif
  139.                                         sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
  140.  
  141.                                         myc2pstrcpy(sfr.sfFile.name, outfilename);
  142.                                         #ifdef WIN_ENV
  143.                                         sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
  144.                                         #endif
  145.                                         sfr.sfScript = 0; // FIXME: is that ok?
  146.                                         savefile(&sfr);
  147.                                 }
  148.  
  149.                                 /* update stored parameters from new user settings */
  150.                                 saveparams(pb->parameters);
  151.                         }else
  152.                                 e = userCanceledErr;
  153.                 }
  154.                 wantdialog = false;
  155.  
  156.                 if(!e){
  157.                         if(setup(pb)){
  158.                                 DoStart(pb);
  159.                         }else{
  160.                                 SYSBEEP(1);
  161.                                 e = filterBadParameters;
  162.                         }
  163.                 }
  164.                 break;
  165.         case filterSelectorContinue:
  166.                 e = DoContinue(pb);
  167.                 break;
  168.         case filterSelectorFinish:
  169.                 DoFinish(pb);
  170.                 break;
  171.         default:
  172.                 e = filterBadParameters;
  173.         }
  174.  
  175.         *result = e;
  176.  
  177.         ExitCodeResource();
  178.  
  179. #ifdef WIN_ENV
  180.         if (activationContextUsed) DeactivateManifest(&manifestVars);
  181. #endif
  182. }
  183.  
  184. int checkandinitparams(Handle params){
  185.         char *reasonstr,*reason;
  186.         int i,f,showdialog;
  187.  
  188.         if (!host_preserves_parameters()) {
  189.                 // Workaround: Load settings in "FilterFoundry.afs" if host does not preserve pb->parameters
  190.                 char outfilename[255];
  191.                 char* tempdir;
  192.                 StandardFileReply sfr;
  193.                 sfr.sfGood = true;
  194.                 sfr.sfReplacing = true;
  195.                 sfr.sfType = PS_FILTER_FILETYPE;
  196.  
  197.                 tempdir = getenv("TMP");
  198.                 #ifdef WIN_ENV
  199.                 if (strlen(tempdir) > 0) strcat(tempdir, "\\");
  200.                 #else
  201.                 if (strlen(tempdir) > 0) strcat(tempdir, "/");
  202.                 #endif
  203.                 sprintf(outfilename, "%sFilterFoundry.afs", tempdir);
  204.  
  205.                 myc2pstrcpy(sfr.sfFile.name, outfilename);
  206.                 #ifdef WIN_ENV
  207.                 sfr.nFileExtension = (WORD)(strlen(outfilename) - strlen(".afs"));
  208.                 #endif
  209.                 sfr.sfScript = 0; // FIXME: is that ok?
  210.                 if (loadfile(&sfr, &reason)) return true;
  211.         }
  212.  
  213.         if( (f = !(params && readparams(params,false,&reasonstr))) ){
  214.                 /* either the parameter handle was uninitialised,
  215.                    or the parameter data couldn't be read; set default values */
  216.  
  217.                 // see if saved parameters exist
  218.                 gdata->standalone = gdata->parmloaded = readPARMresource((HMODULE)hDllInstance,&reason,1);
  219.  
  220.                 if(!gdata->standalone){
  221.                         // no saved settings (not standalone)
  222.                         for(i = 0; i < 8; ++i)
  223.                                 slider[i] = i*10+100;
  224.                         for(i = 0; i < 4; ++i)
  225.                                 if(expr[i])
  226.                                         free(expr[i]);
  227.                         if(gpb->imageMode == plugInModeRGBColor){
  228.                                 expr[0] = my_strdup("r");
  229.                                 expr[1] = my_strdup("g");
  230.                                 expr[2] = my_strdup("b");
  231.                                 expr[3] = my_strdup("a");
  232.                         }else{
  233.                                 expr[0] = my_strdup("c");
  234.                                 expr[1] = my_strdup("c");
  235.                                 expr[2] = my_strdup("c");
  236.                                 expr[3] = my_strdup("c");
  237.                         }
  238.                 }
  239.         }
  240.  
  241.         // let scripting system change parameters, if we're scripted;
  242.         // user may want to force display of dialog during scripting playback
  243.         showdialog = ReadScriptParamsOnRead();
  244.  
  245.         saveparams(params);
  246.         return f || showdialog;
  247. }
  248.  
  249. Boolean host_preserves_parameters() {
  250.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  251.         if (gpb->hostSig == HOSTSIG_IRFANVIEW) return false;
  252.  
  253.         /*
  254.         char x[100];
  255.         sprintf(x, "Host Signature: %u", gpb->hostSig);
  256.         simplealert(x);
  257.         */
  258.  
  259.         // We just assume the other hosts preserve the parameters
  260.         return true;
  261. }
  262.  
  263. int64_t maxspace(){
  264.         if (gpb->maxSpace64 != 0) {
  265.                 // If this is non-zero, the host either support 64-bit OR the host ignored the rule "set reserved fields to 0".
  266.                 uint64_t maxSpace64 = gpb->maxSpace64;
  267.  
  268.                 /*
  269.                 char x[100];
  270.                 sprintf(x, "maxSpace64: %lld", maxSpace64);
  271.                 simplealert(x);
  272.                 */
  273.  
  274.                 // Note: IrfanView currently does not support maxSpace64, so we don't handle HOSTSIG_IRFANVIEW
  275.                 return maxSpace64;
  276.         } else {
  277.                 // Note: If maxSpace gets converted from Int32 to unsigned int, we can reach up to 4 GB RAM. However, after this, there will be a wrap to 0 GB again.
  278.                 unsigned int maxSpace32 = (unsigned int) gpb->maxSpace;
  279.                 uint64_t maxSpace64 = maxSpace32;
  280.  
  281.                 /*
  282.                 char x[100];
  283.                 sprintf(x, "maxSpace32: %lld", maxSpace64);
  284.                 simplealert(x);
  285.                 */
  286.  
  287.                 if (gpb->hostSig == HOSTSIG_IRFANVIEW) maxSpace64 *= 1024; // IrfanView is giving Kilobytes instead of Bytes
  288.                 // TODO: Serif PhotoPlus also gives Kilobytes instead of bytes. But since it uses not a unique host signature, there is nothing we can do???
  289.                 return maxSpace64;
  290.         }
  291. }
  292.  
  293. Boolean maxspace_available() {
  294.         // GIMP sets MaxSpace to hardcoded 100 MB
  295.         if (gpb->hostSig == HOSTSIG_GIMP) return false;
  296.  
  297.         return true;
  298. }
  299.  
  300. void DoPrepare(FilterRecordPtr pb){
  301.         int i;
  302.  
  303.         for(i = 4; i--;){
  304.                 if(expr[i]||tree[i]) DBG("expr[] or tree[] non-NULL in Prepare!");
  305.                 expr[i] = NULL;
  306.                 tree[i] = NULL;
  307.         }
  308.  
  309.         // Commented out by DM, 18 Dec 2018:
  310.         // This code did not work on systems with 8 GB RAM:
  311.         /*
  312.         long space = (pb->maxSpace*9)/10; // don't ask for more than 90% of available memory
  313.  
  314.         maxSpace = 512L<<10; // this is a wild guess, actually
  315.         if(maxSpace > space)
  316.                 maxSpace = space;
  317.         pb->maxSpace = maxSpace;
  318.         */
  319.  
  320.         // New variant:
  321.         if (maxspace_available()) {
  322.                 pb->maxSpace = (int32)ceil((maxspace()/10.)*9); // don't ask for more than 90% of available memory
  323.                 // FIXME: Also maxSpace64
  324.         }
  325. }
  326.  
  327. void RequestNext(FilterRecordPtr pb,long toprow){
  328.         /* Request next block of the image */
  329.  
  330.         pb->inLoPlane = pb->outLoPlane = 0;
  331.         pb->inHiPlane = pb->outHiPlane = nplanes-1;
  332.  
  333.         // if any of the formulae involve random access to image pixels,
  334.         // ask for the entire image
  335.         if(needall){
  336.                 SETRECT(pb->inRect,0,0,pb->imageSize.h,pb->imageSize.v);
  337.         }else{
  338.                 // TODO: This does not work with GIMP. So, if we are using GIMP, we should
  339.                 //       somehow always use "needall=true", and/or find out why this doesn't work
  340.                 //       with GIMP.
  341.  
  342.                 // otherwise, process the filtered area, by chunksize parts
  343.                 pb->inRect.left = pb->filterRect.left;
  344.                 pb->inRect.right = pb->filterRect.right;
  345.                 pb->inRect.top = (int16)toprow;
  346.                 pb->inRect.bottom = (int16)MIN(toprow + chunksize,pb->filterRect.bottom);
  347.  
  348.                 if(cnvused){
  349.                         // cnv() needs one extra pixel in each direction
  350.                         if(pb->inRect.left > 0)
  351.                                 --pb->inRect.left;
  352.                         if(pb->inRect.right < pb->imageSize.h)
  353.                                 ++pb->inRect.right;
  354.                         if(pb->inRect.top > 0)
  355.                                 --pb->inRect.top;
  356.                         if(pb->inRect.bottom < pb->imageSize.v)
  357.                                 ++pb->inRect.bottom;
  358.                 }
  359.         }
  360.         pb->outRect = pb->filterRect;
  361. /*
  362. {char s[0x100];sprintf(s,"RequestNext needall=%d inRect=(%d,%d,%d,%d) filterRect=(%d,%d,%d,%d)",
  363.         needall,
  364.         pb->inRect.left,pb->inRect.top,pb->inRect.right,pb->inRect.bottom,
  365.         pb->filterRect.left,pb->filterRect.top,pb->filterRect.right,pb->filterRect.bottom);dbg(s);}
  366. */
  367. }
  368.  
  369. void DoStart(FilterRecordPtr pb){
  370. //dbg("DoStart");
  371.         /* if src() or rad() functions are used, random access to the image data is required,
  372.            so we must request the entire image in a single chunk. */
  373.         chunksize = needall ? (pb->filterRect.bottom - pb->filterRect.top) : CHUNK_ROWS;
  374.         toprow = pb->filterRect.top;
  375.         RequestNext(pb,toprow);
  376. }
  377.  
  378. OSErr DoContinue(FilterRecordPtr pb){
  379.         OSErr e = noErr;
  380.         Rect fr;
  381.         long outoffset;
  382.  
  383.         if(needall)
  384.                 fr = pb->filterRect;  // filter whole selection at once
  385.         else if(cnvused){
  386.                 // we've requested one pixel extra all around
  387.                 // (see RequestNext()), just for access purposes. But filter
  388.                 // original selection only.
  389.                 fr.left = pb->filterRect.left;
  390.                 fr.right = pb->filterRect.right;
  391.                 fr.top = toprow;
  392.                 fr.bottom = MIN(toprow + chunksize,pb->filterRect.bottom);
  393.         }else  // filter whatever portion we've been given
  394.                 fr = pb->inRect;
  395.  
  396.         outoffset = (long)pb->outRowBytes*(fr.top - pb->outRect.top)
  397.                                 + (long)nplanes*(fr.left - pb->outRect.left);
  398.  
  399.         if(!(e = process_scaled(pb, true, &fr, &fr,
  400.                                 (Ptr)pb->outData+outoffset, pb->outRowBytes, 1.)))
  401.         {
  402.                 toprow += chunksize;
  403.                 if(toprow < pb->filterRect.bottom)
  404.                         RequestNext(pb,toprow);
  405.                 else{
  406.                         SETRECT(pb->inRect,0,0,0,0);
  407.                         pb->outRect = pb->maskRect = pb->inRect;
  408.                 }
  409.         }
  410.         return e;
  411. }
  412.  
  413. void DoFinish(FilterRecordPtr pb){
  414.         int i;
  415.  
  416.         WriteScriptParamsOnRead();
  417.  
  418.         for(i = 4; i--;){
  419.                 freetree(tree[i]);
  420.                 if(expr[i]) free(expr[i]);
  421.         }
  422. }
  423.