2016-11-04 11:00:04 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "error.h"
|
|
|
|
#include "utils.h"
|
2016-11-13 14:43:54 +01:00
|
|
|
#include "png.h"
|
|
|
|
#include "jpeg.h"
|
2016-11-04 11:00:04 +01:00
|
|
|
|
2017-06-03 13:00:00 +02:00
|
|
|
bool cs_compress(const char *input_path, const char *output_path, cs_image_pars *options)
|
2016-11-04 11:00:04 +01:00
|
|
|
{
|
|
|
|
FILE *pInputFile;
|
2016-11-13 14:43:54 +01:00
|
|
|
image_type type;
|
2016-11-04 11:00:04 +01:00
|
|
|
bool result = false;
|
2017-12-29 23:09:13 +01:00
|
|
|
int compression_step_result;
|
2016-11-04 11:00:04 +01:00
|
|
|
|
2016-12-23 09:10:21 +01:00
|
|
|
if ((pInputFile = fopen(input_path, "rb")) == NULL) {
|
2017-02-21 23:15:31 +01:00
|
|
|
display_error(ERROR, 104);
|
2016-11-04 11:00:04 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = detect_image_type(pInputFile);
|
|
|
|
|
|
|
|
fclose(pInputFile);
|
|
|
|
|
|
|
|
if (type == UNKN) {
|
2017-02-21 23:15:31 +01:00
|
|
|
display_error(WARNING, 103);
|
2017-02-25 10:17:33 +01:00
|
|
|
} else if (type == CS_JPEG) {
|
2016-11-13 14:43:54 +01:00
|
|
|
if (options->jpeg.quality != 0) {
|
2017-12-29 23:09:13 +01:00
|
|
|
compression_step_result = cs_jpeg_compress(output_path, cs_jpeg_decompress(input_path, &options->jpeg), &options->jpeg);
|
|
|
|
result = (bool) compression_step_result;
|
2016-11-16 09:41:46 +01:00
|
|
|
//The output is now the new input for optimization
|
2017-06-03 12:22:23 +02:00
|
|
|
if (result) {
|
2017-12-29 23:09:13 +01:00
|
|
|
result = cs_jpeg_optimize(compression_step_result == 1 ? output_path : input_path, output_path, &options->jpeg, input_path);
|
2017-06-03 12:22:23 +02:00
|
|
|
}
|
2016-12-23 09:10:21 +01:00
|
|
|
} else {
|
2017-06-03 12:22:23 +02:00
|
|
|
result = cs_jpeg_optimize(input_path, output_path, &options->jpeg, input_path);
|
2016-11-13 14:43:54 +01:00
|
|
|
}
|
2017-02-25 10:17:33 +01:00
|
|
|
} else if (type == CS_PNG) {
|
2016-12-23 09:10:21 +01:00
|
|
|
result = cs_png_optimize(input_path, output_path, &options->png);
|
2016-11-04 11:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2017-02-21 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_jpeg_parameters(cs_image_pars *options)
|
|
|
|
{
|
|
|
|
options->jpeg.quality = 0;
|
2018-11-25 16:41:35 +01:00
|
|
|
options->jpeg.exif_copy = false;
|
2017-02-21 12:54:52 +01:00
|
|
|
options->jpeg.dct_method = 2048;
|
2017-12-29 23:09:13 +01:00
|
|
|
options->jpeg.scale_factor = 1.0;
|
2017-02-21 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
2017-12-29 23:09:13 +01:00
|
|
|
void initialize_png_parameters(cs_image_pars *options)
|
2017-02-21 12:54:52 +01:00
|
|
|
{
|
2017-12-29 23:09:13 +01:00
|
|
|
options->png.iterations = 2;
|
|
|
|
options->png.iterations_large = 1;
|
|
|
|
options->png.block_split_strategy = 0;
|
|
|
|
options->png.lossy_8 = true;
|
|
|
|
options->png.transparent = true;
|
|
|
|
options->png.auto_filter_strategy = true;
|
|
|
|
options->png.scale_factor = 1.0;
|
2017-02-21 12:54:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cs_image_pars initialize_parameters()
|
|
|
|
{
|
|
|
|
cs_image_pars options;
|
|
|
|
|
|
|
|
initialize_jpeg_parameters(&options);
|
|
|
|
initialize_png_parameters(&options);
|
|
|
|
|
|
|
|
return options;
|
2018-11-25 16:41:35 +01:00
|
|
|
}
|