OSDN Git Service

update todo list with new stuff
[proj16/16.git] / 16 / xlib / xmakevbm.c
1 /*----------------------------------------------------------------------\r
2 ; MODULE XMAKEVBM\r
3 ;\r
4 ; Implements function to generate a Video bitmap (VBM) from a linear bitmap\r
5 ; (LBM)\r
6 ;\r
7 ; Compile with Tasm.\r
8 ; C callable.\r
9 ;\r
10 ;\r
11 ; Based on "CreateMaskedAlignedImage" published in Dr Dobbs Journal\r
12 ; by Michael Abrash (Jul - Aug 1991)\r
13 ;\r
14 ; ****** XLIB - Mode X graphics library                ****************\r
15 ; ******                                               ****************\r
16 ; ****** Written By Themie Gouthas                     ****************\r
17 ; ****** Aeronautical Research Laboratory              ****************\r
18 ; ****** Defence Science and Technology Organisation   ****************\r
19 ; ****** Australia                                     ****************\r
20 ;\r
21 ; egg@dstos3.dsto.gov.au\r
22 ; teg@bart.dsto.gov.au\r
23 ;-----------------------------------------------------------------------*/\r
24 /*\r
25   Here is an outline of the XLIB image formats\r
26 \r
27 \r
28   PLANAR BITMAPS\r
29   --------------\r
30 \r
31   Planar bitmaps as used by these functions have the following structure:\r
32 \r
33   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255\r
34   BYTE 1                 The bitmap height in rows range 1..255\r
35   BYTE 2..n1             The plane 0 pixels width*height bytes\r
36   BYTE n1..n2            The plane 1 pixels width*height bytes\r
37   BYTE n2..n3            The plane 2 pixels width*height bytes\r
38   BYTE n3..n4            The plane 3 pixels width*height bytes\r
39 \r
40   LINEAR BITMAPS\r
41   --------------\r
42 \r
43   Linear bitmaps have the following structure:\r
44 \r
45   BYTE 0                 The bitmap width in pixels  range 1..255\r
46   BYTE 1                 The bitmap height in rows   range 1..255\r
47   BYTE 2..n              The width*height bytes of the bitmap\r
48 \r
49 \r
50   VIDEO BITMAPS\r
51   -------------\r
52 \r
53       WORD  0   Size          Total size of this VBM structure in bytes\r
54       WORD  1   ImageWidth    Width in bytes of the image (for all alignments)\r
55       WORD  2   ImageHeight   Height in scan lines of the image\r
56 \r
57       WORD  3 Alignment 0  ImagePtr   Offset in VidRAM of this aligned image\r
58    +--WORD  4              MaskPtr    Offset (within this structure's DS) of\r
59    |   .                               alignment masks\r
60    |   .\r
61    |   .\r
62    |  WORD  9 Alignment 3  ImagePtr   Offset in VidRAM of this aligned image\r
63   +|--WORD 10              MaskPtr    Offset (within this structure's DS) of\r
64   ||                                   alignment masks\r
65   ||\r
66   |+->BYTE 21 (WORD 11)                -------+-- Image masks for alignment 0\r
67   |   .                                       |\r
68   |   .                                       |\r
69   |   BYTE  21 + ImageWidth*ImageHeight  -----+\r
70   |\r
71   |   .\r
72   |   . (similaly for alignments 1 - 2 )\r
73   |   .\r
74   |\r
75   +-->BYTE  21 + 3*ImageWidth*ImageHeight + 1-+-- Image masks for alignment 3\r
76       .                                       |\r
77       .                                       |\r
78       BYTE  21 + 4*(ImageWidth*ImageHeight) --+\r
79 \r
80       .\r
81       .\r
82       << Similarly for alignments 2 and 3 >>\r
83       .\r
84       .\r
85       BYTE 21 + 4*(ImageWidth*ImageHeight)\r
86   -------------\r
87 \r
88   (And dont forget the corresponding data in video ram)\r
89 \r
90 \r
91 */\r
92 \r
93 \r
94 \r
95 #include <alloc.h>\r
96 #include <dos.h>\r
97 \r
98 /* function to store the linear bitmap in the required video RAM offset */\r
99 /* and in the required alignment                                        */\r
100 \r
101 extern unsigned int x_store_vbm_image(unsigned int, int, char far *);\r
102 \r
103 \r
104 /* Alignment structures, 4 of which make up the header section of the */\r
105 /* video bitmap                                                       */\r
106 \r
107 typedef struct {\r
108   unsigned int  size;\r
109   unsigned int ImageWidth;\r
110   unsigned int ImageHeight;\r
111   struct {\r
112      unsigned int  ImagePtr;\r
113      unsigned int  MaskPtr;\r
114   } alignments[4];\r
115 } alignment_header;\r
116 \r
117 /* Structure to extract width/height frol LBM (linear bit map) */\r
118 \r
119 typedef struct {\r
120   unsigned char width;\r
121   unsigned char height;\r
122 } lbm_header;\r
123 \r
124 \r
125 /*************************************************************************/\r
126 /*                                                                       */\r
127 /* Generates all four possible mode X image/mask alignments, stores      */\r
128 /* image alignments in display memory, allocates memory for and generates*/\r
129 /* mask alignments, and fills out a VBM aligned masked image structure.  */\r
130 /* Each non-zero byte in source bitmap corresponds to image pixel to be  */\r
131 /* drawn.                                                                */\r
132 /* On success returns a far pointer to the new VBM structure otherwise   */\r
133 /* it returns NULL                                                       */\r
134 /*                                                                       */\r
135 /* Source Language: C                                                    */\r
136 /*                                                                       */\r
137 /* Parameters:                                                           */\r
138 /*    lbm        pointer to linear bitmap                                */\r
139 /*    vramStart  contains the next available video offset which is       */\r
140 /*               also updated after calling this function                */\r
141 /*                                                                       */\r
142 /*************************************************************************/\r
143 \r
144 char far *x_make_vbm(char far *lbm, unsigned int *VramStart)\r
145 {\r
146 \r
147   lbm_header far       *lbm_headr;\r
148   alignment_header far *vbm_headr;\r
149   char far             *vbm_mask_ptr,*p;\r
150   char far             *lbm_pixel_ptr;\r
151   int                  align,BitNum,TempImageWidth;\r
152   unsigned int         TempWidth,TempHeight,TempSize,MaskSize,VramOffs,MaskSpace=0;\r
153   int                  scanline;\r
154   unsigned char        MaskTemp;\r
155 \r
156   VramOffs  = *VramStart;\r
157   lbm_headr = (lbm_header far *) lbm;\r
158 \r
159   TempWidth     = (lbm_headr->width+3)/4+1;\r
160   TempHeight    = lbm_headr->height;\r
161   TempSize      = TempWidth*TempHeight;\r
162 \r
163   vbm_headr = (alignment_header far *) farmalloc(22+TempSize*4);\r
164   if (!vbm_headr) return NULL;\r
165 \r
166   MaskSpace=22;\r
167 \r
168   vbm_headr->ImageWidth  = TempWidth;\r
169   vbm_headr->ImageHeight = TempHeight;\r
170   vbm_headr->size        = 22+TempSize*4;\r
171   for (align=0;align<4;align++){\r
172     vbm_headr->alignments[align].ImagePtr = VramOffs;\r
173     x_store_vbm_image(VramOffs,align,lbm);\r
174     MaskSpace+=TempSize;\r
175     VramOffs+=TempSize;\r
176   }\r
177 \r
178 \r
179   vbm_mask_ptr = (char far *)vbm_headr+22;\r
180 \r
181   for (align=0;align<4;align++){\r
182     lbm_pixel_ptr = lbm + 2;\r
183     vbm_headr->alignments[align].MaskPtr = FP_OFF(vbm_mask_ptr);\r
184     for (scanline=0;scanline<TempHeight;scanline++){\r
185       BitNum=align;\r
186       MaskTemp=0;\r
187       TempImageWidth=lbm_headr->width;\r
188       do {\r
189         MaskTemp |= (*lbm_pixel_ptr++ !=0) << BitNum;\r
190         if (++BitNum > 3) {\r
191           *vbm_mask_ptr++=MaskTemp;\r
192           MaskTemp=BitNum=0;\r
193         }\r
194       } while (--TempImageWidth);\r
195       *vbm_mask_ptr++=(BitNum != 0)?MaskTemp:0;\r
196    }\r
197 \r
198  }\r
199 \r
200  *VramStart=VramOffs;\r
201  return (char far *) vbm_headr;\r
202 }\r
203 \r
204 \r
205 \1a