Add a C header file and update C usage guide. (#24)

* Add a C header file.

* Update the C usage guide.

* Move C header file from header directory to include directory.

* Update the C usage guide.
This commit is contained in:
George 2024-11-13 14:53:29 +08:00 committed by GitHub
parent 14519bb253
commit 1b1711ac70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 110 additions and 1 deletions

View File

@ -39,7 +39,9 @@ The result will be a dynamic library usable by external applications through its
## Usage in C
Libcaesium exposes two C functions, auto-detecting the input file type:
*You can find the C header file in the include folder in the project root directory.*
Libcaesium exposes there C functions, auto-detecting the input file type:
### Based on quality values
@ -65,6 +67,7 @@ A `CCSResult` struct
#[repr(C)]
pub struct CCSResult {
pub success: bool,
pub code: u32,
pub error_message: *const c_char,
}
```
@ -80,6 +83,7 @@ pub unsafe extern "C" fn c_compress_to_size(
output_path: *const c_char,
params: CCSParameters,
max_output_size: usize,
return_smallest: bool,
) -> CCSResult
```
@ -89,6 +93,7 @@ pub unsafe extern "C" fn c_compress_to_size(
- `output_path` - output file path (full filename)
- `parameters` - options struct, containing compression parameters (see below)
- `max_output_size` - the maximum output size, in bytes
- `return_smallest` - whether to return the smallest
#### Return
@ -98,6 +103,41 @@ A `CCSResult` struct
#[repr(C)]
pub struct CCSResult {
pub success: bool,
pub code: u32,
pub error_message: *const c_char,
}
```
If `success` is `true` the compression process ended successfully and `error_message` will be empty.
On failure, the `error_message` will be filled with a string containing a brief explanation of the error.
### Based on convert output
```Rust
pub unsafe extern "C" fn c_convert(
input_path: *const c_char,
output_path: *const c_char,
format: SupportedFileTypes,
params: CCSParameters,
) -> CCSResult
```
#### Parameters
- `input_path` - input file path (full filename)
- `output_path` - output file path (full filename)
- `format` - target image format (see below)
- `parameters` - options struct, containing compression parameters (see below)
#### Return
A `CCSResult` struct
```Rust
#[repr(C)]
pub struct CCSResult {
pub success: bool,
pub code: u32,
pub error_message: *const c_char,
}
```
@ -137,6 +177,21 @@ ignored and `0` will be used.
Valid values for `tiff_deflate_level` are `[1 (Fast), 6 (Balanced), 9 (Best)]`. Any other value will be ignored and `Best`
will be used.
### Supported file types
```rust
#[repr(C)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum SupportedFileTypes {
Jpeg,
Png,
Gif,
WebP,
Tiff,
Unkn,
}
```
## Compression vs Optimization
JPEG is a lossy format: that means you will always lose some information after each compression. So, compressing a file

54
include/libcaesium.h Normal file
View File

@ -0,0 +1,54 @@
#ifndef LIB_CAESIUM_H
#define LIB_CAESIUM_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
typedef enum SupportedFileTypes {
Jpeg = 0,
Png = 1,
Gif = 2,
WebP = 3,
Tiff = 4,
Unkn = 5
} SupportedFileTypes;
typedef struct CCSResult {
bool success;
uint32_t code;
char *error_message;
} CCSResult;
typedef struct CCSParameters {
bool keep_matedata;
uint32_t jpeg_quality;
uint32_t jpeg_chroma_subsampling; // support 444, 422, 420, 411
bool jpeg_progressive;
uint32_t png_quality;
uint32_t png_optimization_level;
bool png_force_zopfli;
uint32_t gif_quality;
uint32_t webp_quality;
uint32_t tiff_compression; // support 1:Lzw 2:Deflate 3:Packbits Other Int:Uncompressed
uint32_t tiff_deflate_level; // support 1:Fast 6:Balanced Other Int:Best
bool optimize;
uint32_t width;
uint32_t height;
} CCSParameters;
CCSResult c_compress(const char* input_path, const char* output_path, CCSParameters* params);
CCSResult c_compress_to_size(const char* input_path, const char* output_path, CCSParameters* params, uint64_t max_output_size, bool return_smallest);
CCSResult c_convert(const char* input_path, const char* output_path, SupportedFileTypes format, CCSParameters* params);
#ifdef __cplusplus
}
#endif
#endif