; last update 13/12/2003
*************************************


ReadPixelLine8:
  lea -56(A7),A7
  movem.l D2-D4/A2/A6,-(A7)         ; 20 bytes used
  move.l A0,D4                      ; if( rp != NULL && sizex > 0 )
  movea.w D0,A6                     ;
  move.w D2,D3                      ;
  tst.l D4                          ;
   beq.b JL_0_1AE                   ;
  tst.w D2                          ;
   beq.b JL_0_1AE                   ; {
  lea FillRoutine(PC),a0   ; Args.Hook.h_Entry   = (HOOKFUNC)FillRoutine;
  move.l a0,38(A7)
  lea 30(A7),A0                    ; Args.Hook.h_Data    = &Args;
  move.l A0,46(A7)                 ;
  move.l A2,54(A7)                 ; Args.ChunkyBuffer   = array;
  clr.l 58(A7)                     ; Args.SrcX           = 0;
                                    ; Args.SrcY           = 0;
  move.w D0,62(A7)                 ; Args.DstX           = destx;
  move.w D1,64(A7)                 ; Args.DstY           = desty;
  move.w D2,66(A7)                 ; Args.SizeX          = sizex;
  move.l #$00010000,68(A7)         ; Args.SizeY          = sizey;
                                   ; Args.Pixels         = 0L;
  move.l A1,50(A7)                 ; Args.trp            = trp;
  move.w D0,22(A7)                 ; Bounds.MinX = destx;
  adda.w D3,A6                      ; Bounds.MaxX = destx + sizex - 1;
  subq.w #1,A6                      ;
  move.w A6,26(A7)                 ;
  move.w D1,24(A7)                 ; Bounds.MinY = desty;
  move.w D1,28(A7)                 ; Bounds.MaxY = desty + sizey - 1;    sizey is 1 -> sizey - 1 = 0
  movea.l PLayersBase(PC),A6        ; DoHookClipRects(&Args.Hook,rp,&Bounds);
  movea.l D4,A1                     ;
  lea 22(A7),A2                    ;
   jsr _LVODoHookClipRects(A6)      ;
  move.w 70(A7),D0                 ; return Args.Pixels
  ext.l D0
  movem.l (A7)+,D2-D4/A2/A6         ;
  lea 56(A7),A7                    ;
 rtS                                ; }
 cnop 0,4


JL_0_1AE:
  clr.l D0                          ; return 0L
  movem.l (A7)+,D2-D4/A2/A6         ;
  lea 56(A7),A7                    ;
 rtS                                ;
 cnop 0,4
;------------------------------------------------------------------------------


FillRoutine:        ; a0 -> Hook, a1 -> Bounds, a2 -> RPort
PlanarToChunky:
    movem.l d2-d7/a2-a6,-(sp)
*************************************
  lea -20(A7),A7
  movea.l A2,A4
  movea.l $10(A0),A2             ; Args=Hook->h_Data
  movea.w $1C(A2),A3             ; srcx=Args->srcx
  move.w $1E(A2),D5              ; srcy=Args->srcy
  movea.w $24(A2),A0             ; BufWidth = ((Args->SizeX+15)>>4)<<4;
  moveq.l #$0F,D0                ;
  add.l A0,D0                    ;
  asr.l #4,D0                    ;
  move.w D0,D6                   ;
  lsl.w #4,D6                    ;
  tst.l (A1)                     ; if(Bounds->Layer != NULL)
   beq.b JL_0_1F6                ; {
  move.w $E(A1),D0               ;     SrcX += Bounds->OffsetX - Args->DstX;
  sub.w $20(A2),D0               ;
  adda.w D0,A3                   ;
  move.w $12(A1),D0              ;     SrcY += Bounds->OffsetY - Args->DstY;
  sub.w $22(A2),D0               ;
  add.w D0,D5                    ; }
