diff --git a/3rdparty/libjpeg/jquant2.c b/3rdparty/libjpeg/jquant2.c index fdf3b2088..2ac3f841b 100644 --- a/3rdparty/libjpeg/jquant2.c +++ b/3rdparty/libjpeg/jquant2.c @@ -265,9 +265,9 @@ typedef struct { INT32 volume; /* The number of nonzero histogram cells within this box */ long colorcount; -} box; +} _box; -typedef box * boxptr; +typedef _box * boxptr; LOCAL(boxptr) @@ -546,7 +546,7 @@ select_colors (j_decompress_ptr cinfo, int desired_colors) /* Allocate workspace for box list */ boxlist = (boxptr) (*cinfo->mem->alloc_small) - ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box)); + ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(_box)); /* Initialize one box containing whole space */ numboxes = 1; boxlist[0].c0min = 0; diff --git a/3rdparty/libpng/CHANGES b/3rdparty/libpng/CHANGES index 83f8f0846..2e4d2bb29 100644 --- a/3rdparty/libpng/CHANGES +++ b/3rdparty/libpng/CHANGES @@ -1,11 +1,14 @@ #if 0 CHANGES - changes for libpng -Version 0.2 +version 0.1 [March 29, 1995] + initial work-in-progress release + +version 0.2 [April 1, 1995] added reader into png.h fixed small problems in stub file -Version 0.3 +version 0.3 [April 8, 1995] added pull reader split up pngwrite.c to several files added pnglib.txt @@ -14,9 +17,9 @@ Version 0.3 fixed some bugs in writer interfaced with zlib 0.5 added K&R support - added check for 64 KB blocks for 16-bit machines + added check for 64 KB blocks for 16 bit machines -Version 0.4 +version 0.4 [April 26, 1995] cleaned up code and commented code simplified time handling into png_time created png_color_16 and png_color_8 to handle color needs @@ -27,28 +30,29 @@ Version 0.4 cleaned up zTXt reader and writer (using zlib's Reset functions) split transformations into pngrtran.c and pngwtran.c -Version 0.5 +version 0.5 [April 30, 1995] interfaced with zlib 0.8 fixed many reading and writing bugs saved using 3 spaces instead of tabs -Version 0.6 +version 0.6 [May 1, 1995] + first beta release added png_large_malloc() and png_large_free() added png_size_t cleaned up some compiler warnings added png_start_read_image() -Version 0.7 +version 0.7 [June 24, 1995] cleaned up lots of bugs finished dithering and other stuff added test program changed name from pnglib to libpng -Version 0.71 [June, 1995] +version 0.71 [June 26, 1995] changed pngtest.png for zlib 0.93 fixed error in libpng.txt and example.c -Version 0.8 +version 0.8 [August 20, 1995] cleaned up some bugs added png_set_filler() split up pngstub.c into pngmem.c, pngio.c, and pngerror.c @@ -91,7 +95,7 @@ Version 0.88 [January, 1996] cleaned up documentation added callbacks for read/write and warning/error functions -Version 0.89 [July, 1996] +Version 0.89 [June 5, 1996] Added new initialization API to make libpng work better with shared libs we now have png_create_read_struct(), png_create_write_struct(), png_create_info_struct(), png_destroy_read_struct(), and @@ -118,6 +122,9 @@ Version 0.89 [July, 1996] New pngtest image also has interlacing and zTXt Updated documentation to reflect new API +Version 0.89c [June 17, 1996] + Bug fixes. + Version 0.90 [January, 1997] Made CRC errors/warnings on critical and ancillary chunks configurable libpng will use the zlib CRC routines by (compile-time) default @@ -158,7 +165,7 @@ Version 0.95 [March, 1997] Added new pCAL chunk read/write support Added experimental filter selection weighting (Greg Roelofs) Removed old png_set_rgbx() and png_set_xrgb() functions that have been - obsolete for about 2 years now (use png_set_filler() instead) + obsolete for about 2 years now (use png_set_filler() instead) Added macros to read 16- and 32-bit ints directly from buffer, to be used only on those systems that support it (namely PowerPC and 680x0) With some testing, this may become the default for MACOS/PPC systems. @@ -440,7 +447,7 @@ Version 1.0.3 [January 14, 1999] Version 1.0.3a [August 12, 1999] Added check for PNG_READ_INTERLACE_SUPPORTED in pngread.c; issue a warning - if an attempt is made to read an interlaced image when it's not supported. + if an attempt is made to read an interlaced image when it's not supported. Added check if png_ptr->trans is defined before freeing it in pngread.c Modified the Y2K statement to include versions back to version 0.71 Fixed a bug in the check for valid IHDR bit_depth/color_types in pngrutil.c @@ -448,7 +455,7 @@ Version 1.0.3a [August 12, 1999] Replaced leading blanks with tab characters in makefile.hux Changed "dworkin.wustl.edu" to "ccrc.wustl.edu" in various documents. Changed (float)red and (float)green to (double)red, (double)green - in png_set_rgb_to_gray() to avoid "promotion" problems in AIX. + in png_set_rgb_to_gray() to avoid "promotion" problems in AIX. Fixed a bug in pngconf.h that omitted when PNG_DEBUG==0 (K Bracey). Reformatted libpng.3 and libpngpf.3 with proper fonts (script by J. vanZandt). Updated documentation to refer to the PNG-1.2 specification. @@ -491,7 +498,7 @@ Version 1.0.3d [September 4, 1999] Added new png_expand functions to scripts/pngdef.pas and pngos2.def Added a demo read_user_transform_fn that examines the row filters in pngtest.c -Version 1.0.4 [September 24, 1999] +Version 1.0.4 [September 24, 1999, not distributed publicly] Define PNG_ALWAYS_EXTERN in pngconf.h if __STDC__ is defined Delete #define PNG_INTERNAL and include "png.h" from pngasmrd.h Made several minor corrections to pngtest.c @@ -518,6 +525,7 @@ Version 1.0.4c [October 1, 1999] Added a "png_check_version" function in png.c and pngtest.c that will generate a helpful compiler error if an old png.h is found in the search path. Changed type of png_user_transform_depth|channels from int to png_byte. + Added "Libpng is OSI Certified Open Source Software" statement to png.h Version 1.0.4d [October 6, 1999] Changed 0.45 to 0.45455 in png_set_sRGB() @@ -904,7 +912,7 @@ Version 1.0.7 [July 1, 2000] Version 1.0.8beta1 [July 8, 2000] Added png_free(png_ptr, key) two places in pngpread.c to stop memory leaks. Changed PNG_NO_STDIO to PNG_NO_CONSOLE_IO, several places in pngrutil.c and - pngwutil.c. + pngwutil.c. Changed PNG_EXPORT_VAR to use PNG_IMPEXP, in pngconf.h. Removed unused "#include " from png.c Added WindowsCE support. @@ -912,12 +920,12 @@ Version 1.0.8beta1 [July 8, 2000] Version 1.0.8beta2 [July 10, 2000] Added project files to the wince directory and made further revisions - of pngtest.c, pngrio.c, and pngwio.c in support of WindowsCE. + of pngtest.c, pngrio.c, and pngwio.c in support of WindowsCE. Version 1.0.8beta3 [July 11, 2000] Only set the PNG_FLAG_FREE_TRNS or PNG_FREE_TRNS flag in png_handle_tRNS() - for indexed-color input files to avoid potential double-freeing trans array - under some unusual conditions; problem was introduced in version 1.0.6f. + for indexed-color input files to avoid potential double-freeing trans array + under some unusual conditions; problem was introduced in version 1.0.6f. Further revisions to pngtest.c and files in the wince subdirectory. Version 1.0.8beta4 [July 14, 2000] @@ -1089,16 +1097,16 @@ Version 1.2.0beta3 [May 17, 2001] Version 1.2.0beta4 [June 23, 2001] Check for missing profile length field in iCCP chunk and free chunk_data - in case of truncated iCCP chunk. + in case of truncated iCCP chunk. Bumped shared-library number to 3 in makefile.sgi and makefile.sggcc Bumped dll-number from 2 to 3 in makefile.cygwin Revised contrib/gregbook/rpng*-x.c to avoid a memory leak and to exit cleanly - if user attempts to run it on an 8-bit display. + if user attempts to run it on an 8-bit display. Updated contrib/gregbook Use png_malloc instead of png_zalloc to allocate palette in pngset.c Updated makefile.ibmc Added some typecasts to eliminate gcc 3.0 warnings. Changed prototypes - of png_write_oFFS width and height from png_uint_32 to png_int_32. + of png_write_oFFS width and height from png_uint_32 to png_int_32. Updated example.c Revised prototypes for png_debug_malloc and png_debug_free in pngtest.c @@ -1106,9 +1114,9 @@ Version 1.2.0beta5 [August 8, 2001] Revised contrib/gregbook Revised makefile.gcmmx Revised pnggccrd.c to conditionally compile some thread-unsafe code only - when PNG_THREAD_UNSAFE_OK is defined. + when PNG_THREAD_UNSAFE_OK is defined. Added tests to prevent pngwutil.c from writing a bKGD or tRNS chunk with - value exceeding 2^bit_depth-1 + value exceeding 2^bit_depth-1 Revised makefile.sgi and makefile.sggcc Replaced calls to fprintf(stderr,...) with png_warning() in pnggccrd.c Removed restriction that do_invert_mono only operate on 1-bit opaque files @@ -1449,8 +1457,9 @@ Version 1.2.6beta4 [July 28, 2004] Use png_malloc instead of png_zalloc to allocate the pallete. Version 1.0.16rc1 and 1.2.6rc1 [August 4, 2004] - Fixed buffer overflow vulnerability in png_handle_tRNS() - Fixed integer arithmetic overflow vulnerability in png_read_png(). + Fixed buffer overflow vulnerability (CVE-2004-0597) in png_handle_tRNS(). + Fixed NULL dereference vulnerability (CVE-2004-0598) in png_handle_iCCP(). + Fixed integer overflow vulnerability (CVE-2004-0599) in png_read_png(). Fixed some harmless bugs in png_handle_sBIT, etc, that would cause duplicate chunk types to go undetected. Fixed some timestamps in the -config version @@ -1493,7 +1502,7 @@ Version 1.0.16rc4 and 1.2.6rc4 [August 10, 2004] Version 1.0.16rc5 and 1.2.6rc5 [August 10, 2004] Moved "PNG_HANDLE_CHUNK_*" macros out of PNG_ASSEMBLER_CODE_SUPPORTED - section of png.h where they were inadvertently placed in version rc3. + section of png.h where they were inadvertently placed in version rc3. Version 1.2.6 and 1.0.16 [August 15, 2004] Revised pngtest so memory allocation testing is only done when PNG_DEBUG==1. @@ -2102,7 +2111,7 @@ Version 1.4.0beta24 [July 25, 2008] png_decompress_chunk(), and remove "chunkdata" from parameter list. Put a call to png_check_chunk_name() in png_read_chunk_header(). Revised png_check_chunk_name() to reject a name with a lowercase 3rd byte. - Removed two calls to png_check_chunk_name() occuring later in the process. + Removed two calls to png_check_chunk_name() occurring later in the process. Define PNG_NO_ERROR_NUMBERS by default in pngconf.h Version 1.4.0beta25 [July 30, 2008] @@ -2325,7 +2334,7 @@ Version 1.4.0beta63 [June 15, 2009] Version 1.4.0beta64 [June 24, 2009] Eliminated PNG_LEGACY_SUPPORTED code. Moved the various unknown chunk macro definitions outside of the - PNG_READ|WRITE_ANCILLARY_CHUNK_SUPPORTED blocks. + PNG_READ|WRITE_ANCILLARY_CHUNK_SUPPORTED blocks. Version 1.4.0beta65 [June 26, 2009] Added a reference to the libpng license in each file. @@ -3672,7 +3681,8 @@ Version 1.5.6 [November 3, 2011] No changes. Version 1.5.7beta01 [November 4, 2011] - Added support for ARM processor (Mans Rullgard) + Added support for ARM processor, when decoding all PNG up-filtered rows + and any other-filtered rows with 3 or 4 bytes per pixel (Mans Rullgard). Fixed bug in pngvalid on early allocation failure; fixed type cast in pngmem.c; pngvalid would attempt to call png_error() if the allocation of a png_struct or png_info failed. This would probably have led to a @@ -3746,8 +3756,9 @@ Version 1.5.7beta04 [November 17, 2011] Version 1.5.7beta05 [November 25, 2011] Removed "zTXt" from warning in generic chunk decompression function. - Validate time settings passed to pngset() and png_convert_to_rfc1123() - (Frank Busse). + Validate time settings passed to png_set_tIME() and png_convert_to_rfc1123() + (Frank Busse). Note: This prevented CVE-2015-7981 from affecting + libpng-1.5.7 and later. Added MINGW support to CMakeLists.txt Reject invalid compression flag or method when reading the iTXt chunk. Backed out 'simplified' API changes. The API seems too complex and there @@ -3775,127 +3786,1634 @@ Version 1.5.7rc03 [December 7, 2011] Version 1.5.7 [December 15, 2011] Minor fixes to pngvalid.c for gcc 4.6.2 compatibility to remove warnings reported by earlier versions. + Fixed minor memset/sizeof errors in pngvalid.c. -Version 1.5.8beta01 [January 15, 2011] - Removed '#include config.h"' from contrib/libtests/pngvalid.c. It's not - needed and causes trouble for VPATH building. +Version 1.6.0beta01 [December 15, 2011] + Removed machine-generated configure files from the GIT repository (they will + continue to appear in the tarball distributions and in the libpng15 and + earlier GIT branches). + Restored the new 'simplified' API, which was started in libpng-1.5.7beta02 + but later deleted from libpng-1.5.7beta05. + Added example programs for the new 'simplified' API. + Added ANSI-C (C90) headers and require them, and take advantage of the + change. Also fixed some of the projects/* and contrib/* files that needed + updates for libpng16 and the move of pngvalid.c. + With this change the required ANSI-C header files are assumed to exist: the + implementation must provide float.h, limits.h, stdarg.h and stddef.h and + libpng relies on limits.h and stddef.h existing and behaving as defined + (the other two required headers aren't used). Non-ANSI systems that don't + have stddef.h or limits.h will have to provide an appropriate fake + containing the relevant types and #defines. + Dropped support for 16-bit platforms. The use of FAR/far has been eliminated + and the definition of png_alloc_size_t is now controlled by a flag so + that 'small size_t' systems can select it if necessary. Libpng 1.6 may + not currently work on such systems -- it seems likely that it will + ask 'malloc' for more than 65535 bytes with any image that has a + sufficiently large row size (rather than simply failing to read such + images). + New tools directory containing tools used to generate libpng code. + Fixed race conditions in parallel make builds. With higher degrees of + parallelism during 'make' the use of the same temporary file names such + as 'dfn*' can result in a race where a temporary file from one arm of the + build is deleted or overwritten in another arm. This changes the + temporary files for suffix rules to always use $* and ensures that the + non-suffix rules use unique file names. + +Version 1.6.0beta02 [December 21, 2011] + Correct configure builds where build and source directories are separate. + The include path of 'config.h' was erroneously made relative in pngvalid.c + in libpng 1.5.7. + +Version 1.6.0beta03 [December 22, 2011] + Start-up code size improvements, error handler flexibility. These changes + alter how the tricky allocation of the initial png_struct and png_info + structures are handled. png_info is now handled in pretty much the same + way as everything else, except that the allocations handle NULL return + silently. png_struct is changed in a similar way on allocation and on + deallocation a 'safety' error handler is put in place (which should never + be required). The error handler itself is changed to permit mismatches + in the application and libpng error buffer size; however, this means a + silent change to the API to return the jmp_buf if the size doesn't match + the size from the libpng compilation; libpng now allocates the memory and + this may fail. Overall these changes result in slight code size + reductions; however, this is a reduction in code that is always executed + so is particularly valuable. Overall on a 64-bit system the libpng DLL + decreases in code size by 1733 bytes. pngerror.o increases in size by + about 465 bytes because of the new functionality. + Added png_convert_to_rfc1123_buffer() and deprecated png_convert_to_rfc1123() + to avoid including a spurious buffer in the png_struct. + +Version 1.6.0beta04 [December 30, 2011] + Regenerated configure scripts with automake-1.11.2 + Eliminated png_info_destroy(). It is now used only in png.c and only calls + one other internal function and memset(). + Enabled png_get_sCAL_fixed() if floating point APIs are enabled. Previously + it was disabled whenever internal fixed point arithmetic was selected, + which meant it didn't exist even on systems where FP was available but not + preferred. + Added pngvalid.c compile time checks for const APIs. + Implemented 'restrict' for png_info and png_struct. Because of the way + libpng works both png_info and png_struct are always accessed via a + single pointer. This means adding C99 'restrict' to the pointer gives + the compiler some opportunity to optimize the code. This change allows + that. Moved AC_MSG_CHECKING([if libraries can be versioned]) later to the proper location in configure.ac (Gilles Espinasse). + Changed png_memcpy to C assignment where appropriate. Changed all those + uses of png_memcpy that were doing a simple assignment to assignments + (all those cases where the thing being copied is a non-array C L-value). + Added some error checking to png_set_*() routines. + Removed the reference to the non-exported function png_memcpy() from + example.c. + Fixed the Visual C 64-bit build - it requires jmp_buf to be aligned, but + it had become misaligned. + Revised contrib/pngminus/pnm2png.c to avoid warnings when png_uint_32 + and unsigned long are of different sizes. + +Version 1.6.0beta05 [January 15, 2012] + Updated manual with description of the simplified API (copied from png.h) Fix bug in pngerror.c: some long warnings were being improperly truncated (CVE-2011-3464, bug introduced in libpng-1.5.3beta05). -Version 1.5.8rc01 [January 21, 2012] - No changes. - -Version 1.5.8rc02 [January 25, 2012] +Version 1.6.0beta06 [January 24, 2012] + Added palette support to the simplified APIs. This commit + changes some of the macro definitions in png.h, app code + may need corresponding changes. + Increased the formatted warning buffer to 192 bytes. + Added color-map support to simplified API. This is an initial version for + review; the documentation has not yet been updated. Fixed Min/GW uninstall to remove libpng.dll.a - Conditionalize the install rules for MINGW and CYGWIN in CMakeLists.txt -Version 1.5.8 [February 1, 2012] - No changes. +Version 1.6.0beta07 [January 28, 2012] + Eliminated Intel icc/icl compiler warnings. The Intel (GCC derived) + compiler issues slightly different warnings from those issued by the + current vesions of GCC. This eliminates those warnings by + adding/removing casts and small code rewrites. + Updated configure.ac from autoupdate: added --enable-werror option. + Also some layout regularization and removal of introduced tab characters + (replaced with 3-character indentation). Obsolete macros identified by + autoupdate have been removed; the replacements are all in 2.59 so + the pre-req hasn't been changed. --enable-werror checks for support + for -Werror (or the given argument) in the compiler. This mimics the + gcc configure option by allowing -Werror to be turned on safely; without + the option the tests written in configure itself fail compilation because + they cause compiler warnings. + Rewrote autogen.sh to run autoreconf instead of running tools one-by-one. + Conditionalize the install rules for MINGW and CYGWIN in CMakeLists.txt and + set CMAKE_LIBRARY_OUTPUT_DIRECTORY to "lib" on all platforms (C. Yapp). + Freeze libtool files in the 'scripts' directory. This version of autogen.sh + attempts to dissuade people from running it when it is not, or should not, + be necessary. In fact, autogen.sh does not work when run in a libpng + directory extracted from a tar distribution anymore. You must run it in + a GIT clone instead. + Added two images to contrib/pngsuite (1-bit and 2-bit transparent grayscale), + and renamed three whose names were inconsistent with those in + pngsuite/README.txt. -Version 1.5.9beta01 [February 3, 2012] - Rebuilt configure scripts in the tar distributions. +Version 1.6.0beta08 [February 1, 2012] + Fixed Image::colormap misalignment in pngstest.c + Check libtool/libtoolize version number (2.4.2) in configure.ac + Divide test-pngstest.sh into separate pngstest runs for basic and + transparent images. + Moved automake options to AM_INIT_AUTOMAKE in configure.ac + Added color-tests, silent-rules (Not yet implemented in Makefile.am) and + version checking to configure.ac + Improved pngstest speed by not doing redundant tests and add const to + the background parameter of png_image_finish_read. The --background + option is now done automagically only when required, so that commandline + option no longer exists. + Cleaned up pngpriv.h to consistently declare all functions and data. + Also eliminated PNG_CONST_DATA, which is apparently not needed but we + can't be sure until it is gone. + Added symbol prefixing that allows all the libpng external symbols + to be prefixed (suggested by Reuben Hawkins). + Updated "ftbb*.png" list in the owatcom and vstudio projects. + Fixed 'prefix' builds on clean systems. The generation of pngprefix.h + should not require itself. + Updated INSTALL to explain that autogen.sh must be run in a GIT clone, + not in a libpng directory extracted from a tar distribution. -Version 1.5.9beta02 [February 16, 2012] - Removed two unused definitions from scripts/pnglibconf.h.prebuilt +Version 1.6.0beta09 [February 1, 2012] + Reverted the prebuilt configure files to libpng-1.6.0beta05 condition. + +Version 1.6.0beta10 [February 3, 2012] + Added Z_SOLO for zlib-1.2.6+ and correct pngstest tests + Updated list of test images in CMakeLists.txt + Updated the prebuilt configure files to current condition. + Revised INSTALL information about autogen.sh; it works in tar distributions. + +Version 1.6.0beta11 [February 16, 2012] + Fix character count in pngstest command in projects/owatcom/pngstest.tgt + Revised test-pngstest.sh to report PASS/FAIL for each image. + Updated documentation about the simplified API. + Corrected estimate of error in libpng png_set_rgb_to_gray API. The API is + extremely inaccurate for sRGB conversions because it uses an 8-bit + intermediate linear value and it does not use the sRGB transform, so it + suffers from the known instability in gamma transforms for values close + to 0 (see Poynton). The net result is that the calculation has a maximum + error of 14.99/255; 0.5/255^(1/2.2). pngstest now uses 15 for the + permitted 8-bit error. This may still not be enough because of arithmetic + error. Removed some unused arrays (with #ifdef) from png_read_push_finish_row(). + Fixed a memory overwrite bug in simplified read of RGB PNG with + non-linear gamma Also bugs in the error checking in pngread.c and changed + quite a lot of the checks in pngstest.c to be correct; either correctly + written or not over-optimistic. The pngstest changes are insufficient to + allow all possible RGB transforms to be passed; pngstest cmppixel needs + to be rewritten to make it clearer which errors it allows and then changed + to permit known inaccuracies. Removed tests for no-longer-used *_EMPTY_PLTE_SUPPORTED from pngstruct.h + Fixed fixed/float API export conditionals. 1) If FIXED_POINT or + FLOATING_POINT options were switched off, png.h ended up with lone ';' + characters. This is not valid ANSI-C outside a function. The ';' + characters have been moved inside the definition of PNG_FP_EXPORT and + PNG_FIXED_EXPORT. 2) If either option was switched off, the declaration + of the corresponding functions were completely omitted, even though some + of them are still used internally. The result is still valid, but + produces warnings from gcc with some warning options (including -Wall). The + fix is to cause png.h to declare the functions with PNG_INTERNAL_FUNCTION + when png.h is included from pngpriv.h. + Check for invalid palette index while reading paletted PNG. When one is + found, issue a warning and increase png_ptr->num_palette accordingly. + Apps are responsible for checking to see if that happened. -Version 1.5.9rc01 [February 17, 2012] +Version 1.6.0beta12 [February 18, 2012] + Do not increase num_palette on invalid_index. + Relocated check for invalid palette index to pngrtran.c, after unpacking + the sub-8-bit pixels. Fixed CVE-2011-3026 buffer overrun bug. This bug was introduced when iCCP chunk support was added at libpng-1.0.6. Deal more correctly with the test on iCCP chunk length. Also removed spurious casts that may hide problems on 16-bit systems. -Version 1.5.9 [February 18, 2012] - No changes. - -Version 1.5.10beta01 [February 24, 2012] - Removed two useless #ifdef directives from pngread.c and one from pngrutil.c - Always put the CMAKE_LIBRARY in "lib" (removed special WIN32 case). - Removed empty vstudio/pngstest directory (Clifford Yapp). +Version 1.6.0beta13 [February 24, 2012] Eliminated redundant png_push_read_tEXt|zTXt|iTXt|unknown code from pngpread.c and use the sequential png_handle_tEXt, etc., in pngrutil.c; now that png_ptr->buffer is inaccessible to applications, the special handling is no longer useful. - Fixed bug with png_handle_hIST with odd chunk length (Frank Busse). - Added PNG_SAFE_LIMITS feature to pnglibconf.dfa and code in pngconf.h - to reset the user limits to safe ones if PNG_SAFE_LIMITS is defined. - To enable, use "CPPFLAGS=-DPNG_SAFE_LIMITS_SUPPORTED" on the configure - command or put "#define PNG_SAFE_LIMITS_SUPPORTED" in pnglibconf.h. - Revised the SAFE_LIMITS feature to be the same as the feature in libpng16. - Added information about the new limits in the manual. + Added PNG_SAFE_LIMITS feature to pnglibconf.dfa, pngpriv.h, and new + pngusr.dfa to reset the user limits to safe ones if PNG_SAFE_LIMITS is + defined. To enable, use "CPPFLAGS=-DPNG_SAFE_LIMITS_SUPPORTED=1" on the + configure command or put #define PNG_SAFE_LIMITS_SUPPORTED in + pnglibconf.h.prebuilt and pnglibconf.h. -Version 1.5.10beta02 [February 27, 2012] +Version 1.6.0beta14 [February 27, 2012] + Added information about the new limits in the manual. Updated Makefile.in -Version 1.5.10beta03 [March 6, 2012] +Version 1.6.0beta15 [March 2, 2012] Removed unused "current_text" members of png_struct and the png_free() of png_ptr->current_text from pngread.c - Added palette-index checking. Issue a png_warning() if an invalid index is - found. + Rewrote pngstest.c for substantial speed improvement. + Fixed transparent pixel and 16-bit rgb tests in pngstest and removed a + spurious check in pngwrite.c + Added PNG_IMAGE_FLAG_FAST for the benefit of applications that store + intermediate files, or intermediate in-memory data, while processing + image data with the simplified API. The option makes the files larger + but faster to write and read. pngstest now uses this by default; this + can be disabled with the --slow option. + Improved pngstest fine tuning of error numbers, new test file generator. + The generator generates images that test the full range of sample values, + allow the error numbers in pngstest to be tuned and checked. makepng + also allows generation of images with extra chunks, although this is + still work-in-progress. + Added check for invalid palette index while reading. + Fixed some bugs in ICC profile writing. The code should now accept + all potentially valid ICC profiles and reject obviously invalid ones. + It now uses png_error() to do so rather than casually writing a PNG + without the necessary color data. + Removed whitespace from the end of lines in all source files and scripts. -Version 1.5.10beta04 [March 10, 2012] - Fixed PNG_LIBPNG_BUILD_BASE_TYPE definition. - Fixed CMF optimization of non-IDAT compressed chunks, which was added at - libpng-1.5.4. It sometimes produced too small of a window. +Version 1.6.0beta16 [March 6, 2012] + Relocated palette-index checking function from pngrutil.c to pngtrans.c + Added palette-index checking while writing. + Changed png_inflate() and calling routines to avoid overflow problems. + This is an intermediate check-in that solves the immediate problems and + introduces one performance improvement (avoiding a copy via png_ptr->zbuf.) + Further changes will be made to make ICC profile handling more secure. + Fixed build warnings (MSVC, GCC, GCC v3). Cygwin GCC with default options + declares 'index' as a global, causing a warning if it is used as a local + variable. GCC 64-bit warns about assigning a (size_t) (unsigned 64-bit) + to an (int) (signed 32-bit). MSVC, however, warns about using the + unary '-' operator on an unsigned value (even though it is well defined + by ANSI-C to be ~x+1). The padding calculation was changed to use a + different method. Removed the tests on png_ptr->pass. + Added contrib/libtests/tarith.c to test internal arithmetic functions from + png.c. This is a libpng maintainer program used to validate changes to the + internal arithmetic functions. + Made read 'inflate' handling like write 'deflate' handling. The read + code now claims and releases png_ptr->zstream, like the write code. + The bug whereby the progressive reader failed to release the zstream + is now fixed, all initialization is delayed, and the code checks for + changed parameters on deflate rather than always calling + deflatedEnd/deflateInit. + Validate the zTXt strings in pngvalid. + Added code to validate the windowBits value passed to deflateInit2(). + If the call to deflateInit2() is wrong a png_warning will be issued + (in fact this is harmless, but the PNG data produced may be sub-optimal). -Version 1.5.10beta05 [March 10, 2012] +Version 1.6.0beta17 [March 10, 2012] + Fixed PNG_LIBPNG_BUILD_BASE_TYPE definition. Reject all iCCP chunks after the first, even if the first one is invalid. + Deflate/inflate was reworked to move common zlib calls into single + functions [rw]util.c. A new shared keyword check routine was also added + and the 'zbuf' is no longer allocated on progressive read. It is now + possible to call png_inflate() incrementally. A warning is no longer + issued if the language tag or translated keyword in the iTXt chunk + has zero length. + If benign errors are disabled use maximum window on ancilliary inflate. + This works round a bug introduced in 1.5.4 where compressed ancillary + chunks could end up with a too-small windowBits value in the deflate + header. + +Version 1.6.0beta18 [March 16, 2012] Issue a png_benign_error() instead of png_warning() about bad palette index. + In pngtest, treat benign errors as errors if "-strict" is present. Fixed an off-by-one error in the palette index checking function. + Fixed a compiler warning under Cygwin (Windows-7, 32-bit system) Revised example.c to put text strings in a temporary character array instead of directly assigning string constants to png_textp members. This avoids compiler warnings when -Wwrite-strings is enabled. + Added output flushing to aid debugging under Visual Studio. Unfortunately + this is necessary because the VS2010 output window otherwise simply loses + the error messages on error (they weren't flushed to the window before + the process exited, apparently!) + Added configuration support for benign errors and changed the read + default. Also changed some warnings in the iCCP and sRGB handling + from to benign errors. Configuration now makes read benign + errors warnings and write benign errors to errors by default (thus + changing the behavior on read). The simplified API always forces + read benign errors to warnings (regardless of the system default, unless + this is disabled in which case the simplified API can't be built.) -Version 1.5.10 [March 29, 2012] +Version 1.6.0beta19 [March 18, 2012] + Work around for duplicate row start calls; added warning messages. + This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that + fails to call one of the 'start' routines (not enabled in libpng-1.5 + because it is technically an API change, since it did normally work + before.) It also makes duplicate calls to png_read_start_row (an + internal function called at the start of the image read) benign, as + they were before changes to use png_inflate_claim. Somehow webkit is + causing this to happen; this is probably a mis-feature in the zlib + changes so this commit is only a work-round. + Removed erroneous setting of DETECT_UNINITIALIZED and added more + checks. The code now does a png_error if an attempt is made to do the + row initialization twice; this is an application error and it has + serious consequences because the transform data in png_struct is + changed by each call. + Added application error reporting and added chunk names to read + benign errors; also added --strict to pngstest - not enabled + yet because a warning is produced. + Avoid the double gamma correction warning in the simplified API. + This allows the --strict option to pass in the pngstest checks + +Version 1.6.0beta20 [March 29, 2012] + Changed chunk handler warnings into benign errors, incrementally load iCCP + Added checksum-icc.c to contrib/tools Prevent PNG_EXPAND+PNG_SHIFT doing the shift twice. + Recognize known sRGB ICC profiles while reading; prefer writing the + iCCP profile over writing the sRGB chunk, controlled by the + PNG_sRGB_PROFILE_CHECKS option. Revised png_set_text_2() to avoid potential memory corruption (fixes - CVE-2011-3048). + CVE-2011-3048, also known as CVE-2012-3425). -Version 1.5.11beta01 [April 28, 2012] +Version 1.6.0beta21 [April 27, 2012] Revised scripts/makefile.darwin: use system zlib; remove quotes around architecture list; add missing ppc architecture; add architecture options to shared library link; don't try to create a shared lib based on missing RELEASE variable. Enable png_set_check_for_invalid_index() for both read and write. - Removed #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED/#endif in pngpriv.h around + Removed #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED in pngpriv.h around declaration of png_handle_unknown(). Added -lssp_nonshared in a comment in scripts/makefile.freebsd and changed deprecated NOOBJ and NOPROFILE to NO_OBJ and NO_PROFILE. -Version 1.5.11rc01 [May 23, 2012] - No changes. +Version 1.6.0beta22 [May 23, 2012] + Removed need for -Wno-cast-align with clang. clang correctly warns on + alignment increasing pointer casts when -Wcast-align is passed. This + fixes the cases that clang warns about either by eliminating the + casts from png_bytep to png_uint_16p (pngread.c), or, for pngrutil.c + where the cast is previously verified or pngstest.c where it is OK, by + introducing new png_aligncast macros to do the cast in a way that clang + accepts. -Version 1.5.11rc02 [May 29, 2012] - Fixed some typos in comments. +Version 1.6.0beta23 [June 6, 2012] Revised CMakeLists.txt to not attempt to make a symlink under mingw. - Added two images to contrib/pngsuite (1-bit and 2-bit transparent grayscale), - and renamed three whose names were inconsistent with those in - pngsuite/README.txt. - -Version 1.5.11rc03 [June 4, 2012] + Made fixes for new optimization warnings from gcc 4.7.0. The compiler + performs an optimization which is safe; however it then warns about it. + Changing the type of 'palette_number' in pngvalid.c removes the warning. Do not depend upon a GCC feature macro being available for use in generating the linker mapfile symbol prefix. - Made fixes for new optimization warnings from gcc 4.7.0. The compiler - performed an optimization which is safe but then warned about it. - Changing the type of 'palette_number' in pngvalid.c removes the warning. + Improved performance of new do_check_palette_indexes() function (only + update the value when it actually increases, move test for whether + the check is wanted out of the function. -Version 1.5.11rc04 [June 6, 2012] - Improved performance of new do_check_palette_indexes() function. - -Version 1.5.11rc05 [June 7, 2012] +Version 1.6.0beta24 [June 7, 2012] Don't check palette indexes if num_palette is 0 (as it can be in MNG files). -Version 1.5.11 [June 14, 2012] - Include zlib.h in contrib/gregbook and contrib/visupng examples. +Version 1.6.0beta25 [June 16, 2012] + Revised png_set_keep_unknown_chunks() so num_chunks < 0 means ignore all + unknown chunks and all known chunks except for IHDR, PLTE, tRNS, IDAT, + and IEND. Previously it only meant ignore all unknown chunks, the + same as num_chunks == 0. Revised png_image_skip_unused_chunks() to + provide a list of chunks to be processed instead of a list of chunks to + ignore. Revised contrib/gregbook/readpng2.c accordingly. -Version 1.5.12 [July 11, 2012] +Version 1.6.0beta26 [July 10, 2012] Removed scripts/makefile.cegcc from the *.zip and *.7z distributions; it depends on configure, which is not included in those archives. + Moved scripts/chkfmt to contrib/tools. Changed "a+w" to "u+w" in Makefile.in to fix CVE-2012-3386. +Version 1.6.0beta27 [August 11, 2012] + Do not compile PNG_DEPRECATED, PNG_ALLOC and PNG_PRIVATE when __GNUC__ < 3. + Do not use __restrict when GNUC is <= 3.1 + Removed references to png_zalloc() and png_zfree() from the manual. + Fixed configurations where floating point is completely disabled. Because + of the changes to support symbol prefixing PNG_INTERNAL_FUNCTION declares + floating point APIs during libpng builds even if they are completely + disabled. This requires the png floating point types (png_double*) to be + declared even though the functions are never actually defined. This + change provides a dummy definition so that the declarations work, yet any + implementation will fail to compile because of an incomplete type. + Re-eliminated the use of strcpy() in pngtest.c. An unncessary use of + strcpy() was accidentally re-introduced in libpng16; this change replaces + it with strncpy(). + Eliminated use of png_sizeof(); use sizeof() instead. + Use a consistent style for (sizeof type) and (sizeof (array)) + Cleanup of png_set_filler(). This function does very different things on + read and write. In libpng 1.6 the two cases can be distinguished and + considerable code cleanup, and extra error checking, is possible. This + makes calls on the write side that have no effect be ignored with a + png_app_error(), which can be disabled in the app using + png_set_benign_errors(), and removes the spurious use of usr_channels + on the read side. + Insist on autotools 1.12.1 for git builds because there are security issues + with 1.12 and insisting on anything less would allow 1.12 to be used. + Removed info_ptr->signature[8] from WRITE-only builds. + Add some conditions for compiling png_fixed(). This is a small function + but it requires "-lm" on some platforms. + Cause pngtest --strict to fail on any warning from libpng (not just errors) + and cause it not to fail at the comparison step if libpng lacks support + for writing chunks that it reads from the input (currently only implemented + for compressed text chunks). + Make all three "make check" test programs work without READ or WRITE support. + Now "make check" will succeed even if libpng is compiled with -DPNG_NO_READ + or -DPNG_NO_WRITE. The tests performed are reduced, but the basic reading + and writing of a PNG file is always tested by one or more of the tests. + Consistently use strlen(), memset(), memcpy(), and memcmp() instead of the + png_strlen(), png_memset(), png_memcpy(), and png_memcmp() macros. + Removed the png_sizeof(), png_strlen(), png_memset(), png_memcpy(), and + png_memcmp() macros. + Work around gcc 3.x and Microsoft Visual Studio 2010 complaints. Both object + to the split initialization of num_chunks. + +Version 1.6.0beta28 [August 29, 2012] + Unknown handling fixes and clean up. This adds more correct option + control of the unknown handling, corrects the pre-existing bug where + the per-chunk 'keep' setting is ignored and makes it possible to skip + IDAT chunks in the sequential reader (broken in earlier 1.6 versions). + There is a new test program, test-unknown.c, which is a work in progress + (not currently part of the test suite). Comments in the header files now + explain how the unknown handling works. + Allow fine grain control of unknown chunk APIs. This change allows + png_set_keep_unknown_chunks() to be turned off if not required and causes + both read and write to behave appropriately (on read this is only possible + if the user callback is used to handle unknown chunks). The change + also removes the support for storing unknown chunks in the info_struct + if the only unknown handling enabled is via the callback, allowing libpng + to be configured with callback reading and none of the unnecessary code. + Corrected fix for unknown handling in pngtest. This reinstates the + libpng handling of unknown chunks other than vpAg and sTER (including + unsafe-to-copy chunks which were dropped before) and eliminates the + repositioning of vpAg and sTER in pngtest.png by changing pngtest.png + (so the chunks are where libpng would put them). + Added "tunknown" test and corrected a logic error in png_handle_unknown() + when SAVE support is absent. Moved the shell test scripts for + contrib/libtests from the libpng top directory to contrib/libtests. + png_handle_unknown() must always read or skip the chunk, if + SAVE_UNKNOWN_CHUNKS is turned off *and* the application does not set + a user callback an unknown chunk will not be read, leading to a read + error, which was revealed by the "tunknown" test. + Cleaned up and corrected ICC profile handling. + contrib/libtests/makepng: corrected 'rgb' and 'gray' cases. profile_error + messages could be truncated; made a correct buffer size calculation and + adjusted pngerror.c appropriately. png_icc_check_* checking improved; + changed the functions to receive the correct color type of the PNG on read + or write and check that it matches the color space of the profile (despite + what the comments said before, there is danger in assuming the app will + cope correctly with an RGB profile on a grayscale image and, since it + violates the PNG spec, allowing it is certain to produce inconsistent + app behavior and might even cause app crashes.) Check that profiles + contain the tags needed to process the PNG (tags all required by the ICC + spec). Removed unused PNG_STATIC from pngpriv.h. + +Version 1.6.0beta29 [September 4, 2012] + Fixed the simplified API example programs to add the *colormap parameter + to several of he API and improved the error message if the version field + is not set. + Added contrib/examples/* to the *.zip and *.7z distributions. + Updated simplified API synopses and description of the png_image structure + in the manual. + Made makepng and pngtest produce identical PNGs, add "--relaxed" option + to pngtest. The "--relaxed" option turns off the benign errors that are + enabled by default in pre-RC builds. makepng can now write ICC profiles + where the length has not been extended to a multiple of 4, and pngtest + now intercepts all libpng errors, allowing the previously-introduced + "--strict test" on no warnings to actually work. + Improved ICC profile handling including cHRM chunk generation and fixed + Cygwin+MSVC build errors. The ICC profile handling now includes more + checking. Several errors that caused rejection of the profile are now + handled with a warning in such a way that the invalid profiles will be + read by default in release (but not pre-RC) builds but will not be + written by default. The easy part of handling the cHRM chunk is written, + where the ICC profile contains the required data. The more difficult + part plus guessing a gAMA value requires code to pass selected RGB values + through the profile. + +Version 1.6.0beta30 [October 24, 2012] + Changed ICC profile matrix/vector types to not depend on array type rules. + By the ANSI-C standard the new types should be identical to the previous + versions, and all known versions of gcc tested with the previous versions + except for GCC-4.2.1 work with this version. The change makes the ANSI-C + rule that const applied to an array of elements applies instead to the + elements in the array moot by explicitly applying const to the base + elements of the png_icc_matrix and png_icc_vector types. The accidental + (harmless) 'const' previously applied to the parameters of two of the + functions have also been removed. + Added a work around for GCC 4.2 optimization bug. + Marked the broken (bad white point) original HP sRGB profiles correctly and + correct comments. + Added -DZ_SOLO to contrib/pngminim/*/makefile to work with zlib-1.2.7 + Use /MDd for vstudio debug builds. Also added pngunkown to the vstudio + builds, fixed build errors and corrected a minor exit code error in + pngvalid if the 'touch' file name is invalid. + Add updated WARNING file to projects/vstudio from libpng 1.5/vstudio + Fixed build when using #define PNG_NO_READ_GAMMA in png_do_compose() in + pngrtran.c (Domani Hannes). + +Version 1.6.0beta31 [November 1, 2012] + Undid the erroneous change to vstudio/pngvalid build in libpng-1.6.0beta30. + Made pngvalid so that it will build outside the libpng source tree. + Made builds -DPNG_NO_READ_GAMMA compile (the unit tests still fail). + Made PNG_NO_READ_GAMMA switch off interfaces that depend on READ_GAMMA. + Prior to 1.6.0 switching off READ_GAMMA did unpredictable things to the + interfaces that use it (specifically, png_do_background in 1.4 would + simply display composite for grayscale images but do composition + with the incorrect arithmetic for color ones). In 1.6 the semantic + of -DPNG_NO_READ_GAMMA is changed to simply disable any interface that + depends on it; this obliges people who set it to consider whether they + really want it off if they happen to use any of the interfaces in + question (typically most users who disable it won't). + Fixed GUIDs in projects/vstudio. Some were duplicated or missing, + resulting in VS2010 having to update the files. + Removed non-working ICC profile support code that was mostly added to + libpng-1.6.0beta29 and beta30. There was too much code for too little + gain; implementing full ICC color correction may be desireable but is left + up to applications. + +Version 1.6.0beta32 [November 25, 2012] + Fixed an intermittent SEGV in pngstest due to an uninitialized array element. + Added the ability for contrib/libtests/makepng.c to make a PNG with just one + color. This is useful for debugging pngstest color inaccuracy reports. + Fixed error checking in the simplified write API (Olaf van der Spek) + Made png_user_version_check() ok to use with libpng version 1.10.x and later. + +Version 1.6.0beta33 [December 15, 2012] + Fixed typo in png.c (PNG_SET_CHUNK_MALLOC_MAX should be PNG_CHUNK_MALLOC_MAX) + that causes the MALLOC_MAX limit not to work (John Bowler) + Change png_warning() to png_app_error() in pngwrite.c and comment the + fall-through condition. + Change png_warning() to png_app_warning() in png_write_tRNS(). + Rearranged the ARM-NEON optimizations: Isolated the machine specific code + to the hardware subdirectory and added comments to pngrutil.c so that + implementors of other optimizations know what to do. + Fixed cases of unquoted DESTDIR in Makefile.am + Rebuilt Makefile.in, etc., with autoconf-2.69 and automake-1.12.5. + +Version 1.6.0beta34 [December 19, 2012] + Cleaned up whitespace in the synopsis portion of the manpage "libpng.3" + Disassembled the version number in scripts/options.awk (necessary for + building on SunOs). + +Version 1.6.0beta35 [December 23, 2012] + Made default Zlib compression settings be configurable. This adds #defines to + pnglibconf.h to control the defaults. + Fixed Windows build issues, enabled ARM compilation. Various warnings issued + by earlier versions of GCC fixed for Cygwin and Min/GW (which both use old + GCCs.) ARM support is enabled by default in zlib.props (unsupported by + Microsoft) and ARM compilation is made possible by deleting the check for + x86. The test programs cannot be run because they are not signed. + +Version 1.6.0beta36 [January 2, 2013] + Discontinued distributing libpng-1.x.x.tar.bz2. + Discontinued distributing libpng-1.7.0-1.6.0-diff.txt and similar. + Rebuilt configure with autoconf-2.69 (inadvertently not done in beta33) + Fixed 'make distcheck' on SUN OS - libpng.so was not being removed + +Version 1.6.0beta37 [January 10, 2013] + Fixed conceivable but difficult to repro overflow. Also added two test + programs to generate and test a PNG which should have the problem. + +Version 1.6.0beta39 [January 19, 2013] + Again corrected attempt at overflow detection in png_set_unknown_chunks() + (CVE-2013-7353). Added overflow detection in png_set_sPLT() and + png_set_text_2() (CVE-2013-7354). + +Version 1.6.0beta40 [January 20, 2013] + Use consistent handling of overflows in text, sPLT and unknown png_set_* APIs + +Version 1.6.0rc01 [January 26, 2013] + No changes. + +Version 1.6.0rc02 [February 4, 2013] + Added png_get_palette_max() function. + +Version 1.6.0rc03 [February 5, 2013] + Fixed the png_get_palette_max API. + +Version 1.6.0rc04 [February 7, 2013] + Turn serial tests back on (recently turned off by autotools upgrade). + +Version 1.6.0rc05 [February 8, 2013] + Update manual about png_get_palette_max(). + +Version 1.6.0rc06 [February 9, 2013] + Fixed missing dependency in --prefix builds The intermediate + internal 'prefix.h' file can only be generated correctly after + pnglibconf.h, however the dependency was not in Makefile.am. The + symptoms are unpredictable depending on the order make chooses to + build pngprefix.h and pnglibconf.h, often the error goes unnoticed + because there is a system pnglibconf.h to use instead. + +Version 1.6.0rc07 [February 10, 2013] + Enclosed the new png_get_palette_max in #ifdef PNG_GET_PALETTE_MAX_SUPPORTED + block, and revised pnglibconf.h and pnglibconf.h.prebuilt accordingly. + +Version 1.6.0rc08 [February 10, 2013] + Fix typo in png.h #ifdef + +Version 1.6.0 [February 14, 2013] + No changes. + +Version 1.6.1beta01 [February 16, 2013] + Made symbol prefixing work with the ARM neon optimizations. Also allow + pngpriv.h to be included for preprocessor definitions only, so it can + be used in non-C/C++ files. Back ported from libpng 1.7. + Made sRGB check numbers consistent. + Ported libpng 1.5 options.awk/dfn file handling to 1.6, fixed one bug. + Removed cc -E workround, corrected png_get_palette_max API Tested on + SUN OS cc 5.9, which demonstrates the tokenization problem previously + avoided by using /lib/cpp. Since all .dfn output is now protected in + double quotes unless it is to be macro substituted the fix should + work everywhere. + Enabled parallel tests - back ported from libpng-1.7. + scripts/pnglibconf.dfa formatting improvements back ported from libpng17. + Fixed a race condition in the creation of the build 'scripts' directory + while building with a parallel make. + Use approved/supported Android method to check for NEON, use Linux/POSIX + 1003.1 API to check /proc/self/auxv avoiding buffer allocation and other + library calls (ported from libpng15). + +Version 1.6.1beta02 [February 19, 2013] + Use parentheses more consistently in "#if defined(MACRO)" tests. + Folded long lines. + Reenabled code to allow zero length PLTE chunks for MNG. + +Version 1.6.1beta03 [February 22, 2013] + Fixed ALIGNED_MEMORY support. + Added a new configure option: + --enable-arm-neon=always will stop the run-time checks. New checks + within arm/arm_init.c will cause the code not to be compiled unless + __ARM_NEON__ is set. This should make it fail safe (if someone asks + for it on then the build will fail if it can't be done.) + Updated the INSTALL document. + +Version 1.6.1beta04 [February 27, 2013] + Revised INSTALL to recommend using CPPFLAGS instead of INCLUDES. + Revised scripts/makefile.freebsd to respect ZLIBLIB and ZLIBINC. + Revised scripts/dfn.awk to work with the buggy MSYS awk that has trouble + with CRLF line endings. + +Version 1.6.1beta05 [March 1, 2013] + Avoid a possible memory leak in contrib/gregbook/readpng.c + +Version 1.6.1beta06 [March 4, 2013] + Better documentation of unknown handling API interactions. + Corrected Android builds and corrected libpng.vers with symbol + prefixing. It also makes those tests compile and link on Android. + Added an API png_set_option() to set optimization options externally, + providing an alternative and general solution for the non-portable + run-time tests used by the ARM Neon code, using the PNG_ARM_NEON option. + The order of settings vs options in pnglibconf.h is reversed to allow + settings to depend on options and options can now set (or override) the + defaults for settings. + +Version 1.6.1beta07 [March 7, 2013] + Corrected simplified API default gamma for color-mapped output, added + a flag to change default. In 1.6.0 when the simplified API was used + to produce color-mapped output from an input image with no gamma + information the gamma assumed for the input could be different from + that assumed for non-color-mapped output. In particular 16-bit depth + input files were assumed to be sRGB encoded, whereas in the 'direct' + case they were assumed to have linear data. This was an error. The + fix makes the simplified API treat all input files the same way and + adds a new flag to the png_image::flags member to allow the + application/user to specify that 16-bit files contain sRGB data + rather than the default linear. + Fixed bugs in the pngpixel and makepng test programs. + +Version 1.6.1beta08 [March 7, 2013] + Fixed CMakelists.txt to allow building a single variant of the library + (Claudio Bley): + Introduced a PNG_LIB_TARGETS variable that lists all activated library + targets. It is an error if this variable ends up empty, ie. you have + to build at least one library variant. + Made the *_COPY targets only depend on library targets actually being build. + Use PNG_LIB_TARGETS to unify a code path. + Changed the CREATE_SYMLINK macro to expect the full path to a file as the + first argument. When symlinking the filename component of that path is + determined and used as the link target. + Use copy_if_different in the CREATE_SYMLINK macro. + +Version 1.6.1beta09 [March 13, 2013] + Eliminated two warnings from the Intel C compiler. The warnings are + technically valid, although a reasonable treatment of division would + show it to be incorrect. + +Version 1.6.1rc01 [March 21, 2013] + No changes. + +Version 1.6.1 [March 28, 2013] + No changes. + +Version 1.6.2beta01 [April 14, 2013] + Updated documentation of 1.5.x to 1.6.x changes in iCCP chunk handling. + Fixed incorrect warning of excess deflate data. End condition - the + warning would be produced if the end of the deflate stream wasn't read + in the last row. The warning is harmless. + Corrected the test on user transform changes on read. It was in the + png_set of the transform function, but that doesn't matter unless the + transform function changes the rowbuf size, and that is only valid if + transform_info is called. + Corrected a misplaced closing bracket in contrib/libtests/pngvalid.c + (Flavio Medeiros). + Corrected length written to uncompressed iTXt chunks (Samuli Suominen). + Bug was introduced in libpng-1.6.0. + +Version 1.6.2rc01 [April 18, 2013] + Added contrib/tools/fixitxt.c, to repair the erroneous iTXt chunk length + written by libpng-1.6.0 and 1.6.1. + Disallow storing sRGB information when the sRGB is not supported. + +Version 1.6.2rc02 [April 18, 2013] + Merge pngtest.c with libpng-1.7.0 + +Version 1.6.2rc03 [April 22, 2013] + Trivial spelling cleanup. + +Version 1.6.2rc04 and 1.6.2rc05 [omitted] + +Version 1.6.2rc06 [April 24, 2013] + Reverted to version 1.6.2rc03. Recent changes to arm/neon support + have been ported to libpng-1.7.0beta09 and will reappear in version + 1.6.3beta01. + +Version 1.6.2 [April 25, 2013] + No changes. + +Version 1.6.3beta01 [April 25, 2013] + Revised stack marking in arm/filter_neon.S and configure.ac. + Ensure that NEON filter stuff is completely disabled when switched 'off'. + Previously the ARM NEON specific files were still built if the option + was switched 'off' as opposed to being explicitly disabled. + +Version 1.6.3beta02 [April 26, 2013] + Test for 'arm*' not just 'arm' in the host_cpu configure variable. + Rebuilt the configure scripts. + +Version 1.6.3beta03 [April 30, 2013] + Expanded manual paragraph about writing private chunks, particularly + the need to call png_set_keep_unknown_chunks() when writing them. + Avoid dereferencing NULL pointer possibly returned from + png_create_write_struct() (Andrew Church). + +Version 1.6.3beta05 [May 9, 2013] + Calculate our own zlib windowBits when decoding rather than trusting the + CMF bytes in the PNG datastream. + Added an option to force maximum window size for inflating, which was + the behavior of libpng15 and earlier, via a new PNG_MAXIMUM_INFLATE_WINDOW + option for png_set_options(). + Added png-fix-itxt and png-fix-too-far-back to the built programs and + removed warnings from the source code and timepng that are revealed as + a result. + Detect wrong libpng versions linked to png-fix-too-far-back, which currently + only works with libpng versions that can be made to reliably fail when + the deflate data contains an out-of-window reference. This means only + 1.6 and later. + Fixed gnu issues: g++ needs a static_cast, gcc 4.4.7 has a broken warning + message which it is easier to work round than ignore. + Updated contrib/pngminus/pnm2png.c (Paul Stewart): + Check for EOF + Ignore "#" delimited comments in input file to pnm2png.c. + Fixed whitespace handling + Added a call to png_set_packing() + Initialize dimension values so if sscanf fails at least we have known + invalid values. + Attempt to detect configuration issues with png-fix-too-far-back, which + requires both the correct libpng and the correct zlib to function + correctly. + Check ZLIB_VERNUM for mismatches, enclose #error in quotes + Added information in the documentation about problems with and fixes for + the bad CRC and bad iTXt chunk situations. + +Version 1.6.3beta06 [May 12, 2013] + Allow contrib/pngminus/pnm2png.c to compile without WRITE_INVERT and + WRITE_PACK supported (writes error message that it can't read P1 or + P4 PBM files). + Improved png-fix-too-far-back usage message, added --suffix option. + Revised contrib/pngminim/*/makefile to generate pnglibconf.h with the + right zlib header files. + Separated CPPFLAGS and CFLAGS in contrib/pngminim/*/makefile + +Version 1.6.3beta07 [June 8, 2013] + Removed a redundant test in png_set_IHDR(). + Added set(CMAKE_CONFIGURATION_TYPES ...) to CMakeLists.txt (Andrew Hundt) + Deleted set(CMAKE_BUILD_TYPE) block from CMakeLists.txt + Enclose the prototypes for the simplified write API in + #ifdef PNG_STDIO_SUPPORTED/#endif + Make ARM NEON support work at compile time (not just configure time). + This moves the test on __ARM_NEON__ into pngconf.h to avoid issues when + using a compiler that compiles for multiple architectures at one time. + Removed PNG_FILTER_OPTIMIZATIONS and PNG_ARM_NEON_SUPPORTED from + pnglibconf.h, allowing more of the decisions to be made internally + (pngpriv.h) during the compile. Without this, symbol prefixing is broken + under certain circumstances on ARM platforms. Now only the API parts of + the optimizations ('check' vs 'api') are exposed in the public header files + except that the new setting PNG_ARM_NEON_OPT documents how libpng makes the + decision about whether or not to use the optimizations. + Protect symbol prefixing against CC/CPPFLAGS/CFLAGS useage. + Previous iOS/Xcode fixes for the ARM NEON optimizations moved the test + on __ARM_NEON__ from configure time to compile time. This breaks symbol + prefixing because the definition of the special png_init_filter_functions + call was hidden at configure time if the relevant compiler arguments are + passed in CFLAGS as opposed to CC. This change attempts to avoid all + the confusion that would result by declaring the init function even when + it is not used, so that it will always get prefixed. + +Version 1.6.3beta08 [June 18, 2013] + Revised libpng.3 so that "doclifter" can process it. + +Version 1.6.3beta09 [June 27, 2013] + Revised example.c to illustrate use of PNG_DEFAULT_sRGB and PNG_GAMMA_MAC_18 + as parameters for png_set_gamma(). These have been available since + libpng-1.5.4. + Renamed contrib/tools/png-fix-too-far-back.c to pngfix.c and revised it + to check all compressed chunks known to libpng. + +Version 1.6.3beta10 [July 5, 2013] + Updated documentation to show default behavior of benign errors correctly. + Only compile ARM code when PNG_READ_SUPPORTED is defined. + Fixed undefined behavior in contrib/tools/pngfix.c and added new strip + option. pngfix relied on undefined behavior and even a simple change from + gcc to g++ caused it to fail. The new strip option 'unsafe' has been + implemented and is the default if --max is given. Option names have + been clarified, with --strip=transform now stripping the bKGD chunk, + which was stripped previously with --strip=unused. + Added all documented chunk types to pngpriv.h + Unified pngfix.c source with libpng17. + +Version 1.6.3rc01 [July 11, 2013] + No changes. + +Version 1.6.3 [July 18, 2013] + Revised manual about changes in iTXt chunk handling made in libpng-1.6.0. + Added "/* SAFE */" comments in pngrutil.c and pngrtran.c where warnings + may be erroneously issued by code-checking applications. + +Version 1.6.4beta01 [August 21, 2013] + Added information about png_set_options() to the manual. + Delay calling png_init_filter_functions() until a row with nonzero filter + is found. + +Version 1.6.4beta02 [August 30, 2013] + Fixed inconsistent conditional compilation of png_chunk_unknown_handling() + prototype, definition, and usage. Made it depend on + PNG_HANDLE_AS_UNKNOWN_SUPPORTED everywhere. + +Version 1.6.4rc01 [September 5, 2013] + No changes. + +Version 1.6.4 [September 12, 2013] + No changes. + +Version 1.6.5 [September 14, 2013] + Removed two stray lines of code from arm/arm_init.c. + +Version 1.6.6 [September 16, 2013] + Removed two stray lines of code from arm/arm_init.c, again. + +Version 1.6.7beta01 [September 30, 2013] + Revised unknown chunk code to correct several bugs in the NO_SAVE_/NO_WRITE + combination + Allow HANDLE_AS_UNKNOWN to work when other options are configured off. Also + fixed the pngminim makefiles to work when $(MAKEFLAGS) contains stuff + which terminates the make options (as by default in recent versions of + Gentoo). + Avoid up-cast warnings in pngvalid.c. On ARM the alignment requirements of + png_modifier are greater than that of png_store and as a consequence + compilation of pngvalid.c results in a warning about increased alignment + requirements because of the bare cast to (png_modifier*). The code is safe, + because the pointer is known to point to a stack allocated png_modifier, + but this change avoids the warning. + Fixed default behavior of ARM_NEON_API. If the ARM NEON API option was + compiled without the CHECK option it defaulted to on, not off. + Check user callback behavior in pngunknown.c. Previous versions compiled + if SAVE_UNKNOWN was not available but did nothing since the callback + was never implemented. + Merged pngunknown.c with 1.7 version and back ported 1.7 improvements/fixes + +Version 1.6.7beta02 [October 12, 2013] + Made changes for compatibility with automake 1.14: + 1) Added the 'compile' program to the list of programs that must be cleaned + in autogen.sh + 2) Added 'subdir-objects' which causes .c files in sub-directories to be + compiled such that the corresponding .o files are also in the + sub-directory. This is because automake 1.14 warns that the + current behavior of compiling to the top level directory may be removed + in the future. + 3) Updated dependencies on pnglibconf.h to match the new .o locations and + added all the files in contrib/libtests and contrib/tools that depend + on pnglibconf.h + 4) Added 'BUILD_SOURCES = pnglibconf.h'; this is the automake recommended + way of handling the dependencies of sources that are machine generated; + unfortunately it only works if the user does 'make all' or 'make check', + so the dependencies (3) are still required. + Cleaned up (char*) casts of zlib messages. The latest version of the Intel C + compiler complains about casting a string literal as (char*), so copied the + treatment of z_const from the library code into pngfix.c + Simplified error message code in pngunknown. The simplification has the + useful side effect of avoiding a bogus warning generated by the latest + version of the Intel C compiler (it objects to + condition ? string-literal : string-literal). + Make autogen.sh work with automake 1.13 as well as 1.14. Do this by always + removing the 1.14 'compile' script but never checking for it. + +Version 1.6.7beta03 [October 19, 2013] + Added ARMv8 support (James Yu ). Added file + arm/filter_neon_intrinsics.c; enable with -mfpu=neon. + Revised pngvalid to generate size images with as many filters as it can + manage, limited by the number of rows. + Cleaned up ARM NEON compilation handling. The tests are now in pngpriv.h + and detect the broken GCC compilers. + +Version 1.6.7beta04 [October 26, 2013] + Allow clang derived from older GCC versions to use ARM intrinsics. This + causes all clang builds that use -mfpu=neon to use the intrinsics code, + not the assembler code. This has only been tested on iOS 7. It may be + necessary to exclude some earlier clang versions but this seems unlikely. + Changed NEON implementation selection mechanism. This allows assembler + or intrinsics to be turned on at compile time during the build by defining + PNG_ARM_NEON_IMPLEMENTATION to the correct value (2 or 1). This macro + is undefined by default and the build type is selected in pngpriv.h. + +Version 1.6.7rc01 [November 2, 2013] + No changes. + +Version 1.6.7rc02 [November 7, 2013] + Fixed #include in filter_neon_intrinsics.c and ctype macros. The ctype char + checking macros take an unsigned char argument, not a signed char. + +Version 1.6.7 [November 14, 2013] + No changes. + +Version 1.6.8beta01 [November 24, 2013] + Moved prototype for png_handle_unknown() in pngpriv.h outside of + the #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED/#endif block. + Added "-Wall" to CFLAGS in contrib/pngminim/*/makefile + Conditionally compile some unused functions reported by -Wall in + pngminim. + Fixed 'minimal' builds. Various obviously useful minimal configurations + don't build because of missing contrib/libtests test programs and + overly complex dependencies in scripts/pnglibconf.dfa. This change + adds contrib/conftest/*.dfa files that can be used in automatic build + scripts to ensure that these configurations continue to build. + Enabled WRITE_INVERT and WRITE_PACK in contrib/pngminim/encoder. + Fixed pngvalid 'fail' function declaration on the Intel C Compiler. + This reverts to the previous 'static' implementation and works round + the 'unused static function' warning by using PNG_UNUSED(). + +Version 1.6.8beta02 [November 30, 2013] + Removed or marked PNG_UNUSED some harmless "dead assignments" reported + by clang scan-build. + Changed tabs to 3 spaces in png_debug macros and changed '"%s"m' + to '"%s" m' to improve portability among compilers. + Changed png_free_default() to free() in pngtest.c + +Version 1.6.8rc01 [December 12, 2013] + Tidied up pngfix inits and fixed pngtest no-write builds. + +Version 1.6.8rc02 [December 14, 2013] + Handle zero-length PLTE chunk or NULL palette with png_error() + instead of png_chunk_report(), which by default issues a warning + rather than an error, leading to later reading from a NULL pointer + (png_ptr->palette) in png_do_expand_palette(). This is CVE-2013-6954 + and VU#650142. Libpng-1.6.1 through 1.6.7 are vulnerable. + Libpng-1.6.0 and earlier do not have this bug. + +Version 1.6.8 [December 19, 2013] + No changes. + +Version 1.6.9beta01 [December 26, 2013] + Bookkeeping: Moved functions around (no changes). Moved transform + function definitions before the place where they are called so that + they can be made static. Move the intrapixel functions and the + grayscale palette builder out of the png?tran.c files. The latter + isn't a transform function and is no longer used internally, and the + former MNG specific functions are better placed in pngread/pngwrite.c + Made transform implementation functions static. This makes the internal + functions called by png_do_{read|write}_transformations static. On an + x86-64 DLL build (Gentoo Linux) this reduces the size of the text + segment of the DLL by 1208 bytes, about 0.6%. It also simplifies + maintenance by removing the declarations from pngpriv.h and allowing + easier changes to the internal interfaces. + Rebuilt configure scripts with automake-1.14.1 and autoconf-2.69 + in the tar distributions. + +Version 1.6.9beta02 [January 1, 2014] + Added checks for libpng 1.5 to pngvalid.c. This supports the use of + this version of pngvalid in libpng 1.5 + Merged with pngvalid.c from libpng-1.7 changes to create a single + pngvalid.c + Removed #error macro from contrib/tools/pngfix.c (Thomas Klausner). + Merged pngrio.c, pngtrans.c, pngwio.c, and pngerror.c with libpng-1.7.0 + Merged libpng-1.7.0 changes to make no-interlace configurations work + with test programs. + Revised pngvalid.c to support libpng 1.5, which does not support the + PNG_MAXIMUM_INFLATE_WINDOW option, so #define it out when appropriate in + pngvalid.c + Allow unversioned links created on install to be disabled in configure. + In configure builds 'make install' changes/adds links like png.h + and libpng.a to point to the newly installed, versioned, files (e.g. + libpng17/png.h and libpng17.a). Three new configure options and some + rearrangement of Makefile.am allow creation of these links to be disabled. + +Version 1.6.9beta03 [January 10, 2014] + Removed potentially misleading warning from png_check_IHDR(). + +Version 1.6.9beta04 [January 20, 2014] + Updated scripts/makefile.* to use CPPFLAGS (Cosmin). + Added clang attribute support (Cosmin). + +Version 1.6.9rc01 [January 28, 2014] + No changes. + +Version 1.6.9rc02 [January 30, 2014] + Quiet an uninitialized memory warning from VC2013 in png_get_png(). + +Version 1.6.9 [February 6, 2014] + +Version 1.6.10beta01 [February 9, 2014] + Backported changes from libpng-1.7.0beta30 and beta31: + Fixed a large number of instances where PNGCBAPI was omitted from + function definitions. + Added pngimage test program for png_read_png() and png_write_png() + with two new test scripts. + Removed dependence on !PNG_READ_EXPAND_SUPPORTED for calling + png_set_packing() in png_read_png(). + Fixed combination of ~alpha with shift. On read invert alpha, processing + occurred after shift processing, which causes the final values to be + outside the range that should be produced by the shift. Reversing the + order on read makes the two transforms work together correctly and mirrors + the order used on write. + Do not read invalid sBIT chunks. Previously libpng only checked sBIT + values on write, so a malicious PNG writer could therefore cause + the read code to return an invalid sBIT chunk, which might lead to + application errors or crashes. Such chunks are now skipped (with + chunk_benign_error). + Make png_read_png() and png_write_png() prototypes in png.h depend + upon PNG_READ_SUPPORTED and PNG_WRITE_SUPPORTED. + Support builds with unsupported PNG_TRANSFORM_* values. All of the + PNG_TRANSFORM_* values are always defined in png.h and, because they + are used for both read and write in some cases, it is not reliable + to #if out ones that are totally unsupported. This change adds error + detection in png_read_image() and png_write_image() to do a + png_app_error() if the app requests something that cannot be done + and it adds corresponding code to pngimage.c to handle such options + by not attempting to test them. + +Version 1.6.10beta02 [February 23, 2014] + Moved redefines of png_error(), png_warning(), png_chunk_error(), + and png_chunk_warning() from pngpriv.h to png.h to make them visible + to libpng-calling applications. + Moved OS dependent code from arm/arm_init.c, to allow the included + implementation of the ARM NEON discovery function to be set at + build-time and provide sample implementations from the current code in the + contrib/arm-neon subdirectory. The __linux__ code has also been changed to + compile and link on Android by using /proc/cpuinfo, and the old linux code + is in contrib/arm-neon/linux-auxv.c. The new code avoids POSIX and Linux + dependencies apart from opening /proc/cpuinfo and is C90 compliant. + Check for info_ptr == NULL early in png_read_end() so we don't need to + run all the png_handle_*() and depend on them to return if info_ptr == NULL. + This improves the performance of png_read_end(png_ptr, NULL) and makes + it more robust against future programming errors. + Check for __has_extension before using it in pngconf.h, to + support older Clang versions (Jeremy Sequoia). + Treat CRC error handling with png_set_crc_action(), instead of with + png_set_benign_errors(), which has been the case since libpng-1.6.0beta18. + Use a user warning handler in contrib/gregbook/readpng2.c instead of default, + so warnings will be put on stderr even if libpng has CONSOLE_IO disabled. + Added png_ptr->process_mode = PNG_READ_IDAT_MODE in png_push_read_chunk + after recognizing the IDAT chunk, which avoids an infinite loop while + reading a datastream whose first IDAT chunk is of zero-length. + This fixes CERT VU#684412 and CVE-2014-0333. + Don't recognize known sRGB profiles as sRGB if they have been hacked, + but don't reject them and don't issue a copyright violation warning. + +Version 1.6.10beta03 [February 25, 2014] + Moved some documentation from png.h to libpng.3 and libpng-manual.txt + Minor editing of contrib/arm-neon/README and contrib/examples/*.c + +Version 1.6.10rc01 [February 27, 2014] + Fixed typos in the manual and in scripts/pnglibconf.dfa (CFLAGS -> CPPFLAGS + and PNG_USR_CONFIG -> PNG_USER_CONFIG). + +Version 1.6.10rc02 [February 28, 2014] + Removed unreachable return statement after png_chunk_error() + in pngrutil.c + +Version 1.6.10rc03 [March 4, 2014] + Un-deprecated png_data_freer(). + +Version 1.6.10 [March 6, 2014] + No changes. + +Version 1.6.11beta01 [March 17, 2014] + Use "if (value != 0)" instead of "if (value)" consistently. + Changed ZlibSrcDir from 1.2.5 to 1.2.8 in projects/vstudio. + Moved configuration information from the manual to the INSTALL file. + +Version 1.6.11beta02 [April 6, 2014] + Removed #if/#else/#endif from inside two pow() calls in pngvalid.c because + they were handled improperly by Portland Group's PGI-14.1 - PGI-14.3 + when using its "__builtin_pow()" function. + Silence 'unused parameter' build warnings (Cosmin Truta). + $(CP) is now used alongside $(RM_F). Also, use 'copy' instead of 'cp' + where applicable, and applied other minor makefile changes (Cosmin). + Don't warn about invalid dimensions exceeding user limits (Cosmin). + Allow an easy replacement of the default pre-built configuration + header with a custom header, via the make PNGLIBCONF_H_PREBUILT + macro (Cosmin). + +Version 1.6.11beta03 [April 6, 2014] + Fixed a typo in pngrutil.c, introduced in libpng-1.5.6, that interferes + with "blocky" expansion of sub-8-bit interlaced PNG files (Eric Huss). + Optionally use __builtin_bswap16() in png_do_swap(). + +Version 1.6.11beta04 [April 19, 2014] + Made progressive reading of interlaced images consistent with the + behavior of the sequential reader and consistent with the manual, by + moving some code out of the PNG_READ_INTERLACING_SUPPORTED blocks. The + row_callback now receives the proper pass number and unexpanded rows, when + png_combine_row() isn't built or used, and png_set_interlace_handling() + is not called. + Allow PNG_sRGB_PROFILE_CHECKING = (-1) to mean no sRGB profile checking. + +Version 1.6.11beta05 [April 26, 2014] + Do not reject ICC V2 profiles that lack padding (Kai-Uwe Behrmann). + Relocated closing bracket of the sRGB profile test loop to avoid getting + "Not recognizing known sRGB profile that has been edited" warning for + ICC V2 profiles that lack the MD5 signature in the profile header. + +Version 1.6.11beta06 [May 19, 2014] + Added PNG_SKIP_sRGB_CHECK_PROFILE choice for png_set_option(). + +Version 1.6.11rc01 [May 27, 2014] + No changes. + +Version 1.6.11rc02 [June 3, 2014] + Test ZLIB_VERNUM instead of PNG_ZLIB_VERNUM in contrib/tools/pngfix.c + +Version 1.6.11 [June 5, 2014] + No changes. + +Version 1.6.12rc01 [June 6, 2014] + Relocated new code from 1.6.11beta06 in png.c to a point after the + declarations (Max Stepin). + +Version 1.6.12rc02 [June 7, 2014] + Changed file permissions of contrib/tools/intgamma.sh, + test-driver, and compile from 0644 to 0755 (Cosmin). + +Version 1.6.12rc03 [June 8, 2014] + Ensure "__has_attribute()" macro exists before trying to use it with + old clang compilers (MacPorts Ticket #43939). + +Version 1.6.12 [June 12, 2014] + No changes. + +Version 1.6.13beta01 [July 4, 2014] + Quieted -Wsign-compare and -Wclobber compiler warnings in + contrib/pngminus/*.c + Added "(void) png_ptr;" where needed in contrib/gregbook to quiet + compiler complaints about unused pointers. + Split a long output string in contrib/gregbook/rpng2-x.c. + Added "PNG_SET_OPTION" requirement for sRGB chunk support to pnglibconf.dfa, + Needed for write-only support (John Bowler). + Changed "if defined(__ARM_NEON__)" to + "if (defined(__ARM_NEON__) || defined(__ARM_NEON))" (James Wu). + Fixed clang no-warning builds: png_digit was defined but never used. + +Version 1.6.13beta02 [July 21, 2014] + Fixed an incorrect separator ("/" should be "\") in scripts/makefile.vcwin32 + (bug report from Wolfgang S. Kechel). Bug was introduced in libpng-1.6.11. + Also fixed makefile.bc32, makefile.bor, makefile.msc, makefile.intel, and + makefile.tc3 similarly. + +Version 1.6.13beta03 [August 3, 2014] + Removed scripts/makefile.elf. It has not worked since libpng-1.5.0beta14 + due to elimination of the PNG_FUNCTION_EXPORT and PNG_DATA_EXPORT + definitions from pngconf.h. + Ensure that CMakeLists.txt makes the target "lib" directory before making + symbolic link into it (SourceForge bug report #226 by Rolf Timmermans). + +Version 1.6.13beta04 [August 8, 2014] + Added opinion that the ECCN (Export Control Classification Number) for + libpng is EAR99 to the README file. + Eliminated use of "$<" in makefile explicit rules, when copying + $PNGLIBCONF_H_PREBUILT. This does not work on some versions of make; + bug introduced in libpng version 1.6.11. + +Version 1.6.13rc01 [August 14, 2014] + Made "ccopts" agree with "CFLAGS" in scripts/makefile.hp* and makefile.*sunu + +Version 1.6.13 [August 21, 2014] + No changes. + +Version 1.6.14beta01 [September 14, 2014] + Guard usage of png_ptr->options with #ifdef PNG_SET_OPTION_SUPPORTED. + Do not build contrib/tools/pngfix.c when PNG_SETJMP_NOT_SUPPORTED, + to allow "make" to complete without setjmp support (bug report by + Claudio Fontana) + Add "#include " to contrib/tools/pngfix.c (John Bowler) + +Version 1.6.14beta02 [September 18, 2014] + Use nanosleep() instead of usleep() in contrib/gregbook/rpng2-x.c + because usleep() is deprecated. + Define usleep() in contrib/gregbook/rpng2-x.c if not already defined + in unistd.h and nanosleep() is not available; fixes error introduced + in libpng-1.6.13. + Disable floating point exception handling in pngvalid.c when + PNG_FLOATING_ARITHMETIC is not supported (bug report by "zootus + at users.sourceforge.net"). + +Version 1.6.14beta03 [September 19, 2014] + Define FE_DIVBYZERO, FE_INVALID, and FE_OVERFLOW in pngvalid.c if not + already defined. Revert floating point exception handling in pngvalid.c + to version 1.6.14beta01 behavior. + +Version 1.6.14beta04 [September 27, 2014] + Fixed incorrect handling of the iTXt compression flag in pngrutil.c + (bug report by Shunsaku Hirata). Bug was introduced in libpng-1.6.0. + +Version 1.6.14beta05 [October 1, 2014] + Added "option READ_iCCP enables READ_COMPRESSED_TEXT" to pnglibconf.dfa + +Version 1.6.14beta06 [October 5, 2014] + Removed unused "text_len" parameter from private function png_write_zTXt(). + Conditionally compile some code in png_deflate_claim(), when + PNG_WARNINGS_SUPPORTED and PNG_ERROR_TEXT_SUPPORTED are disabled. + Replaced repeated code in pngpread.c with PNG_PUSH_SAVE_BUFFER_IF_FULL. + Added "chunk iTXt enables TEXT" and "chunk zTXt enables TEXT" + to pnglibconf.dfa. + Removed "option READ_COMPRESSED_TEXT enables READ_TEXT" from pnglibconf.dfa, + to make it possible to configure a libpng that supports iCCP but not TEXT. + +Version 1.6.14beta07 [October 7, 2014] + Removed "option WRITE_COMPRESSED_TEXT enables WRITE_TEXT" from pnglibconf.dfa + Only mark text chunks as written after successfully writing them. + +Version 1.6.14rc01 [October 15, 2014] + Fixed some typos in comments. + +Version 1.6.14rc02 [October 17, 2014] + Changed png_convert_to_rfc_1123() to png_convert_to_rfc_1123_buffer() + in the manual, to reflect the change made in libpng-1.6.0. + Updated README file to explain that direct access to the png_struct + and info_struct members has not been permitted since libpng-1.5.0. + +Version 1.6.14 [October 23, 2014] + No changes. + +Version 1.6.15beta01 [October 29, 2014] + Changed "if (!x)" to "if (x == 0)" and "if (x)" to "if (x != 0)" + Simplified png_free_data(). + Added missing "ptr = NULL" after some instances of png_free(). + +Version 1.6.15beta02 [November 1, 2014] + Changed remaining "if (!x)" to "if (x == 0)" and "if (x)" to "if (x != 0)" + +Version 1.6.15beta03 [November 3, 2014] + Added PNG_USE_ARM_NEON configuration flag (Marcin Juszkiewicz). + +Version 1.6.15beta04 [November 4, 2014] + Removed new PNG_USE_ARM_NEON configuration flag and made a one-line + revision to configure.ac to support ARM on aarch64 instead (John Bowler). + +Version 1.6.15beta05 [November 5, 2014] + Use png_get_libpng_ver(NULL) instead of PNG_LIBPNG_VER_STRING in + example.c, pngtest.c, and applications in the contrib directory. + Avoid out-of-bounds memory access in png_user_version_check(). + Simplified and future-proofed png_user_version_check(). + Fixed GCC unsigned int->float warnings. Various versions of GCC + seem to generate warnings when an unsigned value is implicitly + converted to double. This is probably a GCC bug but this change + avoids the issue by explicitly converting to (int) where safe. + Free all allocated memory in pngimage. The file buffer cache was left + allocated at the end of the program, harmless but it causes memory + leak reports from clang. + Fixed array size calculations to avoid warnings. At various points + in the code the number of elements in an array is calculated using + sizeof. This generates a compile time constant of type (size_t) which + is then typically assigned to an (unsigned int) or (int). Some versions + of GCC on 64-bit systems warn about the apparent narrowing, even though + the same compiler does apparently generate the correct, in-range, + numeric constant. This adds appropriate, safe, casts to make the + warnings go away. + +Version 1.6.15beta06 [November 6, 2014] + Reverted use png_get_libpng_ver(NULL) instead of PNG_LIBPNG_VER_STRING + in the manual, example.c, pngtest.c, and applications in the contrib + directory. It was incorrect advice. + +Version 1.6.15beta07 [November 7, 2014] + Removed #ifdef PNG_16BIT_SUPPORTED/#endif around png_product2(); it is + needed by png_reciprocal2(). + Added #ifdef PNG_16BIT_SUPPORTED/#endif around png_log16bit() and + png_do_swap(). + Changed all "#endif /* PNG_FEATURE_SUPPORTED */" to "#endif /* FEATURE */" + +Version 1.6.15beta08 [November 8, 2014] + More housecleaning in *.h + +Version 1.6.15rc01 [November 13, 2014] + +Version 1.6.15rc02 [November 14, 2014] + The macros passed in the command line to Borland make were ignored if + similarly-named macros were already defined in makefiles. This behavior + is different from POSIX make and other make programs. Surround the + macro definitions with ifndef guards (Cosmin). + +Version 1.6.15rc03 [November 16, 2014] + Added "-D_CRT_SECURE_NO_WARNINGS" to CFLAGS in scripts/makefile.vcwin32. + Removed the obsolete $ARCH variable from scripts/makefile.darwin. + +Version 1.6.15 [November 20, 2014] + No changes. + +Version 1.6.16beta01 [December 14, 2014] + Added ".align 2" to arm/filter_neon.S to support old GAS assemblers that + don't do alignment correctly. + Revised Makefile.am and scripts/symbols.dfn to work with MinGW/MSYS + (Bob Friesenhahn). + +Version 1.6.16beta02 [December 15, 2014] + Revised Makefile.am and scripts/*.dfn again to work with MinGW/MSYS; + renamed scripts/*.dfn to scripts/*.c (John Bowler). + +Version 1.6.16beta03 [December 21, 2014] + Quiet a "comparison always true" warning in pngstest.c (John Bowler). + +Version 1.6.16rc01 [December 21, 2014] + Restored a test on width that was removed from png.c at libpng-1.6.9 + (Bug report by Alex Eubanks, CVE-2015-0973). + +Version 1.6.16rc02 [December 21, 2014] + Undid the update to pngrutil.c in 1.6.16rc01. + +Version 1.6.16rc03 [December 21, 2014] + Fixed an overflow in png_combine_row() with very wide interlaced images + (Bug report and fix by John Bowler, CVE-2014-9495). + +Version 1.6.16 [December 22, 2014] + No changes. + +Version 1.6.17beta01 [January 29, 2015] + Removed duplicate PNG_SAFE_LIMITS_SUPPORTED handling from pngconf.h + Corrected the width limit calculation in png_check_IHDR(). + Removed user limits from pngfix. Also pass NULL pointers to + png_read_row to skip the unnecessary row de-interlace stuff. + Added testing of png_set_packing() to pngvalid.c + Regenerated configure scripts in the *.tar distributions with libtool-2.4.4 + Implement previously untested cases of libpng transforms in pngvalid.c + Fixed byte order in png_do_read_filler() with 16-bit input. Previously + the high and low bytes of the filler, from png_set_filler() or from + png_set_add_alpha(), were read in the wrong order. + Made the check for out-of-range values in png_set_tRNS() detect + values that are exactly 2^bit_depth, and work on 16-bit platforms. + Merged some parts of libpng-1.6.17beta01 and libpng-1.7.0beta47. + Added #ifndef __COVERITY__ where needed in png.c, pngrutil.c and + pngset.c to avoid warnings about dead code. + Added "& 0xff" to many instances of expressions that are typecast + to (png_byte), to avoid Coverity warnings. + +Version 1.6.17beta02 [February 7, 2015] + Work around one more Coverity-scan dead-code warning. + Do not build png_product2() when it is unused. + +Version 1.6.17beta03 [February 17, 2015] + Display user limits in the output from pngtest. + Eliminated the PNG_SAFE_LIMITS macro and restored the 1-million-column + and 1-million-row default limits in pnglibconf.dfa, that can be reset + by the user at build time or run time. This provides a more robust + defense against DOS and as-yet undiscovered overflows. + +Version 1.6.17beta04 [February 21, 2015] + Added PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED macro, on by default. + Allow user to call png_get_IHDR() with NULL arguments (Reuben Hawkins). + Rebuilt configure scripts with automake-1.15 and libtool-2.4.6 + +Version 1.6.17beta05 [February 25, 2015] + Restored compiling of png_reciprocal2 with PNG_NO_16BIT. + +Version 1.6.17beta06 [February 27, 2015] + Moved png_set_filter() prototype into a PNG_WRITE_SUPPORTED block + of png.h. + Avoid runtime checks when converting integer to png_byte with + Visual Studio (Sergey Kosarevsky) + +Version 1.6.17rc01 [March 4, 2015] + No changes. + +Version 1.6.17rc02 [March 9, 2015] + Removed some comments that the configure script did not handle + properly from scripts/pnglibconf.dfa and pnglibconf.h.prebuilt. + Free the unknown_chunks structure even when it contains no data. + +Version 1.6.17rc03 [March 12, 2015] + Updated CMakeLists.txt to add OSX framework, change YES/NO to ON/OFF + for consistency, and remove some useless tests (Alexey Petruchik). + +Version 1.6.17rc04 [March 16, 2015] + Remove pnglibconf.h, pnglibconf.c, and pnglibconf.out instead of + pnglibconf.* in "make clean" (Cosmin). + Fix bug in calculation of maxbits, in png_write_sBIT, introduced + in libpng-1.6.17beta01 (John Bowler). + +Version 1.6.17rc05 [March 21, 2015] + Define PNG_FILTER_* and PNG_FILTER_VALUE_* in png.h even when WRITE + is not supported (John Bowler). This fixes an error introduced in + libpng-1.6.17beta06. + Reverted "& 0xff" additions of version 1.6.17beta01. Libpng passes + the Coverity scan without them. + +Version 1.6.17rc06 [March 23, 2015] + Remove pnglibconf.dfn and pnglibconf.pre with "make clean". + Reformatted some "&0xff" instances to "& 0xff". + Fixed simplified 8-bit-linear to sRGB alpha. The calculated alpha + value was wrong. It's not clear if this affected the final stored + value; in the obvious code path the upper and lower 8-bits of the + alpha value were identical and the alpha was truncated to 8-bits + rather than dividing by 257 (John Bowler). + +Version 1.6.17 [March 26, 2015] + No changes. + +Version 1.6.18beta01 [April 1, 2015] + Removed PNG_SET_CHUNK_[CACHE|MALLOC]_LIMIT_SUPPORTED macros. They + have been combined with PNG_SET_USER_LIMITS_SUPPORTED (resolves + bug report by Andrew Church). + Fixed rgb_to_gray checks and added tRNS checks to pngvalid.c. This + fixes some arithmetic errors that caused some tests to fail on + some 32-bit platforms (Bug reports by Peter Breitenlohner [i686] + and Petr Gajdos [i586]). + +Version 1.6.18beta02 [April 26, 2015] + Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler + (Bug report by Viktor Szakats). + +Version 1.6.18beta03 [May 6, 2015] + Replaced "unexpected" with an integer (0xabadca11) in pngset.c + where a long was expected, to avoid a compiler warning when PNG_DEBUG > 1. + Added contrib/examples/simpleover.c, to demonstrate how to handle + alpha compositing of multiple images, using the "simplified API" + and an example PNG generation tool, contrib/examples/genpng.c + (John Bowler). + +Version 1.6.18beta04 [May 20, 2015] + PNG_RELEASE_BUILD replaces tests where the code depended on the build base + type and can be defined on the command line, allowing testing in beta + builds (John Bowler). + Avoid Coverity issue 80858 (REVERSE NULL) in pngtest.c PNG_DEBUG builds. + Avoid a harmless potential integer overflow in png_XYZ_from_xy() (Bug + report from Christopher Ferris). + +Version 1.6.18beta05 [May 31, 2015] + Backport filter selection code from libpng-1.7.0beta51, to combine + sub_row, up_row, avg_row, and paeth_row into try_row and tst_row. + Changed png_voidcast(), etc., to voidcast(), etc., in contrib/tools/pngfix.c + to avoid confusion with the libpng private macros. + Fixed old cut&paste bug in the weighted filter selection code in + pngwutil.c, introduced in libpng-0.95, March 1997. + +Version 1.6.18beta06 [June 1, 2015] + Removed WRITE_WEIGHTED_FILTERED code, to save a few kbytes of the + compiled library size. It never worked properly and as far as we can + tell, no one uses it. The png_set_filter_heuristics() and + png_set_filter_heuristics_fixed() APIs are retained but deprecated + and do nothing. + +Version 1.6.18beta07 [June 6, 2015] + Removed non-working progressive reader 'skip' function. This + function has apparently never been used. It was implemented + to support back-door modification of png_struct in libpng-1.4.x + but (because it does nothing and cannot do anything) was apparently + never tested (John Bowler). + Fixed cexcept.h in which GCC 5 now reports that one of the auto + variables in the Try macro needs to be volatile to prevent value + being lost over the setjmp (John Bowler). + Fixed NO_WRITE_FILTER and -Wconversion build breaks (John Bowler). + Fix g++ build breaks (John Bowler). + Quieted some Coverity issues in pngfix.c, png-fix-itxt.c, pngvalid.c, + pngstest.c, and pngimage.c. Most seem harmless, but png-fix-itxt + would only work with iTXt chunks with length 255 or less. + Added #ifdef's to contrib/examples programs so people don't try + to compile them without the minimum required support enabled + (suggested by Flavio Medeiros). + +Version 1.6.18beta08 [June 30, 2015] + Eliminated the final two Coverity defects (insecure temporary file + handling in contrib/libtests/pngstest.c; possible overflow of + unsigned char in contrib/tools/png-fix-itxt.c). To use the "secure" + file handling, define PNG_USE_MKSTEMP, otherwise "tmpfile()" will + be used. + Removed some unused WEIGHTED_FILTER macros from png.h and pngstruct.h + +Version 1.6.18beta09 [July 5, 2015] + Removed some useless typecasts from contrib/tools/png-fix-itxt.c + Fixed a new signed-unsigned comparison in pngrtran.c (Max Stepin). + Replaced arbitrary use of 'extern' with #define PNG_LINKAGE_*. To + preserve API compatibility, the new defines all default to "extern" + (requested by Jan Nijtmans). + +Version 1.6.18rc01 [July 9, 2015] + Belatedly added Mans Rullgard and James Yu to the list of Contributing + Authors. + +Version 1.6.18rc02 [July 12, 2015] + Restored unused FILTER_HEURISTIC macros removed at libpng-1.6.18beta08 + to png.h to avoid compatibility warnings. + +Version 1.6.18rc03 [July 15, 2015] + Minor changes to the man page + +Version 1.6.18 [July 23, 2015] + No changes. + +Version 1.6.19beta01 [July 30, 2015] + Updated obsolete information about the simplified API macros in the + manual pages (Bug report by Arc Riley). + Avoid potentially dereferencing NULL info_ptr in png_info_init_3(). + Rearranged png.h to put the major sections in the same order as + in libpng17. + Eliminated unused PNG_COST_SHIFT, PNG_WEIGHT_SHIFT, PNG_COST_FACTOR, and + PNG_WEIGHT_FACTOR macros. + Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler + (Bug report by Viktor Szakats). Several warnings remain and are + unavoidable, where we test for overflow. + Fixed potential leak of png_pixels in contrib/pngminus/pnm2png.c + Fixed uninitialized variable in contrib/gregbook/rpng2-x.c + +Version 1.6.19beta02 [August 19, 2015] + Moved config.h.in~ from the "libpng_autotools_files" list to the + "libpng_autotools_extra" list in autogen.sh because it was causing a + false positive for missing files (bug report by Robert C. Seacord). + Removed unreachable "break" statements in png.c, pngread.c, and pngrtran.c + to suppress clang warnings (Bug report by Viktor Szakats). + Fixed some bad links in the man page. + Changed "n bit" to "n-bit" in comments. + Added signed/unsigned 16-bit safety net. This removes the dubious + 0x8000 flag definitions on 16-bit systems. They aren't supported + yet the defs *probably* work, however it seems much safer to do this + and be advised if anyone, contrary to advice, is building libpng 1.6 + on a 16-bit system. It also adds back various switch default clauses + for GCC; GCC errors out if they are not present (with an appropriately + high level of warnings). + Safely convert num_bytes to a png_byte in png_set_sig_bytes() (Robert + Seacord). + Fixed the recently reported 1's complement security issue by replacing + the value that is illegal in the PNG spec, in both signed and unsigned + values, with 0. Illegal unsigned values (anything greater than or equal + to 0x80000000) can still pass through, but since these are not illegal + in ANSI-C (unlike 0x80000000 in the signed case) the checking that + occurs later can catch them (John Bowler). + +Version 1.6.19beta03 [September 26, 2015] + Fixed png_save_int_32 when int is not 2's complement (John Bowler). + Updated libpng16 with all the recent test changes from libpng17, + including changes to pngvalid.c to ensure that the original, + distributed, version of contrib/visupng/cexcept.h can be used + (John Bowler). + pngvalid contains the correction to the use of SAVE/STORE_ + UNKNOWN_CHUNKS; a bug revealed by changes in libpng 1.7. More + tests contain the --strict option to detect warnings and the + pngvalid-standard test has been corrected so that it does not + turn on progressive-read. There is a separate test which does + that. (John Bowler) + Also made some signed/unsigned fixes. + Make pngstest error limits version specific. Splitting the machine + generated error structs out to a file allows the values to be updated + without changing pngstest.c itself. Since libpng 1.6 and 1.7 have + slightly different error limits this simplifies maintenance. The + makepngs.sh script has also been updated to more accurately reflect + current problems in libpng 1.7 (John Bowler). + Incorporated new test PNG files into make check. tests/pngstest-* + are changed so that the new test files are divided into 8 groups by + gamma and alpha channel. These tests have considerably better code + and pixel-value coverage than contrib/pngsuite; however,coverage is + still incomplete (John Bowler). + Removed the '--strict' in 1.6 because of the double-gamma-correction + warning, updated pngstest-errors.h for the errors detected with the + new contrib/testspngs PNG test files (John Bowler). + +Version 1.6.19beta04 [October 15, 2015] + Worked around rgb-to-gray issues in libpng 1.6. The previous + attempts to ignore the errors in the code aren't quite enough to + deal with the 'channel selection' encoding added to libpng 1.7; abort. + pngvalid.c is changed to drop this encoding in prior versions. + Fixed 'pow' macros in pngvalid.c. It is legal for 'pow' to be a + macro, therefore the argument list cannot contain preprocessing + directives. Make sure pow is a function where this happens. This is + a minimal safe fix, the issue only arises in non-performance-critical + code (bug report by Curtis Leach, fix by John Bowler). + Added sPLT support to pngtest.c + +Version 1.6.19rc01 [October 23, 2015] + No changes. + +Version 1.6.19rc02 [October 31, 2015] + Prevent setting or writing over-length PLTE chunk (Cosmin Truta). + Silently truncate over-length PLTE chunk while reading. + Libpng incorrectly calculated the output rowbytes when the application + decreased either the number of channels or the bit depth (or both) in + a user transform. This was safe; libpng overallocated buffer space + (potentially by quite a lot; up to 4 times the amount required) but, + from 1.5.4 on, resulted in a png_error (John Bowler). + +Version 1.6.19rc03 [November 3, 2015] + Fixed some inconsequential cut-and-paste typos in png_set_cHRM_XYZ_fixed(). + Clarified COPYRIGHT information to state explicitly that versions + are derived from previous versions. + Removed much of the long list of previous versions from png.h and + libpng.3. + +Version 1.6.19rc04 [November 5, 2015] + Fixed new bug with CRC error after reading an over-length palette + (bug report by Cosmin Truta). + +Version 1.6.19 [November 12, 2015] + Cleaned up coding style in png_handle_PLTE(). + Send comments/corrections/commendations to png-mng-implement at lists.sf.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement diff --git a/3rdparty/libpng/CMakeLists.txt b/3rdparty/libpng/CMakeLists.txt index 84fd2bc2b..fee9c99bb 100644 --- a/3rdparty/libpng/CMakeLists.txt +++ b/3rdparty/libpng/CMakeLists.txt @@ -3,10 +3,23 @@ # # ---------------------------------------------------------------------------- -if(NEON) - project(${PNG_LIBRARY} ASM) +if(ENABLE_NEON) + project(${PNG_LIBRARY} C ASM) else() - project(${PNG_LIBRARY}) + project(${PNG_LIBRARY} C) +endif() + +if(NOT WIN32) + find_library(M_LIBRARY + NAMES m + PATHS /usr/lib /usr/local/lib + ) + if(NOT M_LIBRARY) + message(STATUS "math lib 'libm' not found; floating point support disabled") + endif() +else() + # not needed on windows + set(M_LIBRARY "") endif() ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" ${ZLIB_INCLUDE_DIRS}) @@ -14,17 +27,16 @@ ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" ${ZLIB_INCLUDE_DIRS}) file(GLOB lib_srcs *.c) file(GLOB lib_hdrs *.h) -if(NEON AND ARM) - list(APPEND lib_srcs arm/filter_neon.S) - add_definitions(-DPNG_ARM_NEON) + +if(ENABLE_NEON) + list(APPEND lib_srcs arm/arm_init.c arm/filter_neon.S arm/filter_neon_intrinsics.c) + add_definitions(-DPNG_ARM_NEON_OPT=2) endif() # ---------------------------------------------------------------------------------- # Define the library target: # ---------------------------------------------------------------------------------- -add_definitions(-DPNG_CONFIGURE_LIBPNG) - if(MSVC) add_definitions(-D_CRT_SECURE_NO_DEPRECATE) endif(MSVC) diff --git a/3rdparty/libpng/LICENSE b/3rdparty/libpng/LICENSE index 3a67d5458..11f6ffe5d 100644 --- a/3rdparty/libpng/LICENSE +++ b/3rdparty/libpng/LICENSE @@ -10,21 +10,18 @@ this sentence. This code is released under the libpng license. -libpng versions 1.2.6, August 15, 2004, through 1.5.12, July 11, 2012, are -Copyright (c) 2004, 2006-2012 Glenn Randers-Pehrson, and are -distributed according to the same disclaimer and license as libpng-1.2.5 -with the following individual added to the list of Contributing Authors - - Cosmin Truta - -libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are -Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are -distributed according to the same disclaimer and license as libpng-1.0.6 -with the following individuals added to the list of Contributing Authors +libpng versions 1.0.7, July 1, 2000, through 1.6.19, November 12, 2015, are +Copyright (c) 2000-2002, 2004, 2006-2015 Glenn Randers-Pehrson, are +derived from libpng-1.0.6, and are distributed according to the same +disclaimer and license as libpng-1.0.6 with the following individuals +added to the list of Contributing Authors: Simon-Pierre Cadieux Eric S. Raymond + Mans Rullgard + Cosmin Truta Gilles Vollant + James Yu and with the following additions to the disclaimer: @@ -36,18 +33,20 @@ and with the following additions to the disclaimer: the user. libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are -Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are -distributed according to the same disclaimer and license as libpng-0.96, -with the following individuals added to the list of Contributing Authors: +Copyright (c) 1998-2000 Glenn Randers-Pehrson, are derived from +libpng-0.96, and are distributed according to the same disclaimer and +license as libpng-0.96, with the following individuals added to the list +of Contributing Authors: Tom Lane Glenn Randers-Pehrson Willem van Schaik libpng versions 0.89, June 1996, through 0.96, May 1997, are -Copyright (c) 1996, 1997 Andreas Dilger -Distributed according to the same disclaimer and license as libpng-0.88, -with the following individuals added to the list of Contributing Authors: +Copyright (c) 1996-1997 Andreas Dilger, are derived from libpng-0.88, +and are distributed according to the same disclaimer and license as +libpng-0.88, with the following individuals added to the list of +Contributing Authors: John Bowler Kevin Bracey @@ -57,7 +56,7 @@ with the following individuals added to the list of Contributing Authors: Tom Tanner libpng versions 0.5, May 1995, through 0.88, January 1996, are -Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. +Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. For the purposes of this copyright and license, "Contributing Authors" is defined as the following set of individuals: @@ -80,13 +79,13 @@ Permission is hereby granted to use, copy, modify, and distribute this source code, or portions hereof, for any purpose, without fee, subject to the following restrictions: -1. The origin of this source code must not be misrepresented. + 1. The origin of this source code must not be misrepresented. -2. Altered versions must be plainly marked as such and must not - be misrepresented as being the original source. + 2. Altered versions must be plainly marked as such and must not + be misrepresented as being the original source. -3. This Copyright notice may not be removed or altered from any - source or altered source distribution. + 3. This Copyright notice may not be removed or altered from any + source or altered source distribution. The Contributing Authors and Group 42, Inc. specifically permit, without fee, and encourage the use of this source code as a component to @@ -94,18 +93,20 @@ supporting the PNG file format in commercial products. If you use this source code in a product, acknowledgment is not required but would be appreciated. +END OF COPYRIGHT NOTICE, DISCLAIMER, and LICENSE. A "png_get_copyright" function is available, for convenient use in "about" boxes and the like: - printf("%s",png_get_copyright(NULL)); + printf("%s", png_get_copyright(NULL)); Also, the PNG logo (in PNG format, of course) is supplied in the files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31). -Libpng is OSI Certified Open Source Software. OSI Certified Open Source is a -certification mark of the Open Source Initiative. +Libpng is OSI Certified Open Source Software. OSI Certified Open Source is +a certification mark of the Open Source Initiative. OSI has not addressed +the additional disclaimers inserted at version 1.0.7. Glenn Randers-Pehrson glennrp at users.sourceforge.net -July 11, 2012 +November 12, 2015 diff --git a/3rdparty/libpng/README b/3rdparty/libpng/README index 3f5b0d6b6..17484e0fd 100644 --- a/3rdparty/libpng/README +++ b/3rdparty/libpng/README @@ -1,11 +1,11 @@ -README for libpng version 1.5.12 - July 11, 2012 (shared library 15.0) +README for libpng version 1.6.19 - November 12, 2015 (shared library 16.0) See the note about version numbers near the top of png.h See INSTALL for instructions on how to install libpng. -Libpng comes in several distribution formats. Get libpng-*.tar.gz, -libpng-*.tar.xz or libpng-*.tar.bz2 if you want UNIX-style line endings -in the text files, or lpng*.zip if you want DOS-style line endings. +Libpng comes in several distribution formats. Get libpng-*.tar.gz or +libpng-*.tar.xz or if you want UNIX-style line endings in the text files, +or lpng*.7z or lpng*.zip if you want DOS-style line endings. Version 0.89 was the first official release of libpng. Don't let the fact that it's the first release fool you. The libpng library has been in @@ -23,18 +23,25 @@ earlier versions if you are using a shared library. The type of the png_uint_32, which will affect shared-library applications that use this function. -To avoid problems with changes to the internals of png_info_struct, +To avoid problems with changes to the internals of png info_struct, new APIs have been made available in 0.95 to avoid direct application access to info_ptr. These functions are the png_set_ and png_get_ functions. These functions should be used when accessing/storing the info_struct data, rather than manipulating it directly, to avoid such problems in the future. -It is important to note that the APIs do not make current programs +It is important to note that the APIs did not make current programs that access the info struct directly incompatible with the new -library. However, it is strongly suggested that new programs use -the new APIs (as shown in example.c and pngtest.c), and older programs -be converted to the new format, to facilitate upgrades in the future. +library, through libpng-1.2.x. In libpng-1.4.x, which was meant to +be a transitional release, members of the png_struct and the +info_struct can still be accessed, but the compiler will issue a +warning about deprecated usage. Since libpng-1.5.0, direct access +to these structs is not allowed, and the definitions of the structs +reside in private pngstruct.h and pnginfo.h header files that are not +accessible to applications. It is strongly suggested that new +programs use the new APIs (as shown in example.c and pngtest.c), and +older programs be converted to the new format, to facilitate upgrades +in the future. **** Additions since 0.90 include the ability to compile libpng as a @@ -77,17 +84,21 @@ compression library that is useful for more things than just PNG files. You can use zlib as a drop-in replacement for fread() and fwrite() if you are so inclined. -zlib should be available at the same place that libpng is, or at. -ftp://ftp.info-zip.org/pub/infozip/zlib +zlib should be available at the same place that libpng is, or at zlib.net. You may also want a copy of the PNG specification. It is available as an RFC, a W3C Recommendation, and an ISO/IEC Standard. You can find these at http://www.libpng.org/pub/png/documents/ This code is currently being archived at libpng.sf.net in the -[DOWNLOAD] area, and on CompuServe, Lib 20 (PNG SUPPORT) -at GO GRAPHSUP. If you can't find it in any of those places, -e-mail me, and I'll help you find it. +[DOWNLOAD] area, and at ftp://ftp.simplesystems.org. If you can't find it +in any of those places, e-mail me, and I'll help you find it. + +I am not a lawyer, but I believe that the Export Control Classification +Number (ECCN) for libpng is EAR99, which means not subject to export +controls or International Traffic in Arms Regulations (ITAR) because it +is open source, publicly available software, that does not contain any +encryption software. See the EAR, paragraphs 734.3(b)(3) and 734.7(b). If you have any code changes, requests, problems, etc., please e-mail them to me. Also, I'd appreciate any make files or project files, @@ -105,7 +116,7 @@ based in a large way on Guy's and Andreas' earlier work), and the PNG development group. Send comments/corrections/commendations to png-mng-implement at -lists.sourceforge.net (subscription required; visit +lists.sourceforge.net (subscription required; visit https://lists.sourceforge.net/lists/listinfo/png-mng-implement to subscribe) or to glennrp at users.sourceforge.net @@ -123,7 +134,7 @@ and ...". If in doubt, send questions to me. I'll bounce them to others, if necessary. Please do not send suggestions on how to change PNG. We have -been discussing PNG for sixteen years now, and it is official and +been discussing PNG for twenty years now, and it is official and finished. If you have suggestions for libpng, however, I'll gladly listen. Even if your suggestion is not used immediately, it may be used later. @@ -167,23 +178,25 @@ Files in this distribution: pngwrite.c => High-level write functions pngwtran.c => Write data transformations pngwutil.c => Write utility functions + arm => Contains optimized code for the ARM platform contrib => Contributions + examples => Example programs gregbook => source code for PNG reading and writing, from Greg Roelofs' "PNG: The Definitive Guide", O'Reilly, 1999 - msvctest => Builds and runs pngtest using a MSVC workspace - pngminus => Simple pnm2png and png2pnm programs - pngsuite => Test images - visupng => Contains a MSVC workspace for VisualPng + libtests => Test programs + pngminim => Minimal decoder, encoder, and progressive decoder + programs demonstrating use of pngusr.dfa + pngminus => Simple pnm2png and png2pnm programs + pngsuite => Test images + tools => Various tools + visupng => Contains a MSVC workspace for VisualPng projects => Contains project files and workspaces for building a DLL - cbuilder5 => Contains a Borland workspace for building - libpng and zlib - visualc6 => Contains a Microsoft Visual C++ (MSVC) - workspace for building libpng and zlib + owatcom => Contains a WATCOM project for building libpng visualc71 => Contains a Microsoft Visual C++ (MSVC) workspace for building libpng and zlib - xcode => Contains an Apple xcode + vstudio => Contains a Microsoft Visual C++ (MSVC) workspace for building libpng and zlib scripts => Directory containing scripts for building libpng: (see scripts/README.txt for the list of scripts) diff --git a/3rdparty/libpng/arm/arm_init.c b/3rdparty/libpng/arm/arm_init.c new file mode 100644 index 000000000..fb3d50d04 --- /dev/null +++ b/3rdparty/libpng/arm/arm_init.c @@ -0,0 +1,134 @@ + +/* arm_init.c - NEON optimised filter functions + * + * Copyright (c) 2014 Glenn Randers-Pehrson + * Written by Mans Rullgard, 2011. + * Last changed in libpng 1.6.16 [December 22, 2014] + * + * This code is released under the libpng license. + * For conditions of distribution and use, see the disclaimer + * and license in png.h + */ +/* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are + * called. + */ +#define _POSIX_SOURCE 1 + +#include "../pngpriv.h" + +#ifdef PNG_READ_SUPPORTED + +#if PNG_ARM_NEON_OPT > 0 +#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */ +/* WARNING: it is strongly recommended that you do not build libpng with + * run-time checks for CPU features if at all possible. In the case of the ARM + * NEON instructions there is no processor-specific way of detecting the + * presence of the required support, therefore run-time detection is extremely + * OS specific. + * + * You may set the macro PNG_ARM_NEON_FILE to the file name of file containing + * a fragment of C source code which defines the png_have_neon function. There + * are a number of implementations in contrib/arm-neon, but the only one that + * has partial support is contrib/arm-neon/linux.c - a generic Linux + * implementation which reads /proc/cpufino. + */ +#ifndef PNG_ARM_NEON_FILE +# ifdef __linux__ +# define PNG_ARM_NEON_FILE "contrib/arm-neon/linux.c" +# endif +#endif + +#ifdef PNG_ARM_NEON_FILE + +#include /* for sig_atomic_t */ +static int png_have_neon(png_structp png_ptr); +#include PNG_ARM_NEON_FILE + +#else /* PNG_ARM_NEON_FILE */ +# error "PNG_ARM_NEON_FILE undefined: no support for run-time ARM NEON checks" +#endif /* PNG_ARM_NEON_FILE */ +#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */ + +#ifndef PNG_ALIGNED_MEMORY_SUPPORTED +# error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED" +#endif + +void +png_init_filter_functions_neon(png_structp pp, unsigned int bpp) +{ + /* The switch statement is compiled in for ARM_NEON_API, the call to + * png_have_neon is compiled in for ARM_NEON_CHECK. If both are defined + * the check is only performed if the API has not set the NEON option on + * or off explicitly. In this case the check controls what happens. + * + * If the CHECK is not compiled in and the option is UNSET the behavior prior + * to 1.6.7 was to use the NEON code - this was a bug caused by having the + * wrong order of the 'ON' and 'default' cases. UNSET now defaults to OFF, + * as documented in png.h + */ +#ifdef PNG_ARM_NEON_API_SUPPORTED + switch ((pp->options >> PNG_ARM_NEON) & 3) + { + case PNG_OPTION_UNSET: + /* Allow the run-time check to execute if it has been enabled - + * thus both API and CHECK can be turned on. If it isn't supported + * this case will fall through to the 'default' below, which just + * returns. + */ +#endif /* PNG_ARM_NEON_API_SUPPORTED */ +#ifdef PNG_ARM_NEON_CHECK_SUPPORTED + { + static volatile sig_atomic_t no_neon = -1; /* not checked */ + + if (no_neon < 0) + no_neon = !png_have_neon(pp); + + if (no_neon) + return; + } +#ifdef PNG_ARM_NEON_API_SUPPORTED + break; +#endif +#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */ + +#ifdef PNG_ARM_NEON_API_SUPPORTED + default: /* OFF or INVALID */ + return; + + case PNG_OPTION_ON: + /* Option turned on */ + break; + } +#endif + + /* IMPORTANT: any new external functions used here must be declared using + * PNG_INTERNAL_FUNCTION in ../pngpriv.h. This is required so that the + * 'prefix' option to configure works: + * + * ./configure --with-libpng-prefix=foobar_ + * + * Verify you have got this right by running the above command, doing a build + * and examining pngprefix.h; it must contain a #define for every external + * function you add. (Notice that this happens automatically for the + * initialization function.) + */ + pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon; + + if (bpp == 3) + { + pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon; + pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon; + pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = + png_read_filter_row_paeth3_neon; + } + + else if (bpp == 4) + { + pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon; + pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon; + pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = + png_read_filter_row_paeth4_neon; + } +} +#endif /* PNG_ARM_NEON_OPT > 0 */ +#endif /* READ */ diff --git a/3rdparty/libpng/arm/filter_neon.S b/3rdparty/libpng/arm/filter_neon.S index 27428283e..3b061d6bb 100644 --- a/3rdparty/libpng/arm/filter_neon.S +++ b/3rdparty/libpng/arm/filter_neon.S @@ -1,18 +1,36 @@ /* filter_neon.S - NEON optimised filter functions * - * Copyright (c) 2011 Glenn Randers-Pehrson + * Copyright (c) 2014 Glenn Randers-Pehrson * Written by Mans Rullgard, 2011. + * Last changed in libpng 1.6.16 [December 22, 2014] * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ +/* This is required to get the symbol renames, which are #defines, and the + * definitions (or not) of PNG_ARM_NEON_OPT and PNG_ARM_NEON_IMPLEMENTATION. + */ +#define PNG_VERSION_INFO_ONLY +#include "../pngpriv.h" + #if defined(__linux__) && defined(__ELF__) .section .note.GNU-stack,"",%progbits /* mark stack as non-executable */ #endif +#ifdef PNG_READ_SUPPORTED + +/* Assembler NEON support - only works for 32-bit ARM (i.e. it does not work for + * ARM64). The code in arm/filter_neon_intrinsics.c supports ARM64, however it + * only works if -mfpu=neon is specified on the GCC command line. See pngpriv.h + * for the logic which sets PNG_USE_ARM_NEON_ASM: + */ +#if PNG_ARM_NEON_IMPLEMENTATION == 2 /* hand-coded assembler */ + +#if PNG_ARM_NEON_OPT > 0 + #ifdef __ELF__ # define ELF #else @@ -29,6 +47,13 @@ ELF .size \name, . - \name .purgem endfunc .endm .text + + /* Explicitly specifying alignment here because some versions of + * GAS don't align code correctly. This is harmless in correctly + * written versions of GAS. + */ + .align 2 + .if \export .global \name .endif @@ -41,12 +66,12 @@ func png_read_filter_row_sub4_neon, export=1 ldr r3, [r0, #4] @ rowbytes vmov.i8 d3, #0 1: - vld4.32 {d4[],d5[],d6[],d7[]}, [r1] + vld4.32 {d4[],d5[],d6[],d7[]}, [r1,:128] vadd.u8 d0, d3, d4 vadd.u8 d1, d0, d5 vadd.u8 d2, d1, d6 vadd.u8 d3, d2, d7 - vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1]! + vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1,:128]! subs r3, r3, #16 bgt 1b @@ -67,7 +92,7 @@ func png_read_filter_row_sub3_neon, export=1 vadd.u8 d1, d0, d5 vext.8 d7, d23, d23, #1 vld1.8 {q11}, [r0], r12 - vst1.32 {d0[0]}, [r1], r2 + vst1.32 {d0[0]}, [r1,:32], r2 vadd.u8 d2, d1, d6 vst1.32 {d1[0]}, [r1], r2 vadd.u8 d3, d2, d7 @@ -82,10 +107,10 @@ endfunc func png_read_filter_row_up_neon, export=1 ldr r3, [r0, #4] @ rowbytes 1: - vld1.8 {q0}, [r1] - vld1.8 {q1}, [r2]! + vld1.8 {q0}, [r1,:128] + vld1.8 {q1}, [r2,:128]! vadd.u8 q0, q0, q1 - vst1.8 {q0}, [r1]! + vst1.8 {q0}, [r1,:128]! subs r3, r3, #16 bgt 1b @@ -96,8 +121,8 @@ func png_read_filter_row_avg4_neon, export=1 ldr r12, [r0, #4] @ rowbytes vmov.i8 d3, #0 1: - vld4.32 {d4[],d5[],d6[],d7[]}, [r1] - vld4.32 {d16[],d17[],d18[],d19[]},[r2]! + vld4.32 {d4[],d5[],d6[],d7[]}, [r1,:128] + vld4.32 {d16[],d17[],d18[],d19[]},[r2,:128]! vhadd.u8 d0, d3, d16 vadd.u8 d0, d0, d4 vhadd.u8 d1, d0, d17 @@ -106,7 +131,7 @@ func png_read_filter_row_avg4_neon, export=1 vadd.u8 d2, d2, d6 vhadd.u8 d3, d2, d19 vadd.u8 d3, d3, d7 - vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1]! + vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1,:128]! subs r12, r12, #16 bgt 1b @@ -133,7 +158,7 @@ func png_read_filter_row_avg3_neon, export=1 vadd.u8 d1, d1, d5 vext.8 d7, d23, d23, #1 vld1.8 {q11}, [r0], lr - vst1.32 {d0[0]}, [r1], r4 + vst1.32 {d0[0]}, [r1,:32], r4 vhadd.u8 d2, d1, d18 vst1.32 {d1[0]}, [r1], r4 vext.8 d19, d21, d21, #1 @@ -169,8 +194,8 @@ func png_read_filter_row_paeth4_neon, export=1 vmov.i8 d3, #0 vmov.i8 d20, #0 1: - vld4.32 {d4[],d5[],d6[],d7[]}, [r1] - vld4.32 {d16[],d17[],d18[],d19[]},[r2]! + vld4.32 {d4[],d5[],d6[],d7[]}, [r1,:128] + vld4.32 {d16[],d17[],d18[],d19[]},[r2,:128]! paeth d0, d3, d16, d20 vadd.u8 d0, d0, d4 paeth d1, d0, d17, d16 @@ -180,7 +205,7 @@ func png_read_filter_row_paeth4_neon, export=1 paeth d3, d2, d19, d18 vmov d20, d19 vadd.u8 d3, d3, d7 - vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1]! + vst4.32 {d0[0],d1[0],d2[0],d3[0]},[r1,:128]! subs r12, r12, #16 bgt 1b @@ -203,7 +228,7 @@ func png_read_filter_row_paeth3_neon, export=1 vadd.u8 d0, d0, d22 vext.8 d17, d20, d21, #3 paeth d1, d0, d17, d20 - vst1.32 {d0[0]}, [r1], r4 + vst1.32 {d0[0]}, [r1,:32], r4 vext.8 d6, d22, d23, #6 vadd.u8 d1, d1, d5 vext.8 d18, d20, d21, #6 @@ -223,3 +248,6 @@ func png_read_filter_row_paeth3_neon, export=1 pop {r4,pc} endfunc +#endif /* PNG_ARM_NEON_OPT > 0 */ +#endif /* PNG_ARM_NEON_IMPLEMENTATION == 2 (assembler) */ +#endif /* READ */ diff --git a/3rdparty/libpng/arm/filter_neon_intrinsics.c b/3rdparty/libpng/arm/filter_neon_intrinsics.c new file mode 100644 index 000000000..d42c78890 --- /dev/null +++ b/3rdparty/libpng/arm/filter_neon_intrinsics.c @@ -0,0 +1,373 @@ + +/* filter_neon_intrinsics.c - NEON optimised filter functions + * + * Copyright (c) 2014 Glenn Randers-Pehrson + * Written by James Yu , October 2013. + * Based on filter_neon.S, written by Mans Rullgard, 2011. + * + * Last changed in libpng 1.6.16 [December 22, 2014] + * + * This code is released under the libpng license. + * For conditions of distribution and use, see the disclaimer + * and license in png.h + */ + +#include "../pngpriv.h" + +#ifdef PNG_READ_SUPPORTED + +/* This code requires -mfpu=neon on the command line: */ +#if PNG_ARM_NEON_IMPLEMENTATION == 1 /* intrinsics code from pngpriv.h */ + +#include + +/* libpng row pointers are not necessarily aligned to any particular boundary, + * however this code will only work with appropriate alignment. arm/arm_init.c + * checks for this (and will not compile unless it is done). This code uses + * variants of png_aligncast to avoid compiler warnings. + */ +#define png_ptr(type,pointer) png_aligncast(type *,pointer) +#define png_ptrc(type,pointer) png_aligncastconst(const type *,pointer) + +/* The following relies on a variable 'temp_pointer' being declared with type + * 'type'. This is written this way just to hide the GCC strict aliasing + * warning; note that the code is safe because there never is an alias between + * the input and output pointers. + */ +#define png_ldr(type,pointer)\ + (temp_pointer = png_ptr(type,pointer), *temp_pointer) + +#if PNG_ARM_NEON_OPT > 0 + +void +png_read_filter_row_up_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_bytep rp_stop = row + row_info->rowbytes; + png_const_bytep pp = prev_row; + + for (; rp < rp_stop; rp += 16, pp += 16) + { + uint8x16_t qrp, qpp; + + qrp = vld1q_u8(rp); + qpp = vld1q_u8(pp); + qrp = vaddq_u8(qrp, qpp); + vst1q_u8(rp, qrp); + } +} + +void +png_read_filter_row_sub3_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_bytep rp_stop = row + row_info->rowbytes; + + uint8x16_t vtmp = vld1q_u8(rp); + uint8x8x2_t *vrpt = png_ptr(uint8x8x2_t, &vtmp); + uint8x8x2_t vrp = *vrpt; + + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + for (; rp < rp_stop;) + { + uint8x8_t vtmp1, vtmp2; + uint32x2_t *temp_pointer; + + vtmp1 = vext_u8(vrp.val[0], vrp.val[1], 3); + vdest.val[0] = vadd_u8(vdest.val[3], vrp.val[0]); + vtmp2 = vext_u8(vrp.val[0], vrp.val[1], 6); + vdest.val[1] = vadd_u8(vdest.val[0], vtmp1); + + vtmp1 = vext_u8(vrp.val[1], vrp.val[1], 1); + vdest.val[2] = vadd_u8(vdest.val[1], vtmp2); + vdest.val[3] = vadd_u8(vdest.val[2], vtmp1); + + vtmp = vld1q_u8(rp + 12); + vrpt = png_ptr(uint8x8x2_t, &vtmp); + vrp = *vrpt; + + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[0]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[1]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[2]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[3]), 0); + rp += 3; + } + + PNG_UNUSED(prev_row) +} + +void +png_read_filter_row_sub4_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_bytep rp_stop = row + row_info->rowbytes; + + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + for (; rp < rp_stop; rp += 16) + { + uint32x2x4_t vtmp = vld4_u32(png_ptr(uint32_t,rp)); + uint8x8x4_t *vrpt = png_ptr(uint8x8x4_t,&vtmp); + uint8x8x4_t vrp = *vrpt; + uint32x2x4_t *temp_pointer; + + vdest.val[0] = vadd_u8(vdest.val[3], vrp.val[0]); + vdest.val[1] = vadd_u8(vdest.val[0], vrp.val[1]); + vdest.val[2] = vadd_u8(vdest.val[1], vrp.val[2]); + vdest.val[3] = vadd_u8(vdest.val[2], vrp.val[3]); + vst4_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2x4_t,&vdest), 0); + } + + PNG_UNUSED(prev_row) +} + +void +png_read_filter_row_avg3_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_const_bytep pp = prev_row; + png_bytep rp_stop = row + row_info->rowbytes; + + uint8x16_t vtmp; + uint8x8x2_t *vrpt; + uint8x8x2_t vrp; + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + vtmp = vld1q_u8(rp); + vrpt = png_ptr(uint8x8x2_t,&vtmp); + vrp = *vrpt; + + for (; rp < rp_stop; pp += 12) + { + uint8x8_t vtmp1, vtmp2, vtmp3; + + uint8x8x2_t *vppt; + uint8x8x2_t vpp; + + uint32x2_t *temp_pointer; + + vtmp = vld1q_u8(pp); + vppt = png_ptr(uint8x8x2_t,&vtmp); + vpp = *vppt; + + vtmp1 = vext_u8(vrp.val[0], vrp.val[1], 3); + vdest.val[0] = vhadd_u8(vdest.val[3], vpp.val[0]); + vdest.val[0] = vadd_u8(vdest.val[0], vrp.val[0]); + + vtmp2 = vext_u8(vpp.val[0], vpp.val[1], 3); + vtmp3 = vext_u8(vrp.val[0], vrp.val[1], 6); + vdest.val[1] = vhadd_u8(vdest.val[0], vtmp2); + vdest.val[1] = vadd_u8(vdest.val[1], vtmp1); + + vtmp2 = vext_u8(vpp.val[0], vpp.val[1], 6); + vtmp1 = vext_u8(vrp.val[1], vrp.val[1], 1); + + vtmp = vld1q_u8(rp + 12); + vrpt = png_ptr(uint8x8x2_t,&vtmp); + vrp = *vrpt; + + vdest.val[2] = vhadd_u8(vdest.val[1], vtmp2); + vdest.val[2] = vadd_u8(vdest.val[2], vtmp3); + + vtmp2 = vext_u8(vpp.val[1], vpp.val[1], 1); + + vdest.val[3] = vhadd_u8(vdest.val[2], vtmp2); + vdest.val[3] = vadd_u8(vdest.val[3], vtmp1); + + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[0]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[1]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[2]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[3]), 0); + rp += 3; + } +} + +void +png_read_filter_row_avg4_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_bytep rp_stop = row + row_info->rowbytes; + png_const_bytep pp = prev_row; + + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + for (; rp < rp_stop; rp += 16, pp += 16) + { + uint32x2x4_t vtmp; + uint8x8x4_t *vrpt, *vppt; + uint8x8x4_t vrp, vpp; + uint32x2x4_t *temp_pointer; + + vtmp = vld4_u32(png_ptr(uint32_t,rp)); + vrpt = png_ptr(uint8x8x4_t,&vtmp); + vrp = *vrpt; + vtmp = vld4_u32(png_ptrc(uint32_t,pp)); + vppt = png_ptr(uint8x8x4_t,&vtmp); + vpp = *vppt; + + vdest.val[0] = vhadd_u8(vdest.val[3], vpp.val[0]); + vdest.val[0] = vadd_u8(vdest.val[0], vrp.val[0]); + vdest.val[1] = vhadd_u8(vdest.val[0], vpp.val[1]); + vdest.val[1] = vadd_u8(vdest.val[1], vrp.val[1]); + vdest.val[2] = vhadd_u8(vdest.val[1], vpp.val[2]); + vdest.val[2] = vadd_u8(vdest.val[2], vrp.val[2]); + vdest.val[3] = vhadd_u8(vdest.val[2], vpp.val[3]); + vdest.val[3] = vadd_u8(vdest.val[3], vrp.val[3]); + + vst4_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2x4_t,&vdest), 0); + } +} + +static uint8x8_t +paeth(uint8x8_t a, uint8x8_t b, uint8x8_t c) +{ + uint8x8_t d, e; + uint16x8_t p1, pa, pb, pc; + + p1 = vaddl_u8(a, b); /* a + b */ + pc = vaddl_u8(c, c); /* c * 2 */ + pa = vabdl_u8(b, c); /* pa */ + pb = vabdl_u8(a, c); /* pb */ + pc = vabdq_u16(p1, pc); /* pc */ + + p1 = vcleq_u16(pa, pb); /* pa <= pb */ + pa = vcleq_u16(pa, pc); /* pa <= pc */ + pb = vcleq_u16(pb, pc); /* pb <= pc */ + + p1 = vandq_u16(p1, pa); /* pa <= pb && pa <= pc */ + + d = vmovn_u16(pb); + e = vmovn_u16(p1); + + d = vbsl_u8(d, b, c); + e = vbsl_u8(e, a, d); + + return e; +} + +void +png_read_filter_row_paeth3_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_const_bytep pp = prev_row; + png_bytep rp_stop = row + row_info->rowbytes; + + uint8x16_t vtmp; + uint8x8x2_t *vrpt; + uint8x8x2_t vrp; + uint8x8_t vlast = vdup_n_u8(0); + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + vtmp = vld1q_u8(rp); + vrpt = png_ptr(uint8x8x2_t,&vtmp); + vrp = *vrpt; + + for (; rp < rp_stop; pp += 12) + { + uint8x8x2_t *vppt; + uint8x8x2_t vpp; + uint8x8_t vtmp1, vtmp2, vtmp3; + uint32x2_t *temp_pointer; + + vtmp = vld1q_u8(pp); + vppt = png_ptr(uint8x8x2_t,&vtmp); + vpp = *vppt; + + vdest.val[0] = paeth(vdest.val[3], vpp.val[0], vlast); + vdest.val[0] = vadd_u8(vdest.val[0], vrp.val[0]); + + vtmp1 = vext_u8(vrp.val[0], vrp.val[1], 3); + vtmp2 = vext_u8(vpp.val[0], vpp.val[1], 3); + vdest.val[1] = paeth(vdest.val[0], vtmp2, vpp.val[0]); + vdest.val[1] = vadd_u8(vdest.val[1], vtmp1); + + vtmp1 = vext_u8(vrp.val[0], vrp.val[1], 6); + vtmp3 = vext_u8(vpp.val[0], vpp.val[1], 6); + vdest.val[2] = paeth(vdest.val[1], vtmp3, vtmp2); + vdest.val[2] = vadd_u8(vdest.val[2], vtmp1); + + vtmp1 = vext_u8(vrp.val[1], vrp.val[1], 1); + vtmp2 = vext_u8(vpp.val[1], vpp.val[1], 1); + + vtmp = vld1q_u8(rp + 12); + vrpt = png_ptr(uint8x8x2_t,&vtmp); + vrp = *vrpt; + + vdest.val[3] = paeth(vdest.val[2], vtmp2, vtmp3); + vdest.val[3] = vadd_u8(vdest.val[3], vtmp1); + + vlast = vtmp2; + + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[0]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[1]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[2]), 0); + rp += 3; + vst1_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2_t,&vdest.val[3]), 0); + rp += 3; + } +} + +void +png_read_filter_row_paeth4_neon(png_row_infop row_info, png_bytep row, + png_const_bytep prev_row) +{ + png_bytep rp = row; + png_bytep rp_stop = row + row_info->rowbytes; + png_const_bytep pp = prev_row; + + uint8x8_t vlast = vdup_n_u8(0); + uint8x8x4_t vdest; + vdest.val[3] = vdup_n_u8(0); + + for (; rp < rp_stop; rp += 16, pp += 16) + { + uint32x2x4_t vtmp; + uint8x8x4_t *vrpt, *vppt; + uint8x8x4_t vrp, vpp; + uint32x2x4_t *temp_pointer; + + vtmp = vld4_u32(png_ptr(uint32_t,rp)); + vrpt = png_ptr(uint8x8x4_t,&vtmp); + vrp = *vrpt; + vtmp = vld4_u32(png_ptrc(uint32_t,pp)); + vppt = png_ptr(uint8x8x4_t,&vtmp); + vpp = *vppt; + + vdest.val[0] = paeth(vdest.val[3], vpp.val[0], vlast); + vdest.val[0] = vadd_u8(vdest.val[0], vrp.val[0]); + vdest.val[1] = paeth(vdest.val[0], vpp.val[1], vpp.val[0]); + vdest.val[1] = vadd_u8(vdest.val[1], vrp.val[1]); + vdest.val[2] = paeth(vdest.val[1], vpp.val[2], vpp.val[1]); + vdest.val[2] = vadd_u8(vdest.val[2], vrp.val[2]); + vdest.val[3] = paeth(vdest.val[2], vpp.val[3], vpp.val[2]); + vdest.val[3] = vadd_u8(vdest.val[3], vrp.val[3]); + + vlast = vpp.val[3]; + + vst4_lane_u32(png_ptr(uint32_t,rp), png_ldr(uint32x2x4_t,&vdest), 0); + } +} + +#endif /* PNG_ARM_NEON_OPT > 0 */ +#endif /* PNG_ARM_NEON_IMPLEMENTATION == 1 (intrinsics) */ +#endif /* READ */ diff --git a/3rdparty/libpng/opencv-libpng.patch b/3rdparty/libpng/opencv-libpng.patch index 9bc0b6903..9fac20355 100644 --- a/3rdparty/libpng/opencv-libpng.patch +++ b/3rdparty/libpng/opencv-libpng.patch @@ -1,8 +1,6 @@ -diff --git a/3rdparty/libpng/pngpriv.h b/3rdparty/libpng/pngpriv.h -index 07b2b0b..e7824b8 100644 --- a/3rdparty/libpng/pngpriv.h +++ b/3rdparty/libpng/pngpriv.h -@@ -360,7 +360,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; +@@ -457,7 +457,7 @@ /* Memory model/platform independent fns */ #ifndef PNG_ABORT @@ -11,12 +9,3 @@ index 07b2b0b..e7824b8 100644 # define PNG_ABORT() ExitProcess(0) # else # define PNG_ABORT() abort() -@@ -378,7 +378,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; - # define png_memcpy _fmemcpy - # define png_memset _fmemset - #else --# ifdef _WINDOWS_ /* Favor Windows over C runtime fns */ -+# if defined(_WINDOWS_) && !defined(WINRT) /* Favor Windows over C runtime fns */ - # define CVT_PTR(ptr) (ptr) - # define CVT_PTR_NOCHECK(ptr) (ptr) - # define png_strlen lstrlenA diff --git a/3rdparty/libpng/png.c b/3rdparty/libpng/png.c index 6808c5cb9..6fcfad72e 100644 --- a/3rdparty/libpng/png.c +++ b/3rdparty/libpng/png.c @@ -1,8 +1,8 @@ /* png.c - location for general purpose libpng functions * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -14,7 +14,7 @@ #include "pngpriv.h" /* Generate a compiler error if there is an old png.h in the search path. */ -typedef png_libpng_version_1_5_12 Your_png_h_is_not_version_1_5_12; +typedef png_libpng_version_1_6_19 Your_png_h_is_not_version_1_6_19; /* Tells libpng that we have already handled the first "num_bytes" bytes * of the PNG file signature. If the PNG data is embedded into another @@ -24,17 +24,22 @@ typedef png_libpng_version_1_5_12 Your_png_h_is_not_version_1_5_12; #ifdef PNG_READ_SUPPORTED void PNGAPI -png_set_sig_bytes(png_structp png_ptr, int num_bytes) +png_set_sig_bytes(png_structrp png_ptr, int num_bytes) { + unsigned int nb = (unsigned int)num_bytes; + png_debug(1, "in png_set_sig_bytes"); if (png_ptr == NULL) return; - if (num_bytes > 8) + if (num_bytes < 0) + nb = 0; + + if (nb > 8) png_error(png_ptr, "Too many bytes for PNG signature"); - png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); + png_ptr->sig_bytes = (png_byte)nb; } /* Checks whether the supplied bytes match the PNG signature. We allow @@ -62,52 +67,46 @@ png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check) if (start + num_to_check > 8) num_to_check = 8 - start; - return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); + return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check))); } -#endif /* PNG_READ_SUPPORTED */ +#endif /* READ */ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) /* Function to allocate memory for zlib */ PNG_FUNCTION(voidpf /* PRIVATE */, png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) { - png_voidp ptr; - png_structp p=(png_structp)png_ptr; - png_uint_32 save_flags=p->flags; - png_alloc_size_t num_bytes; + png_alloc_size_t num_bytes = size; if (png_ptr == NULL) - return (NULL); + return NULL; - if (items > PNG_UINT_32_MAX/size) + if (items >= (~(png_alloc_size_t)0)/size) { - png_warning (p, "Potential overflow in png_zalloc()"); - return (NULL); + png_warning (png_voidcast(png_structrp, png_ptr), + "Potential overflow in png_zalloc()"); + return NULL; } - num_bytes = (png_alloc_size_t)items * size; - p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; - ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); - p->flags=save_flags; - - return ((voidpf)ptr); + num_bytes *= items; + return png_malloc_warn(png_voidcast(png_structrp, png_ptr), num_bytes); } /* Function to free memory for zlib */ void /* PRIVATE */ png_zfree(voidpf png_ptr, voidpf ptr) { - png_free((png_structp)png_ptr, (png_voidp)ptr); + png_free(png_voidcast(png_const_structrp,png_ptr), ptr); } /* Reset the CRC variable to 32 bits of 1's. Care must be taken * in case CRC is > 32 bits to leave the top bits 0. */ void /* PRIVATE */ -png_reset_crc(png_structp png_ptr) +png_reset_crc(png_structrp png_ptr) { - /* The cast is safe because the crc is a 32 bit value. */ + /* The cast is safe because the crc is a 32-bit value. */ png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0); } @@ -117,11 +116,11 @@ png_reset_crc(png_structp png_ptr) * trouble of calculating it. */ void /* PRIVATE */ -png_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length) +png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length) { int need_crc = 1; - if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)) + if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0) { if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) @@ -130,33 +129,35 @@ png_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length) else /* critical */ { - if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) need_crc = 0; } - /* 'uLong' is defined as unsigned long, this means that on some systems it is - * a 64 bit value. crc32, however, returns 32 bits so the following cast is - * safe. 'uInt' may be no more than 16 bits, so it is necessary to perform a - * loop here. + /* 'uLong' is defined in zlib.h as unsigned long; this means that on some + * systems it is a 64-bit value. crc32, however, returns 32 bits so the + * following cast is safe. 'uInt' may be no more than 16 bits, so it is + * necessary to perform a loop here. */ - if (need_crc && length > 0) + if (need_crc != 0 && length > 0) { uLong crc = png_ptr->crc; /* Should never issue a warning */ do { - uInt safeLength = (uInt)length; - if (safeLength == 0) - safeLength = (uInt)-1; /* evil, but safe */ + uInt safe_length = (uInt)length; +#ifndef __COVERITY__ + if (safe_length == 0) + safe_length = (uInt)-1; /* evil, but safe */ +#endif - crc = crc32(crc, ptr, safeLength); + crc = crc32(crc, ptr, safe_length); - /* The following should never issue compiler warnings, if they do the + /* The following should never issue compiler warnings; if they do the * target system has characteristics that will probably violate other * assumptions within the libpng code. */ - ptr += safeLength; - length -= safeLength; + ptr += safe_length; + length -= safe_length; } while (length > 0); @@ -166,97 +167,205 @@ png_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length) } /* Check a user supplied version number, called from both read and write - * functions that create a png_struct + * functions that create a png_struct. */ int -png_user_version_check(png_structp png_ptr, png_const_charp user_png_ver) +png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver) { - if (user_png_ver) + /* Libpng versions 1.0.0 and later are binary compatible if the version + * string matches through the second '.'; we must recompile any + * applications that use any older library version. + */ + + if (user_png_ver != NULL) { - int i = 0; + int i = -1; + int found_dots = 0; do { - if (user_png_ver[i] != png_libpng_ver[i]) + i++; + if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i]) png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; - } while (png_libpng_ver[i++]); + if (user_png_ver[i] == '.') + found_dots++; + } while (found_dots < 2 && user_png_ver[i] != 0 && + PNG_LIBPNG_VER_STRING[i] != 0); } else png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; - if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) + if ((png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) != 0) { - /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so - * we must recompile any applications that use any older library version. - * For versions after libpng 1.0, we will be compatible, so we need - * only check the first digit. - */ - if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || - (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || - (user_png_ver[0] == '0' && user_png_ver[2] < '9')) - { #ifdef PNG_WARNINGS_SUPPORTED - size_t pos = 0; - char m[128]; + size_t pos = 0; + char m[128]; - pos = png_safecat(m, sizeof m, pos, "Application built with libpng-"); - pos = png_safecat(m, sizeof m, pos, user_png_ver); - pos = png_safecat(m, sizeof m, pos, " but running with "); - pos = png_safecat(m, sizeof m, pos, png_libpng_ver); + pos = png_safecat(m, (sizeof m), pos, + "Application built with libpng-"); + pos = png_safecat(m, (sizeof m), pos, user_png_ver); + pos = png_safecat(m, (sizeof m), pos, " but running with "); + pos = png_safecat(m, (sizeof m), pos, PNG_LIBPNG_VER_STRING); + PNG_UNUSED(pos) - png_warning(png_ptr, m); + png_warning(png_ptr, m); #endif #ifdef PNG_ERROR_NUMBERS_SUPPORTED - png_ptr->flags = 0; + png_ptr->flags = 0; #endif - return 0; - } + return 0; } /* Success return. */ return 1; } -/* Allocate the memory for an info_struct for the application. We don't - * really need the png_ptr, but it could potentially be useful in the - * future. This should be used in favour of malloc(png_sizeof(png_info)) - * and png_info_init() so that applications that want to use a shared - * libpng don't have to be recompiled if png_info changes size. +/* Generic function to create a png_struct for either read or write - this + * contains the common initialization. */ -PNG_FUNCTION(png_infop,PNGAPI -png_create_info_struct,(png_structp png_ptr),PNG_ALLOCATED) +PNG_FUNCTION(png_structp /* PRIVATE */, +png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, + png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) { - png_infop info_ptr; + png_struct create_struct; +# ifdef PNG_SETJMP_SUPPORTED + jmp_buf create_jmp_buf; +# endif + + /* This temporary stack-allocated structure is used to provide a place to + * build enough context to allow the user provided memory allocator (if any) + * to be called. + */ + memset(&create_struct, 0, (sizeof create_struct)); + + /* Added at libpng-1.2.6 */ +# ifdef PNG_USER_LIMITS_SUPPORTED + create_struct.user_width_max = PNG_USER_WIDTH_MAX; + create_struct.user_height_max = PNG_USER_HEIGHT_MAX; + +# ifdef PNG_USER_CHUNK_CACHE_MAX + /* Added at libpng-1.2.43 and 1.4.0 */ + create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; +# endif + +# ifdef PNG_USER_CHUNK_MALLOC_MAX + /* Added at libpng-1.2.43 and 1.4.1, required only for read but exists + * in png_struct regardless. + */ + create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; +# endif +# endif + + /* The following two API calls simply set fields in png_struct, so it is safe + * to do them now even though error handling is not yet set up. + */ +# ifdef PNG_USER_MEM_SUPPORTED + png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn); +# else + PNG_UNUSED(mem_ptr) + PNG_UNUSED(malloc_fn) + PNG_UNUSED(free_fn) +# endif + + /* (*error_fn) can return control to the caller after the error_ptr is set, + * this will result in a memory leak unless the error_fn does something + * extremely sophisticated. The design lacks merit but is implicit in the + * API. + */ + png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn); + +# ifdef PNG_SETJMP_SUPPORTED + if (!setjmp(create_jmp_buf)) +# endif + { +# ifdef PNG_SETJMP_SUPPORTED + /* Temporarily fake out the longjmp information until we have + * successfully completed this function. This only works if we have + * setjmp() support compiled in, but it is safe - this stuff should + * never happen. + */ + create_struct.jmp_buf_ptr = &create_jmp_buf; + create_struct.jmp_buf_size = 0; /*stack allocation*/ + create_struct.longjmp_fn = longjmp; +# endif + /* Call the general version checker (shared with read and write code): + */ + if (png_user_version_check(&create_struct, user_png_ver) != 0) + { + png_structrp png_ptr = png_voidcast(png_structrp, + png_malloc_warn(&create_struct, (sizeof *png_ptr))); + + if (png_ptr != NULL) + { + /* png_ptr->zstream holds a back-pointer to the png_struct, so + * this can only be done now: + */ + create_struct.zstream.zalloc = png_zalloc; + create_struct.zstream.zfree = png_zfree; + create_struct.zstream.opaque = png_ptr; + +# ifdef PNG_SETJMP_SUPPORTED + /* Eliminate the local error handling: */ + create_struct.jmp_buf_ptr = NULL; + create_struct.jmp_buf_size = 0; + create_struct.longjmp_fn = 0; +# endif + + *png_ptr = create_struct; + + /* This is the successful return point */ + return png_ptr; + } + } + } + + /* A longjmp because of a bug in the application storage allocator or a + * simple failure to allocate the png_struct. + */ + return NULL; +} + +/* Allocate the memory for an info_struct for the application. */ +PNG_FUNCTION(png_infop,PNGAPI +png_create_info_struct,(png_const_structrp png_ptr),PNG_ALLOCATED) +{ + png_inforp info_ptr; png_debug(1, "in png_create_info_struct"); if (png_ptr == NULL) - return (NULL); + return NULL; + + /* Use the internal API that does not (or at least should not) error out, so + * that this call always returns ok. The application typically sets up the + * error handling *after* creating the info_struct because this is the way it + * has always been done in 'example.c'. + */ + info_ptr = png_voidcast(png_inforp, png_malloc_base(png_ptr, + (sizeof *info_ptr))); -#ifdef PNG_USER_MEM_SUPPORTED - info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, - png_ptr->malloc_fn, png_ptr->mem_ptr); -#else - info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); -#endif if (info_ptr != NULL) - png_info_init_3(&info_ptr, png_sizeof(png_info)); + memset(info_ptr, 0, (sizeof *info_ptr)); - return (info_ptr); + return info_ptr; } /* This function frees the memory associated with a single info struct. * Normally, one would use either png_destroy_read_struct() or * png_destroy_write_struct() to free an info struct, but this may be - * useful for some applications. + * useful for some applications. From libpng 1.6.0 this function is also used + * internally to implement the png_info release part of the 'struct' destroy + * APIs. This ensures that all possible approaches free the same data (all of + * it). */ void PNGAPI -png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) +png_destroy_info_struct(png_const_structrp png_ptr, png_infopp info_ptr_ptr) { - png_infop info_ptr = NULL; + png_inforp info_ptr = NULL; png_debug(1, "in png_destroy_info_struct"); @@ -268,46 +377,59 @@ png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) if (info_ptr != NULL) { - png_info_destroy(png_ptr, info_ptr); - -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn, - png_ptr->mem_ptr); -#else - png_destroy_struct((png_voidp)info_ptr); -#endif + /* Do this first in case of an error below; if the app implements its own + * memory management this can lead to png_free calling png_error, which + * will abort this routine and return control to the app error handler. + * An infinite loop may result if it then tries to free the same info + * ptr. + */ *info_ptr_ptr = NULL; + + png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); + memset(info_ptr, 0, (sizeof *info_ptr)); + png_free(png_ptr, info_ptr); } } /* Initialize the info structure. This is now an internal function (0.89) * and applications using it are urged to use png_create_info_struct() - * instead. + * instead. Use deprecated in 1.6.0, internal use removed (used internally it + * is just a memset). + * + * NOTE: it is almost inconceivable that this API is used because it bypasses + * the user-memory mechanism and the user error handling/warning mechanisms in + * those cases where it does anything other than a memset. */ - -void PNGAPI -png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) +PNG_FUNCTION(void,PNGAPI +png_info_init_3,(png_infopp ptr_ptr, png_size_t png_info_struct_size), + PNG_DEPRECATED) { - png_infop info_ptr = *ptr_ptr; + png_inforp info_ptr = *ptr_ptr; png_debug(1, "in png_info_init_3"); if (info_ptr == NULL) return; - if (png_sizeof(png_info) > png_info_struct_size) + if ((sizeof (png_info)) > png_info_struct_size) { - png_destroy_struct(info_ptr); - info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); + *ptr_ptr = NULL; + /* The following line is why this API should not be used: */ + free(info_ptr); + info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL, + (sizeof *info_ptr))); + if (info_ptr == NULL) + return; *ptr_ptr = info_ptr; } /* Set everything to 0 */ - png_memset(info_ptr, 0, png_sizeof(png_info)); + memset(info_ptr, 0, (sizeof *info_ptr)); } +/* The following API is not called internally */ void PNGAPI -png_data_freer(png_structp png_ptr, png_infop info_ptr, +png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr, int freer, png_uint_32 mask) { png_debug(1, "in png_data_freer"); @@ -322,12 +444,11 @@ png_data_freer(png_structp png_ptr, png_infop info_ptr, info_ptr->free_me &= ~mask; else - png_warning(png_ptr, - "Unknown freer parameter in png_data_freer"); + png_error(png_ptr, "Unknown freer parameter in png_data_freer"); } void PNGAPI -png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, +png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num) { png_debug(1, "in png_free_data"); @@ -337,42 +458,43 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #ifdef PNG_TEXT_SUPPORTED /* Free text item num or (if num == -1) all text items */ - if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) + if (info_ptr->text != 0 && + ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0) { if (num != -1) { - if (info_ptr->text && info_ptr->text[num].key) - { - png_free(png_ptr, info_ptr->text[num].key); - info_ptr->text[num].key = NULL; - } + png_free(png_ptr, info_ptr->text[num].key); + info_ptr->text[num].key = NULL; } else { int i; + for (i = 0; i < info_ptr->num_text; i++) - png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); + png_free(png_ptr, info_ptr->text[i].key); + png_free(png_ptr, info_ptr->text); info_ptr->text = NULL; - info_ptr->num_text=0; + info_ptr->num_text = 0; } } #endif #ifdef PNG_tRNS_SUPPORTED /* Free any tRNS entry */ - if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) + if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0) { + info_ptr->valid &= ~PNG_INFO_tRNS; png_free(png_ptr, info_ptr->trans_alpha); info_ptr->trans_alpha = NULL; - info_ptr->valid &= ~PNG_INFO_tRNS; + info_ptr->num_trans = 0; } #endif #ifdef PNG_sCAL_SUPPORTED /* Free any sCAL entry */ - if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) + if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0) { png_free(png_ptr, info_ptr->scal_s_width); png_free(png_ptr, info_ptr->scal_s_height); @@ -384,20 +506,20 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #ifdef PNG_pCAL_SUPPORTED /* Free any pCAL entry */ - if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) + if (((mask & PNG_FREE_PCAL) & info_ptr->free_me) != 0) { png_free(png_ptr, info_ptr->pcal_purpose); png_free(png_ptr, info_ptr->pcal_units); info_ptr->pcal_purpose = NULL; info_ptr->pcal_units = NULL; + if (info_ptr->pcal_params != NULL) { int i; - for (i = 0; i < (int)info_ptr->pcal_nparams; i++) - { + + for (i = 0; i < info_ptr->pcal_nparams; i++) png_free(png_ptr, info_ptr->pcal_params[i]); - info_ptr->pcal_params[i] = NULL; - } + png_free(png_ptr, info_ptr->pcal_params); info_ptr->pcal_params = NULL; } @@ -406,8 +528,8 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #endif #ifdef PNG_iCCP_SUPPORTED - /* Free any iCCP entry */ - if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) + /* Free any profile entry */ + if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0) { png_free(png_ptr, info_ptr->iccp_name); png_free(png_ptr, info_ptr->iccp_profile); @@ -419,74 +541,62 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #ifdef PNG_sPLT_SUPPORTED /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ - if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) + if (info_ptr->splt_palettes != 0 && + ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0) { if (num != -1) { - if (info_ptr->splt_palettes) - { - png_free(png_ptr, info_ptr->splt_palettes[num].name); - png_free(png_ptr, info_ptr->splt_palettes[num].entries); - info_ptr->splt_palettes[num].name = NULL; - info_ptr->splt_palettes[num].entries = NULL; - } - } - - else - { - if (info_ptr->splt_palettes_num) - { - int i; - for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) - png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i); - - png_free(png_ptr, info_ptr->splt_palettes); - info_ptr->splt_palettes = NULL; - info_ptr->splt_palettes_num = 0; - } - info_ptr->valid &= ~PNG_INFO_sPLT; - } - } -#endif - -#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED - if (png_ptr->unknown_chunk.data) - { - png_free(png_ptr, png_ptr->unknown_chunk.data); - png_ptr->unknown_chunk.data = NULL; - } - - if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) - { - if (num != -1) - { - if (info_ptr->unknown_chunks) - { - png_free(png_ptr, info_ptr->unknown_chunks[num].data); - info_ptr->unknown_chunks[num].data = NULL; - } + png_free(png_ptr, info_ptr->splt_palettes[num].name); + png_free(png_ptr, info_ptr->splt_palettes[num].entries); + info_ptr->splt_palettes[num].name = NULL; + info_ptr->splt_palettes[num].entries = NULL; } else { int i; - if (info_ptr->unknown_chunks_num) + for (i = 0; i < info_ptr->splt_palettes_num; i++) { - for (i = 0; i < info_ptr->unknown_chunks_num; i++) - png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i); - - png_free(png_ptr, info_ptr->unknown_chunks); - info_ptr->unknown_chunks = NULL; - info_ptr->unknown_chunks_num = 0; + png_free(png_ptr, info_ptr->splt_palettes[i].name); + png_free(png_ptr, info_ptr->splt_palettes[i].entries); } + + png_free(png_ptr, info_ptr->splt_palettes); + info_ptr->splt_palettes = NULL; + info_ptr->splt_palettes_num = 0; + info_ptr->valid &= ~PNG_INFO_sPLT; + } + } +#endif + +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED + if (info_ptr->unknown_chunks != 0 && + ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0) + { + if (num != -1) + { + png_free(png_ptr, info_ptr->unknown_chunks[num].data); + info_ptr->unknown_chunks[num].data = NULL; + } + + else + { + int i; + + for (i = 0; i < info_ptr->unknown_chunks_num; i++) + png_free(png_ptr, info_ptr->unknown_chunks[i].data); + + png_free(png_ptr, info_ptr->unknown_chunks); + info_ptr->unknown_chunks = NULL; + info_ptr->unknown_chunks_num = 0; } } #endif #ifdef PNG_hIST_SUPPORTED /* Free any hIST entry */ - if ((mask & PNG_FREE_HIST) & info_ptr->free_me) + if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0) { png_free(png_ptr, info_ptr->hist); info_ptr->hist = NULL; @@ -495,9 +605,9 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #endif /* Free any PLTE entry that was internally allocated */ - if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) + if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0) { - png_zfree(png_ptr, info_ptr->palette); + png_free(png_ptr, info_ptr->palette); info_ptr->palette = NULL; info_ptr->valid &= ~PNG_INFO_PLTE; info_ptr->num_palette = 0; @@ -505,16 +615,14 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, #ifdef PNG_INFO_IMAGE_SUPPORTED /* Free any image bits attached to the info structure */ - if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) + if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0) { - if (info_ptr->row_pointers) + if (info_ptr->row_pointers != 0) { - int row; - for (row = 0; row < (int)info_ptr->height; row++) - { + png_uint_32 row; + for (row = 0; row < info_ptr->height; row++) png_free(png_ptr, info_ptr->row_pointers[row]); - info_ptr->row_pointers[row] = NULL; - } + png_free(png_ptr, info_ptr->row_pointers); info_ptr->row_pointers = NULL; } @@ -527,37 +635,14 @@ png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, info_ptr->free_me &= ~mask; } - -/* This is an internal routine to free any memory that the info struct is - * pointing to before re-using it or freeing the struct itself. Recall - * that png_free() checks for NULL pointers for us. - */ -void /* PRIVATE */ -png_info_destroy(png_structp png_ptr, png_infop info_ptr) -{ - png_debug(1, "in png_info_destroy"); - - png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); - -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - if (png_ptr->num_chunk_list) - { - png_free(png_ptr, png_ptr->chunk_list); - png_ptr->chunk_list = NULL; - png_ptr->num_chunk_list = 0; - } -#endif - - png_info_init_3(&info_ptr, png_sizeof(png_info)); -} -#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ +#endif /* READ || WRITE */ /* This function returns a pointer to the io_ptr associated with the user * functions. The application should free any memory associated with this * pointer before png_write_destroy() or png_read_destroy() are called. */ png_voidp PNGAPI -png_get_io_ptr(png_structp png_ptr) +png_get_io_ptr(png_const_structrp png_ptr) { if (png_ptr == NULL) return (NULL); @@ -574,7 +659,7 @@ png_get_io_ptr(png_structp png_ptr) * function of your own because "FILE *" isn't necessarily available. */ void PNGAPI -png_init_io(png_structp png_ptr, png_FILE_p fp) +png_init_io(png_structrp png_ptr, png_FILE_p fp) { png_debug(1, "in png_init_io"); @@ -585,42 +670,53 @@ png_init_io(png_structp png_ptr, png_FILE_p fp) } # endif +# ifdef PNG_SAVE_INT_32_SUPPORTED +/* PNG signed integers are saved in 32-bit 2's complement format. ANSI C-90 + * defines a cast of a signed integer to an unsigned integer either to preserve + * the value, if it is positive, or to calculate: + * + * (UNSIGNED_MAX+1) + integer + * + * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the + * negative integral value is added the result will be an unsigned value + * correspnding to the 2's complement representation. + */ +void PNGAPI +png_save_int_32(png_bytep buf, png_int_32 i) +{ + png_save_uint_32(buf, i); +} +# endif + # ifdef PNG_TIME_RFC1123_SUPPORTED /* Convert the supplied time into an RFC 1123 string suitable for use in * a "Creation Time" or other text-based time string. */ -png_const_charp PNGAPI -png_convert_to_rfc1123(png_structp png_ptr, png_const_timep ptime) +int PNGAPI +png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime) { static PNG_CONST char short_months[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - if (png_ptr == NULL) - return (NULL); + if (out == NULL) + return 0; if (ptime->year > 9999 /* RFC1123 limitation */ || ptime->month == 0 || ptime->month > 12 || ptime->day == 0 || ptime->day > 31 || ptime->hour > 23 || ptime->minute > 59 || ptime->second > 60) - { - png_warning(png_ptr, "Ignoring invalid time value"); - return (NULL); - } + return 0; { size_t pos = 0; char number_buf[5]; /* enough for a four-digit year */ -# define APPEND_STRING(string)\ - pos = png_safecat(png_ptr->time_buffer, sizeof png_ptr->time_buffer,\ - pos, (string)) +# define APPEND_STRING(string) pos = png_safecat(out, 29, pos, (string)) # define APPEND_NUMBER(format, value)\ APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value))) -# define APPEND(ch)\ - if (pos < (sizeof png_ptr->time_buffer)-1)\ - png_ptr->time_buffer[pos++] = (ch) +# define APPEND(ch) if (pos < 28) out[pos++] = (ch) APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day); APPEND(' '); @@ -634,20 +730,44 @@ png_convert_to_rfc1123(png_structp png_ptr, png_const_timep ptime) APPEND(':'); APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second); APPEND_STRING(" +0000"); /* This reliably terminates the buffer */ + PNG_UNUSED (pos) # undef APPEND # undef APPEND_NUMBER # undef APPEND_STRING } - return png_ptr->time_buffer; + return 1; } -# endif /* PNG_TIME_RFC1123_SUPPORTED */ -#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ +# if PNG_LIBPNG_VER < 10700 +/* To do: remove the following from libpng-1.7 */ +/* Original API that uses a private buffer in png_struct. + * Deprecated because it causes png_struct to carry a spurious temporary + * buffer (png_struct::time_buffer), better to have the caller pass this in. + */ +png_const_charp PNGAPI +png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime) +{ + if (png_ptr != NULL) + { + /* The only failure above if png_ptr != NULL is from an invalid ptime */ + if (png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime) == 0) + png_warning(png_ptr, "Ignoring invalid time value"); + + else + return png_ptr->time_buffer; + } + + return NULL; +} +# endif /* LIBPNG_VER < 10700 */ +# endif /* TIME_RFC1123 */ + +#endif /* READ || WRITE */ png_const_charp PNGAPI -png_get_copyright(png_const_structp png_ptr) +png_get_copyright(png_const_structrp png_ptr) { PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ #ifdef PNG_STRING_COPYRIGHT @@ -655,14 +775,14 @@ png_get_copyright(png_const_structp png_ptr) #else # ifdef __STDC__ return PNG_STRING_NEWLINE \ - "libpng version 1.5.12 - July 11, 2012" PNG_STRING_NEWLINE \ - "Copyright (c) 1998-2012 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ - "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ - "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ - PNG_STRING_NEWLINE; + "libpng version 1.6.19 - November 12, 2015" PNG_STRING_NEWLINE \ + "Copyright (c) 1998-2015 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ + "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ + "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ + PNG_STRING_NEWLINE; # else - return "libpng version 1.5.12 - July 11, 2012\ - Copyright (c) 1998-2012 Glenn Randers-Pehrson\ + return "libpng version 1.6.19 - November 12, 2015\ + Copyright (c) 1998-2015 Glenn Randers-Pehrson\ Copyright (c) 1996-1997 Andreas Dilger\ Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."; # endif @@ -678,14 +798,14 @@ png_get_copyright(png_const_structp png_ptr) * it is guaranteed that png.c uses the correct version of png.h. */ png_const_charp PNGAPI -png_get_libpng_ver(png_const_structp png_ptr) +png_get_libpng_ver(png_const_structrp png_ptr) { /* Version of *.c files used when building libpng */ return png_get_header_ver(png_ptr); } png_const_charp PNGAPI -png_get_header_ver(png_const_structp png_ptr) +png_get_header_ver(png_const_structrp png_ptr) { /* Version of *.h files used when building libpng */ PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ @@ -693,70 +813,137 @@ png_get_header_ver(png_const_structp png_ptr) } png_const_charp PNGAPI -png_get_header_version(png_const_structp png_ptr) +png_get_header_version(png_const_structrp png_ptr) { /* Returns longer string containing both version and date */ PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ #ifdef __STDC__ return PNG_HEADER_VERSION_STRING # ifndef PNG_READ_SUPPORTED - " (NO READ SUPPORT)" + " (NO READ SUPPORT)" # endif - PNG_STRING_NEWLINE; + PNG_STRING_NEWLINE; #else return PNG_HEADER_VERSION_STRING; #endif } -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED +/* NOTE: this routine is not used internally! */ +/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth + * large of png_color. This lets grayscale images be treated as + * paletted. Most useful for gamma correction and simplification + * of code. This API is not used internally. + */ +void PNGAPI +png_build_grayscale_palette(int bit_depth, png_colorp palette) +{ + int num_palette; + int color_inc; + int i; + int v; + + png_debug(1, "in png_do_build_grayscale_palette"); + + if (palette == NULL) + return; + + switch (bit_depth) + { + case 1: + num_palette = 2; + color_inc = 0xff; + break; + + case 2: + num_palette = 4; + color_inc = 0x55; + break; + + case 4: + num_palette = 16; + color_inc = 0x11; + break; + + case 8: + num_palette = 256; + color_inc = 1; + break; + + default: + num_palette = 0; + color_inc = 0; + break; + } + + for (i = 0, v = 0; i < num_palette; i++, v += color_inc) + { + palette[i].red = (png_byte)(v & 0xff); + palette[i].green = (png_byte)(v & 0xff); + palette[i].blue = (png_byte)(v & 0xff); + } +} +#endif + +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED int PNGAPI -png_handle_as_unknown(png_structp png_ptr, png_const_bytep chunk_name) +png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name) { /* Check chunk_name and return "keep" value if it's on the list, else 0 */ png_const_bytep p, p_end; - if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list <= 0) + if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0) return PNG_HANDLE_CHUNK_AS_DEFAULT; p_end = png_ptr->chunk_list; p = p_end + png_ptr->num_chunk_list*5; /* beyond end */ /* The code is the fifth byte after each four byte string. Historically this - * code was always searched from the end of the list, so it should continue - * to do so in case there are duplicated entries. + * code was always searched from the end of the list, this is no longer + * necessary because the 'set' routine handles duplicate entries correcty. */ do /* num_chunk_list > 0, so at least one */ { p -= 5; - if (!png_memcmp(chunk_name, p, 4)) + + if (memcmp(chunk_name, p, 4) == 0) return p[4]; } while (p > p_end); + /* This means that known chunks should be processed and unknown chunks should + * be handled according to the value of png_ptr->unknown_default; this can be + * confusing because, as a result, there are two levels of defaulting for + * unknown chunks. + */ return PNG_HANDLE_CHUNK_AS_DEFAULT; } +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\ + defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) int /* PRIVATE */ -png_chunk_unknown_handling(png_structp png_ptr, png_uint_32 chunk_name) +png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name) { png_byte chunk_string[5]; PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name); return png_handle_as_unknown(png_ptr, chunk_string); } -#endif +#endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */ +#endif /* SET_UNKNOWN_CHUNKS */ #ifdef PNG_READ_SUPPORTED /* This function, added to libpng-1.0.6g, is untested. */ int PNGAPI -png_reset_zstream(png_structp png_ptr) +png_reset_zstream(png_structrp png_ptr) { if (png_ptr == NULL) return Z_STREAM_ERROR; + /* WARNING: this resets the window bits to the maximum! */ return (inflateReset(&png_ptr->zstream)); } -#endif /* PNG_READ_SUPPORTED */ +#endif /* READ */ /* This function was added to libpng-1.0.7 */ png_uint_32 PNGAPI @@ -766,142 +953,308 @@ png_access_version_number(void) return((png_uint_32)PNG_LIBPNG_VER); } - - #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) +/* Ensure that png_ptr->zstream.msg holds some appropriate error message string. + * If it doesn't 'ret' is used to set it to something appropriate, even in cases + * like Z_OK or Z_STREAM_END where the error code is apparently a success code. + */ +void /* PRIVATE */ +png_zstream_error(png_structrp png_ptr, int ret) +{ + /* Translate 'ret' into an appropriate error string, priority is given to the + * one in zstream if set. This always returns a string, even in cases like + * Z_OK or Z_STREAM_END where the error code is a success code. + */ + if (png_ptr->zstream.msg == NULL) switch (ret) + { + default: + case Z_OK: + png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return code"); + break; + + case Z_STREAM_END: + /* Normal exit */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected end of LZ stream"); + break; + + case Z_NEED_DICT: + /* This means the deflate stream did not have a dictionary; this + * indicates a bogus PNG. + */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("missing LZ dictionary"); + break; + + case Z_ERRNO: + /* gz APIs only: should not happen */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("zlib IO error"); + break; + + case Z_STREAM_ERROR: + /* internal libpng error */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("bad parameters to zlib"); + break; + + case Z_DATA_ERROR: + png_ptr->zstream.msg = PNGZ_MSG_CAST("damaged LZ stream"); + break; + + case Z_MEM_ERROR: + png_ptr->zstream.msg = PNGZ_MSG_CAST("insufficient memory"); + break; + + case Z_BUF_ERROR: + /* End of input or output; not a problem if the caller is doing + * incremental read or write. + */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("truncated"); + break; + + case Z_VERSION_ERROR: + png_ptr->zstream.msg = PNGZ_MSG_CAST("unsupported zlib version"); + break; + + case PNG_UNEXPECTED_ZLIB_RETURN: + /* Compile errors here mean that zlib now uses the value co-opted in + * pngpriv.h for PNG_UNEXPECTED_ZLIB_RETURN; update the switch above + * and change pngpriv.h. Note that this message is "... return", + * whereas the default/Z_OK one is "... return code". + */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return"); + break; + } +} + /* png_convert_size: a PNGAPI but no longer in png.h, so deleted * at libpng 1.5.5! */ /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ -# ifdef PNG_CHECK_cHRM_SUPPORTED - -int /* PRIVATE */ -png_check_cHRM_fixed(png_structp png_ptr, - png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, - png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, - png_fixed_point blue_x, png_fixed_point blue_y) -{ - int ret = 1; - unsigned long xy_hi,xy_lo,yx_hi,yx_lo; - - png_debug(1, "in function png_check_cHRM_fixed"); - - if (png_ptr == NULL) - return 0; - - /* (x,y,z) values are first limited to 0..100000 (PNG_FP_1), the white - * y must also be greater than 0. To test for the upper limit calculate - * (PNG_FP_1-y) - x must be <= to this for z to be >= 0 (and the expression - * cannot overflow.) At this point we know x and y are >= 0 and (x+y) is - * <= PNG_FP_1. The previous test on PNG_MAX_UINT_31 is removed because it - * pointless (and it produces compiler warnings!) +#ifdef PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */ +static int +png_colorspace_check_gamma(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_fixed_point gAMA, int from) + /* This is called to check a new gamma value against an existing one. The + * routine returns false if the new gamma value should not be written. + * + * 'from' says where the new gamma value comes from: + * + * 0: the new gamma value is the libpng estimate for an ICC profile + * 1: the new gamma value comes from a gAMA chunk + * 2: the new gamma value comes from an sRGB chunk */ - if (white_x < 0 || white_y <= 0 || - red_x < 0 || red_y < 0 || - green_x < 0 || green_y < 0 || - blue_x < 0 || blue_y < 0) +{ + png_fixed_point gtest; + + if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && + (png_muldiv(>est, colorspace->gamma, PNG_FP_1, gAMA) == 0 || + png_gamma_significant(gtest) != 0)) { - png_warning(png_ptr, - "Ignoring attempt to set negative chromaticity value"); - ret = 0; - } - /* And (x+y) must be <= PNG_FP_1 (so z is >= 0) */ - if (white_x > PNG_FP_1 - white_y) - { - png_warning(png_ptr, "Invalid cHRM white point"); - ret = 0; + /* Either this is an sRGB image, in which case the calculated gamma + * approximation should match, or this is an image with a profile and the + * value libpng calculates for the gamma of the profile does not match the + * value recorded in the file. The former, sRGB, case is an error, the + * latter is just a warning. + */ + if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2) + { + png_chunk_report(png_ptr, "gamma value does not match sRGB", + PNG_CHUNK_ERROR); + /* Do not overwrite an sRGB value */ + return from == 2; + } + + else /* sRGB tag not involved */ + { + png_chunk_report(png_ptr, "gamma value does not match libpng estimate", + PNG_CHUNK_WARNING); + return from == 1; + } } - if (red_x > PNG_FP_1 - red_y) - { - png_warning(png_ptr, "Invalid cHRM red point"); - ret = 0; - } - - if (green_x > PNG_FP_1 - green_y) - { - png_warning(png_ptr, "Invalid cHRM green point"); - ret = 0; - } - - if (blue_x > PNG_FP_1 - blue_y) - { - png_warning(png_ptr, "Invalid cHRM blue point"); - ret = 0; - } - - png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo); - png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo); - - if (xy_hi == yx_hi && xy_lo == yx_lo) - { - png_warning(png_ptr, - "Ignoring attempt to set cHRM RGB triangle with zero area"); - ret = 0; - } - - return ret; + return 1; } -# endif /* PNG_CHECK_cHRM_SUPPORTED */ -#ifdef PNG_cHRM_SUPPORTED +void /* PRIVATE */ +png_colorspace_set_gamma(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_fixed_point gAMA) +{ + /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't + * occur. Since the fixed point representation is asymetrical it is + * possible for 1/gamma to overflow the limit of 21474 and this means the + * gamma value must be at least 5/100000 and hence at most 20000.0. For + * safety the limits here are a little narrower. The values are 0.00016 to + * 6250.0, which are truly ridiculous gamma values (and will produce + * displays that are all black or all white.) + * + * In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk + * handling code, which only required the value to be >0. + */ + png_const_charp errmsg; + + if (gAMA < 16 || gAMA > 625000000) + errmsg = "gamma value out of range"; + +# ifdef PNG_READ_gAMA_SUPPORTED + /* Allow the application to set the gamma value more than once */ + else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && + (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0) + errmsg = "duplicate"; +# endif + + /* Do nothing if the colorspace is already invalid */ + else if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) + return; + + else + { + if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA, + 1/*from gAMA*/) != 0) + { + /* Store this gamma value. */ + colorspace->gamma = gAMA; + colorspace->flags |= + (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA); + } + + /* At present if the check_gamma test fails the gamma of the colorspace is + * not updated however the colorspace is not invalidated. This + * corresponds to the case where the existing gamma comes from an sRGB + * chunk or profile. An error message has already been output. + */ + return; + } + + /* Error exit - errmsg has been set. */ + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR); +} + +void /* PRIVATE */ +png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr) +{ + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) + { + /* Everything is invalid */ + info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB| + PNG_INFO_iCCP); + +# ifdef PNG_COLORSPACE_SUPPORTED + /* Clean up the iCCP profile now if it won't be used. */ + png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/); +# else + PNG_UNUSED(png_ptr) +# endif + } + + else + { +# ifdef PNG_COLORSPACE_SUPPORTED + /* Leave the INFO_iCCP flag set if the pngset.c code has already set + * it; this allows a PNG to contain a profile which matches sRGB and + * yet still have that profile retrievable by the application. + */ + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB) != 0) + info_ptr->valid |= PNG_INFO_sRGB; + + else + info_ptr->valid &= ~PNG_INFO_sRGB; + + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) + info_ptr->valid |= PNG_INFO_cHRM; + + else + info_ptr->valid &= ~PNG_INFO_cHRM; +# endif + + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0) + info_ptr->valid |= PNG_INFO_gAMA; + + else + info_ptr->valid &= ~PNG_INFO_gAMA; + } +} + +#ifdef PNG_READ_SUPPORTED +void /* PRIVATE */ +png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr) +{ + if (info_ptr == NULL) /* reduce code size; check here not in the caller */ + return; + + info_ptr->colorspace = png_ptr->colorspace; + png_colorspace_sync_info(png_ptr, info_ptr); +} +#endif +#endif /* GAMMA */ + +#ifdef PNG_COLORSPACE_SUPPORTED /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for * cHRM, as opposed to using chromaticities. These internal APIs return * non-zero on a parameter error. The X, Y and Z values are required to be * positive and less than 1.0. */ -int png_xy_from_XYZ(png_xy *xy, png_XYZ XYZ) +static int +png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ) { png_int_32 d, dwhite, whiteX, whiteY; - d = XYZ.redX + XYZ.redY + XYZ.redZ; - if (!png_muldiv(&xy->redx, XYZ.redX, PNG_FP_1, d)) return 1; - if (!png_muldiv(&xy->redy, XYZ.redY, PNG_FP_1, d)) return 1; + d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z; + if (png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d) == 0) + return 1; + if (png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d) == 0) + return 1; dwhite = d; - whiteX = XYZ.redX; - whiteY = XYZ.redY; + whiteX = XYZ->red_X; + whiteY = XYZ->red_Y; - d = XYZ.greenX + XYZ.greenY + XYZ.greenZ; - if (!png_muldiv(&xy->greenx, XYZ.greenX, PNG_FP_1, d)) return 1; - if (!png_muldiv(&xy->greeny, XYZ.greenY, PNG_FP_1, d)) return 1; + d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z; + if (png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d) == 0) + return 1; + if (png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d) == 0) + return 1; dwhite += d; - whiteX += XYZ.greenX; - whiteY += XYZ.greenY; + whiteX += XYZ->green_X; + whiteY += XYZ->green_Y; - d = XYZ.blueX + XYZ.blueY + XYZ.blueZ; - if (!png_muldiv(&xy->bluex, XYZ.blueX, PNG_FP_1, d)) return 1; - if (!png_muldiv(&xy->bluey, XYZ.blueY, PNG_FP_1, d)) return 1; + d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z; + if (png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d) == 0) + return 1; + if (png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d) == 0) + return 1; dwhite += d; - whiteX += XYZ.blueX; - whiteY += XYZ.blueY; + whiteX += XYZ->blue_X; + whiteY += XYZ->blue_Y; - /* The reference white is simply the same of the end-point (X,Y,Z) vectors, + /* The reference white is simply the sum of the end-point (X,Y,Z) vectors, * thus: */ - if (!png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite)) return 1; - if (!png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite)) return 1; + if (png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite) == 0) + return 1; + if (png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite) == 0) + return 1; return 0; } -int png_XYZ_from_xy(png_XYZ *XYZ, png_xy xy) +static int +png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy) { png_fixed_point red_inverse, green_inverse, blue_scale; png_fixed_point left, right, denominator; /* Check xy and, implicitly, z. Note that wide gamut color spaces typically * have end points with 0 tristimulus values (these are impossible end - * points, but they are used to cover the possible colors.) + * points, but they are used to cover the possible colors). We check + * xy->whitey against 5, not 0, to avoid a possible integer overflow. */ - if (xy.redx < 0 || xy.redx > PNG_FP_1) return 1; - if (xy.redy < 0 || xy.redy > PNG_FP_1-xy.redx) return 1; - if (xy.greenx < 0 || xy.greenx > PNG_FP_1) return 1; - if (xy.greeny < 0 || xy.greeny > PNG_FP_1-xy.greenx) return 1; - if (xy.bluex < 0 || xy.bluex > PNG_FP_1) return 1; - if (xy.bluey < 0 || xy.bluey > PNG_FP_1-xy.bluex) return 1; - if (xy.whitex < 0 || xy.whitex > PNG_FP_1) return 1; - if (xy.whitey < 0 || xy.whitey > PNG_FP_1-xy.whitex) return 1; + if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1; + if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1; + if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1; + if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1; + if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1; + if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1; + if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1; + if (xy->whitey < 5 || xy->whitey > PNG_FP_1-xy->whitex) return 1; /* The reverse calculation is more difficult because the original tristimulus * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8 @@ -976,7 +1329,7 @@ int png_XYZ_from_xy(png_XYZ *XYZ, png_xy xy) * (1/white-y), so we can immediately see that as white-y approaches 0 the * accuracy inherent in the cHRM chunk drops off substantially. * - * libpng arithmetic: a simple invertion of the above equations + * libpng arithmetic: a simple inversion of the above equations * ------------------------------------------------------------ * * white_scale = 1/white-y @@ -1082,91 +1435,1048 @@ int png_XYZ_from_xy(png_XYZ *XYZ, png_xy xy) /* By the argument, above overflow should be impossible here. The return * value of 2 indicates an internal error to the caller. */ - if (!png_muldiv(&left, xy.greenx-xy.bluex, xy.redy - xy.bluey, 7)) return 2; - if (!png_muldiv(&right, xy.greeny-xy.bluey, xy.redx - xy.bluex, 7)) return 2; + if (png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7) == 0) + return 2; + if (png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7) == 0) + return 2; denominator = left - right; /* Now find the red numerator. */ - if (!png_muldiv(&left, xy.greenx-xy.bluex, xy.whitey-xy.bluey, 7)) return 2; - if (!png_muldiv(&right, xy.greeny-xy.bluey, xy.whitex-xy.bluex, 7)) return 2; + if (png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7) == 0) + return 2; + if (png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7) == 0) + return 2; /* Overflow is possible here and it indicates an extreme set of PNG cHRM * chunk values. This calculation actually returns the reciprocal of the * scale value because this allows us to delay the multiplication of white-y * into the denominator, which tends to produce a small number. */ - if (!png_muldiv(&red_inverse, xy.whitey, denominator, left-right) || - red_inverse <= xy.whitey /* r+g+b scales = white scale */) + if (png_muldiv(&red_inverse, xy->whitey, denominator, left-right) == 0 || + red_inverse <= xy->whitey /* r+g+b scales = white scale */) return 1; /* Similarly for green_inverse: */ - if (!png_muldiv(&left, xy.redy-xy.bluey, xy.whitex-xy.bluex, 7)) return 2; - if (!png_muldiv(&right, xy.redx-xy.bluex, xy.whitey-xy.bluey, 7)) return 2; - if (!png_muldiv(&green_inverse, xy.whitey, denominator, left-right) || - green_inverse <= xy.whitey) + if (png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7) == 0) + return 2; + if (png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7) == 0) + return 2; + if (png_muldiv(&green_inverse, xy->whitey, denominator, left-right) == 0 || + green_inverse <= xy->whitey) return 1; /* And the blue scale, the checks above guarantee this can't overflow but it * can still produce 0 for extreme cHRM values. */ - blue_scale = png_reciprocal(xy.whitey) - png_reciprocal(red_inverse) - - png_reciprocal(green_inverse); - if (blue_scale <= 0) return 1; + blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) - + png_reciprocal(green_inverse); + if (blue_scale <= 0) + return 1; /* And fill in the png_XYZ: */ - if (!png_muldiv(&XYZ->redX, xy.redx, PNG_FP_1, red_inverse)) return 1; - if (!png_muldiv(&XYZ->redY, xy.redy, PNG_FP_1, red_inverse)) return 1; - if (!png_muldiv(&XYZ->redZ, PNG_FP_1 - xy.redx - xy.redy, PNG_FP_1, - red_inverse)) + if (png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse) == 0) + return 1; + if (png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse) == 0) + return 1; + if (png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1, + red_inverse) == 0) return 1; - if (!png_muldiv(&XYZ->greenX, xy.greenx, PNG_FP_1, green_inverse)) return 1; - if (!png_muldiv(&XYZ->greenY, xy.greeny, PNG_FP_1, green_inverse)) return 1; - if (!png_muldiv(&XYZ->greenZ, PNG_FP_1 - xy.greenx - xy.greeny, PNG_FP_1, - green_inverse)) + if (png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse) == 0) + return 1; + if (png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse) == 0) + return 1; + if (png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1, + green_inverse) == 0) return 1; - if (!png_muldiv(&XYZ->blueX, xy.bluex, blue_scale, PNG_FP_1)) return 1; - if (!png_muldiv(&XYZ->blueY, xy.bluey, blue_scale, PNG_FP_1)) return 1; - if (!png_muldiv(&XYZ->blueZ, PNG_FP_1 - xy.bluex - xy.bluey, blue_scale, - PNG_FP_1)) + if (png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1) == 0) + return 1; + if (png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1) == 0) + return 1; + if (png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale, + PNG_FP_1) == 0) return 1; return 0; /*success*/ } -int png_XYZ_from_xy_checked(png_structp png_ptr, png_XYZ *XYZ, png_xy xy) +static int +png_XYZ_normalize(png_XYZ *XYZ) { - switch (png_XYZ_from_xy(XYZ, xy)) + png_int_32 Y; + + if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 || + XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 || + XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0) + return 1; + + /* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1. + * IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore + * relying on addition of two positive values producing a negative one is not + * safe. + */ + Y = XYZ->red_Y; + if (0x7fffffff - Y < XYZ->green_X) + return 1; + Y += XYZ->green_Y; + if (0x7fffffff - Y < XYZ->blue_X) + return 1; + Y += XYZ->blue_Y; + + if (Y != PNG_FP_1) { - case 0: /* success */ + if (png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y) == 0) return 1; + if (png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y) == 0) + return 1; + + if (png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y) == 0) + return 1; + if (png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y) == 0) + return 1; + } + + return 0; +} + +static int +png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta) +{ + /* Allow an error of +/-0.01 (absolute value) on each chromaticity */ + if (PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) || + PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) || + PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) || + PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) || + PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) || + PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) || + PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) || + PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta)) + return 0; + return 1; +} + +/* Added in libpng-1.6.0, a different check for the validity of a set of cHRM + * chunk chromaticities. Earlier checks used to simply look for the overflow + * condition (where the determinant of the matrix to solve for XYZ ends up zero + * because the chromaticity values are not all distinct.) Despite this it is + * theoretically possible to produce chromaticities that are apparently valid + * but that rapidly degrade to invalid, potentially crashing, sets because of + * arithmetic inaccuracies when calculations are performed on them. The new + * check is to round-trip xy -> XYZ -> xy and then check that the result is + * within a small percentage of the original. + */ +static int +png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy) +{ + int result; + png_xy xy_test; + + /* As a side-effect this routine also returns the XYZ endpoints. */ + result = png_XYZ_from_xy(XYZ, xy); + if (result != 0) + return result; + + result = png_xy_from_XYZ(&xy_test, XYZ); + if (result != 0) + return result; + + if (png_colorspace_endpoints_match(xy, &xy_test, + 5/*actually, the math is pretty accurate*/) != 0) + return 0; + + /* Too much slip */ + return 1; +} + +/* This is the check going the other way. The XYZ is modified to normalize it + * (another side-effect) and the xy chromaticities are returned. + */ +static int +png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ) +{ + int result; + png_XYZ XYZtemp; + + result = png_XYZ_normalize(XYZ); + if (result != 0) + return result; + + result = png_xy_from_XYZ(xy, XYZ); + if (result != 0) + return result; + + XYZtemp = *XYZ; + return png_colorspace_check_xy(&XYZtemp, xy); +} + +/* Used to check for an endpoint match against sRGB */ +static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */ +{ + /* color x y */ + /* red */ 64000, 33000, + /* green */ 30000, 60000, + /* blue */ 15000, 6000, + /* white */ 31270, 32900 +}; + +static int +png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr, + png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ, + int preferred) +{ + if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) + return 0; + + /* The consistency check is performed on the chromaticities; this factors out + * variations because of the normalization (or not) of the end point Y + * values. + */ + if (preferred < 2 && + (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) + { + /* The end points must be reasonably close to any we already have. The + * following allows an error of up to +/-.001 + */ + if (png_colorspace_endpoints_match(xy, &colorspace->end_points_xy, + 100) == 0) + { + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_benign_error(png_ptr, "inconsistent chromaticities"); + return 0; /* failed */ + } + + /* Only overwrite with preferred values */ + if (preferred == 0) + return 1; /* ok, but no change */ + } + + colorspace->end_points_xy = *xy; + colorspace->end_points_XYZ = *XYZ; + colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS; + + /* The end points are normally quoted to two decimal digits, so allow +/-0.01 + * on this test. + */ + if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000) != 0) + colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB; + + else + colorspace->flags &= PNG_COLORSPACE_CANCEL( + PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB); + + return 2; /* ok and changed */ +} + +int /* PRIVATE */ +png_colorspace_set_chromaticities(png_const_structrp png_ptr, + png_colorspacerp colorspace, const png_xy *xy, int preferred) +{ + /* We must check the end points to ensure they are reasonable - in the past + * color management systems have crashed as a result of getting bogus + * colorant values, while this isn't the fault of libpng it is the + * responsibility of libpng because PNG carries the bomb and libpng is in a + * position to protect against it. + */ + png_XYZ XYZ; + + switch (png_colorspace_check_xy(&XYZ, xy)) + { + case 0: /* success */ + return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, xy, &XYZ, + preferred); + case 1: - /* The chunk may be technically valid, but we got png_fixed_point - * overflow while trying to get XYZ values out of it. This is - * entirely benign - the cHRM chunk is pretty extreme. + /* We can't invert the chromaticities so we can't produce value XYZ + * values. Likely as not a color management system will fail too. */ - png_warning(png_ptr, - "extreme cHRM chunk cannot be converted to tristimulus values"); + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_benign_error(png_ptr, "invalid chromaticities"); break; default: /* libpng is broken; this should be a warning but if it happens we * want error reports so for the moment it is an error. */ - png_error(png_ptr, "internal error in png_XYZ_from_xy"); + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_error(png_ptr, "internal error checking chromaticities"); + } + + return 0; /* failed */ +} + +int /* PRIVATE */ +png_colorspace_set_endpoints(png_const_structrp png_ptr, + png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred) +{ + png_XYZ XYZ = *XYZ_in; + png_xy xy; + + switch (png_colorspace_check_XYZ(&xy, &XYZ)) + { + case 0: + return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ, + preferred); + + case 1: + /* End points are invalid. */ + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_benign_error(png_ptr, "invalid end points"); + break; + + default: + colorspace->flags |= PNG_COLORSPACE_INVALID; + png_error(png_ptr, "internal error checking chromaticities"); + } + + return 0; /* failed */ +} + +#if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED) +/* Error message generation */ +static char +png_icc_tag_char(png_uint_32 byte) +{ + byte &= 0xff; + if (byte >= 32 && byte <= 126) + return (char)byte; + else + return '?'; +} + +static void +png_icc_tag_name(char *name, png_uint_32 tag) +{ + name[0] = '\''; + name[1] = png_icc_tag_char(tag >> 24); + name[2] = png_icc_tag_char(tag >> 16); + name[3] = png_icc_tag_char(tag >> 8); + name[4] = png_icc_tag_char(tag ); + name[5] = '\''; +} + +static int +is_ICC_signature_char(png_alloc_size_t it) +{ + return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) || + (it >= 97 && it <= 122); +} + +static int +is_ICC_signature(png_alloc_size_t it) +{ + return is_ICC_signature_char(it >> 24) /* checks all the top bits */ && + is_ICC_signature_char((it >> 16) & 0xff) && + is_ICC_signature_char((it >> 8) & 0xff) && + is_ICC_signature_char(it & 0xff); +} + +static int +png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_charp name, png_alloc_size_t value, png_const_charp reason) +{ + size_t pos; + char message[196]; /* see below for calculation */ + + if (colorspace != NULL) + colorspace->flags |= PNG_COLORSPACE_INVALID; + + pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */ + pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */ + pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */ + if (is_ICC_signature(value) != 0) + { + /* So 'value' is at most 4 bytes and the following cast is safe */ + png_icc_tag_name(message+pos, (png_uint_32)value); + pos += 6; /* total +8; less than the else clause */ + message[pos++] = ':'; + message[pos++] = ' '; + } +# ifdef PNG_WARNINGS_SUPPORTED + else + { + char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/ + + pos = png_safecat(message, (sizeof message), pos, + png_format_number(number, number+(sizeof number), + PNG_NUMBER_FORMAT_x, value)); + pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/ + } +# endif + /* The 'reason' is an arbitrary message, allow +79 maximum 195 */ + pos = png_safecat(message, (sizeof message), pos, reason); + PNG_UNUSED(pos) + + /* This is recoverable, but make it unconditionally an app_error on write to + * avoid writing invalid ICC profiles into PNG files (i.e., we handle them + * on read, with a warning, but on write unless the app turns off + * application errors the PNG won't be written.) + */ + png_chunk_report(png_ptr, message, + (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR); + + return 0; +} +#endif /* sRGB || iCCP */ + +#ifdef PNG_sRGB_SUPPORTED +int /* PRIVATE */ +png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, + int intent) +{ + /* sRGB sets known gamma, end points and (from the chunk) intent. */ + /* IMPORTANT: these are not necessarily the values found in an ICC profile + * because ICC profiles store values adapted to a D50 environment; it is + * expected that the ICC profile mediaWhitePointTag will be D50; see the + * checks and code elsewhere to understand this better. + * + * These XYZ values, which are accurate to 5dp, produce rgb to gray + * coefficients of (6968,23435,2366), which are reduced (because they add up + * to 32769 not 32768) to (6968,23434,2366). These are the values that + * libpng has traditionally used (and are the best values given the 15bit + * algorithm used by the rgb to gray code.) + */ + static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */ + { + /* color X Y Z */ + /* red */ 41239, 21264, 1933, + /* green */ 35758, 71517, 11919, + /* blue */ 18048, 7219, 95053 + }; + + /* Do nothing if the colorspace is already invalidated. */ + if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) + return 0; + + /* Check the intent, then check for existing settings. It is valid for the + * PNG file to have cHRM or gAMA chunks along with sRGB, but the values must + * be consistent with the correct values. If, however, this function is + * called below because an iCCP chunk matches sRGB then it is quite + * conceivable that an older app recorded incorrect gAMA and cHRM because of + * an incorrect calculation based on the values in the profile - this does + * *not* invalidate the profile (though it still produces an error, which can + * be ignored.) + */ + if (intent < 0 || intent >= PNG_sRGB_INTENT_LAST) + return png_icc_profile_error(png_ptr, colorspace, "sRGB", + (unsigned)intent, "invalid sRGB rendering intent"); + + if ((colorspace->flags & PNG_COLORSPACE_HAVE_INTENT) != 0 && + colorspace->rendering_intent != intent) + return png_icc_profile_error(png_ptr, colorspace, "sRGB", + (unsigned)intent, "inconsistent rendering intents"); + + if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0) + { + png_benign_error(png_ptr, "duplicate sRGB information ignored"); + return 0; + } + + /* If the standard sRGB cHRM chunk does not match the one from the PNG file + * warn but overwrite the value with the correct one. + */ + if ((colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0 && + !png_colorspace_endpoints_match(&sRGB_xy, &colorspace->end_points_xy, + 100)) + png_chunk_report(png_ptr, "cHRM chunk does not match sRGB", + PNG_CHUNK_ERROR); + + /* This check is just done for the error reporting - the routine always + * returns true when the 'from' argument corresponds to sRGB (2). + */ + (void)png_colorspace_check_gamma(png_ptr, colorspace, PNG_GAMMA_sRGB_INVERSE, + 2/*from sRGB*/); + + /* intent: bugs in GCC force 'int' to be used as the parameter type. */ + colorspace->rendering_intent = (png_uint_16)intent; + colorspace->flags |= PNG_COLORSPACE_HAVE_INTENT; + + /* endpoints */ + colorspace->end_points_xy = sRGB_xy; + colorspace->end_points_XYZ = sRGB_XYZ; + colorspace->flags |= + (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB); + + /* gamma */ + colorspace->gamma = PNG_GAMMA_sRGB_INVERSE; + colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA; + + /* Finally record that we have an sRGB profile */ + colorspace->flags |= + (PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB); + + return 1; /* set */ +} +#endif /* sRGB */ + +#ifdef PNG_iCCP_SUPPORTED +/* Encoded value of D50 as an ICC XYZNumber. From the ICC 2010 spec the value + * is XYZ(0.9642,1.0,0.8249), which scales to: + * + * (63189.8112, 65536, 54060.6464) + */ +static const png_byte D50_nCIEXYZ[12] = + { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d }; + +int /* PRIVATE */ +png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_charp name, png_uint_32 profile_length) +{ + if (profile_length < 132) + return png_icc_profile_error(png_ptr, colorspace, name, profile_length, + "too short"); + + return 1; +} + +int /* PRIVATE */ +png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_charp name, png_uint_32 profile_length, + png_const_bytep profile/* first 132 bytes only */, int color_type) +{ + png_uint_32 temp; + + /* Length check; this cannot be ignored in this code because profile_length + * is used later to check the tag table, so even if the profile seems over + * long profile_length from the caller must be correct. The caller can fix + * this up on read or write by just passing in the profile header length. + */ + temp = png_get_uint_32(profile); + if (temp != profile_length) + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "length does not match profile"); + + temp = (png_uint_32) (*(profile+8)); + if (temp > 3 && (profile_length & 3)) + return png_icc_profile_error(png_ptr, colorspace, name, profile_length, + "invalid length"); + + temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */ + if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */ + profile_length < 132+12*temp) /* truncated tag table */ + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "tag count too large"); + + /* The 'intent' must be valid or we can't store it, ICC limits the intent to + * 16 bits. + */ + temp = png_get_uint_32(profile+64); + if (temp >= 0xffff) /* The ICC limit */ + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "invalid rendering intent"); + + /* This is just a warning because the profile may be valid in future + * versions. + */ + if (temp >= PNG_sRGB_INTENT_LAST) + (void)png_icc_profile_error(png_ptr, NULL, name, temp, + "intent outside defined range"); + + /* At this point the tag table can't be checked because it hasn't necessarily + * been loaded; however, various header fields can be checked. These checks + * are for values permitted by the PNG spec in an ICC profile; the PNG spec + * restricts the profiles that can be passed in an iCCP chunk (they must be + * appropriate to processing PNG data!) + */ + + /* Data checks (could be skipped). These checks must be independent of the + * version number; however, the version number doesn't accomodate changes in + * the header fields (just the known tags and the interpretation of the + * data.) + */ + temp = png_get_uint_32(profile+36); /* signature 'ascp' */ + if (temp != 0x61637370) + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "invalid signature"); + + /* Currently the PCS illuminant/adopted white point (the computational + * white point) are required to be D50, + * however the profile contains a record of the illuminant so perhaps ICC + * expects to be able to change this in the future (despite the rationale in + * the introduction for using a fixed PCS adopted white.) Consequently the + * following is just a warning. + */ + if (memcmp(profile+68, D50_nCIEXYZ, 12) != 0) + (void)png_icc_profile_error(png_ptr, NULL, name, 0/*no tag value*/, + "PCS illuminant is not D50"); + + /* The PNG spec requires this: + * "If the iCCP chunk is present, the image samples conform to the colour + * space represented by the embedded ICC profile as defined by the + * International Color Consortium [ICC]. The colour space of the ICC profile + * shall be an RGB colour space for colour images (PNG colour types 2, 3, and + * 6), or a greyscale colour space for greyscale images (PNG colour types 0 + * and 4)." + * + * This checking code ensures the embedded profile (on either read or write) + * conforms to the specification requirements. Notice that an ICC 'gray' + * color-space profile contains the information to transform the monochrome + * data to XYZ or L*a*b (according to which PCS the profile uses) and this + * should be used in preference to the standard libpng K channel replication + * into R, G and B channels. + * + * Previously it was suggested that an RGB profile on grayscale data could be + * handled. However it it is clear that using an RGB profile in this context + * must be an error - there is no specification of what it means. Thus it is + * almost certainly more correct to ignore the profile. + */ + temp = png_get_uint_32(profile+16); /* data colour space field */ + switch (temp) + { + case 0x52474220: /* 'RGB ' */ + if ((color_type & PNG_COLOR_MASK_COLOR) == 0) + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "RGB color space not permitted on grayscale PNG"); + break; + + case 0x47524159: /* 'GRAY' */ + if ((color_type & PNG_COLOR_MASK_COLOR) != 0) + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "Gray color space not permitted on RGB PNG"); + break; + + default: + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "invalid ICC profile color space"); + } + + /* It is up to the application to check that the profile class matches the + * application requirements; the spec provides no guidance, but it's pretty + * weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer + * ('prtr') or 'spac' (for generic color spaces). Issue a warning in these + * cases. Issue an error for device link or abstract profiles - these don't + * contain the records necessary to transform the color-space to anything + * other than the target device (and not even that for an abstract profile). + * Profiles of these classes may not be embedded in images. + */ + temp = png_get_uint_32(profile+12); /* profile/device class */ + switch (temp) + { + case 0x73636e72: /* 'scnr' */ + case 0x6d6e7472: /* 'mntr' */ + case 0x70727472: /* 'prtr' */ + case 0x73706163: /* 'spac' */ + /* All supported */ + break; + + case 0x61627374: /* 'abst' */ + /* May not be embedded in an image */ + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "invalid embedded Abstract ICC profile"); + + case 0x6c696e6b: /* 'link' */ + /* DeviceLink profiles cannot be interpreted in a non-device specific + * fashion, if an app uses the AToB0Tag in the profile the results are + * undefined unless the result is sent to the intended device, + * therefore a DeviceLink profile should not be found embedded in a + * PNG. + */ + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "unexpected DeviceLink ICC profile class"); + + case 0x6e6d636c: /* 'nmcl' */ + /* A NamedColor profile is also device specific, however it doesn't + * contain an AToB0 tag that is open to misinterpretation. Almost + * certainly it will fail the tests below. + */ + (void)png_icc_profile_error(png_ptr, NULL, name, temp, + "unexpected NamedColor ICC profile class"); + break; + + default: + /* To allow for future enhancements to the profile accept unrecognized + * profile classes with a warning, these then hit the test below on the + * tag content to ensure they are backward compatible with one of the + * understood profiles. + */ + (void)png_icc_profile_error(png_ptr, NULL, name, temp, + "unrecognized ICC profile class"); break; } - /* ERROR RETURN */ + /* For any profile other than a device link one the PCS must be encoded + * either in XYZ or Lab. + */ + temp = png_get_uint_32(profile+20); + switch (temp) + { + case 0x58595a20: /* 'XYZ ' */ + case 0x4c616220: /* 'Lab ' */ + break; + + default: + return png_icc_profile_error(png_ptr, colorspace, name, temp, + "unexpected ICC PCS encoding"); + } + + return 1; +} + +int /* PRIVATE */ +png_icc_check_tag_table(png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_charp name, png_uint_32 profile_length, + png_const_bytep profile /* header plus whole tag table */) +{ + png_uint_32 tag_count = png_get_uint_32(profile+128); + png_uint_32 itag; + png_const_bytep tag = profile+132; /* The first tag */ + + /* First scan all the tags in the table and add bits to the icc_info value + * (temporarily in 'tags'). + */ + for (itag=0; itag < tag_count; ++itag, tag += 12) + { + png_uint_32 tag_id = png_get_uint_32(tag+0); + png_uint_32 tag_start = png_get_uint_32(tag+4); /* must be aligned */ + png_uint_32 tag_length = png_get_uint_32(tag+8);/* not padded */ + + /* The ICC specification does not exclude zero length tags, therefore the + * start might actually be anywhere if there is no data, but this would be + * a clear abuse of the intent of the standard so the start is checked for + * being in range. All defined tag types have an 8 byte header - a 4 byte + * type signature then 0. + */ + if ((tag_start & 3) != 0) + { + /* CNHP730S.icc shipped with Microsoft Windows 64 violates this, it is + * only a warning here because libpng does not care about the + * alignment. + */ + (void)png_icc_profile_error(png_ptr, NULL, name, tag_id, + "ICC profile tag start not a multiple of 4"); + } + + /* This is a hard error; potentially it can cause read outside the + * profile. + */ + if (tag_start > profile_length || tag_length > profile_length - tag_start) + return png_icc_profile_error(png_ptr, colorspace, name, tag_id, + "ICC profile tag outside profile"); + } + + return 1; /* success, maybe with warnings */ +} + +#ifdef PNG_sRGB_SUPPORTED +#if PNG_sRGB_PROFILE_CHECKS >= 0 +/* Information about the known ICC sRGB profiles */ +static const struct +{ + png_uint_32 adler, crc, length; + png_uint_32 md5[4]; + png_byte have_md5; + png_byte is_broken; + png_uint_16 intent; + +# define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0) +# define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)\ + { adler, crc, length, md5, broke, intent }, + +} png_sRGB_checks[] = +{ + /* This data comes from contrib/tools/checksum-icc run on downloads of + * all four ICC sRGB profiles from www.color.org. + */ + /* adler32, crc32, MD5[4], intent, date, length, file-name */ + PNG_ICC_CHECKSUM(0x0a3fd9f6, 0x3b8772b9, + PNG_MD5(0x29f83dde, 0xaff255ae, 0x7842fae4, 0xca83390d), 0, 0, + "2009/03/27 21:36:31", 3048, "sRGB_IEC61966-2-1_black_scaled.icc") + + /* ICC sRGB v2 perceptual no black-compensation: */ + PNG_ICC_CHECKSUM(0x4909e5e1, 0x427ebb21, + PNG_MD5(0xc95bd637, 0xe95d8a3b, 0x0df38f99, 0xc1320389), 1, 0, + "2009/03/27 21:37:45", 3052, "sRGB_IEC61966-2-1_no_black_scaling.icc") + + PNG_ICC_CHECKSUM(0xfd2144a1, 0x306fd8ae, + PNG_MD5(0xfc663378, 0x37e2886b, 0xfd72e983, 0x8228f1b8), 0, 0, + "2009/08/10 17:28:01", 60988, "sRGB_v4_ICC_preference_displayclass.icc") + + /* ICC sRGB v4 perceptual */ + PNG_ICC_CHECKSUM(0x209c35d2, 0xbbef7812, + PNG_MD5(0x34562abf, 0x994ccd06, 0x6d2c5721, 0xd0d68c5d), 0, 0, + "2007/07/25 00:05:37", 60960, "sRGB_v4_ICC_preference.icc") + + /* The following profiles have no known MD5 checksum. If there is a match + * on the (empty) MD5 the other fields are used to attempt a match and + * a warning is produced. The first two of these profiles have a 'cprt' tag + * which suggests that they were also made by Hewlett Packard. + */ + PNG_ICC_CHECKSUM(0xa054d762, 0x5d5129ce, + PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 0, + "2004/07/21 18:57:42", 3024, "sRGB_IEC61966-2-1_noBPC.icc") + + /* This is a 'mntr' (display) profile with a mediaWhitePointTag that does not + * match the D50 PCS illuminant in the header (it is in fact the D65 values, + * so the white point is recorded as the un-adapted value.) The profiles + * below only differ in one byte - the intent - and are basically the same as + * the previous profile except for the mediaWhitePointTag error and a missing + * chromaticAdaptationTag. + */ + PNG_ICC_CHECKSUM(0xf784f3fb, 0x182ea552, + PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/, + "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual") + + PNG_ICC_CHECKSUM(0x0398f3fc, 0xf29e526d, + PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/, + "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative") +}; + +static int +png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr, + png_const_bytep profile, uLong adler) +{ + /* The quick check is to verify just the MD5 signature and trust the + * rest of the data. Because the profile has already been verified for + * correctness this is safe. png_colorspace_set_sRGB will check the 'intent' + * field too, so if the profile has been edited with an intent not defined + * by sRGB (but maybe defined by a later ICC specification) the read of + * the profile will fail at that point. + */ + + png_uint_32 length = 0; + png_uint_32 intent = 0x10000; /* invalid */ +#if PNG_sRGB_PROFILE_CHECKS > 1 + uLong crc = 0; /* the value for 0 length data */ +#endif + unsigned int i; + +#ifdef PNG_SET_OPTION_SUPPORTED + /* First see if PNG_SKIP_sRGB_CHECK_PROFILE has been set to "on" */ + if (((png_ptr->options >> PNG_SKIP_sRGB_CHECK_PROFILE) & 3) == + PNG_OPTION_ON) + return 0; +#endif + + for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i) + { + if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] && + png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] && + png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] && + png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3]) + { + /* This may be one of the old HP profiles without an MD5, in that + * case we can only use the length and Adler32 (note that these + * are not used by default if there is an MD5!) + */ +# if PNG_sRGB_PROFILE_CHECKS == 0 + if (png_sRGB_checks[i].have_md5 != 0) + return 1+png_sRGB_checks[i].is_broken; +# endif + + /* Profile is unsigned or more checks have been configured in. */ + if (length == 0) + { + length = png_get_uint_32(profile); + intent = png_get_uint_32(profile+64); + } + + /* Length *and* intent must match */ + if (length == (png_uint_32) png_sRGB_checks[i].length && + intent == (png_uint_32) png_sRGB_checks[i].intent) + { + /* Now calculate the adler32 if not done already. */ + if (adler == 0) + { + adler = adler32(0, NULL, 0); + adler = adler32(adler, profile, length); + } + + if (adler == png_sRGB_checks[i].adler) + { + /* These basic checks suggest that the data has not been + * modified, but if the check level is more than 1 perform + * our own crc32 checksum on the data. + */ +# if PNG_sRGB_PROFILE_CHECKS > 1 + if (crc == 0) + { + crc = crc32(0, NULL, 0); + crc = crc32(crc, profile, length); + } + + /* So this check must pass for the 'return' below to happen. + */ + if (crc == png_sRGB_checks[i].crc) +# endif + { + if (png_sRGB_checks[i].is_broken != 0) + { + /* These profiles are known to have bad data that may cause + * problems if they are used, therefore attempt to + * discourage their use, skip the 'have_md5' warning below, + * which is made irrelevant by this error. + */ + png_chunk_report(png_ptr, "known incorrect sRGB profile", + PNG_CHUNK_ERROR); + } + + /* Warn that this being done; this isn't even an error since + * the profile is perfectly valid, but it would be nice if + * people used the up-to-date ones. + */ + else if (png_sRGB_checks[i].have_md5 == 0) + { + png_chunk_report(png_ptr, + "out-of-date sRGB profile with no signature", + PNG_CHUNK_WARNING); + } + + return 1+png_sRGB_checks[i].is_broken; + } + } + +# if PNG_sRGB_PROFILE_CHECKS > 0 + /* The signature matched, but the profile had been changed in some + * way. This probably indicates a data error or uninformed hacking. + * Fall through to "no match". + */ + png_chunk_report(png_ptr, + "Not recognizing known sRGB profile that has been edited", + PNG_CHUNK_WARNING); + break; +# endif + } + } + } + + return 0; /* no match */ +} +#endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */ + +void /* PRIVATE */ +png_icc_set_sRGB(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_const_bytep profile, uLong adler) +{ + /* Is this profile one of the known ICC sRGB profiles? If it is, just set + * the sRGB information. + */ +#if PNG_sRGB_PROFILE_CHECKS >= 0 + if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0) +#endif + (void)png_colorspace_set_sRGB(png_ptr, colorspace, + (int)/*already checked*/png_get_uint_32(profile+64)); +} +#endif /* sRGB */ + +int /* PRIVATE */ +png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_charp name, png_uint_32 profile_length, png_const_bytep profile, + int color_type) +{ + if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) + return 0; + + if (png_icc_check_length(png_ptr, colorspace, name, profile_length) != 0 && + png_icc_check_header(png_ptr, colorspace, name, profile_length, profile, + color_type) != 0 && + png_icc_check_tag_table(png_ptr, colorspace, name, profile_length, + profile) != 0) + { +# ifdef PNG_sRGB_SUPPORTED + /* If no sRGB support, don't try storing sRGB information */ + png_icc_set_sRGB(png_ptr, colorspace, profile, 0); +# endif + return 1; + } + + /* Failure case */ return 0; } +#endif /* iCCP */ + +#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED +void /* PRIVATE */ +png_colorspace_set_rgb_coefficients(png_structrp png_ptr) +{ + /* Set the rgb_to_gray coefficients from the colorspace. */ + if (png_ptr->rgb_to_gray_coefficients_set == 0 && + (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) + { + /* png_set_background has not been called, get the coefficients from the Y + * values of the colorspace colorants. + */ + png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y; + png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y; + png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y; + png_fixed_point total = r+g+b; + + if (total > 0 && + r >= 0 && png_muldiv(&r, r, 32768, total) && r >= 0 && r <= 32768 && + g >= 0 && png_muldiv(&g, g, 32768, total) && g >= 0 && g <= 32768 && + b >= 0 && png_muldiv(&b, b, 32768, total) && b >= 0 && b <= 32768 && + r+g+b <= 32769) + { + /* We allow 0 coefficients here. r+g+b may be 32769 if two or + * all of the coefficients were rounded up. Handle this by + * reducing the *largest* coefficient by 1; this matches the + * approach used for the default coefficients in pngrtran.c + */ + int add = 0; + + if (r+g+b > 32768) + add = -1; + else if (r+g+b < 32768) + add = 1; + + if (add != 0) + { + if (g >= r && g >= b) + g += add; + else if (r >= g && r >= b) + r += add; + else + b += add; + } + + /* Check for an internal error. */ + if (r+g+b != 32768) + png_error(png_ptr, + "internal error handling cHRM coefficients"); + + else + { + png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r; + png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g; + } + } + + /* This is a png_error at present even though it could be ignored - + * it should never happen, but it is important that if it does, the + * bug is fixed. + */ + else + png_error(png_ptr, "internal error handling cHRM->XYZ"); + } +} +#endif /* READ_RGB_TO_GRAY */ + +#endif /* COLORSPACE */ + +#ifdef __GNUC__ +/* This exists solely to work round a warning from GNU C. */ +static int /* PRIVATE */ +png_gt(size_t a, size_t b) +{ + return a > b; +} +#else +# define png_gt(a,b) ((a) > (b)) #endif void /* PRIVATE */ -png_check_IHDR(png_structp png_ptr, +png_check_IHDR(png_const_structrp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type) @@ -1180,36 +2490,47 @@ png_check_IHDR(png_structp png_ptr, error = 1; } - if (height == 0) + if (width > PNG_UINT_31_MAX) { - png_warning(png_ptr, "Image height is zero in IHDR"); + png_warning(png_ptr, "Invalid image width in IHDR"); error = 1; } -# ifdef PNG_SET_USER_LIMITS_SUPPORTED - if (width > png_ptr->user_width_max) + if (png_gt(((width + 7) & (~7)), + ((PNG_SIZE_MAX + - 48 /* big_row_buf hack */ + - 1) /* filter byte */ + / 8) /* 8-byte RGBA pixels */ + - 1)) /* extra max_pixel_depth pad */ + { + /* The size of the row must be within the limits of this architecture. + * Because the read code can perform arbitrary transformations the + * maximum size is checked here. Because the code in png_read_start_row + * adds extra space "for safety's sake" in several places a conservative + * limit is used here. + * + * NOTE: it would be far better to check the size that is actually used, + * but the effect in the real world is minor and the changes are more + * extensive, therefore much more dangerous and much more difficult to + * write in a way that avoids compiler warnings. + */ + png_warning(png_ptr, "Image width is too large for this architecture"); + error = 1; + } -# else +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + if (width > png_ptr->user_width_max) +#else if (width > PNG_USER_WIDTH_MAX) -# endif +#endif { png_warning(png_ptr, "Image width exceeds user limit in IHDR"); error = 1; } -# ifdef PNG_SET_USER_LIMITS_SUPPORTED - if (height > png_ptr->user_height_max) -# else - if (height > PNG_USER_HEIGHT_MAX) -# endif + if (height == 0) { - png_warning(png_ptr, "Image height exceeds user limit in IHDR"); - error = 1; - } - - if (width > PNG_UINT_31_MAX) - { - png_warning(png_ptr, "Invalid image width in IHDR"); + png_warning(png_ptr, "Image height is zero in IHDR"); error = 1; } @@ -1219,13 +2540,15 @@ png_check_IHDR(png_structp png_ptr, error = 1; } - if (width > (PNG_UINT_32_MAX - >> 3) /* 8-byte RGBA pixels */ - - 48 /* bigrowbuf hack */ - - 1 /* filter byte */ - - 7*8 /* rounding of width to multiple of 8 pixels */ - - 8) /* extra max_pixel_depth pad */ - png_warning(png_ptr, "Width is too large for libpng to process pixels"); +#ifdef PNG_SET_USER_LIMITS_SUPPORTED + if (height > png_ptr->user_height_max) +#else + if (height > PNG_USER_HEIGHT_MAX) +#endif + { + png_warning(png_ptr, "Image height exceeds user limit in IHDR"); + error = 1; + } /* Check other values */ if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && @@ -1263,7 +2586,7 @@ png_check_IHDR(png_structp png_ptr, error = 1; } -# ifdef PNG_MNG_FEATURES_SUPPORTED +#ifdef PNG_MNG_FEATURES_SUPPORTED /* Accept filter_method 64 (intrapixel differencing) only if * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and * 2. Libpng did not read a PNG signature (this filter_method is only @@ -1273,13 +2596,13 @@ png_check_IHDR(png_structp png_ptr, * 4. The filter_method is 64 and * 5. The color_type is RGB or RGBA */ - if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) && - png_ptr->mng_features_permitted) + if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && + png_ptr->mng_features_permitted != 0) png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); if (filter_type != PNG_FILTER_TYPE_BASE) { - if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && (color_type == PNG_COLOR_TYPE_RGB || @@ -1289,20 +2612,20 @@ png_check_IHDR(png_structp png_ptr, error = 1; } - if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) + if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0) { png_warning(png_ptr, "Invalid filter method in IHDR"); error = 1; } } -# else +#else if (filter_type != PNG_FILTER_TYPE_BASE) { png_warning(png_ptr, "Unknown filter method in IHDR"); error = 1; } -# endif +#endif if (error == 1) png_error(png_ptr, "Invalid IHDR data"); @@ -1349,7 +2672,7 @@ png_check_fp_number(png_const_charp string, png_size_t size, int *statep, switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY)) { case PNG_FP_INTEGER + PNG_FP_SAW_SIGN: - if (state & PNG_FP_SAW_ANY) + if ((state & PNG_FP_SAW_ANY) != 0) goto PNG_FP_End; /* not a part of the number */ png_fp_add(state, type); @@ -1357,10 +2680,10 @@ png_check_fp_number(png_const_charp string, png_size_t size, int *statep, case PNG_FP_INTEGER + PNG_FP_SAW_DOT: /* Ok as trailer, ok as lead of fraction. */ - if (state & PNG_FP_SAW_DOT) /* two dots */ + if ((state & PNG_FP_SAW_DOT) != 0) /* two dots */ goto PNG_FP_End; - else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */ + else if ((state & PNG_FP_SAW_DIGIT) != 0) /* trailing dot? */ png_fp_add(state, type); else @@ -1369,7 +2692,7 @@ png_check_fp_number(png_const_charp string, png_size_t size, int *statep, break; case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT: - if (state & PNG_FP_SAW_DOT) /* delayed fraction */ + if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */ png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); png_fp_add(state, type | PNG_FP_WAS_VALID); @@ -1407,7 +2730,7 @@ png_check_fp_number(png_const_charp string, png_size_t size, int *statep, break; case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN: - if (state & PNG_FP_SAW_ANY) + if ((state & PNG_FP_SAW_ANY) != 0) goto PNG_FP_End; /* not a part of the number */ png_fp_add(state, PNG_FP_SAW_SIGN); @@ -1450,15 +2773,15 @@ png_check_fp_string(png_const_charp string, png_size_t size) int state=0; png_size_t char_index=0; - if (png_check_fp_number(string, size, &state, &char_index) && + if (png_check_fp_number(string, size, &state, &char_index) != 0 && (char_index == size || string[char_index] == 0)) return state /* must be non-zero - see above */; return 0; /* i.e. fail */ } -#endif /* pCAL or sCAL */ +#endif /* pCAL || sCAL */ -#ifdef PNG_READ_sCAL_SUPPORTED +#ifdef PNG_sCAL_SUPPORTED # ifdef PNG_FLOATING_POINT_SUPPORTED /* Utility used below - a simple accurate power of ten from an integral * exponent. @@ -1467,7 +2790,7 @@ static double png_pow10(int power) { int recip = 0; - double d = 1.0; + double d = 1; /* Handle negative exponent with a reciprocal at the end because * 10 is exact whereas .1 is inexact in base 2 @@ -1481,7 +2804,7 @@ png_pow10(int power) if (power > 0) { /* Decompose power bitwise. */ - double mult = 10.0; + double mult = 10; do { if (power & 1) d *= mult; @@ -1490,7 +2813,7 @@ png_pow10(int power) } while (power > 0); - if (recip) d = 1/d; + if (recip != 0) d = 1/d; } /* else power is 0 and d is 1 */ @@ -1501,7 +2824,7 @@ png_pow10(int power) * precision. */ void /* PRIVATE */ -png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, +png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size, double fp, unsigned int precision) { /* We use standard functions from math.h, but not printf because @@ -1528,7 +2851,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, if (fp >= DBL_MIN && fp <= DBL_MAX) { - int exp_b10; /* A base 10 exponent */ + int exp_b10; /* A base 10 exponent */ double base; /* 10^exp_b10 */ /* First extract a base 10 exponent of the number, @@ -1576,7 +2899,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, */ { - int czero, clead, cdigits; + unsigned int czero, clead, cdigits; char exponent[10]; /* Allow up to two leading zeros - this will not lengthen @@ -1600,21 +2923,20 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, { double d; - fp *= 10.0; - + fp *= 10; /* Use modf here, not floor and subtract, so that * the separation is done in one step. At the end * of the loop don't break the number into parts so * that the final digit is rounded. */ - if (cdigits+czero-clead+1 < (int)precision) + if (cdigits+czero+1 < precision+clead) fp = modf(fp, &d); else { d = floor(fp + .5); - if (d > 9.0) + if (d > 9) { /* Rounding up to 10, handle that here. */ if (czero > 0) @@ -1622,10 +2944,9 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, --czero, d = 1; if (cdigits == 0) --clead; } - else { - while (cdigits > 0 && d > 9.0) + while (cdigits > 0 && d > 9) { int ch = *--ascii; @@ -1650,7 +2971,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, * exponent but take into account the leading * decimal point. */ - if (d > 9.0) /* cdigits == 0 */ + if (d > 9) /* cdigits == 0 */ { if (exp_b10 == (-1)) { @@ -1671,19 +2992,18 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, ++exp_b10; /* In all cases we output a '1' */ - d = 1.0; + d = 1; } } } fp = 0; /* Guarantees termination below. */ } - if (d == 0.0) + if (d == 0) { ++czero; if (cdigits == 0) ++clead; } - else { /* Included embedded zeros in the digit count. */ @@ -1707,22 +3027,22 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, if (exp_b10 != (-1)) { - if (exp_b10 == 0) *ascii++ = 46, --size; /* counted - above */ + if (exp_b10 == 0) + *ascii++ = 46, --size; /* counted above */ + --exp_b10; } - *ascii++ = (char)(48 + (int)d), ++cdigits; } } - while (cdigits+czero-clead < (int)precision && fp > DBL_MIN); + while (cdigits+czero < precision+clead && fp > DBL_MIN); /* The total output count (max) is now 4+precision */ /* Check for an exponent, if we don't need one we are * done and just need to terminate the string. At * this point exp_b10==(-1) is effectively if flag - it got - * to '-1' because of the decrement after outputing + * to '-1' because of the decrement after outputting * the decimal point above (the exponent required is * *not* -1!) */ @@ -1730,7 +3050,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, { /* The following only happens if we didn't output the * leading zeros above for negative exponent, so this - * doest add to the digit requirement. Note that the + * doesn't add to the digit requirement. Note that the * two zeros here can only be output if the two leading * zeros were *not* output, so this doesn't increase * the output count. @@ -1783,7 +3103,7 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, /* Need another size check here for the exponent digits, so * this need not be considered above. */ - if ((int)size > cdigits) + if (size > cdigits) { while (cdigits > 0) *ascii++ = exponent[--cdigits]; @@ -1819,8 +3139,8 @@ png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, /* Function to format a fixed point value in ASCII. */ void /* PRIVATE */ -png_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size, - png_fixed_point fp) +png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii, + png_size_t size, png_fixed_point fp) { /* Require space for 10 decimal digits, a decimal point, a minus sign and a * trailing \0, 13 characters: @@ -1831,7 +3151,7 @@ png_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size, /* Avoid overflow here on the minimum integer. */ if (fp < 0) - *ascii++ = 45, --size, num = -fp; + *ascii++ = 45, num = -fp; else num = fp; @@ -1887,24 +3207,33 @@ png_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size, png_error(png_ptr, "ASCII conversion buffer too small"); } # endif /* FIXED_POINT */ -#endif /* READ_SCAL */ +#endif /* SCAL */ #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ - !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) + !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ + (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ + defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ + defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ + (defined(PNG_sCAL_SUPPORTED) && \ + defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) png_fixed_point -png_fixed(png_structp png_ptr, double fp, png_const_charp text) +png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text) { double r = floor(100000 * fp + .5); if (r > 2147483647. || r < -2147483648.) png_fixed_error(png_ptr, text); +# ifndef PNG_ERROR_TEXT_SUPPORTED + PNG_UNUSED(text) +# endif + return (png_fixed_point)r; } #endif -#if defined(PNG_READ_GAMMA_SUPPORTED) || \ - defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG__READ_pHYs_SUPPORTED) +#if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_COLORSPACE_SUPPORTED) ||\ + defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) /* muldiv functions */ /* This API takes signed arguments and rounds the result to the nearest * integer (or, for a fixed point number - the standard argument - to @@ -2008,11 +3337,12 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, if (s00 >= (D >> 1)) ++result; - if (negative) + if (negative != 0) result = -result; /* Check for overflow. */ - if ((negative && result <= 0) || (!negative && result >= 0)) + if ((negative != 0 && result <= 0) || + (negative == 0 && result >= 0)) { *res = result; return 1; @@ -2031,12 +3361,12 @@ png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, * result. */ png_fixed_point -png_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times, +png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times, png_int_32 divisor) { png_fixed_point result; - if (png_muldiv(&result, a, times, divisor)) + if (png_muldiv(&result, a, times, divisor) != 0) return result; png_warning(png_ptr, "fixed point overflow ignored"); @@ -2044,7 +3374,7 @@ png_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times, } #endif -#ifdef PNG_READ_GAMMA_SUPPORTED /* more fixed point functions for gamma */ +#ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */ /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ png_fixed_point png_reciprocal(png_fixed_point a) @@ -2057,13 +3387,26 @@ png_reciprocal(png_fixed_point a) #else png_fixed_point res; - if (png_muldiv(&res, 100000, 100000, a)) + if (png_muldiv(&res, 100000, 100000, a) != 0) return res; #endif return 0; /* error/overflow */ } +/* This is the shared test on whether a gamma value is 'significant' - whether + * it is worth doing gamma correction. + */ +int /* PRIVATE */ +png_gamma_significant(png_fixed_point gamma_val) +{ + return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || + gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; +} +#endif + +#ifdef PNG_READ_GAMMA_SUPPORTED +#ifdef PNG_16BIT_SUPPORTED /* A local convenience routine. */ static png_fixed_point png_product2(png_fixed_point a, png_fixed_point b) @@ -2079,12 +3422,13 @@ png_product2(png_fixed_point a, png_fixed_point b) #else png_fixed_point res; - if (png_muldiv(&res, a, b, 100000)) + if (png_muldiv(&res, a, b, 100000) != 0) return res; #endif return 0; /* overflow */ } +#endif /* 16BIT */ /* The inverse of the above. */ png_fixed_point @@ -2092,12 +3436,15 @@ png_reciprocal2(png_fixed_point a, png_fixed_point b) { /* The required result is 1/a * 1/b; the following preserves accuracy. */ #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED - double r = 1E15/a; - r /= b; - r = floor(r+.5); + if (a != 0 && b != 0) + { + double r = 1E15/a; + r /= b; + r = floor(r+.5); - if (r <= 2147483647. && r >= -2147483648.) - return (png_fixed_point)r; + if (r <= 2147483647. && r >= -2147483648.) + return (png_fixed_point)r; + } #else /* This may overflow because the range of png_fixed_point isn't symmetric, * but this API is only used for the product of file and screen gamma so it @@ -2114,73 +3461,28 @@ png_reciprocal2(png_fixed_point a, png_fixed_point b) } #endif /* READ_GAMMA */ -#ifdef PNG_CHECK_cHRM_SUPPORTED -/* Added at libpng version 1.2.34 (Dec 8, 2008) and 1.4.0 (Jan 2, - * 2010: moved from pngset.c) */ -/* - * Multiply two 32-bit numbers, V1 and V2, using 32-bit - * arithmetic, to produce a 64-bit result in the HI/LO words. - * - * A B - * x C D - * ------ - * AD || BD - * AC || CB || 0 - * - * where A and B are the high and low 16-bit words of V1, - * C and D are the 16-bit words of V2, AD is the product of - * A and D, and X || Y is (X << 16) + Y. -*/ - -void /* PRIVATE */ -png_64bit_product (long v1, long v2, unsigned long *hi_product, - unsigned long *lo_product) -{ - int a, b, c, d; - long lo, hi, x, y; - - a = (v1 >> 16) & 0xffff; - b = v1 & 0xffff; - c = (v2 >> 16) & 0xffff; - d = v2 & 0xffff; - - lo = b * d; /* BD */ - x = a * d + c * b; /* AD + CB */ - y = ((lo >> 16) & 0xffff) + x; - - lo = (lo & 0xffff) | ((y & 0xffff) << 16); - hi = (y >> 16) & 0xffff; - - hi += a * c; /* AC */ - - *hi_product = (unsigned long)hi; - *lo_product = (unsigned long)lo; -} -#endif /* CHECK_cHRM */ - #ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */ #ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED /* Fixed point gamma. + * + * The code to calculate the tables used below can be found in the shell script + * contrib/tools/intgamma.sh * * To calculate gamma this code implements fast log() and exp() calls using only * fixed point arithmetic. This code has sufficient precision for either 8-bit * or 16-bit sample values. * * The tables used here were calculated using simple 'bc' programs, but C double - * precision floating point arithmetic would work fine. The programs are given - * at the head of each table. + * precision floating point arithmetic would work fine. * * 8-bit log table * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to * 255, so it's the base 2 logarithm of a normalized 8-bit floating point * mantissa. The numbers are 32-bit fractions. */ -static png_uint_32 +static const png_uint_32 png_8bit_l2[128] = { -# ifdef PNG_DO_BC - for (i=128;i<256;++i) { .5 - l(i/255)/l(2)*65536*65536; } -# else 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U, 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U, 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U, @@ -2203,7 +3505,6 @@ png_8bit_l2[128] = 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U, 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U, 24347096U, 0U -# endif #if 0 /* The following are the values for 16-bit tables - these work fine for the @@ -2226,18 +3527,18 @@ png_8bit_l2[128] = #endif }; -PNG_STATIC png_int_32 +static png_int_32 png_log8bit(unsigned int x) { unsigned int lg2 = 0; /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log, * because the log is actually negate that means adding 1. The final * returned value thus has the range 0 (for 255 input) to 7.994 (for 1 - * input), return 7.99998 for the overflow (log 0) case - so the result is + * input), return -1 for the overflow (log 0) case, - so the result is * always at most 19 bits. */ if ((x &= 0xff) == 0) - return 0xffffffff; + return -1; if ((x & 0xf0) == 0) lg2 = 4, x <<= 4; @@ -2282,14 +3583,15 @@ png_log8bit(unsigned int x) * Zero (257): 0 * End (258): 23499 */ -PNG_STATIC png_int_32 +#ifdef PNG_16BIT_SUPPORTED +static png_int_32 png_log16bit(png_uint_32 x) { unsigned int lg2 = 0; /* As above, but now the input has 16 bits. */ if ((x &= 0xffff) == 0) - return 0xffffffff; + return -1; if ((x & 0xff00) == 0) lg2 = 8, x <<= 8; @@ -2332,13 +3634,14 @@ png_log16bit(png_uint_32 x) /* Safe, because the result can't have more than 20 bits: */ return (png_int_32)((lg2 + 2048) >> 12); } +#endif /* 16BIT */ /* The 'exp()' case must invert the above, taking a 20-bit fixed point * logarithmic value and returning a 16 or 8-bit number as appropriate. In * each case only the low 16 bits are relevant - the fraction - since the * integer bits (the top 4) simply determine a shift. * - * The worst case is the 16-bit distinction between 65535 and 65534, this + * The worst case is the 16-bit distinction between 65535 and 65534. This * requires perhaps spurious accuracy in the decoding of the logarithm to * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance * of getting this accuracy in practice. @@ -2347,21 +3650,17 @@ png_log16bit(png_uint_32 x) * frational part of the logarithm by using an accurate 32-bit value from the * top four fractional bits then multiplying in the remaining bits. */ -static png_uint_32 +static const png_uint_32 png_32bit_exp[16] = { -# ifdef PNG_DO_BC - for (i=0;i<16;++i) { .5 + e(-i/16*l(2))*2^32; } -# else /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */ 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U, 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U, 2553802834U, 2445529972U, 2341847524U, 2242560872U -# endif }; /* Adjustment table; provided to explain the numbers in the code below. */ -#ifdef PNG_DO_BC +#if 0 for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} 11 44937.64284865548751208448 10 45180.98734845585101160448 @@ -2377,13 +3676,13 @@ for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} 0 45425.85339951654943850496 #endif -PNG_STATIC png_uint_32 +static png_uint_32 png_exp(png_fixed_point x) { if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ { /* Obtain a 4-bit approximation */ - png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf]; + png_uint_32 e = png_32bit_exp[(x >> 12) & 0x0f]; /* Incorporate the low 12 bits - these decrease the returned value by * multiplying by a number less than 1 if the bit is set. The multiplier @@ -2425,21 +3724,22 @@ png_exp(png_fixed_point x) return 0; } -PNG_STATIC png_byte +static png_byte png_exp8bit(png_fixed_point lg2) { /* Get a 32-bit value: */ png_uint_32 x = png_exp(lg2); - /* Convert the 32-bit value to 0..255 by multiplying by 256-1, note that the + /* Convert the 32-bit value to 0..255 by multiplying by 256-1. Note that the * second, rounding, step can't overflow because of the first, subtraction, * step. */ x -= x >> 8; - return (png_byte)((x + 0x7fffffU) >> 24); + return (png_byte)(((x + 0x7fffffU) >> 24) & 0xff); } -PNG_STATIC png_uint_16 +#ifdef PNG_16BIT_SUPPORTED +static png_uint_16 png_exp16bit(png_fixed_point lg2) { /* Get a 32-bit value: */ @@ -2449,6 +3749,7 @@ png_exp16bit(png_fixed_point lg2) x -= x >> 16; return (png_uint_16)((x + 32767U) >> 16); } +#endif /* 16BIT */ #endif /* FLOATING_ARITHMETIC */ png_byte @@ -2457,13 +3758,37 @@ png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) if (value > 0 && value < 255) { # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED - double r = floor(255*pow(value/255.,gamma_val*.00001)+.5); + /* 'value' is unsigned, ANSI-C90 requires the compiler to correctly + * convert this to a floating point value. This includes values that + * would overflow if 'value' were to be converted to 'int'. + * + * Apparently GCC, however, does an intermediate conversion to (int) + * on some (ARM) but not all (x86) platforms, possibly because of + * hardware FP limitations. (E.g. if the hardware conversion always + * assumes the integer register contains a signed value.) This results + * in ANSI-C undefined behavior for large values. + * + * Other implementations on the same machine might actually be ANSI-C90 + * conformant and therefore compile spurious extra code for the large + * values. + * + * We can be reasonably sure that an unsigned to float conversion + * won't be faster than an int to float one. Therefore this code + * assumes responsibility for the undefined behavior, which it knows + * can't happen because of the check above. + * + * Note the argument to this routine is an (unsigned int) because, on + * 16-bit platforms, it is assigned a value which might be out of + * range for an (int); that would result in undefined behavior in the + * caller if the *argument* ('value') were to be declared (int). + */ + double r = floor(255*pow((int)/*SAFE*/value/255.,gamma_val*.00001)+.5); return (png_byte)r; # else png_int_32 lg2 = png_log8bit(value); png_fixed_point res; - if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) + if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0) return png_exp8bit(res); /* Overflow. */ @@ -2471,22 +3796,29 @@ png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) # endif } - return (png_byte)value; + return (png_byte)(value & 0xff); } +#ifdef PNG_16BIT_SUPPORTED png_uint_16 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) { if (value > 0 && value < 65535) { # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED - double r = floor(65535*pow(value/65535.,gamma_val*.00001)+.5); + /* The same (unsigned int)->(double) constraints apply here as above, + * however in this case the (unsigned int) to (int) conversion can + * overflow on an ANSI-C90 compliant system so the cast needs to ensure + * that this is not possible. + */ + double r = floor(65535*pow((png_int_32)value/65535., + gamma_val*.00001)+.5); return (png_uint_16)r; # else png_int_32 lg2 = png_log16bit(value); png_fixed_point res; - if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) + if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0) return png_exp16bit(res); /* Overflow. */ @@ -2496,6 +3828,7 @@ png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) return (png_uint_16)value; } +#endif /* 16BIT */ /* This does the right thing based on the bit_depth field of the * png_struct, interpreting values as 8-bit or 16-bit. While the result @@ -2503,28 +3836,24 @@ png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) * 8-bit (as are the arguments.) */ png_uint_16 /* PRIVATE */ -png_gamma_correct(png_structp png_ptr, unsigned int value, +png_gamma_correct(png_structrp png_ptr, unsigned int value, png_fixed_point gamma_val) { if (png_ptr->bit_depth == 8) return png_gamma_8bit_correct(value, gamma_val); +#ifdef PNG_16BIT_SUPPORTED else return png_gamma_16bit_correct(value, gamma_val); +#else + /* should not reach this */ + return 0; +#endif /* 16BIT */ } -/* This is the shared test on whether a gamma value is 'significant' - whether - * it is worth doing gamma correction. - */ -int /* PRIVATE */ -png_gamma_significant(png_fixed_point gamma_val) -{ - return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || - gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; -} - +#ifdef PNG_16BIT_SUPPORTED /* Internal function to build a single 16-bit table - the table consists of - * 'num' 256-entry subtables, where 'num' is determined by 'shift' - the amount + * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount * to shift the input values right (or 16-number_of_signifiant_bits). * * The caller is responsible for ensuring that the table gets cleaned up on @@ -2532,27 +3861,33 @@ png_gamma_significant(png_fixed_point gamma_val) * should be somewhere that will be cleaned. */ static void -png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, +png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable, PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) { /* Various values derived from 'shift': */ PNG_CONST unsigned int num = 1U << (8U - shift); +#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED + /* CSE the division and work round wacky GCC warnings (see the comments + * in png_gamma_8bit_correct for where these come from.) + */ + PNG_CONST double fmax = 1./(((png_int_32)1 << (16U - shift))-1); +#endif PNG_CONST unsigned int max = (1U << (16U - shift))-1U; PNG_CONST unsigned int max_by_2 = 1U << (15U-shift); unsigned int i; png_uint_16pp table = *ptable = - (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p)); + (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); for (i = 0; i < num; i++) { png_uint_16p sub_table = table[i] = - (png_uint_16p)png_malloc(png_ptr, 256 * png_sizeof(png_uint_16)); + (png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16))); /* The 'threshold' test is repeated here because it can arise for one of * the 16-bit tables even if the others don't hit it. */ - if (png_gamma_significant(gamma_val)) + if (png_gamma_significant(gamma_val) != 0) { /* The old code would overflow at the end and this would cause the * 'pow' function to return a result >1, resulting in an @@ -2568,10 +3903,13 @@ png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, png_uint_32 ig = (j << (8-shift)) + i; # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED /* Inline the 'max' scaling operation: */ - double d = floor(65535*pow(ig/(double)max, gamma_val*.00001)+.5); + /* See png_gamma_8bit_correct for why the cast to (int) is + * required here. + */ + double d = floor(65535.*pow(ig*fmax, gamma_val*.00001)+.5); sub_table[j] = (png_uint_16)d; # else - if (shift) + if (shift != 0) ig = (ig * 65535U + max_by_2)/max; sub_table[j] = png_gamma_16bit_correct(ig, gamma_val); @@ -2587,7 +3925,7 @@ png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, { png_uint_32 ig = (j << (8-shift)) + i; - if (shift) + if (shift != 0) ig = (ig * 65535U + max_by_2)/max; sub_table[j] = (png_uint_16)ig; @@ -2600,7 +3938,7 @@ png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, * required. */ static void -png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable, +png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable, PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) { PNG_CONST unsigned int num = 1U << (8U - shift); @@ -2609,15 +3947,15 @@ png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable, png_uint_32 last; png_uint_16pp table = *ptable = - (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p)); + (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); - /* 'num' is the number of tables and also the number of low bits of the - * input 16-bit value used to select a table. Each table is itself indexed - * by the high 8 bits of the value. + /* 'num' is the number of tables and also the number of low bits of low + * bits of the input 16-bit value used to select a table. Each table is + * itself indexed by the high 8 bits of the value. */ for (i = 0; i < num; i++) table[i] = (png_uint_16p)png_malloc(png_ptr, - 256 * png_sizeof(png_uint_16)); + 256 * (sizeof (png_uint_16))); /* 'gamma_val' is set to the reciprocal of the value calculated above, so * pow(out,g) is an *input* value. 'last' is the last input value set. @@ -2661,34 +3999,38 @@ png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable, last++; } } +#endif /* 16BIT */ /* Build a single 8-bit table: same as the 16-bit case but much simpler (and * typically much faster). Note that libpng currently does no sBIT processing * (apparently contrary to the spec) so a 256-entry table is always generated. */ static void -png_build_8bit_table(png_structp png_ptr, png_bytepp ptable, +png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable, PNG_CONST png_fixed_point gamma_val) { unsigned int i; png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256); - if (png_gamma_significant(gamma_val)) for (i=0; i<256; i++) - table[i] = png_gamma_8bit_correct(i, gamma_val); + if (png_gamma_significant(gamma_val) != 0) + for (i=0; i<256; i++) + table[i] = png_gamma_8bit_correct(i, gamma_val); - else for (i=0; i<256; ++i) - table[i] = (png_byte)i; + else + for (i=0; i<256; ++i) + table[i] = (png_byte)(i & 0xff); } /* Used from png_read_destroy and below to release the memory used by the gamma * tables. */ void /* PRIVATE */ -png_destroy_gamma_table(png_structp png_ptr) +png_destroy_gamma_table(png_structrp png_ptr) { png_free(png_ptr, png_ptr->gamma_table); png_ptr->gamma_table = NULL; +#ifdef PNG_16BIT_SUPPORTED if (png_ptr->gamma_16_table != NULL) { int i; @@ -2700,6 +4042,7 @@ png_destroy_gamma_table(png_structp png_ptr) png_free(png_ptr, png_ptr->gamma_16_table); png_ptr->gamma_16_table = NULL; } +#endif /* 16BIT */ #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ @@ -2709,6 +4052,7 @@ png_destroy_gamma_table(png_structp png_ptr) png_free(png_ptr, png_ptr->gamma_to_1); png_ptr->gamma_to_1 = NULL; +#ifdef PNG_16BIT_SUPPORTED if (png_ptr->gamma_16_from_1 != NULL) { int i; @@ -2731,6 +4075,7 @@ png_destroy_gamma_table(png_structp png_ptr) png_free(png_ptr, png_ptr->gamma_16_to_1); png_ptr->gamma_16_to_1 = NULL; } +#endif /* 16BIT */ #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ } @@ -2740,7 +4085,7 @@ png_destroy_gamma_table(png_structp png_ptr) * we don't need to allocate > 64K chunks for a full 16-bit table. */ void /* PRIVATE */ -png_build_gamma_table(png_structp png_ptr, int bit_depth) +png_build_gamma_table(png_structrp png_ptr, int bit_depth) { png_debug(1, "in png_build_gamma_table"); @@ -2759,28 +4104,29 @@ png_build_gamma_table(png_structp png_ptr, int bit_depth) if (bit_depth <= 8) { png_build_8bit_table(png_ptr, &png_ptr->gamma_table, - png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, + png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, png_ptr->screen_gamma) : PNG_FP_1); #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) - if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) + if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0) { png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1, - png_reciprocal(png_ptr->gamma)); + png_reciprocal(png_ptr->colorspace.gamma)); png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : - png_ptr->gamma/* Probably doing rgb_to_gray */); + png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); } #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ } +#ifdef PNG_16BIT_SUPPORTED else { png_byte shift, sig_bit; - if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) { sig_bit = png_ptr->sig_bit.red; @@ -2812,12 +4158,13 @@ png_build_gamma_table(png_structp png_ptr, int bit_depth) * */ if (sig_bit > 0 && sig_bit < 16U) - shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */ + /* shift == insignificant bits */ + shift = (png_byte)((16U - sig_bit) & 0xff); else shift = 0; /* keep all 16 bits */ - if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) + if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0) { /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively * the significant bits in the *input* when the output will @@ -2832,32 +4179,28 @@ png_build_gamma_table(png_structp png_ptr, int bit_depth) png_ptr->gamma_shift = shift; -#ifdef PNG_16BIT_SUPPORTED /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now * PNG_COMPOSE). This effectively smashed the background calculation for * 16-bit output because the 8-bit table assumes the result will be reduced * to 8 bits. */ - if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) -#endif + if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0) png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, - png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma, + png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma, png_ptr->screen_gamma) : PNG_FP_1); -#ifdef PNG_16BIT_SUPPORTED else png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, - png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, + png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, png_ptr->screen_gamma) : PNG_FP_1); -#endif #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) - if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) + if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0) { png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift, - png_reciprocal(png_ptr->gamma)); + png_reciprocal(png_ptr->colorspace.gamma)); /* Notice that the '16 from 1' table should be full precision, however * the lookup on this table still uses gamma_shift, so it can't be. @@ -2865,10 +4208,291 @@ png_build_gamma_table(png_structp png_ptr, int bit_depth) */ png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift, png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : - png_ptr->gamma/* Probably doing rgb_to_gray */); + png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); } #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ } +#endif /* 16BIT */ } #endif /* READ_GAMMA */ -#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ + +/* HARDWARE OR SOFTWARE OPTION SUPPORT */ +#ifdef PNG_SET_OPTION_SUPPORTED +int PNGAPI +png_set_option(png_structrp png_ptr, int option, int onoff) +{ + if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT && + (option & 1) == 0) + { + int mask = 3 << option; + int setting = (2 + (onoff != 0)) << option; + int current = png_ptr->options; + + png_ptr->options = (png_byte)(((current & ~mask) | setting) & 0xff); + + return (current & mask) >> option; + } + + return PNG_OPTION_INVALID; +} +#endif + +/* sRGB support */ +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) +/* sRGB conversion tables; these are machine generated with the code in + * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the + * specification (see the article at http://en.wikipedia.org/wiki/SRGB) + * is used, not the gamma=1/2.2 approximation use elsewhere in libpng. + * The sRGB to linear table is exact (to the nearest 16-bit linear fraction). + * The inverse (linear to sRGB) table has accuracies as follows: + * + * For all possible (255*65535+1) input values: + * + * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact + * + * For the input values corresponding to the 65536 16-bit values: + * + * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact + * + * In all cases the inexact readings are only off by one. + */ + +#ifdef PNG_SIMPLIFIED_READ_SUPPORTED +/* The convert-to-sRGB table is only currently required for read. */ +const png_uint_16 png_sRGB_table[256] = +{ + 0,20,40,60,80,99,119,139, + 159,179,199,219,241,264,288,313, + 340,367,396,427,458,491,526,562, + 599,637,677,718,761,805,851,898, + 947,997,1048,1101,1156,1212,1270,1330, + 1391,1453,1517,1583,1651,1720,1790,1863, + 1937,2013,2090,2170,2250,2333,2418,2504, + 2592,2681,2773,2866,2961,3058,3157,3258, + 3360,3464,3570,3678,3788,3900,4014,4129, + 4247,4366,4488,4611,4736,4864,4993,5124, + 5257,5392,5530,5669,5810,5953,6099,6246, + 6395,6547,6700,6856,7014,7174,7335,7500, + 7666,7834,8004,8177,8352,8528,8708,8889, + 9072,9258,9445,9635,9828,10022,10219,10417, + 10619,10822,11028,11235,11446,11658,11873,12090, + 12309,12530,12754,12980,13209,13440,13673,13909, + 14146,14387,14629,14874,15122,15371,15623,15878, + 16135,16394,16656,16920,17187,17456,17727,18001, + 18277,18556,18837,19121,19407,19696,19987,20281, + 20577,20876,21177,21481,21787,22096,22407,22721, + 23038,23357,23678,24002,24329,24658,24990,25325, + 25662,26001,26344,26688,27036,27386,27739,28094, + 28452,28813,29176,29542,29911,30282,30656,31033, + 31412,31794,32179,32567,32957,33350,33745,34143, + 34544,34948,35355,35764,36176,36591,37008,37429, + 37852,38278,38706,39138,39572,40009,40449,40891, + 41337,41785,42236,42690,43147,43606,44069,44534, + 45002,45473,45947,46423,46903,47385,47871,48359, + 48850,49344,49841,50341,50844,51349,51858,52369, + 52884,53401,53921,54445,54971,55500,56032,56567, + 57105,57646,58190,58737,59287,59840,60396,60955, + 61517,62082,62650,63221,63795,64372,64952,65535 +}; +#endif /* SIMPLIFIED_READ */ + +/* The base/delta tables are required for both read and write (but currently + * only the simplified versions.) + */ +const png_uint_16 png_sRGB_base[512] = +{ + 128,1782,3383,4644,5675,6564,7357,8074, + 8732,9346,9921,10463,10977,11466,11935,12384, + 12816,13233,13634,14024,14402,14769,15125,15473, + 15812,16142,16466,16781,17090,17393,17690,17981, + 18266,18546,18822,19093,19359,19621,19879,20133, + 20383,20630,20873,21113,21349,21583,21813,22041, + 22265,22487,22707,22923,23138,23350,23559,23767, + 23972,24175,24376,24575,24772,24967,25160,25352, + 25542,25730,25916,26101,26284,26465,26645,26823, + 27000,27176,27350,27523,27695,27865,28034,28201, + 28368,28533,28697,28860,29021,29182,29341,29500, + 29657,29813,29969,30123,30276,30429,30580,30730, + 30880,31028,31176,31323,31469,31614,31758,31902, + 32045,32186,32327,32468,32607,32746,32884,33021, + 33158,33294,33429,33564,33697,33831,33963,34095, + 34226,34357,34486,34616,34744,34873,35000,35127, + 35253,35379,35504,35629,35753,35876,35999,36122, + 36244,36365,36486,36606,36726,36845,36964,37083, + 37201,37318,37435,37551,37668,37783,37898,38013, + 38127,38241,38354,38467,38580,38692,38803,38915, + 39026,39136,39246,39356,39465,39574,39682,39790, + 39898,40005,40112,40219,40325,40431,40537,40642, + 40747,40851,40955,41059,41163,41266,41369,41471, + 41573,41675,41777,41878,41979,42079,42179,42279, + 42379,42478,42577,42676,42775,42873,42971,43068, + 43165,43262,43359,43456,43552,43648,43743,43839, + 43934,44028,44123,44217,44311,44405,44499,44592, + 44685,44778,44870,44962,45054,45146,45238,45329, + 45420,45511,45601,45692,45782,45872,45961,46051, + 46140,46229,46318,46406,46494,46583,46670,46758, + 46846,46933,47020,47107,47193,47280,47366,47452, + 47538,47623,47709,47794,47879,47964,48048,48133, + 48217,48301,48385,48468,48552,48635,48718,48801, + 48884,48966,49048,49131,49213,49294,49376,49458, + 49539,49620,49701,49782,49862,49943,50023,50103, + 50183,50263,50342,50422,50501,50580,50659,50738, + 50816,50895,50973,51051,51129,51207,51285,51362, + 51439,51517,51594,51671,51747,51824,51900,51977, + 52053,52129,52205,52280,52356,52432,52507,52582, + 52657,52732,52807,52881,52956,53030,53104,53178, + 53252,53326,53400,53473,53546,53620,53693,53766, + 53839,53911,53984,54056,54129,54201,54273,54345, + 54417,54489,54560,54632,54703,54774,54845,54916, + 54987,55058,55129,55199,55269,55340,55410,55480, + 55550,55620,55689,55759,55828,55898,55967,56036, + 56105,56174,56243,56311,56380,56448,56517,56585, + 56653,56721,56789,56857,56924,56992,57059,57127, + 57194,57261,57328,57395,57462,57529,57595,57662, + 57728,57795,57861,57927,57993,58059,58125,58191, + 58256,58322,58387,58453,58518,58583,58648,58713, + 58778,58843,58908,58972,59037,59101,59165,59230, + 59294,59358,59422,59486,59549,59613,59677,59740, + 59804,59867,59930,59993,60056,60119,60182,60245, + 60308,60370,60433,60495,60558,60620,60682,60744, + 60806,60868,60930,60992,61054,61115,61177,61238, + 61300,61361,61422,61483,61544,61605,61666,61727, + 61788,61848,61909,61969,62030,62090,62150,62211, + 62271,62331,62391,62450,62510,62570,62630,62689, + 62749,62808,62867,62927,62986,63045,63104,63163, + 63222,63281,63340,63398,63457,63515,63574,63632, + 63691,63749,63807,63865,63923,63981,64039,64097, + 64155,64212,64270,64328,64385,64443,64500,64557, + 64614,64672,64729,64786,64843,64900,64956,65013, + 65070,65126,65183,65239,65296,65352,65409,65465 +}; + +const png_byte png_sRGB_delta[512] = +{ + 207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54, + 52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36, + 35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28, + 28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24, + 23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21, + 21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19, + 19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17, + 17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16, + 16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15, + 15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14, + 14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13, + 13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12, + 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, + 12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11, + 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, + 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, + 11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, + 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, + 10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 +}; +#endif /* SIMPLIFIED READ/WRITE sRGB support */ + +/* SIMPLIFIED READ/WRITE SUPPORT */ +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) +static int +png_image_free_function(png_voidp argument) +{ + png_imagep image = png_voidcast(png_imagep, argument); + png_controlp cp = image->opaque; + png_control c; + + /* Double check that we have a png_ptr - it should be impossible to get here + * without one. + */ + if (cp->png_ptr == NULL) + return 0; + + /* First free any data held in the control structure. */ +# ifdef PNG_STDIO_SUPPORTED + if (cp->owned_file != 0) + { + FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr); + cp->owned_file = 0; + + /* Ignore errors here. */ + if (fp != NULL) + { + cp->png_ptr->io_ptr = NULL; + (void)fclose(fp); + } + } +# endif + + /* Copy the control structure so that the original, allocated, version can be + * safely freed. Notice that a png_error here stops the remainder of the + * cleanup, but this is probably fine because that would indicate bad memory + * problems anyway. + */ + c = *cp; + image->opaque = &c; + png_free(c.png_ptr, cp); + + /* Then the structures, calling the correct API. */ + if (c.for_write != 0) + { +# ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED + png_destroy_write_struct(&c.png_ptr, &c.info_ptr); +# else + png_error(c.png_ptr, "simplified write not supported"); +# endif + } + else + { +# ifdef PNG_SIMPLIFIED_READ_SUPPORTED + png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL); +# else + png_error(c.png_ptr, "simplified read not supported"); +# endif + } + + /* Success. */ + return 1; +} + +void PNGAPI +png_image_free(png_imagep image) +{ + /* Safely call the real function, but only if doing so is safe at this point + * (if not inside an error handling context). Otherwise assume + * png_safe_execute will call this API after the return. + */ + if (image != NULL && image->opaque != NULL && + image->opaque->error_buf == NULL) + { + /* Ignore errors here: */ + (void)png_safe_execute(image, png_image_free_function, image); + image->opaque = NULL; + } +} + +int /* PRIVATE */ +png_image_error(png_imagep image, png_const_charp error_message) +{ + /* Utility to log an error. */ + png_safecat(image->message, (sizeof image->message), 0, error_message); + image->warning_or_error |= PNG_IMAGE_ERROR; + png_image_free(image); + return 0; +} + +#endif /* SIMPLIFIED READ/WRITE */ +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/png.h b/3rdparty/libpng/png.h index 4c37e58a4..c83051b1c 100644 --- a/3rdparty/libpng/png.h +++ b/3rdparty/libpng/png.h @@ -1,8 +1,9 @@ /* png.h - header file for PNG reference library * - * libpng version 1.5.12 - July 11, 2012 - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * libpng version 1.6.19, November 12, 2015 + * + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -10,17 +11,136 @@ * * Authors and maintainers: * libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat - * libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger - * libpng versions 0.97, January 1998, through 1.5.12 - July 11, 2012: Glenn + * libpng versions 0.89, June 1996, through 0.96, May 1997: Andreas Dilger + * libpng versions 0.97, January 1998, through 1.6.19, November 12, 2015: Glenn * See also "Contributing Authors", below. + */ + +/* + * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE: * - * Note about libpng version numbers: + * If you modify libpng you may insert additional notices immediately following + * this sentence. * - * Due to various miscommunications, unforeseen code incompatibilities - * and occasional factors outside the authors' control, version numbering - * on the library has not always been consistent and straightforward. - * The following table summarizes matters since version 0.89c, which was - * the first widely used release: + * This code is released under the libpng license. + * + * libpng versions 1.0.7, July 1, 2000, through 1.6.19, November 12, 2015, are + * Copyright (c) 2000-2002, 2004, 2006-2015 Glenn Randers-Pehrson, are + * derived from libpng-1.0.6, and are distributed according to the same + * disclaimer and license as libpng-1.0.6 with the following individuals + * added to the list of Contributing Authors: + * + * Simon-Pierre Cadieux + * Eric S. Raymond + * Mans Rullgard + * Cosmin Truta + * Gilles Vollant + * James Yu + * + * and with the following additions to the disclaimer: + * + * There is no warranty against interference with your enjoyment of the + * library or against infringement. There is no warranty that our + * efforts or the library will fulfill any of your particular purposes + * or needs. This library is provided with all faults, and the entire + * risk of satisfactory quality, performance, accuracy, and effort is with + * the user. + * + * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are + * Copyright (c) 1998-2000 Glenn Randers-Pehrson, are derived from + * libpng-0.96, and are distributed according to the same disclaimer and + * license as libpng-0.96, with the following individuals added to the list + * of Contributing Authors: + * + * Tom Lane + * Glenn Randers-Pehrson + * Willem van Schaik + * + * libpng versions 0.89, June 1996, through 0.96, May 1997, are + * Copyright (c) 1996-1997 Andreas Dilger, are derived from libpng-0.88, + * and are distributed according to the same disclaimer and license as + * libpng-0.88, with the following individuals added to the list of + * Contributing Authors: + * + * John Bowler + * Kevin Bracey + * Sam Bushell + * Magnus Holmgren + * Greg Roelofs + * Tom Tanner + * + * libpng versions 0.5, May 1995, through 0.88, January 1996, are + * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. + * + * For the purposes of this copyright and license, "Contributing Authors" + * is defined as the following set of individuals: + * + * Andreas Dilger + * Dave Martindale + * Guy Eric Schalnat + * Paul Schmidt + * Tim Wegner + * + * The PNG Reference Library is supplied "AS IS". The Contributing Authors + * and Group 42, Inc. disclaim all warranties, expressed or implied, + * including, without limitation, the warranties of merchantability and of + * fitness for any purpose. The Contributing Authors and Group 42, Inc. + * assume no liability for direct, indirect, incidental, special, exemplary, + * or consequential damages, which may result from the use of the PNG + * Reference Library, even if advised of the possibility of such damage. + * + * Permission is hereby granted to use, copy, modify, and distribute this + * source code, or portions hereof, for any purpose, without fee, subject + * to the following restrictions: + * + * 1. The origin of this source code must not be misrepresented. + * + * 2. Altered versions must be plainly marked as such and must not + * be misrepresented as being the original source. + * + * 3. This Copyright notice may not be removed or altered from any + * source or altered source distribution. + * + * The Contributing Authors and Group 42, Inc. specifically permit, without + * fee, and encourage the use of this source code as a component to + * supporting the PNG file format in commercial products. If you use this + * source code in a product, acknowledgment is not required but would be + * appreciated. + * + * END OF COPYRIGHT NOTICE, DISCLAIMER, and LICENSE. + */ + +/* + * A "png_get_copyright" function is available, for convenient use in "about" + * boxes and the like: + * + * printf("%s", png_get_copyright(NULL)); + * + * Also, the PNG logo (in PNG format, of course) is supplied in the + * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31). + */ + +/* + * Libpng is OSI Certified Open Source Software. OSI Certified Open Source is + * a certification mark of the Open Source Initiative. OSI has not addressed + * the additional disclaimers inserted at version 1.0.7. + */ + +/* + * The contributing authors would like to thank all those who helped + * with testing, bug fixes, and patience. This wouldn't have been + * possible without all of you. + * + * Thanks to Frank J. T. Wojcik for helping with the documentation. + */ + +/* Note about libpng version numbers: + * + * Due to various miscommunications, unforeseen code incompatibilities + * and occasional factors outside the authors' control, version numbering + * on the library has not always been consistent and straightforward. + * The following table summarizes matters since version 0.89c, which was + * the first widely used release: * * source png.h png.h shared-lib * version string int version @@ -58,292 +178,65 @@ * 1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible) * 1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible) * 1.0.7 1 10007 (still compatible) - * 1.0.8beta1-4 1 10008 2.1.0.8beta1-4 - * 1.0.8rc1 1 10008 2.1.0.8rc1 - * 1.0.8 1 10008 2.1.0.8 - * 1.0.9beta1-6 1 10009 2.1.0.9beta1-6 - * 1.0.9rc1 1 10009 2.1.0.9rc1 - * 1.0.9beta7-10 1 10009 2.1.0.9beta7-10 - * 1.0.9rc2 1 10009 2.1.0.9rc2 - * 1.0.9 1 10009 2.1.0.9 - * 1.0.10beta1 1 10010 2.1.0.10beta1 - * 1.0.10rc1 1 10010 2.1.0.10rc1 - * 1.0.10 1 10010 2.1.0.10 - * 1.0.11beta1-3 1 10011 2.1.0.11beta1-3 - * 1.0.11rc1 1 10011 2.1.0.11rc1 - * 1.0.11 1 10011 2.1.0.11 - * 1.0.12beta1-2 2 10012 2.1.0.12beta1-2 - * 1.0.12rc1 2 10012 2.1.0.12rc1 - * 1.0.12 2 10012 2.1.0.12 - * 1.1.0a-f - 10100 2.1.1.0a-f (branch abandoned) - * 1.2.0beta1-2 2 10200 2.1.2.0beta1-2 - * 1.2.0beta3-5 3 10200 3.1.2.0beta3-5 - * 1.2.0rc1 3 10200 3.1.2.0rc1 - * 1.2.0 3 10200 3.1.2.0 - * 1.2.1beta1-4 3 10201 3.1.2.1beta1-4 - * 1.2.1rc1-2 3 10201 3.1.2.1rc1-2 - * 1.2.1 3 10201 3.1.2.1 - * 1.2.2beta1-6 12 10202 12.so.0.1.2.2beta1-6 - * 1.0.13beta1 10 10013 10.so.0.1.0.13beta1 - * 1.0.13rc1 10 10013 10.so.0.1.0.13rc1 - * 1.2.2rc1 12 10202 12.so.0.1.2.2rc1 - * 1.0.13 10 10013 10.so.0.1.0.13 - * 1.2.2 12 10202 12.so.0.1.2.2 - * 1.2.3rc1-6 12 10203 12.so.0.1.2.3rc1-6 - * 1.2.3 12 10203 12.so.0.1.2.3 - * 1.2.4beta1-3 13 10204 12.so.0.1.2.4beta1-3 - * 1.0.14rc1 13 10014 10.so.0.1.0.14rc1 - * 1.2.4rc1 13 10204 12.so.0.1.2.4rc1 - * 1.0.14 10 10014 10.so.0.1.0.14 - * 1.2.4 13 10204 12.so.0.1.2.4 - * 1.2.5beta1-2 13 10205 12.so.0.1.2.5beta1-2 - * 1.0.15rc1-3 10 10015 10.so.0.1.0.15rc1-3 - * 1.2.5rc1-3 13 10205 12.so.0.1.2.5rc1-3 - * 1.0.15 10 10015 10.so.0.1.0.15 - * 1.2.5 13 10205 12.so.0.1.2.5 - * 1.2.6beta1-4 13 10206 12.so.0.1.2.6beta1-4 - * 1.0.16 10 10016 10.so.0.1.0.16 - * 1.2.6 13 10206 12.so.0.1.2.6 - * 1.2.7beta1-2 13 10207 12.so.0.1.2.7beta1-2 - * 1.0.17rc1 10 10017 12.so.0.1.0.17rc1 - * 1.2.7rc1 13 10207 12.so.0.1.2.7rc1 - * 1.0.17 10 10017 12.so.0.1.0.17 - * 1.2.7 13 10207 12.so.0.1.2.7 - * 1.2.8beta1-5 13 10208 12.so.0.1.2.8beta1-5 - * 1.0.18rc1-5 10 10018 12.so.0.1.0.18rc1-5 - * 1.2.8rc1-5 13 10208 12.so.0.1.2.8rc1-5 - * 1.0.18 10 10018 12.so.0.1.0.18 - * 1.2.8 13 10208 12.so.0.1.2.8 - * 1.2.9beta1-3 13 10209 12.so.0.1.2.9beta1-3 - * 1.2.9beta4-11 13 10209 12.so.0.9[.0] - * 1.2.9rc1 13 10209 12.so.0.9[.0] - * 1.2.9 13 10209 12.so.0.9[.0] - * 1.2.10beta1-7 13 10210 12.so.0.10[.0] - * 1.2.10rc1-2 13 10210 12.so.0.10[.0] - * 1.2.10 13 10210 12.so.0.10[.0] - * 1.4.0beta1-5 14 10400 14.so.0.0[.0] - * 1.2.11beta1-4 13 10211 12.so.0.11[.0] - * 1.4.0beta7-8 14 10400 14.so.0.0[.0] - * 1.2.11 13 10211 12.so.0.11[.0] - * 1.2.12 13 10212 12.so.0.12[.0] - * 1.4.0beta9-14 14 10400 14.so.0.0[.0] - * 1.2.13 13 10213 12.so.0.13[.0] - * 1.4.0beta15-36 14 10400 14.so.0.0[.0] - * 1.4.0beta37-87 14 10400 14.so.14.0[.0] - * 1.4.0rc01 14 10400 14.so.14.0[.0] - * 1.4.0beta88-109 14 10400 14.so.14.0[.0] - * 1.4.0rc02-08 14 10400 14.so.14.0[.0] - * 1.4.0 14 10400 14.so.14.0[.0] - * 1.4.1beta01-03 14 10401 14.so.14.1[.0] - * 1.4.1rc01 14 10401 14.so.14.1[.0] - * 1.4.1beta04-12 14 10401 14.so.14.1[.0] - * 1.4.1 14 10401 14.so.14.1[.0] - * 1.4.2 14 10402 14.so.14.2[.0] - * 1.4.3 14 10403 14.so.14.3[.0] - * 1.4.4 14 10404 14.so.14.4[.0] - * 1.5.0beta01-58 15 10500 15.so.15.0[.0] - * 1.5.0rc01-07 15 10500 15.so.15.0[.0] - * 1.5.0 15 10500 15.so.15.0[.0] - * 1.5.1beta01-11 15 10501 15.so.15.1[.0] - * 1.5.1rc01-02 15 10501 15.so.15.1[.0] - * 1.5.1 15 10501 15.so.15.1[.0] - * 1.5.2beta01-03 15 10502 15.so.15.2[.0] - * 1.5.2rc01-03 15 10502 15.so.15.2[.0] - * 1.5.2 15 10502 15.so.15.2[.0] - * 1.5.3beta01-10 15 10503 15.so.15.3[.0] - * 1.5.3rc01-02 15 10503 15.so.15.3[.0] - * 1.5.3beta11 15 10503 15.so.15.3[.0] - * 1.5.3 [omitted] - * 1.5.4beta01-08 15 10504 15.so.15.4[.0] - * 1.5.4rc01 15 10504 15.so.15.4[.0] - * 1.5.4 15 10504 15.so.15.4[.0] - * 1.5.5beta01-08 15 10505 15.so.15.5[.0] - * 1.5.5rc01 15 10505 15.so.15.5[.0] - * 1.5.5 15 10505 15.so.15.5[.0] - * 1.5.6beta01-07 15 10506 15.so.15.6[.0] - * 1.5.6rc01-03 15 10506 15.so.15.6[.0] - * 1.5.6 15 10506 15.so.15.6[.0] - * 1.5.7beta01-05 15 10507 15.so.15.7[.0] - * 1.5.7rc01-03 15 10507 15.so.15.7[.0] - * 1.5.7 15 10507 15.so.15.7[.0] - * 1.5.8beta01 15 10508 15.so.15.8[.0] - * 1.5.8rc01 15 10508 15.so.15.8[.0] - * 1.5.8 15 10508 15.so.15.8[.0] - * 1.5.9beta01-02 15 10509 15.so.15.9[.0] - * 1.5.9rc01 15 10509 15.so.15.9[.0] - * 1.5.9 15 10509 15.so.15.9[.0] - * 1.5.10beta01-05 15 10510 15.so.15.10[.0] - * 1.5.10 15 10510 15.so.15.10[.0] - * 1.5.11beta01 15 10511 15.so.15.11[.0] - * 1.5.11rc01-05 15 10511 15.so.15.11[.0] - * 1.5.11 15 10511 15.so.15.11[.0] - * 1.5.12 15 10512 15.so.15.12[.0] + * ... + * 1.0.19 10 10019 10.so.0.19[.0] + * ... + * 1.2.53 13 10253 12.so.0.53[.0] + * ... + * 1.5.23 15 10523 15.so.15.23[.0] + * ... + * 1.6.19 16 10619 16.so.16.19[.0] * - * Henceforth the source version will match the shared-library major - * and minor numbers; the shared-library major version number will be - * used for changes in backward compatibility, as it is intended. The - * PNG_LIBPNG_VER macro, which is not used within libpng but is available - * for applications, is an unsigned integer of the form xyyzz corresponding - * to the source version x.y.z (leading zeros in y and z). Beta versions - * were given the previous public release number plus a letter, until - * version 1.0.6j; from then on they were given the upcoming public - * release number plus "betaNN" or "rcNN". + * Henceforth the source version will match the shared-library major + * and minor numbers; the shared-library major version number will be + * used for changes in backward compatibility, as it is intended. The + * PNG_LIBPNG_VER macro, which is not used within libpng but is available + * for applications, is an unsigned integer of the form xyyzz corresponding + * to the source version x.y.z (leading zeros in y and z). Beta versions + * were given the previous public release number plus a letter, until + * version 1.0.6j; from then on they were given the upcoming public + * release number plus "betaNN" or "rcNN". * - * Binary incompatibility exists only when applications make direct access - * to the info_ptr or png_ptr members through png.h, and the compiled - * application is loaded with a different version of the library. + * Binary incompatibility exists only when applications make direct access + * to the info_ptr or png_ptr members through png.h, and the compiled + * application is loaded with a different version of the library. * - * DLLNUM will change each time there are forward or backward changes - * in binary compatibility (e.g., when a new feature is added). + * DLLNUM will change each time there are forward or backward changes + * in binary compatibility (e.g., when a new feature is added). * - * See libpng-manual.txt or libpng.3 for more information. The PNG - * specification is available as a W3C Recommendation and as an ISO - * Specification, * * If you just need to read a PNG file and don't want to read the documentation * skip to the end of this file and read the section entitled 'simplified API'. */ /* Version information for png.h - this should match the version in png.c */ -#define PNG_LIBPNG_VER_STRING "1.5.12" +#define PNG_LIBPNG_VER_STRING "1.6.19" #define PNG_HEADER_VERSION_STRING \ - " libpng version 1.5.12 - July 11, 2012\n" + " libpng version 1.6.19 - November 12, 2015\n" -#define PNG_LIBPNG_VER_SONUM 15 -#define PNG_LIBPNG_VER_DLLNUM 15 +#define PNG_LIBPNG_VER_SONUM 16 +#define PNG_LIBPNG_VER_DLLNUM 16 /* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */ #define PNG_LIBPNG_VER_MAJOR 1 -#define PNG_LIBPNG_VER_MINOR 5 -#define PNG_LIBPNG_VER_RELEASE 12 +#define PNG_LIBPNG_VER_MINOR 6 +#define PNG_LIBPNG_VER_RELEASE 19 /* This should match the numeric part of the final component of * PNG_LIBPNG_VER_STRING, omitting any leading zero: @@ -427,7 +322,7 @@ * version 1.0.0 was mis-numbered 100 instead of 10000). From * version 1.0.1 it's xxyyzz, where x=major, y=minor, z=release */ -#define PNG_LIBPNG_VER 10512 /* 1.5.12 */ +#define PNG_LIBPNG_VER 10619 /* 1.6.19 */ /* Library configuration: these options cannot be changed after * the library has been built. @@ -440,25 +335,7 @@ #endif #ifndef PNG_VERSION_INFO_ONLY -# ifndef PNG_BUILDING_SYMBOL_TABLE - /* - * Standard header files (not needed for the version info or while - * building symbol table -- see scripts/pnglibconf.dfa) - */ -# ifdef PNG_SETJMP_SUPPORTED -# include -# endif - - /* Need the time information for converting tIME chunks, it - * defines struct tm: - */ -# ifdef PNG_CONVERT_tIME_SUPPORTED - /* "time.h" functions are not supported on all operating systems */ -# include -# endif -# endif - -/* Machine specific configuration. */ + /* Machine specific configuration. */ # include "pngconf.h" #endif @@ -502,16 +379,22 @@ extern "C" { /* This file is arranged in several sections: * - * 1. Any configuration options that can be specified by for the application + * 1. [omitted] + * 2. Any configuration options that can be specified by for the application * code when it is built. (Build time configuration is in pnglibconf.h) - * 2. Type definitions (base types are defined in pngconf.h), structure + * 3. Type definitions (base types are defined in pngconf.h), structure * definitions. - * 3. Exported library functions. + * 4. Exported library functions. + * 5. Simplified API. + * 6. Implementation options. * * The library source code has additional files (principally pngpriv.h) that * allow configuration of the library. */ -/* Section 1: run time configuration + +/* Section 1: [omitted] */ + +/* Section 2: run time configuration * See pnglibconf.h for build time configuration * * Run time configuration allows the application to choose between @@ -541,7 +424,7 @@ extern "C" { * Otherwise the calls are mapped to png_error. */ -/* Section 2: type definitions, including structures and compile time +/* Section 3: type definitions, including structures and compile time * constants. * See pngconf.h for base types that vary by machine/system */ @@ -549,7 +432,48 @@ extern "C" { /* This triggers a compiler error in png.c, if png.c and png.h * do not agree upon the version number. */ -typedef char* png_libpng_version_1_5_12; +typedef char* png_libpng_version_1_6_19; + +/* Basic control structions. Read libpng-manual.txt or libpng.3 for more info. + * + * png_struct is the cache of information used while reading or writing a single + * PNG file. One of these is always required, although the simplified API + * (below) hides the creation and destruction of it. + */ +typedef struct png_struct_def png_struct; +typedef const png_struct * png_const_structp; +typedef png_struct * png_structp; +typedef png_struct * * png_structpp; + +/* png_info contains information read from or to be written to a PNG file. One + * or more of these must exist while reading or creating a PNG file. The + * information is not used by libpng during read but is used to control what + * gets written when a PNG file is created. "png_get_" function calls read + * information during read and "png_set_" functions calls write information + * when creating a PNG. + * been moved into a separate header file that is not accessible to + * applications. Read libpng-manual.txt or libpng.3 for more info. + */ +typedef struct png_info_def png_info; +typedef png_info * png_infop; +typedef const png_info * png_const_infop; +typedef png_info * * png_infopp; + +/* Types with names ending 'p' are pointer types. The corresponding types with + * names ending 'rp' are identical pointer types except that the pointer is + * marked 'restrict', which means that it is the only pointer to the object + * passed to the function. Applications should not use the 'restrict' types; + * it is always valid to pass 'p' to a pointer with a function argument of the + * corresponding 'rp' type. Different compilers have different rules with + * regard to type matching in the presence of 'restrict'. For backward + * compatibility libpng callbacks never have 'restrict' in their parameters and, + * consequentially, writing portable application code is extremely difficult if + * an attempt is made to use 'restrict'. + */ +typedef png_struct * PNG_RESTRICT png_structrp; +typedef const png_struct * PNG_RESTRICT png_const_structrp; +typedef png_info * PNG_RESTRICT png_inforp; +typedef const png_info * PNG_RESTRICT png_const_inforp; /* Three color definitions. The order of the red, green, and blue, (and the * exact size) is not important, although the size of the fields need to @@ -561,9 +485,9 @@ typedef struct png_color_struct png_byte green; png_byte blue; } png_color; -typedef png_color FAR * png_colorp; -typedef PNG_CONST png_color FAR * png_const_colorp; -typedef png_color FAR * FAR * png_colorpp; +typedef png_color * png_colorp; +typedef const png_color * png_const_colorp; +typedef png_color * * png_colorpp; typedef struct png_color_16_struct { @@ -573,9 +497,9 @@ typedef struct png_color_16_struct png_uint_16 blue; png_uint_16 gray; /* for use in grayscale files */ } png_color_16; -typedef png_color_16 FAR * png_color_16p; -typedef PNG_CONST png_color_16 FAR * png_const_color_16p; -typedef png_color_16 FAR * FAR * png_color_16pp; +typedef png_color_16 * png_color_16p; +typedef const png_color_16 * png_const_color_16p; +typedef png_color_16 * * png_color_16pp; typedef struct png_color_8_struct { @@ -585,9 +509,9 @@ typedef struct png_color_8_struct png_byte gray; /* for use in grayscale files */ png_byte alpha; /* for alpha channel files */ } png_color_8; -typedef png_color_8 FAR * png_color_8p; -typedef PNG_CONST png_color_8 FAR * png_const_color_8p; -typedef png_color_8 FAR * FAR * png_color_8pp; +typedef png_color_8 * png_color_8p; +typedef const png_color_8 * png_const_color_8p; +typedef png_color_8 * * png_color_8pp; /* * The following two structures are used for the in-core representation @@ -601,9 +525,9 @@ typedef struct png_sPLT_entry_struct png_uint_16 alpha; png_uint_16 frequency; } png_sPLT_entry; -typedef png_sPLT_entry FAR * png_sPLT_entryp; -typedef PNG_CONST png_sPLT_entry FAR * png_const_sPLT_entryp; -typedef png_sPLT_entry FAR * FAR * png_sPLT_entrypp; +typedef png_sPLT_entry * png_sPLT_entryp; +typedef const png_sPLT_entry * png_const_sPLT_entryp; +typedef png_sPLT_entry * * png_sPLT_entrypp; /* When the depth of the sPLT palette is 8 bits, the color and alpha samples * occupy the LSB of their respective members, and the MSB of each member @@ -617,9 +541,9 @@ typedef struct png_sPLT_struct png_sPLT_entryp entries; /* palette entries */ png_int_32 nentries; /* number of palette entries */ } png_sPLT_t; -typedef png_sPLT_t FAR * png_sPLT_tp; -typedef PNG_CONST png_sPLT_t FAR * png_const_sPLT_tp; -typedef png_sPLT_t FAR * FAR * png_sPLT_tpp; +typedef png_sPLT_t * png_sPLT_tp; +typedef const png_sPLT_t * png_const_sPLT_tp; +typedef png_sPLT_t * * png_sPLT_tpp; #ifdef PNG_TEXT_SUPPORTED /* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file, @@ -656,9 +580,9 @@ typedef struct png_text_struct png_charp lang_key; /* keyword translated UTF-8 string, 0 or more chars or a NULL pointer */ } png_text; -typedef png_text FAR * png_textp; -typedef PNG_CONST png_text FAR * png_const_textp; -typedef png_text FAR * FAR * png_textpp; +typedef png_text * png_textp; +typedef const png_text * png_const_textp; +typedef png_text * * png_textpp; #endif /* Supported compression types for text in PNG files (tEXt, and zTXt). @@ -686,49 +610,45 @@ typedef struct png_time_struct png_byte minute; /* minute of hour, 0 - 59 */ png_byte second; /* second of minute, 0 - 60 (for leap seconds) */ } png_time; -typedef png_time FAR * png_timep; -typedef PNG_CONST png_time FAR * png_const_timep; -typedef png_time FAR * FAR * png_timepp; +typedef png_time * png_timep; +typedef const png_time * png_const_timep; +typedef png_time * * png_timepp; -#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \ - defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) +#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) ||\ + defined(PNG_USER_CHUNKS_SUPPORTED) /* png_unknown_chunk is a structure to hold queued chunks for which there is * no specific support. The idea is that we can use this to queue * up private chunks for output even though the library doesn't actually * know about their semantics. + * + * The data in the structure is set by libpng on read and used on write. */ typedef struct png_unknown_chunk_t { - png_byte name[5]; - png_byte *data; + png_byte name[5]; /* Textual chunk name with '\0' terminator */ + png_byte *data; /* Data, should not be modified on read! */ png_size_t size; - /* libpng-using applications should NOT directly modify this byte. */ + /* On write 'location' must be set using the flag values listed below. + * Notice that on read it is set by libpng however the values stored have + * more bits set than are listed below. Always treat the value as a + * bitmask. On write set only one bit - setting multiple bits may cause the + * chunk to be written in multiple places. + */ png_byte location; /* mode of operation at read time */ } - - png_unknown_chunk; -typedef png_unknown_chunk FAR * png_unknown_chunkp; -typedef PNG_CONST png_unknown_chunk FAR * png_const_unknown_chunkp; -typedef png_unknown_chunk FAR * FAR * png_unknown_chunkpp; + +typedef png_unknown_chunk * png_unknown_chunkp; +typedef const png_unknown_chunk * png_const_unknown_chunkp; +typedef png_unknown_chunk * * png_unknown_chunkpp; #endif -/* Values for the unknown chunk location byte */ - +/* Flag values for the unknown chunk location byte. */ #define PNG_HAVE_IHDR 0x01 #define PNG_HAVE_PLTE 0x02 #define PNG_AFTER_IDAT 0x08 -/* The complete definition of png_info has, as of libpng-1.5.0, - * been moved into a separate header file that is not accessible to - * applications. Read libpng-manual.txt or libpng.3 for more info. - */ -typedef struct png_info_def png_info; -typedef png_info FAR * png_infop; -typedef PNG_CONST png_info FAR * png_const_infop; -typedef png_info FAR * FAR * png_infopp; - /* Maximum positive integer used in PNG is (2^31)-1 */ #define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL) #define PNG_UINT_32_MAX ((png_uint_32)(-1)) @@ -828,7 +748,9 @@ typedef png_info FAR * FAR * png_infopp; #define PNG_INFO_iCCP 0x1000 /* ESR, 1.0.6 */ #define PNG_INFO_sPLT 0x2000 /* ESR, 1.0.6 */ #define PNG_INFO_sCAL 0x4000 /* ESR, 1.0.6 */ +#if INT_MAX >= 0x8000 /* else this might break */ #define PNG_INFO_IDAT 0x8000 /* ESR, 1.0.6 */ +#endif /* This is used for the transformation routines, as some of them * change these values for the row. It also should enable using @@ -844,16 +766,8 @@ typedef struct png_row_info_struct png_byte pixel_depth; /* bits per pixel (depth * channels) */ } png_row_info; -typedef png_row_info FAR * png_row_infop; -typedef png_row_info FAR * FAR * png_row_infopp; - -/* The complete definition of png_struct has, as of libpng-1.5.0, - * been moved into a separate header file that is not accessible to - * applications. Read libpng-manual.txt or libpng.3 for more info. - */ -typedef struct png_struct_def png_struct; -typedef PNG_CONST png_struct FAR * png_const_structp; -typedef png_struct FAR * png_structp; +typedef png_row_info * png_row_infop; +typedef png_row_info * * png_row_infopp; /* These are the function types for the I/O functions and for the functions * that allow the user to override the default I/O functions with his or her @@ -900,7 +814,8 @@ typedef PNG_CALLBACK(int, *png_user_chunk_ptr, (png_structp, png_unknown_chunkp)); #endif #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED -typedef PNG_CALLBACK(void, *png_unknown_chunk_ptr, (png_structp)); +/* not used anywhere */ +/* typedef PNG_CALLBACK(void, *png_unknown_chunk_ptr, (png_structp)); */ #endif #ifdef PNG_SETJMP_SUPPORTED @@ -939,7 +854,9 @@ PNG_FUNCTION(void, (PNGCAPI *png_longjmp_ptr), PNGARG((jmp_buf, int)), typedef); #define PNG_TRANSFORM_GRAY_TO_RGB 0x2000 /* read only */ /* Added to libpng-1.5.4 */ #define PNG_TRANSFORM_EXPAND_16 0x4000 /* read only */ +#if INT_MAX >= 0x8000 /* else this might break */ #define PNG_TRANSFORM_SCALE_16 0x8000 /* read only */ +#endif /* Flags for MNG supported features */ #define PNG_FLAG_MNG_EMPTY_PLTE 0x01 @@ -956,9 +873,7 @@ typedef PNG_CALLBACK(png_voidp, *png_malloc_ptr, (png_structp, png_alloc_size_t)); typedef PNG_CALLBACK(void, *png_free_ptr, (png_structp, png_voidp)); -typedef png_struct FAR * FAR * png_structpp; - -/* Section 3: exported functions +/* Section 4: exported functions * Here are the function definitions most commonly used. This is not * the place to find out how to use libpng. See libpng-manual.txt for the * full explanation, see example.c for the summary. This just provides @@ -993,7 +908,7 @@ PNG_EXPORT(1, png_uint_32, png_access_version_number, (void)); /* Tell lib we have already handled the first magic bytes. * Handling more than 8 bytes from the beginning of the file is an error. */ -PNG_EXPORT(2, void, png_set_sig_bytes, (png_structp png_ptr, int num_bytes)); +PNG_EXPORT(2, void, png_set_sig_bytes, (png_structrp png_ptr, int num_bytes)); /* Check sig[start] through sig[start + num_to_check - 1] to see if it's a * PNG file. Returns zero if the supplied bytes match the 8-byte PNG @@ -1021,9 +936,9 @@ PNG_EXPORTA(5, png_structp, png_create_write_struct, PNG_ALLOCATED); PNG_EXPORT(6, png_size_t, png_get_compression_buffer_size, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); -PNG_EXPORT(7, void, png_set_compression_buffer_size, (png_structp png_ptr, +PNG_EXPORT(7, void, png_set_compression_buffer_size, (png_structrp png_ptr, png_size_t size)); /* Moved from pngconf.h in 1.4.0 and modified to ensure setjmp/longjmp @@ -1037,10 +952,10 @@ PNG_EXPORT(7, void, png_set_compression_buffer_size, (png_structp png_ptr, * allocated by the library - the call will return NULL on a mismatch * indicating an ABI mismatch. */ -PNG_EXPORT(8, jmp_buf*, png_set_longjmp_fn, (png_structp png_ptr, +PNG_EXPORT(8, jmp_buf*, png_set_longjmp_fn, (png_structrp png_ptr, png_longjmp_ptr longjmp_fn, size_t jmp_buf_size)); # define png_jmpbuf(png_ptr) \ - (*png_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf))) + (*png_set_longjmp_fn((png_ptr), longjmp, (sizeof (jmp_buf)))) #else # define png_jmpbuf(png_ptr) \ (LIBPNG_WAS_COMPILED_WITH__PNG_NO_SETJMP) @@ -1050,12 +965,12 @@ PNG_EXPORT(8, jmp_buf*, png_set_longjmp_fn, (png_structp png_ptr, * will use it; otherwise it will call PNG_ABORT(). This function was * added in libpng-1.5.0. */ -PNG_EXPORTA(9, void, png_longjmp, (png_structp png_ptr, int val), +PNG_EXPORTA(9, void, png_longjmp, (png_const_structrp png_ptr, int val), PNG_NORETURN); #ifdef PNG_READ_SUPPORTED /* Reset the compression stream */ -PNG_EXPORT(10, int, png_reset_zstream, (png_structp png_ptr)); +PNG_EXPORTA(10, int, png_reset_zstream, (png_structrp png_ptr), PNG_DEPRECATED); #endif /* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */ @@ -1073,81 +988,92 @@ PNG_EXPORTA(12, png_structp, png_create_write_struct_2, #endif /* Write the PNG file signature. */ -PNG_EXPORT(13, void, png_write_sig, (png_structp png_ptr)); +PNG_EXPORT(13, void, png_write_sig, (png_structrp png_ptr)); /* Write a PNG chunk - size, type, (optional) data, CRC. */ -PNG_EXPORT(14, void, png_write_chunk, (png_structp png_ptr, png_const_bytep +PNG_EXPORT(14, void, png_write_chunk, (png_structrp png_ptr, png_const_bytep chunk_name, png_const_bytep data, png_size_t length)); /* Write the start of a PNG chunk - length and chunk name. */ -PNG_EXPORT(15, void, png_write_chunk_start, (png_structp png_ptr, +PNG_EXPORT(15, void, png_write_chunk_start, (png_structrp png_ptr, png_const_bytep chunk_name, png_uint_32 length)); /* Write the data of a PNG chunk started with png_write_chunk_start(). */ -PNG_EXPORT(16, void, png_write_chunk_data, (png_structp png_ptr, +PNG_EXPORT(16, void, png_write_chunk_data, (png_structrp png_ptr, png_const_bytep data, png_size_t length)); /* Finish a chunk started with png_write_chunk_start() (includes CRC). */ -PNG_EXPORT(17, void, png_write_chunk_end, (png_structp png_ptr)); +PNG_EXPORT(17, void, png_write_chunk_end, (png_structrp png_ptr)); /* Allocate and initialize the info structure */ -PNG_EXPORTA(18, png_infop, png_create_info_struct, (png_structp png_ptr), +PNG_EXPORTA(18, png_infop, png_create_info_struct, (png_const_structrp png_ptr), PNG_ALLOCATED); -PNG_EXPORT(19, void, png_info_init_3, (png_infopp info_ptr, - png_size_t png_info_struct_size)); +/* DEPRECATED: this function allowed init structures to be created using the + * default allocation method (typically malloc). Use is deprecated in 1.6.0 and + * the API will be removed in the future. + */ +PNG_EXPORTA(19, void, png_info_init_3, (png_infopp info_ptr, + png_size_t png_info_struct_size), PNG_DEPRECATED); /* Writes all the PNG information before the image. */ PNG_EXPORT(20, void, png_write_info_before_PLTE, - (png_structp png_ptr, png_infop info_ptr)); + (png_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(21, void, png_write_info, - (png_structp png_ptr, png_infop info_ptr)); + (png_structrp png_ptr, png_const_inforp info_ptr)); #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the information before the actual image data. */ PNG_EXPORT(22, void, png_read_info, - (png_structp png_ptr, png_infop info_ptr)); + (png_structrp png_ptr, png_inforp info_ptr)); #endif #ifdef PNG_TIME_RFC1123_SUPPORTED -PNG_EXPORT(23, png_const_charp, png_convert_to_rfc1123, - (png_structp png_ptr, + /* Convert to a US string format: there is no localization support in this + * routine. The original implementation used a 29 character buffer in + * png_struct, this will be removed in future versions. + */ +#if PNG_LIBPNG_VER < 10700 +/* To do: remove this from libpng17 (and from libpng17/png.c and pngstruct.h) */ +PNG_EXPORTA(23, png_const_charp, png_convert_to_rfc1123, (png_structrp png_ptr, + png_const_timep ptime),PNG_DEPRECATED); +#endif +PNG_EXPORT(241, int, png_convert_to_rfc1123_buffer, (char out[29], png_const_timep ptime)); #endif #ifdef PNG_CONVERT_tIME_SUPPORTED /* Convert from a struct tm to png_time */ PNG_EXPORT(24, void, png_convert_from_struct_tm, (png_timep ptime, - PNG_CONST struct tm FAR * ttime)); + const struct tm * ttime)); /* Convert from time_t to png_time. Uses gmtime() */ -PNG_EXPORT(25, void, png_convert_from_time_t, - (png_timep ptime, time_t ttime)); -#endif /* PNG_CONVERT_tIME_SUPPORTED */ +PNG_EXPORT(25, void, png_convert_from_time_t, (png_timep ptime, time_t ttime)); +#endif /* CONVERT_tIME */ #ifdef PNG_READ_EXPAND_SUPPORTED /* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */ -PNG_EXPORT(26, void, png_set_expand, (png_structp png_ptr)); -PNG_EXPORT(27, void, png_set_expand_gray_1_2_4_to_8, (png_structp png_ptr)); -PNG_EXPORT(28, void, png_set_palette_to_rgb, (png_structp png_ptr)); -PNG_EXPORT(29, void, png_set_tRNS_to_alpha, (png_structp png_ptr)); +PNG_EXPORT(26, void, png_set_expand, (png_structrp png_ptr)); +PNG_EXPORT(27, void, png_set_expand_gray_1_2_4_to_8, (png_structrp png_ptr)); +PNG_EXPORT(28, void, png_set_palette_to_rgb, (png_structrp png_ptr)); +PNG_EXPORT(29, void, png_set_tRNS_to_alpha, (png_structrp png_ptr)); #endif #ifdef PNG_READ_EXPAND_16_SUPPORTED /* Expand to 16-bit channels, forces conversion of palette to RGB and expansion * of a tRNS chunk if present. */ -PNG_EXPORT(221, void, png_set_expand_16, (png_structp png_ptr)); +PNG_EXPORT(221, void, png_set_expand_16, (png_structrp png_ptr)); #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) /* Use blue, green, red order for pixels. */ -PNG_EXPORT(30, void, png_set_bgr, (png_structp png_ptr)); +PNG_EXPORT(30, void, png_set_bgr, (png_structrp png_ptr)); #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand the grayscale to 24-bit RGB if necessary. */ -PNG_EXPORT(31, void, png_set_gray_to_rgb, (png_structp png_ptr)); +PNG_EXPORT(31, void, png_set_gray_to_rgb, (png_structrp png_ptr)); #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED @@ -1157,12 +1083,12 @@ PNG_EXPORT(31, void, png_set_gray_to_rgb, (png_structp png_ptr)); #define PNG_ERROR_ACTION_ERROR 3 #define PNG_RGB_TO_GRAY_DEFAULT (-1)/*for red/green coefficients*/ -PNG_FP_EXPORT(32, void, png_set_rgb_to_gray, (png_structp png_ptr, - int error_action, double red, double green)); -PNG_FIXED_EXPORT(33, void, png_set_rgb_to_gray_fixed, (png_structp png_ptr, - int error_action, png_fixed_point red, png_fixed_point green)); +PNG_FP_EXPORT(32, void, png_set_rgb_to_gray, (png_structrp png_ptr, + int error_action, double red, double green)) +PNG_FIXED_EXPORT(33, void, png_set_rgb_to_gray_fixed, (png_structrp png_ptr, + int error_action, png_fixed_point red, png_fixed_point green)) -PNG_EXPORT(34, png_byte, png_get_rgb_to_gray_status, (png_const_structp +PNG_EXPORT(34, png_byte, png_get_rgb_to_gray_status, (png_const_structrp png_ptr)); #endif @@ -1172,9 +1098,9 @@ PNG_EXPORT(35, void, png_build_grayscale_palette, (int bit_depth, #endif #ifdef PNG_READ_ALPHA_MODE_SUPPORTED -/* How the alpha channel is interpreted - this affects how the color channels of - * a PNG file are returned when an alpha channel, or tRNS chunk in a palette - * file, is present. +/* How the alpha channel is interpreted - this affects how the color channels + * of a PNG file are returned to the calling application when an alpha channel, + * or a tRNS chunk in a palette file, is present. * * This has no effect on the way pixels are written into a PNG output * datastream. The color samples in a PNG datastream are never premultiplied @@ -1182,33 +1108,19 @@ PNG_EXPORT(35, void, png_build_grayscale_palette, (int bit_depth, * * The default is to return data according to the PNG specification: the alpha * channel is a linear measure of the contribution of the pixel to the - * corresponding composited pixel. The gamma encoded color channels must be - * scaled according to the contribution and to do this it is necessary to undo + * corresponding composited pixel, and the color channels are unassociated + * (not premultiplied). The gamma encoded color channels must be scaled + * according to the contribution and to do this it is necessary to undo * the encoding, scale the color values, perform the composition and reencode * the values. This is the 'PNG' mode. * * The alternative is to 'associate' the alpha with the color information by - * storing color channel values that have been scaled by the alpha. The - * advantage is that the color channels can be resampled (the image can be - * scaled) in this form. The disadvantage is that normal practice is to store - * linear, not (gamma) encoded, values and this requires 16-bit channels for - * still images rather than the 8-bit channels that are just about sufficient if - * gamma encoding is used. In addition all non-transparent pixel values, - * including completely opaque ones, must be gamma encoded to produce the final - * image. This is the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' mode (the - * latter being the two common names for associated alpha color channels.) + * storing color channel values that have been scaled by the alpha. + * image. These are the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' modes + * (the latter being the two common names for associated alpha color channels). * - * Since it is not necessary to perform arithmetic on opaque color values so - * long as they are not to be resampled and are in the final color space it is - * possible to optimize the handling of alpha by storing the opaque pixels in - * the PNG format (adjusted for the output color space) while storing partially - * opaque pixels in the standard, linear, format. The accuracy required for - * standard alpha composition is relatively low, because the pixels are - * isolated, therefore typically the accuracy loss in storing 8-bit linear - * values is acceptable. (This is not true if the alpha channel is used to - * simulate transparency over large areas - use 16 bits or the PNG mode in - * this case!) This is the 'OPTIMIZED' mode. For this mode a pixel is - * treated as opaque only if the alpha value is equal to the maximum value. + * For the 'OPTIMIZED' mode, a pixel is treated as opaque only if the alpha + * value is equal to the maximum value. * * The final choice is to gamma encode the alpha channel as well. This is * broken because, in practice, no implementation that uses this choice @@ -1227,76 +1139,15 @@ PNG_EXPORT(35, void, png_build_grayscale_palette, (int bit_depth, #define PNG_ALPHA_OPTIMIZED 2 /* 'PNG' for opaque pixels, else 'STANDARD' */ #define PNG_ALPHA_BROKEN 3 /* the alpha channel is gamma encoded */ -PNG_FP_EXPORT(227, void, png_set_alpha_mode, (png_structp png_ptr, int mode, - double output_gamma)); -PNG_FIXED_EXPORT(228, void, png_set_alpha_mode_fixed, (png_structp png_ptr, - int mode, png_fixed_point output_gamma)); +PNG_FP_EXPORT(227, void, png_set_alpha_mode, (png_structrp png_ptr, int mode, + double output_gamma)) +PNG_FIXED_EXPORT(228, void, png_set_alpha_mode_fixed, (png_structrp png_ptr, + int mode, png_fixed_point output_gamma)) #endif -#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_ALPHA_MODE_SUPPORTED) +#if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_READ_ALPHA_MODE_SUPPORTED) /* The output_gamma value is a screen gamma in libpng terminology: it expresses - * how to decode the output values, not how they are encoded. The values used - * correspond to the normal numbers used to describe the overall gamma of a - * computer display system; for example 2.2 for an sRGB conformant system. The - * values are scaled by 100000 in the _fixed version of the API (so 220000 for - * sRGB.) - * - * The inverse of the value is always used to provide a default for the PNG file - * encoding if it has no gAMA chunk and if png_set_gamma() has not been called - * to override the PNG gamma information. - * - * When the ALPHA_OPTIMIZED mode is selected the output gamma is used to encode - * opaque pixels however pixels with lower alpha values are not encoded, - * regardless of the output gamma setting. - * - * When the standard Porter Duff handling is requested with mode 1 the output - * encoding is set to be linear and the output_gamma value is only relevant - * as a default for input data that has no gamma information. The linear output - * encoding will be overridden if png_set_gamma() is called - the results may be - * highly unexpected! - * - * The following numbers are derived from the sRGB standard and the research - * behind it. sRGB is defined to be approximated by a PNG gAMA chunk value of - * 0.45455 (1/2.2) for PNG. The value implicitly includes any viewing - * correction required to take account of any differences in the color - * environment of the original scene and the intended display environment; the - * value expresses how to *decode* the image for display, not how the original - * data was *encoded*. - * - * sRGB provides a peg for the PNG standard by defining a viewing environment. - * sRGB itself, and earlier TV standards, actually use a more complex transform - * (a linear portion then a gamma 2.4 power law) than PNG can express. (PNG is - * limited to simple power laws.) By saying that an image for direct display on - * an sRGB conformant system should be stored with a gAMA chunk value of 45455 - * (11.3.3.2 and 11.3.3.5 of the ISO PNG specification) the PNG specification - * makes it possible to derive values for other display systems and - * environments. - * - * The Mac value is deduced from the sRGB based on an assumption that the actual - * extra viewing correction used in early Mac display systems was implemented as - * a power 1.45 lookup table. - * - * Any system where a programmable lookup table is used or where the behavior of - * the final display device characteristics can be changed requires system - * specific code to obtain the current characteristic. However this can be - * difficult and most PNG gamma correction only requires an approximate value. - * - * By default, if png_set_alpha_mode() is not called, libpng assumes that all - * values are unencoded, linear, values and that the output device also has a - * linear characteristic. This is only very rarely correct - it is invariably - * better to call png_set_alpha_mode() with PNG_DEFAULT_sRGB than rely on the - * default if you don't know what the right answer is! - * - * The special value PNG_GAMMA_MAC_18 indicates an older Mac system (pre Mac OS - * 10.6) which used a correction table to implement a somewhat lower gamma on an - * otherwise sRGB system. - * - * Both these values are reserved (not simple gamma values) in order to allow - * more precise correction internally in the future. - * - * NOTE: the following values can be passed to either the fixed or floating - * point APIs, but the floating point API will also accept floating point - * values. + * how to decode the output values, not how they are encoded. */ #define PNG_DEFAULT_sRGB -1 /* sRGB gamma and color space */ #define PNG_GAMMA_MAC_18 -2 /* Old Mac '1.8' gamma and color space */ @@ -1381,51 +1232,50 @@ PNG_FIXED_EXPORT(228, void, png_set_alpha_mode_fixed, (png_structp png_ptr, */ #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED -PNG_EXPORT(36, void, png_set_strip_alpha, (png_structp png_ptr)); +PNG_EXPORT(36, void, png_set_strip_alpha, (png_structrp png_ptr)); #endif #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) -PNG_EXPORT(37, void, png_set_swap_alpha, (png_structp png_ptr)); +PNG_EXPORT(37, void, png_set_swap_alpha, (png_structrp png_ptr)); #endif #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) -PNG_EXPORT(38, void, png_set_invert_alpha, (png_structp png_ptr)); +PNG_EXPORT(38, void, png_set_invert_alpha, (png_structrp png_ptr)); #endif #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) -/* Add a filler byte to 8-bit Gray or 24-bit RGB images. */ -PNG_EXPORT(39, void, png_set_filler, (png_structp png_ptr, png_uint_32 filler, +/* Add a filler byte to 8-bit or 16-bit Gray or 24-bit or 48-bit RGB images. */ +PNG_EXPORT(39, void, png_set_filler, (png_structrp png_ptr, png_uint_32 filler, int flags)); /* The values of the PNG_FILLER_ defines should NOT be changed */ # define PNG_FILLER_BEFORE 0 # define PNG_FILLER_AFTER 1 -/* Add an alpha byte to 8-bit Gray or 24-bit RGB images. */ -PNG_EXPORT(40, void, png_set_add_alpha, - (png_structp png_ptr, png_uint_32 filler, - int flags)); -#endif /* PNG_READ_FILLER_SUPPORTED || PNG_WRITE_FILLER_SUPPORTED */ +/* Add an alpha byte to 8-bit or 16-bit Gray or 24-bit or 48-bit RGB images. */ +PNG_EXPORT(40, void, png_set_add_alpha, (png_structrp png_ptr, + png_uint_32 filler, int flags)); +#endif /* READ_FILLER || WRITE_FILLER */ #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) /* Swap bytes in 16-bit depth files. */ -PNG_EXPORT(41, void, png_set_swap, (png_structp png_ptr)); +PNG_EXPORT(41, void, png_set_swap, (png_structrp png_ptr)); #endif #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) /* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */ -PNG_EXPORT(42, void, png_set_packing, (png_structp png_ptr)); +PNG_EXPORT(42, void, png_set_packing, (png_structrp png_ptr)); #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED) || \ defined(PNG_WRITE_PACKSWAP_SUPPORTED) /* Swap packing order of pixels in bytes. */ -PNG_EXPORT(43, void, png_set_packswap, (png_structp png_ptr)); +PNG_EXPORT(43, void, png_set_packswap, (png_structrp png_ptr)); #endif #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) /* Converts files to legal bit depths. */ -PNG_EXPORT(44, void, png_set_shift, (png_structp png_ptr, png_const_color_8p +PNG_EXPORT(44, void, png_set_shift, (png_structrp png_ptr, png_const_color_8p true_bits)); #endif @@ -1437,12 +1287,12 @@ PNG_EXPORT(44, void, png_set_shift, (png_structp png_ptr, png_const_color_8p * necessary to call png_read_row or png_read_rows png_get_image_height * times for each pass. */ -PNG_EXPORT(45, int, png_set_interlace_handling, (png_structp png_ptr)); +PNG_EXPORT(45, int, png_set_interlace_handling, (png_structrp png_ptr)); #endif #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) /* Invert monochrome files */ -PNG_EXPORT(46, void, png_set_invert_mono, (png_structp png_ptr)); +PNG_EXPORT(46, void, png_set_invert_mono, (png_structrp png_ptr)); #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED @@ -1451,12 +1301,12 @@ PNG_EXPORT(46, void, png_set_invert_mono, (png_structp png_ptr)); * read. Doing so will result in unexpected behavior and possible warnings or * errors if the PNG file contains a bKGD chunk. */ -PNG_FP_EXPORT(47, void, png_set_background, (png_structp png_ptr, +PNG_FP_EXPORT(47, void, png_set_background, (png_structrp png_ptr, png_const_color_16p background_color, int background_gamma_code, - int need_expand, double background_gamma)); -PNG_FIXED_EXPORT(215, void, png_set_background_fixed, (png_structp png_ptr, + int need_expand, double background_gamma)) +PNG_FIXED_EXPORT(215, void, png_set_background_fixed, (png_structrp png_ptr, png_const_color_16p background_color, int background_gamma_code, - int need_expand, png_fixed_point background_gamma)); + int need_expand, png_fixed_point background_gamma)) #endif #ifdef PNG_READ_BACKGROUND_SUPPORTED # define PNG_BACKGROUND_GAMMA_UNKNOWN 0 @@ -1467,23 +1317,22 @@ PNG_FIXED_EXPORT(215, void, png_set_background_fixed, (png_structp png_ptr, #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED /* Scale a 16-bit depth file down to 8-bit, accurately. */ -PNG_EXPORT(229, void, png_set_scale_16, (png_structp png_ptr)); +PNG_EXPORT(229, void, png_set_scale_16, (png_structrp png_ptr)); #endif #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED #define PNG_READ_16_TO_8 SUPPORTED /* Name prior to 1.5.4 */ /* Strip the second byte of information from a 16-bit depth file. */ -PNG_EXPORT(48, void, png_set_strip_16, (png_structp png_ptr)); +PNG_EXPORT(48, void, png_set_strip_16, (png_structrp png_ptr)); #endif #ifdef PNG_READ_QUANTIZE_SUPPORTED /* Turn on quantizing, and reduce the palette to the number of colors * available. */ -PNG_EXPORT(49, void, png_set_quantize, - (png_structp png_ptr, png_colorp palette, - int num_palette, int maximum_colors, png_const_uint_16p histogram, - int full_quantize)); +PNG_EXPORT(49, void, png_set_quantize, (png_structrp png_ptr, + png_colorp palette, int num_palette, int maximum_colors, + png_const_uint_16p histogram, int full_quantize)); #endif #ifdef PNG_READ_GAMMA_SUPPORTED @@ -1503,71 +1352,69 @@ PNG_EXPORT(49, void, png_set_quantize, * API (floating point or fixed.) Notice, however, that the 'file_gamma' value * is the inverse of a 'screen gamma' value. */ -PNG_FP_EXPORT(50, void, png_set_gamma, - (png_structp png_ptr, double screen_gamma, - double override_file_gamma)); -PNG_FIXED_EXPORT(208, void, png_set_gamma_fixed, (png_structp png_ptr, - png_fixed_point screen_gamma, png_fixed_point override_file_gamma)); +PNG_FP_EXPORT(50, void, png_set_gamma, (png_structrp png_ptr, + double screen_gamma, double override_file_gamma)) +PNG_FIXED_EXPORT(208, void, png_set_gamma_fixed, (png_structrp png_ptr, + png_fixed_point screen_gamma, png_fixed_point override_file_gamma)) #endif #ifdef PNG_WRITE_FLUSH_SUPPORTED /* Set how many lines between output flushes - 0 for no flushing */ -PNG_EXPORT(51, void, png_set_flush, (png_structp png_ptr, int nrows)); +PNG_EXPORT(51, void, png_set_flush, (png_structrp png_ptr, int nrows)); /* Flush the current PNG output buffer */ -PNG_EXPORT(52, void, png_write_flush, (png_structp png_ptr)); +PNG_EXPORT(52, void, png_write_flush, (png_structrp png_ptr)); #endif /* Optional update palette with requested transformations */ -PNG_EXPORT(53, void, png_start_read_image, (png_structp png_ptr)); +PNG_EXPORT(53, void, png_start_read_image, (png_structrp png_ptr)); /* Optional call to update the users info structure */ -PNG_EXPORT(54, void, png_read_update_info, - (png_structp png_ptr, png_infop info_ptr)); +PNG_EXPORT(54, void, png_read_update_info, (png_structrp png_ptr, + png_inforp info_ptr)); #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read one or more rows of image data. */ -PNG_EXPORT(55, void, png_read_rows, (png_structp png_ptr, png_bytepp row, +PNG_EXPORT(55, void, png_read_rows, (png_structrp png_ptr, png_bytepp row, png_bytepp display_row, png_uint_32 num_rows)); #endif #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read a row of data. */ -PNG_EXPORT(56, void, png_read_row, (png_structp png_ptr, png_bytep row, +PNG_EXPORT(56, void, png_read_row, (png_structrp png_ptr, png_bytep row, png_bytep display_row)); #endif #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the whole image into memory at once. */ -PNG_EXPORT(57, void, png_read_image, (png_structp png_ptr, png_bytepp image)); +PNG_EXPORT(57, void, png_read_image, (png_structrp png_ptr, png_bytepp image)); #endif /* Write a row of image data */ -PNG_EXPORT(58, void, png_write_row, - (png_structp png_ptr, png_const_bytep row)); +PNG_EXPORT(58, void, png_write_row, (png_structrp png_ptr, + png_const_bytep row)); /* Write a few rows of image data: (*row) is not written; however, the type * is declared as writeable to maintain compatibility with previous versions * of libpng and to allow the 'display_row' array from read_rows to be passed * unchanged to write_rows. */ -PNG_EXPORT(59, void, png_write_rows, (png_structp png_ptr, png_bytepp row, +PNG_EXPORT(59, void, png_write_rows, (png_structrp png_ptr, png_bytepp row, png_uint_32 num_rows)); /* Write the image data */ -PNG_EXPORT(60, void, png_write_image, - (png_structp png_ptr, png_bytepp image)); +PNG_EXPORT(60, void, png_write_image, (png_structrp png_ptr, png_bytepp image)); /* Write the end of the PNG file. */ -PNG_EXPORT(61, void, png_write_end, - (png_structp png_ptr, png_infop info_ptr)); +PNG_EXPORT(61, void, png_write_end, (png_structrp png_ptr, + png_inforp info_ptr)); #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the end of the PNG file. */ -PNG_EXPORT(62, void, png_read_end, (png_structp png_ptr, png_infop info_ptr)); +PNG_EXPORT(62, void, png_read_end, (png_structrp png_ptr, png_inforp info_ptr)); #endif /* Free any memory associated with the png_info_struct */ -PNG_EXPORT(63, void, png_destroy_info_struct, (png_structp png_ptr, +PNG_EXPORT(63, void, png_destroy_info_struct, (png_const_structrp png_ptr, png_infopp info_ptr_ptr)); /* Free any memory associated with the png_struct and the png_info_structs */ @@ -1579,8 +1426,8 @@ PNG_EXPORT(65, void, png_destroy_write_struct, (png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)); /* Set the libpng method of handling chunk CRC errors */ -PNG_EXPORT(66, void, png_set_crc_action, - (png_structp png_ptr, int crit_action, int ancil_action)); +PNG_EXPORT(66, void, png_set_crc_action, (png_structrp png_ptr, int crit_action, + int ancil_action)); /* Values for png_set_crc_action() say how to handle CRC errors in * ancillary and critical chunks, and whether to use the data contained @@ -1598,6 +1445,7 @@ PNG_EXPORT(66, void, png_set_crc_action, #define PNG_CRC_QUIET_USE 4 /* quiet/use data quiet/use data */ #define PNG_CRC_NO_CHANGE 5 /* use current value use current value */ +#ifdef PNG_WRITE_SUPPORTED /* These functions give the user control over the scan-line filtering in * libpng and the compression methods used by zlib. These functions are * mainly useful for testing, as the defaults should work with most users. @@ -1609,8 +1457,9 @@ PNG_EXPORT(66, void, png_set_crc_action, /* Set the filtering method(s) used by libpng. Currently, the only valid * value for "method" is 0. */ -PNG_EXPORT(67, void, png_set_filter, - (png_structp png_ptr, int method, int filters)); +PNG_EXPORT(67, void, png_set_filter, (png_structrp png_ptr, int method, + int filters)); +#endif /* WRITE */ /* Flags for png_set_filter() to say which filters to use. The flags * are chosen so that they don't conflict with real filter types @@ -1636,53 +1485,23 @@ PNG_EXPORT(67, void, png_set_filter, #define PNG_FILTER_VALUE_PAETH 4 #define PNG_FILTER_VALUE_LAST 5 -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* EXPERIMENTAL */ -/* The "heuristic_method" is given by one of the PNG_FILTER_HEURISTIC_ - * defines, either the default (minimum-sum-of-absolute-differences), or - * the experimental method (weighted-minimum-sum-of-absolute-differences). - * - * Weights are factors >= 1.0, indicating how important it is to keep the - * filter type consistent between rows. Larger numbers mean the current - * filter is that many times as likely to be the same as the "num_weights" - * previous filters. This is cumulative for each previous row with a weight. - * There needs to be "num_weights" values in "filter_weights", or it can be - * NULL if the weights aren't being specified. Weights have no influence on - * the selection of the first row filter. Well chosen weights can (in theory) - * improve the compression for a given image. - * - * Costs are factors >= 1.0 indicating the relative decoding costs of a - * filter type. Higher costs indicate more decoding expense, and are - * therefore less likely to be selected over a filter with lower computational - * costs. There needs to be a value in "filter_costs" for each valid filter - * type (given by PNG_FILTER_VALUE_LAST), or it can be NULL if you aren't - * setting the costs. Costs try to improve the speed of decompression without - * unduly increasing the compressed image size. - * - * A negative weight or cost indicates the default value is to be used, and - * values in the range [0.0, 1.0) indicate the value is to remain unchanged. - * The default values for both weights and costs are currently 1.0, but may - * change if good general weighting/cost heuristics can be found. If both - * the weights and costs are set to 1.0, this degenerates the WEIGHTED method - * to the UNWEIGHTED method, but with added encoding time/computation. - */ -PNG_FP_EXPORT(68, void, png_set_filter_heuristics, (png_structp png_ptr, +#ifdef PNG_WRITE_SUPPORTED +#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */ +PNG_FP_EXPORT(68, void, png_set_filter_heuristics, (png_structrp png_ptr, int heuristic_method, int num_weights, png_const_doublep filter_weights, - png_const_doublep filter_costs)); + png_const_doublep filter_costs)) PNG_FIXED_EXPORT(209, void, png_set_filter_heuristics_fixed, - (png_structp png_ptr, - int heuristic_method, int num_weights, png_const_fixed_point_p - filter_weights, png_const_fixed_point_p filter_costs)); -#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ + (png_structrp png_ptr, int heuristic_method, int num_weights, + png_const_fixed_point_p filter_weights, + png_const_fixed_point_p filter_costs)) +#endif /* WRITE_WEIGHTED_FILTER */ -/* Heuristic used for row filter selection. These defines should NOT be - * changed. - */ +/* The following are no longer used and will be removed from libpng-1.7: */ #define PNG_FILTER_HEURISTIC_DEFAULT 0 /* Currently "UNWEIGHTED" */ #define PNG_FILTER_HEURISTIC_UNWEIGHTED 1 /* Used by libpng < 0.95 */ #define PNG_FILTER_HEURISTIC_WEIGHTED 2 /* Experimental feature */ #define PNG_FILTER_HEURISTIC_LAST 3 /* Not a valid value */ -#ifdef PNG_WRITE_SUPPORTED /* Set the library compression level. Currently, valid values range from * 0 - 9, corresponding directly to the zlib compression levels 0 - 9 * (0 - no compression, 9 - "maximal" compression). Note that tests have @@ -1690,45 +1509,47 @@ PNG_FIXED_EXPORT(209, void, png_set_filter_heuristics_fixed, * for PNG images, and do considerably fewer caclulations. In the future, * these values may not correspond directly to the zlib compression levels. */ -PNG_EXPORT(69, void, png_set_compression_level, - (png_structp png_ptr, int level)); +#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED +PNG_EXPORT(69, void, png_set_compression_level, (png_structrp png_ptr, + int level)); -PNG_EXPORT(70, void, png_set_compression_mem_level, (png_structp png_ptr, +PNG_EXPORT(70, void, png_set_compression_mem_level, (png_structrp png_ptr, int mem_level)); -PNG_EXPORT(71, void, png_set_compression_strategy, (png_structp png_ptr, +PNG_EXPORT(71, void, png_set_compression_strategy, (png_structrp png_ptr, int strategy)); /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a * smaller value of window_bits if it can do so safely. */ -PNG_EXPORT(72, void, png_set_compression_window_bits, (png_structp png_ptr, +PNG_EXPORT(72, void, png_set_compression_window_bits, (png_structrp png_ptr, int window_bits)); -PNG_EXPORT(73, void, png_set_compression_method, (png_structp png_ptr, +PNG_EXPORT(73, void, png_set_compression_method, (png_structrp png_ptr, int method)); -#endif +#endif /* WRITE_CUSTOMIZE_COMPRESSION */ #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED /* Also set zlib parameters for compressing non-IDAT chunks */ -PNG_EXPORT(222, void, png_set_text_compression_level, - (png_structp png_ptr, int level)); +PNG_EXPORT(222, void, png_set_text_compression_level, (png_structrp png_ptr, + int level)); -PNG_EXPORT(223, void, png_set_text_compression_mem_level, (png_structp png_ptr, +PNG_EXPORT(223, void, png_set_text_compression_mem_level, (png_structrp png_ptr, int mem_level)); -PNG_EXPORT(224, void, png_set_text_compression_strategy, (png_structp png_ptr, +PNG_EXPORT(224, void, png_set_text_compression_strategy, (png_structrp png_ptr, int strategy)); /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a * smaller value of window_bits if it can do so safely. */ -PNG_EXPORT(225, void, png_set_text_compression_window_bits, (png_structp - png_ptr, int window_bits)); +PNG_EXPORT(225, void, png_set_text_compression_window_bits, + (png_structrp png_ptr, int window_bits)); -PNG_EXPORT(226, void, png_set_text_compression_method, (png_structp png_ptr, +PNG_EXPORT(226, void, png_set_text_compression_method, (png_structrp png_ptr, int method)); -#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ +#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ +#endif /* WRITE */ /* These next functions are called for input/output, memory, and error * handling. They are in the file pngrio.c, pngwio.c, and pngerror.c, @@ -1741,7 +1562,7 @@ PNG_EXPORT(226, void, png_set_text_compression_method, (png_structp png_ptr, #ifdef PNG_STDIO_SUPPORTED /* Initialize the input/output for the PNG file to the default functions. */ -PNG_EXPORT(74, void, png_init_io, (png_structp png_ptr, png_FILE_p fp)); +PNG_EXPORT(74, void, png_init_io, (png_structrp png_ptr, png_FILE_p fp)); #endif /* Replace the (error and abort), and warning functions with user @@ -1752,12 +1573,11 @@ PNG_EXPORT(74, void, png_init_io, (png_structp png_ptr, png_FILE_p fp)); * default function will be used. */ -PNG_EXPORT(75, void, png_set_error_fn, - (png_structp png_ptr, png_voidp error_ptr, - png_error_ptr error_fn, png_error_ptr warning_fn)); +PNG_EXPORT(75, void, png_set_error_fn, (png_structrp png_ptr, + png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn)); /* Return the user pointer associated with the error functions */ -PNG_EXPORT(76, png_voidp, png_get_error_ptr, (png_const_structp png_ptr)); +PNG_EXPORT(76, png_voidp, png_get_error_ptr, (png_const_structrp png_ptr)); /* Replace the default data output functions with a user supplied one(s). * If buffered output is not used, then output_flush_fn can be set to NULL. @@ -1769,47 +1589,47 @@ PNG_EXPORT(76, png_voidp, png_get_error_ptr, (png_const_structp png_ptr)); * default flush function, which uses the standard *FILE structure, will * be used. */ -PNG_EXPORT(77, void, png_set_write_fn, (png_structp png_ptr, png_voidp io_ptr, +PNG_EXPORT(77, void, png_set_write_fn, (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)); /* Replace the default data input function with a user supplied one. */ -PNG_EXPORT(78, void, png_set_read_fn, (png_structp png_ptr, png_voidp io_ptr, +PNG_EXPORT(78, void, png_set_read_fn, (png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn)); /* Return the user pointer associated with the I/O functions */ -PNG_EXPORT(79, png_voidp, png_get_io_ptr, (png_structp png_ptr)); +PNG_EXPORT(79, png_voidp, png_get_io_ptr, (png_const_structrp png_ptr)); -PNG_EXPORT(80, void, png_set_read_status_fn, (png_structp png_ptr, +PNG_EXPORT(80, void, png_set_read_status_fn, (png_structrp png_ptr, png_read_status_ptr read_row_fn)); -PNG_EXPORT(81, void, png_set_write_status_fn, (png_structp png_ptr, +PNG_EXPORT(81, void, png_set_write_status_fn, (png_structrp png_ptr, png_write_status_ptr write_row_fn)); #ifdef PNG_USER_MEM_SUPPORTED /* Replace the default memory allocation functions with user supplied one(s). */ -PNG_EXPORT(82, void, png_set_mem_fn, (png_structp png_ptr, png_voidp mem_ptr, +PNG_EXPORT(82, void, png_set_mem_fn, (png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn)); /* Return the user pointer associated with the memory functions */ -PNG_EXPORT(83, png_voidp, png_get_mem_ptr, (png_const_structp png_ptr)); +PNG_EXPORT(83, png_voidp, png_get_mem_ptr, (png_const_structrp png_ptr)); #endif #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED -PNG_EXPORT(84, void, png_set_read_user_transform_fn, (png_structp png_ptr, +PNG_EXPORT(84, void, png_set_read_user_transform_fn, (png_structrp png_ptr, png_user_transform_ptr read_user_transform_fn)); #endif #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED -PNG_EXPORT(85, void, png_set_write_user_transform_fn, (png_structp png_ptr, +PNG_EXPORT(85, void, png_set_write_user_transform_fn, (png_structrp png_ptr, png_user_transform_ptr write_user_transform_fn)); #endif #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED -PNG_EXPORT(86, void, png_set_user_transform_info, (png_structp png_ptr, +PNG_EXPORT(86, void, png_set_user_transform_info, (png_structrp png_ptr, png_voidp user_transform_ptr, int user_transform_depth, int user_transform_channels)); /* Return the user pointer associated with the user transform functions */ PNG_EXPORT(87, png_voidp, png_get_user_transform_ptr, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); #endif #ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED @@ -1824,31 +1644,53 @@ PNG_EXPORT(87, png_voidp, png_get_user_transform_ptr, * find the output pixel (x,y) given an interlaced sub-image pixel * (row,col,pass). (See below for these macros.) */ -PNG_EXPORT(217, png_uint_32, png_get_current_row_number, (png_const_structp)); -PNG_EXPORT(218, png_byte, png_get_current_pass_number, (png_const_structp)); +PNG_EXPORT(217, png_uint_32, png_get_current_row_number, (png_const_structrp)); +PNG_EXPORT(218, png_byte, png_get_current_pass_number, (png_const_structrp)); +#endif + +#ifdef PNG_READ_USER_CHUNKS_SUPPORTED +/* This callback is called only for *unknown* chunks. If + * PNG_HANDLE_AS_UNKNOWN_SUPPORTED is set then it is possible to set known + * chunks to be treated as unknown, however in this case the callback must do + * any processing required by the chunk (e.g. by calling the appropriate + * png_set_ APIs.) + * + * There is no write support - on write, by default, all the chunks in the + * 'unknown' list are written in the specified position. + * + * The integer return from the callback function is interpreted thus: + * + * negative: An error occurred; png_chunk_error will be called. + * zero: The chunk was not handled, the chunk will be saved. A critical + * chunk will cause an error at this point unless it is to be saved. + * positive: The chunk was handled, libpng will ignore/discard it. + * + * See "INTERACTION WTIH USER CHUNK CALLBACKS" below for important notes about + * how this behavior will change in libpng 1.7 + */ +PNG_EXPORT(88, void, png_set_read_user_chunk_fn, (png_structrp png_ptr, + png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn)); #endif #ifdef PNG_USER_CHUNKS_SUPPORTED -PNG_EXPORT(88, void, png_set_read_user_chunk_fn, (png_structp png_ptr, - png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn)); -PNG_EXPORT(89, png_voidp, png_get_user_chunk_ptr, (png_const_structp png_ptr)); +PNG_EXPORT(89, png_voidp, png_get_user_chunk_ptr, (png_const_structrp png_ptr)); #endif #ifdef PNG_PROGRESSIVE_READ_SUPPORTED /* Sets the function callbacks for the push reader, and a pointer to a * user-defined structure available to the callback functions. */ -PNG_EXPORT(90, void, png_set_progressive_read_fn, (png_structp png_ptr, +PNG_EXPORT(90, void, png_set_progressive_read_fn, (png_structrp png_ptr, png_voidp progressive_ptr, png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn)); /* Returns the user pointer associated with the push read functions */ -PNG_EXPORT(91, png_voidp, png_get_progressive_ptr, (png_const_structp png_ptr)); +PNG_EXPORT(91, png_voidp, png_get_progressive_ptr, + (png_const_structrp png_ptr)); /* Function to be called when data becomes available */ -PNG_EXPORT(92, void, png_process_data, - (png_structp png_ptr, png_infop info_ptr, - png_bytep buffer, png_size_t buffer_size)); +PNG_EXPORT(92, void, png_process_data, (png_structrp png_ptr, + png_inforp info_ptr, png_bytep buffer, png_size_t buffer_size)); /* A function which may be called *only* within png_process_data to stop the * processing of any more data. The function returns the number of bytes @@ -1857,7 +1699,7 @@ PNG_EXPORT(92, void, png_process_data, * 'save' is set to true the routine will first save all the pending data and * will always return 0. */ -PNG_EXPORT(219, png_size_t, png_process_data_pause, (png_structp, int save)); +PNG_EXPORT(219, png_size_t, png_process_data_pause, (png_structrp, int save)); /* A function which may be called *only* outside (after) a call to * png_process_data. It returns the number of bytes of data to skip in the @@ -1865,42 +1707,43 @@ PNG_EXPORT(219, png_size_t, png_process_data_pause, (png_structp, int save)); * application must skip than number of bytes of input data and pass the * following data to the next call to png_process_data. */ -PNG_EXPORT(220, png_uint_32, png_process_data_skip, (png_structp)); +PNG_EXPORT(220, png_uint_32, png_process_data_skip, (png_structrp)); -#ifdef PNG_READ_INTERLACING_SUPPORTED /* Function that combines rows. 'new_row' is a flag that should come from * the callback and be non-NULL if anything needs to be done; the library * stores its own version of the new data internally and ignores the passed * in value. */ -PNG_EXPORT(93, void, png_progressive_combine_row, (png_structp png_ptr, +PNG_EXPORT(93, void, png_progressive_combine_row, (png_const_structrp png_ptr, png_bytep old_row, png_const_bytep new_row)); -#endif /* PNG_READ_INTERLACING_SUPPORTED */ -#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ +#endif /* PROGRESSIVE_READ */ -PNG_EXPORTA(94, png_voidp, png_malloc, - (png_structp png_ptr, png_alloc_size_t size), - PNG_ALLOCATED); +PNG_EXPORTA(94, png_voidp, png_malloc, (png_const_structrp png_ptr, + png_alloc_size_t size), PNG_ALLOCATED); /* Added at libpng version 1.4.0 */ -PNG_EXPORTA(95, png_voidp, png_calloc, - (png_structp png_ptr, png_alloc_size_t size), - PNG_ALLOCATED); +PNG_EXPORTA(95, png_voidp, png_calloc, (png_const_structrp png_ptr, + png_alloc_size_t size), PNG_ALLOCATED); /* Added at libpng version 1.2.4 */ -PNG_EXPORTA(96, png_voidp, png_malloc_warn, (png_structp png_ptr, +PNG_EXPORTA(96, png_voidp, png_malloc_warn, (png_const_structrp png_ptr, png_alloc_size_t size), PNG_ALLOCATED); /* Frees a pointer allocated by png_malloc() */ -PNG_EXPORT(97, void, png_free, (png_structp png_ptr, png_voidp ptr)); +PNG_EXPORT(97, void, png_free, (png_const_structrp png_ptr, png_voidp ptr)); /* Free data that was allocated internally */ -PNG_EXPORT(98, void, png_free_data, - (png_structp png_ptr, png_infop info_ptr, png_uint_32 free_me, int num)); +PNG_EXPORT(98, void, png_free_data, (png_const_structrp png_ptr, + png_inforp info_ptr, png_uint_32 free_me, int num)); /* Reassign responsibility for freeing existing data, whether allocated - * by libpng or by the application */ -PNG_EXPORT(99, void, png_data_freer, - (png_structp png_ptr, png_infop info_ptr, int freer, png_uint_32 mask)); + * by libpng or by the application; this works on the png_info structure passed + * in, it does not change the state for other png_info structures. + * + * It is unlikely that this function works correctly as of 1.6.0 and using it + * may result either in memory leaks or double free of allocated data. + */ +PNG_EXPORT(99, void, png_data_freer, (png_const_structrp png_ptr, + png_inforp info_ptr, int freer, png_uint_32 mask)); /* Assignments for png_data_freer */ #define PNG_DESTROY_WILL_FREE_DATA 1 @@ -1913,8 +1756,10 @@ PNG_EXPORT(99, void, png_data_freer, #define PNG_FREE_ROWS 0x0040 #define PNG_FREE_PCAL 0x0080 #define PNG_FREE_SCAL 0x0100 -#define PNG_FREE_UNKN 0x0200 -#define PNG_FREE_LIST 0x0400 +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED +# define PNG_FREE_UNKN 0x0200 +#endif +/* PNG_FREE_LIST 0x0400 removed in 1.6.0 because it is ignored */ #define PNG_FREE_PLTE 0x1000 #define PNG_FREE_TRNS 0x2000 #define PNG_FREE_TEXT 0x4000 @@ -1922,50 +1767,55 @@ PNG_EXPORT(99, void, png_data_freer, #define PNG_FREE_MUL 0x4220 /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */ #ifdef PNG_USER_MEM_SUPPORTED -PNG_EXPORTA(100, png_voidp, png_malloc_default, (png_structp png_ptr, - png_alloc_size_t size), PNG_ALLOCATED); -PNG_EXPORT(101, void, png_free_default, (png_structp png_ptr, png_voidp ptr)); +PNG_EXPORTA(100, png_voidp, png_malloc_default, (png_const_structrp png_ptr, + png_alloc_size_t size), PNG_ALLOCATED PNG_DEPRECATED); +PNG_EXPORTA(101, void, png_free_default, (png_const_structrp png_ptr, + png_voidp ptr), PNG_DEPRECATED); #endif #ifdef PNG_ERROR_TEXT_SUPPORTED /* Fatal error in PNG image of libpng - can't continue */ -PNG_EXPORTA(102, void, png_error, - (png_structp png_ptr, png_const_charp error_message), - PNG_NORETURN); +PNG_EXPORTA(102, void, png_error, (png_const_structrp png_ptr, + png_const_charp error_message), PNG_NORETURN); /* The same, but the chunk name is prepended to the error string. */ -PNG_EXPORTA(103, void, png_chunk_error, (png_structp png_ptr, +PNG_EXPORTA(103, void, png_chunk_error, (png_const_structrp png_ptr, png_const_charp error_message), PNG_NORETURN); #else /* Fatal error in PNG image of libpng - can't continue */ -PNG_EXPORTA(104, void, png_err, (png_structp png_ptr), PNG_NORETURN); +PNG_EXPORTA(104, void, png_err, (png_const_structrp png_ptr), PNG_NORETURN); +# define png_error(s1,s2) png_err(s1) +# define png_chunk_error(s1,s2) png_err(s1) #endif #ifdef PNG_WARNINGS_SUPPORTED /* Non-fatal error in libpng. Can continue, but may have a problem. */ -PNG_EXPORT(105, void, png_warning, (png_structp png_ptr, +PNG_EXPORT(105, void, png_warning, (png_const_structrp png_ptr, png_const_charp warning_message)); /* Non-fatal error in libpng, chunk name is prepended to message. */ -PNG_EXPORT(106, void, png_chunk_warning, (png_structp png_ptr, +PNG_EXPORT(106, void, png_chunk_warning, (png_const_structrp png_ptr, png_const_charp warning_message)); +#else +# define png_warning(s1,s2) ((void)(s1)) +# define png_chunk_warning(s1,s2) ((void)(s1)) #endif #ifdef PNG_BENIGN_ERRORS_SUPPORTED /* Benign error in libpng. Can continue, but may have a problem. * User can choose whether to handle as a fatal error or as a warning. */ -# undef png_benign_error -PNG_EXPORT(107, void, png_benign_error, (png_structp png_ptr, +PNG_EXPORT(107, void, png_benign_error, (png_const_structrp png_ptr, png_const_charp warning_message)); -/* Same, chunk name is prepended to message. */ -# undef png_chunk_benign_error -PNG_EXPORT(108, void, png_chunk_benign_error, (png_structp png_ptr, +#ifdef PNG_READ_SUPPORTED +/* Same, chunk name is prepended to message (only during read) */ +PNG_EXPORT(108, void, png_chunk_benign_error, (png_const_structrp png_ptr, png_const_charp warning_message)); +#endif PNG_EXPORT(109, void, png_set_benign_errors, - (png_structp png_ptr, int allowed)); + (png_structrp png_ptr, int allowed)); #else # ifdef PNG_ALLOW_BENIGN_ERRORS # define png_benign_error png_warning @@ -1989,289 +1839,274 @@ PNG_EXPORT(109, void, png_set_benign_errors, * png_info_struct. */ /* Returns "flag" if chunk data is valid in info_ptr. */ -PNG_EXPORT(110, png_uint_32, png_get_valid, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_uint_32 flag)); +PNG_EXPORT(110, png_uint_32, png_get_valid, (png_const_structrp png_ptr, + png_const_inforp info_ptr, png_uint_32 flag)); /* Returns number of bytes needed to hold a transformed row. */ -PNG_EXPORT(111, png_size_t, png_get_rowbytes, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(111, png_size_t, png_get_rowbytes, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); #ifdef PNG_INFO_IMAGE_SUPPORTED /* Returns row_pointers, which is an array of pointers to scanlines that was * returned from png_read_png(). */ -PNG_EXPORT(112, png_bytepp, png_get_rows, - (png_const_structp png_ptr, png_const_infop info_ptr)); +PNG_EXPORT(112, png_bytepp, png_get_rows, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); + /* Set row_pointers, which is an array of pointers to scanlines for use * by png_write_png(). */ -PNG_EXPORT(113, void, png_set_rows, (png_structp png_ptr, - png_infop info_ptr, png_bytepp row_pointers)); +PNG_EXPORT(113, void, png_set_rows, (png_const_structrp png_ptr, + png_inforp info_ptr, png_bytepp row_pointers)); #endif /* Returns number of color channels in image. */ -PNG_EXPORT(114, png_byte, png_get_channels, - (png_const_structp png_ptr, png_const_infop info_ptr)); +PNG_EXPORT(114, png_byte, png_get_channels, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); #ifdef PNG_EASY_ACCESS_SUPPORTED /* Returns image width in pixels. */ -PNG_EXPORT(115, png_uint_32, png_get_image_width, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(115, png_uint_32, png_get_image_width, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image height in pixels. */ -PNG_EXPORT(116, png_uint_32, png_get_image_height, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(116, png_uint_32, png_get_image_height, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image bit_depth. */ -PNG_EXPORT(117, png_byte, png_get_bit_depth, - (png_const_structp png_ptr, png_const_infop info_ptr)); +PNG_EXPORT(117, png_byte, png_get_bit_depth, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image color_type. */ -PNG_EXPORT(118, png_byte, png_get_color_type, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(118, png_byte, png_get_color_type, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image filter_type. */ -PNG_EXPORT(119, png_byte, png_get_filter_type, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(119, png_byte, png_get_filter_type, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image interlace_type. */ -PNG_EXPORT(120, png_byte, png_get_interlace_type, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(120, png_byte, png_get_interlace_type, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image compression_type. */ -PNG_EXPORT(121, png_byte, png_get_compression_type, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_EXPORT(121, png_byte, png_get_compression_type, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); /* Returns image resolution in pixels per meter, from pHYs chunk data. */ PNG_EXPORT(122, png_uint_32, png_get_pixels_per_meter, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(123, png_uint_32, png_get_x_pixels_per_meter, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(124, png_uint_32, png_get_y_pixels_per_meter, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); /* Returns pixel aspect ratio, computed from pHYs chunk data. */ PNG_FP_EXPORT(125, float, png_get_pixel_aspect_ratio, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)) PNG_FIXED_EXPORT(210, png_fixed_point, png_get_pixel_aspect_ratio_fixed, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)) /* Returns image x, y offset in pixels or microns, from oFFs chunk data. */ PNG_EXPORT(126, png_int_32, png_get_x_offset_pixels, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(127, png_int_32, png_get_y_offset_pixels, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(128, png_int_32, png_get_x_offset_microns, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(129, png_int_32, png_get_y_offset_microns, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); -#endif /* PNG_EASY_ACCESS_SUPPORTED */ +#endif /* EASY_ACCESS */ +#ifdef PNG_READ_SUPPORTED /* Returns pointer to signature string read from PNG header */ -PNG_EXPORT(130, png_const_bytep, png_get_signature, - (png_const_structp png_ptr, png_infop info_ptr)); - -#ifdef PNG_bKGD_SUPPORTED -PNG_EXPORT(131, png_uint_32, png_get_bKGD, - (png_const_structp png_ptr, png_infop info_ptr, - png_color_16p *background)); +PNG_EXPORT(130, png_const_bytep, png_get_signature, (png_const_structrp png_ptr, + png_const_inforp info_ptr)); #endif #ifdef PNG_bKGD_SUPPORTED -PNG_EXPORT(132, void, png_set_bKGD, (png_structp png_ptr, png_infop info_ptr, - png_const_color_16p background)); +PNG_EXPORT(131, png_uint_32, png_get_bKGD, (png_const_structrp png_ptr, + png_inforp info_ptr, png_color_16p *background)); +#endif + +#ifdef PNG_bKGD_SUPPORTED +PNG_EXPORT(132, void, png_set_bKGD, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_color_16p background)); #endif #ifdef PNG_cHRM_SUPPORTED -PNG_FP_EXPORT(133, png_uint_32, png_get_cHRM, (png_const_structp png_ptr, - png_const_infop info_ptr, double *white_x, double *white_y, double *red_x, +PNG_FP_EXPORT(133, png_uint_32, png_get_cHRM, (png_const_structrp png_ptr, + png_const_inforp info_ptr, double *white_x, double *white_y, double *red_x, double *red_y, double *green_x, double *green_y, double *blue_x, - double *blue_y)); -PNG_FP_EXPORT(230, png_uint_32, png_get_cHRM_XYZ, (png_structp png_ptr, - png_const_infop info_ptr, double *red_X, double *red_Y, double *red_Z, + double *blue_y)) +PNG_FP_EXPORT(230, png_uint_32, png_get_cHRM_XYZ, (png_const_structrp png_ptr, + png_const_inforp info_ptr, double *red_X, double *red_Y, double *red_Z, double *green_X, double *green_Y, double *green_Z, double *blue_X, - double *blue_Y, double *blue_Z)); -#ifdef PNG_FIXED_POINT_SUPPORTED /* Otherwise not implemented */ + double *blue_Y, double *blue_Z)) PNG_FIXED_EXPORT(134, png_uint_32, png_get_cHRM_fixed, - (png_const_structp png_ptr, - png_const_infop info_ptr, png_fixed_point *int_white_x, - png_fixed_point *int_white_y, png_fixed_point *int_red_x, - png_fixed_point *int_red_y, png_fixed_point *int_green_x, - png_fixed_point *int_green_y, png_fixed_point *int_blue_x, - png_fixed_point *int_blue_y)); -#endif + (png_const_structrp png_ptr, png_const_inforp info_ptr, + png_fixed_point *int_white_x, png_fixed_point *int_white_y, + png_fixed_point *int_red_x, png_fixed_point *int_red_y, + png_fixed_point *int_green_x, png_fixed_point *int_green_y, + png_fixed_point *int_blue_x, png_fixed_point *int_blue_y)) PNG_FIXED_EXPORT(231, png_uint_32, png_get_cHRM_XYZ_fixed, - (png_structp png_ptr, png_const_infop info_ptr, + (png_const_structrp png_ptr, png_const_inforp info_ptr, png_fixed_point *int_red_X, png_fixed_point *int_red_Y, png_fixed_point *int_red_Z, png_fixed_point *int_green_X, png_fixed_point *int_green_Y, png_fixed_point *int_green_Z, png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y, - png_fixed_point *int_blue_Z)); + png_fixed_point *int_blue_Z)) #endif #ifdef PNG_cHRM_SUPPORTED -PNG_FP_EXPORT(135, void, png_set_cHRM, - (png_structp png_ptr, png_infop info_ptr, +PNG_FP_EXPORT(135, void, png_set_cHRM, (png_const_structrp png_ptr, + png_inforp info_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, - double green_y, double blue_x, double blue_y)); -PNG_FP_EXPORT(232, void, png_set_cHRM_XYZ, (png_structp png_ptr, - png_infop info_ptr, double red_X, double red_Y, double red_Z, + double green_y, double blue_x, double blue_y)) +PNG_FP_EXPORT(232, void, png_set_cHRM_XYZ, (png_const_structrp png_ptr, + png_inforp info_ptr, double red_X, double red_Y, double red_Z, double green_X, double green_Y, double green_Z, double blue_X, - double blue_Y, double blue_Z)); -PNG_FIXED_EXPORT(136, void, png_set_cHRM_fixed, (png_structp png_ptr, - png_infop info_ptr, png_fixed_point int_white_x, + double blue_Y, double blue_Z)) +PNG_FIXED_EXPORT(136, void, png_set_cHRM_fixed, (png_const_structrp png_ptr, + png_inforp info_ptr, png_fixed_point int_white_x, png_fixed_point int_white_y, png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, - png_fixed_point int_blue_y)); -PNG_FIXED_EXPORT(233, void, png_set_cHRM_XYZ_fixed, (png_structp png_ptr, - png_infop info_ptr, png_fixed_point int_red_X, png_fixed_point int_red_Y, + png_fixed_point int_blue_y)) +PNG_FIXED_EXPORT(233, void, png_set_cHRM_XYZ_fixed, (png_const_structrp png_ptr, + png_inforp info_ptr, png_fixed_point int_red_X, png_fixed_point int_red_Y, png_fixed_point int_red_Z, png_fixed_point int_green_X, png_fixed_point int_green_Y, png_fixed_point int_green_Z, png_fixed_point int_blue_X, png_fixed_point int_blue_Y, - png_fixed_point int_blue_Z)); + png_fixed_point int_blue_Z)) #endif #ifdef PNG_gAMA_SUPPORTED -PNG_FP_EXPORT(137, png_uint_32, png_get_gAMA, - (png_const_structp png_ptr, png_const_infop info_ptr, - double *file_gamma)); +PNG_FP_EXPORT(137, png_uint_32, png_get_gAMA, (png_const_structrp png_ptr, + png_const_inforp info_ptr, double *file_gamma)) PNG_FIXED_EXPORT(138, png_uint_32, png_get_gAMA_fixed, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_fixed_point *int_file_gamma)); + (png_const_structrp png_ptr, png_const_inforp info_ptr, + png_fixed_point *int_file_gamma)) #endif #ifdef PNG_gAMA_SUPPORTED -PNG_FP_EXPORT(139, void, png_set_gAMA, (png_structp png_ptr, - png_infop info_ptr, double file_gamma)); -PNG_FIXED_EXPORT(140, void, png_set_gAMA_fixed, (png_structp png_ptr, - png_infop info_ptr, png_fixed_point int_file_gamma)); +PNG_FP_EXPORT(139, void, png_set_gAMA, (png_const_structrp png_ptr, + png_inforp info_ptr, double file_gamma)) +PNG_FIXED_EXPORT(140, void, png_set_gAMA_fixed, (png_const_structrp png_ptr, + png_inforp info_ptr, png_fixed_point int_file_gamma)) #endif #ifdef PNG_hIST_SUPPORTED -PNG_EXPORT(141, png_uint_32, png_get_hIST, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_uint_16p *hist)); +PNG_EXPORT(141, png_uint_32, png_get_hIST, (png_const_structrp png_ptr, + png_inforp info_ptr, png_uint_16p *hist)); #endif #ifdef PNG_hIST_SUPPORTED -PNG_EXPORT(142, void, png_set_hIST, (png_structp png_ptr, - png_infop info_ptr, png_const_uint_16p hist)); +PNG_EXPORT(142, void, png_set_hIST, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_uint_16p hist)); #endif -PNG_EXPORT(143, png_uint_32, png_get_IHDR, - (png_structp png_ptr, png_infop info_ptr, - png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, - int *interlace_method, int *compression_method, int *filter_method)); +PNG_EXPORT(143, png_uint_32, png_get_IHDR, (png_const_structrp png_ptr, + png_const_inforp info_ptr, png_uint_32 *width, png_uint_32 *height, + int *bit_depth, int *color_type, int *interlace_method, + int *compression_method, int *filter_method)); -PNG_EXPORT(144, void, png_set_IHDR, - (png_structp png_ptr, png_infop info_ptr, - png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, - int interlace_method, int compression_method, int filter_method)); +PNG_EXPORT(144, void, png_set_IHDR, (png_const_structrp png_ptr, + png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, + int color_type, int interlace_method, int compression_method, + int filter_method)); #ifdef PNG_oFFs_SUPPORTED -PNG_EXPORT(145, png_uint_32, png_get_oFFs, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)); +PNG_EXPORT(145, png_uint_32, png_get_oFFs, (png_const_structrp png_ptr, + png_const_inforp info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, + int *unit_type)); #endif #ifdef PNG_oFFs_SUPPORTED -PNG_EXPORT(146, void, png_set_oFFs, - (png_structp png_ptr, png_infop info_ptr, - png_int_32 offset_x, png_int_32 offset_y, int unit_type)); +PNG_EXPORT(146, void, png_set_oFFs, (png_const_structrp png_ptr, + png_inforp info_ptr, png_int_32 offset_x, png_int_32 offset_y, + int unit_type)); #endif #ifdef PNG_pCAL_SUPPORTED -PNG_EXPORT(147, png_uint_32, png_get_pCAL, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, - int *nparams, - png_charp *units, png_charpp *params)); +PNG_EXPORT(147, png_uint_32, png_get_pCAL, (png_const_structrp png_ptr, + png_inforp info_ptr, png_charp *purpose, png_int_32 *X0, + png_int_32 *X1, int *type, int *nparams, png_charp *units, + png_charpp *params)); #endif #ifdef PNG_pCAL_SUPPORTED -PNG_EXPORT(148, void, png_set_pCAL, (png_structp png_ptr, - png_infop info_ptr, - png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type, - int nparams, png_const_charp units, png_charpp params)); +PNG_EXPORT(148, void, png_set_pCAL, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_charp purpose, png_int_32 X0, png_int_32 X1, + int type, int nparams, png_const_charp units, png_charpp params)); #endif #ifdef PNG_pHYs_SUPPORTED -PNG_EXPORT(149, png_uint_32, png_get_pHYs, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)); +PNG_EXPORT(149, png_uint_32, png_get_pHYs, (png_const_structrp png_ptr, + png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, + int *unit_type)); #endif #ifdef PNG_pHYs_SUPPORTED -PNG_EXPORT(150, void, png_set_pHYs, - (png_structp png_ptr, png_infop info_ptr, - png_uint_32 res_x, png_uint_32 res_y, int unit_type)); +PNG_EXPORT(150, void, png_set_pHYs, (png_const_structrp png_ptr, + png_inforp info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)); #endif -PNG_EXPORT(151, png_uint_32, png_get_PLTE, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_colorp *palette, int *num_palette)); +PNG_EXPORT(151, png_uint_32, png_get_PLTE, (png_const_structrp png_ptr, + png_inforp info_ptr, png_colorp *palette, int *num_palette)); -PNG_EXPORT(152, void, png_set_PLTE, - (png_structp png_ptr, png_infop info_ptr, - png_const_colorp palette, int num_palette)); +PNG_EXPORT(152, void, png_set_PLTE, (png_structrp png_ptr, + png_inforp info_ptr, png_const_colorp palette, int num_palette)); #ifdef PNG_sBIT_SUPPORTED -PNG_EXPORT(153, png_uint_32, png_get_sBIT, - (png_const_structp png_ptr, png_infop info_ptr, - png_color_8p *sig_bit)); +PNG_EXPORT(153, png_uint_32, png_get_sBIT, (png_const_structrp png_ptr, + png_inforp info_ptr, png_color_8p *sig_bit)); #endif #ifdef PNG_sBIT_SUPPORTED -PNG_EXPORT(154, void, png_set_sBIT, - (png_structp png_ptr, png_infop info_ptr, png_const_color_8p sig_bit)); +PNG_EXPORT(154, void, png_set_sBIT, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_color_8p sig_bit)); #endif #ifdef PNG_sRGB_SUPPORTED -PNG_EXPORT(155, png_uint_32, png_get_sRGB, (png_const_structp png_ptr, - png_const_infop info_ptr, int *file_srgb_intent)); +PNG_EXPORT(155, png_uint_32, png_get_sRGB, (png_const_structrp png_ptr, + png_const_inforp info_ptr, int *file_srgb_intent)); #endif #ifdef PNG_sRGB_SUPPORTED -PNG_EXPORT(156, void, png_set_sRGB, - (png_structp png_ptr, png_infop info_ptr, int srgb_intent)); -PNG_EXPORT(157, void, png_set_sRGB_gAMA_and_cHRM, (png_structp png_ptr, - png_infop info_ptr, int srgb_intent)); +PNG_EXPORT(156, void, png_set_sRGB, (png_const_structrp png_ptr, + png_inforp info_ptr, int srgb_intent)); +PNG_EXPORT(157, void, png_set_sRGB_gAMA_and_cHRM, (png_const_structrp png_ptr, + png_inforp info_ptr, int srgb_intent)); #endif #ifdef PNG_iCCP_SUPPORTED -PNG_EXPORT(158, png_uint_32, png_get_iCCP, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_charpp name, int *compression_type, png_bytepp profile, - png_uint_32 *proflen)); +PNG_EXPORT(158, png_uint_32, png_get_iCCP, (png_const_structrp png_ptr, + png_inforp info_ptr, png_charpp name, int *compression_type, + png_bytepp profile, png_uint_32 *proflen)); #endif #ifdef PNG_iCCP_SUPPORTED -PNG_EXPORT(159, void, png_set_iCCP, - (png_structp png_ptr, png_infop info_ptr, - png_const_charp name, int compression_type, png_const_bytep profile, - png_uint_32 proflen)); +PNG_EXPORT(159, void, png_set_iCCP, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_charp name, int compression_type, + png_const_bytep profile, png_uint_32 proflen)); #endif #ifdef PNG_sPLT_SUPPORTED -PNG_EXPORT(160, png_uint_32, png_get_sPLT, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_sPLT_tpp entries)); +PNG_EXPORT(160, int, png_get_sPLT, (png_const_structrp png_ptr, + png_inforp info_ptr, png_sPLT_tpp entries)); #endif #ifdef PNG_sPLT_SUPPORTED -PNG_EXPORT(161, void, png_set_sPLT, - (png_structp png_ptr, png_infop info_ptr, - png_const_sPLT_tp entries, int nentries)); +PNG_EXPORT(161, void, png_set_sPLT, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)); #endif #ifdef PNG_TEXT_SUPPORTED /* png_get_text also returns the number of text chunks in *num_text */ -PNG_EXPORT(162, png_uint_32, png_get_text, - (png_const_structp png_ptr, png_const_infop info_ptr, - png_textp *text_ptr, int *num_text)); +PNG_EXPORT(162, int, png_get_text, (png_const_structrp png_ptr, + png_inforp info_ptr, png_textp *text_ptr, int *num_text)); #endif /* Note while png_set_text() will accept a structure whose text, @@ -2282,122 +2117,220 @@ PNG_EXPORT(162, png_uint_32, png_get_text, */ #ifdef PNG_TEXT_SUPPORTED -PNG_EXPORT(163, void, png_set_text, - (png_structp png_ptr, png_infop info_ptr, - png_const_textp text_ptr, int num_text)); +PNG_EXPORT(163, void, png_set_text, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_textp text_ptr, int num_text)); #endif #ifdef PNG_tIME_SUPPORTED -PNG_EXPORT(164, png_uint_32, png_get_tIME, - (png_const_structp png_ptr, png_infop info_ptr, png_timep *mod_time)); +PNG_EXPORT(164, png_uint_32, png_get_tIME, (png_const_structrp png_ptr, + png_inforp info_ptr, png_timep *mod_time)); #endif #ifdef PNG_tIME_SUPPORTED -PNG_EXPORT(165, void, png_set_tIME, - (png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time)); +PNG_EXPORT(165, void, png_set_tIME, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_timep mod_time)); #endif #ifdef PNG_tRNS_SUPPORTED -PNG_EXPORT(166, png_uint_32, png_get_tRNS, - (png_const_structp png_ptr, png_infop info_ptr, - png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)); +PNG_EXPORT(166, png_uint_32, png_get_tRNS, (png_const_structrp png_ptr, + png_inforp info_ptr, png_bytep *trans_alpha, int *num_trans, + png_color_16p *trans_color)); #endif #ifdef PNG_tRNS_SUPPORTED -PNG_EXPORT(167, void, png_set_tRNS, - (png_structp png_ptr, png_infop info_ptr, - png_const_bytep trans_alpha, int num_trans, +PNG_EXPORT(167, void, png_set_tRNS, (png_structrp png_ptr, + png_inforp info_ptr, png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)); #endif #ifdef PNG_sCAL_SUPPORTED -PNG_FP_EXPORT(168, png_uint_32, png_get_sCAL, - (png_const_structp png_ptr, png_const_infop info_ptr, - int *unit, double *width, double *height)); -#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED +PNG_FP_EXPORT(168, png_uint_32, png_get_sCAL, (png_const_structrp png_ptr, + png_const_inforp info_ptr, int *unit, double *width, double *height)) +#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \ + defined(PNG_FLOATING_POINT_SUPPORTED) /* NOTE: this API is currently implemented using floating point arithmetic, * consequently it can only be used on systems with floating point support. * In any case the range of values supported by png_fixed_point is small and it * is highly recommended that png_get_sCAL_s be used instead. */ PNG_FIXED_EXPORT(214, png_uint_32, png_get_sCAL_fixed, - (png_structp png_ptr, png_const_infop info_ptr, int *unit, - png_fixed_point *width, - png_fixed_point *height)); + (png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, + png_fixed_point *width, png_fixed_point *height)) #endif PNG_EXPORT(169, png_uint_32, png_get_sCAL_s, - (png_const_structp png_ptr, png_const_infop info_ptr, - int *unit, png_charpp swidth, png_charpp sheight)); + (png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, + png_charpp swidth, png_charpp sheight)); -PNG_FP_EXPORT(170, void, png_set_sCAL, - (png_structp png_ptr, png_infop info_ptr, - int unit, double width, double height)); -PNG_FIXED_EXPORT(213, void, png_set_sCAL_fixed, (png_structp png_ptr, - png_infop info_ptr, int unit, png_fixed_point width, - png_fixed_point height)); -PNG_EXPORT(171, void, png_set_sCAL_s, - (png_structp png_ptr, png_infop info_ptr, - int unit, png_const_charp swidth, png_const_charp sheight)); -#endif /* PNG_sCAL_SUPPORTED */ +PNG_FP_EXPORT(170, void, png_set_sCAL, (png_const_structrp png_ptr, + png_inforp info_ptr, int unit, double width, double height)) +PNG_FIXED_EXPORT(213, void, png_set_sCAL_fixed, (png_const_structrp png_ptr, + png_inforp info_ptr, int unit, png_fixed_point width, + png_fixed_point height)) +PNG_EXPORT(171, void, png_set_sCAL_s, (png_const_structrp png_ptr, + png_inforp info_ptr, int unit, + png_const_charp swidth, png_const_charp sheight)); +#endif /* sCAL */ -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED -/* Provide a list of chunks and how they are to be handled, if the built-in - handling or default unknown chunk handling is not desired. Any chunks not - listed will be handled in the default manner. The IHDR and IEND chunks - must not be listed. Because this turns off the default handling for chunks - that would otherwise be recognized the behavior of libpng transformations may - well become incorrect! - keep = 0: PNG_HANDLE_CHUNK_AS_DEFAULT: follow default behavior - = 1: PNG_HANDLE_CHUNK_NEVER: do not keep - = 2: PNG_HANDLE_CHUNK_IF_SAFE: keep only if safe-to-copy - = 3: PNG_HANDLE_CHUNK_ALWAYS: keep even if unsafe-to-copy -*/ -PNG_EXPORT(172, void, png_set_keep_unknown_chunks, - (png_structp png_ptr, int keep, - png_const_bytep chunk_list, int num_chunks)); - -/* The handling code is returned; the result is therefore true (non-zero) if - * special handling is required, false for the default handling. +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED +/* Provide the default handling for all unknown chunks or, optionally, for + * specific unknown chunks. + * + * NOTE: prior to 1.6.0 the handling specified for particular chunks on read was + * ignored and the default was used, the per-chunk setting only had an effect on + * write. If you wish to have chunk-specific handling on read in code that must + * work on earlier versions you must use a user chunk callback to specify the + * desired handling (keep or discard.) + * + * The 'keep' parameter is a PNG_HANDLE_CHUNK_ value as listed below. The + * parameter is interpreted as follows: + * + * READ: + * PNG_HANDLE_CHUNK_AS_DEFAULT: + * Known chunks: do normal libpng processing, do not keep the chunk (but + * see the comments below about PNG_HANDLE_AS_UNKNOWN_SUPPORTED) + * Unknown chunks: for a specific chunk use the global default, when used + * as the default discard the chunk data. + * PNG_HANDLE_CHUNK_NEVER: + * Discard the chunk data. + * PNG_HANDLE_CHUNK_IF_SAFE: + * Keep the chunk data if the chunk is not critical else raise a chunk + * error. + * PNG_HANDLE_CHUNK_ALWAYS: + * Keep the chunk data. + * + * If the chunk data is saved it can be retrieved using png_get_unknown_chunks, + * below. Notice that specifying "AS_DEFAULT" as a global default is equivalent + * to specifying "NEVER", however when "AS_DEFAULT" is used for specific chunks + * it simply resets the behavior to the libpng default. + * + * INTERACTION WTIH USER CHUNK CALLBACKS: + * The per-chunk handling is always used when there is a png_user_chunk_ptr + * callback and the callback returns 0; the chunk is then always stored *unless* + * it is critical and the per-chunk setting is other than ALWAYS. Notice that + * the global default is *not* used in this case. (In effect the per-chunk + * value is incremented to at least IF_SAFE.) + * + * IMPORTANT NOTE: this behavior will change in libpng 1.7 - the global and + * per-chunk defaults will be honored. If you want to preserve the current + * behavior when your callback returns 0 you must set PNG_HANDLE_CHUNK_IF_SAFE + * as the default - if you don't do this libpng 1.6 will issue a warning. + * + * If you want unhandled unknown chunks to be discarded in libpng 1.6 and + * earlier simply return '1' (handled). + * + * PNG_HANDLE_AS_UNKNOWN_SUPPORTED: + * If this is *not* set known chunks will always be handled by libpng and + * will never be stored in the unknown chunk list. Known chunks listed to + * png_set_keep_unknown_chunks will have no effect. If it is set then known + * chunks listed with a keep other than AS_DEFAULT will *never* be processed + * by libpng, in addition critical chunks must either be processed by the + * callback or saved. + * + * The IHDR and IEND chunks must not be listed. Because this turns off the + * default handling for chunks that would otherwise be recognized the + * behavior of libpng transformations may well become incorrect! + * + * WRITE: + * When writing chunks the options only apply to the chunks specified by + * png_set_unknown_chunks (below), libpng will *always* write known chunks + * required by png_set_ calls and will always write the core critical chunks + * (as required for PLTE). + * + * Each chunk in the png_set_unknown_chunks list is looked up in the + * png_set_keep_unknown_chunks list to find the keep setting, this is then + * interpreted as follows: + * + * PNG_HANDLE_CHUNK_AS_DEFAULT: + * Write safe-to-copy chunks and write other chunks if the global + * default is set to _ALWAYS, otherwise don't write this chunk. + * PNG_HANDLE_CHUNK_NEVER: + * Do not write the chunk. + * PNG_HANDLE_CHUNK_IF_SAFE: + * Write the chunk if it is safe-to-copy, otherwise do not write it. + * PNG_HANDLE_CHUNK_ALWAYS: + * Write the chunk. + * + * Note that the default behavior is effectively the opposite of the read case - + * in read unknown chunks are not stored by default, in write they are written + * by default. Also the behavior of PNG_HANDLE_CHUNK_IF_SAFE is very different + * - on write the safe-to-copy bit is checked, on read the critical bit is + * checked and on read if the chunk is critical an error will be raised. + * + * num_chunks: + * =========== + * If num_chunks is positive, then the "keep" parameter specifies the manner + * for handling only those chunks appearing in the chunk_list array, + * otherwise the chunk list array is ignored. + * + * If num_chunks is 0 the "keep" parameter specifies the default behavior for + * unknown chunks, as described above. + * + * If num_chunks is negative, then the "keep" parameter specifies the manner + * for handling all unknown chunks plus all chunks recognized by libpng + * except for the IHDR, PLTE, tRNS, IDAT, and IEND chunks (which continue to + * be processed by libpng. */ -PNG_EXPORT(173, int, png_handle_as_unknown, (png_structp png_ptr, +PNG_EXPORT(172, void, png_set_keep_unknown_chunks, (png_structrp png_ptr, + int keep, png_const_bytep chunk_list, int num_chunks)); + +/* The "keep" PNG_HANDLE_CHUNK_ parameter for the specified chunk is returned; + * the result is therefore true (non-zero) if special handling is required, + * false for the default handling. + */ +PNG_EXPORT(173, int, png_handle_as_unknown, (png_const_structrp png_ptr, png_const_bytep chunk_name)); #endif -#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED -PNG_EXPORT(174, void, png_set_unknown_chunks, (png_structp png_ptr, - png_infop info_ptr, png_const_unknown_chunkp unknowns, + +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED +PNG_EXPORT(174, void, png_set_unknown_chunks, (png_const_structrp png_ptr, + png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)); + /* NOTE: prior to 1.6.0 this routine set the 'location' field of the added + * unknowns to the location currently stored in the png_struct. This is + * invariably the wrong value on write. To fix this call the following API + * for each chunk in the list with the correct location. If you know your + * code won't be compiled on earlier versions you can rely on + * png_set_unknown_chunks(write-ptr, png_get_unknown_chunks(read-ptr)) doing + * the correct thing. + */ + PNG_EXPORT(175, void, png_set_unknown_chunk_location, - (png_structp png_ptr, png_infop info_ptr, int chunk, int location)); -PNG_EXPORT(176, int, png_get_unknown_chunks, (png_const_structp png_ptr, - png_const_infop info_ptr, png_unknown_chunkpp entries)); + (png_const_structrp png_ptr, png_inforp info_ptr, int chunk, int location)); + +PNG_EXPORT(176, int, png_get_unknown_chunks, (png_const_structrp png_ptr, + png_inforp info_ptr, png_unknown_chunkpp entries)); #endif /* Png_free_data() will turn off the "valid" flag for anything it frees. * If you need to turn it off for a chunk that your application has freed, * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); */ -PNG_EXPORT(177, void, png_set_invalid, - (png_structp png_ptr, png_infop info_ptr, int mask)); +PNG_EXPORT(177, void, png_set_invalid, (png_const_structrp png_ptr, + png_inforp info_ptr, int mask)); #ifdef PNG_INFO_IMAGE_SUPPORTED /* The "params" pointer is currently not used and is for future expansion. */ -PNG_EXPORT(178, void, png_read_png, (png_structp png_ptr, png_infop info_ptr, +#ifdef PNG_SEQUENTIAL_READ_SUPPORTED +PNG_EXPORT(178, void, png_read_png, (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params)); -PNG_EXPORT(179, void, png_write_png, (png_structp png_ptr, png_infop info_ptr, +#endif +#ifdef PNG_WRITE_SUPPORTED +PNG_EXPORT(179, void, png_write_png, (png_structrp png_ptr, png_inforp info_ptr, int transforms, png_voidp params)); #endif +#endif PNG_EXPORT(180, png_const_charp, png_get_copyright, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); PNG_EXPORT(181, png_const_charp, png_get_header_ver, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); PNG_EXPORT(182, png_const_charp, png_get_header_version, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); PNG_EXPORT(183, png_const_charp, png_get_libpng_ver, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); #ifdef PNG_MNG_FEATURES_SUPPORTED -PNG_EXPORT(184, png_uint_32, png_permit_mng_features, (png_structp png_ptr, +PNG_EXPORT(184, png_uint_32, png_permit_mng_features, (png_structrp png_ptr, png_uint_32 mng_features_permitted)); #endif @@ -2406,75 +2339,77 @@ PNG_EXPORT(184, png_uint_32, png_permit_mng_features, (png_structp png_ptr, #define PNG_HANDLE_CHUNK_NEVER 1 #define PNG_HANDLE_CHUNK_IF_SAFE 2 #define PNG_HANDLE_CHUNK_ALWAYS 3 +#define PNG_HANDLE_CHUNK_LAST 4 /* Strip the prepended error numbers ("#nnn ") from error and warning * messages before passing them to the error or warning handler. */ #ifdef PNG_ERROR_NUMBERS_SUPPORTED -PNG_EXPORT(185, void, png_set_strip_error_numbers, - (png_structp png_ptr, +PNG_EXPORT(185, void, png_set_strip_error_numbers, (png_structrp png_ptr, png_uint_32 strip_mode)); #endif /* Added in libpng-1.2.6 */ #ifdef PNG_SET_USER_LIMITS_SUPPORTED -PNG_EXPORT(186, void, png_set_user_limits, (png_structp png_ptr, +PNG_EXPORT(186, void, png_set_user_limits, (png_structrp png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max)); PNG_EXPORT(187, png_uint_32, png_get_user_width_max, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); PNG_EXPORT(188, png_uint_32, png_get_user_height_max, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); /* Added in libpng-1.4.0 */ -PNG_EXPORT(189, void, png_set_chunk_cache_max, (png_structp png_ptr, +PNG_EXPORT(189, void, png_set_chunk_cache_max, (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)); PNG_EXPORT(190, png_uint_32, png_get_chunk_cache_max, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); /* Added in libpng-1.4.1 */ -PNG_EXPORT(191, void, png_set_chunk_malloc_max, (png_structp png_ptr, +PNG_EXPORT(191, void, png_set_chunk_malloc_max, (png_structrp png_ptr, png_alloc_size_t user_chunk_cache_max)); PNG_EXPORT(192, png_alloc_size_t, png_get_chunk_malloc_max, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); #endif #if defined(PNG_INCH_CONVERSIONS_SUPPORTED) PNG_EXPORT(193, png_uint_32, png_get_pixels_per_inch, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(194, png_uint_32, png_get_x_pixels_per_inch, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_EXPORT(195, png_uint_32, png_get_y_pixels_per_inch, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)); PNG_FP_EXPORT(196, float, png_get_x_offset_inches, - (png_const_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)) #ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */ PNG_FIXED_EXPORT(211, png_fixed_point, png_get_x_offset_inches_fixed, - (png_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)) #endif -PNG_FP_EXPORT(197, float, png_get_y_offset_inches, (png_const_structp png_ptr, - png_const_infop info_ptr)); +PNG_FP_EXPORT(197, float, png_get_y_offset_inches, (png_const_structrp png_ptr, + png_const_inforp info_ptr)) #ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */ PNG_FIXED_EXPORT(212, png_fixed_point, png_get_y_offset_inches_fixed, - (png_structp png_ptr, png_const_infop info_ptr)); + (png_const_structrp png_ptr, png_const_inforp info_ptr)) #endif # ifdef PNG_pHYs_SUPPORTED -PNG_EXPORT(198, png_uint_32, png_get_pHYs_dpi, (png_const_structp png_ptr, - png_const_infop info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, +PNG_EXPORT(198, png_uint_32, png_get_pHYs_dpi, (png_const_structrp png_ptr, + png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)); -# endif /* PNG_pHYs_SUPPORTED */ -#endif /* PNG_INCH_CONVERSIONS_SUPPORTED */ +# endif /* pHYs */ +#endif /* INCH_CONVERSIONS */ /* Added in libpng-1.4.0 */ #ifdef PNG_IO_STATE_SUPPORTED -PNG_EXPORT(199, png_uint_32, png_get_io_state, (png_structp png_ptr)); +PNG_EXPORT(199, png_uint_32, png_get_io_state, (png_const_structrp png_ptr)); + +/* Removed from libpng 1.6; use png_get_io_chunk_type. */ +PNG_REMOVED(200, png_const_bytep, png_get_io_chunk_name, (png_structrp png_ptr), + PNG_DEPRECATED) -PNG_EXPORTA(200, png_const_bytep, png_get_io_chunk_name, - (png_structp png_ptr), PNG_DEPRECATED); PNG_EXPORT(216, png_uint_32, png_get_io_chunk_type, - (png_const_structp png_ptr)); + (png_const_structrp png_ptr)); /* The flags returned by png_get_io_state() are the following: */ # define PNG_IO_NONE 0x0000 /* no I/O at this moment */ @@ -2486,7 +2421,7 @@ PNG_EXPORT(216, png_uint_32, png_get_io_chunk_type, # define PNG_IO_CHUNK_CRC 0x0080 /* currently at the chunk crc */ # define PNG_IO_MASK_OP 0x000f /* current operation: reading/writing */ # define PNG_IO_MASK_LOC 0x00f0 /* current location: sig/hdr/data/crc */ -#endif /* ?PNG_IO_STATE_SUPPORTED */ +#endif /* IO_STATE */ /* Interlace support. The following macros are always defined so that if * libpng interlace handling is turned off the macros may be used to handle @@ -2530,10 +2465,10 @@ PNG_EXPORT(216, png_uint_32, png_get_io_chunk_type, * necessary to find the row in the output image given a row in an interlaced * image, so two more macros: */ -#define PNG_ROW_FROM_PASS_ROW(yIn, pass) \ - (((yIn)<> 8)) >> 8); } + (composite) = (png_byte)(((temp + (temp >> 8)) >> 8) & 0xff); } # define png_composite_16(composite, fg, alpha, bg) \ { png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) \ * (png_uint_32)(alpha) \ + (png_uint_32)(bg)*(65535 \ - (png_uint_32)(alpha)) + 32768); \ - (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); } + (composite) = (png_uint_16)(0xffff & ((temp + (temp >> 16)) >> 16)); } #else /* Standard method using integer division */ -# define png_composite(composite, fg, alpha, bg) \ - (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) + \ - (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \ - 127) / 255) +# define png_composite(composite, fg, alpha, bg) \ + (composite) = \ + (png_byte)(0xff & (((png_uint_16)(fg) * (png_uint_16)(alpha) + \ + (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \ + 127) / 255)) # define png_composite_16(composite, fg, alpha, bg) \ - (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \ - (png_uint_32)(bg)*(png_uint_32)(65535 - (png_uint_32)(alpha)) + \ - 32767) / 65535) -#endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */ + (composite) = \ + (png_uint_16)(0xffff & (((png_uint_32)(fg) * (png_uint_32)(alpha) + \ + (png_uint_32)(bg)*(png_uint_32)(65535 - (png_uint_32)(alpha)) + \ + 32767) / 65535)) +#endif /* READ_COMPOSITE_NODIV */ #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED PNG_EXPORT(201, png_uint_32, png_get_uint_32, (png_const_bytep buf)); @@ -2598,7 +2535,7 @@ PNG_EXPORT(202, png_uint_16, png_get_uint_16, (png_const_bytep buf)); PNG_EXPORT(203, png_int_32, png_get_int_32, (png_const_bytep buf)); #endif -PNG_EXPORT(204, png_uint_32, png_get_uint_31, (png_structp png_ptr, +PNG_EXPORT(204, png_uint_32, png_get_uint_31, (png_const_structrp png_ptr, png_const_bytep buf)); /* No png_get_int_16 -- may be added if there's a real need for it. */ @@ -2624,7 +2561,7 @@ PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i)); * The png_get_int_32() routine assumes we are using two's complement * format for negative values, which is almost certainly true. */ -# define png_get_uint_32(buf) \ +# define PNG_get_uint_32(buf) \ (((png_uint_32)(*(buf)) << 24) + \ ((png_uint_32)(*((buf) + 1)) << 16) + \ ((png_uint_32)(*((buf) + 2)) << 8) + \ @@ -2633,33 +2570,554 @@ PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i)); /* From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the * function) incorrectly returned a value of type png_uint_32. */ -# define png_get_uint_16(buf) \ +# define PNG_get_uint_16(buf) \ ((png_uint_16) \ (((unsigned int)(*(buf)) << 8) + \ ((unsigned int)(*((buf) + 1))))) -# define png_get_int_32(buf) \ +# define PNG_get_int_32(buf) \ ((png_int_32)((*(buf) & 0x80) \ - ? -((png_int_32)((png_get_uint_32(buf) ^ 0xffffffffL) + 1)) \ + ? -((png_int_32)(((png_get_uint_32(buf)^0xffffffffU)+1U)&0x7fffffffU)) \ : (png_int_32)png_get_uint_32(buf))) + + /* If PNG_PREFIX is defined the same thing as below happens in pnglibconf.h, + * but defining a macro name prefixed with PNG_PREFIX. + */ +# ifndef PNG_PREFIX +# define png_get_uint_32(buf) PNG_get_uint_32(buf) +# define png_get_uint_16(buf) PNG_get_uint_16(buf) +# define png_get_int_32(buf) PNG_get_int_32(buf) +# endif +#else +# ifdef PNG_PREFIX + /* No macros; revert to the (redefined) function */ +# define PNG_get_uint_32 (png_get_uint_32) +# define PNG_get_uint_16 (png_get_uint_16) +# define PNG_get_int_32 (png_get_int_32) +# endif #endif -#if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \ - defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED) -PNG_EXPORT(234, void, png_set_check_for_invalid_index, (png_structp png_ptr, - int allowed)); +#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED +PNG_EXPORT(242, void, png_set_check_for_invalid_index, + (png_structrp png_ptr, int allowed)); +# ifdef PNG_GET_PALETTE_MAX_SUPPORTED +PNG_EXPORT(243, int, png_get_palette_max, (png_const_structp png_ptr, + png_const_infop info_ptr)); +# endif +#endif /* CHECK_FOR_INVALID_INDEX */ + +/******************************************************************************* + * Section 5: SIMPLIFIED API + ******************************************************************************* + * + * Please read the documentation in libpng-manual.txt (TODO: write said + * documentation) if you don't understand what follows. + * + * The simplified API hides the details of both libpng and the PNG file format + * itself. It allows PNG files to be read into a very limited number of + * in-memory bitmap formats or to be written from the same formats. If these + * formats do not accomodate your needs then you can, and should, use the more + * sophisticated APIs above - these support a wide variety of in-memory formats + * and a wide variety of sophisticated transformations to those formats as well + * as a wide variety of APIs to manipulate ancillary information. + * + * To read a PNG file using the simplified API: + * + * 1) Declare a 'png_image' structure (see below) on the stack, set the + * version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL + * (this is REQUIRED, your program may crash if you don't do it.) + * 2) Call the appropriate png_image_begin_read... function. + * 3) Set the png_image 'format' member to the required sample format. + * 4) Allocate a buffer for the image and, if required, the color-map. + * 5) Call png_image_finish_read to read the image and, if required, the + * color-map into your buffers. + * + * There are no restrictions on the format of the PNG input itself; all valid + * color types, bit depths, and interlace methods are acceptable, and the + * input image is transformed as necessary to the requested in-memory format + * during the png_image_finish_read() step. The only caveat is that if you + * request a color-mapped image from a PNG that is full-color or makes + * complex use of an alpha channel the transformation is extremely lossy and the + * result may look terrible. + * + * To write a PNG file using the simplified API: + * + * 1) Declare a 'png_image' structure on the stack and memset() it to all zero. + * 2) Initialize the members of the structure that describe the image, setting + * the 'format' member to the format of the image samples. + * 3) Call the appropriate png_image_write... function with a pointer to the + * image and, if necessary, the color-map to write the PNG data. + * + * png_image is a structure that describes the in-memory format of an image + * when it is being read or defines the in-memory format of an image that you + * need to write: + */ +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) || \ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) + +#define PNG_IMAGE_VERSION 1 + +typedef struct png_control *png_controlp; +typedef struct +{ + png_controlp opaque; /* Initialize to NULL, free with png_image_free */ + png_uint_32 version; /* Set to PNG_IMAGE_VERSION */ + png_uint_32 width; /* Image width in pixels (columns) */ + png_uint_32 height; /* Image height in pixels (rows) */ + png_uint_32 format; /* Image format as defined below */ + png_uint_32 flags; /* A bit mask containing informational flags */ + png_uint_32 colormap_entries; + /* Number of entries in the color-map */ + + /* In the event of an error or warning the following field will be set to a + * non-zero value and the 'message' field will contain a '\0' terminated + * string with the libpng error or warning message. If both warnings and + * an error were encountered, only the error is recorded. If there + * are multiple warnings, only the first one is recorded. + * + * The upper 30 bits of this value are reserved, the low two bits contain + * a value as follows: + */ +# define PNG_IMAGE_WARNING 1 +# define PNG_IMAGE_ERROR 2 + /* + * The result is a two-bit code such that a value more than 1 indicates + * a failure in the API just called: + * + * 0 - no warning or error + * 1 - warning + * 2 - error + * 3 - error preceded by warning + */ +# define PNG_IMAGE_FAILED(png_cntrl) ((((png_cntrl).warning_or_error)&0x03)>1) + + png_uint_32 warning_or_error; + + char message[64]; +} png_image, *png_imagep; + +/* The samples of the image have one to four channels whose components have + * original values in the range 0 to 1.0: + * + * 1: A single gray or luminance channel (G). + * 2: A gray/luminance channel and an alpha channel (GA). + * 3: Three red, green, blue color channels (RGB). + * 4: Three color channels and an alpha channel (RGBA). + * + * The components are encoded in one of two ways: + * + * a) As a small integer, value 0..255, contained in a single byte. For the + * alpha channel the original value is simply value/255. For the color or + * luminance channels the value is encoded according to the sRGB specification + * and matches the 8-bit format expected by typical display devices. + * + * The color/gray channels are not scaled (pre-multiplied) by the alpha + * channel and are suitable for passing to color management software. + * + * b) As a value in the range 0..65535, contained in a 2-byte integer. All + * channels can be converted to the original value by dividing by 65535; all + * channels are linear. Color channels use the RGB encoding (RGB end-points) of + * the sRGB specification. This encoding is identified by the + * PNG_FORMAT_FLAG_LINEAR flag below. + * + * When the simplified API needs to convert between sRGB and linear colorspaces, + * the actual sRGB transfer curve defined in the sRGB specification (see the + * article at http://en.wikipedia.org/wiki/SRGB) is used, not the gamma=1/2.2 + * approximation used elsewhere in libpng. + * + * When an alpha channel is present it is expected to denote pixel coverage + * of the color or luminance channels and is returned as an associated alpha + * channel: the color/gray channels are scaled (pre-multiplied) by the alpha + * value. + * + * The samples are either contained directly in the image data, between 1 and 8 + * bytes per pixel according to the encoding, or are held in a color-map indexed + * by bytes in the image data. In the case of a color-map the color-map entries + * are individual samples, encoded as above, and the image data has one byte per + * pixel to select the relevant sample from the color-map. + */ + +/* PNG_FORMAT_* + * + * #defines to be used in png_image::format. Each #define identifies a + * particular layout of sample data and, if present, alpha values. There are + * separate defines for each of the two component encodings. + * + * A format is built up using single bit flag values. All combinations are + * valid. Formats can be built up from the flag values or you can use one of + * the predefined values below. When testing formats always use the FORMAT_FLAG + * macros to test for individual features - future versions of the library may + * add new flags. + * + * When reading or writing color-mapped images the format should be set to the + * format of the entries in the color-map then png_image_{read,write}_colormap + * called to read or write the color-map and set the format correctly for the + * image data. Do not set the PNG_FORMAT_FLAG_COLORMAP bit directly! + * + * NOTE: libpng can be built with particular features disabled. If you see + * compiler errors because the definition of one of the following flags has been + * compiled out it is because libpng does not have the required support. It is + * possible, however, for the libpng configuration to enable the format on just + * read or just write; in that case you may see an error at run time. You can + * guard against this by checking for the definition of the appropriate + * "_SUPPORTED" macro, one of: + * + * PNG_SIMPLIFIED_{READ,WRITE}_{BGR,AFIRST}_SUPPORTED + */ +#define PNG_FORMAT_FLAG_ALPHA 0x01U /* format with an alpha channel */ +#define PNG_FORMAT_FLAG_COLOR 0x02U /* color format: otherwise grayscale */ +#define PNG_FORMAT_FLAG_LINEAR 0x04U /* 2-byte channels else 1-byte */ +#define PNG_FORMAT_FLAG_COLORMAP 0x08U /* image data is color-mapped */ + +#ifdef PNG_FORMAT_BGR_SUPPORTED +# define PNG_FORMAT_FLAG_BGR 0x10U /* BGR colors, else order is RGB */ #endif -/* Maintainer: Put new public prototypes here ^, in libpng.3, and project - * defs +#ifdef PNG_FORMAT_AFIRST_SUPPORTED +# define PNG_FORMAT_FLAG_AFIRST 0x20U /* alpha channel comes first */ +#endif + +/* Commonly used formats have predefined macros. + * + * First the single byte (sRGB) formats: + */ +#define PNG_FORMAT_GRAY 0 +#define PNG_FORMAT_GA PNG_FORMAT_FLAG_ALPHA +#define PNG_FORMAT_AG (PNG_FORMAT_GA|PNG_FORMAT_FLAG_AFIRST) +#define PNG_FORMAT_RGB PNG_FORMAT_FLAG_COLOR +#define PNG_FORMAT_BGR (PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_BGR) +#define PNG_FORMAT_RGBA (PNG_FORMAT_RGB|PNG_FORMAT_FLAG_ALPHA) +#define PNG_FORMAT_ARGB (PNG_FORMAT_RGBA|PNG_FORMAT_FLAG_AFIRST) +#define PNG_FORMAT_BGRA (PNG_FORMAT_BGR|PNG_FORMAT_FLAG_ALPHA) +#define PNG_FORMAT_ABGR (PNG_FORMAT_BGRA|PNG_FORMAT_FLAG_AFIRST) + +/* Then the linear 2-byte formats. When naming these "Y" is used to + * indicate a luminance (gray) channel. + */ +#define PNG_FORMAT_LINEAR_Y PNG_FORMAT_FLAG_LINEAR +#define PNG_FORMAT_LINEAR_Y_ALPHA (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_ALPHA) +#define PNG_FORMAT_LINEAR_RGB (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_COLOR) +#define PNG_FORMAT_LINEAR_RGB_ALPHA \ + (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_ALPHA) + +/* With color-mapped formats the image data is one byte for each pixel, the byte + * is an index into the color-map which is formatted as above. To obtain a + * color-mapped format it is sufficient just to add the PNG_FOMAT_FLAG_COLORMAP + * to one of the above definitions, or you can use one of the definitions below. + */ +#define PNG_FORMAT_RGB_COLORMAP (PNG_FORMAT_RGB|PNG_FORMAT_FLAG_COLORMAP) +#define PNG_FORMAT_BGR_COLORMAP (PNG_FORMAT_BGR|PNG_FORMAT_FLAG_COLORMAP) +#define PNG_FORMAT_RGBA_COLORMAP (PNG_FORMAT_RGBA|PNG_FORMAT_FLAG_COLORMAP) +#define PNG_FORMAT_ARGB_COLORMAP (PNG_FORMAT_ARGB|PNG_FORMAT_FLAG_COLORMAP) +#define PNG_FORMAT_BGRA_COLORMAP (PNG_FORMAT_BGRA|PNG_FORMAT_FLAG_COLORMAP) +#define PNG_FORMAT_ABGR_COLORMAP (PNG_FORMAT_ABGR|PNG_FORMAT_FLAG_COLORMAP) + +/* PNG_IMAGE macros + * + * These are convenience macros to derive information from a png_image + * structure. The PNG_IMAGE_SAMPLE_ macros return values appropriate to the + * actual image sample values - either the entries in the color-map or the + * pixels in the image. The PNG_IMAGE_PIXEL_ macros return corresponding values + * for the pixels and will always return 1 for color-mapped formats. The + * remaining macros return information about the rows in the image and the + * complete image. + * + * NOTE: All the macros that take a png_image::format parameter are compile time + * constants if the format parameter is, itself, a constant. Therefore these + * macros can be used in array declarations and case labels where required. + * Similarly the macros are also pre-processor constants (sizeof is not used) so + * they can be used in #if tests. + * + * First the information about the samples. + */ +#define PNG_IMAGE_SAMPLE_CHANNELS(fmt)\ + (((fmt)&(PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_ALPHA))+1) + /* Return the total number of channels in a given format: 1..4 */ + +#define PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt)\ + ((((fmt) & PNG_FORMAT_FLAG_LINEAR) >> 2)+1) + /* Return the size in bytes of a single component of a pixel or color-map + * entry (as appropriate) in the image: 1 or 2. + */ + +#define PNG_IMAGE_SAMPLE_SIZE(fmt)\ + (PNG_IMAGE_SAMPLE_CHANNELS(fmt) * PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt)) + /* This is the size of the sample data for one sample. If the image is + * color-mapped it is the size of one color-map entry (and image pixels are + * one byte in size), otherwise it is the size of one image pixel. + */ + +#define PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(fmt)\ + (PNG_IMAGE_SAMPLE_CHANNELS(fmt) * 256) + /* The maximum size of the color-map required by the format expressed in a + * count of components. This can be used to compile-time allocate a + * color-map: + * + * png_uint_16 colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(linear_fmt)]; + * + * png_byte colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(sRGB_fmt)]; + * + * Alternatively use the PNG_IMAGE_COLORMAP_SIZE macro below to use the + * information from one of the png_image_begin_read_ APIs and dynamically + * allocate the required memory. + */ + +/* Corresponding information about the pixels */ +#define PNG_IMAGE_PIXEL_(test,fmt)\ + (((fmt)&PNG_FORMAT_FLAG_COLORMAP)?1:test(fmt)) + +#define PNG_IMAGE_PIXEL_CHANNELS(fmt)\ + PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_CHANNELS,fmt) + /* The number of separate channels (components) in a pixel; 1 for a + * color-mapped image. + */ + +#define PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)\ + PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_COMPONENT_SIZE,fmt) + /* The size, in bytes, of each component in a pixel; 1 for a color-mapped + * image. + */ + +#define PNG_IMAGE_PIXEL_SIZE(fmt) PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_SIZE,fmt) + /* The size, in bytes, of a complete pixel; 1 for a color-mapped image. */ + +/* Information about the whole row, or whole image */ +#define PNG_IMAGE_ROW_STRIDE(image)\ + (PNG_IMAGE_PIXEL_CHANNELS((image).format) * (image).width) + /* Return the total number of components in a single row of the image; this + * is the minimum 'row stride', the minimum count of components between each + * row. For a color-mapped image this is the minimum number of bytes in a + * row. + */ + +#define PNG_IMAGE_BUFFER_SIZE(image, row_stride)\ + (PNG_IMAGE_PIXEL_COMPONENT_SIZE((image).format)*(image).height*(row_stride)) + /* Return the size, in bytes, of an image buffer given a png_image and a row + * stride - the number of components to leave space for in each row. + */ + +#define PNG_IMAGE_SIZE(image)\ + PNG_IMAGE_BUFFER_SIZE(image, PNG_IMAGE_ROW_STRIDE(image)) + /* Return the size, in bytes, of the image in memory given just a png_image; + * the row stride is the minimum stride required for the image. + */ + +#define PNG_IMAGE_COLORMAP_SIZE(image)\ + (PNG_IMAGE_SAMPLE_SIZE((image).format) * (image).colormap_entries) + /* Return the size, in bytes, of the color-map of this image. If the image + * format is not a color-map format this will return a size sufficient for + * 256 entries in the given format; check PNG_FORMAT_FLAG_COLORMAP if + * you don't want to allocate a color-map in this case. + */ + +/* PNG_IMAGE_FLAG_* + * + * Flags containing additional information about the image are held in the + * 'flags' field of png_image. + */ +#define PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB 0x01 + /* This indicates the the RGB values of the in-memory bitmap do not + * correspond to the red, green and blue end-points defined by sRGB. + */ + +#define PNG_IMAGE_FLAG_FAST 0x02 + /* On write emphasise speed over compression; the resultant PNG file will be + * larger but will be produced significantly faster, particular for large + * images. Do not use this option for images which will be distributed, only + * used it when producing intermediate files that will be read back in + * repeatedly. For a typical 24-bit image the option will double the read + * speed at the cost of increasing the image size by 25%, however for many + * more compressible images the PNG file can be 10 times larger with only a + * slight speed gain. + */ + +#define PNG_IMAGE_FLAG_16BIT_sRGB 0x04 + /* On read if the image is a 16-bit per component image and there is no gAMA + * or sRGB chunk assume that the components are sRGB encoded. Notice that + * images output by the simplified API always have gamma information; setting + * this flag only affects the interpretation of 16-bit images from an + * external source. It is recommended that the application expose this flag + * to the user; the user can normally easily recognize the difference between + * linear and sRGB encoding. This flag has no effect on write - the data + * passed to the write APIs must have the correct encoding (as defined + * above.) + * + * If the flag is not set (the default) input 16-bit per component data is + * assumed to be linear. + * + * NOTE: the flag can only be set after the png_image_begin_read_ call, + * because that call initializes the 'flags' field. + */ + +#ifdef PNG_SIMPLIFIED_READ_SUPPORTED +/* READ APIs + * --------- + * + * The png_image passed to the read APIs must have been initialized by setting + * the png_controlp field 'opaque' to NULL (or, safer, memset the whole thing.) + */ +#ifdef PNG_STDIO_SUPPORTED +PNG_EXPORT(234, int, png_image_begin_read_from_file, (png_imagep image, + const char *file_name)); + /* The named file is opened for read and the image header is filled in + * from the PNG header in the file. + */ + +PNG_EXPORT(235, int, png_image_begin_read_from_stdio, (png_imagep image, + FILE* file)); + /* The PNG header is read from the stdio FILE object. */ +#endif /* STDIO */ + +PNG_EXPORT(236, int, png_image_begin_read_from_memory, (png_imagep image, + png_const_voidp memory, png_size_t size)); + /* The PNG header is read from the given memory buffer. */ + +PNG_EXPORT(237, int, png_image_finish_read, (png_imagep image, + png_const_colorp background, void *buffer, png_int_32 row_stride, + void *colormap)); + /* Finish reading the image into the supplied buffer and clean up the + * png_image structure. + * + * row_stride is the step, in byte or 2-byte units as appropriate, + * between adjacent rows. A positive stride indicates that the top-most row + * is first in the buffer - the normal top-down arrangement. A negative + * stride indicates that the bottom-most row is first in the buffer. + * + * background need only be supplied if an alpha channel must be removed from + * a png_byte format and the removal is to be done by compositing on a solid + * color; otherwise it may be NULL and any composition will be done directly + * onto the buffer. The value is an sRGB color to use for the background, + * for grayscale output the green channel is used. + * + * background must be supplied when an alpha channel must be removed from a + * single byte color-mapped output format, in other words if: + * + * 1) The original format from png_image_begin_read_from_* had + * PNG_FORMAT_FLAG_ALPHA set. + * 2) The format set by the application does not. + * 3) The format set by the application has PNG_FORMAT_FLAG_COLORMAP set and + * PNG_FORMAT_FLAG_LINEAR *not* set. + * + * For linear output removing the alpha channel is always done by compositing + * on black and background is ignored. + * + * colormap must be supplied when PNG_FORMAT_FLAG_COLORMAP is set. It must + * be at least the size (in bytes) returned by PNG_IMAGE_COLORMAP_SIZE. + * image->colormap_entries will be updated to the actual number of entries + * written to the colormap; this may be less than the original value. + */ + +PNG_EXPORT(238, void, png_image_free, (png_imagep image)); + /* Free any data allocated by libpng in image->opaque, setting the pointer to + * NULL. May be called at any time after the structure is initialized. + */ +#endif /* SIMPLIFIED_READ */ + +#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED +#ifdef PNG_STDIO_SUPPORTED +/* WRITE APIS + * ---------- + * For write you must initialize a png_image structure to describe the image to + * be written. To do this use memset to set the whole structure to 0 then + * initialize fields describing your image. + * + * version: must be set to PNG_IMAGE_VERSION + * opaque: must be initialized to NULL + * width: image width in pixels + * height: image height in rows + * format: the format of the data (image and color-map) you wish to write + * flags: set to 0 unless one of the defined flags applies; set + * PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB for color format images where the RGB + * values do not correspond to the colors in sRGB. + * colormap_entries: set to the number of entries in the color-map (0 to 256) + */ +PNG_EXPORT(239, int, png_image_write_to_file, (png_imagep image, + const char *file, int convert_to_8bit, const void *buffer, + png_int_32 row_stride, const void *colormap)); + /* Write the image to the named file. */ + +PNG_EXPORT(240, int, png_image_write_to_stdio, (png_imagep image, FILE *file, + int convert_to_8_bit, const void *buffer, png_int_32 row_stride, + const void *colormap)); + /* Write the image to the given (FILE*). */ + +/* With both write APIs if image is in one of the linear formats with 16-bit + * data then setting convert_to_8_bit will cause the output to be an 8-bit PNG + * gamma encoded according to the sRGB specification, otherwise a 16-bit linear + * encoded PNG file is written. + * + * With color-mapped data formats the colormap parameter point to a color-map + * with at least image->colormap_entries encoded in the specified format. If + * the format is linear the written PNG color-map will be converted to sRGB + * regardless of the convert_to_8_bit flag. + * + * With all APIs row_stride is handled as in the read APIs - it is the spacing + * from one row to the next in component sized units (1 or 2 bytes) and if + * negative indicates a bottom-up row layout in the buffer. If row_stride is zero, + * libpng will calculate it for you from the image width and number of channels. + * + * Note that the write API does not support interlacing, sub-8-bit pixels, indexed + * PNG (color_type 3) or most ancillary chunks. + */ +#endif /* STDIO */ +#endif /* SIMPLIFIED_WRITE */ +/******************************************************************************* + * END OF SIMPLIFIED API + ******************************************************************************/ +#endif /* SIMPLIFIED_{READ|WRITE} */ + +/******************************************************************************* + * Section 6: IMPLEMENTATION OPTIONS + ******************************************************************************* + * + * Support for arbitrary implementation-specific optimizations. The API allows + * particular options to be turned on or off. 'Option' is the number of the + * option and 'onoff' is 0 (off) or non-0 (on). The value returned is given + * by the PNG_OPTION_ defines below. + * + * HARDWARE: normally hardware capabilites, such as the Intel SSE instructions, + * are detected at run time, however sometimes it may be impossible + * to do this in user mode, in which case it is necessary to discover + * the capabilities in an OS specific way. Such capabilities are + * listed here when libpng has support for them and must be turned + * ON by the application if present. + * + * SOFTWARE: sometimes software optimizations actually result in performance + * decrease on some architectures or systems, or with some sets of + * PNG images. 'Software' options allow such optimizations to be + * selected at run time. + */ +#ifdef PNG_SET_OPTION_SUPPORTED +#ifdef PNG_ARM_NEON_API_SUPPORTED +# define PNG_ARM_NEON 0 /* HARDWARE: ARM Neon SIMD instructions supported */ +#endif +#define PNG_MAXIMUM_INFLATE_WINDOW 2 /* SOFTWARE: force maximum window */ +#define PNG_SKIP_sRGB_CHECK_PROFILE 4 /* SOFTWARE: Check ICC profile for sRGB */ +#define PNG_OPTION_NEXT 6 /* Next option - numbers must be even */ + +/* Return values: NOTE: there are four values and 'off' is *not* zero */ +#define PNG_OPTION_UNSET 0 /* Unset - defaults to off */ +#define PNG_OPTION_INVALID 1 /* Option number out of range */ +#define PNG_OPTION_OFF 2 +#define PNG_OPTION_ON 3 + +PNG_EXPORT(244, int, png_set_option, (png_structrp png_ptr, int option, + int onoff)); +#endif /* SET_OPTION */ + +/******************************************************************************* + * END OF HARDWARE AND SOFTWARE OPTIONS + ******************************************************************************/ + +/* Maintainer: Put new public prototypes here ^, in libpng.3, in project + * defs, and in scripts/symbols.def. */ /* The last ordinal number (this is the *last* one already used; the next - * one to use is one more than this.) Maintainer, remember to add an entry to - * scripts/symbols.def as well. + * one to use is one more than this.) */ #ifdef PNG_EXPORT_LAST_ORDINAL - PNG_EXPORT_LAST_ORDINAL(234); + PNG_EXPORT_LAST_ORDINAL(244); #endif #ifdef __cplusplus diff --git a/3rdparty/libpng/pngconf.h b/3rdparty/libpng/pngconf.h index 5c3eb1454..f1b795b47 100644 --- a/3rdparty/libpng/pngconf.h +++ b/3rdparty/libpng/pngconf.h @@ -1,9 +1,9 @@ /* pngconf.h - machine configurable file for libpng * - * libpng version 1.5.12 - July 11, 2012 + * libpng version 1.6.19, July 23, 2015 * - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -11,9 +11,7 @@ * For conditions of distribution and use, see the disclaimer * and license in png.h * - */ - -/* Any machine specific code is near the front of this file, so if you + * Any machine specific code is near the front of this file, so if you * are configuring libpng for a machine, you may want to read the section * starting here down to where it starts to typedef png_color, png_text, * and png_info. @@ -22,34 +20,50 @@ #ifndef PNGCONF_H #define PNGCONF_H -#ifndef PNG_BUILDING_SYMBOL_TABLE -/* PNG_NO_LIMITS_H may be used to turn off the use of the standard C - * definition file for machine specific limits, this may impact the - * correctness of the definitions below (see uses of INT_MAX). - */ -# ifndef PNG_NO_LIMITS_H -# include -# endif +#ifndef PNG_BUILDING_SYMBOL_TABLE /* else includes may cause problems */ -/* For the memory copy APIs (i.e. the standard definitions of these), - * because this file defines png_memcpy and so on the base APIs must - * be defined here. +/* From libpng 1.6.0 libpng requires an ANSI X3.159-1989 ("ISOC90") compliant C + * compiler for correct compilation. The following header files are required by + * the standard. If your compiler doesn't provide these header files, or they + * do not match the standard, you will need to provide/improve them. */ -# ifdef BSD -# include -# else -# include -# endif +#include +#include -/* For png_FILE_p - this provides the standard definition of a - * FILE +/* Library header files. These header files are all defined by ISOC90; libpng + * expects conformant implementations, however, an ISOC90 conformant system need + * not provide these header files if the functionality cannot be implemented. + * In this case it will be necessary to disable the relevant parts of libpng in + * the build of pnglibconf.h. + * + * Prior to 1.6.0 string.h was included here; the API changes in 1.6.0 to not + * include this unnecessary header file. */ -# ifdef PNG_STDIO_SUPPORTED -# include -# endif + +#ifdef PNG_STDIO_SUPPORTED + /* Required for the definition of FILE: */ +# include #endif -/* This controls optimization of the reading of 16 and 32 bit values +#ifdef PNG_SETJMP_SUPPORTED + /* Required for the definition of jmp_buf and the declaration of longjmp: */ +# include +#endif + +#ifdef PNG_CONVERT_tIME_SUPPORTED + /* Required for struct tm: */ +# include +#endif + +#endif /* PNG_BUILDING_SYMBOL_TABLE */ + +/* Prior to 1.6.0 it was possible to turn off 'const' in declarations using + * PNG_NO_CONST; this is no longer supported except for data declarations which + * apparently still cause problems in 2011 on some compilers. + */ +#define PNG_CONST const /* backward compatibility only */ + +/* This controls optimization of the reading of 16-bit and 32-bit values * from PNG files. It can be set on a per-app-file basis - it * just changes whether a macro is used when the function is called. * The library builder sets the default; if read functions are not @@ -72,28 +86,13 @@ * may be changed on a per-file basis when compiling against libpng. */ -/* The PNGARG macro protects us against machines that don't have function - * prototypes (ie K&R style headers). If your compiler does not handle - * function prototypes, define this macro and use the included ansi2knr. - * I've always been able to use _NO_PROTO as the indicator, but you may - * need to drag the empty declaration out in front of here, or change the - * ifdef to suit your own needs. +/* The PNGARG macro was used in versions of libpng prior to 1.6.0 to protect + * against legacy (pre ISOC90) compilers that did not understand function + * prototypes. It is not required for modern C compilers. */ #ifndef PNGARG - -# ifdef OF /* zlib prototype munger */ -# define PNGARG(arglist) OF(arglist) -# else - -# ifdef _NO_PROTO -# define PNGARG(arglist) () -# else -# define PNGARG(arglist) arglist -# endif /* _NO_PROTO */ - -# endif /* OF */ - -#endif /* PNGARG */ +# define PNGARG(arglist) arglist +#endif /* Function calling conventions. * ============================= @@ -177,18 +176,16 @@ * ========================== * This code is used at build time to find PNG_IMPEXP, the API settings * and PNG_EXPORT_TYPE(), it may also set a macro to indicate the DLL - * import processing is possible. On Windows/x86 systems it also sets + * import processing is possible. On Windows systems it also sets * compiler-specific macros to the values required to change the calling * conventions of the various functions. */ -#if ( defined(_Windows) || defined(_WINDOWS) || defined(WIN32) ||\ - defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) ) &&\ - ( defined(_X86_) || defined(_X64_) || defined(_M_IX86) ||\ - defined(_M_X64) || defined(_M_IA64) ) - /* Windows system (DOS doesn't support DLLs) running on x86/x64. Includes - * builds under Cygwin or MinGW. Also includes Watcom builds but these need - * special treatment because they are not compatible with GCC or Visual C - * because of different calling conventions. +#if defined(_Windows) || defined(_WINDOWS) || defined(WIN32) ||\ + defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) + /* Windows system (DOS doesn't support DLLs). Includes builds under Cygwin or + * MinGW on any architecture currently supported by Windows. Also includes + * Watcom builds but these need special treatment because they are not + * compatible with GCC or Visual C because of different calling conventions. */ # if PNG_API_RULE == 2 /* If this line results in an error, either because __watcall is not @@ -199,9 +196,12 @@ # define PNGCAPI __watcall # endif -# if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800)) +# if defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 800)) # define PNGCAPI __cdecl # if PNG_API_RULE == 1 + /* If this line results in an error __stdcall is not understood and + * PNG_API_RULE should not have been set to '1'. + */ # define PNGAPI __stdcall # endif # else @@ -216,10 +216,11 @@ # define PNGAPI _stdcall # endif # endif /* compiler/api */ + /* NOTE: PNGCBAPI always defaults to PNGCAPI. */ # if defined(PNGAPI) && !defined(PNG_USER_PRIVATEBUILD) - ERROR: PNG_USER_PRIVATEBUILD must be defined if PNGAPI is changed +# error "PNG_USER_PRIVATEBUILD must be defined if PNGAPI is changed" # endif # if (defined(_MSC_VER) && _MSC_VER < 800) ||\ @@ -239,7 +240,7 @@ # endif # endif /* compiler */ -#else /* !Windows/x86 */ +#else /* !Windows */ # if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__) # define PNGAPI _System # else /* !Windows/x86 && !OS/2 */ @@ -294,11 +295,11 @@ * table entries, so we discard it here. See the .dfn files in the * scripts directory. */ -#ifndef PNG_EXPORTA -# define PNG_EXPORTA(ordinal, type, name, args, attributes)\ - PNG_FUNCTION(PNG_EXPORT_TYPE(type),(PNGAPI name),PNGARG(args), \ - extern attributes) +#ifndef PNG_EXPORTA +# define PNG_EXPORTA(ordinal, type, name, args, attributes) \ + PNG_FUNCTION(PNG_EXPORT_TYPE(type), (PNGAPI name), PNGARG(args), \ + PNG_LINKAGE_API attributes) #endif /* ANSI-C (C90) does not permit a macro to be invoked with an empty argument, @@ -306,7 +307,7 @@ */ #define PNG_EMPTY /*empty list*/ -#define PNG_EXPORT(ordinal, type, name, args)\ +#define PNG_EXPORT(ordinal, type, name, args) \ PNG_EXPORTA(ordinal, type, name, args, PNG_EMPTY) /* Use PNG_REMOVED to comment out a removed interface. */ @@ -333,40 +334,73 @@ #ifdef PNG_PEDANTIC_WARNINGS_SUPPORTED /* Support for compiler specific function attributes. These are used - * so that where compiler support is available incorrect use of API + * so that where compiler support is available, incorrect use of API * functions in png.h will generate compiler warnings. Added at libpng - * version 1.2.41. + * version 1.2.41. Disabling these removes the warnings but may also produce + * less efficient code. */ -# if defined(__GNUC__) +# if defined(__clang__) && defined(__has_attribute) + /* Clang defines both __clang__ and __GNUC__. Check __clang__ first. */ +# if !defined(PNG_USE_RESULT) && __has_attribute(__warn_unused_result__) +# define PNG_USE_RESULT __attribute__((__warn_unused_result__)) +# endif +# if !defined(PNG_NORETURN) && __has_attribute(__noreturn__) +# define PNG_NORETURN __attribute__((__noreturn__)) +# endif +# if !defined(PNG_ALLOCATED) && __has_attribute(__malloc__) +# define PNG_ALLOCATED __attribute__((__malloc__)) +# endif +# if !defined(PNG_DEPRECATED) && __has_attribute(__deprecated__) +# define PNG_DEPRECATED __attribute__((__deprecated__)) +# endif +# if !defined(PNG_PRIVATE) +# ifdef __has_extension +# if __has_extension(attribute_unavailable_with_message) +# define PNG_PRIVATE __attribute__((__unavailable__(\ + "This function is not exported by libpng."))) +# endif +# endif +# endif +# ifndef PNG_RESTRICT +# define PNG_RESTRICT __restrict +# endif + +# elif defined(__GNUC__) # ifndef PNG_USE_RESULT # define PNG_USE_RESULT __attribute__((__warn_unused_result__)) # endif # ifndef PNG_NORETURN # define PNG_NORETURN __attribute__((__noreturn__)) # endif -# ifndef PNG_ALLOCATED -# define PNG_ALLOCATED __attribute__((__malloc__)) -# endif -# ifndef PNG_DEPRECATED -# define PNG_DEPRECATED __attribute__((__deprecated__)) -# endif -# ifndef PNG_PRIVATE -# if 0 /* Doesn't work so we use deprecated instead*/ -# define PNG_PRIVATE \ - __attribute__((warning("This function is not exported by libpng."))) -# else -# define PNG_PRIVATE \ - __attribute__((__deprecated__)) +# if __GNUC__ >= 3 +# ifndef PNG_ALLOCATED +# define PNG_ALLOCATED __attribute__((__malloc__)) # endif -# endif -# endif /* __GNUC__ */ +# ifndef PNG_DEPRECATED +# define PNG_DEPRECATED __attribute__((__deprecated__)) +# endif +# ifndef PNG_PRIVATE +# if 0 /* Doesn't work so we use deprecated instead*/ +# define PNG_PRIVATE \ + __attribute__((warning("This function is not exported by libpng."))) +# else +# define PNG_PRIVATE \ + __attribute__((__deprecated__)) +# endif +# endif +# if ((__GNUC__ > 3) || !defined(__GNUC_MINOR__) || (__GNUC_MINOR__ >= 1)) +# ifndef PNG_RESTRICT +# define PNG_RESTRICT __restrict +# endif +# endif /* __GNUC__.__GNUC_MINOR__ > 3.0 */ +# endif /* __GNUC__ >= 3 */ -# if defined(_MSC_VER) && (_MSC_VER >= 1300) +# elif defined(_MSC_VER) && (_MSC_VER >= 1300) # ifndef PNG_USE_RESULT # define PNG_USE_RESULT /* not supported */ # endif # ifndef PNG_NORETURN -# define PNG_NORETURN __declspec(noreturn) +# define PNG_NORETURN __declspec(noreturn) # endif # ifndef PNG_ALLOCATED # if (_MSC_VER >= 1400) @@ -379,7 +413,17 @@ # ifndef PNG_PRIVATE # define PNG_PRIVATE __declspec(deprecated) # endif -# endif /* _MSC_VER */ +# ifndef PNG_RESTRICT +# if (_MSC_VER >= 1400) +# define PNG_RESTRICT __restrict +# endif +# endif + +# elif defined(__WATCOMC__) +# ifndef PNG_RESTRICT +# define PNG_RESTRICT __restrict +# endif +# endif #endif /* PNG_PEDANTIC_WARNINGS */ #ifndef PNG_DEPRECATED @@ -397,10 +441,14 @@ #ifndef PNG_PRIVATE # define PNG_PRIVATE /* This is a private libpng function */ #endif +#ifndef PNG_RESTRICT +# define PNG_RESTRICT /* The C99 "restrict" feature */ +#endif + #ifndef PNG_FP_EXPORT /* A floating point API. */ # ifdef PNG_FLOATING_POINT_SUPPORTED # define PNG_FP_EXPORT(ordinal, type, name, args)\ - PNG_EXPORT(ordinal, type, name, args) + PNG_EXPORT(ordinal, type, name, args); # else /* No floating point APIs */ # define PNG_FP_EXPORT(ordinal, type, name, args) # endif @@ -408,189 +456,167 @@ #ifndef PNG_FIXED_EXPORT /* A fixed point API. */ # ifdef PNG_FIXED_POINT_SUPPORTED # define PNG_FIXED_EXPORT(ordinal, type, name, args)\ - PNG_EXPORT(ordinal, type, name, args) + PNG_EXPORT(ordinal, type, name, args); # else /* No fixed point APIs */ # define PNG_FIXED_EXPORT(ordinal, type, name, args) # endif #endif -/* The following uses const char * instead of char * for error - * and warning message functions, so some compilers won't complain. - * If you do not want to use const, define PNG_NO_CONST here. +#ifndef PNG_BUILDING_SYMBOL_TABLE +/* Some typedefs to get us started. These should be safe on most of the common + * platforms. * - * This should not change how the APIs are called, so it can be done - * on a per-file basis in the application. + * png_uint_32 and png_int_32 may, currently, be larger than required to hold a + * 32-bit value however this is not normally advisable. + * + * png_uint_16 and png_int_16 should always be two bytes in size - this is + * verified at library build time. + * + * png_byte must always be one byte in size. + * + * The checks below use constants from limits.h, as defined by the ISOC90 + * standard. */ -#ifndef PNG_CONST -# ifndef PNG_NO_CONST -# define PNG_CONST const -# else -# define PNG_CONST -# endif +#if CHAR_BIT == 8 && UCHAR_MAX == 255 + typedef unsigned char png_byte; +#else +# error "libpng requires 8-bit bytes" #endif -/* Some typedefs to get us started. These should be safe on most of the - * common platforms. The typedefs should be at least as large as the - * numbers suggest (a png_uint_32 must be at least 32 bits long), but they - * don't have to be exactly that size. Some compilers dislike passing - * unsigned shorts as function parameters, so you may be better off using - * unsigned int for png_uint_16. - */ - -#if defined(INT_MAX) && (INT_MAX > 0x7ffffffeL) -typedef unsigned int png_uint_32; -typedef int png_int_32; +#if INT_MIN == -32768 && INT_MAX == 32767 + typedef int png_int_16; +#elif SHRT_MIN == -32768 && SHRT_MAX == 32767 + typedef short png_int_16; #else -typedef unsigned long png_uint_32; -typedef long png_int_32; +# error "libpng requires a signed 16-bit type" #endif -typedef unsigned short png_uint_16; -typedef short png_int_16; -typedef unsigned char png_byte; -#ifdef PNG_NO_SIZE_T -typedef unsigned int png_size_t; +#if UINT_MAX == 65535 + typedef unsigned int png_uint_16; +#elif USHRT_MAX == 65535 + typedef unsigned short png_uint_16; #else +# error "libpng requires an unsigned 16-bit type" +#endif + +#if INT_MIN < -2147483646 && INT_MAX > 2147483646 + typedef int png_int_32; +#elif LONG_MIN < -2147483646 && LONG_MAX > 2147483646 + typedef long int png_int_32; +#else +# error "libpng requires a signed 32-bit (or more) type" +#endif + +#if UINT_MAX > 4294967294 + typedef unsigned int png_uint_32; +#elif ULONG_MAX > 4294967294 + typedef unsigned long int png_uint_32; +#else +# error "libpng requires an unsigned 32-bit (or more) type" +#endif + +/* Prior to 1.6.0 it was possible to disable the use of size_t, 1.6.0, however, + * requires an ISOC90 compiler and relies on consistent behavior of sizeof. + */ typedef size_t png_size_t; -#endif -#define png_sizeof(x) (sizeof (x)) +typedef ptrdiff_t png_ptrdiff_t; -/* The following is needed for medium model support. It cannot be in the - * pngpriv.h header. Needs modification for other compilers besides - * MSC. Model independent support declares all arrays and pointers to be - * large using the far keyword. The zlib version used must also support - * model independent data. As of version zlib 1.0.4, the necessary changes - * have been made in zlib. The USE_FAR_KEYWORD define triggers other - * changes that are needed. (Tim Wegner) +/* libpng needs to know the maximum value of 'size_t' and this controls the + * definition of png_alloc_size_t, below. This maximum value of size_t limits + * but does not control the maximum allocations the library makes - there is + * direct application control of this through png_set_user_limits(). */ - -/* Separate compiler dependencies (problem here is that zlib.h always - * defines FAR. (SJT) - */ -#ifdef __BORLANDC__ -# if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__) -# define LDATA 1 -# else -# define LDATA 0 -# endif - /* GRR: why is Cygwin in here? Cygwin is not Borland C... */ -# if !defined(__WIN32__) && !defined(__FLAT__) && !defined(__CYGWIN__) -# define PNG_MAX_MALLOC_64K /* only used in build */ -# if (LDATA != 1) -# ifndef FAR -# define FAR __far -# endif -# define USE_FAR_KEYWORD -# endif /* LDATA != 1 */ - /* Possibly useful for moving data out of default segment. - * Uncomment it if you want. Could also define FARDATA as - * const if your compiler supports it. (SJT) -# define FARDATA FAR - */ -# endif /* __WIN32__, __FLAT__, __CYGWIN__ */ -#endif /* __BORLANDC__ */ - - -/* Suggest testing for specific compiler first before testing for - * FAR. The Watcom compiler defines both __MEDIUM__ and M_I86MM, - * making reliance oncertain keywords suspect. (SJT) - */ - -/* MSC Medium model */ -#ifdef FAR -# ifdef M_I86MM -# define USE_FAR_KEYWORD -# define FARDATA FAR -# include +#ifndef PNG_SMALL_SIZE_T + /* Compiler specific tests for systems where size_t is known to be less than + * 32 bits (some of these systems may no longer work because of the lack of + * 'far' support; see above.) + */ +# if (defined(__TURBOC__) && !defined(__FLAT__)) ||\ + (defined(_MSC_VER) && defined(MAXSEG_64K)) +# define PNG_SMALL_SIZE_T # endif #endif -/* SJT: default case */ -#ifndef FAR -# define FAR +/* png_alloc_size_t is guaranteed to be no smaller than png_size_t, and no + * smaller than png_uint_32. Casts from png_size_t or png_uint_32 to + * png_alloc_size_t are not necessary; in fact, it is recommended not to use + * them at all so that the compiler can complain when something turns out to be + * problematic. + * + * Casts in the other direction (from png_alloc_size_t to png_size_t or + * png_uint_32) should be explicitly applied; however, we do not expect to + * encounter practical situations that require such conversions. + * + * PNG_SMALL_SIZE_T must be defined if the maximum value of size_t is less than + * 4294967295 - i.e. less than the maximum value of png_uint_32. + */ +#ifdef PNG_SMALL_SIZE_T + typedef png_uint_32 png_alloc_size_t; +#else + typedef png_size_t png_alloc_size_t; #endif -/* At this point FAR is always defined */ -#ifndef FARDATA -# define FARDATA -#endif +/* Prior to 1.6.0 libpng offered limited support for Microsoft C compiler + * implementations of Intel CPU specific support of user-mode segmented address + * spaces, where 16-bit pointers address more than 65536 bytes of memory using + * separate 'segment' registers. The implementation requires two different + * types of pointer (only one of which includes the segment value.) + * + * If required this support is available in version 1.2 of libpng and may be + * available in versions through 1.5, although the correctness of the code has + * not been verified recently. + */ -/* Typedef for floating-point numbers that are converted - * to fixed-point with a multiple of 100,000, e.g., gamma +/* Typedef for floating-point numbers that are converted to fixed-point with a + * multiple of 100,000, e.g., gamma */ typedef png_int_32 png_fixed_point; /* Add typedefs for pointers */ -typedef void FAR * png_voidp; -typedef PNG_CONST void FAR * png_const_voidp; -typedef png_byte FAR * png_bytep; -typedef PNG_CONST png_byte FAR * png_const_bytep; -typedef png_uint_32 FAR * png_uint_32p; -typedef PNG_CONST png_uint_32 FAR * png_const_uint_32p; -typedef png_int_32 FAR * png_int_32p; -typedef PNG_CONST png_int_32 FAR * png_const_int_32p; -typedef png_uint_16 FAR * png_uint_16p; -typedef PNG_CONST png_uint_16 FAR * png_const_uint_16p; -typedef png_int_16 FAR * png_int_16p; -typedef PNG_CONST png_int_16 FAR * png_const_int_16p; -typedef char FAR * png_charp; -typedef PNG_CONST char FAR * png_const_charp; -typedef png_fixed_point FAR * png_fixed_point_p; -typedef PNG_CONST png_fixed_point FAR * png_const_fixed_point_p; -typedef png_size_t FAR * png_size_tp; -typedef PNG_CONST png_size_t FAR * png_const_size_tp; +typedef void * png_voidp; +typedef const void * png_const_voidp; +typedef png_byte * png_bytep; +typedef const png_byte * png_const_bytep; +typedef png_uint_32 * png_uint_32p; +typedef const png_uint_32 * png_const_uint_32p; +typedef png_int_32 * png_int_32p; +typedef const png_int_32 * png_const_int_32p; +typedef png_uint_16 * png_uint_16p; +typedef const png_uint_16 * png_const_uint_16p; +typedef png_int_16 * png_int_16p; +typedef const png_int_16 * png_const_int_16p; +typedef char * png_charp; +typedef const char * png_const_charp; +typedef png_fixed_point * png_fixed_point_p; +typedef const png_fixed_point * png_const_fixed_point_p; +typedef png_size_t * png_size_tp; +typedef const png_size_t * png_const_size_tp; #ifdef PNG_STDIO_SUPPORTED typedef FILE * png_FILE_p; #endif #ifdef PNG_FLOATING_POINT_SUPPORTED -typedef double FAR * png_doublep; -typedef PNG_CONST double FAR * png_const_doublep; +typedef double * png_doublep; +typedef const double * png_const_doublep; #endif /* Pointers to pointers; i.e. arrays */ -typedef png_byte FAR * FAR * png_bytepp; -typedef png_uint_32 FAR * FAR * png_uint_32pp; -typedef png_int_32 FAR * FAR * png_int_32pp; -typedef png_uint_16 FAR * FAR * png_uint_16pp; -typedef png_int_16 FAR * FAR * png_int_16pp; -typedef PNG_CONST char FAR * FAR * png_const_charpp; -typedef char FAR * FAR * png_charpp; -typedef png_fixed_point FAR * FAR * png_fixed_point_pp; +typedef png_byte * * png_bytepp; +typedef png_uint_32 * * png_uint_32pp; +typedef png_int_32 * * png_int_32pp; +typedef png_uint_16 * * png_uint_16pp; +typedef png_int_16 * * png_int_16pp; +typedef const char * * png_const_charpp; +typedef char * * png_charpp; +typedef png_fixed_point * * png_fixed_point_pp; #ifdef PNG_FLOATING_POINT_SUPPORTED -typedef double FAR * FAR * png_doublepp; +typedef double * * png_doublepp; #endif /* Pointers to pointers to pointers; i.e., pointer to array */ -typedef char FAR * FAR * FAR * png_charppp; +typedef char * * * png_charppp; -/* png_alloc_size_t is guaranteed to be no smaller than png_size_t, - * and no smaller than png_uint_32. Casts from png_size_t or png_uint_32 - * to png_alloc_size_t are not necessary; in fact, it is recommended - * not to use them at all so that the compiler can complain when something - * turns out to be problematic. - * Casts in the other direction (from png_alloc_size_t to png_size_t or - * png_uint_32) should be explicitly applied; however, we do not expect - * to encounter practical situations that require such conversions. - */ -#if defined(__TURBOC__) && !defined(__FLAT__) - typedef unsigned long png_alloc_size_t; -#else -# if defined(_MSC_VER) && defined(MAXSEG_64K) - typedef unsigned long png_alloc_size_t; -# else - /* This is an attempt to detect an old Windows system where (int) is - * actually 16 bits, in that case png_malloc must have an argument with a - * bigger size to accomodate the requirements of the library. - */ -# if (defined(_Windows) || defined(_WINDOWS) || defined(_WINDOWS_)) && \ - (!defined(INT_MAX) || INT_MAX <= 0x7ffffffeL) - typedef DWORD png_alloc_size_t; -# else - typedef png_size_t png_alloc_size_t; -# endif -# endif -#endif +#endif /* PNG_BUILDING_SYMBOL_TABLE */ #endif /* PNGCONF_H */ diff --git a/3rdparty/libpng/pngdebug.h b/3rdparty/libpng/pngdebug.h index 16f81fdd1..6a01b106e 100644 --- a/3rdparty/libpng/pngdebug.h +++ b/3rdparty/libpng/pngdebug.h @@ -1,12 +1,11 @@ /* pngdebug.h - Debugging macros for libpng, also used in pngtest.c * - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.8 [December 19, 2013] + * Copyright (c) 1998-2013 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * - * Last changed in libpng 1.5.0 [January 6, 2011] - * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h @@ -25,7 +24,7 @@ * (actually ((void)0)). * * level: level of detail of message, starting at 0. A level 'n' - * message is preceded by 'n' tab characters (not implemented + * message is preceded by 'n' 3-space indentations (not implemented * on Microsoft compilers unless PNG_DEBUG_FILE is also * defined, to allow debug DLL compilation with no standard IO). * message: a printf(3) style text string. A trailing '\n' is added @@ -77,32 +76,29 @@ # endif /* PNG_DEBUG_FILE */ # if (PNG_DEBUG > 1) -/* Note: ["%s"m PNG_STRING_NEWLINE] probably does not work on - * non-ISO compilers - */ # ifdef __STDC__ # ifndef png_debug # define png_debug(l,m) \ do { \ int num_tabs=l; \ - fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ - (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":"")))); \ + fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ + (num_tabs==2 ? " " : (num_tabs>2 ? " " : "")))); \ } while (0) # endif # ifndef png_debug1 # define png_debug1(l,m,p1) \ do { \ int num_tabs=l; \ - fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ - (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1); \ + fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ + (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1); \ } while (0) # endif # ifndef png_debug2 # define png_debug2(l,m,p1,p2) \ do { \ int num_tabs=l; \ - fprintf(PNG_DEBUG_FILE,"%s"m PNG_STRING_NEWLINE,(num_tabs==1 ? "\t" : \ - (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))),p1,p2); \ + fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ + (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1,p2);\ } while (0) # endif # else /* __STDC __ */ diff --git a/3rdparty/libpng/pngerror.c b/3rdparty/libpng/pngerror.c index e0585a856..0781866a8 100644 --- a/3rdparty/libpng/pngerror.c +++ b/3rdparty/libpng/pngerror.c @@ -1,8 +1,8 @@ /* pngerror.c - stub functions for i/o and memory allocation * - * Last changed in libpng 1.5.8 [February 1, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.15 [November 20, 2014] + * Copyright (c) 1998-2014 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -20,14 +20,14 @@ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) -static PNG_FUNCTION(void, png_default_error,PNGARG((png_structp png_ptr, +static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr, png_const_charp error_message)),PNG_NORETURN); #ifdef PNG_WARNINGS_SUPPORTED static void /* PRIVATE */ -png_default_warning PNGARG((png_structp png_ptr, +png_default_warning PNGARG((png_const_structrp png_ptr, png_const_charp warning_message)); -#endif /* PNG_WARNINGS_SUPPORTED */ +#endif /* WARNINGS */ /* This function is called whenever there is a fatal error. This function * should not be changed. If there is a need to handle errors differently, @@ -36,14 +36,15 @@ png_default_warning PNGARG((png_structp png_ptr, */ #ifdef PNG_ERROR_TEXT_SUPPORTED PNG_FUNCTION(void,PNGAPI -png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN) +png_error,(png_const_structrp png_ptr, png_const_charp error_message), + PNG_NORETURN) { #ifdef PNG_ERROR_NUMBERS_SUPPORTED char msg[16]; if (png_ptr != NULL) { - if (png_ptr->flags& - (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) + if ((png_ptr->flags & + (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0 { if (*error_message == PNG_LITERAL_SHARP) { @@ -53,7 +54,7 @@ png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN) if (error_message[offset] == ' ') break; - if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) + if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0) { int i; for (i = 0; i < offset - 1; i++) @@ -68,7 +69,7 @@ png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN) else { - if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) + if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0) { msg[0] = '0'; msg[1] = '\0'; @@ -79,7 +80,8 @@ png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN) } #endif if (png_ptr != NULL && png_ptr->error_fn != NULL) - (*(png_ptr->error_fn))(png_ptr, error_message); + (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), + error_message); /* If the custom handler doesn't exist, or if it returns, use the default handler, which will not return. */ @@ -87,7 +89,7 @@ png_error,(png_structp png_ptr, png_const_charp error_message),PNG_NORETURN) } #else PNG_FUNCTION(void,PNGAPI -png_err,(png_structp png_ptr),PNG_NORETURN) +png_err,(png_const_structrp png_ptr),PNG_NORETURN) { /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed * erroneously as '\0', instead of the empty string "". This was @@ -95,13 +97,13 @@ png_err,(png_structp png_ptr),PNG_NORETURN) * will crash in this case. */ if (png_ptr != NULL && png_ptr->error_fn != NULL) - (*(png_ptr->error_fn))(png_ptr, ""); + (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ""); /* If the custom handler doesn't exist, or if it returns, use the default handler, which will not return. */ png_default_error(png_ptr, ""); } -#endif /* PNG_ERROR_TEXT_SUPPORTED */ +#endif /* ERROR_TEXT */ /* Utility to safely appends strings to a buffer. This never errors out so * error checking is not required in the caller. @@ -150,7 +152,7 @@ png_format_number(png_const_charp start, png_charp end, int format, case PNG_NUMBER_FORMAT_fixed: /* Needs five digits (the fraction) */ mincount = 5; - if (output || number % 10 != 0) + if (output != 0 || number % 10 != 0) { *--end = digits[number % 10]; output = 1; @@ -161,7 +163,7 @@ png_format_number(png_const_charp start, png_charp end, int format, case PNG_NUMBER_FORMAT_02u: /* Expects at least 2 digits. */ mincount = 2; - /* fall through */ + /* FALL THROUGH */ case PNG_NUMBER_FORMAT_u: *--end = digits[number % 10]; @@ -171,7 +173,7 @@ png_format_number(png_const_charp start, png_charp end, int format, case PNG_NUMBER_FORMAT_02x: /* This format expects at least two digits */ mincount = 2; - /* fall through */ + /* FALL THROUGH */ case PNG_NUMBER_FORMAT_x: *--end = digits[number & 0xf]; @@ -187,13 +189,13 @@ png_format_number(png_const_charp start, png_charp end, int format, ++count; /* Float a fixed number here: */ - if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start) + if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start)) { /* End of the fraction, but maybe nothing was output? In that case * drop the decimal point. If the number is a true zero handle that * here. */ - if (output) + if (output != 0) *--end = '.'; else if (number == 0) /* and !output */ *--end = '0'; @@ -211,14 +213,14 @@ png_format_number(png_const_charp start, png_charp end, int format, * png_set_error_fn() to replace the warning function at run-time. */ void PNGAPI -png_warning(png_structp png_ptr, png_const_charp warning_message) +png_warning(png_const_structrp png_ptr, png_const_charp warning_message) { int offset = 0; if (png_ptr != NULL) { #ifdef PNG_ERROR_NUMBERS_SUPPORTED - if (png_ptr->flags& - (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) + if ((png_ptr->flags & + (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0) #endif { if (*warning_message == PNG_LITERAL_SHARP) @@ -230,7 +232,8 @@ png_warning(png_structp png_ptr, png_const_charp warning_message) } } if (png_ptr != NULL && png_ptr->warning_fn != NULL) - (*(png_ptr->warning_fn))(png_ptr, warning_message + offset); + (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr), + warning_message + offset); else png_default_warning(png_ptr, warning_message + offset); } @@ -278,7 +281,7 @@ png_warning_parameter_signed(png_warning_parameters p, int number, int format, } void -png_formatted_warning(png_structp png_ptr, png_warning_parameters p, +png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p, png_const_charp message) { /* The internal buffer is just 192 bytes - enough for all our messages, @@ -346,29 +349,79 @@ png_formatted_warning(png_structp png_ptr, png_warning_parameters p, /* i is always less than (sizeof msg), so: */ msg[i] = '\0'; - /* And this is the formatted message, it may be larger than - * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these are - * not (currently) formatted. + /* And this is the formatted message. It may be larger than + * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these + * are not (currently) formatted. */ png_warning(png_ptr, msg); } -#endif /* PNG_WARNINGS_SUPPORTED */ +#endif /* WARNINGS */ #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI -png_benign_error(png_structp png_ptr, png_const_charp error_message) +png_benign_error(png_const_structrp png_ptr, png_const_charp error_message) { - if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) + if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0) + { +# ifdef PNG_READ_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && + png_ptr->chunk_name != 0) + png_chunk_warning(png_ptr, error_message); + else +# endif + png_warning(png_ptr, error_message); + } + + else + { +# ifdef PNG_READ_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && + png_ptr->chunk_name != 0) + png_chunk_error(png_ptr, error_message); + else +# endif + png_error(png_ptr, error_message); + } + +# ifndef PNG_ERROR_TEXT_SUPPORTED + PNG_UNUSED(error_message) +# endif +} + +void /* PRIVATE */ +png_app_warning(png_const_structrp png_ptr, png_const_charp error_message) +{ + if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0) png_warning(png_ptr, error_message); else png_error(png_ptr, error_message); -} -#endif +# ifndef PNG_ERROR_TEXT_SUPPORTED + PNG_UNUSED(error_message) +# endif +} + +void /* PRIVATE */ +png_app_error(png_const_structrp png_ptr, png_const_charp error_message) +{ + if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0) + png_warning(png_ptr, error_message); + else + png_error(png_ptr, error_message); + +# ifndef PNG_ERROR_TEXT_SUPPORTED + PNG_UNUSED(error_message) +# endif +} +#endif /* BENIGN_ERRORS */ + +#define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */ +#if defined(PNG_WARNINGS_SUPPORTED) || \ + (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)) /* These utilities are used internally to build an error message that relates * to the current chunk. The chunk name comes from png_ptr->chunk_name, - * this is used to prefix the message. The message is limited in length - * to 63 bytes, the name characters are output as hex digits wrapped in [] + * which is used to prefix the message. The message is limited in length + * to 63 bytes. The name characters are output as hex digits wrapped in [] * if the character is invalid. */ #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) @@ -377,10 +430,8 @@ static PNG_CONST char png_digit[16] = { 'A', 'B', 'C', 'D', 'E', 'F' }; -#define PNG_MAX_ERROR_TEXT 64 -#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) static void /* PRIVATE */ -png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp +png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp error_message) { png_uint_32 chunk_name = png_ptr->chunk_name; @@ -391,7 +442,7 @@ png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp int c = (int)(chunk_name >> ishift) & 0xff; ishift -= 8; - if (isnonalpha(c)) + if (isnonalpha(c) != 0) { buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; buffer[iout++] = png_digit[(c & 0xf0) >> 4]; @@ -422,11 +473,11 @@ png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp buffer[iout] = '\0'; } } -#endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */ +#endif /* WARNINGS || ERROR_TEXT */ #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) PNG_FUNCTION(void,PNGAPI -png_chunk_error,(png_structp png_ptr, png_const_charp error_message), +png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message), PNG_NORETURN) { char msg[18+PNG_MAX_ERROR_TEXT]; @@ -439,11 +490,11 @@ png_chunk_error,(png_structp png_ptr, png_const_charp error_message), png_error(png_ptr, msg); } } -#endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */ +#endif /* READ && ERROR_TEXT */ #ifdef PNG_WARNINGS_SUPPORTED void PNGAPI -png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) +png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message) { char msg[18+PNG_MAX_ERROR_TEXT]; if (png_ptr == NULL) @@ -455,38 +506,83 @@ png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) png_warning(png_ptr, msg); } } -#endif /* PNG_WARNINGS_SUPPORTED */ +#endif /* WARNINGS */ #ifdef PNG_READ_SUPPORTED #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI -png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message) +png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp + error_message) { - if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) + if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0) png_chunk_warning(png_ptr, error_message); else png_chunk_error(png_ptr, error_message); + +# ifndef PNG_ERROR_TEXT_SUPPORTED + PNG_UNUSED(error_message) +# endif } #endif -#endif /* PNG_READ_SUPPORTED */ +#endif /* READ */ + +void /* PRIVATE */ +png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error) +{ +# ifndef PNG_WARNINGS_SUPPORTED + PNG_UNUSED(message) +# endif + + /* This is always supported, but for just read or just write it + * unconditionally does the right thing. + */ +# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) +# endif + +# ifdef PNG_READ_SUPPORTED + { + if (error < PNG_CHUNK_ERROR) + png_chunk_warning(png_ptr, message); + + else + png_chunk_benign_error(png_ptr, message); + } +# endif + +# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) + else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) +# endif + +# ifdef PNG_WRITE_SUPPORTED + { + if (error < PNG_CHUNK_WRITE_ERROR) + png_app_warning(png_ptr, message); + + else + png_app_error(png_ptr, message); + } +# endif +} #ifdef PNG_ERROR_TEXT_SUPPORTED #ifdef PNG_FLOATING_POINT_SUPPORTED PNG_FUNCTION(void, -png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN) +png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN) { # define fixed_message "fixed point overflow in " # define fixed_message_ln ((sizeof fixed_message)-1) int iin; char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT]; - png_memcpy(msg, fixed_message, fixed_message_ln); + memcpy(msg, fixed_message, fixed_message_ln); iin = 0; - if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0) - { - msg[fixed_message_ln + iin] = name[iin]; - ++iin; - } + if (name != NULL) + while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0) + { + msg[fixed_message_ln + iin] = name[iin]; + ++iin; + } msg[fixed_message_ln + iin] = 0; png_error(png_ptr, msg); } @@ -498,14 +594,111 @@ png_fixed_error,(png_structp png_ptr, png_const_charp name),PNG_NORETURN) * otherwise it is necessary for png_default_error to be overridden. */ jmp_buf* PNGAPI -png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn, +png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn, size_t jmp_buf_size) { - if (png_ptr == NULL || jmp_buf_size != png_sizeof(jmp_buf)) + /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value + * and it must not change after that. Libpng doesn't care how big the + * buffer is, just that it doesn't change. + * + * If the buffer size is no *larger* than the size of jmp_buf when libpng is + * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0 + * semantics that this call will not fail. If the size is larger, however, + * the buffer is allocated and this may fail, causing the function to return + * NULL. + */ + if (png_ptr == NULL) return NULL; + if (png_ptr->jmp_buf_ptr == NULL) + { + png_ptr->jmp_buf_size = 0; /* not allocated */ + + if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local)) + png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; + + else + { + png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *, + png_malloc_warn(png_ptr, jmp_buf_size)); + + if (png_ptr->jmp_buf_ptr == NULL) + return NULL; /* new NULL return on OOM */ + + png_ptr->jmp_buf_size = jmp_buf_size; + } + } + + else /* Already allocated: check the size */ + { + size_t size = png_ptr->jmp_buf_size; + + if (size == 0) + { + size = (sizeof png_ptr->jmp_buf_local); + if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local) + { + /* This is an internal error in libpng: somehow we have been left + * with a stack allocated jmp_buf when the application regained + * control. It's always possible to fix this up, but for the moment + * this is a png_error because that makes it easy to detect. + */ + png_error(png_ptr, "Libpng jmp_buf still allocated"); + /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */ + } + } + + if (size != jmp_buf_size) + { + png_warning(png_ptr, "Application jmp_buf size changed"); + return NULL; /* caller will probably crash: no choice here */ + } + } + + /* Finally fill in the function, now we have a satisfactory buffer. It is + * valid to change the function on every call. + */ png_ptr->longjmp_fn = longjmp_fn; - return &png_ptr->longjmp_buffer; + return png_ptr->jmp_buf_ptr; +} + +void /* PRIVATE */ +png_free_jmpbuf(png_structrp png_ptr) +{ + if (png_ptr != NULL) + { + jmp_buf *jb = png_ptr->jmp_buf_ptr; + + /* A size of 0 is used to indicate a local, stack, allocation of the + * pointer; used here and in png.c + */ + if (jb != NULL && png_ptr->jmp_buf_size > 0) + { + + /* This stuff is so that a failure to free the error control structure + * does not leave libpng in a state with no valid error handling: the + * free always succeeds, if there is an error it gets ignored. + */ + if (jb != &png_ptr->jmp_buf_local) + { + /* Make an internal, libpng, jmp_buf to return here */ + jmp_buf free_jmp_buf; + + if (!setjmp(free_jmp_buf)) + { + png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */ + png_ptr->jmp_buf_size = 0; /* stack allocation */ + png_ptr->longjmp_fn = longjmp; + png_free(png_ptr, jb); /* Return to setjmp on error */ + } + } + } + + /* *Always* cancel everything out: */ + png_ptr->jmp_buf_size = 0; + png_ptr->jmp_buf_ptr = NULL; + png_ptr->longjmp_fn = 0; + } } #endif @@ -515,7 +708,7 @@ png_set_longjmp_fn(png_structp png_ptr, png_longjmp_ptr longjmp_fn, * error function pointer in png_set_error_fn(). */ static PNG_FUNCTION(void /* PRIVATE */, -png_default_error,(png_structp png_ptr, png_const_charp error_message), +png_default_error,(png_const_structrp png_ptr, png_const_charp error_message), PNG_NORETURN) { #ifdef PNG_CONSOLE_IO_SUPPORTED @@ -562,24 +755,23 @@ png_default_error,(png_structp png_ptr, png_const_charp error_message), } PNG_FUNCTION(void,PNGAPI -png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN) +png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN) { #ifdef PNG_SETJMP_SUPPORTED - if (png_ptr && png_ptr->longjmp_fn) - { -# ifdef USE_FAR_KEYWORD - { - jmp_buf tmp_jmpbuf; - png_memcpy(tmp_jmpbuf, png_ptr->longjmp_buffer, png_sizeof(jmp_buf)); - png_ptr->longjmp_fn(tmp_jmpbuf, val); - } - -# else - png_ptr->longjmp_fn(png_ptr->longjmp_buffer, val); -# endif - } + if (png_ptr != NULL && png_ptr->longjmp_fn != NULL && + png_ptr->jmp_buf_ptr != NULL) + png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val); +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(val) #endif - /* Here if not setjmp support or if png_ptr is null. */ + + /* If control reaches this point, png_longjmp() must not return. The only + * choice is to terminate the whole process (or maybe the thread); to do + * this the ANSI-C abort() function is used unless a different method is + * implemented by overriding the default configuration setting for + * PNG_ABORT(). + */ PNG_ABORT(); } @@ -590,7 +782,7 @@ png_longjmp,(png_structp png_ptr, int val),PNG_NORETURN) * not used, but it is passed in case it may be useful. */ static void /* PRIVATE */ -png_default_warning(png_structp png_ptr, png_const_charp warning_message) +png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message) { #ifdef PNG_CONSOLE_IO_SUPPORTED # ifdef PNG_ERROR_NUMBERS_SUPPORTED @@ -632,15 +824,15 @@ png_default_warning(png_structp png_ptr, png_const_charp warning_message) #endif PNG_UNUSED(png_ptr) /* Make compiler happy */ } -#endif /* PNG_WARNINGS_SUPPORTED */ +#endif /* WARNINGS */ /* This function is called when the application wants to use another method * of handling errors and warnings. Note that the error function MUST NOT * return to the calling routine or serious problems will occur. The return - * method used in the default routine calls longjmp(png_ptr->longjmp_buffer, 1) + * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1) */ void PNGAPI -png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, +png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn) { if (png_ptr == NULL) @@ -661,7 +853,7 @@ png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, * pointer before png_write_destroy and png_read_destroy are called. */ png_voidp PNGAPI -png_get_error_ptr(png_const_structp png_ptr) +png_get_error_ptr(png_const_structrp png_ptr) { if (png_ptr == NULL) return NULL; @@ -672,7 +864,7 @@ png_get_error_ptr(png_const_structp png_ptr) #ifdef PNG_ERROR_NUMBERS_SUPPORTED void PNGAPI -png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) +png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode) { if (png_ptr != NULL) { @@ -682,4 +874,90 @@ png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) } } #endif -#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ + +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) + /* Currently the above both depend on SETJMP_SUPPORTED, however it would be + * possible to implement without setjmp support just so long as there is some + * way to handle the error return here: + */ +PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI +png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message), + PNG_NORETURN) +{ + const png_const_structrp png_ptr = png_nonconst_ptr; + png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); + + /* An error is always logged here, overwriting anything (typically a warning) + * that is already there: + */ + if (image != NULL) + { + png_safecat(image->message, (sizeof image->message), 0, error_message); + image->warning_or_error |= PNG_IMAGE_ERROR; + + /* Retrieve the jmp_buf from within the png_control, making this work for + * C++ compilation too is pretty tricky: C++ wants a pointer to the first + * element of a jmp_buf, but C doesn't tell us the type of that. + */ + if (image->opaque != NULL && image->opaque->error_buf != NULL) + longjmp(png_control_jmp_buf(image->opaque), 1); + + /* Missing longjmp buffer, the following is to help debugging: */ + { + size_t pos = png_safecat(image->message, (sizeof image->message), 0, + "bad longjmp: "); + png_safecat(image->message, (sizeof image->message), pos, + error_message); + } + } + + /* Here on an internal programming error. */ + abort(); +} + +#ifdef PNG_WARNINGS_SUPPORTED +void /* PRIVATE */ PNGCBAPI +png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message) +{ + const png_const_structrp png_ptr = png_nonconst_ptr; + png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); + + /* A warning is only logged if there is no prior warning or error. */ + if (image->warning_or_error == 0) + { + png_safecat(image->message, (sizeof image->message), 0, warning_message); + image->warning_or_error |= PNG_IMAGE_WARNING; + } +} +#endif + +int /* PRIVATE */ +png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg) +{ + volatile png_imagep image = image_in; + volatile int result; + volatile png_voidp saved_error_buf; + jmp_buf safe_jmpbuf; + + /* Safely execute function(arg) with png_error returning to this function. */ + saved_error_buf = image->opaque->error_buf; + result = setjmp(safe_jmpbuf) == 0; + + if (result != 0) + { + + image->opaque->error_buf = safe_jmpbuf; + result = function(arg); + } + + image->opaque->error_buf = saved_error_buf; + + /* And do the cleanup prior to any failure return. */ + if (result == 0) + png_image_free(image); + + return result; +} +#endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */ +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/pngget.c b/3rdparty/libpng/pngget.c index 0e56124ba..743a6a9bb 100644 --- a/3rdparty/libpng/pngget.c +++ b/3rdparty/libpng/pngget.c @@ -1,8 +1,8 @@ /* pngget.c - retrieval of values from info struct * - * Last changed in libpng 1.5.7 [December 15, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.17 [March 26, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -17,7 +17,7 @@ #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) png_uint_32 PNGAPI -png_get_valid(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_valid(png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 flag) { if (png_ptr != NULL && info_ptr != NULL) @@ -27,7 +27,7 @@ png_get_valid(png_const_structp png_ptr, png_const_infop info_ptr, } png_size_t PNGAPI -png_get_rowbytes(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_rowbytes(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->rowbytes); @@ -37,7 +37,7 @@ png_get_rowbytes(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_INFO_IMAGE_SUPPORTED png_bytepp PNGAPI -png_get_rows(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_rows(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->row_pointers); @@ -49,7 +49,7 @@ png_get_rows(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_EASY_ACCESS_SUPPORTED /* Easy access to info, added in libpng-0.99 */ png_uint_32 PNGAPI -png_get_image_width(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_image_width(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->width; @@ -58,7 +58,7 @@ png_get_image_width(png_const_structp png_ptr, png_const_infop info_ptr) } png_uint_32 PNGAPI -png_get_image_height(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_image_height(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->height; @@ -67,7 +67,7 @@ png_get_image_height(png_const_structp png_ptr, png_const_infop info_ptr) } png_byte PNGAPI -png_get_bit_depth(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_bit_depth(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->bit_depth; @@ -76,7 +76,7 @@ png_get_bit_depth(png_const_structp png_ptr, png_const_infop info_ptr) } png_byte PNGAPI -png_get_color_type(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_color_type(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->color_type; @@ -85,7 +85,7 @@ png_get_color_type(png_const_structp png_ptr, png_const_infop info_ptr) } png_byte PNGAPI -png_get_filter_type(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_filter_type(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->filter_type; @@ -94,7 +94,7 @@ png_get_filter_type(png_const_structp png_ptr, png_const_infop info_ptr) } png_byte PNGAPI -png_get_interlace_type(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_interlace_type(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->interlace_type; @@ -103,7 +103,7 @@ png_get_interlace_type(png_const_structp png_ptr, png_const_infop info_ptr) } png_byte PNGAPI -png_get_compression_type(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_compression_type(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return info_ptr->compression_type; @@ -112,10 +112,12 @@ png_get_compression_type(png_const_structp png_ptr, png_const_infop info_ptr) } png_uint_32 PNGAPI -png_get_x_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_x_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp + info_ptr) { #ifdef PNG_pHYs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_x_pixels_per_meter"); @@ -123,16 +125,21 @@ png_get_x_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER) return (info_ptr->x_pixels_per_unit); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); } png_uint_32 PNGAPI -png_get_y_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_y_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp + info_ptr) { #ifdef PNG_pHYs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_y_pixels_per_meter"); @@ -140,16 +147,20 @@ png_get_y_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER) return (info_ptr->y_pixels_per_unit); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); } png_uint_32 PNGAPI -png_get_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp info_ptr) { #ifdef PNG_pHYs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter"); @@ -157,6 +168,9 @@ png_get_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) info_ptr->x_pixels_per_unit == info_ptr->y_pixels_per_unit) return (info_ptr->x_pixels_per_unit); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); @@ -164,10 +178,12 @@ png_get_pixels_per_meter(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_FLOATING_POINT_SUPPORTED float PNGAPI -png_get_pixel_aspect_ratio(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_pixel_aspect_ratio(png_const_structrp png_ptr, png_const_inforp + info_ptr) { #ifdef PNG_READ_pHYs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio"); @@ -175,6 +191,9 @@ png_get_pixel_aspect_ratio(png_const_structp png_ptr, png_const_infop info_ptr) return ((float)((float)info_ptr->y_pixels_per_unit /(float)info_ptr->x_pixels_per_unit)); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return ((float)0.0); @@ -183,14 +202,15 @@ png_get_pixel_aspect_ratio(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_FIXED_POINT_SUPPORTED png_fixed_point PNGAPI -png_get_pixel_aspect_ratio_fixed(png_const_structp png_ptr, - png_const_infop info_ptr) +png_get_pixel_aspect_ratio_fixed(png_const_structrp png_ptr, + png_const_inforp info_ptr) { #ifdef PNG_READ_pHYs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) - && info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0 - && info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX - && info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0 && + info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0 && + info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX && + info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX) { png_fixed_point res; @@ -200,9 +220,12 @@ png_get_pixel_aspect_ratio_fixed(png_const_structp png_ptr, * range of 0..2^31-1; otherwise the cast might overflow. */ if (png_muldiv(&res, (png_int_32)info_ptr->y_pixels_per_unit, PNG_FP_1, - (png_int_32)info_ptr->x_pixels_per_unit)) + (png_int_32)info_ptr->x_pixels_per_unit) != 0) return res; } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return 0; @@ -210,64 +233,80 @@ png_get_pixel_aspect_ratio_fixed(png_const_structp png_ptr, #endif png_int_32 PNGAPI -png_get_x_offset_microns(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_x_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr) { #ifdef PNG_oFFs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_oFFs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns"); if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER) return (info_ptr->x_offset); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); } png_int_32 PNGAPI -png_get_y_offset_microns(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_y_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr) { #ifdef PNG_oFFs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_oFFs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns"); if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER) return (info_ptr->y_offset); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); } png_int_32 PNGAPI -png_get_x_offset_pixels(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_x_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr) { #ifdef PNG_oFFs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_oFFs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels"); if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL) return (info_ptr->x_offset); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); } png_int_32 PNGAPI -png_get_y_offset_pixels(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_y_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr) { #ifdef PNG_oFFs_SUPPORTED - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_oFFs) != 0) { png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels"); if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL) return (info_ptr->y_offset); } +#else + PNG_UNUSED(png_ptr) + PNG_UNUSED(info_ptr) #endif return (0); @@ -298,7 +337,7 @@ ppi_from_ppm(png_uint_32 ppm) */ png_fixed_point result; if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127, - 5000)) + 5000) != 0) return result; /* Overflow. */ @@ -307,26 +346,26 @@ ppi_from_ppm(png_uint_32 ppm) } png_uint_32 PNGAPI -png_get_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) { return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr)); } png_uint_32 PNGAPI -png_get_x_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_x_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) { return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr)); } png_uint_32 PNGAPI -png_get_y_pixels_per_inch(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_y_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) { return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr)); } #ifdef PNG_FIXED_POINT_SUPPORTED static png_fixed_point -png_fixed_inches_from_microns(png_structp png_ptr, png_int_32 microns) +png_fixed_inches_from_microns(png_const_structrp png_ptr, png_int_32 microns) { /* Convert from metres * 1,000,000 to inches * 100,000, meters to * inches is simply *(100/2.54), so we want *(10/2.54) == 500/127. @@ -337,8 +376,8 @@ png_fixed_inches_from_microns(png_structp png_ptr, png_int_32 microns) } png_fixed_point PNGAPI -png_get_x_offset_inches_fixed(png_structp png_ptr, - png_const_infop info_ptr) +png_get_x_offset_inches_fixed(png_const_structrp png_ptr, + png_const_inforp info_ptr) { return png_fixed_inches_from_microns(png_ptr, png_get_x_offset_microns(png_ptr, info_ptr)); @@ -347,8 +386,8 @@ png_get_x_offset_inches_fixed(png_structp png_ptr, #ifdef PNG_FIXED_POINT_SUPPORTED png_fixed_point PNGAPI -png_get_y_offset_inches_fixed(png_structp png_ptr, - png_const_infop info_ptr) +png_get_y_offset_inches_fixed(png_const_structrp png_ptr, + png_const_inforp info_ptr) { return png_fixed_inches_from_microns(png_ptr, png_get_y_offset_microns(png_ptr, info_ptr)); @@ -357,7 +396,7 @@ png_get_y_offset_inches_fixed(png_structp png_ptr, #ifdef PNG_FLOATING_POINT_SUPPORTED float PNGAPI -png_get_x_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_x_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr) { /* To avoid the overflow do the conversion directly in floating * point. @@ -368,7 +407,7 @@ png_get_x_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_FLOATING_POINT_SUPPORTED float PNGAPI -png_get_y_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_y_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr) { /* To avoid the overflow do the conversion directly in floating * point. @@ -379,12 +418,13 @@ png_get_y_offset_inches(png_const_structp png_ptr, png_const_infop info_ptr) #ifdef PNG_pHYs_SUPPORTED png_uint_32 PNGAPI -png_get_pHYs_dpi(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_pHYs_dpi(png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) { png_uint_32 retval = 0; - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pHYs) != 0) { png_debug1(1, "in %s retrieval function", "pHYs"); @@ -415,15 +455,16 @@ png_get_pHYs_dpi(png_const_structp png_ptr, png_const_infop info_ptr, return (retval); } -#endif /* PNG_pHYs_SUPPORTED */ -#endif /* PNG_INCH_CONVERSIONS_SUPPORTED */ +#endif /* pHYs */ +#endif /* INCH_CONVERSIONS */ /* png_get_channels really belongs in here, too, but it's been around longer */ -#endif /* PNG_EASY_ACCESS_SUPPORTED */ +#endif /* EASY_ACCESS */ + png_byte PNGAPI -png_get_channels(png_const_structp png_ptr, png_const_infop info_ptr) +png_get_channels(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->channels); @@ -431,22 +472,25 @@ png_get_channels(png_const_structp png_ptr, png_const_infop info_ptr) return (0); } +#ifdef PNG_READ_SUPPORTED png_const_bytep PNGAPI -png_get_signature(png_const_structp png_ptr, png_infop info_ptr) +png_get_signature(png_const_structrp png_ptr, png_const_inforp info_ptr) { if (png_ptr != NULL && info_ptr != NULL) return(info_ptr->signature); return (NULL); } +#endif #ifdef PNG_bKGD_SUPPORTED png_uint_32 PNGAPI -png_get_bKGD(png_const_structp png_ptr, png_infop info_ptr, +png_get_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, png_color_16p *background) { - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) - && background != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_bKGD) != 0 && + background != NULL) { png_debug1(1, "in %s retrieval function", "bKGD"); @@ -463,87 +507,47 @@ png_get_bKGD(png_const_structp png_ptr, png_infop info_ptr, * same time to correct the rgb grayscale coefficient defaults obtained from the * cHRM chunk in 1.5.4 */ -png_uint_32 PNGFAPI -png_get_cHRM_XYZ_fixed(png_structp png_ptr, png_const_infop info_ptr, - png_fixed_point *int_red_X, png_fixed_point *int_red_Y, - png_fixed_point *int_red_Z, png_fixed_point *int_green_X, - png_fixed_point *int_green_Y, png_fixed_point *int_green_Z, - png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y, - png_fixed_point *int_blue_Z) -{ - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) - { - png_xy xy; - png_XYZ XYZ; - - png_debug1(1, "in %s retrieval function", "cHRM_XYZ"); - - xy.whitex = info_ptr->x_white; - xy.whitey = info_ptr->y_white; - xy.redx = info_ptr->x_red; - xy.redy = info_ptr->y_red; - xy.greenx = info_ptr->x_green; - xy.greeny = info_ptr->y_green; - xy.bluex = info_ptr->x_blue; - xy.bluey = info_ptr->y_blue; - - /* The *_checked function handles error reporting, so just return 0 if - * there is a failure here. - */ - if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy)) - { - if (int_red_X != NULL) - *int_red_X = XYZ.redX; - if (int_red_Y != NULL) - *int_red_Y = XYZ.redY; - if (int_red_Z != NULL) - *int_red_Z = XYZ.redZ; - if (int_green_X != NULL) - *int_green_X = XYZ.greenX; - if (int_green_Y != NULL) - *int_green_Y = XYZ.greenY; - if (int_green_Z != NULL) - *int_green_Z = XYZ.greenZ; - if (int_blue_X != NULL) - *int_blue_X = XYZ.blueX; - if (int_blue_Y != NULL) - *int_blue_Y = XYZ.blueY; - if (int_blue_Z != NULL) - *int_blue_Z = XYZ.blueZ; - - return (PNG_INFO_cHRM); - } - } - - return (0); -} - # ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI -png_get_cHRM(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_cHRM(png_const_structrp png_ptr, png_const_inforp info_ptr, double *white_x, double *white_y, double *red_x, double *red_y, double *green_x, double *green_y, double *blue_x, double *blue_y) { - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) + /* Quiet API change: this code used to only return the end points if a cHRM + * chunk was present, but the end points can also come from iCCP or sRGB + * chunks, so in 1.6.0 the png_get_ APIs return the end points regardless and + * the png_set_ APIs merely check that set end points are mutually + * consistent. + */ + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) { png_debug1(1, "in %s retrieval function", "cHRM"); if (white_x != NULL) - *white_x = png_float(png_ptr, info_ptr->x_white, "cHRM white X"); + *white_x = png_float(png_ptr, + info_ptr->colorspace.end_points_xy.whitex, "cHRM white X"); if (white_y != NULL) - *white_y = png_float(png_ptr, info_ptr->y_white, "cHRM white Y"); + *white_y = png_float(png_ptr, + info_ptr->colorspace.end_points_xy.whitey, "cHRM white Y"); if (red_x != NULL) - *red_x = png_float(png_ptr, info_ptr->x_red, "cHRM red X"); + *red_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redx, + "cHRM red X"); if (red_y != NULL) - *red_y = png_float(png_ptr, info_ptr->y_red, "cHRM red Y"); + *red_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redy, + "cHRM red Y"); if (green_x != NULL) - *green_x = png_float(png_ptr, info_ptr->x_green, "cHRM green X"); + *green_x = png_float(png_ptr, + info_ptr->colorspace.end_points_xy.greenx, "cHRM green X"); if (green_y != NULL) - *green_y = png_float(png_ptr, info_ptr->y_green, "cHRM green Y"); + *green_y = png_float(png_ptr, + info_ptr->colorspace.end_points_xy.greeny, "cHRM green Y"); if (blue_x != NULL) - *blue_x = png_float(png_ptr, info_ptr->x_blue, "cHRM blue X"); + *blue_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluex, + "cHRM blue X"); if (blue_y != NULL) - *blue_y = png_float(png_ptr, info_ptr->y_blue, "cHRM blue Y"); + *blue_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluey, + "cHRM blue Y"); return (PNG_INFO_cHRM); } @@ -551,35 +555,43 @@ png_get_cHRM(png_const_structp png_ptr, png_const_infop info_ptr, } png_uint_32 PNGAPI -png_get_cHRM_XYZ(png_structp png_ptr, png_const_infop info_ptr, +png_get_cHRM_XYZ(png_const_structrp png_ptr, png_const_inforp info_ptr, double *red_X, double *red_Y, double *red_Z, double *green_X, double *green_Y, double *green_Z, double *blue_X, double *blue_Y, double *blue_Z) { - png_XYZ XYZ; - - if (png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, - &XYZ.redX, &XYZ.redY, &XYZ.redZ, &XYZ.greenX, &XYZ.greenY, &XYZ.greenZ, - &XYZ.blueX, &XYZ.blueY, &XYZ.blueZ) & PNG_INFO_cHRM) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) { + png_debug1(1, "in %s retrieval function", "cHRM_XYZ(float)"); + if (red_X != NULL) - *red_X = png_float(png_ptr, XYZ.redX, "cHRM red X"); + *red_X = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_X, + "cHRM red X"); if (red_Y != NULL) - *red_Y = png_float(png_ptr, XYZ.redY, "cHRM red Y"); + *red_Y = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Y, + "cHRM red Y"); if (red_Z != NULL) - *red_Z = png_float(png_ptr, XYZ.redZ, "cHRM red Z"); + *red_Z = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Z, + "cHRM red Z"); if (green_X != NULL) - *green_X = png_float(png_ptr, XYZ.greenX, "cHRM green X"); + *green_X = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.green_X, "cHRM green X"); if (green_Y != NULL) - *green_Y = png_float(png_ptr, XYZ.greenY, "cHRM green Y"); + *green_Y = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.green_Y, "cHRM green Y"); if (green_Z != NULL) - *green_Z = png_float(png_ptr, XYZ.greenZ, "cHRM green Z"); + *green_Z = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.green_Z, "cHRM green Z"); if (blue_X != NULL) - *blue_X = png_float(png_ptr, XYZ.blueX, "cHRM blue X"); + *blue_X = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.blue_X, "cHRM blue X"); if (blue_Y != NULL) - *blue_Y = png_float(png_ptr, XYZ.blueY, "cHRM blue Y"); + *blue_Y = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.blue_Y, "cHRM blue Y"); if (blue_Z != NULL) - *blue_Z = png_float(png_ptr, XYZ.blueZ, "cHRM blue Z"); + *blue_Z = png_float(png_ptr, + info_ptr->colorspace.end_points_XYZ.blue_Z, "cHRM blue Z"); return (PNG_INFO_cHRM); } @@ -589,31 +601,69 @@ png_get_cHRM_XYZ(png_structp png_ptr, png_const_infop info_ptr, # ifdef PNG_FIXED_POINT_SUPPORTED png_uint_32 PNGAPI -png_get_cHRM_fixed(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, + png_fixed_point *int_red_X, png_fixed_point *int_red_Y, + png_fixed_point *int_red_Z, png_fixed_point *int_green_X, + png_fixed_point *int_green_Y, png_fixed_point *int_green_Z, + png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y, + png_fixed_point *int_blue_Z) +{ + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) + { + png_debug1(1, "in %s retrieval function", "cHRM_XYZ"); + + if (int_red_X != NULL) + *int_red_X = info_ptr->colorspace.end_points_XYZ.red_X; + if (int_red_Y != NULL) + *int_red_Y = info_ptr->colorspace.end_points_XYZ.red_Y; + if (int_red_Z != NULL) + *int_red_Z = info_ptr->colorspace.end_points_XYZ.red_Z; + if (int_green_X != NULL) + *int_green_X = info_ptr->colorspace.end_points_XYZ.green_X; + if (int_green_Y != NULL) + *int_green_Y = info_ptr->colorspace.end_points_XYZ.green_Y; + if (int_green_Z != NULL) + *int_green_Z = info_ptr->colorspace.end_points_XYZ.green_Z; + if (int_blue_X != NULL) + *int_blue_X = info_ptr->colorspace.end_points_XYZ.blue_X; + if (int_blue_Y != NULL) + *int_blue_Y = info_ptr->colorspace.end_points_XYZ.blue_Y; + if (int_blue_Z != NULL) + *int_blue_Z = info_ptr->colorspace.end_points_XYZ.blue_Z; + return (PNG_INFO_cHRM); + } + + return (0); +} + +png_uint_32 PNGAPI +png_get_cHRM_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x, png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y, png_fixed_point *blue_x, png_fixed_point *blue_y) { png_debug1(1, "in %s retrieval function", "cHRM"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) { if (white_x != NULL) - *white_x = info_ptr->x_white; + *white_x = info_ptr->colorspace.end_points_xy.whitex; if (white_y != NULL) - *white_y = info_ptr->y_white; + *white_y = info_ptr->colorspace.end_points_xy.whitey; if (red_x != NULL) - *red_x = info_ptr->x_red; + *red_x = info_ptr->colorspace.end_points_xy.redx; if (red_y != NULL) - *red_y = info_ptr->y_red; + *red_y = info_ptr->colorspace.end_points_xy.redy; if (green_x != NULL) - *green_x = info_ptr->x_green; + *green_x = info_ptr->colorspace.end_points_xy.greenx; if (green_y != NULL) - *green_y = info_ptr->y_green; + *green_y = info_ptr->colorspace.end_points_xy.greeny; if (blue_x != NULL) - *blue_x = info_ptr->x_blue; + *blue_x = info_ptr->colorspace.end_points_xy.bluex; if (blue_y != NULL) - *blue_y = info_ptr->y_blue; + *blue_y = info_ptr->colorspace.end_points_xy.bluey; return (PNG_INFO_cHRM); } @@ -623,49 +673,57 @@ png_get_cHRM_fixed(png_const_structp png_ptr, png_const_infop info_ptr, #endif #ifdef PNG_gAMA_SUPPORTED -png_uint_32 PNGFAPI -png_get_gAMA_fixed(png_const_structp png_ptr, png_const_infop info_ptr, +# ifdef PNG_FIXED_POINT_SUPPORTED +png_uint_32 PNGAPI +png_get_gAMA_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, png_fixed_point *file_gamma) { png_debug1(1, "in %s retrieval function", "gAMA"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) - && file_gamma != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && + file_gamma != NULL) { - *file_gamma = info_ptr->gamma; + *file_gamma = info_ptr->colorspace.gamma; return (PNG_INFO_gAMA); } return (0); } +# endif + # ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI -png_get_gAMA(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_gAMA(png_const_structrp png_ptr, png_const_inforp info_ptr, double *file_gamma) { - png_fixed_point igamma; - png_uint_32 ok = png_get_gAMA_fixed(png_ptr, info_ptr, &igamma); + png_debug1(1, "in %s retrieval function", "gAMA(float)"); - if (ok) - *file_gamma = png_float(png_ptr, igamma, "png_get_gAMA"); + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && + file_gamma != NULL) + { + *file_gamma = png_float(png_ptr, info_ptr->colorspace.gamma, + "png_get_gAMA"); + return (PNG_INFO_gAMA); + } - return ok; + return (0); } - # endif #endif #ifdef PNG_sRGB_SUPPORTED png_uint_32 PNGAPI -png_get_sRGB(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_sRGB(png_const_structrp png_ptr, png_const_inforp info_ptr, int *file_srgb_intent) { png_debug1(1, "in %s retrieval function", "sRGB"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB) - && file_srgb_intent != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_sRGB) != 0 && file_srgb_intent != NULL) { - *file_srgb_intent = (int)info_ptr->srgb_intent; + *file_srgb_intent = info_ptr->colorspace.rendering_intent; return (PNG_INFO_sRGB); } @@ -675,23 +733,24 @@ png_get_sRGB(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_iCCP_SUPPORTED png_uint_32 PNGAPI -png_get_iCCP(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_iCCP(png_const_structrp png_ptr, png_inforp info_ptr, png_charpp name, int *compression_type, png_bytepp profile, png_uint_32 *proflen) { png_debug1(1, "in %s retrieval function", "iCCP"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP) - && name != NULL && compression_type != NULL && profile != NULL && - proflen != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_iCCP) != 0 && + name != NULL && compression_type != NULL && profile != NULL && + proflen != NULL) { *name = info_ptr->iccp_name; *profile = info_ptr->iccp_profile; - /* Compression_type is a dummy so the API won't have to change - * if we introduce multiple compression types later. + *proflen = png_get_uint_32(info_ptr->iccp_profile); + /* This is somewhat irrelevant since the profile data returned has + * actually been uncompressed. */ - *proflen = info_ptr->iccp_proflen; - *compression_type = info_ptr->iccp_compression; + *compression_type = PNG_COMPRESSION_TYPE_BASE; return (PNG_INFO_iCCP); } @@ -700,14 +759,14 @@ png_get_iCCP(png_const_structp png_ptr, png_const_infop info_ptr, #endif #ifdef PNG_sPLT_SUPPORTED -png_uint_32 PNGAPI -png_get_sPLT(png_const_structp png_ptr, png_const_infop info_ptr, +int PNGAPI +png_get_sPLT(png_const_structrp png_ptr, png_inforp info_ptr, png_sPLT_tpp spalettes) { if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL) { *spalettes = info_ptr->splt_palettes; - return ((png_uint_32)info_ptr->splt_palettes_num); + return info_ptr->splt_palettes_num; } return (0); @@ -716,13 +775,13 @@ png_get_sPLT(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_hIST_SUPPORTED png_uint_32 PNGAPI -png_get_hIST(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_16p *hist) { png_debug1(1, "in %s retrieval function", "hIST"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) - && hist != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_hIST) != 0 && hist != NULL) { *hist = info_ptr->hist; return (PNG_INFO_hIST); @@ -733,22 +792,27 @@ png_get_hIST(png_const_structp png_ptr, png_const_infop info_ptr, #endif png_uint_32 PNGAPI -png_get_IHDR(png_structp png_ptr, png_infop info_ptr, +png_get_IHDR(png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_type, int *compression_type, int *filter_type) - { png_debug1(1, "in %s retrieval function", "IHDR"); - if (png_ptr == NULL || info_ptr == NULL || width == NULL || - height == NULL || bit_depth == NULL || color_type == NULL) + if (png_ptr == NULL || info_ptr == NULL) return (0); - *width = info_ptr->width; - *height = info_ptr->height; - *bit_depth = info_ptr->bit_depth; - *color_type = info_ptr->color_type; + if (width != NULL) + *width = info_ptr->width; + + if (height != NULL) + *height = info_ptr->height; + + if (bit_depth != NULL) + *bit_depth = info_ptr->bit_depth; + + if (color_type != NULL) + *color_type = info_ptr->color_type; if (compression_type != NULL) *compression_type = info_ptr->compression_type; @@ -764,7 +828,7 @@ png_get_IHDR(png_structp png_ptr, png_infop info_ptr, * application has ignored our advice not to mess with the members * of info_ptr directly. */ - png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height, + png_check_IHDR(png_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type, info_ptr->compression_type, info_ptr->filter_type); @@ -773,13 +837,14 @@ png_get_IHDR(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_oFFs_SUPPORTED png_uint_32 PNGAPI -png_get_oFFs(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_oFFs(png_const_structrp png_ptr, png_const_inforp info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type) { png_debug1(1, "in %s retrieval function", "oFFs"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) - && offset_x != NULL && offset_y != NULL && unit_type != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_oFFs) != 0 && + offset_x != NULL && offset_y != NULL && unit_type != NULL) { *offset_x = info_ptr->x_offset; *offset_y = info_ptr->y_offset; @@ -793,14 +858,15 @@ png_get_oFFs(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_pCAL_SUPPORTED png_uint_32 PNGAPI -png_get_pCAL(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_pCAL(png_const_structrp png_ptr, png_inforp info_ptr, png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams, png_charp *units, png_charpp *params) { png_debug1(1, "in %s retrieval function", "pCAL"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) - && purpose != NULL && X0 != NULL && X1 != NULL && type != NULL && + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_pCAL) != 0 && + purpose != NULL && X0 != NULL && X1 != NULL && type != NULL && nparams != NULL && units != NULL && params != NULL) { *purpose = info_ptr->pcal_purpose; @@ -819,16 +885,20 @@ png_get_pCAL(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_sCAL_SUPPORTED # ifdef PNG_FIXED_POINT_SUPPORTED -# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED +# if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \ + defined(PNG_FLOATING_POINT_SUPPORTED) png_uint_32 PNGAPI -png_get_sCAL_fixed(png_structp png_ptr, png_const_infop info_ptr, +png_get_sCAL_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, png_fixed_point *width, png_fixed_point *height) { if (png_ptr != NULL && info_ptr != NULL && - (info_ptr->valid & PNG_INFO_sCAL)) + (info_ptr->valid & PNG_INFO_sCAL) != 0) { *unit = info_ptr->scal_unit; - /*TODO: make this work without FP support */ + /*TODO: make this work without FP support; the API is currently eliminated + * if neither floating point APIs nor internal floating point arithmetic + * are enabled. + */ *width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width"); *height = png_fixed(png_ptr, atof(info_ptr->scal_s_height), "sCAL height"); @@ -841,11 +911,11 @@ png_get_sCAL_fixed(png_structp png_ptr, png_const_infop info_ptr, # endif /* FIXED_POINT */ # ifdef PNG_FLOATING_POINT_SUPPORTED png_uint_32 PNGAPI -png_get_sCAL(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_sCAL(png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, double *width, double *height) { if (png_ptr != NULL && info_ptr != NULL && - (info_ptr->valid & PNG_INFO_sCAL)) + (info_ptr->valid & PNG_INFO_sCAL) != 0) { *unit = info_ptr->scal_unit; *width = atof(info_ptr->scal_s_width); @@ -857,11 +927,11 @@ png_get_sCAL(png_const_structp png_ptr, png_const_infop info_ptr, } # endif /* FLOATING POINT */ png_uint_32 PNGAPI -png_get_sCAL_s(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_sCAL_s(png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, png_charpp width, png_charpp height) { if (png_ptr != NULL && info_ptr != NULL && - (info_ptr->valid & PNG_INFO_sCAL)) + (info_ptr->valid & PNG_INFO_sCAL) != 0) { *unit = info_ptr->scal_unit; *width = info_ptr->scal_s_width; @@ -875,7 +945,7 @@ png_get_sCAL_s(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_pHYs_SUPPORTED png_uint_32 PNGAPI -png_get_pHYs(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_pHYs(png_const_structrp png_ptr, png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) { png_uint_32 retval = 0; @@ -883,7 +953,7 @@ png_get_pHYs(png_const_structp png_ptr, png_const_infop info_ptr, png_debug1(1, "in %s retrieval function", "pHYs"); if (png_ptr != NULL && info_ptr != NULL && - (info_ptr->valid & PNG_INFO_pHYs)) + (info_ptr->valid & PNG_INFO_pHYs) != 0) { if (res_x != NULL) { @@ -909,13 +979,13 @@ png_get_pHYs(png_const_structp png_ptr, png_const_infop info_ptr, #endif /* pHYs */ png_uint_32 PNGAPI -png_get_PLTE(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_PLTE(png_const_structrp png_ptr, png_inforp info_ptr, png_colorp *palette, int *num_palette) { png_debug1(1, "in %s retrieval function", "PLTE"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_PLTE) - && palette != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_PLTE) != 0 && palette != NULL) { *palette = info_ptr->palette; *num_palette = info_ptr->num_palette; @@ -928,13 +998,13 @@ png_get_PLTE(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_sBIT_SUPPORTED png_uint_32 PNGAPI -png_get_sBIT(png_const_structp png_ptr, png_infop info_ptr, +png_get_sBIT(png_const_structrp png_ptr, png_inforp info_ptr, png_color_8p *sig_bit) { png_debug1(1, "in %s retrieval function", "sBIT"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) - && sig_bit != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_sBIT) != 0 && sig_bit != NULL) { *sig_bit = &(info_ptr->sig_bit); return (PNG_INFO_sBIT); @@ -945,8 +1015,8 @@ png_get_sBIT(png_const_structp png_ptr, png_infop info_ptr, #endif #ifdef PNG_TEXT_SUPPORTED -png_uint_32 PNGAPI -png_get_text(png_const_structp png_ptr, png_const_infop info_ptr, +int PNGAPI +png_get_text(png_const_structrp png_ptr, png_inforp info_ptr, png_textp *text_ptr, int *num_text) { if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0) @@ -960,7 +1030,7 @@ png_get_text(png_const_structp png_ptr, png_const_infop info_ptr, if (num_text != NULL) *num_text = info_ptr->num_text; - return ((png_uint_32)info_ptr->num_text); + return info_ptr->num_text; } if (num_text != NULL) @@ -972,12 +1042,13 @@ png_get_text(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_tIME_SUPPORTED png_uint_32 PNGAPI -png_get_tIME(png_const_structp png_ptr, png_infop info_ptr, png_timep *mod_time) +png_get_tIME(png_const_structrp png_ptr, png_inforp info_ptr, + png_timep *mod_time) { png_debug1(1, "in %s retrieval function", "tIME"); - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) - && mod_time != NULL) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_tIME) != 0 && mod_time != NULL) { *mod_time = &(info_ptr->mod_time); return (PNG_INFO_tIME); @@ -989,11 +1060,12 @@ png_get_tIME(png_const_structp png_ptr, png_infop info_ptr, png_timep *mod_time) #ifdef PNG_tRNS_SUPPORTED png_uint_32 PNGAPI -png_get_tRNS(png_const_structp png_ptr, png_infop info_ptr, +png_get_tRNS(png_const_structrp png_ptr, png_inforp info_ptr, png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color) { png_uint_32 retval = 0; - if (png_ptr != NULL && info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) + if (png_ptr != NULL && info_ptr != NULL && + (info_ptr->valid & PNG_INFO_tRNS) != 0) { png_debug1(1, "in %s retrieval function", "tRNS"); @@ -1032,9 +1104,9 @@ png_get_tRNS(png_const_structp png_ptr, png_infop info_ptr, } #endif -#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED int PNGAPI -png_get_unknown_chunks(png_const_structp png_ptr, png_const_infop info_ptr, +png_get_unknown_chunks(png_const_structrp png_ptr, png_inforp info_ptr, png_unknown_chunkpp unknowns) { if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL) @@ -1049,7 +1121,7 @@ png_get_unknown_chunks(png_const_structp png_ptr, png_const_infop info_ptr, #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED png_byte PNGAPI -png_get_rgb_to_gray_status (png_const_structp png_ptr) +png_get_rgb_to_gray_status (png_const_structrp png_ptr) { return (png_byte)(png_ptr ? png_ptr->rgb_to_gray_status : 0); } @@ -1057,68 +1129,91 @@ png_get_rgb_to_gray_status (png_const_structp png_ptr) #ifdef PNG_USER_CHUNKS_SUPPORTED png_voidp PNGAPI -png_get_user_chunk_ptr(png_const_structp png_ptr) +png_get_user_chunk_ptr(png_const_structrp png_ptr) { return (png_ptr ? png_ptr->user_chunk_ptr : NULL); } #endif png_size_t PNGAPI -png_get_compression_buffer_size(png_const_structp png_ptr) +png_get_compression_buffer_size(png_const_structrp png_ptr) { - return (png_ptr ? png_ptr->zbuf_size : 0); + if (png_ptr == NULL) + return 0; + +#ifdef PNG_WRITE_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) +#endif + { +#ifdef PNG_SEQUENTIAL_READ_SUPPORTED + return png_ptr->IDAT_read_size; +#else + return PNG_IDAT_READ_SIZE; +#endif + } + +#ifdef PNG_WRITE_SUPPORTED + else + return png_ptr->zbuffer_size; +#endif } #ifdef PNG_SET_USER_LIMITS_SUPPORTED /* These functions were added to libpng 1.2.6 and were enabled * by default in libpng-1.4.0 */ png_uint_32 PNGAPI -png_get_user_width_max (png_const_structp png_ptr) +png_get_user_width_max (png_const_structrp png_ptr) { return (png_ptr ? png_ptr->user_width_max : 0); } png_uint_32 PNGAPI -png_get_user_height_max (png_const_structp png_ptr) +png_get_user_height_max (png_const_structrp png_ptr) { return (png_ptr ? png_ptr->user_height_max : 0); } /* This function was added to libpng 1.4.0 */ png_uint_32 PNGAPI -png_get_chunk_cache_max (png_const_structp png_ptr) +png_get_chunk_cache_max (png_const_structrp png_ptr) { return (png_ptr ? png_ptr->user_chunk_cache_max : 0); } /* This function was added to libpng 1.4.1 */ png_alloc_size_t PNGAPI -png_get_chunk_malloc_max (png_const_structp png_ptr) +png_get_chunk_malloc_max (png_const_structrp png_ptr) { return (png_ptr ? png_ptr->user_chunk_malloc_max : 0); } -#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ +#endif /* SET_USER_LIMITS */ /* These functions were added to libpng 1.4.0 */ #ifdef PNG_IO_STATE_SUPPORTED png_uint_32 PNGAPI -png_get_io_state (png_structp png_ptr) +png_get_io_state (png_const_structrp png_ptr) { return png_ptr->io_state; } png_uint_32 PNGAPI -png_get_io_chunk_type (png_const_structp png_ptr) +png_get_io_chunk_type (png_const_structrp png_ptr) { return png_ptr->chunk_name; } +#endif /* IO_STATE */ -png_const_bytep PNGAPI -png_get_io_chunk_name (png_structp png_ptr) +#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED +# ifdef PNG_GET_PALETTE_MAX_SUPPORTED +int PNGAPI +png_get_palette_max(png_const_structp png_ptr, png_const_infop info_ptr) { - PNG_CSTRING_FROM_CHUNK(png_ptr->io_chunk_string, png_ptr->chunk_name); - return png_ptr->io_chunk_string; -} -#endif /* ?PNG_IO_STATE_SUPPORTED */ + if (png_ptr != NULL && info_ptr != NULL) + return png_ptr->num_palette_max; -#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ + return (-1); +} +# endif +#endif + +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/pnginfo.h b/3rdparty/libpng/pnginfo.h index a33bfab06..c8c874dd1 100644 --- a/3rdparty/libpng/pnginfo.h +++ b/3rdparty/libpng/pnginfo.h @@ -1,12 +1,11 @@ /* pnginfo.h - header file for PNG reference library * - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.1 [March 28, 2013] + * Copyright (c) 1998-2013 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * - * Last changed in libpng 1.5.0 [January 6, 2011] - * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h @@ -55,7 +54,7 @@ struct png_info_def { - /* the following are necessary for every PNG file */ + /* The following are necessary for every PNG file */ png_uint_32 width; /* width of image in pixels (from IHDR) */ png_uint_32 height; /* height of image in pixels (from IHDR) */ png_uint_32 valid; /* valid chunk data (see PNG_INFO_ below) */ @@ -70,11 +69,17 @@ struct png_info_def png_byte filter_type; /* must be PNG_FILTER_TYPE_BASE (from IHDR) */ png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ - /* The following is informational only on read, and not used on writes. */ + /* The following are set by png_set_IHDR, called from the application on + * write, but the are never actually used by the write code. + */ png_byte channels; /* number of data channels per pixel (1, 2, 3, 4) */ png_byte pixel_depth; /* number of bits per pixel */ png_byte spare_byte; /* to align the data, and for future use */ + +#ifdef PNG_READ_SUPPORTED + /* This is never set during write */ png_byte signature[8]; /* magic bytes read by libpng from start of file */ +#endif /* The rest of the data is optional. If you are reading, check the * valid field to see if the information in these are valid. If you @@ -82,18 +87,25 @@ struct png_info_def * and initialize the appropriate fields below. */ -#if defined(PNG_gAMA_SUPPORTED) - /* The gAMA chunk describes the gamma characteristics of the system - * on which the image was created, normally in the range [1.0, 2.5]. - * Data is valid if (valid & PNG_INFO_gAMA) is non-zero. +#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) + /* png_colorspace only contains 'flags' if neither GAMMA or COLORSPACE are + * defined. When COLORSPACE is switched on all the colorspace-defining + * chunks should be enabled, when GAMMA is switched on all the gamma-defining + * chunks should be enabled. If this is not done it becomes possible to read + * inconsistent PNG files and assign a probably incorrect interpretation to + * the information. (In other words, by carefully choosing which chunks to + * recognize the system configuration can select an interpretation for PNG + * files containing ambiguous data and this will result in inconsistent + * behavior between different libpng builds!) */ - png_fixed_point gamma; + png_colorspace colorspace; #endif -#ifdef PNG_sRGB_SUPPORTED - /* GR-P, 0.96a */ - /* Data valid if (valid & PNG_INFO_sRGB) non-zero. */ - png_byte srgb_intent; /* sRGB rendering intent [0, 1, 2, or 3] */ +#ifdef PNG_iCCP_SUPPORTED + /* iCCP chunk data. */ + png_charp iccp_name; /* profile name */ + png_bytep iccp_profile; /* International Color Consortium profile data */ + png_uint_32 iccp_proflen; /* ICC profile data length */ #endif #ifdef PNG_TEXT_SUPPORTED @@ -108,7 +120,7 @@ struct png_info_def int num_text; /* number of comments read or comments to write */ int max_text; /* current size of text array */ png_textp text; /* array of comments read or comments to write */ -#endif /* PNG_TEXT_SUPPORTED */ +#endif /* TEXT */ #ifdef PNG_tIME_SUPPORTED /* The tIME chunk holds the last time the displayed image data was @@ -183,23 +195,6 @@ defined(PNG_READ_BACKGROUND_SUPPORTED) png_uint_16p hist; #endif -#ifdef PNG_cHRM_SUPPORTED - /* The cHRM chunk describes the CIE color characteristics of the monitor - * on which the PNG was created. This data allows the viewer to do gamut - * mapping of the input image to ensure that the viewer sees the same - * colors in the image as the creator. Values are in the range - * [0.0, 0.8]. Data valid if (valid & PNG_INFO_cHRM) non-zero. - */ - png_fixed_point x_white; - png_fixed_point y_white; - png_fixed_point x_red; - png_fixed_point y_red; - png_fixed_point x_green; - png_fixed_point y_green; - png_fixed_point x_blue; - png_fixed_point y_blue; -#endif - #ifdef PNG_pCAL_SUPPORTED /* The pCAL chunk describes a transformation between the stored pixel * values and original physical data values used to create the image. @@ -224,25 +219,20 @@ defined(PNG_READ_BACKGROUND_SUPPORTED) /* New members added in libpng-1.0.6 */ png_uint_32 free_me; /* flags items libpng is responsible for freeing */ -#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) || \ - defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED /* Storage for unknown chunks that the library doesn't recognize. */ png_unknown_chunkp unknown_chunks; - int unknown_chunks_num; -#endif -#ifdef PNG_iCCP_SUPPORTED - /* iCCP chunk data. */ - png_charp iccp_name; /* profile name */ - png_bytep iccp_profile; /* International Color Consortium profile data */ - png_uint_32 iccp_proflen; /* ICC profile data length */ - png_byte iccp_compression; /* Always zero */ + /* The type of this field is limited by the type of + * png_struct::user_chunk_cache_max, else overflow can occur. + */ + int unknown_chunks_num; #endif #ifdef PNG_sPLT_SUPPORTED /* Data on sPLT chunks (there may be more than one). */ png_sPLT_tp splt_palettes; - png_uint_32 splt_palettes_num; + int splt_palettes_num; /* Match type returned by png_get API */ #endif #ifdef PNG_sCAL_SUPPORTED diff --git a/3rdparty/libpng/pnglibconf.h b/3rdparty/libpng/pnglibconf.h index d93adabaa..ff5d156a6 100644 --- a/3rdparty/libpng/pnglibconf.h +++ b/3rdparty/libpng/pnglibconf.h @@ -1,48 +1,31 @@ - -/* libpng STANDARD API DEFINITION */ +/* libpng 1.6.19 STANDARD API DEFINITION */ /* pnglibconf.h - library build configuration */ -/* Libpng 1.5.12 - July 11, 2012 */ +/* Libpng version 1.6.19 - November 12, 2015 */ -/* Copyright (c) 1998-2012 Glenn Randers-Pehrson */ +/* Copyright (c) 1998-2015 Glenn Randers-Pehrson */ /* This code is released under the libpng license. */ /* For conditions of distribution and use, see the disclaimer */ /* and license in png.h */ /* pnglibconf.h */ +/* Machine generated file: DO NOT EDIT */ /* Derived from: scripts/pnglibconf.dfa */ -/* If you edit this file by hand you must obey the rules expressed in */ -/* pnglibconf.dfa with respect to the dependencies between the following */ -/* symbols. It is much better to generate a new file using */ -/* scripts/libpngconf.mak */ - #ifndef PNGLCONF_H #define PNGLCONF_H -/* settings */ -#define PNG_API_RULE 0 -#define PNG_CALLOC_SUPPORTED -#define PNG_COST_SHIFT 3 -#define PNG_DEFAULT_READ_MACROS 1 -#define PNG_GAMMA_THRESHOLD_FIXED 5000 -#define PNG_MAX_GAMMA_8 11 -#define PNG_QUANTIZE_BLUE_BITS 5 -#define PNG_QUANTIZE_GREEN_BITS 5 -#define PNG_QUANTIZE_RED_BITS 5 -#define PNG_sCAL_PRECISION 5 -#define PNG_WEIGHT_SHIFT 8 -#define PNG_ZBUF_SIZE 8192 -/* end of settings */ /* options */ #define PNG_16BIT_SUPPORTED -#define PNG_ALIGN_MEMORY_SUPPORTED +#define PNG_ALIGNED_MEMORY_SUPPORTED +/*#undef PNG_ARM_NEON_API_SUPPORTED*/ +/*#undef PNG_ARM_NEON_CHECK_SUPPORTED*/ #define PNG_BENIGN_ERRORS_SUPPORTED -#define PNG_bKGD_SUPPORTED +#define PNG_BENIGN_READ_ERRORS_SUPPORTED +/*#undef PNG_BENIGN_WRITE_ERRORS_SUPPORTED*/ #define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED -#define PNG_CHECK_cHRM_SUPPORTED #define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED -#define PNG_cHRM_SUPPORTED +#define PNG_COLORSPACE_SUPPORTED #define PNG_CONSOLE_IO_SUPPORTED #define PNG_CONVERT_tIME_SUPPORTED #define PNG_EASY_ACCESS_SUPPORTED @@ -51,18 +34,15 @@ #define PNG_FIXED_POINT_SUPPORTED #define PNG_FLOATING_ARITHMETIC_SUPPORTED #define PNG_FLOATING_POINT_SUPPORTED -#define PNG_gAMA_SUPPORTED +#define PNG_FORMAT_AFIRST_SUPPORTED +#define PNG_FORMAT_BGR_SUPPORTED +#define PNG_GAMMA_SUPPORTED +#define PNG_GET_PALETTE_MAX_SUPPORTED #define PNG_HANDLE_AS_UNKNOWN_SUPPORTED -#define PNG_hIST_SUPPORTED -#define PNG_iCCP_SUPPORTED #define PNG_INCH_CONVERSIONS_SUPPORTED #define PNG_INFO_IMAGE_SUPPORTED #define PNG_IO_STATE_SUPPORTED -#define PNG_iTXt_SUPPORTED #define PNG_MNG_FEATURES_SUPPORTED -#define PNG_oFFs_SUPPORTED -#define PNG_pCAL_SUPPORTED -#define PNG_pHYs_SUPPORTED #define PNG_POINTER_INDEXING_SUPPORTED #define PNG_PROGRESSIVE_READ_SUPPORTED #define PNG_READ_16BIT_SUPPORTED @@ -70,68 +50,70 @@ #define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED #define PNG_READ_BACKGROUND_SUPPORTED #define PNG_READ_BGR_SUPPORTED -#define PNG_READ_bKGD_SUPPORTED #define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED -#define PNG_READ_cHRM_SUPPORTED #define PNG_READ_COMPOSITE_NODIV_SUPPORTED #define PNG_READ_COMPRESSED_TEXT_SUPPORTED #define PNG_READ_EXPAND_16_SUPPORTED #define PNG_READ_EXPAND_SUPPORTED #define PNG_READ_FILLER_SUPPORTED -#define PNG_READ_gAMA_SUPPORTED #define PNG_READ_GAMMA_SUPPORTED +#define PNG_READ_GET_PALETTE_MAX_SUPPORTED #define PNG_READ_GRAY_TO_RGB_SUPPORTED -#define PNG_READ_hIST_SUPPORTED -#define PNG_READ_iCCP_SUPPORTED #define PNG_READ_INTERLACING_SUPPORTED #define PNG_READ_INT_FUNCTIONS_SUPPORTED #define PNG_READ_INVERT_ALPHA_SUPPORTED #define PNG_READ_INVERT_SUPPORTED -#define PNG_READ_iTXt_SUPPORTED -#define PNG_READ_oFFs_SUPPORTED #define PNG_READ_OPT_PLTE_SUPPORTED -#define PNG_READ_PACK_SUPPORTED #define PNG_READ_PACKSWAP_SUPPORTED -#define PNG_READ_pCAL_SUPPORTED -#define PNG_READ_pHYs_SUPPORTED +#define PNG_READ_PACK_SUPPORTED #define PNG_READ_QUANTIZE_SUPPORTED #define PNG_READ_RGB_TO_GRAY_SUPPORTED -#define PNG_READ_sBIT_SUPPORTED #define PNG_READ_SCALE_16_TO_8_SUPPORTED -#define PNG_READ_sCAL_SUPPORTED #define PNG_READ_SHIFT_SUPPORTED -#define PNG_READ_sPLT_SUPPORTED -#define PNG_READ_sRGB_SUPPORTED #define PNG_READ_STRIP_16_TO_8_SUPPORTED #define PNG_READ_STRIP_ALPHA_SUPPORTED #define PNG_READ_SUPPORTED #define PNG_READ_SWAP_ALPHA_SUPPORTED #define PNG_READ_SWAP_SUPPORTED -#define PNG_READ_tEXt_SUPPORTED #define PNG_READ_TEXT_SUPPORTED -#define PNG_READ_tIME_SUPPORTED #define PNG_READ_TRANSFORMS_SUPPORTED -#define PNG_READ_tRNS_SUPPORTED #define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED #define PNG_READ_USER_CHUNKS_SUPPORTED #define PNG_READ_USER_TRANSFORM_SUPPORTED +#define PNG_READ_bKGD_SUPPORTED +#define PNG_READ_cHRM_SUPPORTED +#define PNG_READ_gAMA_SUPPORTED +#define PNG_READ_hIST_SUPPORTED +#define PNG_READ_iCCP_SUPPORTED +#define PNG_READ_iTXt_SUPPORTED +#define PNG_READ_oFFs_SUPPORTED +#define PNG_READ_pCAL_SUPPORTED +#define PNG_READ_pHYs_SUPPORTED +#define PNG_READ_sBIT_SUPPORTED +#define PNG_READ_sCAL_SUPPORTED +#define PNG_READ_sPLT_SUPPORTED +#define PNG_READ_sRGB_SUPPORTED +#define PNG_READ_tEXt_SUPPORTED +#define PNG_READ_tIME_SUPPORTED +#define PNG_READ_tRNS_SUPPORTED #define PNG_READ_zTXt_SUPPORTED #define PNG_SAVE_INT_32_SUPPORTED -#define PNG_sBIT_SUPPORTED -#define PNG_sCAL_SUPPORTED +#define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED #define PNG_SEQUENTIAL_READ_SUPPORTED -#define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED -#define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED #define PNG_SETJMP_SUPPORTED +#define PNG_SET_OPTION_SUPPORTED +#define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED #define PNG_SET_USER_LIMITS_SUPPORTED -#define PNG_sPLT_SUPPORTED -#define PNG_sRGB_SUPPORTED +#define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED +#define PNG_SIMPLIFIED_READ_BGR_SUPPORTED +#define PNG_SIMPLIFIED_READ_SUPPORTED +#define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED +#define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED +#define PNG_SIMPLIFIED_WRITE_SUPPORTED #define PNG_STDIO_SUPPORTED -#define PNG_tEXt_SUPPORTED +#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED #define PNG_TEXT_SUPPORTED #define PNG_TIME_RFC1123_SUPPORTED -#define PNG_tIME_SUPPORTED -#define PNG_tRNS_SUPPORTED #define PNG_UNKNOWN_CHUNKS_SUPPORTED #define PNG_USER_CHUNKS_SUPPORTED #define PNG_USER_LIMITS_SUPPORTED @@ -142,45 +124,91 @@ #define PNG_WRITE_16BIT_SUPPORTED #define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED #define PNG_WRITE_BGR_SUPPORTED -#define PNG_WRITE_bKGD_SUPPORTED #define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED -#define PNG_WRITE_cHRM_SUPPORTED #define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED +#define PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED #define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED #define PNG_WRITE_FILLER_SUPPORTED #define PNG_WRITE_FILTER_SUPPORTED #define PNG_WRITE_FLUSH_SUPPORTED -#define PNG_WRITE_gAMA_SUPPORTED -#define PNG_WRITE_hIST_SUPPORTED -#define PNG_WRITE_iCCP_SUPPORTED +#define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED #define PNG_WRITE_INTERLACING_SUPPORTED #define PNG_WRITE_INT_FUNCTIONS_SUPPORTED #define PNG_WRITE_INVERT_ALPHA_SUPPORTED #define PNG_WRITE_INVERT_SUPPORTED +#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED +#define PNG_WRITE_PACKSWAP_SUPPORTED +#define PNG_WRITE_PACK_SUPPORTED +#define PNG_WRITE_SHIFT_SUPPORTED +#define PNG_WRITE_SUPPORTED +#define PNG_WRITE_SWAP_ALPHA_SUPPORTED +#define PNG_WRITE_SWAP_SUPPORTED +#define PNG_WRITE_TEXT_SUPPORTED +#define PNG_WRITE_TRANSFORMS_SUPPORTED +#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED +#define PNG_WRITE_USER_TRANSFORM_SUPPORTED +#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED +#define PNG_WRITE_bKGD_SUPPORTED +#define PNG_WRITE_cHRM_SUPPORTED +#define PNG_WRITE_gAMA_SUPPORTED +#define PNG_WRITE_hIST_SUPPORTED +#define PNG_WRITE_iCCP_SUPPORTED #define PNG_WRITE_iTXt_SUPPORTED #define PNG_WRITE_oFFs_SUPPORTED -#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED -#define PNG_WRITE_PACK_SUPPORTED -#define PNG_WRITE_PACKSWAP_SUPPORTED #define PNG_WRITE_pCAL_SUPPORTED #define PNG_WRITE_pHYs_SUPPORTED #define PNG_WRITE_sBIT_SUPPORTED #define PNG_WRITE_sCAL_SUPPORTED -#define PNG_WRITE_SHIFT_SUPPORTED #define PNG_WRITE_sPLT_SUPPORTED #define PNG_WRITE_sRGB_SUPPORTED -#define PNG_WRITE_SUPPORTED -#define PNG_WRITE_SWAP_ALPHA_SUPPORTED -#define PNG_WRITE_SWAP_SUPPORTED #define PNG_WRITE_tEXt_SUPPORTED -#define PNG_WRITE_TEXT_SUPPORTED #define PNG_WRITE_tIME_SUPPORTED -#define PNG_WRITE_TRANSFORMS_SUPPORTED #define PNG_WRITE_tRNS_SUPPORTED -#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED -#define PNG_WRITE_USER_TRANSFORM_SUPPORTED -#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED #define PNG_WRITE_zTXt_SUPPORTED +#define PNG_bKGD_SUPPORTED +#define PNG_cHRM_SUPPORTED +#define PNG_gAMA_SUPPORTED +#define PNG_hIST_SUPPORTED +#define PNG_iCCP_SUPPORTED +#define PNG_iTXt_SUPPORTED +#define PNG_oFFs_SUPPORTED +#define PNG_pCAL_SUPPORTED +#define PNG_pHYs_SUPPORTED +#define PNG_sBIT_SUPPORTED +#define PNG_sCAL_SUPPORTED +#define PNG_sPLT_SUPPORTED +#define PNG_sRGB_SUPPORTED +#define PNG_tEXt_SUPPORTED +#define PNG_tIME_SUPPORTED +#define PNG_tRNS_SUPPORTED #define PNG_zTXt_SUPPORTED /* end of options */ +/* settings */ +#define PNG_API_RULE 0 +#define PNG_DEFAULT_READ_MACROS 1 +#define PNG_GAMMA_THRESHOLD_FIXED 5000 +#define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE +#define PNG_INFLATE_BUF_SIZE 1024 +#define PNG_LINKAGE_API extern +#define PNG_LINKAGE_CALLBACK extern +#define PNG_LINKAGE_DATA extern +#define PNG_LINKAGE_FUNCTION extern +#define PNG_MAX_GAMMA_8 11 +#define PNG_QUANTIZE_BLUE_BITS 5 +#define PNG_QUANTIZE_GREEN_BITS 5 +#define PNG_QUANTIZE_RED_BITS 5 +#define PNG_TEXT_Z_DEFAULT_COMPRESSION (-1) +#define PNG_TEXT_Z_DEFAULT_STRATEGY 0 +#define PNG_USER_CHUNK_CACHE_MAX 1000 +#define PNG_USER_CHUNK_MALLOC_MAX 8000000 +#define PNG_USER_HEIGHT_MAX 1000000 +#define PNG_USER_WIDTH_MAX 1000000 +#define PNG_ZBUF_SIZE 8192 +#define PNG_ZLIB_VERNUM 0 /* unknown */ +#define PNG_Z_DEFAULT_COMPRESSION (-1) +#define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0 +#define PNG_Z_DEFAULT_STRATEGY 1 +#define PNG_sCAL_PRECISION 5 +#define PNG_sRGB_PROFILE_CHECKS 2 +/* end of settings */ #endif /* PNGLCONF_H */ diff --git a/3rdparty/libpng/pngmem.c b/3rdparty/libpng/pngmem.c index bf5ff037d..45ac5579b 100644 --- a/3rdparty/libpng/pngmem.c +++ b/3rdparty/libpng/pngmem.c @@ -1,8 +1,8 @@ /* pngmem.c - stub functions for memory allocation * - * Last changed in libpng 1.5.7 [December 15, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.15 [November 20, 2014] + * Copyright (c) 1998-2014 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -20,627 +20,244 @@ #include "pngpriv.h" #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) - -/* Borland DOS special memory handler */ -#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) -/* If you change this, be sure to change the one in png.h also */ - -/* Allocate memory for a png_struct. The malloc and memset can be replaced - by a single call to calloc() if this is thought to improve performance. */ -PNG_FUNCTION(png_voidp /* PRIVATE */, -png_create_struct,(int type),PNG_ALLOCATED) -{ -# ifdef PNG_USER_MEM_SUPPORTED - return (png_create_struct_2(type, NULL, NULL)); -} - -/* Alternate version of png_create_struct, for use with user-defined malloc. */ -PNG_FUNCTION(png_voidp /* PRIVATE */, -png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr), - PNG_ALLOCATED) -{ -# endif /* PNG_USER_MEM_SUPPORTED */ - png_size_t size; - png_voidp struct_ptr; - - if (type == PNG_STRUCT_INFO) - size = png_sizeof(png_info); - - else if (type == PNG_STRUCT_PNG) - size = png_sizeof(png_struct); - - else - return (png_get_copyright(NULL)); - -# ifdef PNG_USER_MEM_SUPPORTED - if (malloc_fn != NULL) - { - png_struct dummy_struct; - memset(&dummy_struct, 0, sizeof dummy_struct); - dummy_struct.mem_ptr=mem_ptr; - struct_ptr = (*(malloc_fn))(&dummy_struct, (png_alloc_size_t)size); - } - - else -# endif /* PNG_USER_MEM_SUPPORTED */ - struct_ptr = (png_voidp)farmalloc(size); - if (struct_ptr != NULL) - png_memset(struct_ptr, 0, size); - - return (struct_ptr); -} - -/* Free memory allocated by a png_create_struct() call */ +/* Free a png_struct */ void /* PRIVATE */ -png_destroy_struct(png_voidp struct_ptr) +png_destroy_png_struct(png_structrp png_ptr) { -# ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2(struct_ptr, NULL, NULL); -} - -/* Free memory allocated by a png_create_struct() call */ -void /* PRIVATE */ -png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, - png_voidp mem_ptr) -{ -# endif - if (struct_ptr != NULL) + if (png_ptr != NULL) { -# ifdef PNG_USER_MEM_SUPPORTED - if (free_fn != NULL) - { - png_struct dummy_struct; - memset(&dummy_struct, 0, sizeof dummy_struct); - dummy_struct.mem_ptr=mem_ptr; - (*(free_fn))(&dummy_struct, struct_ptr); - return; - } + /* png_free might call png_error and may certainly call + * png_get_mem_ptr, so fake a temporary png_struct to support this. + */ + png_struct dummy_struct = *png_ptr; + memset(png_ptr, 0, (sizeof *png_ptr)); + png_free(&dummy_struct, png_ptr); -# endif /* PNG_USER_MEM_SUPPORTED */ - farfree (struct_ptr); +# ifdef PNG_SETJMP_SUPPORTED + /* We may have a jmp_buf left to deallocate. */ + png_free_jmpbuf(&dummy_struct); +# endif } } /* Allocate memory. For reasonable files, size should never exceed - * 64K. However, zlib may allocate more then 64K if you don't tell - * it not to. See zconf.h and png.h for more information. zlib does - * need to allocate exactly 64K, so whatever you call here must - * have the ability to do that. - * - * Borland seems to have a problem in DOS mode for exactly 64K. - * It gives you a segment with an offset of 8 (perhaps to store its - * memory stuff). zlib doesn't like this at all, so we have to - * detect and deal with it. This code should not be needed in - * Windows or OS/2 modes, and only in 16 bit mode. This code has - * been updated by Alexander Lehmann for version 0.89 to waste less - * memory. - * - * Note that we can't use png_size_t for the "size" declaration, - * since on some systems a png_size_t is a 16-bit quantity, and as a - * result, we would be truncating potentially larger memory requests - * (which should cause a fatal error) and introducing major problems. - */ -PNG_FUNCTION(png_voidp,PNGAPI -png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) -{ - png_voidp ret; - - ret = (png_malloc(png_ptr, size)); - - if (ret != NULL) - png_memset(ret,0,(png_size_t)size); - - return (ret); -} - -PNG_FUNCTION(png_voidp,PNGAPI -png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) -{ - png_voidp ret; - - if (png_ptr == NULL || size == 0) - return (NULL); - -# ifdef PNG_USER_MEM_SUPPORTED - if (png_ptr->malloc_fn != NULL) - ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size)); - - else - ret = (png_malloc_default(png_ptr, size)); - - if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of memory"); - - return (ret); -} - -PNG_FUNCTION(png_voidp,PNGAPI -png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) -{ - png_voidp ret; -# endif /* PNG_USER_MEM_SUPPORTED */ - - if (png_ptr == NULL || size == 0) - return (NULL); - -# ifdef PNG_MAX_MALLOC_64K - if (size > (png_uint_32)65536L) - { - png_warning(png_ptr, "Cannot Allocate > 64K"); - ret = NULL; - } - - else -# endif - - if (size != (size_t)size) - ret = NULL; - - else if (size == (png_uint_32)65536L) - { - if (png_ptr->offset_table == NULL) - { - /* Try to see if we need to do any of this fancy stuff */ - ret = farmalloc(size); - if (ret == NULL || ((png_size_t)ret & 0xffff)) - { - int num_blocks; - png_uint_32 total_size; - png_bytep table; - int i, mem_level, window_bits; - png_byte huge * hptr; - int window_bits - - if (ret != NULL) - { - farfree(ret); - ret = NULL; - } - - window_bits = - png_ptr->zlib_window_bits >= png_ptr->zlib_text_window_bits ? - png_ptr->zlib_window_bits : png_ptr->zlib_text_window_bits; - - if (window_bits > 14) - num_blocks = (int)(1 << (window_bits - 14)); - - else - num_blocks = 1; - - mem_level = - png_ptr->zlib_mem_level >= png_ptr->zlib_text_mem_level ? - png_ptr->zlib_mem_level : png_ptr->zlib_text_mem_level; - - if (mem_level >= 7) - num_blocks += (int)(1 << (mem_level - 7)); - - else - num_blocks++; - - total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; - - table = farmalloc(total_size); - - if (table == NULL) - { -# ifndef PNG_USER_MEM_SUPPORTED - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */ - - else - png_warning(png_ptr, "Out Of Memory"); -# endif - return (NULL); - } - - if ((png_size_t)table & 0xfff0) - { -# ifndef PNG_USER_MEM_SUPPORTED - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, - "Farmalloc didn't return normalized pointer"); - - else - png_warning(png_ptr, - "Farmalloc didn't return normalized pointer"); -# endif - return (NULL); - } - - png_ptr->offset_table = table; - png_ptr->offset_table_ptr = farmalloc(num_blocks * - png_sizeof(png_bytep)); - - if (png_ptr->offset_table_ptr == NULL) - { -# ifndef PNG_USER_MEM_SUPPORTED - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */ - - else - png_warning(png_ptr, "Out Of memory"); -# endif - return (NULL); - } - - hptr = (png_byte huge *)table; - if ((png_size_t)hptr & 0xf) - { - hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); - hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */ - } - - for (i = 0; i < num_blocks; i++) - { - png_ptr->offset_table_ptr[i] = (png_bytep)hptr; - hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */ - } - - png_ptr->offset_table_number = num_blocks; - png_ptr->offset_table_count = 0; - png_ptr->offset_table_count_free = 0; - } - } - - if (png_ptr->offset_table_count >= png_ptr->offset_table_number) - { -# ifndef PNG_USER_MEM_SUPPORTED - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of Memory"); /* Note "O" and "M" */ - - else - png_warning(png_ptr, "Out of Memory"); -# endif - return (NULL); - } - - ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; - } - - else - ret = farmalloc(size); - -# ifndef PNG_USER_MEM_SUPPORTED - if (ret == NULL) - { - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */ - - else - png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */ - } -# endif - - return (ret); -} - -/* Free a pointer allocated by png_malloc(). In the default - * configuration, png_ptr is not used, but is passed in case it - * is needed. If ptr is NULL, return without taking any action. - */ -void PNGAPI -png_free(png_structp png_ptr, png_voidp ptr) -{ - if (png_ptr == NULL || ptr == NULL) - return; - -# ifdef PNG_USER_MEM_SUPPORTED - if (png_ptr->free_fn != NULL) - { - (*(png_ptr->free_fn))(png_ptr, ptr); - return; - } - - else - png_free_default(png_ptr, ptr); -} - -void PNGAPI -png_free_default(png_structp png_ptr, png_voidp ptr) -{ -# endif /* PNG_USER_MEM_SUPPORTED */ - - if (png_ptr == NULL || ptr == NULL) - return; - - if (png_ptr->offset_table != NULL) - { - int i; - - for (i = 0; i < png_ptr->offset_table_count; i++) - { - if (ptr == png_ptr->offset_table_ptr[i]) - { - ptr = NULL; - png_ptr->offset_table_count_free++; - break; - } - } - if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) - { - farfree(png_ptr->offset_table); - farfree(png_ptr->offset_table_ptr); - png_ptr->offset_table = NULL; - png_ptr->offset_table_ptr = NULL; - } - } - - if (ptr != NULL) - farfree(ptr); -} - -#else /* Not the Borland DOS special memory handler */ - -/* Allocate memory for a png_struct or a png_info. The malloc and - memset can be replaced by a single call to calloc() if this is thought - to improve performance noticably. */ -PNG_FUNCTION(png_voidp /* PRIVATE */, -png_create_struct,(int type),PNG_ALLOCATED) -{ -# ifdef PNG_USER_MEM_SUPPORTED - return (png_create_struct_2(type, NULL, NULL)); -} - -/* Allocate memory for a png_struct or a png_info. The malloc and - memset can be replaced by a single call to calloc() if this is thought - to improve performance noticably. */ -PNG_FUNCTION(png_voidp /* PRIVATE */, -png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr), - PNG_ALLOCATED) -{ -# endif /* PNG_USER_MEM_SUPPORTED */ - png_size_t size; - png_voidp struct_ptr; - - if (type == PNG_STRUCT_INFO) - size = png_sizeof(png_info); - - else if (type == PNG_STRUCT_PNG) - size = png_sizeof(png_struct); - - else - return (NULL); - -# ifdef PNG_USER_MEM_SUPPORTED - if (malloc_fn != NULL) - { - png_struct dummy_struct; - png_structp png_ptr = &dummy_struct; - png_ptr->mem_ptr=mem_ptr; - struct_ptr = (*(malloc_fn))(png_ptr, size); - - if (struct_ptr != NULL) - png_memset(struct_ptr, 0, size); - - return (struct_ptr); - } -# endif /* PNG_USER_MEM_SUPPORTED */ - -# if defined(__TURBOC__) && !defined(__FLAT__) - struct_ptr = (png_voidp)farmalloc(size); -# else -# if defined(_MSC_VER) && defined(MAXSEG_64K) - struct_ptr = (png_voidp)halloc(size, 1); -# else - struct_ptr = (png_voidp)malloc(size); -# endif -# endif - - if (struct_ptr != NULL) - png_memset(struct_ptr, 0, size); - - return (struct_ptr); -} - - -/* Free memory allocated by a png_create_struct() call */ -void /* PRIVATE */ -png_destroy_struct(png_voidp struct_ptr) -{ -# ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2(struct_ptr, NULL, NULL); -} - -/* Free memory allocated by a png_create_struct() call */ -void /* PRIVATE */ -png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, - png_voidp mem_ptr) -{ -# endif /* PNG_USER_MEM_SUPPORTED */ - if (struct_ptr != NULL) - { -# ifdef PNG_USER_MEM_SUPPORTED - if (free_fn != NULL) - { - png_struct dummy_struct; - png_structp png_ptr = &dummy_struct; - png_ptr->mem_ptr=mem_ptr; - (*(free_fn))(png_ptr, struct_ptr); - return; - } -# endif /* PNG_USER_MEM_SUPPORTED */ -# if defined(__TURBOC__) && !defined(__FLAT__) - farfree(struct_ptr); - -# else -# if defined(_MSC_VER) && defined(MAXSEG_64K) - hfree(struct_ptr); - -# else - free(struct_ptr); - -# endif -# endif - } -} - -/* Allocate memory. For reasonable files, size should never exceed - * 64K. However, zlib may allocate more then 64K if you don't tell + * 64K. However, zlib may allocate more than 64K if you don't tell * it not to. See zconf.h and png.h for more information. zlib does * need to allocate exactly 64K, so whatever you call here must * have the ability to do that. */ - PNG_FUNCTION(png_voidp,PNGAPI -png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) +png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) { png_voidp ret; - ret = (png_malloc(png_ptr, size)); + ret = png_malloc(png_ptr, size); if (ret != NULL) - png_memset(ret,0,(png_size_t)size); + memset(ret, 0, size); - return (ret); + return ret; } -PNG_FUNCTION(png_voidp,PNGAPI -png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) +/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of + * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED. + * Checking and error handling must happen outside this routine; it returns NULL + * if the allocation cannot be done (for any reason.) + */ +PNG_FUNCTION(png_voidp /* PRIVATE */, +png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), + PNG_ALLOCATED) { - png_voidp ret; + /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS + * allocators have also been removed in 1.6.0, so any 16-bit system now has + * to implement a user memory handler. This checks to be sure it isn't + * called with big numbers. + */ +#ifndef PNG_USER_MEM_SUPPORTED + PNG_UNUSED(png_ptr) +#endif -# ifdef PNG_USER_MEM_SUPPORTED - if (png_ptr == NULL || size == 0) - return (NULL); - - if (png_ptr->malloc_fn != NULL) - ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size)); - - else - ret = (png_malloc_default(png_ptr, size)); - - if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of Memory"); - - return (ret); -} - -PNG_FUNCTION(png_voidp,PNGAPI -png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) -{ - png_voidp ret; -# endif /* PNG_USER_MEM_SUPPORTED */ - - if (png_ptr == NULL || size == 0) - return (NULL); - -# ifdef PNG_MAX_MALLOC_64K - if (size > (png_uint_32)65536L) + /* Some compilers complain that this is always true. However, it + * can be false when integer overflow happens. + */ + if (size > 0 && size <= PNG_SIZE_MAX +# ifdef PNG_MAX_MALLOC_64K + && size <= 65536U +# endif + ) { -# ifndef PNG_USER_MEM_SUPPORTED - if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Cannot Allocate > 64K"); +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr != NULL && png_ptr->malloc_fn != NULL) + return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); else -# endif - return NULL; +#endif + return malloc((size_t)size); /* checked for truncation above */ } -# endif - - /* Check for overflow */ -# if defined(__TURBOC__) && !defined(__FLAT__) - - if (size != (unsigned long)size) - ret = NULL; else - ret = farmalloc(size); + return NULL; +} -# else -# if defined(_MSC_VER) && defined(MAXSEG_64K) - if (size != (unsigned long)size) - ret = NULL; +#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ + defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) +/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7 + * that arises because of the checks in png_realloc_array that are repeated in + * png_malloc_array. + */ +static png_voidp +png_malloc_array_checked(png_const_structrp png_ptr, int nelements, + size_t element_size) +{ + png_alloc_size_t req = nelements; /* known to be > 0 */ - else - ret = halloc(size, 1); + if (req <= PNG_SIZE_MAX/element_size) + return png_malloc_base(png_ptr, req * element_size); -# else - if (size != (size_t)size) - ret = NULL; + /* The failure case when the request is too large */ + return NULL; +} - else - ret = malloc((size_t)size); -# endif -# endif +PNG_FUNCTION(png_voidp /* PRIVATE */, +png_malloc_array,(png_const_structrp png_ptr, int nelements, + size_t element_size),PNG_ALLOCATED) +{ + if (nelements <= 0 || element_size == 0) + png_error(png_ptr, "internal error: array alloc"); -# ifndef PNG_USER_MEM_SUPPORTED - if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0) - png_error(png_ptr, "Out of Memory"); -# endif + return png_malloc_array_checked(png_ptr, nelements, element_size); +} - return (ret); +PNG_FUNCTION(png_voidp /* PRIVATE */, +png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array, + int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED) +{ + /* These are internal errors: */ + if (add_elements <= 0 || element_size == 0 || old_elements < 0 || + (old_array == NULL && old_elements > 0)) + png_error(png_ptr, "internal error: array realloc"); + + /* Check for overflow on the elements count (so the caller does not have to + * check.) + */ + if (add_elements <= INT_MAX - old_elements) + { + png_voidp new_array = png_malloc_array_checked(png_ptr, + old_elements+add_elements, element_size); + + if (new_array != NULL) + { + /* Because png_malloc_array worked the size calculations below cannot + * overflow. + */ + if (old_elements > 0) + memcpy(new_array, old_array, element_size*(unsigned)old_elements); + + memset((char*)new_array + element_size*(unsigned)old_elements, 0, + element_size*(unsigned)add_elements); + + return new_array; + } + } + + return NULL; /* error */ +} +#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */ + +/* Various functions that have different error handling are derived from this. + * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate + * function png_malloc_default is also provided. + */ +PNG_FUNCTION(png_voidp,PNGAPI +png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) +{ + png_voidp ret; + + if (png_ptr == NULL) + return NULL; + + ret = png_malloc_base(png_ptr, size); + + if (ret == NULL) + png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */ + + return ret; +} + +#ifdef PNG_USER_MEM_SUPPORTED +PNG_FUNCTION(png_voidp,PNGAPI +png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size), + PNG_ALLOCATED PNG_DEPRECATED) +{ + png_voidp ret; + + if (png_ptr == NULL) + return NULL; + + /* Passing 'NULL' here bypasses the application provided memory handler. */ + ret = png_malloc_base(NULL/*use malloc*/, size); + + if (ret == NULL) + png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ + + return ret; +} +#endif /* USER_MEM */ + +/* This function was added at libpng version 1.2.3. The png_malloc_warn() + * function will issue a png_warning and return NULL instead of issuing a + * png_error, if it fails to allocate the requested memory. + */ +PNG_FUNCTION(png_voidp,PNGAPI +png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), + PNG_ALLOCATED) +{ + if (png_ptr != NULL) + { + png_voidp ret = png_malloc_base(png_ptr, size); + + if (ret != NULL) + return ret; + + png_warning(png_ptr, "Out of memory"); + } + + return NULL; } /* Free a pointer allocated by png_malloc(). If ptr is NULL, return * without taking any action. */ void PNGAPI -png_free(png_structp png_ptr, png_voidp ptr) +png_free(png_const_structrp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; -# ifdef PNG_USER_MEM_SUPPORTED +#ifdef PNG_USER_MEM_SUPPORTED if (png_ptr->free_fn != NULL) - { - (*(png_ptr->free_fn))(png_ptr, ptr); - return; - } + png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr); else png_free_default(png_ptr, ptr); } -void PNGAPI -png_free_default(png_structp png_ptr, png_voidp ptr) +PNG_FUNCTION(void,PNGAPI +png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) { if (png_ptr == NULL || ptr == NULL) return; +#endif /* USER_MEM */ -# endif /* PNG_USER_MEM_SUPPORTED */ - -# if defined(__TURBOC__) && !defined(__FLAT__) - farfree(ptr); - -# else -# if defined(_MSC_VER) && defined(MAXSEG_64K) - hfree(ptr); - -# else free(ptr); - -# endif -# endif } -#endif /* Not Borland DOS special memory handler */ - -/* This function was added at libpng version 1.2.3. The png_malloc_warn() - * function will set up png_malloc() to issue a png_warning and return NULL - * instead of issuing a png_error, if it fails to allocate the requested - * memory. - */ -PNG_FUNCTION(png_voidp,PNGAPI -png_malloc_warn,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) -{ - png_voidp ptr; - png_uint_32 save_flags; - if (png_ptr == NULL) - return (NULL); - - save_flags = png_ptr->flags; - png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; - ptr = (png_voidp)png_malloc((png_structp)png_ptr, size); - png_ptr->flags=save_flags; - return(ptr); -} - #ifdef PNG_USER_MEM_SUPPORTED /* This function is called when the application wants to use another method * of allocating and freeing memory. */ void PNGAPI -png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr +png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn) { if (png_ptr != NULL) @@ -656,12 +273,12 @@ png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr * pointer before png_write_destroy and png_read_destroy are called. */ png_voidp PNGAPI -png_get_mem_ptr(png_const_structp png_ptr) +png_get_mem_ptr(png_const_structrp png_ptr) { if (png_ptr == NULL) - return (NULL); + return NULL; - return ((png_voidp)png_ptr->mem_ptr); + return png_ptr->mem_ptr; } -#endif /* PNG_USER_MEM_SUPPORTED */ -#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ +#endif /* USER_MEM */ +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/pngpread.c b/3rdparty/libpng/pngpread.c index 6b65ba8f4..9f68f9902 100644 --- a/3rdparty/libpng/pngpread.c +++ b/3rdparty/libpng/pngpread.c @@ -1,8 +1,8 @@ /* pngpread.c - read a png file in push mode * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.18 [July 23, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -19,15 +19,21 @@ #define PNG_READ_SIG_MODE 0 #define PNG_READ_CHUNK_MODE 1 #define PNG_READ_IDAT_MODE 2 -#define PNG_SKIP_MODE 3 #define PNG_READ_tEXt_MODE 4 #define PNG_READ_zTXt_MODE 5 #define PNG_READ_DONE_MODE 6 #define PNG_READ_iTXt_MODE 7 #define PNG_ERROR_MODE 8 +#define PNG_PUSH_SAVE_BUFFER_IF_FULL \ +if (png_ptr->push_length + 4 > png_ptr->buffer_size) \ + { png_push_save_buffer(png_ptr); return; } +#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \ +if (png_ptr->buffer_size < N) \ + { png_push_save_buffer(png_ptr); return; } + void PNGAPI -png_process_data(png_structp png_ptr, png_infop info_ptr, +png_process_data(png_structrp png_ptr, png_inforp info_ptr, png_bytep buffer, png_size_t buffer_size) { if (png_ptr == NULL || info_ptr == NULL) @@ -42,14 +48,14 @@ png_process_data(png_structp png_ptr, png_infop info_ptr, } png_size_t PNGAPI -png_process_data_pause(png_structp png_ptr, int save) +png_process_data_pause(png_structrp png_ptr, int save) { if (png_ptr != NULL) { - /* It's easiest for the caller if we do the save, then the caller doesn't + /* It's easiest for the caller if we do the save; then the caller doesn't * have to supply the same data again: */ - if (save) + if (save != 0) png_push_save_buffer(png_ptr); else { @@ -69,41 +75,23 @@ png_process_data_pause(png_structp png_ptr, int save) } png_uint_32 PNGAPI -png_process_data_skip(png_structp png_ptr) +png_process_data_skip(png_structrp png_ptr) { - png_uint_32 remaining = 0; - - if (png_ptr != NULL && png_ptr->process_mode == PNG_SKIP_MODE && - png_ptr->skip_length > 0) - { - /* At the end of png_process_data the buffer size must be 0 (see the loop - * above) so we can detect a broken call here: - */ - if (png_ptr->buffer_size != 0) - png_error(png_ptr, - "png_process_data_skip called inside png_process_data"); - - /* If is impossible for there to be a saved buffer at this point - - * otherwise we could not be in SKIP mode. This will also happen if - * png_process_skip is called inside png_process_data (but only very - * rarely.) - */ - if (png_ptr->save_buffer_size != 0) - png_error(png_ptr, "png_process_data_skip called with saved data"); - - remaining = png_ptr->skip_length; - png_ptr->skip_length = 0; - png_ptr->process_mode = PNG_READ_CHUNK_MODE; - } - - return remaining; + /* TODO: Deprecate and remove this API. + * Somewhere the implementation of this seems to have been lost, + * or abandoned. It was only to support some internal back-door access + * to png_struct) in libpng-1.4.x. + */ + png_app_warning(png_ptr, +"png_process_data_skip is not implemented in any current version of libpng"); + return 0; } /* What we do with the incoming data depends on what we were previously * doing before we ran out of data... */ void /* PRIVATE */ -png_process_some_data(png_structp png_ptr, png_infop info_ptr) +png_process_some_data(png_structrp png_ptr, png_inforp info_ptr) { if (png_ptr == NULL) return; @@ -128,12 +116,6 @@ png_process_some_data(png_structp png_ptr, png_infop info_ptr) break; } - case PNG_SKIP_MODE: - { - png_push_crc_finish(png_ptr); - break; - } - default: { png_ptr->buffer_size = 0; @@ -149,9 +131,9 @@ png_process_some_data(png_structp png_ptr, png_infop info_ptr) * routine. */ void /* PRIVATE */ -png_push_read_sig(png_structp png_ptr, png_infop info_ptr) +png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr) { - png_size_t num_checked = png_ptr->sig_bytes, + png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */ num_to_check = 8 - num_checked; if (png_ptr->buffer_size < num_to_check) @@ -172,7 +154,6 @@ png_push_read_sig(png_structp png_ptr, png_infop info_ptr) else png_error(png_ptr, "PNG file corrupted by ASCII conversion"); } - else { if (png_ptr->sig_bytes >= 8) @@ -183,27 +164,25 @@ png_push_read_sig(png_structp png_ptr, png_infop info_ptr) } void /* PRIVATE */ -png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) +png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr) { png_uint_32 chunk_name; +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + int keep; /* unknown handling method */ +#endif - /* First we make sure we have enough data for the 4 byte chunk name - * and the 4 byte chunk length before proceeding with decoding the + /* First we make sure we have enough data for the 4-byte chunk name + * and the 4-byte chunk length before proceeding with decoding the * chunk data. To fully decode each of these chunks, we also make - * sure we have enough data in the buffer for the 4 byte CRC at the + * sure we have enough data in the buffer for the 4-byte CRC at the * end of every chunk (except IDAT, which is handled separately). */ - if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) + if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0) { png_byte chunk_length[4]; png_byte chunk_tag[4]; - if (png_ptr->buffer_size < 8) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_LT(8) png_push_fill_buffer(png_ptr, chunk_length, 4); png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); png_reset_crc(png_ptr); @@ -217,14 +196,29 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) if (chunk_name == png_IDAT) { - /* This is here above the if/else case statement below because if the - * unknown handling marks 'IDAT' as unknown then the IDAT handling case is - * completely skipped. - * - * TODO: there must be a better way of doing this. - */ - if (png_ptr->mode & PNG_AFTER_IDAT) + if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; + + /* If we reach an IDAT chunk, this means we have read all of the + * header chunks, and we can start reading the image (or if this + * is called after the image has been read - we have an error). + */ + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_error(png_ptr, "Missing IHDR before IDAT"); + + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + (png_ptr->mode & PNG_HAVE_PLTE) == 0) + png_error(png_ptr, "Missing PLTE before IDAT"); + + png_ptr->mode |= PNG_HAVE_IDAT; + png_ptr->process_mode = PNG_READ_IDAT_MODE; + + if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0) + if (png_ptr->push_length == 0) + return; + + if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) + png_benign_error(png_ptr, "Too many IDATs found"); } if (chunk_name == png_IHDR) @@ -232,23 +226,13 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) if (png_ptr->push_length != 13) png_error(png_ptr, "Invalid IHDR length"); - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); } else if (chunk_name == png_IEND) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); png_ptr->process_mode = PNG_READ_DONE_MODE; @@ -256,70 +240,25 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) } #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - else if (png_chunk_unknown_handling(png_ptr, chunk_name)) + else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - - if (chunk_name == png_IDAT) - png_ptr->mode |= PNG_HAVE_IDAT; - - png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); + PNG_PUSH_SAVE_BUFFER_IF_FULL + png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep); if (chunk_name == png_PLTE) png_ptr->mode |= PNG_HAVE_PLTE; - - else if (chunk_name == png_IDAT) - { - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before IDAT"); - - else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && - !(png_ptr->mode & PNG_HAVE_PLTE)) - png_error(png_ptr, "Missing PLTE before IDAT"); - } } #endif else if (chunk_name == png_PLTE) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); } else if (chunk_name == png_IDAT) { - /* If we reach an IDAT chunk, this means we have read all of the - * header chunks, and we can start reading the image (or if this - * is called after the image has been read - we have an error). - */ - - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before IDAT"); - - else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && - !(png_ptr->mode & PNG_HAVE_PLTE)) - png_error(png_ptr, "Missing PLTE before IDAT"); - - if (png_ptr->mode & PNG_HAVE_IDAT) - { - if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) - if (png_ptr->push_length == 0) - return; - - if (png_ptr->mode & PNG_AFTER_IDAT) - png_benign_error(png_ptr, "Too many IDATs found"); - } - png_ptr->idat_size = png_ptr->push_length; - png_ptr->mode |= PNG_HAVE_IDAT; png_ptr->process_mode = PNG_READ_IDAT_MODE; png_push_have_info(png_ptr, info_ptr); png_ptr->zstream.avail_out = @@ -332,12 +271,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_gAMA_SUPPORTED else if (png_ptr->chunk_name == png_gAMA) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); } @@ -345,12 +279,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_sBIT_SUPPORTED else if (png_ptr->chunk_name == png_sBIT) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); } @@ -358,12 +287,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_cHRM_SUPPORTED else if (png_ptr->chunk_name == png_cHRM) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); } @@ -371,12 +295,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_sRGB_SUPPORTED else if (chunk_name == png_sRGB) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); } @@ -384,12 +303,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_iCCP_SUPPORTED else if (png_ptr->chunk_name == png_iCCP) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length); } @@ -397,12 +311,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_sPLT_SUPPORTED else if (chunk_name == png_sPLT) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length); } @@ -410,12 +319,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_tRNS_SUPPORTED else if (chunk_name == png_tRNS) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); } @@ -423,12 +327,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_bKGD_SUPPORTED else if (chunk_name == png_bKGD) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); } @@ -436,12 +335,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_hIST_SUPPORTED else if (chunk_name == png_hIST) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); } @@ -449,12 +343,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_pHYs_SUPPORTED else if (chunk_name == png_pHYs) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); } @@ -462,12 +351,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_oFFs_SUPPORTED else if (chunk_name == png_oFFs) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); } #endif @@ -475,12 +359,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_pCAL_SUPPORTED else if (chunk_name == png_pCAL) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); } @@ -488,12 +367,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_sCAL_SUPPORTED else if (chunk_name == png_sCAL) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length); } @@ -501,12 +375,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_tIME_SUPPORTED else if (chunk_name == png_tIME) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); } @@ -514,12 +383,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_tEXt_SUPPORTED else if (chunk_name == png_tEXt) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); } @@ -527,12 +391,7 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_zTXt_SUPPORTED else if (chunk_name == png_zTXt) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); } @@ -540,100 +399,21 @@ png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_READ_iTXt_SUPPORTED else if (chunk_name == png_iTXt) { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_FULL png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length); } - #endif else { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); + PNG_PUSH_SAVE_BUFFER_IF_FULL + png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, + PNG_HANDLE_CHUNK_AS_DEFAULT); } png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; } -void /* PRIVATE */ -png_push_crc_skip(png_structp png_ptr, png_uint_32 skip) -{ - png_ptr->process_mode = PNG_SKIP_MODE; - png_ptr->skip_length = skip; -} - -void /* PRIVATE */ -png_push_crc_finish(png_structp png_ptr) -{ - if (png_ptr->skip_length && png_ptr->save_buffer_size) - { - png_size_t save_size = png_ptr->save_buffer_size; - png_uint_32 skip_length = png_ptr->skip_length; - - /* We want the smaller of 'skip_length' and 'save_buffer_size', but - * they are of different types and we don't know which variable has the - * fewest bits. Carefully select the smaller and cast it to the type of - * the larger - this cannot overflow. Do not cast in the following test - * - it will break on either 16 or 64 bit platforms. - */ - if (skip_length < save_size) - save_size = (png_size_t)skip_length; - - else - skip_length = (png_uint_32)save_size; - - png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); - - png_ptr->skip_length -= skip_length; - png_ptr->buffer_size -= save_size; - png_ptr->save_buffer_size -= save_size; - png_ptr->save_buffer_ptr += save_size; - } - - if (png_ptr->skip_length && png_ptr->current_buffer_size) - { - png_size_t save_size = png_ptr->current_buffer_size; - png_uint_32 skip_length = png_ptr->skip_length; - - /* We want the smaller of 'skip_length' and 'current_buffer_size', here, - * the same problem exists as above and the same solution. - */ - if (skip_length < save_size) - save_size = (png_size_t)skip_length; - - else - skip_length = (png_uint_32)save_size; - - png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); - - png_ptr->skip_length -= skip_length; - png_ptr->buffer_size -= save_size; - png_ptr->current_buffer_size -= save_size; - png_ptr->current_buffer_ptr += save_size; - } - - if (!png_ptr->skip_length) - { - if (png_ptr->buffer_size < 4) - { - png_push_save_buffer(png_ptr); - return; - } - - png_crc_finish(png_ptr, 0); - png_ptr->process_mode = PNG_READ_CHUNK_MODE; - } -} - void PNGCBAPI png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) { @@ -643,8 +423,7 @@ png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) return; ptr = buffer; - - if (png_ptr->save_buffer_size) + if (png_ptr->save_buffer_size != 0) { png_size_t save_size; @@ -654,15 +433,14 @@ png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) else save_size = png_ptr->save_buffer_size; - png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); + memcpy(ptr, png_ptr->save_buffer_ptr, save_size); length -= save_size; ptr += save_size; png_ptr->buffer_size -= save_size; png_ptr->save_buffer_size -= save_size; png_ptr->save_buffer_ptr += save_size; } - - if (length && png_ptr->current_buffer_size) + if (length != 0 && png_ptr->current_buffer_size != 0) { png_size_t save_size; @@ -672,7 +450,7 @@ png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) else save_size = png_ptr->current_buffer_size; - png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); + memcpy(ptr, png_ptr->current_buffer_ptr, save_size); png_ptr->buffer_size -= save_size; png_ptr->current_buffer_size -= save_size; png_ptr->current_buffer_ptr += save_size; @@ -680,9 +458,9 @@ png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) } void /* PRIVATE */ -png_push_save_buffer(png_structp png_ptr) +png_push_save_buffer(png_structrp png_ptr) { - if (png_ptr->save_buffer_size) + if (png_ptr->save_buffer_size != 0) { if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) { @@ -691,7 +469,6 @@ png_push_save_buffer(png_structp png_ptr) png_bytep dp; istop = png_ptr->save_buffer_size; - for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; i < istop; i++, sp++, dp++) { @@ -699,7 +476,6 @@ png_push_save_buffer(png_structp png_ptr) } } } - if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > png_ptr->save_buffer_max) { @@ -714,7 +490,8 @@ png_push_save_buffer(png_structp png_ptr) new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; old_buffer = png_ptr->save_buffer; - png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, new_max); + png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, + (png_size_t)new_max); if (png_ptr->save_buffer == NULL) { @@ -722,25 +499,23 @@ png_push_save_buffer(png_structp png_ptr) png_error(png_ptr, "Insufficient memory for save_buffer"); } - png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); + memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); png_free(png_ptr, old_buffer); png_ptr->save_buffer_max = new_max; } - if (png_ptr->current_buffer_size) { - png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, + memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); png_ptr->save_buffer_size += png_ptr->current_buffer_size; png_ptr->current_buffer_size = 0; } - png_ptr->save_buffer_ptr = png_ptr->save_buffer; png_ptr->buffer_size = 0; } void /* PRIVATE */ -png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, +png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer, png_size_t buffer_length) { png_ptr->current_buffer = buffer; @@ -750,20 +525,15 @@ png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, } void /* PRIVATE */ -png_push_read_IDAT(png_structp png_ptr) +png_push_read_IDAT(png_structrp png_ptr) { - if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER)) + if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0) { png_byte chunk_length[4]; png_byte chunk_tag[4]; /* TODO: this code can be commoned up with the same code in push_read */ - if (png_ptr->buffer_size < 8) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_LT(8) png_push_fill_buffer(png_ptr, chunk_length, 4); png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); png_reset_crc(png_ptr); @@ -775,7 +545,7 @@ png_push_read_IDAT(png_structp png_ptr) { png_ptr->process_mode = PNG_READ_CHUNK_MODE; - if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) png_error(png_ptr, "Not enough compressed data"); return; @@ -784,7 +554,7 @@ png_push_read_IDAT(png_structp png_ptr) png_ptr->idat_size = png_ptr->push_length; } - if (png_ptr->idat_size && png_ptr->save_buffer_size) + if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0) { png_size_t save_size = png_ptr->save_buffer_size; png_uint_32 idat_size = png_ptr->idat_size; @@ -793,7 +563,7 @@ png_push_read_IDAT(png_structp png_ptr) * are of different types and we don't know which variable has the fewest * bits. Carefully select the smaller and cast it to the type of the * larger - this cannot overflow. Do not cast in the following test - it - * will break on either 16 or 64 bit platforms. + * will break on either 16-bit or 64-bit platforms. */ if (idat_size < save_size) save_size = (png_size_t)idat_size; @@ -811,7 +581,7 @@ png_push_read_IDAT(png_structp png_ptr) png_ptr->save_buffer_ptr += save_size; } - if (png_ptr->idat_size && png_ptr->current_buffer_size) + if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0) { png_size_t save_size = png_ptr->current_buffer_size; png_uint_32 idat_size = png_ptr->idat_size; @@ -837,22 +607,18 @@ png_push_read_IDAT(png_structp png_ptr) png_ptr->current_buffer_ptr += save_size; } - if (!png_ptr->idat_size) + if (png_ptr->idat_size == 0) { - if (png_ptr->buffer_size < 4) - { - png_push_save_buffer(png_ptr); - return; - } - + PNG_PUSH_SAVE_BUFFER_IF_LT(4) png_crc_finish(png_ptr, 0); png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->zowner = 0; } } void /* PRIVATE */ -png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, +png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer, png_size_t buffer_length) { /* The caller checks for a non-zero buffer length. */ @@ -864,13 +630,14 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, * handle the uncompressed results. */ png_ptr->zstream.next_in = buffer; + /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */ png_ptr->zstream.avail_in = (uInt)buffer_length; /* Keep going until the decompressed data is all processed * or the stream marked as finished. */ while (png_ptr->zstream.avail_in > 0 && - !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) + (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) { int ret; @@ -881,9 +648,9 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, */ if (!(png_ptr->zstream.avail_out > 0)) { - png_ptr->zstream.avail_out = - (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, - png_ptr->iwidth) + 1; + /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */ + png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth, + png_ptr->iwidth) + 1); png_ptr->zstream.next_out = png_ptr->row_buf; } @@ -901,7 +668,8 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, if (ret != Z_OK && ret != Z_STREAM_END) { /* Terminate the decompression. */ - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; + png_ptr->zowner = 0; /* This may be a truncated stream (missing or * damaged end code). Treat that as a warning. @@ -929,7 +697,8 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, { /* Extra data. */ png_warning(png_ptr, "Extra compressed data in IDAT"); - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; + png_ptr->zowner = 0; /* Do no more processing; skip the unprocessed * input check below. @@ -944,7 +713,7 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, /* And check for the end of the stream. */ if (ret == Z_STREAM_END) - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; } /* All the data should have been processed, if anything @@ -956,7 +725,7 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, } void /* PRIVATE */ -png_push_process_row(png_structp png_ptr) +png_push_process_row(png_structrp png_ptr) { /* 1.5.6: row_info moved out of png_struct to a local here. */ png_row_info row_info; @@ -982,10 +751,10 @@ png_push_process_row(png_structp png_ptr) * it may not be in the future, so this was changed just to copy the * interlaced row count: */ - png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); + memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); #ifdef PNG_READ_TRANSFORMS_SUPPORTED - if (png_ptr->transformations) + if (png_ptr->transformations != 0) png_do_read_transformations(png_ptr, &row_info); #endif @@ -1002,15 +771,16 @@ png_push_process_row(png_structp png_ptr) #ifdef PNG_READ_INTERLACING_SUPPORTED - /* Blow up interlaced rows to full size */ - if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + /* Expand interlaced rows to full size */ + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) != 0) { if (png_ptr->pass < 6) png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); - switch (png_ptr->pass) - { + switch (png_ptr->pass) + { case 0: { int i; @@ -1185,26 +955,26 @@ png_push_process_row(png_structp png_ptr) } void /* PRIVATE */ -png_read_push_finish_row(png_structp png_ptr) +png_read_push_finish_row(png_structrp png_ptr) { #ifdef PNG_READ_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ - static PNG_CONST png_byte FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; + static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; /* Offset to next interlace block */ - static PNG_CONST png_byte FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; + static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; /* Start of interlace block in the y direction */ - static PNG_CONST png_byte FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; + static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; /* Offset to next interlace block in the y direction */ - static PNG_CONST png_byte FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; + static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; /* Height of interlace block. This is not currently used - if you need * it, uncomment it here and in png.h - static PNG_CONST png_byte FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; + static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; */ #endif @@ -1213,10 +983,10 @@ png_read_push_finish_row(png_structp png_ptr) return; #ifdef PNG_READ_INTERLACING_SUPPORTED - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) { png_ptr->row_number = 0; - png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); + memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); do { @@ -1237,7 +1007,7 @@ png_read_push_finish_row(png_structp png_ptr) png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; - if (png_ptr->transformations & PNG_INTERLACE) + if ((png_ptr->transformations & PNG_INTERLACE) != 0) break; png_ptr->num_rows = (png_ptr->height + @@ -1247,25 +1017,25 @@ png_read_push_finish_row(png_structp png_ptr) } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); } -#endif /* PNG_READ_INTERLACING_SUPPORTED */ +#endif /* READ_INTERLACING */ } void /* PRIVATE */ -png_push_have_info(png_structp png_ptr, png_infop info_ptr) +png_push_have_info(png_structrp png_ptr, png_inforp info_ptr) { if (png_ptr->info_fn != NULL) (*(png_ptr->info_fn))(png_ptr, info_ptr); } void /* PRIVATE */ -png_push_have_end(png_structp png_ptr, png_infop info_ptr) +png_push_have_end(png_structrp png_ptr, png_inforp info_ptr) { if (png_ptr->end_fn != NULL) (*(png_ptr->end_fn))(png_ptr, info_ptr); } void /* PRIVATE */ -png_push_have_row(png_structp png_ptr, png_bytep row) +png_push_have_row(png_structrp png_ptr, png_bytep row) { if (png_ptr->row_fn != NULL) (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, @@ -1274,7 +1044,7 @@ png_push_have_row(png_structp png_ptr, png_bytep row) #ifdef PNG_READ_INTERLACING_SUPPORTED void PNGAPI -png_progressive_combine_row (png_structp png_ptr, png_bytep old_row, +png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row, png_const_bytep new_row) { if (png_ptr == NULL) @@ -1285,12 +1055,12 @@ png_progressive_combine_row (png_structp png_ptr, png_bytep old_row, * it must be png_ptr->row_buf+1 */ if (new_row != NULL) - png_combine_row(png_ptr, old_row, 1/*display*/); + png_combine_row(png_ptr, old_row, 1/*blocky display*/); } -#endif /* PNG_READ_INTERLACING_SUPPORTED */ +#endif /* READ_INTERLACING */ void PNGAPI -png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, +png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr, png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn) { @@ -1305,11 +1075,11 @@ png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, } png_voidp PNGAPI -png_get_progressive_ptr(png_const_structp png_ptr) +png_get_progressive_ptr(png_const_structrp png_ptr) { if (png_ptr == NULL) return (NULL); return png_ptr->io_ptr; } -#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ +#endif /* PROGRESSIVE_READ */ diff --git a/3rdparty/libpng/pngpriv.h b/3rdparty/libpng/pngpriv.h index 880d2996f..e30b0434a 100644 --- a/3rdparty/libpng/pngpriv.h +++ b/3rdparty/libpng/pngpriv.h @@ -1,20 +1,18 @@ /* pngpriv.h - private declarations for use inside libpng * - * For conditions of distribution and use, see copyright notice in png.h - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.18 [July 23, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * - * Last changed in libpng 1.5.10 [March 29, 2012] - * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h */ /* The symbols declared in this file (including the functions declared - * as PNG_EXTERN) are PRIVATE. They are not part of the libpng public + * as extern) are PRIVATE. They are not part of the libpng public * interface, and are not recommended for use by regular applications. * Some of them may become public in the future; others may stay private, * change in an incompatible way, or even disappear. @@ -39,16 +37,44 @@ */ #define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */ -/* This is required for the definition of abort(), used as a last ditch - * error handler when all else fails. - */ -#include - -/* This is used to find 'offsetof', used below for alignment tests. */ -#include +#ifndef PNG_VERSION_INFO_ONLY +/* Standard library headers not required by png.h: */ +# include +# include +#endif #define PNGLIB_BUILD /*libpng is being built, not used*/ +/* If HAVE_CONFIG_H is defined during the build then the build system must + * provide an appropriate "config.h" file on the include path. The header file + * must provide definitions as required below (search for "HAVE_CONFIG_H"); + * see configure.ac for more details of the requirements. The macro + * "PNG_NO_CONFIG_H" is provided for maintainers to test for dependencies on + * 'configure'; define this macro to prevent the configure build including the + * configure generated config.h. Libpng is expected to compile without *any* + * special build system support on a reasonably ANSI-C compliant system. + */ +#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H) +# include + + /* Pick up the definition of 'restrict' from config.h if it was read: */ +# define PNG_RESTRICT restrict +#endif + +/* To support symbol prefixing it is necessary to know *before* including png.h + * whether the fixed point (and maybe other) APIs are exported, because if they + * are not internal definitions may be required. This is handled below just + * before png.h is included, but load the configuration now if it is available. + */ +#ifndef PNGLCONF_H +# include "pnglibconf.h" +#endif + +/* Local renames may change non-exported API functions from png.h */ +#if defined(PNG_PREFIX) && !defined(PNGPREFIX_H) +# include "pngprefix.h" +#endif + #ifdef PNG_USER_CONFIG # include "pngusr.h" /* These should have been defined in pngusr.h */ @@ -60,6 +86,102 @@ # endif #endif +/* Compile time options. + * ===================== + * In a multi-arch build the compiler may compile the code several times for the + * same object module, producing different binaries for different architectures. + * When this happens configure-time setting of the target host options cannot be + * done and this interferes with the handling of the ARM NEON optimizations, and + * possibly other similar optimizations. Put additional tests here; in general + * this is needed when the same option can be changed at both compile time and + * run time depending on the target OS (i.e. iOS vs Android.) + * + * NOTE: symbol prefixing does not pass $(CFLAGS) to the preprocessor, because + * this is not possible with certain compilers (Oracle SUN OS CC), as a result + * it is necessary to ensure that all extern functions that *might* be used + * regardless of $(CFLAGS) get declared in this file. The test on __ARM_NEON__ + * below is one example of this behavior because it is controlled by the + * presence or not of -mfpu=neon on the GCC command line, it is possible to do + * this in $(CC), e.g. "CC=gcc -mfpu=neon", but people who build libpng rarely + * do this. + */ +#ifndef PNG_ARM_NEON_OPT + /* ARM NEON optimizations are being controlled by the compiler settings, + * typically the target FPU. If the FPU has been set to NEON (-mfpu=neon + * with GCC) then the compiler will define __ARM_NEON__ and we can rely + * unconditionally on NEON instructions not crashing, otherwise we must + * disable use of NEON instructions. + * + * NOTE: at present these optimizations depend on 'ALIGNED_MEMORY', so they + * can only be turned on automatically if that is supported too. If + * PNG_ARM_NEON_OPT is set in CPPFLAGS (to >0) then arm/arm_init.c will fail + * to compile with an appropriate #error if ALIGNED_MEMORY has been turned + * off. + * + * Note that gcc-4.9 defines __ARM_NEON instead of the deprecated + * __ARM_NEON__, so we check both variants. + * + * To disable ARM_NEON optimizations entirely, and skip compiling the + * associated assembler code, pass --enable-arm-neon=no to configure + * or put -DPNG_ARM_NEON_OPT=0 in CPPFLAGS. + */ +# if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \ + defined(PNG_ALIGNED_MEMORY_SUPPORTED) +# define PNG_ARM_NEON_OPT 2 +# else +# define PNG_ARM_NEON_OPT 0 +# endif +#endif + +#if PNG_ARM_NEON_OPT > 0 + /* NEON optimizations are to be at least considered by libpng, so enable the + * callbacks to do this. + */ +# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_neon + + /* By default the 'intrinsics' code in arm/filter_neon_intrinsics.c is used + * if possible - if __ARM_NEON__ is set and the compiler version is not known + * to be broken. This is controlled by PNG_ARM_NEON_IMPLEMENTATION which can + * be: + * + * 1 The intrinsics code (the default with __ARM_NEON__) + * 2 The hand coded assembler (the default without __ARM_NEON__) + * + * It is possible to set PNG_ARM_NEON_IMPLEMENTATION in CPPFLAGS, however + * this is *NOT* supported and may cease to work even after a minor revision + * to libpng. It *is* valid to do this for testing purposes, e.g. speed + * testing or a new compiler, but the results should be communicated to the + * libpng implementation list for incorporation in the next minor release. + */ +# ifndef PNG_ARM_NEON_IMPLEMENTATION +# if defined(__ARM_NEON__) || defined(__ARM_NEON) +# if defined(__clang__) + /* At present it is unknown by the libpng developers which versions + * of clang support the intrinsics, however some or perhaps all + * versions do not work with the assembler so this may be + * irrelevant, so just use the default (do nothing here.) + */ +# elif defined(__GNUC__) + /* GCC 4.5.4 NEON support is known to be broken. 4.6.3 is known to + * work, so if this *is* GCC, or G++, look for a version >4.5 + */ +# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6) +# define PNG_ARM_NEON_IMPLEMENTATION 2 +# endif /* no GNUC support */ +# endif /* __GNUC__ */ +# else /* !defined __ARM_NEON__ */ + /* The 'intrinsics' code simply won't compile without this -mfpu=neon: + */ +# define PNG_ARM_NEON_IMPLEMENTATION 2 +# endif /* __ARM_NEON__ */ +# endif /* !PNG_ARM_NEON_IMPLEMENTATION */ + +# ifndef PNG_ARM_NEON_IMPLEMENTATION + /* Use the intrinsics code by default. */ +# define PNG_ARM_NEON_IMPLEMENTATION 1 +# endif +#endif /* PNG_ARM_NEON_OPT > 0 */ + /* Is this a build of a DLL where compilation of the object modules requires * different preprocessor settings to those required for a simple library? If * so PNG_BUILD_DLL must be set. @@ -124,76 +246,82 @@ # define PNG_PRIVATE #endif +/* Symbol preprocessing support. + * + * To enable listing global, but internal, symbols the following macros should + * always be used to declare an extern data or function object in this file. + */ +#ifndef PNG_INTERNAL_DATA +# define PNG_INTERNAL_DATA(type, name, array) PNG_LINKAGE_DATA type name array +#endif + +#ifndef PNG_INTERNAL_FUNCTION +# define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\ + PNG_LINKAGE_FUNCTION PNG_FUNCTION(type, name, args, PNG_EMPTY attributes) +#endif + +#ifndef PNG_INTERNAL_CALLBACK +# define PNG_INTERNAL_CALLBACK(type, name, args, attributes)\ + PNG_LINKAGE_CALLBACK PNG_FUNCTION(type, (PNGCBAPI name), args,\ + PNG_EMPTY attributes) +#endif + +/* If floating or fixed point APIs are disabled they may still be compiled + * internally. To handle this make sure they are declared as the appropriate + * internal extern function (otherwise the symbol prefixing stuff won't work and + * the functions will be used without definitions.) + * + * NOTE: although all the API functions are declared here they are not all + * actually built! Because the declarations are still made it is necessary to + * fake out types that they depend on. + */ +#ifndef PNG_FP_EXPORT +# ifndef PNG_FLOATING_POINT_SUPPORTED +# define PNG_FP_EXPORT(ordinal, type, name, args)\ + PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY); +# ifndef PNG_VERSION_INFO_ONLY + typedef struct png_incomplete png_double; + typedef png_double* png_doublep; + typedef const png_double* png_const_doublep; + typedef png_double** png_doublepp; +# endif +# endif +#endif +#ifndef PNG_FIXED_EXPORT +# ifndef PNG_FIXED_POINT_SUPPORTED +# define PNG_FIXED_EXPORT(ordinal, type, name, args)\ + PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY); +# endif +#endif + #include "png.h" -#include "pnginfo.h" -#include "pngstruct.h" /* pngconf.h does not set PNG_DLL_EXPORT unless it is required, so: */ #ifndef PNG_DLL_EXPORT # define PNG_DLL_EXPORT #endif +/* This is a global switch to set the compilation for an installed system + * (a release build). It can be set for testing debug builds to ensure that + * they will compile when the build type is switched to RC or STABLE, the + * default is just to use PNG_LIBPNG_BUILD_BASE_TYPE. Set this in CPPFLAGS + * with either: + * + * -DPNG_RELEASE_BUILD Turns on the release compile path + * -DPNG_RELEASE_BUILD=0 Turns it off + * or in your pngusr.h with + * #define PNG_RELEASE_BUILD=1 Turns on the release compile path + * #define PNG_RELEASE_BUILD=0 Turns it off + */ +#ifndef PNG_RELEASE_BUILD +# define PNG_RELEASE_BUILD (PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC) +#endif + /* SECURITY and SAFETY: * - * By default libpng is built without any internal limits on image size, - * individual heap (png_malloc) allocations or the total amount of memory used. - * If PNG_SAFE_LIMITS_SUPPORTED is defined, however, the limits below are used - * (unless individually overridden). These limits are believed to be fairly - * safe, but builders of secure systems should verify the values against the - * real system capabilities. - */ - -#ifdef PNG_SAFE_LIMITS_SUPPORTED - /* 'safe' limits */ -# ifndef PNG_USER_WIDTH_MAX -# define PNG_USER_WIDTH_MAX 1000000 -# endif -# ifndef PNG_USER_HEIGHT_MAX -# define PNG_USER_HEIGHT_MAX 1000000 -# endif -# ifndef PNG_USER_CHUNK_CACHE_MAX -# define PNG_USER_CHUNK_CACHE_MAX 128 -# endif -# ifndef PNG_USER_CHUNK_MALLOC_MAX -# define PNG_USER_CHUNK_MALLOC_MAX 8000000 -# endif -#else - /* values for no limits */ -# ifndef PNG_USER_WIDTH_MAX -# define PNG_USER_WIDTH_MAX 0x7fffffff -# endif -# ifndef PNG_USER_HEIGHT_MAX -# define PNG_USER_HEIGHT_MAX 0x7fffffff -# endif -# ifndef PNG_USER_CHUNK_CACHE_MAX -# define PNG_USER_CHUNK_CACHE_MAX 0 -# endif -# ifndef PNG_USER_CHUNK_MALLOC_MAX -# define PNG_USER_CHUNK_MALLOC_MAX 0 -# endif -#endif - -/* This is used for 16 bit gamma tables - only the top level pointers are const, - * this could be changed: - */ -typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; - -/* Added at libpng-1.2.9 */ -/* Moved to pngpriv.h at libpng-1.5.0 */ - -/* config.h is created by and PNG_CONFIGURE_LIBPNG is set by the "configure" - * script. We may need it here to get the correct configuration on things - * like limits. - */ -#ifdef PNG_CONFIGURE_LIBPNG -# ifdef HAVE_CONFIG_H -# include "config.h" -# endif -#endif - -/* Moved to pngpriv.h at libpng-1.5.0 */ -/* NOTE: some of these may have been used in external applications as - * these definitions were exposed in pngconf.h prior to 1.5. + * libpng is built with support for internal limits on image dimensions and + * memory usage. These are documented in scripts/pnglibconf.dfa of the + * source and recorded in the machine generated header file pnglibconf.h. */ /* If you are running on a machine where you cannot allocate more @@ -229,30 +357,6 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; # define PNG_ZBUF_SIZE 65536L #endif -/* PNG_STATIC is used to mark internal file scope functions if they need to be - * accessed for implementation tests (see the code in tests/?*). - */ -#ifndef PNG_STATIC -# define PNG_STATIC static -#endif - -/* C99 restrict is used where possible, to do this 'restrict' is defined as - * empty if we can't be sure it is supported. configure builds have already - * done this work. - */ -#ifdef PNG_CONFIGURE_LIBPNG -# define PNG_RESTRICT restrict -#else - /* Modern compilers support restrict, but assume not for anything not - * recognized here: - */ -# if defined __GNUC__ || defined _MSC_VER || defined __WATCOMC__ -# define PNG_RESTRICT restrict -# else -# define PNG_RESTRICT -# endif -#endif - /* If warnings or errors are turned off the code is disabled or redirected here. * From 1.5.4 functions have been added to allow very limited formatting of * error and warning messages - this code will also be disabled here. @@ -260,8 +364,6 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #ifdef PNG_WARNINGS_SUPPORTED # define PNG_WARNING_PARAMETERS(p) png_warning_parameters p; #else -# define png_warning(s1,s2) ((void)(s1)) -# define png_chunk_warning(s1,s2) ((void)(s1)) # define png_warning_parameter(p,number,string) ((void)0) # define png_warning_parameter_unsigned(p,number,format,value) ((void)0) # define png_warning_parameter_signed(p,number,format,value) ((void)0) @@ -269,8 +371,6 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; # define PNG_WARNING_PARAMETERS(p) #endif #ifndef PNG_ERROR_TEXT_SUPPORTED -# define png_error(s1,s2) png_err(s1) -# define png_chunk_error(s1,s2) png_err(s1) # define png_fixed_error(s1,s2) png_err(s1) #endif @@ -281,23 +381,18 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; */ #ifdef __cplusplus # define png_voidcast(type, value) static_cast(value) +# define png_constcast(type, value) const_cast(value) +# define png_aligncast(type, value) \ + static_cast(static_cast(value)) +# define png_aligncastconst(type, value) \ + static_cast(static_cast(value)) #else # define png_voidcast(type, value) (value) +# define png_constcast(type, value) ((type)(value)) +# define png_aligncast(type, value) ((void*)(value)) +# define png_aligncastconst(type, value) ((const void*)(value)) #endif /* __cplusplus */ -#ifndef PNG_EXTERN -/* The functions exported by PNG_EXTERN are internal functions, which - * aren't usually used outside the library (as far as I know), so it is - * debatable if they should be exported at all. In the future, when it - * is possible to have run-time registry of chunk-handling functions, - * some of these might be made available again. - * - * 1.5.7: turned the use of 'extern' back on, since it is localized to pngpriv.h - * it should be safe now (it is unclear why it was turned off.) - */ -# define PNG_EXTERN extern -#endif - /* Some fixed point APIs are still required even if not exported because * they get used by the corresponding floating point APIs. This magic * deals with this: @@ -308,6 +403,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; # define PNGFAPI /* PRIVATE */ #endif +#ifndef PNG_VERSION_INFO_ONLY /* Other defines specific to compilers can go here. Try to keep * them inside an appropriate ifdef/endif pair for portability. */ @@ -352,6 +448,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; defined(_WIN32) || defined(__WIN32__) # include /* defines _WINDOWS_ macro */ #endif +#endif /* PNG_VERSION_INFO_ONLY */ /* Moved here around 1.5.0beta36 from pngconf.h */ /* Users may want to use these so they are not private. Any library @@ -367,34 +464,6 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; # endif #endif -#ifdef USE_FAR_KEYWORD -/* Use this to make far-to-near assignments */ -# define CHECK 1 -# define NOCHECK 0 -# define CVT_PTR(ptr) (png_far_to_near(png_ptr,ptr,CHECK)) -# define CVT_PTR_NOCHECK(ptr) (png_far_to_near(png_ptr,ptr,NOCHECK)) -# define png_strlen _fstrlen -# define png_memcmp _fmemcmp /* SJT: added */ -# define png_memcpy _fmemcpy -# define png_memset _fmemset -#else -# if defined(_WINDOWS_) && !defined(WINRT) /* Favor Windows over C runtime fns */ -# define CVT_PTR(ptr) (ptr) -# define CVT_PTR_NOCHECK(ptr) (ptr) -# define png_strlen lstrlenA -# define png_memcmp memcmp -# define png_memcpy CopyMemory -# define png_memset memset -# else -# define CVT_PTR(ptr) (ptr) -# define CVT_PTR_NOCHECK(ptr) (ptr) -# define png_strlen strlen -# define png_memcmp memcmp /* SJT: added */ -# define png_memcpy memcpy -# define png_memset memset -# endif -#endif - /* These macros may need to be architecture dependent. */ #define PNG_ALIGN_NONE 0 /* do not use data alignment */ #define PNG_ALIGN_ALWAYS 1 /* assume unaligned accesses are OK */ @@ -416,7 +485,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #if PNG_ALIGN_TYPE == PNG_ALIGN_SIZE /* This is used because in some compiler implementations non-aligned * structure members are supported, so the offsetof approach below fails. - * Set PNG_ALIGN_TO_SIZE=0 for compiler combinations where unaligned access + * Set PNG_ALIGN_SIZE=0 for compiler combinations where unaligned access * is good for performance. Do not do this unless you have tested the result * and understand it. */ @@ -457,16 +526,17 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #define PNG_HAVE_IDAT 0x04 /* #define PNG_AFTER_IDAT 0x08 (defined in png.h) */ #define PNG_HAVE_IEND 0x10 -#define PNG_HAVE_gAMA 0x20 -#define PNG_HAVE_cHRM 0x40 -#define PNG_HAVE_sRGB 0x80 + /* 0x20 (unused) */ + /* 0x40 (unused) */ + /* 0x80 (unused) */ #define PNG_HAVE_CHUNK_HEADER 0x100 #define PNG_WROTE_tIME 0x200 #define PNG_WROTE_INFO_BEFORE_PLTE 0x400 #define PNG_BACKGROUND_IS_GRAY 0x800 #define PNG_HAVE_PNG_SIGNATURE 0x1000 #define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000 /* Have another chunk after IDAT */ -#define PNG_HAVE_iCCP 0x4000 + /* 0x4000 (unused) */ +#define PNG_IS_READ_STRUCT 0x8000 /* Else is a write struct */ /* Flags for the transformations the PNG library does on the image data */ #define PNG_BGR 0x0001 @@ -494,53 +564,49 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #define PNG_RGB_TO_GRAY_WARN 0x400000 #define PNG_RGB_TO_GRAY 0x600000 /* two bits, RGB_TO_GRAY_ERR|WARN */ #define PNG_ENCODE_ALPHA 0x800000 /* Added to libpng-1.5.4 */ -#define PNG_ADD_ALPHA 0x1000000 /* Added to libpng-1.2.7 */ -#define PNG_EXPAND_tRNS 0x2000000 /* Added to libpng-1.2.9 */ -#define PNG_SCALE_16_TO_8 0x4000000 /* Added to libpng-1.5.4 */ - /* 0x8000000 unused */ - /* 0x10000000 unused */ - /* 0x20000000 unused */ - /* 0x40000000 unused */ +#define PNG_ADD_ALPHA 0x1000000 /* Added to libpng-1.2.7 */ +#define PNG_EXPAND_tRNS 0x2000000 /* Added to libpng-1.2.9 */ +#define PNG_SCALE_16_TO_8 0x4000000 /* Added to libpng-1.5.4 */ + /* 0x8000000 unused */ + /* 0x10000000 unused */ + /* 0x20000000 unused */ + /* 0x40000000 unused */ /* Flags for png_create_struct */ #define PNG_STRUCT_PNG 0x0001 #define PNG_STRUCT_INFO 0x0002 -/* Scaling factor for filter heuristic weighting calculations */ -#define PNG_WEIGHT_FACTOR (1<<(PNG_WEIGHT_SHIFT)) -#define PNG_COST_FACTOR (1<<(PNG_COST_SHIFT)) - /* Flags for the png_ptr->flags rather than declaring a byte for each one */ #define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001 -#define PNG_FLAG_ZLIB_CUSTOM_LEVEL 0x0002 -#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL 0x0004 -#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS 0x0008 -#define PNG_FLAG_ZLIB_CUSTOM_METHOD 0x0010 -#define PNG_FLAG_ZLIB_FINISHED 0x0020 +#define PNG_FLAG_ZSTREAM_INITIALIZED 0x0002 /* Added to libpng-1.6.0 */ + /* 0x0004 unused */ +#define PNG_FLAG_ZSTREAM_ENDED 0x0008 /* Added to libpng-1.6.0 */ + /* 0x0010 unused */ + /* 0x0020 unused */ #define PNG_FLAG_ROW_INIT 0x0040 #define PNG_FLAG_FILLER_AFTER 0x0080 #define PNG_FLAG_CRC_ANCILLARY_USE 0x0100 #define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200 #define PNG_FLAG_CRC_CRITICAL_USE 0x0400 #define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800 -#define PNG_FLAG_ASSUME_sRGB 0x1000 /* Added to libpng-1.5.4 */ -#define PNG_FLAG_OPTIMIZE_ALPHA 0x2000 /* Added to libpng-1.5.4 */ -#define PNG_FLAG_DETECT_UNINITIALIZED 0x4000 /* Added to libpng-1.5.4 */ -#define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000 -#define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000 -#define PNG_FLAG_LIBRARY_MISMATCH 0x20000 -#define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000 -#define PNG_FLAG_STRIP_ERROR_TEXT 0x80000 -#define PNG_FLAG_MALLOC_NULL_MEM_OK 0x100000 - /* 0x200000 unused */ - /* 0x400000 unused */ -#define PNG_FLAG_BENIGN_ERRORS_WARN 0x800000 /* Added to libpng-1.4.0 */ -#define PNG_FLAG_ZTXT_CUSTOM_STRATEGY 0x1000000 /* 5 lines added */ -#define PNG_FLAG_ZTXT_CUSTOM_LEVEL 0x2000000 /* to libpng-1.5.4 */ -#define PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL 0x4000000 -#define PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS 0x8000000 -#define PNG_FLAG_ZTXT_CUSTOM_METHOD 0x10000000 - /* 0x20000000 unused */ - /* 0x40000000 unused */ +#define PNG_FLAG_ASSUME_sRGB 0x1000 /* Added to libpng-1.5.4 */ +#define PNG_FLAG_OPTIMIZE_ALPHA 0x2000 /* Added to libpng-1.5.4 */ +#define PNG_FLAG_DETECT_UNINITIALIZED 0x4000 /* Added to libpng-1.5.4 */ +/* #define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000 */ +/* #define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000 */ +#define PNG_FLAG_LIBRARY_MISMATCH 0x20000 +#define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000 +#define PNG_FLAG_STRIP_ERROR_TEXT 0x80000 +#define PNG_FLAG_BENIGN_ERRORS_WARN 0x100000 /* Added to libpng-1.4.0 */ +#define PNG_FLAG_APP_WARNINGS_WARN 0x200000 /* Added to libpng-1.6.0 */ +#define PNG_FLAG_APP_ERRORS_WARN 0x400000 /* Added to libpng-1.6.0 */ + /* 0x800000 unused */ + /* 0x1000000 unused */ + /* 0x2000000 unused */ + /* 0x4000000 unused */ + /* 0x8000000 unused */ + /* 0x10000000 unused */ + /* 0x20000000 unused */ + /* 0x40000000 unused */ #define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ PNG_FLAG_CRC_ANCILLARY_NOWARN) @@ -551,24 +617,23 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #define PNG_FLAG_CRC_MASK (PNG_FLAG_CRC_ANCILLARY_MASK | \ PNG_FLAG_CRC_CRITICAL_MASK) -/* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib - * can handle at once. This type need be no larger than 16 bits (so maximum of - * 65535), this define allows us to discover how big it is, but limited by the - * maximuum for png_size_t. The value can be overriden in a library build - * (pngusr.h, or set it in CPPFLAGS) and it works to set it to a considerably - * lower value (e.g. 255 works). A lower value may help memory usage (slightly) - * and may even improve performance on some systems (and degrade it on others.) - */ -#ifndef ZLIB_IO_MAX -# define ZLIB_IO_MAX ((uInt)-1) -#endif - /* Save typing and make code easier to understand */ #define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \ abs((int)((c1).green) - (int)((c2).green)) + \ abs((int)((c1).blue) - (int)((c2).blue))) +/* Added to libpng-1.6.0: scale a 16-bit value in the range 0..65535 to 0..255 + * by dividing by 257 *with rounding*. This macro is exact for the given range. + * See the discourse in pngrtran.c png_do_scale_16_to_8. The values in the + * macro were established by experiment (modifying the added value). The macro + * has a second variant that takes a value already scaled by 255 and divides by + * 65535 - this has a maximum error of .502. Over the range 0..65535*65535 it + * only gives off-by-one errors and only for 0.5% (1 in 200) of the values. + */ +#define PNG_DIV65535(v24) (((v24) + 32895) >> 16) +#define PNG_DIV257(v16) PNG_DIV65535((png_uint_32)(v16) * 255) + /* Added to libpng-1.2.6 JB */ #define PNG_ROWBYTES(pixel_bits, width) \ ((pixel_bits) >= 8 ? \ @@ -600,7 +665,7 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; /* The fixed point conversion performs range checking and evaluates * its argument multiple times, so must be used with care. The * range checking uses the PNG specification values for a signed - * 32 bit fixed point value except that the values are deliberately + * 32-bit fixed point value except that the values are deliberately * rounded-to-zero to an integral value - 21474 (21474.83 is roughly * (2^31-1) * 100000). 's' is a string that describes the value being * converted. @@ -616,10 +681,10 @@ typedef PNG_CONST png_uint_16p FAR * png_const_uint_16pp; #ifdef PNG_FIXED_POINT_MACRO_SUPPORTED #define png_fixed(png_ptr, fp, s) ((fp) <= 21474 && (fp) >= -21474 ?\ ((png_fixed_point)(100000 * (fp))) : (png_fixed_error(png_ptr, s),0)) -#else -PNG_EXTERN png_fixed_point png_fixed PNGARG((png_structp png_ptr, double fp, - png_const_charp text)); #endif +/* else the corresponding function is defined below, inside the scope of the + * cplusplus test. + */ #endif /* Constants for known chunk types. If you need to add a chunk, define the name @@ -636,53 +701,82 @@ PNG_EXTERN png_fixed_point png_fixed PNGARG((png_structp png_ptr, double fp, * architectures where (int) is only 16 bits. */ #define PNG_32b(b,s) ((png_uint_32)(b) << (s)) -#define PNG_CHUNK(b1,b2,b3,b4) \ +#define PNG_U32(b1,b2,b3,b4) \ (PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0)) -#define png_IHDR PNG_CHUNK( 73, 72, 68, 82) -#define png_IDAT PNG_CHUNK( 73, 68, 65, 84) -#define png_IEND PNG_CHUNK( 73, 69, 78, 68) -#define png_PLTE PNG_CHUNK( 80, 76, 84, 69) -#define png_bKGD PNG_CHUNK( 98, 75, 71, 68) -#define png_cHRM PNG_CHUNK( 99, 72, 82, 77) -#define png_gAMA PNG_CHUNK(103, 65, 77, 65) -#define png_hIST PNG_CHUNK(104, 73, 83, 84) -#define png_iCCP PNG_CHUNK(105, 67, 67, 80) -#define png_iTXt PNG_CHUNK(105, 84, 88, 116) -#define png_oFFs PNG_CHUNK(111, 70, 70, 115) -#define png_pCAL PNG_CHUNK(112, 67, 65, 76) -#define png_sCAL PNG_CHUNK(115, 67, 65, 76) -#define png_pHYs PNG_CHUNK(112, 72, 89, 115) -#define png_sBIT PNG_CHUNK(115, 66, 73, 84) -#define png_sPLT PNG_CHUNK(115, 80, 76, 84) -#define png_sRGB PNG_CHUNK(115, 82, 71, 66) -#define png_sTER PNG_CHUNK(115, 84, 69, 82) -#define png_tEXt PNG_CHUNK(116, 69, 88, 116) -#define png_tIME PNG_CHUNK(116, 73, 77, 69) -#define png_tRNS PNG_CHUNK(116, 82, 78, 83) -#define png_zTXt PNG_CHUNK(122, 84, 88, 116) +/* Constants for known chunk types. + * + * MAINTAINERS: If you need to add a chunk, define the name here. + * For historical reasons these constants have the form png_; i.e. + * the prefix is lower case. Please use decimal values as the parameters to + * match the ISO PNG specification and to avoid relying on the C locale + * interpretation of character values. Please keep the list sorted. + * + * Notice that PNG_U32 is used to define a 32-bit value for the 4 byte chunk + * type. In fact the specification does not express chunk types this way, + * however using a 32-bit value means that the chunk type can be read from the + * stream using exactly the same code as used for a 32-bit unsigned value and + * can be examined far more efficiently (using one arithmetic compare). + * + * Prior to 1.5.6 the chunk type constants were expressed as C strings. The + * libpng API still uses strings for 'unknown' chunks and a macro, + * PNG_STRING_FROM_CHUNK, allows a string to be generated if required. Notice + * that for portable code numeric values must still be used; the string "IHDR" + * is not portable and neither is PNG_U32('I', 'H', 'D', 'R'). + * + * In 1.7.0 the definitions will be made public in png.h to avoid having to + * duplicate the same definitions in application code. + */ +#define png_IDAT PNG_U32( 73, 68, 65, 84) +#define png_IEND PNG_U32( 73, 69, 78, 68) +#define png_IHDR PNG_U32( 73, 72, 68, 82) +#define png_PLTE PNG_U32( 80, 76, 84, 69) +#define png_bKGD PNG_U32( 98, 75, 71, 68) +#define png_cHRM PNG_U32( 99, 72, 82, 77) +#define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */ +#define png_gAMA PNG_U32(103, 65, 77, 65) +#define png_gIFg PNG_U32(103, 73, 70, 103) +#define png_gIFt PNG_U32(103, 73, 70, 116) /* deprecated */ +#define png_gIFx PNG_U32(103, 73, 70, 120) +#define png_hIST PNG_U32(104, 73, 83, 84) +#define png_iCCP PNG_U32(105, 67, 67, 80) +#define png_iTXt PNG_U32(105, 84, 88, 116) +#define png_oFFs PNG_U32(111, 70, 70, 115) +#define png_pCAL PNG_U32(112, 67, 65, 76) +#define png_pHYs PNG_U32(112, 72, 89, 115) +#define png_sBIT PNG_U32(115, 66, 73, 84) +#define png_sCAL PNG_U32(115, 67, 65, 76) +#define png_sPLT PNG_U32(115, 80, 76, 84) +#define png_sRGB PNG_U32(115, 82, 71, 66) +#define png_sTER PNG_U32(115, 84, 69, 82) +#define png_tEXt PNG_U32(116, 69, 88, 116) +#define png_tIME PNG_U32(116, 73, 77, 69) +#define png_tRNS PNG_U32(116, 82, 78, 83) +#define png_zTXt PNG_U32(122, 84, 88, 116) /* The following will work on (signed char*) strings, whereas the get_uint_32 * macro will fail on top-bit-set values because of the sign extension. */ #define PNG_CHUNK_FROM_STRING(s)\ - PNG_CHUNK(0xff&(s)[0], 0xff&(s)[1], 0xff&(s)[2], 0xff&(s)[3]) + PNG_U32(0xff & (s)[0], 0xff & (s)[1], 0xff & (s)[2], 0xff & (s)[3]) /* This uses (char), not (png_byte) to avoid warnings on systems where (char) is * signed and the argument is a (char[]) This macro will fail miserably on * systems where (char) is more than 8 bits. */ #define PNG_STRING_FROM_CHUNK(s,c)\ - (void)(((char*)(s))[0]=(char)((c)>>24), ((char*)(s))[1]=(char)((c)>>16),\ - ((char*)(s))[2]=(char)((c)>>8), ((char*)(s))[3]=(char)((c))) + (void)(((char*)(s))[0]=(char)(((c)>>24) & 0xff), \ + ((char*)(s))[1]=(char)(((c)>>16) & 0xff),\ + ((char*)(s))[2]=(char)(((c)>>8) & 0xff), \ + ((char*)(s))[3]=(char)((c & 0xff))) /* Do the same but terminate with a null character. */ #define PNG_CSTRING_FROM_CHUNK(s,c)\ (void)(PNG_STRING_FROM_CHUNK(s,c), ((char*)(s))[4] = 0) /* Test on flag values as defined in the spec (section 5.4): */ -#define PNG_CHUNK_ANCILLIARY(c) (1 & ((c) >> 29)) -#define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLIARY(c)) +#define PNG_CHUNK_ANCILLARY(c) (1 & ((c) >> 29)) +#define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c)) #define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21)) #define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13)) #define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5)) @@ -692,113 +786,212 @@ PNG_EXTERN png_fixed_point png_fixed PNGARG((png_structp png_ptr, double fp, #define PNG_GAMMA_MAC_INVERSE 65909 #define PNG_GAMMA_sRGB_INVERSE 45455 +/* Almost everything below is C specific; the #defines above can be used in + * non-C code (so long as it is C-preprocessed) the rest of this stuff cannot. + */ +#ifndef PNG_VERSION_INFO_ONLY + +#include "pngstruct.h" +#include "pnginfo.h" + +/* Validate the include paths - the include path used to generate pnglibconf.h + * must match that used in the build, or we must be using pnglibconf.h.prebuilt: + */ +#if PNG_ZLIB_VERNUM != 0 && PNG_ZLIB_VERNUM != ZLIB_VERNUM +# error ZLIB_VERNUM != PNG_ZLIB_VERNUM \ + "-I (include path) error: see the notes in pngpriv.h" + /* This means that when pnglibconf.h was built the copy of zlib.h that it + * used is not the same as the one being used here. Because the build of + * libpng makes decisions to use inflateInit2 and inflateReset2 based on the + * zlib version number and because this affects handling of certain broken + * PNG files the -I directives must match. + * + * The most likely explanation is that you passed a -I in CFLAGS. This will + * not work; all the preprocessor directories and in particular all the -I + * directives must be in CPPFLAGS. + */ +#endif + +/* This is used for 16-bit gamma tables -- only the top level pointers are + * const; this could be changed: + */ +typedef const png_uint_16p * png_const_uint_16pp; + +/* Added to libpng-1.5.7: sRGB conversion tables */ +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) +#ifdef PNG_SIMPLIFIED_READ_SUPPORTED +PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_table, [256]); + /* Convert from an sRGB encoded value 0..255 to a 16-bit linear value, + * 0..65535. This table gives the closest 16-bit answers (no errors). + */ +#endif + +PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_base, [512]); +PNG_INTERNAL_DATA(const png_byte, png_sRGB_delta, [512]); + +#define PNG_sRGB_FROM_LINEAR(linear) \ + ((png_byte)(0xff & ((png_sRGB_base[(linear)>>15] \ + + ((((linear) & 0x7fff)*png_sRGB_delta[(linear)>>15])>>12)) >> 8))) + /* Given a value 'linear' in the range 0..255*65535 calculate the 8-bit sRGB + * encoded value with maximum error 0.646365. Note that the input is not a + * 16-bit value; it has been multiplied by 255! */ +#endif /* SIMPLIFIED_READ/WRITE */ + /* Inhibit C++ name-mangling for libpng functions but not for system calls. */ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -/* These functions are used internally in the code. They generally - * shouldn't be used unless you are writing code to add or replace some - * functionality in libpng. More information about most functions can - * be found in the files where the functions are located. +/* Internal functions; these are not exported from a DLL however because they + * are used within several of the C source files they have to be C extern. + * + * All of these functions must be declared with PNG_INTERNAL_FUNCTION. */ +/* Zlib support */ +#define PNG_UNEXPECTED_ZLIB_RETURN (-7) +PNG_INTERNAL_FUNCTION(void, png_zstream_error,(png_structrp png_ptr, int ret), + PNG_EMPTY); + /* Used by the zlib handling functions to ensure that z_stream::msg is always + * set before they return. + */ + +#ifdef PNG_WRITE_SUPPORTED +PNG_INTERNAL_FUNCTION(void,png_free_buffer_list,(png_structrp png_ptr, + png_compression_bufferp *list),PNG_EMPTY); + /* Free the buffer list used by the compressed write code. */ +#endif + +#if defined(PNG_FLOATING_POINT_SUPPORTED) && \ + !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ + (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ + defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ + defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ + (defined(PNG_sCAL_SUPPORTED) && \ + defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) +PNG_INTERNAL_FUNCTION(png_fixed_point,png_fixed,(png_const_structrp png_ptr, + double fp, png_const_charp text),PNG_EMPTY); +#endif + /* Check the user version string for compatibility, returns false if the version * numbers aren't compatible. */ -PNG_EXTERN int png_user_version_check(png_structp png_ptr, - png_const_charp user_png_ver); +PNG_INTERNAL_FUNCTION(int,png_user_version_check,(png_structrp png_ptr, + png_const_charp user_png_ver),PNG_EMPTY); -/* Allocate memory for an internal libpng struct */ -PNG_EXTERN PNG_FUNCTION(png_voidp,png_create_struct,PNGARG((int type)), - PNG_ALLOCATED); +/* Internal base allocator - no messages, NULL on failure to allocate. This + * does, however, call the application provided allocator and that could call + * png_error (although that would be a bug in the application implementation.) + */ +PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_base,(png_const_structrp png_ptr, + png_alloc_size_t size),PNG_ALLOCATED); + +#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ + defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) +/* Internal array allocator, outputs no error or warning messages on failure, + * just returns NULL. + */ +PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_array,(png_const_structrp png_ptr, + int nelements, size_t element_size),PNG_ALLOCATED); + +/* The same but an existing array is extended by add_elements. This function + * also memsets the new elements to 0 and copies the old elements. The old + * array is not freed or altered. + */ +PNG_INTERNAL_FUNCTION(png_voidp,png_realloc_array,(png_const_structrp png_ptr, + png_const_voidp array, int old_elements, int add_elements, + size_t element_size),PNG_ALLOCATED); +#endif /* text, sPLT or unknown chunks */ + +/* Magic to create a struct when there is no struct to call the user supplied + * memory allocators. Because error handling has not been set up the memory + * handlers can't safely call png_error, but this is an obscure and undocumented + * restriction so libpng has to assume that the 'free' handler, at least, might + * call png_error. + */ +PNG_INTERNAL_FUNCTION(png_structp,png_create_png_struct, + (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, + png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, + png_free_ptr free_fn),PNG_ALLOCATED); /* Free memory from internal libpng struct */ -PNG_EXTERN void png_destroy_struct PNGARG((png_voidp struct_ptr)); +PNG_INTERNAL_FUNCTION(void,png_destroy_png_struct,(png_structrp png_ptr), + PNG_EMPTY); -PNG_EXTERN PNG_FUNCTION(png_voidp,png_create_struct_2, - PNGARG((int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)), - PNG_ALLOCATED); -PNG_EXTERN void png_destroy_struct_2 PNGARG((png_voidp struct_ptr, - png_free_ptr free_fn, png_voidp mem_ptr)); - -/* Free any memory that info_ptr points to and reset struct. */ -PNG_EXTERN void png_info_destroy PNGARG((png_structp png_ptr, - png_infop info_ptr)); +/* Free an allocated jmp_buf (always succeeds) */ +PNG_INTERNAL_FUNCTION(void,png_free_jmpbuf,(png_structrp png_ptr),PNG_EMPTY); /* Function to allocate memory for zlib. PNGAPI is disallowed. */ -PNG_EXTERN PNG_FUNCTION(voidpf,png_zalloc,PNGARG((voidpf png_ptr, uInt items, - uInt size)),PNG_ALLOCATED); +PNG_INTERNAL_FUNCTION(voidpf,png_zalloc,(voidpf png_ptr, uInt items, uInt size), + PNG_ALLOCATED); /* Function to free memory for zlib. PNGAPI is disallowed. */ -PNG_EXTERN void png_zfree PNGARG((voidpf png_ptr, voidpf ptr)); +PNG_INTERNAL_FUNCTION(void,png_zfree,(voidpf png_ptr, voidpf ptr),PNG_EMPTY); /* Next four functions are used internally as callbacks. PNGCBAPI is required * but not PNG_EXPORT. PNGAPI added at libpng version 1.2.3, changed to * PNGCBAPI at 1.5.0 */ -PNG_EXTERN void PNGCBAPI png_default_read_data PNGARG((png_structp png_ptr, - png_bytep data, png_size_t length)); +PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_read_data,(png_structp png_ptr, + png_bytep data, png_size_t length),PNG_EMPTY); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED -PNG_EXTERN void PNGCBAPI png_push_fill_buffer PNGARG((png_structp png_ptr, - png_bytep buffer, png_size_t length)); +PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_push_fill_buffer,(png_structp png_ptr, + png_bytep buffer, png_size_t length),PNG_EMPTY); #endif -PNG_EXTERN void PNGCBAPI png_default_write_data PNGARG((png_structp png_ptr, - png_bytep data, png_size_t length)); +PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_write_data,(png_structp png_ptr, + png_bytep data, png_size_t length),PNG_EMPTY); #ifdef PNG_WRITE_FLUSH_SUPPORTED # ifdef PNG_STDIO_SUPPORTED -PNG_EXTERN void PNGCBAPI png_default_flush PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_flush,(png_structp png_ptr), + PNG_EMPTY); # endif #endif /* Reset the CRC variable */ -PNG_EXTERN void png_reset_crc PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_reset_crc,(png_structrp png_ptr),PNG_EMPTY); /* Write the "data" buffer to whatever output you are using */ -PNG_EXTERN void png_write_data PNGARG((png_structp png_ptr, - png_const_bytep data, png_size_t length)); +PNG_INTERNAL_FUNCTION(void,png_write_data,(png_structrp png_ptr, + png_const_bytep data, png_size_t length),PNG_EMPTY); /* Read and check the PNG file signature */ -PNG_EXTERN void png_read_sig PNGARG((png_structp png_ptr, png_infop info_ptr)); +PNG_INTERNAL_FUNCTION(void,png_read_sig,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); /* Read the chunk header (length + type name) */ -PNG_EXTERN png_uint_32 png_read_chunk_header PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(png_uint_32,png_read_chunk_header,(png_structrp png_ptr), + PNG_EMPTY); /* Read data from whatever input you are using into the "data" buffer */ -PNG_EXTERN void png_read_data PNGARG((png_structp png_ptr, png_bytep data, - png_size_t length)); +PNG_INTERNAL_FUNCTION(void,png_read_data,(png_structrp png_ptr, png_bytep data, + png_size_t length),PNG_EMPTY); /* Read bytes into buf, and update png_ptr->crc */ -PNG_EXTERN void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, - png_size_t length)); - -/* Decompress data in a chunk that uses compression */ -#if defined(PNG_READ_COMPRESSED_TEXT_SUPPORTED) -PNG_EXTERN void png_decompress_chunk PNGARG((png_structp png_ptr, - int comp_type, png_size_t chunklength, png_size_t prefix_length, - png_size_t *data_length)); -#endif +PNG_INTERNAL_FUNCTION(void,png_crc_read,(png_structrp png_ptr, png_bytep buf, + png_uint_32 length),PNG_EMPTY); /* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */ -PNG_EXTERN int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip)); +PNG_INTERNAL_FUNCTION(int,png_crc_finish,(png_structrp png_ptr, + png_uint_32 skip),PNG_EMPTY); /* Read the CRC from the file and compare it to the libpng calculated CRC */ -PNG_EXTERN int png_crc_error PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(int,png_crc_error,(png_structrp png_ptr),PNG_EMPTY); /* Calculate the CRC over a section of data. Note that we are only * passing a maximum of 64K on systems that have this as a memory limit, * since this is the maximum buffer size we can specify. */ -PNG_EXTERN void png_calculate_crc PNGARG((png_structp png_ptr, - png_const_bytep ptr, png_size_t length)); +PNG_INTERNAL_FUNCTION(void,png_calculate_crc,(png_structrp png_ptr, + png_const_bytep ptr, png_size_t length),PNG_EMPTY); #ifdef PNG_WRITE_FLUSH_SUPPORTED -PNG_EXTERN void png_flush PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_flush,(png_structrp png_ptr),PNG_EMPTY); #endif /* Write various chunks */ @@ -806,145 +999,130 @@ PNG_EXTERN void png_flush PNGARG((png_structp png_ptr)); /* Write the IHDR chunk, and update the png_struct with the necessary * information. */ -PNG_EXTERN void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width, - png_uint_32 height, - int bit_depth, int color_type, int compression_method, int filter_method, - int interlace_method)); +PNG_INTERNAL_FUNCTION(void,png_write_IHDR,(png_structrp png_ptr, + png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, + int compression_method, int filter_method, int interlace_method),PNG_EMPTY); -PNG_EXTERN void png_write_PLTE PNGARG((png_structp png_ptr, - png_const_colorp palette, png_uint_32 num_pal)); +PNG_INTERNAL_FUNCTION(void,png_write_PLTE,(png_structrp png_ptr, + png_const_colorp palette, png_uint_32 num_pal),PNG_EMPTY); -PNG_EXTERN void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data, - png_size_t length)); +PNG_INTERNAL_FUNCTION(void,png_compress_IDAT,(png_structrp png_ptr, + png_const_bytep row_data, png_alloc_size_t row_data_length, int flush), + PNG_EMPTY); -PNG_EXTERN void png_write_IEND PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_write_IEND,(png_structrp png_ptr),PNG_EMPTY); #ifdef PNG_WRITE_gAMA_SUPPORTED -# ifdef PNG_FLOATING_POINT_SUPPORTED -PNG_EXTERN void png_write_gAMA PNGARG((png_structp png_ptr, double file_gamma)); -# endif -# ifdef PNG_FIXED_POINT_SUPPORTED -PNG_EXTERN void png_write_gAMA_fixed PNGARG((png_structp png_ptr, - png_fixed_point file_gamma)); -# endif +PNG_INTERNAL_FUNCTION(void,png_write_gAMA_fixed,(png_structrp png_ptr, + png_fixed_point file_gamma),PNG_EMPTY); #endif #ifdef PNG_WRITE_sBIT_SUPPORTED -PNG_EXTERN void png_write_sBIT PNGARG((png_structp png_ptr, - png_const_color_8p sbit, int color_type)); +PNG_INTERNAL_FUNCTION(void,png_write_sBIT,(png_structrp png_ptr, + png_const_color_8p sbit, int color_type),PNG_EMPTY); #endif #ifdef PNG_WRITE_cHRM_SUPPORTED -# ifdef PNG_FLOATING_POINT_SUPPORTED -PNG_EXTERN void png_write_cHRM PNGARG((png_structp png_ptr, - double white_x, double white_y, - double red_x, double red_y, double green_x, double green_y, - double blue_x, double blue_y)); -# endif -PNG_EXTERN void png_write_cHRM_fixed PNGARG((png_structp png_ptr, - png_fixed_point int_white_x, png_fixed_point int_white_y, - png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point - int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, - png_fixed_point int_blue_y)); +PNG_INTERNAL_FUNCTION(void,png_write_cHRM_fixed,(png_structrp png_ptr, + const png_xy *xy), PNG_EMPTY); + /* The xy value must have been previously validated */ #endif #ifdef PNG_WRITE_sRGB_SUPPORTED -PNG_EXTERN void png_write_sRGB PNGARG((png_structp png_ptr, - int intent)); +PNG_INTERNAL_FUNCTION(void,png_write_sRGB,(png_structrp png_ptr, + int intent),PNG_EMPTY); #endif #ifdef PNG_WRITE_iCCP_SUPPORTED -PNG_EXTERN void png_write_iCCP PNGARG((png_structp png_ptr, - png_const_charp name, int compression_type, - png_const_charp profile, int proflen)); - /* Note to maintainer: profile should be png_bytep */ +PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr, + png_const_charp name, png_const_bytep profile), PNG_EMPTY); + /* The profile must have been previously validated for correctness, the + * length comes from the first four bytes. Only the base, deflate, + * compression is supported. + */ #endif #ifdef PNG_WRITE_sPLT_SUPPORTED -PNG_EXTERN void png_write_sPLT PNGARG((png_structp png_ptr, - png_const_sPLT_tp palette)); +PNG_INTERNAL_FUNCTION(void,png_write_sPLT,(png_structrp png_ptr, + png_const_sPLT_tp palette),PNG_EMPTY); #endif #ifdef PNG_WRITE_tRNS_SUPPORTED -PNG_EXTERN void png_write_tRNS PNGARG((png_structp png_ptr, +PNG_INTERNAL_FUNCTION(void,png_write_tRNS,(png_structrp png_ptr, png_const_bytep trans, png_const_color_16p values, int number, - int color_type)); + int color_type),PNG_EMPTY); #endif #ifdef PNG_WRITE_bKGD_SUPPORTED -PNG_EXTERN void png_write_bKGD PNGARG((png_structp png_ptr, - png_const_color_16p values, int color_type)); +PNG_INTERNAL_FUNCTION(void,png_write_bKGD,(png_structrp png_ptr, + png_const_color_16p values, int color_type),PNG_EMPTY); #endif #ifdef PNG_WRITE_hIST_SUPPORTED -PNG_EXTERN void png_write_hIST PNGARG((png_structp png_ptr, - png_const_uint_16p hist, int num_hist)); +PNG_INTERNAL_FUNCTION(void,png_write_hIST,(png_structrp png_ptr, + png_const_uint_16p hist, int num_hist),PNG_EMPTY); #endif /* Chunks that have keywords */ -#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ - defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) -PNG_EXTERN png_size_t png_check_keyword PNGARG((png_structp png_ptr, - png_const_charp key, png_charpp new_key)); -#endif - #ifdef PNG_WRITE_tEXt_SUPPORTED -PNG_EXTERN void png_write_tEXt PNGARG((png_structp png_ptr, png_const_charp key, - png_const_charp text, png_size_t text_len)); +PNG_INTERNAL_FUNCTION(void,png_write_tEXt,(png_structrp png_ptr, + png_const_charp key, png_const_charp text, png_size_t text_len),PNG_EMPTY); #endif #ifdef PNG_WRITE_zTXt_SUPPORTED -PNG_EXTERN void png_write_zTXt PNGARG((png_structp png_ptr, png_const_charp key, - png_const_charp text, png_size_t text_len, int compression)); +PNG_INTERNAL_FUNCTION(void,png_write_zTXt,(png_structrp png_ptr, png_const_charp + key, png_const_charp text, int compression),PNG_EMPTY); #endif #ifdef PNG_WRITE_iTXt_SUPPORTED -PNG_EXTERN void png_write_iTXt PNGARG((png_structp png_ptr, +PNG_INTERNAL_FUNCTION(void,png_write_iTXt,(png_structrp png_ptr, int compression, png_const_charp key, png_const_charp lang, - png_const_charp lang_key, png_const_charp text)); + png_const_charp lang_key, png_const_charp text),PNG_EMPTY); #endif #ifdef PNG_TEXT_SUPPORTED /* Added at version 1.0.14 and 1.2.4 */ -PNG_EXTERN int png_set_text_2 PNGARG((png_structp png_ptr, - png_infop info_ptr, png_const_textp text_ptr, int num_text)); +PNG_INTERNAL_FUNCTION(int,png_set_text_2,(png_const_structrp png_ptr, + png_inforp info_ptr, png_const_textp text_ptr, int num_text),PNG_EMPTY); #endif #ifdef PNG_WRITE_oFFs_SUPPORTED -PNG_EXTERN void png_write_oFFs PNGARG((png_structp png_ptr, - png_int_32 x_offset, png_int_32 y_offset, int unit_type)); +PNG_INTERNAL_FUNCTION(void,png_write_oFFs,(png_structrp png_ptr, + png_int_32 x_offset, png_int_32 y_offset, int unit_type),PNG_EMPTY); #endif #ifdef PNG_WRITE_pCAL_SUPPORTED -PNG_EXTERN void png_write_pCAL PNGARG((png_structp png_ptr, png_charp purpose, - png_int_32 X0, png_int_32 X1, int type, int nparams, - png_const_charp units, png_charpp params)); +PNG_INTERNAL_FUNCTION(void,png_write_pCAL,(png_structrp png_ptr, + png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, + png_const_charp units, png_charpp params),PNG_EMPTY); #endif #ifdef PNG_WRITE_pHYs_SUPPORTED -PNG_EXTERN void png_write_pHYs PNGARG((png_structp png_ptr, +PNG_INTERNAL_FUNCTION(void,png_write_pHYs,(png_structrp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, - int unit_type)); + int unit_type),PNG_EMPTY); #endif #ifdef PNG_WRITE_tIME_SUPPORTED -PNG_EXTERN void png_write_tIME PNGARG((png_structp png_ptr, - png_const_timep mod_time)); +PNG_INTERNAL_FUNCTION(void,png_write_tIME,(png_structrp png_ptr, + png_const_timep mod_time),PNG_EMPTY); #endif #ifdef PNG_WRITE_sCAL_SUPPORTED -PNG_EXTERN void png_write_sCAL_s PNGARG((png_structp png_ptr, - int unit, png_const_charp width, png_const_charp height)); +PNG_INTERNAL_FUNCTION(void,png_write_sCAL_s,(png_structrp png_ptr, + int unit, png_const_charp width, png_const_charp height),PNG_EMPTY); #endif /* Called when finished processing a row of data */ -PNG_EXTERN void png_write_finish_row PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_write_finish_row,(png_structrp png_ptr), + PNG_EMPTY); /* Internal use only. Called before first row of data */ -PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_write_start_row,(png_structrp png_ptr), + PNG_EMPTY); /* Combine a row of data, dealing with alpha, etc. if requested. 'row' is an * array of png_ptr->width pixels. If the image is not interlaced or this - * is the final pass this just does a png_memcpy, otherwise the "display" flag + * is the final pass this just does a memcpy, otherwise the "display" flag * is used to determine whether to copy pixels that are not in the current pass. * * Because 'png_do_read_interlace' (below) replicates pixels this allows this @@ -968,8 +1146,8 @@ PNG_EXTERN void png_write_start_row PNGARG((png_structp png_ptr)); #ifndef PNG_USE_COMPILE_TIME_MASKS # define PNG_USE_COMPILE_TIME_MASKS 1 #endif -PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row, - int display)); +PNG_INTERNAL_FUNCTION(void,png_combine_row,(png_const_structrp png_ptr, + png_bytep row, int display),PNG_EMPTY); #ifdef PNG_READ_INTERLACING_SUPPORTED /* Expand an interlaced row: the 'row_info' describes the pass data that has @@ -978,188 +1156,99 @@ PNG_EXTERN void png_combine_row PNGARG((png_structp png_ptr, png_bytep row, * the pixels are *replicated* to the intervening space. This is essential for * the correct operation of png_combine_row, above. */ -PNG_EXTERN void png_do_read_interlace PNGARG((png_row_infop row_info, - png_bytep row, int pass, png_uint_32 transformations)); +PNG_INTERNAL_FUNCTION(void,png_do_read_interlace,(png_row_infop row_info, + png_bytep row, int pass, png_uint_32 transformations),PNG_EMPTY); #endif /* GRR TO DO (2.0 or whenever): simplify other internal calling interfaces */ #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Grab pixels out of a row for an interlaced pass */ -PNG_EXTERN void png_do_write_interlace PNGARG((png_row_infop row_info, - png_bytep row, int pass)); +PNG_INTERNAL_FUNCTION(void,png_do_write_interlace,(png_row_infop row_info, + png_bytep row, int pass),PNG_EMPTY); #endif /* Unfilter a row: check the filter value before calling this, there is no point * calling it for PNG_FILTER_VALUE_NONE. */ -PNG_EXTERN void png_read_filter_row PNGARG((png_structp pp, png_row_infop row_info, - png_bytep row, png_const_bytep prev_row, int filter)); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row,(png_structrp pp, png_row_infop + row_info, png_bytep row, png_const_bytep prev_row, int filter),PNG_EMPTY); -PNG_EXTERN void png_read_filter_row_up_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_sub3_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_sub4_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_avg3_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_avg4_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_paeth3_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); -PNG_EXTERN void png_read_filter_row_paeth4_neon PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep prev_row)); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_neon,(png_row_infop row_info, + png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_neon,(png_row_infop + row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); /* Choose the best filter to use and filter the row data */ -PNG_EXTERN void png_write_find_filter PNGARG((png_structp png_ptr, - png_row_infop row_info)); +PNG_INTERNAL_FUNCTION(void,png_write_find_filter,(png_structrp png_ptr, + png_row_infop row_info),PNG_EMPTY); -/* Finish a row while reading, dealing with interlacing passes, etc. */ -PNG_EXTERN void png_read_finish_row PNGARG((png_structp png_ptr)); +#ifdef PNG_SEQUENTIAL_READ_SUPPORTED +PNG_INTERNAL_FUNCTION(void,png_read_IDAT_data,(png_structrp png_ptr, + png_bytep output, png_alloc_size_t avail_out),PNG_EMPTY); + /* Read 'avail_out' bytes of data from the IDAT stream. If the output buffer + * is NULL the function checks, instead, for the end of the stream. In this + * case a benign error will be issued if the stream end is not found or if + * extra data has to be consumed. + */ +PNG_INTERNAL_FUNCTION(void,png_read_finish_IDAT,(png_structrp png_ptr), + PNG_EMPTY); + /* This cleans up when the IDAT LZ stream does not end when the last image + * byte is read; there is still some pending input. + */ + +PNG_INTERNAL_FUNCTION(void,png_read_finish_row,(png_structrp png_ptr), + PNG_EMPTY); + /* Finish a row while reading, dealing with interlacing passes, etc. */ +#endif /* SEQUENTIAL_READ */ /* Initialize the row buffers, etc. */ -PNG_EXTERN void png_read_start_row PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_read_start_row,(png_structrp png_ptr),PNG_EMPTY); #ifdef PNG_READ_TRANSFORMS_SUPPORTED /* Optional call to update the users info structure */ -PNG_EXTERN void png_read_transform_info PNGARG((png_structp png_ptr, - png_infop info_ptr)); -#endif - -/* These are the functions that do the transformations */ -#ifdef PNG_READ_FILLER_SUPPORTED -PNG_EXTERN void png_do_read_filler PNGARG((png_row_infop row_info, - png_bytep row, png_uint_32 filler, png_uint_32 flags)); -#endif - -#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED -PNG_EXTERN void png_do_read_swap_alpha PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED -PNG_EXTERN void png_do_write_swap_alpha PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED -PNG_EXTERN void png_do_read_invert_alpha PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED -PNG_EXTERN void png_do_write_invert_alpha PNGARG((png_row_infop row_info, - png_bytep row)); +PNG_INTERNAL_FUNCTION(void,png_read_transform_info,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); #endif +/* Shared transform functions, defined in pngtran.c */ #if defined(PNG_WRITE_FILLER_SUPPORTED) || \ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) -PNG_EXTERN void png_do_strip_channel PNGARG((png_row_infop row_info, - png_bytep row, int at_start)); +PNG_INTERNAL_FUNCTION(void,png_do_strip_channel,(png_row_infop row_info, + png_bytep row, int at_start),PNG_EMPTY); #endif #ifdef PNG_16BIT_SUPPORTED #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) -PNG_EXTERN void png_do_swap PNGARG((png_row_infop row_info, - png_bytep row)); +PNG_INTERNAL_FUNCTION(void,png_do_swap,(png_row_infop row_info, + png_bytep row),PNG_EMPTY); #endif #endif #if defined(PNG_READ_PACKSWAP_SUPPORTED) || \ defined(PNG_WRITE_PACKSWAP_SUPPORTED) -PNG_EXTERN void png_do_packswap PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED -PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, - png_row_infop row_info, png_bytep row)); -#endif - -#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED -PNG_EXTERN void png_do_gray_to_rgb PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_PACK_SUPPORTED -PNG_EXTERN void png_do_unpack PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_SHIFT_SUPPORTED -PNG_EXTERN void png_do_unshift PNGARG((png_row_infop row_info, - png_bytep row, png_const_color_8p sig_bits)); +PNG_INTERNAL_FUNCTION(void,png_do_packswap,(png_row_infop row_info, + png_bytep row),PNG_EMPTY); #endif #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) -PNG_EXTERN void png_do_invert PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED -PNG_EXTERN void png_do_scale_16_to_8 PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED -PNG_EXTERN void png_do_chop PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_READ_QUANTIZE_SUPPORTED -PNG_EXTERN void png_do_quantize PNGARG((png_row_infop row_info, - png_bytep row, png_const_bytep palette_lookup, - png_const_bytep quantize_lookup)); - -# ifdef PNG_CORRECT_PALETTE_SUPPORTED -PNG_EXTERN void png_correct_palette PNGARG((png_structp png_ptr, - png_colorp palette, int num_palette)); -# endif +PNG_INTERNAL_FUNCTION(void,png_do_invert,(png_row_infop row_info, + png_bytep row),PNG_EMPTY); #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) -PNG_EXTERN void png_do_bgr PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - -#ifdef PNG_WRITE_PACK_SUPPORTED -PNG_EXTERN void png_do_pack PNGARG((png_row_infop row_info, - png_bytep row, png_uint_32 bit_depth)); -#endif - -#ifdef PNG_WRITE_SHIFT_SUPPORTED -PNG_EXTERN void png_do_shift PNGARG((png_row_infop row_info, - png_bytep row, png_const_color_8p bit_depth)); -#endif - -#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ - defined(PNG_READ_ALPHA_MODE_SUPPORTED) -PNG_EXTERN void png_do_compose PNGARG((png_row_infop row_info, - png_bytep row, png_structp png_ptr)); -#endif - -#ifdef PNG_READ_GAMMA_SUPPORTED -PNG_EXTERN void png_do_gamma PNGARG((png_row_infop row_info, - png_bytep row, png_structp png_ptr)); -#endif - -#ifdef PNG_READ_ALPHA_MODE_SUPPORTED -PNG_EXTERN void png_do_encode_alpha PNGARG((png_row_infop row_info, - png_bytep row, png_structp png_ptr)); -#endif - -#ifdef PNG_READ_EXPAND_SUPPORTED -PNG_EXTERN void png_do_expand_palette PNGARG((png_row_infop row_info, - png_bytep row, png_const_colorp palette, png_const_bytep trans, - int num_trans)); -PNG_EXTERN void png_do_expand PNGARG((png_row_infop row_info, - png_bytep row, png_const_color_16p trans_color)); -#endif - -#ifdef PNG_READ_EXPAND_16_SUPPORTED -PNG_EXTERN void png_do_expand_16 PNGARG((png_row_infop row_info, - png_bytep row)); +PNG_INTERNAL_FUNCTION(void,png_do_bgr,(png_row_infop row_info, + png_bytep row),PNG_EMPTY); #endif /* The following decodes the appropriate chunks, and does error correction, @@ -1167,254 +1256,279 @@ PNG_EXTERN void png_do_expand_16 PNGARG((png_row_infop row_info, */ /* Decode the IHDR chunk */ -PNG_EXTERN void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); -PNG_EXTERN void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); -PNG_EXTERN void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_IHDR,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_handle_PLTE,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_handle_IEND,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #ifdef PNG_READ_bKGD_SUPPORTED -PNG_EXTERN void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_bKGD,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_cHRM_SUPPORTED -PNG_EXTERN void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_cHRM,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_gAMA_SUPPORTED -PNG_EXTERN void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_gAMA,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_hIST_SUPPORTED -PNG_EXTERN void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_hIST,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_iCCP_SUPPORTED -PNG_EXTERN void png_handle_iCCP PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); -#endif /* PNG_READ_iCCP_SUPPORTED */ +PNG_INTERNAL_FUNCTION(void,png_handle_iCCP,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +#endif /* READ_iCCP */ #ifdef PNG_READ_iTXt_SUPPORTED -PNG_EXTERN void png_handle_iTXt PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_iTXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_oFFs_SUPPORTED -PNG_EXTERN void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_oFFs,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_pCAL_SUPPORTED -PNG_EXTERN void png_handle_pCAL PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_pCAL,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_pHYs_SUPPORTED -PNG_EXTERN void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_pHYs,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_sBIT_SUPPORTED -PNG_EXTERN void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_sBIT,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_sCAL_SUPPORTED -PNG_EXTERN void png_handle_sCAL PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_sCAL,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_sPLT_SUPPORTED -PNG_EXTERN void png_handle_sPLT PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); -#endif /* PNG_READ_sPLT_SUPPORTED */ +PNG_INTERNAL_FUNCTION(void,png_handle_sPLT,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +#endif /* READ_sPLT */ #ifdef PNG_READ_sRGB_SUPPORTED -PNG_EXTERN void png_handle_sRGB PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_sRGB,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_tEXt_SUPPORTED -PNG_EXTERN void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_tEXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_tIME_SUPPORTED -PNG_EXTERN void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_tIME,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_tRNS_SUPPORTED -PNG_EXTERN void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_tRNS,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif #ifdef PNG_READ_zTXt_SUPPORTED -PNG_EXTERN void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, - png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_handle_zTXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); #endif -PNG_EXTERN void png_handle_unknown PNGARG((png_structp png_ptr, - png_infop info_ptr, png_uint_32 length)); +PNG_INTERNAL_FUNCTION(void,png_check_chunk_name,(png_structrp png_ptr, + png_uint_32 chunk_name),PNG_EMPTY); -PNG_EXTERN void png_check_chunk_name PNGARG((png_structp png_ptr, - png_uint_32 chunk_name)); +PNG_INTERNAL_FUNCTION(void,png_handle_unknown,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length, int keep),PNG_EMPTY); + /* This is the function that gets called for unknown chunks. The 'keep' + * argument is either non-zero for a known chunk that has been set to be + * handled as unknown or zero for an unknown chunk. By default the function + * just skips the chunk or errors out if it is critical. + */ -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED -/* Exactly as png_handle_as_unknown() except that the argument is a 32-bit chunk - * name, not a string. - */ -PNG_EXTERN int png_chunk_unknown_handling PNGARG((png_structp png_ptr, - png_uint_32 chunk_name)); -#endif +#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\ + defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) +PNG_INTERNAL_FUNCTION(int,png_chunk_unknown_handling, + (png_const_structrp png_ptr, png_uint_32 chunk_name),PNG_EMPTY); + /* Exactly as the API png_handle_as_unknown() except that the argument is a + * 32-bit chunk name, not a string. + */ +#endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */ /* Handle the transformations for reading and writing */ #ifdef PNG_READ_TRANSFORMS_SUPPORTED -PNG_EXTERN void png_do_read_transformations PNGARG((png_structp png_ptr, - png_row_infop row_info)); +PNG_INTERNAL_FUNCTION(void,png_do_read_transformations,(png_structrp png_ptr, + png_row_infop row_info),PNG_EMPTY); #endif #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED -PNG_EXTERN void png_do_write_transformations PNGARG((png_structp png_ptr, - png_row_infop row_info)); +PNG_INTERNAL_FUNCTION(void,png_do_write_transformations,(png_structrp png_ptr, + png_row_infop row_info),PNG_EMPTY); #endif #ifdef PNG_READ_TRANSFORMS_SUPPORTED -PNG_EXTERN void png_init_read_transformations PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_init_read_transformations,(png_structrp png_ptr), + PNG_EMPTY); #endif #ifdef PNG_PROGRESSIVE_READ_SUPPORTED -PNG_EXTERN void png_push_read_chunk PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_push_read_sig PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_push_check_crc PNGARG((png_structp png_ptr)); -PNG_EXTERN void png_push_crc_skip PNGARG((png_structp png_ptr, - png_uint_32 length)); -PNG_EXTERN void png_push_crc_finish PNGARG((png_structp png_ptr)); -PNG_EXTERN void png_push_save_buffer PNGARG((png_structp png_ptr)); -PNG_EXTERN void png_push_restore_buffer PNGARG((png_structp png_ptr, - png_bytep buffer, png_size_t buffer_length)); -PNG_EXTERN void png_push_read_IDAT PNGARG((png_structp png_ptr)); -PNG_EXTERN void png_process_IDAT_data PNGARG((png_structp png_ptr, - png_bytep buffer, png_size_t buffer_length)); -PNG_EXTERN void png_push_process_row PNGARG((png_structp png_ptr)); -PNG_EXTERN void png_push_handle_unknown PNGARG((png_structp png_ptr, - png_infop info_ptr, png_uint_32 length)); -PNG_EXTERN void png_push_have_info PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_push_have_end PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row)); -PNG_EXTERN void png_push_read_end PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_process_some_data PNGARG((png_structp png_ptr, - png_infop info_ptr)); -PNG_EXTERN void png_read_push_finish_row PNGARG((png_structp png_ptr)); +PNG_INTERNAL_FUNCTION(void,png_push_read_chunk,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_sig,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_check_crc,(png_structrp png_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_save_buffer,(png_structrp png_ptr), + PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_restore_buffer,(png_structrp png_ptr, + png_bytep buffer, png_size_t buffer_length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_IDAT,(png_structrp png_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_process_IDAT_data,(png_structrp png_ptr, + png_bytep buffer, png_size_t buffer_length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_process_row,(png_structrp png_ptr), + PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_handle_unknown,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_have_info,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_have_end,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_have_row,(png_structrp png_ptr, + png_bytep row),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_end,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_process_some_data,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_read_push_finish_row,(png_structrp png_ptr), + PNG_EMPTY); # ifdef PNG_READ_tEXt_SUPPORTED -PNG_EXTERN void png_push_handle_tEXt PNGARG((png_structp png_ptr, - png_infop info_ptr, png_uint_32 length)); -PNG_EXTERN void png_push_read_tEXt PNGARG((png_structp png_ptr, - png_infop info_ptr)); +PNG_INTERNAL_FUNCTION(void,png_push_handle_tEXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_tEXt,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); # endif # ifdef PNG_READ_zTXt_SUPPORTED -PNG_EXTERN void png_push_handle_zTXt PNGARG((png_structp png_ptr, - png_infop info_ptr, png_uint_32 length)); -PNG_EXTERN void png_push_read_zTXt PNGARG((png_structp png_ptr, - png_infop info_ptr)); +PNG_INTERNAL_FUNCTION(void,png_push_handle_zTXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_zTXt,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); # endif # ifdef PNG_READ_iTXt_SUPPORTED -PNG_EXTERN void png_push_handle_iTXt PNGARG((png_structp png_ptr, - png_infop info_ptr, png_uint_32 length)); -PNG_EXTERN void png_push_read_iTXt PNGARG((png_structp png_ptr, - png_infop info_ptr)); +PNG_INTERNAL_FUNCTION(void,png_push_handle_iTXt,(png_structrp png_ptr, + png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_push_read_iTXt,(png_structrp png_ptr, + png_inforp info_ptr),PNG_EMPTY); # endif -#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ +#endif /* PROGRESSIVE_READ */ -#ifdef PNG_MNG_FEATURES_SUPPORTED -PNG_EXTERN void png_do_read_intrapixel PNGARG((png_row_infop row_info, - png_bytep row)); -PNG_EXTERN void png_do_write_intrapixel PNGARG((png_row_infop row_info, - png_bytep row)); +/* Added at libpng version 1.6.0 */ +#ifdef PNG_GAMMA_SUPPORTED +PNG_INTERNAL_FUNCTION(void,png_colorspace_set_gamma,(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_fixed_point gAMA), PNG_EMPTY); + /* Set the colorspace gamma with a value provided by the application or by + * the gAMA chunk on read. The value will override anything set by an ICC + * profile. + */ + +PNG_INTERNAL_FUNCTION(void,png_colorspace_sync_info,(png_const_structrp png_ptr, + png_inforp info_ptr), PNG_EMPTY); + /* Synchronize the info 'valid' flags with the colorspace */ + +PNG_INTERNAL_FUNCTION(void,png_colorspace_sync,(png_const_structrp png_ptr, + png_inforp info_ptr), PNG_EMPTY); + /* Copy the png_struct colorspace to the info_struct and call the above to + * synchronize the flags. Checks for NULL info_ptr and does nothing. + */ #endif /* Added at libpng version 1.4.0 */ -#ifdef PNG_CHECK_cHRM_SUPPORTED -PNG_EXTERN int png_check_cHRM_fixed PNGARG((png_structp png_ptr, - png_fixed_point int_white_x, png_fixed_point int_white_y, - png_fixed_point int_red_x, png_fixed_point int_red_y, png_fixed_point - int_green_x, png_fixed_point int_green_y, png_fixed_point int_blue_x, - png_fixed_point int_blue_y)); -#endif - -#ifdef PNG_CHECK_cHRM_SUPPORTED -/* Added at libpng version 1.2.34 and 1.4.0 */ -/* Currently only used by png_check_cHRM_fixed */ -PNG_EXTERN void png_64bit_product PNGARG((long v1, long v2, - unsigned long *hi_product, unsigned long *lo_product)); -#endif - -#ifdef PNG_cHRM_SUPPORTED -/* Added at libpng version 1.5.5 */ -typedef struct png_xy -{ - png_fixed_point redx, redy; - png_fixed_point greenx, greeny; - png_fixed_point bluex, bluey; - png_fixed_point whitex, whitey; -} png_xy; - -typedef struct png_XYZ -{ - png_fixed_point redX, redY, redZ; - png_fixed_point greenX, greenY, greenZ; - png_fixed_point blueX, blueY, blueZ; -} png_XYZ; - -/* The conversion APIs return 0 on success, non-zero on a parameter error. They - * allow conversion between the above representations of a color encoding. When - * converting from XYZ end points to chromaticities the absolute magnitude of - * the end points is lost, when converting back the sum of the Y values of the - * three end points will be 1.0 +#ifdef PNG_COLORSPACE_SUPPORTED +/* These internal functions are for maintaining the colorspace structure within + * a png_info or png_struct (or, indeed, both). */ -PNG_EXTERN int png_xy_from_XYZ PNGARG((png_xy *xy, png_XYZ XYZ)); -PNG_EXTERN int png_XYZ_from_xy PNGARG((png_XYZ *XYZ, png_xy xy)); -PNG_EXTERN int png_XYZ_from_xy_checked PNGARG((png_structp png_ptr, - png_XYZ *XYZ, png_xy xy)); +PNG_INTERNAL_FUNCTION(int,png_colorspace_set_chromaticities, + (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, + int preferred), PNG_EMPTY); + +PNG_INTERNAL_FUNCTION(int,png_colorspace_set_endpoints, + (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_XYZ *XYZ, + int preferred), PNG_EMPTY); + +#ifdef PNG_sRGB_SUPPORTED +PNG_INTERNAL_FUNCTION(int,png_colorspace_set_sRGB,(png_const_structrp png_ptr, + png_colorspacerp colorspace, int intent), PNG_EMPTY); + /* This does set the colorspace gAMA and cHRM values too, but doesn't set the + * flags to write them, if it returns false there was a problem and an error + * message has already been output (but the colorspace may still need to be + * synced to record the invalid flag). + */ +#endif /* sRGB */ + +#ifdef PNG_iCCP_SUPPORTED +PNG_INTERNAL_FUNCTION(int,png_colorspace_set_ICC,(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_const_charp name, + png_uint_32 profile_length, png_const_bytep profile, int color_type), + PNG_EMPTY); + /* The 'name' is used for information only */ + +/* Routines for checking parts of an ICC profile. */ +PNG_INTERNAL_FUNCTION(int,png_icc_check_length,(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_const_charp name, + png_uint_32 profile_length), PNG_EMPTY); +PNG_INTERNAL_FUNCTION(int,png_icc_check_header,(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_const_charp name, + png_uint_32 profile_length, + png_const_bytep profile /* first 132 bytes only */, int color_type), + PNG_EMPTY); +PNG_INTERNAL_FUNCTION(int,png_icc_check_tag_table,(png_const_structrp png_ptr, + png_colorspacerp colorspace, png_const_charp name, + png_uint_32 profile_length, + png_const_bytep profile /* header plus whole tag table */), PNG_EMPTY); +#ifdef PNG_sRGB_SUPPORTED +PNG_INTERNAL_FUNCTION(void,png_icc_set_sRGB,( + png_const_structrp png_ptr, png_colorspacerp colorspace, + png_const_bytep profile, uLong adler), PNG_EMPTY); + /* 'adler' is the Adler32 checksum of the uncompressed profile data. It may + * be zero to indicate that it is not available. It is used, if provided, + * as a fast check on the profile when checking to see if it is sRGB. + */ #endif +#endif /* iCCP */ + +#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED +PNG_INTERNAL_FUNCTION(void,png_colorspace_set_rgb_coefficients, + (png_structrp png_ptr), PNG_EMPTY); + /* Set the rgb_to_gray coefficients from the colorspace Y values */ +#endif /* READ_RGB_TO_GRAY */ +#endif /* COLORSPACE */ /* Added at libpng version 1.4.0 */ -PNG_EXTERN void png_check_IHDR PNGARG((png_structp png_ptr, +PNG_INTERNAL_FUNCTION(void,png_check_IHDR,(png_const_structrp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, - int filter_type)); + int filter_type),PNG_EMPTY); /* Added at libpng version 1.5.10 */ #if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \ defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED) -PNG_EXTERN void png_do_check_palette_indexes PNGARG((png_structp png_ptr, - png_row_infop row_info)); +PNG_INTERNAL_FUNCTION(void,png_do_check_palette_indexes, + (png_structrp png_ptr, png_row_infop row_info),PNG_EMPTY); #endif -/* Free all memory used by the read (old method - NOT DLL EXPORTED) */ -PNG_EXTERN void png_read_destroy PNGARG((png_structp png_ptr, - png_infop info_ptr, png_infop end_info_ptr)); - -/* Free any memory used in png_ptr struct (old method - NOT DLL EXPORTED) */ -PNG_EXTERN void png_write_destroy PNGARG((png_structp png_ptr)); - -#ifdef USE_FAR_KEYWORD /* memory model conversion function */ -PNG_EXTERN void *png_far_to_near PNGARG((png_structp png_ptr, png_voidp ptr, - int check)); -#endif /* USE_FAR_KEYWORD */ - #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) -PNG_EXTERN PNG_FUNCTION(void, png_fixed_error, (png_structp png_ptr, +PNG_INTERNAL_FUNCTION(void,png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN); #endif @@ -1422,8 +1536,8 @@ PNG_EXTERN PNG_FUNCTION(void, png_fixed_error, (png_structp png_ptr, * the end. Always leaves the buffer nul terminated. Never errors out (and * there is no error code.) */ -PNG_EXTERN size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, - png_const_charp string); +PNG_INTERNAL_FUNCTION(size_t,png_safecat,(png_charp buffer, size_t bufsize, + size_t pos, png_const_charp string),PNG_EMPTY); /* Various internal functions to handle formatted warning messages, currently * only implemented for warnings. @@ -1434,8 +1548,8 @@ PNG_EXTERN size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, * Returns the pointer to the start of the formatted string. This utility only * does unsigned values. */ -PNG_EXTERN png_charp png_format_number(png_const_charp start, png_charp end, - int format, png_alloc_size_t number); +PNG_INTERNAL_FUNCTION(png_charp,png_format_number,(png_const_charp start, + png_charp end, int format, png_alloc_size_t number),PNG_EMPTY); /* Convenience macro that takes an array: */ #define PNG_FORMAT_NUMBER(buffer,format,number) \ @@ -1459,7 +1573,7 @@ PNG_EXTERN png_charp png_format_number(png_const_charp start, png_charp end, #ifdef PNG_WARNINGS_SUPPORTED /* New defines and members adding in libpng-1.5.4 */ # define PNG_WARNING_PARAMETER_SIZE 32 -# define PNG_WARNING_PARAMETER_COUNT 8 +# define PNG_WARNING_PARAMETER_COUNT 8 /* Maximum 9; see pngerror.c */ /* An l-value of this type has to be passed to the APIs below to cache the * values of the parameters to a formatted warning message. @@ -1467,48 +1581,97 @@ PNG_EXTERN png_charp png_format_number(png_const_charp start, png_charp end, typedef char png_warning_parameters[PNG_WARNING_PARAMETER_COUNT][ PNG_WARNING_PARAMETER_SIZE]; -PNG_EXTERN void png_warning_parameter(png_warning_parameters p, int number, - png_const_charp string); - /* Parameters are limited in size to PNG_WARNING_PARAMETER_SIZE characters, - * including the trailing '\0'. - */ -PNG_EXTERN void png_warning_parameter_unsigned(png_warning_parameters p, - int number, int format, png_alloc_size_t value); - /* Use png_alloc_size_t because it is an unsigned type as big as any we - * need to output. Use the following for a signed value. - */ -PNG_EXTERN void png_warning_parameter_signed(png_warning_parameters p, - int number, int format, png_int_32 value); +PNG_INTERNAL_FUNCTION(void,png_warning_parameter,(png_warning_parameters p, + int number, png_const_charp string),PNG_EMPTY); + /* Parameters are limited in size to PNG_WARNING_PARAMETER_SIZE characters, + * including the trailing '\0'. + */ +PNG_INTERNAL_FUNCTION(void,png_warning_parameter_unsigned, + (png_warning_parameters p, int number, int format, png_alloc_size_t value), + PNG_EMPTY); + /* Use png_alloc_size_t because it is an unsigned type as big as any we + * need to output. Use the following for a signed value. + */ +PNG_INTERNAL_FUNCTION(void,png_warning_parameter_signed, + (png_warning_parameters p, int number, int format, png_int_32 value), + PNG_EMPTY); -PNG_EXTERN void png_formatted_warning(png_structp png_ptr, - png_warning_parameters p, png_const_charp message); - /* 'message' follows the X/Open approach of using @1, @2 to insert - * parameters previously supplied using the above functions. Errors in - * specifying the paramters will simple result in garbage substitutions. - */ +PNG_INTERNAL_FUNCTION(void,png_formatted_warning,(png_const_structrp png_ptr, + png_warning_parameters p, png_const_charp message),PNG_EMPTY); + /* 'message' follows the X/Open approach of using @1, @2 to insert + * parameters previously supplied using the above functions. Errors in + * specifying the parameters will simply result in garbage substitutions. + */ #endif +#ifdef PNG_BENIGN_ERRORS_SUPPORTED +/* Application errors (new in 1.6); use these functions (declared below) for + * errors in the parameters or order of API function calls on read. The + * 'warning' should be used for an error that can be handled completely; the + * 'error' for one which can be handled safely but which may lose application + * information or settings. + * + * By default these both result in a png_error call prior to release, while in a + * released version the 'warning' is just a warning. However if the application + * explicitly disables benign errors (explicitly permitting the code to lose + * information) they both turn into warnings. + * + * If benign errors aren't supported they end up as the corresponding base call + * (png_warning or png_error.) + */ +PNG_INTERNAL_FUNCTION(void,png_app_warning,(png_const_structrp png_ptr, + png_const_charp message),PNG_EMPTY); + /* The application provided invalid parameters to an API function or called + * an API function at the wrong time, libpng can completely recover. + */ + +PNG_INTERNAL_FUNCTION(void,png_app_error,(png_const_structrp png_ptr, + png_const_charp message),PNG_EMPTY); + /* As above but libpng will ignore the call, or attempt some other partial + * recovery from the error. + */ +#else +# define png_app_warning(pp,s) png_warning(pp,s) +# define png_app_error(pp,s) png_error(pp,s) +#endif + +PNG_INTERNAL_FUNCTION(void,png_chunk_report,(png_const_structrp png_ptr, + png_const_charp message, int error),PNG_EMPTY); + /* Report a recoverable issue in chunk data. On read this is used to report + * a problem found while reading a particular chunk and the + * png_chunk_benign_error or png_chunk_warning function is used as + * appropriate. On write this is used to report an error that comes from + * data set via an application call to a png_set_ API and png_app_error or + * png_app_warning is used as appropriate. + * + * The 'error' parameter must have one of the following values: + */ +#define PNG_CHUNK_WARNING 0 /* never an error */ +#define PNG_CHUNK_WRITE_ERROR 1 /* an error only on write */ +#define PNG_CHUNK_ERROR 2 /* always an error */ + /* ASCII to FP interfaces, currently only implemented if sCAL * support is required. */ -#if defined(PNG_READ_sCAL_SUPPORTED) +#if defined(PNG_sCAL_SUPPORTED) /* MAX_DIGITS is actually the maximum number of characters in an sCAL * width or height, derived from the precision (number of significant - * digits - a build time settable option) and assumpitions about the + * digits - a build time settable option) and assumptions about the * maximum ridiculous exponent. */ #define PNG_sCAL_MAX_DIGITS (PNG_sCAL_PRECISION+1/*.*/+1/*E*/+10/*exponent*/) #ifdef PNG_FLOATING_POINT_SUPPORTED -PNG_EXTERN void png_ascii_from_fp PNGARG((png_structp png_ptr, png_charp ascii, - png_size_t size, double fp, unsigned int precision)); +PNG_INTERNAL_FUNCTION(void,png_ascii_from_fp,(png_const_structrp png_ptr, + png_charp ascii, png_size_t size, double fp, unsigned int precision), + PNG_EMPTY); #endif /* FLOATING_POINT */ #ifdef PNG_FIXED_POINT_SUPPORTED -PNG_EXTERN void png_ascii_from_fixed PNGARG((png_structp png_ptr, - png_charp ascii, png_size_t size, png_fixed_point fp)); +PNG_INTERNAL_FUNCTION(void,png_ascii_from_fixed,(png_const_structrp png_ptr, + png_charp ascii, png_size_t size, png_fixed_point fp),PNG_EMPTY); #endif /* FIXED_POINT */ -#endif /* READ_sCAL */ +#endif /* sCAL */ #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) /* An internal API to validate the format of a floating point number. @@ -1532,7 +1695,7 @@ PNG_EXTERN void png_ascii_from_fixed PNGARG((png_structp png_ptr, * NOTE: The dangling E problem. * There is a PNG valid floating point number in the following: * - * PNG floating point numb1.ers are not greedy. + * PNG floating point numbers are not greedy. * * Working this out requires *TWO* character lookahead (because of the * sign), the parser does not do this - it will fail at the 'r' - this @@ -1583,14 +1746,14 @@ PNG_EXTERN void png_ascii_from_fixed PNGARG((png_structp png_ptr, #define PNG_FP_IS_POSITIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_Z_MASK) #define PNG_FP_IS_NEGATIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_NZ_MASK) -/* The actual parser. This can be called repeatedly, it updates +/* The actual parser. This can be called repeatedly. It updates * the index into the string and the state variable (which must - * be initialzed to 0). It returns a result code, as above. There + * be initialized to 0). It returns a result code, as above. There * is no point calling the parser any more if it fails to advance to * the end of the string - it is stuck on an invalid character (or * terminated by '\0'). * - * Note that the pointer will consume an E or even an E+ then leave + * Note that the pointer will consume an E or even an E+ and then leave * a 'maybe' state even though a preceding integer.fraction is valid. * The PNG_FP_WAS_VALID flag indicates that a preceding substring was * a valid number. It's possible to recover from this by calling @@ -1598,8 +1761,8 @@ PNG_EXTERN void png_ascii_from_fixed PNGARG((png_structp png_ptr, * that omits the last character (i.e. set the size to the index of * the problem character.) This has not been tested within libpng. */ -PNG_EXTERN int png_check_fp_number PNGARG((png_const_charp string, - png_size_t size, int *statep, png_size_tp whereami)); +PNG_INTERNAL_FUNCTION(int,png_check_fp_number,(png_const_charp string, + png_size_t size, int *statep, png_size_tp whereami),PNG_EMPTY); /* This is the same but it checks a complete string and returns true * only if it just contains a floating point number. As of 1.5.4 this @@ -1607,11 +1770,11 @@ PNG_EXTERN int png_check_fp_number PNGARG((png_const_charp string, * it was valid (otherwise it returns 0.) This can be used for testing * for negative or zero values using the sticky flag. */ -PNG_EXTERN int png_check_fp_string PNGARG((png_const_charp string, - png_size_t size)); +PNG_INTERNAL_FUNCTION(int,png_check_fp_string,(png_const_charp string, + png_size_t size),PNG_EMPTY); #endif /* pCAL || sCAL */ -#if defined(PNG_READ_GAMMA_SUPPORTED) ||\ +#if defined(PNG_GAMMA_SUPPORTED) ||\ defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) /* Added at libpng version 1.5.0 */ /* This is a utility to provide a*times/div (rounded) and indicate @@ -1619,29 +1782,37 @@ PNG_EXTERN int png_check_fp_string PNGARG((png_const_charp string, * for overflow, true (1) if no overflow, in which case *res * holds the result. */ -PNG_EXTERN int png_muldiv PNGARG((png_fixed_point_p res, png_fixed_point a, - png_int_32 multiplied_by, png_int_32 divided_by)); +PNG_INTERNAL_FUNCTION(int,png_muldiv,(png_fixed_point_p res, png_fixed_point a, + png_int_32 multiplied_by, png_int_32 divided_by),PNG_EMPTY); #endif #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) /* Same deal, but issue a warning on overflow and return 0. */ -PNG_EXTERN png_fixed_point png_muldiv_warn PNGARG((png_structp png_ptr, - png_fixed_point a, png_int_32 multiplied_by, png_int_32 divided_by)); +PNG_INTERNAL_FUNCTION(png_fixed_point,png_muldiv_warn, + (png_const_structrp png_ptr, png_fixed_point a, png_int_32 multiplied_by, + png_int_32 divided_by),PNG_EMPTY); #endif -#ifdef PNG_READ_GAMMA_SUPPORTED +#ifdef PNG_GAMMA_SUPPORTED /* Calculate a reciprocal - used for gamma values. This returns - * 0 if the argument is 0 in order to maintain an undefined value, + * 0 if the argument is 0 in order to maintain an undefined value; * there are no warnings. */ -PNG_EXTERN png_fixed_point png_reciprocal PNGARG((png_fixed_point a)); +PNG_INTERNAL_FUNCTION(png_fixed_point,png_reciprocal,(png_fixed_point a), + PNG_EMPTY); +#ifdef PNG_READ_GAMMA_SUPPORTED /* The same but gives a reciprocal of the product of two fixed point * values. Accuracy is suitable for gamma calculations but this is - * not exact - use png_muldiv for that. + * not exact - use png_muldiv for that. Only required at present on read. */ -PNG_EXTERN png_fixed_point png_reciprocal2 PNGARG((png_fixed_point a, - png_fixed_point b)); +PNG_INTERNAL_FUNCTION(png_fixed_point,png_reciprocal2,(png_fixed_point a, + png_fixed_point b),PNG_EMPTY); +#endif + +/* Return true if the gamma value is significantly different from 1.0 */ +PNG_INTERNAL_FUNCTION(int,png_gamma_significant,(png_fixed_point gamma_value), + PNG_EMPTY); #endif #ifdef PNG_READ_GAMMA_SUPPORTED @@ -1652,19 +1823,93 @@ PNG_EXTERN png_fixed_point png_reciprocal2 PNGARG((png_fixed_point a, * While the input is an 'unsigned' value it must actually be the * correct bit value - 0..255 or 0..65535 as required. */ -PNG_EXTERN png_uint_16 png_gamma_correct PNGARG((png_structp png_ptr, - unsigned int value, png_fixed_point gamma_value)); -PNG_EXTERN int png_gamma_significant PNGARG((png_fixed_point gamma_value)); -PNG_EXTERN png_uint_16 png_gamma_16bit_correct PNGARG((unsigned int value, - png_fixed_point gamma_value)); -PNG_EXTERN png_byte png_gamma_8bit_correct PNGARG((unsigned int value, - png_fixed_point gamma_value)); -PNG_EXTERN void png_destroy_gamma_table(png_structp png_ptr); -PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr, - int bit_depth)); +PNG_INTERNAL_FUNCTION(png_uint_16,png_gamma_correct,(png_structrp png_ptr, + unsigned int value, png_fixed_point gamma_value),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(png_uint_16,png_gamma_16bit_correct,(unsigned int value, + png_fixed_point gamma_value),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(png_byte,png_gamma_8bit_correct,(unsigned int value, + png_fixed_point gamma_value),PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_destroy_gamma_table,(png_structrp png_ptr), + PNG_EMPTY); +PNG_INTERNAL_FUNCTION(void,png_build_gamma_table,(png_structrp png_ptr, + int bit_depth),PNG_EMPTY); #endif -/* Maintainer: Put new private prototypes here ^ and in libpngpf.3 */ +/* SIMPLIFIED READ/WRITE SUPPORT */ +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ + defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) +/* The internal structure that png_image::opaque points to. */ +typedef struct png_control +{ + png_structp png_ptr; + png_infop info_ptr; + png_voidp error_buf; /* Always a jmp_buf at present. */ + + png_const_bytep memory; /* Memory buffer. */ + png_size_t size; /* Size of the memory buffer. */ + + unsigned int for_write :1; /* Otherwise it is a read structure */ + unsigned int owned_file :1; /* We own the file in io_ptr */ +} png_control; + +/* Return the pointer to the jmp_buf from a png_control: necessary because C + * does not reveal the type of the elements of jmp_buf. + */ +#ifdef __cplusplus +# define png_control_jmp_buf(pc) (((jmp_buf*)((pc)->error_buf))[0]) +#else +# define png_control_jmp_buf(pc) ((pc)->error_buf) +#endif + +/* Utility to safely execute a piece of libpng code catching and logging any + * errors that might occur. Returns true on success, false on failure (either + * of the function or as a result of a png_error.) + */ +PNG_INTERNAL_CALLBACK(void,png_safe_error,(png_structp png_ptr, + png_const_charp error_message),PNG_NORETURN); + +#ifdef PNG_WARNINGS_SUPPORTED +PNG_INTERNAL_CALLBACK(void,png_safe_warning,(png_structp png_ptr, + png_const_charp warning_message),PNG_EMPTY); +#else +# define png_safe_warning 0/*dummy argument*/ +#endif + +PNG_INTERNAL_FUNCTION(int,png_safe_execute,(png_imagep image, + int (*function)(png_voidp), png_voidp arg),PNG_EMPTY); + +/* Utility to log an error; this also cleans up the png_image; the function + * always returns 0 (false). + */ +PNG_INTERNAL_FUNCTION(int,png_image_error,(png_imagep image, + png_const_charp error_message),PNG_EMPTY); + +#ifndef PNG_SIMPLIFIED_READ_SUPPORTED +/* png_image_free is used by the write code but not exported */ +PNG_INTERNAL_FUNCTION(void, png_image_free, (png_imagep image), PNG_EMPTY); +#endif /* !SIMPLIFIED_READ */ + +#endif /* SIMPLIFIED READ/WRITE */ + +/* These are initialization functions for hardware specific PNG filter + * optimizations; list these here then select the appropriate one at compile + * time using the macro PNG_FILTER_OPTIMIZATIONS. If the macro is not defined + * the generic code is used. + */ +#ifdef PNG_FILTER_OPTIMIZATIONS +PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structp png_ptr, + unsigned int bpp), PNG_EMPTY); + /* Just declare the optimization that will be used */ +#else + /* List *all* the possible optimizations here - this branch is required if + * the builder of libpng passes the definition of PNG_FILTER_OPTIMIZATIONS in + * CFLAGS in place of CPPFLAGS *and* uses symbol prefixing. + */ +PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_neon, + (png_structp png_ptr, unsigned int bpp), PNG_EMPTY); +#endif + +/* Maintainer: Put new private prototypes here ^ */ #include "pngdebug.h" @@ -1672,4 +1917,5 @@ PNG_EXTERN void png_build_gamma_table PNGARG((png_structp png_ptr, } #endif +#endif /* PNG_VERSION_INFO_ONLY */ #endif /* PNGPRIV_H */ diff --git a/3rdparty/libpng/pngread.c b/3rdparty/libpng/pngread.c index 1d8c6b334..48aae8488 100644 --- a/3rdparty/libpng/pngread.c +++ b/3rdparty/libpng/pngread.c @@ -1,8 +1,8 @@ /* pngread.c - read a PNG file * - * Last changed in libpng 1.5.10 [March 8, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.17 [March 26, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -15,6 +15,9 @@ */ #include "pngpriv.h" +#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) +# include +#endif #ifdef PNG_READ_SUPPORTED @@ -23,10 +26,12 @@ PNG_FUNCTION(png_structp,PNGAPI png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) { - -#ifdef PNG_USER_MEM_SUPPORTED - return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, - warn_fn, NULL, NULL, NULL)); +#ifndef PNG_USER_MEM_SUPPORTED + png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, + error_fn, warn_fn, NULL, NULL, NULL); +#else + return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, + warn_fn, NULL, NULL, NULL); } /* Alternate create PNG structure for reading, and allocate any memory @@ -37,131 +42,40 @@ png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) { -#endif /* PNG_USER_MEM_SUPPORTED */ + png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, + error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); +#endif /* USER_MEM */ -#ifdef PNG_SETJMP_SUPPORTED - volatile -#endif - png_structp png_ptr; - volatile int png_cleanup_needed = 0; - -#ifdef PNG_SETJMP_SUPPORTED -#ifdef USE_FAR_KEYWORD - jmp_buf tmp_jmpbuf; -#endif -#endif - - png_debug(1, "in png_create_read_struct"); - -#ifdef PNG_USER_MEM_SUPPORTED - png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, - malloc_fn, mem_ptr); -#else - png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); -#endif - if (png_ptr == NULL) - return (NULL); - - /* Added at libpng-1.2.6 */ -#ifdef PNG_USER_LIMITS_SUPPORTED - png_ptr->user_width_max = PNG_USER_WIDTH_MAX; - png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; - - /* Added at libpng-1.2.43 and 1.4.0 */ - png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; - - /* Added at libpng-1.2.43 and 1.4.1 */ - png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; -#endif - -#ifdef PNG_SETJMP_SUPPORTED -/* Applications that neglect to set up their own setjmp() and then - * encounter a png_error() will longjmp here. Since the jmpbuf is - * then meaningless we abort instead of returning. - */ -#ifdef USE_FAR_KEYWORD - if (setjmp(tmp_jmpbuf)) -#else - if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */ -#endif - PNG_ABORT(); -#ifdef USE_FAR_KEYWORD - png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf)); -#endif -#endif /* PNG_SETJMP_SUPPORTED */ - -#ifdef PNG_USER_MEM_SUPPORTED - png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); -#endif - - png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); - - /* Call the general version checker (shared with read and write code): */ - if (!png_user_version_check(png_ptr, user_png_ver)) - png_cleanup_needed = 1; - - if (!png_cleanup_needed) + if (png_ptr != NULL) { - /* Initialize zbuf - compression buffer */ - png_ptr->zbuf_size = PNG_ZBUF_SIZE; - png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size); + png_ptr->mode = PNG_IS_READ_STRUCT; - if (png_ptr->zbuf == NULL) - png_cleanup_needed = 1; + /* Added in libpng-1.6.0; this can be used to detect a read structure if + * required (it will be zero in a write structure.) + */ +# ifdef PNG_SEQUENTIAL_READ_SUPPORTED + png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; +# endif + +# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED + png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; + + /* In stable builds only warn if an application error can be completely + * handled. + */ +# if PNG_RELEASE_BUILD + png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; +# endif +# endif + + /* TODO: delay this, it can be done in png_init_io (if the app doesn't + * do it itself) avoiding setting the default function if it is not + * required. + */ + png_set_read_fn(png_ptr, NULL, NULL); } - png_ptr->zstream.zalloc = png_zalloc; - png_ptr->zstream.zfree = png_zfree; - png_ptr->zstream.opaque = (voidpf)png_ptr; - - if (!png_cleanup_needed) - { - switch (inflateInit(&png_ptr->zstream)) - { - case Z_OK: - break; /* Do nothing */ - - case Z_MEM_ERROR: - png_warning(png_ptr, "zlib memory error"); - png_cleanup_needed = 1; - break; - - case Z_STREAM_ERROR: - png_warning(png_ptr, "zlib stream error"); - png_cleanup_needed = 1; - break; - - case Z_VERSION_ERROR: - png_warning(png_ptr, "zlib version error"); - png_cleanup_needed = 1; - break; - - default: png_warning(png_ptr, "Unknown zlib error"); - png_cleanup_needed = 1; - } - } - - if (png_cleanup_needed) - { - /* Clean up PNG structure and deallocate any memory. */ - png_free(png_ptr, png_ptr->zbuf); - png_ptr->zbuf = NULL; -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)png_ptr, - (png_free_ptr)free_fn, (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)png_ptr); -#endif - return (NULL); - } - - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - - png_set_read_fn(png_ptr, NULL, NULL); - - - return (png_ptr); + return png_ptr; } @@ -175,8 +89,12 @@ png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, * read if it is determined that this isn't a valid PNG file. */ void PNGAPI -png_read_info(png_structp png_ptr, png_infop info_ptr) +png_read_info(png_structrp png_ptr, png_inforp info_ptr) { +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + int keep; +#endif + png_debug(1, "in png_read_info"); if (png_ptr == NULL || info_ptr == NULL) @@ -190,13 +108,30 @@ png_read_info(png_structp png_ptr, png_infop info_ptr) png_uint_32 length = png_read_chunk_header(png_ptr); png_uint_32 chunk_name = png_ptr->chunk_name; + /* IDAT logic needs to happen here to simplify getting the two flags + * right. + */ + if (chunk_name == png_IDAT) + { + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "Missing IHDR before IDAT"); + + else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + (png_ptr->mode & PNG_HAVE_PLTE) == 0) + png_chunk_error(png_ptr, "Missing PLTE before IDAT"); + + else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) + png_chunk_benign_error(png_ptr, "Too many IDATs found"); + + png_ptr->mode |= PNG_HAVE_IDAT; + } + + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) + png_ptr->mode |= PNG_AFTER_IDAT; + /* This should be a binary subdivision search or a hash for * matching the chunk name rather than a linear search. */ - if (chunk_name == png_IDAT) - if (png_ptr->mode & PNG_AFTER_IDAT) - png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; - if (chunk_name == png_IHDR) png_handle_IHDR(png_ptr, info_ptr, length); @@ -204,26 +139,16 @@ png_read_info(png_structp png_ptr, png_infop info_ptr) png_handle_IEND(png_ptr, info_ptr, length); #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - else if (png_chunk_unknown_handling(png_ptr, chunk_name) != - PNG_HANDLE_CHUNK_AS_DEFAULT) + else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) { - if (chunk_name == png_IDAT) - png_ptr->mode |= PNG_HAVE_IDAT; - - png_handle_unknown(png_ptr, info_ptr, length); + png_handle_unknown(png_ptr, info_ptr, length, keep); if (chunk_name == png_PLTE) png_ptr->mode |= PNG_HAVE_PLTE; else if (chunk_name == png_IDAT) { - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before IDAT"); - - else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && - !(png_ptr->mode & PNG_HAVE_PLTE)) - png_error(png_ptr, "Missing PLTE before IDAT"); - + png_ptr->idat_size = 0; /* It has been consumed */ break; } } @@ -233,15 +158,7 @@ png_read_info(png_structp png_ptr, png_infop info_ptr) else if (chunk_name == png_IDAT) { - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before IDAT"); - - else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && - !(png_ptr->mode & PNG_HAVE_PLTE)) - png_error(png_ptr, "Missing PLTE before IDAT"); - png_ptr->idat_size = length; - png_ptr->mode |= PNG_HAVE_IDAT; break; } @@ -331,27 +248,36 @@ png_read_info(png_structp png_ptr, png_infop info_ptr) #endif else - png_handle_unknown(png_ptr, info_ptr, length); + png_handle_unknown(png_ptr, info_ptr, length, + PNG_HANDLE_CHUNK_AS_DEFAULT); } } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ /* Optional call to update the users info_ptr structure */ void PNGAPI -png_read_update_info(png_structp png_ptr, png_infop info_ptr) +png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) { png_debug(1, "in png_read_update_info"); - if (png_ptr == NULL) - return; + if (png_ptr != NULL) + { + if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) + { + png_read_start_row(png_ptr); - png_read_start_row(png_ptr); +# ifdef PNG_READ_TRANSFORMS_SUPPORTED + png_read_transform_info(png_ptr, info_ptr); +# else + PNG_UNUSED(info_ptr) +# endif + } -#ifdef PNG_READ_TRANSFORMS_SUPPORTED - png_read_transform_info(png_ptr, info_ptr); -#else - PNG_UNUSED(info_ptr) -#endif + /* New in 1.6.0 this avoids the bug of doing the initializations twice */ + else + png_app_error(png_ptr, + "png_read_update_info/png_start_read_image: duplicate call"); + } } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED @@ -361,21 +287,93 @@ png_read_update_info(png_structp png_ptr, png_infop info_ptr) * If the user doesn't call this, we will do it ourselves. */ void PNGAPI -png_start_read_image(png_structp png_ptr) +png_start_read_image(png_structrp png_ptr) { png_debug(1, "in png_start_read_image"); if (png_ptr != NULL) - png_read_start_row(png_ptr); + { + if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) + png_read_start_row(png_ptr); + + /* New in 1.6.0 this avoids the bug of doing the initializations twice */ + else + png_app_error(png_ptr, + "png_start_read_image/png_read_update_info: duplicate call"); + } } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED -void PNGAPI -png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) +#ifdef PNG_MNG_FEATURES_SUPPORTED +/* Undoes intrapixel differencing, + * NOTE: this is apparently only supported in the 'sequential' reader. + */ +static void +png_do_read_intrapixel(png_row_infop row_info, png_bytep row) { - int ret; + png_debug(1, "in png_do_read_intrapixel"); + if ( + (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) + { + int bytes_per_pixel; + png_uint_32 row_width = row_info->width; + + if (row_info->bit_depth == 8) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 3; + + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 4; + + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); + *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); + } + } + else if (row_info->bit_depth == 16) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 6; + + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 8; + + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); + png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); + png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); + png_uint_32 red = (s0 + s1 + 65536) & 0xffff; + png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; + *(rp ) = (png_byte)((red >> 8) & 0xff); + *(rp + 1) = (png_byte)(red & 0xff); + *(rp + 4) = (png_byte)((blue >> 8) & 0xff); + *(rp + 5) = (png_byte)(blue & 0xff); + } + } + } +} +#endif /* MNG_FEATURES */ + +void PNGAPI +png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) +{ png_row_info row_info; if (png_ptr == NULL) @@ -387,7 +385,7 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) /* png_read_start_row sets the information (in particular iwidth) for this * interlace pass. */ - if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) + if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) png_read_start_row(png_ptr); /* 1.5.6: row_info moved out of png_struct to a local here. */ @@ -398,45 +396,47 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) row_info.pixel_depth = png_ptr->pixel_depth; row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); +#ifdef PNG_WARNINGS_SUPPORTED if (png_ptr->row_number == 0 && png_ptr->pass == 0) { /* Check for transforms that have been set but were defined out */ #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) - if (png_ptr->transformations & PNG_INVERT_MONO) + if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) - if (png_ptr->transformations & PNG_FILLER) + if ((png_ptr->transformations & PNG_FILLER) != 0) png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ !defined(PNG_READ_PACKSWAP_SUPPORTED) - if (png_ptr->transformations & PNG_PACKSWAP) + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) - if (png_ptr->transformations & PNG_PACK) + if ((png_ptr->transformations & PNG_PACK) != 0) png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) - if (png_ptr->transformations & PNG_SHIFT) + if ((png_ptr->transformations & PNG_SHIFT) != 0) png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) - if (png_ptr->transformations & PNG_BGR) + if ((png_ptr->transformations & PNG_BGR) != 0) png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); #endif #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) - if (png_ptr->transformations & PNG_SWAP_BYTES) + if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); #endif } +#endif /* WARNINGS */ #ifdef PNG_READ_INTERLACING_SUPPORTED /* If interlaced and we do not need a new row, combine row and return. @@ -445,7 +445,8 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) * untransformed) and, because of the libpng API for interlaced images, this * means we must transform before de-interlacing. */ - if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) != 0) { switch (png_ptr->pass) { @@ -502,6 +503,7 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) return; } break; + case 5: if ((png_ptr->row_number & 1) || png_ptr->width < 2) { @@ -515,7 +517,7 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) default: case 6: - if (!(png_ptr->row_number & 1)) + if ((png_ptr->row_number & 1) == 0) { png_read_finish_row(png_ptr); return; @@ -525,52 +527,11 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) } #endif - if (!(png_ptr->mode & PNG_HAVE_IDAT)) + if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) png_error(png_ptr, "Invalid attempt to read row data"); - png_ptr->zstream.next_out = png_ptr->row_buf; - png_ptr->zstream.avail_out = - (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth, - png_ptr->iwidth) + 1); - - do - { - if (!(png_ptr->zstream.avail_in)) - { - while (!png_ptr->idat_size) - { - png_crc_finish(png_ptr, 0); - - png_ptr->idat_size = png_read_chunk_header(png_ptr); - if (png_ptr->chunk_name != png_IDAT) - png_error(png_ptr, "Not enough image data"); - } - png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; - png_ptr->zstream.next_in = png_ptr->zbuf; - if (png_ptr->zbuf_size > png_ptr->idat_size) - png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; - png_crc_read(png_ptr, png_ptr->zbuf, - (png_size_t)png_ptr->zstream.avail_in); - png_ptr->idat_size -= png_ptr->zstream.avail_in; - } - - ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); - - if (ret == Z_STREAM_END) - { - if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || - png_ptr->idat_size) - png_benign_error(png_ptr, "Extra compressed data"); - png_ptr->mode |= PNG_AFTER_IDAT; - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; - break; - } - - if (ret != Z_OK) - png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : - "Decompression error"); - - } while (png_ptr->zstream.avail_out); + /* Fill the row with IDAT data: */ + png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) { @@ -586,10 +547,10 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) * it may not be in the future, so this was changed just to copy the * interlaced count: */ - png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); + memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); #ifdef PNG_MNG_FEATURES_SUPPORTED - if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) { /* Intrapixel differencing */ @@ -597,7 +558,6 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) } #endif - #ifdef PNG_READ_TRANSFORMS_SUPPORTED if (png_ptr->transformations) png_do_read_transformations(png_ptr, &row_info); @@ -615,9 +575,9 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) png_error(png_ptr, "internal sequential row size calculation error"); #ifdef PNG_READ_INTERLACING_SUPPORTED - /* Blow up interlaced rows to full size */ - if (png_ptr->interlaced && - (png_ptr->transformations & PNG_INTERLACE)) + /* Expand interlaced rows to full size */ + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) != 0) { if (png_ptr->pass < 6) png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, @@ -643,8 +603,9 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) if (png_ptr->read_row_fn != NULL) (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); + } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read one or more rows of image data. If the image is interlaced, @@ -672,7 +633,7 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) */ void PNGAPI -png_read_rows(png_structp png_ptr, png_bytepp row, +png_read_rows(png_structrp png_ptr, png_bytepp row, png_bytepp display_row, png_uint_32 num_rows) { png_uint_32 i; @@ -711,7 +672,7 @@ png_read_rows(png_structp png_ptr, png_bytepp row, dp++; } } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the entire image. If the image has an alpha channel or a tRNS @@ -727,7 +688,7 @@ png_read_rows(png_structp png_ptr, png_bytepp row, * [*] png_handle_alpha() does not exist yet, as of this version of libpng */ void PNGAPI -png_read_image(png_structp png_ptr, png_bytepp image) +png_read_image(png_structrp png_ptr, png_bytepp image) { png_uint_32 i, image_height; int pass, j; @@ -739,7 +700,7 @@ png_read_image(png_structp png_ptr, png_bytepp image) return; #ifdef PNG_READ_INTERLACING_SUPPORTED - if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) + if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) { pass = png_set_interlace_handling(png_ptr); /* And make sure transforms are initialized. */ @@ -747,7 +708,8 @@ png_read_image(png_structp png_ptr, png_bytepp image) } else { - if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE)) + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) == 0) { /* Caller called png_start_read_image or png_read_update_info without * first turning on the PNG_INTERLACE transform. We can fix this here, @@ -784,7 +746,7 @@ png_read_image(png_structp png_ptr, png_bytepp image) } } } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ #ifdef PNG_SEQUENTIAL_READ_SUPPORTED /* Read the end of the PNG file. Will not read past the end of the @@ -792,14 +754,24 @@ png_read_image(png_structp png_ptr, png_bytepp image) * or time information at the end of the file, if info is not NULL. */ void PNGAPI -png_read_end(png_structp png_ptr, png_infop info_ptr) +png_read_end(png_structrp png_ptr, png_inforp info_ptr) { +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + int keep; +#endif + png_debug(1, "in png_read_end"); if (png_ptr == NULL) return; - png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */ + /* If png_read_end is called in the middle of reading the rows there may + * still be pending IDAT data and an owned zstream. Deal with this here. + */ +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED + if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0) +#endif + png_read_finish_IDAT(png_ptr); #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED /* Report invalid palette index; added at libng-1.5.10 */ @@ -813,22 +785,25 @@ png_read_end(png_structp png_ptr, png_infop info_ptr) png_uint_32 length = png_read_chunk_header(png_ptr); png_uint_32 chunk_name = png_ptr->chunk_name; - if (chunk_name == png_IHDR) - png_handle_IHDR(png_ptr, info_ptr, length); - - else if (chunk_name == png_IEND) + if (chunk_name == png_IEND) png_handle_IEND(png_ptr, info_ptr, length); + else if (chunk_name == png_IHDR) + png_handle_IHDR(png_ptr, info_ptr, length); + + else if (info_ptr == NULL) + png_crc_finish(png_ptr, length); + #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - else if (png_chunk_unknown_handling(png_ptr, chunk_name) != - PNG_HANDLE_CHUNK_AS_DEFAULT) + else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) { if (chunk_name == png_IDAT) { - if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) + if ((length > 0) || + (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) png_benign_error(png_ptr, "Too many IDATs found"); } - png_handle_unknown(png_ptr, info_ptr, length); + png_handle_unknown(png_ptr, info_ptr, length, keep); if (chunk_name == png_PLTE) png_ptr->mode |= PNG_HAVE_PLTE; } @@ -839,7 +814,7 @@ png_read_end(png_structp png_ptr, png_infop info_ptr) /* Zero length IDATs are legal after the last IDAT has been * read, but not after other chunks have been read. */ - if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT)) + if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) png_benign_error(png_ptr, "Too many IDATs found"); png_crc_finish(png_ptr, length); @@ -933,181 +908,106 @@ png_read_end(png_structp png_ptr, png_infop info_ptr) #endif else - png_handle_unknown(png_ptr, info_ptr, length); - } while (!(png_ptr->mode & PNG_HAVE_IEND)); + png_handle_unknown(png_ptr, info_ptr, length, + PNG_HANDLE_CHUNK_AS_DEFAULT); + } while ((png_ptr->mode & PNG_HAVE_IEND) == 0); } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ -/* Free all memory used by the read */ -void PNGAPI -png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, - png_infopp end_info_ptr_ptr) +/* Free all memory used in the read struct */ +static void +png_read_destroy(png_structrp png_ptr) { - png_structp png_ptr = NULL; - png_infop info_ptr = NULL, end_info_ptr = NULL; -#ifdef PNG_USER_MEM_SUPPORTED - png_free_ptr free_fn = NULL; - png_voidp mem_ptr = NULL; -#endif - - png_debug(1, "in png_destroy_read_struct"); - - if (png_ptr_ptr != NULL) - png_ptr = *png_ptr_ptr; - if (png_ptr == NULL) - return; - -#ifdef PNG_USER_MEM_SUPPORTED - free_fn = png_ptr->free_fn; - mem_ptr = png_ptr->mem_ptr; -#endif - - if (info_ptr_ptr != NULL) - info_ptr = *info_ptr_ptr; - - if (end_info_ptr_ptr != NULL) - end_info_ptr = *end_info_ptr_ptr; - - png_read_destroy(png_ptr, info_ptr, end_info_ptr); - - if (info_ptr != NULL) - { -#ifdef PNG_TEXT_SUPPORTED - png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1); -#endif - -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, - (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)info_ptr); -#endif - *info_ptr_ptr = NULL; - } - - if (end_info_ptr != NULL) - { -#ifdef PNG_READ_TEXT_SUPPORTED - png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1); -#endif -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn, - (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)end_info_ptr); -#endif - *end_info_ptr_ptr = NULL; - } - - if (png_ptr != NULL) - { -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, - (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)png_ptr); -#endif - *png_ptr_ptr = NULL; - } -} - -/* Free all memory used by the read (old method) */ -void /* PRIVATE */ -png_read_destroy(png_structp png_ptr, png_infop info_ptr, - png_infop end_info_ptr) -{ -#ifdef PNG_SETJMP_SUPPORTED - jmp_buf tmp_jmp; -#endif - png_error_ptr error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - png_error_ptr warning_fn; -#endif - png_voidp error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - png_free_ptr free_fn; -#endif - png_debug(1, "in png_read_destroy"); - if (info_ptr != NULL) - png_info_destroy(png_ptr, info_ptr); - - if (end_info_ptr != NULL) - png_info_destroy(png_ptr, end_info_ptr); - #ifdef PNG_READ_GAMMA_SUPPORTED png_destroy_gamma_table(png_ptr); #endif - png_free(png_ptr, png_ptr->zbuf); png_free(png_ptr, png_ptr->big_row_buf); + png_ptr->big_row_buf = NULL; png_free(png_ptr, png_ptr->big_prev_row); - png_free(png_ptr, png_ptr->chunkdata); + png_ptr->big_prev_row = NULL; + png_free(png_ptr, png_ptr->read_buffer); + png_ptr->read_buffer = NULL; #ifdef PNG_READ_QUANTIZE_SUPPORTED png_free(png_ptr, png_ptr->palette_lookup); + png_ptr->palette_lookup = NULL; png_free(png_ptr, png_ptr->quantize_index); + png_ptr->quantize_index = NULL; #endif - if (png_ptr->free_me & PNG_FREE_PLTE) + if ((png_ptr->free_me & PNG_FREE_PLTE) != 0) + { png_zfree(png_ptr, png_ptr->palette); + png_ptr->palette = NULL; + } png_ptr->free_me &= ~PNG_FREE_PLTE; #if defined(PNG_tRNS_SUPPORTED) || \ defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) - if (png_ptr->free_me & PNG_FREE_TRNS) + if ((png_ptr->free_me & PNG_FREE_TRNS) != 0) + { png_free(png_ptr, png_ptr->trans_alpha); + png_ptr->trans_alpha = NULL; + } png_ptr->free_me &= ~PNG_FREE_TRNS; #endif -#ifdef PNG_READ_hIST_SUPPORTED - if (png_ptr->free_me & PNG_FREE_HIST) - png_free(png_ptr, png_ptr->hist); - png_ptr->free_me &= ~PNG_FREE_HIST; -#endif - inflateEnd(&png_ptr->zstream); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED png_free(png_ptr, png_ptr->save_buffer); + png_ptr->save_buffer = NULL; #endif - /* Save the important info out of the png_struct, in case it is - * being used again. +#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \ + defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + png_free(png_ptr, png_ptr->unknown_chunk.data); + png_ptr->unknown_chunk.data = NULL; +#endif + +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + png_free(png_ptr, png_ptr->chunk_list); + png_ptr->chunk_list = NULL; +#endif + + /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error + * callbacks are still set at this point. They are required to complete the + * destruction of the png_struct itself. */ -#ifdef PNG_SETJMP_SUPPORTED - png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf)); -#endif +} - error_fn = png_ptr->error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - warning_fn = png_ptr->warning_fn; -#endif - error_ptr = png_ptr->error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - free_fn = png_ptr->free_fn; -#endif +/* Free all memory used by the read */ +void PNGAPI +png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, + png_infopp end_info_ptr_ptr) +{ + png_structrp png_ptr = NULL; - png_memset(png_ptr, 0, png_sizeof(png_struct)); + png_debug(1, "in png_destroy_read_struct"); - png_ptr->error_fn = error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - png_ptr->warning_fn = warning_fn; -#endif - png_ptr->error_ptr = error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - png_ptr->free_fn = free_fn; -#endif + if (png_ptr_ptr != NULL) + png_ptr = *png_ptr_ptr; -#ifdef PNG_SETJMP_SUPPORTED - png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf)); -#endif + if (png_ptr == NULL) + return; + /* libpng 1.6.0: use the API to destroy info structs to ensure consistent + * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. + * The extra was, apparently, unnecessary yet this hides memory leak bugs. + */ + png_destroy_info_struct(png_ptr, end_info_ptr_ptr); + png_destroy_info_struct(png_ptr, info_ptr_ptr); + + *png_ptr_ptr = NULL; + png_read_destroy(png_ptr); + png_destroy_png_struct(png_ptr); } void PNGAPI -png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) +png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) { if (png_ptr == NULL) return; @@ -1119,12 +1019,10 @@ png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn) #ifdef PNG_SEQUENTIAL_READ_SUPPORTED #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI -png_read_png(png_structp png_ptr, png_infop info_ptr, +png_read_png(png_structrp png_ptr, png_inforp info_ptr, int transforms, voidp params) { - int row; - if (png_ptr == NULL || info_ptr == NULL) return; @@ -1132,130 +1030,153 @@ png_read_png(png_structp png_ptr, png_infop info_ptr, * PNG file before the first IDAT (image data chunk). */ png_read_info(png_ptr, info_ptr); - if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep)) + if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) png_error(png_ptr, "Image is too high to process with png_read_png()"); /* -------------- image transformations start here ------------------- */ + /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM + * is not implemented. This will only happen in de-configured (non-default) + * libpng builds. The results can be unexpected - png_read_png may return + * short or mal-formed rows because the transform is skipped. + */ -#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED /* Tell libpng to strip 16-bit/color files down to 8 bits per color. */ - if (transforms & PNG_TRANSFORM_SCALE_16) - { - /* Added at libpng-1.5.4. "strip_16" produces the same result that it - * did in earlier versions, while "scale_16" is now more accurate. - */ + if ((transforms & PNG_TRANSFORM_SCALE_16) != 0) + /* Added at libpng-1.5.4. "strip_16" produces the same result that it + * did in earlier versions, while "scale_16" is now more accurate. + */ +#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED png_set_scale_16(png_ptr); - } +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); #endif -#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED /* If both SCALE and STRIP are required pngrtran will effectively cancel the * latter by doing SCALE first. This is ok and allows apps not to check for * which is supported to get the right answer. */ - if (transforms & PNG_TRANSFORM_STRIP_16) + if ((transforms & PNG_TRANSFORM_STRIP_16) != 0) +#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED png_set_strip_16(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); #endif -#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED /* Strip alpha bytes from the input data without combining with * the background (not recommended). */ - if (transforms & PNG_TRANSFORM_STRIP_ALPHA) + if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0) +#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED png_set_strip_alpha(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); #endif -#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED) /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single * byte into separate bytes (useful for paletted and grayscale images). */ - if (transforms & PNG_TRANSFORM_PACKING) + if ((transforms & PNG_TRANSFORM_PACKING) != 0) +#ifdef PNG_READ_PACK_SUPPORTED png_set_packing(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); #endif -#ifdef PNG_READ_PACKSWAP_SUPPORTED /* Change the order of packed pixels to least significant bit first * (not useful if you are using png_set_packing). */ - if (transforms & PNG_TRANSFORM_PACKSWAP) + if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) +#ifdef PNG_READ_PACKSWAP_SUPPORTED png_set_packswap(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); #endif -#ifdef PNG_READ_EXPAND_SUPPORTED /* Expand paletted colors into true RGB triplets * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel * Expand paletted or RGB images with transparency to full alpha * channels so the data will be available as RGBA quartets. */ - if (transforms & PNG_TRANSFORM_EXPAND) - if ((png_ptr->bit_depth < 8) || - (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) || - (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))) - png_set_expand(png_ptr); + if ((transforms & PNG_TRANSFORM_EXPAND) != 0) +#ifdef PNG_READ_EXPAND_SUPPORTED + png_set_expand(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); #endif /* We don't handle background color or gamma transformation or quantizing. */ -#ifdef PNG_READ_INVERT_SUPPORTED /* Invert monochrome files to have 0 as white and 1 as black */ - if (transforms & PNG_TRANSFORM_INVERT_MONO) + if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) +#ifdef PNG_READ_INVERT_SUPPORTED png_set_invert_mono(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); #endif -#ifdef PNG_READ_SHIFT_SUPPORTED /* If you want to shift the pixel values from the range [0,255] or * [0,65535] to the original [0,7] or [0,31], or whatever range the * colors were originally in: */ - if ((transforms & PNG_TRANSFORM_SHIFT) - && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) - { - png_color_8p sig_bit; - - png_get_sBIT(png_ptr, info_ptr, &sig_bit); - png_set_shift(png_ptr, sig_bit); - } + if ((transforms & PNG_TRANSFORM_SHIFT) != 0) +#ifdef PNG_READ_SHIFT_SUPPORTED + if ((info_ptr->valid & PNG_INFO_sBIT) != 0) + png_set_shift(png_ptr, &info_ptr->sig_bit); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); #endif -#ifdef PNG_READ_BGR_SUPPORTED /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ - if (transforms & PNG_TRANSFORM_BGR) + if ((transforms & PNG_TRANSFORM_BGR) != 0) +#ifdef PNG_READ_BGR_SUPPORTED png_set_bgr(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); #endif -#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ - if (transforms & PNG_TRANSFORM_SWAP_ALPHA) + if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) +#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED png_set_swap_alpha(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); #endif -#ifdef PNG_READ_SWAP_SUPPORTED /* Swap bytes of 16-bit files to least significant byte first */ - if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) + if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) +#ifdef PNG_READ_SWAP_SUPPORTED png_set_swap(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); #endif /* Added at libpng-1.2.41 */ -#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel from opacity to transparency */ - if (transforms & PNG_TRANSFORM_INVERT_ALPHA) + if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) +#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED png_set_invert_alpha(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); #endif /* Added at libpng-1.2.41 */ -#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand grayscale image to RGB */ - if (transforms & PNG_TRANSFORM_GRAY_TO_RGB) + if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0) +#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED png_set_gray_to_rgb(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); #endif /* Added at libpng-1.5.4 */ + if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0) #ifdef PNG_READ_EXPAND_16_SUPPORTED - if (transforms & PNG_TRANSFORM_EXPAND_16) png_set_expand_16(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); #endif /* We don't handle adding filler bytes */ @@ -1278,16 +1199,17 @@ png_read_png(png_structp png_ptr, png_infop info_ptr, { png_uint_32 iptr; - info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr, - info_ptr->height * png_sizeof(png_bytep)); + info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr, + info_ptr->height * (sizeof (png_bytep)))); + for (iptr=0; iptrheight; iptr++) info_ptr->row_pointers[iptr] = NULL; info_ptr->free_me |= PNG_FREE_ROWS; - for (row = 0; row < (int)info_ptr->height; row++) - info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr, - png_get_rowbytes(png_ptr, info_ptr)); + for (iptr = 0; iptr < info_ptr->height; iptr++) + info_ptr->row_pointers[iptr] = png_voidcast(png_bytep, + png_malloc(png_ptr, info_ptr->rowbytes)); } png_read_image(png_ptr, info_ptr->row_pointers); @@ -1296,10 +1218,2920 @@ png_read_png(png_structp png_ptr, png_infop info_ptr, /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ png_read_end(png_ptr, info_ptr); - PNG_UNUSED(transforms) /* Quiet compiler warnings */ PNG_UNUSED(params) - } -#endif /* PNG_INFO_IMAGE_SUPPORTED */ -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ -#endif /* PNG_READ_SUPPORTED */ +#endif /* INFO_IMAGE */ +#endif /* SEQUENTIAL_READ */ + +#ifdef PNG_SIMPLIFIED_READ_SUPPORTED +/* SIMPLIFIED READ + * + * This code currently relies on the sequential reader, though it could easily + * be made to work with the progressive one. + */ +/* Arguments to png_image_finish_read: */ + +/* Encoding of PNG data (used by the color-map code) */ +# define P_NOTSET 0 /* File encoding not yet known */ +# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */ +# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ +# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ +# define P_LINEAR8 4 /* 8-bit linear: only from a file value */ + +/* Color-map processing: after libpng has run on the PNG image further + * processing may be needed to convert the data to color-map indices. + */ +#define PNG_CMAP_NONE 0 +#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ +#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ +#define PNG_CMAP_RGB 3 /* Process RGB data */ +#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ + +/* The following document where the background is for each processing case. */ +#define PNG_CMAP_NONE_BACKGROUND 256 +#define PNG_CMAP_GA_BACKGROUND 231 +#define PNG_CMAP_TRANS_BACKGROUND 254 +#define PNG_CMAP_RGB_BACKGROUND 256 +#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 + +typedef struct +{ + /* Arguments: */ + png_imagep image; + png_voidp buffer; + png_int_32 row_stride; + png_voidp colormap; + png_const_colorp background; + /* Local variables: */ + png_voidp local_row; + png_voidp first_row; + ptrdiff_t row_bytes; /* step between rows */ + int file_encoding; /* E_ values above */ + png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */ + int colormap_processing; /* PNG_CMAP_ values above */ +} png_image_read_control; + +/* Do all the *safe* initialization - 'safe' means that png_error won't be + * called, so setting up the jmp_buf is not required. This means that anything + * called from here must *not* call png_malloc - it has to call png_malloc_warn + * instead so that control is returned safely back to this routine. + */ +static int +png_image_read_init(png_imagep image) +{ + if (image->opaque == NULL) + { + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, + png_safe_error, png_safe_warning); + + /* And set the rest of the structure to NULL to ensure that the various + * fields are consistent. + */ + memset(image, 0, (sizeof *image)); + image->version = PNG_IMAGE_VERSION; + + if (png_ptr != NULL) + { + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (info_ptr != NULL) + { + png_controlp control = png_voidcast(png_controlp, + png_malloc_warn(png_ptr, (sizeof *control))); + + if (control != NULL) + { + memset(control, 0, (sizeof *control)); + + control->png_ptr = png_ptr; + control->info_ptr = info_ptr; + control->for_write = 0; + + image->opaque = control; + return 1; + } + + /* Error clean up */ + png_destroy_info_struct(png_ptr, &info_ptr); + } + + png_destroy_read_struct(&png_ptr, NULL, NULL); + } + + return png_image_error(image, "png_image_read: out of memory"); + } + + return png_image_error(image, "png_image_read: opaque pointer not NULL"); +} + +/* Utility to find the base format of a PNG file from a png_struct. */ +static png_uint_32 +png_image_format(png_structrp png_ptr) +{ + png_uint_32 format = 0; + + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) + format |= PNG_FORMAT_FLAG_COLOR; + + if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) + format |= PNG_FORMAT_FLAG_ALPHA; + + /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS + * sets the png_struct fields; that's all we are interested in here. The + * precise interaction with an app call to png_set_tRNS and PNG file reading + * is unclear. + */ + else if (png_ptr->num_trans > 0) + format |= PNG_FORMAT_FLAG_ALPHA; + + if (png_ptr->bit_depth == 16) + format |= PNG_FORMAT_FLAG_LINEAR; + + if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0) + format |= PNG_FORMAT_FLAG_COLORMAP; + + return format; +} + +/* Is the given gamma significantly different from sRGB? The test is the same + * one used in pngrtran.c when deciding whether to do gamma correction. The + * arithmetic optimizes the division by using the fact that the inverse of the + * file sRGB gamma is 2.2 + */ +static int +png_gamma_not_sRGB(png_fixed_point g) +{ + if (g < PNG_FP_1) + { + /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ + if (g == 0) + return 0; + + return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); + } + + return 1; +} + +/* Do the main body of a 'png_image_begin_read' function; read the PNG file + * header and fill in all the information. This is executed in a safe context, + * unlike the init routine above. + */ +static int +png_image_read_header(png_voidp argument) +{ + png_imagep image = png_voidcast(png_imagep, argument); + png_structrp png_ptr = image->opaque->png_ptr; + png_inforp info_ptr = image->opaque->info_ptr; + + png_set_benign_errors(png_ptr, 1/*warn*/); + png_read_info(png_ptr, info_ptr); + + /* Do this the fast way; just read directly out of png_struct. */ + image->width = png_ptr->width; + image->height = png_ptr->height; + + { + png_uint_32 format = png_image_format(png_ptr); + + image->format = format; + +#ifdef PNG_COLORSPACE_SUPPORTED + /* Does the colorspace match sRGB? If there is no color endpoint + * (colorant) information assume yes, otherwise require the + * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the + * colorspace has been determined to be invalid ignore it. + */ + if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags + & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| + PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) + image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; +#endif + } + + /* We need the maximum number of entries regardless of the format the + * application sets here. + */ + { + png_uint_32 cmap_entries; + + switch (png_ptr->color_type) + { + case PNG_COLOR_TYPE_GRAY: + cmap_entries = 1U << png_ptr->bit_depth; + break; + + case PNG_COLOR_TYPE_PALETTE: + cmap_entries = png_ptr->num_palette; + break; + + default: + cmap_entries = 256; + break; + } + + if (cmap_entries > 256) + cmap_entries = 256; + + image->colormap_entries = cmap_entries; + } + + return 1; +} + +#ifdef PNG_STDIO_SUPPORTED +int PNGAPI +png_image_begin_read_from_stdio(png_imagep image, FILE* file) +{ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + if (file != NULL) + { + if (png_image_read_init(image) != 0) + { + /* This is slightly evil, but png_init_io doesn't do anything other + * than this and we haven't changed the standard IO functions so + * this saves a 'safe' function. + */ + image->opaque->png_ptr->io_ptr = file; + return png_safe_execute(image, png_image_read_header, image); + } + } + + else + return png_image_error(image, + "png_image_begin_read_from_stdio: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); + + return 0; +} + +int PNGAPI +png_image_begin_read_from_file(png_imagep image, const char *file_name) +{ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + if (file_name != NULL) + { + FILE *fp = fopen(file_name, "rb"); + + if (fp != NULL) + { + if (png_image_read_init(image) != 0) + { + image->opaque->png_ptr->io_ptr = fp; + image->opaque->owned_file = 1; + return png_safe_execute(image, png_image_read_header, image); + } + + /* Clean up: just the opened file. */ + (void)fclose(fp); + } + + else + return png_image_error(image, strerror(errno)); + } + + else + return png_image_error(image, + "png_image_begin_read_from_file: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); + + return 0; +} +#endif /* STDIO */ + +static void PNGCBAPI +png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) +{ + if (png_ptr != NULL) + { + png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); + if (image != NULL) + { + png_controlp cp = image->opaque; + if (cp != NULL) + { + png_const_bytep memory = cp->memory; + png_size_t size = cp->size; + + if (memory != NULL && size >= need) + { + memcpy(out, memory, need); + cp->memory = memory + need; + cp->size = size - need; + return; + } + + png_error(png_ptr, "read beyond end of data"); + } + } + + png_error(png_ptr, "invalid memory read"); + } +} + +int PNGAPI png_image_begin_read_from_memory(png_imagep image, + png_const_voidp memory, png_size_t size) +{ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + if (memory != NULL && size > 0) + { + if (png_image_read_init(image) != 0) + { + /* Now set the IO functions to read from the memory buffer and + * store it into io_ptr. Again do this in-place to avoid calling a + * libpng function that requires error handling. + */ + image->opaque->memory = png_voidcast(png_const_bytep, memory); + image->opaque->size = size; + image->opaque->png_ptr->io_ptr = image; + image->opaque->png_ptr->read_data_fn = png_image_memory_read; + + return png_safe_execute(image, png_image_read_header, image); + } + } + + else + return png_image_error(image, + "png_image_begin_read_from_memory: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); + + return 0; +} + +/* Utility function to skip chunks that are not used by the simplified image + * read functions and an appropriate macro to call it. + */ +#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +static void +png_image_skip_unused_chunks(png_structrp png_ptr) +{ + /* Prepare the reader to ignore all recognized chunks whose data will not + * be used, i.e., all chunks recognized by libpng except for those + * involved in basic image reading: + * + * IHDR, PLTE, IDAT, IEND + * + * Or image data handling: + * + * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT. + * + * This provides a small performance improvement and eliminates any + * potential vulnerability to security problems in the unused chunks. + * + * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored + * too. This allows the simplified API to be compiled without iCCP support, + * however if the support is there the chunk is still checked to detect + * errors (which are unfortunately quite common.) + */ + { + static PNG_CONST png_byte chunks_to_process[] = { + 98, 75, 71, 68, '\0', /* bKGD */ + 99, 72, 82, 77, '\0', /* cHRM */ + 103, 65, 77, 65, '\0', /* gAMA */ +# ifdef PNG_READ_iCCP_SUPPORTED + 105, 67, 67, 80, '\0', /* iCCP */ +# endif + 115, 66, 73, 84, '\0', /* sBIT */ + 115, 82, 71, 66, '\0', /* sRGB */ + }; + + /* Ignore unknown chunks and all other chunks except for the + * IHDR, PLTE, tRNS, IDAT, and IEND chunks. + */ + png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, + NULL, -1); + + /* But do not ignore image data handling chunks */ + png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, + chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5); + } +} + +# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) +#else +# define PNG_SKIP_CHUNKS(p) ((void)0) +#endif /* HANDLE_AS_UNKNOWN */ + +/* The following macro gives the exact rounded answer for all values in the + * range 0..255 (it actually divides by 51.2, but the rounding still generates + * the correct numbers 0..5 + */ +#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) + +/* Utility functions to make particular color-maps */ +static void +set_file_encoding(png_image_read_control *display) +{ + png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; + if (png_gamma_significant(g) != 0) + { + if (png_gamma_not_sRGB(g) != 0) + { + display->file_encoding = P_FILE; + display->gamma_to_linear = png_reciprocal(g); + } + + else + display->file_encoding = P_sRGB; + } + + else + display->file_encoding = P_LINEAR8; +} + +static unsigned int +decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) +{ + if (encoding == P_FILE) /* double check */ + encoding = display->file_encoding; + + if (encoding == P_NOTSET) /* must be the file encoding */ + { + set_file_encoding(display); + encoding = display->file_encoding; + } + + switch (encoding) + { + case P_FILE: + value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); + break; + + case P_sRGB: + value = png_sRGB_table[value]; + break; + + case P_LINEAR: + break; + + case P_LINEAR8: + value *= 257; + break; + +#ifdef __GNUC__ + default: + png_error(display->image->opaque->png_ptr, + "unexpected encoding (internal error)"); +#endif + } + + return value; +} + +static png_uint_32 +png_colormap_compose(png_image_read_control *display, + png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, + png_uint_32 background, int encoding) +{ + /* The file value is composed on the background, the background has the given + * encoding and so does the result, the file is encoded with P_FILE and the + * file and alpha are 8-bit values. The (output) encoding will always be + * P_LINEAR or P_sRGB. + */ + png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); + png_uint_32 b = decode_gamma(display, background, encoding); + + /* The alpha is always an 8-bit value (it comes from the palette), the value + * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. + */ + f = f * alpha + b * (255-alpha); + + if (encoding == P_LINEAR) + { + /* Scale to 65535; divide by 255, approximately (in fact this is extremely + * accurate, it divides by 255.00000005937181414556, with no overflow.) + */ + f *= 257; /* Now scaled by 65535 */ + f += f >> 16; + f = (f+32768) >> 16; + } + + else /* P_sRGB */ + f = PNG_sRGB_FROM_LINEAR(f); + + return f; +} + +/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must + * be 8-bit. + */ +static void +png_create_colormap_entry(png_image_read_control *display, + png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, + png_uint_32 alpha, int encoding) +{ + png_imagep image = display->image; + const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ? + P_LINEAR : P_sRGB; + const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && + (red != green || green != blue); + + if (ip > 255) + png_error(image->opaque->png_ptr, "color-map index out of range"); + + /* Update the cache with whether the file gamma is significantly different + * from sRGB. + */ + if (encoding == P_FILE) + { + if (display->file_encoding == P_NOTSET) + set_file_encoding(display); + + /* Note that the cached value may be P_FILE too, but if it is then the + * gamma_to_linear member has been set. + */ + encoding = display->file_encoding; + } + + if (encoding == P_FILE) + { + png_fixed_point g = display->gamma_to_linear; + + red = png_gamma_16bit_correct(red*257, g); + green = png_gamma_16bit_correct(green*257, g); + blue = png_gamma_16bit_correct(blue*257, g); + + if (convert_to_Y != 0 || output_encoding == P_LINEAR) + { + alpha *= 257; + encoding = P_LINEAR; + } + + else + { + red = PNG_sRGB_FROM_LINEAR(red * 255); + green = PNG_sRGB_FROM_LINEAR(green * 255); + blue = PNG_sRGB_FROM_LINEAR(blue * 255); + encoding = P_sRGB; + } + } + + else if (encoding == P_LINEAR8) + { + /* This encoding occurs quite frequently in test cases because PngSuite + * includes a gAMA 1.0 chunk with most images. + */ + red *= 257; + green *= 257; + blue *= 257; + alpha *= 257; + encoding = P_LINEAR; + } + + else if (encoding == P_sRGB && + (convert_to_Y != 0 || output_encoding == P_LINEAR)) + { + /* The values are 8-bit sRGB values, but must be converted to 16-bit + * linear. + */ + red = png_sRGB_table[red]; + green = png_sRGB_table[green]; + blue = png_sRGB_table[blue]; + alpha *= 257; + encoding = P_LINEAR; + } + + /* This is set if the color isn't gray but the output is. */ + if (encoding == P_LINEAR) + { + if (convert_to_Y != 0) + { + /* NOTE: these values are copied from png_do_rgb_to_gray */ + png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + + (png_uint_32)2366 * blue; + + if (output_encoding == P_LINEAR) + y = (y + 16384) >> 15; + + else + { + /* y is scaled by 32768, we need it scaled by 255: */ + y = (y + 128) >> 8; + y *= 255; + y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); + alpha = PNG_DIV257(alpha); + encoding = P_sRGB; + } + + blue = red = green = y; + } + + else if (output_encoding == P_sRGB) + { + red = PNG_sRGB_FROM_LINEAR(red * 255); + green = PNG_sRGB_FROM_LINEAR(green * 255); + blue = PNG_sRGB_FROM_LINEAR(blue * 255); + alpha = PNG_DIV257(alpha); + encoding = P_sRGB; + } + } + + if (encoding != output_encoding) + png_error(image->opaque->png_ptr, "bad encoding (internal error)"); + + /* Store the value. */ + { +# ifdef PNG_FORMAT_AFIRST_SUPPORTED + const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && + (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; +# else +# define afirst 0 +# endif +# ifdef PNG_FORMAT_BGR_SUPPORTED + const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; +# else +# define bgr 0 +# endif + + if (output_encoding == P_LINEAR) + { + png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); + + entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); + + /* The linear 16-bit values must be pre-multiplied by the alpha channel + * value, if less than 65535 (this is, effectively, composite on black + * if the alpha channel is removed.) + */ + switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) + { + case 4: + entry[afirst ? 0 : 3] = (png_uint_16)alpha; + /* FALL THROUGH */ + + case 3: + if (alpha < 65535) + { + if (alpha > 0) + { + blue = (blue * alpha + 32767U)/65535U; + green = (green * alpha + 32767U)/65535U; + red = (red * alpha + 32767U)/65535U; + } + + else + red = green = blue = 0; + } + entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; + entry[afirst + 1] = (png_uint_16)green; + entry[afirst + bgr] = (png_uint_16)red; + break; + + case 2: + entry[1 ^ afirst] = (png_uint_16)alpha; + /* FALL THROUGH */ + + case 1: + if (alpha < 65535) + { + if (alpha > 0) + green = (green * alpha + 32767U)/65535U; + + else + green = 0; + } + entry[afirst] = (png_uint_16)green; + break; + + default: + break; + } + } + + else /* output encoding is P_sRGB */ + { + png_bytep entry = png_voidcast(png_bytep, display->colormap); + + entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); + + switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) + { + case 4: + entry[afirst ? 0 : 3] = (png_byte)alpha; + case 3: + entry[afirst + (2 ^ bgr)] = (png_byte)blue; + entry[afirst + 1] = (png_byte)green; + entry[afirst + bgr] = (png_byte)red; + break; + + case 2: + entry[1 ^ afirst] = (png_byte)alpha; + case 1: + entry[afirst] = (png_byte)green; + break; + + default: + break; + } + } + +# ifdef afirst +# undef afirst +# endif +# ifdef bgr +# undef bgr +# endif + } +} + +static int +make_gray_file_colormap(png_image_read_control *display) +{ + unsigned int i; + + for (i=0; i<256; ++i) + png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); + + return i; +} + +static int +make_gray_colormap(png_image_read_control *display) +{ + unsigned int i; + + for (i=0; i<256; ++i) + png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); + + return i; +} +#define PNG_GRAY_COLORMAP_ENTRIES 256 + +static int +make_ga_colormap(png_image_read_control *display) +{ + unsigned int i, a; + + /* Alpha is retained, the output will be a color-map with entries + * selected by six levels of alpha. One transparent entry, 6 gray + * levels for all the intermediate alpha values, leaving 230 entries + * for the opaque grays. The color-map entries are the six values + * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the + * relevant entry. + * + * if (alpha > 229) // opaque + * { + * // The 231 entries are selected to make the math below work: + * base = 0; + * entry = (231 * gray + 128) >> 8; + * } + * else if (alpha < 26) // transparent + * { + * base = 231; + * entry = 0; + * } + * else // partially opaque + * { + * base = 226 + 6 * PNG_DIV51(alpha); + * entry = PNG_DIV51(gray); + * } + */ + i = 0; + while (i < 231) + { + unsigned int gray = (i * 256 + 115) / 231; + png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); + } + + /* 255 is used here for the component values for consistency with the code + * that undoes premultiplication in pngwrite.c. + */ + png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); + + for (a=1; a<5; ++a) + { + unsigned int g; + + for (g=0; g<6; ++g) + png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, + P_sRGB); + } + + return i; +} + +#define PNG_GA_COLORMAP_ENTRIES 256 + +static int +make_rgb_colormap(png_image_read_control *display) +{ + unsigned int i, r; + + /* Build a 6x6x6 opaque RGB cube */ + for (i=r=0; r<6; ++r) + { + unsigned int g; + + for (g=0; g<6; ++g) + { + unsigned int b; + + for (b=0; b<6; ++b) + png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, + P_sRGB); + } + } + + return i; +} + +#define PNG_RGB_COLORMAP_ENTRIES 216 + +/* Return a palette index to the above palette given three 8-bit sRGB values. */ +#define PNG_RGB_INDEX(r,g,b) \ + ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) + +static int +png_image_read_colormap(png_voidp argument) +{ + png_image_read_control *display = + png_voidcast(png_image_read_control*, argument); + const png_imagep image = display->image; + + const png_structrp png_ptr = image->opaque->png_ptr; + const png_uint_32 output_format = image->format; + const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ? + P_LINEAR : P_sRGB; + + unsigned int cmap_entries; + unsigned int output_processing; /* Output processing option */ + unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */ + + /* Background information; the background color and the index of this color + * in the color-map if it exists (else 256). + */ + unsigned int background_index = 256; + png_uint_32 back_r, back_g, back_b; + + /* Flags to accumulate things that need to be done to the input. */ + int expand_tRNS = 0; + + /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is + * very difficult to do, the results look awful, and it is difficult to see + * what possible use it is because the application can't control the + * color-map. + */ + if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || + png_ptr->num_trans > 0) /* alpha in input */ && + ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) + { + if (output_encoding == P_LINEAR) /* compose on black */ + back_b = back_g = back_r = 0; + + else if (display->background == NULL /* no way to remove it */) + png_error(png_ptr, + "a background color must be supplied to remove alpha/transparency"); + + /* Get a copy of the background color (this avoids repeating the checks + * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the + * output format. + */ + else + { + back_g = display->background->green; + if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0) + { + back_r = display->background->red; + back_b = display->background->blue; + } + else + back_b = back_r = back_g; + } + } + + else if (output_encoding == P_LINEAR) + back_b = back_r = back_g = 65535; + + else + back_b = back_r = back_g = 255; + + /* Default the input file gamma if required - this is necessary because + * libpng assumes that if no gamma information is present the data is in the + * output format, but the simplified API deduces the gamma from the input + * format. + */ + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0) + { + /* Do this directly, not using the png_colorspace functions, to ensure + * that it happens even if the colorspace is invalid (though probably if + * it is the setting will be ignored) Note that the same thing can be + * achieved at the application interface with png_set_gAMA. + */ + if (png_ptr->bit_depth == 16 && + (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) + png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR; + + else + png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE; + + png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; + } + + /* Decide what to do based on the PNG color type of the input data. The + * utility function png_create_colormap_entry deals with most aspects of the + * output transformations; this code works out how to produce bytes of + * color-map entries from the original format. + */ + switch (png_ptr->color_type) + { + case PNG_COLOR_TYPE_GRAY: + if (png_ptr->bit_depth <= 8) + { + /* There at most 256 colors in the output, regardless of + * transparency. + */ + unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; + + cmap_entries = 1U << png_ptr->bit_depth; + if (cmap_entries > image->colormap_entries) + png_error(png_ptr, "gray[8] color-map: too few entries"); + + step = 255 / (cmap_entries - 1); + output_processing = PNG_CMAP_NONE; + + /* If there is a tRNS chunk then this either selects a transparent + * value or, if the output has no alpha, the background color. + */ + if (png_ptr->num_trans > 0) + { + trans = png_ptr->trans_color.gray; + + if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) + back_alpha = output_encoding == P_LINEAR ? 65535 : 255; + } + + /* png_create_colormap_entry just takes an RGBA and writes the + * corresponding color-map entry using the format from 'image', + * including the required conversion to sRGB or linear as + * appropriate. The input values are always either sRGB (if the + * gamma correction flag is 0) or 0..255 scaled file encoded values + * (if the function must gamma correct them). + */ + for (i=val=0; ibit_depth < 8) + png_set_packing(png_ptr); + } + + else /* bit depth is 16 */ + { + /* The 16-bit input values can be converted directly to 8-bit gamma + * encoded values; however, if a tRNS chunk is present 257 color-map + * entries are required. This means that the extra entry requires + * special processing; add an alpha channel, sacrifice gray level + * 254 and convert transparent (alpha==0) entries to that. + * + * Use libpng to chop the data to 8 bits. Convert it to sRGB at the + * same time to minimize quality loss. If a tRNS chunk is present + * this means libpng must handle it too; otherwise it is impossible + * to do the exact match on the 16-bit value. + * + * If the output has no alpha channel *and* the background color is + * gray then it is possible to let libpng handle the substitution by + * ensuring that the corresponding gray level matches the background + * color exactly. + */ + data_encoding = P_sRGB; + + if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "gray[16] color-map: too few entries"); + + cmap_entries = make_gray_colormap(display); + + if (png_ptr->num_trans > 0) + { + unsigned int back_alpha; + + if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) + back_alpha = 0; + + else + { + if (back_r == back_g && back_g == back_b) + { + /* Background is gray; no special processing will be + * required. + */ + png_color_16 c; + png_uint_32 gray = back_g; + + if (output_encoding == P_LINEAR) + { + gray = PNG_sRGB_FROM_LINEAR(gray * 255); + + /* And make sure the corresponding palette entry + * matches. + */ + png_create_colormap_entry(display, gray, back_g, back_g, + back_g, 65535, P_LINEAR); + } + + /* The background passed to libpng, however, must be the + * sRGB value. + */ + c.index = 0; /*unused*/ + c.gray = c.red = c.green = c.blue = (png_uint_16)gray; + + /* NOTE: does this work without expanding tRNS to alpha? + * It should be the color->gray case below apparently + * doesn't. + */ + png_set_background_fixed(png_ptr, &c, + PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, + 0/*gamma: not used*/); + + output_processing = PNG_CMAP_NONE; + break; + } +#ifdef __COVERITY__ + /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) + * here. + */ + back_alpha = 255; +#else + back_alpha = output_encoding == P_LINEAR ? 65535 : 255; +#endif + } + + /* output_processing means that the libpng-processed row will be + * 8-bit GA and it has to be processing to single byte color-map + * values. Entry 254 is replaced by either a completely + * transparent entry or by the background color at full + * precision (and the background color is not a simple gray + * level in this case.) + */ + expand_tRNS = 1; + output_processing = PNG_CMAP_TRANS; + background_index = 254; + + /* And set (overwrite) color-map entry 254 to the actual + * background color at full precision. + */ + png_create_colormap_entry(display, 254, back_r, back_g, back_b, + back_alpha, output_encoding); + } + + else + output_processing = PNG_CMAP_NONE; + } + break; + + case PNG_COLOR_TYPE_GRAY_ALPHA: + /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum + * of 65536 combinations. If, however, the alpha channel is to be + * removed there are only 256 possibilities if the background is gray. + * (Otherwise there is a subset of the 65536 possibilities defined by + * the triangle between black, white and the background color.) + * + * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to + * worry about tRNS matching - tRNS is ignored if there is an alpha + * channel. + */ + data_encoding = P_sRGB; + + if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "gray+alpha color-map: too few entries"); + + cmap_entries = make_ga_colormap(display); + + background_index = PNG_CMAP_GA_BACKGROUND; + output_processing = PNG_CMAP_GA; + } + + else /* alpha is removed */ + { + /* Alpha must be removed as the PNG data is processed when the + * background is a color because the G and A channels are + * independent and the vector addition (non-parallel vectors) is a + * 2-D problem. + * + * This can be reduced to the same algorithm as above by making a + * colormap containing gray levels (for the opaque grays), a + * background entry (for a transparent pixel) and a set of four six + * level color values, one set for each intermediate alpha value. + * See the comments in make_ga_colormap for how this works in the + * per-pixel processing. + * + * If the background is gray, however, we only need a 256 entry gray + * level color map. It is sufficient to make the entry generated + * for the background color be exactly the color specified. + */ + if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || + (back_r == back_g && back_g == back_b)) + { + /* Background is gray; no special processing will be required. */ + png_color_16 c; + png_uint_32 gray = back_g; + + if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "gray-alpha color-map: too few entries"); + + cmap_entries = make_gray_colormap(display); + + if (output_encoding == P_LINEAR) + { + gray = PNG_sRGB_FROM_LINEAR(gray * 255); + + /* And make sure the corresponding palette entry matches. */ + png_create_colormap_entry(display, gray, back_g, back_g, + back_g, 65535, P_LINEAR); + } + + /* The background passed to libpng, however, must be the sRGB + * value. + */ + c.index = 0; /*unused*/ + c.gray = c.red = c.green = c.blue = (png_uint_16)gray; + + png_set_background_fixed(png_ptr, &c, + PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, + 0/*gamma: not used*/); + + output_processing = PNG_CMAP_NONE; + } + + else + { + png_uint_32 i, a; + + /* This is the same as png_make_ga_colormap, above, except that + * the entries are all opaque. + */ + if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "ga-alpha color-map: too few entries"); + + i = 0; + while (i < 231) + { + png_uint_32 gray = (i * 256 + 115) / 231; + png_create_colormap_entry(display, i++, gray, gray, gray, + 255, P_sRGB); + } + + /* NOTE: this preserves the full precision of the application + * background color. + */ + background_index = i; + png_create_colormap_entry(display, i++, back_r, back_g, back_b, +#ifdef __COVERITY__ + /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) + * here. + */ 255U, +#else + output_encoding == P_LINEAR ? 65535U : 255U, +#endif + output_encoding); + + /* For non-opaque input composite on the sRGB background - this + * requires inverting the encoding for each component. The input + * is still converted to the sRGB encoding because this is a + * reasonable approximate to the logarithmic curve of human + * visual sensitivity, at least over the narrow range which PNG + * represents. Consequently 'G' is always sRGB encoded, while + * 'A' is linear. We need the linear background colors. + */ + if (output_encoding == P_sRGB) /* else already linear */ + { + /* This may produce a value not exactly matching the + * background, but that's ok because these numbers are only + * used when alpha != 0 + */ + back_r = png_sRGB_table[back_r]; + back_g = png_sRGB_table[back_g]; + back_b = png_sRGB_table[back_b]; + } + + for (a=1; a<5; ++a) + { + unsigned int g; + + /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled + * by an 8-bit alpha value (0..255). + */ + png_uint_32 alpha = 51 * a; + png_uint_32 back_rx = (255-alpha) * back_r; + png_uint_32 back_gx = (255-alpha) * back_g; + png_uint_32 back_bx = (255-alpha) * back_b; + + for (g=0; g<6; ++g) + { + png_uint_32 gray = png_sRGB_table[g*51] * alpha; + + png_create_colormap_entry(display, i++, + PNG_sRGB_FROM_LINEAR(gray + back_rx), + PNG_sRGB_FROM_LINEAR(gray + back_gx), + PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); + } + } + + cmap_entries = i; + output_processing = PNG_CMAP_GA; + } + } + break; + + case PNG_COLOR_TYPE_RGB: + case PNG_COLOR_TYPE_RGB_ALPHA: + /* Exclude the case where the output is gray; we can always handle this + * with the cases above. + */ + if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) + { + /* The color-map will be grayscale, so we may as well convert the + * input RGB values to a simple grayscale and use the grayscale + * code above. + * + * NOTE: calling this apparently damages the recognition of the + * transparent color in background color handling; call + * png_set_tRNS_to_alpha before png_set_background_fixed. + */ + png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, + -1); + data_encoding = P_sRGB; + + /* The output will now be one or two 8-bit gray or gray+alpha + * channels. The more complex case arises when the input has alpha. + */ + if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + png_ptr->num_trans > 0) && + (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + /* Both input and output have an alpha channel, so no background + * processing is required; just map the GA bytes to the right + * color-map entry. + */ + expand_tRNS = 1; + + if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "rgb[ga] color-map: too few entries"); + + cmap_entries = make_ga_colormap(display); + background_index = PNG_CMAP_GA_BACKGROUND; + output_processing = PNG_CMAP_GA; + } + + else + { + /* Either the input or the output has no alpha channel, so there + * will be no non-opaque pixels in the color-map; it will just be + * grayscale. + */ + if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "rgb[gray] color-map: too few entries"); + + /* Ideally this code would use libpng to do the gamma correction, + * but if an input alpha channel is to be removed we will hit the + * libpng bug in gamma+compose+rgb-to-gray (the double gamma + * correction bug). Fix this by dropping the gamma correction in + * this case and doing it in the palette; this will result in + * duplicate palette entries, but that's better than the + * alternative of double gamma correction. + */ + if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + png_ptr->num_trans > 0) && + png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0) + { + cmap_entries = make_gray_file_colormap(display); + data_encoding = P_FILE; + } + + else + cmap_entries = make_gray_colormap(display); + + /* But if the input has alpha or transparency it must be removed + */ + if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + png_ptr->num_trans > 0) + { + png_color_16 c; + png_uint_32 gray = back_g; + + /* We need to ensure that the application background exists in + * the colormap and that completely transparent pixels map to + * it. Achieve this simply by ensuring that the entry + * selected for the background really is the background color. + */ + if (data_encoding == P_FILE) /* from the fixup above */ + { + /* The app supplied a gray which is in output_encoding, we + * need to convert it to a value of the input (P_FILE) + * encoding then set this palette entry to the required + * output encoding. + */ + if (output_encoding == P_sRGB) + gray = png_sRGB_table[gray]; /* now P_LINEAR */ + + gray = PNG_DIV257(png_gamma_16bit_correct(gray, + png_ptr->colorspace.gamma)); /* now P_FILE */ + + /* And make sure the corresponding palette entry contains + * exactly the required sRGB value. + */ + png_create_colormap_entry(display, gray, back_g, back_g, + back_g, 0/*unused*/, output_encoding); + } + + else if (output_encoding == P_LINEAR) + { + gray = PNG_sRGB_FROM_LINEAR(gray * 255); + + /* And make sure the corresponding palette entry matches. + */ + png_create_colormap_entry(display, gray, back_g, back_g, + back_g, 0/*unused*/, P_LINEAR); + } + + /* The background passed to libpng, however, must be the + * output (normally sRGB) value. + */ + c.index = 0; /*unused*/ + c.gray = c.red = c.green = c.blue = (png_uint_16)gray; + + /* NOTE: the following is apparently a bug in libpng. Without + * it the transparent color recognition in + * png_set_background_fixed seems to go wrong. + */ + expand_tRNS = 1; + png_set_background_fixed(png_ptr, &c, + PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, + 0/*gamma: not used*/); + } + + output_processing = PNG_CMAP_NONE; + } + } + + else /* output is color */ + { + /* We could use png_quantize here so long as there is no transparent + * color or alpha; png_quantize ignores alpha. Easier overall just + * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. + * Consequently we always want libpng to produce sRGB data. + */ + data_encoding = P_sRGB; + + /* Is there any transparency or alpha? */ + if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + png_ptr->num_trans > 0) + { + /* Is there alpha in the output too? If so all four channels are + * processed into a special RGB cube with alpha support. + */ + if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + png_uint_32 r; + + if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) + png_error(png_ptr, "rgb+alpha color-map: too few entries"); + + cmap_entries = make_rgb_colormap(display); + + /* Add a transparent entry. */ + png_create_colormap_entry(display, cmap_entries, 255, 255, + 255, 0, P_sRGB); + + /* This is stored as the background index for the processing + * algorithm. + */ + background_index = cmap_entries++; + + /* Add 27 r,g,b entries each with alpha 0.5. */ + for (r=0; r<256; r = (r << 1) | 0x7f) + { + png_uint_32 g; + + for (g=0; g<256; g = (g << 1) | 0x7f) + { + png_uint_32 b; + + /* This generates components with the values 0, 127 and + * 255 + */ + for (b=0; b<256; b = (b << 1) | 0x7f) + png_create_colormap_entry(display, cmap_entries++, + r, g, b, 128, P_sRGB); + } + } + + expand_tRNS = 1; + output_processing = PNG_CMAP_RGB_ALPHA; + } + + else + { + /* Alpha/transparency must be removed. The background must + * exist in the color map (achieved by setting adding it after + * the 666 color-map). If the standard processing code will + * pick up this entry automatically that's all that is + * required; libpng can be called to do the background + * processing. + */ + unsigned int sample_size = + PNG_IMAGE_SAMPLE_SIZE(output_format); + png_uint_32 r, g, b; /* sRGB background */ + + if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) + png_error(png_ptr, "rgb-alpha color-map: too few entries"); + + cmap_entries = make_rgb_colormap(display); + + png_create_colormap_entry(display, cmap_entries, back_r, + back_g, back_b, 0/*unused*/, output_encoding); + + if (output_encoding == P_LINEAR) + { + r = PNG_sRGB_FROM_LINEAR(back_r * 255); + g = PNG_sRGB_FROM_LINEAR(back_g * 255); + b = PNG_sRGB_FROM_LINEAR(back_b * 255); + } + + else + { + r = back_r; + g = back_g; + b = back_g; + } + + /* Compare the newly-created color-map entry with the one the + * PNG_CMAP_RGB algorithm will use. If the two entries don't + * match, add the new one and set this as the background + * index. + */ + if (memcmp((png_const_bytep)display->colormap + + sample_size * cmap_entries, + (png_const_bytep)display->colormap + + sample_size * PNG_RGB_INDEX(r,g,b), + sample_size) != 0) + { + /* The background color must be added. */ + background_index = cmap_entries++; + + /* Add 27 r,g,b entries each with created by composing with + * the background at alpha 0.5. + */ + for (r=0; r<256; r = (r << 1) | 0x7f) + { + for (g=0; g<256; g = (g << 1) | 0x7f) + { + /* This generates components with the values 0, 127 + * and 255 + */ + for (b=0; b<256; b = (b << 1) | 0x7f) + png_create_colormap_entry(display, cmap_entries++, + png_colormap_compose(display, r, P_sRGB, 128, + back_r, output_encoding), + png_colormap_compose(display, g, P_sRGB, 128, + back_g, output_encoding), + png_colormap_compose(display, b, P_sRGB, 128, + back_b, output_encoding), + 0/*unused*/, output_encoding); + } + } + + expand_tRNS = 1; + output_processing = PNG_CMAP_RGB_ALPHA; + } + + else /* background color is in the standard color-map */ + { + png_color_16 c; + + c.index = 0; /*unused*/ + c.red = (png_uint_16)back_r; + c.gray = c.green = (png_uint_16)back_g; + c.blue = (png_uint_16)back_b; + + png_set_background_fixed(png_ptr, &c, + PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, + 0/*gamma: not used*/); + + output_processing = PNG_CMAP_RGB; + } + } + } + + else /* no alpha or transparency in the input */ + { + /* Alpha in the output is irrelevant, simply map the opaque input + * pixels to the 6x6x6 color-map. + */ + if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) + png_error(png_ptr, "rgb color-map: too few entries"); + + cmap_entries = make_rgb_colormap(display); + output_processing = PNG_CMAP_RGB; + } + } + break; + + case PNG_COLOR_TYPE_PALETTE: + /* It's already got a color-map. It may be necessary to eliminate the + * tRNS entries though. + */ + { + unsigned int num_trans = png_ptr->num_trans; + png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; + png_const_colorp colormap = png_ptr->palette; + const int do_background = trans != NULL && + (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; + unsigned int i; + + /* Just in case: */ + if (trans == NULL) + num_trans = 0; + + output_processing = PNG_CMAP_NONE; + data_encoding = P_FILE; /* Don't change from color-map indices */ + cmap_entries = png_ptr->num_palette; + if (cmap_entries > 256) + cmap_entries = 256; + + if (cmap_entries > image->colormap_entries) + png_error(png_ptr, "palette color-map: too few entries"); + + for (i=0; i < cmap_entries; ++i) + { + if (do_background != 0 && i < num_trans && trans[i] < 255) + { + if (trans[i] == 0) + png_create_colormap_entry(display, i, back_r, back_g, + back_b, 0, output_encoding); + + else + { + /* Must compose the PNG file color in the color-map entry + * on the sRGB color in 'back'. + */ + png_create_colormap_entry(display, i, + png_colormap_compose(display, colormap[i].red, P_FILE, + trans[i], back_r, output_encoding), + png_colormap_compose(display, colormap[i].green, P_FILE, + trans[i], back_g, output_encoding), + png_colormap_compose(display, colormap[i].blue, P_FILE, + trans[i], back_b, output_encoding), + output_encoding == P_LINEAR ? trans[i] * 257U : + trans[i], + output_encoding); + } + } + + else + png_create_colormap_entry(display, i, colormap[i].red, + colormap[i].green, colormap[i].blue, + i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/); + } + + /* The PNG data may have indices packed in fewer than 8 bits, it + * must be expanded if so. + */ + if (png_ptr->bit_depth < 8) + png_set_packing(png_ptr); + } + break; + + default: + png_error(png_ptr, "invalid PNG color type"); + /*NOT REACHED*/ + break; + } + + /* Now deal with the output processing */ + if (expand_tRNS != 0 && png_ptr->num_trans > 0 && + (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) + png_set_tRNS_to_alpha(png_ptr); + + switch (data_encoding) + { + case P_sRGB: + /* Change to 8-bit sRGB */ + png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); + /* FALL THROUGH */ + + case P_FILE: + if (png_ptr->bit_depth > 8) + png_set_scale_16(png_ptr); + break; + +#ifdef __GNUC__ + default: + png_error(png_ptr, "bad data option (internal error)"); +#endif + } + + if (cmap_entries > 256 || cmap_entries > image->colormap_entries) + png_error(png_ptr, "color map overflow (BAD internal error)"); + + image->colormap_entries = cmap_entries; + + /* Double check using the recorded background index */ + switch (output_processing) + { + case PNG_CMAP_NONE: + if (background_index != PNG_CMAP_NONE_BACKGROUND) + goto bad_background; + break; + + case PNG_CMAP_GA: + if (background_index != PNG_CMAP_GA_BACKGROUND) + goto bad_background; + break; + + case PNG_CMAP_TRANS: + if (background_index >= cmap_entries || + background_index != PNG_CMAP_TRANS_BACKGROUND) + goto bad_background; + break; + + case PNG_CMAP_RGB: + if (background_index != PNG_CMAP_RGB_BACKGROUND) + goto bad_background; + break; + + case PNG_CMAP_RGB_ALPHA: + if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) + goto bad_background; + break; + + default: + png_error(png_ptr, "bad processing option (internal error)"); + + bad_background: + png_error(png_ptr, "bad background index (internal error)"); + } + + display->colormap_processing = output_processing; + + return 1/*ok*/; +} + +/* The final part of the color-map read called from png_image_finish_read. */ +static int +png_image_read_and_map(png_voidp argument) +{ + png_image_read_control *display = png_voidcast(png_image_read_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + int passes; + + /* Called when the libpng data must be transformed into the color-mapped + * form. There is a local row buffer in display->local and this routine must + * do the interlace handling. + */ + switch (png_ptr->interlaced) + { + case PNG_INTERLACE_NONE: + passes = 1; + break; + + case PNG_INTERLACE_ADAM7: + passes = PNG_INTERLACE_ADAM7_PASSES; + break; + + default: + png_error(png_ptr, "unknown interlace type"); + } + + { + png_uint_32 height = image->height; + png_uint_32 width = image->width; + int proc = display->colormap_processing; + png_bytep first_row = png_voidcast(png_bytep, display->first_row); + ptrdiff_t step_row = display->row_bytes; + int pass; + + for (pass = 0; pass < passes; ++pass) + { + unsigned int startx, stepx, stepy; + png_uint_32 y; + + if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) + { + /* The row may be empty for a short image: */ + if (PNG_PASS_COLS(width, pass) == 0) + continue; + + startx = PNG_PASS_START_COL(pass); + stepx = PNG_PASS_COL_OFFSET(pass); + y = PNG_PASS_START_ROW(pass); + stepy = PNG_PASS_ROW_OFFSET(pass); + } + + else + { + y = 0; + startx = 0; + stepx = stepy = 1; + } + + for (; ylocal_row); + png_bytep outrow = first_row + y * step_row; + png_const_bytep end_row = outrow + width; + + /* Read read the libpng data into the temporary buffer. */ + png_read_row(png_ptr, inrow, NULL); + + /* Now process the row according to the processing option, note + * that the caller verifies that the format of the libpng output + * data is as required. + */ + outrow += startx; + switch (proc) + { + case PNG_CMAP_GA: + for (; outrow < end_row; outrow += stepx) + { + /* The data is always in the PNG order */ + unsigned int gray = *inrow++; + unsigned int alpha = *inrow++; + unsigned int entry; + + /* NOTE: this code is copied as a comment in + * make_ga_colormap above. Please update the + * comment if you change this code! + */ + if (alpha > 229) /* opaque */ + { + entry = (231 * gray + 128) >> 8; + } + else if (alpha < 26) /* transparent */ + { + entry = 231; + } + else /* partially opaque */ + { + entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); + } + + *outrow = (png_byte)entry; + } + break; + + case PNG_CMAP_TRANS: + for (; outrow < end_row; outrow += stepx) + { + png_byte gray = *inrow++; + png_byte alpha = *inrow++; + + if (alpha == 0) + *outrow = PNG_CMAP_TRANS_BACKGROUND; + + else if (gray != PNG_CMAP_TRANS_BACKGROUND) + *outrow = gray; + + else + *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); + } + break; + + case PNG_CMAP_RGB: + for (; outrow < end_row; outrow += stepx) + { + *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); + inrow += 3; + } + break; + + case PNG_CMAP_RGB_ALPHA: + for (; outrow < end_row; outrow += stepx) + { + unsigned int alpha = inrow[3]; + + /* Because the alpha entries only hold alpha==0.5 values + * split the processing at alpha==0.25 (64) and 0.75 + * (196). + */ + + if (alpha >= 196) + *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], + inrow[2]); + + else if (alpha < 64) + *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; + + else + { + /* Likewise there are three entries for each of r, g + * and b. We could select the entry by popcount on + * the top two bits on those architectures that + * support it, this is what the code below does, + * crudely. + */ + unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; + + /* Here are how the values map: + * + * 0x00 .. 0x3f -> 0 + * 0x40 .. 0xbf -> 1 + * 0xc0 .. 0xff -> 2 + * + * So, as above with the explicit alpha checks, the + * breakpoints are at 64 and 196. + */ + if (inrow[0] & 0x80) back_i += 9; /* red */ + if (inrow[0] & 0x40) back_i += 9; + if (inrow[0] & 0x80) back_i += 3; /* green */ + if (inrow[0] & 0x40) back_i += 3; + if (inrow[0] & 0x80) back_i += 1; /* blue */ + if (inrow[0] & 0x40) back_i += 1; + + *outrow = (png_byte)back_i; + } + + inrow += 4; + } + break; + + default: + break; + } + } + } + } + + return 1; +} + +static int +png_image_read_colormapped(png_voidp argument) +{ + png_image_read_control *display = png_voidcast(png_image_read_control*, + argument); + png_imagep image = display->image; + png_controlp control = image->opaque; + png_structrp png_ptr = control->png_ptr; + png_inforp info_ptr = control->info_ptr; + + int passes = 0; /* As a flag */ + + PNG_SKIP_CHUNKS(png_ptr); + + /* Update the 'info' structure and make sure the result is as required; first + * make sure to turn on the interlace handling if it will be required + * (because it can't be turned on *after* the call to png_read_update_info!) + */ + if (display->colormap_processing == PNG_CMAP_NONE) + passes = png_set_interlace_handling(png_ptr); + + png_read_update_info(png_ptr, info_ptr); + + /* The expected output can be deduced from the colormap_processing option. */ + switch (display->colormap_processing) + { + case PNG_CMAP_NONE: + /* Output must be one channel and one byte per pixel, the output + * encoding can be anything. + */ + if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || + info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && + info_ptr->bit_depth == 8) + break; + + goto bad_output; + + case PNG_CMAP_TRANS: + case PNG_CMAP_GA: + /* Output must be two channels and the 'G' one must be sRGB, the latter + * can be checked with an exact number because it should have been set + * to this number above! + */ + if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && + info_ptr->bit_depth == 8 && + png_ptr->screen_gamma == PNG_GAMMA_sRGB && + image->colormap_entries == 256) + break; + + goto bad_output; + + case PNG_CMAP_RGB: + /* Output must be 8-bit sRGB encoded RGB */ + if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && + info_ptr->bit_depth == 8 && + png_ptr->screen_gamma == PNG_GAMMA_sRGB && + image->colormap_entries == 216) + break; + + goto bad_output; + + case PNG_CMAP_RGB_ALPHA: + /* Output must be 8-bit sRGB encoded RGBA */ + if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && + info_ptr->bit_depth == 8 && + png_ptr->screen_gamma == PNG_GAMMA_sRGB && + image->colormap_entries == 244 /* 216 + 1 + 27 */) + break; + + /* goto bad_output; */ + /* FALL THROUGH */ + + default: + bad_output: + png_error(png_ptr, "bad color-map processing (internal error)"); + } + + /* Now read the rows. Do this here if it is possible to read directly into + * the output buffer, otherwise allocate a local row buffer of the maximum + * size libpng requires and call the relevant processing routine safely. + */ + { + png_voidp first_row = display->buffer; + ptrdiff_t row_bytes = display->row_stride; + + /* The following expression is designed to work correctly whether it gives + * a signed or an unsigned result. + */ + if (row_bytes < 0) + { + char *ptr = png_voidcast(char*, first_row); + ptr += (image->height-1) * (-row_bytes); + first_row = png_voidcast(png_voidp, ptr); + } + + display->first_row = first_row; + display->row_bytes = row_bytes; + } + + if (passes == 0) + { + int result; + png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); + + display->local_row = row; + result = png_safe_execute(image, png_image_read_and_map, display); + display->local_row = NULL; + png_free(png_ptr, row); + + return result; + } + + else + { + png_alloc_size_t row_bytes = display->row_bytes; + + while (--passes >= 0) + { + png_uint_32 y = image->height; + png_bytep row = png_voidcast(png_bytep, display->first_row); + + while (y-- > 0) + { + png_read_row(png_ptr, row, NULL); + row += row_bytes; + } + } + + return 1; + } +} + +/* Just the row reading part of png_image_read. */ +static int +png_image_read_composite(png_voidp argument) +{ + png_image_read_control *display = png_voidcast(png_image_read_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + int passes; + + switch (png_ptr->interlaced) + { + case PNG_INTERLACE_NONE: + passes = 1; + break; + + case PNG_INTERLACE_ADAM7: + passes = PNG_INTERLACE_ADAM7_PASSES; + break; + + default: + png_error(png_ptr, "unknown interlace type"); + } + + { + png_uint_32 height = image->height; + png_uint_32 width = image->width; + ptrdiff_t step_row = display->row_bytes; + unsigned int channels = + (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; + int pass; + + for (pass = 0; pass < passes; ++pass) + { + unsigned int startx, stepx, stepy; + png_uint_32 y; + + if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) + { + /* The row may be empty for a short image: */ + if (PNG_PASS_COLS(width, pass) == 0) + continue; + + startx = PNG_PASS_START_COL(pass) * channels; + stepx = PNG_PASS_COL_OFFSET(pass) * channels; + y = PNG_PASS_START_ROW(pass); + stepy = PNG_PASS_ROW_OFFSET(pass); + } + + else + { + y = 0; + startx = 0; + stepx = channels; + stepy = 1; + } + + for (; ylocal_row); + png_bytep outrow; + png_const_bytep end_row; + + /* Read the row, which is packed: */ + png_read_row(png_ptr, inrow, NULL); + + outrow = png_voidcast(png_bytep, display->first_row); + outrow += y * step_row; + end_row = outrow + width * channels; + + /* Now do the composition on each pixel in this row. */ + outrow += startx; + for (; outrow < end_row; outrow += stepx) + { + png_byte alpha = inrow[channels]; + + if (alpha > 0) /* else no change to the output */ + { + unsigned int c; + + for (c=0; cimage; + png_structrp png_ptr = image->opaque->png_ptr; + png_inforp info_ptr = image->opaque->info_ptr; + png_uint_32 height = image->height; + png_uint_32 width = image->width; + int pass, passes; + + /* Double check the convoluted logic below. We expect to get here with + * libpng doing rgb to gray and gamma correction but background processing + * left to the png_image_read_background function. The rows libpng produce + * might be 8 or 16-bit but should always have two channels; gray plus alpha. + */ + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) + png_error(png_ptr, "lost rgb to gray"); + + if ((png_ptr->transformations & PNG_COMPOSE) != 0) + png_error(png_ptr, "unexpected compose"); + + if (png_get_channels(png_ptr, info_ptr) != 2) + png_error(png_ptr, "lost/gained channels"); + + /* Expect the 8-bit case to always remove the alpha channel */ + if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && + (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) + png_error(png_ptr, "unexpected 8-bit transformation"); + + switch (png_ptr->interlaced) + { + case PNG_INTERLACE_NONE: + passes = 1; + break; + + case PNG_INTERLACE_ADAM7: + passes = PNG_INTERLACE_ADAM7_PASSES; + break; + + default: + png_error(png_ptr, "unknown interlace type"); + } + + /* Use direct access to info_ptr here because otherwise the simplified API + * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is + * checking the value after libpng expansions, not the original value in the + * PNG. + */ + switch (info_ptr->bit_depth) + { + case 8: + /* 8-bit sRGB gray values with an alpha channel; the alpha channel is + * to be removed by composing on a background: either the row if + * display->background is NULL or display->background->green if not. + * Unlike the code above ALPHA_OPTIMIZED has *not* been done. + */ + { + png_bytep first_row = png_voidcast(png_bytep, display->first_row); + ptrdiff_t step_row = display->row_bytes; + + for (pass = 0; pass < passes; ++pass) + { + png_bytep row = png_voidcast(png_bytep, + display->first_row); + unsigned int startx, stepx, stepy; + png_uint_32 y; + + if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) + { + /* The row may be empty for a short image: */ + if (PNG_PASS_COLS(width, pass) == 0) + continue; + + startx = PNG_PASS_START_COL(pass); + stepx = PNG_PASS_COL_OFFSET(pass); + y = PNG_PASS_START_ROW(pass); + stepy = PNG_PASS_ROW_OFFSET(pass); + } + + else + { + y = 0; + startx = 0; + stepx = stepy = 1; + } + + if (display->background == NULL) + { + for (; ylocal_row); + png_bytep outrow = first_row + y * step_row; + png_const_bytep end_row = outrow + width; + + /* Read the row, which is packed: */ + png_read_row(png_ptr, inrow, NULL); + + /* Now do the composition on each pixel in this row. */ + outrow += startx; + for (; outrow < end_row; outrow += stepx) + { + png_byte alpha = inrow[1]; + + if (alpha > 0) /* else no change to the output */ + { + png_uint_32 component = inrow[0]; + + if (alpha < 255) /* else just use component */ + { + /* Since PNG_OPTIMIZED_ALPHA was not set it is + * necessary to invert the sRGB transfer + * function and multiply the alpha out. + */ + component = png_sRGB_table[component] * alpha; + component += png_sRGB_table[outrow[0]] * + (255-alpha); + component = PNG_sRGB_FROM_LINEAR(component); + } + + outrow[0] = (png_byte)component; + } + + inrow += 2; /* gray and alpha channel */ + } + } + } + + else /* constant background value */ + { + png_byte background8 = display->background->green; + png_uint_16 background = png_sRGB_table[background8]; + + for (; ylocal_row); + png_bytep outrow = first_row + y * step_row; + png_const_bytep end_row = outrow + width; + + /* Read the row, which is packed: */ + png_read_row(png_ptr, inrow, NULL); + + /* Now do the composition on each pixel in this row. */ + outrow += startx; + for (; outrow < end_row; outrow += stepx) + { + png_byte alpha = inrow[1]; + + if (alpha > 0) /* else use background */ + { + png_uint_32 component = inrow[0]; + + if (alpha < 255) /* else just use component */ + { + component = png_sRGB_table[component] * alpha; + component += background * (255-alpha); + component = PNG_sRGB_FROM_LINEAR(component); + } + + outrow[0] = (png_byte)component; + } + + else + outrow[0] = background8; + + inrow += 2; /* gray and alpha channel */ + } + + row += display->row_bytes; + } + } + } + } + break; + + case 16: + /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must + * still be done and, maybe, the alpha channel removed. This code also + * handles the alpha-first option. + */ + { + png_uint_16p first_row = png_voidcast(png_uint_16p, + display->first_row); + /* The division by two is safe because the caller passed in a + * stride which was multiplied by 2 (below) to get row_bytes. + */ + ptrdiff_t step_row = display->row_bytes / 2; + int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; + unsigned int outchannels = 1+preserve_alpha; + int swap_alpha = 0; + +# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED + if (preserve_alpha != 0 && + (image->format & PNG_FORMAT_FLAG_AFIRST) != 0) + swap_alpha = 1; +# endif + + for (pass = 0; pass < passes; ++pass) + { + unsigned int startx, stepx, stepy; + png_uint_32 y; + + /* The 'x' start and step are adjusted to output components here. + */ + if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) + { + /* The row may be empty for a short image: */ + if (PNG_PASS_COLS(width, pass) == 0) + continue; + + startx = PNG_PASS_START_COL(pass) * outchannels; + stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; + y = PNG_PASS_START_ROW(pass); + stepy = PNG_PASS_ROW_OFFSET(pass); + } + + else + { + y = 0; + startx = 0; + stepx = outchannels; + stepy = 1; + } + + for (; ylocal_row), NULL); + inrow = png_voidcast(png_const_uint_16p, display->local_row); + + /* Now do the pre-multiplication on each pixel in this row. + */ + outrow += startx; + for (; outrow < end_row; outrow += stepx) + { + png_uint_32 component = inrow[0]; + png_uint_16 alpha = inrow[1]; + + if (alpha > 0) /* else 0 */ + { + if (alpha < 65535) /* else just use component */ + { + component *= alpha; + component += 32767; + component /= 65535; + } + } + + else + component = 0; + + outrow[swap_alpha] = (png_uint_16)component; + if (preserve_alpha != 0) + outrow[1 ^ swap_alpha] = alpha; + + inrow += 2; /* components and alpha channel */ + } + } + } + } + break; + +#ifdef __GNUC__ + default: + png_error(png_ptr, "unexpected bit depth"); +#endif + } + + return 1; +} + +/* The guts of png_image_finish_read as a png_safe_execute callback. */ +static int +png_image_read_direct(png_voidp argument) +{ + png_image_read_control *display = png_voidcast(png_image_read_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + png_inforp info_ptr = image->opaque->info_ptr; + + png_uint_32 format = image->format; + int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; + int do_local_compose = 0; + int do_local_background = 0; /* to avoid double gamma correction bug */ + int passes = 0; + + /* Add transforms to ensure the correct output format is produced then check + * that the required implementation support is there. Always expand; always + * need 8 bits minimum, no palette and expanded tRNS. + */ + png_set_expand(png_ptr); + + /* Now check the format to see if it was modified. */ + { + png_uint_32 base_format = png_image_format(png_ptr) & + ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; + png_uint_32 change = format ^ base_format; + png_fixed_point output_gamma; + int mode; /* alpha mode */ + + /* Do this first so that we have a record if rgb to gray is happening. */ + if ((change & PNG_FORMAT_FLAG_COLOR) != 0) + { + /* gray<->color transformation required. */ + if ((format & PNG_FORMAT_FLAG_COLOR) != 0) + png_set_gray_to_rgb(png_ptr); + + else + { + /* libpng can't do both rgb to gray and + * background/pre-multiplication if there is also significant gamma + * correction, because both operations require linear colors and + * the code only supports one transform doing the gamma correction. + * Handle this by doing the pre-multiplication or background + * operation in this code, if necessary. + * + * TODO: fix this by rewriting pngrtran.c (!) + * + * For the moment (given that fixing this in pngrtran.c is an + * enormous change) 'do_local_background' is used to indicate that + * the problem exists. + */ + if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) + do_local_background = 1/*maybe*/; + + png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, + PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); + } + + change &= ~PNG_FORMAT_FLAG_COLOR; + } + + /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. + */ + { + png_fixed_point input_gamma_default; + + if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 && + (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) + input_gamma_default = PNG_GAMMA_LINEAR; + else + input_gamma_default = PNG_DEFAULT_sRGB; + + /* Call png_set_alpha_mode to set the default for the input gamma; the + * output gamma is set by a second call below. + */ + png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); + } + + if (linear != 0) + { + /* If there *is* an alpha channel in the input it must be multiplied + * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. + */ + if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) + mode = PNG_ALPHA_STANDARD; /* associated alpha */ + + else + mode = PNG_ALPHA_PNG; + + output_gamma = PNG_GAMMA_LINEAR; + } + + else + { + mode = PNG_ALPHA_PNG; + output_gamma = PNG_DEFAULT_sRGB; + } + + /* If 'do_local_background' is set check for the presence of gamma + * correction; this is part of the work-round for the libpng bug + * described above. + * + * TODO: fix libpng and remove this. + */ + if (do_local_background != 0) + { + png_fixed_point gtest; + + /* This is 'png_gamma_threshold' from pngrtran.c; the test used for + * gamma correction, the screen gamma hasn't been set on png_struct + * yet; it's set below. png_struct::gamma, however, is set to the + * final value. + */ + if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, + PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0) + do_local_background = 0; + + else if (mode == PNG_ALPHA_STANDARD) + { + do_local_background = 2/*required*/; + mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ + } + + /* else leave as 1 for the checks below */ + } + + /* If the bit-depth changes then handle that here. */ + if ((change & PNG_FORMAT_FLAG_LINEAR) != 0) + { + if (linear != 0 /*16-bit output*/) + png_set_expand_16(png_ptr); + + else /* 8-bit output */ + png_set_scale_16(png_ptr); + + change &= ~PNG_FORMAT_FLAG_LINEAR; + } + + /* Now the background/alpha channel changes. */ + if ((change & PNG_FORMAT_FLAG_ALPHA) != 0) + { + /* Removing an alpha channel requires composition for the 8-bit + * formats; for the 16-bit it is already done, above, by the + * pre-multiplication and the channel just needs to be stripped. + */ + if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + /* If RGB->gray is happening the alpha channel must be left and the + * operation completed locally. + * + * TODO: fix libpng and remove this. + */ + if (do_local_background != 0) + do_local_background = 2/*required*/; + + /* 16-bit output: just remove the channel */ + else if (linear != 0) /* compose on black (well, pre-multiply) */ + png_set_strip_alpha(png_ptr); + + /* 8-bit output: do an appropriate compose */ + else if (display->background != NULL) + { + png_color_16 c; + + c.index = 0; /*unused*/ + c.red = display->background->red; + c.green = display->background->green; + c.blue = display->background->blue; + c.gray = display->background->green; + + /* This is always an 8-bit sRGB value, using the 'green' channel + * for gray is much better than calculating the luminance here; + * we can get off-by-one errors in that calculation relative to + * the app expectations and that will show up in transparent + * pixels. + */ + png_set_background_fixed(png_ptr, &c, + PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, + 0/*gamma: not used*/); + } + + else /* compose on row: implemented below. */ + { + do_local_compose = 1; + /* This leaves the alpha channel in the output, so it has to be + * removed by the code below. Set the encoding to the 'OPTIMIZE' + * one so the code only has to hack on the pixels that require + * composition. + */ + mode = PNG_ALPHA_OPTIMIZED; + } + } + + else /* output needs an alpha channel */ + { + /* This is tricky because it happens before the swap operation has + * been accomplished; however, the swap does *not* swap the added + * alpha channel (weird API), so it must be added in the correct + * place. + */ + png_uint_32 filler; /* opaque filler */ + int where; + + if (linear != 0) + filler = 65535; + + else + filler = 255; + +# ifdef PNG_FORMAT_AFIRST_SUPPORTED + if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) + { + where = PNG_FILLER_BEFORE; + change &= ~PNG_FORMAT_FLAG_AFIRST; + } + + else +# endif + where = PNG_FILLER_AFTER; + + png_set_add_alpha(png_ptr, filler, where); + } + + /* This stops the (irrelevant) call to swap_alpha below. */ + change &= ~PNG_FORMAT_FLAG_ALPHA; + } + + /* Now set the alpha mode correctly; this is always done, even if there is + * no alpha channel in either the input or the output because it correctly + * sets the output gamma. + */ + png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); + +# ifdef PNG_FORMAT_BGR_SUPPORTED + if ((change & PNG_FORMAT_FLAG_BGR) != 0) + { + /* Check only the output format; PNG is never BGR; don't do this if + * the output is gray, but fix up the 'format' value in that case. + */ + if ((format & PNG_FORMAT_FLAG_COLOR) != 0) + png_set_bgr(png_ptr); + + else + format &= ~PNG_FORMAT_FLAG_BGR; + + change &= ~PNG_FORMAT_FLAG_BGR; + } +# endif + +# ifdef PNG_FORMAT_AFIRST_SUPPORTED + if ((change & PNG_FORMAT_FLAG_AFIRST) != 0) + { + /* Only relevant if there is an alpha channel - it's particularly + * important to handle this correctly because do_local_compose may + * be set above and then libpng will keep the alpha channel for this + * code to remove. + */ + if ((format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + /* Disable this if doing a local background, + * TODO: remove this when local background is no longer required. + */ + if (do_local_background != 2) + png_set_swap_alpha(png_ptr); + } + + else + format &= ~PNG_FORMAT_FLAG_AFIRST; + + change &= ~PNG_FORMAT_FLAG_AFIRST; + } +# endif + + /* If the *output* is 16-bit then we need to check for a byte-swap on this + * architecture. + */ + if (linear != 0) + { + PNG_CONST png_uint_16 le = 0x0001; + + if ((*(png_const_bytep) & le) != 0) + png_set_swap(png_ptr); + } + + /* If change is not now 0 some transformation is missing - error out. */ + if (change != 0) + png_error(png_ptr, "png_read_image: unsupported transformation"); + } + + PNG_SKIP_CHUNKS(png_ptr); + + /* Update the 'info' structure and make sure the result is as required; first + * make sure to turn on the interlace handling if it will be required + * (because it can't be turned on *after* the call to png_read_update_info!) + * + * TODO: remove the do_local_background fixup below. + */ + if (do_local_compose == 0 && do_local_background != 2) + passes = png_set_interlace_handling(png_ptr); + + png_read_update_info(png_ptr, info_ptr); + + { + png_uint_32 info_format = 0; + + if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) + info_format |= PNG_FORMAT_FLAG_COLOR; + + if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) + { + /* do_local_compose removes this channel below. */ + if (do_local_compose == 0) + { + /* do_local_background does the same if required. */ + if (do_local_background != 2 || + (format & PNG_FORMAT_FLAG_ALPHA) != 0) + info_format |= PNG_FORMAT_FLAG_ALPHA; + } + } + + else if (do_local_compose != 0) /* internal error */ + png_error(png_ptr, "png_image_read: alpha channel lost"); + + if (info_ptr->bit_depth == 16) + info_format |= PNG_FORMAT_FLAG_LINEAR; + +# ifdef PNG_FORMAT_BGR_SUPPORTED + if ((png_ptr->transformations & PNG_BGR) != 0) + info_format |= PNG_FORMAT_FLAG_BGR; +# endif + +# ifdef PNG_FORMAT_AFIRST_SUPPORTED + if (do_local_background == 2) + { + if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) + info_format |= PNG_FORMAT_FLAG_AFIRST; + } + + if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || + ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && + (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) + { + if (do_local_background == 2) + png_error(png_ptr, "unexpected alpha swap transformation"); + + info_format |= PNG_FORMAT_FLAG_AFIRST; + } +# endif + + /* This is actually an internal error. */ + if (info_format != format) + png_error(png_ptr, "png_read_image: invalid transformations"); + } + + /* Now read the rows. If do_local_compose is set then it is necessary to use + * a local row buffer. The output will be GA, RGBA or BGRA and must be + * converted to G, RGB or BGR as appropriate. The 'local_row' member of the + * display acts as a flag. + */ + { + png_voidp first_row = display->buffer; + ptrdiff_t row_bytes = display->row_stride; + + if (linear != 0) + row_bytes *= 2; + + /* The following expression is designed to work correctly whether it gives + * a signed or an unsigned result. + */ + if (row_bytes < 0) + { + char *ptr = png_voidcast(char*, first_row); + ptr += (image->height-1) * (-row_bytes); + first_row = png_voidcast(png_voidp, ptr); + } + + display->first_row = first_row; + display->row_bytes = row_bytes; + } + + if (do_local_compose != 0) + { + int result; + png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); + + display->local_row = row; + result = png_safe_execute(image, png_image_read_composite, display); + display->local_row = NULL; + png_free(png_ptr, row); + + return result; + } + + else if (do_local_background == 2) + { + int result; + png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); + + display->local_row = row; + result = png_safe_execute(image, png_image_read_background, display); + display->local_row = NULL; + png_free(png_ptr, row); + + return result; + } + + else + { + png_alloc_size_t row_bytes = display->row_bytes; + + while (--passes >= 0) + { + png_uint_32 y = image->height; + png_bytep row = png_voidcast(png_bytep, display->first_row); + + while (y-- > 0) + { + png_read_row(png_ptr, row, NULL); + row += row_bytes; + } + } + + return 1; + } +} + +int PNGAPI +png_image_finish_read(png_imagep image, png_const_colorp background, + void *buffer, png_int_32 row_stride, void *colormap) +{ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + png_uint_32 check; + + if (row_stride == 0) + row_stride = PNG_IMAGE_ROW_STRIDE(*image); + + if (row_stride < 0) + check = -row_stride; + + else + check = row_stride; + + if (image->opaque != NULL && buffer != NULL && + check >= PNG_IMAGE_ROW_STRIDE(*image)) + { + if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || + (image->colormap_entries > 0 && colormap != NULL)) + { + int result; + png_image_read_control display; + + memset(&display, 0, (sizeof display)); + display.image = image; + display.buffer = buffer; + display.row_stride = row_stride; + display.colormap = colormap; + display.background = background; + display.local_row = NULL; + + /* Choose the correct 'end' routine; for the color-map case all the + * setup has already been done. + */ + if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) + result = + png_safe_execute(image, png_image_read_colormap, &display) && + png_safe_execute(image, png_image_read_colormapped, &display); + + else + result = + png_safe_execute(image, png_image_read_direct, &display); + + png_image_free(image); + return result; + } + + else + return png_image_error(image, + "png_image_finish_read[color-map]: no color-map"); + } + + else + return png_image_error(image, + "png_image_finish_read: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_finish_read: damaged PNG_IMAGE_VERSION"); + + return 0; +} + +#endif /* SIMPLIFIED_READ */ +#endif /* READ */ diff --git a/3rdparty/libpng/pngrio.c b/3rdparty/libpng/pngrio.c index e9c381c5b..38f7fd49d 100644 --- a/3rdparty/libpng/pngrio.c +++ b/3rdparty/libpng/pngrio.c @@ -1,8 +1,8 @@ /* pngrio.c - functions for data input * - * Last changed in libpng 1.5.0 [January 6, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.17 [March 26, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -26,10 +26,10 @@ * reads from a file pointer. Note that this routine sometimes gets called * with very small lengths, so you should implement some kind of simple * buffering if you are using unbuffered reads. This should never be asked - * to read more then 64K on a 16 bit machine. + * to read more than 64K on a 16-bit machine. */ void /* PRIVATE */ -png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) +png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length) { png_debug1(4, "reading %d bytes", (int)length); @@ -46,7 +46,6 @@ png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) * read_data function and use it at run time with png_set_read_fn(), rather * than changing the library. */ -# ifndef USE_FAR_KEYWORD void PNGCBAPI png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { @@ -58,68 +57,11 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) /* fread() returns 0 on error, so it is OK to store this in a png_size_t * instead of an int, which is what fread() actually returns. */ - check = fread(data, 1, length, (png_FILE_p)png_ptr->io_ptr); + check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); if (check != length) png_error(png_ptr, "Read Error"); } -# else -/* This is the model-independent version. Since the standard I/O library - can't handle far buffers in the medium and small models, we have to copy - the data. -*/ - -#define NEAR_BUF_SIZE 1024 -#define MIN(a,b) (a <= b ? a : b) - -static void PNGCBAPI -png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) -{ - png_size_t check; - png_byte *n_data; - png_FILE_p io_ptr; - - if (png_ptr == NULL) - return; - - /* Check if data really is near. If so, use usual code. */ - n_data = (png_byte *)CVT_PTR_NOCHECK(data); - io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); - - if ((png_bytep)n_data == data) - { - check = fread(n_data, 1, length, io_ptr); - } - - else - { - png_byte buf[NEAR_BUF_SIZE]; - png_size_t read, remaining, err; - check = 0; - remaining = length; - - do - { - read = MIN(NEAR_BUF_SIZE, remaining); - err = fread(buf, 1, read, io_ptr); - png_memcpy(data, buf, read); /* copy far buffer to near buffer */ - - if (err != read) - break; - - else - check += err; - - data += read; - remaining -= read; - } - while (remaining != 0); - } - - if ((png_uint_32)check != (png_uint_32)length) - png_error(png_ptr, "read Error"); -} -# endif #endif /* This function allows the application to supply a new input function @@ -142,7 +84,7 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length) * be used. */ void PNGAPI -png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, +png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr read_data_fn) { if (png_ptr == NULL) @@ -160,6 +102,7 @@ png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, png_ptr->read_data_fn = read_data_fn; #endif +#ifdef PNG_WRITE_SUPPORTED /* It is an error to write to a read device */ if (png_ptr->write_data_fn != NULL) { @@ -168,9 +111,10 @@ png_set_read_fn(png_structp png_ptr, png_voidp io_ptr, "Can't set both read_data_fn and write_data_fn in the" " same structure"); } +#endif #ifdef PNG_WRITE_FLUSH_SUPPORTED png_ptr->output_flush_fn = NULL; #endif } -#endif /* PNG_READ_SUPPORTED */ +#endif /* READ */ diff --git a/3rdparty/libpng/pngrtran.c b/3rdparty/libpng/pngrtran.c index 96732b55c..f129ef129 100644 --- a/3rdparty/libpng/pngrtran.c +++ b/3rdparty/libpng/pngrtran.c @@ -1,8 +1,8 @@ /* pngrtran.c - transforms the data in a row for PNG readers * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -22,7 +22,7 @@ /* Set the action on getting a CRC error for an ancillary or critical chunk. */ void PNGAPI -png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) +png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) { png_debug(1, "in png_set_crc_action"); @@ -88,16 +88,47 @@ png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) } } +#ifdef PNG_READ_TRANSFORMS_SUPPORTED +/* Is it OK to set a transformation now? Only if png_start_read_image or + * png_read_update_info have not been called. It is not necessary for the IHDR + * to have been read in all cases; the need_IHDR parameter allows for this + * check too. + */ +static int +png_rtran_ok(png_structrp png_ptr, int need_IHDR) +{ + if (png_ptr != NULL) + { + if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) + png_app_error(png_ptr, + "invalid after png_start_read_image or png_read_update_info"); + + else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_app_error(png_ptr, "invalid before the PNG header has been read"); + + else + { + /* Turn on failure to initialize correctly for all transforms. */ + png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; + + return 1; /* Ok */ + } + } + + return 0; /* no png_error possible! */ +} +#endif + #ifdef PNG_READ_BACKGROUND_SUPPORTED /* Handle alpha and tRNS via a background color */ void PNGFAPI -png_set_background_fixed(png_structp png_ptr, +png_set_background_fixed(png_structrp png_ptr, png_const_color_16p background_color, int background_gamma_code, int need_expand, png_fixed_point background_gamma) { png_debug(1, "in png_set_background_fixed"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL) return; if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) @@ -110,11 +141,10 @@ png_set_background_fixed(png_structp png_ptr, png_ptr->transformations &= ~PNG_ENCODE_ALPHA; png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; - png_memcpy(&(png_ptr->background), background_color, - png_sizeof(png_color_16)); + png_ptr->background = *background_color; png_ptr->background_gamma = background_gamma; png_ptr->background_gamma_type = (png_byte)(background_gamma_code); - if (need_expand) + if (need_expand != 0) png_ptr->transformations |= PNG_BACKGROUND_EXPAND; else png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; @@ -122,7 +152,7 @@ png_set_background_fixed(png_structp png_ptr, # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_background(png_structp png_ptr, +png_set_background(png_structrp png_ptr, png_const_color_16p background_color, int background_gamma_code, int need_expand, double background_gamma) { @@ -138,11 +168,11 @@ png_set_background(png_structp png_ptr, */ #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED void PNGAPI -png_set_scale_16(png_structp png_ptr) +png_set_scale_16(png_structrp png_ptr) { png_debug(1, "in png_set_scale_16"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= PNG_SCALE_16_TO_8; @@ -152,11 +182,11 @@ png_set_scale_16(png_structp png_ptr) #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED /* Chop 16-bit depth files to 8-bit depth */ void PNGAPI -png_set_strip_16(png_structp png_ptr) +png_set_strip_16(png_structrp png_ptr) { png_debug(1, "in png_set_strip_16"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= PNG_16_TO_8; @@ -165,11 +195,11 @@ png_set_strip_16(png_structp png_ptr) #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED void PNGAPI -png_set_strip_alpha(png_structp png_ptr) +png_set_strip_alpha(png_structrp png_ptr) { png_debug(1, "in png_set_strip_alpha"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= PNG_STRIP_ALPHA; @@ -178,7 +208,7 @@ png_set_strip_alpha(png_structp png_ptr) #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) static png_fixed_point -translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma, +translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, int is_screen) { /* Check for flag values. The main reason for having the old Mac value as a @@ -194,8 +224,10 @@ translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma, */ # ifdef PNG_READ_sRGB_SUPPORTED png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; +# else + PNG_UNUSED(png_ptr) # endif - if (is_screen) + if (is_screen != 0) output_gamma = PNG_GAMMA_sRGB; else output_gamma = PNG_GAMMA_sRGB_INVERSE; @@ -204,7 +236,7 @@ translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma, else if (output_gamma == PNG_GAMMA_MAC_18 || output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) { - if (is_screen) + if (is_screen != 0) output_gamma = PNG_GAMMA_MAC_OLD; else output_gamma = PNG_GAMMA_MAC_INVERSE; @@ -215,7 +247,7 @@ translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma, # ifdef PNG_FLOATING_POINT_SUPPORTED static png_fixed_point -convert_gamma_value(png_structp png_ptr, double output_gamma) +convert_gamma_value(png_structrp png_ptr, double output_gamma) { /* The following silently ignores cases where fixed point (times 100,000) * gamma values are passed to the floating point API. This is safe and it @@ -240,7 +272,7 @@ convert_gamma_value(png_structp png_ptr, double output_gamma) #ifdef PNG_READ_ALPHA_MODE_SUPPORTED void PNGFAPI -png_set_alpha_mode_fixed(png_structp png_ptr, int mode, +png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, png_fixed_point output_gamma) { int compose = 0; @@ -248,7 +280,7 @@ png_set_alpha_mode_fixed(png_structp png_ptr, int mode, png_debug(1, "in png_set_alpha_mode"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); @@ -320,8 +352,11 @@ png_set_alpha_mode_fixed(png_structp png_ptr, int mode, * the side effect that the gamma in a second call to png_set_alpha_mode will * be ignored.) */ - if (png_ptr->gamma == 0) - png_ptr->gamma = file_gamma; + if (png_ptr->colorspace.gamma == 0) + { + png_ptr->colorspace.gamma = file_gamma; + png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; + } /* But always set the output gamma: */ png_ptr->screen_gamma = output_gamma; @@ -329,28 +364,25 @@ png_set_alpha_mode_fixed(png_structp png_ptr, int mode, /* Finally, if pre-multiplying, set the background fields to achieve the * desired result. */ - if (compose) + if (compose != 0) { /* And obtain alpha pre-multiplication by composing on black: */ - png_memset(&png_ptr->background, 0, sizeof png_ptr->background); - png_ptr->background_gamma = png_ptr->gamma; /* just in case */ + memset(&png_ptr->background, 0, (sizeof png_ptr->background)); + png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; - if (png_ptr->transformations & PNG_COMPOSE) + if ((png_ptr->transformations & PNG_COMPOSE) != 0) png_error(png_ptr, "conflicting calls to set alpha mode and background"); png_ptr->transformations |= PNG_COMPOSE; } - - /* New API, make sure apps call the correct initializers: */ - png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; } # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma) +png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) { png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, output_gamma)); @@ -362,7 +394,7 @@ png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma) /* Dither file to 8-bit. Supply a palette, the current number * of elements in the palette, the maximum number of elements * allowed, and a histogram if possible. If the current number - * of colors is greater then the maximum number, the palette will be + * of colors is greater than the maximum number, the palette will be * modified to fit in the maximum number. "full_quantize" indicates * whether we need a quantizing cube set up for RGB images, or if we * simply are reducing the number of colors in a paletted image. @@ -370,31 +402,31 @@ png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma) typedef struct png_dsort_struct { - struct png_dsort_struct FAR * next; + struct png_dsort_struct * next; png_byte left; png_byte right; } png_dsort; -typedef png_dsort FAR * png_dsortp; -typedef png_dsort FAR * FAR * png_dsortpp; +typedef png_dsort * png_dsortp; +typedef png_dsort * * png_dsortpp; void PNGAPI -png_set_quantize(png_structp png_ptr, png_colorp palette, +png_set_quantize(png_structrp png_ptr, png_colorp palette, int num_palette, int maximum_colors, png_const_uint_16p histogram, int full_quantize) { png_debug(1, "in png_set_quantize"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= PNG_QUANTIZE; - if (!full_quantize) + if (full_quantize == 0) { int i; png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, - (png_uint_32)(num_palette * png_sizeof(png_byte))); + (png_uint_32)(num_palette * (sizeof (png_byte)))); for (i = 0; i < num_palette; i++) png_ptr->quantize_index[i] = (png_byte)i; } @@ -411,7 +443,7 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, /* Initialize an array to sort colors */ png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, - (png_uint_32)(num_palette * png_sizeof(png_byte))); + (png_uint_32)(num_palette * (sizeof (png_byte)))); /* Initialize the quantize_sort array */ for (i = 0; i < num_palette; i++) @@ -444,12 +476,12 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, } } - if (done) + if (done != 0) break; } /* Swap the palette around, and set up a table, if necessary */ - if (full_quantize) + if (full_quantize != 0) { int j = num_palette; @@ -545,9 +577,9 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, /* Initialize palette index arrays */ png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, - (png_uint_32)(num_palette * png_sizeof(png_byte))); + (png_uint_32)(num_palette * (sizeof (png_byte)))); png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, - (png_uint_32)(num_palette * png_sizeof(png_byte))); + (png_uint_32)(num_palette * (sizeof (png_byte)))); /* Initialize the sort array */ for (i = 0; i < num_palette; i++) @@ -557,7 +589,7 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, } hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 * - png_sizeof(png_dsortp))); + (sizeof (png_dsortp)))); num_new_palette = num_palette; @@ -587,7 +619,7 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, { t = (png_dsortp)png_malloc_warn(png_ptr, - (png_uint_32)(png_sizeof(png_dsort))); + (png_uint_32)(sizeof (png_dsort))); if (t == NULL) break; @@ -632,7 +664,7 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, num_new_palette--; palette[png_ptr->index_to_palette[j]] = palette[num_new_palette]; - if (!full_quantize) + if (full_quantize == 0) { int k; @@ -700,7 +732,7 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, } png_ptr->num_palette = (png_uint_16)num_palette; - if (full_quantize) + if (full_quantize != 0) { int i; png_bytep distance; @@ -712,12 +744,12 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, png_size_t num_entries = ((png_size_t)1 << total_bits); png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, - (png_uint_32)(num_entries * png_sizeof(png_byte))); + (png_uint_32)(num_entries * (sizeof (png_byte)))); distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * - png_sizeof(png_byte))); + (sizeof (png_byte)))); - png_memset(distance, 0xff, num_entries * png_sizeof(png_byte)); + memset(distance, 0xff, num_entries * (sizeof (png_byte))); for (i = 0; i < num_palette; i++) { @@ -762,23 +794,22 @@ png_set_quantize(png_structp png_ptr, png_colorp palette, png_free(png_ptr, distance); } } -#endif /* PNG_READ_QUANTIZE_SUPPORTED */ +#endif /* READ_QUANTIZE */ #ifdef PNG_READ_GAMMA_SUPPORTED void PNGFAPI -png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma, +png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, png_fixed_point file_gamma) { png_debug(1, "in png_set_gamma_fixed"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; /* New in libpng-1.5.4 - reserve particular negative values as flags. */ scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); -#if PNG_LIBPNG_VER >= 10600 /* Checking the gamma values for being >0 was added in 1.5.4 along with the * premultiplied alpha support; this actually hides an undocumented feature * of the previous implementation which allowed gamma processing to be @@ -787,31 +818,32 @@ png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma, * accept '0' for the gamma value it takes, because it isn't always used. * * Since this is an API change (albeit a very minor one that removes an - * undocumented API feature) it will not be made until libpng-1.6.0. + * undocumented API feature) the following checks were only enabled in + * libpng-1.6.0. */ if (file_gamma <= 0) png_error(png_ptr, "invalid file gamma in png_set_gamma"); if (scrn_gamma <= 0) png_error(png_ptr, "invalid screen gamma in png_set_gamma"); -#endif /* Set the gamma values unconditionally - this overrides the value in the PNG * file if a gAMA chunk was present. png_set_alpha_mode provides a * different, easier, way to default the file gamma. */ - png_ptr->gamma = file_gamma; + png_ptr->colorspace.gamma = file_gamma; + png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; png_ptr->screen_gamma = scrn_gamma; } # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) +png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) { png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), convert_gamma_value(png_ptr, file_gamma)); } -# endif /* FLOATING_POINT_SUPPORTED */ +# endif /* FLOATING_POINT */ #endif /* READ_GAMMA */ #ifdef PNG_READ_EXPAND_SUPPORTED @@ -820,15 +852,14 @@ png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) * to alpha channels. */ void PNGAPI -png_set_expand(png_structp png_ptr) +png_set_expand(png_structrp png_ptr) { png_debug(1, "in png_set_expand"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } /* GRR 19990627: the following three functions currently are identical @@ -851,90 +882,85 @@ png_set_expand(png_structp png_ptr) /* Expand paletted images to RGB. */ void PNGAPI -png_set_palette_to_rgb(png_structp png_ptr) +png_set_palette_to_rgb(png_structrp png_ptr) { png_debug(1, "in png_set_palette_to_rgb"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } /* Expand grayscale images of less than 8-bit depth to 8 bits. */ void PNGAPI -png_set_expand_gray_1_2_4_to_8(png_structp png_ptr) +png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) { png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= PNG_EXPAND; - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } - - /* Expand tRNS chunks to alpha channels. */ void PNGAPI -png_set_tRNS_to_alpha(png_structp png_ptr) +png_set_tRNS_to_alpha(png_structrp png_ptr) { png_debug(1, "in png_set_tRNS_to_alpha"); + if (png_rtran_ok(png_ptr, 0) == 0) + return; + png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; } -#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ +#endif /* READ_EXPAND */ #ifdef PNG_READ_EXPAND_16_SUPPORTED /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise * it may not work correctly.) */ void PNGAPI -png_set_expand_16(png_structp png_ptr) +png_set_expand_16(png_structrp png_ptr) { png_debug(1, "in png_set_expand_16"); - if (png_ptr == NULL) + if (png_rtran_ok(png_ptr, 0) == 0) return; png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; - - /* New API, make sure apps call the correct initializers: */ - png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; } #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED void PNGAPI -png_set_gray_to_rgb(png_structp png_ptr) +png_set_gray_to_rgb(png_structrp png_ptr) { png_debug(1, "in png_set_gray_to_rgb"); - if (png_ptr != NULL) - { - /* Because rgb must be 8 bits or more: */ - png_set_expand_gray_1_2_4_to_8(png_ptr); - png_ptr->transformations |= PNG_GRAY_TO_RGB; - png_ptr->flags &= ~PNG_FLAG_ROW_INIT; - } + if (png_rtran_ok(png_ptr, 0) == 0) + return; + + /* Because rgb must be 8 bits or more: */ + png_set_expand_gray_1_2_4_to_8(png_ptr); + png_ptr->transformations |= PNG_GRAY_TO_RGB; } #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED void PNGFAPI -png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, +png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, png_fixed_point red, png_fixed_point green) { png_debug(1, "in png_set_rgb_to_gray"); - if (png_ptr == NULL) + /* Need the IHDR here because of the check on color_type below. */ + /* TODO: fix this */ + if (png_rtran_ok(png_ptr, 1) == 0) return; - switch(error_action) + switch (error_action) { case PNG_ERROR_ACTION_NONE: png_ptr->transformations |= PNG_RGB_TO_GRAY; @@ -950,17 +976,20 @@ png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, default: png_error(png_ptr, "invalid error action to rgb_to_gray"); - break; } + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) #ifdef PNG_READ_EXPAND_SUPPORTED png_ptr->transformations |= PNG_EXPAND; #else { - png_warning(png_ptr, + /* Make this an error in 1.6 because otherwise the application may assume + * that it just worked and get a memory overwrite. + */ + png_error(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); - png_ptr->transformations &= ~PNG_RGB_TO_GRAY; + /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ } #endif { @@ -969,7 +998,7 @@ png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, png_uint_16 red_int, green_int; /* NOTE: this calculation does not round, but this behavior is retained - * for consistency, the inaccuracy is very small. The code here always + * for consistency; the inaccuracy is very small. The code here always * overwrites the coefficients, regardless of whether they have been * defaulted or set already. */ @@ -984,7 +1013,7 @@ png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, else { if (red >= 0 && green >= 0) - png_warning(png_ptr, + png_app_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); /* Use the defaults, from the cHRM chunk if set, else the historical @@ -1010,31 +1039,25 @@ png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, */ void PNGAPI -png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, +png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, double green) { - if (png_ptr == NULL) - return; - png_set_rgb_to_gray_fixed(png_ptr, error_action, png_fixed(png_ptr, red, "rgb to gray red coefficient"), png_fixed(png_ptr, green, "rgb to gray green coefficient")); } #endif /* FLOATING POINT */ -#endif +#endif /* RGB_TO_GRAY */ #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) void PNGAPI -png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr +png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr read_user_transform_fn) { png_debug(1, "in png_set_read_user_transform_fn"); - if (png_ptr == NULL) - return; - #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED png_ptr->transformations |= PNG_USER_TRANSFORM; png_ptr->read_user_transform_fn = read_user_transform_fn; @@ -1068,13 +1091,13 @@ png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) * the palette. */ -/*For the moment 'png_init_palette_transformations' and +/* For the moment 'png_init_palette_transformations' and * 'png_init_rgb_transformations' only do some flag canceling optimizations. * The intent is that these two routines should have palette or rgb operations * extracted from 'png_init_read_transformations'. */ static void /* PRIVATE */ -png_init_palette_transformations(png_structp png_ptr) +png_init_palette_transformations(png_structrp png_ptr) { /* Called to handle the (input) palette case. In png_do_read_transformations * the first step is to expand the palette if requested, so this code must @@ -1093,25 +1116,31 @@ png_init_palette_transformations(png_structp png_ptr) /* Ignore if all the entries are opaque (unlikely!) */ for (i=0; inum_trans; ++i) + { if (png_ptr->trans_alpha[i] == 255) continue; else if (png_ptr->trans_alpha[i] == 0) input_has_transparency = 1; else + { + input_has_transparency = 1; input_has_alpha = 1; + break; + } + } } /* If no alpha we can optimize. */ - if (!input_has_alpha) + if (input_has_alpha == 0) { /* Any alpha means background and associative alpha processing is - * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA + * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA * and ENCODE_ALPHA are irrelevant. */ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; - if (!input_has_transparency) + if (input_has_transparency == 0) png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); } @@ -1124,8 +1153,8 @@ png_init_palette_transformations(png_structp png_ptr) /* The following code cannot be entered in the alpha pre-multiplication case * because PNG_BACKGROUND_EXPAND is cancelled below. */ - if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && - (png_ptr->transformations & PNG_EXPAND)) + if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && + (png_ptr->transformations & PNG_EXPAND) != 0) { { png_ptr->background.red = @@ -1136,9 +1165,9 @@ png_init_palette_transformations(png_structp png_ptr) png_ptr->palette[png_ptr->background.index].blue; #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_INVERT_ALPHA) + if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) { - if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) + if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) { /* Invert the alpha channel (in tRNS) unless the pixels are * going to be expanded, in which case leave it for later @@ -1150,14 +1179,14 @@ png_init_palette_transformations(png_structp png_ptr) png_ptr->trans_alpha[i]); } } -#endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */ +#endif /* READ_INVERT_ALPHA */ } } /* background expand and (therefore) no alpha association. */ -#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ +#endif /* READ_EXPAND && READ_BACKGROUND */ } static void /* PRIVATE */ -png_init_rgb_transformations(png_structp png_ptr) +png_init_rgb_transformations(png_structrp png_ptr) { /* Added to libpng-1.5.4: check the color type to determine whether there * is any alpha or transparency in the image and simply cancel the @@ -1167,10 +1196,10 @@ png_init_rgb_transformations(png_structp png_ptr) int input_has_transparency = png_ptr->num_trans > 0; /* If no alpha we can optimize. */ - if (!input_has_alpha) + if (input_has_alpha == 0) { /* Any alpha means background and associative alpha processing is - * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA + * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA * and ENCODE_ALPHA are irrelevant. */ # ifdef PNG_READ_ALPHA_MODE_SUPPORTED @@ -1178,7 +1207,7 @@ png_init_rgb_transformations(png_structp png_ptr) png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; # endif - if (!input_has_transparency) + if (input_has_transparency == 0) png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); } @@ -1191,9 +1220,9 @@ png_init_rgb_transformations(png_structp png_ptr) /* The following code cannot be entered in the alpha pre-multiplication case * because PNG_BACKGROUND_EXPAND is cancelled below. */ - if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && - (png_ptr->transformations & PNG_EXPAND) && - !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) + if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && + (png_ptr->transformations & PNG_EXPAND) != 0 && + (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* i.e., GRAY or GRAY_ALPHA */ { { @@ -1221,7 +1250,7 @@ png_init_rgb_transformations(png_structp png_ptr) default: case 8: - /* Already 8 bits, fall through */ + /* FALL THROUGH (Already 8 bits) */ case 16: /* Already a full 16 bits */ @@ -1231,18 +1260,18 @@ png_init_rgb_transformations(png_structp png_ptr) png_ptr->background.red = png_ptr->background.green = png_ptr->background.blue = (png_uint_16)gray; - if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) + if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) { png_ptr->trans_color.red = png_ptr->trans_color.green = png_ptr->trans_color.blue = (png_uint_16)trans_gray; } } } /* background expand and (therefore) no alpha association. */ -#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ +#endif /* READ_EXPAND && READ_BACKGROUND */ } void /* PRIVATE */ -png_init_read_transformations(png_structp png_ptr) +png_init_read_transformations(png_structrp png_ptr) { png_debug(1, "in png_init_read_transformations"); @@ -1267,17 +1296,17 @@ png_init_read_transformations(png_structp png_ptr) */ int gamma_correction = 0; - if (png_ptr->gamma != 0) /* has been set */ + if (png_ptr->colorspace.gamma != 0) /* has been set */ { if (png_ptr->screen_gamma != 0) /* screen set too */ - gamma_correction = png_gamma_threshold(png_ptr->gamma, + gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, png_ptr->screen_gamma); else /* Assume the output matches the input; a long time default behavior * of libpng, although the standard has nothing to say about this. */ - png_ptr->screen_gamma = png_reciprocal(png_ptr->gamma); + png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); } else if (png_ptr->screen_gamma != 0) @@ -1286,7 +1315,7 @@ png_init_read_transformations(png_structp png_ptr) * png_set_alpha_mode (even if the alpha handling mode isn't required * or isn't changed from the default.) */ - png_ptr->gamma = png_reciprocal(png_ptr->screen_gamma); + png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); else /* neither are set */ /* Just in case the following prevents any processing - file and screen @@ -1294,7 +1323,10 @@ png_init_read_transformations(png_structp png_ptr) * third gamma value other than png_set_background with 'UNIQUE', and, * prior to 1.5.4 */ - png_ptr->screen_gamma = png_ptr->gamma = PNG_FP_1; + png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; + + /* We have a gamma value now. */ + png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; /* Now turn the gamma transformation on or off as appropriate. Notice * that PNG_GAMMA just refers to the file->screen correction. Alpha @@ -1304,7 +1336,7 @@ png_init_read_transformations(png_structp png_ptr) * the code immediately below if the transform can be handled outside the * row loop. */ - if (gamma_correction) + if (gamma_correction != 0) png_ptr->transformations |= PNG_GAMMA; else @@ -1313,7 +1345,7 @@ png_init_read_transformations(png_structp png_ptr) #endif /* Certain transformations have the effect of preventing other - * transformations that happen afterward in png_do_read_transformations, + * transformations that happen afterward in png_do_read_transformations; * resolve the interdependencies here. From the code of * png_do_read_transformations the order is: * @@ -1331,19 +1363,19 @@ png_init_read_transformations(png_structp png_ptr) * 12) PNG_EXPAND_16 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY * 14) PNG_INVERT_MONO - * 15) PNG_SHIFT - * 16) PNG_PACK - * 17) PNG_BGR - * 18) PNG_PACKSWAP - * 19) PNG_FILLER (includes PNG_ADD_ALPHA) - * 20) PNG_INVERT_ALPHA + * 15) PNG_INVERT_ALPHA + * 16) PNG_SHIFT + * 17) PNG_PACK + * 18) PNG_BGR + * 19) PNG_PACKSWAP + * 20) PNG_FILLER (includes PNG_ADD_ALPHA) * 21) PNG_SWAP_ALPHA * 22) PNG_SWAP_BYTES * 23) PNG_USER_TRANSFORM [must be last] */ #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED - if ((png_ptr->transformations & PNG_STRIP_ALPHA) && - !(png_ptr->transformations & PNG_COMPOSE)) + if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && + (png_ptr->transformations & PNG_COMPOSE) == 0) { /* Stripping the alpha channel happens immediately after the 'expand' * transformations, before all other transformation, so it cancels out @@ -1369,16 +1401,23 @@ png_init_read_transformations(png_structp png_ptr) /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA * settings will have no effect. */ - if (!png_gamma_significant(png_ptr->screen_gamma)) + if (png_gamma_significant(png_ptr->screen_gamma) == 0) { png_ptr->transformations &= ~PNG_ENCODE_ALPHA; png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; } #endif -#if defined(PNG_READ_EXPAND_SUPPORTED) && \ - defined(PNG_READ_BACKGROUND_SUPPORTED) && \ - defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED + /* Make sure the coefficients for the rgb to gray conversion are set + * appropriately. + */ + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) + png_colorspace_set_rgb_coefficients(png_ptr); +#endif + +#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED +#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) /* Detect gray background and attempt to enable optimization for * gray --> RGB case. * @@ -1394,23 +1433,23 @@ png_init_read_transformations(png_structp png_ptr) * png_set_background, along with the bit depth, then the code has a record * of exactly what color space the background is currently in. */ - if (png_ptr->transformations & PNG_BACKGROUND_EXPAND) + if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0) { /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if * the file was grayscale the background value is gray. */ - if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; } - else if (png_ptr->transformations & PNG_COMPOSE) + else if ((png_ptr->transformations & PNG_COMPOSE) != 0) { /* PNG_COMPOSE: png_set_background was called with need_expand false, * so the color is in the color space of the output or png_set_alpha_mode * was called and the color is black. Ignore RGB_TO_GRAY because that * happens before GRAY_TO_RGB. */ - if (png_ptr->transformations & PNG_GRAY_TO_RGB) + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) { if (png_ptr->background.red == png_ptr->background.green && png_ptr->background.red == png_ptr->background.blue) @@ -1420,7 +1459,8 @@ png_init_read_transformations(png_structp png_ptr) } } } -#endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED (etc) */ +#endif /* READ_EXPAND && READ_BACKGROUND */ +#endif /* READ_GRAY_TO_RGB */ /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations * can be performed directly on the palette, and some (such as rgb to gray) @@ -1441,10 +1481,10 @@ png_init_read_transformations(png_structp png_ptr) #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ defined(PNG_READ_EXPAND_16_SUPPORTED) - if ((png_ptr->transformations & PNG_EXPAND_16) && - (png_ptr->transformations & PNG_COMPOSE) && - !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && - png_ptr->bit_depth != 16) + if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && + (png_ptr->transformations & PNG_COMPOSE) != 0 && + (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && + png_ptr->bit_depth != 16) { /* TODO: fix this. Because the expand_16 operation is after the compose * handling the background color must be 8, not 16, bits deep, but the @@ -1456,22 +1496,22 @@ png_init_read_transformations(png_structp png_ptr) * NOTE: this discards the low 16 bits of the user supplied background * color, but until expand_16 works properly there is no choice! */ -# define CHOP(x) (x)=((png_uint_16)(((png_uint_32)(x)*255+32895) >> 16)) +# define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) CHOP(png_ptr->background.red); CHOP(png_ptr->background.green); CHOP(png_ptr->background.blue); CHOP(png_ptr->background.gray); # undef CHOP } -#endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */ +#endif /* READ_BACKGROUND && READ_EXPAND_16 */ #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) - if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) && - (png_ptr->transformations & PNG_COMPOSE) && - !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && - png_ptr->bit_depth == 16) + if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 && + (png_ptr->transformations & PNG_COMPOSE) != 0 && + (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && + png_ptr->bit_depth == 16) { /* On the other hand, if a 16-bit file is to be reduced to 8-bits per * component this will also happen after PNG_COMPOSE and so the background @@ -1514,25 +1554,24 @@ png_init_read_transformations(png_structp png_ptr) * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the * tables. */ - if ((png_ptr->transformations & PNG_GAMMA) - || ((png_ptr->transformations & PNG_RGB_TO_GRAY) - && (png_gamma_significant(png_ptr->gamma) || - png_gamma_significant(png_ptr->screen_gamma))) - || ((png_ptr->transformations & PNG_COMPOSE) - && (png_gamma_significant(png_ptr->gamma) - || png_gamma_significant(png_ptr->screen_gamma) + if ((png_ptr->transformations & PNG_GAMMA) != 0 || + ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 && + (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || + png_gamma_significant(png_ptr->screen_gamma) != 0)) || + ((png_ptr->transformations & PNG_COMPOSE) != 0 && + (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || + png_gamma_significant(png_ptr->screen_gamma) != 0 # ifdef PNG_READ_BACKGROUND_SUPPORTED - || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE - && png_gamma_significant(png_ptr->background_gamma)) + || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE && + png_gamma_significant(png_ptr->background_gamma) != 0) # endif - )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) - && png_gamma_significant(png_ptr->screen_gamma)) - ) + )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && + png_gamma_significant(png_ptr->screen_gamma) != 0)) { png_build_gamma_table(png_ptr, png_ptr->bit_depth); #ifdef PNG_READ_BACKGROUND_SUPPORTED - if (png_ptr->transformations & PNG_COMPOSE) + if ((png_ptr->transformations & PNG_COMPOSE) != 0) { /* Issue a warning about this combination: because RGB_TO_GRAY is * optimized to do the gamma transform if present yet do_background has @@ -1540,11 +1579,11 @@ png_init_read_transformations(png_structp png_ptr) * double-gamma-correction happens. This is true in all versions of * libpng to date. */ - if (png_ptr->transformations & PNG_RGB_TO_GRAY) + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) png_warning(png_ptr, "libpng does not support gamma+background+rgb_to_gray"); - if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0) { /* We don't get to here unless there is a tRNS chunk with non-opaque * entries - see the checking code at the start of this function. @@ -1576,8 +1615,8 @@ png_init_read_transformations(png_structp png_ptr) break; case PNG_BACKGROUND_GAMMA_FILE: - g = png_reciprocal(png_ptr->gamma); - gs = png_reciprocal2(png_ptr->gamma, + g = png_reciprocal(png_ptr->colorspace.gamma); + gs = png_reciprocal2(png_ptr->colorspace.gamma, png_ptr->screen_gamma); break; @@ -1592,7 +1631,7 @@ png_init_read_transformations(png_structp png_ptr) break; } - if (png_gamma_significant(gs)) + if (png_gamma_significant(gs) != 0) { back.red = png_gamma_8bit_correct(png_ptr->background.red, gs); @@ -1609,7 +1648,7 @@ png_init_read_transformations(png_structp png_ptr) back.blue = (png_byte)png_ptr->background.blue; } - if (png_gamma_significant(g)) + if (png_gamma_significant(g) != 0) { back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g); @@ -1685,8 +1724,9 @@ png_init_read_transformations(png_structp png_ptr) break; case PNG_BACKGROUND_GAMMA_FILE: - g = png_reciprocal(png_ptr->gamma); - gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma); + g = png_reciprocal(png_ptr->colorspace.gamma); + gs = png_reciprocal2(png_ptr->colorspace.gamma, + png_ptr->screen_gamma); break; case PNG_BACKGROUND_GAMMA_UNIQUE: @@ -1702,11 +1742,11 @@ png_init_read_transformations(png_structp png_ptr) g_sig = png_gamma_significant(g); gs_sig = png_gamma_significant(gs); - if (g_sig) + if (g_sig != 0) png_ptr->background_1.gray = png_gamma_correct(png_ptr, png_ptr->background.gray, g); - if (gs_sig) + if (gs_sig != 0) png_ptr->background.gray = png_gamma_correct(png_ptr, png_ptr->background.gray, gs); @@ -1715,7 +1755,7 @@ png_init_read_transformations(png_structp png_ptr) (png_ptr->background.red != png_ptr->background.gray)) { /* RGB or RGBA with color background */ - if (g_sig) + if (g_sig != 0) { png_ptr->background_1.red = png_gamma_correct(png_ptr, png_ptr->background.red, g); @@ -1727,7 +1767,7 @@ png_init_read_transformations(png_structp png_ptr) png_ptr->background.blue, g); } - if (gs_sig) + if (gs_sig != 0) { png_ptr->background.red = png_gamma_correct(png_ptr, png_ptr->background.red, gs); @@ -1757,7 +1797,7 @@ png_init_read_transformations(png_structp png_ptr) else /* Transformation does not include PNG_BACKGROUND */ -#endif /* PNG_READ_BACKGROUND_SUPPORTED */ +#endif /* READ_BACKGROUND */ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED /* RGB_TO_GRAY needs to have non-gamma-corrected values! */ @@ -1787,11 +1827,11 @@ png_init_read_transformations(png_structp png_ptr) #ifdef PNG_READ_BACKGROUND_SUPPORTED else #endif -#endif /* PNG_READ_GAMMA_SUPPORTED */ +#endif /* READ_GAMMA */ #ifdef PNG_READ_BACKGROUND_SUPPORTED /* No GAMMA transformation (see the hanging else 4 lines above) */ - if ((png_ptr->transformations & PNG_COMPOSE) && + if ((png_ptr->transformations & PNG_COMPOSE) != 0 && (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) { int i; @@ -1826,11 +1866,11 @@ png_init_read_transformations(png_structp png_ptr) png_ptr->transformations &= ~PNG_COMPOSE; } -#endif /* PNG_READ_BACKGROUND_SUPPORTED */ +#endif /* READ_BACKGROUND */ #ifdef PNG_READ_SHIFT_SUPPORTED - if ((png_ptr->transformations & PNG_SHIFT) && - !(png_ptr->transformations & PNG_EXPAND) && + if ((png_ptr->transformations & PNG_SHIFT) != 0 && + (png_ptr->transformations & PNG_EXPAND) == 0 && (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) { int i; @@ -1843,33 +1883,36 @@ png_init_read_transformations(png_structp png_ptr) * the number of significant bits is 0 then no shift is done (this is an * error condition which is silently ignored.) */ - if (shift > 0 && shift < 8) for (i=0; ipalette[i].red; + if (shift > 0 && shift < 8) + for (i=0; ipalette[i].red; - component >>= shift; - png_ptr->palette[i].red = (png_byte)component; - } + component >>= shift; + png_ptr->palette[i].red = (png_byte)component; + } shift = 8 - png_ptr->sig_bit.green; - if (shift > 0 && shift < 8) for (i=0; ipalette[i].green; + if (shift > 0 && shift < 8) + for (i=0; ipalette[i].green; - component >>= shift; - png_ptr->palette[i].green = (png_byte)component; - } + component >>= shift; + png_ptr->palette[i].green = (png_byte)component; + } shift = 8 - png_ptr->sig_bit.blue; - if (shift > 0 && shift < 8) for (i=0; ipalette[i].blue; + if (shift > 0 && shift < 8) + for (i=0; ipalette[i].blue; - component >>= shift; - png_ptr->palette[i].blue = (png_byte)component; - } + component >>= shift; + png_ptr->palette[i].blue = (png_byte)component; + } } -#endif /* PNG_READ_SHIFT_SUPPORTED */ +#endif /* READ_SHIFT */ } /* Modify the info structure to reflect the transformations. The @@ -1877,12 +1920,12 @@ png_init_read_transformations(png_structp png_ptr) * assuming the transformations result in valid PNG data. */ void /* PRIVATE */ -png_read_transform_info(png_structp png_ptr, png_infop info_ptr) +png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) { png_debug(1, "in png_read_transform_info"); #ifdef PNG_READ_EXPAND_SUPPORTED - if (png_ptr->transformations & PNG_EXPAND) + if ((png_ptr->transformations & PNG_EXPAND) != 0) { if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { @@ -1898,12 +1941,15 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) info_ptr->bit_depth = 8; info_ptr->num_trans = 0; + + if (png_ptr->palette == NULL) + png_error (png_ptr, "Palette is NULL in indexed image"); } else { - if (png_ptr->num_trans) + if (png_ptr->num_trans != 0) { - if (png_ptr->transformations & PNG_EXPAND_tRNS) + if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0) info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; } if (info_ptr->bit_depth < 8) @@ -1919,7 +1965,7 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) /* The following is almost certainly wrong unless the background value is in * the screen space! */ - if (png_ptr->transformations & PNG_COMPOSE) + if ((png_ptr->transformations & PNG_COMPOSE) != 0) info_ptr->background = png_ptr->background; #endif @@ -1928,25 +1974,29 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) * however it seems that the code in png_init_read_transformations, which has * been called before this from png_read_update_info->png_read_start_row * sometimes does the gamma transform and cancels the flag. + * + * TODO: this looks wrong; the info_ptr should end up with a gamma equal to + * the screen_gamma value. The following probably results in weirdness if + * the info_ptr is used by the app after the rows have been read. */ - info_ptr->gamma = png_ptr->gamma; + info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; #endif if (info_ptr->bit_depth == 16) { # ifdef PNG_READ_16BIT_SUPPORTED # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED - if (png_ptr->transformations & PNG_SCALE_16_TO_8) + if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) info_ptr->bit_depth = 8; # endif # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED - if (png_ptr->transformations & PNG_16_TO_8) + if ((png_ptr->transformations & PNG_16_TO_8) != 0) info_ptr->bit_depth = 8; # endif # else - /* No 16 bit support: force chopping 16-bit input down to 8, in this case + /* No 16-bit support: force chopping 16-bit input down to 8, in this case * the app program can chose if both APIs are available by setting the * correct scaling to use. */ @@ -1967,27 +2017,27 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) CONFIGURATION ERROR: you must enable at least one 16 to 8 method # endif # endif -#endif /* !READ_16BIT_SUPPORTED */ +#endif /* !READ_16BIT */ } #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED - if (png_ptr->transformations & PNG_GRAY_TO_RGB) + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) info_ptr->color_type = (png_byte)(info_ptr->color_type | PNG_COLOR_MASK_COLOR); #endif #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED - if (png_ptr->transformations & PNG_RGB_TO_GRAY) + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) info_ptr->color_type = (png_byte)(info_ptr->color_type & ~PNG_COLOR_MASK_COLOR); #endif #ifdef PNG_READ_QUANTIZE_SUPPORTED - if (png_ptr->transformations & PNG_QUANTIZE) + if ((png_ptr->transformations & PNG_QUANTIZE) != 0) { if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && - png_ptr->palette_lookup && info_ptr->bit_depth == 8) + png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8) { info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; } @@ -1995,29 +2045,31 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) #endif #ifdef PNG_READ_EXPAND_16_SUPPORTED - if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 && - info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) + if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && + info_ptr->bit_depth == 8 && + info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) { info_ptr->bit_depth = 16; } #endif #ifdef PNG_READ_PACK_SUPPORTED - if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) + if ((png_ptr->transformations & PNG_PACK) != 0 && + (info_ptr->bit_depth < 8)) info_ptr->bit_depth = 8; #endif if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) info_ptr->channels = 1; - else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) + else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) info_ptr->channels = 3; else info_ptr->channels = 1; #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_STRIP_ALPHA) + if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0) { info_ptr->color_type = (png_byte)(info_ptr->color_type & ~PNG_COLOR_MASK_ALPHA); @@ -2025,30 +2077,30 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) } #endif - if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) + if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) info_ptr->channels++; #ifdef PNG_READ_FILLER_SUPPORTED /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ - if ((png_ptr->transformations & PNG_FILLER) && - ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || - (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) + if ((png_ptr->transformations & PNG_FILLER) != 0 && + (info_ptr->color_type == PNG_COLOR_TYPE_RGB || + info_ptr->color_type == PNG_COLOR_TYPE_GRAY)) { info_ptr->channels++; /* If adding a true alpha channel not just filler */ - if (png_ptr->transformations & PNG_ADD_ALPHA) + if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0) info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; } #endif #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ defined(PNG_READ_USER_TRANSFORM_SUPPORTED) - if (png_ptr->transformations & PNG_USER_TRANSFORM) + if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) { - if (info_ptr->bit_depth < png_ptr->user_transform_depth) + if (png_ptr->user_transform_depth != 0) info_ptr->bit_depth = png_ptr->user_transform_depth; - if (info_ptr->channels < png_ptr->user_transform_channels) + if (png_ptr->user_transform_channels != 0) info_ptr->channels = png_ptr->user_transform_channels; } #endif @@ -2067,307 +2119,11 @@ defined(PNG_READ_USER_TRANSFORM_SUPPORTED) png_ptr->info_rowbytes = info_ptr->rowbytes; #ifndef PNG_READ_EXPAND_SUPPORTED - if (png_ptr) + if (png_ptr != NULL) return; #endif } -/* Transform the row. The order of transformations is significant, - * and is very touchy. If you add a transformation, take care to - * decide how it fits in with the other transformations here. - */ -void /* PRIVATE */ -png_do_read_transformations(png_structp png_ptr, png_row_infop row_info) -{ - png_debug(1, "in png_do_read_transformations"); - - if (png_ptr->row_buf == NULL) - { - /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this - * error is incredibly rare and incredibly easy to debug without this - * information. - */ - png_error(png_ptr, "NULL row buffer"); - } - - /* The following is debugging; prior to 1.5.4 the code was never compiled in; - * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro - * PNG_WARN_UNINITIALIZED_ROW removed. In 1.5 the new flag is set only for - * selected new APIs to ensure that there is no API change. - */ - if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && - !(png_ptr->flags & PNG_FLAG_ROW_INIT)) - { - /* Application has failed to call either png_read_start_image() or - * png_read_update_info() after setting transforms that expand pixels. - * This check added to libpng-1.2.19 (but not enabled until 1.5.4). - */ - png_error(png_ptr, "Uninitialized row"); - } - -#ifdef PNG_READ_EXPAND_SUPPORTED - if (png_ptr->transformations & PNG_EXPAND) - { - if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) - { - png_do_expand_palette(row_info, png_ptr->row_buf + 1, - png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); - } - - else - { - if (png_ptr->num_trans && - (png_ptr->transformations & PNG_EXPAND_tRNS)) - png_do_expand(row_info, png_ptr->row_buf + 1, - &(png_ptr->trans_color)); - - else - png_do_expand(row_info, png_ptr->row_buf + 1, - NULL); - } - } -#endif - -#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED - if ((png_ptr->transformations & PNG_STRIP_ALPHA) && - !(png_ptr->transformations & PNG_COMPOSE) && - (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || - row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) - png_do_strip_channel(row_info, png_ptr->row_buf + 1, - 0 /* at_start == false, because SWAP_ALPHA happens later */); -#endif - -#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED - if (png_ptr->transformations & PNG_RGB_TO_GRAY) - { - int rgb_error = - png_do_rgb_to_gray(png_ptr, row_info, - png_ptr->row_buf + 1); - - if (rgb_error) - { - png_ptr->rgb_to_gray_status=1; - if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == - PNG_RGB_TO_GRAY_WARN) - png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); - - if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == - PNG_RGB_TO_GRAY_ERR) - png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); - } - } -#endif - -/* From Andreas Dilger e-mail to png-implement, 26 March 1998: - * - * In most cases, the "simple transparency" should be done prior to doing - * gray-to-RGB, or you will have to test 3x as many bytes to check if a - * pixel is transparent. You would also need to make sure that the - * transparency information is upgraded to RGB. - * - * To summarize, the current flow is: - * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite - * with background "in place" if transparent, - * convert to RGB if necessary - * - Gray + alpha -> composite with gray background and remove alpha bytes, - * convert to RGB if necessary - * - * To support RGB backgrounds for gray images we need: - * - Gray + simple transparency -> convert to RGB + simple transparency, - * compare 3 or 6 bytes and composite with - * background "in place" if transparent - * (3x compare/pixel compared to doing - * composite with gray bkgrnd) - * - Gray + alpha -> convert to RGB + alpha, composite with background and - * remove alpha bytes (3x float - * operations/pixel compared with composite - * on gray background) - * - * Greg's change will do this. The reason it wasn't done before is for - * performance, as this increases the per-pixel operations. If we would check - * in advance if the background was gray or RGB, and position the gray-to-RGB - * transform appropriately, then it would save a lot of work/time. - */ - -#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED - /* If gray -> RGB, do so now only if background is non-gray; else do later - * for performance reasons - */ - if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && - !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) - png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); -#endif - -#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\ - (defined PNG_READ_ALPHA_MODE_SUPPORTED) - if (png_ptr->transformations & PNG_COMPOSE) - png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); -#endif - -#ifdef PNG_READ_GAMMA_SUPPORTED - if ((png_ptr->transformations & PNG_GAMMA) && -#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED - /* Because RGB_TO_GRAY does the gamma transform. */ - !(png_ptr->transformations & PNG_RGB_TO_GRAY) && -#endif -#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\ - (defined PNG_READ_ALPHA_MODE_SUPPORTED) - /* Because PNG_COMPOSE does the gamma transform if there is something to - * do (if there is an alpha channel or transparency.) - */ - !((png_ptr->transformations & PNG_COMPOSE) && - ((png_ptr->num_trans != 0) || - (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && -#endif - /* Because png_init_read_transformations transforms the palette, unless - * RGB_TO_GRAY will do the transform. - */ - (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) - png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); -#endif - -#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED - if ((png_ptr->transformations & PNG_STRIP_ALPHA) && - (png_ptr->transformations & PNG_COMPOSE) && - (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || - row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) - png_do_strip_channel(row_info, png_ptr->row_buf + 1, - 0 /* at_start == false, because SWAP_ALPHA happens later */); -#endif - -#ifdef PNG_READ_ALPHA_MODE_SUPPORTED - if ((png_ptr->transformations & PNG_ENCODE_ALPHA) && - (row_info->color_type & PNG_COLOR_MASK_ALPHA)) - png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); -#endif - -#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED - if (png_ptr->transformations & PNG_SCALE_16_TO_8) - png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED - /* There is no harm in doing both of these because only one has any effect, - * by putting the 'scale' option first if the app asks for scale (either by - * calling the API or in a TRANSFORM flag) this is what happens. - */ - if (png_ptr->transformations & PNG_16_TO_8) - png_do_chop(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_QUANTIZE_SUPPORTED - if (png_ptr->transformations & PNG_QUANTIZE) - { - png_do_quantize(row_info, png_ptr->row_buf + 1, - png_ptr->palette_lookup, png_ptr->quantize_index); - - if (row_info->rowbytes == 0) - png_error(png_ptr, "png_do_quantize returned rowbytes=0"); - } -#endif /* PNG_READ_QUANTIZE_SUPPORTED */ - -#ifdef PNG_READ_EXPAND_16_SUPPORTED - /* Do the expansion now, after all the arithmetic has been done. Notice - * that previous transformations can handle the PNG_EXPAND_16 flag if this - * is efficient (particularly true in the case of gamma correction, where - * better accuracy results faster!) - */ - if (png_ptr->transformations & PNG_EXPAND_16) - png_do_expand_16(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED - /* NOTE: moved here in 1.5.4 (from much later in this list.) */ - if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && - (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) - png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_INVERT_SUPPORTED - if (png_ptr->transformations & PNG_INVERT_MONO) - png_do_invert(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_SHIFT_SUPPORTED - if (png_ptr->transformations & PNG_SHIFT) - png_do_unshift(row_info, png_ptr->row_buf + 1, - &(png_ptr->shift)); -#endif - -#ifdef PNG_READ_PACK_SUPPORTED - if (png_ptr->transformations & PNG_PACK) - png_do_unpack(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED - /* Added at libpng-1.5.10 */ - if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && - png_ptr->num_palette_max >= 0) - png_do_check_palette_indexes(png_ptr, row_info); -#endif - -#ifdef PNG_READ_BGR_SUPPORTED - if (png_ptr->transformations & PNG_BGR) - png_do_bgr(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_PACKSWAP_SUPPORTED - if (png_ptr->transformations & PNG_PACKSWAP) - png_do_packswap(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_FILLER_SUPPORTED - if (png_ptr->transformations & PNG_FILLER) - png_do_read_filler(row_info, png_ptr->row_buf + 1, - (png_uint_32)png_ptr->filler, png_ptr->flags); -#endif - -#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_INVERT_ALPHA) - png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_SWAP_ALPHA) - png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_READ_16BIT_SUPPORTED -#ifdef PNG_READ_SWAP_SUPPORTED - if (png_ptr->transformations & PNG_SWAP_BYTES) - png_do_swap(row_info, png_ptr->row_buf + 1); -#endif -#endif - -#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED - if (png_ptr->transformations & PNG_USER_TRANSFORM) - { - if (png_ptr->read_user_transform_fn != NULL) - (*(png_ptr->read_user_transform_fn)) /* User read transform function */ - (png_ptr, /* png_ptr */ - row_info, /* row_info: */ - /* png_uint_32 width; width of row */ - /* png_size_t rowbytes; number of bytes in row */ - /* png_byte color_type; color type of pixels */ - /* png_byte bit_depth; bit depth of samples */ - /* png_byte channels; number of channels (1-4) */ - /* png_byte pixel_depth; bits per pixel (depth*channels) */ - png_ptr->row_buf + 1); /* start of pixel data for row */ -#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED - if (png_ptr->user_transform_depth) - row_info->bit_depth = png_ptr->user_transform_depth; - - if (png_ptr->user_transform_channels) - row_info->channels = png_ptr->user_transform_channels; -#endif - row_info->pixel_depth = (png_byte)(row_info->bit_depth * - row_info->channels); - - row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); - } -#endif -} - #ifdef PNG_READ_PACK_SUPPORTED /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, * without changing the actual values. Thus, if you had a row with @@ -2375,7 +2131,7 @@ png_do_read_transformations(png_structp png_ptr, png_row_infop row_info) * the numbers 0 or 1. If you would rather they contain 0 and 255, use * png_do_shift() after this. */ -void /* PRIVATE */ +static void png_do_unpack(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_unpack"); @@ -2473,7 +2229,7 @@ png_do_unpack(png_row_infop row_info, png_bytep row) * a row of bit depth 8, but only 5 are significant, this will shift * the values back to 0 through 31. */ -void /* PRIVATE */ +static void png_do_unshift(png_row_infop row_info, png_bytep row, png_const_color_8p sig_bits) { @@ -2490,7 +2246,7 @@ png_do_unshift(png_row_infop row_info, png_bytep row, int channels = 0; int bit_depth = row_info->bit_depth; - if (color_type & PNG_COLOR_MASK_COLOR) + if ((color_type & PNG_COLOR_MASK_COLOR) != 0) { shift[channels++] = bit_depth - sig_bits->red; shift[channels++] = bit_depth - sig_bits->green; @@ -2502,7 +2258,7 @@ png_do_unshift(png_row_infop row_info, png_bytep row, shift[channels++] = bit_depth - sig_bits->gray; } - if (color_type & PNG_COLOR_MASK_ALPHA) + if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) { shift[channels++] = bit_depth - sig_bits->alpha; } @@ -2522,7 +2278,7 @@ png_do_unshift(png_row_infop row_info, png_bytep row, have_shift = 1; } - if (!have_shift) + if (have_shift == 0) return; } @@ -2600,7 +2356,7 @@ png_do_unshift(png_row_infop row_info, png_bytep row, if (++channel >= channels) channel = 0; *bp++ = (png_byte)(value >> 8); - *bp++ = (png_byte)(value & 0xff); + *bp++ = (png_byte)value; } break; } @@ -2612,7 +2368,7 @@ png_do_unshift(png_row_infop row_info, png_bytep row, #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED /* Scale rows of bit depth 16 down to 8 accurately */ -void /* PRIVATE */ +static void png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_scale_16_to_8"); @@ -2625,8 +2381,8 @@ png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) while (sp < ep) { - /* The input is an array of 16 bit components, these must be scaled to - * 8 bits each. For a 16 bit value V the required value (from the PNG + /* The input is an array of 16-bit components, these must be scaled to + * 8 bits each. For a 16-bit value V the required value (from the PNG * specification) is: * * (V * 255) / 65535 @@ -2647,7 +2403,7 @@ png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) * * The approximate differs from the exact answer only when (vlo-vhi) is * 128; it then gives a correction of +1 when the exact correction is - * 0. This gives 128 errors. The exact answer (correct for all 16 bit + * 0. This gives 128 errors. The exact answer (correct for all 16-bit * input values) is: * * error = (vlo-vhi+128)*65535 >> 24; @@ -2670,7 +2426,7 @@ png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) #endif #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED -void /* PRIVATE */ +static void /* Simply discard the low byte. This was the default behavior prior * to libpng-1.5.4. */ @@ -2698,7 +2454,7 @@ png_do_chop(png_row_infop row_info, png_bytep row) #endif #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED -void /* PRIVATE */ +static void png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_read_swap_alpha"); @@ -2795,7 +2551,7 @@ png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) #endif #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED -void /* PRIVATE */ +static void png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) { png_uint_32 row_width; @@ -2897,7 +2653,7 @@ png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) #ifdef PNG_READ_FILLER_SUPPORTED /* Add filler channel if we have RGB color */ -void /* PRIVATE */ +static void png_do_read_filler(png_row_infop row_info, png_bytep row, png_uint_32 filler, png_uint_32 flags) { @@ -2905,9 +2661,9 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, png_uint_32 row_width = row_info->width; #ifdef PNG_READ_16BIT_SUPPORTED - png_byte hi_filler = (png_byte)((filler>>8) & 0xff); + png_byte hi_filler = (png_byte)(filler>>8); #endif - png_byte lo_filler = (png_byte)(filler & 0xff); + png_byte lo_filler = (png_byte)filler; png_debug(1, "in png_do_read_filler"); @@ -2916,7 +2672,7 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, { if (row_info->bit_depth == 8) { - if (flags & PNG_FLAG_FILLER_AFTER) + if ((flags & PNG_FLAG_FILLER_AFTER) != 0) { /* This changes the data from G to GX */ png_bytep sp = row + (png_size_t)row_width; @@ -2951,20 +2707,20 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, #ifdef PNG_READ_16BIT_SUPPORTED else if (row_info->bit_depth == 16) { - if (flags & PNG_FLAG_FILLER_AFTER) + if ((flags & PNG_FLAG_FILLER_AFTER) != 0) { /* This changes the data from GG to GGXX */ png_bytep sp = row + (png_size_t)row_width * 2; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 1; i < row_width; i++) { - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; *(--dp) = *(--sp); *(--dp) = *(--sp); } - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; row_info->channels = 2; row_info->pixel_depth = 32; row_info->rowbytes = row_width * 4; @@ -2979,8 +2735,8 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, { *(--dp) = *(--sp); *(--dp) = *(--sp); - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; } row_info->channels = 2; row_info->pixel_depth = 32; @@ -2993,7 +2749,7 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, { if (row_info->bit_depth == 8) { - if (flags & PNG_FLAG_FILLER_AFTER) + if ((flags & PNG_FLAG_FILLER_AFTER) != 0) { /* This changes the data from RGB to RGBX */ png_bytep sp = row + (png_size_t)row_width * 3; @@ -3032,15 +2788,15 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, #ifdef PNG_READ_16BIT_SUPPORTED else if (row_info->bit_depth == 16) { - if (flags & PNG_FLAG_FILLER_AFTER) + if ((flags & PNG_FLAG_FILLER_AFTER) != 0) { /* This changes the data from RRGGBB to RRGGBBXX */ png_bytep sp = row + (png_size_t)row_width * 6; png_bytep dp = sp + (png_size_t)row_width * 2; for (i = 1; i < row_width; i++) { - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); @@ -3048,8 +2804,8 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, *(--dp) = *(--sp); *(--dp) = *(--sp); } - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; row_info->channels = 4; row_info->pixel_depth = 64; row_info->rowbytes = row_width * 8; @@ -3068,8 +2824,8 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, *(--dp) = *(--sp); *(--dp) = *(--sp); *(--dp) = *(--sp); - *(--dp) = hi_filler; *(--dp) = lo_filler; + *(--dp) = hi_filler; } row_info->channels = 4; @@ -3084,7 +2840,7 @@ png_do_read_filler(png_row_infop row_info, png_bytep row, #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED /* Expand grayscale files to RGB, with or without alpha */ -void /* PRIVATE */ +static void png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) { png_uint_32 i; @@ -3093,7 +2849,7 @@ png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) png_debug(1, "in png_do_gray_to_rgb"); if (row_info->bit_depth >= 8 && - !(row_info->color_type & PNG_COLOR_MASK_COLOR)) + (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0) { if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { @@ -3223,16 +2979,16 @@ png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) * calculated to make the sum 32768. This will result in different rounding * to that used above. */ -int /* PRIVATE */ -png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) +static int +png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) { int rgb_error = 0; png_debug(1, "in png_do_rgb_to_gray"); - if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) && - (row_info->color_type & PNG_COLOR_MASK_COLOR)) + if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 && + (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) { PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; @@ -3243,7 +2999,7 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) if (row_info->bit_depth == 8) { -#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) +#ifdef PNG_READ_GAMMA_SUPPORTED /* Notice that gamma to/from 1 are not necessarily inverses (if * there is an overall gamma correction). Prior to 1.5.5 this code * checked the linearized values for equality; this doesn't match @@ -3283,7 +3039,7 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) *(dp++) = red; } - if (have_alpha) + if (have_alpha != 0) *(dp++) = *(sp++); } } @@ -3312,7 +3068,7 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) else *(dp++) = red; - if (have_alpha) + if (have_alpha != 0) *(dp++) = *(sp++); } } @@ -3320,7 +3076,7 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) else /* RGB bit_depth == 16 */ { -#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) +#ifdef PNG_READ_GAMMA_SUPPORTED if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) { png_bytep sp = row; @@ -3330,16 +3086,17 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, w; + png_byte hi,lo; - red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; - green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; - blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; + hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); + hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); + hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); if (red == green && red == blue) { if (png_ptr->gamma_16_table != NULL) - w = png_ptr->gamma_16_table[(red&0xff) - >> png_ptr->gamma_shift][red>>8]; + w = png_ptr->gamma_16_table[(red & 0xff) + >> png_ptr->gamma_shift][red >> 8]; else w = red; @@ -3347,16 +3104,16 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) else { - png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) + png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff) >> png_ptr->gamma_shift][red>>8]; png_uint_16 green_1 = - png_ptr->gamma_16_to_1[(green&0xff) >> + png_ptr->gamma_16_to_1[(green & 0xff) >> png_ptr->gamma_shift][green>>8]; - png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) + png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff) >> png_ptr->gamma_shift][blue>>8]; png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 + bc*blue_1 + 16384)>>15); - w = png_ptr->gamma_16_from_1[(gray16&0xff) >> + w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >> png_ptr->gamma_shift][gray16 >> 8]; rgb_error |= 1; } @@ -3364,7 +3121,7 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) *(dp++) = (png_byte)((w>>8) & 0xff); *(dp++) = (png_byte)(w & 0xff); - if (have_alpha) + if (have_alpha != 0) { *(dp++) = *(sp++); *(dp++) = *(sp++); @@ -3381,24 +3138,25 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) for (i = 0; i < row_width; i++) { png_uint_16 red, green, blue, gray16; + png_byte hi,lo; - red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; - green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; - blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; + hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); + hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); + hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); if (red != green || red != blue) rgb_error |= 1; - /* From 1.5.5 in the 16 bit case do the accurate conversion even + /* From 1.5.5 in the 16-bit case do the accurate conversion even * in the 'fast' case - this is because this is where the code - * ends up when handling linear 16 bit data. + * ends up when handling linear 16-bit data. */ gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> 15); - *(dp++) = (png_byte)((gray16>>8) & 0xff); + *(dp++) = (png_byte)((gray16 >> 8) & 0xff); *(dp++) = (png_byte)(gray16 & 0xff); - if (have_alpha) + if (have_alpha != 0) { *(dp++) = *(sp++); *(dp++) = *(sp++); @@ -3417,74 +3175,15 @@ png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) return rgb_error; } #endif -#endif /* PNG_READ_TRANSFORMS_SUPPORTED */ -#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED -/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth - * large of png_color. This lets grayscale images be treated as - * paletted. Most useful for gamma correction and simplification - * of code. This API is not used internally. - */ -void PNGAPI -png_build_grayscale_palette(int bit_depth, png_colorp palette) -{ - int num_palette; - int color_inc; - int i; - int v; - - png_debug(1, "in png_do_build_grayscale_palette"); - - if (palette == NULL) - return; - - switch (bit_depth) - { - case 1: - num_palette = 2; - color_inc = 0xff; - break; - - case 2: - num_palette = 4; - color_inc = 0x55; - break; - - case 4: - num_palette = 16; - color_inc = 0x11; - break; - - case 8: - num_palette = 256; - color_inc = 1; - break; - - default: - num_palette = 0; - color_inc = 0; - break; - } - - for (i = 0, v = 0; i < num_palette; i++, v += color_inc) - { - palette[i].red = (png_byte)v; - palette[i].green = (png_byte)v; - palette[i].blue = (png_byte)v; - } -} -#endif - - -#ifdef PNG_READ_TRANSFORMS_SUPPORTED -#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\ - (defined PNG_READ_ALPHA_MODE_SUPPORTED) +#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ + defined(PNG_READ_ALPHA_MODE_SUPPORTED) /* Replace any alpha or transparency with the supplied background color. * "background" is already in the screen gamma, while "background_1" is * at a gamma of 1.0. Paletted files have already been taken care of. */ -void /* PRIVATE */ -png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) +static void +png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) { #ifdef PNG_READ_GAMMA_SUPPORTED png_const_bytep gamma_table = png_ptr->gamma_table; @@ -3494,12 +3193,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; int gamma_shift = png_ptr->gamma_shift; + int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; #endif png_bytep sp; png_uint_32 i; png_uint_32 row_width = row_info->width; - int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; int shift; png_debug(1, "in png_do_compose"); @@ -3520,11 +3219,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if ((png_uint_16)((*sp >> shift) & 0x01) == png_ptr->trans_color.gray) { - *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); - *sp |= (png_byte)(png_ptr->background.gray << shift); + unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); + tmp |= png_ptr->background.gray << shift; + *sp = (png_byte)(tmp & 0xff); } - if (!shift) + if (shift == 0) { shift = 7; sp++; @@ -3548,20 +3248,22 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if ((png_uint_16)((*sp >> shift) & 0x03) == png_ptr->trans_color.gray) { - *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); - *sp |= (png_byte)(png_ptr->background.gray << shift); + unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); + tmp |= png_ptr->background.gray << shift; + *sp = (png_byte)(tmp & 0xff); } else { - png_byte p = (png_byte)((*sp >> shift) & 0x03); - png_byte g = (png_byte)((gamma_table [p | (p << 2) | - (p << 4) | (p << 6)] >> 6) & 0x03); - *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); - *sp |= (png_byte)(g << shift); + unsigned int p = (*sp >> shift) & 0x03; + unsigned int g = (gamma_table [p | (p << 2) | + (p << 4) | (p << 6)] >> 6) & 0x03; + unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); + tmp |= g << shift; + *sp = (png_byte)(tmp & 0xff); } - if (!shift) + if (shift == 0) { shift = 6; sp++; @@ -3582,11 +3284,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if ((png_uint_16)((*sp >> shift) & 0x03) == png_ptr->trans_color.gray) { - *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); - *sp |= (png_byte)(png_ptr->background.gray << shift); + unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); + tmp |= png_ptr->background.gray << shift; + *sp = (png_byte)(tmp & 0xff); } - if (!shift) + if (shift == 0) { shift = 6; sp++; @@ -3611,20 +3314,22 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if ((png_uint_16)((*sp >> shift) & 0x0f) == png_ptr->trans_color.gray) { - *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); - *sp |= (png_byte)(png_ptr->background.gray << shift); + unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); + tmp |= png_ptr->background.gray << shift; + *sp = (png_byte)(tmp & 0xff); } else { - png_byte p = (png_byte)((*sp >> shift) & 0x0f); - png_byte g = (png_byte)((gamma_table[p | - (p << 4)] >> 4) & 0x0f); - *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); - *sp |= (png_byte)(g << shift); + unsigned int p = (*sp >> shift) & 0x0f; + unsigned int g = (gamma_table[p | (p << 4)] >> 4) & + 0x0f; + unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); + tmp |= g << shift; + *sp = (png_byte)(tmp & 0xff); } - if (!shift) + if (shift == 0) { shift = 4; sp++; @@ -3645,11 +3350,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if ((png_uint_16)((*sp >> shift) & 0x0f) == png_ptr->trans_color.gray) { - *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); - *sp |= (png_byte)(png_ptr->background.gray << shift); + unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); + tmp |= png_ptr->background.gray << shift; + *sp = (png_byte)(tmp & 0xff); } - if (!shift) + if (shift == 0) { shift = 4; sp++; @@ -3705,8 +3411,10 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if (v == png_ptr->trans_color.gray) { /* Background is already in screen gamma */ - *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff); - *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); + *sp = (png_byte)((png_ptr->background.gray >> 8) + & 0xff); + *(sp + 1) = (png_byte)(png_ptr->background.gray + & 0xff); } else @@ -3729,8 +3437,10 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if (v == png_ptr->trans_color.gray) { - *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff); - *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); + *sp = (png_byte)((png_ptr->background.gray >> 8) + & 0xff); + *(sp + 1) = (png_byte)(png_ptr->background.gray + & 0xff); } } } @@ -3810,9 +3520,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) /* Background is already in screen gamma */ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); - *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff); - *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff); - *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff); + *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) + & 0xff); + *(sp + 3) = (png_byte)(png_ptr->background.green + & 0xff); + *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) + & 0xff); *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); } @@ -3853,9 +3566,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) { *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); - *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff); - *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff); - *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff); + *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) + & 0xff); + *(sp + 3) = (png_byte)(png_ptr->background.green + & 0xff); + *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) + & 0xff); *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); } } @@ -3892,7 +3608,7 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) v = gamma_to_1[*sp]; png_composite(w, v, a, png_ptr->background_1.gray); - if (!optimize) + if (optimize == 0) w = gamma_from_1[w]; *sp = w; } @@ -3910,7 +3626,7 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) *sp = (png_byte)png_ptr->background.gray; else if (a < 0xff) - png_composite(*sp, *sp, a, png_ptr->background_1.gray); + png_composite(*sp, *sp, a, png_ptr->background.gray); } } } @@ -3938,7 +3654,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) else if (a == 0) { /* Background is already in screen gamma */ - *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff); + *sp = (png_byte)((png_ptr->background.gray >> 8) + & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); } @@ -3948,10 +3665,11 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; png_composite_16(v, g, a, png_ptr->background_1.gray); - if (optimize) + if (optimize != 0) w = v; else - w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; + w = gamma_16_from_1[(v & 0xff) >> + gamma_shift][v >> 8]; *sp = (png_byte)((w >> 8) & 0xff); *(sp + 1) = (png_byte)(w & 0xff); } @@ -3968,7 +3686,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) if (a == 0) { - *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff); + *sp = (png_byte)((png_ptr->background.gray >> 8) + & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); } @@ -3977,7 +3696,7 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) png_uint_16 g, v; g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); - png_composite_16(v, g, a, png_ptr->background_1.gray); + png_composite_16(v, g, a, png_ptr->background.gray); *sp = (png_byte)((v >> 8) & 0xff); *(sp + 1) = (png_byte)(v & 0xff); } @@ -4021,17 +3740,17 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) v = gamma_to_1[*sp]; png_composite(w, v, a, png_ptr->background_1.red); - if (!optimize) w = gamma_from_1[w]; + if (optimize == 0) w = gamma_from_1[w]; *sp = w; v = gamma_to_1[*(sp + 1)]; png_composite(w, v, a, png_ptr->background_1.green); - if (!optimize) w = gamma_from_1[w]; + if (optimize == 0) w = gamma_from_1[w]; *(sp + 1) = w; v = gamma_to_1[*(sp + 2)]; png_composite(w, v, a, png_ptr->background_1.blue); - if (!optimize) w = gamma_from_1[w]; + if (optimize == 0) w = gamma_from_1[w]; *(sp + 2) = w; } } @@ -4098,9 +3817,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) /* Background is already in screen gamma */ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); - *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff); - *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff); - *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff); + *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) + & 0xff); + *(sp + 3) = (png_byte)(png_ptr->background.green + & 0xff); + *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) + & 0xff); *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); } @@ -4110,23 +3832,26 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; png_composite_16(w, v, a, png_ptr->background_1.red); - if (!optimize) - w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; + if (optimize == 0) + w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> + 8]; *sp = (png_byte)((w >> 8) & 0xff); *(sp + 1) = (png_byte)(w & 0xff); v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; png_composite_16(w, v, a, png_ptr->background_1.green); - if (!optimize) - w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; + if (optimize == 0) + w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> + 8]; *(sp + 2) = (png_byte)((w >> 8) & 0xff); *(sp + 3) = (png_byte)(w & 0xff); v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; png_composite_16(w, v, a, png_ptr->background_1.blue); - if (!optimize) - w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; + if (optimize == 0) + w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> + 8]; *(sp + 4) = (png_byte)((w >> 8) & 0xff); *(sp + 5) = (png_byte)(w & 0xff); @@ -4147,9 +3872,12 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) { *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); - *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff); - *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff); - *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff); + *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) + & 0xff); + *(sp + 3) = (png_byte)(png_ptr->background.green + & 0xff); + *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) + & 0xff); *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); } @@ -4186,7 +3914,7 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) } } } -#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */ +#endif /* READ_BACKGROUND || READ_ALPHA_MODE */ #ifdef PNG_READ_GAMMA_SUPPORTED /* Gamma correct the image, avoiding the alpha channel. Make sure @@ -4195,8 +3923,8 @@ png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr) * is 16, use gamma_16_table and gamma_shift. Build these with * build_gamma_table(). */ -void /* PRIVATE */ -png_do_gamma(png_row_infop row_info, png_bytep row, png_structp png_ptr) +static void +png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) { png_const_bytep gamma_table = png_ptr->gamma_table; png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; @@ -4396,14 +4124,14 @@ png_do_gamma(png_row_infop row_info, png_bytep row, png_structp png_ptr) * linear.) Called only with color types that have an alpha channel. Needs the * from_1 tables. */ -void /* PRIVATE */ -png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structp png_ptr) +static void +png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) { png_uint_32 row_width = row_info->width; png_debug(1, "in png_do_encode_alpha"); - if (row_info->color_type & PNG_COLOR_MASK_ALPHA) + if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) { if (row_info->bit_depth == 8) { @@ -4462,7 +4190,7 @@ png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structp png_ptr) /* Expands a palette row to an RGB or RGBA row depending * upon whether you supply trans and num_trans. */ -void /* PRIVATE */ +static void png_do_expand_palette(png_row_infop row_info, png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha, int num_trans) { @@ -4615,7 +4343,7 @@ png_do_expand_palette(png_row_infop row_info, png_bytep row, /* If the bit depth < 8, it is expanded to 8. Also, if the already * expanded transparency value is supplied, an alpha channel is built. */ -void /* PRIVATE */ +static void png_do_expand(png_row_infop row_info, png_bytep row, png_const_color_16p trans_color) { @@ -4629,7 +4357,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, { if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { - png_uint_16 gray = (png_uint_16)(trans_color ? trans_color->gray : 0); + unsigned int gray = trans_color != NULL ? trans_color->gray : 0; if (row_info->bit_depth < 8) { @@ -4637,7 +4365,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, { case 1: { - gray = (png_uint_16)((gray & 0x01) * 0xff); + gray = (gray & 0x01) * 0xff; sp = row + (png_size_t)((row_width - 1) >> 3); dp = row + (png_size_t)row_width - 1; shift = 7 - (int)((row_width + 7) & 0x07); @@ -4665,7 +4393,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, case 2: { - gray = (png_uint_16)((gray & 0x03) * 0x55); + gray = (gray & 0x03) * 0x55; sp = row + (png_size_t)((row_width - 1) >> 2); dp = row + (png_size_t)row_width - 1; shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); @@ -4690,7 +4418,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, case 4: { - gray = (png_uint_16)((gray & 0x0f) * 0x11); + gray = (gray & 0x0f) * 0x11; sp = row + (png_size_t)((row_width - 1) >> 1); dp = row + (png_size_t)row_width - 1; shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); @@ -4731,7 +4459,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, for (i = 0; i < row_width; i++) { - if (*sp == gray) + if ((*sp & 0xffU) == gray) *dp-- = 0; else @@ -4743,13 +4471,14 @@ png_do_expand(png_row_infop row_info, png_bytep row, else if (row_info->bit_depth == 16) { - png_byte gray_high = (png_byte)((gray >> 8) & 0xff); - png_byte gray_low = (png_byte)(gray & 0xff); + unsigned int gray_high = (gray >> 8) & 0xff; + unsigned int gray_low = gray & 0xff; sp = row + row_info->rowbytes - 1; dp = row + (row_info->rowbytes << 1) - 1; for (i = 0; i < row_width; i++) { - if (*(sp - 1) == gray_high && *(sp) == gray_low) + if ((*(sp - 1) & 0xffU) == gray_high && + (*(sp) & 0xffU) == gray_low) { *dp-- = 0; *dp-- = 0; @@ -4773,7 +4502,8 @@ png_do_expand(png_row_infop row_info, png_bytep row, row_width); } } - else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color) + else if (row_info->color_type == PNG_COLOR_TYPE_RGB && + trans_color != NULL) { if (row_info->bit_depth == 8) { @@ -4845,7 +4575,7 @@ png_do_expand(png_row_infop row_info, png_bytep row, /* If the bit depth is 8 and the color type is not a palette type expand the * whole row to 16 bits. Has no effect otherwise. */ -void /* PRIVATE */ +static void png_do_expand_16(png_row_infop row_info, png_bytep row) { if (row_info->bit_depth == 8 && @@ -4873,7 +4603,7 @@ png_do_expand_16(png_row_infop row_info, png_bytep row) #endif #ifdef PNG_READ_QUANTIZE_SUPPORTED -void /* PRIVATE */ +static void png_do_quantize(png_row_infop row_info, png_bytep row, png_const_bytep palette_lookup, png_const_bytep quantize_lookup) { @@ -4964,70 +4694,304 @@ png_do_quantize(png_row_infop row_info, png_bytep row, } } } -#endif /* PNG_READ_QUANTIZE_SUPPORTED */ -#endif /* PNG_READ_TRANSFORMS_SUPPORTED */ +#endif /* READ_QUANTIZE */ -#ifdef PNG_MNG_FEATURES_SUPPORTED -/* Undoes intrapixel differencing */ +/* Transform the row. The order of transformations is significant, + * and is very touchy. If you add a transformation, take care to + * decide how it fits in with the other transformations here. + */ void /* PRIVATE */ -png_do_read_intrapixel(png_row_infop row_info, png_bytep row) +png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) { - png_debug(1, "in png_do_read_intrapixel"); + png_debug(1, "in png_do_read_transformations"); - if ( - (row_info->color_type & PNG_COLOR_MASK_COLOR)) + if (png_ptr->row_buf == NULL) { - int bytes_per_pixel; - png_uint_32 row_width = row_info->width; + /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this + * error is incredibly rare and incredibly easy to debug without this + * information. + */ + png_error(png_ptr, "NULL row buffer"); + } - if (row_info->bit_depth == 8) + /* The following is debugging; prior to 1.5.4 the code was never compiled in; + * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro + * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for + * all transformations, however in practice the ROW_INIT always gets done on + * demand, if necessary. + */ + if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && + (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) + { + /* Application has failed to call either png_read_start_image() or + * png_read_update_info() after setting transforms that expand pixels. + * This check added to libpng-1.2.19 (but not enabled until 1.5.4). + */ + png_error(png_ptr, "Uninitialized row"); + } + +#ifdef PNG_READ_EXPAND_SUPPORTED + if ((png_ptr->transformations & PNG_EXPAND) != 0) + { + if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) { - png_bytep rp; - png_uint_32 i; - - if (row_info->color_type == PNG_COLOR_TYPE_RGB) - bytes_per_pixel = 3; - - else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) - bytes_per_pixel = 4; - - else - return; - - for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) - { - *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); - *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); - } + png_do_expand_palette(row_info, png_ptr->row_buf + 1, + png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); } - else if (row_info->bit_depth == 16) + + else { - png_bytep rp; - png_uint_32 i; - - if (row_info->color_type == PNG_COLOR_TYPE_RGB) - bytes_per_pixel = 6; - - else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) - bytes_per_pixel = 8; + if (png_ptr->num_trans != 0 && + (png_ptr->transformations & PNG_EXPAND_tRNS) != 0) + png_do_expand(row_info, png_ptr->row_buf + 1, + &(png_ptr->trans_color)); else - return; - - for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) - { - png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); - png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); - png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); - png_uint_32 red = (s0 + s1 + 65536) & 0xffff; - png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; - *(rp ) = (png_byte)((red >> 8) & 0xff); - *(rp + 1) = (png_byte)(red & 0xff); - *(rp + 4) = (png_byte)((blue >> 8) & 0xff); - *(rp + 5) = (png_byte)(blue & 0xff); - } + png_do_expand(row_info, png_ptr->row_buf + 1, + NULL); } } +#endif + +#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && + (png_ptr->transformations & PNG_COMPOSE) == 0 && + (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) + png_do_strip_channel(row_info, png_ptr->row_buf + 1, + 0 /* at_start == false, because SWAP_ALPHA happens later */); +#endif + +#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) + { + int rgb_error = + png_do_rgb_to_gray(png_ptr, row_info, + png_ptr->row_buf + 1); + + if (rgb_error != 0) + { + png_ptr->rgb_to_gray_status=1; + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == + PNG_RGB_TO_GRAY_WARN) + png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); + + if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == + PNG_RGB_TO_GRAY_ERR) + png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); + } + } +#endif + +/* From Andreas Dilger e-mail to png-implement, 26 March 1998: + * + * In most cases, the "simple transparency" should be done prior to doing + * gray-to-RGB, or you will have to test 3x as many bytes to check if a + * pixel is transparent. You would also need to make sure that the + * transparency information is upgraded to RGB. + * + * To summarize, the current flow is: + * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite + * with background "in place" if transparent, + * convert to RGB if necessary + * - Gray + alpha -> composite with gray background and remove alpha bytes, + * convert to RGB if necessary + * + * To support RGB backgrounds for gray images we need: + * - Gray + simple transparency -> convert to RGB + simple transparency, + * compare 3 or 6 bytes and composite with + * background "in place" if transparent + * (3x compare/pixel compared to doing + * composite with gray bkgrnd) + * - Gray + alpha -> convert to RGB + alpha, composite with background and + * remove alpha bytes (3x float + * operations/pixel compared with composite + * on gray background) + * + * Greg's change will do this. The reason it wasn't done before is for + * performance, as this increases the per-pixel operations. If we would check + * in advance if the background was gray or RGB, and position the gray-to-RGB + * transform appropriately, then it would save a lot of work/time. + */ + +#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED + /* If gray -> RGB, do so now only if background is non-gray; else do later + * for performance reasons + */ + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && + (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0) + png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); +#endif + +#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ + defined(PNG_READ_ALPHA_MODE_SUPPORTED) + if ((png_ptr->transformations & PNG_COMPOSE) != 0) + png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); +#endif + +#ifdef PNG_READ_GAMMA_SUPPORTED + if ((png_ptr->transformations & PNG_GAMMA) != 0 && +#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED + /* Because RGB_TO_GRAY does the gamma transform. */ + (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 && +#endif +#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ + defined(PNG_READ_ALPHA_MODE_SUPPORTED) + /* Because PNG_COMPOSE does the gamma transform if there is something to + * do (if there is an alpha channel or transparency.) + */ + !((png_ptr->transformations & PNG_COMPOSE) != 0 && + ((png_ptr->num_trans != 0) || + (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) && +#endif + /* Because png_init_read_transformations transforms the palette, unless + * RGB_TO_GRAY will do the transform. + */ + (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) + png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); +#endif + +#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && + (png_ptr->transformations & PNG_COMPOSE) != 0 && + (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) + png_do_strip_channel(row_info, png_ptr->row_buf + 1, + 0 /* at_start == false, because SWAP_ALPHA happens later */); +#endif + +#ifdef PNG_READ_ALPHA_MODE_SUPPORTED + if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && + (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) + png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); +#endif + +#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED + if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) + png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED + /* There is no harm in doing both of these because only one has any effect, + * by putting the 'scale' option first if the app asks for scale (either by + * calling the API or in a TRANSFORM flag) this is what happens. + */ + if ((png_ptr->transformations & PNG_16_TO_8) != 0) + png_do_chop(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_QUANTIZE_SUPPORTED + if ((png_ptr->transformations & PNG_QUANTIZE) != 0) + { + png_do_quantize(row_info, png_ptr->row_buf + 1, + png_ptr->palette_lookup, png_ptr->quantize_index); + + if (row_info->rowbytes == 0) + png_error(png_ptr, "png_do_quantize returned rowbytes=0"); + } +#endif /* READ_QUANTIZE */ + +#ifdef PNG_READ_EXPAND_16_SUPPORTED + /* Do the expansion now, after all the arithmetic has been done. Notice + * that previous transformations can handle the PNG_EXPAND_16 flag if this + * is efficient (particularly true in the case of gamma correction, where + * better accuracy results faster!) + */ + if ((png_ptr->transformations & PNG_EXPAND_16) != 0) + png_do_expand_16(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED + /* NOTE: moved here in 1.5.4 (from much later in this list.) */ + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && + (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0) + png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_INVERT_SUPPORTED + if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) + png_do_invert(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) + png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_SHIFT_SUPPORTED + if ((png_ptr->transformations & PNG_SHIFT) != 0) + png_do_unshift(row_info, png_ptr->row_buf + 1, + &(png_ptr->shift)); +#endif + +#ifdef PNG_READ_PACK_SUPPORTED + if ((png_ptr->transformations & PNG_PACK) != 0) + png_do_unpack(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED + /* Added at libpng-1.5.10 */ + if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && + png_ptr->num_palette_max >= 0) + png_do_check_palette_indexes(png_ptr, row_info); +#endif + +#ifdef PNG_READ_BGR_SUPPORTED + if ((png_ptr->transformations & PNG_BGR) != 0) + png_do_bgr(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_PACKSWAP_SUPPORTED + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) + png_do_packswap(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_FILLER_SUPPORTED + if ((png_ptr->transformations & PNG_FILLER) != 0) + png_do_read_filler(row_info, png_ptr->row_buf + 1, + (png_uint_32)png_ptr->filler, png_ptr->flags); +#endif + +#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) + png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_READ_16BIT_SUPPORTED +#ifdef PNG_READ_SWAP_SUPPORTED + if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) + png_do_swap(row_info, png_ptr->row_buf + 1); +#endif +#endif + +#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED + if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) + { + if (png_ptr->read_user_transform_fn != NULL) + (*(png_ptr->read_user_transform_fn)) /* User read transform function */ + (png_ptr, /* png_ptr */ + row_info, /* row_info: */ + /* png_uint_32 width; width of row */ + /* png_size_t rowbytes; number of bytes in row */ + /* png_byte color_type; color type of pixels */ + /* png_byte bit_depth; bit depth of samples */ + /* png_byte channels; number of channels (1-4) */ + /* png_byte pixel_depth; bits per pixel (depth*channels) */ + png_ptr->row_buf + 1); /* start of pixel data for row */ +#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED + if (png_ptr->user_transform_depth != 0) + row_info->bit_depth = png_ptr->user_transform_depth; + + if (png_ptr->user_transform_channels != 0) + row_info->channels = png_ptr->user_transform_channels; +#endif + row_info->pixel_depth = (png_byte)(row_info->bit_depth * + row_info->channels); + + row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); + } +#endif } -#endif /* PNG_MNG_FEATURES_SUPPORTED */ -#endif /* PNG_READ_SUPPORTED */ + +#endif /* READ_TRANSFORMS */ +#endif /* READ */ diff --git a/3rdparty/libpng/pngrutil.c b/3rdparty/libpng/pngrutil.c index d8fe54cc6..ee584a8c4 100644 --- a/3rdparty/libpng/pngrutil.c +++ b/3rdparty/libpng/pngrutil.c @@ -1,8 +1,8 @@ /* pngrutil.c - utilities to read a PNG file * - * Last changed in libpng 1.5.10 [March 8, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -18,10 +18,8 @@ #ifdef PNG_READ_SUPPORTED -#define png_strtod(p,a,b) strtod(a,b) - png_uint_32 PNGAPI -png_get_uint_31(png_structp png_ptr, png_const_bytep buf) +png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) { png_uint_32 uval = png_get_uint_32(buf); @@ -40,7 +38,7 @@ png_get_uint_31(png_structp png_ptr, png_const_bytep buf) #define PNG_FIXED_ERROR (-1) static png_fixed_point /* PRIVATE */ -png_get_fixed_point(png_structp png_ptr, png_const_bytep buf) +png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf) { png_uint_32 uval = png_get_uint_32(buf); @@ -91,7 +89,13 @@ png_get_int_32)(png_const_bytep buf) return uval; uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ - return -(png_int_32)uval; + if ((uval & 0x80000000) == 0) /* no overflow */ + return -(png_int_32)uval; + /* The following has to be safe; this function only gets called on PNG data + * and if we get here that data is invalid. 0 is the most safe value and + * if not then an attacker would surely just generate a PNG with 0 instead. + */ + return 0; } /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ @@ -100,7 +104,7 @@ png_get_uint_16)(png_const_bytep buf) { /* ANSI-C requires an int value to accomodate at least 16 bits so this * works and allows the compiler not to worry about possible narrowing - * on 32 bit systems. (Pre-ANSI systems did not make integers smaller + * on 32-bit systems. (Pre-ANSI systems did not make integers smaller * than 16 bits either.) */ unsigned int val = @@ -110,11 +114,11 @@ png_get_uint_16)(png_const_bytep buf) return (png_uint_16)val; } -#endif /* PNG_READ_INT_FUNCTIONS_SUPPORTED */ +#endif /* READ_INT_FUNCTIONS */ /* Read and check the PNG file signature */ void /* PRIVATE */ -png_read_sig(png_structp png_ptr, png_infop info_ptr) +png_read_sig(png_structrp png_ptr, png_inforp info_ptr) { png_size_t num_checked, num_to_check; @@ -133,7 +137,7 @@ png_read_sig(png_structp png_ptr, png_infop info_ptr) png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); png_ptr->sig_bytes = 8; - if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) + if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0) { if (num_checked < 4 && png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) @@ -149,7 +153,7 @@ png_read_sig(png_structp png_ptr, png_infop info_ptr) * Put the type name into png_ptr->chunk_name, and return the length. */ png_uint_32 /* PRIVATE */ -png_read_chunk_header(png_structp png_ptr) +png_read_chunk_header(png_structrp png_ptr) { png_byte buf[8]; png_uint_32 length; @@ -186,7 +190,7 @@ png_read_chunk_header(png_structp png_ptr) /* Read data, and (optionally) run it through the CRC. */ void /* PRIVATE */ -png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) +png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) { if (png_ptr == NULL) return; @@ -196,40 +200,40 @@ png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) } /* Optionally skip data and then check the CRC. Depending on whether we - * are reading a ancillary or critical chunk, and how the program has set + * are reading an ancillary or critical chunk, and how the program has set * things up, we may calculate the CRC on the data and print a message. * Returns '1' if there was a CRC error, '0' otherwise. */ int /* PRIVATE */ -png_crc_finish(png_structp png_ptr, png_uint_32 skip) +png_crc_finish(png_structrp png_ptr, png_uint_32 skip) { - png_size_t i; - png_size_t istop = png_ptr->zbuf_size; - - for (i = (png_size_t)skip; i > istop; i -= istop) + /* The size of the local buffer for inflate is a good guess as to a + * reasonable size to use for buffering reads from the application. + */ + while (skip > 0) { - png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); + png_uint_32 len; + png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; + + len = (sizeof tmpbuf); + if (len > skip) + len = skip; + skip -= len; + + png_crc_read(png_ptr, tmpbuf, len); } - if (i) + if (png_crc_error(png_ptr) != 0) { - png_crc_read(png_ptr, png_ptr->zbuf, i); - } - - if (png_crc_error(png_ptr)) - { - if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name) ? - !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) : - (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) + if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ? + (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 : + (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0) { png_chunk_warning(png_ptr, "CRC error"); } else - { - png_chunk_benign_error(png_ptr, "CRC error"); - return (0); - } + png_chunk_error(png_ptr, "CRC error"); return (1); } @@ -241,13 +245,13 @@ png_crc_finish(png_structp png_ptr, png_uint_32 skip) * the data it has read thus far. */ int /* PRIVATE */ -png_crc_error(png_structp png_ptr) +png_crc_error(png_structrp png_ptr) { png_byte crc_bytes[4]; png_uint_32 crc; int need_crc = 1; - if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)) + if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0) { if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) @@ -256,7 +260,7 @@ png_crc_error(png_structp png_ptr) else /* critical */ { - if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) need_crc = 0; } @@ -267,7 +271,7 @@ png_crc_error(png_structp png_ptr) /* The chunk CRC must be serialized in a single I/O call. */ png_read_data(png_ptr, crc_bytes, 4); - if (need_crc) + if (need_crc != 0) { crc = png_get_uint_32(crc_bytes); return ((int)(crc != png_ptr->crc)); @@ -277,248 +281,521 @@ png_crc_error(png_structp png_ptr) return (0); } -#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED -static png_size_t -png_inflate(png_structp png_ptr, png_bytep data, png_size_t size, - png_bytep output, png_size_t output_size) +#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\ + defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\ + defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\ + defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED) +/* Manage the read buffer; this simply reallocates the buffer if it is not small + * enough (or if it is not allocated). The routine returns a pointer to the + * buffer; if an error occurs and 'warn' is set the routine returns NULL, else + * it will call png_error (via png_malloc) on failure. (warn == 2 means + * 'silent'). + */ +static png_bytep +png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) { - png_size_t count = 0; + png_bytep buffer = png_ptr->read_buffer; - /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it can't - * even necessarily handle 65536 bytes) because the type uInt is "16 bits or - * more". Consequently it is necessary to chunk the input to zlib. This - * code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the maximum value - * that can be stored in a uInt.) It is possible to set ZLIB_IO_MAX to a - * lower value in pngpriv.h and this may sometimes have a performance - * advantage, because it forces access of the input data to be separated from - * at least some of the use by some period of time. - */ - png_ptr->zstream.next_in = data; - /* avail_in is set below from 'size' */ - png_ptr->zstream.avail_in = 0; - - while (1) + if (buffer != NULL && new_size > png_ptr->read_buffer_size) { - int ret, avail; + png_ptr->read_buffer = NULL; + png_ptr->read_buffer = NULL; + png_ptr->read_buffer_size = 0; + png_free(png_ptr, buffer); + buffer = NULL; + } - /* The setting of 'avail_in' used to be outside the loop; by setting it - * inside it is possible to chunk the input to zlib and simply rely on - * zlib to advance the 'next_in' pointer. This allows arbitrary amounts o - * data to be passed through zlib at the unavoidable cost of requiring a - * window save (memcpy of up to 32768 output bytes) every ZLIB_IO_MAX - * input bytes. - */ - if (png_ptr->zstream.avail_in == 0 && size > 0) + if (buffer == NULL) + { + buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); + + if (buffer != NULL) { - if (size <= ZLIB_IO_MAX) - { - /* The value is less than ZLIB_IO_MAX so the cast is safe: */ - png_ptr->zstream.avail_in = (uInt)size; - size = 0; - } - - else - { - png_ptr->zstream.avail_in = ZLIB_IO_MAX; - size -= ZLIB_IO_MAX; - } + png_ptr->read_buffer = buffer; + png_ptr->read_buffer_size = new_size; } - /* Reset the output buffer each time round - we empty it - * after every inflate call. - */ - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = png_ptr->zbuf_size; - - ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); - avail = png_ptr->zbuf_size - png_ptr->zstream.avail_out; - - /* First copy/count any new output - but only if we didn't - * get an error code. - */ - if ((ret == Z_OK || ret == Z_STREAM_END) && avail > 0) + else if (warn < 2) /* else silent */ { - png_size_t space = avail; /* > 0, see above */ + if (warn != 0) + png_chunk_warning(png_ptr, "insufficient memory to read chunk"); - if (output != 0 && output_size > count) - { - png_size_t copy = output_size - count; + else + png_chunk_error(png_ptr, "insufficient memory to read chunk"); + } + } - if (space < copy) - copy = space; + return buffer; +} +#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */ - png_memcpy(output + count, png_ptr->zbuf, copy); - } - count += space; +/* png_inflate_claim: claim the zstream for some nefarious purpose that involves + * decompression. Returns Z_OK on success, else a zlib error code. It checks + * the owner but, in final release builds, just issues a warning if some other + * chunk apparently owns the stream. Prior to release it does a png_error. + */ +static int +png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) +{ + if (png_ptr->zowner != 0) + { + char msg[64]; + + PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); + /* So the message that results is " using zstream"; this is an + * internal error, but is very useful for debugging. i18n requirements + * are minimal. + */ + (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); +#if PNG_RELEASE_BUILD + png_chunk_warning(png_ptr, msg); + png_ptr->zowner = 0; +#else + png_chunk_error(png_ptr, msg); +#endif + } + + /* Implementation note: unlike 'png_deflate_claim' this internal function + * does not take the size of the data as an argument. Some efficiency could + * be gained by using this when it is known *if* the zlib stream itself does + * not record the number; however, this is an illusion: the original writer + * of the PNG may have selected a lower window size, and we really must + * follow that because, for systems with with limited capabilities, we + * would otherwise reject the application's attempts to use a smaller window + * size (zlib doesn't have an interface to say "this or lower"!). + * + * inflateReset2 was added to zlib 1.2.4; before this the window could not be + * reset, therefore it is necessary to always allocate the maximum window + * size with earlier zlibs just in case later compressed chunks need it. + */ + { + int ret; /* zlib return code */ +#if PNG_ZLIB_VERNUM >= 0x1240 + +# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW) + int window_bits; + + if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == + PNG_OPTION_ON) + window_bits = 15; + + else + window_bits = 0; +# else +# define window_bits 0 +# endif +#endif + + /* Set this for safety, just in case the previous owner left pointers to + * memory allocations. + */ + png_ptr->zstream.next_in = NULL; + png_ptr->zstream.avail_in = 0; + png_ptr->zstream.next_out = NULL; + png_ptr->zstream.avail_out = 0; + + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) + { +#if PNG_ZLIB_VERNUM < 0x1240 + ret = inflateReset(&png_ptr->zstream); +#else + ret = inflateReset2(&png_ptr->zstream, window_bits); +#endif + } + + else + { +#if PNG_ZLIB_VERNUM < 0x1240 + ret = inflateInit(&png_ptr->zstream); +#else + ret = inflateInit2(&png_ptr->zstream, window_bits); +#endif + + if (ret == Z_OK) + png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; } if (ret == Z_OK) - continue; + png_ptr->zowner = owner; - /* Termination conditions - always reset the zstream, it - * must be left in inflateInit state. + else + png_zstream_error(png_ptr, ret); + + return ret; + } + +#ifdef window_bits +# undef window_bits +#endif +} + +#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED +/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to + * allow the caller to do multiple calls if required. If the 'finish' flag is + * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must + * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and + * Z_OK or Z_STREAM_END will be returned on success. + * + * The input and output sizes are updated to the actual amounts of data consumed + * or written, not the amount available (as in a z_stream). The data pointers + * are not changed, so the next input is (data+input_size) and the next + * available output is (output+output_size). + */ +static int +png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, + /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, + /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) +{ + if (png_ptr->zowner == owner) /* Else not claimed */ + { + int ret; + png_alloc_size_t avail_out = *output_size_ptr; + png_uint_32 avail_in = *input_size_ptr; + + /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it + * can't even necessarily handle 65536 bytes) because the type uInt is + * "16 bits or more". Consequently it is necessary to chunk the input to + * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the + * maximum value that can be stored in a uInt.) It is possible to set + * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have + * a performance advantage, because it reduces the amount of data accessed + * at each step and that may give the OS more time to page it in. */ + png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); + /* avail_in and avail_out are set below from 'size' */ png_ptr->zstream.avail_in = 0; - inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_out = 0; - if (ret == Z_STREAM_END) - return count; /* NOTE: may be zero. */ - - /* Now handle the error codes - the API always returns 0 - * and the error message is dumped into the uncompressed - * buffer if available. + /* Read directly into the output if it is available (this is set to + * a local buffer below if output is NULL). */ -# ifdef PNG_WARNINGS_SUPPORTED + if (output != NULL) + png_ptr->zstream.next_out = output; + + do { - png_const_charp msg; + uInt avail; + Byte local_buffer[PNG_INFLATE_BUF_SIZE]; - if (png_ptr->zstream.msg != 0) - msg = png_ptr->zstream.msg; + /* zlib INPUT BUFFER */ + /* The setting of 'avail_in' used to be outside the loop; by setting it + * inside it is possible to chunk the input to zlib and simply rely on + * zlib to advance the 'next_in' pointer. This allows arbitrary + * amounts of data to be passed through zlib at the unavoidable cost of + * requiring a window save (memcpy of up to 32768 output bytes) + * every ZLIB_IO_MAX input bytes. + */ + avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ - else switch (ret) + avail = ZLIB_IO_MAX; + + if (avail_in < avail) + avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ + + avail_in -= avail; + png_ptr->zstream.avail_in = avail; + + /* zlib OUTPUT BUFFER */ + avail_out += png_ptr->zstream.avail_out; /* not written last time */ + + avail = ZLIB_IO_MAX; /* maximum zlib can process */ + + if (output == NULL) { - case Z_BUF_ERROR: - msg = "Buffer error in compressed datastream"; - break; - - case Z_DATA_ERROR: - msg = "Data error in compressed datastream"; - break; - - default: - msg = "Incomplete compressed datastream"; - break; + /* Reset the output buffer each time round if output is NULL and + * make available the full buffer, up to 'remaining_space' + */ + png_ptr->zstream.next_out = local_buffer; + if ((sizeof local_buffer) < avail) + avail = (sizeof local_buffer); } - png_chunk_warning(png_ptr, msg); - } -# endif + if (avail_out < avail) + avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ - /* 0 means an error - notice that this code simply ignores - * zero length compressed chunks as a result. + png_ptr->zstream.avail_out = avail; + avail_out -= avail; + + /* zlib inflate call */ + /* In fact 'avail_out' may be 0 at this point, that happens at the end + * of the read when the final LZ end code was not passed at the end of + * the previous chunk of input data. Tell zlib if we have reached the + * end of the output buffer. + */ + ret = inflate(&png_ptr->zstream, avail_out > 0 ? Z_NO_FLUSH : + (finish ? Z_FINISH : Z_SYNC_FLUSH)); + } while (ret == Z_OK); + + /* For safety kill the local buffer pointer now */ + if (output == NULL) + png_ptr->zstream.next_out = NULL; + + /* Claw back the 'size' and 'remaining_space' byte counts. */ + avail_in += png_ptr->zstream.avail_in; + avail_out += png_ptr->zstream.avail_out; + + /* Update the input and output sizes; the updated values are the amount + * consumed or written, effectively the inverse of what zlib uses. */ - return 0; + if (avail_out > 0) + *output_size_ptr -= avail_out; + + if (avail_in > 0) + *input_size_ptr -= avail_in; + + /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ + png_zstream_error(png_ptr, ret); + return ret; + } + + else + { + /* This is a bad internal error. The recovery assigns to the zstream msg + * pointer, which is not owned by the caller, but this is safe; it's only + * used on errors! + */ + png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); + return Z_STREAM_ERROR; } } /* - * Decompress trailing data in a chunk. The assumption is that chunkdata + * Decompress trailing data in a chunk. The assumption is that read_buffer * points at an allocated area holding the contents of a chunk with a * trailing compressed part. What we get back is an allocated area * holding the original prefix part and an uncompressed version of the * trailing part (the malloc area passed in is freed). */ -void /* PRIVATE */ -png_decompress_chunk(png_structp png_ptr, int comp_type, - png_size_t chunklength, - png_size_t prefix_size, png_size_t *newlength) +static int +png_decompress_chunk(png_structrp png_ptr, + png_uint_32 chunklength, png_uint_32 prefix_size, + png_alloc_size_t *newlength /* must be initialized to the maximum! */, + int terminate /*add a '\0' to the end of the uncompressed data*/) { - /* The caller should guarantee this */ - if (prefix_size > chunklength) + /* TODO: implement different limits for different types of chunk. + * + * The caller supplies *newlength set to the maximum length of the + * uncompressed data, but this routine allocates space for the prefix and + * maybe a '\0' terminator too. We have to assume that 'prefix_size' is + * limited only by the maximum chunk size. + */ + png_alloc_size_t limit = PNG_SIZE_MAX; + +# ifdef PNG_SET_USER_LIMITS_SUPPORTED + if (png_ptr->user_chunk_malloc_max > 0 && + png_ptr->user_chunk_malloc_max < limit) + limit = png_ptr->user_chunk_malloc_max; +# elif PNG_USER_CHUNK_MALLOC_MAX > 0 + if (PNG_USER_CHUNK_MALLOC_MAX < limit) + limit = PNG_USER_CHUNK_MALLOC_MAX; +# endif + + if (limit >= prefix_size + (terminate != 0)) { - /* The recovery is to delete the chunk. */ - png_warning(png_ptr, "invalid chunklength"); - prefix_size = 0; /* To delete everything */ - } + int ret; - else if (comp_type == PNG_COMPRESSION_TYPE_BASE) - { - png_size_t expanded_size = png_inflate(png_ptr, - (png_bytep)(png_ptr->chunkdata + prefix_size), - chunklength - prefix_size, - 0, /* output */ - 0); /* output size */ + limit -= prefix_size + (terminate != 0); - /* Now check the limits on this chunk - if the limit fails the - * compressed data will be removed, the prefix will remain. - */ - if (prefix_size >= (~(png_size_t)0) - 1 || - expanded_size >= (~(png_size_t)0) - 1 - prefix_size -#ifdef PNG_USER_LIMITS_SUPPORTED - || (png_ptr->user_chunk_malloc_max && - (prefix_size + expanded_size >= png_ptr->user_chunk_malloc_max - 1)) -#else - || ((PNG_USER_CHUNK_MALLOC_MAX > 0) && - prefix_size + expanded_size >= PNG_USER_CHUNK_MALLOC_MAX - 1) -#endif - ) - png_warning(png_ptr, "Exceeded size limit while expanding chunk"); + if (limit < *newlength) + *newlength = limit; - /* If the size is zero either there was an error and a message - * has already been output (warning) or the size really is zero - * and we have nothing to do - the code will exit through the - * error case below. - */ - else if (expanded_size > 0) + /* Now try to claim the stream. */ + ret = png_inflate_claim(png_ptr, png_ptr->chunk_name); + + if (ret == Z_OK) { - /* Success (maybe) - really uncompress the chunk. */ - png_size_t new_size = 0; - png_charp text = (png_charp)png_malloc_warn(png_ptr, - prefix_size + expanded_size + 1); + png_uint_32 lzsize = chunklength - prefix_size; - if (text != NULL) + ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, + /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, + /* output: */ NULL, newlength); + + if (ret == Z_STREAM_END) { - png_memcpy(text, png_ptr->chunkdata, prefix_size); - new_size = png_inflate(png_ptr, - (png_bytep)(png_ptr->chunkdata + prefix_size), - chunklength - prefix_size, - (png_bytep)(text + prefix_size), expanded_size); - text[prefix_size + expanded_size] = 0; /* just in case */ - - if (new_size == expanded_size) + /* Use 'inflateReset' here, not 'inflateReset2' because this + * preserves the previously decided window size (otherwise it would + * be necessary to store the previous window size.) In practice + * this doesn't matter anyway, because png_inflate will call inflate + * with Z_FINISH in almost all cases, so the window will not be + * maintained. + */ + if (inflateReset(&png_ptr->zstream) == Z_OK) { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = text; - *newlength = prefix_size + expanded_size; - return; /* The success return! */ + /* Because of the limit checks above we know that the new, + * expanded, size will fit in a size_t (let alone an + * png_alloc_size_t). Use png_malloc_base here to avoid an + * extra OOM message. + */ + png_alloc_size_t new_size = *newlength; + png_alloc_size_t buffer_size = prefix_size + new_size + + (terminate != 0); + png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, + buffer_size)); + + if (text != NULL) + { + ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, + png_ptr->read_buffer + prefix_size, &lzsize, + text + prefix_size, newlength); + + if (ret == Z_STREAM_END) + { + if (new_size == *newlength) + { + if (terminate != 0) + text[prefix_size + *newlength] = 0; + + if (prefix_size > 0) + memcpy(text, png_ptr->read_buffer, prefix_size); + + { + png_bytep old_ptr = png_ptr->read_buffer; + + png_ptr->read_buffer = text; + png_ptr->read_buffer_size = buffer_size; + text = old_ptr; /* freed below */ + } + } + + else + { + /* The size changed on the second read, there can be no + * guarantee that anything is correct at this point. + * The 'msg' pointer has been set to "unexpected end of + * LZ stream", which is fine, but return an error code + * that the caller won't accept. + */ + ret = PNG_UNEXPECTED_ZLIB_RETURN; + } + } + + else if (ret == Z_OK) + ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ + + /* Free the text pointer (this is the old read_buffer on + * success) + */ + png_free(png_ptr, text); + + /* This really is very benign, but it's still an error because + * the extra space may otherwise be used as a Trojan Horse. + */ + if (ret == Z_STREAM_END && + chunklength - prefix_size != lzsize) + png_chunk_benign_error(png_ptr, "extra compressed data"); + } + + else + { + /* Out of memory allocating the buffer */ + ret = Z_MEM_ERROR; + png_zstream_error(png_ptr, Z_MEM_ERROR); + } } - png_warning(png_ptr, "png_inflate logic error"); - png_free(png_ptr, text); + else + { + /* inflateReset failed, store the error message */ + png_zstream_error(png_ptr, ret); + + if (ret == Z_STREAM_END) + ret = PNG_UNEXPECTED_ZLIB_RETURN; + } } - else - png_warning(png_ptr, "Not enough memory to decompress chunk"); + else if (ret == Z_OK) + ret = PNG_UNEXPECTED_ZLIB_RETURN; + + /* Release the claimed stream */ + png_ptr->zowner = 0; } + + else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ + ret = PNG_UNEXPECTED_ZLIB_RETURN; + + return ret; } - else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ + else { - PNG_WARNING_PARAMETERS(p) - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, comp_type); - png_formatted_warning(png_ptr, p, "Unknown compression type @1"); - - /* The recovery is to simply drop the data. */ + /* Application/configuration limits exceeded */ + png_zstream_error(png_ptr, Z_MEM_ERROR); + return Z_MEM_ERROR; } - - /* Generic error return - leave the prefix, delete the compressed - * data, reallocate the chunkdata to remove the potentially large - * amount of compressed data. - */ - { - png_charp text = (png_charp)png_malloc_warn(png_ptr, prefix_size + 1); - - if (text != NULL) - { - if (prefix_size > 0) - png_memcpy(text, png_ptr->chunkdata, prefix_size); - - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = text; - - /* This is an extra zero in the 'uncompressed' part. */ - *(png_ptr->chunkdata + prefix_size) = 0x00; - } - /* Ignore a malloc error here - it is safe. */ - } - - *newlength = prefix_size; } -#endif /* PNG_READ_COMPRESSED_TEXT_SUPPORTED */ +#endif /* READ_COMPRESSED_TEXT */ + +#ifdef PNG_READ_iCCP_SUPPORTED +/* Perform a partial read and decompress, producing 'avail_out' bytes and + * reading from the current chunk as required. + */ +static int +png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, + png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, + int finish) +{ + if (png_ptr->zowner == png_ptr->chunk_name) + { + int ret; + + /* next_in and avail_in must have been initialized by the caller. */ + png_ptr->zstream.next_out = next_out; + png_ptr->zstream.avail_out = 0; /* set in the loop */ + + do + { + if (png_ptr->zstream.avail_in == 0) + { + if (read_size > *chunk_bytes) + read_size = (uInt)*chunk_bytes; + *chunk_bytes -= read_size; + + if (read_size > 0) + png_crc_read(png_ptr, read_buffer, read_size); + + png_ptr->zstream.next_in = read_buffer; + png_ptr->zstream.avail_in = read_size; + } + + if (png_ptr->zstream.avail_out == 0) + { + uInt avail = ZLIB_IO_MAX; + if (avail > *out_size) + avail = (uInt)*out_size; + *out_size -= avail; + + png_ptr->zstream.avail_out = avail; + } + + /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all + * the available output is produced; this allows reading of truncated + * streams. + */ + ret = inflate(&png_ptr->zstream, + *chunk_bytes > 0 ? Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); + } + while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); + + *out_size += png_ptr->zstream.avail_out; + png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ + + /* Ensure the error message pointer is always set: */ + png_zstream_error(png_ptr, ret); + return ret; + } + + else + { + png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); + return Z_STREAM_ERROR; + } +} +#endif /* Read and check the IDHR chunk */ + void /* PRIVATE */ -png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte buf[13]; png_uint_32 width, height; @@ -527,12 +804,12 @@ png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) png_debug(1, "in png_handle_IHDR"); - if (png_ptr->mode & PNG_HAVE_IHDR) - png_error(png_ptr, "Out of place IHDR"); + if ((png_ptr->mode & PNG_HAVE_IHDR) != 0) + png_chunk_error(png_ptr, "out of place"); /* Check the length */ if (length != 13) - png_error(png_ptr, "Invalid IHDR chunk"); + png_chunk_error(png_ptr, "invalid"); png_ptr->mode |= PNG_HAVE_IHDR; @@ -581,8 +858,7 @@ png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) } /* Set up other useful info */ - png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * - png_ptr->channels); + png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels); png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); png_debug1(3, "channels = %d", png_ptr->channels); @@ -593,36 +869,43 @@ png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) /* Read and check the palette */ void /* PRIVATE */ -png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_color palette[PNG_MAX_PALETTE_LENGTH]; - int num, i; + int max_palette_length, num, i; #ifdef PNG_POINTER_INDEXING_SUPPORTED png_colorp pal_ptr; #endif png_debug(1, "in png_handle_PLTE"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before PLTE"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + /* Moved to before the 'after IDAT' check below because otherwise duplicate + * PLTE chunks are potentially ignored (the spec says there shall not be more + * than one PLTE, the error is not treated as benign, so this check trumps + * the requirement that PLTE appears before IDAT.) + */ + else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0) + png_chunk_error(png_ptr, "duplicate"); + + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid PLTE after IDAT"); + /* This is benign because the non-benign error happened before, when an + * IDAT was encountered in a color-mapped image with no PLTE. + */ png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (png_ptr->mode & PNG_HAVE_PLTE) - png_error(png_ptr, "Duplicate PLTE chunk"); - png_ptr->mode |= PNG_HAVE_PLTE; - if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) { - png_warning(png_ptr, - "Ignoring PLTE chunk in grayscale PNG"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); return; } @@ -636,21 +919,33 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) { + png_crc_finish(png_ptr, length); + if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) - { - png_warning(png_ptr, "Invalid palette chunk"); - png_crc_finish(png_ptr, length); - return; - } + png_chunk_benign_error(png_ptr, "invalid"); else - { - png_error(png_ptr, "Invalid palette chunk"); - } + png_chunk_error(png_ptr, "invalid"); + + return; } + /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ num = (int)length / 3; + /* If the palette has 256 or fewer entries but is too large for the bit + * depth, we don't issue an error, to preserve the behavior of previous + * libpng versions. We silently truncate the unused extra palette entries + * here. + */ + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + max_palette_length = (1 << png_ptr->bit_depth); + else + max_palette_length = PNG_MAX_PALETTE_LENGTH; + + if (num > max_palette_length) + num = max_palette_length; + #ifdef PNG_POINTER_INDEXING_SUPPORTED for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) { @@ -683,218 +978,202 @@ png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) #endif { - png_crc_finish(png_ptr, 0); + png_crc_finish(png_ptr, (int) length - num * 3); } #ifndef PNG_READ_OPT_PLTE_SUPPORTED - else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ + else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */ { /* If we don't want to use the data from an ancillary chunk, * we have two options: an error abort, or a warning and we * ignore the data in this chunk (which should be OK, since * it's considered ancillary for a RGB or RGBA image). + * + * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the + * chunk type to determine whether to check the ancillary or the critical + * flags. */ - if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0) { - if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) - { - png_chunk_benign_error(png_ptr, "CRC error"); - } + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0) + return; else - { - png_chunk_warning(png_ptr, "CRC error"); - return; - } + png_chunk_error(png_ptr, "CRC error"); } /* Otherwise, we (optionally) emit a warning and use the chunk. */ - else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) - { + else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0) png_chunk_warning(png_ptr, "CRC error"); - } } #endif + /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its + * own copy of the palette. This has the side effect that when png_start_row + * is called (this happens after any call to png_read_update_info) the + * info_ptr palette gets changed. This is extremely unexpected and + * confusing. + * + * Fix this by not sharing the palette in this way. + */ png_set_PLTE(png_ptr, info_ptr, palette, num); + /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before + * IDAT. Prior to 1.6.0 this was not checked; instead the code merely + * checked the apparent validity of a tRNS chunk inserted before PLTE on a + * palette PNG. 1.6.0 attempts to rigorously follow the standard and + * therefore does a benign error if the erroneous condition is detected *and* + * cancels the tRNS if the benign error returns. The alternative is to + * amend the standard since it would be rather hypocritical of the standards + * maintainers to ignore it. + */ #ifdef PNG_READ_tRNS_SUPPORTED - if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + if (png_ptr->num_trans > 0 || + (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) { - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) - { - if (png_ptr->num_trans > (png_uint_16)num) - { - png_warning(png_ptr, "Truncating incorrect tRNS chunk length"); - png_ptr->num_trans = (png_uint_16)num; - } + /* Cancel this because otherwise it would be used if the transforms + * require it. Don't cancel the 'valid' flag because this would prevent + * detection of duplicate chunks. + */ + png_ptr->num_trans = 0; - if (info_ptr->num_trans > (png_uint_16)num) - { - png_warning(png_ptr, "Truncating incorrect info tRNS chunk length"); - info_ptr->num_trans = (png_uint_16)num; - } - } + if (info_ptr != NULL) + info_ptr->num_trans = 0; + + png_chunk_benign_error(png_ptr, "tRNS must be after"); } #endif +#ifdef PNG_READ_hIST_SUPPORTED + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) + png_chunk_benign_error(png_ptr, "hIST must be after"); +#endif + +#ifdef PNG_READ_bKGD_SUPPORTED + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) + png_chunk_benign_error(png_ptr, "bKGD must be after"); +#endif } void /* PRIVATE */ -png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_debug(1, "in png_handle_IEND"); - if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) - { - png_error(png_ptr, "No image in file"); - } + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 || + (png_ptr->mode & PNG_HAVE_IDAT) == 0) + png_chunk_error(png_ptr, "out of place"); png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); - if (length != 0) - { - png_warning(png_ptr, "Incorrect IEND chunk length"); - } - png_crc_finish(png_ptr, length); - PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */ + if (length != 0) + png_chunk_benign_error(png_ptr, "invalid"); + + PNG_UNUSED(info_ptr) } #ifdef PNG_READ_gAMA_SUPPORTED void /* PRIVATE */ -png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_fixed_point igamma; png_byte buf[4]; png_debug(1, "in png_handle_gAMA"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before gAMA"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) { - png_warning(png_ptr, "Invalid gAMA after IDAT"); - png_crc_finish(png_ptr, length); - return; - } - - else if (png_ptr->mode & PNG_HAVE_PLTE) - /* Should be an error, but we can cope with it */ - png_warning(png_ptr, "Out of place gAMA chunk"); - - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA) -#ifdef PNG_READ_sRGB_SUPPORTED - && !(info_ptr->valid & PNG_INFO_sRGB) -#endif - ) - { - png_warning(png_ptr, "Duplicate gAMA chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } if (length != 4) { - png_warning(png_ptr, "Incorrect gAMA chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, 4); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; igamma = png_get_fixed_point(NULL, buf); - /* Check for zero gamma or an error. */ - if (igamma <= 0) - { - png_warning(png_ptr, - "Ignoring gAMA chunk with out of range gamma"); - - return; - } - -# ifdef PNG_READ_sRGB_SUPPORTED - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) - { - if (PNG_OUT_OF_RANGE(igamma, 45500, 500)) - { - PNG_WARNING_PARAMETERS(p) - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, igamma); - png_formatted_warning(png_ptr, p, - "Ignoring incorrect gAMA value @1 when sRGB is also present"); - return; - } - } -# endif /* PNG_READ_sRGB_SUPPORTED */ - -# ifdef PNG_READ_GAMMA_SUPPORTED - /* Gamma correction on read is supported. */ - png_ptr->gamma = igamma; -# endif - /* And set the 'info' structure members. */ - png_set_gAMA_fixed(png_ptr, info_ptr, igamma); + png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma); + png_colorspace_sync(png_ptr, info_ptr); } #endif #ifdef PNG_READ_sBIT_SUPPORTED void /* PRIVATE */ -png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_size_t truelen; + unsigned int truelen, i; + png_byte sample_depth; png_byte buf[4]; png_debug(1, "in png_handle_sBIT"); - buf[0] = buf[1] = buf[2] = buf[3] = 0; + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before sBIT"); - - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) { - png_warning(png_ptr, "Invalid sBIT after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (png_ptr->mode & PNG_HAVE_PLTE) + if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0) { - /* Should be an error, but we can cope with it */ - png_warning(png_ptr, "Out of place sBIT chunk"); - } - - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT)) - { - png_warning(png_ptr, "Duplicate sBIT chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { truelen = 3; + sample_depth = 8; + } else - truelen = (png_size_t)png_ptr->channels; + { + truelen = png_ptr->channels; + sample_depth = png_ptr->bit_depth; + } if (length != truelen || length > 4) { - png_warning(png_ptr, "Incorrect sBIT chunk length"); + png_chunk_benign_error(png_ptr, "invalid"); png_crc_finish(png_ptr, length); return; } + buf[0] = buf[1] = buf[2] = buf[3] = sample_depth; png_crc_read(png_ptr, buf, truelen); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; - if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + for (i=0; i sample_depth) + { + png_chunk_benign_error(png_ptr, "invalid"); + return; + } + } + + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) { png_ptr->sig_bit.red = buf[0]; png_ptr->sig_bit.green = buf[1]; @@ -917,474 +1196,418 @@ png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_cHRM_SUPPORTED void /* PRIVATE */ -png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte buf[32]; - png_fixed_point x_white, y_white, x_red, y_red, x_green, y_green, x_blue, - y_blue; + png_xy xy; png_debug(1, "in png_handle_cHRM"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before cHRM"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) { - png_warning(png_ptr, "Invalid cHRM after IDAT"); - png_crc_finish(png_ptr, length); - return; - } - - else if (png_ptr->mode & PNG_HAVE_PLTE) - /* Should be an error, but we can cope with it */ - png_warning(png_ptr, "Out of place cHRM chunk"); - - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM) -# ifdef PNG_READ_sRGB_SUPPORTED - && !(info_ptr->valid & PNG_INFO_sRGB) -# endif - ) - { - png_warning(png_ptr, "Duplicate cHRM chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } if (length != 32) { - png_warning(png_ptr, "Incorrect cHRM chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, 32); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; - x_white = png_get_fixed_point(NULL, buf); - y_white = png_get_fixed_point(NULL, buf + 4); - x_red = png_get_fixed_point(NULL, buf + 8); - y_red = png_get_fixed_point(NULL, buf + 12); - x_green = png_get_fixed_point(NULL, buf + 16); - y_green = png_get_fixed_point(NULL, buf + 20); - x_blue = png_get_fixed_point(NULL, buf + 24); - y_blue = png_get_fixed_point(NULL, buf + 28); + xy.whitex = png_get_fixed_point(NULL, buf); + xy.whitey = png_get_fixed_point(NULL, buf + 4); + xy.redx = png_get_fixed_point(NULL, buf + 8); + xy.redy = png_get_fixed_point(NULL, buf + 12); + xy.greenx = png_get_fixed_point(NULL, buf + 16); + xy.greeny = png_get_fixed_point(NULL, buf + 20); + xy.bluex = png_get_fixed_point(NULL, buf + 24); + xy.bluey = png_get_fixed_point(NULL, buf + 28); - if (x_white == PNG_FIXED_ERROR || - y_white == PNG_FIXED_ERROR || - x_red == PNG_FIXED_ERROR || - y_red == PNG_FIXED_ERROR || - x_green == PNG_FIXED_ERROR || - y_green == PNG_FIXED_ERROR || - x_blue == PNG_FIXED_ERROR || - y_blue == PNG_FIXED_ERROR) + if (xy.whitex == PNG_FIXED_ERROR || + xy.whitey == PNG_FIXED_ERROR || + xy.redx == PNG_FIXED_ERROR || + xy.redy == PNG_FIXED_ERROR || + xy.greenx == PNG_FIXED_ERROR || + xy.greeny == PNG_FIXED_ERROR || + xy.bluex == PNG_FIXED_ERROR || + xy.bluey == PNG_FIXED_ERROR) { - png_warning(png_ptr, "Ignoring cHRM chunk with negative chromaticities"); + png_chunk_benign_error(png_ptr, "invalid values"); return; } -#ifdef PNG_READ_sRGB_SUPPORTED - if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB)) + /* If a colorspace error has already been output skip this chunk */ + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) + return; + + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0) { - if (PNG_OUT_OF_RANGE(x_white, 31270, 1000) || - PNG_OUT_OF_RANGE(y_white, 32900, 1000) || - PNG_OUT_OF_RANGE(x_red, 64000, 1000) || - PNG_OUT_OF_RANGE(y_red, 33000, 1000) || - PNG_OUT_OF_RANGE(x_green, 30000, 1000) || - PNG_OUT_OF_RANGE(y_green, 60000, 1000) || - PNG_OUT_OF_RANGE(x_blue, 15000, 1000) || - PNG_OUT_OF_RANGE(y_blue, 6000, 1000)) - { - PNG_WARNING_PARAMETERS(p) - - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, x_white); - png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_fixed, y_white); - png_warning_parameter_signed(p, 3, PNG_NUMBER_FORMAT_fixed, x_red); - png_warning_parameter_signed(p, 4, PNG_NUMBER_FORMAT_fixed, y_red); - png_warning_parameter_signed(p, 5, PNG_NUMBER_FORMAT_fixed, x_green); - png_warning_parameter_signed(p, 6, PNG_NUMBER_FORMAT_fixed, y_green); - png_warning_parameter_signed(p, 7, PNG_NUMBER_FORMAT_fixed, x_blue); - png_warning_parameter_signed(p, 8, PNG_NUMBER_FORMAT_fixed, y_blue); - - png_formatted_warning(png_ptr, p, - "Ignoring incorrect cHRM white(@1,@2) r(@3,@4)g(@5,@6)b(@7,@8) " - "when sRGB is also present"); - } + png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; + png_colorspace_sync(png_ptr, info_ptr); + png_chunk_benign_error(png_ptr, "duplicate"); return; } -#endif /* PNG_READ_sRGB_SUPPORTED */ -#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED - /* Store the _white values as default coefficients for the rgb to gray - * operation if it is supported. Check if the transform is already set to - * avoid destroying the transform values. - */ - if (!png_ptr->rgb_to_gray_coefficients_set) - { - /* png_set_background has not been called and we haven't seen an sRGB - * chunk yet. Find the XYZ of the three end points. - */ - png_XYZ XYZ; - png_xy xy; - - xy.redx = x_red; - xy.redy = y_red; - xy.greenx = x_green; - xy.greeny = y_green; - xy.bluex = x_blue; - xy.bluey = y_blue; - xy.whitex = x_white; - xy.whitey = y_white; - - if (png_XYZ_from_xy_checked(png_ptr, &XYZ, xy)) - { - /* The success case, because XYZ_from_xy normalises to a reference - * white Y of 1.0 we just need to scale the numbers. This should - * always work just fine. It is an internal error if this overflows. - */ - { - png_fixed_point r, g, b; - if (png_muldiv(&r, XYZ.redY, 32768, PNG_FP_1) && - r >= 0 && r <= 32768 && - png_muldiv(&g, XYZ.greenY, 32768, PNG_FP_1) && - g >= 0 && g <= 32768 && - png_muldiv(&b, XYZ.blueY, 32768, PNG_FP_1) && - b >= 0 && b <= 32768 && - r+g+b <= 32769) - { - /* We allow 0 coefficients here. r+g+b may be 32769 if two or - * all of the coefficients were rounded up. Handle this by - * reducing the *largest* coefficient by 1; this matches the - * approach used for the default coefficients in pngrtran.c - */ - int add = 0; - - if (r+g+b > 32768) - add = -1; - else if (r+g+b < 32768) - add = 1; - - if (add != 0) - { - if (g >= r && g >= b) - g += add; - else if (r >= g && r >= b) - r += add; - else - b += add; - } - - /* Check for an internal error. */ - if (r+g+b != 32768) - png_error(png_ptr, - "internal error handling cHRM coefficients"); - - png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r; - png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g; - } - - /* This is a png_error at present even though it could be ignored - - * it should never happen, but it is important that if it does, the - * bug is fixed. - */ - else - png_error(png_ptr, "internal error handling cHRM->XYZ"); - } - } - } -#endif - - png_set_cHRM_fixed(png_ptr, info_ptr, x_white, y_white, x_red, y_red, - x_green, y_green, x_blue, y_blue); + png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; + (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, + 1/*prefer cHRM values*/); + png_colorspace_sync(png_ptr, info_ptr); } #endif #ifdef PNG_READ_sRGB_SUPPORTED void /* PRIVATE */ -png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - int intent; - png_byte buf[1]; + png_byte intent; png_debug(1, "in png_handle_sRGB"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before sRGB"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) { - png_warning(png_ptr, "Invalid sRGB after IDAT"); - png_crc_finish(png_ptr, length); - return; - } - - else if (png_ptr->mode & PNG_HAVE_PLTE) - /* Should be an error, but we can cope with it */ - png_warning(png_ptr, "Out of place sRGB chunk"); - - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB)) - { - png_warning(png_ptr, "Duplicate sRGB chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } if (length != 1) { - png_warning(png_ptr, "Incorrect sRGB chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } - png_crc_read(png_ptr, buf, 1); + png_crc_read(png_ptr, &intent, 1); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; - intent = buf[0]; - - /* Check for bad intent */ - if (intent >= PNG_sRGB_INTENT_LAST) - { - png_warning(png_ptr, "Unknown sRGB intent"); + /* If a colorspace error has already been output skip this chunk */ + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) return; - } -#if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)) - { - if (PNG_OUT_OF_RANGE(info_ptr->gamma, 45500, 500)) - { - PNG_WARNING_PARAMETERS(p) - - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_fixed, - info_ptr->gamma); - - png_formatted_warning(png_ptr, p, - "Ignoring incorrect gAMA value @1 when sRGB is also present"); - } - } -#endif /* PNG_READ_gAMA_SUPPORTED */ - -#ifdef PNG_READ_cHRM_SUPPORTED - if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) - if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) || - PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) || - PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) || - PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) || - PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) || - PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) || - PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) || - PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000)) - { - png_warning(png_ptr, - "Ignoring incorrect cHRM value when sRGB is also present"); - } -#endif /* PNG_READ_cHRM_SUPPORTED */ - - /* This is recorded for use when handling the cHRM chunk above. An sRGB - * chunk unconditionally overwrites the coefficients for grayscale conversion - * too. + /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect + * this. */ - png_ptr->is_sRGB = 1; + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0) + { + png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; + png_colorspace_sync(png_ptr, info_ptr); + png_chunk_benign_error(png_ptr, "too many profiles"); + return; + } -# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED - /* Don't overwrite user supplied values: */ - if (!png_ptr->rgb_to_gray_coefficients_set) - { - /* These numbers come from the sRGB specification (or, since one has to - * pay much money to get a copy, the wikipedia sRGB page) the - * chromaticity values quoted have been inverted to get the reverse - * transformation from RGB to XYZ and the 'Y' coefficients scaled by - * 32768 (then rounded). - * - * sRGB and ITU Rec-709 both truncate the values for the D65 white - * point to four digits and, even though it actually stores five - * digits, the PNG spec gives the truncated value. - * - * This means that when the chromaticities are converted back to XYZ - * end points we end up with (6968,23435,2366), which, as described in - * pngrtran.c, would overflow. If the five digit precision and up is - * used we get, instead: - * - * 6968*R + 23435*G + 2365*B - * - * (Notice that this rounds the blue coefficient down, rather than the - * choice used in pngrtran.c which is to round the green one down.) - */ - png_ptr->rgb_to_gray_red_coeff = 6968; /* 0.212639005871510 */ - png_ptr->rgb_to_gray_green_coeff = 23434; /* 0.715168678767756 */ - /* png_ptr->rgb_to_gray_blue_coeff = 2366; 0.072192315360734 */ - - /* The following keeps the cHRM chunk from destroying the - * coefficients again in the event that it follows the sRGB chunk. - */ - png_ptr->rgb_to_gray_coefficients_set = 1; - } -# endif - - png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); + (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent); + png_colorspace_sync(png_ptr, info_ptr); } -#endif /* PNG_READ_sRGB_SUPPORTED */ +#endif /* READ_sRGB */ #ifdef PNG_READ_iCCP_SUPPORTED void /* PRIVATE */ -png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) -/* Note: this does not properly handle chunks that are > 64K under DOS */ +png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) +/* Note: this does not properly handle profiles that are > 64K under DOS */ { - png_byte compression_type; - png_bytep pC; - png_charp profile; - png_uint_32 skip = 0; - png_uint_32 profile_size; - png_alloc_size_t profile_length; - png_size_t slength, prefix_length, data_length; + png_const_charp errmsg = NULL; /* error message output, or no error */ + int finished = 0; /* crc checked */ png_debug(1, "in png_handle_iCCP"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before iCCP"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) + { + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); + return; + } + + /* Consistent with all the above colorspace handling an obviously *invalid* + * chunk is just ignored, so does not invalidate the color space. An + * alternative is to set the 'invalid' flags at the start of this routine + * and only clear them in they were not set before and all the tests pass. + * The minimum 'deflate' stream is assumed to be just the 2 byte header and + * 4 byte checksum. The keyword must be at least one character and there is + * a terminator (0) byte and the compression method. + */ + if (length < 9) + { + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "too short"); + return; + } + + /* If a colorspace error has already been output skip this chunk */ + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) { - png_warning(png_ptr, "Invalid iCCP after IDAT"); png_crc_finish(png_ptr, length); return; } - else if (png_ptr->mode & PNG_HAVE_PLTE) - /* Should be an error, but we can cope with it */ - png_warning(png_ptr, "Out of place iCCP chunk"); - - if ((png_ptr->mode & PNG_HAVE_iCCP) || (info_ptr != NULL && - (info_ptr->valid & (PNG_INFO_iCCP|PNG_INFO_sRGB)))) + /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect + * this. + */ + if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) { - png_warning(png_ptr, "Duplicate iCCP chunk"); - png_crc_finish(png_ptr, length); - return; - } + uInt read_length, keyword_length; + char keyword[81]; - png_ptr->mode |= PNG_HAVE_iCCP; + /* Find the keyword; the keyword plus separator and compression method + * bytes can be at most 81 characters long. + */ + read_length = 81; /* maximum */ + if (read_length > length) + read_length = (uInt)length; -#ifdef PNG_MAX_MALLOC_64K - if (length > (png_uint_32)65535L) - { - png_warning(png_ptr, "iCCP chunk too large to fit in memory"); - skip = length - (png_uint_32)65535L; - length = (png_uint_32)65535L; - } + png_crc_read(png_ptr, (png_bytep)keyword, read_length); + length -= read_length; + + keyword_length = 0; + while (keyword_length < 80 && keyword_length < read_length && + keyword[keyword_length] != 0) + ++keyword_length; + + /* TODO: make the keyword checking common */ + if (keyword_length >= 1 && keyword_length <= 79) + { + /* We only understand '0' compression - deflate - so if we get a + * different value we can't safely decode the chunk. + */ + if (keyword_length+1 < read_length && + keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) + { + read_length -= keyword_length+2; + + if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) + { + Byte profile_header[132]; + Byte local_buffer[PNG_INFLATE_BUF_SIZE]; + png_alloc_size_t size = (sizeof profile_header); + + png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); + png_ptr->zstream.avail_in = read_length; + (void)png_inflate_read(png_ptr, local_buffer, + (sizeof local_buffer), &length, profile_header, &size, + 0/*finish: don't, because the output is too small*/); + + if (size == 0) + { + /* We have the ICC profile header; do the basic header checks. + */ + const png_uint_32 profile_length = + png_get_uint_32(profile_header); + + if (png_icc_check_length(png_ptr, &png_ptr->colorspace, + keyword, profile_length) != 0) + { + /* The length is apparently ok, so we can check the 132 + * byte header. + */ + if (png_icc_check_header(png_ptr, &png_ptr->colorspace, + keyword, profile_length, profile_header, + png_ptr->color_type) != 0) + { + /* Now read the tag table; a variable size buffer is + * needed at this point, allocate one for the whole + * profile. The header check has already validated + * that none of these stuff will overflow. + */ + const png_uint_32 tag_count = png_get_uint_32( + profile_header+128); + png_bytep profile = png_read_buffer(png_ptr, + profile_length, 2/*silent*/); + + if (profile != NULL) + { + memcpy(profile, profile_header, + (sizeof profile_header)); + + size = 12 * tag_count; + + (void)png_inflate_read(png_ptr, local_buffer, + (sizeof local_buffer), &length, + profile + (sizeof profile_header), &size, 0); + + /* Still expect a buffer error because we expect + * there to be some tag data! + */ + if (size == 0) + { + if (png_icc_check_tag_table(png_ptr, + &png_ptr->colorspace, keyword, profile_length, + profile) != 0) + { + /* The profile has been validated for basic + * security issues, so read the whole thing in. + */ + size = profile_length - (sizeof profile_header) + - 12 * tag_count; + + (void)png_inflate_read(png_ptr, local_buffer, + (sizeof local_buffer), &length, + profile + (sizeof profile_header) + + 12 * tag_count, &size, 1/*finish*/); + + if (length > 0 && !(png_ptr->flags & + PNG_FLAG_BENIGN_ERRORS_WARN)) + errmsg = "extra compressed data"; + + /* But otherwise allow extra data: */ + else if (size == 0) + { + if (length > 0) + { + /* This can be handled completely, so + * keep going. + */ + png_chunk_warning(png_ptr, + "extra compressed data"); + } + + png_crc_finish(png_ptr, length); + finished = 1; + +# ifdef PNG_sRGB_SUPPORTED + /* Check for a match against sRGB */ + png_icc_set_sRGB(png_ptr, + &png_ptr->colorspace, profile, + png_ptr->zstream.adler); +# endif + + /* Steal the profile for info_ptr. */ + if (info_ptr != NULL) + { + png_free_data(png_ptr, info_ptr, + PNG_FREE_ICCP, 0); + + info_ptr->iccp_name = png_voidcast(char*, + png_malloc_base(png_ptr, + keyword_length+1)); + if (info_ptr->iccp_name != NULL) + { + memcpy(info_ptr->iccp_name, keyword, + keyword_length+1); + info_ptr->iccp_proflen = + profile_length; + info_ptr->iccp_profile = profile; + png_ptr->read_buffer = NULL; /*steal*/ + info_ptr->free_me |= PNG_FREE_ICCP; + info_ptr->valid |= PNG_INFO_iCCP; + } + + else + { + png_ptr->colorspace.flags |= + PNG_COLORSPACE_INVALID; + errmsg = "out of memory"; + } + } + + /* else the profile remains in the read + * buffer which gets reused for subsequent + * chunks. + */ + + if (info_ptr != NULL) + png_colorspace_sync(png_ptr, info_ptr); + + if (errmsg == NULL) + { + png_ptr->zowner = 0; + return; + } + } + + else if (size > 0) + errmsg = "truncated"; + +#ifndef __COVERITY__ + else + errmsg = png_ptr->zstream.msg; #endif + } - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + /* else png_icc_check_tag_table output an error */ + } - if (png_crc_finish(png_ptr, skip)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; + else /* profile truncated */ + errmsg = png_ptr->zstream.msg; + } + + else + errmsg = "out of memory"; + } + + /* else png_icc_check_header output an error */ + } + + /* else png_icc_check_length output an error */ + } + + else /* profile truncated */ + errmsg = png_ptr->zstream.msg; + + /* Release the stream */ + png_ptr->zowner = 0; + } + + else /* png_inflate_claim failed */ + errmsg = png_ptr->zstream.msg; + } + + else + errmsg = "bad compression method"; /* or missing */ + } + + else + errmsg = "bad keyword"; } - png_ptr->chunkdata[slength] = 0x00; + else + errmsg = "too many profiles"; - for (profile = png_ptr->chunkdata; *profile; profile++) - /* Empty loop to find end of name */ ; + /* Failure: the reason is in 'errmsg' */ + if (finished == 0) + png_crc_finish(png_ptr, length); - ++profile; - - /* There should be at least one zero (the compression type byte) - * following the separator, and we should be on it - */ - if (profile >= png_ptr->chunkdata + slength - 1) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - png_warning(png_ptr, "Malformed iCCP chunk"); - return; - } - - /* Compression_type should always be zero */ - compression_type = *profile++; - - if (compression_type) - { - png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); - compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 - wrote nonzero) */ - } - - prefix_length = profile - png_ptr->chunkdata; - png_decompress_chunk(png_ptr, compression_type, - slength, prefix_length, &data_length); - - profile_length = data_length - prefix_length; - - if (prefix_length > data_length || profile_length < 4) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - png_warning(png_ptr, "Profile size field missing from iCCP chunk"); - return; - } - - /* Check the profile_size recorded in the first 32 bits of the ICC profile */ - pC = (png_bytep)(png_ptr->chunkdata + prefix_length); - profile_size = ((*(pC )) << 24) | - ((*(pC + 1)) << 16) | - ((*(pC + 2)) << 8) | - ((*(pC + 3)) ); - - /* NOTE: the following guarantees that 'profile_length' fits into 32 bits, - * because profile_size is a 32 bit value. - */ - if (profile_size < profile_length) - profile_length = profile_size; - - /* And the following guarantees that profile_size == profile_length. */ - if (profile_size > profile_length) - { - PNG_WARNING_PARAMETERS(p) - - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - - png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_u, profile_size); - png_warning_parameter_unsigned(p, 2, PNG_NUMBER_FORMAT_u, profile_length); - png_formatted_warning(png_ptr, p, - "Ignoring iCCP chunk with declared size = @1 and actual length = @2"); - return; - } - - png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata, - compression_type, (png_bytep)png_ptr->chunkdata + prefix_length, - profile_size); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; + png_colorspace_sync(png_ptr, info_ptr); + if (errmsg != NULL) /* else already output */ + png_chunk_benign_error(png_ptr, errmsg); } -#endif /* PNG_READ_iCCP_SUPPORTED */ +#endif /* READ_iCCP */ #ifdef PNG_READ_sPLT_SUPPORTED void /* PRIVATE */ -png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) /* Note: this does not properly handle chunks that are > 64K under DOS */ { - png_bytep entry_start; + png_bytep entry_start, buffer; png_sPLT_t new_palette; png_sPLT_entryp pp; png_uint_32 data_length; int entry_size, i; png_uint_32 skip = 0; - png_size_t slength; png_uint_32 dl; png_size_t max_dl; png_debug(1, "in png_handle_sPLT"); #ifdef PNG_USER_LIMITS_SUPPORTED - if (png_ptr->user_chunk_cache_max != 0) { if (png_ptr->user_chunk_cache_max == 1) @@ -1402,55 +1625,53 @@ png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) } #endif - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before sPLT"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid sPLT after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } #ifdef PNG_MAX_MALLOC_64K - if (length > (png_uint_32)65535L) + if (length > 65535U) { - png_warning(png_ptr, "sPLT chunk too large to fit in memory"); - skip = length - (png_uint_32)65535L; - length = (png_uint_32)65535L; + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "too large to fit in memory"); + return; } #endif - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1); + buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); + if (buffer == NULL) + { + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of memory"); + return; + } + /* WARNING: this may break if size_t is less than 32 bits; it is assumed * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a * potential breakage point if the types in pngconf.h aren't exactly right. */ - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + png_crc_read(png_ptr, buffer, length); - if (png_crc_finish(png_ptr, skip)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, skip) != 0) return; - } - png_ptr->chunkdata[slength] = 0x00; + buffer[length] = 0; - for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; - entry_start++) + for (entry_start = buffer; *entry_start; entry_start++) /* Empty loop to find end of name */ ; ++entry_start; /* A sample depth should follow the separator, and we should be on it */ - if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2) + if (entry_start > buffer + length - 2) { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; png_warning(png_ptr, "malformed sPLT chunk"); return; } @@ -1458,39 +1679,35 @@ png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) new_palette.depth = *entry_start++; entry_size = (new_palette.depth == 8 ? 6 : 10); /* This must fit in a png_uint_32 because it is derived from the original - * chunk data length (and use 'length', not 'slength' here for clarity - - * they are guaranteed to be the same, see the tests above.) + * chunk data length. */ - data_length = length - (png_uint_32)(entry_start - - (png_bytep)png_ptr->chunkdata); + data_length = length - (png_uint_32)(entry_start - buffer); /* Integrity-check the data length */ - if (data_length % entry_size) + if ((data_length % entry_size) != 0) { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; png_warning(png_ptr, "sPLT chunk has bad length"); return; } dl = (png_int_32)(data_length / entry_size); - max_dl = PNG_SIZE_MAX / png_sizeof(png_sPLT_entry); + max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); if (dl > max_dl) { - png_warning(png_ptr, "sPLT chunk too long"); - return; + png_warning(png_ptr, "sPLT chunk too long"); + return; } new_palette.nentries = (png_int_32)(data_length / entry_size); new_palette.entries = (png_sPLT_entryp)png_malloc_warn( - png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); + png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry))); if (new_palette.entries == NULL) { - png_warning(png_ptr, "sPLT chunk requires too much memory"); - return; + png_warning(png_ptr, "sPLT chunk requires too much memory"); + return; } #ifdef PNG_POINTER_INDEXING_SUPPORTED @@ -1543,38 +1760,36 @@ png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #endif /* Discard all chunk data except the name and stash that */ - new_palette.name = png_ptr->chunkdata; + new_palette.name = (png_charp)buffer; png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; png_free(png_ptr, new_palette.entries); } -#endif /* PNG_READ_sPLT_SUPPORTED */ +#endif /* READ_sPLT */ #ifdef PNG_READ_tRNS_SUPPORTED void /* PRIVATE */ -png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; png_debug(1, "in png_handle_tRNS"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before tRNS"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid tRNS after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0) { - png_warning(png_ptr, "Duplicate tRNS chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } @@ -1584,8 +1799,8 @@ png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (length != 2) { - png_warning(png_ptr, "Incorrect tRNS chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } @@ -1600,12 +1815,12 @@ png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (length != 6) { - png_warning(png_ptr, "Incorrect tRNS chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } - png_crc_read(png_ptr, buf, (png_size_t)length); + png_crc_read(png_ptr, buf, length); png_ptr->num_trans = 1; png_ptr->trans_color.red = png_get_uint_16(buf); png_ptr->trans_color.green = png_get_uint_16(buf + 2); @@ -1614,44 +1829,44 @@ png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { - if (!(png_ptr->mode & PNG_HAVE_PLTE)) + if ((png_ptr->mode & PNG_HAVE_PLTE) == 0) { - /* Should be an error, but we can cope with it. */ - png_warning(png_ptr, "Missing PLTE before tRNS"); - } - - if (length > (png_uint_32)png_ptr->num_palette || - length > PNG_MAX_PALETTE_LENGTH) - { - png_warning(png_ptr, "Incorrect tRNS chunk length"); + /* TODO: is this actually an error in the ISO spec? */ png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - if (length == 0) + if (length > (unsigned int) png_ptr->num_palette || + length > (unsigned int) PNG_MAX_PALETTE_LENGTH || + length == 0) { - png_warning(png_ptr, "Zero length tRNS chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } - png_crc_read(png_ptr, readbuf, (png_size_t)length); + png_crc_read(png_ptr, readbuf, length); png_ptr->num_trans = (png_uint_16)length; } else { - png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid with alpha channel"); return; } - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) { png_ptr->num_trans = 0; return; } + /* TODO: this is a horrible side effect in the palette case because the + * png_struct ends up with a pointer to the tRNS buffer owned by the + * png_info. Fix this. + */ png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, &(png_ptr->trans_color)); } @@ -1659,43 +1874,37 @@ png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_bKGD_SUPPORTED void /* PRIVATE */ -png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_size_t truelen; + unsigned int truelen; png_byte buf[6]; png_color_16 background; png_debug(1, "in png_handle_bKGD"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before bKGD"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 || + (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && + (png_ptr->mode & PNG_HAVE_PLTE) == 0)) { - png_warning(png_ptr, "Invalid bKGD after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && - !(png_ptr->mode & PNG_HAVE_PLTE)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) { - png_warning(png_ptr, "Missing PLTE before bKGD"); - png_crc_finish(png_ptr, length); - return; - } - - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD)) - { - png_warning(png_ptr, "Duplicate bKGD chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) truelen = 1; - else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) + else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) truelen = 6; else @@ -1703,14 +1912,14 @@ png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (length != truelen) { - png_warning(png_ptr, "Incorrect bKGD chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, truelen); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; /* We convert the index value into RGB components so that we can allow @@ -1722,11 +1931,11 @@ png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { background.index = buf[0]; - if (info_ptr && info_ptr->num_palette) + if (info_ptr != NULL && info_ptr->num_palette != 0) { if (buf[0] >= info_ptr->num_palette) { - png_warning(png_ptr, "Incorrect bKGD chunk index value"); + png_chunk_benign_error(png_ptr, "invalid index"); return; } @@ -1741,7 +1950,7 @@ png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) background.gray = 0; } - else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */ + else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */ { background.index = 0; background.red = @@ -1765,47 +1974,41 @@ png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_hIST_SUPPORTED void /* PRIVATE */ -png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { unsigned int num, i; png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; png_debug(1, "in png_handle_hIST"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before hIST"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 || + (png_ptr->mode & PNG_HAVE_PLTE) == 0) { - png_warning(png_ptr, "Invalid hIST after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (!(png_ptr->mode & PNG_HAVE_PLTE)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) { - png_warning(png_ptr, "Missing PLTE before hIST"); - png_crc_finish(png_ptr, length); - return; - } - - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST)) - { - png_warning(png_ptr, "Duplicate hIST chunk"); - png_crc_finish(png_ptr, length); - return; - } - - if (length > 2*PNG_MAX_PALETTE_LENGTH || - length != (unsigned int) (2*png_ptr->num_palette)) - { - png_warning(png_ptr, "Incorrect hIST chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } num = length / 2 ; + if (num != (unsigned int) png_ptr->num_palette || + num > (unsigned int) PNG_MAX_PALETTE_LENGTH) + { + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); + return; + } + for (i = 0; i < num; i++) { png_byte buf[2]; @@ -1814,7 +2017,7 @@ png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) readbuf[i] = png_get_uint_16(buf); } - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; png_set_hIST(png_ptr, info_ptr, readbuf); @@ -1823,7 +2026,7 @@ png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_pHYs_SUPPORTED void /* PRIVATE */ -png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte buf[9]; png_uint_32 res_x, res_y; @@ -1831,33 +2034,33 @@ png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) png_debug(1, "in png_handle_pHYs"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before pHYs"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid pHYs after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0) { - png_warning(png_ptr, "Duplicate pHYs chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } if (length != 9) { - png_warning(png_ptr, "Incorrect pHYs chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, 9); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; res_x = png_get_uint_32(buf); @@ -1869,7 +2072,7 @@ png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_oFFs_SUPPORTED void /* PRIVATE */ -png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte buf[9]; png_int_32 offset_x, offset_y; @@ -1877,33 +2080,33 @@ png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) png_debug(1, "in png_handle_oFFs"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before oFFs"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid oFFs after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0) { - png_warning(png_ptr, "Duplicate oFFs chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } if (length != 9) { - png_warning(png_ptr, "Incorrect oFFs chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, 9); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; offset_x = png_get_int_32(buf); @@ -1916,71 +2119,64 @@ png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_pCAL_SUPPORTED /* Read the pCAL chunk (described in the PNG Extensions document) */ void /* PRIVATE */ -png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_int_32 X0, X1; png_byte type, nparams; - png_charp buf, units, endptr; + png_bytep buffer, buf, units, endptr; png_charpp params; - png_size_t slength; int i; png_debug(1, "in png_handle_pCAL"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before pCAL"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid pCAL after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0) { - png_warning(png_ptr, "Duplicate pCAL chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", length + 1); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); - if (png_ptr->chunkdata == NULL) + buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); + + if (buffer == NULL) { - png_warning(png_ptr, "No memory for pCAL purpose"); + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of memory"); return; } - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + png_crc_read(png_ptr, buffer, length); - if (png_crc_finish(png_ptr, 0)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, 0) != 0) return; - } - png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */ + buffer[length] = 0; /* Null terminate the last string */ png_debug(3, "Finding end of pCAL purpose string"); - for (buf = png_ptr->chunkdata; *buf; buf++) + for (buf = buffer; *buf; buf++) /* Empty loop */ ; - endptr = png_ptr->chunkdata + slength; + endptr = buffer + length; /* We need to have at least 12 bytes after the purpose string * in order to get the parameter information. */ if (endptr <= buf + 12) { - png_warning(png_ptr, "Invalid pCAL data"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + png_chunk_benign_error(png_ptr, "invalid"); return; } @@ -2000,15 +2196,13 @@ png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) (type == PNG_EQUATION_ARBITRARY && nparams != 3) || (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) { - png_warning(png_ptr, "Invalid pCAL parameters for equation type"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + png_chunk_benign_error(png_ptr, "invalid parameter count"); return; } else if (type >= PNG_EQUATION_LAST) { - png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); + png_chunk_benign_error(png_ptr, "unrecognized equation type"); } for (buf = units; *buf; buf++) @@ -2016,43 +2210,37 @@ png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) png_debug(3, "Allocating pCAL parameters array"); - params = (png_charpp)png_malloc_warn(png_ptr, - (png_size_t)(nparams * png_sizeof(png_charp))); + params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, + nparams * (sizeof (png_charp)))); if (params == NULL) { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - png_warning(png_ptr, "No memory for pCAL params"); + png_chunk_benign_error(png_ptr, "out of memory"); return; } /* Get pointers to the start of each parameter string. */ - for (i = 0; i < (int)nparams; i++) + for (i = 0; i < nparams; i++) { buf++; /* Skip the null string terminator from previous parameter. */ png_debug1(3, "Reading pCAL parameter %d", i); - for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++) + for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) /* Empty loop to move past each parameter string */ ; /* Make sure we haven't run out of data yet */ if (buf > endptr) { - png_warning(png_ptr, "Invalid pCAL data"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; png_free(png_ptr, params); + png_chunk_benign_error(png_ptr, "invalid data"); return; } } - png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams, - units, params); + png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, + (png_charp)units, params); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; png_free(png_ptr, params); } #endif @@ -2060,67 +2248,61 @@ png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_sCAL_SUPPORTED /* Read the sCAL chunk */ void /* PRIVATE */ -png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_size_t slength, i; + png_bytep buffer; + png_size_t i; int state; png_debug(1, "in png_handle_sCAL"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before sCAL"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (png_ptr->mode & PNG_HAVE_IDAT) + else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) { - png_warning(png_ptr, "Invalid sCAL after IDAT"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "out of place"); return; } - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0) { - png_warning(png_ptr, "Duplicate sCAL chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } /* Need unit type, width, \0, height: minimum 4 bytes */ else if (length < 4) { - png_warning(png_ptr, "sCAL chunk too short"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", length + 1); - png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); + buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); - if (png_ptr->chunkdata == NULL) + if (buffer == NULL) { - png_warning(png_ptr, "Out of memory while processing sCAL chunk"); + png_chunk_benign_error(png_ptr, "out of memory"); png_crc_finish(png_ptr, length); return; } - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); - png_ptr->chunkdata[slength] = 0x00; /* Null terminate the last string */ + png_crc_read(png_ptr, buffer, length); + buffer[length] = 0; /* Null terminate the last string */ - if (png_crc_finish(png_ptr, 0)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, 0) != 0) return; - } /* Validate the unit. */ - if (png_ptr->chunkdata[0] != 1 && png_ptr->chunkdata[0] != 2) + if (buffer[0] != 1 && buffer[0] != 2) { - png_warning(png_ptr, "Invalid sCAL ignored: invalid unit"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + png_chunk_benign_error(png_ptr, "invalid unit"); return; } @@ -2130,70 +2312,65 @@ png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) i = 1; state = 0; - if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) || - i >= slength || png_ptr->chunkdata[i++] != 0) - png_warning(png_ptr, "Invalid sCAL chunk ignored: bad width format"); + if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 || + i >= length || buffer[i++] != 0) + png_chunk_benign_error(png_ptr, "bad width format"); - else if (!PNG_FP_IS_POSITIVE(state)) - png_warning(png_ptr, "Invalid sCAL chunk ignored: non-positive width"); + else if (PNG_FP_IS_POSITIVE(state) == 0) + png_chunk_benign_error(png_ptr, "non-positive width"); else { png_size_t heighti = i; state = 0; - if (!png_check_fp_number(png_ptr->chunkdata, slength, &state, &i) || - i != slength) - png_warning(png_ptr, "Invalid sCAL chunk ignored: bad height format"); + if (png_check_fp_number((png_const_charp)buffer, length, + &state, &i) == 0 || i != length) + png_chunk_benign_error(png_ptr, "bad height format"); - else if (!PNG_FP_IS_POSITIVE(state)) - png_warning(png_ptr, - "Invalid sCAL chunk ignored: non-positive height"); + else if (PNG_FP_IS_POSITIVE(state) == 0) + png_chunk_benign_error(png_ptr, "non-positive height"); else /* This is the (only) success case. */ - png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], - png_ptr->chunkdata+1, png_ptr->chunkdata+heighti); + png_set_sCAL_s(png_ptr, info_ptr, buffer[0], + (png_charp)buffer+1, (png_charp)buffer+heighti); } - - /* Clean up - just free the temporarily allocated buffer. */ - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; } #endif #ifdef PNG_READ_tIME_SUPPORTED void /* PRIVATE */ -png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { png_byte buf[7]; png_time mod_time; png_debug(1, "in png_handle_tIME"); - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Out of place tIME chunk"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME)) + else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0) { - png_warning(png_ptr, "Duplicate tIME chunk"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "duplicate"); return; } - if (png_ptr->mode & PNG_HAVE_IDAT) + if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) png_ptr->mode |= PNG_AFTER_IDAT; if (length != 7) { - png_warning(png_ptr, "Incorrect tIME chunk length"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "invalid"); return; } png_crc_read(png_ptr, buf, 7); - if (png_crc_finish(png_ptr, 0)) + if (png_crc_finish(png_ptr, 0) != 0) return; mod_time.second = buf[6]; @@ -2210,14 +2387,13 @@ png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_tEXt_SUPPORTED /* Note: this does not properly handle chunks that are > 64K under DOS */ void /* PRIVATE */ -png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_textp text_ptr; + png_text text_info; + png_bytep buffer; png_charp key; png_charp text; png_uint_32 skip = 0; - png_size_t slength; - int ret; png_debug(1, "in png_handle_tEXt"); @@ -2232,84 +2408,59 @@ png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (--png_ptr->user_chunk_cache_max == 1) { - png_warning(png_ptr, "No space in chunk cache for tEXt"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "no space in chunk cache"); return; } } #endif - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before tEXt"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - if (png_ptr->mode & PNG_HAVE_IDAT) + if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) png_ptr->mode |= PNG_AFTER_IDAT; #ifdef PNG_MAX_MALLOC_64K - if (length > (png_uint_32)65535L) + if (length > 65535U) { - png_warning(png_ptr, "tEXt chunk too large to fit in memory"); - skip = length - (png_uint_32)65535L; - length = (png_uint_32)65535L; + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "too large to fit in memory"); + return; } #endif - png_free(png_ptr, png_ptr->chunkdata); + buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); - png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); - - if (png_ptr->chunkdata == NULL) + if (buffer == NULL) { - png_warning(png_ptr, "No memory to process text chunk"); + png_chunk_benign_error(png_ptr, "out of memory"); return; } - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + png_crc_read(png_ptr, buffer, length); - if (png_crc_finish(png_ptr, skip)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, skip) != 0) return; - } - key = png_ptr->chunkdata; - - key[slength] = 0x00; + key = (png_charp)buffer; + key[length] = 0; for (text = key; *text; text++) /* Empty loop to find end of key */ ; - if (text != key + slength) + if (text != key + length) text++; - text_ptr = (png_textp)png_malloc_warn(png_ptr, - png_sizeof(png_text)); + text_info.compression = PNG_TEXT_COMPRESSION_NONE; + text_info.key = key; + text_info.lang = NULL; + text_info.lang_key = NULL; + text_info.itxt_length = 0; + text_info.text = text; + text_info.text_length = strlen(text); - if (text_ptr == NULL) - { - png_warning(png_ptr, "Not enough memory to process text chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; - text_ptr->key = key; - text_ptr->lang = NULL; - text_ptr->lang_key = NULL; - text_ptr->itxt_length = 0; - text_ptr->text = text; - text_ptr->text_length = png_strlen(text); - - ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); - - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - png_free(png_ptr, text_ptr); - - if (ret) + if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0) png_warning(png_ptr, "Insufficient memory to process text chunk"); } #endif @@ -2317,13 +2468,11 @@ png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) #ifdef PNG_READ_zTXt_SUPPORTED /* Note: this does not correctly handle chunks that are > 64K under DOS */ void /* PRIVATE */ -png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_textp text_ptr; - png_charp text; - int comp_type; - int ret; - png_size_t slength, prefix_len, data_len; + png_const_charp errmsg = NULL; + png_bytep buffer; + png_uint_32 keyword_length; png_debug(1, "in png_handle_zTXt"); @@ -2338,123 +2487,101 @@ png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (--png_ptr->user_chunk_cache_max == 1) { - png_warning(png_ptr, "No space in chunk cache for zTXt"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "no space in chunk cache"); return; } } #endif - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before zTXt"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - if (png_ptr->mode & PNG_HAVE_IDAT) + if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) png_ptr->mode |= PNG_AFTER_IDAT; -#ifdef PNG_MAX_MALLOC_64K - /* We will no doubt have problems with chunks even half this size, but - * there is no hard and fast rule to tell us where to stop. - */ - if (length > (png_uint_32)65535L) + buffer = png_read_buffer(png_ptr, length, 2/*silent*/); + + if (buffer == NULL) { - png_warning(png_ptr, "zTXt chunk too large to fit in memory"); png_crc_finish(png_ptr, length); - return; - } -#endif - - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); - - if (png_ptr->chunkdata == NULL) - { - png_warning(png_ptr, "Out of memory processing zTXt chunk"); + png_chunk_benign_error(png_ptr, "out of memory"); return; } - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + png_crc_read(png_ptr, buffer, length); - if (png_crc_finish(png_ptr, 0)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, 0) != 0) return; - } - png_ptr->chunkdata[slength] = 0x00; + /* TODO: also check that the keyword contents match the spec! */ + for (keyword_length = 0; + keyword_length < length && buffer[keyword_length] != 0; + ++keyword_length) + /* Empty loop to find end of name */ ; - for (text = png_ptr->chunkdata; *text; text++) - /* Empty loop */ ; + if (keyword_length > 79 || keyword_length < 1) + errmsg = "bad keyword"; - /* zTXt must have some text after the chunkdataword */ - if (text >= png_ptr->chunkdata + slength - 2) - { - png_warning(png_ptr, "Truncated zTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } + /* zTXt must have some LZ data after the keyword, although it may expand to + * zero bytes; we need a '\0' at the end of the keyword, the compression type + * then the LZ data: + */ + else if (keyword_length + 3 > length) + errmsg = "truncated"; + + else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) + errmsg = "unknown compression type"; else { - comp_type = *(++text); + png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; - if (comp_type != PNG_TEXT_COMPRESSION_zTXt) - { - png_warning(png_ptr, "Unknown compression type in zTXt chunk"); - comp_type = PNG_TEXT_COMPRESSION_zTXt; - } + /* TODO: at present png_decompress_chunk imposes a single application + * level memory limit, this should be split to different values for iCCP + * and text chunks. + */ + if (png_decompress_chunk(png_ptr, length, keyword_length+2, + &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) + { + png_text text; - text++; /* Skip the compression_method byte */ + /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except + * for the extra compression type byte and the fact that it isn't + * necessarily '\0' terminated. + */ + buffer = png_ptr->read_buffer; + buffer[uncompressed_length+(keyword_length+2)] = 0; + + text.compression = PNG_TEXT_COMPRESSION_zTXt; + text.key = (png_charp)buffer; + text.text = (png_charp)(buffer + keyword_length+2); + text.text_length = uncompressed_length; + text.itxt_length = 0; + text.lang = NULL; + text.lang_key = NULL; + + if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0) + errmsg = "insufficient memory"; + } + + else + errmsg = png_ptr->zstream.msg; } - prefix_len = text - png_ptr->chunkdata; - - png_decompress_chunk(png_ptr, comp_type, - (png_size_t)length, prefix_len, &data_len); - - text_ptr = (png_textp)png_malloc_warn(png_ptr, - png_sizeof(png_text)); - - if (text_ptr == NULL) - { - png_warning(png_ptr, "Not enough memory to process zTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - text_ptr->compression = comp_type; - text_ptr->key = png_ptr->chunkdata; - text_ptr->lang = NULL; - text_ptr->lang_key = NULL; - text_ptr->itxt_length = 0; - text_ptr->text = png_ptr->chunkdata + prefix_len; - text_ptr->text_length = data_len; - - ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); - - png_free(png_ptr, text_ptr); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - - if (ret) - png_error(png_ptr, "Insufficient memory to store zTXt chunk"); + if (errmsg != NULL) + png_chunk_benign_error(png_ptr, errmsg); } #endif #ifdef PNG_READ_iTXt_SUPPORTED /* Note: this does not correctly handle chunks that are > 64K under DOS */ void /* PRIVATE */ -png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) { - png_textp text_ptr; - png_charp key, lang, text, lang_key; - int comp_flag; - int comp_type = 0; - int ret; - png_size_t slength, prefix_len, data_len; + png_const_charp errmsg = NULL; + png_bytep buffer; + png_uint_32 prefix_length; png_debug(1, "in png_handle_iTXt"); @@ -2469,279 +2596,393 @@ png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) if (--png_ptr->user_chunk_cache_max == 1) { - png_warning(png_ptr, "No space in chunk cache for iTXt"); png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "no space in chunk cache"); return; } } #endif - if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before iTXt"); + if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) + png_chunk_error(png_ptr, "missing IHDR"); - if (png_ptr->mode & PNG_HAVE_IDAT) + if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) png_ptr->mode |= PNG_AFTER_IDAT; -#ifdef PNG_MAX_MALLOC_64K - /* We will no doubt have problems with chunks even half this size, but - * there is no hard and fast rule to tell us where to stop. - */ - if (length > (png_uint_32)65535L) + buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); + + if (buffer == NULL) { - png_warning(png_ptr, "iTXt chunk too large to fit in memory"); png_crc_finish(png_ptr, length); - return; - } -#endif - - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); - - if (png_ptr->chunkdata == NULL) - { - png_warning(png_ptr, "No memory to process iTXt chunk"); + png_chunk_benign_error(png_ptr, "out of memory"); return; } - slength = length; - png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength); + png_crc_read(png_ptr, buffer, length); - if (png_crc_finish(png_ptr, 0)) - { - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; + if (png_crc_finish(png_ptr, 0) != 0) return; - } - png_ptr->chunkdata[slength] = 0x00; - - for (lang = png_ptr->chunkdata; *lang; lang++) + /* First the keyword. */ + for (prefix_length=0; + prefix_length < length && buffer[prefix_length] != 0; + ++prefix_length) /* Empty loop */ ; - lang++; /* Skip NUL separator */ + /* Perform a basic check on the keyword length here. */ + if (prefix_length > 79 || prefix_length < 1) + errmsg = "bad keyword"; - /* iTXt must have a language tag (possibly empty), two compression bytes, - * translated keyword (possibly empty), and possibly some text after the - * keyword + /* Expect keyword, compression flag, compression type, language, translated + * keyword (both may be empty but are 0 terminated) then the text, which may + * be empty. */ + else if (prefix_length + 5 > length) + errmsg = "truncated"; - if (lang >= png_ptr->chunkdata + slength - 3) + else if (buffer[prefix_length+1] == 0 || + (buffer[prefix_length+1] == 1 && + buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) { - png_warning(png_ptr, "Truncated iTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; + int compressed = buffer[prefix_length+1] != 0; + png_uint_32 language_offset, translated_keyword_offset; + png_alloc_size_t uncompressed_length = 0; + + /* Now the language tag */ + prefix_length += 3; + language_offset = prefix_length; + + for (; prefix_length < length && buffer[prefix_length] != 0; + ++prefix_length) + /* Empty loop */ ; + + /* WARNING: the length may be invalid here, this is checked below. */ + translated_keyword_offset = ++prefix_length; + + for (; prefix_length < length && buffer[prefix_length] != 0; + ++prefix_length) + /* Empty loop */ ; + + /* prefix_length should now be at the trailing '\0' of the translated + * keyword, but it may already be over the end. None of this arithmetic + * can overflow because chunks are at most 2^31 bytes long, but on 16-bit + * systems the available allocation may overflow. + */ + ++prefix_length; + + if (compressed == 0 && prefix_length <= length) + uncompressed_length = length - prefix_length; + + else if (compressed != 0 && prefix_length < length) + { + uncompressed_length = PNG_SIZE_MAX; + + /* TODO: at present png_decompress_chunk imposes a single application + * level memory limit, this should be split to different values for + * iCCP and text chunks. + */ + if (png_decompress_chunk(png_ptr, length, prefix_length, + &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) + buffer = png_ptr->read_buffer; + + else + errmsg = png_ptr->zstream.msg; + } + + else + errmsg = "truncated"; + + if (errmsg == NULL) + { + png_text text; + + buffer[uncompressed_length+prefix_length] = 0; + + if (compressed == 0) + text.compression = PNG_ITXT_COMPRESSION_NONE; + + else + text.compression = PNG_ITXT_COMPRESSION_zTXt; + + text.key = (png_charp)buffer; + text.lang = (png_charp)buffer + language_offset; + text.lang_key = (png_charp)buffer + translated_keyword_offset; + text.text = (png_charp)buffer + prefix_length; + text.text_length = 0; + text.itxt_length = uncompressed_length; + + if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0) + errmsg = "insufficient memory"; + } } else - { - comp_flag = *lang++; - comp_type = *lang++; - } + errmsg = "bad compression info"; - if (comp_type || (comp_flag && comp_flag != PNG_TEXT_COMPRESSION_zTXt)) - { - png_warning(png_ptr, "Unknown iTXt compression type or method"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - for (lang_key = lang; *lang_key; lang_key++) - /* Empty loop */ ; - - lang_key++; /* Skip NUL separator */ - - if (lang_key >= png_ptr->chunkdata + slength) - { - png_warning(png_ptr, "Truncated iTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - for (text = lang_key; *text; text++) - /* Empty loop */ ; - - text++; /* Skip NUL separator */ - - if (text >= png_ptr->chunkdata + slength) - { - png_warning(png_ptr, "Malformed iTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - prefix_len = text - png_ptr->chunkdata; - - key=png_ptr->chunkdata; - - if (comp_flag) - png_decompress_chunk(png_ptr, comp_type, - (size_t)length, prefix_len, &data_len); - - else - data_len = png_strlen(png_ptr->chunkdata + prefix_len); - - text_ptr = (png_textp)png_malloc_warn(png_ptr, - png_sizeof(png_text)); - - if (text_ptr == NULL) - { - png_warning(png_ptr, "Not enough memory to process iTXt chunk"); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - return; - } - - text_ptr->compression = (int)comp_flag + 1; - text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key); - text_ptr->lang = png_ptr->chunkdata + (lang - key); - text_ptr->itxt_length = data_len; - text_ptr->text_length = 0; - text_ptr->key = png_ptr->chunkdata; - text_ptr->text = png_ptr->chunkdata + prefix_len; - - ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1); - - png_free(png_ptr, text_ptr); - png_free(png_ptr, png_ptr->chunkdata); - png_ptr->chunkdata = NULL; - - if (ret) - png_error(png_ptr, "Insufficient memory to store iTXt chunk"); + if (errmsg != NULL) + png_chunk_benign_error(png_ptr, errmsg); } #endif -/* This function is called when we haven't found a handler for a - * chunk. If there isn't a problem with the chunk itself (ie bad - * chunk name, CRC, or a critical chunk), the chunk is silently ignored - * -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which - * case it will be saved away to be written out later. - */ -void /* PRIVATE */ -png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) -{ - png_uint_32 skip = 0; - - png_debug(1, "in png_handle_unknown"); - -#ifdef PNG_USER_LIMITS_SUPPORTED - if (png_ptr->user_chunk_cache_max != 0) - { - if (png_ptr->user_chunk_cache_max == 1) - { - png_crc_finish(png_ptr, length); - return; - } - - if (--png_ptr->user_chunk_cache_max == 1) - { - png_warning(png_ptr, "No space in chunk cache for unknown chunk"); - png_crc_finish(png_ptr, length); - return; - } - } -#endif - - if (png_ptr->mode & PNG_HAVE_IDAT) - { - if (png_ptr->chunk_name != png_IDAT) - png_ptr->mode |= PNG_AFTER_IDAT; - } - - if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) - { -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != - PNG_HANDLE_CHUNK_ALWAYS -#ifdef PNG_READ_USER_CHUNKS_SUPPORTED - && png_ptr->read_user_chunk_fn == NULL -#endif - ) -#endif - png_chunk_error(png_ptr, "unknown critical chunk"); - } - #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED - if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) -#ifdef PNG_READ_USER_CHUNKS_SUPPORTED - || (png_ptr->read_user_chunk_fn != NULL) -#endif - ) - { -#ifdef PNG_MAX_MALLOC_64K - if (length > 65535) - { - png_warning(png_ptr, "unknown chunk too large to fit in memory"); - skip = length - 65535; - length = 65535; - } -#endif +/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ +static int +png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) +{ + png_alloc_size_t limit = PNG_SIZE_MAX; - /* TODO: this code is very close to the unknown handling in pngpread.c, - * maybe it can be put into a common utility routine? - * png_struct::unknown_chunk is just used as a temporary variable, along - * with the data into which the chunk is read. These can be eliminated. - */ + if (png_ptr->unknown_chunk.data != NULL) + { + png_free(png_ptr, png_ptr->unknown_chunk.data); + png_ptr->unknown_chunk.data = NULL; + } + +# ifdef PNG_SET_USER_LIMITS_SUPPORTED + if (png_ptr->user_chunk_malloc_max > 0 && + png_ptr->user_chunk_malloc_max < limit) + limit = png_ptr->user_chunk_malloc_max; + +# elif PNG_USER_CHUNK_MALLOC_MAX > 0 + if (PNG_USER_CHUNK_MALLOC_MAX < limit) + limit = PNG_USER_CHUNK_MALLOC_MAX; +# endif + + if (length <= limit) + { PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); - png_ptr->unknown_chunk.size = (png_size_t)length; + /* The following is safe because of the PNG_SIZE_MAX init above */ + png_ptr->unknown_chunk.size = (png_size_t)length/*SAFE*/; + /* 'mode' is a flag array, only the bottom four bits matter here */ + png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; if (length == 0) png_ptr->unknown_chunk.data = NULL; else { - png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); - png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); + /* Do a 'warn' here - it is handled below. */ + png_ptr->unknown_chunk.data = png_voidcast(png_bytep, + png_malloc_warn(png_ptr, length)); } + } -#ifdef PNG_READ_USER_CHUNKS_SUPPORTED - if (png_ptr->read_user_chunk_fn != NULL) + if (png_ptr->unknown_chunk.data == NULL && length > 0) + { + /* This is benign because we clean up correctly */ + png_crc_finish(png_ptr, length); + png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits"); + return 0; + } + + else + { + if (length > 0) + png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); + png_crc_finish(png_ptr, 0); + return 1; + } +} +#endif /* READ_UNKNOWN_CHUNKS */ + +/* Handle an unknown, or known but disabled, chunk */ +void /* PRIVATE */ +png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, + png_uint_32 length, int keep) +{ + int handled = 0; /* the chunk was handled */ + + png_debug(1, "in png_handle_unknown"); + +#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED + /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing + * the bug which meant that setting a non-default behavior for a specific + * chunk would be ignored (the default was always used unless a user + * callback was installed). + * + * 'keep' is the value from the png_chunk_unknown_handling, the setting for + * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it + * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. + * This is just an optimization to avoid multiple calls to the lookup + * function. + */ +# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED +# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); +# endif +# endif + + /* One of the following methods will read the chunk or skip it (at least one + * of these is always defined because this is the only way to switch on + * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) + */ +# ifdef PNG_READ_USER_CHUNKS_SUPPORTED + /* The user callback takes precedence over the chunk keep value, but the + * keep value is still required to validate a save of a critical chunk. + */ + if (png_ptr->read_user_chunk_fn != NULL) + { + if (png_cache_unknown_chunk(png_ptr, length) != 0) { /* Callback to user unknown chunk handler */ - int ret; - - ret = (*(png_ptr->read_user_chunk_fn)) - (png_ptr, &png_ptr->unknown_chunk); + int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, + &png_ptr->unknown_chunk); + /* ret is: + * negative: An error occurred; png_chunk_error will be called. + * zero: The chunk was not handled, the chunk will be discarded + * unless png_set_keep_unknown_chunks has been used to set + * a 'keep' behavior for this particular chunk, in which + * case that will be used. A critical chunk will cause an + * error at this point unless it is to be saved. + * positive: The chunk was handled, libpng will ignore/discard it. + */ if (ret < 0) png_chunk_error(png_ptr, "error in user chunk"); - if (ret == 0) + else if (ret == 0) { - if (PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) + /* If the keep value is 'default' or 'never' override it, but + * still error out on critical chunks unless the keep value is + * 'always' While this is weird it is the behavior in 1.4.12. + * A possible improvement would be to obey the value set for the + * chunk, but this would be an API change that would probably + * damage some applications. + * + * The png_app_warning below catches the case that matters, where + * the application has not set specific save or ignore for this + * chunk or global save or ignore. + */ + if (keep < PNG_HANDLE_CHUNK_IF_SAFE) { -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - if (png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name) != - PNG_HANDLE_CHUNK_ALWAYS) -#endif - png_chunk_error(png_ptr, "unknown critical chunk"); +# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) + { + png_chunk_warning(png_ptr, "Saving unknown chunk:"); + png_app_warning(png_ptr, + "forcing save of an unhandled chunk;" + " please call png_set_keep_unknown_chunks"); + /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */ + } +# endif + keep = PNG_HANDLE_CHUNK_IF_SAFE; } + } - png_set_unknown_chunks(png_ptr, info_ptr, - &png_ptr->unknown_chunk, 1); + else /* chunk was handled */ + { + handled = 1; + /* Critical chunks can be safely discarded at this point. */ + keep = PNG_HANDLE_CHUNK_NEVER; } } else -#endif - png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); - - png_free(png_ptr, png_ptr->unknown_chunk.data); - png_ptr->unknown_chunk.data = NULL; + keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ } else -#endif - skip = length; + /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ +# endif /* READ_USER_CHUNKS */ - png_crc_finish(png_ptr, skip); +# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED + { + /* keep is currently just the per-chunk setting, if there was no + * setting change it to the global default now (not that this may + * still be AS_DEFAULT) then obtain the cache of the chunk if required, + * if not simply skip the chunk. + */ + if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) + keep = png_ptr->unknown_default; -#ifndef PNG_READ_USER_CHUNKS_SUPPORTED - PNG_UNUSED(info_ptr) /* Quiet compiler warnings about unused info_ptr */ -#endif + if (keep == PNG_HANDLE_CHUNK_ALWAYS || + (keep == PNG_HANDLE_CHUNK_IF_SAFE && + PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) + { + if (png_cache_unknown_chunk(png_ptr, length) == 0) + keep = PNG_HANDLE_CHUNK_NEVER; + } + + else + png_crc_finish(png_ptr, length); + } +# else +# ifndef PNG_READ_USER_CHUNKS_SUPPORTED +# error no method to support READ_UNKNOWN_CHUNKS +# endif + + { + /* If here there is no read callback pointer set and no support is + * compiled in to just save the unknown chunks, so simply skip this + * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then + * the app has erroneously asked for unknown chunk saving when there + * is no support. + */ + if (keep > PNG_HANDLE_CHUNK_NEVER) + png_app_error(png_ptr, "no unknown chunk support available"); + + png_crc_finish(png_ptr, length); + } +# endif + +# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED + /* Now store the chunk in the chunk list if appropriate, and if the limits + * permit it. + */ + if (keep == PNG_HANDLE_CHUNK_ALWAYS || + (keep == PNG_HANDLE_CHUNK_IF_SAFE && + PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) + { +# ifdef PNG_USER_LIMITS_SUPPORTED + switch (png_ptr->user_chunk_cache_max) + { + case 2: + png_ptr->user_chunk_cache_max = 1; + png_chunk_benign_error(png_ptr, "no space in chunk cache"); + /* FALL THROUGH */ + case 1: + /* NOTE: prior to 1.6.0 this case resulted in an unknown critical + * chunk being skipped, now there will be a hard error below. + */ + break; + + default: /* not at limit */ + --(png_ptr->user_chunk_cache_max); + /* FALL THROUGH */ + case 0: /* no limit */ +# endif /* USER_LIMITS */ + /* Here when the limit isn't reached or when limits are compiled + * out; store the chunk. + */ + png_set_unknown_chunks(png_ptr, info_ptr, + &png_ptr->unknown_chunk, 1); + handled = 1; +# ifdef PNG_USER_LIMITS_SUPPORTED + break; + } +# endif + } +# else /* no store support: the chunk must be handled by the user callback */ + PNG_UNUSED(info_ptr) +# endif + + /* Regardless of the error handling below the cached data (if any) can be + * freed now. Notice that the data is not freed if there is a png_error, but + * it will be freed by destroy_read_struct. + */ + if (png_ptr->unknown_chunk.data != NULL) + png_free(png_ptr, png_ptr->unknown_chunk.data); + png_ptr->unknown_chunk.data = NULL; + +#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ + /* There is no support to read an unknown chunk, so just skip it. */ + png_crc_finish(png_ptr, length); + PNG_UNUSED(info_ptr) + PNG_UNUSED(keep) +#endif /* !READ_UNKNOWN_CHUNKS */ + + /* Check for unhandled critical chunks */ + if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) + png_chunk_error(png_ptr, "unhandled critical chunk"); } /* This function is called to verify that a chunk name is valid. @@ -2757,7 +2998,7 @@ png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) */ void /* PRIVATE */ -png_check_chunk_name(png_structp png_ptr, png_uint_32 chunk_name) +png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name) { int i; @@ -2782,11 +3023,11 @@ png_check_chunk_name(png_structp png_ptr, png_uint_32 chunk_name) * 'display' is false only those pixels present in the pass are filled in. */ void /* PRIVATE */ -png_combine_row(png_structp png_ptr, png_bytep dp, int display) +png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) { unsigned int pixel_depth = png_ptr->transformed_pixel_depth; png_const_bytep sp = png_ptr->row_buf + 1; - png_uint_32 row_width = png_ptr->width; + png_alloc_size_t row_width = png_ptr->width; unsigned int pass = png_ptr->pass; png_bytep end_ptr = 0; png_byte end_byte = 0; @@ -2823,26 +3064,28 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; end_byte = *end_ptr; # ifdef PNG_READ_PACKSWAP_SUPPORTED - if (png_ptr->transformations & PNG_PACKSWAP) /* little-endian byte */ - end_mask = 0xff << end_mask; + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) + /* little-endian byte */ + end_mask = 0xff << end_mask; - else /* big-endian byte */ + else /* big-endian byte */ # endif - end_mask = 0xff >> end_mask; + end_mask = 0xff >> end_mask; /* end_mask is now the bits to *keep* from the destination row */ } - /* For non-interlaced images this reduces to a png_memcpy(). A png_memcpy() + /* For non-interlaced images this reduces to a memcpy(). A memcpy() * will also happen if interlacing isn't supported or if the application * does not call png_set_interlace_handling(). In the latter cases the * caller just gets a sequence of the unexpanded rows from each interlace * pass. */ #ifdef PNG_READ_INTERLACING_SUPPORTED - if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE) && - pass < 6 && (display == 0 || - /* The following copies everything for 'display' on passes 0, 2 and 4. */ - (display == 1 && (pass & 1) != 0))) + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) != 0 && + pass < 6 && (display == 0 || + /* The following copies everything for 'display' on passes 0, 2 and 4. */ + (display == 1 && (pass & 1) != 0))) { /* Narrow images may have no bits in a pass; the caller should handle * this, but this test is cheap: @@ -2938,7 +3181,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } -# define B_MASKS(d,s) { B_MASK(1,d,s), S_MASK(3,d,s), S_MASK(5,d,s) } +# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) } # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) @@ -2974,10 +3217,10 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) */ # define MASK(pass,depth,display,png)\ ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) -#endif /* !PNG_USE_COMPILE_TIME_MASKS */ +#endif /* !USE_COMPILE_TIME_MASKS */ /* Use the appropriate mask to copy the required bits. In some cases - * the byte mask will be 0 or 0xff, optimize these cases. row_width is + * the byte mask will be 0 or 0xff; optimize these cases. row_width is * the number of pixels, but the code copies bytes, so it is necessary * to special case the end. */ @@ -2985,12 +3228,12 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) png_uint_32 mask; # ifdef PNG_READ_PACKSWAP_SUPPORTED - if (png_ptr->transformations & PNG_PACKSWAP) - mask = MASK(pass, pixel_depth, display, 0); + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) + mask = MASK(pass, pixel_depth, display, 0); - else + else # endif - mask = MASK(pass, pixel_depth, display, 1); + mask = MASK(pass, pixel_depth, display, 1); for (;;) { @@ -3049,7 +3292,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) } /* Work out the bytes to copy. */ - if (display) + if (display != 0) { /* When doing the 'block' algorithm the pixel in the pass gets * replicated to adjacent pixels. This is why the even (0,2,4,6) @@ -3059,7 +3302,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) /* But don't allow this number to exceed the actual row width. */ if (bytes_to_copy > row_width) - bytes_to_copy = row_width; + bytes_to_copy = (unsigned int)/*SAFE*/row_width; } else /* normal row; Adam7 only ever gives us one pixel to copy. */ @@ -3116,7 +3359,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) /* This can only be the RGB case, so each copy is exactly one * pixel and it is not necessary to check for a partial copy. */ - for(;;) + for (;;) { dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2]; @@ -3133,26 +3376,27 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) /* Check for double byte alignment and, if possible, use a * 16-bit copy. Don't attempt this for narrow images - ones that * are less than an interlace panel wide. Don't attempt it for - * wide bytes_to_copy either - use the png_memcpy there. + * wide bytes_to_copy either - use the memcpy there. */ - if (bytes_to_copy < 16 /*else use png_memcpy*/ && - png_isaligned(dp, png_uint_16) && - png_isaligned(sp, png_uint_16) && - bytes_to_copy % sizeof (png_uint_16) == 0 && - bytes_to_jump % sizeof (png_uint_16) == 0) + if (bytes_to_copy < 16 /*else use memcpy*/ && + png_isaligned(dp, png_uint_16) && + png_isaligned(sp, png_uint_16) && + bytes_to_copy % (sizeof (png_uint_16)) == 0 && + bytes_to_jump % (sizeof (png_uint_16)) == 0) { /* Everything is aligned for png_uint_16 copies, but try for * png_uint_32 first. */ - if (png_isaligned(dp, png_uint_32) && - png_isaligned(sp, png_uint_32) && - bytes_to_copy % sizeof (png_uint_32) == 0 && - bytes_to_jump % sizeof (png_uint_32) == 0) + if (png_isaligned(dp, png_uint_32) != 0 && + png_isaligned(sp, png_uint_32) != 0 && + bytes_to_copy % (sizeof (png_uint_32)) == 0 && + bytes_to_jump % (sizeof (png_uint_32)) == 0) { - png_uint_32p dp32 = (png_uint_32p)dp; - png_const_uint_32p sp32 = (png_const_uint_32p)sp; - unsigned int skip = (bytes_to_jump-bytes_to_copy) / - sizeof (png_uint_32); + png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); + png_const_uint_32p sp32 = png_aligncastconst( + png_const_uint_32p, sp); + size_t skip = (bytes_to_jump-bytes_to_copy) / + (sizeof (png_uint_32)); do { @@ -3160,7 +3404,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) do { *dp32++ = *sp32++; - c -= sizeof (png_uint_32); + c -= (sizeof (png_uint_32)); } while (c > 0); @@ -3190,10 +3434,11 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) */ else { - png_uint_16p dp16 = (png_uint_16p)dp; - png_const_uint_16p sp16 = (png_const_uint_16p)sp; - unsigned int skip = (bytes_to_jump-bytes_to_copy) / - sizeof (png_uint_16); + png_uint_16p dp16 = png_aligncast(png_uint_16p, dp); + png_const_uint_16p sp16 = png_aligncastconst( + png_const_uint_16p, sp); + size_t skip = (bytes_to_jump-bytes_to_copy) / + (sizeof (png_uint_16)); do { @@ -3201,7 +3446,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) do { *dp16++ = *sp16++; - c -= sizeof (png_uint_16); + c -= (sizeof (png_uint_16)); } while (c > 0); @@ -3223,12 +3468,12 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) return; } } -#endif /* PNG_ALIGN_ code */ +#endif /* ALIGN_TYPE code */ - /* The true default - use a png_memcpy: */ + /* The true default - use a memcpy: */ for (;;) { - png_memcpy(dp, sp, bytes_to_copy); + memcpy(dp, sp, bytes_to_copy); if (row_width <= bytes_to_jump) return; @@ -3237,7 +3482,7 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) dp += bytes_to_jump; row_width -= bytes_to_jump; if (bytes_to_copy > row_width) - bytes_to_copy = row_width; + bytes_to_copy = (unsigned int)/*SAFE*/row_width; } } @@ -3247,13 +3492,13 @@ png_combine_row(png_structp png_ptr, png_bytep dp, int display) /* Here if pixel_depth < 8 to check 'end_ptr' below. */ } else -#endif +#endif /* READ_INTERLACING */ - /* If here then the switch above wasn't used so just png_memcpy the whole row + /* If here then the switch above wasn't used so just memcpy the whole row * from the temporary row buffer (notice that this overwrites the end of the * destination row if it is a partial byte.) */ - png_memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); + memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); /* Restore the overwritten bits from the last byte if necessary. */ if (end_ptr != NULL) @@ -3290,7 +3535,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, int j; #ifdef PNG_READ_PACKSWAP_SUPPORTED - if (transformations & PNG_PACKSWAP) + if ((transformations & PNG_PACKSWAP) != 0) { sshift = (int)((row_info->width + 7) & 0x07); dshift = (int)((final_width + 7) & 0x07); @@ -3314,8 +3559,9 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, v = (png_byte)((*sp >> sshift) & 0x01); for (j = 0; j < jstop; j++) { - *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff); - *dp |= (png_byte)(v << dshift); + unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); + tmp |= v << dshift; + *dp = (png_byte)(tmp & 0xff); if (dshift == s_end) { @@ -3349,7 +3595,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, png_uint_32 i; #ifdef PNG_READ_PACKSWAP_SUPPORTED - if (transformations & PNG_PACKSWAP) + if ((transformations & PNG_PACKSWAP) != 0) { sshift = (int)(((row_info->width + 3) & 0x03) << 1); dshift = (int)(((final_width + 3) & 0x03) << 1); @@ -3376,8 +3622,9 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, v = (png_byte)((*sp >> sshift) & 0x03); for (j = 0; j < jstop; j++) { - *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff); - *dp |= (png_byte)(v << dshift); + unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); + tmp |= v << dshift; + *dp = (png_byte)(tmp & 0xff); if (dshift == s_end) { @@ -3411,7 +3658,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, int jstop = png_pass_inc[pass]; #ifdef PNG_READ_PACKSWAP_SUPPORTED - if (transformations & PNG_PACKSWAP) + if ((transformations & PNG_PACKSWAP) != 0) { sshift = (int)(((row_info->width + 1) & 0x01) << 2); dshift = (int)(((final_width + 1) & 0x01) << 2); @@ -3437,8 +3684,9 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, for (j = 0; j < jstop; j++) { - *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff); - *dp |= (png_byte)(v << dshift); + unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); + tmp |= v << dshift; + *dp = (png_byte)(tmp & 0xff); if (dshift == s_end) { @@ -3476,14 +3724,14 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, for (i = 0; i < row_info->width; i++) { - png_byte v[8]; + png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */ int j; - png_memcpy(v, sp, pixel_bytes); + memcpy(v, sp, pixel_bytes); for (j = 0; j < jstop; j++) { - png_memcpy(dp, v, pixel_bytes); + memcpy(dp, v, pixel_bytes); dp -= pixel_bytes; } @@ -3500,7 +3748,7 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, PNG_UNUSED(transformations) /* Silence compiler warning */ #endif } -#endif /* PNG_READ_INTERLACING_SUPPORTED */ +#endif /* READ_INTERLACING */ static void png_read_filter_row_sub(png_row_infop row_info, png_bytep row, @@ -3586,15 +3834,15 @@ png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, p = b - c; pc = a - c; -# ifdef PNG_USE_ABS - pa = abs(p); - pb = abs(pc); - pc = abs(p + pc); -# else - pa = p < 0 ? -p : p; - pb = pc < 0 ? -pc : pc; - pc = (p + pc) < 0 ? -(p + pc) : p + pc; -# endif +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif /* Find the best predictor, the least of pa, pb, pc favoring the earlier * ones in the case of a tie. @@ -3641,87 +3889,35 @@ png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, p = b - c; pc = a - c; -# ifdef PNG_USE_ABS - pa = abs(p); - pb = abs(pc); - pc = abs(p + pc); -# else - pa = p < 0 ? -p : p; - pb = pc < 0 ? -pc : pc; - pc = (p + pc) < 0 ? -(p + pc) : p + pc; -# endif +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; +#endif if (pb < pa) pa = pb, a = b; if (pc < pa) a = c; - c = b; a += *row; *row++ = (png_byte)a; } } -#ifdef PNG_ARM_NEON - -#if defined __linux__ && !defined __ANDROID__ -#include -#include -#include - -static int png_have_hwcap(unsigned cap) -{ - FILE *f = fopen("/proc/self/auxv", "r"); - Elf32_auxv_t aux; - int have_cap = 0; - - if (!f) - return 0; - - while (fread(&aux, sizeof(aux), 1, f) > 0) - { - if (aux.a_type == AT_HWCAP && - aux.a_un.a_val & cap) - { - have_cap = 1; - break; - } - } - - fclose(f); - - return have_cap; -} -#endif /* __linux__ */ - static void -png_init_filter_functions_neon(png_structp pp, unsigned int bpp) -{ -#if defined __linux__ && !defined __ANDROID__ - if (!png_have_hwcap(HWCAP_NEON)) - return; -#endif - - pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up_neon; - - if (bpp == 3) - { - pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub3_neon; - pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg3_neon; - pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = - png_read_filter_row_paeth3_neon; - } - - else if (bpp == 4) - { - pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub4_neon; - pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg4_neon; - pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = - png_read_filter_row_paeth4_neon; - } -} -#endif /* PNG_ARM_NEON */ - -static void -png_init_filter_functions(png_structp pp) +png_init_filter_functions(png_structrp pp) + /* This function is called once for every PNG image (except for PNG images + * that only use PNG_FILTER_VALUE_NONE for all rows) to set the + * implementations required to reverse the filtering of PNG rows. Reversing + * the filter is the first transformation performed on the row data. It is + * performed in place, therefore an implementation can be selected based on + * the image pixel format. If the implementation depends on image width then + * take care to ensure that it works correctly if the image is interlaced - + * interlacing causes the actual row width to vary. + */ { unsigned int bpp = (pp->pixel_depth + 7) >> 3; @@ -3735,26 +3931,217 @@ png_init_filter_functions(png_structp pp) pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = png_read_filter_row_paeth_multibyte_pixel; -#ifdef PNG_ARM_NEON - png_init_filter_functions_neon(pp, bpp); +#ifdef PNG_FILTER_OPTIMIZATIONS + /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to + * call to install hardware optimizations for the above functions; simply + * replace whatever elements of the pp->read_filter[] array with a hardware + * specific (or, for that matter, generic) optimization. + * + * To see an example of this examine what configure.ac does when + * --enable-arm-neon is specified on the command line. + */ + PNG_FILTER_OPTIMIZATIONS(pp, bpp); #endif } void /* PRIVATE */ -png_read_filter_row(png_structp pp, png_row_infop row_info, png_bytep row, +png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, png_const_bytep prev_row, int filter) { - if (pp->read_filter[0] == NULL) - png_init_filter_functions(pp); + /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define + * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic + * implementations. See png_init_filter_functions above. + */ if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) + { + if (pp->read_filter[0] == NULL) + png_init_filter_functions(pp); + pp->read_filter[filter-1](row_info, row, prev_row); + } } #ifdef PNG_SEQUENTIAL_READ_SUPPORTED void /* PRIVATE */ -png_read_finish_row(png_structp png_ptr) +png_read_IDAT_data(png_structrp png_ptr, png_bytep output, + png_alloc_size_t avail_out) +{ + /* Loop reading IDATs and decompressing the result into output[avail_out] */ + png_ptr->zstream.next_out = output; + png_ptr->zstream.avail_out = 0; /* safety: set below */ + + if (output == NULL) + avail_out = 0; + + do + { + int ret; + png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; + + if (png_ptr->zstream.avail_in == 0) + { + uInt avail_in; + png_bytep buffer; + + while (png_ptr->idat_size == 0) + { + png_crc_finish(png_ptr, 0); + + png_ptr->idat_size = png_read_chunk_header(png_ptr); + /* This is an error even in the 'check' case because the code just + * consumed a non-IDAT header. + */ + if (png_ptr->chunk_name != png_IDAT) + png_error(png_ptr, "Not enough image data"); + } + + avail_in = png_ptr->IDAT_read_size; + + if (avail_in > png_ptr->idat_size) + avail_in = (uInt)png_ptr->idat_size; + + /* A PNG with a gradually increasing IDAT size will defeat this attempt + * to minimize memory usage by causing lots of re-allocs, but + * realistically doing IDAT_read_size re-allocs is not likely to be a + * big problem. + */ + buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); + + png_crc_read(png_ptr, buffer, avail_in); + png_ptr->idat_size -= avail_in; + + png_ptr->zstream.next_in = buffer; + png_ptr->zstream.avail_in = avail_in; + } + + /* And set up the output side. */ + if (output != NULL) /* standard read */ + { + uInt out = ZLIB_IO_MAX; + + if (out > avail_out) + out = (uInt)avail_out; + + avail_out -= out; + png_ptr->zstream.avail_out = out; + } + + else /* after last row, checking for end */ + { + png_ptr->zstream.next_out = tmpbuf; + png_ptr->zstream.avail_out = (sizeof tmpbuf); + } + + /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the + * process. If the LZ stream is truncated the sequential reader will + * terminally damage the stream, above, by reading the chunk header of the + * following chunk (it then exits with png_error). + * + * TODO: deal more elegantly with truncated IDAT lists. + */ + ret = inflate(&png_ptr->zstream, Z_NO_FLUSH); + + /* Take the unconsumed output back. */ + if (output != NULL) + avail_out += png_ptr->zstream.avail_out; + + else /* avail_out counts the extra bytes */ + avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; + + png_ptr->zstream.avail_out = 0; + + if (ret == Z_STREAM_END) + { + /* Do this for safety; we won't read any more into this row. */ + png_ptr->zstream.next_out = NULL; + + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; + + if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) + png_chunk_benign_error(png_ptr, "Extra compressed data"); + break; + } + + if (ret != Z_OK) + { + png_zstream_error(png_ptr, ret); + + if (output != NULL) + png_chunk_error(png_ptr, png_ptr->zstream.msg); + + else /* checking */ + { + png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); + return; + } + } + } while (avail_out > 0); + + if (avail_out > 0) + { + /* The stream ended before the image; this is the same as too few IDATs so + * should be handled the same way. + */ + if (output != NULL) + png_error(png_ptr, "Not enough image data"); + + else /* the deflate stream contained extra data */ + png_chunk_benign_error(png_ptr, "Too much image data"); + } +} + +void /* PRIVATE */ +png_read_finish_IDAT(png_structrp png_ptr) +{ + /* We don't need any more data and the stream should have ended, however the + * LZ end code may actually not have been processed. In this case we must + * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk + * may still remain to be consumed. + */ + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) + { + /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in + * the compressed stream, but the stream may be damaged too, so even after + * this call we may need to terminate the zstream ownership. + */ + png_read_IDAT_data(png_ptr, NULL, 0); + png_ptr->zstream.next_out = NULL; /* safety */ + + /* Now clear everything out for safety; the following may not have been + * done. + */ + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) + { + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; + } + } + + /* If the zstream has not been released do it now *and* terminate the reading + * of the final IDAT chunk. + */ + if (png_ptr->zowner == png_IDAT) + { + /* Always do this; the pointers otherwise point into the read buffer. */ + png_ptr->zstream.next_in = NULL; + png_ptr->zstream.avail_in = 0; + + /* Now we no longer own the zstream. */ + png_ptr->zowner = 0; + + /* The slightly weird semantics of the sequential IDAT reading is that we + * are always in or at the end of an IDAT chunk, so we always need to do a + * crc_finish here. If idat_size is non-zero we also need to read the + * spurious bytes at the end of the chunk now. + */ + (void)png_crc_finish(png_ptr, png_ptr->idat_size); + } +} + +void /* PRIVATE */ +png_read_finish_row(png_structrp png_ptr) { -#ifdef PNG_READ_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ @@ -3768,22 +4155,20 @@ png_read_finish_row(png_structp png_ptr) /* Offset to next interlace block in the y direction */ static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; -#endif /* PNG_READ_INTERLACING_SUPPORTED */ png_debug(1, "in png_read_finish_row"); png_ptr->row_number++; if (png_ptr->row_number < png_ptr->num_rows) return; -#ifdef PNG_READ_INTERLACING_SUPPORTED - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) { png_ptr->row_number = 0; /* TO DO: don't do this if prev_row isn't needed (requires * read-ahead of the next row's filter byte. */ - png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); + memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); do { @@ -3797,7 +4182,7 @@ png_read_finish_row(png_structp png_ptr) png_pass_start[png_ptr->pass]) / png_pass_inc[png_ptr->pass]; - if (!(png_ptr->transformations & PNG_INTERLACE)) + if ((png_ptr->transformations & PNG_INTERLACE) == 0) { png_ptr->num_rows = (png_ptr->height + png_pass_yinc[png_ptr->pass] - 1 - @@ -3813,80 +4198,15 @@ png_read_finish_row(png_structp png_ptr) if (png_ptr->pass < 7) return; } -#endif /* PNG_READ_INTERLACING_SUPPORTED */ - if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) - { - char extra; - int ret; - - png_ptr->zstream.next_out = (Byte *)&extra; - png_ptr->zstream.avail_out = (uInt)1; - - for (;;) - { - if (!(png_ptr->zstream.avail_in)) - { - while (!png_ptr->idat_size) - { - png_crc_finish(png_ptr, 0); - png_ptr->idat_size = png_read_chunk_header(png_ptr); - if (png_ptr->chunk_name != png_IDAT) - png_error(png_ptr, "Not enough image data"); - } - - png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; - png_ptr->zstream.next_in = png_ptr->zbuf; - - if (png_ptr->zbuf_size > png_ptr->idat_size) - png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; - - png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); - png_ptr->idat_size -= png_ptr->zstream.avail_in; - } - - ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); - - if (ret == Z_STREAM_END) - { - if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || - png_ptr->idat_size) - png_warning(png_ptr, "Extra compressed data"); - - png_ptr->mode |= PNG_AFTER_IDAT; - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; - break; - } - - if (ret != Z_OK) - png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : - "Decompression Error"); - - if (!(png_ptr->zstream.avail_out)) - { - png_warning(png_ptr, "Extra compressed data"); - png_ptr->mode |= PNG_AFTER_IDAT; - png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; - break; - } - - } - png_ptr->zstream.avail_out = 0; - } - - if (png_ptr->idat_size || png_ptr->zstream.avail_in) - png_warning(png_ptr, "Extra compression data"); - - inflateReset(&png_ptr->zstream); - - png_ptr->mode |= PNG_AFTER_IDAT; + /* Here after at the end of the last row of the last pass. */ + png_read_finish_IDAT(png_ptr); } -#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */ +#endif /* SEQUENTIAL_READ */ void /* PRIVATE */ -png_read_start_row(png_structp png_ptr) +png_read_start_row(png_structrp png_ptr) { -#ifdef PNG_READ_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ /* Start of interlace block */ @@ -3900,20 +4220,18 @@ png_read_start_row(png_structp png_ptr) /* Offset to next interlace block in the y direction */ static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; -#endif int max_pixel_depth; png_size_t row_bytes; png_debug(1, "in png_read_start_row"); - png_ptr->zstream.avail_in = 0; + #ifdef PNG_READ_TRANSFORMS_SUPPORTED png_init_read_transformations(png_ptr); #endif -#ifdef PNG_READ_INTERLACING_SUPPORTED - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) { - if (!(png_ptr->transformations & PNG_INTERLACE)) + if ((png_ptr->transformations & PNG_INTERLACE) == 0) png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - png_pass_ystart[0]) / png_pass_yinc[0]; @@ -3927,7 +4245,6 @@ png_read_start_row(png_structp png_ptr) } else -#endif /* PNG_READ_INTERLACING_SUPPORTED */ { png_ptr->num_rows = png_ptr->height; png_ptr->iwidth = png_ptr->width; @@ -3935,7 +4252,7 @@ png_read_start_row(png_structp png_ptr) max_pixel_depth = png_ptr->pixel_depth; - /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpliar set of + /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of * calculations to calculate the final pixel depth, then * png_do_read_transforms actually does the transforms. This means that the * code which effectively calculates this value is actually repeated in three @@ -3946,16 +4263,16 @@ png_read_start_row(png_structp png_ptr) * TODO: fix this. */ #ifdef PNG_READ_PACK_SUPPORTED - if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8) + if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8) max_pixel_depth = 8; #endif #ifdef PNG_READ_EXPAND_SUPPORTED - if (png_ptr->transformations & PNG_EXPAND) + if ((png_ptr->transformations & PNG_EXPAND) != 0) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { - if (png_ptr->num_trans) + if (png_ptr->num_trans != 0) max_pixel_depth = 32; else @@ -3967,13 +4284,13 @@ png_read_start_row(png_structp png_ptr) if (max_pixel_depth < 8) max_pixel_depth = 8; - if (png_ptr->num_trans) + if (png_ptr->num_trans != 0) max_pixel_depth *= 2; } else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) { - if (png_ptr->num_trans) + if (png_ptr->num_trans != 0) { max_pixel_depth *= 4; max_pixel_depth /= 3; @@ -3983,25 +4300,25 @@ png_read_start_row(png_structp png_ptr) #endif #ifdef PNG_READ_EXPAND_16_SUPPORTED - if (png_ptr->transformations & PNG_EXPAND_16) + if ((png_ptr->transformations & PNG_EXPAND_16) != 0) { -# ifdef PNG_READ_EXPAND_SUPPORTED - /* In fact it is an error if it isn't supported, but checking is - * the safe way. - */ - if (png_ptr->transformations & PNG_EXPAND) - { - if (png_ptr->bit_depth < 16) - max_pixel_depth *= 2; - } - else -# endif - png_ptr->transformations &= ~PNG_EXPAND_16; +# ifdef PNG_READ_EXPAND_SUPPORTED + /* In fact it is an error if it isn't supported, but checking is + * the safe way. + */ + if ((png_ptr->transformations & PNG_EXPAND) != 0) + { + if (png_ptr->bit_depth < 16) + max_pixel_depth *= 2; + } + else +# endif + png_ptr->transformations &= ~PNG_EXPAND_16; } #endif #ifdef PNG_READ_FILLER_SUPPORTED - if (png_ptr->transformations & (PNG_FILLER)) + if ((png_ptr->transformations & (PNG_FILLER)) != 0) { if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) { @@ -4025,14 +4342,15 @@ png_read_start_row(png_structp png_ptr) #endif #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED - if (png_ptr->transformations & PNG_GRAY_TO_RGB) + if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) { if ( #ifdef PNG_READ_EXPAND_SUPPORTED - (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) || + (png_ptr->num_trans != 0 && + (png_ptr->transformations & PNG_EXPAND) != 0) || #endif #ifdef PNG_READ_FILLER_SUPPORTED - (png_ptr->transformations & (PNG_FILLER)) || + (png_ptr->transformations & (PNG_FILLER)) != 0 || #endif png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { @@ -4065,7 +4383,7 @@ png_read_start_row(png_structp png_ptr) #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) - if (png_ptr->transformations & PNG_USER_TRANSFORM) + if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) { int user_pixel_depth = png_ptr->user_transform_depth * png_ptr->user_transform_channels; @@ -4101,7 +4419,7 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) png_free(png_ptr, png_ptr->big_row_buf); png_free(png_ptr, png_ptr->big_prev_row); - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, row_bytes + 48); @@ -4144,7 +4462,7 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) png_error(png_ptr, "Row has too many bytes to allocate in memory"); - png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); + memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); png_debug1(3, "width = %u,", png_ptr->width); png_debug1(3, "height = %u,", png_ptr->height); @@ -4154,6 +4472,27 @@ defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) png_debug1(3, "irowbytes = %lu", (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); + /* The sequential reader needs a buffer for IDAT, but the progressive reader + * does not, so free the read buffer now regardless; the sequential reader + * reallocates it on demand. + */ + if (png_ptr->read_buffer != 0) + { + png_bytep buffer = png_ptr->read_buffer; + + png_ptr->read_buffer_size = 0; + png_ptr->read_buffer = NULL; + png_free(png_ptr, buffer); + } + + /* Finally claim the zstream for the inflate of the IDAT data, use the bits + * value from the stream (note that this will result in a fatal error if the + * IDAT stream has a bogus deflate header window_bits value, but this should + * not be happening any longer!) + */ + if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg); + png_ptr->flags |= PNG_FLAG_ROW_INIT; } -#endif /* PNG_READ_SUPPORTED */ +#endif /* READ */ diff --git a/3rdparty/libpng/pngset.c b/3rdparty/libpng/pngset.c index 8c07eec3e..05a2134db 100644 --- a/3rdparty/libpng/pngset.c +++ b/3rdparty/libpng/pngset.c @@ -1,8 +1,8 @@ /* pngset.c - storage of image information into info struct * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -22,50 +22,51 @@ #ifdef PNG_bKGD_SUPPORTED void PNGAPI -png_set_bKGD(png_structp png_ptr, png_infop info_ptr, +png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, png_const_color_16p background) { png_debug1(1, "in %s storage function", "bKGD"); - if (png_ptr == NULL || info_ptr == NULL) + if (png_ptr == NULL || info_ptr == NULL || background == NULL) return; - png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16)); + info_ptr->background = *background; info_ptr->valid |= PNG_INFO_bKGD; } #endif #ifdef PNG_cHRM_SUPPORTED void PNGFAPI -png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr, +png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr, png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, png_fixed_point blue_y) { + png_xy xy; + png_debug1(1, "in %s storage function", "cHRM fixed"); if (png_ptr == NULL || info_ptr == NULL) return; -# ifdef PNG_CHECK_cHRM_SUPPORTED - if (png_check_cHRM_fixed(png_ptr, - white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y)) -# endif - { - info_ptr->x_white = white_x; - info_ptr->y_white = white_y; - info_ptr->x_red = red_x; - info_ptr->y_red = red_y; - info_ptr->x_green = green_x; - info_ptr->y_green = green_y; - info_ptr->x_blue = blue_x; - info_ptr->y_blue = blue_y; - info_ptr->valid |= PNG_INFO_cHRM; - } + xy.redx = red_x; + xy.redy = red_y; + xy.greenx = green_x; + xy.greeny = green_y; + xy.bluex = blue_x; + xy.bluey = blue_y; + xy.whitex = white_x; + xy.whitey = white_y; + + if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy, + 2/* override with app values*/) != 0) + info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; + + png_colorspace_sync_info(png_ptr, info_ptr); } void PNGFAPI -png_set_cHRM_XYZ_fixed(png_structp png_ptr, png_infop info_ptr, +png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr, png_fixed_point int_red_X, png_fixed_point int_red_Y, png_fixed_point int_red_Z, png_fixed_point int_green_X, png_fixed_point int_green_Y, png_fixed_point int_green_Z, @@ -73,33 +74,32 @@ png_set_cHRM_XYZ_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point int_blue_Z) { png_XYZ XYZ; - png_xy xy; png_debug1(1, "in %s storage function", "cHRM XYZ fixed"); if (png_ptr == NULL || info_ptr == NULL) return; - XYZ.redX = int_red_X; - XYZ.redY = int_red_Y; - XYZ.redZ = int_red_Z; - XYZ.greenX = int_green_X; - XYZ.greenY = int_green_Y; - XYZ.greenZ = int_green_Z; - XYZ.blueX = int_blue_X; - XYZ.blueY = int_blue_Y; - XYZ.blueZ = int_blue_Z; + XYZ.red_X = int_red_X; + XYZ.red_Y = int_red_Y; + XYZ.red_Z = int_red_Z; + XYZ.green_X = int_green_X; + XYZ.green_Y = int_green_Y; + XYZ.green_Z = int_green_Z; + XYZ.blue_X = int_blue_X; + XYZ.blue_Y = int_blue_Y; + XYZ.blue_Z = int_blue_Z; - if (png_xy_from_XYZ(&xy, XYZ)) - png_error(png_ptr, "XYZ values out of representable range"); + if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace, + &XYZ, 2) != 0) + info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; - png_set_cHRM_fixed(png_ptr, info_ptr, xy.whitex, xy.whitey, xy.redx, xy.redy, - xy.greenx, xy.greeny, xy.bluex, xy.bluey); + png_colorspace_sync_info(png_ptr, info_ptr); } # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_cHRM(png_structp png_ptr, png_infop info_ptr, +png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y) { @@ -115,7 +115,7 @@ png_set_cHRM(png_structp png_ptr, png_infop info_ptr, } void PNGAPI -png_set_cHRM_XYZ(png_structp png_ptr, png_infop info_ptr, double red_X, +png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X, double red_Y, double red_Z, double green_X, double green_Y, double green_Z, double blue_X, double blue_Y, double blue_Z) { @@ -123,48 +123,34 @@ png_set_cHRM_XYZ(png_structp png_ptr, png_infop info_ptr, double red_X, png_fixed(png_ptr, red_X, "cHRM Red X"), png_fixed(png_ptr, red_Y, "cHRM Red Y"), png_fixed(png_ptr, red_Z, "cHRM Red Z"), - png_fixed(png_ptr, green_X, "cHRM Red X"), - png_fixed(png_ptr, green_Y, "cHRM Red Y"), - png_fixed(png_ptr, green_Z, "cHRM Red Z"), - png_fixed(png_ptr, blue_X, "cHRM Red X"), - png_fixed(png_ptr, blue_Y, "cHRM Red Y"), - png_fixed(png_ptr, blue_Z, "cHRM Red Z")); + png_fixed(png_ptr, green_X, "cHRM Green X"), + png_fixed(png_ptr, green_Y, "cHRM Green Y"), + png_fixed(png_ptr, green_Z, "cHRM Green Z"), + png_fixed(png_ptr, blue_X, "cHRM Blue X"), + png_fixed(png_ptr, blue_Y, "cHRM Blue Y"), + png_fixed(png_ptr, blue_Z, "cHRM Blue Z")); } -# endif /* PNG_FLOATING_POINT_SUPPORTED */ +# endif /* FLOATING_POINT */ -#endif /* PNG_cHRM_SUPPORTED */ +#endif /* cHRM */ #ifdef PNG_gAMA_SUPPORTED void PNGFAPI -png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point - file_gamma) +png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr, + png_fixed_point file_gamma) { png_debug1(1, "in %s storage function", "gAMA"); if (png_ptr == NULL || info_ptr == NULL) return; - /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't - * occur. Since the fixed point representation is assymetrical it is - * possible for 1/gamma to overflow the limit of 21474 and this means the - * gamma value must be at least 5/100000 and hence at most 20000.0. For - * safety the limits here are a little narrower. The values are 0.00016 to - * 6250.0, which are truly ridiculous gamma values (and will produce - * displays that are all black or all white.) - */ - if (file_gamma < 16 || file_gamma > 625000000) - png_warning(png_ptr, "Out of range gamma value ignored"); - - else - { - info_ptr->gamma = file_gamma; - info_ptr->valid |= PNG_INFO_gAMA; - } + png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma); + png_colorspace_sync_info(png_ptr, info_ptr); } # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma) +png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma) { png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma, "png_set_gAMA")); @@ -174,7 +160,8 @@ png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma) #ifdef PNG_hIST_SUPPORTED void PNGAPI -png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_const_uint_16p hist) +png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr, + png_const_uint_16p hist) { int i; @@ -197,26 +184,27 @@ png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_const_uint_16p hist) /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in * version 1.2.1 */ - png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr, - PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)); + info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr, + PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16)))); - if (png_ptr->hist == NULL) + if (info_ptr->hist == NULL) { png_warning(png_ptr, "Insufficient memory for hIST chunk data"); + return; } - for (i = 0; i < info_ptr->num_palette; i++) - png_ptr->hist[i] = hist[i]; - - info_ptr->hist = png_ptr->hist; - info_ptr->valid |= PNG_INFO_hIST; info_ptr->free_me |= PNG_FREE_HIST; + + for (i = 0; i < info_ptr->num_palette; i++) + info_ptr->hist[i] = hist[i]; + + info_ptr->valid |= PNG_INFO_hIST; } #endif void PNGAPI -png_set_IHDR(png_structp png_ptr, png_infop info_ptr, +png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type) @@ -241,32 +229,23 @@ png_set_IHDR(png_structp png_ptr, png_infop info_ptr, if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) info_ptr->channels = 1; - else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) + else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) info_ptr->channels = 3; else info_ptr->channels = 1; - if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) + if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) info_ptr->channels++; info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); - /* Check for potential overflow */ - if (width > - (PNG_UINT_32_MAX >> 3) /* 8-byte RRGGBBAA pixels */ - - 48 /* bigrowbuf hack */ - - 1 /* filter byte */ - - 7*8 /* rounding of width to multiple of 8 pixels */ - - 8) /* extra max_pixel_depth pad */ - info_ptr->rowbytes = 0; - else - info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width); + info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width); } #ifdef PNG_oFFs_SUPPORTED void PNGAPI -png_set_oFFs(png_structp png_ptr, png_infop info_ptr, +png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr, png_int_32 offset_x, png_int_32 offset_y, int unit_type) { png_debug1(1, "in %s storage function", "oFFs"); @@ -283,7 +262,7 @@ png_set_oFFs(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_pCAL_SUPPORTED void PNGAPI -png_set_pCAL(png_structp png_ptr, png_infop info_ptr, +png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr, png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_const_charp units, png_charpp params) { @@ -292,10 +271,11 @@ png_set_pCAL(png_structp png_ptr, png_infop info_ptr, png_debug1(1, "in %s storage function", "pCAL"); - if (png_ptr == NULL || info_ptr == NULL) + if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL + || (nparams > 0 && params == NULL)) return; - length = png_strlen(purpose) + 1; + length = strlen(purpose) + 1; png_debug1(3, "allocating purpose for info (%lu bytes)", (unsigned long)length); @@ -305,20 +285,28 @@ png_set_pCAL(png_structp png_ptr, png_infop info_ptr, if (type < 0 || type > 3) png_error(png_ptr, "Invalid pCAL equation type"); + if (nparams < 0 || nparams > 255) + png_error(png_ptr, "Invalid pCAL parameter count"); + /* Validate params[nparams] */ for (i=0; ipcal_purpose = (png_charp)png_malloc_warn(png_ptr, length); + info_ptr->pcal_purpose = png_voidcast(png_charp, + png_malloc_warn(png_ptr, length)); if (info_ptr->pcal_purpose == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL purpose"); + return; } - png_memcpy(info_ptr->pcal_purpose, purpose, length); + memcpy(info_ptr->pcal_purpose, purpose, length); png_debug(3, "storing X0, X1, type, and nparams in info"); info_ptr->pcal_X0 = X0; @@ -326,34 +314,37 @@ png_set_pCAL(png_structp png_ptr, png_infop info_ptr, info_ptr->pcal_type = (png_byte)type; info_ptr->pcal_nparams = (png_byte)nparams; - length = png_strlen(units) + 1; + length = strlen(units) + 1; png_debug1(3, "allocating units for info (%lu bytes)", (unsigned long)length); - info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length); + info_ptr->pcal_units = png_voidcast(png_charp, + png_malloc_warn(png_ptr, length)); if (info_ptr->pcal_units == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL units"); + return; } - png_memcpy(info_ptr->pcal_units, units, length); + memcpy(info_ptr->pcal_units, units, length); - info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr, - (png_size_t)((nparams + 1) * png_sizeof(png_charp))); + info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, + (png_size_t)((nparams + 1) * (sizeof (png_charp))))); if (info_ptr->pcal_params == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL params"); + return; } - png_memset(info_ptr->pcal_params, 0, (nparams + 1) * png_sizeof(png_charp)); + memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp))); for (i = 0; i < nparams; i++) { - length = png_strlen(params[i]) + 1; + length = strlen(params[i]) + 1; png_debug2(3, "allocating parameter %d for info (%lu bytes)", i, (unsigned long)length); @@ -362,10 +353,11 @@ png_set_pCAL(png_structp png_ptr, png_infop info_ptr, if (info_ptr->pcal_params[i] == NULL) { png_warning(png_ptr, "Insufficient memory for pCAL parameter"); + return; } - png_memcpy(info_ptr->pcal_params[i], params[i], length); + memcpy(info_ptr->pcal_params[i], params[i], length); } info_ptr->valid |= PNG_INFO_pCAL; @@ -375,7 +367,7 @@ png_set_pCAL(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_sCAL_SUPPORTED void PNGAPI -png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, +png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr, int unit, png_const_charp swidth, png_const_charp sheight) { png_size_t lengthw = 0, lengthh = 0; @@ -391,11 +383,11 @@ png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, if (unit != 1 && unit != 2) png_error(png_ptr, "Invalid sCAL unit"); - if (swidth == NULL || (lengthw = png_strlen(swidth)) == 0 || + if (swidth == NULL || (lengthw = strlen(swidth)) == 0 || swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw)) png_error(png_ptr, "Invalid sCAL width"); - if (sheight == NULL || (lengthh = png_strlen(sheight)) == 0 || + if (sheight == NULL || (lengthh = strlen(sheight)) == 0 || sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh)) png_error(png_ptr, "Invalid sCAL height"); @@ -405,21 +397,24 @@ png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw); - info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, lengthw); + info_ptr->scal_s_width = png_voidcast(png_charp, + png_malloc_warn(png_ptr, lengthw)); if (info_ptr->scal_s_width == NULL) { png_warning(png_ptr, "Memory allocation failed while processing sCAL"); + return; } - png_memcpy(info_ptr->scal_s_width, swidth, lengthw); + memcpy(info_ptr->scal_s_width, swidth, lengthw); ++lengthh; png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh); - info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, lengthh); + info_ptr->scal_s_height = png_voidcast(png_charp, + png_malloc_warn(png_ptr, lengthh)); if (info_ptr->scal_s_height == NULL) { @@ -427,10 +422,11 @@ png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, info_ptr->scal_s_width = NULL; png_warning(png_ptr, "Memory allocation failed while processing sCAL"); + return; } - png_memcpy(info_ptr->scal_s_height, sheight, lengthh); + memcpy(info_ptr->scal_s_height, sheight, lengthh); info_ptr->valid |= PNG_INFO_sCAL; info_ptr->free_me |= PNG_FREE_SCAL; @@ -438,8 +434,8 @@ png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr, # ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width, - double height) +png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit, + double width, double height) { png_debug1(1, "in %s storage function", "sCAL"); @@ -456,9 +452,9 @@ png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width, char swidth[PNG_sCAL_MAX_DIGITS+1]; char sheight[PNG_sCAL_MAX_DIGITS+1]; - png_ascii_from_fp(png_ptr, swidth, sizeof swidth, width, + png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width, PNG_sCAL_PRECISION); - png_ascii_from_fp(png_ptr, sheight, sizeof sheight, height, + png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height, PNG_sCAL_PRECISION); png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight); @@ -468,7 +464,7 @@ png_set_sCAL(png_structp png_ptr, png_infop info_ptr, int unit, double width, # ifdef PNG_FIXED_POINT_SUPPORTED void PNGAPI -png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit, +png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit, png_fixed_point width, png_fixed_point height) { png_debug1(1, "in %s storage function", "sCAL"); @@ -486,8 +482,8 @@ png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit, char swidth[PNG_sCAL_MAX_DIGITS+1]; char sheight[PNG_sCAL_MAX_DIGITS+1]; - png_ascii_from_fixed(png_ptr, swidth, sizeof swidth, width); - png_ascii_from_fixed(png_ptr, sheight, sizeof sheight, height); + png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width); + png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height); png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight); } @@ -497,7 +493,7 @@ png_set_sCAL_fixed(png_structp png_ptr, png_infop info_ptr, int unit, #ifdef PNG_pHYs_SUPPORTED void PNGAPI -png_set_pHYs(png_structp png_ptr, png_infop info_ptr, +png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type) { png_debug1(1, "in %s storage function", "pHYs"); @@ -513,16 +509,21 @@ png_set_pHYs(png_structp png_ptr, png_infop info_ptr, #endif void PNGAPI -png_set_PLTE(png_structp png_ptr, png_infop info_ptr, +png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette) { + png_uint_32 max_palette_length; + png_debug1(1, "in %s storage function", "PLTE"); if (png_ptr == NULL || info_ptr == NULL) return; - if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH) + max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? + (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH; + + if (num_palette < 0 || num_palette > (int) max_palette_length) { if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) png_error(png_ptr, "Invalid palette length"); @@ -530,24 +531,39 @@ png_set_PLTE(png_structp png_ptr, png_infop info_ptr, else { png_warning(png_ptr, "Invalid palette length"); + return; } } + if ((num_palette > 0 && palette == NULL) || + (num_palette == 0 +# ifdef PNG_MNG_FEATURES_SUPPORTED + && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 +# endif + )) + { + png_error(png_ptr, "Invalid palette"); + } + /* It may not actually be necessary to set png_ptr->palette here; * we do it for backward compatibility with the way the png_handle_tRNS * function used to do the allocation. + * + * 1.6.0: the above statement appears to be incorrect; something has to set + * the palette inside png_struct on read. */ png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead - * of num_palette entries, in case of an invalid PNG file that has - * too-large sample values. + * of num_palette entries, in case of an invalid PNG file or incorrect + * call to png_set_PLTE() with too-large sample values. */ - png_ptr->palette = (png_colorp)png_calloc(png_ptr, - PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color)); + png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr, + PNG_MAX_PALETTE_LENGTH * (sizeof (png_color)))); - png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color)); + if (num_palette > 0) + memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color))); info_ptr->palette = png_ptr->palette; info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette; @@ -558,34 +574,34 @@ png_set_PLTE(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_sBIT_SUPPORTED void PNGAPI -png_set_sBIT(png_structp png_ptr, png_infop info_ptr, +png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr, png_const_color_8p sig_bit) { png_debug1(1, "in %s storage function", "sBIT"); - if (png_ptr == NULL || info_ptr == NULL) + if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL) return; - png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8)); + info_ptr->sig_bit = *sig_bit; info_ptr->valid |= PNG_INFO_sBIT; } #endif #ifdef PNG_sRGB_SUPPORTED void PNGAPI -png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int srgb_intent) +png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent) { png_debug1(1, "in %s storage function", "sRGB"); if (png_ptr == NULL || info_ptr == NULL) return; - info_ptr->srgb_intent = (png_byte)srgb_intent; - info_ptr->valid |= PNG_INFO_sRGB; + (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent); + png_colorspace_sync_info(png_ptr, info_ptr); } void PNGAPI -png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr, +png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent) { png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM"); @@ -593,28 +609,22 @@ png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr, if (png_ptr == NULL || info_ptr == NULL) return; - png_set_sRGB(png_ptr, info_ptr, srgb_intent); + if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, + srgb_intent) != 0) + { + /* This causes the gAMA and cHRM to be written too */ + info_ptr->colorspace.flags |= + PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM; + } -# ifdef PNG_gAMA_SUPPORTED - png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE); -# endif - -# ifdef PNG_cHRM_SUPPORTED - png_set_cHRM_fixed(png_ptr, info_ptr, - /* color x y */ - /* white */ 31270, 32900, - /* red */ 64000, 33000, - /* green */ 30000, 60000, - /* blue */ 15000, 6000 - ); -# endif /* cHRM */ + png_colorspace_sync_info(png_ptr, info_ptr); } #endif /* sRGB */ #ifdef PNG_iCCP_SUPPORTED void PNGAPI -png_set_iCCP(png_structp png_ptr, png_infop info_ptr, +png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr, png_const_charp name, int compression_type, png_const_bytep profile, png_uint_32 proflen) { @@ -627,37 +637,60 @@ png_set_iCCP(png_structp png_ptr, png_infop info_ptr, if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL) return; - length = png_strlen(name)+1; - new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length); + if (compression_type != PNG_COMPRESSION_TYPE_BASE) + png_app_error(png_ptr, "Invalid iCCP compression method"); + + /* Set the colorspace first because this validates the profile; do not + * override previously set app cHRM or gAMA here (because likely as not the + * application knows better than libpng what the correct values are.) Pass + * the info_ptr color_type field to png_colorspace_set_ICC because in the + * write case it has not yet been stored in png_ptr. + */ + { + int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name, + proflen, profile, info_ptr->color_type); + + png_colorspace_sync_info(png_ptr, info_ptr); + + /* Don't do any of the copying if the profile was bad, or inconsistent. */ + if (result == 0) + return; + + /* But do write the gAMA and cHRM chunks from the profile. */ + info_ptr->colorspace.flags |= + PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM; + } + + length = strlen(name)+1; + new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length)); if (new_iccp_name == NULL) { - png_warning(png_ptr, "Insufficient memory to process iCCP chunk"); + png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk"); + return; } - png_memcpy(new_iccp_name, name, length); - new_iccp_profile = (png_bytep)png_malloc_warn(png_ptr, proflen); + memcpy(new_iccp_name, name, length); + new_iccp_profile = png_voidcast(png_bytep, + png_malloc_warn(png_ptr, proflen)); if (new_iccp_profile == NULL) { - png_free (png_ptr, new_iccp_name); - png_warning(png_ptr, + png_free(png_ptr, new_iccp_name); + png_benign_error(png_ptr, "Insufficient memory to process iCCP profile"); + return; } - png_memcpy(new_iccp_profile, profile, (png_size_t)proflen); + memcpy(new_iccp_profile, profile, proflen); png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0); info_ptr->iccp_proflen = proflen; info_ptr->iccp_name = new_iccp_name; info_ptr->iccp_profile = new_iccp_profile; - /* Compression is always zero but is here so the API and info structure - * does not have to change if we introduce multiple compression types - */ - info_ptr->iccp_compression = (png_byte)compression_type; info_ptr->free_me |= PNG_FREE_ICCP; info_ptr->valid |= PNG_INFO_iCCP; } @@ -665,82 +698,82 @@ png_set_iCCP(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_TEXT_SUPPORTED void PNGAPI -png_set_text(png_structp png_ptr, png_infop info_ptr, png_const_textp text_ptr, - int num_text) +png_set_text(png_const_structrp png_ptr, png_inforp info_ptr, + png_const_textp text_ptr, int num_text) { int ret; ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text); - if (ret) + if (ret != 0) png_error(png_ptr, "Insufficient memory to store text"); } int /* PRIVATE */ -png_set_text_2(png_structp png_ptr, png_infop info_ptr, +png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr, png_const_textp text_ptr, int num_text) { int i; - png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" : + png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U : (unsigned long)png_ptr->chunk_name); - if (png_ptr == NULL || info_ptr == NULL || num_text == 0) + if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL) return(0); /* Make sure we have enough space in the "text" array in info_struct - * to hold all of the incoming text_ptr objects. + * to hold all of the incoming text_ptr objects. This compare can't overflow + * because max_text >= num_text (anyway, subtract of two positive integers + * can't overflow in any case.) */ - if (info_ptr->num_text + num_text > info_ptr->max_text) + if (num_text > info_ptr->max_text - info_ptr->num_text) { - int old_max_text = info_ptr->max_text; int old_num_text = info_ptr->num_text; + int max_text; + png_textp new_text = NULL; - if (info_ptr->text != NULL) + /* Calculate an appropriate max_text, checking for overflow. */ + max_text = old_num_text; + if (num_text <= INT_MAX - max_text) { - png_textp old_text; + max_text += num_text; - info_ptr->max_text = info_ptr->num_text + num_text + 8; - old_text = info_ptr->text; + /* Round up to a multiple of 8 */ + if (max_text < INT_MAX-8) + max_text = (max_text + 8) & ~0x7; - info_ptr->text = (png_textp)png_malloc_warn(png_ptr, - (png_size_t)(info_ptr->max_text * png_sizeof(png_text))); + else + max_text = INT_MAX; - if (info_ptr->text == NULL) - { - /* Restore to previous condition */ - info_ptr->max_text = old_max_text; - info_ptr->text = old_text; - return(1); - } - - png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text * - png_sizeof(png_text))); - png_free(png_ptr, old_text); + /* Now allocate a new array and copy the old members in; this does all + * the overflow checks. + */ + new_text = png_voidcast(png_textp,png_realloc_array(png_ptr, + info_ptr->text, old_num_text, max_text-old_num_text, + sizeof *new_text)); } - else + if (new_text == NULL) { - info_ptr->max_text = num_text + 8; - info_ptr->num_text = 0; - info_ptr->text = (png_textp)png_malloc_warn(png_ptr, - (png_size_t)(info_ptr->max_text * png_sizeof(png_text))); - if (info_ptr->text == NULL) - { - /* Restore to previous condition */ - info_ptr->num_text = old_num_text; - info_ptr->max_text = old_max_text; - return(1); - } - info_ptr->free_me |= PNG_FREE_TEXT; + png_chunk_report(png_ptr, "too many text chunks", + PNG_CHUNK_WRITE_ERROR); + + return 1; } - png_debug1(3, "allocated %d entries for info_ptr->text", - info_ptr->max_text); + png_free(png_ptr, info_ptr->text); + + info_ptr->text = new_text; + info_ptr->free_me |= PNG_FREE_TEXT; + info_ptr->max_text = max_text; + /* num_text is adjusted below as the entries are copied in */ + + png_debug1(3, "allocated %d entries for info_ptr->text", max_text); } + for (i = 0; i < num_text; i++) { - png_size_t text_length, key_len; - png_size_t lang_len, lang_key_len; + size_t text_length, key_len; + size_t lang_len, lang_key_len; png_textp textp = &(info_ptr->text[info_ptr->num_text]); if (text_ptr[i].key == NULL) @@ -749,11 +782,12 @@ png_set_text_2(png_structp png_ptr, png_infop info_ptr, if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE || text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST) { - png_warning(png_ptr, "text compression mode is out of range"); + png_chunk_report(png_ptr, "text compression mode is out of range", + PNG_CHUNK_WRITE_ERROR); continue; } - key_len = png_strlen(text_ptr[i].key); + key_len = strlen(text_ptr[i].key); if (text_ptr[i].compression <= 0) { @@ -767,20 +801,21 @@ png_set_text_2(png_structp png_ptr, png_infop info_ptr, /* Set iTXt data */ if (text_ptr[i].lang != NULL) - lang_len = png_strlen(text_ptr[i].lang); + lang_len = strlen(text_ptr[i].lang); else lang_len = 0; if (text_ptr[i].lang_key != NULL) - lang_key_len = png_strlen(text_ptr[i].lang_key); + lang_key_len = strlen(text_ptr[i].lang_key); else lang_key_len = 0; } -# else /* PNG_iTXt_SUPPORTED */ +# else /* iTXt */ { - png_warning(png_ptr, "iTXt chunk not supported"); + png_chunk_report(png_ptr, "iTXt chunk not supported", + PNG_CHUNK_WRITE_ERROR); continue; } # endif @@ -799,32 +834,36 @@ png_set_text_2(png_structp png_ptr, png_infop info_ptr, else { - text_length = png_strlen(text_ptr[i].text); + text_length = strlen(text_ptr[i].text); textp->compression = text_ptr[i].compression; } - textp->key = (png_charp)png_malloc_warn(png_ptr, - (png_size_t) - (key_len + text_length + lang_len + lang_key_len + 4)); + textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr, + key_len + text_length + lang_len + lang_key_len + 4)); if (textp->key == NULL) - return(1); + { + png_chunk_report(png_ptr, "text chunk: out of memory", + PNG_CHUNK_WRITE_ERROR); + + return 1; + } png_debug2(2, "Allocated %lu bytes at %p in png_set_text", (unsigned long)(png_uint_32) (key_len + lang_len + lang_key_len + text_length + 4), textp->key); - png_memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len)); + memcpy(textp->key, text_ptr[i].key, key_len); *(textp->key + key_len) = '\0'; if (text_ptr[i].compression > 0) { textp->lang = textp->key + key_len + 1; - png_memcpy(textp->lang, text_ptr[i].lang, lang_len); + memcpy(textp->lang, text_ptr[i].lang, lang_len); *(textp->lang + lang_len) = '\0'; textp->lang_key = textp->lang + lang_len + 1; - png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len); + memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len); *(textp->lang_key + lang_key_len) = '\0'; textp->text = textp->lang_key + lang_key_len + 1; } @@ -836,9 +875,8 @@ png_set_text_2(png_structp png_ptr, png_infop info_ptr, textp->text = textp->key + key_len + 1; } - if (text_length) - png_memcpy(textp->text, text_ptr[i].text, - (png_size_t)(text_length)); + if (text_length != 0) + memcpy(textp->text, text_ptr[i].text, text_length); *(textp->text + text_length) = '\0'; @@ -859,18 +897,20 @@ png_set_text_2(png_structp png_ptr, png_infop info_ptr, info_ptr->num_text++; png_debug1(3, "transferred text chunk %d", info_ptr->num_text); } + return(0); } #endif #ifdef PNG_tIME_SUPPORTED void PNGAPI -png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time) +png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr, + png_const_timep mod_time) { png_debug1(1, "in %s storage function", "tIME"); - if (png_ptr == NULL || info_ptr == NULL || - (png_ptr->mode & PNG_WROTE_tIME)) + if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL || + (png_ptr->mode & PNG_WROTE_tIME) != 0) return; if (mod_time->month == 0 || mod_time->month > 12 || @@ -879,22 +919,24 @@ png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_const_timep mod_time) mod_time->second > 60) { png_warning(png_ptr, "Ignoring invalid time value"); + return; } - png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time)); + info_ptr->mod_time = *mod_time; info_ptr->valid |= PNG_INFO_tIME; } #endif #ifdef PNG_tRNS_SUPPORTED void PNGAPI -png_set_tRNS(png_structp png_ptr, png_infop info_ptr, +png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color) { png_debug1(1, "in %s storage function", "tRNS"); if (png_ptr == NULL || info_ptr == NULL) + return; if (trans_alpha != NULL) @@ -902,33 +944,41 @@ png_set_tRNS(png_structp png_ptr, png_infop info_ptr, /* It may not actually be necessary to set png_ptr->trans_alpha here; * we do it for backward compatibility with the way the png_handle_tRNS * function used to do the allocation. + * + * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively + * relies on png_set_tRNS storing the information in png_struct + * (otherwise it won't be there for the code in pngrtran.c). */ png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */ - png_ptr->trans_alpha = info_ptr->trans_alpha = - (png_bytep)png_malloc(png_ptr, (png_size_t)PNG_MAX_PALETTE_LENGTH); + png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep, + png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH)); if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) - png_memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans); + memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans); } if (trans_color != NULL) { - int sample_max = (1 << info_ptr->bit_depth); +#ifdef PNG_WARNINGS_SUPPORTED + if (info_ptr->bit_depth < 16) + { + int sample_max = (1 << info_ptr->bit_depth) - 1; - if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && - (int)trans_color->gray > sample_max) || - (info_ptr->color_type == PNG_COLOR_TYPE_RGB && - ((int)trans_color->red > sample_max || - (int)trans_color->green > sample_max || - (int)trans_color->blue > sample_max))) - png_warning(png_ptr, - "tRNS chunk has out-of-range samples for bit_depth"); + if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && + trans_color->gray > sample_max) || + (info_ptr->color_type == PNG_COLOR_TYPE_RGB && + (trans_color->red > sample_max || + trans_color->green > sample_max || + trans_color->blue > sample_max))) + png_warning(png_ptr, + "tRNS chunk has out-of-range samples for bit_depth"); + } +#endif - png_memcpy(&(info_ptr->trans_color), trans_color, - png_sizeof(png_color_16)); + info_ptr->trans_color = *trans_color; if (num_trans == 0) num_trans = 1; @@ -946,8 +996,8 @@ png_set_tRNS(png_structp png_ptr, png_infop info_ptr, #ifdef PNG_sPLT_SUPPORTED void PNGAPI -png_set_sPLT(png_structp png_ptr, - png_infop info_ptr, png_const_sPLT_tp entries, int nentries) +png_set_sPLT(png_const_structrp png_ptr, + png_inforp info_ptr, png_const_sPLT_tp entries, int nentries) /* * entries - array of png_sPLT_t structures * to be added to the list of palettes @@ -958,220 +1008,462 @@ png_set_sPLT(png_structp png_ptr, */ { png_sPLT_tp np; - int i; - if (png_ptr == NULL || info_ptr == NULL) + if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL) return; - np = (png_sPLT_tp)png_malloc_warn(png_ptr, - (info_ptr->splt_palettes_num + nentries) * - (png_size_t)png_sizeof(png_sPLT_t)); + /* Use the internal realloc function, which checks for all the possible + * overflows. Notice that the parameters are (int) and (size_t) + */ + np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr, + info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries, + sizeof *np)); if (np == NULL) { - png_warning(png_ptr, "No memory for sPLT palettes"); + /* Out of memory or too many chunks */ + png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR); + return; } - png_memcpy(np, info_ptr->splt_palettes, - info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t)); - png_free(png_ptr, info_ptr->splt_palettes); - info_ptr->splt_palettes=NULL; + info_ptr->splt_palettes = np; + info_ptr->free_me |= PNG_FREE_SPLT; - for (i = 0; i < nentries; i++) + np += info_ptr->splt_palettes_num; + + do { - png_sPLT_tp to = np + info_ptr->splt_palettes_num + i; - png_const_sPLT_tp from = entries + i; png_size_t length; - length = png_strlen(from->name) + 1; - to->name = (png_charp)png_malloc_warn(png_ptr, length); - - if (to->name == NULL) + /* Skip invalid input entries */ + if (entries->name == NULL || entries->entries == NULL) { - png_warning(png_ptr, - "Out of memory while processing sPLT chunk"); + /* png_handle_sPLT doesn't do this, so this is an app error */ + png_app_error(png_ptr, "png_set_sPLT: invalid sPLT"); + /* Just skip the invalid entry */ continue; } - png_memcpy(to->name, from->name, length); - to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr, - from->nentries * png_sizeof(png_sPLT_entry)); + np->depth = entries->depth; - if (to->entries == NULL) + /* In the event of out-of-memory just return - there's no point keeping + * on trying to add sPLT chunks. + */ + length = strlen(entries->name) + 1; + np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length)); + + if (np->name == NULL) + break; + + memcpy(np->name, entries->name, length); + + /* IMPORTANT: we have memory now that won't get freed if something else + * goes wrong; this code must free it. png_malloc_array produces no + * warnings; use a png_chunk_report (below) if there is an error. + */ + np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr, + entries->nentries, sizeof (png_sPLT_entry))); + + if (np->entries == NULL) { - png_warning(png_ptr, - "Out of memory while processing sPLT chunk"); - png_free(png_ptr, to->name); - to->name = NULL; - continue; + png_free(png_ptr, np->name); + np->name = NULL; + break; } - png_memcpy(to->entries, from->entries, - from->nentries * png_sizeof(png_sPLT_entry)); + np->nentries = entries->nentries; + /* This multiply can't overflow because png_malloc_array has already + * checked it when doing the allocation. + */ + memcpy(np->entries, entries->entries, + entries->nentries * sizeof (png_sPLT_entry)); - to->nentries = from->nentries; - to->depth = from->depth; + /* Note that 'continue' skips the advance of the out pointer and out + * count, so an invalid entry is not added. + */ + info_ptr->valid |= PNG_INFO_sPLT; + ++(info_ptr->splt_palettes_num); + ++np; + } + while (++entries, --nentries); + + if (nentries > 0) + png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR); +} +#endif /* sPLT */ + +#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED +static png_byte +check_location(png_const_structrp png_ptr, int location) +{ + location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT); + + /* New in 1.6.0; copy the location and check it. This is an API + * change; previously the app had to use the + * png_set_unknown_chunk_location API below for each chunk. + */ + if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0) + { + /* Write struct, so unknown chunks come from the app */ + png_app_warning(png_ptr, + "png_set_unknown_chunks now expects a valid location"); + /* Use the old behavior */ + location = (png_byte)(png_ptr->mode & + (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)); } - info_ptr->splt_palettes = np; - info_ptr->splt_palettes_num += nentries; - info_ptr->valid |= PNG_INFO_sPLT; - info_ptr->free_me |= PNG_FREE_SPLT; -} -#endif /* PNG_sPLT_SUPPORTED */ + /* This need not be an internal error - if the app calls + * png_set_unknown_chunks on a read pointer it must get the location right. + */ + if (location == 0) + png_error(png_ptr, "invalid location in png_set_unknown_chunks"); + + /* Now reduce the location to the top-most set bit by removing each least + * significant bit in turn. + */ + while (location != (location & -location)) + location &= ~(location & -location); + + /* The cast is safe because 'location' is a bit mask and only the low four + * bits are significant. + */ + return (png_byte)location; +} -#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED void PNGAPI -png_set_unknown_chunks(png_structp png_ptr, - png_infop info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns) +png_set_unknown_chunks(png_const_structrp png_ptr, + png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns) { png_unknown_chunkp np; - int i; - if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0) + if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 || + unknowns == NULL) return; - np = (png_unknown_chunkp)png_malloc_warn(png_ptr, - (png_size_t)(info_ptr->unknown_chunks_num + num_unknowns) * - png_sizeof(png_unknown_chunk)); + /* Check for the failure cases where support has been disabled at compile + * time. This code is hardly ever compiled - it's here because + * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this + * code) but may be meaningless if the read or write handling of unknown + * chunks is not compiled in. + */ +# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \ + defined(PNG_READ_SUPPORTED) + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) + { + png_app_error(png_ptr, "no unknown chunk support on read"); + + return; + } +# endif +# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \ + defined(PNG_WRITE_SUPPORTED) + if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) + { + png_app_error(png_ptr, "no unknown chunk support on write"); + + return; + } +# endif + + /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that + * unknown critical chunks could be lost with just a warning resulting in + * undefined behavior. Now png_chunk_report is used to provide behavior + * appropriate to read or write. + */ + np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr, + info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns, + sizeof *np)); if (np == NULL) { - png_warning(png_ptr, - "Out of memory while processing unknown chunk"); + png_chunk_report(png_ptr, "too many unknown chunks", + PNG_CHUNK_WRITE_ERROR); + return; } - png_memcpy(np, info_ptr->unknown_chunks, - (png_size_t)info_ptr->unknown_chunks_num * - png_sizeof(png_unknown_chunk)); - png_free(png_ptr, info_ptr->unknown_chunks); - info_ptr->unknown_chunks = NULL; + info_ptr->unknown_chunks = np; /* safe because it is initialized */ + info_ptr->free_me |= PNG_FREE_UNKN; - for (i = 0; i < num_unknowns; i++) + np += info_ptr->unknown_chunks_num; + + /* Increment unknown_chunks_num each time round the loop to protect the + * just-allocated chunk data. + */ + for (; num_unknowns > 0; --num_unknowns, ++unknowns) { - png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i; - png_const_unknown_chunkp from = unknowns + i; + memcpy(np->name, unknowns->name, (sizeof np->name)); + np->name[(sizeof np->name)-1] = '\0'; + np->location = check_location(png_ptr, unknowns->location); - png_memcpy(to->name, from->name, png_sizeof(from->name)); - to->name[png_sizeof(to->name)-1] = '\0'; - to->size = from->size; - - /* Note our location in the read or write sequence */ - to->location = (png_byte)(png_ptr->mode & 0xff); - - if (from->size == 0) - to->data=NULL; + if (unknowns->size == 0) + { + np->data = NULL; + np->size = 0; + } else { - to->data = (png_bytep)png_malloc_warn(png_ptr, - (png_size_t)from->size); + np->data = png_voidcast(png_bytep, + png_malloc_base(png_ptr, unknowns->size)); - if (to->data == NULL) + if (np->data == NULL) { - png_warning(png_ptr, - "Out of memory while processing unknown chunk"); - to->size = 0; + png_chunk_report(png_ptr, "unknown chunk: out of memory", + PNG_CHUNK_WRITE_ERROR); + /* But just skip storing the unknown chunk */ + continue; } - else - png_memcpy(to->data, from->data, from->size); + memcpy(np->data, unknowns->data, unknowns->size); + np->size = unknowns->size; } - } - info_ptr->unknown_chunks = np; - info_ptr->unknown_chunks_num += num_unknowns; - info_ptr->free_me |= PNG_FREE_UNKN; + /* These increments are skipped on out-of-memory for the data - the + * unknown chunk entry gets overwritten if the png_chunk_report returns. + * This is correct in the read case (the chunk is just dropped.) + */ + ++np; + ++(info_ptr->unknown_chunks_num); + } } void PNGAPI -png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr, +png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr, int chunk, int location) { - if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk < - info_ptr->unknown_chunks_num) - info_ptr->unknown_chunks[chunk].location = (png_byte)location; -} -#endif + /* This API is pretty pointless in 1.6.0 because the location can be set + * before the call to png_set_unknown_chunks. + * + * TODO: add a png_app_warning in 1.7 + */ + if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && + chunk < info_ptr->unknown_chunks_num) + { + if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0) + { + png_app_error(png_ptr, "invalid unknown chunk location"); + /* Fake out the pre 1.6.0 behavior: */ + if ((location & PNG_HAVE_IDAT) != 0) /* undocumented! */ + location = PNG_AFTER_IDAT; + else + location = PNG_HAVE_IHDR; /* also undocumented */ + } + + info_ptr->unknown_chunks[chunk].location = + check_location(png_ptr, location); + } +} +#endif /* STORE_UNKNOWN_CHUNKS */ #ifdef PNG_MNG_FEATURES_SUPPORTED png_uint_32 PNGAPI -png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features) +png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features) { png_debug(1, "in png_permit_mng_features"); if (png_ptr == NULL) - return (png_uint_32)0; + return 0; - png_ptr->mng_features_permitted = - (png_byte)(mng_features & PNG_ALL_MNG_FEATURES); + png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES; - return (png_uint_32)png_ptr->mng_features_permitted; + return png_ptr->mng_features_permitted; } #endif #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED -void PNGAPI -png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_const_bytep - chunk_list, int num_chunks) +static unsigned int +add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep) { - png_bytep new_list, p; - int i, old_num_chunks; + unsigned int i; + + /* Utility function: update the 'keep' state of a chunk if it is already in + * the list, otherwise add it to the list. + */ + for (i=0; i= PNG_HANDLE_CHUNK_LAST) { - if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE) - png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS; - - else - png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS; - - if (keep == PNG_HANDLE_CHUNK_ALWAYS) - png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS; - - else - png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS; + png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep"); return; } - if (chunk_list == NULL) - return; + if (num_chunks_in <= 0) + { + png_ptr->unknown_default = keep; + + /* '0' means just set the flags, so stop here */ + if (num_chunks_in == 0) + return; + } + + if (num_chunks_in < 0) + { + /* Ignore all unknown chunks and all chunks recognized by + * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND + */ + static PNG_CONST png_byte chunks_to_ignore[] = { + 98, 75, 71, 68, '\0', /* bKGD */ + 99, 72, 82, 77, '\0', /* cHRM */ + 103, 65, 77, 65, '\0', /* gAMA */ + 104, 73, 83, 84, '\0', /* hIST */ + 105, 67, 67, 80, '\0', /* iCCP */ + 105, 84, 88, 116, '\0', /* iTXt */ + 111, 70, 70, 115, '\0', /* oFFs */ + 112, 67, 65, 76, '\0', /* pCAL */ + 112, 72, 89, 115, '\0', /* pHYs */ + 115, 66, 73, 84, '\0', /* sBIT */ + 115, 67, 65, 76, '\0', /* sCAL */ + 115, 80, 76, 84, '\0', /* sPLT */ + 115, 84, 69, 82, '\0', /* sTER */ + 115, 82, 71, 66, '\0', /* sRGB */ + 116, 69, 88, 116, '\0', /* tEXt */ + 116, 73, 77, 69, '\0', /* tIME */ + 122, 84, 88, 116, '\0' /* zTXt */ + }; + + chunk_list = chunks_to_ignore; + num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U; + } + + else /* num_chunks_in > 0 */ + { + if (chunk_list == NULL) + { + /* Prior to 1.6.0 this was silently ignored, now it is an app_error + * which can be switched off. + */ + png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list"); + + return; + } + + num_chunks = num_chunks_in; + } old_num_chunks = png_ptr->num_chunk_list; - new_list=(png_bytep)png_malloc(png_ptr, - (png_size_t)(5*(num_chunks + old_num_chunks))); + if (png_ptr->chunk_list == NULL) + old_num_chunks = 0; - if (png_ptr->chunk_list != NULL) + /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow. + */ + if (num_chunks + old_num_chunks > UINT_MAX/5) { - png_memcpy(new_list, png_ptr->chunk_list, - (png_size_t)(5*old_num_chunks)); - png_free(png_ptr, png_ptr->chunk_list); - png_ptr->chunk_list=NULL; + png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks"); + + return; } - png_memcpy(new_list + 5*old_num_chunks, chunk_list, - (png_size_t)(5*num_chunks)); + /* If these chunks are being reset to the default then no more memory is + * required because add_one_chunk above doesn't extend the list if the 'keep' + * parameter is the default. + */ + if (keep != 0) + { + new_list = png_voidcast(png_bytep, png_malloc(png_ptr, + 5 * (num_chunks + old_num_chunks))); - for (p = new_list + 5*old_num_chunks + 4, i = 0; i 0) + memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks); + } - png_ptr->num_chunk_list = old_num_chunks + num_chunks; - png_ptr->chunk_list = new_list; - png_ptr->free_me |= PNG_FREE_LIST; + else if (old_num_chunks > 0) + new_list = png_ptr->chunk_list; + + else + new_list = NULL; + + /* Add the new chunks together with each one's handling code. If the chunk + * already exists the code is updated, otherwise the chunk is added to the + * end. (In libpng 1.6.0 order no longer matters because this code enforces + * the earlier convention that the last setting is the one that is used.) + */ + if (new_list != NULL) + { + png_const_bytep inlist; + png_bytep outlist; + unsigned int i; + + for (i=0; ichunk_list != new_list) + png_free(png_ptr, new_list); + + new_list = NULL; + } + } + + else + num_chunks = 0; + + png_ptr->num_chunk_list = num_chunks; + + if (png_ptr->chunk_list != new_list) + { + if (png_ptr->chunk_list != NULL) + png_free(png_ptr, png_ptr->chunk_list); + + png_ptr->chunk_list = new_list; + } } #endif #ifdef PNG_READ_USER_CHUNKS_SUPPORTED void PNGAPI -png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr, +png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn) { png_debug(1, "in png_set_read_user_chunk_fn"); @@ -1186,69 +1478,102 @@ png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr, #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI -png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers) +png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr, + png_bytepp row_pointers) { png_debug1(1, "in %s storage function", "rows"); if (png_ptr == NULL || info_ptr == NULL) return; - if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers)) + if (info_ptr->row_pointers != NULL && + (info_ptr->row_pointers != row_pointers)) png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); info_ptr->row_pointers = row_pointers; - if (row_pointers) + if (row_pointers != NULL) info_ptr->valid |= PNG_INFO_IDAT; } #endif void PNGAPI -png_set_compression_buffer_size(png_structp png_ptr, png_size_t size) +png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size) { if (png_ptr == NULL) return; - png_free(png_ptr, png_ptr->zbuf); + if (size == 0 || size > PNG_UINT_31_MAX) + png_error(png_ptr, "invalid compression buffer size"); - if (size > ZLIB_IO_MAX) - { - png_warning(png_ptr, "Attempt to set buffer size beyond max ignored"); - png_ptr->zbuf_size = ZLIB_IO_MAX; - size = ZLIB_IO_MAX; /* must fit */ - } +# ifdef PNG_SEQUENTIAL_READ_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) + { + png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */ + return; + } +# endif - else - png_ptr->zbuf_size = (uInt)size; +# ifdef PNG_WRITE_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) + { + if (png_ptr->zowner != 0) + { + png_warning(png_ptr, + "Compression buffer size cannot be changed because it is in use"); - png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size); + return; + } - /* The following ensures a relatively safe failure if this gets called while - * the buffer is actually in use. - */ - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = 0; - png_ptr->zstream.avail_in = 0; +#ifndef __COVERITY__ + /* Some compilers complain that this is always false. However, it + * can be true when integer overflow happens. + */ + if (size > ZLIB_IO_MAX) + { + png_warning(png_ptr, + "Compression buffer size limited to system maximum"); + size = ZLIB_IO_MAX; /* must fit */ + } +#endif + + if (size < 6) + { + /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH + * if this is permitted. + */ + png_warning(png_ptr, + "Compression buffer size cannot be reduced below 6"); + + return; + } + + if (png_ptr->zbuffer_size != size) + { + png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); + png_ptr->zbuffer_size = (uInt)size; + } + } +# endif } void PNGAPI -png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask) +png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask) { - if (png_ptr && info_ptr) + if (png_ptr != NULL && info_ptr != NULL) info_ptr->valid &= ~mask; } - #ifdef PNG_SET_USER_LIMITS_SUPPORTED /* This function was added to libpng 1.2.6 */ void PNGAPI -png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max, +png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max, png_uint_32 user_height_max) { /* Images with dimensions larger than these limits will be * rejected by png_set_IHDR(). To accept any PNG datastream - * regardless of dimensions, set both limits to 0x7ffffffL. + * regardless of dimensions, set both limits to 0x7ffffff. */ if (png_ptr == NULL) return; @@ -1259,53 +1584,64 @@ png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max, /* This function was added to libpng 1.4.0 */ void PNGAPI -png_set_chunk_cache_max (png_structp png_ptr, - png_uint_32 user_chunk_cache_max) +png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max) { - if (png_ptr) - png_ptr->user_chunk_cache_max = user_chunk_cache_max; + if (png_ptr != NULL) + png_ptr->user_chunk_cache_max = user_chunk_cache_max; } /* This function was added to libpng 1.4.1 */ void PNGAPI -png_set_chunk_malloc_max (png_structp png_ptr, +png_set_chunk_malloc_max (png_structrp png_ptr, png_alloc_size_t user_chunk_malloc_max) { - if (png_ptr) + if (png_ptr != NULL) png_ptr->user_chunk_malloc_max = user_chunk_malloc_max; } -#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */ +#endif /* ?SET_USER_LIMITS */ #ifdef PNG_BENIGN_ERRORS_SUPPORTED void PNGAPI -png_set_benign_errors(png_structp png_ptr, int allowed) +png_set_benign_errors(png_structrp png_ptr, int allowed) { png_debug(1, "in png_set_benign_errors"); - if (allowed) - png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; + /* If allowed is 1, png_benign_error() is treated as a warning. + * + * If allowed is 0, png_benign_error() is treated as an error (which + * is the default behavior if png_set_benign_errors() is not called). + */ + + if (allowed != 0) + png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN | + PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN; else - png_ptr->flags &= ~PNG_FLAG_BENIGN_ERRORS_WARN; + png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN | + PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN); } -#endif /* PNG_BENIGN_ERRORS_SUPPORTED */ +#endif /* BENIGN_ERRORS */ #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED -/* Whether to report invalid palette index; added at libng-1.5.10 - * allowed - one of 0: disable; 1: enable - */ + /* Whether to report invalid palette index; added at libng-1.5.10. + * It is possible for an indexed (color-type==3) PNG file to contain + * pixels with invalid (out-of-range) indexes if the PLTE chunk has + * fewer entries than the image's bit-depth would allow. We recover + * from this gracefully by filling any incomplete palette with zeros + * (opaque black). By default, when this occurs libpng will issue + * a benign error. This API can be used to override that behavior. + */ void PNGAPI -png_set_check_for_invalid_index(png_structp png_ptr, int allowed) +png_set_check_for_invalid_index(png_structrp png_ptr, int allowed) { png_debug(1, "in png_set_check_for_invalid_index"); - if (allowed) + if (allowed > 0) png_ptr->num_palette_max = 0; else png_ptr->num_palette_max = -1; } #endif - -#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/pngstruct.h b/3rdparty/libpng/pngstruct.h index db0d4e494..c8c0e46e8 100644 --- a/3rdparty/libpng/pngstruct.h +++ b/3rdparty/libpng/pngstruct.h @@ -1,12 +1,11 @@ /* pngstruct.h - header file for PNG reference library * - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.18 [July 23, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * - * Last changed in libpng 1.5.9 [February 18, 2012] - * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h @@ -24,13 +23,130 @@ * in this structure and is required for decompressing the LZ compressed * data in PNG files. */ +#ifndef ZLIB_CONST + /* We must ensure that zlib uses 'const' in declarations. */ +# define ZLIB_CONST +#endif #include "zlib.h" +#ifdef const + /* zlib.h sometimes #defines const to nothing, undo this. */ +# undef const +#endif + +/* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility + * with older builds. + */ +#if ZLIB_VERNUM < 0x1260 +# define PNGZ_MSG_CAST(s) png_constcast(char*,s) +# define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b) +#else +# define PNGZ_MSG_CAST(s) (s) +# define PNGZ_INPUT_CAST(b) (b) +#endif + +/* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib + * can handle at once. This type need be no larger than 16 bits (so maximum of + * 65535), this define allows us to discover how big it is, but limited by the + * maximuum for png_size_t. The value can be overriden in a library build + * (pngusr.h, or set it in CPPFLAGS) and it works to set it to a considerably + * lower value (e.g. 255 works). A lower value may help memory usage (slightly) + * and may even improve performance on some systems (and degrade it on others.) + */ +#ifndef ZLIB_IO_MAX +# define ZLIB_IO_MAX ((uInt)-1) +#endif + +#ifdef PNG_WRITE_SUPPORTED +/* The type of a compression buffer list used by the write code. */ +typedef struct png_compression_buffer +{ + struct png_compression_buffer *next; + png_byte output[1]; /* actually zbuf_size */ +} png_compression_buffer, *png_compression_bufferp; + +#define PNG_COMPRESSION_BUFFER_SIZE(pp)\ + (offsetof(png_compression_buffer, output) + (pp)->zbuffer_size) +#endif + +/* Colorspace support; structures used in png_struct, png_info and in internal + * functions to hold and communicate information about the color space. + * + * PNG_COLORSPACE_SUPPORTED is only required if the application will perform + * colorspace corrections, otherwise all the colorspace information can be + * skipped and the size of libpng can be reduced (significantly) by compiling + * out the colorspace support. + */ +#ifdef PNG_COLORSPACE_SUPPORTED +/* The chromaticities of the red, green and blue colorants and the chromaticity + * of the corresponding white point (i.e. of rgb(1.0,1.0,1.0)). + */ +typedef struct png_xy +{ + png_fixed_point redx, redy; + png_fixed_point greenx, greeny; + png_fixed_point bluex, bluey; + png_fixed_point whitex, whitey; +} png_xy; + +/* The same data as above but encoded as CIE XYZ values. When this data comes + * from chromaticities the sum of the Y values is assumed to be 1.0 + */ +typedef struct png_XYZ +{ + png_fixed_point red_X, red_Y, red_Z; + png_fixed_point green_X, green_Y, green_Z; + png_fixed_point blue_X, blue_Y, blue_Z; +} png_XYZ; +#endif /* COLORSPACE */ + +#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) +/* A colorspace is all the above plus, potentially, profile information; + * however at present libpng does not use the profile internally so it is only + * stored in the png_info struct (if iCCP is supported.) The rendering intent + * is retained here and is checked. + * + * The file gamma encoding information is also stored here and gamma correction + * is done by libpng, whereas color correction must currently be done by the + * application. + */ +typedef struct png_colorspace +{ +#ifdef PNG_GAMMA_SUPPORTED + png_fixed_point gamma; /* File gamma */ +#endif + +#ifdef PNG_COLORSPACE_SUPPORTED + png_xy end_points_xy; /* End points as chromaticities */ + png_XYZ end_points_XYZ; /* End points as CIE XYZ colorant values */ + png_uint_16 rendering_intent; /* Rendering intent of a profile */ +#endif + + /* Flags are always defined to simplify the code. */ + png_uint_16 flags; /* As defined below */ +} png_colorspace, * PNG_RESTRICT png_colorspacerp; + +typedef const png_colorspace * PNG_RESTRICT png_const_colorspacerp; + +/* General flags for the 'flags' field */ +#define PNG_COLORSPACE_HAVE_GAMMA 0x0001 +#define PNG_COLORSPACE_HAVE_ENDPOINTS 0x0002 +#define PNG_COLORSPACE_HAVE_INTENT 0x0004 +#define PNG_COLORSPACE_FROM_gAMA 0x0008 +#define PNG_COLORSPACE_FROM_cHRM 0x0010 +#define PNG_COLORSPACE_FROM_sRGB 0x0020 +#define PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB 0x0040 +#define PNG_COLORSPACE_MATCHES_sRGB 0x0080 /* exact match on profile */ +#define PNG_COLORSPACE_INVALID 0x8000 +#define PNG_COLORSPACE_CANCEL(flags) (0xffff ^ (flags)) +#endif /* COLORSPACE || GAMMA */ struct png_struct_def { #ifdef PNG_SETJMP_SUPPORTED - jmp_buf longjmp_buffer; /* used in png_error */ + jmp_buf jmp_buf_local; /* New name in 1.6.0 for jmp_buf in png_struct */ png_longjmp_ptr longjmp_fn;/* setjmp non-local goto function. */ + jmp_buf *jmp_buf_ptr; /* passed to longjmp_fn */ + size_t jmp_buf_size; /* size of the above, if allocated */ #endif png_error_ptr error_fn; /* function for printing errors and aborting */ #ifdef PNG_WARNINGS_SUPPORTED @@ -63,22 +179,12 @@ struct png_struct_def png_uint_32 flags; /* flags indicating various things to libpng */ png_uint_32 transformations; /* which transformations to perform */ - z_stream zstream; /* pointer to decompression structure (below) */ - png_bytep zbuf; /* buffer for zlib */ - uInt zbuf_size; /* size of zbuf (typically 65536) */ + png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */ + z_stream zstream; /* decompression structure */ + #ifdef PNG_WRITE_SUPPORTED - -/* Added in 1.5.4: state to keep track of whether the zstream has been - * initialized and if so whether it is for IDAT or some other chunk. - */ -#define PNG_ZLIB_UNINITIALIZED 0 -#define PNG_ZLIB_FOR_IDAT 1 -#define PNG_ZLIB_FOR_TEXT 2 /* anything other than IDAT */ -#define PNG_ZLIB_USE_MASK 3 /* bottom two bits */ -#define PNG_ZLIB_IN_USE 4 /* a flag value */ - - png_uint_32 zlib_state; /* State of zlib initialization */ -/* End of material added at libpng 1.5.4 */ + png_compression_bufferp zbuffer_list; /* Created on demand during write */ + uInt zbuffer_size; /* size of the actual buffer */ int zlib_level; /* holds zlib compression level */ int zlib_method; /* holds zlib compression method */ @@ -87,8 +193,7 @@ struct png_struct_def int zlib_strategy; /* holds zlib compression strategy */ #endif /* Added at libpng 1.5.4 */ -#if defined(PNG_WRITE_COMPRESSED_TEXT_SUPPORTED) || \ - defined(PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED) +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED int zlib_text_level; /* holds zlib compression level */ int zlib_text_method; /* holds zlib compression method */ int zlib_text_window_bits; /* holds zlib compression window bits */ @@ -96,6 +201,14 @@ struct png_struct_def int zlib_text_strategy; /* holds zlib compression strategy */ #endif /* End of material added at libpng 1.5.4 */ +/* Added at libpng 1.6.0 */ +#ifdef PNG_WRITE_SUPPORTED + int zlib_set_level; /* Actual values set into the zstream on write */ + int zlib_set_method; + int zlib_set_window_bits; + int zlib_set_mem_level; + int zlib_set_strategy; +#endif png_uint_32 width; /* width of image in pixels */ png_uint_32 height; /* height of image in pixels */ @@ -106,15 +219,19 @@ struct png_struct_def png_uint_32 row_number; /* current row in interlace pass */ png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */ png_bytep prev_row; /* buffer to save previous (unfiltered) row. - * This is a pointer into big_prev_row + * While reading this is a pointer into + * big_prev_row; while writing it is separately + * allocated if needed. */ png_bytep row_buf; /* buffer to save current (unfiltered) row. - * This is a pointer into big_row_buf + * While reading, this is a pointer into + * big_row_buf; while writing it is separately + * allocated. */ - png_bytep sub_row; /* buffer to save "sub" row when filtering */ - png_bytep up_row; /* buffer to save "up" row when filtering */ - png_bytep avg_row; /* buffer to save "avg" row when filtering */ - png_bytep paeth_row; /* buffer to save "Paeth" row when filtering */ +#ifdef PNG_WRITE_FILTER_SUPPORTED + png_bytep try_row; /* buffer to save trial row when filtering */ + png_bytep tst_row; /* buffer to save best trial row when filtering */ +#endif png_size_t info_rowbytes; /* Added in 1.5.4: cache of updated row bytes */ png_uint_32 idat_size; /* current IDAT size for read */ @@ -138,15 +255,14 @@ struct png_struct_def png_byte usr_bit_depth; /* bit depth of users row: write only */ png_byte pixel_depth; /* number of bits per pixel */ png_byte channels; /* number of channels in file */ +#ifdef PNG_WRITE_SUPPORTED png_byte usr_channels; /* channels at start of write: write only */ +#endif png_byte sig_bytes; /* magic bytes read/written from start of file */ png_byte maximum_pixel_depth; /* pixel depth used for the row buffers */ png_byte transformed_pixel_depth; /* pixel depth after read/write transforms */ - png_byte io_chunk_string[5]; - /* string name of chunk */ - #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) png_uint_16 filler; /* filler bytes for pixel expansion */ #endif @@ -159,7 +275,7 @@ struct png_struct_def #ifdef PNG_READ_GAMMA_SUPPORTED png_color_16 background_1; /* background normalized to gamma 1.0 */ #endif -#endif /* PNG_bKGD_SUPPORTED */ +#endif /* bKGD */ #ifdef PNG_WRITE_FLUSH_SUPPORTED png_flush_ptr output_flush_fn; /* Function for flushing output */ @@ -169,7 +285,6 @@ struct png_struct_def #ifdef PNG_READ_GAMMA_SUPPORTED int gamma_shift; /* number of "insignificant" bits in 16-bit gamma */ - png_fixed_point gamma; /* file gamma value */ png_fixed_point screen_gamma; /* screen gamma value (display_exponent) */ png_bytep gamma_table; /* gamma table for 8-bit depth files */ @@ -217,7 +332,7 @@ struct png_struct_def int process_mode; /* what push library is currently doing */ int cur_palette; /* current push library palette index */ -#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ +#endif /* PROGRESSIVE_READ */ #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) /* For the Borland special 64K segment handler */ @@ -233,24 +348,17 @@ struct png_struct_def png_bytep quantize_index; /* index translation for palette files */ #endif -#if defined(PNG_READ_QUANTIZE_SUPPORTED) || defined(PNG_hIST_SUPPORTED) - png_uint_16p hist; /* histogram */ -#endif - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - png_byte heuristic_method; /* heuristic for row filter selection */ - png_byte num_prev_filters; /* number of weights for previous rows */ - png_bytep prev_filters; /* filter type(s) of previous row(s) */ - png_uint_16p filter_weights; /* weight(s) for previous line(s) */ - png_uint_16p inv_filter_weights; /* 1/weight(s) for previous line(s) */ - png_uint_16p filter_costs; /* relative filter calculation cost */ - png_uint_16p inv_filter_costs; /* 1/relative filter calculation cost */ +/* Options */ +#ifdef PNG_SET_OPTION_SUPPORTED + png_byte options; /* On/off state (up to 4 options) */ #endif +#if PNG_LIBPNG_VER < 10700 +/* To do: remove this from libpng-1.7 */ #ifdef PNG_TIME_RFC1123_SUPPORTED - /* This is going to be unused in libpng16 and removed from libpng17 */ char time_buffer[29]; /* String to hold RFC 1123 time text */ #endif +#endif /* New members added in libpng-1.0.6 */ @@ -258,17 +366,16 @@ struct png_struct_def #ifdef PNG_USER_CHUNKS_SUPPORTED png_voidp user_chunk_ptr; +#ifdef PNG_READ_USER_CHUNKS_SUPPORTED png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */ #endif - -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - int num_chunk_list; - png_bytep chunk_list; #endif -#ifdef PNG_READ_sRGB_SUPPORTED - /* Added in 1.5.5 to record an sRGB chunk in the png. */ - png_byte is_sRGB; +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + int unknown_default; /* As PNG_HANDLE_* */ + unsigned int num_chunk_list; /* Number of entries in the list */ + png_bytep chunk_list; /* List of png_byte[5]; the textual chunk name + * followed by a PNG_HANDLE_* byte */ #endif /* New members added in libpng-1.0.3 */ @@ -333,16 +440,24 @@ struct png_struct_def #endif /* New member added in libpng-1.0.25 and 1.2.17 */ -#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED - /* Storage for unknown chunk that the library doesn't recognize. */ +#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED + /* Temporary storage for unknown chunk that the library doesn't recognize, + * used while reading the chunk. + */ png_unknown_chunk unknown_chunk; #endif /* New member added in libpng-1.2.26 */ png_size_t old_big_row_buf_size; +#ifdef PNG_READ_SUPPORTED /* New member added in libpng-1.2.30 */ - png_charp chunkdata; /* buffer for reading chunk data */ + png_bytep read_buffer; /* buffer for reading chunk data */ + png_alloc_size_t read_buffer_size; /* current size of the buffer */ +#endif +#ifdef PNG_SEQUENTIAL_READ_SUPPORTED + uInt IDAT_read_size; /* limit on read buffer size for IDAT */ +#endif #ifdef PNG_IO_STATE_SUPPORTED /* New member added in libpng-1.4.0 */ @@ -352,7 +467,14 @@ struct png_struct_def /* New member added in libpng-1.5.6 */ png_bytep big_prev_row; +/* New member added in libpng-1.5.7 */ void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info, png_bytep row, png_const_bytep prev_row); + +#ifdef PNG_READ_SUPPORTED +#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) + png_colorspace colorspace; +#endif +#endif }; #endif /* PNGSTRUCT_H */ diff --git a/3rdparty/libpng/pngtrans.c b/3rdparty/libpng/pngtrans.c index ee60957fc..0c0d92d8f 100644 --- a/3rdparty/libpng/pngtrans.c +++ b/3rdparty/libpng/pngtrans.c @@ -1,8 +1,8 @@ /* pngtrans.c - transforms the data in a row (used by both readers and writers) * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.18 [July 23, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -18,7 +18,7 @@ #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) /* Turn on BGR-to-RGB mapping */ void PNGAPI -png_set_bgr(png_structp png_ptr) +png_set_bgr(png_structrp png_ptr) { png_debug(1, "in png_set_bgr"); @@ -30,9 +30,9 @@ png_set_bgr(png_structp png_ptr) #endif #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) -/* Turn on 16 bit byte swapping */ +/* Turn on 16-bit byte swapping */ void PNGAPI -png_set_swap(png_structp png_ptr) +png_set_swap(png_structrp png_ptr) { png_debug(1, "in png_set_swap"); @@ -47,7 +47,7 @@ png_set_swap(png_structp png_ptr) #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) /* Turn on pixel packing */ void PNGAPI -png_set_packing(png_structp png_ptr) +png_set_packing(png_structrp png_ptr) { png_debug(1, "in png_set_packing"); @@ -57,7 +57,9 @@ png_set_packing(png_structp png_ptr) if (png_ptr->bit_depth < 8) { png_ptr->transformations |= PNG_PACK; - png_ptr->usr_bit_depth = 8; +# ifdef PNG_WRITE_SUPPORTED + png_ptr->usr_bit_depth = 8; +# endif } } #endif @@ -65,7 +67,7 @@ png_set_packing(png_structp png_ptr) #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) /* Turn on packed pixel swapping */ void PNGAPI -png_set_packswap(png_structp png_ptr) +png_set_packswap(png_structrp png_ptr) { png_debug(1, "in png_set_packswap"); @@ -79,7 +81,7 @@ png_set_packswap(png_structp png_ptr) #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) void PNGAPI -png_set_shift(png_structp png_ptr, png_const_color_8p true_bits) +png_set_shift(png_structrp png_ptr, png_const_color_8p true_bits) { png_debug(1, "in png_set_shift"); @@ -94,11 +96,11 @@ png_set_shift(png_structp png_ptr, png_const_color_8p true_bits) #if defined(PNG_READ_INTERLACING_SUPPORTED) || \ defined(PNG_WRITE_INTERLACING_SUPPORTED) int PNGAPI -png_set_interlace_handling(png_structp png_ptr) +png_set_interlace_handling(png_structrp png_ptr) { png_debug(1, "in png_set_interlace handling"); - if (png_ptr && png_ptr->interlaced) + if (png_ptr != 0 && png_ptr->interlaced != 0) { png_ptr->transformations |= PNG_INTERLACE; return (7); @@ -115,44 +117,91 @@ png_set_interlace_handling(png_structp png_ptr) * that don't like bytes as parameters. */ void PNGAPI -png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) +png_set_filler(png_structrp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_filler"); if (png_ptr == NULL) return; + /* In libpng 1.6 it is possible to determine whether this is a read or write + * operation and therefore to do more checking here for a valid call. + */ + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) + { +# ifdef PNG_READ_FILLER_SUPPORTED + /* On read png_set_filler is always valid, regardless of the base PNG + * format, because other transformations can give a format where the + * filler code can execute (basically an 8 or 16-bit component RGB or G + * format.) + * + * NOTE: usr_channels is not used by the read code! (This has led to + * confusion in the past.) The filler is only used in the read code. + */ + png_ptr->filler = (png_uint_16)filler; +# else + png_app_error(png_ptr, "png_set_filler not supported on read"); + PNG_UNUSED(filler) /* not used in the write case */ + return; +# endif + } + + else /* write */ + { +# ifdef PNG_WRITE_FILLER_SUPPORTED + /* On write the usr_channels parameter must be set correctly at the + * start to record the number of channels in the app-supplied data. + */ + switch (png_ptr->color_type) + { + case PNG_COLOR_TYPE_RGB: + png_ptr->usr_channels = 4; + break; + + case PNG_COLOR_TYPE_GRAY: + if (png_ptr->bit_depth >= 8) + { + png_ptr->usr_channels = 2; + break; + } + + else + { + /* There simply isn't any code in libpng to strip out bits + * from bytes when the components are less than a byte in + * size! + */ + png_app_error(png_ptr, + "png_set_filler is invalid for low bit depth gray output"); + return; + } + + default: + png_app_error(png_ptr, + "png_set_filler: inappropriate color type"); + return; + } +# else + png_app_error(png_ptr, "png_set_filler not supported on write"); + return; +# endif + } + + /* Here on success - libpng supports the operation, set the transformation + * and the flag to say where the filler channel is. + */ png_ptr->transformations |= PNG_FILLER; - png_ptr->filler = (png_uint_16)filler; if (filler_loc == PNG_FILLER_AFTER) png_ptr->flags |= PNG_FLAG_FILLER_AFTER; else png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER; - - /* This should probably go in the "do_read_filler" routine. - * I attempted to do that in libpng-1.0.1a but that caused problems - * so I restored it in libpng-1.0.2a - */ - - if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) - { - png_ptr->usr_channels = 4; - } - - /* Also I added this in libpng-1.0.2a (what happens when we expand - * a less-than-8-bit grayscale to GA?) */ - - if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8) - { - png_ptr->usr_channels = 2; - } } /* Added to libpng-1.2.7 */ void PNGAPI -png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) +png_set_add_alpha(png_structrp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_add_alpha"); @@ -160,7 +209,9 @@ png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) return; png_set_filler(png_ptr, filler, filler_loc); - png_ptr->transformations |= PNG_ADD_ALPHA; + /* The above may fail to do anything. */ + if ((png_ptr->transformations & PNG_FILLER) != 0) + png_ptr->transformations |= PNG_ADD_ALPHA; } #endif @@ -168,7 +219,7 @@ png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) void PNGAPI -png_set_swap_alpha(png_structp png_ptr) +png_set_swap_alpha(png_structrp png_ptr) { png_debug(1, "in png_set_swap_alpha"); @@ -182,7 +233,7 @@ png_set_swap_alpha(png_structp png_ptr) #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) void PNGAPI -png_set_invert_alpha(png_structp png_ptr) +png_set_invert_alpha(png_structrp png_ptr) { png_debug(1, "in png_set_invert_alpha"); @@ -195,7 +246,7 @@ png_set_invert_alpha(png_structp png_ptr) #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) void PNGAPI -png_set_invert_mono(png_structp png_ptr) +png_set_invert_mono(png_structrp png_ptr) { png_debug(1, "in png_set_invert_mono"); @@ -262,7 +313,7 @@ png_do_invert(png_row_infop row_info, png_bytep row) #ifdef PNG_16BIT_SUPPORTED #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) -/* Swaps byte order on 16 bit depth images */ +/* Swaps byte order on 16-bit depth images */ void /* PRIVATE */ png_do_swap(png_row_infop row_info, png_bytep row) { @@ -276,9 +327,16 @@ png_do_swap(png_row_infop row_info, png_bytep row) for (i = 0; i < istop; i++, rp += 2) { +#ifdef PNG_BUILTIN_BSWAP16_SUPPORTED + /* Feature added to libpng-1.6.11 for testing purposes, not + * enabled by default. + */ + *(png_uint_16*)rp = __builtin_bswap16(*(png_uint_16*)rp); +#else png_byte t = *rp; *rp = *(rp + 1); *(rp + 1) = t; +#endif } } } @@ -420,7 +478,7 @@ png_do_packswap(png_row_infop row_info, png_bytep row) *rp = table[*rp]; } } -#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */ +#endif /* PACKSWAP || WRITE_PACKSWAP */ #if defined(PNG_WRITE_FILLER_SUPPORTED) || \ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) @@ -452,7 +510,7 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start) { if (row_info->bit_depth == 8) { - if (at_start) /* Skip initial filler */ + if (at_start != 0) /* Skip initial filler */ ++sp; else /* Skip initial channel and, for sp, the filler */ sp += 2, ++dp; @@ -466,7 +524,7 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start) else if (row_info->bit_depth == 16) { - if (at_start) /* Skip initial filler */ + if (at_start != 0) /* Skip initial filler */ sp += 2; else /* Skip initial channel and, for sp, the filler */ sp += 4, dp += 2; @@ -492,7 +550,7 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start) { if (row_info->bit_depth == 8) { - if (at_start) /* Skip initial filler */ + if (at_start != 0) /* Skip initial filler */ ++sp; else /* Skip initial channels and, for sp, the filler */ sp += 4, dp += 3; @@ -506,7 +564,7 @@ png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start) else if (row_info->bit_depth == 16) { - if (at_start) /* Skip initial filler */ + if (at_start != 0) /* Skip initial filler */ sp += 2; else /* Skip initial channels and, for sp, the filler */ sp += 8, dp += 6; @@ -547,7 +605,7 @@ png_do_bgr(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_bgr"); - if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) + if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) { png_uint_32 row_width = row_info->width; if (row_info->bit_depth == 8) @@ -617,13 +675,13 @@ png_do_bgr(png_row_infop row_info, png_bytep row) #endif } } -#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */ +#endif /* READ_BGR || WRITE_BGR */ #if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \ defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED) /* Added at libpng-1.5.10 */ void /* PRIVATE */ -png_do_check_palette_indexes(png_structp png_ptr, png_row_infop row_info) +png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info) { if (png_ptr->num_palette < (1 << row_info->bit_depth) && png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */ @@ -646,7 +704,7 @@ png_do_check_palette_indexes(png_structp png_ptr, png_row_infop row_info) */ for (; rp > png_ptr->row_buf; rp--) { - if (*rp >> padding != 0) + if ((*rp >> padding) != 0) png_ptr->num_palette_max = 1; padding = 0; } @@ -720,19 +778,30 @@ png_do_check_palette_indexes(png_structp png_ptr, png_row_infop row_info) } } } -#endif /* PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED */ +#endif /* CHECK_FOR_INVALID_INDEX */ #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED void PNGAPI -png_set_user_transform_info(png_structp png_ptr, png_voidp +png_set_user_transform_info(png_structrp png_ptr, png_voidp user_transform_ptr, int user_transform_depth, int user_transform_channels) { png_debug(1, "in png_set_user_transform_info"); if (png_ptr == NULL) return; + +#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED + if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && + (png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) + { + png_app_error(png_ptr, + "info change after png_start_read_image or png_read_update_info"); + return; + } +#endif + png_ptr->user_transform_ptr = user_transform_ptr; png_ptr->user_transform_depth = (png_byte)user_transform_depth; png_ptr->user_transform_channels = (png_byte)user_transform_channels; @@ -746,20 +815,20 @@ png_set_user_transform_info(png_structp png_ptr, png_voidp */ #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED png_voidp PNGAPI -png_get_user_transform_ptr(png_const_structp png_ptr) +png_get_user_transform_ptr(png_const_structrp png_ptr) { if (png_ptr == NULL) return (NULL); - return ((png_voidp)png_ptr->user_transform_ptr); + return png_ptr->user_transform_ptr; } #endif #ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED png_uint_32 PNGAPI -png_get_current_row_number(png_const_structp png_ptr) +png_get_current_row_number(png_const_structrp png_ptr) { - /* See the comments in png.h - this is the sub-image row when reading and + /* See the comments in png.h - this is the sub-image row when reading an * interlaced image. */ if (png_ptr != NULL) @@ -769,13 +838,12 @@ png_get_current_row_number(png_const_structp png_ptr) } png_byte PNGAPI -png_get_current_pass_number(png_const_structp png_ptr) +png_get_current_pass_number(png_const_structrp png_ptr) { if (png_ptr != NULL) return png_ptr->pass; return 8; /* invalid */ } -#endif /* PNG_USER_TRANSFORM_INFO_SUPPORTED */ -#endif /* PNG_READ_USER_TRANSFORM_SUPPORTED || - PNG_WRITE_USER_TRANSFORM_SUPPORTED */ -#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ +#endif /* USER_TRANSFORM_INFO */ +#endif /* READ_USER_TRANSFORM || WRITE_USER_TRANSFORM */ +#endif /* READ || WRITE */ diff --git a/3rdparty/libpng/pngwio.c b/3rdparty/libpng/pngwio.c index 95ffb3429..db76e6b8d 100644 --- a/3rdparty/libpng/pngwio.c +++ b/3rdparty/libpng/pngwio.c @@ -1,8 +1,8 @@ /* pngwio.c - functions for data output * - * Last changed in libpng 1.5.0 [January 6, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.15 [November 20, 2014] + * Copyright (c) 1998-2014 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -26,15 +26,16 @@ * writes to a file pointer. Note that this routine sometimes gets called * with very small lengths, so you should implement some kind of simple * buffering if you are using unbuffered writes. This should never be asked - * to write more than 64K on a 16 bit machine. + * to write more than 64K on a 16-bit machine. */ void /* PRIVATE */ -png_write_data(png_structp png_ptr, png_const_bytep data, png_size_t length) +png_write_data(png_structrp png_ptr, png_const_bytep data, png_size_t length) { /* NOTE: write_data_fn must not change the buffer! */ if (png_ptr->write_data_fn != NULL ) - (*(png_ptr->write_data_fn))(png_ptr, (png_bytep)data, length); + (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data), + length); else png_error(png_ptr, "Call to NULL write function"); @@ -46,7 +47,6 @@ png_write_data(png_structp png_ptr, png_const_bytep data, png_size_t length) * write_data function and use it at run time with png_set_write_fn(), rather * than changing the library. */ -#ifndef USE_FAR_KEYWORD void PNGCBAPI png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { @@ -60,64 +60,6 @@ png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) if (check != length) png_error(png_ptr, "Write Error"); } -#else -/* This is the model-independent version. Since the standard I/O library - * can't handle far buffers in the medium and small models, we have to copy - * the data. - */ - -#define NEAR_BUF_SIZE 1024 -#define MIN(a,b) (a <= b ? a : b) - -void PNGCBAPI -png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) -{ - png_uint_32 check; - png_byte *near_data; /* Needs to be "png_byte *" instead of "png_bytep" */ - png_FILE_p io_ptr; - - if (png_ptr == NULL) - return; - - /* Check if data really is near. If so, use usual code. */ - near_data = (png_byte *)CVT_PTR_NOCHECK(data); - io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); - - if ((png_bytep)near_data == data) - { - check = fwrite(near_data, 1, length, io_ptr); - } - - else - { - png_byte buf[NEAR_BUF_SIZE]; - png_size_t written, remaining, err; - check = 0; - remaining = length; - - do - { - written = MIN(NEAR_BUF_SIZE, remaining); - png_memcpy(buf, data, written); /* Copy far buffer to near buffer */ - err = fwrite(buf, 1, written, io_ptr); - - if (err != written) - break; - - else - check += err; - - data += written; - remaining -= written; - } - while (remaining != 0); - } - - if (check != length) - png_error(png_ptr, "Write Error"); -} - -#endif #endif /* This function is called to output any data pending writing (normally @@ -126,7 +68,7 @@ png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length) */ #ifdef PNG_WRITE_FLUSH_SUPPORTED void /* PRIVATE */ -png_flush(png_structp png_ptr) +png_flush(png_structrp png_ptr) { if (png_ptr->output_flush_fn != NULL) (*(png_ptr->output_flush_fn))(png_ptr); @@ -141,7 +83,7 @@ png_default_flush(png_structp png_ptr) if (png_ptr == NULL) return; - io_ptr = (png_FILE_p)CVT_PTR((png_ptr->io_ptr)); + io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr)); fflush(io_ptr); } # endif @@ -177,7 +119,7 @@ png_default_flush(png_structp png_ptr) * *FILE structure. */ void PNGAPI -png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, +png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) { if (png_ptr == NULL) @@ -207,8 +149,11 @@ png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, # else png_ptr->output_flush_fn = output_flush_fn; # endif -#endif /* PNG_WRITE_FLUSH_SUPPORTED */ +#else + PNG_UNUSED(output_flush_fn) +#endif /* WRITE_FLUSH */ +#ifdef PNG_READ_SUPPORTED /* It is an error to read while writing a png file */ if (png_ptr->read_data_fn != NULL) { @@ -218,37 +163,6 @@ png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, "Can't set both read_data_fn and write_data_fn in the" " same structure"); } -} - -#ifdef USE_FAR_KEYWORD -# ifdef _MSC_VER -void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) -{ - void *near_ptr; - void FAR *far_ptr; - FP_OFF(near_ptr) = FP_OFF(ptr); - far_ptr = (void FAR *)near_ptr; - - if (check != 0) - if (FP_SEG(ptr) != FP_SEG(far_ptr)) - png_error(png_ptr, "segment lost in conversion"); - - return(near_ptr); -} -# else -void *png_far_to_near(png_structp png_ptr, png_voidp ptr, int check) -{ - void *near_ptr; - void FAR *far_ptr; - near_ptr = (void FAR *)ptr; - far_ptr = (void FAR *)near_ptr; - - if (check != 0) - if (far_ptr != ptr) - png_error(png_ptr, "segment lost in conversion"); - - return(near_ptr); -} -# endif #endif -#endif /* PNG_WRITE_SUPPORTED */ +} +#endif /* WRITE */ diff --git a/3rdparty/libpng/pngwrite.c b/3rdparty/libpng/pngwrite.c index 2a72ad33f..0d4ee9f7a 100644 --- a/3rdparty/libpng/pngwrite.c +++ b/3rdparty/libpng/pngwrite.c @@ -1,8 +1,8 @@ /* pngwrite.c - general routines to write a PNG file * - * Last changed in libpng 1.5.11 [June 14, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -12,9 +12,65 @@ */ #include "pngpriv.h" +#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) +# include +#endif #ifdef PNG_WRITE_SUPPORTED +#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED +/* Write out all the unknown chunks for the current given location */ +static void +write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr, + unsigned int where) +{ + if (info_ptr->unknown_chunks_num != 0) + { + png_const_unknown_chunkp up; + + png_debug(5, "writing extra chunks"); + + for (up = info_ptr->unknown_chunks; + up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; + ++up) + if ((up->location & where) != 0) + { + /* If per-chunk unknown chunk handling is enabled use it, otherwise + * just write the chunks the application has set. + */ +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + int keep = png_handle_as_unknown(png_ptr, up->name); + + /* NOTE: this code is radically different from the read side in the + * matter of handling an ancillary unknown chunk. In the read side + * the default behavior is to discard it, in the code below the default + * behavior is to write it. Critical chunks are, however, only + * written if explicitly listed or if the default is set to write all + * unknown chunks. + * + * The default handling is also slightly weird - it is not possible to + * stop the writing of all unsafe-to-copy chunks! + * + * TODO: REVIEW: this would seem to be a bug. + */ + if (keep != PNG_HANDLE_CHUNK_NEVER && + ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ || + keep == PNG_HANDLE_CHUNK_ALWAYS || + (keep == PNG_HANDLE_CHUNK_AS_DEFAULT && + png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS))) +#endif + { + /* TODO: review, what is wrong with a zero length unknown chunk? */ + if (up->size == 0) + png_warning(png_ptr, "Writing zero-length unknown chunk"); + + png_write_chunk(png_ptr, up->name, up->data, up->size); + } + } + } +} +#endif /* WRITE_UNKNOWN_CHUNKS */ + /* Writes all the PNG information. This is the suggested way to use the * library. If you have a new chunk to add, make a function to write it, * and put it in the correct location here. If you want the chunk written @@ -25,101 +81,115 @@ * them in png_write_end(), and compressing them. */ void PNGAPI -png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr) +png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr) { png_debug(1, "in png_write_info_before_PLTE"); if (png_ptr == NULL || info_ptr == NULL) return; - if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) + if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) { - /* Write PNG signature */ - png_write_sig(png_ptr); + /* Write PNG signature */ + png_write_sig(png_ptr); #ifdef PNG_MNG_FEATURES_SUPPORTED - if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \ - (png_ptr->mng_features_permitted)) - { - png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); - png_ptr->mng_features_permitted = 0; - } + if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \ + png_ptr->mng_features_permitted != 0) + { + png_warning(png_ptr, + "MNG features are not allowed in a PNG datastream"); + png_ptr->mng_features_permitted = 0; + } #endif - /* Write IHDR information. */ - png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, - info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, - info_ptr->filter_type, + /* Write IHDR information. */ + png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, + info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, + info_ptr->filter_type, #ifdef PNG_WRITE_INTERLACING_SUPPORTED - info_ptr->interlace_type); + info_ptr->interlace_type #else - 0); + 0 #endif - /* The rest of these check to see if the valid field has the appropriate - * flag set, and if it does, writes the chunk. - */ -#ifdef PNG_WRITE_gAMA_SUPPORTED - if (info_ptr->valid & PNG_INFO_gAMA) - png_write_gAMA_fixed(png_ptr, info_ptr->gamma); -#endif -#ifdef PNG_WRITE_sRGB_SUPPORTED - if (info_ptr->valid & PNG_INFO_sRGB) - png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent); + ); + + /* The rest of these check to see if the valid field has the appropriate + * flag set, and if it does, writes the chunk. + * + * 1.6.0: COLORSPACE support controls the writing of these chunks too, and + * the chunks will be written if the WRITE routine is there and + * information * is available in the COLORSPACE. (See + * png_colorspace_sync_info in png.c for where the valid flags get set.) + * + * Under certain circumstances the colorspace can be invalidated without + * syncing the info_struct 'valid' flags; this happens if libpng detects + * an error and calls png_error while the color space is being set, yet + * the application continues writing the PNG. So check the 'invalid' + * flag here too. + */ +#ifdef PNG_GAMMA_SUPPORTED +# ifdef PNG_WRITE_gAMA_SUPPORTED + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && + (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 && + (info_ptr->valid & PNG_INFO_gAMA) != 0) + png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma); +# endif #endif -#ifdef PNG_WRITE_iCCP_SUPPORTED - if (info_ptr->valid & PNG_INFO_iCCP) - png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE, - (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen); -#endif +#ifdef PNG_COLORSPACE_SUPPORTED + /* Write only one of sRGB or an ICC profile. If a profile was supplied + * and it matches one of the known sRGB ones issue a warning. + */ +# ifdef PNG_WRITE_iCCP_SUPPORTED + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && + (info_ptr->valid & PNG_INFO_iCCP) != 0) + { +# ifdef PNG_WRITE_sRGB_SUPPORTED + if ((info_ptr->valid & PNG_INFO_sRGB) != 0) + png_app_warning(png_ptr, + "profile matches sRGB but writing iCCP instead"); +# endif + + png_write_iCCP(png_ptr, info_ptr->iccp_name, + info_ptr->iccp_profile); + } +# ifdef PNG_WRITE_sRGB_SUPPORTED + else +# endif +# endif + +# ifdef PNG_WRITE_sRGB_SUPPORTED + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && + (info_ptr->valid & PNG_INFO_sRGB) != 0) + png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent); +# endif /* WRITE_sRGB */ +#endif /* COLORSPACE */ + #ifdef PNG_WRITE_sBIT_SUPPORTED - if (info_ptr->valid & PNG_INFO_sBIT) - png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); + if ((info_ptr->valid & PNG_INFO_sBIT) != 0) + png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); #endif -#ifdef PNG_WRITE_cHRM_SUPPORTED - if (info_ptr->valid & PNG_INFO_cHRM) - png_write_cHRM_fixed(png_ptr, - info_ptr->x_white, info_ptr->y_white, - info_ptr->x_red, info_ptr->y_red, - info_ptr->x_green, info_ptr->y_green, - info_ptr->x_blue, info_ptr->y_blue); + +#ifdef PNG_COLORSPACE_SUPPORTED +# ifdef PNG_WRITE_cHRM_SUPPORTED + if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && + (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 && + (info_ptr->valid & PNG_INFO_cHRM) != 0) + png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy); +# endif #endif #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED - if (info_ptr->unknown_chunks_num) - { - png_unknown_chunk *up; - - png_debug(5, "writing extra chunks"); - - for (up = info_ptr->unknown_chunks; - up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; - up++) - { - int keep = png_handle_as_unknown(png_ptr, up->name); - - if (keep != PNG_HANDLE_CHUNK_NEVER && - up->location && - !(up->location & PNG_HAVE_PLTE) && - !(up->location & PNG_HAVE_IDAT) && - !(up->location & PNG_AFTER_IDAT) && - ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || - (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) - { - if (up->size == 0) - png_warning(png_ptr, "Writing zero-length unknown chunk"); - - png_write_chunk(png_ptr, up->name, up->data, up->size); - } - } - } + write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR); #endif + png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; } } void PNGAPI -png_write_info(png_structp png_ptr, png_infop info_ptr) +png_write_info(png_structrp png_ptr, png_const_inforp info_ptr) { #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) int i; @@ -132,7 +202,7 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) png_write_info_before_PLTE(png_ptr, info_ptr); - if (info_ptr->valid & PNG_INFO_PLTE) + if ((info_ptr->valid & PNG_INFO_PLTE) != 0) png_write_PLTE(png_ptr, info_ptr->palette, (png_uint_32)info_ptr->num_palette); @@ -140,15 +210,20 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) png_error(png_ptr, "Valid palette required for paletted images"); #ifdef PNG_WRITE_tRNS_SUPPORTED - if (info_ptr->valid & PNG_INFO_tRNS) + if ((info_ptr->valid & PNG_INFO_tRNS) !=0) { #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel (in tRNS) */ - if ((png_ptr->transformations & PNG_INVERT_ALPHA) && + if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 && info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { - int j; - for (j = 0; j<(int)info_ptr->num_trans; j++) + int j, jend; + + jend = info_ptr->num_trans; + if (jend > PNG_MAX_PALETTE_LENGTH) + jend = PNG_MAX_PALETTE_LENGTH; + + for (j = 0; jtrans_alpha[j] = (png_byte)(255 - info_ptr->trans_alpha[j]); } @@ -158,42 +233,42 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) } #endif #ifdef PNG_WRITE_bKGD_SUPPORTED - if (info_ptr->valid & PNG_INFO_bKGD) + if ((info_ptr->valid & PNG_INFO_bKGD) != 0) png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); #endif #ifdef PNG_WRITE_hIST_SUPPORTED - if (info_ptr->valid & PNG_INFO_hIST) + if ((info_ptr->valid & PNG_INFO_hIST) != 0) png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); #endif #ifdef PNG_WRITE_oFFs_SUPPORTED - if (info_ptr->valid & PNG_INFO_oFFs) + if ((info_ptr->valid & PNG_INFO_oFFs) != 0) png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, info_ptr->offset_unit_type); #endif #ifdef PNG_WRITE_pCAL_SUPPORTED - if (info_ptr->valid & PNG_INFO_pCAL) + if ((info_ptr->valid & PNG_INFO_pCAL) != 0) png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, info_ptr->pcal_units, info_ptr->pcal_params); #endif #ifdef PNG_WRITE_sCAL_SUPPORTED - if (info_ptr->valid & PNG_INFO_sCAL) + if ((info_ptr->valid & PNG_INFO_sCAL) != 0) png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, info_ptr->scal_s_width, info_ptr->scal_s_height); #endif /* sCAL */ #ifdef PNG_WRITE_pHYs_SUPPORTED - if (info_ptr->valid & PNG_INFO_pHYs) + if ((info_ptr->valid & PNG_INFO_pHYs) != 0) png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); #endif /* pHYs */ #ifdef PNG_WRITE_tIME_SUPPORTED - if (info_ptr->valid & PNG_INFO_tIME) + if ((info_ptr->valid & PNG_INFO_tIME) != 0) { png_write_tIME(png_ptr, &(info_ptr->mod_time)); png_ptr->mode |= PNG_WROTE_tIME; @@ -201,7 +276,7 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) #endif /* tIME */ #ifdef PNG_WRITE_sPLT_SUPPORTED - if (info_ptr->valid & PNG_INFO_sPLT) + if ((info_ptr->valid & PNG_INFO_sPLT) != 0) for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); #endif /* sPLT */ @@ -223,11 +298,14 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) info_ptr->text[i].lang, info_ptr->text[i].lang_key, info_ptr->text[i].text); + /* Mark this chunk as written */ + if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + else + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; #else - png_warning(png_ptr, "Unable to write international text"); + png_warning(png_ptr, "Unable to write international text"); #endif - /* Mark this chunk as written */ - info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } /* If we want a compressed text chunk */ @@ -236,13 +314,12 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write compressed chunk */ png_write_zTXt(png_ptr, info_ptr->text[i].key, - info_ptr->text[i].text, 0, - info_ptr->text[i].compression); + info_ptr->text[i].text, info_ptr->text[i].compression); + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; #else png_warning(png_ptr, "Unable to write compressed text"); #endif - /* Mark this chunk as written */ - info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; } else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) @@ -263,29 +340,7 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) #endif /* tEXt */ #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED - if (info_ptr->unknown_chunks_num) - { - png_unknown_chunk *up; - - png_debug(5, "writing extra chunks"); - - for (up = info_ptr->unknown_chunks; - up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; - up++) - { - int keep = png_handle_as_unknown(png_ptr, up->name); - if (keep != PNG_HANDLE_CHUNK_NEVER && - up->location && - (up->location & PNG_HAVE_PLTE) && - !(up->location & PNG_HAVE_IDAT) && - !(up->location & PNG_AFTER_IDAT) && - ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || - (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) - { - png_write_chunk(png_ptr, up->name, up->data, up->size); - } - } - } + write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE); #endif } @@ -295,14 +350,14 @@ png_write_info(png_structp png_ptr, png_infop info_ptr) * comments, I suggest writing them here, and compressing them. */ void PNGAPI -png_write_end(png_structp png_ptr, png_infop info_ptr) +png_write_end(png_structrp png_ptr, png_inforp info_ptr) { png_debug(1, "in png_write_end"); if (png_ptr == NULL) return; - if (!(png_ptr->mode & PNG_HAVE_IDAT)) + if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) png_error(png_ptr, "No IDATs written into file"); #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED @@ -318,8 +373,8 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) #endif #ifdef PNG_WRITE_tIME_SUPPORTED /* Check to see if user has supplied a time chunk */ - if ((info_ptr->valid & PNG_INFO_tIME) && - !(png_ptr->mode & PNG_WROTE_tIME)) + if ((info_ptr->valid & PNG_INFO_tIME) != 0 && + (png_ptr->mode & PNG_WROTE_tIME) == 0) png_write_tIME(png_ptr, &(info_ptr->mod_time)); #endif @@ -340,11 +395,14 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) info_ptr->text[i].lang, info_ptr->text[i].lang_key, info_ptr->text[i].text); + /* Mark this chunk as written */ + if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; + else + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; #else png_warning(png_ptr, "Unable to write international text"); #endif - /* Mark this chunk as written */ - info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) @@ -352,13 +410,12 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write compressed chunk */ png_write_zTXt(png_ptr, info_ptr->text[i].key, - info_ptr->text[i].text, 0, - info_ptr->text[i].compression); + info_ptr->text[i].text, info_ptr->text[i].compression); + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; #else png_warning(png_ptr, "Unable to write compressed text"); #endif - /* Mark this chunk as written */ - info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; } else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) @@ -367,37 +424,16 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) /* Write uncompressed chunk */ png_write_tEXt(png_ptr, info_ptr->text[i].key, info_ptr->text[i].text, 0); + /* Mark this chunk as written */ + info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; #else png_warning(png_ptr, "Unable to write uncompressed text"); #endif - - /* Mark this chunk as written */ - info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; } } #endif #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED - if (info_ptr->unknown_chunks_num) - { - png_unknown_chunk *up; - - png_debug(5, "writing extra chunks"); - - for (up = info_ptr->unknown_chunks; - up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; - up++) - { - int keep = png_handle_as_unknown(png_ptr, up->name); - if (keep != PNG_HANDLE_CHUNK_NEVER && - up->location && - (up->location & PNG_AFTER_IDAT) && - ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS || - (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS))) - { - png_write_chunk(png_ptr, up->name, up->data, up->size); - } - } - } + write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT); #endif } @@ -405,6 +441,7 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) /* Write end of PNG file */ png_write_IEND(png_ptr); + /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, * and restored again in libpng-1.2.30, may cause some applications that * do not set png_ptr->output_flush_fn to crash. If your application @@ -420,9 +457,8 @@ png_write_end(png_structp png_ptr, png_infop info_ptr) } #ifdef PNG_CONVERT_tIME_SUPPORTED -/* "tm" structure is not supported on WindowsCE */ void PNGAPI -png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime) +png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime) { png_debug(1, "in png_convert_from_struct_tm"); @@ -451,104 +487,75 @@ PNG_FUNCTION(png_structp,PNGAPI png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) { -#ifdef PNG_USER_MEM_SUPPORTED - return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn, - warn_fn, NULL, NULL, NULL)); +#ifndef PNG_USER_MEM_SUPPORTED + png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, + error_fn, warn_fn, NULL, NULL, NULL); +#else + return png_create_write_struct_2(user_png_ver, error_ptr, error_fn, + warn_fn, NULL, NULL, NULL); } /* Alternate initialize png_ptr structure, and allocate any memory needed */ -static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */ - PNG_FUNCTION(png_structp,PNGAPI png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) { -#endif /* PNG_USER_MEM_SUPPORTED */ - volatile int png_cleanup_needed = 0; -#ifdef PNG_SETJMP_SUPPORTED - volatile -#endif - png_structp png_ptr; -#ifdef PNG_SETJMP_SUPPORTED -#ifdef USE_FAR_KEYWORD - jmp_buf tmp_jmpbuf; -#endif -#endif - - png_debug(1, "in png_create_write_struct"); - -#ifdef PNG_USER_MEM_SUPPORTED - png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, - (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr); -#else - png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); -#endif /* PNG_USER_MEM_SUPPORTED */ - if (png_ptr == NULL) - return (NULL); - - /* Added at libpng-1.2.6 */ -#ifdef PNG_SET_USER_LIMITS_SUPPORTED - png_ptr->user_width_max = PNG_USER_WIDTH_MAX; - png_ptr->user_height_max = PNG_USER_HEIGHT_MAX; -#endif - -#ifdef PNG_SETJMP_SUPPORTED -/* Applications that neglect to set up their own setjmp() and then - * encounter a png_error() will longjmp here. Since the jmpbuf is - * then meaningless we abort instead of returning. - */ -#ifdef USE_FAR_KEYWORD - if (setjmp(tmp_jmpbuf)) -#else - if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */ -#endif -#ifdef USE_FAR_KEYWORD - png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf)); -#endif - PNG_ABORT(); -#endif - -#ifdef PNG_USER_MEM_SUPPORTED - png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn); -#endif /* PNG_USER_MEM_SUPPORTED */ - png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); - - if (!png_user_version_check(png_ptr, user_png_ver)) - png_cleanup_needed = 1; - - /* Initialize zbuf - compression buffer */ - png_ptr->zbuf_size = PNG_ZBUF_SIZE; - - if (!png_cleanup_needed) + png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, + error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); +#endif /* USER_MEM */ + if (png_ptr != NULL) { - png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, - png_ptr->zbuf_size); - if (png_ptr->zbuf == NULL) - png_cleanup_needed = 1; + /* Set the zlib control values to defaults; they can be overridden by the + * application after the struct has been created. + */ + png_ptr->zbuffer_size = PNG_ZBUF_SIZE; + + /* The 'zlib_strategy' setting is irrelevant because png_default_claim in + * pngwutil.c defaults it according to whether or not filters will be + * used, and ignores this setting. + */ + png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY; + png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION; + png_ptr->zlib_mem_level = 8; + png_ptr->zlib_window_bits = 15; + png_ptr->zlib_method = 8; + +#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED + png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY; + png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION; + png_ptr->zlib_text_mem_level = 8; + png_ptr->zlib_text_window_bits = 15; + png_ptr->zlib_text_method = 8; +#endif /* WRITE_COMPRESSED_TEXT */ + + /* This is a highly dubious configuration option; by default it is off, + * but it may be appropriate for private builds that are testing + * extensions not conformant to the current specification, or of + * applications that must not fail to write at all costs! + */ +#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED + /* In stable builds only warn if an application error can be completely + * handled. + */ + png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; +#endif + + /* App warnings are warnings in release (or release candidate) builds but + * are errors during development. + */ +#if PNG_RELEASE_BUILD + png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; +#endif + + /* TODO: delay this, it can be done in png_init_io() (if the app doesn't + * do it itself) avoiding setting the default function if it is not + * required. + */ + png_set_write_fn(png_ptr, NULL, NULL, NULL); } - if (png_cleanup_needed) - { - /* Clean up PNG structure and deallocate any memory. */ - png_free(png_ptr, png_ptr->zbuf); - png_ptr->zbuf = NULL; -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)png_ptr, - (png_free_ptr)free_fn, (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)png_ptr); -#endif - return (NULL); - } - - png_set_write_fn(png_ptr, NULL, NULL, NULL); - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - png_reset_filter_heuristics(png_ptr); -#endif - - return (png_ptr); + return png_ptr; } @@ -558,7 +565,7 @@ png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, * "write" the image seven times. */ void PNGAPI -png_write_rows(png_structp png_ptr, png_bytepp row, +png_write_rows(png_structrp png_ptr, png_bytepp row, png_uint_32 num_rows) { png_uint_32 i; /* row counter */ @@ -580,7 +587,7 @@ png_write_rows(png_structp png_ptr, png_bytepp row, * if you are writing an interlaced image. */ void PNGAPI -png_write_image(png_structp png_ptr, png_bytepp image) +png_write_image(png_structrp png_ptr, png_bytepp image) { png_uint_32 i; /* row index */ int pass, num_pass; /* pass variables */ @@ -610,9 +617,74 @@ png_write_image(png_structp png_ptr, png_bytepp image) } } +#ifdef PNG_MNG_FEATURES_SUPPORTED +/* Performs intrapixel differencing */ +static void +png_do_write_intrapixel(png_row_infop row_info, png_bytep row) +{ + png_debug(1, "in png_do_write_intrapixel"); + + if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) + { + int bytes_per_pixel; + png_uint_32 row_width = row_info->width; + if (row_info->bit_depth == 8) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 3; + + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 4; + + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + *(rp) = (png_byte)(*rp - *(rp + 1)); + *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1)); + } + } + +#ifdef PNG_WRITE_16BIT_SUPPORTED + else if (row_info->bit_depth == 16) + { + png_bytep rp; + png_uint_32 i; + + if (row_info->color_type == PNG_COLOR_TYPE_RGB) + bytes_per_pixel = 6; + + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) + bytes_per_pixel = 8; + + else + return; + + for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) + { + png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); + png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); + png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); + png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); + png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); + *(rp ) = (png_byte)(red >> 8); + *(rp + 1) = (png_byte)red; + *(rp + 4) = (png_byte)(blue >> 8); + *(rp + 5) = (png_byte)blue; + } + } +#endif /* WRITE_16BIT */ + } +} +#endif /* MNG_FEATURES */ + /* Called by user to write a row of image data */ void PNGAPI -png_write_row(png_structp png_ptr, png_const_bytep row) +png_write_row(png_structrp png_ptr, png_const_bytep row) { /* 1.5.6: moved from png_struct to be a local structure: */ png_row_info row_info; @@ -627,44 +699,44 @@ png_write_row(png_structp png_ptr, png_const_bytep row) if (png_ptr->row_number == 0 && png_ptr->pass == 0) { /* Make sure we wrote the header info */ - if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE)) + if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) png_error(png_ptr, "png_write_info was never called before png_write_row"); /* Check for transforms that have been set but were defined out */ #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) - if (png_ptr->transformations & PNG_INVERT_MONO) + if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) - if (png_ptr->transformations & PNG_FILLER) + if ((png_ptr->transformations & PNG_FILLER) != 0) png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ defined(PNG_READ_PACKSWAP_SUPPORTED) - if (png_ptr->transformations & PNG_PACKSWAP) + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) - if (png_ptr->transformations & PNG_PACK) + if ((png_ptr->transformations & PNG_PACK) != 0) png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) - if (png_ptr->transformations & PNG_SHIFT) + if ((png_ptr->transformations & PNG_SHIFT) != 0) png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) - if (png_ptr->transformations & PNG_BGR) + if ((png_ptr->transformations & PNG_BGR) != 0) png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); #endif #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) - if (png_ptr->transformations & PNG_SWAP_BYTES) + if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); #endif @@ -673,12 +745,13 @@ png_write_row(png_structp png_ptr, png_const_bytep row) #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced and not interested in row, return */ - if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) + if (png_ptr->interlaced != 0 && + (png_ptr->transformations & PNG_INTERLACE) != 0) { switch (png_ptr->pass) { case 0: - if (png_ptr->row_number & 0x07) + if ((png_ptr->row_number & 0x07) != 0) { png_write_finish_row(png_ptr); return; @@ -686,7 +759,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) break; case 1: - if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) + if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5) { png_write_finish_row(png_ptr); return; @@ -702,7 +775,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) break; case 3: - if ((png_ptr->row_number & 0x03) || png_ptr->width < 3) + if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3) { png_write_finish_row(png_ptr); return; @@ -718,7 +791,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) break; case 5: - if ((png_ptr->row_number & 0x01) || png_ptr->width < 2) + if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2) { png_write_finish_row(png_ptr); return; @@ -726,7 +799,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) break; case 6: - if (!(png_ptr->row_number & 0x01)) + if ((png_ptr->row_number & 0x01) == 0) { png_write_finish_row(png_ptr); return; @@ -755,16 +828,16 @@ png_write_row(png_structp png_ptr, png_const_bytep row) png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes); /* Copy user's row into buffer, leaving room for filter byte. */ - png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); + memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Handle interlacing */ if (png_ptr->interlaced && png_ptr->pass < 6 && - (png_ptr->transformations & PNG_INTERLACE)) + (png_ptr->transformations & PNG_INTERLACE) != 0) { png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass); /* This should always get caught above, but still ... */ - if (!(row_info.width)) + if (row_info.width == 0) { png_write_finish_row(png_ptr); return; @@ -774,7 +847,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED /* Handle other transformations */ - if (png_ptr->transformations) + if (png_ptr->transformations != 0) png_do_write_transformations(png_ptr, &row_info); #endif @@ -782,7 +855,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) * which is also the output depth. */ if (row_info.pixel_depth != png_ptr->pixel_depth || - row_info.pixel_depth != png_ptr->transformed_pixel_depth) + row_info.pixel_depth != png_ptr->transformed_pixel_depth) png_error(png_ptr, "internal write transform logic error"); #ifdef PNG_MNG_FEATURES_SUPPORTED @@ -795,7 +868,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) * 4. The filter_method is 64 and * 5. The color_type is RGB or RGBA */ - if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) { /* Intrapixel differencing */ @@ -821,7 +894,7 @@ png_write_row(png_structp png_ptr, png_const_bytep row) #ifdef PNG_WRITE_FLUSH_SUPPORTED /* Set the automatic flush interval or 0 to turn flushing off */ void PNGAPI -png_set_flush(png_structp png_ptr, int nrows) +png_set_flush(png_structrp png_ptr, int nrows) { png_debug(1, "in png_set_flush"); @@ -833,10 +906,8 @@ png_set_flush(png_structp png_ptr, int nrows) /* Flush the current output buffers now */ void PNGAPI -png_write_flush(png_structp png_ptr) +png_write_flush(png_structrp png_ptr) { - int wrote_IDAT; - png_debug(1, "in png_write_flush"); if (png_ptr == NULL) @@ -846,182 +917,76 @@ png_write_flush(png_structp png_ptr) if (png_ptr->row_number >= png_ptr->num_rows) return; - do - { - int ret; - - /* Compress the data */ - ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); - wrote_IDAT = 0; - - /* Check for compression errors */ - if (ret != Z_OK) - { - if (png_ptr->zstream.msg != NULL) - png_error(png_ptr, png_ptr->zstream.msg); - - else - png_error(png_ptr, "zlib error"); - } - - if (!(png_ptr->zstream.avail_out)) - { - /* Write the IDAT and reset the zlib output buffer */ - png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - wrote_IDAT = 1; - } - } while (wrote_IDAT == 1); - - /* If there is any data left to be output, write it into a new IDAT */ - if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) - { - /* Write the IDAT and reset the zlib output buffer */ - png_write_IDAT(png_ptr, png_ptr->zbuf, - png_ptr->zbuf_size - png_ptr->zstream.avail_out); - } + png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH); png_ptr->flush_rows = 0; png_flush(png_ptr); } -#endif /* PNG_WRITE_FLUSH_SUPPORTED */ +#endif /* WRITE_FLUSH */ -/* Free all memory used by the write */ -void PNGAPI -png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) +/* Free any memory used in png_ptr struct without freeing the struct itself. */ +static void +png_write_destroy(png_structrp png_ptr) { - png_structp png_ptr = NULL; - png_infop info_ptr = NULL; -#ifdef PNG_USER_MEM_SUPPORTED - png_free_ptr free_fn = NULL; - png_voidp mem_ptr = NULL; -#endif - - png_debug(1, "in png_destroy_write_struct"); - - if (png_ptr_ptr != NULL) - png_ptr = *png_ptr_ptr; - -#ifdef PNG_USER_MEM_SUPPORTED - if (png_ptr != NULL) - { - free_fn = png_ptr->free_fn; - mem_ptr = png_ptr->mem_ptr; - } -#endif - - if (info_ptr_ptr != NULL) - info_ptr = *info_ptr_ptr; - - if (info_ptr != NULL) - { - if (png_ptr != NULL) - { - png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); - -#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED - if (png_ptr->num_chunk_list) - { - png_free(png_ptr, png_ptr->chunk_list); - png_ptr->num_chunk_list = 0; - } -#endif - } - -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn, - (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)info_ptr); -#endif - *info_ptr_ptr = NULL; - } - - if (png_ptr != NULL) - { - png_write_destroy(png_ptr); -#ifdef PNG_USER_MEM_SUPPORTED - png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn, - (png_voidp)mem_ptr); -#else - png_destroy_struct((png_voidp)png_ptr); -#endif - *png_ptr_ptr = NULL; - } -} - - -/* Free any memory used in png_ptr struct (old method) */ -void /* PRIVATE */ -png_write_destroy(png_structp png_ptr) -{ -#ifdef PNG_SETJMP_SUPPORTED - jmp_buf tmp_jmp; /* Save jump buffer */ -#endif - png_error_ptr error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - png_error_ptr warning_fn; -#endif - png_voidp error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - png_free_ptr free_fn; -#endif - png_debug(1, "in png_write_destroy"); /* Free any memory zlib uses */ - if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED) + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) deflateEnd(&png_ptr->zstream); /* Free our memory. png_free checks NULL for us. */ - png_free(png_ptr, png_ptr->zbuf); + png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); png_free(png_ptr, png_ptr->row_buf); + png_ptr->row_buf = NULL; #ifdef PNG_WRITE_FILTER_SUPPORTED png_free(png_ptr, png_ptr->prev_row); - png_free(png_ptr, png_ptr->sub_row); - png_free(png_ptr, png_ptr->up_row); - png_free(png_ptr, png_ptr->avg_row); - png_free(png_ptr, png_ptr->paeth_row); + png_free(png_ptr, png_ptr->try_row); + png_free(png_ptr, png_ptr->tst_row); + png_ptr->prev_row = NULL; + png_ptr->try_row = NULL; + png_ptr->tst_row = NULL; #endif -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - /* Use this to save a little code space, it doesn't free the filter_costs */ - png_reset_filter_heuristics(png_ptr); - png_free(png_ptr, png_ptr->filter_costs); - png_free(png_ptr, png_ptr->inv_filter_costs); +#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED + png_free(png_ptr, png_ptr->chunk_list); + png_ptr->chunk_list = NULL; #endif -#ifdef PNG_SETJMP_SUPPORTED - /* Reset structure */ - png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf)); -#endif + /* The error handling and memory handling information is left intact at this + * point: the jmp_buf may still have to be freed. See png_destroy_png_struct + * for how this happens. + */ +} - error_fn = png_ptr->error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - warning_fn = png_ptr->warning_fn; -#endif - error_ptr = png_ptr->error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - free_fn = png_ptr->free_fn; -#endif +/* Free all memory used by the write. + * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for + * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free + * the passed in info_structs but it would quietly fail to free any of the data + * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it + * has no png_ptr.) + */ +void PNGAPI +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) +{ + png_debug(1, "in png_destroy_write_struct"); - png_memset(png_ptr, 0, png_sizeof(png_struct)); + if (png_ptr_ptr != NULL) + { + png_structrp png_ptr = *png_ptr_ptr; - png_ptr->error_fn = error_fn; -#ifdef PNG_WARNINGS_SUPPORTED - png_ptr->warning_fn = warning_fn; -#endif - png_ptr->error_ptr = error_ptr; -#ifdef PNG_USER_MEM_SUPPORTED - png_ptr->free_fn = free_fn; -#endif + if (png_ptr != NULL) /* added in libpng 1.6.0 */ + { + png_destroy_info_struct(png_ptr, info_ptr_ptr); -#ifdef PNG_SETJMP_SUPPORTED - png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf)); -#endif + *png_ptr_ptr = NULL; + png_write_destroy(png_ptr); + png_destroy_png_struct(png_ptr); + } + } } /* Allow the application to select one or more row filters to use. */ void PNGAPI -png_set_filter(png_structp png_ptr, int method, int filters) +png_set_filter(png_structrp png_ptr, int method, int filters) { png_debug(1, "in png_set_filter"); @@ -1029,7 +994,7 @@ png_set_filter(png_structp png_ptr, int method, int filters) return; #ifdef PNG_MNG_FEATURES_SUPPORTED - if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && + if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && (method == PNG_INTRAPIXEL_DIFFERENCING)) method = PNG_FILTER_TYPE_BASE; @@ -1041,8 +1006,9 @@ png_set_filter(png_structp png_ptr, int method, int filters) #ifdef PNG_WRITE_FILTER_SUPPORTED case 5: case 6: - case 7: png_warning(png_ptr, "Unknown row filter for method 0"); -#endif /* PNG_WRITE_FILTER_SUPPORTED */ + case 7: png_app_error(png_ptr, "Unknown row filter for method 0"); + /* FALL THROUGH */ +#endif /* WRITE_FILTER */ case PNG_FILTER_VALUE_NONE: png_ptr->do_filter = PNG_FILTER_NONE; break; @@ -1063,367 +1029,152 @@ png_set_filter(png_structp png_ptr, int method, int filters) png_ptr->do_filter = (png_byte)filters; break; #else default: - png_warning(png_ptr, "Unknown row filter for method 0"); -#endif /* PNG_WRITE_FILTER_SUPPORTED */ + png_app_error(png_ptr, "Unknown row filter for method 0"); +#endif /* WRITE_FILTER */ } +#ifdef PNG_WRITE_FILTER_SUPPORTED /* If we have allocated the row_buf, this means we have already started * with the image and we should have allocated all of the filter buffers * that have been selected. If prev_row isn't already allocated, then * it is too late to start using the filters that need it, since we * will be missing the data in the previous row. If an application * wants to start and stop using particular filters during compression, - * it should start out with all of the filters, and then add and - * remove them after the start of compression. + * it should start out with all of the filters, and then remove them + * or add them back after the start of compression. + * + * NOTE: this is a nasty constraint on the code, because it means that the + * prev_row buffer must be maintained even if there are currently no + * 'prev_row' requiring filters active. */ if (png_ptr->row_buf != NULL) { -#ifdef PNG_WRITE_FILTER_SUPPORTED - if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL) + int num_filters; + png_alloc_size_t buf_size; + + /* Repeat the checks in png_write_start_row; 1 pixel high or wide + * images cannot benefit from certain filters. If this isn't done here + * the check below will fire on 1 pixel high images. + */ + if (png_ptr->height == 1) + filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); + + if (png_ptr->width == 1) + filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); + + if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0 + && png_ptr->prev_row == NULL) { - png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, - (png_ptr->rowbytes + 1)); - png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; + /* This is the error case, however it is benign - the previous row + * is not available so the filter can't be used. Just warn here. + */ + png_app_warning(png_ptr, + "png_set_filter: UP/AVG/PAETH cannot be added after start"); + filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); } - if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL) + num_filters = 0; + + if (filters & PNG_FILTER_SUB) + num_filters++; + + if (filters & PNG_FILTER_UP) + num_filters++; + + if (filters & PNG_FILTER_AVG) + num_filters++; + + if (filters & PNG_FILTER_PAETH) + num_filters++; + + /* Allocate needed row buffers if they have not already been + * allocated. + */ + buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth, + png_ptr->width) + 1; + + if (png_ptr->try_row == NULL) + png_ptr->try_row = png_voidcast(png_bytep, + png_malloc(png_ptr, buf_size)); + + if (num_filters > 1) { - if (png_ptr->prev_row == NULL) - { - png_warning(png_ptr, "Can't add Up filter after starting"); - png_ptr->do_filter = (png_byte)(png_ptr->do_filter & - ~PNG_FILTER_UP); - } - - else - { - png_ptr->up_row = (png_bytep)png_malloc(png_ptr, - (png_ptr->rowbytes + 1)); - png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; - } + if (png_ptr->tst_row == NULL) + png_ptr->tst_row = png_voidcast(png_bytep, + png_malloc(png_ptr, buf_size)); } - - if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL) - { - if (png_ptr->prev_row == NULL) - { - png_warning(png_ptr, "Can't add Average filter after starting"); - png_ptr->do_filter = (png_byte)(png_ptr->do_filter & - ~PNG_FILTER_AVG); - } - - else - { - png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, - (png_ptr->rowbytes + 1)); - png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; - } - } - - if ((png_ptr->do_filter & PNG_FILTER_PAETH) && - png_ptr->paeth_row == NULL) - { - if (png_ptr->prev_row == NULL) - { - png_warning(png_ptr, "Can't add Paeth filter after starting"); - png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH); - } - - else - { - png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, - (png_ptr->rowbytes + 1)); - png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; - } - } - - if (png_ptr->do_filter == PNG_NO_FILTERS) -#endif /* PNG_WRITE_FILTER_SUPPORTED */ - png_ptr->do_filter = PNG_FILTER_NONE; } + png_ptr->do_filter = (png_byte)filters; +#endif } else png_error(png_ptr, "Unknown custom filter method"); } -/* This allows us to influence the way in which libpng chooses the "best" - * filter for the current scanline. While the "minimum-sum-of-absolute- - * differences metric is relatively fast and effective, there is some - * question as to whether it can be improved upon by trying to keep the - * filtered data going to zlib more consistent, hopefully resulting in - * better compression. - */ -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */ -/* Convenience reset API. */ -static void -png_reset_filter_heuristics(png_structp png_ptr) -{ - /* Clear out any old values in the 'weights' - this must be done because if - * the app calls set_filter_heuristics multiple times with different - * 'num_weights' values we would otherwise potentially have wrong sized - * arrays. - */ - png_ptr->num_prev_filters = 0; - png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED; - if (png_ptr->prev_filters != NULL) - { - png_bytep old = png_ptr->prev_filters; - png_ptr->prev_filters = NULL; - png_free(png_ptr, old); - } - if (png_ptr->filter_weights != NULL) - { - png_uint_16p old = png_ptr->filter_weights; - png_ptr->filter_weights = NULL; - png_free(png_ptr, old); - } - - if (png_ptr->inv_filter_weights != NULL) - { - png_uint_16p old = png_ptr->inv_filter_weights; - png_ptr->inv_filter_weights = NULL; - png_free(png_ptr, old); - } - - /* Leave the filter_costs - this array is fixed size. */ -} - -static int -png_init_filter_heuristics(png_structp png_ptr, int heuristic_method, - int num_weights) -{ - if (png_ptr == NULL) - return 0; - - /* Clear out the arrays */ - png_reset_filter_heuristics(png_ptr); - - /* Check arguments; the 'reset' function makes the correct settings for the - * unweighted case, but we must handle the weight case by initializing the - * arrays for the caller. - */ - if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int i; - - if (num_weights > 0) - { - png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr, - (png_uint_32)(png_sizeof(png_byte) * num_weights)); - - /* To make sure that the weighting starts out fairly */ - for (i = 0; i < num_weights; i++) - { - png_ptr->prev_filters[i] = 255; - } - - png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr, - (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); - - png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr, - (png_uint_32)(png_sizeof(png_uint_16) * num_weights)); - - for (i = 0; i < num_weights; i++) - { - png_ptr->inv_filter_weights[i] = - png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; - } - - /* Safe to set this now */ - png_ptr->num_prev_filters = (png_byte)num_weights; - } - - /* If, in the future, there are other filter methods, this would - * need to be based on png_ptr->filter. - */ - if (png_ptr->filter_costs == NULL) - { - png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr, - (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); - - png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr, - (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST)); - } - - for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) - { - png_ptr->inv_filter_costs[i] = - png_ptr->filter_costs[i] = PNG_COST_FACTOR; - } - - /* All the arrays are inited, safe to set this: */ - png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED; - - /* Return the 'ok' code. */ - return 1; - } - else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT || - heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED) - { - return 1; - } - else - { - png_warning(png_ptr, "Unknown filter heuristic method"); - return 0; - } -} - +#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */ /* Provide floating and fixed point APIs */ #ifdef PNG_FLOATING_POINT_SUPPORTED void PNGAPI -png_set_filter_heuristics(png_structp png_ptr, int heuristic_method, +png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method, int num_weights, png_const_doublep filter_weights, png_const_doublep filter_costs) { - png_debug(1, "in png_set_filter_heuristics"); - - /* The internal API allocates all the arrays and ensures that the elements of - * those arrays are set to the default value. - */ - if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) - return; - - /* If using the weighted method copy in the weights. */ - if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int i; - for (i = 0; i < num_weights; i++) - { - if (filter_weights[i] <= 0.0) - { - png_ptr->inv_filter_weights[i] = - png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; - } - - else - { - png_ptr->inv_filter_weights[i] = - (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5); - - png_ptr->filter_weights[i] = - (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5); - } - } - - /* Here is where we set the relative costs of the different filters. We - * should take the desired compression level into account when setting - * the costs, so that Paeth, for instance, has a high relative cost at low - * compression levels, while it has a lower relative cost at higher - * compression settings. The filter types are in order of increasing - * relative cost, so it would be possible to do this with an algorithm. - */ - for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0) - { - png_ptr->inv_filter_costs[i] = - (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5); - - png_ptr->filter_costs[i] = - (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5); - } - } + PNG_UNUSED(png_ptr) + PNG_UNUSED(heuristic_method) + PNG_UNUSED(num_weights) + PNG_UNUSED(filter_weights) + PNG_UNUSED(filter_costs) } #endif /* FLOATING_POINT */ #ifdef PNG_FIXED_POINT_SUPPORTED void PNGAPI -png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method, +png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method, int num_weights, png_const_fixed_point_p filter_weights, png_const_fixed_point_p filter_costs) { - png_debug(1, "in png_set_filter_heuristics_fixed"); - - /* The internal API allocates all the arrays and ensures that the elements of - * those arrays are set to the default value. - */ - if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights)) - return; - - /* If using the weighted method copy in the weights. */ - if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int i; - for (i = 0; i < num_weights; i++) - { - if (filter_weights[i] <= 0) - { - png_ptr->inv_filter_weights[i] = - png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR; - } - - else - { - png_ptr->inv_filter_weights[i] = (png_uint_16) - ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1); - - png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR* - PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]); - } - } - - /* Here is where we set the relative costs of the different filters. We - * should take the desired compression level into account when setting - * the costs, so that Paeth, for instance, has a high relative cost at low - * compression levels, while it has a lower relative cost at higher - * compression settings. The filter types are in order of increasing - * relative cost, so it would be possible to do this with an algorithm. - */ - for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) - if (filter_costs[i] >= PNG_FP_1) - { - png_uint_32 tmp; - - /* Use a 32 bit unsigned temporary here because otherwise the - * intermediate value will be a 32 bit *signed* integer (ANSI rules) - * and this will get the wrong answer on division. - */ - tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2); - tmp /= filter_costs[i]; - - png_ptr->inv_filter_costs[i] = (png_uint_16)tmp; - - tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF; - tmp /= PNG_FP_1; - - png_ptr->filter_costs[i] = (png_uint_16)tmp; - } - } + PNG_UNUSED(png_ptr) + PNG_UNUSED(heuristic_method) + PNG_UNUSED(num_weights) + PNG_UNUSED(filter_weights) + PNG_UNUSED(filter_costs) } #endif /* FIXED_POINT */ -#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */ +#endif /* WRITE_WEIGHTED_FILTER */ +#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED void PNGAPI -png_set_compression_level(png_structp png_ptr, int level) +png_set_compression_level(png_structrp png_ptr, int level) { png_debug(1, "in png_set_compression_level"); if (png_ptr == NULL) return; - png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL; png_ptr->zlib_level = level; } void PNGAPI -png_set_compression_mem_level(png_structp png_ptr, int mem_level) +png_set_compression_mem_level(png_structrp png_ptr, int mem_level) { png_debug(1, "in png_set_compression_mem_level"); if (png_ptr == NULL) return; - png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL; png_ptr->zlib_mem_level = mem_level; } void PNGAPI -png_set_compression_strategy(png_structp png_ptr, int strategy) +png_set_compression_strategy(png_structrp png_ptr, int strategy) { png_debug(1, "in png_set_compression_strategy"); if (png_ptr == NULL) return; + /* The flag setting here prevents the libpng dynamic selection of strategy. + */ png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; png_ptr->zlib_strategy = strategy; } @@ -1432,80 +1183,82 @@ png_set_compression_strategy(png_structp png_ptr, int strategy) * smaller value of window_bits if it can do so safely. */ void PNGAPI -png_set_compression_window_bits(png_structp png_ptr, int window_bits) +png_set_compression_window_bits(png_structrp png_ptr, int window_bits) { if (png_ptr == NULL) return; + /* Prior to 1.6.0 this would warn but then set the window_bits value. This + * meant that negative window bits values could be selected that would cause + * libpng to write a non-standard PNG file with raw deflate or gzip + * compressed IDAT or ancillary chunks. Such files can be read and there is + * no warning on read, so this seems like a very bad idea. + */ if (window_bits > 15) + { png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); + window_bits = 15; + } else if (window_bits < 8) + { png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); + window_bits = 8; + } -#ifndef WBITS_8_OK - /* Avoid libpng bug with 256-byte windows */ - if (window_bits == 8) - { - png_warning(png_ptr, "Compression window is being reset to 512"); - window_bits = 9; - } - -#endif - png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS; png_ptr->zlib_window_bits = window_bits; } void PNGAPI -png_set_compression_method(png_structp png_ptr, int method) +png_set_compression_method(png_structrp png_ptr, int method) { png_debug(1, "in png_set_compression_method"); if (png_ptr == NULL) return; + /* This would produce an invalid PNG file if it worked, but it doesn't and + * deflate will fault it, so it is harmless to just warn here. + */ if (method != 8) png_warning(png_ptr, "Only compression method 8 is supported by PNG"); - png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD; png_ptr->zlib_method = method; } +#endif /* WRITE_CUSTOMIZE_COMPRESSION */ /* The following were added to libpng-1.5.4 */ #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED void PNGAPI -png_set_text_compression_level(png_structp png_ptr, int level) +png_set_text_compression_level(png_structrp png_ptr, int level) { png_debug(1, "in png_set_text_compression_level"); if (png_ptr == NULL) return; - png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL; png_ptr->zlib_text_level = level; } void PNGAPI -png_set_text_compression_mem_level(png_structp png_ptr, int mem_level) +png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level) { png_debug(1, "in png_set_text_compression_mem_level"); if (png_ptr == NULL) return; - png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL; png_ptr->zlib_text_mem_level = mem_level; } void PNGAPI -png_set_text_compression_strategy(png_structp png_ptr, int strategy) +png_set_text_compression_strategy(png_structrp png_ptr, int strategy) { png_debug(1, "in png_set_text_compression_strategy"); if (png_ptr == NULL) return; - png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY; png_ptr->zlib_text_strategy = strategy; } @@ -1513,32 +1266,28 @@ png_set_text_compression_strategy(png_structp png_ptr, int strategy) * smaller value of window_bits if it can do so safely. */ void PNGAPI -png_set_text_compression_window_bits(png_structp png_ptr, int window_bits) +png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits) { if (png_ptr == NULL) return; if (window_bits > 15) + { png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); + window_bits = 15; + } else if (window_bits < 8) + { png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); + window_bits = 8; + } -#ifndef WBITS_8_OK - /* Avoid libpng bug with 256-byte windows */ - if (window_bits == 8) - { - png_warning(png_ptr, "Text compression window is being reset to 512"); - window_bits = 9; - } - -#endif - png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS; png_ptr->zlib_text_window_bits = window_bits; } void PNGAPI -png_set_text_compression_method(png_structp png_ptr, int method) +png_set_text_compression_method(png_structrp png_ptr, int method) { png_debug(1, "in png_set_text_compression_method"); @@ -1548,14 +1297,13 @@ png_set_text_compression_method(png_structp png_ptr, int method) if (method != 8) png_warning(png_ptr, "Only compression method 8 is supported by PNG"); - png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD; png_ptr->zlib_text_method = method; } -#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ +#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ /* end of API added to libpng-1.5.4 */ void PNGAPI -png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) +png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn) { if (png_ptr == NULL) return; @@ -1565,7 +1313,7 @@ png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn) #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED void PNGAPI -png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr +png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr write_user_transform_fn) { png_debug(1, "in png_set_write_user_transform_fn"); @@ -1581,88 +1329,902 @@ png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr #ifdef PNG_INFO_IMAGE_SUPPORTED void PNGAPI -png_write_png(png_structp png_ptr, png_infop info_ptr, +png_write_png(png_structrp png_ptr, png_inforp info_ptr, int transforms, voidp params) { if (png_ptr == NULL || info_ptr == NULL) return; + if ((info_ptr->valid & PNG_INFO_IDAT) == 0) + { + png_app_error(png_ptr, "no rows for png_write_image to write"); + return; + } + /* Write the file header information. */ png_write_info(png_ptr, info_ptr); /* ------ these transformations don't touch the info structure ------- */ -#ifdef PNG_WRITE_INVERT_SUPPORTED /* Invert monochrome pixels */ - if (transforms & PNG_TRANSFORM_INVERT_MONO) + if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) +#ifdef PNG_WRITE_INVERT_SUPPORTED png_set_invert_mono(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); #endif -#ifdef PNG_WRITE_SHIFT_SUPPORTED /* Shift the pixels up to a legal bit depth and fill in * as appropriate to correctly scale the image. */ - if ((transforms & PNG_TRANSFORM_SHIFT) - && (info_ptr->valid & PNG_INFO_sBIT)) - png_set_shift(png_ptr, &info_ptr->sig_bit); + if ((transforms & PNG_TRANSFORM_SHIFT) != 0) +#ifdef PNG_WRITE_SHIFT_SUPPORTED + if ((info_ptr->valid & PNG_INFO_sBIT) != 0) + png_set_shift(png_ptr, &info_ptr->sig_bit); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); #endif -#ifdef PNG_WRITE_PACK_SUPPORTED /* Pack pixels into bytes */ - if (transforms & PNG_TRANSFORM_PACKING) - png_set_packing(png_ptr); + if ((transforms & PNG_TRANSFORM_PACKING) != 0) +#ifdef PNG_WRITE_PACK_SUPPORTED + png_set_packing(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); #endif -#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED /* Swap location of alpha bytes from ARGB to RGBA */ - if (transforms & PNG_TRANSFORM_SWAP_ALPHA) + if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) +#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED png_set_swap_alpha(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); #endif + /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into + * RGB, note that the code expects the input color type to be G or RGB; no + * alpha channel. + */ + if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER| + PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0) + { #ifdef PNG_WRITE_FILLER_SUPPORTED - /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */ - if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) - png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); + if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0) + { + if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) + png_app_error(png_ptr, + "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported"); - else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) - png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); + /* Continue if ignored - this is the pre-1.6.10 behavior */ + png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); + } + + else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) + png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported"); #endif + } -#ifdef PNG_WRITE_BGR_SUPPORTED /* Flip BGR pixels to RGB */ - if (transforms & PNG_TRANSFORM_BGR) + if ((transforms & PNG_TRANSFORM_BGR) != 0) +#ifdef PNG_WRITE_BGR_SUPPORTED png_set_bgr(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); #endif -#ifdef PNG_WRITE_SWAP_SUPPORTED /* Swap bytes of 16-bit files to most significant byte first */ - if (transforms & PNG_TRANSFORM_SWAP_ENDIAN) + if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) +#ifdef PNG_WRITE_SWAP_SUPPORTED png_set_swap(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); #endif + /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */ + if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) #ifdef PNG_WRITE_PACKSWAP_SUPPORTED - /* Swap bits of 1, 2, 4 bit packed pixel formats */ - if (transforms & PNG_TRANSFORM_PACKSWAP) png_set_packswap(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); #endif -#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED /* Invert the alpha channel from opacity to transparency */ - if (transforms & PNG_TRANSFORM_INVERT_ALPHA) + if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) +#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED png_set_invert_alpha(png_ptr); +#else + png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); #endif /* ----------------------- end of transformations ------------------- */ /* Write the bits */ - if (info_ptr->valid & PNG_INFO_IDAT) - png_write_image(png_ptr, info_ptr->row_pointers); + png_write_image(png_ptr, info_ptr->row_pointers); /* It is REQUIRED to call this to finish writing the rest of the file */ png_write_end(png_ptr, info_ptr); - PNG_UNUSED(transforms) /* Quiet compiler warnings */ PNG_UNUSED(params) } #endif -#endif /* PNG_WRITE_SUPPORTED */ + + +#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED +# ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */ +/* Initialize the write structure - general purpose utility. */ +static int +png_image_write_init(png_imagep image) +{ + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image, + png_safe_error, png_safe_warning); + + if (png_ptr != NULL) + { + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (info_ptr != NULL) + { + png_controlp control = png_voidcast(png_controlp, + png_malloc_warn(png_ptr, (sizeof *control))); + + if (control != NULL) + { + memset(control, 0, (sizeof *control)); + + control->png_ptr = png_ptr; + control->info_ptr = info_ptr; + control->for_write = 1; + + image->opaque = control; + return 1; + } + + /* Error clean up */ + png_destroy_info_struct(png_ptr, &info_ptr); + } + + png_destroy_write_struct(&png_ptr, NULL); + } + + return png_image_error(image, "png_image_write_: out of memory"); +} + +/* Arguments to png_image_write_main: */ +typedef struct +{ + /* Arguments: */ + png_imagep image; + png_const_voidp buffer; + png_int_32 row_stride; + png_const_voidp colormap; + int convert_to_8bit; + /* Local variables: */ + png_const_voidp first_row; + ptrdiff_t row_bytes; + png_voidp local_row; +} png_image_write_control; + +/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to + * do any necessary byte swapping. The component order is defined by the + * png_image format value. + */ +static int +png_write_image_16bit(png_voidp argument) +{ + png_image_write_control *display = png_voidcast(png_image_write_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + + png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, + display->first_row); + png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row); + png_uint_16p row_end; + const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; + int aindex = 0; + png_uint_32 y = image->height; + + if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) + { +# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED + if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) + { + aindex = -1; + ++input_row; /* To point to the first component */ + ++output_row; + } + else + aindex = channels; +# else + aindex = channels; +# endif + } + + else + png_error(png_ptr, "png_write_image: internal call error"); + + /* Work out the output row end and count over this, note that the increment + * above to 'row' means that row_end can actually be beyond the end of the + * row; this is correct. + */ + row_end = output_row + image->width * (channels+1); + + while (y-- > 0) + { + png_const_uint_16p in_ptr = input_row; + png_uint_16p out_ptr = output_row; + + while (out_ptr < row_end) + { + const png_uint_16 alpha = in_ptr[aindex]; + png_uint_32 reciprocal = 0; + int c; + + out_ptr[aindex] = alpha; + + /* Calculate a reciprocal. The correct calculation is simply + * component/alpha*65535 << 15. (I.e. 15 bits of precision); this + * allows correct rounding by adding .5 before the shift. 'reciprocal' + * is only initialized when required. + */ + if (alpha > 0 && alpha < 65535) + reciprocal = ((0xffff<<15)+(alpha>>1))/alpha; + + c = channels; + do /* always at least one channel */ + { + png_uint_16 component = *in_ptr++; + + /* The following gives 65535 for an alpha of 0, which is fine, + * otherwise if 0/0 is represented as some other value there is more + * likely to be a discontinuity which will probably damage + * compression when moving from a fully transparent area to a + * nearly transparent one. (The assumption here is that opaque + * areas tend not to be 0 intensity.) + */ + if (component >= alpha) + component = 65535; + + /* component 0 && alpha < 65535) + { + png_uint_32 calc = component * reciprocal; + calc += 16384; /* round to nearest */ + component = (png_uint_16)(calc >> 15); + } + + *out_ptr++ = component; + } + while (--c > 0); + + /* Skip to next component (skip the intervening alpha channel) */ + ++in_ptr; + ++out_ptr; + } + + png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row)); + input_row += display->row_bytes/(sizeof (png_uint_16)); + } + + return 1; +} + +/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel + * is present it must be removed from the components, the components are then + * written in sRGB encoding. No components are added or removed. + * + * Calculate an alpha reciprocal to reverse pre-multiplication. As above the + * calculation can be done to 15 bits of accuracy; however, the output needs to + * be scaled in the range 0..255*65535, so include that scaling here. + */ +# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha) + +static png_byte +png_unpremultiply(png_uint_32 component, png_uint_32 alpha, + png_uint_32 reciprocal/*from the above macro*/) +{ + /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0 + * is represented as some other value there is more likely to be a + * discontinuity which will probably damage compression when moving from a + * fully transparent area to a nearly transparent one. (The assumption here + * is that opaque areas tend not to be 0 intensity.) + * + * There is a rounding problem here; if alpha is less than 128 it will end up + * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the + * output change for this too. + */ + if (component >= alpha || alpha < 128) + return 255; + + /* component 0) + { + /* The test is that alpha/257 (rounded) is less than 255, the first value + * that becomes 255 is 65407. + * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore, + * be exact!) [Could also test reciprocal != 0] + */ + if (alpha < 65407) + { + component *= reciprocal; + component += 64; /* round to nearest */ + component >>= 7; + } + + else + component *= 255; + + /* Convert the component to sRGB. */ + return (png_byte)PNG_sRGB_FROM_LINEAR(component); + } + + else + return 0; +} + +static int +png_write_image_8bit(png_voidp argument) +{ + png_image_write_control *display = png_voidcast(png_image_write_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + + png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, + display->first_row); + png_bytep output_row = png_voidcast(png_bytep, display->local_row); + png_uint_32 y = image->height; + const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; + + if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) + { + png_bytep row_end; + int aindex; + +# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED + if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) + { + aindex = -1; + ++input_row; /* To point to the first component */ + ++output_row; + } + + else +# endif + aindex = channels; + + /* Use row_end in place of a loop counter: */ + row_end = output_row + image->width * (channels+1); + + while (y-- > 0) + { + png_const_uint_16p in_ptr = input_row; + png_bytep out_ptr = output_row; + + while (out_ptr < row_end) + { + png_uint_16 alpha = in_ptr[aindex]; + png_byte alphabyte = (png_byte)PNG_DIV257(alpha); + png_uint_32 reciprocal = 0; + int c; + + /* Scale and write the alpha channel. */ + out_ptr[aindex] = alphabyte; + + if (alphabyte > 0 && alphabyte < 255) + reciprocal = UNP_RECIPROCAL(alpha); + + c = channels; + do /* always at least one channel */ + *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal); + while (--c > 0); + + /* Skip to next component (skip the intervening alpha channel) */ + ++in_ptr; + ++out_ptr; + } /* while out_ptr < row_end */ + + png_write_row(png_ptr, png_voidcast(png_const_bytep, + display->local_row)); + input_row += display->row_bytes/(sizeof (png_uint_16)); + } /* while y */ + } + + else + { + /* No alpha channel, so the row_end really is the end of the row and it + * is sufficient to loop over the components one by one. + */ + png_bytep row_end = output_row + image->width * channels; + + while (y-- > 0) + { + png_const_uint_16p in_ptr = input_row; + png_bytep out_ptr = output_row; + + while (out_ptr < row_end) + { + png_uint_32 component = *in_ptr++; + + component *= 255; + *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component); + } + + png_write_row(png_ptr, output_row); + input_row += display->row_bytes/(sizeof (png_uint_16)); + } + } + + return 1; +} + +static void +png_image_set_PLTE(png_image_write_control *display) +{ + const png_imagep image = display->image; + const void *cmap = display->colormap; + const int entries = image->colormap_entries > 256 ? 256 : + (int)image->colormap_entries; + + /* NOTE: the caller must check for cmap != NULL and entries != 0 */ + const png_uint_32 format = image->format; + const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format); + +# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\ + defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED) + const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 && + (format & PNG_FORMAT_FLAG_ALPHA) != 0; +# else +# define afirst 0 +# endif + +# ifdef PNG_FORMAT_BGR_SUPPORTED + const int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; +# else +# define bgr 0 +# endif + + int i, num_trans; + png_color palette[256]; + png_byte tRNS[256]; + + memset(tRNS, 255, (sizeof tRNS)); + memset(palette, 0, (sizeof palette)); + + for (i=num_trans=0; i= 3) /* RGB */ + { + palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 * + entry[(2 ^ bgr)]); + palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 * + entry[1]); + palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 * + entry[bgr]); + } + + else /* Gray */ + palette[i].blue = palette[i].red = palette[i].green = + (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry); + } + + else /* alpha */ + { + png_uint_16 alpha = entry[afirst ? 0 : channels-1]; + png_byte alphabyte = (png_byte)PNG_DIV257(alpha); + png_uint_32 reciprocal = 0; + + /* Calculate a reciprocal, as in the png_write_image_8bit code above + * this is designed to produce a value scaled to 255*65535 when + * divided by 128 (i.e. asr 7). + */ + if (alphabyte > 0 && alphabyte < 255) + reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha; + + tRNS[i] = alphabyte; + if (alphabyte < 255) + num_trans = i+1; + + if (channels >= 3) /* RGB */ + { + palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)], + alpha, reciprocal); + palette[i].green = png_unpremultiply(entry[afirst + 1], alpha, + reciprocal); + palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha, + reciprocal); + } + + else /* gray */ + palette[i].blue = palette[i].red = palette[i].green = + png_unpremultiply(entry[afirst], alpha, reciprocal); + } + } + + else /* Color-map has sRGB values */ + { + png_const_bytep entry = png_voidcast(png_const_bytep, cmap); + + entry += i * channels; + + switch (channels) + { + case 4: + tRNS[i] = entry[afirst ? 0 : 3]; + if (tRNS[i] < 255) + num_trans = i+1; + /* FALL THROUGH */ + case 3: + palette[i].blue = entry[afirst + (2 ^ bgr)]; + palette[i].green = entry[afirst + 1]; + palette[i].red = entry[afirst + bgr]; + break; + + case 2: + tRNS[i] = entry[1 ^ afirst]; + if (tRNS[i] < 255) + num_trans = i+1; + /* FALL THROUGH */ + case 1: + palette[i].blue = palette[i].red = palette[i].green = + entry[afirst]; + break; + + default: + break; + } + } + } + +# ifdef afirst +# undef afirst +# endif +# ifdef bgr +# undef bgr +# endif + + png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette, + entries); + + if (num_trans > 0) + png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS, + num_trans, NULL); + + image->colormap_entries = entries; +} + +static int +png_image_write_main(png_voidp argument) +{ + png_image_write_control *display = png_voidcast(png_image_write_control*, + argument); + png_imagep image = display->image; + png_structrp png_ptr = image->opaque->png_ptr; + png_inforp info_ptr = image->opaque->info_ptr; + png_uint_32 format = image->format; + + /* The following four ints are actually booleans */ + int colormap = (format & PNG_FORMAT_FLAG_COLORMAP); + int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */ + int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA); + int write_16bit = linear && !colormap && (display->convert_to_8bit == 0); + +# ifdef PNG_BENIGN_ERRORS_SUPPORTED + /* Make sure we error out on any bad situation */ + png_set_benign_errors(png_ptr, 0/*error*/); +# endif + + /* Default the 'row_stride' parameter if required. */ + if (display->row_stride == 0) + display->row_stride = PNG_IMAGE_ROW_STRIDE(*image); + + /* Set the required transforms then write the rows in the correct order. */ + if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0) + { + if (display->colormap != NULL && image->colormap_entries > 0) + { + png_uint_32 entries = image->colormap_entries; + + png_set_IHDR(png_ptr, info_ptr, image->width, image->height, + entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)), + PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_image_set_PLTE(display); + } + + else + png_error(image->opaque->png_ptr, + "no color-map for color-mapped image"); + } + + else + png_set_IHDR(png_ptr, info_ptr, image->width, image->height, + write_16bit ? 16 : 8, + ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) + + ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0), + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + /* Counter-intuitively the data transformations must be called *after* + * png_write_info, not before as in the read code, but the 'set' functions + * must still be called before. Just set the color space information, never + * write an interlaced image. + */ + + if (write_16bit != 0) + { + /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */ + png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR); + + if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) + png_set_cHRM_fixed(png_ptr, info_ptr, + /* color x y */ + /* white */ 31270, 32900, + /* red */ 64000, 33000, + /* green */ 30000, 60000, + /* blue */ 15000, 6000 + ); + } + + else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) + png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); + + /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit + * space must still be gamma encoded. + */ + else + png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE); + + /* Write the file header. */ + png_write_info(png_ptr, info_ptr); + + /* Now set up the data transformations (*after* the header is written), + * remove the handled transformations from the 'format' flags for checking. + * + * First check for a little endian system if writing 16-bit files. + */ + if (write_16bit != 0) + { + PNG_CONST png_uint_16 le = 0x0001; + + if ((*(png_const_bytep) & le) != 0) + png_set_swap(png_ptr); + } + +# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED + if ((format & PNG_FORMAT_FLAG_BGR) != 0) + { + if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0) + png_set_bgr(png_ptr); + format &= ~PNG_FORMAT_FLAG_BGR; + } +# endif + +# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED + if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) + { + if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0) + png_set_swap_alpha(png_ptr); + format &= ~PNG_FORMAT_FLAG_AFIRST; + } +# endif + + /* If there are 16 or fewer color-map entries we wrote a lower bit depth + * above, but the application data is still byte packed. + */ + if (colormap != 0 && image->colormap_entries <= 16) + png_set_packing(png_ptr); + + /* That should have handled all (both) the transforms. */ + if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR | + PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0) + png_error(png_ptr, "png_write_image: unsupported transformation"); + + { + png_const_bytep row = png_voidcast(png_const_bytep, display->buffer); + ptrdiff_t row_bytes = display->row_stride; + + if (linear != 0) + row_bytes *= (sizeof (png_uint_16)); + + if (row_bytes < 0) + row += (image->height-1) * (-row_bytes); + + display->first_row = row; + display->row_bytes = row_bytes; + } + + /* Apply 'fast' options if the flag is set. */ + if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0) + { + png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS); + /* NOTE: determined by experiment using pngstest, this reflects some + * balance between the time to write the image once and the time to read + * it about 50 times. The speed-up in pngstest was about 10-20% of the + * total (user) time on a heavily loaded system. + */ +# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED + png_set_compression_level(png_ptr, 3); +# endif + } + + /* Check for the cases that currently require a pre-transform on the row + * before it is written. This only applies when the input is 16-bit and + * either there is an alpha channel or it is converted to 8-bit. + */ + if ((linear != 0 && alpha != 0 ) || + (colormap == 0 && display->convert_to_8bit != 0)) + { + png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, + png_get_rowbytes(png_ptr, info_ptr))); + int result; + + display->local_row = row; + if (write_16bit != 0) + result = png_safe_execute(image, png_write_image_16bit, display); + else + result = png_safe_execute(image, png_write_image_8bit, display); + display->local_row = NULL; + + png_free(png_ptr, row); + + /* Skip the 'write_end' on error: */ + if (result == 0) + return 0; + } + + /* Otherwise this is the case where the input is in a format currently + * supported by the rest of the libpng write code; call it directly. + */ + else + { + png_const_bytep row = png_voidcast(png_const_bytep, display->first_row); + ptrdiff_t row_bytes = display->row_bytes; + png_uint_32 y = image->height; + + while (y-- > 0) + { + png_write_row(png_ptr, row); + row += row_bytes; + } + } + + png_write_end(png_ptr, info_ptr); + return 1; +} + +int PNGAPI +png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit, + const void *buffer, png_int_32 row_stride, const void *colormap) +{ + /* Write the image to the given (FILE*). */ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + if (file != NULL) + { + if (png_image_write_init(image) != 0) + { + png_image_write_control display; + int result; + + /* This is slightly evil, but png_init_io doesn't do anything other + * than this and we haven't changed the standard IO functions so + * this saves a 'safe' function. + */ + image->opaque->png_ptr->io_ptr = file; + + memset(&display, 0, (sizeof display)); + display.image = image; + display.buffer = buffer; + display.row_stride = row_stride; + display.colormap = colormap; + display.convert_to_8bit = convert_to_8bit; + + result = png_safe_execute(image, png_image_write_main, &display); + png_image_free(image); + return result; + } + + else + return 0; + } + + else + return png_image_error(image, + "png_image_write_to_stdio: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION"); + + else + return 0; +} + +int PNGAPI +png_image_write_to_file(png_imagep image, const char *file_name, + int convert_to_8bit, const void *buffer, png_int_32 row_stride, + const void *colormap) +{ + /* Write the image to the named file. */ + if (image != NULL && image->version == PNG_IMAGE_VERSION) + { + if (file_name != NULL) + { + FILE *fp = fopen(file_name, "wb"); + + if (fp != NULL) + { + if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, + row_stride, colormap) != 0) + { + int error; /* from fflush/fclose */ + + /* Make sure the file is flushed correctly. */ + if (fflush(fp) == 0 && ferror(fp) == 0) + { + if (fclose(fp) == 0) + return 1; + + error = errno; /* from fclose */ + } + + else + { + error = errno; /* from fflush or ferror */ + (void)fclose(fp); + } + + (void)remove(file_name); + /* The image has already been cleaned up; this is just used to + * set the error (because the original write succeeded). + */ + return png_image_error(image, strerror(error)); + } + + else + { + /* Clean up: just the opened file. */ + (void)fclose(fp); + (void)remove(file_name); + return 0; + } + } + + else + return png_image_error(image, strerror(errno)); + } + + else + return png_image_error(image, + "png_image_write_to_file: invalid argument"); + } + + else if (image != NULL) + return png_image_error(image, + "png_image_write_to_file: incorrect PNG_IMAGE_VERSION"); + + else + return 0; +} +# endif /* STDIO */ +#endif /* SIMPLIFIED_WRITE */ +#endif /* WRITE */ diff --git a/3rdparty/libpng/pngwtran.c b/3rdparty/libpng/pngwtran.c index 96608efcb..5dc949157 100644 --- a/3rdparty/libpng/pngwtran.c +++ b/3rdparty/libpng/pngwtran.c @@ -1,8 +1,8 @@ /* pngwtran.c - transforms the data in a row for PNG writers * - * Last changed in libpng 1.5.6 [November 3, 2011] - * Copyright (c) 1998-2011 Glenn Randers-Pehrson + * Last changed in libpng 1.6.18 [July 23, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -14,90 +14,14 @@ #include "pngpriv.h" #ifdef PNG_WRITE_SUPPORTED - #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED -/* Transform the data according to the user's wishes. The order of - * transformations is significant. - */ -void /* PRIVATE */ -png_do_write_transformations(png_structp png_ptr, png_row_infop row_info) -{ - png_debug(1, "in png_do_write_transformations"); - - if (png_ptr == NULL) - return; - -#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED - if (png_ptr->transformations & PNG_USER_TRANSFORM) - if (png_ptr->write_user_transform_fn != NULL) - (*(png_ptr->write_user_transform_fn)) /* User write transform - function */ - (png_ptr, /* png_ptr */ - row_info, /* row_info: */ - /* png_uint_32 width; width of row */ - /* png_size_t rowbytes; number of bytes in row */ - /* png_byte color_type; color type of pixels */ - /* png_byte bit_depth; bit depth of samples */ - /* png_byte channels; number of channels (1-4) */ - /* png_byte pixel_depth; bits per pixel (depth*channels) */ - png_ptr->row_buf + 1); /* start of pixel data for row */ -#endif - -#ifdef PNG_WRITE_FILLER_SUPPORTED - if (png_ptr->transformations & PNG_FILLER) - png_do_strip_channel(row_info, png_ptr->row_buf + 1, - !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); -#endif - -#ifdef PNG_WRITE_PACKSWAP_SUPPORTED - if (png_ptr->transformations & PNG_PACKSWAP) - png_do_packswap(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_WRITE_PACK_SUPPORTED - if (png_ptr->transformations & PNG_PACK) - png_do_pack(row_info, png_ptr->row_buf + 1, - (png_uint_32)png_ptr->bit_depth); -#endif - -#ifdef PNG_WRITE_SWAP_SUPPORTED - if (png_ptr->transformations & PNG_SWAP_BYTES) - png_do_swap(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_WRITE_SHIFT_SUPPORTED - if (png_ptr->transformations & PNG_SHIFT) - png_do_shift(row_info, png_ptr->row_buf + 1, - &(png_ptr->shift)); -#endif - -#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_SWAP_ALPHA) - png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED - if (png_ptr->transformations & PNG_INVERT_ALPHA) - png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_WRITE_BGR_SUPPORTED - if (png_ptr->transformations & PNG_BGR) - png_do_bgr(row_info, png_ptr->row_buf + 1); -#endif - -#ifdef PNG_WRITE_INVERT_SUPPORTED - if (png_ptr->transformations & PNG_INVERT_MONO) - png_do_invert(row_info, png_ptr->row_buf + 1); -#endif -} #ifdef PNG_WRITE_PACK_SUPPORTED /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The * row_info bit depth should be 8 (one pixel per byte). The channels * should be 1 (this only happens on grayscale and paletted images). */ -void /* PRIVATE */ +static void png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) { png_debug(1, "in png_do_pack"); @@ -147,7 +71,8 @@ png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) case 2: { png_bytep sp, dp; - int shift, v; + unsigned int shift; + int v; png_uint_32 i; png_uint_32 row_width = row_info->width; @@ -186,7 +111,8 @@ png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) case 4: { png_bytep sp, dp; - int shift, v; + unsigned int shift; + int v; png_uint_32 i; png_uint_32 row_width = row_info->width; @@ -242,7 +168,7 @@ png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) * would pass 3 as bit_depth, and this routine would translate the * data to 0 to 15. */ -void /* PRIVATE */ +static void png_do_shift(png_row_infop row_info, png_bytep row, png_const_color_8p bit_depth) { @@ -253,7 +179,7 @@ png_do_shift(png_row_infop row_info, png_bytep row, int shift_start[4], shift_dec[4]; int channels = 0; - if (row_info->color_type & PNG_COLOR_MASK_COLOR) + if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) { shift_start[channels] = row_info->bit_depth - bit_depth->red; shift_dec[channels] = bit_depth->red; @@ -275,7 +201,7 @@ png_do_shift(png_row_infop row_info, png_bytep row, channels++; } - if (row_info->color_type & PNG_COLOR_MASK_ALPHA) + if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) { shift_start[channels] = row_info->bit_depth - bit_depth->alpha; shift_dec[channels] = bit_depth->alpha; @@ -287,7 +213,7 @@ png_do_shift(png_row_infop row_info, png_bytep row, { png_bytep bp = row; png_size_t i; - png_byte mask; + unsigned int mask; png_size_t row_bytes = row_info->rowbytes; if (bit_depth->gray == 1 && row_info->bit_depth == 2) @@ -301,20 +227,22 @@ png_do_shift(png_row_infop row_info, png_bytep row, for (i = 0; i < row_bytes; i++, bp++) { - png_uint_16 v; int j; + unsigned int v, out; v = *bp; - *bp = 0; + out = 0; for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) { if (j > 0) - *bp |= (png_byte)((v << j) & 0xff); + out |= v << j; else - *bp |= (png_byte)((v >> (-j)) & mask); + out |= (v >> (-j)) & mask; } + + *bp = (png_byte)(out & 0xff); } } @@ -327,21 +255,23 @@ png_do_shift(png_row_infop row_info, png_bytep row, for (i = 0; i < istop; i++, bp++) { - png_uint_16 v; + const unsigned int c = i%channels; int j; - int c = (int)(i%channels); + unsigned int v, out; v = *bp; - *bp = 0; + out = 0; for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) { if (j > 0) - *bp |= (png_byte)((v << j) & 0xff); + out |= v << j; else - *bp |= (png_byte)((v >> (-j)) & 0xff); + out |= v >> (-j); } + + *bp = (png_byte)(out & 0xff); } } @@ -353,22 +283,22 @@ png_do_shift(png_row_infop row_info, png_bytep row, for (bp = row, i = 0; i < istop; i++) { - int c = (int)(i%channels); - png_uint_16 value, v; + const unsigned int c = i%channels; int j; + unsigned int value, v; - v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1)); + v = png_get_uint_16(bp); value = 0; for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) { if (j > 0) - value |= (png_uint_16)((v << j) & (png_uint_16)0xffff); + value |= v << j; else - value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); + value |= v >> (-j); } - *bp++ = (png_byte)(value >> 8); + *bp++ = (png_byte)((value >> 8) & 0xff); *bp++ = (png_byte)(value & 0xff); } } @@ -377,7 +307,7 @@ png_do_shift(png_row_infop row_info, png_bytep row, #endif #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED -void /* PRIVATE */ +static void png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_write_swap_alpha"); @@ -425,7 +355,7 @@ png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) *(dp++) = save[1]; } } -#endif /* PNG_WRITE_16BIT_SUPPORTED */ +#endif /* WRITE_16BIT */ } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) @@ -464,14 +394,14 @@ png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) *(dp++) = save[1]; } } -#endif /* PNG_WRITE_16BIT_SUPPORTED */ +#endif /* WRITE_16BIT */ } } } #endif #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED -void /* PRIVATE */ +static void png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_write_invert_alpha"); @@ -494,7 +424,7 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) *(dp++) = *(sp++); */ sp+=3; dp = sp; - *(dp++) = (png_byte)(255 - *(sp++)); + *dp = (png_byte)(255 - *(sp++)); } } @@ -518,10 +448,10 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) */ sp+=6; dp = sp; *(dp++) = (png_byte)(255 - *(sp++)); - *(dp++) = (png_byte)(255 - *(sp++)); + *dp = (png_byte)(255 - *(sp++)); } } -#endif /* PNG_WRITE_16BIT_SUPPORTED */ +#endif /* WRITE_16BIT */ } else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) @@ -556,78 +486,91 @@ png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) */ sp+=2; dp = sp; *(dp++) = (png_byte)(255 - *(sp++)); - *(dp++) = (png_byte)(255 - *(sp++)); + *dp = (png_byte)(255 - *(sp++)); } } -#endif /* PNG_WRITE_16BIT_SUPPORTED */ +#endif /* WRITE_16BIT */ } } } #endif -#endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */ -#ifdef PNG_MNG_FEATURES_SUPPORTED -/* Undoes intrapixel differencing */ +/* Transform the data according to the user's wishes. The order of + * transformations is significant. + */ void /* PRIVATE */ -png_do_write_intrapixel(png_row_infop row_info, png_bytep row) +png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info) { - png_debug(1, "in png_do_write_intrapixel"); + png_debug(1, "in png_do_write_transformations"); - if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) - { - int bytes_per_pixel; - png_uint_32 row_width = row_info->width; - if (row_info->bit_depth == 8) - { - png_bytep rp; - png_uint_32 i; + if (png_ptr == NULL) + return; - if (row_info->color_type == PNG_COLOR_TYPE_RGB) - bytes_per_pixel = 3; +#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED + if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) + if (png_ptr->write_user_transform_fn != NULL) + (*(png_ptr->write_user_transform_fn)) /* User write transform + function */ + (png_ptr, /* png_ptr */ + row_info, /* row_info: */ + /* png_uint_32 width; width of row */ + /* png_size_t rowbytes; number of bytes in row */ + /* png_byte color_type; color type of pixels */ + /* png_byte bit_depth; bit depth of samples */ + /* png_byte channels; number of channels (1-4) */ + /* png_byte pixel_depth; bits per pixel (depth*channels) */ + png_ptr->row_buf + 1); /* start of pixel data for row */ +#endif - else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) - bytes_per_pixel = 4; +#ifdef PNG_WRITE_FILLER_SUPPORTED + if ((png_ptr->transformations & PNG_FILLER) != 0) + png_do_strip_channel(row_info, png_ptr->row_buf + 1, + !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); +#endif - else - return; +#ifdef PNG_WRITE_PACKSWAP_SUPPORTED + if ((png_ptr->transformations & PNG_PACKSWAP) != 0) + png_do_packswap(row_info, png_ptr->row_buf + 1); +#endif - for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) - { - *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff); - *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff); - } - } +#ifdef PNG_WRITE_PACK_SUPPORTED + if ((png_ptr->transformations & PNG_PACK) != 0) + png_do_pack(row_info, png_ptr->row_buf + 1, + (png_uint_32)png_ptr->bit_depth); +#endif -#ifdef PNG_WRITE_16BIT_SUPPORTED - else if (row_info->bit_depth == 16) - { - png_bytep rp; - png_uint_32 i; +#ifdef PNG_WRITE_SWAP_SUPPORTED +# ifdef PNG_16BIT_SUPPORTED + if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) + png_do_swap(row_info, png_ptr->row_buf + 1); +# endif +#endif - if (row_info->color_type == PNG_COLOR_TYPE_RGB) - bytes_per_pixel = 6; +#ifdef PNG_WRITE_SHIFT_SUPPORTED + if ((png_ptr->transformations & PNG_SHIFT) != 0) + png_do_shift(row_info, png_ptr->row_buf + 1, + &(png_ptr->shift)); +#endif - else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) - bytes_per_pixel = 8; +#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) + png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); +#endif - else - return; +#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED + if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) + png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); +#endif - for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) - { - png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); - png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); - png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); - png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); - png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); - *(rp ) = (png_byte)((red >> 8) & 0xff); - *(rp + 1) = (png_byte)(red & 0xff); - *(rp + 4) = (png_byte)((blue >> 8) & 0xff); - *(rp + 5) = (png_byte)(blue & 0xff); - } - } -#endif /* PNG_WRITE_16BIT_SUPPORTED */ - } +#ifdef PNG_WRITE_BGR_SUPPORTED + if ((png_ptr->transformations & PNG_BGR) != 0) + png_do_bgr(row_info, png_ptr->row_buf + 1); +#endif + +#ifdef PNG_WRITE_INVERT_SUPPORTED + if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) + png_do_invert(row_info, png_ptr->row_buf + 1); +#endif } -#endif /* PNG_MNG_FEATURES_SUPPORTED */ -#endif /* PNG_WRITE_SUPPORTED */ +#endif /* WRITE_TRANSFORMS */ +#endif /* WRITE */ diff --git a/3rdparty/libpng/pngwutil.c b/3rdparty/libpng/pngwutil.c index b49704f1a..adc4729c2 100644 --- a/3rdparty/libpng/pngwutil.c +++ b/3rdparty/libpng/pngwutil.c @@ -1,8 +1,8 @@ /* pngwutil.c - utilities to write a PNG file * - * Last changed in libpng 1.5.10 [March 8, 2012] - * Copyright (c) 1998-2012 Glenn Randers-Pehrson + * Last changed in libpng 1.6.19 [November 12, 2015] + * Copyright (c) 1998-2015 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -23,29 +23,12 @@ void PNGAPI png_save_uint_32(png_bytep buf, png_uint_32 i) { - buf[0] = (png_byte)((i >> 24) & 0xff); - buf[1] = (png_byte)((i >> 16) & 0xff); - buf[2] = (png_byte)((i >> 8) & 0xff); - buf[3] = (png_byte)(i & 0xff); + buf[0] = (png_byte)(i >> 24); + buf[1] = (png_byte)(i >> 16); + buf[2] = (png_byte)(i >> 8); + buf[3] = (png_byte)(i ); } -#ifdef PNG_SAVE_INT_32_SUPPORTED -/* The png_save_int_32 function assumes integers are stored in two's - * complement format. If this isn't the case, then this routine needs to - * be modified to write data in two's complement format. Note that, - * the following works correctly even if png_int_32 has more than 32 bits - * (compare the more complex code required on read for sign extention.) - */ -void PNGAPI -png_save_int_32(png_bytep buf, png_int_32 i) -{ - buf[0] = (png_byte)((i >> 24) & 0xff); - buf[1] = (png_byte)((i >> 16) & 0xff); - buf[2] = (png_byte)((i >> 8) & 0xff); - buf[3] = (png_byte)(i & 0xff); -} -#endif - /* Place a 16-bit number into a buffer in PNG byte order. * The parameter is declared unsigned int, not png_uint_16, * just to avoid potential problems on pre-ANSI C compilers. @@ -53,8 +36,8 @@ png_save_int_32(png_bytep buf, png_int_32 i) void PNGAPI png_save_uint_16(png_bytep buf, unsigned int i) { - buf[0] = (png_byte)((i >> 8) & 0xff); - buf[1] = (png_byte)(i & 0xff); + buf[0] = (png_byte)(i >> 8); + buf[1] = (png_byte)(i ); } #endif @@ -65,7 +48,7 @@ png_save_uint_16(png_bytep buf, unsigned int i) * bytes have already been written. */ void PNGAPI -png_write_sig(png_structp png_ptr) +png_write_sig(png_structrp png_ptr) { png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; @@ -87,7 +70,7 @@ png_write_sig(png_structp png_ptr) * passing in png_write_chunk_data(). */ static void -png_write_chunk_header(png_structp png_ptr, png_uint_32 chunk_name, +png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length) { png_byte buf[8]; @@ -129,7 +112,7 @@ png_write_chunk_header(png_structp png_ptr, png_uint_32 chunk_name, } void PNGAPI -png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_string, +png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string, png_uint_32 length) { png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length); @@ -141,7 +124,7 @@ png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_string, * given to png_write_chunk_header(). */ void PNGAPI -png_write_chunk_data(png_structp png_ptr, png_const_bytep data, +png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length) { /* Write the data, and run the CRC over it */ @@ -153,7 +136,7 @@ png_write_chunk_data(png_structp png_ptr, png_const_bytep data, png_write_data(png_ptr, data, length); /* Update the CRC after writing the data, - * in case that the user I/O routine alters it. + * in case the user I/O routine alters it. */ png_calculate_crc(png_ptr, data, length); } @@ -161,7 +144,7 @@ png_write_chunk_data(png_structp png_ptr, png_const_bytep data, /* Finish a chunk started with png_write_chunk_header(). */ void PNGAPI -png_write_chunk_end(png_structp png_ptr) +png_write_chunk_end(png_structrp png_ptr) { png_byte buf[4]; @@ -190,15 +173,15 @@ png_write_chunk_end(png_structp png_ptr) * functions instead. */ static void -png_write_complete_chunk(png_structp png_ptr, png_uint_32 chunk_name, +png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length) { if (png_ptr == NULL) return; - /* On 64 bit architectures 'length' may not fit in a png_uint_32. */ - if (length > PNG_UINT_32_MAX) - png_error(png_ptr, "length exceeds PNG maxima"); + /* On 64-bit architectures 'length' may not fit in a png_uint_32. */ + if (length > PNG_UINT_31_MAX) + png_error(png_ptr, "length exceeds PNG maximum"); png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length); png_write_chunk_data(png_ptr, data, length); @@ -207,474 +190,571 @@ png_write_complete_chunk(png_structp png_ptr, png_uint_32 chunk_name, /* This is the API that calls the internal function above. */ void PNGAPI -png_write_chunk(png_structp png_ptr, png_const_bytep chunk_string, +png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string, png_const_bytep data, png_size_t length) { png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data, length); } -/* Initialize the compressor for the appropriate type of compression. */ -static void -png_zlib_claim(png_structp png_ptr, png_uint_32 state) +/* This is used below to find the size of an image to pass to png_deflate_claim, + * so it only needs to be accurate if the size is less than 16384 bytes (the + * point at which a lower LZ window size can be used.) + */ +static png_alloc_size_t +png_image_size(png_structrp png_ptr) { - if (!(png_ptr->zlib_state & PNG_ZLIB_IN_USE)) + /* Only return sizes up to the maximum of a png_uint_32; do this by limiting + * the width and height used to 15 bits. + */ + png_uint_32 h = png_ptr->height; + + if (png_ptr->rowbytes < 32768 && h < 32768) { - /* If already initialized for 'state' do not re-init. */ - if (png_ptr->zlib_state != state) + if (png_ptr->interlaced != 0) { - int ret = Z_OK; - png_const_charp who = "-"; + /* Interlacing makes the image larger because of the replication of + * both the filter byte and the padding to a byte boundary. + */ + png_uint_32 w = png_ptr->width; + unsigned int pd = png_ptr->pixel_depth; + png_alloc_size_t cb_base; + int pass; - /* If actually initialized for another state do a deflateEnd. */ - if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED) + for (cb_base=0, pass=0; pass<=6; ++pass) { - ret = deflateEnd(&png_ptr->zstream); - who = "end"; - png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED; + png_uint_32 pw = PNG_PASS_COLS(w, pass); + + if (pw > 0) + cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass); } - /* zlib itself detects an incomplete state on deflateEnd */ - if (ret == Z_OK) switch (state) - { -# ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED - case PNG_ZLIB_FOR_TEXT: - ret = deflateInit2(&png_ptr->zstream, - png_ptr->zlib_text_level, png_ptr->zlib_text_method, - png_ptr->zlib_text_window_bits, - png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy); - who = "text"; - break; -# endif - - case PNG_ZLIB_FOR_IDAT: - ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, - png_ptr->zlib_method, png_ptr->zlib_window_bits, - png_ptr->zlib_mem_level, png_ptr->zlib_strategy); - who = "IDAT"; - break; - - default: - png_error(png_ptr, "invalid zlib state"); - } - - if (ret == Z_OK) - png_ptr->zlib_state = state; - - else /* an error in deflateEnd or deflateInit2 */ - { - size_t pos = 0; - char msg[64]; - - pos = png_safecat(msg, sizeof msg, pos, - "zlib failed to initialize compressor ("); - pos = png_safecat(msg, sizeof msg, pos, who); - - switch (ret) - { - case Z_VERSION_ERROR: - pos = png_safecat(msg, sizeof msg, pos, ") version error"); - break; - - case Z_STREAM_ERROR: - pos = png_safecat(msg, sizeof msg, pos, ") stream error"); - break; - - case Z_MEM_ERROR: - pos = png_safecat(msg, sizeof msg, pos, ") memory error"); - break; - - default: - pos = png_safecat(msg, sizeof msg, pos, ") unknown error"); - break; - } - - png_error(png_ptr, msg); - } + return cb_base; } - /* Here on success, claim the zstream: */ - png_ptr->zlib_state |= PNG_ZLIB_IN_USE; + else + return (png_ptr->rowbytes+1) * h; } else - png_error(png_ptr, "zstream already in use (internal error)"); + return 0xffffffffU; } -/* The opposite: release the stream. It is also reset, this API will warn on - * error but will not fail. - */ +#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED + /* This is the code to hack the first two bytes of the deflate stream (the + * deflate header) to correct the windowBits value to match the actual data + * size. Note that the second argument is the *uncompressed* size but the + * first argument is the *compressed* data (and it must be deflate + * compressed.) + */ static void -png_zlib_release(png_structp png_ptr) +optimize_cmf(png_bytep data, png_alloc_size_t data_size) { - if (png_ptr->zlib_state & PNG_ZLIB_IN_USE) + /* Optimize the CMF field in the zlib stream. The resultant zlib stream is + * still compliant to the stream specification. + */ + if (data_size <= 16384) /* else windowBits must be 15 */ { - int ret = deflateReset(&png_ptr->zstream); + unsigned int z_cmf = data[0]; /* zlib compression method and flags */ - png_ptr->zlib_state &= ~PNG_ZLIB_IN_USE; - - if (ret != Z_OK) + if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) { - png_const_charp err; - PNG_WARNING_PARAMETERS(p) + unsigned int z_cinfo; + unsigned int half_z_window_size; - switch (ret) + z_cinfo = z_cmf >> 4; + half_z_window_size = 1U << (z_cinfo + 7); + + if (data_size <= half_z_window_size) /* else no change */ { - case Z_VERSION_ERROR: - err = "version"; - break; + unsigned int tmp; - case Z_STREAM_ERROR: - err = "stream"; - break; + do + { + half_z_window_size >>= 1; + --z_cinfo; + } + while (z_cinfo > 0 && data_size <= half_z_window_size); - case Z_MEM_ERROR: - err = "memory"; - break; + z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); - default: - err = "unknown"; - break; + data[0] = (png_byte)z_cmf; + tmp = data[1] & 0xe0; + tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; + data[1] = (png_byte)tmp; } - - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, ret); - png_warning_parameter(p, 2, err); - - if (png_ptr->zstream.msg) - err = png_ptr->zstream.msg; - else - err = "[no zlib message]"; - - png_warning_parameter(p, 3, err); - - png_formatted_warning(png_ptr, p, - "zlib failed to reset compressor: @1(@2): @3"); } } +} +#endif /* WRITE_OPTIMIZE_CMF */ - else - png_warning(png_ptr, "zstream not in use (internal error)"); +/* Initialize the compressor for the appropriate type of compression. */ +static int +png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, + png_alloc_size_t data_size) +{ + if (png_ptr->zowner != 0) + { +#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) + char msg[64]; + + PNG_STRING_FROM_CHUNK(msg, owner); + msg[4] = ':'; + msg[5] = ' '; + PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner); + /* So the message that results is " using zstream"; this is an + * internal error, but is very useful for debugging. i18n requirements + * are minimal. + */ + (void)png_safecat(msg, (sizeof msg), 10, " using zstream"); +#endif +#if PNG_RELEASE_BUILD + png_warning(png_ptr, msg); + + /* Attempt sane error recovery */ + if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */ + { + png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT"); + return Z_STREAM_ERROR; + } + + png_ptr->zowner = 0; +#else + png_error(png_ptr, msg); +#endif + } + + { + int level = png_ptr->zlib_level; + int method = png_ptr->zlib_method; + int windowBits = png_ptr->zlib_window_bits; + int memLevel = png_ptr->zlib_mem_level; + int strategy; /* set below */ + int ret; /* zlib return code */ + + if (owner == png_IDAT) + { + if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0) + strategy = png_ptr->zlib_strategy; + + else if (png_ptr->do_filter != PNG_FILTER_NONE) + strategy = PNG_Z_DEFAULT_STRATEGY; + + else + strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY; + } + + else + { +#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED + level = png_ptr->zlib_text_level; + method = png_ptr->zlib_text_method; + windowBits = png_ptr->zlib_text_window_bits; + memLevel = png_ptr->zlib_text_mem_level; + strategy = png_ptr->zlib_text_strategy; +#else + /* If customization is not supported the values all come from the + * IDAT values except for the strategy, which is fixed to the + * default. (This is the pre-1.6.0 behavior too, although it was + * implemented in a very different way.) + */ + strategy = Z_DEFAULT_STRATEGY; +#endif + } + + /* Adjust 'windowBits' down if larger than 'data_size'; to stop this + * happening just pass 32768 as the data_size parameter. Notice that zlib + * requires an extra 262 bytes in the window in addition to the data to be + * able to see the whole of the data, so if data_size+262 takes us to the + * next windowBits size we need to fix up the value later. (Because even + * though deflate needs the extra window, inflate does not!) + */ + if (data_size <= 16384) + { + /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to + * work round a Microsoft Visual C misbehavior which, contrary to C-90, + * widens the result of the following shift to 64-bits if (and, + * apparently, only if) it is used in a test. + */ + unsigned int half_window_size = 1U << (windowBits-1); + + while (data_size + 262 <= half_window_size) + { + half_window_size >>= 1; + --windowBits; + } + } + + /* Check against the previous initialized values, if any. */ + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 && + (png_ptr->zlib_set_level != level || + png_ptr->zlib_set_method != method || + png_ptr->zlib_set_window_bits != windowBits || + png_ptr->zlib_set_mem_level != memLevel || + png_ptr->zlib_set_strategy != strategy)) + { + if (deflateEnd(&png_ptr->zstream) != Z_OK) + png_warning(png_ptr, "deflateEnd failed (ignored)"); + + png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED; + } + + /* For safety clear out the input and output pointers (currently zlib + * doesn't use them on Init, but it might in the future). + */ + png_ptr->zstream.next_in = NULL; + png_ptr->zstream.avail_in = 0; + png_ptr->zstream.next_out = NULL; + png_ptr->zstream.avail_out = 0; + + /* Now initialize if required, setting the new parameters, otherwise just + * to a simple reset to the previous parameters. + */ + if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) + ret = deflateReset(&png_ptr->zstream); + + else + { + ret = deflateInit2(&png_ptr->zstream, level, method, windowBits, + memLevel, strategy); + + if (ret == Z_OK) + png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; + } + + /* The return code is from either deflateReset or deflateInit2; they have + * pretty much the same set of error codes. + */ + if (ret == Z_OK) + png_ptr->zowner = owner; + + else + png_zstream_error(png_ptr, ret); + + return ret; + } +} + +/* Clean up (or trim) a linked list of compression buffers. */ +void /* PRIVATE */ +png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp) +{ + png_compression_bufferp list = *listp; + + if (list != NULL) + { + *listp = NULL; + + do + { + png_compression_bufferp next = list->next; + + png_free(png_ptr, list); + list = next; + } + while (list != NULL); + } } #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED /* This pair of functions encapsulates the operation of (a) compressing a * text string, and (b) issuing it later as a series of chunk data writes. * The compression_state structure is shared context for these functions - * set up by the caller in order to make the whole mess thread-safe. + * set up by the caller to allow access to the relevant local variables. + * + * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size + * temporary buffers. From 1.6.0 it is retained in png_struct so that it will + * be correctly freed in the event of a write error (previous implementations + * just leaked memory.) */ - typedef struct { - png_const_bytep input; /* The uncompressed input data */ - png_size_t input_len; /* Its length */ - int num_output_ptr; /* Number of output pointers used */ - int max_output_ptr; /* Size of output_ptr */ - png_bytep *output_ptr; /* Array of pointers to output */ + png_const_bytep input; /* The uncompressed input data */ + png_alloc_size_t input_len; /* Its length */ + png_uint_32 output_len; /* Final compressed length */ + png_byte output[1024]; /* First block of output */ } compression_state; -/* Compress given text into storage in the png_ptr structure */ -static int /* PRIVATE */ -png_text_compress(png_structp png_ptr, - png_const_charp text, png_size_t text_len, int compression, - compression_state *comp) +static void +png_text_compress_init(compression_state *comp, png_const_bytep input, + png_alloc_size_t input_len) +{ + comp->input = input; + comp->input_len = input_len; + comp->output_len = 0; +} + +/* Compress the data in the compression state input */ +static int +png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, + compression_state *comp, png_uint_32 prefix_len) { int ret; - comp->num_output_ptr = 0; - comp->max_output_ptr = 0; - comp->output_ptr = NULL; - comp->input = NULL; - comp->input_len = text_len; - - /* We may just want to pass the text right through */ - if (compression == PNG_TEXT_COMPRESSION_NONE) - { - comp->input = (png_const_bytep)text; - return((int)text_len); - } - - if (compression >= PNG_TEXT_COMPRESSION_LAST) - { - PNG_WARNING_PARAMETERS(p) - - png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, - compression); - png_formatted_warning(png_ptr, p, "Unknown compression type @1"); - } - - /* We can't write the chunk until we find out how much data we have, - * which means we need to run the compressor first and save the - * output. This shouldn't be a problem, as the vast majority of - * comments should be reasonable, but we will set up an array of - * malloc'd pointers to be sure. + /* To find the length of the output it is necessary to first compress the + * input. The result is buffered rather than using the two-pass algorithm + * that is used on the inflate side; deflate is assumed to be slower and a + * PNG writer is assumed to have more memory available than a PNG reader. * - * If we knew the application was well behaved, we could simplify this - * greatly by assuming we can always malloc an output buffer large - * enough to hold the compressed text ((1001 * text_len / 1000) + 12) - * and malloc this directly. The only time this would be a bad idea is - * if we can't malloc more than 64K and we have 64K of random input - * data, or if the input string is incredibly large (although this - * wouldn't cause a failure, just a slowdown due to swapping). + * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an + * upper limit on the output size, but it is always bigger than the input + * size so it is likely to be more efficient to use this linked-list + * approach. */ - png_zlib_claim(png_ptr, PNG_ZLIB_FOR_TEXT); + ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len); - /* Set up the compression buffers */ - /* TODO: the following cast hides a potential overflow problem. */ - png_ptr->zstream.avail_in = (uInt)text_len; + if (ret != Z_OK) + return ret; - /* NOTE: assume zlib doesn't overwrite the input */ - png_ptr->zstream.next_in = (Bytef *)text; - png_ptr->zstream.avail_out = png_ptr->zbuf_size; - png_ptr->zstream.next_out = png_ptr->zbuf; - - /* This is the same compression loop as in png_write_row() */ - do + /* Set up the compression buffers, we need a loop here to avoid overflowing a + * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited + * by the output buffer size, so there is no need to check that. Since this + * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits + * in size. + */ { - /* Compress the data */ - ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); + png_compression_bufferp *end = &png_ptr->zbuffer_list; + png_alloc_size_t input_len = comp->input_len; /* may be zero! */ + png_uint_32 output_len; - if (ret != Z_OK) + /* zlib updates these for us: */ + png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input); + png_ptr->zstream.avail_in = 0; /* Set below */ + png_ptr->zstream.next_out = comp->output; + png_ptr->zstream.avail_out = (sizeof comp->output); + + output_len = png_ptr->zstream.avail_out; + + do { - /* Error */ - if (png_ptr->zstream.msg != NULL) - png_error(png_ptr, png_ptr->zstream.msg); + uInt avail_in = ZLIB_IO_MAX; - else - png_error(png_ptr, "zlib error"); - } + if (avail_in > input_len) + avail_in = (uInt)input_len; - /* Check to see if we need more room */ - if (!(png_ptr->zstream.avail_out)) - { - /* Make sure the output array has room */ - if (comp->num_output_ptr >= comp->max_output_ptr) + input_len -= avail_in; + + png_ptr->zstream.avail_in = avail_in; + + if (png_ptr->zstream.avail_out == 0) { - int old_max; + png_compression_buffer *next; - old_max = comp->max_output_ptr; - comp->max_output_ptr = comp->num_output_ptr + 4; - if (comp->output_ptr != NULL) + /* Chunk data is limited to 2^31 bytes in length, so the prefix + * length must be counted here. + */ + if (output_len + prefix_len > PNG_UINT_31_MAX) { - png_bytepp old_ptr; - - old_ptr = comp->output_ptr; - - comp->output_ptr = (png_bytepp)png_malloc(png_ptr, - (png_alloc_size_t) - (comp->max_output_ptr * png_sizeof(png_charpp))); - - png_memcpy(comp->output_ptr, old_ptr, old_max - * png_sizeof(png_charp)); - - png_free(png_ptr, old_ptr); + ret = Z_MEM_ERROR; + break; } - else - comp->output_ptr = (png_bytepp)png_malloc(png_ptr, - (png_alloc_size_t) - (comp->max_output_ptr * png_sizeof(png_charp))); - } - /* Save the data */ - comp->output_ptr[comp->num_output_ptr] = - (png_bytep)png_malloc(png_ptr, - (png_alloc_size_t)png_ptr->zbuf_size); - - png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, - png_ptr->zbuf_size); - - comp->num_output_ptr++; - - /* and reset the buffer */ - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream.next_out = png_ptr->zbuf; - } - /* Continue until we don't have any more to compress */ - } while (png_ptr->zstream.avail_in); - - /* Finish the compression */ - do - { - /* Tell zlib we are finished */ - ret = deflate(&png_ptr->zstream, Z_FINISH); - - if (ret == Z_OK) - { - /* Check to see if we need more room */ - if (!(png_ptr->zstream.avail_out)) - { - /* Check to make sure our output array has room */ - if (comp->num_output_ptr >= comp->max_output_ptr) + /* Need a new (malloc'ed) buffer, but there may be one present + * already. + */ + next = *end; + if (next == NULL) { - int old_max; + next = png_voidcast(png_compression_bufferp, png_malloc_base + (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); - old_max = comp->max_output_ptr; - comp->max_output_ptr = comp->num_output_ptr + 4; - if (comp->output_ptr != NULL) + if (next == NULL) { - png_bytepp old_ptr; - - old_ptr = comp->output_ptr; - - /* This could be optimized to realloc() */ - comp->output_ptr = (png_bytepp)png_malloc(png_ptr, - (png_alloc_size_t)(comp->max_output_ptr * - png_sizeof(png_charp))); - - png_memcpy(comp->output_ptr, old_ptr, - old_max * png_sizeof(png_charp)); - - png_free(png_ptr, old_ptr); + ret = Z_MEM_ERROR; + break; } - else - comp->output_ptr = (png_bytepp)png_malloc(png_ptr, - (png_alloc_size_t)(comp->max_output_ptr * - png_sizeof(png_charp))); + /* Link in this buffer (so that it will be freed later) */ + next->next = NULL; + *end = next; } - /* Save the data */ - comp->output_ptr[comp->num_output_ptr] = - (png_bytep)png_malloc(png_ptr, - (png_alloc_size_t)png_ptr->zbuf_size); + png_ptr->zstream.next_out = next->output; + png_ptr->zstream.avail_out = png_ptr->zbuffer_size; + output_len += png_ptr->zstream.avail_out; - png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, - png_ptr->zbuf_size); - - comp->num_output_ptr++; - - /* and reset the buffer pointers */ - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream.next_out = png_ptr->zbuf; + /* Move 'end' to the next buffer pointer. */ + end = &next->next; } + + /* Compress the data */ + ret = deflate(&png_ptr->zstream, + input_len > 0 ? Z_NO_FLUSH : Z_FINISH); + + /* Claw back input data that was not consumed (because avail_in is + * reset above every time round the loop). + */ + input_len += png_ptr->zstream.avail_in; + png_ptr->zstream.avail_in = 0; /* safety */ } - else if (ret != Z_STREAM_END) + while (ret == Z_OK); + + /* There may be some space left in the last output buffer. This needs to + * be subtracted from output_len. + */ + output_len -= png_ptr->zstream.avail_out; + png_ptr->zstream.avail_out = 0; /* safety */ + comp->output_len = output_len; + + /* Now double check the output length, put in a custom message if it is + * too long. Otherwise ensure the z_stream::msg pointer is set to + * something. + */ + if (output_len + prefix_len >= PNG_UINT_31_MAX) { - /* We got an error */ - if (png_ptr->zstream.msg != NULL) - png_error(png_ptr, png_ptr->zstream.msg); - - else - png_error(png_ptr, "zlib error"); + png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long"); + ret = Z_MEM_ERROR; } - } while (ret != Z_STREAM_END); - /* Text length is number of buffers plus last buffer */ - text_len = png_ptr->zbuf_size * comp->num_output_ptr; + else + png_zstream_error(png_ptr, ret); - if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) - text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; + /* Reset zlib for another zTXt/iTXt or image data */ + png_ptr->zowner = 0; - return((int)text_len); + /* The only success case is Z_STREAM_END, input_len must be 0; if not this + * is an internal error. + */ + if (ret == Z_STREAM_END && input_len == 0) + { +#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED + /* Fix up the deflate header, if required */ + optimize_cmf(comp->output, comp->input_len); +#endif + /* But Z_OK is returned, not Z_STREAM_END; this allows the claim + * function above to return Z_STREAM_END on an error (though it never + * does in the current versions of zlib.) + */ + return Z_OK; + } + + else + return ret; + } } /* Ship the compressed text out via chunk writes */ -static void /* PRIVATE */ -png_write_compressed_data_out(png_structp png_ptr, compression_state *comp, - png_size_t data_len) +static void +png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp) { - int i; + png_uint_32 output_len = comp->output_len; + png_const_bytep output = comp->output; + png_uint_32 avail = (sizeof comp->output); + png_compression_buffer *next = png_ptr->zbuffer_list; - /* Handle the no-compression case */ - if (comp->input) + for (;;) { - png_write_chunk_data(png_ptr, comp->input, data_len); + if (avail > output_len) + avail = output_len; - return; + png_write_chunk_data(png_ptr, output, avail); + + output_len -= avail; + + if (output_len == 0 || next == NULL) + break; + + avail = png_ptr->zbuffer_size; + output = next->output; + next = next->next; } -#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED - /* The zbuf_size test is because the code below doesn't work if zbuf_size is - * '1'; simply skip it to avoid memory overwrite. - */ - if (data_len >= 2 && comp->input_len < 16384 && png_ptr->zbuf_size > 1) + /* This is an internal error; 'next' must have been NULL! */ + if (output_len > 0) + png_error(png_ptr, "error writing ancillary chunked compressed data"); +} +#endif /* WRITE_COMPRESSED_TEXT */ + +#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ + defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) +/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, + * and if invalid, correct the keyword rather than discarding the entire + * chunk. The PNG 1.0 specification requires keywords 1-79 characters in + * length, forbids leading or trailing whitespace, multiple internal spaces, + * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. + * + * The 'new_key' buffer must be 80 characters in size (for the keyword plus a + * trailing '\0'). If this routine returns 0 then there was no keyword, or a + * valid one could not be generated, and the caller must png_error. + */ +static png_uint_32 +png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key) +{ + png_const_charp orig_key = key; + png_uint_32 key_len = 0; + int bad_character = 0; + int space = 1; + + png_debug(1, "in png_check_keyword"); + + if (key == NULL) { - unsigned int z_cmf; /* zlib compression method and flags */ + *new_key = 0; + return 0; + } - /* Optimize the CMF field in the zlib stream. This hack of the zlib - * stream is compliant to the stream specification. - */ + while (*key && key_len < 79) + { + png_byte ch = (png_byte)*key++; - if (comp->num_output_ptr) - z_cmf = comp->output_ptr[0][0]; - else - z_cmf = png_ptr->zbuf[0]; + if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) + *new_key++ = ch, ++key_len, space = 0; - if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) + else if (space == 0) { - unsigned int z_cinfo; - unsigned int half_z_window_size; - png_size_t uncompressed_text_size = comp->input_len; + /* A space or an invalid character when one wasn't seen immediately + * before; output just a space. + */ + *new_key++ = 32, ++key_len, space = 1; - z_cinfo = z_cmf >> 4; - half_z_window_size = 1 << (z_cinfo + 7); - - while (uncompressed_text_size <= half_z_window_size && - half_z_window_size >= 256) - { - z_cinfo--; - half_z_window_size >>= 1; - } - - z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); - - if (comp->num_output_ptr) - { - - if (comp->output_ptr[0][0] != z_cmf) - { - int tmp; - - comp->output_ptr[0][0] = (png_byte)z_cmf; - tmp = comp->output_ptr[0][1] & 0xe0; - tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; - comp->output_ptr[0][1] = (png_byte)tmp; - } - } - else - { - int tmp; - - png_ptr->zbuf[0] = (png_byte)z_cmf; - tmp = png_ptr->zbuf[1] & 0xe0; - tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; - png_ptr->zbuf[1] = (png_byte)tmp; - } + /* If the character was not a space then it is invalid. */ + if (ch != 32) + bad_character = ch; } - else - png_error(png_ptr, - "Invalid zlib compression method or flags in non-IDAT chunk"); + else if (bad_character == 0) + bad_character = ch; /* just skip it, record the first error */ } -#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ - /* Write saved output buffers, if any */ - for (i = 0; i < comp->num_output_ptr; i++) + if (key_len > 0 && space != 0) /* trailing space */ { - png_write_chunk_data(png_ptr, comp->output_ptr[i], - (png_size_t)png_ptr->zbuf_size); - - png_free(png_ptr, comp->output_ptr[i]); + --key_len, --new_key; + if (bad_character == 0) + bad_character = 32; } - if (comp->max_output_ptr != 0) - png_free(png_ptr, comp->output_ptr); + /* Terminate the keyword */ + *new_key = 0; - /* Write anything left in zbuf */ - if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) - png_write_chunk_data(png_ptr, png_ptr->zbuf, - (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); + if (key_len == 0) + return 0; - /* Reset zlib for another zTXt/iTXt or image data */ - png_zlib_release(png_ptr); +#ifdef PNG_WARNINGS_SUPPORTED + /* Try to only output one warning per keyword: */ + if (*key != 0) /* keyword too long */ + png_warning(png_ptr, "keyword truncated"); + + else if (bad_character != 0) + { + PNG_WARNING_PARAMETERS(p) + + png_warning_parameter(p, 1, orig_key); + png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); + + png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); + } +#endif /* WARNINGS */ + + return key_len; } -#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ +#endif /* WRITE_TEXT || WRITE_pCAL || WRITE_iCCP || WRITE_sPLT */ /* Write the IHDR chunk, and update the png_struct with the necessary * information. Note that the rest of this code depends upon this * information being correct. */ void /* PRIVATE */ -png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, +png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_type, int filter_type, int interlace_type) { @@ -768,8 +848,8 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, */ if ( #ifdef PNG_MNG_FEATURES_SUPPORTED - !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && - ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && + !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && + ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) && (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && @@ -791,7 +871,7 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, interlace_type=PNG_INTERLACE_NONE; #endif - /* Save the relevent information */ + /* Save the relevant information */ png_ptr->bit_depth = (png_byte)bit_depth; png_ptr->color_type = (png_byte)color_type; png_ptr->interlaced = (png_byte)interlace_type; @@ -821,12 +901,7 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, /* Write the chunk */ png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); - /* Initialize zlib with PNG info */ - png_ptr->zstream.zalloc = png_zalloc; - png_ptr->zstream.zfree = png_zfree; - png_ptr->zstream.opaque = (voidpf)png_ptr; - - if (!(png_ptr->do_filter)) + if ((png_ptr->do_filter) == PNG_NO_FILTERS) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || png_ptr->bit_depth < 8) @@ -836,55 +911,6 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, png_ptr->do_filter = PNG_ALL_FILTERS; } - if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) - { - if (png_ptr->do_filter != PNG_FILTER_NONE) - png_ptr->zlib_strategy = Z_FILTERED; - - else - png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; - } - - if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) - png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; - - if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) - png_ptr->zlib_mem_level = 8; - - if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) - png_ptr->zlib_window_bits = 15; - - if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) - png_ptr->zlib_method = 8; - -#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED -#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED - if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY)) - png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; - - if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL)) - png_ptr->zlib_text_level = png_ptr->zlib_level; - - if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL)) - png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; - - if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS)) - png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; - - if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD)) - png_ptr->zlib_text_method = png_ptr->zlib_method; -#else - png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; - png_ptr->zlib_text_level = png_ptr->zlib_level; - png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; - png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; - png_ptr->zlib_text_method = png_ptr->zlib_method; -#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */ -#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ - - /* Record that the compressor has not yet been initialized. */ - png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED; - png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ } @@ -893,20 +919,23 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, * structure. */ void /* PRIVATE */ -png_write_PLTE(png_structp png_ptr, png_const_colorp palette, +png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, png_uint_32 num_pal) { - png_uint_32 i; + png_uint_32 max_palette_length, i; png_const_colorp pal_ptr; png_byte buf[3]; png_debug(1, "in png_write_PLTE"); + max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? + (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH; + if (( #ifdef PNG_MNG_FEATURES_SUPPORTED - !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && + (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 && #endif - num_pal == 0) || num_pal > 256) + num_pal == 0) || num_pal > max_palette_length) { if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { @@ -920,7 +949,7 @@ png_write_PLTE(png_structp png_ptr, png_const_colorp palette, } } - if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) + if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) { png_warning(png_ptr, "Ignoring request to write a PLTE chunk in grayscale PNG"); @@ -961,94 +990,165 @@ png_write_PLTE(png_structp png_ptr, png_const_colorp palette, png_ptr->mode |= PNG_HAVE_PLTE; } -/* Write an IDAT chunk */ +/* This is similar to png_text_compress, above, except that it does not require + * all of the data at once and, instead of buffering the compressed result, + * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out + * because it calls the write interface. As a result it does its own error + * reporting and does not return an error code. In the event of error it will + * just call png_error. The input data length may exceed 32-bits. The 'flush' + * parameter is exactly the same as that to deflate, with the following + * meanings: + * + * Z_NO_FLUSH: normal incremental output of compressed data + * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush + * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up + * + * The routine manages the acquire and release of the png_ptr->zstream by + * checking and (at the end) clearing png_ptr->zowner; it does some sanity + * checks on the 'mode' flags while doing this. + */ void /* PRIVATE */ -png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) +png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, + png_alloc_size_t input_len, int flush) { - png_debug(1, "in png_write_IDAT"); - -#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED - if (!(png_ptr->mode & PNG_HAVE_IDAT) && - png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) + if (png_ptr->zowner != png_IDAT) { - /* Optimize the CMF field in the zlib stream. This hack of the zlib - * stream is compliant to the stream specification. + /* First time. Ensure we have a temporary buffer for compression and + * trim the buffer list if it has more than one entry to free memory. + * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been + * created at this point, but the check here is quick and safe. */ - unsigned int z_cmf = data[0]; /* zlib compression method and flags */ - - if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) + if (png_ptr->zbuffer_list == NULL) { - /* Avoid memory underflows and multiplication overflows. - * - * The conditions below are practically always satisfied; - * however, they still must be checked. - */ - if (length >= 2 && - png_ptr->height < 16384 && png_ptr->width < 16384) - { - /* Compute the maximum possible length of the datastream */ - - /* Number of pixels, plus for each row a filter byte - * and possibly a padding byte, so increase the maximum - * size to account for these. - */ - unsigned int z_cinfo; - unsigned int half_z_window_size; - png_uint_32 uncompressed_idat_size = png_ptr->height * - ((png_ptr->width * - png_ptr->channels * png_ptr->bit_depth + 15) >> 3); - - /* If it's interlaced, each block of 8 rows is sent as up to - * 14 rows, i.e., 6 additional rows, each with a filter byte - * and possibly a padding byte - */ - if (png_ptr->interlaced) - uncompressed_idat_size += ((png_ptr->height + 7)/8) * - (png_ptr->bit_depth < 8 ? 12 : 6); - - z_cinfo = z_cmf >> 4; - half_z_window_size = 1 << (z_cinfo + 7); - - while (uncompressed_idat_size <= half_z_window_size && - half_z_window_size >= 256) - { - z_cinfo--; - half_z_window_size >>= 1; - } - - z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); - - if (data[0] != z_cmf) - { - int tmp; - data[0] = (png_byte)z_cmf; - tmp = data[1] & 0xe0; - tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; - data[1] = (png_byte)tmp; - } - } + png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp, + png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); + png_ptr->zbuffer_list->next = NULL; } else - png_error(png_ptr, - "Invalid zlib compression method or flags in IDAT"); + png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next); + + /* It is a terminal error if we can't claim the zstream. */ + if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg); + + /* The output state is maintained in png_ptr->zstream, so it must be + * initialized here after the claim. + */ + png_ptr->zstream.next_out = png_ptr->zbuffer_list->output; + png_ptr->zstream.avail_out = png_ptr->zbuffer_size; } -#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */ - png_write_complete_chunk(png_ptr, png_IDAT, data, length); - png_ptr->mode |= PNG_HAVE_IDAT; - - /* Prior to 1.5.4 this code was replicated in every caller (except at the - * end, where it isn't technically necessary). Since this function has - * flushed the data we can safely reset the zlib output buffer here. + /* Now loop reading and writing until all the input is consumed or an error + * terminates the operation. The _out values are maintained across calls to + * this function, but the input must be reset each time. */ - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); + png_ptr->zstream.avail_in = 0; /* set below */ + for (;;) + { + int ret; + + /* INPUT: from the row data */ + uInt avail = ZLIB_IO_MAX; + + if (avail > input_len) + avail = (uInt)input_len; /* safe because of the check */ + + png_ptr->zstream.avail_in = avail; + input_len -= avail; + + ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush); + + /* Include as-yet unconsumed input */ + input_len += png_ptr->zstream.avail_in; + png_ptr->zstream.avail_in = 0; + + /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note + * that these two zstream fields are preserved across the calls, therefore + * there is no need to set these up on entry to the loop. + */ + if (png_ptr->zstream.avail_out == 0) + { + png_bytep data = png_ptr->zbuffer_list->output; + uInt size = png_ptr->zbuffer_size; + + /* Write an IDAT containing the data then reset the buffer. The + * first IDAT may need deflate header optimization. + */ +#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED + if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && + png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) + optimize_cmf(data, png_image_size(png_ptr)); +#endif + + png_write_complete_chunk(png_ptr, png_IDAT, data, size); + png_ptr->mode |= PNG_HAVE_IDAT; + + png_ptr->zstream.next_out = data; + png_ptr->zstream.avail_out = size; + + /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with + * the same flush parameter until it has finished output, for NO_FLUSH + * it doesn't matter. + */ + if (ret == Z_OK && flush != Z_NO_FLUSH) + continue; + } + + /* The order of these checks doesn't matter much; it just affects which + * possible error might be detected if multiple things go wrong at once. + */ + if (ret == Z_OK) /* most likely return code! */ + { + /* If all the input has been consumed then just return. If Z_FINISH + * was used as the flush parameter something has gone wrong if we get + * here. + */ + if (input_len == 0) + { + if (flush == Z_FINISH) + png_error(png_ptr, "Z_OK on Z_FINISH with output space"); + + return; + } + } + + else if (ret == Z_STREAM_END && flush == Z_FINISH) + { + /* This is the end of the IDAT data; any pending output must be + * flushed. For small PNG files we may still be at the beginning. + */ + png_bytep data = png_ptr->zbuffer_list->output; + uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out; + +#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED + if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && + png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) + optimize_cmf(data, png_image_size(png_ptr)); +#endif + + png_write_complete_chunk(png_ptr, png_IDAT, data, size); + png_ptr->zstream.avail_out = 0; + png_ptr->zstream.next_out = NULL; + png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT; + + png_ptr->zowner = 0; /* Release the stream */ + return; + } + + else + { + /* This is an error condition. */ + png_zstream_error(png_ptr, ret); + png_error(png_ptr, png_ptr->zstream.msg); + } + } } /* Write an IEND chunk */ void /* PRIVATE */ -png_write_IEND(png_structp png_ptr) +png_write_IEND(png_structrp png_ptr) { png_debug(1, "in png_write_IEND"); @@ -1059,7 +1159,7 @@ png_write_IEND(png_structp png_ptr) #ifdef PNG_WRITE_gAMA_SUPPORTED /* Write a gAMA chunk */ void /* PRIVATE */ -png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) +png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma) { png_byte buf[4]; @@ -1074,7 +1174,7 @@ png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) #ifdef PNG_WRITE_sRGB_SUPPORTED /* Write a sRGB chunk */ void /* PRIVATE */ -png_write_sRGB(png_structp png_ptr, int srgb_intent) +png_write_sRGB(png_structrp png_ptr, int srgb_intent) { png_byte buf[1]; @@ -1092,94 +1192,72 @@ png_write_sRGB(png_structp png_ptr, int srgb_intent) #ifdef PNG_WRITE_iCCP_SUPPORTED /* Write an iCCP chunk */ void /* PRIVATE */ -png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type, - png_const_charp profile, int profile_len) +png_write_iCCP(png_structrp png_ptr, png_const_charp name, + png_const_bytep profile) { - png_size_t name_len; - png_charp new_name; + png_uint_32 name_len; + png_uint_32 profile_len; + png_byte new_name[81]; /* 1 byte for the compression byte */ compression_state comp; - int embedded_profile_len = 0; + png_uint_32 temp; png_debug(1, "in png_write_iCCP"); - comp.num_output_ptr = 0; - comp.max_output_ptr = 0; - comp.output_ptr = NULL; - comp.input = NULL; - comp.input_len = 0; - - if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0) - return; - - if (compression_type != PNG_COMPRESSION_TYPE_BASE) - png_warning(png_ptr, "Unknown compression type in iCCP chunk"); - + /* These are all internal problems: the profile should have been checked + * before when it was stored. + */ if (profile == NULL) - profile_len = 0; + png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */ - if (profile_len > 3) - embedded_profile_len = - ((*( (png_const_bytep)profile ))<<24) | - ((*( (png_const_bytep)profile + 1))<<16) | - ((*( (png_const_bytep)profile + 2))<< 8) | - ((*( (png_const_bytep)profile + 3)) ); + profile_len = png_get_uint_32(profile); + + if (profile_len < 132) + png_error(png_ptr, "ICC profile too short"); + + temp = (png_uint_32) (*(profile+8)); + if (temp > 3 && (profile_len & 0x03)) + png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)"); - if (embedded_profile_len < 0) { - png_warning(png_ptr, - "Embedded profile length in iCCP chunk is negative"); + png_uint_32 embedded_profile_len = png_get_uint_32(profile); - png_free(png_ptr, new_name); - return; + if (profile_len != embedded_profile_len) + png_error(png_ptr, "Profile length does not match profile"); } - if (profile_len < embedded_profile_len) - { - png_warning(png_ptr, - "Embedded profile length too large in iCCP chunk"); + name_len = png_check_keyword(png_ptr, name, new_name); - png_free(png_ptr, new_name); - return; - } + if (name_len == 0) + png_error(png_ptr, "iCCP: invalid keyword"); - if (profile_len > embedded_profile_len) - { - png_warning(png_ptr, - "Truncating profile to actual length in iCCP chunk"); - - profile_len = embedded_profile_len; - } - - if (profile_len) - profile_len = png_text_compress(png_ptr, profile, - (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp); + new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE; /* Make sure we include the NULL after the name and the compression type */ - png_write_chunk_header(png_ptr, png_iCCP, - (png_uint_32)(name_len + profile_len + 2)); + ++name_len; - new_name[name_len + 1] = 0x00; + png_text_compress_init(&comp, profile, profile_len); - png_write_chunk_data(png_ptr, (png_bytep)new_name, - (png_size_t)(name_len + 2)); + /* Allow for keyword terminator and compression byte */ + if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg); - if (profile_len) - { - png_write_compressed_data_out(png_ptr, &comp, profile_len); - } + png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len); + + png_write_chunk_data(png_ptr, new_name, name_len); + + png_write_compressed_data_out(png_ptr, &comp); png_write_chunk_end(png_ptr); - png_free(png_ptr, new_name); } #endif #ifdef PNG_WRITE_sPLT_SUPPORTED /* Write a sPLT chunk */ void /* PRIVATE */ -png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette) +png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette) { - png_size_t name_len; - png_charp new_name; + png_uint_32 name_len; + png_byte new_name[80]; png_byte entrybuf[10]; png_size_t entry_size = (spalette->depth == 8 ? 6 : 10); png_size_t palette_size = entry_size * spalette->nentries; @@ -1190,8 +1268,10 @@ png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette) png_debug(1, "in png_write_sPLT"); - if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0) - return; + name_len = png_check_keyword(png_ptr, spalette->name, new_name); + + if (name_len == 0) + png_error(png_ptr, "sPLT: invalid keyword"); /* Make sure we include the NULL after the name */ png_write_chunk_header(png_ptr, png_sPLT, @@ -1224,7 +1304,7 @@ png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette) png_save_uint_16(entrybuf + 8, ep->frequency); } - png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); + png_write_chunk_data(png_ptr, entrybuf, entry_size); } #else ep=spalette->entries; @@ -1248,19 +1328,18 @@ png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette) png_save_uint_16(entrybuf + 8, ep[i].frequency); } - png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); + png_write_chunk_data(png_ptr, entrybuf, entry_size); } #endif png_write_chunk_end(png_ptr); - png_free(png_ptr, new_name); } #endif #ifdef PNG_WRITE_sBIT_SUPPORTED /* Write the sBIT chunk */ void /* PRIVATE */ -png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type) +png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type) { png_byte buf[4]; png_size_t size; @@ -1268,7 +1347,7 @@ png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type) png_debug(1, "in png_write_sBIT"); /* Make sure we don't depend upon the order of PNG_COLOR_8 */ - if (color_type & PNG_COLOR_MASK_COLOR) + if ((color_type & PNG_COLOR_MASK_COLOR) != 0) { png_byte maxbits; @@ -1301,7 +1380,7 @@ png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type) size = 1; } - if (color_type & PNG_COLOR_MASK_ALPHA) + if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) { if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) { @@ -1319,42 +1398,33 @@ png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type) #ifdef PNG_WRITE_cHRM_SUPPORTED /* Write the cHRM chunk */ void /* PRIVATE */ -png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, - png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, - png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, - png_fixed_point blue_y) +png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy) { png_byte buf[32]; png_debug(1, "in png_write_cHRM"); /* Each value is saved in 1/100,000ths */ -#ifdef PNG_CHECK_cHRM_SUPPORTED - if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y, - green_x, green_y, blue_x, blue_y)) -#endif - { - png_save_uint_32(buf, (png_uint_32)white_x); - png_save_uint_32(buf + 4, (png_uint_32)white_y); + png_save_int_32(buf, xy->whitex); + png_save_int_32(buf + 4, xy->whitey); - png_save_uint_32(buf + 8, (png_uint_32)red_x); - png_save_uint_32(buf + 12, (png_uint_32)red_y); + png_save_int_32(buf + 8, xy->redx); + png_save_int_32(buf + 12, xy->redy); - png_save_uint_32(buf + 16, (png_uint_32)green_x); - png_save_uint_32(buf + 20, (png_uint_32)green_y); + png_save_int_32(buf + 16, xy->greenx); + png_save_int_32(buf + 20, xy->greeny); - png_save_uint_32(buf + 24, (png_uint_32)blue_x); - png_save_uint_32(buf + 28, (png_uint_32)blue_y); + png_save_int_32(buf + 24, xy->bluex); + png_save_int_32(buf + 28, xy->bluey); - png_write_complete_chunk(png_ptr, png_cHRM, buf, (png_size_t)32); - } + png_write_complete_chunk(png_ptr, png_cHRM, buf, 32); } #endif #ifdef PNG_WRITE_tRNS_SUPPORTED /* Write the tRNS chunk */ void /* PRIVATE */ -png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, +png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha, png_const_color_16p tran, int num_trans, int color_type) { png_byte buf[6]; @@ -1365,20 +1435,22 @@ png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, { if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) { - png_warning(png_ptr, "Invalid number of transparent colors specified"); + png_app_warning(png_ptr, + "Invalid number of transparent colors specified"); return; } /* Write the chunk out as it is */ - png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans); + png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, + (png_size_t)num_trans); } else if (color_type == PNG_COLOR_TYPE_GRAY) { - /* One 16 bit value */ + /* One 16-bit value */ if (tran->gray >= (1 << png_ptr->bit_depth)) { - png_warning(png_ptr, + png_app_warning(png_ptr, "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); return; @@ -1390,17 +1462,17 @@ png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, else if (color_type == PNG_COLOR_TYPE_RGB) { - /* Three 16 bit values */ + /* Three 16-bit values */ png_save_uint_16(buf, tran->red); png_save_uint_16(buf + 2, tran->green); png_save_uint_16(buf + 4, tran->blue); #ifdef PNG_WRITE_16BIT_SUPPORTED - if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) + if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) #else - if (buf[0] | buf[2] | buf[4]) + if ((buf[0] | buf[2] | buf[4]) != 0) #endif { - png_warning(png_ptr, + png_app_warning(png_ptr, "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); return; } @@ -1410,7 +1482,7 @@ png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, else { - png_warning(png_ptr, "Can't write tRNS with an alpha channel"); + png_app_warning(png_ptr, "Can't write tRNS with an alpha channel"); } } #endif @@ -1418,7 +1490,7 @@ png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, #ifdef PNG_WRITE_bKGD_SUPPORTED /* Write the background chunk */ void /* PRIVATE */ -png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type) +png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type) { png_byte buf[6]; @@ -1428,8 +1500,8 @@ png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type) { if ( #ifdef PNG_MNG_FEATURES_SUPPORTED - (png_ptr->num_palette || - (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && + (png_ptr->num_palette != 0 || + (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) && #endif back->index >= png_ptr->num_palette) { @@ -1441,15 +1513,15 @@ png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type) png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); } - else if (color_type & PNG_COLOR_MASK_COLOR) + else if ((color_type & PNG_COLOR_MASK_COLOR) != 0) { png_save_uint_16(buf, back->red); png_save_uint_16(buf + 2, back->green); png_save_uint_16(buf + 4, back->blue); #ifdef PNG_WRITE_16BIT_SUPPORTED - if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) + if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) #else - if (buf[0] | buf[2] | buf[4]) + if ((buf[0] | buf[2] | buf[4]) != 0) #endif { png_warning(png_ptr, @@ -1480,7 +1552,7 @@ png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type) #ifdef PNG_WRITE_hIST_SUPPORTED /* Write the histogram */ void /* PRIVATE */ -png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist) +png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist) { int i; png_byte buf[3]; @@ -1508,234 +1580,94 @@ png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist) } #endif -#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ - defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) -/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, - * and if invalid, correct the keyword rather than discarding the entire - * chunk. The PNG 1.0 specification requires keywords 1-79 characters in - * length, forbids leading or trailing whitespace, multiple internal spaces, - * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. - * - * The new_key is allocated to hold the corrected keyword and must be freed - * by the calling routine. This avoids problems with trying to write to - * static keywords without having to have duplicate copies of the strings. - */ -png_size_t /* PRIVATE */ -png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key) -{ - png_size_t key_len; - png_const_charp ikp; - png_charp kp, dp; - int kflag; - int kwarn=0; - - png_debug(1, "in png_check_keyword"); - - *new_key = NULL; - - if (key == NULL || (key_len = png_strlen(key)) == 0) - { - png_warning(png_ptr, "zero length keyword"); - return ((png_size_t)0); - } - - png_debug1(2, "Keyword to be checked is '%s'", key); - - *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); - - if (*new_key == NULL) - { - png_warning(png_ptr, "Out of memory while procesing keyword"); - return ((png_size_t)0); - } - - /* Replace non-printing characters with a blank and print a warning */ - for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++) - { - if ((png_byte)*ikp < 0x20 || - ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1)) - { - PNG_WARNING_PARAMETERS(p) - - png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_02x, - (png_byte)*ikp); - png_formatted_warning(png_ptr, p, "invalid keyword character 0x@1"); - *dp = ' '; - } - - else - { - *dp = *ikp; - } - } - *dp = '\0'; - - /* Remove any trailing white space. */ - kp = *new_key + key_len - 1; - if (*kp == ' ') - { - png_warning(png_ptr, "trailing spaces removed from keyword"); - - while (*kp == ' ') - { - *(kp--) = '\0'; - key_len--; - } - } - - /* Remove any leading white space. */ - kp = *new_key; - if (*kp == ' ') - { - png_warning(png_ptr, "leading spaces removed from keyword"); - - while (*kp == ' ') - { - kp++; - key_len--; - } - } - - png_debug1(2, "Checking for multiple internal spaces in '%s'", kp); - - /* Remove multiple internal spaces. */ - for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) - { - if (*kp == ' ' && kflag == 0) - { - *(dp++) = *kp; - kflag = 1; - } - - else if (*kp == ' ') - { - key_len--; - kwarn = 1; - } - - else - { - *(dp++) = *kp; - kflag = 0; - } - } - *dp = '\0'; - if (kwarn) - png_warning(png_ptr, "extra interior spaces removed from keyword"); - - if (key_len == 0) - { - png_free(png_ptr, *new_key); - png_warning(png_ptr, "Zero length keyword"); - } - - if (key_len > 79) - { - png_warning(png_ptr, "keyword length must be 1 - 79 characters"); - (*new_key)[79] = '\0'; - key_len = 79; - } - - return (key_len); -} -#endif - #ifdef PNG_WRITE_tEXt_SUPPORTED /* Write a tEXt chunk */ void /* PRIVATE */ -png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text, +png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, png_size_t text_len) { - png_size_t key_len; - png_charp new_key; + png_uint_32 key_len; + png_byte new_key[80]; png_debug(1, "in png_write_tEXt"); - if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) - return; + key_len = png_check_keyword(png_ptr, key, new_key); + + if (key_len == 0) + png_error(png_ptr, "tEXt: invalid keyword"); if (text == NULL || *text == '\0') text_len = 0; else - text_len = png_strlen(text); + text_len = strlen(text); + + if (text_len > PNG_UINT_31_MAX - (key_len+1)) + png_error(png_ptr, "tEXt: text too long"); /* Make sure we include the 0 after the key */ png_write_chunk_header(png_ptr, png_tEXt, - (png_uint_32)(key_len + text_len + 1)); + (png_uint_32)/*checked above*/(key_len + text_len + 1)); /* * We leave it to the application to meet PNG-1.0 requirements on the * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. */ - png_write_chunk_data(png_ptr, (png_bytep)new_key, - (png_size_t)(key_len + 1)); + png_write_chunk_data(png_ptr, new_key, key_len + 1); - if (text_len) - png_write_chunk_data(png_ptr, (png_const_bytep)text, - (png_size_t)text_len); + if (text_len != 0) + png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len); png_write_chunk_end(png_ptr); - png_free(png_ptr, new_key); } #endif #ifdef PNG_WRITE_zTXt_SUPPORTED /* Write a compressed text chunk */ void /* PRIVATE */ -png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text, - png_size_t text_len, int compression) +png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, + int compression) { - png_size_t key_len; - png_byte buf; - png_charp new_key; + png_uint_32 key_len; + png_byte new_key[81]; compression_state comp; png_debug(1, "in png_write_zTXt"); - comp.num_output_ptr = 0; - comp.max_output_ptr = 0; - comp.output_ptr = NULL; - comp.input = NULL; - comp.input_len = 0; - - if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0) + if (compression == PNG_TEXT_COMPRESSION_NONE) { - png_free(png_ptr, new_key); + png_write_tEXt(png_ptr, key, text, 0); return; } - if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) - { - png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); - png_free(png_ptr, new_key); - return; - } + if (compression != PNG_TEXT_COMPRESSION_zTXt) + png_error(png_ptr, "zTXt: invalid compression type"); - text_len = png_strlen(text); + key_len = png_check_keyword(png_ptr, key, new_key); + + if (key_len == 0) + png_error(png_ptr, "zTXt: invalid keyword"); + + /* Add the compression method and 1 for the keyword separator. */ + new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; + ++key_len; /* Compute the compressed data; do it now for the length */ - text_len = png_text_compress(png_ptr, text, text_len, compression, - &comp); + png_text_compress_init(&comp, (png_const_bytep)text, + text == NULL ? 0 : strlen(text)); + + if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg); /* Write start of chunk */ - png_write_chunk_header(png_ptr, png_zTXt, - (png_uint_32)(key_len+text_len + 2)); + png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len); /* Write key */ - png_write_chunk_data(png_ptr, (png_bytep)new_key, - (png_size_t)(key_len + 1)); - - png_free(png_ptr, new_key); - - buf = (png_byte)compression; - - /* Write compression */ - png_write_chunk_data(png_ptr, &buf, (png_size_t)1); + png_write_chunk_data(png_ptr, new_key, key_len); /* Write the compressed data */ - png_write_compressed_data_out(png_ptr, &comp, text_len); + png_write_compressed_data_out(png_ptr, &comp); /* Close the chunk */ png_write_chunk_end(png_ptr); @@ -1745,100 +1677,107 @@ png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text, #ifdef PNG_WRITE_iTXt_SUPPORTED /* Write an iTXt chunk */ void /* PRIVATE */ -png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key, +png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key, png_const_charp lang, png_const_charp lang_key, png_const_charp text) { - png_size_t lang_len, key_len, lang_key_len, text_len; - png_charp new_lang; - png_charp new_key = NULL; - png_byte cbuf[2]; + png_uint_32 key_len, prefix_len; + png_size_t lang_len, lang_key_len; + png_byte new_key[82]; compression_state comp; png_debug(1, "in png_write_iTXt"); - comp.num_output_ptr = 0; - comp.max_output_ptr = 0; - comp.output_ptr = NULL; - comp.input = NULL; + key_len = png_check_keyword(png_ptr, key, new_key); - if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0) - return; + if (key_len == 0) + png_error(png_ptr, "iTXt: invalid keyword"); - if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0) + /* Set the compression flag */ + switch (compression) { - png_warning(png_ptr, "Empty language field in iTXt chunk"); - new_lang = NULL; - lang_len = 0; + case PNG_ITXT_COMPRESSION_NONE: + case PNG_TEXT_COMPRESSION_NONE: + compression = new_key[++key_len] = 0; /* no compression */ + break; + + case PNG_TEXT_COMPRESSION_zTXt: + case PNG_ITXT_COMPRESSION_zTXt: + compression = new_key[++key_len] = 1; /* compressed */ + break; + + default: + png_error(png_ptr, "iTXt: invalid compression"); } - if (lang_key == NULL) - lang_key_len = 0; - - else - lang_key_len = png_strlen(lang_key); - - if (text == NULL) - text_len = 0; - - else - text_len = png_strlen(text); - - /* Compute the compressed data; do it now for the length */ - text_len = png_text_compress(png_ptr, text, text_len, compression - 2, - &comp); - - - /* Make sure we include the compression flag, the compression byte, - * and the NULs after the key, lang, and lang_key parts - */ - - png_write_chunk_header(png_ptr, png_iTXt, (png_uint_32)( - 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ - + key_len - + lang_len - + lang_key_len - + text_len)); + new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; + ++key_len; /* for the keywod separator */ /* We leave it to the application to meet PNG-1.0 requirements on the * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of - * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. + * any non-Latin-1 characters except for NEWLINE. ISO PNG, however, + * specifies that the text is UTF-8 and this really doesn't require any + * checking. + * * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. + * + * TODO: validate the language tag correctly (see the spec.) */ - png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1)); + if (lang == NULL) lang = ""; /* empty language is valid */ + lang_len = strlen(lang)+1; + if (lang_key == NULL) lang_key = ""; /* may be empty */ + lang_key_len = strlen(lang_key)+1; + if (text == NULL) text = ""; /* may be empty */ - /* Set the compression flag */ - if (compression == PNG_ITXT_COMPRESSION_NONE || - compression == PNG_TEXT_COMPRESSION_NONE) - cbuf[0] = 0; + prefix_len = key_len; + if (lang_len > PNG_UINT_31_MAX-prefix_len) + prefix_len = PNG_UINT_31_MAX; + else + prefix_len = (png_uint_32)(prefix_len + lang_len); - else /* compression == PNG_ITXT_COMPRESSION_zTXt */ - cbuf[0] = 1; + if (lang_key_len > PNG_UINT_31_MAX-prefix_len) + prefix_len = PNG_UINT_31_MAX; + else + prefix_len = (png_uint_32)(prefix_len + lang_key_len); - /* Set the compression method */ - cbuf[1] = 0; + png_text_compress_init(&comp, (png_const_bytep)text, strlen(text)); - png_write_chunk_data(png_ptr, cbuf, (png_size_t)2); + if (compression != 0) + { + if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK) + png_error(png_ptr, png_ptr->zstream.msg); + } - cbuf[0] = 0; - png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf), - (png_size_t)(lang_len + 1)); + else + { + if (comp.input_len > PNG_UINT_31_MAX-prefix_len) + png_error(png_ptr, "iTXt: uncompressed text too long"); - png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf), - (png_size_t)(lang_key_len + 1)); + /* So the string will fit in a chunk: */ + comp.output_len = (png_uint_32)/*SAFE*/comp.input_len; + } - png_write_compressed_data_out(png_ptr, &comp, text_len); + png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len); + + png_write_chunk_data(png_ptr, new_key, key_len); + + png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len); + + png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len); + + if (compression != 0) + png_write_compressed_data_out(png_ptr, &comp); + + else + png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.output_len); png_write_chunk_end(png_ptr); - - png_free(png_ptr, new_key); - png_free(png_ptr, new_lang); } #endif #ifdef PNG_WRITE_oFFs_SUPPORTED /* Write the oFFs chunk */ void /* PRIVATE */ -png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, +png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, int unit_type) { png_byte buf[9]; @@ -1858,36 +1797,43 @@ png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, #ifdef PNG_WRITE_pCAL_SUPPORTED /* Write the pCAL chunk (described in the PNG extensions document) */ void /* PRIVATE */ -png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, +png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_const_charp units, png_charpp params) { - png_size_t purpose_len, units_len, total_len; + png_uint_32 purpose_len; + png_size_t units_len, total_len; png_size_tp params_len; png_byte buf[10]; - png_charp new_purpose; + png_byte new_purpose[80]; int i; png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); if (type >= PNG_EQUATION_LAST) - png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); + png_error(png_ptr, "Unrecognized equation type for pCAL chunk"); + + purpose_len = png_check_keyword(png_ptr, purpose, new_purpose); + + if (purpose_len == 0) + png_error(png_ptr, "pCAL: invalid keyword"); + + ++purpose_len; /* terminator */ - purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); - units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); + units_len = strlen(units) + (nparams == 0 ? 0 : 1); png_debug1(3, "pCAL units length = %d", (int)units_len); total_len = purpose_len + units_len + 10; params_len = (png_size_tp)png_malloc(png_ptr, - (png_alloc_size_t)(nparams * png_sizeof(png_size_t))); + (png_alloc_size_t)(nparams * (sizeof (png_size_t)))); /* Find the length of each parameter, making sure we don't count the * null terminator for the last parameter. */ for (i = 0; i < nparams; i++) { - params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); + params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1); png_debug2(3, "pCAL parameter %d length = %lu", i, (unsigned long)params_len[i]); total_len += params_len[i]; @@ -1895,7 +1841,7 @@ png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, png_debug1(3, "pCAL total length = %d", (int)total_len); png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len); - png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose, purpose_len); + png_write_chunk_data(png_ptr, new_purpose, purpose_len); png_save_int_32(buf, X0); png_save_int_32(buf + 4, X1); buf[8] = (png_byte)type; @@ -1903,8 +1849,6 @@ png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, png_write_chunk_data(png_ptr, buf, (png_size_t)10); png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len); - png_free(png_ptr, new_purpose); - for (i = 0; i < nparams; i++) { png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]); @@ -1918,7 +1862,7 @@ png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, #ifdef PNG_WRITE_sCAL_SUPPORTED /* Write the sCAL chunk */ void /* PRIVATE */ -png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, +png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width, png_const_charp height) { png_byte buf[64]; @@ -1926,8 +1870,8 @@ png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, png_debug(1, "in png_write_sCAL_s"); - wlen = png_strlen(width); - hlen = png_strlen(height); + wlen = strlen(width); + hlen = strlen(height); total_len = wlen + hlen + 2; if (total_len > 64) @@ -1937,8 +1881,8 @@ png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, } buf[0] = (png_byte)unit; - png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ - png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ + memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ + memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len); @@ -1948,7 +1892,7 @@ png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, #ifdef PNG_WRITE_pHYs_SUPPORTED /* Write the pHYs chunk */ void /* PRIVATE */ -png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, +png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, int unit_type) { @@ -1972,7 +1916,7 @@ png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, * or png_convert_from_time_t(), or fill in the structure yourself. */ void /* PRIVATE */ -png_write_tIME(png_structp png_ptr, png_const_timep mod_time) +png_write_tIME(png_structrp png_ptr, png_const_timep mod_time) { png_byte buf[7]; @@ -1999,7 +1943,7 @@ png_write_tIME(png_structp png_ptr, png_const_timep mod_time) /* Initializes the row writing capability of libpng */ void /* PRIVATE */ -png_write_start_row(png_structp png_ptr) +png_write_start_row(png_structrp png_ptr) { #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ @@ -2020,6 +1964,10 @@ png_write_start_row(png_structp png_ptr) png_alloc_size_t buf_size; int usr_pixel_depth; +#ifdef PNG_WRITE_FILTER_SUPPORTED + png_byte filters; +#endif + png_debug(1, "in png_write_start_row"); usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth; @@ -2030,56 +1978,61 @@ png_write_start_row(png_structp png_ptr) png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth; /* Set up row buffer */ - png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size); + png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; #ifdef PNG_WRITE_FILTER_SUPPORTED - /* Set up filtering buffer, if using this filter */ - if (png_ptr->do_filter & PNG_FILTER_SUB) - { - png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1); + filters = png_ptr->do_filter; - png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; + if (png_ptr->height == 1) + filters &= 0xff & ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); + + if (png_ptr->width == 1) + filters &= 0xff & ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); + + if (filters == 0) + filters = PNG_FILTER_NONE; + + png_ptr->do_filter = filters; + + if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG | + PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL) + { + int num_filters = 0; + + png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); + + if (filters & PNG_FILTER_SUB) + num_filters++; + + if (filters & PNG_FILTER_UP) + num_filters++; + + if (filters & PNG_FILTER_AVG) + num_filters++; + + if (filters & PNG_FILTER_PAETH) + num_filters++; + + if (num_filters > 1) + png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr, + buf_size)); } - /* We only need to keep the previous row if we are using one of these. */ - if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) - { - /* Set up previous row buffer */ - png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size); - - if (png_ptr->do_filter & PNG_FILTER_UP) - { - png_ptr->up_row = (png_bytep)png_malloc(png_ptr, - png_ptr->rowbytes + 1); - - png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; - } - - if (png_ptr->do_filter & PNG_FILTER_AVG) - { - png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, - png_ptr->rowbytes + 1); - - png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; - } - - if (png_ptr->do_filter & PNG_FILTER_PAETH) - { - png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, - png_ptr->rowbytes + 1); - - png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; - } - } -#endif /* PNG_WRITE_FILTER_SUPPORTED */ + /* We only need to keep the previous row if we are using one of the following + * filters. + */ + if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0) + png_ptr->prev_row = png_voidcast(png_bytep, + png_calloc(png_ptr, buf_size)); +#endif /* WRITE_FILTER */ #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced, we need to set up width and height of pass */ - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) { - if (!(png_ptr->transformations & PNG_INTERLACE)) + if ((png_ptr->transformations & PNG_INTERLACE) == 0) { png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - png_pass_ystart[0]) / png_pass_yinc[0]; @@ -2101,15 +2054,11 @@ png_write_start_row(png_structp png_ptr) png_ptr->num_rows = png_ptr->height; png_ptr->usr_width = png_ptr->width; } - - png_zlib_claim(png_ptr, PNG_ZLIB_FOR_IDAT); - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream.next_out = png_ptr->zbuf; } /* Internal use only. Called when finished processing a row of data. */ void /* PRIVATE */ -png_write_finish_row(png_structp png_ptr) +png_write_finish_row(png_structrp png_ptr) { #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ @@ -2127,8 +2076,6 @@ png_write_finish_row(png_structp png_ptr) static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; #endif - int ret; - png_debug(1, "in png_write_finish_row"); /* Next row */ @@ -2140,10 +2087,10 @@ png_write_finish_row(png_structp png_ptr) #ifdef PNG_WRITE_INTERLACING_SUPPORTED /* If interlaced, go to next pass */ - if (png_ptr->interlaced) + if (png_ptr->interlaced != 0) { png_ptr->row_number = 0; - if (png_ptr->transformations & PNG_INTERLACE) + if ((png_ptr->transformations & PNG_INTERLACE) != 0) { png_ptr->pass++; } @@ -2168,7 +2115,7 @@ png_write_finish_row(png_structp png_ptr) png_pass_ystart[png_ptr->pass]) / png_pass_yinc[png_ptr->pass]; - if (png_ptr->transformations & PNG_INTERLACE) + if ((png_ptr->transformations & PNG_INTERLACE) != 0) break; } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); @@ -2179,7 +2126,7 @@ png_write_finish_row(png_structp png_ptr) if (png_ptr->pass < 7) { if (png_ptr->prev_row != NULL) - png_memset(png_ptr->prev_row, 0, + memset(png_ptr->prev_row, 0, (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* png_ptr->usr_bit_depth, png_ptr->width)) + 1); @@ -2190,42 +2137,7 @@ png_write_finish_row(png_structp png_ptr) /* If we get here, we've just written the last row, so we need to flush the compressor */ - do - { - /* Tell the compressor we are done */ - ret = deflate(&png_ptr->zstream, Z_FINISH); - - /* Check for an error */ - if (ret == Z_OK) - { - /* Check to see if we need more room */ - if (!(png_ptr->zstream.avail_out)) - { - png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - png_ptr->zstream.next_out = png_ptr->zbuf; - png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; - } - } - - else if (ret != Z_STREAM_END) - { - if (png_ptr->zstream.msg != NULL) - png_error(png_ptr, png_ptr->zstream.msg); - - else - png_error(png_ptr, "zlib error"); - } - } while (ret != Z_STREAM_END); - - /* Write any extra space */ - if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) - { - png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - - png_ptr->zstream.avail_out); - } - - png_zlib_release(png_ptr); - png_ptr->zstream.data_type = Z_BINARY; + png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH); } #ifdef PNG_WRITE_INTERLACING_SUPPORTED @@ -2259,7 +2171,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) { png_bytep sp; png_bytep dp; - int shift; + unsigned int shift; int d; int value; png_uint_32 i; @@ -2297,7 +2209,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) { png_bytep sp; png_bytep dp; - int shift; + unsigned int shift; int d; int value; png_uint_32 i; @@ -2334,7 +2246,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) { png_bytep sp; png_bytep dp; - int shift; + unsigned int shift; int d; int value; png_uint_32 i; @@ -2389,7 +2301,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) /* Move the pixel */ if (dp != sp) - png_memcpy(dp, sp, pixel_bytes); + memcpy(dp, sp, pixel_bytes); /* Next pixel */ dp += pixel_bytes; @@ -2409,49 +2321,181 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) } #endif + /* This filters the row, chooses which filter to use, if it has not already * been specified by the application, and then writes the row out with the * chosen filter. */ -static void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row, +static void /* PRIVATE */ +png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, png_size_t row_bytes); -#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) -#define PNG_HISHIFT 10 -#define PNG_LOMASK ((png_uint_32)0xffffL) -#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) -void /* PRIVATE */ -png_write_find_filter(png_structp png_ptr, png_row_infop row_info) -{ - png_bytep best_row; #ifdef PNG_WRITE_FILTER_SUPPORTED - png_bytep prev_row, row_buf; - png_uint_32 mins, bpp; - png_byte filter_to_do = png_ptr->do_filter; - png_size_t row_bytes = row_info->rowbytes; -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - int num_p_filters = png_ptr->num_prev_filters; +static png_size_t /* PRIVATE */ +png_setup_sub_row(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes, const png_size_t lmins) +{ + png_bytep rp, dp, lp; + png_size_t i; + png_size_t sum = 0; + int v; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp; + i++, rp++, dp++) + { + v = *dp = *rp; + sum += (v < 128) ? v : 256 - v; + } + + for (lp = png_ptr->row_buf + 1; i < row_bytes; + i++, rp++, lp++, dp++) + { + v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + + return (sum); +} + +static png_size_t /* PRIVATE */ +png_setup_up_row(png_structrp png_ptr, const png_size_t row_bytes, + const png_size_t lmins) +{ + png_bytep rp, dp, pp; + png_size_t i; + png_size_t sum = 0; + int v; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < row_bytes; + i++, rp++, pp++, dp++) + { + v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + + return (sum); +} + +static png_size_t /* PRIVATE */ +png_setup_avg_row(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes, const png_size_t lmins) +{ + png_bytep rp, dp, pp, lp; + png_uint_32 i; + png_size_t sum = 0; + int v; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < bpp; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); + + sum += (v < 128) ? v : 256 - v; + } + + for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) + & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + + return (sum); +} + +static png_size_t /* PRIVATE */ +png_setup_paeth_row(png_structrp png_ptr, const png_uint_32 bpp, + const png_size_t row_bytes, const png_size_t lmins) +{ + png_bytep rp, dp, pp, cp, lp; + png_size_t i; + png_size_t sum = 0; + int v; + + png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; + + for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, + pp = png_ptr->prev_row + 1; i < bpp; i++) + { + v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); + + sum += (v < 128) ? v : 256 - v; + } + + for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; + i++) + { + int a, b, c, pa, pb, pc, p; + + b = *pp++; + c = *cp++; + a = *lp++; + + p = b - c; + pc = a - c; + +#ifdef PNG_USE_ABS + pa = abs(p); + pb = abs(pc); + pc = abs(p + pc); +#else + pa = p < 0 ? -p : p; + pb = pc < 0 ? -pc : pc; + pc = (p + pc) < 0 ? -(p + pc) : p + pc; #endif + p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; + + v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); + + sum += (v < 128) ? v : 256 - v; + + if (sum > lmins) /* We are already worse, don't continue. */ + break; + } + + return (sum); +} +#endif /* WRITE_FILTER */ + +void /* PRIVATE */ +png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) +{ +#ifndef PNG_WRITE_FILTER_SUPPORTED + png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1); +#else + png_byte filter_to_do = png_ptr->do_filter; + png_bytep row_buf; + png_bytep best_row; + png_uint_32 bpp; + png_size_t mins; + png_size_t row_bytes = row_info->rowbytes; + png_debug(1, "in png_write_find_filter"); -#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS) - { - /* These will never be selected so we need not test them. */ - filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH); - } -#endif - /* Find out how many bytes offset each pixel is */ bpp = (row_info->pixel_depth + 7) >> 3; - prev_row = png_ptr->prev_row; -#endif - best_row = png_ptr->row_buf; -#ifdef PNG_WRITE_FILTER_SUPPORTED - row_buf = best_row; - mins = PNG_MAXSUM; + row_buf = png_ptr->row_buf; + mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the + running sum */; /* The prediction method we use is to find which method provides the * smallest value when summing the absolute values of the distances @@ -2481,57 +2525,37 @@ png_write_find_filter(png_structp png_ptr, png_row_infop row_info) /* We don't need to test the 'no filter' case if this is the only filter * that has been chosen, as it doesn't actually do anything to the data. */ - if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE) + best_row = png_ptr->row_buf; + + + if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE) { png_bytep rp; - png_uint_32 sum = 0; + png_size_t sum = 0; png_size_t i; int v; - for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) + if (PNG_SIZE_MAX/128 <= row_bytes) { - v = *rp; - sum += (v < 128) ? v : 256 - v; - } - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - png_uint_32 sumhi, sumlo; - int j; - sumlo = sum & PNG_LOMASK; - sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ - - /* Reduce the sum if we match any of the previous rows */ - for (j = 0; j < num_p_filters; j++) + for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) - { - sumlo = (sumlo * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; + /* Check for overflow */ + if (sum > PNG_SIZE_MAX/128 - 256) + break; - sumhi = (sumhi * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } + v = *rp; + sum += (v < 128) ? v : 256 - v; } - - /* Factor in the cost of this filter (this is here for completeness, - * but it makes no sense to have a "cost" for the NONE filter, as - * it has the minimum possible computational cost - none). - */ - sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> - PNG_COST_SHIFT; - - sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> - PNG_COST_SHIFT; - - if (sumhi > PNG_HIMASK) - sum = PNG_MAXSUM; - - else - sum = (sumhi << PNG_HISHIFT) + sumlo; } -#endif + else /* Overflow is not possible */ + { + for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) + { + v = *rp; + sum += (v < 128) ? v : 256 - v; + } + } + mins = sum; } @@ -2539,620 +2563,125 @@ png_write_find_filter(png_structp png_ptr, png_row_infop row_info) if (filter_to_do == PNG_FILTER_SUB) /* It's the only filter so no testing is needed */ { - png_bytep rp, lp, dp; - png_size_t i; - - for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; - i++, rp++, dp++) - { - *dp = *rp; - } - - for (lp = row_buf + 1; i < row_bytes; - i++, rp++, lp++, dp++) - { - *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); - } - - best_row = png_ptr->sub_row; + (void) png_setup_sub_row(png_ptr, bpp, row_bytes, mins); + best_row = png_ptr->try_row; } - else if (filter_to_do & PNG_FILTER_SUB) + else if ((filter_to_do & PNG_FILTER_SUB) != 0) { - png_bytep rp, dp, lp; - png_uint_32 sum = 0, lmins = mins; - png_size_t i; - int v; + png_size_t sum; + png_size_t lmins = mins; -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - /* We temporarily increase the "minimum sum" by the factor we - * would reduce the sum of this filter, so that we can do the - * early exit comparison without scaling the sum each time. - */ - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 lmhi, lmlo; - lmlo = lmins & PNG_LOMASK; - lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) - { - lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> - PNG_COST_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> - PNG_COST_SHIFT; - - if (lmhi > PNG_HIMASK) - lmins = PNG_MAXSUM; - - else - lmins = (lmhi << PNG_HISHIFT) + lmlo; - } -#endif - - for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; - i++, rp++, dp++) - { - v = *dp = *rp; - - sum += (v < 128) ? v : 256 - v; - } - - for (lp = row_buf + 1; i < row_bytes; - i++, rp++, lp++, dp++) - { - v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); - - sum += (v < 128) ? v : 256 - v; - - if (sum > lmins) /* We are already worse, don't continue. */ - break; - } - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 sumhi, sumlo; - sumlo = sum & PNG_LOMASK; - sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) - { - sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> - PNG_COST_SHIFT; - - sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> - PNG_COST_SHIFT; - - if (sumhi > PNG_HIMASK) - sum = PNG_MAXSUM; - - else - sum = (sumhi << PNG_HISHIFT) + sumlo; - } -#endif + sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins); if (sum < mins) { mins = sum; - best_row = png_ptr->sub_row; + best_row = png_ptr->try_row; + if (png_ptr->tst_row != NULL) + { + png_ptr->try_row = png_ptr->tst_row; + png_ptr->tst_row = best_row; + } } } /* Up filter */ if (filter_to_do == PNG_FILTER_UP) { - png_bytep rp, dp, pp; - png_size_t i; - - for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, - pp = prev_row + 1; i < row_bytes; - i++, rp++, pp++, dp++) - { - *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); - } - - best_row = png_ptr->up_row; + (void) png_setup_up_row(png_ptr, row_bytes, mins); + best_row = png_ptr->try_row; } - else if (filter_to_do & PNG_FILTER_UP) + else if ((filter_to_do & PNG_FILTER_UP) != 0) { - png_bytep rp, dp, pp; - png_uint_32 sum = 0, lmins = mins; - png_size_t i; - int v; + png_size_t sum; + png_size_t lmins = mins; - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 lmhi, lmlo; - lmlo = lmins & PNG_LOMASK; - lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) - { - lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> - PNG_COST_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> - PNG_COST_SHIFT; - - if (lmhi > PNG_HIMASK) - lmins = PNG_MAXSUM; - - else - lmins = (lmhi << PNG_HISHIFT) + lmlo; - } -#endif - - for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, - pp = prev_row + 1; i < row_bytes; i++) - { - v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); - - sum += (v < 128) ? v : 256 - v; - - if (sum > lmins) /* We are already worse, don't continue. */ - break; - } - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 sumhi, sumlo; - sumlo = sum & PNG_LOMASK; - sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) - { - sumlo = (sumlo * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - sumhi = (sumhi * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> - PNG_COST_SHIFT; - - sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> - PNG_COST_SHIFT; - - if (sumhi > PNG_HIMASK) - sum = PNG_MAXSUM; - - else - sum = (sumhi << PNG_HISHIFT) + sumlo; - } -#endif + sum = png_setup_up_row(png_ptr, row_bytes, lmins); if (sum < mins) { mins = sum; - best_row = png_ptr->up_row; + best_row = png_ptr->try_row; + if (png_ptr->tst_row != NULL) + { + png_ptr->try_row = png_ptr->tst_row; + png_ptr->tst_row = best_row; + } } } /* Avg filter */ if (filter_to_do == PNG_FILTER_AVG) { - png_bytep rp, dp, pp, lp; - png_uint_32 i; - - for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, - pp = prev_row + 1; i < bpp; i++) - { - *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); - } - - for (lp = row_buf + 1; i < row_bytes; i++) - { - *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) - & 0xff); - } - best_row = png_ptr->avg_row; + (void) png_setup_avg_row(png_ptr, bpp, row_bytes, mins); + best_row = png_ptr->try_row; } - else if (filter_to_do & PNG_FILTER_AVG) + else if ((filter_to_do & PNG_FILTER_AVG) != 0) { - png_bytep rp, dp, pp, lp; - png_uint_32 sum = 0, lmins = mins; - png_size_t i; - int v; + png_size_t sum; + png_size_t lmins = mins; -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 lmhi, lmlo; - lmlo = lmins & PNG_LOMASK; - lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) - { - lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> - PNG_COST_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> - PNG_COST_SHIFT; - - if (lmhi > PNG_HIMASK) - lmins = PNG_MAXSUM; - - else - lmins = (lmhi << PNG_HISHIFT) + lmlo; - } -#endif - - for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, - pp = prev_row + 1; i < bpp; i++) - { - v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); - - sum += (v < 128) ? v : 256 - v; - } - - for (lp = row_buf + 1; i < row_bytes; i++) - { - v = *dp++ = - (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); - - sum += (v < 128) ? v : 256 - v; - - if (sum > lmins) /* We are already worse, don't continue. */ - break; - } - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 sumhi, sumlo; - sumlo = sum & PNG_LOMASK; - sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) - { - sumlo = (sumlo * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - sumhi = (sumhi * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> - PNG_COST_SHIFT; - - sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> - PNG_COST_SHIFT; - - if (sumhi > PNG_HIMASK) - sum = PNG_MAXSUM; - - else - sum = (sumhi << PNG_HISHIFT) + sumlo; - } -#endif + sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins); if (sum < mins) { mins = sum; - best_row = png_ptr->avg_row; + best_row = png_ptr->try_row; + if (png_ptr->tst_row != NULL) + { + png_ptr->try_row = png_ptr->tst_row; + png_ptr->tst_row = best_row; + } } } /* Paeth filter */ - if (filter_to_do == PNG_FILTER_PAETH) + if ((filter_to_do == PNG_FILTER_PAETH) != 0) { - png_bytep rp, dp, pp, cp, lp; - png_size_t i; - - for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, - pp = prev_row + 1; i < bpp; i++) - { - *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); - } - - for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) - { - int a, b, c, pa, pb, pc, p; - - b = *pp++; - c = *cp++; - a = *lp++; - - p = b - c; - pc = a - c; - -#ifdef PNG_USE_ABS - pa = abs(p); - pb = abs(pc); - pc = abs(p + pc); -#else - pa = p < 0 ? -p : p; - pb = pc < 0 ? -pc : pc; - pc = (p + pc) < 0 ? -(p + pc) : p + pc; -#endif - - p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; - - *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); - } - best_row = png_ptr->paeth_row; + (void) png_setup_paeth_row(png_ptr, bpp, row_bytes, mins); + best_row = png_ptr->try_row; } - else if (filter_to_do & PNG_FILTER_PAETH) + else if ((filter_to_do & PNG_FILTER_PAETH) != 0) { - png_bytep rp, dp, pp, cp, lp; - png_uint_32 sum = 0, lmins = mins; - png_size_t i; - int v; + png_size_t sum; + png_size_t lmins = mins; -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 lmhi, lmlo; - lmlo = lmins & PNG_LOMASK; - lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) - { - lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> - PNG_COST_SHIFT; - - lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> - PNG_COST_SHIFT; - - if (lmhi > PNG_HIMASK) - lmins = PNG_MAXSUM; - - else - lmins = (lmhi << PNG_HISHIFT) + lmlo; - } -#endif - - for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, - pp = prev_row + 1; i < bpp; i++) - { - v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); - - sum += (v < 128) ? v : 256 - v; - } - - for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) - { - int a, b, c, pa, pb, pc, p; - - b = *pp++; - c = *cp++; - a = *lp++; - -#ifndef PNG_SLOW_PAETH - p = b - c; - pc = a - c; -#ifdef PNG_USE_ABS - pa = abs(p); - pb = abs(pc); - pc = abs(p + pc); -#else - pa = p < 0 ? -p : p; - pb = pc < 0 ? -pc : pc; - pc = (p + pc) < 0 ? -(p + pc) : p + pc; -#endif - p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; -#else /* PNG_SLOW_PAETH */ - p = a + b - c; - pa = abs(p - a); - pb = abs(p - b); - pc = abs(p - c); - - if (pa <= pb && pa <= pc) - p = a; - - else if (pb <= pc) - p = b; - - else - p = c; -#endif /* PNG_SLOW_PAETH */ - - v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); - - sum += (v < 128) ? v : 256 - v; - - if (sum > lmins) /* We are already worse, don't continue. */ - break; - } - -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) - { - int j; - png_uint_32 sumhi, sumlo; - sumlo = sum & PNG_LOMASK; - sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; - - for (j = 0; j < num_p_filters; j++) - { - if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) - { - sumlo = (sumlo * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - - sumhi = (sumhi * png_ptr->filter_weights[j]) >> - PNG_WEIGHT_SHIFT; - } - } - - sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> - PNG_COST_SHIFT; - - sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> - PNG_COST_SHIFT; - - if (sumhi > PNG_HIMASK) - sum = PNG_MAXSUM; - - else - sum = (sumhi << PNG_HISHIFT) + sumlo; - } -#endif + sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins); if (sum < mins) { - best_row = png_ptr->paeth_row; + best_row = png_ptr->try_row; + if (png_ptr->tst_row != NULL) + { + png_ptr->try_row = png_ptr->tst_row; + png_ptr->tst_row = best_row; + } } } -#endif /* PNG_WRITE_FILTER_SUPPORTED */ /* Do the actual writing of the filtered row data from the chosen filter. */ png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1); -#ifdef PNG_WRITE_FILTER_SUPPORTED -#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED - /* Save the type of filter we picked this time for future calculations */ - if (png_ptr->num_prev_filters > 0) - { - int j; - - for (j = 1; j < num_p_filters; j++) - { - png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; - } - - png_ptr->prev_filters[j] = best_row[0]; - } -#endif -#endif /* PNG_WRITE_FILTER_SUPPORTED */ +#endif /* WRITE_FILTER */ } /* Do the actual writing of a previously filtered row. */ static void -png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row, - png_size_t avail/*includes filter byte*/) +png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, + png_size_t full_row_length/*includes filter byte*/) { png_debug(1, "in png_write_filtered_row"); png_debug1(2, "filter = %d", filtered_row[0]); - /* Set up the zlib input buffer */ - png_ptr->zstream.next_in = filtered_row; - png_ptr->zstream.avail_in = 0; - /* Repeat until we have compressed all the data */ - do - { - int ret; /* Return of zlib */ - - /* Record the number of bytes available - zlib supports at least 65535 - * bytes at one step, depending on the size of the zlib type 'uInt', the - * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h). - * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e. - * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a - * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called - * with smaller chunks of data. - */ - if (png_ptr->zstream.avail_in == 0) - { - if (avail > ZLIB_IO_MAX) - { - png_ptr->zstream.avail_in = ZLIB_IO_MAX; - avail -= ZLIB_IO_MAX; - } - - else - { - /* So this will fit in the available uInt space: */ - png_ptr->zstream.avail_in = (uInt)avail; - avail = 0; - } - } - - /* Compress the data */ - ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); - - /* Check for compression errors */ - if (ret != Z_OK) - { - if (png_ptr->zstream.msg != NULL) - png_error(png_ptr, png_ptr->zstream.msg); - - else - png_error(png_ptr, "zlib error"); - } - - /* See if it is time to write another IDAT */ - if (!(png_ptr->zstream.avail_out)) - { - /* Write the IDAT and reset the zlib output buffer */ - png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - } - /* Repeat until all data has been compressed */ - } while (avail > 0 || png_ptr->zstream.avail_in > 0); + png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH); +#ifdef PNG_WRITE_FILTER_SUPPORTED /* Swap the current and previous rows */ if (png_ptr->prev_row != NULL) { @@ -3162,6 +2691,7 @@ png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row, png_ptr->prev_row = png_ptr->row_buf; png_ptr->row_buf = tptr; } +#endif /* WRITE_FILTER */ /* Finish row - updates counters and flushes zlib if last row */ png_write_finish_row(png_ptr); @@ -3174,6 +2704,6 @@ png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row, { png_write_flush(png_ptr); } -#endif +#endif /* WRITE_FLUSH */ } -#endif /* PNG_WRITE_SUPPORTED */ +#endif /* WRITE */ diff --git a/3rdparty/libwebp/CMakeLists.txt b/3rdparty/libwebp/CMakeLists.txt index f3c458fe8..e9ac92a2c 100644 --- a/3rdparty/libwebp/CMakeLists.txt +++ b/3rdparty/libwebp/CMakeLists.txt @@ -10,6 +10,7 @@ ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/cpu-features") file(GLOB lib_srcs dec/*.c dsp/*.c enc/*.c mux/*.c utils/*.c webp/*.c) file(GLOB lib_hdrs dec/*.h dsp/*.h enc/*.h mux/*.h utils/*.h webp/*.h) +# FIXIT if(ANDROID AND ARMEABI_V7A AND NOT NEON) foreach(file ${lib_srcs}) if("${file}" MATCHES "_neon.c") diff --git a/3rdparty/readme.txt b/3rdparty/readme.txt index b06700902..2d6d38260 100644 --- a/3rdparty/readme.txt +++ b/3rdparty/readme.txt @@ -14,7 +14,7 @@ libjpeg The Independent JPEG Group's JPEG software. WITH_JPEG CMake option must be ON to add libjpeg support to imgcodecs. ------------------------------------------------------------------------------------ libpng Portable Network Graphics library. - Copyright (c) 2004, 2006-2012 Glenn Randers-Pehrson. + The license and copyright notes can be found in libpng/LICENSE. See libpng home page http://www.libpng.org for details and links to the source code diff --git a/3rdparty/tbb/CMakeLists.txt b/3rdparty/tbb/CMakeLists.txt index 93d943523..a76854d4a 100644 --- a/3rdparty/tbb/CMakeLists.txt +++ b/3rdparty/tbb/CMakeLists.txt @@ -5,21 +5,26 @@ if (WIN32 AND NOT ARM) message(FATAL_ERROR "BUILD_TBB option supports Windows on ARM only!\nUse regular official TBB build instead of the BUILD_TBB option!") endif() -if (WIN32 AND ARM) - # 4.1 update 4 - The first release that supports Windows RT. Hangs on some Android devices - set(tbb_ver "tbb41_20130613oss") - set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130613oss_src.tgz") - set(tbb_md5 "108c8c1e481b0aaea61878289eb28b6a") - set(tbb_version_file "version_string.ver") - ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4702) -else() - # 4.1 update 2 - works fine - set(tbb_ver "tbb41_20130116oss") - set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130116oss_src.tgz") - set(tbb_md5 "3809790e1001a1b32d59c9fee590ee85") - set(tbb_version_file "version_string.ver") - ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) -endif() +set(tbb_ver "tbb43_20141204oss") +set(tbb_url "http://www.threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb43_20141204oss_src.tgz") +set(tbb_md5 "e903dd92d9433701f097fa7ca29a3c1f") +set(tbb_version_file "version_string.ver") +ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4702) +ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) + +# 4.1 update 4 - The first release that supports Windows RT. Hangs on some Android devices +#set(tbb_ver "tbb41_20130613oss") +#set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130613oss_src.tgz") +#set(tbb_md5 "108c8c1e481b0aaea61878289eb28b6a") +#set(tbb_version_file "version_string.ver") +#ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4702) + +# 4.1 update 2 - works fine +#set(tbb_ver "tbb41_20130116oss") +#set(tbb_url "http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130116oss_src.tgz") +#set(tbb_md5 "3809790e1001a1b32d59c9fee590ee85") +#set(tbb_version_file "version_string.ver") +#ocv_warnings_disable(CMAKE_CXX_FLAGS -Wshadow) # 4.1 update 3 dev - Hangs on some Android devices #set(tbb_ver "tbb41_20130401oss") @@ -97,7 +102,7 @@ if(NOT EXISTS "${tbb_tarball}") message(STATUS "Downloading ${tbb_ver}_src.tgz") file(DOWNLOAD "${tbb_url}" "${tbb_tarball}" TIMEOUT 600 STATUS __statvar) if(NOT __statvar EQUAL 0) - message(FATAL_ERROR "Failed to download TBB sources: ${tbb_url}") + message(FATAL_ERROR "Failed to download TBB sources (${__statvar}): ${tbb_url}") endif() file(MD5 "${tbb_tarball}" tbb_local_md5) if(NOT tbb_local_md5 STREQUAL tbb_md5) @@ -153,6 +158,7 @@ if (WIN32) set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} /APPCONTAINER") else() add_definitions(-D__TBB_DYNAMIC_LOAD_ENABLED=0 #required + -D__TBB_WEAK_SYMBOLS_PRESENT=0 #required for 4.3 -D__TBB_BUILD=1 #required -D__TBB_SURVIVE_THREAD_SWITCH=0 #no cilk support -DTBB_USE_DEBUG=0 #just to be sure diff --git a/3rdparty/zlib/gzread.c b/3rdparty/zlib/gzread.c index bf4538eb2..68ad01d99 100644 --- a/3rdparty/zlib/gzread.c +++ b/3rdparty/zlib/gzread.c @@ -27,7 +27,7 @@ local int gz_load(state, buf, len, have) *have = 0; do { - ret = read(state->fd, buf + *have, len - *have); + ret = (int) read(state->fd, buf + *have, len - *have); if (ret <= 0) break; *have += ret; diff --git a/3rdparty/zlib/gzwrite.c b/3rdparty/zlib/gzwrite.c index aa767fbf6..e9525417c 100644 --- a/3rdparty/zlib/gzwrite.c +++ b/3rdparty/zlib/gzwrite.c @@ -81,7 +81,7 @@ local int gz_comp(state, flush) /* write directly if requested */ if (state->direct) { - got = write(state->fd, strm->next_in, strm->avail_in); + got = (int) write(state->fd, strm->next_in, strm->avail_in); if (got < 0 || (unsigned)got != strm->avail_in) { gz_error(state, Z_ERRNO, zstrerror()); return -1; @@ -98,7 +98,7 @@ local int gz_comp(state, flush) if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) { have = (unsigned)(strm->next_out - state->x.next); - if (have && ((got = write(state->fd, state->x.next, have)) < 0 || + if (have && ((got = (int) write(state->fd, state->x.next, have)) < 0 || (unsigned)got != have)) { gz_error(state, Z_ERRNO, zstrerror()); return -1; diff --git a/CMakeLists.txt b/CMakeLists.txt index deed0a648..0065507b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,10 @@ if(WINRT) add_definitions(-DWINRT_STORE) endif() - if(CMAKE_SYSTEM_VERSION MATCHES 8.1) + if(CMAKE_SYSTEM_VERSION MATCHES 10) + set(WINRT_10 TRUE) + add_definitions(-DWINRT_10) + elseif(CMAKE_SYSTEM_VERSION MATCHES 8.1) set(WINRT_8_1 TRUE) add_definitions(-DWINRT_8_1) elseif(CMAKE_SYSTEM_VERSION MATCHES 8.0) @@ -95,6 +98,8 @@ endif() include(cmake/OpenCVUtils.cmake) +ocv_cmake_eval(DEBUG_PRE ONCE) + ocv_clear_vars(OpenCVModules_TARGETS) # ---------------------------------------------------------------------------- @@ -169,7 +174,7 @@ OCV_OPTION(WITH_NVCUVID "Include NVidia Video Decoding library support" OCV_OPTION(WITH_EIGEN "Include Eigen2/Eigen3 support" ON IF (NOT WINRT) ) OCV_OPTION(WITH_VFW "Include Video for Windows support" ON IF WIN32 ) OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" ON IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) -OCV_OPTION(WITH_GSTREAMER "Include Gstreamer support" ON IF (UNIX AND NOT ANDROID) ) +OCV_OPTION(WITH_GSTREAMER "Include Gstreamer support" ON IF (NOT ANDROID) ) OCV_OPTION(WITH_GSTREAMER_0_10 "Enable Gstreamer 0.10 support (instead of 1.x)" OFF ) OCV_OPTION(WITH_GTK "Include GTK support" ON IF (UNIX AND NOT APPLE AND NOT ANDROID) ) OCV_OPTION(WITH_GTK_2_X "Use GTK version 2" OFF IF (UNIX AND NOT APPLE AND NOT ANDROID) ) @@ -182,8 +187,8 @@ OCV_OPTION(WITH_OPENGL "Include OpenGL support" OFF OCV_OPTION(WITH_OPENNI "Include OpenNI support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_OPENNI2 "Include OpenNI2 support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_PNG "Include PNG support" ON) -OCV_OPTION(WITH_PVAPI "Include Prosilica GigE support" ON IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) -OCV_OPTION(WITH_GIGEAPI "Include Smartek GigE support" ON IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) +OCV_OPTION(WITH_PVAPI "Include Prosilica GigE support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) +OCV_OPTION(WITH_GIGEAPI "Include Smartek GigE support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_QT "Build with Qt Backend support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) OCV_OPTION(WITH_WIN32UI "Build with Win32 UI Backend support" ON IF WIN32 AND NOT WINRT) OCV_OPTION(WITH_QUICKTIME "Use QuickTime for Video I/O insted of QTKit" OFF IF APPLE ) @@ -194,7 +199,7 @@ OCV_OPTION(WITH_PTHREADS_PF "Use pthreads-based parallel_for" ON OCV_OPTION(WITH_TIFF "Include TIFF support" ON IF (NOT IOS) ) OCV_OPTION(WITH_UNICAP "Include Unicap support (GPL)" OFF IF (UNIX AND NOT APPLE AND NOT ANDROID) ) OCV_OPTION(WITH_V4L "Include Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) ) -OCV_OPTION(WITH_LIBV4L "Use libv4l for Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) ) +OCV_OPTION(WITH_LIBV4L "Use libv4l for Video 4 Linux support" OFF IF (UNIX AND NOT ANDROID) ) OCV_OPTION(WITH_DSHOW "Build VideoIO with DirectShow support" ON IF (WIN32 AND NOT ARM AND NOT WINRT) ) OCV_OPTION(WITH_MSMF "Build VideoIO with Media Foundation support" OFF IF WIN32 ) OCV_OPTION(WITH_XIMEA "Include XIMEA cameras support" OFF IF (NOT ANDROID AND NOT WINRT) ) @@ -207,6 +212,7 @@ OCV_OPTION(WITH_OPENCLAMDBLAS "Include AMD OpenCL BLAS library support" ON OCV_OPTION(WITH_DIRECTX "Include DirectX support" ON IF (WIN32 AND NOT WINRT) ) OCV_OPTION(WITH_INTELPERC "Include Intel Perceptual Computing support" OFF IF (WIN32 AND NOT WINRT) ) OCV_OPTION(WITH_IPP_A "Include Intel IPP_A support" OFF IF (MSVC OR X86 OR X86_64) ) +OCV_OPTION(WITH_MATLAB "Include Matlab support" ON IF (NOT ANDROID AND NOT IOS AND NOT WINRT)) OCV_OPTION(WITH_VA "Include VA support" OFF IF (UNIX AND NOT ANDROID) ) OCV_OPTION(WITH_VA_INTEL "Include Intel VA-API/OpenCL support" OFF IF (UNIX AND NOT ANDROID) ) OCV_OPTION(WITH_GDAL "Include GDAL Support" OFF IF (NOT ANDROID AND NOT IOS AND NOT WINRT) ) @@ -214,20 +220,20 @@ OCV_OPTION(WITH_GPHOTO2 "Include gPhoto2 library support" ON # OpenCV build components # =================================================== -OCV_OPTION(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" NOT (ANDROID OR IOS) ) -OCV_OPTION(BUILD_opencv_apps "Build utility applications (used for example to train classifiers)" (NOT ANDROID AND NOT WINRT) IF (NOT IOS) ) +OCV_OPTION(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" NOT (ANDROID OR APPLE_FRAMEWORK) ) +OCV_OPTION(BUILD_opencv_apps "Build utility applications (used for example to train classifiers)" (NOT ANDROID AND NOT WINRT) IF (NOT APPLE_FRAMEWORK) ) OCV_OPTION(BUILD_ANDROID_EXAMPLES "Build examples for Android platform" ON IF ANDROID ) -OCV_OPTION(BUILD_DOCS "Create build rules for OpenCV Documentation" ON IF NOT WINRT) +OCV_OPTION(BUILD_DOCS "Create build rules for OpenCV Documentation" ON IF (NOT WINRT OR APPLE_FRAMEWORK)) OCV_OPTION(BUILD_EXAMPLES "Build all examples" OFF ) OCV_OPTION(BUILD_PACKAGE "Enables 'make package_source' command" ON IF NOT WINRT) -OCV_OPTION(BUILD_PERF_TESTS "Build performance tests" ON IF (NOT IOS) ) -OCV_OPTION(BUILD_TESTS "Build accuracy & regression tests" ON IF (NOT IOS) ) +OCV_OPTION(BUILD_PERF_TESTS "Build performance tests" ON IF (NOT APPLE_FRAMEWORK) ) +OCV_OPTION(BUILD_TESTS "Build accuracy & regression tests" ON IF (NOT APPLE_FRAMEWORK) ) OCV_OPTION(BUILD_WITH_DEBUG_INFO "Include debug info into debug libs (not MSCV only)" ON ) OCV_OPTION(BUILD_WITH_STATIC_CRT "Enables use of staticaly linked CRT for staticaly linked OpenCV" ON IF MSVC ) OCV_OPTION(BUILD_WITH_DYNAMIC_IPP "Enables dynamic linking of IPP (only for standalone IPP)" OFF ) OCV_OPTION(BUILD_FAT_JAVA_LIB "Create fat java wrapper containing the whole OpenCV library" ON IF NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX ) OCV_OPTION(BUILD_ANDROID_SERVICE "Build OpenCV Manager for Google Play" OFF IF ANDROID ) -OCV_OPTION(BUILD_CUDA_STUBS "Build CUDA modules stubs when no CUDA SDK" OFF IF (NOT IOS) ) +OCV_OPTION(BUILD_CUDA_STUBS "Build CUDA modules stubs when no CUDA SDK" OFF IF (NOT APPLE_FRAMEWORK) ) # 3rd party libs OCV_OPTION(BUILD_ZLIB "Build zlib from source" WIN32 OR APPLE ) @@ -244,12 +250,12 @@ OCV_OPTION(INSTALL_CREATE_DISTRIB "Change install rules to build the distribut OCV_OPTION(INSTALL_C_EXAMPLES "Install C examples" OFF ) OCV_OPTION(INSTALL_PYTHON_EXAMPLES "Install Python examples" OFF ) OCV_OPTION(INSTALL_ANDROID_EXAMPLES "Install Android examples" OFF IF ANDROID ) -OCV_OPTION(INSTALL_TO_MANGLED_PATHS "Enables mangled install paths, that help with side by side installs." OFF IF (UNIX AND NOT ANDROID AND NOT IOS AND BUILD_SHARED_LIBS) ) +OCV_OPTION(INSTALL_TO_MANGLED_PATHS "Enables mangled install paths, that help with side by side installs." OFF IF (UNIX AND NOT ANDROID AND NOT APPLE_FRAMEWORK AND BUILD_SHARED_LIBS) ) OCV_OPTION(INSTALL_TESTS "Install accuracy and performance test binaries and test data" OFF) # OpenCV build options # =================================================== -OCV_OPTION(ENABLE_PRECOMPILED_HEADERS "Use precompiled headers" ON IF (NOT IOS) ) +OCV_OPTION(ENABLE_PRECOMPILED_HEADERS "Use precompiled headers" ON IF (NOT IOS AND NOT CMAKE_CROSSCOMPILING) ) OCV_OPTION(ENABLE_SOLUTION_FOLDERS "Solution folder in Visual Studio or in other IDEs" (MSVC_IDE OR CMAKE_GENERATOR MATCHES Xcode) ) OCV_OPTION(ENABLE_PROFILING "Enable profiling in the GCC compiler (Add flags: -g -pg)" OFF IF CMAKE_COMPILER_IS_GNUCXX ) OCV_OPTION(ENABLE_COVERAGE "Enable coverage collection with GCov" OFF IF CMAKE_COMPILER_IS_GNUCXX ) @@ -266,7 +272,7 @@ OCV_OPTION(ENABLE_POPCNT "Enable POPCNT instructions" OCV_OPTION(ENABLE_AVX "Enable AVX instructions" OFF IF ((MSVC OR CMAKE_COMPILER_IS_GNUCXX) AND (X86 OR X86_64)) ) OCV_OPTION(ENABLE_AVX2 "Enable AVX2 instructions" OFF IF ((MSVC OR CMAKE_COMPILER_IS_GNUCXX) AND (X86 OR X86_64)) ) OCV_OPTION(ENABLE_FMA3 "Enable FMA3 instructions" OFF IF ((MSVC OR CMAKE_COMPILER_IS_GNUCXX) AND (X86 OR X86_64)) ) -OCV_OPTION(ENABLE_NEON "Enable NEON instructions" OFF IF CMAKE_COMPILER_IS_GNUCXX AND (ARM OR AARCH64 OR IOS) ) +OCV_OPTION(ENABLE_NEON "Enable NEON instructions" "${NEON}" IF CMAKE_COMPILER_IS_GNUCXX AND (ARM OR AARCH64 OR IOS) ) OCV_OPTION(ENABLE_VFPV3 "Enable VFPv3-D32 instructions" OFF IF CMAKE_COMPILER_IS_GNUCXX AND (ARM OR AARCH64 OR IOS) ) OCV_OPTION(ENABLE_NOISY_WARNINGS "Show all warnings even if they are too noisy" OFF ) OCV_OPTION(OPENCV_WARNINGS_ARE_ERRORS "Treat warnings as errors" OFF ) @@ -274,6 +280,9 @@ OCV_OPTION(ANDROID_EXAMPLES_WITH_LIBS "Build binaries of Android examples with n OCV_OPTION(ENABLE_IMPL_COLLECTION "Collect implementation data on function call" OFF ) OCV_OPTION(GENERATE_ABI_DESCRIPTOR "Generate XML file for abi_compliance_checker tool" OFF IF UNIX) +OCV_OPTION(DOWNLOAD_EXTERNAL_TEST_DATA "Download external test data (Python executable and OPENCV_TEST_DATA_PATH environment variable may be required)" OFF ) + + if(ENABLE_IMPL_COLLECTION) add_definitions(-DCV_COLLECT_IMPL_DATA) endif() @@ -419,7 +428,7 @@ if(DEFINED CMAKE_DEBUG_POSTFIX) set(OPENCV_DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}") endif() -if(INSTALL_CREATE_DISTRIB AND BUILD_SHARED_LIBS AND NOT DEFINED BUILD_opencv_world) +if((INSTALL_CREATE_DISTRIB AND BUILD_SHARED_LIBS AND NOT DEFINED BUILD_opencv_world) OR APPLE_FRAMEWORK) set(BUILD_opencv_world ON CACHE INTERNAL "") endif() @@ -583,14 +592,36 @@ if(WITH_DIRECTX) endif() # --- Matlab/Octave --- -include(cmake/OpenCVFindMatlab.cmake) +if(WITH_MATLAB) + include(cmake/OpenCVFindMatlab.cmake) +endif() include(cmake/OpenCVDetectVTK.cmake) +# -- Custom HAL replacement -- +set(_includes "") +# assuming OPENCV_HAL_HEADERS and OPENCV_HAL_LIBS are lists of files: +# option example: -DOPENCV_HAL_HEADERS="/header1.h;/header2.h" if (OPENCV_HAL_HEADERS AND OPENCV_HAL_LIBS) - get_filename_component(OPENCV_HAL_HEADERS "${OPENCV_HAL_HEADERS}" ABSOLUTE) - get_filename_component(OPENCV_HAL_LIBS "${OPENCV_HAL_LIBS}" ABSOLUTE) + foreach (h ${OPENCV_HAL_HEADERS}) + get_filename_component(h "${h}" ABSOLUTE) + set(_includes "${_includes}\n#include \"${h}\"") + endforeach() + foreach (l ${OPENCV_HAL_LIBS}) + get_filename_component(l "${l}" ABSOLUTE) + set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} ${l}) + # TODO: install? + # ocv_install_target(${l} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev) + endforeach() +else() + set(_includes "// using default HAL") + unset(OPENCV_HAL_HEADERS CACHE) + unset(OPENCV_HAL_LIBS CACHE) endif() +set(OPENCV_HAL_HEADERS "${OPENCV_HAL_HEADERS}" CACHE STRING "Headers with custom HAL implementation") +set(OPENCV_HAL_LIBS "${OPENCV_HAL_LIBS}" CACHE STRING "Libraries with custom HAL implementation") +configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/custom_hal.hpp.in" "${CMAKE_BINARY_DIR}/custom_hal.hpp" @ONLY) +unset(_includes) # ---------------------------------------------------------------------------- # Add CUDA libraries (needed for apps/tools, samples) @@ -603,6 +634,9 @@ if(HAVE_CUDA) if(HAVE_CUFFT) set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} ${CUDA_cufft_LIBRARY}) endif() + foreach(p ${CUDA_LIBS_PATH}) + set(OPENCV_LINKER_LIBS ${OPENCV_LINKER_LIBS} -L${p}) + endforeach() endif() # ---------------------------------------------------------------------------- # Solution folders: @@ -659,11 +693,13 @@ include(cmake/OpenCVGenPkgconfig.cmake) # Generate OpenCV.mk for ndk-build (Android build tool) include(cmake/OpenCVGenAndroidMK.cmake) -# Generate OpenCVСonfig.cmake and OpenCVConfig-version.cmake for cmake projects +# Generate OpenCVConfig.cmake and OpenCVConfig-version.cmake for cmake projects include(cmake/OpenCVGenConfig.cmake) # Generate Info.plist for the IOS framework -include(cmake/OpenCVGenInfoPlist.cmake) +if(APPLE_FRAMEWORK) + include(cmake/OpenCVGenInfoPlist.cmake) +endif() # Generate ABI descriptor include(cmake/OpenCVGenABI.cmake) @@ -719,10 +755,46 @@ status("General configuration for OpenCV ${OPENCV_VERSION} ===================== if(OPENCV_VCSVERSION) status(" Version control:" ${OPENCV_VCSVERSION}) endif() +if(OPENCV_EXTRA_MODULES_PATH AND NOT BUILD_INFO_SKIP_EXTRA_MODULES) + set(__dump_extra_header OFF) + foreach(p ${OPENCV_EXTRA_MODULES_PATH}) + if(EXISTS ${p}) + if(NOT __dump_extra_header) + set(__dump_extra_header ON) + status("") + status(" Extra modules:") + else() + status("") + endif() + set(EXTRA_MODULES_VCSVERSION "unknown") + if(GIT_FOUND) + execute_process(COMMAND "${GIT_EXECUTABLE}" describe --tags --always --dirty --match "[0-9].[0-9].[0-9]*" + WORKING_DIRECTORY "${p}" + OUTPUT_VARIABLE EXTRA_MODULES_VCSVERSION + RESULT_VARIABLE GIT_RESULT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT GIT_RESULT EQUAL 0) + set(EXTRA_MODULES_VCSVERSION "unknown") + endif() + endif() + status(" Location (extra):" ${p}) + status(" Version control (extra):" ${EXTRA_MODULES_VCSVERSION}) + endif() + endforeach() + unset(__dump_extra_header) +endif() # ========================== build platform ========================== status("") status(" Platform:") +if(NOT CMAKE_VERSION VERSION_LESS 2.8.11 AND NOT BUILD_INFO_SKIP_TIMESTAMP) + string(TIMESTAMP TIMESTAMP "" UTC) + if(TIMESTAMP) + status(" Timestamp:" ${TIMESTAMP}) + endif() +endif() status(" Host:" ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_VERSION} ${CMAKE_HOST_SYSTEM_PROCESSOR}) if(CMAKE_CROSSCOMPILING) status(" Target:" ${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION} ${CMAKE_SYSTEM_PROCESSOR}) @@ -829,6 +901,9 @@ status(" Windows RT support:" WINRT THEN YES ELSE NO) status(" Building for Microsoft platform: " ${CMAKE_SYSTEM_NAME}) status(" Building for architectures: " ${CMAKE_VS_EFFECTIVE_PLATFORMS}) status(" Building for version: " ${CMAKE_SYSTEM_VERSION}) + if (DEFINED ENABLE_WINRT_MODE_NATIVE) + status(" Building for C++ without CX extensions") + endif() endif() endif(WIN32) @@ -1085,6 +1160,7 @@ endif(DEFINED WITH_VA_INTEL) status(" Use Eigen:" HAVE_EIGEN THEN "YES (ver ${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION})" ELSE NO) status(" Use Cuda:" HAVE_CUDA THEN "YES (ver ${CUDA_VERSION_STRING})" ELSE NO) status(" Use OpenCL:" HAVE_OPENCL THEN YES ELSE NO) +status(" Use custom HAL:" OPENCV_HAL_HEADERS AND OPENCV_HAL_LIBS THEN "YES (${OPENCV_HAL_HEADERS}; ${OPENCV_HAL_LIBS})" ELSE "NO") if(HAVE_CUDA) status("") @@ -1100,14 +1176,13 @@ endif() if(HAVE_OPENCL) status("") - status(" OpenCL:") if(HAVE_OPENCL_STATIC) - set(__opencl_ver "static") + set(__opencl_type "") else() - set(__opencl_ver "dynamic") + set(__opencl_type "") endif() - status(" Version:" ${__opencl_ver}) - if(OPENCL_INCLUDE_DIR) + status(" OpenCL:" ${__opencl_type}) + if(OPENCL_INCLUDE_DIRS) status(" Include path:" ${OPENCL_INCLUDE_DIRS}) endif() if(OPENCL_LIBRARIES) @@ -1124,7 +1199,7 @@ if(HAVE_OPENCL) list(APPEND __libs "${l}") endif() endforeach() - status(" libraries:" ${__libs}) + status(" Link libraries:" ${__libs}) endif() status(" Use AMDFFT:" HAVE_CLAMDFFT THEN YES ELSE NO) status(" Use AMDBLAS:" HAVE_CLAMDBLAS THEN YES ELSE NO) @@ -1172,10 +1247,12 @@ status(" Java tests:" BUILD_TESTS AND opencv_test_java_BINARY_DIR # ========================= matlab ========================= status("") -status(" Matlab:") -status(" mex:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO) -if (MATLAB_FOUND) - status(" Compiler/generator:" MEX_WORKS THEN "Working" ELSE "Not working (bindings will not be generated)") +if(MATLAB_FOUND) + status(" Matlab:") + status(" mex:" MATLAB_MEX_SCRIPT THEN "${MATLAB_MEX_SCRIPT}" ELSE NO) + status(" Compiler/generator:" MEX_WORKS THEN "Working" ELSE "Not working (bindings will not be generated)") +else() + status(" Matlab:" WITH_MATLAB AND NOT MATLAB_FOUND THEN "Matlab not found or implicitly disabled" ELSE NO) endif() # ========================== documentation ========================== @@ -1215,3 +1292,7 @@ endif() # ---------------------------------------------------------------------------- include(cmake/OpenCVPackaging.cmake) + +# This should be the last command +ocv_cmake_dump_vars("" TOFILE "CMakeVars.txt") +ocv_cmake_eval(DEBUG_POST ONCE) diff --git a/LICENSE b/LICENSE index ab58eebca..fce70d713 100644 --- a/LICENSE +++ b/LICENSE @@ -7,12 +7,12 @@ copy or use the software. For Open Source Computer Vision Library (3-clause BSD License) -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. +Copyright (C) 2000-2016, Intel Corporation, all rights reserved. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. +Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. +Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. +Copyright (C) 2015-2016, Itseez Inc., all rights reserved. Third party copyrights are property of their respective owners. Redistribution and use in source and binary forms, with or without modification, diff --git a/README.md b/README.md index da3c9613c..7dbc82320 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ ### OpenCV: Open Source Computer Vision Library -[![Gittip](http://img.shields.io/gittip/OpenCV.png)](https://www.gittip.com/OpenCV/) - #### Resources * Homepage: diff --git a/apps/traincascade/cascadeclassifier.cpp b/apps/traincascade/cascadeclassifier.cpp index f1ba0d78e..2924fe341 100644 --- a/apps/traincascade/cascadeclassifier.cpp +++ b/apps/traincascade/cascadeclassifier.cpp @@ -206,7 +206,7 @@ bool CvCascadeClassifier::train( const string _cascadeDirName, cout << endl << "===== TRAINING " << i << "-stage =====" << endl; cout << "((int)idx)); - Mat innTilted(winSize.height + 1, winSize.width + 1, tilted.type(), tilted.ptr((int)idx)); Mat innSqSum; - integral(img, innSum, innSqSum, innTilted); + if (((const CvHaarFeatureParams*)featureParams)->mode == CvHaarFeatureParams::ALL) + { + Mat innTilted(winSize.height + 1, winSize.width + 1, tilted.type(), tilted.ptr((int)idx)); + integral(img, innSum, innSqSum, innTilted); + } + else + integral(img, innSum, innSqSum); normfactor.ptr(0)[idx] = calcNormFactor( innSum, innSqSum ); } diff --git a/apps/traincascade/imagestorage.cpp b/apps/traincascade/imagestorage.cpp index fdaa33b6d..4055350c2 100644 --- a/apps/traincascade/imagestorage.cpp +++ b/apps/traincascade/imagestorage.cpp @@ -28,7 +28,7 @@ CvCascadeImageReader::NegReader::NegReader() bool CvCascadeImageReader::NegReader::create( const string _filename, Size _winSize ) { - string dirname, str; + string str; std::ifstream file(_filename.c_str()); if ( !file.is_open() ) return false; diff --git a/cmake/FindGstreamerWindows.cmake b/cmake/FindGstreamerWindows.cmake new file mode 100644 index 000000000..c37fecdf1 --- /dev/null +++ b/cmake/FindGstreamerWindows.cmake @@ -0,0 +1,139 @@ +FILE(TO_CMAKE_PATH "$ENV{GSTREAMER_DIR}" TRY1_DIR) +FILE(TO_CMAKE_PATH "${GSTREAMER_DIR}" TRY2_DIR) +FILE(GLOB GSTREAMER_DIR ${TRY1_DIR} ${TRY2_DIR}) + +FIND_PATH(GSTREAMER_gst_INCLUDE_DIR gst/gst.h + PATHS ${GSTREAMER_DIR}/include/gstreamer-1.0 ${GSTREAMER_DIR}/include /usr/local/include/gstreamer-1.0 /usr/include/gstreamer-1.0 + ENV INCLUDE DOC "Directory containing gst/gst.h include file") + +FIND_PATH(GSTREAMER_glib_INCLUDE_DIR glib.h + PATHS ${GSTREAMER_DIR}/include/glib-2.0/ + ENV INCLUDE DOC "Directory containing glib.h include file") + +FIND_PATH(GSTREAMER_glibconfig_INCLUDE_DIR glibconfig.h + PATHS ${GSTREAMER_DIR}/lib/glib-2.0/include + ENV INCLUDE DOC "Directory containing glibconfig.h include file") + +FIND_PATH(GSTREAMER_gstconfig_INCLUDE_DIR gst/gstconfig.h + PATHS ${GSTREAMER_DIR}/lib/gstreamer-1.0/include ${GSTREAMER_DIR}/include ${GSTREAMER_DIR}/lib/include /usr/local/include/gstreamer-1.0 /usr/include/gstreamer-1.0 /usr/local/lib/include/gstreamer-1.0 /usr/lib/include/gstreamer-1.0 + ENV INCLUDE DOC "Directory containing gst/gstconfig.h include file") + +FIND_LIBRARY(GSTREAMER_gstaudio_LIBRARY NAMES gstaudio libgstaudio-1.0 gstaudio-1.0 + PATHS ${GSTREMAER_DIR}/lib ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstaudio library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstapp_LIBRARY NAMES gstapp libgstapp-1.0 gstapp-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstapp library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstbase_LIBRARY NAMES gstbase libgstbase-1.0 gstbase-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstbase library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GLIB_gstcdda_LIBRARY NAMES gstcdda libgstcdda-1.0 gstcdda-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstcdda library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstcontroller_LIBRARY NAMES gstcontroller libgstcontroller-1.0 gstcontroller-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstcontroller library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + + +FIND_LIBRARY(GSTREAMER_gstnet_LIBRARY NAMES gstnet libgstnet-1.0 gstnet-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstnet library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstpbutils_LIBRARY NAMES gstpbutils libgstpbutils-1.0 gstpbutils-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstpbutils library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstreamer_LIBRARY NAMES gstreamer libgstreamer-1.0 gstreamer-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstreamer library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstriff_LIBRARY NAMES gstriff libgstriff-1.0 gstriff-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstriff library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstrtp_LIBRARY NAMES gstrtp libgstrtp-1.0 gstrtp-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstrtp library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstrtsp_LIBRARY NAMES gstrtsp libgstrtsp-1.0 gstrtsp-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstrtsp library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstsdp_LIBRARY NAMES gstsdp libgstsdp-1.0 gstsdp-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstsdp library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gsttag_LIBRARY NAMES gsttag libgsttag-1.0 gsttag-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gsttag library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GSTREAMER_gstvideo_LIBRARY NAMES gstvideo libgstvideo-1.0 gstvideo-1.0 + PATHS ${GSTREAMER_DIR}/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/bin ${GSTREAMER_DIR}/bin/bin C:/gstreamer/bin ${GSTREAMER_DIR}/lib ${GSTREAMER_DIR}/win32/lib /usr/local/lib /usr/lib + ENV LIB + DOC "gstvideo library to link with" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GLIB_LIBRARY NAMES libglib-2.0 glib-2.0 + PATHS ${GSTREAMER_DIR}/lib + ENV LIB + DOC "Glib library" + NO_SYSTEM_ENVIRONMENT_PATH) + +FIND_LIBRARY(GOBJECT_LIBRARY NAMES libobject-2.0 gobject-2.0 + PATHS ${GSTREAMER_DIR}/lib + ENV LIB + DOC "Glib library" + NO_SYSTEM_ENVIRONMENT_PATH) + +IF (GSTREAMER_gst_INCLUDE_DIR AND GSTREAMER_gstconfig_INCLUDE_DIR AND + GSTREAMER_gstaudio_LIBRARY AND GSTREAMER_gstbase_LIBRARY AND GSTREAMER_gstcontroller_LIBRARY AND GSTREAMER_gstnet_LIBRARY + AND GSTREAMER_gstpbutils_LIBRARY AND GSTREAMER_gstreamer_LIBRARY AND + GSTREAMER_gstriff_LIBRARY AND GSTREAMER_gstrtp_LIBRARY AND GSTREAMER_gstrtsp_LIBRARY AND GSTREAMER_gstsdp_LIBRARY AND + GSTREAMER_gsttag_LIBRARY AND GSTREAMER_gstvideo_LIBRARY AND GLIB_LIBRARY AND GSTREAMER_gstapp_LIBRARY AND GOBJECT_LIBRARY) + SET(GSTREAMER_INCLUDE_DIR ${GSTREAMER_gst_INCLUDE_DIR} ${GSTREAMER_gstconfig_INCLUDE_DIR} ${GSTREAMER_glib_INCLUDE_DIR} ${GSTREAMER_glibconfig_INCLUDE_DIR}) + + list(REMOVE_DUPLICATES GSTREAMER_INCLUDE_DIR) + SET(GSTREAMER_LIBRARIES ${GSTREAMER_gstaudio_LIBRARY} ${GSTREAMER_gstbase_LIBRARY} + ${GSTREAMER_gstcontroller_LIBRARY} ${GSTREAMER_gstdataprotocol_LIBRARY} ${GSTREAMER_gstinterfaces_LIBRARY} + ${GSTREAMER_gstnet_LIBRARY} ${GSTREAMER_gstpbutils_LIBRARY} + ${GSTREAMER_gstreamer_LIBRARY} ${GSTREAMER_gstriff_LIBRARY} ${GSTREAMER_gstrtp_LIBRARY} + ${GSTREAMER_gstrtsp_LIBRARY} ${GSTREAMER_gstsdp_LIBRARY} ${GSTREAMER_gsttag_LIBRARY} ${GSTREAMER_gstvideo_LIBRARY} ${GLIB_LIBRARY} + ${GSTREAMER_gstapp_LIBRARY} ${GOBJECT_LIBRARY}) + + list(REMOVE_DUPLICATES GSTREAMER_LIBRARIES) + SET(GSTREAMER_FOUND TRUE) +ENDIF (GSTREAMER_gst_INCLUDE_DIR AND GSTREAMER_gstconfig_INCLUDE_DIR AND + GSTREAMER_gstaudio_LIBRARY AND GSTREAMER_gstbase_LIBRARY AND GSTREAMER_gstcontroller_LIBRARY + AND GSTREAMER_gstnet_LIBRARY AND GSTREAMER_gstpbutils_LIBRARY AND GSTREAMER_gstreamer_LIBRARY AND + GSTREAMER_gstriff_LIBRARY AND GSTREAMER_gstrtp_LIBRARY AND GSTREAMER_gstrtsp_LIBRARY AND GSTREAMER_gstsdp_LIBRARY AND + GSTREAMER_gsttag_LIBRARY AND GSTREAMER_gstvideo_LIBRARY AND GLIB_LIBRARY AND GSTREAMER_gstapp_LIBRARY AND GOBJECT_LIBRARY) \ No newline at end of file diff --git a/cmake/OpenCVCRTLinkage.cmake b/cmake/OpenCVCRTLinkage.cmake index 2168c72e6..f5509d83f 100644 --- a/cmake/OpenCVCRTLinkage.cmake +++ b/cmake/OpenCVCRTLinkage.cmake @@ -2,11 +2,14 @@ if(NOT MSVC) message(FATAL_ERROR "CRT options are available only for MSVC") endif() -#if (${CMAKE_SYSTEM_NAME} MATCHES "WindowsStore" OR ${CMAKE_SYSTEM_NAME} MATCHES "WindowsPhone") -# set(WINRT TRUE) - if (WINRT) - add_definitions(/DWINVER=_WIN32_WINNT_WIN8 /DNTDDI_VERSION=NTDDI_WIN8 /D_WIN32_WINNT=_WIN32_WINNT_WIN8) + if (CMAKE_SYSTEM_VERSION MATCHES 10) + add_definitions(/DWINVER=_WIN32_WINNT_WIN10 /DNTDDI_VERSION=NTDDI_WIN10 /D_WIN32_WINNT=_WIN32_WINNT_WIN10) + elseif(CMAKE_SYSTEM_VERSION MATCHES 8.1) + add_definitions(/DWINVER=_WIN32_WINNT_WINBLUE /DNTDDI_VERSION=NTDDI_WINBLUE /D_WIN32_WINNT=_WIN32_WINNT_WINBLUE) + else() + add_definitions(/DWINVER=_WIN32_WINNT_WIN8 /DNTDDI_VERSION=NTDDI_WIN8 /D_WIN32_WINNT=_WIN32_WINNT_WIN8) + endif() endif() # Removing LNK4075 warnings for debug WinRT builds diff --git a/cmake/OpenCVDetectCUDA.cmake b/cmake/OpenCVDetectCUDA.cmake index c1cb95888..9b52a5e8e 100644 --- a/cmake/OpenCVDetectCUDA.cmake +++ b/cmake/OpenCVDetectCUDA.cmake @@ -33,7 +33,12 @@ if(CUDA_FOUND) if(WIN32) find_cuda_helper_libs(nvcuvenc) endif() - set(HAVE_NVCUVID 1) + if(CUDA_nvcuvid_LIBRARY) + set(HAVE_NVCUVID 1) + endif() + if(CUDA_nvcuvenc_LIBRARY) + set(HAVE_NVCUVENC 1) + endif() endif() message(STATUS "CUDA detected: " ${CUDA_VERSION}) diff --git a/cmake/OpenCVDetectOpenCL.cmake b/cmake/OpenCVDetectOpenCL.cmake index 67e10ede8..433f244fe 100644 --- a/cmake/OpenCVDetectOpenCL.cmake +++ b/cmake/OpenCVDetectOpenCL.cmake @@ -1,21 +1,21 @@ +set(OPENCL_FOUND ON CACHE BOOL "OpenCL library is found") if(APPLE) - set(OPENCL_FOUND YES) set(OPENCL_LIBRARY "-framework OpenCL" CACHE STRING "OpenCL library") - set(OPENCL_INCLUDE_DIR "" CACHE STRING "OpenCL include directory") - mark_as_advanced(OPENCL_INCLUDE_DIR OPENCL_LIBRARY) - set(HAVE_OPENCL_STATIC ON) + set(OPENCL_INCLUDE_DIR "" CACHE PATH "OpenCL include directory") else(APPLE) - set(OPENCL_FOUND YES) - set(HAVE_OPENCL_STATIC OFF) - set(OPENCL_INCLUDE_DIR "${OpenCV_SOURCE_DIR}/3rdparty/include/opencl/1.2") + set(OPENCL_LIBRARY "" CACHE STRING "OpenCL library") + set(OPENCL_INCLUDE_DIR "${OpenCV_SOURCE_DIR}/3rdparty/include/opencl/1.2" CACHE PATH "OpenCL include directory") endif(APPLE) - -if(WINRT) - set(OPENCL_FOUND NO) - set(HAVE_OPENCL_STATIC OFF) -endif(WINRT) +mark_as_advanced(OPENCL_INCLUDE_DIR OPENCL_LIBRARY) if(OPENCL_FOUND) + if(OPENCL_LIBRARY) + set(HAVE_OPENCL_STATIC ON) + set(OPENCL_LIBRARIES "${OPENCL_LIBRARY}") + else() + set(HAVE_OPENCL_STATIC OFF) + endif() + if(NOT HAVE_OPENCL_STATIC) try_compile(__VALID_OPENCL "${OpenCV_BINARY_DIR}" @@ -29,20 +29,12 @@ if(OPENCL_FOUND) endif() endif() - if(NOT WINRT) - set(HAVE_OPENCL 1) - endif() + set(HAVE_OPENCL 1) if(WITH_OPENCL_SVM) set(HAVE_OPENCL_SVM 1) endif() - if(HAVE_OPENCL_STATIC) - set(OPENCL_LIBRARIES "${OPENCL_LIBRARY}") - else() - unset(OPENCL_LIBRARIES) - endif() - set(OPENCL_INCLUDE_DIRS ${OPENCL_INCLUDE_DIR}) if(WITH_OPENCLAMDFFT) diff --git a/cmake/OpenCVDetectPython.cmake b/cmake/OpenCVDetectPython.cmake index 7f93d3458..64054d1ef 100644 --- a/cmake/OpenCVDetectPython.cmake +++ b/cmake/OpenCVDetectPython.cmake @@ -26,7 +26,7 @@ function(find_python preferred_version min_version library_env include_dir_env libs_found libs_version_string libraries library debug_libraries debug_library include_path include_dir include_dir2 packages_path numpy_include_dirs numpy_version) - +if(NOT ${found}) ocv_check_environment_variables(${executable}) if(${executable}) set(PYTHON_EXECUTABLE "${${executable}}") @@ -73,7 +73,7 @@ function(find_python preferred_version min_version library_env include_dir_env if(_found) set(_version_major_minor "${_version_major}.${_version_minor}") - if(NOT ANDROID AND NOT IOS) + if(NOT ANDROID AND NOT APPLE_FRAMEWORK) ocv_check_environment_variables(${library_env} ${include_dir_env}) if(NOT ${${library_env}} EQUAL "") set(PYTHON_LIBRARY "${${library_env}}") @@ -197,23 +197,24 @@ function(find_python preferred_version min_version library_env include_dir_env endif() # Export return values - set(${found} "${_found}" PARENT_SCOPE) + set(${found} "${_found}" CACHE INTERNAL "") set(${executable} "${_executable}" CACHE FILEPATH "Path to Python interpretor") - set(${version_string} "${_version_string}" PARENT_SCOPE) - set(${version_major} "${_version_major}" PARENT_SCOPE) - set(${version_minor} "${_version_minor}" PARENT_SCOPE) - set(${libs_found} "${_libs_found}" PARENT_SCOPE) - set(${libs_version_string} "${_libs_version_string}" PARENT_SCOPE) - set(${libraries} "${_libraries}" PARENT_SCOPE) + set(${version_string} "${_version_string}" CACHE INTERNAL "") + set(${version_major} "${_version_major}" CACHE INTERNAL "") + set(${version_minor} "${_version_minor}" CACHE INTERNAL "") + set(${libs_found} "${_libs_found}" CACHE INTERNAL "") + set(${libs_version_string} "${_libs_version_string}" CACHE INTERNAL "") + set(${libraries} "${_libraries}" CACHE INTERNAL "Python libraries") set(${library} "${_library}" CACHE FILEPATH "Path to Python library") - set(${debug_libraries} "${_debug_libraries}" PARENT_SCOPE) + set(${debug_libraries} "${_debug_libraries}" CACHE INTERNAL "") set(${debug_library} "${_debug_library}" CACHE FILEPATH "Path to Python debug") - set(${include_path} "${_include_path}" PARENT_SCOPE) + set(${include_path} "${_include_path}" CACHE INTERNAL "") set(${include_dir} "${_include_dir}" CACHE PATH "Python include dir") set(${include_dir2} "${_include_dir2}" CACHE PATH "Python include dir 2") set(${packages_path} "${_packages_path}" CACHE PATH "Where to install the python packages.") set(${numpy_include_dirs} ${_numpy_include_dirs} CACHE PATH "Path to numpy headers") - set(${numpy_version} "${_numpy_version}" PARENT_SCOPE) + set(${numpy_version} "${_numpy_version}" CACHE INTERNAL "") +endif() endfunction(find_python) find_python(2.7 "${MIN_VER_PYTHON2}" PYTHON2_LIBRARY PYTHON2_INCLUDE_DIR diff --git a/cmake/OpenCVDetectTBB.cmake b/cmake/OpenCVDetectTBB.cmake index 6e7493f06..2acf3477f 100644 --- a/cmake/OpenCVDetectTBB.cmake +++ b/cmake/OpenCVDetectTBB.cmake @@ -65,6 +65,8 @@ if(NOT HAVE_TBB) set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc11") elseif(MSVC12) set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc12") + elseif(MSVC14) + set(_TBB_LIB_PATH "${_TBB_LIB_PATH}/vc14") endif() set(TBB_LIB_DIR "${_TBB_LIB_PATH}" CACHE PATH "Full path of TBB library directory") link_directories("${TBB_LIB_DIR}") diff --git a/cmake/OpenCVDetectVTK.cmake b/cmake/OpenCVDetectVTK.cmake index 66dcdd011..e75aef8a0 100644 --- a/cmake/OpenCVDetectVTK.cmake +++ b/cmake/OpenCVDetectVTK.cmake @@ -3,7 +3,14 @@ if(NOT WITH_VTK) endif() # VTK 6.x components -find_package(VTK QUIET COMPONENTS vtkRenderingOpenGL vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE) +find_package(VTK QUIET COMPONENTS vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE) +IF(VTK_FOUND) + IF(VTK_RENDERING_BACKEND) #in vtk 7, the rendering backend is exported as a var. + find_package(VTK QUIET COMPONENTS vtkRendering${VTK_RENDERING_BACKEND} vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE) + ELSE(VTK_RENDERING_BACKEND) + find_package(VTK QUIET COMPONENTS vtkRenderingOpenGL vtkInteractionStyle vtkRenderingLOD vtkIOPLY vtkFiltersTexture vtkRenderingFreeType vtkIOExport NO_MODULE) + ENDIF(VTK_RENDERING_BACKEND) +ENDIF(VTK_FOUND) # VTK 5.x components if(NOT VTK_FOUND) diff --git a/cmake/OpenCVFindIPP.cmake b/cmake/OpenCVFindIPP.cmake index 71001d9c6..3bff76693 100644 --- a/cmake/OpenCVFindIPP.cmake +++ b/cmake/OpenCVFindIPP.cmake @@ -146,12 +146,14 @@ macro(ipp_detect_version) IMPORTED_LOCATION ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX} ) list(APPEND IPP_LIBRARIES ipp${name}) - # CMake doesn't support "install(TARGETS ${IPP_PREFIX}${name} " command with imported targets - install(FILES ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX} - DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev) - string(TOUPPER ${name} uname) - set(IPP${uname}_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_3P_LIB_INSTALL_PATH}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE) - set(IPP${uname}_LOCATION_PATH "${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE) + if (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB) + # CMake doesn't support "install(TARGETS ${IPP_PREFIX}${name} " command with imported targets + install(FILES ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX} + DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev) + string(TOUPPER ${name} uname) + set(IPP${uname}_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_3P_LIB_INSTALL_PATH}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE) + set(IPP${uname}_LOCATION_PATH "${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}" CACHE INTERNAL "" FORCE) + endif() endif() else() message(STATUS "Can't find IPP library: ${name} at ${IPP_LIBRARY_DIR}/${IPP_LIB_PREFIX}${IPP_PREFIX}${name}${IPP_SUFFIX}${IPP_LIB_SUFFIX}") diff --git a/cmake/OpenCVFindLibsVideo.cmake b/cmake/OpenCVFindLibsVideo.cmake index cce583b5d..20e31518a 100644 --- a/cmake/OpenCVFindLibsVideo.cmake +++ b/cmake/OpenCVFindLibsVideo.cmake @@ -12,23 +12,36 @@ endif(WITH_VFW) # --- GStreamer --- ocv_clear_vars(HAVE_GSTREAMER) -# try to find gstreamer 1.x first +# try to find gstreamer 1.x first if 0.10 was not requested if(WITH_GSTREAMER AND NOT WITH_GSTREAMER_0_10) - CHECK_MODULE(gstreamer-base-1.0 HAVE_GSTREAMER_BASE) - CHECK_MODULE(gstreamer-video-1.0 HAVE_GSTREAMER_VIDEO) - CHECK_MODULE(gstreamer-app-1.0 HAVE_GSTREAMER_APP) - CHECK_MODULE(gstreamer-riff-1.0 HAVE_GSTREAMER_RIFF) - CHECK_MODULE(gstreamer-pbutils-1.0 HAVE_GSTREAMER_PBUTILS) + if(WIN32) + SET(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_LIST_DIR}") + FIND_PACKAGE(GstreamerWindows) + IF(GSTREAMER_gstbase_LIBRARY AND GSTREAMER_gstvideo_LIBRARY AND GSTREAMER_gstapp_LIBRARY AND GSTREAMER_gstpbutils_LIBRARY AND GSTREAMER_gstriff_LIBRARY) + set(HAVE_GSTREAMER TRUE) + set(GSTREAMER_BASE_VERSION 1.0) + set(GSTREAMER_VIDEO_VERSION 1.0) + set(GSTREAMER_APP_VERSION 1.0) + set(GSTREAMER_RIFF_VERSION 1.0) + set(GSTREAMER_PBUTILS_VERSION 1.0) + ENDIF(GSTREAMER_gstbase_LIBRARY AND GSTREAMER_gstvideo_LIBRARY AND GSTREAMER_gstapp_LIBRARY AND GSTREAMER_gstpbutils_LIBRARY AND GSTREAMER_gstriff_LIBRARY) - if(HAVE_GSTREAMER_BASE AND HAVE_GSTREAMER_VIDEO AND HAVE_GSTREAMER_APP AND HAVE_GSTREAMER_RIFF AND HAVE_GSTREAMER_PBUTILS) + else(WIN32) + CHECK_MODULE(gstreamer-base-1.0 HAVE_GSTREAMER_BASE) + CHECK_MODULE(gstreamer-video-1.0 HAVE_GSTREAMER_VIDEO) + CHECK_MODULE(gstreamer-app-1.0 HAVE_GSTREAMER_APP) + CHECK_MODULE(gstreamer-riff-1.0 HAVE_GSTREAMER_RIFF) + CHECK_MODULE(gstreamer-pbutils-1.0 HAVE_GSTREAMER_PBUTILS) + + if(HAVE_GSTREAMER_BASE AND HAVE_GSTREAMER_VIDEO AND HAVE_GSTREAMER_APP AND HAVE_GSTREAMER_RIFF AND HAVE_GSTREAMER_PBUTILS) set(HAVE_GSTREAMER TRUE) set(GSTREAMER_BASE_VERSION ${ALIASOF_gstreamer-base-1.0_VERSION}) set(GSTREAMER_VIDEO_VERSION ${ALIASOF_gstreamer-video-1.0_VERSION}) set(GSTREAMER_APP_VERSION ${ALIASOF_gstreamer-app-1.0_VERSION}) set(GSTREAMER_RIFF_VERSION ${ALIASOF_gstreamer-riff-1.0_VERSION}) set(GSTREAMER_PBUTILS_VERSION ${ALIASOF_gstreamer-pbutils-1.0_VERSION}) - endif() - + endif() + endif(WIN32) endif(WITH_GSTREAMER AND NOT WITH_GSTREAMER_0_10) # if gstreamer 1.x was not found, or we specified we wanted 0.10, try to find it @@ -201,7 +214,7 @@ if(WITH_FFMPEG) CHECK_INCLUDE_FILE(libavformat/avformat.h HAVE_GENTOO_FFMPEG) CHECK_INCLUDE_FILE(ffmpeg/avformat.h HAVE_FFMPEG_FFMPEG) if(NOT HAVE_GENTOO_FFMPEG AND NOT HAVE_FFMPEG_FFMPEG) - if(EXISTS /usr/include/ffmpeg/libavformat/avformat.h OR HAVE_FFMPEG_SWSCALE) + if((NOT CMAKE_CROSSCOMPILING AND EXISTS /usr/include/ffmpeg/libavformat/avformat.h) OR HAVE_FFMPEG_SWSCALE) set(HAVE_GENTOO_FFMPEG TRUE) endif() endif() @@ -211,11 +224,7 @@ if(WITH_FFMPEG) if(HAVE_FFMPEG) # Find the bzip2 library because it is required on some systems - FIND_LIBRARY(BZIP2_LIBRARIES NAMES bz2 bzip2) - if(NOT BZIP2_LIBRARIES) - # Do an other trial - FIND_FILE(BZIP2_LIBRARIES NAMES libbz2.so.1 PATHS /lib) - endif() + FIND_LIBRARY(BZIP2_LIBRARIES NAMES bz2 bzip2 libbz2.so.1) else() find_path(FFMPEG_INCLUDE_DIR "libavformat/avformat.h" PATHS /usr/local /usr /opt diff --git a/cmake/OpenCVGenConfig.cmake b/cmake/OpenCVGenConfig.cmake index ae8fc8939..dbfd7ca4a 100644 --- a/cmake/OpenCVGenConfig.cmake +++ b/cmake/OpenCVGenConfig.cmake @@ -83,7 +83,7 @@ endif() export(TARGETS ${OpenCVModules_TARGETS} FILE "${CMAKE_BINARY_DIR}/OpenCVModules${modules_file_suffix}.cmake") -if(TARGET ippicv) +if(TARGET ippicv AND (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)) set(USE_IPPICV TRUE) file(RELATIVE_PATH INSTALL_PATH_RELATIVE_IPPICV ${CMAKE_BINARY_DIR} ${IPPICV_LOCATION_PATH}) else() @@ -133,11 +133,7 @@ if(WIN32) exec_program(mkdir ARGS "-p \"${CMAKE_BINARY_DIR}/win-install/\"" OUTPUT_VARIABLE RET_VAL) if(USE_IPPICV) - if(BUILD_SHARED_LIBS) - file(RELATIVE_PATH INSTALL_PATH_RELATIVE_IPPICV "${CMAKE_INSTALL_PREFIX}/${OpenCV_INSTALL_BINARIES_PREFIX}lib" ${IPPICV_INSTALL_PATH}) - else() - file(RELATIVE_PATH INSTALL_PATH_RELATIVE_IPPICV "${CMAKE_INSTALL_PREFIX}/${OpenCV_INSTALL_BINARIES_PREFIX}staticlib" ${IPPICV_INSTALL_PATH}) - endif() + file(RELATIVE_PATH INSTALL_PATH_RELATIVE_IPPICV "${CMAKE_INSTALL_PREFIX}/${OpenCV_INSTALL_BINARIES_PREFIX}staticlib" ${IPPICV_INSTALL_PATH}) endif() configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/OpenCVConfig.cmake.in" "${CMAKE_BINARY_DIR}/win-install/OpenCVConfig.cmake" @ONLY) configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/OpenCVConfig-version.cmake.in" "${CMAKE_BINARY_DIR}/win-install/OpenCVConfig-version.cmake" @ONLY) diff --git a/cmake/OpenCVGenInfoPlist.cmake b/cmake/OpenCVGenInfoPlist.cmake index 680afb2df..2b78ae1e5 100644 --- a/cmake/OpenCVGenInfoPlist.cmake +++ b/cmake/OpenCVGenInfoPlist.cmake @@ -1,10 +1,5 @@ -if(OPENCV_EXTRA_WORLD) - set(OPENCV_APPLE_BUNDLE_NAME "OpenCV_contrib") - set(OPENCV_APPLE_BUNDLE_ID "org.opencv_contrib") -else() - set(OPENCV_APPLE_BUNDLE_NAME "OpenCV") - set(OPENCV_APPLE_BUNDLE_ID "org.opencv") -endif() +set(OPENCV_APPLE_BUNDLE_NAME "OpenCV") +set(OPENCV_APPLE_BUNDLE_ID "org.opencv") if(IOS) configure_file("${OpenCV_SOURCE_DIR}/platforms/ios/Info.plist.in" diff --git a/cmake/OpenCVGenPkgconfig.cmake b/cmake/OpenCVGenPkgconfig.cmake index 28a6da686..b8cb8777c 100644 --- a/cmake/OpenCVGenPkgconfig.cmake +++ b/cmake/OpenCVGenPkgconfig.cmake @@ -21,6 +21,8 @@ macro(fix_prefix lst isown) endif() if(item MATCHES "^-l") list(APPEND _lst "${item}") + elseif(item MATCHES "^-framework") # MacOS framework (assume single entry "-framework OpenCL") + list(APPEND _lst "${item}") elseif(item MATCHES "[\\/]") get_filename_component(libdir "${item}" PATH) get_filename_component(libname "${item}" NAME_WE) diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake index 92894b8b8..23a637a25 100644 --- a/cmake/OpenCVModule.cmake +++ b/cmake/OpenCVModule.cmake @@ -305,9 +305,10 @@ macro(ocv_glob_modules) set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE) set(OPENCV_INITIAL_PASS OFF) if(${BUILD_opencv_world}) - add_subdirectory("${OPENCV_MODULE_opencv_world_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/world") foreach(m ${OPENCV_MODULES_BUILD}) - if(NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD AND NOT ${m} STREQUAL opencv_world) + if("${m}" STREQUAL opencv_world) + add_subdirectory("${OPENCV_MODULE_opencv_world_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/world") + elseif(NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD AND NOT ${m} STREQUAL opencv_world) message(STATUS "Processing module ${m}...") if(m MATCHES "^opencv_") string(REGEX REPLACE "^opencv_" "" __shortname "${m}") @@ -351,6 +352,7 @@ function(__ocv_sort_modules_by_deps __lst) ocv_list_sort(${__lst}) set(input ${${__lst}}) set(result "") + set(result_extra "") while(input) list(LENGTH input length_before) foreach (m ${input}) @@ -375,16 +377,27 @@ function(__ocv_sort_modules_by_deps __lst) list(LENGTH input length_after) # check for infinite loop or unresolved dependencies if (NOT length_after LESS length_before) - message(WARNING "Unresolved dependencies or loop in dependency graph (${length_after})\n" - "Processed ${__lst}: ${${__lst}}\n" - "Good modules: ${result}\n" - "Bad modules: ${input}" - ) - list(APPEND result ${input}) - break() + if(NOT BUILD_SHARED_LIBS) + if (";${input};" MATCHES ";opencv_world;") + list(REMOVE_ITEM input "opencv_world") + list(APPEND result_extra "opencv_world") + else() + # We can't do here something + list(APPEND result ${input}) + break() + endif() + else() + message(FATAL_ERROR WARNING "Unresolved dependencies or loop in dependency graph (${length_after})\n" + "Processed ${__lst}: ${${__lst}}\n" + "Good modules: ${result}\n" + "Bad modules: ${input}" + ) + list(APPEND result ${input}) + break() + endif() endif() endwhile() - set(${__lst} "${result}" PARENT_SCOPE) + set(${__lst} "${result};${result_extra}" PARENT_SCOPE) endfunction() # resolve dependensies @@ -446,6 +459,17 @@ function(__ocv_resolve_dependencies) list(APPEND deps_${m} ${d}) set(has_changes ON) endif() + if(BUILD_opencv_world + AND NOT "${m}" STREQUAL "opencv_world" + AND NOT "${m2}" STREQUAL "opencv_world" + AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD + AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD) + if(NOT (";${deps_${m}};" MATCHES ";opencv_world;")) +# message(STATUS " Transfer dependency opencv_world alias ${m2} to ${m}") + list(APPEND deps_${m} opencv_world) + set(has_changes ON) + endif() + endif() endforeach() endif() endforeach() @@ -643,6 +667,8 @@ macro(ocv_glob_module_sources) "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp" "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp" "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h" + "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp" + "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h" ) file(GLOB lib_hdrs_detail "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp" @@ -675,7 +701,6 @@ macro(ocv_glob_module_sources) ) if(cl_kernels) set(OCL_NAME opencl_kernels_${name}) - ocv_include_directories(${OPENCL_INCLUDE_DIRS}) add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp" COMMAND ${CMAKE_COMMAND} "-DMODULE_NAME=${name}" "-DCL_DIR=${CMAKE_CURRENT_LIST_DIR}/src/opencl" "-DOUTPUT=${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake" @@ -777,11 +802,20 @@ macro(_ocv_create_module) set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module") - ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) - ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) - ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN}) - if (HAVE_CUDA) - ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY}) + if(NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB) + ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) + ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) + ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN}) + if (HAVE_CUDA) + ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY}) + endif() + else() + ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) + ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}) + ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN}) + if (HAVE_CUDA) + ocv_target_link_libraries(${the_module} LINK_PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY}) + endif() endif() add_dependencies(opencv_modules ${the_module}) @@ -825,12 +859,14 @@ macro(_ocv_create_module) set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG") endif() - ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL - RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs - LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP - ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev - ) get_target_property(_target_type ${the_module} TYPE) + if("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)) + ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL + RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs + LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP + ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev + ) + endif() if("${_target_type}" STREQUAL "SHARED_LIBRARY") install(TARGETS ${the_module} LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY) diff --git a/cmake/OpenCVPCHSupport.cmake b/cmake/OpenCVPCHSupport.cmake index 28ccc1c6b..243fb4c0c 100644 --- a/cmake/OpenCVPCHSupport.cmake +++ b/cmake/OpenCVPCHSupport.cmake @@ -164,9 +164,9 @@ MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn ) # if you have different versions of the headers for different build types # you may set _pch_dowarn IF (_dowarn) - SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" -Winvalid-pch " ) + SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -Winvalid-pch " ) ELSE (_dowarn) - SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_header_name}\" " ) + SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} " ) ENDIF (_dowarn) ELSE(CMAKE_COMPILER_IS_GNUCXX) diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index e4e8771bf..765ee7109 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -1,11 +1,15 @@ +include(CMakeParseArguments) + # Debugging function function(ocv_cmake_dump_vars) + set(VARS "") + get_cmake_property(_variableNames VARIABLES) cmake_parse_arguments(DUMP "" "TOFILE" "" ${ARGN}) set(regex "${DUMP_UNPARSED_ARGUMENTS}") - get_cmake_property(_variableNames VARIABLES) - set(VARS "") + string(TOLOWER "${regex}" regex_lower) foreach(_variableName ${_variableNames}) - if(_variableName MATCHES "${regex}") + string(TOLOWER "${_variableName}" _variableName_lower) + if(_variableName MATCHES "${regex}" OR _variableName_lower MATCHES "${regex_lower}") set(VARS "${VARS}${_variableName}=${${_variableName}}\n") endif() endforeach() @@ -16,6 +20,15 @@ function(ocv_cmake_dump_vars) endif() endfunction() +function(ocv_cmake_eval var_name) + if(DEFINED ${var_name}) + file(WRITE "${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake" ${${var_name}}) + include("${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake") + endif() + if(";${ARGN};" MATCHES ";ONCE;") + unset(${var_name} CACHE) + endif() +endfunction() # Search packages for host system instead of packages for target system # in case of cross compilation thess macro should be defined by toolchain file @@ -503,17 +516,15 @@ endmacro() # convert list of paths to libraries names without lib prefix -macro(ocv_convert_to_lib_name var) - set(__tmp "") +function(ocv_convert_to_lib_name var) + set(tmp "") foreach(path ${ARGN}) - get_filename_component(__tmp_name "${path}" NAME_WE) - string(REGEX REPLACE "^lib" "" __tmp_name ${__tmp_name}) - list(APPEND __tmp "${__tmp_name}") + get_filename_component(tmp_name "${path}" NAME_WE) + string(REGEX REPLACE "^lib" "" tmp_name "${tmp_name}") + list(APPEND tmp "${tmp_name}") endforeach() - set(${var} ${__tmp}) - unset(__tmp) - unset(__tmp_name) -endmacro() + set(${var} ${tmp} PARENT_SCOPE) +endfunction() # add install command @@ -727,6 +738,9 @@ function(ocv_target_link_libraries target) endif() endforeach() endif() + if(";${LINK_DEPS};" MATCHES ";${target};") + list(REMOVE_ITEM LINK_DEPS "${target}") # prevent "link to itself" warning (world problem) + endif() target_link_libraries(${target} ${LINK_DEPS}) endfunction() @@ -746,20 +760,9 @@ function(ocv_add_executable target) endfunction() function(ocv_add_library target) - set(cuda_objs "") - if(HAVE_CUDA) - set(cuda_srcs "") - - foreach(var ${ARGN}) - if(var MATCHES ".cu") - list(APPEND cuda_srcs ${var}) - endif() - endforeach() - - if(cuda_srcs) - ocv_include_directories(${CUDA_INCLUDE_DIRS}) - ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs}) - endif() + if(HAVE_CUDA AND ARGN MATCHES "\\.cu") + ocv_include_directories(${CUDA_INCLUDE_DIRS}) + ocv_cuda_compile(cuda_objs ${ARGN}) set(OPENCV_MODULE_${target}_CUDA_OBJECTS ${cuda_objs} CACHE INTERNAL "Compiled CUDA object files") endif() @@ -770,9 +773,10 @@ function(ocv_add_library target) AND NOT OPENCV_MODULE_${target}_CHILDREN AND NOT OPENCV_MODULE_${target}_CLASS STREQUAL "BINDINGS" AND NOT ${target} STREQUAL "opencv_ts" + AND (NOT BUILD_opencv_world OR NOT HAVE_CUDA) ) set(sources ${ARGN}) - ocv_list_filterout(sources "\\\\.(cl|inc)$") + ocv_list_filterout(sources "\\\\.(cl|inc|cu)$") add_library(${target}_object OBJECT ${sources}) set_target_properties(${target}_object PROPERTIES EXCLUDE_FROM_ALL True @@ -805,7 +809,13 @@ macro(ocv_get_all_libs _modules _extra _3rdparty) else() set(deps "") endif() - list(INSERT ${_modules} 0 ${deps} ${m}) + set(_rev_deps "${deps};${m}") + ocv_list_reverse(_rev_deps) + foreach (dep ${_rev_deps}) + if(DEFINED OPENCV_MODULE_${dep}_LOCATION) + list(INSERT ${_modules} 0 ${dep}) + endif() + endforeach() foreach (dep ${deps} ${OPENCV_LINKER_LIBS}) if (NOT DEFINED OPENCV_MODULE_${dep}_LOCATION) if (TARGET ${dep}) @@ -826,11 +836,14 @@ macro(ocv_get_all_libs _modules _extra _3rdparty) list(FIND ${_extra} "ippicv" ippicv_idx) if (${ippicv_idx} GREATER -1) list(REMOVE_ITEM ${_extra} "ippicv") - list(INSERT ${_3rdparty} 0 "ippicv") + if(NOT BUILD_SHARED_LIBS) + list(INSERT ${_3rdparty} 0 "ippicv") + endif() endif() - # split 3rdparty libs and modules - list(REMOVE_ITEM ${_modules} ${${_3rdparty}} ${${_extra}} non_empty_list) + ocv_list_filterout(${_modules} "^[\$]<") + ocv_list_filterout(${_3rdparty} "^[\$]<") + ocv_list_filterout(${_extra} "^[\$]<") # convert CMake lists to makefile literals foreach(lst ${_modules} ${_3rdparty} ${_extra}) diff --git a/cmake/checks/opencl.cpp b/cmake/checks/opencl.cpp index 95a36f3ac..b71b92f67 100644 --- a/cmake/checks/opencl.cpp +++ b/cmake/checks/opencl.cpp @@ -1,8 +1,5 @@ -#if defined __APPLE__ -#include -#else +// custom OpenCL headers are located in "CL" subfolder (3rdparty/include/...) #include -#endif #ifndef _MSC_VER #ifdef CL_VERSION_1_2 diff --git a/cmake/cl2cpp.cmake b/cmake/cl2cpp.cmake index d5e3d5f03..ed5dcb876 100644 --- a/cmake/cl2cpp.cmake +++ b/cmake/cl2cpp.cmake @@ -24,6 +24,7 @@ endif() set(STR_CPP "// This file is auto-generated. Do not edit! #include \"precomp.hpp\" +#include \"cvconfig.h\" #include \"${OUTPUT_HPP_NAME}\" #ifdef HAVE_OPENCL diff --git a/cmake/templates/OpenCVConfig-version.cmake.in b/cmake/templates/OpenCVConfig-version.cmake.in index b5ac5f8e2..829cf9d97 100644 --- a/cmake/templates/OpenCVConfig-version.cmake.in +++ b/cmake/templates/OpenCVConfig-version.cmake.in @@ -9,6 +9,7 @@ if(PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION) set(PACKAGE_VERSION_COMPATIBLE True) endif() -if(PACKAGE_FIND_VERSION VERSION_LESS PACKAGE_VERSION) +if(PACKAGE_FIND_VERSION_MAJOR EQUAL @OPENCV_VERSION_MAJOR@ + AND PACKAGE_FIND_VERSION VERSION_LESS PACKAGE_VERSION) set(PACKAGE_VERSION_COMPATIBLE True) endif() diff --git a/cmake/templates/OpenCVConfig.cmake.in b/cmake/templates/OpenCVConfig.cmake.in index ee57ecda6..80ffbaf01 100644 --- a/cmake/templates/OpenCVConfig.cmake.in +++ b/cmake/templates/OpenCVConfig.cmake.in @@ -159,6 +159,7 @@ SET(OpenCV_VERSION_STATUS "@OPENCV_VERSION_STATUS@") # ==================================================================== SET(OpenCV_LIB_COMPONENTS @OPENCV_MODULES_CONFIGCMAKE@) +list(REMOVE_ITEM OpenCV_LIB_COMPONENTS opencv_hal) SET(OpenCV_WORLD_COMPONENTS @OPENCV_WORLD_MODULES@) # ============================================================== diff --git a/cmake/templates/custom_hal.hpp.in b/cmake/templates/custom_hal.hpp.in index b298a033e..f1c651595 100644 --- a/cmake/templates/custom_hal.hpp.in +++ b/cmake/templates/custom_hal.hpp.in @@ -1,6 +1,6 @@ #ifndef _CUSTOM_HAL_INCLUDED_ #define _CUSTOM_HAL_INCLUDED_ -@OPENCV_HAL_HEADERS_INCLUDES@ +@_includes@ #endif diff --git a/cmake/templates/cvconfig.h.in b/cmake/templates/cvconfig.h.in index 608372967..c80720dcd 100644 --- a/cmake/templates/cvconfig.h.in +++ b/cmake/templates/cvconfig.h.in @@ -120,6 +120,9 @@ /* NVidia Video Decoding API*/ #cmakedefine HAVE_NVCUVID +/* NVidia Video Encoding API*/ +#cmakedefine HAVE_NVCUVENC + /* OpenCL Support */ #cmakedefine HAVE_OPENCL #cmakedefine HAVE_OPENCL_STATIC diff --git a/cmake/templates/opencv_abi.xml.in b/cmake/templates/opencv_abi.xml.in index 292d9b491..43a53c3ee 100644 --- a/cmake/templates/opencv_abi.xml.in +++ b/cmake/templates/opencv_abi.xml.in @@ -21,7 +21,7 @@ - opencv2/hal/intrin* + opencv2/core/hal/intrin* opencv2/core/cuda* opencv2/core/private* opencv/cxeigen.hpp diff --git a/data/haarcascades_cuda/haarcascade_upperbody.xml b/data/haarcascades_cuda/haarcascade_upperbody.xml index 98452e259..f22f8e670 100644 --- a/data/haarcascades_cuda/haarcascade_upperbody.xml +++ b/data/haarcascades_cuda/haarcascade_upperbody.xml @@ -1,5 +1,5 @@ - good_matches; for( int i = 0; i < descriptors_object.rows; i++ ) - { if( matches[i].distance < 3*min_dist ) + { if( matches[i].distance <= 3*min_dist ) { good_matches.push_back( matches[i]); } } diff --git a/doc/tutorials/highgui/table_of_content_highgui.markdown b/doc/tutorials/highgui/table_of_content_highgui.markdown index 3ff0e0322..a8f1d4e34 100644 --- a/doc/tutorials/highgui/table_of_content_highgui.markdown +++ b/doc/tutorials/highgui/table_of_content_highgui.markdown @@ -1,8 +1,7 @@ High Level GUI and Media (highgui module) {#tutorial_table_of_content_highgui} ========================================= -This section contains valuable tutorials about how to read/save your image/video files and how to -use the built-in graphical user interface of the library. +This section contains tutorials about how to use the built-in graphical user interface of the library. - @subpage tutorial_trackbar @@ -11,33 +10,3 @@ use the built-in graphical user interface of the library. *Author:* Ana Huamán We will learn how to add a Trackbar to our applications - -- @subpage tutorial_video_input_psnr_ssim - - *Compatibility:* \> OpenCV 2.0 - - *Author:* Bernát Gábor - - You will learn how to read video streams, and how to calculate similarity values such as PSNR - or SSIM. - -- @subpage tutorial_video_write - - *Compatibility:* \> OpenCV 2.0 - - *Author:* Bernát Gábor - - Whenever you work with video feeds you may eventually want to save your image processing - result in a form of a new video file. Here's how to do it. - -- @subpage tutorial_raster_io_gdal - - *Compatibility:* \> OpenCV 2.0 - - *Author:* Marvin Smith - - Read common GIS Raster and DEM files to display and manipulate geographic data. - -- @subpage tutorial_kinect_openni - -- @subpage tutorial_intelperc diff --git a/doc/tutorials/highgui/images/gdal-io.jpg b/doc/tutorials/imgcodecs/images/gdal-io.jpg similarity index 100% rename from doc/tutorials/highgui/images/gdal-io.jpg rename to doc/tutorials/imgcodecs/images/gdal-io.jpg diff --git a/doc/tutorials/highgui/raster-gdal/images/gdal_flood-zone.jpg b/doc/tutorials/imgcodecs/raster-gdal/images/gdal_flood-zone.jpg similarity index 100% rename from doc/tutorials/highgui/raster-gdal/images/gdal_flood-zone.jpg rename to doc/tutorials/imgcodecs/raster-gdal/images/gdal_flood-zone.jpg diff --git a/doc/tutorials/highgui/raster-gdal/images/gdal_heat-map.jpg b/doc/tutorials/imgcodecs/raster-gdal/images/gdal_heat-map.jpg similarity index 100% rename from doc/tutorials/highgui/raster-gdal/images/gdal_heat-map.jpg rename to doc/tutorials/imgcodecs/raster-gdal/images/gdal_heat-map.jpg diff --git a/doc/tutorials/highgui/raster-gdal/images/gdal_output.jpg b/doc/tutorials/imgcodecs/raster-gdal/images/gdal_output.jpg similarity index 100% rename from doc/tutorials/highgui/raster-gdal/images/gdal_output.jpg rename to doc/tutorials/imgcodecs/raster-gdal/images/gdal_output.jpg diff --git a/doc/tutorials/highgui/raster-gdal/raster_io_gdal.markdown b/doc/tutorials/imgcodecs/raster-gdal/raster_io_gdal.markdown similarity index 98% rename from doc/tutorials/highgui/raster-gdal/raster_io_gdal.markdown rename to doc/tutorials/imgcodecs/raster-gdal/raster_io_gdal.markdown index 95137be2d..a511d8088 100644 --- a/doc/tutorials/highgui/raster-gdal/raster_io_gdal.markdown +++ b/doc/tutorials/imgcodecs/raster-gdal/raster_io_gdal.markdown @@ -28,7 +28,7 @@ of the bay rise 10, 50, and 100 meters. Code ---- -@include cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp +@include cpp/tutorial_code/imgcodecs/GDAL_IO/gdal-image.cpp How to Read Raster Data using GDAL ---------------------------------- diff --git a/doc/tutorials/imgcodecs/table_of_content_highgui.markdown b/doc/tutorials/imgcodecs/table_of_content_highgui.markdown new file mode 100644 index 000000000..e78e8276f --- /dev/null +++ b/doc/tutorials/imgcodecs/table_of_content_highgui.markdown @@ -0,0 +1,12 @@ +Image Input and Output (imgcodecs module) {#tutorial_table_of_content_imgcodecs} +========================================= + +This section contains tutorials about how to read/save your image files. + +- @subpage tutorial_raster_io_gdal + + *Compatibility:* \> OpenCV 2.0 + + *Author:* Marvin Smith + + Read common GIS Raster and DEM files to display and manipulate geographic data. diff --git a/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown b/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown index 7d5df93d2..78776af33 100644 --- a/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown +++ b/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.markdown @@ -71,9 +71,9 @@ application. In contrast the *Release* is an optimized version, where the goal i application run as fast as possible or to be as small as possible. You may figure that these modes also require different rules to use during build. Therefore, there exist different rule packages for each of your build modes. These rule packages are called inside the IDE as *project properties* and -you can view and modify them by using the *Property Manger*. You can bring up this with -View --\> Property Pages. Expand it and you can see the existing rule packages (called *Proporty -Sheets*). +you can view and modify them by using the *Property Manager*. You can bring this up with +View --\> Property Pages (For Visual Studio 2013 onwards, go to View --\> Other Windows --\> Property Manager). +Expand it and you can see the existing rule packages (called *Property Sheets*). ![](images/PropertyPageExample.jpg) diff --git a/doc/tutorials/tutorials.markdown b/doc/tutorials/tutorials.markdown index 552420c3c..931921777 100644 --- a/doc/tutorials/tutorials.markdown +++ b/doc/tutorials/tutorials.markdown @@ -25,10 +25,17 @@ As always, we would be happy to hear your comments and receive your contribution - @subpage tutorial_table_of_content_highgui - This section - contains valuable tutorials about how to read/save your image/video files and how to use the + This section contains valuable tutorials about how to use the built-in graphical user interface of the library. +- @subpage tutorial_table_of_content_imgcodecs + + These tutorials show how to read and write images using imgcodecs module. + +- @subpage tutorial_table_of_content_videoio + + These tutorials show how to read and write videos using videio module. + - @subpage tutorial_table_of_content_calib3d Although we got diff --git a/doc/tutorials/highgui/images/video-input-psnr-ssim.png b/doc/tutorials/videoio/images/video-input-psnr-ssim.png similarity index 100% rename from doc/tutorials/highgui/images/video-input-psnr-ssim.png rename to doc/tutorials/videoio/images/video-input-psnr-ssim.png diff --git a/doc/tutorials/highgui/images/video-write.png b/doc/tutorials/videoio/images/video-write.png similarity index 100% rename from doc/tutorials/highgui/images/video-write.png rename to doc/tutorials/videoio/images/video-write.png diff --git a/doc/tutorials/highgui/intelperc.markdown b/doc/tutorials/videoio/intelperc.markdown similarity index 100% rename from doc/tutorials/highgui/intelperc.markdown rename to doc/tutorials/videoio/intelperc.markdown diff --git a/doc/tutorials/highgui/kinect_openni.markdown b/doc/tutorials/videoio/kinect_openni.markdown similarity index 100% rename from doc/tutorials/highgui/kinect_openni.markdown rename to doc/tutorials/videoio/kinect_openni.markdown diff --git a/doc/tutorials/videoio/table_of_content_videoio.markdown b/doc/tutorials/videoio/table_of_content_videoio.markdown new file mode 100644 index 000000000..4f6276511 --- /dev/null +++ b/doc/tutorials/videoio/table_of_content_videoio.markdown @@ -0,0 +1,23 @@ +Video Input and Output (videoio module) {#tutorial_table_of_content_videoio} +========================================= + +This section contains tutorials about how to read/save your video files. + +- @subpage tutorial_video_input_psnr_ssim + + *Compatibility:* \> OpenCV 2.0 + + *Author:* Bernát Gábor + + You will learn how to read video streams, and how to calculate similarity values such as PSNR + or SSIM. + +- @subpage tutorial_video_write + + *Compatibility:* \> OpenCV 2.0 + + *Author:* Bernát Gábor + +- @subpage tutorial_kinect_openni + +- @subpage tutorial_intelperc diff --git a/doc/tutorials/highgui/video-input-psnr-ssim/images/outputVideoInput.png b/doc/tutorials/videoio/video-input-psnr-ssim/images/outputVideoInput.png similarity index 100% rename from doc/tutorials/highgui/video-input-psnr-ssim/images/outputVideoInput.png rename to doc/tutorials/videoio/video-input-psnr-ssim/images/outputVideoInput.png diff --git a/doc/tutorials/highgui/video-input-psnr-ssim/video_input_psnr_ssim.markdown b/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown similarity index 96% rename from doc/tutorials/highgui/video-input-psnr-ssim/video_input_psnr_ssim.markdown rename to doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown index 1e61a8775..9dbff286c 100644 --- a/doc/tutorials/highgui/video-input-psnr-ssim/video_input_psnr_ssim.markdown +++ b/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown @@ -20,12 +20,12 @@ As a test case where to show off these using OpenCV I've created a small program video files and performs a similarity check between them. This is something you could use to check just how well a new video compressing algorithms works. Let there be a reference (original) video like [this small Megamind clip -](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi) and [a compressed -version of it ](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind_bugy.avi). +](https://github.com/Itseez/opencv/tree/master/samples/data/Megamind.avi) and [a compressed +version of it ](https://github.com/Itseez/opencv/tree/master/samples/data/Megamind_bugy.avi). You may also find the source code and these video file in the -`samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/` folder of the OpenCV source library. +`samples/data` folder of the OpenCV source library. -@include cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video-input-psnr-ssim.cpp +@include cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp How to read a video stream (online-camera or offline-file)? ----------------------------------------------------------- diff --git a/doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png b/doc/tutorials/videoio/video-write/images/resultOutputWideoWrite.png similarity index 100% rename from doc/tutorials/highgui/video-write/images/resultOutputWideoWrite.png rename to doc/tutorials/videoio/video-write/images/resultOutputWideoWrite.png diff --git a/doc/tutorials/highgui/video-write/images/videoCompressSelect.png b/doc/tutorials/videoio/video-write/images/videoCompressSelect.png similarity index 100% rename from doc/tutorials/highgui/video-write/images/videoCompressSelect.png rename to doc/tutorials/videoio/video-write/images/videoCompressSelect.png diff --git a/doc/tutorials/highgui/video-write/images/videoFileStructure.png b/doc/tutorials/videoio/video-write/images/videoFileStructure.png similarity index 100% rename from doc/tutorials/highgui/video-write/images/videoFileStructure.png rename to doc/tutorials/videoio/video-write/images/videoFileStructure.png diff --git a/doc/tutorials/highgui/video-write/video_write.markdown b/doc/tutorials/videoio/video-write/video_write.markdown similarity index 97% rename from doc/tutorials/highgui/video-write/video_write.markdown rename to doc/tutorials/videoio/video-write/video_write.markdown index d10f6d051..399ac7a1e 100644 --- a/doc/tutorials/highgui/video-write/video_write.markdown +++ b/doc/tutorials/videoio/video-write/video_write.markdown @@ -30,10 +30,10 @@ The source code --------------- You may also find the source code and these video file in the -`samples/cpp/tutorial_code/highgui/video-write/` folder of the OpenCV source library or [download it -from here ](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp). +`samples/cpp/tutorial_code/videoio/video-write/` folder of the OpenCV source library or [download it +from here ](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/videoio/video-write/video-write.cpp). -@include cpp/tutorial_code/HighGUI/video-write/video-write.cpp +@include cpp/tutorial_code/videoio/video-write/video-write.cpp The structure of a video ------------------------ diff --git a/include/opencv2/opencv.hpp b/include/opencv2/opencv.hpp index fd9ca5898..49b6a6691 100644 --- a/include/opencv2/opencv.hpp +++ b/include/opencv2/opencv.hpp @@ -43,16 +43,38 @@ #ifndef __OPENCV_ALL_HPP__ #define __OPENCV_ALL_HPP__ +#include "opencv2/opencv_modules.hpp" + #include "opencv2/core.hpp" +#ifdef HAVE_OPENCV_IMGPROC #include "opencv2/imgproc.hpp" +#endif +#ifdef HAVE_OPENCV_PHOTO #include "opencv2/photo.hpp" +#endif +#ifdef HAVE_OPENCV_VIDEO #include "opencv2/video.hpp" +#endif +#ifdef HAVE_OPENCV_FEATURES2D #include "opencv2/features2d.hpp" +#endif +#ifdef HAVE_OPENCV_OBJDETECT #include "opencv2/objdetect.hpp" +#endif +#ifdef HAVE_OPENCV_CALIB3D #include "opencv2/calib3d.hpp" +#endif +#ifdef HAVE_OPENCV_IMGCODECS #include "opencv2/imgcodecs.hpp" +#endif +#ifdef HAVE_OPENCV_VIDEOIO #include "opencv2/videoio.hpp" +#endif +#ifdef HAVE_OPENCV_HIGHGUI #include "opencv2/highgui.hpp" +#endif +#ifdef HAVE_OPENCV_ML #include "opencv2/ml.hpp" +#endif #endif diff --git a/modules/calib3d/doc/calib3d.bib b/modules/calib3d/doc/calib3d.bib new file mode 100644 index 000000000..57989b34f --- /dev/null +++ b/modules/calib3d/doc/calib3d.bib @@ -0,0 +1,41 @@ +@article{lepetit2009epnp, + title={Epnp: An accurate o (n) solution to the pnp problem}, + author={Lepetit, Vincent and Moreno-Noguer, Francesc and Fua, Pascal}, + journal={International journal of computer vision}, + volume={81}, + number={2}, + pages={155--166}, + year={2009}, + publisher={Springer} +} + +@article{gao2003complete, + title={Complete solution classification for the perspective-three-point problem}, + author={Gao, Xiao-Shan and Hou, Xiao-Rong and Tang, Jianliang and Cheng, Hang-Fei}, + journal={Pattern Analysis and Machine Intelligence, IEEE Transactions on}, + volume={25}, + number={8}, + pages={930--943}, + year={2003}, + publisher={IEEE} +} + +@inproceedings{hesch2011direct, + title={A direct least-squares (DLS) method for PnP}, + author={Hesch, Joel and Roumeliotis, Stergios and others}, + booktitle={Computer Vision (ICCV), 2011 IEEE International Conference on}, + pages={383--390}, + year={2011}, + organization={IEEE} +} + +@article{penate2013exhaustive, + title={Exhaustive linearization for robust camera pose and focal length estimation}, + author={Penate-Sanchez, Adrian and Andrade-Cetto, Juan and Moreno-Noguer, Francesc}, + journal={Pattern Analysis and Machine Intelligence, IEEE Transactions on}, + volume={35}, + number={10}, + pages={2387--2400}, + year={2013}, + publisher={IEEE} +} diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index 21fbf9ddd..c52356a75 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -99,14 +99,50 @@ v = f_y*y' + c_y Real lenses usually have some distortion, mostly radial distortion and slight tangential distortion. So, the above model is extended as: -\f[\begin{array}{l} \vecthree{x}{y}{z} = R \vecthree{X}{Y}{Z} + t \\ x' = x/z \\ y' = y/z \\ x'' = x' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + 2 p_1 x' y' + p_2(r^2 + 2 x'^2) + s_1 r^2 + s_2 r^4 \\ y'' = y' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' + s_3 r^2 + s_4 r^4 \\ \text{where} \quad r^2 = x'^2 + y'^2 \\ u = f_x*x'' + c_x \\ v = f_y*y'' + c_y \end{array}\f] +\f[\begin{array}{l} +\vecthree{x}{y}{z} = R \vecthree{X}{Y}{Z} + t \\ +x' = x/z \\ +y' = y/z \\ +x'' = x' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + 2 p_1 x' y' + p_2(r^2 + 2 x'^2) + s_1 r^2 + s_2 r^4 \\ +y'' = y' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' + s_3 r^2 + s_4 r^4 \\ +\text{where} \quad r^2 = x'^2 + y'^2 \\ +u = f_x*x'' + c_x \\ +v = f_y*y'' + c_y +\end{array}\f] \f$k_1\f$, \f$k_2\f$, \f$k_3\f$, \f$k_4\f$, \f$k_5\f$, and \f$k_6\f$ are radial distortion coefficients. \f$p_1\f$ and \f$p_2\f$ are tangential distortion coefficients. \f$s_1\f$, \f$s_2\f$, \f$s_3\f$, and \f$s_4\f$, are the thin prism distortion -coefficients. Higher-order coefficients are not considered in OpenCV. In the functions below the -coefficients are passed or returned as +coefficients. Higher-order coefficients are not considered in OpenCV. -\f[(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f] +In some cases the image sensor may be tilted in order to focus an oblique plane in front of the +camera (Scheimpfug condition). This can be useful for particle image velocimetry (PIV) or +triangulation with a laser fan. The tilt causes a perspective distortion of \f$x''\f$ and +\f$y''\f$. This distortion can be modelled in the following way, see e.g. @cite Louhichi07. + +\f[\begin{array}{l} +s\vecthree{x'''}{y'''}{1} = +\vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}(\tau_x, \tau_y)} +{0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)} +{0}{0}{1} R(\tau_x, \tau_y) \vecthree{x''}{y''}{1}\\ +u = f_x*x''' + c_x \\ +v = f_y*y''' + c_y +\end{array}\f] + +where the matrix \f$R(\tau_x, \tau_y)\f$ is defined by two rotations with angular parameter \f$\tau_x\f$ +and \f$\tau_y\f$, respectively, + +\f[ +R(\tau_x, \tau_y) = +\vecthreethree{\cos(\tau_y)}{0}{-\sin(\tau_y)}{0}{1}{0}{\sin(\tau_y)}{0}{\cos(\tau_y)} +\vecthreethree{1}{0}{0}{0}{\cos(\tau_x)}{\sin(\tau_x)}{0}{-\sin(\tau_x)}{\cos(\tau_x)} = +\vecthreethree{\cos(\tau_y)}{\sin(\tau_y)\sin(\tau_x)}{-\sin(\tau_y)\cos(\tau_x)} +{0}{\cos(\tau_x)}{\sin(\tau_x)} +{\sin(\tau_y)}{-\cos(\tau_y)\sin(\tau_x)}{\cos(\tau_y)\cos(\tau_x)}. +\f] + +In the functions below the coefficients are passed or returned as + +\f[(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f] vector. That is, if the vector contains four elements, it means that \f$k_3=0\f$ . The distortion coefficients do not depend on the scene viewed. Thus, they also belong to the intrinsic camera @@ -139,7 +175,7 @@ pattern (every view is described by several 3D-2D point correspondences). - A calibration example on stereo matching can be found at opencv_source_code/samples/cpp/stereo_match.cpp - (Python) A camera calibration sample can be found at - opencv_source_code/samples/python2/calibrate.py + opencv_source_code/samples/python/calibrate.py @{ @defgroup calib3d_fisheye Fisheye camera model @@ -164,12 +200,12 @@ pattern (every view is described by several 3D-2D point correspondences). The distorted point coordinates are [x'; y'] where - \f[x' = (\theta_d / r) x \\ y' = (\theta_d / r) y \f] + \f[x' = (\theta_d / r) a \\ y' = (\theta_d / r) b \f] Finally, conversion into pixel coordinates: The final pixel coordinates vector [u; v] where: \f[u = f_x (x' + \alpha y') + c_x \\ - v = f_y yy + c_y\f] + v = f_y y' + c_y\f] @defgroup calib3d_c C API @@ -189,10 +225,10 @@ enum { LMEDS = 4, //!< least-median algorithm }; enum { SOLVEPNP_ITERATIVE = 0, - SOLVEPNP_EPNP = 1, // F.Moreno-Noguer, V.Lepetit and P.Fua "EPnP: Efficient Perspective-n-Point Camera Pose Estimation" - SOLVEPNP_P3P = 2, // X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang; "Complete Solution Classification for the Perspective-Three-Point Problem" - SOLVEPNP_DLS = 3, // Joel A. Hesch and Stergios I. Roumeliotis. "A Direct Least-Squares (DLS) Method for PnP" - SOLVEPNP_UPNP = 4 // A.Penate-Sanchez, J.Andrade-Cetto, F.Moreno-Noguer. "Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation" + SOLVEPNP_EPNP = 1, //!< EPnP: Efficient Perspective-n-Point Camera Pose Estimation @cite lepetit2009epnp + SOLVEPNP_P3P = 2, //!< Complete Solution Classification for the Perspective-Three-Point Problem @cite gao2003complete + SOLVEPNP_DLS = 3, //!< A Direct Least-Squares (DLS) Method for PnP @cite hesch2011direct + SOLVEPNP_UPNP = 4 //!< Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation @cite penate2013exhaustive }; @@ -221,11 +257,14 @@ enum { CALIB_USE_INTRINSIC_GUESS = 0x00001, CALIB_RATIONAL_MODEL = 0x04000, CALIB_THIN_PRISM_MODEL = 0x08000, CALIB_FIX_S1_S2_S3_S4 = 0x10000, + CALIB_TILTED_MODEL = 0x40000, + CALIB_FIX_TAUX_TAUY = 0x80000, // only for stereo CALIB_FIX_INTRINSIC = 0x00100, CALIB_SAME_FOCAL_LENGTH = 0x00200, // for stereo rectification - CALIB_ZERO_DISPARITY = 0x00400 + CALIB_ZERO_DISPARITY = 0x00400, + CALIB_USE_LU = (1 << 17), //!< use LU instead of SVD decomposition for solving. much faster but potentially less precise }; //! the algorithm for finding fundamental matrix @@ -443,8 +482,8 @@ vector\ ), where N is the number of points in the view. @param tvec Translation vector. @param cameraMatrix Camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements. If -the vector is empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. If the vector is empty, the zero distortion coefficients are assumed. @param imagePoints Output array of image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, or vector\ . @param jacobian Optional output 2Nx(10+\) jacobian matrix of derivatives of image @@ -476,14 +515,15 @@ CV_EXPORTS_W void projectPoints( InputArray objectPoints, /** @brief Finds an object pose from 3D-2D point correspondences. -@param objectPoints Array of object points in the object coordinate space, 3xN/Nx3 1-channel or +@param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector\ can be also passed here. -@param imagePoints Array of corresponding image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, +@param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector\ can be also passed here. @param cameraMatrix Input camera matrix \f$A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements. If -the vector is NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are +assumed. @param rvec Output rotation vector (see Rodrigues ) that, together with tvec , brings points from the model coordinate system to the camera coordinate system. @param tvec Output translation vector. @@ -513,7 +553,7 @@ projections, as well as the camera matrix and the distortion coefficients. @note - An example of how to use solvePnP for planar augmented reality can be found at - opencv_source_code/samples/python2/plane_ar.py + opencv_source_code/samples/python/plane_ar.py - If you are using Python: - Numpy array slices won't work as input because solvePnP requires contiguous arrays (enforced by the assertion using cv::Mat::checkVector() around line 55 of @@ -532,14 +572,15 @@ CV_EXPORTS_W bool solvePnP( InputArray objectPoints, InputArray imagePoints, /** @brief Finds an object pose from 3D-2D point correspondences using the RANSAC scheme. -@param objectPoints Array of object points in the object coordinate space, 3xN/Nx3 1-channel or +@param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel, where N is the number of points. vector\ can be also passed here. -@param imagePoints Array of corresponding image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, +@param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, where N is the number of points. vector\ can be also passed here. @param cameraMatrix Input camera matrix \f$A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements. If -the vector is NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are +assumed. @param rvec Output rotation vector (see Rodrigues ) that, together with tvec , brings points from the model coordinate system to the camera coordinate system. @param tvec Output translation vector. @@ -718,7 +759,8 @@ together. and/or CV_CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be initialized before calling the function. @param distCoeffs Output vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. @param rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each pattern view (e.g. std::vector>). That is, each k-th rotation vector together with the corresponding k-th translation vector (see the next output parameter description) brings the calibration pattern @@ -754,6 +796,13 @@ set, the function computes and returns only 5 distortion coefficients. - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during the optimization. If CV_CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0. +- **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the +backward compatibility, this extra flag should be explicitly specified to make the +calibration function use the tilted sensor model and return 14 coefficients. If the flag is not +set, the function computes and returns only 5 distortion coefficients. +- **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during +the optimization. If CV_CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the +supplied distCoeffs matrix is used. Otherwise, it is set to 0. @param criteria Termination criteria for the iterative optimization algorithm. The function estimates the intrinsic camera parameters and extrinsic parameters for each of the @@ -838,8 +887,8 @@ any of CV_CALIB_USE_INTRINSIC_GUESS , CV_CALIB_FIX_ASPECT_RATIO , CV_CALIB_FIX_INTRINSIC , or CV_CALIB_FIX_FOCAL_LENGTH are specified, some or all of the matrix components must be initialized. See the flags description for details. @param distCoeffs1 Input/output vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 ot 12 elements. The -output vector length depends on the flags. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. The output vector length depends on the flags. @param cameraMatrix2 Input/output second camera matrix. The parameter is similar to cameraMatrix1 @param distCoeffs2 Input/output lens distortion coefficients for the second camera. The parameter is similar to distCoeffs1 . @@ -874,6 +923,13 @@ set, the function computes and returns only 5 distortion coefficients. - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during the optimization. If CV_CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0. +- **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the +backward compatibility, this extra flag should be explicitly specified to make the +calibration function use the tilted sensor model and return 14 coefficients. If the flag is not +set, the function computes and returns only 5 distortion coefficients. +- **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during +the optimization. If CV_CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the +supplied distCoeffs matrix is used. Otherwise, it is set to 0. @param criteria Termination criteria for the iterative optimization algorithm. The function estimates transformation between two cameras making a stereo pair. If you have a stereo @@ -922,8 +978,8 @@ CV_EXPORTS_W double stereoCalibrate( InputArrayOfArrays objectPoints, /** @brief Computes rectification transforms for each head of a calibrated stereo camera. @param cameraMatrix1 First camera matrix. -@param cameraMatrix2 Second camera matrix. @param distCoeffs1 First camera distortion parameters. +@param cameraMatrix2 Second camera matrix. @param distCoeffs2 Second camera distortion parameters. @param imageSize Size of the image used for stereo calibration. @param R Rotation matrix between the coordinate systems of the first and the second cameras. @@ -1057,8 +1113,9 @@ CV_EXPORTS_W float rectify3Collinear( InputArray cameraMatrix1, InputArray distC @param cameraMatrix Input camera matrix. @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements. If -the vector is NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of +4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are +assumed. @param imageSize Original image size. @param alpha Free scaling parameter between 0 (when all the pixels in the undistorted image are valid) and 1 (when all the source image pixels are retained in the undistorted image). See @@ -1179,6 +1236,39 @@ CV_EXPORTS Mat findFundamentalMat( InputArray points1, InputArray points2, /** @brief Calculates an essential matrix from the corresponding points in two images. +@param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should +be floating-point (single or double precision). +@param points2 Array of the second image points of the same size and format as points1 . +@param cameraMatrix Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . +Note that this function assumes that points1 and points2 are feature points from cameras with the +same camera matrix. +@param method Method for computing a fundamental matrix. +- **RANSAC** for the RANSAC algorithm. +- **MEDS** for the LMedS algorithm. +@param prob Parameter used for the RANSAC or LMedS methods only. It specifies a desirable level of +confidence (probability) that the estimated matrix is correct. +@param threshold Parameter used for RANSAC. It is the maximum distance from a point to an epipolar +line in pixels, beyond which the point is considered an outlier and is not used for computing the +final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the +point localization, image resolution, and the image noise. +@param mask Output array of N elements, every element of which is set to 0 for outliers and to 1 +for the other points. The array is computed only in the RANSAC and LMedS methods. + +This function estimates essential matrix based on the five-point algorithm solver in @cite Nister03 . +@cite SteweniusCFS is also a related. The epipolar geometry is described by the following equation: + +\f[[p_2; 1]^T K^{-T} E K^{-1} [p_1; 1] = 0\f] + +where \f$E\f$ is an essential matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the +second images, respectively. The result of this function may be passed further to +decomposeEssentialMat or recoverPose to recover the relative pose between cameras. + */ +CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2, + InputArray cameraMatrix, int method = RANSAC, + double prob = 0.999, double threshold = 1.0, + OutputArray mask = noArray() ); + +/** @overload @param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should be floating-point (single or double precision). @param points2 Array of the second image points of the same size and format as points1 . @@ -1197,19 +1287,15 @@ confidence (probability) that the estimated matrix is correct. @param mask Output array of N elements, every element of which is set to 0 for outliers and to 1 for the other points. The array is computed only in the RANSAC and LMedS methods. -This function estimates essential matrix based on the five-point algorithm solver in @cite Nister03 . -@cite SteweniusCFS is also a related. The epipolar geometry is described by the following equation: +This function differs from the one above that it computes camera matrix from focal length and +principal point: -\f[[p_2; 1]^T K^{-T} E K^{-1} [p_1; 1] = 0 \\\f]\f[K = +\f[K = \begin{bmatrix} f & 0 & x_{pp} \\ 0 & f & y_{pp} \\ 0 & 0 & 1 \end{bmatrix}\f] - -where \f$E\f$ is an essential matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the -second images, respectively. The result of this function may be passed further to -decomposeEssentialMat or recoverPose to recover the relative pose between cameras. */ CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2, double focal = 1.0, Point2d pp = Point2d(0, 0), @@ -1237,11 +1323,11 @@ the check. @param points1 Array of N 2D points from the first image. The point coordinates should be floating-point (single or double precision). @param points2 Array of the second image points of the same size and format as points1 . +@param cameraMatrix Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . +Note that this function assumes that points1 and points2 are feature points from cameras with the +same camera matrix. @param R Recovered relative rotation. @param t Recoverd relative translation. -@param focal Focal length of the camera. Note that this function assumes that points1 and points2 -are feature points from cameras with same focal length and principle point. -@param pp Principle point of the camera. @param mask Input/output mask for inliers in points1 and points2. : If it is not empty, then it marks inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to recover pose. In the output mask only inliers @@ -1265,20 +1351,49 @@ points1 and points2 are the same input for findEssentialMat. : points2[i] = ...; } - double focal = 1.0; - cv::Point2d pp(0.0, 0.0); + // cametra matrix with both focal lengths = 1, and principal point = (0, 0) + Mat cameraMatrix = Mat::eye(3, 3, CV_64F); + Mat E, R, t, mask; - E = findEssentialMat(points1, points2, focal, pp, RANSAC, 0.999, 1.0, mask); - recoverPose(E, points1, points2, R, t, focal, pp, mask); + E = findEssentialMat(points1, points2, cameraMatrix, RANSAC, 0.999, 1.0, mask); + recoverPose(E, points1, points2, cameraMatrix, R, t, mask); @endcode */ +CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2, + InputArray cameraMatrix, OutputArray R, OutputArray t, + InputOutputArray mask = noArray() ); + +/** @overload +@param E The input essential matrix. +@param points1 Array of N 2D points from the first image. The point coordinates should be +floating-point (single or double precision). +@param points2 Array of the second image points of the same size and format as points1 . +@param R Recovered relative rotation. +@param t Recoverd relative translation. +@param focal Focal length of the camera. Note that this function assumes that points1 and points2 +are feature points from cameras with same focal length and principle point. +@param pp Principle point of the camera. +@param mask Input/output mask for inliers in points1 and points2. +: If it is not empty, then it marks inliers in points1 and points2 for then given essential +matrix E. Only these inliers will be used to recover pose. In the output mask only inliers +which pass the cheirality check. + +This function differs from the one above that it computes camera matrix from focal length and +principal point: + +\f[K = +\begin{bmatrix} +f & 0 & x_{pp} \\ +0 & f & y_{pp} \\ +0 & 0 & 1 +\end{bmatrix}\f] + */ CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2, OutputArray R, OutputArray t, double focal = 1.0, Point2d pp = Point2d(0, 0), InputOutputArray mask = noArray() ); - /** @brief For points in an image of a stereo pair, computes the corresponding epilines in the other image. @param points Input points. \f$N \times 1\f$ or \f$1 \times N\f$ matrix of type CV_32FC2 or @@ -1403,6 +1518,17 @@ CV_EXPORTS_W void reprojectImageTo3D( InputArray disparity, bool handleMissingValues = false, int ddepth = -1 ); +/** @brief Calculates the Sampson Distance between two points. + +The function sampsonDistance calculates and returns the first order approximation of the geometric error as: +\f[sd( \texttt{pt1} , \texttt{pt2} )= \frac{(\texttt{pt2}^t \cdot \texttt{F} \cdot \texttt{pt1})^2}{(\texttt{F} \cdot \texttt{pt1})(0) + (\texttt{F} \cdot \texttt{pt1})(1) + (\texttt{F}^t \cdot \texttt{pt2})(0) + (\texttt{F}^t \cdot \texttt{pt2})(1)}\f] +The fundamental matrix may be calculated using the cv::findFundamentalMat function. See HZ 11.4.3 for details. +@param pt1 first homogeneous 2d point +@param pt2 second homogeneous 2d point +@param F fundamental matrix +*/ +CV_EXPORTS_W double sampsonDistance(InputArray pt1, InputArray pt2, InputArray F); + /** @brief Computes an optimal affine transformation between two 3D point sets. @param src First input 3D point set. @@ -1548,7 +1674,7 @@ check, quadratic interpolation and speckle filtering). @note - (Python) An example illustrating the use of the StereoSGBM matching algorithm can be found - at opencv_source_code/samples/python2/stereo_match.py + at opencv_source_code/samples/python/stereo_match.py */ class CV_EXPORTS_W StereoSGBM : public StereoMatcher { diff --git a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h index 239269238..0e77aa883 100644 --- a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h +++ b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h @@ -243,6 +243,8 @@ CVAPI(void) cvDrawChessboardCorners( CvArr* image, CvSize pattern_size, #define CV_CALIB_RATIONAL_MODEL 16384 #define CV_CALIB_THIN_PRISM_MODEL 32768 #define CV_CALIB_FIX_S1_S2_S3_S4 65536 +#define CV_CALIB_TILTED_MODEL 262144 +#define CV_CALIB_FIX_TAUX_TAUY 524288 /* Finds intrinsic and extrinsic camera parameters @@ -415,6 +417,7 @@ public: int state; int iters; bool completeSymmFlag; + int solveMethod; }; #endif diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index fc0a267be..b8f569aeb 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -42,6 +42,7 @@ #include "precomp.hpp" #include "opencv2/imgproc/imgproc_c.h" +#include "opencv2/imgproc/detail/distortion_model.hpp" #include "opencv2/calib3d/calib3d_c.h" #include #include @@ -286,7 +287,6 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) if( src->cols == 1 || src->rows == 1 ) { - double rx, ry, rz, theta; int step = src->rows > 1 ? src->step / elem_size : 1; if( src->rows + src->cols*CV_MAT_CN(src->type) - 1 != 3 ) @@ -295,19 +295,21 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) if( dst->rows != 3 || dst->cols != 3 || CV_MAT_CN(dst->type) != 1 ) CV_Error( CV_StsBadSize, "Output matrix must be 3x3, single-channel floating point matrix" ); + Point3d r; if( depth == CV_32F ) { - rx = src->data.fl[0]; - ry = src->data.fl[step]; - rz = src->data.fl[step*2]; + r.x = src->data.fl[0]; + r.y = src->data.fl[step]; + r.z = src->data.fl[step*2]; } else { - rx = src->data.db[0]; - ry = src->data.db[step]; - rz = src->data.db[step*2]; + r.x = src->data.db[0]; + r.y = src->data.db[step]; + r.z = src->data.db[step*2]; } - theta = std::sqrt(rx*rx + ry*ry + rz*rz); + + double theta = norm(r); if( theta < DBL_EPSILON ) { @@ -322,54 +324,48 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) } else { - const double I[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; - double c = cos(theta); double s = sin(theta); double c1 = 1. - c; double itheta = theta ? 1./theta : 0.; - rx *= itheta; ry *= itheta; rz *= itheta; + r *= itheta; - double rrt[] = { rx*rx, rx*ry, rx*rz, rx*ry, ry*ry, ry*rz, rx*rz, ry*rz, rz*rz }; - double _r_x_[] = { 0, -rz, ry, rz, 0, -rx, -ry, rx, 0 }; - double R[9]; - CvMat matR = cvMat( 3, 3, CV_64F, R ); + Matx33d rrt( r.x*r.x, r.x*r.y, r.x*r.z, r.x*r.y, r.y*r.y, r.y*r.z, r.x*r.z, r.y*r.z, r.z*r.z ); + Matx33d r_x( 0, -r.z, r.y, + r.z, 0, -r.x, + -r.y, r.x, 0 ); // R = cos(theta)*I + (1 - cos(theta))*r*rT + sin(theta)*[r_x] - // where [r_x] is [0 -rz ry; rz 0 -rx; -ry rx 0] - for( k = 0; k < 9; k++ ) - R[k] = c*I[k] + c1*rrt[k] + s*_r_x_[k]; + Matx33d R = c*Matx33d::eye() + c1*rrt + s*r_x; - cvConvert( &matR, dst ); + Mat(R).convertTo(cvarrToMat(dst), dst->type); if( jacobian ) { - double drrt[] = { rx+rx, ry, rz, ry, 0, 0, rz, 0, 0, - 0, rx, 0, rx, ry+ry, rz, 0, rz, 0, - 0, 0, rx, 0, 0, ry, rx, ry, rz+rz }; + const double I[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; + double drrt[] = { r.x+r.x, r.y, r.z, r.y, 0, 0, r.z, 0, 0, + 0, r.x, 0, r.x, r.y+r.y, r.z, 0, r.z, 0, + 0, 0, r.x, 0, 0, r.y, r.x, r.y, r.z+r.z }; double d_r_x_[] = { 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0 }; for( i = 0; i < 3; i++ ) { - double ri = i == 0 ? rx : i == 1 ? ry : rz; + double ri = i == 0 ? r.x : i == 1 ? r.y : r.z; double a0 = -s*ri, a1 = (s - 2*c1*itheta)*ri, a2 = c1*itheta; double a3 = (c - s*itheta)*ri, a4 = s*itheta; for( k = 0; k < 9; k++ ) - J[i*9+k] = a0*I[k] + a1*rrt[k] + a2*drrt[i*9+k] + - a3*_r_x_[k] + a4*d_r_x_[i*9+k]; + J[i*9+k] = a0*I[k] + a1*rrt.val[k] + a2*drrt[i*9+k] + + a3*r_x.val[k] + a4*d_r_x_[i*9+k]; } } } } else if( src->cols == 3 && src->rows == 3 ) { - double R[9], U[9], V[9], W[3], rx, ry, rz; - CvMat matR = cvMat( 3, 3, CV_64F, R ); - CvMat matU = cvMat( 3, 3, CV_64F, U ); - CvMat matV = cvMat( 3, 3, CV_64F, V ); - CvMat matW = cvMat( 3, 1, CV_64F, W ); + Matx33d U, Vt; + Vec3d W; double theta, s, c; int step = dst->rows > 1 ? dst->step / elem_size : 1; @@ -377,8 +373,9 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) (dst->rows != 3 || dst->cols != 1 || CV_MAT_CN(dst->type) != 1)) CV_Error( CV_StsBadSize, "Output matrix must be 1x3 or 3x1" ); - cvConvert( src, &matR ); - if( !cvCheckArr( &matR, CV_CHECK_RANGE+CV_CHECK_QUIET, -100, 100 ) ) + Matx33d R = cvarrToMat(src); + + if( !checkRange(R, true, NULL, -100, 100) ) { cvZero(dst); if( jacobian ) @@ -386,15 +383,13 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) return 0; } - cvSVD( &matR, &matW, &matU, &matV, CV_SVD_MODIFY_A + CV_SVD_U_T + CV_SVD_V_T ); - cvGEMM( &matU, &matV, 1, 0, 0, &matR, CV_GEMM_A_T ); + SVD::compute(R, W, U, Vt); + R = U*Vt; - rx = R[7] - R[5]; - ry = R[2] - R[6]; - rz = R[3] - R[1]; + Point3d r(R(2, 1) - R(1, 2), R(0, 2) - R(2, 0), R(1, 0) - R(0, 1)); - s = std::sqrt((rx*rx + ry*ry + rz*rz)*0.25); - c = (R[0] + R[4] + R[8] - 1)*0.5; + s = std::sqrt((r.x*r.x + r.y*r.y + r.z*r.z)*0.25); + c = (R(0, 0) + R(1, 1) + R(2, 2) - 1)*0.5; c = c > 1. ? 1. : c < -1. ? -1. : c; theta = acos(c); @@ -403,21 +398,19 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) double t; if( c > 0 ) - rx = ry = rz = 0; + r = Point3d(0, 0, 0); else { - t = (R[0] + 1)*0.5; - rx = std::sqrt(MAX(t,0.)); - t = (R[4] + 1)*0.5; - ry = std::sqrt(MAX(t,0.))*(R[1] < 0 ? -1. : 1.); - t = (R[8] + 1)*0.5; - rz = std::sqrt(MAX(t,0.))*(R[2] < 0 ? -1. : 1.); - if( fabs(rx) < fabs(ry) && fabs(rx) < fabs(rz) && (R[5] > 0) != (ry*rz > 0) ) - rz = -rz; - theta /= std::sqrt(rx*rx + ry*ry + rz*rz); - rx *= theta; - ry *= theta; - rz *= theta; + t = (R(0, 0) + 1)*0.5; + r.x = std::sqrt(MAX(t,0.)); + t = (R(1, 1) + 1)*0.5; + r.y = std::sqrt(MAX(t,0.))*(R(0, 1) < 0 ? -1. : 1.); + t = (R(2, 2) + 1)*0.5; + r.z = std::sqrt(MAX(t,0.))*(R(0, 2) < 0 ? -1. : 1.); + if( fabs(r.x) < fabs(r.y) && fabs(r.x) < fabs(r.z) && (R(1, 2) > 0) != (r.y*r.z > 0) ) + r.z = -r.z; + theta /= norm(r); + r *= theta; } if( jacobian ) @@ -454,16 +447,16 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) // var2 = [om;theta] double dvar2dvar[] = { - vth, 0, 0, rx, 0, - 0, vth, 0, ry, 0, - 0, 0, vth, rz, 0, + vth, 0, 0, r.x, 0, + 0, vth, 0, r.y, 0, + 0, 0, vth, r.z, 0, 0, 0, 0, 0, 1 }; double domegadvar2[] = { - theta, 0, 0, rx*vth, - 0, theta, 0, ry*vth, - 0, 0, theta, rz*vth + theta, 0, 0, r.x*vth, + 0, theta, 0, r.y*vth, + 0, 0, theta, r.z*vth }; CvMat _dvardR = cvMat( 5, 9, CV_64FC1, dvardR ); @@ -482,20 +475,20 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) } vth *= theta; - rx *= vth; ry *= vth; rz *= vth; + r *= vth; } if( depth == CV_32F ) { - dst->data.fl[0] = (float)rx; - dst->data.fl[step] = (float)ry; - dst->data.fl[step*2] = (float)rz; + dst->data.fl[0] = (float)r.x; + dst->data.fl[step] = (float)r.y; + dst->data.fl[step*2] = (float)r.z; } else { - dst->data.db[0] = rx; - dst->data.db[step] = ry; - dst->data.db[step*2] = rz; + dst->data.db[0] = r.x; + dst->data.db[step] = r.y; + dst->data.db[step*2] = r.z; } } @@ -523,7 +516,7 @@ CV_IMPL int cvRodrigues2( const CvMat* src, CvMat* dst, CvMat* jacobian ) } -static const char* cvDistCoeffErr = "Distortion coefficients must be 1x4, 4x1, 1x5, 5x1, 1x8, 8x1, 1x12 or 12x1 floating-point vector"; +static const char* cvDistCoeffErr = "Distortion coefficients must be 1x4, 4x1, 1x5, 5x1, 1x8, 8x1, 1x12, 12x1, 1x14 or 14x1 floating-point vector"; CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, const CvMat* r_vec, @@ -542,7 +535,10 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, int calc_derivatives; const CvPoint3D64f* M; CvPoint2D64f* m; - double r[3], R[9], dRdr[27], t[3], a[9], k[12] = {0,0,0,0,0,0,0,0,0,0,0,0}, fx, fy, cx, cy; + double r[3], R[9], dRdr[27], t[3], a[9], k[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, fx, fy, cx, cy; + Matx33d matTilt = Matx33d::eye(); + Matx33d dMatTiltdTauX(0,0,0,0,0,0,0,-1,0); + Matx33d dMatTiltdTauY(0,0,0,0,0,0,1,0,0); CvMat _r, _t, _a = cvMat( 3, 3, CV_64F, a ), _k; CvMat matR = cvMat( 3, 3, CV_64F, R ), _dRdr = cvMat( 3, 9, CV_64F, dRdr ); double *dpdr_p = 0, *dpdt_p = 0, *dpdk_p = 0, *dpdf_p = 0, *dpdc_p = 0; @@ -646,12 +642,18 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, (distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 4 && distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 5 && distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 8 && - distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 12) ) + distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 12 && + distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) != 14) ) CV_Error( CV_StsBadArg, cvDistCoeffErr ); _k = cvMat( distCoeffs->rows, distCoeffs->cols, CV_MAKETYPE(CV_64F,CV_MAT_CN(distCoeffs->type)), k ); cvConvert( distCoeffs, &_k ); + if(k[12] != 0 || k[13] != 0) + { + detail::computeTiltProjectionMatrix(k[12], k[13], + &matTilt, &dMatTiltdTauX, &dMatTiltdTauY); + } } if( dpdr ) @@ -728,8 +730,8 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, { if( !CV_IS_MAT(dpdk) || (CV_MAT_TYPE(dpdk->type) != CV_32FC1 && CV_MAT_TYPE(dpdk->type) != CV_64FC1) || - dpdk->rows != count*2 || (dpdk->cols != 12 && dpdk->cols != 8 && dpdk->cols != 5 && dpdk->cols != 4 && dpdk->cols != 2) ) - CV_Error( CV_StsBadArg, "dp/df must be 2Nx12, 2Nx8, 2Nx5, 2Nx4 or 2Nx2 floating-point matrix" ); + dpdk->rows != count*2 || (dpdk->cols != 14 && dpdk->cols != 12 && dpdk->cols != 8 && dpdk->cols != 5 && dpdk->cols != 4 && dpdk->cols != 2) ) + CV_Error( CV_StsBadArg, "dp/df must be 2Nx14, 2Nx12, 2Nx8, 2Nx5, 2Nx4 or 2Nx2 floating-point matrix" ); if( !distCoeffs ) CV_Error( CV_StsNullPtr, "distCoeffs is NULL while dpdk is not" ); @@ -753,7 +755,11 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, double y = R[3]*X + R[4]*Y + R[5]*Z + t[1]; double z = R[6]*X + R[7]*Y + R[8]*Z + t[2]; double r2, r4, r6, a1, a2, a3, cdist, icdist2; - double xd, yd; + double xd, yd, xd0, yd0, invProj; + Vec3d vecTilt; + Vec3d dVecTilt; + Matx22d dMatTilt; + Vec2d dXdYd; z = z ? 1./z : 1; x *= z; y *= z; @@ -766,8 +772,14 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, a3 = r2 + 2*y*y; cdist = 1 + k[0]*r2 + k[1]*r4 + k[4]*r6; icdist2 = 1./(1 + k[5]*r2 + k[6]*r4 + k[7]*r6); - xd = x*cdist*icdist2 + k[2]*a1 + k[3]*a2 + k[8]*r2+k[9]*r4; - yd = y*cdist*icdist2 + k[2]*a3 + k[3]*a1 + k[10]*r2+k[11]*r4; + xd0 = x*cdist*icdist2 + k[2]*a1 + k[3]*a2 + k[8]*r2+k[9]*r4; + yd0 = y*cdist*icdist2 + k[2]*a3 + k[3]*a1 + k[10]*r2+k[11]*r4; + + // additional distortion by projecting onto a tilt plane + vecTilt = matTilt*Vec3d(xd0, yd0, 1); + invProj = vecTilt(2) ? 1./vecTilt(2) : 1; + xd = invProj * vecTilt(0); + yd = invProj * vecTilt(1); m[i].x = xd*fx + cx; m[i].y = yd*fy + cy; @@ -798,42 +810,75 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, } dpdf_p += dpdf_step*2; } - + for (int row = 0; row < 2; ++row) + for (int col = 0; col < 2; ++col) + dMatTilt(row,col) = matTilt(row,col)*vecTilt(2) + - matTilt(2,col)*vecTilt(row); + double invProjSquare = (invProj*invProj); + dMatTilt *= invProjSquare; if( dpdk_p ) { - dpdk_p[0] = fx*x*icdist2*r2; - dpdk_p[1] = fx*x*icdist2*r4; - dpdk_p[dpdk_step] = fy*y*icdist2*r2; - dpdk_p[dpdk_step+1] = fy*y*icdist2*r4; + dXdYd = dMatTilt*Vec2d(x*icdist2*r2, y*icdist2*r2); + dpdk_p[0] = fx*dXdYd(0); + dpdk_p[dpdk_step] = fy*dXdYd(1); + dXdYd = dMatTilt*Vec2d(x*icdist2*r4, y*icdist2*r4); + dpdk_p[1] = fx*dXdYd(0); + dpdk_p[dpdk_step+1] = fy*dXdYd(1); if( _dpdk->cols > 2 ) { - dpdk_p[2] = fx*a1; - dpdk_p[3] = fx*a2; - dpdk_p[dpdk_step+2] = fy*a3; - dpdk_p[dpdk_step+3] = fy*a1; + dXdYd = dMatTilt*Vec2d(a1, a3); + dpdk_p[2] = fx*dXdYd(0); + dpdk_p[dpdk_step+2] = fy*dXdYd(1); + dXdYd = dMatTilt*Vec2d(a2, a1); + dpdk_p[3] = fx*dXdYd(0); + dpdk_p[dpdk_step+3] = fy*dXdYd(1); if( _dpdk->cols > 4 ) { - dpdk_p[4] = fx*x*icdist2*r6; - dpdk_p[dpdk_step+4] = fy*y*icdist2*r6; + dXdYd = dMatTilt*Vec2d(x*icdist2*r6, y*icdist2*r6); + dpdk_p[4] = fx*dXdYd(0); + dpdk_p[dpdk_step+4] = fy*dXdYd(1); if( _dpdk->cols > 5 ) { - dpdk_p[5] = fx*x*cdist*(-icdist2)*icdist2*r2; - dpdk_p[dpdk_step+5] = fy*y*cdist*(-icdist2)*icdist2*r2; - dpdk_p[6] = fx*x*cdist*(-icdist2)*icdist2*r4; - dpdk_p[dpdk_step+6] = fy*y*cdist*(-icdist2)*icdist2*r4; - dpdk_p[7] = fx*x*cdist*(-icdist2)*icdist2*r6; - dpdk_p[dpdk_step+7] = fy*y*cdist*(-icdist2)*icdist2*r6; + dXdYd = dMatTilt*Vec2d( + x*cdist*(-icdist2)*icdist2*r2, y*cdist*(-icdist2)*icdist2*r2); + dpdk_p[5] = fx*dXdYd(0); + dpdk_p[dpdk_step+5] = fy*dXdYd(1); + dXdYd = dMatTilt*Vec2d( + x*cdist*(-icdist2)*icdist2*r4, y*cdist*(-icdist2)*icdist2*r4); + dpdk_p[6] = fx*dXdYd(0); + dpdk_p[dpdk_step+6] = fy*dXdYd(1); + dXdYd = dMatTilt*Vec2d( + x*cdist*(-icdist2)*icdist2*r6, y*cdist*(-icdist2)*icdist2*r6); + dpdk_p[7] = fx*dXdYd(0); + dpdk_p[dpdk_step+7] = fy*dXdYd(1); if( _dpdk->cols > 8 ) { - dpdk_p[8] = fx*r2; //s1 - dpdk_p[9] = fx*r4; //s2 - dpdk_p[10] = 0;//s3 - dpdk_p[11] = 0;//s4 - dpdk_p[dpdk_step+8] = 0; //s1 - dpdk_p[dpdk_step+9] = 0; //s2 - dpdk_p[dpdk_step+10] = fy*r2; //s3 - dpdk_p[dpdk_step+11] = fy*r4; //s4 + dXdYd = dMatTilt*Vec2d(r2, 0); + dpdk_p[8] = fx*dXdYd(0); //s1 + dpdk_p[dpdk_step+8] = fy*dXdYd(1); //s1 + dXdYd = dMatTilt*Vec2d(r4, 0); + dpdk_p[9] = fx*dXdYd(0); //s2 + dpdk_p[dpdk_step+9] = fy*dXdYd(1); //s2 + dXdYd = dMatTilt*Vec2d(0, r2); + dpdk_p[10] = fx*dXdYd(0);//s3 + dpdk_p[dpdk_step+10] = fy*dXdYd(1); //s3 + dXdYd = dMatTilt*Vec2d(0, r4); + dpdk_p[11] = fx*dXdYd(0);//s4 + dpdk_p[dpdk_step+11] = fy*dXdYd(1); //s4 + if( _dpdk->cols > 12 ) + { + dVecTilt = dMatTiltdTauX * Vec3d(xd0, yd0, 1); + dpdk_p[12] = fx * invProjSquare * ( + dVecTilt(0) * vecTilt(2) - dVecTilt(2) * vecTilt(0)); + dpdk_p[dpdk_step+12] = fy*invProjSquare * ( + dVecTilt(1) * vecTilt(2) - dVecTilt(2) * vecTilt(1)); + dVecTilt = dMatTiltdTauY * Vec3d(xd0, yd0, 1); + dpdk_p[13] = fx * invProjSquare * ( + dVecTilt(0) * vecTilt(2) - dVecTilt(2) * vecTilt(0)); + dpdk_p[dpdk_step+13] = fy * invProjSquare * ( + dVecTilt(1) * vecTilt(2) - dVecTilt(2) * vecTilt(1)); + } } } } @@ -850,12 +895,13 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, double dcdist_dt = k[0]*dr2dt + 2*k[1]*r2*dr2dt + 3*k[4]*r4*dr2dt; double dicdist2_dt = -icdist2*icdist2*(k[5]*dr2dt + 2*k[6]*r2*dr2dt + 3*k[7]*r4*dr2dt); double da1dt = 2*(x*dydt[j] + y*dxdt[j]); - double dmxdt = fx*(dxdt[j]*cdist*icdist2 + x*dcdist_dt*icdist2 + x*cdist*dicdist2_dt + + double dmxdt = (dxdt[j]*cdist*icdist2 + x*dcdist_dt*icdist2 + x*cdist*dicdist2_dt + k[2]*da1dt + k[3]*(dr2dt + 2*x*dxdt[j]) + k[8]*dr2dt + 2*r2*k[9]*dr2dt); - double dmydt = fy*(dydt[j]*cdist*icdist2 + y*dcdist_dt*icdist2 + y*cdist*dicdist2_dt + + double dmydt = (dydt[j]*cdist*icdist2 + y*dcdist_dt*icdist2 + y*cdist*dicdist2_dt + k[2]*(dr2dt + 2*y*dydt[j]) + k[3]*da1dt + k[10]*dr2dt + 2*r2*k[11]*dr2dt); - dpdt_p[j] = dmxdt; - dpdt_p[dpdt_step+j] = dmydt; + dXdYd = dMatTilt*Vec2d(dmxdt, dmydt); + dpdt_p[j] = fx*dXdYd(0); + dpdt_p[dpdt_step+j] = fy*dXdYd(1); } dpdt_p += dpdt_step*2; } @@ -885,15 +931,16 @@ CV_IMPL void cvProjectPoints2( const CvMat* objectPoints, double dxdr = z*(dx0dr[j] - x*dz0dr[j]); double dydr = z*(dy0dr[j] - y*dz0dr[j]); double dr2dr = 2*x*dxdr + 2*y*dydr; - double dcdist_dr = k[0]*dr2dr + 2*k[1]*r2*dr2dr + 3*k[4]*r4*dr2dr; - double dicdist2_dr = -icdist2*icdist2*(k[5]*dr2dr + 2*k[6]*r2*dr2dr + 3*k[7]*r4*dr2dr); + double dcdist_dr = (k[0] + 2*k[1]*r2 + 3*k[4]*r4)*dr2dr; + double dicdist2_dr = -icdist2*icdist2*(k[5] + 2*k[6]*r2 + 3*k[7]*r4)*dr2dr; double da1dr = 2*(x*dydr + y*dxdr); - double dmxdr = fx*(dxdr*cdist*icdist2 + x*dcdist_dr*icdist2 + x*cdist*dicdist2_dr + - k[2]*da1dr + k[3]*(dr2dr + 2*x*dxdr) + k[8]*dr2dr + 2*r2*k[9]*dr2dr); - double dmydr = fy*(dydr*cdist*icdist2 + y*dcdist_dr*icdist2 + y*cdist*dicdist2_dr + - k[2]*(dr2dr + 2*y*dydr) + k[3]*da1dr + k[10]*dr2dr + 2*r2*k[11]*dr2dr); - dpdr_p[j] = dmxdr; - dpdr_p[dpdr_step+j] = dmydr; + double dmxdr = (dxdr*cdist*icdist2 + x*dcdist_dr*icdist2 + x*cdist*dicdist2_dr + + k[2]*da1dr + k[3]*(dr2dr + 2*x*dxdr) + (k[8] + 2*r2*k[9])*dr2dr); + double dmydr = (dydr*cdist*icdist2 + y*dcdist_dr*icdist2 + y*cdist*dicdist2_dr + + k[2]*(dr2dr + 2*y*dydr) + k[3]*da1dr + (k[10] + 2*r2*k[11])*dr2dr); + dXdYd = dMatTilt*Vec2d(dmxdr, dmydr); + dpdr_p[j] = fx*dXdYd(0); + dpdr_p[dpdr_step+j] = fy*dXdYd(1); } dpdr_p += dpdr_step*2; } @@ -1231,12 +1278,11 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, CvSize imageSize, CvMat* cameraMatrix, CvMat* distCoeffs, CvMat* rvecs, CvMat* tvecs, int flags, CvTermCriteria termCrit ) { - const int NINTRINSIC = 16; - CvLevMarq solver; + const int NINTRINSIC = 18; double reprojErr = 0; Matx33d A; - double k[12] = {0}; + double k[14] = {0}; CvMat matA = cvMat(3, 3, CV_64F, A.val), _k; int i, nimages, maxPoints = 0, ni = 0, pos, total = 0, nparams, npstep, cn; double aspectRatio = 0.; @@ -1253,9 +1299,19 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, (npoints->rows != 1 && npoints->cols != 1) ) CV_Error( CV_StsUnsupportedFormat, "the array of point counters must be 1-dimensional integer vector" ); - //when the thin prism model is used the distortion coefficients matrix must have 12 parameters - if((flags & CV_CALIB_THIN_PRISM_MODEL) && (distCoeffs->cols*distCoeffs->rows != 12)) - CV_Error( CV_StsBadArg, "Thin prism model must have 12 parameters in the distortion matrix" ); + if(flags & CV_CALIB_TILTED_MODEL) + { + //when the tilted sensor model is used the distortion coefficients matrix must have 14 parameters + if (distCoeffs->cols*distCoeffs->rows != 14) + CV_Error( CV_StsBadArg, "The tilted sensor model must have 14 parameters in the distortion matrix" ); + } + else + { + //when the thin prism model is used the distortion coefficients matrix must have 12 parameters + if(flags & CV_CALIB_THIN_PRISM_MODEL) + if (distCoeffs->cols*distCoeffs->rows != 12) + CV_Error( CV_StsBadArg, "Thin prism model must have 12 parameters in the distortion matrix" ); + } nimages = npoints->rows*npoints->cols; npstep = npoints->rows == 1 ? 1 : npoints->step/CV_ELEM_SIZE(npoints->type); @@ -1294,7 +1350,8 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, (distCoeffs->cols*distCoeffs->rows != 4 && distCoeffs->cols*distCoeffs->rows != 5 && distCoeffs->cols*distCoeffs->rows != 8 && - distCoeffs->cols*distCoeffs->rows != 12) ) + distCoeffs->cols*distCoeffs->rows != 12 && + distCoeffs->cols*distCoeffs->rows != 14) ) CV_Error( CV_StsBadArg, cvDistCoeffErr ); for( i = 0; i < nimages; i++ ) @@ -1332,14 +1389,14 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, if( distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) < 8 ) { if( distCoeffs->rows*distCoeffs->cols*CV_MAT_CN(distCoeffs->type) < 5 ) - flags |= CV_CALIB_FIX_K3; - flags |= CV_CALIB_FIX_K4 | CV_CALIB_FIX_K5 | CV_CALIB_FIX_K6; + flags |= CALIB_FIX_K3; + flags |= CALIB_FIX_K4 | CALIB_FIX_K5 | CALIB_FIX_K6; } const double minValidAspectRatio = 0.01; const double maxValidAspectRatio = 100.0; // 1. initialize intrinsic parameters & LM solver - if( flags & CV_CALIB_USE_INTRINSIC_GUESS ) + if( flags & CALIB_USE_INTRINSIC_GUESS ) { cvConvert( cameraMatrix, &matA ); if( A(0, 0) <= 0 || A(1, 1) <= 0 ) @@ -1356,7 +1413,7 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, A(0, 1) = A(1, 0) = A(2, 0) = A(2, 1) = 0.; A(2, 2) = 1.; - if( flags & CV_CALIB_FIX_ASPECT_RATIO ) + if( flags & CALIB_FIX_ASPECT_RATIO ) { aspectRatio = A(0, 0)/A(1, 1); @@ -1376,7 +1433,7 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, for( i = 0; i < total; i++ ) matM.at(i).z = 0.; - if( flags & CV_CALIB_FIX_ASPECT_RATIO ) + if( flags & CALIB_FIX_ASPECT_RATIO ) { aspectRatio = cvmGet(cameraMatrix,0,0); aspectRatio /= cvmGet(cameraMatrix,1,1); @@ -1388,14 +1445,18 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, cvInitIntrinsicParams2D( &_matM, &m, npoints, imageSize, &matA, aspectRatio ); } - solver.init( nparams, 0, termCrit ); + CvLevMarq solver( nparams, 0, termCrit ); + + if(flags & CALIB_USE_LU) { + solver.solveMethod = DECOMP_LU; + } { double* param = solver.param->data.db; uchar* mask = solver.mask->data.ptr; param[0] = A(0, 0); param[1] = A(1, 1); param[2] = A(0, 2); param[3] = A(1, 2); - std::copy(k, k + 12, param + 4); + std::copy(k, k + 14, param + 4); if( flags & CV_CALIB_FIX_FOCAL_LENGTH ) mask[0] = mask[1] = 0; @@ -1406,10 +1467,12 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, param[6] = param[7] = 0; mask[6] = mask[7] = 0; } - if( !(flags & CV_CALIB_RATIONAL_MODEL) ) - flags |= CV_CALIB_FIX_K4 + CV_CALIB_FIX_K5 + CV_CALIB_FIX_K6; + if( !(flags & CALIB_RATIONAL_MODEL) ) + flags |= CALIB_FIX_K4 + CALIB_FIX_K5 + CALIB_FIX_K6; if( !(flags & CV_CALIB_THIN_PRISM_MODEL)) flags |= CALIB_FIX_S1_S2_S3_S4; + if( !(flags & CV_CALIB_TILTED_MODEL)) + flags |= CALIB_FIX_TAUX_TAUY; mask[ 4] = !(flags & CALIB_FIX_K1); mask[ 5] = !(flags & CALIB_FIX_K2); @@ -1425,6 +1488,11 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, mask[14] = 0; mask[15] = 0; } + if(flags & CALIB_FIX_TAUX_TAUY) + { + mask[16] = 0; + mask[17] = 0; + } } // 2. initialize extrinsic parameters @@ -1451,14 +1519,14 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, bool proceed = solver.updateAlt( _param, _JtJ, _JtErr, _errNorm ); double *param = solver.param->data.db, *pparam = solver.prevParam->data.db; - if( flags & CV_CALIB_FIX_ASPECT_RATIO ) + if( flags & CALIB_FIX_ASPECT_RATIO ) { param[0] = param[1]*aspectRatio; pparam[0] = pparam[1]*aspectRatio; } A(0, 0) = param[0]; A(1, 1) = param[1]; A(0, 2) = param[2]; A(1, 2) = param[3]; - std::copy(param + 4, param + 4 + 12, k); + std::copy(param + 4, param + 4 + 14, k); if( !proceed ) break; @@ -1487,9 +1555,9 @@ CV_IMPL double cvCalibrateCamera2( const CvMat* objectPoints, if( solver.state == CvLevMarq::CALC_J ) { cvProjectPoints2( &_Mi, &_ri, &_ti, &matA, &_k, &_mp, &_dpdr, &_dpdt, - (flags & CV_CALIB_FIX_FOCAL_LENGTH) ? 0 : &_dpdf, - (flags & CV_CALIB_FIX_PRINCIPAL_POINT) ? 0 : &_dpdc, &_dpdk, - (flags & CV_CALIB_FIX_ASPECT_RATIO) ? aspectRatio : 0); + (flags & CALIB_FIX_FOCAL_LENGTH) ? 0 : &_dpdf, + (flags & CALIB_FIX_PRINCIPAL_POINT) ? 0 : &_dpdc, &_dpdk, + (flags & CALIB_FIX_ASPECT_RATIO) ? aspectRatio : 0); } else cvProjectPoints2( &_Mi, &_ri, &_ti, &matA, &_k, &_mp ); @@ -1633,12 +1701,11 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 int flags, CvTermCriteria termCrit ) { - const int NINTRINSIC = 16; + const int NINTRINSIC = 18; Ptr npoints, err, J_LR, Je, Ji, imagePoints[2], objectPoints, RT0; - CvLevMarq solver; double reprojErr = 0; - double A[2][9], dk[2][12]={{0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}}, rlr[9]; + double A[2][9], dk[2][14]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0}}, rlr[9]; CvMat K[2], Dist[2], om_LR, T_LR; CvMat R_LR = cvMat(3, 3, CV_64F, rlr); int i, k, p, ni = 0, ofs, nimages, pointsTotal, maxPoints = 0; @@ -1684,7 +1751,7 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 (_imagePoints1->rows == 1 && _imagePoints1->cols == pointsTotal && cn == 2)) ); K[k] = cvMat(3,3,CV_64F,A[k]); - Dist[k] = cvMat(1,12,CV_64F,dk[k]); + Dist[k] = cvMat(1,14,CV_64F,dk[k]); imagePoints[k].reset(cvCreateMat( points->rows, points->cols, CV_64FC(CV_MAT_CN(points->type)))); cvConvert( points, imagePoints[k] ); @@ -1737,7 +1804,12 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 // storage for initial [om(R){i}|t{i}] (in order to compute the median for each component) RT0.reset(cvCreateMat( 6, nimages, CV_64F )); - solver.init( nparams, 0, termCrit ); + CvLevMarq solver( nparams, 0, termCrit ); + + if(flags & CALIB_USE_LU) { + solver.solveMethod = DECOMP_LU; + } + if( recomputeIntrinsics ) { uchar* imask = solver.mask->data.ptr + nparams - NINTRINSIC*2; @@ -1745,6 +1817,8 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 flags |= CV_CALIB_FIX_K4 | CV_CALIB_FIX_K5 | CV_CALIB_FIX_K6; if( !(flags & CV_CALIB_THIN_PRISM_MODEL) ) flags |= CV_CALIB_FIX_S1_S2_S3_S4; + if( !(flags & CV_CALIB_TILTED_MODEL) ) + flags |= CV_CALIB_FIX_TAUX_TAUY; if( flags & CV_CALIB_FIX_ASPECT_RATIO ) imask[0] = imask[NINTRINSIC] = 0; if( flags & CV_CALIB_FIX_FOCAL_LENGTH ) @@ -1772,6 +1846,11 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 imask[14] = imask[NINTRINSIC+14] = 0; imask[15] = imask[NINTRINSIC+15] = 0; } + if( flags & CV_CALIB_FIX_TAUX_TAUY ) + { + imask[16] = imask[NINTRINSIC+16] = 0; + imask[17] = imask[NINTRINSIC+17] = 0; + } } /* @@ -1850,6 +1929,8 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 iparam[13] = dk[k][9]; iparam[14] = dk[k][10]; iparam[15] = dk[k][11]; + iparam[16] = dk[k][12]; + iparam[17] = dk[k][13]; } om_LR = cvMat(3, 1, CV_64F, solver.param->data.db); @@ -1920,6 +2001,8 @@ double cvStereoCalibrate( const CvMat* _objectPoints, const CvMat* _imagePoints1 dk[k][9] = iparam[k*NINTRINSIC+13]; dk[k][10] = iparam[k*NINTRINSIC+14]; dk[k][11] = iparam[k*NINTRINSIC+15]; + dk[k][12] = iparam[k*NINTRINSIC+16]; + dk[k][13] = iparam[k*NINTRINSIC+17]; } } @@ -3019,15 +3102,17 @@ static Mat prepareCameraMatrix(Mat& cameraMatrix0, int rtype) static Mat prepareDistCoeffs(Mat& distCoeffs0, int rtype) { - Mat distCoeffs = Mat::zeros(distCoeffs0.cols == 1 ? Size(1, 12) : Size(12, 1), rtype); + Mat distCoeffs = Mat::zeros(distCoeffs0.cols == 1 ? Size(1, 14) : Size(14, 1), rtype); if( distCoeffs0.size() == Size(1, 4) || distCoeffs0.size() == Size(1, 5) || distCoeffs0.size() == Size(1, 8) || distCoeffs0.size() == Size(1, 12) || + distCoeffs0.size() == Size(1, 14) || distCoeffs0.size() == Size(4, 1) || distCoeffs0.size() == Size(5, 1) || distCoeffs0.size() == Size(8, 1) || - distCoeffs0.size() == Size(12, 1) ) + distCoeffs0.size() == Size(12, 1) || + distCoeffs0.size() == Size(14, 1) ) { Mat dstCoeffs(distCoeffs, Rect(0, 0, distCoeffs0.cols, distCoeffs0.rows)); distCoeffs0.convertTo(dstCoeffs, rtype); @@ -3212,13 +3297,38 @@ double cv::calibrateCamera( InputArrayOfArrays _objectPoints, cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype); Mat distCoeffs = _distCoeffs.getMat(); distCoeffs = prepareDistCoeffs(distCoeffs, rtype); - if( !(flags & CALIB_RATIONAL_MODEL) &&(!(flags & CALIB_THIN_PRISM_MODEL))) + if( !(flags & CALIB_RATIONAL_MODEL) && + (!(flags & CALIB_THIN_PRISM_MODEL)) && + (!(flags & CALIB_TILTED_MODEL))) distCoeffs = distCoeffs.rows == 1 ? distCoeffs.colRange(0, 5) : distCoeffs.rowRange(0, 5); - int i; - size_t nimages = _objectPoints.total(); + int nimages = int(_objectPoints.total()); CV_Assert( nimages > 0 ); - Mat objPt, imgPt, npoints, rvecM((int)nimages, 3, CV_64FC1), tvecM((int)nimages, 3, CV_64FC1); + Mat objPt, imgPt, npoints, rvecM, tvecM; + + bool rvecs_needed = _rvecs.needed(), tvecs_needed = _tvecs.needed(); + + bool rvecs_mat_vec = _rvecs.isMatVector(); + bool tvecs_mat_vec = _tvecs.isMatVector(); + + if( rvecs_needed ) { + _rvecs.create(nimages, 1, CV_64FC3); + + if(rvecs_mat_vec) + rvecM.create(nimages, 3, CV_64F); + else + rvecM = _rvecs.getMat(); + } + + if( tvecs_needed ) { + _tvecs.create(nimages, 1, CV_64FC3); + + if(tvecs_mat_vec) + tvecM.create(nimages, 3, CV_64F); + else + tvecM = _tvecs.getMat(); + } + collectCalibrationData( _objectPoints, _imagePoints, noArray(), objPt, imgPt, 0, npoints ); CvMat c_objPt = objPt, c_imgPt = imgPt, c_npoints = npoints; @@ -3226,31 +3336,27 @@ double cv::calibrateCamera( InputArrayOfArrays _objectPoints, CvMat c_rvecM = rvecM, c_tvecM = tvecM; double reprojErr = cvCalibrateCamera2(&c_objPt, &c_imgPt, &c_npoints, imageSize, - &c_cameraMatrix, &c_distCoeffs, &c_rvecM, - &c_tvecM, flags, criteria ); + &c_cameraMatrix, &c_distCoeffs, + rvecs_needed ? &c_rvecM : NULL, + tvecs_needed ? &c_tvecM : NULL, flags, criteria ); - bool rvecs_needed = _rvecs.needed(), tvecs_needed = _tvecs.needed(); - - if( rvecs_needed ) - _rvecs.create((int)nimages, 1, CV_64FC3); - if( tvecs_needed ) - _tvecs.create((int)nimages, 1, CV_64FC3); - - for( i = 0; i < (int)nimages; i++ ) + // overly complicated and inefficient rvec/ tvec handling to support vector + for(int i = 0; i < nimages; i++ ) { - if( rvecs_needed ) + if( rvecs_needed && rvecs_mat_vec) { _rvecs.create(3, 1, CV_64F, i, true); Mat rv = _rvecs.getMat(i); - memcpy(rv.ptr(), rvecM.ptr(i), 3*sizeof(double)); + memcpy(rv.ptr(), rvecM.ptr(i), 3*sizeof(double)); } - if( tvecs_needed ) + if( tvecs_needed && tvecs_mat_vec) { _tvecs.create(3, 1, CV_64F, i, true); Mat tv = _tvecs.getMat(i); - memcpy(tv.ptr(), tvecM.ptr(i), 3*sizeof(double)); + memcpy(tv.ptr(), tvecM.ptr(i), 3*sizeof(double)); } } + cameraMatrix.copyTo(_cameraMatrix); distCoeffs.copyTo(_distCoeffs); @@ -3288,7 +3394,9 @@ double cv::stereoCalibrate( InputArrayOfArrays _objectPoints, distCoeffs1 = prepareDistCoeffs(distCoeffs1, rtype); distCoeffs2 = prepareDistCoeffs(distCoeffs2, rtype); - if( !(flags & CALIB_RATIONAL_MODEL) &&(!(flags & CALIB_THIN_PRISM_MODEL))) + if( !(flags & CALIB_RATIONAL_MODEL) && + (!(flags & CALIB_THIN_PRISM_MODEL)) && + (!(flags & CALIB_TILTED_MODEL))) { distCoeffs1 = distCoeffs1.rows == 1 ? distCoeffs1.colRange(0, 5) : distCoeffs1.rowRange(0, 5); distCoeffs2 = distCoeffs2.rows == 1 ? distCoeffs2.colRange(0, 5) : distCoeffs2.rowRange(0, 5); diff --git a/modules/calib3d/src/compat_ptsetreg.cpp b/modules/calib3d/src/compat_ptsetreg.cpp index d2ab252d3..e0f387da3 100644 --- a/modules/calib3d/src/compat_ptsetreg.cpp +++ b/modules/calib3d/src/compat_ptsetreg.cpp @@ -58,6 +58,7 @@ CvLevMarq::CvLevMarq() iters = 0; completeSymmFlag = false; errNorm = prevErrNorm = DBL_MAX; + solveMethod = cv::DECOMP_SVD; } CvLevMarq::CvLevMarq( int nparams, int nerrs, CvTermCriteria criteria0, bool _completeSymmFlag ) @@ -93,9 +94,6 @@ void CvLevMarq::init( int nparams, int nerrs, CvTermCriteria criteria0, bool _co prevParam.reset(cvCreateMat( nparams, 1, CV_64F )); param.reset(cvCreateMat( nparams, 1, CV_64F )); JtJ.reset(cvCreateMat( nparams, nparams, CV_64F )); - JtJN.reset(cvCreateMat( nparams, nparams, CV_64F )); - JtJV.reset(cvCreateMat( nparams, nparams, CV_64F )); - JtJW.reset(cvCreateMat( nparams, 1, CV_64F )); JtErr.reset(cvCreateMat( nparams, 1, CV_64F )); if( nerrs > 0 ) { @@ -116,6 +114,7 @@ void CvLevMarq::init( int nparams, int nerrs, CvTermCriteria criteria0, bool _co state = STARTED; iters = 0; completeSymmFlag = _completeSymmFlag; + solveMethod = cv::DECOMP_SVD; } bool CvLevMarq::update( const CvMat*& _param, CvMat*& matJ, CvMat*& _err ) @@ -256,6 +255,34 @@ bool CvLevMarq::updateAlt( const CvMat*& _param, CvMat*& _JtJ, CvMat*& _JtErr, d return true; } +namespace { +static void subMatrix(const cv::Mat& src, cv::Mat& dst, const std::vector& cols, + const std::vector& rows) { + int nonzeros_cols = cv::countNonZero(cols); + cv::Mat tmp(src.rows, nonzeros_cols, CV_64FC1); + + for (int i = 0, j = 0; i < (int)cols.size(); i++) + { + if (cols[i]) + { + src.col(i).copyTo(tmp.col(j++)); + } + } + + int nonzeros_rows = cv::countNonZero(rows); + dst.create(nonzeros_rows, nonzeros_cols, CV_64FC1); + for (int i = 0, j = 0; i < (int)rows.size(); i++) + { + if (rows[i]) + { + tmp.row(i).copyTo(dst.row(j++)); + } + } +} + +} + + void CvLevMarq::step() { using namespace cv; @@ -264,33 +291,36 @@ void CvLevMarq::step() int nparams = param->rows; Mat _JtJ = cvarrToMat(JtJ); - Mat _JtJN = cvarrToMat(JtJN); - Mat _JtJW = cvarrToMat(JtJW); - Mat _JtJV = cvarrToMat(JtJV); + Mat _mask = cvarrToMat(mask); - for( int i = 0; i < nparams; i++ ) - if( mask->data.ptr[i] == 0 ) - { - _JtJ.row(i) = 0; - _JtJ.col(i) = 0; - JtErr->data.db[i] = 0; - } + int nparams_nz = countNonZero(_mask); + if(!JtJN || JtJN->rows != nparams_nz) { + // prevent re-allocation in every step + JtJN.reset(cvCreateMat( nparams_nz, nparams_nz, CV_64F )); + JtJV.reset(cvCreateMat( nparams_nz, 1, CV_64F )); + JtJW.reset(cvCreateMat( nparams_nz, 1, CV_64F )); + } + + Mat _JtJN = cvarrToMat(JtJN); + Mat _JtErr = cvarrToMat(JtJV); + Mat_ nonzero_param = cvarrToMat(JtJW); + + subMatrix(cvarrToMat(JtErr), _JtErr, std::vector(1, 1), _mask); + subMatrix(_JtJ, _JtJN, _mask, _mask); if( !err ) - completeSymm( _JtJ, completeSymmFlag ); + completeSymm( _JtJN, completeSymmFlag ); - _JtJ.copyTo(_JtJN); #if 1 _JtJN.diag() *= 1. + lambda; #else _JtJN.diag() += lambda; #endif - // solve(JtJN, JtErr, param, DECOMP_SVD); - SVD::compute(_JtJN, _JtJW, noArray(), _JtJV, SVD::MODIFY_A); - SVD::backSubst(_JtJW, _JtJV.t(), _JtJV, cvarrToMat(JtErr), cvarrToMat(param)); + solve(_JtJN, _JtErr, nonzero_param, solveMethod); + int j = 0; for( int i = 0; i < nparams; i++ ) - param->data.db[i] = prevParam->data.db[i] - (mask->data.ptr[i] ? param->data.db[i] : 0); + param->data.db[i] = prevParam->data.db[i] - (mask->data.ptr[i] ? nonzero_param(j++) : 0); } diff --git a/modules/calib3d/src/fisheye.cpp b/modules/calib3d/src/fisheye.cpp index 78c066afb..3d737e7f4 100644 --- a/modules/calib3d/src/fisheye.cpp +++ b/modules/calib3d/src/fisheye.cpp @@ -53,7 +53,7 @@ namespace cv { namespace double dalpha; }; - void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std::vector& rows); + void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std::vector& rows); }} ////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -762,12 +762,12 @@ double cv::fisheye::calibrate(InputArrayOfArrays objectPoints, InputArrayOfArray double alpha_smooth2 = 1 - std::pow(1 - alpha_smooth, iter + 1.0); - Mat JJ2_inv, ex3; - ComputeJacobians(objectPoints, imagePoints, finalParam, omc, Tc, check_cond,thresh_cond, JJ2_inv, ex3); + Mat JJ2, ex3; + ComputeJacobians(objectPoints, imagePoints, finalParam, omc, Tc, check_cond,thresh_cond, JJ2, ex3); - Mat G = alpha_smooth2 * JJ2_inv * ex3; - - currentParam = finalParam + G; + Mat G; + solve(JJ2, ex3, G); + currentParam = finalParam + alpha_smooth2*G; change = norm(Vec4d(currentParam.f[0], currentParam.f[1], currentParam.c[0], currentParam.c[1]) - Vec4d(finalParam.f[0], finalParam.f[1], finalParam.c[0], finalParam.c[1])) @@ -899,7 +899,7 @@ double cv::fisheye::stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayO intrinsicLeft_errors.isEstimate = intrinsicLeft.isEstimate; intrinsicRight_errors.isEstimate = intrinsicRight.isEstimate; - std::vector selectedParams; + std::vector selectedParams; std::vector tmp(6 * (n_images + 1), 1); selectedParams.insert(selectedParams.end(), intrinsicLeft.isEstimate.begin(), intrinsicLeft.isEstimate.end()); selectedParams.insert(selectedParams.end(), intrinsicRight.isEstimate.begin(), intrinsicRight.isEstimate.end()); @@ -923,7 +923,6 @@ double cv::fisheye::stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayO cv::Mat J = cv::Mat::zeros(4 * n_points * n_images, 18 + 6 * (n_images + 1), CV_64FC1), e = cv::Mat::zeros(4 * n_points * n_images, 1, CV_64FC1), Jkk, ekk; - cv::Mat J2_inv; for(int iter = 0; ; ++iter) { @@ -1000,12 +999,11 @@ double cv::fisheye::stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayO cv::Vec6d oldTom(Tcur[0], Tcur[1], Tcur[2], omcur[0], omcur[1], omcur[2]); //update all parameters - cv::subMatrix(J, J, selectedParams, std::vector(J.rows, 1)); - cv::Mat J2 = J.t() * J; - J2_inv = J2.inv(); + cv::subMatrix(J, J, selectedParams, std::vector(J.rows, 1)); int a = cv::countNonZero(intrinsicLeft.isEstimate); int b = cv::countNonZero(intrinsicRight.isEstimate); - cv::Mat deltas = J2_inv * J.t() * e; + cv::Mat deltas; + solve(J.t() * J, J.t()*e, deltas); intrinsicLeft = intrinsicLeft + deltas.rowRange(0, a); intrinsicRight = intrinsicRight + deltas.rowRange(a, a + b); omcur = omcur + cv::Vec3d(deltas.rowRange(a + b, a + b + 3)); @@ -1052,12 +1050,12 @@ double cv::fisheye::stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayO } namespace cv{ namespace { -void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std::vector& rows) +void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std::vector& rows) { - CV_Assert(src.type() == CV_64FC1); + CV_Assert(src.channels() == 1); int nonzeros_cols = cv::countNonZero(cols); - Mat tmp(src.rows, nonzeros_cols, CV_64FC1); + Mat tmp(src.rows, nonzeros_cols, CV_64F); for (int i = 0, j = 0; i < (int)cols.size(); i++) { @@ -1068,16 +1066,14 @@ void subMatrix(const Mat& src, Mat& dst, const std::vector& cols, const std } int nonzeros_rows = cv::countNonZero(rows); - Mat tmp1(nonzeros_rows, nonzeros_cols, CV_64FC1); + dst.create(nonzeros_rows, nonzeros_cols, CV_64F); for (int i = 0, j = 0; i < (int)rows.size(); i++) { if (rows[i]) { - tmp.row(i).copyTo(tmp1.row(j++)); + tmp.row(i).copyTo(dst.row(j++)); } } - - dst = tmp1.clone(); } }} @@ -1386,7 +1382,7 @@ void cv::internal::CalibrateExtrinsics(InputArrayOfArrays objectPoints, InputArr void cv::internal::ComputeJacobians(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, const IntrinsicParams& param, InputArray omc, InputArray Tc, - const int& check_cond, const double& thresh_cond, Mat& JJ2_inv, Mat& ex3) + const int& check_cond, const double& thresh_cond, Mat& JJ2, Mat& ex3) { CV_Assert(!objectPoints.empty() && (objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3)); CV_Assert(!imagePoints.empty() && (imagePoints.type() == CV_32FC2 || imagePoints.type() == CV_64FC2)); @@ -1396,7 +1392,7 @@ void cv::internal::ComputeJacobians(InputArrayOfArrays objectPoints, InputArrayO int n = (int)objectPoints.total(); - Mat JJ3 = Mat::zeros(9 + 6 * n, 9 + 6 * n, CV_64FC1); + JJ2 = Mat::zeros(9 + 6 * n, 9 + 6 * n, CV_64FC1); ex3 = Mat::zeros(9 + 6 * n, 1, CV_64FC1 ); for (int image_idx = 0; image_idx < n; ++image_idx) @@ -1422,16 +1418,14 @@ void cv::internal::ComputeJacobians(InputArrayOfArrays objectPoints, InputArrayO Mat B = jacobians.colRange(8, 14).clone(); B = B.t(); - JJ3(Rect(0, 0, 9, 9)) = JJ3(Rect(0, 0, 9, 9)) + A * A.t(); - JJ3(Rect(9 + 6 * image_idx, 9 + 6 * image_idx, 6, 6)) = B * B.t(); + JJ2(Rect(0, 0, 9, 9)) += A * A.t(); + JJ2(Rect(9 + 6 * image_idx, 9 + 6 * image_idx, 6, 6)) = B * B.t(); - Mat AB = A * B.t(); - AB.copyTo(JJ3(Rect(9 + 6 * image_idx, 0, 6, 9))); + JJ2(Rect(9 + 6 * image_idx, 0, 6, 9)) = A * B.t(); + JJ2(Rect(0, 9 + 6 * image_idx, 9, 6)) = JJ2(Rect(9 + 6 * image_idx, 0, 6, 9)).t(); - JJ3(Rect(0, 9 + 6 * image_idx, 9, 6)) = AB.t(); - ex3(Rect(0,0,1,9)) = ex3(Rect(0,0,1,9)) + A * exkk.reshape(1, 2 * exkk.rows); - - ex3(Rect(0, 9 + 6 * image_idx, 1, 6)) = B * exkk.reshape(1, 2 * exkk.rows); + ex3.rowRange(0, 9) += A * exkk.reshape(1, 2 * exkk.rows); + ex3.rowRange(9 + 6 * image_idx, 9 + 6 * (image_idx + 1)) = B * exkk.reshape(1, 2 * exkk.rows); if (check_cond) { @@ -1441,12 +1435,11 @@ void cv::internal::ComputeJacobians(InputArrayOfArrays objectPoints, InputArrayO } } - std::vector idxs(param.isEstimate); + std::vector idxs(param.isEstimate); idxs.insert(idxs.end(), 6 * n, 1); - subMatrix(JJ3, JJ3, idxs, idxs); - subMatrix(ex3, ex3, std::vector(1, 1), idxs); - JJ2_inv = JJ3.inv(); + subMatrix(JJ2, JJ2, idxs, idxs); + subMatrix(ex3, ex3, std::vector(1, 1), idxs); } void cv::internal::EstimateUncertainties(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, @@ -1478,30 +1471,17 @@ void cv::internal::EstimateUncertainties(InputArrayOfArrays objectPoints, InputA meanStdDev(ex, noArray(), std_err); std_err *= sqrt((double)ex.total()/((double)ex.total() - 1.0)); - Mat sigma_x; + Vec sigma_x; meanStdDev(ex.reshape(1, 1), noArray(), sigma_x); sigma_x *= sqrt(2.0 * (double)ex.total()/(2.0 * (double)ex.total() - 1.0)); - Mat _JJ2_inv, ex3; - ComputeJacobians(objectPoints, imagePoints, params, omc, Tc, check_cond, thresh_cond, _JJ2_inv, ex3); + Mat JJ2, ex3; + ComputeJacobians(objectPoints, imagePoints, params, omc, Tc, check_cond, thresh_cond, JJ2, ex3); - Mat_& JJ2_inv = (Mat_&)_JJ2_inv; + sqrt(JJ2.inv(), JJ2); - sqrt(JJ2_inv, JJ2_inv); - - double s = sigma_x.at(0); - Mat r = 3 * s * JJ2_inv.diag(); - errors = r; - - rms = 0; - const Vec2d* ptr_ex = ex.ptr(); - for (size_t i = 0; i < ex.total(); i++) - { - rms += ptr_ex[i][0] * ptr_ex[i][0] + ptr_ex[i][1] * ptr_ex[i][1]; - } - - rms /= (double)ex.total(); - rms = sqrt(rms); + errors = 3 * sigma_x(0) * JJ2.diag(); + rms = sqrt(norm(ex, NORM_L2SQR)/ex.total()); } void cv::internal::dAB(InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB) diff --git a/modules/calib3d/src/fisheye.hpp b/modules/calib3d/src/fisheye.hpp index 82c9f3459..d46ac8646 100644 --- a/modules/calib3d/src/fisheye.hpp +++ b/modules/calib3d/src/fisheye.hpp @@ -10,7 +10,7 @@ struct CV_EXPORTS IntrinsicParams Vec2d c; Vec4d k; double alpha; - std::vector isEstimate; + std::vector isEstimate; IntrinsicParams(); IntrinsicParams(Vec2d f, Vec2d c, Vec4d k, double alpha = 0); diff --git a/modules/calib3d/src/five-point.cpp b/modules/calib3d/src/five-point.cpp index f575b02a9..92e652b0e 100644 --- a/modules/calib3d/src/five-point.cpp +++ b/modules/calib3d/src/five-point.cpp @@ -402,37 +402,41 @@ protected: } // Input should be a vector of n 2D points or a Nx2 matrix -cv::Mat cv::findEssentialMat( InputArray _points1, InputArray _points2, double focal, Point2d pp, +cv::Mat cv::findEssentialMat( InputArray _points1, InputArray _points2, InputArray _cameraMatrix, int method, double prob, double threshold, OutputArray _mask) { - Mat points1, points2; + Mat points1, points2, cameraMatrix; _points1.getMat().convertTo(points1, CV_64F); _points2.getMat().convertTo(points2, CV_64F); + _cameraMatrix.getMat().convertTo(cameraMatrix, CV_64F); int npoints = points1.checkVector(2); - CV_Assert( npoints >= 5 && points2.checkVector(2) == npoints && + CV_Assert( npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()); - if( points1.channels() > 1 ) + CV_Assert(cameraMatrix.rows == 3 && cameraMatrix.cols == 3 && cameraMatrix.channels() == 1); + + if (points1.channels() > 1) { points1 = points1.reshape(1, npoints); points2 = points2.reshape(1, npoints); } - double ifocal = focal != 0 ? 1./focal : 1.; - for( int i = 0; i < npoints; i++ ) - { - points1.at(i, 0) = (points1.at(i, 0) - pp.x)*ifocal; - points1.at(i, 1) = (points1.at(i, 1) - pp.y)*ifocal; - points2.at(i, 0) = (points2.at(i, 0) - pp.x)*ifocal; - points2.at(i, 1) = (points2.at(i, 1) - pp.y)*ifocal; - } + double fx = cameraMatrix.at(0,0); + double fy = cameraMatrix.at(1,1); + double cx = cameraMatrix.at(0,2); + double cy = cameraMatrix.at(1,2); + + points1.col(0) = (points1.col(0) - cx) / fx; + points2.col(0) = (points2.col(0) - cx) / fx; + points1.col(1) = (points1.col(1) - cy) / fy; + points2.col(1) = (points2.col(1) - cy) / fy; // Reshape data to fit opencv ransac function points1 = points1.reshape(2, npoints); points2 = points2.reshape(2, npoints); - threshold /= focal; + threshold /= (fx+fy)/2; Mat E; if( method == RANSAC ) @@ -443,29 +447,42 @@ cv::Mat cv::findEssentialMat( InputArray _points1, InputArray _points2, double f return E; } -int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, OutputArray _R, - OutputArray _t, double focal, Point2d pp, InputOutputArray _mask) +cv::Mat cv::findEssentialMat( InputArray _points1, InputArray _points2, double focal, Point2d pp, + int method, double prob, double threshold, OutputArray _mask) { - Mat points1, points2; - _points1.getMat().copyTo(points1); - _points2.getMat().copyTo(points2); + Mat cameraMatrix = (Mat_(3,3) << focal, 0, pp.x, 0, focal, pp.y, 0, 0, 1); + return cv::findEssentialMat(_points1, _points2, cameraMatrix, method, prob, threshold, _mask); +} + +int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, InputArray _cameraMatrix, + OutputArray _R, OutputArray _t, InputOutputArray _mask) +{ + Mat points1, points2, cameraMatrix; + _points1.getMat().convertTo(points1, CV_64F); + _points2.getMat().convertTo(points2, CV_64F); + _cameraMatrix.getMat().convertTo(cameraMatrix, CV_64F); int npoints = points1.checkVector(2); CV_Assert( npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()); + CV_Assert(cameraMatrix.rows == 3 && cameraMatrix.cols == 3 && cameraMatrix.channels() == 1); + if (points1.channels() > 1) { points1 = points1.reshape(1, npoints); points2 = points2.reshape(1, npoints); } - points1.convertTo(points1, CV_64F); - points2.convertTo(points2, CV_64F); - points1.col(0) = (points1.col(0) - pp.x) / focal; - points2.col(0) = (points2.col(0) - pp.x) / focal; - points1.col(1) = (points1.col(1) - pp.y) / focal; - points2.col(1) = (points2.col(1) - pp.y) / focal; + double fx = cameraMatrix.at(0,0); + double fy = cameraMatrix.at(1,1); + double cx = cameraMatrix.at(0,2); + double cy = cameraMatrix.at(1,2); + + points1.col(0) = (points1.col(0) - cx) / fx; + points2.col(0) = (points2.col(0) - cx) / fx; + points1.col(1) = (points1.col(1) - cy) / fy; + points2.col(1) = (points2.col(1) - cy) / fy; points1 = points1.t(); points2 = points2.t(); @@ -590,6 +607,12 @@ int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, Out } } +int cv::recoverPose( InputArray E, InputArray _points1, InputArray _points2, OutputArray _R, + OutputArray _t, double focal, Point2d pp, InputOutputArray _mask) +{ + Mat cameraMatrix = (Mat_(3,3) << focal, 0, pp.x, 0, focal, pp.y, 0, 0, 1); + return cv::recoverPose(E, _points1, _points2, cameraMatrix, _R, _t, _mask); +} void cv::decomposeEssentialMat( InputArray _E, OutputArray _R1, OutputArray _R2, OutputArray _t ) { diff --git a/modules/calib3d/src/fundam.cpp b/modules/calib3d/src/fundam.cpp index 5d7e706a4..20803a5fe 100644 --- a/modules/calib3d/src/fundam.cpp +++ b/modules/calib3d/src/fundam.cpp @@ -411,7 +411,13 @@ cv::Mat cv::findHomography( InputArray _points1, InputArray _points2, tempMask.copyTo(_mask); } else + { H.release(); + if(_mask.needed() ) { + tempMask = Mat::zeros(npoints >= 0 ? npoints : 0, 1, CV_8U); + tempMask.copyTo(_mask); + } + } return H; } @@ -1039,4 +1045,24 @@ void cv::convertPointsHomogeneous( InputArray _src, OutputArray _dst ) convertPointsToHomogeneous(_src, _dst); } +double cv::sampsonDistance(InputArray _pt1, InputArray _pt2, InputArray _F) { + CV_Assert(_pt1.type() == CV_64F && _pt1.type() == CV_64F && _F.type() == CV_64F); + CV_DbgAssert(_pt1.rows() == 3 && _F.size() == Size(3, 3) && _pt1.rows() == _pt2.rows()); + + Mat pt1(_pt1.getMat()); + Mat pt2(_pt2.getMat()); + Mat F(_F.getMat()); + + Vec3d F_pt1 = *F.ptr() * *pt1.ptr(); + Vec3d Ft_pt2 = F.ptr()->t() * *pt2.ptr(); + + double v = pt2.ptr()->dot(F_pt1); + + // square + Ft_pt2 = Ft_pt2.mul(Ft_pt2); + F_pt1 = F_pt1.mul(F_pt1); + + return v*v / (F_pt1[0] + F_pt1[1] + Ft_pt2[0] + Ft_pt2[1]); +} + /* End of file. */ diff --git a/modules/calib3d/src/ptsetreg.cpp b/modules/calib3d/src/ptsetreg.cpp index 75a73a5cb..f41769654 100644 --- a/modules/calib3d/src/ptsetreg.cpp +++ b/modules/calib3d/src/ptsetreg.cpp @@ -109,9 +109,9 @@ public: cv::AutoBuffer _idx(modelPoints); int* idx = _idx; int i = 0, j, k, iters = 0; - int esz1 = (int)m1.elemSize(), esz2 = (int)m2.elemSize(); int d1 = m1.channels() > 1 ? m1.channels() : m1.cols; int d2 = m2.channels() > 1 ? m2.channels() : m2.cols; + int esz1 = (int)m1.elemSize1()*d1, esz2 = (int)m2.elemSize1()*d2; int count = m1.checkVector(d1), count2 = m2.checkVector(d2); const int *m1ptr = m1.ptr(), *m2ptr = m2.ptr(); @@ -206,7 +206,7 @@ public: for( iter = 0; iter < niters; iter++ ) { - int i, goodCount, nmodels; + int i, nmodels; if( count > modelPoints ) { bool found = getSubset( m1, m2, ms1, ms2, rng, 10000 ); @@ -227,7 +227,7 @@ public: for( i = 0; i < nmodels; i++ ) { Mat model_i = model.rowRange( i*modelSize.height, (i+1)*modelSize.height ); - goodCount = findInliers( m1, m2, model_i, err, mask, threshold ); + int goodCount = findInliers( m1, m2, model_i, err, mask, threshold ); if( goodCount > MAX(maxGoodCount, modelPoints-1) ) { @@ -284,7 +284,7 @@ public: int d1 = m1.channels() > 1 ? m1.channels() : m1.cols; int d2 = m2.channels() > 1 ? m2.channels() : m2.cols; int count = m1.checkVector(d1), count2 = m2.checkVector(d2); - double minMedian = DBL_MAX, sigma; + double minMedian = DBL_MAX; RNG rng((uint64)-1); @@ -359,7 +359,7 @@ public: if( minMedian < DBL_MAX ) { - sigma = 2.5*1.4826*(1 + 5./(count - modelPoints))*std::sqrt(minMedian); + double sigma = 2.5*1.4826*(1 + 5./(count - modelPoints))*std::sqrt(minMedian); sigma = MAX( sigma, 0.001 ); count = findInliers( m1, m2, bestModel, err, mask, sigma ); diff --git a/modules/calib3d/src/rho.cpp b/modules/calib3d/src/rho.cpp index 2f27728a2..95259b76b 100644 --- a/modules/calib3d/src/rho.cpp +++ b/modules/calib3d/src/rho.cpp @@ -44,7 +44,7 @@ */ /* Includes */ -#include +#include "precomp.hpp" #include #include #include diff --git a/modules/calib3d/src/solvepnp.cpp b/modules/calib3d/src/solvepnp.cpp index 8b20cf7e4..23575199d 100644 --- a/modules/calib3d/src/solvepnp.cpp +++ b/modules/calib3d/src/solvepnp.cpp @@ -270,6 +270,8 @@ bool solvePnPRansac(InputArray _opoints, InputArray _ipoints, { vector opoints_inliers; vector ipoints_inliers; + opoints = opoints.reshape(3); + ipoints = ipoints.reshape(2); opoints.convertTo(opoints_inliers, CV_64F); ipoints.convertTo(ipoints_inliers, CV_64F); diff --git a/modules/calib3d/src/stereosgbm.cpp b/modules/calib3d/src/stereosgbm.cpp index 15b35fcf1..1c085f9b3 100644 --- a/modules/calib3d/src/stereosgbm.cpp +++ b/modules/calib3d/src/stereosgbm.cpp @@ -52,7 +52,7 @@ #include "precomp.hpp" #include -#include "opencv2/hal/intrin.hpp" +#include "opencv2/core/hal/intrin.hpp" namespace cv { diff --git a/modules/calib3d/test/test_cameracalibration_tilt.cpp b/modules/calib3d/test/test_cameracalibration_tilt.cpp new file mode 100644 index 000000000..1b916dae9 --- /dev/null +++ b/modules/calib3d/test/test_cameracalibration_tilt.cpp @@ -0,0 +1,700 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "test_precomp.hpp" +#include +#include "opencv2/calib3d.hpp" + +#define NUM_DIST_COEFF_TILT 14 + +/** +Some conventions: +- the first camera determines the world coordinate system +- y points down, hence top means minimal y value (negative) and +bottom means maximal y value (positive) +- the field of view plane is tilted around x such that it +intersects the xy-plane in a line with a large (positive) +y-value +- image sensor and object are both modelled in the halfspace +z > 0 + + +**/ +class cameraCalibrationTiltTest : public ::testing::Test { + +protected: + cameraCalibrationTiltTest() + : m_toRadian(acos(-1.0)/180.0) + , m_toDegree(180.0/acos(-1.0)) + {} + virtual void SetUp(); + +protected: + static const cv::Size m_imageSize; + static const double m_pixelSize; + static const double m_circleConfusionPixel; + static const double m_lensFocalLength; + static const double m_lensFNumber; + static const double m_objectDistance; + static const double m_planeTiltDegree; + static const double m_pointTargetDist; + static const int m_pointTargetNum; + + /** image distance coresponding to working distance */ + double m_imageDistance; + /** image tilt angle corresponding to the tilt of the object plane */ + double m_imageTiltDegree; + /** center of the field of view, near and far plane */ + std::vector m_fovCenter; + /** normal of the field of view, near and far plane */ + std::vector m_fovNormal; + /** points on a plane calibration target */ + std::vector m_pointTarget; + /** rotations for the calibration target */ + std::vector m_pointTargetRvec; + /** translations for the calibration target */ + std::vector m_pointTargetTvec; + /** camera matrix */ + cv::Matx33d m_cameraMatrix; + /** distortion coefficients */ + cv::Vec m_distortionCoeff; + + /** random generator */ + cv::RNG m_rng; + /** degree to radian conversion factor */ + const double m_toRadian; + /** radian to degree conversion factor */ + const double m_toDegree; + + /** + computes for a given distance of an image or object point + the distance of the corresponding object or image point + */ + double opticalMap(double dist) { + return m_lensFocalLength*dist/(dist - m_lensFocalLength); + } + + /** magnification of the optical map */ + double magnification(double dist) { + return m_lensFocalLength/(dist - m_lensFocalLength); + } + + /** + Changes given distortion coefficients randomly by adding + a uniformly distributed random variable in [-max max] + \param coeff input + \param max limits for the random variables + */ + void randomDistortionCoeff( + cv::Vec& coeff, + const cv::Vec& max) + { + for (int i = 0; i < coeff.rows; ++i) + coeff(i) += m_rng.uniform(-max(i), max(i)); + } + + /** numerical jacobian */ + void numericalDerivative( + cv::Mat& jac, + double eps, + const std::vector& obj, + const cv::Vec3d& rvec, + const cv::Vec3d& tvec, + const cv::Matx33d& camera, + const cv::Vec& distor); + + /** remove points with projection outside the sensor array */ + void removeInvalidPoints( + std::vector& imagePoints, + std::vector& objectPoints); + + /** add uniform distribute noise in [-halfWidthNoise, halfWidthNoise] + to the image points and remove out of range points */ + void addNoiseRemoveInvalidPoints( + std::vector& imagePoints, + std::vector& objectPoints, + std::vector& noisyImagePoints, + double halfWidthNoise); +}; + +/** Number of Pixel of the sensor */ +const cv::Size cameraCalibrationTiltTest::m_imageSize(1600, 1200); +/** Size of a pixel in mm */ +const double cameraCalibrationTiltTest::m_pixelSize(.005); +/** Diameter of the circle of confusion */ +const double cameraCalibrationTiltTest::m_circleConfusionPixel(3); +/** Focal length of the lens */ +const double cameraCalibrationTiltTest::m_lensFocalLength(16.4); +/** F-Number */ +const double cameraCalibrationTiltTest::m_lensFNumber(8); +/** Working distance */ +const double cameraCalibrationTiltTest::m_objectDistance(200); +/** Angle between optical axis and object plane normal */ +const double cameraCalibrationTiltTest::m_planeTiltDegree(55); +/** the calibration target are points on a square grid with this side length */ +const double cameraCalibrationTiltTest::m_pointTargetDist(5); +/** the calibration target has (2*n + 1) x (2*n + 1) points */ +const int cameraCalibrationTiltTest::m_pointTargetNum(15); + + +void cameraCalibrationTiltTest::SetUp() +{ + m_imageDistance = opticalMap(m_objectDistance); + m_imageTiltDegree = m_toDegree * atan2( + m_imageDistance * tan(m_toRadian * m_planeTiltDegree), + m_objectDistance); + // half sensor height + double tmp = .5 * (m_imageSize.height - 1) * m_pixelSize + * cos(m_toRadian * m_imageTiltDegree); + // y-Value of tilted sensor + double yImage[2] = {tmp, -tmp}; + // change in z because of the tilt + tmp *= sin(m_toRadian * m_imageTiltDegree); + // z-values of the sensor lower and upper corner + double zImage[2] = { + m_imageDistance + tmp, + m_imageDistance - tmp}; + // circle of confusion + double circleConfusion = m_circleConfusionPixel*m_pixelSize; + // aperture of the lense + double aperture = m_lensFocalLength/m_lensFNumber; + // near and far factor on the image side + double nearFarFactorImage[2] = { + aperture/(aperture - circleConfusion), + aperture/(aperture + circleConfusion)}; + // on the object side - points that determin the field of + // view + std::vector fovBottomTop(6); + std::vector::iterator itFov = fovBottomTop.begin(); + for (size_t iBottomTop = 0; iBottomTop < 2; ++iBottomTop) + { + // mapping sensor to field of view + *itFov = cv::Vec3d(0,yImage[iBottomTop],zImage[iBottomTop]); + *itFov *= magnification((*itFov)(2)); + ++itFov; + for (size_t iNearFar = 0; iNearFar < 2; ++iNearFar, ++itFov) + { + // scaling to the near and far distance on the + // image side + *itFov = cv::Vec3d(0,yImage[iBottomTop],zImage[iBottomTop]) * + nearFarFactorImage[iNearFar]; + // scaling to the object side + *itFov *= magnification((*itFov)(2)); + } + } + m_fovCenter.resize(3); + m_fovNormal.resize(3); + for (size_t i = 0; i < 3; ++i) + { + m_fovCenter[i] = .5*(fovBottomTop[i] + fovBottomTop[i+3]); + m_fovNormal[i] = fovBottomTop[i+3] - fovBottomTop[i]; + m_fovNormal[i] = cv::normalize(m_fovNormal[i]); + m_fovNormal[i] = cv::Vec3d( + m_fovNormal[i](0), + -m_fovNormal[i](2), + m_fovNormal[i](1)); + // one target position in each plane + m_pointTargetTvec.push_back(m_fovCenter[i]); + cv::Vec3d rvec = cv::Vec3d(0,0,1).cross(m_fovNormal[i]); + rvec = cv::normalize(rvec); + rvec *= acos(m_fovNormal[i](2)); + m_pointTargetRvec.push_back(rvec); + } + // calibration target + size_t num = 2*m_pointTargetNum + 1; + m_pointTarget.resize(num*num); + std::vector::iterator itTarget = m_pointTarget.begin(); + for (int iY = -m_pointTargetNum; iY <= m_pointTargetNum; ++iY) + { + for (int iX = -m_pointTargetNum; iX <= m_pointTargetNum; ++iX, ++itTarget) + { + *itTarget = cv::Point3d(iX, iY, 0) * m_pointTargetDist; + } + } + // oblique target positions + // approximate distance to the near and far plane + double dist = std::max( + std::abs(m_fovNormal[0].dot(m_fovCenter[0] - m_fovCenter[1])), + std::abs(m_fovNormal[0].dot(m_fovCenter[0] - m_fovCenter[2]))); + // maximal angle such that target border "reaches" near and far plane + double maxAngle = atan2(dist, m_pointTargetNum*m_pointTargetDist); + std::vector angle; + angle.push_back(-maxAngle); + angle.push_back(maxAngle); + cv::Matx33d baseMatrix; + cv::Rodrigues(m_pointTargetRvec.front(), baseMatrix); + for (std::vector::const_iterator itAngle = angle.begin(); itAngle != angle.end(); ++itAngle) + { + cv::Matx33d rmat; + for (int i = 0; i < 2; ++i) + { + cv::Vec3d rvec(0,0,0); + rvec(i) = *itAngle; + cv::Rodrigues(rvec, rmat); + rmat = baseMatrix*rmat; + cv::Rodrigues(rmat, rvec); + m_pointTargetTvec.push_back(m_fovCenter.front()); + m_pointTargetRvec.push_back(rvec); + } + } + // camera matrix + double cx = .5 * (m_imageSize.width - 1); + double cy = .5 * (m_imageSize.height - 1); + double f = m_imageDistance/m_pixelSize; + m_cameraMatrix = cv::Matx33d( + f,0,cx, + 0,f,cy, + 0,0,1); + // distortion coefficients + m_distortionCoeff = cv::Vec::all(0); + // tauX + m_distortionCoeff(12) = -m_toRadian*m_imageTiltDegree; + +} + +void cameraCalibrationTiltTest::numericalDerivative( + cv::Mat& jac, + double eps, + const std::vector& obj, + const cv::Vec3d& rvec, + const cv::Vec3d& tvec, + const cv::Matx33d& camera, + const cv::Vec& distor) +{ + cv::Vec3d r(rvec); + cv::Vec3d t(tvec); + cv::Matx33d cm(camera); + cv::Vec dc(distor); + double* param[10+NUM_DIST_COEFF_TILT] = { + &r(0), &r(1), &r(2), + &t(0), &t(1), &t(2), + &cm(0,0), &cm(1,1), &cm(0,2), &cm(1,2), + &dc(0), &dc(1), &dc(2), &dc(3), &dc(4), &dc(5), &dc(6), + &dc(7), &dc(8), &dc(9), &dc(10), &dc(11), &dc(12), &dc(13)}; + std::vector pix0, pix1; + double invEps = .5/eps; + + for (int col = 0; col < 10+NUM_DIST_COEFF_TILT; ++col) + { + double save = *(param[col]); + *(param[col]) = save + eps; + cv::projectPoints(obj, r, t, cm, dc, pix0); + *(param[col]) = save - eps; + cv::projectPoints(obj, r, t, cm, dc, pix1); + *(param[col]) = save; + + std::vector::const_iterator it0 = pix0.begin(); + std::vector::const_iterator it1 = pix1.begin(); + int row = 0; + for (;it0 != pix0.end(); ++it0, ++it1) + { + cv::Point2d d = invEps*(*it0 - *it1); + jac.at(row, col) = d.x; + ++row; + jac.at(row, col) = d.y; + ++row; + } + } +} + +void cameraCalibrationTiltTest::removeInvalidPoints( + std::vector& imagePoints, + std::vector& objectPoints) +{ + // remove object and imgage points out of range + std::vector::iterator itImg = imagePoints.begin(); + std::vector::iterator itObj = objectPoints.begin(); + while (itImg != imagePoints.end()) + { + bool ok = + itImg->x >= 0 && + itImg->x <= m_imageSize.width - 1.0 && + itImg->y >= 0 && + itImg->y <= m_imageSize.height - 1.0; + if (ok) + { + ++itImg; + ++itObj; + } + else + { + itImg = imagePoints.erase(itImg); + itObj = objectPoints.erase(itObj); + } + } +} + +void cameraCalibrationTiltTest::addNoiseRemoveInvalidPoints( + std::vector& imagePoints, + std::vector& objectPoints, + std::vector& noisyImagePoints, + double halfWidthNoise) +{ + std::vector::iterator itImg = imagePoints.begin(); + std::vector::iterator itObj = objectPoints.begin(); + noisyImagePoints.clear(); + noisyImagePoints.reserve(imagePoints.size()); + while (itImg != imagePoints.end()) + { + cv::Point2f pix = *itImg + cv::Point2f( + (float)m_rng.uniform(-halfWidthNoise, halfWidthNoise), + (float)m_rng.uniform(-halfWidthNoise, halfWidthNoise)); + bool ok = + pix.x >= 0 && + pix.x <= m_imageSize.width - 1.0 && + pix.y >= 0 && + pix.y <= m_imageSize.height - 1.0; + if (ok) + { + noisyImagePoints.push_back(pix); + ++itImg; + ++itObj; + } + else + { + itImg = imagePoints.erase(itImg); + itObj = objectPoints.erase(itObj); + } + } +} + + +TEST_F(cameraCalibrationTiltTest, projectPoints) +{ + std::vector imagePoints; + std::vector objectPoints = m_pointTarget; + cv::Vec3d rvec = m_pointTargetRvec.front(); + cv::Vec3d tvec = m_pointTargetTvec.front(); + + cv::Vec coeffNoiseHalfWidth( + .1, .1, // k1 k2 + .01, .01, // p1 p2 + .001, .001, .001, .001, // k3 k4 k5 k6 + .001, .001, .001, .001, // s1 s2 s3 s4 + .01, .01); // tauX tauY + for (size_t numTest = 0; numTest < 10; ++numTest) + { + // create random distortion coefficients + cv::Vec distortionCoeff = m_distortionCoeff; + randomDistortionCoeff(distortionCoeff, coeffNoiseHalfWidth); + + // projection + cv::projectPoints( + objectPoints, + rvec, + tvec, + m_cameraMatrix, + distortionCoeff, + imagePoints); + + // remove object and imgage points out of range + removeInvalidPoints(imagePoints, objectPoints); + + int numPoints = (int)imagePoints.size(); + int numParams = 10 + distortionCoeff.rows; + cv::Mat jacobian(2*numPoints, numParams, CV_64FC1); + + // projection and jacobian + cv::projectPoints( + objectPoints, + rvec, + tvec, + m_cameraMatrix, + distortionCoeff, + imagePoints, + jacobian); + + // numerical derivatives + cv::Mat numericJacobian(2*numPoints, numParams, CV_64FC1); + double eps = 1e-7; + numericalDerivative( + numericJacobian, + eps, + objectPoints, + rvec, + tvec, + m_cameraMatrix, + distortionCoeff); + +#if 0 + for (size_t row = 0; row < 2; ++row) + { + std::cout << "------ Row = " << row << " ------\n"; + for (size_t i = 0; i < 10+NUM_DIST_COEFF_TILT; ++i) + { + std::cout << i + << " jac = " << jacobian.at(row,i) + << " num = " << numericJacobian.at(row,i) + << " rel. diff = " << abs(numericJacobian.at(row,i) - jacobian.at(row,i))/abs(numericJacobian.at(row,i)) + << "\n"; + } + } +#endif + // relative difference for large values (rvec and tvec) + cv::Mat check = abs(jacobian(cv::Range::all(), cv::Range(0,6)) - numericJacobian(cv::Range::all(), cv::Range(0,6)))/ + (1 + abs(jacobian(cv::Range::all(), cv::Range(0,6)))); + double minVal, maxVal; + cv::minMaxIdx(check, &minVal, &maxVal); + EXPECT_LE(maxVal, .01); + // absolute difference for distortion and camera matrix + EXPECT_MAT_NEAR(jacobian(cv::Range::all(), cv::Range(6,numParams)), numericJacobian(cv::Range::all(), cv::Range(6,numParams)), 1e-5); + } +} + +TEST_F(cameraCalibrationTiltTest, undistortPoints) +{ + cv::Vec coeffNoiseHalfWidth( + .2, .1, // k1 k2 + .01, .01, // p1 p2 + .01, .01, .01, .01, // k3 k4 k5 k6 + .001, .001, .001, .001, // s1 s2 s3 s4 + .001, .001); // tauX tauY + double step = 99; + double toleranceBackProjection = 1e-5; + + for (size_t numTest = 0; numTest < 10; ++numTest) + { + cv::Vec distortionCoeff = m_distortionCoeff; + randomDistortionCoeff(distortionCoeff, coeffNoiseHalfWidth); + + // distorted points + std::vector distorted; + for (double x = 0; x <= m_imageSize.width-1; x += step) + for (double y = 0; y <= m_imageSize.height-1; y += step) + distorted.push_back(cv::Point2d(x,y)); + std::vector normalizedUndistorted; + + // undistort + cv::undistortPoints(distorted, + normalizedUndistorted, + m_cameraMatrix, + distortionCoeff); + + // copy normalized points to 3D + std::vector objectPoints; + for (std::vector::const_iterator itPnt = normalizedUndistorted.begin(); + itPnt != normalizedUndistorted.end(); ++itPnt) + objectPoints.push_back(cv::Point3d(itPnt->x, itPnt->y, 1)); + + // project + std::vector imagePoints(objectPoints.size()); + cv::projectPoints(objectPoints, + cv::Vec3d(0,0,0), + cv::Vec3d(0,0,0), + m_cameraMatrix, + distortionCoeff, + imagePoints); + + EXPECT_MAT_NEAR(distorted, imagePoints, toleranceBackProjection); + } +} + +template +void show(const std::string& name, const INPUT in, const ESTIMATE est) +{ + std::cout << name << " = " << est << " (init = " << in + << ", diff = " << est-in << ")\n"; +} + +template +void showVec(const std::string& name, const INPUT& in, const cv::Mat& est) +{ + + for (size_t i = 0; i < in.channels; ++i) + { + std::stringstream ss; + ss << name << "[" << i << "]"; + show(ss.str(), in(i), est.at(i)); + } +} + +/** +For given camera matrix and distortion coefficients +- project point target in different positions onto the sensor +- add pixel noise +- estimate camera modell with noisy measurements +- compare result with initial model parameter + +Parameter are differently affected by the noise +*/ +TEST_F(cameraCalibrationTiltTest, calibrateCamera) +{ + cv::Vec coeffNoiseHalfWidth( + .2, .1, // k1 k2 + .01, .01, // p1 p2 + 0, 0, 0, 0, // k3 k4 k5 k6 + .001, .001, .001, .001, // s1 s2 s3 s4 + .001, .001); // tauX tauY + double pixelNoiseHalfWidth = .5; + std::vector pointTarget; + pointTarget.reserve(m_pointTarget.size()); + for (std::vector::const_iterator it = m_pointTarget.begin(); it != m_pointTarget.end(); ++it) + pointTarget.push_back(cv::Point3f( + (float)(it->x), + (float)(it->y), + (float)(it->z))); + + for (size_t numTest = 0; numTest < 5; ++numTest) + { + // create random distortion coefficients + cv::Vec distortionCoeff = m_distortionCoeff; + randomDistortionCoeff(distortionCoeff, coeffNoiseHalfWidth); + + // container for calibration data + std::vector > viewsObjectPoints; + std::vector > viewsImagePoints; + std::vector > viewsNoisyImagePoints; + + // simulate calibration data with projectPoints + std::vector::const_iterator itRvec = m_pointTargetRvec.begin(); + std::vector::const_iterator itTvec = m_pointTargetTvec.begin(); + // loop over different views + for (;itRvec != m_pointTargetRvec.end(); ++ itRvec, ++itTvec) + { + std::vector objectPoints(pointTarget); + std::vector imagePoints; + std::vector noisyImagePoints; + // project calibration target to sensor + cv::projectPoints( + objectPoints, + *itRvec, + *itTvec, + m_cameraMatrix, + distortionCoeff, + imagePoints); + // remove invisible points + addNoiseRemoveInvalidPoints( + imagePoints, + objectPoints, + noisyImagePoints, + pixelNoiseHalfWidth); + // add data for view + viewsNoisyImagePoints.push_back(noisyImagePoints); + viewsImagePoints.push_back(imagePoints); + viewsObjectPoints.push_back(objectPoints); + } + + // Output + std::vector outRvecs, outTvecs; + cv::Mat outCameraMatrix(3, 3, CV_64F, cv::Scalar::all(1)), outDistCoeff; + + // Stopping criteria + cv::TermCriteria stop( + cv::TermCriteria::COUNT+cv::TermCriteria::EPS, + 50000, + 1e-14); + // modell coice + int flag = + cv::CALIB_FIX_ASPECT_RATIO | + // cv::CALIB_RATIONAL_MODEL | + cv::CALIB_FIX_K3 | + // cv::CALIB_FIX_K6 | + cv::CALIB_THIN_PRISM_MODEL | + cv::CALIB_TILTED_MODEL; + // estimate + double backProjErr = cv::calibrateCamera( + viewsObjectPoints, + viewsNoisyImagePoints, + m_imageSize, + outCameraMatrix, + outDistCoeff, + outRvecs, + outTvecs, + flag, + stop); + + EXPECT_LE(backProjErr, pixelNoiseHalfWidth); + +#if 0 + std::cout << "------ estimate ------\n"; + std::cout << "back projection error = " << backProjErr << "\n"; + std::cout << "points per view = {" << viewsObjectPoints.front().size(); + for (size_t i = 1; i < viewsObjectPoints.size(); ++i) + std::cout << ", " << viewsObjectPoints[i].size(); + std::cout << "}\n"; + show("fx", m_cameraMatrix(0,0), outCameraMatrix.at(0,0)); + show("fy", m_cameraMatrix(1,1), outCameraMatrix.at(1,1)); + show("cx", m_cameraMatrix(0,2), outCameraMatrix.at(0,2)); + show("cy", m_cameraMatrix(1,2), outCameraMatrix.at(1,2)); + showVec("distor", distortionCoeff, outDistCoeff); +#endif + if (pixelNoiseHalfWidth > 0) + { + double tolRvec = pixelNoiseHalfWidth; + double tolTvec = m_objectDistance * tolRvec; + // back projection error + for (size_t i = 0; i < viewsNoisyImagePoints.size(); ++i) + { + double dRvec = norm( + m_pointTargetRvec[i] - + cv::Vec3d( + outRvecs[i].at(0), + outRvecs[i].at(1), + outRvecs[i].at(2))); + // std::cout << dRvec << " " << tolRvec << "\n"; + EXPECT_LE(dRvec, + tolRvec); + double dTvec = norm( + m_pointTargetTvec[i] - + cv::Vec3d( + outTvecs[i].at(0), + outTvecs[i].at(1), + outTvecs[i].at(2))); + // std::cout << dTvec << " " << tolTvec << "\n"; + EXPECT_LE(dTvec, + tolTvec); + + std::vector backProjection; + cv::projectPoints( + viewsObjectPoints[i], + outRvecs[i], + outTvecs[i], + outCameraMatrix, + outDistCoeff, + backProjection); + EXPECT_MAT_NEAR(backProjection, viewsNoisyImagePoints[i], 1.5*pixelNoiseHalfWidth); + EXPECT_MAT_NEAR(backProjection, viewsImagePoints[i], 1.5*pixelNoiseHalfWidth); + } + } + pixelNoiseHalfWidth *= .25; + } +} diff --git a/modules/calib3d/test/test_fisheye.cpp b/modules/calib3d/test/test_fisheye.cpp index 32b77c654..006f37891 100644 --- a/modules/calib3d/test/test_fisheye.cpp +++ b/modules/calib3d/test/test_fisheye.cpp @@ -368,7 +368,7 @@ TEST_F(fisheyeTest, EtimateUncertainties) double thresh_cond = 1e6; int check_cond = 1; param.Init(cv::Vec2d(theK(0,0), theK(1,1)), cv::Vec2d(theK(0,2), theK(1, 2)), theD); - param.isEstimate = std::vector(9, 1); + param.isEstimate = std::vector(9, 1); param.isEstimate[4] = 0; errors.isEstimate = param.isEstimate; diff --git a/modules/calib3d/test/test_homography.cpp b/modules/calib3d/test/test_homography.cpp index 0d4fae50b..126f373a6 100644 --- a/modules/calib3d/test/test_homography.cpp +++ b/modules/calib3d/test/test_homography.cpp @@ -12,6 +12,7 @@ // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2015, Itseez Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, @@ -564,6 +565,9 @@ void CV_HomographyTest::run(int) default: continue; } } + + delete[]src_data; + src_data = NULL; } } diff --git a/modules/calib3d/test/test_solvepnp_ransac.cpp b/modules/calib3d/test/test_solvepnp_ransac.cpp index 5e3dbc60d..4efdc7990 100644 --- a/modules/calib3d/test/test_solvepnp_ransac.cpp +++ b/modules/calib3d/test/test_solvepnp_ransac.cpp @@ -314,6 +314,43 @@ TEST(Calib3d_SolvePnPRansac, concurrency) EXPECT_LT(tnorm, 1e-6); } +TEST(Calib3d_SolvePnPRansac, input_type) +{ + const int numPoints = 10; + Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0., + 5.4817724002728005e+002, 2.3062194051986233e+002, 0., 0., 1.); + + std::vector points3d; + std::vector points2d; + for (int i = 0; i < numPoints; i++) + { + points3d.push_back(cv::Point3i(i, 0, 0)); + points2d.push_back(cv::Point2i(i, 0)); + } + Mat R1, t1, R2, t2, R3, t3, R4, t4; + + EXPECT_TRUE(solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R1, t1)); + + Mat points3dMat(points3d); + Mat points2dMat(points2d); + EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R2, t2)); + + points3dMat = points3dMat.reshape(3, 1); + points2dMat = points2dMat.reshape(2, 1); + EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R3, t3)); + + points3dMat = points3dMat.reshape(1, numPoints); + points2dMat = points2dMat.reshape(1, numPoints); + EXPECT_TRUE(solvePnPRansac(points3dMat, points2dMat, intrinsics, cv::Mat(), R4, t4)); + + EXPECT_LE(norm(R1, R2, NORM_INF), 1e-6); + EXPECT_LE(norm(t1, t2, NORM_INF), 1e-6); + EXPECT_LE(norm(R1, R3, NORM_INF), 1e-6); + EXPECT_LE(norm(t1, t3, NORM_INF), 1e-6); + EXPECT_LE(norm(R1, R4, NORM_INF), 1e-6); + EXPECT_LE(norm(t1, t4, NORM_INF), 1e-6); +} + TEST(Calib3d_SolvePnP, double_support) { Matx33d intrinsics(5.4794130238156129e+002, 0., 2.9835545700043139e+002, 0., @@ -335,8 +372,8 @@ TEST(Calib3d_SolvePnP, double_support) solvePnPRansac(points3dF, points2dF, intrinsics, cv::Mat(), RF, tF, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P); solvePnPRansac(points3d, points2d, intrinsics, cv::Mat(), R, t, true, 100, 8.f, 0.999, inliers, cv::SOLVEPNP_P3P); - ASSERT_LE(norm(R, Mat_(RF), NORM_INF), 1e-3); - ASSERT_LE(norm(t, Mat_(tF), NORM_INF), 1e-3); + EXPECT_LE(norm(R, Mat_(RF), NORM_INF), 1e-3); + EXPECT_LE(norm(t, Mat_(tF), NORM_INF), 1e-3); } TEST(Calib3d_SolvePnP, translation) @@ -365,16 +402,16 @@ TEST(Calib3d_SolvePnP, translation) tvec = (Mat_(3,1) << 100, 100, 0); solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true); - ASSERT_TRUE(checkRange(rvec)); - ASSERT_TRUE(checkRange(tvec)); + EXPECT_TRUE(checkRange(rvec)); + EXPECT_TRUE(checkRange(tvec)); rvec =(Mat_(3,1) << 0, 0, 0); tvec = (Mat_(3,1) << 100, 100, 0); solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, true); - ASSERT_TRUE(checkRange(rvec)); - ASSERT_TRUE(checkRange(tvec)); + EXPECT_TRUE(checkRange(rvec)); + EXPECT_TRUE(checkRange(tvec)); solvePnP(p3d, p2d, cameraIntrinsic, noArray(), rvec, tvec, false); - ASSERT_TRUE(checkRange(rvec)); - ASSERT_TRUE(checkRange(tvec)); + EXPECT_TRUE(checkRange(rvec)); + EXPECT_TRUE(checkRange(tvec)); } diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index 14fac578a..8a68c9028 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -1,6 +1,5 @@ set(the_description "The Core Functionality") ocv_add_module(core - opencv_hal PRIVATE_REQUIRED ${ZLIB_LIBRARIES} "${OPENCL_LIBRARIES}" "${VA_LIBRARIES}" OPTIONAL opencv_cudev WRAP java python) @@ -11,6 +10,10 @@ if(WINRT AND CMAKE_SYSTEM_NAME MATCHES WindowsStore AND CMAKE_SYSTEM_VERSION MAT list(APPEND extra_libs ole32.lib) endif() +if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") +endif() + if(HAVE_CUDA) ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wenum-compare -Wunused-function -Wshadow) endif() @@ -24,7 +27,7 @@ source_group("Cuda Headers\\Detail" FILES ${lib_cuda_hdrs_detail}) ocv_glob_module_sources(SOURCES "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc" HEADERS ${lib_cuda_hdrs} ${lib_cuda_hdrs_detail}) -ocv_module_include_directories(${the_module} ${ZLIB_INCLUDE_DIRS}) +ocv_module_include_directories(${the_module} ${ZLIB_INCLUDE_DIRS} ${OPENCL_INCLUDE_DIRS}) ocv_create_module(${extra_libs}) ocv_add_accuracy_tests() diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index 239c849ea..2e47658f7 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -72,6 +72,7 @@ @defgroup core_cluster Clustering @defgroup core_utils Utility and system functions and macros @{ + @defgroup core_utils_sse SSE utilities @defgroup core_utils_neon NEON utilities @} @defgroup core_opengl OpenGL interoperability @@ -80,6 +81,16 @@ @defgroup core_directx DirectX interoperability @defgroup core_eigen Eigen support @defgroup core_opencl OpenCL support + @defgroup core_va_intel Intel VA-API/OpenCL (CL-VA) interoperability + @defgroup core_hal Hardware Acceleration Layer + @{ + @defgroup core_hal_functions Functions + @defgroup core_hal_interface Interface + @defgroup core_hal_intrin Universal intrinsics + @{ + @defgroup core_hal_intrin_impl Private implementation helpers + @} + @} @} */ @@ -829,19 +840,19 @@ otherwise, its type will be CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()). */ CV_EXPORTS_W void reduce(InputArray src, OutputArray dst, int dim, int rtype, int dtype = -1); -/** @brief Creates one multichannel array out of several single-channel ones. +/** @brief Creates one multi-channel array out of several single-channel ones. -The functions merge merge several arrays to make a single multi-channel array. That is, each +The function merge merges several arrays to make a single multi-channel array. That is, each element of the output array will be a concatenation of the elements of the input arrays, where elements of i-th input array are treated as mv[i].channels()-element vectors. -The function split does the reverse operation. If you need to shuffle channels in some other -advanced way, use mixChannels . +The function cv::split does the reverse operation. If you need to shuffle channels in some other +advanced way, use cv::mixChannels. @param mv input array of matrices to be merged; all the matrices in mv must have the same size and the same depth. @param count number of input matrices when mv is a plain C array; it must be greater than zero. @param dst output array of the same size and the same depth as mv[0]; The number of channels will -be the total number of channels in the matrix array. +be equal to the parameter count. @sa mixChannels, split, Mat::reshape */ CV_EXPORTS void merge(const Mat* mv, size_t count, OutputArray dst); @@ -996,24 +1007,24 @@ CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode); /** @brief Fills the output array with repeated copies of the input array. -The functions repeat duplicate the input array one or more times along each of the two axes: +The function cv::repeat duplicates the input array one or more times along each of the two axes: \f[\texttt{dst} _{ij}= \texttt{src} _{i\mod src.rows, \; j\mod src.cols }\f] The second variant of the function is more convenient to use with @ref MatrixExpressions. @param src input array to replicate. -@param dst output array of the same type as src. -@param ny Flag to specify how many times the src is repeated along the +@param ny Flag to specify how many times the `src` is repeated along the vertical axis. -@param nx Flag to specify how many times the src is repeated along the +@param nx Flag to specify how many times the `src` is repeated along the horizontal axis. -@sa reduce +@param dst output array of the same type as `src`. +@sa cv::reduce */ CV_EXPORTS_W void repeat(InputArray src, int ny, int nx, OutputArray dst); /** @overload @param src input array to replicate. -@param ny Flag to specify how many times the src is repeated along the +@param ny Flag to specify how many times the `src` is repeated along the vertical axis. -@param nx Flag to specify how many times the src is repeated along the +@param nx Flag to specify how many times the `src` is repeated along the horizontal axis. */ CV_EXPORTS Mat repeat(const Mat& src, int ny, int nx); @@ -1509,7 +1520,7 @@ CV_EXPORTS_W void magnitude(InputArray x, InputArray y, OutputArray magnitude); /** @brief Checks every element of an input array for invalid values. -The functions checkRange check that every array element is neither NaN nor infinite. When minVal \< +The functions checkRange check that every array element is neither NaN nor infinite. When minVal \> -DBL_MAX and maxVal \< DBL_MAX, the functions also check that each value is between minVal and maxVal. In case of multi-channel arrays, each channel is processed independently. If some values are out of range, position of the first outlier is stored in pos (when pos != NULL). Then, the @@ -2024,9 +2035,9 @@ so you need to "flip" the second convolution operand B vertically and horizontal - An example using the discrete fourier transform can be found at opencv_source_code/samples/cpp/dft.cpp - (Python) An example using the dft functionality to perform Wiener deconvolution can be found - at opencv_source/samples/python2/deconvolution.py + at opencv_source/samples/python/deconvolution.py - (Python) An example rearranging the quadrants of a Fourier image can be found at - opencv_source/samples/python2/dft.py + opencv_source/samples/python/dft.py @param src input array that could be real or complex. @param dst output array whose size and type depends on the flags . @param flags transformation flags, representing a combination of the cv::DftFlags @@ -2837,7 +2848,7 @@ and groups the input samples around the clusters. As an output, \f$\texttt{label @note - (Python) An example on K-means clustering can be found at - opencv_source_code/samples/python2/kmeans.py + opencv_source_code/samples/python/kmeans.py @param data Data for clustering. An array of N-Dimensional points with float coordinates is needed. Examples of this array can be: - Mat points(count, 2, CV_32F); @@ -2906,6 +2917,21 @@ public: }; +static inline +String& operator << (String& out, Ptr fmtd) +{ + fmtd->reset(); + for(const char* str = fmtd->next(); str; str = fmtd->next()) + out += cv::String(str); + return out; +} + +static inline +String& operator << (String& out, const Mat& mtx) +{ + return out << Formatter::get()->format(mtx); +} + //////////////////////////////////////// Algorithm //////////////////////////////////// class CV_EXPORTS Algorithm; diff --git a/modules/core/include/opencv2/core/affine.hpp b/modules/core/include/opencv2/core/affine.hpp index 2bce5b989..7f8deb5d7 100644 --- a/modules/core/include/opencv2/core/affine.hpp +++ b/modules/core/include/opencv2/core/affine.hpp @@ -241,30 +241,25 @@ void cv::Affine3::rotation(const Mat3& R) template inline void cv::Affine3::rotation(const Vec3& _rvec) { - double rx = _rvec[0], ry = _rvec[1], rz = _rvec[2]; - double theta = std::sqrt(rx*rx + ry*ry + rz*rz); + double theta = norm(_rvec); if (theta < DBL_EPSILON) rotation(Mat3::eye()); else { - const double I[] = { 1, 0, 0, 0, 1, 0, 0, 0, 1 }; - double c = std::cos(theta); double s = std::sin(theta); double c1 = 1. - c; double itheta = (theta != 0) ? 1./theta : 0.; - rx *= itheta; ry *= itheta; rz *= itheta; + Point3_ r = _rvec*itheta; - double rrt[] = { rx*rx, rx*ry, rx*rz, rx*ry, ry*ry, ry*rz, rx*rz, ry*rz, rz*rz }; - double _r_x_[] = { 0, -rz, ry, rz, 0, -rx, -ry, rx, 0 }; - Mat3 R; + Mat3 rrt( r.x*r.x, r.x*r.y, r.x*r.z, r.x*r.y, r.y*r.y, r.y*r.z, r.x*r.z, r.y*r.z, r.z*r.z ); + Mat3 r_x( 0, -r.z, r.y, r.z, 0, -r.x, -r.y, r.x, 0 ); // R = cos(theta)*I + (1 - cos(theta))*r*rT + sin(theta)*[r_x] // where [r_x] is [0 -rz ry; rz 0 -rx; -ry rx 0] - for(int k = 0; k < 9; ++k) - R.val[k] = static_cast(c*I[k] + c1*rrt[k] + s*_r_x_[k]); + Mat3 R = c*Mat3::eye() + c1*rrt + s*r_x; rotation(R); } diff --git a/modules/core/include/opencv2/core/base.hpp b/modules/core/include/opencv2/core/base.hpp index a8a0b23e1..ed633f5dc 100644 --- a/modules/core/include/opencv2/core/base.hpp +++ b/modules/core/include/opencv2/core/base.hpp @@ -50,10 +50,10 @@ #endif #include +#include #include "opencv2/core/cvdef.h" #include "opencv2/core/cvstd.hpp" -#include "opencv2/hal.hpp" namespace cv { @@ -679,8 +679,11 @@ CV_EXPORTS void setUseIPP(bool flag); //! @} core_utils + + + } // cv -#include "opencv2/hal/neon_utils.hpp" +#include "opencv2/core/neon_utils.hpp" #endif //__OPENCV_CORE_BASE_HPP__ diff --git a/modules/core/include/opencv2/core/cuda.hpp b/modules/core/include/opencv2/core/cuda.hpp index a9c7a39a8..64bc53ef5 100644 --- a/modules/core/include/opencv2/core/cuda.hpp +++ b/modules/core/include/opencv2/core/cuda.hpp @@ -528,6 +528,7 @@ public: private: Ptr impl_; + Event(const Ptr& impl); friend struct EventAccessor; }; diff --git a/modules/core/include/opencv2/core/cuda.inl.hpp b/modules/core/include/opencv2/core/cuda.inl.hpp index d9ab2ae4f..01dc6d7c0 100644 --- a/modules/core/include/opencv2/core/cuda.inl.hpp +++ b/modules/core/include/opencv2/core/cuda.inl.hpp @@ -540,6 +540,16 @@ Stream::Stream(const Ptr& impl) { } +//=================================================================================== +// Event +//=================================================================================== + +inline +Event::Event(const Ptr& impl) + : impl_(impl) +{ +} + //=================================================================================== // Initialization & Info //=================================================================================== diff --git a/modules/core/include/opencv2/core/cuda/detail/reduce.hpp b/modules/core/include/opencv2/core/cuda/detail/reduce.hpp index 0c35eaba6..44400c8e1 100644 --- a/modules/core/include/opencv2/core/cuda/detail/reduce.hpp +++ b/modules/core/include/opencv2/core/cuda/detail/reduce.hpp @@ -275,7 +275,7 @@ namespace cv { namespace cuda { namespace device template static __device__ void reduce(Pointer smem, Reference val, unsigned int tid, Op op) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 (void) smem; (void) tid; @@ -298,7 +298,7 @@ namespace cv { namespace cuda { namespace device { const unsigned int laneId = Warp::laneId(); - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 Unroll<16, Pointer, Reference, Op>::loopShfl(val, op, warpSize); if (laneId == 0) @@ -321,7 +321,7 @@ namespace cv { namespace cuda { namespace device if (tid < 32) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 Unroll::loopShfl(val, op, M); #else Unroll::loop(smem, val, tid, op); diff --git a/modules/core/include/opencv2/core/cuda/saturate_cast.hpp b/modules/core/include/opencv2/core/cuda/saturate_cast.hpp index e7633c76a..f55ae4f4a 100644 --- a/modules/core/include/opencv2/core/cuda/saturate_cast.hpp +++ b/modules/core/include/opencv2/core/cuda/saturate_cast.hpp @@ -101,7 +101,7 @@ namespace cv { namespace cuda { namespace device } template<> __device__ __forceinline__ uchar saturate_cast(double v) { - #if __CUDA_ARCH__ >= 130 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130 uint res = 0; asm("cvt.rni.sat.u8.f64 %0, %1;" : "=r"(res) : "d"(v)); return res; @@ -149,7 +149,7 @@ namespace cv { namespace cuda { namespace device } template<> __device__ __forceinline__ schar saturate_cast(double v) { - #if __CUDA_ARCH__ >= 130 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130 uint res = 0; asm("cvt.rni.sat.s8.f64 %0, %1;" : "=r"(res) : "d"(v)); return res; @@ -191,7 +191,7 @@ namespace cv { namespace cuda { namespace device } template<> __device__ __forceinline__ ushort saturate_cast(double v) { - #if __CUDA_ARCH__ >= 130 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130 ushort res = 0; asm("cvt.rni.sat.u16.f64 %0, %1;" : "=h"(res) : "d"(v)); return res; @@ -226,7 +226,7 @@ namespace cv { namespace cuda { namespace device } template<> __device__ __forceinline__ short saturate_cast(double v) { - #if __CUDA_ARCH__ >= 130 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 130 short res = 0; asm("cvt.rni.sat.s16.f64 %0, %1;" : "=h"(res) : "d"(v)); return res; diff --git a/modules/core/include/opencv2/core/cuda/warp_shuffle.hpp b/modules/core/include/opencv2/core/cuda/warp_shuffle.hpp index 5cf42ec41..256fc2a68 100644 --- a/modules/core/include/opencv2/core/cuda/warp_shuffle.hpp +++ b/modules/core/include/opencv2/core/cuda/warp_shuffle.hpp @@ -54,7 +54,7 @@ namespace cv { namespace cuda { namespace device template __device__ __forceinline__ T shfl(T val, int srcLane, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return __shfl(val, srcLane, width); #else return T(); @@ -62,7 +62,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ unsigned int shfl(unsigned int val, int srcLane, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return (unsigned int) __shfl((int) val, srcLane, width); #else return 0; @@ -70,7 +70,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ double shfl(double val, int srcLane, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 int lo = __double2loint(val); int hi = __double2hiint(val); @@ -86,7 +86,7 @@ namespace cv { namespace cuda { namespace device template __device__ __forceinline__ T shfl_down(T val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return __shfl_down(val, delta, width); #else return T(); @@ -94,7 +94,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ unsigned int shfl_down(unsigned int val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return (unsigned int) __shfl_down((int) val, delta, width); #else return 0; @@ -102,7 +102,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ double shfl_down(double val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 int lo = __double2loint(val); int hi = __double2hiint(val); @@ -118,7 +118,7 @@ namespace cv { namespace cuda { namespace device template __device__ __forceinline__ T shfl_up(T val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return __shfl_up(val, delta, width); #else return T(); @@ -126,7 +126,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ unsigned int shfl_up(unsigned int val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 return (unsigned int) __shfl_up((int) val, delta, width); #else return 0; @@ -134,7 +134,7 @@ namespace cv { namespace cuda { namespace device } __device__ __forceinline__ double shfl_up(double val, unsigned int delta, int width = warpSize) { - #if __CUDA_ARCH__ >= 300 + #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 int lo = __double2loint(val); int hi = __double2hiint(val); diff --git a/modules/core/include/opencv2/core/cuda_stream_accessor.hpp b/modules/core/include/opencv2/core/cuda_stream_accessor.hpp index dd6589bcb..0f8ee9b2d 100644 --- a/modules/core/include/opencv2/core/cuda_stream_accessor.hpp +++ b/modules/core/include/opencv2/core/cuda_stream_accessor.hpp @@ -52,7 +52,7 @@ */ #include -#include "opencv2/core/cvdef.h" +#include "opencv2/core/cuda.hpp" namespace cv { @@ -62,14 +62,12 @@ namespace cv //! @addtogroup cudacore_struct //! @{ - class Stream; - class Event; - /** @brief Class that enables getting cudaStream_t from cuda::Stream */ struct StreamAccessor { CV_EXPORTS static cudaStream_t getStream(const Stream& stream); + CV_EXPORTS static Stream wrapStream(cudaStream_t stream); }; /** @brief Class that enables getting cudaEvent_t from cuda::Event @@ -77,6 +75,7 @@ namespace cv struct EventAccessor { CV_EXPORTS static cudaEvent_t getEvent(const Event& event); + CV_EXPORTS static Event wrapEvent(cudaEvent_t event); }; //! @} diff --git a/modules/core/include/opencv2/core/cvdef.h b/modules/core/include/opencv2/core/cvdef.h index 9947bdfdb..af2abfbb2 100644 --- a/modules/core/include/opencv2/core/cvdef.h +++ b/modules/core/include/opencv2/core/cvdef.h @@ -45,6 +45,9 @@ #ifndef __OPENCV_CORE_CVDEF_H__ #define __OPENCV_CORE_CVDEF_H__ +//! @addtogroup core_utils +//! @{ + #if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER && _MSC_VER > 1300 # define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio warnings */ #endif @@ -56,7 +59,265 @@ #undef abs #undef Complex -#include "opencv2/hal/defs.h" +#if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER && _MSC_VER > 1300 +# define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio warnings */ +#endif + +#include +#include "opencv2/core/hal/interface.h" + +#if defined __ICL +# define CV_ICC __ICL +#elif defined __ICC +# define CV_ICC __ICC +#elif defined __ECL +# define CV_ICC __ECL +#elif defined __ECC +# define CV_ICC __ECC +#elif defined __INTEL_COMPILER +# define CV_ICC __INTEL_COMPILER +#endif + +#ifndef CV_INLINE +# if defined __cplusplus +# define CV_INLINE static inline +# elif defined _MSC_VER +# define CV_INLINE __inline +# else +# define CV_INLINE static +# endif +#endif + +#if defined CV_ICC && !defined CV_ENABLE_UNROLLED +# define CV_ENABLE_UNROLLED 0 +#else +# define CV_ENABLE_UNROLLED 1 +#endif + +#ifdef __GNUC__ +# define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x))) +#elif defined _MSC_VER +# define CV_DECL_ALIGNED(x) __declspec(align(x)) +#else +# define CV_DECL_ALIGNED(x) +#endif + +/* CPU features and intrinsics support */ +#define CV_CPU_NONE 0 +#define CV_CPU_MMX 1 +#define CV_CPU_SSE 2 +#define CV_CPU_SSE2 3 +#define CV_CPU_SSE3 4 +#define CV_CPU_SSSE3 5 +#define CV_CPU_SSE4_1 6 +#define CV_CPU_SSE4_2 7 +#define CV_CPU_POPCNT 8 + +#define CV_CPU_AVX 10 +#define CV_CPU_AVX2 11 +#define CV_CPU_FMA3 12 + +#define CV_CPU_AVX_512F 13 +#define CV_CPU_AVX_512BW 14 +#define CV_CPU_AVX_512CD 15 +#define CV_CPU_AVX_512DQ 16 +#define CV_CPU_AVX_512ER 17 +#define CV_CPU_AVX_512IFMA512 18 +#define CV_CPU_AVX_512PF 19 +#define CV_CPU_AVX_512VBMI 20 +#define CV_CPU_AVX_512VL 21 + +#define CV_CPU_NEON 100 + +// when adding to this list remember to update the following enum +#define CV_HARDWARE_MAX_FEATURE 255 + +/** @brief Available CPU features. +*/ +enum CpuFeatures { + CPU_MMX = 1, + CPU_SSE = 2, + CPU_SSE2 = 3, + CPU_SSE3 = 4, + CPU_SSSE3 = 5, + CPU_SSE4_1 = 6, + CPU_SSE4_2 = 7, + CPU_POPCNT = 8, + + CPU_AVX = 10, + CPU_AVX2 = 11, + CPU_FMA3 = 12, + + CPU_AVX_512F = 13, + CPU_AVX_512BW = 14, + CPU_AVX_512CD = 15, + CPU_AVX_512DQ = 16, + CPU_AVX_512ER = 17, + CPU_AVX_512IFMA512 = 18, + CPU_AVX_512PF = 19, + CPU_AVX_512VBMI = 20, + CPU_AVX_512VL = 21, + + CPU_NEON = 100 +}; + +// do not include SSE/AVX/NEON headers for NVCC compiler +#ifndef __CUDACC__ + +#if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2) +# include +# define CV_MMX 1 +# define CV_SSE 1 +# define CV_SSE2 1 +# if defined __SSE3__ || (defined _MSC_VER && _MSC_VER >= 1500) +# include +# define CV_SSE3 1 +# endif +# if defined __SSSE3__ || (defined _MSC_VER && _MSC_VER >= 1500) +# include +# define CV_SSSE3 1 +# endif +# if defined __SSE4_1__ || (defined _MSC_VER && _MSC_VER >= 1500) +# include +# define CV_SSE4_1 1 +# endif +# if defined __SSE4_2__ || (defined _MSC_VER && _MSC_VER >= 1500) +# include +# define CV_SSE4_2 1 +# endif +# if defined __POPCNT__ || (defined _MSC_VER && _MSC_VER >= 1500) +# ifdef _MSC_VER +# include +# else +# include +# endif +# define CV_POPCNT 1 +# endif +# if defined __AVX__ || (defined _MSC_VER && _MSC_VER >= 1600 && 0) +// MS Visual Studio 2010 (2012?) has no macro pre-defined to identify the use of /arch:AVX +// See: http://connect.microsoft.com/VisualStudio/feedback/details/605858/arch-avx-should-define-a-predefined-macro-in-x64-and-set-a-unique-value-for-m-ix86-fp-in-win32 +# include +# define CV_AVX 1 +# if defined(_XCR_XFEATURE_ENABLED_MASK) +# define __xgetbv() _xgetbv(_XCR_XFEATURE_ENABLED_MASK) +# else +# define __xgetbv() 0 +# endif +# endif +# if defined __AVX2__ || (defined _MSC_VER && _MSC_VER >= 1800 && 0) +# include +# define CV_AVX2 1 +# if defined __FMA__ +# define CV_FMA3 1 +# endif +# endif +#endif + +#if (defined WIN32 || defined _WIN32) && defined(_M_ARM) +# include +# include "arm_neon.h" +# define CV_NEON 1 +# define CPU_HAS_NEON_FEATURE (true) +#elif defined(__ARM_NEON__) || (defined (__ARM_NEON) && defined(__aarch64__)) +# include +# define CV_NEON 1 +#endif + +#if defined __GNUC__ && defined __arm__ && (defined __ARM_PCS_VFP || defined __ARM_VFPV3__ || defined __ARM_NEON__) && !defined __SOFTFP__ +# define CV_VFP 1 +#endif + +#endif // __CUDACC__ + +#ifndef CV_POPCNT +#define CV_POPCNT 0 +#endif +#ifndef CV_MMX +# define CV_MMX 0 +#endif +#ifndef CV_SSE +# define CV_SSE 0 +#endif +#ifndef CV_SSE2 +# define CV_SSE2 0 +#endif +#ifndef CV_SSE3 +# define CV_SSE3 0 +#endif +#ifndef CV_SSSE3 +# define CV_SSSE3 0 +#endif +#ifndef CV_SSE4_1 +# define CV_SSE4_1 0 +#endif +#ifndef CV_SSE4_2 +# define CV_SSE4_2 0 +#endif +#ifndef CV_AVX +# define CV_AVX 0 +#endif +#ifndef CV_AVX2 +# define CV_AVX2 0 +#endif +#ifndef CV_FMA3 +# define CV_FMA3 0 +#endif +#ifndef CV_AVX_512F +# define CV_AVX_512F 0 +#endif +#ifndef CV_AVX_512BW +# define CV_AVX_512BW 0 +#endif +#ifndef CV_AVX_512CD +# define CV_AVX_512CD 0 +#endif +#ifndef CV_AVX_512DQ +# define CV_AVX_512DQ 0 +#endif +#ifndef CV_AVX_512ER +# define CV_AVX_512ER 0 +#endif +#ifndef CV_AVX_512IFMA512 +# define CV_AVX_512IFMA512 0 +#endif +#ifndef CV_AVX_512PF +# define CV_AVX_512PF 0 +#endif +#ifndef CV_AVX_512VBMI +# define CV_AVX_512VBMI 0 +#endif +#ifndef CV_AVX_512VL +# define CV_AVX_512VL 0 +#endif + +#ifndef CV_NEON +# define CV_NEON 0 +#endif + +#ifndef CV_VFP +# define CV_VFP 0 +#endif + +/* fundamental constants */ +#define CV_PI 3.1415926535897932384626433832795 +#define CV_2PI 6.283185307179586476925286766559 +#define CV_LOG2 0.69314718055994530941723212145818 + +typedef union Cv32suf +{ + int i; + unsigned u; + float f; +} +Cv32suf; + +typedef union Cv64suf +{ + int64 i; + uint64 u; + double f; +} +Cv64suf; #define OPENCV_ABI_COMPATIBILITY 300 @@ -169,12 +430,12 @@ #define CV_SUBMAT_FLAG (1 << CV_SUBMAT_FLAG_SHIFT) #define CV_IS_SUBMAT(flags) ((flags) & CV_MAT_SUBMAT_FLAG) -/* Size of each channel item, +/** Size of each channel item, 0x124489 = 1000 0100 0100 0010 0010 0001 0001 ~ array of sizeof(arr_type_elem) */ #define CV_ELEM_SIZE1(type) \ ((((sizeof(size_t)<<28)|0x8442211) >> CV_MAT_DEPTH(type)*4) & 15) -/* 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */ +/** 0x3a50 = 11 10 10 01 01 00 00 ~ array of log2(sizeof(arr_type_elem)) */ #define CV_ELEM_SIZE(type) \ (CV_MAT_CN(type) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> CV_MAT_DEPTH(type)*2) & 3)) @@ -249,4 +510,6 @@ # endif #endif +//! @} + #endif // __OPENCV_CORE_CVDEF_H__ diff --git a/modules/core/include/opencv2/core/fast_math.hpp b/modules/core/include/opencv2/core/fast_math.hpp new file mode 100644 index 000000000..b8b241b02 --- /dev/null +++ b/modules/core/include/opencv2/core/fast_math.hpp @@ -0,0 +1,302 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Copyright (C) 2015, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_CORE_FAST_MATH_HPP__ +#define __OPENCV_CORE_FAST_MATH_HPP__ + +#include "opencv2/core/cvdef.h" + +//! @addtogroup core_utils +//! @{ + +/****************************************************************************************\ +* fast math * +\****************************************************************************************/ + +#if defined __BORLANDC__ +# include +#elif defined __cplusplus +# include +#else +# include +#endif + +#ifdef HAVE_TEGRA_OPTIMIZATION +# include "tegra_round.hpp" +#endif + +#if CV_VFP + // 1. general scheme + #define ARM_ROUND(_value, _asm_string) \ + int res; \ + float temp; \ + asm(_asm_string : [res] "=r" (res), [temp] "=w" (temp) : [value] "w" (_value)); \ + return res + // 2. version for double + #ifdef __clang__ + #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %[value] \n vmov %[res], %[temp]") + #else + #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %P[value] \n vmov %[res], %[temp]") + #endif + // 3. version for float + #define ARM_ROUND_FLT(value) ARM_ROUND(value, "vcvtr.s32.f32 %[temp], %[value]\n vmov %[res], %[temp]") +#endif // CV_VFP + +/** @brief Rounds floating-point number to the nearest integer + + @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the + result is not defined. + */ +CV_INLINE int +cvRound( double value ) +{ +#if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ \ + && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) + __m128d t = _mm_set_sd( value ); + return _mm_cvtsd_si32(t); +#elif defined _MSC_VER && defined _M_IX86 + int t; + __asm + { + fld value; + fistp t; + } + return t; +#elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \ + defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION + TEGRA_ROUND_DBL(value); +#elif defined CV_ICC || defined __GNUC__ +# if CV_VFP + ARM_ROUND_DBL(value); +# else + return (int)lrint(value); +# endif +#else + /* it's ok if round does not comply with IEEE754 standard; + the tests should allow +/-1 difference when the tested functions use round */ + return (int)(value + (value >= 0 ? 0.5 : -0.5)); +#endif +} + + +/** @brief Rounds floating-point number to the nearest integer not larger than the original. + + The function computes an integer i such that: + \f[i \le \texttt{value} < i+1\f] + @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the + result is not defined. + */ +CV_INLINE int cvFloor( double value ) +{ +#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) + __m128d t = _mm_set_sd( value ); + int i = _mm_cvtsd_si32(t); + return i - _mm_movemask_pd(_mm_cmplt_sd(t, _mm_cvtsi32_sd(t,i))); +#elif defined __GNUC__ + int i = (int)value; + return i - (i > value); +#else + int i = cvRound(value); + float diff = (float)(value - i); + return i - (diff < 0); +#endif +} + +/** @brief Rounds floating-point number to the nearest integer not smaller than the original. + + The function computes an integer i such that: + \f[i \le \texttt{value} < i+1\f] + @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the + result is not defined. + */ +CV_INLINE int cvCeil( double value ) +{ +#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__) + __m128d t = _mm_set_sd( value ); + int i = _mm_cvtsd_si32(t); + return i + _mm_movemask_pd(_mm_cmplt_sd(_mm_cvtsi32_sd(t,i), t)); +#elif defined __GNUC__ + int i = (int)value; + return i + (i < value); +#else + int i = cvRound(value); + float diff = (float)(i - value); + return i + (diff < 0); +#endif +} + +/** @brief Determines if the argument is Not A Number. + + @param value The input floating-point value + + The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0 + otherwise. */ +CV_INLINE int cvIsNaN( double value ) +{ + Cv64suf ieee754; + ieee754.f = value; + return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) + + ((unsigned)ieee754.u != 0) > 0x7ff00000; +} + +/** @brief Determines if the argument is Infinity. + + @param value The input floating-point value + + The function returns 1 if the argument is a plus or minus infinity (as defined by IEEE754 standard) + and 0 otherwise. */ +CV_INLINE int cvIsInf( double value ) +{ + Cv64suf ieee754; + ieee754.f = value; + return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 && + (unsigned)ieee754.u == 0; +} + +#ifdef __cplusplus + +/** @overload */ +CV_INLINE int cvRound(float value) +{ +#if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ && \ + defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) + __m128 t = _mm_set_ss( value ); + return _mm_cvtss_si32(t); +#elif defined _MSC_VER && defined _M_IX86 + int t; + __asm + { + fld value; + fistp t; + } + return t; +#elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \ + defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION + TEGRA_ROUND_FLT(value); +#elif defined CV_ICC || defined __GNUC__ +# if CV_VFP + ARM_ROUND_FLT(value); +# else + return (int)lrintf(value); +# endif +#else + /* it's ok if round does not comply with IEEE754 standard; + the tests should allow +/-1 difference when the tested functions use round */ + return (int)(value + (value >= 0 ? 0.5f : -0.5f)); +#endif +} + +/** @overload */ +CV_INLINE int cvRound( int value ) +{ + return value; +} + +/** @overload */ +CV_INLINE int cvFloor( float value ) +{ +#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) + __m128 t = _mm_set_ss( value ); + int i = _mm_cvtss_si32(t); + return i - _mm_movemask_ps(_mm_cmplt_ss(t, _mm_cvtsi32_ss(t,i))); +#elif defined __GNUC__ + int i = (int)value; + return i - (i > value); +#else + int i = cvRound(value); + float diff = (float)(value - i); + return i - (diff < 0); +#endif +} + +/** @overload */ +CV_INLINE int cvFloor( int value ) +{ + return value; +} + +/** @overload */ +CV_INLINE int cvCeil( float value ) +{ +#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__) + __m128 t = _mm_set_ss( value ); + int i = _mm_cvtss_si32(t); + return i + _mm_movemask_ps(_mm_cmplt_ss(_mm_cvtsi32_ss(t,i), t)); +#elif defined __GNUC__ + int i = (int)value; + return i + (i < value); +#else + int i = cvRound(value); + float diff = (float)(i - value); + return i + (diff < 0); +#endif +} + +/** @overload */ +CV_INLINE int cvCeil( int value ) +{ + return value; +} + +/** @overload */ +CV_INLINE int cvIsNaN( float value ) +{ + Cv32suf ieee754; + ieee754.f = value; + return (ieee754.u & 0x7fffffff) > 0x7f800000; +} + +/** @overload */ +CV_INLINE int cvIsInf( float value ) +{ + Cv32suf ieee754; + ieee754.f = value; + return (ieee754.u & 0x7fffffff) == 0x7f800000; +} + +#endif // __cplusplus + +//! @} core_utils + +#endif diff --git a/modules/core/include/opencv2/core/hal/hal.hpp b/modules/core/include/opencv2/core/hal/hal.hpp new file mode 100644 index 000000000..118913eb7 --- /dev/null +++ b/modules/core/include/opencv2/core/hal/hal.hpp @@ -0,0 +1,218 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Copyright (C) 2015, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_HAL_HPP__ +#define __OPENCV_HAL_HPP__ + +#include "opencv2/core/cvdef.h" +#include "opencv2/core/hal/interface.h" + +//! @cond IGNORED +#define CALL_HAL(name, fun, ...) \ + int res = fun(__VA_ARGS__); \ + if (res == CV_HAL_ERROR_OK) \ + return; \ + else if (res != CV_HAL_ERROR_NOT_IMPLEMENTED) \ + CV_Error_(cv::Error::StsInternal, \ + ("HAL implementation " CVAUX_STR(name) " ==> " CVAUX_STR(fun) " returned %d (0x%08x)", res, res)); +//! @endcond + + +namespace cv { namespace hal { + +//! @addtogroup core_hal_functions +//! @{ + +CV_EXPORTS int normHamming(const uchar* a, int n); +CV_EXPORTS int normHamming(const uchar* a, const uchar* b, int n); + +CV_EXPORTS int normHamming(const uchar* a, int n, int cellSize); +CV_EXPORTS int normHamming(const uchar* a, const uchar* b, int n, int cellSize); + +CV_EXPORTS int LU32f(float* A, size_t astep, int m, float* b, size_t bstep, int n); +CV_EXPORTS int LU64f(double* A, size_t astep, int m, double* b, size_t bstep, int n); +CV_EXPORTS bool Cholesky32f(float* A, size_t astep, int m, float* b, size_t bstep, int n); +CV_EXPORTS bool Cholesky64f(double* A, size_t astep, int m, double* b, size_t bstep, int n); + +CV_EXPORTS int normL1_(const uchar* a, const uchar* b, int n); +CV_EXPORTS float normL1_(const float* a, const float* b, int n); +CV_EXPORTS float normL2Sqr_(const float* a, const float* b, int n); + +CV_EXPORTS void exp32f(const float* src, float* dst, int n); +CV_EXPORTS void exp64f(const double* src, double* dst, int n); +CV_EXPORTS void log32f(const float* src, float* dst, int n); +CV_EXPORTS void log64f(const double* src, double* dst, int n); + +CV_EXPORTS void fastAtan2(const float* y, const float* x, float* dst, int n, bool angleInDegrees); +CV_EXPORTS void magnitude32f(const float* x, const float* y, float* dst, int n); +CV_EXPORTS void magnitude64f(const double* x, const double* y, double* dst, int n); +CV_EXPORTS void sqrt32f(const float* src, float* dst, int len); +CV_EXPORTS void sqrt64f(const double* src, double* dst, int len); +CV_EXPORTS void invSqrt32f(const float* src, float* dst, int len); +CV_EXPORTS void invSqrt64f(const double* src, double* dst, int len); + +CV_EXPORTS void split8u(const uchar* src, uchar** dst, int len, int cn ); +CV_EXPORTS void split16u(const ushort* src, ushort** dst, int len, int cn ); +CV_EXPORTS void split32s(const int* src, int** dst, int len, int cn ); +CV_EXPORTS void split64s(const int64* src, int64** dst, int len, int cn ); + +CV_EXPORTS void merge8u(const uchar** src, uchar* dst, int len, int cn ); +CV_EXPORTS void merge16u(const ushort** src, ushort* dst, int len, int cn ); +CV_EXPORTS void merge32s(const int** src, int* dst, int len, int cn ); +CV_EXPORTS void merge64s(const int64** src, int64* dst, int len, int cn ); + +CV_EXPORTS void add8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void add64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void sub8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void sub64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void max8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void max64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void min8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void min64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void absdiff8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void absdiff64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void and8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void or8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void xor8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); +CV_EXPORTS void not8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); + +CV_EXPORTS void cmp8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp8s(const schar* src1, size_t step1, const schar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp16u(const ushort* src1, size_t step1, const ushort* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp16s(const short* src1, size_t step1, const short* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp32s(const int* src1, size_t step1, const int* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp32f(const float* src1, size_t step1, const float* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); +CV_EXPORTS void cmp64f(const double* src1, size_t step1, const double* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); + +CV_EXPORTS void mul8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void mul64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); + +CV_EXPORTS void div8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void div64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); + +CV_EXPORTS void recip8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); +CV_EXPORTS void recip64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); + +CV_EXPORTS void addWeighted8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _scalars ); +CV_EXPORTS void addWeighted8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scalars ); +CV_EXPORTS void addWeighted16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scalars ); +CV_EXPORTS void addWeighted16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scalars ); +CV_EXPORTS void addWeighted32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scalars ); +CV_EXPORTS void addWeighted32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scalars ); +CV_EXPORTS void addWeighted64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scalars ); + +//! @} core_hal + +//============================================================================= +// for binary compatibility with 3.0 + +//! @cond IGNORED + +CV_EXPORTS int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n); +CV_EXPORTS int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n); +CV_EXPORTS bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n); +CV_EXPORTS bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n); + +CV_EXPORTS void exp(const float* src, float* dst, int n); +CV_EXPORTS void exp(const double* src, double* dst, int n); +CV_EXPORTS void log(const float* src, float* dst, int n); +CV_EXPORTS void log(const double* src, double* dst, int n); + +CV_EXPORTS void magnitude(const float* x, const float* y, float* dst, int n); +CV_EXPORTS void magnitude(const double* x, const double* y, double* dst, int n); +CV_EXPORTS void sqrt(const float* src, float* dst, int len); +CV_EXPORTS void sqrt(const double* src, double* dst, int len); +CV_EXPORTS void invSqrt(const float* src, float* dst, int len); +CV_EXPORTS void invSqrt(const double* src, double* dst, int len); + +//! @endcond + +}} //cv::hal + +#endif //__OPENCV_HAL_HPP__ diff --git a/modules/hal/include/opencv2/hal/interface.hpp b/modules/core/include/opencv2/core/hal/interface.h similarity index 83% rename from modules/hal/include/opencv2/hal/interface.hpp rename to modules/core/include/opencv2/core/hal/interface.h index 2a5bff04d..51f760610 100644 --- a/modules/hal/include/opencv2/hal/interface.hpp +++ b/modules/core/include/opencv2/core/hal/interface.h @@ -1,8 +1,11 @@ #ifndef _HAL_INTERFACE_HPP_INCLUDED_ #define _HAL_INTERFACE_HPP_INCLUDED_ +//! @addtogroup core_hal_interface +//! @{ + #define CV_HAL_ERROR_OK 0 -#define CV_HAL_ERROR_NI 1 +#define CV_HAL_ERROR_NOT_IMPLEMENTED 1 #define CV_HAL_ERROR_UNKNOWN -1 #define CV_HAL_CMP_EQ 0 @@ -12,33 +15,6 @@ #define CV_HAL_CMP_LE 4 #define CV_HAL_CMP_NE 5 -#ifdef __cplusplus -namespace cv { namespace hal { - -namespace Error { - -enum -{ - Ok = 0, - NotImplemented = 1, - Unknown = -1 -}; - -} - -enum -{ - CMP_EQ = 0, - CMP_GT = 1, - CMP_GE = 2, - CMP_LT = 3, - CMP_LE = 4, - CMP_NE = 5 -}; - -}} -#endif - #ifdef __cplusplus #include #else @@ -88,4 +64,6 @@ typedef signed char schar; # define CV_BIG_UINT(n) n##ULL #endif +//! @} + #endif diff --git a/modules/hal/include/opencv2/hal/intrin.hpp b/modules/core/include/opencv2/core/hal/intrin.hpp similarity index 97% rename from modules/hal/include/opencv2/hal/intrin.hpp rename to modules/core/include/opencv2/core/hal/intrin.hpp index c8d59c964..33e14b486 100644 --- a/modules/hal/include/opencv2/hal/intrin.hpp +++ b/modules/core/include/opencv2/core/hal/intrin.hpp @@ -48,7 +48,7 @@ #include #include #include -#include "opencv2/hal/defs.h" +#include "opencv2/core/cvdef.h" #define OPENCV_HAL_ADD(a, b) ((a) + (b)) #define OPENCV_HAL_AND(a, b) ((a) & (b)) @@ -60,7 +60,7 @@ // access from within opencv code more accessible namespace cv { -//! @addtogroup hal_intrin +//! @addtogroup core_hal_intrin //! @{ //! @cond IGNORED @@ -290,19 +290,19 @@ template struct V_SIMD128Traits #if CV_SSE2 -#include "opencv2/hal/intrin_sse.hpp" +#include "opencv2/core/hal/intrin_sse.hpp" #elif CV_NEON -#include "opencv2/hal/intrin_neon.hpp" +#include "opencv2/core/hal/intrin_neon.hpp" #else -#include "opencv2/hal/intrin_cpp.hpp" +#include "opencv2/core/hal/intrin_cpp.hpp" #endif -//! @addtogroup hal_intrin +//! @addtogroup core_hal_intrin //! @{ #ifndef CV_SIMD128 diff --git a/modules/hal/include/opencv2/hal/intrin_cpp.hpp b/modules/core/include/opencv2/core/hal/intrin_cpp.hpp similarity index 98% rename from modules/hal/include/opencv2/hal/intrin_cpp.hpp rename to modules/core/include/opencv2/core/hal/intrin_cpp.hpp index e1b1044a3..3929e0d03 100644 --- a/modules/hal/include/opencv2/hal/intrin_cpp.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_cpp.hpp @@ -47,11 +47,13 @@ #include #include +#include +#include "opencv2/core/saturate.hpp" namespace cv { -/** @addtogroup hal_intrin +/** @addtogroup core_hal_intrin "Universal intrinsics" is a types and functions set intended to simplify vectorization of code on different platforms. Currently there are two supported SIMD extensions: __SSE/SSE2__ on x86 @@ -370,7 +372,7 @@ typedef v_reg v_uint64x2; typedef v_reg v_int64x2; //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_BIN_OP(bin_op) \ template inline v_reg<_Tp, n> \ operator bin_op (const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \ @@ -409,7 +411,7 @@ For floating types only. */ OPENCV_HAL_IMPL_BIN_OP(/) //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_BIT_OP(bit_op) \ template inline v_reg<_Tp, n> operator bit_op \ (const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \ @@ -458,7 +460,7 @@ template inline v_reg<_Tp, n> operator ~ (const v_reg<_Tp, } //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_MATH_FUNC(func, cfunc, _Tp2) \ template inline v_reg<_Tp2, n> func(const v_reg<_Tp, n>& a) \ { \ @@ -507,7 +509,7 @@ Only for floating point types.*/ OPENCV_HAL_IMPL_MATH_FUNC(v_trunc, int, int) //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_MINMAX_FUNC(func, cfunc) \ template inline v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \ { \ @@ -518,7 +520,7 @@ template inline v_reg<_Tp, n> func(const v_reg<_Tp, n>& a, } //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_REDUCE_MINMAX_FUNC(func, cfunc) \ template inline _Tp func(const v_reg<_Tp, n>& a) \ { \ @@ -584,7 +586,7 @@ inline void v_minmax( const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b, //! @endcond //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_CMP_OP(cmp_op) \ template \ inline v_reg<_Tp, n> operator cmp_op(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \ @@ -627,7 +629,7 @@ For all types except 64-bit integer values. */ OPENCV_HAL_IMPL_CMP_OP(!=) //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_ADD_SUB_OP(func, bin_op, cast_op, _Tp2) \ template \ inline v_reg<_Tp2, n> func(const v_reg<_Tp, n>& a, const v_reg<_Tp, n>& b) \ @@ -821,7 +823,7 @@ template inline void v_hsum(const v_reg<_Tp, n>& a, //! @endcond //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_SHIFT_OP(shift_op) \ template inline v_reg<_Tp, n> operator shift_op(const v_reg<_Tp, n>& a, int imm) \ { \ @@ -1465,7 +1467,7 @@ inline void v_transpose4x4( v_reg<_Tp, 4>& a0, const v_reg<_Tp, 4>& a1, } //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_INIT_ZERO(_Tpvec, _Tp, suffix) \ inline _Tpvec v_setzero_##suffix() { return _Tpvec::zero(); } @@ -1485,7 +1487,7 @@ OPENCV_HAL_IMPL_C_INIT_ZERO(v_int64x2, int64, s64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_INIT_VAL(_Tpvec, _Tp, suffix) \ inline _Tpvec v_setall_##suffix(_Tp val) { return _Tpvec::all(val); } @@ -1505,7 +1507,7 @@ OPENCV_HAL_IMPL_C_INIT_VAL(v_int64x2, int64, s64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_REINTERPRET(_Tpvec, _Tp, suffix) \ template inline _Tpvec \ v_reinterpret_as_##suffix(const v_reg<_Tp0, n0>& a) \ @@ -1527,7 +1529,7 @@ OPENCV_HAL_IMPL_C_REINTERPRET(v_int64x2, int64, s64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_SHIFTL(_Tpvec, _Tp) \ template inline _Tpvec v_shl(const _Tpvec& a) \ { return a << n; } @@ -1544,7 +1546,7 @@ OPENCV_HAL_IMPL_C_SHIFTL(v_int64x2, int64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_SHIFTR(_Tpvec, _Tp) \ template inline _Tpvec v_shr(const _Tpvec& a) \ { return a >> n; } @@ -1561,7 +1563,7 @@ OPENCV_HAL_IMPL_C_SHIFTR(v_int64x2, int64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_RSHIFTR(_Tpvec, _Tp) \ template inline _Tpvec v_rshr(const _Tpvec& a) \ { \ @@ -1583,7 +1585,7 @@ OPENCV_HAL_IMPL_C_RSHIFTR(v_int64x2, int64) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_PACK(_Tpvec, _Tpnvec, _Tpn, pack_suffix) \ inline _Tpnvec v_##pack_suffix(const _Tpvec& a, const _Tpvec& b) \ { \ @@ -1616,7 +1618,7 @@ OPENCV_HAL_IMPL_C_PACK(v_int32x4, v_uint16x8, ushort, pack_u) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_RSHR_PACK(_Tpvec, _Tp, _Tpnvec, _Tpn, pack_suffix) \ template inline _Tpnvec v_rshr_##pack_suffix(const _Tpvec& a, const _Tpvec& b) \ { \ @@ -1649,7 +1651,7 @@ OPENCV_HAL_IMPL_C_RSHR_PACK(v_int32x4, int, v_uint16x8, ushort, pack_u) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_PACK_STORE(_Tpvec, _Tp, _Tpnvec, _Tpn, pack_suffix) \ inline void v_##pack_suffix##_store(_Tpn* ptr, const _Tpvec& a) \ { \ @@ -1677,7 +1679,7 @@ OPENCV_HAL_IMPL_C_PACK_STORE(v_int32x4, int, v_uint16x8, ushort, pack_u) //! @} //! @brief Helper macro -//! @ingroup hal_intrin_impl +//! @ingroup core_hal_intrin_impl #define OPENCV_HAL_IMPL_C_RSHR_PACK_STORE(_Tpvec, _Tp, _Tpnvec, _Tpn, pack_suffix) \ template inline void v_rshr_##pack_suffix##_store(_Tpn* ptr, const _Tpvec& a) \ { \ diff --git a/modules/hal/include/opencv2/hal/intrin_neon.hpp b/modules/core/include/opencv2/core/hal/intrin_neon.hpp similarity index 99% rename from modules/hal/include/opencv2/hal/intrin_neon.hpp rename to modules/core/include/opencv2/core/hal/intrin_neon.hpp index d53971f96..f3e47ca8b 100644 --- a/modules/hal/include/opencv2/hal/intrin_neon.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_neon.hpp @@ -45,6 +45,8 @@ #ifndef __OPENCV_HAL_INTRIN_NEON_HPP__ #define __OPENCV_HAL_INTRIN_NEON_HPP__ +#include + namespace cv { diff --git a/modules/hal/include/opencv2/hal/intrin_sse.hpp b/modules/core/include/opencv2/core/hal/intrin_sse.hpp similarity index 99% rename from modules/hal/include/opencv2/hal/intrin_sse.hpp rename to modules/core/include/opencv2/core/hal/intrin_sse.hpp index e237ccd93..1840e0305 100644 --- a/modules/hal/include/opencv2/hal/intrin_sse.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_sse.hpp @@ -45,6 +45,8 @@ #ifndef __OPENCV_HAL_SSE_HPP__ #define __OPENCV_HAL_SSE_HPP__ +#include + #define CV_SIMD128 1 #define CV_SIMD128_64F 1 diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 6e67835d9..d55466378 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -1702,6 +1702,17 @@ public: for(int j = 0; j < H.cols; j++) H.at(i,j)=1./(i+j+1); @endcode + + Keep in mind that the size identifier used in the at operator cannot be chosen at random. It depends + on the image from which you are trying to retrieve the data. The table below gives a better insight in this: + - If matrix is of type `CV_8U` then use `Mat.at(y,x)`. + - If matrix is of type `CV_8S` then use `Mat.at(y,x)`. + - If matrix is of type `CV_16U` then use `Mat.at(y,x)`. + - If matrix is of type `CV_16S` then use `Mat.at(y,x)`. + - If matrix is of type `CV_32S` then use `Mat.at(y,x)`. + - If matrix is of type `CV_32F` then use `Mat.at(y,x)`. + - If matrix is of type `CV_64F` then use `Mat.at(y,x)`. + @param i0 Index along the dimension 0 */ template _Tp& at(int i0=0); @@ -1895,6 +1906,8 @@ public: MatAllocator* allocator; //! and the standard allocator static MatAllocator* getStdAllocator(); + static MatAllocator* getDefaultAllocator(); + static void setDefaultAllocator(MatAllocator* allocator); //! interaction with UMat UMatData* u; diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 54103401f..b4b141879 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -1338,6 +1338,11 @@ Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s) : Mat(_dims, _sz, DataType<_Tp>::type, Scalar(_s)) {} +template inline +Mat_<_Tp>::Mat_(int _dims, const int* _sz, _Tp* _data, const size_t* _steps) + : Mat(_dims, _sz, DataType<_Tp>::type, _data, _steps) +{} + template inline Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges) : Mat(m, ranges) diff --git a/modules/core/include/opencv2/core/matx.hpp b/modules/core/include/opencv2/core/matx.hpp index e3ffa9dfe..ca5f26198 100644 --- a/modules/core/include/opencv2/core/matx.hpp +++ b/modules/core/include/opencv2/core/matx.hpp @@ -51,6 +51,7 @@ #include "opencv2/core/cvdef.h" #include "opencv2/core/base.hpp" #include "opencv2/core/traits.hpp" +#include "opencv2/core/saturate.hpp" namespace cv { @@ -114,6 +115,10 @@ public: Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix + Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, + _Tp v4, _Tp v5, _Tp v6, _Tp v7, + _Tp v8, _Tp v9, _Tp v10, _Tp v11, + _Tp v12, _Tp v13); //!< 1x14, 2x7, 7x2 or 14x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, @@ -319,6 +324,7 @@ public: Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor + Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13); //!< 14-element vector constructor explicit Vec(const _Tp* values); Vec(const Vec<_Tp, cn>& v); @@ -581,6 +587,17 @@ Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp for(int i = 12; i < channels; i++) val[i] = _Tp(0); } +template inline +Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13) +{ + CV_StaticAssert(channels == 14, "Matx should have at least 14 elements."); + val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; + val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; + val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11; + val[12] = v12; val[13] = v13; +} + + template inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13, _Tp v14, _Tp v15) { @@ -931,6 +948,10 @@ template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {} +template inline +Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13) + : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {} + template inline Vec<_Tp, cn>::Vec(const _Tp* values) : Matx<_Tp, cn, 1>(values) {} diff --git a/modules/hal/include/opencv2/hal/neon_utils.hpp b/modules/core/include/opencv2/core/neon_utils.hpp similarity index 98% rename from modules/hal/include/opencv2/hal/neon_utils.hpp rename to modules/core/include/opencv2/core/neon_utils.hpp index 6026777a6..adb750fd4 100644 --- a/modules/hal/include/opencv2/hal/neon_utils.hpp +++ b/modules/core/include/opencv2/core/neon_utils.hpp @@ -42,9 +42,10 @@ #ifndef __OPENCV_HAL_NEON_UTILS_HPP__ #define __OPENCV_HAL_NEON_UTILS_HPP__ -#include "opencv2/hal/defs.h" +#include "opencv2/core/cvdef.h" -namespace cv { +//! @addtogroup core_utils_neon +//! @{ #if CV_NEON @@ -122,6 +123,6 @@ inline float32x2_t cv_vsqrt_f32(float32x2_t val) #endif -} +//! @} #endif // __OPENCV_HAL_NEON_UTILS_HPP__ diff --git a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp index f824ce933..801709e2c 100644 --- a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp +++ b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_core.hpp @@ -95,11 +95,7 @@ #define clUnloadPlatformCompiler clUnloadPlatformCompiler_ #define clWaitForEvents clWaitForEvents_ -#if defined __APPLE__ -#include -#else #include -#endif // generated by parser_cl.py #undef clBuildProgram diff --git a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp index f37ad158e..1ad4a6acc 100644 --- a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp +++ b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl.hpp @@ -17,11 +17,7 @@ #define clGetGLObjectInfo clGetGLObjectInfo_ #define clGetGLTextureInfo clGetGLTextureInfo_ -#if defined __APPLE__ -#include -#else #include -#endif // generated by parser_cl.py #undef clCreateFromGLBuffer @@ -45,6 +41,8 @@ #undef clGetGLTextureInfo #define clGetGLTextureInfo clGetGLTextureInfo_pfn +#ifdef cl_khr_gl_sharing + // generated by parser_cl.py extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLBuffer)(cl_context, cl_mem_flags, cl_GLuint, int*); extern CL_RUNTIME_EXPORT cl_mem (CL_API_CALL*clCreateFromGLRenderbuffer)(cl_context, cl_mem_flags, cl_GLuint, cl_int*); @@ -56,3 +54,5 @@ extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clEnqueueReleaseGLObjects)(cl_comma extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLContextInfoKHR)(const cl_context_properties*, cl_gl_context_info, size_t, void*, size_t*); extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLObjectInfo)(cl_mem, cl_gl_object_type*, cl_GLuint*); extern CL_RUNTIME_EXPORT cl_int (CL_API_CALL*clGetGLTextureInfo)(cl_mem, cl_gl_texture_info, size_t, void*, size_t*); + +#endif // cl_khr_gl_sharing diff --git a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp index 105867f47..8338ac681 100644 --- a/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp +++ b/modules/core/include/opencv2/core/opencl/runtime/autogenerated/opencl_gl_wrappers.hpp @@ -5,6 +5,8 @@ #error "Invalid usage" #endif +#ifdef cl_khr_gl_sharing + // generated by parser_cl.py #undef clCreateFromGLBuffer #define clCreateFromGLBuffer clCreateFromGLBuffer_fn @@ -36,3 +38,5 @@ inline cl_int clGetGLObjectInfo(cl_mem p0, cl_gl_object_type* p1, cl_GLuint* p2) #undef clGetGLTextureInfo #define clGetGLTextureInfo clGetGLTextureInfo_fn inline cl_int clGetGLTextureInfo(cl_mem p0, cl_gl_texture_info p1, size_t p2, void* p3, size_t* p4) { return clGetGLTextureInfo_pfn(p0, p1, p2, p3, p4); } + +#endif // cl_khr_gl_sharing diff --git a/modules/core/include/opencv2/core/saturate.hpp b/modules/core/include/opencv2/core/saturate.hpp new file mode 100644 index 000000000..1442eab11 --- /dev/null +++ b/modules/core/include/opencv2/core/saturate.hpp @@ -0,0 +1,150 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Copyright (C) 2014, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_CORE_SATURATE_HPP__ +#define __OPENCV_CORE_SATURATE_HPP__ + +#include "opencv2/core/cvdef.h" +#include "opencv2/core/fast_math.hpp" + +namespace cv +{ + +//! @addtogroup core_utils +//! @{ + +/////////////// saturate_cast (used in image & signal processing) /////////////////// + +/** @brief Template function for accurate conversion from one primitive type to another. + + The functions saturate_cast resemble the standard C++ cast operations, such as static_cast\() + and others. They perform an efficient and accurate conversion from one primitive type to another + (see the introduction chapter). saturate in the name means that when the input value v is out of the + range of the target type, the result is not formed just by taking low bits of the input, but instead + the value is clipped. For example: + @code + uchar a = saturate_cast(-100); // a = 0 (UCHAR_MIN) + short b = saturate_cast(33333.33333); // b = 32767 (SHRT_MAX) + @endcode + Such clipping is done when the target type is unsigned char , signed char , unsigned short or + signed short . For 32-bit integers, no clipping is done. + + When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), + the floating-point value is first rounded to the nearest integer and then clipped if needed (when + the target type is 8- or 16-bit). + + This operation is used in the simplest or most complex image processing functions in OpenCV. + + @param v Function parameter. + @sa add, subtract, multiply, divide, Mat::convertTo + */ +template static inline _Tp saturate_cast(uchar v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(schar v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(ushort v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(short v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(unsigned v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(int v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(float v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(double v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(int64 v) { return _Tp(v); } +/** @overload */ +template static inline _Tp saturate_cast(uint64 v) { return _Tp(v); } + +template<> inline uchar saturate_cast(schar v) { return (uchar)std::max((int)v, 0); } +template<> inline uchar saturate_cast(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); } +template<> inline uchar saturate_cast(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } +template<> inline uchar saturate_cast(short v) { return saturate_cast((int)v); } +template<> inline uchar saturate_cast(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); } +template<> inline uchar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline uchar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline uchar saturate_cast(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } +template<> inline uchar saturate_cast(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); } + +template<> inline schar saturate_cast(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); } +template<> inline schar saturate_cast(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); } +template<> inline schar saturate_cast(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } +template<> inline schar saturate_cast(short v) { return saturate_cast((int)v); } +template<> inline schar saturate_cast(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); } +template<> inline schar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline schar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline schar saturate_cast(int64 v) { return (schar)((uint64)((int64)v-SCHAR_MIN) <= (uint64)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } +template<> inline schar saturate_cast(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); } + +template<> inline ushort saturate_cast(schar v) { return (ushort)std::max((int)v, 0); } +template<> inline ushort saturate_cast(short v) { return (ushort)std::max((int)v, 0); } +template<> inline ushort saturate_cast(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } +template<> inline ushort saturate_cast(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); } +template<> inline ushort saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline ushort saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline ushort saturate_cast(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } +template<> inline ushort saturate_cast(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); } + +template<> inline short saturate_cast(ushort v) { return (short)std::min((int)v, SHRT_MAX); } +template<> inline short saturate_cast(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } +template<> inline short saturate_cast(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); } +template<> inline short saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline short saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } +template<> inline short saturate_cast(int64 v) { return (short)((uint64)((int64)v - SHRT_MIN) <= (uint64)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } +template<> inline short saturate_cast(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); } + +template<> inline int saturate_cast(float v) { return cvRound(v); } +template<> inline int saturate_cast(double v) { return cvRound(v); } + +// we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc. +template<> inline unsigned saturate_cast(float v) { return cvRound(v); } +template<> inline unsigned saturate_cast(double v) { return cvRound(v); } + +//! @} + +} // cv + +#endif // __OPENCV_CORE_SATURATE_HPP__ diff --git a/modules/hal/include/opencv2/hal/sse_utils.hpp b/modules/core/include/opencv2/core/sse_utils.hpp similarity index 99% rename from modules/hal/include/opencv2/hal/sse_utils.hpp rename to modules/core/include/opencv2/core/sse_utils.hpp index 9ce4098ba..c87b029ab 100644 --- a/modules/hal/include/opencv2/hal/sse_utils.hpp +++ b/modules/core/include/opencv2/core/sse_utils.hpp @@ -46,7 +46,10 @@ # error sse_utils.hpp header must be compiled as C++ #endif -#include "opencv2/hal/defs.h" +#include "opencv2/core/cvdef.h" + +//! @addtogroup core_utils_sse +//! @{ #if CV_SSE2 @@ -644,4 +647,6 @@ inline void _mm_interleave_ps(__m128 & v_r0, __m128 & v_r1, __m128 & v_g0, __m12 #endif // CV_SSE2 +//! @} + #endif //__OPENCV_CORE_SSE_UTILS_HPP__ diff --git a/modules/core/include/opencv2/core/version.hpp b/modules/core/include/opencv2/core/version.hpp index 4a46f7930..a69d42fcb 100644 --- a/modules/core/include/opencv2/core/version.hpp +++ b/modules/core/include/opencv2/core/version.hpp @@ -51,7 +51,7 @@ #define __OPENCV_VERSION_HPP__ #define CV_VERSION_MAJOR 3 -#define CV_VERSION_MINOR 0 +#define CV_VERSION_MINOR 1 #define CV_VERSION_REVISION 0 #define CV_VERSION_STATUS "-dev" diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index 06cd7916e..c3acca054 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -2258,4 +2258,1110 @@ cvMaxS( const void* srcarr1, double value, void* dstarr ) cv::max( src1, value, dst ); } + + +namespace cv { namespace hal { + +//======================================= + +#if (ARITHM_USE_IPP == 1) +static inline void fixSteps(int width, int height, size_t elemSize, size_t& step1, size_t& step2, size_t& step) +{ + if( height == 1 ) + step1 = step2 = step = width*elemSize; +} +#define CALL_IPP_BIN_E_12(fun) \ + CV_IPP_CHECK() \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + if (0 <= fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), 0)) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } + +#define CALL_IPP_BIN_E_21(fun) \ + CV_IPP_CHECK() \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + if (0 <= fun(src2, (int)step2, src1, (int)step1, dst, (int)step, ippiSize(width, height), 0)) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } + +#define CALL_IPP_BIN_12(fun) \ + CV_IPP_CHECK() \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + if (0 <= fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height))) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } + +#define CALL_IPP_BIN_21(fun) \ + CV_IPP_CHECK() \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + if (0 <= fun(src2, (int)step2, src1, (int)step1, dst, (int)step, ippiSize(width, height))) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } + +#else +#define CALL_IPP_BIN_E_12(fun) +#define CALL_IPP_BIN_E_21(fun) +#define CALL_IPP_BIN_12(fun) +#define CALL_IPP_BIN_21(fun) +#endif + + +//======================================= +// Add +//======================================= + +void add8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add8u, cv_hal_add8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_12(ippiAdd_8u_C1RSfs) + (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void add8s( const schar* src1, size_t step1, + const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add8s, cv_hal_add8s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); +} + +void add16u( const ushort* src1, size_t step1, + const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add16u, cv_hal_add16u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_12(ippiAdd_16u_C1RSfs) + (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void add16s( const short* src1, size_t step1, + const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add16s, cv_hal_add16s, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_12(ippiAdd_16s_C1RSfs) + (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void add32s( const int* src1, size_t step1, + const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add32s, cv_hal_add32s, src1, step1, src2, step2, dst, step, width, height) + vBinOp32, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); +} + +void add32f( const float* src1, size_t step1, + const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add32f, cv_hal_add32f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiAdd_32f_C1R) + (vBinOp32, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void add64f( const double* src1, size_t step1, + const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(add64f, cv_hal_add64f, src1, step1, src2, step2, dst, step, width, height) + vBinOp64, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); +} + +//======================================= +// Subtract +//======================================= + +void sub8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub8u, cv_hal_sub8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_21(ippiSub_8u_C1RSfs) + (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void sub8s( const schar* src1, size_t step1, + const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub8s, cv_hal_sub8s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); +} + +void sub16u( const ushort* src1, size_t step1, + const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub16u, cv_hal_sub16u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_21(ippiSub_16u_C1RSfs) + (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void sub16s( const short* src1, size_t step1, + const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub16s, cv_hal_sub16s, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_E_21(ippiSub_16s_C1RSfs) + (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void sub32s( const int* src1, size_t step1, + const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub32s, cv_hal_sub32s, src1, step1, src2, step2, dst, step, width, height) + vBinOp32, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); +} + +void sub32f( const float* src1, size_t step1, + const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub32f, cv_hal_sub32f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_21(ippiSub_32f_C1R) + (vBinOp32, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void sub64f( const double* src1, size_t step1, + const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(sub64f, cv_hal_sub64f, src1, step1, src2, step2, dst, step, width, height) + vBinOp64, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); +} + +//======================================= + +#if (ARITHM_USE_IPP == 1) +#define CALL_IPP_MIN_MAX(fun, type) \ + CV_IPP_CHECK() \ + { \ + type* s1 = (type*)src1; \ + type* s2 = (type*)src2; \ + type* d = dst; \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + int i = 0; \ + for(; i < height; i++) \ + { \ + if (0 > fun(s1, s2, d, width)) \ + break; \ + s1 = (type*)((uchar*)s1 + step1); \ + s2 = (type*)((uchar*)s2 + step2); \ + d = (type*)((uchar*)d + step); \ + } \ + if (i == height) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } +#else +#define CALL_IPP_MIN_MAX(fun, type) +#endif + +//======================================= +// Max +//======================================= + +void max8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max8u, cv_hal_max8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMaxEvery_8u, uchar) + vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max8s( const schar* src1, size_t step1, + const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max8s, cv_hal_max8s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max16u( const ushort* src1, size_t step1, + const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max16u, cv_hal_max16u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMaxEvery_16u, ushort) + vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max16s( const short* src1, size_t step1, + const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max16s, cv_hal_max16s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max32s( const int* src1, size_t step1, + const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max32s, cv_hal_max32s, src1, step1, src2, step2, dst, step, width, height) + vBinOp32, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max32f( const float* src1, size_t step1, + const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max32f, cv_hal_max32f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMaxEvery_32f, float) + vBinOp32, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +void max64f( const double* src1, size_t step1, + const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(max64f, cv_hal_max64f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMaxEvery_64f, double) + vBinOp64, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); +} + +//======================================= +// Min +//======================================= + +void min8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min8u, cv_hal_min8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMinEvery_8u, uchar) + vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min8s( const schar* src1, size_t step1, + const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min8s, cv_hal_min8s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min16u( const ushort* src1, size_t step1, + const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min16u, cv_hal_min16u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMinEvery_16u, ushort) + vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min16s( const short* src1, size_t step1, + const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min16s, cv_hal_min16s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min32s( const int* src1, size_t step1, + const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min32s, cv_hal_min32s, src1, step1, src2, step2, dst, step, width, height) + vBinOp32, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min32f( const float* src1, size_t step1, + const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min32f, cv_hal_min32f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMinEvery_32f, float) + vBinOp32, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +void min64f( const double* src1, size_t step1, + const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(min64f, cv_hal_min64f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_MIN_MAX(ippsMinEvery_64f, double) + vBinOp64, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); +} + +//======================================= +// AbsDiff +//======================================= + +void absdiff8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff8u, cv_hal_absdiff8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiAbsDiff_8u_C1R) + (vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void absdiff8s( const schar* src1, size_t step1, + const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff8s, cv_hal_absdiff8s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); +} + +void absdiff16u( const ushort* src1, size_t step1, + const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff16u, cv_hal_absdiff16u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiAbsDiff_16u_C1R) + (vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void absdiff16s( const short* src1, size_t step1, + const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff16s, cv_hal_absdiff16s, src1, step1, src2, step2, dst, step, width, height) + vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); +} + +void absdiff32s( const int* src1, size_t step1, + const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff32s, cv_hal_absdiff32s, src1, step1, src2, step2, dst, step, width, height) + vBinOp32, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); +} + +void absdiff32f( const float* src1, size_t step1, + const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff32f, cv_hal_absdiff32f, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiAbsDiff_32f_C1R) + (vBinOp32, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void absdiff64f( const double* src1, size_t step1, + const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(absdiff64f, cv_hal_absdiff64f, src1, step1, src2, step2, dst, step, width, height) + vBinOp64, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); +} + +//======================================= +// Logical +//======================================= + +#if (ARITHM_USE_IPP == 1) +#define CALL_IPP_UN(fun) \ + CV_IPP_CHECK() \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); (void)src2; \ + if (0 <= fun(src1, (int)step1, dst, (int)step, ippiSize(width, height))) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } +#else +#define CALL_IPP_UN(fun) +#endif + +void and8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(and8u, cv_hal_and8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiAnd_8u_C1R) + (vBinOp, IF_SIMD(VAnd)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void or8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(or8u, cv_hal_or8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiOr_8u_C1R) + (vBinOp, IF_SIMD(VOr)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void xor8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(xor8u, cv_hal_xor8u, src1, step1, src2, step2, dst, step, width, height) + CALL_IPP_BIN_12(ippiXor_8u_C1R) + (vBinOp, IF_SIMD(VXor)>(src1, step1, src2, step2, dst, step, width, height)); +} + +void not8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* ) +{ + CALL_HAL(not8u, cv_hal_not8u, src1, step1, dst, step, width, height) + CALL_IPP_UN(ippiNot_8u_C1R) + (vBinOp, IF_SIMD(VNot)>(src1, step1, src2, step2, dst, step, width, height)); +} + +//======================================= + +#if ARITHM_USE_IPP +inline static IppCmpOp convert_cmp(int _cmpop) +{ + return _cmpop == CMP_EQ ? ippCmpEq : + _cmpop == CMP_GT ? ippCmpGreater : + _cmpop == CMP_GE ? ippCmpGreaterEq : + _cmpop == CMP_LT ? ippCmpLess : + _cmpop == CMP_LE ? ippCmpLessEq : + (IppCmpOp)-1; +} +#define CALL_IPP_CMP(fun) \ + CV_IPP_CHECK() \ + { \ + IppCmpOp op = convert_cmp(*(int *)_cmpop); \ + if( op >= 0 ) \ + { \ + fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ + if (0 <= fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), op)) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } \ + } +#else +#define CALL_IPP_CMP(fun) +#endif + +//======================================= +// Compare +//======================================= + +void cmp8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp8u, cv_hal_cmp8u, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + CALL_IPP_CMP(ippiCompare_8u_C1R) + //vz optimized cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); + int code = *(int*)_cmpop; + step1 /= sizeof(src1[0]); + step2 /= sizeof(src2[0]); + if( code == CMP_GE || code == CMP_LT ) + { + std::swap(src1, src2); + std::swap(step1, step2); + code = code == CMP_GE ? CMP_LE : CMP_GT; + } + + if( code == CMP_GT || code == CMP_LE ) + { + int m = code == CMP_GT ? 0 : 255; + for( ; height--; src1 += step1, src2 += step2, dst += step ) + { + int x =0; + #if CV_SSE2 + if( USE_SSE2 ) + { + __m128i m128 = code == CMP_GT ? _mm_setzero_si128() : _mm_set1_epi8 (-1); + __m128i c128 = _mm_set1_epi8 (-128); + for( ; x <= width - 16; x += 16 ) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + // no simd for 8u comparison, that's why we need the trick + r00 = _mm_sub_epi8(r00,c128); + r10 = _mm_sub_epi8(r10,c128); + + r00 =_mm_xor_si128(_mm_cmpgt_epi8(r00, r10), m128); + _mm_storeu_si128((__m128i*)(dst + x),r00); + + } + } + #elif CV_NEON + uint8x16_t mask = code == CMP_GT ? vdupq_n_u8(0) : vdupq_n_u8(255); + + for( ; x <= width - 16; x += 16 ) + { + vst1q_u8(dst+x, veorq_u8(vcgtq_u8(vld1q_u8(src1+x), vld1q_u8(src2+x)), mask)); + } + + #endif + + for( ; x < width; x++ ){ + dst[x] = (uchar)(-(src1[x] > src2[x]) ^ m); + } + } + } + else if( code == CMP_EQ || code == CMP_NE ) + { + int m = code == CMP_EQ ? 0 : 255; + for( ; height--; src1 += step1, src2 += step2, dst += step ) + { + int x = 0; + #if CV_SSE2 + if( USE_SSE2 ) + { + __m128i m128 = code == CMP_EQ ? _mm_setzero_si128() : _mm_set1_epi8 (-1); + for( ; x <= width - 16; x += 16 ) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + r00 = _mm_xor_si128 ( _mm_cmpeq_epi8 (r00, r10), m128); + _mm_storeu_si128((__m128i*)(dst + x), r00); + } + } + #elif CV_NEON + uint8x16_t mask = code == CMP_EQ ? vdupq_n_u8(0) : vdupq_n_u8(255); + + for( ; x <= width - 16; x += 16 ) + { + vst1q_u8(dst+x, veorq_u8(vceqq_u8(vld1q_u8(src1+x), vld1q_u8(src2+x)), mask)); + } + #endif + for( ; x < width; x++ ) + dst[x] = (uchar)(-(src1[x] == src2[x]) ^ m); + } + } +} + +void cmp8s(const schar* src1, size_t step1, const schar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp8s, cv_hal_cmp8s, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); +} + +void cmp16u(const ushort* src1, size_t step1, const ushort* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp16u, cv_hal_cmp16u, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + CALL_IPP_CMP(ippiCompare_16u_C1R) + cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); +} + +void cmp16s(const short* src1, size_t step1, const short* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp16s, cv_hal_cmp16s, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + CALL_IPP_CMP(ippiCompare_16s_C1R) + //vz optimized cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); + + int code = *(int*)_cmpop; + step1 /= sizeof(src1[0]); + step2 /= sizeof(src2[0]); + if( code == CMP_GE || code == CMP_LT ) + { + std::swap(src1, src2); + std::swap(step1, step2); + code = code == CMP_GE ? CMP_LE : CMP_GT; + } + + if( code == CMP_GT || code == CMP_LE ) + { + int m = code == CMP_GT ? 0 : 255; + for( ; height--; src1 += step1, src2 += step2, dst += step ) + { + int x =0; + #if CV_SSE2 + if( USE_SSE2) + { + __m128i m128 = code == CMP_GT ? _mm_setzero_si128() : _mm_set1_epi16 (-1); + for( ; x <= width - 16; x += 16 ) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + r00 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r00, r10), m128); + __m128i r01 = _mm_loadu_si128((const __m128i*)(src1 + x + 8)); + __m128i r11 = _mm_loadu_si128((const __m128i*)(src2 + x + 8)); + r01 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r01, r11), m128); + r11 = _mm_packs_epi16(r00, r01); + _mm_storeu_si128((__m128i*)(dst + x), r11); + } + if( x <= width-8) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + r00 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r00, r10), m128); + r10 = _mm_packs_epi16(r00, r00); + _mm_storel_epi64((__m128i*)(dst + x), r10); + + x += 8; + } + } + #elif CV_NEON + uint8x16_t mask = code == CMP_GT ? vdupq_n_u8(0) : vdupq_n_u8(255); + + for( ; x <= width - 16; x += 16 ) + { + int16x8_t in1 = vld1q_s16(src1 + x); + int16x8_t in2 = vld1q_s16(src2 + x); + uint8x8_t t1 = vmovn_u16(vcgtq_s16(in1, in2)); + + in1 = vld1q_s16(src1 + x + 8); + in2 = vld1q_s16(src2 + x + 8); + uint8x8_t t2 = vmovn_u16(vcgtq_s16(in1, in2)); + + vst1q_u8(dst+x, veorq_u8(vcombine_u8(t1, t2), mask)); + } + #endif + + for( ; x < width; x++ ){ + dst[x] = (uchar)(-(src1[x] > src2[x]) ^ m); + } + } + } + else if( code == CMP_EQ || code == CMP_NE ) + { + int m = code == CMP_EQ ? 0 : 255; + for( ; height--; src1 += step1, src2 += step2, dst += step ) + { + int x = 0; + #if CV_SSE2 + if( USE_SSE2 ) + { + __m128i m128 = code == CMP_EQ ? _mm_setzero_si128() : _mm_set1_epi16 (-1); + for( ; x <= width - 16; x += 16 ) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + r00 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r00, r10), m128); + __m128i r01 = _mm_loadu_si128((const __m128i*)(src1 + x + 8)); + __m128i r11 = _mm_loadu_si128((const __m128i*)(src2 + x + 8)); + r01 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r01, r11), m128); + r11 = _mm_packs_epi16(r00, r01); + _mm_storeu_si128((__m128i*)(dst + x), r11); + } + if( x <= width - 8) + { + __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); + __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); + r00 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r00, r10), m128); + r10 = _mm_packs_epi16(r00, r00); + _mm_storel_epi64((__m128i*)(dst + x), r10); + + x += 8; + } + } + #elif CV_NEON + uint8x16_t mask = code == CMP_EQ ? vdupq_n_u8(0) : vdupq_n_u8(255); + + for( ; x <= width - 16; x += 16 ) + { + int16x8_t in1 = vld1q_s16(src1 + x); + int16x8_t in2 = vld1q_s16(src2 + x); + uint8x8_t t1 = vmovn_u16(vceqq_s16(in1, in2)); + + in1 = vld1q_s16(src1 + x + 8); + in2 = vld1q_s16(src2 + x + 8); + uint8x8_t t2 = vmovn_u16(vceqq_s16(in1, in2)); + + vst1q_u8(dst+x, veorq_u8(vcombine_u8(t1, t2), mask)); + } + #endif + for( ; x < width; x++ ) + dst[x] = (uchar)(-(src1[x] == src2[x]) ^ m); + } + } +} + +void cmp32s(const int* src1, size_t step1, const int* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp32s, cv_hal_cmp32s, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); +} + +void cmp32f(const float* src1, size_t step1, const float* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp32f, cv_hal_cmp32f, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + CALL_IPP_CMP(ippiCompare_32f_C1R) + cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); +} + +void cmp64f(const double* src1, size_t step1, const double* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* _cmpop) +{ + CALL_HAL(cmp64f, cv_hal_cmp64f, src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop) + cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); +} + +//======================================= + +#if defined HAVE_IPP +#define CALL_IPP_MUL(fun) \ + CV_IPP_CHECK() \ + { \ + if (std::fabs(fscale - 1) <= FLT_EPSILON) \ + { \ + if (fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), 0) >= 0) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } \ + } + +#define CALL_IPP_MUL_2(fun) \ + CV_IPP_CHECK() \ + { \ + if (std::fabs(fscale - 1) <= FLT_EPSILON) \ + { \ + if (fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height)) >= 0) \ + { \ + CV_IMPL_ADD(CV_IMPL_IPP); \ + return; \ + } \ + setIppErrorStatus(); \ + } \ + } + +#else +#define CALL_IPP_MUL(fun) +#define CALL_IPP_MUL_2(fun) +#endif + +//======================================= +// Multilpy +//======================================= + +void mul8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul8u, cv_hal_mul8u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + float fscale = (float)*(const double*)scale; + CALL_IPP_MUL(ippiMul_8u_C1RSfs) + mul_(src1, step1, src2, step2, dst, step, width, height, fscale); +} + +void mul8s( const schar* src1, size_t step1, const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul8s, cv_hal_mul8s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + mul_(src1, step1, src2, step2, dst, step, width, height, (float)*(const double*)scale); +} + +void mul16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul16u, cv_hal_mul16u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + float fscale = (float)*(const double*)scale; + CALL_IPP_MUL(ippiMul_16u_C1RSfs) + mul_(src1, step1, src2, step2, dst, step, width, height, fscale); +} + +void mul16s( const short* src1, size_t step1, const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul16s, cv_hal_mul16s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + float fscale = (float)*(const double*)scale; + CALL_IPP_MUL(ippiMul_16s_C1RSfs) + mul_(src1, step1, src2, step2, dst, step, width, height, fscale); +} + +void mul32s( const int* src1, size_t step1, const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul32s, cv_hal_mul32s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + mul_(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void mul32f( const float* src1, size_t step1, const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul32f, cv_hal_mul32f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + float fscale = (float)*(const double*)scale; + CALL_IPP_MUL_2(ippiMul_32f_C1R) + mul_(src1, step1, src2, step2, dst, step, width, height, fscale); +} + +void mul64f( const double* src1, size_t step1, const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(mul64f, cv_hal_mul64f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + mul_(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +//======================================= +// Divide +//======================================= + +void div8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div8u, cv_hal_div8u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + if( src1 ) + div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); + else + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div8s( const schar* src1, size_t step1, const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div8s, cv_hal_div8s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div16u, cv_hal_div16u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div16s( const short* src1, size_t step1, const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div16s, cv_hal_div16s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div32s( const int* src1, size_t step1, const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div32s, cv_hal_div32s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div32f( const float* src1, size_t step1, const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div32f, cv_hal_div32f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void div64f( const double* src1, size_t step1, const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(div64f, cv_hal_div64f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + div_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +//======================================= +// Reciprocial +//======================================= + +void recip8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip8u, cv_hal_recip8u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip8s( const schar* src1, size_t step1, const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip8s, cv_hal_recip8s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip16u, cv_hal_recip16u, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip16s( const short* src1, size_t step1, const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip16s, cv_hal_recip16s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip32s( const int* src1, size_t step1, const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip32s, cv_hal_recip32s, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip32f( const float* src1, size_t step1, const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip32f, cv_hal_recip32f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +void recip64f( const double* src1, size_t step1, const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* scale) +{ + CALL_HAL(recip64f, cv_hal_recip64f, src1, step1, src2, step2, dst, step, width, height, *(const double*)scale) + recip_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); +} + +//======================================= +// Add weighted +//======================================= + +void +addWeighted8u( const uchar* src1, size_t step1, + const uchar* src2, size_t step2, + uchar* dst, size_t step, int width, int height, + void* scalars ) +{ + CALL_HAL(addWeighted8u, cv_hal_addWeighted8u, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + const double* scalars_ = (const double*)scalars; + float alpha = (float)scalars_[0], beta = (float)scalars_[1], gamma = (float)scalars_[2]; + + for( ; height--; src1 += step1, src2 += step2, dst += step ) + { + int x = 0; + +#if CV_SSE2 + if( USE_SSE2 ) + { + __m128 a4 = _mm_set1_ps(alpha), b4 = _mm_set1_ps(beta), g4 = _mm_set1_ps(gamma); + __m128i z = _mm_setzero_si128(); + + for( ; x <= width - 8; x += 8 ) + { + __m128i u = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(src1 + x)), z); + __m128i v = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(src2 + x)), z); + + __m128 u0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(u, z)); + __m128 u1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(u, z)); + __m128 v0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v, z)); + __m128 v1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v, z)); + + u0 = _mm_add_ps(_mm_mul_ps(u0, a4), _mm_mul_ps(v0, b4)); + u1 = _mm_add_ps(_mm_mul_ps(u1, a4), _mm_mul_ps(v1, b4)); + u0 = _mm_add_ps(u0, g4); u1 = _mm_add_ps(u1, g4); + + u = _mm_packs_epi32(_mm_cvtps_epi32(u0), _mm_cvtps_epi32(u1)); + u = _mm_packus_epi16(u, u); + + _mm_storel_epi64((__m128i*)(dst + x), u); + } + } +#elif CV_NEON + float32x4_t g = vdupq_n_f32 (gamma); + + for( ; x <= width - 8; x += 8 ) + { + uint8x8_t in1 = vld1_u8(src1+x); + uint16x8_t in1_16 = vmovl_u8(in1); + float32x4_t in1_f_l = vcvtq_f32_u32(vmovl_u16(vget_low_u16(in1_16))); + float32x4_t in1_f_h = vcvtq_f32_u32(vmovl_u16(vget_high_u16(in1_16))); + + uint8x8_t in2 = vld1_u8(src2+x); + uint16x8_t in2_16 = vmovl_u8(in2); + float32x4_t in2_f_l = vcvtq_f32_u32(vmovl_u16(vget_low_u16(in2_16))); + float32x4_t in2_f_h = vcvtq_f32_u32(vmovl_u16(vget_high_u16(in2_16))); + + float32x4_t out_f_l = vaddq_f32(vmulq_n_f32(in1_f_l, alpha), vmulq_n_f32(in2_f_l, beta)); + float32x4_t out_f_h = vaddq_f32(vmulq_n_f32(in1_f_h, alpha), vmulq_n_f32(in2_f_h, beta)); + out_f_l = vaddq_f32(out_f_l, g); + out_f_h = vaddq_f32(out_f_h, g); + + uint16x4_t out_16_l = vqmovun_s32(cv_vrndq_s32_f32(out_f_l)); + uint16x4_t out_16_h = vqmovun_s32(cv_vrndq_s32_f32(out_f_h)); + + uint16x8_t out_16 = vcombine_u16(out_16_l, out_16_h); + uint8x8_t out = vqmovn_u16(out_16); + + vst1_u8(dst+x, out); + } +#endif + #if CV_ENABLE_UNROLLED + for( ; x <= width - 4; x += 4 ) + { + float t0, t1; + t0 = CV_8TO32F(src1[x])*alpha + CV_8TO32F(src2[x])*beta + gamma; + t1 = CV_8TO32F(src1[x+1])*alpha + CV_8TO32F(src2[x+1])*beta + gamma; + + dst[x] = saturate_cast(t0); + dst[x+1] = saturate_cast(t1); + + t0 = CV_8TO32F(src1[x+2])*alpha + CV_8TO32F(src2[x+2])*beta + gamma; + t1 = CV_8TO32F(src1[x+3])*alpha + CV_8TO32F(src2[x+3])*beta + gamma; + + dst[x+2] = saturate_cast(t0); + dst[x+3] = saturate_cast(t1); + } + #endif + + for( ; x < width; x++ ) + { + float t0 = CV_8TO32F(src1[x])*alpha + CV_8TO32F(src2[x])*beta + gamma; + dst[x] = saturate_cast(t0); + } + } +} + +void addWeighted8s( const schar* src1, size_t step1, const schar* src2, size_t step2, + schar* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted8s, cv_hal_addWeighted8s, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +void addWeighted16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, + ushort* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted16u, cv_hal_addWeighted16u, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +void addWeighted16s( const short* src1, size_t step1, const short* src2, size_t step2, + short* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted16s, cv_hal_addWeighted16s, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +void addWeighted32s( const int* src1, size_t step1, const int* src2, size_t step2, + int* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted32s, cv_hal_addWeighted32s, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +void addWeighted32f( const float* src1, size_t step1, const float* src2, size_t step2, + float* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted32f, cv_hal_addWeighted32f, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +void addWeighted64f( const double* src1, size_t step1, const double* src2, size_t step2, + double* dst, size_t step, int width, int height, void* scalars ) +{ + CALL_HAL(addWeighted64f, cv_hal_addWeighted64f, src1, step1, src2, step2, dst, step, width, height, (const double*)scalars) + addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); +} + +}} // cv::hal:: + /* End of file. */ diff --git a/modules/hal/src/arithm_core.hpp b/modules/core/src/arithm_core.hpp similarity index 67% rename from modules/hal/src/arithm_core.hpp rename to modules/core/src/arithm_core.hpp index a65e74c38..4790586eb 100644 --- a/modules/hal/src/arithm_core.hpp +++ b/modules/core/src/arithm_core.hpp @@ -42,144 +42,94 @@ // //M*/ -#ifndef __OPENCV_HAL_ARITHM_CORE_HPP__ -#define __OPENCV_HAL_ARITHM_CORE_HPP__ +#ifndef __OPENCV_ARITHM_CORE_HPP__ +#define __OPENCV_ARITHM_CORE_HPP__ #include "arithm_simd.hpp" -const uchar g_Saturate8u[] = -{ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, - 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, - 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, - 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, - 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, - 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, - 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, - 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, - 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, - 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, - 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, - 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255 -}; - - -#define CV_FAST_CAST_8U(t) (assert(-256 <= (t) && (t) <= 512), g_Saturate8u[(t)+256]) -#define CV_MIN_8U(a,b) ((a) - CV_FAST_CAST_8U((a) - (b))) -#define CV_MAX_8U(a,b) ((a) + CV_FAST_CAST_8U((b) - (a))) - -const float g_8x32fTab[] = -{ - -128.f, -127.f, -126.f, -125.f, -124.f, -123.f, -122.f, -121.f, - -120.f, -119.f, -118.f, -117.f, -116.f, -115.f, -114.f, -113.f, - -112.f, -111.f, -110.f, -109.f, -108.f, -107.f, -106.f, -105.f, - -104.f, -103.f, -102.f, -101.f, -100.f, -99.f, -98.f, -97.f, - -96.f, -95.f, -94.f, -93.f, -92.f, -91.f, -90.f, -89.f, - -88.f, -87.f, -86.f, -85.f, -84.f, -83.f, -82.f, -81.f, - -80.f, -79.f, -78.f, -77.f, -76.f, -75.f, -74.f, -73.f, - -72.f, -71.f, -70.f, -69.f, -68.f, -67.f, -66.f, -65.f, - -64.f, -63.f, -62.f, -61.f, -60.f, -59.f, -58.f, -57.f, - -56.f, -55.f, -54.f, -53.f, -52.f, -51.f, -50.f, -49.f, - -48.f, -47.f, -46.f, -45.f, -44.f, -43.f, -42.f, -41.f, - -40.f, -39.f, -38.f, -37.f, -36.f, -35.f, -34.f, -33.f, - -32.f, -31.f, -30.f, -29.f, -28.f, -27.f, -26.f, -25.f, - -24.f, -23.f, -22.f, -21.f, -20.f, -19.f, -18.f, -17.f, - -16.f, -15.f, -14.f, -13.f, -12.f, -11.f, -10.f, -9.f, - -8.f, -7.f, -6.f, -5.f, -4.f, -3.f, -2.f, -1.f, - 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, - 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f, - 16.f, 17.f, 18.f, 19.f, 20.f, 21.f, 22.f, 23.f, - 24.f, 25.f, 26.f, 27.f, 28.f, 29.f, 30.f, 31.f, - 32.f, 33.f, 34.f, 35.f, 36.f, 37.f, 38.f, 39.f, - 40.f, 41.f, 42.f, 43.f, 44.f, 45.f, 46.f, 47.f, - 48.f, 49.f, 50.f, 51.f, 52.f, 53.f, 54.f, 55.f, - 56.f, 57.f, 58.f, 59.f, 60.f, 61.f, 62.f, 63.f, - 64.f, 65.f, 66.f, 67.f, 68.f, 69.f, 70.f, 71.f, - 72.f, 73.f, 74.f, 75.f, 76.f, 77.f, 78.f, 79.f, - 80.f, 81.f, 82.f, 83.f, 84.f, 85.f, 86.f, 87.f, - 88.f, 89.f, 90.f, 91.f, 92.f, 93.f, 94.f, 95.f, - 96.f, 97.f, 98.f, 99.f, 100.f, 101.f, 102.f, 103.f, - 104.f, 105.f, 106.f, 107.f, 108.f, 109.f, 110.f, 111.f, - 112.f, 113.f, 114.f, 115.f, 116.f, 117.f, 118.f, 119.f, - 120.f, 121.f, 122.f, 123.f, 124.f, 125.f, 126.f, 127.f, - 128.f, 129.f, 130.f, 131.f, 132.f, 133.f, 134.f, 135.f, - 136.f, 137.f, 138.f, 139.f, 140.f, 141.f, 142.f, 143.f, - 144.f, 145.f, 146.f, 147.f, 148.f, 149.f, 150.f, 151.f, - 152.f, 153.f, 154.f, 155.f, 156.f, 157.f, 158.f, 159.f, - 160.f, 161.f, 162.f, 163.f, 164.f, 165.f, 166.f, 167.f, - 168.f, 169.f, 170.f, 171.f, 172.f, 173.f, 174.f, 175.f, - 176.f, 177.f, 178.f, 179.f, 180.f, 181.f, 182.f, 183.f, - 184.f, 185.f, 186.f, 187.f, 188.f, 189.f, 190.f, 191.f, - 192.f, 193.f, 194.f, 195.f, 196.f, 197.f, 198.f, 199.f, - 200.f, 201.f, 202.f, 203.f, 204.f, 205.f, 206.f, 207.f, - 208.f, 209.f, 210.f, 211.f, 212.f, 213.f, 214.f, 215.f, - 216.f, 217.f, 218.f, 219.f, 220.f, 221.f, 222.f, 223.f, - 224.f, 225.f, 226.f, 227.f, 228.f, 229.f, 230.f, 231.f, - 232.f, 233.f, 234.f, 235.f, 236.f, 237.f, 238.f, 239.f, - 240.f, 241.f, 242.f, 243.f, 244.f, 245.f, 246.f, 247.f, - 248.f, 249.f, 250.f, 251.f, 252.f, 253.f, 254.f, 255.f -}; - -#define CV_8TO32F(x) g_8x32fTab[(x)+128] - namespace cv { -template<> inline uchar OpAdd::operator ()(uchar a, uchar b) const -{ return CV_FAST_CAST_8U(a + b); } +template struct OpAdd +{ + typedef T1 type1; + typedef T2 type2; + typedef T3 rtype; + T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(a + b); } +}; -template<> inline uchar OpSub::operator ()(uchar a, uchar b) const -{ return CV_FAST_CAST_8U(a - b); } +template struct OpSub +{ + typedef T1 type1; + typedef T2 type2; + typedef T3 rtype; + T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(a - b); } +}; -template<> inline short OpAbsDiff::operator ()(short a, short b) const -{ return saturate_cast(std::abs(a - b)); } +template struct OpRSub +{ + typedef T1 type1; + typedef T2 type2; + typedef T3 rtype; + T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(b - a); } +}; -template<> inline schar OpAbsDiff::operator ()(schar a, schar b) const -{ return saturate_cast(std::abs(a - b)); } +template struct OpMin +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator ()(const T a, const T b) const { return std::min(a, b); } +}; -template<> inline uchar OpMin::operator ()(uchar a, uchar b) const { return CV_MIN_8U(a, b); } +template struct OpMax +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator ()(const T a, const T b) const { return std::max(a, b); } +}; -template<> inline uchar OpMax::operator ()(uchar a, uchar b) const { return CV_MAX_8U(a, b); } +template struct OpAbsDiff +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator()(T a, T b) const { return a > b ? a - b : b - a; } +}; -} +template struct OpAnd +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator()( T a, T b ) const { return a & b; } +}; -namespace cv { namespace hal { +template struct OpOr +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator()( T a, T b ) const { return a | b; } +}; + +template struct OpXor +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator()( T a, T b ) const { return a ^ b; } +}; + +template struct OpNot +{ + typedef T type1; + typedef T type2; + typedef T rtype; + T operator()( T a, T ) const { return ~a; } +}; + +//============================================================================= template void vBinOp(const T* src1, size_t step1, const T* src2, size_t step2, T* dst, size_t step, int width, int height) @@ -651,7 +601,7 @@ addWeighted_( const T* src1, size_t step1, const T* src2, size_t step2, } } -}} // cv::hal:: +} // cv:: -#endif // __OPENCV_HAL_ARITHM_CORE_HPP__ +#endif // __OPENCV_ARITHM_CORE_HPP__ diff --git a/modules/hal/src/arithm_simd.hpp b/modules/core/src/arithm_simd.hpp similarity index 99% rename from modules/hal/src/arithm_simd.hpp rename to modules/core/src/arithm_simd.hpp index 4e4029875..b6a549ed9 100644 --- a/modules/hal/src/arithm_simd.hpp +++ b/modules/core/src/arithm_simd.hpp @@ -42,10 +42,10 @@ // //M*/ -#ifndef __OPENCV_HAL_ARITHM_SIMD_HPP__ -#define __OPENCV_HAL_ARITHM_SIMD_HPP__ +#ifndef __OPENCV_ARITHM_SIMD_HPP__ +#define __OPENCV_ARITHM_SIMD_HPP__ -namespace cv { namespace hal { +namespace cv { struct NOP {}; @@ -2020,6 +2020,6 @@ struct AddWeighted_SIMD #endif -}} +} -#endif // __OPENCV_HAL_ARITHM_SIMD_HPP__ +#endif // __OPENCV_ARITHM_SIMD_HPP__ diff --git a/modules/core/src/convert.cpp b/modules/core/src/convert.cpp index 6c693a43a..f41bfa105 100644 --- a/modules/core/src/convert.cpp +++ b/modules/core/src/convert.cpp @@ -5352,22 +5352,21 @@ void cv::normalize( InputArray _src, InputOutputArray _dst, double a, double b, else CV_Error( CV_StsBadArg, "Unknown/unsupported norm type" ); - int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); + int type = _src.type(), depth = CV_MAT_DEPTH(type); if( rtype < 0 ) rtype = _dst.fixedType() ? _dst.depth() : depth; - _dst.createSameSize(_src, CV_MAKETYPE(rtype, cn)); CV_OCL_RUN(_dst.isUMat(), ocl_normalize(_src, _dst, _mask, rtype, scale, shift)) - Mat src = _src.getMat(), dst = _dst.getMat(); + Mat src = _src.getMat(); if( _mask.empty() ) - src.convertTo( dst, rtype, scale, shift ); + src.convertTo( _dst, rtype, scale, shift ); else { Mat temp; src.convertTo( temp, rtype, scale, shift ); - temp.copyTo( dst, _mask ); + temp.copyTo( _dst, _mask ); } } diff --git a/modules/core/src/cuda_gpu_mat.cpp b/modules/core/src/cuda_gpu_mat.cpp index 9a17ddd85..0d1e3ba33 100644 --- a/modules/core/src/cuda_gpu_mat.cpp +++ b/modules/core/src/cuda_gpu_mat.cpp @@ -260,7 +260,7 @@ namespace { const int area = rows * cols; - if (obj.empty() || obj.type() != type || !obj.isContinuous() || obj.size().area() < area) + if (obj.empty() || obj.type() != type || !obj.isContinuous() || obj.size().area() != area) obj.create(1, area, type); obj = obj.reshape(obj.channels(), rows); diff --git a/modules/core/src/cuda_stream.cpp b/modules/core/src/cuda_stream.cpp index d3b5545e9..1ea8df37b 100644 --- a/modules/core/src/cuda_stream.cpp +++ b/modules/core/src/cuda_stream.cpp @@ -280,32 +280,37 @@ class cv::cuda::Stream::Impl { public: cudaStream_t stream; - Ptr stackAllocator_; + bool ownStream; + + Ptr stackAllocator; Impl(); - Impl(cudaStream_t stream); + explicit Impl(cudaStream_t stream); ~Impl(); }; -cv::cuda::Stream::Impl::Impl() : stream(0) +cv::cuda::Stream::Impl::Impl() : stream(0), ownStream(false) { cudaSafeCall( cudaStreamCreate(&stream) ); + ownStream = true; - stackAllocator_ = makePtr(stream); + stackAllocator = makePtr(stream); } -cv::cuda::Stream::Impl::Impl(cudaStream_t stream_) : stream(stream_) +cv::cuda::Stream::Impl::Impl(cudaStream_t stream_) : stream(stream_), ownStream(false) { - stackAllocator_ = makePtr(stream); + stackAllocator = makePtr(stream); } cv::cuda::Stream::Impl::~Impl() { - stackAllocator_.release(); + stackAllocator.release(); - if (stream) + if (stream && ownStream) + { cudaStreamDestroy(stream); + } } #endif @@ -516,6 +521,11 @@ cudaStream_t cv::cuda::StreamAccessor::getStream(const Stream& stream) return stream.impl_->stream; } +Stream cv::cuda::StreamAccessor::wrapStream(cudaStream_t stream) +{ + return Stream(makePtr(stream)); +} + #endif ///////////////////////////////////////////////////////////// @@ -660,7 +670,7 @@ void cv::cuda::setBufferPoolConfig(int deviceId, size_t stackSize, int stackCoun #ifdef HAVE_CUDA -cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->stackAllocator_.get()) +cv::cuda::BufferPool::BufferPool(Stream& stream) : allocator_(stream.impl_->stackAllocator.get()) { } @@ -693,20 +703,29 @@ class cv::cuda::Event::Impl { public: cudaEvent_t event; + bool ownEvent; - Impl(unsigned int flags); + explicit Impl(unsigned int flags); + explicit Impl(cudaEvent_t event); ~Impl(); }; -cv::cuda::Event::Impl::Impl(unsigned int flags) : event(0) +cv::cuda::Event::Impl::Impl(unsigned int flags) : event(0), ownEvent(false) { cudaSafeCall( cudaEventCreateWithFlags(&event, flags) ); + ownEvent = true; +} + +cv::cuda::Event::Impl::Impl(cudaEvent_t e) : event(e), ownEvent(false) +{ } cv::cuda::Event::Impl::~Impl() { - if (event) + if (event && ownEvent) + { cudaEventDestroy(event); + } } cudaEvent_t cv::cuda::EventAccessor::getEvent(const Event& event) @@ -714,6 +733,11 @@ cudaEvent_t cv::cuda::EventAccessor::getEvent(const Event& event) return event.impl_->event; } +Event cv::cuda::EventAccessor::wrapEvent(cudaEvent_t event) +{ + return Event(makePtr(event)); +} + #endif cv::cuda::Event::Event(CreateFlags flags) diff --git a/modules/core/src/dxt.cpp b/modules/core/src/dxt.cpp index 5576a347a..691b29746 100644 --- a/modules/core/src/dxt.cpp +++ b/modules/core/src/dxt.cpp @@ -2215,7 +2215,7 @@ class PlanCache clAmdFftDim dim = dft_size.height == 1 || dft_rows ? CLFFT_1D : CLFFT_2D; size_t batchSize = dft_rows ? dft_size.height : 1; - size_t clLengthsIn[3] = { dft_size.width, dft_rows ? 1 : dft_size.height, 1 }; + size_t clLengthsIn[3] = { (size_t)dft_size.width, dft_rows ? 1 : (size_t)dft_size.height, 1 }; size_t clStridesIn[3] = { 1, 1, 1 }; size_t clStridesOut[3] = { 1, 1, 1 }; int elemSize = doubleFP ? sizeof(double) : sizeof(float); diff --git a/modules/core/src/hal_replacement.hpp b/modules/core/src/hal_replacement.hpp new file mode 100644 index 000000000..65866f8bf --- /dev/null +++ b/modules/core/src/hal_replacement.hpp @@ -0,0 +1,228 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Copyright (C) 2013, OpenCV Foundation, all rights reserved. +// Copyright (C) 2015, Itseez Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_CORE_HAL_REPLACEMENT_HPP__ +#define __OPENCV_CORE_HAL_REPLACEMENT_HPP__ + +#include "opencv2/core/hal/interface.h" + +inline int hal_ni_add8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_add64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_sub64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_max64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_min64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_absdiff64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_and8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_or8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_xor8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_not8u(const uchar*, size_t, uchar*, size_t, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_add8u hal_ni_add8u +#define cv_hal_add8s hal_ni_add8s +#define cv_hal_add16u hal_ni_add16u +#define cv_hal_add16s hal_ni_add16s +#define cv_hal_add32s hal_ni_add32s +#define cv_hal_add32f hal_ni_add32f +#define cv_hal_add64f hal_ni_add64f +#define cv_hal_sub8u hal_ni_sub8u +#define cv_hal_sub8s hal_ni_sub8s +#define cv_hal_sub16u hal_ni_sub16u +#define cv_hal_sub16s hal_ni_sub16s +#define cv_hal_sub32s hal_ni_sub32s +#define cv_hal_sub32f hal_ni_sub32f +#define cv_hal_sub64f hal_ni_sub64f +#define cv_hal_max8u hal_ni_max8u +#define cv_hal_max8s hal_ni_max8s +#define cv_hal_max16u hal_ni_max16u +#define cv_hal_max16s hal_ni_max16s +#define cv_hal_max32s hal_ni_max32s +#define cv_hal_max32f hal_ni_max32f +#define cv_hal_max64f hal_ni_max64f +#define cv_hal_min8u hal_ni_min8u +#define cv_hal_min8s hal_ni_min8s +#define cv_hal_min16u hal_ni_min16u +#define cv_hal_min16s hal_ni_min16s +#define cv_hal_min32s hal_ni_min32s +#define cv_hal_min32f hal_ni_min32f +#define cv_hal_min64f hal_ni_min64f +#define cv_hal_absdiff8u hal_ni_absdiff8u +#define cv_hal_absdiff8s hal_ni_absdiff8s +#define cv_hal_absdiff16u hal_ni_absdiff16u +#define cv_hal_absdiff16s hal_ni_absdiff16s +#define cv_hal_absdiff32s hal_ni_absdiff32s +#define cv_hal_absdiff32f hal_ni_absdiff32f +#define cv_hal_absdiff64f hal_ni_absdiff64f +#define cv_hal_and8u hal_ni_and8u +#define cv_hal_or8u hal_ni_or8u +#define cv_hal_xor8u hal_ni_xor8u +#define cv_hal_not8u hal_ni_not8u + +inline int hal_ni_cmp8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp8s(const schar*, size_t, const schar*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp16u(const ushort*, size_t, const ushort*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp16s(const short*, size_t, const short*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp32s(const int*, size_t, const int*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp32f(const float*, size_t, const float*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_cmp64f(const double*, size_t, const double*, size_t, uchar*, size_t, int, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_cmp8u hal_ni_cmp8u +#define cv_hal_cmp8s hal_ni_cmp8s +#define cv_hal_cmp16u hal_ni_cmp16u +#define cv_hal_cmp16s hal_ni_cmp16s +#define cv_hal_cmp32s hal_ni_cmp32s +#define cv_hal_cmp32f hal_ni_cmp32f +#define cv_hal_cmp64f hal_ni_cmp64f + +inline int hal_ni_mul8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_mul64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_div64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_recip64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_mul8u hal_ni_mul8u +#define cv_hal_mul8s hal_ni_mul8s +#define cv_hal_mul16u hal_ni_mul16u +#define cv_hal_mul16s hal_ni_mul16s +#define cv_hal_mul32s hal_ni_mul32s +#define cv_hal_mul32f hal_ni_mul32f +#define cv_hal_mul64f hal_ni_mul64f +#define cv_hal_div8u hal_ni_div8u +#define cv_hal_div8s hal_ni_div8s +#define cv_hal_div16u hal_ni_div16u +#define cv_hal_div16s hal_ni_div16s +#define cv_hal_div32s hal_ni_div32s +#define cv_hal_div32f hal_ni_div32f +#define cv_hal_div64f hal_ni_div64f +#define cv_hal_recip8u hal_ni_recip8u +#define cv_hal_recip8s hal_ni_recip8s +#define cv_hal_recip16u hal_ni_recip16u +#define cv_hal_recip16s hal_ni_recip16s +#define cv_hal_recip32s hal_ni_recip32s +#define cv_hal_recip32f hal_ni_recip32f +#define cv_hal_recip64f hal_ni_recip64f + +inline int hal_ni_addWeighted8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_addWeighted64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, const double*) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_addWeighted8u hal_ni_addWeighted8u +#define cv_hal_addWeighted8s hal_ni_addWeighted8s +#define cv_hal_addWeighted16u hal_ni_addWeighted16u +#define cv_hal_addWeighted16s hal_ni_addWeighted16s +#define cv_hal_addWeighted32s hal_ni_addWeighted32s +#define cv_hal_addWeighted32f hal_ni_addWeighted32f +#define cv_hal_addWeighted64f hal_ni_addWeighted64f + +inline int hal_ni_split8u(const uchar*, uchar**, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_split16u(const ushort*, ushort**, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_split32s(const int*, int**, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_split64s(const int64*, int64**, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_split8u hal_ni_split8u +#define cv_hal_split16u hal_ni_split16u +#define cv_hal_split32s hal_ni_split32s +#define cv_hal_split64s hal_ni_split64s + +inline int hal_ni_merge8u(const uchar**, uchar*, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_merge16u(const ushort**, ushort*, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_merge32s(const int**, int*, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } +inline int hal_ni_merge64s(const int64**, int64*, int, int) { return CV_HAL_ERROR_NOT_IMPLEMENTED; } + +#define cv_hal_merge8u hal_ni_merge8u +#define cv_hal_merge16u hal_ni_merge16u +#define cv_hal_merge32s hal_ni_merge32s +#define cv_hal_merge64s hal_ni_merge64s + +#include "custom_hal.hpp" + +#endif diff --git a/modules/core/src/kmeans.cpp b/modules/core/src/kmeans.cpp index fe5a0cf6e..a81334aa8 100644 --- a/modules/core/src/kmeans.cpp +++ b/modules/core/src/kmeans.cpp @@ -221,9 +221,9 @@ double cv::kmeans( InputArray _data, int K, { const int SPP_TRIALS = 3; Mat data0 = _data.getMat(); - bool isrow = data0.rows == 1 && data0.channels() > 1; - int N = !isrow ? data0.rows : data0.cols; - int dims = (!isrow ? data0.cols : 1)*data0.channels(); + bool isrow = data0.rows == 1; + int N = isrow ? data0.cols : data0.rows; + int dims = (isrow ? 1 : data0.cols)*data0.channels(); int type = data0.depth(); attempts = std::max(attempts, 1); diff --git a/modules/core/src/lapack.cpp b/modules/core/src/lapack.cpp index b3e1b5294..4fcf3c792 100644 --- a/modules/core/src/lapack.cpp +++ b/modules/core/src/lapack.cpp @@ -52,22 +52,22 @@ namespace cv int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n) { - return hal::LU(A, astep, m, b, bstep, n); + return hal::LU32f(A, astep, m, b, bstep, n); } int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n) { - return hal::LU(A, astep, m, b, bstep, n); + return hal::LU64f(A, astep, m, b, bstep, n); } bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n) { - return hal::Cholesky(A, astep, m, b, bstep, n); + return hal::Cholesky32f(A, astep, m, b, bstep, n); } bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n) { - return hal::Cholesky(A, astep, m, b, bstep, n); + return hal::Cholesky64f(A, astep, m, b, bstep, n); } template static inline _Tp hypot(_Tp a, _Tp b) @@ -740,7 +740,7 @@ double cv::determinant( InputArray _mat ) Mat a(rows, rows, CV_32F, (uchar*)buffer); mat.copyTo(a); - result = hal::LU(a.ptr(), a.step, rows, 0, 0, 0); + result = hal::LU32f(a.ptr(), a.step, rows, 0, 0, 0); if( result ) { for( int i = 0; i < rows; i++ ) @@ -764,7 +764,7 @@ double cv::determinant( InputArray _mat ) Mat a(rows, rows, CV_64F, (uchar*)buffer); mat.copyTo(a); - result = hal::LU(a.ptr(), a.step, rows, 0, 0, 0); + result = hal::LU64f(a.ptr(), a.step, rows, 0, 0, 0); if( result ) { for( int i = 0; i < rows; i++ ) @@ -1027,13 +1027,13 @@ double cv::invert( InputArray _src, OutputArray _dst, int method ) setIdentity(dst); if( method == DECOMP_LU && type == CV_32F ) - result = hal::LU(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n) != 0; + result = hal::LU32f(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n) != 0; else if( method == DECOMP_LU && type == CV_64F ) - result = hal::LU(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n) != 0; + result = hal::LU64f(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n) != 0; else if( method == DECOMP_CHOLESKY && type == CV_32F ) - result = hal::Cholesky(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n); + result = hal::Cholesky32f(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n); else - result = hal::Cholesky(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n); + result = hal::Cholesky64f(src1.ptr(), src1.step, n, dst.ptr(), dst.step, n); if( !result ) dst = Scalar(0); @@ -1265,16 +1265,16 @@ bool cv::solve( InputArray _src, InputArray _src2arg, OutputArray _dst, int meth if( method == DECOMP_LU ) { if( type == CV_32F ) - result = hal::LU(a.ptr(), a.step, n, dst.ptr(), dst.step, nb) != 0; + result = hal::LU32f(a.ptr(), a.step, n, dst.ptr(), dst.step, nb) != 0; else - result = hal::LU(a.ptr(), a.step, n, dst.ptr(), dst.step, nb) != 0; + result = hal::LU64f(a.ptr(), a.step, n, dst.ptr(), dst.step, nb) != 0; } else if( method == DECOMP_CHOLESKY ) { if( type == CV_32F ) - result = hal::Cholesky(a.ptr(), a.step, n, dst.ptr(), dst.step, nb); + result = hal::Cholesky32f(a.ptr(), a.step, n, dst.ptr(), dst.step, nb); else - result = hal::Cholesky(a.ptr(), a.step, n, dst.ptr(), dst.step, nb); + result = hal::Cholesky64f(a.ptr(), a.step, n, dst.ptr(), dst.step, nb); } else { diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 54c4e6d49..495711f8d 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -191,13 +191,13 @@ void magnitude( InputArray src1, InputArray src2, OutputArray dst ) { const float *x = (const float*)ptrs[0], *y = (const float*)ptrs[1]; float *mag = (float*)ptrs[2]; - hal::magnitude( x, y, mag, len ); + hal::magnitude32f( x, y, mag, len ); } else { const double *x = (const double*)ptrs[0], *y = (const double*)ptrs[1]; double *mag = (double*)ptrs[2]; - hal::magnitude( x, y, mag, len ); + hal::magnitude64f( x, y, mag, len ); } } } @@ -374,7 +374,7 @@ void cartToPolar( InputArray src1, InputArray src2, { const float *x = (const float*)ptrs[0], *y = (const float*)ptrs[1]; float *mag = (float*)ptrs[2], *angle = (float*)ptrs[3]; - hal::magnitude( x, y, mag, len ); + hal::magnitude32f( x, y, mag, len ); hal::fastAtan2( y, x, angle, len, angleInDegrees ); } else @@ -382,7 +382,7 @@ void cartToPolar( InputArray src1, InputArray src2, const double *x = (const double*)ptrs[0], *y = (const double*)ptrs[1]; double *angle = (double*)ptrs[3]; - hal::magnitude(x, y, (double*)ptrs[2], len); + hal::magnitude64f(x, y, (double*)ptrs[2], len); k = 0; #if CV_SSE2 @@ -760,7 +760,7 @@ static void Exp_32f_ipp(const float *x, float *y, int n) } setIppErrorStatus(); } - hal::exp(x, y, n); + hal::exp32f(x, y, n); } static void Exp_64f_ipp(const double *x, double *y, int n) @@ -774,14 +774,14 @@ static void Exp_64f_ipp(const double *x, double *y, int n) } setIppErrorStatus(); } - hal::exp(x, y, n); + hal::exp64f(x, y, n); } #define Exp_32f Exp_32f_ipp #define Exp_64f Exp_64f_ipp #else -#define Exp_32f hal::exp -#define Exp_64f hal::exp +#define Exp_32f hal::exp32f +#define Exp_64f hal::exp64f #endif @@ -828,7 +828,7 @@ static void Log_32f_ipp(const float *x, float *y, int n) } setIppErrorStatus(); } - hal::log(x, y, n); + hal::log32f(x, y, n); } static void Log_64f_ipp(const double *x, double *y, int n) @@ -842,14 +842,14 @@ static void Log_64f_ipp(const double *x, double *y, int n) } setIppErrorStatus(); } - hal::log(x, y, n); + hal::log64f(x, y, n); } #define Log_32f Log_32f_ipp #define Log_64f Log_64f_ipp #else -#define Log_32f hal::log -#define Log_64f hal::log +#define Log_32f hal::log32f +#define Log_64f hal::log64f #endif void log( InputArray _src, OutputArray _dst ) @@ -1356,10 +1356,10 @@ static bool ocl_pow(InputArray _src, double power, OutputArray _dst, #endif -static void InvSqrt_32f(const float* src, float* dst, int n) { hal::invSqrt(src, dst, n); } -static void InvSqrt_64f(const double* src, double* dst, int n) { hal::invSqrt(src, dst, n); } -static void Sqrt_32f(const float* src, float* dst, int n) { hal::sqrt(src, dst, n); } -static void Sqrt_64f(const double* src, double* dst, int n) { hal::sqrt(src, dst, n); } +static void InvSqrt_32f(const float* src, float* dst, int n) { hal::invSqrt32f(src, dst, n); } +static void InvSqrt_64f(const double* src, double* dst, int n) { hal::invSqrt64f(src, dst, n); } +static void Sqrt_32f(const float* src, float* dst, int n) { hal::sqrt32f(src, dst, n); } +static void Sqrt_64f(const double* src, double* dst, int n) { hal::sqrt64f(src, dst, n); } void pow( InputArray _src, double power, OutputArray _dst ) { @@ -1571,9 +1571,8 @@ template<> struct mat_type_assotiations static const type max_allowable = INT_MAX; }; -// inclusive maxVal !!! template -bool checkIntegerRange(cv::Mat src, Point& bad_pt, int minVal, int maxVal, double& bad_value) +static bool checkIntegerRange(cv::Mat src, Point& bad_pt, int minVal, int maxVal) { typedef mat_type_assotiations type_ass; @@ -1591,20 +1590,19 @@ bool checkIntegerRange(cv::Mat src, Point& bad_pt, int minVal, int maxVal, doubl for (int j = 0; j < as_one_channel.rows; ++j) for (int i = 0; i < as_one_channel.cols; ++i) { - if (as_one_channel.at(j ,i) < minVal || as_one_channel.at(j ,i) > maxVal) + typename type_ass::type v = as_one_channel.at(j ,i); + if (v < minVal || v > maxVal) { - bad_pt.y = j ; - bad_pt.x = i % src.channels(); - bad_value = as_one_channel.at(j ,i); + bad_pt.y = j; + bad_pt.x = i / src.channels(); return false; } } - bad_value = 0.0; return true; } -typedef bool (*check_range_function)(cv::Mat src, Point& bad_pt, int minVal, int maxVal, double& bad_value); +typedef bool (*check_range_function)(cv::Mat src, Point& bad_pt, int minVal, int maxVal); check_range_function check_range_functions[] = { @@ -1621,15 +1619,16 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma if ( src.dims > 2 ) { + CV_Assert(pt == NULL); // no way to provide location info + const Mat* arrays[] = {&src, 0}; Mat planes[1]; NAryMatIterator it(arrays, planes); for ( size_t i = 0; i < it.nplanes; i++, ++it ) { - if (!checkRange( it.planes[0], quiet, pt, minVal, maxVal )) + if (!checkRange( it.planes[0], quiet, NULL, minVal, maxVal )) { - // todo: set index properly return false; } } @@ -1638,20 +1637,19 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma int depth = src.depth(); Point badPt(-1, -1); - double badValue = 0; if (depth < CV_32F) { - // see "Bug #1784" - int minVali = minVal<(-INT_MAX - 1) ? (-INT_MAX - 1) : cvFloor(minVal); - int maxVali = maxVal>INT_MAX ? INT_MAX : cvCeil(maxVal) - 1; // checkIntegerRang() use inclusive maxVal + int minVali = minVal <= INT_MIN ? INT_MIN : cvFloor(minVal); + int maxVali = maxVal > INT_MAX ? INT_MAX : cvCeil(maxVal) - 1; - (check_range_functions[depth])(src, badPt, minVali, maxVali, badValue); + (check_range_functions[depth])(src, badPt, minVali, maxVali); } else { int i, loc = 0; - Size size = getContinuousSize( src, src.channels() ); + int cn = src.channels(); + Size size = getContinuousSize( src, cn ); if( depth == CV_32F ) { @@ -1675,8 +1673,8 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma if( val < ia || val >= ib ) { - badPt = Point((loc + i) % src.cols, (loc + i) / src.cols); - badValue = ((const float*)isrc)[i]; + int pixelId = (loc + i) / cn; + badPt = Point(pixelId % src.cols, pixelId / src.cols); break; } } @@ -1704,8 +1702,8 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma if( val < ia || val >= ib ) { - badPt = Point((loc + i) % src.cols, (loc + i) / src.cols); - badValue = ((const double*)isrc)[i]; + int pixelId = (loc + i) / cn; + badPt = Point(pixelId % src.cols, pixelId / src.cols); break; } } @@ -1718,10 +1716,15 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma if( pt ) *pt = badPt; if( !quiet ) + { + cv::String value_str; + value_str << src(cv::Range(badPt.y, badPt.y + 1), cv::Range(badPt.x, badPt.x + 1)); CV_Error_( CV_StsOutOfRange, - ("the value at (%d, %d)=%g is out of range", badPt.x, badPt.y, badValue)); + ("the value at (%d, %d)=%s is out of range [%f, %f)", badPt.x, badPt.y, value_str.c_str(), minVal, maxVal)); + } + return false; } - return badPt.x < 0; + return true; } #ifdef HAVE_OPENCL diff --git a/modules/hal/src/mathfuncs.cpp b/modules/core/src/mathfuncs_core.cpp similarity index 97% rename from modules/hal/src/mathfuncs.cpp rename to modules/core/src/mathfuncs_core.cpp index 66a03e1a5..7b3ec319c 100644 --- a/modules/hal/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs_core.cpp @@ -52,16 +52,6 @@ static const float atan2_p3 = -0.3258083974640975f*(float)(180/CV_PI); static const float atan2_p5 = 0.1555786518463281f*(float)(180/CV_PI); static const float atan2_p7 = -0.04432655554792128f*(float)(180/CV_PI); -#if CV_NEON -static inline float32x4_t cv_vrecpq_f32(float32x4_t val) -{ - float32x4_t reciprocal = vrecpeq_f32(val); - reciprocal = vmulq_f32(vrecpsq_f32(val, reciprocal), reciprocal); - reciprocal = vmulq_f32(vrecpsq_f32(val, reciprocal), reciprocal); - return reciprocal; -} -#endif - void fastAtan2(const float *Y, const float *X, float *angle, int len, bool angleInDegrees ) { int i = 0; @@ -160,7 +150,7 @@ void fastAtan2(const float *Y, const float *X, float *angle, int len, bool angle } -void magnitude(const float* x, const float* y, float* mag, int len) +void magnitude32f(const float* x, const float* y, float* mag, int len) { #if defined HAVE_IPP CV_IPP_CHECK() @@ -196,7 +186,7 @@ void magnitude(const float* x, const float* y, float* mag, int len) } } -void magnitude(const double* x, const double* y, double* mag, int len) +void magnitude64f(const double* x, const double* y, double* mag, int len) { #if defined(HAVE_IPP) CV_IPP_CHECK() @@ -233,7 +223,7 @@ void magnitude(const double* x, const double* y, double* mag, int len) } -void invSqrt(const float* src, float* dst, int len) +void invSqrt32f(const float* src, float* dst, int len) { #if defined(HAVE_IPP) CV_IPP_CHECK() @@ -264,7 +254,7 @@ void invSqrt(const float* src, float* dst, int len) } -void invSqrt(const double* src, double* dst, int len) +void invSqrt64f(const double* src, double* dst, int len) { int i = 0; @@ -279,7 +269,7 @@ void invSqrt(const double* src, double* dst, int len) } -void sqrt(const float* src, float* dst, int len) +void sqrt32f(const float* src, float* dst, int len) { #if defined(HAVE_IPP) CV_IPP_CHECK() @@ -310,7 +300,7 @@ void sqrt(const float* src, float* dst, int len) } -void sqrt(const double* src, double* dst, int len) +void sqrt64f(const double* src, double* dst, int len) { #if defined(HAVE_IPP) CV_IPP_CHECK() @@ -441,7 +431,7 @@ static const double exp_prescale = 1.4426950408889634073599246810019 * (1 << EXP static const double exp_postscale = 1./(1 << EXPTAB_SCALE); static const double exp_max_val = 3000.*(1 << EXPTAB_SCALE); // log10(DBL_MAX) < 3000 -void exp( const float *_x, float *y, int n ) +void exp32f( const float *_x, float *y, int n ) { static const float A4 = (float)(1.000000000000002438532970795181890933776 / EXPPOLY_32F_A0), @@ -640,7 +630,7 @@ void exp( const float *_x, float *y, int n ) } } -void exp( const double *_x, double *y, int n ) +void exp64f( const double *_x, double *y, int n ) { static const double A5 = .99999999999999999998285227504999 / EXPPOLY_32F_A0, @@ -1084,7 +1074,7 @@ static const double CV_DECL_ALIGNED(16) icvLogTab[] = { #define LOGTAB_TRANSLATE(x,h) (((x) - 1.)*icvLogTab[(h)+1]) static const double ln_2 = 0.69314718055994530941723212145818; -void log( const float *_x, float *y, int n ) +void log32f( const float *_x, float *y, int n ) { static const float shift[] = { 0, -1.f/512 }; static const float @@ -1228,7 +1218,7 @@ void log( const float *_x, float *y, int n ) } } -void log( const double *x, double *y, int n ) +void log64f( const double *x, double *y, int n ) { static const double shift[] = { 0, -1./512 }; static const double @@ -1413,4 +1403,58 @@ void log( const double *x, double *y, int n ) } } -}} +//============================================================================= +// for compatibility with 3.0 + +void exp(const float* src, float* dst, int n) +{ + exp32f(src, dst, n); +} + +void exp(const double* src, double* dst, int n) +{ + exp64f(src, dst, n); +} + +void log(const float* src, float* dst, int n) +{ + log32f(src, dst, n); +} + +void log(const double* src, double* dst, int n) +{ + log64f(src, dst, n); +} + +void magnitude(const float* x, const float* y, float* dst, int n) +{ + magnitude32f(x, y, dst, n); +} + +void magnitude(const double* x, const double* y, double* dst, int n) +{ + magnitude64f(x, y, dst, n); +} + +void sqrt(const float* src, float* dst, int len) +{ + sqrt32f(src, dst, len); +} + +void sqrt(const double* src, double* dst, int len) +{ + sqrt64f(src, dst, len); +} + +void invSqrt(const float* src, float* dst, int len) +{ + invSqrt32f(src, dst, len); +} + +void invSqrt(const double* src, double* dst, int len) +{ + invSqrt64f(src, dst, len); +} + + +}} // cv::hal:: diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 488b2b8ed..08c5501e4 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -218,7 +218,24 @@ public: delete u; } }; +namespace +{ + MatAllocator* g_matAllocator = NULL; +} + +MatAllocator* Mat::getDefaultAllocator() +{ + if (g_matAllocator == NULL) + { + g_matAllocator = getStdAllocator(); + } + return g_matAllocator; +} +void Mat::setDefaultAllocator(MatAllocator* allocator) +{ + g_matAllocator = allocator; +} MatAllocator* Mat::getStdAllocator() { CV_SINGLETON_LAZY_INIT(MatAllocator, new StdMatAllocator()) @@ -380,6 +397,14 @@ void Mat::create(int d, const int* _sizes, int _type) return; } + int _sizes_backup[CV_MAX_DIM]; // #5991 + if (_sizes == (this->size.p)) + { + for(i = 0; i < d; i++ ) + _sizes_backup[i] = _sizes[i]; + _sizes = _sizes_backup; + } + release(); if( d == 0 ) return; @@ -388,7 +413,7 @@ void Mat::create(int d, const int* _sizes, int _type) if( total() > 0 ) { - MatAllocator *a = allocator, *a0 = getStdAllocator(); + MatAllocator *a = allocator, *a0 = getDefaultAllocator(); #ifdef HAVE_TGPU if( !a || a == tegra::getAllocator() ) a = tegra::getAllocator(d, _sizes, _type); @@ -426,7 +451,7 @@ void Mat::copySize(const Mat& m) void Mat::deallocate() { if(u) - (u->currAllocator ? u->currAllocator : allocator ? allocator : getStdAllocator())->unmap(u); + (u->currAllocator ? u->currAllocator : allocator ? allocator : getDefaultAllocator())->unmap(u); u = NULL; } @@ -4882,6 +4907,13 @@ void SparseMat::create(int d, const int* _sizes, int _type) return; } } + int _sizes_backup[CV_MAX_DIM]; // #5991 + if (_sizes == hdr->size) + { + for(i = 0; i < d; i++ ) + _sizes_backup[i] = _sizes[i]; + _sizes = _sizes_backup; + } release(); flags = MAGIC_VAL | _type; hdr = new Hdr(d, _sizes, _type); diff --git a/modules/hal/src/matrix.cpp b/modules/core/src/matrix_decomp.cpp similarity index 90% rename from modules/hal/src/matrix.cpp rename to modules/core/src/matrix_decomp.cpp index 921b7783f..3fe9eca72 100644 --- a/modules/hal/src/matrix.cpp +++ b/modules/core/src/matrix_decomp.cpp @@ -109,18 +109,17 @@ LUImpl(_Tp* A, size_t astep, int m, _Tp* b, size_t bstep, int n, _Tp eps) } -int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n) +int LU32f(float* A, size_t astep, int m, float* b, size_t bstep, int n) { return LUImpl(A, astep, m, b, bstep, n, FLT_EPSILON*10); } -int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n) +int LU64f(double* A, size_t astep, int m, double* b, size_t bstep, int n) { return LUImpl(A, astep, m, b, bstep, n, DBL_EPSILON*100); } - template static inline bool CholImpl(_Tp* A, size_t astep, int m, _Tp* b, size_t bstep, int n) { @@ -195,6 +194,29 @@ CholImpl(_Tp* A, size_t astep, int m, _Tp* b, size_t bstep, int n) } +bool Cholesky32f(float* A, size_t astep, int m, float* b, size_t bstep, int n) +{ + return CholImpl(A, astep, m, b, bstep, n); +} + +bool Cholesky64f(double* A, size_t astep, int m, double* b, size_t bstep, int n) +{ + return CholImpl(A, astep, m, b, bstep, n); +} + +//============================================================================= +// for compatibility with 3.0 + +int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n) +{ + return LUImpl(A, astep, m, b, bstep, n, FLT_EPSILON*10); +} + +int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n) +{ + return LUImpl(A, astep, m, b, bstep, n, DBL_EPSILON*100); +} + bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n) { return CholImpl(A, astep, m, b, bstep, n); @@ -205,4 +227,5 @@ bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n) return CholImpl(A, astep, m, b, bstep, n); } + }} diff --git a/modules/hal/src/merge.cpp b/modules/core/src/merge.cpp similarity index 98% rename from modules/hal/src/merge.cpp rename to modules/core/src/merge.cpp index 982b24c25..abe9f643e 100644 --- a/modules/hal/src/merge.cpp +++ b/modules/core/src/merge.cpp @@ -387,21 +387,25 @@ merge_( const T** src, T* dst, int len, int cn ) void merge8u(const uchar** src, uchar* dst, int len, int cn ) { + CALL_HAL(merge8u, cv_hal_merge8u, src, dst, len, cn) merge_(src, dst, len, cn); } void merge16u(const ushort** src, ushort* dst, int len, int cn ) { + CALL_HAL(merge16u, cv_hal_merge16u, src, dst, len, cn) merge_(src, dst, len, cn); } void merge32s(const int** src, int* dst, int len, int cn ) { + CALL_HAL(merge32s, cv_hal_merge32s, src, dst, len, cn) merge_(src, dst, len, cn); } void merge64s(const int64** src, int64* dst, int len, int cn ) { + CALL_HAL(merge64s, cv_hal_merge64s, src, dst, len, cn) merge_(src, dst, len, cn); } diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 1345c52e3..6780d85e5 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -684,10 +684,13 @@ typedef struct _cl_buffer_region { #define CL_CALLBACK CV_STDCALL -static volatile bool g_haveOpenCL = false; -static const char* oclFuncToCheck = "clEnqueueReadBufferRect"; -#if defined(__APPLE__) +#ifdef HAVE_OPENCL +static const char* oclFuncToCheck = "clEnqueueReadBufferRect"; +static volatile bool g_haveOpenCL = false; +#endif + +#if defined(__APPLE__) && defined(HAVE_OPENCL) #include static void* initOpenCLAndLoad(const char* funcname) @@ -716,7 +719,7 @@ static void* initOpenCLAndLoad(const char* funcname) return funcname && handle ? dlsym(handle, funcname) : 0; } -#elif defined WIN32 || defined _WIN32 +#elif (defined WIN32 || defined _WIN32) && defined(HAVE_OPENCL) #ifndef _WIN32_WINNT // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?) #define _WIN32_WINNT 0x0400 // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx @@ -751,7 +754,7 @@ static void* initOpenCLAndLoad(const char* funcname) return funcname ? (void*)GetProcAddress(handle, funcname) : 0; } -#elif defined(__linux) +#elif defined(__linux) && defined(HAVE_OPENCL) #include #include @@ -3902,7 +3905,7 @@ protected: if (e.capacity_ >= size) { size_t diff = e.capacity_ - size; - if (diff < size / 8 && (result_pos == reservedEntries_.end() || diff < minDiff)) + if (diff < std::max((size_t)4096, size / 8) && (result_pos == reservedEntries_.end() || diff < minDiff)) { minDiff = diff; result_pos = i; @@ -3941,12 +3944,8 @@ protected: inline size_t _allocationGranularity(size_t size) { // heuristic values - if (size < 1024) - return 16; - else if (size < 64*1024) - return 64; - else if (size < 1024*1024) - return 4096; + if (size < 1024*1024) + return 4096; // don't work with buffers smaller than 4Kb (hidden allocation overhead issue) else if (size < 16*1024*1024) return 64*1024; else @@ -4292,7 +4291,7 @@ public: bufferPoolSVM.setMaxReservedSize(poolSize); #endif - matStdAllocator = Mat::getStdAllocator(); + matStdAllocator = Mat::getDefaultAllocator(); } UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step, @@ -4918,7 +4917,7 @@ public: if( u->data && !u->hostCopyObsolete() ) { - Mat::getStdAllocator()->download(u, dstptr, dims, sz, srcofs, srcstep, dststep); + Mat::getDefaultAllocator()->download(u, dstptr, dims, sz, srcofs, srcstep, dststep); return; } CV_Assert( u->handle != 0 ); @@ -5042,7 +5041,7 @@ public: // 2. we overwrite part of the matrix, but the GPU copy is out-of-date if( u->data && (u->hostCopyObsolete() < u->deviceCopyObsolete() || total == u->size)) { - Mat::getStdAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep); + Mat::getDefaultAllocator()->upload(u, srcptr, dims, sz, dstofs, dststep, srcstep); u->markHostCopyObsolete(false); u->markDeviceCopyObsolete(true); return; diff --git a/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp b/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp index 6d9718050..5b371330c 100644 --- a/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp +++ b/modules/core/src/opencl/runtime/autogenerated/opencl_gl_impl.hpp @@ -139,6 +139,8 @@ struct opencl_gl_fn14 } // anonymous namespace +#ifdef cl_khr_gl_sharing + // generated by parser_cl.py cl_mem (CL_API_CALL*clCreateFromGLBuffer)(cl_context, cl_mem_flags, cl_GLuint, int*) = opencl_gl_fn4::switch_fn; @@ -196,3 +198,5 @@ static const struct DynamicFnEntry* opencl_gl_fn_list[] = { }; // number of enabled functions: 10 + +#endif // cl_khr_gl_sharing diff --git a/modules/core/src/opencl/runtime/generator/common.py b/modules/core/src/opencl/runtime/generator/common.py index 80c545295..5f81e8ee5 100644 --- a/modules/core/src/opencl/runtime/generator/common.py +++ b/modules/core/src/opencl/runtime/generator/common.py @@ -254,6 +254,6 @@ def ProcessTemplate(inputFile, ctx, noteLine='//\n// AUTOGENERATED, DO NOT EDIT\ assert line[-1] == '@' name = line[1:-1] assert ctx.has_key(name), name - line = ctx[name] - print line, + line = ctx[name] + ('\n' if len(ctx[name]) > 0 and ctx[name][-1] != '\n' else '') + sys.stdout.write(line) f.close() diff --git a/modules/core/src/opencl/runtime/generator/generate.sh b/modules/core/src/opencl/runtime/generator/generate.sh old mode 100644 new mode 100755 diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_core.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_core.hpp.in index 4196622c0..b6c8f05d5 100644 --- a/modules/core/src/opencl/runtime/generator/template/opencl_core.hpp.in +++ b/modules/core/src/opencl/runtime/generator/template/opencl_core.hpp.in @@ -4,11 +4,7 @@ @CL_REMAP_ORIGIN@ -#if defined __APPLE__ -#include -#else #include -#endif @CL_REMAP_DYNAMIC@ diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in index 24434d2de..13a33c715 100644 --- a/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl.hpp.in @@ -4,12 +4,12 @@ @CL_REMAP_ORIGIN@ -#if defined __APPLE__ -#include -#else #include -#endif @CL_REMAP_DYNAMIC@ +#ifdef cl_khr_gl_sharing + @CL_FN_DECLARATIONS@ + +#endif // cl_khr_gl_sharing diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in index 14586017a..cba1278a3 100644 --- a/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl_impl.hpp.in @@ -4,8 +4,12 @@ namespace { @CL_FN_SWITCH@ } // anonymous namespace +#ifdef cl_khr_gl_sharing + @CL_FN_ENTRY_DEFINITIONS@ @CL_FN_ENTRY_LIST@ @CL_NUMBER_OF_ENABLED_FUNCTIONS@ + +#endif // cl_khr_gl_sharing diff --git a/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in b/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in index 0aeefb4f4..fc0317914 100644 --- a/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in +++ b/modules/core/src/opencl/runtime/generator/template/opencl_gl_wrappers.hpp.in @@ -2,4 +2,8 @@ #error "Invalid usage" #endif +#ifdef cl_khr_gl_sharing + @CL_FN_INLINE_WRAPPERS@ + +#endif // cl_khr_gl_sharing diff --git a/modules/core/src/opencl/runtime/opencl_core.cpp b/modules/core/src/opencl/runtime/opencl_core.cpp index 971c0770e..405a5936a 100644 --- a/modules/core/src/opencl/runtime/opencl_core.cpp +++ b/modules/core/src/opencl/runtime/opencl_core.cpp @@ -58,11 +58,11 @@ static void* AppleCLGetProcAddress(const char* name) { static bool initialized = false; static void* handle = NULL; - if (!handle) + if (!handle && !initialized) { - if(!initialized) + cv::AutoLock lock(cv::getInitializationMutex()); + if (!initialized) { - initialized = true; const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL"; const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); if (envPath) @@ -78,10 +78,11 @@ static void* AppleCLGetProcAddress(const char* name) fprintf(stderr, ERROR_MSG_INVALID_VERSION); handle = NULL; } + initialized = true; } - if (!handle) - return NULL; } + if (!handle) + return NULL; return dlsym(handle, name); } #define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name) @@ -94,11 +95,11 @@ static void* WinGetProcAddress(const char* name) { static bool initialized = false; static HMODULE handle = NULL; - if (!handle) + if (!handle && !initialized) { - if(!initialized) + cv::AutoLock lock(cv::getInitializationMutex()); + if (!initialized) { - initialized = true; handle = GetModuleHandleA("OpenCL.dll"); if (!handle) { @@ -118,10 +119,11 @@ static void* WinGetProcAddress(const char* name) handle = NULL; } } + initialized = true; } - if (!handle) - return NULL; } + if (!handle) + return NULL; return (void*)GetProcAddress(handle, name); } #define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name) @@ -135,11 +137,11 @@ static void* GetProcAddress(const char* name) { static bool initialized = false; static void* handle = NULL; - if (!handle) + if (!handle && !initialized) { - if(!initialized) + cv::AutoLock lock(cv::getInitializationMutex()); + if (!initialized) { - initialized = true; const char* path = "libOpenCL.so"; const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); if (envPath) @@ -155,10 +157,11 @@ static void* GetProcAddress(const char* name) fprintf(stderr, ERROR_MSG_INVALID_VERSION); handle = NULL; } + initialized = true; } - if (!handle) - return NULL; } + if (!handle) + return NULL; return dlsym(handle, name); } #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name) @@ -283,6 +286,8 @@ static void* opencl_check_fn(int ID) #include "opencv2/core/opencl/runtime/opencl_gl.hpp" +#ifdef cl_khr_gl_sharing + static void* opencl_gl_check_fn(int ID); #include "autogenerated/opencl_gl_impl.hpp" @@ -303,6 +308,8 @@ static void* opencl_gl_check_fn(int ID) return func; } +#endif // cl_khr_gl_sharing + #endif // HAVE_OPENGL #endif diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 3bbc0f8e9..f3d106fd2 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1580,6 +1580,11 @@ void cv::ogl::render(const ogl::Arrays& arr, InputArray indices, int mode, Scala #ifdef HAVE_OPENCL # include "opencv2/core/opencl/runtime/opencl_gl.hpp" +# ifdef cl_khr_gl_sharing +# define HAVE_OPENCL_OPENGL_SHARING +# else +# define NO_OPENCL_SHARING_ERROR CV_ErrorNoReturn(cv::Error::StsBadFunc, "OpenCV was build without OpenCL/OpenGL sharing support") +# endif #else // HAVE_OPENCL # define NO_OPENCL_SUPPORT_ERROR CV_ErrorNoReturn(cv::Error::StsBadFunc, "OpenCV was build without OpenCL support") #endif // HAVE_OPENCL @@ -1602,6 +1607,8 @@ Context& initializeContextFromGL() NO_OPENGL_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL_OPENGL_SHARING) + NO_OPENCL_SHARING_ERROR; #else cl_uint numPlatforms; cl_int status = clGetPlatformIDs(0, NULL, &numPlatforms); @@ -1701,6 +1708,8 @@ void convertToGLTexture2D(InputArray src, Texture2D& texture) NO_OPENGL_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL_OPENGL_SHARING) + NO_OPENCL_SHARING_ERROR; #else Size srcSize = src.size(); CV_Assert(srcSize.width == (int)texture.cols() && srcSize.height == (int)texture.rows()); @@ -1753,6 +1762,8 @@ void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst) NO_OPENGL_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL_OPENGL_SHARING) + NO_OPENCL_SHARING_ERROR; #else // check texture format const int dtype = CV_8UC4; @@ -1812,6 +1823,8 @@ UMat mapGLBuffer(const Buffer& buffer, int accessFlags) NO_OPENGL_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL_OPENGL_SHARING) + NO_OPENCL_SHARING_ERROR; #else using namespace cv::ocl; Context& ctx = Context::getDefault(); @@ -1862,6 +1875,8 @@ void unmapGLBuffer(UMat& u) NO_OPENGL_SUPPORT_ERROR; #elif !defined(HAVE_OPENCL) NO_OPENCL_SUPPORT_ERROR; +#elif !defined(HAVE_OPENCL_OPENGL_SHARING) + NO_OPENCL_SHARING_ERROR; #else using namespace cv::ocl; cl_command_queue clQueue = (cl_command_queue)Queue::getDefault().ptr(); diff --git a/modules/core/src/parallel.cpp b/modules/core/src/parallel.cpp index 064d44cd9..b805ab46b 100644 --- a/modules/core/src/parallel.cpp +++ b/modules/core/src/parallel.cpp @@ -107,7 +107,7 @@ #elif defined HAVE_GCD #include #include - #elif defined WINRT + #elif defined WINRT && _MSC_VER < 1900 #include #elif defined HAVE_CONCURRENCY #include diff --git a/modules/core/src/parallel_pthreads.cpp b/modules/core/src/parallel_pthreads.cpp index 8bf131e14..96ddfe7ea 100644 --- a/modules/core/src/parallel_pthreads.cpp +++ b/modules/core/src/parallel_pthreads.cpp @@ -155,22 +155,9 @@ public: static ThreadManager& instance() { - if(!m_instance.ptr) - { - pthread_mutex_lock(&m_manager_access_mutex); - - if(!m_instance.ptr) - { - m_instance.ptr = new ThreadManager(); - } - - pthread_mutex_unlock(&m_manager_access_mutex); - } - - return *m_instance.ptr; + CV_SINGLETON_LAZY_INIT_REF(ThreadManager, new ThreadManager()) } - static void stop() { ThreadManager& manager = instance(); @@ -194,21 +181,6 @@ public: private: - struct ptr_holder - { - ThreadManager* ptr; - - ptr_holder(): ptr(NULL) { } - - ~ptr_holder() - { - if(ptr) - { - delete ptr; - } - } - }; - ThreadManager(); ~ThreadManager(); @@ -231,8 +203,7 @@ private: unsigned int m_task_position; unsigned int m_num_of_completed_tasks; - static pthread_mutex_t m_manager_access_mutex; - static ptr_holder m_instance; + pthread_mutex_t m_manager_access_mutex; static const char m_env_name[]; static const unsigned int m_default_number_of_threads; @@ -250,13 +221,6 @@ private: ThreadManagerPoolState m_pool_state; }; -#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP -#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER -#endif - -pthread_mutex_t ThreadManager::m_manager_access_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; - -ThreadManager::ptr_holder ThreadManager::m_instance; const char ThreadManager::m_env_name[] = "OPENCV_FOR_THREADS_NUM"; #ifdef ANDROID @@ -383,6 +347,12 @@ ThreadManager::ThreadManager(): m_num_threads(0), m_task_complete(false), m_num_ { int res = 0; + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + res |= pthread_mutex_init(&m_manager_access_mutex, &attr); + pthread_mutexattr_destroy(&attr); + res |= pthread_mutex_init(&m_manager_task_mutex, NULL); res |= pthread_cond_init(&m_cond_thread_task_complete, NULL); diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index d1f2ec22e..f699ede39 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -58,8 +58,6 @@ #include "opencv2/core/ocl.hpp" #endif -#include "opencv2/hal.hpp" - #include #include #include @@ -69,6 +67,27 @@ #include #include +#include +#include +#include +#include +#include +#include +#include + +#define USE_SSE2 (cv::checkHardwareSupport(CV_CPU_SSE)) +#define USE_SSE4_2 (cv::checkHardwareSupport(CV_CPU_SSE4_2)) +#define USE_AVX (cv::checkHardwareSupport(CV_CPU_AVX)) +#define USE_AVX2 (cv::checkHardwareSupport(CV_CPU_AVX2)) + +#include "opencv2/core/hal/hal.hpp" +#include "opencv2/core/hal/intrin.hpp" +#include "opencv2/core/sse_utils.hpp" +#include "opencv2/core/neon_utils.hpp" + +#include "arithm_core.hpp" +#include "hal_replacement.hpp" + #ifdef HAVE_TEGRA_OPTIMIZATION #include "opencv2/core/core_tegra.hpp" #else @@ -78,6 +97,34 @@ namespace cv { +// -128.f ... 255.f +extern const float g_8x32fTab[]; +#define CV_8TO32F(x) cv::g_8x32fTab[(x)+128] + +extern const ushort g_8x16uSqrTab[]; +#define CV_SQR_8U(x) cv::g_8x16uSqrTab[(x)+255] + +extern const uchar g_Saturate8u[]; +#define CV_FAST_CAST_8U(t) (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256]) +#define CV_MIN_8U(a,b) ((a) - CV_FAST_CAST_8U((a) - (b))) +#define CV_MAX_8U(a,b) ((a) + CV_FAST_CAST_8U((b) - (a))) + +template<> inline uchar OpAdd::operator ()(uchar a, uchar b) const +{ return CV_FAST_CAST_8U(a + b); } + +template<> inline uchar OpSub::operator ()(uchar a, uchar b) const +{ return CV_FAST_CAST_8U(a - b); } + +template<> inline short OpAbsDiff::operator ()(short a, short b) const +{ return saturate_cast(std::abs(a - b)); } + +template<> inline schar OpAbsDiff::operator ()(schar a, schar b) const +{ return saturate_cast(std::abs(a - b)); } + +template<> inline uchar OpMin::operator ()(uchar a, uchar b) const { return CV_MIN_8U(a, b); } + +template<> inline uchar OpMax::operator ()(uchar a, uchar b) const { return CV_MAX_8U(a, b); } + typedef void (*BinaryFunc)(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, Size sz, @@ -100,21 +147,6 @@ BinaryFunc getCopyMaskFunc(size_t esz); /* maximal average node_count/hash_size ratio beyond which hash table is resized */ #define CV_SPARSE_HASH_RATIO 3 - - -// -128.f ... 255.f -extern const float g_8x32fTab[]; -#define CV_8TO32F(x) cv::g_8x32fTab[(x)+128] - -extern const ushort g_8x16uSqrTab[]; -#define CV_SQR_8U(x) cv::g_8x16uSqrTab[(x)+255] - -extern const uchar g_Saturate8u[]; -#define CV_FAST_CAST_8U(t) (assert(-256 <= (t) && (t) <= 512), cv::g_Saturate8u[(t)+256]) -#define CV_MIN_8U(a,b) ((a) - CV_FAST_CAST_8U((a) - (b))) -#define CV_MAX_8U(a,b) ((a) + CV_FAST_CAST_8U((b) - (a))) - - #if defined WIN32 || defined _WIN32 void deleteThreadAllocData(); #endif @@ -282,6 +314,4 @@ cv::Mutex& getInitializationMutex(); } -#include "opencv2/hal/intrin.hpp" - #endif /*_CXCORE_INTERNAL_H_*/ diff --git a/modules/hal/src/split.cpp b/modules/core/src/split.cpp similarity index 98% rename from modules/hal/src/split.cpp rename to modules/core/src/split.cpp index c31bf8cc4..311e97d55 100644 --- a/modules/hal/src/split.cpp +++ b/modules/core/src/split.cpp @@ -403,21 +403,25 @@ split_( const T* src, T** dst, int len, int cn ) void split8u(const uchar* src, uchar** dst, int len, int cn ) { + CALL_HAL(split8u, cv_hal_split8u, src,dst, len, cn) split_(src, dst, len, cn); } void split16u(const ushort* src, ushort** dst, int len, int cn ) { + CALL_HAL(split16u, cv_hal_split16u, src,dst, len, cn) split_(src, dst, len, cn); } void split32s(const int* src, int** dst, int len, int cn ) { + CALL_HAL(split32s, cv_hal_split32s, src,dst, len, cn) split_(src, dst, len, cn); } void split64s(const int64* src, int64** dst, int len, int cn ) { + CALL_HAL(split64s, cv_hal_split64s, src,dst, len, cn) split_(src, dst, len, cn); } diff --git a/modules/core/src/stat.cpp b/modules/core/src/stat.cpp index 4e60dbe4f..e3525752e 100644 --- a/modules/core/src/stat.cpp +++ b/modules/core/src/stat.cpp @@ -3996,3 +3996,266 @@ cvNorm( const void* imgA, const void* imgB, int normType, const void* maskarr ) return !maskarr ? cv::norm(a, b, normType) : cv::norm(a, b, normType, mask); } + +namespace cv { namespace hal { + +static const uchar popCountTable[] = +{ + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 +}; + +static const uchar popCountTable2[] = +{ + 0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, + 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, + 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, + 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, + 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, + 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, + 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, + 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4 +}; + +static const uchar popCountTable4[] = +{ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 +}; + +int normHamming(const uchar* a, int n) +{ + int i = 0; + int result = 0; +#if CV_NEON + { + uint32x4_t bits = vmovq_n_u32(0); + for (; i <= n - 16; i += 16) { + uint8x16_t A_vec = vld1q_u8 (a + i); + uint8x16_t bitsSet = vcntq_u8 (A_vec); + uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet); + uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8); + bits = vaddq_u32(bits, bitSet4); + } + uint64x2_t bitSet2 = vpaddlq_u32 (bits); + result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0); + result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2); + } +#endif + for( ; i <= n - 4; i += 4 ) + result += popCountTable[a[i]] + popCountTable[a[i+1]] + + popCountTable[a[i+2]] + popCountTable[a[i+3]]; + for( ; i < n; i++ ) + result += popCountTable[a[i]]; + return result; +} + +int normHamming(const uchar* a, const uchar* b, int n) +{ + int i = 0; + int result = 0; +#if CV_NEON + { + uint32x4_t bits = vmovq_n_u32(0); + for (; i <= n - 16; i += 16) { + uint8x16_t A_vec = vld1q_u8 (a + i); + uint8x16_t B_vec = vld1q_u8 (b + i); + uint8x16_t AxorB = veorq_u8 (A_vec, B_vec); + uint8x16_t bitsSet = vcntq_u8 (AxorB); + uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet); + uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8); + bits = vaddq_u32(bits, bitSet4); + } + uint64x2_t bitSet2 = vpaddlq_u32 (bits); + result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0); + result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2); + } +#endif + for( ; i <= n - 4; i += 4 ) + result += popCountTable[a[i] ^ b[i]] + popCountTable[a[i+1] ^ b[i+1]] + + popCountTable[a[i+2] ^ b[i+2]] + popCountTable[a[i+3] ^ b[i+3]]; + for( ; i < n; i++ ) + result += popCountTable[a[i] ^ b[i]]; + return result; +} + +int normHamming(const uchar* a, int n, int cellSize) +{ + if( cellSize == 1 ) + return normHamming(a, n); + const uchar* tab = 0; + if( cellSize == 2 ) + tab = popCountTable2; + else if( cellSize == 4 ) + tab = popCountTable4; + else + return -1; + int i = 0; + int result = 0; +#if CV_ENABLE_UNROLLED + for( ; i <= n - 4; i += 4 ) + result += tab[a[i]] + tab[a[i+1]] + tab[a[i+2]] + tab[a[i+3]]; +#endif + for( ; i < n; i++ ) + result += tab[a[i]]; + return result; +} + +int normHamming(const uchar* a, const uchar* b, int n, int cellSize) +{ + if( cellSize == 1 ) + return normHamming(a, b, n); + const uchar* tab = 0; + if( cellSize == 2 ) + tab = popCountTable2; + else if( cellSize == 4 ) + tab = popCountTable4; + else + return -1; + int i = 0; + int result = 0; + #if CV_ENABLE_UNROLLED + for( ; i <= n - 4; i += 4 ) + result += tab[a[i] ^ b[i]] + tab[a[i+1] ^ b[i+1]] + + tab[a[i+2] ^ b[i+2]] + tab[a[i+3] ^ b[i+3]]; + #endif + for( ; i < n; i++ ) + result += tab[a[i] ^ b[i]]; + return result; +} + +float normL2Sqr_(const float* a, const float* b, int n) +{ + int j = 0; float d = 0.f; +#if CV_SSE + float CV_DECL_ALIGNED(16) buf[4]; + __m128 d0 = _mm_setzero_ps(), d1 = _mm_setzero_ps(); + + for( ; j <= n - 8; j += 8 ) + { + __m128 t0 = _mm_sub_ps(_mm_loadu_ps(a + j), _mm_loadu_ps(b + j)); + __m128 t1 = _mm_sub_ps(_mm_loadu_ps(a + j + 4), _mm_loadu_ps(b + j + 4)); + d0 = _mm_add_ps(d0, _mm_mul_ps(t0, t0)); + d1 = _mm_add_ps(d1, _mm_mul_ps(t1, t1)); + } + _mm_store_ps(buf, _mm_add_ps(d0, d1)); + d = buf[0] + buf[1] + buf[2] + buf[3]; +#endif + { + for( ; j <= n - 4; j += 4 ) + { + float t0 = a[j] - b[j], t1 = a[j+1] - b[j+1], t2 = a[j+2] - b[j+2], t3 = a[j+3] - b[j+3]; + d += t0*t0 + t1*t1 + t2*t2 + t3*t3; + } + } + + for( ; j < n; j++ ) + { + float t = a[j] - b[j]; + d += t*t; + } + return d; +} + + +float normL1_(const float* a, const float* b, int n) +{ + int j = 0; float d = 0.f; +#if CV_SSE + float CV_DECL_ALIGNED(16) buf[4]; + static const int CV_DECL_ALIGNED(16) absbuf[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff}; + __m128 d0 = _mm_setzero_ps(), d1 = _mm_setzero_ps(); + __m128 absmask = _mm_load_ps((const float*)absbuf); + + for( ; j <= n - 8; j += 8 ) + { + __m128 t0 = _mm_sub_ps(_mm_loadu_ps(a + j), _mm_loadu_ps(b + j)); + __m128 t1 = _mm_sub_ps(_mm_loadu_ps(a + j + 4), _mm_loadu_ps(b + j + 4)); + d0 = _mm_add_ps(d0, _mm_and_ps(t0, absmask)); + d1 = _mm_add_ps(d1, _mm_and_ps(t1, absmask)); + } + _mm_store_ps(buf, _mm_add_ps(d0, d1)); + d = buf[0] + buf[1] + buf[2] + buf[3]; +#elif CV_NEON + float32x4_t v_sum = vdupq_n_f32(0.0f); + for ( ; j <= n - 4; j += 4) + v_sum = vaddq_f32(v_sum, vabdq_f32(vld1q_f32(a + j), vld1q_f32(b + j))); + + float CV_DECL_ALIGNED(16) buf[4]; + vst1q_f32(buf, v_sum); + d = buf[0] + buf[1] + buf[2] + buf[3]; +#endif + { + for( ; j <= n - 4; j += 4 ) + { + d += std::abs(a[j] - b[j]) + std::abs(a[j+1] - b[j+1]) + + std::abs(a[j+2] - b[j+2]) + std::abs(a[j+3] - b[j+3]); + } + } + + for( ; j < n; j++ ) + d += std::abs(a[j] - b[j]); + return d; +} + +int normL1_(const uchar* a, const uchar* b, int n) +{ + int j = 0, d = 0; +#if CV_SSE + __m128i d0 = _mm_setzero_si128(); + + for( ; j <= n - 16; j += 16 ) + { + __m128i t0 = _mm_loadu_si128((const __m128i*)(a + j)); + __m128i t1 = _mm_loadu_si128((const __m128i*)(b + j)); + + d0 = _mm_add_epi32(d0, _mm_sad_epu8(t0, t1)); + } + + for( ; j <= n - 4; j += 4 ) + { + __m128i t0 = _mm_cvtsi32_si128(*(const int*)(a + j)); + __m128i t1 = _mm_cvtsi32_si128(*(const int*)(b + j)); + + d0 = _mm_add_epi32(d0, _mm_sad_epu8(t0, t1)); + } + d = _mm_cvtsi128_si32(_mm_add_epi32(d0, _mm_unpackhi_epi64(d0, d0))); +#elif CV_NEON + uint32x4_t v_sum = vdupq_n_u32(0.0f); + for ( ; j <= n - 16; j += 16) + { + uint8x16_t v_dst = vabdq_u8(vld1q_u8(a + j), vld1q_u8(b + j)); + uint16x8_t v_low = vmovl_u8(vget_low_u8(v_dst)), v_high = vmovl_u8(vget_high_u8(v_dst)); + v_sum = vaddq_u32(v_sum, vaddl_u16(vget_low_u16(v_low), vget_low_u16(v_high))); + v_sum = vaddq_u32(v_sum, vaddl_u16(vget_high_u16(v_low), vget_high_u16(v_high))); + } + + uint CV_DECL_ALIGNED(16) buf[4]; + vst1q_u32(buf, v_sum); + d = buf[0] + buf[1] + buf[2] + buf[3]; +#endif + { + for( ; j <= n - 4; j += 4 ) + { + d += std::abs(a[j] - b[j]) + std::abs(a[j+1] - b[j+1]) + + std::abs(a[j+2] - b[j+2]) + std::abs(a[j+3] - b[j+3]); + } + } + for( ; j < n; j++ ) + d += std::abs(a[j] - b[j]); + return d; +} + +}} //cv::hal diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index ba2c9d536..768280e1a 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -86,6 +86,45 @@ Mutex* __initialization_mutex_initializer = &getInitializationMutex(); #undef max #undef abs #include +#if defined _MSC_VER + #if _MSC_VER >= 1400 + #include + #elif defined _M_IX86 + static void __cpuid(int* cpuid_data, int) + { + __asm + { + push ebx + push edi + mov edi, cpuid_data + mov eax, 1 + cpuid + mov [edi], eax + mov [edi + 4], ebx + mov [edi + 8], ecx + mov [edi + 12], edx + pop edi + pop ebx + } + } + static void __cpuidex(int* cpuid_data, int, int) + { + __asm + { + push edi + mov edi, cpuid_data + mov eax, 7 + mov ecx, 0 + cpuid + mov [edi], eax + mov [edi + 4], ebx + mov [edi + 8], ecx + mov [edi + 12], edx + pop edi + } + } + #endif +#endif #ifdef WINRT #include @@ -198,15 +237,154 @@ void Exception::formatMessage() msg = format("%s:%d: error: (%d) %s\n", file.c_str(), line, code, err.c_str()); } +struct HWFeatures +{ + enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE }; + + HWFeatures(void) + { + memset( have, 0, sizeof(have) ); + x86_family = 0; + } + + static HWFeatures initialize(void) + { + HWFeatures f; + int cpuid_data[4] = { 0, 0, 0, 0 }; + + #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64) + __cpuid(cpuid_data, 1); + #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__) + #ifdef __x86_64__ + asm __volatile__ + ( + "movl $1, %%eax\n\t" + "cpuid\n\t" + :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3]) + : + : "cc" + ); + #else + asm volatile + ( + "pushl %%ebx\n\t" + "movl $1,%%eax\n\t" + "cpuid\n\t" + "popl %%ebx\n\t" + : "=a"(cpuid_data[0]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) + : + : "cc" + ); + #endif + #endif + + f.x86_family = (cpuid_data[0] >> 8) & 15; + if( f.x86_family >= 6 ) + { + f.have[CV_CPU_MMX] = (cpuid_data[3] & (1 << 23)) != 0; + f.have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0; + f.have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0; + f.have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0; + f.have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0; + f.have[CV_CPU_FMA3] = (cpuid_data[2] & (1<<12)) != 0; + f.have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0; + f.have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0; + f.have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0; + f.have[CV_CPU_AVX] = (((cpuid_data[2] & (1<<28)) != 0)&&((cpuid_data[2] & (1<<27)) != 0));//OS uses XSAVE_XRSTORE and CPU support AVX + + // make the second call to the cpuid command in order to get + // information about extended features like AVX2 + #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64) + __cpuidex(cpuid_data, 7, 0); + #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__) + #ifdef __x86_64__ + asm __volatile__ + ( + "movl $7, %%eax\n\t" + "movl $0, %%ecx\n\t" + "cpuid\n\t" + :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3]) + : + : "cc" + ); + #else + asm volatile + ( + "pushl %%ebx\n\t" + "movl $7,%%eax\n\t" + "movl $0,%%ecx\n\t" + "cpuid\n\t" + "movl %%ebx, %0\n\t" + "popl %%ebx\n\t" + : "=r"(cpuid_data[1]), "=c"(cpuid_data[2]) + : + : "cc" + ); + #endif + #endif + f.have[CV_CPU_AVX2] = (cpuid_data[1] & (1<<5)) != 0; + + f.have[CV_CPU_AVX_512F] = (cpuid_data[1] & (1<<16)) != 0; + f.have[CV_CPU_AVX_512DQ] = (cpuid_data[1] & (1<<17)) != 0; + f.have[CV_CPU_AVX_512IFMA512] = (cpuid_data[1] & (1<<21)) != 0; + f.have[CV_CPU_AVX_512PF] = (cpuid_data[1] & (1<<26)) != 0; + f.have[CV_CPU_AVX_512ER] = (cpuid_data[1] & (1<<27)) != 0; + f.have[CV_CPU_AVX_512CD] = (cpuid_data[1] & (1<<28)) != 0; + f.have[CV_CPU_AVX_512BW] = (cpuid_data[1] & (1<<30)) != 0; + f.have[CV_CPU_AVX_512VL] = (cpuid_data[1] & (1<<31)) != 0; + f.have[CV_CPU_AVX_512VBMI] = (cpuid_data[2] & (1<<1)) != 0; + } + + #if defined ANDROID || defined __linux__ + #ifdef __aarch64__ + f.have[CV_CPU_NEON] = true; + #else + int cpufile = open("/proc/self/auxv", O_RDONLY); + + if (cpufile >= 0) + { + Elf32_auxv_t auxv; + const size_t size_auxv_t = sizeof(auxv); + + while ((size_t)read(cpufile, &auxv, size_auxv_t) == size_auxv_t) + { + if (auxv.a_type == AT_HWCAP) + { + f.have[CV_CPU_NEON] = (auxv.a_un.a_val & 4096) != 0; + break; + } + } + + close(cpufile); + } + #endif + #elif (defined __clang__ || defined __APPLE__) && (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__)) + f.have[CV_CPU_NEON] = true; + #endif + + return f; + } + + int x86_family; + bool have[MAX_FEATURE+1]; +}; + +static HWFeatures featuresEnabled = HWFeatures::initialize(), featuresDisabled = HWFeatures(); +static HWFeatures* currentFeatures = &featuresEnabled; + bool checkHardwareSupport(int feature) { CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE ); - return cv::hal::checkHardwareSupport(feature); + return currentFeatures->have[feature]; } + +volatile bool useOptimizedFlag = true; + void setUseOptimized( bool flag ) { - cv::hal::setUseOptimized(flag); + useOptimizedFlag = flag; + currentFeatures = flag ? &featuresEnabled : &featuresDisabled; ipp::setUseIPP(flag); #ifdef HAVE_OPENCL @@ -219,7 +397,7 @@ void setUseOptimized( bool flag ) bool useOptimized(void) { - return cv::hal::useOptimized(); + return useOptimizedFlag; } int64 getTickCount(void) @@ -499,12 +677,12 @@ redirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata) CV_IMPL int cvCheckHardwareSupport(int feature) { CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE ); - return cv::hal::checkHardwareSupport(feature); + return cv::currentFeatures->have[feature]; } CV_IMPL int cvUseOptimized( int flag ) { - int prevMode = cv::useOptimized(); + int prevMode = cv::useOptimizedFlag; cv::setUseOptimized( flag != 0 ); return prevMode; } @@ -907,11 +1085,14 @@ public: for(size_t i = 0; i < threads.size(); i++) { - std::vector& thread_slots = threads[i]->slots; - if (thread_slots.size() > slotIdx && thread_slots[slotIdx]) + if(threads[i]) { - dataVec.push_back(thread_slots[slotIdx]); - threads[i]->slots[slotIdx] = 0; + std::vector& thread_slots = threads[i]->slots; + if (thread_slots.size() > slotIdx && thread_slots[slotIdx]) + { + dataVec.push_back(thread_slots[slotIdx]); + threads[i]->slots[slotIdx] = 0; + } } } @@ -938,9 +1119,12 @@ public: for(size_t i = 0; i < threads.size(); i++) { - std::vector& thread_slots = threads[i]->slots; - if (thread_slots.size() > slotIdx && thread_slots[slotIdx]) - dataVec.push_back(thread_slots[slotIdx]); + if(threads[i]) + { + std::vector& thread_slots = threads[i]->slots; + if (thread_slots.size() > slotIdx && thread_slots[slotIdx]) + dataVec.push_back(thread_slots[slotIdx]); + } } } diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index 999a5cc18..958723b7b 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -94,7 +94,7 @@ UMatData::~UMatData() // simulate Mat::deallocate if (u->mapcount != 0) { - (u->currAllocator ? u->currAllocator : /* TODO allocator ? allocator :*/ Mat::getStdAllocator())->unmap(u); + (u->currAllocator ? u->currAllocator : /* TODO allocator ? allocator :*/ Mat::getDefaultAllocator())->unmap(u); } else { @@ -144,7 +144,7 @@ MatAllocator* UMat::getStdAllocator() if( ocl::haveOpenCL() && ocl::useOpenCL() ) return ocl::getOpenCLAllocator(); #endif - return Mat::getStdAllocator(); + return Mat::getDefaultAllocator(); } void swap( UMat& a, UMat& b ) @@ -286,7 +286,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const accessFlags |= ACCESS_RW; UMatData* new_u = NULL; { - MatAllocator *a = allocator, *a0 = getStdAllocator(); + MatAllocator *a = allocator, *a0 = getDefaultAllocator(); if(!a) a = a0; new_u = a->allocate(dims, size.p, type(), data, step.p, accessFlags, usageFlags); @@ -302,7 +302,7 @@ UMat Mat::getUMat(int accessFlags, UMatUsageFlags usageFlags) const } if (!allocated) { - allocated = getStdAllocator()->allocate(new_u, accessFlags, usageFlags); + allocated = getDefaultAllocator()->allocate(new_u, accessFlags, usageFlags); CV_Assert(allocated); } if (u != NULL) @@ -345,6 +345,14 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag return; } + int _sizes_backup[CV_MAX_DIM]; // #5991 + if (_sizes == (this->size.p)) + { + for(i = 0; i < d; i++ ) + _sizes_backup[i] = _sizes[i]; + _sizes = _sizes_backup; + } + release(); if( d == 0 ) return; @@ -358,7 +366,7 @@ void UMat::create(int d, const int* _sizes, int _type, UMatUsageFlags _usageFlag if (!a) { a = a0; - a0 = Mat::getStdAllocator(); + a0 = Mat::getDefaultAllocator(); } try { diff --git a/modules/core/test/ocl/test_arithm.cpp b/modules/core/test/ocl/test_arithm.cpp index f1efe9b22..85dca9649 100644 --- a/modules/core/test/ocl/test_arithm.cpp +++ b/modules/core/test/ocl/test_arithm.cpp @@ -1885,6 +1885,22 @@ OCL_INSTANTIATE_TEST_CASE_P(Arithm, ReduceMin, Combine(testing::Values(std::make OCL_ALL_CHANNELS, testing::Values(0, 1), Bool())); +// T-API BUG (haveOpenCL() is false): modules/core/src/matrix.cpp:212: error: (-215) u->refcount == 0 in function deallocate +OCL_TEST(Normalize, DISABLED_regression_5876_inplace_change_type) +{ + double initial_values[] = {1, 2, 5, 4, 3}; + float result_values[] = {0, 0.25, 1, 0.75, 0.5}; + Mat m(Size(5, 1), CV_64FC1, initial_values); + Mat result(Size(5, 1), CV_32FC1, result_values); + + UMat um; m.copyTo(um); + UMat uresult; result.copyTo(uresult); + + OCL_ON(normalize(um, um, 1, 0, NORM_MINMAX, CV_32F)); + + EXPECT_EQ(0, cvtest::norm(um, uresult, NORM_INF)); +} + } } // namespace cvtest::ocl #endif // HAVE_OPENCL diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 0471ffc0b..ace7950a6 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -1833,3 +1833,14 @@ TEST(MinMaxLoc, Mat_IntMax_Without_Mask) ASSERT_EQ(Point(0, 0), minLoc); ASSERT_EQ(Point(0, 0), maxLoc); } + +TEST(Normalize, regression_5876_inplace_change_type) +{ + double initial_values[] = {1, 2, 5, 4, 3}; + float result_values[] = {0, 0.25, 1, 0.75, 0.5}; + Mat m(Size(5, 1), CV_64FC1, initial_values); + Mat result(Size(5, 1), CV_32FC1, result_values); + + normalize(m, m, 1, 0, NORM_MINMAX, CV_32F); + EXPECT_EQ(0, cvtest::norm(m, result, NORM_INF)); +} diff --git a/modules/core/test/test_hal_core.cpp b/modules/core/test/test_hal_core.cpp index 3dd0a4c43..dfd0867de 100644 --- a/modules/core/test/test_hal_core.cpp +++ b/modules/core/test/test_hal_core.cpp @@ -40,7 +40,6 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/hal.hpp" using namespace cv; @@ -72,21 +71,21 @@ TEST(Core_HAL, mathfuncs) { case HAL_EXP: if( depth == CV_32F ) - hal::exp(src.ptr(), dst.ptr(), n); + hal::exp32f(src.ptr(), dst.ptr(), n); else - hal::exp(src.ptr(), dst.ptr(), n); + hal::exp64f(src.ptr(), dst.ptr(), n); break; case HAL_LOG: if( depth == CV_32F ) - hal::log(src.ptr(), dst.ptr(), n); + hal::log32f(src.ptr(), dst.ptr(), n); else - hal::log(src.ptr(), dst.ptr(), n); + hal::log64f(src.ptr(), dst.ptr(), n); break; case HAL_SQRT: if( depth == CV_32F ) - hal::sqrt(src.ptr(), dst.ptr(), n); + hal::sqrt32f(src.ptr(), dst.ptr(), n); else - hal::sqrt(src.ptr(), dst.ptr(), n); + hal::sqrt64f(src.ptr(), dst.ptr(), n); break; default: CV_Error(Error::StsBadArg, "unknown function"); @@ -159,15 +158,15 @@ TEST(Core_HAL, mat_decomp) { case HAL_LU: if( depth == CV_32F ) - hal::LU(a.ptr(), a.step, size, x.ptr(), x.step, 1); + hal::LU32f(a.ptr(), a.step, size, x.ptr(), x.step, 1); else - hal::LU(a.ptr(), a.step, size, x.ptr(), x.step, 1); + hal::LU64f(a.ptr(), a.step, size, x.ptr(), x.step, 1); break; case HAL_CHOL: if( depth == CV_32F ) - hal::Cholesky(a.ptr(), a.step, size, x.ptr(), x.step, 1); + hal::Cholesky32f(a.ptr(), a.step, size, x.ptr(), x.step, 1); else - hal::Cholesky(a.ptr(), a.step, size, x.ptr(), x.step, 1); + hal::Cholesky64f(a.ptr(), a.step, size, x.ptr(), x.step, 1); break; default: CV_Error(Error::StsBadArg, "unknown function"); diff --git a/modules/hal/test/test_intrin.cpp b/modules/core/test/test_intrin.cpp similarity index 100% rename from modules/hal/test/test_intrin.cpp rename to modules/core/test/test_intrin.cpp diff --git a/modules/hal/test/test_intrin_utils.hpp b/modules/core/test/test_intrin_utils.hpp similarity index 99% rename from modules/hal/test/test_intrin_utils.hpp rename to modules/core/test/test_intrin_utils.hpp index 47473ae46..a0eab56a5 100644 --- a/modules/hal/test/test_intrin_utils.hpp +++ b/modules/core/test/test_intrin_utils.hpp @@ -1,7 +1,7 @@ #ifndef _TEST_UTILS_HPP_ #define _TEST_UTILS_HPP_ -#include "opencv2/hal/intrin.hpp" +#include "opencv2/core/hal/intrin.hpp" #include "opencv2/ts.hpp" #include #include diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 2344df28a..e2de3249b 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1472,3 +1472,14 @@ TEST(Core_Mat_vector, copyTo_roi_row) EXPECT_EQ(4, (int)dst2[3]); EXPECT_EQ(5, (int)dst2[4]); } + +TEST(Mat, regression_5991) +{ + int sz[] = {2,3,2}; + Mat mat(3, sz, CV_32F, Scalar(1)); + ASSERT_NO_THROW(mat.convertTo(mat, CV_8U)); + EXPECT_EQ(sz[0], mat.size[0]); + EXPECT_EQ(sz[1], mat.size[1]); + EXPECT_EQ(sz[2], mat.size[2]); + EXPECT_EQ(0, cvtest::norm(mat, Mat(3, sz, CV_8U, Scalar(1)), NORM_INF)); +} diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index b7d660541..65e3861ed 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -2502,40 +2502,25 @@ protected: } }; -class Core_CheckRange_Empty : public cvtest::BaseTest -{ -public: - Core_CheckRange_Empty(){} - ~Core_CheckRange_Empty(){} -protected: - virtual void run( int start_from ); -}; - -void Core_CheckRange_Empty::run( int ) +TEST(Core_CheckRange_Empty, accuracy) { cv::Mat m; ASSERT_TRUE( cv::checkRange(m) ); } -TEST(Core_CheckRange_Empty, accuracy) { Core_CheckRange_Empty test; test.safe_run(); } - -class Core_CheckRange_INT_MAX : public cvtest::BaseTest -{ -public: - Core_CheckRange_INT_MAX(){} - ~Core_CheckRange_INT_MAX(){} -protected: - virtual void run( int start_from ); -}; - -void Core_CheckRange_INT_MAX::run( int ) +TEST(Core_CheckRange_INT_MAX, accuracy) { cv::Mat m(3, 3, CV_32SC1, cv::Scalar(INT_MAX)); ASSERT_FALSE( cv::checkRange(m, true, 0, 0, INT_MAX) ); ASSERT_TRUE( cv::checkRange(m) ); } -TEST(Core_CheckRange_INT_MAX, accuracy) { Core_CheckRange_INT_MAX test; test.safe_run(); } +TEST(Core_CheckRange_INT_MAX1, accuracy) +{ + cv::Mat m(3, 3, CV_32SC1, cv::Scalar(INT_MAX)); + ASSERT_TRUE( cv::checkRange(m, true, 0, 0, INT_MAX+1.0f) ); + ASSERT_TRUE( cv::checkRange(m) ); +} template class Core_CheckRange : public testing::Test {}; @@ -2546,13 +2531,30 @@ TYPED_TEST_P(Core_CheckRange, Negative) double min_bound = 4.5; double max_bound = 16.0; - TypeParam data[] = {5, 10, 15, 4, 10, 2, 8, 12, 14}; + TypeParam data[] = {5, 10, 15, 10, 10, 2, 8, 12, 14}; cv::Mat src = cv::Mat(3,3, cv::DataDepth::value, data); cv::Point bad_pt(0, 0); ASSERT_FALSE(checkRange(src, true, &bad_pt, min_bound, max_bound)); - ASSERT_EQ(bad_pt.x, 0); + ASSERT_EQ(bad_pt.x, 2); + ASSERT_EQ(bad_pt.y, 1); +} + +TYPED_TEST_P(Core_CheckRange, Negative3CN) +{ + double min_bound = 4.5; + double max_bound = 16.0; + + TypeParam data[] = { 5, 6, 7, 10, 11, 12, 13, 14, 15, + 10, 11, 12, 10, 11, 12, 2, 5, 6, + 8, 8, 8, 12, 12, 12, 14, 14, 14}; + cv::Mat src = cv::Mat(3,3, CV_MAKETYPE(cv::DataDepth::value, 3), data); + + cv::Point bad_pt(0, 0); + + ASSERT_FALSE(checkRange(src, true, &bad_pt, min_bound, max_bound)); + ASSERT_EQ(bad_pt.x, 2); ASSERT_EQ(bad_pt.y, 1); } @@ -2614,7 +2616,49 @@ TYPED_TEST_P(Core_CheckRange, One) ASSERT_TRUE( checkRange(src2, true, NULL, min_bound, max_bound) ); } -REGISTER_TYPED_TEST_CASE_P(Core_CheckRange, Negative, Positive, Bounds, Zero, One); +TEST(Core_CheckRange, NaN) +{ + float data[] = { 5, 6, 7, 10, 11, 12, 13, 14, 15, + 10, 11, 12, 10, 11, 12, 5, 5, std::numeric_limits::quiet_NaN(), + 8, 8, 8, 12, 12, 12, 14, 14, 14}; + cv::Mat src = cv::Mat(3,3, CV_32FC3, data); + + cv::Point bad_pt(0, 0); + + ASSERT_FALSE(checkRange(src, true, &bad_pt)); + ASSERT_EQ(bad_pt.x, 2); + ASSERT_EQ(bad_pt.y, 1); +} + +TEST(Core_CheckRange, Inf) +{ + float data[] = { 5, 6, 7, 10, 11, 12, 13, 14, 15, + 10, 11, 12, 10, 11, 12, 5, 5, std::numeric_limits::infinity(), + 8, 8, 8, 12, 12, 12, 14, 14, 14}; + cv::Mat src = cv::Mat(3,3, CV_32FC3, data); + + cv::Point bad_pt(0, 0); + + ASSERT_FALSE(checkRange(src, true, &bad_pt)); + ASSERT_EQ(bad_pt.x, 2); + ASSERT_EQ(bad_pt.y, 1); +} + +TEST(Core_CheckRange, Inf_Minus) +{ + float data[] = { 5, 6, 7, 10, 11, 12, 13, 14, 15, + 10, 11, 12, 10, 11, 12, 5, 5, -std::numeric_limits::infinity(), + 8, 8, 8, 12, 12, 12, 14, 14, 14}; + cv::Mat src = cv::Mat(3,3, CV_32FC3, data); + + cv::Point bad_pt(0, 0); + + ASSERT_FALSE(checkRange(src, true, &bad_pt)); + ASSERT_EQ(bad_pt.x, 2); + ASSERT_EQ(bad_pt.y, 1); +} + +REGISTER_TYPED_TEST_CASE_P(Core_CheckRange, Negative, Negative3CN, Positive, Bounds, Zero, One); typedef ::testing::Types mat_data_types; INSTANTIATE_TYPED_TEST_CASE_P(Negative_Test, Core_CheckRange, mat_data_types); diff --git a/modules/core/test/test_precomp.hpp b/modules/core/test/test_precomp.hpp index d981cea06..962348b04 100644 --- a/modules/core/test/test_precomp.hpp +++ b/modules/core/test/test_precomp.hpp @@ -13,6 +13,9 @@ #include "opencv2/ts.hpp" #include "opencv2/core/core_c.h" +#include "opencv2/core/cvdef.h" #include "opencv2/core/private.hpp" +#include "opencv2/core/hal/hal.hpp" +#include "opencv2/core/hal/intrin.hpp" #endif diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index a8641d098..26b1bbd2b 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -1035,7 +1035,7 @@ TEST(UMat, synchronization_map_unmap) }; try { - UMat u(1000, 1000, CV_32FC1); + UMat u(1000, 1000, CV_32FC1, Scalar::all(0)); parallel_for_(cv::Range(0, 2), TestParallelLoopBody(u)); } catch (const cv::Exception& e) @@ -1056,7 +1056,7 @@ TEST(UMat, async_unmap) { try { - Mat m = Mat(1000, 1000, CV_8UC1); + Mat m = Mat(1000, 1000, CV_8UC1, Scalar::all(0)); UMat u = m.getUMat(ACCESS_READ); UMat dst; add(u, Scalar::all(0), dst); // start async operation @@ -1101,7 +1101,7 @@ TEST(UMat, unmap_in_class) }; try { - Mat m = Mat(1000, 1000, CV_8UC1); + Mat m = Mat(1000, 1000, CV_8UC1, Scalar::all(0)); Logic l; l.processData(m); UMat result = l.getResult(); @@ -1127,7 +1127,7 @@ TEST(UMat, map_unmap_counting) return; } std::cout << "Host memory: " << cv::ocl::Device::getDefault().hostUnifiedMemory() << std::endl; - Mat m(Size(10, 10), CV_8UC1); + Mat m(Size(10, 10), CV_8UC1, Scalar::all(0)); UMat um = m.getUMat(ACCESS_RW); { Mat d1 = um.getMat(ACCESS_RW); @@ -1156,7 +1156,7 @@ OCL_TEST(UMat, DISABLED_OCL_ThreadSafe_CleanupCallback_1_VeryLongTest) const int type = CV_8UC1; const int dtype = CV_16UC1; - Mat src(srcSize, type); + Mat src(srcSize, type, Scalar::all(0)); Mat dst_ref(srcSize, dtype); // Generate reference data as additional check @@ -1198,7 +1198,7 @@ OCL_TEST(UMat, DISABLED_OCL_ThreadSafe_CleanupCallback_2_VeryLongTest) // Use multiple iterations to increase chance of data race catching for(int k = 0; k < 10000; k++) { - Mat src(srcSize, type); // Declare src inside loop now to catch its destruction on stack + Mat src(srcSize, type, Scalar::all(0)); // Declare src inside loop now to catch its destruction on stack { UMat tmpUMat = src.getUMat(ACCESS_RW); tmpUMat.convertTo(dst, dtype); @@ -1216,7 +1216,7 @@ TEST(UMat, DISABLED_Test_same_behaviour_read_and_read) bool exceptionDetected = false; try { - UMat u(Size(10, 10), CV_8UC1); + UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_READ); UMat dst; add(u, Scalar::all(1), dst); @@ -1234,7 +1234,7 @@ TEST(UMat, DISABLED_Test_same_behaviour_read_and_write) bool exceptionDetected = false; try { - UMat u(Size(10, 10), CV_8UC1); + UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_READ); add(u, Scalar::all(1), u); } @@ -1250,7 +1250,7 @@ TEST(UMat, DISABLED_Test_same_behaviour_write_and_read) bool exceptionDetected = false; try { - UMat u(Size(10, 10), CV_8UC1); + UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_WRITE); UMat dst; add(u, Scalar::all(1), dst); @@ -1267,7 +1267,7 @@ TEST(UMat, DISABLED_Test_same_behaviour_write_and_write) bool exceptionDetected = false; try { - UMat u(Size(10, 10), CV_8UC1); + UMat u(Size(10, 10), CV_8UC1, Scalar::all(0)); Mat m = u.getMat(ACCESS_WRITE); add(u, Scalar::all(1), u); } @@ -1343,4 +1343,15 @@ TEST(UMat, testWrongLifetime_Mat) } } +TEST(UMat, DISABLED_regression_5991) +{ + int sz[] = {2,3,2}; + UMat mat(3, sz, CV_32F, Scalar(1)); + ASSERT_NO_THROW(mat.convertTo(mat, CV_8U)); + EXPECT_EQ(sz[0], mat.size[0]); + EXPECT_EQ(sz[1], mat.size[1]); + EXPECT_EQ(sz[2], mat.size[2]); + EXPECT_EQ(0, cvtest::norm(mat.getMat(ACCESS_READ), Mat(3, sz, CV_8U, Scalar(1)), NORM_INF)); +} + } } // namespace cvtest::ocl diff --git a/modules/cudaarithm/perf/perf_reductions.cpp b/modules/cudaarithm/perf/perf_reductions.cpp index 78699c0a7..54c514737 100644 --- a/modules/cudaarithm/perf/perf_reductions.cpp +++ b/modules/cudaarithm/perf/perf_reductions.cpp @@ -368,6 +368,8 @@ PERF_TEST_P(Sz_Depth_Cn_Code_Dim, Reduce, TEST_CYCLE() cv::cuda::reduce(d_src, dst, dim, reduceOp, CV_32F); + dst = dst.reshape(dst.channels(), 1); + CUDA_SANITY_CHECK(dst); } else diff --git a/modules/cudaarithm/src/cuda/reduce.cu b/modules/cudaarithm/src/cuda/reduce.cu index 5fb90287a..3f907c795 100644 --- a/modules/cudaarithm/src/cuda/reduce.cu +++ b/modules/cudaarithm/src/cuda/reduce.cu @@ -137,7 +137,7 @@ void cv::cuda::reduce(InputArray _src, OutputArray _dst, int dim, int reduceOp, if (dtype < 0) dtype = src.depth(); - GpuMat dst = getOutputMat(_dst, 1, dim == 0 ? src.cols : src.rows, CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()), stream); + GpuMat dst = getOutputMat(_dst, dim == 0 ? 1 : src.rows, dim == 0 ? src.cols : 1, CV_MAKE_TYPE(CV_MAT_DEPTH(dtype), src.channels()), stream); if (dim == 0) { diff --git a/modules/cudaarithm/test/test_gpumat.cpp b/modules/cudaarithm/test/test_gpumat.cpp index 3f63352b8..d3c4bbbad 100644 --- a/modules/cudaarithm/test/test_gpumat.cpp +++ b/modules/cudaarithm/test/test_gpumat.cpp @@ -361,4 +361,51 @@ CUDA_TEST_P(EnsureSizeIsEnough, BufferReuse) INSTANTIATE_TEST_CASE_P(CUDA, EnsureSizeIsEnough, ALL_DEVICES); +//////////////////////////////////////////////////////////////////////////////// +// createContinuous + +struct CreateContinuous : testing::TestWithParam +{ + virtual void SetUp() + { + cv::cuda::DeviceInfo devInfo = GetParam(); + cv::cuda::setDevice(devInfo.deviceID()); + } +}; + +CUDA_TEST_P(CreateContinuous, BufferReuse) +{ + cv::cuda::GpuMat buffer; + + cv::cuda::createContinuous(100, 100, CV_8UC1, buffer); + EXPECT_EQ(100, buffer.rows); + EXPECT_EQ(100, buffer.cols); + EXPECT_EQ(CV_8UC1, buffer.type()); + EXPECT_TRUE(buffer.isContinuous()); + EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step); + + cv::cuda::createContinuous(10, 1000, CV_8UC1, buffer); + EXPECT_EQ(10, buffer.rows); + EXPECT_EQ(1000, buffer.cols); + EXPECT_EQ(CV_8UC1, buffer.type()); + EXPECT_TRUE(buffer.isContinuous()); + EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step); + + cv::cuda::createContinuous(10, 10, CV_8UC1, buffer); + EXPECT_EQ(10, buffer.rows); + EXPECT_EQ(10, buffer.cols); + EXPECT_EQ(CV_8UC1, buffer.type()); + EXPECT_TRUE(buffer.isContinuous()); + EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step); + + cv::cuda::createContinuous(100, 100, CV_8UC1, buffer); + EXPECT_EQ(100, buffer.rows); + EXPECT_EQ(100, buffer.cols); + EXPECT_EQ(CV_8UC1, buffer.type()); + EXPECT_TRUE(buffer.isContinuous()); + EXPECT_EQ(buffer.cols * sizeof(uchar), buffer.step); +} + +INSTANTIATE_TEST_CASE_P(CUDA, CreateContinuous, ALL_DEVICES); + #endif // HAVE_CUDA diff --git a/modules/cudaarithm/test/test_reductions.cpp b/modules/cudaarithm/test/test_reductions.cpp index a0ff0dfa4..48abdfe40 100644 --- a/modules/cudaarithm/test/test_reductions.cpp +++ b/modules/cudaarithm/test/test_reductions.cpp @@ -877,14 +877,11 @@ CUDA_TEST_P(Reduce, Cols) { cv::Mat src = randomMat(size, type); - cv::cuda::GpuMat dst = createMat(cv::Size(src.rows, 1), dst_type, useRoi); + cv::cuda::GpuMat dst; cv::cuda::reduce(loadMat(src, useRoi), dst, 1, reduceOp, dst_depth); cv::Mat dst_gold; cv::reduce(src, dst_gold, 1, reduceOp, dst_depth); - dst_gold.cols = dst_gold.rows; - dst_gold.rows = 1; - dst_gold.step = dst_gold.cols * dst_gold.elemSize(); EXPECT_MAT_NEAR(dst_gold, dst, dst_depth < CV_32F ? 0.0 : 0.02); } diff --git a/modules/cudaarithm/test/test_stream.cpp b/modules/cudaarithm/test/test_stream.cpp index c9a5e694f..785b10e74 100644 --- a/modules/cudaarithm/test/test_stream.cpp +++ b/modules/cudaarithm/test/test_stream.cpp @@ -47,6 +47,7 @@ #include #include "opencv2/core/cuda.hpp" +#include "opencv2/core/cuda_stream_accessor.hpp" #include "opencv2/ts/cuda_test.hpp" using namespace cvtest; @@ -129,6 +130,27 @@ CUDA_TEST_P(Async, Convert) stream.waitForCompletion(); } +CUDA_TEST_P(Async, WrapStream) +{ + cudaStream_t cuda_stream = NULL; + ASSERT_EQ(cudaSuccess, cudaStreamCreate(&cuda_stream)); + + { + cv::cuda::Stream stream = cv::cuda::StreamAccessor::wrapStream(cuda_stream); + + d_src.upload(src, stream); + d_src.convertTo(d_dst, CV_32S, stream); + d_dst.download(dst, stream); + + Async* test = this; + stream.enqueueHostCallback(checkConvert, test); + + stream.waitForCompletion(); + } + + ASSERT_EQ(cudaSuccess, cudaStreamDestroy(cuda_stream)); +} + CUDA_TEST_P(Async, HostMemAllocator) { cv::cuda::Stream stream; diff --git a/modules/cudacodec/CMakeLists.txt b/modules/cudacodec/CMakeLists.txt index f2e955b6c..6aecd26db 100644 --- a/modules/cudacodec/CMakeLists.txt +++ b/modules/cudacodec/CMakeLists.txt @@ -15,7 +15,9 @@ set(extra_libs "") if(HAVE_NVCUVID) list(APPEND extra_libs ${CUDA_CUDA_LIBRARY} ${CUDA_nvcuvid_LIBRARY}) +endif() +if(HAVE_NVCUVENC) if(WIN32) list(APPEND extra_libs ${CUDA_nvcuvenc_LIBRARY}) endif() diff --git a/modules/cudacodec/src/precomp.hpp b/modules/cudacodec/src/precomp.hpp index 80257a0cd..59504c2c4 100644 --- a/modules/cudacodec/src/precomp.hpp +++ b/modules/cudacodec/src/precomp.hpp @@ -61,7 +61,9 @@ #ifdef WIN32 #define NOMINMAX #include - #include + #ifdef HAVE_NVCUVENC + #include + #endif #else #include #include diff --git a/modules/cudacodec/src/video_writer.cpp b/modules/cudacodec/src/video_writer.cpp index 6faa9d3bf..f6b9fc738 100644 --- a/modules/cudacodec/src/video_writer.cpp +++ b/modules/cudacodec/src/video_writer.cpp @@ -47,7 +47,7 @@ using namespace cv; using namespace cv::cuda; using namespace cv::cudacodec; -#if !defined(HAVE_NVCUVID) || !defined(WIN32) +#if !defined(HAVE_NVCUVENC) || !defined(WIN32) cv::cudacodec::EncoderParams::EncoderParams() { throw_no_cuda(); } cv::cudacodec::EncoderParams::EncoderParams(const String&) { throw_no_cuda(); } @@ -60,7 +60,7 @@ Ptr cv::cudacodec::createVideoWriter(const String&, Size, double, c Ptr cv::cudacodec::createVideoWriter(const Ptr&, Size, double, SurfaceFormat) { throw_no_cuda(); return Ptr(); } Ptr cv::cudacodec::createVideoWriter(const Ptr&, Size, double, const EncoderParams&, SurfaceFormat) { throw_no_cuda(); return Ptr(); } -#else // !defined HAVE_CUDA || !defined WIN32 +#else // !defined HAVE_NVCUVENC || !defined WIN32 void RGB_to_YV12(const GpuMat& src, GpuMat& dst); @@ -913,4 +913,4 @@ Ptr cv::cudacodec::createVideoWriter(const Ptr& en return makePtr(encoderCallback, frameSize, fps, params, format); } -#endif // !defined HAVE_CUDA || !defined WIN32 +#endif // !defined HAVE_NVCUVENC || !defined WIN32 diff --git a/modules/cudafilters/include/opencv2/cudafilters.hpp b/modules/cudafilters/include/opencv2/cudafilters.hpp index 9e86cc3a7..2d5226561 100644 --- a/modules/cudafilters/include/opencv2/cudafilters.hpp +++ b/modules/cudafilters/include/opencv2/cudafilters.hpp @@ -314,6 +314,18 @@ CV_EXPORTS Ptr createColumnSumFilter(int srcType, int dstType, int ksize //! @} +///////////////////////////// Median Filtering ////////////////////////////// + +/** @brief Performs median filtering for each point of the source image. + +@param srcType type of of source image. Only CV_8UC1 images are supported for now. +@param windowSize Size of the kernerl used for the filtering. Uses a (windowSize x windowSize) filter. +@param partition Specifies the parallel granularity of the workload. This parameter should be used GPU experts when optimizing performance. + +Outputs an image that has been filtered using median-filtering formulation. + */ +CV_EXPORTS Ptr createMedianFilter(int srcType, int windowSize, int partition=128); + }} // namespace cv { namespace cuda { #endif /* __OPENCV_CUDAFILTERS_HPP__ */ diff --git a/modules/cudafilters/perf/perf_filters.cpp b/modules/cudafilters/perf/perf_filters.cpp index e6bb200bb..63abf6df7 100644 --- a/modules/cudafilters/perf/perf_filters.cpp +++ b/modules/cudafilters/perf/perf_filters.cpp @@ -375,3 +375,43 @@ PERF_TEST_P(Sz_Type_Op, MorphologyEx, Combine(CUDA_TYPICAL_MAT_SIZES, Values(CV_ CPU_SANITY_CHECK(dst); } } +////////////////////////////////////////////////////////////////////// +// MedianFilter +////////////////////////////////////////////////////////////////////// +// Median + +DEF_PARAM_TEST(Sz_KernelSz, cv::Size, int); + +//PERF_TEST_P(Sz_Type_KernelSz, Median, Combine(CUDA_TYPICAL_MAT_SIZES, Values(CV_8UC1,CV_8UC1), Values(3, 5, 7, 9, 11, 13, 15))) +PERF_TEST_P(Sz_KernelSz, Median, Combine(CUDA_TYPICAL_MAT_SIZES, Values(3, 5, 7, 9, 11, 13, 15))) +{ + declare.time(20.0); + + const cv::Size size = GET_PARAM(0); + // const int type = GET_PARAM(1); + const int type = CV_8UC1; + const int kernel = GET_PARAM(1); + + cv::Mat src(size, type); + declare.in(src, WARMUP_RNG); + + if (PERF_RUN_CUDA()) + { + const cv::cuda::GpuMat d_src(src); + cv::cuda::GpuMat dst; + + cv::Ptr median = cv::cuda::createMedianFilter(d_src.type(), kernel); + + TEST_CYCLE() median->apply(d_src, dst); + + SANITY_CHECK_NOTHING(); + } + else + { + cv::Mat dst; + + TEST_CYCLE() cv::medianBlur(src,dst,kernel); + + SANITY_CHECK_NOTHING(); + } +} \ No newline at end of file diff --git a/modules/cudafilters/src/cuda/median_filter.cu b/modules/cudafilters/src/cuda/median_filter.cu new file mode 100644 index 000000000..f66b429a3 --- /dev/null +++ b/modules/cudafilters/src/cuda/median_filter.cu @@ -0,0 +1,348 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#if !defined CUDA_DISABLER + +#include "precomp.hpp" + +using namespace cv; +using namespace cv::cuda; + +#include "opencv2/core/cuda/common.hpp" +#include "opencv2/core/cuda/vec_traits.hpp" +#include "opencv2/core/cuda/vec_math.hpp" +#include "opencv2/core/cuda/saturate_cast.hpp" +#include "opencv2/core/cuda/border_interpolate.hpp" + +namespace cv { namespace cuda { namespace device +{ + // // namespace imgproc + // { + + __device__ void histogramAddAndSub8(int* H, const int * hist_colAdd,const int * hist_colSub){ + int tx = threadIdx.x; + if (tx<8){ + H[tx]+=hist_colAdd[tx]-hist_colSub[tx]; + } + } + + __device__ void histogramMultipleAdd8(int* H, const int * hist_col,int histCount){ + int tx = threadIdx.x; + if (tx<8){ + int temp=H[tx]; + for(int i=0; i=1 ) + Hscan[tx]+=Hscan[tx-1]; + if(tx>=2) + Hscan[tx]+=Hscan[tx-2]; + if(tx>=4) + Hscan[tx]+=Hscan[tx-4]; + } + __syncthreads(); + + if(tx<7){ + if(Hscan[tx+1] > medPos && Hscan[tx] < medPos){ + *retval=tx+1; + *countAtMed=Hscan[tx]; + } + else if(Hscan[tx]==medPos){ + if(Hscan[tx+1]>medPos){ + *retval=tx+1; + *countAtMed=Hscan[tx]; + } + } + } + } + + __device__ void histogramMedianPar32LookupOnly(int* H,int* Hscan, const int medPos,int* retval, int* countAtMed){ + int tx=threadIdx.x; + *retval=*countAtMed=0; + if(tx<32){ + Hscan[tx]=H[tx]; + } + __syncthreads(); + if(tx<32){ + if(tx>=1) + Hscan[tx]+=Hscan[tx-1]; + if(tx>=2) + Hscan[tx]+=Hscan[tx-2]; + if(tx>=4) + Hscan[tx]+=Hscan[tx-4]; + if(tx>=8) + Hscan[tx]+=Hscan[tx-8]; + if(tx>=16) + Hscan[tx]+=Hscan[tx-16]; + } + __syncthreads(); + if(tx<31){ + if(Hscan[tx+1] > medPos && Hscan[tx] < medPos){ + *retval=tx+1; + *countAtMed=Hscan[tx]; + } + else if(Hscan[tx]==medPos){ + if(Hscan[tx+1]>medPos){ + *retval=tx+1; + *countAtMed=Hscan[tx]; + } + } + } + } + + __global__ void cuMedianFilterMultiBlock(PtrStepSzb src, PtrStepSzb dest, PtrStepSzi histPar, PtrStepSzi coarseHistGrid,int r, int medPos_) + { + __shared__ int HCoarse[8]; + __shared__ int HCoarseScan[32]; + __shared__ int HFine[8][32]; + + __shared__ int luc[8]; + + __shared__ int firstBin,countAtMed, retval; + + int rows = src.rows, cols=src.cols; + + int extraRowThread=rows%gridDim.x; + int doExtraRow=blockIdx.x>5)]=initVal; + } + } + __syncthreads(); + + // Fot all remaining rows in the median filter, add the values to the the histogram + for (int j=threadIdx.x; j>5)]++; + } + } + __syncthreads(); + // Going through all the rows that the block is responsible for. + int inc=blockDim.x*256; + int incCoarse=blockDim.x*8; + for(int i=startRow; i< stopRow; i++){ + // For every new row that is started the global histogram for the entire window is restarted. + + histogramClear8(HCoarse); + lucClear8(luc); + // Computing some necessary indices + int possub=::max(0,i-r-1),posadd=::min(rows-1,i+r); + int histPos=threadIdx.x*256; + int histCoarsePos=threadIdx.x*8; + // Going through all the elements of a specific row. Foeach histogram, a value is taken out and + // one value is added. + for (int j=threadIdx.x; j>5) ]--; + histCoarse[histCoarsePos+ (src.ptr(posadd)[j]>>5) ]++; + + histPos+=inc; + histCoarsePos+=incCoarse; + } + __syncthreads(); + + histogramMultipleAdd8(HCoarse,histCoarse, 2*r+1); +// __syncthreads(); + int cols_m_1=cols-1; + + for(int j=r;j=0){ + histogramMedianPar32LookupOnly(HFine[firstBin],HCoarseScan,leftOver,&retval,&countAtMed); + } + else retval=0; + __syncthreads(); + + if (threadIdx.x==0){ + dest.ptr(i)[j]=(firstBin<<5) + retval; + } + histogramAddAndSub8(HCoarse, histCoarse+(int)(posadd<<3),histCoarse+(int)(possub<<3)); + + __syncthreads(); + } + __syncthreads(); + } + } + + void medianFiltering_gpu(const PtrStepSzb src, PtrStepSzb dst, PtrStepSzi devHist, PtrStepSzi devCoarseHist,int kernel, int partitions,cudaStream_t stream){ + int medPos=2*kernel*kernel+2*kernel; + dim3 gridDim; gridDim.x=partitions; + dim3 blockDim; blockDim.x=32; + cuMedianFilterMultiBlock<<>>(src, dst, devHist,devCoarseHist, kernel, medPos); + if (!stream) + cudaSafeCall( cudaDeviceSynchronize() ); + } + +}}} + +#endif diff --git a/modules/cudafilters/src/filtering.cpp b/modules/cudafilters/src/filtering.cpp index ed72a3ab5..b90a8c193 100644 --- a/modules/cudafilters/src/filtering.cpp +++ b/modules/cudafilters/src/filtering.cpp @@ -69,6 +69,8 @@ Ptr cv::cuda::createBoxMinFilter(int, Size, Point, int, Scalar) { throw_ Ptr cv::cuda::createRowSumFilter(int, int, int, int, int, Scalar) { throw_no_cuda(); return Ptr(); } Ptr cv::cuda::createColumnSumFilter(int, int, int, int, int, Scalar) { throw_no_cuda(); return Ptr(); } +Ptr cv::cuda::createMedianFilter(int srcType, int _windowSize, int _partitions){ throw_no_cuda(); return Ptr();} + #else namespace @@ -995,4 +997,74 @@ Ptr cv::cuda::createColumnSumFilter(int srcType, int dstType, int ksize, return makePtr(srcType, dstType, ksize, anchor, borderMode, borderVal); } +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Median Filter + + + +namespace cv { namespace cuda { namespace device +{ + void medianFiltering_gpu(const PtrStepSzb src, PtrStepSzb dst, PtrStepSzi devHist, + PtrStepSzi devCoarseHist,int kernel, int partitions, cudaStream_t stream); +}}} + +namespace +{ + class MedianFilter : public Filter + { + public: + MedianFilter(int srcType, int _windowSize, int _partitions=128); + + void apply(InputArray src, OutputArray dst, Stream& stream = Stream::Null()); + + private: + int windowSize; + int partitions; + }; + + MedianFilter::MedianFilter(int srcType, int _windowSize, int _partitions) : + windowSize(_windowSize),partitions(_partitions) + { + CV_Assert( srcType == CV_8UC1 ); + CV_Assert(windowSize>=3); + CV_Assert(_partitions>=1); + + } + + void MedianFilter::apply(InputArray _src, OutputArray _dst, Stream& _stream) + { + using namespace cv::cuda::device; + + GpuMat src = _src.getGpuMat(); + _dst.create(src.rows, src.cols, src.type()); + GpuMat dst = _dst.getGpuMat(); + + if (partitions>src.rows) + partitions=src.rows/2; + + // Kernel needs to be half window size + int kernel=windowSize/2; + + CV_Assert(kernel < src.rows); + CV_Assert(kernel < src.cols); + + // Note - these are hardcoded in the actual GPU kernel. Do not change these values. + int histSize=256, histCoarseSize=8; + + BufferPool pool(_stream); + GpuMat devHist = pool.getBuffer(1, src.cols*histSize*partitions,CV_32SC1); + GpuMat devCoarseHist = pool.getBuffer(1,src.cols*histCoarseSize*partitions,CV_32SC1); + + devHist.setTo(0, _stream); + devCoarseHist.setTo(0, _stream); + + medianFiltering_gpu(src,dst,devHist, devCoarseHist,kernel,partitions,StreamAccessor::getStream(_stream)); + } +} + +Ptr cv::cuda::createMedianFilter(int srcType, int _windowSize, int _partitions) +{ + return makePtr(srcType, _windowSize,_partitions); +} + #endif diff --git a/modules/cudafilters/test/test_filters.cpp b/modules/cudafilters/test/test_filters.cpp index 97661b0ca..bac81c30b 100644 --- a/modules/cudafilters/test/test_filters.cpp +++ b/modules/cudafilters/test/test_filters.cpp @@ -53,6 +53,7 @@ namespace IMPLEMENT_PARAM_CLASS(Deriv_X, int) IMPLEMENT_PARAM_CLASS(Deriv_Y, int) IMPLEMENT_PARAM_CLASS(Iterations, int) + IMPLEMENT_PARAM_CLASS(KernelSize, int) cv::Mat getInnerROI(cv::InputArray m_, cv::Size ksize) { @@ -647,4 +648,64 @@ INSTANTIATE_TEST_CASE_P(CUDA_Filters, MorphEx, testing::Combine( testing::Values(Iterations(1), Iterations(2), Iterations(3)), WHOLE_SUBMAT)); +///////////////////////////////////////////////////////////////////////////////////////////////// +// Median + + +PARAM_TEST_CASE(Median, cv::cuda::DeviceInfo, cv::Size, MatDepth, KernelSize, UseRoi) +{ + cv::cuda::DeviceInfo devInfo; + cv::Size size; + int type; + int kernel; + bool useRoi; + + virtual void SetUp() + { + devInfo = GET_PARAM(0); + size = GET_PARAM(1); + type = GET_PARAM(2); + kernel = GET_PARAM(3); + useRoi = GET_PARAM(4); + + cv::cuda::setDevice(devInfo.deviceID()); + } +}; + + + +CUDA_TEST_P(Median, Accuracy) +{ + cv::Mat src = randomMat(size, type); + + cv::Ptr median = cv::cuda::createMedianFilter(src.type(), kernel); + + cv::cuda::GpuMat dst = createMat(size, type, useRoi); + median->apply(loadMat(src, useRoi), dst); + + cv::Mat dst_gold; + cv::medianBlur(src,dst_gold,kernel); + + cv::Rect rect(kernel+1,0,src.cols-(2*kernel+1),src.rows); + cv::Mat dst_gold_no_border = dst_gold(rect); + cv::cuda::GpuMat dst_no_border = cv::cuda::GpuMat(dst, rect); + + EXPECT_MAT_NEAR(dst_gold_no_border, dst_no_border, 1); + +} + +INSTANTIATE_TEST_CASE_P(CUDA_Filters, Median, testing::Combine( + ALL_DEVICES, + DIFFERENT_SIZES, + testing::Values(MatDepth(CV_8U)), + testing::Values(KernelSize(3), + KernelSize(5), + KernelSize(7), + KernelSize(9), + KernelSize(11), + KernelSize(13), + KernelSize(15)), + WHOLE_SUBMAT) + ); + #endif // HAVE_CUDA diff --git a/modules/cudaimgproc/include/opencv2/cudaimgproc.hpp b/modules/cudaimgproc/include/opencv2/cudaimgproc.hpp index d84ae2453..b4a05eecd 100644 --- a/modules/cudaimgproc/include/opencv2/cudaimgproc.hpp +++ b/modules/cudaimgproc/include/opencv2/cudaimgproc.hpp @@ -588,6 +588,7 @@ CV_EXPORTS Ptr createGoodFeaturesToTrackDetector(int srcType, i //! @} cudaimgproc_feature + ///////////////////////////// Mean Shift ////////////////////////////// /** @brief Performs mean-shift filtering for each point of the source image. diff --git a/modules/cudaobjdetect/include/opencv2/cudaobjdetect.hpp b/modules/cudaobjdetect/include/opencv2/cudaobjdetect.hpp index 8f3745cfc..f29ed5367 100644 --- a/modules/cudaobjdetect/include/opencv2/cudaobjdetect.hpp +++ b/modules/cudaobjdetect/include/opencv2/cudaobjdetect.hpp @@ -73,7 +73,7 @@ namespace cv { namespace cuda { - A CUDA example applying the HOG descriptor for people detection can be found at opencv_source_code/samples/gpu/hog.cpp - (Python) An example applying the HOG descriptor for people detection can be found at - opencv_source_code/samples/python2/peopledetect.py + opencv_source_code/samples/python/peopledetect.py */ class CV_EXPORTS HOG : public Algorithm { diff --git a/modules/cudaoptflow/perf/perf_optflow.cpp b/modules/cudaoptflow/perf/perf_optflow.cpp index 57994b7f4..d2992c30c 100644 --- a/modules/cudaoptflow/perf/perf_optflow.cpp +++ b/modules/cudaoptflow/perf/perf_optflow.cpp @@ -116,10 +116,10 @@ PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, PyrLKOpticalFlowSparse, const int levels = GET_PARAM(4); const int iters = GET_PARAM(5); - const cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); + cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); ASSERT_FALSE(frame0.empty()); - const cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); + cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); ASSERT_FALSE(frame1.empty()); cv::Mat gray_frame; @@ -131,6 +131,14 @@ PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, PyrLKOpticalFlowSparse, cv::Mat pts; cv::goodFeaturesToTrack(gray_frame, pts, points, 0.01, 0.0); + frame0.convertTo(frame0, CV_32F); + frame1.convertTo(frame1, CV_32F); + if(!useGray) + { + cv::cvtColor(frame0, frame0, cv::COLOR_BGR2BGRA); + cv::cvtColor(frame1, frame1, cv::COLOR_BGR2BGRA); + } + if (PERF_RUN_CUDA()) { const cv::cuda::GpuMat d_pts(pts.reshape(2, 1)); @@ -318,4 +326,4 @@ PERF_TEST_P(ImagePair, OpticalFlowDual_TVL1, CPU_SANITY_CHECK(flow); } -} +} \ No newline at end of file diff --git a/modules/cudaoptflow/src/cuda/pyrlk.cu b/modules/cudaoptflow/src/cuda/pyrlk.cu index 7693551fc..5d40a47ea 100644 --- a/modules/cudaoptflow/src/cuda/pyrlk.cu +++ b/modules/cudaoptflow/src/cuda/pyrlk.cu @@ -48,6 +48,8 @@ #include "opencv2/core/cuda/limits.hpp" #include "opencv2/core/cuda/vec_math.hpp" #include "opencv2/core/cuda/reduce.hpp" +#include "opencv2/core/cuda/filters.hpp" +#include "opencv2/core/cuda/border_interpolate.hpp" using namespace cv::cuda; using namespace cv::cuda::device; @@ -60,53 +62,240 @@ namespace pyrlk __constant__ int c_halfWin_y; __constant__ int c_iters; + texture tex_I8U(false, cudaFilterModeLinear, cudaAddressModeClamp); + texture tex_I8UC4(false, cudaFilterModeLinear, cudaAddressModeClamp); + + texture tex_I16UC4(false, cudaFilterModeLinear, cudaAddressModeClamp); + + texture tex_If(false, cudaFilterModeLinear, cudaAddressModeClamp); texture tex_If4(false, cudaFilterModeLinear, cudaAddressModeClamp); + texture tex_Ib(false, cudaFilterModePoint, cudaAddressModeClamp); + texture tex_J8U(false, cudaFilterModeLinear, cudaAddressModeClamp); + texture tex_J8UC4(false, cudaFilterModeLinear, cudaAddressModeClamp); + + texture tex_J16UC4(false, cudaFilterModeLinear, cudaAddressModeClamp); + + texture tex_Jf(false, cudaFilterModeLinear, cudaAddressModeClamp); texture tex_Jf4(false, cudaFilterModeLinear, cudaAddressModeClamp); - template struct Tex_I; - template <> struct Tex_I<1> + + template struct Tex_I + { + static __host__ __forceinline__ void bindTexture_(PtrStepSz::vec_type> I) + { + (void)I; + } + }; + + template <> struct Tex_I<1, uchar> + { + static __device__ __forceinline__ float read(float x, float y) + { + return tex2D(tex_I8U, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + bindTexture(&tex_I8U, I); + } + }; + template <> struct Tex_I<1, ushort> + { + static __device__ __forceinline__ float read(float x, float y) + { + return 0.0; + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + (void)I; + } + }; + template <> struct Tex_I<1, int> + { + static __device__ __forceinline__ float read(float x, float y) + { + return 0.0; + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + (void)I; + } + }; + template <> struct Tex_I<1, float> { static __device__ __forceinline__ float read(float x, float y) { return tex2D(tex_If, x, y); } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + bindTexture(&tex_If, I); + } }; - template <> struct Tex_I<4> + // ****************** 3 channel specializations ************************ + template <> struct Tex_I<3, uchar> + { + static __device__ __forceinline__ float3 read(float x, float y) + { + return make_float3(0,0,0); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz I) + { + (void)I; + } + }; + template <> struct Tex_I<3, ushort> + { + static __device__ __forceinline__ float3 read(float x, float y) + { + return make_float3(0, 0, 0); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz I) + { + (void)I; + } + }; + template <> struct Tex_I<3, int> + { + static __device__ __forceinline__ float3 read(float x, float y) + { + return make_float3(0, 0, 0); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz I) + { + (void)I; + } + }; + template <> struct Tex_I<3, float> + { + static __device__ __forceinline__ float3 read(float x, float y) + { + return make_float3(0, 0, 0); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz I) + { + (void)I; + } + }; + // ****************** 4 channel specializations ************************ + + template <> struct Tex_I<4, uchar> + { + static __device__ __forceinline__ float4 read(float x, float y) + { + return tex2D(tex_I8UC4, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + bindTexture(&tex_I8UC4, I); + } + }; + template <> struct Tex_I<4, ushort> + { + static __device__ __forceinline__ float4 read(float x, float y) + { + return tex2D(tex_I16UC4, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + bindTexture(&tex_I16UC4, I); + } + }; + template <> struct Tex_I<4, float> { static __device__ __forceinline__ float4 read(float x, float y) { return tex2D(tex_If4, x, y); } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& I) + { + bindTexture(&tex_If4, I); + } }; - - template struct Tex_J; - template <> struct Tex_J<1> + // ************* J *************** + template struct Tex_J + { + static __host__ __forceinline__ void bindTexture_(PtrStepSz::vec_type>& J) + { + (void)J; + } + }; + template <> struct Tex_J<1, uchar> + { + static __device__ __forceinline__ float read(float x, float y) + { + return tex2D(tex_J8U, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& J) + { + bindTexture(&tex_J8U, J); + } + }; + template <> struct Tex_J<1, float> { static __device__ __forceinline__ float read(float x, float y) { return tex2D(tex_Jf, x, y); } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& J) + { + bindTexture(&tex_Jf, J); + } }; - template <> struct Tex_J<4> + // ************* 4 channel specializations *************** + template <> struct Tex_J<4, uchar> + { + static __device__ __forceinline__ float4 read(float x, float y) + { + return tex2D(tex_J8UC4, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& J) + { + bindTexture(&tex_J8UC4, J); + } + }; + template <> struct Tex_J<4, ushort> + { + static __device__ __forceinline__ float4 read(float x, float y) + { + return tex2D(tex_J16UC4, x, y); + } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& J) + { + bindTexture(&tex_J16UC4, J); + } + }; + template <> struct Tex_J<4, float> { static __device__ __forceinline__ float4 read(float x, float y) { return tex2D(tex_Jf4, x, y); } + static __host__ __forceinline__ void bindTexture_(PtrStepSz& J) + { + bindTexture(&tex_Jf4, J); + } }; - __device__ __forceinline__ void accum(float& dst, float val) + __device__ __forceinline__ void accum(float& dst, const float& val) { dst += val; } - __device__ __forceinline__ void accum(float& dst, const float4& val) + __device__ __forceinline__ void accum(float& dst, const float2& val) + { + dst += val.x + val.y; + } + __device__ __forceinline__ void accum(float& dst, const float3& val) { dst += val.x + val.y + val.z; } + __device__ __forceinline__ void accum(float& dst, const float4& val) + { + dst += val.x + val.y + val.z + val.w; + } __device__ __forceinline__ float abs_(float a) { @@ -116,8 +305,46 @@ namespace pyrlk { return abs(a); } + __device__ __forceinline__ float2 abs_(const float2& a) + { + return abs(a); + } + __device__ __forceinline__ float3 abs_(const float3& a) + { + return abs(a); + } - template + + template __device__ __forceinline__ typename TypeVec::vec_type ToFloat(const typename TypeVec::vec_type& other) + { + return other; + } + template __device__ __forceinline__ typename TypeVec::vec_type ToFloat(const typename TypeVec::vec_type& other) + { + typename TypeVec::vec_type ret; + ret.x = other.x; + ret.y = other.y; + return ret; + } + template __device__ __forceinline__ typename TypeVec::vec_type ToFloat(const typename TypeVec::vec_type& other) + { + typename TypeVec::vec_type ret; + ret.x = other.x; + ret.y = other.y; + ret.z = other.z; + return ret; + } + template __device__ __forceinline__ typename TypeVec::vec_type ToFloat(const typename TypeVec::vec_type& other) + { + typename TypeVec::vec_type ret; + ret.x = other.x; + ret.y = other.y; + ret.z = other.z; + ret.w = other.w; + return ret; + } + + template __global__ void sparseKernel(const float2* prevPts, float2* nextPts, uchar* status, float* err, const int level, const int rows, const int cols) { #if __CUDA_ARCH__ <= 110 @@ -166,15 +393,15 @@ namespace pyrlk float x = prevPt.x + xBase + 0.5f; float y = prevPt.y + yBase + 0.5f; - I_patch[i][j] = Tex_I::read(x, y); + I_patch[i][j] = Tex_I::read(x, y); // Sharr Deriv - work_type dIdx = 3.0f * Tex_I::read(x+1, y-1) + 10.0f * Tex_I::read(x+1, y) + 3.0f * Tex_I::read(x+1, y+1) - - (3.0f * Tex_I::read(x-1, y-1) + 10.0f * Tex_I::read(x-1, y) + 3.0f * Tex_I::read(x-1, y+1)); + work_type dIdx = 3.0f * Tex_I::read(x+1, y-1) + 10.0f * Tex_I::read(x+1, y) + 3.0f * Tex_I::read(x+1, y+1) - + (3.0f * Tex_I::read(x-1, y-1) + 10.0f * Tex_I::read(x-1, y) + 3.0f * Tex_I::read(x-1, y+1)); - work_type dIdy = 3.0f * Tex_I::read(x-1, y+1) + 10.0f * Tex_I::read(x, y+1) + 3.0f * Tex_I::read(x+1, y+1) - - (3.0f * Tex_I::read(x-1, y-1) + 10.0f * Tex_I::read(x, y-1) + 3.0f * Tex_I::read(x+1, y-1)); + work_type dIdy = 3.0f * Tex_I::read(x-1, y+1) + 10.0f * Tex_I::read(x, y+1) + 3.0f * Tex_I::read(x+1, y+1) - + (3.0f * Tex_I::read(x-1, y-1) + 10.0f * Tex_I::read(x, y-1) + 3.0f * Tex_I::read(x+1, y-1)); dIdx_patch[i][j] = dIdx; dIdy_patch[i][j] = dIdy; @@ -243,7 +470,7 @@ namespace pyrlk for (int x = threadIdx.x, j = 0; x < c_winSize_x; x += blockDim.x, ++j) { work_type I_val = I_patch[i][j]; - work_type J_val = Tex_J::read(nextPt.x + x + 0.5f, nextPt.y + y + 0.5f); + work_type J_val = Tex_J::read(nextPt.x + x + 0.5f, nextPt.y + y + 0.5f); work_type diff = (J_val - I_val) * 32.0f; @@ -286,7 +513,7 @@ namespace pyrlk for (int x = threadIdx.x, j = 0; x < c_winSize_x; x += blockDim.x, ++j) { work_type I_val = I_patch[i][j]; - work_type J_val = Tex_J::read(nextPt.x + x + 0.5f, nextPt.y + y + 0.5f); + work_type J_val = Tex_J::read(nextPt.x + x + 0.5f, nextPt.y + y + 0.5f); work_type diff = J_val - I_val; @@ -309,22 +536,352 @@ namespace pyrlk } } - template - void sparse_caller(int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, cudaStream_t stream) + // Kernel, uses non texture fetches + template + __global__ void sparseKernel_(Ptr2D I, Ptr2D J, const float2* prevPts, float2* nextPts, uchar* status, float* err, const int level, const int rows, const int cols) { - dim3 grid(ptcount); +#if __CUDA_ARCH__ <= 110 + const int BLOCK_SIZE = 128; +#else + const int BLOCK_SIZE = 256; +#endif - if (level == 0 && err) - sparseKernel<<>>(prevPts, nextPts, status, err, level, rows, cols); - else - sparseKernel<<>>(prevPts, nextPts, status, err, level, rows, cols); + __shared__ float smem1[BLOCK_SIZE]; + __shared__ float smem2[BLOCK_SIZE]; + __shared__ float smem3[BLOCK_SIZE]; - cudaSafeCall( cudaGetLastError() ); + const unsigned int tid = threadIdx.y * blockDim.x + threadIdx.x; + + float2 prevPt = prevPts[blockIdx.x]; + prevPt.x *= (1.0f / (1 << level)); + prevPt.y *= (1.0f / (1 << level)); + + if (prevPt.x < 0 || prevPt.x >= cols || prevPt.y < 0 || prevPt.y >= rows) + { + if (tid == 0 && level == 0) + status[blockIdx.x] = 0; + + return; + } + + prevPt.x -= c_halfWin_x; + prevPt.y -= c_halfWin_y; + + // extract the patch from the first image, compute covariation matrix of derivatives + + float A11 = 0; + float A12 = 0; + float A22 = 0; + + typedef typename TypeVec::vec_type work_type; + + work_type I_patch[PATCH_Y][PATCH_X]; + work_type dIdx_patch[PATCH_Y][PATCH_X]; + work_type dIdy_patch[PATCH_Y][PATCH_X]; + + for (int yBase = threadIdx.y, i = 0; yBase < c_winSize_y; yBase += blockDim.y, ++i) + { + for (int xBase = threadIdx.x, j = 0; xBase < c_winSize_x; xBase += blockDim.x, ++j) + { + float x = prevPt.x + xBase + 0.5f; + float y = prevPt.y + yBase + 0.5f; + + I_patch[i][j] = ToFloat(I(y, x)); + + // Sharr Deriv + + work_type dIdx = 3.0f * I(y - 1, x + 1) + 10.0f * I(y, x + 1) + 3.0f * I(y + 1, x + 1) - + (3.0f * I(y - 1, x - 1) + 10.0f * I(y, x - 1) + 3.0f * I(y + 1 , x - 1)); + + work_type dIdy = 3.0f * I(y + 1, x - 1) + 10.0f * I(y + 1, x) + 3.0f * I(y+1, x + 1) - + (3.0f * I(y - 1, x - 1) + 10.0f * I(y-1, x) + 3.0f * I(y - 1, x + 1)); + + dIdx_patch[i][j] = dIdx; + dIdy_patch[i][j] = dIdy; + + accum(A11, dIdx * dIdx); + accum(A12, dIdx * dIdy); + accum(A22, dIdy * dIdy); + } + } + + reduce(smem_tuple(smem1, smem2, smem3), thrust::tie(A11, A12, A22), tid, thrust::make_tuple(plus(), plus(), plus())); + +#if __CUDA_ARCH__ >= 300 + if (tid == 0) + { + smem1[0] = A11; + smem2[0] = A12; + smem3[0] = A22; + } +#endif + + __syncthreads(); + + A11 = smem1[0]; + A12 = smem2[0]; + A22 = smem3[0]; + + float D = A11 * A22 - A12 * A12; + + if (D < numeric_limits::epsilon()) + { + if (tid == 0 && level == 0) + status[blockIdx.x] = 0; + + return; + } + + D = 1.f / D; + + A11 *= D; + A12 *= D; + A22 *= D; + + float2 nextPt = nextPts[blockIdx.x]; + nextPt.x *= 2.f; + nextPt.y *= 2.f; + + nextPt.x -= c_halfWin_x; + nextPt.y -= c_halfWin_y; + + for (int k = 0; k < c_iters; ++k) + { + if (nextPt.x < -c_halfWin_x || nextPt.x >= cols || nextPt.y < -c_halfWin_y || nextPt.y >= rows) + { + if (tid == 0 && level == 0) + status[blockIdx.x] = 0; + + return; + } + + float b1 = 0; + float b2 = 0; + + for (int y = threadIdx.y, i = 0; y < c_winSize_y; y += blockDim.y, ++i) + { + for (int x = threadIdx.x, j = 0; x < c_winSize_x; x += blockDim.x, ++j) + { + work_type I_val = I_patch[i][j]; + work_type J_val = ToFloat(J(nextPt.y + y + 0.5f, nextPt.x + x + 0.5f)); + + work_type diff = (J_val - I_val) * 32.0f; + + accum(b1, diff * dIdx_patch[i][j]); + accum(b2, diff * dIdy_patch[i][j]); + } + } + + reduce(smem_tuple(smem1, smem2), thrust::tie(b1, b2), tid, thrust::make_tuple(plus(), plus())); + +#if __CUDA_ARCH__ >= 300 + if (tid == 0) + { + smem1[0] = b1; + smem2[0] = b2; + } +#endif + + __syncthreads(); + + b1 = smem1[0]; + b2 = smem2[0]; + + float2 delta; + delta.x = A12 * b2 - A22 * b1; + delta.y = A12 * b1 - A11 * b2; + + nextPt.x += delta.x; + nextPt.y += delta.y; + + if (::fabs(delta.x) < 0.01f && ::fabs(delta.y) < 0.01f) + break; + } + + float errval = 0; + if (calcErr) + { + for (int y = threadIdx.y, i = 0; y < c_winSize_y; y += blockDim.y, ++i) + { + for (int x = threadIdx.x, j = 0; x < c_winSize_x; x += blockDim.x, ++j) + { + work_type I_val = I_patch[i][j]; + work_type J_val = ToFloat(J(nextPt.y + y + 0.5f, nextPt.x + x + 0.5f)); + + work_type diff = J_val - I_val; + + accum(errval, abs_(diff)); + } + } + + reduce(smem1, errval, tid, plus()); + } + + if (tid == 0) + { + nextPt.x += c_halfWin_x; + nextPt.y += c_halfWin_y; + + nextPts[blockIdx.x] = nextPt; + + if (calcErr) + err[blockIdx.x] = static_cast(errval) / (3 * c_winSize_x * c_winSize_y); + } + } // __global__ void sparseKernel_ + + + template class sparse_caller + { + public: + static void call(PtrStepSz::vec_type> I, PtrStepSz::vec_type> J, int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream) + { + dim3 grid(ptcount); + (void)I; + (void)J; + if (level == 0 && err) + sparseKernel <<>>(prevPts, nextPts, status, err, level, rows, cols); + else + sparseKernel <<>>(prevPts, nextPts, status, err, level, rows, cols); + + cudaSafeCall(cudaGetLastError()); + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; + // Specialization to use non texture path because for some reason the texture path keeps failing accuracy tests + template class sparse_caller<1, PATCH_X, PATCH_Y, unsigned short> + { + public: + typedef typename TypeVec::vec_type work_type; + typedef PtrStepSz Ptr2D; + typedef BrdConstant BrdType; + typedef BorderReader Reader; + typedef LinearFilter Filter; + static void call(Ptr2D I, Ptr2D J, int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream) + { + dim3 grid(ptcount); + if (level == 0 && err) + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + else + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + cudaSafeCall(cudaGetLastError()); + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; + // Specialization for int because the texture path keeps failing + template class sparse_caller<1, PATCH_X, PATCH_Y, int> + { + public: + typedef typename TypeVec::vec_type work_type; + typedef PtrStepSz Ptr2D; + typedef BrdConstant BrdType; + typedef BorderReader Reader; + typedef LinearFilter Filter; + static void call(Ptr2D I, Ptr2D J, int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream) + { + dim3 grid(ptcount); + if (level == 0 && err) + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + else + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + cudaSafeCall(cudaGetLastError()); + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; + template class sparse_caller<4, PATCH_X, PATCH_Y, int> + { + public: + typedef typename TypeVec::vec_type work_type; + typedef PtrStepSz Ptr2D; + typedef BrdConstant BrdType; + typedef BorderReader Reader; + typedef LinearFilter Filter; + static void call(Ptr2D I, Ptr2D J, int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream) + { + dim3 grid(ptcount); + if (level == 0 && err) + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + else + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + cudaSafeCall(cudaGetLastError()); + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; + using namespace cv::cuda::device; + template class sparse_caller<3, PATCH_X, PATCH_Y, T> + { + public: + typedef typename TypeVec::vec_type work_type; + typedef PtrStepSz Ptr2D; + typedef BrdConstant BrdType; + typedef BorderReader Reader; + typedef LinearFilter Filter; + static void call(Ptr2D I, Ptr2D J, int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream) + { + dim3 grid(ptcount); + if (level == 0 && err) + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + else + { + sparseKernel_ <<>>( + Filter(Reader(I, BrdType(rows, cols))), + Filter(Reader(J, BrdType(rows, cols))), + prevPts, nextPts, status, err, level, rows, cols); + } + cudaSafeCall(cudaGetLastError()); + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; - if (stream == 0) - cudaSafeCall( cudaDeviceSynchronize() ); - } template __global__ void denseKernel(PtrStepf u, PtrStepf v, const PtrStepf prevU, const PtrStepf prevV, PtrStepf err, const int rows, const int cols) @@ -484,77 +1041,72 @@ namespace pyrlk cudaSafeCall( cudaMemcpyToSymbolAsync(c_iters, &iters, sizeof(int), 0, cudaMemcpyHostToDevice, stream) ); } - void sparse1(PtrStepSzf I, PtrStepSzf J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, dim3 patch, cudaStream_t stream) + template struct pyrLK_caller { - typedef void (*func_t)(int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, cudaStream_t stream); - - static const func_t funcs[5][5] = + static void sparse(PtrStepSz::vec_type> I, PtrStepSz::vec_type> J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, dim3 patch, cudaStream_t stream) { - {sparse_caller<1, 1, 1>, sparse_caller<1, 2, 1>, sparse_caller<1, 3, 1>, sparse_caller<1, 4, 1>, sparse_caller<1, 5, 1>}, - {sparse_caller<1, 1, 2>, sparse_caller<1, 2, 2>, sparse_caller<1, 3, 2>, sparse_caller<1, 4, 2>, sparse_caller<1, 5, 2>}, - {sparse_caller<1, 1, 3>, sparse_caller<1, 2, 3>, sparse_caller<1, 3, 3>, sparse_caller<1, 4, 3>, sparse_caller<1, 5, 3>}, - {sparse_caller<1, 1, 4>, sparse_caller<1, 2, 4>, sparse_caller<1, 3, 4>, sparse_caller<1, 4, 4>, sparse_caller<1, 5, 4>}, - {sparse_caller<1, 1, 5>, sparse_caller<1, 2, 5>, sparse_caller<1, 3, 5>, sparse_caller<1, 4, 5>, sparse_caller<1, 5, 5>} - }; + typedef void(*func_t)(PtrStepSz::vec_type> I, PtrStepSz::vec_type> J, + int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, cudaStream_t stream); - bindTexture(&tex_If, I); - bindTexture(&tex_Jf, J); + static const func_t funcs[5][5] = + { + { sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call }, + { sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call }, + { sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call }, + { sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call }, + { sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call, sparse_caller::call } + }; - funcs[patch.y - 1][patch.x - 1](I.rows, I.cols, prevPts, nextPts, status, err, ptcount, - level, block, stream); - } + Tex_I::bindTexture_(I); + Tex_J::bindTexture_(J); - void sparse4(PtrStepSz I, PtrStepSz J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, dim3 patch, cudaStream_t stream) - { - typedef void (*func_t)(int rows, int cols, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, cudaStream_t stream); - - static const func_t funcs[5][5] = - { - {sparse_caller<4, 1, 1>, sparse_caller<4, 2, 1>, sparse_caller<4, 3, 1>, sparse_caller<4, 4, 1>, sparse_caller<4, 5, 1>}, - {sparse_caller<4, 1, 2>, sparse_caller<4, 2, 2>, sparse_caller<4, 3, 2>, sparse_caller<4, 4, 2>, sparse_caller<4, 5, 2>}, - {sparse_caller<4, 1, 3>, sparse_caller<4, 2, 3>, sparse_caller<4, 3, 3>, sparse_caller<4, 4, 3>, sparse_caller<4, 5, 3>}, - {sparse_caller<4, 1, 4>, sparse_caller<4, 2, 4>, sparse_caller<4, 3, 4>, sparse_caller<4, 4, 4>, sparse_caller<4, 5, 4>}, - {sparse_caller<4, 1, 5>, sparse_caller<4, 2, 5>, sparse_caller<4, 3, 5>, sparse_caller<4, 4, 5>, sparse_caller<4, 5, 5>} - }; - - bindTexture(&tex_If4, I); - bindTexture(&tex_Jf4, J); - - funcs[patch.y - 1][patch.x - 1](I.rows, I.cols, prevPts, nextPts, status, err, ptcount, - level, block, stream); - } - - void dense(PtrStepSzb I, PtrStepSzf J, PtrStepSzf u, PtrStepSzf v, PtrStepSzf prevU, PtrStepSzf prevV, PtrStepSzf err, int2 winSize, cudaStream_t stream) - { - dim3 block(16, 16); - dim3 grid(divUp(I.cols, block.x), divUp(I.rows, block.y)); - - bindTexture(&tex_Ib, I); - bindTexture(&tex_Jf, J); - - int2 halfWin = make_int2((winSize.x - 1) / 2, (winSize.y - 1) / 2); - const int patchWidth = block.x + 2 * halfWin.x; - const int patchHeight = block.y + 2 * halfWin.y; - size_t smem_size = 3 * patchWidth * patchHeight * sizeof(int); - - if (err.data) - { - denseKernel<<>>(u, v, prevU, prevV, err, I.rows, I.cols); - cudaSafeCall( cudaGetLastError() ); + funcs[patch.y - 1][patch.x - 1](I, J, I.rows, I.cols, prevPts, nextPts, status, err, ptcount, + level, block, stream); } - else + static void dense(PtrStepSzb I, PtrStepSz J, PtrStepSzf u, PtrStepSzf v, PtrStepSzf prevU, PtrStepSzf prevV, PtrStepSzf err, int2 winSize, cudaStream_t stream) { - denseKernel<<>>(u, v, prevU, prevV, PtrStepf(), I.rows, I.cols); - cudaSafeCall( cudaGetLastError() ); - } + dim3 block(16, 16); + dim3 grid(divUp(I.cols, block.x), divUp(I.rows, block.y)); + Tex_I<1, uchar>::bindTexture_(I); + Tex_J<1, T>::bindTexture_(J); - if (stream == 0) - cudaSafeCall( cudaDeviceSynchronize() ); - } + int2 halfWin = make_int2((winSize.x - 1) / 2, (winSize.y - 1) / 2); + const int patchWidth = block.x + 2 * halfWin.x; + const int patchHeight = block.y + 2 * halfWin.y; + size_t smem_size = 3 * patchWidth * patchHeight * sizeof(int); + + if (err.data) + { + denseKernel << > >(u, v, prevU, prevV, err, I.rows, I.cols); + cudaSafeCall(cudaGetLastError()); + } + else + { + denseKernel << > >(u, v, prevU, prevV, PtrStepf(), I.rows, I.cols); + cudaSafeCall(cudaGetLastError()); + } + + if (stream == 0) + cudaSafeCall(cudaDeviceSynchronize()); + } + }; + + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; + + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; + + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; + template class pyrLK_caller; } -#endif /* CUDA_DISABLER */ +#endif /* CUDA_DISABLER */ \ No newline at end of file diff --git a/modules/cudaoptflow/src/precomp.hpp b/modules/cudaoptflow/src/precomp.hpp index 3c818dd4e..d5ac49334 100644 --- a/modules/cudaoptflow/src/precomp.hpp +++ b/modules/cudaoptflow/src/precomp.hpp @@ -52,7 +52,7 @@ #include "opencv2/video.hpp" #include "opencv2/core/private.cuda.hpp" - +#include "opencv2/core/cuda/vec_traits.hpp" #include "opencv2/opencv_modules.hpp" #ifdef HAVE_OPENCV_CUDALEGACY diff --git a/modules/cudaoptflow/src/pyrlk.cpp b/modules/cudaoptflow/src/pyrlk.cpp index 9d7db0a43..dcfd1f66d 100644 --- a/modules/cudaoptflow/src/pyrlk.cpp +++ b/modules/cudaoptflow/src/pyrlk.cpp @@ -56,14 +56,20 @@ Ptr cv::cuda::DensePyrLKOpticalFlow::create(Size, int, in namespace pyrlk { void loadConstants(int2 winSize, int iters, cudaStream_t stream); + template struct pyrLK_caller + { + static void sparse(PtrStepSz::vec_type> I, PtrStepSz::vec_type> J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, dim3 patch, cudaStream_t stream); - void sparse1(PtrStepSzf I, PtrStepSzf J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, dim3 patch, cudaStream_t stream); - void sparse4(PtrStepSz I, PtrStepSz J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, - int level, dim3 block, dim3 patch, cudaStream_t stream); + static void dense(PtrStepSzb I, PtrStepSzf J, PtrStepSzf u, PtrStepSzf v, PtrStepSzf prevU, PtrStepSzf prevV, + PtrStepSzf err, int2 winSize, cudaStream_t stream); + }; - void dense(PtrStepSzb I, PtrStepSzf J, PtrStepSzf u, PtrStepSzf v, PtrStepSzf prevU, PtrStepSzf prevV, - PtrStepSzf err, int2 winSize, cudaStream_t stream); + template void dispatcher(GpuMat I, GpuMat J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, dim3 patch, cudaStream_t stream) + { + pyrLK_caller::sparse(I, J, prevPts, nextPts, status, err, ptcount, level, block, patch, stream); + } } namespace @@ -76,6 +82,9 @@ namespace void sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts, GpuMat& status, GpuMat* err, Stream& stream); + void sparse(std::vector& prevPyr, std::vector& nextPyr, const GpuMat& prevPts, GpuMat& nextPts, + GpuMat& status, GpuMat* err, Stream& stream); + void dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, Stream& stream); protected: @@ -83,8 +92,9 @@ namespace int maxLevel_; int iters_; bool useInitialFlow_; - + void buildImagePyramid(const GpuMat& prevImg, std::vector& prevPyr, const GpuMat& nextImg, std::vector& nextPyr, Stream stream); private: + friend class SparsePyrLKOpticalFlowImpl; std::vector prevPyr_; std::vector nextPyr_; }; @@ -113,6 +123,88 @@ namespace block.z = patch.z = 1; } + void PyrLKOpticalFlowBase::buildImagePyramid(const GpuMat& prevImg, std::vector& prevPyr, const GpuMat& nextImg, std::vector& nextPyr, Stream stream) + { + prevPyr.resize(maxLevel_ + 1); + nextPyr.resize(maxLevel_ + 1); + + int cn = prevImg.channels(); + + CV_Assert(cn == 1 || cn == 3 || cn == 4); + + prevPyr[0] = prevImg; + nextPyr[0] = nextImg; + + for (int level = 1; level <= maxLevel_; ++level) + { + cuda::pyrDown(prevPyr[level - 1], prevPyr[level], stream); + cuda::pyrDown(nextPyr[level - 1], nextPyr[level], stream); + } + } + void PyrLKOpticalFlowBase::sparse(std::vector& prevPyr, std::vector& nextPyr, const GpuMat& prevPts, GpuMat& nextPts, + GpuMat& status, GpuMat* err, Stream& stream) + { + CV_Assert(prevPyr.size() && nextPyr.size() && "Pyramid needs to at least contain the original matrix as the first element"); + CV_Assert(prevPyr[0].size() == nextPyr[0].size()); + CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2); + CV_Assert(maxLevel_ >= 0); + CV_Assert(winSize_.width > 2 && winSize_.height > 2); + if (useInitialFlow_) + CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == prevPts.type()); + else + ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts); + + GpuMat temp1 = (useInitialFlow_ ? nextPts : prevPts).reshape(1); + GpuMat temp2 = nextPts.reshape(1); + cuda::multiply(temp1, Scalar::all(1.0 / (1 << maxLevel_) / 2.0), temp2, 1, -1, stream); + + + ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status); + status.setTo(Scalar::all(1), stream); + + if (err) + ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); + + if (prevPyr.size() != size_t(maxLevel_ + 1) || nextPyr.size() != size_t(maxLevel_ + 1)) + { + buildImagePyramid(prevPyr[0], prevPyr, nextPyr[0], nextPyr, stream); + } + + dim3 block, patch; + calcPatchSize(winSize_, block, patch); + CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6); + pyrlk::loadConstants(make_int2(winSize_.width, winSize_.height), iters_, StreamAccessor::getStream(stream)); + + const int cn = prevPyr[0].channels(); + const int type = prevPyr[0].depth(); + + typedef void(*func_t)(GpuMat I, GpuMat J, const float2* prevPts, float2* nextPts, uchar* status, float* err, int ptcount, + int level, dim3 block, dim3 patch, cudaStream_t stream); + + // Current int datatype is disabled due to pyrDown not implementing it + // while ushort does work, it has significantly worse performance, and thus doesn't pass accuracy tests. + static const func_t funcs[6][4] = + { + { pyrlk::dispatcher , /*pyrlk::dispatcher*/ 0, pyrlk::dispatcher , pyrlk::dispatcher }, + { /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/ 0 }, + { pyrlk::dispatcher , /*pyrlk::dispatcher*/0, pyrlk::dispatcher , pyrlk::dispatcher }, + { /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/ 0, /*pyrlk::dispatcher*/0 }, + { pyrlk::dispatcher , /*pyrlk::dispatcher*/ 0, pyrlk::dispatcher , pyrlk::dispatcher }, + { pyrlk::dispatcher , /*pyrlk::dispatcher*/ 0, pyrlk::dispatcher , pyrlk::dispatcher } + }; + + func_t func = funcs[type][cn-1]; + CV_Assert(func != NULL && "Datatype not implemented"); + for (int level = maxLevel_; level >= 0; level--) + { + func(prevPyr[level], nextPyr[level], + prevPts.ptr(), nextPts.ptr(), + status.ptr(), level == 0 && err ? err->ptr() : 0, + prevPts.cols, level, block, patch, + StreamAccessor::getStream(stream)); + } + } + void PyrLKOpticalFlowBase::sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts, GpuMat& status, GpuMat* err, Stream& stream) { if (prevPts.empty()) @@ -122,86 +214,14 @@ namespace if (err) err->release(); return; } - - dim3 block, patch; - calcPatchSize(winSize_, block, patch); - CV_Assert( prevImg.channels() == 1 || prevImg.channels() == 3 || prevImg.channels() == 4 ); CV_Assert( prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type() ); - CV_Assert( maxLevel_ >= 0 ); - CV_Assert( winSize_.width > 2 && winSize_.height > 2 ); - CV_Assert( patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6 ); - CV_Assert( prevPts.rows == 1 && prevPts.type() == CV_32FC2 ); - - if (useInitialFlow_) - CV_Assert( nextPts.size() == prevPts.size() && nextPts.type() == prevPts.type() ); - else - ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts); - - GpuMat temp1 = (useInitialFlow_ ? nextPts : prevPts).reshape(1); - GpuMat temp2 = nextPts.reshape(1); - cuda::multiply(temp1, Scalar::all(1.0 / (1 << maxLevel_) / 2.0), temp2, 1, -1, stream); - - ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status); - status.setTo(Scalar::all(1), stream); - - if (err) - ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); // build the image pyramids. + buildImagePyramid(prevImg, prevPyr_, nextImg, nextPyr_, stream); - BufferPool pool(stream); + sparse(prevPyr_, nextPyr_, prevPts, nextPts, status, err, stream); - prevPyr_.resize(maxLevel_ + 1); - nextPyr_.resize(maxLevel_ + 1); - - int cn = prevImg.channels(); - - if (cn == 1 || cn == 4) - { - prevImg.convertTo(prevPyr_[0], CV_32F, stream); - nextImg.convertTo(nextPyr_[0], CV_32F, stream); - } - else - { - GpuMat buf = pool.getBuffer(prevImg.size(), CV_MAKE_TYPE(prevImg.depth(), 4)); - - cuda::cvtColor(prevImg, buf, COLOR_BGR2BGRA, 0, stream); - buf.convertTo(prevPyr_[0], CV_32F, stream); - - cuda::cvtColor(nextImg, buf, COLOR_BGR2BGRA, 0, stream); - buf.convertTo(nextPyr_[0], CV_32F, stream); - } - - for (int level = 1; level <= maxLevel_; ++level) - { - cuda::pyrDown(prevPyr_[level - 1], prevPyr_[level], stream); - cuda::pyrDown(nextPyr_[level - 1], nextPyr_[level], stream); - } - - pyrlk::loadConstants(make_int2(winSize_.width, winSize_.height), iters_, StreamAccessor::getStream(stream)); - - for (int level = maxLevel_; level >= 0; level--) - { - if (cn == 1) - { - pyrlk::sparse1(prevPyr_[level], nextPyr_[level], - prevPts.ptr(), nextPts.ptr(), - status.ptr(), - level == 0 && err ? err->ptr() : 0, prevPts.cols, - level, block, patch, - StreamAccessor::getStream(stream)); - } - else - { - pyrlk::sparse4(prevPyr_[level], nextPyr_[level], - prevPts.ptr(), nextPts.ptr(), - status.ptr(), - level == 0 && err ? err->ptr() : 0, prevPts.cols, - level, block, patch, - StreamAccessor::getStream(stream)); - } - } } void PyrLKOpticalFlowBase::dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, Stream& stream) @@ -250,7 +270,7 @@ namespace { int idx2 = (idx + 1) & 1; - pyrlk::dense(prevPyr_[level], nextPyr_[level], + pyrlk::pyrLK_caller::dense(prevPyr_[level], nextPyr_[level], uPyr[idx], vPyr[idx], uPyr[idx2], vPyr[idx2], PtrStepSzf(), winSize2i, StreamAccessor::getStream(stream)); @@ -289,14 +309,23 @@ namespace OutputArray _err, Stream& stream) { - const GpuMat prevImg = _prevImg.getGpuMat(); - const GpuMat nextImg = _nextImg.getGpuMat(); const GpuMat prevPts = _prevPts.getGpuMat(); GpuMat& nextPts = _nextPts.getGpuMatRef(); GpuMat& status = _status.getGpuMatRef(); GpuMat* err = _err.needed() ? &(_err.getGpuMatRef()) : NULL; - - sparse(prevImg, nextImg, prevPts, nextPts, status, err, stream); + if (_prevImg.kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT && _prevImg.kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT) + { + std::vector prevPyr, nextPyr; + _prevImg.getGpuMatVector(prevPyr); + _nextImg.getGpuMatVector(nextPyr); + sparse(prevPyr, nextPyr, prevPts, nextPts, status, err, stream); + } + else + { + const GpuMat prevImg = _prevImg.getGpuMat(); + const GpuMat nextImg = _nextImg.getGpuMat(); + sparse(prevImg, nextImg, prevPts, nextPts, status, err, stream); + } } }; @@ -347,4 +376,4 @@ Ptr cv::cuda::DensePyrLKOpticalFlow::create(Size winSize, return makePtr(winSize, maxLevel, iters, useInitialFlow); } -#endif /* !defined (HAVE_CUDA) */ +#endif /* !defined (HAVE_CUDA) */ \ No newline at end of file diff --git a/modules/cudaoptflow/test/test_optflow.cpp b/modules/cudaoptflow/test/test_optflow.cpp index 63bc461bb..9a3e3e57f 100644 --- a/modules/cudaoptflow/test/test_optflow.cpp +++ b/modules/cudaoptflow/test/test_optflow.cpp @@ -167,33 +167,34 @@ INSTANTIATE_TEST_CASE_P(CUDA_OptFlow, BroxOpticalFlow, ALL_DEVICES); namespace { - IMPLEMENT_PARAM_CLASS(UseGray, bool) + IMPLEMENT_PARAM_CLASS(Chan, int) + IMPLEMENT_PARAM_CLASS(DataType, int) } -PARAM_TEST_CASE(PyrLKOpticalFlow, cv::cuda::DeviceInfo, UseGray) +PARAM_TEST_CASE(PyrLKOpticalFlow, cv::cuda::DeviceInfo, Chan, DataType) { cv::cuda::DeviceInfo devInfo; - bool useGray; - + int channels; + int dataType; virtual void SetUp() { devInfo = GET_PARAM(0); - useGray = GET_PARAM(1); - + channels = GET_PARAM(1); + dataType = GET_PARAM(2); cv::cuda::setDevice(devInfo.deviceID()); } }; CUDA_TEST_P(PyrLKOpticalFlow, Sparse) { - cv::Mat frame0 = readImage("opticalflow/frame0.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); + cv::Mat frame0 = readImage("opticalflow/frame0.png", channels == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); ASSERT_FALSE(frame0.empty()); - cv::Mat frame1 = readImage("opticalflow/frame1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); + cv::Mat frame1 = readImage("opticalflow/frame1.png", channels == 1 ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR); ASSERT_FALSE(frame1.empty()); cv::Mat gray_frame; - if (useGray) + if (channels == 1) gray_frame = frame0; else cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY); @@ -208,22 +209,32 @@ CUDA_TEST_P(PyrLKOpticalFlow, Sparse) cv::Ptr pyrLK = cv::cuda::SparsePyrLKOpticalFlow::create(); - cv::cuda::GpuMat d_nextPts; - cv::cuda::GpuMat d_status; - pyrLK->calc(loadMat(frame0), loadMat(frame1), d_pts, d_nextPts, d_status); - - std::vector nextPts(d_nextPts.cols); - cv::Mat nextPts_mat(1, d_nextPts.cols, CV_32FC2, (void*) &nextPts[0]); - d_nextPts.download(nextPts_mat); - - std::vector status(d_status.cols); - cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void*) &status[0]); - d_status.download(status_mat); - std::vector nextPts_gold; std::vector status_gold; cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts_gold, status_gold, cv::noArray()); + + cv::cuda::GpuMat d_nextPts; + cv::cuda::GpuMat d_status; + cv::Mat converted0, converted1; + if(channels == 4) + { + cv::cvtColor(frame0, frame0, cv::COLOR_BGR2BGRA); + cv::cvtColor(frame1, frame1, cv::COLOR_BGR2BGRA); + } + frame0.convertTo(converted0, dataType); + frame1.convertTo(converted1, dataType); + + pyrLK->calc(loadMat(converted0), loadMat(converted1), d_pts, d_nextPts, d_status); + + std::vector nextPts(d_nextPts.cols); + cv::Mat nextPts_mat(1, d_nextPts.cols, CV_32FC2, (void*)&nextPts[0]); + d_nextPts.download(nextPts_mat); + + std::vector status(d_status.cols); + cv::Mat status_mat(1, d_status.cols, CV_8UC1, (void*)&status[0]); + d_status.download(status_mat); + ASSERT_EQ(nextPts_gold.size(), nextPts.size()); ASSERT_EQ(status_gold.size(), status.size()); @@ -251,11 +262,16 @@ CUDA_TEST_P(PyrLKOpticalFlow, Sparse) double bad_ratio = static_cast(mistmatch) / nextPts.size(); ASSERT_LE(bad_ratio, 0.01); + + } INSTANTIATE_TEST_CASE_P(CUDA_OptFlow, PyrLKOpticalFlow, testing::Combine( ALL_DEVICES, - testing::Values(UseGray(true), UseGray(false)))); + testing::Values(Chan(1), Chan(3), Chan(4)), + testing::Values(DataType(CV_8U), DataType(CV_16U), DataType(CV_32S), DataType(CV_32F)))); + + ////////////////////////////////////////////////////// // FarnebackOpticalFlow @@ -385,4 +401,4 @@ INSTANTIATE_TEST_CASE_P(CUDA_OptFlow, OpticalFlowDual_TVL1, testing::Combine( ALL_DEVICES, testing::Values(Gamma(0.0), Gamma(1.0)))); -#endif // HAVE_CUDA +#endif // HAVE_CUDA \ No newline at end of file diff --git a/modules/cudawarping/src/cuda/pyr_down.cu b/modules/cudawarping/src/cuda/pyr_down.cu index 3207d65cb..03e791dcf 100644 --- a/modules/cudawarping/src/cuda/pyr_down.cu +++ b/modules/cudawarping/src/cuda/pyr_down.cu @@ -212,10 +212,10 @@ namespace cv { namespace cuda { namespace device template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); - //template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); + template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); //template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); - //template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); - //template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); + template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); + template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); //template void pyrDown_gpu(PtrStepSzb src, PtrStepSzb dst, cudaStream_t stream); @@ -225,4 +225,4 @@ namespace cv { namespace cuda { namespace device }}} // namespace cv { namespace cuda { namespace cudev -#endif /* CUDA_DISABLER */ +#endif /* CUDA_DISABLER */ \ No newline at end of file diff --git a/modules/cudawarping/src/pyramids.cpp b/modules/cudawarping/src/pyramids.cpp index 0cb0f5de5..817a16715 100644 --- a/modules/cudawarping/src/pyramids.cpp +++ b/modules/cudawarping/src/pyramids.cpp @@ -74,7 +74,7 @@ void cv::cuda::pyrDown(InputArray _src, OutputArray _dst, Stream& stream) {0 /*pyrDown_gpu*/, 0 /*pyrDown_gpu*/ , 0 /*pyrDown_gpu*/, 0 /*pyrDown_gpu*/}, {pyrDown_gpu , 0 /*pyrDown_gpu*/, pyrDown_gpu , pyrDown_gpu }, {pyrDown_gpu , 0 /*pyrDown_gpu*/ , pyrDown_gpu , pyrDown_gpu }, - {0 /*pyrDown_gpu*/ , 0 /*pyrDown_gpu*/ , 0 /*pyrDown_gpu*/ , 0 /*pyrDown_gpu*/ }, + {pyrDown_gpu , 0 /*pyrDown_gpu*/ , pyrDown_gpu , pyrDown_gpu }, {pyrDown_gpu , 0 /*pyrDown_gpu*/ , pyrDown_gpu , pyrDown_gpu } }; @@ -131,4 +131,4 @@ void cv::cuda::pyrUp(InputArray _src, OutputArray _dst, Stream& stream) func(src, dst, StreamAccessor::getStream(stream)); } -#endif +#endif \ No newline at end of file diff --git a/modules/cudev/CMakeLists.txt b/modules/cudev/CMakeLists.txt index 257572951..f9f1d0bb9 100644 --- a/modules/cudev/CMakeLists.txt +++ b/modules/cudev/CMakeLists.txt @@ -17,6 +17,6 @@ ocv_set_module_sources(HEADERS ${lib_hdrs} SOURCES ${lib_srcs}) ocv_create_module() -if(BUILD_TESTS) +if(BUILD_TESTS AND NOT BUILD_opencv_world) add_subdirectory(test) endif() diff --git a/modules/cudev/include/opencv2/cudev/grid/reduce_to_vec.hpp b/modules/cudev/include/opencv2/cudev/grid/reduce_to_vec.hpp index 595ee8be6..599c2514e 100644 --- a/modules/cudev/include/opencv2/cudev/grid/reduce_to_vec.hpp +++ b/modules/cudev/include/opencv2/cudev/grid/reduce_to_vec.hpp @@ -182,7 +182,7 @@ __host__ void gridReduceToColumn_(const SrcPtr& src, GpuMat_& dst, cons CV_Assert( getRows(mask) == rows && getCols(mask) == cols ); - dst.create(1, rows); + cuda::createContinuous(rows, 1, dst.type(), dst); grid_reduce_to_vec_detail::reduceToColumn(shrinkPtr(src), dst[0], @@ -197,7 +197,7 @@ __host__ void gridReduceToColumn_(const SrcPtr& src, GpuMat_& dst, Stre const int rows = getRows(src); const int cols = getCols(src); - dst.create(1, rows); + cuda::createContinuous(rows, 1, dst.type(), dst); grid_reduce_to_vec_detail::reduceToColumn(shrinkPtr(src), dst[0], diff --git a/modules/cudev/test/test_reduction.cu b/modules/cudev/test/test_reduction.cu index c37605987..03c78def1 100644 --- a/modules/cudev/test/test_reduction.cu +++ b/modules/cudev/test/test_reduction.cu @@ -228,9 +228,6 @@ TEST(ReduceToColumn, Sum) Mat dst_gold; cv::reduce(src, dst_gold, 1, REDUCE_SUM, CV_32S); - dst_gold.cols = dst_gold.rows; - dst_gold.rows = 1; - dst_gold.step = dst_gold.cols * dst_gold.elemSize(); EXPECT_MAT_NEAR(dst_gold, dst, 0.0); } @@ -247,9 +244,6 @@ TEST(ReduceToColumn, Avg) Mat dst_gold; cv::reduce(src, dst_gold, 1, REDUCE_AVG, CV_32F); - dst_gold.cols = dst_gold.rows; - dst_gold.rows = 1; - dst_gold.step = dst_gold.cols * dst_gold.elemSize(); EXPECT_MAT_NEAR(dst_gold, dst, 1e-4); } @@ -266,9 +260,6 @@ TEST(ReduceToColumn, Min) Mat dst_gold; cv::reduce(src, dst_gold, 1, REDUCE_MIN); - dst_gold.cols = dst_gold.rows; - dst_gold.rows = 1; - dst_gold.step = dst_gold.cols * dst_gold.elemSize(); EXPECT_MAT_NEAR(dst_gold, dst, 0.0); } @@ -285,9 +276,6 @@ TEST(ReduceToColumn, Max) Mat dst_gold; cv::reduce(src, dst_gold, 1, REDUCE_MAX); - dst_gold.cols = dst_gold.rows; - dst_gold.rows = 1; - dst_gold.step = dst_gold.cols * dst_gold.elemSize(); EXPECT_MAT_NEAR(dst_gold, dst, 0.0); } diff --git a/modules/features2d/doc/agast.txt b/modules/features2d/doc/agast.txt new file mode 100644 index 000000000..974446f84 --- /dev/null +++ b/modules/features2d/doc/agast.txt @@ -0,0 +1,7701 @@ +/* This is AGAST and OAST, an optimal and accelerated corner detector + based on the accelerated segment tests + Below is the original copyright and the references */ + +/* +Copyright (C) 2010 Elmar Mair +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + *Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + *Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + *Neither the name of the University of Cambridge nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* +The references are: + * Adaptive and Generic Corner Detection Based on the Accelerated Segment Test, + Elmar Mair and Gregory D. Hager and Darius Burschka + and Michael Suppa and Gerhard Hirzinger ECCV 2010 + URL: http://www6.in.tum.de/Main/ResearchAgast +*/ + +#include "precomp.hpp" +#include "agast_score.hpp" + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) +#endif + +namespace cv +{ + +static void AGAST_5_8(InputArray _img, std::vector& keypoints, int threshold) +{ + + cv::Mat img; + if(!_img.getMat().isContinuous()) + img = _img.getMat().clone(); + else + img = _img.getMat(); + + size_t total = 0; + int xsize = img.cols; + int ysize = img.rows; + size_t nExpectedCorners = keypoints.capacity(); + register int x, y; + register int xsizeB = xsize - 2; + register int ysizeB = ysize - 1; + register int width; + + keypoints.resize(0); + + int pixel_5_8_[16]; + makeAgastOffsets(pixel_5_8_, (int)img.step, AgastFeatureDetector::AGAST_5_8); + + register short offset0 = (short) pixel_5_8_[0]; + register short offset1 = (short) pixel_5_8_[1]; + register short offset2 = (short) pixel_5_8_[2]; + register short offset3 = (short) pixel_5_8_[3]; + register short offset4 = (short) pixel_5_8_[4]; + register short offset5 = (short) pixel_5_8_[5]; + register short offset6 = (short) pixel_5_8_[6]; + register short offset7 = (short) pixel_5_8_[7]; + + width = xsize; + + for(y = 1; y < ysizeB; y++) + { + x = 0; + while(true) + { + homogeneous: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + if(ptr[offset7] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset1] > cb) + goto success_homogeneous; + else + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + } + } + structured: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset1] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto structured; + else + if(ptr[offset7] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + } + } + success_homogeneous: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto homogeneous; + success_structured: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto structured; + } + } +} + +static void AGAST_7_12d(InputArray _img, std::vector& keypoints, int threshold) +{ + cv::Mat img; + if(!_img.getMat().isContinuous()) + img = _img.getMat().clone(); + else + img = _img.getMat(); + + size_t total = 0; + int xsize = img.cols; + int ysize = img.rows; + size_t nExpectedCorners = keypoints.capacity(); + register int x, y; + register int xsizeB = xsize - 4; + register int ysizeB = ysize - 3; + register int width; + + keypoints.resize(0); + + int pixel_7_12d_[16]; + makeAgastOffsets(pixel_7_12d_, (int)img.step, AgastFeatureDetector::AGAST_7_12d); + + register short offset0 = (short) pixel_7_12d_[0]; + register short offset1 = (short) pixel_7_12d_[1]; + register short offset2 = (short) pixel_7_12d_[2]; + register short offset3 = (short) pixel_7_12d_[3]; + register short offset4 = (short) pixel_7_12d_[4]; + register short offset5 = (short) pixel_7_12d_[5]; + register short offset6 = (short) pixel_7_12d_[6]; + register short offset7 = (short) pixel_7_12d_[7]; + register short offset8 = (short) pixel_7_12d_[8]; + register short offset9 = (short) pixel_7_12d_[9]; + register short offset10 = (short) pixel_7_12d_[10]; + register short offset11 = (short) pixel_7_12d_[11]; + + width = xsize; + + for(y = 3; y < ysizeB; y++) + { + x = 2; + while(true) + { + homogeneous: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_homogeneous; + else + if(ptr[offset7] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + goto success_homogeneous; + else + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto success_homogeneous; + else + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset2] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + } + } + structured: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset2] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset2] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + } + } + success_homogeneous: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto homogeneous; + success_structured: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto structured; + } + } +} + +static void AGAST_7_12s(InputArray _img, std::vector& keypoints, int threshold) +{ + cv::Mat img; + if(!_img.getMat().isContinuous()) + img = _img.getMat().clone(); + else + img = _img.getMat(); + + size_t total = 0; + int xsize = img.cols; + int ysize = img.rows; + size_t nExpectedCorners = keypoints.capacity(); + register int x, y; + register int xsizeB=xsize - 3; //2, +1 due to faster test x>xsizeB + register int ysizeB=ysize - 2; + register int width; + + keypoints.resize(0); + + int pixel_7_12s_[16]; + makeAgastOffsets(pixel_7_12s_, (int)img.step, AgastFeatureDetector::AGAST_7_12s); + + register short offset0 = (short) pixel_7_12s_[0]; + register short offset1 = (short) pixel_7_12s_[1]; + register short offset2 = (short) pixel_7_12s_[2]; + register short offset3 = (short) pixel_7_12s_[3]; + register short offset4 = (short) pixel_7_12s_[4]; + register short offset5 = (short) pixel_7_12s_[5]; + register short offset6 = (short) pixel_7_12s_[6]; + register short offset7 = (short) pixel_7_12s_[7]; + register short offset8 = (short) pixel_7_12s_[8]; + register short offset9 = (short) pixel_7_12s_[9]; + register short offset10 = (short) pixel_7_12s_[10]; + register short offset11 = (short) pixel_7_12s_[11]; + + width = xsize; + + for(y = 2; y < ysizeB; y++) + { + x = 1; + while(true) + { + homogeneous: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + goto success_homogeneous; + else + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto success_homogeneous; + else + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_homogeneous; + else + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_homogeneous; + else + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_homogeneous; + else + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset2] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + else + goto homogeneous; + } + } + structured: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset7] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + if(ptr[offset11] > cb) + goto success_structured; + else + goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto success_structured; + else + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto success_structured; + else + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto success_structured; + else + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto success_structured; + else + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + if(ptr[offset1] < c_b) + if(ptr[offset2] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto success_structured; + else + goto structured; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto success_structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto structured; + else + goto homogeneous; + } + } + success_homogeneous: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto homogeneous; + success_structured: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto structured; + } + } +} + +static void OAST_9_16(InputArray _img, std::vector& keypoints, int threshold) +{ + cv::Mat img; + if(!_img.getMat().isContinuous()) + img = _img.getMat().clone(); + else + img = _img.getMat(); + + size_t total = 0; + int xsize = img.cols; + int ysize = img.rows; + size_t nExpectedCorners = keypoints.capacity(); + register int x, y; + register int xsizeB=xsize - 4; + register int ysizeB=ysize - 3; + register int width; + + keypoints.resize(0); + + int pixel_9_16_[16]; + makeAgastOffsets(pixel_9_16_, (int)img.step, AgastFeatureDetector::OAST_9_16); + + register short offset0 = (short) pixel_9_16_[0]; + register short offset1 = (short) pixel_9_16_[1]; + register short offset2 = (short) pixel_9_16_[2]; + register short offset3 = (short) pixel_9_16_[3]; + register short offset4 = (short) pixel_9_16_[4]; + register short offset5 = (short) pixel_9_16_[5]; + register short offset6 = (short) pixel_9_16_[6]; + register short offset7 = (short) pixel_9_16_[7]; + register short offset8 = (short) pixel_9_16_[8]; + register short offset9 = (short) pixel_9_16_[9]; + register short offset10 = (short) pixel_9_16_[10]; + register short offset11 = (short) pixel_9_16_[11]; + register short offset12 = (short) pixel_9_16_[12]; + register short offset13 = (short) pixel_9_16_[13]; + register short offset14 = (short) pixel_9_16_[14]; + register short offset15 = (short) pixel_9_16_[15]; + + width = xsize; + + for(y = 3; y < ysizeB; y++) + { + x = 2; + while(true) + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset10] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset12] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset10] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset14] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset10] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset12] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset10] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset2] < c_b) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset12] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] > cb) + if(ptr[offset12] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset14] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset6] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset6] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset14] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset12] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset1] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + {} // goto success_homogeneous; + else + if(ptr[offset10] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + {} // goto success_homogeneous; + else + if(ptr[offset10] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + {} // goto success_homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + else + continue; // goto homogeneous; + } + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + } + } +} + + +void AGAST(InputArray _img, std::vector& keypoints, int threshold, bool nonmax_suppression) +{ + AGAST(_img, keypoints, threshold, nonmax_suppression, AgastFeatureDetector::OAST_9_16); +} + + +class AgastFeatureDetector_Impl : public AgastFeatureDetector +{ +public: + AgastFeatureDetector_Impl( int _threshold, bool _nonmaxSuppression, int _type ) + : threshold(_threshold), nonmaxSuppression(_nonmaxSuppression), type((short)_type) + {} + + void detect( InputArray _image, std::vector& keypoints, InputArray _mask ) + { + Mat mask = _mask.getMat(), grayImage; + UMat ugrayImage; + _InputArray gray = _image; + if( _image.type() != CV_8U ) + { + _OutputArray ogray = _image.isUMat() ? _OutputArray(ugrayImage) : _OutputArray(grayImage); + cvtColor( _image, ogray, COLOR_BGR2GRAY ); + gray = ogray; + } + AGAST( gray, keypoints, threshold, nonmaxSuppression, type ); + KeyPointsFilter::runByPixelsMask( keypoints, mask ); + } + + void set(int prop, double value) + { + if(prop == THRESHOLD) + threshold = cvRound(value); + else if(prop == NONMAX_SUPPRESSION) + nonmaxSuppression = value != 0; + else + CV_Error(Error::StsBadArg, ""); + } + + double get(int prop) const + { + if(prop == THRESHOLD) + return threshold; + if(prop == NONMAX_SUPPRESSION) + return nonmaxSuppression; + CV_Error(Error::StsBadArg, ""); + return 0; + } + + void setThreshold(int threshold_) { threshold = threshold_; } + int getThreshold() const { return threshold; } + + void setNonmaxSuppression(bool f) { nonmaxSuppression = f; } + bool getNonmaxSuppression() const { return nonmaxSuppression; } + + void setType(int type_) { type = type_; } + int getType() const { return type; } + + int threshold; + bool nonmaxSuppression; + int type; +}; + +Ptr AgastFeatureDetector::create( int threshold, bool nonmaxSuppression, int type ) +{ + return makePtr(threshold, nonmaxSuppression, type); +} + +void AGAST(InputArray _img, std::vector& keypoints, int threshold, bool nonmax_suppression, int type) +{ + + std::vector kpts; + + // detect + switch(type) { + case AgastFeatureDetector::AGAST_5_8: + AGAST_5_8(_img, kpts, threshold); + break; + case AgastFeatureDetector::AGAST_7_12d: + AGAST_7_12d(_img, kpts, threshold); + break; + case AgastFeatureDetector::AGAST_7_12s: + AGAST_7_12s(_img, kpts, threshold); + break; + case AgastFeatureDetector::OAST_9_16: + OAST_9_16(_img, kpts, threshold); + break; + } + + cv::Mat img = _img.getMat(); + + // score + int pixel_[16]; + makeAgastOffsets(pixel_, (int)img.step, type); + + std::vector::iterator kpt; + for(kpt = kpts.begin(); kpt != kpts.end(); kpt++) + { + switch(type) { + case AgastFeatureDetector::AGAST_5_8: + kpt->response = (float)agast_cornerScore + (&img.at((int)kpt->pt.y, (int)kpt->pt.x), pixel_, threshold); + break; + case AgastFeatureDetector::AGAST_7_12d: + kpt->response = (float)agast_cornerScore + (&img.at((int)kpt->pt.y, (int)kpt->pt.x), pixel_, threshold); + break; + case AgastFeatureDetector::AGAST_7_12s: + kpt->response = (float)agast_cornerScore + (&img.at((int)kpt->pt.y, (int)kpt->pt.x), pixel_, threshold); + break; + case AgastFeatureDetector::OAST_9_16: + kpt->response = (float)agast_cornerScore + (&img.at((int)kpt->pt.y, (int)kpt->pt.x), pixel_, threshold); + break; + } + } + + // suppression + if(nonmax_suppression) + { + size_t j; + size_t curr_idx; + size_t lastRow = 0, next_lastRow = 0; + size_t num_Corners = kpts.size(); + size_t lastRowCorner_ind = 0, next_lastRowCorner_ind = 0; + + std::vector nmsFlags; + std::vector::iterator currCorner_nms; + std::vector::const_iterator currCorner; + + currCorner = kpts.begin(); + + nmsFlags.resize((int)num_Corners); + + // set all flags to MAXIMUM + for(j = 0; j < num_Corners; j++) + nmsFlags[j] = -1; + + for(curr_idx = 0; curr_idx < num_Corners; curr_idx++) + { + int t; + // check above + if(lastRow + 1 < currCorner->pt.y) + { + lastRow = next_lastRow; + lastRowCorner_ind = next_lastRowCorner_ind; + } + if(next_lastRow != currCorner->pt.y) + { + next_lastRow = (size_t) currCorner->pt.y; + next_lastRowCorner_ind = curr_idx; + } + if(lastRow + 1 == currCorner->pt.y) + { + // find the corner above the current one + while( (kpts[lastRowCorner_ind].pt.x < currCorner->pt.x) + && (kpts[lastRowCorner_ind].pt.y == lastRow) ) + lastRowCorner_ind++; + + if( (kpts[lastRowCorner_ind].pt.x == currCorner->pt.x) + && (lastRowCorner_ind != curr_idx) ) + { + size_t w = lastRowCorner_ind; + // find the maximum in this block + while(nmsFlags[w] != -1) + w = nmsFlags[w]; + + if(kpts[curr_idx].response < kpts[w].response) + nmsFlags[curr_idx] = (int)w; + else + nmsFlags[w] = (int)curr_idx; + } + } + + // check left + t = (int)curr_idx - 1; + if( (curr_idx != 0) && (kpts[t].pt.y == currCorner->pt.y) + && (kpts[t].pt.x + 1 == currCorner->pt.x) ) + { + int currCornerMaxAbove_ind = nmsFlags[curr_idx]; + // find the maximum in that area + while(nmsFlags[t] != -1) + t = nmsFlags[t]; + // no maximum above + if(currCornerMaxAbove_ind == -1) + { + if((size_t)t != curr_idx) + { + if ( kpts[curr_idx].response < kpts[t].response ) + nmsFlags[curr_idx] = t; + else + nmsFlags[t] = (int)curr_idx; + } + } + else // maximum above + { + if(t != currCornerMaxAbove_ind) + { + if(kpts[currCornerMaxAbove_ind].response < kpts[t].response) + { + nmsFlags[currCornerMaxAbove_ind] = t; + nmsFlags[curr_idx] = t; + } + else + { + nmsFlags[t] = currCornerMaxAbove_ind; + nmsFlags[curr_idx] = currCornerMaxAbove_ind; + } + } + } + } + currCorner++; + } + + // collecting maximum corners + for(curr_idx = 0; curr_idx < num_Corners; curr_idx++) + { + if (nmsFlags[curr_idx] == -1) + keypoints.push_back(kpts[curr_idx]); + } + } else + { + keypoints = kpts; + } +} + +} // END NAMESPACE CV diff --git a/modules/features2d/doc/agast_score.txt b/modules/features2d/doc/agast_score.txt new file mode 100644 index 000000000..baca9b07c --- /dev/null +++ b/modules/features2d/doc/agast_score.txt @@ -0,0 +1,9402 @@ +/* This is AGAST and OAST, an optimal and accelerated corner detector + based on the accelerated segment tests + Below is the original copyright and the references */ + +/* +Copyright (C) 2010 Elmar Mair +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + *Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + *Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + *Neither the name of the University of Cambridge nor the names of + its contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* +The references are: + * Adaptive and Generic Corner Detection Based on the Accelerated Segment Test, + Elmar Mair and Gregory D. Hager and Darius Burschka + and Michael Suppa and Gerhard Hirzinger ECCV 2010 + URL: http://www6.in.tum.de/Main/ResearchAgast +*/ + +#include "agast_score.hpp" + +#ifdef _MSC_VER +#pragma warning( disable : 4127 ) +#endif + +namespace cv +{ + +void makeAgastOffsets(int pixel[16], int rowStride, int type) +{ + static const int offsets16[][2] = + { + {-3, 0}, {-3, -1}, {-2, -2}, {-1, -3}, {0, -3}, { 1, -3}, { 2, -2}, { 3, -1}, + { 3, 0}, { 3, 1}, { 2, 2}, { 1, 3}, {0, 3}, {-1, 3}, {-2, 2}, {-3, 1} + }; + + static const int offsets12d[][2] = + { + {-3, 0}, {-2, -1}, {-1, -2}, {0, -3}, { 1, -2}, { 2, -1}, + { 3, 0}, { 2, 1}, { 1, 2}, {0, 3}, {-1, 2}, {-2, 1} + }; + + static const int offsets12s[][2] = + { + {-2, 0}, {-2, -1}, {-1, -2}, {0, -2}, { 1, -2}, { 2, -1}, + { 2, 0}, { 2, 1}, { 1, 2}, {0, 2}, {-1, 2}, {-2, 1} + }; + + static const int offsets8[][2] = + { + {-1, 0}, {-1, -1}, {0, -1}, { 1, -1}, + { 1, 0}, { 1, 1}, {0, 1}, {-1, 1} + }; + + const int (*offsets)[2] = type == AgastFeatureDetector::OAST_9_16 ? offsets16 : + type == AgastFeatureDetector::AGAST_7_12d ? offsets12d : + type == AgastFeatureDetector::AGAST_7_12s ? offsets12s : + type == AgastFeatureDetector::AGAST_5_8 ? offsets8 : 0; + + CV_Assert(pixel && offsets); + + int k = 0; + for( ; k < 16; k++ ) + pixel[k] = offsets[k][0] + offsets[k][1] * rowStride; +} + +// 16 pixel mask +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + int bmin = threshold; + int bmax = 255; + int b_test = (bmax + bmin) / 2; + + register short offset0 = (short) pixel[0]; + register short offset1 = (short) pixel[1]; + register short offset2 = (short) pixel[2]; + register short offset3 = (short) pixel[3]; + register short offset4 = (short) pixel[4]; + register short offset5 = (short) pixel[5]; + register short offset6 = (short) pixel[6]; + register short offset7 = (short) pixel[7]; + register short offset8 = (short) pixel[8]; + register short offset9 = (short) pixel[9]; + register short offset10 = (short) pixel[10]; + register short offset11 = (short) pixel[11]; + register short offset12 = (short) pixel[12]; + register short offset13 = (short) pixel[13]; + register short offset14 = (short) pixel[14]; + register short offset15 = (short) pixel[15]; + + while(true) + { + register const int cb = *ptr + b_test; + register const int c_b = *ptr - b_test; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset10] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset12] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset10] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + goto is_a_corner; + else + if(ptr[offset14] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset5] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset12] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset12] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] > cb) + if(ptr[offset12] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset14] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset10] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + goto is_a_corner; + else + if(ptr[offset14] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset12] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset12] > cb) + if(ptr[offset13] > cb) + if(ptr[offset14] > cb) + if(ptr[offset15] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset12] < c_b) + if(ptr[offset13] < c_b) + if(ptr[offset14] < c_b) + if(ptr[offset15] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + + is_a_corner: + bmin = b_test; + goto end; + + is_not_a_corner: + bmax = b_test; + goto end; + + end: + + if(bmin == bmax - 1 || bmin == bmax) + return bmin; + b_test = (bmin + bmax) / 2; + } +} + +// 12 pixel mask in diamond format +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + int bmin = threshold; + int bmax = 255; + int b_test = (bmax + bmin)/2; + + register short offset0 = (short) pixel[0]; + register short offset1 = (short) pixel[1]; + register short offset2 = (short) pixel[2]; + register short offset3 = (short) pixel[3]; + register short offset4 = (short) pixel[4]; + register short offset5 = (short) pixel[5]; + register short offset6 = (short) pixel[6]; + register short offset7 = (short) pixel[7]; + register short offset8 = (short) pixel[8]; + register short offset9 = (short) pixel[9]; + register short offset10 = (short) pixel[10]; + register short offset11 = (short) pixel[11]; + + while(true) + { + register const int cb = *ptr + b_test; + register const int c_b = *ptr - b_test; + if(ptr[offset0] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset2] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset11] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset9] > cb) + if(ptr[offset6] > cb) + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset2] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset2] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + + is_a_corner: + bmin = b_test; + goto end; + + is_not_a_corner: + bmax = b_test; + goto end; + + end: + + if(bmin == bmax - 1 || bmin == bmax) + return bmin; + b_test = (bmin + bmax) / 2; + } +} + +//12 pixel mask in square format +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + int bmin = threshold; + int bmax = 255; + int b_test = (bmax + bmin)/2; + + register short offset0 = (short) pixel[0]; + register short offset1 = (short) pixel[1]; + register short offset2 = (short) pixel[2]; + register short offset3 = (short) pixel[3]; + register short offset4 = (short) pixel[4]; + register short offset5 = (short) pixel[5]; + register short offset6 = (short) pixel[6]; + register short offset7 = (short) pixel[7]; + register short offset8 = (short) pixel[8]; + register short offset9 = (short) pixel[9]; + register short offset10 = (short) pixel[10]; + register short offset11 = (short) pixel[11]; + + while(true) + { + register const int cb = *ptr + b_test; + register const int c_b = *ptr - b_test; + if(ptr[offset0] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset2] < c_b) + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset3] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset7] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset0] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset2] > cb) + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset2] > cb) + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset9] > cb) + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset4] < c_b) + if(ptr[offset10] > cb) + if(ptr[offset8] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset9] > cb) + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset7] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset9] > cb) + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] > cb) + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset2] > cb) + if(ptr[offset9] > cb) + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] < c_b) + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] < c_b) + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset8] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset8] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset3] < c_b) + goto is_a_corner; + else + if(ptr[offset10] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] < c_b) + if(ptr[offset11] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset2] < c_b) + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset2] > cb) + if(ptr[offset9] < c_b) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset8] > cb) + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset8] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset9] < c_b) + goto is_not_a_corner; + else + if(ptr[offset9] > cb) + if(ptr[offset1] > cb) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + goto is_not_a_corner; + else + if(ptr[offset6] > cb) + if(ptr[offset8] > cb) + if(ptr[offset4] > cb) + if(ptr[offset3] > cb) + goto is_a_corner; + else + if(ptr[offset10] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset10] > cb) + if(ptr[offset11] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + + is_a_corner: + bmin = b_test; + goto end; + + is_not_a_corner: + bmax = b_test; + goto end; + + end: + + if(bmin == bmax - 1 || bmin == bmax) + return bmin; + b_test = (bmin + bmax) / 2; + } +} + +// 8 pixel mask +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + int bmin = threshold; + int bmax = 255; + int b_test = (bmax + bmin)/2; + + register short offset0 = (short) pixel[0]; + register short offset1 = (short) pixel[1]; + register short offset2 = (short) pixel[2]; + register short offset3 = (short) pixel[3]; + register short offset4 = (short) pixel[4]; + register short offset5 = (short) pixel[5]; + register short offset6 = (short) pixel[6]; + register short offset7 = (short) pixel[7]; + + while(true) + { + register const int cb = *ptr + b_test; + register const int c_b = *ptr - b_test; + if(ptr[offset0] > cb) + if(ptr[offset2] > cb) + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + if(ptr[offset7] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset5] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset7] > cb) + if(ptr[offset6] > cb) + if(ptr[offset1] > cb) + goto is_a_corner; + else + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset0] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset7] > cb) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset6] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] > cb) + if(ptr[offset3] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset5] < c_b) + if(ptr[offset7] < c_b) + if(ptr[offset6] < c_b) + if(ptr[offset1] < c_b) + goto is_a_corner; + else + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] > cb) + if(ptr[offset5] > cb) + if(ptr[offset2] > cb) + if(ptr[offset1] > cb) + if(ptr[offset4] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] > cb) + if(ptr[offset4] > cb) + if(ptr[offset6] > cb) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset3] < c_b) + if(ptr[offset5] < c_b) + if(ptr[offset2] < c_b) + if(ptr[offset1] < c_b) + if(ptr[offset4] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + if(ptr[offset7] < c_b) + if(ptr[offset4] < c_b) + if(ptr[offset6] < c_b) + goto is_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + else + goto is_not_a_corner; + + is_a_corner: + bmin=b_test; + goto end; + + is_not_a_corner: + bmax=b_test; + goto end; + + end: + + if(bmin == bmax - 1 || bmin == bmax) + return bmin; + b_test = (bmin + bmax) / 2; + } +} + +} // namespace cv diff --git a/modules/features2d/doc/read_file_nondiff32.pl b/modules/features2d/doc/read_file_nondiff32.pl new file mode 100644 index 000000000..6f1b420ec --- /dev/null +++ b/modules/features2d/doc/read_file_nondiff32.pl @@ -0,0 +1,284 @@ +#!/usr/bin/perl +use strict; +use warnings; +use autodie; # die if problem reading or writing a file + +my $filein = "./agast.txt"; +my $fileout = "./agast_new.txt"; +my $i1=1; +my $i2=1; +my $i3=1; +my $tmp; +my $ifcount0=0; +my $ifcount1=0; +my $ifcount2=0; +my $ifcount3=0; +my $ifcount4=0; +my $elsecount; +my $myfirstline = $ARGV[0]; +my $mylastline = $ARGV[1]; +my $tablename = $ARGV[2]; +my @array0 = (); +my @array1 = (); +my @array2 = (); +my @array3 = (); +my $homogeneous; +my $success_homogeneous; +my $structured; +my $success_structured; + + open(my $in1, "<", $filein) or die "Can't open $filein: $!"; + open(my $out, ">", $fileout) or die "Can't open $fileout: $!"; + + + $array0[0] = 0; + $i1=1; + while (my $line1 = <$in1>) + { + chomp $line1; + $array0[$i1] = 0; + if (($i1>=$myfirstline)&&($i1<=$mylastline)) + { + if($line1=~/if\(ptr\[offset(\d+)/) + { + if($line1=~/if\(ptr\[offset(\d+).*\>.*cb/) + { + $tmp=$1; + } + else + { + if($line1=~/if\(ptr\[offset(\d+).*\<.*c\_b/) + { + $tmp=$1+128; + } + else + { + die "invalid array index!" + } + } + $array1[$ifcount1] = $tmp; + $array0[$ifcount1] = $i1; + $ifcount1++; + } + else + { + } + } + $i1++; + } + $homogeneous=$ifcount1; + $success_homogeneous=$ifcount1+1; + $structured=$ifcount1+2; + $success_structured=$ifcount1+3; + + close $in1 or die "Can't close $filein: $!"; + + open($in1, "<", $filein) or die "Can't open $filein: $!"; + + + $i1=1; + while (my $line1 = <$in1>) + { + chomp $line1; + if (($i1>=$myfirstline)&&($i1<=$mylastline)) + { + if ($array0[$ifcount2] == $i1) + { + $array2[$ifcount2]=0; + $array3[$ifcount2]=0; + if ($array0[$ifcount2+1] == ($i1+1)) + { + $array2[$ifcount2]=($ifcount2+1); + } + else + { + open(my $in2, "<", $filein) or die "Can't open $filein: $!"; + $i2=1; + while (my $line2 = <$in2>) + { + chomp $line2; + if ($i2 == $i1) + { + last; + } + $i2++; + } + my $line2 = <$in2>; + chomp $line2; + if ($line2=~/goto (\w+)/) + { + $tmp=$1; + if ($tmp eq "homogeneous") + { + $array2[$ifcount2]=$homogeneous; + } + if ($tmp eq "success_homogeneous") + { + $array2[$ifcount2]=$success_homogeneous; + } + if ($tmp eq "structured") + { + $array2[$ifcount2]=$structured; + } + if ($tmp eq "success_structured") + { + $array2[$ifcount2]=$success_structured; + } + } + else + { + die "goto expected: $!"; + } + close $in2 or die "Can't close $filein: $!"; + } + #find next else and interprete it + open(my $in3, "<", $filein) or die "Can't open $filein: $!"; + $i3=1; + $ifcount3=0; + $elsecount=0; + while (my $line3 = <$in3>) + { + chomp $line3; + $i3++; + if ($i3 == $i1) + { + last; + } + } + while (my $line3 = <$in3>) + { + chomp $line3; + $ifcount3++; + if (($elsecount==0)&&($i3>$i1)) + { + if ($line3=~/goto (\w+)/) + { + $tmp=$1; + if ($tmp eq "homogeneous") + { + $array3[$ifcount2]=$homogeneous; + } + if ($tmp eq "success_homogeneous") + { + $array3[$ifcount2]=$success_homogeneous; + } + if ($tmp eq "structured") + { + $array3[$ifcount2]=$structured; + } + if ($tmp eq "success_structured") + { + $array3[$ifcount2]=$success_structured; + } + } + else + { + if ($line3=~/if\(ptr\[offset/) + { + $ifcount4=0; + while ($array0[$ifcount4]!=$i3) + { + $ifcount4++; + if ($ifcount4==$ifcount1) + { + die "if else match expected: $!"; + } + $array3[$ifcount2]=$ifcount4; + } + } + else + { + die "elseif or elsegoto match expected: $!"; + } + } + last; + } + else + { + if ($line3=~/if\(ptr\[offset/) + { + $elsecount++; + } + else + { + if ($line3=~/else/) + { + $elsecount--; + } + } + } + $i3++; + } + printf("%3d [%3d][0x%08x]\n", $array0[$ifcount2], $ifcount2, (($array1[$ifcount2]&15)<<28)|($array2[$ifcount2]<<16)|(($array1[$ifcount2]&128)<<5)|($array3[$ifcount2])); + close $in3 or die "Can't close $filein: $!"; + $ifcount2++; + } + else + { + } + } + $i1++; + } + + printf(" [%3d][0x%08x]\n", $homogeneous, 252); + printf(" [%3d][0x%08x]\n", $success_homogeneous, 253); + printf(" [%3d][0x%08x]\n", $structured, 254); + printf(" [%3d][0x%08x]\n", $success_structured, 255); + + close $in1 or die "Can't close $filein: $!"; + + $ifcount0=0; + $ifcount2=0; + printf $out " static const unsigned long %s[] = {\n ", $tablename; + while ($ifcount0 < $ifcount1) + { + printf $out "0x%08x, ", (($array1[$ifcount0]&15)<<28)|($array2[$ifcount0]<<16)|(($array1[$ifcount0]&128)<<5)|($array3[$ifcount0]); + + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + + } + printf $out "0x%08x, ", 252; + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + printf $out "0x%08x, ", 253; + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + printf $out "0x%08x, ", 254; + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + printf $out "0x%08x\n", 255; + $ifcount0++; + $ifcount2++; + printf $out " };\n\n"; + + $#array0 = -1; + $#array1 = -1; + $#array2 = -1; + $#array3 = -1; + + close $out or die "Can't close $fileout: $!"; diff --git a/modules/features2d/doc/read_file_score32.pl b/modules/features2d/doc/read_file_score32.pl new file mode 100644 index 000000000..c1adedac2 --- /dev/null +++ b/modules/features2d/doc/read_file_score32.pl @@ -0,0 +1,244 @@ +#!/usr/bin/perl +use strict; +use warnings; +use autodie; # die if problem reading or writing a file + +my $filein = "./agast_score.txt"; +my $fileout = "./agast_new.txt"; +my $i1=1; +my $i2=1; +my $i3=1; +my $tmp; +my $ifcount0=0; +my $ifcount1=0; +my $ifcount2=0; +my $ifcount3=0; +my $ifcount4=0; +my $elsecount; +my $myfirstline = $ARGV[0]; +my $mylastline = $ARGV[1]; +my $tablename = $ARGV[2]; +my @array0 = (); +my @array1 = (); +my @array2 = (); +my @array3 = (); +my $is_not_a_corner; +my $is_a_corner; + + open(my $in1, "<", $filein) or die "Can't open $filein: $!"; + open(my $out, ">", $fileout) or die "Can't open $fileout: $!"; + + + $array0[0] = 0; + $i1=1; + while (my $line1 = <$in1>) + { + chomp $line1; + $array0[$i1] = 0; + if (($i1>=$myfirstline)&&($i1<=$mylastline)) + { + if($line1=~/if\(ptr\[offset(\d+)/) + { + if($line1=~/if\(ptr\[offset(\d+).*\>.*cb/) + { + $tmp=$1; + } + else + { + if($line1=~/if\(ptr\[offset(\d+).*\<.*c\_b/) + { + $tmp=$1+128; + } + else + { + die "invalid array index!" + } + } + $array1[$ifcount1] = $tmp; + $array0[$ifcount1] = $i1; + $ifcount1++; + } + else + { + } + } + $i1++; + } + $is_not_a_corner=$ifcount1; + $is_a_corner=$ifcount1+1; + + close $in1 or die "Can't close $filein: $!"; + + open($in1, "<", $filein) or die "Can't open $filein: $!"; + + + $i1=1; + while (my $line1 = <$in1>) + { + chomp $line1; + if (($i1>=$myfirstline)&&($i1<=$mylastline)) + { + if ($array0[$ifcount2] == $i1) + { + $array2[$ifcount2]=0; + $array3[$ifcount2]=0; + if ($array0[$ifcount2+1] == ($i1+1)) + { + $array2[$ifcount2]=($ifcount2+1); + } + else + { + open(my $in2, "<", $filein) or die "Can't open $filein: $!"; + $i2=1; + while (my $line2 = <$in2>) + { + chomp $line2; + if ($i2 == $i1) + { + last; + } + $i2++; + } + my $line2 = <$in2>; + chomp $line2; + if ($line2=~/goto (\w+)/) + { + $tmp=$1; + if ($tmp eq "is_not_a_corner") + { + $array2[$ifcount2]=$is_not_a_corner; + } + if ($tmp eq "is_a_corner") + { + $array2[$ifcount2]=$is_a_corner; + } + } + else + { + die "goto expected: $!"; + } + close $in2 or die "Can't close $filein: $!"; + } + #find next else and interprete it + open(my $in3, "<", $filein) or die "Can't open $filein: $!"; + $i3=1; + $ifcount3=0; + $elsecount=0; + while (my $line3 = <$in3>) + { + chomp $line3; + $i3++; + if ($i3 == $i1) + { + last; + } + } + while (my $line3 = <$in3>) + { + chomp $line3; + $ifcount3++; + if (($elsecount==0)&&($i3>$i1)) + { + if ($line3=~/goto (\w+)/) + { + $tmp=$1; + if ($tmp eq "is_not_a_corner") + { + $array3[$ifcount2]=$is_not_a_corner; + } + if ($tmp eq "is_a_corner") + { + $array3[$ifcount2]=$is_a_corner; + } + } + else + { + if ($line3=~/if\(ptr\[offset/) + { + $ifcount4=0; + while ($array0[$ifcount4]!=$i3) + { + $ifcount4++; + if ($ifcount4==$ifcount1) + { + die "if else match expected: $!"; + } + $array3[$ifcount2]=$ifcount4; + } + } + else + { + die "elseif or elsegoto match expected: $!"; + } + } + last; + } + else + { + if ($line3=~/if\(ptr\[offset/) + { + $elsecount++; + } + else + { + if ($line3=~/else/) + { + $elsecount--; + } + } + } + $i3++; + } + printf("%3d [%3d][0x%08x]\n", $array0[$ifcount2], $ifcount2, (($array1[$ifcount2]&15)<<28)|($array2[$ifcount2]<<16)|(($array1[$ifcount2]&128)<<5)|($array3[$ifcount2])); + close $in3 or die "Can't close $filein: $!"; + $ifcount2++; + } + else + { + } + } + $i1++; + } + + printf(" [%3d][0x%08x]\n", $is_not_a_corner, 254); + printf(" [%3d][0x%08x]\n", $is_a_corner, 255); + + close $in1 or die "Can't close $filein: $!"; + + $ifcount0=0; + $ifcount2=0; + printf $out " static const unsigned long %s[] = {\n ", $tablename; + while ($ifcount0 < $ifcount1) + { + printf $out "0x%08x, ", (($array1[$ifcount0]&15)<<28)|($array2[$ifcount0]<<16)|(($array1[$ifcount0]&128)<<5)|($array3[$ifcount0]); + + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + + } + printf $out "0x%08x, ", 254; + $ifcount0++; + $ifcount2++; + if ($ifcount2==8) + { + $ifcount2=0; + printf $out "\n"; + printf $out " "; + } + printf $out "0x%08x\n", 255; + $ifcount0++; + $ifcount2++; + printf $out " };\n\n"; + + $#array0 = -1; + $#array1 = -1; + $#array2 = -1; + $#array3 = -1; + + close $out or die "Can't close $fileout: $!"; diff --git a/modules/features2d/doc/run_agast_tables.bat b/modules/features2d/doc/run_agast_tables.bat new file mode 100644 index 000000000..7d76e13f9 --- /dev/null +++ b/modules/features2d/doc/run_agast_tables.bat @@ -0,0 +1,32 @@ +perl read_file_score32.pl 9059 9385 table_5_8_corner_struct +move agast_new.txt agast_score_table.txt +perl read_file_score32.pl 2215 3387 table_7_12d_corner_struct +copy /A agast_score_table.txt + agast_new.txt agast_score_table.txt +del agast_new.txt +perl read_file_score32.pl 3428 9022 table_7_12s_corner_struct +copy /A agast_score_table.txt + agast_new.txt agast_score_table.txt +del agast_new.txt +perl read_file_score32.pl 118 2174 table_9_16_corner_struct +copy /A agast_score_table.txt + agast_new.txt agast_score_table.txt +del agast_new.txt + +perl read_file_nondiff32.pl 103 430 table_5_8_struct1 +move agast_new.txt agast_table.txt +perl read_file_nondiff32.pl 440 779 table_5_8_struct2 +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt +perl read_file_nondiff32.pl 869 2042 table_7_12d_struct1 +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt +perl read_file_nondiff32.pl 2052 3225 table_7_12d_struct2 +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt +perl read_file_nondiff32.pl 3315 4344 table_7_12s_struct1 +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt +perl read_file_nondiff32.pl 4354 5308 table_7_12s_struct2 +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt +perl read_file_nondiff32.pl 5400 7454 table_9_16_struct +copy /A agast_table.txt + agast_new.txt agast_table.txt +del agast_new.txt diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index 2542d537f..692d3d9fd 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -74,7 +74,7 @@ This section describes approaches based on local 2D features and used to categor - A complete Bag-Of-Words sample can be found at opencv_source_code/samples/cpp/bagofwords_classification.cpp - (Python) An example using the features2D framework to perform object categorization can be - found at opencv_source_code/samples/python2/find_obj.py + found at opencv_source_code/samples/python/find_obj.py @} */ @@ -331,7 +331,7 @@ than union-find method; it actually get 1.5~2m/s on my centrino L7200 1.2GHz lap than grey image method ( 3~4 times ); the chi_table.h file is taken directly from paper's source code which is distributed under GPL. -- (Python) A complete example showing the use of the %MSER detector can be found at samples/python2/mser.py +- (Python) A complete example showing the use of the %MSER detector can be found at samples/python/mser.py */ class CV_EXPORTS_W MSER : public Feature2D { @@ -447,6 +447,9 @@ circle around this pixel. AgastFeatureDetector::AGAST_5_8, AgastFeatureDetector::AGAST_7_12d, AgastFeatureDetector::AGAST_7_12s, AgastFeatureDetector::OAST_9_16 +For non-Intel platforms, there is a tree optimised variant of AGAST with same numerical results. +The 32-bit binary tree tables were generated automatically from original code using perl script. +The perl script and examples of tree generation are placed in features2d/doc folder. Detects corners using the AGAST algorithm by @cite mair2010_agast . */ diff --git a/modules/features2d/src/agast.cpp b/modules/features2d/src/agast.cpp index 95c74fbee..44f08384c 100644 --- a/modules/features2d/src/agast.cpp +++ b/modules/features2d/src/agast.cpp @@ -52,6 +52,8 @@ The references are: namespace cv { +#if (defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) + static void AGAST_5_8(InputArray _img, std::vector& keypoints, int threshold) { @@ -214,7 +216,8 @@ static void AGAST_5_8(InputArray _img, std::vector& keypoints, int thr goto homogeneous; else goto homogeneous; - else if(ptr[offset0] < c_b) + else + if(ptr[offset0] < c_b) if(ptr[offset2] < c_b) if(ptr[offset7] > cb) if(ptr[offset3] < c_b) @@ -562,7 +565,8 @@ static void AGAST_5_8(InputArray _img, std::vector& keypoints, int thr goto structured; else goto homogeneous; - else if(ptr[offset0] < c_b) + else + if(ptr[offset0] < c_b) if(ptr[offset2] < c_b) if(ptr[offset7] > cb) if(ptr[offset3] < c_b) @@ -3257,6 +3261,7 @@ static void AGAST_7_12d(InputArray _img, std::vector& keypoints, int t } } + static void AGAST_7_12s(InputArray _img, std::vector& keypoints, int threshold) { cv::Mat img; @@ -7442,12 +7447,502 @@ static void OAST_9_16(InputArray _img, std::vector& keypoints, int thr } + +#else // !(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) + +static void AGAST_ALL(InputArray _img, std::vector& keypoints, int threshold, int agasttype) +{ + cv::Mat img; + if(!_img.getMat().isContinuous()) + img = _img.getMat().clone(); + else + img = _img.getMat(); + + int agastbase; + int result; + uint32_t *table_struct1; + uint32_t *table_struct2; + static const uint32_t table_5_8_struct1[] = + { + 0x00010026,0x20020017,0x3003000c,0x50040009,0x10050007,0x406f0006,0x706f006c,0x4008006c, + 0x606f006c,0x100a006c,0x406d000b,0x706d006c,0x700d0012,0x600e006c,0x500f0011,0x106f0010, + 0x406f006c,0x106d006c,0x5013106c,0x3014106c,0x7015106c,0x4016106c,0x606f106e,0x5018001c, + 0x7019006c,0x601a006c,0x106d001b,0x406d006c,0x501d106c,0x301e106c,0x201f1023,0x10201021, + 0x406f106c,0x4022106c,0x606f106c,0x7024106c,0x4025106c,0x606f106c,0x00271058,0x20281049, + 0x70290035,0x302a1031,0x502b102f,0x102c102d,0x406f106e,0x402e106c,0x606f106e,0x1030106c, + 0x406f106c,0x5032006c,0x3033006c,0x4034006c,0x606f006e,0x70361041,0x3037103c,0x5038103b, + 0x106f1039,0x403a106c,0x606f106e,0x106d106c,0x603d106c,0x503e1040,0x106f103f,0x406f106c, + 0x106d106c,0x3042106c,0x50431047,0x10441045,0x406f106c,0x4046106c,0x606f106c,0x1048106c, + 0x406d106c,0x504a0053,0x304b006c,0x204c0050,0x104d004e,0x406f006c,0x404f006c,0x606f006c, + 0x7051006c,0x4052006c,0x606f006c,0x5054106c,0x7055106c,0x6056106c,0x106d1057,0x406d106c, + 0x30590062,0x505a006c,0x205b005f,0x105c005d,0x406d006c,0x405e006c,0x606d006c,0x7060006c, + 0x4061006c,0x606d006c,0x3063106c,0x5064106c,0x20651069,0x10661067,0x406d106c,0x4068106c, + 0x606d106c,0x706a106c,0x406b106c,0x606d106c,0x000000fc,0x000000fd,0x000000fe,0x000000ff + }; + + static const uint32_t table_5_8_struct2[] = + { + 0x0001002a,0x2002001b,0x30030010,0x5004000c,0x70050008,0x10730006,0x40070072,0x60730072, + 0x1009000a,0x40730072,0x400b0072,0x60730072,0x700d000e,0x10730072,0x100f0072,0x40730072, + 0x70110016,0x60120072,0x50130015,0x10730014,0x40730072,0x10730072,0x50171072,0x30181070, + 0x70191070,0x401a1072,0x60731072,0x501c0020,0x701d0072,0x601e0072,0x1073001f,0x40730072, + 0x50211070,0x30221072,0x20231027,0x10241025,0x40731072,0x40261072,0x60731072,0x70281070, + 0x40291070,0x60711070,0x002b105c,0x202c104d,0x702d0039,0x302e1035,0x502f1033,0x10301031, + 0x40731072,0x40321072,0x60731072,0x10341072,0x40731072,0x50360072,0x30370070,0x40380072, + 0x60730072,0x703a1045,0x303b1040,0x503c103f,0x1073103d,0x403e1072,0x60731072,0x10731072, + 0x60411072,0x50421044,0x10731043,0x40731072,0x10731072,0x30461070,0x5047104b,0x10481049, + 0x40711070,0x404a1070,0x60711070,0x104c1070,0x40711070,0x504e0057,0x304f0072,0x20500054, + 0x10510052,0x40730072,0x40530072,0x60730072,0x70550070,0x40560070,0x60710070,0x50581070, + 0x70591072,0x605a1072,0x1073105b,0x40731072,0x305d0066,0x505e0070,0x205f0063,0x10600061, + 0x40710070,0x40620070,0x60710070,0x70640070,0x40650070,0x60710070,0x30671070,0x50681070, + 0x2069106d,0x106a106b,0x40711070,0x406c1070,0x60711070,0x706e1070,0x406f1070,0x60711070, + 0x000000fc,0x000000fd,0x000000fe,0x000000ff + }; + + static const uint32_t table_7_12d_struct1[] = + { + 0x000100b5,0x50020036,0x20030025,0x9004001d,0x10050015,0x6006000f,0x3007000a,0x41870008, + 0xa0090186,0xb1890186,0x800b0186,0xa00c0186,0xb189000d,0x400e0186,0x71890188,0xb0100186, + 0x30110013,0x41870012,0xa1870186,0x80140186,0xa1870186,0x60160186,0x70170186,0x80180186, + 0x4019001b,0x3189001a,0xa1890186,0xa01c0186,0xb1890186,0x301e0186,0x401f0186,0x10200022, + 0x61870021,0xb1870186,0x60230186,0x70240186,0x81870186,0x90260186,0x70270186,0x80280186, + 0x10290030,0xa02a002d,0xb187002b,0x602c0186,0x41890186,0x602e0186,0x302f0186,0x41890186, + 0x60310186,0x40320034,0x31870033,0xa1870186,0xa0350186,0xb1870186,0x503710a1,0x9038006b, + 0x3039105b,0x403a1053,0xb03b004d,0x103c0044,0x803d0040,0xa03e0186,0x2189003f,0x71890188, + 0x60411186,0x20421186,0x70431188,0x81891188,0x60450048,0x70460186,0x80470186,0xa1890188, + 0x60491186,0x204a1186,0x704b1186,0x1189104c,0x81891188,0x204e1186,0x704f1186,0x10501051, + 0x61891186,0x60521186,0x81891186,0xb0540186,0x80550186,0xa0560186,0x10570059,0x21890058, + 0x71890186,0x605a0186,0x71890186,0xb05c0186,0xa05d0186,0x305e0065,0x105f0062,0x21870060, + 0x70610186,0x81890186,0x60630186,0x70640186,0x81890186,0x80660186,0x10670069,0x21870068, + 0x71870186,0x606a0186,0x71870186,0x906c1093,0x206d0087,0x106e007f,0x406f0077,0xa0700072, + 0x30710186,0xb1890186,0x60731186,0x70741186,0x80751186,0xb0761188,0xa1891188,0x60781186, + 0x70791186,0x807a1186,0xa07b107d,0x4189107c,0xb1891188,0x307e1186,0x41891188,0x60801186, + 0x70811186,0x80821186,0x40831085,0x31891084,0xa1891186,0xa0861186,0xb1891186,0x60881186, + 0x70891186,0x808a108f,0x408b108d,0x3187108c,0xa1871186,0xa08e1186,0xb1871186,0x20901186, + 0x10911186,0x30921186,0x41891186,0x20940099,0x10950186,0x30960186,0x40970186,0xa0980186, + 0xb1870186,0x209a1186,0x309b1186,0x409c1186,0x709d1186,0x109e109f,0x61871186,0x60a01186, + 0x81871186,0x20a200ae,0xa0a30186,0xb0a40186,0x90a500ab,0x10a600a8,0x318700a7,0x81870186, + 0x60a90186,0x70aa0186,0x81870186,0x10ac0186,0x30ad0186,0x41870186,0x90af0186,0x70b00186, + 0x80b10186,0xa0b20186,0xb0b30186,0x118700b4,0x61870186,0x00b6115a,0x20b700e2,0x50b800cc, + 0x70b900c5,0x60ba0186,0x40bb00c1,0x30bc00be,0x118700bd,0x81870186,0x90bf0186,0x80c00186, + 0xa1890186,0x90c20186,0x80c30186,0xa0c40186,0xb1890186,0x90c61186,0x80c71186,0xa0c81186, + 0xb0c91186,0x70ca1186,0x118910cb,0x61891186,0x90cd1186,0x70ce1186,0x80cf1186,0x50d010de, + 0x10d110d8,0xa0d210d5,0xb18910d3,0x60d41186,0x41891188,0x60d61186,0x30d71186,0x41891188, + 0x60d91186,0x40da10dc,0x318910db,0xa1891186,0xa0dd1186,0xb1891186,0xa0df1186,0xb0e01186, + 0x118710e1,0x61871186,0x20e3113a,0x90e4010b,0x50e500ff,0x10e610f7,0x40e710ef,0xa0e810ea, + 0x30e91186,0xb1891186,0x60eb0186,0x70ec0186,0x80ed0186,0xb0ee0188,0xa1890188,0x60f00186, + 0x70f10186,0x80f20186,0xa0f300f5,0x418900f4,0xb1890188,0x30f60186,0x41890188,0x60f80186, + 0x70f90186,0x80fa0186,0x40fb00fd,0x318900fc,0xa1890186,0xa0fe0186,0xb1890186,0x31001186, + 0x41011186,0x51021108,0x11031105,0x61871104,0xb1871186,0x61061186,0x71071186,0x81891186, + 0x11091186,0xa10a1186,0xb1871186,0x910c112e,0x510d1126,0x110e111e,0x610f1118,0x31101113, + 0x41871111,0xa1121186,0xb1891186,0x81141186,0xa1151186,0xb1891116,0x41171186,0x71891188, + 0xb1191186,0x311a111c,0x4187111b,0xa1871186,0x811d1186,0xa1871186,0x611f1186,0x71201186, + 0x81211186,0x41221124,0x31891123,0xa1891186,0xa1251186,0xb1891186,0xa1271186,0xb1281186, + 0x1129112b,0x3187112a,0x81871186,0x612c1186,0x712d1186,0x81871186,0x312f1186,0x41301186, + 0x51311137,0x11321134,0x61871133,0xb1871186,0x61351186,0x71361186,0x81871186,0x11381186, + 0xa1391186,0xb1871186,0x913b1150,0x713c1186,0x813d1186,0x513e114c,0x113f1146,0xa1401143, + 0xb1871141,0x61421186,0x41891186,0x61441186,0x31451186,0x41891186,0x61471186,0x4148114a, + 0x31871149,0xa1871186,0xa14b1186,0xb1871186,0xa14d1186,0xb14e1186,0x1187114f,0x61871186, + 0x51510186,0x91520186,0x61530186,0x71540186,0x81550186,0x41560158,0x31870157,0xa1870186, + 0xa1590186,0xb1870186,0x515b0170,0x915c0168,0x615d0186,0x715e0186,0x415f0165,0x31600163, + 0x81870161,0x11620186,0x21870186,0x81640186,0xa1870186,0xb1660186,0x81670186,0xa1870186, + 0x21690186,0x316a0186,0x416b0186,0x716c0186,0x116d016e,0x61870186,0x616f0186,0x81870186, + 0x51711186,0x9172117e,0x61731186,0x71741186,0x4175117b,0x31761179,0x81871177,0x11781186, + 0x21871186,0x817a1186,0xa1871186,0xb17c1186,0x817d1186,0xa1871186,0x217f1186,0x31801186, + 0x41811186,0x71821186,0x11831184,0x61871186,0x61851186,0x81871186,0x000000fc,0x000000fd, + 0x000000fe,0x000000ff + }; + + static const uint32_t table_7_12d_struct2[] = + { + 0x000100b5,0x50020036,0x20030025,0x9004001d,0x10050015,0x6006000f,0x3007000a,0x41890008, + 0xa0090188,0xb1890188,0x800b0188,0xa00c0188,0xb189000d,0x400e0188,0x71890188,0xb0100188, + 0x30110013,0x41890012,0xa1890188,0x80140188,0xa1890188,0x60160188,0x70170188,0x80180188, + 0x4019001b,0x3189001a,0xa1890188,0xa01c0188,0xb1890188,0x301e0188,0x401f0188,0x10200022, + 0x61890021,0xb1890188,0x60230188,0x70240188,0x81890188,0x90260188,0x70270188,0x80280188, + 0x10290030,0xa02a002d,0xb189002b,0x602c0188,0x41890188,0x602e0188,0x302f0188,0x41890188, + 0x60310188,0x40320034,0x31890033,0xa1890188,0xa0350188,0xb1890188,0x503710a1,0x9038006b, + 0x3039105b,0x403a1053,0xb03b004d,0x103c0044,0x803d0040,0xa03e0188,0x2189003f,0x71890188, + 0x60411188,0x20421188,0x70431188,0x81891188,0x60450048,0x70460188,0x80470188,0xa1890188, + 0x60491188,0x204a1188,0x704b1188,0x1189104c,0x81891188,0x204e1188,0x704f1188,0x10501051, + 0x61891188,0x60521188,0x81891188,0xb0540188,0x80550188,0xa0560188,0x10570059,0x21890058, + 0x71890188,0x605a0188,0x71890188,0xb05c0188,0xa05d0188,0x305e0065,0x105f0062,0x21890060, + 0x70610188,0x81890188,0x60630188,0x70640188,0x81890188,0x80660188,0x10670069,0x21890068, + 0x71890188,0x606a0188,0x71890188,0x906c1093,0x206d0087,0x106e007f,0x406f0077,0xa0700072, + 0x30710188,0xb1890188,0x60731188,0x70741188,0x80751188,0xb0761188,0xa1891188,0x60781188, + 0x70791188,0x807a1188,0xa07b107d,0x4189107c,0xb1891188,0x307e1188,0x41891188,0x60801188, + 0x70811188,0x80821188,0x40831085,0x31891084,0xa1891188,0xa0861188,0xb1891188,0x60881188, + 0x70891188,0x808a108f,0x408b108d,0x3189108c,0xa1891188,0xa08e1188,0xb1891188,0x20901188, + 0x10911188,0x30921188,0x41891188,0x20940099,0x10950188,0x30960188,0x40970188,0xa0980188, + 0xb1890188,0x209a1186,0x309b1188,0x409c1188,0x709d1188,0x109e109f,0x61891188,0x60a01188, + 0x81891188,0x20a200ae,0xa0a30188,0xb0a40188,0x90a500ab,0x10a600a8,0x318900a7,0x81890188, + 0x60a90188,0x70aa0188,0x81890188,0x10ac0188,0x30ad0188,0x41890188,0x90af0188,0x70b00188, + 0x80b10188,0xa0b20188,0xb0b30188,0x118900b4,0x61890188,0x00b6115a,0x20b700e2,0x50b800cc, + 0x70b900c5,0x60ba0188,0x40bb00c1,0x30bc00be,0x118900bd,0x81890188,0x90bf0188,0x80c00188, + 0xa1890188,0x90c20188,0x80c30188,0xa0c40188,0xb1890188,0x90c61188,0x80c71188,0xa0c81188, + 0xb0c91188,0x70ca1188,0x118910cb,0x61891188,0x90cd1188,0x70ce1188,0x80cf1188,0x50d010de, + 0x10d110d8,0xa0d210d5,0xb18910d3,0x60d41188,0x41891188,0x60d61188,0x30d71188,0x41891188, + 0x60d91188,0x40da10dc,0x318910db,0xa1891188,0xa0dd1188,0xb1891188,0xa0df1188,0xb0e01188, + 0x118910e1,0x61891188,0x20e3113a,0x90e4010b,0x50e500ff,0x10e610f7,0x40e710ef,0xa0e810ea, + 0x30e91188,0xb1891188,0x60eb0188,0x70ec0188,0x80ed0188,0xb0ee0188,0xa1890188,0x60f00188, + 0x70f10188,0x80f20188,0xa0f300f5,0x418900f4,0xb1890188,0x30f60188,0x41890188,0x60f80188, + 0x70f90188,0x80fa0188,0x40fb00fd,0x318900fc,0xa1890188,0xa0fe0188,0xb1890188,0x31001188, + 0x41011188,0x51021108,0x11031105,0x61891104,0xb1891188,0x61061188,0x71071188,0x81891188, + 0x11091188,0xa10a1188,0xb1891188,0x910c112e,0x510d1126,0x110e111e,0x610f1118,0x31101113, + 0x41891111,0xa1121188,0xb1891188,0x81141188,0xa1151188,0xb1891116,0x41171188,0x71891188, + 0xb1191188,0x311a111c,0x4189111b,0xa1891188,0x811d1188,0xa1891188,0x611f1188,0x71201188, + 0x81211188,0x41221124,0x31891123,0xa1891188,0xa1251188,0xb1891188,0xa1271188,0xb1281188, + 0x1129112b,0x3189112a,0x81891188,0x612c1188,0x712d1188,0x81891188,0x312f1188,0x41301188, + 0x51311137,0x11321134,0x61891133,0xb1891188,0x61351188,0x71361188,0x81891188,0x11381188, + 0xa1391188,0xb1891188,0x913b1150,0x713c1188,0x813d1188,0x513e114c,0x113f1146,0xa1401143, + 0xb1891141,0x61421188,0x41891188,0x61441188,0x31451188,0x41891188,0x61471188,0x4148114a, + 0x31891149,0xa1891188,0xa14b1188,0xb1891188,0xa14d1188,0xb14e1188,0x1189114f,0x61891188, + 0x51510188,0x91520186,0x61530188,0x71540188,0x81550188,0x41560158,0x31890157,0xa1890188, + 0xa1590188,0xb1890188,0x515b0170,0x915c0168,0x615d0188,0x715e0188,0x415f0165,0x31600163, + 0x81890161,0x11620188,0x21890188,0x81640188,0xa1890188,0xb1660188,0x81670188,0xa1890188, + 0x21690188,0x316a0188,0x416b0188,0x716c0188,0x116d016e,0x61890188,0x616f0188,0x81890188, + 0x51711186,0x9172117e,0x61731188,0x71741188,0x4175117b,0x31761179,0x81891177,0x11781188, + 0x21891188,0x817a1188,0xa1891188,0xb17c1188,0x817d1188,0xa1891188,0x217f1188,0x31801188, + 0x41811188,0x71821188,0x11831184,0x61891188,0x61851188,0x81891188,0x000000fc,0x000000fd, + 0x000000fe,0x000000ff + }; + + static const uint32_t table_7_12s_struct1[] = + { + 0x00010091,0x20020064,0x50030031,0x90040026,0x7005001c,0x10060015,0x6007000f,0x3008000b, + 0x41590009,0xa00a0156,0xb1590158,0x800c0156,0xa00d0156,0x4159000e,0xb1590158,0xb0100156, + 0x30110013,0x41590012,0xa1590156,0x80140156,0xa1590156,0x60160156,0x80170156,0x4018001a, + 0x31590019,0xa1590156,0xa01b0156,0xb1590156,0x101d0156,0xb01e0023,0x301f0021,0x41570020, + 0xa1570156,0x80220156,0xa1570156,0x60240156,0x30250156,0x41570156,0x30270156,0x40280156, + 0x7029002e,0x102a002c,0x6157002b,0xb1570156,0x602d0156,0x81570156,0x102f0156,0x61570030, + 0xb1570156,0x90321055,0x70331050,0x5034104b,0x10350044,0x4036003d,0xa0370039,0x30380156, + 0xb1590158,0x603a1156,0x803b1156,0xb03c1158,0xa1591158,0x603e1156,0x803f1156,0xa0401042, + 0x41591041,0xb1591158,0x30431156,0x41591158,0x60451156,0x80461156,0x40471049,0x31591048, + 0xa1591156,0xa04a1156,0xb1591156,0x104c0156,0x304d0156,0x404e0156,0xa04f0156,0xb1590156, + 0x10510156,0x30520156,0x40530156,0xa0540156,0xb1570156,0xa0560156,0xb0570156,0x90580061, + 0x7059005e,0x105a005c,0x3157005b,0x81570156,0x605d0156,0x81570156,0x105f0156,0x31570060, + 0x81570156,0x10620156,0x30630156,0x41570156,0x7065007a,0x90660156,0x80670156,0x50680076, + 0x10690070,0xa06a006d,0xb157006b,0x606c0156,0x41590156,0x606e0156,0x306f0156,0x41590156, + 0x60710156,0x40720074,0x31570073,0xa1570156,0xa0750156,0xb1570156,0xa0770156,0xb0780156, + 0x11570079,0x61570156,0x707b1156,0x507c1156,0x207d1089,0x607e1156,0x407f1085,0x30801082, + 0x11571081,0x81571156,0x90831156,0x80841156,0xa1591156,0x90861156,0x80871156,0xa0881156, + 0xb1591156,0x908a1156,0x608b1156,0x808c1156,0x408d108f,0x3157108e,0xa1571156,0xa0901156, + 0xb1571156,0x0092112c,0x209310ff,0x909410c2,0x509510b7,0x709610ad,0x109710a6,0x609810a0, + 0x3099109c,0x4159109a,0xa09b1156,0xb1591158,0x809d1156,0xa09e1156,0x4159109f,0xb1591158, + 0xb0a11156,0x30a210a4,0x415910a3,0xa1591156,0x80a51156,0xa1591156,0x60a71156,0x80a81156, + 0x40a910ab,0x315910aa,0xa1591156,0xa0ac1156,0xb1591156,0x10ae1156,0xb0af10b4,0x30b010b2, + 0x415710b1,0xa1571156,0x80b31156,0xa1571156,0x60b51156,0x30b61156,0x41571156,0xa0b81156, + 0xb0b91156,0x70ba10bf,0x10bb10bd,0x315710bc,0x81571156,0x60be1156,0x81571156,0x10c01156, + 0x315710c1,0x81571156,0x90c300f0,0x50c400e1,0x70c500dc,0x10c610d5,0x40c710ce,0xa0c810ca, + 0x30c91156,0xb1591158,0x60cb0156,0x80cc0156,0xb0cd0158,0xa1590158,0x60cf0156,0x80d00156, + 0xa0d100d3,0x415900d2,0xb1590158,0x30d40156,0x41590158,0x60d60156,0x80d70156,0x40d800da, + 0x315900d9,0xa1590156,0xa0db0156,0xb1590156,0x10dd1156,0x30de1156,0x40df1156,0xa0e01156, + 0xb1591156,0x30e21156,0x40e31156,0x50e410ed,0x70e510ea,0x10e610e8,0x615910e7,0xb1591156, + 0x60e91156,0x81591156,0x10eb1156,0x615710ec,0xb1571156,0x10ee1156,0xa0ef1156,0xb1571156, + 0x30f11156,0x40f21156,0x50f310fc,0x70f410f9,0x10f510f7,0x615710f6,0xb1571156,0x60f81156, + 0x81571156,0x10fa1156,0x615710fb,0xb1571156,0x10fd1156,0xa0fe1156,0xb1571156,0x71000116, + 0x51010156,0x2102010e,0x61030156,0x4104010a,0x31050107,0x11570106,0x81570156,0x91080156, + 0x81090156,0xa1590156,0x910b0156,0x810c0156,0xa10d0156,0xb1590156,0x910f0156,0x61100156, + 0x81110156,0x41120114,0x31570113,0xa1570156,0xa1150156,0xb1570156,0x71171156,0x91181156, + 0x81191156,0x511a1128,0x111b1122,0xa11c111f,0xb157111d,0x611e1156,0x41591156,0x61201156, + 0x31211156,0x41591156,0x61231156,0x41241126,0x31571125,0xa1571156,0xa1271156,0xb1571156, + 0xa1291156,0xb12a1156,0x1157112b,0x61571156,0x512d0141,0x712e0156,0x912f013a,0x61300156, + 0x41310137,0x31320135,0x81570133,0x11340156,0x21570156,0x81360156,0xa1570156,0xb1380156, + 0x81390156,0xa1570156,0x213b0156,0x313c0156,0x413d0156,0x113e013f,0x61570156,0x61400156, + 0x81570156,0x51421156,0x71431156,0x9144114f,0x61451156,0x4146114c,0x3147114a,0x81571148, + 0x11491156,0x21571156,0x814b1156,0xa1571156,0xb14d1156,0x814e1156,0xa1571156,0x21501156, + 0x31511156,0x41521156,0x11531154,0x61571156,0x61551156,0x81571156,0x000000fc,0x000000fd, + 0x000000fe,0x000000ff + }; + + static const uint32_t table_7_12s_struct2[] = + { + 0x00010092,0x20020065,0x50030031,0x90040026,0x7005001c,0x10060015,0x6007000f,0x3008000b, + 0x41400009,0xa00a013f,0xb140013f,0x800c013f,0xa00d013f,0x4140000e,0xb140013f,0xb010013f, + 0x30110013,0x41400012,0xa140013f,0x8014013f,0xa140013f,0x6016013f,0x8017013f,0x4018001a, + 0x31400019,0xa140013f,0xa01b013f,0xb140013f,0x101d013f,0xb01e0023,0x301f0021,0x41400020, + 0xa140013f,0x8022013f,0xa140013f,0x6024013f,0x3025013f,0x4140013f,0x3027013f,0x4028013f, + 0x7029002e,0x102a002c,0x6140002b,0xb140013f,0x602d013f,0x8140013f,0x102f013f,0x61400030, + 0xb140013f,0x70321059,0x90331050,0x5034104b,0x10350044,0x4036003d,0xa0370039,0x3038013f, + 0xb140013f,0x603a113f,0x803b113f,0xb03c113f,0xa140113f,0x603e113f,0x803f113f,0xa0401042, + 0x41401041,0xb140113f,0x3043113f,0x4140113f,0x6045113f,0x8046113f,0x40471049,0x31401048, + 0xa140113f,0xa04a113f,0xb140113f,0x104c013f,0x304d013f,0x404e013f,0xa04f013f,0xb140013f, + 0xa051013f,0xb052013f,0x90530056,0x1054013f,0x31400055,0x8140013f,0x1057013f,0x3058013f, + 0x4140013f,0xa05a013f,0xb05b013f,0x905c0062,0x105d005f,0x3140005e,0x8140013f,0x6060013f, + 0x8061013f,0x7140013f,0x1063013f,0x3064013f,0x4140013f,0x7066007b,0x9067013f,0x8068013f, + 0x50690077,0x106a0071,0xa06b006e,0xb140006c,0x606d013f,0x4140013f,0x606f013f,0x3070013f, + 0x4140013f,0x6072013f,0x40730075,0x31400074,0xa140013f,0xa076013f,0xb140013f,0xa078013f, + 0xb079013f,0x1140007a,0x6140013f,0x707c113f,0x507d113f,0x207e108a,0x607f113f,0x40801086, + 0x30811083,0x11401082,0x8140113f,0x9084113f,0x8085113f,0xa140113f,0x9087113f,0x8088113f, + 0xa089113f,0xb140113f,0x908b113f,0x608c113f,0x808d113f,0x408e1090,0x3140108f,0xa140113f, + 0xa091113f,0xb140113f,0x00931113,0x209410e6,0xb09510c8,0x309610b9,0x509710a9,0x909810a3, + 0x709910a0,0x109a109c,0x4140109b,0xa140113f,0x609d113f,0x809e113f,0x4140109f,0xa140113f, + 0x10a1113f,0x414010a2,0xa140113f,0x40a4113f,0x70a510a8,0x114010a6,0x60a7113f,0x8140113f, + 0x1140113f,0xa0aa10b2,0x90ab10b0,0x70ac10af,0x114010ad,0x60ae113f,0x8140113f,0x1140113f, + 0x10b1113f,0x4140113f,0x70b3013f,0x90b4013f,0x50b5013f,0x40b6013f,0x60b7013f,0x80b8013f, + 0xa140013f,0x90ba10c0,0x80bb113f,0xa0bc113f,0x70bd10bf,0x114010be,0x6140113f,0x1140113f, + 0x50c1013f,0x70c2013f,0x90c3013f,0x40c4013f,0x60c5013f,0x80c6013f,0x314000c7,0xa140013f, + 0x40c910dc,0x50ca10d5,0x70cb10d2,0x60cc113f,0x30cd10cf,0x114010ce,0x8140113f,0x90d0113f, + 0x80d1113f,0xa140113f,0x10d3113f,0x60d4113f,0x3140113f,0x70d6013f,0x90d7013f,0x50d8013f, + 0x60d9013f,0x80da013f,0xa0db013f,0xb140013f,0x50dd013f,0x70de013f,0x90df013f,0x60e0013f, + 0x80e1013f,0xa0e200e4,0x414000e3,0xb140013d,0x30e5013f,0x4140013f,0x70e700fd,0x50e8013f, + 0x20e900f5,0x60ea013f,0x40eb00f1,0x30ec00ee,0x114000ed,0x8140013f,0x90ef013f,0x80f0013f, + 0xa140013f,0x90f2013f,0x80f3013f,0xa0f4013f,0xb140013f,0x90f6013f,0x60f7013f,0x80f8013f, + 0x40f900fb,0x314000fa,0xa140013f,0xa0fc013f,0xb140013f,0x70fe113f,0x90ff113f,0x8100113f, + 0x5101110f,0x11021109,0xa1031106,0xb1401104,0x6105113f,0x4140113f,0x6107113f,0x3108113f, + 0x4140113f,0x610a113f,0x410b110d,0x3140110c,0xa140113f,0xa10e113f,0xb140113f,0xa110113f, + 0xb111113f,0x11401112,0x6140113f,0x51140128,0x7115013f,0x91160121,0x6117013f,0x4118011e, + 0x3119011c,0x8140011a,0x111b013f,0x2140013f,0x811d013f,0xa140013f,0xb11f013f,0x8120013f, + 0xa140013f,0x2122013f,0x3123013f,0x4124013f,0x11250126,0x6140013f,0x6127013f,0x8140013f, + 0x5129113d,0x712a113f,0x912b1136,0x612c113f,0x412d1133,0x312e1131,0x8140112f,0x1130113f, + 0x2140113f,0x8132113f,0xa140113f,0xb134113f,0x8135113f,0xa140113f,0x2137113f,0x3138113f, + 0x4139113f,0x113a113b,0x6140113f,0x613c113f,0x8140113f,0x000000fc,0x000000fd,0x000000fe, + 0x000000ff + }; + + static const uint32_t table_9_16_struct[] = + { + 0x00010138,0x200200d3,0x4003008a,0x50040051,0x70050027,0x30060016,0x1007000d,0x6008000a, + 0x82ad0009,0xf2ad02ac,0xd00b02ac,0xe00c02ac,0xf2ad02ac,0x800e02ac,0x900f02ac,0xa01002ac, + 0x62ad0011,0xb01202ac,0xc01302ac,0xd01402ac,0xe01502ac,0xf2ad02ac,0xa01702ac,0xb01802ac, + 0xc01902ac,0x801a0023,0x901b001f,0x62ad001c,0xd01d02ac,0xe01e02ac,0xf2ad02ac,0x102002ac, + 0xd02102ac,0xe02202ac,0xf2ad02ac,0x102402ac,0xd02502ac,0xe02602ac,0xf2ad02ac,0x70281041, + 0xe0290038,0xf02a02ac,0x102b0032,0x302c002e,0x62ad002d,0xd2ad02ac,0xa02f02ac,0xb03002ac, + 0xc03102ac,0xd2ad02ac,0x803302ac,0x903402ac,0xa03502ac,0xb03602ac,0xc03702ac,0xd2ad02ac, + 0xe03912ac,0x803a12ac,0x903b12ac,0xa03c12ac,0xb03d12ac,0xc03e12ac,0xd03f12ac,0x62ad1040, + 0xf2ad12ac,0xe04202ac,0xf04302ac,0x1044004b,0x30450047,0x62ad0046,0xd2ad02ac,0xa04802ac, + 0xb04902ac,0xc04a02ac,0xd2ad02ac,0x804c02ac,0x904d02ac,0xa04e02ac,0xb04f02ac,0xc05002ac, + 0xd2ad02ac,0x5052106e,0xc0530064,0xd05402ac,0xe05502ac,0xf056005e,0x1057005a,0x32ad0058, + 0xa05902ac,0xb2ad02ac,0x805b02ac,0x905c02ac,0xa05d02ac,0xb2ad02ac,0x605f02ac,0x706002ac, + 0x806102ac,0x906202ac,0xa06302ac,0xb2ad02ac,0xc06512ac,0x706612ac,0x806712ac,0x906812ac, + 0xa06912ac,0xb06a12ac,0xd06b12ac,0x62ad106c,0xe06d12ac,0xf2ad12ac,0xc06f0080,0xd07002ac, + 0xe07102ac,0xf072007a,0x10730076,0x32ad0074,0xa07502ac,0xb2ad02ac,0x807702ac,0x907802ac, + 0xa07902ac,0xb2ad02ac,0x607b02ac,0x707c02ac,0x807d02ac,0x907e02ac,0xa07f02ac,0xb2ad02ac, + 0xc08112ac,0x708212ac,0x808312ac,0x908412ac,0xa08512ac,0xb08612ac,0xd08712ac,0xe08812ac, + 0x62ad1089,0xf2ad12ac,0x408b10b1,0xb08c00a1,0xc08d02ac,0xd08e02ac,0xa08f009d,0xe0900098, + 0xf0910094,0x12ad0092,0x809302ac,0x92ad02ac,0x609502ac,0x709602ac,0x809702ac,0x92ad02ac, + 0x509902ac,0x609a02ac,0x709b02ac,0x809c02ac,0x92ad02ac,0x109e02ac,0x309f02ac,0xe0a002ac, + 0xf2ad02ac,0xb0a212ac,0x70a312ac,0x80a412ac,0x90a512ac,0xa0a612ac,0x60a710ad,0x50a810aa, + 0x32ad10a9,0xc2ad12ac,0xc0ab12ac,0xd0ac12ac,0xe2ad12ac,0xc0ae12ac,0xd0af12ac,0xe0b012ac, + 0xf2ad12ac,0xb0b200c7,0xc0b302ac,0xd0b402ac,0xa0b500c3,0xe0b600be,0xf0b700ba,0x12ad00b8, + 0x80b902ac,0x92ad02ac,0x60bb02ac,0x70bc02ac,0x80bd02ac,0x92ad02ac,0x50bf02ac,0x60c002ac, + 0x70c102ac,0x80c202ac,0x92ad02ac,0x10c402ac,0x30c502ac,0xe0c602ac,0xf2ad02ac,0xb0c812ac, + 0x70c912ac,0x80ca12ac,0x90cb12ac,0xa0cc12ac,0xc0cd12ac,0xd0ce12ac,0x60cf10d1,0x52ad10d0, + 0xe2ad12ac,0xe0d212ac,0xf2ad12ac,0x20d4110a,0x90d500ef,0xa0d602ac,0xb0d702ac,0x80d800ea, + 0xc0d900e5,0xd0da00e1,0xe0db00de,0xf2ad00dc,0x60dd02ac,0x72ad02ac,0x50df02ac,0x60e002ac, + 0x72ad02ac,0x40e202ac,0x50e302ac,0x60e402ac,0x72ad02ac,0x30e602ac,0x40e702ac,0x50e802ac, + 0x60e902ac,0x72ad02ac,0x10eb02ac,0xc0ec02ac,0xd0ed02ac,0xe0ee02ac,0xf2ad02ac,0x90f012ac, + 0x70f112ac,0x80f212ac,0x60f31104,0x50f410ff,0x40f510fb,0x30f610f8,0x12ad10f7,0xa2ad12ac, + 0xa0f912ac,0xb0fa12ac,0xc2ad12ac,0xa0fc12ac,0xb0fd12ac,0xc0fe12ac,0xd2ad12ac,0xa10012ac, + 0xb10112ac,0xc10212ac,0xd10312ac,0xe2ad12ac,0xa10512ac,0xb10612ac,0xc10712ac,0xd10812ac, + 0xe10912ac,0xf2ad12ac,0x910b0125,0xa10c02ac,0xb10d02ac,0x810e0120,0xc10f011b,0xd1100117, + 0xe1110114,0xf2ad0112,0x611302ac,0x72ad02ac,0x511502ac,0x611602ac,0x72ad02ac,0x411802ac, + 0x511902ac,0x611a02ac,0x72ad02ac,0x311c02ac,0x411d02ac,0x511e02ac,0x611f02ac,0x72ad02ac, + 0x112102ac,0xc12202ac,0xd12302ac,0xe12402ac,0xf2ad02ac,0x912612ac,0x712712ac,0x812812ac, + 0xa12912ac,0xb12a12ac,0x612b1134,0x512c1131,0x412d112f,0x32ad112e,0xc2ad12ac,0xc13012ac, + 0xd2ad12ac,0xc13212ac,0xd13312ac,0xe2ad12ac,0xc13512ac,0xd13612ac,0xe13712ac,0xf2ad12ac, + 0x01391270,0x213a0170,0x913b0155,0x713c02ac,0x813d02ac,0x613e014f,0x513f014a,0x41400146, + 0x31410143,0x12ad0142,0xa2ad02ac,0xa14402ac,0xb14502ac,0xc2ad02ac,0xa14702ac,0xb14802ac, + 0xc14902ac,0xd2ad02ac,0xa14b02ac,0xb14c02ac,0xc14d02ac,0xd14e02ac,0xe2ad02ac,0xa15002ac, + 0xb15102ac,0xc15202ac,0xd15302ac,0xe15402ac,0xf2ad02ac,0x915612ac,0xa15712ac,0xb15812ac, + 0x8159116b,0xc15a1166,0xd15b1162,0xe15c115f,0xf2ad115d,0x615e12ac,0x72ad12ac,0x516012ac, + 0x616112ac,0x72ad12ac,0x416312ac,0x516412ac,0x616512ac,0x72ad12ac,0x316712ac,0x416812ac, + 0x516912ac,0x616a12ac,0x72ad12ac,0x116c12ac,0xc16d12ac,0xd16e12ac,0xe16f12ac,0xf2ad12ac, + 0x21711242,0x41720198,0xb1730182,0x717402ac,0x817502ac,0x917602ac,0xa17702ac,0x6178017e, + 0x5179017b,0x32ad017a,0xc2ad02ac,0xc17c02ac,0xd17d02ac,0xe2ad02ac,0xc17f02ac,0xd18002ac, + 0xe18102ac,0xf2ad02ac,0xb18312ac,0xc18412ac,0xd18512ac,0xa1861194,0xe187118f,0xf188118b, + 0x12ad1189,0x818a12ac,0x92ad12ac,0x618c12ac,0x718d12ac,0x818e12ac,0x92ad12ac,0x519012ac, + 0x619112ac,0x719212ac,0x819312ac,0x92ad12ac,0x119512ac,0x319612ac,0xe19712ac,0xf2ad12ac, + 0x41991220,0x519a01b6,0xc19b01a4,0x719c02ac,0x819d02ac,0x919e02ac,0xa19f02ac,0xb1a002ac, + 0xd1a102ac,0x62ad01a2,0xe1a302ac,0xf2ad02ac,0xc1a512ac,0xd1a612ac,0xe1a712ac,0xf1a811b0, + 0x11a911ac,0x32ad11aa,0xa1ab12ac,0xb2ad12ac,0x81ad12ac,0x91ae12ac,0xa1af12ac,0xb2ad12ac, + 0x61b112ac,0x71b212ac,0x81b312ac,0x91b412ac,0xa1b512ac,0xb2ad12ac,0x51b71204,0x71b801d1, + 0xe1b901c1,0x81ba02ac,0x91bb02ac,0xa1bc02ac,0xb1bd02ac,0xc1be02ac,0xd1bf02ac,0x62ad01c0, + 0xf2ad02ac,0xe1c212ac,0xf1c312ac,0x11c411cb,0x31c511c7,0x62ad11c6,0xd2ad12ac,0xa1c812ac, + 0xb1c912ac,0xc1ca12ac,0xd2ad12ac,0x81cc12ac,0x91cd12ac,0xa1ce12ac,0xb1cf12ac,0xc1d012ac, + 0xd2ad12ac,0x71d211f4,0x31d311e3,0x11d411da,0x61d511d7,0x82ad11d6,0xf2ad12ac,0xd1d812ac, + 0xe1d912ac,0xf2ad12ac,0x81db12ac,0x91dc12ac,0xa1dd12ac,0x62ad11de,0xb1df12ac,0xc1e012ac, + 0xd1e112ac,0xe1e212ac,0xf2ad12ac,0xa1e412ac,0xb1e512ac,0xc1e612ac,0x81e711f0,0x91e811ec, + 0x62ad11e9,0xd1ea12ac,0xe1eb12ac,0xf2ad12ac,0x11ed12ac,0xd1ee12ac,0xe1ef12ac,0xf2ad12ac, + 0x11f112ac,0xd1f212ac,0xe1f312ac,0xf2ad12ac,0xe1f512ac,0xf1f612ac,0x11f711fe,0x31f811fa, + 0x62ad11f9,0xd2ad12ac,0xa1fb12ac,0xb1fc12ac,0xc1fd12ac,0xd2ad12ac,0x81ff12ac,0x920012ac, + 0xa20112ac,0xb20212ac,0xc20312ac,0xd2ad12ac,0xc205020e,0x720602ac,0x820702ac,0x920802ac, + 0xa20902ac,0xb20a02ac,0xd20b02ac,0xe20c02ac,0x62ad020d,0xf2ad02ac,0xc20f12ac,0xd21012ac, + 0xe21112ac,0xf212121a,0x12131216,0x32ad1214,0xa21512ac,0xb2ad12ac,0x821712ac,0x921812ac, + 0xa21912ac,0xb2ad12ac,0x621b12ac,0x721c12ac,0x821d12ac,0x921e12ac,0xa21f12ac,0xb2ad12ac, + 0xb221022c,0x722202ac,0x822302ac,0x922402ac,0xa22502ac,0xc22602ac,0xd22702ac,0x6228022a, + 0x52ad0229,0xe2ad02ac,0xe22b02ac,0xf2ad02ac,0xb22d12ac,0xc22e12ac,0xd22f12ac,0xa230123e, + 0xe2311239,0xf2321235,0x12ad1233,0x823412ac,0x92ad12ac,0x623612ac,0x723712ac,0x823812ac, + 0x92ad12ac,0x523a12ac,0x623b12ac,0x723c12ac,0x823d12ac,0x92ad12ac,0x123f12ac,0x324012ac, + 0xe24112ac,0xf2ad12ac,0x92430255,0x724402ac,0x824502ac,0xa24602ac,0xb24702ac,0x62480251, + 0x5249024e,0x424a024c,0x32ad024b,0xc2ad02ac,0xc24d02ac,0xd2ad02ac,0xc24f02ac,0xd25002ac, + 0xe2ad02ac,0xc25202ac,0xd25302ac,0xe25402ac,0xf2ad02ac,0x925612ac,0xa25712ac,0xb25812ac, + 0x8259126b,0xc25a1266,0xd25b1262,0xe25c125f,0xf2ad125d,0x625e12ac,0x72ad12ac,0x526012ac, + 0x626112ac,0x72ad12ac,0x426312ac,0x526412ac,0x626512ac,0x72ad12ac,0x326712ac,0x426812ac, + 0x526912ac,0x626a12ac,0x72ad12ac,0x126c12ac,0xc26d12ac,0xd26e12ac,0xe26f12ac,0xf2ad12ac, + 0x7271028e,0x827202ac,0x927302ac,0x62740288,0x52750283,0x4276027f,0x3277027c,0x2278027a, + 0x12ad0279,0xa2ad02ac,0xa27b02ac,0xb2ad02ac,0xa27d02ac,0xb27e02ac,0xc2ad02ac,0xa28002ac, + 0xb28102ac,0xc28202ac,0xd2ad02ac,0xa28402ac,0xb28502ac,0xc28602ac,0xd28702ac,0xe2ad02ac, + 0xa28902ac,0xb28a02ac,0xc28b02ac,0xd28c02ac,0xe28d02ac,0xf2ad02ac,0x728f12ac,0x829012ac, + 0x929112ac,0x629212a6,0x529312a1,0x4294129d,0x3295129a,0x22961298,0x12ad1297,0xa2ad12ac, + 0xa29912ac,0xb2ad12ac,0xa29b12ac,0xb29c12ac,0xc2ad12ac,0xa29e12ac,0xb29f12ac,0xc2a012ac, + 0xd2ad12ac,0xa2a212ac,0xb2a312ac,0xc2a412ac,0xd2a512ac,0xe2ad12ac,0xa2a712ac,0xb2a812ac, + 0xc2a912ac,0xd2aa12ac,0xe2ab12ac,0xf2ad12ac,0x000000fc,0x000000fd,0x000000fe,0x000000ff + }; + switch(agasttype) { + case AgastFeatureDetector::AGAST_5_8: + agastbase=0; + table_struct1=(uint32_t *)(table_5_8_struct1); + table_struct2=(uint32_t *)(table_5_8_struct2); + break; + case AgastFeatureDetector::AGAST_7_12d: + agastbase=2; + table_struct1=(uint32_t *)(table_7_12d_struct1); + table_struct2=(uint32_t *)(table_7_12d_struct2); + break; + case AgastFeatureDetector::AGAST_7_12s: + agastbase=1; + table_struct1=(uint32_t *)(table_7_12s_struct1); + table_struct2=(uint32_t *)(table_7_12s_struct2); + break; + case AgastFeatureDetector::OAST_9_16: + default: + agastbase=2; + table_struct1=(uint32_t *)(table_9_16_struct); + table_struct2=(uint32_t *)(table_9_16_struct); + break; + } + + size_t total = 0; + int xsize = img.cols; + int ysize = img.rows; + size_t nExpectedCorners = keypoints.capacity(); + register int x, y; + register int xsizeB = xsize - (agastbase + 2); + register int ysizeB = ysize - (agastbase + 1); + register int width; + + keypoints.resize(0); + + int pixel[16]; + makeAgastOffsets(pixel, (int)img.step, agasttype); + + width = xsize; + + for(y = agastbase+1; y < ysizeB; y++) + { + x = agastbase; + while(true) + { + homogeneous: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + result = agast_tree_search(table_struct1, pixel, ptr, threshold); + switch (result) + { + case 252: + goto homogeneous; + case 253: + goto success_homogeneous; + case 254: + goto structured; + case 255: + goto success_structured; + } + } + } + structured: + { + x++; + if(x > xsizeB) + break; + else + { + register const unsigned char* const ptr = img.ptr() + y*width + x; + result = agast_tree_search(table_struct2, pixel, ptr, threshold); + switch (result) + { + case 252: + goto homogeneous; + case 253: + goto success_homogeneous; + case 254: + goto structured; + case 255: + goto success_structured; + } + } + } + success_homogeneous: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto homogeneous; + success_structured: + if(total == nExpectedCorners) + { + if(nExpectedCorners == 0) + { + nExpectedCorners = 512; + keypoints.reserve(nExpectedCorners); + } + else + { + nExpectedCorners *= 2; + keypoints.reserve(nExpectedCorners); + } + } + keypoints.push_back(KeyPoint(Point2f((float)x, (float)y), 1.0f)); + total++; + goto structured; + } + } +} + +static void AGAST_5_8(InputArray _img, std::vector& keypoints, int threshold) +{ + AGAST_ALL(_img, keypoints, threshold, AgastFeatureDetector::AGAST_5_8); +} + +static void AGAST_7_12d(InputArray _img, std::vector& keypoints, int threshold) +{ + AGAST_ALL(_img, keypoints, threshold, AgastFeatureDetector::AGAST_7_12d); +} + +static void AGAST_7_12s(InputArray _img, std::vector& keypoints, int threshold) +{ + AGAST_ALL(_img, keypoints, threshold, AgastFeatureDetector::AGAST_7_12s); +} + +static void OAST_9_16(InputArray _img, std::vector& keypoints, int threshold) +{ + AGAST_ALL(_img, keypoints, threshold, AgastFeatureDetector::OAST_9_16); +} + +#endif // !(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) + void AGAST(InputArray _img, std::vector& keypoints, int threshold, bool nonmax_suppression) { AGAST(_img, keypoints, threshold, nonmax_suppression, AgastFeatureDetector::OAST_9_16); } - class AgastFeatureDetector_Impl : public AgastFeatureDetector { public: @@ -7466,6 +7961,7 @@ public: cvtColor( _image, ogray, COLOR_BGR2GRAY ); gray = ogray; } + keypoints.clear(); AGAST( gray, keypoints, threshold, nonmaxSuppression, type ); KeyPointsFilter::runByPixelsMask( keypoints, mask ); } diff --git a/modules/features2d/src/agast_score.cpp b/modules/features2d/src/agast_score.cpp index 7b1835402..9a8163b64 100644 --- a/modules/features2d/src/agast_score.cpp +++ b/modules/features2d/src/agast_score.cpp @@ -89,6 +89,7 @@ void makeAgastOffsets(int pixel[16], int rowStride, int type) pixel[k] = offsets[k][0] + offsets[k][1] * rowStride; } +#if (defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) // 16 pixel mask template<> int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) @@ -9371,5 +9372,491 @@ int agast_cornerScore(const uchar* ptr, const i b_test = (bmin + bmax) / 2; } } +#else // !(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) + + +int agast_tree_search(const uint32_t table_struct32[], int pixel_[], const unsigned char* const ptr, int threshold) +{ + register const int cb = *ptr + threshold; + register const int c_b = *ptr - threshold; + register int index; + register int offset; + register int cmpresult; + index = 0; + while ((table_struct32[index]>>16)!=0) + { + offset=(int) pixel_[table_struct32[index]>>28]; + if ((table_struct32[index]&(1<<12))!=0) + cmpresult=(ptr[offset] < c_b); + else + cmpresult=(ptr[offset] > cb); + if (cmpresult) + index =(table_struct32[index]>>16)&0xfff; + else + index =table_struct32[index]&0xfff; + } + return (int)(table_struct32[index]&0xff); +} + +// universal pixel mask +int AGAST_ALL_SCORE(const uchar* ptr, const int pixel[], int threshold, int agasttype) +{ + int bmin = threshold; + int bmax = 255; + int b_test = (bmax + bmin)/2; + uint32_t *table_struct; + + int result; + static const uint32_t table_5_8_corner_struct[] = + { 0x00010026,0x20020017,0x3003000c,0x50040009,0x10050007,0x406d0006,0x706d006c,0x4008006c, + 0x606d006c,0x100a006c,0x406d000b,0x706d006c,0x700d0012,0x600e006c,0x500f0011,0x106d0010, + 0x406d006c,0x106d006c,0x5013106c,0x3014106c,0x7015106c,0x4016106c,0x606d106c,0x5018001c, + 0x7019006c,0x601a006c,0x106d001b,0x406d006c,0x501d106c,0x301e106c,0x201f1023,0x10201021, + 0x406d106c,0x4022106c,0x606d106c,0x7024106c,0x4025106c,0x606d106c,0x00271058,0x20281049, + 0x70290035,0x302a1031,0x502b102f,0x102c102d,0x406d106c,0x402e106c,0x606d106c,0x1030106c, + 0x406d106c,0x5032006c,0x3033006c,0x4034006c,0x606d006c,0x70361041,0x3037103c,0x5038103b, + 0x106d1039,0x403a106c,0x606d106c,0x106d106c,0x603d106c,0x503e1040,0x106d103f,0x406d106c, + 0x106d106c,0x3042106c,0x50431047,0x10441045,0x406d106c,0x4046106c,0x606d106c,0x1048106c, + 0x406d106c,0x504a0053,0x304b006c,0x204c0050,0x104d004e,0x406d006c,0x404f006c,0x606d006c, + 0x7051006c,0x4052006c,0x606d006c,0x5054106c,0x7055106c,0x6056106c,0x106d1057,0x406d106c, + 0x30590062,0x505a006c,0x205b005f,0x105c005d,0x406d006c,0x405e006c,0x606d006c,0x7060006c, + 0x4061006c,0x606d006c,0x3063106c,0x5064106c,0x20651069,0x10661067,0x406d106c,0x4068106c, + 0x606d106c,0x706a106c,0x406b106c,0x606d106c,0x000000fe,0x000000ff}; + + static const uint32_t table_7_12d_corner_struct[] = + { 0x000100b5,0x50020036,0x20030025,0x9004001d,0x10050015,0x6006000f,0x3007000a,0x41870008, + 0xa0090186,0xb1870186,0x800b0186,0xa00c0186,0xb187000d,0x400e0186,0x71870186,0xb0100186, + 0x30110013,0x41870012,0xa1870186,0x80140186,0xa1870186,0x60160186,0x70170186,0x80180186, + 0x4019001b,0x3187001a,0xa1870186,0xa01c0186,0xb1870186,0x301e0186,0x401f0186,0x10200022, + 0x61870021,0xb1870186,0x60230186,0x70240186,0x81870186,0x90260186,0x70270186,0x80280186, + 0x10290030,0xa02a002d,0xb187002b,0x602c0186,0x41870186,0x602e0186,0x302f0186,0x41870186, + 0x60310186,0x40320034,0x31870033,0xa1870186,0xa0350186,0xb1870186,0x503710a1,0x9038006b, + 0x3039105b,0x403a1053,0xb03b004d,0x103c0044,0x803d0040,0xa03e0186,0x2187003f,0x71870186, + 0x60411186,0x20421186,0x70431186,0x81871186,0x60450048,0x70460186,0x80470186,0xa1870186, + 0x60491186,0x204a1186,0x704b1186,0x1187104c,0x81871186,0x204e1186,0x704f1186,0x10501051, + 0x61871186,0x60521186,0x81871186,0xb0540186,0x80550186,0xa0560186,0x10570059,0x21870058, + 0x71870186,0x605a0186,0x71870186,0xb05c0186,0xa05d0186,0x305e0065,0x105f0062,0x21870060, + 0x70610186,0x81870186,0x60630186,0x70640186,0x81870186,0x80660186,0x10670069,0x21870068, + 0x71870186,0x606a0186,0x71870186,0x906c1093,0x206d0087,0x106e007f,0x406f0077,0xa0700072, + 0x30710186,0xb1870186,0x60731186,0x70741186,0x80751186,0xb0761186,0xa1871186,0x60781186, + 0x70791186,0x807a1186,0xa07b107d,0x4187107c,0xb1871186,0x307e1186,0x41871186,0x60801186, + 0x70811186,0x80821186,0x40831085,0x31871084,0xa1871186,0xa0861186,0xb1871186,0x60881186, + 0x70891186,0x808a108f,0x408b108d,0x3187108c,0xa1871186,0xa08e1186,0xb1871186,0x20901186, + 0x10911186,0x30921186,0x41871186,0x20940099,0x10950186,0x30960186,0x40970186,0xa0980186, + 0xb1870186,0x209a1186,0x309b1186,0x409c1186,0x709d1186,0x109e109f,0x61871186,0x60a01186, + 0x81871186,0x20a200ae,0xa0a30186,0xb0a40186,0x90a500ab,0x10a600a8,0x318700a7,0x81870186, + 0x60a90186,0x70aa0186,0x81870186,0x10ac0186,0x30ad0186,0x41870186,0x90af0186,0x70b00186, + 0x80b10186,0xa0b20186,0xb0b30186,0x118700b4,0x61870186,0x00b6115a,0x20b700e2,0x50b800cc, + 0x70b900c5,0x60ba0186,0x40bb00c1,0x30bc00be,0x118700bd,0x81870186,0x90bf0186,0x80c00186, + 0xa1870186,0x90c20186,0x80c30186,0xa0c40186,0xb1870186,0x90c61186,0x80c71186,0xa0c81186, + 0xb0c91186,0x70ca1186,0x118710cb,0x61871186,0x90cd1186,0x70ce1186,0x80cf1186,0x50d010de, + 0x10d110d8,0xa0d210d5,0xb18710d3,0x60d41186,0x41871186,0x60d61186,0x30d71186,0x41871186, + 0x60d91186,0x40da10dc,0x318710db,0xa1871186,0xa0dd1186,0xb1871186,0xa0df1186,0xb0e01186, + 0x118710e1,0x61871186,0x20e3113a,0x90e4010b,0x50e500ff,0x10e610f7,0x40e710ef,0xa0e810ea, + 0x30e91186,0xb1871186,0x60eb0186,0x70ec0186,0x80ed0186,0xb0ee0186,0xa1870186,0x60f00186, + 0x70f10186,0x80f20186,0xa0f300f5,0x418700f4,0xb1870186,0x30f60186,0x41870186,0x60f80186, + 0x70f90186,0x80fa0186,0x40fb00fd,0x318700fc,0xa1870186,0xa0fe0186,0xb1870186,0x31001186, + 0x41011186,0x51021108,0x11031105,0x61871104,0xb1871186,0x61061186,0x71071186,0x81871186, + 0x11091186,0xa10a1186,0xb1871186,0x910c112e,0x510d1126,0x110e111e,0x610f1118,0x31101113, + 0x41871111,0xa1121186,0xb1871186,0x81141186,0xa1151186,0xb1871116,0x41171186,0x71871186, + 0xb1191186,0x311a111c,0x4187111b,0xa1871186,0x811d1186,0xa1871186,0x611f1186,0x71201186, + 0x81211186,0x41221124,0x31871123,0xa1871186,0xa1251186,0xb1871186,0xa1271186,0xb1281186, + 0x1129112b,0x3187112a,0x81871186,0x612c1186,0x712d1186,0x81871186,0x312f1186,0x41301186, + 0x51311137,0x11321134,0x61871133,0xb1871186,0x61351186,0x71361186,0x81871186,0x11381186, + 0xa1391186,0xb1871186,0x913b1150,0x713c1186,0x813d1186,0x513e114c,0x113f1146,0xa1401143, + 0xb1871141,0x61421186,0x41871186,0x61441186,0x31451186,0x41871186,0x61471186,0x4148114a, + 0x31871149,0xa1871186,0xa14b1186,0xb1871186,0xa14d1186,0xb14e1186,0x1187114f,0x61871186, + 0x51510186,0x91520186,0x61530186,0x71540186,0x81550186,0x41560158,0x31870157,0xa1870186, + 0xa1590186,0xb1870186,0x515b0170,0x915c0168,0x615d0186,0x715e0186,0x415f0165,0x31600163, + 0x81870161,0x11620186,0x21870186,0x81640186,0xa1870186,0xb1660186,0x81670186,0xa1870186, + 0x21690186,0x316a0186,0x416b0186,0x716c0186,0x116d016e,0x61870186,0x616f0186,0x81870186, + 0x51711186,0x9172117e,0x61731186,0x71741186,0x4175117b,0x31761179,0x81871177,0x11781186, + 0x21871186,0x817a1186,0xa1871186,0xb17c1186,0x817d1186,0xa1871186,0x217f1186,0x31801186, + 0x41811186,0x71821186,0x11831184,0x61871186,0x61851186,0x81871186,0x000000fe,0x000000ff}; + + static const uint32_t table_7_12s_corner_struct[] = + { 0x0001032b,0x50020104,0x20031026,0x70040748,0x97481005,0x90060748,0x1007100f,0x67481008, + 0x60090748,0x800a0748,0x400b000d,0x3749000c,0xa7490748,0xa00e0748,0xb7490748,0x1010001e, + 0x60111014,0x80120748,0xa0130748,0xb7490748,0x6015001b,0x80160748,0x40170019,0x37490018, + 0xa7490748,0xa01a0748,0xb7490748,0x801c0748,0xa01d0748,0xb7490748,0x6748101f,0x60200748, + 0x80210748,0x40220024,0x37490023,0xa7490748,0xa0250748,0xb7490748,0x202700e1,0x70281059, + 0x90291035,0x1748102a,0x102b0748,0x602c002e,0x302d0748,0x47490748,0x602f1032,0x30300748, + 0x40310748,0xb7490748,0x30330748,0x40340748,0xb7490748,0x9036004d,0x17481037,0x10380748, + 0x6039103f,0xb03a0748,0x303b003d,0x4749003c,0xa7490748,0x803e0748,0xa7490748,0x60400047, + 0x30410044,0x47490042,0xa0430748,0xb7490748,0x80450748,0xa0460748,0xb7490748,0xb0480748, + 0x3049004b,0x4749004a,0xa7490748,0x804c0748,0xa7490748,0x1748104e,0x104f0748,0x60500052, + 0x30510748,0x47490748,0x60531056,0x30540748,0x40550748,0xb7490748,0x30570748,0x40580748, + 0xb7490748,0x905a107d,0x705b0071,0x105c1061,0x6748105d,0x605e0748,0x305f0748,0x40600748, + 0x87490748,0x1062006c,0x60630065,0x30640748,0x47490748,0x60661069,0x30670748,0x40680748, + 0xb7490748,0x306a0748,0x406b0748,0xb7490748,0x6748106d,0x606e0748,0x306f0748,0x40700748, + 0x87490748,0x17481072,0x10730748,0x60740076,0x30750748,0x47490748,0x6077107a,0x30780748, + 0x40790748,0xb7490748,0x307b0748,0x407c0748,0xb7490748,0x707e00bd,0x907f00a7,0x10801088, + 0x67481081,0x60820748,0x80830748,0x40840086,0x37490085,0xa7490748,0xa0870748,0xb7490748, + 0x1089009f,0x608a1090,0xb08b0748,0x308c008e,0x4749008d,0xa7490748,0x808f0748,0xa7490748, + 0x60910099,0x30920095,0x47490093,0xa0940748,0xb7490748,0x80960748,0xa0970748,0x47490098, + 0xb7490748,0xb09a0748,0x309b009d,0x4749009c,0xa7490748,0x809e0748,0xa7490748,0x674810a0, + 0x60a10748,0x80a20748,0x40a300a5,0x374900a4,0xa7490748,0xa0a60748,0xb7490748,0x10a810ad, + 0x674810a9,0x60aa0748,0x30ab0748,0x40ac0748,0x87490748,0x10ae00b8,0x60af00b1,0x30b00748, + 0x47490748,0x60b210b5,0x30b30748,0x40b40748,0xb7490748,0x30b60748,0x40b70748,0xb7490748, + 0x674810b9,0x60ba0748,0x30bb0748,0x40bc0748,0x87490748,0x90be00d5,0x174810bf,0x10c00748, + 0x60c110c7,0xb0c20748,0x30c300c5,0x474900c4,0xa7490748,0x80c60748,0xa7490748,0x60c800cf, + 0x30c900cc,0x474900ca,0xa0cb0748,0xb7490748,0x80cd0748,0xa0ce0748,0xb7490748,0xb0d00748, + 0x30d100d3,0x474900d2,0xa7490748,0x80d40748,0xa7490748,0x174810d6,0x10d70748,0x60d800da, + 0x30d90748,0x47490748,0x60db10de,0x30dc0748,0x40dd0748,0xb7490748,0x30df0748,0x40e00748, + 0xb7490748,0x70e20748,0x974810e3,0x90e40748,0x10e510ed,0x674810e6,0x60e70748,0x80e80748, + 0x40e900eb,0x374900ea,0xa7490748,0xa0ec0748,0xb7490748,0x10ee00fc,0x60ef10f2,0x80f00748, + 0xa0f10748,0xb7490748,0x60f300f9,0x80f40748,0x40f500f7,0x374900f6,0xa7490748,0xa0f80748, + 0xb7490748,0x80fa0748,0xa0fb0748,0xb7490748,0x674810fd,0x60fe0748,0x80ff0748,0x41000102, + 0x37490101,0xa7490748,0xa1030748,0xb7490748,0x51051253,0x9106118c,0x71070119,0x27481108, + 0x21090748,0x1748110a,0x110b0748,0x610c0110,0x310d0748,0x410e0748,0xa10f0748,0xb7490748, + 0x61111115,0x31120748,0x41130748,0xa1140748,0xb7490748,0x31160748,0x41170748,0xa1180748, + 0xb7490748,0x711a117a,0x211b1136,0x111c0124,0x6748011d,0x611e1748,0x811f1748,0x41201122, + 0x37491121,0xa7491748,0xa1231748,0xb7491748,0x1125112e,0x67480126,0x61271748,0x4128112b, + 0x37491129,0x812a1748,0xa7491748,0x812c1748,0xa12d1748,0xb7491748,0x6748012f,0x61301748, + 0x81311748,0x41321134,0x37491133,0xa7491748,0xa1351748,0xb7491748,0x21370160,0x11381140, + 0x67480139,0x613a1748,0x813b1748,0x413c113e,0x3749113d,0xa7491748,0xa13f1748,0xb7491748, + 0x11410158,0x61420146,0x31430748,0x41440748,0xa1450748,0xb7490748,0x61471154,0x4148014e, + 0xa149014b,0x314a0748,0xb7490748,0x814c1748,0xb14d1748,0xa7491748,0x814f1748,0xa1501152, + 0x47491151,0xb7491748,0x31531748,0x47491748,0x31550748,0x41560748,0xa1570748,0xb7490748, + 0x67480159,0x615a1748,0x815b1748,0x415c115e,0x3749115d,0xa7491748,0xa15f1748,0xb7491748, + 0x11610169,0x67480162,0x61631748,0x81641748,0x41651167,0x37491166,0xa7491748,0xa1681748, + 0xb7491748,0x116a1172,0x6748016b,0x616c1748,0x816d1748,0x416e1170,0x3749116f,0xa7491748, + 0xa1711748,0xb7491748,0x67480173,0x61741748,0x81751748,0x41761178,0x37491177,0xa7491748, + 0xa1791748,0xb7491748,0x2748117b,0x217c0748,0x1748117d,0x117e0748,0x617f0183,0x31800748, + 0x41810748,0xa1820748,0xb7490748,0x61841188,0x31850748,0x41860748,0xa1870748,0xb7490748, + 0x31890748,0x418a0748,0xa18b0748,0xb7490748,0x918d020d,0x718e11b0,0x218f019f,0x17481190, + 0x11910748,0x61920196,0xa1930748,0xb1940748,0x37490195,0x87490748,0x6197119b,0xa1980748, + 0xb1990748,0x3749019a,0x87490748,0xa19c0748,0xb19d0748,0x3749019e,0x87490748,0x21a01748, + 0x11a111a5,0x674801a2,0x61a31748,0x31a41748,0x47491748,0x11a601ab,0x674801a7,0x61a81748, + 0x31a91748,0x41aa1748,0x87491748,0x674801ac,0x61ad1748,0x31ae1748,0x41af1748,0x87491748, + 0x71b101fb,0x21b211c9,0x11b311b8,0x674811b4,0x61b50748,0x81b60748,0xa1b70748,0xb7490748, + 0x11b901c4,0x61ba01bd,0x81bb0748,0xa1bc0748,0xb7490748,0x61be11c1,0x81bf0748,0xa1c00748, + 0xb7490748,0x81c20748,0xa1c30748,0xb7490748,0x674811c5,0x61c60748,0x81c70748,0xa1c80748, + 0xb7490748,0x21ca01e4,0x11cb11d0,0x674811cc,0x61cd0748,0x81ce0748,0xa1cf0748,0xb7490748, + 0x11d101df,0x61d201d6,0xa1d30748,0xb1d40748,0x374901d5,0x87490748,0x61d711db,0xa1d80748, + 0xb1d90748,0x374901da,0x87490748,0xa1dc0748,0xb1dd0748,0x374901de,0x87490748,0x674811e0, + 0x61e10748,0x81e20748,0xa1e30748,0xb7490748,0x11e511ea,0x674811e6,0x61e70748,0x81e80748, + 0xa1e90748,0xb7490748,0x11eb01f6,0x61ec01ef,0x81ed0748,0xa1ee0748,0xb7490748,0x61f011f3, + 0x81f10748,0xa1f20748,0xb7490748,0x81f40748,0xa1f50748,0xb7490748,0x674811f7,0x61f80748, + 0x81f90748,0xa1fa0748,0xb7490748,0x274811fc,0x21fd0748,0x174811fe,0x11ff0748,0x62000204, + 0xa2010748,0xb2020748,0x37490203,0x87490748,0x62051209,0xa2060748,0xb2070748,0x37490208, + 0x87490748,0xa20a0748,0xb20b0748,0x3749020c,0x87490748,0x220e1220,0x7748020f,0x72101748, + 0x12111215,0x67480212,0x62131748,0x32141748,0x47491748,0x1216021b,0x67480217,0x62181748, + 0x32191748,0x421a1748,0x87491748,0x6748021c,0x621d1748,0x321e1748,0x421f1748,0x87491748, + 0x22210748,0x72220232,0x17481223,0x12240748,0x62250229,0x32260748,0x42270748,0xa2280748, + 0xb7490748,0x622a122e,0x322b0748,0x422c0748,0xa22d0748,0xb7490748,0x322f0748,0x42300748, + 0xa2310748,0xb7490748,0x72331243,0x17481234,0x12350748,0x6236023a,0x32370748,0x42380748, + 0xa2390748,0xb7490748,0x623b123f,0x323c0748,0x423d0748,0xa23e0748,0xb7490748,0x32400748, + 0x42410748,0xa2420748,0xb7490748,0x17481244,0x12450748,0x6246024a,0x32470748,0x42480748, + 0xa2490748,0xb7490748,0x624b124f,0x324c0748,0x424d0748,0xa24e0748,0xb7490748,0x32500748, + 0x42510748,0xa2520748,0xb7490748,0x2254126e,0x72550748,0x97481256,0x92570748,0x1258125d, + 0x67481259,0x625a0748,0x825b0748,0xa25c0748,0xb7490748,0x125e0269,0x625f0262,0x82600748, + 0xa2610748,0xb7490748,0x62631266,0x82640748,0xa2650748,0xb7490748,0x82670748,0xa2680748, + 0xb7490748,0x6748126a,0x626b0748,0x826c0748,0xa26d0748,0xb7490748,0x226f0311,0x727012a2, + 0x92711281,0x17481272,0x12730748,0x62740278,0x32750748,0x42760748,0xa2770748,0xb7490748, + 0x6279127d,0x327a0748,0x427b0748,0xa27c0748,0xb7490748,0x327e0748,0x427f0748,0xa2800748, + 0xb7490748,0x92820292,0x17481283,0x12840748,0x62850289,0xa2860748,0xb2870748,0x37490288, + 0x87490748,0x628a128e,0xa28b0748,0xb28c0748,0x3749028d,0x87490748,0xa28f0748,0xb2900748, + 0x37490291,0x87490748,0x17481293,0x12940748,0x62950299,0x32960748,0x42970748,0xa2980748, + 0xb7490748,0x629a129e,0x329b0748,0x429c0748,0xa29d0748,0xb7490748,0x329f0748,0x42a00748, + 0xa2a10748,0xb7490748,0x92a312c4,0x72a402b4,0x174812a5,0x12a60748,0x62a702ab,0x32a80748, + 0x42a90748,0xa2aa0748,0xb7490748,0x62ac12b0,0x32ad0748,0x42ae0748,0xa2af0748,0xb7490748, + 0x32b10748,0x42b20748,0xa2b30748,0xb7490748,0x174812b5,0x12b60748,0x62b702bb,0x32b80748, + 0x42b90748,0xa2ba0748,0xb7490748,0x62bc12c0,0x32bd0748,0x42be0748,0xa2bf0748,0xb7490748, + 0x32c10748,0x42c20748,0xa2c30748,0xb7490748,0x72c502f0,0x92c602e0,0x12c712cc,0x674812c8, + 0x62c90748,0x82ca0748,0xa2cb0748,0xb7490748,0x12cd02db,0x62ce02d2,0xa2cf0748,0xb2d00748, + 0x374902d1,0x87490748,0x62d312d7,0xa2d40748,0xb2d50748,0x374902d6,0x87490748,0xa2d80748, + 0xb2d90748,0x374902da,0x87490748,0x674812dc,0x62dd0748,0x82de0748,0xa2df0748,0xb7490748, + 0x174812e1,0x12e20748,0x62e302e7,0x32e40748,0x42e50748,0xa2e60748,0xb7490748,0x62e812ec, + 0x32e90748,0x42ea0748,0xa2eb0748,0xb7490748,0x32ed0748,0x42ee0748,0xa2ef0748,0xb7490748, + 0x92f10301,0x174812f2,0x12f30748,0x62f402f8,0xa2f50748,0xb2f60748,0x374902f7,0x87490748, + 0x62f912fd,0xa2fa0748,0xb2fb0748,0x374902fc,0x87490748,0xa2fe0748,0xb2ff0748,0x37490300, + 0x87490748,0x17481302,0x13030748,0x63040308,0x33050748,0x43060748,0xa3070748,0xb7490748, + 0x6309130d,0x330a0748,0x430b0748,0xa30c0748,0xb7490748,0x330e0748,0x430f0748,0xa3100748, + 0xb7490748,0x73120748,0x97481313,0x93140748,0x1315131a,0x67481316,0x63170748,0x83180748, + 0xa3190748,0xb7490748,0x131b0326,0x631c031f,0x831d0748,0xa31e0748,0xb7490748,0x63201323, + 0x83210748,0xa3220748,0xb7490748,0x83240748,0xa3250748,0xb7490748,0x67481327,0x63280748, + 0x83290748,0xa32a0748,0xb7490748,0x032c1655,0x532d1431,0x932e0360,0x2748032f,0x23301748, + 0x7331033d,0x17480332,0x13331748,0x63341336,0x33351748,0x47491748,0x6337033a,0x33381748, + 0x43391748,0xb7491748,0x333b1748,0x433c1748,0xb7491748,0x733e1354,0x133f0344,0x67480340, + 0x63411748,0x33421748,0x43431748,0x87491748,0x1345134f,0x63461348,0x33471748,0x47491748, + 0x6349034c,0x334a1748,0x434b1748,0xb7491748,0x334d1748,0x434e1748,0xb7491748,0x67480350, + 0x63511748,0x33521748,0x43531748,0x87491748,0x17480355,0x13561748,0x63571359,0x33581748, + 0x47491748,0x635a035d,0x335b1748,0x435c1748,0xb7491748,0x335e1748,0x435f1748,0xb7491748, + 0x936113ff,0x7362037b,0x27480363,0x23641748,0x17480365,0x13661748,0x6367036d,0xb3681748, + 0x3369136b,0x4749136a,0xa7491748,0x836c1748,0xa7491748,0x636e1375,0x336f1372,0x47491370, + 0xa3711748,0xb7491748,0x83731748,0xa3741748,0xb7491748,0xb3761748,0x33771379,0x47491378, + 0xa7491748,0x837a1748,0xa7491748,0x737c13e6,0x237d039d,0x137e0386,0x6748037f,0x63801748, + 0x83811748,0x43821384,0x37491383,0xa7491748,0xa3851748,0xb7491748,0x13871395,0x6388038b, + 0x83891748,0xa38a1748,0xb7491748,0x638c1392,0x838d1748,0x438e1390,0x3749138f,0xa7491748, + 0xa3911748,0xb7491748,0x83931748,0xa3941748,0xb7491748,0x67480396,0x63971748,0x83981748, + 0x4399139b,0x3749139a,0xa7491748,0xa39c1748,0xb7491748,0x239e13c6,0x139f03a7,0x674803a0, + 0x63a11748,0x83a21748,0x43a313a5,0x374913a4,0xa7491748,0xa3a61748,0xb7491748,0x13a813be, + 0x63a903af,0xb3aa1748,0x33ab13ad,0x474913ac,0xa7491748,0x83ae1748,0xa7491748,0x63b013b8, + 0x33b113b4,0x474913b2,0xa3b31748,0xb7491748,0x83b51748,0xa3b61748,0x474913b7,0xb7491748, + 0xb3b91748,0x33ba13bc,0x474913bb,0xa7491748,0x83bd1748,0xa7491748,0x674803bf,0x63c01748, + 0x83c11748,0x43c213c4,0x374913c3,0xa7491748,0xa3c51748,0xb7491748,0x13c703cf,0x674803c8, + 0x63c91748,0x83ca1748,0x43cb13cd,0x374913cc,0xa7491748,0xa3ce1748,0xb7491748,0x13d013de, + 0x63d103d4,0x83d21748,0xa3d31748,0xb7491748,0x63d513db,0x83d61748,0x43d713d9,0x374913d8, + 0xa7491748,0xa3da1748,0xb7491748,0x83dc1748,0xa3dd1748,0xb7491748,0x674803df,0x63e01748, + 0x83e11748,0x43e213e4,0x374913e3,0xa7491748,0xa3e51748,0xb7491748,0x274803e7,0x23e81748, + 0x174803e9,0x13ea1748,0x63eb03f1,0xb3ec1748,0x33ed13ef,0x474913ee,0xa7491748,0x83f01748, + 0xa7491748,0x63f213f9,0x33f313f6,0x474913f4,0xa3f51748,0xb7491748,0x83f71748,0xa3f81748, + 0xb7491748,0xb3fa1748,0x33fb13fd,0x474913fc,0xa7491748,0x83fe1748,0xa7491748,0x27480400, + 0x24011748,0x7402040e,0x17480403,0x14041748,0x64051407,0x34061748,0x47491748,0x6408040b, + 0x34091748,0x440a1748,0xb7491748,0x340c1748,0x440d1748,0xb7491748,0x740f1425,0x14100415, + 0x67480411,0x64121748,0x34131748,0x44141748,0x87491748,0x14161420,0x64171419,0x34181748, + 0x47491748,0x641a041d,0x341b1748,0x441c1748,0xb7491748,0x341e1748,0x441f1748,0xb7491748, + 0x67480421,0x64221748,0x34231748,0x44241748,0x87491748,0x17480426,0x14271748,0x6428142a, + 0x34291748,0x47491748,0x642b042e,0x342c1748,0x442d1748,0xb7491748,0x342f1748,0x44301748, + 0xb7491748,0x5432057d,0x2433048b,0x7434144d,0x97480435,0x94361748,0x1437043c,0x67480438, + 0x64391748,0x843a1748,0xa43b1748,0xb7491748,0x143d1448,0x643e0441,0x843f1748,0xa4401748, + 0xb7491748,0x64421445,0x84431748,0xa4441748,0xb7491748,0x84461748,0xa4471748,0xb7491748, + 0x67480449,0x644a1748,0x844b1748,0xa44c1748,0xb7491748,0x744e0748,0x944f145f,0x14500454, + 0x67481451,0x64520748,0x34530748,0x47490748,0x1455145a,0x67481456,0x64570748,0x34580748, + 0x44590748,0x87490748,0x6748145b,0x645c0748,0x345d0748,0x445e0748,0x87490748,0x9460047b, + 0x14611469,0x67481462,0x64630748,0x84640748,0x44650467,0x37490466,0xa7490748,0xa4680748, + 0xb7490748,0x146a0473,0x6748146b,0x646c0748,0x446d0470,0x3749046e,0x846f0748,0xa7490748, + 0x84710748,0xa4720748,0xb7490748,0x67481474,0x64750748,0x84760748,0x44770479,0x37490478, + 0xa7490748,0xa47a0748,0xb7490748,0x147c0480,0x6748147d,0x647e0748,0x347f0748,0x47490748, + 0x14811486,0x67481482,0x64830748,0x34840748,0x44850748,0x87490748,0x67481487,0x64880748, + 0x34890748,0x448a0748,0x87490748,0x248c1547,0x748d14c9,0x948e049e,0x1748048f,0x14901748, + 0x64910495,0x34921748,0x44931748,0xa4941748,0xb7491748,0x6496149a,0x34971748,0x44981748, + 0xa4991748,0xb7491748,0x349b1748,0x449c1748,0xa49d1748,0xb7491748,0x949f14b9,0x14a004a5, + 0x674804a1,0x64a21748,0x84a31748,0xa4a41748,0xb7491748,0x14a614b4,0x64a704ab,0xa4a81748, + 0xb4a91748,0x374914aa,0x87491748,0x64ac14b0,0xa4ad1748,0xb4ae1748,0x374914af,0x87491748, + 0xa4b11748,0xb4b21748,0x374914b3,0x87491748,0x674804b5,0x64b61748,0x84b71748,0xa4b81748, + 0xb7491748,0x174804ba,0x14bb1748,0x64bc04c0,0x34bd1748,0x44be1748,0xa4bf1748,0xb7491748, + 0x64c114c5,0x34c21748,0x44c31748,0xa4c41748,0xb7491748,0x34c61748,0x44c71748,0xa4c81748, + 0xb7491748,0x74ca0515,0x94cb14db,0x174804cc,0x14cd1748,0x64ce04d2,0xa4cf1748,0xb4d01748, + 0x374914d1,0x87491748,0x64d314d7,0xa4d41748,0xb4d51748,0x374914d6,0x87491748,0xa4d81748, + 0xb4d91748,0x374914da,0x87491748,0x94dc0505,0x14dd04e5,0x674814de,0x64df0748,0x84e00748, + 0x44e104e3,0x374904e2,0xa7490748,0xa4e40748,0xb7490748,0x14e614fd,0x64e714eb,0x34e81748, + 0x44e91748,0xa4ea1748,0xb7491748,0x64ec04f9,0x44ed14f3,0xa4ee04f0,0x84ef0748,0xb7490748, + 0x34f11748,0xb4f21748,0xa7491748,0x84f40748,0xa4f504f7,0x474904f6,0xb7490748,0x34f80748, + 0x47490748,0x34fa1748,0x44fb1748,0xa4fc1748,0xb7491748,0x674814fe,0x64ff0748,0x85000748, + 0x45010503,0x37490502,0xa7490748,0xa5040748,0xb7490748,0x17480506,0x15071748,0x6508050c, + 0x35091748,0x450a1748,0xa50b1748,0xb7491748,0x650d1511,0x350e1748,0x450f1748,0xa5101748, + 0xb7491748,0x35121748,0x45131748,0xa5141748,0xb7491748,0x95160526,0x17480517,0x15181748, + 0x6519051d,0x351a1748,0x451b1748,0xa51c1748,0xb7491748,0x651e1522,0x351f1748,0x45201748, + 0xa5211748,0xb7491748,0x35231748,0x45241748,0xa5251748,0xb7491748,0x95271537,0x17480528, + 0x15291748,0x652a052e,0xa52b1748,0xb52c1748,0x3749152d,0x87491748,0x652f1533,0xa5301748, + 0xb5311748,0x37491532,0x87491748,0xa5341748,0xb5351748,0x37491536,0x87491748,0x17480538, + 0x15391748,0x653a053e,0x353b1748,0x453c1748,0xa53d1748,0xb7491748,0x653f1543,0x35401748, + 0x45411748,0xa5421748,0xb7491748,0x35441748,0x45451748,0xa5461748,0xb7491748,0x75480564, + 0x97481549,0x954a0748,0x154b0553,0x6748154c,0x654d0748,0x854e0748,0x454f0551,0x37490550, + 0xa7490748,0xa5520748,0xb7490748,0x1554155c,0x67481555,0x65560748,0x85570748,0x4558055a, + 0x37490559,0xa7490748,0xa55b0748,0xb7490748,0x6748155d,0x655e0748,0x855f0748,0x45600562, + 0x37490561,0xa7490748,0xa5630748,0xb7490748,0x95651748,0x75661748,0x1567056c,0x67480568, + 0x65691748,0x856a1748,0xa56b1748,0xb7491748,0x156d1578,0x656e0571,0x856f1748,0xa5701748, + 0xb7491748,0x65721575,0x85731748,0xa5741748,0xb7491748,0x85761748,0xa5771748,0xb7491748, + 0x67480579,0x657a1748,0x857b1748,0xa57c1748,0xb7491748,0x257e0598,0x757f1748,0x97480580, + 0x95811748,0x15820587,0x67480583,0x65841748,0x85851748,0xa5861748,0xb7491748,0x15881593, + 0x6589058c,0x858a1748,0xa58b1748,0xb7491748,0x658d1590,0x858e1748,0xa58f1748,0xb7491748, + 0x85911748,0xa5921748,0xb7491748,0x67480594,0x65951748,0x85961748,0xa5971748,0xb7491748, + 0x2599163b,0x759a05cc,0x959b05ab,0x1748059c,0x159d1748,0x659e05a2,0x359f1748,0x45a01748, + 0xa5a11748,0xb7491748,0x65a315a7,0x35a41748,0x45a51748,0xa5a61748,0xb7491748,0x35a81748, + 0x45a91748,0xa5aa1748,0xb7491748,0x95ac15bc,0x174805ad,0x15ae1748,0x65af05b3,0xa5b01748, + 0xb5b11748,0x374915b2,0x87491748,0x65b415b8,0xa5b51748,0xb5b61748,0x374915b7,0x87491748, + 0xa5b91748,0xb5ba1748,0x374915bb,0x87491748,0x174805bd,0x15be1748,0x65bf05c3,0x35c01748, + 0x45c11748,0xa5c21748,0xb7491748,0x65c415c8,0x35c51748,0x45c61748,0xa5c71748,0xb7491748, + 0x35c91748,0x45ca1748,0xa5cb1748,0xb7491748,0x95cd05ee,0x75ce15de,0x174805cf,0x15d01748, + 0x65d105d5,0x35d21748,0x45d31748,0xa5d41748,0xb7491748,0x65d615da,0x35d71748,0x45d81748, + 0xa5d91748,0xb7491748,0x35db1748,0x45dc1748,0xa5dd1748,0xb7491748,0x174805df,0x15e01748, + 0x65e105e5,0x35e21748,0x45e31748,0xa5e41748,0xb7491748,0x65e615ea,0x35e71748,0x45e81748, + 0xa5e91748,0xb7491748,0x35eb1748,0x45ec1748,0xa5ed1748,0xb7491748,0x75ef161a,0x95f0160a, + 0x15f105f6,0x674805f2,0x65f31748,0x85f41748,0xa5f51748,0xb7491748,0x15f71605,0x65f805fc, + 0xa5f91748,0xb5fa1748,0x374915fb,0x87491748,0x65fd1601,0xa5fe1748,0xb5ff1748,0x37491600, + 0x87491748,0xa6021748,0xb6031748,0x37491604,0x87491748,0x67480606,0x66071748,0x86081748, + 0xa6091748,0xb7491748,0x1748060b,0x160c1748,0x660d0611,0x360e1748,0x460f1748,0xa6101748, + 0xb7491748,0x66121616,0x36131748,0x46141748,0xa6151748,0xb7491748,0x36171748,0x46181748, + 0xa6191748,0xb7491748,0x961b162b,0x1748061c,0x161d1748,0x661e0622,0xa61f1748,0xb6201748, + 0x37491621,0x87491748,0x66231627,0xa6241748,0xb6251748,0x37491626,0x87491748,0xa6281748, + 0xb6291748,0x3749162a,0x87491748,0x1748062c,0x162d1748,0x662e0632,0x362f1748,0x46301748, + 0xa6311748,0xb7491748,0x66331637,0x36341748,0x46351748,0xa6361748,0xb7491748,0x36381748, + 0x46391748,0xa63a1748,0xb7491748,0x763c1748,0x9748063d,0x963e1748,0x163f0644,0x67480640, + 0x66411748,0x86421748,0xa6431748,0xb7491748,0x16451650,0x66460649,0x86471748,0xa6481748, + 0xb7491748,0x664a164d,0x864b1748,0xa64c1748,0xb7491748,0x864e1748,0xa64f1748,0xb7491748, + 0x67480651,0x66521748,0x86531748,0xa6541748,0xb7491748,0x565616cf,0x77480657,0x76581748, + 0x26590675,0x9748065a,0x965b1748,0x165c0664,0x6748065d,0x665e1748,0x865f1748,0x46601662, + 0x37491661,0xa7491748,0xa6631748,0xb7491748,0x1665166d,0x67480666,0x66671748,0x86681748, + 0x4669166b,0x3749166a,0xa7491748,0xa66c1748,0xb7491748,0x6748066e,0x666f1748,0x86701748, + 0x46711673,0x37491672,0xa7491748,0xa6741748,0xb7491748,0x267616b3,0x96770687,0x1678167c, + 0x67480679,0x667a1748,0x367b1748,0x47491748,0x167d0682,0x6748067e,0x667f1748,0x36801748, + 0x46811748,0x87491748,0x67480683,0x66841748,0x36851748,0x46861748,0x87491748,0x968816a3, + 0x16890691,0x6748068a,0x668b1748,0x868c1748,0x468d168f,0x3749168e,0xa7491748,0xa6901748, + 0xb7491748,0x1692169b,0x67480693,0x66941748,0x46951698,0x37491696,0x86971748,0xa7491748, + 0x86991748,0xa69a1748,0xb7491748,0x6748069c,0x669d1748,0x869e1748,0x469f16a1,0x374916a0, + 0xa7491748,0xa6a21748,0xb7491748,0x16a416a8,0x674806a5,0x66a61748,0x36a71748,0x47491748, + 0x16a906ae,0x674806aa,0x66ab1748,0x36ac1748,0x46ad1748,0x87491748,0x674806af,0x66b01748, + 0x36b11748,0x46b21748,0x87491748,0x974806b4,0x96b51748,0x16b606be,0x674806b7,0x66b81748, + 0x86b91748,0x46ba16bc,0x374916bb,0xa7491748,0xa6bd1748,0xb7491748,0x16bf16c7,0x674806c0, + 0x66c11748,0x86c21748,0x46c316c5,0x374916c4,0xa7491748,0xa6c61748,0xb7491748,0x674806c8, + 0x66c91748,0x86ca1748,0x46cb16cd,0x374916cc,0xa7491748,0xa6ce1748,0xb7491748,0x56d00748, + 0x76d10748,0x26d216ee,0x974816d3,0x96d40748,0x16d506dd,0x674816d6,0x66d70748,0x86d80748, + 0x46d906db,0x374906da,0xa7490748,0xa6dc0748,0xb7490748,0x16de16e6,0x674816df,0x66e00748, + 0x86e10748,0x46e206e4,0x374906e3,0xa7490748,0xa6e50748,0xb7490748,0x674816e7,0x66e80748, + 0x86e90748,0x46ea06ec,0x374906eb,0xa7490748,0xa6ed0748,0xb7490748,0x26ef072c,0x96f01700, + 0x16f106f5,0x674816f2,0x66f30748,0x36f40748,0x47490748,0x16f616fb,0x674816f7,0x66f80748, + 0x36f90748,0x46fa0748,0x87490748,0x674816fc,0x66fd0748,0x36fe0748,0x46ff0748,0x87490748, + 0x9701071c,0x1702170a,0x67481703,0x67040748,0x87050748,0x47060708,0x37490707,0xa7490748, + 0xa7090748,0xb7490748,0x170b0714,0x6748170c,0x670d0748,0x470e0711,0x3749070f,0x87100748, + 0xa7490748,0x87120748,0xa7130748,0xb7490748,0x67481715,0x67160748,0x87170748,0x4718071a, + 0x37490719,0xa7490748,0xa71b0748,0xb7490748,0x171d0721,0x6748171e,0x671f0748,0x37200748, + 0x47490748,0x17221727,0x67481723,0x67240748,0x37250748,0x47260748,0x87490748,0x67481728, + 0x67290748,0x372a0748,0x472b0748,0x87490748,0x9748172d,0x972e0748,0x172f0737,0x67481730, + 0x67310748,0x87320748,0x47330735,0x37490734,0xa7490748,0xa7360748,0xb7490748,0x17381740, + 0x67481739,0x673a0748,0x873b0748,0x473c073e,0x3749073d,0xa7490748,0xa73f0748,0xb7490748, + 0x67481741,0x67420748,0x87430748,0x47440746,0x37490745,0xa7490748,0xa7470748,0xb7490748, + 0x000000fe,0x000000ff}; + + static const uint32_t table_9_16_corner_struct[] = + { 0x00010138,0x200200d3,0x4003008a,0x50040051,0x70050027,0x30060016,0x1007000d,0x6008000a, + 0x82ad0009,0xf2ad02ac,0xd00b02ac,0xe00c02ac,0xf2ad02ac,0x800e02ac,0x900f02ac,0xa01002ac, + 0x62ad0011,0xb01202ac,0xc01302ac,0xd01402ac,0xe01502ac,0xf2ad02ac,0xa01702ac,0xb01802ac, + 0xc01902ac,0x801a0023,0x901b001f,0x62ad001c,0xd01d02ac,0xe01e02ac,0xf2ad02ac,0x102002ac, + 0xd02102ac,0xe02202ac,0xf2ad02ac,0x102402ac,0xd02502ac,0xe02602ac,0xf2ad02ac,0x70281041, + 0xe0290038,0xf02a02ac,0x102b0032,0x302c002e,0x62ad002d,0xd2ad02ac,0xa02f02ac,0xb03002ac, + 0xc03102ac,0xd2ad02ac,0x803302ac,0x903402ac,0xa03502ac,0xb03602ac,0xc03702ac,0xd2ad02ac, + 0xe03912ac,0x803a12ac,0x903b12ac,0xa03c12ac,0xb03d12ac,0xc03e12ac,0xd03f12ac,0x62ad1040, + 0xf2ad12ac,0xe04202ac,0xf04302ac,0x1044004b,0x30450047,0x62ad0046,0xd2ad02ac,0xa04802ac, + 0xb04902ac,0xc04a02ac,0xd2ad02ac,0x804c02ac,0x904d02ac,0xa04e02ac,0xb04f02ac,0xc05002ac, + 0xd2ad02ac,0x5052106e,0xc0530064,0xd05402ac,0xe05502ac,0xf056005e,0x1057005a,0x32ad0058, + 0xa05902ac,0xb2ad02ac,0x805b02ac,0x905c02ac,0xa05d02ac,0xb2ad02ac,0x605f02ac,0x706002ac, + 0x806102ac,0x906202ac,0xa06302ac,0xb2ad02ac,0xc06512ac,0x706612ac,0x806712ac,0x906812ac, + 0xa06912ac,0xb06a12ac,0xd06b12ac,0x62ad106c,0xe06d12ac,0xf2ad12ac,0xc06f0080,0xd07002ac, + 0xe07102ac,0xf072007a,0x10730076,0x32ad0074,0xa07502ac,0xb2ad02ac,0x807702ac,0x907802ac, + 0xa07902ac,0xb2ad02ac,0x607b02ac,0x707c02ac,0x807d02ac,0x907e02ac,0xa07f02ac,0xb2ad02ac, + 0xc08112ac,0x708212ac,0x808312ac,0x908412ac,0xa08512ac,0xb08612ac,0xd08712ac,0xe08812ac, + 0x62ad1089,0xf2ad12ac,0x408b10b1,0xb08c00a1,0xc08d02ac,0xd08e02ac,0xa08f009d,0xe0900098, + 0xf0910094,0x12ad0092,0x809302ac,0x92ad02ac,0x609502ac,0x709602ac,0x809702ac,0x92ad02ac, + 0x509902ac,0x609a02ac,0x709b02ac,0x809c02ac,0x92ad02ac,0x109e02ac,0x309f02ac,0xe0a002ac, + 0xf2ad02ac,0xb0a212ac,0x70a312ac,0x80a412ac,0x90a512ac,0xa0a612ac,0x60a710ad,0x50a810aa, + 0x32ad10a9,0xc2ad12ac,0xc0ab12ac,0xd0ac12ac,0xe2ad12ac,0xc0ae12ac,0xd0af12ac,0xe0b012ac, + 0xf2ad12ac,0xb0b200c7,0xc0b302ac,0xd0b402ac,0xa0b500c3,0xe0b600be,0xf0b700ba,0x12ad00b8, + 0x80b902ac,0x92ad02ac,0x60bb02ac,0x70bc02ac,0x80bd02ac,0x92ad02ac,0x50bf02ac,0x60c002ac, + 0x70c102ac,0x80c202ac,0x92ad02ac,0x10c402ac,0x30c502ac,0xe0c602ac,0xf2ad02ac,0xb0c812ac, + 0x70c912ac,0x80ca12ac,0x90cb12ac,0xa0cc12ac,0xc0cd12ac,0xd0ce12ac,0x60cf10d1,0x52ad10d0, + 0xe2ad12ac,0xe0d212ac,0xf2ad12ac,0x20d4110a,0x90d500ef,0xa0d602ac,0xb0d702ac,0x80d800ea, + 0xc0d900e5,0xd0da00e1,0xe0db00de,0xf2ad00dc,0x60dd02ac,0x72ad02ac,0x50df02ac,0x60e002ac, + 0x72ad02ac,0x40e202ac,0x50e302ac,0x60e402ac,0x72ad02ac,0x30e602ac,0x40e702ac,0x50e802ac, + 0x60e902ac,0x72ad02ac,0x10eb02ac,0xc0ec02ac,0xd0ed02ac,0xe0ee02ac,0xf2ad02ac,0x90f012ac, + 0x70f112ac,0x80f212ac,0x60f31104,0x50f410ff,0x40f510fb,0x30f610f8,0x12ad10f7,0xa2ad12ac, + 0xa0f912ac,0xb0fa12ac,0xc2ad12ac,0xa0fc12ac,0xb0fd12ac,0xc0fe12ac,0xd2ad12ac,0xa10012ac, + 0xb10112ac,0xc10212ac,0xd10312ac,0xe2ad12ac,0xa10512ac,0xb10612ac,0xc10712ac,0xd10812ac, + 0xe10912ac,0xf2ad12ac,0x910b0125,0xa10c02ac,0xb10d02ac,0x810e0120,0xc10f011b,0xd1100117, + 0xe1110114,0xf2ad0112,0x611302ac,0x72ad02ac,0x511502ac,0x611602ac,0x72ad02ac,0x411802ac, + 0x511902ac,0x611a02ac,0x72ad02ac,0x311c02ac,0x411d02ac,0x511e02ac,0x611f02ac,0x72ad02ac, + 0x112102ac,0xc12202ac,0xd12302ac,0xe12402ac,0xf2ad02ac,0x912612ac,0x712712ac,0x812812ac, + 0xa12912ac,0xb12a12ac,0x612b1134,0x512c1131,0x412d112f,0x32ad112e,0xc2ad12ac,0xc13012ac, + 0xd2ad12ac,0xc13212ac,0xd13312ac,0xe2ad12ac,0xc13512ac,0xd13612ac,0xe13712ac,0xf2ad12ac, + 0x01391270,0x213a0170,0x913b0155,0x713c02ac,0x813d02ac,0x613e014f,0x513f014a,0x41400146, + 0x31410143,0x12ad0142,0xa2ad02ac,0xa14402ac,0xb14502ac,0xc2ad02ac,0xa14702ac,0xb14802ac, + 0xc14902ac,0xd2ad02ac,0xa14b02ac,0xb14c02ac,0xc14d02ac,0xd14e02ac,0xe2ad02ac,0xa15002ac, + 0xb15102ac,0xc15202ac,0xd15302ac,0xe15402ac,0xf2ad02ac,0x915612ac,0xa15712ac,0xb15812ac, + 0x8159116b,0xc15a1166,0xd15b1162,0xe15c115f,0xf2ad115d,0x615e12ac,0x72ad12ac,0x516012ac, + 0x616112ac,0x72ad12ac,0x416312ac,0x516412ac,0x616512ac,0x72ad12ac,0x316712ac,0x416812ac, + 0x516912ac,0x616a12ac,0x72ad12ac,0x116c12ac,0xc16d12ac,0xd16e12ac,0xe16f12ac,0xf2ad12ac, + 0x21711242,0x41720198,0xb1730182,0x717402ac,0x817502ac,0x917602ac,0xa17702ac,0x6178017e, + 0x5179017b,0x32ad017a,0xc2ad02ac,0xc17c02ac,0xd17d02ac,0xe2ad02ac,0xc17f02ac,0xd18002ac, + 0xe18102ac,0xf2ad02ac,0xb18312ac,0xc18412ac,0xd18512ac,0xa1861194,0xe187118f,0xf188118b, + 0x12ad1189,0x818a12ac,0x92ad12ac,0x618c12ac,0x718d12ac,0x818e12ac,0x92ad12ac,0x519012ac, + 0x619112ac,0x719212ac,0x819312ac,0x92ad12ac,0x119512ac,0x319612ac,0xe19712ac,0xf2ad12ac, + 0x41991220,0x519a01b6,0xc19b01a4,0x719c02ac,0x819d02ac,0x919e02ac,0xa19f02ac,0xb1a002ac, + 0xd1a102ac,0x62ad01a2,0xe1a302ac,0xf2ad02ac,0xc1a512ac,0xd1a612ac,0xe1a712ac,0xf1a811b0, + 0x11a911ac,0x32ad11aa,0xa1ab12ac,0xb2ad12ac,0x81ad12ac,0x91ae12ac,0xa1af12ac,0xb2ad12ac, + 0x61b112ac,0x71b212ac,0x81b312ac,0x91b412ac,0xa1b512ac,0xb2ad12ac,0x51b71204,0x71b801d1, + 0xe1b901c1,0x81ba02ac,0x91bb02ac,0xa1bc02ac,0xb1bd02ac,0xc1be02ac,0xd1bf02ac,0x62ad01c0, + 0xf2ad02ac,0xe1c212ac,0xf1c312ac,0x11c411cb,0x31c511c7,0x62ad11c6,0xd2ad12ac,0xa1c812ac, + 0xb1c912ac,0xc1ca12ac,0xd2ad12ac,0x81cc12ac,0x91cd12ac,0xa1ce12ac,0xb1cf12ac,0xc1d012ac, + 0xd2ad12ac,0x71d211f4,0x31d311e3,0x11d411da,0x61d511d7,0x82ad11d6,0xf2ad12ac,0xd1d812ac, + 0xe1d912ac,0xf2ad12ac,0x81db12ac,0x91dc12ac,0xa1dd12ac,0x62ad11de,0xb1df12ac,0xc1e012ac, + 0xd1e112ac,0xe1e212ac,0xf2ad12ac,0xa1e412ac,0xb1e512ac,0xc1e612ac,0x81e711f0,0x91e811ec, + 0x62ad11e9,0xd1ea12ac,0xe1eb12ac,0xf2ad12ac,0x11ed12ac,0xd1ee12ac,0xe1ef12ac,0xf2ad12ac, + 0x11f112ac,0xd1f212ac,0xe1f312ac,0xf2ad12ac,0xe1f512ac,0xf1f612ac,0x11f711fe,0x31f811fa, + 0x62ad11f9,0xd2ad12ac,0xa1fb12ac,0xb1fc12ac,0xc1fd12ac,0xd2ad12ac,0x81ff12ac,0x920012ac, + 0xa20112ac,0xb20212ac,0xc20312ac,0xd2ad12ac,0xc205020e,0x720602ac,0x820702ac,0x920802ac, + 0xa20902ac,0xb20a02ac,0xd20b02ac,0xe20c02ac,0x62ad020d,0xf2ad02ac,0xc20f12ac,0xd21012ac, + 0xe21112ac,0xf212121a,0x12131216,0x32ad1214,0xa21512ac,0xb2ad12ac,0x821712ac,0x921812ac, + 0xa21912ac,0xb2ad12ac,0x621b12ac,0x721c12ac,0x821d12ac,0x921e12ac,0xa21f12ac,0xb2ad12ac, + 0xb221022c,0x722202ac,0x822302ac,0x922402ac,0xa22502ac,0xc22602ac,0xd22702ac,0x6228022a, + 0x52ad0229,0xe2ad02ac,0xe22b02ac,0xf2ad02ac,0xb22d12ac,0xc22e12ac,0xd22f12ac,0xa230123e, + 0xe2311239,0xf2321235,0x12ad1233,0x823412ac,0x92ad12ac,0x623612ac,0x723712ac,0x823812ac, + 0x92ad12ac,0x523a12ac,0x623b12ac,0x723c12ac,0x823d12ac,0x92ad12ac,0x123f12ac,0x324012ac, + 0xe24112ac,0xf2ad12ac,0x92430255,0x724402ac,0x824502ac,0xa24602ac,0xb24702ac,0x62480251, + 0x5249024e,0x424a024c,0x32ad024b,0xc2ad02ac,0xc24d02ac,0xd2ad02ac,0xc24f02ac,0xd25002ac, + 0xe2ad02ac,0xc25202ac,0xd25302ac,0xe25402ac,0xf2ad02ac,0x925612ac,0xa25712ac,0xb25812ac, + 0x8259126b,0xc25a1266,0xd25b1262,0xe25c125f,0xf2ad125d,0x625e12ac,0x72ad12ac,0x526012ac, + 0x626112ac,0x72ad12ac,0x426312ac,0x526412ac,0x626512ac,0x72ad12ac,0x326712ac,0x426812ac, + 0x526912ac,0x626a12ac,0x72ad12ac,0x126c12ac,0xc26d12ac,0xd26e12ac,0xe26f12ac,0xf2ad12ac, + 0x7271028e,0x827202ac,0x927302ac,0x62740288,0x52750283,0x4276027f,0x3277027c,0x2278027a, + 0x12ad0279,0xa2ad02ac,0xa27b02ac,0xb2ad02ac,0xa27d02ac,0xb27e02ac,0xc2ad02ac,0xa28002ac, + 0xb28102ac,0xc28202ac,0xd2ad02ac,0xa28402ac,0xb28502ac,0xc28602ac,0xd28702ac,0xe2ad02ac, + 0xa28902ac,0xb28a02ac,0xc28b02ac,0xd28c02ac,0xe28d02ac,0xf2ad02ac,0x728f12ac,0x829012ac, + 0x929112ac,0x629212a6,0x529312a1,0x4294129d,0x3295129a,0x22961298,0x12ad1297,0xa2ad12ac, + 0xa29912ac,0xb2ad12ac,0xa29b12ac,0xb29c12ac,0xc2ad12ac,0xa29e12ac,0xb29f12ac,0xc2a012ac, + 0xd2ad12ac,0xa2a212ac,0xb2a312ac,0xc2a412ac,0xd2a512ac,0xe2ad12ac,0xa2a712ac,0xb2a812ac, + 0xc2a912ac,0xd2aa12ac,0xe2ab12ac,0xf2ad12ac,0x000000fe,0x000000ff}; + + + switch(agasttype) { + case AgastFeatureDetector::AGAST_5_8: + table_struct=(uint32_t *)(table_5_8_corner_struct); + break; + case AgastFeatureDetector::AGAST_7_12d: + table_struct=(uint32_t *)(table_7_12d_corner_struct); + break; + case AgastFeatureDetector::AGAST_7_12s: + table_struct=(uint32_t *)(table_7_12s_corner_struct); + break; + case AgastFeatureDetector::OAST_9_16: + default: + table_struct=(uint32_t *)(table_9_16_corner_struct); + break; + } + + while(true) + { + result = agast_tree_search(table_struct, (int *)pixel, ptr, b_test); + if (result == 254) + bmax = b_test; + else + bmin = b_test; + + if(bmin == bmax - 1 || bmin == bmax) + return bmin; + b_test = (bmin + bmax) / 2; + } +} + +// 8 pixel mask +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + return AGAST_ALL_SCORE(ptr, pixel, threshold, AgastFeatureDetector::AGAST_5_8); +} + +// 12 pixel mask in square format +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + return AGAST_ALL_SCORE(ptr, pixel, threshold, AgastFeatureDetector::AGAST_7_12d); +} + +// 12 pixel mask in diamond format +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + return AGAST_ALL_SCORE(ptr, pixel, threshold, AgastFeatureDetector::AGAST_7_12s); +} + +// 16 pixel mask +template<> +int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold) +{ + return AGAST_ALL_SCORE(ptr, pixel, threshold, AgastFeatureDetector::OAST_9_16); +} + +#endif // !(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) } // namespace cv diff --git a/modules/features2d/src/agast_score.hpp b/modules/features2d/src/agast_score.hpp index bd5ddb333..f7d668ba7 100644 --- a/modules/features2d/src/agast_score.hpp +++ b/modules/features2d/src/agast_score.hpp @@ -52,11 +52,18 @@ The references are: namespace cv { +#if !(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) +int agast_tree_search(const uint32_t table_struct32[], int pixel_[], const unsigned char* const ptr, int threshold); +int AGAST_ALL_SCORE(const uchar* ptr, const int pixel[], int threshold, int agasttype); +#endif //!(defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64)) + + void makeAgastOffsets(int pixel[16], int row_stride, int type); template int agast_cornerScore(const uchar* ptr, const int pixel[], int threshold); + } #endif #endif diff --git a/modules/features2d/src/blobdetector.cpp b/modules/features2d/src/blobdetector.cpp index e53eff35a..aa8593a29 100644 --- a/modules/features2d/src/blobdetector.cpp +++ b/modules/features2d/src/blobdetector.cpp @@ -311,6 +311,10 @@ void SimpleBlobDetectorImpl::detect(InputArray image, std::vector& else grayscaleImage = image.getMat(); + if (grayscaleImage.type() != CV_8UC1) { + CV_Error(Error::StsUnsupportedFormat, "Blob detector only supports 8-bit images!"); + } + std::vector < std::vector
> centers; for (double thresh = params.minThreshold; thresh < params.maxThreshold; thresh += params.thresholdStep) { diff --git a/modules/features2d/src/kaze/AKAZEFeatures.cpp b/modules/features2d/src/kaze/AKAZEFeatures.cpp index d12656e99..6f1b610cc 100644 --- a/modules/features2d/src/kaze/AKAZEFeatures.cpp +++ b/modules/features2d/src/kaze/AKAZEFeatures.cpp @@ -191,7 +191,7 @@ public: for (int i = range.start; i < range.end; i++) { - float ratio = (float)fastpow(2, evolution[i].octave); + float ratio = (float)fastpow(2, evolution[i].octave); int sigma_size_ = fRound(evolution[i].esigma * options_.derivative_factor / ratio); compute_scharr_derivatives(evolution[i].Lsmooth, evolution[i].Lx, 1, 0, sigma_size_); @@ -839,7 +839,7 @@ void AKAZEFeatures::Compute_Main_Orientation(KeyPoint& kpt, const std::vector max) { // store largest orientation max = sumX*sumX + sumY*sumY; - kpt.angle = getAngle(sumX, sumY); + kpt.angle = getAngle(sumX, sumY) * 180.f / static_cast(CV_PI); } } } @@ -1000,7 +1000,7 @@ void MSURF_Descriptor_64_Invoker::Get_MSURF_Descriptor_64(const KeyPoint& kpt, f // Get the information from the keypoint ratio = (float)(1 << kpt.octave); scale = fRound(0.5f*kpt.size / ratio); - angle = kpt.angle; + angle = (kpt.angle * static_cast(CV_PI)) / 180.f; level = kpt.class_id; yf = kpt.pt.y / ratio; xf = kpt.pt.x / ratio; @@ -1406,8 +1406,9 @@ void MLDB_Full_Descriptor_Invoker::Get_MLDB_Full_Descriptor(const KeyPoint& kpt, float scale = (float)fRound(0.5f*kpt.size / ratio); float xf = kpt.pt.x / ratio; float yf = kpt.pt.y / ratio; - float co = cos(kpt.angle); - float si = sin(kpt.angle); + float angle = (kpt.angle * static_cast(CV_PI)) / 180.f; + float co = cos(angle); + float si = sin(angle); int pattern_size = options_->descriptor_pattern_size; int dpos = 0; @@ -1441,7 +1442,7 @@ void MLDB_Descriptor_Subset_Invoker::Get_MLDB_Descriptor_Subset(const KeyPoint& // Get the information from the keypoint float ratio = (float)(1 << kpt.octave); int scale = fRound(0.5f*kpt.size / ratio); - float angle = kpt.angle; + float angle = (kpt.angle * static_cast(CV_PI)) / 180.f; int level = kpt.class_id; float yf = kpt.pt.y / ratio; float xf = kpt.pt.x / ratio; diff --git a/modules/features2d/src/kaze/KAZEFeatures.cpp b/modules/features2d/src/kaze/KAZEFeatures.cpp index 294898c26..29d85d328 100644 --- a/modules/features2d/src/kaze/KAZEFeatures.cpp +++ b/modules/features2d/src/kaze/KAZEFeatures.cpp @@ -512,7 +512,7 @@ public: for (int i = range.start; i < range.end; i++) { kpts[i].angle = 0.0; - if (options_.upright) + if (options_.upright) { kpts[i].angle = 0.0; if (options_.extended) @@ -638,7 +638,7 @@ void KAZEFeatures::Compute_Main_Orientation(KeyPoint &kpt, const std::vector max) { // store largest orientation max = sumX*sumX + sumY*sumY; - kpt.angle = getAngle(sumX, sumY); + kpt.angle = getAngle(sumX, sumY) * 180.f / static_cast(CV_PI); } } } @@ -805,7 +805,7 @@ void KAZE_Descriptor_Invoker::Get_KAZE_Descriptor_64(const KeyPoint &kpt, float yf = kpt.pt.y; xf = kpt.pt.x; scale = fRound(kpt.size / 2.0f); - angle = kpt.angle; + angle = (kpt.angle * static_cast(CV_PI)) / 180.f; level = kpt.class_id; co = cos(angle); si = sin(angle); @@ -1088,7 +1088,7 @@ void KAZE_Descriptor_Invoker::Get_KAZE_Descriptor_128(const KeyPoint &kpt, float yf = kpt.pt.y; xf = kpt.pt.x; scale = fRound(kpt.size / 2.0f); - angle = kpt.angle; + angle = (kpt.angle * static_cast(CV_PI)) / 180.f; level = kpt.class_id; co = cos(angle); si = sin(angle); diff --git a/modules/features2d/src/matchers.cpp b/modules/features2d/src/matchers.cpp index fad73a271..a1b557fa9 100644 --- a/modules/features2d/src/matchers.cpp +++ b/modules/features2d/src/matchers.cpp @@ -517,30 +517,32 @@ DescriptorMatcher::~DescriptorMatcher() void DescriptorMatcher::add( InputArrayOfArrays _descriptors ) { - if(_descriptors.isUMatVector()) + if( _descriptors.isUMatVector() ) { std::vector descriptors; - _descriptors.getUMatVector(descriptors); + _descriptors.getUMatVector( descriptors ); utrainDescCollection.insert( utrainDescCollection.end(), descriptors.begin(), descriptors.end() ); } - else if(_descriptors.isUMat()) + else if( _descriptors.isUMat() ) { std::vector descriptors = std::vector(1, _descriptors.getUMat()); utrainDescCollection.insert( utrainDescCollection.end(), descriptors.begin(), descriptors.end() ); } - else if(_descriptors.isMatVector()) + else if( _descriptors.isMatVector() ) { std::vector descriptors; _descriptors.getMatVector(descriptors); trainDescCollection.insert( trainDescCollection.end(), descriptors.begin(), descriptors.end() ); } - else if(_descriptors.isMat()) + else if( _descriptors.isMat() ) { std::vector descriptors = std::vector(1, _descriptors.getMat()); trainDescCollection.insert( trainDescCollection.end(), descriptors.begin(), descriptors.end() ); } else + { CV_Assert( _descriptors.isUMat() || _descriptors.isUMatVector() || _descriptors.isMat() || _descriptors.isMatVector() ); + } } const std::vector& DescriptorMatcher::getTrainDescriptors() const @@ -1032,12 +1034,37 @@ FlannBasedMatcher::FlannBasedMatcher( const Ptr& _indexParam void FlannBasedMatcher::add( InputArrayOfArrays _descriptors ) { DescriptorMatcher::add( _descriptors ); - std::vector descriptors; - _descriptors.getUMatVector(descriptors); - for( size_t i = 0; i < descriptors.size(); i++ ) + if( _descriptors.isUMatVector() ) { - addedDescCount += descriptors[i].rows; + std::vector descriptors; + _descriptors.getUMatVector( descriptors ); + + for( size_t i = 0; i < descriptors.size(); i++ ) + { + addedDescCount += descriptors[i].rows; + } + } + else if( _descriptors.isUMat() ) + { + addedDescCount += _descriptors.getUMat().rows; + } + else if( _descriptors.isMatVector() ) + { + std::vector descriptors; + _descriptors.getMatVector(descriptors); + for( size_t i = 0; i < descriptors.size(); i++ ) + { + addedDescCount += descriptors[i].rows; + } + } + else if( _descriptors.isMat() ) + { + addedDescCount += _descriptors.getMat().rows; + } + else + { + CV_Assert( _descriptors.isUMat() || _descriptors.isUMatVector() || _descriptors.isMat() || _descriptors.isMatVector() ); } } diff --git a/modules/features2d/src/mser.cpp b/modules/features2d/src/mser.cpp index b37b69ba7..1143addc1 100644 --- a/modules/features2d/src/mser.cpp +++ b/modules/features2d/src/mser.cpp @@ -1,16 +1,16 @@ /* Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: - * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * The name of Contributor may not be used to endorse or - * promote products derived from this software without - * specific prior written permission. + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * The name of Contributor may not be used to endorse or + * promote products derived from this software without + * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, diff --git a/modules/features2d/src/precomp.hpp b/modules/features2d/src/precomp.hpp index 2f77d9270..c3db78b9d 100644 --- a/modules/features2d/src/precomp.hpp +++ b/modules/features2d/src/precomp.hpp @@ -49,6 +49,7 @@ #include "opencv2/core/utility.hpp" #include "opencv2/core/private.hpp" #include "opencv2/core/ocl.hpp" +#include "opencv2/core/hal/hal.hpp" #include diff --git a/modules/features2d/test/test_descriptors_regression.cpp b/modules/features2d/test/test_descriptors_regression.cpp index 717cae58d..7f44bc2bd 100644 --- a/modules/features2d/test/test_descriptors_regression.cpp +++ b/modules/features2d/test/test_descriptors_regression.cpp @@ -162,7 +162,8 @@ protected: ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); } - image.create( 50, 50, CV_8UC3 ); + RNG rng; + image = cvtest::randomMat(rng, Size(50, 50), CV_8UC3, 0, 255, false); try { dextractor->compute( image, keypoints, descriptors ); diff --git a/modules/hal/CMakeLists.txt b/modules/hal/CMakeLists.txt deleted file mode 100644 index 982913dba..000000000 --- a/modules/hal/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -set(the_description "The Hardware Acceleration Layer (HAL) module") - -set(OPENCV_MODULE_TYPE STATIC) - -if(OPENCV_HAL_HEADERS AND OPENCV_HAL_LIBS) - set(OPENCV_HAL_HEADERS_INCLUDES "#include \"${OPENCV_HAL_HEADERS}\"") - set(DEPS "${OPENCV_HAL_LIBS}") -else() - set(OPENCV_HAL_HEADERS_INCLUDES "// using default HAL") - set(DEPS "") -endif() - -configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/custom_hal.hpp.in" "${CMAKE_BINARY_DIR}/custom_hal.hpp" @ONLY) - -if(UNIX) - if(CMAKE_COMPILER_IS_GNUCXX OR CV_ICC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") - endif() -endif() - -ocv_define_module(hal ${DEPS}) diff --git a/modules/hal/include/opencv2/hal.hpp b/modules/hal/include/opencv2/hal.hpp deleted file mode 100644 index 125bbc824..000000000 --- a/modules/hal/include/opencv2/hal.hpp +++ /dev/null @@ -1,287 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Copyright (C) 2015, Itseez Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef __OPENCV_HAL_HPP__ -#define __OPENCV_HAL_HPP__ - -#include "opencv2/hal/defs.h" -#include "opencv2/hal/interface.hpp" - -/** - @defgroup hal Hardware Acceleration Layer - @{ - @defgroup hal_intrin Universal intrinsics - @{ - @defgroup hal_intrin_impl Private implementation helpers - @} - @defgroup hal_utils Platform-dependent utils - @} -*/ - -namespace cv { namespace hal { - -//! @addtogroup hal -//! @{ - -class Failure -{ -public: - Failure(int code_ = Error::Unknown) : code(code_) {} -public: - int code; -}; - -int normHamming(const uchar* a, int n); -int normHamming(const uchar* a, const uchar* b, int n); - -int normHamming(const uchar* a, int n, int cellSize); -int normHamming(const uchar* a, const uchar* b, int n, int cellSize); - -//////////////////////////////// low-level functions //////////////////////////////// - -int LU(float* A, size_t astep, int m, float* b, size_t bstep, int n); -int LU(double* A, size_t astep, int m, double* b, size_t bstep, int n); -bool Cholesky(float* A, size_t astep, int m, float* b, size_t bstep, int n); -bool Cholesky(double* A, size_t astep, int m, double* b, size_t bstep, int n); - -int normL1_(const uchar* a, const uchar* b, int n); -float normL1_(const float* a, const float* b, int n); -float normL2Sqr_(const float* a, const float* b, int n); - -void exp(const float* src, float* dst, int n); -void exp(const double* src, double* dst, int n); -void log(const float* src, float* dst, int n); -void log(const double* src, double* dst, int n); - -void fastAtan2(const float* y, const float* x, float* dst, int n, bool angleInDegrees); -void magnitude(const float* x, const float* y, float* dst, int n); -void magnitude(const double* x, const double* y, double* dst, int n); -void sqrt(const float* src, float* dst, int len); -void sqrt(const double* src, double* dst, int len); -void invSqrt(const float* src, float* dst, int len); -void invSqrt(const double* src, double* dst, int len); - -void split8u(const uchar* src, uchar** dst, int len, int cn ); -void split16u(const ushort* src, ushort** dst, int len, int cn ); -void split32s(const int* src, int** dst, int len, int cn ); -void split64s(const int64* src, int64** dst, int len, int cn ); - -void merge8u(const uchar** src, uchar* dst, int len, int cn ); -void merge16u(const ushort** src, ushort* dst, int len, int cn ); -void merge32s(const int** src, int* dst, int len, int cn ); -void merge64s(const int64** src, int64* dst, int len, int cn ); - -void add8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void add8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); -void add16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); -void add16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); -void add32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); -void add32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); -void add64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); - -void sub8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void sub8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); -void sub16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); -void sub16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); -void sub32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); -void sub32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); -void sub64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); - -void max8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void max8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); -void max16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); -void max16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); -void max32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); -void max32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); -void max64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); - -void min8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void min8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); -void min16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); -void min16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); -void min32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); -void min32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); -void min64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); - -void absdiff8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void absdiff8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* ); -void absdiff16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* ); -void absdiff16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* ); -void absdiff32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* ); -void absdiff32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* ); -void absdiff64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* ); - -void and8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void or8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void xor8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); -void not8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* ); - -void cmp8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp8s(const schar* src1, size_t step1, const schar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp16u(const ushort* src1, size_t step1, const ushort* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp16s(const short* src1, size_t step1, const short* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp32s(const int* src1, size_t step1, const int* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp32f(const float* src1, size_t step1, const float* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); -void cmp64f(const double* src1, size_t step1, const double* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _cmpop); - -void mul8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); -void mul8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); -void mul16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); -void mul16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); -void mul32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); -void mul32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); -void mul64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); - -void div8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); -void div8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); -void div16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); -void div16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); -void div32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); -void div32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); -void div64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); - -void recip8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* scale); -void recip8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scale); -void recip16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scale); -void recip16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scale); -void recip32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scale); -void recip32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scale); -void recip64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scale); - -void addWeighted8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height, void* _scalars ); -void addWeighted8s( const schar* src1, size_t step1, const schar* src2, size_t step2, schar* dst, size_t step, int width, int height, void* scalars ); -void addWeighted16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, ushort* dst, size_t step, int width, int height, void* scalars ); -void addWeighted16s( const short* src1, size_t step1, const short* src2, size_t step2, short* dst, size_t step, int width, int height, void* scalars ); -void addWeighted32s( const int* src1, size_t step1, const int* src2, size_t step2, int* dst, size_t step, int width, int height, void* scalars ); -void addWeighted32f( const float* src1, size_t step1, const float* src2, size_t step2, float* dst, size_t step, int width, int height, void* scalars ); -void addWeighted64f( const double* src1, size_t step1, const double* src2, size_t step2, double* dst, size_t step, int width, int height, void* scalars ); -//! @} - -}} //cv::hal - -namespace cv { - -template struct OpAdd -{ - typedef T1 type1; - typedef T2 type2; - typedef T3 rtype; - T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(a + b); } -}; - -template struct OpSub -{ - typedef T1 type1; - typedef T2 type2; - typedef T3 rtype; - T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(a - b); } -}; - -template struct OpRSub -{ - typedef T1 type1; - typedef T2 type2; - typedef T3 rtype; - T3 operator ()(const T1 a, const T2 b) const { return saturate_cast(b - a); } -}; - -template struct OpMin -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator ()(const T a, const T b) const { return std::min(a, b); } -}; - -template struct OpMax -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator ()(const T a, const T b) const { return std::max(a, b); } -}; - -template struct OpAbsDiff -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator()(T a, T b) const { return a > b ? a - b : b - a; } -}; - -template struct OpAnd -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator()( T a, T b ) const { return a & b; } -}; - -template struct OpOr -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator()( T a, T b ) const { return a | b; } -}; - -template struct OpXor -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator()( T a, T b ) const { return a ^ b; } -}; - -template struct OpNot -{ - typedef T type1; - typedef T type2; - typedef T rtype; - T operator()( T a, T ) const { return ~a; } -}; - -} - -#endif //__OPENCV_HAL_HPP__ diff --git a/modules/hal/include/opencv2/hal/defs.h b/modules/hal/include/opencv2/hal/defs.h deleted file mode 100644 index 117ec6046..000000000 --- a/modules/hal/include/opencv2/hal/defs.h +++ /dev/null @@ -1,675 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Copyright (C) 2015, Itseez Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef __OPENCV_DEF_H__ -#define __OPENCV_DEF_H__ - -//! @addtogroup hal_utils -//! @{ - -#if !defined _CRT_SECURE_NO_DEPRECATE && defined _MSC_VER && _MSC_VER > 1300 -# define _CRT_SECURE_NO_DEPRECATE /* to avoid multiple Visual Studio warnings */ -#endif - -#include -#include "opencv2/hal/interface.hpp" - -#if defined __ICL -# define CV_ICC __ICL -#elif defined __ICC -# define CV_ICC __ICC -#elif defined __ECL -# define CV_ICC __ECL -#elif defined __ECC -# define CV_ICC __ECC -#elif defined __INTEL_COMPILER -# define CV_ICC __INTEL_COMPILER -#endif - -#ifndef CV_INLINE -# if defined __cplusplus -# define CV_INLINE static inline -# elif defined _MSC_VER -# define CV_INLINE __inline -# else -# define CV_INLINE static -# endif -#endif - -#if defined CV_ICC && !defined CV_ENABLE_UNROLLED -# define CV_ENABLE_UNROLLED 0 -#else -# define CV_ENABLE_UNROLLED 1 -#endif - -#ifdef __GNUC__ -# define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x))) -#elif defined _MSC_VER -# define CV_DECL_ALIGNED(x) __declspec(align(x)) -#else -# define CV_DECL_ALIGNED(x) -#endif - -/* CPU features and intrinsics support */ -#define CV_CPU_NONE 0 -#define CV_CPU_MMX 1 -#define CV_CPU_SSE 2 -#define CV_CPU_SSE2 3 -#define CV_CPU_SSE3 4 -#define CV_CPU_SSSE3 5 -#define CV_CPU_SSE4_1 6 -#define CV_CPU_SSE4_2 7 -#define CV_CPU_POPCNT 8 - -#define CV_CPU_AVX 10 -#define CV_CPU_AVX2 11 -#define CV_CPU_FMA3 12 - -#define CV_CPU_AVX_512F 13 -#define CV_CPU_AVX_512BW 14 -#define CV_CPU_AVX_512CD 15 -#define CV_CPU_AVX_512DQ 16 -#define CV_CPU_AVX_512ER 17 -#define CV_CPU_AVX_512IFMA512 18 -#define CV_CPU_AVX_512PF 19 -#define CV_CPU_AVX_512VBMI 20 -#define CV_CPU_AVX_512VL 21 - -#define CV_CPU_NEON 100 - -// when adding to this list remember to update the following enum -#define CV_HARDWARE_MAX_FEATURE 255 - -/** @brief Available CPU features. -*/ -enum CpuFeatures { - CPU_MMX = 1, - CPU_SSE = 2, - CPU_SSE2 = 3, - CPU_SSE3 = 4, - CPU_SSSE3 = 5, - CPU_SSE4_1 = 6, - CPU_SSE4_2 = 7, - CPU_POPCNT = 8, - - CPU_AVX = 10, - CPU_AVX2 = 11, - CPU_FMA3 = 12, - - CPU_AVX_512F = 13, - CPU_AVX_512BW = 14, - CPU_AVX_512CD = 15, - CPU_AVX_512DQ = 16, - CPU_AVX_512ER = 17, - CPU_AVX_512IFMA512 = 18, - CPU_AVX_512PF = 19, - CPU_AVX_512VBMI = 20, - CPU_AVX_512VL = 21, - - CPU_NEON = 100 -}; - -// do not include SSE/AVX/NEON headers for NVCC compiler -#ifndef __CUDACC__ - -#if defined __SSE2__ || defined _M_X64 || (defined _M_IX86_FP && _M_IX86_FP >= 2) -# include -# define CV_MMX 1 -# define CV_SSE 1 -# define CV_SSE2 1 -# if defined __SSE3__ || (defined _MSC_VER && _MSC_VER >= 1500) -# include -# define CV_SSE3 1 -# endif -# if defined __SSSE3__ || (defined _MSC_VER && _MSC_VER >= 1500) -# include -# define CV_SSSE3 1 -# endif -# if defined __SSE4_1__ || (defined _MSC_VER && _MSC_VER >= 1500) -# include -# define CV_SSE4_1 1 -# endif -# if defined __SSE4_2__ || (defined _MSC_VER && _MSC_VER >= 1500) -# include -# define CV_SSE4_2 1 -# endif -# if defined __POPCNT__ || (defined _MSC_VER && _MSC_VER >= 1500) -# ifdef _MSC_VER -# include -# else -# include -# endif -# define CV_POPCNT 1 -# endif -# if defined __AVX__ || (defined _MSC_VER && _MSC_VER >= 1600 && 0) -// MS Visual Studio 2010 (2012?) has no macro pre-defined to identify the use of /arch:AVX -// See: http://connect.microsoft.com/VisualStudio/feedback/details/605858/arch-avx-should-define-a-predefined-macro-in-x64-and-set-a-unique-value-for-m-ix86-fp-in-win32 -# include -# define CV_AVX 1 -# if defined(_XCR_XFEATURE_ENABLED_MASK) -# define __xgetbv() _xgetbv(_XCR_XFEATURE_ENABLED_MASK) -# else -# define __xgetbv() 0 -# endif -# endif -# if defined __AVX2__ || (defined _MSC_VER && _MSC_VER >= 1800 && 0) -# include -# define CV_AVX2 1 -# if defined __FMA__ -# define CV_FMA3 1 -# endif -# endif -#endif - -#if (defined WIN32 || defined _WIN32) && defined(_M_ARM) -# include -# include "arm_neon.h" -# define CV_NEON 1 -# define CPU_HAS_NEON_FEATURE (true) -#elif defined(__ARM_NEON__) || (defined (__ARM_NEON) && defined(__aarch64__)) -# include -# define CV_NEON 1 -#endif - -#if defined __GNUC__ && defined __arm__ && (defined __ARM_PCS_VFP || defined __ARM_VFPV3__ || defined __ARM_NEON__) && !defined __SOFTFP__ -# define CV_VFP 1 -#endif - -#endif // __CUDACC__ - -#ifndef CV_POPCNT -#define CV_POPCNT 0 -#endif -#ifndef CV_MMX -# define CV_MMX 0 -#endif -#ifndef CV_SSE -# define CV_SSE 0 -#endif -#ifndef CV_SSE2 -# define CV_SSE2 0 -#endif -#ifndef CV_SSE3 -# define CV_SSE3 0 -#endif -#ifndef CV_SSSE3 -# define CV_SSSE3 0 -#endif -#ifndef CV_SSE4_1 -# define CV_SSE4_1 0 -#endif -#ifndef CV_SSE4_2 -# define CV_SSE4_2 0 -#endif -#ifndef CV_AVX -# define CV_AVX 0 -#endif -#ifndef CV_AVX2 -# define CV_AVX2 0 -#endif -#ifndef CV_FMA3 -# define CV_FMA3 0 -#endif -#ifndef CV_AVX_512F -# define CV_AVX_512F 0 -#endif -#ifndef CV_AVX_512BW -# define CV_AVX_512BW 0 -#endif -#ifndef CV_AVX_512CD -# define CV_AVX_512CD 0 -#endif -#ifndef CV_AVX_512DQ -# define CV_AVX_512DQ 0 -#endif -#ifndef CV_AVX_512ER -# define CV_AVX_512ER 0 -#endif -#ifndef CV_AVX_512IFMA512 -# define CV_AVX_512IFMA512 0 -#endif -#ifndef CV_AVX_512PF -# define CV_AVX_512PF 0 -#endif -#ifndef CV_AVX_512VBMI -# define CV_AVX_512VBMI 0 -#endif -#ifndef CV_AVX_512VL -# define CV_AVX_512VL 0 -#endif - -#ifndef CV_NEON -# define CV_NEON 0 -#endif - -#ifndef CV_VFP -# define CV_VFP 0 -#endif - -/* fundamental constants */ -#define CV_PI 3.1415926535897932384626433832795 -#define CV_2PI 6.283185307179586476925286766559 -#define CV_LOG2 0.69314718055994530941723212145818 - -typedef union Cv32suf -{ - int i; - unsigned u; - float f; -} -Cv32suf; - -typedef union Cv64suf -{ - int64 i; - uint64 u; - double f; -} -Cv64suf; - -namespace cv { namespace hal { - -bool checkHardwareSupport(int feature); -void setUseOptimized(bool onoff); -bool useOptimized(); - -}} - -#define USE_SSE2 (cv::hal::checkHardwareSupport(CV_CPU_SSE)) -#define USE_SSE4_2 (cv::hal::checkHardwareSupport(CV_CPU_SSE4_2)) -#define USE_AVX (cv::hal::checkHardwareSupport(CV_CPU_AVX)) -#define USE_AVX2 (cv::hal::checkHardwareSupport(CV_CPU_AVX2)) - - -/****************************************************************************************\ -* fast math * -\****************************************************************************************/ - -#if defined __BORLANDC__ -# include -#elif defined __cplusplus -# include -#else -# include -#endif - -#ifdef HAVE_TEGRA_OPTIMIZATION -# include "tegra_round.hpp" -#endif - -#if CV_VFP - // 1. general scheme - #define ARM_ROUND(_value, _asm_string) \ - int res; \ - float temp; \ - asm(_asm_string : [res] "=r" (res), [temp] "=w" (temp) : [value] "w" (_value)); \ - return res - // 2. version for double - #ifdef __clang__ - #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %[value] \n vmov %[res], %[temp]") - #else - #define ARM_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %P[value] \n vmov %[res], %[temp]") - #endif - // 3. version for float - #define ARM_ROUND_FLT(value) ARM_ROUND(value, "vcvtr.s32.f32 %[temp], %[value]\n vmov %[res], %[temp]") -#endif // CV_VFP - -/** @brief Rounds floating-point number to the nearest integer - - @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the - result is not defined. - */ -CV_INLINE int -cvRound( double value ) -{ -#if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ \ - && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) - __m128d t = _mm_set_sd( value ); - return _mm_cvtsd_si32(t); -#elif defined _MSC_VER && defined _M_IX86 - int t; - __asm - { - fld value; - fistp t; - } - return t; -#elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \ - defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION - TEGRA_ROUND_DBL(value); -#elif defined CV_ICC || defined __GNUC__ -# if CV_VFP - ARM_ROUND_DBL(value); -# else - return (int)lrint(value); -# endif -#else - /* it's ok if round does not comply with IEEE754 standard; - the tests should allow +/-1 difference when the tested functions use round */ - return (int)(value + (value >= 0 ? 0.5 : -0.5)); -#endif -} - - -/** @brief Rounds floating-point number to the nearest integer not larger than the original. - - The function computes an integer i such that: - \f[i \le \texttt{value} < i+1\f] - @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the - result is not defined. - */ -CV_INLINE int cvFloor( double value ) -{ -#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) - __m128d t = _mm_set_sd( value ); - int i = _mm_cvtsd_si32(t); - return i - _mm_movemask_pd(_mm_cmplt_sd(t, _mm_cvtsi32_sd(t,i))); -#elif defined __GNUC__ - int i = (int)value; - return i - (i > value); -#else - int i = cvRound(value); - float diff = (float)(value - i); - return i - (diff < 0); -#endif -} - -/** @brief Rounds floating-point number to the nearest integer not larger than the original. - - The function computes an integer i such that: - \f[i \le \texttt{value} < i+1\f] - @param value floating-point number. If the value is outside of INT_MIN ... INT_MAX range, the - result is not defined. - */ -CV_INLINE int cvCeil( double value ) -{ -#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__) - __m128d t = _mm_set_sd( value ); - int i = _mm_cvtsd_si32(t); - return i + _mm_movemask_pd(_mm_cmplt_sd(_mm_cvtsi32_sd(t,i), t)); -#elif defined __GNUC__ - int i = (int)value; - return i + (i < value); -#else - int i = cvRound(value); - float diff = (float)(i - value); - return i + (diff < 0); -#endif -} - -/** @brief Determines if the argument is Not A Number. - - @param value The input floating-point value - - The function returns 1 if the argument is Not A Number (as defined by IEEE754 standard), 0 - otherwise. */ -CV_INLINE int cvIsNaN( double value ) -{ - Cv64suf ieee754; - ieee754.f = value; - return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) + - ((unsigned)ieee754.u != 0) > 0x7ff00000; -} - -/** @brief Determines if the argument is Infinity. - - @param value The input floating-point value - - The function returns 1 if the argument is a plus or minus infinity (as defined by IEEE754 standard) - and 0 otherwise. */ -CV_INLINE int cvIsInf( double value ) -{ - Cv64suf ieee754; - ieee754.f = value; - return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 && - (unsigned)ieee754.u == 0; -} - -#ifdef __cplusplus - -/** @overload */ -CV_INLINE int cvRound(float value) -{ -#if ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ && \ - defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) - __m128 t = _mm_set_ss( value ); - return _mm_cvtss_si32(t); -#elif defined _MSC_VER && defined _M_IX86 - int t; - __asm - { - fld value; - fistp t; - } - return t; -#elif ((defined _MSC_VER && defined _M_ARM) || defined CV_ICC || \ - defined __GNUC__) && defined HAVE_TEGRA_OPTIMIZATION - TEGRA_ROUND_FLT(value); -#elif defined CV_ICC || defined __GNUC__ -# if CV_VFP - ARM_ROUND_FLT(value); -# else - return (int)lrintf(value); -# endif -#else - /* it's ok if round does not comply with IEEE754 standard; - the tests should allow +/-1 difference when the tested functions use round */ - return (int)(value + (value >= 0 ? 0.5f : -0.5f)); -#endif -} - -/** @overload */ -CV_INLINE int cvRound( int value ) -{ - return value; -} - -/** @overload */ -CV_INLINE int cvFloor( float value ) -{ -#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__ && !defined __APPLE__)) && !defined(__CUDACC__) - __m128 t = _mm_set_ss( value ); - int i = _mm_cvtss_si32(t); - return i - _mm_movemask_ps(_mm_cmplt_ss(t, _mm_cvtsi32_ss(t,i))); -#elif defined __GNUC__ - int i = (int)value; - return i - (i > value); -#else - int i = cvRound(value); - float diff = (float)(value - i); - return i - (diff < 0); -#endif -} - -/** @overload */ -CV_INLINE int cvFloor( int value ) -{ - return value; -} - -/** @overload */ -CV_INLINE int cvCeil( float value ) -{ -#if (defined _MSC_VER && defined _M_X64 || (defined __GNUC__ && defined __SSE2__&& !defined __APPLE__)) && !defined(__CUDACC__) - __m128 t = _mm_set_ss( value ); - int i = _mm_cvtss_si32(t); - return i + _mm_movemask_ps(_mm_cmplt_ss(_mm_cvtsi32_ss(t,i), t)); -#elif defined __GNUC__ - int i = (int)value; - return i + (i < value); -#else - int i = cvRound(value); - float diff = (float)(i - value); - return i + (diff < 0); -#endif -} - -/** @overload */ -CV_INLINE int cvCeil( int value ) -{ - return value; -} - -/** @overload */ -CV_INLINE int cvIsNaN( float value ) -{ - Cv32suf ieee754; - ieee754.f = value; - return (ieee754.u & 0x7fffffff) > 0x7f800000; -} - -/** @overload */ -CV_INLINE int cvIsInf( float value ) -{ - Cv32suf ieee754; - ieee754.f = value; - return (ieee754.u & 0x7fffffff) == 0x7f800000; -} - -//! @} - -#include - -namespace cv -{ - -//! @addtogroup hal_utils -//! @{ - -/////////////// saturate_cast (used in image & signal processing) /////////////////// - -/** @brief Template function for accurate conversion from one primitive type to another. - - The functions saturate_cast resemble the standard C++ cast operations, such as static_cast\() - and others. They perform an efficient and accurate conversion from one primitive type to another - (see the introduction chapter). saturate in the name means that when the input value v is out of the - range of the target type, the result is not formed just by taking low bits of the input, but instead - the value is clipped. For example: - @code - uchar a = saturate_cast(-100); // a = 0 (UCHAR_MIN) - short b = saturate_cast(33333.33333); // b = 32767 (SHRT_MAX) - @endcode - Such clipping is done when the target type is unsigned char , signed char , unsigned short or - signed short . For 32-bit integers, no clipping is done. - - When the parameter is a floating-point value and the target type is an integer (8-, 16- or 32-bit), - the floating-point value is first rounded to the nearest integer and then clipped if needed (when - the target type is 8- or 16-bit). - - This operation is used in the simplest or most complex image processing functions in OpenCV. - - @param v Function parameter. - @sa add, subtract, multiply, divide, Mat::convertTo - */ -template static inline _Tp saturate_cast(uchar v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(schar v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(ushort v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(short v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(unsigned v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(int v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(float v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(double v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(int64 v) { return _Tp(v); } -/** @overload */ -template static inline _Tp saturate_cast(uint64 v) { return _Tp(v); } - -template<> inline uchar saturate_cast(schar v) { return (uchar)std::max((int)v, 0); } -template<> inline uchar saturate_cast(ushort v) { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); } -template<> inline uchar saturate_cast(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } -template<> inline uchar saturate_cast(short v) { return saturate_cast((int)v); } -template<> inline uchar saturate_cast(unsigned v) { return (uchar)std::min(v, (unsigned)UCHAR_MAX); } -template<> inline uchar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline uchar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline uchar saturate_cast(int64 v) { return (uchar)((uint64)v <= (uint64)UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } -template<> inline uchar saturate_cast(uint64 v) { return (uchar)std::min(v, (uint64)UCHAR_MAX); } - -template<> inline schar saturate_cast(uchar v) { return (schar)std::min((int)v, SCHAR_MAX); } -template<> inline schar saturate_cast(ushort v) { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); } -template<> inline schar saturate_cast(int v) { return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } -template<> inline schar saturate_cast(short v) { return saturate_cast((int)v); } -template<> inline schar saturate_cast(unsigned v) { return (schar)std::min(v, (unsigned)SCHAR_MAX); } -template<> inline schar saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline schar saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline schar saturate_cast(int64 v) { return (schar)((uint64)((int64)v-SCHAR_MIN) <= (uint64)UCHAR_MAX ? v : v > 0 ? SCHAR_MAX : SCHAR_MIN); } -template<> inline schar saturate_cast(uint64 v) { return (schar)std::min(v, (uint64)SCHAR_MAX); } - -template<> inline ushort saturate_cast(schar v) { return (ushort)std::max((int)v, 0); } -template<> inline ushort saturate_cast(short v) { return (ushort)std::max((int)v, 0); } -template<> inline ushort saturate_cast(int v) { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } -template<> inline ushort saturate_cast(unsigned v) { return (ushort)std::min(v, (unsigned)USHRT_MAX); } -template<> inline ushort saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline ushort saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline ushort saturate_cast(int64 v) { return (ushort)((uint64)v <= (uint64)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); } -template<> inline ushort saturate_cast(uint64 v) { return (ushort)std::min(v, (uint64)USHRT_MAX); } - -template<> inline short saturate_cast(ushort v) { return (short)std::min((int)v, SHRT_MAX); } -template<> inline short saturate_cast(int v) { return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } -template<> inline short saturate_cast(unsigned v) { return (short)std::min(v, (unsigned)SHRT_MAX); } -template<> inline short saturate_cast(float v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline short saturate_cast(double v) { int iv = cvRound(v); return saturate_cast(iv); } -template<> inline short saturate_cast(int64 v) { return (short)((uint64)((int64)v - SHRT_MIN) <= (uint64)USHRT_MAX ? v : v > 0 ? SHRT_MAX : SHRT_MIN); } -template<> inline short saturate_cast(uint64 v) { return (short)std::min(v, (uint64)SHRT_MAX); } - -template<> inline int saturate_cast(float v) { return cvRound(v); } -template<> inline int saturate_cast(double v) { return cvRound(v); } - -// we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc. -template<> inline unsigned saturate_cast(float v) { return cvRound(v); } -template<> inline unsigned saturate_cast(double v) { return cvRound(v); } - -//! @} - -} - -#endif // __cplusplus - -#endif //__OPENCV_HAL_H__ diff --git a/modules/hal/samples/simple_hal/CMakeLists.txt b/modules/hal/samples/simple_hal/CMakeLists.txt deleted file mode 100644 index dd0be70f2..000000000 --- a/modules/hal/samples/simple_hal/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR) - -if(UNIX) - if(CMAKE_COMPILER_IS_GNUCXX OR CV_ICC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") - endif() -endif() - -add_library(simple_hal simple.cpp) -set(OPENCV_HAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") -target_include_directories(simple_hal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${OPENCV_HAL_DIR}/include) diff --git a/modules/hal/src/arithm.cpp b/modules/hal/src/arithm.cpp deleted file mode 100644 index e30cd7d9e..000000000 --- a/modules/hal/src/arithm.cpp +++ /dev/null @@ -1,1131 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Copyright (C) 2015, Itseez Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" -#include "arithm_simd.hpp" -#include "arithm_core.hpp" -#include "replacement.hpp" - -namespace cv { namespace hal { - -//======================================= - -#undef CALL_HAL -#define CALL_HAL(fun) \ - int res = fun(src1, step1, src2, step2, dst, step, width, height); \ - if (res == Error::Ok) \ - return; \ - else if (res != Error::NotImplemented) \ - throw Failure(res); - -#if (ARITHM_USE_IPP == 1) -static inline void fixSteps(width, height, size_t elemSize, size_t& step1, size_t& step2, size_t& step) -{ - if( height == 1 ) - step1 = step2 = step = width*elemSize; -} -#define CALL_IPP_BIN_12(fun) \ - CV_IPP_CHECK() \ - { \ - fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ - if (0 <= fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), 0)) \ - { \ - CV_IMPL_ADD(CV_IMPL_IPP); \ - return; \ - } \ - setIppErrorStatus(); \ - } -#else -#define CALL_IPP_BIN_12(fun) -#endif - -//======================================= -// Add -//======================================= - -void add8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add8u) - CALL_IPP_BIN_12(ippiAdd_8u_C1RSfs) - (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void add8s( const schar* src1, size_t step1, - const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add8s) - vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); -} - -void add16u( const ushort* src1, size_t step1, - const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add16u) - CALL_IPP_BIN_12(ippiAdd_16u_C1RSfs) - (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void add16s( const short* src1, size_t step1, - const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add16s) - CALL_IPP_BIN_12(ippiAdd_16s_C1RSfs) - (vBinOp, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void add32s( const int* src1, size_t step1, - const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add32s) - vBinOp32, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); -} - -void add32f( const float* src1, size_t step1, - const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add32f) - CALL_IPP_BIN_12(ippiAdd_32f_C1R) - (vBinOp32, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void add64f( const double* src1, size_t step1, - const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_add64f) - vBinOp64, IF_SIMD(VAdd)>(src1, step1, src2, step2, dst, step, width, height); -} - -//======================================= - -#if (ARITHM_USE_IPP == 1) -#define CALL_IPP_BIN_21(fun) \ - CV_IPP_CHECK() \ - { \ - fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ - if (0 <= fun(src2, (int)step2, src1, (int)step1, dst, (int)step, ippiSize(width, height), 0)) \ - { \ - CV_IMPL_ADD(CV_IMPL_IPP); \ - return; \ - } \ - setIppErrorStatus(); \ - } -#else -#define CALL_IPP_BIN_21(fun) -#endif - -//======================================= -// Subtract -//======================================= - -void sub8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub8u) - CALL_IPP_BIN_21(ippiSub_8u_C1RSfs) - (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void sub8s( const schar* src1, size_t step1, - const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub8s) - vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); -} - -void sub16u( const ushort* src1, size_t step1, - const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub16u) - CALL_IPP_BIN_21(ippiSub_16u_C1RSfs) - (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void sub16s( const short* src1, size_t step1, - const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub16s) - CALL_IPP_BIN_21(ippiSub_16s_C1RSfs) - (vBinOp, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void sub32s( const int* src1, size_t step1, - const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub32s) - vBinOp32, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); -} - -void sub32f( const float* src1, size_t step1, - const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub32f) - CALL_IPP_BIN_21(ippiSub_32f_C1R) - (vBinOp32, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void sub64f( const double* src1, size_t step1, - const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_sub64f) - vBinOp64, IF_SIMD(VSub)>(src1, step1, src2, step2, dst, step, width, height); -} - -//======================================= - -#if (ARITHM_USE_IPP == 1) -#define CALL_IPP_MIN_MAX(fun, type) \ - CV_IPP_CHECK() \ - { \ - type* s1 = (type*)src1; \ - type* s2 = (type*)src2; \ - type* d = dst; \ - fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ - int i = 0; \ - for(; i < height; i++) \ - { \ - if (0 > fun(s1, s2, d, width)) \ - break; \ - s1 = (type*)((uchar*)s1 + step1); \ - s2 = (type*)((uchar*)s2 + step2); \ - d = (type*)((uchar*)d + step); \ - } \ - if (i == height) \ - { \ - CV_IMPL_ADD(CV_IMPL_IPP); \ - return; \ - } \ - setIppErrorStatus(); \ - } -#else -#define CALL_IPP_MIN_MAX(fun, type) -#endif - -//======================================= -// Max -//======================================= - -void max8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max8u) - CALL_IPP_MIN_MAX(ippsMaxEvery_8u, uchar) - vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max8s( const schar* src1, size_t step1, - const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max8s) - vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max16u( const ushort* src1, size_t step1, - const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max16u) - CALL_IPP_MIN_MAX(ippsMaxEvery_16u, ushort) - vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max16s( const short* src1, size_t step1, - const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max16s) - vBinOp, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max32s( const int* src1, size_t step1, - const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max32s) - vBinOp32, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max32f( const float* src1, size_t step1, - const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max32f) - CALL_IPP_MIN_MAX(ippsMaxEvery_32f, float) - vBinOp32, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -void max64f( const double* src1, size_t step1, - const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_max64f) - CALL_IPP_MIN_MAX(ippsMaxEvery_64f, double) - vBinOp64, IF_SIMD(VMax)>(src1, step1, src2, step2, dst, step, width, height); -} - -//======================================= -// Min -//======================================= - -void min8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min8u) - CALL_IPP_MIN_MAX(ippsMinEvery_8u, uchar) - vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min8s( const schar* src1, size_t step1, - const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min8s) - vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min16u( const ushort* src1, size_t step1, - const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min16u) - CALL_IPP_MIN_MAX(ippsMinEvery_16u, ushort) - vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min16s( const short* src1, size_t step1, - const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min16s) - vBinOp, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min32s( const int* src1, size_t step1, - const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min32s) - vBinOp32, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min32f( const float* src1, size_t step1, - const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min32f) - CALL_IPP_MIN_MAX(ippsMinEvery_32f, float) - vBinOp32, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -void min64f( const double* src1, size_t step1, - const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_min64f) - CALL_IPP_MIN_MAX(ippsMinEvery_64f, double) - vBinOp64, IF_SIMD(VMin)>(src1, step1, src2, step2, dst, step, width, height); -} - -//======================================= -// AbsDiff -//======================================= - -void absdiff8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff8u) - CALL_IPP_BIN_12(ippiAbsDiff_8u_C1R) - (vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void absdiff8s( const schar* src1, size_t step1, - const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff8s) - vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); -} - -void absdiff16u( const ushort* src1, size_t step1, - const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff16u) - CALL_IPP_BIN_12(ippiAbsDiff_16u_C1R) - (vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void absdiff16s( const short* src1, size_t step1, - const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff16s) - vBinOp, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); -} - -void absdiff32s( const int* src1, size_t step1, - const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff32s) - vBinOp32, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); -} - -void absdiff32f( const float* src1, size_t step1, - const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff32f) - CALL_IPP_BIN_12(ippiAbsDiff_32f_C1R) - (vBinOp32, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void absdiff64f( const double* src1, size_t step1, - const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_absdiff64f) - vBinOp64, IF_SIMD(VAbsDiff)>(src1, step1, src2, step2, dst, step, width, height); -} - -//======================================= -// Logical -//======================================= - -void and8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_and8u) - CALL_IPP_BIN_12(ippiAnd_8u_C1R) - (vBinOp, IF_SIMD(VAnd)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void or8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_or8u) - CALL_IPP_BIN_12(ippiOr_8u_C1R) - (vBinOp, IF_SIMD(VOr)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void xor8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_xor8u) - CALL_IPP_BIN_12(ippiXor_8u_C1R) - (vBinOp, IF_SIMD(VXor)>(src1, step1, src2, step2, dst, step, width, height)); -} - -void not8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* ) -{ - CALL_HAL(hal_not8u) - CALL_IPP_BIN_12(ippiNot_8u_C1R) - (vBinOp, IF_SIMD(VNot)>(src1, step1, src2, step2, dst, step, width, height)); -} - -//======================================= - -#undef CALL_HAL -#define CALL_HAL(fun) \ - int res = fun(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); \ - if (res == Error::Ok) \ - return; \ - else if (res != Error::NotImplemented) \ - throw Failure(res); - -#if ARITHM_USE_IPP -inline static IppCmpOp convert_cmp(int _cmpop) -{ - return _cmpop == CMP_EQ ? ippCmpEq : - _cmpop == CMP_GT ? ippCmpGreater : - _cmpop == CMP_GE ? ippCmpGreaterEq : - _cmpop == CMP_LT ? ippCmpLess : - _cmpop == CMP_LE ? ippCmpLessEq : - (IppCmpOp)-1; -} -#define CALL_IPP_CMP(fun) \ - CV_IPP_CHECK() \ - { \ - IppCmpOp op = convert_cmp(*(int *)_cmpop); \ - if( op >= 0 ) \ - { \ - fixSteps(width, height, sizeof(dst[0]), step1, step2, step); \ - if (0 <= fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), op)) \ - { \ - CV_IMPL_ADD(CV_IMPL_IPP); \ - return; \ - } \ - setIppErrorStatus(); \ - } \ - } -#else -#define CALL_IPP_CMP(fun) -#endif - -//======================================= -// Compare -//======================================= - -void cmp8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp8u) - CALL_IPP_CMP(ippiCompare_8u_C1R) - //vz optimized cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); - int code = *(int*)_cmpop; - step1 /= sizeof(src1[0]); - step2 /= sizeof(src2[0]); - if( code == CMP_GE || code == CMP_LT ) - { - std::swap(src1, src2); - std::swap(step1, step2); - code = code == CMP_GE ? CMP_LE : CMP_GT; - } - - if( code == CMP_GT || code == CMP_LE ) - { - int m = code == CMP_GT ? 0 : 255; - for( ; height--; src1 += step1, src2 += step2, dst += step ) - { - int x =0; - #if CV_SSE2 - if( USE_SSE2 ) - { - __m128i m128 = code == CMP_GT ? _mm_setzero_si128() : _mm_set1_epi8 (-1); - __m128i c128 = _mm_set1_epi8 (-128); - for( ; x <= width - 16; x += 16 ) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - // no simd for 8u comparison, that's why we need the trick - r00 = _mm_sub_epi8(r00,c128); - r10 = _mm_sub_epi8(r10,c128); - - r00 =_mm_xor_si128(_mm_cmpgt_epi8(r00, r10), m128); - _mm_storeu_si128((__m128i*)(dst + x),r00); - - } - } - #elif CV_NEON - uint8x16_t mask = code == CMP_GT ? vdupq_n_u8(0) : vdupq_n_u8(255); - - for( ; x <= width - 16; x += 16 ) - { - vst1q_u8(dst+x, veorq_u8(vcgtq_u8(vld1q_u8(src1+x), vld1q_u8(src2+x)), mask)); - } - - #endif - - for( ; x < width; x++ ){ - dst[x] = (uchar)(-(src1[x] > src2[x]) ^ m); - } - } - } - else if( code == CMP_EQ || code == CMP_NE ) - { - int m = code == CMP_EQ ? 0 : 255; - for( ; height--; src1 += step1, src2 += step2, dst += step ) - { - int x = 0; - #if CV_SSE2 - if( USE_SSE2 ) - { - __m128i m128 = code == CMP_EQ ? _mm_setzero_si128() : _mm_set1_epi8 (-1); - for( ; x <= width - 16; x += 16 ) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - r00 = _mm_xor_si128 ( _mm_cmpeq_epi8 (r00, r10), m128); - _mm_storeu_si128((__m128i*)(dst + x), r00); - } - } - #elif CV_NEON - uint8x16_t mask = code == CMP_EQ ? vdupq_n_u8(0) : vdupq_n_u8(255); - - for( ; x <= width - 16; x += 16 ) - { - vst1q_u8(dst+x, veorq_u8(vceqq_u8(vld1q_u8(src1+x), vld1q_u8(src2+x)), mask)); - } - #endif - for( ; x < width; x++ ) - dst[x] = (uchar)(-(src1[x] == src2[x]) ^ m); - } - } -} - -void cmp8s(const schar* src1, size_t step1, const schar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp8s) - cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); -} - -void cmp16u(const ushort* src1, size_t step1, const ushort* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp16u) - CALL_IPP_CMP(ippiCompare_16u_C1R) - cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); -} - -void cmp16s(const short* src1, size_t step1, const short* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp16s) - CALL_IPP_CMP(ippiCompare_16s_C1R) - //vz optimized cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); - - int code = *(int*)_cmpop; - step1 /= sizeof(src1[0]); - step2 /= sizeof(src2[0]); - if( code == CMP_GE || code == CMP_LT ) - { - std::swap(src1, src2); - std::swap(step1, step2); - code = code == CMP_GE ? CMP_LE : CMP_GT; - } - - if( code == CMP_GT || code == CMP_LE ) - { - int m = code == CMP_GT ? 0 : 255; - for( ; height--; src1 += step1, src2 += step2, dst += step ) - { - int x =0; - #if CV_SSE2 - if( USE_SSE2) - { - __m128i m128 = code == CMP_GT ? _mm_setzero_si128() : _mm_set1_epi16 (-1); - for( ; x <= width - 16; x += 16 ) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - r00 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r00, r10), m128); - __m128i r01 = _mm_loadu_si128((const __m128i*)(src1 + x + 8)); - __m128i r11 = _mm_loadu_si128((const __m128i*)(src2 + x + 8)); - r01 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r01, r11), m128); - r11 = _mm_packs_epi16(r00, r01); - _mm_storeu_si128((__m128i*)(dst + x), r11); - } - if( x <= width-8) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - r00 = _mm_xor_si128 ( _mm_cmpgt_epi16 (r00, r10), m128); - r10 = _mm_packs_epi16(r00, r00); - _mm_storel_epi64((__m128i*)(dst + x), r10); - - x += 8; - } - } - #elif CV_NEON - uint8x16_t mask = code == CMP_GT ? vdupq_n_u8(0) : vdupq_n_u8(255); - - for( ; x <= width - 16; x += 16 ) - { - int16x8_t in1 = vld1q_s16(src1 + x); - int16x8_t in2 = vld1q_s16(src2 + x); - uint8x8_t t1 = vmovn_u16(vcgtq_s16(in1, in2)); - - in1 = vld1q_s16(src1 + x + 8); - in2 = vld1q_s16(src2 + x + 8); - uint8x8_t t2 = vmovn_u16(vcgtq_s16(in1, in2)); - - vst1q_u8(dst+x, veorq_u8(vcombine_u8(t1, t2), mask)); - } - #endif - - for( ; x < width; x++ ){ - dst[x] = (uchar)(-(src1[x] > src2[x]) ^ m); - } - } - } - else if( code == CMP_EQ || code == CMP_NE ) - { - int m = code == CMP_EQ ? 0 : 255; - for( ; height--; src1 += step1, src2 += step2, dst += step ) - { - int x = 0; - #if CV_SSE2 - if( USE_SSE2 ) - { - __m128i m128 = code == CMP_EQ ? _mm_setzero_si128() : _mm_set1_epi16 (-1); - for( ; x <= width - 16; x += 16 ) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - r00 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r00, r10), m128); - __m128i r01 = _mm_loadu_si128((const __m128i*)(src1 + x + 8)); - __m128i r11 = _mm_loadu_si128((const __m128i*)(src2 + x + 8)); - r01 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r01, r11), m128); - r11 = _mm_packs_epi16(r00, r01); - _mm_storeu_si128((__m128i*)(dst + x), r11); - } - if( x <= width - 8) - { - __m128i r00 = _mm_loadu_si128((const __m128i*)(src1 + x)); - __m128i r10 = _mm_loadu_si128((const __m128i*)(src2 + x)); - r00 = _mm_xor_si128 ( _mm_cmpeq_epi16 (r00, r10), m128); - r10 = _mm_packs_epi16(r00, r00); - _mm_storel_epi64((__m128i*)(dst + x), r10); - - x += 8; - } - } - #elif CV_NEON - uint8x16_t mask = code == CMP_EQ ? vdupq_n_u8(0) : vdupq_n_u8(255); - - for( ; x <= width - 16; x += 16 ) - { - int16x8_t in1 = vld1q_s16(src1 + x); - int16x8_t in2 = vld1q_s16(src2 + x); - uint8x8_t t1 = vmovn_u16(vceqq_s16(in1, in2)); - - in1 = vld1q_s16(src1 + x + 8); - in2 = vld1q_s16(src2 + x + 8); - uint8x8_t t2 = vmovn_u16(vceqq_s16(in1, in2)); - - vst1q_u8(dst+x, veorq_u8(vcombine_u8(t1, t2), mask)); - } - #endif - for( ; x < width; x++ ) - dst[x] = (uchar)(-(src1[x] == src2[x]) ^ m); - } - } -} - -void cmp32s(const int* src1, size_t step1, const int* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp32s) - cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); -} - -void cmp32f(const float* src1, size_t step1, const float* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp32f) - CALL_IPP_CMP(ippiCompare_32f_C1R) - cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); -} - -void cmp64f(const double* src1, size_t step1, const double* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* _cmpop) -{ - CALL_HAL(hal_cmp64f) - cmp_(src1, step1, src2, step2, dst, step, width, height, *(int*)_cmpop); -} - -//======================================= - -#undef CALL_HAL -#define CALL_HAL(fun) \ - int res = fun(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); \ - if (res == Error::Ok) \ - return; \ - else if (res != Error::NotImplemented) \ - throw Failure(res); - -#if defined HAVE_IPP -#define CALL_IPP_MUL(fun) \ - CV_IPP_CHECK() \ - { \ - if (std::fabs(fscale - 1) <= FLT_EPSILON) \ - { \ - if (fun(src1, (int)step1, src2, (int)step2, dst, (int)step, ippiSize(width, height), 0) >= 0) \ - { \ - CV_IMPL_ADD(CV_IMPL_IPP); \ - return; \ - } \ - setIppErrorStatus(); \ - } \ - } -#else -#define CALL_IPP_MUL(fun) -#endif - -//======================================= -// Multilpy -//======================================= - -void mul8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul8u) - float fscale = (float)*(const double*)scale; - CALL_IPP_MUL(ippiMul_8u_C1RSfs) - mul_(src1, step1, src2, step2, dst, step, width, height, fscale); -} - -void mul8s( const schar* src1, size_t step1, const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul8s) - mul_(src1, step1, src2, step2, dst, step, width, height, (float)*(const double*)scale); -} - -void mul16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul16u) - float fscale = (float)*(const double*)scale; - CALL_IPP_MUL(ippiMul_16u_C1RSfs) - mul_(src1, step1, src2, step2, dst, step, width, height, fscale); -} - -void mul16s( const short* src1, size_t step1, const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul16s) - float fscale = (float)*(const double*)scale; - CALL_IPP_MUL(ippiMul_16s_C1RSfs) - mul_(src1, step1, src2, step2, dst, step, width, height, fscale); -} - -void mul32s( const int* src1, size_t step1, const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul32s) - mul_(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void mul32f( const float* src1, size_t step1, const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul32f) - float fscale = (float)*(const double*)scale; - CALL_IPP_MUL(ippiMul_32f_C1R) - mul_(src1, step1, src2, step2, dst, step, width, height, fscale); -} - -void mul64f( const double* src1, size_t step1, const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_mul64f) - mul_(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -//======================================= -// Divide -//======================================= - -void div8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div8u) - if( src1 ) - div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); - else - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div8s( const schar* src1, size_t step1, const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div8s) - div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div16u) - div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div16s( const short* src1, size_t step1, const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div16s) - div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div32s( const int* src1, size_t step1, const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div32s) - div_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div32f( const float* src1, size_t step1, const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div32f) - div_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void div64f( const double* src1, size_t step1, const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_div64f) - div_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -//======================================= -// Reciprocial -//======================================= - -void recip8u( const uchar* src1, size_t step1, const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip8u) - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip8s( const schar* src1, size_t step1, const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip8s) - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip16u) - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip16s( const short* src1, size_t step1, const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip16s) - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip32s( const int* src1, size_t step1, const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip32s) - recip_i(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip32f( const float* src1, size_t step1, const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip32f) - recip_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -void recip64f( const double* src1, size_t step1, const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* scale) -{ - CALL_HAL(hal_recip64f) - recip_f(src1, step1, src2, step2, dst, step, width, height, *(const double*)scale); -} - -//======================================= - -#undef CALL_HAL -#define CALL_HAL(fun) \ - int res = fun(src1, step1, src2, step2, dst, step, width, height, scalars); \ - if (res == Error::Ok) \ - return; \ - else if (res != Error::NotImplemented) \ - throw Failure(res); - -//======================================= -// Add weighted -//======================================= - -void -addWeighted8u( const uchar* src1, size_t step1, - const uchar* src2, size_t step2, - uchar* dst, size_t step, int width, int height, - void* scalars ) -{ - CALL_HAL(hal_addWeighted8u) - const double* scalars_ = (const double*)scalars; - float alpha = (float)scalars_[0], beta = (float)scalars_[1], gamma = (float)scalars_[2]; - - for( ; height--; src1 += step1, src2 += step2, dst += step ) - { - int x = 0; - -#if CV_SSE2 - if( USE_SSE2 ) - { - __m128 a4 = _mm_set1_ps(alpha), b4 = _mm_set1_ps(beta), g4 = _mm_set1_ps(gamma); - __m128i z = _mm_setzero_si128(); - - for( ; x <= width - 8; x += 8 ) - { - __m128i u = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(src1 + x)), z); - __m128i v = _mm_unpacklo_epi8(_mm_loadl_epi64((const __m128i*)(src2 + x)), z); - - __m128 u0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(u, z)); - __m128 u1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(u, z)); - __m128 v0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v, z)); - __m128 v1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v, z)); - - u0 = _mm_add_ps(_mm_mul_ps(u0, a4), _mm_mul_ps(v0, b4)); - u1 = _mm_add_ps(_mm_mul_ps(u1, a4), _mm_mul_ps(v1, b4)); - u0 = _mm_add_ps(u0, g4); u1 = _mm_add_ps(u1, g4); - - u = _mm_packs_epi32(_mm_cvtps_epi32(u0), _mm_cvtps_epi32(u1)); - u = _mm_packus_epi16(u, u); - - _mm_storel_epi64((__m128i*)(dst + x), u); - } - } -#elif CV_NEON - float32x4_t g = vdupq_n_f32 (gamma); - - for( ; x <= width - 8; x += 8 ) - { - uint8x8_t in1 = vld1_u8(src1+x); - uint16x8_t in1_16 = vmovl_u8(in1); - float32x4_t in1_f_l = vcvtq_f32_u32(vmovl_u16(vget_low_u16(in1_16))); - float32x4_t in1_f_h = vcvtq_f32_u32(vmovl_u16(vget_high_u16(in1_16))); - - uint8x8_t in2 = vld1_u8(src2+x); - uint16x8_t in2_16 = vmovl_u8(in2); - float32x4_t in2_f_l = vcvtq_f32_u32(vmovl_u16(vget_low_u16(in2_16))); - float32x4_t in2_f_h = vcvtq_f32_u32(vmovl_u16(vget_high_u16(in2_16))); - - float32x4_t out_f_l = vaddq_f32(vmulq_n_f32(in1_f_l, alpha), vmulq_n_f32(in2_f_l, beta)); - float32x4_t out_f_h = vaddq_f32(vmulq_n_f32(in1_f_h, alpha), vmulq_n_f32(in2_f_h, beta)); - out_f_l = vaddq_f32(out_f_l, g); - out_f_h = vaddq_f32(out_f_h, g); - - uint16x4_t out_16_l = vqmovun_s32(cv_vrndq_s32_f32(out_f_l)); - uint16x4_t out_16_h = vqmovun_s32(cv_vrndq_s32_f32(out_f_h)); - - uint16x8_t out_16 = vcombine_u16(out_16_l, out_16_h); - uint8x8_t out = vqmovn_u16(out_16); - - vst1_u8(dst+x, out); - } -#endif - #if CV_ENABLE_UNROLLED - for( ; x <= width - 4; x += 4 ) - { - float t0, t1; - t0 = CV_8TO32F(src1[x])*alpha + CV_8TO32F(src2[x])*beta + gamma; - t1 = CV_8TO32F(src1[x+1])*alpha + CV_8TO32F(src2[x+1])*beta + gamma; - - dst[x] = saturate_cast(t0); - dst[x+1] = saturate_cast(t1); - - t0 = CV_8TO32F(src1[x+2])*alpha + CV_8TO32F(src2[x+2])*beta + gamma; - t1 = CV_8TO32F(src1[x+3])*alpha + CV_8TO32F(src2[x+3])*beta + gamma; - - dst[x+2] = saturate_cast(t0); - dst[x+3] = saturate_cast(t1); - } - #endif - - for( ; x < width; x++ ) - { - float t0 = CV_8TO32F(src1[x])*alpha + CV_8TO32F(src2[x])*beta + gamma; - dst[x] = saturate_cast(t0); - } - } -} - -void addWeighted8s( const schar* src1, size_t step1, const schar* src2, size_t step2, - schar* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted8s) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -void addWeighted16u( const ushort* src1, size_t step1, const ushort* src2, size_t step2, - ushort* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted16u) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -void addWeighted16s( const short* src1, size_t step1, const short* src2, size_t step2, - short* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted16s) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -void addWeighted32s( const int* src1, size_t step1, const int* src2, size_t step2, - int* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted32s) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -void addWeighted32f( const float* src1, size_t step1, const float* src2, size_t step2, - float* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted32f) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -void addWeighted64f( const double* src1, size_t step1, const double* src2, size_t step2, - double* dst, size_t step, int width, int height, void* scalars ) -{ - CALL_HAL(hal_addWeighted64f) - addWeighted_(src1, step1, src2, step2, dst, step, width, height, scalars); -} - -}} // cv::hal:: diff --git a/modules/hal/src/color.cpp b/modules/hal/src/color.cpp deleted file mode 100644 index a3f69facc..000000000 --- a/modules/hal/src/color.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" - -namespace cv { namespace hal { - -}} diff --git a/modules/hal/src/filter.cpp b/modules/hal/src/filter.cpp deleted file mode 100644 index a3f69facc..000000000 --- a/modules/hal/src/filter.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" - -namespace cv { namespace hal { - -}} diff --git a/modules/hal/src/hardware.cpp b/modules/hal/src/hardware.cpp deleted file mode 100644 index 6a08b9f44..000000000 --- a/modules/hal/src/hardware.cpp +++ /dev/null @@ -1,221 +0,0 @@ -#include "precomp.hpp" - -#if defined WIN32 || defined _WIN32 || defined WINCE -#include -#if defined _MSC_VER - #if _MSC_VER >= 1400 - #include - #elif defined _M_IX86 - static void __cpuid(int* cpuid_data, int) - { - __asm - { - push ebx - push edi - mov edi, cpuid_data - mov eax, 1 - cpuid - mov [edi], eax - mov [edi + 4], ebx - mov [edi + 8], ecx - mov [edi + 12], edx - pop edi - pop ebx - } - } - static void __cpuidex(int* cpuid_data, int, int) - { - __asm - { - push edi - mov edi, cpuid_data - mov eax, 7 - mov ecx, 0 - cpuid - mov [edi], eax - mov [edi + 4], ebx - mov [edi + 8], ecx - mov [edi + 12], edx - pop edi - } - } - #endif -#endif -#endif - -#if defined ANDROID || defined __linux__ -# include -# include -# include -# include -#endif - -#if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ -#include -#include -#include -#if defined ANDROID -#include -#endif -#endif - -#ifdef ANDROID -# include -#endif - -struct HWFeatures -{ - enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE }; - - HWFeatures(void) - { - memset( have, 0, sizeof(have) ); - x86_family = 0; - } - - static HWFeatures initialize(void) - { - HWFeatures f; - int cpuid_data[4] = { 0, 0, 0, 0 }; - - #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64) - __cpuid(cpuid_data, 1); - #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__) - #ifdef __x86_64__ - asm __volatile__ - ( - "movl $1, %%eax\n\t" - "cpuid\n\t" - :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3]) - : - : "cc" - ); - #else - asm volatile - ( - "pushl %%ebx\n\t" - "movl $1,%%eax\n\t" - "cpuid\n\t" - "popl %%ebx\n\t" - : "=a"(cpuid_data[0]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) - : - : "cc" - ); - #endif - #endif - - f.x86_family = (cpuid_data[0] >> 8) & 15; - if( f.x86_family >= 6 ) - { - f.have[CV_CPU_MMX] = (cpuid_data[3] & (1 << 23)) != 0; - f.have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0; - f.have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0; - f.have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0; - f.have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0; - f.have[CV_CPU_FMA3] = (cpuid_data[2] & (1<<12)) != 0; - f.have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0; - f.have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0; - f.have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0; - f.have[CV_CPU_AVX] = (((cpuid_data[2] & (1<<28)) != 0)&&((cpuid_data[2] & (1<<27)) != 0));//OS uses XSAVE_XRSTORE and CPU support AVX - - // make the second call to the cpuid command in order to get - // information about extended features like AVX2 - #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64) - __cpuidex(cpuid_data, 7, 0); - #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__) - #ifdef __x86_64__ - asm __volatile__ - ( - "movl $7, %%eax\n\t" - "movl $0, %%ecx\n\t" - "cpuid\n\t" - :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3]) - : - : "cc" - ); - #else - asm volatile - ( - "pushl %%ebx\n\t" - "movl $7,%%eax\n\t" - "movl $0,%%ecx\n\t" - "cpuid\n\t" - "movl %%ebx, %0\n\t" - "popl %%ebx\n\t" - : "=r"(cpuid_data[1]), "=c"(cpuid_data[2]) - : - : "cc" - ); - #endif - #endif - f.have[CV_CPU_AVX2] = (cpuid_data[1] & (1<<5)) != 0; - - f.have[CV_CPU_AVX_512F] = (cpuid_data[1] & (1<<16)) != 0; - f.have[CV_CPU_AVX_512DQ] = (cpuid_data[1] & (1<<17)) != 0; - f.have[CV_CPU_AVX_512IFMA512] = (cpuid_data[1] & (1<<21)) != 0; - f.have[CV_CPU_AVX_512PF] = (cpuid_data[1] & (1<<26)) != 0; - f.have[CV_CPU_AVX_512ER] = (cpuid_data[1] & (1<<27)) != 0; - f.have[CV_CPU_AVX_512CD] = (cpuid_data[1] & (1<<28)) != 0; - f.have[CV_CPU_AVX_512BW] = (cpuid_data[1] & (1<<30)) != 0; - f.have[CV_CPU_AVX_512VL] = (cpuid_data[1] & (1<<31)) != 0; - f.have[CV_CPU_AVX_512VBMI] = (cpuid_data[2] & (1<<1)) != 0; - } - - #if defined ANDROID || defined __linux__ - #ifdef __aarch64__ - f.have[CV_CPU_NEON] = true; - #else - int cpufile = open("/proc/self/auxv", O_RDONLY); - - if (cpufile >= 0) - { - Elf32_auxv_t auxv; - const size_t size_auxv_t = sizeof(auxv); - - while ((size_t)read(cpufile, &auxv, size_auxv_t) == size_auxv_t) - { - if (auxv.a_type == AT_HWCAP) - { - f.have[CV_CPU_NEON] = (auxv.a_un.a_val & 4096) != 0; - break; - } - } - - close(cpufile); - } - #endif - #elif (defined __clang__ || defined __APPLE__) && (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__)) - f.have[CV_CPU_NEON] = true; - #endif - - return f; - } - - int x86_family; - bool have[MAX_FEATURE+1]; -}; - -static HWFeatures featuresEnabled = HWFeatures::initialize(), featuresDisabled = HWFeatures(); -static HWFeatures* currentFeatures = &featuresEnabled; -volatile bool useOptimizedFlag = true; - -namespace cv { namespace hal { - -bool checkHardwareSupport(int feature) -{ -// CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE ); - return currentFeatures->have[feature]; -} - -void setUseOptimized( bool flag ) -{ - useOptimizedFlag = flag; - currentFeatures = flag ? &featuresEnabled : &featuresDisabled; -} - -bool useOptimized(void) -{ - return useOptimizedFlag; -} - -}} diff --git a/modules/hal/src/precomp.hpp b/modules/hal/src/precomp.hpp deleted file mode 100644 index 16586368e..000000000 --- a/modules/hal/src/precomp.hpp +++ /dev/null @@ -1,60 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "opencv2/hal.hpp" -#include "opencv2/hal/intrin.hpp" -#include -#include -#include -#include -#include -#include -#include - -#include "opencv2/hal/sse_utils.hpp" -#include "opencv2/hal/neon_utils.hpp" - -#if defined HAVE_IPP && (IPP_VERSION_X100 >= 700) -#define ARITHM_USE_IPP 1 -#else -#define ARITHM_USE_IPP 0 -#endif diff --git a/modules/hal/src/replacement.hpp b/modules/hal/src/replacement.hpp deleted file mode 100644 index c8cc19224..000000000 --- a/modules/hal/src/replacement.hpp +++ /dev/null @@ -1,208 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009, Willow Garage Inc., all rights reserved. -// Copyright (C) 2013, OpenCV Foundation, all rights reserved. -// Copyright (C) 2015, Itseez Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef __OPENCV_HAL_REPLACEMENT_HPP__ -#define __OPENCV_HAL_REPLACEMENT_HPP__ - -#include "opencv2/hal.hpp" - -inline int hal_t_add8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_add64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_sub64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_max64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_min64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff16s(const short*, size_t, const short*, size_t, short*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff32s(const int*, size_t, const int*, size_t, int*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff32f(const float*, size_t, const float*, size_t, float*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_absdiff64f(const double*, size_t, const double*, size_t, double*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_and8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_or8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_xor8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_not8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int) { return cv::hal::Error::NotImplemented; } - -#define hal_add8u hal_t_add8u -#define hal_add8s hal_t_add8s -#define hal_add16u hal_t_add16u -#define hal_add16s hal_t_add16s -#define hal_add32s hal_t_add32s -#define hal_add32f hal_t_add32f -#define hal_add64f hal_t_add64f -#define hal_sub8u hal_t_sub8u -#define hal_sub8s hal_t_sub8s -#define hal_sub16u hal_t_sub16u -#define hal_sub16s hal_t_sub16s -#define hal_sub32s hal_t_sub32s -#define hal_sub32f hal_t_sub32f -#define hal_sub64f hal_t_sub64f -#define hal_max8u hal_t_max8u -#define hal_max8s hal_t_max8s -#define hal_max16u hal_t_max16u -#define hal_max16s hal_t_max16s -#define hal_max32s hal_t_max32s -#define hal_max32f hal_t_max32f -#define hal_max64f hal_t_max64f -#define hal_min8u hal_t_min8u -#define hal_min8s hal_t_min8s -#define hal_min16u hal_t_min16u -#define hal_min16s hal_t_min16s -#define hal_min32s hal_t_min32s -#define hal_min32f hal_t_min32f -#define hal_min64f hal_t_min64f -#define hal_absdiff8u hal_t_absdiff8u -#define hal_absdiff8s hal_t_absdiff8s -#define hal_absdiff16u hal_t_absdiff16u -#define hal_absdiff16s hal_t_absdiff16s -#define hal_absdiff32s hal_t_absdiff32s -#define hal_absdiff32f hal_t_absdiff32f -#define hal_absdiff64f hal_t_absdiff64f -#define hal_and8u hal_t_and8u -#define hal_or8u hal_t_or8u -#define hal_xor8u hal_t_xor8u -#define hal_not8u hal_t_not8u - -inline int hal_t_cmp8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp8s(const schar*, size_t, const schar*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp16u(const ushort*, size_t, const ushort*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp16s(const short*, size_t, const short*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp32s(const int*, size_t, const int*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp32f(const float*, size_t, const float*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } -inline int hal_t_cmp64f(const double*, size_t, const double*, size_t, uchar*, size_t, int, int, int) { return cv::hal::Error::NotImplemented; } - -#define hal_cmp8u hal_t_cmp8u -#define hal_cmp8s hal_t_cmp8s -#define hal_cmp16u hal_t_cmp16u -#define hal_cmp16s hal_t_cmp16s -#define hal_cmp32s hal_t_cmp32s -#define hal_cmp32f hal_t_cmp32f -#define hal_cmp64f hal_t_cmp64f - -inline int hal_t_mul8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_mul64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_div64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } -inline int hal_t_recip64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, double) { return cv::hal::Error::NotImplemented; } - -#define hal_mul8u hal_t_mul8u -#define hal_mul8s hal_t_mul8s -#define hal_mul16u hal_t_mul16u -#define hal_mul16s hal_t_mul16s -#define hal_mul32s hal_t_mul32s -#define hal_mul32f hal_t_mul32f -#define hal_mul64f hal_t_mul64f -#define hal_div8u hal_t_div8u -#define hal_div8s hal_t_div8s -#define hal_div16u hal_t_div16u -#define hal_div16s hal_t_div16s -#define hal_div32s hal_t_div32s -#define hal_div32f hal_t_div32f -#define hal_div64f hal_t_div64f -#define hal_recip8u hal_t_recip8u -#define hal_recip8s hal_t_recip8s -#define hal_recip16u hal_t_recip16u -#define hal_recip16s hal_t_recip16s -#define hal_recip32s hal_t_recip32s -#define hal_recip32f hal_t_recip32f -#define hal_recip64f hal_t_recip64f - -inline int hal_t_addWeighted8u(const uchar*, size_t, const uchar*, size_t, uchar*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted8s(const schar*, size_t, const schar*, size_t, schar*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted16u(const ushort*, size_t, const ushort*, size_t, ushort*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted16s(const short*, size_t, const short*, size_t, short*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted32s(const int*, size_t, const int*, size_t, int*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted32f(const float*, size_t, const float*, size_t, float*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } -inline int hal_t_addWeighted64f(const double*, size_t, const double*, size_t, double*, size_t, int, int, void*) { return cv::hal::Error::NotImplemented; } - -#define hal_addWeighted8u hal_t_addWeighted8u -#define hal_addWeighted8s hal_t_addWeighted8s -#define hal_addWeighted16u hal_t_addWeighted16u -#define hal_addWeighted16s hal_t_addWeighted16s -#define hal_addWeighted32s hal_t_addWeighted32s -#define hal_addWeighted32f hal_t_addWeighted32f -#define hal_addWeighted64f hal_t_addWeighted64f - -#include "custom_hal.hpp" - -#endif diff --git a/modules/hal/src/resize.cpp b/modules/hal/src/resize.cpp deleted file mode 100644 index a3f69facc..000000000 --- a/modules/hal/src/resize.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" - -namespace cv { namespace hal { - -}} diff --git a/modules/hal/src/stat.cpp b/modules/hal/src/stat.cpp deleted file mode 100644 index ec3b8db5a..000000000 --- a/modules/hal/src/stat.cpp +++ /dev/null @@ -1,306 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" - -namespace cv { namespace hal { - -static const uchar popCountTable[] = -{ - 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 -}; - -static const uchar popCountTable2[] = -{ - 0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, - 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, - 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, - 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, - 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, - 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, - 1, 2, 2, 2, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, - 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4, 2, 3, 3, 3, 3, 4, 4, 4, 3, 4, 4, 4, 3, 4, 4, 4 -}; - -static const uchar popCountTable4[] = -{ - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 -}; - -int normHamming(const uchar* a, int n) -{ - int i = 0; - int result = 0; -#if CV_NEON - { - uint32x4_t bits = vmovq_n_u32(0); - for (; i <= n - 16; i += 16) { - uint8x16_t A_vec = vld1q_u8 (a + i); - uint8x16_t bitsSet = vcntq_u8 (A_vec); - uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet); - uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8); - bits = vaddq_u32(bits, bitSet4); - } - uint64x2_t bitSet2 = vpaddlq_u32 (bits); - result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0); - result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2); - } -#endif - for( ; i <= n - 4; i += 4 ) - result += popCountTable[a[i]] + popCountTable[a[i+1]] + - popCountTable[a[i+2]] + popCountTable[a[i+3]]; - for( ; i < n; i++ ) - result += popCountTable[a[i]]; - return result; -} - -int normHamming(const uchar* a, const uchar* b, int n) -{ - int i = 0; - int result = 0; -#if CV_NEON - { - uint32x4_t bits = vmovq_n_u32(0); - for (; i <= n - 16; i += 16) { - uint8x16_t A_vec = vld1q_u8 (a + i); - uint8x16_t B_vec = vld1q_u8 (b + i); - uint8x16_t AxorB = veorq_u8 (A_vec, B_vec); - uint8x16_t bitsSet = vcntq_u8 (AxorB); - uint16x8_t bitSet8 = vpaddlq_u8 (bitsSet); - uint32x4_t bitSet4 = vpaddlq_u16 (bitSet8); - bits = vaddq_u32(bits, bitSet4); - } - uint64x2_t bitSet2 = vpaddlq_u32 (bits); - result = vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),0); - result += vgetq_lane_s32 (vreinterpretq_s32_u64(bitSet2),2); - } -#endif - for( ; i <= n - 4; i += 4 ) - result += popCountTable[a[i] ^ b[i]] + popCountTable[a[i+1] ^ b[i+1]] + - popCountTable[a[i+2] ^ b[i+2]] + popCountTable[a[i+3] ^ b[i+3]]; - for( ; i < n; i++ ) - result += popCountTable[a[i] ^ b[i]]; - return result; -} - -int normHamming(const uchar* a, int n, int cellSize) -{ - if( cellSize == 1 ) - return normHamming(a, n); - const uchar* tab = 0; - if( cellSize == 2 ) - tab = popCountTable2; - else if( cellSize == 4 ) - tab = popCountTable4; - else - return -1; - int i = 0; - int result = 0; -#if CV_ENABLE_UNROLLED - for( ; i <= n - 4; i += 4 ) - result += tab[a[i]] + tab[a[i+1]] + tab[a[i+2]] + tab[a[i+3]]; -#endif - for( ; i < n; i++ ) - result += tab[a[i]]; - return result; -} - -int normHamming(const uchar* a, const uchar* b, int n, int cellSize) -{ - if( cellSize == 1 ) - return normHamming(a, b, n); - const uchar* tab = 0; - if( cellSize == 2 ) - tab = popCountTable2; - else if( cellSize == 4 ) - tab = popCountTable4; - else - return -1; - int i = 0; - int result = 0; - #if CV_ENABLE_UNROLLED - for( ; i <= n - 4; i += 4 ) - result += tab[a[i] ^ b[i]] + tab[a[i+1] ^ b[i+1]] + - tab[a[i+2] ^ b[i+2]] + tab[a[i+3] ^ b[i+3]]; - #endif - for( ; i < n; i++ ) - result += tab[a[i] ^ b[i]]; - return result; -} - -float normL2Sqr_(const float* a, const float* b, int n) -{ - int j = 0; float d = 0.f; -#if CV_SSE - float CV_DECL_ALIGNED(16) buf[4]; - __m128 d0 = _mm_setzero_ps(), d1 = _mm_setzero_ps(); - - for( ; j <= n - 8; j += 8 ) - { - __m128 t0 = _mm_sub_ps(_mm_loadu_ps(a + j), _mm_loadu_ps(b + j)); - __m128 t1 = _mm_sub_ps(_mm_loadu_ps(a + j + 4), _mm_loadu_ps(b + j + 4)); - d0 = _mm_add_ps(d0, _mm_mul_ps(t0, t0)); - d1 = _mm_add_ps(d1, _mm_mul_ps(t1, t1)); - } - _mm_store_ps(buf, _mm_add_ps(d0, d1)); - d = buf[0] + buf[1] + buf[2] + buf[3]; -#endif - { - for( ; j <= n - 4; j += 4 ) - { - float t0 = a[j] - b[j], t1 = a[j+1] - b[j+1], t2 = a[j+2] - b[j+2], t3 = a[j+3] - b[j+3]; - d += t0*t0 + t1*t1 + t2*t2 + t3*t3; - } - } - - for( ; j < n; j++ ) - { - float t = a[j] - b[j]; - d += t*t; - } - return d; -} - - -float normL1_(const float* a, const float* b, int n) -{ - int j = 0; float d = 0.f; -#if CV_SSE - float CV_DECL_ALIGNED(16) buf[4]; - static const int CV_DECL_ALIGNED(16) absbuf[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff}; - __m128 d0 = _mm_setzero_ps(), d1 = _mm_setzero_ps(); - __m128 absmask = _mm_load_ps((const float*)absbuf); - - for( ; j <= n - 8; j += 8 ) - { - __m128 t0 = _mm_sub_ps(_mm_loadu_ps(a + j), _mm_loadu_ps(b + j)); - __m128 t1 = _mm_sub_ps(_mm_loadu_ps(a + j + 4), _mm_loadu_ps(b + j + 4)); - d0 = _mm_add_ps(d0, _mm_and_ps(t0, absmask)); - d1 = _mm_add_ps(d1, _mm_and_ps(t1, absmask)); - } - _mm_store_ps(buf, _mm_add_ps(d0, d1)); - d = buf[0] + buf[1] + buf[2] + buf[3]; -#elif CV_NEON - float32x4_t v_sum = vdupq_n_f32(0.0f); - for ( ; j <= n - 4; j += 4) - v_sum = vaddq_f32(v_sum, vabdq_f32(vld1q_f32(a + j), vld1q_f32(b + j))); - - float CV_DECL_ALIGNED(16) buf[4]; - vst1q_f32(buf, v_sum); - d = buf[0] + buf[1] + buf[2] + buf[3]; -#endif - { - for( ; j <= n - 4; j += 4 ) - { - d += std::abs(a[j] - b[j]) + std::abs(a[j+1] - b[j+1]) + - std::abs(a[j+2] - b[j+2]) + std::abs(a[j+3] - b[j+3]); - } - } - - for( ; j < n; j++ ) - d += std::abs(a[j] - b[j]); - return d; -} - -int normL1_(const uchar* a, const uchar* b, int n) -{ - int j = 0, d = 0; -#if CV_SSE - __m128i d0 = _mm_setzero_si128(); - - for( ; j <= n - 16; j += 16 ) - { - __m128i t0 = _mm_loadu_si128((const __m128i*)(a + j)); - __m128i t1 = _mm_loadu_si128((const __m128i*)(b + j)); - - d0 = _mm_add_epi32(d0, _mm_sad_epu8(t0, t1)); - } - - for( ; j <= n - 4; j += 4 ) - { - __m128i t0 = _mm_cvtsi32_si128(*(const int*)(a + j)); - __m128i t1 = _mm_cvtsi32_si128(*(const int*)(b + j)); - - d0 = _mm_add_epi32(d0, _mm_sad_epu8(t0, t1)); - } - d = _mm_cvtsi128_si32(_mm_add_epi32(d0, _mm_unpackhi_epi64(d0, d0))); -#elif CV_NEON - uint32x4_t v_sum = vdupq_n_u32(0.0f); - for ( ; j <= n - 16; j += 16) - { - uint8x16_t v_dst = vabdq_u8(vld1q_u8(a + j), vld1q_u8(b + j)); - uint16x8_t v_low = vmovl_u8(vget_low_u8(v_dst)), v_high = vmovl_u8(vget_high_u8(v_dst)); - v_sum = vaddq_u32(v_sum, vaddl_u16(vget_low_u16(v_low), vget_low_u16(v_high))); - v_sum = vaddq_u32(v_sum, vaddl_u16(vget_high_u16(v_low), vget_high_u16(v_high))); - } - - uint CV_DECL_ALIGNED(16) buf[4]; - vst1q_u32(buf, v_sum); - d = buf[0] + buf[1] + buf[2] + buf[3]; -#endif - { - for( ; j <= n - 4; j += 4 ) - { - d += std::abs(a[j] - b[j]) + std::abs(a[j+1] - b[j+1]) + - std::abs(a[j+2] - b[j+2]) + std::abs(a[j+3] - b[j+3]); - } - } - for( ; j < n; j++ ) - d += std::abs(a[j] - b[j]); - return d; -} - -}} //cv::hal diff --git a/modules/hal/src/warp.cpp b/modules/hal/src/warp.cpp deleted file mode 100644 index a3f69facc..000000000 --- a/modules/hal/src/warp.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#include "precomp.hpp" - -namespace cv { namespace hal { - -}} diff --git a/modules/hal/test/test_main.cpp b/modules/hal/test/test_main.cpp deleted file mode 100644 index d337a5ba7..000000000 --- a/modules/hal/test/test_main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "opencv2/ts.hpp" - -CV_TEST_MAIN("cv") diff --git a/modules/hal/test/test_precomp.hpp b/modules/hal/test/test_precomp.hpp deleted file mode 100644 index 387b7ba2b..000000000 --- a/modules/hal/test/test_precomp.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __OPENCV_HAL_TEST_PRECOMP_HPP__ -#define __OPENCV_HAL_TEST_PRECOMP_HPP__ - -#include -#include -#include "opencv2/ts.hpp" -#include "opencv2/hal.hpp" -#include "opencv2/hal/defs.h" -#include "opencv2/hal/intrin.hpp" - -#endif diff --git a/modules/highgui/CMakeLists.txt b/modules/highgui/CMakeLists.txt index 9d1745e7b..eee6a934a 100644 --- a/modules/highgui/CMakeLists.txt +++ b/modules/highgui/CMakeLists.txt @@ -7,7 +7,7 @@ ocv_add_module(highgui opencv_imgproc opencv_imgcodecs opencv_videoio WRAP pytho # Jose Luis Blanco, 2008 # ---------------------------------------------------------------------------- -if(WINRT_8_1) +if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") endif() diff --git a/modules/highgui/src/window_winrt_bridge.cpp b/modules/highgui/src/window_winrt_bridge.cpp index e0818eafb..b107b1fcf 100644 --- a/modules/highgui/src/window_winrt_bridge.cpp +++ b/modules/highgui/src/window_winrt_bridge.cpp @@ -183,7 +183,7 @@ void CvTrackbar::setMinPosition(double pos) if (pos < 0) pos = 0; //Min is always less than Max. - if ((pos > slider->Maximum) + if (pos > slider->Maximum) pos = slider->Maximum; slider->Minimum = pos; } diff --git a/modules/imgcodecs/CMakeLists.txt b/modules/imgcodecs/CMakeLists.txt index 7b3fc0bd7..8b8c57716 100644 --- a/modules/imgcodecs/CMakeLists.txt +++ b/modules/imgcodecs/CMakeLists.txt @@ -83,7 +83,6 @@ file(GLOB imgcodecs_ext_hdrs ) if(IOS) - add_definitions(-DHAVE_IOS=1) list(APPEND imgcodecs_srcs ${CMAKE_CURRENT_LIST_DIR}/src/ios_conversions.mm) list(APPEND IMGCODECS_LIBRARIES "-framework Accelerate" "-framework CoreGraphics" "-framework CoreImage" "-framework QuartzCore" "-framework AssetsLibrary") endif() diff --git a/modules/imgcodecs/src/grfmt_exr.cpp b/modules/imgcodecs/src/grfmt_exr.cpp index 5957549f3..71d89126a 100644 --- a/modules/imgcodecs/src/grfmt_exr.cpp +++ b/modules/imgcodecs/src/grfmt_exr.cpp @@ -381,6 +381,11 @@ bool ExrDecoder::readData( Mat& img ) close(); + if( !m_native_depth || (!color && m_iscolor )) + { + delete[] buffer; + } + return result; } diff --git a/modules/imgcodecs/src/grfmt_jpeg.cpp b/modules/imgcodecs/src/grfmt_jpeg.cpp index 6862a06eb..093b88c40 100644 --- a/modules/imgcodecs/src/grfmt_jpeg.cpp +++ b/modules/imgcodecs/src/grfmt_jpeg.cpp @@ -267,10 +267,17 @@ int JpegDecoder::getOrientation() { int orientation = JPEG_ORIENTATION_TL; - ExifReader reader( m_filename ); - if( reader.parse() ) + if (m_filename.size() > 0) { - orientation = reader.getTag( ORIENTATION ).field_u16;//orientation is unsigned short, so check field_u16 + ExifReader reader( m_filename ); + if( reader.parse() ) + { + ExifEntry_t entry = reader.getTag( ORIENTATION ); + if (entry.tag != INVALID_TAG) + { + orientation = entry.field_u16; //orientation is unsigned short, so check field_u16 + } + } } return orientation; diff --git a/modules/imgcodecs/src/jpeg_exif.cpp b/modules/imgcodecs/src/jpeg_exif.cpp index c7b5b76a2..adb87e5b7 100644 --- a/modules/imgcodecs/src/jpeg_exif.cpp +++ b/modules/imgcodecs/src/jpeg_exif.cpp @@ -52,10 +52,16 @@ namespace { namespace cv { +ExifEntry_t::ExifEntry_t() : + field_float(0), field_double(0), field_u32(0), field_s32(0), + tag(INVALID_TAG), field_u16(0), field_s16(0), field_u8(0), field_s8(0) +{ +} + /** * @brief ExifReader constructor */ -ExifReader::ExifReader(std::string filename) : m_filename(filename) +ExifReader::ExifReader(std::string filename) : m_filename(filename), m_format(NONE) { } @@ -122,6 +128,11 @@ std::map ExifReader::getExif() size_t count; + if (m_filename.size() == 0) + { + return m_exif; + } + FILE* f = fopen( m_filename.c_str(), "rb" ); if( !f ) @@ -159,6 +170,7 @@ std::map ExifReader::getExif() case APP1: //actual Exif Marker exifSize = getFieldSize(f); if (exifSize <= offsetToTiffHeader) { + fclose(f); throw ExifParsingError(); } m_data.resize( exifSize - offsetToTiffHeader ); @@ -239,7 +251,10 @@ void ExifReader::parseExif() */ Endianess_t ExifReader::getFormat() const { - if( m_data[0] != m_data[1] ) + if (m_data.size() < 1) + return NONE; + + if( m_data.size() > 1 && m_data[0] != m_data[1] ) { return NONE; } diff --git a/modules/imgcodecs/src/jpeg_exif.hpp b/modules/imgcodecs/src/jpeg_exif.hpp index 9ca138167..c8502c5c8 100644 --- a/modules/imgcodecs/src/jpeg_exif.hpp +++ b/modules/imgcodecs/src/jpeg_exif.hpp @@ -111,6 +111,8 @@ typedef std::pair u_rational_t; */ struct ExifEntry_t { + ExifEntry_t(); + std::vector field_u_rational; ///< vector of rational fields std::string field_str; ///< any kind of textual information diff --git a/modules/imgproc/doc/colors.markdown b/modules/imgproc/doc/colors.markdown index c372d280d..52152c9e8 100644 --- a/modules/imgproc/doc/colors.markdown +++ b/modules/imgproc/doc/colors.markdown @@ -123,7 +123,7 @@ In case of 8-bit and 16-bit images, R, G, and B are converted to the floating-po scaled to fit 0 to 1 range. \f[\vecthree{X}{Y}{Z} \leftarrow \vecthreethree{0.412453}{0.357580}{0.180423}{0.212671}{0.715160}{0.072169}{0.019334}{0.119193}{0.950227} \cdot \vecthree{R}{G}{B}\f] -\f[L \leftarrow \fork{116 Y^{1/3}}{for \(Y>0.008856\)}{903.3 Y}{for \(Y\leq 0.008856\)}\f] +\f[L \leftarrow \fork{116*Y^{1/3} - 16}{for \(Y>0.008856\)}{903.3 Y}{for \(Y\leq 0.008856\)}\f] \f[u' \leftarrow 4*X/(X + 15*Y + 3 Z)\f] \f[v' \leftarrow 9*Y/(X + 15*Y + 3 Z)\f] \f[u \leftarrow 13*L*(u' - u_n) \quad \text{where} \quad u_n=0.19793943\f] diff --git a/modules/imgproc/include/opencv2/imgproc.hpp b/modules/imgproc/include/opencv2/imgproc.hpp index 86d6e4669..08c6036a7 100644 --- a/modules/imgproc/include/opencv2/imgproc.hpp +++ b/modules/imgproc/include/opencv2/imgproc.hpp @@ -2598,8 +2598,8 @@ the same. @param dst Output (corrected) image that has the same size and type as src . @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])\f$ of 4, 5, or 8 elements. If the vector is -NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6[, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ +of 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed. @param newCameraMatrix Camera matrix of the distorted image. By default, it is the same as cameraMatrix but you may additionally scale and shift the result by using a different matrix. */ @@ -2625,8 +2625,28 @@ The function actually builds the maps for the inverse mapping algorithm that is is, for each pixel \f$(u, v)\f$ in the destination (corrected and rectified) image, the function computes the corresponding coordinates in the source image (that is, in the original image from camera). The following process is applied: -\f[\begin{array}{l} x \leftarrow (u - {c'}_x)/{f'}_x \\ y \leftarrow (v - {c'}_y)/{f'}_y \\{[X\,Y\,W]} ^T \leftarrow R^{-1}*[x \, y \, 1]^T \\ x' \leftarrow X/W \\ y' \leftarrow Y/W \\ x" \leftarrow x' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + 2p_1 x' y' + p_2(r^2 + 2 x'^2) \\ y" \leftarrow y' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' \\ map_x(u,v) \leftarrow x" f_x + c_x \\ map_y(u,v) \leftarrow y" f_y + c_y \end{array}\f] -where \f$(k_1, k_2, p_1, p_2[, k_3])\f$ are the distortion coefficients. +\f[ +\begin{array}{l} +x \leftarrow (u - {c'}_x)/{f'}_x \\ +y \leftarrow (v - {c'}_y)/{f'}_y \\ +{[X\,Y\,W]} ^T \leftarrow R^{-1}*[x \, y \, 1]^T \\ +x' \leftarrow X/W \\ +y' \leftarrow Y/W \\ +r^2 \leftarrow x'^2 + y'^2 \\ +x'' \leftarrow x' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} ++ 2p_1 x' y' + p_2(r^2 + 2 x'^2) + s_1 r^2 + s_2 r^4\\ +y'' \leftarrow y' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} ++ p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' + s_3 r^2 + s_4 r^4 \\ +s\vecthree{x'''}{y'''}{1} = +\vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}((\tau_x, \tau_y)} +{0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)} +{0}{0}{1} R(\tau_x, \tau_y) \vecthree{x''}{y''}{1}\\ +map_x(u,v) \leftarrow x''' f_x + c_x \\ +map_y(u,v) \leftarrow y''' f_y + c_y +\end{array} +\f] +where \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6[, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ +are the distortion coefficients. In case of a stereo camera, this function is called twice: once for each camera head, after stereoRectify, which in its turn is called after cv::stereoCalibrate. But if the stereo camera @@ -2639,8 +2659,8 @@ where cameraMatrix can be chosen arbitrarily. @param cameraMatrix Input camera matrix \f$A=\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])\f$ of 4, 5, or 8 elements. If the vector is -NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6[, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ +of 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed. @param R Optional rectification transformation in the object space (3x3 matrix). R1 or R2 , computed by stereoRectify can be passed here. If the matrix is empty, the identity transformation is assumed. In cvInitUndistortMap R assumed to be an identity matrix. @@ -2715,8 +2735,8 @@ The function can be used for both a stereo camera head or a monocular camera (wh transformation. If matrix P is identity or omitted, dst will contain normalized point coordinates. @param cameraMatrix Camera matrix \f$\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . @param distCoeffs Input vector of distortion coefficients -\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])\f$ of 4, 5, or 8 elements. If the vector is -NULL/empty, the zero distortion coefficients are assumed. +\f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6[, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ +of 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed. @param R Rectification transformation in the object space (3x3 matrix). R1 or R2 computed by cv::stereoRectify can be passed here. If the matrix is empty, the identity transformation is used. @param P New camera matrix (3x3) or new projection matrix (3x4). P1 or P2 computed by @@ -3927,8 +3947,8 @@ marker types are supported, see cv::MarkerTypes for more information. @param img Image. @param position The point where the crosshair is positioned. -@param markerType The specific type of marker you want to use, see cv::MarkerTypes @param color Line color. +@param markerType The specific type of marker you want to use, see cv::MarkerTypes @param thickness Line thickness. @param line_type Type of the line, see cv::LineTypes @param markerSize The length of the marker axis [default = 20 pixels] diff --git a/modules/imgproc/include/opencv2/imgproc/detail/distortion_model.hpp b/modules/imgproc/include/opencv2/imgproc/detail/distortion_model.hpp new file mode 100644 index 000000000..ca293040e --- /dev/null +++ b/modules/imgproc/include/opencv2/imgproc/detail/distortion_model.hpp @@ -0,0 +1,123 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifndef __OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP__ +#define __OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP__ + +//! @cond IGNORED + +namespace cv { namespace detail { +/** +Computes the matrix for the projection onto a tilted image sensor +\param tauX angular parameter rotation around x-axis +\param tauY angular parameter rotation around y-axis +\param matTilt if not NULL returns the matrix +\f[ +\vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}((\tau_x, \tau_y)} +{0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)} +{0}{0}{1} R(\tau_x, \tau_y) +\f] +where +\f[ +R(\tau_x, \tau_y) = +\vecthreethree{\cos(\tau_y)}{0}{-\sin(\tau_y)}{0}{1}{0}{\sin(\tau_y)}{0}{\cos(\tau_y)} +\vecthreethree{1}{0}{0}{0}{\cos(\tau_x)}{\sin(\tau_x)}{0}{-\sin(\tau_x)}{\cos(\tau_x)} = +\vecthreethree{\cos(\tau_y)}{\sin(\tau_y)\sin(\tau_x)}{-\sin(\tau_y)\cos(\tau_x)} +{0}{\cos(\tau_x)}{\sin(\tau_x)} +{\sin(\tau_y)}{-\cos(\tau_y)\sin(\tau_x)}{\cos(\tau_y)\cos(\tau_x)}. +\f] +\param dMatTiltdTauX if not NULL it returns the derivative of matTilt with +respect to \f$\tau_x\f$. +\param dMatTiltdTauY if not NULL it returns the derivative of matTilt with +respect to \f$\tau_y\f$. +\param invMatTilt if not NULL it returns the inverse of matTilt +**/ +template +void computeTiltProjectionMatrix(FLOAT tauX, + FLOAT tauY, + Matx* matTilt = 0, + Matx* dMatTiltdTauX = 0, + Matx* dMatTiltdTauY = 0, + Matx* invMatTilt = 0) +{ + FLOAT cTauX = cos(tauX); + FLOAT sTauX = sin(tauX); + FLOAT cTauY = cos(tauY); + FLOAT sTauY = sin(tauY); + Matx matRotX = Matx(1,0,0,0,cTauX,sTauX,0,-sTauX,cTauX); + Matx matRotY = Matx(cTauY,0,-sTauY,0,1,0,sTauY,0,cTauY); + Matx matRotXY = matRotY * matRotX; + Matx matProjZ = Matx(matRotXY(2,2),0,-matRotXY(0,2),0,matRotXY(2,2),-matRotXY(1,2),0,0,1); + if (matTilt) + { + // Matrix for trapezoidal distortion of tilted image sensor + *matTilt = matProjZ * matRotXY; + } + if (dMatTiltdTauX) + { + // Derivative with respect to tauX + Matx dMatRotXYdTauX = matRotY * Matx(0,0,0,0,-sTauX,cTauX,0,-cTauX,-sTauX); + Matx dMatProjZdTauX = Matx(dMatRotXYdTauX(2,2),0,-dMatRotXYdTauX(0,2), + 0,dMatRotXYdTauX(2,2),-dMatRotXYdTauX(1,2),0,0,0); + *dMatTiltdTauX = (matProjZ * dMatRotXYdTauX) + (dMatProjZdTauX * matRotXY); + } + if (dMatTiltdTauY) + { + // Derivative with respect to tauY + Matx dMatRotXYdTauY = Matx(-sTauY,0,-cTauY,0,0,0,cTauY,0,-sTauY) * matRotX; + Matx dMatProjZdTauY = Matx(dMatRotXYdTauY(2,2),0,-dMatRotXYdTauY(0,2), + 0,dMatRotXYdTauY(2,2),-dMatRotXYdTauY(1,2),0,0,0); + *dMatTiltdTauY = (matProjZ * dMatRotXYdTauY) + (dMatProjZdTauY * matRotXY); + } + if (invMatTilt) + { + FLOAT inv = 1./matRotXY(2,2); + Matx invMatProjZ = Matx(inv,0,inv*matRotXY(0,2),0,inv,inv*matRotXY(1,2),0,0,1); + *invMatTilt = matRotXY.t()*invMatProjZ; + } +} +}} // namespace detail, cv + + +//! @endcond + +#endif // __OPENCV_IMGPROC_DETAIL_DISTORTION_MODEL_HPP__ diff --git a/modules/imgproc/misc/java/src/java/imgproc+Moments.java b/modules/imgproc/misc/java/src/java/imgproc+Moments.java new file mode 100644 index 000000000..431b0259d --- /dev/null +++ b/modules/imgproc/misc/java/src/java/imgproc+Moments.java @@ -0,0 +1,244 @@ +package org.opencv.imgproc; + +import java.lang.Math; + +//javadoc:Moments +public class Moments { + + public double m00; + public double m10; + public double m01; + public double m20; + public double m11; + public double m02; + public double m30; + public double m21; + public double m12; + public double m03; + + public double mu20; + public double mu11; + public double mu02; + public double mu30; + public double mu21; + public double mu12; + public double mu03; + + public double nu20; + public double nu11; + public double nu02; + public double nu30; + public double nu21; + public double nu12; + public double nu03; + + public Moments( + double m00, + double m10, + double m01, + double m20, + double m11, + double m02, + double m30, + double m21, + double m12, + double m03) + { + this.m00 = m00; + this.m10 = m10; + this.m01 = m01; + this.m20 = m20; + this.m11 = m11; + this.m02 = m02; + this.m30 = m30; + this.m21 = m21; + this.m12 = m12; + this.m03 = m03; + this.completeState(); + } + + public Moments() { + this(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + } + + public Moments(double[] vals) { + set(vals); + } + + public void set(double[] vals) { + if (vals != null) { + m00 = vals.length > 0 ? (int) vals[0] : 0; + m10 = vals.length > 1 ? (int) vals[1] : 0; + m01 = vals.length > 2 ? (int) vals[2] : 0; + m20 = vals.length > 3 ? (int) vals[3] : 0; + m11 = vals.length > 4 ? (int) vals[4] : 0; + m02 = vals.length > 5 ? (int) vals[5] : 0; + m30 = vals.length > 6 ? (int) vals[6] : 0; + m21 = vals.length > 7 ? (int) vals[7] : 0; + m12 = vals.length > 8 ? (int) vals[8] : 0; + m03 = vals.length > 9 ? (int) vals[9] : 0; + this.completeState(); + } else { + m00 = 0; + m10 = 0; + m01 = 0; + m20 = 0; + m11 = 0; + m02 = 0; + m30 = 0; + m21 = 0; + m12 = 0; + m03 = 0; + mu20 = 0; + mu11 = 0; + mu02 = 0; + mu30 = 0; + mu21 = 0; + mu12 = 0; + mu03 = 0; + nu20 = 0; + nu11 = 0; + nu02 = 0; + nu30 = 0; + nu21 = 0; + nu12 = 0; + nu03 = 0; + } + } + + @Override + public String toString() { + return "Moments [ " + + "\n" + + "m00=" + m00 + ", " + + "\n" + + "m10=" + m10 + ", " + + "m01=" + m01 + ", " + + "\n" + + "m20=" + m20 + ", " + + "m11=" + m11 + ", " + + "m02=" + m02 + ", " + + "\n" + + "m30=" + m30 + ", " + + "m21=" + m21 + ", " + + "m12=" + m12 + ", " + + "m03=" + m03 + ", " + + "\n" + + "mu20=" + mu20 + ", " + + "mu11=" + mu11 + ", " + + "mu02=" + mu02 + ", " + + "\n" + + "mu30=" + mu30 + ", " + + "mu21=" + mu21 + ", " + + "mu12=" + mu12 + ", " + + "mu03=" + mu03 + ", " + + "\n" + + "nu20=" + nu20 + ", " + + "nu11=" + nu11 + ", " + + "nu02=" + nu02 + ", " + + "\n" + + "nu30=" + nu30 + ", " + + "nu21=" + nu21 + ", " + + "nu12=" + nu12 + ", " + + "nu03=" + nu03 + ", " + + "\n]"; + } + + protected void completeState() + { + double cx = 0, cy = 0; + double mu20, mu11, mu02; + double inv_m00 = 0.0; + + if( Math.abs(this.m00) > 0.00000001 ) + { + inv_m00 = 1. / this.m00; + cx = this.m10 * inv_m00; + cy = this.m01 * inv_m00; + } + + // mu20 = m20 - m10*cx + mu20 = this.m20 - this.m10 * cx; + // mu11 = m11 - m10*cy + mu11 = this.m11 - this.m10 * cy; + // mu02 = m02 - m01*cy + mu02 = this.m02 - this.m01 * cy; + + this.mu20 = mu20; + this.mu11 = mu11; + this.mu02 = mu02; + + // mu30 = m30 - cx*(3*mu20 + cx*m10) + this.mu30 = this.m30 - cx * (3 * mu20 + cx * this.m10); + mu11 += mu11; + // mu21 = m21 - cx*(2*mu11 + cx*m01) - cy*mu20 + this.mu21 = this.m21 - cx * (mu11 + cx * this.m01) - cy * mu20; + // mu12 = m12 - cy*(2*mu11 + cy*m10) - cx*mu02 + this.mu12 = this.m12 - cy * (mu11 + cy * this.m10) - cx * mu02; + // mu03 = m03 - cy*(3*mu02 + cy*m01) + this.mu03 = this.m03 - cy * (3 * mu02 + cy * this.m01); + + + double inv_sqrt_m00 = Math.sqrt(Math.abs(inv_m00)); + double s2 = inv_m00*inv_m00, s3 = s2*inv_sqrt_m00; + + this.nu20 = this.mu20*s2; + this.nu11 = this.mu11*s2; + this.nu02 = this.mu02*s2; + this.nu30 = this.mu30*s3; + this.nu21 = this.mu21*s3; + this.nu12 = this.mu12*s3; + this.nu03 = this.mu03*s3; + + } + + public double get_m00() { return this.m00; } + public double get_m10() { return this.m10; } + public double get_m01() { return this.m01; } + public double get_m20() { return this.m20; } + public double get_m11() { return this.m11; } + public double get_m02() { return this.m02; } + public double get_m30() { return this.m30; } + public double get_m21() { return this.m21; } + public double get_m12() { return this.m12; } + public double get_m03() { return this.m03; } + public double get_mu20() { return this.mu20; } + public double get_mu11() { return this.mu11; } + public double get_mu02() { return this.mu02; } + public double get_mu30() { return this.mu30; } + public double get_mu21() { return this.mu21; } + public double get_mu12() { return this.mu12; } + public double get_mu03() { return this.mu03; } + public double get_nu20() { return this.nu20; } + public double get_nu11() { return this.nu11; } + public double get_nu02() { return this.nu02; } + public double get_nu30() { return this.nu30; } + public double get_nu21() { return this.nu21; } + public double get_nu12() { return this.nu12; } + public double get_nu03() { return this.nu03; } + + public void set_m00(double m00) { this.m00 = m00; } + public void set_m10(double m10) { this.m10 = m10; } + public void set_m01(double m01) { this.m01 = m01; } + public void set_m20(double m20) { this.m20 = m20; } + public void set_m11(double m11) { this.m11 = m11; } + public void set_m02(double m02) { this.m02 = m02; } + public void set_m30(double m30) { this.m30 = m30; } + public void set_m21(double m21) { this.m21 = m21; } + public void set_m12(double m12) { this.m12 = m12; } + public void set_m03(double m03) { this.m03 = m03; } + public void set_mu20(double mu20) { this.mu20 = mu20; } + public void set_mu11(double mu11) { this.mu11 = mu11; } + public void set_mu02(double mu02) { this.mu02 = mu02; } + public void set_mu30(double mu30) { this.mu30 = mu30; } + public void set_mu21(double mu21) { this.mu21 = mu21; } + public void set_mu12(double mu12) { this.mu12 = mu12; } + public void set_mu03(double mu03) { this.mu03 = mu03; } + public void set_nu20(double nu20) { this.nu20 = nu20; } + public void set_nu11(double nu11) { this.nu11 = nu11; } + public void set_nu02(double nu02) { this.nu02 = nu02; } + public void set_nu30(double nu30) { this.nu30 = nu30; } + public void set_nu21(double nu21) { this.nu21 = nu21; } + public void set_nu12(double nu12) { this.nu12 = nu12; } + public void set_nu03(double nu03) { this.nu03 = nu03; } +} diff --git a/modules/imgproc/misc/java/test/MomentsTest.java b/modules/imgproc/misc/java/test/MomentsTest.java index f62071fc4..9bec229b2 100644 --- a/modules/imgproc/misc/java/test/MomentsTest.java +++ b/modules/imgproc/misc/java/test/MomentsTest.java @@ -1,199 +1,51 @@ package org.opencv.test.imgproc; import org.opencv.test.OpenCVTestCase; +import org.opencv.core.Core; +import org.opencv.core.Mat; +import org.opencv.core.CvType; +import org.opencv.core.Scalar; +import org.opencv.imgproc.Imgproc; +import org.opencv.imgproc.Moments; public class MomentsTest extends OpenCVTestCase { - public void testGet_m00() { - fail("Not yet implemented"); + Mat data; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + data = new Mat(3,3, CvType.CV_8UC1, new Scalar(1)); + data.row(1).setTo(new Scalar(5)); } - public void testGet_m01() { - fail("Not yet implemented"); - } - - public void testGet_m02() { - fail("Not yet implemented"); - } - - public void testGet_m03() { - fail("Not yet implemented"); - } - - public void testGet_m10() { - fail("Not yet implemented"); - } - - public void testGet_m11() { - fail("Not yet implemented"); - } - - public void testGet_m12() { - fail("Not yet implemented"); - } - - public void testGet_m20() { - fail("Not yet implemented"); - } - - public void testGet_m21() { - fail("Not yet implemented"); - } - - public void testGet_m30() { - fail("Not yet implemented"); - } - - public void testGet_mu02() { - fail("Not yet implemented"); - } - - public void testGet_mu03() { - fail("Not yet implemented"); - } - - public void testGet_mu11() { - fail("Not yet implemented"); - } - - public void testGet_mu12() { - fail("Not yet implemented"); - } - - public void testGet_mu20() { - fail("Not yet implemented"); - } - - public void testGet_mu21() { - fail("Not yet implemented"); - } - - public void testGet_mu30() { - fail("Not yet implemented"); - } - - public void testGet_nu02() { - fail("Not yet implemented"); - } - - public void testGet_nu03() { - fail("Not yet implemented"); - } - - public void testGet_nu11() { - fail("Not yet implemented"); - } - - public void testGet_nu12() { - fail("Not yet implemented"); - } - - public void testGet_nu20() { - fail("Not yet implemented"); - } - - public void testGet_nu21() { - fail("Not yet implemented"); - } - - public void testGet_nu30() { - fail("Not yet implemented"); - } - - public void testSet_m00() { - fail("Not yet implemented"); - } - - public void testSet_m01() { - fail("Not yet implemented"); - } - - public void testSet_m02() { - fail("Not yet implemented"); - } - - public void testSet_m03() { - fail("Not yet implemented"); - } - - public void testSet_m10() { - fail("Not yet implemented"); - } - - public void testSet_m11() { - fail("Not yet implemented"); - } - - public void testSet_m12() { - fail("Not yet implemented"); - } - - public void testSet_m20() { - fail("Not yet implemented"); - } - - public void testSet_m21() { - fail("Not yet implemented"); - } - - public void testSet_m30() { - fail("Not yet implemented"); - } - - public void testSet_mu02() { - fail("Not yet implemented"); - } - - public void testSet_mu03() { - fail("Not yet implemented"); - } - - public void testSet_mu11() { - fail("Not yet implemented"); - } - - public void testSet_mu12() { - fail("Not yet implemented"); - } - - public void testSet_mu20() { - fail("Not yet implemented"); - } - - public void testSet_mu21() { - fail("Not yet implemented"); - } - - public void testSet_mu30() { - fail("Not yet implemented"); - } - - public void testSet_nu02() { - fail("Not yet implemented"); - } - - public void testSet_nu03() { - fail("Not yet implemented"); - } - - public void testSet_nu11() { - fail("Not yet implemented"); - } - - public void testSet_nu12() { - fail("Not yet implemented"); - } - - public void testSet_nu20() { - fail("Not yet implemented"); - } - - public void testSet_nu21() { - fail("Not yet implemented"); - } - - public void testSet_nu30() { - fail("Not yet implemented"); + public void testAll() { + Moments res = Imgproc.moments(data); + assertEquals(res.m00, 21.0, EPS); + assertEquals(res.m10, 21.0, EPS); + assertEquals(res.m01, 21.0, EPS); + assertEquals(res.m20, 35.0, EPS); + assertEquals(res.m11, 21.0, EPS); + assertEquals(res.m02, 27.0, EPS); + assertEquals(res.m30, 63.0, EPS); + assertEquals(res.m21, 35.0, EPS); + assertEquals(res.m12, 27.0, EPS); + assertEquals(res.m03, 39.0, EPS); + assertEquals(res.mu20, 14.0, EPS); + assertEquals(res.mu11, 0.0, EPS); + assertEquals(res.mu02, 6.0, EPS); + assertEquals(res.mu30, 0.0, EPS); + assertEquals(res.mu21, 0.0, EPS); + assertEquals(res.mu12, 0.0, EPS); + assertEquals(res.mu03, 0.0, EPS); + assertEquals(res.nu20, 0.031746031746031744, EPS); + assertEquals(res.nu11, 0.0, EPS); + assertEquals(res.nu02, 0.013605442176870746, EPS); + assertEquals(res.nu30, 0.0, EPS); + assertEquals(res.nu21, 0.0, EPS); + assertEquals(res.nu12, 0.0, EPS); + assertEquals(res.nu03, 0.0, EPS); } } diff --git a/modules/imgproc/src/convhull.cpp b/modules/imgproc/src/convhull.cpp index df50dde4e..b870ee421 100644 --- a/modules/imgproc/src/convhull.cpp +++ b/modules/imgproc/src/convhull.cpp @@ -302,7 +302,7 @@ void convexityDefects( InputArray _points, InputArray _hull, OutputArray _defect int defect_deepest_point = -1; double defect_depth = 0; bool is_defect = false; - + j=hcurr; for(;;) { // go through points to achieve next hull point diff --git a/modules/imgproc/src/deriv.cpp b/modules/imgproc/src/deriv.cpp index b69656a74..dacc23d2e 100644 --- a/modules/imgproc/src/deriv.cpp +++ b/modules/imgproc/src/deriv.cpp @@ -858,7 +858,7 @@ void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize, Mat src = _src.getMat(), dst = _dst.getMat(); int y = fx->start(src), dsty = 0, dy = 0; fy->start(src); - const uchar* sptr = src.ptr(y); + const uchar* sptr = src.ptr() + src.step[0] * y; int dy0 = std::min(std::max((int)(STRIPE_SIZE/(CV_ELEM_SIZE(stype)*src.cols)), 1), src.rows); Mat d2x( dy0 + kd.rows - 1, src.cols, wtype ); diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index 2d975f1c3..4396e0a5a 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -1702,7 +1702,7 @@ void drawMarker(Mat& img, Point position, const Scalar& color, int markerType, i case MARKER_TRIANGLE_UP: line(img, Point(position.x-(markerSize/2), position.y+(markerSize/2)), Point(position.x+(markerSize/2), position.y+(markerSize/2)), color, thickness, line_type); line(img, Point(position.x+(markerSize/2), position.y+(markerSize/2)), Point(position.x, position.y-(markerSize/2)), color, thickness, line_type); - line(img, Point(position.x, position.y-(markerSize/2)), Point(position.x-(markerSize/2), position.y-(markerSize/2)), color, thickness, line_type); + line(img, Point(position.x, position.y-(markerSize/2)), Point(position.x-(markerSize/2), position.y+(markerSize/2)), color, thickness, line_type); break; // The triangle down marker case @@ -2331,10 +2331,10 @@ static void addChildContour(InputArrayOfArrays contours, int h_next = hierarchy[i][0], h_prev = hierarchy[i][1], v_next = hierarchy[i][2], v_prev = hierarchy[i][3]; - seq[i].h_next = (size_t)h_next < ncontours ? &seq[h_next] : 0; - seq[i].h_prev = (size_t)h_prev < ncontours ? &seq[h_prev] : 0; - seq[i].v_next = (size_t)v_next < ncontours ? &seq[v_next] : 0; - seq[i].v_prev = (size_t)v_prev < ncontours ? &seq[v_prev] : 0; + seq[i].h_next = (0 <= h_next && h_next < (int)ncontours) ? &seq[h_next] : 0; + seq[i].h_prev = (0 <= h_prev && h_prev < (int)ncontours) ? &seq[h_prev] : 0; + seq[i].v_next = (0 <= v_next && v_next < (int)ncontours) ? &seq[v_next] : 0; + seq[i].v_prev = (0 <= v_prev && v_prev < (int)ncontours) ? &seq[v_prev] : 0; if( v_next >= 0 ) addChildContour(contours, ncontours, hierarchy, v_next, seq, block); diff --git a/modules/imgproc/src/featureselect.cpp b/modules/imgproc/src/featureselect.cpp index 3d5af6330..2921d3c11 100644 --- a/modules/imgproc/src/featureselect.cpp +++ b/modules/imgproc/src/featureselect.cpp @@ -309,11 +309,18 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners, tmpCorners.push_back(eig_data + x); } } - std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() ); std::vector corners; size_t i, j, total = tmpCorners.size(), ncorners = 0; + if (total == 0) + { + _corners.release(); + return; + } + + std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() ); + if (minDistance >= 1) { // Partition the image into larger grids @@ -351,6 +358,7 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners, y2 = std::min(grid_height-1, y2); for( int yy = y1; yy <= y2; yy++ ) + { for( int xx = x1; xx <= x2; xx++ ) { std::vector &m = grid[yy*grid_width + xx]; @@ -370,6 +378,7 @@ void cv::goodFeaturesToTrack( InputArray _image, OutputArray _corners, } } } + } break_out: diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 585d3a6e9..5822726ee 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -3258,6 +3258,12 @@ void cv::resize( InputArray _src, OutputArray _dst, Size dsize, _dst.create(dsize, src.type()); Mat dst = _dst.getMat(); + if (dsize == ssize) { + // Source and destination are of same size. Use simple copy. + src.copyTo(dst); + return; + } + #ifdef HAVE_TEGRA_OPTIMIZATION if (tegra::useTegra() && tegra::resize(src, dst, (float)inv_scale_x, (float)inv_scale_y, interpolation)) return; diff --git a/modules/imgproc/src/precomp.hpp b/modules/imgproc/src/precomp.hpp index 3bb8d8e76..ed27eeabb 100644 --- a/modules/imgproc/src/precomp.hpp +++ b/modules/imgproc/src/precomp.hpp @@ -49,7 +49,7 @@ #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/core/private.hpp" #include "opencv2/core/ocl.hpp" -#include "opencv2/hal.hpp" +#include "opencv2/core/hal/hal.hpp" #include #include @@ -94,6 +94,6 @@ extern const float icv8x32fSqrTab[]; #include "_geom.h" #include "filterengine.hpp" -#include "opencv2/hal/sse_utils.hpp" +#include "opencv2/core/sse_utils.hpp" #endif /*__OPENCV_CV_INTERNAL_H_*/ diff --git a/modules/imgproc/src/shapedescr.cpp b/modules/imgproc/src/shapedescr.cpp index 87ced482a..294f4fa49 100644 --- a/modules/imgproc/src/shapedescr.cpp +++ b/modules/imgproc/src/shapedescr.cpp @@ -43,163 +43,171 @@ namespace cv { -static int intersectLines( double x1, double dx1, double y1, double dy1, - double x2, double dx2, double y2, double dy2, double *t2 ) +// inner product +static float innerProduct(Point2f &v1, Point2f &v2) { - double d = dx1 * dy2 - dx2 * dy1; - int result = -1; - - if( d != 0 ) - { - *t2 = ((x2 - x1) * dy1 - (y2 - y1) * dx1) / d; - result = 0; - } - return result; + return v1.x * v2.y - v1.y * v2.x; } -static bool findCircle( Point2f pt0, Point2f pt1, Point2f pt2, - Point2f* center, float* radius ) +static void findCircle3pts(Point2f *pts, Point2f ¢er, float &radius) { - double x1 = (pt0.x + pt1.x) * 0.5; - double dy1 = pt0.x - pt1.x; - double x2 = (pt1.x + pt2.x) * 0.5; - double dy2 = pt1.x - pt2.x; - double y1 = (pt0.y + pt1.y) * 0.5; - double dx1 = pt1.y - pt0.y; - double y2 = (pt1.y + pt2.y) * 0.5; - double dx2 = pt2.y - pt1.y; - double t = 0; + // two edges of the triangle v1, v2 + Point2f v1 = pts[1] - pts[0]; + Point2f v2 = pts[2] - pts[0]; - if( intersectLines( x1, dx1, y1, dy1, x2, dx2, y2, dy2, &t ) >= 0 ) + if (innerProduct(v1, v2) == 0.0f) { - center->x = (float) (x2 + dx2 * t); - center->y = (float) (y2 + dy2 * t); - *radius = (float)norm(*center - pt0); - return true; + // v1, v2 colineation, can not determine a unique circle + // find the longtest distance as diameter line + float d1 = (float)norm(pts[0] - pts[1]); + float d2 = (float)norm(pts[0] - pts[2]); + float d3 = (float)norm(pts[1] - pts[2]); + if (d1 >= d2 && d1 >= d3) + { + center = (pts[0] + pts[1]) / 2.0f; + radius = (d1 / 2.0f); + } + else if (d2 >= d1 && d2 >= d3) + { + center = (pts[0] + pts[2]) / 2.0f; + radius = (d2 / 2.0f); + } + else if (d3 >= d1 && d3 >= d2) + { + center = (pts[1] + pts[2]) / 2.0f; + radius = (d3 / 2.0f); + } + } + else + { + // center is intersection of midperpendicular lines of the two edges v1, v2 + // a1*x + b1*y = c1 where a1 = v1.x, b1 = v1.y + // a2*x + b2*y = c2 where a2 = v2.x, b2 = v2.y + Point2f midPoint1 = (pts[0] + pts[1]) / 2.0f; + float c1 = midPoint1.x * v1.x + midPoint1.y * v1.y; + Point2f midPoint2 = (pts[0] + pts[2]) / 2.0f; + float c2 = midPoint2.x * v2.x + midPoint2.y * v2.y; + float det = v1.x * v2.y - v1.y * v2.x; + float cx = (c1 * v2.y - c2 * v1.y) / det; + float cy = (v1.x * c2 - v2.x * c1) / det; + center.x = (float)cx; + center.y = (float)cy; + cx -= pts[0].x; + cy -= pts[0].y; + radius = (float)(std::sqrt(cx *cx + cy * cy)); + } +} + +const float EPS = 1.0e-4f; + +static void findEnclosingCircle3pts_orLess_32f(Point2f *pts, int count, Point2f ¢er, float &radius) +{ + switch (count) + { + case 1: + center = pts[0]; + radius = 0.0f; + break; + case 2: + center.x = (pts[0].x + pts[1].x) / 2.0f; + center.y = (pts[0].y + pts[1].y) / 2.0f; + radius = (float)(norm(pts[0] - pts[1]) / 2.0); + break; + case 3: + findCircle3pts(pts, center, radius); + break; + default: + break; } - center->x = center->y = 0.f; - *radius = 0; - return false; + radius += EPS; } - -static double pointInCircle( Point2f pt, Point2f center, float radius ) +template +static void findThirdPoint(const PT *pts, int i, int j, Point2f ¢er, float &radius) { - double dx = pt.x - center.x; - double dy = pt.y - center.y; - return (double)radius*radius - dx*dx - dy*dy; -} + center.x = (float)(pts[j].x + pts[i].x) / 2.0f; + center.y = (float)(pts[j].y + pts[i].y) / 2.0f; + float dx = (float)(pts[j].x - pts[i].x); + float dy = (float)(pts[j].y - pts[i].y); + radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS; - -static int findEnslosingCicle4pts_32f( Point2f* pts, Point2f& _center, float& _radius ) -{ - int shuffles[4][4] = { {0, 1, 2, 3}, {0, 1, 3, 2}, {2, 3, 0, 1}, {2, 3, 1, 0} }; - - int idxs[4] = { 0, 1, 2, 3 }; - int i, j, k = 1, mi = 0; - float max_dist = 0; - Point2f center; - Point2f min_center; - float radius, min_radius = FLT_MAX; - Point2f res_pts[4]; - - center = min_center = pts[0]; - radius = 1.f; - - for( i = 0; i < 4; i++ ) - for( j = i + 1; j < 4; j++ ) - { - float dist = (float)norm(pts[i] - pts[j]); - - if( max_dist < dist ) - { - max_dist = dist; - idxs[0] = i; - idxs[1] = j; - } - } - - if( max_dist > 0 ) + for (int k = 0; k < j; ++k) { - k = 2; - for( i = 0; i < 4; i++ ) + dx = center.x - (float)pts[k].x; + dy = center.y - (float)pts[k].y; + if (norm(Point2f(dx, dy)) < radius) { - for( j = 0; j < k; j++ ) - if( i == idxs[j] ) - break; - if( j == k ) - idxs[k++] = i; - } - - center = Point2f( (pts[idxs[0]].x + pts[idxs[1]].x)*0.5f, - (pts[idxs[0]].y + pts[idxs[1]].y)*0.5f ); - radius = (float)(norm(pts[idxs[0]] - center)*1.03); - if( radius < 1.f ) - radius = 1.f; - - if( pointInCircle( pts[idxs[2]], center, radius ) >= 0 && - pointInCircle( pts[idxs[3]], center, radius ) >= 0 ) - { - k = 2; //rand()%2+2; + continue; } else { - mi = -1; - for( i = 0; i < 4; i++ ) - { - if( findCircle( pts[shuffles[i][0]], pts[shuffles[i][1]], - pts[shuffles[i][2]], ¢er, &radius ) ) - { - radius *= 1.03f; - if( radius < 2.f ) - radius = 2.f; - - if( pointInCircle( pts[shuffles[i][3]], center, radius ) >= 0 && - min_radius > radius ) - { - min_radius = radius; - min_center = center; - mi = i; - } - } - } - CV_Assert( mi >= 0 ); - if( mi < 0 ) - mi = 0; - k = 3; - center = min_center; - radius = min_radius; - for( i = 0; i < 4; i++ ) - idxs[i] = shuffles[mi][i]; + Point2f ptsf[3]; + ptsf[0] = (Point2f)pts[i]; + ptsf[1] = (Point2f)pts[j]; + ptsf[2] = (Point2f)pts[k]; + findEnclosingCircle3pts_orLess_32f(ptsf, 3, center, radius); } } +} - _center = center; - _radius = radius; - /* reorder output points */ - for( i = 0; i < 4; i++ ) - res_pts[i] = pts[idxs[i]]; +template +void findSecondPoint(const PT *pts, int i, Point2f ¢er, float &radius) +{ + center.x = (float)(pts[0].x + pts[i].x) / 2.0f; + center.y = (float)(pts[0].y + pts[i].y) / 2.0f; + float dx = (float)(pts[0].x - pts[i].x); + float dy = (float)(pts[0].y - pts[i].y); + radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS; - for( i = 0; i < 4; i++ ) + for (int j = 1; j < i; ++j) { - pts[i] = res_pts[i]; - CV_Assert( pointInCircle( pts[i], center, radius ) >= 0 ); + dx = center.x - (float)pts[j].x; + dy = center.y - (float)pts[j].y; + if (norm(Point2f(dx, dy)) < radius) + { + continue; + } + else + { + findThirdPoint(pts, i, j, center, radius); + } } - - return k; } -} +template +static void findMinEnclosingCircle(const PT *pts, int count, Point2f ¢er, float &radius) +{ + center.x = (float)(pts[0].x + pts[1].x) / 2.0f; + center.y = (float)(pts[0].y + pts[1].y) / 2.0f; + float dx = (float)(pts[0].x - pts[1].x); + float dy = (float)(pts[0].y - pts[1].y); + radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS; + + for (int i = 2; i < count; ++i) + { + dx = (float)pts[i].x - center.x; + dy = (float)pts[i].y - center.y; + float d = (float)norm(Point2f(dx, dy)); + if (d < radius) + { + continue; + } + else + { + findSecondPoint(pts, i, center, radius); + } + } +} +} // namespace cv + +// see Welzl, Emo. Smallest enclosing disks (balls and ellipsoids). Springer Berlin Heidelberg, 1991. void cv::minEnclosingCircle( InputArray _points, Point2f& _center, float& _radius ) { - int max_iters = 100; - const float eps = FLT_EPSILON*2; - bool result = false; Mat points = _points.getMat(); - int i, j, k, count = points.checkVector(2); + int count = points.checkVector(2); int depth = points.depth(); Point2f center; float radius = 0.f; @@ -215,78 +223,50 @@ void cv::minEnclosingCircle( InputArray _points, Point2f& _center, float& _radiu const Point* ptsi = points.ptr(); const Point2f* ptsf = points.ptr(); - Point2f pt = is_float ? ptsf[0] : Point2f((float)ptsi[0].x,(float)ptsi[0].y); - Point2f pts[4] = {pt, pt, pt, pt}; - - for( i = 1; i < count; i++ ) + // point count <= 3 + if (count <= 3) { - pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y); - - if( pt.x < pts[0].x ) - pts[0] = pt; - if( pt.x > pts[1].x ) - pts[1] = pt; - if( pt.y < pts[2].y ) - pts[2] = pt; - if( pt.y > pts[3].y ) - pts[3] = pt; + Point2f ptsf3[3]; + for (int i = 0; i < count; ++i) + { + ptsf3[i] = (is_float) ? ptsf[i] : Point2f((float)ptsi[i].x, (float)ptsi[i].y); + } + findEnclosingCircle3pts_orLess_32f(ptsf3, count, center, radius); + _center = center; + _radius = radius; + return; } - for( k = 0; k < max_iters; k++ ) + if (is_float) { - double min_delta = 0, delta; - Point2f farAway(0,0); - /*only for first iteration because the alg is repared at the loop's foot*/ - if( k == 0 ) - findEnslosingCicle4pts_32f( pts, center, radius ); - - for( i = 0; i < count; i++ ) + findMinEnclosingCircle(ptsf, count, center, radius); +#if 0 + for (size_t m = 0; m < count; ++m) { - pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y); - - delta = pointInCircle( pt, center, radius ); - if( delta < min_delta ) + float d = (float)norm(ptsf[m] - center); + if (d > radius) { - min_delta = delta; - farAway = pt; + printf("error!\n"); } } - result = min_delta >= 0; - if( result ) - break; - - Point2f ptsCopy[4]; - // find good replacement partner for the point which is at most far away, - // starting with the one that lays in the actual circle (i=3) - for( i = 3; i >= 0; i-- ) +#endif + } + else + { + findMinEnclosingCircle(ptsi, count, center, radius); +#if 0 + for (size_t m = 0; m < count; ++m) { - for( j = 0; j < 4; j++ ) - ptsCopy[j] = i != j ? pts[j] : farAway; - - findEnslosingCicle4pts_32f( ptsCopy, center, radius ); - if( pointInCircle( pts[i], center, radius ) >= 0) + double dx = ptsi[m].x - center.x; + double dy = ptsi[m].y - center.y; + double d = std::sqrt(dx * dx + dy * dy); + if (d > radius) { - // replaced one again in the new circle? - pts[i] = farAway; - break; + printf("error!\n"); } } +#endif } - - if( !result ) - { - radius = 0.f; - for( i = 0; i < count; i++ ) - { - pt = is_float ? ptsf[i] : Point2f((float)ptsi[i].x,(float)ptsi[i].y); - float dx = center.x - pt.x, dy = center.y - pt.y; - float t = dx*dx + dy*dy; - radius = MAX(radius, t); - } - - radius = (float)(std::sqrt(radius)*(1 + eps)); - } - _center = center; _radius = radius; } diff --git a/modules/imgproc/src/smooth.cpp b/modules/imgproc/src/smooth.cpp index 5eb4b388d..df744effd 100644 --- a/modules/imgproc/src/smooth.cpp +++ b/modules/imgproc/src/smooth.cpp @@ -1851,8 +1851,8 @@ static inline void histogram_add_simd( const HT x[16], HT y[16] ) static inline void histogram_sub_simd( const HT x[16], HT y[16] ) { - vst1q_u16(y, vsubq_u16(vld1q_u16(x), vld1q_u16(y))); - vst1q_u16(y + 8, vsubq_u16(vld1q_u16(x + 8), vld1q_u16(y + 8))); + vst1q_u16(y, vsubq_u16(vld1q_u16(y), vld1q_u16(x))); + vst1q_u16(y + 8, vsubq_u16(vld1q_u16(y + 8), vld1q_u16(x + 8))); } #else diff --git a/modules/imgproc/src/spatialgradient.cpp b/modules/imgproc/src/spatialgradient.cpp index b4dc032ac..411c29947 100644 --- a/modules/imgproc/src/spatialgradient.cpp +++ b/modules/imgproc/src/spatialgradient.cpp @@ -41,7 +41,7 @@ //M*/ #include "precomp.hpp" -#include "opencv2/hal/intrin.hpp" +#include "opencv2/core/hal/intrin.hpp" #include namespace cv diff --git a/modules/imgproc/src/undistort.cpp b/modules/imgproc/src/undistort.cpp index 1a19fdb81..5e6cf05de 100644 --- a/modules/imgproc/src/undistort.cpp +++ b/modules/imgproc/src/undistort.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "opencv2/imgproc/detail/distortion_model.hpp" cv::Mat cv::getDefaultNewCameraMatrix( InputArray _cameraMatrix, Size imgsize, bool centerPrincipalPoint ) @@ -94,7 +95,7 @@ void cv::initUndistortRectifyMap( InputArray _cameraMatrix, InputArray _distCoef distCoeffs = Mat_(distCoeffs); else { - distCoeffs.create(12, 1, CV_64F); + distCoeffs.create(14, 1, CV_64F); distCoeffs = 0.; } @@ -109,7 +110,8 @@ void cv::initUndistortRectifyMap( InputArray _cameraMatrix, InputArray _distCoef CV_Assert( distCoeffs.size() == Size(1, 4) || distCoeffs.size() == Size(4, 1) || distCoeffs.size() == Size(1, 5) || distCoeffs.size() == Size(5, 1) || distCoeffs.size() == Size(1, 8) || distCoeffs.size() == Size(8, 1) || - distCoeffs.size() == Size(1, 12) || distCoeffs.size() == Size(12, 1)); + distCoeffs.size() == Size(1, 12) || distCoeffs.size() == Size(12, 1) || + distCoeffs.size() == Size(1, 14) || distCoeffs.size() == Size(14, 1)); if( distCoeffs.rows != 1 && !distCoeffs.isContinuous() ) distCoeffs = distCoeffs.t(); @@ -127,6 +129,12 @@ void cv::initUndistortRectifyMap( InputArray _cameraMatrix, InputArray _distCoef double s2 = distCoeffs.cols + distCoeffs.rows - 1 >= 12 ? distPtr[9] : 0.; double s3 = distCoeffs.cols + distCoeffs.rows - 1 >= 12 ? distPtr[10] : 0.; double s4 = distCoeffs.cols + distCoeffs.rows - 1 >= 12 ? distPtr[11] : 0.; + double tauX = distCoeffs.cols + distCoeffs.rows - 1 >= 14 ? distPtr[12] : 0.; + double tauY = distCoeffs.cols + distCoeffs.rows - 1 >= 14 ? distPtr[13] : 0.; + + // Matrix for trapezoidal distortion of tilted image sensor + cv::Matx33d matTilt = cv::Matx33d::eye(); + cv::detail::computeTiltProjectionMatrix(tauX, tauY, &matTilt); for( int i = 0; i < size.height; i++ ) { @@ -142,8 +150,12 @@ void cv::initUndistortRectifyMap( InputArray _cameraMatrix, InputArray _distCoef double x2 = x*x, y2 = y*y; double r2 = x2 + y2, _2xy = 2*x*y; double kr = (1 + ((k3*r2 + k2)*r2 + k1)*r2)/(1 + ((k6*r2 + k5)*r2 + k4)*r2); - double u = fx*(x*kr + p1*_2xy + p2*(r2 + 2*x2) + s1*r2+s2*r2*r2) + u0; - double v = fy*(y*kr + p1*(r2 + 2*y2) + p2*_2xy + s3*r2+s4*r2*r2) + v0; + double xd = (x*kr + p1*_2xy + p2*(r2 + 2*x2) + s1*r2+s2*r2*r2); + double yd = (y*kr + p1*(r2 + 2*y2) + p2*_2xy + s3*r2+s4*r2*r2); + cv::Vec3d vecTilt = matTilt*cv::Vec3d(xd, yd, 1); + double invProj = vecTilt(2) ? 1./vecTilt(2) : 1; + double u = fx*invProj*vecTilt(0) + u0; + double v = fy*invProj*vecTilt(1) + v0; if( m1type == CV_16SC2 ) { int iu = saturate_cast(u*INTER_TAB_SIZE); @@ -266,7 +278,7 @@ void cvUndistortPoints( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatr const CvMat* _distCoeffs, const CvMat* matR, const CvMat* matP ) { - double A[3][3], RR[3][3], k[12]={0,0,0,0,0,0,0,0,0,0,0}, fx, fy, ifx, ify, cx, cy; + double A[3][3], RR[3][3], k[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0}, fx, fy, ifx, ify, cx, cy; CvMat matA=cvMat(3, 3, CV_64F, A), _Dk; CvMat _RR=cvMat(3, 3, CV_64F, RR); const CvPoint2D32f* srcf; @@ -276,6 +288,7 @@ void cvUndistortPoints( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatr int stype, dtype; int sstep, dstep; int i, j, n, iters = 1; + cv::Matx33d invMatTilt = cv::Matx33d::eye(); CV_Assert( CV_IS_MAT(_src) && CV_IS_MAT(_dst) && (_src->rows == 1 || _src->cols == 1) && @@ -296,13 +309,16 @@ void cvUndistortPoints( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatr (_distCoeffs->rows*_distCoeffs->cols == 4 || _distCoeffs->rows*_distCoeffs->cols == 5 || _distCoeffs->rows*_distCoeffs->cols == 8 || - _distCoeffs->rows*_distCoeffs->cols == 12)); + _distCoeffs->rows*_distCoeffs->cols == 12 || + _distCoeffs->rows*_distCoeffs->cols == 14)); _Dk = cvMat( _distCoeffs->rows, _distCoeffs->cols, CV_MAKETYPE(CV_64F,CV_MAT_CN(_distCoeffs->type)), k); cvConvert( _distCoeffs, &_Dk ); iters = 5; + if (k[12] != 0 || k[13] != 0) + cv::detail::computeTiltProjectionMatrix(k[12], k[13], NULL, NULL, NULL, &invMatTilt); } if( matR ) @@ -354,8 +370,14 @@ void cvUndistortPoints( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatr y = srcd[i*sstep].y; } - x0 = x = (x - cx)*ifx; - y0 = y = (y - cy)*ify; + x = (x - cx)*ifx; + y = (y - cy)*ify; + + // compensate tilt distortion + cv::Vec3d vecUntilt = invMatTilt * cv::Vec3d(x, y, 1); + double invProj = vecUntilt(2) ? 1./vecUntilt(2) : 1; + x0 = x = invProj * vecUntilt(0); + y0 = y = invProj * vecUntilt(1); // compensate distortion iteratively for( j = 0; j < iters; j++ ) @@ -500,7 +522,7 @@ float cv::initWideAngleProjMap( InputArray _cameraMatrix0, InputArray _distCoeff OutputArray _map1, OutputArray _map2, int projType, double _alpha ) { Mat cameraMatrix0 = _cameraMatrix0.getMat(), distCoeffs0 = _distCoeffs0.getMat(); - double k[12] = {0,0,0,0,0,0,0,0,0,0,0}, M[9]={0,0,0,0,0,0,0,0,0}; + double k[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}, M[9]={0,0,0,0,0,0,0,0,0}; Mat distCoeffs(distCoeffs0.rows, distCoeffs0.cols, CV_MAKETYPE(CV_64F,distCoeffs0.channels()), k); Mat cameraMatrix(3,3,CV_64F,M); Point2f scenter((float)cameraMatrix.at(0,2), (float)cameraMatrix.at(1,2)); @@ -513,7 +535,7 @@ float cv::initWideAngleProjMap( InputArray _cameraMatrix0, InputArray _distCoeff int ndcoeffs = distCoeffs0.cols*distCoeffs0.rows*distCoeffs0.channels(); CV_Assert((distCoeffs0.cols == 1 || distCoeffs0.rows == 1) && - (ndcoeffs == 4 || ndcoeffs == 5 || ndcoeffs == 8)); + (ndcoeffs == 4 || ndcoeffs == 5 || ndcoeffs == 8 || ndcoeffs == 12 || ndcoeffs == 14)); CV_Assert(cameraMatrix0.size() == Size(3,3)); distCoeffs0.convertTo(distCoeffs,CV_64F); cameraMatrix0.convertTo(cameraMatrix,CV_64F); @@ -540,6 +562,8 @@ float cv::initWideAngleProjMap( InputArray _cameraMatrix0, InputArray _distCoeff Mat mapxy(dsize, CV_32FC2); double k1 = k[0], k2 = k[1], k3 = k[2], p1 = k[3], p2 = k[4], k4 = k[5], k5 = k[6], k6 = k[7], s1 = k[8], s2 = k[9], s3 = k[10], s4 = k[11]; double fx = cameraMatrix.at(0,0), fy = cameraMatrix.at(1,1), cx = scenter.x, cy = scenter.y; + cv::Matx33d matTilt; + cv::detail::computeTiltProjectionMatrix(k[12], k[13], &matTilt); for( int y = 0; y < dsize.height; y++ ) { @@ -556,8 +580,12 @@ float cv::initWideAngleProjMap( InputArray _cameraMatrix0, InputArray _distCoeff double x2 = q.x*q.x, y2 = q.y*q.y; double r2 = x2 + y2, _2xy = 2*q.x*q.y; double kr = 1 + ((k3*r2 + k2)*r2 + k1)*r2/(1 + ((k6*r2 + k5)*r2 + k4)*r2); - double u = fx*(q.x*kr + p1*_2xy + p2*(r2 + 2*x2) + s1*r2+ s2*r2*r2) + cx; - double v = fy*(q.y*kr + p1*(r2 + 2*y2) + p2*_2xy + s3*r2+ s4*r2*r2) + cy; + double xd = (q.x*kr + p1*_2xy + p2*(r2 + 2*x2) + s1*r2+ s2*r2*r2); + double yd = (q.y*kr + p1*(r2 + 2*y2) + p2*_2xy + s3*r2+ s4*r2*r2); + cv::Vec3d vecTilt = matTilt*cv::Vec3d(xd, yd, 1); + double invProj = vecTilt(2) ? 1./vecTilt(2) : 1; + double u = fx*invProj*vecTilt(0) + cx; + double v = fy*invProj*vecTilt(1) + cy; mxy[x] = Point2f((float)u, (float)v); } diff --git a/modules/imgproc/test/test_convhull.cpp b/modules/imgproc/test/test_convhull.cpp index 116a4ae3e..fdca2d47f 100644 --- a/modules/imgproc/test/test_convhull.cpp +++ b/modules/imgproc/test/test_convhull.cpp @@ -1913,4 +1913,61 @@ TEST(Imgproc_ContourMoments, accuracy) { CV_ContourMomentsTest test; test.safe_r TEST(Imgproc_ContourPerimeterSlice, accuracy) { CV_PerimeterAreaSliceTest test; test.safe_run(); } TEST(Imgproc_FitEllipse, small) { CV_FitEllipseSmallTest test; test.safe_run(); } + + +PARAM_TEST_CASE(ConvexityDefects_regression_5908, bool, int) +{ +public: + int start_index; + bool clockwise; + + Mat contour; + + virtual void SetUp() + { + clockwise = GET_PARAM(0); + start_index = GET_PARAM(1); + + const int N = 11; + const Point2i points[N] = { + Point2i(154, 408), + Point2i(45, 223), + Point2i(115, 275), // inner + Point2i(104, 166), + Point2i(154, 256), // inner + Point2i(169, 144), + Point2i(185, 256), // inner + Point2i(235, 170), + Point2i(240, 320), // inner + Point2i(330, 287), + Point2i(224, 390) + }; + + contour = Mat(N, 1, CV_32SC2); + for (int i = 0; i < N; i++) + { + contour.at(i) = (!clockwise) // image and convexHull coordinate systems are different + ? points[(start_index + i) % N] + : points[N - 1 - ((start_index + i) % N)]; + } + } +}; + +TEST_P(ConvexityDefects_regression_5908, simple) +{ + std::vector hull; + cv::convexHull(contour, hull, clockwise, false); + + std::vector result; + cv::convexityDefects(contour, hull, result); + + EXPECT_EQ(4, (int)result.size()); +} + +INSTANTIATE_TEST_CASE_P(Imgproc, ConvexityDefects_regression_5908, + testing::Combine( + testing::Bool(), + testing::Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + )); + /* End of file. */ diff --git a/modules/imgproc/test/test_goodfeaturetotrack.cpp b/modules/imgproc/test/test_goodfeaturetotrack.cpp new file mode 100644 index 000000000..53aa08a86 --- /dev/null +++ b/modules/imgproc/test/test_goodfeaturetotrack.cpp @@ -0,0 +1,494 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// Intel License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000, Intel Corporation, all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of Intel Corporation may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "test_precomp.hpp" + +using namespace cv; +using namespace std; + +enum { MINEIGENVAL=0, HARRIS=1, EIGENVALSVECS=2 }; + + +#if 0 //set 1 to switch ON debug message + #define TEST_MESSAGE( message ) std::cout << message; + #define TEST_MESSAGEL( message, val) std::cout << message << val << std::endl; +#else + #define TEST_MESSAGE( message ) + #define TEST_MESSAGEL( message, val) +#endif + +/////////////////////ref////////////////////// + +struct greaterThanPtr : + public std::binary_function +{ + bool operator () (const float * a, const float * b) const + { return *a > *b; } +}; + +static void +test_cornerEigenValsVecs( const Mat& src, Mat& eigenv, int block_size, + int _aperture_size, double k, int mode, int borderType, const Scalar& _borderValue ) +{ + int i, j; + Scalar borderValue = _borderValue; + int aperture_size = _aperture_size < 0 ? 3 : _aperture_size; + Point anchor( aperture_size/2, aperture_size/2 ); + + CV_Assert( src.type() == CV_8UC1 || src.type() == CV_32FC1 ); + CV_Assert( eigenv.type() == CV_32FC1 ); + CV_Assert( ( src.rows == eigenv.rows ) && + (((mode == MINEIGENVAL)||(mode == HARRIS)) && (src.cols == eigenv.cols)) ); + + int type = src.type(); + int ftype = CV_32FC1; + double kernel_scale = 1; + + Mat dx2, dy2, dxdy(src.size(), CV_32F), kernel; + + kernel = cvtest::calcSobelKernel2D( 1, 0, _aperture_size ); + cvtest::filter2D( src, dx2, ftype, kernel*kernel_scale, anchor, 0, borderType, borderValue ); + kernel = cvtest::calcSobelKernel2D( 0, 1, _aperture_size ); + cvtest::filter2D( src, dy2, ftype, kernel*kernel_scale, anchor, 0, borderType,borderValue ); + + double denom = (1 << (aperture_size-1))*block_size; + denom = denom * denom; + + if( _aperture_size < 0 ) + denom *= 4; + if(type != ftype ) + denom *= 255.; + + denom = 1./denom; + + for( i = 0; i < src.rows; i++ ) + { + float* dxdyp = dxdy.ptr(i); + float* dx2p = dx2.ptr(i); + float* dy2p = dy2.ptr(i); + + for( j = 0; j < src.cols; j++ ) + { + double xval = dx2p[j], yval = dy2p[j]; + dxdyp[j] = (float)(xval*yval*denom); + dx2p[j] = (float)(xval*xval*denom); + dy2p[j] = (float)(yval*yval*denom); + } + } + + kernel = Mat::ones(block_size, block_size, CV_32F); + anchor = Point(block_size/2, block_size/2); + + cvtest::filter2D( dx2, dx2, ftype, kernel, anchor, 0, borderType, borderValue ); + cvtest::filter2D( dy2, dy2, ftype, kernel, anchor, 0, borderType, borderValue ); + cvtest::filter2D( dxdy, dxdy, ftype, kernel, anchor, 0, borderType, borderValue ); + + if( mode == MINEIGENVAL ) + { + for( i = 0; i < src.rows; i++ ) + { + float* eigenvp = eigenv.ptr(i); + const float* dxdyp = dxdy.ptr(i); + const float* dx2p = dx2.ptr(i); + const float* dy2p = dy2.ptr(i); + + for( j = 0; j < src.cols; j++ ) + { + double a = dx2p[j], b = dxdyp[j], c = dy2p[j]; + double d = sqrt( ( a - c )*( a - c ) + 4*b*b ); + eigenvp[j] = (float)( 0.5*(a + c - d)); + } + } + } + else if( mode == HARRIS ) + { + + for( i = 0; i < src.rows; i++ ) + { + float* eigenvp = eigenv.ptr(i); + const float* dxdyp = dxdy.ptr(i); + const float* dx2p = dx2.ptr(i); + const float* dy2p = dy2.ptr(i); + + for( j = 0; j < src.cols; j++ ) + { + double a = dx2p[j], b = dxdyp[j], c = dy2p[j]; + eigenvp[j] = (float)(a*c - b*b - k*(a + c)*(a + c)); + } + } + } +} + + +static void +test_goodFeaturesToTrack( InputArray _image, OutputArray _corners, + int maxCorners, double qualityLevel, double minDistance, + InputArray _mask, int blockSize, + bool useHarrisDetector, double harrisK ) +{ + + CV_Assert( qualityLevel > 0 && minDistance >= 0 && maxCorners >= 0 ); + CV_Assert( _mask.empty() || (_mask.type() == CV_8UC1 && _mask.sameSize(_image)) ); + + + Mat image = _image.getMat(), mask = _mask.getMat(); + int aperture_size = 3; + int borderType = BORDER_DEFAULT; + + Mat eig, tmp, tt; + + eig.create( image.size(), CV_32F ); + + if( useHarrisDetector ) + test_cornerEigenValsVecs( image, eig, blockSize, aperture_size, harrisK, HARRIS, borderType, 0 ); + else + test_cornerEigenValsVecs( image, eig, blockSize, aperture_size, 0, MINEIGENVAL, borderType, 0 ); + + double maxVal = 0; + + cvtest::minMaxIdx( eig, 0, &maxVal, 0, 0, mask ); + cvtest::threshold( eig, eig, (float)(maxVal*qualityLevel), 0.f,THRESH_TOZERO ); + cvtest::dilate( eig, tmp, Mat(),Point(-1,-1),borderType,0); + + Size imgsize = image.size(); + + vector tmpCorners; + + // collect list of pointers to features - put them into temporary image + for( int y = 1; y < imgsize.height - 1; y++ ) + { + const float* eig_data = (const float*)eig.ptr(y); + const float* tmp_data = (const float*)tmp.ptr(y); + const uchar* mask_data = mask.data ? mask.ptr(y) : 0; + + for( int x = 1; x < imgsize.width - 1; x++ ) + { + float val = eig_data[x]; + if( val != 0 && val == tmp_data[x] && (!mask_data || mask_data[x]) ) + { + tmpCorners.push_back(eig_data + x); + } + } + } + + vector corners; + size_t i, j, total = tmpCorners.size(), ncorners = 0; + + std::sort( tmpCorners.begin(), tmpCorners.end(), greaterThanPtr() ); + + if(minDistance >= 1) + { + // Partition the image into larger grids + int w = image.cols; + int h = image.rows; + + const int cell_size = cvRound(minDistance); + const int grid_width = (w + cell_size - 1) / cell_size; + const int grid_height = (h + cell_size - 1) / cell_size; + + std::vector > grid(grid_width*grid_height); + + minDistance *= minDistance; + + for( i = 0; i < total; i++ ) + { + int ofs = (int)((const uchar*)tmpCorners[i] - eig.data); + int y = (int)(ofs / eig.step); + int x = (int)((ofs - y*eig.step)/sizeof(float)); + + bool good = true; + + int x_cell = x / cell_size; + int y_cell = y / cell_size; + + int x1 = x_cell - 1; + int y1 = y_cell - 1; + int x2 = x_cell + 1; + int y2 = y_cell + 1; + + // boundary check + x1 = std::max(0, x1); + y1 = std::max(0, y1); + x2 = std::min(grid_width-1, x2); + y2 = std::min(grid_height-1, y2); + + for( int yy = y1; yy <= y2; yy++ ) + { + for( int xx = x1; xx <= x2; xx++ ) + { + vector &m = grid[yy*grid_width + xx]; + + if( m.size() ) + { + for(j = 0; j < m.size(); j++) + { + float dx = x - m[j].x; + float dy = y - m[j].y; + + if( dx*dx + dy*dy < minDistance ) + { + good = false; + goto break_out; + } + } + } + } + } + + break_out: + + if(good) + { + grid[y_cell*grid_width + x_cell].push_back(Point2f((float)x, (float)y)); + + corners.push_back(Point2f((float)x, (float)y)); + ++ncorners; + + if( maxCorners > 0 && (int)ncorners == maxCorners ) + break; + } + } + } + else + { + for( i = 0; i < total; i++ ) + { + int ofs = (int)((const uchar*)tmpCorners[i] - eig.data); + int y = (int)(ofs / eig.step); + int x = (int)((ofs - y*eig.step)/sizeof(float)); + + corners.push_back(Point2f((float)x, (float)y)); + ++ncorners; + if( maxCorners > 0 && (int)ncorners == maxCorners ) + break; + } + } + + Mat(corners).convertTo(_corners, _corners.fixedType() ? _corners.type() : CV_32F); + +} + +/////////////////end of ref code////////////////////////// + + + +class CV_GoodFeatureToTTest : public cvtest::ArrayTest +{ +public: + CV_GoodFeatureToTTest(); + +protected: + int prepare_test_case( int test_case_idx ); + void run_func(); + int validate_test_results( int test_case_idx ); + + Mat src, src_gray; + Mat src_gray32f, src_gray8U; + Mat mask; + + int maxCorners; + vector corners; + vector Refcorners; + double qualityLevel; + double minDistance; + int blockSize; + bool useHarrisDetector; + double k; + int SrcType; +}; + + +CV_GoodFeatureToTTest::CV_GoodFeatureToTTest() +{ + RNG& rng = ts->get_rng(); + maxCorners = rng.uniform( 50, 100 ); + qualityLevel = 0.01; + minDistance = 10; + blockSize = 3; + useHarrisDetector = false; + k = 0.04; + mask = Mat(); + test_case_count = 4; + SrcType = 0; +} + +int CV_GoodFeatureToTTest::prepare_test_case( int test_case_idx ) +{ + const static int types[] = { CV_32FC1, CV_8UC1 }; + + cvtest::TS& tst = *cvtest::TS::ptr(); + src = imread(string(tst.get_data_path()) + "shared/fruits.png", IMREAD_COLOR); + + CV_Assert(src.data != NULL); + + cvtColor( src, src_gray, CV_BGR2GRAY ); + SrcType = types[test_case_idx & 0x1]; + useHarrisDetector = test_case_idx & 2 ? true : false; + return 1; +} + + +void CV_GoodFeatureToTTest::run_func() +{ + int cn = src_gray.channels(); + + CV_Assert( cn == 1 ); + CV_Assert( ( CV_MAT_DEPTH(SrcType) == CV_32FC1 ) || ( CV_MAT_DEPTH(SrcType) == CV_8UC1 )); + + TEST_MESSAGEL (" maxCorners = ", maxCorners) + if (useHarrisDetector) + { + TEST_MESSAGE (" useHarrisDetector = true\n"); + } + else + { + TEST_MESSAGE (" useHarrisDetector = false\n"); + } + + if( CV_MAT_DEPTH(SrcType) == CV_32FC1) + { + if (src_gray.depth() != CV_32FC1 ) src_gray.convertTo(src_gray32f, CV_32FC1); + else src_gray32f = src_gray.clone(); + + TEST_MESSAGE ("goodFeaturesToTrack 32f\n") + + goodFeaturesToTrack( src_gray32f, + corners, + maxCorners, + qualityLevel, + minDistance, + Mat(), + blockSize, + useHarrisDetector, + k ); + } + else + { + if (src_gray.depth() != CV_8UC1 ) src_gray.convertTo(src_gray8U, CV_8UC1); + else src_gray8U = src_gray.clone(); + + TEST_MESSAGE ("goodFeaturesToTrack 8U\n") + + goodFeaturesToTrack( src_gray8U, + corners, + maxCorners, + qualityLevel, + minDistance, + Mat(), + blockSize, + useHarrisDetector, + k ); + } +} + + +int CV_GoodFeatureToTTest::validate_test_results( int test_case_idx ) +{ + static const double eps = 2e-6; + + if( CV_MAT_DEPTH(SrcType) == CV_32FC1 ) + { + if (src_gray.depth() != CV_32FC1 ) src_gray.convertTo(src_gray32f, CV_32FC1); + else src_gray32f = src_gray.clone(); + + TEST_MESSAGE ("test_goodFeaturesToTrack 32f\n") + + test_goodFeaturesToTrack( src_gray32f, + Refcorners, + maxCorners, + qualityLevel, + minDistance, + Mat(), + blockSize, + useHarrisDetector, + k ); + } + else + { + if (src_gray.depth() != CV_8UC1 ) src_gray.convertTo(src_gray8U, CV_8UC1); + else src_gray8U = src_gray.clone(); + + TEST_MESSAGE ("test_goodFeaturesToTrack 8U\n") + + test_goodFeaturesToTrack( src_gray8U, + Refcorners, + maxCorners, + qualityLevel, + minDistance, + Mat(), + blockSize, + useHarrisDetector, + k ); + } + + double e =norm(corners, Refcorners); + + if (e > eps) + { + TEST_MESSAGEL ("Number of features: Refcorners = ", Refcorners.size()) + TEST_MESSAGEL (" TestCorners = ", corners.size()) + TEST_MESSAGE ("\n") + + ts->printf(cvtest::TS::CONSOLE, "actual error: %g, expected: %g", e, eps); + ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); + + for(int i = 0; i < (int)std::min((unsigned int)(corners.size()), (unsigned int)(Refcorners.size())); i++){ + if ( (corners[i].x != Refcorners[i].x) || (corners[i].y != Refcorners[i].y)) + printf("i = %i X %2.2f Xref %2.2f Y %2.2f Yref %2.2f\n",i,corners[i].x,Refcorners[i].x,corners[i].y,Refcorners[i].y); + } + } + else + { + TEST_MESSAGEL (" Refcorners = ", Refcorners.size()) + TEST_MESSAGEL (" TestCorners = ", corners.size()) + TEST_MESSAGE ("\n") + + ts->set_failed_test_info(cvtest::TS::OK); + } + + return BaseTest::validate_test_results(test_case_idx); + +} + +TEST(Imgproc_GoodFeatureToT, accuracy) { CV_GoodFeatureToTTest test; test.safe_run(); } + + +/* End of file. */ diff --git a/modules/java/CMakeLists.txt b/modules/java/CMakeLists.txt index aa48d9d44..9f241d3f1 100644 --- a/modules/java/CMakeLists.txt +++ b/modules/java/CMakeLists.txt @@ -1,7 +1,8 @@ # ---------------------------------------------------------------------------- # CMake file for java support # ---------------------------------------------------------------------------- -if(IOS OR WINRT OR NOT PYTHON_DEFAULT_AVAILABLE OR NOT ANT_EXECUTABLE OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)) +if(APPLE_FRAMEWORK OR WINRT OR NOT PYTHON_DEFAULT_AVAILABLE OR NOT ANT_EXECUTABLE + OR NOT (JNI_FOUND OR (ANDROID AND ANDROID_NATIVE_API_LEVEL GREATER 7)) OR BUILD_opencv_world ) ocv_module_disable(java) @@ -346,9 +347,16 @@ endif(ANDROID) # workarounding lack of `__attribute__ ((visibility("default")))` in jni_md.h/JNIEXPORT string(REPLACE "-fvisibility=hidden" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -ocv_add_library(${the_module} SHARED ${handwritten_h_sources} ${handwritten_cpp_sources} ${generated_cpp_sources} +if(ANDROID) + # Android native code need to link with libopencv_java.so + ocv_add_library(${the_module} SHARED ${handwritten_h_sources} ${handwritten_cpp_sources} ${generated_cpp_sources} ${copied_files} "${JAR_FILE}" "${JAR_FILE}.dephelper") +else() + ocv_add_library(${the_module} MODULE ${handwritten_h_sources} ${handwritten_cpp_sources} ${generated_cpp_sources} + ${copied_files} + "${JAR_FILE}" "${JAR_FILE}.dephelper") +endif() if(BUILD_FAT_JAVA_LIB) set(__deps ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULES_BUILD}) diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index a51bef170..8c869756b 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -299,6 +299,13 @@ type_dict = { "jn_type" : "double[]", "jni_var" : "Vec3d %(n)s(%(n)s_val0, %(n)s_val1, %(n)s_val2)", "jni_type" : "jdoubleArray", "suffix" : "DDD"}, + "Moments" : { + "j_type" : "Moments", + "jn_args" : (("double", ".m00"), ("double", ".m10"), ("double", ".m01"), ("double", ".m20"), ("double", ".m11"), + ("double", ".m02"), ("double", ".m30"), ("double", ".m21"), ("double", ".m12"), ("double", ".m03")), + "jni_var" : "Moments %(n)s(%(n)s_m00, %(n)s_m10, %(n)s_m01, %(n)s_m20, %(n)s_m11, %(n)s_m02, %(n)s_m30, %(n)s_m21, %(n)s_m12, %(n)s_m03)", + "jni_type" : "jdoubleArray", + "suffix" : "DDDDDDDDDD"}, } diff --git a/modules/java/generator/src/java/android+OpenCVLoader.java b/modules/java/generator/src/java/android+OpenCVLoader.java index 339ea2d9d..b8a970592 100644 --- a/modules/java/generator/src/java/android+OpenCVLoader.java +++ b/modules/java/generator/src/java/android+OpenCVLoader.java @@ -62,6 +62,11 @@ public class OpenCVLoader */ public static final String OPENCV_VERSION_3_0_0 = "3.0.0"; + /** + * OpenCV Library version 3.1.0. + */ + public static final String OPENCV_VERSION_3_1_0 = "3.1.0"; + /** * Loads and initializes OpenCV library from current application package. Roughly, it's an analog of system.loadLibrary("opencv_java"). diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index 715cbd998..0b9026950 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -224,6 +224,9 @@ public: CV_WRAP virtual void setTrainTestSplitRatio(double ratio, bool shuffle=true) = 0; CV_WRAP virtual void shuffleTrainTest() = 0; + /** @brief Returns matrix of test samples */ + CV_WRAP Mat getTestSamples() const; + CV_WRAP static Mat getSubVector(const Mat& vec, const Mat& idx); /** @brief Reads the dataset from a .csv file and returns the ready-to-use training data. @@ -675,11 +678,19 @@ public: /** @brief Retrieves all the support vectors - The method returns all the support vector as floating-point matrix, where support vectors are + The method returns all the support vectors as a floating-point matrix, where support vectors are stored as matrix rows. */ CV_WRAP virtual Mat getSupportVectors() const = 0; + /** @brief Retrieves all the uncompressed support vectors of a linear %SVM + + The method returns all the uncompressed support vectors of a linear %SVM that the compressed + support vector, used for prediction, was derived from. They are returned in a floating-point + matrix, where the support vectors are stored as matrix rows. + */ + CV_WRAP Mat getUncompressedSupportVectors() const; + /** @brief Retrieves the decision function @param i the index of the decision function. If the problem solved is regression, 1-class or @@ -711,6 +722,15 @@ public: Use StatModel::train to train the model. Since %SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto. */ CV_WRAP static Ptr create(); + + /** @brief Loads and creates a serialized svm from a file + * + * Use SVM::save to serialize and store an SVM to disk. + * Load the SVM from this file again, by calling this function with the path to the file. + * + * @param filepath path to serialized svm + */ + CV_WRAP static Ptr load(const String& filepath); }; /****************************************************************************************\ @@ -1381,6 +1401,16 @@ public: Note that the train method has optional flags: ANN_MLP::TrainFlags. */ CV_WRAP static Ptr create(); + + /** @brief Loads and creates a serialized ANN from a file + * + * Use ANN::save to serialize and store an ANN to disk. + * Load the ANN from this file again, by calling this function with the path to the file. + * + * @param filepath path to serialized ANN + */ + CV_WRAP static Ptr load(const String& filepath); + }; /****************************************************************************************\ diff --git a/modules/ml/src/ann_mlp.cpp b/modules/ml/src/ann_mlp.cpp index ff6512ded..19ee91332 100644 --- a/modules/ml/src/ann_mlp.cpp +++ b/modules/ml/src/ann_mlp.cpp @@ -1317,6 +1317,18 @@ Ptr ANN_MLP::create() return makePtr(); } -}} +Ptr ANN_MLP::load(const String& filepath) +{ + FileStorage fs; + fs.open(filepath, FileStorage::READ); + + Ptr ann = makePtr(); + + ((ANN_MLPImpl*)ann.get())->read(fs.getFirstTopLevelNode()); + return ann; +} + + + }} /* End of file. */ diff --git a/modules/ml/src/data.cpp b/modules/ml/src/data.cpp index feb79d93a..ad652568c 100644 --- a/modules/ml/src/data.cpp +++ b/modules/ml/src/data.cpp @@ -50,6 +50,13 @@ static const int VAR_MISSED = VAR_ORDERED; TrainData::~TrainData() {} +Mat TrainData::getTestSamples() const +{ + Mat idx = getTestSampleIdx(); + Mat samples = getSamples(); + return idx.empty() ? Mat() : getSubVector(samples, idx); +} + Mat TrainData::getSubVector(const Mat& vec, const Mat& idx) { if( idx.empty() ) @@ -253,7 +260,7 @@ public: if( !sampleIdx.empty() ) { CV_Assert( (sampleIdx.checkVector(1, CV_32S, true) > 0 && - checkRange(sampleIdx, true, 0, 0, nsamples-1)) || + checkRange(sampleIdx, true, 0, 0, nsamples)) || sampleIdx.checkVector(1, CV_8U, true) == nsamples ); if( sampleIdx.type() == CV_8U ) sampleIdx = convertMaskToIdx(sampleIdx); diff --git a/modules/ml/src/em.cpp b/modules/ml/src/em.cpp index 0ee3c4a71..99b68666f 100644 --- a/modules/ml/src/em.cpp +++ b/modules/ml/src/em.cpp @@ -161,7 +161,7 @@ public: { bool needprobs = _outputs.needed(); Mat samples = _inputs.getMat(), probs, probsrow; - int ptype = CV_32F; + int ptype = CV_64F; float firstres = 0.f; int i, nsamples = samples.rows; @@ -170,6 +170,7 @@ public: if( _outputs.fixedType() ) ptype = _outputs.type(); _outputs.create(samples.rows, nclusters, ptype); + probs = _outputs.getMat(); } else nsamples = std::min(nsamples, 1); @@ -187,7 +188,7 @@ public: Vec2d predict2(InputArray _sample, OutputArray _probs) const { - int ptype = CV_32F; + int ptype = CV_64F; Mat sample = _sample.getMat(); CV_Assert(isTrained()); @@ -198,7 +199,7 @@ public: sample.convertTo(tmp, CV_64FC1); sample = tmp; } - sample.reshape(1, 1); + sample = sample.reshape(1, 1); Mat probs; if( _probs.needed() ) @@ -342,7 +343,7 @@ public: if(weights0 && (startStep == START_E_STEP && covs0)) { weights0->convertTo(weights, CV_64FC1); - weights.reshape(1,1); + weights = weights.reshape(1,1); preprocessProbability(weights); } diff --git a/modules/ml/src/lr.cpp b/modules/ml/src/lr.cpp index 24fc29f67..585162cf9 100644 --- a/modules/ml/src/lr.cpp +++ b/modules/ml/src/lr.cpp @@ -96,11 +96,11 @@ public: CV_IMPL_PROPERTY(TermCriteria, TermCriteria, params.term_crit) virtual bool train( const Ptr& trainData, int=0 ); - virtual float predict(InputArray samples, OutputArray results, int) const; + virtual float predict(InputArray samples, OutputArray results, int flags=0) const; virtual void clear(); virtual void write(FileStorage& fs) const; virtual void read(const FileNode& fn); - virtual Mat get_learnt_thetas() const; + virtual Mat get_learnt_thetas() const { return learnt_thetas; } virtual int getVarCount() const { return learnt_thetas.cols; } virtual bool isTrained() const { return !learnt_thetas.empty(); } virtual bool isClassifier() const { return true; } @@ -108,8 +108,9 @@ public: protected: Mat calc_sigmoid(const Mat& data) const; double compute_cost(const Mat& _data, const Mat& _labels, const Mat& _init_theta); - Mat compute_batch_gradient(const Mat& _data, const Mat& _labels, const Mat& _init_theta); - Mat compute_mini_batch_gradient(const Mat& _data, const Mat& _labels, const Mat& _init_theta); + void compute_gradient(const Mat& _data, const Mat& _labels, const Mat &_theta, const double _lambda, Mat & _gradient ); + Mat batch_gradient_descent(const Mat& _data, const Mat& _labels, const Mat& _init_theta); + Mat mini_batch_gradient_descent(const Mat& _data, const Mat& _labels, const Mat& _init_theta); bool set_label_map(const Mat& _labels_i); Mat remap_labels(const Mat& _labels_i, const map& lmap) const; protected: @@ -128,173 +129,177 @@ Ptr LogisticRegression::create() bool LogisticRegressionImpl::train(const Ptr& trainData, int) { + // return value + bool ok = false; + clear(); Mat _data_i = trainData->getSamples(); Mat _labels_i = trainData->getResponses(); + // check size and type of training data CV_Assert( !_labels_i.empty() && !_data_i.empty()); - - // check the number of columns if(_labels_i.cols != 1) { - CV_Error( CV_StsBadArg, "_labels_i should be a column matrix" ); + CV_Error( CV_StsBadArg, "labels should be a column matrix" ); } - - // check data type. - // data should be of floating type CV_32FC1 - - if((_data_i.type() != CV_32FC1) || (_labels_i.type() != CV_32FC1)) + if(_data_i.type() != CV_32FC1 || _labels_i.type() != CV_32FC1) { CV_Error( CV_StsBadArg, "data and labels must be a floating point matrix" ); } - - bool ok = false; - - Mat labels; - - set_label_map(_labels_i); - int num_classes = (int) this->forward_mapper.size(); - - // add a column of ones - Mat data_t = Mat::zeros(_data_i.rows, _data_i.cols+1, CV_32F); - vconcat(Mat(_data_i.rows, 1, _data_i.type(), Scalar::all(1.0)), data_t.col(0)); - - for (int i=1;iforward_mapper); + int num_classes = (int) this->forward_mapper.size(); if(num_classes < 2) { CV_Error( CV_StsBadArg, "data should have atleast 2 classes" ); } - if(_labels_i.rows != _data_i.rows) - { - CV_Error( CV_StsBadArg, "number of rows in data and labels should be the equal" ); - } + // add a column of ones to the data (bias/intercept term) + Mat data_t; + hconcat( cv::Mat::ones( _data_i.rows, 1, CV_32F ), _data_i, data_t ); - - Mat thetas = Mat::zeros(num_classes, data_t.cols, CV_32F); + // coefficient matrix (zero-initialized) + Mat thetas; Mat init_theta = Mat::zeros(data_t.cols, 1, CV_32F); - Mat labels_l = remap_labels(_labels_i, this->forward_mapper); - Mat new_local_labels; - - int ii=0; + // fit the model (handles binary and multiclass cases) Mat new_theta; - + Mat labels; if(num_classes == 2) { labels_l.convertTo(labels, CV_32F); if(this->params.train_method == LogisticRegression::BATCH) - new_theta = compute_batch_gradient(data_t, labels, init_theta); + new_theta = batch_gradient_descent(data_t, labels, init_theta); else - new_theta = compute_mini_batch_gradient(data_t, labels, init_theta); + new_theta = mini_batch_gradient_descent(data_t, labels, init_theta); thetas = new_theta.t(); } else { /* take each class and rename classes you will get a theta per class as in multi class class scenario, we will have n thetas for n classes */ - ii = 0; - + thetas.create(num_classes, data_t.cols, CV_32F); + Mat labels_binary; + int ii = 0; for(map::iterator it = this->forward_mapper.begin(); it != this->forward_mapper.end(); ++it) { - new_local_labels = (labels_l == it->second)/255; - new_local_labels.convertTo(labels, CV_32F); + // one-vs-rest (OvR) scheme + labels_binary = (labels_l == it->second)/255; + labels_binary.convertTo(labels, CV_32F); if(this->params.train_method == LogisticRegression::BATCH) - new_theta = compute_batch_gradient(data_t, labels, init_theta); + new_theta = batch_gradient_descent(data_t, labels, init_theta); else - new_theta = compute_mini_batch_gradient(data_t, labels, init_theta); + new_theta = mini_batch_gradient_descent(data_t, labels, init_theta); hconcat(new_theta.t(), thetas.row(ii)); ii += 1; } } + // check that the estimates are stable and finite this->learnt_thetas = thetas.clone(); if( cvIsNaN( (double)sum(this->learnt_thetas)[0] ) ) { CV_Error( CV_StsBadArg, "check training parameters. Invalid training classifier" ); } + + // success ok = true; return ok; } -float LogisticRegressionImpl::predict(InputArray samples, OutputArray results, int) const +float LogisticRegressionImpl::predict(InputArray samples, OutputArray results, int flags) const { - /* returns a class of the predicted class - class names can be 1,2,3,4, .... etc */ - Mat thetas, data, pred_labs; - data = samples.getMat(); - // check if learnt_mats array is populated - if(this->learnt_thetas.total()<=0) + if(!this->isTrained()) { CV_Error( CV_StsBadArg, "classifier should be trained first" ); } + + // coefficient matrix + Mat thetas; + if ( learnt_thetas.type() == CV_32F ) + { + thetas = learnt_thetas; + } + else + { + this->learnt_thetas.convertTo( thetas, CV_32F ); + } + CV_Assert(thetas.rows > 0); + + // data samples + Mat data = samples.getMat(); if(data.type() != CV_32F) { CV_Error( CV_StsBadArg, "data must be of floating type" ); } - // add a column of ones - Mat data_t = Mat::zeros(data.rows, data.cols+1, CV_32F); - for (int i=0;ilearnt_thetas.convertTo(thetas, CV_32F); - - CV_Assert(thetas.rows > 0); - - double min_val; - double max_val; - - Point min_loc; - Point max_loc; - - Mat labels; + // predict class labels for samples (handles binary and multiclass cases) Mat labels_c; + Mat pred_m; Mat temp_pred; - Mat pred_m = Mat::zeros(data_t.rows, thetas.rows, data.type()); - if(thetas.rows == 1) { - temp_pred = calc_sigmoid(data_t*thetas.t()); + // apply sigmoid function + temp_pred = calc_sigmoid(data_t * thetas.t()); CV_Assert(temp_pred.cols==1); + pred_m = temp_pred.clone(); // if greater than 0.5, predict class 0 or predict class 1 - temp_pred = (temp_pred>0.5)/255; + temp_pred = (temp_pred > 0.5f) / 255; temp_pred.convertTo(labels_c, CV_32S); } else { - for(int i = 0;ireverse_mapper); - // convert pred_labs to integer type + + // return label of the predicted class. class names can be 1,2,3,... + Mat pred_labs = remap_labels(labels_c, this->reverse_mapper); pred_labs.convertTo(pred_labs, CV_32S); - pred_labs.copyTo(results); - // TODO: determine - return 0; + + // return either the labels or the raw output + if ( results.needed() ) + { + if ( flags & StatModel::RAW_OUTPUT ) + { + pred_m.copyTo( results ); + } + else + { + pred_labs.copyTo(results); + } + } + + return ( pred_labs.empty() ? 0.f : static_cast(pred_labs.at(0)) ); } Mat LogisticRegressionImpl::calc_sigmoid(const Mat& data) const @@ -320,7 +325,6 @@ double LogisticRegressionImpl::compute_cost(const Mat& _data, const Mat& _labels n = _data.cols; theta_b = _init_theta(Range(1, n), Range::all()); - multiply(theta_b, theta_b, theta_c, 1); if (params.norm != REG_DISABLE) { @@ -334,31 +338,63 @@ double LogisticRegressionImpl::compute_cost(const Mat& _data, const Mat& _labels else { // assuming it to be L2 by default + multiply(theta_b, theta_b, theta_c, 1); rparameter = (llambda/(2*m)) * sum(theta_c)[0]; } - d_a = calc_sigmoid(_data* _init_theta); - - + d_a = calc_sigmoid(_data * _init_theta); log(d_a, d_a); multiply(d_a, _labels, d_a); - d_b = 1 - calc_sigmoid(_data * _init_theta); + // use the fact that: log(1 - sigmoid(x)) = log(sigmoid(-x)) + d_b = calc_sigmoid(- _data * _init_theta); log(d_b, d_b); multiply(d_b, 1-_labels, d_b); cost = (-1.0/m) * (sum(d_a)[0] + sum(d_b)[0]); cost = cost + rparameter; + if(cvIsNaN( cost ) == 1) + { + CV_Error( CV_StsBadArg, "check training parameters. Invalid training classifier" ); + } + return cost; } -Mat LogisticRegressionImpl::compute_batch_gradient(const Mat& _data, const Mat& _labels, const Mat& _init_theta) + +void LogisticRegressionImpl::compute_gradient(const Mat& _data, const Mat& _labels, const Mat &_theta, const double _lambda, Mat & _gradient ) +{ + const int m = _data.rows; + Mat pcal_a, pcal_b, pcal_ab; + + const Mat z = _data * _theta; + + CV_Assert( _gradient.rows == _theta.rows && _gradient.cols == _theta.cols ); + + pcal_a = calc_sigmoid(z) - _labels; + pcal_b = _data(Range::all(), Range(0,1)); + multiply(pcal_a, pcal_b, pcal_ab, 1); + + _gradient.row(0) = ((float)1/m) * sum(pcal_ab)[0]; + + //cout<<"for each training data entry"<params.alpha<=0) { - CV_Error( CV_StsBadArg, "check training parameters for the classifier" ); + CV_Error( CV_StsBadArg, "check training parameters (learning rate) for the classifier" ); } if(this->params.num_iters <= 0) @@ -367,15 +403,10 @@ Mat LogisticRegressionImpl::compute_batch_gradient(const Mat& _data, const Mat& } int llambda = 0; - double ccost; - int m, n; - Mat pcal_a; - Mat pcal_b; - Mat pcal_ab; - Mat gradient; + int m; Mat theta_p = _init_theta.clone(); + Mat gradient( theta_p.rows, theta_p.cols, theta_p.type() ); m = _data.rows; - n = _data.cols; if (params.norm != REG_DISABLE) { @@ -384,50 +415,21 @@ Mat LogisticRegressionImpl::compute_batch_gradient(const Mat& _data, const Mat& for(int i = 0;iparams.num_iters;i++) { - ccost = compute_cost(_data, _labels, theta_p); + // this seems to only be called to ensure that cost is not NaN + compute_cost(_data, _labels, theta_p); - if( cvIsNaN( ccost ) ) - { - CV_Error( CV_StsBadArg, "check training parameters. Invalid training classifier" ); - } - - pcal_b = calc_sigmoid((_data*theta_p) - _labels); - - pcal_a = (static_cast(1/m)) * _data.t(); - - gradient = pcal_a * pcal_b; - - pcal_a = calc_sigmoid(_data*theta_p) - _labels; - - pcal_b = _data(Range::all(), Range(0,1)); - - multiply(pcal_a, pcal_b, pcal_ab, 1); - - gradient.row(0) = ((float)1/m) * sum(pcal_ab)[0]; - - pcal_b = _data(Range::all(), Range(1,n)); - - //cout<<"for each training data entry"<(this->params.alpha)/m)*gradient; } return theta_p; } -Mat LogisticRegressionImpl::compute_mini_batch_gradient(const Mat& _data, const Mat& _labels, const Mat& _init_theta) +Mat LogisticRegressionImpl::mini_batch_gradient_descent(const Mat& _data, const Mat& _labels, const Mat& _init_theta) { // implements batch gradient descent int lambda_l = 0; - double ccost; - int m, n; + int m; int j = 0; int size_b = this->params.mini_batch_size; @@ -441,11 +443,8 @@ Mat LogisticRegressionImpl::compute_mini_batch_gradient(const Mat& _data, const CV_Error( CV_StsBadArg, "number of iterations cannot be zero or a negative number" ); } - Mat pcal_a; - Mat pcal_b; - Mat pcal_ab; - Mat gradient; Mat theta_p = _init_theta.clone(); + Mat gradient( theta_p.rows, theta_p.cols, theta_p.type() ); Mat data_d; Mat labels_l; @@ -468,46 +467,19 @@ Mat LogisticRegressionImpl::compute_mini_batch_gradient(const Mat& _data, const } m = data_d.rows; - n = data_d.cols; - ccost = compute_cost(data_d, labels_l, theta_p); + // this seems to only be called to ensure that cost is not NaN + compute_cost(data_d, labels_l, theta_p); - if( cvIsNaN( ccost ) == 1) - { - CV_Error( CV_StsBadArg, "check training parameters. Invalid training classifier" ); - } - - pcal_b = calc_sigmoid((data_d*theta_p) - labels_l); - - pcal_a = (static_cast(1/m)) * data_d.t(); - - gradient = pcal_a * pcal_b; - - pcal_a = calc_sigmoid(data_d*theta_p) - labels_l; - - pcal_b = data_d(Range::all(), Range(0,1)); - - multiply(pcal_a, pcal_b, pcal_ab, 1); - - gradient.row(0) = ((float)1/m) * sum(pcal_ab)[0]; - - pcal_b = data_d(Range::all(), Range(1,n)); - - for(int k = 1;k(this->params.alpha)/m)*gradient; - j+=this->params.mini_batch_size; + j += this->params.mini_batch_size; - if(j+size_b>_data.rows) - { - // if parsed through all data variables - break; + // if parsed through all data variables + if (j >= _data.rows) { + j = 0; } } return theta_p; @@ -619,11 +591,6 @@ void LogisticRegressionImpl::read(const FileNode& fn) } } -Mat LogisticRegressionImpl::get_learnt_thetas() const -{ - return this->learnt_thetas; -} - } } diff --git a/modules/ml/src/nbayes.cpp b/modules/ml/src/nbayes.cpp index 221db93e2..733dcf39f 100644 --- a/modules/ml/src/nbayes.cpp +++ b/modules/ml/src/nbayes.cpp @@ -236,7 +236,7 @@ public: if (results_prob) { rptype = results_prob->type(); - rpstep = results_prob->isContinuous() ? 1 : results_prob->step/results_prob->elemSize(); + rpstep = results_prob->isContinuous() ? results_prob->cols : results_prob->step/results_prob->elemSize(); } // allocate memory and initializing headers for calculating cv::AutoBuffer _buffer(nvars*2); diff --git a/modules/ml/src/rtrees.cpp b/modules/ml/src/rtrees.cpp index 4da34992d..1c9120a6d 100644 --- a/modules/ml/src/rtrees.cpp +++ b/modules/ml/src/rtrees.cpp @@ -187,7 +187,7 @@ public: oobidx.clear(); for( i = 0; i < n; i++ ) { - if( !oobmask[i] ) + if( oobmask[i] ) oobidx.push_back(i); } int n_oob = (int)oobidx.size(); @@ -217,6 +217,7 @@ public: else { int ival = cvRound(val); + //Voting scheme to combine OOB errors of each tree int* votes = &oobvotes[j*nclasses]; votes[ival]++; int best_class = 0; @@ -232,38 +233,39 @@ public: oobError /= n_oob; if( rparams.calcVarImportance && n_oob > 1 ) { + Mat sample_clone; oobperm.resize(n_oob); for( i = 0; i < n_oob; i++ ) oobperm[i] = oobidx[i]; + for (i = n_oob - 1; i > 0; --i) //Randomly shuffle indices so we can permute features + { + int r_i = rng.uniform(0, n_oob); + std::swap(oobperm[i], oobperm[r_i]); + } for( vi_ = 0; vi_ < nvars; vi_++ ) { - vi = vidx ? vidx[vi_] : vi_; + vi = vidx ? vidx[vi_] : vi_; //Ensure that only the user specified predictors are used for training double ncorrect_responses_permuted = 0; - for( i = 0; i < n_oob; i++ ) - { - int i1 = rng.uniform(0, n_oob); - int i2 = rng.uniform(0, n_oob); - std::swap(i1, i2); - } for( i = 0; i < n_oob; i++ ) { j = oobidx[i]; int vj = oobperm[i]; sample0 = Mat( nallvars, 1, CV_32F, psamples + sstep0*w->sidx[j], sstep1*sizeof(psamples[0]) ); - for( k = 0; k < nallvars; k++ ) - sample.at(k) = sample0.at(k); - sample.at(vi) = psamples[sstep0*w->sidx[vj] + sstep1*vi]; + sample0.copyTo(sample_clone); //create a copy so we don't mess up the original data + sample_clone.at(vi) = psamples[sstep0*w->sidx[vj] + sstep1*vi]; - double val = predictTrees(Range(treeidx, treeidx+1), sample, predictFlags); + double val = predictTrees(Range(treeidx, treeidx+1), sample_clone, predictFlags); if( !_isClassifier ) { val = (val - w->ord_responses[w->sidx[j]])/max_response; ncorrect_responses_permuted += exp( -val*val ); } else + { ncorrect_responses_permuted += cvRound(val) == w->cat_responses[w->sidx[j]]; + } } varImportance[vi] += (float)(ncorrect_responses - ncorrect_responses_permuted); } diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 402de3f1d..34acebb99 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -1241,6 +1241,12 @@ public: df_alpha.clear(); df_index.clear(); sv.release(); + uncompressed_sv.release(); + } + + Mat getUncompressedSupportVectors_() const + { + return uncompressed_sv; } Mat getSupportVectors() const @@ -1538,6 +1544,7 @@ public: } optimize_linear_svm(); + return true; } @@ -1588,6 +1595,7 @@ public: setRangeVector(df_index, df_count); df_alpha.assign(df_count, 1.); + sv.copyTo(uncompressed_sv); std::swap(sv, new_sv); std::swap(decision_func, new_df); } @@ -1822,8 +1830,8 @@ public: } } - params = best_params; class_labels = class_labels0; + setParams(best_params); return do_train( samples, responses ); } @@ -2056,6 +2064,21 @@ public: } fs << "]"; + if ( !uncompressed_sv.empty() ) + { + // write the joint collection of uncompressed support vectors + int uncompressed_sv_total = uncompressed_sv.rows; + fs << "uncompressed_sv_total" << uncompressed_sv_total; + fs << "uncompressed_support_vectors" << "["; + for( i = 0; i < uncompressed_sv_total; i++ ) + { + fs << "[:"; + fs.writeRaw("f", uncompressed_sv.ptr(i), uncompressed_sv.cols*uncompressed_sv.elemSize()); + fs << "]"; + } + fs << "]"; + } + // write decision functions int df_count = (int)decision_func.size(); @@ -2096,7 +2119,7 @@ public: svm_type_str == "NU_SVR" ? NU_SVR : -1; if( svmType < 0 ) - CV_Error( CV_StsParseError, "Missing of invalid SVM type" ); + CV_Error( CV_StsParseError, "Missing or invalid SVM type" ); FileNode kernel_node = fn["kernel"]; if( kernel_node.empty() ) @@ -2168,14 +2191,31 @@ public: FileNode sv_node = fn["support_vectors"]; CV_Assert((int)sv_node.size() == sv_total); - sv.create(sv_total, var_count, CV_32F); + sv.create(sv_total, var_count, CV_32F); FileNodeIterator sv_it = sv_node.begin(); for( i = 0; i < sv_total; i++, ++sv_it ) { (*sv_it).readRaw("f", sv.ptr(i), var_count*sv.elemSize()); } + int uncompressed_sv_total = (int)fn["uncompressed_sv_total"]; + + if( uncompressed_sv_total > 0 ) + { + // read uncompressed support vectors + FileNode uncompressed_sv_node = fn["uncompressed_support_vectors"]; + + CV_Assert((int)uncompressed_sv_node.size() == uncompressed_sv_total); + uncompressed_sv.create(uncompressed_sv_total, var_count, CV_32F); + + FileNodeIterator uncompressed_sv_it = uncompressed_sv_node.begin(); + for( i = 0; i < uncompressed_sv_total; i++, ++uncompressed_sv_it ) + { + (*uncompressed_sv_it).readRaw("f", uncompressed_sv.ptr(i), var_count*uncompressed_sv.elemSize()); + } + } + // read decision functions int df_count = class_count > 1 ? class_count*(class_count-1)/2 : 1; FileNode df_node = fn["decision_functions"]; @@ -2207,7 +2247,7 @@ public: SvmParams params; Mat class_labels; int var_count; - Mat sv; + Mat sv, uncompressed_sv; vector decision_func; vector df_alpha; vector df_index; @@ -2221,6 +2261,25 @@ Ptr SVM::create() return makePtr(); } +Ptr SVM::load(const String& filepath) +{ + FileStorage fs; + fs.open(filepath, FileStorage::READ); + + Ptr svm = makePtr(); + + ((SVMImpl*)svm.get())->read(fs.getFirstTopLevelNode()); + return svm; +} + +Mat SVM::getUncompressedSupportVectors() const +{ + const SVMImpl* this_ = dynamic_cast(this); + if(!this_) + CV_Error(Error::StsNotImplemented, "the class is not SVMImpl"); + return this_->getUncompressedSupportVectors_(); +} + } } diff --git a/modules/ml/src/testset.cpp b/modules/ml/src/testset.cpp index 8b8bba545..48cd13415 100644 --- a/modules/ml/src/testset.cpp +++ b/modules/ml/src/testset.cpp @@ -104,7 +104,7 @@ void createConcentricSpheresTestSet( int num_samples, int num_features, int num_ max_dst = std::max( max_dst, dis[i].d ); for( ; i < num_samples && dis[i].d <= max_dst; ++i ) - responses.at(i) = cur_class; + responses.at(dis[i].i) = cur_class; } } diff --git a/modules/ml/test/test_mltests.cpp b/modules/ml/test/test_mltests.cpp index 2ffa531ec..70cc0f7ec 100644 --- a/modules/ml/test/test_mltests.cpp +++ b/modules/ml/test/test_mltests.cpp @@ -128,4 +128,48 @@ TEST(ML_Boost, regression) { CV_AMLTest test( CV_BOOST ); test.safe_run(); } TEST(ML_RTrees, regression) { CV_AMLTest test( CV_RTREES ); test.safe_run(); } TEST(DISABLED_ML_ERTrees, regression) { CV_AMLTest test( CV_ERTREES ); test.safe_run(); } +TEST(ML_NBAYES, regression_5911) +{ + int N=12; + Ptr nb = cv::ml::NormalBayesClassifier::create(); + + // data: + Mat_ X(N,4); + X << 1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4, + 5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5, + 4,3,2,1, 4,3,2,1, 4,3,2,1, 4,3,2,1; + + // labels: + Mat_ Y(N,1); + Y << 0,0,0,0, 1,1,1,1, 2,2,2,2; + nb->train(X, ml::ROW_SAMPLE, Y); + + // single prediction: + Mat R1,P1; + for (int i=0; ipredictProb(X.row(i), r, p); + R1.push_back(r); + P1.push_back(p); + } + + // bulk prediction (continuous memory): + Mat R2,P2; + nb->predictProb(X, R2, P2); + + EXPECT_EQ(sum(R1 == R2)[0], 255 * R2.total()); + EXPECT_EQ(sum(P1 == P2)[0], 255 * P2.total()); + + // bulk prediction, with non-continuous memory storage + Mat R3_(N, 1+1, CV_32S), + P3_(N, 3+1, CV_32F); + nb->predictProb(X, R3_.col(0), P3_.colRange(0,3)); + Mat R3 = R3_.col(0).clone(), + P3 = P3_.colRange(0,3).clone(); + + EXPECT_EQ(sum(R1 == R3)[0], 255 * R3.total()); + EXPECT_EQ(sum(P1 == P3)[0], 255 * P3.total()); +} + /* End of file. */ diff --git a/modules/ml/test/test_svmtrainauto.cpp b/modules/ml/test/test_svmtrainauto.cpp index 3c4b72924..13cbe98f4 100644 --- a/modules/ml/test/test_svmtrainauto.cpp +++ b/modules/ml/test/test_svmtrainauto.cpp @@ -118,3 +118,51 @@ TEST(ML_SVM, trainAuto_regression_5369) EXPECT_EQ(0., result0); EXPECT_EQ(1., result1); } + +class CV_SVMGetSupportVectorsTest : public cvtest::BaseTest { +public: + CV_SVMGetSupportVectorsTest() {} +protected: + virtual void run( int startFrom ); +}; +void CV_SVMGetSupportVectorsTest::run(int /*startFrom*/ ) +{ + int code = cvtest::TS::OK; + + // Set up training data + int labels[4] = {1, -1, -1, -1}; + float trainingData[4][2] = { {501, 10}, {255, 10}, {501, 255}, {10, 501} }; + Mat trainingDataMat(4, 2, CV_32FC1, trainingData); + Mat labelsMat(4, 1, CV_32SC1, labels); + + Ptr svm = SVM::create(); + svm->setType(SVM::C_SVC); + svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6)); + + + // Test retrieval of SVs and compressed SVs on linear SVM + svm->setKernel(SVM::LINEAR); + svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat); + + Mat sv = svm->getSupportVectors(); + CV_Assert(sv.rows == 1); // by default compressed SV returned + sv = svm->getUncompressedSupportVectors(); + CV_Assert(sv.rows == 3); + + + // Test retrieval of SVs and compressed SVs on non-linear SVM + svm->setKernel(SVM::POLY); + svm->setDegree(2); + svm->train(trainingDataMat, cv::ml::ROW_SAMPLE, labelsMat); + + sv = svm->getSupportVectors(); + CV_Assert(sv.rows == 3); + sv = svm->getUncompressedSupportVectors(); + CV_Assert(sv.rows == 0); // inapplicable for non-linear SVMs + + + ts->set_failed_test_info(code); +} + + +TEST(ML_SVM, getSupportVectors) { CV_SVMGetSupportVectorsTest test; test.safe_run(); } diff --git a/modules/objdetect/include/opencv2/objdetect.hpp b/modules/objdetect/include/opencv2/objdetect.hpp index bd932e6e6..6587b3d77 100644 --- a/modules/objdetect/include/opencv2/objdetect.hpp +++ b/modules/objdetect/include/opencv2/objdetect.hpp @@ -261,7 +261,7 @@ public: @note - (Python) A face detection example using cascade classifiers can be found at - opencv_source_code/samples/python2/facedetect.py + opencv_source_code/samples/python/facedetect.py */ CV_WRAP void detectMultiScale( InputArray image, CV_OUT std::vector& objects, diff --git a/modules/photo/include/opencv2/photo.hpp b/modules/photo/include/opencv2/photo.hpp index 3d969124e..c093f6552 100644 --- a/modules/photo/include/opencv2/photo.hpp +++ b/modules/photo/include/opencv2/photo.hpp @@ -107,7 +107,7 @@ objects from still images or video. See (0) = response.at(1); } - log(response, response); - CV_Assert(response.rows == LDR_SIZE && response.cols == 1 && - response.channels() == channels); + + Mat log_response; + log(response, log_response); + CV_Assert(log_response.rows == LDR_SIZE && log_response.cols == 1 && + log_response.channels() == channels); Mat exp_values(times); log(exp_values, exp_values); @@ -103,7 +105,7 @@ public: w /= channels; Mat response_img; - LUT(images[i], response, response_img); + LUT(images[i], log_response, response_img); split(response_img, splitted); for(int c = 0; c < channels; c++) { result_split[c] += w.mul(splitted[c] - exp_values.at((int)i)); @@ -194,10 +196,11 @@ public: wellexp = Mat::ones(size, CV_32F); for(int c = 0; c < channels; c++) { - Mat exp = splitted[c] - 0.5f; - pow(exp, 2.0f, exp); - exp = -exp / 0.08f; - wellexp = wellexp.mul(exp); + Mat expo = splitted[c] - 0.5f; + pow(expo, 2.0f, expo); + expo = -expo / 0.08f; + exp(expo, expo); + wellexp = wellexp.mul(expo); } pow(contrast, wcon, contrast); diff --git a/modules/photo/src/tonemap.cpp b/modules/photo/src/tonemap.cpp index e475482d6..1ccc84ea0 100644 --- a/modules/photo/src/tonemap.cpp +++ b/modules/photo/src/tonemap.cpp @@ -510,8 +510,11 @@ protected: void calculateSum(std::vector& x_contrast, std::vector& y_contrast, Mat& sum) { - sum = Mat::zeros(x_contrast[x_contrast.size() - 1].size(), CV_32F); - for(int i = (int)x_contrast.size() - 1; i >= 0; i--) + if (x_contrast.empty()) + return; + const int last = (int)x_contrast.size() - 1; + sum = Mat::zeros(x_contrast[last].size(), CV_32F); + for(int i = last; i >= 0; i--) { Mat grad_x, grad_y; getGradient(x_contrast[i], grad_x, 1); diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index 37673da34..1da5e329d 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -9,7 +9,7 @@ if((WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug") ocv_module_disable(python3) endif() -if(ANDROID OR IOS OR WINRT) +if(ANDROID OR APPLE_FRAMEWORK OR WINRT) ocv_module_disable(python2) ocv_module_disable(python3) endif() diff --git a/modules/python/common.cmake b/modules/python/common.cmake index 2444b77a9..cf74f8dd9 100644 --- a/modules/python/common.cmake +++ b/modules/python/common.cmake @@ -28,6 +28,7 @@ endforeach(m) ocv_list_filterout(opencv_hdrs ".h$") ocv_list_filterout(opencv_hdrs "cuda") ocv_list_filterout(opencv_hdrs "cudev") +ocv_list_filterout(opencv_hdrs "/hal/") ocv_list_filterout(opencv_hdrs "detection_based_tracker.hpp") # Conditional compilation set(cv2_generated_hdrs @@ -46,7 +47,7 @@ add_custom_command( DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txt DEPENDS ${opencv_hdrs}) -ocv_add_library(${the_module} SHARED ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs}) +ocv_add_library(${the_module} MODULE ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs}) if(PYTHON_DEBUG_LIBRARIES AND NOT PYTHON_LIBRARIES MATCHES "optimized.*debug") ocv_target_link_libraries(${the_module} debug ${PYTHON_DEBUG_LIBRARIES} optimized ${PYTHON_LIBRARIES}) @@ -66,6 +67,7 @@ execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; pri set_target_properties(${the_module} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}/${MODULE_INSTALL_SUBDIR}" + ARCHIVE_OUTPUT_NAME ${the_module} # prevent name conflict for python2/3 outputs PREFIX "" OUTPUT_NAME cv2 SUFFIX ${CVPY_SUFFIX}) diff --git a/modules/shape/src/sc_dis.cpp b/modules/shape/src/sc_dis.cpp index 3f11e8b1b..4d1de1281 100644 --- a/modules/shape/src/sc_dis.cpp +++ b/modules/shape/src/sc_dis.cpp @@ -124,10 +124,8 @@ public: virtual void getImages(OutputArray _image1, OutputArray _image2) const { CV_Assert((!image1.empty()) && (!image2.empty())); - _image1.create(image1.size(), image1.type()); - _image2.create(image2.size(), image2.type()); - _image1.getMat()=image1; - _image2.getMat()=image2; + image1.copyTo(_image1); + image2.copyTo(_image2); } virtual void setIterations(int _iterations) {CV_Assert(_iterations>0); iterations=_iterations;} diff --git a/modules/stitching/include/opencv2/stitching/detail/warpers.hpp b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp index ee8e824cb..0cb9e42e8 100644 --- a/modules/stitching/include/opencv2/stitching/detail/warpers.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp @@ -214,7 +214,8 @@ struct CV_EXPORTS SphericalProjector : ProjectorBase /** @brief Warper that maps an image onto the unit sphere located at the origin. - Projects image onto unit sphere with origin at (0, 0, 0). + Projects image onto unit sphere with origin at (0, 0, 0) and radius scale, measured in pixels. + A 360° panorama would therefore have a resulting width of 2 * scale * PI pixels. Poles are located at (0, -1, 0) and (0, 1, 0) points. */ class CV_EXPORTS SphericalWarper : public RotationWarperBase @@ -222,7 +223,8 @@ class CV_EXPORTS SphericalWarper : public RotationWarperBase public: /** @brief Construct an instance of the spherical warper class. - @param scale Projected image scale multiplier + @param scale Radius of the projected sphere, in pixels. An image spanning the + whole sphere will have a width of 2 * scale * PI pixels. */ SphericalWarper(float scale) { projector_.scale = scale; } diff --git a/modules/stitching/src/autocalib.cpp b/modules/stitching/src/autocalib.cpp index 91244bde1..24145242c 100644 --- a/modules/stitching/src/autocalib.cpp +++ b/modules/stitching/src/autocalib.cpp @@ -41,19 +41,19 @@ //M*/ #include "precomp.hpp" +#include "opencv2/core/hal/hal.hpp" using namespace cv; namespace { -template static inline bool -decomposeCholesky(_Tp* A, size_t astep, int m) +static inline bool decomposeCholesky(double* A, size_t astep, int m) { - if (!hal::Cholesky(A, astep, m, 0, 0, 0)) + if (!hal::Cholesky64f(A, astep, m, 0, 0, 0)) return false; astep /= sizeof(A[0]); for (int i = 0; i < m; ++i) - A[i*astep + i] = (_Tp)(1./A[i*astep + i]); + A[i*astep + i] = (double)(1./A[i*astep + i]); return true; } diff --git a/modules/stitching/src/matchers.cpp b/modules/stitching/src/matchers.cpp index 2587da914..40988b988 100644 --- a/modules/stitching/src/matchers.cpp +++ b/modules/stitching/src/matchers.cpp @@ -220,7 +220,11 @@ void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &feat descriptors1_.upload(features1.descriptors); descriptors2_.upload(features2.descriptors); - Ptr matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L2); + //TODO: NORM_L1 allows to avoid matcher crashes for ORB features, but is not absolutely correct for them. + // The best choice for ORB features is NORM_HAMMING, but it is incorrect for SURF features. + // More accurate fix in this place should be done in the future -- the type of the norm + // should be either a parameter of this method, or a field of the class. + Ptr matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L1); MatchesSet matches; diff --git a/modules/stitching/src/seam_finders.cpp b/modules/stitching/src/seam_finders.cpp index fc91135fa..dbed801d5 100644 --- a/modules/stitching/src/seam_finders.cpp +++ b/modules/stitching/src/seam_finders.cpp @@ -115,7 +115,7 @@ void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi) Mat submask2(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U); Size img1 = sizes_[first], img2 = sizes_[second]; - Mat mask1 = masks_[first].getMat(ACCESS_READ), mask2 = masks_[second].getMat(ACCESS_READ); + Mat mask1 = masks_[first].getMat(ACCESS_RW), mask2 = masks_[second].getMat(ACCESS_RW); Point tl1 = corners_[first], tl2 = corners_[second]; // Cut submasks with some gap diff --git a/modules/stitching/src/stitcher.cpp b/modules/stitching/src/stitcher.cpp index c515c192f..41fe81f5f 100644 --- a/modules/stitching/src/stitcher.cpp +++ b/modules/stitching/src/stitcher.cpp @@ -82,6 +82,11 @@ Stitcher Stitcher::createDefault(bool try_use_gpu) stitcher.setExposureCompensator(makePtr()); stitcher.setBlender(makePtr(try_use_gpu)); + stitcher.work_scale_ = 1; + stitcher.seam_scale_ = 1; + stitcher.seam_work_aspect_ = 1; + stitcher.warped_image_scale_ = 1; + return stitcher; } diff --git a/modules/ts/CMakeLists.txt b/modules/ts/CMakeLists.txt index 2732924b7..8d625f8d0 100644 --- a/modules/ts/CMakeLists.txt +++ b/modules/ts/CMakeLists.txt @@ -1,6 +1,6 @@ set(the_description "The ts module") -if(IOS) +if(NOT BUILD_opencv_ts AND NOT BUILD_TESTS AND NOT BUILD_PERF_TESTS) ocv_module_disable(ts) endif() diff --git a/modules/ts/include/opencv2/ts.hpp b/modules/ts/include/opencv2/ts.hpp index 0f3b3149d..5f153f457 100644 --- a/modules/ts/include/opencv2/ts.hpp +++ b/modules/ts/include/opencv2/ts.hpp @@ -35,6 +35,9 @@ # define GTEST_USES_POSIX_RE 0 #endif +#define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > > +#define GET_PARAM(k) std::tr1::get< k >(GetParam()) + #include "opencv2/core.hpp" #include "opencv2/core/utility.hpp" @@ -161,8 +164,11 @@ CV_EXPORTS void compare(const Mat& src1, const Mat& src2, Mat& dst, int cmpop); CV_EXPORTS void compare(const Mat& src, double s, Mat& dst, int cmpop); CV_EXPORTS void gemm(const Mat& src1, const Mat& src2, double alpha, const Mat& src3, double beta, Mat& dst, int flags); - CV_EXPORTS void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift ); +CV_EXPORTS void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift ); CV_EXPORTS double crossCorr(const Mat& src1, const Mat& src2); +CV_EXPORTS void threshold( const Mat& src, Mat& dst, double thresh, double maxval, int thresh_type ); +CV_EXPORTS void minMaxIdx( InputArray _img, double* minVal, double* maxVal, + Point* minLoc, Point* maxLoc, InputArray _mask ); struct CV_EXPORTS MatInfo { diff --git a/modules/ts/include/opencv2/ts/cuda_test.hpp b/modules/ts/include/opencv2/ts/cuda_test.hpp index 8783fb641..64b7eb90c 100644 --- a/modules/ts/include/opencv2/ts/cuda_test.hpp +++ b/modules/ts/include/opencv2/ts/cuda_test.hpp @@ -208,9 +208,6 @@ namespace cvtest } \ void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::UnsafeTestBody() - #define PARAM_TEST_CASE(name, ...) struct name : testing::TestWithParam< std::tr1::tuple< __VA_ARGS__ > > - #define GET_PARAM(k) std::tr1::get< k >(GetParam()) - #define DIFFERENT_SIZES testing::Values(cv::Size(128, 128), cv::Size(113, 113)) // Depth diff --git a/modules/ts/include/opencv2/ts/ocl_test.hpp b/modules/ts/include/opencv2/ts/ocl_test.hpp index 559f4aa32..2dac2110e 100644 --- a/modules/ts/include/opencv2/ts/ocl_test.hpp +++ b/modules/ts/include/opencv2/ts/ocl_test.hpp @@ -324,10 +324,9 @@ struct CV_EXPORTS TSTestWithParam : public TestUtils, public ::testing::TestWith }; +#undef PARAM_TEST_CASE #define PARAM_TEST_CASE(name, ...) struct name : public TSTestWithParam< std::tr1::tuple< __VA_ARGS__ > > -#define GET_PARAM(k) std::tr1::get< k >(GetParam()) - #ifndef IMPLEMENT_PARAM_CLASS #define IMPLEMENT_PARAM_CLASS(name, type) \ class name \ diff --git a/modules/ts/src/ts_func.cpp b/modules/ts/src/ts_func.cpp index b6409ad37..c74065bf0 100644 --- a/modules/ts/src/ts_func.cpp +++ b/modules/ts/src/ts_func.cpp @@ -1,6 +1,7 @@ #include "precomp.hpp" #include #include +#include "opencv2/imgproc/types_c.h" #ifdef HAVE_TEGRA_OPTIMIZATION #include "tegra.hpp" @@ -3074,4 +3075,265 @@ void printVersionInfo(bool useStdOut) #endif } + + +void threshold( const Mat& _src, Mat& _dst, + double thresh, double maxval, int thresh_type ) +{ + int i, j; + int depth = _src.depth(), cn = _src.channels(); + int width_n = _src.cols*cn, height = _src.rows; + int ithresh = cvFloor(thresh); + int imaxval, ithresh2; + + if( depth == CV_8U ) + { + ithresh2 = saturate_cast(ithresh); + imaxval = saturate_cast(maxval); + } + else if( depth == CV_16S ) + { + ithresh2 = saturate_cast(ithresh); + imaxval = saturate_cast(maxval); + } + else + { + ithresh2 = cvRound(ithresh); + imaxval = cvRound(maxval); + } + + assert( depth == CV_8U || depth == CV_16S || depth == CV_32F ); + + switch( thresh_type ) + { + case CV_THRESH_BINARY: + for( i = 0; i < height; i++ ) + { + if( depth == CV_8U ) + { + const uchar* src = _src.ptr(i); + uchar* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0); + } + else if( depth == CV_16S ) + { + const short* src = _src.ptr(i); + short* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (short)(src[j] > ithresh ? imaxval : 0); + } + else + { + const float* src = _src.ptr(i); + float* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (float)((double)src[j] > thresh ? maxval : 0.f); + } + } + break; + case CV_THRESH_BINARY_INV: + for( i = 0; i < height; i++ ) + { + if( depth == CV_8U ) + { + const uchar* src = _src.ptr(i); + uchar* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval); + } + else if( depth == CV_16S ) + { + const short* src = _src.ptr(i); + short* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (short)(src[j] > ithresh ? 0 : imaxval); + } + else + { + const float* src = _src.ptr(i); + float* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = (float)((double)src[j] > thresh ? 0.f : maxval); + } + } + break; + case CV_THRESH_TRUNC: + for( i = 0; i < height; i++ ) + { + if( depth == CV_8U ) + { + const uchar* src = _src.ptr(i); + uchar* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (uchar)(s > ithresh ? ithresh2 : s); + } + } + else if( depth == CV_16S ) + { + const short* src = _src.ptr(i); + short* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (short)(s > ithresh ? ithresh2 : s); + } + } + else + { + const float* src = _src.ptr(i); + float* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + double s = src[j]; + dst[j] = (float)(s > thresh ? thresh : s); + } + } + } + break; + case CV_THRESH_TOZERO: + for( i = 0; i < height; i++ ) + { + if( depth == CV_8U ) + { + const uchar* src = _src.ptr(i); + uchar* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (uchar)(s > ithresh ? s : 0); + } + } + else if( depth == CV_16S ) + { + const short* src = _src.ptr(i); + short* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (short)(s > ithresh ? s : 0); + } + } + else + { + const float* src = _src.ptr(i); + float* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + float s = src[j]; + dst[j] = s > thresh ? s : 0.f; + } + } + } + break; + case CV_THRESH_TOZERO_INV: + for( i = 0; i < height; i++ ) + { + if( depth == CV_8U ) + { + const uchar* src = _src.ptr(i); + uchar* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (uchar)(s > ithresh ? 0 : s); + } + } + else if( depth == CV_16S ) + { + const short* src = _src.ptr(i); + short* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + int s = src[j]; + dst[j] = (short)(s > ithresh ? 0 : s); + } + } + else + { + const float* src = _src.ptr(i); + float* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + float s = src[j]; + dst[j] = s > thresh ? 0.f : s; + } + } + } + break; + default: + assert(0); + } +} + + +static void +_minMaxIdx( const float* src, const uchar* mask, double* _minVal, double* _maxVal, + size_t* _minIdx, size_t* _maxIdx, int len, size_t startIdx ) +{ + double minVal = FLT_MAX, maxVal = -FLT_MAX; + size_t minIdx = 0, maxIdx = 0; + + if( !mask ) + { + for( int i = 0; i < len; i++ ) + { + float val = src[i]; + if( val < minVal ) + { + minVal = val; + minIdx = startIdx + i; + } + if( val > maxVal ) + { + maxVal = val; + maxIdx = startIdx + i; + } + } + } + else + { + for( int i = 0; i < len; i++ ) + { + float val = src[i]; + if( mask[i] && val < minVal ) + { + minVal = val; + minIdx = startIdx + i; + } + if( mask[i] && val > maxVal ) + { + maxVal = val; + maxIdx = startIdx + i; + } + } + } + + if (_minIdx) + *_minIdx = minIdx; + if (_maxIdx) + *_maxIdx = maxIdx; + if (_minVal) + *_minVal = minVal; + if (_maxVal) + *_maxVal = maxVal; +} + + +void minMaxIdx( InputArray _img, double* minVal, double* maxVal, + Point* minLoc, Point* maxLoc, InputArray _mask ) +{ + Mat img = _img.getMat(); + Mat mask = _mask.getMat(); + CV_Assert(img.dims <= 2); + + _minMaxIdx((const float*)img.data, mask.data, minVal, maxVal, (size_t*)minLoc, (size_t*)maxLoc, (int)img.total(),1); + if( minLoc ) + std::swap(minLoc->x, minLoc->y); + if( maxLoc ) + std::swap(maxLoc->x, maxLoc->y); +} + } diff --git a/modules/video/include/opencv2/video/tracking.hpp b/modules/video/include/opencv2/video/tracking.hpp index 217a26a75..d6954fecc 100644 --- a/modules/video/include/opencv2/video/tracking.hpp +++ b/modules/video/include/opencv2/video/tracking.hpp @@ -74,7 +74,7 @@ See the OpenCV sample camshiftdemo.c that tracks colored objects. @note - (Python) A sample explaining the camshift tracking algorithm can be found at - opencv_source_code/samples/python2/camshift.py + opencv_source_code/samples/python/camshift.py */ CV_EXPORTS_W RotatedRect CamShift( InputArray probImage, CV_IN_OUT Rect& window, TermCriteria criteria ); @@ -166,9 +166,9 @@ The function implements a sparse iterative version of the Lucas-Kanade optical f - An example using the Lucas-Kanade optical flow algorithm can be found at opencv_source_code/samples/cpp/lkdemo.cpp - (Python) An example using the Lucas-Kanade optical flow algorithm can be found at - opencv_source_code/samples/python2/lk_track.py + opencv_source_code/samples/python/lk_track.py - (Python) An example using the Lucas-Kanade tracker for homography matching can be found at - opencv_source_code/samples/python2/lk_homography.py + opencv_source_code/samples/python/lk_homography.py */ CV_EXPORTS_W void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg, InputArray prevPts, InputOutputArray nextPts, @@ -213,7 +213,7 @@ The function finds an optical flow for each prev pixel using the @cite Farneback - An example using the optical flow algorithm described by Gunnar Farneback can be found at opencv_source_code/samples/cpp/fback.cpp - (Python) An example using the optical flow algorithm described by Gunnar Farneback can be - found at opencv_source_code/samples/python2/opt_flow.py + found at opencv_source_code/samples/python/opt_flow.py */ CV_EXPORTS_W void calcOpticalFlowFarneback( InputArray prev, InputArray next, InputOutputArray flow, double pyr_scale, int levels, int winsize, diff --git a/modules/video/src/tvl1flow.cpp b/modules/video/src/tvl1flow.cpp index 429f9141c..b10dc3f34 100644 --- a/modules/video/src/tvl1flow.cpp +++ b/modules/video/src/tvl1flow.cpp @@ -1133,18 +1133,22 @@ void EstimateDualVariablesBody::operator() (const Range& range) const { const float g1 = static_cast(hypot(u1xRow[x], u1yRow[x])); const float g2 = static_cast(hypot(u2xRow[x], u2yRow[x])); - const float g3 = static_cast(hypot(u3xRow[x], u3yRow[x])); const float ng1 = 1.0f + taut * g1; const float ng2 = 1.0f + taut * g2; - const float ng3 = 1.0f + taut * g3; p11Row[x] = (p11Row[x] + taut * u1xRow[x]) / ng1; p12Row[x] = (p12Row[x] + taut * u1yRow[x]) / ng1; p21Row[x] = (p21Row[x] + taut * u2xRow[x]) / ng2; p22Row[x] = (p22Row[x] + taut * u2yRow[x]) / ng2; - if (use_gamma) p31Row[x] = (p31Row[x] + taut * u3xRow[x]) / ng3; - if (use_gamma) p32Row[x] = (p32Row[x] + taut * u3yRow[x]) / ng3; + + if (use_gamma) + { + const float g3 = static_cast(hypot(u3xRow[x], u3yRow[x])); + const float ng3 = 1.0f + taut * g3; + p31Row[x] = (p31Row[x] + taut * u3xRow[x]) / ng3; + p32Row[x] = (p32Row[x] + taut * u3yRow[x]) / ng3; + } } } } diff --git a/modules/videoio/CMakeLists.txt b/modules/videoio/CMakeLists.txt index 75851c224..41b0691b7 100644 --- a/modules/videoio/CMakeLists.txt +++ b/modules/videoio/CMakeLists.txt @@ -7,7 +7,7 @@ ocv_add_module(videoio opencv_imgproc opencv_imgcodecs WRAP java python) # Jose Luis Blanco, 2008 # ---------------------------------------------------------------------------- -if(WINRT_8_1) +if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") endif() @@ -38,7 +38,7 @@ list(REMOVE_ITEM videoio_ext_hdrs "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${n # Dependencies used by the implementation referenced # below are not available on WinRT 8.0. # Enabling it for WiRT 8.1+ only. -if(DEFINED WINRT AND NOT DEFINED WINRT_8_0) +if(DEFINED WINRT AND NOT DEFINED WINRT_8_0 AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) # WinRT detected. Adding WinRT API header message(STATUS " ${name}: WinRT detected. Adding WinRT API header") @@ -92,6 +92,10 @@ if(HAVE_DC1394) endif(HAVE_DC1394) if(HAVE_GSTREAMER) + IF(WIN32) + INCLUDE_DIRECTORIES(${GSTREAMER_INCLUDE_DIR}) + list(APPEND VIDEOIO_LIBRARIES ${GSTREAMER_LIBRARIES}) + ENDIF(WIN32) list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_gstreamer.cpp) endif(HAVE_GSTREAMER) @@ -127,10 +131,12 @@ if(HAVE_XIMEA) endif() if(WIN32 AND X86_64) list(APPEND VIDEOIO_LIBRARIES xiapi64) + elseif(WIN32) + list(APPEND VIDEOIO_LIBRARIES xiapi32) elseif(APPLE) list(APPEND VIDEOIO_LIBRARIES "-framework m3api") else() - list(APPEND VIDEOIO_LIBRARIES xiapi32) + list(APPEND VIDEOIO_LIBRARIES m3api) endif() endif(HAVE_XIMEA) @@ -185,7 +191,6 @@ if(HAVE_GPHOTO2) endif(HAVE_GPHOTO2) if(IOS) - add_definitions(-DHAVE_IOS=1) list(APPEND videoio_srcs ${CMAKE_CURRENT_LIST_DIR}/src/cap_ios_abstract_camera.mm ${CMAKE_CURRENT_LIST_DIR}/src/cap_ios_photo_camera.mm @@ -263,6 +268,11 @@ if(WIN32 AND WITH_FFMPEG) endif() install(FILES "${ffmpeg_path}" DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs RENAME "${ffmpeg_bare_name_ver}") + + if(INSTALL_CREATE_DISTRIB) + install(FILES "${OpenCV_SOURCE_DIR}/3rdparty/ffmpeg/opencv_ffmpeg.dll" DESTINATION "bin/" COMPONENT libs RENAME "opencv_ffmpeg${OPENCV_DLLVERSION}.dll") + install(FILES "${OpenCV_SOURCE_DIR}/3rdparty/ffmpeg/opencv_ffmpeg_64.dll" DESTINATION "bin/" COMPONENT libs RENAME "opencv_ffmpeg${OPENCV_DLLVERSION}_64.dll") + endif() endif() endmacro() diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index 503fd5be6..6b3b97b6a 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -254,30 +254,160 @@ enum { CAP_PVAPI_PIXELFORMAT_MONO8 = 1, // Mono8 CAP_PVAPI_PIXELFORMAT_BGRA32 = 8, // Bgra32 }; -// Properties of cameras available through XIMEA SDK interface -enum { CAP_PROP_XI_DOWNSAMPLING = 400, // Change image resolution by binning or skipping. - CAP_PROP_XI_DATA_FORMAT = 401, // Output data format. - CAP_PROP_XI_OFFSET_X = 402, // Horizontal offset from the origin to the area of interest (in pixels). - CAP_PROP_XI_OFFSET_Y = 403, // Vertical offset from the origin to the area of interest (in pixels). - CAP_PROP_XI_TRG_SOURCE = 404, // Defines source of trigger. - CAP_PROP_XI_TRG_SOFTWARE = 405, // Generates an internal trigger. PRM_TRG_SOURCE must be set to TRG_SOFTWARE. - CAP_PROP_XI_GPI_SELECTOR = 406, // Selects general purpose input - CAP_PROP_XI_GPI_MODE = 407, // Set general purpose input mode - CAP_PROP_XI_GPI_LEVEL = 408, // Get general purpose level - CAP_PROP_XI_GPO_SELECTOR = 409, // Selects general purpose output - CAP_PROP_XI_GPO_MODE = 410, // Set general purpose output mode - CAP_PROP_XI_LED_SELECTOR = 411, // Selects camera signalling LED - CAP_PROP_XI_LED_MODE = 412, // Define camera signalling LED functionality - CAP_PROP_XI_MANUAL_WB = 413, // Calculates White Balance(must be called during acquisition) - CAP_PROP_XI_AUTO_WB = 414, // Automatic white balance - CAP_PROP_XI_AEAG = 415, // Automatic exposure/gain - CAP_PROP_XI_EXP_PRIORITY = 416, // Exposure priority (0.5 - exposure 50%, gain 50%). - CAP_PROP_XI_AE_MAX_LIMIT = 417, // Maximum limit of exposure in AEAG procedure - CAP_PROP_XI_AG_MAX_LIMIT = 418, // Maximum limit of gain in AEAG procedure - CAP_PROP_XI_AEAG_LEVEL = 419, // Average intensity of output signal AEAG should achieve(in %) - CAP_PROP_XI_TIMEOUT = 420 // Image capture timeout in milliseconds + // Properties of cameras available through XIMEA SDK interface +enum { CAP_PROP_XI_DOWNSAMPLING = 400, // Change image resolution by binning or skipping. + CAP_PROP_XI_DATA_FORMAT = 401, // Output data format. + CAP_PROP_XI_OFFSET_X = 402, // Horizontal offset from the origin to the area of interest (in pixels). + CAP_PROP_XI_OFFSET_Y = 403, // Vertical offset from the origin to the area of interest (in pixels). + CAP_PROP_XI_TRG_SOURCE = 404, // Defines source of trigger. + CAP_PROP_XI_TRG_SOFTWARE = 405, // Generates an internal trigger. PRM_TRG_SOURCE must be set to TRG_SOFTWARE. + CAP_PROP_XI_GPI_SELECTOR = 406, // Selects general purpose input + CAP_PROP_XI_GPI_MODE = 407, // Set general purpose input mode + CAP_PROP_XI_GPI_LEVEL = 408, // Get general purpose level + CAP_PROP_XI_GPO_SELECTOR = 409, // Selects general purpose output + CAP_PROP_XI_GPO_MODE = 410, // Set general purpose output mode + CAP_PROP_XI_LED_SELECTOR = 411, // Selects camera signalling LED + CAP_PROP_XI_LED_MODE = 412, // Define camera signalling LED functionality + CAP_PROP_XI_MANUAL_WB = 413, // Calculates White Balance(must be called during acquisition) + CAP_PROP_XI_AUTO_WB = 414, // Automatic white balance + CAP_PROP_XI_AEAG = 415, // Automatic exposure/gain + CAP_PROP_XI_EXP_PRIORITY = 416, // Exposure priority (0.5 - exposure 50%, gain 50%). + CAP_PROP_XI_AE_MAX_LIMIT = 417, // Maximum limit of exposure in AEAG procedure + CAP_PROP_XI_AG_MAX_LIMIT = 418, // Maximum limit of gain in AEAG procedure + CAP_PROP_XI_AEAG_LEVEL = 419, // Average intensity of output signal AEAG should achieve(in %) + CAP_PROP_XI_TIMEOUT = 420, // Image capture timeout in milliseconds + CAP_PROP_XI_EXPOSURE = 421, // Exposure time in microseconds + CAP_PROP_XI_EXPOSURE_BURST_COUNT = 422, // Sets the number of times of exposure in one frame. + CAP_PROP_XI_GAIN_SELECTOR = 423, // Gain selector for parameter Gain allows to select different type of gains. + CAP_PROP_XI_GAIN = 424, // Gain in dB + CAP_PROP_XI_DOWNSAMPLING_TYPE = 426, // Change image downsampling type. + CAP_PROP_XI_BINNING_SELECTOR = 427, // Binning engine selector. + CAP_PROP_XI_BINNING_VERTICAL = 428, // Vertical Binning - number of vertical photo-sensitive cells to combine together. + CAP_PROP_XI_BINNING_HORIZONTAL = 429, // Horizontal Binning - number of horizontal photo-sensitive cells to combine together. + CAP_PROP_XI_BINNING_PATTERN = 430, // Binning pattern type. + CAP_PROP_XI_DECIMATION_SELECTOR = 431, // Decimation engine selector. + CAP_PROP_XI_DECIMATION_VERTICAL = 432, // Vertical Decimation - vertical sub-sampling of the image - reduces the vertical resolution of the image by the specified vertical decimation factor. + CAP_PROP_XI_DECIMATION_HORIZONTAL = 433, // Horizontal Decimation - horizontal sub-sampling of the image - reduces the horizontal resolution of the image by the specified vertical decimation factor. + CAP_PROP_XI_DECIMATION_PATTERN = 434, // Decimation pattern type. + CAP_PROP_XI_TEST_PATTERN_GENERATOR_SELECTOR = 587, // Selects which test pattern generator is controlled by the TestPattern feature. + CAP_PROP_XI_TEST_PATTERN = 588, // Selects which test pattern type is generated by the selected generator. + CAP_PROP_XI_IMAGE_DATA_FORMAT = 435, // Output data format. + CAP_PROP_XI_SHUTTER_TYPE = 436, // Change sensor shutter type(CMOS sensor). + CAP_PROP_XI_SENSOR_TAPS = 437, // Number of taps + CAP_PROP_XI_AEAG_ROI_OFFSET_X = 439, // Automatic exposure/gain ROI offset X + CAP_PROP_XI_AEAG_ROI_OFFSET_Y = 440, // Automatic exposure/gain ROI offset Y + CAP_PROP_XI_AEAG_ROI_WIDTH = 441, // Automatic exposure/gain ROI Width + CAP_PROP_XI_AEAG_ROI_HEIGHT = 442, // Automatic exposure/gain ROI Height + CAP_PROP_XI_BPC = 445, // Correction of bad pixels + CAP_PROP_XI_WB_KR = 448, // White balance red coefficient + CAP_PROP_XI_WB_KG = 449, // White balance green coefficient + CAP_PROP_XI_WB_KB = 450, // White balance blue coefficient + CAP_PROP_XI_WIDTH = 451, // Width of the Image provided by the device (in pixels). + CAP_PROP_XI_HEIGHT = 452, // Height of the Image provided by the device (in pixels). + CAP_PROP_XI_REGION_SELECTOR = 589, // Selects Region in Multiple ROI which parameters are set by width, height, ... ,region mode + CAP_PROP_XI_REGION_MODE = 595, // Activates/deactivates Region selected by Region Selector + CAP_PROP_XI_LIMIT_BANDWIDTH = 459, // Set/get bandwidth(datarate)(in Megabits) + CAP_PROP_XI_SENSOR_DATA_BIT_DEPTH = 460, // Sensor output data bit depth. + CAP_PROP_XI_OUTPUT_DATA_BIT_DEPTH = 461, // Device output data bit depth. + CAP_PROP_XI_IMAGE_DATA_BIT_DEPTH = 462, // bitdepth of data returned by function xiGetImage + CAP_PROP_XI_OUTPUT_DATA_PACKING = 463, // Device output data packing (or grouping) enabled. Packing could be enabled if output_data_bit_depth > 8 and packing capability is available. + CAP_PROP_XI_OUTPUT_DATA_PACKING_TYPE = 464, // Data packing type. Some cameras supports only specific packing type. + CAP_PROP_XI_IS_COOLED = 465, // Returns 1 for cameras that support cooling. + CAP_PROP_XI_COOLING = 466, // Start camera cooling. + CAP_PROP_XI_TARGET_TEMP = 467, // Set sensor target temperature for cooling. + CAP_PROP_XI_CHIP_TEMP = 468, // Camera sensor temperature + CAP_PROP_XI_HOUS_TEMP = 469, // Camera housing tepmerature + CAP_PROP_XI_HOUS_BACK_SIDE_TEMP = 590, // Camera housing back side tepmerature + CAP_PROP_XI_SENSOR_BOARD_TEMP = 596, // Camera sensor board temperature + CAP_PROP_XI_CMS = 470, // Mode of color management system. + CAP_PROP_XI_APPLY_CMS = 471, // Enable applying of CMS profiles to xiGetImage (see XI_PRM_INPUT_CMS_PROFILE, XI_PRM_OUTPUT_CMS_PROFILE). + CAP_PROP_XI_IMAGE_IS_COLOR = 474, // Returns 1 for color cameras. + CAP_PROP_XI_COLOR_FILTER_ARRAY = 475, // Returns color filter array type of RAW data. + CAP_PROP_XI_GAMMAY = 476, // Luminosity gamma + CAP_PROP_XI_GAMMAC = 477, // Chromaticity gamma + CAP_PROP_XI_SHARPNESS = 478, // Sharpness Strenght + CAP_PROP_XI_CC_MATRIX_00 = 479, // Color Correction Matrix element [0][0] + CAP_PROP_XI_CC_MATRIX_01 = 480, // Color Correction Matrix element [0][1] + CAP_PROP_XI_CC_MATRIX_02 = 481, // Color Correction Matrix element [0][2] + CAP_PROP_XI_CC_MATRIX_03 = 482, // Color Correction Matrix element [0][3] + CAP_PROP_XI_CC_MATRIX_10 = 483, // Color Correction Matrix element [1][0] + CAP_PROP_XI_CC_MATRIX_11 = 484, // Color Correction Matrix element [1][1] + CAP_PROP_XI_CC_MATRIX_12 = 485, // Color Correction Matrix element [1][2] + CAP_PROP_XI_CC_MATRIX_13 = 486, // Color Correction Matrix element [1][3] + CAP_PROP_XI_CC_MATRIX_20 = 487, // Color Correction Matrix element [2][0] + CAP_PROP_XI_CC_MATRIX_21 = 488, // Color Correction Matrix element [2][1] + CAP_PROP_XI_CC_MATRIX_22 = 489, // Color Correction Matrix element [2][2] + CAP_PROP_XI_CC_MATRIX_23 = 490, // Color Correction Matrix element [2][3] + CAP_PROP_XI_CC_MATRIX_30 = 491, // Color Correction Matrix element [3][0] + CAP_PROP_XI_CC_MATRIX_31 = 492, // Color Correction Matrix element [3][1] + CAP_PROP_XI_CC_MATRIX_32 = 493, // Color Correction Matrix element [3][2] + CAP_PROP_XI_CC_MATRIX_33 = 494, // Color Correction Matrix element [3][3] + CAP_PROP_XI_DEFAULT_CC_MATRIX = 495, // Set default Color Correction Matrix + CAP_PROP_XI_TRG_SELECTOR = 498, // Selects the type of trigger. + CAP_PROP_XI_ACQ_FRAME_BURST_COUNT = 499, // Sets number of frames acquired by burst. This burst is used only if trigger is set to FrameBurstStart + CAP_PROP_XI_DEBOUNCE_EN = 507, // Enable/Disable debounce to selected GPI + CAP_PROP_XI_DEBOUNCE_T0 = 508, // Debounce time (x * 10us) + CAP_PROP_XI_DEBOUNCE_T1 = 509, // Debounce time (x * 10us) + CAP_PROP_XI_DEBOUNCE_POL = 510, // Debounce polarity (pol = 1 t0 - falling edge, t1 - rising edge) + CAP_PROP_XI_LENS_MODE = 511, // Status of lens control interface. This shall be set to XI_ON before any Lens operations. + CAP_PROP_XI_LENS_APERTURE_VALUE = 512, // Current lens aperture value in stops. Examples: 2.8, 4, 5.6, 8, 11 + CAP_PROP_XI_LENS_FOCUS_MOVEMENT_VALUE = 513, // Lens current focus movement value to be used by XI_PRM_LENS_FOCUS_MOVE in motor steps. + CAP_PROP_XI_LENS_FOCUS_MOVE = 514, // Moves lens focus motor by steps set in XI_PRM_LENS_FOCUS_MOVEMENT_VALUE. + CAP_PROP_XI_LENS_FOCUS_DISTANCE = 515, // Lens focus distance in cm. + CAP_PROP_XI_LENS_FOCAL_LENGTH = 516, // Lens focal distance in mm. + CAP_PROP_XI_LENS_FEATURE_SELECTOR = 517, // Selects the current feature which is accessible by XI_PRM_LENS_FEATURE. + CAP_PROP_XI_LENS_FEATURE = 518, // Allows access to lens feature value currently selected by XI_PRM_LENS_FEATURE_SELECTOR. + CAP_PROP_XI_DEVICE_MODEL_ID = 521, // Return device model id + CAP_PROP_XI_DEVICE_SN = 522, // Return device serial number + CAP_PROP_XI_IMAGE_DATA_FORMAT_RGB32_ALPHA = 529, // The alpha channel of RGB32 output image format. + CAP_PROP_XI_IMAGE_PAYLOAD_SIZE = 530, // Buffer size in bytes sufficient for output image returned by xiGetImage + CAP_PROP_XI_TRANSPORT_PIXEL_FORMAT = 531, // Current format of pixels on transport layer. + CAP_PROP_XI_SENSOR_CLOCK_FREQ_HZ = 532, // Sensor clock frequency in Hz. + CAP_PROP_XI_SENSOR_CLOCK_FREQ_INDEX = 533, // Sensor clock frequency index. Sensor with selected frequencies have possibility to set the frequency only by this index. + CAP_PROP_XI_SENSOR_OUTPUT_CHANNEL_COUNT = 534, // Number of output channels from sensor used for data transfer. + CAP_PROP_XI_FRAMERATE = 535, // Define framerate in Hz + CAP_PROP_XI_COUNTER_SELECTOR = 536, // Select counter + CAP_PROP_XI_COUNTER_VALUE = 537, // Counter status + CAP_PROP_XI_ACQ_TIMING_MODE = 538, // Type of sensor frames timing. + CAP_PROP_XI_AVAILABLE_BANDWIDTH = 539, // Calculate and return available interface bandwidth(int Megabits) + CAP_PROP_XI_BUFFER_POLICY = 540, // Data move policy + CAP_PROP_XI_LUT_EN = 541, // Activates LUT. + CAP_PROP_XI_LUT_INDEX = 542, // Control the index (offset) of the coefficient to access in the LUT. + CAP_PROP_XI_LUT_VALUE = 543, // Value at entry LUTIndex of the LUT + CAP_PROP_XI_TRG_DELAY = 544, // Specifies the delay in microseconds (us) to apply after the trigger reception before activating it. + CAP_PROP_XI_TS_RST_MODE = 545, // Defines how time stamp reset engine will be armed + CAP_PROP_XI_TS_RST_SOURCE = 546, // Defines which source will be used for timestamp reset. Writing this parameter will trigger settings of engine (arming) + CAP_PROP_XI_IS_DEVICE_EXIST = 547, // Returns 1 if camera connected and works properly. + CAP_PROP_XI_ACQ_BUFFER_SIZE = 548, // Acquisition buffer size in buffer_size_unit. Default bytes. + CAP_PROP_XI_ACQ_BUFFER_SIZE_UNIT = 549, // Acquisition buffer size unit in bytes. Default 1. E.g. Value 1024 means that buffer_size is in KiBytes + CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_SIZE = 550, // Acquisition transport buffer size in bytes + CAP_PROP_XI_BUFFERS_QUEUE_SIZE = 551, // Queue of field/frame buffers + CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_COMMIT = 552, // Number of buffers to commit to low level + CAP_PROP_XI_RECENT_FRAME = 553, // GetImage returns most recent frame + CAP_PROP_XI_DEVICE_RESET = 554, // Resets the camera to default state. + CAP_PROP_XI_COLUMN_FPN_CORRECTION = 555, // Correction of column FPN + CAP_PROP_XI_ROW_FPN_CORRECTION = 591, // Correction of row FPN + CAP_PROP_XI_SENSOR_MODE = 558, // Current sensor mode. Allows to select sensor mode by one integer. Setting of this parameter affects: image dimensions and downsampling. + CAP_PROP_XI_HDR = 559, // Enable High Dynamic Range feature. + CAP_PROP_XI_HDR_KNEEPOINT_COUNT = 560, // The number of kneepoints in the PWLR. + CAP_PROP_XI_HDR_T1 = 561, // position of first kneepoint(in % of XI_PRM_EXPOSURE) + CAP_PROP_XI_HDR_T2 = 562, // position of second kneepoint (in % of XI_PRM_EXPOSURE) + CAP_PROP_XI_KNEEPOINT1 = 563, // value of first kneepoint (% of sensor saturation) + CAP_PROP_XI_KNEEPOINT2 = 564, // value of second kneepoint (% of sensor saturation) + CAP_PROP_XI_IMAGE_BLACK_LEVEL = 565, // Last image black level counts. Can be used for Offline processing to recall it. + CAP_PROP_XI_HW_REVISION = 571, // Returns hardware revision number. + CAP_PROP_XI_DEBUG_LEVEL = 572, // Set debug level + CAP_PROP_XI_AUTO_BANDWIDTH_CALCULATION = 573, // Automatic bandwidth calculation, + CAP_PROP_XI_FFS_FILE_ID = 594, // File number. + CAP_PROP_XI_FFS_FILE_SIZE = 580, // Size of file. + CAP_PROP_XI_FREE_FFS_SIZE = 581, // Size of free camera FFS. + CAP_PROP_XI_USED_FFS_SIZE = 582, // Size of used camera FFS. + CAP_PROP_XI_FFS_ACCESS_KEY = 583, // Setting of key enables file operations on some cameras. + CAP_PROP_XI_SENSOR_FEATURE_SELECTOR = 585, // Selects the current feature which is accessible by XI_PRM_SENSOR_FEATURE_VALUE. + CAP_PROP_XI_SENSOR_FEATURE_VALUE = 586, // Allows access to sensor feature value currently selected by XI_PRM_SENSOR_FEATURE_SELECTOR. }; + // Properties of cameras available through AVFOUNDATION interface enum { CAP_PROP_IOS_DEVICE_FOCUS = 9001, CAP_PROP_IOS_DEVICE_EXPOSURE = 9002, @@ -380,11 +510,11 @@ class can be used: : - Another basic video processing sample can be found at opencv_source_code/samples/cpp/video_dmtx.cpp - (Python) A basic sample on using the VideoCapture interface can be found at - opencv_source_code/samples/python2/video.py + opencv_source_code/samples/python/video.py - (Python) Another basic video processing sample can be found at - opencv_source_code/samples/python2/video_dmtx.py + opencv_source_code/samples/python/video_dmtx.py - (Python) A multi threaded video processing sample can be found at - opencv_source_code/samples/python2/video_threaded.py + opencv_source_code/samples/python/video_threaded.py */ class CV_EXPORTS_W VideoCapture { diff --git a/modules/videoio/include/opencv2/videoio/cap_winrt.hpp b/modules/videoio/include/opencv2/videoio/cap_winrt.hpp index 470f4da25..7fe04bc93 100644 --- a/modules/videoio/include/opencv2/videoio/cap_winrt.hpp +++ b/modules/videoio/include/opencv2/videoio/cap_winrt.hpp @@ -28,8 +28,7 @@ #include #include #include - -using namespace Windows::UI::Xaml::Controls; +#include "opencv2/core/cvdef.h" namespace cv { @@ -130,4 +129,4 @@ CV_EXPORTS void winrt_imshow(); //! @} videoio_winrt -} // cv \ No newline at end of file +} // cv diff --git a/modules/videoio/include/opencv2/videoio/videoio_c.h b/modules/videoio/include/opencv2/videoio/videoio_c.h index 47f46fa76..91d26ea5a 100644 --- a/modules/videoio/include/opencv2/videoio/videoio_c.h +++ b/modules/videoio/include/opencv2/videoio/videoio_c.h @@ -274,6 +274,8 @@ enum CV_CAP_PROP_XI_DECIMATION_VERTICAL = 432, // Vertical Decimation - vertical sub-sampling of the image - reduces the vertical resolution of the image by the specified vertical decimation factor. CV_CAP_PROP_XI_DECIMATION_HORIZONTAL = 433, // Horizontal Decimation - horizontal sub-sampling of the image - reduces the horizontal resolution of the image by the specified vertical decimation factor. CV_CAP_PROP_XI_DECIMATION_PATTERN = 434, // Decimation pattern type. + CV_CAP_PROP_XI_TEST_PATTERN_GENERATOR_SELECTOR = 587, // Selects which test pattern generator is controlled by the TestPattern feature. + CV_CAP_PROP_XI_TEST_PATTERN = 588, // Selects which test pattern type is generated by the selected generator. CV_CAP_PROP_XI_IMAGE_DATA_FORMAT = 435, // Output data format. CV_CAP_PROP_XI_SHUTTER_TYPE = 436, // Change sensor shutter type(CMOS sensor). CV_CAP_PROP_XI_SENSOR_TAPS = 437, // Number of taps @@ -287,6 +289,8 @@ enum CV_CAP_PROP_XI_WB_KB = 450, // White balance blue coefficient CV_CAP_PROP_XI_WIDTH = 451, // Width of the Image provided by the device (in pixels). CV_CAP_PROP_XI_HEIGHT = 452, // Height of the Image provided by the device (in pixels). + CV_CAP_PROP_XI_REGION_SELECTOR = 589, // Selects Region in Multiple ROI which parameters are set by width, height, ... ,region mode + CV_CAP_PROP_XI_REGION_MODE = 595, // Activates/deactivates Region selected by Region Selector CV_CAP_PROP_XI_LIMIT_BANDWIDTH = 459, // Set/get bandwidth(datarate)(in Megabits) CV_CAP_PROP_XI_SENSOR_DATA_BIT_DEPTH = 460, // Sensor output data bit depth. CV_CAP_PROP_XI_OUTPUT_DATA_BIT_DEPTH = 461, // Device output data bit depth. @@ -298,6 +302,8 @@ enum CV_CAP_PROP_XI_TARGET_TEMP = 467, // Set sensor target temperature for cooling. CV_CAP_PROP_XI_CHIP_TEMP = 468, // Camera sensor temperature CV_CAP_PROP_XI_HOUS_TEMP = 469, // Camera housing tepmerature + CV_CAP_PROP_XI_HOUS_BACK_SIDE_TEMP = 590, // Camera housing back side tepmerature + CV_CAP_PROP_XI_SENSOR_BOARD_TEMP = 596, // Camera sensor board temperature CV_CAP_PROP_XI_CMS = 470, // Mode of color management system. CV_CAP_PROP_XI_APPLY_CMS = 471, // Enable applying of CMS profiles to xiGetImage (see XI_PRM_INPUT_CMS_PROFILE, XI_PRM_OUTPUT_CMS_PROFILE). CV_CAP_PROP_XI_IMAGE_IS_COLOR = 474, // Returns 1 for color cameras. @@ -365,6 +371,7 @@ enum CV_CAP_PROP_XI_RECENT_FRAME = 553, // GetImage returns most recent frame CV_CAP_PROP_XI_DEVICE_RESET = 554, // Resets the camera to default state. CV_CAP_PROP_XI_COLUMN_FPN_CORRECTION = 555, // Correction of column FPN + CV_CAP_PROP_XI_ROW_FPN_CORRECTION = 591, // Correction of row FPN CV_CAP_PROP_XI_SENSOR_MODE = 558, // Current sensor mode. Allows to select sensor mode by one integer. Setting of this parameter affects: image dimensions and downsampling. CV_CAP_PROP_XI_HDR = 559, // Enable High Dynamic Range feature. CV_CAP_PROP_XI_HDR_KNEEPOINT_COUNT = 560, // The number of kneepoints in the PWLR. @@ -376,6 +383,8 @@ enum CV_CAP_PROP_XI_HW_REVISION = 571, // Returns hardware revision number. CV_CAP_PROP_XI_DEBUG_LEVEL = 572, // Set debug level CV_CAP_PROP_XI_AUTO_BANDWIDTH_CALCULATION = 573, // Automatic bandwidth calculation, + CV_CAP_PROP_XI_FFS_FILE_ID = 594, // File number. + CV_CAP_PROP_XI_FFS_FILE_SIZE = 580, // Size of file. CV_CAP_PROP_XI_FREE_FFS_SIZE = 581, // Size of free camera FFS. CV_CAP_PROP_XI_USED_FFS_SIZE = 582, // Size of used camera FFS. CV_CAP_PROP_XI_FFS_ACCESS_KEY = 583, // Setting of key enables file operations on some cameras. diff --git a/modules/videoio/src/agile_wrl.hpp b/modules/videoio/src/agile_wrl.hpp deleted file mode 100644 index 99fbf4185..000000000 --- a/modules/videoio/src/agile_wrl.hpp +++ /dev/null @@ -1,568 +0,0 @@ -// -// Copyright (C) Microsoft Corporation -// All rights reserved. -// Modified for native C++ WRL support by Gregory Morse -// -// Code in Details namespace is for internal usage within the library code -// - -#ifndef _PLATFORM_AGILE_H_ -#define _PLATFORM_AGILE_H_ - -#ifdef _MSC_VER -#pragma once -#endif // _MSC_VER - -#include -#include - -template class Agile; - -template -struct UnwrapAgile -{ - static const bool _IsAgile = false; -}; -template -struct UnwrapAgile> -{ - static const bool _IsAgile = true; -}; -template -struct UnwrapAgile> -{ - static const bool _IsAgile = true; -}; - -#define IS_AGILE(T) UnwrapAgile::_IsAgile - -#define __is_winrt_agile(T) (std::is_same::value || std::is_base_of::value || std::is_base_of::value) //derived from Microsoft::WRL::FtmBase or IAgileObject - -#define __is_win_interface(T) (std::is_base_of::value || std::is_base_of::value) //derived from IUnknown or IInspectable - -#define __is_win_class(T) (std::is_same::value || std::is_base_of::value) //derived from Microsoft::WRL::RuntimeClass or HSTRING - - namespace Details - { - IUnknown* __stdcall GetObjectContext(); - HRESULT __stdcall GetProxyImpl(IUnknown*, REFIID, IUnknown*, IUnknown**); - HRESULT __stdcall ReleaseInContextImpl(IUnknown*, IUnknown*); - - template -#if _MSC_VER >= 1800 - __declspec(no_refcount) inline HRESULT GetProxy(T *ObjectIn, IUnknown *ContextCallBack, T **Proxy) -#else - inline HRESULT GetProxy(T *ObjectIn, IUnknown *ContextCallBack, T **Proxy) -#endif - { -#if _MSC_VER >= 1800 - return GetProxyImpl(*reinterpret_cast(&ObjectIn), __uuidof(T*), ContextCallBack, reinterpret_cast(Proxy)); -#else - return GetProxyImpl(*reinterpret_cast(&const_cast(ObjectIn)), __uuidof(T*), ContextCallBack, reinterpret_cast(Proxy)); -#endif - } - - template - inline HRESULT ReleaseInContext(T *ObjectIn, IUnknown *ContextCallBack) - { - return ReleaseInContextImpl(ObjectIn, ContextCallBack); - } - - template - class AgileHelper - { - __abi_IUnknown* _p; - bool _release; - public: - AgileHelper(__abi_IUnknown* p, bool release = true) : _p(p), _release(release) - { - } - AgileHelper(AgileHelper&& other) : _p(other._p), _release(other._release) - { - _other._p = nullptr; - _other._release = true; - } - AgileHelper operator=(AgileHelper&& other) - { - _p = other._p; - _release = other._release; - _other._p = nullptr; - _other._release = true; - return *this; - } - - ~AgileHelper() - { - if (_release && _p) - { - _p->__abi_Release(); - } - } - - __declspec(no_refcount) __declspec(no_release_return) - T* operator->() - { - return reinterpret_cast(_p); - } - - __declspec(no_refcount) __declspec(no_release_return) - operator T * () - { - return reinterpret_cast(_p); - } - private: - AgileHelper(const AgileHelper&); - AgileHelper operator=(const AgileHelper&); - }; - template - struct __remove_hat - { - typedef T type; - }; - template - struct __remove_hat - { - typedef T type; - }; - template - struct AgileTypeHelper - { - typename typedef __remove_hat::type type; - typename typedef __remove_hat::type* agileMemberType; - }; - } // namespace Details - -#pragma warning(push) -#pragma warning(disable: 4451) // Usage of ref class inside this context can lead to invalid marshaling of object across contexts - - template < - typename T, - bool TIsNotAgile = (__is_win_class(typename Details::AgileTypeHelper::type) && !__is_winrt_agile(typename Details::AgileTypeHelper::type)) || - __is_win_interface(typename Details::AgileTypeHelper::type) - > - class Agile - { - static_assert(__is_win_class(typename Details::AgileTypeHelper::type) || __is_win_interface(typename Details::AgileTypeHelper::type), "Agile can only be used with ref class or interface class types"); - typename typedef Details::AgileTypeHelper::agileMemberType TypeT; - TypeT _object; - ::Microsoft::WRL::ComPtr _contextCallback; - ULONG_PTR _contextToken; - -#if _MSC_VER >= 1800 - enum class AgileState - { - NonAgilePointer = 0, - AgilePointer = 1, - Unknown = 2 - }; - AgileState _agileState; -#endif - - void CaptureContext() - { - _contextCallback = Details::GetObjectContext(); - __abi_ThrowIfFailed(CoGetContextToken(&_contextToken)); - } - - void SetObject(TypeT object) - { - // Capture context before setting the pointer - // If context capture fails then nothing to cleanup - Release(); - if (object != nullptr) - { - ::Microsoft::WRL::ComPtr checkIfAgile; - HRESULT hr = reinterpret_cast(object)->QueryInterface(__uuidof(IAgileObject), &checkIfAgile); - // Don't Capture context if object is agile - if (hr != S_OK) - { -#if _MSC_VER >= 1800 - _agileState = AgileState::NonAgilePointer; -#endif - CaptureContext(); - } -#if _MSC_VER >= 1800 - else - { - _agileState = AgileState::AgilePointer; - } -#endif - } - _object = object; - } - - public: - Agile() throw() : _object(nullptr), _contextToken(0) -#if _MSC_VER >= 1800 - , _agileState(AgileState::Unknown) -#endif - { - } - - Agile(nullptr_t) throw() : _object(nullptr), _contextToken(0) -#if _MSC_VER >= 1800 - , _agileState(AgileState::Unknown) -#endif - { - } - - explicit Agile(TypeT object) throw() : _object(nullptr), _contextToken(0) -#if _MSC_VER >= 1800 - , _agileState(AgileState::Unknown) -#endif - { - // Assumes that the source object is from the current context - SetObject(object); - } - - Agile(const Agile& object) throw() : _object(nullptr), _contextToken(0) -#if _MSC_VER >= 1800 - , _agileState(AgileState::Unknown) -#endif - { - // Get returns pointer valid for current context - SetObject(object.Get()); - } - - Agile(Agile&& object) throw() : _object(nullptr), _contextToken(0) -#if _MSC_VER >= 1800 - , _agileState(AgileState::Unknown) -#endif - { - // Assumes that the source object is from the current context - Swap(object); - } - - ~Agile() throw() - { - Release(); - } - - TypeT Get() const - { - // Agile object, no proxy required -#if _MSC_VER >= 1800 - if (_agileState == AgileState::AgilePointer || _object == nullptr) -#else - if (_contextToken == 0 || _contextCallback == nullptr || _object == nullptr) -#endif - { - return _object; - } - - // Do the check for same context - ULONG_PTR currentContextToken; - __abi_ThrowIfFailed(CoGetContextToken(¤tContextToken)); - if (currentContextToken == _contextToken) - { - return _object; - } - -#if _MSC_VER >= 1800 - // Different context and holding on to a non agile object - // Do the costly work of getting a proxy - TypeT localObject; - __abi_ThrowIfFailed(Details::GetProxy(_object, _contextCallback.Get(), &localObject)); - - if (_agileState == AgileState::Unknown) -#else - // Object is agile if it implements IAgileObject - // GetAddressOf captures the context with out knowing the type of object that it will hold - if (_object != nullptr) -#endif - { -#if _MSC_VER >= 1800 - // Object is agile if it implements IAgileObject - // GetAddressOf captures the context with out knowing the type of object that it will hold - ::Microsoft::WRL::ComPtr checkIfAgile; - HRESULT hr = reinterpret_cast(localObject)->QueryInterface(__uuidof(IAgileObject), &checkIfAgile); -#else - ::Microsoft::WRL::ComPtr checkIfAgile; - HRESULT hr = reinterpret_cast(_object)->QueryInterface(__uuidof(IAgileObject), &checkIfAgile); -#endif - if (hr == S_OK) - { - auto pThis = const_cast(this); -#if _MSC_VER >= 1800 - pThis->_agileState = AgileState::AgilePointer; -#endif - pThis->_contextToken = 0; - pThis->_contextCallback = nullptr; - return _object; - } -#if _MSC_VER >= 1800 - else - { - auto pThis = const_cast(this); - pThis->_agileState = AgileState::NonAgilePointer; - } -#endif - } - -#if _MSC_VER < 1800 - // Different context and holding on to a non agile object - // Do the costly work of getting a proxy - TypeT localObject; - __abi_ThrowIfFailed(Details::GetProxy(_object, _contextCallback.Get(), &localObject)); -#endif - return localObject; - } - - TypeT* GetAddressOf() throw() - { - Release(); - CaptureContext(); - return &_object; - } - - TypeT* GetAddressOfForInOut() throw() - { - CaptureContext(); - return &_object; - } - - TypeT operator->() const throw() - { - return Get(); - } - - Agile& operator=(nullptr_t) throw() - { - Release(); - return *this; - } - - Agile& operator=(TypeT object) throw() - { - Agile(object).Swap(*this); - return *this; - } - - Agile& operator=(Agile object) throw() - { - // parameter is by copy which gets pointer valid for current context - object.Swap(*this); - return *this; - } - -#if _MSC_VER < 1800 - Agile& operator=(IUnknown* lp) throw() - { - // bump ref count - ::Microsoft::WRL::ComPtr spObject(lp); - - // put it into Platform Object - Platform::Object object; - *(IUnknown**)(&object) = spObject.Detach(); - - SetObject(object); - return *this; - } -#endif - - void Swap(Agile& object) - { - std::swap(_object, object._object); - std::swap(_contextCallback, object._contextCallback); - std::swap(_contextToken, object._contextToken); -#if _MSC_VER >= 1800 - std::swap(_agileState, object._agileState); -#endif - } - - // Release the interface and set to NULL - void Release() throw() - { - if (_object) - { - // Cast to IInspectable (no QI) - IUnknown* pObject = *(IUnknown**)(&_object); - // Set * to null without release - *(IUnknown**)(&_object) = nullptr; - - ULONG_PTR currentContextToken; - __abi_ThrowIfFailed(CoGetContextToken(¤tContextToken)); - if (_contextToken == 0 || _contextCallback == nullptr || _contextToken == currentContextToken) - { - pObject->Release(); - } - else - { - Details::ReleaseInContext(pObject, _contextCallback.Get()); - } - _contextCallback = nullptr; - _contextToken = 0; -#if _MSC_VER >= 1800 - _agileState = AgileState::Unknown; -#endif - } - } - - bool operator==(nullptr_t) const throw() - { - return _object == nullptr; - } - - bool operator==(const Agile& other) const throw() - { - return _object == other._object && _contextToken == other._contextToken; - } - - bool operator<(const Agile& other) const throw() - { - if (reinterpret_cast(_object) < reinterpret_cast(other._object)) - { - return true; - } - - return _object == other._object && _contextToken < other._contextToken; - } - }; - - template - class Agile - { - static_assert(__is_win_class(typename Details::AgileTypeHelper::type) || __is_win_interface(typename Details::AgileTypeHelper::type), "Agile can only be used with ref class or interface class types"); - typename typedef Details::AgileTypeHelper::agileMemberType TypeT; - TypeT _object; - - public: - Agile() throw() : _object(nullptr) - { - } - - Agile(nullptr_t) throw() : _object(nullptr) - { - } - - explicit Agile(TypeT object) throw() : _object(object) - { - } - - Agile(const Agile& object) throw() : _object(object._object) - { - } - - Agile(Agile&& object) throw() : _object(nullptr) - { - Swap(object); - } - - ~Agile() throw() - { - Release(); - } - - TypeT Get() const - { - return _object; - } - - TypeT* GetAddressOf() throw() - { - Release(); - return &_object; - } - - TypeT* GetAddressOfForInOut() throw() - { - return &_object; - } - - TypeT operator->() const throw() - { - return Get(); - } - - Agile& operator=(nullptr_t) throw() - { - Release(); - return *this; - } - - Agile& operator=(TypeT object) throw() - { - if (_object != object) - { - _object = object; - } - return *this; - } - - Agile& operator=(Agile object) throw() - { - object.Swap(*this); - return *this; - } - -#if _MSC_VER < 1800 - Agile& operator=(IUnknown* lp) throw() - { - Release(); - // bump ref count - ::Microsoft::WRL::ComPtr spObject(lp); - - // put it into Platform Object - Platform::Object object; - *(IUnknown**)(&object) = spObject.Detach(); - - _object = object; - return *this; - } -#endif - - // Release the interface and set to NULL - void Release() throw() - { - _object = nullptr; - } - - void Swap(Agile& object) - { - std::swap(_object, object._object); - } - - bool operator==(nullptr_t) const throw() - { - return _object == nullptr; - } - - bool operator==(const Agile& other) const throw() - { - return _object == other._object; - } - - bool operator<(const Agile& other) const throw() - { - return reinterpret_cast(_object) < reinterpret_cast(other._object); - } - }; - -#pragma warning(pop) - - template - bool operator==(nullptr_t, const Agile& a) throw() - { - return a == nullptr; - } - - template - bool operator!=(const Agile& a, nullptr_t) throw() - { - return !(a == nullptr); - } - - template - bool operator!=(nullptr_t, const Agile& a) throw() - { - return !(a == nullptr); - } - - template - bool operator!=(const Agile& a, const Agile& b) throw() - { - return !(a == b); - } - - -#endif // _PLATFORM_AGILE_H_ diff --git a/modules/videoio/src/cap.cpp b/modules/videoio/src/cap.cpp index c1623f1cc..3211badea 100644 --- a/modules/videoio/src/cap.cpp +++ b/modules/videoio/src/cap.cpp @@ -44,7 +44,7 @@ #include "cap_dshow.hpp" // All WinRT versions older than 8.0 should provide classes used for video support -#if defined(WINRT) && !defined(WINRT_8_0) +#if defined(WINRT) && !defined(WINRT_8_0) && defined(__cplusplus_winrt) # include "cap_winrt_capture.hpp" # include "cap_winrt_bridge.hpp" # define WINRT_VIDEO @@ -166,10 +166,12 @@ CV_IMPL CvCapture * cvCreateCameraCapture (int index) #ifdef HAVE_GSTREAMER if (!capture) - capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0); + capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, + reinterpret_cast(index)); if (!capture) - capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0); + capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, + reinterpret_cast(index)); #endif if (pref) break; // CV_CAP_VFW diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp index cf8a1c668..504561d1e 100644 --- a/modules/videoio/src/cap_ffmpeg_impl.hpp +++ b/modules/videoio/src/cap_ffmpeg_impl.hpp @@ -129,12 +129,22 @@ extern "C" { #if defined WIN32 || defined _WIN32 #include + #if defined _MSC_VER && _MSC_VER < 1900 + struct timespec + { + time_t tv_sec; + long tv_nsec; + }; + #endif #elif defined __linux__ || defined __APPLE__ #include #include #include + #include #if defined __APPLE__ #include + #include + #include #endif #endif @@ -174,6 +184,141 @@ extern "C" { #define AV_PIX_FMT_GRAY16BE PIX_FMT_GRAY16BE #endif +#if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 \ + ? CALC_FFMPEG_VERSION(52, 38, 100) : CALC_FFMPEG_VERSION(52, 13, 0)) +#define USE_AV_FRAME_GET_BUFFER 1 +#else +#define USE_AV_FRAME_GET_BUFFER 0 +#ifndef AV_NUM_DATA_POINTERS // required for 0.7.x/0.8.x ffmpeg releases +#define AV_NUM_DATA_POINTERS 4 +#endif +#endif + + +#ifndef USE_AV_INTERRUPT_CALLBACK +#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 21, 0) +#define USE_AV_INTERRUPT_CALLBACK 1 +#else +#define USE_AV_INTERRUPT_CALLBACK 0 +#endif +#endif + +#if USE_AV_INTERRUPT_CALLBACK +#define LIBAVFORMAT_INTERRUPT_TIMEOUT_MS 30000 + +#ifdef WIN32 +// http://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows + +static +inline LARGE_INTEGER get_filetime_offset() +{ + SYSTEMTIME s; + FILETIME f; + LARGE_INTEGER t; + + s.wYear = 1970; + s.wMonth = 1; + s.wDay = 1; + s.wHour = 0; + s.wMinute = 0; + s.wSecond = 0; + s.wMilliseconds = 0; + SystemTimeToFileTime(&s, &f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + return t; +} + +static +inline void get_monotonic_time(timespec *tv) +{ + LARGE_INTEGER t; + FILETIME f; + double microseconds; + static LARGE_INTEGER offset; + static double frequencyToMicroseconds; + static int initialized = 0; + static BOOL usePerformanceCounter = 0; + + if (!initialized) + { + LARGE_INTEGER performanceFrequency; + initialized = 1; + usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency); + if (usePerformanceCounter) + { + QueryPerformanceCounter(&offset); + frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.; + } + else + { + offset = get_filetime_offset(); + frequencyToMicroseconds = 10.; + } + } + + if (usePerformanceCounter) + { + QueryPerformanceCounter(&t); + } else { + GetSystemTimeAsFileTime(&f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + } + + t.QuadPart -= offset.QuadPart; + microseconds = (double)t.QuadPart / frequencyToMicroseconds; + t.QuadPart = microseconds; + tv->tv_sec = t.QuadPart / 1000000; + tv->tv_nsec = (t.QuadPart % 1000000) * 1000; +} +#else +static +inline void get_monotonic_time(timespec *time) +{ +#if defined(__APPLE__) && defined(__MACH__) + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + time->tv_sec = mts.tv_sec; + time->tv_nsec = mts.tv_nsec; +#else + clock_gettime(CLOCK_MONOTONIC, time); +#endif +} +#endif + +static +inline timespec get_monotonic_time_diff(timespec start, timespec end) +{ + timespec temp; + if (end.tv_nsec - start.tv_nsec < 0) + { + temp.tv_sec = end.tv_sec - start.tv_sec - 1; + temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; + } + else + { + temp.tv_sec = end.tv_sec - start.tv_sec; + temp.tv_nsec = end.tv_nsec - start.tv_nsec; + } + return temp; +} + +static +inline double get_monotonic_time_diff_ms(timespec time1, timespec time2) +{ + timespec delta = get_monotonic_time_diff(time1, time2); + double milliseconds = delta.tv_sec * 1000 + (double)delta.tv_nsec / 1000000.0; + + return milliseconds; +} +#endif // USE_AV_INTERRUPT_CALLBACK + static int get_number_of_cpus(void) { #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(52, 111, 0) @@ -223,12 +368,36 @@ struct Image_FFMPEG }; +#if USE_AV_INTERRUPT_CALLBACK +struct AVInterruptCallbackMetadata +{ + timespec value; + unsigned int timeout_after_ms; + int timeout; +}; + +static inline void _opencv_ffmpeg_free(void** ptr) { if(*ptr) free(*ptr); *ptr = 0; } +static +inline int _opencv_ffmpeg_interrupt_callback(void *ptr) +{ + AVInterruptCallbackMetadata* metadata = (AVInterruptCallbackMetadata*)ptr; + assert(metadata); + + timespec now; + get_monotonic_time(&now); + + metadata->timeout = get_monotonic_time_diff_ms(metadata->value, now) > metadata->timeout_after_ms; + + return metadata->timeout ? -1 : 0; +} +#endif + struct CvCapture_FFMPEG { @@ -283,6 +452,9 @@ struct CvCapture_FFMPEG #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0) AVDictionary *dict; #endif +#if USE_AV_INTERRUPT_CALLBACK + AVInterruptCallbackMetadata interrupt_metadata; +#endif }; void CvCapture_FFMPEG::init() @@ -354,11 +526,15 @@ void CvCapture_FFMPEG::close() ic = NULL; } +#if USE_AV_FRAME_GET_BUFFER + av_frame_unref(&rgb_picture); +#else if( rgb_picture.data[0] ) { free( rgb_picture.data[0] ); rgb_picture.data[0] = 0; } +#endif // free last packet if exist if (packet.data) { @@ -577,6 +753,16 @@ bool CvCapture_FFMPEG::open( const char* _filename ) close(); +#if USE_AV_INTERRUPT_CALLBACK + /* interrupt callback */ + interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_TIMEOUT_MS; + get_monotonic_time(&interrupt_metadata.value); + + ic = avformat_alloc_context(); + ic->interrupt_callback.callback = _opencv_ffmpeg_interrupt_callback; + ic->interrupt_callback.opaque = &interrupt_metadata; +#endif + #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0) av_dict_set(&dict, "rtsp_transport", "tcp", 0); int err = avformat_open_input(&ic, _filename, NULL, &dict); @@ -648,17 +834,11 @@ bool CvCapture_FFMPEG::open( const char* _filename ) picture = avcodec_alloc_frame(); #endif - rgb_picture.data[0] = (uint8_t*)malloc( - avpicture_get_size( AV_PIX_FMT_BGR24, - enc->width, enc->height )); - avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0], - AV_PIX_FMT_BGR24, enc->width, enc->height ); - frame.width = enc->width; frame.height = enc->height; frame.cn = 3; - frame.step = rgb_picture.linesize[0]; - frame.data = rgb_picture.data[0]; + frame.step = 0; + frame.data = NULL; break; } } @@ -695,6 +875,15 @@ bool CvCapture_FFMPEG::grabFrame() { av_free_packet (&packet); + +#if USE_AV_INTERRUPT_CALLBACK + if (interrupt_metadata.timeout) + { + valid = false; + break; + } +#endif + int ret = av_read_frame(ic, &packet); if (ret == AVERROR(EAGAIN)) continue; @@ -730,6 +919,11 @@ bool CvCapture_FFMPEG::grabFrame() picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts; frame_number++; valid = true; + +#if USE_AV_INTERRUPT_CALLBACK + // update interrupt value + get_monotonic_time(&interrupt_metadata.value); +#endif } else { @@ -754,19 +948,18 @@ bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* if( img_convert_ctx == NULL || frame.width != video_st->codec->width || - frame.height != video_st->codec->height ) + frame.height != video_st->codec->height || + frame.data == NULL ) { - if( img_convert_ctx ) - sws_freeContext(img_convert_ctx); - - frame.width = video_st->codec->width; - frame.height = video_st->codec->height; + // Some sws_scale optimizations have some assumptions about alignment of data/step/width/height + // Also we use coded_width/height to workaround problem with legacy ffmpeg versions (like n0.8) + int buffer_width = video_st->codec->coded_width, buffer_height = video_st->codec->coded_height; img_convert_ctx = sws_getCachedContext( - NULL, - video_st->codec->width, video_st->codec->height, + img_convert_ctx, + buffer_width, buffer_height, video_st->codec->pix_fmt, - video_st->codec->width, video_st->codec->height, + buffer_width, buffer_height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL @@ -775,21 +968,37 @@ bool CvCapture_FFMPEG::retrieveFrame(int, unsigned char** data, int* step, int* if (img_convert_ctx == NULL) return false;//CV_Error(0, "Cannot initialize the conversion context!"); +#if USE_AV_FRAME_GET_BUFFER + av_frame_unref(&rgb_picture); + rgb_picture.format = AV_PIX_FMT_BGR24; + rgb_picture.width = buffer_width; + rgb_picture.height = buffer_height; + if (0 != av_frame_get_buffer(&rgb_picture, 32)) + { + CV_WARN("OutOfMemory"); + return false; + } +#else + int aligns[AV_NUM_DATA_POINTERS]; + avcodec_align_dimensions2(video_st->codec, &buffer_width, &buffer_height, aligns); rgb_picture.data[0] = (uint8_t*)realloc(rgb_picture.data[0], avpicture_get_size( AV_PIX_FMT_BGR24, - video_st->codec->width, video_st->codec->height )); + buffer_width, buffer_height )); + avpicture_fill( (AVPicture*)&rgb_picture, rgb_picture.data[0], + AV_PIX_FMT_BGR24, buffer_width, buffer_height ); +#endif + frame.width = video_st->codec->width; + frame.height = video_st->codec->height; + frame.cn = 3; frame.data = rgb_picture.data[0]; + frame.step = rgb_picture.linesize[0]; } - avpicture_fill((AVPicture*)&rgb_picture, rgb_picture.data[0], AV_PIX_FMT_RGB24, - video_st->codec->width, video_st->codec->height); - frame.step = rgb_picture.linesize[0]; - sws_scale( img_convert_ctx, picture->data, picture->linesize, - 0, video_st->codec->height, + 0, video_st->codec->coded_height, rgb_picture.data, rgb_picture.linesize ); @@ -2300,6 +2509,10 @@ private: AVFormatContext* ctx_; int video_stream_id_; AVPacket pkt_; + +#if USE_AV_INTERRUPT_CALLBACK + AVInterruptCallbackMetadata interrupt_metadata; +#endif }; bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma_format, int* width, int* height) @@ -2310,6 +2523,16 @@ bool InputMediaStream_FFMPEG::open(const char* fileName, int* codec, int* chroma video_stream_id_ = -1; memset(&pkt_, 0, sizeof(AVPacket)); +#if USE_AV_INTERRUPT_CALLBACK + /* interrupt callback */ + interrupt_metadata.timeout_after_ms = LIBAVFORMAT_INTERRUPT_TIMEOUT_MS; + get_monotonic_time(&interrupt_metadata.value); + + ctx_ = avformat_alloc_context(); + ctx_->interrupt_callback.callback = _opencv_ffmpeg_interrupt_callback; + ctx_->interrupt_callback.opaque = &interrupt_metadata; +#endif + #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 13, 0) avformat_network_init(); #endif @@ -2426,11 +2649,23 @@ bool InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFi // get the next frame for (;;) { +#if USE_AV_INTERRUPT_CALLBACK + if(interrupt_metadata.timeout) + { + break; + } +#endif + int ret = av_read_frame(ctx_, &pkt_); if (ret == AVERROR(EAGAIN)) continue; +#if USE_AV_INTERRUPT_CALLBACK + // update interrupt value + get_monotonic_time(&interrupt_metadata.value); +#endif + if (ret < 0) { if (ret == (int)AVERROR_EOF) diff --git a/modules/videoio/src/cap_gstreamer.cpp b/modules/videoio/src/cap_gstreamer.cpp index 4740126c0..41754eaa0 100644 --- a/modules/videoio/src/cap_gstreamer.cpp +++ b/modules/videoio/src/cap_gstreamer.cpp @@ -48,7 +48,9 @@ * \brief Use GStreamer to read/write video */ #include "precomp.hpp" +#ifndef _MSC_VER #include +#endif #include #include #include @@ -75,10 +77,21 @@ #if GST_VERSION_MAJOR == 0 #define COLOR_ELEM "ffmpegcolorspace" +#define COLOR_ELEM_NAME "ffmpegcsp" #elif FULL_GST_VERSION < VERSION_NUM(1,5,0) #define COLOR_ELEM "videoconvert" +#define COLOR_ELEM_NAME COLOR_ELEM #else #define COLOR_ELEM "autovideoconvert" +#define COLOR_ELEM_NAME COLOR_ELEM +#endif + +#if defined(_WIN32) || defined(_WIN64) +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#define strcasecmp _stricmp +#define strncasecmp _strnicmp +#include #endif void toFraction(double decimal, double &numerator, double &denominator); @@ -142,6 +155,7 @@ protected: gpointer data); GstElement* pipeline; GstElement* uridecodebin; + GstElement* v4l2src; GstElement* color; GstElement* sink; #if GST_VERSION_MAJOR > 0 @@ -164,6 +178,7 @@ void CvCapture_GStreamer::init() { pipeline = NULL; uridecodebin = NULL; + v4l2src = NULL; color = NULL; sink = NULL; #if GST_VERSION_MAJOR > 0 @@ -368,9 +383,7 @@ void CvCapture_GStreamer::startPipeline() if (status == GST_STATE_CHANGE_ASYNC) { // wait for status update - GstState st1; - GstState st2; - status = gst_element_get_state(pipeline, &st1, &st2, GST_CLOCK_TIME_NONE); + status = gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE); } if (status == GST_STATE_CHANGE_FAILURE) { @@ -587,9 +600,20 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) // else, we might have a file or a manual pipeline. // if gstreamer cannot parse the manual pipeline, we assume we were given and // ordinary file path. - if(!gst_uri_is_valid(filename)) + if (!gst_uri_is_valid(filename)) { +#ifdef _MSC_VER + uri = new char[2048]; + DWORD pathSize = GetFullPathName(filename, 2048, uri, NULL); + struct stat buf; + if (pathSize == 0 || stat(uri, &buf) != 0) + { + delete uri; + uri = NULL; + } +#else uri = realpath(filename, NULL); +#endif stream = false; if(uri) { @@ -619,7 +643,9 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) stream = true; manualpipeline = true; } - } else { + } + else + { stream = true; uri = g_strdup(filename); } @@ -640,68 +666,86 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) uridecodebin = gst_element_make_from_uri(GST_URI_SRC, uri, "src", NULL); #endif element_from_uri = true; - }else{ + } + else + { uridecodebin = gst_element_factory_make("uridecodebin", NULL); g_object_set(G_OBJECT(uridecodebin), "uri", uri, NULL); } g_free(protocol); - if(!uridecodebin) { + if(!uridecodebin) + { //fprintf(stderr, "GStreamer: Error opening bin: %s\n", err->message); close(); return false; } } - if(manualpipeline) + if (manualpipeline) { - GstIterator *it = NULL; -#if GST_VERSION_MAJOR == 0 - it = gst_bin_iterate_sinks(GST_BIN(uridecodebin)); - if(gst_iterator_next(it, (gpointer *)&sink) != GST_ITERATOR_OK) { - CV_ERROR(CV_StsError, "GStreamer: cannot find appsink in manual pipeline\n"); - return false; - } -#else - it = gst_bin_iterate_sinks (GST_BIN(uridecodebin)); + GstIterator *it = gst_bin_iterate_elements(GST_BIN(uridecodebin)); - gboolean done = FALSE; GstElement *element = NULL; + gboolean done = false; gchar* name = NULL; +#if GST_VERSION_MAJOR > 0 GValue value = G_VALUE_INIT; +#endif - while (!done) { - switch (gst_iterator_next (it, &value)) { + while (!done) + { +#if GST_VERSION_MAJOR > 0 + switch (gst_iterator_next (it, &value)) + { case GST_ITERATOR_OK: - element = GST_ELEMENT (g_value_get_object (&value)); - name = gst_element_get_name(element); - if (name){ - if(strstr(name, "opencvsink") != NULL || strstr(name, "appsink") != NULL) { - sink = GST_ELEMENT ( gst_object_ref (element) ); - done = TRUE; - } - g_free(name); - } - g_value_unset (&value); + element = GST_ELEMENT (g_value_get_object (&value)); +#else + switch (gst_iterator_next (it, (gpointer *)&element)) + { + case GST_ITERATOR_OK: +#endif + name = gst_element_get_name(element); + if (name) + { + if (strstr(name, "opencvsink") != NULL || strstr(name, "appsink") != NULL) + { + sink = GST_ELEMENT ( gst_object_ref (element) ); + } + else if (strstr(name, COLOR_ELEM_NAME) != NULL) + { + color = GST_ELEMENT ( gst_object_ref (element) ); + } + else if (strstr(name, "v4l") != NULL) + { + v4l2src = GST_ELEMENT ( gst_object_ref (element) ); + } + g_free(name); - break; + done = sink && color && v4l2src; + } +#if GST_VERSION_MAJOR > 0 + g_value_unset (&value); +#endif + + break; case GST_ITERATOR_RESYNC: - gst_iterator_resync (it); - break; + gst_iterator_resync (it); + break; case GST_ITERATOR_ERROR: case GST_ITERATOR_DONE: - done = TRUE; - break; - } + done = TRUE; + break; + } } gst_iterator_free (it); - - if (!sink){ + if (!sink) + { CV_ERROR(CV_StsError, "GStreamer: cannot find appsink in manual pipeline\n"); return false; } -#endif + pipeline = uridecodebin; } else @@ -714,18 +758,23 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) gst_bin_add_many(GST_BIN(pipeline), uridecodebin, color, sink, NULL); - if(element_from_uri) { - if(!gst_element_link(uridecodebin, color)) { + if(element_from_uri) + { + if(!gst_element_link(uridecodebin, color)) + { CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n"); gst_object_unref(pipeline); pipeline = NULL; return false; } - }else{ + } + else + { g_signal_connect(uridecodebin, "pad-added", G_CALLBACK(newPad), color); } - if(!gst_element_link(color, sink)) { + if(!gst_element_link(color, sink)) + { CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n"); gst_object_unref(pipeline); pipeline = NULL; @@ -753,16 +802,13 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) gst_app_sink_set_caps(GST_APP_SINK(sink), caps); gst_caps_unref(caps); - // For video files only: set pipeline to PAUSED state to get its duration - if (file) { - status = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PAUSED); + status = gst_element_set_state(GST_ELEMENT(pipeline), + file ? GST_STATE_PAUSED : GST_STATE_PLAYING); if (status == GST_STATE_CHANGE_ASYNC) { // wait for status update - GstState st1; - GstState st2; - status = gst_element_get_state(pipeline, &st1, &st2, GST_CLOCK_TIME_NONE); + status = gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE); } if (status == GST_STATE_CHANGE_FAILURE) { @@ -813,14 +859,9 @@ bool CvCapture_GStreamer::open( int type, const char* filename ) fps = (double)num/(double)denom; - // GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline"); - } - else - { - duration = -1; - width = -1; - height = -1; - fps = -1; + // GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline") + + stopPipeline(); } __END__; @@ -851,7 +892,7 @@ double CvCapture_GStreamer::getProperty( int propId ) const if(!pipeline) { CV_WARN("GStreamer: no pipeline"); - return false; + return 0; } switch(propId) { @@ -860,7 +901,7 @@ double CvCapture_GStreamer::getProperty( int propId ) const status = gst_element_query_position(sink, FORMAT, &value); if(!status) { CV_WARN("GStreamer: unable to query position of stream"); - return false; + return 0; } return value * 1e-6; // nano seconds to milli seconds case CV_CAP_PROP_POS_FRAMES: @@ -868,7 +909,7 @@ double CvCapture_GStreamer::getProperty( int propId ) const status = gst_element_query_position(sink, FORMAT, &value); if(!status) { CV_WARN("GStreamer: unable to query position of stream"); - return false; + return 0; } return value; case CV_CAP_PROP_POS_AVI_RATIO: @@ -876,7 +917,7 @@ double CvCapture_GStreamer::getProperty( int propId ) const status = gst_element_query_position(sink, FORMAT, &value); if(!status) { CV_WARN("GStreamer: unable to query position of stream"); - return false; + return 0; } return ((double) value) / GST_FORMAT_PERCENT_MAX; case CV_CAP_PROP_FRAME_WIDTH: @@ -895,6 +936,21 @@ double CvCapture_GStreamer::getProperty( int propId ) const case CV_CAP_PROP_CONTRAST: case CV_CAP_PROP_SATURATION: case CV_CAP_PROP_HUE: + if (v4l2src) + { + const gchar * propName = + propId == CV_CAP_PROP_BRIGHTNESS ? "brightness" : + propId == CV_CAP_PROP_CONTRAST ? "contrast" : + propId == CV_CAP_PROP_SATURATION ? "saturation" : + propId == CV_CAP_PROP_HUE ? "hue" : NULL; + + if (propName) + { + gint32 value32 = 0; + g_object_get(G_OBJECT(v4l2src), propName, &value32, NULL); + return value32; + } + } case CV_CAP_PROP_GAIN: case CV_CAP_PROP_CONVERT_RGB: break; @@ -911,7 +967,7 @@ double CvCapture_GStreamer::getProperty( int propId ) const #undef FORMAT - return false; + return 0; } /*! @@ -990,6 +1046,21 @@ bool CvCapture_GStreamer::setProperty( int propId, double value ) case CV_CAP_PROP_CONTRAST: case CV_CAP_PROP_SATURATION: case CV_CAP_PROP_HUE: + if (v4l2src) + { + const gchar * propName = + propId == CV_CAP_PROP_BRIGHTNESS ? "brightness" : + propId == CV_CAP_PROP_CONTRAST ? "contrast" : + propId == CV_CAP_PROP_SATURATION ? "saturation" : + propId == CV_CAP_PROP_HUE ? "hue" : NULL; + + if (propName) + { + gint32 value32 = cv::saturate_cast(value); + g_object_set(G_OBJECT(v4l2src), propName, &value32, NULL); + return true; + } + } case CV_CAP_PROP_GAIN: case CV_CAP_PROP_CONVERT_RGB: break; diff --git a/modules/videoio/src/cap_images.cpp b/modules/videoio/src/cap_images.cpp index 253261adc..00d5af47d 100644 --- a/modules/videoio/src/cap_images.cpp +++ b/modules/videoio/src/cap_images.cpp @@ -67,10 +67,11 @@ class CvCapture_Images : public CvCapture public: CvCapture_Images() { - filename = 0; + filename = NULL; currentframe = firstframe = 0; length = 0; - frame = 0; + frame = NULL; + grabbedInOpen = false; } virtual ~CvCapture_Images() @@ -92,6 +93,7 @@ protected: unsigned length; // length of sequence IplImage* frame; + bool grabbedInOpen; }; @@ -100,7 +102,7 @@ void CvCapture_Images::close() if( filename ) { free(filename); - filename = 0; + filename = NULL; } currentframe = firstframe = 0; length = 0; @@ -113,17 +115,25 @@ bool CvCapture_Images::grabFrame() char str[_MAX_PATH]; sprintf(str, filename, firstframe + currentframe); + if (grabbedInOpen) + { + grabbedInOpen = false; + ++currentframe; + + return frame != NULL; + } + cvReleaseImage(&frame); frame = cvLoadImage(str, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if( frame ) currentframe++; - return frame != 0; + return frame != NULL; } IplImage* CvCapture_Images::retrieveFrame(int) { - return frame; + return grabbedInOpen ? NULL : frame; } double CvCapture_Images::getProperty(int id) const @@ -168,6 +178,8 @@ bool CvCapture_Images::setProperty(int id, double value) value = length - 1; } currentframe = cvRound(value); + if (currentframe != 0) + grabbedInOpen = false; // grabbed frame is not valid anymore return true; case CV_CAP_PROP_POS_AVI_RATIO: if(value > 1) { @@ -178,6 +190,8 @@ bool CvCapture_Images::setProperty(int id, double value) value = 0; } currentframe = cvRound((length - 1) * value); + if (currentframe != 0) + grabbedInOpen = false; // grabbed frame is not valid anymore return true; } CV_WARN("unknown/unhandled property\n"); @@ -280,7 +294,13 @@ bool CvCapture_Images::open(const char * _filename) } firstframe = offset; - return true; + + // grab frame to enable properties retrieval + bool grabRes = grabFrame(); + grabbedInOpen = true; + currentframe = 0; + + return grabRes; } @@ -292,7 +312,7 @@ CvCapture* cvCreateFileCapture_Images(const char * filename) return capture; delete capture; - return 0; + return NULL; } // diff --git a/modules/videoio/src/cap_mjpeg_decoder.cpp b/modules/videoio/src/cap_mjpeg_decoder.cpp index 77e5d2122..7abce2004 100644 --- a/modules/videoio/src/cap_mjpeg_decoder.cpp +++ b/modules/videoio/src/cap_mjpeg_decoder.cpp @@ -790,7 +790,7 @@ std::vector MotionJpegCapture::readFrame(frame_iterator it) result.reserve(chunk.m_size); result.resize(chunk.m_size); - m_file_stream.read(result.data(), chunk.m_size); + m_file_stream.read(&(result[0]), chunk.m_size); // result.data() failed with MSVS2008 return result; } diff --git a/modules/videoio/src/cap_msmf.cpp b/modules/videoio/src/cap_msmf.cpp index 19f40ca52..0694a7502 100644 --- a/modules/videoio/src/cap_msmf.cpp +++ b/modules/videoio/src/cap_msmf.cpp @@ -103,163 +103,9 @@ #ifdef HAVE_CONCURRENCY #include #ifndef __cplusplus_winrt -__declspec(noreturn) void __stdcall __abi_WinRTraiseException(long); - -inline void __abi_ThrowIfFailed(long __hrArg) -{ - if (__hrArg < 0) - { - __abi_WinRTraiseException(__hrArg); - } -} - -struct Guid -{ -public: - Guid(); - Guid(__rcGUID_t); - operator ::__rcGUID_t(); - bool Equals(Guid __guidArg); - bool Equals(__rcGUID_t __guidArg); - Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, unsigned __int8 __dArg, - unsigned __int8 __eArg, unsigned __int8 __fArg, unsigned __int8 __gArg, unsigned __int8 __hArg, - unsigned __int8 __iArg, unsigned __int8 __jArg, unsigned __int8 __kArg); - Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, const unsigned __int8* __dArg); -private: - unsigned long __a; - unsigned short __b; - unsigned short __c; - unsigned char __d; - unsigned char __e; - unsigned char __f; - unsigned char __g; - unsigned char __h; - unsigned char __i; - unsigned char __j; - unsigned char __k; -}; - -static_assert(sizeof(Guid) == sizeof(::_GUID), "Incorect size for Guid"); -static_assert(sizeof(__rcGUID_t) == sizeof(::_GUID), "Incorect size for __rcGUID_t"); - -//////////////////////////////////////////////////////////////////////////////// -inline Guid::Guid() : __a(0), __b(0), __c(0), __d(0), __e(0), __f(0), __g(0), __h(0), __i(0), __j(0), __k(0) -{ -} - -inline Guid::Guid(__rcGUID_t __guid) : -__a(reinterpret_cast(__guid).Data1), -__b(reinterpret_cast(__guid).Data2), -__c(reinterpret_cast(__guid).Data3), -__d(reinterpret_cast(__guid).Data4[0]), -__e(reinterpret_cast(__guid).Data4[1]), -__f(reinterpret_cast(__guid).Data4[2]), -__g(reinterpret_cast(__guid).Data4[3]), -__h(reinterpret_cast(__guid).Data4[4]), -__i(reinterpret_cast(__guid).Data4[5]), -__j(reinterpret_cast(__guid).Data4[6]), -__k(reinterpret_cast(__guid).Data4[7]) -{ -} - -inline Guid::operator ::__rcGUID_t() -{ - return reinterpret_cast<__rcGUID_t>(*this); -} - -inline bool Guid::Equals(Guid __guidArg) -{ - return *this == __guidArg; -} - -inline bool Guid::Equals(__rcGUID_t __guidArg) -{ - return *this == static_cast< Guid>(__guidArg); -} - -inline bool operator==(Guid __aArg, Guid __bArg) -{ - auto __a = reinterpret_cast(&__aArg); - auto __b = reinterpret_cast(&__bArg); - - return (__a[0] == __b[0] && __a[1] == __b[1] && __a[2] == __b[2] && __a[3] == __b[3]); -} - -inline bool operator!=(Guid __aArg, Guid __bArg) -{ - return !(__aArg == __bArg); -} - -inline bool operator<(Guid __aArg, Guid __bArg) -{ - auto __a = reinterpret_cast(&__aArg); - auto __b = reinterpret_cast(&__bArg); - - if (__a[0] != __b[0]) - { - return __a[0] < __b[0]; - } - - if (__a[1] != __b[1]) - { - return __a[1] < __b[1]; - } - - if (__a[2] != __b[2]) - { - return __a[2] < __b[2]; - } - - if (__a[3] != __b[3]) - { - return __a[3] < __b[3]; - } - - return false; -} - -inline Guid::Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, unsigned __int8 __dArg, - unsigned __int8 __eArg, unsigned __int8 __fArg, unsigned __int8 __gArg, unsigned __int8 __hArg, - unsigned __int8 __iArg, unsigned __int8 __jArg, unsigned __int8 __kArg) : - __a(__aArg), __b(__bArg), __c(__cArg), __d(__dArg), __e(__eArg), __f(__fArg), __g(__gArg), __h(__hArg), __i(__iArg), __j(__jArg), __k(__kArg) -{ -} - -inline Guid::Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, const unsigned __int8 __dArg[8]) : -__a(__aArg), __b(__bArg), __c(__cArg) -{ - __d = __dArg[0]; - __e = __dArg[1]; - __f = __dArg[2]; - __g = __dArg[3]; - __h = __dArg[4]; - __i = __dArg[5]; - __j = __dArg[6]; - __k = __dArg[7]; -} - -__declspec(selectany) Guid __winrt_GUID_NULL(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); - -// -//// Don't want to define the real IUnknown from unknown.h here. That would means if the user has -//// any broken code that uses it, compile errors will take the form of e.g.: -//// predefined C++ WinRT types (compiler internal)(41) : see declaration of 'IUnknown::QueryInterface' -//// This is not helpful. If they use IUnknown, we still need to point them to the actual unknown.h so -//// that they can see the original definition. -//// -//// For WinRT, we'll instead have a parallel COM interface hierarchy for basic interfaces starting with _. -//// The type mismatch is not an issue. COM passes types through GUID / void* combos - the original type -//// doesn't come into play unless the user static_casts an implementation type to one of these, but -//// the WinRT implementation types are hidden. -__interface __declspec(uuid("00000000-0000-0000-C000-000000000046")) __abi_IUnknown -{ -public: - virtual long __stdcall __abi_QueryInterface(Guid&, void**) = 0; - virtual unsigned long __stdcall __abi_AddRef() = 0; - virtual unsigned long __stdcall __abi_Release() = 0; -}; +#include "wrl.h #endif -#include "ppltasks_winrt.h" +#include "ppltasks_winrt.hpp" #endif #else #include diff --git a/modules/videoio/src/cap_msmf.hpp b/modules/videoio/src/cap_msmf.hpp index 0987c704f..87a747668 100644 --- a/modules/videoio/src/cap_msmf.hpp +++ b/modules/videoio/src/cap_msmf.hpp @@ -1,205 +1,7 @@ #ifdef WINRT #define ICustomStreamSink StreamSink #ifndef __cplusplus_winrt - -#define __is_winrt_array(type) (type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt8Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int16Array ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt16Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int32Array ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt32Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int64Array ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt64Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_SingleArray ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_DoubleArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_Char16Array ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_BooleanArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_StringArray ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_InspectableArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_DateTimeArray ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_TimeSpanArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_GuidArray ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_PointArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_SizeArray ||\ - type == ABI::Windows::Foundation::PropertyType::PropertyType_RectArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_OtherTypeArray) - -template::value> -struct winrt_type -{ -}; -template -struct winrt_type<_Type, true> -{ - static IUnknown* create(_Type* _ObjInCtx) { - return reinterpret_cast(_ObjInCtx); - } - static IID getuuid() { return __uuidof(_Type); } - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherType; -}; -template -struct winrt_type<_Type, false> -{ - static IUnknown* create(_Type* _ObjInCtx) { - Microsoft::WRL::ComPtr _PObj; - Microsoft::WRL::ComPtr objFactory; - HRESULT hr = Windows::Foundation::GetActivationFactory(Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), objFactory.ReleaseAndGetAddressOf()); - if (FAILED(hr)) return nullptr; - Microsoft::WRL::ComPtr spPropVal; - if (SUCCEEDED(hr)) - hr = objFactory.As(&spPropVal); - if (SUCCEEDED(hr)) { - hr = winrt_type<_Type>::create(spPropVal.Get(), _ObjInCtx, _PObj.GetAddressOf()); - if (SUCCEEDED(hr)) - return reinterpret_cast(_PObj.Detach()); - } - return nullptr; - } - static IID getuuid() { return __uuidof(ABI::Windows::Foundation::IPropertyValue); } - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherType; -}; - -template<> -struct winrt_type -{ - static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, void* _ObjInCtx, IInspectable** ppInsp) { - (void)_ObjInCtx; - return spPropVal->CreateEmpty(ppInsp); - } - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_Empty; -}; -#define MAKE_TYPE(Type, Name) template<>\ -struct winrt_type\ -{\ - static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, Type* _ObjInCtx, IInspectable** ppInsp) {\ - return spPropVal->Create##Name(*_ObjInCtx, ppInsp);\ -}\ - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_##Name;\ -}; - -template -struct winrt_array_type -{ - static IUnknown* create(_Type* _ObjInCtx, size_t N) { - Microsoft::WRL::ComPtr _PObj; - Microsoft::WRL::ComPtr objFactory; - HRESULT hr = Windows::Foundation::GetActivationFactory(Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), objFactory.ReleaseAndGetAddressOf()); - if (FAILED(hr)) return nullptr; - Microsoft::WRL::ComPtr spPropVal; - if (SUCCEEDED(hr)) - hr = objFactory.As(&spPropVal); - if (SUCCEEDED(hr)) { - hr = winrt_array_type<_Type>::create(spPropVal.Get(), N, _ObjInCtx, _PObj.GetAddressOf()); - if (SUCCEEDED(hr)) - return reinterpret_cast(_PObj.Detach()); - } - return nullptr; - } - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherTypeArray; -}; -template -struct winrt_prop_type {}; - -template <> -struct winrt_prop_type { - typedef void _Type; -}; - -template <> -struct winrt_prop_type { - typedef void _Type; -}; - -template <> -struct winrt_prop_type { - typedef void _Type; -}; - -#define MAKE_PROP(Prop, Type) template <>\ -struct winrt_prop_type {\ - typedef Type _Type;\ -}; - -#define MAKE_ARRAY_TYPE(Type, Name) MAKE_PROP(Name, Type)\ - MAKE_PROP(Name##Array, Type*)\ - MAKE_TYPE(Type, Name)\ - template<>\ -struct winrt_array_type\ -{\ - static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, UINT32 __valueSize, Type** _ObjInCtx, IInspectable** ppInsp) {\ - return spPropVal->Create##Name##Array(__valueSize, *_ObjInCtx, ppInsp);\ -}\ - static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_##Name##Array;\ - static std::vector PropertyValueToVector(ABI::Windows::Foundation::IPropertyValue* propValue)\ -{\ - UINT32 uLen = 0;\ - Type* pArray = nullptr;\ - propValue->Get##Name##Array(&uLen, &pArray);\ - return std::vector(pArray, pArray + uLen);\ -}\ -}; -MAKE_ARRAY_TYPE(BYTE, UInt8) -MAKE_ARRAY_TYPE(INT16, Int16) -MAKE_ARRAY_TYPE(UINT16, UInt16) -MAKE_ARRAY_TYPE(INT32, Int32) -MAKE_ARRAY_TYPE(UINT32, UInt32) -MAKE_ARRAY_TYPE(INT64, Int64) -MAKE_ARRAY_TYPE(UINT64, UInt64) -MAKE_ARRAY_TYPE(FLOAT, Single) -MAKE_ARRAY_TYPE(DOUBLE, Double) -MAKE_ARRAY_TYPE(WCHAR, Char16) -//MAKE_ARRAY_TYPE(boolean, Boolean) //conflict with identical type in C++ of BYTE/UInt8 -MAKE_ARRAY_TYPE(HSTRING, String) -MAKE_ARRAY_TYPE(IInspectable*, Inspectable) -MAKE_ARRAY_TYPE(GUID, Guid) -MAKE_ARRAY_TYPE(ABI::Windows::Foundation::DateTime, DateTime) -MAKE_ARRAY_TYPE(ABI::Windows::Foundation::TimeSpan, TimeSpan) -MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Point, Point) -MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Size, Size) -MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Rect, Rect) - -template < typename T > -struct DerefHelper -{ - typedef T DerefType; -}; - -template < typename T > -struct DerefHelper -{ - typedef T DerefType; -}; - -#define __is_valid_winrt_type(_Type) (std::is_void<_Type>::value || \ - std::is_same<_Type, BYTE>::value || \ - std::is_same<_Type, INT16>::value || \ - std::is_same<_Type, UINT16>::value || \ - std::is_same<_Type, INT32>::value || \ - std::is_same<_Type, UINT32>::value || \ - std::is_same<_Type, INT64>::value || \ - std::is_same<_Type, UINT64>::value || \ - std::is_same<_Type, FLOAT>::value || \ - std::is_same<_Type, DOUBLE>::value || \ - std::is_same<_Type, WCHAR>::value || \ - std::is_same<_Type, boolean>::value || \ - std::is_same<_Type, HSTRING>::value || \ - std::is_same<_Type, IInspectable *>::value || \ - std::is_base_of::value || \ - std::is_base_of::DerefType>::value || \ - std::is_same<_Type, GUID>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::DateTime>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::TimeSpan>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Point>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Size>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Rect>::value || \ - std::is_same<_Type, BYTE*>::value || \ - std::is_same<_Type, INT16*>::value || \ - std::is_same<_Type, UINT16*>::value || \ - std::is_same<_Type, INT32*>::value || \ - std::is_same<_Type, UINT32*>::value || \ - std::is_same<_Type, INT64*>::value || \ - std::is_same<_Type, UINT64*>::value || \ - std::is_same<_Type, FLOAT*>::value || \ - std::is_same<_Type, DOUBLE*>::value || \ - std::is_same<_Type, WCHAR*>::value || \ - std::is_same<_Type, boolean*>::value || \ - std::is_same<_Type, HSTRING*>::value || \ - std::is_same<_Type, IInspectable **>::value || \ - std::is_same<_Type, GUID*>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::DateTime*>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::TimeSpan*>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Point*>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Size*>::value || \ - std::is_same<_Type, ABI::Windows::Foundation::Rect*>::value) +#include "wrl.h" #endif #else EXTERN_C const IID IID_ICustomStreamSink; diff --git a/modules/videoio/src/cap_qtkit.mm b/modules/videoio/src/cap_qtkit.mm index bdbbd2d1e..ad6037b76 100644 --- a/modules/videoio/src/cap_qtkit.mm +++ b/modules/videoio/src/cap_qtkit.mm @@ -93,6 +93,8 @@ didDropVideoFrameWithSampleBuffer:(QTSampleBuffer *)sampleBuffer - (int)updateImage; - (IplImage*)getOutput; +- (void)doFireTimer:(NSTimer *)timer; + @end /***************************************************************************** @@ -622,6 +624,11 @@ didDropVideoFrameWithSampleBuffer:(QTSampleBuffer *)sampleBuffer return 1; } +- (void)doFireTimer:(NSTimer *)timer { + (void)timer; + // dummy +} + @end diff --git a/modules/videoio/src/cap_ximea.cpp b/modules/videoio/src/cap_ximea.cpp index bf3fdf598..e31c01a5c 100644 --- a/modules/videoio/src/cap_ximea.cpp +++ b/modules/videoio/src/cap_ximea.cpp @@ -2,7 +2,7 @@ #include "precomp.hpp" #ifdef WIN32 -#include "xiApi.h" +#include #else #include #endif @@ -19,7 +19,7 @@ public: virtual bool open( int index ); virtual void close(); - virtual double getProperty(int); + virtual double getProperty(int) const; virtual bool setProperty(int, double); virtual bool grabFrame(); virtual IplImage* retrieveFrame(int); @@ -27,9 +27,9 @@ public: private: void init(); - void errMsg(const char* msg, int errNum); + void errMsg(const char* msg, int errNum) const; void resetCvImage(); - int ocvParamtoXimeaParam(int value); + int ocvParamtoXimeaParam(int value) const; IplImage* frame; HANDLE hmv; @@ -284,7 +284,7 @@ void CvCaptureCAM_XIMEA::resetCvImage() /**********************************************************************************/ -int CvCaptureCAM_XIMEA::ocvParamtoXimeaParam(int property_id) +int CvCaptureCAM_XIMEA::ocvParamtoXimeaParam(int property_id) const { XI_RETURN stat = XI_OK; switch (property_id) @@ -399,6 +399,16 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) value_type = xiTypeEnum; doAcqReset = true; break; + case CV_CAP_PROP_XI_TEST_PATTERN_GENERATOR_SELECTOR: + ximea_param = "test_pattern_generator_selector"; + value_type = xiTypeEnum; + doAcqReset = true; + break; + case CV_CAP_PROP_XI_TEST_PATTERN: + ximea_param = "test_pattern"; + value_type = xiTypeEnum; + doAcqReset = true; + break; case CV_CAP_PROP_XI_IMAGE_DATA_FORMAT: ximea_param = "imgdataformat"; value_type = xiTypeEnum; @@ -478,6 +488,16 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) value_type = xiTypeInteger; doAcqReset = true; break; + case CV_CAP_PROP_XI_REGION_SELECTOR : + ximea_param = "region_selector"; + value_type = xiTypeInteger; + doAcqReset = true; + break; + case CV_CAP_PROP_XI_REGION_MODE : + ximea_param = "region_mode"; + value_type = xiTypeInteger; + doAcqReset = true; + break; case CV_CAP_PROP_XI_EXP_PRIORITY: ximea_param = "exp_priority"; value_type = xiTypeFloat; @@ -544,6 +564,14 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) ximea_param = "hous_temp"; value_type = xiTypeFloat; break; + case CV_CAP_PROP_XI_HOUS_BACK_SIDE_TEMP: + ximea_param = "hous_back_side_temp"; + value_type = xiTypeFloat; + break; + case CV_CAP_PROP_XI_SENSOR_BOARD_TEMP: + ximea_param = "sensor_board_temp"; + value_type = xiTypeFloat; + break; case CV_CAP_PROP_XI_CMS: ximea_param = "cms"; value_type = xiTypeEnum; @@ -652,6 +680,7 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) case CV_CAP_PROP_XI_TRG_SELECTOR: ximea_param = "trigger_selector"; value_type = xiTypeEnum; + doAcqReset = true; break; case CV_CAP_PROP_XI_ACQ_FRAME_BURST_COUNT: ximea_param = "acq_frame_burst_count"; @@ -756,14 +785,17 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) case CV_CAP_PROP_XI_SENSOR_CLOCK_FREQ_HZ: ximea_param = "sensor_clock_freq_hz"; value_type = xiTypeFloat; + doAcqReset = true; break; case CV_CAP_PROP_XI_SENSOR_CLOCK_FREQ_INDEX: ximea_param = "sensor_clock_freq_index"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_SENSOR_OUTPUT_CHANNEL_COUNT: ximea_param = "sensor_output_channel_count"; value_type = xiTypeEnum; + doAcqReset = true; break; case CV_CAP_PROP_XI_FRAMERATE: ximea_param = "framerate"; @@ -784,6 +816,7 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) case CV_CAP_PROP_XI_AVAILABLE_BANDWIDTH: ximea_param = "available_bandwidth"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_BUFFER_POLICY: ximea_param = "buffer_policy"; @@ -792,14 +825,17 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) case CV_CAP_PROP_XI_LUT_EN: ximea_param = "LUTEnable"; value_type = xiTypeBoolean; + doAcqReset = true; break; case CV_CAP_PROP_XI_LUT_INDEX: ximea_param = "LUTIndex"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_LUT_VALUE: ximea_param = "LUTValue"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_TRG_DELAY: ximea_param = "trigger_delay"; @@ -820,22 +856,27 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) case CV_CAP_PROP_XI_ACQ_BUFFER_SIZE: ximea_param = "acq_buffer_size"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_ACQ_BUFFER_SIZE_UNIT: ximea_param = "acq_buffer_size_unit"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_SIZE: ximea_param = "acq_transport_buffer_size"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_BUFFERS_QUEUE_SIZE: ximea_param = "buffers_queue_size"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_ACQ_TRANSPORT_BUFFER_COMMIT: ximea_param = "acq_transport_buffer_commit"; value_type = xiTypeInteger; + doAcqReset = true; break; case CV_CAP_PROP_XI_RECENT_FRAME: ximea_param = "recent_frame"; @@ -850,9 +891,14 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) ximea_param = "column_fpn_correction"; value_type = xiTypeEnum; break; + case CV_CAP_PROP_XI_ROW_FPN_CORRECTION: + ximea_param = "row_fpn_correction"; + value_type = xiTypeEnum; + break; case CV_CAP_PROP_XI_SENSOR_MODE: ximea_param = "sensor_mode"; value_type = xiTypeEnum; + doAcqReset = true; break; case CV_CAP_PROP_XI_HDR: ximea_param = "hdr"; @@ -894,6 +940,14 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) ximea_param = "auto_bandwidth_calculation"; value_type = xiTypeBoolean; break; + case CV_CAP_PROP_XI_FFS_FILE_ID: + ximea_param = "ffs_file_id"; + value_type = xiTypeInteger; + break; + case CV_CAP_PROP_XI_FFS_FILE_SIZE: + ximea_param = "ffs_file_size"; + value_type = xiTypeInteger; + break; case CV_CAP_PROP_XI_FREE_FFS_SIZE: ximea_param = "free_ffs_size"; value_type = xiTypeInteger; @@ -963,7 +1017,7 @@ bool CvCaptureCAM_XIMEA::setProperty( int property_id, double value ) /**********************************************************************************/ -double CvCaptureCAM_XIMEA::getProperty( int property_id ) +double CvCaptureCAM_XIMEA::getProperty( int property_id ) const { XI_RETURN stat = XI_OK; double getPropVal = 0; @@ -1042,6 +1096,14 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) ximea_param = "decimation_pattern"; value_type = xiTypeEnum; break; + case CV_CAP_PROP_XI_TEST_PATTERN_GENERATOR_SELECTOR: + ximea_param = "test_pattern_generator_selector"; + value_type = xiTypeEnum; + break; + case CV_CAP_PROP_XI_TEST_PATTERN: + ximea_param = "test_pattern"; + value_type = xiTypeEnum; + break; case CV_CAP_PROP_XI_IMAGE_DATA_FORMAT: ximea_param = "imgdataformat"; value_type = xiTypeEnum; @@ -1114,6 +1176,14 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) ximea_param = "offsetY"; value_type = xiTypeInteger; break; + case CV_CAP_PROP_XI_REGION_SELECTOR : + ximea_param = "region_selector"; + value_type = xiTypeInteger; + break; + case CV_CAP_PROP_XI_REGION_MODE : + ximea_param = "region_mode"; + value_type = xiTypeInteger; + break; case CV_CAP_PROP_XI_EXP_PRIORITY: ximea_param = "exp_priority"; value_type = xiTypeFloat; @@ -1174,6 +1244,14 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) ximea_param = "hous_temp"; value_type = xiTypeFloat; break; + case CV_CAP_PROP_XI_HOUS_BACK_SIDE_TEMP: + ximea_param = "hous_back_side_temp"; + value_type = xiTypeFloat; + break; + case CV_CAP_PROP_XI_SENSOR_BOARD_TEMP: + ximea_param = "sensor_board_temp"; + value_type = xiTypeFloat; + break; case CV_CAP_PROP_XI_CMS: ximea_param = "cms"; value_type = xiTypeEnum; @@ -1478,6 +1556,10 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) ximea_param = "column_fpn_correction"; value_type = xiTypeEnum; break; + case CV_CAP_PROP_XI_ROW_FPN_CORRECTION: + ximea_param = "row_fpn_correction"; + value_type = xiTypeEnum; + break; case CV_CAP_PROP_XI_SENSOR_MODE: ximea_param = "sensor_mode"; value_type = xiTypeEnum; @@ -1522,6 +1604,14 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) ximea_param = "auto_bandwidth_calculation"; value_type = xiTypeBoolean; break; + case CV_CAP_PROP_XI_FFS_FILE_ID: + ximea_param = "ffs_file_id"; + value_type = xiTypeInteger; + break; + case CV_CAP_PROP_XI_FFS_FILE_SIZE: + ximea_param = "ffs_file_size"; + value_type = xiTypeInteger; + break; case CV_CAP_PROP_XI_FREE_FFS_SIZE: ximea_param = "free_ffs_size"; value_type = xiTypeInteger; @@ -1572,7 +1662,7 @@ double CvCaptureCAM_XIMEA::getProperty( int property_id ) /**********************************************************************************/ -void CvCaptureCAM_XIMEA::errMsg(const char* msg, int errNum) +void CvCaptureCAM_XIMEA::errMsg(const char* msg, int errNum) const { // with XI_OK there is nothing to report if(errNum == XI_OK) return; @@ -1640,6 +1730,7 @@ void CvCaptureCAM_XIMEA::errMsg(const char* msg, int errNum) case XI_BUFFER_SIZE_TOO_SMALL : error_message = "Buffer provided by user is too small"; break; case XI_COULDNT_INIT_PROCESSOR : error_message = "Couldnt initialize processor."; break; case XI_NOT_INITIALIZED : error_message = "The object/module/procedure/process being referred to has not been started."; break; + case XI_RESOURCE_NOT_FOUND : error_message = "Resource not found(could be processor, file, item..)."; break; case XI_UNKNOWN_PARAM : error_message = "Unknown parameter"; break; case XI_WRONG_PARAM_VALUE : error_message = "Wrong parameter value"; break; case XI_WRONG_PARAM_TYPE : error_message = "Wrong parameter type"; break; diff --git a/modules/videoio/src/ppltasks_winrt.hpp b/modules/videoio/src/ppltasks_winrt.hpp deleted file mode 100644 index 8fe132a53..000000000 --- a/modules/videoio/src/ppltasks_winrt.hpp +++ /dev/null @@ -1,9466 +0,0 @@ -/*** -* ==++== -* -* Copyright (c) Microsoft Corporation. All rights reserved. -* -* Modified for native C++ WRL support by Gregory Morse -* -* ==--== -* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ -* -* ppltasks_winrt.h -* -* Parallel Patterns Library - PPL Tasks -* -* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -****/ - -#pragma once - -#ifndef _PPLTASKS_WINRT_H -#define _PPLTASKS_WINRT_H - -#include -#include -#if _MSC_VER >= 1800 -#include - -// Cannot build using a compiler that is older than dev10 SP1 -#ifdef _MSC_VER -#if _MSC_FULL_VER < 160040219 /*IFSTRIP=IGN*/ -#error ERROR: Visual Studio 2010 SP1 or later is required to build ppltasks -#endif /*IFSTRIP=IGN*/ -#endif -#else -#include -#endif -#include -#include -#include -#include -#if _MSC_VER >= 1800 -#include -#endif - -#ifndef __cplusplus_winrt - -#include -#include -#if _MSC_VER >= 1800 -#include "agile_wrl.hpp" -#endif -#include -#include - -#ifndef _UITHREADCTXT_SUPPORT - -#ifdef WINAPI_FAMILY /*IFSTRIP=IGN*/ - -// It is safe to include winapifamily as WINAPI_FAMILY was defined by the user -#include - -#if WINAPI_FAMILY == WINAPI_FAMILY_APP /*IFSTRIP=IGN*/ - // UI thread context support is not required for desktop and Windows Store apps - #define _UITHREADCTXT_SUPPORT 0 -#elif WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP /*IFSTRIP=IGN*/ - // UI thread context support is not required for desktop and Windows Store apps - #define _UITHREADCTXT_SUPPORT 0 -#else /* WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP */ - #define _UITHREADCTXT_SUPPORT 1 -#endif /* WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP */ - -#else /* WINAPI_FAMILY */ - // Not supported without a WINAPI_FAMILY setting. - #define _UITHREADCTXT_SUPPORT 0 -#endif /* WINAPI_FAMILY */ - -#endif /* _UITHREADCTXT_SUPPORT */ - -#if _UITHREADCTXT_SUPPORT -#include -#endif /* _UITHREADCTXT_SUPPORT */ - -#pragma detect_mismatch("_PPLTASKS_WITH_WINRT", "0") - -#ifdef _DEBUG -#define _DBG_ONLY(X) X -#else -#define _DBG_ONLY(X) -#endif // #ifdef _DEBUG - -// std::copy_exception changed to std::make_exception_ptr from VS 2010 to VS 11. -#ifdef _MSC_VER -#if _MSC_VER < 1700 /*IFSTRIP=IGN*/ -namespace std -{ - template exception_ptr make_exception_ptr(_E _Except) - { - return copy_exception(_Except); - } -} -#endif -#ifndef _PPLTASK_ASYNC_LOGGING -#if _MSC_VER >= 1800 && defined(__cplusplus_winrt) -#define _PPLTASK_ASYNC_LOGGING 1 // Only enable async logging under dev12 winrt -#else -#define _PPLTASK_ASYNC_LOGGING 0 -#endif -#endif -#endif - -#pragma pack(push,_CRT_PACKING) - -#pragma warning(push) -#pragma warning(disable: 28197) -#pragma warning(disable: 4100) // Unreferenced formal parameter - needed for document generation -#if _MSC_VER >= 1800 -#pragma warning(disable: 4127) // constant express in if condition - we use it for meta programming -#else -#pragma warning(disable: 4702) // Unreachable code - it is caused by user lambda throw exceptions -#endif - -// All CRT public header files are required to be protected from the macro new -#pragma push_macro("new") -#undef new - -// stuff ported from Dev11 CRT -// NOTE: this doesn't actually match std::declval. it behaves differently for void! -// so don't blindly change it to std::declval. -namespace stdx -{ - template - _T&& declval(); -} - -/// -/// The Concurrency_winrt namespace provides classes and functions that give you access to the Concurrency Runtime, -/// a concurrent programming framework for C++. For more information, see . -/// -/**/ -namespace Concurrency_winrt -{ - // In debug builds, default to 10 frames, unless this is overridden prior to #includ'ing ppltasks.h. In retail builds, default to only one frame. -#ifndef PPL_TASK_SAVE_FRAME_COUNT -#ifdef _DEBUG -#define PPL_TASK_SAVE_FRAME_COUNT 10 -#else -#define PPL_TASK_SAVE_FRAME_COUNT 1 -#endif -#endif - - /// - /// Helper macro to determine how many stack frames need to be saved. When any number less or equal to 1 is specified, - /// only one frame is captured and no stackwalk will be involved. Otherwise, the number of callstack frames will be captured. - /// - /// - /// This needs to be defined as a macro rather than a function so that if we're only gathering one frame, _ReturnAddress() - /// will evaluate to client code, rather than a helper function inside of _TaskCreationCallstack, itself. - /// -#ifdef _CAPTURE_CALLSTACK -#undef _CAPTURE_CALLSTACK -#endif -#if PPL_TASK_SAVE_FRAME_COUNT > 1 -#if !defined(_DEBUG) -#pragma message ("WARNING: Redefinning PPL_TASK_SAVE_FRAME_COUNT under Release build for non-desktop applications is not supported; only one frame will be captured!") -#define _CAPTURE_CALLSTACK() ::Concurrency_winrt::details::_TaskCreationCallstack::_CaptureSingleFrameCallstack(_ReturnAddress()) -#else -#define _CAPTURE_CALLSTACK() ::Concurrency_winrt::details::_TaskCreationCallstack::_CaptureMultiFramesCallstack(PPL_TASK_SAVE_FRAME_COUNT) -#endif -#else -#define _CAPTURE_CALLSTACK() ::Concurrency_winrt::details::_TaskCreationCallstack::_CaptureSingleFrameCallstack(_ReturnAddress()) -#endif -/// - -/// A type that represents the terminal state of a task. Valid values are completed and canceled. -/// -/// -/**/ -typedef Concurrency::task_group_status task_status; - -template class task; -template <> class task; - -/// -/// Returns an indication of whether the task that is currently executing has received a request to cancel its -/// execution. Cancellation is requested on a task if the task was created with a cancellation token, and -/// the token source associated with that token is canceled. -/// -/// -/// true if the currently executing task has received a request for cancellation, false otherwise. -/// -/// -/// If you call this method in the body of a task and it returns true, you must respond with a call to -/// cancel_current_task to acknowledge the cancellation request, -/// after performing any cleanup you need. This will abort the execution of the task and cause it to enter into -/// the canceled state. If you do not respond and continue execution, or return instead of calling -/// cancel_current_task, the task will enter the completed state when it is done. -/// state. -/// A task is not cancellable if it was created without a cancellation token. -/// -/// -/// -/// -/// -/**/ -#if _MSC_VER >= 1800 -inline bool __cdecl is_task_cancellation_requested() -{ - return ::Concurrency::details::_TaskCollection_t::_Is_cancellation_requested(); -} -#else -inline bool __cdecl is_task_cancellation_requested() -{ - // ConcRT scheduler under the hood is using TaskCollection, which is same as task_group - return ::Concurrency::is_current_task_group_canceling(); -} -#endif - -/// -/// Cancels the currently executing task. This function can be called from within the body of a task to abort the -/// task's execution and cause it to enter the canceled state. While it may be used in response to -/// the is_task_cancellation_requested function, you may -/// also use it by itself, to initiate cancellation of the task that is currently executing. -/// It is not a supported scenario to call this function if you are not within the body of a task. -/// Doing so will result in undefined behavior such as a crash or a hang in your application. -/// -/// -/// -/**/ -//#if _MSC_VER >= 1800 -inline __declspec(noreturn) void __cdecl cancel_current_task() -{ - throw Concurrency::task_canceled(); -} -//#else -//_CRTIMP2 __declspec(noreturn) void __cdecl cancel_current_task(); -//#endif - -namespace details -{ -#if _MSC_VER >= 1800 - /// - /// Callstack container, which is used to capture and preserve callstacks in ppltasks. - /// Members of this class is examined by vc debugger, thus there will be no public access methods. - /// Please note that names of this class should be kept stable for debugger examining. - /// - class _TaskCreationCallstack - { - private: - // If _M_SingleFrame != nullptr, there will be only one frame of callstacks, which is stored in _M_SingleFrame; - // otherwise, _M_Frame will store all the callstack frames. - void* _M_SingleFrame; - std::vector _M_frames; - public: - _TaskCreationCallstack() - { - _M_SingleFrame = nullptr; - } - - // Store one frame of callstack. This function works for both Debug / Release CRT. - static _TaskCreationCallstack _CaptureSingleFrameCallstack(void *_SingleFrame) - { - _TaskCreationCallstack _csc; - _csc._M_SingleFrame = _SingleFrame; - return _csc; - } - - // Capture _CaptureFrames number of callstack frames. This function only work properly for Desktop or Debug CRT. - __declspec(noinline) - static _TaskCreationCallstack _CaptureMultiFramesCallstack(size_t _CaptureFrames) - { - _TaskCreationCallstack _csc; - _csc._M_frames.resize(_CaptureFrames); - // skip 2 frames to make sure callstack starts from user code - _csc._M_frames.resize(::Concurrency::details::platform::CaptureCallstack(&_csc._M_frames[0], 2, _CaptureFrames)); - return _csc; - } - }; -#endif - typedef UINT32 _Unit_type; - - struct _TypeSelectorNoAsync {}; - struct _TypeSelectorAsyncOperationOrTask {}; - struct _TypeSelectorAsyncOperation : public _TypeSelectorAsyncOperationOrTask { }; - struct _TypeSelectorAsyncTask : public _TypeSelectorAsyncOperationOrTask { }; - struct _TypeSelectorAsyncAction {}; - struct _TypeSelectorAsyncActionWithProgress {}; - struct _TypeSelectorAsyncOperationWithProgress {}; - - template - struct _NormalizeVoidToUnitType - { - typedef _Ty _Type; - }; - - template<> - struct _NormalizeVoidToUnitType - { - typedef _Unit_type _Type; - }; - - template - struct _IsUnwrappedAsyncSelector - { - static const bool _Value = true; - }; - - template<> - struct _IsUnwrappedAsyncSelector<_TypeSelectorNoAsync> - { - static const bool _Value = false; - }; - - template - struct _UnwrapTaskType - { - typedef _Ty _Type; - }; - - template - struct _UnwrapTaskType> - { - typedef _Ty _Type; - }; - - template - _TypeSelectorAsyncTask _AsyncOperationKindSelector(task<_T>); - - _TypeSelectorNoAsync _AsyncOperationKindSelector(...); - - template - struct _Unhat - { - typedef _Type _Value; - }; - - template - struct _Unhat<_Type*> - { - typedef _Type _Value; - }; - - //struct _NonUserType { public: int _Dummy; }; - - template - struct _ValueTypeOrRefType - { - typedef _Unit_type _Value; - }; - - template - struct _ValueTypeOrRefType<_Type, true> - { - typedef _Type _Value; - }; - - template - _Ty _UnwrapAsyncActionWithProgressSelector(ABI::Windows::Foundation::IAsyncActionWithProgress_impl<_Ty>*); - - template - _Ty _UnwrapAsyncActionWithProgressSelector(...); - - template - _Progress _UnwrapAsyncOperationWithProgressProgressSelector(ABI::Windows::Foundation::IAsyncOperationWithProgress_impl<_Ty, _Progress>*); - - template - _Progress _UnwrapAsyncOperationWithProgressProgressSelector(...); - - template - _T2 _ProgressTypeSelector(ABI::Windows::Foundation::IAsyncOperationWithProgress<_T1, _T2>*); - - template - _T1 _ProgressTypeSelector(ABI::Windows::Foundation::IAsyncActionWithProgress<_T1>*); - - template - struct _GetProgressType - { - typedef decltype(_ProgressTypeSelector(stdx::declval<_Type>())) _Value; - }; - - template - _TypeSelectorAsyncOperation _AsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncOperation<_T>*); - - _TypeSelectorAsyncAction _AsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncAction*); - - template - _TypeSelectorAsyncOperationWithProgress _AsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncOperationWithProgress<_T1, _T2>*); - - template - _TypeSelectorAsyncActionWithProgress _AsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncActionWithProgress<_T>*); - - template - struct _IsIAsyncInfo - { - static const bool _Value = std::is_base_of::_Value>::value || - std::is_same<_TypeSelectorAsyncAction, decltype(details::_AsyncOperationKindSelector(stdx::declval<_Type>()))>::value || - std::is_same<_TypeSelectorAsyncOperation, decltype(details::_AsyncOperationKindSelector(stdx::declval<_Type>()))>::value || - std::is_same<_TypeSelectorAsyncOperationWithProgress, decltype(details::_AsyncOperationKindSelector(stdx::declval<_Type>()))>::value || - std::is_same<_TypeSelectorAsyncActionWithProgress, decltype(details::_AsyncOperationKindSelector(stdx::declval<_Type>()))>::value; - }; - - template <> - struct _IsIAsyncInfo - { - static const bool _Value = false; - }; - - template - _Ty _UnwrapAsyncOperationSelector(ABI::Windows::Foundation::IAsyncOperation_impl<_Ty>*); - - template - _Ty _UnwrapAsyncOperationSelector(...); - - template - _Ty _UnwrapAsyncOperationWithProgressSelector(ABI::Windows::Foundation::IAsyncOperationWithProgress_impl<_Ty, _Progress>*); - - template - _Ty _UnwrapAsyncOperationWithProgressSelector(...); - - // Unwrap functions for asyncOperations - template - auto _GetUnwrappedType(ABI::Windows::Foundation::IAsyncOperation<_Ty>*) -> typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type; - - void _GetUnwrappedType(ABI::Windows::Foundation::IAsyncAction*); - - template - auto _GetUnwrappedType(ABI::Windows::Foundation::IAsyncOperationWithProgress<_Ty, _Progress>*) -> typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type; - - template - void _GetUnwrappedType(ABI::Windows::Foundation::IAsyncActionWithProgress<_Progress>*); - - template - _T _ReturnAsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncOperation<_T>*); - - void _ReturnAsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncAction*); - - template - _T1 _ReturnAsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncOperationWithProgress<_T1, _T2>*); - - template - void _ReturnAsyncOperationKindSelector(ABI::Windows::Foundation::IAsyncActionWithProgress<_T>*); - - class _ProgressReporterCtorArgType{}; - - template ::_Value> - struct _TaskTypeTraits - { - typedef typename details::_UnwrapTaskType<_Type>::_Type _TaskRetType; - typedef _TaskRetType _TaskRetType_abi; - typedef decltype(_AsyncOperationKindSelector(stdx::declval<_Type>())) _AsyncKind; - typedef typename details::_NormalizeVoidToUnitType<_TaskRetType>::_Type _NormalizedTaskRetType; - - static const bool _IsAsyncTask = _IsAsync; - static const bool _IsUnwrappedTaskOrAsync = details::_IsUnwrappedAsyncSelector<_AsyncKind>::_Value; - }; - - template - struct _TaskTypeTraits<_Type, true> - { - typedef decltype(_ReturnAsyncOperationKindSelector(stdx::declval<_Type>())) _TaskRetType; - typedef decltype(_GetUnwrappedType(stdx::declval<_Type>())) _TaskRetType_abi; - typedef _TaskRetType _NormalizedTaskRetType; - typedef decltype(_AsyncOperationKindSelector(stdx::declval<_Type>())) _AsyncKind; - - static const bool _IsAsyncTask = true; - static const bool _IsUnwrappedTaskOrAsync = details::_IsUnwrappedAsyncSelector<_AsyncKind>::_Value; - }; - - template auto _IsCallable(_Function _Func, int, int, int) -> decltype(_Func(stdx::declval*>()), std::true_type()) { (void)_Func; return std::true_type(); } - template auto _IsCallable(_Function _Func, int, int, ...) -> decltype(_Func(stdx::declval<_ReturnType*>()), std::true_type()) { (void)_Func; return std::true_type(); } - template auto _IsCallable(_Function _Func, int, ...) -> decltype(_Func(), std::true_type()) { (void)_Func; return std::true_type(); } - template std::false_type _IsCallable(_Function, ...) { return std::false_type(); } - - template <> - struct _TaskTypeTraits - { - typedef void _TaskRetType; - typedef void _TaskRetType_abi; - typedef _TypeSelectorNoAsync _AsyncKind; - typedef _Unit_type _NormalizedTaskRetType; - - static const bool _IsAsyncTask = false; - static const bool _IsUnwrappedTaskOrAsync = false; - }; - - // *************************************************************************** - // Template type traits and helpers for async production APIs: - // - - struct _ZeroArgumentFunctor { }; - struct _OneArgumentFunctor { }; - struct _TwoArgumentFunctor { }; - struct _ThreeArgumentFunctor { }; - - // **************************************** - // CLASS TYPES: - - // mutable functions - // ******************** - // THREE ARGUMENTS: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3)); - - // non-void arg: - template - _Arg2 _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3)); - - // non-void arg: - template - _Arg3 _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3)); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3)); - - template - _ThreeArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3)); - - // ******************** - // TWO ARGUMENTS: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2)); - - // non-void arg: - template - _Arg2 _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2)); - - // non-void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2)); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2)); - - template - _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1, _Arg2)); - - // ******************** - // ONE ARGUMENT: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1)); - - // non-void arg: - template - void _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1)); - - // non-void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1)); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1)); - - template - _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1)); - - // ******************** - // ZERO ARGUMENT: - - // void arg: - template - void _Arg1ClassHelperThunk(_ReturnType(_Class::*)()); - - // void arg: - template - void _Arg2ClassHelperThunk(_ReturnType(_Class::*)()); - - // void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)()); - - // void arg: - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)()); - - template - _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)()); - - // ******************** - // THREE ARGUMENTS: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3) const); - - // non-void arg: - template - _Arg2 _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3) const); - - // non-void arg: - template - _Arg3 _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3) const); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3) const); - - template - _ThreeArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1, _Arg2, _Arg3) const); - - // ******************** - // TWO ARGUMENTS: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2) const); - - // non-void arg: - template - _Arg2 _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2) const); - - // non-void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2) const); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1, _Arg2) const); - - template - _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1, _Arg2) const); - - // ******************** - // ONE ARGUMENT: - - // non-void arg: - template - _Arg1 _Arg1ClassHelperThunk(_ReturnType(_Class::*)(_Arg1) const); - - // non-void arg: - template - void _Arg2ClassHelperThunk(_ReturnType(_Class::*)(_Arg1) const); - - // non-void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)(_Arg1) const); - - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)(_Arg1) const); - - template - _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)(_Arg1) const); - - // ******************** - // ZERO ARGUMENT: - - // void arg: - template - void _Arg1ClassHelperThunk(_ReturnType(_Class::*)() const); - - // void arg: - template - void _Arg2ClassHelperThunk(_ReturnType(_Class::*)() const); - - // void arg: - template - void _Arg3ClassHelperThunk(_ReturnType(_Class::*)() const); - - // void arg: - template - _ReturnType _ReturnTypeClassHelperThunk(_ReturnType(_Class::*)() const); - - template - _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(_Class::*)() const); - - // **************************************** - // POINTER TYPES: - - // ******************** - // THREE ARGUMENTS: - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg3 _Arg3PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2, _Arg3)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2, _Arg3)); - - template - _ThreeArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg3 _Arg3PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2, _Arg3)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2, _Arg3)); - - template - _ThreeArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2, _Arg3)); - - template - _Arg3 _Arg3PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2, _Arg3)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2, _Arg3)); - - template - _ThreeArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)(_Arg1, _Arg2, _Arg3)); - - // ******************** - // TWO ARGUMENTS: - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)(_Arg1, _Arg2)); - - template - _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)(_Arg1, _Arg2)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)(_Arg1, _Arg2)); - - template - _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)(_Arg1, _Arg2)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2)); - - template - _Arg2 _Arg2PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)(_Arg1, _Arg2)); - - template - _TwoArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)(_Arg1, _Arg2)); - - // ******************** - // ONE ARGUMENT: - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1)); - - template - void _Arg2PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__cdecl *)(_Arg1)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)(_Arg1)); - - template - _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)(_Arg1)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1)); - - template - void _Arg2PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__stdcall *)(_Arg1)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)(_Arg1)); - - template - _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)(_Arg1)); - - template - _Arg1 _Arg1PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1)); - - template - void _Arg2PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1)); - - template - void _Arg3PFNHelperThunk(_ReturnType(__fastcall *)(_Arg1)); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)(_Arg1)); - - template - _OneArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)(_Arg1)); - - // ******************** - // ZERO ARGUMENT: - - template - void _Arg1PFNHelperThunk(_ReturnType(__cdecl *)()); - - template - void _Arg2PFNHelperThunk(_ReturnType(__cdecl *)()); - - template - void _Arg3PFNHelperThunk(_ReturnType(__cdecl *)()); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__cdecl *)()); - - template - _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__cdecl *)()); - - template - void _Arg1PFNHelperThunk(_ReturnType(__stdcall *)()); - - template - void _Arg2PFNHelperThunk(_ReturnType(__stdcall *)()); - - template - void _Arg3PFNHelperThunk(_ReturnType(__stdcall *)()); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__stdcall *)()); - - template - _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__stdcall *)()); - - template - void _Arg1PFNHelperThunk(_ReturnType(__fastcall *)()); - - template - void _Arg2PFNHelperThunk(_ReturnType(__fastcall *)()); - - template - void _Arg3PFNHelperThunk(_ReturnType(__fastcall *)()); - - template - _ReturnType _ReturnTypePFNHelperThunk(_ReturnType(__fastcall *)()); - - template - _ZeroArgumentFunctor _ArgumentCountHelper(_ReturnType(__fastcall *)()); - - template - struct _FunctorArguments - { - static const size_t _Count = 0; - }; - - template<> - struct _FunctorArguments<_OneArgumentFunctor> - { - static const size_t _Count = 1; - }; - - template<> - struct _FunctorArguments<_TwoArgumentFunctor> - { - static const size_t _Count = 2; - }; - - template<> - struct _FunctorArguments<_ThreeArgumentFunctor> - { - static const size_t _Count = 3; - }; - - template - struct _FunctorTypeTraits - { - typedef decltype(_ArgumentCountHelper(&(_T::operator()))) _ArgumentCountType; - static const size_t _ArgumentCount = _FunctorArguments<_ArgumentCountType>::_Count; - - typedef decltype(_ReturnTypeClassHelperThunk(&(_T::operator()))) _ReturnType; - typedef decltype(_Arg1ClassHelperThunk(&(_T::operator()))) _Argument1Type; - typedef decltype(_Arg2ClassHelperThunk(&(_T::operator()))) _Argument2Type; - typedef decltype(_Arg3ClassHelperThunk(&(_T::operator()))) _Argument3Type; - }; - - template - struct _FunctorTypeTraits<_T *> - { - typedef decltype(_ArgumentCountHelper(stdx::declval<_T*>())) _ArgumentCountType; - static const size_t _ArgumentCount = _FunctorArguments<_ArgumentCountType>::_Count; - - typedef decltype(_ReturnTypePFNHelperThunk(stdx::declval<_T*>())) _ReturnType; - typedef decltype(_Arg1PFNHelperThunk(stdx::declval<_T*>())) _Argument1Type; - typedef decltype(_Arg2PFNHelperThunk(stdx::declval<_T*>())) _Argument2Type; - typedef decltype(_Arg3PFNHelperThunk(stdx::declval<_T*>())) _Argument3Type; - }; - - task _To_task(); - - template auto _IsVoidConversionHelper(_Function _Func, int) -> typename decltype(_Func(_To_task()), std::true_type()); - template std::false_type _IsVoidConversionHelper(_Function _Func, ...); - - template std::true_type _VoidIsTaskHelper(task _Arg, int); - template std::false_type _VoidIsTaskHelper(T _Arg, ...); - - template(), 0)), std::true_type>::value, const size_t _Count = _FunctorTypeTraits<_Function>::_ArgumentCount> - struct _FunctionTypeTraits - { - typedef typename _Unhat::_Argument2Type>::_Value _FuncRetType; - static_assert(std::is_same::_Argument1Type, _ExpectedParameterType>::value || - std::is_same::_Argument1Type, task<_ExpectedParameterType>>::value, "incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below)"); - - typedef decltype(_VoidIsTaskHelper(stdx::declval<_FunctorTypeTraits<_Function>::_Argument1Type>(), 0)) _Takes_task; - }; - - //if there is a continuation parameter, then must use void/no return value - template - struct _FunctionTypeTraits<_Function, _ExpectedParameterType, _IsVoidConversion, 1> - { - typedef void _FuncRetType; - static_assert(std::is_same::_Argument1Type, _ExpectedParameterType>::value || - std::is_same::_Argument1Type, task<_ExpectedParameterType>>::value, "incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below)"); - - typedef decltype(_VoidIsTaskHelper(stdx::declval<_FunctorTypeTraits<_Function>::_Argument1Type>(), 0)) _Takes_task; - }; - - template - struct _FunctionTypeTraits<_Function, void, true, 1> - { - typedef void _FuncRetType; - static_assert(std::is_same::_Argument1Type, decltype(_To_task())>::value, "incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType> (see below)"); - - typedef decltype(_VoidIsTaskHelper(stdx::declval<_FunctorTypeTraits<_Function>::_Argument1Type>(), 0)) _Takes_task; - }; - - template - struct _FunctionTypeTraits<_Function, void, false, 1> - { - typedef typename _Unhat::_Argument1Type>::_Value _FuncRetType; - - typedef std::false_type _Takes_task; - }; - - template - struct _FunctionTypeTraits<_Function, _ExpectedParameterType, _IsVoidConversion, 0> - { - typedef void _FuncRetType; - - typedef std::false_type _Takes_task; - }; - - template - struct _ContinuationTypeTraits - { - typedef typename task::_FuncRetType>::_TaskRetType_abi> _TaskOfType; - }; - - // _InitFunctorTypeTraits is used to decide whether a task constructed with a lambda should be unwrapped. Depending on how the variable is - // declared, the constructor may or may not perform unwrapping. For eg. - // - // This declaration SHOULD NOT cause unwrapping - // task> t1([]() -> task { - // task t2([]() {}); - // return t2; - // }); - // - // This declaration SHOULD cause unwrapping - // task> t1([]() -> task { - // task t2([]() {}); - // return t2; - // }); - // If the type of the task is the same as the return type of the function, no unwrapping should take place. Else normal rules apply. - template - struct _InitFunctorTypeTraits - { - typedef typename _TaskTypeTraits<_FuncRetType>::_AsyncKind _AsyncKind; - static const bool _IsAsyncTask = _TaskTypeTraits<_FuncRetType>::_IsAsyncTask; - static const bool _IsUnwrappedTaskOrAsync = _TaskTypeTraits<_FuncRetType>::_IsUnwrappedTaskOrAsync; - }; - - template - struct _InitFunctorTypeTraits - { - typedef _TypeSelectorNoAsync _AsyncKind; - static const bool _IsAsyncTask = false; - static const bool _IsUnwrappedTaskOrAsync = false; - }; - /// - /// Helper object used for LWT invocation. - /// - struct _TaskProcThunk - { - _TaskProcThunk(const std::function & _Callback) : - _M_func(_Callback) - { - } - - static void __cdecl _Bridge(void *_PData) - { - _TaskProcThunk *_PThunk = reinterpret_cast<_TaskProcThunk *>(_PData); -#if _MSC_VER >= 1800 - _Holder _ThunkHolder(_PThunk); -#endif - _PThunk->_M_func(); -#if _MSC_VER < 1800 - delete _PThunk; -#endif - } - private: -#if _MSC_VER >= 1800 - // RAII holder - struct _Holder - { - _Holder(_TaskProcThunk * _PThunk) : _M_pThunk(_PThunk) - { - } - - ~_Holder() - { - delete _M_pThunk; - } - - _TaskProcThunk * _M_pThunk; - - private: - _Holder& operator=(const _Holder&); - }; -#endif - std::function _M_func; - _TaskProcThunk& operator=(const _TaskProcThunk&); - }; - - /// - /// Schedule a functor with automatic inlining. Note that this is "fire and forget" scheduling, which cannot be - /// waited on or canceled after scheduling. - /// This schedule method will perform automatic inlining base on . - /// - /// - /// The user functor need to be scheduled. - /// - /// - /// The inlining scheduling policy for current functor. - /// -#if _MSC_VER >= 1800 - typedef Concurrency::details::_TaskInliningMode_t _TaskInliningMode; -#else - typedef Concurrency::details::_TaskInliningMode _TaskInliningMode; -#endif - static void _ScheduleFuncWithAutoInline(const std::function & _Func, _TaskInliningMode _InliningMode) - { -#if _MSC_VER >= 1800 - Concurrency::details::_TaskCollection_t::_RunTask(&_TaskProcThunk::_Bridge, new _TaskProcThunk(_Func), _InliningMode); -#else - Concurrency::details::_StackGuard _Guard; - if (_Guard._ShouldInline(_InliningMode)) - { - _Func(); - } - else - { - Concurrency::details::_CurrentScheduler::_ScheduleTask(reinterpret_cast(&_TaskProcThunk::_Bridge), new _TaskProcThunk(_Func)); - } -#endif - } - class _ContextCallback - { - typedef std::function _CallbackFunction; - - public: - - static _ContextCallback _CaptureCurrent() - { - _ContextCallback _Context; - _Context._Capture(); - return _Context; - } - - ~_ContextCallback() - { - _Reset(); - } - - _ContextCallback(bool _DeferCapture = false) - { - if (_DeferCapture) - { - _M_context._M_captureMethod = _S_captureDeferred; - } - else - { - _M_context._M_pContextCallback = nullptr; - } - } - - // Resolves a context that was created as _S_captureDeferred based on the environment (ancestor, current context). - void _Resolve(bool _CaptureCurrent) - { - if (_M_context._M_captureMethod == _S_captureDeferred) - { - _M_context._M_pContextCallback = nullptr; - - if (_CaptureCurrent) - { - if (_IsCurrentOriginSTA()) - { - _Capture(); - } -#if _UITHREADCTXT_SUPPORT - else - { - // This method will fail if not called from the UI thread. - HRESULT _Hr = CaptureUiThreadContext(&_M_context._M_pContextCallback); - if (FAILED(_Hr)) - { - _M_context._M_pContextCallback = nullptr; - } - } -#endif // _UITHREADCTXT_SUPPORT - } - } - } - - void _Capture() - { - HRESULT _Hr = CoGetObjectContext(IID_IContextCallback, reinterpret_cast(&_M_context._M_pContextCallback)); - if (FAILED(_Hr)) - { - _M_context._M_pContextCallback = nullptr; - } - } - - _ContextCallback(const _ContextCallback& _Src) - { - _Assign(_Src._M_context._M_pContextCallback); - } - - _ContextCallback(_ContextCallback&& _Src) - { - _M_context._M_pContextCallback = _Src._M_context._M_pContextCallback; - _Src._M_context._M_pContextCallback = nullptr; - } - - _ContextCallback& operator=(const _ContextCallback& _Src) - { - if (this != &_Src) - { - _Reset(); - _Assign(_Src._M_context._M_pContextCallback); - } - return *this; - } - - _ContextCallback& operator=(_ContextCallback&& _Src) - { - if (this != &_Src) - { - _M_context._M_pContextCallback = _Src._M_context._M_pContextCallback; - _Src._M_context._M_pContextCallback = nullptr; - } - return *this; - } - - bool _HasCapturedContext() const - { - _CONCRT_ASSERT(_M_context._M_captureMethod != _S_captureDeferred); - return (_M_context._M_pContextCallback != nullptr); - } - - HRESULT _CallInContext(_CallbackFunction _Func) const - { - if (!_HasCapturedContext()) - { - _Func(); - } - else - { - ComCallData callData; - ZeroMemory(&callData, sizeof(callData)); - callData.pUserDefined = reinterpret_cast(&_Func); - - HRESULT _Hr = _M_context._M_pContextCallback->ContextCallback(&_Bridge, &callData, IID_ICallbackWithNoReentrancyToApplicationSTA, 5, nullptr); - if (FAILED(_Hr)) - { - return _Hr; - } - } - return S_OK; - } - - bool operator==(const _ContextCallback& _Rhs) const - { - return (_M_context._M_pContextCallback == _Rhs._M_context._M_pContextCallback); - } - - bool operator!=(const _ContextCallback& _Rhs) const - { - return !(operator==(_Rhs)); - } - - private: - - void _Reset() - { - if (_M_context._M_captureMethod != _S_captureDeferred && _M_context._M_pContextCallback != nullptr) - { - _M_context._M_pContextCallback->Release(); - } - } - - void _Assign(IContextCallback *_PContextCallback) - { - _M_context._M_pContextCallback = _PContextCallback; - if (_M_context._M_captureMethod != _S_captureDeferred && _M_context._M_pContextCallback != nullptr) - { - _M_context._M_pContextCallback->AddRef(); - } - } - - static HRESULT __stdcall _Bridge(ComCallData *_PParam) - { - _CallbackFunction *pFunc = reinterpret_cast<_CallbackFunction *>(_PParam->pUserDefined); - return (*pFunc)(); - } - - // Returns the origin information for the caller (runtime / Windows Runtime apartment as far as task continuations need know) - static bool _IsCurrentOriginSTA() - { - APTTYPE _AptType; - APTTYPEQUALIFIER _AptTypeQualifier; - - HRESULT hr = CoGetApartmentType(&_AptType, &_AptTypeQualifier); - if (SUCCEEDED(hr)) - { - // We determine the origin of a task continuation by looking at where .then is called, so we can tell whether - // to need to marshal the continuation back to the originating apartment. If an STA thread is in executing in - // a neutral aparment when it schedules a continuation, we will not marshal continuations back to the STA, - // since variables used within a neutral apartment are expected to be apartment neutral. - switch (_AptType) - { - case APTTYPE_MAINSTA: - case APTTYPE_STA: - return true; - default: - break; - } - } - return false; - } - - union - { - IContextCallback *_M_pContextCallback; - size_t _M_captureMethod; - } _M_context; - - static const size_t _S_captureDeferred = 1; - }; - -#if _MSC_VER >= 1800 - template - struct _ResultHolder - { - void Set(const _Type& _type) - { - _Result = _type; - } - - _Type Get() - { - return _Result; - } - - _Type _Result; - }; - - template - struct _ResultHolder<_Type*> - { - void Set(_Type* const & _type) - { - _M_Result = _type; - } - - _Type* Get() - { - return _M_Result.Get(); - } - private: - // ::Platform::Agile handle specialization of all hats - // including ::Platform::String and ::Platform::Array - Agile<_Type*> _M_Result; - }; - - // - // The below are for composability with tasks auto-created from when_any / when_all / && / || constructs. - // - template - struct _ResultHolder> - { - void Set(const std::vector<_Type*>& _type) - { - _Result.reserve(_type.size()); - - for (auto _PTask = _type.begin(); _PTask != _type.end(); ++_PTask) - { - _Result.emplace_back(*_PTask); - } - } - - std::vector<_Type*> Get() - { - // Return vectory with the objects that are marshaled in the proper appartment - std::vector<_Type*> _Return; - _Return.reserve(_Result.size()); - - for (auto _PTask = _Result.begin(); _PTask != _Result.end(); ++_PTask) - { - _Return.push_back(_PTask->Get()); // Agile will marshal the object to appropriate appartment if neccessary - } - - return _Return; - } - - std::vector< Agile<_Type*> > _Result; - }; - - template - struct _ResultHolder > - { - void Set(const std::pair<_Type*, size_t>& _type) - { - _M_Result = _type; - } - - std::pair<_Type*, size_t> Get() - { - return std::make_pair(_M_Result.first, _M_Result.second); - } - private: - std::pair, size_t> _M_Result; - }; -#else - template - struct _ResultContext - { - static _ContextCallback _GetContext(bool /* _RuntimeAggregate */) - { - return _ContextCallback(); - } - - static _Type _GetValue(_Type _ObjInCtx, const _ContextCallback & /* _Ctx */, bool /* _RuntimeAggregate */) - { - return _ObjInCtx; - } - }; - - template::value> - struct _MarshalHelper - { - }; - template - struct _MarshalHelper<_Type, N, true> - { - static _Type* _Perform(_Type(&_ObjInCtx)[N], const _ContextCallback& _Ctx) - { - static_assert(__is_valid_winrt_type(_Type*), "must be a WinRT array compatible type"); - if (_ObjInCtx == nullptr) - { - return nullptr; - } - - HRESULT _Hr; - IStream * _PStream; - _Ctx._CallInContext([&]() -> HRESULT { - // It isn't safe to simply reinterpret_cast a hat type to IUnknown* because some types do not have a real vtable ptr. - // Instead, we could to create a property value to make it "grow" the vtable ptr but instead primitives are not marshalled. - - IUnknown * _PUnk = winrt_array_type::create(_ObjInCtx, N); - _Hr = CoMarshalInterThreadInterfaceInStream(winrt_type<_Type>::getuuid(), _PUnk, &_PStream); - return S_OK; - }); - - // With an APPX manifest, this call should never fail. - _CONCRT_ASSERT(SUCCEEDED(_Hr)); - - _Type* _Proxy; - // - // Cannot use IID_PPV_ARGS with ^ types. - // - _Hr = CoGetInterfaceAndReleaseStream(_PStream, winrt_type<_Type>::getuuid(), reinterpret_cast(&_Proxy)); - if (FAILED(_Hr)) - { - throw std::make_exception_ptr(_Hr); - } - return _Proxy; - } - }; - template - struct _MarshalHelper<_Type, 0, false> - { - static _Type* _Perform(_Type* _ObjInCtx, const _ContextCallback& _Ctx) - { - static_assert(std::is_base_of::value || __is_valid_winrt_type(_Type), "must be a COM or WinRT type"); - if (_ObjInCtx == nullptr) - { - return nullptr; - } - - HRESULT _Hr; - IStream * _PStream; - _Ctx._CallInContext([&]() -> HRESULT { - // It isn't safe to simply reinterpret_cast a hat type to IUnknown* because some types do not have a real vtable ptr. - // Instead, we could to create a property value to make it "grow" the vtable ptr but instead primitives are not marshalled. - - IUnknown * _PUnk = winrt_type<_Type>::create(_ObjInCtx); - _Hr = CoMarshalInterThreadInterfaceInStream(winrt_type<_Type>::getuuid(), _PUnk, &_PStream); - return S_OK; - }); - - // With an APPX manifest, this call should never fail. - _CONCRT_ASSERT(SUCCEEDED(_Hr)); - - _Type* _Proxy; - // - // Cannot use IID_PPV_ARGS with ^ types. - // - _Hr = CoGetInterfaceAndReleaseStream(_PStream, winrt_type<_Type>::getuuid(), reinterpret_cast(&_Proxy)); - if (FAILED(_Hr)) - { - throw std::make_exception_ptr(_Hr); - } - return _Proxy; - } - }; - - // Arrays must be converted to IPropertyValue objects. - - template<> - struct _MarshalHelper - { - static HSTRING _Perform(HSTRING _ObjInCtx, const _ContextCallback& _Ctx) - { - return _ObjInCtx; - } - }; - - template - _Type* _Marshal(_Type* _ObjInCtx, const _ContextCallback& _Ctx) - { - return _MarshalHelper<_Type>::_Perform(_ObjInCtx, _Ctx); - } - - template - struct _InContext - { - static _Type _Get(_Type _ObjInCtx, const _ContextCallback& _Ctx) - { - return _ObjInCtx; - } - }; - - template - struct _InContext<_Type*> - { - static _Type* _Get(_Type* _ObjInCtx, const _ContextCallback& _Ctx) - { - _ContextCallback _CurrentContext = _ContextCallback::_CaptureCurrent(); - if (!_Ctx._HasCapturedContext() || _Ctx == _CurrentContext) - { - return _ObjInCtx; - } - - // - // The object is from another apartment. If it's marshalable, do so. - // - return _Marshal<_Type>(_ObjInCtx, _Ctx); - } - }; - - template - struct _ResultContext<_Type*> - { - static _Type* _GetValue(_Type* _ObjInCtx, const _ContextCallback& _Ctx, bool /* _RuntimeAggregate */) - { - return _InContext<_Type*>::_Get(_ObjInCtx, _Ctx); - } - - static _ContextCallback _GetContext(bool /* _RuntimeAggregate */) - { - return _ContextCallback::_CaptureCurrent(); - } - }; - - // - // The below are for composability with tasks auto-created from when_any / when_all / && / || constructs. - // - template - struct _ResultContext> - { - static std::vector<_Type*> _GetValue(std::vector<_Type*> _ObjInCtx, const _ContextCallback& _Ctx, bool _RuntimeAggregate) - { - if (!_RuntimeAggregate) - { - return _ObjInCtx; - } - - _ContextCallback _CurrentContext = _ContextCallback::_CaptureCurrent(); - if (!_Ctx._HasCapturedContext() || _Ctx == _CurrentContext) - { - return _ObjInCtx; - } - - for (auto _It = _ObjInCtx.begin(); _It != _ObjInCtx.end(); ++_It) - { - *_It = _Marshal<_Type>(*_It, _Ctx); - } - - return _ObjInCtx; - } - - static _ContextCallback _GetContext(bool _RuntimeAggregate) - { - if (!_RuntimeAggregate) - { - return _ContextCallback(); - } - else - { - return _ContextCallback::_CaptureCurrent(); - } - } - }; - - template - struct _ResultContext> - { - static std::pair<_Type*, size_t> _GetValue(std::pair<_Type*, size_t> _ObjInCtx, const _ContextCallback& _Ctx, bool _RuntimeAggregate) - { - if (!_RuntimeAggregate) - { - return _ObjInCtx; - } - - _ContextCallback _CurrentContext = _ContextCallback::_CaptureCurrent(); - if (!_Ctx._HasCapturedContext() || _Ctx == _CurrentContext) - { - return _ObjInCtx; - } - - return std::pair<_Type*, size_t>(_Marshal<_Type>(_ObjInCtx.first, _Ctx), _ObjInCtx.second); - } - - static _ContextCallback _GetContext(bool _RuntimeAggregate) - { - if (!_RuntimeAggregate) - { - return _ContextCallback(); - } - else - { - return _ContextCallback::_CaptureCurrent(); - } - } - }; -#endif - // An exception thrown by the task body is captured in an exception holder and it is shared with all value based continuations rooted at the task. - // The exception is 'observed' if the user invokes get()/wait() on any of the tasks that are sharing this exception holder. If the exception - // is not observed by the time the internal object owned by the shared pointer destructs, the process will fail fast. - struct _ExceptionHolder - { -#if _MSC_VER >= 1800 - private: - void ReportUnhandledError() - { - if (_M_winRTException != nullptr) - { - throw _M_winRTException.Get(); - } - } - public: - explicit _ExceptionHolder(const std::exception_ptr& _E, const _TaskCreationCallstack &_stackTrace) : - _M_exceptionObserved(0), _M_stdException(_E), _M_stackTrace(_stackTrace) - { - } - - explicit _ExceptionHolder(IRestrictedErrorInfo*& _E, const _TaskCreationCallstack &_stackTrace) : - _M_exceptionObserved(0), _M_winRTException(_E), _M_stackTrace(_stackTrace) - { - } -#else - explicit _ExceptionHolder(const std::exception_ptr& _E, void* _SourceAddressHint) : - _M_exceptionObserved(0), _M_stdException(_E), _M_disassembleMe(_SourceAddressHint) - { - } - - explicit _ExceptionHolder(IRestrictedErrorInfo*& _E, void* _SourceAddressHint) : - _M_exceptionObserved(0), _M_disassembleMe(_SourceAddressHint), _M_winRTException(_E) - { - } -#endif - __declspec(noinline) - ~_ExceptionHolder() - { - if (_M_exceptionObserved == 0) - { -#if _MSC_VER >= 1800 - // If you are trapped here, it means an exception thrown in task chain didn't get handled. - // Please add task-based continuation to handle all exceptions coming from tasks. - // this->_M_stackTrace keeps the creation callstack of the task generates this exception. - _REPORT_PPLTASK_UNOBSERVED_EXCEPTION(); -#else - // Disassemble at this->_M_disassembleMe to get to the source location right after either the creation of the task (constructor - // or then method) that encountered this exception, or the set_exception call for a task_completion_event. - Concurrency::details::_ReportUnobservedException(); -#endif - } - } - - void _RethrowUserException() - { - if (_M_exceptionObserved == 0) - { -#if _MSC_VER >= 1800 - Concurrency::details::atomic_exchange(_M_exceptionObserved, 1l); -#else - _InterlockedExchange(&_M_exceptionObserved, 1); -#endif - } - - if (_M_winRTException != nullptr) - { - throw _M_winRTException.Get(); - } - std::rethrow_exception(_M_stdException); - } - - // A variable that remembers if this exception was every rethrown into user code (and hence handled by the user). Exceptions that - // are unobserved when the exception holder is destructed will terminate the process. -#if _MSC_VER >= 1800 - Concurrency::details::atomic_long _M_exceptionObserved; -#else - long volatile _M_exceptionObserved; -#endif - - // Either _M_stdException or _M_winRTException is populated based on the type of exception encountered. - std::exception_ptr _M_stdException; - Microsoft::WRL::ComPtr _M_winRTException; - - // Disassembling this value will point to a source instruction right after a call instruction. If the call is to create_task, - // a task constructor or the then method, the task created by that method is the one that encountered this exception. If the call - // is to task_completion_event::set_exception, the set_exception method was the source of the exception. - // DO NOT REMOVE THIS VARIABLE. It is extremely helpful for debugging. -#if _MSC_VER >= 1800 - _TaskCreationCallstack _M_stackTrace; -#else - void* _M_disassembleMe; -#endif - }; - -#ifndef RUNTIMECLASS_Concurrency_winrt_details__AsyncInfoImpl_DEFINED -#define RUNTIMECLASS_Concurrency_winrt_details__AsyncInfoImpl_DEFINED - extern const __declspec(selectany) WCHAR RuntimeClass_Concurrency_winrt_details__AsyncInfoImpl[] = L"Concurrency_winrt.details._AsyncInfoImpl"; -#endif - - /// - /// Base converter class for converting asynchronous interfaces to IAsyncOperation - /// - template - struct _AsyncInfoImpl abstract : public Microsoft::WRL::RuntimeClass< - Microsoft::WRL::RuntimeClassFlags< Microsoft::WRL::RuntimeClassType::WinRt>, - Microsoft::WRL::Implements>> - { - InspectableClass(RuntimeClass_Concurrency_winrt_details__AsyncInfoImpl, BaseTrust) - public: - // The async action, action with progress or operation with progress that this stub forwards to. -#if _MSC_VER >= 1800 - Agile<_AsyncOperationType> _M_asyncInfo; -#else - Microsoft::WRL::ComPtr<_AsyncOperationType> _M_asyncInfo; - // The context in which this async info is valid - may be different from the context where the completion handler runs, - // and may require marshalling before it is used. - _ContextCallback _M_asyncInfoContext; -#endif - - Microsoft::WRL::ComPtr<_CompletionHandlerType> _M_CompletedHandler; - - _AsyncInfoImpl(_AsyncOperationType* _AsyncInfo) : _M_asyncInfo(_AsyncInfo) -#if _MSC_VER < 1800 - , _M_asyncInfoContext(_ContextCallback::_CaptureCurrent()) -#endif - {} - - public: - virtual HRESULT OnStart() { return S_OK; } - virtual void OnCancel() { - Microsoft::WRL::ComPtr pAsyncInfo; - HRESULT hr; -#if _MSC_VER >= 1800 - if (SUCCEEDED(hr = _M_asyncInfo.Get()->QueryInterface(pAsyncInfo.GetAddressOf()))) -#else - if (SUCCEEDED(hr = _M_asyncInfo.As(&pAsyncInfo))) -#endif - pAsyncInfo->Cancel(); - else - throw std::make_exception_ptr(hr); - } - virtual void OnClose() { - Microsoft::WRL::ComPtr pAsyncInfo; - HRESULT hr; -#if _MSC_VER >= 1800 - if (SUCCEEDED(hr = _M_asyncInfo.Get()->QueryInterface(pAsyncInfo.GetAddressOf()))) -#else - if (SUCCEEDED(hr = _M_asyncInfo.As(&pAsyncInfo))) -#endif - pAsyncInfo->Close(); - else - throw std::make_exception_ptr(hr); - } - - virtual STDMETHODIMP get_ErrorCode(HRESULT* errorCode) - { - Microsoft::WRL::ComPtr pAsyncInfo; - HRESULT hr; -#if _MSC_VER >= 1800 - if (SUCCEEDED(hr = _M_asyncInfo.Get()->QueryInterface(pAsyncInfo.GetAddressOf()))) -#else - if (SUCCEEDED(hr = _M_asyncInfo.As(&pAsyncInfo))) -#endif - return pAsyncInfo->get_ErrorCode(errorCode); - return hr; - } - - virtual STDMETHODIMP get_Id(UINT* id) - { - Microsoft::WRL::ComPtr pAsyncInfo; - HRESULT hr; -#if _MSC_VER >= 1800 - if (SUCCEEDED(hr = _M_asyncInfo.Get()->QueryInterface(pAsyncInfo.GetAddressOf()))) -#else - if (SUCCEEDED(hr = _M_asyncInfo.As(&pAsyncInfo))) -#endif - return pAsyncInfo->get_Id(id); - return hr; - } - - virtual STDMETHODIMP get_Status(ABI::Windows::Foundation::AsyncStatus *status) - { - Microsoft::WRL::ComPtr pAsyncInfo; - HRESULT hr; -#if _MSC_VER >= 1800 - if (SUCCEEDED(hr = _M_asyncInfo.Get()->QueryInterface(pAsyncInfo.GetAddressOf()))) -#else - if (SUCCEEDED(hr = _M_asyncInfo.As(&pAsyncInfo))) -#endif - return pAsyncInfo->get_Status(status); - return hr; - } - - virtual STDMETHODIMP GetResults(_Result_abi*) { throw std::runtime_error("derived class must implement"); } - - virtual STDMETHODIMP get_Completed(_CompletionHandlerType** handler) - { - if (!handler) return E_POINTER; - _M_CompletedHandler.CopyTo(handler); - return S_OK; - } - - virtual STDMETHODIMP put_Completed(_CompletionHandlerType* value) - { - _M_CompletedHandler = value; - Microsoft::WRL::ComPtr<_CompletionHandlerType> handler = Microsoft::WRL::Callback<_CompletionHandlerType>([&](_AsyncOperationType*, ABI::Windows::Foundation::AsyncStatus status) -> HRESULT { -#if _MSC_VER < 1800 - // Update the saved _M_asyncInfo with a proxy valid in the current context if required. Some Windows APIs return an IAsyncInfo - // that is only valid for the thread that called the API to retrieve. Since this completion handler can run on any thread, we - // need to ensure that the async info is valid in the current apartment. _M_asyncInfo will be accessed via calls to 'this' inside - // _AsyncInit. - _M_asyncInfo = _ResultContext<_AsyncOperationType*>::_GetValue(_M_asyncInfo.Get(), _M_asyncInfoContext, false); -#endif - return _M_CompletedHandler->Invoke(_M_asyncInfo.Get(), status); - }); -#if _MSC_VER >= 1800 - return _M_asyncInfo.Get()->put_Completed(handler.Get()); -#else - return _M_asyncInfo->put_Completed(handler.Get()); -#endif - } - }; - - extern const __declspec(selectany) WCHAR RuntimeClass_IAsyncOperationToAsyncOperationConverter[] = L"_IAsyncOperationToAsyncOperationConverter"; - - /// - /// Class _IAsyncOperationToAsyncOperationConverter is used to convert an instance of IAsyncOperationWithProgress into IAsyncOperation - /// - template - struct _IAsyncOperationToAsyncOperationConverter : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncOperationCompletedHandler<_Result>, - typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type> - { - typedef typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type _Result_abi; - - InspectableClass(RuntimeClass_IAsyncOperationToAsyncOperationConverter, BaseTrust) - public: - _IAsyncOperationToAsyncOperationConverter(ABI::Windows::Foundation::IAsyncOperation<_Result>* _Operation) : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncOperationCompletedHandler<_Result>, - _Result_abi>(_Operation) {} - public: - virtual STDMETHODIMP GetResults(_Result_abi* results) override { - if (!results) return E_POINTER; -#if _MSC_VER >= 1800 - return _M_asyncInfo.Get()->GetResults(results); -#else - return _M_asyncInfo->GetResults(results); -#endif - } - }; - - extern const __declspec(selectany) WCHAR RuntimeClass_IAsyncOperationWithProgressToAsyncOperationConverter[] = L"_IAsyncOperationWithProgressToAsyncOperationConverter"; - - /// - /// Class _IAsyncOperationWithProgressToAsyncOperationConverter is used to convert an instance of IAsyncOperationWithProgress into IAsyncOperation - /// - template - struct _IAsyncOperationWithProgressToAsyncOperationConverter : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<_Result, _Progress>, - typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type> - { - typedef typename ABI::Windows::Foundation::Internal::GetAbiType*>()))>::type _Result_abi; - - InspectableClass(RuntimeClass_IAsyncOperationWithProgressToAsyncOperationConverter, BaseTrust) - public: - _IAsyncOperationWithProgressToAsyncOperationConverter(ABI::Windows::Foundation::IAsyncOperationWithProgress<_Result, _Progress>* _Operation) : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<_Result, _Progress>, - _Result_abi>(_Operation) {} - public: - virtual STDMETHODIMP GetResults(_Result_abi* results) override { - if (!results) return E_POINTER; -#if _MSC_VER >= 1800 - return _M_asyncInfo.Get()->GetResults(results); -#else - return _M_asyncInfo->GetResults(results); -#endif - } - }; - - extern const __declspec(selectany) WCHAR RuntimeClass_IAsyncActionToAsyncOperationConverter[] = L"_IAsyncActionToAsyncOperationConverter"; - - /// - /// Class _IAsyncActionToAsyncOperationConverter is used to convert an instance of IAsyncAction into IAsyncOperation<_Unit_type> - /// - struct _IAsyncActionToAsyncOperationConverter : - _AsyncInfoImpl - { - InspectableClass(RuntimeClass_IAsyncActionToAsyncOperationConverter, BaseTrust) - public: - _IAsyncActionToAsyncOperationConverter(ABI::Windows::Foundation::IAsyncAction* _Operation) : - _AsyncInfoImpl(_Operation) {} - - public: - virtual STDMETHODIMP GetResults(details::_Unit_type* results) - { - if (!results) return E_POINTER; - // Invoke GetResults on the IAsyncAction to allow exceptions to be thrown to higher layers before returning a dummy value. -#if _MSC_VER >= 1800 - HRESULT hr = _M_asyncInfo.Get()->GetResults(); -#else - HRESULT hr = _M_asyncInfo->GetResults(); -#endif - if (SUCCEEDED(hr)) *results = _Unit_type(); - return hr; - } - }; - - extern const __declspec(selectany) WCHAR RuntimeClass_IAsyncActionWithProgressToAsyncOperationConverter[] = L"_IAsyncActionWithProgressToAsyncOperationConverter"; - - /// - /// Class _IAsyncActionWithProgressToAsyncOperationConverter is used to convert an instance of IAsyncActionWithProgress into IAsyncOperation<_Unit_type> - /// - template - struct _IAsyncActionWithProgressToAsyncOperationConverter : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncActionWithProgressCompletedHandler<_Progress>, - _Unit_type> - { - InspectableClass(RuntimeClass_IAsyncActionWithProgressToAsyncOperationConverter, BaseTrust) - public: - _IAsyncActionWithProgressToAsyncOperationConverter(ABI::Windows::Foundation::IAsyncActionWithProgress<_Progress>* _Action) : - _AsyncInfoImpl, - ABI::Windows::Foundation::IAsyncActionWithProgressCompletedHandler<_Progress>, - _Unit_type>(_Action) {} - public: - virtual STDMETHODIMP GetResults(_Unit_type* results) override - { - if (!results) return E_POINTER; - // Invoke GetResults on the IAsyncActionWithProgress to allow exceptions to be thrown before returning a dummy value. -#if _MSC_VER >= 1800 - HRESULT hr = _M_asyncInfo.Get()->GetResults(); -#else - HRESULT hr = _M_asyncInfo->GetResults(); -#endif - if (SUCCEEDED(hr)) *results = _Unit_type(); - return hr; - } - }; -} - -/// -/// The task_continuation_context class allows you to specify where you would like a continuation to be executed. -/// It is only useful to use this class from a Windows Store app. For non-Windows Store apps, the task continuation's -/// execution context is determined by the runtime, and not configurable. -/// -/// -/**/ -class task_continuation_context : public details::_ContextCallback -{ -public: - - /// - /// Creates the default task continuation context. - /// - /// - /// The default continuation context. - /// - /// - /// The default context is used if you don't specifiy a continuation context when you call the then method. In Windows - /// applications for Windows 7 and below, as well as desktop applications on Windows 8 and higher, the runtime determines where - /// task continuations will execute. However, in a Windows Store app, the default continuation context for a continuation on an - /// apartment aware task is the apartment where then is invoked. - /// An apartment aware task is a task that unwraps a Windows Runtime IAsyncInfo interface, or a task that is descended from such - /// a task. Therefore, if you schedule a continuation on an apartment aware task in a Windows Runtime STA, the continuation will execute in - /// that STA. - /// A continuation on a non-apartment aware task will execute in a context the Runtime chooses. - /// - /**/ - static task_continuation_context use_default() - { - // The callback context is created with the context set to CaptureDeferred and resolved when it is used in .then() - return task_continuation_context(true); // sets it to deferred, is resolved in the constructor of _ContinuationTaskHandle - } - - /// - /// Creates a task continuation context which allows the Runtime to choose the execution context for a continuation. - /// - /// - /// A task continuation context that represents an arbitrary location. - /// - /// - /// When this continuation context is used the continuation will execute in a context the runtime chooses even if the antecedent task - /// is apartment aware. - /// use_arbitrary can be used to turn off the default behavior for a continuation on an apartment - /// aware task created in an STA. - /// This method is only available to Windows Store apps. - /// - /**/ - static task_continuation_context use_arbitrary() - { - task_continuation_context _Arbitrary(true); - _Arbitrary._Resolve(false); - return _Arbitrary; - } - - /// - /// Returns a task continuation context object that represents the current execution context. - /// - /// - /// The current execution context. - /// - /// - /// This method captures the caller's Windows Runtime context so that continuations can be executed in the right apartment. - /// The value returned by use_current can be used to indicate to the Runtime that the continuation should execute in - /// the captured context (STA vs MTA) regardless of whether or not the antecedent task is apartment aware. An apartment aware task is - /// a task that unwraps a Windows Runtime IAsyncInfo interface, or a task that is descended from such a task. - /// This method is only available to Windows Store apps. - /// - /**/ - static task_continuation_context use_current() - { - task_continuation_context _Current(true); - _Current._Resolve(true); - return _Current; - } - -private: - - task_continuation_context(bool _DeferCapture = false) : details::_ContextCallback(_DeferCapture) - { - } -}; - -#if _MSC_VER >= 1800 -class task_options; -namespace details -{ - struct _Internal_task_options - { - bool _M_hasPresetCreationCallstack; - _TaskCreationCallstack _M_presetCreationCallstack; - - void _set_creation_callstack(const _TaskCreationCallstack &_callstack) - { - _M_hasPresetCreationCallstack = true; - _M_presetCreationCallstack = _callstack; - } - _Internal_task_options() - { - _M_hasPresetCreationCallstack = false; - } - }; - - inline _Internal_task_options &_get_internal_task_options(task_options &options); - inline const _Internal_task_options &_get_internal_task_options(const task_options &options); -} -/// -/// Represents the allowed options for creating a task -/// -class task_options -{ -public: - - - /// - /// Default list of task creation options - /// - task_options() - : _M_Scheduler(Concurrency::get_ambient_scheduler()), - _M_CancellationToken(Concurrency::cancellation_token::none()), - _M_ContinuationContext(task_continuation_context::use_default()), - _M_HasCancellationToken(false), - _M_HasScheduler(false) - { - } - - /// - /// Task option that specify a cancellation token - /// - task_options(Concurrency::cancellation_token _Token) - : _M_Scheduler(Concurrency::get_ambient_scheduler()), - _M_CancellationToken(_Token), - _M_ContinuationContext(task_continuation_context::use_default()), - _M_HasCancellationToken(true), - _M_HasScheduler(false) - { - } - - /// - /// Task option that specify a continuation context. This is valid only for continuations (then) - /// - task_options(task_continuation_context _ContinuationContext) - : _M_Scheduler(Concurrency::get_ambient_scheduler()), - _M_CancellationToken(Concurrency::cancellation_token::none()), - _M_ContinuationContext(_ContinuationContext), - _M_HasCancellationToken(false), - _M_HasScheduler(false) - { - } - - /// - /// Task option that specify a cancellation token and a continuation context. This is valid only for continuations (then) - /// - task_options(Concurrency::cancellation_token _Token, task_continuation_context _ContinuationContext) - : _M_Scheduler(Concurrency::get_ambient_scheduler()), - _M_CancellationToken(_Token), - _M_ContinuationContext(_ContinuationContext), - _M_HasCancellationToken(false), - _M_HasScheduler(false) - { - } - - /// - /// Task option that specify a scheduler with shared lifetime - /// - template - task_options(std::shared_ptr<_SchedType> _Scheduler) - : _M_Scheduler(std::move(_Scheduler)), - _M_CancellationToken(cancellation_token::none()), - _M_ContinuationContext(task_continuation_context::use_default()), - _M_HasCancellationToken(false), - _M_HasScheduler(true) - { - } - - /// - /// Task option that specify a scheduler reference - /// - task_options(Concurrency::scheduler_interface& _Scheduler) - : _M_Scheduler(&_Scheduler), - _M_CancellationToken(Concurrency::cancellation_token::none()), - _M_ContinuationContext(task_continuation_context::use_default()), - _M_HasCancellationToken(false), - _M_HasScheduler(true) - { - } - - /// - /// Task option that specify a scheduler - /// - task_options(Concurrency::scheduler_ptr _Scheduler) - : _M_Scheduler(std::move(_Scheduler)), - _M_CancellationToken(Concurrency::cancellation_token::none()), - _M_ContinuationContext(task_continuation_context::use_default()), - _M_HasCancellationToken(false), - _M_HasScheduler(true) - { - } - - /// - /// Task option copy constructor - /// - task_options(const task_options& _TaskOptions) - : _M_Scheduler(_TaskOptions.get_scheduler()), - _M_CancellationToken(_TaskOptions.get_cancellation_token()), - _M_ContinuationContext(_TaskOptions.get_continuation_context()), - _M_HasCancellationToken(_TaskOptions.has_cancellation_token()), - _M_HasScheduler(_TaskOptions.has_scheduler()) - { - } - - /// - /// Sets the given token in the options - /// - void set_cancellation_token(Concurrency::cancellation_token _Token) - { - _M_CancellationToken = _Token; - _M_HasCancellationToken = true; - } - - /// - /// Sets the given continuation context in the options - /// - void set_continuation_context(task_continuation_context _ContinuationContext) - { - _M_ContinuationContext = _ContinuationContext; - } - - /// - /// Indicates whether a cancellation token was specified by the user - /// - bool has_cancellation_token() const - { - return _M_HasCancellationToken; - } - - /// - /// Returns the cancellation token - /// - Concurrency::cancellation_token get_cancellation_token() const - { - return _M_CancellationToken; - } - - /// - /// Returns the continuation context - /// - task_continuation_context get_continuation_context() const - { - return _M_ContinuationContext; - } - - /// - /// Indicates whether a scheduler n was specified by the user - /// - bool has_scheduler() const - { - return _M_HasScheduler; - } - - /// - /// Returns the scheduler - /// - Concurrency::scheduler_ptr get_scheduler() const - { - return _M_Scheduler; - } - -private: - - task_options const& operator=(task_options const& _Right); - friend details::_Internal_task_options &details::_get_internal_task_options(task_options &); - friend const details::_Internal_task_options &details::_get_internal_task_options(const task_options &); - - Concurrency::scheduler_ptr _M_Scheduler; - Concurrency::cancellation_token _M_CancellationToken; - task_continuation_context _M_ContinuationContext; - details::_Internal_task_options _M_InternalTaskOptions; - bool _M_HasCancellationToken; - bool _M_HasScheduler; -}; -#endif - -namespace details -{ -#if _MSC_VER >= 1800 - inline _Internal_task_options & _get_internal_task_options(task_options &options) - { - return options._M_InternalTaskOptions; - } - inline const _Internal_task_options & _get_internal_task_options(const task_options &options) - { - return options._M_InternalTaskOptions; - } -#endif - struct _Task_impl_base; - template struct _Task_impl; - - template - struct _Task_ptr - { - typedef std::shared_ptr<_Task_impl<_ReturnType>> _Type; -#if _MSC_VER >= 1800 - static _Type _Make(Concurrency::details::_CancellationTokenState * _Ct, Concurrency::scheduler_ptr _Scheduler_arg) { return std::make_shared<_Task_impl<_ReturnType>>(_Ct, _Scheduler_arg); } -#else - static _Type _Make(Concurrency::details::_CancellationTokenState * _Ct) { return std::make_shared<_Task_impl<_ReturnType>>(_Ct); } -#endif - }; -#if _MSC_VER >= 1800 - typedef Concurrency::details::_TaskCollection_t::_TaskProcHandle_t _UnrealizedChore_t; - typedef _UnrealizedChore_t _UnrealizedChore; - typedef Concurrency::extensibility::scoped_critical_section_t scoped_lock; - typedef Concurrency::extensibility::critical_section_t critical_section; - typedef Concurrency::details::atomic_size_t atomic_size_t; -#else - typedef Concurrency::details::_UnrealizedChore _UnrealizedChore; - typedef Concurrency::critical_section::scoped_lock scoped_lock; - typedef Concurrency::critical_section critical_section; - typedef volatile size_t atomic_size_t; -#endif - typedef std::shared_ptr<_Task_impl_base> _Task_ptr_base; - // The weak-typed base task handler for continuation tasks. - struct _ContinuationTaskHandleBase : _UnrealizedChore - { - _ContinuationTaskHandleBase * _M_next; - task_continuation_context _M_continuationContext; - bool _M_isTaskBasedContinuation; - - // This field gives inlining scheduling policy for current chore. - _TaskInliningMode _M_inliningMode; - - virtual _Task_ptr_base _GetTaskImplBase() const = 0; - - _ContinuationTaskHandleBase() : - _M_next(nullptr), _M_isTaskBasedContinuation(false), _M_continuationContext(task_continuation_context::use_default()), _M_inliningMode(Concurrency::details::_NoInline) - { - } - virtual ~_ContinuationTaskHandleBase() {} - }; -#if _MSC_VER >= 1800 -#if _PPLTASK_ASYNC_LOGGING - // GUID used for identifying causality logs from PPLTask - const ::Platform::Guid _PPLTaskCausalityPlatformID(0x7A76B220, 0xA758, 0x4E6E, 0xB0, 0xE0, 0xD7, 0xC6, 0xD7, 0x4A, 0x88, 0xFE); - - __declspec(selectany) volatile long _isCausalitySupported = 0; - - inline bool _IsCausalitySupported() - { -#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) - if (_isCausalitySupported == 0) - { - long _causality = 1; - OSVERSIONINFOEX _osvi = {}; - _osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); - - // The Causality is supported on Windows version higher than Windows 8 - _osvi.dwMajorVersion = 6; - _osvi.dwMinorVersion = 3; - - DWORDLONG _conditionMask = 0; - VER_SET_CONDITION(_conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL); - VER_SET_CONDITION(_conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL); - - if (::VerifyVersionInfo(&_osvi, VER_MAJORVERSION | VER_MINORVERSION, _conditionMask)) - { - _causality = 2; - } - - _isCausalitySupported = _causality; - return _causality == 2; - } - - return _isCausalitySupported == 2 ? true : false; -#else - return true; -#endif - } - - // Stateful logger rests inside task_impl_base. - struct _TaskEventLogger - { - _Task_impl_base *_M_task; - bool _M_scheduled; - bool _M_taskPostEventStarted; - - // Log before scheduling task - void _LogScheduleTask(bool _isContinuation) - { - if (details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationCreation(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - _PPLTaskCausalityPlatformID, reinterpret_cast(_M_task), - _isContinuation ? "Concurrency::PPLTask::ScheduleContinuationTask" : "Concurrency::PPLTask::ScheduleTask", 0); - _M_scheduled = true; - } - } - - // It will log the cancel event but not canceled state. _LogTaskCompleted will log the terminal state, which includes cancel state. - void _LogCancelTask() - { - if (details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationRelation(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Important, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - _PPLTaskCausalityPlatformID, reinterpret_cast(_M_task), ::Windows::Foundation::Diagnostics::CausalityRelation::Cancel); - - } - } - - // Log when task reaches terminal state. Note: the task can reach a terminal state (by cancellation or exception) without having run - void _LogTaskCompleted(); - - // Log when task body (which includes user lambda and other scheduling code) begin to run - void _LogTaskExecutionStarted() { } - - // Log when task body finish executing - void _LogTaskExecutionCompleted() - { - if (_M_taskPostEventStarted && details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::CompletionNotification); - } - } - - // Log right before user lambda being invoked - void _LogWorkItemStarted() - { - if (details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkStart(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - _PPLTaskCausalityPlatformID, reinterpret_cast(_M_task), ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::Execution); - } - } - - // Log right after user lambda being invoked - void _LogWorkItemCompleted() - { - if (details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::Execution); - - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceSynchronousWorkStart(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - _PPLTaskCausalityPlatformID, reinterpret_cast(_M_task), ::Windows::Foundation::Diagnostics::CausalitySynchronousWork::CompletionNotification); - _M_taskPostEventStarted = true; - } - } - - _TaskEventLogger(_Task_impl_base *_task) : _M_task(_task) - { - _M_scheduled = false; - _M_taskPostEventStarted = false; - } - }; - - // Exception safe logger for user lambda - struct _TaskWorkItemRAIILogger - { - _TaskEventLogger &_M_logger; - _TaskWorkItemRAIILogger(_TaskEventLogger &_taskHandleLogger) : _M_logger(_taskHandleLogger) - { - _M_logger._LogWorkItemStarted(); - } - - ~_TaskWorkItemRAIILogger() - { - _M_logger._LogWorkItemCompleted(); - } - _TaskWorkItemRAIILogger &operator =(const _TaskWorkItemRAIILogger &); // cannot be assigned - }; - -#else - inline void _LogCancelTask(_Task_impl_base *) {} - struct _TaskEventLogger - { - void _LogScheduleTask(bool) {} - void _LogCancelTask() {} - void _LogWorkItemStarted() {} - void _LogWorkItemCompleted() {} - void _LogTaskExecutionStarted() {} - void _LogTaskExecutionCompleted() {} - void _LogTaskCompleted() {} - _TaskEventLogger(_Task_impl_base *) {} - }; - struct _TaskWorkItemRAIILogger - { - _TaskWorkItemRAIILogger(_TaskEventLogger &) {} - }; -#endif -#endif - /// - /// The _PPLTaskHandle is the strong-typed task handle base. All user task functions need to be wrapped in this task handler - /// to be executable by PPL. By deriving from a different _BaseTaskHandle, it can be used for both initial tasks and continuation tasks. - /// For initial tasks, _PPLTaskHandle will be derived from _UnrealizedChore, and for continuation tasks, it will be derived from - /// _ContinuationTaskHandleBase. The life time of the _PPLTaskHandle object is be managed by runtime if task handle is scheduled. - /// - /// - /// The result type of the _Task_impl. - /// - /// - /// The derived task handle class. The operator () needs to be implemented. - /// - /// - /// The base class from which _PPLTaskHandle should be derived. This is either _UnrealizedChore or _ContinuationTaskHandleBase. - /// - template - struct _PPLTaskHandle : _BaseTaskHandle - { - _PPLTaskHandle(const typename _Task_ptr<_ReturnType>::_Type & _PTask) : _M_pTask(_PTask) - { -#if _MSC_VER < 1800 - m_pFunction = reinterpret_cast (&_UnrealizedChore::_InvokeBridge<_PPLTaskHandle>); - _SetRuntimeOwnsLifetime(true); -#endif - } - virtual ~_PPLTaskHandle() { -#if _MSC_VER >= 1800 - // Here is the sink of all task completion code paths - _M_pTask->_M_taskEventLogger._LogTaskCompleted(); -#endif - } -#if _MSC_VER >= 1800 - virtual void invoke() const -#else - void operator()() const -#endif - { - // All exceptions should be rethrown to finish cleanup of the task collection. They will be caught and handled - // by the runtime. - _CONCRT_ASSERT(_M_pTask != nullptr); - if (!_M_pTask->_TransitionedToStarted()) { -#if _MSC_VER >= 1800 - static_cast(this)->_SyncCancelAndPropagateException(); -#endif - return; - } -#if _MSC_VER >= 1800 - _M_pTask->_M_taskEventLogger._LogTaskExecutionStarted(); -#endif - try - { - // All derived task handle must implement this contract function. - static_cast(this)->_Perform(); - } - catch (const Concurrency::task_canceled &) - { - _M_pTask->_Cancel(true); -#if _MSC_VER < 1800 - throw; -#endif - } - catch (const Concurrency::details::_Interruption_exception &) - { - _M_pTask->_Cancel(true); -#if _MSC_VER < 1800 - throw; -#endif - } - catch (IRestrictedErrorInfo*& _E) - { - _M_pTask->_CancelWithException(_E); -#if _MSC_VER < 1800 - throw; -#endif - } - catch (...) - { - _M_pTask->_CancelWithException(std::current_exception()); -#if _MSC_VER < 1800 - throw; -#endif - } -#if _MSC_VER >= 1800 - _M_pTask->_M_taskEventLogger._LogTaskExecutionCompleted(); -#endif - } - - // Cast _M_pTask pointer to "type-less" _Task_impl_base pointer, which can be used in _ContinuationTaskHandleBase. - // The return value should be automatically optimized by R-value ref. - _Task_ptr_base _GetTaskImplBase() const - { - return _M_pTask; - } - - typename _Task_ptr<_ReturnType>::_Type _M_pTask; - - private: - _PPLTaskHandle const & operator=(_PPLTaskHandle const&); // no assignment operator - }; - - /// - /// The base implementation of a first-class task. This class contains all the non-type specific - /// implementation details of the task. - /// - /**/ - struct _Task_impl_base - { - enum _TaskInternalState - { - // Tracks the state of the task, rather than the task collection on which the task is scheduled - _Created, - _Started, - _PendingCancel, - _Completed, - _Canceled - }; -#if _MSC_VER >= 1800 - _Task_impl_base(Concurrency::details::_CancellationTokenState * _PTokenState, Concurrency::scheduler_ptr _Scheduler_arg) - : _M_TaskState(_Created), - _M_fFromAsync(false), _M_fUnwrappedTask(false), - _M_pRegistration(nullptr), _M_Continuations(nullptr), _M_TaskCollection(_Scheduler_arg), - _M_taskEventLogger(this) -#else - _Task_impl_base(Concurrency::details::_CancellationTokenState * _PTokenState) : _M_TaskState(_Created), - _M_fFromAsync(false), _M_fRuntimeAggregate(false), _M_fUnwrappedTask(false), - _M_pRegistration(nullptr), _M_Continuations(nullptr), _M_pTaskCollection(nullptr), - _M_pTaskCreationAddressHint(nullptr) -#endif - { - // Set cancelation token - _M_pTokenState = _PTokenState; - _CONCRT_ASSERT(_M_pTokenState != nullptr); - if (_M_pTokenState != Concurrency::details::_CancellationTokenState::_None()) - _M_pTokenState->_Reference(); - - } - - virtual ~_Task_impl_base() - { - _CONCRT_ASSERT(_M_pTokenState != nullptr); - if (_M_pTokenState != Concurrency::details::_CancellationTokenState::_None()) - { - _M_pTokenState->_Release(); - } -#if _MSC_VER < 1800 - if (_M_pTaskCollection != nullptr) - { - _M_pTaskCollection->_Release(); - _M_pTaskCollection = nullptr; - } -#endif - } - - task_status _Wait() - { - bool _DoWait = true; - - if (_IsNonBlockingThread()) - { - // In order to prevent Windows Runtime STA threads from blocking the UI, calling task.wait() task.get() is illegal - // if task has not been completed. - if (!_IsCompleted() && !_IsCanceled()) - { - throw Concurrency::invalid_operation("Illegal to wait on a task in a Windows Runtime STA"); - } - else - { - // Task Continuations are 'scheduled' *inside* the chore that is executing on the ancestors's task group. If a continuation - // needs to be marshalled to a different apartment, instead of scheduling, we make a synchronous cross apartment COM - // call to execute the continuation. If it then happens to do something which waits on the ancestor (say it calls .get(), which - // task based continuations are wont to do), waiting on the task group results in on the chore that is making this - // synchronous callback, which causes a deadlock. To avoid this, we test the state ancestor's event , and we will NOT wait on - // if it has finished execution (which means now we are on the inline synchronous callback). - _DoWait = false; - } - } - if (_DoWait) - { -#if _MSC_VER < 1800 - // Wait for the task to be actually scheduled, otherwise the underlying task collection - // might not be created yet. If we don't wait, we will miss the chance to inline this task. - _M_Scheduled.wait(); - - - // A PPL task created by a task_completion_event does not have an underlying TaskCollection. For - // These tasks, a call to wait should wait for the event to be set. The TaskCollection must either - // be nullptr or allocated (the setting of _M_Scheduled) ensures that. -#endif - // If this task was created from a Windows Runtime async operation, do not attempt to inline it. The - // async operation will take place on a thread in the appropriate apartment Simply wait for the completed - // event to be set. -#if _MSC_VER >= 1800 - if (_M_fFromAsync) -#else - if ((_M_pTaskCollection == nullptr) || _M_fFromAsync) -#endif - { -#if _MSC_VER >= 1800 - _M_TaskCollection._Wait(); -#else - _M_Completed.wait(); -#endif - } - else - { - // Wait on the task collection to complete. The task collection is guaranteed to still be - // valid since the task must be still within scope so that the _Task_impl_base destructor - // has not yet been called. This call to _Wait potentially inlines execution of work. - try - { - // Invoking wait on a task collection resets the state of the task collection. This means that - // if the task collection itself were canceled, or had encountered an exception, only the first - // call to wait will receive this status. However, both cancellation and exceptions flowing through - // tasks set state in the task impl itself. - - // When it returns cancelled, either work chore or the cancel thread should already have set task's state - // properly -- cancelled state or completed state (because there was no interruption point). - // For tasks with unwrapped tasks, we should not change the state of current task, since the unwrapped task are still running. -#if _MSC_VER >= 1800 - _M_TaskCollection._RunAndWait(); -#else - _M_pTaskCollection->_RunAndWait(); -#endif - } - catch (Concurrency::details::_Interruption_exception&) - { - // The _TaskCollection will never be an interruption point since it has a none token. - _CONCRT_ASSERT(false); - } - catch (Concurrency::task_canceled&) - { - // task_canceled is a special exception thrown by cancel_current_task. The spec states that cancel_current_task - // must be called from code that is executed within the task (throwing it from parallel work created by and waited - // upon by the task is acceptable). We can safely assume that the task wrapper _PPLTaskHandle::operator() has seen - // the exception and canceled the task. Swallow the exception here. - _CONCRT_ASSERT(_IsCanceled()); - } - catch (IRestrictedErrorInfo*& _E) - { - // Its possible the task body hasn't seen the exception, if so we need to cancel with exception here. - if(!_HasUserException()) - { - _CancelWithException(_E); - } - // Rethrow will mark the exception as observed. - _M_exceptionHolder->_RethrowUserException(); - } - catch (...) - { - // Its possible the task body hasn't seen the exception, if so we need to cancel with exception here. - if (!_HasUserException()) - { - _CancelWithException(std::current_exception()); - } - // Rethrow will mark the exception as observed. - _M_exceptionHolder->_RethrowUserException(); - } - - // If the lambda body for this task (executed or waited upon in _RunAndWait above) happened to return a task - // which is to be unwrapped and plumbed to the output of this task, we must not only wait on the lambda body, we must - // wait on the **INNER** body. It is in theory possible that we could inline such if we plumb a series of things through; - // however, this takes the tact of simply waiting upon the completion signal. - if (_M_fUnwrappedTask) - { -#if _MSC_VER >= 1800 - _M_TaskCollection._Wait(); -#else - _M_Completed.wait(); -#endif - } - } - } - - if (_HasUserException()) - { - _M_exceptionHolder->_RethrowUserException(); - } - else if (_IsCanceled()) - { - return Concurrency::canceled; - } - _CONCRT_ASSERT(_IsCompleted()); - return Concurrency::completed; - } - /// - /// Requests cancellation on the task and schedules continuations if the task can be transitioned to a terminal state. - /// - /// - /// Set to true if the cancel takes place as a result of the task body encountering an exception, or because an ancestor or task_completion_event the task - /// was registered with were canceled with an exception. A synchronous cancel is one that assures the task could not be running on a different thread at - /// the time the cancellation is in progress. An asynchronous cancel is one where the thread performing the cancel has no control over the thread that could - /// be executing the task, that is the task could execute concurrently while the cancellation is in progress. - /// - /// - /// Whether an exception other than the internal runtime cancellation exceptions caused this cancellation. - /// - /// - /// Whether this exception came from an ancestor task or a task_completion_event as opposed to an exception that was encountered by the task itself. Only valid when - /// _UserException is set to true. - /// - /// - /// The exception holder that represents the exception. Only valid when _UserException is set to true. - /// - virtual bool _CancelAndRunContinuations(bool _SynchronousCancel, bool _UserException, bool _PropagatedFromAncestor, const std::shared_ptr<_ExceptionHolder>& _ExHolder) = 0; - - bool _Cancel(bool _SynchronousCancel) - { - // Send in a dummy value for exception. It is not used when the first parameter is false. - return _CancelAndRunContinuations(_SynchronousCancel, false, false, _M_exceptionHolder); - } - - bool _CancelWithExceptionHolder(const std::shared_ptr<_ExceptionHolder>& _ExHolder, bool _PropagatedFromAncestor) - { - // This task was canceled because an ancestor task encountered an exception. - return _CancelAndRunContinuations(true, true, _PropagatedFromAncestor, _ExHolder); - } - - bool _CancelWithException(IRestrictedErrorInfo*& _Exception) - { - // This task was canceled because the task body encountered an exception. - _CONCRT_ASSERT(!_HasUserException()); -#if _MSC_VER >= 1800 - return _CancelAndRunContinuations(true, true, false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationCallstack())); -#else - return _CancelAndRunContinuations(true, true, false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationAddressHint())); -#endif - } - bool _CancelWithException(const std::exception_ptr& _Exception) - { - // This task was canceled because the task body encountered an exception. - _CONCRT_ASSERT(!_HasUserException()); -#if _MSC_VER >= 1800 - return _CancelAndRunContinuations(true, true, false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationCallstack())); -#else - return _CancelAndRunContinuations(true, true, false, std::make_shared<_ExceptionHolder>(_Exception, _GetTaskCreationAddressHint())); -#endif - } - -#if _MSC_VER >= 1800 - void _RegisterCancellation(std::weak_ptr<_Task_impl_base> _WeakPtr) -#else - void _RegisterCancellation() -#endif - { - _CONCRT_ASSERT(Concurrency::details::_CancellationTokenState::_IsValid(_M_pTokenState)); -#if _MSC_VER >= 1800 - auto _CancellationCallback = [_WeakPtr](){ - // Taking ownership of the task prevents dead lock during destruction - // if the destructor waits for the cancellations to be finished - auto _task = _WeakPtr.lock(); - if (_task != nullptr) - _task->_Cancel(false); - }; - - _M_pRegistration = new Concurrency::details::_CancellationTokenCallback(_CancellationCallback); - _M_pTokenState->_RegisterCallback(_M_pRegistration); -#else - _M_pRegistration = _M_pTokenState->_RegisterCallback(reinterpret_cast(&_CancelViaToken), (_Task_impl_base *)this); -#endif - } - - void _DeregisterCancellation() - { - if (_M_pRegistration != nullptr) - { - _M_pTokenState->_DeregisterCallback(_M_pRegistration); - _M_pRegistration->_Release(); - _M_pRegistration = nullptr; - } - } -#if _MSC_VER < 1800 - static void _CancelViaToken(_Task_impl_base *_PImpl) - { - _PImpl->_Cancel(false); - } -#endif - bool _IsCreated() - { - return (_M_TaskState == _Created); - } - - bool _IsStarted() - { - return (_M_TaskState == _Started); - } - - bool _IsPendingCancel() - { - return (_M_TaskState == _PendingCancel); - } - - bool _IsCompleted() - { - return (_M_TaskState == _Completed); - } - - bool _IsCanceled() - { - return (_M_TaskState == _Canceled); - } - - bool _HasUserException() - { - return static_cast(_M_exceptionHolder); - } -#if _MSC_VER < 1800 - void _SetScheduledEvent() - { - _M_Scheduled.set(); - } -#endif - const std::shared_ptr<_ExceptionHolder>& _GetExceptionHolder() - { - _CONCRT_ASSERT(_HasUserException()); - return _M_exceptionHolder; - } - - bool _IsApartmentAware() - { - return _M_fFromAsync; - } - - void _SetAsync(bool _Async = true) - { - _M_fFromAsync = _Async; - } -#if _MSC_VER >= 1800 - _TaskCreationCallstack _GetTaskCreationCallstack() - { - return _M_pTaskCreationCallstack; - } - - void _SetTaskCreationCallstack(const _TaskCreationCallstack &_Callstack) - { - _M_pTaskCreationCallstack = _Callstack; - } -#else - void* _GetTaskCreationAddressHint() - { - return _M_pTaskCreationAddressHint; - } - - void _SetTaskCreationAddressHint(void* _AddressHint) - { - _M_pTaskCreationAddressHint = _AddressHint; - } -#endif - /// - /// Helper function to schedule the task on the Task Collection. - /// - /// - /// The task chore handle that need to be executed. - /// - /// - /// The inlining scheduling policy for current _PTaskHandle. - /// - void _ScheduleTask(_UnrealizedChore * _PTaskHandle, _TaskInliningMode _InliningMode) - { -#if _MSC_VER < 1800 - // Construct the task collection; We use none token to provent it becoming interruption point. - _M_pTaskCollection = Concurrency::details::_AsyncTaskCollection::_NewCollection(Concurrency::details::_CancellationTokenState::_None()); - // _M_pTaskCollection->_ScheduleWithAutoInline will schedule the chore onto AsyncTaskCollection with automatic inlining, in a way that honors cancellation etc. -#endif - try - { -#if _MSC_VER >= 1800 - _M_TaskCollection._ScheduleTask(_PTaskHandle, _InliningMode); -#else - // Do not need to check its returning state, more details please refer to _Wait method. - _M_pTaskCollection->_ScheduleWithAutoInline(_PTaskHandle, _InliningMode); -#endif - } - catch (const Concurrency::task_canceled &) - { - // task_canceled is a special exception thrown by cancel_current_task. The spec states that cancel_current_task - // must be called from code that is executed within the task (throwing it from parallel work created by and waited - // upon by the task is acceptable). We can safely assume that the task wrapper _PPLTaskHandle::operator() has seen - // the exception and canceled the task. Swallow the exception here. - _CONCRT_ASSERT(_IsCanceled()); - } - catch (const Concurrency::details::_Interruption_exception &) - { - // The _TaskCollection will never be an interruption point since it has a none token. - _CONCRT_ASSERT(false); - } - catch (...) - { - // This exception could only have come from within the chore body. It should've been caught - // and the task should be canceled with exception. Swallow the exception here. - _CONCRT_ASSERT(_HasUserException()); - } -#if _MSC_VER < 1800 - // Set the event in case anyone is waiting to notify that this task has been scheduled. In the case where we - // execute the chore inline, the event should be set after the chore has executed, to prevent a different thread - // performing a wait on the task from waiting on the task collection before the chore is actually added to it, - // and thereby returning from the wait() before the chore has executed. - _SetScheduledEvent(); -#endif - } - - /// - /// Function executes a continuation. This function is recorded by a parent task implementation - /// when a continuation is created in order to execute later. - /// - /// - /// The continuation task chore handle that need to be executed. - /// - /**/ - void _RunContinuation(_ContinuationTaskHandleBase * _PTaskHandle) - { - _Task_ptr_base _ImplBase = _PTaskHandle->_GetTaskImplBase(); - if (_IsCanceled() && !_PTaskHandle->_M_isTaskBasedContinuation) - { - if (_HasUserException()) - { - // If the ancestor encountered an exception, transfer the exception to the continuation - // This traverses down the tree to propagate the exception. - _ImplBase->_CancelWithExceptionHolder(_GetExceptionHolder(), true); - } - else - { - // If the ancestor was canceled, then your own execution should be canceled. - // This traverses down the tree to cancel it. - _ImplBase->_Cancel(true); - } - } - else - { - // This can only run when the ancestor has completed or it's a task based continuation that fires when a task is canceled - // (with or without a user exception). - _CONCRT_ASSERT(_IsCompleted() || _PTaskHandle->_M_isTaskBasedContinuation); - -#if _MSC_VER >= 1800 - _CONCRT_ASSERT(!_ImplBase->_IsCanceled()); - return _ImplBase->_ScheduleContinuationTask(_PTaskHandle); -#else - // If it has been canceled here (before starting), do nothing. The guy firing cancel will do the clean up. - if (!_ImplBase->_IsCanceled()) - { - return _ImplBase->_ScheduleContinuationTask(_PTaskHandle); - } -#endif - } - - // If the handle is not scheduled, we need to manually delete it. - delete _PTaskHandle; - } - - // Schedule a continuation to run - void _ScheduleContinuationTask(_ContinuationTaskHandleBase * _PTaskHandle) - { -#if _MSC_VER >= 1800 - _M_taskEventLogger._LogScheduleTask(true); -#endif - // Ensure that the continuation runs in proper context (this might be on a Concurrency Runtime thread or in a different Windows Runtime apartment) - if (_PTaskHandle->_M_continuationContext._HasCapturedContext()) - { - // For those continuations need to be scheduled inside captured context, we will try to apply automatic inlining to their inline modes, - // if they haven't been specified as _ForceInline yet. This change will encourage those continuations to be executed inline so that reduce - // the cost of marshaling. - // For normal continuations we won't do any change here, and their inline policies are completely decided by ._ThenImpl method. - if (_PTaskHandle->_M_inliningMode != Concurrency::details::_ForceInline) - { - _PTaskHandle->_M_inliningMode = Concurrency::details::_DefaultAutoInline; - } - details::_ScheduleFuncWithAutoInline([_PTaskHandle]() -> HRESULT { - // Note that we cannot directly capture "this" pointer, instead, we should use _TaskImplPtr, a shared_ptr to the _Task_impl_base. - // Because "this" pointer will be invalid as soon as _PTaskHandle get deleted. _PTaskHandle will be deleted after being scheduled. - auto _TaskImplPtr = _PTaskHandle->_GetTaskImplBase(); - if (details::_ContextCallback::_CaptureCurrent() == _PTaskHandle->_M_continuationContext) - { - _TaskImplPtr->_ScheduleTask(_PTaskHandle, Concurrency::details::_ForceInline); - } - else - { - // - // It's entirely possible that the attempt to marshal the call into a differing context will fail. In this case, we need to handle - // the exception and mark the continuation as canceled with the appropriate exception. There is one slight hitch to this: - // - // NOTE: COM's legacy behavior is to swallow SEH exceptions and marshal them back as HRESULTS. This will in effect turn an SEH into - // a C++ exception that gets tagged on the task. One unfortunate result of this is that various pieces of the task infrastructure will - // not be in a valid state after this in /EHsc (due to the lack of destructors running, etc...). - // - try - { - // Dev10 compiler needs this! - auto _PTaskHandle1 = _PTaskHandle; - _PTaskHandle->_M_continuationContext._CallInContext([_PTaskHandle1, _TaskImplPtr]() -> HRESULT { - _TaskImplPtr->_ScheduleTask(_PTaskHandle1, Concurrency::details::_ForceInline); - return S_OK; - }); - } - catch (IRestrictedErrorInfo*& _E) - { - _TaskImplPtr->_CancelWithException(_E); - } - catch (...) - { - _TaskImplPtr->_CancelWithException(std::current_exception()); - } - } - return S_OK; - }, _PTaskHandle->_M_inliningMode); - } - else - { - _ScheduleTask(_PTaskHandle, _PTaskHandle->_M_inliningMode); - } - } - - /// - /// Schedule the actual continuation. This will either schedule the function on the continuation task's implementation - /// if the task has completed or append it to a list of functions to execute when the task actually does complete. - /// - /// - /// The input type of the task. - /// - /// - /// The output type of the task. - /// - /**/ - void _ScheduleContinuation(_ContinuationTaskHandleBase * _PTaskHandle) - { - enum { _Nothing, _Schedule, _Cancel, _CancelWithException } _Do = _Nothing; - - // If the task has canceled, cancel the continuation. If the task has completed, execute the continuation right away. - // Otherwise, add it to the list of pending continuations - { - scoped_lock _LockHolder(_M_ContinuationsCritSec); - if (_IsCompleted() || (_IsCanceled() && _PTaskHandle->_M_isTaskBasedContinuation)) - { - _Do = _Schedule; - } - else if (_IsCanceled()) - { - if (_HasUserException()) - { - _Do = _CancelWithException; - } - else - { - _Do = _Cancel; - } - } - else - { - // chain itself on the continuation chain. - _PTaskHandle->_M_next = _M_Continuations; - _M_Continuations = _PTaskHandle; - } - } - - // Cancellation and execution of continuations should be performed after releasing the lock. Continuations off of - // async tasks may execute inline. - switch (_Do) - { - case _Schedule: - { - _PTaskHandle->_GetTaskImplBase()->_ScheduleContinuationTask(_PTaskHandle); - break; - } - case _Cancel: - { - // If the ancestor was canceled, then your own execution should be canceled. - // This traverses down the tree to cancel it. - _PTaskHandle->_GetTaskImplBase()->_Cancel(true); - - delete _PTaskHandle; - break; - } - case _CancelWithException: - { - // If the ancestor encountered an exception, transfer the exception to the continuation - // This traverses down the tree to propagate the exception. - _PTaskHandle->_GetTaskImplBase()->_CancelWithExceptionHolder(_GetExceptionHolder(), true); - - delete _PTaskHandle; - break; - } - case _Nothing: - default: - // In this case, we have inserted continuation to continuation chain, - // nothing more need to be done, just leave. - break; - } - } - - void _RunTaskContinuations() - { - // The link list can no longer be modified at this point, - // since all following up continuations will be scheduled by themselves. - _ContinuationList _Cur = _M_Continuations, _Next; - _M_Continuations = nullptr; - while (_Cur) - { - // Current node might be deleted after running, - // so we must fetch the next first. - _Next = _Cur->_M_next; - _RunContinuation(_Cur); - _Cur = _Next; - } - } - static bool _IsNonBlockingThread() - { - APTTYPE _AptType; - APTTYPEQUALIFIER _AptTypeQualifier; - - HRESULT hr = CoGetApartmentType(&_AptType, &_AptTypeQualifier); - // - // If it failed, it's not a Windows Runtime/COM initialized thread. This is not a failure. - // - if (SUCCEEDED(hr)) - { - switch (_AptType) - { - case APTTYPE_STA: - case APTTYPE_MAINSTA: - return true; - break; - case APTTYPE_NA: - switch (_AptTypeQualifier) - { - // A thread executing in a neutral apartment is either STA or MTA. To find out if this thread is allowed - // to wait, we check the app qualifier. If it is an STA thread executing in a neutral apartment, waiting - // is illegal, because the thread is responsible for pumping messages and waiting on a task could take the - // thread out of circulation for a while. - case APTTYPEQUALIFIER_NA_ON_STA: - case APTTYPEQUALIFIER_NA_ON_MAINSTA: - return true; - break; - } - break; - } - } -#if _UITHREADCTXT_SUPPORT - // This method is used to throw an exepection in _Wait() if called within STA. We - // want the same behavior if _Wait is called on the UI thread. - if (SUCCEEDED(CaptureUiThreadContext(nullptr))) - { - return true; - } -#endif // _UITHREADCTXT_SUPPORT - - return false; - } - - template - static void _AsyncInit(const typename _Task_ptr<_ReturnType>::_Type & _OuterTask, - _AsyncInfoImpl<_OpType, _CompHandlerType, _ResultType>* _AsyncOp) - { - typedef typename ABI::Windows::Foundation::Internal::GetAbiType()))>::type _Result_abi; - // This method is invoked either when a task is created from an existing async operation or - // when a lambda that creates an async operation executes. - - // If the outer task is pending cancel, cancel the async operation before setting the completed handler. The COM reference on - // the IAsyncInfo object will be released when all *references to the operation go out of scope. - - // This assertion uses the existence of taskcollection to determine if the task was created from an event. - // That is no longer valid as even tasks created from a user lambda could have no underlying taskcollection - // when a custom scheduler is used. -#if _MSC_VER < 1800 - _CONCRT_ASSERT(((_OuterTask->_M_pTaskCollection == nullptr) || _OuterTask->_M_fUnwrappedTask) && !_OuterTask->_IsCanceled()); -#endif - - // Pass the shared_ptr by value into the lambda instead of using 'this'. - - _AsyncOp->put_Completed(Microsoft::WRL::Callback<_CompHandlerType>( - [_OuterTask, _AsyncOp](_OpType* _Operation, ABI::Windows::Foundation::AsyncStatus _Status) mutable -> HRESULT - { - HRESULT hr = S_OK; - if (_Status == ABI::Windows::Foundation::AsyncStatus::Canceled) - { - _OuterTask->_Cancel(true); - } - else if (_Status == ABI::Windows::Foundation::AsyncStatus::Error) - { - HRESULT _hr; - Microsoft::WRL::ComPtr pAsyncInfo; - if (SUCCEEDED(hr = _Operation->QueryInterface(pAsyncInfo.GetAddressOf())) && SUCCEEDED(hr = pAsyncInfo->get_ErrorCode(&_hr))) - _OuterTask->_CancelWithException(std::make_exception_ptr(_hr)); - } - else - { - _CONCRT_ASSERT(_Status == ABI::Windows::Foundation::AsyncStatus::Completed); - _NormalizeVoidToUnitType<_Result_abi>::_Type results; - if (SUCCEEDED(hr = _AsyncOp->GetResults(&results))) - _OuterTask->_FinalizeAndRunContinuations(results); - } - // Take away this shared pointers reference on the task instead of waiting for the delegate to be released. It could - // be released on a different thread after a delay, and not releasing the reference here could cause the tasks to hold - // on to resources longer than they should. As an example, without this reset, writing to a file followed by reading from - // it using the Windows Runtime Async APIs causes a sharing violation. - // Using const_cast is the workaround for failed mutable keywords - const_cast<_Task_ptr<_ReturnType>::_Type &>(_OuterTask).reset(); - return hr; - }).Get()); - _OuterTask->_SetUnwrappedAsyncOp(_AsyncOp); - } - template - static void _AsyncInit(const typename _Task_ptr<_ReturnType>::_Type& _OuterTask, const task<_InternalReturnType> & _UnwrappedTask) - { - _CONCRT_ASSERT(_OuterTask->_M_fUnwrappedTask && !_OuterTask->_IsCanceled()); - // - // We must ensure that continuations off _OuterTask (especially exception handling ones) continue to function in the - // presence of an exception flowing out of the inner task _UnwrappedTask. This requires an exception handling continuation - // off the inner task which does the appropriate funnelling to the outer one. We use _Then instead of then to prevent - // the exception from being marked as observed by our internal continuation. This continuation must be scheduled regardless - // of whether or not the _OuterTask task is canceled. - // - _UnwrappedTask._Then([_OuterTask](task<_InternalReturnType> _AncestorTask) -> HRESULT { - - if (_AncestorTask._GetImpl()->_IsCompleted()) - { - _OuterTask->_FinalizeAndRunContinuations(_AncestorTask._GetImpl()->_GetResult()); - } - else - { - _CONCRT_ASSERT(_AncestorTask._GetImpl()->_IsCanceled()); - if (_AncestorTask._GetImpl()->_HasUserException()) - { - // Set _PropagatedFromAncestor to false, since _AncestorTask is not an ancestor of _UnwrappedTask. - // Instead, it is the enclosing task. - _OuterTask->_CancelWithExceptionHolder(_AncestorTask._GetImpl()->_GetExceptionHolder(), false); - } - else - { - _OuterTask->_Cancel(true); - } - } - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr, Concurrency::details::_DefaultAutoInline); -#else - }, nullptr, false, Concurrency::details::_DefaultAutoInline); -#endif - } - -#if _MSC_VER >= 1800 - Concurrency::scheduler_ptr _GetScheduler() const - { - return _M_TaskCollection._GetScheduler(); - } -#else - Concurrency::event _M_Completed; - Concurrency::event _M_Scheduled; -#endif - - // Tracks the internal state of the task - volatile _TaskInternalState _M_TaskState; - // Set to true either if the ancestor task had the flag set to true, or if the lambda that does the work of this task returns an - // async operation or async action that is unwrapped by the runtime. - bool _M_fFromAsync; -#if _MSC_VER < 1800 - // Set to true if we need to marshal the inner parts of an aggregate type like std::vector or std::pair. We only marshal - // the contained T^s if we create the vector or pair, such as on a when_any or a when_all operation. - bool _M_fRuntimeAggregate; -#endif - // Set to true when a continuation unwraps a task or async operation. - bool _M_fUnwrappedTask; - - // An exception thrown by the task body is captured in an exception holder and it is shared with all value based continuations rooted at the task. - // The exception is 'observed' if the user invokes get()/wait() on any of the tasks that are sharing this exception holder. If the exception - // is not observed by the time the internal object owned by the shared pointer destructs, the process will fail fast. - std::shared_ptr<_ExceptionHolder> _M_exceptionHolder; - - typedef _ContinuationTaskHandleBase * _ContinuationList; - - critical_section _M_ContinuationsCritSec; - _ContinuationList _M_Continuations; - - // The cancellation token state. - Concurrency::details::_CancellationTokenState * _M_pTokenState; - - // The registration on the token. - Concurrency::details::_CancellationTokenRegistration * _M_pRegistration; - - // The async task collection wrapper -#if _MSC_VER >= 1800 - Concurrency::details::_TaskCollection_t _M_TaskCollection; - - // Callstack for function call (constructor or .then) that created this task impl. - _TaskCreationCallstack _M_pTaskCreationCallstack; - - _TaskEventLogger _M_taskEventLogger; -#else - Concurrency::details::_AsyncTaskCollection * _M_pTaskCollection; - - // Points to the source code instruction right after the function call (constructor or .then) that created this task impl. - void* _M_pTaskCreationAddressHint; -#endif - - private: - // Must not be copied by value: - _Task_impl_base(const _Task_impl_base&); - _Task_impl_base const & operator=(_Task_impl_base const&); - }; - -#if _MSC_VER >= 1800 -#if _PPLTASK_ASYNC_LOGGING - inline void _TaskEventLogger::_LogTaskCompleted() - { - if (_M_scheduled) - { - ::Windows::Foundation::AsyncStatus _State; - if (_M_task->_IsCompleted()) - _State = ::Windows::Foundation::AsyncStatus::Completed; - else if (_M_task->_HasUserException()) - _State = ::Windows::Foundation::AsyncStatus::Error; - else - _State = ::Windows::Foundation::AsyncStatus::Canceled; - - if (details::_IsCausalitySupported()) - { - ::Windows::Foundation::Diagnostics::AsyncCausalityTracer::TraceOperationCompletion(::Windows::Foundation::Diagnostics::CausalityTraceLevel::Required, ::Windows::Foundation::Diagnostics::CausalitySource::Library, - _PPLTaskCausalityPlatformID, reinterpret_cast(_M_task), _State); - } - } - } -#endif -#endif - - template - struct _Task_impl : public _Task_impl_base - { - typedef ABI::Windows::Foundation::IAsyncInfo _AsyncOperationType; -#if _MSC_VER >= 1800 - _Task_impl(Concurrency::details::_CancellationTokenState * _Ct, Concurrency::scheduler_ptr _Scheduler_arg) - : _Task_impl_base(_Ct, _Scheduler_arg) -#else - _Task_impl(Concurrency::details::_CancellationTokenState * _Ct) : _Task_impl_base(_Ct) -#endif - { - _M_unwrapped_async_op = nullptr; - } - virtual ~_Task_impl() - { - // We must invoke _DeregisterCancellation in the derived class destructor. Calling it in the base class destructor could cause - // a partially initialized _Task_impl to be in the list of registrations for a cancellation token. - _DeregisterCancellation(); - } - virtual bool _CancelAndRunContinuations(bool _SynchronousCancel, bool _UserException, bool _PropagatedFromAncestor, const std::shared_ptr<_ExceptionHolder> & _ExceptionHolder) - { - enum { _Nothing, _RunContinuations, _Cancel } _Do = _Nothing; - { - scoped_lock _LockHolder(_M_ContinuationsCritSec); - if (_UserException) - { - _CONCRT_ASSERT(_SynchronousCancel && !_IsCompleted()); - // If the state is _Canceled, the exception has to be coming from an ancestor. - _CONCRT_ASSERT(!_IsCanceled() || _PropagatedFromAncestor); -#if _MSC_VER < 1800 - // If the state is _Started or _PendingCancel, the exception cannot be coming from an ancestor. - _CONCRT_ASSERT((!_IsStarted() && !_IsPendingCancel()) || !_PropagatedFromAncestor); -#endif - // We should not be canceled with an exception more than once. - _CONCRT_ASSERT(!_HasUserException()); - - if (_M_TaskState == _Canceled) - { - // If the task has finished cancelling there should not be any continuation records in the array. - return false; - } - else - { - _CONCRT_ASSERT(_M_TaskState != _Completed); - _M_exceptionHolder = _ExceptionHolder; - } - } - else - { - // Completed is a non-cancellable state, and if this is an asynchronous cancel, we're unable to do better than the last async cancel - // which is to say, cancellation is already initiated, so return early. - if (_IsCompleted() || _IsCanceled() || (_IsPendingCancel() && !_SynchronousCancel)) - { - _CONCRT_ASSERT(!_IsCompleted() || !_HasUserException()); - return false; - } - _CONCRT_ASSERT(!_SynchronousCancel || !_HasUserException()); - } - -#if _MSC_VER >= 1800 - if (_SynchronousCancel) -#else - if (_SynchronousCancel || _IsCreated()) -#endif - { - // Be aware that this set must be done BEFORE _M_Scheduled being set, or race will happen between this and wait() - _M_TaskState = _Canceled; -#if _MSC_VER < 1800 - _M_Scheduled.set(); -#endif - - // Cancellation completes the task, so all dependent tasks must be run to cancel them - // They are canceled when they begin running (see _RunContinuation) and see that their - // ancestor has been canceled. - _Do = _RunContinuations; - } - else - { -#if _MSC_VER >= 1800 - _CONCRT_ASSERT(!_UserException); - - if (_IsStarted()) - { - // should not initiate cancellation under a lock - _Do = _Cancel; - } - - // The _M_TaskState variable transitions to _Canceled when cancellation is completed (the task is not executing user code anymore). - // In the case of a synchronous cancel, this can happen immediately, whereas with an asynchronous cancel, the task has to move from - // _Started to _PendingCancel before it can move to _Canceled when it is finished executing. - _M_TaskState = _PendingCancel; - - _M_taskEventLogger._LogCancelTask(); - } - } - - switch (_Do) - { - case _Cancel: - { -#else - _CONCRT_ASSERT(_IsStarted() && !_UserException); -#endif - // The _M_TaskState variable transitions to _Canceled when cancellation is completed (the task is not executing user code anymore). - // In the case of a synchronous cancel, this can happen immediately, whereas with an asynchronous cancel, the task has to move from - // _Started to _PendingCancel before it can move to _Canceled when it is finished executing. - _M_TaskState = _PendingCancel; - if (_M_unwrapped_async_op != nullptr) - { - // We will only try to cancel async operation but not unwrapped tasks, since unwrapped tasks cannot be canceled without its token. - if (_M_unwrapped_async_op) _M_unwrapped_async_op->Cancel(); - } -#if _MSC_VER >= 1800 - _M_TaskCollection._Cancel(); - break; -#else - // Optimistic trying for cancelation - if (_M_pTaskCollection != nullptr) - { - _M_pTaskCollection->_Cancel(); - } -#endif - } -#if _MSC_VER < 1800 - } -#endif - - // Only execute continuations and mark the task as completed if we were able to move the task to the _Canceled state. -#if _MSC_VER >= 1800 - case _RunContinuations: - { - _M_TaskCollection._Complete(); -#else - if (_RunContinuations) - { - _M_Completed.set(); -#endif - - if (_M_Continuations) - { - // Scheduling cancellation with automatic inlining. - details::_ScheduleFuncWithAutoInline([=]() -> HRESULT { _RunTaskContinuations(); return S_OK; }, Concurrency::details::_DefaultAutoInline); - } -#if _MSC_VER >= 1800 - break; - } -#endif - } - return true; - } - void _FinalizeAndRunContinuations(_ReturnType _Result) - { - -#if _MSC_VER >= 1800 - _M_Result.Set(_Result); -#else - _M_Result = _Result; - _M_ResultContext = _ResultContext<_ReturnType>::_GetContext(_M_fRuntimeAggregate); -#endif - { - // - // Hold this lock to ensure continuations being concurrently either get added - // to the _M_Continuations vector or wait for the result - // - scoped_lock _LockHolder(_M_ContinuationsCritSec); - - // A task could still be in the _Created state if it was created with a task_completion_event. - // It could also be in the _Canceled state for the same reason. - _CONCRT_ASSERT(!_HasUserException() && !_IsCompleted()); - if (_IsCanceled()) - { - return; - } - - // Always transition to "completed" state, even in the face of unacknowledged pending cancellation - _M_TaskState = _Completed; - } -#if _MSC_VER >= 1800 - _M_TaskCollection._Complete(); -#else - _M_Completed.set(); -#endif - _RunTaskContinuations(); - } - // - // This method is invoked when the starts executing. The task returns early if this method returns true. - // - bool _TransitionedToStarted() - { - scoped_lock _LockHolder(_M_ContinuationsCritSec); -#if _MSC_VER >= 1800 - // Canceled state could only result from antecedent task's canceled state, but that code path will not reach here. - _ASSERT(!_IsCanceled()); - if (_IsPendingCancel()) -#else - if (_IsCanceled()) -#endif - { - return false; - } - _CONCRT_ASSERT(_IsCreated()); - _M_TaskState = _Started; - return true; - } - void _SetUnwrappedAsyncOp(_AsyncOperationType* _AsyncOp) - { - scoped_lock _LockHolder(_M_ContinuationsCritSec); - // Cancel the async operation if the task itself is canceled, since the thread that canceled the task missed it. - if (_IsPendingCancel()) - { - _CONCRT_ASSERT(!_IsCanceled()); - if (_AsyncOp) _AsyncOp->Cancel(); - } - else - { - _M_unwrapped_async_op = _AsyncOp; - } - } -#if _MSC_VER >= 1800 - // Return true if the task has reached a terminal state - bool _IsDone() - { - return _IsCompleted() || _IsCanceled(); - } -#endif - _ReturnType _GetResult() - { -#if _MSC_VER >= 1800 - return _M_Result.Get(); -#else - return _ResultContext<_ReturnType>::_GetValue(_M_Result, _M_ResultContext, _M_fRuntimeAggregate); -#endif - } -#if _MSC_VER >= 1800 - _ResultHolder<_ReturnType> _M_Result; // this means that the result type must have a public default ctor. -#else - _ReturnType _M_Result; // this means that the result type must have a public default ctor. -#endif - Microsoft::WRL::ComPtr<_AsyncOperationType> _M_unwrapped_async_op; -#if _MSC_VER < 1800 - _ContextCallback _M_ResultContext; -#endif - }; - - template - struct _Task_completion_event_impl - { -#if _MSC_VER >= 1800 - private: - _Task_completion_event_impl(const _Task_completion_event_impl&); - _Task_completion_event_impl& operator=(const _Task_completion_event_impl&); - - public: -#endif - typedef std::vector::_Type> _TaskList; - - _Task_completion_event_impl() : _M_fHasValue(false), _M_fIsCanceled(false) - { - } - - bool _HasUserException() - { - return _M_exceptionHolder != nullptr; - } - - ~_Task_completion_event_impl() - { - for (auto _TaskIt = _M_tasks.begin(); _TaskIt != _M_tasks.end(); ++_TaskIt) - { - _CONCRT_ASSERT(!_M_fHasValue && !_M_fIsCanceled); - // Cancel the tasks since the event was never signaled or canceled. - (*_TaskIt)->_Cancel(true); - } - } - - // We need to protect the loop over the array, so concurrent_vector would not have helped - _TaskList _M_tasks; - critical_section _M_taskListCritSec; -#if _MSC_VER >= 1800 - _ResultHolder<_ResultType> _M_value; -#else - _ResultType _M_value; -#endif - std::shared_ptr<_ExceptionHolder> _M_exceptionHolder; - bool _M_fHasValue; - bool _M_fIsCanceled; - }; - - // Utility method for dealing with void functions - inline std::function _MakeVoidToUnitFunc(const std::function& _Func) - { - return [=](_Unit_type* retVal) -> HRESULT { HRESULT hr = _Func(); *retVal = _Unit_type(); return hr; }; - } - - template - std::function _MakeUnitToTFunc(const std::function& _Func) - { - return [=](_Unit_type, _Type* retVal) -> HRESULT { HRESULT hr = _Func(retVal); return hr; }; - } - - template - std::function _MakeTToUnitFunc(const std::function& _Func) - { - return[=](_Type t, _Unit_type* retVal) -> HRESULT { HRESULT hr = _Func(t); *retVal = _Unit_type(); return hr; }; - } - - inline std::function _MakeUnitToUnitFunc(const std::function& _Func) - { - return [=](_Unit_type, _Unit_type* retVal) -> HRESULT { HRESULT hr = _Func(); *retVal = _Unit_type(); return hr; }; - } -} - - -/// -/// The task_completion_event class allows you to delay the execution of a task until a condition is satisfied, -/// or start a task in response to an external event. -/// -/// -/// The result type of this task_completion_event class. -/// -/// -/// Use a task created from a task completion event when your scenario requires you to create a task that will complete, and -/// thereby have its continuations scheduled for execution, at some point in the future. The task_completion_event must -/// have the same type as the task you create, and calling the set method on the task completion event with a value of that type -/// will cause the associated task to complete, and provide that value as a result to its continuations. -/// If the task completion event is never signaled, any tasks created from it will be canceled when it is destructed. -/// task_completion_event behaves like a smart pointer, and should be passed by value. -/// -/// -/**/ -template -class task_completion_event -{ -public: - /// - /// Constructs a task_completion_event object. - /// - /**/ - task_completion_event() : _M_Impl(std::make_shared>()) - { - } - - /// - /// Sets the task completion event. - /// - /// - /// The result to set this event with. - /// - /// - /// The method returns true if it was successful in setting the event. It returns false if the event is already set. - /// - /// - /// In the presence of multiple or concurrent calls to set, only the first call will succeed and its result (if any) will be stored in the - /// task completion event. The remaining sets are ignored and the method will return false. When you set a task completion event, all the - /// tasks created from that event will immediately complete, and its continuations, if any, will be scheduled. Task completion objects that have - /// a other than void will pass the value to their continuations. - /// - /**/ - bool set(_ResultType _Result) const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - // Subsequent sets are ignored. This makes races to set benign: the first setter wins and all others are ignored. - if (_IsTriggered()) - { - return false; - } - - _TaskList _Tasks; - bool _RunContinuations = false; - { - details::scoped_lock _LockHolder(_M_Impl->_M_taskListCritSec); - - if (!_IsTriggered()) - { -#if _MSC_VER >= 1800 - _M_Impl->_M_value.Set(_Result); -#else - _M_Impl->_M_value = _Result; -#endif - _M_Impl->_M_fHasValue = true; - - _Tasks.swap(_M_Impl->_M_tasks); - _RunContinuations = true; - } - } - - if (_RunContinuations) - { - for (auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt) - { -#if _MSC_VER >= 1800 - // If current task was cancelled by a cancellation_token, it would be in cancel pending state. - if ((*_TaskIt)->_IsPendingCancel()) - (*_TaskIt)->_Cancel(true); - else - { - // Tasks created with task_completion_events can be marked as async, (we do this in when_any and when_all - // if one of the tasks involved is an async task). Since continuations of async tasks can execute inline, we - // need to run continuations after the lock is released. - (*_TaskIt)->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get()); - } -#else - // Tasks created with task_completion_events can be marked as async, (we do this in when_any and when_all - // if one of the tasks involved is an async task). Since continuations of async tasks can execute inline, we - // need to run continuations after the lock is released. - (*_TaskIt)->_FinalizeAndRunContinuations(_M_Impl->_M_value); -#endif - } - if (_M_Impl->_HasUserException()) - { - _M_Impl->_M_exceptionHolder.reset(); - } - return true; - } - - return false; - } -#if _MSC_VER >= 1800 - - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - bool set_exception(_E _Except) const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - // It is important that _CAPTURE_CALLSTACK() evaluate to the instruction after the call instruction for set_exception. - return _Cancel(std::make_exception_ptr(_Except), _CAPTURE_CALLSTACK()); - } -#endif - - /// - /// Propagates an exception to all tasks associated with this event. - /// - /// - /// The exception_ptr that indicates the exception to set this event with. - /// - /**/ - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - bool set_exception(std::exception_ptr _ExceptionPtr) const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - // It is important that _ReturnAddress() evaluate to the instruction after the call instruction for set_exception. -#if _MSC_VER >= 1800 - return _Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK()); -#else - return _Cancel(_ExceptionPtr, _ReturnAddress()); -#endif - } - - /// - /// Internal method to cancel the task_completion_event. Any task created using this event will be marked as canceled if it has - /// not already been set. - /// - bool _Cancel() const - { - // Cancel with the stored exception if one exists. - return _CancelInternal(); - } - - /// - /// Internal method to cancel the task_completion_event with the exception provided. Any task created using this event will be canceled - /// with the same exception. - /// - template -#if _MSC_VER >= 1800 - bool _Cancel(_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint = details::_TaskCreationCallstack()) const -#else - bool _Cancel(_ExHolderType _ExHolder, void* _SetExceptionAddressHint = nullptr) const -#endif - { - (void)_SetExceptionAddressHint; - bool _Canceled; -#if _MSC_VER >= 1800 - if(_StoreException(_ExHolder, _SetExceptionAddressHint)) -#else - if (_StoreException(_ExHolder)) -#endif - { - _Canceled = _CancelInternal(); - _CONCRT_ASSERT(_Canceled); - } - else - { - _Canceled = false; - } - return _Canceled; - } - - /// - /// Internal method that stores an exception in the task completion event. This is used internally by when_any. - /// Note, this does not cancel the task completion event. A task completion event with a stored exception - /// can bet set() successfully. If it is canceled, it will cancel with the stored exception, if one is present. - /// - template -#if _MSC_VER >= 1800 - bool _StoreException(_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint = details::_TaskCreationCallstack()) const -#else - bool _StoreException(_ExHolderType _ExHolder, void* _SetExceptionAddressHint = nullptr) const -#endif - { - details::scoped_lock _LockHolder(_M_Impl->_M_taskListCritSec); - if (!_IsTriggered() && !_M_Impl->_HasUserException()) - { - // Create the exception holder only if we have ensured there we will be successful in setting it onto the - // task completion event. Failing to do so will result in an unobserved task exception. - _M_Impl->_M_exceptionHolder = _ToExceptionHolder(_ExHolder, _SetExceptionAddressHint); - return true; - } - return false; - } - - /// - /// Tests whether current event has been either Set, or Canceled. - /// - bool _IsTriggered() const - { - return _M_Impl->_M_fHasValue || _M_Impl->_M_fIsCanceled; - } - -private: - -#if _MSC_VER >= 1800 - static std::shared_ptr _ToExceptionHolder(const std::shared_ptr& _ExHolder, const details::_TaskCreationCallstack&) -#else - static std::shared_ptr _ToExceptionHolder(const std::shared_ptr& _ExHolder, void*) -#endif - { - return _ExHolder; - } - -#if _MSC_VER >= 1800 - static std::shared_ptr _ToExceptionHolder(std::exception_ptr _ExceptionPtr, const details::_TaskCreationCallstack &_SetExceptionAddressHint) -#else - static std::shared_ptr _ToExceptionHolder(std::exception_ptr _ExceptionPtr, void* _SetExceptionAddressHint) -#endif - { - return std::make_shared(_ExceptionPtr, _SetExceptionAddressHint); - } - - template friend class task; // task can register itself with the event by calling the private _RegisterTask - template friend class task_completion_event; - - typedef typename details::_Task_completion_event_impl<_ResultType>::_TaskList _TaskList; - - /// - /// Cancels the task_completion_event. - /// - bool _CancelInternal() const - { - // Cancellation of task completion events is an internal only utility. Our usage is such that _CancelInternal - // will never be invoked if the task completion event has been set. - _CONCRT_ASSERT(!_M_Impl->_M_fHasValue); - if (_M_Impl->_M_fIsCanceled) - { - return false; - } - - _TaskList _Tasks; - bool _Cancel = false; - { - details::scoped_lock _LockHolder(_M_Impl->_M_taskListCritSec); - _CONCRT_ASSERT(!_M_Impl->_M_fHasValue); - if (!_M_Impl->_M_fIsCanceled) - { - _M_Impl->_M_fIsCanceled = true; - _Tasks.swap(_M_Impl->_M_tasks); - _Cancel = true; - } - } - - bool _UserException = _M_Impl->_HasUserException(); - - if (_Cancel) - { - for (auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt) - { - // Need to call this after the lock is released. See comments in set(). - if (_UserException) - { - (*_TaskIt)->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true); - } - else - { - (*_TaskIt)->_Cancel(true); - } - } - } - return _Cancel; - } - - /// - /// Register a task with this event. This function is called when a task is constructed using - /// a task_completion_event. - /// - void _RegisterTask(const typename details::_Task_ptr<_ResultType>::_Type & _TaskParam) - { - details::scoped_lock _LockHolder(_M_Impl->_M_taskListCritSec); -#if _MSC_VER < 1800 - _TaskParam->_SetScheduledEvent(); -#endif - //If an exception was already set on this event, then cancel the task with the stored exception. - if (_M_Impl->_HasUserException()) - { - _TaskParam->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true); - } - else if (_M_Impl->_M_fHasValue) - { -#if _MSC_VER >= 1800 - _TaskParam->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get()); -#else - _TaskParam->_FinalizeAndRunContinuations(_M_Impl->_M_value); -#endif - } - else - { - _M_Impl->_M_tasks.push_back(_TaskParam); - } - } - - std::shared_ptr> _M_Impl; -}; - -/// -/// The task_completion_event class allows you to delay the execution of a task until a condition is satisfied, -/// or start a task in response to an external event. -/// -/// -/// Use a task created from a task completion event when your scenario requires you to create a task that will complete, and -/// thereby have its continuations scheduled for execution, at some point in the future. The task_completion_event must -/// have the same type as the task you create, and calling the set method on the task completion event with a value of that type -/// will cause the associated task to complete, and provide that value as a result to its continuations. -/// If the task completion event is never signaled, any tasks created from it will be canceled when it is destructed. -/// task_completion_event behaves like a smart pointer, and should be passed by value. -/// -/// -/**/ -template<> -class task_completion_event -{ -public: - /// - /// Sets the task completion event. - /// - /// - /// The method returns true if it was successful in setting the event. It returns false if the event is already set. - /// - /// - /// In the presence of multiple or concurrent calls to set, only the first call will succeed and its result (if any) will be stored in the - /// task completion event. The remaining sets are ignored and the method will return false. When you set a task completion event, all the - /// tasks created from that event will immediately complete, and its continuations, if any, will be scheduled. Task completion objects that have - /// a other than void will pass the value to their continuations. - /// - /**/ - bool set() const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - return _M_unitEvent.set(details::_Unit_type()); - } -#if _MSC_VER >= 1800 - - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - bool set_exception(_E _Except) const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - return _M_unitEvent._Cancel(std::make_exception_ptr(_Except), _CAPTURE_CALLSTACK()); - } -#endif - - /// - /// Propagates an exception to all tasks associated with this event. - /// - /// - /// The exception_ptr that indicates the exception to set this event with. - /// - /**/ - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - bool set_exception(std::exception_ptr _ExceptionPtr) const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - // It is important that _ReturnAddress() evaluate to the instruction after the call instruction for set_exception. -#if _MSC_VER >= 1800 - return _M_unitEvent._Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK()); -#else - return _M_unitEvent._Cancel(_ExceptionPtr, _ReturnAddress()); -#endif - } - - /// - /// Cancel the task_completion_event. Any task created using this event will be marked as canceled if it has - /// not already been set. - /// - void _Cancel() const // 'const' (even though it's not deep) allows to safely pass events by value into lambdas - { - _M_unitEvent._Cancel(); - } - - /// - /// Cancel the task_completion_event with the exception holder provided. Any task created using this event will be canceled - /// with the same exception. - /// - void _Cancel(const std::shared_ptr& _ExHolder) const - { - _M_unitEvent._Cancel(_ExHolder); - } - - /// - /// Method that stores an exception in the task completion event. This is used internally by when_any. - /// Note, this does not cancel the task completion event. A task completion event with a stored exception - /// can bet set() successfully. If it is canceled, it will cancel with the stored exception, if one is present. - /// - bool _StoreException(const std::shared_ptr& _ExHolder) const - { - return _M_unitEvent._StoreException(_ExHolder); - } - - /// - /// Test whether current event has been either Set, or Canceled. - /// - bool _IsTriggered() const - { - return _M_unitEvent._IsTriggered(); - } - -private: - template friend class task; // task can register itself with the event by calling the private _RegisterTask - - /// - /// Register a task with this event. This function is called when a task is constructed using - /// a task_completion_event. - /// - void _RegisterTask(details::_Task_ptr::_Type _TaskParam) - { - _M_unitEvent._RegisterTask(_TaskParam); - } - - // The void event contains an event a dummy type so common code can be used for events with void and non-void results. - task_completion_event _M_unitEvent; -}; -namespace details -{ - // - // Compile-time validation helpers - // - - // Task constructor validation: issue helpful diagnostics for common user errors. Do not attempt full validation here. - // - // Anything callable is fine - template - auto _IsValidTaskCtor(_Ty _Param, int, int, int, int, int, int, int) -> typename decltype(_Param(), std::true_type()); - - // Anything callable with a task return value is fine - template - auto _IsValidTaskCtor(_Ty _Param, int, int, int, int, int, int, ...) -> typename decltype(_Param(stdx::declval*>()), std::true_type()); - - // Anything callable with a return value is fine - template - auto _IsValidTaskCtor(_Ty _Param, int, int, int, int, int, ...) -> typename decltype(_Param(stdx::declval<_ReturnType*>()), std::true_type()); - - // Anything that has GetResults is fine: this covers AsyncAction* - template - auto _IsValidTaskCtor(_Ty _Param, int, int, int, int, ...) -> typename decltype(_Param->GetResults(), std::true_type()); - - // Anything that has GetResults(TResult_abi*) is fine: this covers AsyncOperation* - template - auto _IsValidTaskCtor(_Ty _Param, int, int, int, ...) -> typename decltype(_Param->GetResults(stdx::declval()))*>()), std::true_type()); - - // Allow parameters with set: this covers task_completion_event - template - auto _IsValidTaskCtor(_Ty _Param, int, int, ...) -> typename decltype(_Param.set(stdx::declval<_ReturnType>()), std::true_type()); - - template - auto _IsValidTaskCtor(_Ty _Param, int, ...) -> typename decltype(_Param.set(), std::true_type()); - - // All else is invalid - template - std::false_type _IsValidTaskCtor(_Ty _Param, ...); - - template - void _ValidateTaskConstructorArgs(_Ty _Param) - { - (void)_Param; - static_assert(std::is_same(_Param, 0, 0, 0, 0, 0, 0, 0)), std::true_type>::value, - "incorrect argument for task constructor; can be a callable object, an asynchronous operation, or a task_completion_event" - ); - static_assert(!(std::is_same<_Ty, _ReturnType>::value && details::_IsIAsyncInfo<_Ty>::_Value), - "incorrect template argument for task; consider using the return type of the async operation"); - } - // Helpers for create_async validation - // - // A parameter lambda taking no arguments is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, int, int, int, int, int) -> typename decltype(_Param(), std::true_type()); - - // A parameter lambda taking a result argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, int, int, int, int, ...) -> typename decltype(_Param(stdx::declval<_ReturnType*>()), std::true_type()); - - // A parameter lambda taking an cancellation_token argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, int, int, int, ...) -> typename decltype(_Param(Concurrency::cancellation_token::none()), std::true_type()); - - // A parameter lambda taking an cancellation_token argument and a result argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, int, int, ...) -> typename decltype(_Param(Concurrency::cancellation_token::none(), stdx::declval<_ReturnType*>()), std::true_type()); - - // A parameter lambda taking a progress report argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, int, ...) -> typename decltype(_Param(details::_ProgressReporterCtorArgType()), std::true_type()); - - // A parameter lambda taking a progress report argument and a result argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, int, ...) -> typename decltype(_Param(details::_ProgressReporterCtorArgType(), stdx::declval<_ReturnType*>()), std::true_type()); - - // A parameter lambda taking a progress report and a cancellation_token argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, int, ...) -> typename decltype(_Param(details::_ProgressReporterCtorArgType(), Concurrency::cancellation_token::none()), std::true_type()); - - // A parameter lambda taking a progress report and a cancellation_token argument and a result argument is valid - template - static auto _IsValidCreateAsync(_Ty _Param, int, ...) -> typename decltype(_Param(details::_ProgressReporterCtorArgType(), Concurrency::cancellation_token::none(), stdx::declval<_ReturnType*>()), std::true_type()); - - // All else is invalid - template - static std::false_type _IsValidCreateAsync(_Ty _Param, ...); -} - -/// -/// The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed asynchronously, -/// and concurrently with other tasks and parallel work produced by parallel algorithms in the Concurrency Runtime. It produces -/// a result of type on successful completion. Tasks of type task<void> produce no result. -/// A task can be waited upon and canceled independently of other tasks. It can also be composed with other tasks using -/// continuations(then), and join(when_all) and choice(when_any) patterns. -/// -/// -/// The result type of this task. -/// -/// -/// For more information, see . -/// -/**/ -template -class task -{ -public: - /// - /// The type of the result an object of this class produces. - /// - /**/ - typedef _ReturnType result_type; - - /// - /// Constructs a task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task() : _M_Impl(nullptr) - { - // The default constructor should create a task with a nullptr impl. This is a signal that the - // task is not usable and should throw if any wait(), get() or then() APIs are used. - } - - /// - /// Constructs a task object. - /// - /// - /// The type of the parameter from which the task is to be constructed. - /// - /// - /// The parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> - /// object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Windows Store app. The lambda or function - /// object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, - /// task<result_type>, or a Windows::Foundation::IAsyncInfo in Windows Store apps. - /// - /// - /// The cancellation token to associate with this task. A task created without a cancellation token cannot be canceled. It implicitly receives - /// the token cancellation_token::none(). - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - explicit task(_Ty _Param) - { -#if _MSC_VER >= 1800 - task_options _TaskOptions; -#endif - details::_ValidateTaskConstructorArgs<_ReturnType, _Ty>(_Param); - -#if _MSC_VER >= 1800 - _CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler()); -#else - _CreateImpl(Concurrency::cancellation_token::none()._GetImplValue()); -#endif - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of the task constructor. -#if _MSC_VER >= 1800 - _SetTaskCreationCallstack(_CAPTURE_CALLSTACK()); -#else - _SetTaskCreationAddressHint(_ReturnAddress()); -#endif - _TaskInitMaybeFunctor(_Param, details::_IsCallable<_ReturnType>(_Param, 0, 0, 0)); - } - - /// - /// Constructs a task object. - /// - /// - /// The type of the parameter from which the task is to be constructed. - /// - /// - /// The parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> - /// object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Windows Store app. The lambda or function - /// object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, - /// task<result_type>, or a Windows::Foundation::IAsyncInfo in Windows Store apps. - /// - /// - /// The cancellation token to associate with this task. A task created without a cancellation token cannot be canceled. It implicitly receives - /// the token cancellation_token::none(). - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -#if _MSC_VER >= 1800 - explicit task(_Ty _Param, const task_options &_TaskOptions) -#else - explicit task(_Ty _Param, Concurrency::cancellation_token _Token) -#endif - { - details::_ValidateTaskConstructorArgs<_ReturnType, _Ty>(_Param); - -#if _MSC_VER >= 1800 - _CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler()); -#else - _CreateImpl(_Token._GetImplValue()); -#endif - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of the task constructor. -#if _MSC_VER >= 1800 - _SetTaskCreationCallstack(details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : _CAPTURE_CALLSTACK()); -#else - _SetTaskCreationAddressHint(_ReturnAddress()); -#endif - _TaskInitMaybeFunctor(_Param, details::_IsCallable<_ReturnType>(_Param, 0, 0, 0)); - } - - /// - /// Constructs a task object. - /// - /// - /// The source task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task(const task& _Other) : _M_Impl(_Other._M_Impl) {} - - /// - /// Constructs a task object. - /// - /// - /// The source task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task(task&& _Other) : _M_Impl(std::move(_Other._M_Impl)) {} - - /// - /// Replaces the contents of one task object with another. - /// - /// - /// The source task object. - /// - /// - /// As task behaves like a smart pointer, after a copy assignment, this task objects represents the same - /// actual task as does. - /// - /**/ - task& operator=(const task& _Other) - { - if (this != &_Other) - { - _M_Impl = _Other._M_Impl; - } - return *this; - } - - /// - /// Replaces the contents of one task object with another. - /// - /// - /// The source task object. - /// - /// - /// As task behaves like a smart pointer, after a copy assignment, this task objects represents the same - /// actual task as does. - /// - /**/ - task& operator=(task&& _Other) - { - if (this != &_Other) - { - _M_Impl = std::move(_Other._M_Impl); - } - return *this; - } - - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - auto then(const _Function& _Func) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType - { -#if _MSC_VER >= 1800 - task_options _TaskOptions; - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions); -#else - auto _ContinuationTask = _ThenImpl<_ReturnType, _Function>(_Func, nullptr, task_continuation_context::use_default()); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; -#endif - } - - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The cancellation token to associate with the continuation task. A continuation task that is created without a cancellation token will inherit - /// the token of its antecedent task. - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -#if _MSC_VER >= 1800 - auto then(const _Function& _Func, task_options _TaskOptions) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType -#else - auto then(const _Function& _Func, Concurrency::cancellation_token _CancellationToken) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType -#endif - { -#if _MSC_VER >= 1800 - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions); -#else - auto _ContinuationTask = _ThenImpl<_ReturnType, _Function>(_Func, _CancellationToken._GetImplValue(), task_continuation_context::use_default()); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; -#endif - } -#if _MSC_VER < 1800 - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// A variable that specifies where the continuation should execute. This variable is only useful when used in a - /// Windows Store app. For more information, see task_continuation_context - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - auto then(const _Function& _Func, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType - { - auto _ContinuationTask = _ThenImpl<_ReturnType, _Function>(_Func, nullptr, _ContinuationContext); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; - } -#endif - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The cancellation token to associate with the continuation task. A continuation task that is created without a cancellation token will inherit - /// the token of its antecedent task. - /// - /// - /// A variable that specifies where the continuation should execute. This variable is only useful when used in a - /// Windows Store app. For more information, see task_continuation_context - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - auto then(const _Function& _Func, Concurrency::cancellation_token _CancellationToken, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType - { -#if _MSC_VER >= 1800 - task_options _TaskOptions(_CancellationToken, _ContinuationContext); - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - return _ThenImpl<_ReturnType, _Function>(_Func, _TaskOptions); -#else - auto _ContinuationTask = _ThenImpl<_ReturnType, _Function>(_Func, _CancellationToken._GetImplValue(), _ContinuationContext); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; -#endif - } - - /// - /// Waits for this task to reach a terminal state. It is possible for wait to execute the task inline, if all of the tasks - /// dependencies are satisfied, and it has not already been picked up for execution by a background worker. - /// - /// - /// A task_status value which could be either completed or canceled. If the task encountered an exception - /// during execution, or an exception was propagated to it from an antecedent task, wait will throw that exception. - /// - /**/ - task_status wait() const - { - if (_M_Impl == nullptr) - { - throw Concurrency::invalid_operation("wait() cannot be called on a default constructed task."); - } - - return _M_Impl->_Wait(); - } - - /// - /// Returns the result this task produced. If the task is not in a terminal state, a call to get will wait for the task to - /// finish. This method does not return a value when called on a task with a result_type of void. - /// - /// - /// The result of the task. - /// - /// - /// If the task is canceled, a call to get will throw a task_canceled exception. If the task - /// encountered an different exception or an exception was propagated to it from an antecedent task, a call to get will throw that exception. - /// - /**/ - _ReturnType get() const - { - if (_M_Impl == nullptr) - { - throw Concurrency::invalid_operation("get() cannot be called on a default constructed task."); - } - - if (_M_Impl->_Wait() == Concurrency::canceled) - { - throw Concurrency::task_canceled(); - } - - return _M_Impl->_GetResult(); - } -#if _MSC_VER >= 1800 - /// - /// Determines if the task is completed. - /// - /// - /// True if the task has completed, false otherwise. - /// - /// - /// The function returns true if the task is completed or canceled (with or without user exception). - /// - bool is_done() const - { - if (!_M_Impl) - { - throw Concurrency::invalid_operation("is_done() cannot be called on a default constructed task."); - } - - return _M_Impl->_IsDone(); - } - - /// - /// Returns the scheduler for this task - /// - /// - /// A pointer to the scheduler - /// - Concurrency::scheduler_ptr scheduler() const - { - if (!_M_Impl) - { - throw Concurrency::invalid_operation("scheduler() cannot be called on a default constructed task."); - } - - return _M_Impl->_GetScheduler(); - } -#endif - /// - /// Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such a task. - /// - /// - /// true if the task unwraps an IAsyncInfo interface or is descended from such a task, false otherwise. - /// - /**/ - bool is_apartment_aware() const - { - if (_M_Impl == nullptr) - { - throw Concurrency::invalid_operation("is_apartment_aware() cannot be called on a default constructed task."); - } - return _M_Impl->_IsApartmentAware(); - } - - /// - /// Determines whether two task objects represent the same internal task. - /// - /// - /// true if the objects refer to the same underlying task, and false otherwise. - /// - /**/ - bool operator==(const task<_ReturnType>& _Rhs) const - { - return (_M_Impl == _Rhs._M_Impl); - } - - /// - /// Determines whether two task objects represent different internal tasks. - /// - /// - /// true if the objects refer to different underlying tasks, and false otherwise. - /// - /**/ - bool operator!=(const task<_ReturnType>& _Rhs) const - { - return !operator==(_Rhs); - } - - /// - /// Create an underlying task implementation. - /// -#if _MSC_VER >= 1800 - void _CreateImpl(Concurrency::details::_CancellationTokenState * _Ct, Concurrency::scheduler_ptr _Scheduler) -#else - void _CreateImpl(Concurrency::details::_CancellationTokenState * _Ct) -#endif - { - _CONCRT_ASSERT(_Ct != nullptr); -#if _MSC_VER >= 1800 - _M_Impl = details::_Task_ptr<_ReturnType>::_Make(_Ct, _Scheduler); -#else - _M_Impl = details::_Task_ptr<_ReturnType>::_Make(_Ct); -#endif - if (_Ct != Concurrency::details::_CancellationTokenState::_None()) - { -#if _MSC_VER >= 1800 - _M_Impl->_RegisterCancellation(_M_Impl); -#else - _M_Impl->_RegisterCancellation(); -#endif - } - } - - /// - /// Return the underlying implementation for this task. - /// - const typename details::_Task_ptr<_ReturnType>::_Type & _GetImpl() const - { - return _M_Impl; - } - - /// - /// Set the implementation of the task to be the supplied implementaion. - /// - void _SetImpl(const typename details::_Task_ptr<_ReturnType>::_Type & _Impl) - { - _CONCRT_ASSERT(_M_Impl == nullptr); - _M_Impl = _Impl; - } - - /// - /// Set the implementation of the task to be the supplied implementaion using a move instead of a copy. - /// - void _SetImpl(typename details::_Task_ptr<_ReturnType>::_Type && _Impl) - { - _CONCRT_ASSERT(_M_Impl == nullptr); - _M_Impl = std::move(_Impl); - } - - /// - /// Sets a property determining whether the task is apartment aware. - /// - void _SetAsync(bool _Async = true) - { - _GetImpl()->_SetAsync(_Async); - } - - /// - /// Sets a field in the task impl to the return address for calls to the task constructors and the then method. - /// -#if _MSC_VER >= 1800 - void _SetTaskCreationCallstack(const details::_TaskCreationCallstack &_callstack) - { - _GetImpl()->_SetTaskCreationCallstack(_callstack); - } -#else - void _SetTaskCreationAddressHint(void* _Address) - { - _GetImpl()->_SetTaskCreationAddressHint(_Address); - } -#endif - /// - /// An internal version of then that takes additional flags and always execute the continuation inline by default. - /// When _ForceInline is set to false, continuations inlining will be limited to default _DefaultAutoInline. - /// This function is Used for runtime internal continuations only. - /// - template -#if _MSC_VER >= 1800 - auto _Then(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, - details::_TaskInliningMode _InliningMode = Concurrency::details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType - { - // inherit from antecedent - auto _Scheduler = _GetImpl()->_GetScheduler(); - - return _ThenImpl<_ReturnType, _Function>(_Func, _PTokenState, task_continuation_context::use_default(), _Scheduler, _CAPTURE_CALLSTACK(), _InliningMode); - } -#else - auto _Then(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, bool _Aggregating, - details::_TaskInliningMode _InliningMode = Concurrency::details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function, _ReturnType>::_TaskOfType - { - return _ThenImpl<_ReturnType, _Function>(_Func, _PTokenState, task_continuation_context::use_default(), _Aggregating, _InliningMode); - } -#endif - -private: - template friend class task; - - // A helper class template that transforms an intial task lambda returns void into a lambda that returns a non-void type (details::_Unit_type is used - // to substitute for void). This is to minimize the special handling required for 'void'. - template - class _Init_func_transformer - { - public: - static auto _Perform(std::function _Func) -> decltype(_Func) - { - return _Func; - } - }; - - template<> - class _Init_func_transformer - { - public: - static auto _Perform(std::function _Func) -> decltype(details::_MakeVoidToUnitFunc(_Func)) - { - return details::_MakeVoidToUnitFunc(_Func); - } - }; - - // The task handle type used to construct an 'initial task' - a task with no dependents. - template - struct _InitialTaskHandle : - details::_PPLTaskHandle<_ReturnType, _InitialTaskHandle<_InternalReturnType, _Function, _TypeSelection>, details::_UnrealizedChore> - { - _Function _M_function; - _InitialTaskHandle(const typename details::_Task_ptr<_ReturnType>::_Type & _TaskImpl, const _Function & _Function) : _M_function(_Function), _PPLTaskHandle(_TaskImpl) - { - } - virtual ~_InitialTaskHandle() {} - -#if _MSC_VER >= 1800 - template - auto _LogWorkItemAndInvokeUserLambda(_Func && _func, _RetArg && _retArg) const -> decltype(_func(std::forward<_RetArg>(_retArg))) - { - details::_TaskWorkItemRAIILogger _LogWorkItem(this->_M_pTask->_M_taskEventLogger); - return _func(std::forward<_RetArg>(_retArg)); - } -#endif - - void _Perform() const - { - _Init(_TypeSelection()); - } -#if _MSC_VER >= 1800 - - void _SyncCancelAndPropagateException() const - { - this->_M_pTask->_Cancel(true); - } -#endif - // - // Overload 0: returns _InternalReturnType - // - // This is the most basic task with no unwrapping - // - void _Init(details::_TypeSelectorNoAsync) const - { - _ReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Init_func_transformer<_InternalReturnType>::_Perform(_M_function), &retVal); -#else - HRESULT hr = _Init_func_transformer<_InternalReturnType>::_Perform(_M_function)(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - _M_pTask->_FinalizeAndRunContinuations(retVal); - } - - // - // Overload 1: returns IAsyncOperation<_InternalReturnType>* - // or - // returns task<_InternalReturnType> - // - // This is task whose functor returns an async operation or a task which will be unwrapped for continuation - // Depending on the output type, the right _AsyncInit gets invoked - // - void _Init(details::_TypeSelectorAsyncTask) const - { - task<_InternalReturnType> retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, &retVal); -#else - HRESULT hr = _M_function(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(_M_pTask, retVal); - } - void _Init(details::_TypeSelectorAsyncOperation) const - { - _ReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, &retVal); -#else - HRESULT hr = _M_function(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal).Get()); - } - - // - // Overload 2: returns IAsyncAction* - // - // This is task whose functor returns an async action which will be unwrapped for continuation - // - void _Init(details::_TypeSelectorAsyncAction) const - { - _ReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, &retVal); -#else - HRESULT hr = _M_function(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(_M_pTask, Microsoft::WRL::Make(retVal).Get()); - } - - // - // Overload 3: returns IAsyncOperationWithProgress<_InternalReturnType, _ProgressType>* - // - // This is task whose functor returns an async operation with progress which will be unwrapped for continuation - // - void _Init(details::_TypeSelectorAsyncOperationWithProgress) const - { - typedef details::_GetProgressType::_Value _ProgressType; - _ReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, &retVal); -#else - HRESULT hr = _M_function(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal).Get()); - } - - // - // Overload 4: returns IAsyncActionWithProgress<_ProgressType>* - // - // This is task whose functor returns an async action with progress which will be unwrapped for continuation - // - void _Init(details::_TypeSelectorAsyncActionWithProgress) const - { - typedef details::_GetProgressType::_Value _ProgressType; - _ReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, &retVal); -#else - HRESULT hr = _M_function(&retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_ReturnType, _InternalReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal).Get()); - } - }; - - /// - /// A helper class template that transforms a continuation lambda that either takes or returns void, or both, into a lambda that takes and returns a - /// non-void type (details::_Unit_type is used to substitute for void). This is to minimize the special handling required for 'void'. - /// - template - class _Continuation_func_transformer - { - public: - static auto _Perform(std::function _Func) -> decltype(_Func) - { - return _Func; - } - }; - - template - class _Continuation_func_transformer - { - public: - static auto _Perform(std::function _Func) -> decltype(details::_MakeUnitToTFunc<_OutType>(_Func)) - { - return details::_MakeUnitToTFunc<_OutType>(_Func); - } - }; - - template - class _Continuation_func_transformer<_InType, void> - { - public: - static auto _Perform(std::function _Func) -> decltype(details::_MakeTToUnitFunc<_InType>(_Func)) - { - return details::_MakeTToUnitFunc<_InType>(_Func); - } - }; - - template<> - class _Continuation_func_transformer - { - public: - static auto _Perform(std::function _Func) -> decltype(details::_MakeUnitToUnitFunc(_Func)) - { - return details::_MakeUnitToUnitFunc(_Func); - } - }; - /// - /// The task handle type used to create a 'continuation task'. - /// - template - struct _ContinuationTaskHandle : - details::_PPLTaskHandle::_Type, - _ContinuationTaskHandle<_InternalReturnType, _ContinuationReturnType, _Function, _IsTaskBased, _TypeSelection>, details::_ContinuationTaskHandleBase> - { - typedef typename details::_NormalizeVoidToUnitType<_ContinuationReturnType>::_Type _NormalizedContinuationReturnType; - - typename details::_Task_ptr<_ReturnType>::_Type _M_ancestorTaskImpl; - _Function _M_function; - - _ContinuationTaskHandle(const typename details::_Task_ptr<_ReturnType>::_Type & _AncestorImpl, - const typename details::_Task_ptr<_NormalizedContinuationReturnType>::_Type & _ContinuationImpl, - const _Function & _Func, const task_continuation_context & _Context, details::_TaskInliningMode _InliningMode) : -#if _MSC_VER >= 1800 - details::_PPLTaskHandle::_Type, - _ContinuationTaskHandle<_InternalReturnType, _ContinuationReturnType, _Function, _IsTaskBased, _TypeSelection>, details::_ContinuationTaskHandleBase> - ::_PPLTaskHandle(_ContinuationImpl) - , _M_ancestorTaskImpl(_AncestorImpl) - , _M_function(_Func) -#else - _M_ancestorTaskImpl(_AncestorImpl), _PPLTaskHandle(_ContinuationImpl), _M_function(_Func) -#endif - { - _M_isTaskBasedContinuation = _IsTaskBased::value; - _M_continuationContext = _Context; - _M_continuationContext._Resolve(_AncestorImpl->_IsApartmentAware()); - _M_inliningMode = _InliningMode; - } - - virtual ~_ContinuationTaskHandle() {} - -#if _MSC_VER >= 1800 - template - auto _LogWorkItemAndInvokeUserLambda(_Func && _func, _Arg && _value, _RetArg && _retArg) const -> decltype(_func(std::forward<_Arg>(_value), std::forward<_RetArg>(_retArg))) - { - details::_TaskWorkItemRAIILogger _LogWorkItem(this->_M_pTask->_M_taskEventLogger); - return _func(std::forward<_Arg>(_value), std::forward<_RetArg>(_retArg)); - } -#endif - - void _Perform() const - { - _Continue(_IsTaskBased(), _TypeSelection()); - } - -#if _MSC_VER >= 1800 - void _SyncCancelAndPropagateException() const - { - if (_M_ancestorTaskImpl->_HasUserException()) - { - // If the ancestor encountered an exception, transfer the exception to the continuation - // This traverses down the tree to propagate the exception. - this->_M_pTask->_CancelWithExceptionHolder(_M_ancestorTaskImpl->_GetExceptionHolder(), true); - } - else - { - // If the ancestor was canceled, then your own execution should be canceled. - // This traverses down the tree to cancel it. - this->_M_pTask->_Cancel(true); - } - } -#endif - - // - // Overload 0-0: _InternalReturnType -> _TaskType - // - // This is a straight task continuation which simply invokes its target with the ancestor's completion argument - // - void _Continue(std::false_type, details::_TypeSelectorNoAsync) const - { - _NormalizedContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _ContinuationReturnType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &retVal); -#else - HRESULT hr =_Continuation_func_transformer<_InternalReturnType, _ContinuationReturnType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - _M_pTask->_FinalizeAndRunContinuations(retVal); - } - - // - // Overload 0-1: _InternalReturnType -> IAsyncOperation<_TaskType>* - // or - // _InternalReturnType -> task<_TaskType> - // - // This is a straight task continuation which returns an async operation or a task which will be unwrapped for continuation - // Depending on the output type, the right _AsyncInit gets invoked - // - void _Continue(std::false_type, details::_TypeSelectorAsyncTask) const - { - typedef typename details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType; - _FuncOutputType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &retVal); -#else - HRESULT hr = _Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>( - _M_pTask, - retVal - ); - } - void _Continue(std::false_type, details::_TypeSelectorAsyncOperation) const - { - typedef typename details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType; - _FuncOutputType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &retVal); -#else - HRESULT hr = _Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>( - _M_pTask, - Microsoft::WRL::Make>(retVal).Get()); - } - - // - // Overload 0-2: _InternalReturnType -> IAsyncAction* - // - // This is a straight task continuation which returns an async action which will be unwrapped for continuation - // - void _Continue(std::false_type, details::_TypeSelectorAsyncAction) const - { - typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType; - _FuncOutputType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &retVal); -#else - HRESULT hr = _Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>( - _M_pTask, - Microsoft::WRL::Make( - retVal).Get()); - } - - // - // Overload 0-3: _InternalReturnType -> IAsyncOperationWithProgress<_TaskType, _ProgressType>* - // - // This is a straight task continuation which returns an async operation with progress which will be unwrapped for continuation - // - void _Continue(std::false_type, details::_TypeSelectorAsyncOperationWithProgress) const - { - typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType; - - _FuncOutputType _OpWithProgress; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &_OpWithProgress); -#else - HRESULT hr = _Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &_OpWithProgress); -#endif - typedef details::_GetProgressType::_Value _ProgressType; - - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>( - _M_pTask, - Microsoft::WRL::Make>(_OpWithProgress).Get()); - } - - // - // Overload 0-4: _InternalReturnType -> IAsyncActionWithProgress<_ProgressType>* - // - // This is a straight task continuation which returns an async action with progress which will be unwrapped for continuation - // - void _Continue(std::false_type, details::_TypeSelectorAsyncActionWithProgress) const - { - typedef details::_FunctionTypeTraits<_Function, _InternalReturnType>::_FuncRetType _FuncOutputType; - - _FuncOutputType _OpWithProgress; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function), _M_ancestorTaskImpl->_GetResult(), &_OpWithProgress); -#else - HRESULT hr = _Continuation_func_transformer<_InternalReturnType, _FuncOutputType>::_Perform(_M_function)(_M_ancestorTaskImpl->_GetResult(), &_OpWithProgress); -#endif - typedef details::_GetProgressType::_Value _ProgressType; - - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>( - _M_pTask, - Microsoft::WRL::Make>(_OpWithProgress).Get()); - } - - - // - // Overload 1-0: task<_InternalReturnType> -> _TaskType - // - // This is an exception handling type of continuation which takes the task rather than the task's result. - // - void _Continue(std::true_type, details::_TypeSelectorNoAsync) const - { - typedef task<_InternalReturnType> _FuncInputType; - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - _NormalizedContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_Continuation_func_transformer<_FuncInputType, _ContinuationReturnType>::_Perform(_M_function), std::move(_ResultTask), &retVal); -#else - HRESULT hr = _Continuation_func_transformer<_FuncInputType, _ContinuationReturnType>::_Perform(_M_function)(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - _M_pTask->_FinalizeAndRunContinuations(retVal); - } - - // - // Overload 1-1: task<_InternalReturnType> -> IAsyncOperation<_TaskType>^ - // or - // task<_TaskType> - // - // This is an exception handling type of continuation which takes the task rather than - // the task's result. It also returns an async operation or a task which will be unwrapped - // for continuation - // - void _Continue(std::true_type, details::_TypeSelectorAsyncTask) const - { - // The continuation takes a parameter of type task<_Input>, which is the same as the ancestor task. - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - _ContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask), &retVal); -#else - HRESULT hr = _M_function(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(_M_pTask, retVal); - } - void _Continue(std::true_type, details::_TypeSelectorAsyncOperation) const - { - // The continuation takes a parameter of type task<_Input>, which is the same as the ancestor task. - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - _ContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask), &retVal); -#else - HRESULT hr = _M_function(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal)); - } - - // - // Overload 1-2: task<_InternalReturnType> -> IAsyncAction* - // - // This is an exception handling type of continuation which takes the task rather than - // the task's result. It also returns an async action which will be unwrapped for continuation - // - void _Continue(std::true_type, details::_TypeSelectorAsyncAction) const - { - // The continuation takes a parameter of type task<_Input>, which is the same as the ancestor task. - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - _ContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask), &retVal); -#else - HRESULT hr = _M_function(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(_M_pTask, - Microsoft::WRL::Make(retVal)); - } - - // - // Overload 1-3: task<_InternalReturnType> -> IAsyncOperationWithProgress<_TaskType, _ProgressType>* - // - // This is an exception handling type of continuation which takes the task rather than - // the task's result. It also returns an async operation with progress which will be unwrapped - // for continuation - // - void _Continue(std::true_type, details::_TypeSelectorAsyncOperationWithProgress) const - { - // The continuation takes a parameter of type task<_Input>, which is the same as the ancestor task. - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - - typedef details::_GetProgressType::_Value _ProgressType; - _ContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask), &retVal); -#else - HRESULT hr = _M_function(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal)); - } - - // - // Overload 1-4: task<_InternalReturnType> -> IAsyncActionWithProgress<_ProgressType>* - // - // This is an exception handling type of continuation which takes the task rather than - // the task's result. It also returns an async operation with progress which will be unwrapped - // for continuation - // - void _Continue(std::true_type, details::_TypeSelectorAsyncActionWithProgress) const - { - // The continuation takes a parameter of type task<_Input>, which is the same as the ancestor task. - task<_InternalReturnType> _ResultTask; - _ResultTask._SetImpl(std::move(_M_ancestorTaskImpl)); - - typedef details::_GetProgressType::_Value _ProgressType; - _ContinuationReturnType retVal; -#if _MSC_VER >= 1800 - HRESULT hr = _LogWorkItemAndInvokeUserLambda(_M_function, std::move(_ResultTask), &retVal); -#else - HRESULT hr = _M_function(std::move(_ResultTask), &retVal); -#endif - if (FAILED(hr)) throw std::make_exception_ptr(hr); - details::_Task_impl_base::_AsyncInit<_NormalizedContinuationReturnType, _ContinuationReturnType>(_M_pTask, - Microsoft::WRL::Make>(retVal)); - } - }; - /// - /// Initializes a task using a lambda, function pointer or function object. - /// - template - void _TaskInitWithFunctor(const _Function& _Func) - { - typedef details::_InitFunctorTypeTraits<_InternalReturnType, details::_FunctionTypeTraits<_Function, void>::_FuncRetType> _Async_type_traits; - - _M_Impl->_M_fFromAsync = _Async_type_traits::_IsAsyncTask; - _M_Impl->_M_fUnwrappedTask = _Async_type_traits::_IsUnwrappedTaskOrAsync; -#if _MSC_VER >= 1800 - _M_Impl->_M_taskEventLogger._LogScheduleTask(false); -#endif - _M_Impl->_ScheduleTask(new _InitialTaskHandle<_InternalReturnType, _Function, typename _Async_type_traits::_AsyncKind>(_GetImpl(), _Func), Concurrency::details::_NoInline); - } - - /// - /// Initializes a task using a task completion event. - /// - void _TaskInitNoFunctor(task_completion_event<_ReturnType>& _Event) - { - _Event._RegisterTask(_M_Impl); - } - - /// - /// Initializes a task using an asynchronous operation IAsyncOperation* - /// - template - void _TaskInitAsyncOp(details::_AsyncInfoImpl<_OpType, _CompHandlerType, _ResultType>* _AsyncOp) - { - _M_Impl->_M_fFromAsync = true; -#if _MSC_VER < 1800 - _M_Impl->_SetScheduledEvent(); -#endif - // Mark this task as started here since we can set the state in the constructor without acquiring a lock. Once _AsyncInit - // returns a completion could execute concurrently and the task must be fully initialized before that happens. - _M_Impl->_M_TaskState = details::_Task_impl_base::_Started; - // Pass the shared pointer into _AsyncInit for storage in the Async Callback. - details::_Task_impl_base::_AsyncInit<_ReturnType, _Result>(_M_Impl, _AsyncOp); - } - - /// - /// Initializes a task using an asynchronous operation IAsyncOperation* - /// - template - void _TaskInitNoFunctor(ABI::Windows::Foundation::IAsyncOperation<_Result>* _AsyncOp) - { - _TaskInitAsyncOp<_Result>(Microsoft::WRL::Make>(_AsyncOp).Get()); - } - - /// - /// Initializes a task using an asynchronous operation with progress IAsyncOperationWithProgress* - /// - template - void _TaskInitNoFunctor(ABI::Windows::Foundation::IAsyncOperationWithProgress<_Result, _Progress>* _AsyncOp) - { - _TaskInitAsyncOp<_Result>(Microsoft::WRL::Make>(_AsyncOp).Get()); - } - /// - /// Initializes a task using a callable object. - /// - template - void _TaskInitMaybeFunctor(_Function & _Func, std::true_type) - { - _TaskInitWithFunctor<_ReturnType, _Function>(_Func); - } - - /// - /// Initializes a task using a non-callable object. - /// - template - void _TaskInitMaybeFunctor(_Ty & _Param, std::false_type) - { - _TaskInitNoFunctor(_Param); - } -#if _MSC_VER >= 1800 - template - auto _ThenImpl(const _Function& _Func, const task_options& _TaskOptions) const -> typename details::_ContinuationTypeTraits<_Function, _InternalReturnType>::_TaskOfType - { - if (!_M_Impl) - { - throw Concurrency::invalid_operation("then() cannot be called on a default constructed task."); - } - - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; - auto _Scheduler = _TaskOptions.has_scheduler() ? _TaskOptions.get_scheduler() : _GetImpl()->_GetScheduler(); - auto _CreationStack = details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : details::_TaskCreationCallstack(); - return _ThenImpl<_InternalReturnType, _Function>(_Func, _PTokenState, _TaskOptions.get_continuation_context(), _Scheduler, _CreationStack); - } -#endif - /// - /// The one and only implementation of then for void and non-void tasks. - /// - template -#if _MSC_VER >= 1800 - auto _ThenImpl(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, const task_continuation_context& _ContinuationContext, Concurrency::scheduler_ptr _Scheduler, details::_TaskCreationCallstack _CreationStack, - details::_TaskInliningMode _InliningMode = Concurrency::details::_NoInline) const -> typename details::_ContinuationTypeTraits<_Function, _InternalReturnType>::_TaskOfType -#else - auto _ThenImpl(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, const task_continuation_context& _ContinuationContext, - bool _Aggregating = false, details::_TaskInliningMode _InliningMode = Concurrency::details::_NoInline) const -> typename details::_ContinuationTypeTraits<_Function, _InternalReturnType>::_TaskOfType -#endif - { - if (_M_Impl == nullptr) - { - throw Concurrency::invalid_operation("then() cannot be called on a default constructed task."); - } - - typedef details::_FunctionTypeTraits<_Function, _InternalReturnType> _Function_type_traits; - typedef details::_TaskTypeTraits _Async_type_traits; - typedef typename _Async_type_traits::_TaskRetType _TaskType; - - // - // A **nullptr** token state indicates that it was not provided by the user. In this case, we inherit the antecedent's token UNLESS this is a - // an exception handling continuation. In that case, we break the chain with a _None. That continuation is never canceled unless the user - // explicitly passes the same token. - // - if (_PTokenState == nullptr) - { -#if _MSC_VER >= 1800 - if (_Function_type_traits::_Takes_task::value) -#else - if (_Function_type_traits::_Takes_task()) -#endif - { - _PTokenState = Concurrency::details::_CancellationTokenState::_None(); - } - else - { - _PTokenState = _GetImpl()->_M_pTokenState; - } - } - - task<_TaskType> _ContinuationTask; -#if _MSC_VER >= 1800 - _ContinuationTask._CreateImpl(_PTokenState, _Scheduler); -#else - _ContinuationTask._CreateImpl(_PTokenState); -#endif - _ContinuationTask._GetImpl()->_M_fFromAsync = (_GetImpl()->_M_fFromAsync || _Async_type_traits::_IsAsyncTask); -#if _MSC_VER < 1800 - _ContinuationTask._GetImpl()->_M_fRuntimeAggregate = _Aggregating; -#endif - _ContinuationTask._GetImpl()->_M_fUnwrappedTask = _Async_type_traits::_IsUnwrappedTaskOrAsync; -#if _MSC_VER >= 1800 - _ContinuationTask._SetTaskCreationCallstack(_CreationStack); -#endif - _GetImpl()->_ScheduleContinuation(new _ContinuationTaskHandle<_InternalReturnType, _TaskType, _Function, typename _Function_type_traits::_Takes_task, typename _Async_type_traits::_AsyncKind>( - _GetImpl(), _ContinuationTask._GetImpl(), _Func, _ContinuationContext, _InliningMode)); - - return _ContinuationTask; - } - - // The underlying implementation for this task - typename details::_Task_ptr<_ReturnType>::_Type _M_Impl; -}; - -/// -/// The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed asynchronously, -/// and concurrently with other tasks and parallel work produced by parallel algorithms in the Concurrency Runtime. It produces -/// a result of type on successful completion. Tasks of type task<void> produce no result. -/// A task can be waited upon and canceled independently of other tasks. It can also be composed with other tasks using -/// continuations(then), and join(when_all) and choice(when_any) patterns. -/// -/// -/// For more information, see . -/// -/**/ -template<> -class task -{ -public: - /// - /// The type of the result an object of this class produces. - /// - /**/ - typedef void result_type; - - /// - /// Constructs a task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task() : _M_unitTask() - { - // The default constructor should create a task with a nullptr impl. This is a signal that the - // task is not usable and should throw if any wait(), get() or then() APIs are used. - } -#if _MSC_VER < 1800 - /// - /// Constructs a task object. - /// - /// - /// The type of the parameter from which the task is to be constructed. - /// - /// - /// The parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> - /// object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Windows Store app. The lambda or function - /// object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, - /// task<result_type>, or a Windows::Foundation::IAsyncInfo in Windows Store apps. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - explicit task(_Ty _Param) - { - details::_ValidateTaskConstructorArgs(_Param); - - _M_unitTask._CreateImpl(Concurrency::cancellation_token::none()._GetImplValue()); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of the task constructor. - _M_unitTask._SetTaskCreationAddressHint(_ReturnAddress()); - - _TaskInitMaybeFunctor(_Param, details::_IsCallable(_Param, 0, 0, 0)); - } -#endif - /// - /// Constructs a task object. - /// - /// - /// The type of the parameter from which the task is to be constructed. - /// - /// - /// The parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> - /// object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Windows Store app. The lambda or function - /// object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, - /// task<result_type>, or a Windows::Foundation::IAsyncInfo in Windows Store apps. - /// - /// - /// The cancellation token to associate with this task. A task created without a cancellation token cannot be canceled. It implicitly receives - /// the token cancellation_token::none(). - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -#if _MSC_VER >= 1800 - explicit task(_Ty _Param, const task_options& _TaskOptions = task_options()) -#else - explicit task(_Ty _Param, Concurrency::cancellation_token _CancellationToken) -#endif - { - details::_ValidateTaskConstructorArgs(_Param); -#if _MSC_VER >= 1800 - _M_unitTask._CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler()); -#else - _M_unitTask._CreateImpl(_CancellationToken._GetImplValue()); -#endif - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of the task constructor. -#if _MSC_VER >= 1800 - _M_unitTask._SetTaskCreationCallstack(details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack ? details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack : _CAPTURE_CALLSTACK()); -#else - _M_unitTask._SetTaskCreationAddressHint(_ReturnAddress()); -#endif - _TaskInitMaybeFunctor(_Param, details::_IsCallable(_Param, 0, 0, 0)); - } - - /// - /// Constructs a task object. - /// - /// - /// The source task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task(const task& _Other) : _M_unitTask(_Other._M_unitTask){} - - /// - /// Constructs a task object. - /// - /// - /// The source task object. - /// - /// - /// The default constructor for a task is only present in order to allow tasks to be used within containers. - /// A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then - /// will throw an invalid_argument exception when called on a default constructed task. - /// A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task - /// completion event is set. - /// The version of the constructor that takes a cancellation token creates a task that can be canceled using the - /// cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable. - /// Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface - /// reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created - /// from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, - /// and not when the lamda returns. - /// task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads - /// without the need for locks. - /// The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available - /// to Windows Store apps. - /// For more information, see . - /// - /**/ - task(task&& _Other) : _M_unitTask(std::move(_Other._M_unitTask)) {} - - /// - /// Replaces the contents of one task object with another. - /// - /// - /// The source task object. - /// - /// - /// As task behaves like a smart pointer, after a copy assignment, this task objects represents the same - /// actual task as does. - /// - /**/ - task& operator=(const task& _Other) - { - if (this != &_Other) - { - _M_unitTask = _Other._M_unitTask; - } - return *this; - } - - /// - /// Replaces the contents of one task object with another. - /// - /// - /// The source task object. - /// - /// - /// As task behaves like a smart pointer, after a copy assignment, this task objects represents the same - /// actual task as does. - /// - /**/ - task& operator=(task&& _Other) - { - if (this != &_Other) - { - _M_unitTask = std::move(_Other._M_unitTask); - } - return *this; - } -#if _MSC_VER < 1800 - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - auto then(const _Function& _Func) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - auto _ContinuationTask = _M_unitTask._ThenImpl(_Func, nullptr, task_continuation_context::use_default()); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; - } -#endif - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The cancellation token to associate with the continuation task. A continuation task that is created without a cancellation token will inherit - /// the token of its antecedent task. - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -#if _MSC_VER >= 1800 - auto then(const _Function& _Func, task_options _TaskOptions = task_options()) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - return _M_unitTask._ThenImpl(_Func, _TaskOptions); - } -#else - auto then(const _Function& _Func, Concurrency::cancellation_token _CancellationToken) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - auto _ContinuationTask = _M_unitTask._ThenImpl(_Func, _CancellationToken._GetImplValue(), task_continuation_context::use_default()); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; - } - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// A variable that specifies where the continuation should execute. This variable is only useful when used in a - /// Windows Store app. For more information, see task_continuation_context - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result - auto then(const _Function& _Func, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - auto _ContinuationTask = _M_unitTask._ThenImpl(_Func, nullptr, _ContinuationContext); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; - - } -#endif - /// - /// Adds a continuation task to this task. - /// - /// - /// The type of the function object that will be invoked by this task. - /// - /// - /// The continuation function to execute when this task completes. This continuation function must take as input - /// a variable of either result_type or task<result_type>, where result_type is the type - /// of the result this task produces. - /// - /// - /// The cancellation token to associate with the continuation task. A continuation task that is created without a cancellation token will inherit - /// the token of its antecedent task. - /// - /// - /// A variable that specifies where the continuation should execute. This variable is only useful when used in a - /// Windows Store app. For more information, see task_continuation_context - /// - /// - /// The newly created continuation task. The result type of the returned task is determined by what returns. - /// - /// - /// The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available - /// to Windows Store apps. - /// For more information on how to use task continuations to compose asynchronous work, see . - /// - /**/ - template - __declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -#if _MSC_VER >= 1800 - auto then(const _Function& _Func, Concurrency::cancellation_token _CancellationToken, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - task_options _TaskOptions(_CancellationToken, _ContinuationContext); - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - return _M_unitTask._ThenImpl(_Func, _TaskOptions); - } -#else - auto then(const _Function& _Func, Concurrency::cancellation_token _CancellationToken, task_continuation_context _ContinuationContext) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - auto _ContinuationTask = _M_unitTask._ThenImpl(_Func, _CancellationToken._GetImplValue(), _ContinuationContext); - // Do not move the next line out of this function. It is important that _ReturnAddress() evaluate to the the call site of then. - _ContinuationTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _ContinuationTask; - } -#endif - - /// - /// Waits for this task to reach a terminal state. It is possible for wait to execute the task inline, if all of the tasks - /// dependencies are satisfied, and it has not already been picked up for execution by a background worker. - /// - /// - /// A task_status value which could be either completed or canceled. If the task encountered an exception - /// during execution, or an exception was propagated to it from an antecedent task, wait will throw that exception. - /// - /**/ - task_status wait() const - { - return _M_unitTask.wait(); - } - - /// - /// Returns the result this task produced. If the task is not in a terminal state, a call to get will wait for the task to - /// finish. This method does not return a value when called on a task with a result_type of void. - /// - /// - /// If the task is canceled, a call to get will throw a task_canceled exception. If the task - /// encountered an different exception or an exception was propagated to it from an antecedent task, a call to get will throw that exception. - /// - /**/ - void get() const - { - _M_unitTask.get(); - } -#if _MSC_VER >= 1800 - - /// - /// Determines if the task is completed. - /// - /// - /// True if the task has completed, false otherwise. - /// - /// - /// The function returns true if the task is completed or canceled (with or without user exception). - /// - bool is_done() const - { - return _M_unitTask.is_done(); - } - - /// - /// Returns the scheduler for this task - /// - /// - /// A pointer to the scheduler - /// - Concurrency::scheduler_ptr scheduler() const - { - return _M_unitTask.scheduler(); - } -#endif - /// - /// Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such a task. - /// - /// - /// true if the task unwraps an IAsyncInfo interface or is descended from such a task, false otherwise. - /// - /**/ - bool is_apartment_aware() const - { - return _M_unitTask.is_apartment_aware(); - } - - /// - /// Determines whether two task objects represent the same internal task. - /// - /// - /// true if the objects refer to the same underlying task, and false otherwise. - /// - /**/ - bool operator==(const task& _Rhs) const - { - return (_M_unitTask == _Rhs._M_unitTask); - } - - /// - /// Determines whether two task objects represent different internal tasks. - /// - /// - /// true if the objects refer to different underlying tasks, and false otherwise. - /// - /**/ - bool operator!=(const task& _Rhs) const - { - return !operator==(_Rhs); - } - - /// - /// Create an underlying task implementation. - /// -#if _MSC_VER >= 1800 - void _CreateImpl(Concurrency::details::_CancellationTokenState * _Ct, Concurrency::scheduler_ptr _Scheduler) - { - _M_unitTask._CreateImpl(_Ct, _Scheduler); - } -#else - void _CreateImpl(Concurrency::details::_CancellationTokenState * _Ct) - { - _M_unitTask._CreateImpl(_Ct); - } -#endif - - /// - /// Return the underlying implementation for this task. - /// - const details::_Task_ptr::_Type & _GetImpl() const - { - return _M_unitTask._M_Impl; - } - - /// - /// Set the implementation of the task to be the supplied implementaion. - /// - void _SetImpl(const details::_Task_ptr::_Type & _Impl) - { - _M_unitTask._SetImpl(_Impl); - } - - /// - /// Set the implementation of the task to be the supplied implementaion using a move instead of a copy. - /// - void _SetImpl(details::_Task_ptr::_Type && _Impl) - { - _M_unitTask._SetImpl(std::move(_Impl)); - } - - /// - /// Sets a property determining whether the task is apartment aware. - /// - void _SetAsync(bool _Async = true) - { - _M_unitTask._SetAsync(_Async); - } - - /// - /// Sets a field in the task impl to the return address for calls to the task constructors and the then method. - /// -#if _MSC_VER >= 1800 - void _SetTaskCreationCallstack(const details::_TaskCreationCallstack &_callstack) - { - _M_unitTask._SetTaskCreationCallstack(_callstack); - } -#else - void _SetTaskCreationAddressHint(void* _Address) - { - _M_unitTask._SetTaskCreationAddressHint(_Address); - } -#endif - - /// - /// An internal version of then that takes additional flags and executes the continuation inline. Used for runtime internal continuations only. - /// - template -#if _MSC_VER >= 1800 - auto _Then(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, - details::_TaskInliningMode _InliningMode = Concurrency::details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - // inherit from antecedent - auto _Scheduler = _GetImpl()->_GetScheduler(); - - return _M_unitTask._ThenImpl(_Func, _PTokenState, task_continuation_context::use_default(), _Scheduler, _CAPTURE_CALLSTACK(), _InliningMode); - } -#else - auto _Then(const _Function& _Func, Concurrency::details::_CancellationTokenState *_PTokenState, - bool _Aggregating, details::_TaskInliningMode _InliningMode = Concurrency::details::_ForceInline) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType - { - return _M_unitTask._ThenImpl(_Func, _PTokenState, task_continuation_context::use_default(), _Aggregating, _InliningMode); - } -#endif - -private: - template friend class task; - template friend class task_completion_event; - - /// - /// Initializes a task using a task completion event. - /// - void _TaskInitNoFunctor(task_completion_event& _Event) - { - _M_unitTask._TaskInitNoFunctor(_Event._M_unitEvent); - } - /// - /// Initializes a task using an asynchronous action IAsyncAction* - /// - void _TaskInitNoFunctor(ABI::Windows::Foundation::IAsyncAction* _AsyncAction) - { - _M_unitTask._TaskInitAsyncOp(Microsoft::WRL::Make(_AsyncAction).Get()); - } - - /// - /// Initializes a task using an asynchronous action with progress IAsyncActionWithProgress<_P>* - /// - template - void _TaskInitNoFunctor(ABI::Windows::Foundation::IAsyncActionWithProgress<_P>* _AsyncActionWithProgress) - { - _M_unitTask._TaskInitAsyncOp(Microsoft::WRL::Make>(_AsyncActionWithProgress).Get()); - } - /// - /// Initializes a task using a callable object. - /// - template - void _TaskInitMaybeFunctor(_Function & _Func, std::true_type) - { - _M_unitTask._TaskInitWithFunctor(_Func); - } - - /// - /// Initializes a task using a non-callable object. - /// - template - void _TaskInitMaybeFunctor(_T & _Param, std::false_type) - { - _TaskInitNoFunctor(_Param); - } - - // The void task contains a task of a dummy type so common code can be used for tasks with void and non-void results. - task _M_unitTask; -}; - -namespace details -{ - - /// - /// The following type traits are used for the create_task function. - /// - - // Unwrap task - template - _Ty _GetUnwrappedType(task<_Ty>); - - // Unwrap all supported types - template - auto _GetUnwrappedReturnType(_Ty _Arg, int) -> decltype(_GetUnwrappedType(_Arg)); - // fallback - template - _Ty _GetUnwrappedReturnType(_Ty, ...); - - /// - /// _GetTaskType functions will retrieve task type T in task[T](Arg), - /// for given constructor argument Arg and its property "callable". - /// It will automatically unwrap argument to get the final return type if necessary. - /// - - // Non-Callable - template - _Ty _GetTaskType(task_completion_event<_Ty>, std::false_type); - - // Non-Callable - template - auto _GetTaskType(_Ty _NonFunc, std::false_type) -> decltype(_GetUnwrappedType(_NonFunc)); - - // Callable - template - auto _GetTaskType(_Ty _Func, std::true_type) -> decltype(_GetUnwrappedReturnType(stdx::declval<_FunctionTypeTraits<_Ty, void>::_FuncRetType>(), 0)); - - // Special callable returns void - void _GetTaskType(std::function, std::true_type); - struct _BadArgType{}; - - template - auto _FilterValidTaskType(_Ty _Param, int) -> decltype(_GetTaskType(_Param, _IsCallable<_ReturnType>(_Param, 0, 0, 0))); - - template - _BadArgType _FilterValidTaskType(_Ty _Param, ...); - - template - struct _TaskTypeFromParam - { - typedef decltype(_FilterValidTaskType<_ReturnType>(stdx::declval<_Ty>(), 0)) _Type; - }; -} - - -/// -/// Creates a PPL task object. create_task can be used anywhere you would have used a task constructor. -/// It is provided mainly for convenience, because it allows use of the auto keyword while creating tasks. -/// -/// -/// The type of the parameter from which the task is to be constructed. -/// -/// -/// The parameter from which the task is to be constructed. This could be a lambda or function object, a task_completion_event -/// object, a different task object, or a Windows::Foundation::IAsyncInfo interface if you are using tasks in your Windows Store app. -/// -/// -/// A new task of type T, that is inferred from . -/// -/// -/// The first overload behaves like a task constructor that takes a single parameter. -/// The second overload associates the cancellation token provided with the newly created task. If you use this overload you are not -/// allowed to pass in a different task object as the first parameter. -/// The type of the returned task is inferred from the first parameter to the function. If is a task_completion_event<T>, -/// a task<T>, or a functor that returns either type T or task<T>, the type of the created task is task<T>. -/// In a Windows Store app, if is of type Windows::Foundation::IAsyncOperation<T>^ or -/// Windows::Foundation::IAsyncOperationWithProgress<T,P>^, or a functor that returns either of those types, the created task will be of type task<T>. -/// If is of type Windows::Foundation::IAsyncAction^ or Windows::Foundation::IAsyncActionWithProgress<P>^, or a functor -/// that returns either of those types, the created task will have type task<void>. -/// -/// -/// -/**/ -template -__declspec(noinline) -#if _MSC_VER >= 1800 -auto create_task(_Ty _Param, task_options _TaskOptions = task_options()) -> task::_Type> -#else -auto create_task(_Ty _Param) -> task::_Type> -#endif -{ - static_assert(!std::is_same::_Type, details::_BadArgType>::value, - "incorrect argument for create_task; can be a callable object, an asynchronous operation, or a task_completion_event" - ); -#if _MSC_VER >= 1800 - details::_get_internal_task_options(_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK()); - task::_Type> _CreatedTask(_Param, _TaskOptions); -#else - task::_Type> _CreatedTask(_Param); - // Ideally we would like to forceinline create_task, but __forceinline does nothing on debug builds. Therefore, we ask for no inlining - // and overwrite the creation address hint set by the task constructor. DO NOT REMOVE this next line from create_task. It is - // essential that _ReturnAddress() evaluate to the instruction right after the call to create_task in client code. - _CreatedTask._SetTaskCreationAddressHint(_ReturnAddress()); -#endif - return _CreatedTask; -} - -/// -/// Creates a PPL task object. create_task can be used anywhere you would have used a task constructor. -/// It is provided mainly for convenience, because it allows use of the auto keyword while creating tasks. -/// -/// -/// The type of the parameter from which the task is to be constructed. -/// -/// -/// The parameter from which the task is to be constructed. This could be a lambda or function object, a task_completion_event -/// object, a different task object, or a Windows::Foundation::IAsyncInfo interface if you are using tasks in your Windows Store app. -/// -/// -/// The cancellation token to associate with the task. When the source for this token is canceled, cancellation will be requested on the task. -/// -/// -/// A new task of type T, that is inferred from . -/// -/// -/// The first overload behaves like a task constructor that takes a single parameter. -/// The second overload associates the cancellation token provided with the newly created task. If you use this overload you are not -/// allowed to pass in a different task object as the first parameter. -/// The type of the returned task is inferred from the first parameter to the function. If is a task_completion_event<T>, -/// a task<T>, or a functor that returns either type T or task<T>, the type of the created task is task<T>. -/// In a Windows Store app, if is of type Windows::Foundation::IAsyncOperation<T>^ or -/// Windows::Foundation::IAsyncOperationWithProgress<T,P>^, or a functor that returns either of those types, the created task will be of type task<T>. -/// If is of type Windows::Foundation::IAsyncAction^ or Windows::Foundation::IAsyncActionWithProgress<P>^, or a functor -/// that returns either of those types, the created task will have type task<void>. -/// -/// -/// -/**/ -#if _MSC_VER >= 1800 -template -__declspec(noinline) -task<_ReturnType> create_task(const task<_ReturnType>& _Task) -{ - task<_ReturnType> _CreatedTask(_Task); - return _CreatedTask; -} -#else -template -__declspec(noinline) -auto create_task(_Ty _Param, Concurrency::cancellation_token _Token) -> task::_Type> -{ - static_assert(!std::is_same::_Type, details::_BadArgType>::value, - "incorrect argument for create_task; can be a callable object, an asynchronous operation, or a task_completion_event" - ); - task::_Type> _CreatedTask(_Param, _Token); - // Ideally we would like to forceinline create_task, but __forceinline does nothing on debug builds. Therefore, we ask for no inlining - // and overwrite the creation address hint set by the task constructor. DO NOT REMOVE this next line from create_task. It is - // essential that _ReturnAddress() evaluate to the instruction right after the call to create_task in client code. - _CreatedTask._SetTaskCreationAddressHint(_ReturnAddress()); - return _CreatedTask; -} -#endif - -namespace details -{ - template - task*>()))>::type> _To_task_helper(ABI::Windows::Foundation::IAsyncOperation<_T>* op) - { - return task<_T>(op); - } - - template - task*>()))>::type> _To_task_helper(ABI::Windows::Foundation::IAsyncOperationWithProgress<_T, _Progress>* op) - { - return task<_T>(op); - } - - inline task _To_task_helper(ABI::Windows::Foundation::IAsyncAction* op) - { - return task(op); - } - - template - task _To_task_helper(ABI::Windows::Foundation::IAsyncActionWithProgress<_Progress>* op) - { - return task(op); - } - - template - class _ProgressDispatcherBase - { - public: - - virtual ~_ProgressDispatcherBase() - { - } - - virtual void _Report(const _ProgressType& _Val) = 0; - }; - - template - class _ProgressDispatcher : public _ProgressDispatcherBase<_ProgressType> - { - public: - - virtual ~_ProgressDispatcher() - { - } - - _ProgressDispatcher(_ClassPtrType _Ptr) : _M_ptr(_Ptr) - { - } - - virtual void _Report(const _ProgressType& _Val) - { - _M_ptr->_FireProgress(_Val); - } - - private: - - _ClassPtrType _M_ptr; - }; -} // namespace details - - -/// -/// The progress reporter class allows reporting progress notifications of a specific type. Each progress_reporter object is bound -/// to a particular asynchronous action or operation. -/// -/// -/// The payload type of each progress notification reported through the progress reporter. -/// -/// -/// This type is only available to Windows Store apps. -/// -/// -/**/ -template -class progress_reporter -{ - typedef std::shared_ptr> _PtrType; - -public: - - /// - /// Sends a progress report to the asynchronous action or operation to which this progress reporter is bound. - /// - /// - /// The payload to report through a progress notification. - /// - /**/ - void report(const _ProgressType& _Val) const - { - _M_dispatcher->_Report(_Val); - } - - template - static progress_reporter _CreateReporter(_ClassPtrType _Ptr) - { - progress_reporter _Reporter; - details::_ProgressDispatcherBase<_ProgressType> *_PDispatcher = new details::_ProgressDispatcher<_ProgressType, _ClassPtrType>(_Ptr); - _Reporter._M_dispatcher = _PtrType(_PDispatcher); - return _Reporter; - } - progress_reporter() {} - -private: - progress_reporter(details::_ProgressReporterCtorArgType); - - _PtrType _M_dispatcher; -}; - -namespace details -{ - // - // maps internal definitions for AsyncStatus and defines states that are not client visible - // - enum _AsyncStatusInternal - { - _AsyncCreated = -1, // externally invisible - // client visible states (must match AsyncStatus exactly) - _AsyncStarted = ABI::Windows::Foundation::AsyncStatus::Started, // 0 - _AsyncCompleted = ABI::Windows::Foundation::AsyncStatus::Completed, // 1 - _AsyncCanceled = ABI::Windows::Foundation::AsyncStatus::Canceled, // 2 - _AsyncError = ABI::Windows::Foundation::AsyncStatus::Error, // 3 - // non-client visible internal states - _AsyncCancelPending, - _AsyncClosed, - _AsyncUndefined - }; - - // - // designates whether the "GetResults" method returns a single result (after complete fires) or multiple results - // (which are progressively consumable between Start state and before Close is called) - // - enum _AsyncResultType - { - SingleResult = 0x0001, - MultipleResults = 0x0002 - }; - - template - struct _ProgressTypeTraits - { - static const bool _TakesProgress = false; - typedef void _ProgressType; - }; - - template - struct _ProgressTypeTraits> - { - static const bool _TakesProgress = true; - typedef typename _T _ProgressType; - }; - - template::value, bool bTakesProgress = _ProgressTypeTraits<_T>::_TakesProgress> - struct _TokenTypeTraits - { - static const bool _TakesToken = false; - typedef typename _T _ReturnType; - }; - - template - struct _TokenTypeTraits<_T, false, true> - { - static const bool _TakesToken = false; - typedef void _ReturnType; - }; - - template - struct _TokenTypeTraits<_T, true, false> - { - static const bool _TakesToken = true; - typedef void _ReturnType; - }; - - template::_ArgumentCount> - struct _CAFunctorOptions - { - static const bool _TakesProgress = false; - static const bool _TakesToken = false; - typedef void _ProgressType; - typedef void _ReturnType; - }; - - template - struct _CAFunctorOptions<_T, 1> - { - private: - - typedef typename _FunctorTypeTraits<_T>::_Argument1Type _Argument1Type; - - public: - - static const bool _TakesProgress = _ProgressTypeTraits<_Argument1Type>::_TakesProgress; - static const bool _TakesToken = _TokenTypeTraits<_Argument1Type>::_TakesToken; - typedef typename _ProgressTypeTraits<_Argument1Type>::_ProgressType _ProgressType; - typedef typename _TokenTypeTraits<_Argument1Type>::_ReturnType _ReturnType; - }; - - template - struct _CAFunctorOptions<_T, 2> - { - private: - - typedef typename _FunctorTypeTraits<_T>::_Argument1Type _Argument1Type; - typedef typename _FunctorTypeTraits<_T>::_Argument2Type _Argument2Type; - - public: - - static const bool _TakesProgress = _ProgressTypeTraits<_Argument1Type>::_TakesProgress; - static const bool _TakesToken = !_TakesProgress ? true : _TokenTypeTraits<_Argument2Type>::_TakesToken; - typedef typename _ProgressTypeTraits<_Argument1Type>::_ProgressType _ProgressType; - typedef typename _TokenTypeTraits<_Argument2Type>::_ReturnType _ReturnType; - }; - - template - struct _CAFunctorOptions<_T, 3> - { - private: - - typedef typename _FunctorTypeTraits<_T>::_Argument1Type _Argument1Type; - - public: - - static const bool _TakesProgress = true; - static const bool _TakesToken = true; - typedef typename _ProgressTypeTraits<_Argument1Type>::_ProgressType _ProgressType; - typedef typename _FunctorTypeTraits<_T>::_Argument3Type _ReturnType; - }; - - class _Zip - { - }; - - // *************************************************************************** - // Async Operation Task Generators - // - - // - // Functor returns an IAsyncInfo - result needs to be wrapped in a task: - // - template - struct _SelectorTaskGenerator - { -#if _MSC_VER >= 1800 - template - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>(_Func(_pRet), _taskOptinos); - } - - template - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>(_Func(_Cts.get_token(), _pRet), _taskOptinos); - } - - template - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>(_Func(_Progress, _pRet), _taskOptinos); - } - - template - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>(_Func(_Progress, _Cts.get_token(), _pRet), _taskOptinos); - } -#else - template - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>(_Func(_pRet), _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>(_Func(_Cts.get_token(), _pRet), _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>(_Func(_Progress, _pRet), _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>(_Func(_Progress, _Cts.get_token(), _pRet), _Cts.get_token()); - } -#endif - }; - - template - struct _SelectorTaskGenerator<_AsyncSelector, void> - { -#if _MSC_VER >= 1800 - template - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task(_Func(), _taskOptinos); - } - - template - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task(_Func(_Cts.get_token()), _taskOptinos); - } - - template - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task(_Func(_Progress), _taskOptinos); - } - - template - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task(_Func(_Progress, _Cts.get_token()), _taskOptinos); - } -#else - template - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts) - { - return task(_Func(), _Cts.get_token()); - } - - template - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts) - { - return task(_Func(_Cts.get_token()), _Cts.get_token()); - } - - template - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) - { - return task(_Func(_Progress), _Cts.get_token()); - } - - template - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) - { - return task(_Func(_Progress, _Cts.get_token()), _Cts.get_token()); - } -#endif - }; - -#if _MSC_VER < 1800 - // For create_async lambdas that return a (non-task) result, we oversubscriber the current task for the duration of the - // lambda. - struct _Task_generator_oversubscriber - { - _Task_generator_oversubscriber() - { - Concurrency::details::_Context::_Oversubscribe(true); - } - - ~_Task_generator_oversubscriber() - { - Concurrency::details::_Context::_Oversubscribe(false); - } - }; -#endif - - // - // Functor returns a result - it needs to be wrapped in a task: - // - template - struct _SelectorTaskGenerator - { -#if _MSC_VER >= 1800 - -#pragma warning(push) -#pragma warning(disable: 4702) - template - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - HRESULT hr = _Func(_pRet); - retVal = _pRet; - return hr; - }, _taskOptinos); - } -#pragma warning(pop) - - template - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - HRESULT hr = _Func(_Cts.get_token(), _pRet); - retVal = _pRet; - return hr; - }, _taskOptinos); - } - - template - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - HRESULT hr = _Func(_Progress, _pRet); - retVal = _pRet; - return hr; - }, _taskOptinos); - } - - template - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - HRESULT hr = _Func(_Progress, _Cts.get_token(), _pRet); - retVal = _pRet; - return hr; - }, _taskOptinos); - } -#else - template - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - HRESULT hr = _Func(_pRet); - retVal = _pRet; - return hr; - }, _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - HRESULT hr = _Func(_Cts.get_token(), _pRet); - retVal = _pRet; - return hr; - }, _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - HRESULT hr = _Func(_Progress, _pRet); - retVal = _pRet; - return hr; - }, _Cts.get_token()); - } - - template - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return task<_ReturnType>([=](_ReturnType* retVal) -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - HRESULT hr = _Func(_Progress, _Cts.get_token(), _pRet); - retVal = _pRet; - return hr; - }, _Cts.get_token()); - } -#endif - }; - - template<> - struct _SelectorTaskGenerator - { -#if _MSC_VER >= 1800 - template - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task([=]() -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - return _Func(); - }, _taskOptinos); - } - - template - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task([=]() -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - return _Func(_Cts.get_token()); - }, _taskOptinos); - } - - template - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task([=]() -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - return _Func(_Progress); - }, _taskOptinos); - } - - template - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - task_options _taskOptinos(_Cts.get_token()); - details::_get_internal_task_options(_taskOptinos)._set_creation_callstack(_callstack); - return task([=]() -> HRESULT { - Concurrency::details::_Task_generator_oversubscriber_t _Oversubscriber; - (_Oversubscriber); - return _Func(_Progress, _Cts.get_token()); - }, _taskOptinos); - } -#else - template - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts) - { - return task([=]() -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - return _Func(); - }, _Cts.get_token()); - } - - template - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts) - { - return task([=]() -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - return _Func(_Cts.get_token()); - }, _Cts.get_token()); - } - - template - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) - { - return task([=]() -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - return _Func(_Progress); - }, _Cts.get_token()); - } - - template - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) - { - return task([=]() -> HRESULT { - _Task_generator_oversubscriber _Oversubscriber; - return _Func(_Progress, _Cts.get_token()); - }, _Cts.get_token()); - } -#endif - }; - - // - // Functor returns a task - the task can directly be returned: - // - template - struct _SelectorTaskGenerator - { - template -#if _MSC_VER >= 1800 - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) -#else - static task<_ReturnType> _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) -#endif - { - task<_ReturnType> _task; - _Func(&_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) -#else - static task<_ReturnType> _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) -#endif - { - task<_ReturnType> _task; - _Func(_Cts.get_token(), &_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) -#else - static task<_ReturnType> _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) -#endif - { - task<_ReturnType> _task; - _Func(_Progress, &_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) -#else - static task<_ReturnType> _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) -#endif - { - task<_ReturnType> _task; - _Func(_Progress, _Cts.get_token(), &_task); - return _task; - } - }; - - template<> - struct _SelectorTaskGenerator - { - template -#if _MSC_VER >= 1800 - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) -#else - static task _GenerateTask_0(const _Function& _Func, Concurrency::cancellation_token_source _Cts) -#endif - { - task _task; - _Func(&_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) -#else - static task _GenerateTask_1C(const _Function& _Func, Concurrency::cancellation_token_source _Cts) -#endif - { - task _task; - _Func(_Cts.get_token(), &_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) -#else - static task _GenerateTask_1P(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) -#endif - { - task _task; - _Func(_Progress, &_task); - return _task; - } - - template -#if _MSC_VER >= 1800 - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) -#else - static task _GenerateTask_2PC(const _Function& _Func, const _ProgressObject& _Progress, Concurrency::cancellation_token_source _Cts) -#endif - { - task _task; - _Func(_Progress, _Cts.get_token(), &_task); - return _task; - } - }; - - template - struct _TaskGenerator - { - }; - - template - struct _TaskGenerator<_Generator, false, false> - { -#if _MSC_VER >= 1800 - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack)) - { - (void)_Ptr; - return _Generator::_GenerateTask_0(_Func, _Cts, _callstack); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet, _callstack)) - { - return _Generator::_GenerateTask_0(_Func, _Cts, _pRet, _callstack); - } -#else - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts)) - { - (void)_Ptr; - return _Generator::_GenerateTask_0(_Func, _Cts); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet)) - { - return _Generator::_GenerateTask_0(_Func, _Cts, _pRet); - } -#endif - }; - - template - struct _TaskGenerator<_Generator, true, false> - { -#if _MSC_VER >= 1800 - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack)) - { - return _Generator::_GenerateTask_1C(_Func, _Cts, _callstack); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet, _callstack)) - { - return _Generator::_GenerateTask_1C(_Func, _Cts, _pRet, _callstack); - } -#else - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts)) - { - return _Generator::_GenerateTask_1C(_Func, _Cts); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet)) - { - return _Generator::_GenerateTask_1C(_Func, _Cts, _pRet); - } -#endif - }; - - template - struct _TaskGenerator<_Generator, false, true> - { -#if _MSC_VER >= 1800 - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack)) - { - return _Generator::_GenerateTask_1P(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _callstack); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet, _callstack)) - { - return _Generator::_GenerateTask_1P(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _pRet, _callstack); - } -#else - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts)) - { - return _Generator::_GenerateTask_1P(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet)) - { - return _Generator::_GenerateTask_1P(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _pRet); - } -#endif - }; - - template - struct _TaskGenerator<_Generator, true, true> - { -#if _MSC_VER >= 1800 - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _callstack)) - { - return _Generator::_GenerateTask_2PC(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _callstack); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet, _callstack)) - { - return _Generator::_GenerateTask_2PC(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _pRet, _callstack); - } -#else - template - static auto _GenerateTaskNoRet(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts)) - { - return _Generator::_GenerateTask_2PC(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts); - } - - template - static auto _GenerateTask(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - -> decltype(_Generator::_GenerateTask_0(_Func, _Cts, _pRet)) - { - return _Generator::_GenerateTask_2PC(_Func, progress_reporter<_ProgressType>::_CreateReporter(_Ptr), _Cts, _pRet); - } -#endif - }; - - // *************************************************************************** - // Async Operation Attributes Classes - // - // These classes are passed through the hierarchy of async base classes in order to hold multiple attributes of a given async construct in - // a single container. An attribute class must define: - // - // Mandatory: - // ------------------------- - // - // _AsyncBaseType : The Windows Runtime interface which is being implemented. - // _CompletionDelegateType : The Windows Runtime completion delegate type for the interface. - // _ProgressDelegateType : If _TakesProgress is true, the Windows Runtime progress delegate type for the interface. If it is false, an empty Windows Runtime type. - // _ReturnType : The return type of the async construct (void for actions / non-void for operations) - // - // _TakesProgress : An indication as to whether or not - // - // _Generate_Task : A function adapting the user's function into what's necessary to produce the appropriate task - // - // Optional: - // ------------------------- - // - - template - struct _AsyncAttributes - { - }; - - template - struct _AsyncAttributes<_Function, _ProgressType, _ReturnType, _TaskTraits, _TakesToken, true> - { - typedef typename ABI::Windows::Foundation::IAsyncOperationWithProgress<_ReturnType, _ProgressType> _AsyncBaseType; - typedef typename ABI::Windows::Foundation::IAsyncOperationProgressHandler<_ReturnType, _ProgressType> _ProgressDelegateType; - typedef typename ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler<_ReturnType, _ProgressType> _CompletionDelegateType; - typedef typename _ReturnType _ReturnType; - typedef typename ABI::Windows::Foundation::Internal::GetAbiType()))>::type _ReturnType_abi; - typedef typename _ProgressType _ProgressType; - typedef typename ABI::Windows::Foundation::Internal::GetAbiType()))>::type _ProgressType_abi; - typedef typename _TaskTraits::_AsyncKind _AsyncKind; - typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator; - typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, true> _TaskGenerator; - - static const bool _TakesProgress = true; - static const bool _TakesToken = _TakesToken; - - template -#if _MSC_VER >= 1800 - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType_abi, _ReturnType>(_Func, _Ptr, _Cts, _pRet, _callstack); - } -#else - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType_abi, _ReturnType>(_Func, _Ptr, _Cts, _pRet); - } -#endif - }; - - template - struct _AsyncAttributes<_Function, _ProgressType, _ReturnType, _TaskTraits, _TakesToken, false> - { - typedef typename ABI::Windows::Foundation::IAsyncOperation<_ReturnType> _AsyncBaseType; - typedef _Zip _ProgressDelegateType; - typedef typename ABI::Windows::Foundation::IAsyncOperationCompletedHandler<_ReturnType> _CompletionDelegateType; - typedef typename _ReturnType _ReturnType; - typedef typename ABI::Windows::Foundation::Internal::GetAbiType()))>::type _ReturnType_abi; - typedef typename _TaskTraits::_AsyncKind _AsyncKind; - typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator; - typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, false> _TaskGenerator; - - static const bool _TakesProgress = false; - static const bool _TakesToken = _TakesToken; - - template -#if _MSC_VER >= 1800 - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType, _ReturnType>(_Func, _Ptr, _Cts, _pRet, _callstack); - } -#else - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType, _ReturnType>(_Func, _Ptr, _Cts, _pRet); - } -#endif - }; - - template - struct _AsyncAttributes<_Function, _ProgressType, void, _TaskTraits, _TakesToken, true> - { - typedef typename ABI::Windows::Foundation::IAsyncActionWithProgress<_ProgressType> _AsyncBaseType; - typedef typename ABI::Windows::Foundation::IAsyncActionProgressHandler<_ProgressType> _ProgressDelegateType; - typedef typename ABI::Windows::Foundation::IAsyncActionWithProgressCompletedHandler<_ProgressType> _CompletionDelegateType; - typedef void _ReturnType; - typedef void _ReturnType_abi; - typedef typename _ProgressType _ProgressType; - typedef typename ABI::Windows::Foundation::Internal::GetAbiType()))>::type _ProgressType_abi; - typedef typename _TaskTraits::_AsyncKind _AsyncKind; - typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator; - typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, true> _TaskGenerator; - - static const bool _TakesProgress = true; - static const bool _TakesToken = _TakesToken; - -#if _MSC_VER >= 1800 - template - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTaskNoRet<_Function, _ClassPtr, _ProgressType_abi>(_Func, _Ptr, _Cts, _callstack); - } - template - static task> _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType_abi>(_Func, _Ptr, _Cts, _pRet, _callstack); - } -#else - template - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - { - return _TaskGenerator::_GenerateTaskNoRet<_Function, _ClassPtr, _ProgressType_abi>(_Func, _Ptr, _Cts); - } - template - static task> _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType_abi>(_Func, _Ptr, _Cts, _pRet); - } -#endif - }; - - template - struct _AsyncAttributes<_Function, _ProgressType, void, _TaskTraits, _TakesToken, false> - { - typedef typename ABI::Windows::Foundation::IAsyncAction _AsyncBaseType; - typedef _Zip _ProgressDelegateType; - typedef typename ABI::Windows::Foundation::IAsyncActionCompletedHandler _CompletionDelegateType; - typedef void _ReturnType; - typedef void _ReturnType_abi; - typedef typename _TaskTraits::_AsyncKind _AsyncKind; - typedef typename _SelectorTaskGenerator<_AsyncKind, _ReturnType> _SelectorTaskGenerator; - typedef typename _TaskGenerator<_SelectorTaskGenerator, _TakesToken, false> _TaskGenerator; - - static const bool _TakesProgress = false; - static const bool _TakesToken = _TakesToken; - -#if _MSC_VER >= 1800 - template - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTaskNoRet<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _callstack); - } - template - static task> _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet, const _TaskCreationCallstack & _callstack) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _pRet, _callstack); - } -#else - template - static task _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts) - { - return _TaskGenerator::_GenerateTaskNoRet<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts); - } - template - static task> _Generate_Task(const _Function& _Func, _ClassPtr _Ptr, Concurrency::cancellation_token_source _Cts, _ReturnType* _pRet) - { - return _TaskGenerator::_GenerateTask<_Function, _ClassPtr, _ProgressType>(_Func, _Ptr, _Cts, _pRet); - } -#endif - }; - - template - struct _AsyncLambdaTypeTraits - { - typedef typename _Unhat::_ReturnType>::_Value _ReturnType; - typedef typename _FunctorTypeTraits<_Function>::_Argument1Type _Argument1Type; - typedef typename _CAFunctorOptions<_Function>::_ProgressType _ProgressType; - - static const bool _TakesProgress = _CAFunctorOptions<_Function>::_TakesProgress; - static const bool _TakesToken = _CAFunctorOptions<_Function>::_TakesToken; - - typedef typename _TaskTypeTraits<_ReturnType> _TaskTraits; - typedef typename _AsyncAttributes<_Function, _ProgressType, typename _TaskTraits::_TaskRetType, _TaskTraits, _TakesToken, _TakesProgress> _AsyncAttributes; - }; - // *************************************************************************** - // AsyncInfo (and completion) Layer: - // -#ifndef RUNTIMECLASS_Concurrency_winrt_details__AsyncInfoBase_DEFINED -#define RUNTIMECLASS_Concurrency_winrt_details__AsyncInfoBase_DEFINED - extern const __declspec(selectany) WCHAR RuntimeClass_Concurrency_winrt_details__AsyncInfoBase[] = L"Concurrency_winrt.details._AsyncInfoBase"; -#endif - - // - // Internal base class implementation for async operations (based on internal Windows representation for ABI level async operations) - // - template < typename _Attributes, _AsyncResultType resultType = SingleResult > - class _AsyncInfoBase abstract : public Microsoft::WRL::RuntimeClass< - Microsoft::WRL::RuntimeClassFlags< Microsoft::WRL::RuntimeClassType::WinRt>, Microsoft::WRL::Implements> - { - InspectableClass(RuntimeClass_Concurrency_winrt_details__AsyncInfoBase, BaseTrust) - public: - _AsyncInfoBase() : - _M_currentStatus(_AsyncStatusInternal::_AsyncCreated), - _M_errorCode(S_OK), - _M_completeDelegate(nullptr), - _M_CompleteDelegateAssigned(0), - _M_CallbackMade(0) - { -#if _MSC_VER < 1800 - _M_id = Concurrency::details::_GetNextAsyncId(); -#else - _M_id = Concurrency::details::platform::GetNextAsyncId(); -#endif - } - public: - virtual STDMETHODIMP GetResults(typename _Attributes::_ReturnType_abi* results) - { - (void)results; - return E_UNEXPECTED; - } - - virtual STDMETHODIMP get_Id(unsigned int* id) - { - HRESULT hr = _CheckValidStateForAsyncInfoCall(); - if (FAILED(hr)) return hr; - if (!id) return E_POINTER; - *id = _M_id; - return S_OK; - } - - virtual STDMETHODIMP put_Id(unsigned int id) - { - HRESULT hr = _CheckValidStateForAsyncInfoCall(); - if (FAILED(hr)) return hr; - - if (id == 0) - { - return E_INVALIDARG; - } - else if (_M_currentStatus != _AsyncStatusInternal::_AsyncCreated) - { - return E_ILLEGAL_METHOD_CALL; - } - - _M_id = id; - return S_OK; - } - virtual STDMETHODIMP get_Status(ABI::Windows::Foundation::AsyncStatus* status) - { - HRESULT hr = _CheckValidStateForAsyncInfoCall(); - if (FAILED(hr)) return hr; - if (!status) return E_POINTER; - - _AsyncStatusInternal _Current = _M_currentStatus; - // - // Map our internal cancel pending to cancelled. This way "pending cancelled" looks to the outside as "cancelled" but - // can still transition to "completed" if the operation completes without acknowledging the cancellation request - // - switch (_Current) - { - case _AsyncCancelPending: - _Current = _AsyncCanceled; - break; - case _AsyncCreated: - _Current = _AsyncStarted; - break; - default: - break; - } - - *status = static_cast(_Current); - return S_OK; - } - - virtual STDMETHODIMP get_ErrorCode(HRESULT* errorCode) - { - HRESULT hr = _CheckValidStateForAsyncInfoCall(); - if (FAILED(hr)) return hr; - if (!hr) return hr; - *errorCode = _M_errorCode; - return S_OK; - } - - virtual STDMETHODIMP get_Progress(typename _Attributes::_ProgressDelegateType** _ProgressHandler) - { - return _GetOnProgress(_ProgressHandler); - } - - virtual STDMETHODIMP put_Progress(typename _Attributes::_ProgressDelegateType* _ProgressHandler) - { - return _PutOnProgress(_ProgressHandler); - } - - virtual STDMETHODIMP Cancel() - { - if (_TransitionToState(_AsyncCancelPending)) - { - _OnCancel(); - } - return S_OK; - } - - virtual STDMETHODIMP Close() - { - if (_TransitionToState(_AsyncClosed)) - { - _OnClose(); - } - else - { - if (_M_currentStatus != _AsyncClosed) // Closed => Closed transition is just ignored - { - return E_ILLEGAL_STATE_CHANGE; - } - } - return S_OK; - } - - virtual STDMETHODIMP get_Completed(typename _Attributes::_CompletionDelegateType** _CompleteHandler) - { - _CheckValidStateForDelegateCall(); - if (!_CompleteHandler) return E_POINTER; - *_CompleteHandler = _M_completeDelegate.Get(); - return S_OK; - } - - virtual STDMETHODIMP put_Completed(typename _Attributes::_CompletionDelegateType* _CompleteHandler) - { - _CheckValidStateForDelegateCall(); - // this delegate property is "write once" - if (InterlockedIncrement(&_M_CompleteDelegateAssigned) == 1) - { - _M_completeDelegateContext = _ContextCallback::_CaptureCurrent(); - _M_completeDelegate = _CompleteHandler; - // Guarantee that the write of _M_completeDelegate is ordered with respect to the read of state below - // as perceived from _FireCompletion on another thread. - MemoryBarrier(); - if (_IsTerminalState()) - { - _FireCompletion(); - } - } - else - { - return E_ILLEGAL_DELEGATE_ASSIGNMENT; - } - return S_OK; - } - - protected: - // _Start - this is not externally visible since async operations "hot start" before returning to the caller - STDMETHODIMP _Start() - { - if (_TransitionToState(_AsyncStarted)) - { - _OnStart(); - } - else - { - return E_ILLEGAL_STATE_CHANGE; - } - return S_OK; - } - - HRESULT _FireCompletion() - { - HRESULT hr = S_OK; - _TryTransitionToCompleted(); - - // we guarantee that completion can only ever be fired once - if (_M_completeDelegate != nullptr && InterlockedIncrement(&_M_CallbackMade) == 1) - { - hr = _M_completeDelegateContext._CallInContext([=]() -> HRESULT { - ABI::Windows::Foundation::AsyncStatus status; - HRESULT hr; - if (SUCCEEDED(hr = this->get_Status(&status))) - _M_completeDelegate->Invoke((_Attributes::_AsyncBaseType*)this, status); - _M_completeDelegate = nullptr; - return hr; - }); - } - return hr; - } - - virtual STDMETHODIMP _GetOnProgress(typename _Attributes::_ProgressDelegateType** _ProgressHandler) - { - (void)_ProgressHandler; - return E_UNEXPECTED; - } - - virtual STDMETHODIMP _PutOnProgress(typename _Attributes::_ProgressDelegateType* _ProgressHandler) - { - (void)_ProgressHandler; - return E_UNEXPECTED; - } - - - bool _TryTransitionToCompleted() - { - return _TransitionToState(_AsyncStatusInternal::_AsyncCompleted); - } - - bool _TryTransitionToCancelled() - { - return _TransitionToState(_AsyncStatusInternal::_AsyncCanceled); - } - - bool _TryTransitionToError(const HRESULT error) - { - _InterlockedCompareExchange(reinterpret_cast(&_M_errorCode), error, S_OK); - return _TransitionToState(_AsyncStatusInternal::_AsyncError); - } - - // This method checks to see if the delegate properties can be - // modified in the current state and generates the appropriate - // error hr in the case of violation. - inline HRESULT _CheckValidStateForDelegateCall() - { - if (_M_currentStatus == _AsyncClosed) - { - return E_ILLEGAL_METHOD_CALL; - } - return S_OK; - } - - // This method checks to see if results can be collected in the - // current state and generates the appropriate error hr in - // the case of a violation. - inline HRESULT _CheckValidStateForResultsCall() - { - _AsyncStatusInternal _Current = _M_currentStatus; - - if (_Current == _AsyncError) - { - return _M_errorCode; - } -#pragma warning(push) -#pragma warning(disable: 4127) // Conditional expression is constant - // single result illegal before transition to Completed or Cancelled state - if (resultType == SingleResult) -#pragma warning(pop) - { - if (_Current != _AsyncCompleted) - { - return E_ILLEGAL_METHOD_CALL; - } - } - // multiple results can be called after Start has been called and before/after Completed - else if (_Current != _AsyncStarted && - _Current != _AsyncCancelPending && - _Current != _AsyncCanceled && - _Current != _AsyncCompleted) - { - return E_ILLEGAL_METHOD_CALL; - } - return S_OK; - } - - // This method can be called by derived classes periodically to determine - // whether the asynchronous operation should continue processing or should - // be halted. - inline bool _ContinueAsyncOperation() - { - return _M_currentStatus == _AsyncStarted; - } - - // These two methods are used to allow the async worker implementation do work on - // state transitions. No real "work" should be done in these methods. In other words - // they should not block for a long time on UI timescales. - virtual void _OnStart() = 0; - virtual void _OnClose() = 0; - virtual void _OnCancel() = 0; - - private: - - // This method is used to check if calls to the AsyncInfo properties - // (id, status, errorcode) are legal in the current state. It also - // generates the appropriate error hr to return in the case of an - // illegal call. - inline HRESULT _CheckValidStateForAsyncInfoCall() - { - _AsyncStatusInternal _Current = _M_currentStatus; - if (_Current == _AsyncClosed) - { - return E_ILLEGAL_METHOD_CALL; - } - else if (_Current == _AsyncCreated) - { - return E_ASYNC_OPERATION_NOT_STARTED; - } - return S_OK; - } - - inline bool _TransitionToState(const _AsyncStatusInternal _NewState) - { - _AsyncStatusInternal _Current = _M_currentStatus; - - // This enforces the valid state transitions of the asynchronous worker object - // state machine. - switch (_NewState) - { - case _AsyncStatusInternal::_AsyncStarted: - if (_Current != _AsyncCreated) - { - return false; - } - break; - case _AsyncStatusInternal::_AsyncCompleted: - if (_Current != _AsyncStarted && _Current != _AsyncCancelPending) - { - return false; - } - break; - case _AsyncStatusInternal::_AsyncCancelPending: - if (_Current != _AsyncStarted) - { - return false; - } - break; - case _AsyncStatusInternal::_AsyncCanceled: - if (_Current != _AsyncStarted && _Current != _AsyncCancelPending) - { - return false; - } - break; - case _AsyncStatusInternal::_AsyncError: - if (_Current != _AsyncStarted && _Current != _AsyncCancelPending) - { - return false; - } - break; - case _AsyncStatusInternal::_AsyncClosed: - if (!_IsTerminalState(_Current)) - { - return false; - } - break; - default: - return false; - break; - } - - // attempt the transition to the new state - // Note: if currentStatus_ == _Current, then there was no intervening write - // by the async work object and the swap succeeded. - _AsyncStatusInternal _RetState = static_cast<_AsyncStatusInternal>( - _InterlockedCompareExchange(reinterpret_cast(&_M_currentStatus), - _NewState, - static_cast(_Current))); - - // ICE returns the former state, if the returned state and the - // state we captured at the beginning of this method are the same, - // the swap succeeded. - return (_RetState == _Current); - } - - inline bool _IsTerminalState() - { - return _IsTerminalState(_M_currentStatus); - } - - inline bool _IsTerminalState(_AsyncStatusInternal status) - { - return (status == _AsyncError || - status == _AsyncCanceled || - status == _AsyncCompleted || - status == _AsyncClosed); - } - - private: - - _ContextCallback _M_completeDelegateContext; - Microsoft::WRL::ComPtr _M_completeDelegate; //ComPtr cannot be volatile as it does not have volatile accessors - _AsyncStatusInternal volatile _M_currentStatus; - HRESULT volatile _M_errorCode; - unsigned int _M_id; - long volatile _M_CompleteDelegateAssigned; - long volatile _M_CallbackMade; - }; - - // *************************************************************************** - // Progress Layer (optional): - // - - template< typename _Attributes, bool _HasProgress, _AsyncResultType _ResultType = SingleResult > - class _AsyncProgressBase abstract : public _AsyncInfoBase<_Attributes, _ResultType> - { - }; - - template< typename _Attributes, _AsyncResultType _ResultType> - class _AsyncProgressBase<_Attributes, true, _ResultType> abstract : public _AsyncInfoBase<_Attributes, _ResultType> - { - public: - - _AsyncProgressBase() : _AsyncInfoBase<_Attributes, _ResultType>(), - _M_progressDelegate(nullptr) - { - } - - virtual STDMETHODIMP _GetOnProgress(typename _Attributes::_ProgressDelegateType** _ProgressHandler) override - { - HRESULT hr = _CheckValidStateForDelegateCall(); - if (FAILED(hr)) return hr; - *_ProgressHandler = _M_progressDelegate; - return S_OK; - } - - virtual STDMETHODIMP _PutOnProgress(typename _Attributes::_ProgressDelegateType* _ProgressHandler) override - { - HRESULT hr = _CheckValidStateForDelegateCall(); - if (FAILED(hr)) return hr; - _M_progressDelegate = _ProgressHandler; - _M_progressDelegateContext = _ContextCallback::_CaptureCurrent(); - return S_OK; - } - - public: - - void _FireProgress(const typename _Attributes::_ProgressType_abi& _ProgressValue) - { - if (_M_progressDelegate != nullptr) - { - _M_progressDelegateContext._CallInContext([=]() -> HRESULT { - _M_progressDelegate->Invoke((_Attributes::_AsyncBaseType*)this, _ProgressValue); - return S_OK; - }); - } - } - - private: - - _ContextCallback _M_progressDelegateContext; - typename _Attributes::_ProgressDelegateType* _M_progressDelegate; - }; - - template - class _AsyncBaseProgressLayer abstract : public _AsyncProgressBase<_Attributes, _Attributes::_TakesProgress, _ResultType> - { - }; - - // *************************************************************************** - // Task Adaptation Layer: - // - - // - // _AsyncTaskThunkBase provides a bridge between IAsync and task. - // - template - class _AsyncTaskThunkBase abstract : public _AsyncBaseProgressLayer<_Attributes> - { - public: - - //AsyncAction* - virtual STDMETHODIMP GetResults() - { - HRESULT hr = _CheckValidStateForResultsCall(); - if (FAILED(hr)) return hr; - _M_task.get(); - return S_OK; - } - public: - typedef task<_ReturnType> _TaskType; - - _AsyncTaskThunkBase(const _TaskType& _Task) - : _M_task(_Task) - { - } - - _AsyncTaskThunkBase() - { - } -#if _MSC_VER < 1800 - void _SetTaskCreationAddressHint(void* _SourceAddressHint) - { - if (!(std::is_same<_Attributes::_AsyncKind, _TypeSelectorAsyncTask>::value)) - { - // Overwrite the creation address with the return address of create_async unless the - // lambda returned a task. If the create async lambda returns a task, that task is reused and - // we want to preserve its creation address hint. - _M_task._SetTaskCreationAddressHint(_SourceAddressHint); - } - } -#endif - protected: - virtual void _OnStart() override - { - _M_task.then([=](_TaskType _Antecedent) -> HRESULT { - try - { - _Antecedent.get(); - } - catch (Concurrency::task_canceled&) - { - _TryTransitionToCancelled(); - } - catch (IRestrictedErrorInfo*& _Ex) - { - HRESULT hr; - HRESULT _hr; - hr = _Ex->GetErrorDetails(NULL, &_hr, NULL, NULL); - if (SUCCEEDED(hr)) hr = _hr; - _TryTransitionToError(hr); - } - catch (...) - { - _TryTransitionToError(E_FAIL); - } - return _FireCompletion(); - }); - } - - protected: - _TaskType _M_task; - Concurrency::cancellation_token_source _M_cts; - }; - - template - class _AsyncTaskReturn abstract : public _AsyncTaskThunkBase<_Attributes, _Return> - { - public: - //AsyncOperation* - virtual STDMETHODIMP GetResults(_ReturnType* results) - { - HRESULT hr = _CheckValidStateForResultsCall(); - if (FAILED(hr)) return hr; - _M_task.get(); - *results = _M_results; - return S_OK; - } - template -#if _MSC_VER >= 1800 - void DoCreateTask(_Function _func, const _TaskCreationCallstack & _callstack) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts, &_M_results, _callstack); - } -#else - void DoCreateTask(_Function _func) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts, &_M_results); - } -#endif - protected: - _ReturnType _M_results; - }; - - template - class _AsyncTaskReturn<_Attributes, _ReturnType, void> abstract : public _AsyncTaskThunkBase<_Attributes, void> - { - public: - template -#if _MSC_VER >= 1800 - void DoCreateTask(_Function _func, const _TaskCreationCallstack & _callstack) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts, _callstack); - } -#else - void DoCreateTask(_Function _func) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts); - } -#endif - }; - - template - class _AsyncTaskReturn<_Attributes, void, task> abstract : public _AsyncTaskThunkBase<_Attributes, task> - { - public: - template -#if _MSC_VER >= 1800 - void DoCreateTask(_Function _func, const _TaskCreationCallstack & _callstack) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts, &_M_results, _callstack); - } -#else - void DoCreateTask(_Function _func) - { - _M_task = _Attributes::_Generate_Task(_func, this, _M_cts, &_M_results); - } -#endif - protected: - task _M_results; - }; - - template - class _AsyncTaskThunk : public _AsyncTaskReturn<_Attributes, typename _Attributes::_ReturnType_abi, typename _Attributes::_ReturnType> - { - public: - - _AsyncTaskThunk(const _TaskType& _Task) : - _AsyncTaskThunkBase(_Task) - { - } - - _AsyncTaskThunk() - { - } - - protected: - - virtual void _OnClose() override - { - } - - virtual void _OnCancel() override - { - _M_cts.cancel(); - } - }; - - // *************************************************************************** - // Async Creation Layer: - // - template - class _AsyncTaskGeneratorThunk : public _AsyncTaskThunk::_AsyncAttributes> - { - public: - - typedef typename _AsyncLambdaTypeTraits<_Function>::_AsyncAttributes _Attributes; - typedef typename _AsyncTaskThunk<_Attributes> _Base; - typedef typename _Attributes::_AsyncBaseType _AsyncBaseType; - -#if _MSC_VER >= 1800 - _AsyncTaskGeneratorThunk(const _Function& _Func, const _TaskCreationCallstack &_callstack) : _M_func(_Func), _M_creationCallstack(_callstack) -#else - _AsyncTaskGeneratorThunk(const _Function& _Func) : _M_func(_Func) -#endif - { - // Virtual call here is safe as the class is declared 'sealed' - _Start(); - } - - protected: - - // - // The only thing we must do different from the base class is we must spin the hot task on transition from Created->Started. Otherwise, - // let the base thunk handle everything. - // - - virtual void _OnStart() override - { - // - // Call the appropriate task generator to actually produce a task of the expected type. This might adapt the user lambda for progress reports, - // wrap the return result in a task, or allow for direct return of a task depending on the form of the lambda. - // -#if _MSC_VER >= 1800 - DoCreateTask<_Function>(_M_func, _M_creationCallstack); -#else - DoCreateTask<_Function>(_M_func); -#endif - _Base::_OnStart(); - } - - virtual void _OnCancel() override - { - _Base::_OnCancel(); - } - - private: -#if _MSC_VER >= 1800 - _TaskCreationCallstack _M_creationCallstack; -#endif - _Function _M_func; - }; -} // namespace details - -/// -/// Creates a Windows Runtime asynchronous construct based on a user supplied lambda or function object. The return type of create_async is -/// one of either IAsyncAction^, IAsyncActionWithProgress<TProgress>^, IAsyncOperation<TResult>^, or -/// IAsyncOperationWithProgress<TResult, TProgress>^ based on the signature of the lambda passed to the method. -/// -/// -/// The lambda or function object from which to create a Windows Runtime asynchronous construct. -/// -/// -/// An asynchronous construct represented by an IAsyncAction^, IAsyncActionWithProgress<TProgress>^, IAsyncOperation<TResult>^, or an -/// IAsyncOperationWithProgress<TResult, TProgress>^. The interface returned depends on the signature of the lambda passed into the function. -/// -/// -/// The return type of the lambda determines whether the construct is an action or an operation. -/// Lambdas that return void cause the creation of actions. Lambdas that return a result of type TResult cause the creation of -/// operations of TResult. -/// The lambda may also return a task<TResult> which encapsulates the aysnchronous work within itself or is the continuation of -/// a chain of tasks that represent the asynchronous work. In this case, the lambda itself is executed inline, since the tasks are the ones that -/// execute asynchronously, and the return type of the lambda is unwrapped to produce the asynchronous construct returned by create_async. -/// This implies that a lambda that returns a task<void> will cause the creation of actions, and a lambda that returns a task<TResult> will -/// cause the creation of operations of TResult. -/// The lambda may take either zero, one or two arguments. The valid arguments are progress_reporter<TProgress> and -/// cancellation_token, in that order if both are used. A lambda without arguments causes the creation of an asynchronous construct without -/// the capability for progress reporting. A lambda that takes a progress_reporter<TProgress> will cause create_async to return an asynchronous -/// construct which reports progress of type TProgress each time the report method of the progress_reporter object is called. A lambda that -/// takes a cancellation_token may use that token to check for cancellation, or pass it to tasks that it creates so that cancellation of the -/// asynchronous construct causes cancellation of those tasks. -/// If the body of the lambda or function object returns a result (and not a task<TResult>), the lamdba will be executed -/// asynchronously within the process MTA in the context of a task the Runtime implicitly creates for it. The IAsyncInfo::Cancel method will -/// cause cancellation of the implicit task. -/// If the body of the lambda returns a task, the lamba executes inline, and by declaring the lambda to take an argument of type -/// cancellation_token you can trigger cancellation of any tasks you create within the lambda by passing that token in when you create them. -/// You may also use the register_callback method on the token to cause the Runtime to invoke a callback when you call IAsyncInfo::Cancel on -/// the async operation or action produced.. -/// This function is only available to Windows Store apps. -/// -/// -/// -/// -/**/ -template -__declspec(noinline) // Ask for no inlining so that the _ReturnAddress intrinsic gives us the expected result -details::_AsyncTaskGeneratorThunk<_Function>* create_async(const _Function& _Func) -{ - static_assert(std::is_same(_Func, 0, 0, 0, 0, 0, 0, 0, 0)), std::true_type>::value, - "argument to create_async must be a callable object taking zero, one, two or three arguments"); -#if _MSC_VER >= 1800 - Microsoft::WRL::ComPtr> _AsyncInfo = Microsoft::WRL::Make>(_Func, _CAPTURE_CALLSTACK()); -#else - Microsoft::WRL::ComPtr> _AsyncInfo = Microsoft::WRL::Make>(_Func); - _AsyncInfo->_SetTaskCreationAddressHint(_ReturnAddress()); -#endif - return _AsyncInfo.Detach(); -} - -namespace details -{ -#if _MSC_VER < 1800 - // Internal API which retrieves the next async id. - _CRTIMP2 unsigned int __cdecl _GetNextAsyncId(); -#endif - // Helper struct for when_all operators to know when tasks have completed - template - struct _RunAllParam - { - _RunAllParam() : _M_completeCount(0), _M_numTasks(0) - { - } - - void _Resize(size_t _Len, bool _SkipVector = false) - { - _M_numTasks = _Len; - if (!_SkipVector) -#if _MSC_VER >= 1800 - { - _M_vector._Result.resize(_Len); - } -#else - _M_vector.resize(_Len); - _M_contexts.resize(_Len); -#endif - } - - task_completion_event<_Unit_type> _M_completed; - atomic_size_t _M_completeCount; -#if _MSC_VER >= 1800 - _ResultHolder > _M_vector; - _ResultHolder<_Type> _M_mergeVal; -#else - std::vector<_Type> _M_vector; - std::vector<_ContextCallback> _M_contexts; - _Type _M_mergeVal; -#endif - size_t _M_numTasks; - }; - -#if _MSC_VER >= 1800 - template - struct _RunAllParam > - { - _RunAllParam() : _M_completeCount(0), _M_numTasks(0) - { - } - - void _Resize(size_t _Len, bool _SkipVector = false) - { - _M_numTasks = _Len; - - if (!_SkipVector) - { - _M_vector.resize(_Len); - } - } - - task_completion_event<_Unit_type> _M_completed; - std::vector<_ResultHolder > > _M_vector; - atomic_size_t _M_completeCount; - size_t _M_numTasks; - }; -#endif - - // Helper struct specialization for void - template<> -#if _MSC_VER >= 1800 - struct _RunAllParam<_Unit_type> -#else - struct _RunAllParam -#endif - { - _RunAllParam() : _M_completeCount(0), _M_numTasks(0) - { - } - - void _Resize(size_t _Len) - { - _M_numTasks = _Len; - } - - task_completion_event<_Unit_type> _M_completed; - atomic_size_t _M_completeCount; - size_t _M_numTasks; - }; - - inline void _JoinAllTokens_Add(const Concurrency::cancellation_token_source& _MergedSrc, Concurrency::details::_CancellationTokenState *_PJoinedTokenState) - { - if (_PJoinedTokenState != nullptr && _PJoinedTokenState != Concurrency::details::_CancellationTokenState::_None()) - { - Concurrency::cancellation_token _T = Concurrency::cancellation_token::_FromImpl(_PJoinedTokenState); - _T.register_callback([=](){ - _MergedSrc.cancel(); - }); - } - } - - template - void _WhenAllContinuationWrapper(_RunAllParam<_ElementType>* _PParam, _Function _Func, task<_TaskType>& _Task) - { - if (_Task._GetImpl()->_IsCompleted()) - { - _Func(); -#if _MSC_VER >= 1800 - if (Concurrency::details::atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks) -#else - if (_InterlockedIncrementSizeT(&_PParam->_M_completeCount) == _PParam->_M_numTasks) -#endif - { - // Inline execute its direct continuation, the _ReturnTask - _PParam->_M_completed.set(_Unit_type()); - // It's safe to delete it since all usage of _PParam in _ReturnTask has been finished. - delete _PParam; - } - } - else - { - _CONCRT_ASSERT(_Task._GetImpl()->_IsCanceled()); - if (_Task._GetImpl()->_HasUserException()) - { - // _Cancel will return false if the TCE is already canceled with or without exception - _PParam->_M_completed._Cancel(_Task._GetImpl()->_GetExceptionHolder()); - } - else - { - _PParam->_M_completed._Cancel(); - } -#if _MSC_VER >= 1800 - if (Concurrency::details::atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks) -#else - if (_InterlockedIncrementSizeT(&_PParam->_M_completeCount) == _PParam->_M_numTasks) -#endif - { - delete _PParam; - } - } - } - - template - struct _WhenAllImpl - { -#if _MSC_VER >= 1800 - static task> _Perform(const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End) -#else - static task> _Perform(Concurrency::details::_CancellationTokenState *_PTokenState, _Iterator _Begin, _Iterator _End) -#endif - { -#if _MSC_VER >= 1800 - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; -#endif - auto _PParam = new _RunAllParam<_ElementType>(); - Concurrency::cancellation_token_source _MergedSource; - - // Step1: Create task completion event. -#if _MSC_VER >= 1800 - task_options _Options(_TaskOptions); - _Options.set_cancellation_token(_MergedSource.get_token()); - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options); -#else - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _MergedSource.get_token()); -#endif - // The return task must be created before step 3 to enforce inline execution. - auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type, std::vector<_ElementType>* retVal) -> HRESULT { -#if _MSC_VER >= 1800 - * retVal = _PParam->_M_vector.Get(); -#else - auto _Result = _PParam->_M_vector; // copy by value - - size_t _Index = 0; - for (auto _It = _Result.begin(); _It != _Result.end(); ++_It) - { - *_It = _ResultContext<_ElementType>::_GetValue(*_It, _PParam->_M_contexts[_Index++], false); - } - *retVal = _Result; -#endif - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, true); -#endif - // Step2: Combine and check tokens, and count elements in range. - if (_PTokenState) - { - details::_JoinAllTokens_Add(_MergedSource, _PTokenState); - _PParam->_Resize(static_cast(std::distance(_Begin, _End))); - } - else - { - size_t _TaskNum = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - _TaskNum++; - details::_JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState); - } - _PParam->_Resize(_TaskNum); - } - - // Step3: Check states of previous tasks. - if (_Begin == _End) - { - _PParam->_M_completed.set(_Unit_type()); - delete _PParam; - } - else - { - size_t _Index = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - if (_PTask->is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PTask->_Then([_PParam, _Index](task<_ElementType> _ResultTask) -> HRESULT { - -#if _MSC_VER >= 1800 - // Dev10 compiler bug - typedef _ElementType _ElementTypeDev10; - auto _PParamCopy = _PParam; - auto _IndexCopy = _Index; - auto _Func = [_PParamCopy, _IndexCopy, &_ResultTask](){ - _PParamCopy->_M_vector._Result[_IndexCopy] = _ResultTask._GetImpl()->_GetResult(); - }; -#else - auto _Func = [_PParam, _Index, &_ResultTask](){ - _PParam->_M_vector[_Index] = _ResultTask._GetImpl()->_GetResult(); - _PParam->_M_contexts[_Index] = _ResultContext<_ElementType>::_GetContext(false); - }; -#endif - _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - - _Index++; - } - } - - return _ReturnTask; - } - }; - - template - struct _WhenAllImpl, _Iterator> - { -#if _MSC_VER >= 1800 - static task> _Perform(const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End) -#else - static task> _Perform(Concurrency::details::_CancellationTokenState *_PTokenState, _Iterator _Begin, _Iterator _End) -#endif - { -#if _MSC_VER >= 1800 - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; -#endif - auto _PParam = new _RunAllParam>(); - Concurrency::cancellation_token_source _MergedSource; - - // Step1: Create task completion event. -#if _MSC_VER >= 1800 - task_options _Options(_TaskOptions); - _Options.set_cancellation_token(_MergedSource.get_token()); - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options); -#else - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _MergedSource.get_token()); -#endif - // The return task must be created before step 3 to enforce inline execution. - auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type, std::vector<_ElementType>* retVal) -> HRESULT { - _CONCRT_ASSERT(_PParam->_M_completeCount == _PParam->_M_numTasks); - std::vector<_ElementType> _Result; - for (size_t _I = 0; _I < _PParam->_M_numTasks; _I++) - { -#if _MSC_VER >= 1800 - const std::vector<_ElementType>& _Vec = _PParam->_M_vector[_I].Get(); -#else - std::vector<_ElementType>& _Vec = _PParam->_M_vector[_I]; - - for (auto _It = _Vec.begin(); _It != _Vec.end(); ++_It) - { - *_It = _ResultContext<_ElementType>::_GetValue(*_It, _PParam->_M_contexts[_I], false); - } -#endif - _Result.insert(_Result.end(), _Vec.begin(), _Vec.end()); - } - *retVal = _Result; - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, true); -#endif - - // Step2: Combine and check tokens, and count elements in range. - if (_PTokenState) - { - details::_JoinAllTokens_Add(_MergedSource, _PTokenState); - _PParam->_Resize(static_cast(std::distance(_Begin, _End))); - } - else - { - size_t _TaskNum = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - _TaskNum++; - details::_JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState); - } - _PParam->_Resize(_TaskNum); - } - - // Step3: Check states of previous tasks. - if (_Begin == _End) - { - _PParam->_M_completed.set(_Unit_type()); - delete _PParam; - } - else - { - size_t _Index = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - if (_PTask->is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PTask->_Then([_PParam, _Index](task> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - typedef _ElementType _ElementTypeDev10; - auto _PParamCopy = _PParam; - auto _IndexCopy = _Index; - auto _Func = [_PParamCopy, _IndexCopy, &_ResultTask]() { - _PParamCopy->_M_vector[_IndexCopy].Set(_ResultTask._GetImpl()->_GetResult()); - }; -#else - auto _Func = [_PParam, _Index, &_ResultTask]() { - _PParam->_M_vector[_Index] = _ResultTask._GetImpl()->_GetResult(); - _PParam->_M_contexts[_Index] = _ResultContext<_ElementType>::_GetContext(false); - }; -#endif - _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - - _Index++; - } - } - - return _ReturnTask; - } - }; - - template - struct _WhenAllImpl - { -#if _MSC_VER >= 1800 - static task _Perform(const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End) -#else - static task _Perform(Concurrency::details::_CancellationTokenState *_PTokenState, _Iterator _Begin, _Iterator _End) -#endif - { -#if _MSC_VER >= 1800 - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; -#endif - auto _PParam = new _RunAllParam<_Unit_type>(); - Concurrency::cancellation_token_source _MergedSource; - - // Step1: Create task completion event. -#if _MSC_VER >= 1800 - task_options _Options(_TaskOptions); - _Options.set_cancellation_token(_MergedSource.get_token()); - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _Options); -#else - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _MergedSource.get_token()); -#endif - // The return task must be created before step 3 to enforce inline execution. - auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type) -> HRESULT { return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, false); -#endif - - // Step2: Combine and check tokens, and count elements in range. - if (_PTokenState) - { - details::_JoinAllTokens_Add(_MergedSource, _PTokenState); - _PParam->_Resize(static_cast(std::distance(_Begin, _End))); - } - else - { - size_t _TaskNum = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - _TaskNum++; - details::_JoinAllTokens_Add(_MergedSource, _PTask->_GetImpl()->_M_pTokenState); - } - _PParam->_Resize(_TaskNum); - } - - // Step3: Check states of previous tasks. - if (_Begin == _End) - { - _PParam->_M_completed.set(_Unit_type()); - delete _PParam; - } - else - { - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - if (_PTask->is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PTask->_Then([_PParam](task _ResultTask) -> HRESULT { - - auto _Func = []() -> HRESULT { return S_OK; }; - _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - } - } - - return _ReturnTask; - } - }; - - template - task> _WhenAllVectorAndValue(const task>& _VectorTask, const task<_ReturnType>& _ValueTask, - bool _OutputVectorFirst) - { - auto _PParam = new _RunAllParam<_ReturnType>(); - Concurrency::cancellation_token_source _MergedSource; - - // Step1: Create task completion event. - task<_Unit_type> _All_tasks_completed(_PParam->_M_completed, _MergedSource.get_token()); - // The return task must be created before step 3 to enforce inline execution. - auto _ReturnTask = _All_tasks_completed._Then([=](_Unit_type, std::vector<_ReturnType>* retVal) -> HRESULT { - _CONCRT_ASSERT(_PParam->_M_completeCount == 2); -#if _MSC_VER >= 1800 - auto _Result = _PParam->_M_vector.Get(); // copy by value - auto _mergeVal = _PParam->_M_mergeVal.Get(); -#else - auto _Result = _PParam->_M_vector; // copy by value - for (auto _It = _Result.begin(); _It != _Result.end(); ++_It) - { - *_It = _ResultContext<_ReturnType>::_GetValue(*_It, _PParam->_M_contexts[0], false); - } -#endif - - if (_OutputVectorFirst == true) - { -#if _MSC_VER >= 1800 - _Result.push_back(_mergeVal); -#else - _Result.push_back(_ResultContext<_ReturnType>::_GetValue(_PParam->_M_mergeVal, _PParam->_M_contexts[1], false)); -#endif - } - else - { -#if _MSC_VER >= 1800 - _Result.insert(_Result.begin(), _mergeVal); -#else - _Result.insert(_Result.begin(), _ResultContext<_ReturnType>::_GetValue(_PParam->_M_mergeVal, _PParam->_M_contexts[1], false)); -#endif - } - *retVal = _Result; - return S_OK; - }, nullptr, true); - - // Step2: Combine and check tokens. - _JoinAllTokens_Add(_MergedSource, _VectorTask._GetImpl()->_M_pTokenState); - _JoinAllTokens_Add(_MergedSource, _ValueTask._GetImpl()->_M_pTokenState); - - // Step3: Check states of previous tasks. - _PParam->_Resize(2, true); - - if (_VectorTask.is_apartment_aware() || _ValueTask.is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - _VectorTask._Then([_PParam](task> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - typedef _ReturnType _ReturnTypeDev10; - auto _PParamCopy = _PParam; - auto _Func = [_PParamCopy, &_ResultTask]() { - auto _ResultLocal = _ResultTask._GetImpl()->_GetResult(); - _PParamCopy->_M_vector.Set(_ResultLocal); - }; -#else - auto _Func = [_PParam, &_ResultTask]() { - _PParam->_M_vector = _ResultTask._GetImpl()->_GetResult(); - _PParam->_M_contexts[0] = _ResultContext<_ReturnType>::_GetContext(false); - }; -#endif - - _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, _CancellationTokenState::_None()); -#else - }, _CancellationTokenState::_None(), false); -#endif - _ValueTask._Then([_PParam](task<_ReturnType> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - typedef _ReturnType _ReturnTypeDev10; - auto _PParamCopy = _PParam; - auto _Func = [_PParamCopy, &_ResultTask]() { - auto _ResultLocal = _ResultTask._GetImpl()->_GetResult(); - _PParamCopy->_M_mergeVal.Set(_ResultLocal); - }; -#else - auto _Func = [_PParam, &_ResultTask]() { - _PParam->_M_mergeVal = _ResultTask._GetImpl()->_GetResult(); - _PParam->_M_contexts[1] = _ResultContext<_ReturnType>::_GetContext(false); - }; -#endif - _WhenAllContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, _CancellationTokenState::_None()); -#else - }, _CancellationTokenState::_None(), false); -#endif - - return _ReturnTask; - } -} // namespace details - -#if _MSC_VER < 1800 -/// -/// Creates a task that will complete successfully when all of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the input iterator. -/// -/// -/// The position of the first element in the range of elements to be combined into the resulting task. -/// -/// -/// The position of the first element beyond the range of elements to be combined into the resulting task. -/// -/// -/// A task that completes sucessfully when all of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -auto when_all(_Iterator _Begin, _Iterator _End) --> decltype (details::_WhenAllImpl::value_type::result_type, _Iterator>::_Perform(nullptr, _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAllImpl<_ElementType, _Iterator>::_Perform(nullptr, _Begin, _End); -} -#endif - -/// -/// Creates a task that will complete successfully when all of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the input iterator. -/// -/// -/// The position of the first element in the range of elements to be combined into the resulting task. -/// -/// -/// The position of the first element beyond the range of elements to be combined into the resulting task. -/// -/// -/// The cancellation token which controls cancellation of the returned task. If you do not provide a cancellation token, the resulting -/// task will be created with a token that is a combination of all the cancelable tokens (tokens created by methods other than -/// cancellation_token::none()of the tasks supplied. -/// -/// -/// A task that completes sucessfully when all of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -#if _MSC_VER >= 1800 -auto when_all(_Iterator _Begin, _Iterator _End, const task_options& _TaskOptions = task_options()) --> decltype (details::_WhenAllImpl::value_type::result_type, _Iterator>::_Perform(_TaskOptions, _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAllImpl<_ElementType, _Iterator>::_Perform(_TaskOptions, _Begin, _End); -} -#else -auto when_all(_Iterator _Begin, _Iterator _End, Concurrency::cancellation_token _CancellationToken) --> decltype (details::_WhenAllImpl::value_type::result_type, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAllImpl<_ElementType, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End); -} -#endif - -/// -/// Creates a task that will complete successfully when both of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes successfully when both of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// To allow for a construct of the sort taskA && taskB && taskC, which are combined in pairs, the && operator -/// produces a task<std::vector<T>> if either one or both of the tasks are of type task<std::vector<T>>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator&&(const task<_ReturnType> & _Lhs, const task<_ReturnType> & _Rhs) -{ - task<_ReturnType> _PTasks[2] = { _Lhs, _Rhs }; - return when_all(_PTasks, _PTasks + 2); -} - -/// -/// Creates a task that will complete successfully when both of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes successfully when both of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// To allow for a construct of the sort taskA && taskB && taskC, which are combined in pairs, the && operator -/// produces a task<std::vector<T>> if either one or both of the tasks are of type task<std::vector<T>>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator&&(const task> & _Lhs, const task<_ReturnType> & _Rhs) -{ - return details::_WhenAllVectorAndValue(_Lhs, _Rhs, true); -} - -/// -/// Creates a task that will complete successfully when both of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes successfully when both of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// To allow for a construct of the sort taskA && taskB && taskC, which are combined in pairs, the && operator -/// produces a task<std::vector<T>> if either one or both of the tasks are of type task<std::vector<T>>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator&&(const task<_ReturnType> & _Lhs, const task> & _Rhs) -{ - return details::_WhenAllVectorAndValue(_Rhs, _Lhs, false); -} - -/// -/// Creates a task that will complete successfully when both of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes successfully when both of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// To allow for a construct of the sort taskA && taskB && taskC, which are combined in pairs, the && operator -/// produces a task<std::vector<T>> if either one or both of the tasks are of type task<std::vector<T>>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator&&(const task> & _Lhs, const task> & _Rhs) -{ - task> _PTasks[2] = { _Lhs, _Rhs }; - return when_all(_PTasks, _PTasks + 2); -} - -/// -/// Creates a task that will complete successfully when both of the tasks supplied as arguments complete successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes successfully when both of the input tasks have completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>>. If the input tasks are of type void the output -/// task will also be a task<void>. -/// To allow for a construct of the sort taskA && taskB && taskC, which are combined in pairs, the && operator -/// produces a task<std::vector<T>> if either one or both of the tasks are of type task<std::vector<T>>. -/// -/// -/// If one of the tasks is canceled or throws an exception, the returned task will complete early, in the canceled state, and the exception, -/// if one is encoutered, will be thrown if you call get() or wait() on that task. -/// -/// -/**/ -inline task operator&&(const task & _Lhs, const task & _Rhs) -{ - task _PTasks[2] = { _Lhs, _Rhs }; - return when_all(_PTasks, _PTasks + 2); -} - -namespace details -{ - // Helper struct for when_any operators to know when tasks have completed - template - struct _RunAnyParam - { - _RunAnyParam() : _M_completeCount(0), _M_numTasks(0), _M_exceptionRelatedToken(nullptr), _M_fHasExplicitToken(false) - { - } - ~_RunAnyParam() - { - if (Concurrency::details::_CancellationTokenState::_IsValid(_M_exceptionRelatedToken)) - _M_exceptionRelatedToken->_Release(); - } - task_completion_event<_CompletionType> _M_Completed; - Concurrency::cancellation_token_source _M_cancellationSource; - Concurrency::details::_CancellationTokenState* _M_exceptionRelatedToken; - atomic_size_t _M_completeCount; - size_t _M_numTasks; - bool _M_fHasExplicitToken; - }; - - template - void _WhenAnyContinuationWrapper(_RunAnyParam<_CompletionType> * _PParam, const _Function & _Func, task<_TaskType>& _Task) - { - bool _IsTokenCancled = !_PParam->_M_fHasExplicitToken && _Task._GetImpl()->_M_pTokenState != Concurrency::details::_CancellationTokenState::_None() && _Task._GetImpl()->_M_pTokenState->_IsCanceled(); - if (_Task._GetImpl()->_IsCompleted() && !_IsTokenCancled) - { - _Func(); -#if _MSC_VER >= 1800 - if (Concurrency::details::atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks) -#else - if (_InterlockedIncrementSizeT(&_PParam->_M_completeCount) == _PParam->_M_numTasks) -#endif - { - delete _PParam; - } - } - else - { - _CONCRT_ASSERT(_Task._GetImpl()->_IsCanceled() || _IsTokenCancled); - if (_Task._GetImpl()->_HasUserException() && !_IsTokenCancled) - { - if (_PParam->_M_Completed._StoreException(_Task._GetImpl()->_GetExceptionHolder())) - { - // This can only enter once. - _PParam->_M_exceptionRelatedToken = _Task._GetImpl()->_M_pTokenState; - _CONCRT_ASSERT(_PParam->_M_exceptionRelatedToken); - // Deref token will be done in the _PParam destructor. - if (_PParam->_M_exceptionRelatedToken != Concurrency::details::_CancellationTokenState::_None()) - { - _PParam->_M_exceptionRelatedToken->_Reference(); - } - } - } - -#if _MSC_VER >= 1800 - if (Concurrency::details::atomic_increment(_PParam->_M_completeCount) == _PParam->_M_numTasks) -#else - if (_InterlockedIncrementSizeT(&_PParam->_M_completeCount) == _PParam->_M_numTasks) -#endif - { - // If no one has be completed so far, we need to make some final cancellation decision. - if (!_PParam->_M_Completed._IsTriggered()) - { - // If we already explicit token, we can skip the token join part. - if (!_PParam->_M_fHasExplicitToken) - { - if (_PParam->_M_exceptionRelatedToken) - { - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _PParam->_M_exceptionRelatedToken); - } - else - { - // If haven't captured any exception token yet, there was no exception for all those tasks, - // so just pick a random token (current one) for normal cancellation. - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _Task._GetImpl()->_M_pTokenState); - } - } - // Do exception cancellation or normal cancellation based on whether it has stored exception. - _PParam->_M_Completed._Cancel(); - } - delete _PParam; - } - } - } - - template - struct _WhenAnyImpl - { -#if _MSC_VER >= 1800 - static task> _Perform(const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End) -#else - static task> _Perform(Concurrency::details::_CancellationTokenState *_PTokenState, _Iterator _Begin, _Iterator _End) -#endif - { - if (_Begin == _End) - { - throw Concurrency::invalid_operation("when_any(begin, end) cannot be called on an empty container."); - } -#if _MSC_VER >= 1800 - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; -#endif - auto _PParam = new _RunAnyParam, Concurrency::details::_CancellationTokenState *>>(); - - if (_PTokenState) - { - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _PTokenState); - _PParam->_M_fHasExplicitToken = true; - } -#if _MSC_VER >= 1800 - task_options _Options(_TaskOptions); - _Options.set_cancellation_token(_PParam->_M_cancellationSource.get_token()); - task, Concurrency::details::_CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _Options); -#else - task, Concurrency::details::_CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); - _Any_tasks_completed._GetImpl()->_M_fRuntimeAggregate = true; -#endif - // Keep a copy ref to the token source - auto _CancellationSource = _PParam->_M_cancellationSource; - - _PParam->_M_numTasks = static_cast(std::distance(_Begin, _End)); - size_t index = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - if (_PTask->is_apartment_aware()) - { - _Any_tasks_completed._SetAsync(); - } - - _PTask->_Then([_PParam, index](task<_ElementType> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - auto _PParamCopy = _PParam; // Dev10 - auto _IndexCopy = index; // Dev10 - auto _Func = [&_ResultTask, _PParamCopy, _IndexCopy]() { - _PParamCopy->_M_Completed.set(std::make_pair(std::make_pair(_ResultTask._GetImpl()->_GetResult(), _IndexCopy), _ResultTask._GetImpl()->_M_pTokenState)); - }; -#else - auto _Func = [&_ResultTask, _PParam, index]() { - _PParam->_M_Completed.set(std::make_pair(std::make_pair(_ResultTask._GetImpl()->_GetResult(), index), _ResultTask._GetImpl()->_M_pTokenState)); - }; -#endif - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - index++; - } - - // All _Any_tasks_completed._SetAsync() must be finished before this return continuation task being created. - return _Any_tasks_completed._Then([=](std::pair, Concurrency::details::_CancellationTokenState *> _Result, std::pair<_ElementType, size_t>* retVal) -> HRESULT { - _CONCRT_ASSERT(_Result.second); - if (!_PTokenState) - { - details::_JoinAllTokens_Add(_CancellationSource, _Result.second); - } - *retVal = _Result.first; - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, true); -#endif - } - }; - - template - struct _WhenAnyImpl - { -#if _MSC_VER >= 1800 - static task _Perform(const task_options& _TaskOptions, _Iterator _Begin, _Iterator _End) -#else - static task _Perform(Concurrency::details::_CancellationTokenState *_PTokenState, _Iterator _Begin, _Iterator _End) -#endif - { - if (_Begin == _End) - { - throw Concurrency::invalid_operation("when_any(begin, end) cannot be called on an empty container."); - } -#if _MSC_VER >= 1800 - Concurrency::details::_CancellationTokenState *_PTokenState = _TaskOptions.has_cancellation_token() ? _TaskOptions.get_cancellation_token()._GetImplValue() : nullptr; -#endif - auto _PParam = new _RunAnyParam>(); - - if (_PTokenState) - { - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _PTokenState); - _PParam->_M_fHasExplicitToken = true; - } - -#if _MSC_VER >= 1800 - task_options _Options(_TaskOptions); - _Options.set_cancellation_token(_PParam->_M_cancellationSource.get_token()); - task> _Any_tasks_completed(_PParam->_M_Completed, _Options); -#else - task> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); -#endif - // Keep a copy ref to the token source - auto _CancellationSource = _PParam->_M_cancellationSource; - - _PParam->_M_numTasks = static_cast(std::distance(_Begin, _End)); - size_t index = 0; - for (auto _PTask = _Begin; _PTask != _End; ++_PTask) - { - if (_PTask->is_apartment_aware()) - { - _Any_tasks_completed._SetAsync(); - } - - _PTask->_Then([_PParam, index](task _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - auto _PParamCopy = _PParam; // Dev10 - auto _IndexCopy = index; // Dev10 - auto _Func = [&_ResultTask, _PParamCopy, _IndexCopy]() { - _PParamCopy->_M_Completed.set(std::make_pair(_IndexCopy, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#else - auto _Func = [&_ResultTask, _PParam, index]() { - _PParam->_M_Completed.set(std::make_pair(index, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#endif - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - - index++; - } - - // All _Any_tasks_completed._SetAsync() must be finished before this return continuation task being created. - return _Any_tasks_completed._Then([=](std::pair _Result, size_t* retVal) -> HRESULT { - _CONCRT_ASSERT(_Result.second); - if (!_PTokenState) - { - details::_JoinAllTokens_Add(_CancellationSource, _Result.second); - } - *retVal = _Result.first; - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, false); -#endif - } - }; -} // namespace details - -/// -/// Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the input iterator. -/// -/// -/// The position of the first element in the range of elements to be combined into the resulting task. -/// -/// -/// The position of the first element beyond the range of elements to be combined into the resulting task. -/// -/// -/// A task that completes successfully when any one of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::pair<T, size_t>>>, where the first element of the pair is the result -/// of the completing task, and the second element is the index of the task that finished. If the input tasks are of type void -/// the output is a task<size_t>, where the result is the index of the completing task. -/// -/// -/**/ -template -#if _MSC_VER >= 1800 -auto when_any(_Iterator _Begin, _Iterator _End, const task_options& _TaskOptions = task_options()) --> decltype (details::_WhenAnyImpl::value_type::result_type, _Iterator>::_Perform(_TaskOptions, _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAnyImpl<_ElementType, _Iterator>::_Perform(_TaskOptions, _Begin, _End); -} -#else -auto when_any(_Iterator _Begin, _Iterator _End) --> decltype (details::_WhenAnyImpl::value_type::result_type, _Iterator>::_Perform(nullptr, _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAnyImpl<_ElementType, _Iterator>::_Perform(nullptr, _Begin, _End); -} -#endif - -/// -/// Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the input iterator. -/// -/// -/// The position of the first element in the range of elements to be combined into the resulting task. -/// -/// -/// The position of the first element beyond the range of elements to be combined into the resulting task. -/// -/// -/// The cancellation token which controls cancellation of the returned task. If you do not provide a cancellation token, the resulting -/// task will receive the cancellation token of the task that causes it to complete. -/// -/// -/// A task that completes successfully when any one of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::pair<T, size_t>>>, where the first element of the pair is the result -/// of the completing task, and the second element is the index of the task that finished. If the input tasks are of type void -/// the output is a task<size_t>, where the result is the index of the completing task. -/// -/// -/**/ -template -auto when_any(_Iterator _Begin, _Iterator _End, Concurrency::cancellation_token _CancellationToken) --> decltype (details::_WhenAnyImpl::value_type::result_type, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End)) -{ - typedef typename std::iterator_traits<_Iterator>::value_type::result_type _ElementType; - return details::_WhenAnyImpl<_ElementType, _Iterator>::_Perform(_CancellationToken._GetImplValue(), _Begin, _End); -} - -/// -/// Creates a task that will complete successfully when either of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes sucessfully when either of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>. If the input tasks are of type void the output task -/// will also be a task<void>. -/// To allow for a construct of the sort taskA || taskB && taskC, which are combined in pairs, with && taking precedence -/// over ||, the operator|| produces a task<std::vector<T>> if one of the tasks is of type task<std::vector<T>> -/// and the other one is of type task<T>. -/// -/// -/// If both of the tasks are canceled or throw exceptions, the returned task will complete in the canceled state, and one of the exceptions, -/// if any are encountered, will be thrown when you call get() or wait() on that task. -/// -/// -/**/ -template -task<_ReturnType> operator||(const task<_ReturnType> & _Lhs, const task<_ReturnType> & _Rhs) -{ -#if _MSC_VER >= 1800 - auto _PParam = new details::_RunAnyParam>(); - - task> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); - // Chain the return continuation task here to ensure it will get inline execution when _M_Completed.set is called, - // So that _PParam can be used before it getting deleted. - auto _ReturnTask = _Any_tasks_completed._Then([=](std::pair<_ReturnType, size_t> _Ret, _ReturnType* retVal) -> HRESULT { - _CONCRT_ASSERT(_Ret.second); - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, reinterpret_cast(_Ret.second)); - *retVal = _Ret.first; - return S_OK; - }, nullptr); -#else - auto _PParam = new details::_RunAnyParam>(); - - task> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); - // Chain the return continuation task here to ensure it will get inline execution when _M_Completed.set is called, - // So that _PParam can be used before it getting deleted. - auto _ReturnTask = _Any_tasks_completed._Then([=](std::pair<_ReturnType, Concurrency::details::_CancellationTokenState *> _Ret, _ReturnType* retVal) -> HRESULT { - _CONCRT_ASSERT(_Ret.second); - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _Ret.second); - *retVal = _Ret.first; - return S_OK; - }, nullptr, false); -#endif - if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PParam->_M_numTasks = 2; - auto _Continuation = [_PParam](task<_ReturnType> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - auto _PParamCopy = _PParam; - auto _Func = [&_ResultTask, _PParamCopy]() { - _PParamCopy->_M_Completed.set(std::make_pair(_ResultTask._GetImpl()->_GetResult(), reinterpret_cast(_ResultTask._GetImpl()->_M_pTokenState))); - }; -#else - auto _Func = [&_ResultTask, _PParam]() { - _PParam->_M_Completed.set(std::make_pair(_ResultTask._GetImpl()->_GetResult(), _ResultTask._GetImpl()->_M_pTokenState)); - }; -#endif - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; - }; - -#if _MSC_VER >= 1800 - _Lhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None()); - _Rhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None()); -#else - _Lhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None(), false); - _Rhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - return _ReturnTask; -} - -/// -/// Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes sucessfully when either of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>. If the input tasks are of type void the output task -/// will also be a task<void>. -/// To allow for a construct of the sort taskA || taskB && taskC, which are combined in pairs, with && taking precedence -/// over ||, the operator|| produces a task<std::vector<T>> if one of the tasks is of type task<std::vector<T>> -/// and the other one is of type task<T>. -/// -/// -/// If both of the tasks are canceled or throw exceptions, the returned task will complete in the canceled state, and one of the exceptions, -/// if any are encountered, will be thrown when you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator||(const task> & _Lhs, const task<_ReturnType> & _Rhs) -{ - auto _PParam = new details::_RunAnyParam, Concurrency::details::_CancellationTokenState *>>(); - - task, Concurrency::details::_CancellationTokenState *>> _Any_tasks_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); -#if _MSC_VER < 1800 - _Any_tasks_completed._GetImpl()->_M_fRuntimeAggregate = true; -#endif - // Chain the return continuation task here to ensure it will get inline execution when _M_Completed.set is called, - // So that _PParam can be used before it getting deleted. - auto _ReturnTask = _Any_tasks_completed._Then([=](std::pair, Concurrency::details::_CancellationTokenState *> _Ret, std::vector<_ReturnType>* retVal) -> HRESULT { - _CONCRT_ASSERT(_Ret.second); - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _Ret.second); - *retVal = _Ret.first; - return S_OK; - }, nullptr, true); - - if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PParam->_M_numTasks = 2; - _Lhs._Then([_PParam](task> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - auto _PParamCopy = _PParam; - auto _Func = [&_ResultTask, _PParamCopy]() { - auto _Result = _ResultTask._GetImpl()->_GetResult(); - _PParamCopy->_M_Completed.set(std::make_pair(_Result, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#else - auto _Func = [&_ResultTask, _PParam]() { - std::vector<_ReturnType> _Result = _ResultTask._GetImpl()->_GetResult(); - _PParam->_M_Completed.set(std::make_pair(_Result, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#endif - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - _Rhs._Then([_PParam](task<_ReturnType> _ResultTask) -> HRESULT { -#if _MSC_VER >= 1800 - // Dev10 compiler bug - typedef _ReturnType _ReturnTypeDev10; - auto _PParamCopy = _PParam; - auto _Func = [&_ResultTask, _PParamCopy]() { - auto _Result = _ResultTask._GetImpl()->_GetResult(); - - std::vector<_ReturnTypeDev10> _Vec; - _Vec.push_back(_Result); - _PParamCopy->_M_Completed.set(std::make_pair(_Vec, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#else - auto _Func = [&_ResultTask, _PParam]() { - _ReturnType _Result = _ResultTask._GetImpl()->_GetResult(); - - std::vector<_ReturnType> _Vec; - _Vec.push_back(_Result); - _PParam->_M_Completed.set(std::make_pair(_Vec, _ResultTask._GetImpl()->_M_pTokenState)); - }; -#endif - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; -#if _MSC_VER >= 1800 - }, Concurrency::details::_CancellationTokenState::_None()); -#else - }, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - return _ReturnTask; -} - -/// -/// Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes sucessfully when either of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>. If the input tasks are of type void the output task -/// will also be a task<void>. -/// To allow for a construct of the sort taskA || taskB && taskC, which are combined in pairs, with && taking precedence -/// over ||, the operator|| produces a task<std::vector<T>> if one of the tasks is of type task<std::vector<T>> -/// and the other one is of type task<T>. -/// -/// -/// If both of the tasks are canceled or throw exceptions, the returned task will complete in the canceled state, and one of the exceptions, -/// if any are encountered, will be thrown when you call get() or wait() on that task. -/// -/// -/**/ -template -task> operator||(const task<_ReturnType> & _Lhs, const task> & _Rhs) -{ - return _Rhs || _Lhs; -} - -/// -/// Creates a task that will complete successfully when any of the tasks supplied as arguments completes successfully. -/// -/// -/// The type of the returned task. -/// -/// -/// The first task to combine into the resulting task. -/// -/// -/// The second task to combine into the resulting task. -/// -/// -/// A task that completes sucessfully when either of the input tasks has completed successfully. If the input tasks are of type T, -/// the output of this function will be a task<std::vector<T>. If the input tasks are of type void the output task -/// will also be a task<void>. -/// To allow for a construct of the sort taskA || taskB && taskC, which are combined in pairs, with && taking precedence -/// over ||, the operator|| produces a task<std::vector<T>> if one of the tasks is of type task<std::vector<T>> -/// and the other one is of type task<T>. -/// -/// -/// If both of the tasks are canceled or throw exceptions, the returned task will complete in the canceled state, and one of the exceptions, -/// if any are encountered, will be thrown when you call get() or wait() on that task. -/// -/// -/**/ -inline task operator||(const task & _Lhs, const task & _Rhs) -{ - auto _PParam = new details::_RunAnyParam>(); - - task> _Any_task_completed(_PParam->_M_Completed, _PParam->_M_cancellationSource.get_token()); - // Chain the return continuation task here to ensure it will get inline execution when _M_Completed.set is called, - // So that _PParam can be used before it getting deleted. - auto _ReturnTask = _Any_task_completed._Then([=](std::pair _Ret) -> HRESULT { - _CONCRT_ASSERT(_Ret.second); - details::_JoinAllTokens_Add(_PParam->_M_cancellationSource, _Ret.second); - return S_OK; -#if _MSC_VER >= 1800 - }, nullptr); -#else - }, nullptr, false); -#endif - - if (_Lhs.is_apartment_aware() || _Rhs.is_apartment_aware()) - { - _ReturnTask._SetAsync(); - } - - _PParam->_M_numTasks = 2; - auto _Continuation = [_PParam](task _ResultTask) mutable -> HRESULT { - // Dev10 compiler needs this. - auto _PParam1 = _PParam; - auto _Func = [&_ResultTask, _PParam1]() { - _PParam1->_M_Completed.set(std::make_pair(details::_Unit_type(), _ResultTask._GetImpl()->_M_pTokenState)); - }; - _WhenAnyContinuationWrapper(_PParam, _Func, _ResultTask); - return S_OK; - }; - -#if _MSC_VER >= 1800 - _Lhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None()); - _Rhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None()); -#else - _Lhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None(), false); - _Rhs._Then(_Continuation, Concurrency::details::_CancellationTokenState::_None(), false); -#endif - - return _ReturnTask; -} - -#if _MSC_VER >= 1800 -template -task<_Ty> task_from_result(_Ty _Param, const task_options& _TaskOptions = task_options()) -{ - task_completion_event<_Ty> _Tce; - _Tce.set(_Param); - return create_task<_Ty>(_Tce, _TaskOptions); -} - -// Work around VS 2010 compiler bug -#if _MSC_VER == 1600 -inline task task_from_result(bool _Param) -{ - task_completion_event _Tce; - _Tce.set(_Param); - return create_task(_Tce, task_options()); -} -#endif -inline task task_from_result(const task_options& _TaskOptions = task_options()) -{ - task_completion_event _Tce; - _Tce.set(); - return create_task(_Tce, _TaskOptions); -} - -template -task<_TaskType> task_from_exception(_ExType _Exception, const task_options& _TaskOptions = task_options()) -{ - task_completion_event<_TaskType> _Tce; - _Tce.set_exception(_Exception); - return create_task<_TaskType>(_Tce, _TaskOptions); -} - -namespace details -{ - /// - /// A convenient extension to Concurrency: loop until a condition is no longer met - /// - /// - /// A function representing the body of the loop. It will be invoked at least once and - /// then repetitively as long as it returns true. - /// - inline - task do_while(std::function(void)> func) - { - task first = func(); - return first.then([=](bool guard, task* retVal) -> HRESULT { - if (guard) - *retVal = do_while(func); - else - *retVal = first; - return S_OK; - }); - } - -} // namespace details -#endif - -} // namespace Concurrency_winrt - -namespace concurrency_winrt = Concurrency_winrt; - -#pragma pop_macro("new") -#pragma warning(pop) -#pragma pack(pop) -#endif - -#endif diff --git a/modules/videoio/src/wrl.h b/modules/videoio/src/wrl.h new file mode 100644 index 000000000..3cb813fa3 --- /dev/null +++ b/modules/videoio/src/wrl.h @@ -0,0 +1,565 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#ifndef __cplusplus_winrt +#include + +__declspec(noreturn) void __stdcall __abi_WinRTraiseException(long); + +inline void __abi_ThrowIfFailed(long __hrArg) +{ + if (__hrArg < 0) + { + __abi_WinRTraiseException(__hrArg); + } +} + +struct Guid +{ +public: + Guid(); + Guid(__rcGUID_t); + operator ::__rcGUID_t(); + bool Equals(Guid __guidArg); + bool Equals(__rcGUID_t __guidArg); + Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, unsigned __int8 __dArg, + unsigned __int8 __eArg, unsigned __int8 __fArg, unsigned __int8 __gArg, unsigned __int8 __hArg, + unsigned __int8 __iArg, unsigned __int8 __jArg, unsigned __int8 __kArg); + Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, const unsigned __int8* __dArg); +private: + unsigned long __a; + unsigned short __b; + unsigned short __c; + unsigned char __d; + unsigned char __e; + unsigned char __f; + unsigned char __g; + unsigned char __h; + unsigned char __i; + unsigned char __j; + unsigned char __k; +}; + +static_assert(sizeof(Guid) == sizeof(::_GUID), "Incorect size for Guid"); +static_assert(sizeof(__rcGUID_t) == sizeof(::_GUID), "Incorect size for __rcGUID_t"); + +//////////////////////////////////////////////////////////////////////////////// +inline Guid::Guid() : __a(0), __b(0), __c(0), __d(0), __e(0), __f(0), __g(0), __h(0), __i(0), __j(0), __k(0) +{ +} + +inline Guid::Guid(__rcGUID_t __guid) : + __a(reinterpret_cast(__guid).Data1), + __b(reinterpret_cast(__guid).Data2), + __c(reinterpret_cast(__guid).Data3), + __d(reinterpret_cast(__guid).Data4[0]), + __e(reinterpret_cast(__guid).Data4[1]), + __f(reinterpret_cast(__guid).Data4[2]), + __g(reinterpret_cast(__guid).Data4[3]), + __h(reinterpret_cast(__guid).Data4[4]), + __i(reinterpret_cast(__guid).Data4[5]), + __j(reinterpret_cast(__guid).Data4[6]), + __k(reinterpret_cast(__guid).Data4[7]) +{ +} + +inline Guid::operator ::__rcGUID_t() +{ + return reinterpret_cast<__rcGUID_t>(*this); +} + +inline bool Guid::Equals(Guid __guidArg) +{ + return *this == __guidArg; +} + +inline bool Guid::Equals(__rcGUID_t __guidArg) +{ + return *this == static_cast< Guid>(__guidArg); +} + +inline bool operator==(Guid __aArg, Guid __bArg) +{ + auto __a = reinterpret_cast(&__aArg); + auto __b = reinterpret_cast(&__bArg); + + return (__a[0] == __b[0] && __a[1] == __b[1] && __a[2] == __b[2] && __a[3] == __b[3]); +} + +inline bool operator!=(Guid __aArg, Guid __bArg) +{ + return !(__aArg == __bArg); +} + +inline bool operator<(Guid __aArg, Guid __bArg) +{ + auto __a = reinterpret_cast(&__aArg); + auto __b = reinterpret_cast(&__bArg); + + if (__a[0] != __b[0]) + { + return __a[0] < __b[0]; + } + + if (__a[1] != __b[1]) + { + return __a[1] < __b[1]; + } + + if (__a[2] != __b[2]) + { + return __a[2] < __b[2]; + } + + if (__a[3] != __b[3]) + { + return __a[3] < __b[3]; + } + + return false; +} + +inline Guid::Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, unsigned __int8 __dArg, + unsigned __int8 __eArg, unsigned __int8 __fArg, unsigned __int8 __gArg, unsigned __int8 __hArg, + unsigned __int8 __iArg, unsigned __int8 __jArg, unsigned __int8 __kArg) : + __a(__aArg), __b(__bArg), __c(__cArg), __d(__dArg), __e(__eArg), __f(__fArg), __g(__gArg), __h(__hArg), __i(__iArg), __j(__jArg), __k(__kArg) +{ +} + +inline Guid::Guid(unsigned int __aArg, unsigned short __bArg, unsigned short __cArg, const unsigned __int8 __dArg[8]) : + __a(__aArg), __b(__bArg), __c(__cArg) +{ + __d = __dArg[0]; + __e = __dArg[1]; + __f = __dArg[2]; + __g = __dArg[3]; + __h = __dArg[4]; + __i = __dArg[5]; + __j = __dArg[6]; + __k = __dArg[7]; +} + +__declspec(selectany) Guid __winrt_GUID_NULL(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); + +// +//// Don't want to define the real IUnknown from unknown.h here. That would means if the user has +//// any broken code that uses it, compile errors will take the form of e.g.: +//// predefined C++ WinRT types (compiler internal)(41) : see declaration of 'IUnknown::QueryInterface' +//// This is not helpful. If they use IUnknown, we still need to point them to the actual unknown.h so +//// that they can see the original definition. +//// +//// For WinRT, we'll instead have a parallel COM interface hierarchy for basic interfaces starting with _. +//// The type mismatch is not an issue. COM passes types through GUID / void* combos - the original type +//// doesn't come into play unless the user static_casts an implementation type to one of these, but +//// the WinRT implementation types are hidden. +__interface __declspec(uuid("00000000-0000-0000-C000-000000000046")) __abi_IUnknown +{ +public: + virtual long __stdcall __abi_QueryInterface(Guid&, void**) = 0; + virtual unsigned long __stdcall __abi_AddRef() = 0; + virtual unsigned long __stdcall __abi_Release() = 0; +}; + +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseNotImplementedException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseInvalidCastException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseNullReferenceException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseOperationCanceledException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseFailureException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseAccessDeniedException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseOutOfMemoryException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseInvalidArgumentException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseOutOfBoundsException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseChangedStateException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseClassNotRegisteredException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseWrongThreadException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseDisconnectedException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseObjectDisposedException(); +__declspec(dllexport) __declspec(noreturn) void __stdcall __abi_WinRTraiseCOMException(long); + +__declspec(noreturn) inline void __stdcall __abi_WinRTraiseException(long __hrArg) +{ + switch (__hrArg) + { + case 0x80004001L: // E_NOTIMPL + __abi_WinRTraiseNotImplementedException(); + + case 0x80004002L: // E_NOINTERFACE + __abi_WinRTraiseInvalidCastException(); + + case 0x80004003L: // E_POINTER + __abi_WinRTraiseNullReferenceException(); + + case 0x80004004L: // E_ABORT + __abi_WinRTraiseOperationCanceledException(); + + case 0x80004005L: // E_FAIL + __abi_WinRTraiseFailureException(); + + case 0x80070005L: // E_ACCESSDENIED + __abi_WinRTraiseAccessDeniedException(); + + case 0x8007000EL: // E_OUTOFMEMORY + __abi_WinRTraiseOutOfMemoryException(); + + case 0x80070057L: // E_INVALIDARG + __abi_WinRTraiseInvalidArgumentException(); + + case 0x8000000BL: // E_BOUNDS + __abi_WinRTraiseOutOfBoundsException(); + + case 0x8000000CL: // E_CHANGED_STATE + __abi_WinRTraiseChangedStateException(); + + case 0x80040154L: // REGDB_E_CLASSNOTREG + __abi_WinRTraiseClassNotRegisteredException(); + + case 0x8001010EL: // RPC_E_WRONG_THREAD + __abi_WinRTraiseWrongThreadException(); + + case 0x80010108L: // RPC_E_DISCONNECTED + __abi_WinRTraiseDisconnectedException(); + + case 0x80000013L: // RO_E_CLOSED + __abi_WinRTraiseObjectDisposedException(); + + default: + __abi_WinRTraiseCOMException(__hrArg); + break; + } +} + +struct __abi_CaptureBase +{ +protected: + virtual __stdcall ~__abi_CaptureBase() {} + +public: + static const size_t __smallCaptureSize = 4 * sizeof(void*); + void* operator new(size_t __sizeArg, void* __pSmallCaptureArg) + { + if (__sizeArg > __smallCaptureSize) + { + return reinterpret_cast<__abi_CaptureBase*>(HeapAlloc(GetProcessHeap(), 0, __sizeArg)); + } + + return __pSmallCaptureArg; + } + + void operator delete(void* __ptrArg, void* __pSmallCaptureArg) + { + __abi_CaptureBase* __pThis = static_cast<__abi_CaptureBase*>(__ptrArg); + __pThis->Delete(__pThis, __pSmallCaptureArg); + } + + inline void* GetVFunction(int __slotArg) + { + return (*reinterpret_cast(this))[__slotArg]; + } + + void Delete(__abi_CaptureBase* __pThisArg, void* __pSmallCaptureArg) + { + __pThisArg->~__abi_CaptureBase(); + if (__pThisArg != __pSmallCaptureArg) + { + HeapFree(GetProcessHeap(), 0, __pThisArg); + } + } +}; + +struct __abi_CapturePtr +{ + char* smallCapture[__abi_CaptureBase::__smallCaptureSize]; + __abi_CaptureBase* ptr; + __abi_CapturePtr() : ptr(reinterpret_cast<__abi_CaptureBase*>(smallCapture)) {} + ~__abi_CapturePtr() + { + ptr->Delete(ptr, smallCapture); + } +}; + +template +struct __abi_FunctorCapture0 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture0(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke() { return functor(); } +}; +template +struct __abi_FunctorCapture1 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture1(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0) { return functor(__arg0); } +}; +template +struct __abi_FunctorCapture2 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture2(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1) { return functor(__arg0, __arg1); } +}; +template +struct __abi_FunctorCapture3 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture3(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2) { return functor(__arg0, __arg1, __arg2); } +}; +template +struct __abi_FunctorCapture4 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture4(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3) { return functor(__arg0, __arg1, __arg2, __arg3); } +}; +template +struct __abi_FunctorCapture5 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture5(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4); } +}; +template +struct __abi_FunctorCapture6 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture6(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4, __TArg5 __arg5) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4, __arg5); } +}; +template +struct __abi_FunctorCapture7 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture7(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4, __TArg5 __arg5, __TArg6 __arg6) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6); } +}; +template +struct __abi_FunctorCapture8 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture8(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4, __TArg5 __arg5, __TArg6 __arg6, __TArg7 __arg7) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7); } +}; +template +struct __abi_FunctorCapture9 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture9(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4, __TArg5 __arg5, __TArg6 __arg6, __TArg7 __arg7, __TArg8 __arg8) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8); } +}; +template +struct __abi_FunctorCapture10 : public __abi_CaptureBase +{ + __TFunctor functor; + __abi_FunctorCapture10(__TFunctor __functor) : functor(__functor) {} + virtual __TReturnType __stdcall Invoke(__TArg0 __arg0, __TArg1 __arg1, __TArg2 __arg2, __TArg3 __arg3, __TArg4 __arg4, __TArg5 __arg5, __TArg6 __arg6, __TArg7 __arg7, __TArg8 __arg8, __TArg9 __arg9) { return functor(__arg0, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8, __arg9); } +}; + +#define __is_winrt_array(type) (type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt8Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int16Array ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt16Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int32Array ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt32Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_Int64Array ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_UInt64Array || type == ABI::Windows::Foundation::PropertyType::PropertyType_SingleArray ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_DoubleArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_Char16Array ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_BooleanArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_StringArray ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_InspectableArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_DateTimeArray ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_TimeSpanArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_GuidArray ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_PointArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_SizeArray ||\ + type == ABI::Windows::Foundation::PropertyType::PropertyType_RectArray || type == ABI::Windows::Foundation::PropertyType::PropertyType_OtherTypeArray) + +template::value> +struct winrt_type +{ +}; +template +struct winrt_type<_Type, true> +{ + static IUnknown* create(_Type* _ObjInCtx) { + return reinterpret_cast(_ObjInCtx); + } + static IID getuuid() { return __uuidof(_Type); } + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherType; +}; +template +struct winrt_type<_Type, false> +{ + static IUnknown* create(_Type* _ObjInCtx) { + Microsoft::WRL::ComPtr _PObj; + Microsoft::WRL::ComPtr objFactory; + HRESULT hr = Windows::Foundation::GetActivationFactory(Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), objFactory.ReleaseAndGetAddressOf()); + if (FAILED(hr)) return nullptr; + Microsoft::WRL::ComPtr spPropVal; + if (SUCCEEDED(hr)) + hr = objFactory.As(&spPropVal); + if (SUCCEEDED(hr)) { + hr = winrt_type<_Type>::create(spPropVal.Get(), _ObjInCtx, _PObj.GetAddressOf()); + if (SUCCEEDED(hr)) + return reinterpret_cast(_PObj.Detach()); + } + return nullptr; + } + static IID getuuid() { return __uuidof(ABI::Windows::Foundation::IPropertyValue); } + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherType; +}; + +template<> +struct winrt_type +{ + static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, void* _ObjInCtx, IInspectable** ppInsp) { + (void)_ObjInCtx; + return spPropVal->CreateEmpty(ppInsp); + } + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_Empty; +}; +#define MAKE_TYPE(Type, Name) template<>\ +struct winrt_type\ +{\ + static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, Type* _ObjInCtx, IInspectable** ppInsp) {\ + return spPropVal->Create##Name(*_ObjInCtx, ppInsp);\ +}\ + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_##Name;\ +}; + +template +struct winrt_array_type +{ + static IUnknown* create(_Type* _ObjInCtx, size_t N) { + Microsoft::WRL::ComPtr _PObj; + Microsoft::WRL::ComPtr objFactory; + HRESULT hr = Windows::Foundation::GetActivationFactory(Microsoft::WRL::Wrappers::HStringReference(RuntimeClass_Windows_Foundation_PropertyValue).Get(), objFactory.ReleaseAndGetAddressOf()); + if (FAILED(hr)) return nullptr; + Microsoft::WRL::ComPtr spPropVal; + if (SUCCEEDED(hr)) + hr = objFactory.As(&spPropVal); + if (SUCCEEDED(hr)) { + hr = winrt_array_type<_Type>::create(spPropVal.Get(), N, _ObjInCtx, _PObj.GetAddressOf()); + if (SUCCEEDED(hr)) + return reinterpret_cast(_PObj.Detach()); + } + return nullptr; + } + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_OtherTypeArray; +}; +template +struct winrt_prop_type {}; + +template <> +struct winrt_prop_type { + typedef void _Type; +}; + +template <> +struct winrt_prop_type { + typedef void _Type; +}; + +template <> +struct winrt_prop_type { + typedef void _Type; +}; + +#define MAKE_PROP(Prop, Type) template <>\ +struct winrt_prop_type {\ + typedef Type _Type;\ +}; + +#define MAKE_ARRAY_TYPE(Type, Name) MAKE_PROP(Name, Type)\ + MAKE_PROP(Name##Array, Type*)\ + MAKE_TYPE(Type, Name)\ + template<>\ +struct winrt_array_type\ +{\ + static HRESULT create(ABI::Windows::Foundation::IPropertyValueStatics* spPropVal, UINT32 __valueSize, Type** _ObjInCtx, IInspectable** ppInsp) {\ + return spPropVal->Create##Name##Array(__valueSize, *_ObjInCtx, ppInsp);\ +}\ + static const ABI::Windows::Foundation::PropertyType _PropType = ABI::Windows::Foundation::PropertyType::PropertyType_##Name##Array;\ + static std::vector PropertyValueToVector(ABI::Windows::Foundation::IPropertyValue* propValue)\ +{\ + UINT32 uLen = 0;\ + Type* pArray = nullptr;\ + propValue->Get##Name##Array(&uLen, &pArray);\ + return std::vector(pArray, pArray + uLen);\ +}\ +}; +MAKE_ARRAY_TYPE(BYTE, UInt8) +MAKE_ARRAY_TYPE(INT16, Int16) +MAKE_ARRAY_TYPE(UINT16, UInt16) +MAKE_ARRAY_TYPE(INT32, Int32) +MAKE_ARRAY_TYPE(UINT32, UInt32) +MAKE_ARRAY_TYPE(INT64, Int64) +MAKE_ARRAY_TYPE(UINT64, UInt64) +MAKE_ARRAY_TYPE(FLOAT, Single) +MAKE_ARRAY_TYPE(DOUBLE, Double) +MAKE_ARRAY_TYPE(WCHAR, Char16) +//MAKE_ARRAY_TYPE(boolean, Boolean) //conflict with identical type in C++ of BYTE/UInt8 +MAKE_ARRAY_TYPE(HSTRING, String) +MAKE_ARRAY_TYPE(IInspectable*, Inspectable) +MAKE_ARRAY_TYPE(GUID, Guid) +MAKE_ARRAY_TYPE(ABI::Windows::Foundation::DateTime, DateTime) +MAKE_ARRAY_TYPE(ABI::Windows::Foundation::TimeSpan, TimeSpan) +MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Point, Point) +MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Size, Size) +MAKE_ARRAY_TYPE(ABI::Windows::Foundation::Rect, Rect) + +template < typename T > +struct DerefHelper +{ + typedef T DerefType; +}; + +template < typename T > +struct DerefHelper +{ + typedef T DerefType; +}; + +#define __is_valid_winrt_type(_Type) (std::is_void<_Type>::value || \ + std::is_same<_Type, BYTE>::value || \ + std::is_same<_Type, INT16>::value || \ + std::is_same<_Type, UINT16>::value || \ + std::is_same<_Type, INT32>::value || \ + std::is_same<_Type, UINT32>::value || \ + std::is_same<_Type, INT64>::value || \ + std::is_same<_Type, UINT64>::value || \ + std::is_same<_Type, FLOAT>::value || \ + std::is_same<_Type, DOUBLE>::value || \ + std::is_same<_Type, WCHAR>::value || \ + std::is_same<_Type, boolean>::value || \ + std::is_same<_Type, HSTRING>::value || \ + std::is_same<_Type, IInspectable *>::value || \ + std::is_base_of::value || \ + std::is_base_of::DerefType>::value || \ + std::is_same<_Type, GUID>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::DateTime>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::TimeSpan>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Point>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Size>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Rect>::value || \ + std::is_same<_Type, BYTE*>::value || \ + std::is_same<_Type, INT16*>::value || \ + std::is_same<_Type, UINT16*>::value || \ + std::is_same<_Type, INT32*>::value || \ + std::is_same<_Type, UINT32*>::value || \ + std::is_same<_Type, INT64*>::value || \ + std::is_same<_Type, UINT64*>::value || \ + std::is_same<_Type, FLOAT*>::value || \ + std::is_same<_Type, DOUBLE*>::value || \ + std::is_same<_Type, WCHAR*>::value || \ + std::is_same<_Type, boolean*>::value || \ + std::is_same<_Type, HSTRING*>::value || \ + std::is_same<_Type, IInspectable **>::value || \ + std::is_same<_Type, GUID*>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::DateTime*>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::TimeSpan*>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Point*>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Size*>::value || \ + std::is_same<_Type, ABI::Windows::Foundation::Rect*>::value) +#endif diff --git a/modules/videoio/test/test_ffmpeg.cpp b/modules/videoio/test/test_ffmpeg.cpp index 3ccce8194..423817715 100644 --- a/modules/videoio/test/test_ffmpeg.cpp +++ b/modules/videoio/test/test_ffmpeg.cpp @@ -431,6 +431,10 @@ TEST(Videoio_Video_parallel_writers_and_readers, accuracy) if (code == 1) std::cerr << "Couldn't delete " << *i << std::endl; } + + // delete the readers + for (std::vector::iterator i = readers.begin(), end = readers.end(); i != end; ++i) + delete *i; } #endif diff --git a/modules/videostab/CMakeLists.txt b/modules/videostab/CMakeLists.txt index 49e75f9ad..c31fcfceb 100644 --- a/modules/videostab/CMakeLists.txt +++ b/modules/videostab/CMakeLists.txt @@ -4,7 +4,7 @@ if(HAVE_CUDA) ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wmissing-declarations -Wshadow -Wunused-parameter) endif() -if(WINRT_8_1) +if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") endif() diff --git a/modules/videostab/src/motion_stabilizing.cpp b/modules/videostab/src/motion_stabilizing.cpp index 65bbd73bd..d31c420f6 100644 --- a/modules/videostab/src/motion_stabilizing.cpp +++ b/modules/videostab/src/motion_stabilizing.cpp @@ -602,13 +602,12 @@ static inline bool isGoodMotion(const float M[], float w, float h, float dx, flo { Point2f pt[4] = {Point2f(0,0), Point2f(w,0), Point2f(w,h), Point2f(0,h)}; Point2f Mpt[4]; - float z; for (int i = 0; i < 4; ++i) { Mpt[i].x = M[0]*pt[i].x + M[1]*pt[i].y + M[2]; Mpt[i].y = M[3]*pt[i].x + M[4]*pt[i].y + M[5]; - z = M[6]*pt[i].x + M[7]*pt[i].y + M[8]; + float z = M[6]*pt[i].x + M[7]*pt[i].y + M[8]; Mpt[i].x /= z; Mpt[i].y /= z; } diff --git a/modules/videostab/src/outlier_rejection.cpp b/modules/videostab/src/outlier_rejection.cpp index ee37a9303..3ae6e6b27 100644 --- a/modules/videostab/src/outlier_rejection.cpp +++ b/modules/videostab/src/outlier_rejection.cpp @@ -84,16 +84,14 @@ void TranslationBasedLocalOutlierRejector::process( Size ncells((frameSize.width + cellSize_.width - 1) / cellSize_.width, (frameSize.height + cellSize_.height - 1) / cellSize_.height); - int cx, cy; - // fill grid cells grid_.assign(ncells.area(), Cell()); for (int i = 0; i < npoints; ++i) { - cx = std::min(cvRound(points0_[i].x / cellSize_.width), ncells.width - 1); - cy = std::min(cvRound(points0_[i].y / cellSize_.height), ncells.height - 1); + int cx = std::min(cvRound(points0_[i].x / cellSize_.width), ncells.width - 1); + int cy = std::min(cvRound(points0_[i].y / cellSize_.height), ncells.height - 1); grid_[cy * ncells.width + cx].push_back(i); } @@ -101,19 +99,16 @@ void TranslationBasedLocalOutlierRejector::process( RNG rng(0); int niters = ransacParams_.niters(); - int ninliers, ninliersMax; std::vector inliers; - float dx, dy, dxBest, dyBest; - float x1, y1; - int idx; for (size_t ci = 0; ci < grid_.size(); ++ci) { // estimate translation model at the current cell using RANSAC + float x1, y1; const Cell &cell = grid_[ci]; - ninliersMax = 0; - dxBest = dyBest = 0.f; + int ninliers, ninliersMax = 0; + float dxBest = 0.f, dyBest = 0.f; // find the best hypothesis @@ -121,9 +116,9 @@ void TranslationBasedLocalOutlierRejector::process( { for (int iter = 0; iter < niters; ++iter) { - idx = cell[static_cast(rng) % cell.size()]; - dx = points1_[idx].x - points0_[idx].x; - dy = points1_[idx].y - points0_[idx].y; + int idx = cell[static_cast(rng) % cell.size()]; + float dx = points1_[idx].x - points0_[idx].x; + float dy = points1_[idx].y - points0_[idx].y; ninliers = 0; for (size_t i = 0; i < cell.size(); ++i) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index fde4fc2c7..47b655424 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -67,7 +67,8 @@ namespace cv REPRESENTATION, IMMEDIATE_RENDERING, SHADING, - AMBIENT + AMBIENT, + LIGHTING }; enum RepresentationValues diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index 698f21c9a..3423ba8de 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -115,6 +115,14 @@ void cv::viz::Widget::setRenderingProperty(int property, double value) case LINE_WIDTH: actor->GetProperty()->SetLineWidth(float(value)); break; case IMMEDIATE_RENDERING: actor->GetMapper()->SetImmediateModeRendering(int(value)); break; case AMBIENT: actor->GetProperty()->SetAmbient(float(value)); break; + case LIGHTING: + { + if (value == 0) + actor->GetProperty()->LightingOff(); + else + actor->GetProperty()->LightingOn(); + break; + } case FONT_SIZE: { vtkTextActor* text_actor = vtkTextActor::SafeDownCast(actor); diff --git a/modules/world/CMakeLists.txt b/modules/world/CMakeLists.txt index 6377853e2..db8928d69 100644 --- a/modules/world/CMakeLists.txt +++ b/modules/world/CMakeLists.txt @@ -2,7 +2,7 @@ set(the_description "All the selected OpenCV modules in a single binary") set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE) set(BUILD_opencv_world_INIT OFF) -if(IOS OR NOT BUILD_SHARED_LIBS) +if(APPLE_FRAMEWORK OR NOT BUILD_SHARED_LIBS) set(OPENCV_MODULE_TYPE STATIC) set(OPENCV_WORLD_FLAGS_PROPERTY STATIC_LIBRARY_FLAGS) else() @@ -14,19 +14,20 @@ function(include_one_module m) endfunction() if(NOT OPENCV_INITIAL_PASS) + set(ENABLE_PRECOMPILED_HEADERS OFF CACHE INTERNAL "" FORCE) project(opencv_world) message(STATUS "Processing WORLD modules...") foreach(m ${OPENCV_MODULES_BUILD}) if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD) message(STATUS " module ${m}...") - set(CMAKE_CURRENT_SOURCE_DIR ${OPENCV_MODULE_${m}_LOCATION}) + set(CMAKE_CURRENT_SOURCE_DIR "${OPENCV_MODULE_${m}_LOCATION}") #add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" ${CMAKE_CURRENT_BINARY_DIR}/${m}) include_one_module(${m}) endif() endforeach() message(STATUS "Processing WORLD modules... DONE") - set(CMAKE_CURRENT_SOURCE_DIR OPENCV_MODULE_${opencv_world}_LOCATION) + set(CMAKE_CURRENT_SOURCE_DIR "${OPENCV_MODULE_opencv_world_LOCATION}") endif() ocv_add_module(world opencv_core) @@ -59,26 +60,3 @@ endif() if(BUILD_opencv_highgui AND OPENCV_MODULE_opencv_highgui_IS_PART_OF_WORLD) ocv_highgui_configure_target() endif() - -if(IOS OR APPLE) - set(merge_libs "") - macro(ios_include_3party_libs) - foreach(l ${ARGN}) - add_dependencies(${the_module} ${l}) - list(APPEND merge_libs "$") - endforeach() - endmacro() - - if(WITH_PNG) - ios_include_3party_libs(zlib libpng) - endif() - - if(WITH_JPEG) - ios_include_3party_libs(libjpeg) - endif() - - add_custom_command(TARGET ${the_module} POST_BUILD - COMMAND /usr/bin/libtool -static -o ${CMAKE_CURRENT_BINARY_DIR}/${the_module}_fat.a $ ${merge_libs} - COMMAND mv ${CMAKE_CURRENT_BINARY_DIR}/${the_module}_fat.a $ - ) -endif() diff --git a/platforms/android/android.toolchain.cmake b/platforms/android/android.toolchain.cmake index 6dca10d7f..900ca8c91 100644 --- a/platforms/android/android.toolchain.cmake +++ b/platforms/android/android.toolchain.cmake @@ -155,6 +155,10 @@ # Implies -frtti -fno-exceptions. # Available for NDK r7b and newer. # Silently degrades to gnustl_static if not available. +# c++_static -> Use the LLVM libc++ runtime as a static library. +# Implies -frtti -fexceptions. +# c++_shared -> Use the LLVM libc++ runtime as a static library. +# Implies -frtti -fno-exceptions. # # ANDROID_STL_FORCE_FEATURES=ON - turn rtti and exceptions support based on # chosen runtime. If disabled, then the user is responsible for settings @@ -842,7 +846,7 @@ set( ANDROID_STL_FORCE_FEATURES ON CACHE BOOL "automatically configure rtti and mark_as_advanced( ANDROID_STL ANDROID_STL_FORCE_FEATURES ) if( BUILD_WITH_ANDROID_NDK ) - if( NOT "${ANDROID_STL}" MATCHES "^(none|system|system_re|gabi\\+\\+_static|gabi\\+\\+_shared|stlport_static|stlport_shared|gnustl_static|gnustl_shared)$") + if( NOT "${ANDROID_STL}" MATCHES "^(none|system|system_re|gabi\\+\\+_static|gabi\\+\\+_shared|stlport_static|stlport_shared|gnustl_static|gnustl_shared|c\\+\\+_static|c\\+\\+_shared)$") message( FATAL_ERROR "ANDROID_STL is set to invalid value \"${ANDROID_STL}\". The possible values are: none -> Do not configure the runtime. @@ -854,15 +858,19 @@ The possible values are: stlport_shared -> Use the STLport runtime as a shared library. gnustl_static -> (default) Use the GNU STL as a static library. gnustl_shared -> Use the GNU STL as a shared library. + c++_shared -> Use the LLVM libc++ runtime as a shared library. + c++_static -> Use the LLVM libc++ runtime as a static library. " ) endif() elseif( BUILD_WITH_STANDALONE_TOOLCHAIN ) - if( NOT "${ANDROID_STL}" MATCHES "^(none|gnustl_static|gnustl_shared)$") + if( NOT "${ANDROID_STL}" MATCHES "^(none|gnustl_static|gnustl_shared|c\\+\\+_static|c\\+\\+_shared)$") message( FATAL_ERROR "ANDROID_STL is set to invalid value \"${ANDROID_STL}\". The possible values are: none -> Do not configure the runtime. gnustl_static -> (default) Use the GNU STL as a static library. gnustl_shared -> Use the GNU STL as a shared library. + c++_shared -> Use the LLVM libc++ runtime as a shared library. + c++_static -> Use the LLVM libc++ runtime as a static library. " ) endif() endif() @@ -1035,9 +1043,17 @@ if( BUILD_WITH_ANDROID_NDK ) else() set( __libstl "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/libstdc++.a" ) endif() + elseif( ANDROID_STL MATCHES "c\\+\\+" ) + set( ANDROID_EXCEPTIONS ON ) + set( ANDROID_RTTI ON ) + set( __libstl "${ANDROID_NDK}/sources/cxx-stl/llvm-libc++" ) + set( __libstl "${__libstl}/libs/${ANDROID_NDK_ABI_NAME}/libc++_static.a" ) + set( __libgnustl "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${ANDROID_COMPILER_VERSION}" ) + set( ANDROID_STL_INCLUDE_DIRS "${__libgnustl}/include" "${__libgnustl}/libs/${ANDROID_NDK_ABI_NAME}/include" "${__libgnustl}/include/backward" ) else() message( FATAL_ERROR "Unknown runtime: ${ANDROID_STL}" ) endif() + # find libsupc++.a - rtti & exceptions if( ANDROID_STL STREQUAL "system_re" OR ANDROID_STL MATCHES "gnustl" ) set( __libsupcxx "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/${ANDROID_COMPILER_VERSION}/libs/${ANDROID_NDK_ABI_NAME}/libsupc++.a" ) # r8b or newer @@ -1067,7 +1083,9 @@ endif() # case of shared STL linkage if( ANDROID_STL MATCHES "shared" AND DEFINED __libstl ) string( REPLACE "_static.a" "_shared.so" __libstl "${__libstl}" ) - # TODO: check if .so file exists before the renaming + if( NOT EXISTS "${__libstl}" ) + message( FATAL_ERROR "Unable to find shared library ${__libstl}" ) + endif() endif() diff --git a/platforms/android/build_sdk.py b/platforms/android/build_sdk.py index 274c02d9f..f262a4546 100755 --- a/platforms/android/build_sdk.py +++ b/platforms/android/build_sdk.py @@ -57,7 +57,7 @@ def determine_opencv_version(version_hpp_path): major = re.search(r'^#define\W+CV_VERSION_MAJOR\W+(\d+)$', data, re.MULTILINE).group(1) minor = re.search(r'^#define\W+CV_VERSION_MINOR\W+(\d+)$', data, re.MULTILINE).group(1) revision = re.search(r'^#define\W+CV_VERSION_REVISION\W+(\d+)$', data, re.MULTILINE).group(1) - version_status = re.search(r'^#define\W+CV_VERSION_STATUS\W+"([^"]*)"', data, re.MULTILINE).group(1) + version_status = re.search(r'^#define\W+CV_VERSION_STATUS\W+"([^"]*)"$', data, re.MULTILINE).group(1) return "%(major)s.%(minor)s.%(revision)s%(version_status)s" % locals() #=================================================================================================== @@ -166,7 +166,7 @@ class Builder: # Add extra data apkxmldest = check_dir(os.path.join(apkdest, "res", "xml"), create=True) apklibdest = check_dir(os.path.join(apkdest, "libs", abi.name), create=True) - for ver, d in self.extra_packs + [("3.0.0", os.path.join(self.libdest, "lib"))]: + for ver, d in self.extra_packs + [("3.1.0", os.path.join(self.libdest, "lib"))]: r = ET.Element("library", attrib={"version": ver}) log.info("Adding libraries from %s", d) diff --git a/platforms/android/service/engine/AndroidManifest.xml b/platforms/android/service/engine/AndroidManifest.xml index 992f5f048..ef1cab2a0 100644 --- a/platforms/android/service/engine/AndroidManifest.xml +++ b/platforms/android/service/engine/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="310@ANDROID_PLATFORM_ID@" + android:versionName="3.10"> diff --git a/platforms/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java b/platforms/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java index eccc36c9d..917a64111 100644 --- a/platforms/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java +++ b/platforms/android/service/engine/src/org/opencv/engine/OpenCVEngineService.java @@ -134,7 +134,7 @@ public class OpenCVEngineService extends Service { @Override public int getEngineVersion() throws RemoteException { - int version = 3000; + int version = 3100; try { version = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { diff --git a/platforms/android/service/readme.txt b/platforms/android/service/readme.txt index 5bd773b0e..82cac6255 100644 --- a/platforms/android/service/readme.txt +++ b/platforms/android/service/readme.txt @@ -10,14 +10,14 @@ from Google Play. If Google Play is not available (i.e. on emulator, developer board, etc), you can install it manually using adb tool: - adb install /apk/OpenCV_3.0.0_Manager_3.00_.apk + adb install /apk/OpenCV_3.1.0_Manager_3.10_.apk Use the list below to determine proper OpenCV Manager package for your device: -- OpenCV_3.0.0-dev_Manager_3.00_armeabi.apk - armeabi (ARMv5, ARMv6) -- OpenCV_3.0.0-dev_Manager_3.00_armeabi-v7a.apk - armeabi-v7a (ARMv7-A + NEON) -- OpenCV_3.0.0-dev_Manager_3.00_arm64-v8a.apk - arm64-v8a (ARM64-v8a) -- OpenCV_3.0.0-dev_Manager_3.00_mips.apk - mips (MIPS) -- OpenCV_3.0.0-dev_Manager_3.00_mips64.apk - mips64 (MIPS64) -- OpenCV_3.0.0-dev_Manager_3.00_x86.apk - x86 -- OpenCV_3.0.0-dev_Manager_3.00_x86_64.apk - x86_64 +- OpenCV_3.1.0-dev_Manager_3.10_armeabi.apk - armeabi (ARMv5, ARMv6) +- OpenCV_3.1.0-dev_Manager_3.10_armeabi-v7a.apk - armeabi-v7a (ARMv7-A + NEON) +- OpenCV_3.1.0-dev_Manager_3.10_arm64-v8a.apk - arm64-v8a (ARM64-v8a) +- OpenCV_3.1.0-dev_Manager_3.10_mips.apk - mips (MIPS) +- OpenCV_3.1.0-dev_Manager_3.10_mips64.apk - mips64 (MIPS64) +- OpenCV_3.1.0-dev_Manager_3.10_x86.apk - x86 +- OpenCV_3.1.0-dev_Manager_3.10_x86_64.apk - x86_64 diff --git a/platforms/ios/build_framework.py b/platforms/ios/build_framework.py old mode 100755 new mode 100644 index 0455ae3df..34500d7f1 --- a/platforms/ios/build_framework.py +++ b/platforms/ios/build_framework.py @@ -43,7 +43,7 @@ def getXCodeMajor(): return 0 class Builder: - def __init__(self, opencv, contrib): + def __init__(self, opencv, contrib, targets): self.opencv = os.path.abspath(opencv) self.contrib = None if contrib: @@ -52,13 +52,7 @@ class Builder: self.contrib = os.path.abspath(modpath) else: print("Note: contrib repository is bad - modules subfolder not found", file=sys.stderr) - self.targets = [ - ("armv7", "iPhoneOS"), - ("armv7s", "iPhoneOS"), - ("arm64", "iPhoneOS"), - ("i386", "iPhoneSimulator"), - ("x86_64", "iPhoneSimulator") - ] + self.targets = targets def getBD(self, parent, t): res = os.path.join(parent, '%s-%s' % t) @@ -66,7 +60,7 @@ class Builder: os.makedirs(res) return os.path.abspath(res) - def build(self, outdir): + def _build(self, outdir): outdir = os.path.abspath(outdir) if not os.path.isdir(outdir): os.makedirs(outdir) @@ -81,36 +75,38 @@ class Builder: cmake_flags = [] if self.contrib: cmake_flags.append("-DOPENCV_EXTRA_MODULES_PATH=%s" % self.contrib) - if xcode_ver >= 7 and not "Simulator" in t[1]: + if xcode_ver >= 7 and t[1] == 'iPhoneOS': cmake_flags.append("-DCMAKE_C_FLAGS=-fembed-bitcode") cmake_flags.append("-DCMAKE_CXX_FLAGS=-fembed-bitcode") self.buildOne(t[0], t[1], mainBD, cmake_flags) self.mergeLibs(mainBD) self.makeFramework(outdir, dirs) - def buildOne(self, arch, target, builddir, cmakeargs = []): - # Run cmake + def build(self, outdir): + try: + self._build(outdir) + except Exception as e: + print("="*60, file=sys.stderr) + print("ERROR: %s" % e, file=sys.stderr) + print("="*60, file=sys.stderr) + traceback.print_exc(file=sys.stderr) + sys.exit(1) + + def getToolchain(self, arch, target): toolchain = os.path.join(self.opencv, "platforms", "ios", "cmake", "Toolchains", "Toolchain-%s_Xcode.cmake" % target) - cmakecmd = [ + return toolchain + + def getCMakeArgs(self, arch, target): + args = [ "cmake", "-GXcode", - "-DCMAKE_BUILD_TYPE=Release", - "-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain, + "-DAPPLE_FRAMEWORK=ON", "-DCMAKE_INSTALL_PREFIX=install", + "-DCMAKE_BUILD_TYPE=Release", ] - if arch.startswith("armv"): - cmakecmd.append("-DENABLE_NEON=ON") - cmakecmd.append(self.opencv) - cmakecmd.extend(cmakeargs) - execute(cmakecmd, cwd = builddir) - # Clean and build - cleanlist = [] - cleanlist.extend(glob.glob(os.path.join(builddir, "lib", "Release", "*.a"))) - cleanlist.extend(glob.glob(os.path.join(builddir, "modules", "*", "UninstalledProducts", "*.a"))) - print("Cleaning files:\n\t%s" % "\n\t".join(cleanlist), file=sys.stderr) - for f in cleanlist: - if os.path.isfile(f): - os.remove(f) + return args + + def getBuildCommand(self, arch, target): buildcmd = [ "xcodebuild", "IPHONEOS_DEPLOYMENT_TARGET=6.0", @@ -118,15 +114,35 @@ class Builder: "-sdk", target.lower(), "-configuration", "Release", "-parallelizeTargets", - "-jobs", "8", + "-jobs", "4" ] + return buildcmd + + def getInfoPlist(self, builddirs): + return os.path.join(builddirs[0], "ios", "Info.plist") + + def buildOne(self, arch, target, builddir, cmakeargs = []): + # Run cmake + toolchain = self.getToolchain(arch, target) + cmakecmd = self.getCMakeArgs(arch, target) + \ + (["-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain] if toolchain is not None else []) + if arch.startswith("armv") or arch.startswith("arm64"): + cmakecmd.append("-DENABLE_NEON=ON") + cmakecmd.append(self.opencv) + cmakecmd.extend(cmakeargs) + execute(cmakecmd, cwd = builddir) + # Clean and build + clean_dir = os.path.join(builddir, "install") + if os.path.isdir(clean_dir): + shutil.rmtree(clean_dir) + buildcmd = self.getBuildCommand(arch, target) execute(buildcmd + ["-target", "ALL_BUILD", "build"], cwd = builddir) - execute(buildcmd + ["-target", "install", "install"], cwd = builddir) + execute(["cmake", "-P", "cmake_install.cmake"], cwd = builddir) def mergeLibs(self, builddir): res = os.path.join(builddir, "lib", "Release", "libopencv_merged.a") - libs = glob.glob(os.path.join(builddir, "lib", "Release", "*.a")) - libs3 = glob.glob(os.path.join(builddir, "3rdparty", "lib", "Release", "*.a")) + libs = glob.glob(os.path.join(builddir, "install", "lib", "*.a")) + libs3 = glob.glob(os.path.join(builddir, "install", "share", "OpenCV", "3rdparty", "lib", "*.a")) print("Merging libraries:\n\t%s" % "\n\t".join(libs + libs3), file=sys.stderr) execute(["libtool", "-static", "-o", res] + libs + libs3) @@ -156,7 +172,7 @@ class Builder: # copy Info.plist resdir = os.path.join(dstdir, "Resources") os.makedirs(resdir) - shutil.copyfile(os.path.join(builddirs[0], "ios", "Info.plist"), os.path.join(resdir, "Info.plist")) + shutil.copyfile(self.getInfoPlist(builddirs), os.path.join(resdir, "Info.plist")) # make symbolic links links = [ @@ -178,12 +194,12 @@ if __name__ == "__main__": parser.add_argument('--contrib', metavar='DIR', default=None, help='folder with opencv_contrib repository (default is "None" - build only main framework)') args = parser.parse_args() - b = Builder(args.opencv, args.contrib) - try: - b.build(args.out) - except Exception as e: - print("="*60, file=sys.stderr) - print("ERROR: %s" % e, file=sys.stderr) - print("="*60, file=sys.stderr) - traceback.print_exc(file=sys.stderr) - sys.exit(1) + b = Builder(args.opencv, args.contrib, + [ + ("armv7", "iPhoneOS"), + ("armv7s", "iPhoneOS"), + ("arm64", "iPhoneOS"), + ("i386", "iPhoneSimulator"), + ("x86_64", "iPhoneSimulator"), + ]) + b.build(args.out) diff --git a/platforms/osx/build_framework.py b/platforms/osx/build_framework.py old mode 100755 new mode 100644 index 456ea9572..94886a2a3 --- a/platforms/osx/build_framework.py +++ b/platforms/osx/build_framework.py @@ -1,124 +1,45 @@ #!/usr/bin/env python """ -The script builds OpenCV.framework for iOS. -The built framework is universal, it can be used to build app and run it on either iOS simulator or real device. - -Usage: - ./build_framework.py - -By cmake conventions (and especially if you work with OpenCV repository), -the output dir should not be a subdirectory of OpenCV source tree. - -Script will create , if it's missing, and a few its subdirectories: - - - build/ - iPhoneOS-*/ - [cmake-generated build tree for an iOS device target] - iPhoneSimulator/ - [cmake-generated build tree for iOS simulator] - opencv2.framework/ - [the framework content] - -The script should handle minor OpenCV updates efficiently -- it does not recompile the library from scratch each time. -However, opencv2.framework directory is erased and recreated on each run. +The script builds OpenCV.framework for OSX. """ -import glob, re, os, os.path, shutil, string, sys +from __future__ import print_function +import os, os.path, sys, argparse, traceback -def build_opencv(srcroot, buildroot, target, arch): - "builds OpenCV for device or simulator" +# import common code +sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios')) +from build_framework import Builder - builddir = os.path.join(buildroot, target + '-' + arch) - if not os.path.isdir(builddir): - os.makedirs(builddir) - currdir = os.getcwd() - os.chdir(builddir) - # for some reason, if you do not specify CMAKE_BUILD_TYPE, it puts libs to "RELEASE" rather than "Release" - cmakeargs = ("-GXcode " + - "-DCMAKE_BUILD_TYPE=Release " + - "-DBUILD_SHARED_LIBS=OFF " + - "-DBUILD_DOCS=OFF " + - "-DBUILD_EXAMPLES=OFF " + - "-DBUILD_TESTS=OFF " + - "-DBUILD_PERF_TESTS=OFF " + - "-DBUILD_opencv_apps=OFF " + - "-DBUILD_opencv_world=ON " + - "-DBUILD_opencv_matlab=OFF " + - "-DWITH_TIFF=OFF -DBUILD_TIFF=OFF " + - "-DWITH_JASPER=OFF -DBUILD_JASPER=OFF " + - "-DWITH_WEBP=OFF -DBUILD_WEBP=OFF " + - "-DWITH_OPENEXR=OFF -DBUILD_OPENEXR=OFF " + - "-DWITH_IPP=OFF -DWITH_IPP_A=OFF " + - "-DCMAKE_C_FLAGS=\"-Wno-implicit-function-declaration\" " + - "-DCMAKE_INSTALL_PREFIX=install") - # if cmake cache exists, just rerun cmake to update OpenCV.xproj if necessary - if os.path.isfile(os.path.join(builddir, "CMakeCache.txt")): - os.system("cmake %s ." % (cmakeargs,)) - else: - os.system("cmake %s %s" % (cmakeargs, srcroot)) +class OSXBuilder(Builder): - for wlib in [builddir + "/modules/world/UninstalledProducts/libopencv_world.a", - builddir + "/lib/Release/libopencv_world.a"]: - if os.path.isfile(wlib): - os.remove(wlib) + def getToolchain(self, arch, target): + return None - os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 2 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower())) - os.system("xcodebuild ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower())) - os.chdir(currdir) + def getBuildCommand(self, arch, target): + buildcmd = [ + "xcodebuild", + "ARCHS=%s" % arch, + "-sdk", target.lower(), + "-configuration", "Release", + "-parallelizeTargets", + "-jobs", "4" + ] + return buildcmd -def put_framework_together(srcroot, dstroot): - "constructs the framework directory after all the targets are built" - - # find the list of targets (basically, ["iPhoneOS", "iPhoneSimulator"]) - targetlist = glob.glob(os.path.join(dstroot, "build", "*")) - targetlist = [os.path.basename(t) for t in targetlist] - - # set the current dir to the dst root - currdir = os.getcwd() - framework_dir = dstroot + "/opencv2.framework" - if os.path.isdir(framework_dir): - shutil.rmtree(framework_dir) - os.makedirs(framework_dir) - os.chdir(framework_dir) - - # form the directory tree - dstdir = "Versions/A" - os.makedirs(dstdir + "/Resources") - - tdir0 = "../build/" + targetlist[0] - # copy headers - shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers") - - # make universal static lib - wlist = " ".join(["../build/" + t + "/lib/Release/libopencv_world.a" for t in targetlist]) - os.system("lipo -create " + wlist + " -o " + dstdir + "/opencv2") - - # copy Info.plist - shutil.copyfile(tdir0 + "/osx/Info.plist", dstdir + "/Resources/Info.plist") - - # make symbolic links - os.symlink("A", "Versions/Current") - os.symlink("Versions/Current/Headers", "Headers") - os.symlink("Versions/Current/Resources", "Resources") - os.symlink("Versions/Current/opencv2", "opencv2") - - -def build_framework(srcroot, dstroot): - "main function to do all the work" - - targets = ["MacOSX", "MacOSX" ] - archs = ["x86_64", "i386" ] - for i in range(len(targets)): - build_opencv(srcroot, os.path.join(dstroot, "build"), targets[i], archs[i]) - - put_framework_together(srcroot, dstroot) + def getInfoPlist(self, builddirs): + return os.path.join(builddirs[0], "osx", "Info.plist") if __name__ == "__main__": - if len(sys.argv) != 2: - print "Usage:\n\t./build_framework.py \n\n" - sys.exit(0) + folder = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")) + parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for OSX.') + parser.add_argument('out', metavar='OUTDIR', help='folder to put built framework') + parser.add_argument('--opencv', metavar='DIR', default=folder, help='folder with opencv repository (default is "../.." relative to script location)') + parser.add_argument('--contrib', metavar='DIR', default=None, help='folder with opencv_contrib repository (default is "None" - build only main framework)') + args = parser.parse_args() - build_framework(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")), os.path.abspath(sys.argv[1])) + b = OSXBuilder(args.opencv, args.contrib, + [ + ("x86_64", "MacOSX") + ]) + b.build(args.out) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 2b0e74ccd..70b85eecd 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -31,7 +31,7 @@ if(ANDROID AND BUILD_ANDROID_EXAMPLES) endif() if(INSTALL_PYTHON_EXAMPLES) - add_subdirectory(python2) + add_subdirectory(python) endif() # diff --git a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java index 71f59fa13..df428b570 100644 --- a/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java +++ b/samples/android/15-puzzle/src/org/opencv/samples/puzzle15/Puzzle15Activity.java @@ -59,6 +59,7 @@ public class Puzzle15Activity extends Activity implements CvCameraViewListener, Log.d(TAG, "Creating and setting view"); mOpenCvCameraView = (CameraBridgeViewBase) new JavaCameraView(this, -1); setContentView(mOpenCvCameraView); + mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); mPuzzle15 = new Puzzle15Processor(); mPuzzle15.prepareNewGame(); diff --git a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java index 97d348c9d..3b4039449 100644 --- a/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java +++ b/samples/android/color-blob-detection/src/org/opencv/samples/colorblobdetect/ColorBlobDetectionActivity.java @@ -25,6 +25,7 @@ import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.View.OnTouchListener; +import android.view.SurfaceView; public class ColorBlobDetectionActivity extends Activity implements OnTouchListener, CvCameraViewListener2 { private static final String TAG = "OCVSample::Activity"; @@ -73,6 +74,7 @@ public class ColorBlobDetectionActivity extends Activity implements OnTouchListe setContentView(R.layout.color_blob_detection_surface_view); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.color_blob_detection_activity_surface_view); + mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); } diff --git a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java index 48a89b0f8..d7d435951 100644 --- a/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java +++ b/samples/android/face-detection/src/org/opencv/samples/facedetect/FdActivity.java @@ -125,6 +125,7 @@ public class FdActivity extends Activity implements CvCameraViewListener2 { setContentView(R.layout.face_detect_surface_view); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view); + mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); } diff --git a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java index 382ce7503..8add58fcb 100644 --- a/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java +++ b/samples/android/image-manipulations/src/org/opencv/samples/imagemanipulations/ImageManipulationsActivity.java @@ -96,6 +96,7 @@ public class ImageManipulationsActivity extends Activity implements CvCameraView setContentView(R.layout.image_manipulations_surface_view); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.image_manipulations_activity_surface_view); + mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); } diff --git a/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java b/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java index d8bb28a7e..bcef8886d 100644 --- a/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java +++ b/samples/android/tutorial-2-mixedprocessing/src/org/opencv/samples/tutorial2/Tutorial2Activity.java @@ -72,6 +72,7 @@ public class Tutorial2Activity extends Activity implements CvCameraViewListener2 setContentView(R.layout.tutorial2_surface_view); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial2_activity_surface_view); + mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); } diff --git a/samples/cpp/3calibration.cpp b/samples/cpp/3calibration.cpp index cf37e6500..12f2c81fc 100644 --- a/samples/cpp/3calibration.cpp +++ b/samples/cpp/3calibration.cpp @@ -6,6 +6,7 @@ #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/imgcodecs/imgcodecs.hpp" #include "opencv2/highgui/highgui.hpp" +#include "opencv2/core/utility.hpp" #include #include @@ -20,12 +21,12 @@ static void help() { printf( "\nThis is a camera calibration sample that calibrates 3 horizontally placed cameras together.\n" "Usage: 3calibration\n" - " -w # the number of inner corners per one of board dimension\n" - " -h # the number of inner corners per another board dimension\n" - " [-s ] # square size in some user-defined units (1 by default)\n" - " [-o ] # the output filename for intrinsic [and extrinsic] parameters\n" + " -w= # the number of inner corners per one of board dimension\n" + " -h= # the number of inner corners per another board dimension\n" + " [-s=] # square size in some user-defined units (1 by default)\n" + " [-o=] # the output filename for intrinsic [and extrinsic] parameters\n" " [-zt] # assume zero tangential distortion\n" - " [-a ] # fix aspect ratio (fx/fy)\n" + " [-a=] # fix aspect ratio (fx/fy)\n" " [-p] # fix the principal point at the center\n" " [input_data] # input data - text file with a list of the images of the board\n" "\n" ); @@ -42,7 +43,7 @@ static void calcChessboardCorners(Size boardSize, float squareSize, vector > imagePoints1, +static bool run3Calibration(vector > imagePoints1, vector > imagePoints2, vector > imagePoints3, Size imageSize, Size boardSize, @@ -177,65 +178,48 @@ int main( int argc, char** argv ) int i, k; int flags = 0; Size boardSize, imageSize; - float squareSize = 1.f, aspectRatio = 1.f; - const char* outputFilename = "out_camera_data.yml"; - const char* inputFilename = 0; + float squareSize, aspectRatio; + string outputFilename; + string inputFilename = ""; vector > imgpt[3]; vector imageList; - if(argc < 2) + cv::CommandLineParser parser(argc, argv, + "{help ||}{w||}{h||}{s|1|}{o|out_camera_data.yml|}" + "{zt||}{a|1|}{p||}{@input||}"); + if (parser.has("help")) { help(); - return 1; + return 0; } - - - for( i = 1; i < argc; i++ ) + boardSize.width = parser.get("w"); + boardSize.height = parser.get("h"); + squareSize = parser.get("s"); + aspectRatio = parser.get("a"); + if (parser.has("a")) + flags |= CALIB_FIX_ASPECT_RATIO; + if (parser.has("zt")) + flags |= CALIB_ZERO_TANGENT_DIST; + if (parser.has("p")) + flags |= CALIB_FIX_PRINCIPAL_POINT; + outputFilename = parser.get("o"); + inputFilename = parser.get("@input"); + if (!parser.check()) { - const char* s = argv[i]; - if( strcmp( s, "-w" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 ) - return fprintf( stderr, "Invalid board width\n" ), -1; - } - else if( strcmp( s, "-h" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 ) - return fprintf( stderr, "Invalid board height\n" ), -1; - } - else if( strcmp( s, "-s" ) == 0 ) - { - if( sscanf( argv[++i], "%f", &squareSize ) != 1 || squareSize <= 0 ) - return fprintf( stderr, "Invalid board square width\n" ), -1; - } - else if( strcmp( s, "-a" ) == 0 ) - { - if( sscanf( argv[++i], "%f", &aspectRatio ) != 1 || aspectRatio <= 0 ) - return printf("Invalid aspect ratio\n" ), -1; - flags |= CALIB_FIX_ASPECT_RATIO; - } - else if( strcmp( s, "-zt" ) == 0 ) - { - flags |= CALIB_ZERO_TANGENT_DIST; - } - else if( strcmp( s, "-p" ) == 0 ) - { - flags |= CALIB_FIX_PRINCIPAL_POINT; - } - else if( strcmp( s, "-o" ) == 0 ) - { - outputFilename = argv[++i]; - } - else if( s[0] != '-' ) - { - inputFilename = s; - } - else - return fprintf( stderr, "Unknown option %s", s ), -1; + help(); + parser.printErrors(); + return -1; } - - if( !inputFilename || + if (boardSize.width <= 0) + return fprintf( stderr, "Invalid board width\n" ), -1; + if (boardSize.height <= 0) + return fprintf( stderr, "Invalid board height\n" ), -1; + if (squareSize <= 0) + return fprintf( stderr, "Invalid board square width\n" ), -1; + if (aspectRatio <= 0) + return printf("Invalid aspect ratio\n" ), -1; + if( inputFilename.empty() || !readStringList(inputFilename, imageList) || imageList.size() == 0 || imageList.size() % 3 != 0 ) { diff --git a/samples/cpp/CMakeLists.txt b/samples/cpp/CMakeLists.txt index ff5caa9fa..d93d0196a 100644 --- a/samples/cpp/CMakeLists.txt +++ b/samples/cpp/CMakeLists.txt @@ -6,7 +6,7 @@ SET(OPENCV_CPP_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_flann opencv_imgcodecs opencv_videoio opencv_highgui opencv_ml opencv_video opencv_objdetect opencv_photo opencv_features2d opencv_calib3d - opencv_stitching opencv_videostab opencv_shape) + opencv_stitching opencv_videostab opencv_shape ${OPENCV_MODULES_PUBLIC} ${OpenCV_LIB_COMPONENTS}) ocv_check_dependencies(${OPENCV_CPP_SAMPLES_REQUIRED_DEPS}) @@ -30,14 +30,6 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND) ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/cudafilters/include") endif() - if (HAVE_opencv_xfeatures2d) - ocv_include_directories("${OPENCV_MODULE_opencv_xfeatures2d_LOCATION}/include") - endif() - - if(HAVE_opencv_ocl) - ocv_include_directories("${OpenCV_SOURCE_DIR}/modules/ocl/include") - endif() - if(CMAKE_COMPILER_IS_GNUCXX AND NOT ENABLE_NOISY_WARNINGS) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") endif() diff --git a/samples/cpp/autofocus.cpp b/samples/cpp/autofocus.cpp index f237fb2dd..cef279d6f 100644 --- a/samples/cpp/autofocus.cpp +++ b/samples/cpp/autofocus.cpp @@ -43,11 +43,11 @@ const double epsylon = 0.0005; // compression, noice, etc. struct Args_t { - const char * deviceName; - const char * output; - unsigned int fps; - unsigned int minimumFocusStep; - unsigned int breakLimit; + string deviceName; + string output; + int fps; + int minimumFocusStep; + int breakLimit; bool measure; bool verbose; } GlobalArgs; @@ -218,12 +218,12 @@ static void showHelp(const char * pName, bool welcomeMsg) cout << "usage " << pName << ": [OPTIONS] DEVICE_NAME\n\n" "OPTIONS:\n" "\t-h\t\treturns this help message,\n" - "\t-o FILENAME\tsave output video in file (MJPEG only),\n" - "\t-f FPS\t\tframes per second in output video,\n" + "\t-o=\tsave output video in file (MJPEG only),\n" + "\t-f=FPS\t\tframes per second in output video,\n" "\t-m\t\tmeasure exposition\n" "\t\t\t(returns rates from closest focus to INTY\n" "\t\t\tfor every minimum step),\n" - "\t-d INT\t\tset minimum focus step,\n" + "\t-d=\t\tset minimum focus step,\n" "\t-v\t\tverbose mode.\n\n\n" "DEVICE_NAME\t\tis your digital camera model substring.\n\n\n" "On runtime you can use keys to control:\n"; @@ -244,60 +244,36 @@ static void showHelp(const char * pName, bool welcomeMsg) static bool parseArguments(int argc, char ** argv) { - int index; - GlobalArgs.deviceName = "Nikon"; - GlobalArgs.output = NULL; - GlobalArgs.fps = DEFAULT_OUTPUT_FPS; - GlobalArgs.minimumFocusStep = 0; + cv::CommandLineParser parser(argc, argv, "{h help ||}{o||}{f||}{m||}{d|0|}{v||}{@device|Nikon|}"); + if (parser.has("help")) + return false; GlobalArgs.breakLimit = DEFAULT_BREAK_LIMIT; - GlobalArgs.measure = false; - GlobalArgs.verbose = false; - - for (index = 1; index < argc; index++) + if (parser.has("o")) + GlobalArgs.output = parser.get("o"); + else + GlobalArgs.output = ""; + if (parser.has("f")) + GlobalArgs.fps = parser.get("f"); + else + GlobalArgs.fps = DEFAULT_OUTPUT_FPS; + GlobalArgs.measure = parser.has("m"); + GlobalArgs.verbose = parser.has("v"); + GlobalArgs.minimumFocusStep = parser.get("d"); + GlobalArgs.deviceName = parser.get("@device"); + if (!parser.check()) { - const char * arg = argv[index]; - if (strcmp(arg, "-h") == 0) - { - return false; - } - else if (strcmp(arg, "-o") == 0) - { - GlobalArgs.output = argv[++index]; - } - else if (strcmp(arg, "-f") == 0) - { - if (sscanf(argv[++index], "%u", &GlobalArgs.fps) != 1 - || GlobalArgs.fps <= 0) - { - cerr << "Invalid fps argument." << endl; - return false; - } - } - else if (strcmp(arg, "-m") == 0) - { - GlobalArgs.measure = true; - } - else if (strcmp(arg, "-v") == 0) - { - GlobalArgs.verbose = true; - } - else if (strcmp(arg, "-d") == 0) - { - if (sscanf(argv[++index], "%u", &GlobalArgs.minimumFocusStep) != 1 - || GlobalArgs.minimumFocusStep <= 0) - { - cerr << "Invalid minimum focus step argument." << endl; - return false; - } - } - else if (arg[0] != '-') - { - GlobalArgs.deviceName = arg; - } - else - { - cerr << "Unknown option " << arg << endl; - } + parser.printErrors(); + return false; + } + if (GlobalArgs.fps < 0) + { + cerr << "Invalid fps argument." << endl; + return false; + } + if (GlobalArgs.minimumFocusStep < 0) + { + cerr << "Invalid minimum focus step argument." << endl; + return false; } return true; } @@ -343,7 +319,7 @@ int main(int argc, char ** argv) cap.set(CAP_PROP_GPHOTO2_PREVIEW, true); cap.set(CAP_PROP_VIEWFINDER, true); cap >> frame; // To check PREVIEW output Size. - if (GlobalArgs.output != NULL) + if (!GlobalArgs.output.empty()) { Size S = Size((int) cap.get(CAP_PROP_FRAME_WIDTH), (int) cap.get(CAP_PROP_FRAME_HEIGHT)); int fourCC = CV_FOURCC('M', 'J', 'P', 'G'); @@ -375,7 +351,7 @@ int main(int argc, char ** argv) { break; } - if (GlobalArgs.output != NULL) + if (!GlobalArgs.output.empty()) { videoWriter << frame; } diff --git a/samples/cpp/calibration.cpp b/samples/cpp/calibration.cpp index 7c751f8f6..415d8bb83 100644 --- a/samples/cpp/calibration.cpp +++ b/samples/cpp/calibration.cpp @@ -16,11 +16,11 @@ using namespace std; const char * usage = " \nexample command line for calibration from a live feed.\n" -" calibration -w 4 -h 5 -s 0.025 -o camera.yml -op -oe\n" +" calibration -w=4 -h=5 -s=0.025 -o=camera.yml -op -oe\n" " \n" " example command line for calibration from a list of stored images:\n" " imagelist_creator image_list.xml *.png\n" -" calibration -w 4 -h 5 -s 0.025 -o camera.yml -op -oe image_list.xml\n" +" calibration -w=4 -h=5 -s=0.025 -o=camera.yml -op -oe image_list.xml\n" " where image_list.xml is the standard OpenCV XML/YAML\n" " use imagelist_creator to create the xml or yaml list\n" " file consisting of the list of strings, e.g.:\n" @@ -50,20 +50,20 @@ static void help() { printf( "This is a camera calibration sample.\n" "Usage: calibration\n" - " -w # the number of inner corners per one of board dimension\n" - " -h # the number of inner corners per another board dimension\n" - " [-pt ] # the type of pattern: chessboard or circles' grid\n" - " [-n ] # the number of frames to use for calibration\n" + " -w= # the number of inner corners per one of board dimension\n" + " -h= # the number of inner corners per another board dimension\n" + " [-pt=] # the type of pattern: chessboard or circles' grid\n" + " [-n=] # the number of frames to use for calibration\n" " # (if not specified, it will be set to the number\n" " # of board views actually available)\n" - " [-d ] # a minimum delay in ms between subsequent attempts to capture a next view\n" + " [-d=] # a minimum delay in ms between subsequent attempts to capture a next view\n" " # (used only for video capturing)\n" - " [-s ] # square size in some user-defined units (1 by default)\n" - " [-o ] # the output filename for intrinsic [and extrinsic] parameters\n" + " [-s=] # square size in some user-defined units (1 by default)\n" + " [-o=] # the output filename for intrinsic [and extrinsic] parameters\n" " [-op] # write detected feature points\n" " [-oe] # write extrinsic parameters\n" " [-zt] # assume zero tangential distortion\n" - " [-a ] # fix aspect ratio (fx/fy)\n" + " [-a=] # fix aspect ratio (fx/fy)\n" " [-p] # fix the principal point at the center\n" " [-v] # flip the captured images around the horizontal axis\n" " [-V] # use a video file, and not an image list, uses\n" @@ -297,20 +297,20 @@ static bool runAndSave(const string& outputFilename, int main( int argc, char** argv ) { Size boardSize, imageSize; - float squareSize = 1.f, aspectRatio = 1.f; + float squareSize, aspectRatio; Mat cameraMatrix, distCoeffs; - const char* outputFilename = "out_camera_data.yml"; - const char* inputFilename = 0; + string outputFilename; + string inputFilename = ""; - int i, nframes = 10; - bool writeExtrinsics = false, writePoints = false; + int i, nframes; + bool writeExtrinsics, writePoints; bool undistortImage = false; int flags = 0; VideoCapture capture; - bool flipVertical = false; - bool showUndistorted = false; - bool videofile = false; - int delay = 1000; + bool flipVertical; + bool showUndistorted; + bool videofile; + int delay; clock_t prevTimestamp = 0; int mode = DETECTION; int cameraId = 0; @@ -318,102 +318,70 @@ int main( int argc, char** argv ) vector imageList; Pattern pattern = CHESSBOARD; - if( argc < 2 ) + cv::CommandLineParser parser(argc, argv, + "{help ||}{w||}{h||}{pt|chessboard|}{n|10|}{d|1000|}{s|1|}{o|out_camera_data.yml|}" + "{op||}{oe||}{zt||}{a|1|}{p||}{v||}{V||}{su||}" + "{@input_data|0|}"); + if (parser.has("help")) { help(); return 0; } - - for( i = 1; i < argc; i++ ) + boardSize.width = parser.get( "w" ); + boardSize.height = parser.get( "h" ); + if ( parser.has("pt") ) { - const char* s = argv[i]; - if( strcmp( s, "-w" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &boardSize.width ) != 1 || boardSize.width <= 0 ) - return fprintf( stderr, "Invalid board width\n" ), -1; - } - else if( strcmp( s, "-h" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &boardSize.height ) != 1 || boardSize.height <= 0 ) - return fprintf( stderr, "Invalid board height\n" ), -1; - } - else if( strcmp( s, "-pt" ) == 0 ) - { - i++; - if( !strcmp( argv[i], "circles" ) ) - pattern = CIRCLES_GRID; - else if( !strcmp( argv[i], "acircles" ) ) - pattern = ASYMMETRIC_CIRCLES_GRID; - else if( !strcmp( argv[i], "chessboard" ) ) - pattern = CHESSBOARD; - else - return fprintf( stderr, "Invalid pattern type: must be chessboard or circles\n" ), -1; - } - else if( strcmp( s, "-s" ) == 0 ) - { - if( sscanf( argv[++i], "%f", &squareSize ) != 1 || squareSize <= 0 ) - return fprintf( stderr, "Invalid board square width\n" ), -1; - } - else if( strcmp( s, "-n" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &nframes ) != 1 || nframes <= 3 ) - return printf("Invalid number of images\n" ), -1; - } - else if( strcmp( s, "-a" ) == 0 ) - { - if( sscanf( argv[++i], "%f", &aspectRatio ) != 1 || aspectRatio <= 0 ) - return printf("Invalid aspect ratio\n" ), -1; - flags |= CALIB_FIX_ASPECT_RATIO; - } - else if( strcmp( s, "-d" ) == 0 ) - { - if( sscanf( argv[++i], "%u", &delay ) != 1 || delay <= 0 ) - return printf("Invalid delay\n" ), -1; - } - else if( strcmp( s, "-op" ) == 0 ) - { - writePoints = true; - } - else if( strcmp( s, "-oe" ) == 0 ) - { - writeExtrinsics = true; - } - else if( strcmp( s, "-zt" ) == 0 ) - { - flags |= CALIB_ZERO_TANGENT_DIST; - } - else if( strcmp( s, "-p" ) == 0 ) - { - flags |= CALIB_FIX_PRINCIPAL_POINT; - } - else if( strcmp( s, "-v" ) == 0 ) - { - flipVertical = true; - } - else if( strcmp( s, "-V" ) == 0 ) - { - videofile = true; - } - else if( strcmp( s, "-o" ) == 0 ) - { - outputFilename = argv[++i]; - } - else if( strcmp( s, "-su" ) == 0 ) - { - showUndistorted = true; - } - else if( s[0] != '-' ) - { - if( isdigit(s[0]) ) - sscanf(s, "%d", &cameraId); - else - inputFilename = s; - } + string val = parser.get("pt"); + if( val == "circles" ) + pattern = CIRCLES_GRID; + else if( val == "acircles" ) + pattern = ASYMMETRIC_CIRCLES_GRID; + else if( val == "chessboard" ) + pattern = CHESSBOARD; else - return fprintf( stderr, "Unknown option %s", s ), -1; + return fprintf( stderr, "Invalid pattern type: must be chessboard or circles\n" ), -1; } + squareSize = parser.get("s"); + nframes = parser.get("n"); + aspectRatio = parser.get("a"); + delay = parser.get("d"); + writePoints = parser.has("op"); + writeExtrinsics = parser.has("oe"); + if (parser.has("a")) + flags |= CALIB_FIX_ASPECT_RATIO; + if ( parser.has("zt") ) + flags |= CALIB_ZERO_TANGENT_DIST; + if ( parser.has("p") ) + flags |= CALIB_FIX_PRINCIPAL_POINT; + flipVertical = parser.has("v"); + videofile = parser.has("V"); + if ( parser.has("o") ) + outputFilename = parser.get("o"); + showUndistorted = parser.has("su"); + if ( isdigit(parser.get("@input_data")[0]) ) + cameraId = parser.get("@input_data"); + else + inputFilename = parser.get("@input_data"); + if (!parser.check()) + { + help(); + parser.printErrors(); + return -1; + } + if ( squareSize <= 0 ) + return fprintf( stderr, "Invalid board square width\n" ), -1; + if ( nframes <= 3 ) + return printf("Invalid number of images\n" ), -1; + if ( aspectRatio <= 0 ) + return printf( "Invalid aspect ratio\n" ), -1; + if ( delay <= 0 ) + return printf( "Invalid delay\n" ), -1; + if ( boardSize.width <= 0 ) + return fprintf( stderr, "Invalid board width\n" ), -1; + if ( boardSize.height <= 0 ) + return fprintf( stderr, "Invalid board height\n" ), -1; - if( inputFilename ) + if( !inputFilename.empty() ) { if( !videofile && readStringList(inputFilename, imageList) ) mode = CAPTURING; diff --git a/samples/cpp/camshiftdemo.cpp b/samples/cpp/camshiftdemo.cpp index 6400f1ec7..4286e253e 100644 --- a/samples/cpp/camshiftdemo.cpp +++ b/samples/cpp/camshiftdemo.cpp @@ -47,6 +47,15 @@ static void onMouse( int event, int x, int y, int, void* ) } } +string hot_keys = + "\n\nHot keys: \n" + "\tESC - quit the program\n" + "\tc - stop the tracking\n" + "\tb - switch to/from backprojection view\n" + "\th - show/hide object histogram\n" + "\tp - pause video\n" + "To initialize tracking, select the object with mouse\n"; + static void help() { cout << "\nThis is a demo that shows mean-shift based tracking\n" @@ -54,33 +63,28 @@ static void help() "This reads from video camera (0 by default, or the camera number the user enters\n" "Usage: \n" " ./camshiftdemo [camera number]\n"; - - cout << "\n\nHot keys: \n" - "\tESC - quit the program\n" - "\tc - stop the tracking\n" - "\tb - switch to/from backprojection view\n" - "\th - show/hide object histogram\n" - "\tp - pause video\n" - "To initialize tracking, select the object with mouse\n"; + cout << hot_keys; } const char* keys = { - "{@camera_number| 0 | camera number}" + "{help h | | show help message}{@camera_number| 0 | camera number}" }; int main( int argc, const char** argv ) { - help(); - VideoCapture cap; Rect trackWindow; int hsize = 16; float hranges[] = {0,180}; const float* phranges = hranges; CommandLineParser parser(argc, argv, keys); + if (parser.has("help")) + { + help(); + return 0; + } int camNum = parser.get(0); - cap.open(camNum); if( !cap.isOpened() ) @@ -91,7 +95,7 @@ int main( int argc, const char** argv ) parser.printMessage(); return -1; } - + cout << hot_keys; namedWindow( "Histogram", 0 ); namedWindow( "CamShift Demo", 0 ); setMouseCallback( "CamShift Demo", onMouse, 0 ); diff --git a/samples/cpp/cloning_gui.cpp b/samples/cpp/cloning_gui.cpp index 668dd2a95..289eae8de 100644 --- a/samples/cpp/cloning_gui.cpp +++ b/samples/cpp/cloning_gui.cpp @@ -231,7 +231,7 @@ void destination(int event, int x, int y, int, void*) if(maxxd > im1.size().width || maxyd > im1.size().height || minxd < 0 || minyd < 0) { cout << "Index out of range" << endl; - exit(0); + exit(1); } final1 = Mat::zeros(img2.size(),CV_8UC3); @@ -322,12 +322,12 @@ int main() if(img0.empty()) { cout << "Source Image does not exist" << endl; - exit(0); + exit(2); } if(img2.empty()) { cout << "Destination Image does not exist" << endl; - exit(0); + exit(2); } channel = img0.channels(); @@ -370,7 +370,7 @@ int main() if(img0.empty()) { cout << "Source Image does not exist" << endl; - exit(0); + exit(2); } res1 = Mat::zeros(img0.size(),CV_8UC1); @@ -400,7 +400,7 @@ int main() if(img0.empty()) { cout << "Source Image does not exist" << endl; - exit(0); + exit(2); } res1 = Mat::zeros(img0.size(),CV_8UC1); @@ -433,7 +433,7 @@ int main() if(img0.empty()) { cout << "Source Image does not exist" << endl; - exit(0); + exit(2); } res1 = Mat::zeros(img0.size(),CV_8UC1); @@ -448,7 +448,7 @@ int main() else { cout << "Wrong Option Choosen" << endl; - exit(0); + exit(1); } for(;;) @@ -541,7 +541,7 @@ int main() imwrite("cloned.png",blend); } else if(key == 'q') - exit(0); + break; } - waitKey(0); + return 0; } diff --git a/samples/cpp/connected_components.cpp b/samples/cpp/connected_components.cpp index d1c3411a7..6c978f153 100644 --- a/samples/cpp/connected_components.cpp +++ b/samples/cpp/connected_components.cpp @@ -43,13 +43,17 @@ static void help() const char* keys = { - "{@image|../data/stuff.jpg|image for converting to a grayscale}" + "{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}" }; int main( int argc, const char** argv ) { - help(); CommandLineParser parser(argc, argv, keys); + if (parser.has("help")) + { + help(); + return 0; + } string inputImage = parser.get(0); img = imread(inputImage.c_str(), 0); diff --git a/samples/cpp/contours2.cpp b/samples/cpp/contours2.cpp index 0b488959e..c5a1fa70f 100644 --- a/samples/cpp/contours2.cpp +++ b/samples/cpp/contours2.cpp @@ -33,14 +33,15 @@ static void on_trackbar(int, void*) imshow("contours", cnt_img); } -int main( int argc, char**) +int main( int argc, char** argv) { - Mat img = Mat::zeros(w, w, CV_8UC1); - if(argc > 1) + cv::CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) { help(); - return -1; + return 0; } + Mat img = Mat::zeros(w, w, CV_8UC1); //Draw 6 faces for( int i = 0; i < 6; i++ ) { diff --git a/samples/cpp/convexhull.cpp b/samples/cpp/convexhull.cpp index 2aef70fe7..36e554454 100644 --- a/samples/cpp/convexhull.cpp +++ b/samples/cpp/convexhull.cpp @@ -13,13 +13,17 @@ static void help() << "./convexhull\n" << endl; } -int main( int /*argc*/, char** /*argv*/ ) +int main( int argc, char** argv ) { + CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) + { + help(); + return 0; + } Mat img(500, 500, CV_8UC3); RNG& rng = theRNG(); - help(); - for(;;) { char key; diff --git a/samples/cpp/cout_mat.cpp b/samples/cpp/cout_mat.cpp index 2261d837c..bf1dfb2a4 100644 --- a/samples/cpp/cout_mat.cpp +++ b/samples/cpp/cout_mat.cpp @@ -25,9 +25,14 @@ static void help() } -int main(int,char**) +int main(int argc, char** argv) { - help(); + cv::CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) + { + help(); + return 0; + } Mat I = Mat::eye(4, 4, CV_64F); I.at(1,1) = CV_PI; cout << "I = \n" << I << ";" << endl << endl; diff --git a/samples/cpp/create_mask.cpp b/samples/cpp/create_mask.cpp index 42225ebc6..7d6a61cfb 100644 --- a/samples/cpp/create_mask.cpp +++ b/samples/cpp/create_mask.cpp @@ -123,14 +123,21 @@ void mouseHandler(int event, int x, int y, int, void*) int main(int argc, char **argv) { - - if(argc != 2) + cv::CommandLineParser parser(argc, argv, "{help h | | show help message}{@input | | input image}"); + if (parser.has("help")) { - cout << "usage: " << argv[0] << " " << endl; - exit(1); + parser.printMessage(); + return 0; + } + string input_image = parser.get("@input"); + if (input_image.empty()) + { + parser.printMessage(); + parser.printErrors(); + return 0; } - Mat src = imread(argv[1]); + Mat src = imread(input_image); minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN; diff --git a/samples/cpp/delaunay2.cpp b/samples/cpp/delaunay2.cpp index a00e31a98..a370feb2a 100644 --- a/samples/cpp/delaunay2.cpp +++ b/samples/cpp/delaunay2.cpp @@ -103,9 +103,14 @@ static void paint_voronoi( Mat& img, Subdiv2D& subdiv ) } -int main( int, char** ) +int main( int argc, char** argv ) { - help(); + cv::CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) + { + help(); + return 0; + } Scalar active_facet_color(0, 0, 255), delaunay_color(255,255,255); Rect rect(0, 0, 600, 600); diff --git a/samples/cpp/demhist.cpp b/samples/cpp/demhist.cpp index 39661f0a8..d4a4c9204 100644 --- a/samples/cpp/demhist.cpp +++ b/samples/cpp/demhist.cpp @@ -64,14 +64,17 @@ static void help() const char* keys = { - "{@image|../data/baboon.jpg|input image file}" + "{help h||}{@image|../data/baboon.jpg|input image file}" }; int main( int argc, const char** argv ) { - help(); - CommandLineParser parser(argc, argv, keys); + if (parser.has("help")) + { + help(); + return 0; + } string inputImage = parser.get(0); // Load the source image. HighGUI use. diff --git a/samples/cpp/detect_blob.cpp b/samples/cpp/detect_blob.cpp index badbaaa41..879b47e69 100644 --- a/samples/cpp/detect_blob.cpp +++ b/samples/cpp/detect_blob.cpp @@ -69,19 +69,13 @@ int main(int argc, char *argv[]) { vector fileName; Mat img(600, 800, CV_8UC1); - if (argc == 1) - { - fileName.push_back("../data/detect_blob.png"); - } - else if (argc == 2) - { - fileName.push_back(argv[1]); - } - else + cv::CommandLineParser parser(argc, argv, "{@input |../data/detect_blob.png| }{h help | | }"); + if (parser.has("h")) { help(); - return(0); + return 0; } + fileName.push_back(parser.get("@input")); img = imread(fileName[0], IMREAD_COLOR); if (img.rows*img.cols <= 0) { diff --git a/samples/cpp/detect_mser.cpp b/samples/cpp/detect_mser.cpp index 796cfa804..a9a3fa329 100644 --- a/samples/cpp/detect_mser.cpp +++ b/samples/cpp/detect_mser.cpp @@ -402,11 +402,18 @@ int main(int argc, char *argv[]) vector fileName; Mat imgOrig,img; Size blurSize(5,5); - if (argc==2) + cv::CommandLineParser parser(argc, argv, "{ help h | | }{ @input | | }"); + if (parser.has("help")) { - fileName.push_back(argv[1]); - imgOrig = imread(fileName[0], IMREAD_GRAYSCALE); blur(imgOrig, img, blurSize); - + help(); + return 0; + } + string input = parser.get("@input"); + if (!input.empty()) + { + fileName.push_back(input); + imgOrig = imread(fileName[0], IMREAD_GRAYSCALE); + blur(imgOrig, img, blurSize); } else { diff --git a/samples/cpp/dft.cpp b/samples/cpp/dft.cpp index 0525d9f4f..ddb56acf0 100644 --- a/samples/cpp/dft.cpp +++ b/samples/cpp/dft.cpp @@ -19,16 +19,20 @@ static void help() const char* keys = { - "{@image|../data/lena.jpg|input image file}" + "{help h||}{@image|../data/lena.jpg|input image file}" }; int main(int argc, const char ** argv) { help(); CommandLineParser parser(argc, argv, keys); + if (parser.has("help")) + { + help(); + return 0; + } string filename = parser.get(0); - - Mat img = imread(filename.c_str(), IMREAD_GRAYSCALE); + Mat img = imread(filename, IMREAD_GRAYSCALE); if( img.empty() ) { help(); diff --git a/samples/cpp/distrans.cpp b/samples/cpp/distrans.cpp index 070df3c48..4bf4707ba 100644 --- a/samples/cpp/distrans.cpp +++ b/samples/cpp/distrans.cpp @@ -107,15 +107,17 @@ static void help() const char* keys = { - "{@image |../data/stuff.jpg|input image file}" + "{help h||}{@image |../data/stuff.jpg|input image file}" }; int main( int argc, const char** argv ) { - help(); CommandLineParser parser(argc, argv, keys); + help(); + if (parser.has("help")) + return 0; string filename = parser.get(0); - gray = imread(filename.c_str(), 0); + gray = imread(filename, 0); if(gray.empty()) { printf("Cannot read image file: %s\n", filename.c_str()); diff --git a/samples/cpp/drawing.cpp b/samples/cpp/drawing.cpp index 43e8c112f..236bcbcc6 100644 --- a/samples/cpp/drawing.cpp +++ b/samples/cpp/drawing.cpp @@ -16,9 +16,14 @@ static Scalar randomColor(RNG& rng) return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255); } -int main() +int main(int argc, char** argv) { - help(); + cv::CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) + { + help(); + return 0; + } char wndname[] = "Drawing Demo"; const int NUMBER = 100; const int DELAY = 5; diff --git a/samples/cpp/edge.cpp b/samples/cpp/edge.cpp index 4b398e777..88abd8b25 100644 --- a/samples/cpp/edge.cpp +++ b/samples/cpp/edge.cpp @@ -33,14 +33,17 @@ static void help() const char* keys = { - "{@image |../data/fruits.jpg|input image name}" + "{help h||}{@image |../data/fruits.jpg|input image name}" }; int main( int argc, const char** argv ) { - help(); - CommandLineParser parser(argc, argv, keys); + if (parser.has("help")) + { + help(); + return 0; + } string filename = parser.get(0); image = imread(filename, 1); diff --git a/samples/cpp/facedetect.cpp b/samples/cpp/facedetect.cpp index 2be4e17e4..39717b733 100644 --- a/samples/cpp/facedetect.cpp +++ b/samples/cpp/facedetect.cpp @@ -27,73 +27,52 @@ void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip ); -string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml"; -string nestedCascadeName = "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; +string cascadeName; +string nestedCascadeName; int main( int argc, const char** argv ) { VideoCapture capture; Mat frame, image; - const string scaleOpt = "--scale="; - size_t scaleOptLen = scaleOpt.length(); - const string cascadeOpt = "--cascade="; - size_t cascadeOptLen = cascadeOpt.length(); - const string nestedCascadeOpt = "--nested-cascade"; - size_t nestedCascadeOptLen = nestedCascadeOpt.length(); - const string tryFlipOpt = "--try-flip"; - size_t tryFlipOptLen = tryFlipOpt.length(); string inputName; - bool tryflip = false; - - help(); - + bool tryflip; CascadeClassifier cascade, nestedCascade; - double scale = 1; + double scale; - for( int i = 1; i < argc; i++ ) + cv::CommandLineParser parser(argc, argv, + "{help h||}" + "{cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}" + "{nested-cascade|../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}" + "{scale|1|}{try-flip||}{@filename||}" + ); + if (parser.has("help")) { - cout << "Processing " << i << " " << argv[i] << endl; - if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 ) - { - cascadeName.assign( argv[i] + cascadeOptLen ); - cout << " from which we have cascadeName= " << cascadeName << endl; - } - else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 ) - { - if( argv[i][nestedCascadeOpt.length()] == '=' ) - nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 ); - if( !nestedCascade.load( nestedCascadeName ) ) - cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; - } - else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 ) - { - if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 ) - scale = 1; - cout << " from which we read scale = " << scale << endl; - } - else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 ) - { - tryflip = true; - cout << " will try to flip image horizontally to detect assymetric objects\n"; - } - else if( argv[i][0] == '-' ) - { - cerr << "WARNING: Unknown option %s" << argv[i] << endl; - } - else - inputName.assign( argv[i] ); + help(); + return 0; } - + cascadeName = parser.get("cascade"); + nestedCascadeName = parser.get("nested-cascade"); + scale = parser.get("scale"); + if (scale < 1) + scale = 1; + tryflip = parser.has("try-flip"); + inputName = parser.get("@filename"); + if (!parser.check()) + { + parser.printErrors(); + return 0; + } + if ( !nestedCascade.load( nestedCascadeName ) ) + cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; if( !cascade.load( cascadeName ) ) { cerr << "ERROR: Could not load classifier cascade" << endl; help(); return -1; } - - if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') ) + if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) ) { - int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ; + int c = inputName.empty() ? 0 : inputName[0] - '0'; if(!capture.open(c)) cout << "Capture from camera #" << c << " didn't work" << endl; } diff --git a/samples/cpp/facial_features.cpp b/samples/cpp/facial_features.cpp index 9cfbd85c7..f46fa3fb5 100644 --- a/samples/cpp/facial_features.cpp +++ b/samples/cpp/facial_features.cpp @@ -18,11 +18,6 @@ using namespace std; using namespace cv; -// Functions to parse command-line arguments -static string getCommandOption(const vector&, const string&); -static void setCommandOptions(vector&, int, char**); -static bool doesCmdOptionExist(const vector& , const string&); - // Functions for facial feature detection static void help(); static void detectFaces(Mat&, vector >&, string); @@ -36,22 +31,23 @@ string face_cascade_path, eye_cascade_path, nose_cascade_path, mouth_cascade_pat int main(int argc, char** argv) { - if(argc < 3) + cv::CommandLineParser parser(argc, argv, + "{eyes||}{nose||}{mouth||}{help h||}"); + if (parser.has("help")) { help(); + return 0; + } + input_image_path = parser.get(0); + face_cascade_path = parser.get(1); + eye_cascade_path = parser.has("eyes") ? parser.get("eyes") : ""; + nose_cascade_path = parser.has("nose") ? parser.get("nose") : ""; + mouth_cascade_path = parser.has("mouth") ? parser.get("mouth") : ""; + if (input_image_path.empty() || face_cascade_path.empty()) + { + cout << "IMAGE or FACE_CASCADE are not specified"; return 1; } - - // Extract command-line options - vector args; - setCommandOptions(args, argc, argv); - - input_image_path = argv[1]; - face_cascade_path = argv[2]; - eye_cascade_path = (doesCmdOptionExist(args, "-eyes")) ? getCommandOption(args, "-eyes") : ""; - nose_cascade_path = (doesCmdOptionExist(args, "-nose")) ? getCommandOption(args, "-nose") : ""; - mouth_cascade_path = (doesCmdOptionExist(args, "-mouth")) ? getCommandOption(args, "-mouth") : ""; - // Load image and cascade classifier files Mat image; image = imread(input_image_path); @@ -67,30 +63,6 @@ int main(int argc, char** argv) return 0; } -void setCommandOptions(vector& args, int argc, char** argv) -{ - for(int i = 1; i < argc; ++i) - { - args.push_back(argv[i]); - } - return; -} - -string getCommandOption(const vector& args, const string& opt) -{ - string answer; - vector::const_iterator it = find(args.begin(), args.end(), opt); - if(it != args.end() && (++it != args.end())) - answer = *it; - return answer; -} - -bool doesCmdOptionExist(const vector& args, const string& opt) -{ - vector::const_iterator it = find(args.begin(), args.end(), opt); - return (it != args.end()); -} - static void help() { cout << "\nThis file demonstrates facial feature points detection using Haarcascade classifiers.\n" @@ -103,15 +75,15 @@ static void help() "FACE_CASCSDE\n\t Path to a haarcascade classifier for face detection.\n" "OPTIONS: \nThere are 3 options available which are described in detail. There must be a " "space between the option and it's argument (All three options accept arguments).\n" - "\t-eyes : Specify the haarcascade classifier for eye detection.\n" - "\t-nose : Specify the haarcascade classifier for nose detection.\n" - "\t-mouth : Specify the haarcascade classifier for mouth detection.\n"; + "\t-eyes= : Specify the haarcascade classifier for eye detection.\n" + "\t-nose= : Specify the haarcascade classifier for nose detection.\n" + "\t-mouth= : Specify the haarcascade classifier for mouth detection.\n"; cout << "EXAMPLE:\n" - "(1) ./cpp-example-facial_features image.jpg face.xml -eyes eyes.xml -mouth mouth.xml\n" + "(1) ./cpp-example-facial_features image.jpg face.xml -eyes=eyes.xml -mouth=mouth.xml\n" "\tThis will detect the face, eyes and mouth in image.jpg.\n" - "(2) ./cpp-example-facial_features image.jpg face.xml -nose nose.xml\n" + "(2) ./cpp-example-facial_features image.jpg face.xml -nose=nose.xml\n" "\tThis will detect the face and nose in image.jpg.\n" "(3) ./cpp-example-facial_features image.jpg face.xml\n" "\tThis will detect only the face in image.jpg.\n"; diff --git a/samples/cpp/fback.cpp b/samples/cpp/fback.cpp index af0f2ca09..5fbec6913 100644 --- a/samples/cpp/fback.cpp +++ b/samples/cpp/fback.cpp @@ -30,8 +30,14 @@ static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step, } } -int main(int, char**) +int main(int argc, char** argv) { + cv::CommandLineParser parser(argc, argv, "{help h||}"); + if (parser.has("help")) + { + help(); + return 0; + } VideoCapture cap(0); help(); if( !cap.isOpened() ) diff --git a/samples/cpp/ffilldemo.cpp b/samples/cpp/ffilldemo.cpp index e099587a1..93bdd672b 100644 --- a/samples/cpp/ffilldemo.cpp +++ b/samples/cpp/ffilldemo.cpp @@ -73,12 +73,21 @@ static void onMouse( int event, int x, int y, int, void* ) int main( int argc, char** argv ) { - char* filename = argc >= 2 ? argv[1] : (char*)"../data/fruits.jpg"; + cv::CommandLineParser parser (argc, argv, + "{help h | | show help message}{@image|../data/fruits.jpg| input image}" + ); + if (parser.has("help")) + { + parser.printMessage(); + return 0; + } + string filename = parser.get("@image"); image0 = imread(filename, 1); if( image0.empty() ) { - cout << "Image empty. Usage: ffilldemo \n"; + cout << "Image empty\n"; + parser.printMessage(); return 0; } help(); diff --git a/samples/cpp/filestorage.cpp b/samples/cpp/filestorage.cpp index 41043d11c..60ea51aca 100644 --- a/samples/cpp/filestorage.cpp +++ b/samples/cpp/filestorage.cpp @@ -70,14 +70,21 @@ static ostream& operator<<(ostream& out, const MyData& m){ } int main(int ac, char** av) { - if (ac != 2) + cv::CommandLineParser parser(ac, av, + "{@input||}{help h ||}" + ); + if (parser.has("help")) + { + help(av); + return 0; + } + string filename = parser.get("@input"); + if (filename.empty()) { help(av); return 1; } - string filename = av[1]; - //write { FileStorage fs(filename, FileStorage::WRITE); diff --git a/samples/cpp/fitellipse.cpp b/samples/cpp/fitellipse.cpp index bd6b77d8a..b83f617c1 100644 --- a/samples/cpp/fitellipse.cpp +++ b/samples/cpp/fitellipse.cpp @@ -21,14 +21,14 @@ using namespace cv; using namespace std; -// static void help() -// { -// cout << -// "\nThis program is demonstration for ellipse fitting. The program finds\n" -// "contours and approximate it by ellipses.\n" -// "Call:\n" -// "./fitellipse [image_name -- Default ../data/stuff.jpg]\n" << endl; -// } +static void help() +{ + cout << + "\nThis program is demonstration for ellipse fitting. The program finds\n" + "contours and approximate it by ellipses.\n" + "Call:\n" + "./fitellipse [image_name -- Default ../data/stuff.jpg]\n" << endl; +} int sliderPos = 70; @@ -38,11 +38,19 @@ void processImage(int, void*); int main( int argc, char** argv ) { - const char* filename = argc == 2 ? argv[1] : (char*)"../data/stuff.jpg"; + cv::CommandLineParser parser(argc, argv, + "{help h||}{@image|../data/stuff.jpg|}" + ); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("@image"); image = imread(filename, 0); if( image.empty() ) { - cout << "Couldn't open image " << filename << "\nUsage: fitellipse \n"; + cout << "Couldn't open image " << filename << "\n"; return 0; } diff --git a/samples/cpp/grabcut.cpp b/samples/cpp/grabcut.cpp index 7ab28f66b..b6b406000 100644 --- a/samples/cpp/grabcut.cpp +++ b/samples/cpp/grabcut.cpp @@ -276,15 +276,16 @@ static void on_mouse( int event, int x, int y, int flags, void* param ) int main( int argc, char** argv ) { - if( argc!=2 ) + cv::CommandLineParser parser(argc, argv, "{help h||}{@input||}"); + if (parser.has("help")) { help(); - return 1; + return 0; } - string filename = argv[1]; + string filename = parser.get("@input"); if( filename.empty() ) { - cout << "\nDurn, couldn't read in " << argv[1] << endl; + cout << "\nDurn, empty filename" << endl; return 1; } Mat image = imread( filename, 1 ); diff --git a/samples/cpp/houghcircles.cpp b/samples/cpp/houghcircles.cpp index 34a2e6b7e..bdafffec3 100644 --- a/samples/cpp/houghcircles.cpp +++ b/samples/cpp/houghcircles.cpp @@ -16,8 +16,21 @@ static void help() int main(int argc, char** argv) { - const char* filename = argc >= 2 ? argv[1] : "../data/board.jpg"; - + cv::CommandLineParser parser(argc, argv, + "{help h ||}{@image|../data/board.jpg|}" + ); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("@image"); + if (filename.empty()) + { + help(); + cout << "no image_name provided" << endl; + return -1; + } Mat img = imread(filename, 0); if(img.empty()) { diff --git a/samples/cpp/houghlines.cpp b/samples/cpp/houghlines.cpp index ec825ab6b..ddeb3bd75 100644 --- a/samples/cpp/houghlines.cpp +++ b/samples/cpp/houghlines.cpp @@ -16,8 +16,21 @@ static void help() int main(int argc, char** argv) { - const char* filename = argc >= 2 ? argv[1] : "../data/pic1.png"; - + cv::CommandLineParser parser(argc, argv, + "{help h||}{@image|../data/pic1.png|}" + ); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("@image"); + if (filename.empty()) + { + help(); + cout << "no image_name provided" << endl; + return -1; + } Mat src = imread(filename, 0); if(src.empty()) { diff --git a/samples/cpp/image.cpp b/samples/cpp/image.cpp index a0e671812..b5925c69a 100644 --- a/samples/cpp/image.cpp +++ b/samples/cpp/image.cpp @@ -27,14 +27,19 @@ static void help() int main( int argc, char** argv ) { - help(); - const char* imagename = argc > 1 ? argv[1] : "../data/lena.jpg"; + cv::CommandLineParser parser(argc, argv, "{help h | |}{@image|../data/lena.jpg|}"); + if (parser.has("help")) + { + help(); + return 0; + } + string imagename = parser.get("@image"); #if DEMO_MIXED_API_USE //! [iplimage] - Ptr iplimg(cvLoadImage(imagename)); // Ptr is safe ref-counting pointer class + Ptr iplimg(cvLoadImage(imagename.c_str())); // Ptr is safe ref-counting pointer class if(!iplimg) { - fprintf(stderr, "Can not load image %s\n", imagename); + fprintf(stderr, "Can not load image %s\n", imagename.c_str()); return -1; } Mat img = cv::cvarrToMat(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert @@ -45,7 +50,7 @@ int main( int argc, char** argv ) Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function if(img.empty()) { - fprintf(stderr, "Can not load image %s\n", imagename); + fprintf(stderr, "Can not load image %s\n", imagename.c_str()); return -1; } #endif diff --git a/samples/cpp/image_alignment.cpp b/samples/cpp/image_alignment.cpp index 08814b84f..c62a1608d 100644 --- a/samples/cpp/image_alignment.cpp +++ b/samples/cpp/image_alignment.cpp @@ -53,6 +53,7 @@ const std::string keys = "{m motionType | affine | type of motion (translation, euclidean, affine, homography) }" "{v verbose | 0 | display initial and final images }" "{w warpedImfile | warpedECC.png | warped input image }" + "{h help | | print help message }" ; @@ -176,12 +177,17 @@ int main (const int argc, const char * argv[]) CommandLineParser parser(argc, argv, keys); parser.about("ECC demo"); - if (argc<2) { + if (argc < 2) { + parser.printMessage(); + help(); + return 1; + } + if (parser.has("help")) + { parser.printMessage(); help(); return 1; } - string imgFile = parser.get(0); string tempImgFile = parser.get(1); string inWarpFile = parser.get(2); @@ -192,7 +198,11 @@ int main (const int argc, const char * argv[]) int verbose = parser.get("v"); string finalWarp = parser.get("o"); string warpedImFile = parser.get("w"); - + if (!parser.check()) + { + parser.printErrors(); + return -1; + } if (!(warpType == "translation" || warpType == "euclidean" || warpType == "affine" || warpType == "homography")) { diff --git a/samples/cpp/image_sequence.cpp b/samples/cpp/image_sequence.cpp index b87b3043e..14b63e39c 100644 --- a/samples/cpp/image_sequence.cpp +++ b/samples/cpp/image_sequence.cpp @@ -18,13 +18,20 @@ static void help(char** argv) int main(int argc, char** argv) { - if(argc != 2) + cv::CommandLineParser parser(argc, argv, "{help h||}{@image||}"); + if (parser.has("help")) + { + help(argv); + return 0; + } + string first_file = parser.get("@image"); + + if(first_file.empty()) { help(argv); return 1; } - string first_file = argv[1]; VideoCapture sequence(first_file); if (!sequence.isOpened()) diff --git a/samples/cpp/imagelist_creator.cpp b/samples/cpp/imagelist_creator.cpp index f2abb11c9..bdb023187 100644 --- a/samples/cpp/imagelist_creator.cpp +++ b/samples/cpp/imagelist_creator.cpp @@ -23,14 +23,20 @@ static void help(char** av) int main(int ac, char** av) { - if (ac < 3) + cv::CommandLineParser parser(ac, av, "{help h||}{@output||}"); + if (parser.has("help")) + { + help(av); + return 0; + } + string outputname = parser.get("@output"); + + if (outputname.empty()) { help(av); return 1; } - string outputname = av[1]; - Mat m = imread(outputname); //check if the output is an image - prevent overwrites! if(!m.empty()){ std::cerr << "fail! Please specify an output file, don't want to overwrite you images!" << endl; diff --git a/samples/cpp/inpaint.cpp b/samples/cpp/inpaint.cpp index 55bd91b73..62d752193 100644 --- a/samples/cpp/inpaint.cpp +++ b/samples/cpp/inpaint.cpp @@ -47,7 +47,13 @@ static void onMouse( int event, int x, int y, int flags, void* ) int main( int argc, char** argv ) { - char* filename = argc >= 2 ? argv[1] : (char*)"../data/fruits.jpg"; + cv::CommandLineParser parser(argc, argv, "{help h||}{@image|../data/fruits.jpg|}"); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("@image"); Mat img0 = imread(filename, -1); if(img0.empty()) { @@ -55,8 +61,6 @@ int main( int argc, char** argv ) return 0; } - help(); - namedWindow( "image", 1 ); img = img0.clone(); diff --git a/samples/cpp/intelperc_capture.cpp b/samples/cpp/intelperc_capture.cpp index 5726ccdf4..daae4420f 100644 --- a/samples/cpp/intelperc_capture.cpp +++ b/samples/cpp/intelperc_capture.cpp @@ -9,14 +9,14 @@ using namespace cv; using namespace std; -static bool g_printStreamSetting = false; -static int g_imageStreamProfileIdx = -1; -static int g_depthStreamProfileIdx = -1; -static bool g_irStreamShow = false; -static double g_imageBrightness = -DBL_MAX; -static double g_imageContrast = -DBL_MAX; -static bool g_printTiming = false; -static bool g_showClosedPoint = false; +static bool g_printStreamSetting; +static int g_imageStreamProfileIdx; +static int g_depthStreamProfileIdx; +static bool g_irStreamShow; +static double g_imageBrightness; +static double g_imageContrast; +static bool g_printTiming; +static bool g_showClosedPoint; static int g_closedDepthPoint[2]; @@ -31,13 +31,13 @@ static void printUsage(const char *arg0) filename++; cout << "This program demonstrates usage of camera supported\nby Intel Perceptual computing SDK." << endl << endl; - cout << "usage: " << filename << "[-ps] [-isp IDX] [-dsp IDX]\n [-ir] [-imb VAL] [-imc VAL]" << endl << endl; + cout << "usage: " << filename << "[-ps] [-isp=IDX] [-dsp=IDX]\n [-ir] [-imb=VAL] [-imc=VAL]" << endl << endl; cout << " -ps, print streams setting and profiles" << endl; - cout << " -isp IDX, set profile index of the image stream" << endl; - cout << " -dsp IDX, set profile index of the depth stream" << endl; + cout << " -isp=IDX, set profile index of the image stream" << endl; + cout << " -dsp=IDX, set profile index of the depth stream" << endl; cout << " -ir, show data from IR stream" << endl; - cout << " -imb VAL, set brighness value for a image stream" << endl; - cout << " -imc VAL, set contrast value for a image stream" << endl; + cout << " -imb=VAL, set brighness value for a image stream" << endl; + cout << " -imc=VAL, set contrast value for a image stream" << endl; cout << " -pts, print frame index and frame time" << endl; cout << " --show-closed, print frame index and frame time" << endl; cout << endl; @@ -45,62 +45,40 @@ static void printUsage(const char *arg0) static void parseCMDLine(int argc, char* argv[]) { - if( argc == 1 ) + cv::CommandLineParser parser(argc, argv, + "{ h help | | }" + "{ ps print-streams | | }" + "{ isp image-stream-prof | -1 | }" + "{ dsp depth-stream-prof | -1 | }" + "{ir||}{imb||}{imc||}{pts||}{show-closed||}"); + if (parser.has("h")) { printUsage(argv[0]); + exit(0); } + g_printStreamSetting = parser.has("ps"); + g_imageStreamProfileIdx = parser.get("isp"); + g_depthStreamProfileIdx = parser.get("dsp"); + g_irStreamShow = parser.has("ir"); + if (parser.has("imb")) + g_imageBrightness = parser.get("imb"); else + g_imageBrightness = -DBL_MAX; + if (parser.has("imc")) + g_imageContrast = parser.get("imc"); + else + g_imageContrast = -DBL_MAX; + g_printTiming = parser.has("pts"); + g_showClosedPoint = parser.has("show-closed"); + if (!parser.check()) { - for( int i = 1; i < argc; i++ ) - { - if ((0 == strcmp(argv[i], "--help")) || (0 == strcmp( argv[i], "-h"))) - { - printUsage(argv[0]); - exit(0); - } - else if ((0 == strcmp( argv[i], "--print-streams")) || (0 == strcmp( argv[i], "-ps"))) - { - g_printStreamSetting = true; - } - else if ((0 == strcmp( argv[i], "--image-stream-prof")) || (0 == strcmp( argv[i], "-isp"))) - { - g_imageStreamProfileIdx = atoi(argv[++i]); - } - else if ((0 == strcmp( argv[i], "--depth-stream-prof")) || (0 == strcmp( argv[i], "-dsp"))) - { - g_depthStreamProfileIdx = atoi(argv[++i]); - } - else if (0 == strcmp( argv[i], "-ir")) - { - g_irStreamShow = true; - } - else if (0 == strcmp( argv[i], "-imb")) - { - g_imageBrightness = atof(argv[++i]); - } - else if (0 == strcmp( argv[i], "-imc")) - { - g_imageContrast = atof(argv[++i]); - } - else if (0 == strcmp(argv[i], "-pts")) - { - g_printTiming = true; - } - else if (0 == strcmp(argv[i], "--show-closed")) - { - g_showClosedPoint = true; - } - else - { - cout << "Unsupported command line argument: " << argv[i] << "." << endl; - exit(-1); - } - } - if (g_showClosedPoint && (-1 == g_depthStreamProfileIdx)) - { - cerr << "For --show-closed depth profile has be selected" << endl; - exit(-1); - } + parser.printErrors(); + exit(-1); + } + if (g_showClosedPoint && (-1 == g_depthStreamProfileIdx)) + { + cerr << "For --show-closed depth profile has be selected" << endl; + exit(-1); } } diff --git a/samples/cpp/laplace.cpp b/samples/cpp/laplace.cpp index bac432ca5..b33b49cbd 100644 --- a/samples/cpp/laplace.cpp +++ b/samples/cpp/laplace.cpp @@ -15,7 +15,7 @@ static void help() "\nThis program demonstrates Laplace point/edge detection using OpenCV function Laplacian()\n" "It captures from the camera of your choice: 0, 1, ... default 0\n" "Call:\n" - "./laplace [camera #, default 0]\n" << endl; + "./laplace -c= -p=\n" << endl; } enum {GAUSSIAN, BLUR, MEDIAN}; @@ -26,25 +26,31 @@ int smoothType = GAUSSIAN; int main( int argc, char** argv ) { VideoCapture cap; - help(); - - if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) - cap.open(argc == 2 ? argv[1][0] - '0' : 0); - else if( argc >= 2 ) + cv::CommandLineParser parser(argc, argv, "{help h | | }{ c | 0 | }{ p | | }"); + if ( parser.has("help") ) { - cap.open(argv[1]); - if( cap.isOpened() ) - cout << "Video " << argv[1] << - ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) << - ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) << - ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl; - if( argc > 2 && isdigit(argv[2][0]) ) + help(); + return 0; + } + if( parser.get("c").size() == 1 && isdigit(parser.get("c")[0]) ) + cap.open(parser.get("c")); + else + cap.open(parser.get("c")); + if( cap.isOpened() ) + cout << "Video " << parser.get("c") << + ": width=" << cap.get(CAP_PROP_FRAME_WIDTH) << + ", height=" << cap.get(CAP_PROP_FRAME_HEIGHT) << + ", nframes=" << cap.get(CAP_PROP_FRAME_COUNT) << endl; + if( parser.has("p") ) + { + int pos = parser.get("p"); + if (!parser.check()) { - int pos; - sscanf(argv[2], "%d", &pos); - cout << "seeking to frame #" << pos << endl; - cap.set(CAP_PROP_POS_FRAMES, pos); + parser.printErrors(); + return -1; } + cout << "seeking to frame #" << pos << endl; + cap.set(CAP_PROP_POS_FRAMES, pos); } if( !cap.isOpened() ) diff --git a/samples/cpp/letter_recog.cpp b/samples/cpp/letter_recog.cpp index 174e7f983..3d7e34ab7 100644 --- a/samples/cpp/letter_recog.cpp +++ b/samples/cpp/letter_recog.cpp @@ -28,9 +28,9 @@ static void help() "and the remaining 4000 (10000 for boosting) - to test the classifier.\n" "======================================================\n"); printf("\nThis is letter recognition sample.\n" - "The usage: letter_recog [-data ] \\\n" - " [-save ] \\\n" - " [-load ] \\\n" + "The usage: letter_recog [-data=] \\\n" + " [-save=] \\\n" + " [-load=] \\\n" " [-boost|-mlp|-knearest|-nbayes|-svm] # to use boost/mlp/knearest/SVM classifier instead of default Random Trees\n" ); } @@ -517,53 +517,32 @@ int main( int argc, char *argv[] ) { string filename_to_save = ""; string filename_to_load = ""; - string data_filename = "../data/letter-recognition.data"; + string data_filename; int method = 0; - int i; - for( i = 1; i < argc; i++ ) + cv::CommandLineParser parser(argc, argv, "{data|../data/letter-recognition.data|}{save||}{load||}{boost||}" + "{mlp||}{knn knearest||}{nbayes||}{svm||}{help h||}"); + data_filename = parser.get("data"); + if (parser.has("save")) + filename_to_save = parser.get("save"); + if (parser.has("load")) + filename_to_load = parser.get("load"); + if (parser.has("boost")) + method = 1; + else if (parser.has("mlp")) + method = 2; + else if (parser.has("knearest")) + method = 3; + else if (parser.has("nbayes")) + method = 4; + else if (parser.has("svm")) + method = 5; + if (parser.has("help")) { - if( strcmp(argv[i],"-data") == 0 ) // flag "-data letter_recognition.xml" - { - i++; - data_filename = argv[i]; - } - else if( strcmp(argv[i],"-save") == 0 ) // flag "-save filename.xml" - { - i++; - filename_to_save = argv[i]; - } - else if( strcmp(argv[i],"-load") == 0) // flag "-load filename.xml" - { - i++; - filename_to_load = argv[i]; - } - else if( strcmp(argv[i],"-boost") == 0) - { - method = 1; - } - else if( strcmp(argv[i],"-mlp") == 0 ) - { - method = 2; - } - else if( strcmp(argv[i], "-knearest") == 0 || strcmp(argv[i], "-knn") == 0 ) - { - method = 3; - } - else if( strcmp(argv[i], "-nbayes") == 0) - { - method = 4; - } - else if( strcmp(argv[i], "-svm") == 0) - { - method = 5; - } - else - break; + help(); + return 0; } - - if( i < argc || - (method == 0 ? + if( (method == 0 ? build_rtrees_classifier( data_filename, filename_to_save, filename_to_load ) : method == 1 ? build_boost_classifier( data_filename, filename_to_save, filename_to_load ) : diff --git a/samples/cpp/lkdemo.cpp b/samples/cpp/lkdemo.cpp index 2f576c3ba..3881aa865 100644 --- a/samples/cpp/lkdemo.cpp +++ b/samples/cpp/lkdemo.cpp @@ -37,8 +37,6 @@ static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ ) int main( int argc, char** argv ) { - help(); - VideoCapture cap; TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03); Size subPixWinSize(10,10), winSize(31,31); @@ -47,10 +45,19 @@ int main( int argc, char** argv ) bool needToInit = false; bool nightMode = false; - if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) - cap.open(argc == 2 ? argv[1][0] - '0' : 0); - else if( argc == 2 ) - cap.open(argv[1]); + cv::CommandLineParser parser(argc, argv, "{@input||}{help h||}"); + string input = parser.get("@input"); + if (parser.has("help")) + { + help(); + return 0; + } + if( input.empty() ) + cap.open(0); + else if( input.size() == 1 && isdigit(input[0]) ) + cap.open(input[0] - '0'); + else + cap.open(input); if( !cap.isOpened() ) { diff --git a/samples/cpp/lsd_lines.cpp b/samples/cpp/lsd_lines.cpp index 34f6b906b..4c8c7e0a4 100644 --- a/samples/cpp/lsd_lines.cpp +++ b/samples/cpp/lsd_lines.cpp @@ -13,15 +13,13 @@ using namespace cv; int main(int argc, char** argv) { std::string in; - if (argc != 2) + cv::CommandLineParser parser(argc, argv, "{@input|../data/building.jpg|input image}{help h||show help message}"); + if (parser.has("help")) { - std::cout << "Usage: lsd_lines [input image]. Now loading ../data/building.jpg" << std::endl; - in = "../data/building.jpg"; - } - else - { - in = argv[1]; + parser.printMessage(); + return 0; } + in = parser.get("@input"); Mat image = imread(in, IMREAD_GRAYSCALE); diff --git a/samples/cpp/mask_tmpl.cpp b/samples/cpp/mask_tmpl.cpp index 2b6bb77bf..661778326 100644 --- a/samples/cpp/mask_tmpl.cpp +++ b/samples/cpp/mask_tmpl.cpp @@ -13,16 +13,25 @@ static void help() { cout << "\nThis program demonstrates template match with mask.\n" "Usage:\n" - "./mask_tmpl , Default is ../data/lena_tmpl.jpg\n" + "./mask_tmpl -i= -t= -m=, Default is ../data/lena_tmpl.jpg\n" << endl; } int main( int argc, const char** argv ) { - const char* filename = argc == 4 ? argv[1] : "../data/lena_tmpl.jpg"; - const char* tmplname = argc == 4 ? argv[2] : "../data/tmpl.png"; - const char* maskname = argc == 4 ? argv[3] : "../data/mask.png"; - + cv::CommandLineParser parser(argc, argv, + "{help h||}" + "{ i | ../data/lena_tmpl.jpg | }" + "{ t | ../data/tmpl.png | }" + "{ m | ../data/mask.png | }"); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("i"); + string tmplname = parser.get("t"); + string maskname = parser.get("m"); Mat img = imread(filename); Mat tmpl = imread(tmplname); Mat mask = imread(maskname); diff --git a/samples/cpp/matchmethod_orb_akaze_brisk.cpp b/samples/cpp/matchmethod_orb_akaze_brisk.cpp index 4e3884d69..379b53ae1 100644 --- a/samples/cpp/matchmethod_orb_akaze_brisk.cpp +++ b/samples/cpp/matchmethod_orb_akaze_brisk.cpp @@ -9,7 +9,7 @@ static void help() { cout << "\n This program demonstrates how to detect compute and match ORB BRISK and AKAZE descriptors \n" "Usage: \n" - " ./matchmethod_orb_akaze_brisk \n" + " ./matchmethod_orb_akaze_brisk --image1= --image2=\n" "Press a key when image window is active to change algorithm or descriptor"; } @@ -20,7 +20,6 @@ int main(int argc, char *argv[]) vector typeDesc; vector typeAlgoMatch; vector fileName; - help(); // This descriptor are going to be detect and compute typeDesc.push_back("AKAZE-DESCRIPTOR_KAZE_UPRIGHT"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html typeDesc.push_back("AKAZE"); // see http://docs.opencv.org/trunk/d8/d30/classcv_1_1AKAZE.html @@ -31,21 +30,17 @@ int main(int argc, char *argv[]) typeAlgoMatch.push_back("BruteForce-L1"); typeAlgoMatch.push_back("BruteForce-Hamming"); typeAlgoMatch.push_back("BruteForce-Hamming(2)"); - if (argc==1) - { - fileName.push_back("../data/basketball1.png"); - fileName.push_back("../data/basketball2.png"); - } - else if (argc==3) - { - fileName.push_back(argv[1]); - fileName.push_back(argv[2]); - } - else + cv::CommandLineParser parser(argc, argv, + "{ @image1 | ../data/basketball1.png | }" + "{ @image2 | ../data/basketball2.png | }" + "{help h ||}"); + if (parser.has("help")) { help(); - return(0); + return 0; } + fileName.push_back(parser.get(0)); + fileName.push_back(parser.get(1)); Mat img1 = imread(fileName[0], IMREAD_GRAYSCALE); Mat img2 = imread(fileName[1], IMREAD_GRAYSCALE); if (img1.rows*img1.cols <= 0) diff --git a/samples/cpp/morphology2.cpp b/samples/cpp/morphology2.cpp index 27eb52aee..04e916c64 100644 --- a/samples/cpp/morphology2.cpp +++ b/samples/cpp/morphology2.cpp @@ -3,6 +3,7 @@ #include "opencv2/highgui/highgui.hpp" #include #include +#include using namespace cv; @@ -58,11 +59,18 @@ static void ErodeDilate(int, void*) int main( int argc, char** argv ) { - char* filename = argc == 2 ? argv[1] : (char*)"../data/baboon.jpg"; + cv::CommandLineParser parser(argc, argv, "{help h||}{ @image | ../data/baboon.jpg | }"); + if (parser.has("help")) + { + help(); + return 0; + } + std::string filename = parser.get("@image"); if( (src = imread(filename,1)).empty() ) + { + help(); return -1; - - help(); + } //create windows for output images namedWindow("Open/Close",1); diff --git a/samples/cpp/npr_demo.cpp b/samples/cpp/npr_demo.cpp index e4fade0d8..64a3e0538 100644 --- a/samples/cpp/npr_demo.cpp +++ b/samples/cpp/npr_demo.cpp @@ -28,15 +28,21 @@ using namespace cv; int main(int argc, char* argv[]) { - if(argc < 2) + cv::CommandLineParser parser(argc, argv, "{help h||show help message}{@image||input image}"); + if (parser.has("help")) { - cout << "usage: " << argv[0] << " " << endl; + parser.printMessage(); + exit(0); + } + if (parser.get("@image").empty()) + { + parser.printMessage(); exit(0); } - int num,type; + Mat I = imread(parser.get("@image")); - Mat I = imread(argv[1]); + int num,type; if(I.empty()) { diff --git a/samples/cpp/openni_capture.cpp b/samples/cpp/openni_capture.cpp index 09f1d21e0..70d4a7c61 100644 --- a/samples/cpp/openni_capture.cpp +++ b/samples/cpp/openni_capture.cpp @@ -87,90 +87,49 @@ static float getMaxDisparity( VideoCapture& capture ) static void printCommandLineParams() { - cout << "-cd Colorized disparity? (0 or 1; 1 by default) Ignored if disparity map is not selected to show." << endl; - cout << "-fmd Fixed max disparity? (0 or 1; 0 by default) Ignored if disparity map is not colorized (-cd 0)." << endl; - cout << "-mode image mode: resolution and fps, supported three values: 0 - CAP_OPENNI_VGA_30HZ, 1 - CAP_OPENNI_SXGA_15HZ," << endl; + cout << "-cd= Colorized disparity? (0 or 1; 1 by default) Ignored if disparity map is not selected to show." << endl; + cout << "-fmd= Fixed max disparity? (0 or 1; 0 by default) Ignored if disparity map is not colorized (-cd 0)." << endl; + cout << "-mode= image mode: resolution and fps, supported three values: 0 - CAP_OPENNI_VGA_30HZ, 1 - CAP_OPENNI_SXGA_15HZ," << endl; cout << " 2 - CAP_OPENNI_SXGA_30HZ (0 by default). Ignored if rgb image or gray image are not selected to show." << endl; - cout << "-m Mask to set which output images are need. It is a string of size 5. Each element of this is '0' or '1' and" << endl; + cout << "-m= Mask to set which output images are need. It is a string of size 5. Each element of this is '0' or '1' and" << endl; cout << " determine: is depth map, disparity map, valid pixels mask, rgb image, gray image need or not (correspondently)?" << endl ; - cout << " By default -m 01010 i.e. disparity map and rgb image will be shown." << endl ; - cout << "-r Filename of .oni video file. The data will grabbed from it." << endl ; + cout << " By default -m=01010 i.e. disparity map and rgb image will be shown." << endl ; + cout << "-r= Filename of .oni video file. The data will grabbed from it." << endl ; } static void parseCommandLine( int argc, char* argv[], bool& isColorizeDisp, bool& isFixedMaxDisp, int& imageMode, bool retrievedImageFlags[], string& filename, bool& isFileReading ) { - // set defaut values - isColorizeDisp = true; - isFixedMaxDisp = false; - imageMode = 0; - - retrievedImageFlags[0] = false; - retrievedImageFlags[1] = true; - retrievedImageFlags[2] = false; - retrievedImageFlags[3] = true; - retrievedImageFlags[4] = false; - filename.clear(); - isFileReading = false; - - if( argc == 1 ) + cv::CommandLineParser parser(argc, argv, "{h help||}{cd|1|}{fmd|0|}{mode|0|}{m|01010|}{r||}"); + if (parser.has("h")) { help(); + printCommandLineParams(); + exit(0); } - else + isColorizeDisp = (parser.get("cd") != 0); + isFixedMaxDisp = (parser.get("fmd") != 0); + imageMode = parser.get("mode"); + int flags = parser.get("m"); + isFileReading = parser.has("r"); + if (isFileReading) + filename = parser.get("r"); + if (!parser.check()) { - for( int i = 1; i < argc; i++ ) - { - if( !strcmp( argv[i], "--help" ) || !strcmp( argv[i], "-h" ) ) - { - printCommandLineParams(); - exit(0); - } - else if( !strcmp( argv[i], "-cd" ) ) - { - isColorizeDisp = atoi(argv[++i]) == 0 ? false : true; - } - else if( !strcmp( argv[i], "-fmd" ) ) - { - isFixedMaxDisp = atoi(argv[++i]) == 0 ? false : true; - } - else if( !strcmp( argv[i], "-mode" ) ) - { - imageMode = atoi(argv[++i]); - } - else if( !strcmp( argv[i], "-m" ) ) - { - string mask( argv[++i] ); - if( mask.size() != 5) - CV_Error( Error::StsBadArg, "Incorrect length of -m argument string" ); - int val = atoi(mask.c_str()); - - int l = 100000, r = 10000, sum = 0; - for( int j = 0; j < 5; j++ ) - { - retrievedImageFlags[j] = ((val % l) / r ) == 0 ? false : true; - l /= 10; r /= 10; - if( retrievedImageFlags[j] ) sum++; - } - - if( sum == 0 ) - { - cout << "No one output image is selected." << endl; - exit(0); - } - } - else if( !strcmp( argv[i], "-r" ) ) - { - filename = argv[++i]; - isFileReading = true; - } - else - { - cout << "Unsupported command line argument: " << argv[i] << "." << endl; - exit(-1); - } - } + parser.printErrors(); + help(); + exit(-1); + } + if (flags % 100000 == 0) + { + cout << "No one output image is selected." << endl; + exit(0); + } + for (int i = 0; i < 5; i++) + { + retrievedImageFlags[4 - i] = (flags % 10 != 0); + flags /= 10; } } diff --git a/samples/cpp/pca.cpp b/samples/cpp/pca.cpp index db5fb3802..35fd0c1a0 100644 --- a/samples/cpp/pca.cpp +++ b/samples/cpp/pca.cpp @@ -121,14 +121,20 @@ static void onTrackbar(int pos, void* ptr) // Main int main(int argc, char** argv) { - if (argc != 2) { - cout << "usage: " << argv[0] << " " << endl; + cv::CommandLineParser parser(argc, argv, "{@input||image list}{help h||show help message}"); + if (parser.has("help")) + { + parser.printMessage(); + exit(0); + } + // Get the path to your CSV. + string imgList = parser.get("@input"); + if (imgList.empty()) + { + parser.printMessage(); exit(1); } - // Get the path to your CSV. - string imgList = string(argv[1]); - // vector to hold the images vector images; diff --git a/samples/cpp/polar_transforms.cpp b/samples/cpp/polar_transforms.cpp index 3e2810e74..872dda8c3 100644 --- a/samples/cpp/polar_transforms.cpp +++ b/samples/cpp/polar_transforms.cpp @@ -1,6 +1,7 @@ #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/videoio/videoio_c.h" #include "opencv2/highgui/highgui_c.h" +#include "opencv2/core/utility.hpp" #include #include @@ -20,15 +21,22 @@ int main( int argc, char** argv ) IplImage* recovered_img = 0; help(); - - if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) - capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 ); - else if( argc == 2 ) - capture = cvCaptureFromAVI( argv[1] ); + cv::CommandLineParser parser(argc, argv, "{help h||}{@input|0|}"); + if (parser.has("help")) + { + help(); + return 0; + } + std::string arg = parser.get("@input"); + if( arg.size() == 1 && isdigit(arg[0]) ) + capture = cvCaptureFromCAM( arg[0] - '0' ); + else + capture = cvCaptureFromAVI( arg.c_str() ); if( !capture ) { + const char* name = argv[0]; fprintf(stderr,"Could not initialize capturing...\n"); - fprintf(stderr,"Usage: %s , or \n %s \n",argv[0],argv[0]); + fprintf(stderr,"Usage: %s , or \n %s \n", name, name); help(); return -1; } diff --git a/samples/cpp/segment_objects.cpp b/samples/cpp/segment_objects.cpp index 72afbdeed..3c217f679 100644 --- a/samples/cpp/segment_objects.cpp +++ b/samples/cpp/segment_objects.cpp @@ -63,12 +63,17 @@ int main(int argc, char** argv) VideoCapture cap; bool update_bg_model = true; - help(); - - if( argc < 2 ) + CommandLineParser parser(argc, argv, "{help h||}{@input||}"); + if (parser.has("help")) + { + help(); + return 0; + } + string input = parser.get("@input"); + if (input.empty()) cap.open(0); else - cap.open(std::string(argv[1])); + cap.open(input); if( !cap.isOpened() ) { diff --git a/samples/cpp/select3dobj.cpp b/samples/cpp/select3dobj.cpp index ed598f2a1..b13697fec 100644 --- a/samples/cpp/select3dobj.cpp +++ b/samples/cpp/select3dobj.cpp @@ -30,14 +30,14 @@ const char* helphelp = "compute the homography of the plane the calibration pattern is on. It also shows grabCut\n" "segmentation etc.\n" "\n" -"select3dobj -w -h [-s ]\n" -" -i -o [video_filename/cameraId]\n" +"select3dobj -w= -h= [-s=]\n" +" -i= -o=\n" "\n" -" -w Number of chessboard corners wide\n" -" -h Number of chessboard corners width\n" -" [-s ] Optional measure of chessboard squares in meters\n" -" -i Camera matrix .yml file from calibration.cpp\n" -" -o Prefix the output segmentation images with this\n" +" -w= Number of chessboard corners wide\n" +" -h= Number of chessboard corners width\n" +" [-s=] Optional measure of chessboard squares in meters\n" +" -i= Camera matrix .yml file from calibration.cpp\n" +" -o= Prefix the output segmentation images with this\n" " [video_filename/cameraId] If present, read from that video file or that ID\n" "\n" "Using a camera's intrinsics (from calibrating a camera -- see calibration.cpp) and an\n" @@ -384,8 +384,8 @@ static bool readStringList( const string& filename, vector& l ) int main(int argc, char** argv) { - const char* help = "Usage: select3dobj -w -h [-s ]\n" - "\t-i -o [video_filename/cameraId]\n"; + const char* help = "Usage: select3dobj -w= -h= [-s=]\n" + "\t-i= -o= [video_filename/cameraId]\n"; const char* screen_help = "Actions: \n" "\tSelect object as 3D box with the mouse. That's it\n" @@ -394,82 +394,59 @@ int main(int argc, char** argv) "\tENTER - Confirm the selection. Grab next object in video mode.\n" "\tq - Exit the program\n"; - if(argc < 5) + cv::CommandLineParser parser(argc, argv, "{help h||}{w||}{h||}{s|1|}{i||}{o||}{@input|0|}"); + if (parser.has("help")) { puts(helphelp); puts(help); return 0; } - const char* intrinsicsFilename = 0; - const char* outprefix = 0; - const char* inputName = 0; + string intrinsicsFilename; + string outprefix = ""; + string inputName = ""; int cameraId = 0; Size boardSize; - double squareSize = 1; + double squareSize; vector imageList; - - for( int i = 1; i < argc; i++ ) + intrinsicsFilename = parser.get("i"); + outprefix = parser.get("o"); + boardSize.width = parser.get("w"); + boardSize.height = parser.get("h"); + squareSize = parser.get("s"); + if ( parser.get("@input").size() == 1 && isdigit(parser.get("@input")[0]) ) + cameraId = parser.get("@input"); + else + inputName = parser.get("@input"); + if (!parser.check()) { - if( strcmp(argv[i], "-i") == 0 ) - intrinsicsFilename = argv[++i]; - else if( strcmp(argv[i], "-o") == 0 ) - outprefix = argv[++i]; - else if( strcmp(argv[i], "-w") == 0 ) - { - if(sscanf(argv[++i], "%d", &boardSize.width) != 1 || boardSize.width <= 0) - { - printf("Incorrect -w parameter (must be a positive integer)\n"); - puts(help); - return 0; - } - } - else if( strcmp(argv[i], "-h") == 0 ) - { - if(sscanf(argv[++i], "%d", &boardSize.height) != 1 || boardSize.height <= 0) - { - printf("Incorrect -h parameter (must be a positive integer)\n"); - puts(help); - return 0; - } - } - else if( strcmp(argv[i], "-s") == 0 ) - { - if(sscanf(argv[++i], "%lf", &squareSize) != 1 || squareSize <= 0) - { - printf("Incorrect -w parameter (must be a positive real number)\n"); - puts(help); - return 0; - } - } - else if( argv[i][0] != '-' ) - { - if( isdigit(argv[i][0])) - sscanf(argv[i], "%d", &cameraId); - else - inputName = argv[i]; - } - else - { - printf("Incorrect option\n"); - puts(help); - return 0; - } + puts(help); + parser.printErrors(); + return 0; } - - if( !intrinsicsFilename || !outprefix || - boardSize.width <= 0 || boardSize.height <= 0 ) + if ( boardSize.width <= 0 ) { - printf("Some of the required parameters are missing\n"); + printf("Incorrect -w parameter (must be a positive integer)\n"); + puts(help); + return 0; + } + if ( boardSize.height <= 0 ) + { + printf("Incorrect -h parameter (must be a positive integer)\n"); + puts(help); + return 0; + } + if ( squareSize <= 0 ) + { + printf("Incorrect -s parameter (must be a positive real number)\n"); puts(help); return 0; } - Mat cameraMatrix, distCoeffs; Size calibratedImageSize; readCameraMatrix(intrinsicsFilename, cameraMatrix, distCoeffs, calibratedImageSize ); VideoCapture capture; - if( inputName ) + if( !inputName.empty() ) { if( !readStringList(inputName, imageList) && !capture.open(inputName)) @@ -486,21 +463,21 @@ int main(int argc, char** argv) const char* outbarename = 0; { - outbarename = strrchr(outprefix, '/'); - const char* tmp = strrchr(outprefix, '\\'); + outbarename = strrchr(outprefix.c_str(), '/'); + const char* tmp = strrchr(outprefix.c_str(), '\\'); char cmd[1000]; - sprintf(cmd, "mkdir %s", outprefix); + sprintf(cmd, "mkdir %s", outprefix.c_str()); if( tmp && tmp > outbarename ) outbarename = tmp; if( outbarename ) { - cmd[6 + outbarename - outprefix] = '\0'; + cmd[6 + outbarename - outprefix.c_str()] = '\0'; int result = system(cmd); CV_Assert(result == 0); outbarename++; } else - outbarename = outprefix; + outbarename = outprefix.c_str(); } Mat frame, shownFrame, selectedObjFrame, mapxy; @@ -510,7 +487,7 @@ int main(int argc, char** argv) setMouseCallback("View", onMouse, 0); bool boardFound = false; - string indexFilename = format("%s_index.yml", outprefix); + string indexFilename = format("%s_index.yml", outprefix.c_str()); vector capturedImgList; vector roiList; @@ -588,7 +565,7 @@ int main(int argc, char** argv) char path[1000]; for(;frameIdx < maxFrameIdx;frameIdx++) { - sprintf(path, "%s%04d.jpg", outprefix, frameIdx); + sprintf(path, "%s%04d.jpg", outprefix.c_str(), frameIdx); FILE* f = fopen(path, "rb"); if( !f ) break; @@ -596,7 +573,7 @@ int main(int argc, char** argv) } if( frameIdx == maxFrameIdx ) { - printf("Can not save the image as %s<...>.jpg", outprefix); + printf("Can not save the image as %s<...>.jpg", outprefix.c_str()); break; } imwrite(path, selectedObjFrame(r)); diff --git a/samples/cpp/shape_example.cpp b/samples/cpp/shape_example.cpp index e1c925f8c..cb6cfbeef 100644 --- a/samples/cpp/shape_example.cpp +++ b/samples/cpp/shape_example.cpp @@ -19,7 +19,7 @@ static void help() "This program demonstrates a method for shape comparisson based on Shape Context\n" "You should run the program providing a number between 1 and 20 for selecting an image in the folder ../data/shape_sample.\n" "Call\n" - "./shape_example [number between 1 and 20]\n\n"); + "./shape_example [number between 1 and 20, 1 default]\n\n"); } static vector simpleContour( const Mat& currentQuery, int n=300 ) @@ -54,16 +54,24 @@ static vector simpleContour( const Mat& currentQuery, int n=300 ) int main(int argc, char** argv) { - help(); string path = "../data/shape_sample/"; - int indexQuery = 1; - if( argc < 2 ) + cv::CommandLineParser parser(argc, argv, "{help h||}{@input|1|}"); + if (parser.has("help")) { - std::cout<<"Using first image as query."<("@input"); + if (!parser.check()) { - sscanf( argv[1], "%i", &indexQuery ); + parser.printErrors(); + help(); + return 1; + } + if (indexQuery < 1 || indexQuery > 20) + { + help(); + return 1; } cv::Ptr mysc = cv::createShapeContextDistanceExtractor(); diff --git a/samples/cpp/smiledetect.cpp b/samples/cpp/smiledetect.cpp index d15183fb0..f3f85764f 100644 --- a/samples/cpp/smiledetect.cpp +++ b/samples/cpp/smiledetect.cpp @@ -25,61 +25,42 @@ void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip ); -string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml"; -string nestedCascadeName = "../../data/haarcascades/haarcascade_smile.xml"; +string cascadeName; +string nestedCascadeName; int main( int argc, const char** argv ) { VideoCapture capture; Mat frame, image; - const string scaleOpt = "--scale="; - size_t scaleOptLen = scaleOpt.length(); - const string cascadeOpt = "--cascade="; - size_t cascadeOptLen = cascadeOpt.length(); - const string nestedCascadeOpt = "--smile-cascade"; - size_t nestedCascadeOptLen = nestedCascadeOpt.length(); - const string tryFlipOpt = "--try-flip"; - size_t tryFlipOptLen = tryFlipOpt.length(); string inputName; - bool tryflip = false; + bool tryflip; help(); CascadeClassifier cascade, nestedCascade; - double scale = 1; - - for( int i = 1; i < argc; i++ ) + double scale; + cv::CommandLineParser parser(argc, argv, + "{help h||}{scale|1|}" + "{cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}" + "{smile-cascade|../../data/haarcascades/haarcascade_smile.xml|}" + "{try-flip||}{@input||}"); + if (parser.has("help")) { - cout << "Processing " << i << " " << argv[i] << endl; - if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 ) - { - cascadeName.assign( argv[i] + cascadeOptLen ); - cout << " from which we have cascadeName= " << cascadeName << endl; - } - else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 ) - { - if( argv[i][nestedCascadeOpt.length()] == '=' ) - nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 ); - } - else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 ) - { - if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 ) - scale = 1; - cout << " from which we read scale = " << scale << endl; - } - else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 ) - { - tryflip = true; - cout << " will try to flip image horizontally to detect assymetric objects\n"; - } - else if( argv[i][0] == '-' ) - { - cerr << "WARNING: Unknown option " << argv[i] << endl; - } - else - inputName.assign( argv[i] ); + help(); + return 0; } - + cascadeName = parser.get("cascade"); + nestedCascadeName = parser.get("smile-cascade"); + tryflip = parser.has("try-flip"); + inputName = parser.get("@input"); + scale = parser.get("scale"); + if (!parser.check()) + { + help(); + return 1; + } + if (scale < 1) + scale = 1; if( !cascade.load( cascadeName ) ) { cerr << "ERROR: Could not load face cascade" << endl; @@ -92,10 +73,9 @@ int main( int argc, const char** argv ) help(); return -1; } - - if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') ) + if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) ) { - int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0' ; + int c = inputName.empty() ? 0 : inputName[0] - '0' ; if(!capture.open(c)) cout << "Capture from camera #" << c << " didn't work" << endl; } diff --git a/samples/cpp/starter_imagelist.cpp b/samples/cpp/starter_imagelist.cpp index a576030b9..89df0cc1b 100644 --- a/samples/cpp/starter_imagelist.cpp +++ b/samples/cpp/starter_imagelist.cpp @@ -62,13 +62,18 @@ int process(vector images) int main(int ac, char** av) { - - if (ac != 2) + cv::CommandLineParser parser(ac, av, "{help h||}{@input||}"); + if (parser.has("help")) + { + help(av); + return 0; + } + std::string arg = parser.get("@input"); + if (arg.empty()) { help(av); return 1; } - std::string arg = av[1]; vector imagelist; if (!readStringList(arg,imagelist)) diff --git a/samples/cpp/starter_video.cpp b/samples/cpp/starter_video.cpp index 34f23d376..d6bf1b766 100644 --- a/samples/cpp/starter_video.cpp +++ b/samples/cpp/starter_video.cpp @@ -71,12 +71,17 @@ namespace { } int main(int ac, char** av) { - - if (ac != 2) { + cv::CommandLineParser parser(ac, av, "{help h||}{@input||}"); + if (parser.has("help")) + { + help(av); + return 0; + } + std::string arg = parser.get("@input"); + if (arg.empty()) { help(av); return 1; } - std::string arg = av[1]; VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param capture.open(atoi(arg.c_str())); diff --git a/samples/cpp/stereo_calib.cpp b/samples/cpp/stereo_calib.cpp index 12ed3ef1e..117c9bac5 100644 --- a/samples/cpp/stereo_calib.cpp +++ b/samples/cpp/stereo_calib.cpp @@ -50,7 +50,7 @@ static int print_help() " matrix separately) stereo. \n" " Calibrate the cameras and display the\n" " rectified results along with the computed disparity images. \n" << endl; - cout << "Usage:\n ./stereo_calib -w board_width -h board_height [-nr /*dot not view results*/] \n" << endl; + cout << "Usage:\n ./stereo_calib -w= -h= \n" << endl; return 0; } @@ -347,50 +347,19 @@ int main(int argc, char** argv) { Size boardSize; string imagelistfn; - bool showRectified = true; - - for( int i = 1; i < argc; i++ ) + bool showRectified; + cv::CommandLineParser parser(argc, argv, "{w|9|}{h|6|}{nr||}{help||}{@input|../data/stereo_calib.xml|}"); + if (parser.has("help")) + return print_help(); + showRectified = !parser.has("nr"); + imagelistfn = parser.get("@input"); + boardSize.width = parser.get("w"); + boardSize.height = parser.get("h"); + if (!parser.check()) { - if( string(argv[i]) == "-w" ) - { - if( sscanf(argv[++i], "%d", &boardSize.width) != 1 || boardSize.width <= 0 ) - { - cout << "invalid board width" << endl; - return print_help(); - } - } - else if( string(argv[i]) == "-h" ) - { - if( sscanf(argv[++i], "%d", &boardSize.height) != 1 || boardSize.height <= 0 ) - { - cout << "invalid board height" << endl; - return print_help(); - } - } - else if( string(argv[i]) == "-nr" ) - showRectified = false; - else if( string(argv[i]) == "--help" ) - return print_help(); - else if( argv[i][0] == '-' ) - { - cout << "invalid option " << argv[i] << endl; - return 0; - } - else - imagelistfn = argv[i]; + parser.printErrors(); + return 1; } - - if( imagelistfn == "" ) - { - imagelistfn = "../data/stereo_calib.xml"; - boardSize = Size(9, 6); - } - else if( boardSize.width <= 0 || boardSize.height <= 0 ) - { - cout << "if you specified XML file with chessboards, you should also specify the board width and height (-w and -h options)" << endl; - return 0; - } - vector imagelist; bool ok = readStringList(imagelistfn, imagelist); if(!ok || imagelist.empty()) diff --git a/samples/cpp/stereo_match.cpp b/samples/cpp/stereo_match.cpp index 3a2943afe..e88b139a1 100644 --- a/samples/cpp/stereo_match.cpp +++ b/samples/cpp/stereo_match.cpp @@ -21,8 +21,8 @@ static void print_help() { printf("\nDemo stereo matching converting L and R images into disparity and point clouds\n"); printf("\nUsage: stereo_match [--algorithm=bm|sgbm|hh|sgbm3way] [--blocksize=]\n" - "[--max-disparity=] [--scale=scale_factor>] [-i ] [-e ]\n" - "[--no-display] [-o ] [-p ]\n"); + "[--max-disparity=] [--scale=scale_factor>] [-i=] [-e=]\n" + "[--no-display] [-o=] [-p=]\n"); } static void saveXYZ(const char* filename, const Mat& mat) @@ -43,114 +43,90 @@ static void saveXYZ(const char* filename, const Mat& mat) int main(int argc, char** argv) { - const char* algorithm_opt = "--algorithm="; - const char* maxdisp_opt = "--max-disparity="; - const char* blocksize_opt = "--blocksize="; - const char* nodisplay_opt = "--no-display"; - const char* scale_opt = "--scale="; + std::string img1_filename = ""; + std::string img2_filename = ""; + std::string intrinsic_filename = ""; + std::string extrinsic_filename = ""; + std::string disparity_filename = ""; + std::string point_cloud_filename = ""; - if(argc < 3) + enum { STEREO_BM=0, STEREO_SGBM=1, STEREO_HH=2, STEREO_VAR=3, STEREO_3WAY=4 }; + int alg = STEREO_SGBM; + int SADWindowSize, numberOfDisparities; + bool no_display; + float scale; + + Ptr bm = StereoBM::create(16,9); + Ptr sgbm = StereoSGBM::create(0,16,3); + cv::CommandLineParser parser(argc, argv, + "{@arg1||}{@arg2||}{help h||}{algorithm||}{max-disparity|0|}{blocksize|0|}{no-display||}{scale|1|}{i||}{e||}{o||}{p||}"); + if(parser.has("help")) { print_help(); return 0; } - const char* img1_filename = 0; - const char* img2_filename = 0; - const char* intrinsic_filename = 0; - const char* extrinsic_filename = 0; - const char* disparity_filename = 0; - const char* point_cloud_filename = 0; - - enum { STEREO_BM=0, STEREO_SGBM=1, STEREO_HH=2, STEREO_VAR=3, STEREO_3WAY=4 }; - int alg = STEREO_SGBM; - int SADWindowSize = 0, numberOfDisparities = 0; - bool no_display = false; - float scale = 1.f; - - Ptr bm = StereoBM::create(16,9); - Ptr sgbm = StereoSGBM::create(0,16,3); - - for( int i = 1; i < argc; i++ ) + img1_filename = parser.get(0); + img2_filename = parser.get(1); + if (parser.has("algorithm")) { - if( argv[i][0] != '-' ) - { - if( !img1_filename ) - img1_filename = argv[i]; - else - img2_filename = argv[i]; - } - else if( strncmp(argv[i], algorithm_opt, strlen(algorithm_opt)) == 0 ) - { - char* _alg = argv[i] + strlen(algorithm_opt); - alg = strcmp(_alg, "bm") == 0 ? STEREO_BM : - strcmp(_alg, "sgbm") == 0 ? STEREO_SGBM : - strcmp(_alg, "hh") == 0 ? STEREO_HH : - strcmp(_alg, "var") == 0 ? STEREO_VAR : - strcmp(_alg, "sgbm3way") == 0 ? STEREO_3WAY : -1; - if( alg < 0 ) - { - printf("Command-line parameter error: Unknown stereo algorithm\n\n"); - print_help(); - return -1; - } - } - else if( strncmp(argv[i], maxdisp_opt, strlen(maxdisp_opt)) == 0 ) - { - if( sscanf( argv[i] + strlen(maxdisp_opt), "%d", &numberOfDisparities ) != 1 || - numberOfDisparities < 1 || numberOfDisparities % 16 != 0 ) - { - printf("Command-line parameter error: The max disparity (--maxdisparity=<...>) must be a positive integer divisible by 16\n"); - print_help(); - return -1; - } - } - else if( strncmp(argv[i], blocksize_opt, strlen(blocksize_opt)) == 0 ) - { - if( sscanf( argv[i] + strlen(blocksize_opt), "%d", &SADWindowSize ) != 1 || - SADWindowSize < 1 || SADWindowSize % 2 != 1 ) - { - printf("Command-line parameter error: The block size (--blocksize=<...>) must be a positive odd number\n"); - return -1; - } - } - else if( strncmp(argv[i], scale_opt, strlen(scale_opt)) == 0 ) - { - if( sscanf( argv[i] + strlen(scale_opt), "%f", &scale ) != 1 || scale < 0 ) - { - printf("Command-line parameter error: The scale factor (--scale=<...>) must be a positive floating-point number\n"); - return -1; - } - } - else if( strcmp(argv[i], nodisplay_opt) == 0 ) - no_display = true; - else if( strcmp(argv[i], "-i" ) == 0 ) - intrinsic_filename = argv[++i]; - else if( strcmp(argv[i], "-e" ) == 0 ) - extrinsic_filename = argv[++i]; - else if( strcmp(argv[i], "-o" ) == 0 ) - disparity_filename = argv[++i]; - else if( strcmp(argv[i], "-p" ) == 0 ) - point_cloud_filename = argv[++i]; - else - { - printf("Command-line parameter error: unknown option %s\n", argv[i]); - return -1; - } + std::string _alg = parser.get("algorithm"); + alg = _alg == "bm" ? STEREO_BM : + _alg == "sgbm" ? STEREO_SGBM : + _alg == "hh" ? STEREO_HH : + _alg == "var" ? STEREO_VAR : + _alg == "sgbm3way" ? STEREO_3WAY : -1; } - - if( !img1_filename || !img2_filename ) + numberOfDisparities = parser.get("max-disparity"); + SADWindowSize = parser.get("blocksize"); + scale = parser.get("scale"); + no_display = parser.has("no-display"); + if( parser.has("i") ) + intrinsic_filename = parser.get("i"); + if( parser.has("e") ) + extrinsic_filename = parser.get("e"); + if( parser.has("o") ) + disparity_filename = parser.get("o"); + if( parser.has("p") ) + point_cloud_filename = parser.get("p"); + if (!parser.check()) + { + parser.printErrors(); + return 1; + } + if( alg < 0 ) + { + printf("Command-line parameter error: Unknown stereo algorithm\n\n"); + print_help(); + return -1; + } + if ( numberOfDisparities < 1 || numberOfDisparities % 16 != 0 ) + { + printf("Command-line parameter error: The max disparity (--maxdisparity=<...>) must be a positive integer divisible by 16\n"); + print_help(); + return -1; + } + if (scale < 0) + { + printf("Command-line parameter error: The scale factor (--scale=<...>) must be a positive floating-point number\n"); + return -1; + } + if (SADWindowSize < 1 || SADWindowSize % 2 != 1) + { + printf("Command-line parameter error: The block size (--blocksize=<...>) must be a positive odd number\n"); + return -1; + } + if( img1_filename.empty() || img2_filename.empty() ) { printf("Command-line parameter error: both left and right images must be specified\n"); return -1; } - - if( (intrinsic_filename != 0) ^ (extrinsic_filename != 0) ) + if( (!intrinsic_filename.empty()) ^ (!extrinsic_filename.empty()) ) { printf("Command-line parameter error: either both intrinsic and extrinsic parameters must be specified, or none of them (when the stereo pair is already rectified)\n"); return -1; } - if( extrinsic_filename == 0 && point_cloud_filename ) + if( extrinsic_filename.empty() && !point_cloud_filename.empty() ) { printf("Command-line parameter error: extrinsic and intrinsic parameters must be specified to compute the point cloud\n"); return -1; @@ -186,13 +162,13 @@ int main(int argc, char** argv) Rect roi1, roi2; Mat Q; - if( intrinsic_filename ) + if( !intrinsic_filename.empty() ) { // reading intrinsic parameters FileStorage fs(intrinsic_filename, FileStorage::READ); if(!fs.isOpened()) { - printf("Failed to open file %s\n", intrinsic_filename); + printf("Failed to open file %s\n", intrinsic_filename.c_str()); return -1; } @@ -208,7 +184,7 @@ int main(int argc, char** argv) fs.open(extrinsic_filename, FileStorage::READ); if(!fs.isOpened()) { - printf("Failed to open file %s\n", extrinsic_filename); + printf("Failed to open file %s\n", extrinsic_filename.c_str()); return -1; } @@ -297,16 +273,16 @@ int main(int argc, char** argv) printf("\n"); } - if(disparity_filename) + if(!disparity_filename.empty()) imwrite(disparity_filename, disp8); - if(point_cloud_filename) + if(!point_cloud_filename.empty()) { printf("storing the point cloud..."); fflush(stdout); Mat xyz; reprojectImageTo3D(disp, xyz, Q, true); - saveXYZ(point_cloud_filename, xyz); + saveXYZ(point_cloud_filename.c_str(), xyz); printf("\n"); } diff --git a/samples/cpp/train_HOG.cpp b/samples/cpp/train_HOG.cpp index 0a94e348c..900637b6b 100644 --- a/samples/cpp/train_HOG.cpp +++ b/samples/cpp/train_HOG.cpp @@ -88,7 +88,7 @@ void load_images( const string & prefix, const string & filename, vector< Mat > while( !end_of_parsing ) { getline( file, line ); - if( line == "" ) // no more file to read + if( line.empty() ) // no more file to read { end_of_parsing = true; break; @@ -403,23 +403,33 @@ void test_it( const Size & size ) int main( int argc, char** argv ) { - if( argc != 5 ) + cv::CommandLineParser parser(argc, argv, "{help h|| show help message}" + "{pd||pos_dir}{p||pos.lst}{nd||neg_dir}{n||neg.lst}"); + if (parser.has("help")) { - cout << "Wrong number of parameters." << endl - << "Usage: " << argv[0] << " pos_dir pos.lst neg_dir neg.lst" << endl - << "example: " << argv[0] << " /INRIA_dataset/ Train/pos.lst /INRIA_dataset/ Train/neg.lst" << endl; - exit( -1 ); + parser.printMessage(); + exit(0); } vector< Mat > pos_lst; vector< Mat > full_neg_lst; vector< Mat > neg_lst; vector< Mat > gradient_lst; vector< int > labels; - - load_images( argv[1], argv[2], pos_lst ); + string pos_dir = parser.get("pd"); + string pos = parser.get("p"); + string neg_dir = parser.get("nd"); + string neg = parser.get("n"); + if( pos_dir.empty() || pos.empty() || neg_dir.empty() || neg.empty() ) + { + cout << "Wrong number of parameters." << endl + << "Usage: " << argv[0] << " --pd=pos_dir -p=pos.lst --nd=neg_dir -n=neg.lst" << endl + << "example: " << argv[0] << " --pd=/INRIA_dataset/ -p=Train/pos.lst --nd=/INRIA_dataset/ -n=Train/neg.lst" << endl; + exit( -1 ); + } + load_images( pos_dir, pos, pos_lst ); labels.assign( pos_lst.size(), +1 ); const unsigned int old = (unsigned int)labels.size(); - load_images( argv[3], argv[4], full_neg_lst ); + load_images( neg_dir, neg, full_neg_lst ); sample_neg( full_neg_lst, neg_lst, Size( 96,160 ) ); labels.insert( labels.end(), neg_lst.size(), -1 ); CV_Assert( old < labels.size() ); diff --git a/samples/cpp/tree_engine.cpp b/samples/cpp/tree_engine.cpp index 2d6824d24..441258875 100644 --- a/samples/cpp/tree_engine.cpp +++ b/samples/cpp/tree_engine.cpp @@ -12,9 +12,9 @@ static void help() { printf( "\nThis sample demonstrates how to use different decision trees and forests including boosting and random trees.\n" - "Usage:\n\t./tree_engine [-r ] [-ts type_spec] \n" - "where -r specified the 0-based index of the response (0 by default)\n" - "-ts specifies the var type spec in the form ord[n1,n2-n3,n4-n5,...]cat[m1-m2,m3,m4-m5,...]\n" + "Usage:\n\t./tree_engine [-r=] [-ts=type_spec] \n" + "where -r= specified the 0-based index of the response (0 by default)\n" + "-ts= specifies the var type spec in the form ord[n1,n2-n3,n4-n5,...]cat[m1-m2,m3,m4-m5,...]\n" " is the name of training data file in comma-separated value format\n\n"); } @@ -34,43 +34,35 @@ static void train_and_print_errs(Ptr model, const Ptr& dat int main(int argc, char** argv) { - if(argc < 2) + cv::CommandLineParser parser(argc, argv, "{ help h | | }{r | 0 | }{ts | | }{@input | | }"); + if (parser.has("help")) { help(); return 0; } - const char* filename = 0; - int response_idx = 0; + std::string filename = parser.get("@input"); + int response_idx; std::string typespec; - - for(int i = 1; i < argc; i++) + response_idx = parser.get("r"); + typespec = parser.get("ts"); + if( filename.empty() || !parser.check() ) { - if(strcmp(argv[i], "-r") == 0) - sscanf(argv[++i], "%d", &response_idx); - else if(strcmp(argv[i], "-ts") == 0) - typespec = argv[++i]; - else if(argv[i][0] != '-' ) - filename = argv[i]; - else - { - printf("Error. Invalid option %s\n", argv[i]); - help(); - return -1; - } + parser.printErrors(); + help(); + return 0; } - - printf("\nReading in %s...\n\n",filename); + printf("\nReading in %s...\n\n",filename.c_str()); const double train_test_split_ratio = 0.5; Ptr data = TrainData::loadFromCSV(filename, 0, response_idx, response_idx+1, typespec); - if( data.empty() ) { - printf("ERROR: File %s can not be read\n", filename); + printf("ERROR: File %s can not be read\n", filename.c_str()); return 0; } data->setTrainTestSplitRatio(train_test_split_ratio); + std::cout << "Test/Train: " << data->getNTestSamples() << "/" << data->getNTrainSamples(); printf("======DTREE=====\n"); Ptr dtree = DTrees::create(); @@ -106,10 +98,19 @@ int main(int argc, char** argv) rtrees->setUseSurrogates(false); rtrees->setMaxCategories(16); rtrees->setPriors(Mat()); - rtrees->setCalculateVarImportance(false); + rtrees->setCalculateVarImportance(true); rtrees->setActiveVarCount(0); rtrees->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 0)); train_and_print_errs(rtrees, data); + cv::Mat ref_labels = data->getClassLabels(); + cv::Mat test_data = data->getTestSampleIdx(); + cv::Mat predict_labels; + rtrees->predict(data->getSamples(), predict_labels); + cv::Mat variable_importance = rtrees->getVarImportance(); + std::cout << "Estimated variable importance" << std::endl; + for (int i = 0; i < variable_importance.rows; i++) { + std::cout << "Variable " << i << ": " << variable_importance.at(i, 0) << std::endl; + } return 0; } diff --git a/samples/cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp b/samples/cpp/tutorial_code/imgcodecs/GDAL_IO/gdal-image.cpp similarity index 100% rename from samples/cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp rename to samples/cpp/tutorial_code/imgcodecs/GDAL_IO/gdal-image.cpp diff --git a/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp b/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp index 0513e367d..9b0d569c6 100644 --- a/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp +++ b/samples/cpp/tutorial_code/ml/introduction_to_svm/introduction_to_svm.cpp @@ -65,7 +65,7 @@ int main(int, char**) //! [show_vectors] thickness = 2; lineType = 8; - Mat sv = svm->getSupportVectors(); + Mat sv = svm->getUncompressedSupportVectors(); for (int i = 0; i < sv.rows; ++i) { diff --git a/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video-input-psnr-ssim.cpp b/samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp similarity index 100% rename from samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video-input-psnr-ssim.cpp rename to samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp diff --git a/samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp b/samples/cpp/tutorial_code/videoio/video-write/video-write.cpp similarity index 100% rename from samples/cpp/tutorial_code/HighGUI/video-write/video-write.cpp rename to samples/cpp/tutorial_code/videoio/video-write/video-write.cpp diff --git a/samples/cpp/tvl1_optical_flow.cpp b/samples/cpp/tvl1_optical_flow.cpp index dee9cf6e3..55b5558eb 100644 --- a/samples/cpp/tvl1_optical_flow.cpp +++ b/samples/cpp/tvl1_optical_flow.cpp @@ -148,23 +148,33 @@ static void writeOpticalFlowToFile(const Mat_& flow, const string& file int main(int argc, const char* argv[]) { - if (argc < 3) + cv::CommandLineParser parser(argc, argv, "{help h || show help message}" + "{ @frame0 | | frame 0}{ @frame1 | | frame 1}{ @output | | output flow}"); + if (parser.has("help")) { - cerr << "Usage : " << argv[0] << " []" << endl; + parser.printMessage(); + return 0; + } + string frame0_name = parser.get("@frame0"); + string frame1_name = parser.get("@frame1"); + string file = parser.get("@output"); + if (frame0_name.empty() || frame1_name.empty() || file.empty()) + { + cerr << "Usage : " << argv[0] << " [] [] []" << endl; return -1; } - Mat frame0 = imread(argv[1], IMREAD_GRAYSCALE); - Mat frame1 = imread(argv[2], IMREAD_GRAYSCALE); + Mat frame0 = imread(frame0_name, IMREAD_GRAYSCALE); + Mat frame1 = imread(frame1_name, IMREAD_GRAYSCALE); if (frame0.empty()) { - cerr << "Can't open image [" << argv[1] << "]" << endl; + cerr << "Can't open image [" << parser.get("frame0") << "]" << endl; return -1; } if (frame1.empty()) { - cerr << "Can't open image [" << argv[2] << "]" << endl; + cerr << "Can't open image [" << parser.get("frame1") << "]" << endl; return -1; } @@ -184,9 +194,8 @@ int main(int argc, const char* argv[]) Mat out; drawOpticalFlow(flow, out); - - if (argc == 4) - writeOpticalFlowToFile(flow, argv[3]); + if (!file.empty()) + writeOpticalFlowToFile(flow, file); imshow("Flow", out); waitKey(); diff --git a/samples/cpp/videostab.cpp b/samples/cpp/videostab.cpp index 2eea9b902..3fb7fb40b 100644 --- a/samples/cpp/videostab.cpp +++ b/samples/cpp/videostab.cpp @@ -73,9 +73,9 @@ void printHelp() cout << "OpenCV video stabilizer.\n" "Usage: videostab [arguments]\n\n" "Arguments:\n" - " -m, --model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n" + " -m=, --model=(transl|transl_and_scale|rigid|similarity|affine|homography)\n" " Set motion model. The default is affine.\n" - " -lp, --lin-prog-motion-est=(yes|no)\n" + " -lp=, --lin-prog-motion-est=(yes|no)\n" " Turn on/off LP based motion estimation. The default is no.\n" " --subset=(|auto)\n" " Number of random samples per one motion hypothesis. The default is auto.\n" @@ -89,16 +89,16 @@ void printHelp() " Number of keypoints to find in each frame. The default is 1000.\n" " --local-outlier-rejection=(yes|no)\n" " Perform local outlier rejection. The default is no.\n\n" - " -sm, --save-motions=(|no)\n" + " -sm=, --save-motions=(|no)\n" " Save estimated motions into file. The default is no.\n" - " -lm, --load-motions=(|no)\n" + " -lm=, --load-motions=(|no)\n" " Load motions from file. The default is no.\n\n" - " -r, --radius=\n" + " -r=, --radius=\n" " Set sliding window radius. The default is 15.\n" " --stdev=(|auto)\n" " Set smoothing weights standard deviation. The default is auto\n" " (i.e. sqrt(radius)).\n" - " -lps, --lin-prog-stab=(yes|no)\n" + " -lps=, --lin-prog-stab=(yes|no)\n" " Turn on/off linear programming based stabilization method.\n" " --lps-trim-ratio=(|auto)\n" " Trimming ratio used in linear programming based method.\n" @@ -114,28 +114,28 @@ void printHelp() " Do deblurring.\n" " --deblur-sens=\n" " Set deblurring sensitivity (from 0 to +inf). The default is 0.1.\n\n" - " -t, --trim-ratio=\n" + " -t=, --trim-ratio=\n" " Set trimming ratio (from 0 to 0.5). The default is 0.1.\n" - " -et, --est-trim=(yes|no)\n" + " -et=, --est-trim=(yes|no)\n" " Estimate trim ratio automatically. The default is yes.\n" - " -ic, --incl-constr=(yes|no)\n" + " -ic=, --incl-constr=(yes|no)\n" " Ensure the inclusion constraint is always satisfied. The default is no.\n\n" - " -bm, --border-mode=(replicate|reflect|const)\n" + " -bm=, --border-mode=(replicate|reflect|const)\n" " Set border extrapolation mode. The default is replicate.\n\n" " --mosaic=(yes|no)\n" " Do consistent mosaicing. The default is no.\n" " --mosaic-stdev=\n" " Consistent mosaicing stdev threshold. The default is 10.0.\n\n" - " -mi, --motion-inpaint=(yes|no)\n" + " -mi=, --motion-inpaint=(yes|no)\n" " Do motion inpainting (requires CUDA support). The default is no.\n" " --mi-dist-thresh=\n" " Estimated flow distance threshold for motion inpainting. The default is 5.0.\n\n" - " -ci, --color-inpaint=(no|average|ns|telea)\n" + " -ci=, --color-inpaint=(no|average|ns|telea)\n" " Do color inpainting. The defailt is no.\n" " --ci-radius=\n" " Set color inpainting radius (for ns and telea options only).\n" " The default is 2.0\n\n" - " -ws, --wobble-suppress=(yes|no)\n" + " -ws=, --wobble-suppress=(yes|no)\n" " Perform wobble suppression. The default is no.\n" " --ws-lp=(yes|no)\n" " Turn on/off LP based motion estimation. The default is no.\n" @@ -156,13 +156,13 @@ void printHelp() " Number of keypoints to find in each frame. The default is 1000.\n" " --ws-local-outlier-rejection=(yes|no)\n" " Perform local outlier rejection. The default is no.\n\n" - " -sm2, --save-motions2=(|no)\n" + " -sm2=, --save-motions2=(|no)\n" " Save motions estimated for wobble suppression. The default is no.\n" - " -lm2, --load-motions2=(|no)\n" + " -lm2=, --load-motions2=(|no)\n" " Load motions for wobble suppression from file. The default is no.\n\n" " -gpu=(yes|no)\n" " Use CUDA optimization whenever possible. The default is no.\n\n" - " -o, --output=(no|)\n" + " -o=, --output=(no|)\n" " Set output file path explicitely. The default is stabilized.avi.\n" " --fps=(|auto)\n" " Set output video FPS explicitely. By default the source FPS is used (auto).\n" diff --git a/samples/cpp/watershed.cpp b/samples/cpp/watershed.cpp index b92bbf1fd..9f2e69ec6 100644 --- a/samples/cpp/watershed.cpp +++ b/samples/cpp/watershed.cpp @@ -48,7 +48,13 @@ static void onMouse( int event, int x, int y, int flags, void* ) int main( int argc, char** argv ) { - char* filename = argc >= 2 ? argv[1] : (char*)"../data/fruits.jpg"; + cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | ../data/fruits.jpg | }"); + if (parser.has("help")) + { + help(); + return 0; + } + string filename = parser.get("@input"); Mat img0 = imread(filename, 1), imgGray; if( img0.empty() ) diff --git a/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi b/samples/data/Megamind.avi similarity index 100% rename from samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind.avi rename to samples/data/Megamind.avi diff --git a/samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind_bugy.avi b/samples/data/Megamind_bugy.avi similarity index 100% rename from samples/cpp/tutorial_code/HighGUI/video-input-psnr-ssim/video/Megamind_bugy.avi rename to samples/data/Megamind_bugy.avi diff --git a/samples/gpu/CMakeLists.txt b/samples/gpu/CMakeLists.txt index 32c53ecf1..852a8c2da 100644 --- a/samples/gpu/CMakeLists.txt +++ b/samples/gpu/CMakeLists.txt @@ -81,6 +81,10 @@ if(BUILD_EXAMPLES AND OCV_DEPENDENCIES_FOUND) file(GLOB all_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) + if(NOT WITH_OPENGL) + list(REMOVE_ITEM all_samples "opengl.cpp") + endif(NOT WITH_OPENGL) + foreach(sample_filename ${all_samples}) get_filename_component(sample ${sample_filename} NAME_WE) file(GLOB sample_srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${sample}.*) @@ -92,6 +96,9 @@ endif() if(INSTALL_C_EXAMPLES AND NOT WIN32) file(GLOB install_list *.c *.cpp *.jpg *.png *.data makefile.* build_all.sh *.dsp *.cmd ) + if(NOT WITH_OPENGL) + list(REMOVE_ITEM install_list "opengl.cpp") + endif(NOT WITH_OPENGL) install(FILES ${install_list} DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/gpu PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples) diff --git a/samples/gpu/opengl.cpp b/samples/gpu/opengl.cpp index e3e3ddc68..061cc58f3 100644 --- a/samples/gpu/opengl.cpp +++ b/samples/gpu/opengl.cpp @@ -1,13 +1,4 @@ #include -#include "cvconfig.h" - -#ifndef HAVE_OPENGL -int main() -{ - std::cerr << "Library was built without OpenGL support" << std::endl; - return -1; -} -#else #ifdef WIN32 #define WIN32_LEAN_AND_MEAN 1 @@ -124,5 +115,3 @@ int main(int argc, char* argv[]) return 0; } - -#endif diff --git a/samples/hal/README.md b/samples/hal/README.md new file mode 100644 index 000000000..c48eda40b --- /dev/null +++ b/samples/hal/README.md @@ -0,0 +1,34 @@ +Custom HAL samples +================== + +Samples in this folder are intended to demonstrate functionality replacement mechanism in the OpenCV library. + +The __c_hal__ is the example of pure C replacement library with all functions returning error. It can be used to verify error handling in the function switching code. + +The __slow_hal__ contains naive C++ implementations of the element-wise logical array operations (and, or, xor, not) making them twice slower than the default. + +Build custom HAL replacement library +------------------------------------ + +1. Create folder for build (for example `/my-hal-build`) +2. Go to the created folder and run cmake: `cmake /samples/hal/slow_hal` +3. Run make + +After build you will find static library in the build folder: `libslow_hal.a` + +Build OpenCV with HAL replacement +--------------------------------- + +1. Create folder for build (for example `/my-opencv-build`) +2. Go to the created folder and run cmake: + ``` + cmake \ + -DOPENCV_HAL_HEADERS="/samples/hal/slow_hal/impl.hpp" \ + -DOPENCV_HAL_LIBS="/my-hal-build/libslow_hal.a" \ + + ``` +3. Run make (or `make opencv_perf_core` to build the demonstration test executable only) +4. After build you can run the tests and verify that some functions works slower: + ``` + ./bin/opencv_perf_core --gtest_filter=*bitwise_and* + ``` diff --git a/samples/hal/c_hal/CMakeLists.txt b/samples/hal/c_hal/CMakeLists.txt new file mode 100644 index 000000000..51ebcee26 --- /dev/null +++ b/samples/hal/c_hal/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR) + +if(UNIX) + if(CMAKE_COMPILER_IS_GNUC OR CV_ICC) + set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + endif() +endif() + +add_library(c_hal impl.c) +set(OPENCV_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..") +target_include_directories(c_hal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${OPENCV_SRC_DIR}/modules/core/include) diff --git a/samples/hal/c_hal/impl.c b/samples/hal/c_hal/impl.c new file mode 100644 index 000000000..d2fcc5ae3 --- /dev/null +++ b/samples/hal/c_hal/impl.c @@ -0,0 +1,371 @@ +#include "impl.h" + +int wrong_add8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_add64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_sub64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_max64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_min64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_absdiff64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_and8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_or8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_xor8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_not8u(const uchar* src1, size_t sz1, uchar* dst, size_t sz, int w, int h) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp16s(const short* src1, size_t sz1, const short* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp32s(const int* src1, size_t sz1, const int* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp32f(const float* src1, size_t sz1, const float* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_cmp64f(const double* src1, size_t sz1, const double* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_mul64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_div64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_recip64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} + +int wrong_addWeighted64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, const double* scales) +{ + return CV_HAL_ERROR_UNKNOWN; // to test how OpenCV handles errors from external HAL +} diff --git a/samples/hal/c_hal/impl.h b/samples/hal/c_hal/impl.h new file mode 100644 index 000000000..9fc4f196f --- /dev/null +++ b/samples/hal/c_hal/impl.h @@ -0,0 +1,245 @@ +#ifndef _wrong_H_INCLUDED_ +#define _wrong_H_INCLUDED_ + +#include "opencv2/core/hal/interface.h" + +#if defined(__cplusplus) +extern "C" +{ +#endif + +int wrong_add8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_add8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h); +int wrong_add16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h); +int wrong_add16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h); +int wrong_add32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h); +int wrong_add32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h); +int wrong_add64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h); +int wrong_sub8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_sub8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h); +int wrong_sub16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h); +int wrong_sub16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h); +int wrong_sub32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h); +int wrong_sub32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h); +int wrong_sub64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h); +int wrong_max8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_max8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h); +int wrong_max16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h); +int wrong_max16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h); +int wrong_max32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h); +int wrong_max32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h); +int wrong_max64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h); +int wrong_min8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_min8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h); +int wrong_min16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h); +int wrong_min16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h); +int wrong_min32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h); +int wrong_min32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h); +int wrong_min64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h); +int wrong_absdiff8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_absdiff8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h); +int wrong_absdiff16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h); +int wrong_absdiff16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h); +int wrong_absdiff32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h); +int wrong_absdiff32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h); +int wrong_absdiff64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h); +int wrong_and8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_or8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_xor8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h); +int wrong_not8u(const uchar* src1, size_t sz1, uchar* dst, size_t sz, int w, int h); + +#undef cv_hal_add8u +#define cv_hal_add8u wrong_add8u +#undef cv_hal_add8s +#define cv_hal_add8s wrong_add8s +#undef cv_hal_add16u +#define cv_hal_add16u wrong_add16u +#undef cv_hal_add16s +#define cv_hal_add16s wrong_add16s +#undef cv_hal_add32s +#define cv_hal_add32s wrong_add32s +#undef cv_hal_add32f +#define cv_hal_add32f wrong_add32f +#undef cv_hal_add64f +#define cv_hal_add64f wrong_add64f +#undef cv_hal_sub8u +#define cv_hal_sub8u wrong_sub8u +#undef cv_hal_sub8s +#define cv_hal_sub8s wrong_sub8s +#undef cv_hal_sub16u +#define cv_hal_sub16u wrong_sub16u +#undef cv_hal_sub16s +#define cv_hal_sub16s wrong_sub16s +#undef cv_hal_sub32s +#define cv_hal_sub32s wrong_sub32s +#undef cv_hal_sub32f +#define cv_hal_sub32f wrong_sub32f +#undef cv_hal_sub64f +#define cv_hal_sub64f wrong_sub64f +#undef cv_hal_max8u +#define cv_hal_max8u wrong_max8u +#undef cv_hal_max8s +#define cv_hal_max8s wrong_max8s +#undef cv_hal_max16u +#define cv_hal_max16u wrong_max16u +#undef cv_hal_max16s +#define cv_hal_max16s wrong_max16s +#undef cv_hal_max32s +#define cv_hal_max32s wrong_max32s +#undef cv_hal_max32f +#define cv_hal_max32f wrong_max32f +#undef cv_hal_max64f +#define cv_hal_max64f wrong_max64f +#undef cv_hal_min8u +#define cv_hal_min8u wrong_min8u +#undef cv_hal_min8s +#define cv_hal_min8s wrong_min8s +#undef cv_hal_min16u +#define cv_hal_min16u wrong_min16u +#undef cv_hal_min16s +#define cv_hal_min16s wrong_min16s +#undef cv_hal_min32s +#define cv_hal_min32s wrong_min32s +#undef cv_hal_min32f +#define cv_hal_min32f wrong_min32f +#undef cv_hal_min64f +#define cv_hal_min64f wrong_min64f +#undef cv_hal_absdiff8u +#define cv_hal_absdiff8u wrong_absdiff8u +#undef cv_hal_absdiff8s +#define cv_hal_absdiff8s wrong_absdiff8s +#undef cv_hal_absdiff16u +#define cv_hal_absdiff16u wrong_absdiff16u +#undef cv_hal_absdiff16s +#define cv_hal_absdiff16s wrong_absdiff16s +#undef cv_hal_absdiff32s +#define cv_hal_absdiff32s wrong_absdiff32s +#undef cv_hal_absdiff32f +#define cv_hal_absdiff32f wrong_absdiff32f +#undef cv_hal_absdiff64f +#define cv_hal_absdiff64f wrong_absdiff64f +#undef cv_hal_and8u +#define cv_hal_and8u wrong_and8u +#undef cv_hal_or8u +#define cv_hal_or8u wrong_or8u +#undef cv_hal_xor8u +#define cv_hal_xor8u wrong_xor8u +#undef cv_hal_not8u +#define cv_hal_not8u wrong_not8u + +int wrong_cmp8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp16s(const short* src1, size_t sz1, const short* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp32s(const int* src1, size_t sz1, const int* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp32f(const float* src1, size_t sz1, const float* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); +int wrong_cmp64f(const double* src1, size_t sz1, const double* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, int op); + +#undef cv_hal_cmp8u +#define cv_hal_cmp8u wrong_cmp8u +#undef cv_hal_cmp8s +#define cv_hal_cmp8s wrong_cmp8s +#undef cv_hal_cmp16u +#define cv_hal_cmp16u wrong_cmp16u +#undef cv_hal_cmp16s +#define cv_hal_cmp16s wrong_cmp16s +#undef cv_hal_cmp32s +#define cv_hal_cmp32s wrong_cmp32s +#undef cv_hal_cmp32f +#define cv_hal_cmp32f wrong_cmp32f +#undef cv_hal_cmp64f +#define cv_hal_cmp64f wrong_cmp64f + +int wrong_mul8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale); +int wrong_mul8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale); +int wrong_mul16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale); +int wrong_mul16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale); +int wrong_mul32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale); +int wrong_mul32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale); +int wrong_mul64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale); +int wrong_div8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale); +int wrong_div8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale); +int wrong_div16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale); +int wrong_div16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale); +int wrong_div32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale); +int wrong_div32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale); +int wrong_div64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale); +int wrong_recip8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, double scale); +int wrong_recip8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, double scale); +int wrong_recip16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, double scale); +int wrong_recip16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, double scale); +int wrong_recip32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, double scale); +int wrong_recip32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, double scale); +int wrong_recip64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, double scale); + +#undef cv_hal_mul8u +#define cv_hal_mul8u wrong_mul8u +#undef cv_hal_mul8s +#define cv_hal_mul8s wrong_mul8s +#undef cv_hal_mul16u +#define cv_hal_mul16u wrong_mul16u +#undef cv_hal_mul16s +#define cv_hal_mul16s wrong_mul16s +#undef cv_hal_mul32s +#define cv_hal_mul32s wrong_mul32s +#undef cv_hal_mul32f +#define cv_hal_mul32f wrong_mul32f +#undef cv_hal_mul64f +#define cv_hal_mul64f wrong_mul64f +#undef cv_hal_div8u +#define cv_hal_div8u wrong_div8u +#undef cv_hal_div8s +#define cv_hal_div8s wrong_div8s +#undef cv_hal_div16u +#define cv_hal_div16u wrong_div16u +#undef cv_hal_div16s +#define cv_hal_div16s wrong_div16s +#undef cv_hal_div32s +#define cv_hal_div32s wrong_div32s +#undef cv_hal_div32f +#define cv_hal_div32f wrong_div32f +#undef cv_hal_div64f +#define cv_hal_div64f wrong_div64f +#undef cv_hal_recip8u +#define cv_hal_recip8u wrong_recip8u +#undef cv_hal_recip8s +#define cv_hal_recip8s wrong_recip8s +#undef cv_hal_recip16u +#define cv_hal_recip16u wrong_recip16u +#undef cv_hal_recip16s +#define cv_hal_recip16s wrong_recip16s +#undef cv_hal_recip32s +#define cv_hal_recip32s wrong_recip32s +#undef cv_hal_recip32f +#define cv_hal_recip32f wrong_recip32f +#undef cv_hal_recip64f +#define cv_hal_recip64f wrong_recip64f + +int wrong_addWeighted8u(const uchar* src1, size_t sz1, const uchar* src2, size_t sz2, uchar* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted8s(const schar* src1, size_t sz1, const schar* src2, size_t sz2, schar* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted16u(const ushort* src1, size_t sz1, const ushort* src2, size_t sz2, ushort* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted16s(const short* src1, size_t sz1, const short* src2, size_t sz2, short* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted32s(const int* src1, size_t sz1, const int* src2, size_t sz2, int* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted32f(const float* src1, size_t sz1, const float* src2, size_t sz2, float* dst, size_t sz, int w, int h, const double* scales); +int wrong_addWeighted64f(const double* src1, size_t sz1, const double* src2, size_t sz2, double* dst, size_t sz, int w, int h, const double* scales); + +#undef cv_hal_addWeighted8u +#define cv_hal_addWeighted8u wrong_addWeighted8u +#undef cv_hal_addWeighted8s +#define cv_hal_addWeighted8s wrong_addWeighted8s +#undef cv_hal_addWeighted16u +#define cv_hal_addWeighted16u wrong_addWeighted16u +#undef cv_hal_addWeighted16s +#define cv_hal_addWeighted16s wrong_addWeighted16s +#undef cv_hal_addWeighted32s +#define cv_hal_addWeighted32s wrong_addWeighted32s +#undef cv_hal_addWeighted32f +#define cv_hal_addWeighted32f wrong_addWeighted32f +#undef cv_hal_addWeighted64f +#define cv_hal_addWeighted64f wrong_addWeighted64f + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/samples/hal/slow_hal/CMakeLists.txt b/samples/hal/slow_hal/CMakeLists.txt new file mode 100644 index 000000000..d001c4247 --- /dev/null +++ b/samples/hal/slow_hal/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR) + +if(UNIX) + if(CMAKE_COMPILER_IS_GNUCXX OR CV_ICC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + endif() +endif() + +add_library(slow_hal impl.cpp) +set(OPENCV_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..") +target_include_directories(slow_hal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${OPENCV_SRC_DIR}/modules/core/include) diff --git a/modules/hal/samples/simple_hal/simple.cpp b/samples/hal/slow_hal/impl.cpp similarity index 73% rename from modules/hal/samples/simple_hal/simple.cpp rename to samples/hal/slow_hal/impl.cpp index 564a611a5..cebec50d3 100644 --- a/modules/hal/samples/simple_hal/simple.cpp +++ b/samples/hal/slow_hal/impl.cpp @@ -1,11 +1,11 @@ -#include "simple.hpp" +#include "impl.hpp" int slow_and8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height) { for(; height--; src1 = src1 + step1, src2 = src2 + step2, dst = dst + step) for(int x = 0 ; x < width; x++ ) dst[x] = src1[x] & src2[x]; - return cv::hal::Error::Ok; + return CV_HAL_ERROR_OK; } int slow_or8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height) @@ -13,7 +13,7 @@ int slow_or8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, for(; height--; src1 = src1 + step1, src2 = src2 + step2, dst = dst + step) for(int x = 0 ; x < width; x++ ) dst[x] = src1[x] | src2[x]; - return cv::hal::Error::Ok; + return CV_HAL_ERROR_OK; } int slow_xor8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height) @@ -21,13 +21,13 @@ int slow_xor8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, for(; height--; src1 = src1 + step1, src2 = src2 + step2, dst = dst + step) for(int x = 0 ; x < width; x++ ) dst[x] = src1[x] ^ src2[x]; - return cv::hal::Error::Ok; + return CV_HAL_ERROR_OK; } -int slow_not8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height) +int slow_not8u(const uchar* src1, size_t step1, uchar* dst, size_t step, int width, int height) { - for(; height--; src1 = src1 + step1, src2 = src2 + step2, dst = dst + step) + for(; height--; src1 = src1 + step1, dst = dst + step) for(int x = 0 ; x < width; x++ ) dst[x] = ~src1[x]; - return cv::hal::Error::Ok; + return CV_HAL_ERROR_OK; } diff --git a/modules/hal/samples/simple_hal/simple.hpp b/samples/hal/slow_hal/impl.hpp similarity index 56% rename from modules/hal/samples/simple_hal/simple.hpp rename to samples/hal/slow_hal/impl.hpp index 85a16535d..5cbef5e3a 100644 --- a/modules/hal/samples/simple_hal/simple.hpp +++ b/samples/hal/slow_hal/impl.hpp @@ -1,20 +1,20 @@ #ifndef _SIMPLE_HPP_INCLUDED_ #define _SIMPLE_HPP_INCLUDED_ -#include "opencv2/hal/interface.hpp" +#include "opencv2/core/hal/interface.h" int slow_and8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height); int slow_or8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height); int slow_xor8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height); -int slow_not8u(const uchar* src1, size_t step1, const uchar* src2, size_t step2, uchar* dst, size_t step, int width, int height); +int slow_not8u(const uchar* src1, size_t step1, uchar* dst, size_t step, int width, int height); -#undef hal_and8u -#define hal_and8u slow_and8u -#undef hal_or8u -#define hal_or8u slow_or8u -#undef hal_xor8u -#define hal_xor8u slow_xor8u -#undef hal_not8u -#define hal_not8u slow_not8u +#undef cv_hal_and8u +#define cv_hal_and8u slow_and8u +#undef cv_hal_or8u +#define cv_hal_or8u slow_or8u +#undef cv_hal_xor8u +#define cv_hal_xor8u slow_xor8u +#undef cv_hal_not8u +#define cv_hal_not8u slow_not8u #endif diff --git a/samples/opencl/opencl-opencv-interop.cpp b/samples/opencl/opencl-opencv-interop.cpp index 5d5d52e02..4f96280cb 100644 --- a/samples/opencl/opencl-opencv-interop.cpp +++ b/samples/opencl/opencl-opencv-interop.cpp @@ -770,7 +770,7 @@ int App::process_frame_with_open_cl(cv::Mat& frame, bool use_buffer, cl_mem* mem return -1; size_t origin[] = { 0, 0, 0 }; - size_t region[] = { frame.cols, frame.rows, 1 }; + size_t region[] = { (size_t)frame.cols, (size_t)frame.rows, 1 }; res = clEnqueueCopyImage(m_queue, m_img_src, mem, origin, origin, region, 0, 0, &m_event); if (CL_SUCCESS != res) return -1; @@ -796,7 +796,7 @@ int App::process_frame_with_open_cl(cv::Mat& frame, bool use_buffer, cl_mem* mem return -1; // process left half of frame in OpenCL - size_t size[] = { frame.cols / 2, frame.rows }; + size_t size[] = { (size_t)frame.cols / 2, (size_t)frame.rows }; res = clEnqueueNDRangeKernel(m_queue, kernel, 2, 0, size, 0, 0, 0, &m_event); if (CL_SUCCESS != res) return -1; diff --git a/samples/python/.gitignore b/samples/python/.gitignore new file mode 100644 index 000000000..e449f6968 --- /dev/null +++ b/samples/python/.gitignore @@ -0,0 +1,10 @@ +# python binary files +*.pyc +__pycache__ + +# tmp files from examples +/output +*.dat +out.ply +svm_scores.npz +unused_api.txt diff --git a/samples/python2/CMakeLists.txt b/samples/python/CMakeLists.txt similarity index 72% rename from samples/python2/CMakeLists.txt rename to samples/python/CMakeLists.txt index 7fa245447..31c2bd189 100644 --- a/samples/python2/CMakeLists.txt +++ b/samples/python/CMakeLists.txt @@ -1,6 +1,6 @@ if(INSTALL_PYTHON_EXAMPLES) file(GLOB install_list *.py ) install(FILES ${install_list} - DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/python2 + DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/python PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples) endif() diff --git a/samples/python2/_coverage.py b/samples/python/_coverage.py similarity index 100% rename from samples/python2/_coverage.py rename to samples/python/_coverage.py diff --git a/samples/python/_doc.py b/samples/python/_doc.py new file mode 100755 index 000000000..5a2a81bcc --- /dev/null +++ b/samples/python/_doc.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +''' +Scans current directory for *.py files and reports +ones with missing __doc__ string. +''' + +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +from glob import glob + +if __name__ == '__main__': + print('--- undocumented files:') + for fn in glob('*.py'): + loc = {} + try: + if PY3: + exec(open(fn).read(), loc) + else: + execfile(fn, loc) + except: + pass + if '__doc__' not in loc: + print(fn) diff --git a/samples/python/_run_winpack_demo_python27.cmd b/samples/python/_run_winpack_demo_python27.cmd new file mode 100644 index 000000000..a4a65184b --- /dev/null +++ b/samples/python/_run_winpack_demo_python27.cmd @@ -0,0 +1,47 @@ +@echo off +if NOT exist %CD%\..\..\..\build ( + echo ERROR: OpenCV Winpack installation is required + pause + exit +) + +:: Path to FFMPEG binary files +set PATH=%PATH%;%CD%\..\..\..\build\bin\ + +:: Detect Python binary +python -V +if %ERRORLEVEL% EQU 0 ( + set PYTHON=python +) else ( + if exist C:\Python27-x64\python.exe ( + set PYTHON=C:\Python27-x64\python.exe + ) else ( + if exist C:\Python27\python.exe ( + set PYTHON=C:\Python27\python.exe + ) else ( + echo ERROR: Python not found + pause + exit + ) + ) +) +echo Using python: %PYTHON% + +:: Detect python architecture +%PYTHON% -c "import platform; exit(64 if platform.architecture()[0] == '64bit' else 32)" +if %ERRORLEVEL% EQU 32 ( + echo Detected: Python 32-bit + set PYTHONPATH=%CD%\..\..\..\build\python\2.7\x86 +) else ( + if %ERRORLEVEL% EQU 64 ( + echo Detected: Python 64-bit + set PYTHONPATH=%CD%\..\..\..\build\python\2.7\x64 + ) else ( + echo ERROR: Unknown python arch + pause + exit + ) +) + +:: Launch demo +%PYTHON% demo.py diff --git a/samples/python2/asift.py b/samples/python/asift.py similarity index 89% rename from samples/python2/asift.py rename to samples/python/asift.py index b24977bc0..8d2774a72 100755 --- a/samples/python2/asift.py +++ b/samples/python/asift.py @@ -19,6 +19,9 @@ USAGE Press left mouse button on a feature point to see its matching point. ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -96,15 +99,15 @@ def affine_detect(detector, img, mask=None, pool=None): ires = pool.imap(f, params) for i, (k, d) in enumerate(ires): - print 'affine sampling: %d / %d\r' % (i+1, len(params)), + print('affine sampling: %d / %d\r' % (i+1, len(params)), end='') keypoints.extend(k) descrs.extend(d) - print + print() return keypoints, np.array(descrs) if __name__ == '__main__': - print __doc__ + print(__doc__) import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) @@ -121,23 +124,23 @@ if __name__ == '__main__': detector, matcher = init_feature(feature_name) if img1 is None: - print 'Failed to load fn1:', fn1 + print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: - print 'Failed to load fn2:', fn2 + print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: - print 'unknown feature:', feature_name + print('unknown feature:', feature_name) sys.exit(1) - print 'using', feature_name + print('using', feature_name) pool=ThreadPool(processes = cv2.getNumberOfCPUs()) kp1, desc1 = affine_detect(detector, img1, pool=pool) kp2, desc2 = affine_detect(detector, img2, pool=pool) - print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)) + print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): with Timer('matching'): @@ -145,12 +148,12 @@ if __name__ == '__main__': p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) - print '%d / %d inliers/matched' % (np.sum(status), len(status)) + print('%d / %d inliers/matched' % (np.sum(status), len(status))) # do not draw outliers (there will be a lot of them) kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag] else: H, status = None, None - print '%d matches found, not enough for homography estimation' % len(p1) + print('%d matches found, not enough for homography estimation' % len(p1)) vis = explore_match(win, img1, img2, kp_pairs, None, H) diff --git a/samples/python2/browse.py b/samples/python/browse.py similarity index 100% rename from samples/python2/browse.py rename to samples/python/browse.py diff --git a/samples/python/calibrate.py b/samples/python/calibrate.py new file mode 100755 index 000000000..00dfe57fe --- /dev/null +++ b/samples/python/calibrate.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python + +''' +camera calibration for distorted images with chess board samples +reads distorted images, calculates the calibration and write undistorted images + +usage: + calibrate.py [--debug ] [--square_size] [] + +default values: + --debug: ./output/ + --square_size: 1.0 + defaults to ../data/left*.jpg +''' + +# Python 2/3 compatibility +from __future__ import print_function + +import numpy as np +import cv2 + +# local modules +from common import splitfn + +# built-in modules +import os + +if __name__ == '__main__': + import sys + import getopt + from glob import glob + + args, img_mask = getopt.getopt(sys.argv[1:], '', ['debug=', 'square_size=']) + args = dict(args) + args.setdefault('--debug', './output/') + args.setdefault('--square_size', 1.0) + if not img_mask: + img_mask = '../data/left*.jpg' # default + else: + img_mask = img_mask[0] + + img_names = glob(img_mask) + debug_dir = args.get('--debug') + if not os.path.isdir(debug_dir): + os.mkdir(debug_dir) + square_size = float(args.get('--square_size')) + + pattern_size = (9, 6) + pattern_points = np.zeros((np.prod(pattern_size), 3), np.float32) + pattern_points[:, :2] = np.indices(pattern_size).T.reshape(-1, 2) + pattern_points *= square_size + + obj_points = [] + img_points = [] + h, w = 0, 0 + img_names_undistort = [] + for fn in img_names: + print('processing %s... ' % fn, end='') + img = cv2.imread(fn, 0) + if img is None: + print("Failed to load", fn) + continue + + h, w = img.shape[:2] + found, corners = cv2.findChessboardCorners(img, pattern_size) + if found: + term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1) + cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term) + + if debug_dir: + vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) + cv2.drawChessboardCorners(vis, pattern_size, corners, found) + path, name, ext = splitfn(fn) + outfile = debug_dir + name + '_chess.png' + cv2.imwrite(outfile, vis) + if found: + img_names_undistort.append(outfile) + + if not found: + print('chessboard not found') + continue + + img_points.append(corners.reshape(-1, 2)) + obj_points.append(pattern_points) + + print('ok') + + # calculate camera distortion + rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None) + + print("\nRMS:", rms) + print("camera matrix:\n", camera_matrix) + print("distortion coefficients: ", dist_coefs.ravel()) + + # undistort the image with the calibration + print('') + for img_found in img_names_undistort: + img = cv2.imread(img_found) + + h, w = img.shape[:2] + newcameramtx, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coefs, (w, h), 1, (w, h)) + + dst = cv2.undistort(img, camera_matrix, dist_coefs, None, newcameramtx) + + # crop and save the image + x, y, w, h = roi + dst = dst[y:y+h, x:x+w] + outfile = img_found + '_undistorted.png' + print('Undistorted image written to: %s' % outfile) + cv2.imwrite(outfile, dst) + + cv2.destroyAllWindows() diff --git a/samples/python2/camshift.py b/samples/python/camshift.py similarity index 81% rename from samples/python2/camshift.py rename to samples/python/camshift.py index 867cee9b4..141eeed3f 100755 --- a/samples/python2/camshift.py +++ b/samples/python/camshift.py @@ -46,28 +46,22 @@ class App(object): self.selection = None self.drag_start = None - self.tracking_state = 0 self.show_backproj = False + self.track_window = None def onmouse(self, event, x, y, flags, param): - x, y = np.int16([x, y]) # BUG if event == cv2.EVENT_LBUTTONDOWN: self.drag_start = (x, y) - self.tracking_state = 0 - return + self.track_window = None if self.drag_start: - if flags & cv2.EVENT_FLAG_LBUTTON: - h, w = self.frame.shape[:2] - xo, yo = self.drag_start - x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y])) - x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y])) - self.selection = None - if x1-x0 > 0 and y1-y0 > 0: - self.selection = (x0, y0, x1, y1) - else: - self.drag_start = None - if self.selection is not None: - self.tracking_state = 1 + xmin = min(x, self.drag_start[0]) + ymin = min(y, self.drag_start[1]) + xmax = max(x, self.drag_start[0]) + ymax = max(y, self.drag_start[1]) + self.selection = (xmin, ymin, xmax, ymax) + if event == cv2.EVENT_LBUTTONUP: + self.drag_start = None + self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin) def show_hist(self): bin_count = self.hist.shape[0] @@ -88,7 +82,6 @@ class App(object): if self.selection: x0, y0, x1, y1 = self.selection - self.track_window = (x0, y0, x1-x0, y1-y0) hsv_roi = hsv[y0:y1, x0:x1] mask_roi = mask[y0:y1, x0:x1] hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] ) @@ -100,7 +93,7 @@ class App(object): cv2.bitwise_not(vis_roi, vis_roi) vis[mask == 0] = 0 - if self.tracking_state == 1: + if self.track_window and self.track_window[2] > 0 and self.track_window[3] > 0: self.selection = None prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1) prob &= mask diff --git a/samples/python2/coherence.py b/samples/python/coherence.py similarity index 100% rename from samples/python2/coherence.py rename to samples/python/coherence.py diff --git a/samples/python2/color_histogram.py b/samples/python/color_histogram.py similarity index 87% rename from samples/python2/color_histogram.py rename to samples/python/color_histogram.py index 7e964d5c5..9e691b7ef 100755 --- a/samples/python2/color_histogram.py +++ b/samples/python/color_histogram.py @@ -1,11 +1,18 @@ #!/usr/bin/env python +''' +Video histogram sample to show live histogram of video + +Keys: + ESC - exit + +''' + import numpy as np import cv2 # built-in modules import sys -from time import clock # local modules import video @@ -22,6 +29,7 @@ if __name__ == '__main__': cv2.namedWindow('hist', 0) hist_scale = 10 + def set_scale(val): global hist_scale hist_scale = val @@ -42,8 +50,7 @@ if __name__ == '__main__': hsv = cv2.cvtColor(small, cv2.COLOR_BGR2HSV) dark = hsv[...,2] < 32 hsv[dark] = 0 - h = cv2.calcHist( [hsv], [0, 1], None, [180, 256], [0, 180, 0, 256] ) - + h = cv2.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256]) h = np.clip(h*0.005*hist_scale, 0, 1) vis = hsv_map*h[:,:,np.newaxis] / 255.0 diff --git a/samples/python2/common.py b/samples/python/common.py similarity index 100% rename from samples/python2/common.py rename to samples/python/common.py diff --git a/samples/python2/contours.py b/samples/python/contours.py similarity index 88% rename from samples/python2/contours.py rename to samples/python/contours.py index 075401b33..619108b24 100755 --- a/samples/python2/contours.py +++ b/samples/python/contours.py @@ -9,6 +9,14 @@ Usage: A trackbar is put up which controls the contour level from -3 to 3 ''' +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +if PY3: + xrange = range + import numpy as np import cv2 @@ -16,8 +24,8 @@ def make_image(): img = np.zeros((500, 500), np.uint8) black, white = 0, 255 for i in xrange(6): - dx = (i%2)*250 - 30 - dy = (i/2)*150 + dx = int((i%2)*250 - 30) + dy = int((i/2.)*150) if i == 0: for j in xrange(11): @@ -41,7 +49,7 @@ def make_image(): return img if __name__ == '__main__': - print __doc__ + print(__doc__) img = make_image() h, w = img.shape[:2] @@ -52,7 +60,7 @@ if __name__ == '__main__': def update(levels): vis = np.zeros((h, w, 3), np.uint8) levels = levels - 3 - cv2.drawContours( vis, contours, (-1, 3)[levels <= 0], (128,255,255), + cv2.drawContours( vis, contours, (-1, 2)[levels <= 0], (128,255,255), 3, cv2.LINE_AA, hierarchy, abs(levels) ) cv2.imshow('contours', vis) update(3) diff --git a/samples/python2/deconvolution.py b/samples/python/deconvolution.py similarity index 100% rename from samples/python2/deconvolution.py rename to samples/python/deconvolution.py diff --git a/samples/python2/demo.py b/samples/python/demo.py similarity index 100% rename from samples/python2/demo.py rename to samples/python/demo.py diff --git a/samples/python2/dft.py b/samples/python/dft.py similarity index 95% rename from samples/python2/dft.py rename to samples/python/dft.py index d617438a8..4437aad64 100755 --- a/samples/python2/dft.py +++ b/samples/python/dft.py @@ -1,5 +1,13 @@ #!/usr/bin/env python +''' +sample for disctrete fourier transform (dft) + +USAGE: + dft.py +''' + + # Python 2/3 compatibility from __future__ import print_function @@ -56,9 +64,9 @@ def shift_dft(src, dst=None): if __name__ == "__main__": - if len(sys.argv)>1: + if len(sys.argv) > 1: im = cv2.imread(sys.argv[1]) - else : + else: im = cv2.imread('../data/baboon.jpg') print("usage : python dft.py ") diff --git a/samples/python2/digits.py b/samples/python/digits.py similarity index 89% rename from samples/python2/digits.py rename to samples/python/digits.py index 3b1653ff3..04397a143 100755 --- a/samples/python2/digits.py +++ b/samples/python/digits.py @@ -23,6 +23,10 @@ Usage: digits.py ''' + +# Python 2/3 compatibility +from __future__ import print_function + # built-in modules from multiprocessing.pool import ThreadPool @@ -50,7 +54,7 @@ def split2d(img, cell_size, flatten=True): return cells def load_digits(fn): - print 'loading "%s" ...' % fn + print('loading "%s" ...' % fn) digits_img = cv2.imread(fn, 0) digits = split2d(digits_img, (SZ, SZ)) labels = np.repeat(np.arange(CLASS_N), len(digits)/CLASS_N) @@ -67,7 +71,7 @@ def deskew(img): class StatModel(object): def load(self, fn): - self.model.load(fn) + self.model.load(fn) # Known bug: https://github.com/Itseez/opencv/issues/4969 def save(self, fn): self.model.save(fn) @@ -95,20 +99,20 @@ class SVM(StatModel): self.model.train(samples, cv2.ml.ROW_SAMPLE, responses) def predict(self, samples): - return self.model.predict(samples)[1][0].ravel() + return self.model.predict(samples)[1].ravel() def evaluate_model(model, digits, samples, labels): resp = model.predict(samples) err = (labels != resp).mean() - print 'error: %.2f %%' % (err*100) + print('error: %.2f %%' % (err*100)) confusion = np.zeros((10, 10), np.int32) for i, j in zip(labels, resp): confusion[i, j] += 1 - print 'confusion matrix:' - print confusion - print + print('confusion matrix:') + print(confusion) + print() vis = [] for img, flag in zip(digits, resp == labels): @@ -145,17 +149,17 @@ def preprocess_hog(digits): if __name__ == '__main__': - print __doc__ + print(__doc__) digits, labels = load_digits(DIGITS_FN) - print 'preprocessing...' + print('preprocessing...') # shuffle digits rand = np.random.RandomState(321) shuffle = rand.permutation(len(digits)) digits, labels = digits[shuffle], labels[shuffle] - digits2 = map(deskew, digits) + digits2 = list(map(deskew, digits)) samples = preprocess_hog(digits2) train_n = int(0.9*len(samples)) @@ -165,18 +169,18 @@ if __name__ == '__main__': labels_train, labels_test = np.split(labels, [train_n]) - print 'training KNearest...' + print('training KNearest...') model = KNearest(k=4) model.train(samples_train, labels_train) vis = evaluate_model(model, digits_test, samples_test, labels_test) cv2.imshow('KNearest test', vis) - print 'training SVM...' + print('training SVM...') model = SVM(C=2.67, gamma=5.383) model.train(samples_train, labels_train) vis = evaluate_model(model, digits_test, samples_test, labels_test) cv2.imshow('SVM test', vis) - print 'saving SVM as "digits_svm.dat"...' + print('saving SVM as "digits_svm.dat"...') model.save('digits_svm.dat') cv2.waitKey(0) diff --git a/samples/python2/digits_adjust.py b/samples/python/digits_adjust.py similarity index 78% rename from samples/python2/digits_adjust.py rename to samples/python/digits_adjust.py index fd34c3f70..79c46d65e 100755 --- a/samples/python2/digits_adjust.py +++ b/samples/python/digits_adjust.py @@ -13,6 +13,14 @@ Usage: ''' +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +if PY3: + xrange = range + import numpy as np import cv2 from multiprocessing.pool import ThreadPool @@ -33,10 +41,10 @@ def cross_validate(model_class, params, samples, labels, kfold = 3, pool = None) model.train(train_samples, train_labels) resp = model.predict(test_samples) score = (resp != test_labels).mean() - print ".", + print(".", end='') return score if pool is None: - scores = map(f, xrange(kfold)) + scores = list(map(f, xrange(kfold))) else: scores = pool.map(f, xrange(kfold)) return np.mean(scores) @@ -50,7 +58,7 @@ class App(object): digits, labels = load_digits(DIGITS_FN) shuffle = np.random.permutation(len(digits)) digits, labels = digits[shuffle], labels[shuffle] - digits2 = map(deskew, digits) + digits2 = list(map(deskew, digits)) samples = preprocess_hog(digits2) return samples, labels @@ -68,7 +76,7 @@ class App(object): scores = np.zeros((len(Cs), len(gammas))) scores[:] = np.nan - print 'adjusting SVM (may take a long time) ...' + print('adjusting SVM (may take a long time) ...') def f(job): i, j = job samples, labels = self.get_dataset() @@ -79,20 +87,21 @@ class App(object): ires = self.run_jobs(f, np.ndindex(*scores.shape)) for count, (i, j, score) in enumerate(ires): scores[i, j] = score - print '%d / %d (best error: %.2f %%, last: %.2f %%)' % (count+1, scores.size, np.nanmin(scores)*100, score*100) - print scores + print('%d / %d (best error: %.2f %%, last: %.2f %%)' % + (count+1, scores.size, np.nanmin(scores)*100, score*100)) + print(scores) - print 'writing score table to "svm_scores.npz"' + print('writing score table to "svm_scores.npz"') np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas) i, j = np.unravel_index(scores.argmin(), scores.shape) best_params = dict(C = Cs[i], gamma=gammas[j]) - print 'best params:', best_params - print 'best error: %.2f %%' % (scores.min()*100) + print('best params:', best_params) + print('best error: %.2f %%' % (scores.min()*100)) return best_params def adjust_KNearest(self): - print 'adjusting KNearest ...' + print('adjusting KNearest ...') def f(k): samples, labels = self.get_dataset() err = cross_validate(KNearest, dict(k=k), samples, labels) @@ -101,9 +110,9 @@ class App(object): for k, err in self.run_jobs(f, xrange(1, 9)): if err < best_err: best_err, best_k = err, k - print 'k = %d, error: %.2f %%' % (k, err*100) + print('k = %d, error: %.2f %%' % (k, err*100)) best_params = dict(k=best_k) - print 'best params:', best_params, 'err: %.2f' % (best_err*100) + print('best params:', best_params, 'err: %.2f' % (best_err*100)) return best_params @@ -111,14 +120,14 @@ if __name__ == '__main__': import getopt import sys - print __doc__ + print(__doc__) args, _ = getopt.getopt(sys.argv[1:], '', ['model=']) args = dict(args) args.setdefault('--model', 'svm') args.setdefault('--env', '') if args['--model'] not in ['svm', 'knearest']: - print 'unknown model "%s"' % args['--model'] + print('unknown model "%s"' % args['--model']) sys.exit(1) t = clock() @@ -127,4 +136,4 @@ if __name__ == '__main__': app.adjust_KNearest() else: app.adjust_SVM() - print 'work time: %f s' % (clock() - t) + print('work time: %f s' % (clock() - t)) diff --git a/samples/python2/digits_video.py b/samples/python/digits_video.py similarity index 95% rename from samples/python2/digits_video.py rename to samples/python/digits_video.py index bb142eb68..5f57cb8f9 100755 --- a/samples/python2/digits_video.py +++ b/samples/python/digits_video.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -22,7 +25,7 @@ def main(): classifier_fn = 'digits_svm.dat' if not os.path.exists(classifier_fn): - print '"%s" not found, run digits.py first' % classifier_fn + print('"%s" not found, run digits.py first' % classifier_fn) return model = SVM() model.load(classifier_fn) diff --git a/samples/python2/distrans.py b/samples/python/distrans.py similarity index 100% rename from samples/python2/distrans.py rename to samples/python/distrans.py diff --git a/samples/python2/edge.py b/samples/python/edge.py similarity index 95% rename from samples/python2/edge.py rename to samples/python/edge.py index 61d8466bc..c096fb46a 100755 --- a/samples/python2/edge.py +++ b/samples/python/edge.py @@ -14,6 +14,7 @@ Usage: from __future__ import print_function import cv2 +import numpy as np # relative module import video @@ -45,7 +46,7 @@ if __name__ == '__main__': thrs2 = cv2.getTrackbarPos('thrs2', 'edge') edge = cv2.Canny(gray, thrs1, thrs2, apertureSize=5) vis = img.copy() - vis /= 2 + vis = np.uint8(vis/2.) vis[edge != 0] = (0, 255, 0) cv2.imshow('edge', vis) ch = cv2.waitKey(5) & 0xFF diff --git a/samples/python2/facedetect.py b/samples/python/facedetect.py similarity index 87% rename from samples/python2/facedetect.py rename to samples/python/facedetect.py index 2349087dc..15187c62f 100755 --- a/samples/python2/facedetect.py +++ b/samples/python/facedetect.py @@ -1,5 +1,12 @@ #!/usr/bin/env python +''' +face detection using haar cascades + +USAGE: + facedetect.py [--cascade ] [--nested-cascade ] [] +''' + # Python 2/3 compatibility from __future__ import print_function @@ -10,12 +17,10 @@ import cv2 from video import create_capture from common import clock, draw_str -help_message = ''' -USAGE: facedetect.py [--cascade ] [--nested-cascade ] [] -''' def detect(img, cascade): - rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE) + rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), + flags=cv2.CASCADE_SCALE_IMAGE) if len(rects) == 0: return [] rects[:,2:] += rects[:,:2] @@ -27,7 +32,7 @@ def draw_rects(img, rects, color): if __name__ == '__main__': import sys, getopt - print(help_message) + print(__doc__) args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade=']) try: diff --git a/samples/python2/feature_homography.py b/samples/python/feature_homography.py similarity index 96% rename from samples/python2/feature_homography.py rename to samples/python/feature_homography.py index 0625e91e6..4a5e3190d 100755 --- a/samples/python2/feature_homography.py +++ b/samples/python/feature_homography.py @@ -22,6 +22,9 @@ Keys: Select a textured planar object to track by drawing a box with a mouse. ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -84,7 +87,7 @@ class App: if __name__ == '__main__': - print __doc__ + print(__doc__) import sys try: diff --git a/samples/python2/find_obj.py b/samples/python/find_obj.py similarity index 88% rename from samples/python2/find_obj.py rename to samples/python/find_obj.py index 7dab56ef5..d8d3d4133 100755 --- a/samples/python2/find_obj.py +++ b/samples/python/find_obj.py @@ -14,6 +14,9 @@ USAGE Press left mouse button on a feature point to see its matching point. ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 from common import anorm, getsize @@ -82,8 +85,10 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None): if status is None: status = np.ones(len(kp_pairs), np.bool_) - p1 = np.int32([kpp[0].pt for kpp in kp_pairs]) - p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) + (w1, 0) + p1, p2 = [], [] # python 2 / python 3 change of zip unpacking + for kpp in kp_pairs: + p1.append(np.int32(kpp[0].pt)) + p2.append(np.int32(np.array(kpp[1].pt) + [w1, 0])) green = (0, 255, 0) red = (0, 0, 255) @@ -133,7 +138,7 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None): if __name__ == '__main__': - print __doc__ + print(__doc__) import sys, getopt opts, args = getopt.getopt(sys.argv[1:], '', ['feature=']) @@ -150,33 +155,33 @@ if __name__ == '__main__': detector, matcher = init_feature(feature_name) if img1 is None: - print 'Failed to load fn1:', fn1 + print('Failed to load fn1:', fn1) sys.exit(1) if img2 is None: - print 'Failed to load fn2:', fn2 + print('Failed to load fn2:', fn2) sys.exit(1) if detector is None: - print 'unknown feature:', feature_name + print('unknown feature:', feature_name) sys.exit(1) - print 'using', feature_name + print('using', feature_name) kp1, desc1 = detector.detectAndCompute(img1, None) kp2, desc2 = detector.detectAndCompute(img2, None) - print 'img1 - %d features, img2 - %d features' % (len(kp1), len(kp2)) + print('img1 - %d features, img2 - %d features' % (len(kp1), len(kp2))) def match_and_draw(win): - print 'matching...' + print('matching...') raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2 p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches) if len(p1) >= 4: H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0) - print '%d / %d inliers/matched' % (np.sum(status), len(status)) + print('%d / %d inliers/matched' % (np.sum(status), len(status))) else: H, status = None, None - print '%d matches found, not enough for homography estimation' % len(p1) + print('%d matches found, not enough for homography estimation' % len(p1)) vis = explore_match(win, img1, img2, kp_pairs, status, H) diff --git a/samples/python2/fitline.py b/samples/python/fitline.py similarity index 100% rename from samples/python2/fitline.py rename to samples/python/fitline.py diff --git a/samples/python2/floodfill.py b/samples/python/floodfill.py similarity index 100% rename from samples/python2/floodfill.py rename to samples/python/floodfill.py diff --git a/samples/python2/gabor_threads.py b/samples/python/gabor_threads.py similarity index 100% rename from samples/python2/gabor_threads.py rename to samples/python/gabor_threads.py diff --git a/samples/python2/gaussian_mix.py b/samples/python/gaussian_mix.py similarity index 72% rename from samples/python2/gaussian_mix.py rename to samples/python/gaussian_mix.py index 6b1b9c24d..b0c28748f 100755 --- a/samples/python2/gaussian_mix.py +++ b/samples/python/gaussian_mix.py @@ -1,5 +1,13 @@ #!/usr/bin/env python +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +if PY3: + xrange = range + import numpy as np from numpy import random import cv2 @@ -30,19 +38,21 @@ if __name__ == '__main__': cluster_n = 5 img_size = 512 - print 'press any key to update distributions, ESC - exit\n' + print('press any key to update distributions, ESC - exit\n') while True: - print 'sampling distributions...' + print('sampling distributions...') points, ref_distrs = make_gaussians(cluster_n, img_size) - print 'EM (opencv) ...' - em = cv2.EM(cluster_n, cv2.EM_COV_MAT_GENERIC) - em.train(points) - means = em.getMat('means') - covs = em.getMatVector('covs') + print('EM (opencv) ...') + em = cv2.ml.EM_create() + em.setClustersNumber(cluster_n) + em.setCovarianceMatrixType(cv2.ml.EM_COV_MAT_GENERIC) + em.trainEM(points) + means = em.getMeans() + covs = em.getCovs() # Known bug: https://github.com/Itseez/opencv/pull/4232 found_distrs = zip(means, covs) - print 'ready!\n' + print('ready!\n') img = np.zeros((img_size, img_size, 3), np.uint8) for x, y in np.int32(points): diff --git a/samples/python2/grabcut.py b/samples/python/grabcut.py similarity index 100% rename from samples/python2/grabcut.py rename to samples/python/grabcut.py diff --git a/samples/python2/hist.py b/samples/python/hist.py similarity index 84% rename from samples/python2/hist.py rename to samples/python/hist.py index 41f79984a..80cc6b480 100755 --- a/samples/python2/hist.py +++ b/samples/python/hist.py @@ -15,6 +15,9 @@ Usage : python hist.py Abid Rahman 3/14/12 debug Gary Bradski ''' +# Python 2/3 compatibility +from __future__ import print_function + import cv2 import numpy as np @@ -38,8 +41,8 @@ def hist_curve(im): def hist_lines(im): h = np.zeros((300,256,3)) if len(im.shape)!=2: - print "hist_lines applicable only for grayscale images" - #print "so converting image to grayscale for representation" + print("hist_lines applicable only for grayscale images") + #print("so converting image to grayscale for representation" im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) hist_item = cv2.calcHist([im],[0],None,[256],[0,256]) cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX) @@ -58,18 +61,18 @@ if __name__ == '__main__': fname = sys.argv[1] else : fname = '../data/lena.jpg' - print "usage : python hist.py " + print("usage : python hist.py ") im = cv2.imread(fname) if im is None: - print 'Failed to load image file:', fname + print('Failed to load image file:', fname) sys.exit(1) gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) - print ''' Histogram plotting \n + print(''' Histogram plotting \n Keymap :\n a - show histogram for color image in curve mode \n b - show histogram in bin mode \n @@ -77,7 +80,7 @@ if __name__ == '__main__': d - show histogram for color image in curve mode \n e - show histogram for a normalized image in curve mode \n Esc - exit \n - ''' + ''') cv2.imshow('image',im) while True: @@ -86,31 +89,31 @@ if __name__ == '__main__': curve = hist_curve(im) cv2.imshow('histogram',curve) cv2.imshow('image',im) - print 'a' + print('a') elif k == ord('b'): - print 'b' + print('b') lines = hist_lines(im) cv2.imshow('histogram',lines) cv2.imshow('image',gray) elif k == ord('c'): - print 'c' + print('c') equ = cv2.equalizeHist(gray) lines = hist_lines(equ) cv2.imshow('histogram',lines) cv2.imshow('image',equ) elif k == ord('d'): - print 'd' + print('d') curve = hist_curve(gray) cv2.imshow('histogram',curve) cv2.imshow('image',gray) elif k == ord('e'): - print 'e' - norm = cv2.normalize(gray,alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX) + print('e') + norm = cv2.normalize(gray, gray, alpha = 0,beta = 255,norm_type = cv2.NORM_MINMAX) lines = hist_lines(norm) cv2.imshow('histogram',lines) cv2.imshow('image',norm) elif k == 27: - print 'ESC' + print('ESC') cv2.destroyAllWindows() break cv2.destroyAllWindows() diff --git a/samples/python2/houghcircles.py b/samples/python/houghcircles.py similarity index 83% rename from samples/python2/houghcircles.py rename to samples/python/houghcircles.py index fe87d8f3e..5386fc210 100755 --- a/samples/python2/houghcircles.py +++ b/samples/python/houghcircles.py @@ -2,8 +2,10 @@ ''' This example illustrates how to use cv2.HoughCircles() function. -Usage: ./houghcircles.py [] -image argument defaults to ../data/board.jpg + +Usage: + houghcircles.py [] + image argument defaults to ../data/board.jpg ''' # Python 2/3 compatibility @@ -14,11 +16,11 @@ import numpy as np import sys if __name__ == '__main__': - print(__doc__) + try: fn = sys.argv[1] - except: + except IndexError: fn = "../data/board.jpg" src = cv2.imread(fn, 1) @@ -30,7 +32,7 @@ if __name__ == '__main__': a, b, c = circles.shape for i in range(b): cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA) - cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle + cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA) # draw center of circle cv2.imshow("source", src) cv2.imshow("detected circles", cimg) diff --git a/samples/python2/houghlines.py b/samples/python/houghlines.py similarity index 92% rename from samples/python2/houghlines.py rename to samples/python/houghlines.py index 674b26ec7..120d8bbaa 100755 --- a/samples/python2/houghlines.py +++ b/samples/python/houghlines.py @@ -1,9 +1,13 @@ #!/usr/bin/python + ''' This example illustrates how to use Hough Transform to find lines -Usage: ./houghlines.py [] -image argument defaults to ../data/pic1.png + +Usage: + houghlines.py [] + image argument defaults to ../data/pic1.png ''' + # Python 2/3 compatibility from __future__ import print_function @@ -13,12 +17,13 @@ import sys import math if __name__ == '__main__': + print(__doc__) try: fn = sys.argv[1] - except: + except IndexError: fn = "../data/pic1.png" - print(__doc__) + src = cv2.imread(fn) dst = cv2.Canny(src, 50, 200) cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR) @@ -42,7 +47,6 @@ if __name__ == '__main__': pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) - cv2.imshow("source", src) cv2.imshow("detected lines", cdst) cv2.waitKey(0) diff --git a/samples/python2/inpaint.py b/samples/python/inpaint.py similarity index 100% rename from samples/python2/inpaint.py rename to samples/python/inpaint.py diff --git a/samples/python2/kalman.py b/samples/python/kalman.py similarity index 100% rename from samples/python2/kalman.py rename to samples/python/kalman.py diff --git a/samples/python2/kmeans.py b/samples/python/kmeans.py similarity index 85% rename from samples/python2/kmeans.py rename to samples/python/kmeans.py index ecf8c6f37..b2e6b6618 100755 --- a/samples/python2/kmeans.py +++ b/samples/python/kmeans.py @@ -10,6 +10,9 @@ Keyboard shortcuts: space - generate new distribution ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -19,7 +22,7 @@ if __name__ == '__main__': cluster_n = 5 img_size = 512 - print __doc__ + print(__doc__) # generating bright palette colors = np.zeros((1, cluster_n, 3), np.uint8) @@ -28,7 +31,7 @@ if __name__ == '__main__': colors = cv2.cvtColor(colors, cv2.COLOR_HSV2BGR)[0] while True: - print 'sampling distributions...' + print('sampling distributions...') points, _ = make_gaussians(cluster_n, img_size) term_crit = (cv2.TERM_CRITERIA_EPS, 30, 0.1) @@ -36,7 +39,8 @@ if __name__ == '__main__': img = np.zeros((img_size, img_size, 3), np.uint8) for (x, y), label in zip(np.int32(points), labels.ravel()): - c = map(int, colors[label]) + c = list(map(int, colors[label])) + cv2.circle(img, (x, y), 1, c, -1) cv2.imshow('gaussian mixture', img) diff --git a/samples/python2/lappyr.py b/samples/python/lappyr.py similarity index 95% rename from samples/python2/lappyr.py rename to samples/python/lappyr.py index 19f5bd918..d8fde0f00 100755 --- a/samples/python2/lappyr.py +++ b/samples/python/lappyr.py @@ -64,7 +64,7 @@ if __name__ == '__main__': pyr = build_lappyr(frame, leveln) for i in xrange(leveln): - v = cv2.getTrackbarPos('%d'%i, 'level control') / 5 + v = int(cv2.getTrackbarPos('%d'%i, 'level control') / 5) pyr[i] *= v res = merge_lappyr(pyr) diff --git a/samples/python2/letter_recog.py b/samples/python/letter_recog.py similarity index 81% rename from samples/python2/letter_recog.py rename to samples/python/letter_recog.py index cf7aecebc..e68c095bc 100755 --- a/samples/python2/letter_recog.py +++ b/samples/python/letter_recog.py @@ -25,6 +25,9 @@ USAGE: Models: RTrees, KNearest, Boost, SVM, MLP ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -58,22 +61,22 @@ class LetterStatModel(object): class RTrees(LetterStatModel): def __init__(self): - self.model = cv2.RTrees() + self.model = cv2.ml.RTrees_create() def train(self, samples, responses): sample_n, var_n = samples.shape - var_types = np.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL], np.uint8) + var_types = np.array([cv2.ml.VAR_NUMERICAL] * var_n + [cv2.ml.VAR_CATEGORICAL], np.uint8) #CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER)); params = dict(max_depth=10 ) - self.model.train(samples, cv2.CV_ROW_SAMPLE, responses, varType = var_types, params = params) + self.model.train(samples, cv2.ml.ROW_SAMPLE, responses, varType = var_types, params = params) def predict(self, samples): - return np.float32( [self.model.predict(s) for s in samples] ) + return [self.model.predict(s) for s in samples] class KNearest(LetterStatModel): def __init__(self): - self.model = cv2.KNearest() + self.model = cv2.ml.KNearest_create() def train(self, samples, responses): self.model.train(samples, responses) @@ -85,16 +88,16 @@ class KNearest(LetterStatModel): class Boost(LetterStatModel): def __init__(self): - self.model = cv2.Boost() + self.model = cv2.ml.Boost_create() def train(self, samples, responses): sample_n, var_n = samples.shape new_samples = self.unroll_samples(samples) new_responses = self.unroll_responses(responses) - var_types = np.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL, cv2.CV_VAR_CATEGORICAL], np.uint8) + var_types = np.array([cv2.ml.VAR_NUMERICAL] * var_n + [cv2.ml.VAR_CATEGORICAL, cv2.ml.VAR_CATEGORICAL], np.uint8) #CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 ) params = dict(max_depth=5) #, use_surrogates=False) - self.model.train(new_samples, cv2.CV_ROW_SAMPLE, new_responses, varType = var_types, params=params) + self.model.train(new_samples, cv2.ml.ROW_SAMPLE, new_responses, varType = var_types, params=params) def predict(self, samples): new_samples = self.unroll_samples(samples) @@ -105,11 +108,11 @@ class Boost(LetterStatModel): class SVM(LetterStatModel): def __init__(self): - self.model = cv2.SVM() + self.model = cv2.ml.SVM_create() def train(self, samples, responses): - params = dict( kernel_type = cv2.SVM_LINEAR, - svm_type = cv2.SVM_C_SVC, + params = dict( kernel_type = cv2.ml.SVM_LINEAR, + svm_type = cv2.ml.SVM_C_SVC, C = 1 ) self.model.train(samples, responses, params = params) @@ -119,7 +122,7 @@ class SVM(LetterStatModel): class MLP(LetterStatModel): def __init__(self): - self.model = cv2.ANN_MLP() + self.model = cv2.ml.ANN_MLP_create() def train(self, samples, responses): sample_n, var_n = samples.shape @@ -130,7 +133,7 @@ class MLP(LetterStatModel): # CvANN_MLP_TrainParams::BACKPROP,0.001 params = dict( term_crit = (cv2.TERM_CRITERIA_COUNT, 300, 0.01), - train_method = cv2.ANN_MLP_TRAIN_PARAMS_BACKPROP, + train_method = cv2.ml.ANN_MLP_TRAIN_PARAMS_BACKPROP, bp_dw_scale = 0.001, bp_moment_scale = 0.0 ) self.model.train(samples, np.float32(new_responses), None, params = params) @@ -144,7 +147,7 @@ if __name__ == '__main__': import getopt import sys - print __doc__ + print(__doc__) models = [RTrees, KNearest, Boost, SVM, MLP] # NBayes models = dict( [(cls.__name__.lower(), cls) for cls in models] ) @@ -155,7 +158,7 @@ if __name__ == '__main__': args.setdefault('--model', 'rtrees') args.setdefault('--data', '../data/letter-recognition.data') - print 'loading data %s ...' % args['--data'] + print('loading data %s ...' % args['--data']) samples, responses = load_base(args['--data']) Model = models[args['--model']] model = Model() @@ -163,20 +166,20 @@ if __name__ == '__main__': train_n = int(len(samples)*model.train_ratio) if '--load' in args: fn = args['--load'] - print 'loading model from %s ...' % fn + print('loading model from %s ...' % fn) model.load(fn) else: - print 'training %s ...' % Model.__name__ + print('training %s ...' % Model.__name__) model.train(samples[:train_n], responses[:train_n]) - print 'testing...' + print('testing...') train_rate = np.mean(model.predict(samples[:train_n]) == responses[:train_n]) test_rate = np.mean(model.predict(samples[train_n:]) == responses[train_n:]) - print 'train rate: %f test rate: %f' % (train_rate*100, test_rate*100) + print('train rate: %f test rate: %f' % (train_rate*100, test_rate*100)) if '--save' in args: fn = args['--save'] - print 'saving model to %s ...' % fn + print('saving model to %s ...' % fn) model.save(fn) cv2.destroyAllWindows() diff --git a/samples/python2/lk_homography.py b/samples/python/lk_homography.py similarity index 100% rename from samples/python2/lk_homography.py rename to samples/python/lk_homography.py diff --git a/samples/python2/lk_track.py b/samples/python/lk_track.py similarity index 100% rename from samples/python2/lk_track.py rename to samples/python/lk_track.py diff --git a/samples/python2/logpolar.py b/samples/python/logpolar.py similarity index 81% rename from samples/python2/logpolar.py rename to samples/python/logpolar.py index 60695bfd8..fdf03f3f8 100644 --- a/samples/python2/logpolar.py +++ b/samples/python/logpolar.py @@ -1,15 +1,27 @@ #!/usr/bin/env python +''' +plots image as logPolar and linearPolar + +Usage: + logpolar.py + +Keys: + ESC - exit +''' + # Python 2/3 compatibility from __future__ import print_function import cv2 if __name__ == '__main__': + print(__doc__) + import sys try: fn = sys.argv[1] - except: + except IndexError: fn = '../data/fruits.jpg' img = cv2.imread(fn) diff --git a/samples/python2/morphology.py b/samples/python/morphology.py similarity index 100% rename from samples/python2/morphology.py rename to samples/python/morphology.py diff --git a/samples/python2/mosse.py b/samples/python/mosse.py similarity index 100% rename from samples/python2/mosse.py rename to samples/python/mosse.py diff --git a/samples/python2/mouse_and_match.py b/samples/python/mouse_and_match.py similarity index 92% rename from samples/python2/mouse_and_match.py rename to samples/python/mouse_and_match.py index c0b03d77e..adbd8babe 100755 --- a/samples/python2/mouse_and_match.py +++ b/samples/python/mouse_and_match.py @@ -1,11 +1,13 @@ #!/usr/bin/env python ''' -mouse_and_match.py [-i path | --input path: default ./] +mouse_and_match.py [-i path | --input path: default ../data/] Demonstrate using a mouse to interact with an image: Read in the images in a directory one by one Allow the user to select parts of an image with a mouse When they let go of the mouse, it correlates (using matchTemplate) that patch with the image. + + SPACE for next image ESC to exit ''' @@ -54,8 +56,10 @@ def onmouse(event, x, y, flags, param): drag_start = None if __name__ == '__main__': + print(__doc__) + parser = argparse.ArgumentParser(description='Demonstrate mouse interaction with images') - parser.add_argument("-i","--input", default='./', help="Input directory.") + parser.add_argument("-i","--input", default='../data/', help="Input directory.") args = parser.parse_args() path = args.input @@ -68,7 +72,7 @@ if __name__ == '__main__': print(infile) img=cv2.imread(infile,1) - if img == None: + if img is None: continue sel = (0,0,0,0) drag_start = None diff --git a/samples/python2/mser.py b/samples/python/mser.py similarity index 100% rename from samples/python2/mser.py rename to samples/python/mser.py diff --git a/samples/python2/opencv_version.py b/samples/python/opencv_version.py similarity index 60% rename from samples/python2/opencv_version.py rename to samples/python/opencv_version.py index 44f197736..b26b55c1d 100644 --- a/samples/python2/opencv_version.py +++ b/samples/python/opencv_version.py @@ -1,5 +1,15 @@ #!/usr/bin/env python +''' +prints OpenCV version + +Usage: + opencv_version.py [] + params: + --build: print complete build info + --help: print this help +''' + # Python 2/3 compatibility from __future__ import print_function @@ -7,14 +17,16 @@ import cv2 if __name__ == '__main__': import sys + print(__doc__) + try: param = sys.argv[1] - except: + except IndexError: param = "" - if ("--build" == param): + if "--build" == param: print(cv2.getBuildInformation()) - elif ("--help" == param): + elif "--help" == param: print("\t--build\n\t\tprint complete build info") print("\t--help\n\t\tprint this help") else: diff --git a/samples/python2/opt_flow.py b/samples/python/opt_flow.py similarity index 85% rename from samples/python2/opt_flow.py rename to samples/python/opt_flow.py index c8a6086ed..be85262c9 100755 --- a/samples/python2/opt_flow.py +++ b/samples/python/opt_flow.py @@ -1,21 +1,29 @@ #!/usr/bin/env python -import numpy as np -import cv2 -import video +''' +example to show optical flow -help_message = ''' USAGE: opt_flow.py [] Keys: 1 - toggle HSV flow visualization 2 - toggle glitch +Keys: + ESC - exit ''' +# Python 2/3 compatibility +from __future__ import print_function + +import numpy as np +import cv2 +import video + + def draw_flow(img, flow, step=16): h, w = img.shape[:2] - y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1) + y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int) fx, fy = flow[y,x].T lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2) lines = np.int32(lines + 0.5) @@ -25,6 +33,7 @@ def draw_flow(img, flow, step=16): cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1) return vis + def draw_hsv(flow): h, w = flow.shape[:2] fx, fy = flow[:,:,0], flow[:,:,1] @@ -37,6 +46,7 @@ def draw_hsv(flow): bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return bgr + def warp_flow(img, flow): h, w = flow.shape[:2] flow = -flow @@ -47,10 +57,10 @@ def warp_flow(img, flow): if __name__ == '__main__': import sys - print help_message + print(__doc__) try: fn = sys.argv[1] - except: + except IndexError: fn = 0 cam = video.create_capture(fn) @@ -78,10 +88,10 @@ if __name__ == '__main__': break if ch == ord('1'): show_hsv = not show_hsv - print 'HSV flow visualization is', ['off', 'on'][show_hsv] + print('HSV flow visualization is', ['off', 'on'][show_hsv]) if ch == ord('2'): show_glitch = not show_glitch if show_glitch: cur_glitch = img.copy() - print 'glitch is', ['off', 'on'][show_glitch] + print('glitch is', ['off', 'on'][show_glitch]) cv2.destroyAllWindows() diff --git a/samples/python2/peopledetect.py b/samples/python/peopledetect.py similarity index 86% rename from samples/python2/peopledetect.py rename to samples/python/peopledetect.py index c8843e05e..1cad95288 100755 --- a/samples/python2/peopledetect.py +++ b/samples/python/peopledetect.py @@ -1,22 +1,27 @@ #!/usr/bin/env python +''' +example to detect upright people in images using HOG features + +Usage: + peopledetect.py + +Press any key to continue, ESC to stop. +''' + # Python 2/3 compatibility from __future__ import print_function import numpy as np import cv2 -help_message = ''' -USAGE: peopledetect.py ... - -Press any key to continue, ESC to stop. -''' def inside(r, q): rx, ry, rw, rh = r qx, qy, qw, qh = q return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh + def draw_detections(img, rects, thickness = 1): for x, y, w, h in rects: # the HOG detector returns slightly larger rectangles than the real objects. @@ -30,12 +35,14 @@ if __name__ == '__main__': from glob import glob import itertools as it - print(help_message) + print(__doc__) hog = cv2.HOGDescriptor() hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() ) - for fn in it.chain(*map(glob, sys.argv[1:])): + default = ['../data/basketball2.png '] if len(sys.argv[1:]) == 0 else [] + + for fn in it.chain(*map(glob, default + sys.argv[1:])): print(fn, ' - ',) try: img = cv2.imread(fn) diff --git a/samples/python2/plane_ar.py b/samples/python/plane_ar.py similarity index 97% rename from samples/python2/plane_ar.py rename to samples/python/plane_ar.py index 6580be7d0..ef6c8707e 100755 --- a/samples/python2/plane_ar.py +++ b/samples/python/plane_ar.py @@ -22,6 +22,9 @@ Select a textured planar object to track by drawing a box with a mouse. Use 'focal' slider to adjust to camera focal length for proper video augmentation. ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 import video @@ -97,7 +100,7 @@ class App: if __name__ == '__main__': - print __doc__ + print(__doc__) import sys try: diff --git a/samples/python2/plane_tracker.py b/samples/python/plane_tracker.py similarity index 93% rename from samples/python2/plane_tracker.py rename to samples/python/plane_tracker.py index c32f65a44..d8c221841 100755 --- a/samples/python2/plane_tracker.py +++ b/samples/python/plane_tracker.py @@ -21,6 +21,14 @@ Keys: Select a textured planar object to track by drawing a box with a mouse. ''' +# Python 2/3 compatibility +from __future__ import print_function +import sys +PY3 = sys.version_info[0] == 3 + +if PY3: + xrange = range + import numpy as np import cv2 @@ -64,6 +72,7 @@ class PlaneTracker: self.detector = cv2.ORB_create( nfeatures = 1000 ) self.matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329) self.targets = [] + self.frame_points = [] def add_target(self, image, rect, data=None): '''Add a new tracking target.''' @@ -87,8 +96,8 @@ class PlaneTracker: def track(self, frame): '''Returns a list of detected TrackedTarget objects''' - frame_points, frame_descrs = self.detect_features(frame) - if len(frame_points) < MIN_MATCH_COUNT: + self.frame_points, frame_descrs = self.detect_features(frame) + if len(self.frame_points) < MIN_MATCH_COUNT: return [] matches = self.matcher.knnMatch(frame_descrs, k = 2) matches = [m[0] for m in matches if len(m) == 2 and m[0].distance < m[1].distance * 0.75] @@ -103,7 +112,7 @@ class PlaneTracker: continue target = self.targets[imgIdx] p0 = [target.keypoints[m.trainIdx].pt for m in matches] - p1 = [frame_points[m.queryIdx].pt for m in matches] + p1 = [self.frame_points[m.queryIdx].pt for m in matches] p0, p1 = np.float32((p0, p1)) H, status = cv2.findHomography(p0, p1, cv2.RANSAC, 3.0) status = status.ravel() != 0 @@ -169,7 +178,7 @@ class App: break if __name__ == '__main__': - print __doc__ + print(__doc__) import sys try: diff --git a/samples/python2/squares.py b/samples/python/squares.py similarity index 100% rename from samples/python2/squares.py rename to samples/python/squares.py diff --git a/samples/python2/stereo_match.py b/samples/python/stereo_match.py similarity index 83% rename from samples/python2/stereo_match.py rename to samples/python/stereo_match.py index e53ae7702..1f6733015 100755 --- a/samples/python2/stereo_match.py +++ b/samples/python/stereo_match.py @@ -6,6 +6,9 @@ Simple example of stereo image matching and point cloud generation. Resulting .ply file cam be easily viewed using MeshLab ( http://meshlab.sourceforge.net/ ) ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -25,13 +28,13 @@ def write_ply(fn, verts, colors): verts = verts.reshape(-1, 3) colors = colors.reshape(-1, 3) verts = np.hstack([verts, colors]) - with open(fn, 'w') as f: - f.write(ply_header % dict(vert_num=len(verts))) - np.savetxt(f, verts, '%f %f %f %d %d %d') + with open(fn, 'wb') as f: + f.write((ply_header % dict(vert_num=len(verts))).encode('utf-8')) + np.savetxt(f, verts, fmt='%f %f %f %d %d %d ') if __name__ == '__main__': - print 'loading images...' + print('loading images...') imgL = cv2.pyrDown( cv2.imread('../data/aloeL.jpg') ) # downscale images for faster processing imgR = cv2.pyrDown( cv2.imread('../data/aloeR.jpg') ) @@ -50,10 +53,10 @@ if __name__ == '__main__': speckleRange = 32 ) - print 'computing disparity...' + print('computing disparity...') disp = stereo.compute(imgL, imgR).astype(np.float32) / 16.0 - print 'generating 3d point cloud...', + print('generating 3d point cloud...',) h, w = imgL.shape[:2] f = 0.8*w # guess for focal length Q = np.float32([[1, 0, 0, -0.5*w], @@ -67,7 +70,7 @@ if __name__ == '__main__': out_colors = colors[mask] out_fn = 'out.ply' write_ply('out.ply', out_points, out_colors) - print '%s saved' % 'out.ply' + print('%s saved' % 'out.ply') cv2.imshow('left', imgL) cv2.imshow('disparity', (disp-min_disp)/num_disp) diff --git a/samples/python2/texture_flow.py b/samples/python/texture_flow.py similarity index 86% rename from samples/python2/texture_flow.py rename to samples/python/texture_flow.py index e47c4b8e5..fe835fbe5 100755 --- a/samples/python2/texture_flow.py +++ b/samples/python/texture_flow.py @@ -10,6 +10,9 @@ Usage: texture_flow.py [] ''' +# Python 2/3 compatibility +from __future__ import print_function + import numpy as np import cv2 @@ -22,7 +25,7 @@ if __name__ == '__main__': img = cv2.imread(fn) if img is None: - print 'Failed to load image file:', fn + print('Failed to load image file:', fn) sys.exit(1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) @@ -36,7 +39,7 @@ if __name__ == '__main__': vis[:] = (192 + np.uint32(vis)) / 2 d = 12 points = np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2) - for x, y in points: + for x, y in np.int32(points): vx, vy = np.int32(flow[y, x]*d) cv2.line(vis, (x-vx, y-vy), (x+vx, y+vy), (0, 0, 0), 1, cv2.LINE_AA) cv2.imshow('input', img) diff --git a/samples/python2/turing.py b/samples/python/turing.py similarity index 100% rename from samples/python2/turing.py rename to samples/python/turing.py diff --git a/samples/python2/video.py b/samples/python/video.py similarity index 99% rename from samples/python2/video.py rename to samples/python/video.py index 8bdec7440..52faaae07 100755 --- a/samples/python2/video.py +++ b/samples/python/video.py @@ -182,7 +182,7 @@ if __name__ == '__main__': if len(sources) == 0: sources = [ 0 ] - caps = map(create_capture, sources) + caps = list(map(create_capture, sources)) shot_idx = 0 while True: imgs = [] diff --git a/samples/python2/video_threaded.py b/samples/python/video_threaded.py similarity index 100% rename from samples/python2/video_threaded.py rename to samples/python/video_threaded.py diff --git a/samples/python2/video_v4l2.py b/samples/python/video_v4l2.py similarity index 84% rename from samples/python2/video_v4l2.py rename to samples/python/video_v4l2.py index 0f03e6bf0..9ef958e69 100644 --- a/samples/python2/video_v4l2.py +++ b/samples/python/video_v4l2.py @@ -14,6 +14,9 @@ Keys: ''' +# Python 2/3 compatibility +from __future__ import print_function + import cv2 def decode_fourcc(v): @@ -24,13 +27,13 @@ font = cv2.FONT_HERSHEY_SIMPLEX color = (0, 255, 0) cap = cv2.VideoCapture(0) -cap.set(cv2.CAP_PROP_AUTOFOCUS, False) +cap.set(cv2.CAP_PROP_AUTOFOCUS, False) # Known bug: https://github.com/Itseez/opencv/pull/5474 cv2.namedWindow("Video") convert_rgb = True fps = int(cap.get(cv2.CAP_PROP_FPS)) -focus = int(cap.get(cv2.CAP_PROP_FOCUS)) * 100 +focus = int(min(cap.get(cv2.CAP_PROP_FOCUS) * 100, 2**31-1)) # ceil focus to C_LONG as Python3 int can go to +inf cv2.createTrackbar("FPS", "Video", fps, 30, lambda v: cap.set(cv2.CAP_PROP_FPS, v)) cv2.createTrackbar("Focus", "Video", focus, 100, lambda v: cap.set(cv2.CAP_PROP_FOCUS, v / 100)) @@ -55,7 +58,7 @@ while True: cv2.putText(img, "FPS: {}".format(fps), (15, 80), font, 1.0, color) cv2.imshow("Video", img) - k = cv2.waitKey(1) + k = 0xFF & cv2.waitKey(1) if k == 27: break diff --git a/samples/python2/watershed.py b/samples/python/watershed.py similarity index 87% rename from samples/python2/watershed.py rename to samples/python/watershed.py index 75a24a653..6d349e1c1 100755 --- a/samples/python2/watershed.py +++ b/samples/python/watershed.py @@ -22,7 +22,8 @@ Keys ''' - +# Python 2/3 compatibility +from __future__ import print_function import numpy as np import cv2 @@ -44,7 +45,7 @@ class App: self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors) def get_colors(self): - return map(int, self.colors[self.cur_marker]), self.cur_marker + return list(map(int, self.colors[self.cur_marker])), self.cur_marker def watershed(self): m = self.markers.copy() @@ -60,13 +61,13 @@ class App: break if ch >= ord('1') and ch <= ord('7'): self.cur_marker = ch - ord('0') - print 'marker: ', self.cur_marker + print('marker: ', self.cur_marker) if ch == ord(' ') or (self.sketch.dirty and self.auto_update): self.watershed() self.sketch.dirty = False if ch in [ord('a'), ord('A')]: self.auto_update = not self.auto_update - print 'auto_update if', ['off', 'on'][self.auto_update] + print('auto_update if', ['off', 'on'][self.auto_update]) if ch in [ord('r'), ord('R')]: self.markers[:] = 0 self.markers_vis[:] = self.img @@ -80,5 +81,5 @@ if __name__ == '__main__': fn = sys.argv[1] except: fn = '../data/fruits.jpg' - print __doc__ + print(__doc__) App(fn).run() diff --git a/samples/python2/_doc.py b/samples/python2/_doc.py deleted file mode 100755 index fe2b6f32b..000000000 --- a/samples/python2/_doc.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python - -''' -Scans current directory for *.py files and reports -ones with missing __doc__ string. -''' - -from glob import glob - -if __name__ == '__main__': - print '--- undocumented files:' - for fn in glob('*.py'): - loc = {} - execfile(fn, loc) - if '__doc__' not in loc: - print fn diff --git a/samples/python2/calibrate.py b/samples/python2/calibrate.py deleted file mode 100755 index 24e4aa7d0..000000000 --- a/samples/python2/calibrate.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python - -# Python 2/3 compatibility -from __future__ import print_function - -import numpy as np -import cv2 - -# local modules -from common import splitfn - -# built-in modules -import os - - -USAGE = ''' -USAGE: calib.py [--save ] [--debug ] [--square_size] [] -''' - - - -if __name__ == '__main__': - import sys - import getopt - from glob import glob - - args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size=']) - args = dict(args) - try: - img_mask = img_mask[0] - except: - img_mask = '../data/left*.jpg' - - img_names = glob(img_mask) - debug_dir = args.get('--debug') - square_size = float(args.get('--square_size', 1.0)) - - pattern_size = (9, 6) - pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) - pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2) - pattern_points *= square_size - - obj_points = [] - img_points = [] - h, w = 0, 0 - for fn in img_names: - print('processing %s...' % fn,) - img = cv2.imread(fn, 0) - if img is None: - print("Failed to load", fn) - continue - - h, w = img.shape[:2] - found, corners = cv2.findChessboardCorners(img, pattern_size) - if found: - term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 ) - cv2.cornerSubPix(img, corners, (5, 5), (-1, -1), term) - if debug_dir: - vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) - cv2.drawChessboardCorners(vis, pattern_size, corners, found) - path, name, ext = splitfn(fn) - cv2.imwrite('%s/%s_chess.bmp' % (debug_dir, name), vis) - if not found: - print('chessboard not found') - continue - img_points.append(corners.reshape(-1, 2)) - obj_points.append(pattern_points) - - print('ok') - - rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), None, None) - print("RMS:", rms) - print("camera matrix:\n", camera_matrix) - print("distortion coefficients: ", dist_coefs.ravel()) - cv2.destroyAllWindows() diff --git a/samples/tapi/ufacedetect.cpp b/samples/tapi/ufacedetect.cpp index e3c82a2c6..f6dc0ce1d 100644 --- a/samples/tapi/ufacedetect.cpp +++ b/samples/tapi/ufacedetect.cpp @@ -36,56 +36,37 @@ int main( int argc, const char** argv ) VideoCapture capture; UMat frame, image; Mat canvas; - const string scaleOpt = "--scale="; - size_t scaleOptLen = scaleOpt.length(); - const string cascadeOpt = "--cascade="; - size_t cascadeOptLen = cascadeOpt.length(); - const string nestedCascadeOpt = "--nested-cascade"; - size_t nestedCascadeOptLen = nestedCascadeOpt.length(); - const string tryFlipOpt = "--try-flip"; - size_t tryFlipOptLen = tryFlipOpt.length(); - String inputName; - bool tryflip = false; - help(); + string inputName; + bool tryflip; CascadeClassifier cascade, nestedCascade; - double scale = 1; + double scale; - for( int i = 1; i < argc; i++ ) + cv::CommandLineParser parser(argc, argv, + "{cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}" + "{nested-cascade|../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}" + "{help h ||}{scale|1|}{try-flip||}{@filename||}" + ); + if (parser.has("help")) { - cout << "Processing " << i << " " << argv[i] << endl; - if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 ) - { - cascadeName.assign( argv[i] + cascadeOptLen ); - cout << " from which we have cascadeName= " << cascadeName << endl; - } - else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 ) - { - if( argv[i][nestedCascadeOpt.length()] == '=' ) - nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 ); - if( !nestedCascade.load( nestedCascadeName ) ) - cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; - } - else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 ) - { - if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) ) - scale = 1; - cout << " from which we read scale = " << scale << endl; - } - else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 ) - { - tryflip = true; - cout << " will try to flip image horizontally to detect assymetric objects\n"; - } - else if( argv[i][0] == '-' ) - { - cerr << "WARNING: Unknown option " << argv[i] << endl; - } - else - inputName = argv[i]; + help(); + return 0; + } + cascadeName = parser.get("cascade"); + nestedCascadeName = parser.get("nested-cascade"); + scale = parser.get("scale"); + tryflip = parser.has("try-flip"); + inputName = parser.get("@filename"); + if ( !parser.check()) + { + parser.printErrors(); + help(); + return -1; } + if ( !nestedCascade.load( nestedCascadeName ) ) + cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; if( !cascade.load( cascadeName ) ) { cerr << "ERROR: Could not load classifier cascade" << endl; @@ -95,9 +76,9 @@ int main( int argc, const char** argv ) cout << "old cascade: " << (cascade.isOldFormatCascade() ? "TRUE" : "FALSE") << endl; - if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') ) + if( inputName.empty() || (isdigit(inputName[0]) && inputName.size() == 1) ) { - int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0'; + int c = inputName.empty() ? 0 : inputName[0] - '0'; if(!capture.open(c)) cout << "Capture from camera #" << c << " didn't work" << endl; }