void VPictureData_MacPicture::DrawInCGContext(CGContextRef inDC, const VRect& r, VPictureDrawSettings* inSet) const
{
	_Load();
	if (fMetaFile)
	{
#if VERSIONWIN
		xDraw(fMetaFile, inDC, r, inSet);
#else
		if (inSet && inSet->GetDrawingMode() == 1)
		{
			if (!fTrans)
			{
				_CreateTransparent(inSet);
			}
			if (fTrans)
				xDraw(fTrans, inDC, r, inSet);
			else
				xDraw(fMetaFile, inDC, r, inSet);

		}
		else
			xDraw(fMetaFile, inDC, r, inSet);
#endif
	}
	else if (GetPicHandle())
		xDraw(GetPicHandle(), inDC, r, inSet);
}
void VPictureData_MacPicture::DrawInPortRef(PortRef inPortRef, const VRect& inBounds, VPictureDrawSettings* inSet) const
{
	VPictureDrawSettings drset(inSet);

	_Load();
	bool istrans = false;
	if (inSet && (inSet->GetDrawingMode() == 1 || (inSet->GetDrawingMode() == 2 && inSet->GetAlpha() != 100)))
		istrans = true;

	// pp on utilsie pas l'emf pour l'impression (bug hair line)
	// sauf !!! si l'appel viens du write ou view
	// Dans ce cas, l'impression ne passe plus du tout par altura, donc impossible d'utiliser drawpicture
	if (fMetaFile && (!drset.IsDevicePrinter() || drset.CanUseEMFForMacPicture()))
	{
		drset.SetInterpolationMode(0);
		if (istrans)
		{
			if (!fTrans)
			{
				_CreateTransparent(&drset);
			}
			if (fTrans)
			{
				xDraw(fTrans, inPortRef, inBounds, &drset);
			}
			else
				xDraw(fMetaFile, inPortRef, inBounds, &drset);
		}
		else
		{
			xDraw(fMetaFile, inPortRef, inBounds, &drset);
		}
		return;
	}
	else if (GetPicHandle() && sQDBridge)
	{
		VRect dstRect, srcRect;
		_CalcDestRect(inBounds, dstRect, srcRect, drset);

		xMacRect r;
		r.left = dstRect.GetX();
		r.top = dstRect.GetY();
		r.right = dstRect.GetX() + dstRect.GetWidth();
		r.bottom = dstRect.GetY() + dstRect.GetHeight();


		if (drset.GetScaleMode() == PM_TOPLEFT)
		{
			sLONG ox, oy;
			drset.GetOrigin(ox, oy);
			r.left -= ox;
			r.top -= oy;
			r.right = r.left + GetWidth();
			r.bottom = r.top + GetHeight();
		}
		sQDBridge->DrawInMacPort(inPortRef, *this, (xMacRect*) &r, drset.GetDrawingMode() == 1, drset.GetScaleMode() == PM_TILE);
	}
}
void VPictureData_GDIPlus::DrawInPortRef(PortRef inPortRef,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fBitmap)
	{
		xDraw(fBitmap,inPortRef,r,inSet);
	}
}
void VPictureData_EMF::DrawInD2DGraphicContext(VWinD2DGraphicContext* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fMetaFile)
	{
		//here we need to get a HDC from D2D render target 
		//(if called between BeginUsingContext/EndUsingContext,
		// this will force a EndDraw() before getting hdc and a BeginDraw() to resume drawing after releasing hdc)				
		HDC hdc = inDC->BeginUsingParentContext();
		if (hdc)
			xDraw(fMetaFile,hdc,r,inSet);
		inDC->EndUsingParentContext( hdc);
	}
}
void		VPictureData_MacPicture::_CreateTransparent(VPictureDrawSettings* inSet)const
{
	CGImageRef result = 0;

	if (fTrans)
		return;
	VPictureDrawSettings set(inSet);
	if (GetWidth() && GetHeight())
	{
		if (fMetaFile)
		{

			CGContextRef    context = NULL;
			CGColorSpaceRef colorSpace;
			void *          bitmapData;
			int             bitmapByteCount;
			int             bitmapBytesPerRow;
			sLONG width, height;

			CGRect cgr = QDPictGetBounds(fMetaFile);

			width = cgr.size.width;
			height = cgr.size.height;
			bitmapBytesPerRow = (4 * width + 15) & ~15;
			bitmapByteCount = (bitmapBytesPerRow * height);

			bitmapData = malloc(bitmapByteCount);
			if (bitmapData)
			{
				colorSpace = CGColorSpaceCreateDeviceRGB();//CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

				memset(bitmapData, 0, bitmapByteCount);
				context = CGBitmapContextCreate(bitmapData,
					width,
					height,
					8,      // bits per component
					bitmapBytesPerRow,
					colorSpace,
					kCGImageAlphaNoneSkipLast);
				if (context)
				{
					CGRect vr;
					vr.origin.x = 0;
					vr.origin.y = 0;
					vr.size.width = width;
					vr.size.height = height;

					VRect pictrect(0, 0, width, height);
					set.SetScaleMode(PM_SCALE_TO_FIT);
					set.SetYAxisSwap(height, true);
					set.SetPictureMatrix(VAffineTransform());
					CGContextSaveGState(context);

					CGContextSetRGBFillColor(context, 1, 1, 1, 1);
					CGContextFillRect(context, vr);

					xDraw(fMetaFile, context, pictrect, &set);

					CGContextRestoreGState(context);
					CGDataProviderRef dataprov = xV4DPicture_MemoryDataProvider::CGDataProviderCreate((char*) bitmapData, bitmapByteCount, true);

					result = CGImageCreate(width, height, 8, 32, bitmapBytesPerRow, colorSpace, kCGImageAlphaNoneSkipLast, dataprov, 0, 0, kCGRenderingIntentDefault);
					CGContextRelease(context);
					CGDataProviderRelease(dataprov);
					if (set.GetDrawingMode() == 1)
					{
						const VColor& col = set.GetTransparentColor();
						CGFloat MaskingColors[6];
						MaskingColors[0] = col.GetRed();
						MaskingColors[1] = col.GetGreen();
						MaskingColors[2] = col.GetBlue();
						MaskingColors[3] = col.GetRed();
						MaskingColors[4] = col.GetGreen();
						MaskingColors[5] = col.GetBlue();

						CGImageRef trans = CGImageCreateWithMaskingColors(result, MaskingColors);
						if (trans)
						{
							CFRelease(result);
							fTrans = trans;
						}
					}
				}
				CGColorSpaceRelease(colorSpace);
			}
			free(bitmapData);
		}
	}
}
void VPictureData_MacPicture::DrawInD2DGraphicContext(VWinD2DGraphicContext* inDC, const VRect& inBounds, VPictureDrawSettings* inSet)const
{
	VPictureDrawSettings drset(inSet);

	_Load();
	bool istrans = false;
	if (inSet && (inSet->GetDrawingMode() == 1 || (inSet->GetDrawingMode() == 2 && inSet->GetAlpha() != 100)))
		istrans = true;

	// pp on utilsie pas l'emf pour l'impression (bug hair line)
	// sauf !!! si l'appel viens du write ou view
	// Dans ce cas, l'impression ne passe plus du tout par altura, donc impossible d'utiliser drawpicture
	if (fMetaFile && (!drset.IsDevicePrinter() || drset.CanUseEMFForMacPicture()))
	{
		drset.SetInterpolationMode(0);
		if (istrans)
		{
			if (!fTrans)
			{
				_CreateTransparent(&drset);
			}
			if (fTrans)
			{
				xDraw(fTrans, inDC, inBounds, &drset);
			}
			else
			{
				//here we need to get a HDC from D2D render target 
				//because otherwise D2D render target does not support windows metafiles
				//(if called between BeginUsingContext/EndUsingContext,
				// this will force a EndDraw() before getting hdc and a BeginDraw() to resume drawing after releasing hdc)				
				StSaveContext_NoRetain saveCtx(inDC);
				HDC hdc = inDC->BeginUsingParentContext();
				if (hdc)
					xDraw(fMetaFile, hdc, inBounds, &drset);
				inDC->EndUsingParentContext(hdc);
			}
		}
		else
		{
			//here we need to get a HDC from D2D render target 
			//because otherwise D2D render target does not support windows metafiles
			//(if called between BeginUsingContext/EndUsingContext,
			// this will force a EndDraw() before getting hdc and a BeginDraw() to resume drawing after releasing hdc)				
			StSaveContext_NoRetain saveCtx(inDC);
			HDC hdc = inDC->BeginUsingParentContext();
			if (hdc)
				xDraw(fMetaFile, hdc, inBounds, &drset);
			inDC->EndUsingParentContext(hdc);
		}
		return;
	}
	else if (GetPicHandle() && sQDBridge)
	{
		VRect dstRect, srcRect;
		_CalcDestRect(inBounds, dstRect, srcRect, drset);

		xMacRect r;
		r.left = dstRect.GetX();
		r.top = dstRect.GetY();
		r.right = dstRect.GetX() + dstRect.GetWidth();
		r.bottom = dstRect.GetY() + dstRect.GetHeight();

		if (drset.GetScaleMode() == PM_TOPLEFT)
		{
			sLONG ox, oy;
			drset.GetOrigin(ox, oy);
			r.left -= ox;
			r.top -= oy;
			r.right = r.left + GetWidth();
			r.bottom = r.top + GetHeight();
		}
		{
			//here we need to get a HDC from D2D render target 
			//(if called between BeginUsingContext/EndUsingContext,
			// this will force a EndDraw() before getting hdc and a BeginDraw() to resume drawing after releasing hdc)				
			StSaveContext_NoRetain saveCtx(inDC);
			HDC hdc = inDC->BeginUsingParentContext();
			if (hdc)
				sQDBridge->DrawInMacPort(hdc, *this, (xMacRect*) &r, drset.GetDrawingMode() == 1, drset.GetScaleMode() == PM_TILE);
			inDC->EndUsingParentContext(hdc);
		}
	}
}
void VPictureData_EMF::DrawInGDIPlusGraphics(Gdiplus::Graphics* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fMetaFile)
		xDraw(fMetaFile,inDC,r,inSet);
}
void VPictureData_EMF::DrawInPortRef(PortRef inPortRef,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fMetaFile)
		xDraw(fMetaFile,inPortRef,r,inSet);
}
void VPictureData_GDIPlus::DrawInGDIPlusGraphics(Gdiplus::Graphics* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fBitmap)
		xDraw(fBitmap,inDC,r,inSet);
}
void VPictureData_GDIPlus::DrawInD2DGraphicContext(VWinD2DGraphicContext* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fBitmap)
		xDraw(fBitmap,inDC,r,inSet);
}
void VPictureData_GDIBitmap::DrawInGDIPlusGraphics(Gdiplus::Graphics* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	xDraw(GetHBitmap(),inDC,r,inSet);
}
void VPictureData_GDIBitmap::DrawInD2DGraphicContext(VWinD2DGraphicContext* inDC,const VRect& r,VPictureDrawSettings* inSet)const
{
	Gdiplus::Bitmap bm(GetHBitmap(),0);
	xDraw(&bm,inDC,r,inSet,false);
}
void VPictureData_GDIBitmap::DrawInPortRef(PortRef inPortRef,const VRect& r,VPictureDrawSettings* inSet)const
{
	xDraw(GetHBitmap(),inPortRef,r,inSet);
}
void VPictureData_GDIPlus_Vector::DrawInPortRef(PortRef inPortRef,const VRect& r,VPictureDrawSettings* inSet)const
{
	_Load();
	if(fMetafile)
		xDraw(fMetafile,inPortRef,r,inSet);
}