/*
    This file is part of TIFFlib Format, a File Format plugin for Adobe Photoshop
    Copyright (C) 2005-7 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
*/

/* note that Photoshop has this odd behaviour:
   in 16 bit modes, it scales channel values to a range of 0..0x8000 
   before passing to plugin - we must allow for this */

#include "tifflibplugin.h"

unsigned fix16(unsigned x){
	/* scale 0..0x8000 to 0..0xffff */
	return ((long)x << 1) - (x >> 15); 
}

void swapbytes(unsigned char *p,int n){
	int i;
	unsigned char *q;
	/* swap bytes in words */
	for( i = n,q = p ; i-- ; q += 2 ){
		unsigned t = q[0];
		q[0] = q[1];
		q[1] = t;
	}
}

void invertbuf(unsigned char *buf, long bytes){
	unsigned long *p,n;

	for(n = bytes/sizeof(long)+1, p = (unsigned long*)buf; n--; ++p)
		*p = ~(*p);
}

void putrevertinfo(FormatRecordPtr pb, struct revertinfo *p){
	/* when plugin entered for the first time for this document,
	 * pb->revertInfo should be NULL */
	Handle h = pb->revertInfo ? pb->revertInfo : (pb->hostNewHdl)(sizeof(struct revertinfo));
	if(h){
		*((struct revertinfo*)*h) = *p;
		pb->revertInfo = h;
	}
}

int getrevertinfo(FormatRecordPtr pb, struct revertinfo *p){
	if(pb->revertInfo){
		*p = *((struct revertinfo*)*pb->revertInfo);
		return 1;
	}
	return 0;
}
