;
; ByteFilter V1.20   05-Sep-1993
;
; (c) 1993 by Jan Hagqvist
;

; V0.01  Friday,   03-Sep-1993		; Open window etc.
; V0.30  Saturday, 04-Sep-1993		; Filter, slow mode
; V1.00  -      "     "      -		; Filter, fast mode
; V1.05  Sunday,   05-Sep-1993		; Fixed a bug in fast&slow-filter
; V1.20  -      "     "      -		; Filter, ultrafast mode


	include "jhextras.i"

  XREF _LVOOpenLibrary
  XREF _LVOCloseLibrary
  XREF _LVOOpenWindow
  XREF _LVOCloseWindow
  XREF _LVOWaitPort
  XREF _LVOGetMsg
  XREF _LVOReplyMsg
  XREF _LVOMove
  XREF _LVOText
  XREF _LVOActivateGadget
  XREF _LVORefreshGList
  XREF _LVORemoveGadget
  XREF _LVOAddGadget
  XREF _LVOSetAPen
  XREF _LVOOpen
  XREF _LVOClose
  XREF _LVOSeek
  XREF _LVORead
  XREF _LVOWrite
  XREF _LVOAllocMem
  XREF _LVOFreeMem


JAM1				equ 0
WBENCHSCREEN			equ 1
GFLG_GADGHCOMP			equ $0000
GFLG_GADGHIMAGE			equ $0002
GFLG_DISABLED			equ $0100
GACT_RELVERIFY			equ $0001
GACT_TOGGLESELECT		equ $0100
GTYP_BOOLGADGET			equ $0001
GTYP_STRGADGET			equ $0004
IDCMP_GADGETUP			equ $00000040
IDCMP_CLOSEWINDOW		equ $00000200
WFLG_SMART_REFRESH		equ $00000000
WFLG_DRAGBAR			equ $00000002
WFLG_DEPTHGADGET		equ $00000004
WFLG_CLOSEGADGET		equ $00000008
WFLG_ACTIVATE			equ $00001000
WFLG_RMBTRAP			equ $00010000


ByteFilter
  move.l  4,a6
  moveq.l #0,d0
  lea     JHExtrasName(pc),a1
  jsr     _LVOOpenLibrary(a6)
  move.l  d0,JHExtrasBase
  tst.l   d0
  beq     Quit

  move.l  JHExtrasBase(pc),a0
  move.l  ml_DosLib(a0),DosBase
  move.l  ml_GfxLib(a0),GfxBase
  move.l  ml_IntuitionLib(a0),IntuitionBase
  move.w  ml_SysVersion(a0),SysVersion

  bsr     CreateHexGadgets

  move.l  IntuitionBase(pc),a6
  lea     NewWindow(pc),a0
  jsr     _LVOOpenWindow(a6)
  move.l  d0,MyWindow
  move.l  d0,a0
  move.l  50(a0),MyRastPort
  move.l  86(a0),MyUserPort

  lea     SourceNameGadget(pc),a0
  bsr     ActivateGadget

  move.l  GfxBase(pc),a6
  move.l  MyRastPort(pc),a1
  moveq.l #2,d0
  jsr     _LVOSetAPen(a6)

  move.l  #GreetsString,StatusArgs
  bsr     ShowStatusLine


WaitForMessage
  tst.b   QuitFlag
  bne     Quit

  move.l  4,a6
  move.l  MyUserPort(pc),a0
  jsr     _LVOWaitPort(a6)
WaitForMessage2
  move.l  4,a6
  move.l  MyUserPort(pc),a0
  jsr     _LVOGetMsg(a6)
  tst.l   d0
  beq.s   WaitForMessage
  move.l  d0,a1
  move.l  20(a1),Class
  move.w  24(a1),Code
  move.w  26(a1),Qualifier
  move.l  28(a1),IAddress
  jsr     _LVOReplyMsg(a6)
  cmp.l   #IDCMP_CLOSEWINDOW,Class
  bne.s   WaitForMessage3
  move.b  #1,QuitFlag
