Skip to content

Implemented Image.Load for Windows using GDI+ #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Source/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
using std::memcpy;
using std::memcmp;

#ifdef ROBOT_OS_WIN

#include <string>
#include <Windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

#endif

ROBOT_NS_BEGIN


Expand Down Expand Up @@ -153,6 +162,96 @@ void Image::Destroy (void)

////////////////////////////////////////////////////////////////////////////////

bool Image::Load(const char *filename)
{
#ifdef ROBOT_OS_WIN

auto hwnd = GetDesktopWindow();
auto hDesktopDC = GetDC(hwnd);
auto hMemoryDC = ::CreateCompatibleDC(hDesktopDC);

// For convert filename to WCHAR
int size = 0;

// Convert UTF8 string to wide-string
size = MultiByteToWideChar(CP_UTF8, 0, filename, (int)strlen(filename), nullptr, 0);
std::wstring filenameW(size, 0);
size = MultiByteToWideChar(CP_UTF8, 0, filename, (int)strlen(filename), &filenameW[0], size);

// Initialize gdi+
Gdiplus::GdiplusStartupInput si;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &si, NULL);

// Load image file
Gdiplus::Image* pGdiImage = new Gdiplus::Image(filenameW.c_str());

// Converting to bitmap
HBITMAP hBitmap;
Gdiplus::Bitmap* pGdiBitmap = static_cast<Gdiplus::Bitmap*>(pGdiImage);
pGdiBitmap->GetHBITMAP(Gdiplus::Color(0, 0, 0), &hBitmap);

RESET(*this);
if (Create(pGdiImage->GetWidth(), pGdiImage->GetHeight()) == false)
{
delete pGdiImage;
Gdiplus::GdiplusShutdown(gdiplusToken);
return false;
}

bool bFail = true;
HDC hCompatibleDC = nullptr;
HGDIOBJ hOldObj = nullptr;
do
{
struct BITMAPINFO3
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[260];
} bmi;

hCompatibleDC = CreateCompatibleDC(hMemoryDC);
if (!hCompatibleDC)
break;

bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biBitCount = 0;
if (!GetDIBits(hCompatibleDC, hBitmap, 0, 0, nullptr, (LPBITMAPINFO)&bmi, DIB_RGB_COLORS))
break;

bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biHeight = -bmi.bmiHeader.biHeight;

hOldObj = SelectObject(hCompatibleDC, hBitmap);
if (!(GetDIBits(hCompatibleDC, hBitmap, 0, -bmi.bmiHeader.biHeight, GetData(), (LPBITMAPINFO)&bmi, DIB_RGB_COLORS)))
break;

bFail = false;
} while (false);

if (hOldObj)
SelectObject(hCompatibleDC, hOldObj);
if (hCompatibleDC)
DeleteDC(hCompatibleDC);
if (hMemoryDC)
DeleteDC(hMemoryDC);
if (pGdiImage)
delete pGdiImage;

// Shutdown gdi+
Gdiplus::GdiplusShutdown(gdiplusToken);

return (bFail == false);

#endif

// #TODO implement function for MacOS & Linux

return false;
}

////////////////////////////////////////////////////////////////////////////////

Color Image::GetPixel (const Point& point) const
{
return GetPixel (point.X, point.Y);
Expand Down
2 changes: 2 additions & 0 deletions Source/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class ROBOT_EXPORT Image
bool Create (uint16 w, uint16 h);
void Destroy (void);

bool Load (const char* filename);

uint16 GetWidth (void) const { return mWidth; }
uint16 GetHeight (void) const { return mHeight; }
uint32 GetLength (void) const { return mLength; }
Expand Down