home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / xlib / xpbmclip.asm < prev    next >
Encoding:
Assembly Source File  |  1992-11-12  |  34.7 KB  |  1,073 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XPBMCLIP
  3. ;
  4. ; Clipped Planar Bitmap functions - System Ram <-> Video Ram
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17.  
  18.  
  19. COMMENT $
  20.  
  21.   This module implements a set of functions to operate on planar bitmaps.
  22.   Planar bitmaps as used by these functions have the following structure:
  23.  
  24.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  25.   BYTE 1                 The bitmap height in rows range 1..255
  26.   BYTE 2..n1             The plane 0 pixels width*height bytes
  27.   BYTE n1..n2            The plane 1 pixels width*height bytes
  28.   BYTE n2..n3            The plane 2 pixels width*height bytes
  29.   BYTE n3..n4            The plane 3 pixels width*height bytes
  30.  
  31.   These functions provide the fastest possible bitmap blts from system ram to
  32.   to video and further, the single bitmap is applicable to all pixel
  33.   allignments. The masked functions do not need separate masks since all non
  34.   zero pixels are considered to be masking pixels, hence if a pixel is 0 the
  35.   corresponding screen destination pixel is left unchanged.
  36.  
  37.  
  38. $
  39. LOCALS
  40. include xlib.inc
  41. include xpbmclip.inc
  42.  
  43.     .code
  44.  
  45. ;----------------------------------------------------------------------
  46. ; x_put_masked_pbm_clipx - mask write a planar bitmap from system ram to video
  47. ;            ram all zero source bitmap bytes indicate destination
  48. ;            byte to be left unchanged.
  49. ;                       Performs clipping in x directions. similar to
  50. ;                "x_put_masked_pbm".
  51. ;
  52. ; See Also:  x_put_masked_pbm, x_put_masked_pbm_clipxy
  53. ;
  54. ; Clipping region variables: LeftClip,RightClip
  55. ;
  56. ; Written by Themie Gouthas
  57. ;
  58. ; This code is a SLOW hack, any better ideas are welcome
  59. ;----------------------------------------------------------------------
  60. _x_put_masked_pbm_clipx  proc
  61. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  62. LOCAL   Plane:byte,CType,LeftSkip,DataInc,Width,Height,TopRow,LineInc:word=LocalStk
  63.     push  bp
  64.         mov   bp,sp
  65.     sub   sp,LocalStk                 ; Create space for local variables
  66.         push  si
  67.         push  di
  68.         push  ds
  69.     cld
  70.  
  71.     les   si,[Bitmap]                 ; Point ES:SI to start of bitmap
  72.  
  73.     xor   ax,ax                       ; Clear AX
  74.     mov   [CType],ax                  ; Clear Clip type descision var
  75.     mov   al,byte ptr es:[si]         ; AX=width (byte coverted to word)
  76.  
  77.  
  78.     mov   di,[X]                      ; DI = X coordinate of dest
  79.     mov   cx,di                       ; copy to CX
  80.     shr   di,2                        ; convert to offset in row
  81.  
  82.  
  83.     ;;;;; CLIP PROCESSING FOR LEFT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  84.  
  85.     mov   dx,[_LeftClip]              ; Is X Coord to the right of
  86.     sub   dx,di                       ; LeftClip ?
  87.     jle   @@NotLeftClip               ; Yes! => no left clipping
  88.     cmp   dx,ax                       ; Is dist of X Coord from
  89.     jnl   @@NotVisible                ; ClipLeft > Width ? yes => the
  90.                       ; bitmap is not visible
  91.     add   di,dx
  92.     mov   [LeftSkip],dx
  93.     mov   [DataInc],dx
  94.     sub   ax,dx
  95.     mov   [CType],1
  96.     jmp   short @@HorizClipDone
  97.  
  98.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  99.  
  100. @@NotVisible:
  101.     pop   ds                          ; restore data segment
  102.     pop   di                          ; restore registers
  103.     pop   si
  104.     mov   sp,bp                       ; dealloc local variables
  105.     pop   bp
  106.     ret
  107.  
  108.     ;;;;; CLIP PROCESSING FOR RIGHT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  109.  
  110. @@NotLeftClip:
  111.     mov   dx,[_RightClip]
  112.     sub   dx,di
  113.     js    @@NotVisible
  114.     mov   [LeftSkip],0
  115.     mov   [DataInc],0
  116.     cmp   dx,ax
  117.     jg    @@HorizClipDone
  118.     inc   dx
  119.     sub   ax,dx
  120.     mov   [DataInc],ax
  121.     mov   ax,dx
  122.     mov   [CType],-1
  123.  
  124. @@HorizClipDone:
  125.  
  126.         xor   bh,bh
  127.     mov   bl,byte ptr es:[si+1]       ; BX = height
  128.  
  129.     mov   [Width],ax                  ; Save width and height of clipped
  130.     mov   [Height],bx                 ;  image
  131.  
  132.  
  133.     add   si,2                        ; Skip dimension bytes in source
  134.     add   si,[LeftSkip]               ; Skip pixels in front of row that
  135.                       ;  are clipped
  136.  
  137.  
  138.     mov   bx,[_ScrnLogicalByteWidth]  ; Set BX to Logical Screen Width
  139.     mov   dx,bx                       ; BX - Width of image = No. bytes
  140.     sub   dx,ax                       ;  to first byte of next screen
  141.     mov   [LineInc],dx                ;  row.
  142.  
  143.     mov   ax,[Y]                      ; Calculate screen start row
  144.     mul   bx                          ;  then adding screen offset
  145.     add   di,ax
  146.     add   di,[ScrnOffs]
  147.     mov   ax,es                       ; copy ES to DS
  148.     mov   ds,ax
  149.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  150.     mov   es,ax
  151.  
  152.         and   cx,3
  153.     mov   ah,11h                      ; Set up initial plane mask
  154.     shl   ah,cl
  155.  
  156.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  157.         mov   al,MAP_MASK
  158.         out   dx,al
  159.         inc   dx
  160.     mov   [Plane],4                   ; Set plane counter to 4
  161.     mov   bh,byte ptr [Width]         ; set bh to width for fast looping
  162. @@PlaneLoop:
  163.     push  di               ; Save bitmap's start dest. offset
  164.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  165.         mov   al,ah
  166.         out   dx,al                       ; set vga write plane
  167. @@RowLoop:
  168.     mov   cl,bh                       ; Reset Column counter cl
  169.     jcxz  @@NoWidth
  170. @@ColLoop:
  171.     lodsb                          ; Get next source bitmap byte
  172.     or    al,al                       ; If not zero then write to dest.
  173.     jz    @@NoPixel                   ; otherwise skip to next byte
  174.     mov   es:[di],al
  175. @@NoPixel:
  176.     inc   di
  177.     loop @@ColLoop
  178. @@NoWidth:
  179.     add   si,[DataInc]                ; Move to next source row
  180.     add   di,[LineInc]                ; Move to next screen row
  181.         dec   bl                          ; decrement row counter
  182.     jnz   @@RowLoop                   ; Jump if more rows left
  183.         pop   di                          ; Restore bitmaps start dest byte
  184.     rol   ah,1                  ; Shift mask for next plane
  185.  
  186.     ; Plane Transition (A HACK but it works!)
  187.  
  188.     jnb   @@Nocarry                   ; Jump if not plane transition
  189.     mov   bl,ah                       ; Save Plane Mask
  190.     mov   ax,[CType]                  ; set AX to clip type inc variable
  191.     add   bh,al                       ; Update advancing variables
  192.     sub   [DataInc],ax                ;
  193.     sub   [LineInc],ax                ;
  194.     cmp   al,0                        ; What type of clip do we have
  195.     mov   ah,bl                       ;   restore Plane mask
  196.     jg    @@RightAdvance              ; jump on a right clip!
  197.     inc   di                          ; otherwise increment DI
  198.     jmp   @@Nocarry
  199. @@RightAdvance:
  200.     dec si
  201. @@Nocarry:
  202.     dec   [Plane]                     ; Decrement plane counter
  203.         jnz   @@PlaneLoop                 ; Jump if more planes left
  204.  
  205.  
  206.         pop   ds                          ; restore data segment
  207.         pop   di                          ; restore registers
  208.         pop   si
  209.         mov   sp,bp                       ; dealloc local variables
  210.         pop   bp
  211.         ret
  212. _x_put_masked_pbm_clipx  endp
  213.  
  214.  
  215. ;----------------------------------------------------------------------
  216. ; x_put_masked_pbm_clipy - mask write a planar bitmap from system ram to video
  217. ;            ram all zero source bitmap bytes indicate destination
  218. ;            byte to be left unchanged.
  219. ;                       Performs clipping in y direction. similar to
  220. ;                "x_put_masked_pbm".
  221. ;
  222. ; See Also:  x_put_masked_pbm, x_put_masked_pbm_clipx, x_put_masked_pbm_clipy
  223. ;
  224. ; Clipping region variables: TopClip,BottomClip
  225. ;
  226. ; Written by Themie Gouthas
  227. ;----------------------------------------------------------------------
  228. _x_put_masked_pbm_clipy  proc
  229. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  230. LOCAL   Width,Height,TopRow,LineInc,PlaneInc:word=LocalStk
  231.     push  bp
  232.         mov   bp,sp
  233.     sub   sp,LocalStk                 ; Create space for local variables
  234.         push  si
  235.         push  di
  236.         push  ds
  237.     cld
  238.  
  239.     les   si,[Bitmap]
  240.  
  241.     xor   bh,bh
  242.     mov   bl,byte ptr es:[si+1]       ; BX = height
  243.  
  244.     xor   ah,ah
  245.     mov   al,byte ptr es:[si]         ; AX = width
  246.  
  247.         mov   cx,ax                       ; Save AX
  248.     mul   bx                          ; AX = AX*BX = bytes/plane
  249.     mov   [PlaneInc],ax               ;  save as PlaneInc
  250.     mov   ax,cx                       ; Restore AX
  251.  
  252.         mov   di,[X]
  253.     mov   cx,di
  254.     shr   di,2
  255.  
  256.     ;;;;; CLIP PROCESSING FOR TOP CLIP BORDER ;;;;;;;;;;;;;;;;;;;;;
  257.  
  258.     mov   dx,[_TopClip]           ; Compare u.l. Y coord with Top
  259.     sub   dx,[Y]                  ; clipping border
  260.     jle   @@NotTopClip            ; jump if VBM not clipped from above
  261.     cmp   dx,bx
  262.     jnl   @@NotVisible            ; jump if VBM is completely obscured
  263.     mov   [TopRow],dx
  264.     sub   bx,dx
  265.     add   [Y],dx
  266.     jmp   short @@VertClipDone
  267.  
  268.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  269.  
  270. @@NotVisible:
  271.     pop   ds                          ; restore data segment
  272.     pop   di                          ; restore registers
  273.     pop   si
  274.     mov   sp,bp                       ; dealloc local variables
  275.     pop   bp
  276.     ret
  277.  
  278.     ;;;;; CLIP PROCESSING FOR BOTTOM CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  279.  
  280. @@NotTopClip:
  281.     mov   dx,[_BottomClip]
  282.     sub   dx,[Y]
  283.     js    @@NotVisible
  284.         mov   [TopRow],0
  285.     cmp   dx,bx
  286.     jg    @@VertClipDone
  287.     inc   dx
  288.     mov   bx,dx
  289.  
  290. @@VertClipDone:
  291.  
  292.     mov   [Width],ax
  293.     mov   [Height],bx                 ; Calculate relative offset in data
  294.     mul   [TopRow]                    ;  of first visible scanline
  295.     add   ax,2                        ; Skip dimension bytes in source
  296.     add   si,ax                       ; Skip top rows that arent visible
  297.  
  298.  
  299.     mov   ax,[Y]                      ; Calculate screen row
  300.     mov   bx,[_ScrnLogicalByteWidth]  ;  by mult. Y coord by Screen
  301.     mul   bx                          ;  width then adding screen offset
  302.     add   di,ax
  303.     add   di,[ScrnOffs]
  304.         sub   bx,[Width]                  ; calculate difference from end of
  305.     mov   [LineInc],bx                ; b.m. in curr line to beginning of
  306.                       ; b.m. on next scan line
  307.     mov   ax,es                       ; copy ES to DS
  308.     mov   ds,ax
  309.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  310.     mov   es,ax
  311.  
  312.     mov   ah,11h                      ; Set up initial plane mask
  313.         and   cx,3
  314.     shl   ah,cl
  315.  
  316.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  317.         mov   al,MAP_MASK
  318.         out   dx,al
  319.         inc   dx
  320.     mov   bh,4                        ; Set plane counter to 4
  321. @@PlaneLoop:
  322.     push  di               ; Save bitmap's start dest. offset
  323.     push  si                          ; Save Bitmaps data offset
  324.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  325.         mov   al,ah
  326.         out   dx,al                       ; set vga write plane
  327. @@RowLoop:
  328.     mov   cl,byte ptr [Width]         ; Reset Column counter cl
  329. @@ColLoop:
  330.         lodsb                             ; Get next source bitmap byte
  331.         or    al,al                       ; If not zero then write to dest.
  332.         jz    @@NoPixel                   ; otherwise skip to next byte
  333.         mov   es:[di],al
  334. @@NoPixel:
  335.         inc   di
  336.     loop  @@ColLoop                   ; loop if more columns left
  337.     add   di,[LineInc]                ; Move to next row
  338.         dec   bl                          ; decrement row counter
  339.     jnz   @@RowLoop                   ; Jump if more rows left
  340.     pop   si                          ; Restore SI and set to offset of
  341.     add   si,[PlaneInc]               ; first vis pixel in next plane data
  342.         pop   di                          ; Restore bitmaps start dest byte
  343.     rol   ah,1                        ; Shift mask for next plane
  344.     adc   di,0                        ; if carry increment screen offset
  345.     dec   bh                          ; Decrement plane counter
  346.         jnz   @@PlaneLoop                 ; Jump if more planes left
  347.  
  348.  
  349.         pop   ds                          ; restore data segment
  350.         pop   di                          ; restore registers
  351.         pop   si
  352.         mov   sp,bp                       ; dealloc local variables
  353.         pop   bp
  354.         ret
  355. _x_put_masked_pbm_clipy   endp
  356.  
  357. ;----------------------------------------------------------------------
  358. ; x_put_masked_pbm_clipxy - Write a planar bitmap from system ram to video
  359. ;                    RAM with clipping in x and y directions. similar to
  360. ;             "x_put_masked_pbm".
  361. ;
  362. ; See Also: x_put_masked_pbm, x_put_masked_pbm_clipx, x_put_masked_pbm_clipxy
  363. ;
  364. ; Clipping region variables: LeftClip,RightClip,TopClip,BottomClip
  365. ;
  366. ;
  367. ; Written by Themie Gouthas
  368. ;----------------------------------------------------------------------
  369. _x_put_masked_pbm_clipxy  proc
  370. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  371. LOCAL   Plane:byte,CType,LeftSkip,DataInc,Width,Height,TopRow,LineInc,PlaneInc:word=LocalStk
  372.     push  bp
  373.         mov   bp,sp
  374.     sub   sp,LocalStk                 ; Create space for local variables
  375.         push  si
  376.         push  di
  377.         push  ds
  378.     cld
  379.  
  380.     les   si,[Bitmap]
  381.  
  382.     xor   ax,ax
  383.     mov   [CType],ax
  384.     mov   al,byte ptr es:[si]         ; AX = width
  385.         xor   bh,bh
  386.     mov   bl,byte ptr es:[si+1]       ; BX = height
  387.  
  388.         mov   cx,ax                       ; Save AX
  389.     mul   bx                          ; AX = AX*BX = bytes/plane
  390.     mov   [PlaneInc],ax               ;  save as PlaneInc
  391.     mov   ax,cx                       ; Restore AX
  392.  
  393.  
  394.     mov   di,[X]                      ; DI = X coordinate of dest.
  395.     mov   cx,di                       ; save in CX
  396.     shr   di,2                        ; convert to address byte
  397.  
  398.  
  399.             ;;;;; CLIP PROCESSING FOR TOP CLIP BORDER ;;;;;;;;;;;;;;;;;;;;;
  400.  
  401.     mov   dx,[_TopClip]           ; Compare u.l. Y coord with Top
  402.     sub   dx,[Y]                  ; clipping border
  403.     jle   @@NotTopClip            ; jump if VBM not clipped from above
  404.     cmp   dx,bx
  405.     jnl   @@NotVisible            ; jump if VBM is completely obscured
  406.     mov   [TopRow],dx
  407.     sub   bx,dx
  408.     add   [Y],dx
  409.     jmp   short @@VertClipDone
  410.  
  411.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  412.  
  413. @@NotVisible:
  414.     pop   ds                          ; restore data segment
  415.     pop   di                          ; restore registers
  416.     pop   si
  417.     mov   sp,bp                       ; dealloc local variables
  418.     pop   bp
  419.     ret
  420.  
  421.     ;;;;; CLIP PROCESSING FOR BOTTOM CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  422.  
  423. @@NotTopClip:
  424.     mov   dx,[_BottomClip]
  425.     sub   dx,[Y]
  426.     js    @@NotVisible
  427.         mov   [TopRow],0
  428.     cmp   dx,bx
  429.     jg    @@VertClipDone
  430.     inc   dx
  431.     mov   bx,dx
  432.  
  433. @@VertClipDone:
  434.  
  435.     ;;;;; CLIP PROCESSING FOR LEFT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  436.  
  437.     mov   dx,[_LeftClip]
  438.     sub   dx,di
  439.     jle   @@NotLeftClip
  440.     cmp   dx,ax
  441.     jnl   @@NotVisible
  442.  
  443.     add   di,dx
  444.     mov   [LeftSkip],dx
  445.     mov   [DataInc],dx
  446.     sub   ax,dx
  447.     mov   [CType],1
  448.     jmp   short @@HorizClipDone
  449.  
  450.     ;;;;; CLIP PROCESSING FOR RIGHT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  451.  
  452. @@NotLeftClip:
  453.     mov   dx,[_RightClip]
  454.     sub   dx,di
  455.     js    @@NotVisible
  456.     mov   [LeftSkip],0
  457.     mov   [DataInc],0
  458.     cmp   dx,ax
  459.     jg    @@HorizClipDone
  460.     inc   dx
  461.     sub   ax,dx
  462.     mov   [DataInc],ax
  463.     mov   ax,dx
  464.  
  465.  
  466.     mov   [CType],-1
  467.  
  468. @@HorizClipDone:
  469.  
  470.  
  471.  
  472.     mov   [Width],ax                  ; Save width and height of clipped
  473.     mov   [Height],bx                 ;  image
  474.  
  475.         add   ax,[DataInc]                ; AX = original width of image
  476.     mul   [TopRow]                    ; Calculate bytes in clipped top
  477.     add   si,ax                  ;  rows
  478.     add   si,2                        ; Skip dimension bytes in source
  479.     add   si,[LeftSkip]               ; Skip pixels in front of row that
  480.                       ;  are clipped
  481.  
  482.     mov   bx,[_ScrnLogicalByteWidth]  ; Set BX to Logical Screen Width
  483.     mov   dx,bx                       ; BX - Width of image = No. bytes
  484.     sub   dx,[Width]                  ;  to first byte of next screen
  485.     mov   [LineInc],dx                ;  row.
  486.  
  487.     mov   ax,[Y]                      ; Calculate screen start row
  488.     mul   bx                          ;  then adding screen offset
  489.     add   di,ax
  490.     add   di,[ScrnOffs]
  491.     mov   ax,es                       ; copy ES to DS
  492.     mov   ds,ax
  493.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  494.     mov   es,ax
  495.  
  496.  
  497.  
  498.         and   cx,3
  499.     mov   ah,11h                      ; Set up initial plane mask
  500.     shl   ah,cl
  501.  
  502.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  503.         mov   al,MAP_MASK
  504.         out   dx,al
  505.         inc   dx
  506.     mov   [Plane],4                   ; Set plane counter to 4
  507.     mov   bh,byte ptr [Width]         ; set bh to width for fast looping
  508. @@PlaneLoop:
  509.     push  di               ; Save bitmap's start dest. offset
  510.     push  si
  511.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  512.         mov   al,ah
  513.         out   dx,al                       ; set vga write plane
  514. @@RowLoop:
  515.     mov   cl,bh                       ; Reset Column counter cl
  516.     jcxz   @@NoWidth
  517. @@ColLoop:
  518.     lodsb                          ; Get next source bitmap byte
  519.     or    al,al                       ; If not zero then write to dest.
  520.     jz    @@NoPixel                   ; otherwise skip to next byte
  521.     mov   es:[di],al
  522. @@NoPixel:
  523.     inc   di
  524.     loop @@ColLoop
  525. @@NoWidth:
  526.     add   si,[DataInc]                ; Move to next source row
  527.     add   di,[LineInc]                ; Move to next screen row
  528.         dec   bl                          ; decrement row counter
  529.     jnz   @@RowLoop                   ; Jump if more rows left
  530.         pop   si                          ; Restore SI and set to offset of
  531.     add   si,[PlaneInc]               ; first vis pixel in next plane data
  532.     pop   di                          ; Restore bitmaps start dest byte
  533.     rol   ah,1                  ; Shift mask for next plane
  534.  
  535.     ; Plane Transition (A HACK but it works!)
  536.  
  537.     jnb   @@Nocarry                   ; Jump if not plane transition
  538.     mov   bl,ah                       ; Save Plane Mask
  539.     mov   ax,[CType]                  ; set AX to clip type inc variable
  540.     add   bh,al                       ; Update advancing variables
  541.     sub   [DataInc],ax                ;
  542.     sub   [LineInc],ax                ;
  543.     cmp   al,0                        ; What type of clip do we have
  544.     mov   ah,bl                       ;   restore Plane mask
  545.     jg    @@RightAdvance              ; jump on a right clip!
  546.     inc   di                          ; otherwise increment DI
  547.     jmp   @@Nocarry
  548. @@RightAdvance:
  549.     dec   si
  550. @@Nocarry:
  551.     dec   [Plane]                     ; Decrement plane counter
  552.         jnz   @@PlaneLoop                 ; Jump if more planes left
  553.  
  554.  
  555.         pop   ds                          ; restore data segment
  556.         pop   di                          ; restore registers
  557.         pop   si
  558.         mov   sp,bp                       ; dealloc local variables
  559.         pop   bp
  560.         ret
  561. _x_put_masked_pbm_clipxy  endp
  562.  
  563.  
  564.  
  565.  
  566. ;----------------------------------------------------------------------
  567. ; x_put_pbm_clipx - Write a planar bitmap from system ram to video ram
  568. ;                    with clipping in x and y directions. similar to
  569. ;             "x_put_pbm".
  570. ;
  571. ; See Also:  x_put_pbm_clip
  572. ;
  573. ;
  574. ; See Also:  x_put_pbm,x_put_pbm_clipy,x_put_pbm_clipxy
  575. ;
  576. ; Clipping region variables: LeftClip,RightClip
  577. ;
  578. ; Written by Themie Gouthas
  579. ;
  580. ; This code is a SLOW hack, any better ideas are welcome
  581. ;----------------------------------------------------------------------
  582. _x_put_pbm_clipx  proc
  583. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  584. LOCAL   Plane:byte,CType,LeftSkip,DataInc,Width,Height,TopRow,LineInc:word=LocalStk
  585.     push  bp
  586.         mov   bp,sp
  587.     sub   sp,LocalStk                 ; Create space for local variables
  588.         push  si
  589.         push  di
  590.         push  ds
  591.     cld
  592.  
  593.     les   si,[Bitmap]
  594.  
  595.     xor   ax,ax
  596.     mov   [CType],ax
  597.     mov   al,byte ptr es:[si]         ; AX = width
  598.  
  599.  
  600.     mov   di,[X]                      ; DI = X coordinate of dest.
  601.     mov   cx,di                       ; save in CX
  602.     shr   di,2                        ; convert to address byte
  603.  
  604.  
  605.  
  606.     ;;;;; CLIP PROCESSING FOR LEFT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  607.  
  608.     mov   dx,[_LeftClip]
  609.     sub   dx,di
  610.     jle   @@NotLeftClip
  611.     cmp   dx,ax
  612.     jnl   @@NotVisible
  613.  
  614.     add   di,dx
  615.     mov   [LeftSkip],dx
  616.     mov   [DataInc],dx
  617.     sub   ax,dx
  618.     mov   [CType],1
  619.     jmp   short @@HorizClipDone
  620.  
  621.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  622.  
  623. @@NotVisible:
  624.     pop   ds                          ; restore data segment
  625.     pop   di                          ; restore registers
  626.     pop   si
  627.     mov   sp,bp                       ; dealloc local variables
  628.     pop   bp
  629.     ret
  630.  
  631.     ;;;;; CLIP PROCESSING FOR RIGHT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  632.  
  633. @@NotLeftClip:
  634.     mov   dx,[_RightClip]
  635.     sub   dx,di
  636.     js    @@NotVisible
  637.     mov   [LeftSkip],0
  638.     mov   [DataInc],0
  639.     cmp   dx,ax
  640.     jg    @@HorizClipDone
  641.     inc   dx
  642.     sub   ax,dx
  643.     mov   [DataInc],ax
  644.     mov   ax,dx
  645.     mov   [CType],-1
  646.  
  647. @@HorizClipDone:
  648.  
  649.         xor   bh,bh
  650.     mov   bl,byte ptr es:[si+1]       ; BX = height
  651.  
  652.     mov   [Width],ax                  ; Save width and height of clipped
  653.     mov   [Height],bx                 ;  image
  654.  
  655.  
  656.     add   si,2                        ; Skip dimension bytes in source
  657.     add   si,[LeftSkip]               ; Skip pixels in front of row that
  658.                       ;  are clipped
  659.  
  660.  
  661.     mov   bx,[_ScrnLogicalByteWidth]  ; Set BX to Logical Screen Width
  662.     mov   dx,bx                       ; BX - Width of image = No. bytes
  663.     sub   dx,ax                       ;  to first byte of next screen
  664.     mov   [LineInc],dx                ;  row.
  665.  
  666.     mov   ax,[Y]                      ; Calculate screen start row
  667.     mul   bx                          ;  then adding screen offset
  668.     add   di,ax
  669.     add   di,[ScrnOffs]
  670.     mov   ax,es                       ; copy ES to DS
  671.     mov   ds,ax
  672.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  673.     mov   es,ax
  674.  
  675.         and   cx,3
  676.     mov   ah,11h                      ; Set up initial plane mask
  677.     shl   ah,cl
  678.  
  679.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  680.         mov   al,MAP_MASK
  681.         out   dx,al
  682.         inc   dx
  683.     mov   [Plane],4                   ; Set plane counter to 4
  684.     mov   bh,byte ptr [Width]         ; set bh to width for fast looping
  685. @@PlaneLoop:
  686.     push  di               ; Save bitmap's start dest. offset
  687.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  688.         mov   al,ah
  689.         out   dx,al                       ; set vga write plane
  690. @@RowLoop:
  691.     mov   cl,bh                       ; Reset Column counter cl
  692.     shr   cl,1
  693.     rep   movsw                       ; Copy a complete row
  694.     adc   cl,0
  695.     rep   movsb
  696.     add   si,[DataInc]                ; Move to next source row
  697.     add   di,[LineInc]                ; Move to next screen row
  698.         dec   bl                          ; decrement row counter
  699.     jnz   @@RowLoop                   ; Jump if more rows left
  700.         pop   di                          ; Restore bitmaps start dest byte
  701.     rol   ah,1                  ; Shift mask for next plane
  702.  
  703.     ; Plane Transition (A HACK but it works!)
  704.  
  705.     jnb   @@Nocarry                   ; Jump if not plane transition
  706.     mov   bl,ah                       ; Save Plane Mask
  707.     mov   ax,[CType]                  ; set AX to clip type inc variable
  708.     add   bh,al                       ; Update advancing variables
  709.     sub   [DataInc],ax                ;
  710.     sub   [LineInc],ax                ;
  711.     cmp   al,0                        ; What type of clip do we have
  712.     mov   ah,bl                       ;   restore Plane mask
  713.     jg    @@RightAdvance              ; jump on a right clip!
  714.     inc   di                          ; otherwise increment DI
  715.     jmp   @@Nocarry
  716. @@RightAdvance:
  717.     dec si
  718. @@Nocarry:
  719.     dec   [Plane]                     ; Decrement plane counter
  720.         jnz   @@PlaneLoop                 ; Jump if more planes left
  721.  
  722.  
  723.         pop   ds                          ; restore data segment
  724.         pop   di                          ; restore registers
  725.         pop   si
  726.         mov   sp,bp                       ; dealloc local variables
  727.         pop   bp
  728.         ret
  729. _x_put_pbm_clipx  endp
  730.  
  731.  
  732.  
  733. ;----------------------------------------------------------------------
  734. ; x_put_pbm_clipy - Write a planar bitmap from system ram to video ram
  735. ;                    with clipping in y direction only. similar to
  736. ;             "x_put_pbm".
  737. ;
  738. ; See Also:  x_put_pbm,x_put_pbm_clipx,x_put_pbm_clipxy
  739. ;
  740. ; Clipping region variables: TopClip,BottomClip
  741. ;
  742. ; Written by Themie Gouthas
  743. ;----------------------------------------------------------------------
  744. _x_put_pbm_clipy  proc
  745. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  746. LOCAL   Width,Height,TopRow,LineInc,PlaneInc:word=LocalStk
  747.     push  bp
  748.         mov   bp,sp
  749.     sub   sp,LocalStk                 ; Create space for local variables
  750.         push  si
  751.         push  di
  752.         push  ds
  753.     cld
  754.  
  755.     les   si,[Bitmap]
  756.  
  757.     xor   bh,bh
  758.     mov   bl,byte ptr es:[si+1]   ; BX = height
  759.     ;mov   [Height],bx
  760.  
  761.     xor   ah,ah
  762.     mov   al,byte ptr es:[si]     ; AX = width
  763.     mov   [Width],ax
  764.  
  765.         mov   cx,ax                       ; Save AX
  766.     mul   bx                          ; AX = AX*BX = bytes/plane
  767.     mov   [PlaneInc],ax               ;  save as PlaneInc
  768.     mov   ax,cx                       ; Restore AX
  769.  
  770.         mov   di,[X]
  771.     mov   cx,di
  772.     and   cx,3
  773.     shr   di,2
  774.  
  775.     ;;;;; CLIP PROCESSING FOR TOP CLIP BORDER ;;;;;;;;;;;;;;;;;;;;;
  776.  
  777.     mov   dx,[_TopClip]           ; Compare u.l. Y coord with Top
  778.     sub   dx,[Y]                  ; clipping border
  779.     jle   @@NotTopClip            ; jump if VBM not clipped from above
  780.     cmp   dx,bx
  781.     jnl   @@NotVisible            ; jump if VBM is completely obscured
  782.     mov   [TopRow],dx
  783.     sub   bx,dx
  784.     add   [Y],dx
  785.     jmp   short @@VertClipDone
  786.  
  787.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  788.  
  789. @@NotVisible:
  790.     pop   ds                          ; restore data segment
  791.     pop   di                          ; restore registers
  792.     pop   si
  793.     mov   sp,bp                       ; dealloc local variables
  794.     pop   bp
  795.     ret
  796.  
  797.     ;;;;; CLIP PROCESSING FOR BOTTOM CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  798.  
  799. @@NotTopClip:
  800.     mov   dx,[_BottomClip]
  801.     sub   dx,[Y]
  802.     js    @@NotVisible
  803.         mov   [TopRow],0
  804.     cmp   dx,bx
  805.     jg    @@VertClipDone
  806.     inc   dx
  807.     mov   bx,dx
  808.  
  809. @@VertClipDone:
  810.  
  811.     mov   [Height],bx                 ; Calculate relative offset in data
  812.     mul   [TopRow]                    ;  of first visible scanline
  813.     add   ax,2                        ; Skip dimension bytes in source
  814.     add   si,ax                       ; Skip top rows that arent visible
  815.  
  816.  
  817.     mov   ax,[Y]                      ; Calculate screen row
  818.     mov   bx,[_ScrnLogicalByteWidth]  ;  by mult. Y coord by Screen
  819.     mul   bx                          ;  width then adding screen offset
  820.     add   di,ax
  821.     add   di,[ScrnOffs]
  822.         sub   bx,[Width]                  ; calculate difference from end of
  823.     mov   [LineInc],bx                ; b.m. in curr line to beginning of
  824.                       ; b.m. on next scan line
  825.     mov   ax,es                       ; copy ES to DS
  826.     mov   ds,ax
  827.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  828.     mov   es,ax
  829.  
  830.     mov   ah,11h                      ; Set up initial plane mask
  831.     shl   ah,cl
  832.  
  833.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  834.         mov   al,MAP_MASK
  835.         out   dx,al
  836.         inc   dx
  837.     mov   bh,4                        ; Set plane counter to 4
  838. @@PlaneLoop:
  839.     push  di               ; Save bitmap's start dest. offset
  840.     push  si                          ; Save Bitmaps data offset
  841.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  842.         mov   al,ah
  843.         out   dx,al                       ; set vga write plane
  844. @@RowLoop:
  845.     mov   cl,byte ptr [Width]         ; Reset Column counter cl
  846.     shr   cl,1
  847.     rep   movsw                       ; Copy a complete row
  848.     adc   cl,0
  849.     rep   movsb
  850.  
  851.     add   di,[LineInc]                ; Move to next row
  852.         dec   bl                          ; decrement row counter
  853.     jnz   @@RowLoop                   ; Jump if more rows left
  854.     pop   si                          ; Restore SI and set to offset of
  855.     add   si,[PlaneInc]               ; first vis pixel in next plane data
  856.         pop   di                          ; Restore bitmaps start dest byte
  857.     rol   ah,1                        ; Shift mask for next plane
  858.     adc   di,0                        ; if carry increment screen offset
  859.     dec   bh                          ; Decrement plane counter
  860.         jnz   @@PlaneLoop                 ; Jump if more planes left
  861.  
  862.  
  863.         pop   ds                          ; restore data segment
  864.         pop   di                          ; restore registers
  865.         pop   si
  866.         mov   sp,bp                       ; dealloc local variables
  867.         pop   bp
  868.         ret
  869. _x_put_pbm_clipy   endp
  870.  
  871.  
  872. ;----------------------------------------------------------------------
  873. ; x_put_pbm_clipxy - Write a planar bitmap from system ram to video ram
  874. ;                    with clipping in x and y directions. similar to
  875. ;             "x_put_pbm".
  876. ;
  877. ; See Also:  x_put_pbm,x_put_pbm_clipy,x_put_pbm_clipx
  878. ;
  879. ; Clipping region variables: LeftClip,RightClip,TopClip,BottomClip
  880. ;
  881. ; Written by Themie Gouthas
  882. ;----------------------------------------------------------------------
  883. _x_put_pbm_clipxy  proc
  884. ARG X:word,Y:word,ScrnOffs:word,Bitmap:dword
  885. LOCAL   Plane:byte,CType,LeftSkip,DataInc,Width,Height,TopRow,LineInc,PlaneInc:word=LocalStk
  886.     push  bp
  887.         mov   bp,sp
  888.     sub   sp,LocalStk                 ; Create space for local variables
  889.         push  si
  890.         push  di
  891.         push  ds
  892.     cld
  893.  
  894.     les   si,[Bitmap]
  895.  
  896.     xor   ax,ax
  897.     mov   [CType],ax
  898.     mov   al,byte ptr es:[si]         ; AX = width
  899.         xor   bh,bh
  900.     mov   bl,byte ptr es:[si+1]       ; BX = height
  901.  
  902.         mov   cx,ax                       ; Save AX
  903.     mul   bx                          ; AX = AX*BX = bytes/plane
  904.     mov   [PlaneInc],ax               ;  save as PlaneInc
  905.     mov   ax,cx                       ; Restore AX
  906.  
  907.  
  908.     mov   di,[X]                      ; DI = X coordinate of dest.
  909.     mov   cx,di                       ; save in CX
  910.     shr   di,2                        ; convert to address byte
  911.  
  912.  
  913.             ;;;;; CLIP PROCESSING FOR TOP CLIP BORDER ;;;;;;;;;;;;;;;;;;;;;
  914.  
  915.     mov   dx,[_TopClip]           ; Compare u.l. Y coord with Top
  916.     sub   dx,[Y]                  ; clipping border
  917.     jle   @@NotTopClip            ; jump if VBM not clipped from above
  918.     cmp   dx,bx
  919.     jnl   @@NotVisible            ; jump if VBM is completely obscured
  920.     mov   [TopRow],dx
  921.     sub   bx,dx
  922.     add   [Y],dx
  923.     jmp   short @@VertClipDone
  924.  
  925.     ;;;; EXIT FOR COMPLETELY OBSCURED P.B.M's ;;;;;;;;;;;;;;;;;;;;;;
  926.  
  927. @@NotVisible:
  928.     pop   ds                          ; restore data segment
  929.     pop   di                          ; restore registers
  930.     pop   si
  931.     mov   sp,bp                       ; dealloc local variables
  932.     pop   bp
  933.     ret
  934.  
  935.     ;;;;; CLIP PROCESSING FOR BOTTOM CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  936.  
  937. @@NotTopClip:
  938.     mov   dx,[_BottomClip]
  939.     sub   dx,[Y]
  940.     js    @@NotVisible
  941.         mov   [TopRow],0
  942.     cmp   dx,bx
  943.     jg    @@VertClipDone
  944.     inc   dx
  945.     mov   bx,dx
  946.  
  947. @@VertClipDone:
  948.  
  949.     ;;;;; CLIP PROCESSING FOR LEFT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  950.  
  951.     mov   dx,[_LeftClip]
  952.     sub   dx,di
  953.     jle   @@NotLeftClip
  954.     cmp   dx,ax
  955.     jnl   @@NotVisible
  956.  
  957.     add   di,dx
  958.     mov   [LeftSkip],dx
  959.     mov   [DataInc],dx
  960.     sub   ax,dx
  961.     mov   [CType],1
  962.     jmp   short @@HorizClipDone
  963.  
  964.     ;;;;; CLIP PROCESSING FOR RIGHT CLIP BORDER ;;;;;;;;;;;;;;;;;;;
  965.  
  966. @@NotLeftClip:
  967.     mov   dx,[_RightClip]
  968.     sub   dx,di
  969.     js    @@NotVisible
  970.     mov   [LeftSkip],0
  971.     mov   [DataInc],0
  972.     cmp   dx,ax
  973.     jg    @@HorizClipDone
  974.     inc   dx
  975.     sub   ax,dx
  976.     mov   [DataInc],ax
  977.     mov   ax,dx
  978.     mov   [CType],-1
  979.  
  980. @@HorizClipDone:
  981.  
  982.  
  983.  
  984.     mov   [Width],ax                  ; Save width and height of clipped
  985.     mov   [Height],bx                 ;  image
  986.  
  987.         add   ax,[DataInc]                ; AX = original width of image
  988.     mul   [TopRow]                    ; Calculate bytes in clipped top
  989.     add   si,ax                  ;  rows
  990.     add   si,2                        ; Skip dimension bytes in source
  991.     add   si,[LeftSkip]               ; Skip pixels in front of row that
  992.                       ;  are clipped
  993.  
  994.     mov   bx,[_ScrnLogicalByteWidth]  ; Set BX to Logical Screen Width
  995.     mov   dx,bx                       ; BX - Width of image = No. bytes
  996.     sub   dx,[Width]                  ;  to first byte of next screen
  997.     mov   [LineInc],dx                ;  row.
  998.  
  999.     mov   ax,[Y]                      ; Calculate screen start row
  1000.     mul   bx                          ;  then adding screen offset
  1001.     add   di,ax
  1002.     add   di,[ScrnOffs]
  1003.     mov   ax,es                       ; copy ES to DS
  1004.     mov   ds,ax
  1005.     mov   ax,SCREEN_SEG               ; Point ES to VGA segment
  1006.     mov   es,ax
  1007.  
  1008.         
  1009.  
  1010.         and   cx,3
  1011.     mov   ah,11h                      ; Set up initial plane mask
  1012.     shl   ah,cl
  1013.  
  1014.     mov   dx,SC_INDEX                 ; Prepare VGA for cpu to video writes
  1015.         mov   al,MAP_MASK
  1016.         out   dx,al
  1017.         inc   dx
  1018.     mov   [Plane],4                   ; Set plane counter to 4
  1019.     mov   bh,byte ptr [Width]         ; set bh to width for fast looping
  1020. @@PlaneLoop:
  1021.     push  di               ; Save bitmap's start dest. offset
  1022.     push  si
  1023.     mov   bl,byte ptr [Height]        ; Reset row counter (BL)
  1024.         mov   al,ah
  1025.         out   dx,al                       ; set vga write plane
  1026. @@RowLoop:
  1027.     mov   cl,bh                       ; Reset Column counter cl
  1028.     shr   cl,1
  1029.     rep   movsw                       ; Copy a complete row
  1030.     adc   cl,0
  1031.     rep   movsb
  1032.     add   si,[DataInc]                ; Move to next source row
  1033.     add   di,[LineInc]                ; Move to next screen row
  1034.         dec   bl                          ; decrement row counter
  1035.     jnz   @@RowLoop                   ; Jump if more rows left
  1036.         pop   si                          ; Restore SI and set to offset of
  1037.     add   si,[PlaneInc]               ; first vis pixel in next plane data
  1038.     pop   di                          ; Restore bitmaps start dest byte
  1039.     rol   ah,1                  ; Shift mask for next plane
  1040.  
  1041.     ; Plane Transition (A HACK but it works!)
  1042.  
  1043.     jnb   @@Nocarry                   ; Jump if not plane transition
  1044.     mov   bl,ah                       ; Save Plane Mask
  1045.     mov   ax,[CType]                  ; set AX to clip type inc variable
  1046.     add   bh,al                       ; Update advancing variables
  1047.     sub   [DataInc],ax                ;
  1048.     sub   [LineInc],ax                ;
  1049.     cmp   al,0                        ; What type of clip do we have
  1050.     mov   ah,bl                       ;   restore Plane mask
  1051.     jg    @@RightAdvance              ; jump on a right clip!
  1052.     inc   di                          ; otherwise increment DI
  1053.     jmp   @@Nocarry
  1054. @@RightAdvance:
  1055.     dec si
  1056. @@Nocarry:
  1057.     dec   [Plane]                     ; Decrement plane counter
  1058.         jnz   @@PlaneLoop                 ; Jump if more planes left
  1059.  
  1060.  
  1061.         pop   ds                          ; restore data segment
  1062.         pop   di                          ; restore registers
  1063.         pop   si
  1064.         mov   sp,bp                       ; dealloc local variables
  1065.         pop   bp
  1066.         ret
  1067. _x_put_pbm_clipxy  endp
  1068.  
  1069.     end
  1070.  
  1071.  
  1072.  
  1073.