WaitForMessage3
  cmp.l   #IDCMP_GADGETUP,Class
  bne.s   WaitForMessage4
  move.l  IAddress(pc),a0
  tst.l   40(a0)
  beq.s   WaitForMessage4
  move.l  IntuitionBase(pc),a6
  move.l  40(a0),a0
  jsr     (a0)
WaitForMessage4
  bra     WaitForMessage2


Quit
  move.l  IntuitionBase(pc),a6
  tst.l   MyWindow
  beq.s   Quit2
  move.l  MyWindow(pc),a0
  jsr     _LVOCloseWindow(a6)
Quit2
  move.l  4,a6
  tst.l   JHExtrasBase
  beq.s   Quit3
  move.l  JHExtrasBase(pc),a1
  jsr     _LVOCloseLibrary(a6)
Quit3
  moveq.l #0,d0
  rts


ShowStatusLine
  move.l  GfxBase(pc),a6
  move.l  MyRastPort(pc),a1
  move.l  #374,d0
  move.l  #185,d1
  jsr     _LVOMove(a6)

  move.l  JHExtrasBase(pc),a6
  lea     StatusFormat(pc),a0
  lea     StatusArgs(pc),a1
  lea     StatusString,a2
  jsr     _LVOJHFormatAString(a6)

  lea     StatusString,a0
  jsr     _LVOJHStrLen(a6)
  move.l  GfxBase(pc),a6
  move.l  MyRastPort(pc),a1
  jsr     _LVOText(a6)
  rts

Invert
  move.l  #InvertString,StatusArgs
  bsr     ShowStatusLine

  move.l  IntuitionBase(pc),a6
  lea     HexFlags,a3
  lea     HexGadgets,a4
  lea     IntuiTexts,a5
  move.l  #255,d3
Invert2
  bchg    #0,0(a3,d3.w)

  bchg    #7,13(a4)

  add.l   #44,a4
  add.l   #44,a5
  dbf     d3,Invert2

  lea     HexGadgets,a0
  move.l  MyWindow(pc),a1
  sub.l   a2,a2
  moveq.l #-1,d0
  jsr     _LVORefreshGList(a6)

  move.l  #WaitingString,StatusArgs
  bsr     ShowStatusLine
  rts


CheckHexFlag
  lea     HexFlags,a3
  move.l  IAddress,a4

  move.w  38(a4),d3			; Byte number
  bchg    #0,0(a3,d3.w)			; Change byte state

  move.l  #WaitingString,StatusArgs
  bsr     ShowStatusLine
  rts

SourceName
  lea     DestNameGadget(pc),a0
  bsr     ActivateGadget

  move.l  #WaitingString,StatusArgs
  bsr     ShowStatusLine
  rts

DestName
  lea     SourceNameGadget(pc),a0
  bsr     ActivateGadget

  move.l  #WaitingString,StatusArgs
  bsr     ShowStatusLine
  rts


Filter
  move.l  DosBase(pc),a6
  move.l  #SourceFileName,d1
  move.l  #1005,d2
  jsr     _LVOOpen(a6)
  move.l  d0,d6				; d6=sourcefile pointer
  tst.l   d0
  beq     NoSource

  move.l  JHExtrasBase(pc),a6
  move.l  #DestFileName,d1
  move.l  #1006,d2
  move.l  MyWindow(pc),a0
  jsr     _LVOJHOpen(a6)
  move.l  d0,d7				; d7=destfile pointer
  tst.l   d0
  beq     NoDest

  move.l  DosBase(pc),a6
  move.l  d6,d1
  moveq.l #0,d2
  moveq.l #1,d3
  jsr     _LVOSeek(a6)			; Bounce to the end of the sourcefile
  move.l  d6,d1
  moveq.l #0,d2
  moveq.l #-1,d3
  jsr     _LVOSeek(a6)			; And back to the beginning
  move.l  d0,FileSize

  move.l  4,a6
  move.l  FileSize(pc),d0
  moveq.l #0,d1
  jsr     _LVOAllocMem(a6)		; Get mem for sourcefile
  move.l  d0,SourceMem
  tst.l   d0				; Did we get the mem?
  bne.s   GetMem			; Yes
  bsr     FilterSlow			; No, do it slow
  bra.s   GetMem2
