aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLode Vandevenne <lvandeve@users.noreply.github.com>2016-03-18 13:23:04 +0100
committerLode Vandevenne <lvandeve@users.noreply.github.com>2016-03-18 13:23:04 +0100
commit1dd5eba6ade4ca6e35060725de7c6e6a8f540273 (patch)
tree0b0d07be5f2e0a9e388b0e47de809c720f2aca58 /src
parentfdb3745fa0d2c37e73a9654a31f1fd47a32fd276 (diff)
downloadzopfli-1dd5eba6ade4ca6e35060725de7c6e6a8f540273.tar.gz
Improved zopflipng file handling and user messages
Diffstat (limited to 'src')
-rw-r--r--src/zopflipng/zopflipng_bin.cc46
1 files changed, 31 insertions, 15 deletions
diff --git a/src/zopflipng/zopflipng_bin.cc b/src/zopflipng/zopflipng_bin.cc
index ab3105e..f31f40f 100644
--- a/src/zopflipng/zopflipng_bin.cc
+++ b/src/zopflipng/zopflipng_bin.cc
@@ -47,7 +47,17 @@ void GetFileNameParts(const std::string& filename,
}
}
-// Returns the size of the file
+// Returns whether the file exists and we have read permissions.
+bool FileExists(const std::string& filename) {
+ FILE* file = fopen(filename.c_str(), "rb");
+ if (file) {
+ fclose(file);
+ return true;
+ }
+ return false;
+}
+
+// Returns the size of the file, if it exists and we have read permissions.
size_t GetFileSize(const std::string& filename) {
size_t size;
FILE* file = fopen(filename.c_str(), "rb");
@@ -284,9 +294,11 @@ int main(int argc, char *argv[]) {
lodepng::State inputstate;
std::vector<unsigned char> resultpng;
- lodepng::load_file(origpng, files[i]);
- error = ZopfliPNGOptimize(origpng, png_options,
- png_options.verbose, &resultpng);
+ error = lodepng::load_file(origpng, files[i]);
+ if (!error) {
+ error = ZopfliPNGOptimize(origpng, png_options,
+ png_options.verbose, &resultpng);
+ }
if (error) {
if (error == 1) {
@@ -305,9 +317,11 @@ int main(int argc, char *argv[]) {
if (error) {
total_errors++;
} else {
- size_t origsize = GetFileSize(files[i]);
+ size_t origsize = origpng.size();
size_t resultsize = resultpng.size();
+ PrintSize("Input size", origsize);
+ PrintResultSize("Result size", origsize, resultsize);
if (resultsize < origsize) {
printf("Result is smaller\n");
} else if (resultsize == origsize) {
@@ -317,8 +331,6 @@ int main(int argc, char *argv[]) {
? "Original was smaller\n"
: "Preserving original PNG since it was smaller\n");
}
- PrintSize("Input size", origsize);
- PrintResultSize("Result size", origsize, resultsize);
std::string out_filename = user_out_filename;
if (use_prefix) {
@@ -333,28 +345,29 @@ int main(int argc, char *argv[]) {
if (resultpng.size() < origsize) total_files_smaller++;
else if (resultpng.size() == origsize) total_files_equal++;
- if (!always_zopflify && resultpng.size() > origsize) {
- // Set output file to input since input was smaller.
+ if (!always_zopflify && resultpng.size() >= origsize) {
+ // Set output file to input since zopfli didn't improve it.
resultpng = origpng;
}
+ bool already_exists = FileExists(out_filename);
size_t origoutfilesize = GetFileSize(out_filename);
- bool already_exists = true;
- if (origoutfilesize == 0) already_exists = false;
// When using a prefix, and the output file already exist, assume it's
// from a previous run. If that file is smaller, it may represent a
// previous run with different parameters that gave a smaller PNG image.
+ // This also applies when not using prefix but same input as output file.
// In that case, do not overwrite it. This behaviour can be removed by
// adding the always_zopflify flag.
bool keep_earlier_output_file = already_exists &&
- resultpng.size() >= origoutfilesize && !always_zopflify && use_prefix;
+ resultpng.size() >= origoutfilesize && !always_zopflify &&
+ (use_prefix || !different_output_name);
if (keep_earlier_output_file) {
// An output file from a previous run is kept, add that files' size
// to the output size statistics.
total_out_size += origoutfilesize;
- if (different_output_name) {
+ if (use_prefix) {
printf(resultpng.size() == origoutfilesize
? "File not written because a previous run was as good.\n"
: "File not written because a previous run was better.\n");
@@ -373,8 +386,11 @@ int main(int argc, char *argv[]) {
}
if (confirmed) {
if (!dryrun) {
- lodepng::save_file(resultpng, out_filename);
- total_files_saved++;
+ if (lodepng::save_file(resultpng, out_filename) != 0) {
+ printf("Failed to write to file %s\n", out_filename.c_str());
+ } else {
+ total_files_saved++;
+ }
}
total_out_size += resultpng.size();
} else {