CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Manipulate bitmaps images in C++

    I need to segment a scanned page to character zones.... something like identify the layouts of the page into text box and pictures.
    The input of my program is a scanned page saved in bitmap format using a scanner, and my output must be a bitmap file of boxes
    to show the text boxes.

    In this lab, there is a bonus part for seperating the text boxes into lines.

    I am quite new to bitmap processing, and I will be programming in C++ under UNIX.
    Please help me if you know how do I decipher a bitmap?
    How do we actually open a bitmap in the 1st place?
    I have never open any file except ASCII files

    Thanks


  2. #2
    Join Date
    May 1999
    Posts
    8

    Re: Manipulate bitmaps images in C++

    Example code in MFC for opening a Bitmap file.

    BOOL CDib::ReadDib(const char* imageFile)
    {
    DWORD nBytes;
    HANDLE hFile;
    DWORD DibSize;

    //----------------------
    // Open the bitmap file.
    //----------------------
    hFile = CreateFile(imageFile, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
    char errstr[100];

    wsprintf(errstr, "Error opening file %s.", imageFile);
    AfxMessageBox(errstr);
    return FALSE;
    }

    //----------------------------
    // Get the bitmap file header.
    //----------------------------
    ReadFile(hFile, &m_Bmfh, sizeof(BITMAPFILEHEADER), &nBytes, NULL);

    //--------------------------------------
    // If the header was not read completely
    //--------------------------------------
    if (nBytes != sizeof(BITMAPFILEHEADER))
    {
    AfxMessageBox("Could not read BITMAPFILEHEADER from file.");
    CloseHandle(hFile);
    return FALSE;
    }

    //----------------------------------------
    // If the file type does indicate a bitmap
    //----------------------------------------
    if (m_Bmfh.bfType != * (WORD *) "BM")
    {
    AfxMessageBox("File type does not indicate BM.");
    CloseHandle(hFile);
    return FALSE;
    }

    //----------------------------------------
    // Get the device-independent bitmap size.
    //----------------------------------------
    DibSize = m_Bmfh.bfSize - sizeof(BITMAPFILEHEADER);

    //----------------------------------------------------------------
    // Allocate memory to hold the complete device-independent bitmap.
    //----------------------------------------------------------------
    m_pData = new BYTE[DibSize + sizeof(BITMAPFILEHEADER)];

    //---------------------------
    // If error allocating memory
    //---------------------------
    if (!m_pData)
    {
    AfxMessageBox("Error allocating memory for bitmap.");
    CloseHandle(hFile);
    return FALSE;
    }

    //---------------------------------------------
    // Copy the bitmap file header into the memory.
    //---------------------------------------------
    CopyMemory(m_pData, &m_Bmfh, sizeof(BITMAPFILEHEADER));
    m_pBmfh = (BITMAPFILEHEADER*) m_pData;

    //-----------------------------------------------------------------
    // Read the remaining bitmap data following the bitmap file header.
    //-----------------------------------------------------------------
    ReadFile(hFile, &m_pData[sizeof(BITMAPFILEHEADER)], DibSize, &nBytes, NULL);

    CloseHandle(hFile);

    //-----------------------------------
    // If error reading all of the bitmap
    //-----------------------------------
    if (nBytes != DibSize)
    {
    AfxMessageBox("Error reading DIB.");
    return FALSE;
    }

    //-------------------------------------------
    // Set the address of the bitmap info header.
    //-------------------------------------------
    m_pBmih = (BITMAPINFOHEADER*) &m_pData[sizeof(BITMAPFILEHEADER)];

    //------------------------
    // Verify the header size.
    //------------------------
    if (m_pBmih->biSize < 12 || (m_pBmih->biSize > 12 && m_pBmih->biSize < 16))
    {
    AfxMessageBox("Invalid DIB info header size.");
    return FALSE;
    }

    return TRUE;
    }

    Hope this helps.
    Regards,
    Abadon


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured