Subversion Repositories filter_foundry

Rev

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