• Congxiang Pan's avatar
    Merge pull request #9466 from huningxin:js · 89b6e68e
    Congxiang Pan authored
    GSoC 2017: Improve and Extend the JavaScript Bindings for OpenCV (#9466)
    
    * Initial support for build with emscripten
    
    mkdir build_js
    cd build_js
    cmake -D CMAKE_TOOLCHAIN_FILE=/path/to/emsdk/emsdk-portable/emscripten/master/cmake/Modules/Platform/Emscripten.cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
    
    * Add js module
    
    The output is build/bin/opencv_js.js
    
    * Fix opencv2/calib3d.hpp not found issue
    
    * Add module name
    
    Usage:
    var cv = cv();
    
    * Add total memory as 128MB and allow growth
    
    * Add compilation flags for emscripten
    
    * Use EMSCRIPTEN build target
    
    * Disable js module for non emscripten build
    
    * Bind the preload file path to root
    
    Usage:
    face_cascade.load('haarcascade_frontalface_default.xml');
    
    * add test folder
    
    * fix test files
    
    * Copy js module test to bin
    
    * Support to run tests on Node.js
    
    Fix tests to import cv Module when runtime is node.
    Add tests.js to use qunit to auto run tests.
    Modify umd wrapper to support Module is not defined.
    
    Usage:
    node tests.js
    
    * Support UMD and file system
    
    Wrap the opencv_js.js to opencv.js by UMD wrapper
    
    Use emscripten file system API to load files instead of generating data file or
    embedding them. It supports both browser and node.js usages.
    
    * Fix incorrect module name in tests
    
    * Add package.json to add dependence of qunit
    
    * Add js_tutorials folder and a intro page of opencv.js
    
    Enable BUILD_DOCS in CMakeLists.txt.
    Add new folder of js_tutorials in folder opencv/doc.
    Imitate the tutorials of OpenCV-Python to create a intro page of opencv.js and a setup guide
    
    * Import and use binding gen from opencvjs project
    
    * Modify the embindgen.py to pass the build and test
    
    * Add classes and functions white list
    
    * Consolidate hdr_parser.py (#31)
    
    Use hdr_parser.py of python module
    
    Add js flag to support js binding generator.
    
    * Use emscripten::vecFromJSArray for input vector param
    
    Fix part of #23
    
    * Fix test cases after #34
    
    Fix #39
    
    * Expose groupRectangles and CascadeClassifier.empty
    
    * Add js highgui tutorials
    
    add tutorials of imread&imshow and createTrackbar in doc/js_tutorials/js_gui folder
    add interactive tutorial webpage for imread&imshow and createTrackbar in doc/js_tutorials/js_interactive_tutorials folder, and some images needed.
    change doc/CMakeLists.txt to copy the interactive tutorial webpage and opencv.js to the tutorials' destination folder
    
    * rm useless annotation in doc/CMakeLists.txt
    
    * fix some nonstandard indentation and space
    
    * add check if canvas is valid
    
    * Expose BackgroundSubtractorMOG2
    
    Fix #43
    
    * Fix build of js doc
    
    Limit copy_js_interactive_tutorials for doxygen build
    Add dep to opencv.js
    
    Fix #53
    
    * Implement cv.imread & cv.imshow and insert interactive pages in tutorials (#55)
    
    * add helper.js
    
    * delete ALL in add target copy_js_interactive_tutorials to avoid dependence error
    
    * Insert interactive pages in tutorials
    
    insert the old interactive pages in markdown by using \htmlonly and \endhtmlonly command.
    delete the useless interactive page
    rename js_interactive_tutorials to js_assets to put some images needed in
    
    * fix the depends of the target doxygen
    
    add opencv.js to depends and delete the useless target of copy_js_assets
    
    * change filename helper.js to helpers.js
    
    * disable button or trankbar before opencv.js is ready
    
    * Expose CV_64F
    
    Fix #65
    
    * improve cv.imshow to display different types as native imshow
    
    * add utils.js to reuse functions and update tutorials
    
    * Make doxygen depend on bin/opencv.js
    
    * Fix memory issue of matFromArray
    
    Fix #37
    
    * Merge pull request from ganwenyao/tutorial_18
    
    * Add notes for ganwenyao/tutorial_18
    
    * Modifying for ganwenyao/tutorial_18
    
    * Change Mat constructor with data to 5 parameters
    
    * Mat supports constructor with Scalar
    
    Fix #60
    
    * update cv.imread cause the memory issue of matFromArray has been fixed
    
    * fix canvas name and default input image
    
    * Expose cv::Moments
    
    Fix #85
    
    * Add -Wno-missing-prototypes for emscripten build
    
    * fix canvas name
    
    * add tutorial of video input and output
    
    * Expose enums as emscripten consts
    
    Fix #72
    
    * update the tutorial to use Mat constructor with Scalar and change lena.jpg
    
    * Exclude cv::Mat for vecFromJSArray
    
    Fix #82
    
    * Add unit tests for cv.moments
    
    * Fix the unit tests.
    
    * add checkbox and stop button
    
    * add adapter.js to make sure compatibility fo video tutorials
    
    * Support default parameters with function overloading
    
    * modify enums to constants
    
    * Use https URL for MathJax.js
    
    Fix #109
    
    * Comment out the debug print in embindgen.py
    
    * Expose RotatedRect
    
    Fix #96
    
    * replace enum with constants and improve onload function
    
    * delete some useless paras cause #105 fixed this
    
    * Modify const name
    
    * Modify Contour Properties
    
    * tutorials for imgprc2 and objdec
    
    * Expose more functions for img proc tutorials
    
    Fix #76
    
    * Expose polylines for video analysis tutorial
    
    Fix #121
    
    * Expose constants for default parameters of img proc tutorials
    
    Fix #122
    
    * Fix wrong parameter types of Mat.copyTo
    
    Fix #87
    
    * Support default parameters of mat.convertTo
    
    Fix #123
    
    * Support default parameters for external constructors
    
    Fix #131
    
    * Revert "Expose polylines for video analysis tutorial"
    
    This reverts commit 3ce7615652e510d30e3c0014706ac38c98883189.
    
    Fix #121
    
    * Support cv.minMaxLoc
    
    Fix #127
    
    * Expose cv.minEnclosingCircle
    
    Fix #126
    
    * Add video analysis tutorials
    
    add three video tutorials, Meanshift and Camshift, Optical Flow Background Subtraction
    add cup.mp4 and box.mp4 for demo in tutorials
    
    * improve image processing tutorials
    
    * repalce console.warn with throw to throw exception
    
    * add try-catch to throw exception in code demo
    
    * Change mat.size() return value to JS Array object
    
    Fix #140
    
    * add a note about different channels order between canvas and native opencv
    
    * add a note about how to capture video from video files
    
    * Binding cv.Scalar to JS array
    
    Fix #147
    
    * Add JS cv.Scalar object into helpers.js
    
    * Update Install OpenCV-JavaScript tutorial page
    
    Fix #44
    
    * Update the OpenCV-JavaScript introduction page
    
    Fix #44
    
    * add cv.VideoCapture and read() function
    
    * set the size of the hidden canvas same as the video
    
    * Add Using OpenCV-JavaScript tutorial page
    
    Fix #44
    
    * fix some bad code style
    
    * Update tutorials after 8/2 sync meeting
    
    Changes include:
    - Use OpenCV.js name instead of OpenCV-JavaScript
    - Put using OpenCV.js ahead of build OpenCV.js
    - Refine usage and introduction page
    - Muted the video in tutorials
    
    * Fix a typo in introduction page
    
    * use cv.VideoCapture and its read() function to read video
    
    * replace OpenCV-JavaScript with OpenCV.js
    
    * Use onload of async script in js_usage tutorial
    
    * add more info about mat.data
    
    * Change Size to value_object
    
    * Integrate Moh and Sajjad's editing into introduction page
    
    * Change Point to value_object
    
    * Change Rect to value_object with helper object
    
    * Add helper objects for Point and Size
    
    * Change RotatedRect to value_object with helpers
    
    * Change MinMaxLoc and Circle to value_object
    
    * Change TermCriteria to value_object
    
    * Fix core_bindings.cpp for MinMaxLoc and Circle
    
    * Remove unused types
    
    * Change meanShift and CamShift to return Rect
    
    * Change methods of RotatedRect to static
    
    * Change mat.data from methods to property
    
    Fix #75 and #77
    
    * support img id and element in cv.imread
    
    * Change mat.size to property and add mat.step
    
    Fix #163
    
    * Add matFromArray and matFromImageData as JS helpers
    
    Fix #79, #78
    
    * Lower camel case for Mat element getters
    
    Fix #81
    
    * Mat.getRoiRect and tests
    
    Fix #86
    
    * Support type for Mat.ptr
    
    Fix #83
    
    * Name changing of Mat element getters
    
    'getUcharAt` -> 'ucharAt'
    
    * fix code style and args names
    
    * Fix helpers.js due to cv.Mat API update
    
    * Fix opencv.js usage tutorial
    
    * Fix a typo of js_setup
    
    * Change Moments to value_object
    
    * Add Range as value_object
    
    Fix #171
    
    * Support Mat.diag and Mat.isContinous
    
    Fix #84 and #89
    
    * Support Mat.setTo
    
    Fix #88
    
    * Apply edits to js_intro
    
    * Apply edits to js_usage
    
    * Apply edits to js_setup
    
    * update tutorials to apply data type change
    
    * Modify tutorials
    
    * add core tutorials
    
    * delete MatVector elements and delete useless set operation
    
    * add tutorials_objdec_camera
    
    * Add instructions for WebAssembly
    
    * apply tech writer's feedbacks into tutorials
    
    * Organize white list by modules
    
    * Change size to method and bind to MatExpr.size()
    
    Fix #177
    
    * improve tutorials
    
    * Modify core tutorials
    
    * add params list and explanations for OpenCV.js functions
    
    * remove face_profile from Face Detection in Video Capture
    
    * Add demos link
    
    * Change Gui to GUI
    
    * Update js_intro based on Moh and Sajjad's edits
    
    * Fixup for 3.3.0 rebase
    
    * Update js_intro per Moh's suggestion
    
    * Update contributors list per Moh's idea
    
    * add adapter.js in video_display tutorial
    
    * Change Mat.getRoiRect to Mat.roi
    
    Fix #194
    
    * Remove unnecessary files for test
    
    Fix #192
    
    * Licenses updated to UC BSD 3-Clause
    
    * Apply OpenCV coding style for C++ files
    
    * Add OpenCV license for python and js files
    
    * Fix coding style issue in helpers.js
    
    * Remove unused test_commons.js
    
    * Fix coding style of test_imgproc.js
    
    * Fix coding style of test_mat.js
    
    * Fix space before semicolon
    
    * Fix coding style of test_objdetect.js
    
    * Fix coding style of tests.js
    
    * Fix coding style of test_utils.js
    
    * Fix coding style of test_video.js
    
    * Fix failures of node.js tests
    
    * Add eslint rule config and fix eslint errors
    
    * Add eslint config for js/src and fix eslint errors
    
    * Clean up the opencv.js dependencies
    
    Fix #186
    
    * Fix build issue for python generator
    
    * Fix doxygen buildbot failure
    
    * delete trailing whitespace, blank line at EOF and replace tab with space
    
    * Fix tutorial_js_root reference issue for non opencv.js build
    
    * replace the file with small size
    
    * Initial commit of build_js.py
    
    * Move the js build configurations to build script
    
    * Add wasm build support
    
    * Update OpenCV.js build tutorial by using script
    
    * Fix global var issue in tests
    
    * Add a README.md for build_js.py
    
    * Copy the haar cascade files from data dir for tutorials
    
    * Not use memory init file
    
    * Disable debug print for modules/js/CMakeLists.txt
    
    * Check files when build done
    
    * Fix image name in js_gradients tutorial
    
    * Fix image load issue in js_trackbar tutorial
    
    * Find the opencv source directory via relative path by default
    
    * Make the cmake args based on build_doc option
    
    * Fix a typo in js_setup.markdown
    
    * Fix make failure issue on config generated by build_js.py
    
    * Eliminate js branch of hdr_parser.py
    
    * Extract examples from js_basic_ops tutorial
    
    * Fix coding style of utils.js
    
    * Improve examples error handling
    
    Handle:
    1. opencv.js loading errors
    2. script errors (Error)
    3. cv::Exception
    
    Fix #217
    
    * Add enable_exception option into build_js.py
    
    * Support print exception for exception catching disabled build
    
    * Extract example from js_usage tutorial
    
    * Avoid copying .eslintrc.json when building doc
    
    Fix #223
    
    * Revert to use onload as opencv.js ready event
    
    * Use 4 spaces indention for js examples
    
    * embed html in tutorials with iframe tag
    
    * Revert to use onload as opencv.js ready event
    
    * Extract examples from js_video_display tutorial
    
    * Implement Utils object
    
    * modify core imgprc and face_detection tutorials
    
    * Fix examples of js_gui tutorials
    
    * Fix coding style of utils.js
    
    * Modify tutorials
    
    * Extract example from js_face_detection_camera tutorial
    
    * Disable new-cap check in eslint
    
    * Extract examples from js_meanshift tutorial
    
    * Extract examples from video tutorials
    
    * Remove new-cap declaration and update grammer in comments
    
    * Change textarea width to 100 to align with eslint config
    
    * Fix printError issue when opencv.js loading fails
    
    * Remove BUILD_opencv_js dependency for doc build
    
    Fix #213
    
    * Expose cv::getBuildInformation
    
    * Dump opencv build info when opencv.js loaded for live examples
    
    * Make the button to stand out in js live examples
    
    Fix #235
    
    * Style for disabled button
    
    * Add js_imgproc_camera.html example
    
    * Fix coding style of imgproc_camera example
    
    * Add js_imgproc_camera tutorial
    
    * Remove link to opencv.js demos
    
    * doc: copy opencv.js on build, use absolute paths for assets
    
    * doc: reuse existed file box.mp4
    89b6e68e
test_mat.js 30.1 KB
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
// //////////////////////////////////////////////////////////////////////////////////////
//
//  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) 2013, OpenCV Foundation, 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.
//
//

// //////////////////////////////////////////////////////////////////////////////////////
// Author: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
//
//                             LICENSE AGREEMENT
// Copyright (c) 2015 The Regents of the University of California (Regents)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. 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.
// 3. Neither the name of the University 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 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.
//

if (typeof module !== 'undefined' && module.exports) {
    // The envrionment is Node.js
    var cv = require('./opencv.js'); // eslint-disable-line no-var
}

QUnit.module('Core', {});

QUnit.test('test_mat_creation', function(assert) {
    // Mat constructors.
    // Mat::Mat(int rows, int cols, int type)
    {
        let mat = new cv.Mat(10, 20, cv.CV_8UC3);

        assert.equal(mat.type(), cv.CV_8UC3);
        assert.equal(mat.depth(), cv.CV_8U);
        assert.equal(mat.channels(), 3);
        assert.ok(mat.empty() === false);

        let size = mat.size();
        assert.equal(size.height, 10);
        assert.equal(size.width, 20);

        mat.delete();
    }

    // Mat::Mat(const Mat &)
    {
        // Copy from another Mat
        let mat1 = new cv.Mat(10, 20, cv.CV_8UC3);
        let mat2 = new cv.Mat(mat1);

        assert.equal(mat2.type(), mat1.type());
        assert.equal(mat2.depth(), mat1.depth());
        assert.equal(mat2.channels(), mat1.channels());
        assert.equal(mat2.empty(), mat1.empty());

        let size1 = mat1.size;
        let size2 = mat2.size();
        assert.ok(size1[0] === size2[0]);
        assert.ok(size1[1] === size2[1]);

        mat1.delete();
        mat2.delete();
    }

    // Mat::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
    {
        // 10 * 10 and one channel
        let data = cv._malloc(10 * 10 * 1);
        let mat = new cv.Mat(10, 10, cv.CV_8UC1, data, 0);

        assert.equal(mat.type(), cv.CV_8UC1);
        assert.equal(mat.depth(), cv.CV_8U);
        assert.equal(mat.channels(), 1);
        assert.ok(mat.empty() === false);

        let size = mat.size();
        assert.ok(size.height === 10);
        assert.ok(size.width === 10);

        mat.delete();
    }

    // Mat::Mat(int rows, int cols, int type, const Scalar& scalar)
    {
        // 2 * 2 8UC4 mat
        let mat = new cv.Mat(2, 2, cv.CV_8UC4, [0, 1, 2, 3]);

        for (let r = 0; r < mat.rows; r++) {
            for (let c = 0; c < mat.cols; c++) {
                let element = mat.ptr(r, c);
                assert.equal(element[0], 0);
                assert.equal(element[1], 1);
                assert.equal(element[2], 2);
                assert.equal(element[3], 3);
            }
        }

        mat.delete();
    }

    //  Mat::create(int, int, int)
    {
        let mat = new cv.Mat();
        mat.create(10, 5, cv.CV_8UC3);
        let size = mat.size();

        assert.ok(mat.type() === cv.CV_8UC3);
        assert.ok(size.height === 10);
        assert.ok(size.width === 5);
        assert.ok(mat.channels() === 3);

        mat.delete();
    }
    //  Mat::create(Size, int)
    {
        let mat = new cv.Mat();
        mat.create({height: 10, width: 5}, cv.CV_8UC4);
        let size = mat.size();

        assert.ok(mat.type() === cv.CV_8UC4);
        assert.ok(size.height === 10);
        assert.ok(size.width === 5);
        assert.ok(mat.channels() === 4);

        mat.delete();
    }
    //   clone
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
        let mat2 = mat.clone();

        assert.equal(mat.channels, mat2.channels);
        assert.equal(mat.size().height, mat2.size().height);
        assert.equal(mat.size().width, mat2.size().width);

        assert.deepEqual(mat.data, mat2.data);


        mat.delete();
        mat2.delete();
    }
    // copyTo
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
        let mat2 = new cv.Mat();
        mat.copyTo(mat2);

        assert.equal(mat.channels, mat2.channels);
        assert.equal(mat.size().height, mat2.size().height);
        assert.equal(mat.size().width, mat2.size().width);

        assert.deepEqual(mat.data, mat2.data);


        mat.delete();
        mat2.delete();
    }
    // copyTo1
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
        let mat2 = new cv.Mat();
        let mask = new cv.Mat(5, 5, cv.CV_8UC1, new cv.Scalar(1));
        mat.copyTo(mat2, mask);

        assert.equal(mat.channels, mat2.channels);
        assert.equal(mat.size().height, mat2.size().height);
        assert.equal(mat.size().width, mat2.size().width);

        assert.deepEqual(mat.data, mat2.data);


        mat.delete();
        mat2.delete();
        mask.delete();
    }

    // matFromArray
    {
        let arrayC1 = [0, -1, 2, -3];
        let arrayC2 = [0, -1, 2, -3, 4, -5, 6, -7];
        let arrayC3 = [0, -1, 2, -3, 4, -5, 6, -7, 9, -9, 10, -11];
        let arrayC4 = [0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, 13, 14, 15];

        let mat8UC1 = cv.matFromArray(2, 2, cv.CV_8UC1, arrayC1);
        let mat8UC2 = cv.matFromArray(2, 2, cv.CV_8UC2, arrayC2);
        let mat8UC3 = cv.matFromArray(2, 2, cv.CV_8UC3, arrayC3);
        let mat8UC4 = cv.matFromArray(2, 2, cv.CV_8UC4, arrayC4);

        let mat8SC1 = cv.matFromArray(2, 2, cv.CV_8SC1, arrayC1);
        let mat8SC2 = cv.matFromArray(2, 2, cv.CV_8SC2, arrayC2);
        let mat8SC3 = cv.matFromArray(2, 2, cv.CV_8SC3, arrayC3);
        let mat8SC4 = cv.matFromArray(2, 2, cv.CV_8SC4, arrayC4);

        let mat16UC1 = cv.matFromArray(2, 2, cv.CV_16UC1, arrayC1);
        let mat16UC2 = cv.matFromArray(2, 2, cv.CV_16UC2, arrayC2);
        let mat16UC3 = cv.matFromArray(2, 2, cv.CV_16UC3, arrayC3);
        let mat16UC4 = cv.matFromArray(2, 2, cv.CV_16UC4, arrayC4);

        let mat16SC1 = cv.matFromArray(2, 2, cv.CV_16SC1, arrayC1);
        let mat16SC2 = cv.matFromArray(2, 2, cv.CV_16SC2, arrayC2);
        let mat16SC3 = cv.matFromArray(2, 2, cv.CV_16SC3, arrayC3);
        let mat16SC4 = cv.matFromArray(2, 2, cv.CV_16SC4, arrayC4);

        let mat32SC1 = cv.matFromArray(2, 2, cv.CV_32SC1, arrayC1);
        let mat32SC2 = cv.matFromArray(2, 2, cv.CV_32SC2, arrayC2);
        let mat32SC3 = cv.matFromArray(2, 2, cv.CV_32SC3, arrayC3);
        let mat32SC4 = cv.matFromArray(2, 2, cv.CV_32SC4, arrayC4);

        let mat32FC1 = cv.matFromArray(2, 2, cv.CV_32FC1, arrayC1);
        let mat32FC2 = cv.matFromArray(2, 2, cv.CV_32FC2, arrayC2);
        let mat32FC3 = cv.matFromArray(2, 2, cv.CV_32FC3, arrayC3);
        let mat32FC4 = cv.matFromArray(2, 2, cv.CV_32FC4, arrayC4);

        let mat64FC1 = cv.matFromArray(2, 2, cv.CV_64FC1, arrayC1);
        let mat64FC2 = cv.matFromArray(2, 2, cv.CV_64FC2, arrayC2);
        let mat64FC3 = cv.matFromArray(2, 2, cv.CV_64FC3, arrayC3);
        let mat64FC4 = cv.matFromArray(2, 2, cv.CV_64FC4, arrayC4);

        assert.deepEqual(mat8UC1.data, new Uint8Array(arrayC1));
        assert.deepEqual(mat8UC2.data, new Uint8Array(arrayC2));
        assert.deepEqual(mat8UC3.data, new Uint8Array(arrayC3));
        assert.deepEqual(mat8UC4.data, new Uint8Array(arrayC4));

        assert.deepEqual(mat8SC1.data8S, new Int8Array(arrayC1));
        assert.deepEqual(mat8SC2.data8S, new Int8Array(arrayC2));
        assert.deepEqual(mat8SC3.data8S, new Int8Array(arrayC3));
        assert.deepEqual(mat8SC4.data8S, new Int8Array(arrayC4));

        assert.deepEqual(mat16UC1.data16U, new Uint16Array(arrayC1));
        assert.deepEqual(mat16UC2.data16U, new Uint16Array(arrayC2));
        assert.deepEqual(mat16UC3.data16U, new Uint16Array(arrayC3));
        assert.deepEqual(mat16UC4.data16U, new Uint16Array(arrayC4));

        assert.deepEqual(mat16SC1.data16S, new Int16Array(arrayC1));
        assert.deepEqual(mat16SC2.data16S, new Int16Array(arrayC2));
        assert.deepEqual(mat16SC3.data16S, new Int16Array(arrayC3));
        assert.deepEqual(mat16SC4.data16S, new Int16Array(arrayC4));

        assert.deepEqual(mat32SC1.data32S, new Int32Array(arrayC1));
        assert.deepEqual(mat32SC2.data32S, new Int32Array(arrayC2));
        assert.deepEqual(mat32SC3.data32S, new Int32Array(arrayC3));
        assert.deepEqual(mat32SC4.data32S, new Int32Array(arrayC4));

        assert.deepEqual(mat32FC1.data32F, new Float32Array(arrayC1));
        assert.deepEqual(mat32FC2.data32F, new Float32Array(arrayC2));
        assert.deepEqual(mat32FC3.data32F, new Float32Array(arrayC3));
        assert.deepEqual(mat32FC4.data32F, new Float32Array(arrayC4));

        assert.deepEqual(mat64FC1.data64F, new Float64Array(arrayC1));
        assert.deepEqual(mat64FC2.data64F, new Float64Array(arrayC2));
        assert.deepEqual(mat64FC3.data64F, new Float64Array(arrayC3));
        assert.deepEqual(mat64FC4.data64F, new Float64Array(arrayC4));

        mat8UC1.delete();
        mat8UC2.delete();
        mat8UC3.delete();
        mat8UC4.delete();
        mat8SC1.delete();
        mat8SC2.delete();
        mat8SC3.delete();
        mat8SC4.delete();
        mat16UC1.delete();
        mat16UC2.delete();
        mat16UC3.delete();
        mat16UC4.delete();
        mat16SC1.delete();
        mat16SC2.delete();
        mat16SC3.delete();
        mat16SC4.delete();
        mat32SC1.delete();
        mat32SC2.delete();
        mat32SC3.delete();
        mat32SC4.delete();
        mat32FC1.delete();
        mat32FC2.delete();
        mat32FC3.delete();
        mat32FC4.delete();
        mat64FC1.delete();
        mat64FC2.delete();
        mat64FC3.delete();
        mat64FC4.delete();
    }

    // matFromImageData
    {
        // Only test in browser
        if (typeof window === 'undefined') {
            return;
        }
        let canvas = window.document.createElement('canvas');
        canvas.width = 2;
        canvas.height = 2;
        let ctx = canvas.getContext('2d');
        ctx.fillStyle='#FF0000';
        ctx.fillRect(0, 0, 1, 1);
        ctx.fillRect(1, 1, 1, 1);

        let imageData = ctx.getImageData(0, 0, 2, 2);
        let mat = cv.matFromImageData(imageData);

        assert.deepEqual(mat.data, new Uint8Array(imageData.data));

        mat.delete();
    }

    // Mat(mat)
    {
        let mat = new cv.Mat(2, 2, cv.CV_8UC4, new cv.Scalar(1, 0, 1, 0));
        let mat1 = new cv.Mat(mat);
        let mat2 = mat;

        assert.equal(mat.rows, mat1.rows);
        assert.equal(mat.cols, mat1.cols);
        assert.equal(mat.type(), mat1.type());
        assert.deepEqual(mat.data, mat1.data);

        mat.delete();

        assert.equal(mat1.isDeleted(), false);
        assert.equal(mat2.isDeleted(), true);

        mat1.delete();
    }

    // mat.setTo
    {
        let mat = new cv.Mat(2, 2, cv.CV_8UC4);
        let s = [0, 1, 2, 3];

        mat.setTo(s);

        assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s));
        assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
        assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s));
        assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));

        let s1 = [0, 0, 0, 0];
        mat.setTo(s1);
        let mask = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 0, 1]);
        mat.setTo(s, mask);

        assert.deepEqual(mat.ptr(0, 0), new Uint8Array(s1));
        assert.deepEqual(mat.ptr(0, 1), new Uint8Array(s));
        assert.deepEqual(mat.ptr(1, 0), new Uint8Array(s1));
        assert.deepEqual(mat.ptr(1, 1), new Uint8Array(s));

        mat.delete();
        mask.delete();
    }
});

