FreeImage是一个非常好的支持非常多图片格式的库。
写跨平台渲染引擎的时候,如果不想依赖各个平台的API,是一个非常好的选择。
但在macOS上,我们这里下载的是SourceForge上的最新版本FreeImage3180.zip
如果不喜欢brew下载,自己下载编译,会遇到一些问题。首先是C99的错误。
在这里,brew里面很早就有开发者反应过这个问题,并提交了补丁,我们只需要
将这些补丁下载下来,自己打上补丁,然后编译即可。
source/zlib/gzlib.c:252:9: error: implicit declaration of function 'lseek' is invalid in c99
如何去解决在mac上的这个错误?
首先得去下载这个补丁。patch-c99-fixes.diff.txt
--- Source/LibJXR/image/sys/strcodec.c.orig 2015-02-21 04:36:26.000000000 +0000
+++ Source/LibJXR/image/sys/strcodec.c 2020-12-09 12:11:53.000000000 +0000
@@ -664,24 +664,6 @@
//================================================================
// Memory access functions
//================================================================
-#if (defined(WIN32) && !defined(UNDER_CE) && (!defined(__MINGW32__) || defined(__MINGW64_TOOLCHAIN__))) || (defined(UNDER_CE) && defined(_ARM_))
-// WinCE ARM and Desktop x86
-#else
-// other platform
-#ifdef _BIG__ENDIAN_
-#define _byteswap_ulong(x) (x)
-#else // _BIG__ENDIAN_
-U32 _byteswap_ulong(U32 bits)
-{
- U32 r = (bits & 0xffu) << 24;
- r |= (bits << 8) & 0xff0000u;
- r |= ((bits >> 8) & 0xff00u);
- r |= ((bits >> 24) & 0xffu);
-
- return r;
-}
-#endif // _BIG__ENDIAN_
-#endif
U32 load4BE(void* pv)
{
--- Source/LibJXR/image/sys/strcodec.h.orig 2015-02-21 04:35:46.000000000 +0000
+++ Source/LibJXR/image/sys/strcodec.h 2020-12-09 12:28:41.000000000 +0000
@@ -28,6 +28,7 @@
#pragma once
#include <stddef.h>
+#include <stdlib.h>
#if defined(__MINGW32__)
#include <stdint.h>
#endif
@@ -117,6 +118,25 @@
#define TraceResult(a)
+#if (defined(WIN32) && !defined(UNDER_CE) && (!defined(__MINGW32__) || defined(__MINGW64_TOOLCHAIN__))) || (defined(UNDER_CE) && defined(_ARM_))
+// WinCE ARM and Desktop x86
+#else
+// other platform
+#ifdef _BIG__ENDIAN_
+#define _byteswap_ulong(x) (x)
+#else // _BIG__ENDIAN_
+inline U32 _byteswap_ulong(U32 bits)
+{
+ U32 r = (bits & 0xffu) << 24;
+ r |= (bits << 8) & 0xff0000u;
+ r |= ((bits >> 8) & 0xff00u);
+ r |= ((bits >> 24) & 0xffu);
+
+ return r;
+}
+#endif // _BIG__ENDIAN_
+#endif
+
//================================================================
typedef enum tagPacketType
{
--- Source/LibJXR/jxrgluelib/JXRGlueJxr.c.orig 2013-12-06 23:04:54.000000000 +0000
+++ Source/LibJXR/jxrgluelib/JXRGlueJxr.c 2020-12-09 12:14:48.000000000 +0000
@@ -27,6 +27,7 @@
//
//*@@@---@@@@******************************************************************
#include <limits.h>
+#include <wchar.h>
#include <JXRGlue.h>
--- Source/ZLib/gzguts.h.orig 2017-01-01 20:37:10.000000000 +0000
+++ Source/ZLib/gzguts.h 2020-12-09 12:19:37.000000000 +0000
@@ -19,6 +19,8 @@
#endif
#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
#include "zlib.h"
#ifdef STDC
# include <string.h>
然后进入FreeImage的代码文件夹里面,将这个补丁打上。然后开始编译。
patch -p0 < patch-c99-fixes.diff.txt
make -f Makefile.osx -j8
编译成功以后,会生成libfreeimage.a,然后自己加上一个FreeImage.h文件就组成了一个库。
然后就可以在XCode里面使用。 但是还没结束,在链接阶段又会出现问题。
Undefined symbols for architecture i386:
"_PerfTimerCopyStartTime", referenced from:
_ImageStrDecInit in strdec.o-i386
_ImageStrEncInit in strenc.o-i386
"_PerfTimerDelete", referenced from:
_ImageStrDecTerm in strdec.o-i386
_ImageStrEncTerm in strenc.o-i386
"_PerfTimerGetResults", referenced from:
_OutputIndivPerfTimer in strcodec.o-i386
"_PerfTimerNew", referenced from:
_ImageStrDecInit in strdec.o-i386
_ImageStrEncInit in strenc.o-i386
"_PerfTimerStart", referenced from:
_ImageStrDecInit in strdec.o-i386
_ImageStrDecDecode in strdec.o-i386
_ImageStrDecTerm in strdec.o-i386
_ImageStrEncInit in strenc.o-i386
_ImageStrEncEncode in strenc.o-i386
_ImageStrEncTerm in strenc.o-i386
_readIS in strcodec.o-i386
...
"_PerfTimerStop", referenced from:
_ImageStrDecInit in strdec.o-i386
_ImageStrDecDecode in strdec.o-i386
_ImageStrDecTerm in strdec.o-i386
_ImageStrEncInit in strenc.o-i386
_ImageStrEncEncode in strenc.o-i386
_ImageStrEncTerm in strenc.o-i386
_readIS in strcodec.o-i386
这里只需要将 Source/ LibJXR/image/sys/perfTimerANSI.c”添加到Makefile.srcs的SRCS部分
即可。不想自己折腾的同学可以直接这里下载,源码+库。帮我贡献点积分。