Dropped scaling support

This commit is contained in:
Matteo Paonessa 2016-01-11 12:18:44 +01:00
parent ef39a3b5f1
commit a0fe4ace10
6 changed files with 41 additions and 48 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,13 +1,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#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);
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;