;*---------------------------------------------------------------------------
;  :Program.	fixni.asm
;  :Contents.	fix newicons for OS3.5
;  :Author.	Bert Jahn
;  :EMail.	wepl@kagi.com
;  :Address.	Franz-Liszt-Straße 16, Rudolstadt, 07404, Germany
;  :Version.	$Id: fixni.asm 1.3 1999/12/22 00:36:33 jah Exp jah $
;  :History.	15.12.99 initial
;		19.12.99 fix for selected image depth added
;			 fix for different height added
;		22.12.99 reworked for public release
;  :Requires.	OS V37+
;  :Copyright.	Public Domain
;  :Language.	68000 Assembler
;  :Translator.	Barfly V2.9
;---------------------------------------------------------------------------*
;##########################################################################

	INCDIR	Includes:
	INCLUDE	lvo/exec.i
	INCLUDE	lvo/dos.i
	INCLUDE	lvo/newicon_lib.i
	INCLUDE	workbench/workbench.i
	
	INCLUDE	macros/ntypes.i

	STRUCTURE NewDiskObject,0
		APTR	ndo_StdObject		;The old-style icon
		APTR	ndo_NormalImage		;May be NULL, meaning no new style icon
		APTR	ndo_SelectedImage	;May be NULL, meaning no secondary image
		LABEL	ndo_SIZEOF

	ifeq 1

struct ChunkyImage
{
    UWORD Width;        /* width of the image. Must be <= 93 */
    UWORD Height;       /* height of the image. Must be <= 93 */
    UWORD NumColors;    /* number of pens defined in the palette. Must be <= 256 */
    UWORD Flags;        /* see defines below */
    UBYTE *Palette;     /* array of RGB components of the pens (one byte per gun) */
    UBYTE *ChunkyData;  /* Width*Height bytes storing the colors of the pixels */
};

#define CIF_COLOR_0_TRANSP  (1<<0)  /* color 0 is transparent, not the one */
                                    /* stored in palette */

#define CIF_DEFAULT         (1<<1)  /* when this bit is set, the image was */
                                    /* obtained via the DEFAULTIMAGE ToolType, */
                                    /* therefore it is not actually part of the */
                                    /* icon and will not be stored in the */
                                    /* ToolTypes on a PutNewDiskObject(). */
	endc

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

GL	EQUR	A4		;a4 ptr to Globals
LOC	EQUR	A5		;a5 for local vars

	STRUCTURE	ArgArray,0
		ULONG	aa_input
		LABEL	aa_SIZEOF

	NSTRUCTURE	Globals,0
		NAPTR	gl_execbase
		NAPTR	gl_dosbase
		NAPTR	gl_rdargs
		NSTRUCT	gl_rdarray,aa_SIZEOF
		NALIGNLONG
		NLABEL	gl_SIZEOF

;##########################################################################

	PURE
	OUTPUT	C:FixNI
	SECTION	"",CODE

		bra	.start
		dc.b	"$VER: FixNI 1.3 "
	DOSCMD	"WDate >t:date"
	INCBIN	"t:date"
		dc.b	" by Bert Jahn <wepl@kagi.com>"
		dc.b	" V37+",0
	CNOP 0,2
.start

;##########################################################################

		link	GL,#gl_SIZEOF
		move.l	(4).w,(gl_execbase,GL)

		move.l	#37,d0
		lea	(_dosname),a1
		move.l	(gl_execbase,GL),a6
		jsr	_LVOOpenLibrary(a6)
		move.l	d0,(gl_dosbase,GL)
		beq	.nodoslib

		lea	(_template),a0
		move.l	a0,d1
		lea	(gl_rdarray,GL),a0
		move.l	a0,d2
		moveq	#0,d3
		move.l	(gl_dosbase,GL),a6
		jsr	(_LVOReadArgs,a6)
		move.l	d0,(gl_rdargs,GL)
		bne	.argsok
		lea	(_readargs),a0
		bsr	_PrintErrorDOS
		bra	.noargs
.argsok	
		bsr	_Main
.opend
		move.l	(gl_rdargs,GL),d1
		move.l	(gl_dosbase,GL),a6
		jsr	(_LVOFreeArgs,a6)
.noargs
		move.l	(gl_dosbase,GL),a1
		move.l	(gl_execbase,GL),a6
		jsr	(_LVOCloseLibrary,a6)
.nodoslib
		unlk	GL
		moveq	#0,d0
		rts

;##########################################################################

_Main
	;open newicon.library
		moveq	#39,d0
		lea	(_niname),a1
		move.l	(gl_execbase,GL),a6
		jsr	(_LVOOpenLibrary,a6)
		tst.l	d0
		bne	.niok
		lea	(_nonilib),a0
		bsr	_Print
		bra	.noni
.niok		move.l	d0,a6

	;remove ".info"
		move.l	(gl_rdarray+aa_input,GL),a0
		move.l	a0,a1
.1		tst.b	(a1)+
		bne	.1
		subq.l	#1,a1
		cmp.b	#"o",-(a1)
		bne	.2
		cmp.b	#"f",-(a1)
		bne	.2
		cmp.b	#"n",-(a1)
		bne	.2
		cmp.b	#"i",-(a1)
		bne	.2
		cmp.b	#".",-(a1)
		bne	.2
		clr.b	(a1)
.2
	;load icon
		jsr	(_LVOGetNewDiskObject,a6)
		move.l	d0,d7				;d7 = ndo
		bne	.iconok
		lea	(_loadicon),a0
		bsr	_PrintErrorDOS
		bra	.noobj
.iconok
		move.l	d7,a0

	;check & fix depth for normal image
		move.l	(ndo_NormalImage,a0),d0
		beq	.4
		move.l	d0,a1
		cmp.w	#256,(4,a1)
		bls	.3
		move.w	#256,(4,a1)
.3
	;check & fix depth for selected image
		move.l	(ndo_SelectedImage,a0),d0
		beq	.6
		move.l	d0,a2
		cmp.w	#256,(4,a2)
		bls	.5
		move.w	#256,(4,a2)
.5
	;check & fix different height of normal and selected image
		move.w	(2,a1),d0
		cmp.w	(2,a2),d0
		beq	.6
		move.w	d0,(2,a2)
.6
	;write icon back
		move.l	(gl_rdarray+aa_input,GL),a0
		move.l	d7,a1
		jsr	(_LVOPutNewDiskObject,a6)

	;release icon
.4		move.l	d7,a0
		jsr	(_LVOFreeNewDiskObject,a6)

	;release library
.noobj		move.l	a6,a1
		move.l	(gl_execbase,GL),a6
		jsr	(_LVOCloseLibrary,a6)
.noni
		moveq	#20,d0
		rts

;##########################################################################

	INCDIR	Sources:
	INCLUDE	dosio.i
		Print
	INCLUDE	error.i
		PrintErrorDOS

;##########################################################################

; Operationen
_readargs	dc.b	"read arguments",0
_loadicon	dc.b	"load icon",0

_nonilib	dc.b	"need newicon.library version 39",10,0

;subsystems
_dosname	dc.b	"dos.library",0
_niname		dc.b	"new"
_iname		dc.b	"icon.library",0

_template	dc.b	"INPUTFILE/A"		;name eines zu ladenden Files
		dc.b	0

;##########################################################################

	END

