/* 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" char *indata; /* See: http://developer.apple.com/technotes/tn/tn1023.html (Understanding PackBits) http://developer.apple.com/technotes/pt/pt_24.html (MacPaint Document Format) */ OSErr read_start(FormatRecordPtr pb,long *data){ OSErr e = formatCannotRead; int i,j,len,val; char *q; if( (indata = malloc((long)PNTG_ROWS*PNTG_ROWBYTES)) ){ pb->imageMode = plugInModeBitmap; pb->depth = 1; pb->planes = 1; pb->imageHRes = pb->imageVRes = 72L<<16; pb->theRect.left = pb->theRect.top = 0; pb->theRect.right = pb->imageSize.h = PNTG_COLS; pb->theRect.bottom = pb->imageSize.v = PNTG_ROWS; pb->loPlane = pb->hiPlane = 0; pb->colBytes = 1; pb->rowBytes = PNTG_ROWBYTES; pb->data = indata; // if( !(e = read4B(pb->dataFork,&version)) ){ // if(version == 2){ if(!(e = SetFPos((FILEREF)pb->dataFork,fsFromStart,PNTG_HEADERSIZE))){ /* each row is PackBits compressed. */ for(j = PNTG_ROWS,q = indata ; j-- ; ){ //sprintf(s,"j = %d",j);dbg(s); for(i = 0 ; i < PNTG_ROWBYTES ; ){ len = bufgetc((FILEREF)pb->dataFork); if(len == EOF){ //dbg("EOF getting flag byte"); break; // EOF getting flag byte }else if(len == 128) ; /*ignore this flag value*/ else{ if(len > 128){ len = 1+256-len; if((val = bufgetc((FILEREF)pb->dataFork)) == EOF){ //dbg("EOF getting repeat value"); break; // EOF getting repeat value }else if((i+len) <= PNTG_ROWBYTES) memset(q,val,len); else{ //dbg("unpacked data would overflow row(1)"); break; /* unpacked data would overflow row! */ } }else{ ++len; if((i+len) <= PNTG_ROWBYTES){ if( (e = bufgetbytes((FILEREF)pb->dataFork,len,q)) ) break; }else{ //dbg("unpacked data would overflow row(2)"); break; /* unpacked data would overflow row! */ } } q += len; i += len; } //sprintf(s,"end run; %d bytes unpacked",i);dbg(s); } } if(!j) e = noErr; /* we made it! */ }//else dbg("can't SetFPos at end of header"); // }else e = formatCannotRead; // } if(e) free(indata); }else e = memFullErr; return e; } OSErr read_continue(FormatRecordPtr pb,long *data){ pb->data = 0; return noErr; } OSErr read_finish(FormatRecordPtr pb,long *data){ free(indata); return noErr; }