QUnit.test('test_mat_ptr', function(assert) {
    const RValue = 3;
    const GValue = 7;
    const BValue = 197;

    // cv.CV_8UC1 + Mat::ptr(int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_8UC1);
        let view = mat.data;

        // Alter matrix[2, 1].
        let step = 10;
        view[2 * step + 1] = RValue;

        // Access matrix[2, 1].
        view = mat.ptr(2);

        assert.equal(view[1], RValue);

        mat.delete();
    }

    // cv.CV_8UC3 + Mat::ptr(int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_8UC3);
        let view = mat.data;

        // Alter matrix[2, 1].
        let step = 3 * 10;
        view[2 * step + 3] = RValue;
        view[2 * step + 3 + 1] = GValue;
        view[2 * step + 3 + 2] = BValue;

        // Access matrix[2, 1].
        view = mat.ptr(2);

        assert.equal(view[3], RValue);
        assert.equal(view[3 + 1], GValue);
        assert.equal(view[3 + 2], BValue);

        mat.delete();
    }

    // cv.CV_8UC3 + Mat::ptr(int, int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_8UC3);
        let view = mat.data;

        // Alter matrix[2, 1].
        let step = 3 * 10;
        view[2 * step + 3] = RValue;
        view[2 * step + 3 + 1] = GValue;
        view[2 * step + 3 + 2] = BValue;

        // Access matrix[2, 1].
        view = mat.ptr(2, 1);

        assert.equal(view[0], RValue);
        assert.equal(view[1], GValue);
        assert.equal(view[2], BValue);

        mat.delete();
    }

    const RValueF32 = 3.3;
    const GValueF32 = 7.3;
    const BValueF32 = 197.3;
    const EPSILON = 0.001;

    // cv.CV_32FC1 + Mat::ptr(int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_32FC1);
        let view = mat.data32F;

        // Alter matrix[2, 1].
        let step = 10;
        view[2 * step + 1] = RValueF32;

        // Access matrix[2, 1].
        view = mat.floatPtr(2);

        assert.ok(Math.abs(view[1] - RValueF32) < EPSILON);

        mat.delete();
    }

    // cv.CV_32FC3 + Mat::ptr(int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_32FC3);
        let view = mat.data32F;

        // Alter matrix[2, 1].
        let step = mat.step1(0);
        view[2 * step + 3] = RValueF32;
        view[2 * step + 3 + 1] = GValueF32;
        view[2 * step + 3 + 2] = BValueF32;

        // Access matrix[2, 1].
        view = mat.floatPtr(2);

        assert.ok(Math.abs(view[3] - RValueF32) < EPSILON);
        assert.ok(Math.abs(view[3 + 1] - GValueF32) < EPSILON);
        assert.ok(Math.abs(view[3 + 2] - BValueF32) < EPSILON);

        mat.delete();
    }

    // cv.CV_32FC3 + Mat::ptr(int, int).
    {
        let mat = new cv.Mat(10, 10, cv.CV_32FC3);
        let view = mat.data32F;

        // Alter matrix[2, 1].
        let step = mat.step1(0);
        view[2 * step + 3] = RValueF32;
        view[2 * step + 3 + 1] = GValueF32;
        view[2 * step + 3 + 2] = BValueF32;

        // Access matrix[2, 1].
        view = mat.floatPtr(2, 1);

        assert.ok(Math.abs(view[0] - RValueF32) < EPSILON);
        assert.ok(Math.abs(view[1] - GValueF32) < EPSILON);
        assert.ok(Math.abs(view[2] - BValueF32) < EPSILON);

        mat.delete();
    }
});