GetMem
  move.l  4,a6
  move.l  FileSize(pc),d0
  moveq.l #0,d1
  jsr     _LVOAllocMem(a6)		; Get mem for sourcefile
  move.l  d0,DestMem
  tst.l   d0				; Did we get the mem?
  beq.s   GetMem1			; No
  bsr     FilterUltraFast		; Do it ultrafast
  bra.s   GetMem2
GetMem1
  bsr     FilterFast			; Do it fast
GetMem2
  move.l  d0,d5				; Store error flag
  tst.l   DestMem
  beq.s   GetMem3
  move.l  4,a6
  move.l  DestMem(pc),a1
  move.l  FileSize(pc),d0
  jsr     _LVOFreeMem(a6)		; Free the mem
GetMem3
  tst.l   SourceMem
  beq.s   GetMem4
  move.l  4,a6
  move.l  SourceMem(pc),a1
  move.l  FileSize(pc),d0
  jsr     _LVOFreeMem(a6)		; Free the mem
GetMem4

  move.l  DosBase(pc),a6
  move.l  d7,d1
  jsr     _LVOClose(a6)			; Close destfile
  move.l  d6,d1
  jsr     _LVOClose(a6)			; Close sourcefile

  cmp.l   #-1,d5			; Was there a write error?
  beq.s   FilterWriteError
  move.l  #WaitingString,StatusArgs
  bsr     ShowStatusLine
  rts
FilterWriteError
  move.l  #WriteErrorString,StatusArgs
  bsr     ShowStatusLine
  rts

NoSource
  move.l  #NoSourceString,StatusArgs
  bsr     ShowStatusLine
  rts

NoDest
  move.l  DosBase(pc),a6
  move.l  d6,d1
  jsr     _LVOClose(a6)
  move.l  #NoDestString,StatusArgs
  bsr     ShowStatusLine
  rts

FilterUltraFast
  move.l  #FilteringUltraFastString,StatusArgs
  bsr     ShowStatusLine
  move.l  DosBase(pc),a6
  move.l  d6,d1
  move.l  SourceMem(pc),d2
  move.l  FileSize(pc),d3
  jsr     _LVORead(a6)			; Read the source file
  cmp.l   #-1,d0
  beq.s   FilterUltraFast2

  move.l  FileSize(pc),d4
  move.l  SourceMem(pc),a4
  lea     HexFlags,a5
  move.l  DestMem,a3
  moveq.l #0,d5
FilterUltraFast3
  move.b  (a4)+,d0
  ext.w   d0
  and.w   #$00FF,d0
  tst.b   0(a5,d0.w)
  bne.s   FilterUltraFast3.1

  move.b  d0,(a3)+			; Store it to the buffer
  addq.l  #1,d5				; Increase destfile length
FilterUltraFast3.1
  subq.l  #1,d4
  tst.l   d4
  bne.s   FilterUltraFast3

  move.l  d7,d1
  move.l  DestMem(pc),d2
  move.l  d5,d3
  jsr     _LVOWrite(a6)			; Write the destfile
FilterUltraFast2
  rts

FilterFast
  move.l  #FilteringFastString,StatusArgs
  bsr     ShowStatusLine
  move.l  DosBase(pc),a6
  move.l  d6,d1
  move.l  SourceMem(pc),d2
  move.l  FileSize(pc),d3
  jsr     _LVORead(a6)			; Read the source file
  cmp.l   #-1,d0
  beq.s   FilterFast2

  move.l  FileSize(pc),d4
  move.l  SourceMem(pc),a4
  lea     HexFlags,a5
  lea     StatusString,a3
  moveq.l #0,d5
