.386
.model	tiny,c
.stack	0

gmake	segment	use16
assume	cs:gmake,ds:gmake,es:gmake
org	100h

prog:		jmp	near ptr progr

maxfelb		equ	4

aptuf		equ	0
allcolors	equ	176

pmeret		equ	16384
elmer		equ	512+aptuf+1024-2

xfelb		equ	word ptr header+12h
yfelb		equ	word ptr header+16h

kit		db	0
ifile		dw	0
ofile		dw	0
terj		db	"000",0
		db	"256",0
		db	"hic",0
		db	"tru",0
vterj:
sor		dd	0
sor1		dw	0
sor2		dw	0
xtrans		dw	0
ggsor		dw	0
xmax		dw	320,512,640,800,1024,1280,1600,1600
ymax		dw	240,384,480,600,768,1024,1200,1200
gsor		dw	320,512,640,800,1024,1280,1600,1600
		dw	320,512,640,800,1024,1280,1600,1600
		dw	2*320,2*512,2*640,2*800,2*1024,2*1280,2*1600,2*1600
		dw	3*320,3*512,3*640,3*800,3*1024,3*1280,3*1600,3*1600
psor		dw	0
felb		db	0
		db	0
header		db	54 dup(0)
		db	0,0
lines		dw	1200 dup(0)
paletta		db	3*allcolors dup(0)
htx1		db	"Nincs elg szabad t r.",13,10,36
htx2		db	"Hi nyzą forr s.",13,10,36
htx3		db	"Hib s vagy nem ltez <$"
htx4		db	"> file.",13,10,36
htx5		db	"Rendben.",13,10,36
htx6		db	"Hi nyzą sorsz m.",13,10,36
htx7		db	"Nem 24-bites Truecolor file.",13,10,36
htx8		db	"Sajnos ex egy tmrĄtett BMP.",13,10,34
htx9		db	"Nem megfelel form tum.",13,10,36
htx10		db	"Haszn lhatatlan felbont s.",13,10,36
htx11		db	"Kszl: $"
htx12		db	"...",13,10,36

sorfi		db	"0gfx0000."
kiterj		db	"000",0

progr:		push	512
		mov	bx,70000/16+1
		popf
		mov	ah,4ah
		int	21h

		mov	ax,1600*4
		call	fogl
		jc	mhiba
		mov	sor1,ax
		mov	ax,1600*4
		call	fogl
		jc	mhiba
		mov	sor2,ax
		mov	ax,1600*4
		call	fogl
		jc	mhiba
		mov	xtrans,ax

		mov	si,80h
		movzx	cx,byte ptr [si]
		mov	dx,offset htx6
		jcxz	hiba
		push	ds
		inc	si
		dec	cx
		mov	di,offset sorfi+4
		pop	es
		jz	hiba
		inc	si
soroz:		lodsb
		or	al,al
		jz	sever
		cmp	al,32
		je	sever
		stosb
		loop	soroz
sever:		cmp	di,offset sorfi+8
		jae	zever
		mov	eax,dword ptr sorfi+4
		mov	dword ptr sorfi+5,eax
		mov	sorfi+4,"0"
		inc	di
		jmp	sever
zever:		mov	dword ptr sorfi+8,"000."
		mov	dx,offset htx2
		jcxz	hiba
		mov	di,offset progr
		dec	cx
		mov	al,"."
		mov	dx,di
		jz	hiba
		rep	movsb
		stosb
		mov	ax,3d00h
		mov	dword ptr [di],"pmb"
		int	21h
		jnc	hibok
fhiba:		mov	si,offset progr
		jmp	fchiba
chiba:		mov	si,offset sorfi
fchiba:		pop	bx
hhiba:		mov	dx,offset htx3
		mov	ah,9
		int	21h
		mov	cx,64
		mov	ah,2
hikir:		mov	dl,[si]
		or	dl,dl
		jz	hixir
		int	21h
		inc	si
		loop	hikir
hixir:		mov	dx,offset htx4
hiba:		mov	ah,9
		int	21h
		int	20h
mhiba:		mov	dx,offset htx1
		jmp	hiba
hibok:		mov	ofile,ax

ujfelb:		mov	al,felb
		add	al,"0"
		mov	si,offset terj
		mov	sorfi,al
		mov	kit,0