QUnit.test('test_mat_zeros', function(assert) {
    let zeros = new Uint8Array(10*10).fill(0);
    // Mat::zeros(int, int, int)
    {
        let mat = cv.Mat.zeros(10, 10, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, zeros);

        mat.delete();
    }

    // Mat::zeros(Size, int)
    {
        let mat = cv.Mat.zeros({height: 10, width: 10}, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, zeros);

        mat.delete();
    }
});

QUnit.test('test_mat_ones', function(assert) {
    let ones = new Uint8Array(10*10).fill(1);
    // Mat::ones(int, int, int)
    {
        let mat = cv.Mat.ones(10, 10, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, ones);
    }
    // Mat::ones(Size, int)
    {
        let mat = cv.Mat.ones({height: 10, width: 10}, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, ones);
    }
});

QUnit.test('test_mat_eye', function(assert) {
    let eye4by4 = new Uint8Array([1, 0, 0, 0,
                                  0, 1, 0, 0,
                                  0, 0, 1, 0,
                                  0, 0, 0, 1]);
    // Mat::eye(int, int, int)
    {
        let mat = cv.Mat.eye(4, 4, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, eye4by4);
    }

    // Mat::eye(Size, int)
    {
        let mat = cv.Mat.eye({height: 4, width: 4}, cv.CV_8UC1);
        let view = mat.data;

        assert.deepEqual(view, eye4by4);
    }
});

