Solved

converting .shp to .bmp using c++

Posted on 2004-08-26
4
439 Views
Last Modified: 2010-08-05
Hi ,

Can anyone help to solve the following problem,

How to convert shape files to bmp images using c++

How to create a bmp image using c++


waiting for ur replies
0
Comment
Question by:fraughtneo
4 Comments
 

Accepted Solution

by:
mikeant78 earned 125 total points
ID: 12065750
I assume that you are working on a Windows platform. Based on Google search it looks like .shp file is a text format with graphics instructions. If you are willing to parse that format yourself you can do the following:

1. read in the .shp file
2. Create a Windows DIB section (in-memory) bitmap. This can be done similar to the following:

                               UByte* pbitmapData;
                               UInt bytespp = 3, bitspp = bytespp *8;
                               
                    // Bytes/line
                    UInt bpl = ((bytespp*width)+3)&(~3);

            struct      {
                  BITMAPINFOHEADER      Header;
                  RGBQUAD                        Colors[256];
                  }      Info;
            
            // Initialize header for bmp info
            Info.Header.biSize            = sizeof(BITMAPINFOHEADER);
            Info.Header.biWidth            = width;
            Info.Header.biHeight            = -SInt(height);
            Info.Header.biPlanes            = 1;
            Info.Header.biBitCount      = bitspp;
            Info.Header.biCompression      = BI_RGB;
            Info.Header.biSizeImage      = bpl*height;
            Info.Header.biXPelsPerMeter      = 0;
            Info.Header.biYPelsPerMeter      = 0;
            Info.Header.biClrUsed            = bytespp*256;
            Info.Header.biClrImportant      = Info.Header.biClrUsed;

            HBITMAP hbmp = ::CreateDIBSection(0, (BITMAPINFO*)&Info, DIB_RGB_COLORS, (VOID**)&pbitmapData, NULL, NULL);

3. Create a device context and select bitmap into it. You will be able to draw into the bitmap with it.

      HDC hdc=CreateCompatibleDC(0);
      SelectObject(hdc,hbmp );

4. Parse the .shp file and use Windows GDI to draw into the HDC. This will draw into the bitmap.

5. Close the hdc when you are done with DeleteDC(hdc).

6. Serialize the bitmap to file. This can be done similar to the following (you will need to modify this based on file IO mechanism, etc.):

      BITMAPFILEHEADER            bmh;
      BITMAPINFOHEADER            bmi;
      SInt                  size, w;      
      SInt                  height = pbitmap->GetHeight();
      UByte                  *pbuffer;      



// Determine the data size
      size      = height * bpl;

#define      PACK_INT16_B(b1,b2)      SInt16(UInt8(b1)|(UInt8(b2)<<8))

      // Initialize file header
      bmh.bfType            = PACK_INT16_B('B','M');
      bmh.bfSize            = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + size;
      bmh.bfReserved1            =
      bmh.bfReserved2            = 0;
      bmh.bfOffBits            = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
      // And bitmap header
      bmi.biSize                  = sizeof(BITMAPINFOHEADER);
      bmi.biWidth            = width;
      bmi.biHeight            = height;
      bmi.biPlanes            = 1;
      bmi.biBitCount            = 24;
      bmi.biCompression       = BI_RGB;
      bmi.biSizeImage            = size;
      bmi.biXPelsPerMeter   =
      bmi.biYPelsPerMeter   = 0;
      bmi.biClrUsed      = 1<<24;
      bmi.biClrImportant      = 0;

      // Last line in temporary bitmap's used as a buffer
      pbuffer      = pbitmapData + size;
      w      = bpl;

      // We need to flip the bitmap data
      // Windows bitmaps are bottom - up
      for (SInt i=0; i<(height>>1); i++)
            {
            memcpy(pbuffer, pbitmapData+i*w, w);
            memcpy(pbitmapData+i*w, pbitmapData+(height-i-1)*w, w);
            memcpy(pbitmapData+(height-i-1)*w, pbuffer, w);
            }

      // And finally write the data
      pfile->Write((UByte*)&bmh, sizeof(BITMAPFILEHEADER));
      pfile->Write((UByte*)&bmi, sizeof(BITMAPINFOHEADER));
      if (pfile->Write(pbitmapData, size)<size)
            return 0;

Note that this is just sample code to give you the idea. You will need to modify it for your purpose.

Of couse, it may be a better idea to try to find a libraray/program which already does what you need.




0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now