;---------------T----------T-----------------------T
; AGAiff
;
; Last Change > 14.07.1994
; Include > writecolors.s
; 14.07.1994	Allgemeiner Einsprungspunkt, bes. für SaveChunky-Mode

WriteColors	;schreibt Farbwerte entsprechend der Einstellungen
		move.l	colortable(pc),a0
		addq.w	#4,a0
		move.l	colortablelength(pc),d0
		subq.l	#8,d0
		divu.w	#12,d0
		swap	d0
		clr.w	d0
		swap	d0

WriteColorTable	; d0.l *Number of Colors (max. 65535)
		; a0 *Colortable (32BitRed, 32BitGreen, 32BitBlue)
		; Writes ColorTable in right format (--> Gadget)
		; into save.filehandle
		; RETURNS d0 = 0 >> OK !!
		movem.l	d2-d7/a2-a6,-(sp)
		move.l	a0,a2
		move.l	d0,d2
		move.l	4.w,a6
		mulu.l	#12,d0
		moveq	#0,d1
		LIBCALL	AllocVec
		move.l	d0,d7
		bne.s	.memok
		lea	nomemtext(pc),a0
		bsr	type
		moveq	#-1,d0
		rts
.memok		move.l	d7,a3
		subq.w	#1,d2
		move.w	actcolsformat2(pc),d0
		move.l	.wcjumps(pc,d0.w*4),a0
		moveq	#0,d6
		jsr	(a0)
		move.l	save.filehandle(pc),d1
		move.l	d7,d2
		move.l	d6,d3
		bsr	ExtWrite
		move.l	d7,a1
		LIBCALL	FreeVec
		movem.l	(sp)+,d2-d7/a2-a6
		moveq	#0,d0
		rts

.wcjumps	dc.l	.wc12,.wc240rgb,.wc24rgb0,.wc24rgb
		dc.l	.wc24hl,.wc24hli,.wc24lh,.wc24lhi,.wc96

.wc12		moveq	#0,d0
		move.b	(a2),d0
		lsl.w	#4,d0
		move.b	4(a2),d0
		move.b	8(a2),d1
		lsr.b	#4,d1
		and.b	#$f0,d0
		or.b	d1,d0
		move.w	d0,(a3)+
		addq.l	#2,d6
		lea	12(a2),a2
		dbra	d2,.wc12
		rts

.wc24rgb0
		move.b	(a2),(a3)+
		move.b	4(a2),(a3)+
		move.b	8(a2),(a3)+
		clr.b	(a3)+
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc24rgb0
		rts

.wc240rgb
		clr.b	(a3)+
		move.b	(a2),(a3)+
		move.b	4(a2),(a3)+
		move.b	8(a2),(a3)+
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc240rgb
		rts

.wc24rgb
		move.b	(a2),(a3)+
		move.b	4(a2),(a3)+
		move.b	8(a2),(a3)+
		addq.l	#3,d6
		lea	12(a2),a2
		dbra	d2,.wc24rgb
		rts

.wc24hl		move.w	d2,d3
		addq.w	#1,d3
		add.w	d3,d3
.wc24hl_1	moveq	#0,d0
		move.b	(a2),d0	;Highnibble
		lsl.w	#4,d0
		move.b	4(a2),d0
		move.b	8(a2),d1
		lsr.b	#4,d1
		and.b	#$f0,d0
		or.b	d1,d0
		move.w	d0,(a3)+
		moveq	#0,d0
		move.b	(a2),d0	;Lownibble
		and.b	#$f,d0
		lsl.w	#8,d0
		move.b	4(a2),d0
		lsl.b	#4,d0
		move.b	8(a2),d1
		and.b	#$f,d1
		or.b	d1,d0
		move.w	d0,-2(a3,d3.w)
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc24hl_1
		rts

.wc24hli	moveq	#0,d0
		move.b	(a2),d0	;Highnibble
		lsl.w	#4,d0
		move.b	4(a2),d0
		move.b	8(a2),d1
		lsr.b	#4,d1
		and.b	#$f0,d0
		or.b	d1,d0
		move.w	d0,(a3)+
		moveq	#0,d0
		move.b	(a2),d0	;Lownibble
		and.b	#$f,d0
		lsl.w	#8,d0
		move.b	4(a2),d0
		lsl.b	#4,d0
		move.b	8(a2),d1
		and.b	#$f,d1
		or.b	d1,d0
		move.w	d0,(a3)+
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc24hli
		rts

.wc24lh		move.w	d2,d3
		addq.w	#1,d3
		add.w	d3,d3
.wc24lh_1	moveq	#0,d0
		move.b	(a2),d0	;Highnibble
		lsl.w	#4,d0
		move.b	4(a2),d0
		move.b	8(a2),d1
		lsr.b	#4,d1
		and.b	#$f0,d0
		or.b	d1,d0
		move.w	d0,(a3,d3.w)
		moveq	#0,d0
		move.b	(a2),d0	;Lownibble
		and.b	#$f,d0
		lsl.w	#8,d0
		move.b	4(a2),d0
		lsl.b	#4,d0
		move.b	8(a2),d1
		and.b	#$f,d1
		or.b	d1,d0
		move.w	d0,(a3)+
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc24lh_1
		rts

.wc24lhi	moveq	#0,d0
		move.b	(a2),d0	;Lownibble
		and.b	#$f,d0
		lsl.w	#8,d0
		move.b	4(a2),d0
		lsl.b	#4,d0
		move.b	8(a2),d1
		and.b	#$f,d1
		or.b	d1,d0
		move.w	d0,(a3)+
		moveq	#0,d0
		move.b	(a2),d0	;Highnibble
		lsl.w	#4,d0
		move.b	4(a2),d0
		move.b	8(a2),d1
		lsr.b	#4,d1
		and.b	#$f0,d0
		or.b	d1,d0
		move.w	d0,(a3)+
		addq.l	#4,d6
		lea	12(a2),a2
		dbra	d2,.wc24lhi
		rts

.wc96		move.l	(a2)+,(a3)+
		move.l	(a2)+,(a3)+
		move.l	(a2)+,(a3)+
		add.l	#12,d6
		dbra	d2,.wc96
		rts
