Merge remote-tracking branch 'upstream/3.4' into merge-3.4

pull/17697/head
Alexander Alekhin 5 years ago
commit a711e2aa41
  1. 6
      3rdparty/libjpeg-turbo/CMakeLists.txt
  2. 3
      3rdparty/libjpeg-turbo/jconfigint.h.in
  3. 1
      3rdparty/libjpeg-turbo/src/jdatadst.c
  4. 6
      3rdparty/libjpeg-turbo/src/jversion.h
  5. 19
      3rdparty/libjpeg/README
  6. 49
      3rdparty/libjpeg/change.log
  7. 17
      3rdparty/libjpeg/jcarith.c
  8. 79
      3rdparty/libjpeg/jccolor.c
  9. 137
      3rdparty/libjpeg/jchuff.c
  10. 8
      3rdparty/libjpeg/jcmarker.c
  11. 17
      3rdparty/libjpeg/jcmaster.c
  12. 138
      3rdparty/libjpeg/jcomapi.c
  13. 113
      3rdparty/libjpeg/jcparam.c
  14. 28
      3rdparty/libjpeg/jdarith.c
  15. 23
      3rdparty/libjpeg/jdatadst.c
  16. 31
      3rdparty/libjpeg/jdatasrc.c
  17. 110
      3rdparty/libjpeg/jdcolor.c
  18. 9
      3rdparty/libjpeg/jdct.h
  19. 86
      3rdparty/libjpeg/jdhuff.c
  20. 20
      3rdparty/libjpeg/jdmarker.c
  21. 25
      3rdparty/libjpeg/jdmaster.c
  22. 65
      3rdparty/libjpeg/jdmerge.c
  23. 4
      3rdparty/libjpeg/jerror.h
  24. 112
      3rdparty/libjpeg/jfdctint.c
  25. 12
      3rdparty/libjpeg/jidctint.c
  26. 60
      3rdparty/libjpeg/jmemmgr.c
  27. 8
      3rdparty/libjpeg/jmemnobs.c
  28. 9
      3rdparty/libjpeg/jpegint.h
  29. 11
      3rdparty/libjpeg/jpeglib.h
  30. 6
      3rdparty/libjpeg/jutils.c
  31. 6
      3rdparty/libjpeg/jversion.h
  32. 16
      modules/core/include/opencv2/core/cvdef.h
  33. 6
      modules/features2d/include/opencv2/features2d.hpp
  34. 5
      modules/features2d/perf/perf_feature2d.hpp
  35. 85
      modules/features2d/perf/perf_sift.cpp
  36. 5
      modules/features2d/src/sift.dispatch.cpp
  37. 5
      modules/features2d/test/test_descriptors_invariance.cpp
  38. 24
      modules/features2d/test/test_descriptors_invariance.impl.hpp
  39. 5
      modules/features2d/test/test_detectors_invariance.cpp
  40. 4
      modules/flann/include/opencv2/flann/dist.h
  41. 14
      modules/flann/include/opencv2/flann/kdtree_single_index.h
  42. 5
      modules/flann/include/opencv2/flann/kmeans_index.h
  43. 26
      modules/flann/include/opencv2/flann/lsh_index.h
  44. 2
      modules/flann/include/opencv2/flann/lsh_table.h
  45. 4
      modules/flann/include/opencv2/flann/result_set.h
  46. 6
      modules/flann/test/test_lshtable_badarg.cpp
  47. 15
      modules/imgcodecs/src/ios_conversions.mm

@ -4,9 +4,9 @@ ocv_warnings_disable(CMAKE_C_FLAGS -Wunused-parameter -Wsign-compare -Wshorten-6
set(VERSION_MAJOR 2) set(VERSION_MAJOR 2)
set(VERSION_MINOR 0) set(VERSION_MINOR 0)
set(VERSION_REVISION 4) set(VERSION_REVISION 5)
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION})
set(LIBJPEG_TURBO_VERSION_NUMBER 2000004) set(LIBJPEG_TURBO_VERSION_NUMBER 2000005)
string(TIMESTAMP BUILD "opencv-${OPENCV_VERSION}-libjpeg-turbo") string(TIMESTAMP BUILD "opencv-${OPENCV_VERSION}-libjpeg-turbo")
if(CMAKE_BUILD_TYPE STREQUAL "Debug") if(CMAKE_BUILD_TYPE STREQUAL "Debug")
@ -65,6 +65,8 @@ set(JPEG_LIB_VERSION 62)
# OpenCV # OpenCV
set(JPEG_LIB_VERSION "${VERSION}-${JPEG_LIB_VERSION}" PARENT_SCOPE) set(JPEG_LIB_VERSION "${VERSION}-${JPEG_LIB_VERSION}" PARENT_SCOPE)
set(THREAD_LOCAL "") # WITH_TURBOJPEG is not used
if(MSVC) if(MSVC)
add_definitions(-W3 -wd4996 -wd4018) add_definitions(-W3 -wd4996 -wd4018)
endif() endif()

@ -15,6 +15,9 @@
#endif #endif
#endif #endif
/* How to obtain thread-local storage */
#define THREAD_LOCAL @THREAD_LOCAL@
/* Define to the full name of this package. */ /* Define to the full name of this package. */
#define PACKAGE_NAME "@CMAKE_PROJECT_NAME@" #define PACKAGE_NAME "@CMAKE_PROJECT_NAME@"

@ -143,7 +143,6 @@ empty_mem_output_buffer(j_compress_ptr cinfo)
MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
if (dest->newbuffer != NULL)
free(dest->newbuffer); free(dest->newbuffer);
dest->newbuffer = nextbuffer; dest->newbuffer = nextbuffer;

@ -4,7 +4,7 @@
* This file was part of the Independent JPEG Group's software: * This file was part of the Independent JPEG Group's software:
* Copyright (C) 1991-2012, Thomas G. Lane, Guido Vollbeding. * Copyright (C) 1991-2012, Thomas G. Lane, Guido Vollbeding.
* libjpeg-turbo Modifications: * libjpeg-turbo Modifications:
* Copyright (C) 2010, 2012-2019, D. R. Commander. * Copyright (C) 2010, 2012-2020, D. R. Commander.
* For conditions of distribution and use, see the accompanying README.ijg * For conditions of distribution and use, see the accompanying README.ijg
* file. * file.
* *
@ -36,7 +36,7 @@
*/ */
#define JCOPYRIGHT \ #define JCOPYRIGHT \
"Copyright (C) 2009-2019 D. R. Commander\n" \ "Copyright (C) 2009-2020 D. R. Commander\n" \
"Copyright (C) 2011-2016 Siarhei Siamashka\n" \ "Copyright (C) 2011-2016 Siarhei Siamashka\n" \
"Copyright (C) 2015-2016, 2018 Matthieu Darbois\n" \ "Copyright (C) 2015-2016, 2018 Matthieu Darbois\n" \
"Copyright (C) 2015 Intel Corporation\n" \ "Copyright (C) 2015 Intel Corporation\n" \
@ -49,4 +49,4 @@
"Copyright (C) 1991-2016 Thomas G. Lane, Guido Vollbeding" "Copyright (C) 1991-2016 Thomas G. Lane, Guido Vollbeding"
#define JCOPYRIGHT_SHORT \ #define JCOPYRIGHT_SHORT \
"Copyright (C) 1991-2019 The libjpeg-turbo Project and many others" "Copyright (C) 1991-2020 The libjpeg-turbo Project and many others"

@ -1,7 +1,7 @@
The Independent JPEG Group's JPEG software The Independent JPEG Group's JPEG software
========================================== ==========================================
README for release 9c of 14-Jan-2018 README for release 9d of 12-Jan-2020
==================================== ====================================
This distribution contains the ninth public release of the Independent JPEG This distribution contains the ninth public release of the Independent JPEG
@ -10,8 +10,8 @@ to use it for any purpose, subject to the conditions under LEGAL ISSUES, below.
This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone, This software is the work of Tom Lane, Guido Vollbeding, Philip Gladstone,
Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson, Bill Allombert, Jim Boucher, Lee Crocker, Bob Friesenhahn, Ben Jackson,
Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi, Ge' Weijers, John Korejwa, Julian Minguillon, Luis Ortiz, George Phillips, Davide Rossi,
and other members of the Independent JPEG Group. Ge' Weijers, and other members of the Independent JPEG Group.
IJG is not affiliated with the ISO/IEC JTC1/SC29/WG1 standards committee IJG is not affiliated with the ISO/IEC JTC1/SC29/WG1 standards committee
(previously known as JPEG, together with ITU-T SG16). (previously known as JPEG, together with ITU-T SG16).
@ -115,7 +115,7 @@ with respect to this software, its quality, accuracy, merchantability, or
fitness for a particular purpose. This software is provided "AS IS", and you, fitness for a particular purpose. This software is provided "AS IS", and you,
its user, assume the entire risk as to its quality and accuracy. its user, assume the entire risk as to its quality and accuracy.
This software is copyright (C) 1991-2018, Thomas G. Lane, Guido Vollbeding. This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
All Rights Reserved except as specified below. All Rights Reserved except as specified below.
Permission is hereby granted to use, copy, modify, and distribute this Permission is hereby granted to use, copy, modify, and distribute this
@ -152,13 +152,6 @@ The same holds for its supporting scripts (config.guess, config.sub,
ltmain.sh). Another support script, install-sh, is copyright by X Consortium ltmain.sh). Another support script, install-sh, is copyright by X Consortium
but is also freely distributable. but is also freely distributable.
The IJG distribution formerly included code to read and write GIF files.
To avoid entanglement with the Unisys LZW patent (now expired), GIF reading
support has been removed altogether, and the GIF writer has been simplified
to produce "uncompressed GIFs". This technique does not use the LZW
algorithm; the resulting GIF files are larger than usual, but are readable
by all standard GIF decoders.
REFERENCES REFERENCES
========== ==========
@ -246,8 +239,8 @@ ARCHIVE LOCATIONS
The "official" archive site for this software is www.ijg.org. The "official" archive site for this software is www.ijg.org.
The most recent released version can always be found there in The most recent released version can always be found there in
directory "files". This particular version will be archived as directory "files". This particular version will be archived as
http://www.ijg.org/files/jpegsrc.v9c.tar.gz, and in Windows-compatible http://www.ijg.org/files/jpegsrc.v9d.tar.gz, and in Windows-compatible
"zip" archive format as http://www.ijg.org/files/jpegsr9c.zip. "zip" archive format as http://www.ijg.org/files/jpegsr9d.zip.
The JPEG FAQ (Frequently Asked Questions) article is a source of some The JPEG FAQ (Frequently Asked Questions) article is a source of some
general information about JPEG. general information about JPEG.

@ -1,6 +1,55 @@
CHANGE LOG for Independent JPEG Group's JPEG software CHANGE LOG for Independent JPEG Group's JPEG software
Version 9d 12-Jan-2020
-----------------------
Optimize the optimal Huffman code table generation to produce
slightly smaller files. Thank to John Korejwa for suggestion.
Note: Requires rebuild of testimgp.jpg.
Decoding Huffman: Use default tables if tables are not defined.
Thank to Simone Azzalin for report (Motion JPEG),
and to Martin Strunz for hint.
Add sanity check in optimal Huffman code table generation.
Thank to Adam Farley for suggestion.
rdtarga.c: use read_byte(), with EOF check, instead of getc()
in read_*_pixel().
Thank to Chijin Zhou for cjpeg potential vulnerability report.
jmemnobs.c: respect the max_memory_to_use setting in
jpeg_mem_available() computation. Thank to Sheng Shu and
Dongdong She for djpeg potential vulnerability report.
jdarith.c, jdhuff.c: avoid left shift of negative value
compiler warning in decode_mcu_AC_refine().
Thank to Indu Bhagat for suggestion.
Add x64 (64-bit) platform support, avoid compiler warnings.
Thank to Jonathan Potter, Feiyun Wang, and Sheng Shu for suggestion.
Adjust libjpeg version specification for pkg-config file.
Thank to Chen Chen for suggestion.
Restore GIF read and write support from libjpeg version 6a.
Thank to Wolfgang Werner (W.W.) Heinz for suggestion.
Improve consistency in raw (downsampled) image data processing mode.
Thank to Zhongyuan Zhou for hint.
Avoid out of bounds array read (AC derived table pointers)
in start pass in jdhuff.c. Thank to Peng Li for report.
Improve code sanity (jdhuff.c).
Thank to Reza Mirzazade farkhani for reports.
Add jpegtran -drop option; add options to the crop extension and wipe
to fill the extra area with content from the source image region,
instead of gray out.
Version 9c 14-Jan-2018 Version 9c 14-Jan-2018
----------------------- -----------------------

@ -1,7 +1,7 @@
/* /*
* jcarith.c * jcarith.c
* *
* Developed 1997-2013 by Guido Vollbeding. * Developed 1997-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -181,11 +181,11 @@ finish_pass (j_compress_ptr cinfo)
if (e->zc) /* output final pending zero bytes */ if (e->zc) /* output final pending zero bytes */
do emit_byte(0x00, cinfo); do emit_byte(0x00, cinfo);
while (--e->zc); while (--e->zc);
emit_byte((e->c >> 19) & 0xFF, cinfo); emit_byte((int) ((e->c >> 19) & 0xFF), cinfo);
if (((e->c >> 19) & 0xFF) == 0xFF) if (((e->c >> 19) & 0xFF) == 0xFF)
emit_byte(0x00, cinfo); emit_byte(0x00, cinfo);
if (e->c & 0x7F800L) { if (e->c & 0x7F800L) {
emit_byte((e->c >> 11) & 0xFF, cinfo); emit_byte((int) ((e->c >> 11) & 0xFF), cinfo);
if (((e->c >> 11) & 0xFF) == 0xFF) if (((e->c >> 11) & 0xFF) == 0xFF)
emit_byte(0x00, cinfo); emit_byte(0x00, cinfo);
} }
@ -280,7 +280,8 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
/* Note: The 3 spacer bits in the C register guarantee /* Note: The 3 spacer bits in the C register guarantee
* that the new buffer byte can't be 0xFF here * that the new buffer byte can't be 0xFF here
* (see page 160 in the P&M JPEG book). */ * (see page 160 in the P&M JPEG book). */
e->buffer = temp & 0xFF; /* new output byte, might overflow later */ /* New output byte, might overflow later */
e->buffer = (int) (temp & 0xFF);
} else if (temp == 0xFF) { } else if (temp == 0xFF) {
++e->sc; /* stack 0xFF byte (which might overflow later) */ ++e->sc; /* stack 0xFF byte (which might overflow later) */
} else { } else {
@ -302,7 +303,8 @@ arith_encode (j_compress_ptr cinfo, unsigned char *st, int val)
emit_byte(0x00, cinfo); emit_byte(0x00, cinfo);
} while (--e->sc); } while (--e->sc);
} }
e->buffer = temp & 0xFF; /* new output byte (can still overflow) */ /* New output byte (can still overflow) */
e->buffer = (int) (temp & 0xFF);
} }
e->c &= 0x7FFFFL; e->c &= 0x7FFFFL;
e->ct += 8; e->ct += 8;
@ -926,9 +928,8 @@ jinit_arith_encoder (j_compress_ptr cinfo)
arith_entropy_ptr entropy; arith_entropy_ptr entropy;
int i; int i;
entropy = (arith_entropy_ptr) entropy = (arith_entropy_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(arith_entropy_encoder));
SIZEOF(arith_entropy_encoder));
cinfo->entropy = &entropy->pub; cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass; entropy->pub.start_pass = start_pass;
entropy->pub.finish_pass = finish_pass; entropy->pub.finish_pass = finish_pass;