FilterFast3
  move.b  (a4)+,d0
  ext.w   d0
  and.w   #$00FF,d0
  tst.b   0(a5,d0.w)
  bne.s   FilterFast3.1

  move.b  d0,(a3)+			; Store it to the buffer
  addq.l  #1,d5
  cmp.w   #256,d5			; Buffer full?
  bne.s   FilterFast3.1			; No

  move.l  d7,d1
  move.l  #StatusString,d2
  move.l  d5,d3
  jsr     _LVOWrite(a6)			; Write buffer
  cmp.l   #-1,d0			; Error?
  beq.s   FilterFast2			; Yes
  lea     StatusString,a3
  moveq.l #0,d5
FilterFast3.1
  subq.l  #1,d4
  tst.l   d4
  bne.s   FilterFast3

  move.l  d7,d1
  move.l  #StatusString,d2
  move.l  d5,d3
  jsr     _LVOWrite(a6)			; Write rest of the buffer
FilterFast2
  rts

FilterSlow
  move.l  #FilteringSlowString,StatusArgs
  bsr     ShowStatusLine
  move.l  DosBase(pc),a6
FilterSlow0
  move.l  d6,d1
  move.l  #StatusString,d2
  move.l  #256,d3
  jsr     _LVORead(a6)
  tst.l   d0
  beq.s   FilterSlow3.2
  cmp.l   #-1,d0
  beq.s   FilterSlow2

  move.l  d0,d4
  lea     StatusString,a4
  lea     HexFlags,a5
  subq.l  #1,d4
  move.l  a4,a3
  moveq.l #0,d5
FilterSlow3
  move.b  (a4)+,d0
  ext.w   d0
  and.w   #$00FF,d0
  tst.b   0(a5,d0.w)
  bne.s   FilterSlow3.1

  move.b  d0,(a3)+			; Store it to the buffer
  addq.l  #1,d5
  cmp.w   #256,d5			; Buffer full?
  bne.s   FilterSlow3.1			; No

  move.l  d7,d1
  move.l  #StatusString,d2
  move.l  d5,d3
  jsr     _LVOWrite(a6)
  cmp.l   #-1,d0			; Error?
  beq.s   FilterSlow2			; Yes
FilterSlow3.1
  dbf     d4,FilterSlow3
  bra.s   FilterSlow0
FilterSlow3.2
  move.l  d7,d1
  move.l  #StatusString,d2
  move.l  d5,d3
  jsr     _LVOWrite(a6)			; Write rest of the buffer
FilterSlow2
  rts

ActivateGadget
  move.l  IntuitionBase(pc),a6
  move.l  MyWindow(pc),a1
  sub.l   a2,a2
  jsr     _LVOActivateGadget(a6)
  rts

CreateHexGadgets
  lea     HexGadgets,a3
  lea     IntuiTexts,a4
  lea     GadgetTexts,a5
  lea     HexNumbers(pc),a2
  move.l  #255,d3			; Hex number
  moveq.l #15,d4			; y
CreateHexGadgets2
  moveq.l #15,d5			; x
