//----------------------------------------------------------------------------- void CGDrawContext::drawBitmap (CBitmap* bitmap, const CRect& inRect, const CPoint& inOffset, float alpha) { if (bitmap == 0 || alpha == 0.f) return; CGBitmap* cgBitmap = bitmap->getPlatformBitmap () ? dynamic_cast<CGBitmap*> (bitmap->getPlatformBitmap ()) : 0; CGImageRef image = cgBitmap ? cgBitmap->getCGImage () : 0; if (image) { CGContextRef context = beginCGContext (false, true); if (context) { CRect rect (inRect); rect.makeIntegral (); CPoint offset (inOffset); offset.makeIntegral (); CGContextSetAlpha (context, (CGFloat)alpha*currentState.globalAlpha); CGRect dest; dest.origin.x = rect.left - offset.h; dest.origin.y = -(rect.top) - (bitmap->getHeight () - offset.v); dest.size.width = cgBitmap->getSize ().x; dest.size.height = cgBitmap->getSize ().y; CGRect clipRect2; clipRect2.origin.x = rect.left; clipRect2.origin.y = -(rect.top) - rect.height (); clipRect2.size.width = rect.width (); clipRect2.size.height = rect.height (); CGContextClipToRect (context, clipRect2); CGLayerRef layer = cgBitmap->getCGLayer (); if (layer == 0) { BitmapDrawCountMap::iterator it = bitmapDrawCount.find (cgBitmap); if (it == bitmapDrawCount.end ()) { bitmapDrawCount.insert (std::pair<CGBitmap*, int32_t> (cgBitmap, 1)); CGContextDrawImage (context, dest, image); } else { it->second++; layer = cgBitmap->createCGLayer (context); } } if (layer) { CGContextDrawLayerInRect (context, dest, layer); } releaseCGContext (context); } } }
//----------------------------------------------------------------------------- void CGDrawContext::drawCGImageRef (CGContextRef context, CGImageRef image, CGLayerRef layer, double bitmapScaleFactor, const CRect& inRect, const CPoint& inOffset, float alpha, CBitmap* bitmap) { CRect rect (inRect); CPoint offset (inOffset); CGContextSetAlpha (context, (CGFloat)alpha*currentState.globalAlpha); CGRect dest; dest.origin.x = static_cast<CGFloat> (rect.left - offset.x); dest.origin.y = static_cast<CGFloat> (-(rect.top) - (bitmap->getHeight () - offset.y)); dest.size.width = static_cast<CGFloat> (bitmap->getWidth ()); dest.size.height = static_cast<CGFloat> (bitmap->getHeight ()); CGRect clipRect; clipRect.origin.x = static_cast<CGFloat> (rect.left); clipRect.origin.y = static_cast<CGFloat> (-(rect.top) - rect.getHeight ()); clipRect.size.width = static_cast<CGFloat> (rect.getWidth ()); clipRect.size.height = static_cast<CGFloat> (rect.getHeight ()); if (bitmapScaleFactor != 1.) { CGContextConcatCTM (context, CGAffineTransformMakeScale (static_cast<CGFloat> (1./bitmapScaleFactor), static_cast<CGFloat> (1./bitmapScaleFactor))); CGAffineTransform transform = CGAffineTransformMakeScale (static_cast<CGFloat> (bitmapScaleFactor), static_cast<CGFloat> (bitmapScaleFactor)); clipRect.origin = CGPointApplyAffineTransform (clipRect.origin, transform); clipRect.size = CGSizeApplyAffineTransform (clipRect.size, transform); dest.origin = CGPointApplyAffineTransform (dest.origin, transform); dest.size = CGSizeApplyAffineTransform (dest.size, transform); } dest = pixelAlligned (dest); clipRect = pixelAlligned (clipRect); CGContextClipToRect (context, clipRect); if (layer) { CGContextDrawLayerInRect (context, dest, layer); } else { CGContextDrawImage (context, dest, image); } }
void CGContextDrawLayerInRect_wrap( CGContext *con, CGLayer* lay, float x, float y, float width, float height ) { CGContextDrawLayerInRect( con, CGRectMake(x, y, width, height), lay); }