@ -2,7 +2,7 @@
* jccolor.c * jccolor.c
* *
* Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 1991-1996, Thomas G. Lane.
* Modified 2011-2013 by Guido Vollbeding. * Modified 2011-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -105,14 +105,14 @@ rgb_ycc_start (j_compress_ptr cinfo)
/* Allocate and fill in the conversion tables. */ /* Allocate and fill in the conversion tables. */
cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(TABLE_SIZE * SIZEOF(INT32))); TABLE_SIZE * SIZEOF(INT32));
for (i = 0; i <= MAXJSAMPLE; i++) { for (i = 0; i <= MAXJSAMPLE; i++) {
rgb_ycc_tab[i+R_Y_OFF] = FIX(0.299) * i; rgb_ycc_tab[i+R_Y_OFF] = FIX(0.299) * i;
rgb_ycc_tab[i+G_Y_OFF] = FIX(0.587) * i; rgb_ycc_tab[i+G_Y_OFF] = FIX(0.587) * i;
rgb_ycc_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF; rgb_ycc_tab[i+B_Y_OFF] = FIX(0.114) * i + ONE_HALF;
rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.168735892)) * i; rgb_ycc_tab[i+R_CB_OFF] = (- FIX(0.168735892)) * i;
rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.331264108)) * i; rgb_ycc_tab[i+G_CB_OFF] = (- FIX(0.331264108)) * i;
/* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr. /* We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
* This ensures that the maximum output will round to MAXJSAMPLE * This ensures that the maximum output will round to MAXJSAMPLE
* not MAXJSAMPLE+1, and thus that we don't have to range-limit. * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
@ -121,8 +121,8 @@ rgb_ycc_start (j_compress_ptr cinfo)
/* B=>Cb and R=>Cr tables are the same /* B=>Cb and R=>Cr tables are the same
rgb_ycc_tab[i+R_CR_OFF] = FIX(0.5) * i + CBCR_OFFSET + ONE_HALF-1; rgb_ycc_tab[i+R_CR_OFF] = FIX(0.5) * i + CBCR_OFFSET + ONE_HALF-1;
*/ */
rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.418687589)) * i; rgb_ycc_tab[i+G_CR_OFF] = (- FIX(0.418687589)) * i;
rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.081312411)) * i; rgb_ycc_tab[i+B_CR_OFF] = (- FIX(0.081312411)) * i;
} }
} }
@ -131,12 +131,12 @@ rgb_ycc_start (j_compress_ptr cinfo)
* Convert some rows of samples to the JPEG colorspace. * Convert some rows of samples to the JPEG colorspace.
* *
* Note that we change from the application's interleaved-pixel format * Note that we change from the application's interleaved-pixel format
* to our internal noninterleaved, one-plane-per-component format. * to our internal noninterleaved, one-plane-per-component format. The
* The input buffer is therefore three times as wide as the output buffer. * input buffer is therefore three times as wide as the output buffer.
* *
* A starting row offset is provided only for the output buffer. The caller * A starting row offset is provided only for the output buffer. The
* can easily adjust the passed input_buf value to accommodate any row * caller can easily adjust the passed input_buf value to accommodate
* offset required on that side. * any row offset required on that side.
*/ */
METHODDEF(void) METHODDEF(void)
@ -145,8 +145,8 @@ rgb_ycc_convert (j_compress_ptr cinfo,
JDIMENSION output_row, int num_rows) JDIMENSION output_row, int num_rows)
{ {
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register int r, g, b; register int r, g, b;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register JSAMPROW inptr; register JSAMPROW inptr;
register JSAMPROW outptr0, outptr1, outptr2; register JSAMPROW outptr0, outptr1, outptr2;
register JDIMENSION col; register JDIMENSION col;
@ -162,6 +162,7 @@ rgb_ycc_convert (j_compress_ptr cinfo,
r = GETJSAMPLE(inptr[RGB_RED]); r = GETJSAMPLE(inptr[RGB_RED]);
g = GETJSAMPLE(inptr[RGB_GREEN]); g = GETJSAMPLE(inptr[RGB_GREEN]);
b = GETJSAMPLE(inptr[RGB_BLUE]); b = GETJSAMPLE(inptr[RGB_BLUE]);
inptr += RGB_PIXELSIZE;
/* If the inputs are 0..MAXJSAMPLE, the outputs of these equations /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
* must be too; we do not need an explicit range-limiting operation. * must be too; we do not need an explicit range-limiting operation.
* Hence the value being shifted is never negative, and we don't * Hence the value being shifted is never negative, and we don't
@ -179,7 +180,6 @@ rgb_ycc_convert (j_compress_ptr cinfo,
outptr2[col] = (JSAMPLE) outptr2[col] = (JSAMPLE)
((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
>> SCALEBITS); >> SCALEBITS);
inptr += RGB_PIXELSIZE;
} }
} }
} }
@ -201,8 +201,8 @@ rgb_gray_convert (j_compress_ptr cinfo,
JDIMENSION output_row, int num_rows) JDIMENSION output_row, int num_rows)
{ {
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register int r, g, b; register int r, g, b;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register JSAMPROW inptr; register JSAMPROW inptr;
register JSAMPROW outptr; register JSAMPROW outptr;
register JDIMENSION col; register JDIMENSION col;
@ -215,11 +215,11 @@ rgb_gray_convert (j_compress_ptr cinfo,
r = GETJSAMPLE(inptr[RGB_RED]); r = GETJSAMPLE(inptr[RGB_RED]);
g = GETJSAMPLE(inptr[RGB_GREEN]); g = GETJSAMPLE(inptr[RGB_GREEN]);
b = GETJSAMPLE(inptr[RGB_BLUE]); b = GETJSAMPLE(inptr[RGB_BLUE]);
inptr += RGB_PIXELSIZE;
/* Y */ /* Y */
outptr[col] = (JSAMPLE) outptr[col] = (JSAMPLE)
((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF])
>> SCALEBITS); >> SCALEBITS);
inptr += RGB_PIXELSIZE;
} }
} }
} }
@ -228,8 +228,8 @@ rgb_gray_convert (j_compress_ptr cinfo,
/* /*
* Convert some rows of samples to the JPEG colorspace. * Convert some rows of samples to the JPEG colorspace.
* This version handles Adobe-style CMYK->YCCK conversion, * This version handles Adobe-style CMYK->YCCK conversion,
* where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the same * where we convert R=1-C, G=1-M, and B=1-Y to YCbCr using the
* conversion as above, while passing K (black) unchanged. * same conversion as above, while passing K (black) unchanged.
* We assume rgb_ycc_start has been called. * We assume rgb_ycc_start has been called.
*/ */
@ -239,8 +239,8 @@ cmyk_ycck_convert (j_compress_ptr cinfo,
JDIMENSION output_row, int num_rows) JDIMENSION output_row, int num_rows)
{ {
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register int r, g, b; register int r, g, b;
register INT32 * ctab = cconvert->rgb_ycc_tab;
register JSAMPROW inptr; register JSAMPROW inptr;
register JSAMPROW outptr0, outptr1, outptr2, outptr3; register JSAMPROW outptr0, outptr1, outptr2, outptr3;
register JDIMENSION col; register JDIMENSION col;
@ -259,6 +259,7 @@ cmyk_ycck_convert (j_compress_ptr cinfo,
b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); b = MAXJSAMPLE - GETJSAMPLE(inptr[2]);
/* K passes through as-is */ /* K passes through as-is */
outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */ outptr3[col] = inptr[3]; /* don't need GETJSAMPLE here */
inptr += 4;
/* If the inputs are 0..MAXJSAMPLE, the outputs of these equations /* If the inputs are 0..MAXJSAMPLE, the outputs of these equations
* must be too; we do not need an explicit range-limiting operation. * must be too; we do not need an explicit range-limiting operation.
* Hence the value being shifted is never negative, and we don't * Hence the value being shifted is never negative, and we don't
@ -276,7 +277,6 @@ cmyk_ycck_convert (j_compress_ptr cinfo,
outptr2[col] = (JSAMPLE) outptr2[col] = (JSAMPLE)
((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF])
>> SCALEBITS); >> SCALEBITS);
inptr += 4;
} }
} }
} }
@ -312,13 +312,13 @@ rgb_rgb1_convert (j_compress_ptr cinfo,
r = GETJSAMPLE(inptr[RGB_RED]); r = GETJSAMPLE(inptr[RGB_RED]);
g = GETJSAMPLE(inptr[RGB_GREEN]); g = GETJSAMPLE(inptr[RGB_GREEN]);
b = GETJSAMPLE(inptr[RGB_BLUE]); b = GETJSAMPLE(inptr[RGB_BLUE]);
inptr += RGB_PIXELSIZE;
/* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD /* Assume that MAXJSAMPLE+1 is a power of 2, so that the MOD
* (modulo) operator is equivalent to the bitmask operator AND. * (modulo) operator is equivalent to the bitmask operator AND.
*/ */
outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE); outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE);
outptr1[col] = (JSAMPLE) g; outptr1[col] = (JSAMPLE) g;
outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE); outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE);
inptr += RGB_PIXELSIZE;
} }
} }
} }
@ -335,17 +335,17 @@ grayscale_convert (j_compress_ptr cinfo,
JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
JDIMENSION output_row, int num_rows) JDIMENSION output_row, int num_rows)
{ {
int instride = cinfo->input_components;
register JSAMPROW inptr; register JSAMPROW inptr;
register JSAMPROW outptr; register JSAMPROW outptr;
register JDIMENSION col; register JDIMENSION count;
register int instride = cinfo->input_components;
JDIMENSION num_cols = cinfo->image_width; JDIMENSION num_cols = cinfo->image_width;
while (--num_rows >= 0) { while (--num_rows >= 0) {
inptr = *input_buf++; inptr = *input_buf++;
outptr = output_buf[0][output_row++]; outptr = output_buf[0][output_row++];
for (col = 0; col < num_cols; col++) { for (count = num_cols; count > 0; count--) {
outptr[col] = inptr[0]; /* don't need GETJSAMPLE() here */ *outptr++ = *inptr; /* don't need GETJSAMPLE() here */
inptr += instride; inptr += instride;
} }
} }
@ -396,21 +396,21 @@ null_convert (j_compress_ptr cinfo,
JSAMPARRAY input_buf, JSAMPIMAGE output_buf, JSAMPARRAY input_buf, JSAMPIMAGE output_buf,
JDIMENSION output_row, int num_rows) JDIMENSION output_row, int num_rows)
{ {
int ci;
register int nc = cinfo->num_components;
register JSAMPROW inptr; register JSAMPROW inptr;
register JSAMPROW outptr; register JSAMPROW outptr;
register JDIMENSION col; register JDIMENSION count;
register int num_comps = cinfo->num_components;
JDIMENSION num_cols = cinfo->image_width; JDIMENSION num_cols = cinfo->image_width;
int ci;
while (--num_rows >= 0) { while (--num_rows >= 0) {
/* It seems fastest to make a separate pass for each component. */ /* It seems fastest to make a separate pass for each component. */
for (ci = 0; ci < nc; ci++) { for (ci = 0; ci < num_comps; ci++) {
inptr = input_buf[0] + ci; inptr = input_buf[0] + ci;
outptr = output_buf[ci][output_row]; outptr = output_buf[ci][output_row];
for (col = 0; col < num_cols; col++) { for (count = num_cols; count > 0; count--) {
*outptr++ = *inptr; /* don't need GETJSAMPLE() here */ *outptr++ = *inptr; /* don't need GETJSAMPLE() here */
inptr += nc; inptr += num_comps;
} }
} }
input_buf++; input_buf++;
@ -439,9 +439,8 @@ jinit_color_converter (j_compress_ptr cinfo)
{ {
my_cconvert_ptr cconvert; my_cconvert_ptr cconvert;
cconvert = (my_cconvert_ptr) cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_converter));
SIZEOF(my_color_converter));
cinfo->cconvert = &cconvert->pub; cinfo->cconvert = &cconvert->pub;
/* set start_pass to null method until we find out differently */ /* set start_pass to null method until we find out differently */
cconvert->pub.start_pass = null_method; cconvert->pub.start_pass = null_method;
@ -455,9 +454,11 @@ jinit_color_converter (j_compress_ptr cinfo)
case JCS_RGB: case JCS_RGB:
case JCS_BG_RGB: case JCS_BG_RGB:
#if RGB_PIXELSIZE != 3
if (cinfo->input_components != RGB_PIXELSIZE) if (cinfo->input_components != RGB_PIXELSIZE)
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
break; break;
#endif /* else share code with YCbCr */
case JCS_YCbCr: case JCS_YCbCr:
case JCS_BG_YCC: case JCS_BG_YCC:
@ -474,7 +475,6 @@ jinit_color_converter (j_compress_ptr cinfo)
default: /* JCS_UNKNOWN can be anything */ default: /* JCS_UNKNOWN can be anything */
if (cinfo->input_components < 1) if (cinfo->input_components < 1)
ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
break;
} }
/* Support color transform only for RGB colorspaces */ /* Support color transform only for RGB colorspaces */
@ -507,7 +507,8 @@ jinit_color_converter (j_compress_ptr cinfo)
case JCS_BG_RGB: case JCS_BG_RGB:
if (cinfo->num_components != 3) if (cinfo->num_components != 3)
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
if (cinfo->in_color_space == cinfo->jpeg_color_space) { if (cinfo->in_color_space != cinfo->jpeg_color_space)
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
switch (cinfo->color_transform) { switch (cinfo->color_transform) {
case JCT_NONE: case JCT_NONE:
cconvert->pub.color_convert = rgb_convert; cconvert->pub.color_convert = rgb_convert;
@ -518,8 +519,6 @@ jinit_color_converter (j_compress_ptr cinfo)
default: default:
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
} }
} else
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
break; break;
case JCS_YCbCr: case JCS_YCbCr:
@ -572,10 +571,9 @@ jinit_color_converter (j_compress_ptr cinfo)
case JCS_CMYK: case JCS_CMYK:
if (cinfo->num_components != 4) if (cinfo->num_components != 4)
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
if (cinfo->in_color_space == JCS_CMYK) if (cinfo->in_color_space != JCS_CMYK)
cconvert->pub.color_convert = null_convert;
else
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
cconvert->pub.color_convert = null_convert;
break; break;
case JCS_YCCK: case JCS_YCCK:
@ -599,6 +597,5 @@ jinit_color_converter (j_compress_ptr cinfo)
cinfo->num_components != cinfo->input_components) cinfo->num_components != cinfo->input_components)
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
cconvert->pub.color_convert = null_convert; cconvert->pub.color_convert = null_convert;
break;
} }
} }

@ -2,7 +2,7 @@
* jchuff.c * jchuff.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2006-2013 by Guido Vollbeding. * Modified 2006-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -178,13 +178,12 @@ jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
htbl = htbl =
isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
if (htbl == NULL) if (htbl == NULL)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);
/* Allocate a workspace if we haven't already done so. */ /* Allocate a workspace if we haven't already done so. */
if (*pdtbl == NULL) if (*pdtbl == NULL)
*pdtbl = (c_derived_tbl *) *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(c_derived_tbl));
SIZEOF(c_derived_tbl));
dtbl = *pdtbl; dtbl = *pdtbl;
/* Figure C.1: make table of Huffman code length for each symbol */ /* Figure C.1: make table of Huffman code length for each symbol */
@ -1256,10 +1255,82 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */ UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
int codesize[257]; /* codesize[k] = code length of symbol k */ int codesize[257]; /* codesize[k] = code length of symbol k */
int others[257]; /* next symbol in current branch of tree */ int others[257]; /* next symbol in current branch of tree */
int c1, c2; int c1, c2, i, j;
int p, i, j; UINT8 *p;
long v; long v;
freq[256] = 1; /* make sure 256 has a nonzero count */
/* Including the pseudo-symbol 256 in the Huffman procedure guarantees
* that no real symbol is given code-value of all ones, because 256
* will be placed last in the largest codeword category.
* In the symbol list build procedure this element serves as sentinel
* for the zero run loop.
*/
#ifndef DONT_USE_FANCY_HUFF_OPT
/* Build list of symbols sorted in order of descending frequency */
/* This approach has several benefits (thank to John Korejwa for the idea):
* 1.
* If a codelength category is split during the length limiting procedure
* below, the feature that more frequent symbols are assigned shorter
* codewords remains valid for the adjusted code.
* 2.
* To reduce consecutive ones in a Huffman data stream (thus reducing the
* number of stuff bytes in JPEG) it is preferable to follow 0 branches
* (and avoid 1 branches) as much as possible. This is easily done by
* assigning symbols to leaves of the Huffman tree in order of decreasing
* frequency, with no secondary sort based on codelengths.
* 3.
* The symbol list can be built independently from the assignment of code
* lengths by the Huffman procedure below.
* Note: The symbol list build procedure must be performed first, because
* the Huffman procedure assigning the codelengths clobbers the frequency
* counts!
*/
/* Here we use the others array as a linked list of nonzero frequencies
* to be sorted. Already sorted elements are removed from the list.
*/
/* Building list */
/* This item does not correspond to a valid symbol frequency and is used
* as starting index.
*/
j = 256;
for (i = 0;; i++) {
if (freq[i] == 0) /* skip zero frequencies */
continue;
if (i > 255)
break;
others[j] = i; /* this symbol value */
j = i; /* previous symbol value */
}
others[j] = -1; /* mark end of list */
/* Sorting list */
p = htbl->huffval;
while ((c1 = others[256]) >= 0) {
v = freq[c1];
i = c1; /* first symbol value */
j = 256; /* pseudo symbol value for starting index */
while ((c2 = others[c1]) >= 0) {
if (freq[c2] > v) {
v = freq[c2];
i = c2; /* this symbol value */
j = c1; /* previous symbol value */
}
c1 = c2;
}
others[j] = others[i]; /* remove this symbol i from list */
*p++ = (UINT8) i;
}
#endif /* DONT_USE_FANCY_HUFF_OPT */
/* This algorithm is explained in section K.2 of the JPEG standard */ /* This algorithm is explained in section K.2 of the JPEG standard */
MEMZERO(bits, SIZEOF(bits)); MEMZERO(bits, SIZEOF(bits));
@ -1267,12 +1338,6 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
for (i = 0; i < 257; i++) for (i = 0; i < 257; i++)
others[i] = -1; /* init links to empty */ others[i] = -1; /* init links to empty */
freq[256] = 1; /* make sure 256 has a nonzero count */
/* Including the pseudo-symbol 256 in the Huffman procedure guarantees
* that no real symbol is given code-value of all ones, because 256
* will be placed last in the largest codeword category.
*/
/* Huffman's basic algorithm to assign optimal code lengths to symbols */ /* Huffman's basic algorithm to assign optimal code lengths to symbols */
for (;;) { for (;;) {
@ -1329,7 +1394,7 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
/* The JPEG standard seems to think that this can't happen, */ /* The JPEG standard seems to think that this can't happen, */
/* but I'm paranoid... */ /* but I'm paranoid... */
if (codesize[i] > MAX_CLEN) if (codesize[i] > MAX_CLEN)
ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW); ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
bits[codesize[i]]++; bits[codesize[i]]++;
} }
@ -1349,8 +1414,11 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
for (i = MAX_CLEN; i > 16; i--) { for (i = MAX_CLEN; i > 16; i--) {
while (bits[i] > 0) { while (bits[i] > 0) {
j = i - 2; /* find length of new prefix to be used */ j = i - 2; /* find length of new prefix to be used */
while (bits[j] == 0) while (bits[j] == 0) {
if (j == 0)
ERREXIT(cinfo, JERR_HUFF_CLEN_OUTOFBOUNDS);
j--; j--;
}
bits[i] -= 2; /* remove two symbols */ bits[i] -= 2; /* remove two symbols */
bits[i-1]++; /* one goes in this length */ bits[i-1]++; /* one goes in this length */
@ -1367,20 +1435,23 @@ jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
/* Return final symbol counts (only for lengths 0..16) */ /* Return final symbol counts (only for lengths 0..16) */
MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits)); MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
#ifdef DONT_USE_FANCY_HUFF_OPT
/* Return a list of the symbols sorted by code length */ /* Return a list of the symbols sorted by code length */
/* It's not real clear to me why we don't need to consider the codelength /* Note: Due to the codelength changes made above, it can happen
* changes made above, but the JPEG spec seems to think this works. * that more frequent symbols are assigned longer codewords.
*/ */
p = 0; p = htbl->huffval;
for (i = 1; i <= MAX_CLEN; i++) { for (i = 1; i <= MAX_CLEN; i++) {
for (j = 0; j <= 255; j++) { for (j = 0; j <= 255; j++) {
if (codesize[j] == i) { if (codesize[j] == i) {
htbl->huffval[p] = (UINT8) j; *p++ = (UINT8) j;
p++;
} }
} }
} }
#endif /* DONT_USE_FANCY_HUFF_OPT */
/* Set sent_table FALSE so updated table will be written to JPEG file. */ /* Set sent_table FALSE so updated table will be written to JPEG file. */
htbl->sent_table = FALSE; htbl->sent_table = FALSE;
} }
@ -1400,13 +1471,13 @@ finish_pass_gather (j_compress_ptr cinfo)
boolean did_dc[NUM_HUFF_TBLS]; boolean did_dc[NUM_HUFF_TBLS];
boolean did_ac[NUM_HUFF_TBLS]; boolean did_ac[NUM_HUFF_TBLS];
/* It's important not to apply jpeg_gen_optimal_table more than once
* per table, because it clobbers the input frequency counts!
*/
if (cinfo->progressive_mode) if (cinfo->progressive_mode)
/* Flush out buffered data (all we care about is counting the EOB symbol) */ /* Flush out buffered data (all we care about is counting the EOB symbol) */
emit_eobrun(entropy); emit_eobrun(entropy);
/* It's important not to apply jpeg_gen_optimal_table more than once
* per table, because it clobbers the input frequency counts!
*/
MEMZERO(did_dc, SIZEOF(did_dc)); MEMZERO(did_dc, SIZEOF(did_dc));
MEMZERO(did_ac, SIZEOF(did_ac)); MEMZERO(did_ac, SIZEOF(did_ac));
@ -1475,9 +1546,8 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
entropy->pub.encode_mcu = encode_mcu_AC_refine; entropy->pub.encode_mcu = encode_mcu_AC_refine;
/* AC refinement needs a correction bit buffer */ /* AC refinement needs a correction bit buffer */
if (entropy->bit_buffer == NULL) if (entropy->bit_buffer == NULL)
entropy->bit_buffer = (char *) entropy->bit_buffer = (char *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, MAX_CORR_BITS * SIZEOF(char));
MAX_CORR_BITS * SIZEOF(char));
} }
} }
@ -1505,9 +1575,8 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
/* Allocate and zero the statistics tables */ /* Allocate and zero the statistics tables */
/* Note that jpeg_gen_optimal_table expects 257 entries in each table! */ /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
if (entropy->dc_count_ptrs[tbl] == NULL) if (entropy->dc_count_ptrs[tbl] == NULL)
entropy->dc_count_ptrs[tbl] = (long *) entropy->dc_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
257 * SIZEOF(long));
MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long)); MEMZERO(entropy->dc_count_ptrs[tbl], 257 * SIZEOF(long));
} else { } else {
/* Compute derived values for Huffman tables */ /* Compute derived values for Huffman tables */
@ -1525,9 +1594,8 @@ start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
if (tbl < 0 || tbl >= NUM_HUFF_TBLS) if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl); ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
if (entropy->ac_count_ptrs[tbl] == NULL) if (entropy->ac_count_ptrs[tbl] == NULL)
entropy->ac_count_ptrs[tbl] = (long *) entropy->ac_count_ptrs[tbl] = (long *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, 257 * SIZEOF(long));
257 * SIZEOF(long));
MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long)); MEMZERO(entropy->ac_count_ptrs[tbl], 257 * SIZEOF(long));
} else { } else {
jpeg_make_c_derived_tbl(cinfo, FALSE, tbl, jpeg_make_c_derived_tbl(cinfo, FALSE, tbl,
@ -1556,9 +1624,8 @@ jinit_huff_encoder (j_compress_ptr cinfo)
huff_entropy_ptr entropy; huff_entropy_ptr entropy;
int i; int i;
entropy = (huff_entropy_ptr) entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_encoder));
SIZEOF(huff_entropy_encoder));
cinfo->entropy = &entropy->pub; cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass_huff; entropy->pub.start_pass = start_pass_huff;

