Skip to content

Commit

Permalink
Cleanup and better safety checks, 4
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaaa123456789 committed Jan 20, 2022
1 parent 2c01168 commit 2bdb87e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/gifwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void generate_GIF_data (struct context * context) {
void generate_GIF_data_with_palette (struct context * context, unsigned char * header) {
uint_fast16_t colors = context -> source -> max_palette_index + 1;
uint32_t * palette = ctxcalloc(context, 256 * sizeof *palette);
plum_convert_colors(palette, context -> source -> palette, context -> source -> max_palette_index + 1, PLUM_COLOR_32, context -> source -> color_format);
plum_convert_colors(palette, context -> source -> palette, colors, PLUM_COLOR_32, context -> source -> color_format);
int transparent = -1;
uint8_t * mapping = NULL;
uint_fast32_t p;
Expand Down
8 changes: 2 additions & 6 deletions src/jpegcomponents.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ void JPEG_transfer_RGB (uint64_t * restrict output, size_t count, unsigned limit
}

void JPEG_transfer_BGR (uint64_t * restrict output, size_t count, unsigned limit, const double ** input) {
double factor = 65535.0 / limit;
const double * red = input[2];
const double * green = input[1];
const double * blue = *input;
while (count --) *(output ++) = color_from_floats(*(red ++) * factor, *(green ++) * factor, *(blue ++) * factor, 0);
JPEG_transfer_RGB(output, count, limit, (const double * []) {input[2], input[1], *input});
}

void JPEG_transfer_ABGR (uint64_t * restrict output, size_t count, unsigned limit, const double ** input) {
Expand Down Expand Up @@ -262,7 +258,7 @@ void JPEG_transfer_CMYK (uint64_t * restrict output, size_t count, unsigned limi
const double * yellow = input[2];
const double * black = input[3];
while (count --) {
double scale = factor * *(black ++);
double scale = *(black ++) * factor;
*(output ++) = color_from_floats(*(cyan ++) * scale, *(magenta ++) * scale, *(yellow ++) * scale, 0);
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/jpegcompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ struct JPEG_encoded_value * generate_JPEG_luminance_data_stream (struct context
struct JPEG_encoded_value * result = ctxmalloc(context, sizeof *result * allocated);
double predicted = 0.0;
for (unit = 0; unit < units; unit ++) {
if ((allocated - *count) < 64) result = ctxrealloc(context, result, sizeof *result * (allocated += 3 * (units - unit) + 64));
if ((allocated - *count) < 64) {
size_t newsize = allocated + 3 * (units - unit) + 64;
if (newsize < allocated) throw(context, PLUM_ERR_IMAGE_TOO_LARGE);
result = ctxrealloc(context, result, sizeof *result * (allocated = newsize));
}
predicted = generate_JPEG_data_unit(result, count, data[unit], quantization, predicted);
}
return ctxrealloc(context, result, sizeof *result * *count);
return ctxrealloc(context, result, *count * sizeof *result);
}

struct JPEG_encoded_value * generate_JPEG_chrominance_data_stream (struct context * context, double (* restrict blue)[64], double (* restrict red)[64],
Expand All @@ -20,11 +24,15 @@ struct JPEG_encoded_value * generate_JPEG_chrominance_data_stream (struct contex
struct JPEG_encoded_value * result = ctxmalloc(context, sizeof *result * allocated);
double predicted_blue = 0.0, predicted_red = 0.0;
for (unit = 0; unit < units; unit ++) {
if ((allocated - *count) < 128) result = ctxrealloc(context, result, sizeof *result * (allocated += 6 * (units - unit) + 128));
if ((allocated - *count) < 128) {
size_t newsize = allocated + 6 * (units - unit) + 128;
if (newsize < allocated) throw(context, PLUM_ERR_IMAGE_TOO_LARGE);
result = ctxrealloc(context, result, sizeof *result * (allocated = newsize));
}
predicted_blue = generate_JPEG_data_unit(result, count, blue[unit], quantization, predicted_blue);
predicted_red = generate_JPEG_data_unit(result, count, red[unit], quantization, predicted_red);
}
return ctxrealloc(context, result, sizeof *result * *count);
return ctxrealloc(context, result, *count * sizeof *result);
}

double generate_JPEG_data_unit (struct JPEG_encoded_value * data, size_t * restrict count, const double unit[restrict static 64],
Expand Down
2 changes: 1 addition & 1 deletion src/jpegdct.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ double apply_JPEG_DCT (int16_t output[restrict static 64], const double input[re
4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 4, 5, 6, 7, 7, 6, 5, 6, 7, 7};
static const unsigned char cols[] = {0, 1, 0, 0, 1, 2, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4,
3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 5, 6, 7, 7, 6, 7};
// zero flushing threshold: for later coefficients, round some values slightly larger than 0.5 to 0 instead of +/- 1 for better compression
// zero-flushing threshold: for later coefficients, round some values slightly larger than 0.5 to 0 instead of +/- 1 for better compression
static const double zeroflush[] = {
0x0.80p+0, 0x0.80p+0, 0x0.80p+0, 0x0.80p+0, 0x0.81p+0, 0x0.80p+0, 0x0.84p+0, 0x0.85p+0, 0x0.85p+0, 0x0.84p+0,
0x0.88p+0, 0x0.89p+0, 0x0.8ap+0, 0x0.89p+0, 0x0.88p+0, 0x0.8cp+0, 0x0.8dp+0, 0x0.8ep+0, 0x0.8ep+0, 0x0.8dp+0,
Expand Down

0 comments on commit 2bdb87e

Please sign in to comment.