--- /dev/null
+;-----------------------------------------------------------\r
+;\r
+; MXCG.ASM - Color to gray conversion\r
+; Copyright (c) 1994 by Alessandro Scotti\r
+;\r
+;-----------------------------------------------------------\r
+WARN PRO\r
+INCLUDE MODEX.DEF\r
+\r
+PUBLIC mxColorToGray\r
+\r
+MX_TEXT SEGMENT USE16 PARA PUBLIC 'CODE'\r
+ ASSUME cs:MX_TEXT, ds:NOTHING, es:NOTHING\r
+\r
+;-----------------------------------------------------------\r
+;\r
+; Converts RGB colors to gray shades.\r
+;\r
+; Input:\r
+; CPal = pointer to color palette\r
+; GPal = pointer to destination (gray) palette\r
+; Count = number of colors to convert\r
+; Output:\r
+; none\r
+;\r
+; Note: CPal and GPal may point to the same buffer.\r
+;\r
+mxColorToGray PROC FAR\r
+ ARG Count:WORD, \\r
+ DPal:DWORD, \\r
+ SPal:DWORD = ARG_SIZE\r
+ ASSUME ds:NOTHING\r
+ .enter 0\r
+ .push ds, si, es, di\r
+\r
+ mov cx, [Count]\r
+ jcxz @@Exit\r
+ lds si, [SPal]\r
+ les di, [DPal]\r
+ cld\r
+; We use the standard formula\r
+; gray=(red*30 + green*59 + blue*11)/100\r
+; in the equivalent form\r
+; gray=(red*77 + green*151 + blue*28)/256\r
+; which doesn't need the last divide.\r
+ mov bx, 77 SHL 8 + 151\r
+@@Loop:\r
+ lodsb ; Red\r
+ mul bh\r
+ mov dx, ax\r
+ lodsb ; Green\r
+ mul bl\r
+ add dx, ax\r
+ lodsb ; Blue\r
+ mov ah, 28\r
+ mul ah\r
+ add ax, dx\r
+ mov al, ah\r
+ stosw ; Save new RGB\r
+ stosb\r
+ loop @@Loop\r
+\r
+@@Exit:\r
+ .pop ds, si, es, di\r
+ .leave ARG_SIZE\r
+mxColorToGray ENDP\r
+\r
+MX_TEXT ENDS\r
+END\r