]> 4ch.mooo.com Git - 16.git/blob - 16/xlib/xbmtools.asm
in the process of converting xlib into watcom friendly
[16.git] / 16 / xlib / xbmtools.asm
1 ;-----------------------------------------------------------------------\r
2 ; MODULE XBMTOOLS\r
3 ;\r
4 ; Bitmap conversion / manipulation tools\r
5 ;\r
6 ; Compile with Tasm.\r
7 ; C callable.\r
8 ;\r
9 ;\r
10 ; ****** XLIB - Mode X graphics library                ****************\r
11 ; ******                                               ****************\r
12 ; ****** Written By Themie Gouthas                     ****************\r
13 ;\r
14 ; egg@dstos3.dsto.gov.au\r
15 ; teg@bart.dsto.gov.au\r
16 ;-----------------------------------------------------------------------\r
17 COMMENT $\r
18 \r
19   This module implements a set of functions to manipulate both planar\r
20   bitmaps and linear bitmaps (as used by x_compile_bitmap):\r
21 \r
22   PLANAR BITMAPS\r
23 \r
24   Planar bitmaps as used by these functions have the following structure:\r
25 \r
26   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255\r
27   BYTE 1                 The bitmap height in rows range 1..255\r
28   BYTE 2..n1             The plane 0 pixels width*height bytes\r
29   BYTE n1..n2            The plane 1 pixels width*height bytes\r
30   BYTE n2..n3            The plane 2 pixels width*height bytes\r
31   BYTE n3..n4            The plane 3 pixels width*height bytes\r
32 \r
33   LINEAR BITMAPS\r
34 \r
35   Linear bitmaps have the following structure:\r
36 \r
37   BYTE 0                 The bitmap width in pixels  range 1..255\r
38   BYTE 1                 The bitmap height in rows   range 1..255\r
39   BYTE 2..n              The width*height bytes of the bitmap\r
40 \r
41 \r
42 \r
43 $\r
44 \r
45 LOCALS\r
46 .8086\r
47 \r
48 include model.inc\r
49 include xbmtools.inc\r
50 \r
51         .code\r
52 \r
53 \r
54 \r
55 ;-----------------------------------------------------------------------\r
56 ; x_pbm_to_bm\r
57 ;\r
58 ; This function converts a bitmap in the planar format to the linear format\r
59 ; as used by x_compile_bitmap.\r
60 ;\r
61 ; WARNING: the source and destination bitmaps must be pre - allocated\r
62 ;\r
63 ; NOTE: This function can only convert planar bitmaps that are suitable.\r
64 ;       If the source planar bitmap's width (per plane) is >= 256/4\r
65 ;       it cannot be converted. In this situation an error code\r
66 ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.\r
67 ;\r
68 ; C callable as:\r
69 ;    int x_pbm_to_bm(char far * source_pbm, char far * dest_bm);\r
70 ;\r
71 ; Written By Themie Gouthas\r
72 \r
73 proc _x_pbm_to_bm\r
74 ARG   src_pbm:dword,dest_bm:dword\r
75         push bp                  ; Preserve caller's stack frame\r
76         mov  bp,sp\r
77         push ds\r
78         push di\r
79         push si\r
80 \r
81         les  di,[dest_bm]     ; es:di -> destination bitmap\r
82         lds  si,[src_pbm]     ; ds:si -> source planar bitmap\r
83         lodsb                 ; load AL with source pbm pixel width per plane\r
84         mov  bl,al            ; save in CL\r
85         xor  ah,ah            ; convert to word\r
86         shl  ax,2             ; mult by 4 giving source image width\r
87         cmp  ax,255           ; if the result > 255 then we have exceeded\r
88         ja   @@WidthError     ; the max width of linear bm.\r
89 \r
90         stosb                 ; write do dest_bm\r
91 \r
92         lodsb                 ; tranfer source pbm height in pixels to\r
93         stosb             ;  dest_bm\r
94 \r
95         xor  ah,ah            ; convert to word\r
96         mul  bl               ; AX = AX * BL ie. total no. pixels per plane\r
97         mov  dx,di            ; save DI, the pointer to the destination bm\r
98         mov  bl,3             ; set plane loop counter (BL)\r
99 \r
100 @@PlaneLoop:\r
101         mov  cx,ax            ; set CX to total number of pixels per plane\r
102 \r
103 @@PixelLoop:\r
104         movsb                 ; transfer pixel\r
105         add  di,3             ; increment destination to compensate for plane\r
106         loop @@PixelLoop\r
107 \r
108         inc  dx               ; increment original di for next pixel plane\r
109         mov  di,dx            ; and restore di from incremented original\r
110         dec  bl               ; decrement plane counter\r
111         jns  @@PlaneLoop      ; loop if more planes left\r
112         xor  ax,ax\r
113         jmp  short @@Done\r
114 @@WidthError:\r
115         mov  ax,1\r
116 \r
117 @@Done:\r
118         pop  si\r
119         pop  di\r
120         pop  ds\r
121         pop  bp                  ;restore caller's stack frame\r
122         ret\r
123 _x_pbm_to_bm endp\r
124 \r
125 \r
126 ;-----------------------------------------------------------------------\r
127 ; x_bm_to_pbm\r
128 ;\r
129 ; This function converts a bitmap in the linear format as used by\r
130 ; x_compile_bitmap to the planar formap.\r
131 ;\r
132 ; WARNING: the source and destination bitmaps must be pre - allocated\r
133 ;\r
134 ; NOTE: This function can only convert linear bitmaps that are suitable.\r
135 ;       If the source linear bitmap's width is not a multiple of 4\r
136 ;       it cannot be converted. In this situation an error code\r
137 ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.\r
138 ;\r
139 ;\r
140 ; C callable as:\r
141 ;    int x_bm_to_pbm(char far * source_pbm, char far * dest_bm);\r
142 ;\r
143 ; Written By Themie Gouthas\r
144 \r
145 proc _x_bm_to_pbm\r
146 ARG src_bm:dword,dest_pbm:dword\r
147         push bp                  ; Preserve caller's stack frame\r
148         mov  bp,sp\r
149         push ds\r
150         push di\r
151         push si\r
152 \r
153         les  di,[dest_pbm]       ; es:di -> destination planar bitmap\r
154         lds  si,[src_bm]         ; ds:si -> source bitmap\r
155         lodsb                    ; load AX with source bitmap width\r
156         test al,03h              ; Check that width is a multiple of 4\r
157         jnz  @@WidthIncompatible\r
158         shr  al,2                ; divide by 4 giving width of plane\r
159         stosb                    ; store destination planar bitmap width\r
160         mov  bl,al               ;  and copy to bl\r
161         lodsb\r
162         stosb            ; Transfer source bitmap height to dest pbm\r
163         xor  ah,ah               ; Conver height to word\r
164         mul  bl                  ; calculate the total no. of pixels / plane\r
165         mov  dx,si               ; save source offset\r
166         mov  bl,3\r
167 \r
168 @@PlaneLoop:\r
169         mov  cx,ax            ; set CX to total number of pixels per plane\r
170 \r
171 @@PixelLoop:\r
172         movsb                 ; transfer pixel\r
173         add  si,3             ; increment src offset to compensate for plane\r
174         loop @@PixelLoop\r
175 \r
176         inc  dx               ; increment original si for next pixel plane\r
177         mov  si,dx            ; and restore si from incremented original\r
178         dec  bl               ; decrement plane counter\r
179         jns  @@PlaneLoop      ; loop if more planes left\r
180         xor  ax,ax\r
181         jmp  short @@Done\r
182 @@WidthIncompatible:\r
183         mov  ax,1\r
184 \r
185 @@Done:\r
186         pop  si\r
187         pop  di\r
188         pop  ds\r
189         pop  bp                  ;restore caller's stack frame\r
190         ret\r
191 _x_bm_to_pbm endp\r
192 \r
193 \r
194         end\r
195 \r