VPictureData_MacPicture::VPictureData_MacPicture(xMacPictureHandle inMacPicHandle)
:VPictureData_Vector()
{
	_SetDecoderByExtension(sCodec_pict);
	fDataSourceDirty = true;
	fDataSourceMetadatasDirty = true;
	fPicHandle = NULL;
	fMetaFile = NULL;
	fTrans = NULL;
#if VERSIONWIN
	fGdiplusMetaFile = 0;
#endif

	if (inMacPicHandle)
	{

#if VERSIONWIN // pp c'est une resource, sous windows elle est deja swapé
		ByteSwapWordArray((sWORD*) (*inMacPicHandle), 5);
#endif

		VPictureDataProvider *prov = VPictureDataProvider::Create((xMacHandle) inMacPicHandle, false);

#if VERSIONWIN // pp c'est une resource, sous windows elle est deja swapé
		ByteSwapWordArray((sWORD*) (*inMacPicHandle), 5);
#endif

		if (prov)
		{
			prov->SetDecoder(fDecoder);
			SetDataProvider(prov, true);
			prov->Release();
		}

	}
}
VPictureData_GDIPlus_Vector::VPictureData_GDIPlus_Vector(Gdiplus::Metafile* inMetafile)
{
	_Init();
	_SetDecoderByExtension("emf");
	fMetafile=inMetafile;
	_InitSize();
	_SetSafe();
}
void VPictureData_GDIPlus_Vector::_FromEnhMetaFile(HENHMETAFILE inMetaFile)
{
	SetDataProvider(0,false);
	_DisposeMetaFile();
	
	_SetDecoderByExtension("emf");
	
	fMetafile=new Gdiplus::Metafile(inMetaFile,true);
	_InitSize();
}
VPictureData_MacPicture::VPictureData_MacPicture(VPictureDataProvider* inDataProvider, _VPictureAccumulator* inRecorder)
:inherited(inDataProvider, inRecorder)
{
	_SetDecoderByExtension(sCodec_pict);
	fPicHandle = NULL;
	fMetaFile = NULL;
	fTrans = NULL;
#if VERSIONWIN
	fGdiplusMetaFile = NULL;
#endif
}
VPictureData_MacPicture::VPictureData_MacPicture()
:inherited()
{
	_SetDecoderByExtension(sCodec_pict);
	fPicHandle = NULL;
	fMetaFile = NULL;
#if VERSIONWIN
	fGdiplusMetaFile = NULL;
#endif
	fTrans = NULL;
}
VPictureData_MacPicture::VPictureData_MacPicture(const VPictureData_MacPicture& inData)
:inherited(inData)
{
	_SetDecoderByExtension(sCodec_pict);
	fPicHandle = NULL;
	fMetaFile = NULL;
	fTrans = NULL;
#if VERSIONWIN
	fGdiplusMetaFile = NULL;
#endif
	if (!fDataProvider && inData.fPicHandle && GetMacAllocator())
	{
		fPicHandle = GetMacAllocator()->Duplicate(inData.fPicHandle);
	}
}
VPictureData_GDIPlus::VPictureData_GDIPlus(Gdiplus::Bitmap* inPicture,BOOL inOwnedPicture)
{
	_Init();
	if(inOwnedPicture)
		fBitmap=inPicture;
	else
	{
		// IMPORTANT il faut ouvrir la bitmap en mode write pour forcer gdiplus a dupliqué le buffer
		// sans le clone pointe tj vers le buffer de inPicture
		fBitmap=inPicture->Clone(0,0,inPicture->GetWidth(),inPicture->GetHeight(),inPicture->GetPixelFormat());
		if(fBitmap)
		{
			Gdiplus::BitmapData bmData;
			Gdiplus::Rect r(0,0,fBitmap->GetWidth(),fBitmap->GetHeight());
			fBitmap->LockBits(&r,Gdiplus::ImageLockModeWrite,fBitmap->GetPixelFormat(),&bmData);
			fBitmap->UnlockBits(&bmData);
		}
	}
	fBounds.SetCoords(0,0,fBitmap->GetWidth(),fBitmap->GetHeight());
	_SetDecoderByExtension("png");	
	_SetSafe();
}
VPictureData_MacPicture::VPictureData_MacPicture(PortRef inPortRef, const VRect& inBounds)
{
	_SetDecoderByExtension(sCodec_pict);
	fPicHandle = NULL;
	fMetaFile = NULL;
	fTrans = NULL;
#if VERSIONWIN
	inPortRef; // pp pour warning
	inBounds;
	fGdiplusMetaFile = NULL;
#endif
#if VERSIONMAC
	GrafPtr oldPort;
	GetPort(&oldPort);
	SetPort(inPortRef);
	RGBColor saveFore, saveBack, white = { 0xffff, 0xffff, 0xffff }, black = { 0, 0, 0 };
	Rect r;

	inBounds.MAC_ToQDRect(r);

	GetBackColor(&saveFore);
	GetForeColor(&saveFore);

	RGBBackColor(&white);
	RGBForeColor(&black);

	PicHandle	newPicture = ::OpenPicture(&r);
	const BitMap*	portBits = ::GetPortBitMapForCopyBits(inPortRef);

	::ClipRect(&r);
	::CopyBits(portBits, portBits, &r, &r, srcCopy, NULL);
	::ClosePicture();

	RGBBackColor(&saveFore);
	RGBForeColor(&saveFore);

	SetPort(oldPort);
	fPicHandle = newPicture;

#if VERSIONWIN
	qtime::Rect rect;
	QDGetPictureBounds((qtime::Picture**)fPicHandle, &rect);
	fBounds.SetCoords(0, 0, rect.right - rect.left, rect.bottom - rect.top);
#else
	Rect rect;
	QDGetPictureBounds((Picture**) fPicHandle, &rect);
	fBounds.SetCoords(0, 0, rect.right - rect.left, rect.bottom - rect.top);
#endif

#if VERSIONMAC
	if (fPicHandle)
	{
		GetMacAllocator()->Lock(fPicHandle);
		CGDataProviderRef dataprov = xV4DPicture_MemoryDataProvider::CGDataProviderCreate(*(char**) fPicHandle, GetMacAllocator()->GetSize(fPicHandle), true);
		GetMacAllocator()->Unlock(fPicHandle);
		fMetaFile = QDPictCreateWithProvider(dataprov);
		CGDataProviderRelease(dataprov);
	}
#endif
#endif
}
void VPictureData_EMF::_Init()
{
	_SetDecoderByExtension("emf");
	fPicHandle=NULL;
	fMetaFile=NULL;
}