From 9172066d2d7a6b42a9d849a14ba74d5ef842f45a Mon Sep 17 00:00:00 2001
From: LS <lyubomir.savekov@engview.com>
Date: Wed, 6 Nov 2019 13:00:58 +0200
Subject: [PATCH 2/8] Color decoding and encoding for CMC now works properly

---
 src/bits.c                         | 39 ++++++++++++++++++++++++------
 src/common_entity_handle_data.spec |  4 +--
 src/out_dxf.c                      | 18 ++++++--------
 src/out_dxfb.c                     | 18 ++++++--------
 4 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/src/bits.c b/src/bits.c
index 33dfa282..7d7c4f12 100644
--- a/src/bits.c
+++ b/src/bits.c
@@ -1726,16 +1726,39 @@ bit_write_TIMERLL (Bit_Chain *dat, BITCODE_TIMERLL date)
 void
 bit_read_CMC (Bit_Chain *restrict dat, Dwg_Color *restrict color)
 {
-  memset (color, 0, sizeof (Dwg_Color));
   color->index = bit_read_BS (dat);
   if (dat->version >= R_2004)
-    {
-      color->rgb = bit_read_BL (dat);
-      color->flag = bit_read_RC (dat);
-      // wide?
-      color->name = (color->flag & 1) ? (char *)bit_read_TV (dat) : NULL;
-      color->book_name = (color->flag & 2) ? (char *)bit_read_TV (dat) : NULL;
-    }
+  {
+    //The most significant byte from the 4 bytes in rgb contains what should be done
+    color->rgb  = bit_read_BL(dat);
+    color->flag = bit_read_RC(dat);
+    color->name      = (color->flag & 1) ? (char*)bit_read_TV(dat) : NULL;
+    color->book_name = (color->flag & 2) ? (char*)bit_read_TV(dat) : NULL;
+    switch(color->rgb >> 24)
+    {
+      case 0xC0: //ByLayer
+        color->index = 256;
+        color->rgb = 0;
+        break;
+      case 0xC1: //ByBlock
+        color->index = 0;
+        color->rgb = 0;
+        break;
+      case 0xC2: //This is true color
+        //The index value should be index from the AutoCAD Color Index (ACI), that is closest to the true color.
+        //But the ACI or any form of color map is not implemented for now.
+        //Setting index=256 is a workaround. We won't write the index color (code 62) as long as there is true color (code 420).
+        color->index = 256;
+        color->rgb = color->rgb & 0xFFFFFF;
+        break;
+      case 0xC3: //This is index color
+        color->index = color->rgb & 0xFF;
+        color->rgb = 0;
+        break;
+      default:
+        break;
+    }
+  } //R_2004
 }
 
 /** Write color
diff --git a/src/common_entity_handle_data.spec b/src/common_entity_handle_data.spec
index 7fdae017..ec69fe21 100644
--- a/src/common_entity_handle_data.spec
+++ b/src/common_entity_handle_data.spec
@@ -49,7 +49,7 @@
       }
 #ifdef IS_DXF
       switch (FIELD_VALUE(ltype_flags)) {
-      case 0: VALUE_TV("BYLAYER", 6); break;
+      //case 0: VALUE_TV("BYLAYER", 6); break;
       case 1: VALUE_TV("BYBLOCK", 6); break;
       case 2: VALUE_TV("CONTINUOUS", 6); break;
       default: break;
@@ -75,7 +75,7 @@
 
 #ifdef IS_DXF
       switch (FIELD_VALUE(ltype_flags)) {
-      case 0: VALUE_TV("BYLAYER", 6); break;
+      //case 0: VALUE_TV("BYLAYER", 6); break;
       case 1: VALUE_TV("BYBLOCK", 6); break;
       case 2: VALUE_TV("CONTINUOUS", 6); break;
       default: break;
diff --git a/src/out_dxf.c b/src/out_dxf.c
index cf3cfd9d..8873372d 100644
--- a/src/out_dxf.c
+++ b/src/out_dxf.c
@@ -469,26 +469,24 @@ dxf_print_rd (Bit_Chain *dat, BITCODE_RD value, int dxf)
 // skip index 256 bylayer
 #define FIELD_CMC(color, dxf1, dxf2)                                          \
   {                                                                           \
-    if (dxf1 > 0 && _obj->color.index != 256)                                 \
+    if (_obj->color.index != 256)                                             \
       {                                                                       \
-        VALUE_RS ((_obj->color.index & 255), dxf1);                           \
+        VALUE_RS(_obj->color.index, dxf1);                                    \
       }                                                                       \
-    if (dat->version >= R_2004 && dxf2 > 0 && _obj->color.index != 256)       \
+    if (dat->version >= R_2004 && dxf2 > 0 && _obj->color.rgb != 0)           \
       {                                                                       \
-        VALUE_RS (_obj->color.rgb >> 24, dxf1);                               \
-        VALUE_RL (_obj->color.rgb & 0x00ffffff, dxf2);                        \
+        VALUE_RL(_obj->color.rgb, dxf2);                                      \
       }                                                                       \
   }
 #define SUB_FIELD_CMC(o, color, dxf1, dxf2)                                   \
   {                                                                           \
-    if (dxf1 > 0 && _obj->o.color.index != 256)                               \
+    if (_obj->o.color.index != 256)                                           \
       {                                                                       \
-        VALUE_RS ((_obj->o.color.index & 255), dxf1);                         \
+        VALUE_RS(_obj->o.color.index, dxf1);                                  \
       }                                                                       \
-    if (dat->version >= R_2004 && dxf2 > 0 && _obj->o.color.index != 256)     \
+    if (dat->version >= R_2004 && dxf2 > 0 && _obj->o.color.rgb != 0)         \
       {                                                                       \
-        VALUE_RS (_obj->o.color.rgb >> 24, dxf1);                             \
-        VALUE_RL (_obj->o.color.rgb & 0x00ffffff, dxf2);                      \
+        VALUE_RL(_obj->o.color.rgb, dxf2);                                    \
       }                                                                       \
   }
 #define HEADER_TIMEBLL(nam, dxf)                                              \
diff --git a/src/out_dxfb.c b/src/out_dxfb.c
index 45e47ea5..82c12c7c 100644
--- a/src/out_dxfb.c
+++ b/src/out_dxfb.c
@@ -400,26 +400,24 @@ static void dxfb_cvt_tablerecord (Bit_Chain *restrict dat,
 #define FIELD_3DPOINT(nam, dxf) FIELD_3RD (nam, dxf)
 #define FIELD_CMC(color, dxf1, dxf2)                                          \
   {                                                                           \
-    if (dxf1 > 0 && (dat->version < R_2004 || _obj->color.index == 256))      \
+    if (_obj->color.index != 256)                                             \
       {                                                                       \
-        VALUE_RS ((_obj->color.index & 255), dxf1);                           \
+        VALUE_RS(_obj->color.index, dxf1);                                    \
       }                                                                       \
-    if (dat->version >= R_2004 && dxf2 > 0 && _obj->color.index != 256)       \
+    if (dat->version >= R_2004 && dxf2 > 0 && _obj->color.rgb != 0)           \
       {                                                                       \
-        VALUE_RS (_obj->color.rgb >> 24, dxf1);                               \
-        VALUE_RL (_obj->color.rgb & 0x00ffffff, dxf2);                        \
+        VALUE_RL(_obj->color.rgb, dxf2);                                      \
       }                                                                       \
   }
 #define SUB_FIELD_CMC(o, color, dxf1, dxf2)                                   \
   {                                                                           \
-    if (dxf1 > 0 && (dat->version < R_2004 || _obj->o.color.index == 256))    \
+    if (_obj->o.color.index != 256)                                           \
       {                                                                       \
-        VALUE_RS ((_obj->o.color.index & 255), dxf1);                         \
+        VALUE_RS(_obj->o.color.index, dxf1);                                  \
       }                                                                       \
-    if (dat->version >= R_2004 && dxf2 > 0 && _obj->o.color.index != 256)     \
+    if (dat->version >= R_2004 && dxf2 > 0 && _obj->o.color.rgb != 0)         \
       {                                                                       \
-        VALUE_RS (_obj->o.color.rgb >> 24, dxf1);                             \
-        VALUE_RL (_obj->o.color.rgb & 0x00ffffff, dxf2);                      \
+        VALUE_RL(_obj->o.color.rgb, dxf2);                                    \
       }                                                                       \
   }
 #define HEADER_CMC(nam, dxf)                                                  \
-- 
2.22.0.windows.1

