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
|
|
|
|
2016-12-23 09:10:21 +01: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;
|
|
|
|
|
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) {
|
2016-12-23 09:10:21 +01:00
|
|
|
cs_jpeg_compress(output_path, cs_jpeg_decompress(input_path, &options->jpeg), &options->jpeg);
|
2016-11-16 09:41:46 +01:00
|
|
|
//The output is now the new input for optimization
|
2016-12-23 09:10:21 +01:00
|
|
|
result = cs_jpeg_optimize(output_path, output_path, options->jpeg.exif_copy, input_path);
|
|
|
|
} else {
|
|
|
|
result = cs_jpeg_optimize(input_path, output_path, options->jpeg.exif_copy, 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;
|
|
|
|
options->jpeg.exif_copy = false;
|
|
|
|
options->jpeg.dct_method = 2048;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_png_parameters(cs_image_pars *par)
|
|
|
|
{
|
|
|
|
par->png.iterations = 10;
|
|
|
|
par->png.iterations_large = 5;
|
|
|
|
par->png.block_split_strategy = 4;
|
|
|
|
par->png.lossy_8 = true;
|
|
|
|
par->png.transparent = true;
|
|
|
|
par->png.auto_filter_strategy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cs_image_pars initialize_parameters()
|
|
|
|
{
|
|
|
|
cs_image_pars options;
|
|
|
|
|
|
|
|
initialize_jpeg_parameters(&options);
|
|
|
|
initialize_png_parameters(&options);
|
|
|
|
|
|
|
|
return options;
|
2016-11-04 11:00:04 +01:00
|
|
|
}
|