AppImageGuiPy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #include "PreCompiled.h"
00024 #ifndef _PreComp_
00025 # include <QIcon>
00026 # include <QImage>
00027 # include <QFileInfo>
00028 #endif
00029 
00030 #include "ImageView.h"
00031 
00032 #include <Base/Console.h>
00033 #include <Base/Exception.h>
00034 #include <Base/FileInfo.h>
00035 #include <App/Application.h>
00036 #include <Gui/MainWindow.h>
00037 #include <Gui/BitmapFactory.h>
00038 
00039 using namespace ImageGui;
00040 
00041 
00042 /* module functions */
00043 static PyObject * 
00044 open(PyObject *self, PyObject *args) 
00045 {
00046     const char* Name;
00047     const char* DocName=0;
00048     if (!PyArg_ParseTuple(args, "s|s",&Name,&DocName))
00049         return NULL; 
00050     
00051     PY_TRY {
00052         QString fileName = QString::fromUtf8(Name);
00053         QFileInfo file(fileName);
00054             
00055         // Load image from file into a QImage object
00056         QImage imageq(fileName);
00057 
00058         // Extract image into a general RGB format recognised by the ImageView class
00059         int format = IB_CF_RGB24;
00060         unsigned char *pPixelData = NULL;
00061         if (imageq.isNull() == false) {
00062             pPixelData = new unsigned char[3 * (unsigned long)imageq.width() * (unsigned long)imageq.height()];
00063             unsigned char *pPix = pPixelData;
00064             for (int r = 0; r < imageq.height(); r++) {
00065                 for (int c = 0; c < imageq.width(); c++) {
00066                     QRgb rgb = imageq.pixel(c,r);
00067                     *pPix = (unsigned char)qRed(rgb);
00068                     *(pPix + 1) = (unsigned char)qGreen(rgb);
00069                     *(pPix + 2) = (unsigned char)qBlue(rgb);
00070                     pPix += 3;
00071                 }
00072             }
00073         }
00074         else
00075             Py_Error(PyExc_Exception,"Could not load image");
00076 
00077         // Displaying the image in a view.
00078         // This ImageView object takes ownership of the pixel data (in 'pointImageTo') so we don't need to delete it here
00079         ImageView* iView = new ImageView(Gui::getMainWindow());
00080         iView->setWindowIcon( Gui::BitmapFactory().pixmap("colors") );
00081         iView->setWindowTitle(file.fileName());
00082         iView->resize( 400, 300 );
00083         Gui::getMainWindow()->addWindow( iView );
00084         iView->pointImageTo((void *)pPixelData, (unsigned long)imageq.width(), (unsigned long)imageq.height(), format, 0, true);
00085 
00086     } PY_CATCH;
00087 
00088     Py_Return; 
00089 }
00090 
00091 
00092 /* module functions */
00093 static PyObject *
00094 insert(PyObject *self, PyObject *args)
00095 {
00096     return open(self, args);
00097 }
00098 
00099 /* registration table  */
00100 struct PyMethodDef ImageGui_Import_methods[] = {
00101     {"open"       ,open ,       1}, /* method name, C func ptr, always-tuple */
00102     {"insert"     ,insert,      1},
00103     {NULL, NULL}                   /* end of table marker */
00104 };

Generated on Wed Nov 23 18:59:55 2011 for FreeCAD by  doxygen 1.6.1