
*
* This routine cuts the selected range of the current frame
* and returns a pointer to the memory location in d0 and
* the length of it in d1. In case of an error, d0 is NULL.
* >> Created brush is INTERLEAVED!
*

CutRangeI
		movem.l	d2-d7/a2-a4/a6,-(sp)
		movem.w	save.range(pc),d4-d7
		sub.w	d4,d6
		sub.w	d5,d7

		lsr.w	#4,d6
		addq.w	#1,d6
		add.w	d6,d6

		addq.w	#1,d7
		move.w	load.depth(pc),d4
		mulu.w	d4,d7			;[d7.l] Anzahl der zu bearbeitenden Bitplanezeilen

		move.w	d6,d0
		ext.l	d0
		mulu.l	d7,d0
		move.l	d0,a2
		move.l	#MEMF_CLEAR,d1
		move.l	4.w,a6
		LIBCALL	AllocVec
		move.l	d0,d2
		beq.s	.nomem
		move.l	d0,a4			;[a4] Zielbitplanes

		move.w	d5,d0
		mulu.w	d4,d0
		moveq	#0,d1
		move.w	load.pagesize(pc),d1
		lsr.w	#3,d1
		move.l	d1,a6			;[a6] Sourcebreite in Bytes
		mulu.l	d1,d0
		move.l	load.rawmemory(pc),a0
		add.l	d0,a0			;[a0] Sourcezeile

		moveq	#32,d6
		moveq	#16,d3

.yloop		move.w	save.range(pc),d0	;[d0] Source-Bitcounter
		ext.l	d0
		move.w	save.range+4(pc),d1
		ext.l	d1
		sub.l	d0,d1
		addq.l	#1,d1			;[d1] Bits to do

.xloop		cmp.l	d6,d1
		bls.s	.less32
		bfextu	(a0){d0:32},d5
		move.l	d5,(a4)+
		add.l	d6,d0
		sub.l	d6,d1
		bra.s	.xloop

.less32		bfextu	(a0){d0:d1},d5
		bfins	d5,(a4){0:d1}
		cmp.l	d3,d1
		bls.s	.less16
		addq.l	#4,a4
		bra.s	.endx
.less16		addq.l	#2,a4
.endx		add.l	a6,a0

		subq.l	#1,d7
		bne.s	.yloop

		move.l	d2,d0
		move.l	a2,d1
.nomem		movem.l	(sp)+,d2-d7/a2-a4/a6
		rts

*
* >> Created brush is NON-INTERLEAVED!
*

CutRangeN
		movem.l	d2-d7/a2-a4/a6,-(sp)
		movem.w	save.range(pc),d4-d7
		sub.w	d4,d6
		sub.w	d5,d7

		lsr.w	#4,d6
		addq.w	#1,d6
		add.w	d6,d6

		addq.w	#1,d7			;[d7.w] Anzahl der zu bearbeitenden Bitplanezeilen - 1

		move.w	load.depth(pc),d4

		move.w	d6,d0
		ext.l	d0
		move.w	d7,d1
		mulu.w	d4,d1
		mulu.l	d1,d0
		move.l	d0,a2
		move.l	#MEMF_CLEAR,d1
		move.l	4.w,a6
		LIBCALL	AllocVec
		move.l	d0,d2
		beq.s	.nomem
		move.l	d0,a4			;[a4] Zielbitplanes

		move.w	d5,d0
		mulu.w	d4,d0
		moveq	#0,d1
		move.w	load.pagesize(pc),d1
		lsr.w	#3,d1
		move.l	d1,d3			;[d3] Sourcebreite in Bytes
		mulu.l	d1,d0
		mulu.w	d4,d1
		move.l	d1,a3			;[a3] Sourcelänge für eine Zeile/alle Planes in Bytes
		move.l	load.rawmemory(pc),a0
		add.l	d0,a0			;[a0] Sourcezeile

		moveq	#32,d6
		lea	16.w,a6
		subq.w	#1,d4			;[d4] Anzahl der Zielbitplanes minus 1
		movem.l	a0/d7,-(sp)

.planesloop	movem.l	(sp),a0/d7

.yloop		move.w	save.range(pc),d0	;[d0] Source-Bitcounter
		ext.l	d0
		move.w	save.range+4(pc),d1
		ext.l	d1
		sub.l	d0,d1
		addq.l	#1,d1			;[d1] Bits to do

.xloop		cmp.l	d6,d1
		bls.s	.less32
		bfextu	(a0){d0:32},d5
		move.l	d5,(a4)+
		add.l	d6,d0
		sub.l	d6,d1
		bra.s	.xloop

.less32		bfextu	(a0){d0:d1},d5
		bfins	d5,(a4){0:d1}
		cmp.l	a6,d1
		bls.s	.less16
		addq.l	#4,a4
		bra.s	.endx
.less16		addq.l	#2,a4
.endx		add.l	a3,a0

		subq.l	#1,d7
		bne.s	.yloop

		add.l	d3,4(sp)
		dbra	d4,.planesloop
		addq.l	#8,sp

		move.l	d2,d0
		move.l	a2,d1
.nomem		movem.l	(sp)+,d2-d7/a2-a4/a6
		rts