JL_0_1F6:
  move.w $4(A1),D3               ; DstX  = Bounds->Bounds.MinX;
  move.w $6(A1),D4               ; DstY  = Bounds->Bounds.MinY;
  move.w $8(A1),D2               ; SizeX = Bounds->Bounds.MaxX - Bounds->Bounds.MinX + 1;
  sub.w D3,D2                    ;
  addq.w #1,D2                   ;
  move.w $A(A1),D1               ; SizeY = Bounds->Bounds.MaxY - Bounds->Bounds.MinY + 1;
  sub.w D4,D1                    ;
  addq.w #1,D1                   ; Args->Pixels += SizeX*SizeY;
  move.w D2,D0                   ;
  muls D1,D0                     ;
  add.w D0,$28(A2)               ;
  move.l $4(A4),(A7)             ; :- BitMap
  move.w D3,startX(A7)           ; :- startx
  move.w D4,startY(A7)           ; :- starty
  move.w D2,width(A7)            ; :- width
  move.w D1,height(A7)           ; :- eigth
  move.w D5,D0                   ; p2c.chunkybuffer = &Args->ChunkyBuffer[(SrcY*BufWidth)+SrcX];
  muls D6,D0                     ;
  movea.l D0,A1                  ;
  lea $0(A1,A3.W),A0             ;
  adda.l $18(A2),A0              ;
  move.l A0,chunkybuffer(A7)     ; :- chunkybuffer
  movea.l A7,A0              ;
*************************************
    lea.l   -sizeof_buffer(sp),sp   


    move.l  (a0),a1
    move.l  chunkybuffer(a0),a3
    lea p2c(pc),a4


    moveq   #0,d1
    move.w  startX(a0),d0
    andi.w  #%111,d0
    tst.w   d0
    beq.s   noleftpix
    moveq   #8,d1
    sub.w   d0,d1
noleftpix
    move.w  d1,leftpix(sp)


    move.w  width(a0),d0
    move.w  d0,Width(sp)
hit cmp.w   d1,d0
    bhs.s   notinsidebyte
    move.b  #1,insidebyte(sp)
    moveq   #0,d1
    bra.s   isinside
 cnop 0,4


notinsidebyte
    clr.b   insidebyte(sp)
    move.w  Width(sp),d0
    sub.w   leftpix(sp),d0
    move.w  d0,d1
    lsr.w   #5,d0
    lsl.w   #5,d0
    sub.w   d0,d1
isinside
    move.w  d1,rightpix(sp)


    * find initial offset in bytes into bitmap given by (startX, startY)
    moveq   #0,d0
    move.w  startX(a0),d0
    lsr.w   #3,d0
    moveq   #0,d2
    move.w  (a1),d2
    move.b  Flags(a1),d3
    and.b   #BMF_INTERLEAVED,d3
    beq.b   1$
    divu.w  Depth(a1),d2
1$  move.w  startY(a0),d1
    tst.w   d1
    beq.s   line0
    subq    #1,d1
makeoffset
    add.l   d2,d0
    dbra    d1,makeoffset


line0   move.l  d0,a2


    tst.w   leftpix(sp)
    beq.s   nol1
    subq    #3,a2


nol1    move.w  Width(sp),d0
    sub.w   rightpix(sp),d0
    move.w  d2,d1
    lsr.w   #3,d0
    sub.w   d0,d1
    move.w  d1,Modulo(sp)


    tst.b   insidebyte(sp)
    bne.s   nol2
    tst.w   leftpix(sp)
    beq.s   nol2
    subq    #4,Modulo(sp)
nol2


    move.w  height(a0),Height(sp)


    ; a0 - c2p struct, a1 - bmap,
    ; a2 - offset from Planes pointer, a3 - chunkybuffer
    ; a4 - bitspread table


    move.l  a1,a6
    addq.w   #Planes,a6
    moveq   #0,d0
    move.b  Depth(a1),d0
    move.w  d0,depth(sp)
    lsl.w   #2,d0
    add.l   d0,a6
    move.l  a6,lastplaneptr(sp)


convertline
    tst.b   insidebyte(sp)
    beq.s   notinsidesinglebyte
    bsr.w   convert32pixels
    lea buffer+32(sp),a5
    movem.l d0-d7,-(a5)
    moveq   #32,d0
    sub.w   leftpix(sp),d0
    add.l   d0,a5
    move.w  Width(sp),d0
    subq    #1,d0
singleinsert
    move.b  (a5)+,(a3)+
    dbra    d0,singleinsert
    bra.b   linedone
 cnop 0,4


