CADImage Bitmap with black background

Discuss and ask questions about CAD DLL (CAD Image DLL, CAD Importer DLL).

Moderators: support, SDS, admin

Post Reply
S.D.
Posts: 5
Joined: 25 Jun 2012, 14:20
Location: Germany
Contact:

CADImage Bitmap with black background

Post by S.D. » 25 Jun 2012, 14:33

Hello,

I've got a problem loading a dwg-file into a bitmap using DrawCADtoBitmap. Using version 7.2.5.1 this leads to a CAD bitmap with a white background, while version 7.4.1 produces a bitmap with a black background. Since most dwg-files used consists of black elements, this is a real problem.
Trying to use the SetDefaultColor-method before DrawCADToBitmap does not change anything and I was wondering if it is possible to adjust the background color at all and why the background color has changed?

Any help is appreciated.

Best regards
Steve

support
Posts: 2930
Joined: 30 Mar 2005, 11:36
Contact:

Re: CADImage Bitmap with black background

Post by support » 26 Jun 2012, 18:35

Hello Steve.
We have tested mentioned issue with CADImage.dll v 7.4.1. The described problem wasn't found. Generally DrawCADToBitmap doesn't draw background, but entities only. I there any possibility bitmap background filled in your application? Please test with VC demo or send us a sample code that you use to test.

Alexander.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

S.D.
Posts: 5
Joined: 25 Jun 2012, 14:20
Location: Germany
Contact:

Re: CADImage Bitmap with black background

Post by S.D. » 26 Jun 2012, 19:31

Thank you for your fast reply!

Unfortunatly I can not find a code sample, where DrawCADtoBitmap is used, but only an example for DrawCADtoDIB. But I will try to give a code sample. I can not exclude, that the error is on my side, but since it worked with the 7.2.5.1 dll, something might have change since than:

We use CADImage to draw dwg images in 2D using DrawCAD on a device context and in 3D as OpenGL textures by drawing into a windows bitmap and convert it afterwards. Interestingly the 2D method returns the image just fine (without a background, but only the entities), in 3D there was always a white background, but now it is black:

Code: Select all

BYTE * GetBitmap(unsigned int mLastTextureWidth, unsigned int mLastTextureHeight)
    SetClippingRectCAD (mCADImage, NULL);

    CADDRAW r;
    r.R.left   = 0L;
    r.R.bottom = 0L;
    r.R.right  = mLastTextureWidth;
    r.R.top    = mLastTextureHeight;
    r.DC = NULL;
    r.Size = sizeof(r);


    try {
      // creates 8 bit per pixel Bitmap
      HANDLE MyBitmap = DrawCADtoBitmap(mCADImage, &r);
      int tmpW = 0;
      int tmpH = 0;
      BYTE * mBitmapBits = GetEmbeddedImage(GlobalLock(MyBitmap));
      DWORD err = GetLastError();

      GlobalUnlock(MyBitmap);
      GlobalFree(MyBitmap);
    }
    catch (...) {
      return NULL;
    }

    return mBitmapBits 
The embedded image is extracted by the following method:

Code: Select all

BYTE *GetEmbeddedImage(HANDLE _src)
{
  BYTE *colorTbl;

  int mOffset = 0;
  BITMAPFILEHEADER bmfh;
  BITMAPINFOHEADER bmih;

  char *src = (char *)_src;
  if (src == NULL) return NULL;

  // read the file header information
  memcpy(&bmfh, src+mOffset, sizeof(bmfh)); 
  mOffset += sizeof(bmfh);

  // read image header information
  memcpy(&bmih, src+mOffset, sizeof(bmih)); 
  mOffset += sizeof(bmih);

  // build the color table (if one exists)
  if ((bmfh.bfOffBits - sizeof(bmih) - sizeof(bmfh)) > 0)
  {
    int colTblSz = bmfh.bfOffBits - sizeof(bmih) - sizeof(bmfh);
    colorTbl = new BYTE[colTblSz];
    memcpy(colorTbl, src+mOffset, sizeof(BYTE) * colTblSz); 
    mOffset += (sizeof(BYTE) * colTblSz);

    if (bmih.biBitCount == 24 || bmih.biBitCount == 32) {
      delete [] colorTbl;
    }
  }
 
  return Get256ColorEmbeddedImage((void *)(src+mOffset), bmih.biWidth, bmih.biHeight, colorTbl);
  }
}
and

Code: Select all

