From 1544efa0cd095e84d84b5b773dc0e85eec1f0200 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 12 Jun 2014 17:12:52 +0000 Subject: [PATCH] image: the GDI+ backend can now save images, too. --- build/vs2012/Lol.sln | 32 ++++++++++ src/image/codec/gdiplus-image.cpp | 103 +++++++++++++++++++++++++++--- 2 files changed, 126 insertions(+), 9 deletions(-) diff --git a/build/vs2012/Lol.sln b/build/vs2012/Lol.sln index 7d4bdc23..5d35f622 100644 --- a/build/vs2012/Lol.sln +++ b/build/vs2012/Lol.sln @@ -172,6 +172,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sprite_sorter", "..\..\peop EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "squad_shooter", "..\..\people\touky\private\squad_shooter\squad_shooter.vcxproj", "{DB344310-4938-4294-AEE2-18512454585D}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pimp", "Pimp", "{2195FB18-53BF-48AF-96B6-9BD242924EA5}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pimp", "..\..\tools\pimp\pimp.vcxproj", "{FFF58874-D460-41E6-AD65-DBFEC6E47A94}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ORBIS = Debug|ORBIS @@ -722,6 +726,32 @@ Global {DB344310-4938-4294-AEE2-18512454585D}.Release|x64.ActiveCfg = Release|x64 {DB344310-4938-4294-AEE2-18512454585D}.Release|x64.Build.0 = Release|x64 {DB344310-4938-4294-AEE2-18512454585D}.Release|Xbox 360.ActiveCfg = Release|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|ORBIS.ActiveCfg = Debug|ORBIS + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|ORBIS.Build.0 = Debug|ORBIS + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|PS3.ActiveCfg = Debug|PS3 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|PS3.Build.0 = Debug|PS3 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Win32.ActiveCfg = Debug|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Win32.Build.0 = Debug|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Win32.Deploy.0 = Debug|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|x64.ActiveCfg = Debug|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|x64.Build.0 = Debug|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|x64.Deploy.0 = Debug|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Xbox 360.ActiveCfg = Debug|Xbox 360 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Xbox 360.Build.0 = Debug|Xbox 360 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Debug|Xbox 360.Deploy.0 = Debug|Xbox 360 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|ORBIS.ActiveCfg = Release|ORBIS + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|ORBIS.Build.0 = Release|ORBIS + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|PS3.ActiveCfg = Release|PS3 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|PS3.Build.0 = Release|PS3 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Win32.ActiveCfg = Release|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Win32.Build.0 = Release|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Win32.Deploy.0 = Release|Win32 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|x64.ActiveCfg = Release|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|x64.Build.0 = Release|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|x64.Deploy.0 = Release|x64 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Xbox 360.ActiveCfg = Release|Xbox 360 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Xbox 360.Build.0 = Release|Xbox 360 + {FFF58874-D460-41E6-AD65-DBFEC6E47A94}.Release|Xbox 360.Deploy.0 = Release|Xbox 360 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -763,6 +793,7 @@ Global {32F3F8CF-D22E-45E4-BEB8-AD909E8C5515} = {33704AA4-F2B5-4138-A40D-E3E77F89ED46} {EE203B88-44CF-4859-9D42-7A5F40FECB52} = {8C77EAA8-1077-4EF7-AE53-97C6C60A3601} {B357514A-7881-422D-8358-161B689E7620} = {3D341D8A-E400-4B1D-BC05-B5C35487D9B5} + {2195FB18-53BF-48AF-96B6-9BD242924EA5} = {3D341D8A-E400-4B1D-BC05-B5C35487D9B5} {587FCCE9-1D8D-4398-B8B6-E8F4E9A92233} = {B357514A-7881-422D-8358-161B689E7620} {EE203B88-44CF-4859-9D42-7A5F43FECB52} = {9EA99B18-D352-47F6-BC04-A0B49CAA2772} {25493FC8-75AC-4703-AD58-AB0C4A0FB79C} = {9EA99B18-D352-47F6-BC04-A0B49CAA2772} @@ -780,5 +811,6 @@ Global {AA376B9B-484B-4DC4-982F-6CFA645E441E} = {F7D4A671-612F-4FF4-883F-2097697694B7} {5D994A3B-83BF-4FCB-9AC1-E7B642B46FEA} = {50509BA3-D27B-4FAC-9772-9EC76146F709} {C3E2D690-F2B4-4A11-B317-D5CB8EF9561A} = {454DE15C-9063-4EB1-AD6C-77D7150F15DF} + {FFF58874-D460-41E6-AD65-DBFEC6E47A94} = {2195FB18-53BF-48AF-96B6-9BD242924EA5} EndGlobalSection EndGlobal diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index f1175ecb..18e3f0ce 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -65,15 +65,15 @@ bool GdiPlusImageData::Open(char const *path) Array pathlist = System::GetPathList(path); m_bitmap = nullptr; - for (int i = 0; i < pathlist.Count(); ++i) + for (auto fullpath : pathlist) { size_t len; - len = mbstowcs(nullptr, pathlist[i].C(), 0); + len = mbstowcs(nullptr, fullpath.C(), 0); wchar_t *wpath = new wchar_t[len + 1]; - if (mbstowcs(wpath, pathlist[i].C(), len + 1) == (size_t)-1) + if (mbstowcs(wpath, fullpath.C(), len + 1) == (size_t)-1) { #if !LOL_BUILD_RELEASE - Log::Error("invalid image name %s\n", pathlist[i].C()); + Log::Error("invalid image name %s\n", fullpath.C()); #endif delete[] wpath; continue; @@ -90,7 +90,7 @@ bool GdiPlusImageData::Open(char const *path) #if !LOL_BUILD_RELEASE if (status != Gdiplus::InvalidParameter) Log::Error("error %d loading %s\n", - status, pathlist[i].C()); + status, fullpath.C()); #endif delete m_bitmap; m_bitmap = nullptr; @@ -114,8 +114,8 @@ bool GdiPlusImageData::Open(char const *path) m_format = PixelFormat::RGBA_8; Gdiplus::Rect rect(0, 0, m_size.x, m_size.y); - if(m_bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, - PixelFormat32bppARGB, &m_bdata) != Gdiplus::Ok) + if (m_bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, + PixelFormat32bppARGB, &m_bdata) != Gdiplus::Ok) { #if !LOL_BUILD_RELEASE Log::Error("could not lock bits in %s\n", path); @@ -142,9 +142,94 @@ bool GdiPlusImageData::Open(char const *path) bool GdiPlusImageData::Save(char const *path) { - UNUSED(path); + ULONG_PTR token; + Gdiplus::GdiplusStartupInput input; + Gdiplus::GdiplusStartup(&token, &input, NULL); + + wchar_t const *fmt; + if (strstr(path, ".gif")) + fmt = L"image/gif"; + else if (strstr(path, ".jpg") || strstr(path, ".jpeg")) + fmt = L"image/jpeg"; + else if (strstr(path, ".png")) + fmt = L"image/png"; + else if (strstr(path, ".tiff")) + fmt = L"image/tiff"; + else /* if (strstr(path, ".bmp")) */ + fmt = L"image/bmp"; + + unsigned int num = 0, size = 0; + Gdiplus::GetImageEncodersSize(&num, &size); + if (num == 0) + { + Log::Error("no GDI+ image encoders found\n"); + return false; + } + Gdiplus::ImageCodecInfo *codecs + = (Gdiplus::ImageCodecInfo *)new uint8_t[size]; + Gdiplus::GetImageEncoders(num, size, codecs); + CLSID clsid; + for (unsigned int i = 0; i < num; i++) + { + if (wcscmp(codecs[i].MimeType, fmt) == 0) + { + clsid = codecs[i].Clsid; + break; + } + } + + size_t len; + len = mbstowcs(NULL, path, 0); + wchar_t *wpath = new wchar_t[len + 1]; + if (mbstowcs(wpath, path, len + 1) == (size_t)-1) + { + Log::Error("could not convert GDI+ filename '%s' to widechar\n", path); + delete[] wpath; + delete[] codecs; + return false; + } + + /* FIXME: we create a new image because there is no guarantee that + * the image comes from GDI. But the engine architecture doesn't + * allow us to save other images anyway. */ + Gdiplus::Bitmap *b = new Gdiplus::Bitmap(m_size.x, m_size.y, + PixelFormat32bppARGB); + + Gdiplus::BitmapData bdata; + Gdiplus::Rect rect(0, 0, m_size.x, m_size.y); + + if (b->LockBits(&rect, (unsigned int)Gdiplus::ImageLockModeWrite, + PixelFormat32bppARGB, &bdata) != Gdiplus::Ok) + { + Log::Error("could not lock GDI+ image bits\n"); + delete b; + delete[] wpath; + delete[] codecs; + return false; + } + + u8vec4 *psrc = static_cast(m_bdata.Scan0); + u8vec4 *pdst = static_cast(bdata.Scan0); + for (int y = 0; y < m_size.y; y++) + for (int x = 0; x < m_size.x; x++) + { + *pdst++ = (*psrc++).bgra; + } + + b->UnlockBits(&bdata); + + if (b->Save(wpath, &clsid, NULL) != Gdiplus::Ok) + { + Log::Error("could not save GDI+ image '%s'\n", path); + delete[] wpath; + delete[] codecs; + delete b; + return false; + } + delete[] wpath; + delete[] codecs; + delete b; - /* TODO: unimplemented */ return true; }