Merge pull request #89 from pornel/floatq

Parse quality as float
This commit is contained in:
Josh Aas
2014-08-15 14:18:36 -05:00
3 changed files with 22 additions and 15 deletions

View File

@@ -151,14 +151,20 @@ jpeg_set_linear_quality (j_compress_ptr cinfo, int scale_factor,
GLOBAL(int) GLOBAL(int)
jpeg_quality_scaling (int quality) jpeg_quality_scaling (int quality)
{
return jpeg_float_quality_scaling(quality);
}
GLOBAL(float)
jpeg_float_quality_scaling(float quality)
/* Convert a user-specified quality rating to a percentage scaling factor /* Convert a user-specified quality rating to a percentage scaling factor
* for an underlying quantization table, using our recommended scaling curve. * for an underlying quantization table, using our recommended scaling curve.
* The input 'quality' factor should be 0 (terrible) to 100 (very good). * The input 'quality' factor should be 0 (terrible) to 100 (very good).
*/ */
{ {
/* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */ /* Safety limit on quality factor. Convert 0 to 1 to avoid zero divide. */
if (quality <= 0) quality = 1; if (quality <= 0.f) quality = 1.f;
if (quality > 100) quality = 100; if (quality > 100.f) quality = 100.f;
/* The basic table is used as-is (scaling 100) for a quality of 50. /* The basic table is used as-is (scaling 100) for a quality of 50.
* Qualities 50..100 are converted to scaling percentage 200 - 2*Q; * Qualities 50..100 are converted to scaling percentage 200 - 2*Q;
@@ -166,10 +172,10 @@ jpeg_quality_scaling (int quality)
* to make all the table entries 1 (hence, minimum quantization loss). * to make all the table entries 1 (hence, minimum quantization loss).
* Qualities 1..50 are converted to scaling percentage 5000/Q. * Qualities 1..50 are converted to scaling percentage 5000/Q.
*/ */
if (quality < 50) if (quality < 50.f)
quality = 5000 / quality; quality = 5000.f / quality;
else else
quality = 200 - quality*2; quality = 200.f - quality*2.f;
return quality; return quality;
} }

View File

@@ -1062,6 +1062,7 @@ EXTERN(void) jpeg_add_quant_table JPP((j_compress_ptr cinfo, int which_tbl,
int scale_factor, int scale_factor,
boolean force_baseline)); boolean force_baseline));
EXTERN(int) jpeg_quality_scaling JPP((int quality)); EXTERN(int) jpeg_quality_scaling JPP((int quality));
EXTERN(float) jpeg_float_quality_scaling JPP((float quality));
EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_simple_progression JPP((j_compress_ptr cinfo));
EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo, EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
boolean suppress)); boolean suppress));

View File

@@ -336,31 +336,31 @@ set_quality_ratings (j_compress_ptr cinfo, char *arg, boolean force_baseline)
* If there are more q-table slots than parameters, the last value is replicated. * If there are more q-table slots than parameters, the last value is replicated.
*/ */
{ {
int val = 75; /* default value */ float val = 75.f; /* default value */
int tblno; int tblno;
char ch; char ch;
for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
if (*arg) { if (*arg) {
ch = ','; /* if not set by sscanf, will be ',' */ ch = ','; /* if not set by sscanf, will be ',' */
if (sscanf(arg, "%d%c", &val, &ch) < 1) if (sscanf(arg, "%f%c", &val, &ch) < 1)
return FALSE; return FALSE;
if (ch != ',') /* syntax check */ if (ch != ',') /* syntax check */
return FALSE; return FALSE;
/* Convert user 0-100 rating to percentage scaling */ /* Convert user 0-100 rating to percentage scaling */
#if JPEG_LIB_VERSION >= 70 #if JPEG_LIB_VERSION >= 70
cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); cinfo->q_scale_factor[tblno] = jpeg_float_quality_scaling(val);
#else #else
q_scale_factor[tblno] = jpeg_quality_scaling(val); q_scale_factor[tblno] = jpeg_float_quality_scaling(val);
#endif #endif
while (*arg && *arg++ != ',') /* advance to next segment of arg string */ while (*arg && *arg++ != ',') /* advance to next segment of arg string */
; ;
} else { } else {
/* reached end of parameter, set remaining factors to last value */ /* reached end of parameter, set remaining factors to last value */
#if JPEG_LIB_VERSION >= 70 #if JPEG_LIB_VERSION >= 70
cinfo->q_scale_factor[tblno] = jpeg_quality_scaling(val); cinfo->q_scale_factor[tblno] = jpeg_float_quality_scaling(val);
#else #else
q_scale_factor[tblno] = jpeg_quality_scaling(val); q_scale_factor[tblno] = jpeg_float_quality_scaling(val);
#endif #endif
} }
} }