Subversion Repositories filter_foundry

Rev

Rev 485 | Rev 487 | 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-2022 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 "PIBufferSuite.h"
  24. #include "PIHandleSuite.h"
  25.  
  26. void newHandle(FFHandle* hdl, size_t nBytes) {
  27.         PSHandleSuite1* pSHandleSuite1 = NULL;
  28.         PSHandleSuite2* pSHandleSuite2 = NULL;
  29.  
  30.         if ((gpb->sSPBasic != 0) &&
  31.                 (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  32.                 (pSHandleSuite2 != NULL) &&
  33.                 (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  34.                 )
  35.         {
  36.                 // PICA Handle Suite 2.0
  37.                 hdl->signature = HDLVERSION_SUITE2;
  38.                 gdata->lastKnownHandleVersion = hdl->signature;
  39.                 hdl->handle = pSHandleSuite2->New(nBytes);
  40.                 gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  41.         }
  42.         else if ((gpb->sSPBasic != 0) &&
  43.                 (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  44.                 (pSHandleSuite1 != NULL)
  45.                 )
  46.         {
  47.                 // PICA Handle Suite 1.0
  48.                 hdl->signature = HDLVERSION_SUITE1;
  49.                 gdata->lastKnownHandleVersion = hdl->signature;
  50.                 hdl->handle = pSHandleSuite1->New(nBytes);
  51.                 gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  52.         }
  53.         else {
  54.                 // Standard Handle Suite (deprecated)
  55.                 hdl->signature = HDLVERSION_STANDARD;
  56.                 gdata->lastKnownHandleVersion = hdl->signature;
  57.                 hdl->handle = gpb->handleProcs->newProc(nBytes);
  58.         }
  59. }
  60.  
  61. void disposeHandle(FFHandle* hdl) {
  62.         if (hdl->signature == HDLVERSION_SUITE2) {
  63.                 PSHandleSuite2* pSHandleSuite2 = NULL;
  64.                 if ((gpb->sSPBasic != 0) &&
  65.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  66.                         (pSHandleSuite2 != NULL) &&
  67.                         (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  68.                         )
  69.                 {
  70.                         // PICA Handle Suite 2.0
  71.                         pSHandleSuite2->Dispose(hdl->handle);
  72.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  73.                 }
  74.         }
  75.         else if (hdl->signature == HDLVERSION_SUITE1) {
  76.                 PSHandleSuite1* pSHandleSuite1 = NULL;
  77.                 if ((gpb->sSPBasic != 0) &&
  78.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  79.                         (pSHandleSuite1 != NULL)
  80.                         )
  81.                 {
  82.                         // PICA Handle Suite 1.0
  83.                         pSHandleSuite1->Dispose(hdl->handle);
  84.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  85.                 }
  86.         }
  87.         else if (hdl->signature == HDLVERSION_STANDARD) {
  88.                 // Standard Handle Suite (deprecated)
  89.                 gpb->handleProcs->disposeProc(hdl->handle);
  90.         }
  91.         hdl->signature = HDLVERSION_NULL;
  92. }
  93.  
  94. size_t getHandleSize(FFHandle* hdl) {
  95.         if (hdl->signature == HDLVERSION_SUITE2) {
  96.                 PSHandleSuite2* pSHandleSuite2 = NULL;
  97.                 if ((gpb->sSPBasic != 0) &&
  98.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  99.                         (pSHandleSuite2 != NULL) &&
  100.                         (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  101.                         )
  102.                 {
  103.                         // PICA Handle Suite 2.0
  104.                         int32 size = pSHandleSuite2->GetSize(hdl->handle);
  105.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  106.                         return (size_t)size;
  107.                 }
  108.         }
  109.         else if (hdl->signature == HDLVERSION_SUITE1) {
  110.                 PSHandleSuite1* pSHandleSuite1 = NULL;
  111.                 if ((gpb->sSPBasic != 0) &&
  112.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  113.                         (pSHandleSuite1 != NULL)
  114.                         )
  115.                 {
  116.                         // PICA Handle Suite 1.0
  117.                         int32 size = pSHandleSuite1->GetSize(hdl->handle);
  118.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  119.                         return (size_t)size;
  120.                 }
  121.         }
  122.         else if (hdl->signature == HDLVERSION_STANDARD) {
  123.                 // Standard Handle Suite (deprecated)
  124.                 return gpb->handleProcs->getSizeProc(hdl->handle);
  125.         }
  126.         return 0;
  127. }
  128.  
  129. OSErr setHandleSize(FFHandle* hdl, size_t nBytes) {
  130.         if (hdl->signature == HDLVERSION_SUITE2) {
  131.                 PSHandleSuite2* pSHandleSuite2 = NULL;
  132.                 if ((gpb->sSPBasic != 0) &&
  133.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  134.                         (pSHandleSuite2 != NULL) &&
  135.                         (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  136.                         )
  137.                 {
  138.                         // PICA Handle Suite 2.0
  139.                         OSErr ret = pSHandleSuite2->SetSize(hdl->handle, nBytes);
  140.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  141.                         return ret;
  142.                 }
  143.         }
  144.         else if (hdl->signature == HDLVERSION_SUITE1) {
  145.                 PSHandleSuite1* pSHandleSuite1 = NULL;
  146.                 if ((gpb->sSPBasic != 0) &&
  147.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  148.                         (pSHandleSuite1 != NULL)
  149.                         )
  150.                 {
  151.                         // PICA Handle Suite 1.0
  152.                         OSErr ret = pSHandleSuite1->SetSize(hdl->handle, nBytes);
  153.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  154.                         return ret;
  155.                 }
  156.         }
  157.         else if (hdl->signature == HDLVERSION_STANDARD) {
  158.                 // Standard Handle Suite (deprecated)
  159.                 return gpb->handleProcs->setSizeProc(hdl->handle, nBytes);
  160.         }
  161.         return errMissingParameter;
  162. }
  163.  
  164. Ptr lockHandle(FFHandle* hdl) {
  165.         if (hdl->signature == HDLVERSION_SUITE2) {
  166.                 PSHandleSuite2* pSHandleSuite2 = NULL;
  167.                 if ((gpb->sSPBasic != 0) &&
  168.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  169.                         (pSHandleSuite2 != NULL) &&
  170.                         (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  171.                         )
  172.                 {
  173.                         // PICA Handle Suite 2.0
  174.                         Ptr address;
  175.                         Boolean oldLock;
  176.                         pSHandleSuite2->SetLock(hdl->handle, true, &address, &oldLock);
  177.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  178.                         return address;
  179.                 }
  180.         }
  181.         else if (hdl->signature == HDLVERSION_SUITE1) {
  182.                 PSHandleSuite1* pSHandleSuite1 = NULL;
  183.                 if ((gpb->sSPBasic != 0) &&
  184.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  185.                         (pSHandleSuite1 != NULL)
  186.                         )
  187.                 {
  188.                         // PICA Handle Suite 1.0
  189.                         Ptr address;
  190.                         Boolean oldLock;
  191.                         pSHandleSuite1->SetLock(hdl->handle, true, &address, &oldLock);
  192.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  193.                         return address;
  194.                 }
  195.         }
  196.         else if (hdl->signature == HDLVERSION_STANDARD) {
  197.                 // Standard Handle Suite (deprecated)
  198.                 return gpb->handleProcs->lockProc(hdl->handle, true);
  199.         }
  200.         return NULL;
  201. }
  202.  
  203. void unlockHandle(FFHandle* hdl) {
  204.         if (hdl->signature == HDLVERSION_SUITE2) {
  205.                 PSHandleSuite2* pSHandleSuite2 = NULL;
  206.                 if ((gpb->sSPBasic != 0) &&
  207.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion2, (const void**)&pSHandleSuite2) == noErr) &&
  208.                         (pSHandleSuite2 != NULL) &&
  209.                         (pSHandleSuite2 != (PSHandleSuite2*)gpb->handleProcs /*PS7 doesn't have the bug like in BufferSuite2, but we just want to be sure...*/)
  210.                         )
  211.                 {
  212.                         // PICA Handle Suite 2.0
  213.                         Ptr address;
  214.                         Boolean oldLock;
  215.                         pSHandleSuite2->SetLock(hdl->handle, false, &address, &oldLock);
  216.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion2);
  217.                 }
  218.         }
  219.         else if (hdl->signature == HDLVERSION_SUITE1) {
  220.                 PSHandleSuite1* pSHandleSuite1 = NULL;
  221.                 if ((gpb->sSPBasic != 0) &&
  222.                         (gpb->sSPBasic->AcquireSuite(kPIHandleSuite, kPSHandleSuiteVersion1, (const void**)&pSHandleSuite1) == noErr) &&
  223.                         (pSHandleSuite1 != NULL)
  224.                         )
  225.                 {
  226.                         // PICA Handle Suite 1.0
  227.                         Ptr address;
  228.                         Boolean oldLock;
  229.                         pSHandleSuite1->SetLock(hdl->handle, false, &address, &oldLock);
  230.                         gpb->sSPBasic->ReleaseSuite(kPIHandleSuite, kPSHandleSuiteVersion1);
  231.                 }
  232.         }
  233.         else if (hdl->signature == HDLVERSION_STANDARD) {
  234.                 // Standard Handle Suite (deprecated)
  235.                 gpb->handleProcs->unlockProc(hdl->handle);
  236.         }
  237. }
  238.  
  239. // -----------------------------------------------------------------------------------
  240. // These functions are for code backwards compatibility:
  241.  
  242. Handle PINEWHANDLE(int32 size) {
  243.         FFHandle fh;
  244.         newHandle(&fh, size);
  245.         // Note: newHandle() set gdata->lastKnownHandleVersion, so that
  246.         // the other functions like PILOCKHANDLE can use it. This is safe,
  247.         // as we can assume that the version is always the same for all handles from this host.
  248.         return fh.handle;
  249. }
  250.  
  251. void PIDISPOSEHANDLE(Handle h) {
  252.         FFHandle fh;
  253.         fh.signature = gdata->lastKnownHandleVersion;
  254.         fh.handle = h;
  255.         disposeHandle(&fh);
  256. }
  257.  
  258. int32 PIGETHANDLESIZE(Handle h) {
  259.         FFHandle fh;
  260.         fh.signature = gdata->lastKnownHandleVersion;
  261.         fh.handle = h;
  262.         return getHandleSize(&fh);
  263. }
  264.  
  265. OSErr PISETHANDLESIZE(Handle h, int32 newSize) {
  266.         FFHandle fh;
  267.         fh.signature = gdata->lastKnownHandleVersion;
  268.         fh.handle = h;
  269.         return setHandleSize(&fh, newSize);
  270. }
  271.  
  272. Ptr PILOCKHANDLE(Handle h, Boolean moveHigh) {
  273.         FFHandle fh;
  274.         fh.signature = gdata->lastKnownHandleVersion;
  275.         fh.handle = h;
  276.         return lockHandle(&fh);
  277. }
  278.  
  279. void PIUNLOCKHANDLE(Handle h) {
  280.         FFHandle fh;
  281.         fh.signature = gdata->lastKnownHandleVersion;
  282.         fh.handle = h;
  283.         unlockHandle(&fh);
  284. }
  285.  
  286. // -----------------------------------------------------------------------------------
  287.  
  288. void newBuffer(FFBuffer* buf, size_t nBytes) {
  289.         PSBufferSuite1* pSBufferSuite32 = NULL;
  290.         PSBufferSuite2* pSBufferSuite64 = NULL;
  291.  
  292.         if ((gpb->sSPBasic != 0) &&
  293.                 (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion2, (const void**)&pSBufferSuite64) == noErr) &&
  294.                 (pSBufferSuite64 != NULL) &&
  295.                 (pSBufferSuite64 != (PSBufferSuite2*)gpb->bufferProcs /*Implementation mistake in old Photoshop versions! (see note below)*/)
  296.                 )
  297.         {
  298.                 // PICA Buffer Suite 2.0 (64 bit)
  299.                 //
  300.                 // Note: Windows Photoshop 7 and CS 2 (Other Photoshop versions were not tested) accept
  301.                 // kPSBufferSuiteVersion2, but dont't correctly implement it:
  302.                 // Instead of returning a pointer to a PSBufferSuite2 structure,
  303.                 // they return the pointer RecordPtr->bufferProcs (structure BufferProcs)!
  304.                 //
  305.                 // 64-bit support for Windows was established in Photoshop CS 4,
  306.                 // and PSBufferSuite2 was first documented in SDK CS 6.
  307.                 //
  308.                 // So, kPSBufferSuiteVersion2 probably was partially implemented as hidden "Work in progress" version
  309.                 // before it was publicly documented.
  310.                 // Side note:  pb->bufferSpace64/pb->maxSpace64 was documented in SDK CC 2017.
  311.                 //             pb->bufferProcs->allocateProc64/spaceProc64 was documented in SDK CS 6.
  312.                 unsigned32 siz = nBytes;
  313.                 buf->signature = BUFVERSION_SUITE64;
  314.                 gdata->lastKnownBufferVersion = buf->signature;
  315.                 buf->suite = (Ptr)pSBufferSuite64->New(&siz, siz);
  316.                 if (siz < nBytes) {
  317.                         buf->signature = BUFVERSION_NULL;
  318.                         buf->suite = NULL;
  319.                 }
  320.                 gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion2);
  321.         }
  322.         else if ((gpb->sSPBasic != 0) &&
  323.                 (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion1, (const void**)&pSBufferSuite32) == noErr) &&
  324.                 (pSBufferSuite32 != NULL))
  325.         {
  326.                 // PICA Buffer Suite 1.0 (32 bit)
  327.                 unsigned32 siz = nBytes;
  328.                 buf->signature = BUFVERSION_SUITE32;
  329.                 gdata->lastKnownBufferVersion = buf->signature;
  330.                 buf->suite = (Ptr)pSBufferSuite32->New(&siz, siz);
  331.                 if (siz < nBytes) {
  332.                         buf->signature = BUFVERSION_NULL;
  333.                         buf->suite = NULL;
  334.                 }
  335.                 gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion1);
  336.         }
  337.         else if (gpb->bufferProcs->numBufferProcs >= 8)
  338.         {
  339.                 // Standard Buffer Suite 64 bit (deprecated)
  340.                 buf->signature = BUFVERSION_STD64;
  341.                 gdata->lastKnownBufferVersion = buf->signature;
  342.                 if (gpb->bufferProcs->allocateProc64(nBytes, &buf->standard) != noErr) {
  343.                         buf->signature = BUFVERSION_NULL;
  344.                         buf->standard = NULL;
  345.                 }
  346.         }
  347.         else
  348.         {
  349.                 // Standard Buffer Suite 32 bit (deprecated)
  350.                 buf->signature = BUFVERSION_STD32;
  351.                 gdata->lastKnownBufferVersion = buf->signature;
  352.                 if (gpb->bufferProcs->allocateProc(nBytes, &buf->standard) != noErr) {
  353.                         buf->signature = BUFVERSION_NULL;
  354.                         buf->standard = NULL;
  355.                 }
  356.         }
  357. }
  358.  
  359. Ptr lockBuffer(FFBuffer* buf) {
  360.         if (buf->signature == BUFVERSION_SUITE64) {
  361.                 return buf->suite;
  362.         }
  363.         else if (buf->signature == BUFVERSION_SUITE32) {
  364.                 return buf->suite;
  365.         }
  366.         else if (buf->signature == BUFVERSION_STD64) {
  367.                 return gpb->bufferProcs->lockProc(buf->standard, true);
  368.         }
  369.         else if (buf->signature == BUFVERSION_STD32) {
  370.                 return gpb->bufferProcs->lockProc(buf->standard, true);
  371.         }
  372.         else {
  373.                 return NULL;
  374.         }
  375. }
  376.  
  377. void unlockBuffer(FFBuffer* buf) {
  378.         if (buf->signature == BUFVERSION_STD64) {
  379.                 gpb->bufferProcs->unlockProc(buf->standard);
  380.         }
  381.         else if (buf->signature == BUFVERSION_STD32) {
  382.                 gpb->bufferProcs->unlockProc(buf->standard);
  383.         }
  384. }
  385.  
  386. void disposeBuffer(FFBuffer* buf) {
  387.         if (buf->signature == BUFVERSION_SUITE64) {
  388.                 PSBufferSuite2* pSBufferSuite64 = NULL;
  389.                 if ((gpb->sSPBasic != 0) &&
  390.                         (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion2, (const void**)&pSBufferSuite64) == noErr) &&
  391.                         (pSBufferSuite64 != NULL) &&
  392.                         (pSBufferSuite64 != (PSBufferSuite2*)gpb->bufferProcs /*Implementation mistake in old Photoshop versions! (see note below)*/)
  393.                         )
  394.                 {
  395.                         // PICA Buffer Suite 2.0 (64 bit)
  396.                         pSBufferSuite64->Dispose(&buf->suite);
  397.                         gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion2);
  398.                 }
  399.         }
  400.         else if (buf->signature == BUFVERSION_SUITE32) {
  401.                 PSBufferSuite1* pSBufferSuite32 = NULL;
  402.                 if ((gpb->sSPBasic != 0) &&
  403.                         (gpb->sSPBasic->AcquireSuite(kPSBufferSuite, kPSBufferSuiteVersion1, (const void**)&pSBufferSuite32) == noErr) &&
  404.                         (pSBufferSuite32 != NULL))
  405.                 {
  406.                         // PICA Buffer Suite 1.0 (32 bit)
  407.                         pSBufferSuite32->Dispose(&buf->suite);
  408.                         gpb->sSPBasic->ReleaseSuite(kPSBufferSuite, kPSBufferSuiteVersion1);
  409.                 }
  410.         }
  411.         else if (buf->signature == BUFVERSION_STD64) {
  412.                 gpb->bufferProcs->freeProc(buf->standard);
  413.         }
  414.         else if (buf->signature == BUFVERSION_STD32) {
  415.                 gpb->bufferProcs->freeProc(buf->standard);
  416.         }
  417.         buf->signature = BUFVERSION_NULL;
  418. }
  419.