Example #1
0
	void SimpleSurface::BlitChannel (const RenderTarget &outTarget, const Rect &inSrcRect, int inPosX, int inPosY, int inSrcChannel, int inDestChannel) const {
		
		bool src_alpha = (mPixelFormat == pfAlpha);
		bool dest_alpha = (outTarget.mPixelFormat == pfAlpha);
		
		// Flash API does not have alpha images (might be useful somewhere else?)
		if (src_alpha || dest_alpha)
			return;
		
		if (inDestChannel == CHAN_ALPHA && !(outTarget.Format () & pfHasAlpha))
			return;
		
		bool set_255 = (inSrcChannel == CHAN_ALPHA && !(mPixelFormat & pfHasAlpha));
		
		// Translate inSrcRect src_rect to dest ...
		Rect src_rect (inPosX, inPosY, inSrcRect.w, inSrcRect.h);
		// clip ...
		src_rect = src_rect.Intersect (outTarget.mRect);
		
		// translate back to source-coordinates ...
		src_rect.Translate (inSrcRect.x - inPosX, inSrcRect.y - inPosY);
		// clip to origial rect...
		src_rect = src_rect.Intersect (inSrcRect);
		
		if (src_rect.HasPixels ()) {
			
			int dx = inPosX + src_rect.x;
			int dy = inPosY + src_rect.y;
			
			bool c0_red = gC0IsRed != ((mPixelFormat & pfSwapRB) != 0);
			int src_ch = (inSrcChannel == CHAN_ALPHA ? 3 : inSrcChannel == CHAN_BLUE ? (c0_red ? 2 : 0) : inSrcChannel == CHAN_GREEN ? 1 : (c0_red ? 0 : 2));
			
			c0_red = gC0IsRed != ((outTarget.Format () & pfSwapRB) != 0);
			int dest_ch = (inDestChannel == CHAN_ALPHA ? 3 : inDestChannel == CHAN_BLUE ? (c0_red ? 2 : 0) : inDestChannel == CHAN_GREEN ? 1 : (c0_red ? 0 : 2));
			
			for (int y = 0; y < src_rect.h; y++) {
				
				uint8 *d = outTarget.Row (y + dy) + (dx * 4) + dest_ch;
				if (set_255) {
					
					for (int x = 0; x < src_rect.w; x++) {
						
						*d = 255;
						d += 4;
						
					}
					
				} else {
					
					const uint8 *s = Row (y + src_rect.y) + (src_rect.x * 4) + src_ch;
					
					for(int x = 0; x < src_rect.w; x++) {
						
						*d = *s;
						d += 4;
						s += 4;
						
					}
					
				}
				
			}
			
		}
		
	}