ofTheo / ofxKinect

legacy openFrameworks wrapper for the xbox kinect (OF pre-0.8.0+ only) - ofxKinect is now included and is being maintained in OF releases
MIT License
541 stars 106 forks source link

add opencv bayer code: #1

Closed ofTheo closed 13 years ago

ofTheo commented 13 years ago
/*
 *  bayer.h
 *  
 *
 *  Ripped unceremoniously from OpenCV -- cvcolor.cpp and constants.h
 *
 */

/*M///////////////////////////////////////////////////////////////////////////////////////
 //  ORIGINAL OPENCV COPYRIGHT NOTICE:
 //  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*/

/********************************* COPYRIGHT NOTICE *******************************\
 //  ORIGINAL BAYER CODE COPYRIGHT NOTICE:
 Original code for Bayer->BGR/RGB conversion is provided by Dirk Schaefer
 from MD-Mathematische Dienste GmbH. Below is the copyright notice:

 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.

 Contributors License Agreement:

 Copyright (c) 2002,
 MD-Mathematische Dienste GmbH
 Im Defdahl 5-10
 44141 Dortmund
 Germany
 www.md-it.de

 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. 

 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 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.
 \**********************************************************************************/

/* Ripped from OpenCV constants.h */
#ifndef CV_BayerBG2BGR
    #define  CV_BayerBG2BGR 46
#endif
#ifndef CV_BayerGB2BGR
    #define  CV_BayerGB2BGR 47
#endif
#ifndef CV_BayerRG2BGR
    #define  CV_BayerRG2BGR 48
#endif
#ifndef CV_BayerGR2BGR
    #define  CV_BayerGR2BGR 49
#endif
#ifndef CV_BayerBG2RGB
    #define  CV_BayerBG2RGB CV_BayerRG2BGR
#endif
#ifndef CV_BayerGB2RGB
    #define  CV_BayerGB2RGB CV_BayerGR2BGR
#endif
#ifndef CV_BayerRG2RGB
    #define  CV_BayerRG2RGB CV_BayerBG2BGR
#endif
#ifndef CV_BayerGR2RGB
    #define  CV_BayerGR2RGB CV_BayerGB2BGR
#endif

/****************************************************************************************\
 *                            Bayer Pattern -> RGB conversion  
 \****************************************************************************************/

bool bayer2BGR_8u_C1C3R(    const unsigned char* bayer0, 
                            int bayer_step,
                            unsigned char* dst0, int dst_step,
                            int img_width, int img_height, int code )
{
    int blue = code == CV_BayerBG2BGR || code == CV_BayerGB2BGR ? -1 : 1;
    int start_with_green = code == CV_BayerGB2BGR || code == CV_BayerGR2BGR;

    memset( dst0, 0, img_width*3*sizeof(dst0[0]) );
    memset( dst0 + (img_height - 1)*dst_step, 0, img_width*3*sizeof(dst0[0]) );
    dst0 += dst_step + 3 + 1;
    img_height -= 2;
    img_width -= 2;

    int bayer_step2 = bayer_step*2; // small optimization.

    for( ; img_height-- > 0; bayer0 += bayer_step, dst0 += dst_step )
    {
        int t0, t1;
        const unsigned char* bayer = bayer0;
        unsigned char* dst = dst0;
        const unsigned char* bayer_end = bayer + img_width;

        dst[-4] = dst[-3] = dst[-2] = dst[img_width*3-1] =
        dst[img_width*3] = dst[img_width*3+1] = 0;

        if( img_width <= 0 )
            continue;

        if( start_with_green )
        {
            t0 = (bayer[1] + bayer[bayer_step2+1] + 1) >> 1;
            t1 = (bayer[bayer_step] + bayer[bayer_step+2] + 1) >> 1;
            dst[-blue] = (unsigned char)t0;
            dst[0] = bayer[bayer_step+1];
            dst[blue] = (unsigned char)t1;
            bayer++;
            dst += 3;
        }

        if( blue > 0 )
        {
            for( ; bayer <= bayer_end - 2; bayer += 2, dst += 6 )
            {
                t0 = (bayer[0] + bayer[2] + bayer[bayer_step2] +
                      bayer[bayer_step2+2] + 2) >> 2;
                t1 = (bayer[1] + bayer[bayer_step] +
                      bayer[bayer_step+2] + bayer[bayer_step2+1]+2) >> 2;
                dst[-1] = (unsigned char)t0;
                dst[0]  = (unsigned char)t1;
                dst[1]  = bayer[bayer_step+1];

                t0 = (bayer[2] + bayer[bayer_step2+2] + 1) >> 1;
                t1 = (bayer[bayer_step+1] + bayer[bayer_step+3] + 1) >> 1;
                dst[2] = (unsigned char)t0;
                dst[3] = bayer[bayer_step+2];
                dst[4] = (unsigned char)t1;
            }
        }
        else
        {
            for( ; bayer <= bayer_end - 2; bayer += 2, dst += 6 )
            {
                t0 = (bayer[0] + bayer[2] + bayer[bayer_step2] +
                      bayer[bayer_step2+2] + 2) >> 2;
                t1 = (bayer[1] + bayer[bayer_step] +
                      bayer[bayer_step+2] + bayer[bayer_step2+1]+2) >> 2;
                dst[1]  = (unsigned char)t0;
                dst[0]  = (unsigned char)t1;
                dst[-1] = bayer[bayer_step+1];

                t0 = (bayer[2] + bayer[bayer_step2+2] + 1) >> 1;
                t1 = (bayer[bayer_step+1] + bayer[bayer_step+3] + 1) >> 1;
                dst[4] = (unsigned char)t0;
                dst[3] = bayer[bayer_step+2];
                dst[2] = (unsigned char)t1;
            }
        }

        if( bayer < bayer_end )
        {
            t0 = (bayer[0] + bayer[2] + bayer[bayer_step2] +
                  bayer[bayer_step2+2] + 2) >> 2;
            t1 = (bayer[1] + bayer[bayer_step] +
                  bayer[bayer_step+2] + bayer[bayer_step2+1]+2) >> 2;
            dst[-blue] = (unsigned char)t0;
            dst[0] = (unsigned char)t1;
            dst[blue] = bayer[bayer_step+1];
            bayer++;
            dst += 3;
        }

        blue = -blue;
        start_with_green = !start_with_green;
    }

    return true;
}
ofTheo commented 13 years ago

lots of cleanup. fixes to the example - better demo. accelorometer getters. removing filtering and not needed. try catch Closed by ad75342075debc6a3ad065e3415a719f59d14b46. Closed by ad75342075debc6a3ad065e3415a719f59d14b46. Closed by ad75342075debc6a3ad065e3415a719f59d14b46. Closed by ad75342075debc6a3ad065e3415a719f59d14b46.