OSErr WriteScriptParams (GPtr globals) { PIWriteDescriptor token = NULL; double percent = gPercent; OSErr gotErr = noErr; if (DescriptorAvailable(NULL)) { /* recording. Do our thing. */ token = OpenWriter(); if (token) { PIPutEnum(token, keyMyArea, typeMySelect, EnumToKey(gWhatArea, typeMySelect)); if (gWhatArea == iSelectRandom) PIPutUnitFloat(token, keyMyAmount, unitPercent, &percent); PIPutEnum(token, keyMyChannels, typeMyComposite, EnumToKey(gWhatChannels, typeMyComposite)); PIPutEnum(token, keyMyCreate, typeMyCreate, EnumToKey(gCreate, typeMyCreate)); gotErr = CloseWriter(&token); /* closes and sets to dialog optional */ /* done. Now pass handle on to Photoshop */ } } return gotErr; }
void DoExecute (GPtr globals) { size_t total = 0; size_t done = 0; Boolean doThis = true; BufferID sBuffer = 0; Ptr sData = NULL; BufferID dBuffer = 0; Ptr dData = NULL; BufferID mBuffer = 0; Ptr mData = NULL; BufferID rBuffer = 0; Ptr rData = NULL; PixelMemoryDesc sDesc, dDesc, mDesc, rDesc; ReadImageDocumentDesc *doc = gStuff->documentInfo; WriteChannelDesc *selection = gStuff->newSelection; ChannelReadPort selectionRead; ReadChannelDesc *composite; ReadChannelDesc *transparency; ReadChannelDesc *curChannel; /* Access the port procs. */ if (!WarnChannelPortAvailable ()) { gResult = errPlugInHostInsufficient; goto CleanUp; } gQueryForParameters = ReadScriptParams (globals); if ( gQueryForParameters ) doThis = DoUI (globals); if ( !doThis ) goto CleanUp; // user cancelled, etc. /* look in gStuff->supportedTreatments for support for this next thang */ if (gCreate == iCreateMaskpath) { // can't do that! Handle h = PIGetResource (StringResource, errCantCreatePath, &total); if (h != NULL) { Str255 errString = ""; // The handle, since it's a string, is automagically fetched as a // pascal string. If we use PIHandle2PString, we'll get two // length bytes. This way, the length byte (which is already byte // 0 of the handle) is preserved as the very first byte of the // string: PIHandle2CString(h, (char*)errString, 255); // handle, string, maxlength PIDisposeHandle(h); h = NULL; // reset gResult = PIReportError(errString); } else gStuff->treatment = KeyToEnum(EnumToKey(gCreate,typeMyCreate),typeMyPISel); // otherwise Pshop will catch it automatically goto CleanUp; // don't do any of this gunk } else gStuff->treatment = KeyToEnum(EnumToKey(gCreate,typeMyCreate),typeMyPISel); // Set the treatment then move on /* We will need a read port for the selection. */ gResult = ReadFromWritePort(&selectionRead, selection->port); if (gResult != noErr) goto CleanUp; /* Figure out which composite data to use. */ if (DoTarget) { composite = doc->targetCompositeChannels; transparency = doc->targetTransparency; if (composite == NULL) { composite = doc->mergedCompositeChannels; transparency = doc->mergedTransparency; } } else { composite = doc->mergedCompositeChannels; transparency = doc->mergedTransparency; } /* Allocate the buffers. */ if (!WarnBufferProcsAvailable ()) { gResult = +1; goto CleanUp; } #define kBufferSize kBlockRows * (long) kBlockCols gResult = AllocateBuffer (kBufferSize, &sBuffer); if (gResult != noErr) goto CleanUp; gResult = AllocateBuffer (kBufferSize, &dBuffer); if (gResult != noErr) goto CleanUp; gResult = AllocateBuffer (kBufferSize, &rBuffer); if (gResult != noErr) goto CleanUp; if (transparency != NULL) { gResult = AllocateBuffer (kBufferSize, &mBuffer); if (gResult != noErr) goto CleanUp; } /* Lock the buffers down. */ sData = LockBuffer (sBuffer, false); dData = LockBuffer (dBuffer, false); rData = LockBuffer (rBuffer, false); if (mBuffer != 0) mData = LockBuffer (mBuffer, false); /* Set up the pixel memory descriptors. */ sDesc.data = sData; sDesc.rowBits = kBlockCols * 8; sDesc.colBits = 8; sDesc.bitOffset = 0; sDesc.depth = 8; dDesc = sDesc; dDesc.data = dData; rDesc = sDesc; rDesc.data = rData; mDesc = sDesc; mDesc.data = mData; rData[0] = kInitRandom; // flag for uninitialized random buffer /* Count the channels to process. */ if (doc->selection != NULL) total += AccountChannel (doc->selection, NULL, selection); curChannel = composite; while (curChannel != NULL) { if (DoTarget ? curChannel->target : curChannel->shown) total += AccountChannel (curChannel, transparency, selection); curChannel = curChannel->next; } if (doc->targetLayerMask != NULL && (DoTarget ? doc->targetLayerMask->target : doc->targetLayerMask->shown)) total += AccountChannel (doc->targetLayerMask, NULL, selection); curChannel = doc->alphaChannels; while (curChannel != NULL) { if (DoTarget ? curChannel->target : curChannel->shown) total += AccountChannel (curChannel, NULL, selection); curChannel = curChannel->next; } /* Apply any existing selection. */ if (doc->selection != NULL) { ApplyChannel (globals, doc->selection, &sDesc, NULL, &mDesc, selection, selectionRead, &dDesc, &rDesc, &done, total); if (gResult != noErr) goto CleanUp; } /* Apply each of the channels. This isn't the most efficient approach since it reads the data back out of the selection repeatedly, but it provides a good work out for the code. */ curChannel = composite; while (curChannel != NULL) { if (DoTarget ? curChannel->target : curChannel->shown) { ApplyChannel (globals, curChannel, &sDesc, transparency, &mDesc, selection, selectionRead, &dDesc, &rDesc, &done, total); if (gResult != noErr) goto CleanUp; } curChannel = curChannel->next; } /* Apply the layer mask. */ if (doc->targetLayerMask != NULL && (DoTarget ? doc->targetLayerMask->target : doc->targetLayerMask->shown)) { ApplyChannel (globals, doc->targetLayerMask, &sDesc, NULL, &mDesc, selection, selectionRead, &dDesc, &rDesc, &done, total); if (gResult != noErr) goto CleanUp; } /* Process the alpha channels. */ curChannel = doc->alphaChannels; while (curChannel != NULL) { if (DoTarget ? curChannel->target : curChannel->shown) { ApplyChannel (globals, curChannel, &sDesc, NULL, &mDesc, selection, selectionRead, &dDesc, &rDesc, &done, total); if (gResult != noErr) goto CleanUp; } curChannel = curChannel->next; } CleanUp: if (sData != NULL) UnlockBuffer (sBuffer); if (dData != NULL) UnlockBuffer (dBuffer); if (mData != NULL) UnlockBuffer (mBuffer); if (rData != NULL) UnlockBuffer (rBuffer); if (sBuffer != 0) FreeBuffer (sBuffer); if (dBuffer != 0) FreeBuffer (dBuffer); if (mBuffer != 0) FreeBuffer (mBuffer); if (rBuffer != 0) FreeBuffer (rBuffer); WriteScriptParams(globals); }