Solved

converting .shp to .bmp using c++

Posted on 2004-08-26
4
442 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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