ujkite:		lodsd
		mov	dword ptr kiterj,eax
		push	si
		xor	dx,dx
		xor	cx,cx
		mov	bx,ofile
		mov	ax,4200h
		int	21h
		jc	fhiba
		mov	dx,offset header
		mov	cx,54
		mov	bx,ofile
		mov	ah,3fh
		int	21h
		jc	fhiba
		cmp	word ptr header,"MB"
		mov	dx,offset htx9
		je	lyo
rossz:		pop	ax
		jmp	hiba
lyo:		cmp	dword ptr header+2,54
		jb	rossz
		cmp	dword ptr header+0ah,54
		jne	rossz
		cmp	word ptr header+1ah,1
		jne	rossz
		cmp	word ptr header+1ch,24
		mov	dx,offset htx7
		jne	rossz
		cmp	dword ptr header+1eh,0
		mov	dx,offset htx8
		jne	rossz
		cmp	xfelb,0
		mov	dx,offset htx10
		je	rossz
		cmp	yfelb,0
		je	rossz
		cmp	dword ptr xfelb,1600
		ja	rossz
		cmp	dword ptr yfelb,1200
		ja	rossz
		mov	dx,offset sorfi
		xor	cx,cx
		mov	ah,3ch
		int	21h
		jc	chiba
		mov	ifile,ax
		mov	dx,offset htx11
		mov	ah,9
		int	21h
		mov	si,offset sorfi
		mov	cx,64
		mov	ah,2
xikir:		mov	dl,[si]
		or	dl,dl
		jz	xixir
		int	21h
		inc	si
		loop	xikir
xixir:		mov	dx,offset htx12
		mov	ah,9
		int	21h
		movzx	ebx,felb
		movzx	eax,ymax[2*ebx]
		dec	eax
		mov	bx,3
		mov	sor,eax
		mov	ax,xfelb
		mul	bx
		mov	ggsor,ax
		mov	psor,0

;		push	xfelb			;	csak tesztelshez
;		push	yfelb			;	(.BMP fejlc)
;		movzx	ebx,felb
;		mov	ax,xmax[2*ebx]
;		mov	xfelb,ax
;		mov	ax,ymax[2*ebx]
;		mov	yfelb,ax
;		mov	dx,offset header
;		mov	cx,54
;		mov	bx,ifile
;		mov	ah,40h
;		int	21h
;		pop	yfelb
;		pop	xfelb

		movzx	ebx,felb
		mov	dx,offset lines
		mov	cx,ymax[2*ebx]
		mov	bx,ifile
		add	cx,cx
		mov	ah,40h
		int	21h
		jc	chiba

		cmp	kit,1
		jne	nit

		push	ds
		xor	ax,ax
		mov	di,offset paletta
		pop	es
		mov	cx,3*(allcolors-128)
		xor	bx,bx
		xor	dx,dx
		rep	stosb
csiga:		mov	al,dl
		mov	ah,cl
		stosw
		mov	ax,bx
		add	bl,64
		stosb
		jnz	csiga
		add	cl,32
		jnz	csiga
		add	dl,64
		jnz	csiga

		mov	dx,offset paletta
		mov	cx,3*allcolors
		mov	bx,ifile
		mov	ah,40h
		int	21h
		jc	chiba
nit:

		movzx	ebx,felb
		xor	eax,eax
		mov	es,xtrans
		xor	si,si
		mov	bp,xfelb
		xor	di,di
		mov	bx,xmax[2*ebx]
		mov	cx,bx
felep:		mov	ax,si
		mul	bp
		div	bx
		shl	ax,2
		stosd
		inc	si
		dec	di
		loop	felep

sorcik:		movzx	ebx,felb
		movzx	edx,yfelb
		movzx	ecx,ymax[2*ebx]
		mov	eax,sor
		mul	edx
		movzx	ebx,ggsor
		div	ecx
		mul	ebx
		add	eax,54
		mov	dx,ax
		shr	eax,16
		mov	cx,ax
		mov	bx,ofile
		mov	ax,4200h
		int	21h
		jc	fhiba
		push	ds
		xor	dx,dx
		mov	cx,ggsor
		mov	bx,ofile
		mov	ds,sor1
		mov	ah,3fh
		int	21h
		pop	ds
		jc	fhiba

		push	ds
		xor	si,si
		xor	di,di
		mov	cx,xfelb
		mov	es,sor2
		mov	ds,sor1
