#bdchunkylib=10

;--------------------------------------------------------
; How to install it, if you need another lib's number :
;
; First : Change the value of #bdchklib to a free number.
; ------  ( Have a look to DefLibMan in BUM9 )
;
; Second: Make blitzlibs:libmacs.res resident
; ------  ,select only 'Make Smallest Code'
;         in the Compiler Options
;         and create an executable ( BDChunkyLib.obj )
;
; Third : Remake your Deflibs ( via DefLibMan for example )
; ------
;________________________________________________________

!libheader {#bdchunkylib,0,0,0,0}

!dumtoke{"NChunky","is a Chunky Buffer from the BDChunkyLib  ;-)",_toke}

!acommand {#long}
!args {#long,#long,#long}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_InitNChunky,0,0}
!name {"InitNChunky","NChunky#,Width.l,Height.l"}

!acommand {#long}
!args {#long,#long}
!libs {#graphicslib,#la6}
!subs {_NChunkyToBtmA,0,0}
!name {"NChunkyToBtmA","*AddrNChunky.l, *AddrBitMap.l"}

!acommand {#long}
!args {#long,#long}
!libs {#graphicslib,#la6}
!subs {_BtmToNChunkyA,0,0}
!name {"BtmToNChunkyA","*AddrBtm.l, *AddrNChunky.l"}

!astatement
!args {#long,#long,#long,#byte}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCPlot,0,0}
!name {"NCPlot","NChunky#, x.l, y.l, colour.b"}

!afunction {#byte}
!args {#long,#long,#long}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCPoint,0,0}
!name {"NCPoint","( NChunky#, x.l, y.l )"}

!astatement
!args {#long,#long,#long,#long,#long,#byte}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCBoxF,0,0}
!name {"NCBoxF","NChunky#, x1.l, y1.l, x2.l, y2.l, colour.b"}

!astatement
!args {#long,#long,#long,#long,#long,#byte}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCBox,0,0}
!name {"NCBox","NChunky#, x1.l, y1.l, x2.l, y2.l, colour.b"}

!acommand {#long}
!args {#long,#long}
!libs {#bdchunkylib,#ia0|#pd0,#bdchunkylib,#ia1|#pd1}
!subs {_CopyNChunky,0,0}
!name {"CopyNChunky","NChunkySrc#, NChunkyDest#"}

!astatement
!args {#long,#byte}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCCls,0,0}
!args {#long}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_NCCls1,0,0}
!name {"NCCls","NChunky# [,colour.b ]"}

!acommand {#long}
!args {#long,#long}
!libs {#bdchunkylib,#ia0|#pd0,#bitmaplib,#ia1|#pd1,#graphicslib,#la6}
!subs {_NChunkyToBtm,0,0}
!name {"NChunkyToBtm","NChunky#, BitMap#"}

!acommand {#long}
!args {#long,#long}
!libs {#bitmaplib,#ia1|#pd0,#bdchunkylib,#ia0|#pd1,#graphicslib,#la6}
!subs {_BtmToNChunky,0,0}
!name {"BtmToNChunky","BitMap#, NChunky#"}

!afunction {#long}
!args {#long,#long}
!libs
!subs {_InitNChunkyA,0,0}
!name {"InitNChunkyA","( Width.l,Height.l )"}

!acommand {#long}
!args {#long}
!libs
!subs {_FreeNChunkyA,0,0}
!name {"FreeNChunkyA","( *AddrNChunky )"}

!astatement
!args {#long,#long,#long,#byte}
!libs
!subs {_NCPlotA,0,0}
!name {"NCPlotA","*AddrNChunky.l, x.l, y.l, colour.b"}

!afunction {#byte}
!args {#long,#long,#long}
!libs
!subs {_NCPointA,0,0}
!name {"NCPointA","( *AddrNChunky.l, x.l, y.l )"}

!astatement
!args {#long,#long,#long,#long,#long,#byte}
!libs
!subs {_NCBoxFA,0,0}
!name {"NCBoxFA","*AddrNChunky.l ,x1.l ,y1.l ,x2.l ,y2.l ,colour.b"}

!astatement
!args {#long,#long,#long,#long,#long,#byte}
!libs
!subs {_NCBoxA,0,0}
!name {"NCBoxA","*AddrNChunky.l ,x1.l ,y1.l ,x2.l ,y2.l ,colour.b"}

!acommand {#long}
!args {#long,#long}
!libs
!subs {_CopyNChunkyA,0,0}
!name {"CopyNChunkyA","*AddrNChunkySrc.l ,*AddrNChunkyDest.l"}

!astatement
!args {#long,#byte}
!libs
!subs {_NCClsA,0,0}
!name {"NCClsA","*AddrNChunky.l ,colour.b"}

!astatement
!args {#long,#long,#long}
!libs
!subs {_RemapNChunkyLineA,0,0}
!name {"RemapNChunkyLineA","*AddrNChunky.l ,y.l ,&RemapTable.l"}

!astatement
!args {#long,#long,#long}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_RemapNChunkyLine,0,0}
!name {"RemapNChunkyLine","NChunky# ,y.l ,&RemapTable.l"}

!astatement
!args {#long,#long,#long,#long}
!libs
!subs {_RemapNChunkyPointA,0,0}
!name {"RemapNChunkyPointA","*AddrNChunky.l ,x, y.l ,&RemapTable.l"}

!astatement
!args {#long,#long,#long,#long}
!libs {#bdchunkylib,#ia3|#pd0}
!subs {_RemapNChunkyPoint,0,0}
!name {"RemapNChunkyPoint","NChunky# ,x.l ,y.l ,&RemapTable.l"}



_load:!nullsub{0,0,0}
_save:!nullsub{0,0,0}
_use:!nullsub{0,0,0}
_free:!nullsub{_FreeNChunky,0,0}

!libfin{_toke,_load,_save,_use,_free,50,5}

!libfin

;
; Definition d'un NChunky...
;

NEWTYPE.NChunky
  Width.l       ; Width of picture
  Height.l      ; Height of picture
  Size.l        ; Size of buffer
  AWidth.l      ; Aligned Width for buffer
  *ChunkyData.b ; The datas
End NEWTYPE

;-----------------------------
;-----------------------------
._InitNChunkyA:
  MOVE.l  d1,d2         ;d2=height
  MOVE.l  d0,d1         ;d1=width
  MOVEM.l d1/d2,-(a7)
  MOVE.l  #32,d0        ;Size of object NChunky
  MOVE.l  #$10001,d1    ;Mem Type   = #MEMF_PUBLIC|#MEMF_CLEAR
  ALibJsr $c002         ;Alloc mem for NChunky object
  MOVEA.l d0,a3
  MOVEM.l (a7)+,d1/d2
  BRA     _NChunkyEmpty
._InitNChunky:
  MOVE.l  8(a3),d0      ; Test SizeFast
  BEQ     _NChunkyEmpty ; => Empty => Ok
  MOVEA.l 16(a3),a1     ;
  MOVEM.l d1/d2,-(a7)   ;
  ALibJsr $c003         ; Free Old NChunky
  MOVEM.l (a7)+,d1/d2   ;
_NChunkyEmpty:          ; Alloc a new NChunky
  MOVE.l  d1,d0         ;
  ADD.l   #15,d0        ;
  AND.l   #$FFFFFFF0,d0 ;
  MULU    d2,d0         ;SizeFast.l = (((w+15) LSL 4) LSR 4)*h
  MOVEM.l d0-d3,-(a7)
  MOVE.l  #$10001,d1    ;Mem Type   = #MEMF_PUBLIC|#MEMF_CLEAR
  ALibJsr $c002
  CMP     #0,d0
  BEQ     EndInitNChunky
  MOVE.l  d0,16(a3)
  MOVEM.l (a7)+,d0-d3
  MOVE.l  d1,  (a3)     ;Store w
  MOVE.l  d2, 4(a3)     ;Store h
  MOVE.l  d0, 8(a3)     ;Store SizeFast
  ADD.l   #15,d1        ;
  LSR     #3,d1         ;
  AND.w   #$FFFE,d1     ;
  LSL     #3,d1         ;
  MOVE.l  d1,12(a3)     ;Aligned Width=(((w+15) LSR 3) AND $fffe) lsl 3
  MOVE.l  a3,d0
EndInitNChunky:
  RTS

;-----------------------------
;-----------------------------
._FreeNChunkyA:
  MOVEA.l d0,a3
  MOVEQ   #0,d5
  BSR     _FreeNChunky
  TST     d5
  BEQ     _FreeDoneA
  MOVEA.l a3,a1
  MOVE.l  #32,d0
  ALibJsr $c003
  MOVEQ   #-1,d5
_FreeDoneA:
  MOVE.l  d5,d0
  RTS

._FreeNChunky:
  MOVE.l  8(a3),d0  ;Get SizeFast
  BEQ     _FreeDone
  MOVEA.l 16(a3),a1 ;Get Chunky Addr
  ALibJsr $c003
  CLR.l 4(a3)       ;Clear NChunky
  CLR.l 8(a3)       ;
  CLR.l 12(a3)      ;
  CLR.l 16(a3)      ;
  CLR.l (a3)        ;
_FreeDone
  MOVEQ #0,d5
  RTS

;-----------------------------
_AllocRst
  MOVE.l  #$10000,d1         ;#MEMF_ANY|#MEMF_CLEAR
  MOVE.l  #100,d0            ;SizeOf.RastPort
  ALibJsr $c002              ;AllocMem
  RTS

;-----------------------------
_PrepareMoveData
  MOVE.l  0(a0),w           ;Get w
  MOVE.l  4(a0),h           ;Get h
  MOVEA.l 16(a0),a0
  MOVE.l  a0,chky           ;Get chky
  MOVE.l  a1,btm            ;Get btm
  BSR     _AllocRst         ;Alloc Rastport 1
  MOVE.l  d0,rst1           ;
  BNE     _AllocRst2        ;No Rastport ?
  MOVEQ   #0,d5             ;succes=0
  BRA    _Fin1
_AllocRst2
  BSR     _AllocRst         ;Alloc Rastport 2
  MOVE.l  d0,rst2           ;
  BNE     _InitBtm          ;No Rastport ?
  MOVEA.l rst1,a1           ;
  MOVE.l  #100,d0           ;
  ALibJsr $c003             ;FreeMem of Rastport 1
  MOVEQ   #0,d5             ;succes=0
  BRA     _Fin1
_InitBtm
  MOVE.l  w,d0
  ADD.l   #15,d0
  LSL     #4,d0
  LSR     #1,d0             ;sizex     =((w+15) LSL 4) LSR 1
  MOVEQ   #1,d1             ;sizey     =1
  MOVEA.l btm,a0
  MOVEQ   #0,d2
  MOVE.b  5(a0),d2          ;depth     =*AddrBtm\Depth
  MOVEQ   #1,d3             ;flags     =#BMF_CLEAR
  MOVEQ   #0,d4             ;friend btm=0
  JSR     -918(a6)          ;AllocBitMap
  MOVE.l  d0,btm2           ;Get btm2
  CMP     #0,d0
  BNE     _InitRst
  MOVEA.l rst1,a1
  MOVE .l #100,d0
  ALibJsr $c003
  MOVEA.l rst2,a1
  MOVE.l  #100,d0
  ALibJsr $c003
  MOVEQ   #0,d5             ;succes=0
  BRA     _Fin1
_InitRst
  MOVEA.l rst1,a1
  JSR     -198(a6)          ;InitRastPort
  MOVEA.l rst2,a1
  JSR     -198(a6)
  MOVEA.l rst1,a0           ;
  MOVEA.l rst2,a1           ;
  MOVE.l  #0,(a1)           ;rst2\Layer=0
  MOVE.l  btm2,4(a1)        ;rst2\BitMap=btm2
  MOVE.l  btm ,4(a0)        ;rst1\BitMap=btm
  MOVEQ   #0,d0             ;
  MOVEQ   #0,d1             ;
  MOVE.l  w,d2              ;
  MOVE.l  h,d3              ;
  SUBQ    #1,d2             ;
  SUBQ    #1,d3             ;
  MOVEA.l chky,a2           ;
  MOVEQ   #1,d5             ;succes=1
_Fin1:
  RTS

_FreeData:
  MOVEA.l btm2,a0
  JSR     -924(a6)          ;FreeBitMap
  MOVEA.l rst1,a1
  MOVE.l  #100,d0
  ALibJsr $c003
  MOVEA.l rst2,a1
  MOVE.l  #100,d0
  ALibJsr $c003
  RTS

;-----------------------------
;-----------------------------
._NChunkyToBtmA:
  MOVEA.l d1,a1
  MOVEA.l d0,a0
._NChunkyToBtm:
  BSR     _PrepareMoveData  ;
  TST     d5
  BEQ     _EndNCtoB
  JSR     -786(a6)          ;WritePixelArray8
  MOVE.l  d0,d5
  BSR     _FreeData
_EndNCtoB:
  MOVE.l  d5,d0
  RTS

;-----------------------------
;-----------------------------
._BtmToNChunkyA:
  MOVEA.l d0,a1
  MOVEA.l d1,a0
._BtmToNChunky:
  BSR     _PrepareMoveData  ;
  TST     d5
  BEQ     _EndBtoNC
  JSR     -780(a6)          ;ReadPixelArray8
  MOVE.l  d0,d5
  BSR     _FreeData
_EndBtoNC:
  MOVE.l  d5,d0
  RTS

;-----------------------------
;-----------------------------
._NCPlotA:
  MOVEA.l d0,a3
._NCPlot:
  MOVE.l  12(a3),d0           ; d0=AWidth
  MOVEA.l 16(a3),a0           ; a0=*ChunkyData
  MULU    d2,d0               ; d0=y*AWidth
  ADD.l   d1,d0               ; d0=d0+x
  MOVE.b  d3,0(a0,d0.l)       ;
  RTS

;-----------------------------
;-----------------------------
._NCPointA:
  MOVEA.l d0,a3
._NCPoint:
  MOVE.l  12(a3),d0
  MOVEA.l 16(a3),a0
  MULU    d2,d0
  ADD.l   d1,d0
  MOVE.b  0(a0,d0.l),d0
  RTS

;-----------------------------
;-----------------------------
._RemapNChunkyLineA:
  MOVEA.l d0    ,a3           ; a3=Addr NChunky
._RemapNChunkyLine:
  MOVE.l    (a3),d4           ;
  SUBQ    #1,d4               ; d4=Width-1
  MOVE.l  12(a3),d0           ; d0=AWidth
  MOVEA.l 16(a3),a0           ; a0=*ChunkyData
  MOVEA.l d2,a1               ; a1=&RemapTable(0)
  MULU    d0,d1               ; d1=AWidth*Y
  MOVEQ   #0,d6               ; X=0
_RemapLine:
  MOVE.l  d1,d7               ; d7=AWidth*Y
  ADD.l   d6,d7               ; d7=X+(AWidth*Y)
  MOVEQ   #0,d5               ; d5=0
  MOVE.b  0(a0,d7.l),d5       ; NCPoint
  ANDI.w  #$FF,d5             ; d5 => 0 a 255
  MOVE.b  0(a1,d5.w),d0       ; d0=RemapTable(d5)
  MOVE.b  d0,0(a0,d7.l)       ; NCPlot
  ADDQ    #1,d6               ; d6=X+1
  CMP.l   d6,d4               ; Tst d6=d4 ?
  BPL     _RemapLine          ;
  RTS

;-----------------------------
;-----------------------------
._RemapNChunkyPointA:
  MOVEA.l d0    ,a3           ; a3=Addr NChunky
._RemapNChunkyPoint:
  MOVE.l    (a3),d4           ;
  MOVE.l  12(a3),d0           ; d0=AWidth
  MOVEA.l 16(a3),a0           ; a0=*ChunkyData
  MOVEA.l d3,a1               ; a1=&RemapTable(0)
  MULU    d0,d2               ; d2=AWidth*Y
  ADD.l   d1,d2               ; d2=X+(AWidth*Y)
  MOVEQ   #0,d5               ; d5=0
  MOVE.b  0(a0,d2.l),d5       ; NCPoint
  ANDI.w  #$FF,d5             ; d5 => 0 a 255
  MOVE.b  0(a1,d5.w),d0       ; d0=RemapTable(d5)
  MOVE.b  d0,0(a0,d2.l)       ; NCPlot
  RTS

;-----------------------------
;-----------------------------
._NCBoxFA:
  MOVEA.l d0,a3
._NCBoxF:
_LoopBoxFY:
  MOVE.l  12(a3),d0       ; d0=AWidth
  MOVEA.l 16(a3),a0       ;
  MULU    d2,d0           ; d0=y1*AWidth
  MOVE.l  d1,d6           ; d6=x1
_LoopBoxFX:
  MOVE.l  d0,d7           ; d7=y1*AWidth
  ADD.l   d6,d7           ; d7=x1+(y1*AWidth)
  MOVE.b  d5,0(a0,d7.l)   ; Poke d5
  ADDQ    #1,d6           ; d6=x1+1
  CMP.l   d6,d3           ; Tst d6=d3 ?
  BPL     _LoopBoxFX      ;
  ADDQ    #1,d2           ; d2=y1+1
  CMP.l   d2,d4           ; Tst d2=d4 ?
  BPL     _LoopBoxFY      ;
  RTS

;-----------------------------
;-----------------------------
._NCBoxA:
  MOVEA.l d0,a3
._NCBox:

  RTS

;-----------------------------
;-----------------------------
._CopyNChunkyA:
  MOVEA.l d0,a0
  MOVEA.l d1,a1
._CopyNChunky:
  MOVE.l  8(a0),d2
  MOVE.l  8(a1),d3
  CMP.l   d2,d3
  BEQ     _DoCopy
  MOVEQ   #0,d5
  BRA     _ErrorCopy
_DoCopy:
  MOVEA.l 16(a0),a0
  MOVEA.l 16(a1),a1
  MOVE.l  d2,d0
  MOVE.l  4,a6
  JSR     -624(a6)    ;CopyMem(a0/a1,d0)
  MOVEQ   #-1,d5
_ErrorCopy:
  MOVE.l  d5,d0
  RTS

;-----------------------------
;-----------------------------
._NCClsA:
  MOVEA.l d0,a3
  BRA     _NCCls
_NCCls1:
  MOVEQ   #0,d1
._NCCls:
  MOVEQ   #0,d5     ;
  MOVE.b  d1,d5     ; d5=color.b
  MOVEQ   #0,d1     ; d1=x1=0
  MOVEQ   #0,d2     ; d2=y1=0
  MOVE.l  (a3),d3   ; d3=x2=width
  MOVE.l  4(a3),d4  ; d4=x3=height
  SUBQ    #1,d3     ; width-1      !!!!
  SUBQ    #1,d4     ; height-1     !!!!
  BSR     _NCBoxF   ; Do a filled Box
  RTS

;-----------------------------
;-----------------------------
Even
w:        Dc.l 0
h:        Dc.l 0
rst1:     Dc.l 0
rst2:     Dc.l 0
btm:      Dc.l 0
btm2:     Dc.l 0
chky:     Dc.l 0
version:  Dc.b "$VER: BD-Chunky-Library v1.0 (20/05/1998)",0
Even