notinsidesinglebyte
    tst.w   leftpix(sp)
    beq.s   noleftpixels


    tst.w   Width(sp)
    beq.b   linedone


    bsr.w   convert32pixels
    lea buffer+32(sp),a5
    movem.l d0-d7,-(a5)
    moveq   #32,d0
    sub.w   leftpix(sp),d0
    add.l   d0,a5
    move.w  leftpix(sp),d0
    move.w  d0,d1
    subq    #1,d0
leftinsert
    move.b  (a5)+,(a3)+
    dbra    d0,leftinsert
    sub.w   d1,Width(sp)
    addq    #4,a2
    
noleftpixels
    cmp.w   #32,Width(sp)
    blo.s   trailbits


    bsr.b   convert32pixels


flushall
    add.w   #32,a3
    movem.l d0-d7,-(a3)
    add.w   #32,a3


nolongflush
    sub.w   #32,Width(sp)
    addq    #4,a2
    bra.b   noleftpixels
 cnop 0,4


trailbits
    tst.w   Width(sp)
    beq.s   linedone


    bsr.b   convert32pixels
    lea buffer+32(sp),a5
    movem.l d0-d7,-(a5)
    move.w  rightpix(sp),d0
    subq    #1,d0
rightinsert
    move.b  (a5)+,(a3)+
    dbra    d0,rightinsert


linedone
    move.w  width(a0),Width(sp)
    add.w   Modulo(sp),a2
    subq    #1,Height(sp)
    tst.w   Height(sp)
    bne.w   convertline


    lea.l   sizeof_buffer+20(sp),sp


    movem.l (sp)+,d2-d7/a2-a6
    rts
 cnop 0,4


;------------------------------------------------------------------------------ 




convert32pixels:
    move.l  a3,-(sp)
    move.l  a0,-(sp)
    move.w  depth+12(sp),a3
    move.l  lastplaneptr+12(sp),a6
    
    move.l  -(a6),a5
    add.l   a2,a5
    clr.w   d7
    move.b  (a5)+,d7
    lsl.w   #3,d7
    movem.l 0(a4,d7.w),d0/d1
    clr.w   d7
    move.b  (a5)+,d7
    lsl.w   #3,d7
    movem.l 0(a4,d7.w),d2/d3
    clr.w   d7
    move.b  (a5)+,d7
    lsl.w   #3,d7
    movem.l 0(a4,d7.w),d4/d5
    clr.w   d7
    move.b  (a5)+,d7
    lsl.w   #3,d7
    movem.l 0(a4,d7.w),d6/d7


convert32pixelsloop
    subq.w  #1,a3
    tst.w a3 ;cmp.w   #0,a3
    beq.s   done


    move.l  -(a6),a5
    add.l   a2,a5
    move.w  d7,a0
    clr.w   d7


    move.b  (a5)+,d7
    lsl.w   #3,d7
    add.l   d0,d0
    or.l    0(a4,d7.w),d0
    add.l   d1,d1
    or.l    4(a4,d7.w),d1
    clr.w   d7


    move.b  (a5)+,d7
    lsl.w   #3,d7
    add.l   d2,d2
    or.l    0(a4,d7.w),d2
    add.l   d3,d3
    or.l    4(a4,d7.w),d3
    clr.w   d7


    move.b  (a5)+,d7
    lsl.w   #3,d7
    add.l   d4,d4
    or.l    0(a4,d7.w),d4
    add.l   d5,d5
    or.l    4(a4,d7.w),d5
    move.w  a0,d7
    move.w  d5,a0
    clr.w   d5


    move.b  (a5)+,d5
    lsl.w   #3,d5
    add.l   d6,d6
    or.l    0(a4,d5.w),d6
    add.l   d7,d7
    or.l    4(a4,d5.w),d7
    move.w  a0,d5


    bra.s   convert32pixelsloop
 cnop 0,4


done    move.l  (sp)+,a0
    move.l  (sp)+,a3
    rts
 cnop 0,4


  cnop 0,4

;------------------------------------------------------------------------------
* bitspread table *
p2c ds.b    256*8


;------------------------------------------------------------------------------


 
