/* This file is part of WBMP Format, a File Format plugin for Adobe Photoshop Copyright (C) 2004-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 "wbmp.h" #include "file_io.h" #include "bufio.h" Handle outdata; int rb; OSErr write_varnum(FILEREF r,unsigned long v){ int shift; OSErr e = noErr; /* find most significant 7-bit group */ for( shift=28 ; shift && !(v>>shift) ; shift -= 7) ; for( ; shift >= 0 ; shift -= 7) if( (e = bufputc(r, (v>>shift & 0x7f) | (shift != 0)<<7)) ) break; return e; } OSErr write_start(FormatRecordPtr pb,long *data){ OSErr e; (e = write_varnum((FILEREF)pb->dataFork,WBMP_TYPE)) || (e = bufputc((FILEREF)pb->dataFork,0/*FixHeaderField*/)) || (e = write_varnum((FILEREF)pb->dataFork,pb->imageSize.h)) || (e = write_varnum((FILEREF)pb->dataFork,pb->imageSize.v)); if(!e){ rb = (pb->imageSize.h+7)/8; 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 = rb; /* FIXME: this allocation should be done using Handle suite, since it was not allowed for in Prepare call (maxData). */ if(!(outdata = pb->handleProcs->newProc((long)pb->imageSize.v*rb))) e = memFullErr; else pb->data = pb->handleProcs->lockProc(outdata,false); } return e; } OSErr write_continue(FormatRecordPtr pb,long *data){ OSErr e = noErr; char *row; int i,j,mask = ~(0xff >> (pb->imageSize.h & 7)); for(j = 0,row = pb->data ; j < pb->imageSize.v ; ++j,row += rb ){ // invert data (WBMP wants white=1, black=0) for(i = rb ; i-- ; ) row[i] ^= 0xff; // blank out rightmost bits of last image byte if(pb->imageSize.h & 7) row[rb-1] &= mask; if( (e = bufputbytes((FILEREF)pb->dataFork,rb,row)) ) break; } pb->data = NULL; // pb->theRect.left = pb->theRect.top = pb->theRect.right = pb->theRect.bottom = 0; return e; } OSErr write_finish(FormatRecordPtr pb,long *data){ pb->handleProcs->disposeProc(outdata); flushbuffer((FILEREF)pb->dataFork); return noErr; }