From a0fe4ace105379da0f12988f35fefad6496d256f Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Mon, 11 Jan 2016 12:18:44 +0100 Subject: [PATCH] Dropped scaling support --- src/compresshelper.c | 14 +++++++------- src/jpeg.c | 16 ++++++++-------- src/main.c | 27 ++++++++++----------------- src/png.c | 14 +++++++------- src/utils.c | 17 +++++++++-------- src/utils.h | 1 - 6 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/compresshelper.c b/src/compresshelper.c index 584158a..1c6ff64 100644 --- a/src/compresshelper.c +++ b/src/compresshelper.c @@ -15,11 +15,10 @@ cclt_compress_parameters initialize_compression_parameters() { cclt_compress_parameters par; - + par.quality = 0; par.width = 0; par.height = 0; - par.scaling_factor = 100; par.color_space = TJCS_RGB; par.dct_method = TJFLAG_FASTDCT; par.output_folder = NULL; @@ -34,7 +33,7 @@ cclt_compress_parameters initialize_compression_parameters() { } cclt_compress_parameters parse_arguments(int argc, char* argv[]) { - + //Initialize default params cclt_compress_parameters parameters = initialize_compression_parameters(); int c; @@ -75,9 +74,6 @@ cclt_compress_parameters parse_arguments(int argc, char* argv[]) { case 'o': parameters.output_folder = optarg; break; - case 's': - parameters.scaling_factor = string_to_int(optarg); - break; case 'h': print_help(); break; @@ -119,10 +115,14 @@ int cclt_compress_routine(char* input, char* output, cclt_compress_parameters* p } else if (type == JPEG && pars->lossless != 0) { cclt_jpeg_optimize(input, output, pars->exif_copy, input); } else if (type == PNG) { + //Give a message to the user if he set a quality for PNGs + if (pars->quality != 0) { + printf("PNG file, ignoring quality parameter.\n"); + } cclt_png_optimize(input, output); } else { printf("Unknown file type.\n"); return -1; } return 0; -} \ No newline at end of file +} diff --git a/src/jpeg.c b/src/jpeg.c index 3fe7bbd..742d051 100644 --- a/src/jpeg.c +++ b/src/jpeg.c @@ -22,7 +22,7 @@ struct jpeg_decompress_struct cclt_get_markers(char* input) { //Open the input file fp = fopen(input, "r"); - + //Check for errors //TODO Use UNIX error messages if (fp == NULL) { @@ -50,7 +50,7 @@ int cclt_jpeg_optimize(char* input_file, char* output_file, int exif_flag, char* // Happened with a (bugged) server connection //File pointer for both input and output FILE* fp; - + //Those will hold the input/output structs struct jpeg_decompress_struct srcinfo; struct jpeg_compress_struct dstinfo; @@ -67,18 +67,18 @@ int cclt_jpeg_optimize(char* input_file, char* output_file, int exif_flag, char* jpeg_create_decompress(&srcinfo); dstinfo.err = jpeg_std_error(&jdsterr); jpeg_create_compress(&dstinfo); - + //Open the input file fp = fopen(input_file, "r"); - + //Check for errors //TODO Use UNIX error messages if (fp == NULL) { printf("INPUT: Failed to open file \"%s\"\n", input_file); return -1; } - + //Create the IO istance for the input file jpeg_stdio_src(&srcinfo, fp); @@ -211,8 +211,8 @@ unsigned char* cclt_jpeg_decompress(char* fileName, cclt_compress_parameters* pa tjDecompressHandle = tjInitDecompress(); res = tjDecompressHeader3(tjDecompressHandle, sourceJpegBuffer, sourceJpegBufferSize, &fileWidth, &fileHeight, &jpegSubsamp, &colorSpace); - pars->width = ceil(fileWidth * ((double) pars->scaling_factor / 100)); - pars->height = ceil(fileHeight * ((double) pars->scaling_factor / 100)); + pars->width = fileWidth; + pars->height = fileHeight; pars->subsample = jpegSubsamp; pars->color_space = colorSpace; @@ -234,4 +234,4 @@ unsigned char* cclt_jpeg_decompress(char* fileName, cclt_compress_parameters* pa tjDestroy(tjDecompressHandle); return temp; -} \ No newline at end of file +} diff --git a/src/main.c b/src/main.c index e77e4c6..575d61d 100755 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,13 @@ #include #include -#include #include #include #include -#include +#include #include #include #include +#include #include "jpeg.h" #include "compresshelper.h" @@ -19,7 +19,6 @@ -o output folder v -v version v -l lossless v --s scale v -h help v -R recursive v -S keep folder structure @@ -133,7 +132,7 @@ int main (int argc, char *argv[]) { } } else { //One of them is set - //If -q is set check it is within the 1-100 range + //If -q is set check it is within the 1-100 range if (!(pars.quality >= 1 && pars.quality <= 100) && pars.lossless == 0) { fprintf(stderr, "Quality must be within a [1-100] range. Aborting.\n"); exit(-3); @@ -146,12 +145,6 @@ int main (int argc, char *argv[]) { exit(-9); } - //Check if there's a valid scaling factor - if (pars.scaling_factor <= 0) { - fprintf(stderr, "Scaling factor must be > 0. Aborting.\n"); - exit(-6); - } - //Check if the output folder exists, otherwise create it if (pars.output_folder == NULL) { fprintf(stderr, "No -o option pointing to the destination folder. Aborting.\n"); @@ -164,19 +157,19 @@ int main (int argc, char *argv[]) { } } - if ((pars.lossless == 1) && (pars.scaling_factor != 100)) { - fprintf(stderr, "Lossless scaling is not supported. Use -q instead. Aborting.\n"); - exit(-13); - } - + //Start a timer + clock_t start = clock(), diff; //We need the file list right here cclt_start(pars.input_files, pars.input_files_count, pars.output_folder, &pars, &i_t_size, &o_t_size); + diff = clock() - start; - fprintf(stdout, "-------------------------------\nCompression completed.\n%s -> %s [%.2f%% | %s]\n", + fprintf(stdout, "-------------------------------\nCompression completed in %lum%lus\n%s -> %s [%.2f%% | %s]\n", + diff / CLOCKS_PER_SEC / 60, + diff / CLOCKS_PER_SEC % 60, get_human_size((long) i_t_size), get_human_size((long) o_t_size), ((float) o_t_size - i_t_size) * 100 / i_t_size, get_human_size(((long) o_t_size - i_t_size))); - + exit(0); } diff --git a/src/png.c b/src/png.c index 6a6e8a4..648eb62 100644 --- a/src/png.c +++ b/src/png.c @@ -15,14 +15,14 @@ void cclt_png_optimize(char* input, char* output) { unsigned char* orig_buffer; size_t orig_buffer_size; - unsigned char* resultpng; + unsigned char* resultpng; size_t resultpng_size; - png_options.num_iterations = 10; - png_options.num_iterations_large = 15; - png_options.block_split_strategy = 3; + png_options.num_iterations = 15; + png_options.num_iterations_large = 5; + png_options.block_split_strategy = 4; - png_options.lossy_8bit = 1; + png_options.lossy_8bit = 0; png_options.lossy_transparent = 1; png_options.auto_filter_strategy = 1; @@ -33,7 +33,7 @@ void cclt_png_optimize(char* input, char* output) { } if (CZopfliPNGOptimize(orig_buffer, - orig_buffer_size, + orig_buffer_size, &png_options, 0, &resultpng, @@ -49,4 +49,4 @@ void cclt_png_optimize(char* input, char* output) { free(orig_buffer); free(resultpng); -} \ No newline at end of file +} diff --git a/src/utils.c b/src/utils.c index 30e7501..815d771 100644 --- a/src/utils.c +++ b/src/utils.c @@ -25,7 +25,7 @@ int string_to_int(char* in_string) { errno = 0; //Error checking value = strtol(in_string, &endptr, 0); //Convert the string - + //Check errors if ((errno == ERANGE) || (errno != 0 && value == 0)) { perror("strtol"); @@ -36,7 +36,7 @@ int string_to_int(char* in_string) { fprintf(stderr, "Parse error: No digits were found for -q option. Aborting.\n"); exit(-7); } - + return value; } @@ -107,14 +107,14 @@ char** scan_folder(char* basedir, int* n, int recur) { dir = opendir(ptr); - - if (dir != NULL) { + + if (dir != NULL) { while ((ent = readdir(dir)) != NULL) { // Do not allow "." or ".." if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } - + //TODO allocate for this entry //Basedir + filename + separator entpath = realloc(entpath, (strlen(ptr) + strlen(ent->d_name) + 1) * sizeof(char)); @@ -126,8 +126,8 @@ char** scan_folder(char* basedir, int* n, int recur) { //Gets stats stat(entpath, &s); - - if (S_ISDIR(s.st_mode)) { + + if (S_ISDIR(s.st_mode)) { // Directory, walk it if recursive is set if (recur != 0) { fileList = scan_folder(entpath, n, recur); @@ -182,6 +182,7 @@ enum image_type detect_image_type(char* path) { free(type_buffer); return PNG; } else { + free(type_buffer); return UNKN; } } @@ -210,4 +211,4 @@ char* get_human_size(long size) { sprintf(final, "%.2f %s", size / (pow(1024, order)), unit[(int)order]); //And return it return final; -} \ No newline at end of file +} diff --git a/src/utils.h b/src/utils.h index e1520d6..2da5312 100644 --- a/src/utils.h +++ b/src/utils.h @@ -12,7 +12,6 @@ typedef struct cclt_compress_parameters { int quality; int width; int height; - int scaling_factor; char* output_folder; int color_space; int dct_method;