//------------------------------------------------------------------------------
// IqTexOutputFile implementation
boost::shared_ptr<IqTexOutputFile> IqTexOutputFile::open(
		const boostfs::path& fileName, EqImageFileType fileType,
		const CqTexFileHeader& header)
{
	// Check some of the header data to make sure it's minimally sane...
	if(header.width() <= 0 || header.height() <= 0)
	{
		AQSIS_THROW_XQERROR(XqInternal, EqE_BadFile, "Cannot open \"" << fileName
				<< "\" - image width and height cannot be negative or zero.");
	}
	if(header.channelList().numChannels() == 0)
	{
		AQSIS_THROW_XQERROR(XqInternal, EqE_BadFile, "Cannot open \"" << fileName
				<< "\" - no data channels present.");
	}

	// Create the new file object
	boost::shared_ptr<IqTexOutputFile> newFile
		= openMultiOutputFile(fileName, fileType, header);
	if(newFile)
		return newFile;

	switch(fileType)
	{
		// case ...:  // Add new output formats here!
		case ImageFile_Exr:
		case ImageFile_Jpg:
		case ImageFile_Png:
			AQSIS_THROW_XQERROR(XqInternal, EqE_Unimplement, "Cannot open \""
					<< fileName << "\" - unimplemented file type \"" << fileType << "\"");
		default:
			AQSIS_THROW_XQERROR(XqInternal, EqE_BadFile, "Cannot open \""
					<< fileName << "\" - unknown file type \"" << fileType << "\"");
	}

	return newFile;
}
void CqTiffDirHandle::writeRequiredAttrs(const CqTexFileHeader& header)
{
	// Width, height...
	setTiffTagValue<uint32>(TIFFTAG_IMAGEWIDTH, header.width());
	setTiffTagValue<uint32>(TIFFTAG_IMAGELENGTH, header.height());

	// Orientation & planar config should always be fixed.
	setTiffTagValue<uint16>(TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
	setTiffTagValue<uint16>(TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

	// Pixel aspect ratio
	// We have no meaningful resolution unit - we're only interested in pixel
	// aspect ratio, so set the resolution unit to none.
	setTiffTagValue<uint16>(TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
	setTiffTagValue<float>(TIFFTAG_XRESOLUTION, 1.0f);
	setTiffTagValue<float>(TIFFTAG_YRESOLUTION, header.find<Attr::PixelAspectRatio>(1));

	// Compression-related stuff
	writeCompressionAttrs(header);
	// Channel-related stuff
	writeChannelAttrs(header);

	const SqTileInfo* tileInfo = header.findPtr<Attr::TileInfo>();
	if(tileInfo)
	{
		// Set tile dimensions if present.
		setTiffTagValue<uint32>(TIFFTAG_TILEWIDTH, tileInfo->width);
		setTiffTagValue<uint32>(TIFFTAG_TILELENGTH, tileInfo->height);
	}
	else
	{
		// Else write strip size - AFAICT libtiff uses the values of some other
		// fields (compression) to choose a default, so do this last.
		setTiffTagValue<uint32>(TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tiffPtr(), 0));
	}
}