CloseScreen= -30 -36
OpenScreen= -30 -168
OpenLib= -30 -378
OldOpenLibrary= -30 -378
CloseLibrary= -30 -384
AllocMem= -30 -168
FreeMem= -30 -180
LoadRGB4= -30 -162
Open= -30
Close= -30 -6
Read= -30 -12
Write= -30 -18
Exit= -30 -114
ExecBase= 4

even
start:
	clr.b sflag
	jsr open_dos
	move.l dos_base,a6
	move.l #conname,d1
	move.l #1005,d2
	jsr open(a6)
	move.l d0,conbase

	move.l dos_base,a6
	move.l conbase,d1
	move.l #text3,d2
	move.l #lent3,d3
	sub.l #text3,d3
	jsr write(a6)

	move.l dos_base,a6
	move.l conbase,d1
	move.l #name1,d2
	move.l #32,d3
	jsr read(a6)
	cmp.b #$0a,name1
	beq ende3
	sub.l #1,d0
	move.l #name1,a0
	move.b #0,(a0,d0)

	move.l dos_base,a6
	move.l conbase,d1
	move.l #text2,d2
	move.l #lent2,d3
	sub.l #text2,d3
	jsr write(a6)

	move.l dos_base,a6
	move.l conbase,d1
	move.l #name2,d2
	move.l #32,d3
	jsr read(a6)
	cmp.b #$0a,name2
	beq ende3
	sub.l #1,d0
	move.l #name2,a0
	move.b #0,(a0,d0)


	clr.l d0
	move.l ExecBase,a6
	lea.l GfxName,a1
	jsr OldOpenlibrary(a6)
	move.l d0,GfxBase
	move.l ExecBase,a6	
	lea IntName,a1
	jsr OldOpenLibrary(a6)
	move.l d0,a6
	move.l d0,IntuitionBase
	jsr open_file
	move.l #disk_buf,d2
	move.l #8,d3
	jsr load
	move.l d2,a0
	move.l 4(a0),d3
	move.l d3,chunk_size
	jsr allocate
	move.l chunk_size,d3
	move.l pic_buf,d2
	jsr load
	jsr close_file
	jsr iff_search
	move.l intuitionbase,a6
	lea.l newscreen,a0
	jsr openscreen(a6)
	move.l d0,screen
	jsr pic_print
	jsr free
wlp1:
	andi.b #64,$bfe001
	bne wlp1

	add.w #1,planes_num
	clr.l d0
	move.w width,d0
	divu #8,d0
	mulu height,d0
	move.l d0,groesse
	move.w planes_num,d0
	move.l #2,d1
	rol.l d0,d1
	move.l d1,colors

	move.w planes_num,d0
	move.l #planes,a0
	move.l #pl1,a1
bp1:	move.l (a0)+,d1
	sub.l groesse,d1
	move.l d1,(a1)+
	sub.w #1,d0
	bne bp1	

	move.w planes_num,anzahl

	move.l dos_base,a6
	move.l #name2,d1
	move.l #1006,d2
	jsr open(a6)	
	move.l d0,fn	

	move.l dos_base,a6
	move.l fn,d1
	move.l #anzahl,d2
	move.l #2,d3
	jsr write(a6)
	beq ok

	move.l dos_base,a6
	move.l fn,d1
	move.l #width,d2
	move.l #2,d3
	jsr write(a6)
	beq ende

	move.l dos_base,a6
	move.l fn,d1
	move.l #height,d2
	move.l #2,d3
	jsr write(a6)
	beq ende

	move.l dos_base,a6
	move.l fn,d1
	move.l #colour_map,d2
	move.l colors,d3
	jsr write(a6)

	move.l pl1,d2
	jsr save
	sub.w #1,planes_num
	beq ok
	move.l pl1+4,d2
	jsr save
	sub.w #1,planes_num
	beq ok
	move.l pl1+8,d2
	jsr save
	sub.w #1,planes_num
	beq ok
	move.l pl1+12,d2
	jsr save
	sub.w #1,planes_num
	beq ok
	move.l pl1+16,d2
	jsr save
	sub.w #1,planes_num
	jmp ok
	
save:
	move.l dos_base,a6
	move.l fn,d1
	move.l groesse,d3
	jsr write(a6)
	beq ok
	rts

ok:	move.l dos_base,a6
	move.l fn,d1
	jsr close(a6)
	move.b #1,sflag

ende:	move.l intuitionbase,a6
	move.l screen,a0
	jsr closescreen(a6)
	move.l execbase,a6
	move.l intuitionbase,a1
	jsr closelibrary(a6)
	move.l gfxbase,a1
	jsr closelibrary(a6)
