gpt4 book ai didi

swift - 如何在 Swift 中使用 libjpeg 复杂宏?

转载 作者:行者123 更新时间:2023-11-28 16:01:55 24 4
gpt4 key购买 nike

当我尝试调用 libjpeg 的 jpeg_create_decompress() 函数时,我得到了

/usr/include/x86_64-linux-gnu/jconfig.h:8:34: error: invalid suffix '.0' on floating constant
#define LIBJPEG_TURBO_VERSION 1.5.0
^
.../main.swift:49:5: error: use of unresolved identifier 'jpeg_create_decompress'
jpeg_create_decompress(&info)
^~~~~~~~~~~~~~~~~~~~~~
CJPEG.jpeg_CreateDecompress:1:13: note: did you mean 'jpeg_CreateDecompress'?
public func jpeg_CreateDecompress(_ cinfo: j_decompress_ptr!, _ version: Int32, _ structsize: Int)
^
<unknown>:0: error: build had 1 command failures

现在,查看 jpeglib.h 我明白了

/* Initialization of JPEG compression objects.
* jpeg_create_compress() and jpeg_create_decompress() are the exported
* names that applications should call. These expand to calls on
* jpeg_CreateCompress and jpeg_CreateDecompress with additional information
* passed for version mismatch checking.
* NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.
*/
#define jpeg_create_compress(cinfo) \
jpeg_CreateCompress((cinfo), JPEG_LIB_VERSION, \
(size_t) sizeof(struct jpeg_compress_struct))
#define jpeg_create_decompress(cinfo) \
jpeg_CreateDecompress((cinfo), JPEG_LIB_VERSION, \
(size_t) sizeof(struct jpeg_decompress_struct))
EXTERN(void) jpeg_CreateCompress (j_compress_ptr cinfo, int version,
size_t structsize);
EXTERN(void) jpeg_CreateDecompress (j_decompress_ptr cinfo, int version,
size_t structsize);

jpeg_create_decompress 不可用,可能如 documentation 中所述:

Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

但是如果没有这个宏,我该如何在 Swift 中使用 libjpeg 呢?

最佳答案

我没有看到在 Swift 中重新定义它们的方法,例如:

class JPEGLibWrapper {

class func jpeg_create_decompress(cinfo: j_decompress_ptr) {
jpeg_CreateDecompress(cinfo, JPEG_LIB_VERSION, MemoryLayout<jpeg_decompress_struct>.size)
}
}

或者在 Objective-C 中:

@interface JPEGLibWrapper : NSObject

+ (void) jpeg_create_decompress:(j_decompress_ptr)cinfo;

@end

@implementation JPEGLibWrapper

+ (void) jpeg_create_decompress:(j_decompress_ptr)cinfo {
jpeg_create_decompress(cinfo);
}

@end

(可以使用更好的命名...)

编辑 我刚刚注意到您在 Linux 上使用 Swift,对吗?我不知道 Swift–C 桥接在 Linux 上是如何完成的,但我想可以在 C 文件中做一些类似于 Objective-C 代码的事情。

与在 Swift 中声明 Objective-C 或 C 方法/函数相比,声明 Objective-C 或 C 方法/函数的优势在于,在前者中,您实际上使用的是复杂的宏,因此避免(错误)翻译它。您只需在其周围声明一个包装器方法/函数并将其公开给 Swift。

来源:

关于swift - 如何在 Swift 中使用 libjpeg 复杂宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40899565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com