diff options
author | Chris Blume <cblume@chromium.org> | 2019-03-01 01:09:50 -0800 |
---|---|---|
committer | Chris Blume <cblume@chromium.org> | 2019-03-01 01:09:50 -0800 |
commit | cca8c4dec783a048da6933c86028556622d7c355 (patch) | |
tree | 8a7ff526cd8cbe3bf1bfaa4ec1c29fe3268ed51b /wrppm.c | |
parent | 61a2bbaa9aec89cb2c882d87ace6aba9aee49bb9 (diff) |
Update libjpeg-turbo to v2.0.1
In order to apply some performance updates from ARM, we need to update
libjpeg-turbo. These performance updates have yielded a 50% speedup on
some devices.
This CL updates our copy of libjpeg-turbo to v2.0.1 and re-applies our
local patches. This patch also deletes some extra files which were not
being used locally.
Update our local patch that was applied to fix http://crbug.com/398235
(https://codereview.appspot.com/229430043/). The original patch
incorrectly removed "& 0xFF" which limited an array index to within
that array's bounds (effectively reverting
https://github.com/libjpeg-turbo/libjpeg-turbo/commit/fa1d18385d904d530b4aec83ab7757a33397de6e).
Restore the mask, making the array access safe and fixing a graphical
glitch which would otherwise be introduced by this change.
Bug:922430
Change-Id: I3860fdb424deecf7a17818ed09a640e632e71f8d
Diffstat (limited to 'wrppm.c')
-rw-r--r-- | wrppm.c | 214 |
1 files changed, 162 insertions, 52 deletions
@@ -4,8 +4,8 @@ * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1996, Thomas G. Lane. * Modified 2009 by Guido Vollbeding. - * It was modified by The libjpeg-turbo Project to include only code and - * information relevant to libjpeg-turbo. + * libjpeg-turbo Modifications: + * Copyright (C) 2017, D. R. Commander. * For conditions of distribution and use, see the accompanying README.ijg * file. * @@ -19,8 +19,8 @@ * an ordinary stdio stream. */ +#include "cmyk.h" #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ -#include "wrppm.h" #ifdef PPM_SUPPORTED @@ -36,23 +36,23 @@ */ #if BITS_IN_JSAMPLE == 8 -#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v) -#define BYTESPERSAMPLE 1 -#define PPM_MAXVAL 255 +#define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)(v) +#define BYTESPERSAMPLE 1 +#define PPM_MAXVAL 255 #else #ifdef PPM_NORAWWORD -#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8)) -#define BYTESPERSAMPLE 1 -#define PPM_MAXVAL 255 +#define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8)) +#define BYTESPERSAMPLE 1 +#define PPM_MAXVAL 255 #else /* The word-per-sample format always puts the MSB first. */ -#define PUTPPMSAMPLE(ptr,v) \ - { register int val_ = v; \ - *ptr++ = (char) ((val_ >> 8) & 0xFF); \ - *ptr++ = (char) (val_ & 0xFF); \ - } -#define BYTESPERSAMPLE 2 -#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1) +#define PUTPPMSAMPLE(ptr, v) { \ + register int val_ = v; \ + *ptr++ = (char)((val_ >> 8) & 0xFF); \ + *ptr++ = (char)(val_ & 0xFF); \ +} +#define BYTESPERSAMPLE 2 +#define PPM_MAXVAL ((1 << BITS_IN_JSAMPLE) - 1) #endif #endif @@ -63,6 +63,21 @@ */ +/* Private version of data destination object */ + +typedef struct { + struct djpeg_dest_struct pub; /* public fields */ + + /* Usually these two pointers point to the same place: */ + char *iobuffer; /* fwrite's I/O buffer */ + JSAMPROW pixrow; /* decompressor output buffer */ + size_t buffer_width; /* width of I/O buffer */ + JDIMENSION samples_per_row; /* JSAMPLEs per output row */ +} ppm_dest_struct; + +typedef ppm_dest_struct *ppm_dest_ptr; + + /* * Write some pixel data. * In this module rows_supplied will always be 1. @@ -72,12 +87,12 @@ */ METHODDEF(void) -put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, - JDIMENSION rows_supplied) +put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) { - ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; - (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); } @@ -87,20 +102,80 @@ put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, */ METHODDEF(void) -copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, - JDIMENSION rows_supplied) +copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) { - ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; register char *bufferptr; register JSAMPROW ptr; +#if BITS_IN_JSAMPLE != 8 || (!defined(HAVE_UNSIGNED_CHAR) && !defined(__CHAR_UNSIGNED__)) register JDIMENSION col; +#endif ptr = dest->pub.buffer[0]; bufferptr = dest->iobuffer; +#if BITS_IN_JSAMPLE == 8 && (defined(HAVE_UNSIGNED_CHAR) || defined(__CHAR_UNSIGNED__)) + MEMCOPY(bufferptr, ptr, dest->samples_per_row); +#else for (col = dest->samples_per_row; col > 0; col--) { PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++)); } - (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +#endif + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Convert extended RGB to RGB. + */ + +METHODDEF(void) +put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied) +{ + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; + register char *bufferptr; + register JSAMPROW ptr; + register JDIMENSION col; + register int rindex = rgb_red[cinfo->out_color_space]; + register int gindex = rgb_green[cinfo->out_color_space]; + register int bindex = rgb_blue[cinfo->out_color_space]; + register int ps = rgb_pixelsize[cinfo->out_color_space]; + + ptr = dest->pub.buffer[0]; + bufferptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + PUTPPMSAMPLE(bufferptr, ptr[rindex]); + PUTPPMSAMPLE(bufferptr, ptr[gindex]); + PUTPPMSAMPLE(bufferptr, ptr[bindex]); + ptr += ps; + } + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); +} + + +/* + * Convert CMYK to RGB. + */ + +METHODDEF(void) +put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) +{ + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; + register char *bufferptr; + register JSAMPROW ptr; + register JDIMENSION col; + + ptr = dest->pub.buffer[0]; + bufferptr = dest->iobuffer; + for (col = cinfo->output_width; col > 0; col--) { + JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++; + cmyk_to_rgb(c, m, y, k, &r, &g, &b); + PUTPPMSAMPLE(bufferptr, r); + PUTPPMSAMPLE(bufferptr, g); + PUTPPMSAMPLE(bufferptr, b); + } + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); } @@ -110,10 +185,10 @@ copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, */ METHODDEF(void) -put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, - JDIMENSION rows_supplied) +put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) { - ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; register char *bufferptr; register int pixval; register JSAMPROW ptr; @@ -130,15 +205,15 @@ put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval])); PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval])); } - (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); } METHODDEF(void) -put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, - JDIMENSION rows_supplied) +put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, + JDIMENSION rows_supplied) { - ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; register char *bufferptr; register JSAMPROW ptr; register JSAMPROW color_map = cinfo->colormap[0]; @@ -149,7 +224,7 @@ put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, for (col = cinfo->output_width; col > 0; col--) { PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)])); } - (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); + (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); } @@ -158,23 +233,32 @@ put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, */ METHODDEF(void) -start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) { - ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; /* Emit file header */ switch (cinfo->out_color_space) { case JCS_GRAYSCALE: /* emit header for raw PGM format */ fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n", - (long) cinfo->output_width, (long) cinfo->output_height, - PPM_MAXVAL); + (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL); break; case JCS_RGB: + case JCS_EXT_RGB: + case JCS_EXT_RGBX: + case JCS_EXT_BGR: + case JCS_EXT_BGRX: + case JCS_EXT_XBGR: + case JCS_EXT_XRGB: + case JCS_EXT_RGBA: + case JCS_EXT_BGRA: + case JCS_EXT_ABGR: + case JCS_EXT_ARGB: + case JCS_CMYK: /* emit header for raw PPM format */ fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n", - (long) cinfo->output_width, (long) cinfo->output_height, - PPM_MAXVAL); + (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL); break; default: ERREXIT(cinfo, JERR_PPM_COLORSPACE); @@ -187,7 +271,7 @@ start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) */ METHODDEF(void) -finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) { /* Make sure we wrote the output file OK */ fflush(dinfo->output_file); @@ -197,41 +281,67 @@ finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) /* + * Re-calculate buffer dimensions based on output dimensions. + */ + +METHODDEF(void) +calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) +{ + ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; + + if (cinfo->out_color_space == JCS_GRAYSCALE) + dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; + else + dest->samples_per_row = cinfo->output_width * 3; + dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char)); +} + + +/* * The module selection routine for PPM format output. */ GLOBAL(djpeg_dest_ptr) -jinit_write_ppm (j_decompress_ptr cinfo) +jinit_write_ppm(j_decompress_ptr cinfo) { ppm_dest_ptr dest; /* Create module interface object, fill in method pointers */ dest = (ppm_dest_ptr) - (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, + (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, sizeof(ppm_dest_struct)); dest->pub.start_output = start_output_ppm; dest->pub.finish_output = finish_output_ppm; + dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm; /* Calculate output image dimensions so we can allocate space */ jpeg_calc_output_dimensions(cinfo); /* Create physical I/O buffer */ - dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; - dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char)); - dest->iobuffer = (char *) (*cinfo->mem->alloc_small) - ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width); + dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest); + dest->iobuffer = (char *)(*cinfo->mem->alloc_small) + ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width); if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 || - sizeof(JSAMPLE) != sizeof(char)) { + sizeof(JSAMPLE) != sizeof(char) || + (cinfo->out_color_space != JCS_EXT_RGB +#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 + && cinfo->out_color_space != JCS_RGB +#endif + )) { /* When quantizing, we need an output buffer for colormap indexes * that's separate from the physical I/O buffer. We also need a * separate buffer if pixel format translation must take place. */ dest->pub.buffer = (*cinfo->mem->alloc_sarray) - ((j_common_ptr) cinfo, JPOOL_IMAGE, - cinfo->output_width * cinfo->output_components, (JDIMENSION) 1); + ((j_common_ptr)cinfo, JPOOL_IMAGE, + cinfo->output_width * cinfo->output_components, (JDIMENSION)1); dest->pub.buffer_height = 1; - if (! cinfo->quantize_colors) + if (IsExtRGB(cinfo->out_color_space)) + dest->pub.put_pixel_rows = put_rgb; + else if (cinfo->out_color_space == JCS_CMYK) + dest->pub.put_pixel_rows = put_cmyk; + else if (!cinfo->quantize_colors) dest->pub.put_pixel_rows = copy_pixel_rows; else if (cinfo->out_color_space == JCS_GRAYSCALE) dest->pub.put_pixel_rows = put_demapped_gray; @@ -240,13 +350,13 @@ jinit_write_ppm (j_decompress_ptr cinfo) } else { /* We will fwrite() directly from decompressor output buffer. */ /* Synthesize a JSAMPARRAY pointer structure */ - dest->pixrow = (JSAMPROW) dest->iobuffer; - dest->pub.buffer = & dest->pixrow; + dest->pixrow = (JSAMPROW)dest->iobuffer; + dest->pub.buffer = &dest->pixrow; dest->pub.buffer_height = 1; dest->pub.put_pixel_rows = put_pixel_rows; } - return (djpeg_dest_ptr) dest; + return (djpeg_dest_ptr)dest; } #endif /* PPM_SUPPORTED */ |