QUnit.test('test_mat_miscs', function(assert) {
    // Mat::col(int)
    {
        let mat = cv.matFromArray(2, 2, cv.CV_8UC2, [1, 2, 3, 4, 5, 6, 7, 8]);
        let col = mat.col(1);

        assert.equal(col.isContinuous(), false);
        assert.equal(col.ptr(0, 0)[0], 3);
        assert.equal(col.ptr(0, 0)[1], 4);
        assert.equal(col.ptr(1, 0)[0], 7);
        assert.equal(col.ptr(1, 0)[1], 8);

        col.delete();
        mat.delete();
    }

    // Mat::row(int)
    {
        let mat = cv.Mat.zeros(5, 5, cv.CV_8UC2);
        let row = mat.row(1);
        let view = row.data;
        assert.equal(view[0], 0);
        assert.equal(view[4], 0);

        row.delete();
        mat.delete();
    }

    // Mat::convertTo(Mat, int, double, double)
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
        let grayMat = cv.Mat.zeros(5, 5, cv.CV_8UC1);

        mat.convertTo(grayMat, cv.CV_8U, 2, 1);
        // dest = 2 * source(x, y) + 1.
        let view = grayMat.data;
        assert.equal(view[0], (1 * 2) + 1);

        mat.convertTo(grayMat, cv.CV_8U);
        // dest = 1 * source(x, y) + 0.
        assert.equal(view[0], 1);

        mat.convertTo(grayMat, cv.CV_8U, 2);
        // dest = 2 * source(x, y) + 0.
        assert.equal(view[0], 2);

        grayMat.delete();
        mat.delete();
    }

    // split
    {
        const R =7;
        const G =13;
        const B =29;

        let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
        let view = mat.data;
        view[0] = R;
        view[1] = G;
        view[2] = B;

        let bgrPlanes = new cv.MatVector();
        cv.split(mat, bgrPlanes);
        assert.equal(bgrPlanes.size(), 3);

        let rMat = bgrPlanes.get(0);
        view = rMat.data;
        assert.equal(view[0], R);

        let gMat = bgrPlanes.get(1);
        view = gMat.data;
        assert.equal(view[0], G);

        let bMat = bgrPlanes.get(2);
        view = bMat.data;
        assert.equal(view[0], B);

        mat.delete();
        rMat.delete();
        gMat.delete();
        bgrPlanes.delete();
        bMat.delete();
    }

    // elemSize
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
        assert.equal(mat.elemSize(), 3);
        assert.equal(mat.elemSize1(), 1);

        let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
        assert.equal(mat2.elemSize(), 1);
        assert.equal(mat2.elemSize1(), 1);

        let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
        assert.equal(mat3.elemSize(), 2 * 3);
        assert.equal(mat3.elemSize1(), 2);

        mat.delete();
        mat2.delete();
        mat3.delete();
    }

    // step
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC3);
        assert.equal(mat.step[0], 15);
        assert.equal(mat.step[1], 3);

        let mat2 = cv.Mat.zeros(5, 5, cv.CV_8UC1);
        assert.equal(mat2.step[0], 5);
        assert.equal(mat2.step[1], 1);

        let mat3 = cv.Mat.eye(5, 5, cv.CV_16UC3);
        assert.equal(mat3.step[0], 30);
        assert.equal(mat3.step[1], 6);

        mat.delete();
        mat2.delete();
        mat3.delete();
    }

    // dot
    {
        let mat = cv.Mat.ones(5, 5, cv.CV_8UC1);
        let mat2 = cv.Mat.eye(5, 5, cv.CV_8UC1);

        assert.equal(mat.dot(mat), 25);
        assert.equal(mat.dot(mat2), 5);
        assert.equal(mat2.dot(mat2), 5);

        mat.delete();
        mat2.delete();
    }

    // mul
    {
        const FACTOR = 5;
        let mat = cv.Mat.ones(4, 4, cv.CV_8UC1);
        let mat2 = cv.Mat.eye(4, 4, cv.CV_8UC1);

        let expected = new Uint8Array([FACTOR, 0, 0, 0,
                                       0, FACTOR, 0, 0,
                                       0, 0, FACTOR, 0,
                                       0, 0, 0, FACTOR]);
        let mat3 = mat.mul(mat2, FACTOR);

        assert.deepEqual(mat3.data, expected);

        mat.delete();
        mat2.delete();
        mat3.delete();
    }
});


