/*
    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
*/

#include "world.h"

#include "PIAbout.h"
#include "PIFormat.h"

#include "entry.h" // must come after Photoshop includes

#include "ui.h"

#define _TIFF_DATA_TYPEDEFS_ // avoid clash with Photoshop's same named types
#include "tiffio.h"

// sanity check on Endianness (try to prevent a bad build if headers
// don't match target architecture):
#if defined(WIN_ENV) && HOST_BIGENDIAN
	#error building for Windows, but libtiff headers are for BigEndian??
#endif

struct revertinfo{
	uint16 comptype; // TIFF compression type, fetched when image is read
};

#define G ((globals*)*data)

extern FormatRecordPtr gpb;

OSErr read_prepare(FormatRecordPtr pb);
OSErr read_start(FormatRecordPtr pb);
OSErr read_continue(FormatRecordPtr pb);
OSErr read_finish(FormatRecordPtr pb);

void setupcomp(FormatRecordPtr pb);
OSErr options_start(FormatRecordPtr pb,long *data);

OSErr write_start(FormatRecordPtr pb,long *data);
OSErr write_continue(FormatRecordPtr pb);
OSErr write_finish(FormatRecordPtr pb);

TIFF* TIFFFdOpen(int fd, const char* name, const char* mode);

unsigned fix16(unsigned x);
void swapbytes(unsigned char *p,int n);
void invertbuf(unsigned char *buf, long bytes); // note, invert is by longwords, so buffer should have 4 bytes slop beyond given count!
void putrevertinfo(FormatRecordPtr pb, struct revertinfo *p);
int getrevertinfo(FormatRecordPtr pb, struct revertinfo *p);

extern tsize_t _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size);
extern tsize_t _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size);
extern toff_t _tiffSeekProc(thandle_t fd, toff_t off, int whence);
extern int _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize);
extern void _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size);
extern int _tiffCloseProc(thandle_t fd);
extern toff_t _tiffSizeProc(thandle_t fd);
