Skip to content

Commit

Permalink
clamp out-of-range quality
Browse files Browse the repository at this point in the history
see also previous reverting commit

Allowed range for JPEG seem to be 0-100, however 0 result in zero
division, so clamp the values to a valid range as libjpeg does.
  • Loading branch information
MartinPulec committed May 31, 2023
1 parent c87c3ab commit eb91612
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/gpujpeg_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ gpujpeg_table_quantization_set_default(uint8_t* table_raw, enum gpujpeg_componen
void
gpujpeg_table_quantization_apply_quality(uint8_t* table_raw, int quality)
{
if (quality <= 0) quality = 1;
if (quality > 100) quality = 100;
int s = (quality < 50) ? (5000 / quality) : (200 - (2 * quality));
for ( int i = 0; i < 64; i++ ) {
int value = (s * (int)table_raw[i] + 50) / 100;
Expand Down
4 changes: 3 additions & 1 deletion src/gpujpeg_util.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file
* Copyright (c) 2011-2019, CESNET z.s.p.o
* Copyright (c) 2011-2023, CESNET z.s.p.o
* Copyright (c) 2011, Silicon Genome, LLC.
*
* All rights reserved.
Expand Down Expand Up @@ -41,6 +41,8 @@
#ifdef __cplusplus
extern "C" {
#endif

#define GPUJPEG_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))

// CUDA check error
#define gpujpeg_cuda_check_error(msg, action) \
Expand Down
2 changes: 1 addition & 1 deletion src/gpujpeg_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ gpujpeg_writer_write_com_library(struct gpujpeg_encoder* encoder)
{
char creator[] = "CREATOR: GPUJPEG, quality = \0\0\0";
snprintf(creator + strlen(creator), sizeof creator - strlen(creator), "%d",
encoder->coder.param.quality);
GPUJPEG_CLAMP(encoder->coder.param.quality, 1, 100));
gpujpeg_writer_write_com(encoder, creator);
}

Expand Down

0 comments on commit eb91612

Please sign in to comment.