--- jpeg-recompress.c.orig 2018-08-18 07:48:36 +0000 +++ jpeg-recompress.c 2018-10-27 15:24:21 +0000 @@ -23,6 +23,7 @@ #endif const char *COMMENT = "Compressed by jpeg-recompress"; +const int minDelta = 10; // Comparison method enum METHOD { @@ -354,12 +355,14 @@ long originalGraySize = 0; unsigned char *compressed = NULL; unsigned long compressedSize = 0; + unsigned long totalSize = 0; unsigned char *compressedGray; long compressedGraySize = 0; unsigned char *tmpImage; int width, height; unsigned char *metaBuf; unsigned int metaSize = 0; + unsigned int metaSizeCOM = strlen(COMMENT) + 4; FILE *file; char *inputPath = argv[optind]; char *outputPath = argv[optind + 1]; @@ -486,8 +489,9 @@ info(" at q=%i: %f\n", quality, metric); } + totalSize = compressedSize + metaSizeCOM + metaSize + minDelta; if (metric < target) { - if (compressedSize >= bufSize) { + if (totalSize >= bufSize) { free(compressed); free(compressedGray); @@ -545,11 +549,16 @@ free(buf); // Calculate and show savings, if any - int percent = (compressedSize + metaSize) * 100 / bufSize; - unsigned long saved = (bufSize > compressedSize) ? bufSize - compressedSize - metaSize : 0; + int percent = totalSize * 100 / bufSize; + unsigned long saved = (bufSize > totalSize) ? bufSize - totalSize : 0; info("New size is %i%% of original (saved %lu kb)\n", percent, saved / 1024); - if (compressedSize >= bufSize) { + // debug sizes + //info("compressedSize is %i, ", compressedSize); + //info("metaSize is %i, ", metaSize); + //info("totalSize is %i\n", totalSize); + + if ( (totalSize) >= bufSize ) { error("output file is larger than input, aborting!"); return 1; }