Subversion Repositories filter_foundry

Rev

Rev 277 | Rev 346 | 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-2021 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 <stddef.h>
  22. #include <stdint.h>
  23. #include <assert.h>
  24. #include <time.h>
  25.  
  26. #include "ff.h"
  27. #include "symtab.h"
  28.  
  29. #include "scripting.h"
  30. #include "PIActions.h"
  31. #include "PITerminology.h"
  32.  
  33. #include "compat_string.h"
  34.  
  35. //#define PRINTABLE_HASH_FF16
  36. #define ENABLE_APPLESCRIPT
  37.  
  38. /*
  39. Find a printable 4-character key, remembering (see Photoshop API guide):
  40. - All IDs starting with an uppercase letter are reserved by Adobe.
  41. - All IDs that are all uppercase are reserved by Apple.
  42. - All IDs that are all lowercase are reserved by Apple.
  43. - This leaves all IDs that begin with a lowercase letter and have at least
  44.   one uppercase letter for you and other plug-in developers.
  45. Note: It is questionable if "a!!!" or "A!!!" are also reseved by Apple. We don't risk it.
  46. */
  47. unsigned long printablehash(unsigned long hash) {
  48.         #ifdef PRINTABLE_HASH_FF16
  49.  
  50.         // FilterFoundry version 1.6 hashing by Toby Thain
  51.         // Only accepts upper case at the last character
  52.         // 0       = 'a  A'
  53.         // 6100899 = 'z~~Z'
  54.         unsigned long key = 'a' + (hash % 26);  hash /= 26; // first lower case
  55.         key = (key << 8) | (' ' + (hash % 95)); hash /= 95; // any printable
  56.         key = (key << 8) | (' ' + (hash % 95)); hash /= 95; // any printable
  57.         return  (key << 8) | ('A' + (hash % 26));           // last upper case
  58.  
  59.         #else
  60.  
  61.         // FilterFoundry version 1.7 hashing by Daniel Marschall
  62.         // Accepts upper case at character 2, 3 or 4
  63.         // Spaces are only set the right as padding to make a code shorter
  64.         // 0        = 'aA  '
  65.         // 13530139 = 'zZZZ'
  66.         // The key-space is ~2.22 times larger
  67.         long lowlim;
  68.         long uplim;
  69.         int upperCaseInfo;
  70.         int length;
  71.         int lastThreeCharInfo;
  72.         int firstChar;
  73.         unsigned long key;
  74.         int found;
  75.         int i,j,k;
  76.  
  77.         uplim = 0;
  78.         for (k=1; k<4; k++) {
  79.                 int mask;
  80.                 if (k == 1) mask = 1;//0b1;
  81.                 if (k == 2) mask = 2;//0b11;
  82.                 if (k == 3) mask = 4;//0b111;
  83.                 for (i=1; i<=mask; i++) {
  84.                         // 'k' characters
  85.                         long test = 1;
  86.                         for (j=0; j<k; ++j) {
  87.                                 test *= ((i&(1<<j)) != 0) ? 26 : 94-26;
  88.                         }
  89.                         uplim += test;
  90.                 }
  91.         }
  92.  
  93.         lastThreeCharInfo = hash % uplim; hash /= uplim;
  94.         firstChar = hash%26; hash /= 26;
  95.  
  96.         lowlim = 0;
  97.         uplim = 0;
  98.         found = 0;
  99.         length = -1; // avoid compiler warning
  100.         upperCaseInfo = -1; // avoid compiler warning
  101.         for (k=1; k<4; k++) {
  102.                 int mask;
  103.                 if (k == 1) mask = 1;//0b1;
  104.                 if (k == 2) mask = 2;//0b11;
  105.                 if (k == 3) mask = 4;//0b111;
  106.                 if (!found) for (i=1; i<=mask; i++) {
  107.                         // 'k' characters
  108.                         long test = 1;
  109.                         for (j=0; j<k; ++j) {
  110.                                 test *= ((i&(1<<j)) != 0) ? 26 : 94-26;
  111.                         }
  112.                         uplim += test;
  113.                         if ((lastThreeCharInfo >= lowlim) && (lastThreeCharInfo < uplim)) {
  114.                                 lastThreeCharInfo -= lowlim;
  115.                                 found = 1;
  116.                                 length = k;
  117.                                 upperCaseInfo = i;
  118.                                 break;
  119.                         }
  120.                         lowlim = uplim;
  121.                 }
  122.         }
  123.  
  124.         key = ('a' + firstChar) << 24; // first char is lower-case
  125.         for (i=0; i<length; ++i) {
  126.                 char res;
  127.                 if ((upperCaseInfo&(1<<i)) == 0) {
  128.                         res = '!' + (lastThreeCharInfo % (94-26));
  129.                         if (res >= 'A') res += 26;
  130.                         lastThreeCharInfo /= (94-26);
  131.                 } else {
  132.                         res = 'A' + (lastThreeCharInfo % 26);
  133.                         lastThreeCharInfo /= 26;
  134.                 }
  135.                 key |= res << (8*(i+(4-length-1))); // 2nd, 3rd, 4th char are either upper-case or any printable char
  136.         }
  137.         if (length == 1) {
  138.                 key &= 0xFFFF0000;
  139.                 key |= ' ' << 8;
  140.                 key |= ' ';
  141.         }
  142.         if (length == 2) {
  143.                 key &= 0xFFFFFF00;
  144.                 key |= ' ';
  145.         }
  146.         return key;
  147.  
  148.         #endif
  149. }
  150.  
  151. size_t roundToNext4(size_t x) {
  152.         int pad = 4 - (x % 4);
  153.         if (pad == 0) pad = 4;
  154.         return x + pad;
  155. }
  156.  
  157. size_t fixpipl(PIPropertyList *pipl, size_t origsize, StringPtr title, long *event_id) {
  158.         PIProperty *prop;
  159.         char *p;
  160.         struct hstm_data {
  161.                 /* this structure must be 14+1 bytes long, to match PiPL structure */
  162.                 long version; /* = 0 */
  163.                 long class_id;
  164.                 long event_id;
  165.                 short aete_resid;
  166.                 char scope[1];
  167.         };
  168.         struct hstm_data *hstm;
  169.         int scopelen;
  170.         unsigned long hash;
  171.         size_t realLength;
  172.         size_t roundedLength;
  173.         char* scope;
  174.  
  175.         pipl->count += 3; // 3 more keys in PiPL: name, catg, hstm
  176.  
  177.         p = (char*)pipl + origsize;
  178.         prop = (PIProperty*)p;
  179.  
  180.         /* add Title/Name property key */
  181.  
  182.         prop->vendorID = kPhotoshopSignature;
  183.         prop->propertyKey = PINameProperty;
  184.         prop->propertyID = 0;
  185.         prop->propertyLength = (SPInt32)roundToNext4((size_t)title[0] + 1);
  186.         memset(prop->propertyData, 0x00, prop->propertyLength); // fill padding with 00h bytes (cosmetics)
  187.         PLstrcpy((StringPtr)prop->propertyData, title);
  188.  
  189.         // skip past new property record, and any padding
  190.         p += offsetof(PIProperty, propertyData) + prop->propertyLength;
  191.         prop = (PIProperty*)p;
  192.  
  193.         /* add Category property key */
  194.  
  195.         prop->vendorID = kPhotoshopSignature;
  196.         prop->propertyKey = PICategoryProperty;
  197.         prop->propertyID = 0;
  198.  
  199.         prop->propertyLength = (SPInt32)roundToNext4((size_t)gdata->parm.category[0] + 1);
  200.         memset(prop->propertyData, 0x00, prop->propertyLength); // fill padding with 00h bytes (cosmetics)
  201.         PLstrcpy((StringPtr)prop->propertyData, gdata->parm.category);
  202.  
  203.         p += offsetof(PIProperty, propertyData) + prop->propertyLength;
  204.         prop = (PIProperty*)p;
  205.  
  206.         /* add HasTerminology property key */
  207.  
  208.         hstm = (struct hstm_data*)prop->propertyData;
  209.  
  210.         scope = (char*)malloc(0x300);
  211.         if (!scope) return -1;
  212.         sprintf(scope, "%s %s",
  213.             INPLACEP2CSTR(gdata->parm.category),
  214.             INPLACEP2CSTR(title));
  215.  
  216.         #ifdef ENABLE_APPLESCRIPT
  217.         // If the uniqueString/scope is set, the plugin will only communicate with Photoshop.
  218.         // Otherwise it can be accessed with AppleScript, but the AETE keys need to be unique then.
  219.         // This is achieved with getAeteKey().
  220.         scopelen = 0;
  221.         hstm->scope[0] = '\0';
  222.         #else
  223.         // Construct scope string by concatenating Category and Title - hopefully unique!
  224.         // Note: In doresources() we malloc'ed 300h additional bytes,
  225.         // and in the build dialog, title and category are size-limited,
  226.         // so we can write here without malloc.
  227.         scopelen = strlen(scope);
  228.         memcpy(&hstm->scope, scope, scopelen);
  229.         #endif
  230.  
  231.         /* make up a new event ID for this aete, based on printable base-95 hash of scope */
  232.         hash = djb2(scope);
  233.         *event_id = printablehash(hash); /* this is used by aete_generate() later... */
  234.  
  235.         prop->vendorID = kPhotoshopSignature;
  236.         prop->propertyKey = PIHasTerminologyProperty;
  237.         prop->propertyID = 0;
  238.  
  239.         realLength = offsetof(struct hstm_data, scope) + scopelen + 1/*null-term*/;
  240.         roundedLength = roundToNext4(realLength);
  241.         prop->propertyLength = (SPInt32)roundedLength;
  242.         memset(prop->propertyData + realLength, 0x00, roundedLength - realLength); // fill padding with 00h bytes (cosmetics)
  243.  
  244.         hstm->version = 0;
  245.         hstm->class_id = plugInClassID;
  246.         hstm->event_id = *event_id;
  247.         hstm->aete_resid = AETE_ID;
  248.  
  249.         p += offsetof(PIProperty, propertyData) + prop->propertyLength;
  250.  
  251.         return p - (char*)pipl;  // figure how many bytes were added
  252. }
  253.  
  254. void _aete_write_byte(void** aeteptr, uint8_t val) {
  255.         uint8_t* tmp = *((uint8_t**)aeteptr);
  256.         *tmp = val;
  257.         *aeteptr = (void*)((unsigned char*)tmp + 1);
  258. }
  259. #define AETE_WRITE_BYTE(i) _aete_write_byte(&aeteptr, (i));
  260.  
  261. void _aete_write_word(void** aeteptr, uint16_t val) {
  262.         uint16_t* tmp = *((uint16_t**)aeteptr);
  263.         *tmp = val;
  264.         *aeteptr = (void*)((unsigned char*)tmp + 2);
  265. }
  266. #define AETE_WRITE_WORD(i) _aete_write_word(&aeteptr, (i));
  267.  
  268. void _aete_write_dword(void** aeteptr, uint32_t val) {
  269.         uint32_t* tmp = *((uint32_t**)aeteptr);
  270.         *tmp = val;
  271.         *aeteptr = (void*)((unsigned char*)tmp + 4);
  272. }
  273. #define AETE_WRITE_DWORD(i) _aete_write_dword(&aeteptr, (i));
  274.  
  275. void _aete_write_c2pstr(void** aeteptr, char* str) {
  276.         char* tmp;
  277.  
  278.         assert(strlen(str) <= 255);
  279.  
  280.         _aete_write_byte(aeteptr, (uint8_t)strlen(str));
  281.  
  282.         tmp = *((char**)aeteptr);
  283.         strcpy(tmp, str);
  284.         *aeteptr = (void*)((unsigned char*)tmp + strlen(str));
  285. }
  286. #define AETE_WRITE_C2PSTR(s) _aete_write_c2pstr(&aeteptr, (s));
  287.  
  288. void _aete_write_p2pstr(void** aeteptr, char* str) {
  289.     char* tmp;
  290.  
  291.     if (strlen(str) == 0) {
  292.         _aete_write_byte(aeteptr, 0);
  293.     } else {
  294.         tmp = *((char**)aeteptr);
  295.         strcpy(tmp, str);
  296.         *aeteptr = (void*)((unsigned char*)tmp + strlen(str));
  297.     }
  298. }
  299. #define AETE_WRITE_P2PSTR(s) _aete_write_p2pstr(&aeteptr, (s));
  300.  
  301. void _aete_align_word(void** aeteptr) {
  302.         #ifdef MAC_ENV
  303.         unsigned char* tmp = *((unsigned char**)aeteptr);
  304.         tmp += (intptr_t)tmp & 1;
  305.         *aeteptr = (void*)tmp;
  306.         #endif
  307. }
  308. #define AETE_ALIGN_WORD() _aete_align_word(&aeteptr);
  309.  
  310. void* _aete_property(void* aeteptr, PARM_T *pparm, int ctlidx, int mapidx, OSType key) {
  311.         char tmp[256];
  312.  
  313.         if (pparm->ctl_used[ctlidx] || pparm->map_used[mapidx]) {
  314.                 if (pparm->map_used[mapidx]) {
  315.                         if (ctlidx & 1) {
  316.                                 sprintf(tmp, "... %s", (char*)pparm->map[mapidx]);
  317.                         } else {
  318.                                 sprintf(tmp, "%s ...", (char*)pparm->map[mapidx]);
  319.                         }
  320.                         AETE_WRITE_C2PSTR(tmp);
  321.                 } else {
  322.                         AETE_WRITE_P2PSTR((char*)pparm->ctl[ctlidx]);
  323.                 }
  324.                 AETE_ALIGN_WORD();
  325.                 AETE_WRITE_DWORD(key);
  326.                 AETE_WRITE_DWORD(typeSInt32);
  327.                 AETE_WRITE_C2PSTR(_strdup(""));
  328.                 AETE_ALIGN_WORD();
  329.                 AETE_WRITE_WORD(0x8000); /* FLAGS_1_OPT_PARAM / flagsOptionalSingleParameter */
  330.         }
  331.  
  332.         return aeteptr;
  333. }
  334.  
  335. size_t aete_generate(void* aeteptr, PARM_T *pparm, long event_id) {
  336.         int numprops;
  337.         void *beginptr = aeteptr;
  338.  
  339.         // Attention!
  340.         // - On some systems (e.g. ARM based CPUs) this will cause an unaligned memory access exception.
  341.         //   For X86, memory access just becomes slower.
  342.         // - If you change something here, please also change it in Scripting.rc (Windows) and scripting.r (Mac OS)
  343.  
  344.         // Note:
  345.         // - The 'aete' resource for Mac OS has word alignments after strings (but not if the next element is also a string)
  346.         //   see https://developer.apple.com/library/archive/documentation/mac/pdf/Interapplication_Communication/AE_Term_Resources.pdf page 8-9
  347.  
  348.         #ifdef WIN_ENV
  349.         AETE_WRITE_WORD(0x0001); /* Reserved (for Photoshop) */
  350.         #endif
  351.  
  352.         AETE_WRITE_BYTE(0x01); /* aete version */
  353.         AETE_WRITE_BYTE(0x00); /* aete version */
  354.         AETE_WRITE_WORD(english); /* language specifiers */
  355.         AETE_WRITE_WORD(roman);
  356.         AETE_WRITE_WORD(1); /* 1 suite */
  357.         {
  358.                 AETE_WRITE_P2PSTR((char*)pparm->author); /* vendor suite name */
  359.                 AETE_WRITE_C2PSTR(_strdup("")); /* optional description */
  360.                 AETE_ALIGN_WORD();
  361.                 AETE_WRITE_DWORD(plugInSuiteID); /* suite ID */
  362.                 AETE_WRITE_WORD(1); /* suite code, must be 1. Attention: Filters like 'Pointillize' have set this to 0! */
  363.                 AETE_WRITE_WORD(1); /* suite level, must be 1. Attention: Filters like 'Pointillize' have set this to 0! */
  364.                 AETE_WRITE_WORD(1); /* 1 event (structure for filters) */
  365.                 {
  366.                         AETE_WRITE_P2PSTR((char*)pparm->title); /* event name */
  367.                         AETE_WRITE_C2PSTR(_strdup("")); /* event description */
  368.                         AETE_ALIGN_WORD();
  369.                         AETE_WRITE_DWORD(plugInClassID); /* event class */
  370.                         AETE_WRITE_DWORD(/*plugInEventID*/event_id); /* event ID */
  371.                         /* NO_REPLY: */
  372.                         AETE_WRITE_DWORD(noReply); /* noReply='null' */
  373.                         AETE_WRITE_C2PSTR(_strdup("")); /* reply description */
  374.                         AETE_ALIGN_WORD();
  375.                         AETE_WRITE_WORD(0);
  376.                         /* IMAGE_DIRECT_PARAM: */
  377.                         AETE_WRITE_DWORD(typeImageReference); /* typeImageReference='#ImR' */
  378.                         AETE_WRITE_C2PSTR(_strdup("")); /* direct parm description */
  379.                         AETE_ALIGN_WORD();
  380.                         AETE_WRITE_WORD(0xB000);
  381.  
  382.                         numprops = 0;
  383.                         if (pparm->ctl_used[0] || pparm->map_used[0]) numprops++;
  384.                         if (pparm->ctl_used[1] || pparm->map_used[0]) numprops++;
  385.                         if (pparm->ctl_used[2] || pparm->map_used[1]) numprops++;
  386.                         if (pparm->ctl_used[3] || pparm->map_used[1]) numprops++;
  387.                         if (pparm->ctl_used[4] || pparm->map_used[2]) numprops++;
  388.                         if (pparm->ctl_used[5] || pparm->map_used[2]) numprops++;
  389.                         if (pparm->ctl_used[6] || pparm->map_used[3]) numprops++;
  390.                         if (pparm->ctl_used[7] || pparm->map_used[3]) numprops++;
  391.                         AETE_WRITE_WORD(numprops);
  392.                         {
  393.                                 // Standalone filters don't need RGBA expressions
  394.                                 /*
  395.                                 AETE_WRITE_C2PSTR(_strdup("R"));
  396.                                 AETE_ALIGN_WORD();
  397.                                 AETE_WRITE_DWORD(PARAM_R_KEY);
  398.                                 AETE_WRITE_DWORD(typeText);
  399.                                 AETE_WRITE_C2PSTR(_strdup("R channel expression"));
  400.                                 AETE_ALIGN_WORD();
  401.                                 AETE_WRITE_WORD(0x8000);
  402.  
  403.                                 AETE_WRITE_C2PSTR(_strdup("G"));
  404.                                 AETE_ALIGN_WORD();
  405.                                 AETE_WRITE_DWORD(PARAM_G_KEY);
  406.                                 AETE_WRITE_DWORD(typeText);
  407.                                 AETE_WRITE_C2PSTR(_strdup("G channel expression"));
  408.                                 AETE_ALIGN_WORD();
  409.                                 AETE_WRITE_WORD(0x8000);
  410.  
  411.                                 AETE_WRITE_C2PSTR(_strdup("B"));
  412.                                 AETE_ALIGN_WORD();
  413.                                 AETE_WRITE_DWORD(PARAM_B_KEY);
  414.                                 AETE_WRITE_DWORD(typeText);
  415.                                 AETE_WRITE_C2PSTR(_strdup("B channel expression"));
  416.                                 AETE_ALIGN_WORD();
  417.                                 AETE_WRITE_WORD(0x8000);
  418.  
  419.                                 AETE_WRITE_C2PSTR(_strdup("A"));
  420.                                 AETE_ALIGN_WORD();
  421.                                 AETE_WRITE_DWORD(PARAM_A_KEY);
  422.                                 AETE_WRITE_DWORD(typeText);
  423.                                 AETE_WRITE_C2PSTR(_strdup("A channel expression"));
  424.                                 AETE_ALIGN_WORD();
  425.                                 AETE_WRITE_WORD(0x8000);
  426.                                 */
  427.  
  428.                                 aeteptr = _aete_property(aeteptr, pparm, 0, 0, getAeteKey('0', pparm));
  429.                                 aeteptr = _aete_property(aeteptr, pparm, 1, 0, getAeteKey('1', pparm));
  430.                                 aeteptr = _aete_property(aeteptr, pparm, 2, 1, getAeteKey('2', pparm));
  431.                                 aeteptr = _aete_property(aeteptr, pparm, 3, 1, getAeteKey('3', pparm));
  432.                                 aeteptr = _aete_property(aeteptr, pparm, 4, 2, getAeteKey('4', pparm));
  433.                                 aeteptr = _aete_property(aeteptr, pparm, 5, 2, getAeteKey('5', pparm));
  434.                                 aeteptr = _aete_property(aeteptr, pparm, 6, 3, getAeteKey('6', pparm));
  435.                                 aeteptr = _aete_property(aeteptr, pparm, 7, 3, getAeteKey('7', pparm));
  436.                         }
  437.                 }
  438.  
  439.                 /* non-filter plug-in class here */
  440.                 AETE_WRITE_WORD(0); /* 0 classes */
  441.                 {}
  442.                 AETE_WRITE_WORD(0); /* 0 comparison ops (not supported) */
  443.                 {}
  444.                 AETE_WRITE_WORD(0); /* 0 enumerations */
  445.                 {}
  446.         }
  447.         AETE_WRITE_DWORD(0); /* padding (FIXME: do we need that? Adobe's Windows filters don't) */
  448.  
  449.         return (unsigned char*)aeteptr - (unsigned char*)beginptr; // length of stuff written
  450. }
  451.