ende2:	move.l dos_base,a6
	move.l conbase,d1
	jsr close(a6)

	move.l execbase,a6
	move.l dos_base,a1
	jsr closelibrary(a6)
	cmp.b #1,sflag
	beq start
prg_end:
	illegal
	rts

ende3:
	move.b #0,sflag
	jmp ende2
	
align 4
title:		dc.b 'The Blade Runnesr presents:',0
align 4
dosname:	dc.b 'dos.library',0
align 4
intname:	dc.b 'intuition.library',0
align 4
gfxname:	dc.b 'graphics.library',0
align 4
gfxbase:	blk.l 1,0
intuitionbase:	blk.l 1,0
dos_base:	blk.l 1,0

Newscreen:
	dc.w 0,0
width:	dc.w 320
height:	dc.w 200
	dc.w 5
	dc.b 0,1
modes:	dc.w $0,$0f
	dc.l font
	dc.l title
	dc.l 0,0

font = 0

screen:	dc.l 0
even
planes:		blk.l 10,0
colour_map:	blk.w 32,0
black_colour:	blk.l 32,0
even

planes_init:
	move.l screen,a0
	add.l #$c0,a0
	move.l #planes,a1
	move.w planes_num,d0

plane_loop:
	move.w d0,d1
	mulu #4,d1
	move.l (a0,d1),(a1,d1)
	dbra d0,plane_loop
	rts
colours_init:
	move.l cmap_chunk,a0
	add.l #8,a0
	move.l #colour_map,a1
	move.w #31,d7
co_loop1:
	clr.w d0
	move.b (a0)+,d0
	and.b #$f0,d0
	lsl.w #4,d0
	move.b (a0)+,d0
	and.w #$0ff0,d0
	clr.w d1
	move.b (a0)+,d1
	lsr.b #4,d1
	and.b #$0f,d1
	or.b d1,d0
	move.w d0,(a1)+
	dbra d7,co_loop1
	rts
pic_print:
	jsr planes_init
	jsr colours_init	
	jsr black_colour_set
	jsr unpacker
	jsr colour_set
	rts
colour_set:
	move.l screen,a0
	add.l #44,a0
	move.l #colour_map,a1
	move.w #32,d0
	move.l gfxbase,a6
	jsr LoadRGB4(a6)
	rts
black_colour_set:
	move.l screen,a0
	add.l #44,a0
	move.l #black_colour,a1
	move.w #32,d0
	move.l gfxbase,a6
	jsr LoadRGB4(a6)
	rts
iff_search:
	move.l pic_buf,a0
	add.l #4,a0
	move.l a0,bmhd_chunk
	move.l 4(a0),d0
	add.l d0,a0
	add.l #8,a0
	move.l a0,cmap_chunk
iff_l1:
	move.l 4(a0),d0
	add.l d0,a0
	add.l #8,a0
	move.l #'BODY',d1
	cmp.l (a0),d1
	bne iff_l1
	move.l a0,body_chunk
	move.l bmhd_chunk,a2
	move.l a2,a1
	add.l #16,a1
	clr.w d0
	move.b (a1),d0
	cmp.w #6,d0
	blt iff_cont
	move.w #6,d0
iff_cont:
	move.l #newscreen,a0
	move.w d0,8(a0)
	subq.w #1,d0
	move.w d0,planes_num
	move.l a2,a1
	add.l #18,a1
	move.b (a1),comp_flag
	move.l a2,a1
	add.l #8,a1
	clr.l d0
	move.w (a1)+,d0
	move.w d0,width
	clr.l d3
	cmp.w #640,d0
	blt iff_cont3
	or.w #$8000,d3
iff_cont3:
	move.w d0,d1
	and.w #7,d1
	beq iff_cont2
 	or.w #8,d0
iff_cont2:
	divu #8,d0
	move.w d0,width_bytes
	clr.l d0
	move.w (a1),d0
	move.w d0,height
	cmp.w #400,d0
	blt iff_cont4
	or.w #4,d3
iff_cont4:
	move.w d3,modes
	rts
unpacker:
	move.l #planes,a2
	move.l body_chunk,a0
	add.l #4,a0
	move.l (a0)+,d7
	move.l a0,a3
	add.l d7,a3
	move.w planes_num,d2
	addq.w #1,d2
unp_loop:
	cmp.l a3,a0
	bge unpack_end
	clr.w d3
pic_loop1:
	move.w d3,d4
	mulu #4,d4
	move.l (a2,d4),a5
	jsr unpack_row
	move.l a5,(a2,d4)
	addq.w #1,d3
	cmp.w d2,d3
	blt pic_loop1
	move.l bmhd_chunk,a5
	andi.b #1,17(a5)
	beq unp_loop
	move.l #mask_dummy,a5
	jsr unpack_row
	bra unp_loop
