⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagedata.cpp

📁 图像显示软件源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
    TCHAR Buffer[_MAX_PATH] = {0};
    _tcscpy_s(Buffer, _MAX_PATH, GetSourceFilePath().c_str());
    PathRemoveFileSpec(Buffer);
    PathAddBackslash(Buffer);

    return std::tstring(Buffer);
}


std::tstring CImageData::GetSourceLine() const
{
    std::tstring ret;

    if (NULL == Bitmap && NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            std::wstring Text = reinterpret_cast<LPWSTR>(ptr);                  // Get the text
            std::wstring::size_type start = Text.find(L"\n") + 1;               // Skip over the file path
            std::wstring::size_type end = Text.find(L"\n", start);              // The line number is the second line

            ret = CW2CT(Text.substr(start, end - start).c_str());
        }
    }

    return ret;
}

std::tstring CImageData::GetSourceFunction() const
{
    std::tstring ret;

    if (NULL == Bitmap && NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            std::wstring Text = reinterpret_cast<LPWSTR>(ptr);                  // Get the text
            std::wstring::size_type start = Text.find(L"\n") + 1;               // Skip over the file path
            start = Text.find(L"\n", start) + 1;                                // Skip over the line number
            std::wstring::size_type end = Text.find(L"\n", start);              // The function name is the third line

            ret = CW2CT(Text.substr(start, end - start).c_str());
        }
    }

    return ret;
}

std::tstring CImageData::GetTime() const
{
    std::tstring ret;

    if (NULL == Bitmap && NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            std::wstring Text = reinterpret_cast<LPWSTR>(ptr);                  // Get the text
            std::wstring::size_type start = Text.find(L"\n") + 1;               // Skip over the file path
            start = Text.find(L"\n", start) + 1;                                // Skip over the line number
            start = Text.find(L"\n", start) + 1;                                // Skip over the function name
            std::wstring::size_type end = Text.find(L"\n", start);              // The time is the fourth line

            ret = CW2CT(Text.substr(start, end - start).c_str());
        }
    }

    return ret;
}

std::tstring CImageData::GetText() const
{
    std::tstring ret;

    if (NULL == Bitmap && NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            std::wstring Text = reinterpret_cast<LPWSTR>(ptr);                  // Get the text
            std::wstring::size_type start = Text.find(L"\n") + 1;               // Skip over the file path
            start = Text.find(L"\n", start) + 1;                                // Skip over the line number
            start = Text.find(L"\n", start) + 1;                                // Skip over the function name
            start = Text.find(L"\n", start) + 1;                                // Skip over the time

            ret = CW2CT(Text.substr(start).c_str());                            // The text is the remaining lines

            std::tstring::size_type finder = ret.find_first_of(_T("\r\n"));     // change any <CR>s or <LF>s to <CRLF>s for proper display in edit controls
            while (finder != std::tstring::npos)
            {
                if (ret[finder] == _T('\n'))
                {
                    if (finder == 0 || ret[finder - 1] != _T('\r'))
                    {
                        ret.insert(finder++, _T("\r"));
                    }
                }
                else if (ret[++finder] != _T('\n'))
                {
                    ret.insert(finder++, _T("\n"));
                }

                finder = ret.find_first_of(_T("\r\n"), ++finder);
            }
        }
    }

    return ret;
}

CBitmapInfoHeader CImageData::GetBitmapInfoHeader() const
{
    CBitmapInfoHeader BitmapInfoHeader;

    if (NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);

        if (NULL != Bitmap)
        {
            BITMAP b;
            if (GetObject(Bitmap, sizeof(BITMAP), &b))
            {
                BitmapInfoHeader.biWidth = b.bmWidth;
                BitmapInfoHeader.biHeight = b.bmHeight;
                BitmapInfoHeader.biBitCount = 0 == BPP ? b.bmBitsPixel : BPP;
            }
        }
        else if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            LPWSTR Text = reinterpret_cast<LPWSTR>(ptr);                        // The text is next
            ptr += (wcslen(Text) + 1) * sizeof(WCHAR);                          // Skip over all the text

            LPBITMAPINFO pBitmapInfo = reinterpret_cast<LPBITMAPINFO>(ptr);     // The BITMAPINFO structure is after the text

            BitmapInfoHeader = pBitmapInfo->bmiHeader;
        }
    }

    return BitmapInfoHeader;
}