CreateHexGadgets3
  move.l  a3,a0
  add.l   #44,a0
  move.l  a0,(a3)			; NextGadget

  move.l  d5,d0
  mulu    #22,d0
  add.l   #10,d0
  move.w  d0,4(a3)			; LeftEdge

  move.l  d4,d0
  mulu    #11,d0
  add.l   #14,d0
  move.w  d0,6(a3)			; TopEdge

  move.w  #22,8(a3)			; Width
  move.w  #11,10(a3)			; Height
  move.w  #GFLG_GADGHIMAGE,12(a3)	; Flags
  move.w  #GACT_RELVERIFY!GACT_TOGGLESELECT,14(a3)	; Activation
  move.w  #GTYP_BOOLGADGET,16(a3)	; GadgetType
  move.l  #HexBorder,18(a3)		; GadgetRender
  move.l  #HexBorder3,22(a3)		; SelectRender
  move.l  a4,26(a3)			; GadgetText
  move.l  #0,30(a3)			; MutualExclude
  move.l  #0,34(a3)			; SpecialInfo
  move.w  d3,38(a3)			; GadgetID
  move.l  #CheckHexFlag,40(a3)		; UserData

  move.l  #$01020000,(a4)		; FrontPen, BackPen, DrawMode, pad
  move.l  #$00020002,4(a4)		; LeftEdge, TopEdge
  move.l  #Font0,8(a4)			; ITextFont
  move.l  a5,12(a4)			; IText
  move.l  #0,16(a4)			; NextText

  move.w  d3,d0
  and.w   #$00F0,d0
  asr.w   #4,d0
  move.b  0(a2,d0.w),(a5)		; The first digit

  move.w  d3,d0
  and.w   #$000F,d0
  move.b  0(a2,d0.w),1(a5)		; The second digit

  move.w  #0,2(a5)			; Null-Terminate it!

  subq.l  #1,d3				; The next hex number
  add.l   #44,a3			; The next gadget
  add.l   #20,a4			; The next intuitext
  add.l   #4,a5				; The next gadgettext

  dbf     d5,CreateHexGadgets3		; The next x position
  dbf     d4,CreateHexGadgets2		; The next y position
  move.l  #0,-44(a3)			; No next gadget
  rts


NewWindow
  dc.w 0,0,640,195
  dc.b 0,2
  dc.l IDCMP_CLOSEWINDOW!IDCMP_GADGETUP
  dc.l WFLG_DRAGBAR!WFLG_DEPTHGADGET!WFLG_CLOSEGADGET!WFLG_SMART_REFRESH!WFLG_RMBTRAP!WFLG_ACTIVATE
  dc.l SourceNameGadget,0,WindowTitle,0,0
  dc.w 640,195,640,195,WBENCHSCREEN


SourceNameGadget
  dc.l DestNameGadget
  dc.w 430,17,190,10,GFLG_GADGHCOMP,GACT_RELVERIFY,GTYP_STRGADGET
  dc.l SourceNameBorder,0,SourceNameIText,0,SourceNameInfo
  dc.w 0
  dc.l SourceName

SourceNameInfo
  dc.l SourceFileName,0
  dc.w 0,75,0,0,0,0,0,0
  dc.l 0,0,0

SourceNameIText
  dc.b 1,2,JAM1,0
  dc.w -56,0
  dc.l Font0,SourceNameText,0

SourceNameBorder
  dc.w -5,-3
  dc.b 2,3,JAM1,5
  dc.l SourceNameVectors,SourceNameBorder2

SourceNameBorder2
  dc.w -6,-4
  dc.b 1,2,JAM1,3
  dc.l SourceNameVectors2,SourceNameBorder3

SourceNameBorder3
  dc.w -4,-2
  dc.b 1,2,JAM1,3
  dc.l SourceNameVectors3,0

SourceNameVectors
  dc.w 0,0,196,0,196,12,0,12,0,0

SourceNameVectors2
  dc.w 2,14,198,14,198,2

SourceNameVectors3
  dc.w 0,10,0,0,194,0

DestNameGadget
  dc.l InvertGadget
  dc.w 430,35,190,10,GFLG_GADGHCOMP,GACT_RELVERIFY,GTYP_STRGADGET
  dc.l SourceNameBorder,0,DestNameIText,0,DestNameInfo
  dc.w 0
  dc.l DestName

DestNameInfo
  dc.l DestFileName,0
  dc.w 0,75,0,0,0,0,0,0
  dc.l 0,0,0

DestNameIText
  dc.b 1,2,JAM1,0
  dc.w -56,0
  dc.l Font0,DestNameText,0

InvertGadget
  dc.l FilterGadget
  dc.w 374,55,120,16,GFLG_GADGHIMAGE,GACT_RELVERIFY,GTYP_BOOLGADGET
  dc.l InvertBorder,InvertBorder3,InvertIText,0,0
  dc.w 0
  dc.l Invert