@ -2,7 +2,7 @@
* jcmarker.c * jcmarker.c
* *
* Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 1991-1998, Thomas G. Lane.
* Modified 2003-2013 by Guido Vollbeding. * Modified 2003-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -471,7 +471,6 @@ emit_adobe_app14 (j_compress_ptr cinfo)
break; break;
default: default:
emit_byte(cinfo, 0); /* Color transform = 0 */ emit_byte(cinfo, 0); /* Color transform = 0 */
break;
} }
} }
@ -702,9 +701,8 @@ jinit_marker_writer (j_compress_ptr cinfo)
my_marker_ptr marker; my_marker_ptr marker;
/* Create the subobject */ /* Create the subobject */
marker = (my_marker_ptr) marker = (my_marker_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_marker_writer));
SIZEOF(my_marker_writer));
cinfo->marker = &marker->pub; cinfo->marker = &marker->pub;
/* Initialize method pointers */ /* Initialize method pointers */
marker->pub.write_file_header = write_file_header; marker->pub.write_file_header = write_file_header;

@ -2,7 +2,7 @@
* jcmaster.c * jcmaster.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2003-2017 by Guido Vollbeding. * Modified 2003-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -62,7 +62,7 @@ initial_setup (j_compress_ptr cinfo)
case 5: cinfo->natural_order = jpeg_natural_order5; break; case 5: cinfo->natural_order = jpeg_natural_order5; break;
case 6: cinfo->natural_order = jpeg_natural_order6; break; case 6: cinfo->natural_order = jpeg_natural_order6; break;
case 7: cinfo->natural_order = jpeg_natural_order7; break; case 7: cinfo->natural_order = jpeg_natural_order7; break;
default: cinfo->natural_order = jpeg_natural_order; break; default: cinfo->natural_order = jpeg_natural_order;
} }
/* Derive lim_Se from block_size */ /* Derive lim_Se from block_size */
@ -114,18 +114,22 @@ initial_setup (j_compress_ptr cinfo)
*/ */
ssize = 1; ssize = 1;
#ifdef DCT_SCALING_SUPPORTED #ifdef DCT_SCALING_SUPPORTED
if (! cinfo->raw_data_in)
while (cinfo->min_DCT_h_scaled_size * ssize <= while (cinfo->min_DCT_h_scaled_size * ssize <=
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
(cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==
0) {
ssize = ssize * 2; ssize = ssize * 2;
} }
#endif #endif
compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
ssize = 1; ssize = 1;
#ifdef DCT_SCALING_SUPPORTED #ifdef DCT_SCALING_SUPPORTED
if (! cinfo->raw_data_in)
while (cinfo->min_DCT_v_scaled_size * ssize <= while (cinfo->min_DCT_v_scaled_size * ssize <=
(cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) &&
(cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==
0) {
ssize = ssize * 2; ssize = ssize * 2;
} }
#endif #endif
@ -620,9 +624,8 @@ jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
{ {
my_master_ptr master; my_master_ptr master;
master = (my_master_ptr) master = (my_master_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_comp_master));
SIZEOF(my_comp_master));
cinfo->master = &master->pub; cinfo->master = &master->pub;
master->pub.prepare_for_pass = prepare_for_pass; master->pub.prepare_for_pass = prepare_for_pass;
master->pub.pass_startup = pass_startup; master->pub.pass_startup = pass_startup;

@ -2,6 +2,7 @@
* jcomapi.c * jcomapi.c
* *
* Copyright (C) 1994-1997, Thomas G. Lane. * Copyright (C) 1994-1997, Thomas G. Lane.
* Modified 2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -104,3 +105,140 @@ jpeg_alloc_huff_table (j_common_ptr cinfo)
tbl->sent_table = FALSE; /* make sure this is false in any new table */ tbl->sent_table = FALSE; /* make sure this is false in any new table */
return tbl; return tbl;
} }
/*
* Set up the standard Huffman tables (cf. JPEG standard section K.3).
* IMPORTANT: these are only valid for 8-bit data precision!
* (Would jutils.c be a more reasonable place to put this?)
*/
GLOBAL(JHUFF_TBL *)
jpeg_std_huff_table (j_common_ptr cinfo, boolean isDC, int tblno)
{
JHUFF_TBL **htblptr, *htbl;
const UINT8 *bits, *val;
int nsymbols, len;
static const UINT8 bits_dc_luminance[17] =
{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_luminance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_dc_chrominance[17] =
{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_chrominance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_ac_luminance[17] =
{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
static const UINT8 val_ac_luminance[] =
{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
static const UINT8 bits_ac_chrominance[17] =
{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
static const UINT8 val_ac_chrominance[] =
{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
if (cinfo->is_decompressor) {
if (isDC)
htblptr = ((j_decompress_ptr) cinfo)->dc_huff_tbl_ptrs;
else
htblptr = ((j_decompress_ptr) cinfo)->ac_huff_tbl_ptrs;
} else {
if (isDC)
htblptr = ((j_compress_ptr) cinfo)->dc_huff_tbl_ptrs;
else
htblptr = ((j_compress_ptr) cinfo)->ac_huff_tbl_ptrs;
}
switch (tblno) {
case 0:
if (isDC) {
bits = bits_dc_luminance;
val = val_dc_luminance;
} else {
bits = bits_ac_luminance;
val = val_ac_luminance;
}
break;
case 1:
if (isDC) {
bits = bits_dc_chrominance;
val = val_dc_chrominance;
} else {
bits = bits_ac_chrominance;
val = val_ac_chrominance;
}
break;
default:
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
return NULL; /* avoid compiler warnings for uninitialized variables */
}
if (htblptr[tblno] == NULL)
htblptr[tblno] = jpeg_alloc_huff_table(cinfo);
htbl = htblptr[tblno];
/* Copy the number-of-symbols-of-each-code-length counts */
MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
/* Validate the counts. We do this here mainly so we can copy the right
* number of symbols from the val[] array, without risking marching off
* the end of memory. jxhuff.c will do a more thorough test later.
*/
nsymbols = 0;
for (len = 1; len <= 16; len++)
nsymbols += bits[len];
if (nsymbols > 256)
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
if (nsymbols > 0)
MEMCOPY(htbl->huffval, val, nsymbols * SIZEOF(UINT8));
/* Initialize sent_table FALSE so table will be written to JPEG file. */
htbl->sent_table = FALSE;
return htbl;
}

@ -2,7 +2,7 @@
* jcparam.c * jcparam.c
* *
* Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 1991-1998, Thomas G. Lane.
* Modified 2003-2013 by Guido Vollbeding. * Modified 2003-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -162,112 +162,23 @@ jpeg_set_quality (j_compress_ptr cinfo, int quality, boolean force_baseline)
/* /*
* Huffman table setup routines * Reset standard Huffman tables
*/ */
LOCAL(void) LOCAL(void)
add_huff_table (j_compress_ptr cinfo, std_huff_tables (j_compress_ptr cinfo)
JHUFF_TBL **htblptr, const UINT8 *bits, const UINT8 *val)
/* Define a Huffman table */
{ {
int nsymbols, len; if (cinfo->dc_huff_tbl_ptrs[0] != NULL)
(void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 0);
if (*htblptr == NULL)
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
/* Copy the number-of-symbols-of-each-code-length counts */
MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
/* Validate the counts. We do this here mainly so we can copy the right if (cinfo->ac_huff_tbl_ptrs[0] != NULL)
* number of symbols from the val[] array, without risking marching off (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 0);
* the end of memory. jchuff.c will do a more thorough test later.
*/
nsymbols = 0;
for (len = 1; len <= 16; len++)
nsymbols += bits[len];
if (nsymbols < 1 || nsymbols > 256)
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
MEMCOPY((*htblptr)->huffval, val, nsymbols * SIZEOF(UINT8)); if (cinfo->dc_huff_tbl_ptrs[1] != NULL)
(void) jpeg_std_huff_table((j_common_ptr) cinfo, TRUE, 1);
/* Initialize sent_table FALSE so table will be written to JPEG file. */ if (cinfo->ac_huff_tbl_ptrs[1] != NULL)
(*htblptr)->sent_table = FALSE; (void) jpeg_std_huff_table((j_common_ptr) cinfo, FALSE, 1);
}
LOCAL(void)
std_huff_tables (j_compress_ptr cinfo)
/* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
/* IMPORTANT: these are only valid for 8-bit data precision! */
{
static const UINT8 bits_dc_luminance[17] =
{ /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_luminance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_dc_chrominance[17] =
{ /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
static const UINT8 val_dc_chrominance[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
static const UINT8 bits_ac_luminance[17] =
{ /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
static const UINT8 val_ac_luminance[] =
{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
static const UINT8 bits_ac_chrominance[17] =
{ /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
static const UINT8 val_ac_chrominance[] =
{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0xf9, 0xfa };
add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0],
bits_dc_luminance, val_dc_luminance);
add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0],
bits_ac_luminance, val_ac_luminance);
add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1],
bits_dc_chrominance, val_dc_chrominance);
add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1],
bits_ac_chrominance, val_ac_chrominance);
} }
@ -306,7 +217,7 @@ jpeg_set_defaults (j_compress_ptr cinfo)
cinfo->data_precision = BITS_IN_JSAMPLE; cinfo->data_precision = BITS_IN_JSAMPLE;
/* Set up two quantization tables using default quality of 75 */ /* Set up two quantization tables using default quality of 75 */
jpeg_set_quality(cinfo, 75, TRUE); jpeg_set_quality(cinfo, 75, TRUE);
/* Set up two Huffman tables */ /* Reset standard Huffman tables */
std_huff_tables(cinfo); std_huff_tables(cinfo);
/* Initialize default arithmetic coding conditioning */ /* Initialize default arithmetic coding conditioning */

@ -1,7 +1,7 @@
/* /*
* jdarith.c * jdarith.c
* *
* Developed 1997-2015 by Guido Vollbeding. * Developed 1997-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -280,7 +280,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if ((m = arith_decode(cinfo, st)) != 0) { if ((m = arith_decode(cinfo, st)) != 0) {
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
while (arith_decode(cinfo, st)) { while (arith_decode(cinfo, st)) {
if ((m <<= 1) == 0x8000) { if ((m <<= 1) == (int) 0x8000U) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ entropy->ct = -1; /* magnitude overflow */
return TRUE; return TRUE;
@ -370,7 +370,7 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
st = entropy->ac_stats[tbl] + st = entropy->ac_stats[tbl] +
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
while (arith_decode(cinfo, st)) { while (arith_decode(cinfo, st)) {
if ((m <<= 1) == 0x8000) { if ((m <<= 1) == (int) 0x8000U) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ entropy->ct = -1; /* magnitude overflow */
return TRUE; return TRUE;
@ -404,7 +404,8 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{ {
arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy; arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
unsigned char *st; unsigned char *st;
int p1, blkn; JCOEF p1;
int blkn;
/* Process restart marker if needed */ /* Process restart marker if needed */
if (cinfo->restart_interval) { if (cinfo->restart_interval) {
@ -440,7 +441,7 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
JCOEFPTR thiscoef; JCOEFPTR thiscoef;
unsigned char *st; unsigned char *st;
int tbl, k, kex; int tbl, k, kex;
int p1, m1; JCOEF p1, m1;
const int * natural_order; const int * natural_order;
/* Process restart marker if needed */ /* Process restart marker if needed */
@ -459,7 +460,7 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
tbl = cinfo->cur_comp_info[0]->ac_tbl_no; tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ m1 = -p1; /* -1 in the bit position being coded */
/* Establish EOBx (previous stage end-of-block) index */ /* Establish EOBx (previous stage end-of-block) index */
kex = cinfo->Se; kex = cinfo->Se;
@ -555,7 +556,7 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if ((m = arith_decode(cinfo, st)) != 0) { if ((m = arith_decode(cinfo, st)) != 0) {
st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
while (arith_decode(cinfo, st)) { while (arith_decode(cinfo, st)) {
if ((m <<= 1) == 0x8000) { if ((m <<= 1) == (int) 0x8000U) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ entropy->ct = -1; /* magnitude overflow */
return TRUE; return TRUE;
@ -612,7 +613,7 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
st = entropy->ac_stats[tbl] + st = entropy->ac_stats[tbl] +
(k <= cinfo->arith_ac_K[tbl] ? 189 : 217); (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
while (arith_decode(cinfo, st)) { while (arith_decode(cinfo, st)) {
if ((m <<= 1) == 0x8000) { if ((m <<= 1) == (int) 0x8000U) {
WARNMS(cinfo, JWRN_ARITH_BAD_CODE); WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
entropy->ct = -1; /* magnitude overflow */ entropy->ct = -1; /* magnitude overflow */
return TRUE; return TRUE;
@ -766,9 +767,8 @@ jinit_arith_decoder (j_decompress_ptr cinfo)
arith_entropy_ptr entropy; arith_entropy_ptr entropy;
int i; int i;
entropy = (arith_entropy_ptr) entropy = (arith_entropy_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(arith_entropy_decoder));
SIZEOF(arith_entropy_decoder));
cinfo->entropy = &entropy->pub; cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass; entropy->pub.start_pass = start_pass;
entropy->pub.finish_pass = finish_pass; entropy->pub.finish_pass = finish_pass;
@ -785,9 +785,9 @@ jinit_arith_decoder (j_decompress_ptr cinfo)
if (cinfo->progressive_mode) { if (cinfo->progressive_mode) {
/* Create progression status table */ /* Create progression status table */
int *coef_bit_ptr, ci; int *coef_bit_ptr, ci;
cinfo->coef_bits = (int (*)[DCTSIZE2]) cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE,
cinfo->num_components*DCTSIZE2*SIZEOF(int)); cinfo->num_components * DCTSIZE2 * SIZEOF(int));
coef_bit_ptr = & cinfo->coef_bits[0][0]; coef_bit_ptr = & cinfo->coef_bits[0][0];
for (ci = 0; ci < cinfo->num_components; ci++) for (ci = 0; ci < cinfo->num_components; ci++)
for (i = 0; i < DCTSIZE2; i++) for (i = 0; i < DCTSIZE2; i++)

@ -2,7 +2,7 @@
* jdatadst.c * jdatadst.c
* *
* Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 1994-1996, Thomas G. Lane.
* Modified 2009-2017 by Guido Vollbeding. * Modified 2009-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -46,7 +46,7 @@ typedef struct {
struct jpeg_destination_mgr pub; /* public fields */ struct jpeg_destination_mgr pub; /* public fields */
unsigned char ** outbuffer; /* target buffer */ unsigned char ** outbuffer; /* target buffer */
unsigned long * outsize; size_t * outsize;
unsigned char * newbuffer; /* newly allocated buffer */ unsigned char * newbuffer; /* newly allocated buffer */
JOCTET * buffer; /* start of buffer */ JOCTET * buffer; /* start of buffer */
size_t bufsize; size_t bufsize;
@ -66,9 +66,8 @@ init_destination (j_compress_ptr cinfo)
my_dest_ptr dest = (my_dest_ptr) cinfo->dest; my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */ /* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET *) dest->buffer = (JOCTET *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
dest->pub.next_output_byte = dest->buffer; dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
@ -131,7 +130,7 @@ empty_mem_output_buffer (j_compress_ptr cinfo)
nextbuffer = (JOCTET *) malloc(nextsize); nextbuffer = (JOCTET *) malloc(nextsize);
if (nextbuffer == NULL) if (nextbuffer == NULL)
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11);
MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
@ -204,9 +203,8 @@ jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
* sizes may be different. Caveat programmer. * sizes may be different. Caveat programmer.
*/ */
if (cinfo->dest == NULL) { /* first time for this JPEG object? */ if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *) cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_destination_mgr));
SIZEOF(my_destination_mgr));
} }
dest = (my_dest_ptr) cinfo->dest; dest = (my_dest_ptr) cinfo->dest;
@ -233,7 +231,7 @@ jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile)
GLOBAL(void) GLOBAL(void)
jpeg_mem_dest (j_compress_ptr cinfo, jpeg_mem_dest (j_compress_ptr cinfo,
unsigned char ** outbuffer, unsigned long * outsize) unsigned char ** outbuffer, size_t * outsize)
{ {
my_mem_dest_ptr dest; my_mem_dest_ptr dest;
@ -244,9 +242,8 @@ jpeg_mem_dest (j_compress_ptr cinfo,
* can be written to the same buffer without re-executing jpeg_mem_dest. * can be written to the same buffer without re-executing jpeg_mem_dest.
*/ */
if (cinfo->dest == NULL) { /* first time for this JPEG object? */ if (cinfo->dest == NULL) { /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *) cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_mem_destination_mgr));
SIZEOF(my_mem_destination_mgr));
} }
dest = (my_mem_dest_ptr) cinfo->dest; dest = (my_mem_dest_ptr) cinfo->dest;

@ -2,7 +2,7 @@
* jdatasrc.c * jdatasrc.c
* *
* Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 1994-1996, Thomas G. Lane.
* Modified 2009-2015 by Guido Vollbeding. * Modified 2009-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -156,21 +156,23 @@ METHODDEF(void)
skip_input_data (j_decompress_ptr cinfo, long num_bytes) skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{ {
struct jpeg_source_mgr * src = cinfo->src; struct jpeg_source_mgr * src = cinfo->src;
size_t nbytes;
/* Just a dumb implementation for now. Could use fseek() except /* Just a dumb implementation for now. Could use fseek() except
* it doesn't work on pipes. Not clear that being smart is worth * it doesn't work on pipes. Not clear that being smart is worth
* any trouble anyway --- large skips are infrequent. * any trouble anyway --- large skips are infrequent.
*/ */
if (num_bytes > 0) { if (num_bytes > 0) {
while (num_bytes > (long) src->bytes_in_buffer) { nbytes = (size_t) num_bytes;
num_bytes -= (long) src->bytes_in_buffer; while (nbytes > src->bytes_in_buffer) {
nbytes -= src->bytes_in_buffer;
(void) (*src->fill_input_buffer) (cinfo); (void) (*src->fill_input_buffer) (cinfo);
/* note we assume that fill_input_buffer will never return FALSE, /* note we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled. * so suspension need not be handled.
*/ */
} }
src->next_input_byte += (size_t) num_bytes; src->next_input_byte += nbytes;
src->bytes_in_buffer -= (size_t) num_bytes; src->bytes_in_buffer -= nbytes;
} }
} }
@ -219,13 +221,11 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
* manager serially with the same JPEG object. Caveat programmer. * manager serially with the same JPEG object. Caveat programmer.
*/ */
if (cinfo->src == NULL) { /* first time for this JPEG object? */ if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *) cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_source_mgr));
SIZEOF(my_source_mgr));
src = (my_src_ptr) cinfo->src; src = (my_src_ptr) cinfo->src;
src->buffer = (JOCTET *) src->buffer = (JOCTET *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * SIZEOF(JOCTET));
INPUT_BUF_SIZE * SIZEOF(JOCTET));
} }
src = (my_src_ptr) cinfo->src; src = (my_src_ptr) cinfo->src;
@ -247,7 +247,7 @@ jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
GLOBAL(void) GLOBAL(void)
jpeg_mem_src (j_decompress_ptr cinfo, jpeg_mem_src (j_decompress_ptr cinfo,
const unsigned char * inbuffer, unsigned long insize) const unsigned char * inbuffer, size_t insize)
{ {
struct jpeg_source_mgr * src; struct jpeg_source_mgr * src;
@ -259,9 +259,8 @@ jpeg_mem_src (j_decompress_ptr cinfo,
* the first one. * the first one.
*/ */
if (cinfo->src == NULL) { /* first time for this JPEG object? */ if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = (struct jpeg_source_mgr *) cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(struct jpeg_source_mgr));
SIZEOF(struct jpeg_source_mgr));
} }
src = cinfo->src; src = cinfo->src;
@ -270,6 +269,6 @@ jpeg_mem_src (j_decompress_ptr cinfo,
src->skip_input_data = skip_input_data; src->skip_input_data = skip_input_data;
src->resync_to_restart = jpeg_resync_to_restart; /* use default method */ src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->term_source = term_source; src->term_source = term_source;
src->bytes_in_buffer = (size_t) insize; src->bytes_in_buffer = insize;
src->next_input_byte = (const JOCTET *) inbuffer; src->next_input_byte = (const JOCTET *) inbuffer;
} }

@ -2,7 +2,7 @@
* jdcolor.c * jdcolor.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2011-2017 by Guido Vollbeding. * Modified 2011-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -124,28 +124,22 @@ build_ycc_rgb_table (j_decompress_ptr cinfo)
INT32 x; INT32 x;
SHIFT_TEMPS SHIFT_TEMPS
cconvert->Cr_r_tab = (int *) cconvert->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(MAXJSAMPLE+1) * SIZEOF(int)); cconvert->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
cconvert->Cb_b_tab = (int *) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cconvert->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(MAXJSAMPLE+1) * SIZEOF(int)); ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
cconvert->Cr_g_tab = (INT32 *) cconvert->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
(MAXJSAMPLE+1) * SIZEOF(INT32));
cconvert->Cb_g_tab = (INT32 *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32));
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
/* Cr=>R value is nearest int to 1.402 * x */ /* Cr=>R value is nearest int to 1.402 * x */
cconvert->Cr_r_tab[i] = (int) cconvert->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
/* Cb=>B value is nearest int to 1.772 * x */ /* Cb=>B value is nearest int to 1.772 * x */
cconvert->Cb_b_tab[i] = (int) cconvert->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
/* Cr=>G value is scaled-up -0.714136286 * x */ /* Cr=>G value is scaled-up -0.714136286 * x */
cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x; cconvert->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
/* Cb=>G value is scaled-up -0.344136286 * x */ /* Cb=>G value is scaled-up -0.344136286 * x */
@ -164,28 +158,22 @@ build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
INT32 x; INT32 x;
SHIFT_TEMPS SHIFT_TEMPS
cconvert->Cr_r_tab = (int *) cconvert->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(MAXJSAMPLE+1) * SIZEOF(int)); cconvert->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
cconvert->Cb_b_tab = (int *) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, cconvert->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(MAXJSAMPLE+1) * SIZEOF(int)); ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
cconvert->Cr_g_tab = (INT32 *) cconvert->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
(MAXJSAMPLE+1) * SIZEOF(INT32));
cconvert->Cb_g_tab = (INT32 *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32));
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
/* Cr=>R value is nearest int to 2.804 * x */ /* Cr=>R value is nearest int to 2.804 * x */
cconvert->Cr_r_tab[i] = (int) cconvert->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
/* Cb=>B value is nearest int to 3.544 * x */ /* Cb=>B value is nearest int to 3.544 * x */
cconvert->Cb_b_tab[i] = (int) cconvert->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
/* Cr=>G value is scaled-up -1.428272572 * x */ /* Cr=>G value is scaled-up -1.428272572 * x */
cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x; cconvert->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
/* Cb=>G value is scaled-up -0.688272572 * x */ /* Cb=>G value is scaled-up -0.688272572 * x */
@ -201,6 +189,7 @@ build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
* Note that we change from noninterleaved, one-plane-per-component format * Note that we change from noninterleaved, one-plane-per-component format
* to interleaved-pixel format. The output buffer is therefore three times * to interleaved-pixel format. The output buffer is therefore three times
* as wide as the input buffer. * as wide as the input buffer.
*
* A starting row offset is provided only for the input buffer. The caller * A starting row offset is provided only for the input buffer. The caller
* can easily adjust the passed output_buf value to accommodate any row * can easily adjust the passed output_buf value to accommodate any row
* offset required on that side. * offset required on that side.
@ -264,9 +253,8 @@ build_rgb_y_table (j_decompress_ptr cinfo)
INT32 i; INT32 i;
/* Allocate and fill in the conversion tables. */ /* Allocate and fill in the conversion tables. */
cconvert->rgb_y_tab = rgb_y_tab = (INT32 *) cconvert->rgb_y_tab = rgb_y_tab = (INT32 *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, TABLE_SIZE * SIZEOF(INT32));
(TABLE_SIZE * SIZEOF(INT32)));
for (i = 0; i <= MAXJSAMPLE; i++) { for (i = 0; i <= MAXJSAMPLE; i++) {
rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i; rgb_y_tab[i+R_Y_OFF] = FIX(0.299) * i;
@ -286,8 +274,8 @@ rgb_gray_convert (j_decompress_ptr cinfo,
JSAMPARRAY output_buf, int num_rows) JSAMPARRAY output_buf, int num_rows)
{ {
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
register INT32 * ctab = cconvert->rgb_y_tab;
register int r, g, b; register int r, g, b;
register INT32 * ctab = cconvert->rgb_y_tab;
register JSAMPROW outptr; register JSAMPROW outptr;
register JSAMPROW inptr0, inptr1, inptr2; register JSAMPROW inptr0, inptr1, inptr2;
register JDIMENSION col; register JDIMENSION col;
@ -313,6 +301,7 @@ rgb_gray_convert (j_decompress_ptr cinfo,
/* /*
* Convert some rows of samples to the output colorspace.
* [R-G,G,B-G] to [R,G,B] conversion with modulo calculation * [R-G,G,B-G] to [R,G,B] conversion with modulo calculation
* (inverse color transform). * (inverse color transform).
* This can be seen as an adaption of the general YCbCr->RGB * This can be seen as an adaption of the general YCbCr->RGB
@ -364,8 +353,8 @@ rgb1_gray_convert (j_decompress_ptr cinfo,
JSAMPARRAY output_buf, int num_rows) JSAMPARRAY output_buf, int num_rows)
{ {
my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert;
register INT32 * ctab = cconvert->rgb_y_tab;
register int r, g, b; register int r, g, b;
register INT32 * ctab = cconvert->rgb_y_tab;
register JSAMPROW outptr; register JSAMPROW outptr;
register JSAMPROW inptr0, inptr1, inptr2; register JSAMPROW inptr0, inptr1, inptr2;
register JDIMENSION col; register JDIMENSION col;
@ -396,6 +385,7 @@ rgb1_gray_convert (j_decompress_ptr cinfo,
/* /*
* Convert some rows of samples to the output colorspace.
* No colorspace change, but conversion from separate-planes * No colorspace change, but conversion from separate-planes
* to interleaved representation. * to interleaved representation.
*/ */
@ -430,6 +420,7 @@ rgb_convert (j_decompress_ptr cinfo,
/* /*
* Color conversion for no colorspace change: just copy the data, * Color conversion for no colorspace change: just copy the data,
* converting from separate-planes to interleaved representation. * converting from separate-planes to interleaved representation.
* We assume out_color_components == num_components.
*/ */
METHODDEF(void) METHODDEF(void)
@ -437,20 +428,21 @@ null_convert (j_decompress_ptr cinfo,
JSAMPIMAGE input_buf, JDIMENSION input_row, JSAMPIMAGE input_buf, JDIMENSION input_row,
JSAMPARRAY output_buf, int num_rows) JSAMPARRAY output_buf, int num_rows)
{ {
int ci;
register int nc = cinfo->num_components;
register JSAMPROW outptr; register JSAMPROW outptr;
register JSAMPROW inptr; register JSAMPROW inptr;
register JDIMENSION col; register JDIMENSION count;
register int num_comps = cinfo->num_components;
JDIMENSION num_cols = cinfo->output_width; JDIMENSION num_cols = cinfo->output_width;
int ci;
while (--num_rows >= 0) { while (--num_rows >= 0) {
for (ci = 0; ci < nc; ci++) { /* It seems fastest to make a separate pass for each component. */
for (ci = 0; ci < num_comps; ci++) {
inptr = input_buf[ci][input_row]; inptr = input_buf[ci][input_row];
outptr = output_buf[0] + ci; outptr = output_buf[0] + ci;
for (col = 0; col < num_cols; col++) { for (count = num_cols; count > 0; count--) {
*outptr = *inptr++; /* needn't bother with GETJSAMPLE() here */ *outptr = *inptr++; /* don't need GETJSAMPLE() here */
outptr += nc; outptr += num_comps;
} }
} }
input_row++; input_row++;
@ -504,9 +496,10 @@ gray_rgb_convert (j_decompress_ptr cinfo,
/* /*
* Adobe-style YCCK->CMYK conversion. * Convert some rows of samples to the output colorspace.
* We convert YCbCr to R=1-C, G=1-M, and B=1-Y using the same * This version handles Adobe-style YCCK->CMYK conversion,
* conversion as above, while passing K (black) unchanged. * where we convert YCbCr to R=1-C, G=1-M, and B=1-Y using the
* same conversion as above, while passing K (black) unchanged.
* We assume build_ycc_rgb_table has been called. * We assume build_ycc_rgb_table has been called.
*/ */
@ -577,9 +570,8 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
my_cconvert_ptr cconvert; my_cconvert_ptr cconvert;
int ci; int ci;
cconvert = (my_cconvert_ptr) cconvert = (my_cconvert_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_color_deconverter));
SIZEOF(my_color_deconverter));
cinfo->cconvert = &cconvert->pub; cinfo->cconvert = &cconvert->pub;
cconvert->pub.start_pass = start_pass_dcolor; cconvert->pub.start_pass = start_pass_dcolor;
@ -607,7 +599,6 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
default: /* JCS_UNKNOWN can be anything */ default: /* JCS_UNKNOWN can be anything */
if (cinfo->num_components < 1) if (cinfo->num_components < 1)
ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); ERREXIT(cinfo, JERR_BAD_J_COLORSPACE);
break;
} }
/* Support color transform only for RGB colorspaces */ /* Support color transform only for RGB colorspaces */
@ -684,7 +675,8 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
case JCS_BG_RGB: case JCS_BG_RGB:
cinfo->out_color_components = RGB_PIXELSIZE; cinfo->out_color_components = RGB_PIXELSIZE;
if (cinfo->jpeg_color_space == JCS_BG_RGB) { if (cinfo->jpeg_color_space != JCS_BG_RGB)
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
switch (cinfo->color_transform) { switch (cinfo->color_transform) {
case JCT_NONE: case JCT_NONE:
cconvert->pub.color_convert = rgb_convert; cconvert->pub.color_convert = rgb_convert;
@ -695,8 +687,6 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
default: default:
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
} }
} else
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
break; break;
case JCS_CMYK: case JCS_CMYK:
@ -714,14 +704,12 @@ jinit_color_deconverter (j_decompress_ptr cinfo)
} }
break; break;
default: default: /* permit null conversion to same output space */
/* Permit null conversion to same output space */ if (cinfo->out_color_space != cinfo->jpeg_color_space)
if (cinfo->out_color_space == cinfo->jpeg_color_space) { /* unsupported non-null conversion */
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
cinfo->out_color_components = cinfo->num_components; cinfo->out_color_components = cinfo->num_components;
cconvert->pub.color_convert = null_convert; cconvert->pub.color_convert = null_convert;
} else /* unsupported non-null conversion */
ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
break;
} }
if (cinfo->quantize_colors) if (cinfo->quantize_colors)