unpack_row:
	move.l d2,-(sp)
	move.w width_bytes,d2
unp_loop1:
	tst.w d2
	beq unpack_row_end
	clr.w d0
	tst.w comp_flag
	bne unp_comp
	move.w width_bytes,d0
	subq.w #1,d0
	bra unp_loop2
unp_comp:
	move.b (a0)+,d0
	bmi packed
unp_loop2:
	move.b (a0)+,(a5)+
	subq.w #1,d2
	dbra d0,unp_loop2
	bra unp_loop1
packed:
	neg.b d0
	move.b (a0)+,d1
unp_loop3:
	move.b d1,(a5)+
	subq.w #1,d2
	dbra d0,unp_loop3
	bra unp_loop1
unpack_row_end:
	move.l (sp)+,d2
	rts
unpack_end:
	rts

align 4
bmhd_chunk:	blk.l 1,0
cmap_chunk:	blk.l 1,0
body_chunk:	blk.l 1,0
planes_num:	blk.l 1,0
comp_flag:	blk.l 1,0
width_bytes:	blk.l 1,0

open_dos:
	move.l execbase,a6
	lea.l dosname,a1
	jsr OpenLib(a6)
	move.l d0,dos_base
	rts

open_file:
	move.l dos_base,a6
	move.l #name1,d1
	move.l #1005,d2
	jsr Open(a6)
	beq prg_end
	move.l d0,bif
	rts
load:
	move.l dos_base,a6
	move.l bif,d1
	jsr Read(a6)
	rts
close_file:
	move.l dos_base,a6
	move.l bif,d1
	jsr Close(a6)
	rts
allocate:
	move.l execbase,a6
	move.l #$10000,d1
	move.l chunk_size,d0
	jsr AllocMem(a6)
	move.l d0,pic_buf
	rts
free:
	move.l execbase,a6
	move.l pic_buf,a1
	move.l chunk_size,d0
	jsr Freemem(a6)
	rts

align 4
filename:	dc.b 'df1:tbr-logo1',0
align 4
bif:		dc.l 0
disk_buf:	blk.b 10,0
align 4
pic_buf:	blk.l 1,0
chunk_size:	blk.l 1,0
mask_dummy:	blk.b 128,0
align 4
fn:		blk.l 1,0
align 4
bild:		dc.b 'picture',0
align 4
pl1:		blk.l 5,0
align 4
conname:	dc.b 'con:0/0/640/200/'
		dc.b '            THE BLADE RUNNERS PRESENT '
		dc.b 'BILD-KONVERTER VERSION 0.99        ',0
text3:	dc.b $0c,$0a
	dc.b '                          T.B.R. Bild-Konverter '
	dc.b 'V 0.99                                        '
	dc.b '         ============================',$0a,$0a
	dc.b '                    Written 20/04/87 by  The Blade'
	dc.b ' Runners',$0a,$0a
	dc.b ' Dieser Umwandler wandelt IFF-Bilder, die im LO-RES'
	dc.b '-Format gespeichert sind, '
	dc.b ' in ein einheitliches Bitmap-Format um. Dabei spiel'
	dc.b 't es keine Rolle, ob die  '
	dc.b ' Bilder gecruncht, also kompremiert sind (DELUXE-PA'
	dc.b 'INT) oder ungecruncht ge- '
	dc.b ' speichert wurden. Weiterhin spielt es keine Rolle,'
	dc.b ' welches Groessenformat   '
	dc.b ' die Bilder haben. So koennen zum Beispiel Bilder m'
	dc.b 'it beliebiger X- und/oder '
	dc.b ' Y-Aufloesung konvertiert werden (auch PAL-Bilder).'
	dc.b $0a
	dc.b ' Die Bilder werden dabei im folgenden Format gespei'
	dc.b 'chert:',$0a,$0a
	dc.b ' 2 Bytes - Anzahl der Bitplanes',$0a
	dc.b ' 2 Bytes - Aufloesung in X-Richtung',$0a
	dc.b ' 2 Bytes - Aufloesung in Y-Richtung',$0a
	dc.b ' 4 bis 64 Bytes - Farbe (pro Farbe ein WORD)',$0a
	dc.b ' 1 bis 5 Bitplanes'
	dc.b $0a,$0a,' Name des IFF-Bildes: '
	dc.b 0
lent3:	equ *

text2:	dc.b $0a,' Name des neuen Bildes: ',0
lent2:	equ *
align 4
conbase:	blk.l 1,0
name1:		blk.l 8,0
name2:		blk.l 8,0
x:		blk.l 1,0
groesse:	blk.l 1,0
colors:		blk.l 1,0
anzahl:		dc.w 0
sflag:		dc.b 0