InvertIText
  dc.b 1,2,JAM1,0
  dc.w 36,4
  dc.l Font0,InvertText,0

InvertBorder
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l InvertVectors,InvertBorder2

InvertBorder2
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l InvertVectors2,0

InvertBorder3
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l InvertVectors2,InvertBorder4

InvertBorder4
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l InvertVectors,0

InvertVectors
  dc.w 0,16,0,0,120,0

InvertVectors2
  dc.w 0,16,120,16,120,0

FilterGadget
  dc.l HexGadgets
  dc.w 374,95,250,16,GFLG_GADGHIMAGE,GACT_RELVERIFY,GTYP_BOOLGADGET
  dc.l FilterBorder,FilterBorder3,FilterIText,0,0
  dc.w 0
  dc.l Filter

FilterIText
  dc.b 1,2,JAM1,0
  dc.w 49,4
  dc.l Font0,FilterText,0

FilterBorder
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l FilterVectors,FilterBorder2

FilterBorder2
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l FilterVectors2,0

FilterBorder3
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l FilterVectors2,FilterBorder4

FilterBorder4
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l FilterVectors,0

FilterVectors
  dc.w 0,16,0,0,250,0

FilterVectors2
  dc.w 0,16,250,16,250,0



HexBorder
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l HexVectors,HexBorder2

HexBorder2
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l HexVectors2,0

HexBorder3
  dc.w 0,0
  dc.b 2,3,JAM1,3
  dc.l HexVectors2,HexBorder4

HexBorder4
  dc.w 0,0
  dc.b 1,2,JAM1,3
  dc.l HexVectors,0

HexVectors
  dc.w 0,10,0,0,21,0

HexVectors2
  dc.w 0,10,21,10,21,0


Font0
  dc.l Font0Name
  dc.w 8
  dc.b 0,1


JHExtrasBase		dc.l 0
DosBase			dc.l 0
GfxBase			dc.l 0
IntuitionBase		dc.l 0
MyWindow		dc.l 0
MyRastPort		dc.l 0
MyUserPort		dc.l 0
MyProcess		dc.l 0
FileSize		dc.l 0
SourceMem		dc.l 0
DestMem			dc.l 0
StatusArgs		dc.l 0,0

Class			dc.l 0
IAddress		dc.l 0
Code			dc.w 0
Qualifier		dc.w 0

SysVersion		dc.w 0

QuitFlag		dc.b 0

VersionString		dc.b '$VER: '
WindowTitle		dc.b 'ByteFilter V1.20   05-Sep-1993',0
JHExtrasName		dc.b 'jhextras.library',0
Font0Name		dc.b 'topaz.font',0
HexNumbers		dc.b '0123456789ABCDEF',0

StatusFormat		dc.b '%-30s',0
GreetsString		dc.b 'Welcome to ByteFilter.',0
InvertString		dc.b 'Inverting...',0
WaitingString		dc.b 'Waiting for action.',0
FilteringSlowString	dc.b 'Filtering, dead slow mode.',0
FilteringFastString	dc.b 'Filtering, fast mode.',0
FilteringUltraFastString dc.b 'Filtering, ultra fast mode.',0
NoSourceString		dc.b 'Can''t open source file.',0
NoDestString		dc.b 'Can''t open destination file.',0
WriteErrorString	dc.b 'Read/Write error.',0

SourceNameText		dc.b 'Source',0
DestNameText		dc.b 'Dest  ',0
InvertText		dc.b 'Invert',0
FilterText		dc.b 'Begin the filtering',0

  SECTION GadgetStuff,BSS

HexGadgets		dcb.b 44*256,0
IntuiTexts		dcb.b 20*256,0
GadgetTexts		dcb.b 4*256,0

  SECTION OtherStuff,BSS

SourceFileName		dcb.b 75,0
DestFileName		dcb.b 75,0
HexFlags		dcb.b 256,0
StatusString		dcb.b 256,0

  END