HBITMAP CImageData::GetBitmap() const
{
    if (NULL != Bitmap)
    {
        HBITMAP ret = (HBITMAP)CopyImage(Bitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
        return ret;
    }

    HBITMAP hBitmap = NULL;

    if (NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            LPBYTE ptr = pData + sizeof(ULONG) + sizeof(DWORD) + sizeof(RECT);  // Skip over the reference counter, ProcessID and the region rectangle
            LPWSTR Text = reinterpret_cast<LPWSTR>(ptr);                        // The text is next
            ptr += (wcslen(Text) + 1) * sizeof(WCHAR);                          // Skip over all the text

            LPBITMAPINFO pBitmapInfo = reinterpret_cast<LPBITMAPINFO>(ptr);     // The BITMAPINFO structure is after the text
            ptr += sizeof(BITMAPINFO);

            int ColourTableSize = pBitmapInfo->bmiHeader.biBitCount < 16 ? sizeof(RGBQUAD) * 256 : 0;
            LPRGBQUAD pColourTable = NULL;
            if (0 != ColourTableSize)
            {
                pColourTable = reinterpret_cast<LPRGBQUAD>(ptr);                // If there is a colour table it is after the BITMAPINFO structure
                ptr += ColourTableSize;
            }

            pja::CCompatibleDC DC;
            LPVOID dummy = NULL;
            hBitmap = CreateDIBSection(DC, pBitmapInfo, DIB_RGB_COLORS, &dummy, NULL, 0);

            if (NULL != hBitmap)
            {
                if (NULL != pColourTable)
                {
                    SetDIBColorTable(DC, 0, 1 << pBitmapInfo->bmiHeader.biBitCount, pColourTable);
                }

                if (!SetDIBits(DC, hBitmap, 0, pBitmapInfo->bmiHeader.biHeight, ptr, pBitmapInfo, DIB_RGB_COLORS))  // Last is the bitmap bits
                {
                    DeleteObject(hBitmap);
                    hBitmap = NULL;
                }
            }
        }
    }

    return hBitmap;
}

// write the data into a CArchive. See CImageViewerDoc::Serialize()
void CImageData::SaveData(CArchive &ar)
{
    if (NULL != pCriticalSection)
    {
        CSingleLock SingleLock(pCriticalSection, TRUE);
        if (NULL != pData)
        {
            ar << DataSize;
            ar.Write(pData + sizeof(ULONG), DataSize);
        }
    }
}

// read the data from a CArchive. See CImageViewerDoc::Serialize()
bool CImageData::ReadData(CArchive &ar)
{
    bool ret = false;

    if (WAIT_OBJECT_0 != WaitForSingleObject(theApp.MemoryEvent, 0))
    {
        // out of memory, we cannot copy the image data
        return ret;
    }

    DeleteData();

    ar >> DataSize;
    if (0 != DataSize)
    {
        try
        {
            pData = new BYTE[DataSize + sizeof(ULONG)];
            memset(pData, 0, DataSize + sizeof(ULONG));
            pCriticalSection = new CCriticalSection;

            if (ar.Read(pData + sizeof(ULONG), (UINT)DataSize) == DataSize)
            {
                ret = true;
                IncrementReferenceCount();
            }
            else
            {
                AfxThrowArchiveException(CArchiveException::endOfFile);
            }
        }
        catch (CMemoryException *me)
        {
            theApp.m_pMainWnd->PostMessage(WM_SETMESSAGESTRING, IDS_OUTOFMEMORY, 0);
            theApp.MemoryEvent.ResetEvent();
            TRACE("\n***************************  Out of Memory  *****************************\n\n");
            me->ReportError();
            me->Delete();
            ret = false;
        }
    }

    if (!ret)
    {
        delete[] pData;
        pData = NULL;
        DataSize = 0;

        delete pCriticalSection;
        pCriticalSection = NULL;
    }

    if (ret)
    {
        MemUsage += GetSize();
        if (MemUsage / 1024 >= theApp.MaximumMemoryUsage)
        {
            theApp.m_pMainWnd->PostMessage(WM_SETMESSAGESTRING, IDS_OUTOFMEMORY, 0);
            theApp.MemoryEvent.ResetEvent();
            ret = false;
        }
    }

    return ret;
}

size_t CImageData::GetSize(void) const
{
    // Image data + reference counter + this + critical section object
    return DataSize + sizeof(ULONG) + sizeof(CImageData) + sizeof(CCriticalSection);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -