dwg_bmp (const Dwg_Data *restrict dwg, BITCODE_RL *restrict size)
{
  BITCODE_RC i, num_pictures, type;
  int found;
  BITCODE_RL header_size, address, osize;
  Bit_Chain dat = { 0 };

  loglevel = dwg->opts & DWG_OPTS_LOGLEVEL;
  *size = 0;
  assert (dwg);
  // copy the chain data. bit_* needs a full chain with opts and version
  dat = *(Bit_Chain *)&dwg->thumbnail;
  if (!dat.size || !dat.chain)
    {
      LOG_INFO ("no THUMBNAIL Image Data\n")
      return NULL;
    }
  //dat.byte = 0; sentinel at 16
  dat.bit = 0;
  dat.opts = dwg->opts;
  dat.from_version = dwg->header.from_version;
  dat.version = dwg->header.version;
  dat.fh = NULL;

#ifdef USE_TRACING
  /* Before starting, set the logging level, but only do so once.  */
  if (!env_var_checked_p)
    {
      char *probe = getenv ("LIBREDWG_TRACE");
      if (probe)
        loglevel = atoi (probe);
      env_var_checked_p = true;
    }
#endif /* USE_TRACING */

  osize = bit_read_RL (&dat); /* overall size of all images */
  LOG_TRACE ("overall size: " FORMAT_RL " [RL]\n", osize);
  if (osize > (dat.size - 4))
    {
      LOG_ERROR ("Preview overflow > %ld", dat.size - 4);
      return NULL;
    }
  num_pictures = bit_read_RC (&dat);
  LOG_INFO ("num_pictures: %d [RC]\n", (int)num_pictures)

  found = 0;
  header_size = 0;
  for (i = 0; i < num_pictures; i++)
    {
      if (dat.byte > dat.size)
        {
          LOG_ERROR ("Preview overflow");
          break;
        }
      type = bit_read_RC (&dat);
      LOG_TRACE ("\t[%i] Code: %i [RC]\n", i, type)
      address = bit_read_RL (&dat);
      LOG_TRACE ("\t\tHeader data start: 0x%x [RL]\n", address)
      if (type == 1)
        {
          header_size += bit_read_RL (&dat);
          LOG_TRACE ("\t\tHeader data size: %i [RL]\n", header_size)
        }
      else if (type == 2 && found == 0)
        {
          *size = bit_read_RL (&dat);
          found = 1;
          LOG_INFO ("\t\tBMP size: %i [RL]\n", *size)
          if (*size > (dat.size - 4))
            {
              LOG_ERROR ("BMP thumbnail overflow > %ld", dat.size - 4);
              return NULL;
            }
        }
      else if (type == 3)
        {
          osize = bit_read_RL (&dat);
          LOG_INFO ("\t\tWMF size: %i [RL]\n", osize)
        }
      else if (type == 4) // type 4?
        {
          osize = bit_read_RL (&dat);
          LOG_INFO ("\t\tPNG size: %i [RL]\n", osize)
        }
      else
        {
          osize = bit_read_RL (&dat);
          LOG_TRACE ("\t\tSize of unknown type %i: %i [RL]\n", type, osize)
        }
    }
  dat.byte += header_size;
  if (*size)
    LOG_TRACE ("BMP offset: %lu\n", dat.byte);
  if (dat.byte > dat.size)
    {
      *size = 0;
      LOG_ERROR ("Preview overflow");
      return NULL;
    }

  if (*size > 0)
    return (dat.chain + dat.byte);
  else
    return NULL;
}
dwg_bmp (const Dwg_Data *restrict dwg, BITCODE_RL *restrict size)
{
  BITCODE_RC i, num_pictures, type;
  int found;
  BITCODE_RL header_size, address, osize;
  Bit_Chain dat = { 0 };

  loglevel = dwg->opts & DWG_OPTS_LOGLEVEL;
  *size = 0;
  assert (dwg);
   
  dat = *(Bit_Chain *)&dwg->thumbnail;
  if (!dat.size || !dat.chain)
    {
      LOG_INFO ("no THUMBNAIL Image Data\n")
      return NULL;
    }
   
  dat.bit = 0;
  dat.opts = dwg->opts;
  dat.from_version = dwg->header.from_version;
  dat.version = dwg->header.version;
  dat.fh = NULL;

#ifdef USE_TRACING
   
  if (!env_var_checked_p)
    {
      char *probe = getenv ("LIBREDWG_TRACE");
      if (probe)
        loglevel = atoi (probe);
      env_var_checked_p = true;
    }
#endif  

  osize = bit_read_RL (&dat);  
  LOG_TRACE ("overall size: " FORMAT_RL " [RL]\n", osize);
  if (osize > (dat.size - 4))
    {
      LOG_ERROR ("Preview overflow > %ld", dat.size - 4);
      return NULL;
    }
  num_pictures = bit_read_RC (&dat);
  LOG_INFO ("num_pictures: %d [RC]\n", (int)num_pictures)

  found = 0;
  header_size = 0;
  for (i = 0; i < num_pictures; i++)
    {
      if (dat.byte > dat.size)
        {
          LOG_ERROR ("Preview overflow");
          break;
        }
      type = bit_read_RC (&dat);
      LOG_TRACE ("\t[%i] Code: %i [RC]\n", i, type)
      address = bit_read_RL (&dat);
      LOG_TRACE ("\t\tHeader data start: 0x%x [RL]\n", address)
      if (type == 1)
        {
          header_size += bit_read_RL (&dat);
          LOG_TRACE ("\t\tHeader data size: %i [RL]\n", header_size)
        }
      else if (type == 2 && found == 0)
        {
          *size = bit_read_RL (&dat);
          found = 1;
          LOG_INFO ("\t\tBMP size: %i [RL]\n", *size)
          if (*size > (dat.size - 4))
            {
              LOG_ERROR ("BMP thumbnail overflow > %ld", dat.size - 4);
              return NULL;
            }
        }
      else if (type == 3)
        {
          osize = bit_read_RL (&dat);
          LOG_INFO ("\t\tWMF size: %i [RL]\n", osize)
        }
      else if (type == 4)  
        {
          osize = bit_read_RL (&dat);
          LOG_INFO ("\t\tPNG size: %i [RL]\n", osize)
        }
      else
        {
          osize = bit_read_RL (&dat);
          LOG_TRACE ("\t\tSize of unknown type %i: %i [RL]\n", type, osize)
        }
    }
  dat.byte += header_size;
  if (*size)
    LOG_TRACE ("BMP offset: %lu\n", dat.byte);
  if (dat.byte > dat.size)
    {
      *size = 0;
      LOG_ERROR ("Preview overflow");
      return NULL;
    }

  if (*size > 0)
    return (dat.chain + dat.byte);
  else
    return NULL;
}