@ -2,7 +2,7 @@
* jdct.h * jdct.h
* *
* Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 1994-1996, Thomas G. Lane.
* Modified 2002-2017 by Guido Vollbeding. * Modified 2002-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -358,13 +358,6 @@ EXTERN(void) jpeg_idct_1x2
#define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5)) #define FIX(x) ((INT32) ((x) * CONST_SCALE + 0.5))
/* Descale and correctly round an INT32 value that's scaled by N bits.
* We assume RIGHT_SHIFT rounds towards minus infinity, so adding
* the fudge factor is correct for either sign of X.
*/
#define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result. /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
* This macro is used only when the two inputs will actually be no more than * This macro is used only when the two inputs will actually be no more than
* 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a * 16 bits wide, so that a 16x16->32 bit multiply can be used instead of a

@ -2,7 +2,7 @@
* jdhuff.c * jdhuff.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2006-2016 by Guido Vollbeding. * Modified 2006-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -341,13 +341,12 @@ jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
htbl = htbl =
isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
if (htbl == NULL) if (htbl == NULL)
ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);
/* Allocate a workspace if we haven't already done so. */ /* Allocate a workspace if we haven't already done so. */
if (*pdtbl == NULL) if (*pdtbl == NULL)
*pdtbl = (d_derived_tbl *) *pdtbl = (d_derived_tbl *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(d_derived_tbl));
SIZEOF(d_derived_tbl));
dtbl = *pdtbl; dtbl = *pdtbl;
dtbl->pub = htbl; /* fill in back link */ dtbl->pub = htbl; /* fill in back link */
@ -730,7 +729,7 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (! entropy->insufficient_data) { if (! entropy->insufficient_data) {
/* Load up working state */ /* Load up working state */
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved); ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */ /* Outer loop handles each block in the MCU */
@ -759,11 +758,12 @@ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state); ASSIGN_STATE(entropy->saved, state);
} }
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -809,7 +809,7 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (EOBRUN) /* if it's a band of zeroes... */ if (EOBRUN) /* if it's a band of zeroes... */
EOBRUN--; /* ...process it now (we do nothing) */ EOBRUN--; /* ...process it now (we do nothing) */
else { else {
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
Se = cinfo->Se; Se = cinfo->Se;
Al = cinfo->Al; Al = cinfo->Al;
natural_order = cinfo->natural_order; natural_order = cinfo->natural_order;
@ -842,14 +842,15 @@ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
} }
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
} }
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -866,7 +867,8 @@ METHODDEF(boolean)
decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{ {
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
int p1, blkn; JCOEF p1;
int blkn;
BITREAD_STATE_VARS; BITREAD_STATE_VARS;
/* Process restart marker if needed; may have to suspend */ /* Process restart marker if needed; may have to suspend */
@ -881,7 +883,7 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
*/ */
/* Load up working state */ /* Load up working state */
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
@ -896,9 +898,10 @@ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -915,7 +918,8 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int s, k, r; register int s, k, r;
unsigned int EOBRUN; unsigned int EOBRUN;
int Se, p1, m1; int Se;
JCOEF p1, m1;
const int * natural_order; const int * natural_order;
JBLOCKROW block; JBLOCKROW block;
JCOEFPTR thiscoef; JCOEFPTR thiscoef;
@ -937,11 +941,11 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Se = cinfo->Se; Se = cinfo->Se;
p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ m1 = -p1; /* -1 in the bit position being coded */
natural_order = cinfo->natural_order; natural_order = cinfo->natural_order;
/* Load up working state */ /* Load up working state */
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
/* There is always only one block per MCU */ /* There is always only one block per MCU */
@ -1043,11 +1047,12 @@ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
} }
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -1091,7 +1096,7 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
Se = cinfo->lim_Se; Se = cinfo->lim_Se;
/* Load up working state */ /* Load up working state */
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved); ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */ /* Outer loop handles each block in the MCU */
@ -1178,11 +1183,12 @@ decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state); ASSIGN_STATE(entropy->saved, state);
} }
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -1215,7 +1221,7 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
if (! entropy->insufficient_data) { if (! entropy->insufficient_data) {
/* Load up working state */ /* Load up working state */
BITREAD_LOAD_STATE(cinfo,entropy->bitstate); BITREAD_LOAD_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(state, entropy->saved); ASSIGN_STATE(state, entropy->saved);
/* Outer loop handles each block in the MCU */ /* Outer loop handles each block in the MCU */
@ -1302,11 +1308,12 @@ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
} }
/* Completed MCU, so update state */ /* Completed MCU, so update state */
BITREAD_SAVE_STATE(cinfo,entropy->bitstate); BITREAD_SAVE_STATE(cinfo, entropy->bitstate);
ASSIGN_STATE(entropy->saved, state); ASSIGN_STATE(entropy->saved, state);
} }
/* Account for restart interval (no-op if not using restarts) */ /* Account for restart interval if using restarts */
if (cinfo->restart_interval)
entropy->restarts_to_go--; entropy->restarts_to_go--;
return TRUE; return TRUE;
@ -1343,11 +1350,11 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
goto bad; goto bad;
} }
if (cinfo->Al > 13) { /* need not check for < 0 */ if (cinfo->Al > 13) { /* need not check for < 0 */
/* Arguably the maximum Al value should be less than 13 for 8-bit precision, /* Arguably the maximum Al value should be less than 13 for 8-bit
* but the spec doesn't say so, and we try to be liberal about what we * precision, but the spec doesn't say so, and we try to be liberal
* accept. Note: large Al values could result in out-of-range DC * about what we accept. Note: large Al values could result in
* coefficients during early scans, leading to bizarre displays due to * out-of-range DC coefficients during early scans, leading to bizarre
* overflows in the IDCT math. But we won't crash. * displays due to overflows in the IDCT math. But we won't crash.
*/ */
bad: bad:
ERREXIT4(cinfo, JERR_BAD_PROGRESSION, ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
@ -1451,7 +1458,8 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
compptr = cinfo->cur_comp_info[ci]; compptr = cinfo->cur_comp_info[ci];
/* Precalculate which table to use for each block */ /* Precalculate which table to use for each block */
entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; entropy->ac_cur_tbls[blkn] = /* AC needs no table when not present */
cinfo->lim_Se ? entropy->ac_derived_tbls[compptr->ac_tbl_no] : NULL;
/* Decide whether we really care about the coefficient values */ /* Decide whether we really care about the coefficient values */
if (compptr->component_needed) { if (compptr->component_needed) {
ci = compptr->DCT_v_scaled_size; ci = compptr->DCT_v_scaled_size;
@ -1494,7 +1502,6 @@ start_pass_huff_decoder (j_decompress_ptr cinfo)
if (ci <= 0 || ci > 8) ci = 8; if (ci <= 0 || ci > 8) ci = 8;
if (i <= 0 || i > 8) i = 8; if (i <= 0 || i > 8) i = 8;
entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1]; entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
break;
} }
} else { } else {
entropy->coef_limit[blkn] = 0; entropy->coef_limit[blkn] = 0;
@ -1522,9 +1529,8 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
huff_entropy_ptr entropy; huff_entropy_ptr entropy;
int i; int i;
entropy = (huff_entropy_ptr) entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_decoder));
SIZEOF(huff_entropy_decoder));
cinfo->entropy = &entropy->pub; cinfo->entropy = &entropy->pub;
entropy->pub.start_pass = start_pass_huff_decoder; entropy->pub.start_pass = start_pass_huff_decoder;
entropy->pub.finish_pass = finish_pass_huff; entropy->pub.finish_pass = finish_pass_huff;
@ -1532,9 +1538,9 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
if (cinfo->progressive_mode) { if (cinfo->progressive_mode) {
/* Create progression status table */ /* Create progression status table */
int *coef_bit_ptr, ci; int *coef_bit_ptr, ci;
cinfo->coef_bits = (int (*)[DCTSIZE2]) cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE,
cinfo->num_components*DCTSIZE2*SIZEOF(int)); cinfo->num_components * DCTSIZE2 * SIZEOF(int));
coef_bit_ptr = & cinfo->coef_bits[0][0]; coef_bit_ptr = & cinfo->coef_bits[0][0];
for (ci = 0; ci < cinfo->num_components; ci++) for (ci = 0; ci < cinfo->num_components; ci++)
for (i = 0; i < DCTSIZE2; i++) for (i = 0; i < DCTSIZE2; i++)
@ -1545,7 +1551,7 @@ jinit_huff_decoder (j_decompress_ptr cinfo)
entropy->derived_tbls[i] = NULL; entropy->derived_tbls[i] = NULL;
} }
} else { } else {
/* Mark tables unallocated */ /* Mark derived tables unallocated */
for (i = 0; i < NUM_HUFF_TBLS; i++) { for (i = 0; i < NUM_HUFF_TBLS; i++) {
entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
} }

@ -2,7 +2,7 @@
* jdmarker.c * jdmarker.c
* *
* Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 1991-1998, Thomas G. Lane.
* Modified 2009-2013 by Guido Vollbeding. * Modified 2009-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -496,8 +496,6 @@ get_dht (j_decompress_ptr cinfo)
if (count > 256 || ((INT32) count) > length) if (count > 256 || ((INT32) count) > length)
ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
MEMZERO(huffval, SIZEOF(huffval)); /* pre-zero array for later copy */
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
INPUT_BYTE(cinfo, huffval[i], return FALSE); INPUT_BYTE(cinfo, huffval[i], return FALSE);
@ -517,7 +515,8 @@ get_dht (j_decompress_ptr cinfo)
*htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo); *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits)); MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval)); if (count > 0)
MEMCOPY((*htblptr)->huffval, huffval, count * SIZEOF(UINT8));
} }
if (length != 0) if (length != 0)
@ -577,14 +576,14 @@ get_dqt (j_decompress_ptr cinfo)
count = DCTSIZE2; count = DCTSIZE2;
} }
switch (count) { switch ((int) count) {
case (2*2): natural_order = jpeg_natural_order2; break; case (2*2): natural_order = jpeg_natural_order2; break;
case (3*3): natural_order = jpeg_natural_order3; break; case (3*3): natural_order = jpeg_natural_order3; break;
case (4*4): natural_order = jpeg_natural_order4; break; case (4*4): natural_order = jpeg_natural_order4; break;
case (5*5): natural_order = jpeg_natural_order5; break; case (5*5): natural_order = jpeg_natural_order5; break;
case (6*6): natural_order = jpeg_natural_order6; break; case (6*6): natural_order = jpeg_natural_order6; break;
case (7*7): natural_order = jpeg_natural_order7; break; case (7*7): natural_order = jpeg_natural_order7; break;
default: natural_order = jpeg_natural_order; break; default: natural_order = jpeg_natural_order;
} }
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
@ -784,7 +783,6 @@ examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
default: default:
TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION, TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
GETJOCTET(data[5]), (int) totallen); GETJOCTET(data[5]), (int) totallen);
break;
} }
} else { } else {
/* Start of APP0 does not match "JFIF" or "JFXX", or too short */ /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
@ -858,7 +856,6 @@ get_interesting_appn (j_decompress_ptr cinfo)
default: default:
/* can't get here unless jpeg_save_markers chooses wrong processor */ /* can't get here unless jpeg_save_markers chooses wrong processor */
ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
break;
} }
/* skip any remaining data -- could be lots */ /* skip any remaining data -- could be lots */
@ -964,7 +961,6 @@ save_marker (j_decompress_ptr cinfo)
default: default:
TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
(int) (data_length + length)); (int) (data_length + length));
break;
} }
/* skip any remaining data -- could be lots */ /* skip any remaining data -- could be lots */
@ -1240,7 +1236,6 @@ read_markers (j_decompress_ptr cinfo)
* ought to change! * ought to change!
*/ */
ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker); ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
break;
} }
/* Successfully processed marker, so reset state variable */ /* Successfully processed marker, so reset state variable */
cinfo->unread_marker = 0; cinfo->unread_marker = 0;
@ -1416,9 +1411,8 @@ jinit_marker_reader (j_decompress_ptr cinfo)
int i; int i;
/* Create subobject in permanent pool */ /* Create subobject in permanent pool */
marker = (my_marker_ptr) marker = (my_marker_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(my_marker_reader));
SIZEOF(my_marker_reader));
cinfo->marker = &marker->pub; cinfo->marker = &marker->pub;
/* Initialize public method pointers */ /* Initialize public method pointers */
marker->pub.reset_marker_reader = reset_marker_reader; marker->pub.reset_marker_reader = reset_marker_reader;