QUnit.test('test mat access', function(assert) {
    // test memory view
    {
        let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
        let dataPtr = cv._malloc(8);

        let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
        dataHeap.set(new Uint8Array(data.buffer));

        let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);


        let unsignedCharView = new Uint8Array(data.buffer);
        let charView = new Int8Array(data.buffer);
        let shortView = new Int16Array(data.buffer);
        let unsignedShortView = new Uint16Array(data.buffer);
        let intView = new Int32Array(data.buffer);
        let float32View = new Float32Array(data.buffer);
        let float64View = new Float64Array(data.buffer);


        assert.deepEqual(unsignedCharView, mat.data);
        assert.deepEqual(charView, mat.data8S);
        assert.deepEqual(shortView, mat.data16S);
        assert.deepEqual(unsignedShortView, mat.data16U);
        assert.deepEqual(intView, mat.data32S);
        assert.deepEqual(float32View, mat.data32F);
        assert.deepEqual(float64View, mat.data64F);
    }

    // test ucharAt(i)
    {
        let data = new Uint8Array([0, 0, 0, 255, 0, 1, 2, 3]);
        let dataPtr = cv._malloc(8);

        let dataHeap = new Uint8Array(cv.HEAPU8.buffer, dataPtr, 8);
        dataHeap.set(new Uint8Array(data.buffer));

        let mat = new cv.Mat(8, 1, cv.CV_8UC1, dataPtr, 0);

        assert.equal(mat.ucharAt(0), 0);
        assert.equal(mat.ucharAt(1), 0);
        assert.equal(mat.ucharAt(2), 0);
        assert.equal(mat.ucharAt(3), 255);
        assert.equal(mat.ucharAt(4), 0);
        assert.equal(mat.ucharAt(5), 1);
        assert.equal(mat.ucharAt(6), 2);
        assert.equal(mat.ucharAt(7), 3);
    }

    // test ushortAt(i)
    {
        let data = new Uint16Array([0, 1000, 65000, 255, 0, 1, 2, 3]);
        let dataPtr = cv._malloc(16);

        let dataHeap = new Uint16Array(cv.HEAPU8.buffer, dataPtr, 8);
        dataHeap.set(new Uint16Array(data.buffer));

        let mat = new cv.Mat(8, 1, cv.CV_16SC1, dataPtr, 0);

        assert.equal(mat.ushortAt(0), 0);
        assert.equal(mat.ushortAt(1), 1000);
        assert.equal(mat.ushortAt(2), 65000);
        assert.equal(mat.ushortAt(3), 255);
        assert.equal(mat.ushortAt(4), 0);
        assert.equal(mat.ushortAt(5), 1);
        assert.equal(mat.ushortAt(6), 2);
        assert.equal(mat.ushortAt(7), 3);
    }

    // test intAt(i)
    {
        let data = new Int32Array([0, -1000, 65000, 255, -2000000, -1, 2, 3]);
        let dataPtr = cv._malloc(32);

        let dataHeap = new Int32Array(cv.HEAPU32.buffer, dataPtr, 8);
        dataHeap.set(new Int32Array(data.buffer));

        let mat = new cv.Mat(8, 1, cv.CV_32SC1, dataPtr, 0);

        assert.equal(mat.intAt(0), 0);
        assert.equal(mat.intAt(1), -1000);
        assert.equal(mat.intAt(2), 65000);
        assert.equal(mat.intAt(3), 255);
        assert.equal(mat.intAt(4), -2000000);
        assert.equal(mat.intAt(5), -1);
        assert.equal(mat.intAt(6), 2);
        assert.equal(mat.intAt(7), 3);
    }

    // test floatAt(i)
    {
        const EPSILON = 0.001;
        let data = new Float32Array([0, -10.5, 650.001, 255, -20.1, -1.2, 2, 3.5]);
        let dataPtr = cv._malloc(32);

        let dataHeap = new Float32Array(cv.HEAPU32.buffer, dataPtr, 8);
        dataHeap.set(new Float32Array(data.buffer));

        let mat = new cv.Mat(8, 1, cv.CV_32FC1, dataPtr, 0);

        assert.equal(Math.abs(mat.floatAt(0)-0) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(1)+10.5) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(2)-650.001) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(3)-255) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(4)+20.1) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(5)+1.2) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(6)-2) < EPSILON, true);
        assert.equal(Math.abs(mat.floatAt(7)-3.5) < EPSILON, true);
    }

    // test intAt(i,j)
    {
        let mat = cv.Mat.eye({height: 3, width: 3}, cv.CV_32SC1);

        assert.equal(mat.intAt(0, 0), 1);
        assert.equal(mat.intAt(0, 1), 0);
        assert.equal(mat.intAt(0, 2), 0);
        assert.equal(mat.intAt(1, 0), 0);
        assert.equal(mat.intAt(1, 1), 1);
        assert.equal(mat.intAt(1, 2), 0);
        assert.equal(mat.intAt(2, 0), 0);
        assert.equal(mat.intAt(2, 1), 0);
        assert.equal(mat.intAt(2, 2), 1);

        mat.delete();
    }
});

