Subversion Repositories filter_foundry

Rev

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