@ -2,7 +2,7 @@
* jdmaster.c * jdmaster.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2002-2017 by Guido Vollbeding. * Modified 2002-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -104,7 +104,7 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
*/ */
{ {
#ifdef IDCT_SCALING_SUPPORTED #ifdef IDCT_SCALING_SUPPORTED
int ci; int ci, ssize;
jpeg_component_info *compptr; jpeg_component_info *compptr;
#endif #endif
@ -124,17 +124,21 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
*/ */
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) { ci++, compptr++) {
int ssize = 1; ssize = 1;
if (! cinfo->raw_data_out)
while (cinfo->min_DCT_h_scaled_size * ssize <= while (cinfo->min_DCT_h_scaled_size * ssize <=
(cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
(cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==
0) {
ssize = ssize * 2; ssize = ssize * 2;
} }
compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
ssize = 1; ssize = 1;
if (! cinfo->raw_data_out)
while (cinfo->min_DCT_v_scaled_size * ssize <= while (cinfo->min_DCT_v_scaled_size * ssize <=
(cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) && (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
(cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==
0) {
ssize = ssize * 2; ssize = ssize * 2;
} }
compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
@ -144,13 +148,10 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
}
/* Recompute downsampled dimensions of components; /* Recompute downsampled dimensions of components;
* application needs to know these if using raw downsampled data. * application needs to know these if using raw downsampled data.
*/ */
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
/* Size in samples, after IDCT scaling */ /* Size in samples, after IDCT scaling */
compptr->downsampled_width = (JDIMENSION) compptr->downsampled_width = (JDIMENSION)
jdiv_round_up((long) cinfo->image_width * jdiv_round_up((long) cinfo->image_width *
@ -172,8 +173,10 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
break; break;
case JCS_RGB: case JCS_RGB:
case JCS_BG_RGB: case JCS_BG_RGB:
#if RGB_PIXELSIZE != 3
cinfo->out_color_components = RGB_PIXELSIZE; cinfo->out_color_components = RGB_PIXELSIZE;
break; break;
#endif /* else share code with YCbCr */
case JCS_YCbCr: case JCS_YCbCr:
case JCS_BG_YCC: case JCS_BG_YCC:
cinfo->out_color_components = 3; cinfo->out_color_components = 3;
@ -184,7 +187,6 @@ jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
break; break;
default: /* else must be same colorspace as in file */ default: /* else must be same colorspace as in file */
cinfo->out_color_components = cinfo->num_components; cinfo->out_color_components = cinfo->num_components;
break;
} }
cinfo->output_components = (cinfo->quantize_colors ? 1 : cinfo->output_components = (cinfo->quantize_colors ? 1 :
cinfo->out_color_components); cinfo->out_color_components);
@ -525,9 +527,8 @@ jinit_master_decompress (j_decompress_ptr cinfo)
{ {
my_master_ptr master; my_master_ptr master;
master = (my_master_ptr) master = (my_master_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_decomp_master));
SIZEOF(my_decomp_master));
cinfo->master = &master->pub; cinfo->master = &master->pub;
master->pub.prepare_for_output_pass = prepare_for_output_pass; master->pub.prepare_for_output_pass = prepare_for_output_pass;
master->pub.finish_output_pass = finish_output_pass; master->pub.finish_output_pass = finish_output_pass;

@ -2,7 +2,7 @@
* jdmerge.c * jdmerge.c
* *
* Copyright (C) 1994-1996, Thomas G. Lane. * Copyright (C) 1994-1996, Thomas G. Lane.
* Modified 2013-2017 by Guido Vollbeding. * Modified 2013-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -95,28 +95,22 @@ build_ycc_rgb_table (j_decompress_ptr cinfo)
INT32 x; INT32 x;
SHIFT_TEMPS SHIFT_TEMPS
upsample->Cr_r_tab = (int *) upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(MAXJSAMPLE+1) * SIZEOF(int)); upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
upsample->Cb_b_tab = (int *) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(MAXJSAMPLE+1) * SIZEOF(int)); ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
upsample->Cr_g_tab = (INT32 *) upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
(MAXJSAMPLE+1) * SIZEOF(INT32));
upsample->Cb_g_tab = (INT32 *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32));
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
/* Cr=>R value is nearest int to 1.402 * x */ /* Cr=>R value is nearest int to 1.402 * x */
upsample->Cr_r_tab[i] = (int) upsample->Cr_r_tab[i] = (int) DESCALE(FIX(1.402) * x, SCALEBITS);
RIGHT_SHIFT(FIX(1.402) * x + ONE_HALF, SCALEBITS);
/* Cb=>B value is nearest int to 1.772 * x */ /* Cb=>B value is nearest int to 1.772 * x */
upsample->Cb_b_tab[i] = (int) upsample->Cb_b_tab[i] = (int) DESCALE(FIX(1.772) * x, SCALEBITS);
RIGHT_SHIFT(FIX(1.772) * x + ONE_HALF, SCALEBITS);
/* Cr=>G value is scaled-up -0.714136286 * x */ /* Cr=>G value is scaled-up -0.714136286 * x */
upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x; upsample->Cr_g_tab[i] = (- FIX(0.714136286)) * x;
/* Cb=>G value is scaled-up -0.344136286 * x */ /* Cb=>G value is scaled-up -0.344136286 * x */
@ -135,28 +129,22 @@ build_bg_ycc_rgb_table (j_decompress_ptr cinfo)
INT32 x; INT32 x;
SHIFT_TEMPS SHIFT_TEMPS
upsample->Cr_r_tab = (int *) upsample->Cr_r_tab = (int *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(MAXJSAMPLE+1) * SIZEOF(int)); upsample->Cb_b_tab = (int *) (*cinfo->mem->alloc_small)
upsample->Cb_b_tab = (int *) ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(int));
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, upsample->Cr_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(MAXJSAMPLE+1) * SIZEOF(int)); ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
upsample->Cr_g_tab = (INT32 *) upsample->Cb_g_tab = (INT32 *) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE+1) * SIZEOF(INT32));
(MAXJSAMPLE+1) * SIZEOF(INT32));
upsample->Cb_g_tab = (INT32 *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
(MAXJSAMPLE+1) * SIZEOF(INT32));
for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) { for (i = 0, x = -CENTERJSAMPLE; i <= MAXJSAMPLE; i++, x++) {
/* i is the actual input pixel value, in the range 0..MAXJSAMPLE */ /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
/* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */ /* The Cb or Cr value we are thinking of is x = i - CENTERJSAMPLE */
/* Cr=>R value is nearest int to 2.804 * x */ /* Cr=>R value is nearest int to 2.804 * x */
upsample->Cr_r_tab[i] = (int) upsample->Cr_r_tab[i] = (int) DESCALE(FIX(2.804) * x, SCALEBITS);
RIGHT_SHIFT(FIX(2.804) * x + ONE_HALF, SCALEBITS);
/* Cb=>B value is nearest int to 3.544 * x */ /* Cb=>B value is nearest int to 3.544 * x */
upsample->Cb_b_tab[i] = (int) upsample->Cb_b_tab[i] = (int) DESCALE(FIX(3.544) * x, SCALEBITS);
RIGHT_SHIFT(FIX(3.544) * x + ONE_HALF, SCALEBITS);
/* Cr=>G value is scaled-up -1.428272572 * x */ /* Cr=>G value is scaled-up -1.428272572 * x */
upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x; upsample->Cr_g_tab[i] = (- FIX(1.428272572)) * x;
/* Cb=>G value is scaled-up -0.688272572 * x */ /* Cb=>G value is scaled-up -0.688272572 * x */
@ -419,9 +407,8 @@ jinit_merged_upsampler (j_decompress_ptr cinfo)
{ {
my_upsample_ptr upsample; my_upsample_ptr upsample;
upsample = (my_upsample_ptr) upsample = (my_upsample_ptr) (*cinfo->mem->alloc_small)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_upsampler));
SIZEOF(my_upsampler));
cinfo->upsample = &upsample->pub; cinfo->upsample = &upsample->pub;
upsample->pub.start_pass = start_pass_merged_upsample; upsample->pub.start_pass = start_pass_merged_upsample;
upsample->pub.need_context_rows = FALSE; upsample->pub.need_context_rows = FALSE;
@ -432,9 +419,9 @@ jinit_merged_upsampler (j_decompress_ptr cinfo)
upsample->pub.upsample = merged_2v_upsample; upsample->pub.upsample = merged_2v_upsample;
upsample->upmethod = h2v2_merged_upsample; upsample->upmethod = h2v2_merged_upsample;
/* Allocate a spare row buffer */ /* Allocate a spare row buffer */
upsample->spare_row = (JSAMPROW) upsample->spare_row = (JSAMPROW) (*cinfo->mem->alloc_large)
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, ((j_common_ptr) cinfo, JPOOL_IMAGE,
(size_t) (upsample->out_row_width * SIZEOF(JSAMPLE))); (size_t) upsample->out_row_width * SIZEOF(JSAMPLE));
} else { } else {
upsample->pub.upsample = merged_1v_upsample; upsample->pub.upsample = merged_1v_upsample;
upsample->upmethod = h2v1_merged_upsample; upsample->upmethod = h2v1_merged_upsample;

@ -2,7 +2,7 @@
* jerror.h * jerror.h
* *
* Copyright (C) 1994-1997, Thomas G. Lane. * Copyright (C) 1994-1997, Thomas G. Lane.
* Modified 1997-2012 by Guido Vollbeding. * Modified 1997-2018 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -84,7 +84,7 @@ JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
JMESSAGE(JERR_FILE_READ, "Input file read error") JMESSAGE(JERR_FILE_READ, "Input file read error")
JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?") JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet") JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow") JMESSAGE(JERR_HUFF_CLEN_OUTOFBOUNDS, "Huffman code size table out of bounds")
JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry") JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels") JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
JMESSAGE(JERR_INPUT_EMPTY, "Empty input file") JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")

