00001 /*!@file QtUtil/ImageConvert.C Functions to convert our images to QImage or QPixmap 00002 */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00006 // University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/QtUtil/ImageConvert4.C $ 00035 // $Id: ImageConvert4.C 13429 2010-05-18 04:45:07Z rand $ 00036 // 00037 00038 #ifndef IMAGE_CONVERT_QT_C_DEFINED 00039 #define IMAGE_CONVERT_QT_C_DEFINED 00040 00041 #ifdef INVT_HAVE_QT4 00042 00043 #include "QtUtil/ImageConvert4.H" 00044 00045 #include "Image/Image.H" 00046 #include "Image/Pixels.H" 00047 00048 #include <QtGui/QImage> 00049 #include <vector> 00050 00051 // ###################################################################### 00052 QPixmap convertToQPixmap4(const Image<PixRGB<byte> >& img) 00053 { 00054 QImage qimg(img.getWidth(), img.getHeight(), QImage::Format_RGB32); 00055 00056 const PixRGB<byte>* img_ptr = img.getArrayPtr(); 00057 00058 unsigned char* scanline_ptr; 00059 for(int y=0; y<img.getHeight(); y++) 00060 { 00061 scanline_ptr = qimg.scanLine(y); 00062 for(int x=0; x<img.getWidth(); x+=1) 00063 { 00064 scanline_ptr[x*4+0] = img_ptr->p[2]; 00065 scanline_ptr[x*4+1] = img_ptr->p[1]; 00066 scanline_ptr[x*4+2] = img_ptr->p[0]; 00067 scanline_ptr[x*4+3] = 255; 00068 ++img_ptr; 00069 } 00070 } 00071 00072 // NOTE! We can't just return the QImage directly here, like we used 00073 // to do -- http://doc.trolltech.com/3.3/qimage.html#QImage-7 states 00074 // that "The buffer must remain valid throughout the life of the 00075 // QImage". Therefore, if we return the QImage directly, its memory 00076 // becomes invalid because our temporary 'data' array goes out of 00077 // scope. This won't necessarily cause a crash, but it will mean 00078 // that some of our image data will get trashed as the stack 00079 // contents are overwritten by subsequent calls. So, instead we 00080 // return a QPixmap which contains its own private copy of the data. 00081 00082 QPixmap retPixmap; 00083 retPixmap = QPixmap::fromImage(qimg); 00084 return retPixmap; 00085 } 00086 // ###################################################################### 00087 QPixmap convertToQPixmap4(const Image<byte>& img) 00088 { 00089 QImage qimg(img.getWidth(), img.getHeight(), QImage::Format_RGB32); 00090 00091 const byte* img_ptr = img.getArrayPtr(); 00092 00093 unsigned char* scanline_ptr; 00094 for(int y=0; y<img.getHeight(); y++) 00095 { 00096 scanline_ptr = qimg.scanLine(y); 00097 for(int x=0; x<img.getWidth(); x+=1) 00098 { 00099 scanline_ptr[x*4+0] = *img_ptr; 00100 scanline_ptr[x*4+1] = *img_ptr; 00101 scanline_ptr[x*4+2] = *img_ptr; 00102 scanline_ptr[x*4+3] = 255; 00103 ++img_ptr; 00104 } 00105 } 00106 00107 // NOTE! We can't just return the QImage directly here, like we used 00108 // to do -- http://doc.trolltech.com/3.3/qimage.html#QImage-7 states 00109 // that "The buffer must remain valid throughout the life of the 00110 // QImage". Therefore, if we return the QImage directly, its memory 00111 // becomes invalid because our temporary 'data' array goes out of 00112 // scope. This won't necessarily cause a crash, but it will mean 00113 // that some of our image data will get trashed as the stack 00114 // contents are overwritten by subsequent calls. So, instead we 00115 // return a QPixmap which contains its own private copy of the data. 00116 00117 QPixmap retPixmap; 00118 retPixmap = QPixmap::fromImage(qimg); 00119 return retPixmap; 00120 } 00121 00122 // ###################################################################### 00123 QImage convertToQImage4(const Image<PixRGB<byte> >& img) 00124 { 00125 #if INVT_QT4_MINOR > 4 00126 return QImage((uchar*)img.getArrayPtr(), img.getWidth(), img.getHeight(), QImage::Format_RGB888); 00127 #else 00128 QImage qimg(img.getWidth(), img.getHeight(), QImage::Format_RGB32); 00129 00130 const PixRGB<byte>* img_ptr = img.getArrayPtr(); 00131 00132 unsigned char* scanline_ptr; 00133 for(int y=0; y<img.getHeight(); y++) 00134 { 00135 scanline_ptr = qimg.scanLine(y); 00136 for(int x=0; x<img.getWidth(); x+=1) 00137 { 00138 scanline_ptr[x*4+0] = img_ptr->p[2]; 00139 scanline_ptr[x*4+1] = img_ptr->p[1]; 00140 scanline_ptr[x*4+2] = img_ptr->p[0]; 00141 scanline_ptr[x*4+3] = 255; 00142 ++img_ptr; 00143 } 00144 } 00145 00146 return qimg; 00147 #endif 00148 } 00149 00150 // ###################################################################### 00151 // WARNING: UNTESTED IN QT4 00152 Image<PixRGB<byte> > convertToImage4(const QPixmap& qpixm) 00153 { 00154 QImage qimg; 00155 qimg = qpixm.toImage(); 00156 00157 Image<PixRGB<byte> > result(qimg.width(), qimg.height(), NO_INIT); 00158 00159 Image<PixRGB<byte> >::iterator dptr = result.beginw(); 00160 Image<PixRGB<byte> >::iterator stop = result.endw(); 00161 00162 for (int y = 0; y < qimg.height(); ++y) 00163 for (int x = 0; x < qimg.width(); ++x) 00164 { 00165 QRgb pix = qimg.pixel(x, y); 00166 *dptr++ = PixRGB<byte>((pix & 0x00ff0000) >> 16, 00167 (pix & 0x0000ff00) >> 8, 00168 (pix & 0x000000ff)); 00169 } 00170 00171 ASSERT(dptr == stop); 00172 00173 return result; 00174 } 00175 00176 #endif //IMAGE_CONVERT_QT_C_DEFINED 00177 #endif //INVT_HAVE_QT4 00178 00179 // ###################################################################### 00180 /* So things look consistent in everyone's emacs... */ 00181 /* Local Variables: */ 00182 /* indent-tabs-mode: nil */ 00183 /* End: */