QUnit.test('test_mat_operations', function(assert) {
    // test minMaxLoc
    {
        let src = cv.Mat.ones(4, 4, cv.CV_8UC1);

        src.data[2] = 0;
        src.data[5] = 2;

        let result = cv.minMaxLoc(src);

        assert.equal(result.minVal, 0);
        assert.equal(result.maxVal, 2);
        assert.deepEqual(result.minLoc, {x: 2, y: 0});
        assert.deepEqual(result.maxLoc, {x: 1, y: 1});

        src.delete();
    }
});

QUnit.test('test_mat_roi', function(assert) {
    // test minMaxLoc
    {
        let mat = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
        let roi = mat.roi(new cv.Rect(1, 1, 1, 1));

        assert.equal(roi.rows, 1);
        assert.equal(roi.cols, 1);
        assert.deepEqual(roi.data, new Uint8Array([mat.ucharAt(1, 1)]));

        mat.delete();
        roi.delete();
    }
});


QUnit.test('test_mat_range', function(assert) {
    {
        let src = cv.matFromArray(2, 2, cv.CV_8UC1, [0, 1, 2, 3]);
        let mat = src.colRange(0, 1);

        assert.equal(mat.isContinuous(), false);
        assert.equal(mat.rows, 2);
        assert.equal(mat.cols, 1);
        assert.equal(mat.ucharAt(0), 0);
        assert.equal(mat.ucharAt(1), 2);

        mat.delete();

        mat = src.colRange({start: 0, end: 1});

        assert.equal(mat.isContinuous(), false);
        assert.equal(mat.rows, 2);
        assert.equal(mat.cols, 1);
        assert.equal(mat.ucharAt(0), 0);
        assert.equal(mat.ucharAt(1), 2);

        mat.delete();

        mat = src.rowRange(1, 2);

        assert.equal(mat.rows, 1);
        assert.equal(mat.cols, 2);
        assert.deepEqual(mat.data, new Uint8Array([2, 3]));

        mat.delete();

        mat = src.rowRange({start: 1, end: 2});

        assert.equal(mat.rows, 1);
        assert.equal(mat.cols, 2);
        assert.deepEqual(mat.data, new Uint8Array([2, 3]));

        mat.delete();

        src.delete();
    }
});

QUnit.test('test_mat_diag', function(assert) {
    // test diag
    {
        let mat = cv.matFromArray(3, 3, cv.CV_8UC1, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
        let d = mat.diag();
        let d1 = mat.diag(1);
        let d2 = mat.diag(-1);

        assert.equal(mat.isContinuous(), true);
        assert.equal(d.isContinuous(), false);
        assert.equal(d1.isContinuous(), false);
        assert.equal(d2.isContinuous(), false);

        assert.equal(d.ucharAt(0), 0);
        assert.equal(d.ucharAt(1), 4);
        assert.equal(d.ucharAt(2), 8);

        assert.equal(d1.ucharAt(0), 1);
        assert.equal(d1.ucharAt(1), 5);

        assert.equal(d2.ucharAt(0), 3);
        assert.equal(d2.ucharAt(1), 7);

        mat.delete();
        d.delete();
        d1.delete();
        d2.delete();
    }
});