@ -2,7 +2,7 @@
* jfdctint.c * jfdctint.c
* *
* Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 1991-1996, Thomas G. Lane.
* Modification developed 2003-2015 by Guido Vollbeding. * Modification developed 2003-2018 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -3261,78 +3261,84 @@ jpeg_fdct_6x3 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
GLOBAL(void) GLOBAL(void)
jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col) jpeg_fdct_4x2 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
{ {
INT32 tmp0, tmp1; DCTELEM tmp0, tmp2, tmp10, tmp12, tmp4, tmp5;
INT32 tmp10, tmp11; INT32 tmp1, tmp3, tmp11, tmp13;
DCTELEM *dataptr; INT32 z1, z2, z3;
JSAMPROW elemptr; JSAMPROW elemptr;
int ctr;
SHIFT_TEMPS SHIFT_TEMPS
/* Pre-zero output coefficient block. */ /* Pre-zero output coefficient block. */
MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2); MEMZERO(data, SIZEOF(DCTELEM) * DCTSIZE2);
/* Pass 1: process rows. /* Pass 1: process rows.
* Note results are scaled up by sqrt(8) compared to a true DCT; * Note results are scaled up by sqrt(8) compared to a true DCT.
* furthermore, we scale the results by 2**PASS1_BITS.
* We must also scale the output by (8/4)*(8/2) = 2**3, which we add here.
* 4-point FDCT kernel, * 4-point FDCT kernel,
* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
*/ */
dataptr = data; /* Row 0 */
for (ctr = 0; ctr < 2; ctr++) { elemptr = sample_data[0] + start_col;
elemptr = sample_data[ctr] + start_col;
/* Even part */ /* Even part */
tmp0 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]); tmp4 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]);
tmp1 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]); tmp5 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]);
tmp10 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
tmp11 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
/* Apply unsigned->signed conversion. */ tmp0 = tmp4 + tmp5;
dataptr[0] = (DCTELEM) tmp2 = tmp4 - tmp5;
((tmp0 + tmp1 - 4 * CENTERJSAMPLE) << (PASS1_BITS+3));
dataptr[2] = (DCTELEM) ((tmp0 - tmp1) << (PASS1_BITS+3));
/* Odd part */ /* Odd part */
tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ z2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
/* Add fudge factor here for final descale. */ z3 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
tmp0 += ONE << (CONST_BITS-PASS1_BITS-4);
dataptr[1] = (DCTELEM)
RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
CONST_BITS-PASS1_BITS-3);
dataptr[3] = (DCTELEM)
RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
CONST_BITS-PASS1_BITS-3);
dataptr += DCTSIZE; /* advance pointer to next row */ z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */
} /* Add fudge factor here for final descale. */
z1 += ONE << (CONST_BITS-3-1);
tmp1 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */
tmp3 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */
/* Pass 2: process columns. /* Row 1 */
* We remove the PASS1_BITS scaling, but leave the results scaled up elemptr = sample_data[1] + start_col;
* by an overall factor of 8.
*/
dataptr = data;
for (ctr = 0; ctr < 4; ctr++) {
/* Even part */ /* Even part */
/* Add fudge factor here for final descale. */ tmp4 = GETJSAMPLE(elemptr[0]) + GETJSAMPLE(elemptr[3]);
tmp0 = dataptr[DCTSIZE*0] + (ONE << (PASS1_BITS-1)); tmp5 = GETJSAMPLE(elemptr[1]) + GETJSAMPLE(elemptr[2]);
tmp1 = dataptr[DCTSIZE*1];
dataptr[DCTSIZE*0] = (DCTELEM) RIGHT_SHIFT(tmp0 + tmp1, PASS1_BITS); tmp10 = tmp4 + tmp5;
tmp12 = tmp4 - tmp5;
/* Odd part */ /* Odd part */
dataptr[DCTSIZE*1] = (DCTELEM) RIGHT_SHIFT(tmp0 - tmp1, PASS1_BITS); z2 = GETJSAMPLE(elemptr[0]) - GETJSAMPLE(elemptr[3]);
z3 = GETJSAMPLE(elemptr[1]) - GETJSAMPLE(elemptr[2]);
dataptr++; /* advance pointer to next column */ z1 = MULTIPLY(z2 + z3, FIX_0_541196100); /* c6 */
} tmp11 = z1 + MULTIPLY(z2, FIX_0_765366865); /* c2-c6 */
tmp13 = z1 - MULTIPLY(z3, FIX_1_847759065); /* c2+c6 */
/* Pass 2: process columns.
* We leave the results scaled up by an overall factor of 8.
* We must also scale the output by (8/4)*(8/2) = 2**3.
*/
/* Column 0 */
/* Apply unsigned->signed conversion. */
data[DCTSIZE*0] = (tmp0 + tmp10 - 8 * CENTERJSAMPLE) << 3;
data[DCTSIZE*1] = (tmp0 - tmp10) << 3;
/* Column 1 */
data[DCTSIZE*0+1] = (DCTELEM) RIGHT_SHIFT(tmp1 + tmp11, CONST_BITS-3);
data[DCTSIZE*1+1] = (DCTELEM) RIGHT_SHIFT(tmp1 - tmp11, CONST_BITS-3);
/* Column 2 */
data[DCTSIZE*0+2] = (tmp2 + tmp12) << 3;
data[DCTSIZE*1+2] = (tmp2 - tmp12) << 3;
/* Column 3 */
data[DCTSIZE*0+3] = (DCTELEM) RIGHT_SHIFT(tmp3 + tmp13, CONST_BITS-3);
data[DCTSIZE*1+3] = (DCTELEM) RIGHT_SHIFT(tmp3 - tmp13, CONST_BITS-3);
} }
@ -4312,7 +4318,6 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
/* Pass 1: process rows. /* Pass 1: process rows.
* Note results are scaled up by sqrt(8) compared to a true DCT. * Note results are scaled up by sqrt(8) compared to a true DCT.
* We must also scale the output by (8/2)*(8/4) = 2**3, which we add here.
*/ */
dataptr = data; dataptr = data;
@ -4325,17 +4330,18 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
tmp1 = GETJSAMPLE(elemptr[1]); tmp1 = GETJSAMPLE(elemptr[1]);
/* Apply unsigned->signed conversion. */ /* Apply unsigned->signed conversion. */
dataptr[0] = (DCTELEM) ((tmp0 + tmp1 - 2 * CENTERJSAMPLE) << 3); dataptr[0] = (DCTELEM) (tmp0 + tmp1 - 2 * CENTERJSAMPLE);
/* Odd part */ /* Odd part */
dataptr[1] = (DCTELEM) ((tmp0 - tmp1) << 3); dataptr[1] = (DCTELEM) (tmp0 - tmp1);
dataptr += DCTSIZE; /* advance pointer to next row */ dataptr += DCTSIZE; /* advance pointer to next row */
} }
/* Pass 2: process columns. /* Pass 2: process columns.
* We leave the results scaled up by an overall factor of 8. * We leave the results scaled up by an overall factor of 8.
* We must also scale the output by (8/2)*(8/4) = 2**3.
* 4-point FDCT kernel, * 4-point FDCT kernel,
* cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT]. * cK represents sqrt(2) * cos(K*pi/16) [refers to 8-point FDCT].
*/ */
@ -4350,21 +4356,21 @@ jpeg_fdct_2x4 (DCTELEM * data, JSAMPARRAY sample_data, JDIMENSION start_col)
tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3]; tmp10 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*3];
tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2]; tmp11 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*2];
dataptr[DCTSIZE*0] = (DCTELEM) (tmp0 + tmp1); dataptr[DCTSIZE*0] = (DCTELEM) ((tmp0 + tmp1) << 3);
dataptr[DCTSIZE*2] = (DCTELEM) (tmp0 - tmp1); dataptr[DCTSIZE*2] = (DCTELEM) ((tmp0 - tmp1) << 3);
/* Odd part */ /* Odd part */
tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */ tmp0 = MULTIPLY(tmp10 + tmp11, FIX_0_541196100); /* c6 */
/* Add fudge factor here for final descale. */ /* Add fudge factor here for final descale. */
tmp0 += ONE << (CONST_BITS-1); tmp0 += ONE << (CONST_BITS-3-1);
dataptr[DCTSIZE*1] = (DCTELEM) dataptr[DCTSIZE*1] = (DCTELEM)
RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */ RIGHT_SHIFT(tmp0 + MULTIPLY(tmp10, FIX_0_765366865), /* c2-c6 */
CONST_BITS); CONST_BITS-3);
dataptr[DCTSIZE*3] = (DCTELEM) dataptr[DCTSIZE*3] = (DCTELEM)
RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */ RIGHT_SHIFT(tmp0 - MULTIPLY(tmp11, FIX_1_847759065), /* c2+c6 */
CONST_BITS); CONST_BITS-3);
dataptr++; /* advance pointer to next column */ dataptr++; /* advance pointer to next column */
} }

@ -2,7 +2,7 @@
* jidctint.c * jidctint.c
* *
* Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 1991-1998, Thomas G. Lane.
* Modification developed 2002-2016 by Guido Vollbeding. * Modification developed 2002-2018 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -1474,7 +1474,7 @@ jpeg_idct_10x10 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* /*
* Perform dequantization and inverse DCT on one block of coefficients, * Perform dequantization and inverse DCT on one block of coefficients,
* producing a 11x11 output block. * producing an 11x11 output block.
* *
* Optimized algorithm with 24 multiplications in the 1-D kernel. * Optimized algorithm with 24 multiplications in the 1-D kernel.
* cK represents sqrt(2) * cos(K*pi/22). * cK represents sqrt(2) * cos(K*pi/22).
@ -3675,7 +3675,7 @@ jpeg_idct_10x5 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* /*
* Perform dequantization and inverse DCT on one block of coefficients, * Perform dequantization and inverse DCT on one block of coefficients,
* producing a 8x4 output block. * producing an 8x4 output block.
* *
* 4-point IDCT in pass 1 (columns), 8-point in pass 2 (rows). * 4-point IDCT in pass 1 (columns), 8-point in pass 2 (rows).
*/ */
@ -3835,7 +3835,7 @@ jpeg_idct_8x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* /*
* Perform dequantization and inverse DCT on one block of coefficients, * Perform dequantization and inverse DCT on one block of coefficients,
* producing a reduced-size 6x3 output block. * producing a 6x3 output block.
* *
* 3-point IDCT in pass 1 (columns), 6-point in pass 2 (rows). * 3-point IDCT in pass 1 (columns), 6-point in pass 2 (rows).
*/ */
@ -4082,7 +4082,7 @@ jpeg_idct_2x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* /*
* Perform dequantization and inverse DCT on one block of coefficients, * Perform dequantization and inverse DCT on one block of coefficients,
* producing a 8x16 output block. * producing an 8x16 output block.
* *
* 16-point IDCT in pass 1 (columns), 8-point in pass 2 (rows). * 16-point IDCT in pass 1 (columns), 8-point in pass 2 (rows).
*/ */
@ -5004,7 +5004,7 @@ jpeg_idct_4x8 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
/* /*
* Perform dequantization and inverse DCT on one block of coefficients, * Perform dequantization and inverse DCT on one block of coefficients,
* producing a reduced-size 3x6 output block. * producing a 3x6 output block.
* *
* 6-point IDCT in pass 1 (columns), 3-point in pass 2 (rows). * 6-point IDCT in pass 1 (columns), 3-point in pass 2 (rows).
*/ */

