;========================================================================
;
;		STRIPICON 1.3
;
;		© 1994 by Timm S. Müller
;
;========================================================================
;
;		Konstanten

MINDEPTH	EQU	1		; Minimal erlaubte Anz. Bitplanes
MAXDEPTH	EQU	8		; Maximal erlaubte Anz. Bitplanes
DEFAULTDEPTH	EQU	3		; Default Anzahl Bitplanes

;========================================================================

		include	"workbench/workbench.i"
		include	"dos/dos.i"
		include	"exec/exec.i"
		include	"lvo/exec_lib.i"
		include	"lvo/icon_lib.i"
		include	"lvo/dos_lib.i"

;========================================================================

		section	"a",code

;========================================================================

		move.l	4.w,a6

		moveq	#0,d4			; rdargs ptr
		
		moveq	#20,d7			; returncode

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		open all


		moveq	#prg_SIZEOF,d0		; so we can stay pure
		move.l	#MEMF_PUBLIC+MEMF_CLEAR,d1
		jsr	_LVOAllocMem(a6)
		move.l	d0,a5
		beq.w	quit

		lea	iconlibname(pc),a1
		moveq	#0,d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,prg_iconbase(a5)
		beq.s	closeall2

		lea	doslibname(pc),a1
		moveq	#36,d0
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,(a5)			; = prg_dosbase(a5)
closeall2	beq.w	closeall

		move.l	d0,a6

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		read args


		lea	template(pc),a0		; get arguments
		move.l	a0,d1
		moveq	#prg_argarray1,d2
		add.l	a5,d2
		moveq	#0,d3
		jsr	_LVOReadArgs(a6)

		lea	txt_noargs(pc),a0
		move.l	d0,d4			; store rdargs ptr in d4
		beq.w	textoutput

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		checks args


		moveq	#DEFAULTDEPTH,d6	; default depth

		lea	txt_depth(pc),a0
		move.l	prg_argarray2(a5),d0
		beq.b	argsok

		move.l	d0,a1
		move.l	(a1),d6		
		cmp.w	#MINDEPTH,d6
		blt.w	textoutput
		cmp.w	#MAXDEPTH,d6
		bgt.b	textoutput

argsok

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		remove .info


		move.l	prg_argarray1(a5),a0
		move.l	a0,a2
		move.l	a0,a4

seekend		tst.b	(a2)+
		bne.s	seekend

		subq.l	#1,a2
		lea	infotext-2(pc),a1

		moveq	#5,d1

advance		subq.w	#1,d1
		bpl.s	cont

		clr.b	(a2)
		bra.b	cont2

cont		move.b	-(a2),d0
		addq.l	#2,a1
		cmp.b	(a1),d0
		beq.s	advance
		cmp.b	1(a1),d0
		beq.s	advance

cont2

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		load icon


		move.l	prg_iconbase(a5),a6
		jsr	_LVOGetDiskObject(a6)
		lea	txt_notfound(pc),a0
		move.l	d0,d5
		beq.b	textoutput

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		diskobject.Gadget.GadgetRender->Image
;		diskobject.Gadget.SelectRender->Image
;		change Image.Depth


		move.w	#do_Gadget,a0
		add.l	d0,a0

		move.l	gg_GadgetRender(a0),a1
		cmp.w	ig_Depth(a1),d6
		bge.s	nochange1
		move.w	d6,ig_Depth(a1)

nochange1	move.l	gg_SelectRender(a0),d0
		beq.b	nochange2
		move.l	d0,a1
		cmp.w	ig_Depth(a1),d6
		bge.s	nochange2
		move.w	d6,ig_Depth(a1)

nochange2	move.l	a4,a0
		move.l	d5,a1
		jsr	_LVOPutDiskObject(a6)		; save icon
		move.l	d0,d2

		move.l	d5,a0
		jsr	_LVOFreeDiskObject(a6)		; free structure

		moveq	#0,d7

		lea	txt_notsaved(pc),a0
		tst.l	d2
		bne.b	closeall

		moveq	#20,d7

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		print out da text


textoutput	move.w	(a0)+,d3
		move.l	a0,d2
		move.l	(a5),a6			; = prg_dosbase(a5),a6
		jsr	_LVOOutput(a6)
		move.l	d0,d1
		jsr	_LVOWrite(a6)

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;
;		close down all


closeall	move.l	d4,d1
		beq.b	ca_no0
		move.l	(a5),a6
		jsr	_LVOFreeArgs(a6)

ca_no0		move.l	4.w,a6
		move.l	(a5),d0			; = prg_dosbase(a5)
		beq.s	ca_no1
		move.l	d0,a1
		jsr	_LVOCloseLibrary(a6)

ca_no1		move.l	prg_iconbase(a5),d0
		beq.s	ca_no2
		move.l	d0,a1
		jsr	_LVOCloseLibrary(a6)

ca_no2		move.l	a5,a1
		moveq	#prg_SIZEOF,d0
		jsr	_LVOFreeMem(a6)

quit		move.l	d7,d0
		rts

;========================================================================

		dc.b	0,'$VER: StripIcon 1.3 (18-01-1994)',0
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

doslibname	dc.b	'dos.library',0
		even

iconlibname	dc.b	'icon.library',0
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

		STRUCTURE	prgstruc,0
		APTR		prg_dosbase
		APTR		prg_iconbase
		LONG		prg_argarray1
		LONG		prg_argarray2
		LABEL		prg_SIZEOF

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

template	dc.b	'ICON/A,DEPTH/N/K',0
		even

infotext	dc.b	'oOfFnNiI..'
		even

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

txt_noargs	dc.w	txt_noargslen		; Länge
		dc.b	10," StripIcon 1.3 (18-01-1994)",10
		dc.b	" © 1994 by Timm S. Müller",10,10
		dc.b	" Removes redundant image data from",10
		dc.b	" Workbench icons that are sometimes",10
		dc.b	" created with Commodore's IconEdit.",10,10
		dc.b	" Template: StripIcon ICON/A,DEPTH/N/K",10,10
		dc.b	" DEPTH defaults to 3.",10,10
		even
txt_noargslen	=*-txt_noargs-2

txt_depth	dc.w	txt_depthlen		; Länge
		dc.b	' Depth may range from 1 to 8. Default is 3.',10
txt_depthlen	=*-txt_depth-2
		even

txt_notfound	dc.w	txt_notfoundlen
		dc.b	" Couldn't load icon.",10
txt_notfoundlen	=*-txt_notfound-2
		even

txt_notsaved	dc.w	txt_notsavedlen
		dc.b	" Couldn't save icon.",10
txt_notsavedlen	=*-txt_notsaved-2
		even
