]> 4ch.mooo.com Git - 16.git/blob - 16/x/mxcg.asm
push
[16.git] / 16 / x / mxcg.asm
1 ;-----------------------------------------------------------\r
2 ;\r
3 ; MXCG.ASM - Color to gray conversion\r
4 ; Copyright (c) 1994 by Alessandro Scotti\r
5 ;\r
6 ;-----------------------------------------------------------\r
7 WARN    PRO\r
8 INCLUDE MODEX.DEF\r
9 \r
10 PUBLIC  mxColorToGray\r
11 \r
12 MX_TEXT         SEGMENT USE16 PARA PUBLIC 'CODE'\r
13                 ASSUME cs:MX_TEXT, ds:NOTHING, es:NOTHING\r
14 \r
15 ;-----------------------------------------------------------\r
16 ;\r
17 ; Converts RGB colors to gray shades.\r
18 ;\r
19 ; Input:\r
20 ;       CPal    = pointer to color palette\r
21 ;       GPal    = pointer to destination (gray) palette\r
22 ;       Count   = number of colors to convert\r
23 ; Output:\r
24 ;       none\r
25 ;\r
26 ; Note: CPal and GPal may point to the same buffer.\r
27 ;\r
28 mxColorToGray   PROC    FAR\r
29         ARG     Count:WORD,     \\r
30                 DPal:DWORD,     \\r
31                 SPal:DWORD      = ARG_SIZE\r
32         ASSUME  ds:NOTHING\r
33         .enter  0\r
34         .push   ds, si, es, di\r
35 \r
36         mov     cx, [Count]\r
37         jcxz    @@Exit\r
38         lds     si, [SPal]\r
39         les     di, [DPal]\r
40         cld\r
41 ; We use the standard formula\r
42 ;       gray=(red*30 + green*59 + blue*11)/100\r
43 ; in the equivalent form\r
44 ;       gray=(red*77 + green*151 + blue*28)/256\r
45 ; which doesn't need the last divide.\r
46         mov     bx, 77 SHL 8 + 151\r
47 @@Loop:\r
48         lodsb                           ; Red\r
49         mul     bh\r
50         mov     dx, ax\r
51         lodsb                           ; Green\r
52         mul     bl\r
53         add     dx, ax\r
54         lodsb                           ; Blue\r
55         mov     ah, 28\r
56         mul     ah\r
57         add     ax, dx\r
58         mov     al, ah\r
59         stosw                           ; Save new RGB\r
60         stosb\r
61         loop    @@Loop\r
62 \r
63 @@Exit:\r
64         .pop    ds, si, es, di\r
65         .leave  ARG_SIZE\r
66 mxColorToGray   ENDP\r
67 \r
68 MX_TEXT         ENDS\r
69 END\r