Skip to content

Commit

Permalink
improve msys2 compatibility (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptahmose authored Sep 20, 2024
1 parent 900cada commit ae6130d
Show file tree
Hide file tree
Showing 40 changed files with 172 additions and 214 deletions.
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW) # enable new "MSVC runtime library selection" (https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html)

project(libCZI
VERSION 0.62.0
VERSION 0.62.1
HOMEPAGE_URL "https://github.com/ZEISS/libczi"
DESCRIPTION "libCZI is an Open Source Cross-Platform C++ library to read and write CZI")

Expand Down Expand Up @@ -75,6 +75,17 @@ check_cxx_symbol_exists(pwrite unistd.h HAVE_UNISTD_H_PWRITE)
BoolToFoundNotFound(HAVE_UNISTD_H_PWRITE HAVE_UNISTD_H_PWRITE_TEXT)
message("check for open -> ${HAVE_FCNTL_H_OPEN_TEXT} ; check for pread -> ${HAVE_UNISTD_H_PREAD_TEXT} ; check for pwrite -> ${HAVE_UNISTD_H_PWRITE_TEXT}")

# Determine whether the Win32-API can be used (in other words: whether we are on a Windows-platform or targetting the Windows-platform).
# Note that just checking for WIN32 is not sufficient here, as this is not defined with environments such as msys2. In those cases, UNIX is defined.
# The meaning of this switch is: whether Win32-API can be used (and this affects not only compilation but e.g. also linking to the Win32-API, as
# it is not automatically linked to when using MinGW or Cygwin).
# TODO(JBL): check whether cross-compiling to Windows works
if (WIN32 OR CYGWIN OR MSYS OR MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(LIBCZI_HAVE_WIN32_API ON)
else()
set(LIBCZI_HAVE_WIN32_API OFF)
endif()

# This option controls whether to build the curl-based http-/https-stream object. If this option is
# "ON", the build will fail if the curl-library is not available (either as an external package or
# as a private copy downloaded during the CMake-run).
Expand Down
2 changes: 0 additions & 2 deletions Src/CZICmd/BitmapGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "stdafx.h"

#include "IBitmapGen.h"
#include "BitmapGenGdiplus.h"
#include "BitmapGenNull.h"
Expand Down
1 change: 0 additions & 1 deletion Src/CZICmd/BitmapGenFreeType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "stdafx.h"
#include "inc_CZIcmd_Config.h"

#if CZICMD_USE_FREETYPE == 1
Expand Down
9 changes: 4 additions & 5 deletions Src/CZICmd/BitmapGenGdiplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "stdafx.h"
#include "BitmapGenGdiplus.h"
#include "BitmapGenNull.h"
#include "utils.h"
Expand Down Expand Up @@ -47,7 +46,7 @@ class CGdiplusBitmapWrapper : public libCZI::IBitmapData
{
auto gdiplusPxlFmt = this->bitmap->GetPixelFormat();
libCZI::BitmapLockInfo bitmapLockInfo;
Rect rect(0, 0, (INT)this->bitmap->GetWidth(), (INT)this->bitmap->GetHeight());
Rect rect(0, 0, static_cast<INT>(this->bitmap->GetWidth()), static_cast<INT>(this->bitmap->GetHeight()));
this->bitmap->LockBits(&rect, ImageLockModeRead, gdiplusPxlFmt, &this->bd);
bitmapLockInfo.ptrData = this->bd.Scan0;
bitmapLockInfo.ptrDataRoi = this->bd.Scan0;
Expand Down Expand Up @@ -121,7 +120,7 @@ CBitmapGenGdiplus::CBitmapGenGdiplus(const IBitmapGenParameters* params) : fonth
Graphics g(bitmap.get());
g.Clear(Color(255, 0, 0));

Font font(this->fontname.c_str(), (REAL)this->fontheight, FontStyle::FontStyleBold, UnitPoint);
Font font(this->fontname.c_str(), static_cast<REAL>(this->fontheight), FontStyle::FontStyleBold, UnitPoint);
SolidBrush brush(Color(0, 0, 0));

auto text = IBitmapGen::CreateTextW(info);
Expand Down Expand Up @@ -173,7 +172,7 @@ CBitmapGenGdiplus::CBitmapGenGdiplus(const IBitmapGenParameters* params) : fonth
else if (pixeltype == PixelType::Gray8)
{
auto bw = make_shared<CNullBitmapWrapper>(PixelType::Gray8, width, height);
Rect rect(0, 0, width, height);
Rect rect(0, 0, static_cast<INT>(width), static_cast<INT>(height));
BitmapData bd;
bitmap->LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
auto _ = finally([&] { bitmap->UnlockBits(&bd); }); // execute "UnlockBits" when leaving scope
Expand Down Expand Up @@ -210,7 +209,7 @@ CBitmapGenGdiplus::CBitmapGenGdiplus(const IBitmapGenParameters* params) : fonth
else if (pixeltype == PixelType::Bgr48)
{
auto bw = make_shared<CNullBitmapWrapper>(PixelType::Bgr48, width, height);
Rect rect(0, 0, width, height);
Rect rect(0, 0, static_cast<INT>(width), static_cast<INT>(height));
BitmapData bd;
bitmap->LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
auto _ = finally([&] { bitmap->UnlockBits(&bd); });
Expand Down
9 changes: 4 additions & 5 deletions Src/CZICmd/BitmapGenNull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "stdafx.h"
#include "BitmapGenNull.h"
#include "utils.h"
#include "inc_libCZI.h"
Expand Down Expand Up @@ -37,14 +36,14 @@ void CNullBitmapWrapper::Clear(const ColorSpecification& color)
case libCZI::PixelType::Gray8:
for (std::uint32_t y = 0; y < this->height; ++y)
{
memset(((std::uint8_t*)this->ptrData) + static_cast<size_t>(y) * this->stride, color.Gray8.value, this->width);
memset(static_cast<std::uint8_t*>(this->ptrData) + static_cast<size_t>(y) * this->stride, color.Gray8.value, this->width);
}

break;
case libCZI::PixelType::Bgr24:
for (std::uint32_t y = 0; y < this->height; ++y)
{
std::uint8_t* ptr = ((std::uint8_t*)this->ptrData) + static_cast<size_t>(y) * this->stride;
std::uint8_t* ptr = static_cast<std::uint8_t*>(this->ptrData) + static_cast<size_t>(y) * this->stride;
for (std::uint32_t x = 0; x < this->height; ++x)
{
*ptr++ = color.Bgr24.b;
Expand All @@ -57,7 +56,7 @@ void CNullBitmapWrapper::Clear(const ColorSpecification& color)
case libCZI::PixelType::Gray16:
for (std::uint32_t y = 0; y < this->height; ++y)
{
std::uint16_t* ptr = (std::uint16_t*)(((std::uint8_t*)this->ptrData) + static_cast<size_t>(y) * this->stride);
std::uint16_t* ptr = reinterpret_cast<std::uint16_t*>(static_cast<std::uint8_t*>(this->ptrData) + static_cast<size_t>(y) * this->stride);
for (std::uint32_t x = 0; x < this->height; ++x)
{
*ptr++ = color.Gray16.value;
Expand All @@ -68,7 +67,7 @@ void CNullBitmapWrapper::Clear(const ColorSpecification& color)
case libCZI::PixelType::Bgr48:
for (std::uint32_t y = 0; y < this->height; ++y)
{
std::uint16_t* ptr = ((std::uint16_t*)this->ptrData) + static_cast<size_t>(y) * this->stride;
std::uint16_t* ptr = static_cast<std::uint16_t*>(this->ptrData) + static_cast<size_t>(y) * this->stride;
for (std::uint32_t x = 0; x < this->height; ++x)
{
*ptr++ = color.Bgr48.b;
Expand Down
16 changes: 11 additions & 5 deletions Src/CZICmd/BitmapGenNull.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ class CNullBitmapWrapper : public libCZI::IBitmapData
// TODO: posX/posY must be positive for this code to work correctly
for (int y = posY; y < posY + height; ++y)
{
if (y >= (int)this->height) break;
if (y >= static_cast<int>(this->height))
{
break;
}

const std::uint8_t* ptr = ((const std::uint8_t*)ptrData) + (y - posY) * stride;
std::uint8_t* ptrDst = ((std::uint8_t*)this->ptrData) + y * this->stride + tBytesPerPel * posX;
const std::uint8_t* ptr = static_cast<const std::uint8_t*>(ptrData) + static_cast<size_t>(y - posY) * stride;
std::uint8_t* ptrDst = static_cast<std::uint8_t*>(this->ptrData) + static_cast<size_t>(y) * this->stride + tBytesPerPel * static_cast<size_t>(posX);
int v = 0x80;
for (int x = posX; x < posX + width; ++x)
{
if (x >= (int)this->width) break;
if (x >= static_cast<int>(this->width))
{
break;
}

bool pixel = (*ptr & v);
const bool pixel = (*ptr & v);
if (pixel)
{
setPixel.setPixel(ptrDst);
Expand Down
46 changes: 24 additions & 22 deletions Src/CZICmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

IF(UNIX)
IF(NOT LIBCZI_HAVE_WIN32_API)
find_package(ZLIB QUIET)
if (NOT ZLIB_FOUND)
message(FATAL_ERROR [=[
Expand All @@ -24,7 +24,7 @@ IF(UNIX)
message("** FreeType was not found, CZICmd with the option \"CreateCZI\" will create only empty images.")
message(" (consider installing FreeType with e.g. 'sudo apt-get install libfreetype6-dev')")
ENDIF()
ENDIF(UNIX)
ENDIF()

include(FetchContent)

Expand All @@ -39,36 +39,39 @@ if (LIBCZI_BUILD_PREFER_EXTERNALPACKAGE_RAPIDJSON)
]=])
endif()
else()
# since "RapidJSON" is a header-only library, we just have to download it and point to the include directory
FetchContent_Declare(
# Since "RapidJSON" is a header-only library, we just have to download it and point to the include directory.
# Note: when using v1.1.0 of RapidJSON (the latest release) there we problems (with GCC14.2 with msys2), so
# we use a later version from the master branch.
FetchContent_Declare(
RapidJSON
GIT_REPOSITORY https://github.com/Tencent/rapidjson.git
GIT_TAG "v1.1.0"
GIT_TAG 7c73dd7de7c4f14379b781418c6e947ad464c818 # master as of 2024-08-16
GIT_SHALLOW TRUE
PREFIX "${CMAKE_BINARY_DIR}/vendor/rapidjson"
)

if (NOT rapidjson_POPULATED)
FetchContent_Populate(RapidJSON)
set(RAPIDJSON_INCLUDE_DIRS ${rapidjson_SOURCE_DIR}/include)
endif()
set(RAPIDJSON_BUILD_DOC OFF CACHE BOOL "" FORCE)
set(RAPIDJSON_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(RAPIDJSON_BUILD_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(RapidJSON)

set(RAPIDJSON_INCLUDE_DIRS ${rapidjson_SOURCE_DIR}/include)
endif()

# make "CLI11" available
FetchContent_Declare(
cli11
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
GIT_TAG v2.3.2
GIT_TAG v2.4.2
)

if (NOT cli11_POPULATED)
FetchContent_MakeAvailable(cli11)
endif()
set(CLI11_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(CLI11_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(cli11)

set (CZICMDSRCFILES
BitmapGen.cpp
DisplaySettingsHelper.h
targetver.h
BitmapGenFreeType.cpp
cmdlineoptions.cpp
execute.cpp
Expand All @@ -84,17 +87,14 @@ set (CZICMDSRCFILES
consoleio.cpp
executeCreateCzi.h
inc_CZIcmd_Config.h
stdafx.cpp
BitmapGenGdiplus.h
consoleio.h
execute.h
inc_libCZI.h
stdafx.h
BitmapGenNull.cpp
inc_rapidjson.h
BitmapGenNull.h
CZIcmd.cpp
platform_defines.h
executePlaneScan.h
executePlaneScan.cpp
executeBase.h
Expand All @@ -108,8 +108,8 @@ target_compile_definitions(CZIcmd PRIVATE _LIBCZISTATICLIB)
target_link_libraries(CZIcmd PRIVATE ${ZLIB_LIBRARIES} ${PNG_LIBRARIES} CLI11::CLI11 libCZIStatic)
target_include_directories(CZIcmd PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${RAPIDJSON_INCLUDE_DIRS})

IF(WIN32)
target_link_libraries(CZIcmd PRIVATE gdiplus Windowscodecs)
IF(LIBCZI_HAVE_WIN32_API)
target_link_libraries(CZIcmd PRIVATE gdiplus Windowscodecs ole32)
ENDIF()

IF (FREETYPE_FOUND)
Expand All @@ -118,16 +118,17 @@ IF (FREETYPE_FOUND)
target_link_libraries(CZIcmd PRIVATE ${FREETYPE_LIBRARIES})
ENDIF()

IF(UNIX)
IF(NOT LIBCZI_HAVE_WIN32_API)
target_include_directories(CZIcmd PRIVATE ${ZLIB_INCLUDE_DIR} ${PNG_INCLUDE_DIR})
# seems to be problem with glibc I'd reckon -> https://stackoverflow.com/questions/51584960/stdcall-once-throws-stdsystem-error-unknown-error-1
target_link_libraries(CZIcmd PUBLIC pthread)
ENDIF(UNIX)
ENDIF()

set(CZICMD_USE_FREETYPE 0)
set(CZICMD_USE_WIC 0)
set(CZICMD_USE_GDIPLUS 0)
set(CZICMD_USE_LIBPNG 0)
set(CZICMD_WINDOWSAPIAVAILABLE 0)

IF (FREETYPE_FOUND)
set(CZICMD_USE_FREETYPE 1)
Expand All @@ -139,7 +140,8 @@ IF(PNG_FOUND)
set(CZICMD_LIBPNG_VERSION_STRING "${PNG_VERSION_STRING}")
ENDIF()

IF(WIN32)
IF(LIBCZI_HAVE_WIN32_API)
set(CZICMD_WINDOWSAPIAVAILABLE 1)
set(CZICMD_USE_WIC 1)
set(CZICMD_USE_GDIPLUS 1)
ENDIF()
Expand Down
20 changes: 7 additions & 13 deletions Src/CZICmd/CZIcmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "stdafx.h"
#include "inc_CZIcmd_Config.h"
#include "consoleio.h"
#include "cmdlineoptions.h"
#include "execute.h"
#include "inc_libCZI.h"

#if defined(LINUXENV)
#include <clocale>
#endif

#if defined(WIN32ENV)
#define NOMINMAX
#if CZICMD_WINDOWSAPI_AVAILABLE
#include <Windows.h>
#endif

Expand All @@ -24,7 +19,7 @@ class CLibCZISite : public libCZI::ISite
public:
explicit CLibCZISite(const CCmdLineOptions& opts) : options(opts)
{
#if defined(WIN32ENV)
#if CZICMD_WINDOWSAPI_AVAILABLE
if (options.GetUseWICJxrDecoder())
{
this->pSite = libCZI::GetDefaultSiteObject(libCZI::SiteObjectType::WithWICDecoder);
Expand Down Expand Up @@ -61,11 +56,10 @@ class CLibCZISite : public libCZI::ISite

int main(int argc, char** _argv)
{
#if defined(WIN32ENV)
#if CZICMD_WINDOWSAPI_AVAILABLE
CoInitialize(NULL);
CommandlineArgsWindowsHelper args_helper;
#endif
#if defined(LINUXENV)
#else
setlocale(LC_CTYPE, "");
#endif

Expand All @@ -74,7 +68,7 @@ int main(int argc, char** _argv)
try
{
CCmdLineOptions options(log);
#if defined(WIN32ENV)
#if CZICMD_WINDOWSAPI_AVAILABLE
auto cmdLineParseResult = options.Parse(args_helper.GetArgc(), args_helper.GetArgv());
#else
auto cmdLineParseResult = options.Parse(argc, _argv);
Expand Down Expand Up @@ -110,7 +104,7 @@ int main(int argc, char** _argv)
retVal = 1;
}

#if defined(WIN32ENV)
#if CZICMD_WINDOWSAPI_AVAILABLE
CoUninitialize();
#endif

Expand Down
3 changes: 3 additions & 0 deletions Src/CZICmd/CZIcmd_Config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#pragma once

// whether Win32-API can be used
#define CZICMD_WINDOWSAPI_AVAILABLE @CZICMD_WINDOWSAPIAVAILABLE@

#define CZICMD_USE_WIC @CZICMD_USE_WIC@

#define CZICMD_USE_LIBPNG @CZICMD_USE_LIBPNG@
Expand Down
Loading

0 comments on commit ae6130d

Please sign in to comment.