/* This file is part of MacPaint Format, a File Format plugin for Adobe Photoshop Copyright (C) 2003-6 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 #include #include #include "macpaint.h" #include "file_io.h" #include "bufio.h" #include "packbits.h" char *outdata; /* See: http://developer.apple.com/technotes/tn/tn1023.html (Understanding PackBits) http://developer.apple.com/technotes/pt/pt_24.html (MacPaint Document Format) */ OSErr write_start(FormatRecordPtr pb,long *data){ OSErr e; char header[PNTG_HEADERSIZE-4]; long count; if( !(e = write4B((FILEREF)pb->dataFork,PNTG_VERSION)) ){ count = PNTG_HEADERSIZE-4; memset(header,0,count); if( !(e = FSWrite((FILEREF)pb->dataFork,&count,header)) ){ pb->theRect.left = pb->theRect.top = 0; pb->theRect.right = pb->imageSize.h; pb->theRect.bottom = pb->imageSize.v; pb->loPlane = pb->hiPlane = 0; pb->colBytes = 1; pb->rowBytes = PNTG_ROWBYTES; if(!(pb->data = outdata = malloc((long)PNTG_ROWS*PNTG_ROWBYTES))) e = memFullErr; } } return e; } OSErr write_continue(FormatRecordPtr pb,long *data){ OSErr e = noErr; char outrow[PNTG_ROWBYTES+1],temp[PNTG_ROWBYTES],*inrow; int n,j,mask = ~(0xff >> (pb->imageSize.h & 7)); for(j = 0,inrow = outdata ; j < pb->imageSize.v ; ++j,inrow += PNTG_ROWBYTES ){ // sprintf(s,"row %d",j);dbg(s); n = (pb->imageSize.h+7)/8; // bytes of image data // blank out rightmost bits of last image byte if(pb->imageSize.h & 7) inrow[n-1] &= mask; // blank out remainder of row memset(inrow+n,0,PNTG_ROWBYTES-n); n = packbits(inrow,outrow,PNTG_ROWBYTES); if( (e = bufputbytes((FILEREF)pb->dataFork,n,outrow)) ) break; } // output blank rows to fill image depth memset(temp,0,PNTG_ROWBYTES); n = packbits(temp,outrow,PNTG_ROWBYTES); for( ; !e && j < PNTG_ROWS ; ++j ) e = bufputbytes((FILEREF)pb->dataFork,n,outrow); pb->data = 0; // pb->theRect.left = pb->theRect.top = pb->theRect.right = pb->theRect.bottom = 0; return e; } OSErr write_finish(FormatRecordPtr pb,long *data){ free(outdata); flushbuffer((FILEREF)pb->dataFork); return noErr; }