/* This file is part of SGI Format, a File Format plugin for Adobe Photoshop Copyright (C) 2003-2010 Toby Thain, toby@telegraphics.com.au This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "sgiformat.h" #include "PIAbout.h" #include "ui.h" extern struct sgi_header hdr; extern Boolean rle; FormatRecordPtr gpb; // prototype required by MPW MrC DLLEXPORT MACPASCAL void PluginMain(short selector, FormatRecordPtr pb, intptr_t *data, short *result); DLLEXPORT MACPASCAL void PluginMain(short selector, FormatRecordPtr pb, intptr_t *data, short *result){ struct sgi_header h; struct revertinfo *rev; //struct revertinfo *glob; OSErr e = noErr; EnterCodeResource(); if(selector == formatSelectorAbout){ DoAbout((AboutRecordPtr)pb); } else{ gpb = pb; /* A persistent global data block is shared by all uses of the plugin. if(!*data) *data = (intptr_t)PS_NEWHANDLE(sizeof(struct revertinfo)); glob = (struct revertinfo*)PS_LOCKHANDLE((Handle)*data, false);*/ // When reading a document for the first time, revertinfo will // store whether it used RLE compression. // When writing a document, its use of RLE compression is stored; // this setting may have come from a dialog with the user // (options phase). // allocate handle for Revert info, first time: if(!pb->revertInfo) pb->revertInfo = PS_NEWHANDLE(sizeof(struct revertinfo)); rev = (struct revertinfo*)PS_LOCKHANDLE(pb->revertInfo, false); switch (selector){ case formatSelectorReadPrepare: e = read_prepare(pb); break; case formatSelectorReadStart: e = read_start(pb); break; case formatSelectorReadContinue: e = read_continue(pb); break; case formatSelectorReadFinish: if( !(e = read_finish(pb)) ) rev->rle = hdr.storage == SGI_RLE; break; /* write sequence: options, estimate, write */ case formatSelectorOptionsPrepare: case formatSelectorEstimatePrepare: pb->maxData = 0; // nothing allocated in options or estimate phases break; case formatSelectorEstimateStart: { int bpc = pb->depth/8; long rows = pb->planes*pb->imageSize.v, overhead = SGI_HEADER_SIZE + 2*rows*4; /* rle table overhead = 2*zsize*ysize*4 (one 4-byte offset and one count each) best case rle occurs when all runs are minimum length per row = bpc*(xsize/RLE_MAX_RUN+2) [ maximum # of runs ] worst case rle occurs when all runs are verbatim (and maximum length) per row = bpc*(xsize+xsize/RLE_MAX_RUN+2) */ pb->minDataBytes = overhead + rows*bpc*((pb->imageSize.h+RLE_MAX_RUN-1)/RLE_MAX_RUN + 1); pb->maxDataBytes = pb->minDataBytes + rows*bpc*pb->imageSize.h; //DPRINTF("EstimateStart: minDataBytes=%d maxDataBytes=%d",pb->minDataBytes,pb->maxDataBytes); } case formatSelectorEstimateContinue: pb->data = NULL; case formatSelectorEstimateFinish: break; case formatSelectorOptionsStart: rle = rev->rle; if(ReadScriptParamsOnWrite(pb) && !compdlg()) e = userCanceledErr; else rev->rle = rle; case formatSelectorOptionsContinue: pb->data = NULL; case formatSelectorOptionsFinish: break; case formatSelectorWritePrepare: pb->maxData = 2*(pb->imageSize.v*4L) + (pb->imageSize.v+2L)*(long)pb->imageSize.h*(pb->depth/8) + (16L<<10); // slop break; case formatSelectorWriteStart: rle = rev->rle; e = write_start(pb); break; case formatSelectorWriteContinue: e = write_continue(pb); break; case formatSelectorWriteFinish: e = write_finish(pb); WriteScriptParamsOnWrite(pb); if(!e) rev->rle = rle; break; case formatSelectorFilterFile: e = is_sgi((FILEREF)pb->dataFork,&h) ? noErr : formatCannotRead; break; default: e = formatBadParameters; } PS_UNLOCKHANDLE(pb->revertInfo); //PS_UNLOCKHANDLE((Handle)*data); } *result = e; ExitCodeResource(); }