Using Rust libcaesium

This commit is contained in:
Matteo Paonessa 2021-10-23 21:49:43 +02:00
parent 8be97e4201
commit 91ac6ff742
10 changed files with 111 additions and 94 deletions

View File

@ -23,6 +23,6 @@ before_install:
before_script: before_script:
- mkdir build - mkdir build
- cd build - cd build
- cmake .. - cmake .. -DLIBCAESIUM_PATH=libcaesium/target/release
script: make script: make

View File

@ -11,16 +11,13 @@ You will also need cmake if you want to compile it from source.
For Windows you just need to download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases). For Windows you just need to download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases).
Unzip the package using your favorite software, open it and run `caesiumclt.exe` from the Command Prompt or PowerShell. Unzip the package using your favorite software, open it and run `caesiumclt.exe` from the Command Prompt or PowerShell.
### MacOS X ### MacOS X and Linux
**NOTE:** Homebrew installation will come soon. Use Linux instructions below for now.
### Linux
Download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases) or clone from git. Download the latest release package from [here](https://github.com/Lymphatus/caesium-clt/releases) or clone from git.
Then run: Then run:
$ cd caesium-clt $ cd caesium-clt
$ mkdir build && cd build $ mkdir build && cd build
$ cmake .. $ cmake .. -DLIBCAESIUM_PATH=/your/libcaesium/path/dir
$ make $ make
$ sudo make install $ sudo make install

View File

@ -3,10 +3,4 @@
#libcaesium #libcaesium
git clone https://github.com/Lymphatus/libcaesium.git git clone https://github.com/Lymphatus/libcaesium.git
cd libcaesium || exit cd libcaesium || exit
sudo chmod +x install.sh cargo build --release
./install.sh
mkdir build
cd build || exit
cmake ..
make
sudo make install

View File

@ -7,7 +7,6 @@ FILE(GLOB CVendorHeaders vendor/*.h)
add_executable(caesiumclt ${CVendorSources} ${CVendorHeaders} ${CSources} ${CHeaders}) add_executable(caesiumclt ${CVendorSources} ${CVendorHeaders} ${CSources} ${CHeaders})
target_link_libraries(caesiumclt LINK_PUBLIC caesium) target_link_libraries(caesiumclt LINK_PUBLIC caesium m)
target_link_libraries(caesiumclt LINK_PUBLIC m)
install(TARGETS caesiumclt DESTINATION bin) install(TARGETS caesiumclt DESTINATION bin)

View File

@ -16,7 +16,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <caesium.h>
#include <stdlib.h> #include <stdlib.h>
#include "error.h" #include "error.h"

View File

@ -18,6 +18,12 @@
#ifndef CAESIUMCLT_ERROR_H #ifndef CAESIUMCLT_ERROR_H
#define CAESIUMCLT_ERROR_H #define CAESIUMCLT_ERROR_H
typedef enum error_level
{
ERROR = 0,
WARNING = 1
} error_level;
void display_error(error_level level, int code); void display_error(error_level level, int code);
const char *get_error_message(int code); const char *get_error_message(int code);

View File

@ -31,7 +31,7 @@
#include "error.h" #include "error.h"
#include "shared.h" #include "shared.h"
cclt_options parse_arguments(char **argv, cs_image_pars *options) { cclt_options parse_arguments(char **argv, C_CSParameters *options) {
struct optparse opts; struct optparse opts;
//Initialize application options //Initialize application options
cclt_options parameters = {NULL, "", "", false, false, 0, 0, 0, false, all}; cclt_options parameters = {NULL, "", "", false, false, 0, 0, 0, false, all};
@ -53,16 +53,25 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
}; };
optparse_init(&opts, argv); optparse_init(&opts, argv);
int option; int option;
int quality = 0;
while ((option = optparse_long(&opts, longopts, NULL)) != -1) { while ((option = optparse_long(&opts, longopts, NULL)) != -1) {
switch (option) { switch (option) {
case 'q': case 'q':
options->jpeg.quality = (int) strtol(opts.optarg, (char **) NULL, 10); quality = (int) strtol(opts.optarg, (char **) NULL, 10);
if (options->jpeg.quality < 0 || options->jpeg.quality > 100) { if (quality < 0 || quality > 100) {
display_error(ERROR, 1); display_error(ERROR, 1);
} }
if (quality == 0) {
options->optimize = true;
} else {
options->jpeg_quality = quality;
options->png_level = parse_png_quality(quality);
options->webp_quality = quality;
options->gif_quality = quality;
}
break; break;
case 'e': case 'e':
options->jpeg.exif_copy = true; options->keep_metadata = true;
break; break;
case 'o': case 'o':
if (opts.optarg[0] == '~') { if (opts.optarg[0] == '~') {
@ -101,9 +110,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
#endif #endif
} }
break; break;
case 's':
options->jpeg.scale_factor = options->png.scale_factor = parse_scale_factor(opts.optarg);
break;
case 'R': case 'R':
parameters.recursive = true; parameters.recursive = true;
break; break;
@ -221,11 +227,11 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) {
return parameters; return parameters;
} }
int start_compression(cclt_options *options, cs_image_pars *parameters) { int start_compression(cclt_options *options, struct C_CSParameters parameters) {
int compressed_files = 0; int compressed_files = 0;
off_t input_file_size = 0; off_t input_file_size;
off_t output_file_size = 0; off_t output_file_size;
//Create the output folder if does not exists //Create the output folder if it does not exist
if (mkpath(options->output_folder) == -1) { if (mkpath(options->output_folder) == -1) {
display_error(ERROR, 5); display_error(ERROR, 5);
} }
@ -235,8 +241,7 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
char *output_full_path = NULL; char *output_full_path = NULL;
char *original_output_full_path = NULL; char *original_output_full_path = NULL;
bool overwriting = false; bool overwriting = false;
off_t file_size = 0; off_t file_size;
int compression_error_code = 0;
//If we don't need to keep the structure, we put all the files in one folder by just the filename //If we don't need to keep the structure, we put all the files in one folder by just the filename
if (!options->keep_structure) { if (!options->keep_structure) {
output_full_path = malloc((strlen(filename) + strlen(options->output_folder) + 1) * sizeof(char)); output_full_path = malloc((strlen(filename) + strlen(options->output_folder) + 1) * sizeof(char));
@ -307,7 +312,7 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
fflush(stdout); fflush(stdout);
//Prevent compression if running in dry mode //Prevent compression if running in dry mode
if (!options->dry_run) { if (!options->dry_run) {
if (cs_compress(options->input_files[i], output_full_path, parameters, &compression_error_code)) { if (c_compress(options->input_files[i], output_full_path, parameters)) {
compressed_files++; compressed_files++;
output_file_size = get_file_size(output_full_path); output_file_size = get_file_size(output_full_path);
@ -324,10 +329,9 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) {
print_to_console(stdout, verbose, "\r%s -> %s [%.2f%%]\n", print_to_console(stdout, verbose, "\r%s -> %s [%.2f%%]\n",
human_input_size, human_input_size,
human_output_size, human_output_size,
((float) output_file_size - input_file_size) * 100 / input_file_size); ((float) output_file_size - (float) input_file_size) * 100 / (float) input_file_size);
} else { } else {
print_to_console(stdout, verbose, "\n"); print_to_console(stderr, verbose, "\nCompression failed\n");
print_to_console(stderr, verbose, "Compression failed with error %d\n", compression_error_code);
options->input_total_size -= get_file_size(options->input_files[i]); options->input_total_size -= get_file_size(options->input_files[i]);
} }
} }

View File

@ -18,12 +18,12 @@
#ifndef CAESIUM_CLT_HELPER_H #ifndef CAESIUM_CLT_HELPER_H
#define CAESIUM_CLT_HELPER_H #define CAESIUM_CLT_HELPER_H
#include <caesium.h>
#ifdef _WIN32 #ifdef _WIN32
#define MAX_PATH_SIZE _MAX_PATH #define MAX_PATH_SIZE _MAX_PATH
#else #else
#include <limits.h> #include <limits.h>
#include <stdbool.h>
#define MAX_PATH_SIZE PATH_MAX #define MAX_PATH_SIZE PATH_MAX
#endif #endif
@ -34,6 +34,18 @@ typedef enum overwrite_policy {
all all
} overwrite_policy; } overwrite_policy;
typedef struct C_CSParameters {
bool keep_metadata;
unsigned int jpeg_quality;
unsigned int png_level;
bool png_force_zopfli;
unsigned int gif_quality;
unsigned int webp_quality;
bool optimize;
} C_CSParameters;
extern bool c_compress(const char *i, const char *o, struct C_CSParameters params);
typedef struct cclt_options typedef struct cclt_options
{ {
char **input_files; char **input_files;
@ -50,8 +62,8 @@ typedef struct cclt_options
cclt_options parse_arguments(char *argv[], cs_image_pars *options); cclt_options parse_arguments(char *argv[], C_CSParameters *options);
int start_compression(cclt_options *options, cs_image_pars *parameters); int start_compression(cclt_options *options, struct C_CSParameters parameters);
#endif //CAESIUM_CLT_HELPER_H #endif //CAESIUM_CLT_HELPER_H

View File

@ -18,7 +18,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <caesium.h>
#include "utils.h" #include "utils.h"
#include "shared.h" #include "shared.h"
@ -31,18 +30,25 @@ int main(int argc, char *argv[])
} }
long compression_time = 0; long compression_time = 0;
cs_image_pars compress_options;
cclt_options options; cclt_options options;
//Initialize the default parameters //Initialize the default parameters
compress_options = initialize_parameters(); C_CSParameters compress_options = {
false,
80,
3,
false,
20,
60,
false
};
//Set them according to command line parameters //Set them according to command line parameters
options = parse_arguments(argv, &compress_options); options = parse_arguments(argv, &compress_options);
//Start a timer before calling the compression //Start a timer before calling the compression
clock_t start = clock(), diff; clock_t start = clock(), diff;
start_compression(&options, &compress_options); start_compression(&options, compress_options);
//Cleanup the memory allocated objects //Cleanup the memory allocated objects
free(options.input_files); free(options.input_files);

View File

@ -38,12 +38,12 @@ bool file_exists(const char* file_path);
int strndx(const char* string, char search); int strndx(const char* string, char search);
double parse_scale_factor(const char* factor_string);
overwrite_policy parse_overwrite_policy(const char* overwrite_string); overwrite_policy parse_overwrite_policy(const char* overwrite_string);
void print_to_console(FILE* buffer, int verbose, const char* format, ...); void print_to_console(FILE* buffer, int verbose, const char* format, ...);
int parse_png_quality(int quality);
#ifdef _WIN32 #ifdef _WIN32
char *str_replace(char *orig, char *rep, char *with); char *str_replace(char *orig, char *rep, char *with);
char *strsep(char **stringp, const char *delim); char *strsep(char **stringp, const char *delim);