Subversion Repositories filter_foundry

Rev

Rev 206 | Rev 238 | 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 "ff.h"
  22.  
  23. #include "file_compat.h"
  24.  
  25. #ifdef MAC_ENV
  26. #include <Endian.h>
  27. #else
  28. int EndianS32_LtoN(int num) {
  29.         return ((num>>24)&0xff) | // move byte 3 to byte 0
  30.                ((num<<8)&0xff0000) | // move byte 1 to byte 2
  31.                ((num>>8)&0xff00) | // move byte 2 to byte 1
  32.                ((num<<24)&0xff000000); // byte 0 to byte 3
  33. }
  34. #endif
  35.  
  36. enum{
  37.         BUFSIZE = 4L<<10,
  38.         MAXLINE = 0x200,
  39. };
  40.  
  41. Boolean readparams(Handle h,Boolean alerts,char **reason){
  42.         Boolean res = false;
  43.         char linebuf[MAXLINE+1],curexpr[MAXEXPR+1],*p,*dataend,*q;
  44.         int c,linecnt,lineptr,exprcnt;
  45.  
  46.         if(!h){
  47.                 *reason = _strdup("readparams: Null parameter handle.");
  48.                 return false;
  49.         }
  50.  
  51.         p = PILOCKHANDLE(h,false);
  52.         dataend = p + PIGETHANDLESIZE(h);
  53.  
  54.         q = curexpr;
  55.         linecnt = exprcnt = lineptr = 0;
  56.  
  57.         *reason = _strdup("File was too short.");
  58.         while(p < dataend){
  59.  
  60.                 c = *p++;
  61.  
  62.                 if(c==CR || c==LF){ /* detected end of line */
  63.  
  64.                         /* look ahead to see if we need to skip a line feed (DOS EOL convention) */
  65.                         if(c == CR && *p == LF && p < dataend)
  66.                                 ++p;
  67.  
  68.                         linebuf[lineptr] = 0; /* add terminating NUL to line buffer */
  69.  
  70.                         /* process complete line */
  71.                         if(linecnt==0){
  72.                                 if(strcmp(linebuf,"%RGB-1.0")){
  73.                                         if(alerts)
  74.                                                 *reason = _strdup("This doesn't look like a Filter Factory file (first line is not \"%RGB-1.0\").");
  75.                                         break;
  76.                                 }
  77.                         }else if(linecnt<=8){
  78.                                 slider[linecnt-1] = atoi(linebuf);
  79.                         }else{
  80.                                 if(lineptr){
  81.                                         /* it's not an empty line; append it to current expr string */
  82.                                         if( q+lineptr > curexpr+MAXEXPR ){
  83.                                                 *reason = _strdup("Found an expression longer than 1024 characters.");
  84.                                                 break;
  85.                                         }
  86.                                         q = cat(q,linebuf);
  87.                                 }else{
  88.                                         /* it's an empty line: we've completed the expr string */
  89.                                         if(expr[exprcnt])
  90.                                                 free(expr[exprcnt]);
  91.                                         *q = 0;
  92.                                         if(!(expr[exprcnt] = my_strdup(curexpr))){
  93.                                                 *reason = _strdup("Could not get memory for expression.");
  94.                                                 break;
  95.                                         }
  96.  
  97.                                         if(++exprcnt == 4){
  98.                                                 res = true;
  99.                                                 break; /* got everything we want */
  100.                                         }
  101.  
  102.                                         q = curexpr; /* empty current expr, ready for next one */
  103.                                 }
  104.                         }
  105.  
  106.                         ++linecnt;
  107.                         lineptr = 0;
  108.                 }else{
  109.                         /* store character */
  110.                         if(c=='\\'){ /* escape sequence */
  111.                                 if(p < dataend){
  112.                                         c = *p++;
  113.                                         switch(c){
  114.                                         case 'r':
  115. #if WIN_ENV
  116.                                             c = CR;
  117.                                             if (lineptr < MAXLINE)
  118.                                                 linebuf[lineptr++] = c;
  119.                                             c = LF;
  120. #else
  121.                                             c = CR;
  122. #endif
  123.                                             break;
  124.                                         case '\\': break;
  125.                                         default:
  126.                                                 if(alerts) alertuser(_strdup("Warning:"),_strdup("Unknown escape sequence in input."));
  127.                                         }
  128.                                 }//else if(alerts) alertuser(_strdup("Warning:"),_strdup("truncated escape sequence ends input"));
  129.                         }
  130.  
  131.                         if(lineptr < MAXLINE)
  132.                                 linebuf[lineptr++] = c;
  133.                 }
  134.         }
  135.  
  136.         PIUNLOCKHANDLE(h);
  137.  
  138.         return res;
  139. }
  140.  
  141. void convert_premiere_to_photoshop(PARM_T* photoshop, PARM_T_PREMIERE* premiere) {
  142.         int i;
  143.  
  144.         photoshop->cbSize = sizeof(PARM_T);
  145.         photoshop->standalone = premiere->standalone;
  146.         for (i=0;i<8;++i)
  147.           photoshop->val[i] = premiere->val[i];
  148.         photoshop->popDialog = premiere->popDialog;
  149.         photoshop->unknown1 = premiere->unknown1;
  150.         photoshop->unknown2 = premiere->unknown2;
  151.         photoshop->unknown3 = premiere->unknown3;
  152.         for (i=0;i<4;++i)
  153.           photoshop->map_used[i] = premiere->map_used[i];
  154.         for (i=0;i<8;++i)
  155.           photoshop->ctl_used[i] = premiere->ctl_used[i];
  156.         sprintf((char*)photoshop->category, "Filter Factory"); // Premiere plugins do not have a category attribute
  157.         photoshop->iProtected = 0; // Premiere plugins do not have a protect flag
  158.         memcpy((void*)photoshop->title, (void*)premiere->title, sizeof(photoshop->title));
  159.         memcpy((void*)photoshop->copyright, (void*)premiere->copyright, sizeof(photoshop->copyright));
  160.         memcpy((void*)photoshop->author, (void*)premiere->author, sizeof(photoshop->author));
  161.         for (i=0;i<4;++i)
  162.           memcpy((void*)photoshop->map[i], (void*)premiere->map[i], sizeof(photoshop->map[i]));
  163.         for (i=0;i<8;++i)
  164.           memcpy((void*)photoshop->ctl[i], (void*)premiere->ctl[i], sizeof(photoshop->ctl[i]));
  165.  
  166.         if (premiere->singleExpression) {
  167.                 memcpy((void*)photoshop->formula[0], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
  168.                 memcpy((void*)photoshop->formula[1], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
  169.                 memcpy((void*)photoshop->formula[2], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
  170.                 memcpy((void*)photoshop->formula[3], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
  171.         } else {
  172.                 memcpy((void*)photoshop->formula[0], (void*)premiere->formula[2], sizeof(photoshop->formula[2]));
  173.                 memcpy((void*)photoshop->formula[1], (void*)premiere->formula[1], sizeof(photoshop->formula[1]));
  174.                 memcpy((void*)photoshop->formula[2], (void*)premiere->formula[0], sizeof(photoshop->formula[0]));
  175.                 memcpy((void*)photoshop->formula[3], (void*)premiere->formula[3], sizeof(photoshop->formula[3]));
  176.         }
  177. }
  178.  
  179. Boolean read8bfplugin(StandardFileReply *sfr,char **reason){
  180.         unsigned char magic[2];
  181.         FILECOUNT count;
  182.         Handle h;
  183.         Boolean res = false;
  184.         FILEREF refnum;
  185.         int i;
  186.  
  187.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&refnum) == noErr){
  188.                 // check DOS EXE magic number
  189.                 count = 2;
  190.                 if(FSRead(refnum,&count,magic) == noErr /*&& magic[0]=='M' && magic[1]=='Z'*/){
  191.                         if(GetEOF(refnum,(FILEPOS*)&count) == noErr && count < 256L<<10){ // sanity check file size < 256K
  192.                                 if( (h = readfileintohandle(refnum)) ){
  193.                                         long *q = (long*)PILOCKHANDLE(h,false);
  194.  
  195.                                         // look for signature at start of valid PARM resource
  196.                                         // This signature is observed in Filter Factory standalones.
  197.                                         for( count /= 4 ; count >= PARM_SIZE/4 ; --count, ++q )
  198.                                         {
  199.  
  200. #ifdef MAC_ENV
  201.                                                 // Case #1: Mac is reading Windows (Win16/32) plugin
  202.                                                 if( ((EndianS32_LtoN(q[0]) == PARM_SIZE) ||
  203.                                                      (EndianS32_LtoN(q[0]) == PARM_SIZE_PREMIERE) ||
  204.                                                      (EndianS32_LtoN(q[0]) == PARM_SIG_MAC)) && EndianS32_LtoN(q[1]) == 1
  205.                                                         && (res = readPARM((char*)q, &gdata->parm, reason, 1 /*Windows format resource*/)) )
  206.                                                 {
  207.                                                         // these are the only numeric fields we *have* to swap
  208.                                                         // all the rest are flags which (if we're careful) will work in either ordering
  209.                                                         for(i = 0; i < 8; ++i)
  210.                                                                 slider[i] = EndianS32_LtoN(slider[i]);
  211.                                                 }
  212. #else
  213.                                                 // Case #2: Windows is reading a Windows plugin (if Resource API failed, i.e. Win64 tries to open NE file)
  214.                                                 if( ((q[0] == PARM_SIZE) ||
  215.                                                      (q[0] == PARM_SIZE_PREMIERE) ||
  216.                                                      (q[0] == PARM_SIG_MAC)) && q[1] == 1
  217.                                                         && (res = readPARM((char*)q, &gdata->parm, reason, 1)) )
  218.                                                 {
  219.                                                 }
  220.  
  221.                                                 // Case #3: Windows is reading an old FilterFactory Mac file (.bin)
  222.                                                 else if( ((EndianS32_LtoN(q[0]) == PARM_SIZE) ||
  223.                                                      (EndianS32_LtoN(q[0]) == PARM_SIZE_PREMIERE) ||
  224.                                                      (EndianS32_LtoN(q[0]) == PARM_SIG_MAC)) && EndianS32_LtoN(q[1]) == 1
  225.                                                         && (res = readPARM((char*)q, &gdata->parm, reason, 0 /*Strings are already PStrings*/)) )
  226.                                                 {
  227.                                                         // these are the only numeric fields we *have* to swap
  228.                                                         // all the rest are flags which (if we're careful) will work in either ordering
  229.                                                         for(i = 0; i < 8; ++i)
  230.                                                                 slider[i] = EndianS32_LtoN(slider[i]);
  231.                                                 }
  232. #endif
  233.  
  234.                                                 if (res) break;
  235.                                         }
  236.                                         PIDISPOSEHANDLE(h);
  237.                                 }
  238.                         }
  239.                 } // else no point in proceeding
  240.                 FSClose(refnum);
  241.         }else
  242.                 *reason = _strdup("Could not open file.");
  243.         return res;
  244. }
  245.  
  246. Boolean readPARM(Ptr p,PARM_T *pparm,char **reasonstr,int fromwin){
  247.         int i;
  248.  
  249.         if (*((unsigned int*)p) == PARM_SIZE_PREMIERE) {
  250.                 convert_premiere_to_photoshop(pparm, (PARM_T_PREMIERE*)p);
  251.         } else {
  252.                 // Assume it is Photoshop. Signature either PARM_SIZE (0x2068) or 0x1C68
  253.                 memcpy(pparm,p,sizeof(PARM_T));
  254.         }
  255.  
  256.         if(fromwin){
  257.                 /* Windows PARM resource stores C strings - convert to Pascal strings  */
  258.                 myc2pstr((char*)pparm->category);
  259.                 myc2pstr((char*)pparm->title);
  260.                 myc2pstr((char*)pparm->copyright);
  261.                 myc2pstr((char*)pparm->author);
  262.                 for(i = 0; i < 4; ++i)
  263.                         myc2pstr((char*)pparm->map[i]);
  264.                 for(i = 0; i < 8; ++i)
  265.                         myc2pstr((char*)pparm->ctl[i]);
  266.         }
  267.  
  268.         for(i = 0; i < 4; ++i){
  269.                 if(expr[i]) free(expr[i]);
  270.                 expr[i] = my_strdup(pparm->formula[i]);
  271.         }
  272.  
  273.         for(i = 0; i < 8; ++i)
  274.                 slider[i] = pparm->val[i];
  275.  
  276.         return true;
  277. }
  278.  
  279. Handle readfileintohandle(FILEREF r){
  280.         FILEPOS n;
  281.         Handle h;
  282.         Ptr p;
  283.  
  284.         if( GetEOF(r,&n) == noErr && (h = PINEWHANDLE(n)) ){
  285.                 p = PILOCKHANDLE(h,false);
  286.                 if(SetFPos(r,fsFromStart,0) == noErr && FSRead(r,(FILECOUNT*)&n,p) == noErr){
  287.                         PIUNLOCKHANDLE(h);
  288.                         return h;
  289.                 }
  290.                 PIDISPOSEHANDLE(h);
  291.         }
  292.         return NULL;
  293. }
  294.  
  295. Boolean fileHasExtension(StandardFileReply *sfr, const char* extension) {
  296. #ifdef WIN_ENV
  297.         char name[MAX_PATH+1];
  298.         return sfr->nFileExtension && !strcasecmp(myp2cstrcpy(name,sfr->sfFile.name) + sfr->nFileExtension,extension);
  299. #else
  300.         char name[1025]; // https://stackoverflow.com/questions/1295135/longest-pathname-string-in-mac-os-x-hfs
  301.         char* s = myp2cstrcpy(name,sfr->sfFile.name);
  302.         return strcmp(s + strlen(s) - strlen(extension), extension) == 0;
  303. #endif
  304. }
  305.  
  306. Boolean readfile(StandardFileReply *sfr,char **reason){
  307.         FILEREF r;
  308.         Handle h;
  309.         Boolean res = false;
  310.  
  311.         if(FSpOpenDF(&sfr->sfFile,fsRdPerm,&r) == noErr){
  312.                 if( (h = readfileintohandle(r)) ){
  313.                         if( (res = readparams(h,true,reason)) ) {
  314.                                 gdata->standalone = false; // so metadata fields will default, if user chooses Make...
  315.  
  316.                                 if (fileHasExtension(sfr, ".pff")) {
  317.                                         // If it is a Premiere settings file, we need to swap the channels red and blue
  318.                                         char* tmp;
  319.                                         tmp = my_strdup(expr[0]);
  320.                                         memcpy((void*)expr[0], (void*)expr[2], sizeof(expr[0]));
  321.                                         memcpy((void*)expr[2], (void*)tmp, sizeof(expr[2]));
  322.                                         free(tmp);
  323.                                 }
  324.                         }
  325.  
  326.                         PIDISPOSEHANDLE(h);
  327.                 }
  328.                 FSClose(r);
  329.         }else
  330.                 *reason = _strdup("Could not open the file.");
  331.  
  332.         return res;
  333. }
  334.