@ -2,7 +2,7 @@
* jmemmgr.c * jmemmgr.c
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 2011-2012 by Guido Vollbeding. * Modified 2011-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -130,7 +130,7 @@ typedef struct {
jvirt_barray_ptr virt_barray_list; jvirt_barray_ptr virt_barray_list;
/* This counts total space obtained from jpeg_get_small/large */ /* This counts total space obtained from jpeg_get_small/large */
long total_space_allocated; size_t total_space_allocated;
/* alloc_sarray and alloc_barray set this value for use by virtual /* alloc_sarray and alloc_barray set this value for use by virtual
* array routines. * array routines.
@ -195,7 +195,7 @@ print_mem_stats (j_common_ptr cinfo, int pool_id)
* This is helpful because message parm array can't handle longs. * This is helpful because message parm array can't handle longs.
*/ */
fprintf(stderr, "Freeing pool %d, total space = %ld\n", fprintf(stderr, "Freeing pool %d, total space = %ld\n",
pool_id, mem->total_space_allocated); pool_id, (long) mem->total_space_allocated);
for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL; for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
lhdr_ptr = lhdr_ptr->hdr.next) { lhdr_ptr = lhdr_ptr->hdr.next) {
@ -260,11 +260,11 @@ alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
{ {
my_mem_ptr mem = (my_mem_ptr) cinfo->mem; my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
small_pool_ptr hdr_ptr, prev_hdr_ptr; small_pool_ptr hdr_ptr, prev_hdr_ptr;
char * data_ptr;
size_t odd_bytes, min_request, slop; size_t odd_bytes, min_request, slop;
char * data_ptr;
/* Check for unsatisfiable request (do now to ensure no overflow below) */ /* Check for unsatisfiable request (do now to ensure no overflow below) */
if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr))) if (sizeofobject > (size_t) MAX_ALLOC_CHUNK - SIZEOF(small_pool_hdr))
out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
/* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
@ -293,8 +293,8 @@ alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
else else
slop = extra_pool_slop[pool_id]; slop = extra_pool_slop[pool_id];
/* Don't ask for more than MAX_ALLOC_CHUNK */ /* Don't ask for more than MAX_ALLOC_CHUNK */
if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request)) if (slop > (size_t) MAX_ALLOC_CHUNK - min_request)
slop = (size_t) (MAX_ALLOC_CHUNK-min_request); slop = (size_t) MAX_ALLOC_CHUNK - min_request;
/* Try to get space, if fail reduce slop and try again */ /* Try to get space, if fail reduce slop and try again */
for (;;) { for (;;) {
hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop); hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
@ -348,7 +348,7 @@ alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
size_t odd_bytes; size_t odd_bytes;
/* Check for unsatisfiable request (do now to ensure no overflow below) */ /* Check for unsatisfiable request (do now to ensure no overflow below) */
if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr))) if (sizeofobject > (size_t) MAX_ALLOC_CHUNK - SIZEOF(large_pool_hdr))
out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
/* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */ /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
@ -404,7 +404,7 @@ alloc_sarray (j_common_ptr cinfo, int pool_id,
long ltemp; long ltemp;
/* Calculate max # of rows allowed in one allocation chunk */ /* Calculate max # of rows allowed in one allocation chunk */
ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / ltemp = (MAX_ALLOC_CHUNK - SIZEOF(large_pool_hdr)) /
((long) samplesperrow * SIZEOF(JSAMPLE)); ((long) samplesperrow * SIZEOF(JSAMPLE));
if (ltemp <= 0) if (ltemp <= 0)
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
@ -416,15 +416,14 @@ alloc_sarray (j_common_ptr cinfo, int pool_id,
/* Get space for row pointers (small object) */ /* Get space for row pointers (small object) */
result = (JSAMPARRAY) alloc_small(cinfo, pool_id, result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
(size_t) (numrows * SIZEOF(JSAMPROW))); (size_t) numrows * SIZEOF(JSAMPROW));
/* Get the rows themselves (large objects) */ /* Get the rows themselves (large objects) */
currow = 0; currow = 0;
while (currow < numrows) { while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow); rowsperchunk = MIN(rowsperchunk, numrows - currow);
workspace = (JSAMPROW) alloc_large(cinfo, pool_id, workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow (size_t) rowsperchunk * (size_t) samplesperrow * SIZEOF(JSAMPLE));
* SIZEOF(JSAMPLE)));
for (i = rowsperchunk; i > 0; i--) { for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace; result[currow++] = workspace;
workspace += samplesperrow; workspace += samplesperrow;
@ -452,7 +451,7 @@ alloc_barray (j_common_ptr cinfo, int pool_id,
long ltemp; long ltemp;
/* Calculate max # of rows allowed in one allocation chunk */ /* Calculate max # of rows allowed in one allocation chunk */
ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / ltemp = (MAX_ALLOC_CHUNK - SIZEOF(large_pool_hdr)) /
((long) blocksperrow * SIZEOF(JBLOCK)); ((long) blocksperrow * SIZEOF(JBLOCK));
if (ltemp <= 0) if (ltemp <= 0)
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
@ -464,15 +463,14 @@ alloc_barray (j_common_ptr cinfo, int pool_id,
/* Get space for row pointers (small object) */ /* Get space for row pointers (small object) */
result = (JBLOCKARRAY) alloc_small(cinfo, pool_id, result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
(size_t) (numrows * SIZEOF(JBLOCKROW))); (size_t) numrows * SIZEOF(JBLOCKROW));
/* Get the rows themselves (large objects) */ /* Get the rows themselves (large objects) */
currow = 0; currow = 0;
while (currow < numrows) { while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow); rowsperchunk = MIN(rowsperchunk, numrows - currow);
workspace = (JBLOCKROW) alloc_large(cinfo, pool_id, workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
(size_t) ((size_t) rowsperchunk * (size_t) blocksperrow (size_t) rowsperchunk * (size_t) blocksperrow * SIZEOF(JBLOCK));
* SIZEOF(JBLOCK)));
for (i = rowsperchunk; i > 0; i--) { for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace; result[currow++] = workspace;
workspace += blocksperrow; workspace += blocksperrow;
@ -585,8 +583,8 @@ realize_virt_arrays (j_common_ptr cinfo)
/* Allocate the in-memory buffers for any unrealized virtual arrays */ /* Allocate the in-memory buffers for any unrealized virtual arrays */
{ {
my_mem_ptr mem = (my_mem_ptr) cinfo->mem; my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
long space_per_minheight, maximum_space, avail_mem; long bytesperrow, space_per_minheight, maximum_space;
long minheights, max_minheights; long avail_mem, minheights, max_minheights;
jvirt_sarray_ptr sptr; jvirt_sarray_ptr sptr;
jvirt_barray_ptr bptr; jvirt_barray_ptr bptr;
@ -598,18 +596,16 @@ realize_virt_arrays (j_common_ptr cinfo)
maximum_space = 0; maximum_space = 0;
for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
if (sptr->mem_buffer == NULL) { /* if not realized yet */ if (sptr->mem_buffer == NULL) { /* if not realized yet */
space_per_minheight += (long) sptr->maxaccess * bytesperrow = (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
(long) sptr->samplesperrow * SIZEOF(JSAMPLE); space_per_minheight += (long) sptr->maxaccess * bytesperrow;
maximum_space += (long) sptr->rows_in_array * maximum_space += (long) sptr->rows_in_array * bytesperrow;
(long) sptr->samplesperrow * SIZEOF(JSAMPLE);
} }
} }
for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
if (bptr->mem_buffer == NULL) { /* if not realized yet */ if (bptr->mem_buffer == NULL) { /* if not realized yet */
space_per_minheight += (long) bptr->maxaccess * bytesperrow = (long) bptr->blocksperrow * SIZEOF(JBLOCK);
(long) bptr->blocksperrow * SIZEOF(JBLOCK); space_per_minheight += (long) bptr->maxaccess * bytesperrow;
maximum_space += (long) bptr->rows_in_array * maximum_space += (long) bptr->rows_in_array * bytesperrow;
(long) bptr->blocksperrow * SIZEOF(JBLOCK);
} }
} }
@ -618,7 +614,7 @@ realize_virt_arrays (j_common_ptr cinfo)
/* Determine amount of memory to actually use; this is system-dependent. */ /* Determine amount of memory to actually use; this is system-dependent. */
avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space, avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
mem->total_space_allocated); (long) mem->total_space_allocated);
/* If the maximum space needed is available, make all the buffers full /* If the maximum space needed is available, make all the buffers full
* height; otherwise parcel it out with the same number of minheights * height; otherwise parcel it out with the same number of minheights
@ -694,7 +690,7 @@ do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
long bytesperrow, file_offset, byte_count, rows, thisrow, i; long bytesperrow, file_offset, byte_count, rows, thisrow, i;
bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE); bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
file_offset = ptr->cur_start_row * bytesperrow; file_offset = (long) ptr->cur_start_row * bytesperrow;
/* Loop to read or write each allocation chunk in mem_buffer */ /* Loop to read or write each allocation chunk in mem_buffer */
for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
/* One chunk, but check for short chunk at end of buffer */ /* One chunk, but check for short chunk at end of buffer */
@ -727,7 +723,7 @@ do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
long bytesperrow, file_offset, byte_count, rows, thisrow, i; long bytesperrow, file_offset, byte_count, rows, thisrow, i;
bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK); bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
file_offset = ptr->cur_start_row * bytesperrow; file_offset = (long) ptr->cur_start_row * bytesperrow;
/* Loop to read or write each allocation chunk in mem_buffer */ /* Loop to read or write each allocation chunk in mem_buffer */
for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
/* One chunk, but check for short chunk at end of buffer */ /* One chunk, but check for short chunk at end of buffer */
@ -771,7 +767,7 @@ access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
/* Make the desired part of the virtual array accessible */ /* Make the desired part of the virtual array accessible */
if (start_row < ptr->cur_start_row || if (start_row < ptr->cur_start_row ||
end_row > ptr->cur_start_row+ptr->rows_in_mem) { end_row > ptr->cur_start_row + ptr->rows_in_mem) {
if (! ptr->b_s_open) if (! ptr->b_s_open)
ERREXIT(cinfo, JERR_VIRTUAL_BUG); ERREXIT(cinfo, JERR_VIRTUAL_BUG);
/* Flush old buffer contents if necessary */ /* Flush old buffer contents if necessary */
@ -856,7 +852,7 @@ access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
/* Make the desired part of the virtual array accessible */ /* Make the desired part of the virtual array accessible */
if (start_row < ptr->cur_start_row || if (start_row < ptr->cur_start_row ||
end_row > ptr->cur_start_row+ptr->rows_in_mem) { end_row > ptr->cur_start_row + ptr->rows_in_mem) {
if (! ptr->b_s_open) if (! ptr->b_s_open)
ERREXIT(cinfo, JERR_VIRTUAL_BUG); ERREXIT(cinfo, JERR_VIRTUAL_BUG);
/* Flush old buffer contents if necessary */ /* Flush old buffer contents if necessary */
@ -1093,7 +1089,7 @@ jinit_memory_mgr (j_common_ptr cinfo)
mem->total_space_allocated = SIZEOF(my_memory_mgr); mem->total_space_allocated = SIZEOF(my_memory_mgr);
/* Declare ourselves open for business */ /* Declare ourselves open for business */
cinfo->mem = & mem->pub; cinfo->mem = &mem->pub;
/* Check for an environment variable JPEGMEM; if found, override the /* Check for an environment variable JPEGMEM; if found, override the
* default max_memory setting from jpeg_mem_init. Note that the * default max_memory setting from jpeg_mem_init. Note that the

@ -2,6 +2,7 @@
* jmemnobs.c * jmemnobs.c
* *
* Copyright (C) 1992-1996, Thomas G. Lane. * Copyright (C) 1992-1996, Thomas G. Lane.
* Modified 2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -12,7 +13,7 @@
* This is very portable in the sense that it'll compile on almost anything, * This is very portable in the sense that it'll compile on almost anything,
* but you'd better have lots of main memory (or virtual memory) if you want * but you'd better have lots of main memory (or virtual memory) if you want
* to process big images. * to process big images.
* Note that the max_memory_to_use option is ignored by this implementation. * Note that the max_memory_to_use option is respected by this implementation.
*/ */
#define JPEG_INTERNALS #define JPEG_INTERNALS
@ -66,13 +67,16 @@ jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
/* /*
* This routine computes the total memory space available for allocation. * This routine computes the total memory space available for allocation.
* Here we always say, "we got all you want bud!"
*/ */
GLOBAL(long) GLOBAL(long)
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed, jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
long max_bytes_needed, long already_allocated) long max_bytes_needed, long already_allocated)
{ {
if (cinfo->mem->max_memory_to_use)
return cinfo->mem->max_memory_to_use - already_allocated;
/* Here we say, "we got all you want bud!" */
return max_bytes_needed; return max_bytes_needed;
} }

@ -2,7 +2,7 @@
* jpegint.h * jpegint.h
* *
* Copyright (C) 1991-1997, Thomas G. Lane. * Copyright (C) 1991-1997, Thomas G. Lane.
* Modified 1997-2017 by Guido Vollbeding. * Modified 1997-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -302,6 +302,13 @@ struct jpeg_color_quantizer {
#define RIGHT_SHIFT(x,shft) ((x) >> (shft)) #define RIGHT_SHIFT(x,shft) ((x) >> (shft))
#endif #endif
/* Descale and correctly round an INT32 value that's scaled by N bits.
* We assume RIGHT_SHIFT rounds towards minus infinity, so adding
* the fudge factor is correct for either sign of X.
*/
#define DESCALE(x,n) RIGHT_SHIFT((x) + ((INT32) 1 << ((n)-1)), n)
/* Short forms of external names for systems with brain-damaged linkers. */ /* Short forms of external names for systems with brain-damaged linkers. */

@ -2,7 +2,7 @@
* jpeglib.h * jpeglib.h
* *
* Copyright (C) 1991-1998, Thomas G. Lane. * Copyright (C) 1991-1998, Thomas G. Lane.
* Modified 2002-2017 by Guido Vollbeding. * Modified 2002-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -39,7 +39,7 @@ extern "C" {
#define JPEG_LIB_VERSION 90 /* Compatibility version 9.0 */ #define JPEG_LIB_VERSION 90 /* Compatibility version 9.0 */
#define JPEG_LIB_VERSION_MAJOR 9 #define JPEG_LIB_VERSION_MAJOR 9
#define JPEG_LIB_VERSION_MINOR 3 #define JPEG_LIB_VERSION_MINOR 4
/* Various constants determining the sizes of things. /* Various constants determining the sizes of things.
@ -909,6 +909,7 @@ typedef JMETHOD(boolean, jpeg_marker_parser_method, (j_decompress_ptr cinfo));
#define jpeg_suppress_tables jSuppressTables #define jpeg_suppress_tables jSuppressTables
#define jpeg_alloc_quant_table jAlcQTable #define jpeg_alloc_quant_table jAlcQTable
#define jpeg_alloc_huff_table jAlcHTable #define jpeg_alloc_huff_table jAlcHTable
#define jpeg_std_huff_table jStdHTable
#define jpeg_start_compress jStrtCompress #define jpeg_start_compress jStrtCompress
#define jpeg_write_scanlines jWrtScanlines #define jpeg_write_scanlines jWrtScanlines
#define jpeg_finish_compress jFinCompress #define jpeg_finish_compress jFinCompress
@ -977,10 +978,10 @@ EXTERN(void) jpeg_stdio_src JPP((j_decompress_ptr cinfo, FILE * infile));
/* Data source and destination managers: memory buffers. */ /* Data source and destination managers: memory buffers. */
EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo, EXTERN(void) jpeg_mem_dest JPP((j_compress_ptr cinfo,
unsigned char ** outbuffer, unsigned char ** outbuffer,
unsigned long * outsize)); size_t * outsize));
EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo, EXTERN(void) jpeg_mem_src JPP((j_decompress_ptr cinfo,
const unsigned char * inbuffer, const unsigned char * inbuffer,
unsigned long insize)); size_t insize));
/* Default parameter setup for compression */ /* Default parameter setup for compression */
EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo)); EXTERN(void) jpeg_set_defaults JPP((j_compress_ptr cinfo));
@ -1005,6 +1006,8 @@ EXTERN(void) jpeg_suppress_tables JPP((j_compress_ptr cinfo,
boolean suppress)); boolean suppress));
EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo)); EXTERN(JQUANT_TBL *) jpeg_alloc_quant_table JPP((j_common_ptr cinfo));
EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo)); EXTERN(JHUFF_TBL *) jpeg_alloc_huff_table JPP((j_common_ptr cinfo));
EXTERN(JHUFF_TBL *) jpeg_std_huff_table JPP((j_common_ptr cinfo,
boolean isDC, int tblno));
/* Main entry points for compression */ /* Main entry points for compression */
EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo, EXTERN(void) jpeg_start_compress JPP((j_compress_ptr cinfo,

@ -2,7 +2,7 @@
* jutils.c * jutils.c
* *
* Copyright (C) 1991-1996, Thomas G. Lane. * Copyright (C) 1991-1996, Thomas G. Lane.
* Modified 2009-2011 by Guido Vollbeding. * Modified 2009-2019 by Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -185,7 +185,7 @@ jcopy_sample_rows (JSAMPARRAY input_array, int source_row,
{ {
register JSAMPROW inptr, outptr; register JSAMPROW inptr, outptr;
#ifdef FMEMCOPY #ifdef FMEMCOPY
register size_t count = (size_t) (num_cols * SIZEOF(JSAMPLE)); register size_t count = (size_t) num_cols * SIZEOF(JSAMPLE);
#else #else
register JDIMENSION count; register JDIMENSION count;
#endif #endif
@ -213,7 +213,7 @@ jcopy_block_row (JBLOCKROW input_row, JBLOCKROW output_row,
/* Copy a row of coefficient blocks from one place to another. */ /* Copy a row of coefficient blocks from one place to another. */
{ {
#ifdef FMEMCOPY #ifdef FMEMCOPY
FMEMCOPY(output_row, input_row, num_blocks * (DCTSIZE2 * SIZEOF(JCOEF))); FMEMCOPY(output_row, input_row, (size_t) num_blocks * (DCTSIZE2 * SIZEOF(JCOEF)));
#else #else
register JCOEFPTR inptr, outptr; register JCOEFPTR inptr, outptr;
register long count; register long count;

@ -1,7 +1,7 @@
/* /*
* jversion.h * jversion.h
* *
* Copyright (C) 1991-2018, Thomas G. Lane, Guido Vollbeding. * Copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@ -9,6 +9,6 @@
*/ */
#define JVERSION "9c 14-Jan-2018" #define JVERSION "9d 12-Jan-2020"
#define JCOPYRIGHT "Copyright (C) 2018, Thomas G. Lane, Guido Vollbeding" #define JCOPYRIGHT "Copyright (C) 2020, Thomas G. Lane, Guido Vollbeding"

@ -86,12 +86,24 @@ namespace cv { namespace debug_build_guard { } using namespace debug_build_guard
#define __CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define __CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define __CV_VA_NUM_ARGS(...) __CV_EXPAND(__CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) #define __CV_VA_NUM_ARGS(...) __CV_EXPAND(__CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
#if defined __GNUC__ #ifdef CV_Func
// keep current value (through OpenCV port file)
#elif defined __GNUC__ || (defined (__cpluscplus) && (__cpluscplus >= 201103))
#define CV_Func __func__
#elif defined __clang__ && (__clang_minor__ * 100 + __clang_major >= 305)
#define CV_Func __func__
#elif defined(__STDC_VERSION__) && (__STDC_VERSION >= 199901)
#define CV_Func __func__ #define CV_Func __func__
#elif defined _MSC_VER #elif defined _MSC_VER
#define CV_Func __FUNCTION__ #define CV_Func __FUNCTION__
#elif defined(__INTEL_COMPILER) && (_INTEL_COMPILER >= 600)
#define CV_Func __FUNCTION__
#elif defined __IBMCPP__ && __IBMCPP__ >=500
#define CV_Func __FUNCTION__
#elif defined __BORLAND__ && (__BORLANDC__ >= 0x550)
#define CV_Func __FUNC__
#else #else
#define CV_Func "" #define CV_Func "<unknown>"
#endif #endif
//! @cond IGNORED //! @cond IGNORED

@ -261,6 +261,10 @@ public:
@param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform @param contrastThreshold The contrast threshold used to filter out weak features in semi-uniform
(low-contrast) regions. The larger the threshold, the less features are produced by the detector. (low-contrast) regions. The larger the threshold, the less features are produced by the detector.
@note The contrast threshold will be divided by nOctaveLayers when the filtering is applied. When
nOctaveLayers is set to default and if you want to use the value used in D. Lowe paper, 0.03, set
this argument to 0.09.
@param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning @param edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning
is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are is different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are
filtered out (more features are retained). filtered out (more features are retained).
@ -271,6 +275,8 @@ public:
CV_WRAP static Ptr<SIFT> create(int nfeatures = 0, int nOctaveLayers = 3, CV_WRAP static Ptr<SIFT> create(int nfeatures = 0, int nOctaveLayers = 3,
double contrastThreshold = 0.04, double edgeThreshold = 10, double contrastThreshold = 0.04, double edgeThreshold = 10,
double sigma = 1.6); double sigma = 1.6);
CV_WRAP virtual String getDefaultName() const CV_OVERRIDE;
}; };
typedef SIFT SiftFeatureDetector; typedef SIFT SiftFeatureDetector;

@ -21,7 +21,8 @@ namespace opencv_test
ORB_DEFAULT, ORB_1500_13_1, \ ORB_DEFAULT, ORB_1500_13_1, \
AKAZE_DEFAULT, AKAZE_DESCRIPTOR_KAZE, \ AKAZE_DEFAULT, AKAZE_DESCRIPTOR_KAZE, \
BRISK_DEFAULT, \ BRISK_DEFAULT, \
KAZE_DEFAULT KAZE_DEFAULT, \
SIFT_DEFAULT
#define CV_ENUM_EXPAND(name, ...) CV_ENUM(name, __VA_ARGS__) #define CV_ENUM_EXPAND(name, ...) CV_ENUM(name, __VA_ARGS__)
@ -77,6 +78,8 @@ static inline Ptr<Feature2D> getFeature2D(Feature2DType type)
return KAZE::create(); return KAZE::create();
case MSER_DEFAULT: case MSER_DEFAULT:
return MSER::create(); return MSER::create();
case SIFT_DEFAULT:
return SIFT::create();
default: default:
return Ptr<Feature2D>(); return Ptr<Feature2D>();
} }

@ -1,85 +0,0 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "perf_precomp.hpp"
namespace opencv_test { namespace {
typedef perf::TestBaseWithParam<std::string> SIFT_detect;
typedef perf::TestBaseWithParam<std::string> SIFT_extract;
typedef perf::TestBaseWithParam<std::string> SIFT_full;
#define SIFT_IMAGES \
"cv/detectors_descriptors_evaluation/images_datasets/leuven/img1.png",\
"stitching/a3.png"
PERF_TEST_P_(SIFT_detect, SIFT)
{
string filename = getDataPath(GetParam());
Mat frame = imread(filename, IMREAD_GRAYSCALE);
ASSERT_FALSE(frame.empty()) << "Unable to load source image " << filename;
Mat mask;
declare.in(frame).time(90);
Ptr<SIFT> detector = SIFT::create();
vector<KeyPoint> points;
PERF_SAMPLE_BEGIN();
detector->detect(frame, points, mask);
PERF_SAMPLE_END();
SANITY_CHECK_NOTHING();
}
PERF_TEST_P_(SIFT_extract, SIFT)
{
string filename = getDataPath(GetParam());
Mat frame = imread(filename, IMREAD_GRAYSCALE);
ASSERT_FALSE(frame.empty()) << "Unable to load source image " << filename;
Mat mask;
declare.in(frame).time(90);
Ptr<SIFT> detector = SIFT::create();
vector<KeyPoint> points;
Mat descriptors;
detector->detect(frame, points, mask);
PERF_SAMPLE_BEGIN();
detector->compute(frame, points, descriptors);
PERF_SAMPLE_END();
SANITY_CHECK_NOTHING();
}
PERF_TEST_P_(SIFT_full, SIFT)
{
string filename = getDataPath(GetParam());
Mat frame = imread(filename, IMREAD_GRAYSCALE);
ASSERT_FALSE(frame.empty()) << "Unable to load source image " << filename;
Mat mask;
declare.in(frame).time(90);
Ptr<SIFT> detector = SIFT::create();
vector<KeyPoint> points;
Mat descriptors;
PERF_SAMPLE_BEGIN();
detector->detectAndCompute(frame, mask, points, descriptors, false);
PERF_SAMPLE_END();
SANITY_CHECK_NOTHING();
}
INSTANTIATE_TEST_CASE_P(/*nothing*/, SIFT_detect,
testing::Values(SIFT_IMAGES)
);
INSTANTIATE_TEST_CASE_P(/*nothing*/, SIFT_extract,
testing::Values(SIFT_IMAGES)
);
INSTANTIATE_TEST_CASE_P(/*nothing*/, SIFT_full,
testing::Values(SIFT_IMAGES)
);
}} // namespace

@ -126,6 +126,11 @@ Ptr<SIFT> SIFT::create( int _nfeatures, int _nOctaveLayers,
return makePtr<SIFT_Impl>(_nfeatures, _nOctaveLayers, _contrastThreshold, _edgeThreshold, _sigma); return makePtr<SIFT_Impl>(_nfeatures, _nOctaveLayers, _contrastThreshold, _edgeThreshold, _sigma);
} }
String SIFT::getDefaultName() const
{
return (Feature2D::getDefaultName() + ".SIFT");
}
static inline void static inline void
unpackOctave(const KeyPoint& kpt, int& octave, int& layer, float& scale) unpackOctave(const KeyPoint& kpt, int& octave, int& layer, float& scale)
{ {

@ -36,9 +36,8 @@ INSTANTIATE_TEST_CASE_P(AKAZE_DESCRIPTOR_KAZE, DescriptorRotationInvariance,
* Descriptor's scale invariance check * Descriptor's scale invariance check
*/ */
// TODO: Expected: (descInliersRatio) >= (minInliersRatio), actual: 0.330378 vs 0.78 INSTANTIATE_TEST_CASE_P(SIFT, DescriptorScaleInvariance,
INSTANTIATE_TEST_CASE_P(DISABLED_SIFT, DescriptorScaleInvariance, Value(IMAGE_BIKES, SIFT::create(0, 3, 0.09), SIFT::create(0, 3, 0.09), 0.78f));
Value(IMAGE_BIKES, SIFT::create(), SIFT::create(), 0.78f));
INSTANTIATE_TEST_CASE_P(AKAZE, DescriptorScaleInvariance, INSTANTIATE_TEST_CASE_P(AKAZE, DescriptorScaleInvariance,
Value(IMAGE_BIKES, AKAZE::create(), AKAZE::create(), 0.6f)); Value(IMAGE_BIKES, AKAZE::create(), AKAZE::create(), 0.6f));

@ -12,6 +12,26 @@ typedef tuple<std::string, Ptr<FeatureDetector>, Ptr<DescriptorExtractor>, float
String_FeatureDetector_DescriptorExtractor_Float_t; String_FeatureDetector_DescriptorExtractor_Float_t;
static
void SetSuitableSIFTOctave(vector<KeyPoint>& keypoints,
int firstOctave = -1, int nOctaveLayers = 3, double sigma = 1.6)
{
for (size_t i = 0; i < keypoints.size(); i++ )
{
int octv, layer;
KeyPoint& kpt = keypoints[i];
double octv_layer = std::log(kpt.size / sigma) / std::log(2.) - 1;
octv = cvFloor(octv_layer);
layer = cvRound( (octv_layer - octv) * nOctaveLayers );
if (octv < firstOctave)
{
octv = firstOctave;
layer = 0;
}
kpt.octave = (layer << 8) | (octv & 255);
}
}
static static
void rotateKeyPoints(const vector<KeyPoint>& src, const Mat& H, float angle, vector<KeyPoint>& dst) void rotateKeyPoints(const vector<KeyPoint>& src, const Mat& H, float angle, vector<KeyPoint>& dst)
{ {
@ -129,6 +149,10 @@ TEST_P(DescriptorScaleInvariance, scale)
vector<KeyPoint> keypoints1; vector<KeyPoint> keypoints1;
scaleKeyPoints(keypoints0, keypoints1, 1.0f/scale); scaleKeyPoints(keypoints0, keypoints1, 1.0f/scale);
if (featureDetector->getDefaultName() == "Feature2D.SIFT")
{
SetSuitableSIFTOctave(keypoints1);
}
Mat descriptors1; Mat descriptors1;
descriptorExtractor->compute(image1, keypoints1, descriptors1); descriptorExtractor->compute(image1, keypoints1, descriptors1);

@ -36,9 +36,8 @@ INSTANTIATE_TEST_CASE_P(AKAZE_DESCRIPTOR_KAZE, DetectorRotationInvariance,
* Detector's scale invariance check * Detector's scale invariance check
*/ */
// TODO: Expected: (keyPointMatchesRatio) >= (minKeyPointMatchesRatio), actual: 0.596752 vs 0.69 INSTANTIATE_TEST_CASE_P(SIFT, DetectorScaleInvariance,
INSTANTIATE_TEST_CASE_P(DISABLED_SIFT, DetectorScaleInvariance, Value(IMAGE_BIKES, SIFT::create(0, 3, 0.09), 0.65f, 0.98f));
Value(IMAGE_BIKES, SIFT::create(), 0.69f, 0.98f));
INSTANTIATE_TEST_CASE_P(BRISK, DetectorScaleInvariance, INSTANTIATE_TEST_CASE_P(BRISK, DetectorScaleInvariance,
Value(IMAGE_BIKES, BRISK::create(), 0.08f, 0.49f)); Value(IMAGE_BIKES, BRISK::create(), 0.08f, 0.49f));

@ -708,7 +708,7 @@ struct KL_Divergence
Iterator1 last = a + size; Iterator1 last = a + size;
while (a < last) { while (a < last) {
if (* b != 0) { if ( *a != 0 && *b != 0 ) {
ResultType ratio = (ResultType)(*a / *b); ResultType ratio = (ResultType)(*a / *b);
if (ratio>0) { if (ratio>0) {
result += *a * log(ratio); result += *a * log(ratio);
@ -731,7 +731,7 @@ struct KL_Divergence
inline ResultType accum_dist(const U& a, const V& b, int) const inline ResultType accum_dist(const U& a, const V& b, int) const
{ {
ResultType result = ResultType(); ResultType result = ResultType();
if( *b != 0 ) { if( a != 0 && b != 0 ) {
ResultType ratio = (ResultType)(a / b); ResultType ratio = (ResultType)(a / b);
if (ratio>0) { if (ratio>0) {
result = a * log(ratio); result = a * log(ratio);

@ -461,7 +461,7 @@ private:
DistanceType span = bbox[i].high-bbox[i].low; DistanceType span = bbox[i].high-bbox[i].low;
if (span>(DistanceType)((1-EPS)*max_span)) { if (span>(DistanceType)((1-EPS)*max_span)) {
ElementType min_elem, max_elem; ElementType min_elem, max_elem;
computeMinMax(ind, count, cutfeat, min_elem, max_elem); computeMinMax(ind, count, (int)i, min_elem, max_elem);
DistanceType spread = (DistanceType)(max_elem-min_elem); DistanceType spread = (DistanceType)(max_elem-min_elem);
if (spread>max_spread) { if (spread>max_spread) {
cutfeat = (int)i; cutfeat = (int)i;
@ -548,13 +548,21 @@ private:
/* If this is a leaf node, then do check and return. */ /* If this is a leaf node, then do check and return. */
if ((node->child1 == NULL)&&(node->child2 == NULL)) { if ((node->child1 == NULL)&&(node->child2 == NULL)) {
DistanceType worst_dist = result_set.worstDist(); DistanceType worst_dist = result_set.worstDist();
if (reorder_) {
for (int i=node->left; i<node->right; ++i) {
DistanceType dist = distance_(vec, data_[i], dim_, worst_dist);
if (dist<worst_dist) {
result_set.addPoint(dist,vind_[i]);
}
}
} else {
for (int i=node->left; i<node->right; ++i) { for (int i=node->left; i<node->right; ++i) {
int index = reorder_ ? i : vind_[i]; DistanceType dist = distance_(vec, data_[vind_[i]], dim_, worst_dist);
DistanceType dist = distance_(vec, data_[index], dim_, worst_dist);
if (dist<worst_dist) { if (dist<worst_dist) {
result_set.addPoint(dist,vind_[i]); result_set.addPoint(dist,vind_[i]);
} }
} }
}
return; return;
} }

@ -650,7 +650,8 @@ private:
* *
* Params: * Params:
* node = the node to use * node = the node to use
* indices = the indices of the points belonging to the node * indices = array of indices of the points belonging to the node
* indices_length = number of indices in the array
*/ */
void computeNodeStatistics(KMeansNodePtr node, int* indices, int indices_length) void computeNodeStatistics(KMeansNodePtr node, int* indices, int indices_length)
{ {
@ -662,7 +663,7 @@ private:
memset(mean,0,veclen_*sizeof(DistanceType)); memset(mean,0,veclen_*sizeof(DistanceType));
for (size_t i=0; i<size_; ++i) { for (size_t i=0; i<(size_t)indices_length; ++i) {
ElementType* vec = dataset_[indices[i]]; ElementType* vec = dataset_[indices[i]];
for (size_t j=0; j<veclen_; ++j) { for (size_t j=0; j<veclen_; ++j) {
mean[j] += vec[j]; mean[j] += vec[j];

@ -60,20 +60,20 @@ struct LshIndexParams : public IndexParams
{ {
LshIndexParams(unsigned int table_number = 12, unsigned int key_size = 20, unsigned int multi_probe_level = 2) LshIndexParams(unsigned int table_number = 12, unsigned int key_size = 20, unsigned int multi_probe_level = 2)
{ {
(* this)["algorithm"] = FLANN_INDEX_LSH; (*this)["algorithm"] = FLANN_INDEX_LSH;
// The number of hash tables to use // The number of hash tables to use
(*this)["table_number"] = table_number; (*this)["table_number"] = static_cast<int>(table_number);
// The length of the key in the hash tables // The length of the key in the hash tables
(*this)["key_size"] = key_size; (*this)["key_size"] = static_cast<int>(key_size);
// Number of levels to use in multi-probe (0 for standard LSH) // Number of levels to use in multi-probe (0 for standard LSH)
(*this)["multi_probe_level"] = multi_probe_level; (*this)["multi_probe_level"] = static_cast<int>(multi_probe_level);
} }
}; };
/** /**
* Randomized kd-tree index * Locality-sensitive hashing index
* *
* Contains the k-d trees and other information for indexing a set of points * Contains the tables and other information for indexing a set of points
* for nearest-neighbor matching. * for nearest-neighbor matching.
*/ */
template<typename Distance> template<typename Distance>
@ -94,9 +94,9 @@ public:
{ {
// cv::flann::IndexParams sets integer params as 'int', so it is used with get_param // cv::flann::IndexParams sets integer params as 'int', so it is used with get_param
// in place of 'unsigned int' // in place of 'unsigned int'
table_number_ = (unsigned int)get_param<int>(index_params_,"table_number",12); table_number_ = get_param(index_params_,"table_number",12);
key_size_ = (unsigned int)get_param<int>(index_params_,"key_size",20); key_size_ = get_param(index_params_,"key_size",20);
multi_probe_level_ = (unsigned int)get_param<int>(index_params_,"multi_probe_level",2); multi_probe_level_ = get_param(index_params_,"multi_probe_level",2);
feature_size_ = (unsigned)dataset_.cols; feature_size_ = (unsigned)dataset_.cols;
fill_xor_mask(0, key_size_, multi_probe_level_, xor_masks_); fill_xor_mask(0, key_size_, multi_probe_level_, xor_masks_);
@ -112,7 +112,7 @@ public:
void buildIndex() CV_OVERRIDE void buildIndex() CV_OVERRIDE
{ {
tables_.resize(table_number_); tables_.resize(table_number_);
for (unsigned int i = 0; i < table_number_; ++i) { for (int i = 0; i < table_number_; ++i) {
lsh::LshTable<ElementType>& table = tables_[i]; lsh::LshTable<ElementType>& table = tables_[i];
table = lsh::LshTable<ElementType>(feature_size_, key_size_); table = lsh::LshTable<ElementType>(feature_size_, key_size_);
@ -378,11 +378,11 @@ private:
IndexParams index_params_; IndexParams index_params_;
/** table number */ /** table number */
unsigned int table_number_; int table_number_;
/** key size */ /** key size */
unsigned int key_size_; int key_size_;
/** How far should we look for neighbors in multi-probe LSH */ /** How far should we look for neighbors in multi-probe LSH */
unsigned int multi_probe_level_; int multi_probe_level_;
/** The XOR masks to apply to a key to get the neighboring buckets */ /** The XOR masks to apply to a key to get the neighboring buckets */
std::vector<lsh::BucketKey> xor_masks_; std::vector<lsh::BucketKey> xor_masks_;

@ -245,7 +245,7 @@ public:
{ {
std::cerr << "LSH is not implemented for that type" << std::endl; std::cerr << "LSH is not implemented for that type" << std::endl;
assert(0); assert(0);
return 1; return 0;
} }
/** Get statistics about the table /** Get statistics about the table

@ -196,12 +196,10 @@ public:
#endif #endif
{ {
// Check for duplicate indices // Check for duplicate indices
int j = i - 1; for (int j = i; dists[j] == dist && j--;) {
while ((j >= 0) && (dists[j] == dist)) {
if (indices[j] == index) { if (indices[j] == index) {
return; return;
} }
--j;
} }
break; break;
} }

@ -90,4 +90,10 @@ void CV_LshTableBadArgTest::run( int /* start_from */ )
TEST(Flann_LshTable, badarg) { CV_LshTableBadArgTest test; test.safe_run(); } TEST(Flann_LshTable, badarg) { CV_LshTableBadArgTest test; test.safe_run(); }
TEST(Flann_LshTable, bad_any_cast) {
Mat features = Mat::ones(1, 64, CV_8U);
EXPECT_NO_THROW(flann::GenericIndex<cvflann::Hamming2<unsigned char> >(
features, cvflann::LshIndexParams()));
}
}} // namespace }} // namespace

@ -112,6 +112,21 @@ void UIImageToMat(const UIImage* image,
m.step[0], colorSpace, m.step[0], colorSpace,
bitmapInfo); bitmapInfo);
} }
else if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelIndexed)
{
// CGBitmapContextCreate() does not support indexed color spaces.
colorSpace = CGColorSpaceCreateDeviceRGB();
m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
if (!alphaExist)
bitmapInfo = kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault;
else
m = cv::Scalar(0);
contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8,
m.step[0], colorSpace,
bitmapInfo);
CGColorSpaceRelease(colorSpace);
}
else else
{ {
m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels

Loading…
Cancel
Save