libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/opencv.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Redistribution under a license not approved by the Open Source
00013  *       Initiative (http://www.opensource.org) must display the
00014  *       following acknowledgement in all advertising material:
00015  *        This product includes software developed at the Courant
00016  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00017  *     * The names of the authors may not be used to endorse or promote products
00018  *       derived from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00021  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00024  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  ***************************************************************************/
00031 
00032 namespace ebl {
00033 
00034 #ifdef __OPENCV__
00035 
00037   // interface with opencv
00038 
00039   template <typename T>
00040   IplImage* idx_to_ipl(idx<T> &im) {
00041     // TODO: check dimensions match
00042     IplImage* ipl = cvCreateImage(cvSize(im.dim(1), im.dim(0)),
00043                                   IPL_DEPTH_8U, im.dim(2));
00044     uint sz = ipl->width * ipl->height * ipl->nChannels;
00045     ubyte *out = (ubyte*) ipl->imageData;
00046     T *in = im.idx_ptr();
00047     // cast and copy data
00048     for (uint i = 0; i < sz; ++i, ++out, ++in)
00049       *out = (ubyte) *in;
00050     return ipl;
00051   }
00052 
00053   template <typename T>
00054   idx<T> ipl_to_idx(IplImage *im) {
00055     idx<T> f(im->height, im->width, im->nChannels);
00056     uint sz = im->width * im->height * im->nChannels;
00057     ubyte *in = (ubyte*) im->imageData;
00058     T *out = f.idx_ptr();
00059     // cast and copy data
00060     for (uint i = 0; i < sz; ++i, ++out, ++in)
00061       *out = (T) *in;
00062     return f;
00063   }
00064     
00065   template <typename T>
00066   void ipl_to_idx(IplImage *im, idx<T> &out) {
00067     // TODO: check out dimensions match
00068     uint sz = im->width * im->height * im->nChannels;
00069     ubyte *in = (ubyte*) im->imageData;
00070     T *pout = out.idx_ptr();
00071     // cast and copy data
00072     for (uint i = 0; i < sz; ++i, ++pout, ++in)
00073       *pout = (T) *in;
00074   }
00075   
00076 #endif /* __OPENCV__ */
00077 
00078 } // end namespace ebl
00079