masa1:		movsd
		dec	si
		loop	masa1
		pop	ds

		movzx	ebx,felb
		push	ds
		xor	di,di
		mov	cx,xmax[2*ebx]
		mov	fs,xtrans
		mov	es,sor1
		mov	ds,sor2
masa2:		mov	si,fs:[di]
		movsd
		dec	di
		loop	masa2
		pop	ds

		movzx	edx,kit
		xor	si,si
		xor	di,di
		or	dl,dl
		push	ds
		movzx	ebx,felb
		mov	cx,xmax[2*ebx]
		mov	es,sor2
		mov	ds,sor1
		jnz	c256
cgray:		mov	bl,77
		mov	bh,151
		mov	dl,28
		xchg	bl,dl
cgralp:		lodsb
		mul	bl
		mov	bp,ax
		lodsb
		mul	bh
		add	bp,ax
		lodsb
		mul	dl
		add	ax,bp
		mov	al,ah
		;shr	al,2
		;or	al,64
		stosb
		loop	cgralp
		jmp	save
c256:		cmp	dl,1
		jne	chi
c256lp:		lodsw
		xchg	al,dl
		lodsb
		and	ax,192+224*256
		shr	al,1
		shr	ah,3
		or	ah,al
		xchg	al,dl
		shr	al,6
		or	al,ah
		or	al,128
		stosb
		loop	c256lp
		jmp	save
chi:		cmp	dl,2
		jne	ctrue
chilp:		lodsw
		xchg	al,dl
		lodsb
		shr	al,3
		shr	ah,2
		mov	bl,al
		shl	bx,6
		or	bl,ah
		xchg	al,dl
		shl	bx,5
		shr	al,3
		cbw
		or	ax,bx
		stosw
		loop	chilp
		jmp	save
ctrue:		lodsw
		xchg	al,dl
		lodsb
		stosw
		xchg	al,dl
		stosb
		loop	ctrue

;save:		pop	ds			;	tmrĄts nlkl
;		push	ds
;		movzx	edx,kit
;		movzx	ebx,felb
;		shl	dx,4
;		mov	cx,gsor[edx][2*ebx]
;		mov	bx,psor
;		xor	dx,dx
;		mov	lines[2*ebx],cx
;		mov	bx,ifile
;		mov	ds,sor2
;		mov	ah,40h
;		int	21h
;		pop	ds
;		jc	chiba

save:		pop	ds			;	tmrĄtssel
		push	ds
		movzx	edx,kit
		movzx	ebx,felb
		shl	dx,4
		mov	cx,gsor[edx][2*ebx]
		push	ds
		xor	si,si
		xor	di,di
		mov	es,sor1
		mov	ds,sor2
		call	pack
		pop	ds
		mov	bx,psor
		xor	dx,dx
		mov	lines[2*ebx],cx
		mov	bx,ifile
		mov	ds,sor1
		mov	ah,40h
		int	21h
		pop	ds
		jc	chiba

		inc	psor
		dec	sor
		jns	sorcik

		xor	dx,dx
		xor	cx,cx
		mov	bx,ifile
		mov	ax,4200h
		int	21h
		jc	chiba
		movzx	ebx,felb
		mov	dx,offset lines
		mov	cx,ymax[2*ebx]
		mov	bx,ifile
		add	cx,cx
		mov	ah,40h
		int	21h
		jc	chiba

		mov	bx,ifile
		mov	ah,3eh
		int	21h
		pop	si
		inc	kit
		cmp	si,offset vterj
		jb	ujkite
		inc	felb
		cmp	felb,maxfelb
		jbe	ujfelb

		mov	bx,ofile
		mov	ah,3eh
		int	21h
		mov	dx,offset htx5
		mov	ah,9
		int	21h
		int	20h
;------------------------------------------------------------------------------

;------------------------------------------------------------------------------
fogl:		or	ax,ax
		mov	bx,ax
		jz	nemfogl
		shr	bx,4
		and	al,15
		jz	lefogl
		inc	bx
lefogl:		push	bx
		mov	ah,48h
		int	21h
		pop	cx
		jc	nemfogl
		push	ax
		add	cx,cx
		mov	es,ax
		add	cx,cx
		xor	di,di
		xor	eax,eax
		rep	stosd
		pop	ax
nemfogl:	ret
;------------------------------------------------------------------------------

include	<parancs.asm>
include	<pack.asm>

gmake	ends

end	prog