BYTE *Get256ColorEmbeddedImage(HANDLE _src, int w, int h, BYTE *colTbl)
{
  int x, y, z;
  int mOffset = 0;
  int nSkip = (((w*8)+31)&~31) / 8;

  char *src = (char *)_src;
  if (src == NULL) return NULL;

  BYTE *theImgIdx = new BYTE[w];
  BYTE *theImg    = new BYTE[h*w*3];

  for (y = 0; y < h; y++) {

    memcpy(theImgIdx, src+mOffset, sizeof(BYTE)*w);

    for (x = 0; x < w; x++) {
      for (z = 0; z < 3; z++) {
        theImg[(3*(((h-(y+1))*w)+x))+z] = colTbl[((unsigned int) theImgIdx[x]*4)+z];
      }
    }

    mOffset += (sizeof(BYTE)*nSkip);
  }

  delete [] theImgIdx;
  delete [] colTbl;

  return theImg;
}
Unfortunately I am not the original author of the code above, but if I understand it correctly, after the DrawCADtoBitmap method the bitmap is not modified any further, but only converted. If loaded into a texture, the image has a black background.

Is there something I am missing?

Steve

support
Posts: 2930
Joined: 30 Mar 2005, 11:36
Contact:

Re: CADImage Bitmap with black background

Post by support » 06 Jul 2012, 13:56

Hello Steve.
We have tested your code snippet. If I understand correctly R, G and B values noted into theImg for further 3D visualization. Both CADImage versions (7.2.5.1 and 7.4.1) return the same set of color values for a bitmap. It is also not clear if bitmap drawn with own background or just scene background color. Anyway check setting background color in OpenGL.

Alexander.
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

S.D.
Posts: 5
Joined: 25 Jun 2012, 14:20
Location: Germany
Contact:

Re: CADImage Bitmap with black background

Post by S.D. » 01 Aug 2012, 16:07

Hallo,

thanks for your suggestions. I checked the opengl background and all, but this is not the issue.
After this I went deeper into debugging and compared the results of the DrawCADtoBitmap, used within the code, posted above. Directly after loading the the image into the HANDLE MyBitmap, the result differs when using the 7.2.5.1 and 7.4.1.4. After casting the values to unsigned char (as shown in the code snipped above [GetEmbeddedImage]) the values that are 0xff with CADImage.dll 7.2.5.1 are 0x00 when read by the newer one. (The new CADImage 8.0 does also read out 0x00 values). So there has to be something, that changed, has not? Maybe it is important to note, that the CADImage Handle is loaded by the following code:

Code: Select all

int Load (const TCHAR *FileName)
{
  int Result = -1;
  mIsLoaded  = false;

  //new format
  //WCHAR *myFileName = new WCHAR[length];
  //wcscpy (myFileName, FileName);

  //old format
  char *myFileName = new char[length];
  WideCharToMultiByte(CP_ACP, 0, FileName, -1, myFileName, static_cast<int>(length), NULL, NULL);

  mCADImage = CreateCAD(NULL, myFileName);

  delete[] myFileName;

  if (mCADImage != NULL) {
    CurrentLayoutCAD(mCADImage, DefaultLayoutIndex(mCADImage), TRUE);
    ResetDrawingBoxCAD(mCADImage);
    SetCADBorderSize (mCADImage, 0);
    SetCADBorderType(mCADImage, 0);
    
    mIsLoaded = true;
    Result = 1;
  }

  return Result;
}
but I could not make out anything, that might change the behaviour of the Handle. So now I am at a loss here, where I can look for the error. Do you have anymore suggestions?

Best regards
Steve

S.D.
Posts: 5
Joined: 25 Jun 2012, 14:20
Location: Germany
Contact:

Re: CADImage Bitmap with black background

Post by S.D. » 23 Nov 2012, 14:34

Hi,

I am still struggling with the problem above. Temporary I tried to use DrawCADToDIB, however the Dimension of the returned DIB seems to be different from values, given by the rectangle.
So I still trying to get DrawCADToBitmap to work (currently using v.8.0). You told me, that "DrawCADToBitmap doesn't draw background". But the returned Handle is only a bitstream, so how could it not have a background, or am I wrong here? Might you have a sample code, were you extract the bitstream out of the Handle, returned by DrawCADToBitmap, like the sample with DrawCADToDIB in DemoVC?

Many thanks in advance!
Steve

S.D.
Posts: 5
Joined: 25 Jun 2012, 14:20
Location: Germany
Contact:

Re: CADImage Bitmap with black background

Post by S.D. » 26 Nov 2012, 14:24

Hello,

I solved this problem now. For now, I use the methode DrawCAD to draw into a local dc and extracting the bits out of the DIB. However, the bugs I found in DrawCADToDIB and DrawCADToBitmap are still there and might need a fix in the future.

Yours sincerly
Steve

Post Reply