plotmdec.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// @file plotmdec.h
00004 ///
00005 /// Plotting a Morse decomposition in a PNG picture.
00006 /// This file contains the definition of a procedure which can be used
00007 /// to save a Morse decomposition to a PNG bitmap picture file.
00008 ///
00009 /// @author Pawel Pilarczyk
00010 ///
00011 /////////////////////////////////////////////////////////////////////////////
00012 
00013 // Copyright (C) 1997-2008 by Pawel Pilarczyk.
00014 //
00015 // This file is part of my research software package.  This is free software;
00016 // you can redistribute it and/or modify it under the terms of the GNU
00017 // General Public License as published by the Free Software Foundation;
00018 // either version 2 of the License, or (at your option) any later version.
00019 //
00020 // This software is distributed in the hope that it will be useful,
00021 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 // GNU General Public License for more details.
00024 //
00025 // You should have received a copy of the GNU General Public License along
00026 // with this software; see the file "license.txt".  If not, write to the
00027 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00028 // MA 02111-1307, USA.
00029 
00030 // Started on February 14, 2007. Last revision: March 15, 2008.
00031 
00032 
00033 #ifndef _CMGRAPHS_PLOTMDEC_H_
00034 #define _CMGRAPHS_PLOTMDEC_H_
00035 
00036 
00037 #include "pngimage.h"
00038 #include "colorpal.h"
00039 
00040 
00041 // --------------------------------------------------
00042 // ------------ Plot Morse Decomposition ------------
00043 // --------------------------------------------------
00044 
00045 /// Saves all the Morse sets in the provided Morse decomposition
00046 /// to a PNG file. The coordinates of all the boxes in the Morse sets
00047 /// must be non-negative and they should not exceed the size provided
00048 /// unless the size is zero and then the size of the file is adjusted
00049 /// so that all the Morse sets are contained in the picture.
00050 /// Throws an error message in case of failure.
00051 template <class MorseDecType>
00052 inline void plotMorseDecompositionPNG (const MorseDecType &m,
00053         const char *filename, int size = 0, bool colorBar = true,
00054         bool copyright = false)
00055 {
00056         typedef typename MorseDecType::CubSetType CubSetType;
00057         typedef typename MorseDecType::CubeType CubeType;
00058         typedef typename MorseDecType::CubeType::CoordType CoordType;
00059 
00060         // determine the dimension of the cubes in the Morse sets
00061         int dim = 0;
00062         int nSets = m. count ();
00063         for (int n = 0; n < nSets; ++ n)
00064         {
00065                 if (m [n]. empty ())
00066                         continue;
00067                 dim = m [n] [0]. dim ();
00068                 break;
00069         }
00070 
00071         // prepare a temporary array for coordinates of boxes
00072         CoordType *coord = new CoordType [(dim < 2) ? 2 : dim];
00073         coord [0] = coord [1] = 0;
00074 
00075         // determine the size of the bitmap file
00076         int width = size;
00077         int height = size;
00078         int xoffset = 0;
00079         int yoffset = 0;
00080         int nColors = nSets;
00081         if (!size)
00082         {
00083                 xoffset = 0x7FFFFFFF;
00084                 yoffset = 0x7FFFFFFF;
00085                 for (int n = 0; n < nSets; ++ n)
00086                 {
00087                         const CubSetType &theSet = m [n];
00088                         if (theSet. empty ())
00089                         {
00090                                 -- nColors;
00091                                 continue;
00092                         }
00093                         int setSize = theSet. size ();
00094                         for (int i = 0; i < setSize; ++ i)
00095                         {
00096                                 const CubeType &q = theSet [i];
00097                                 int d = q. dim ();
00098                                 if (d != dim)
00099                                         throw "Varying dim of Morse sets.";
00100                                 q. coord (coord);
00101                                 if (width <= coord [0])
00102                                         width = coord [0] + 1;
00103                                 if (height <= coord [1])
00104                                         height = coord [1] + 1;
00105                                 if (xoffset > coord [0])
00106                                         xoffset = coord [0];
00107                                 if (yoffset > coord [1])
00108                                         yoffset = coord [1];
00109                         }
00110                 }
00111                 if (xoffset > width)
00112                         xoffset = 0;
00113                 if (yoffset > height)
00114                         yoffset = 0;
00115                 width -= xoffset;
00116                 height -= yoffset;
00117         }
00118         if (width < 16)
00119                 width = 16;
00120         if (height < 16)
00121                 height = 16;
00122 
00123         // reserve space for a color bar if necessary
00124         int colorBoxSize = 16;
00125         int colorBarHeight = colorBar ? (colorBoxSize + 1) : 0;
00126         int colorBarWidth = colorBar ?
00127                 (nColors * colorBoxSize + nColors - 1) : 0;
00128         height += colorBarHeight;
00129         if (width < colorBarWidth)
00130                 width = colorBarWidth;
00131         int bottomHeight = colorBarHeight;
00132 
00133         // prepare space for the copyright message bitmap
00134         const int copyrightWidth = 152;
00135         const int copyrightHeight = 8;
00136         const int copyrightCount = 342;
00137         const short int copyrightPixels [2 * copyrightCount] = {
00138                 11,0,15,0,16,0,27,0,28,0,43,0,44,0,45,0,77,0,78,0,142,0,
00139                 143,0,1,1,2,1,3,1,7,1,8,1,11,1,17,1,21,1,24,1,29,1,31,1,
00140                 34,1,36,1,37,1,42,1,46,1,50,1,51,1,52,1,53,1,56,1,57,1,
00141                 61,1,62,1,66,1,67,1,73,1,74,1,75,1,79,1,86,1,92,1,93,1,
00142                 94,1,97,1,101,1,105,1,106,1,107,1,110,1,115,1,121,1,123,1,
00143                 126,1,127,1,128,1,130,1,134,1,135,1,138,1,139,1,140,1,
00144                 144,1,148,1,151,1,0,2,4,2,6,2,9,2,11,2,12,2,13,2,17,2,
00145                 18,2,21,2,24,2,27,2,28,2,29,2,31,2,34,2,36,2,41,2,44,2,
00146                 45,2,47,2,50,2,55,2,58,2,60,2,63,2,65,2,68,2,73,2,76,2,
00147                 79,2,80,2,86,2,91,2,94,2,97,2,101,2,104,2,110,2,115,2,
00148                 121,2,123,2,125,2,128,2,130,2,133,2,136,2,138,2,144,2,
00149                 145,2,148,2,150,2,0,3,6,3,9,3,11,3,14,3,16,3,19,3,21,3,
00150                 24,3,26,3,29,3,31,3,34,3,36,3,41,3,43,3,47,3,51,3,55,3,
00151                 58,3,60,3,63,3,65,3,68,3,73,3,76,3,78,3,81,3,86,3,92,3,
00152                 93,3,94,3,96,3,98,3,100,3,102,3,104,3,105,3,106,3,107,3,
00153                 110,3,115,3,121,3,123,3,126,3,127,3,128,3,130,3,133,3,
00154                 139,3,143,3,146,3,148,3,149,3,0,4,6,4,9,4,11,4,14,4,16,4,
00155                 19,4,21,4,24,4,26,4,29,4,31,4,34,4,36,4,41,4,43,4,47,4,
00156                 52,4,55,4,58,4,60,4,63,4,66,4,67,4,73,4,76,4,78,4,81,4,
00157                 86,4,87,4,88,4,89,4,94,4,96,4,99,4,102,4,104,4,107,4,
00158                 109,4,110,4,115,4,116,4,117,4,118,4,121,4,123,4,128,4,
00159                 130,4,133,4,136,4,140,4,143,4,146,4,148,4,150,4,0,5,7,5,
00160                 8,5,11,5,12,5,13,5,16,5,19,5,21,5,22,5,24,5,27,5,28,5,
00161                 29,5,31,5,32,5,33,5,36,5,37,5,41,5,44,5,45,5,47,5,53,5,
00162                 55,5,58,5,60,5,63,5,65,5,68,5,73,5,74,5,75,5,78,5,81,5,
00163                 86,5,90,5,92,5,93,5,96,5,99,5,102,5,105,5,106,5,110,5,
00164                 111,5,115,5,119,5,121,5,123,5,126,5,127,5,130,5,131,5,
00165                 134,5,135,5,138,5,139,5,140,5,143,5,146,5,148,5,151,5,
00166                 0,6,4,6,31,6,36,6,42,6,46,6,50,6,53,6,55,6,58,6,60,6,
00167                 63,6,65,6,68,6,73,6,86,6,90,6,110,6,115,6,119,6,123,6,
00168                 148,6,1,7,2,7,3,7,24,7,31,7,43,7,44,7,45,7,51,7,52,7,
00169                 56,7,57,7,61,7,62,7,66,7,67,7,73,7,86,7,87,7,88,7,89,7,
00170                 110,7,115,7,116,7,117,7,118,7,121,7,123,7,148,7};
00171         if (copyright)
00172         {
00173                 if (width < copyrightWidth + colorBarWidth + 1)
00174                 {
00175                         width = copyrightWidth + colorBarWidth +
00176                                 (colorBar ? 1 : 0);
00177                 }
00178                 if (colorBarHeight < copyrightHeight + 1)
00179                 {
00180                         height += copyrightHeight + 1 - colorBarHeight;
00181                         bottomHeight = copyrightHeight + 1;
00182                 }
00183         }
00184         int copyrightX = width - copyrightWidth;
00185         int copyrightY = height - copyrightHeight;
00186 
00187         // prepare an empty PNG image
00188         PngImage img (width, height);
00189 
00190         // plot the copyright pixels in the default color (black)
00191         if (copyright)
00192         {
00193                 const short int *pixelPtr = copyrightPixels;
00194                 for (int i = 0; i < copyrightCount; ++ i)
00195                 {
00196                         int x = copyrightX + *(pixelPtr ++);
00197                         int y = copyrightY + copyrightHeight -
00198                                 *(pixelPtr ++) - 1;
00199                         img. putPixel (x, y);
00200                 }
00201         }
00202 
00203         // prepare a color palette and draw the color bar if required
00204         ColorPalette col (nColors);
00205         if (colorBar)
00206         {
00207                 for (int n = 0; n < nColors; ++ n)
00208                 {
00209                         int xMin = n * (colorBoxSize + 1);
00210                         int xMax = xMin + colorBoxSize;
00211                         int yMin = height - colorBarHeight + 1;
00212                         int yMax = yMin + colorBarHeight;
00213                         int color = col [n];
00214                         for (int x = xMin; x < xMax; ++ x)
00215                         {
00216                                 for (int y = yMin; y < yMax; ++ y)
00217                                 {
00218                                         img. putPixel (x, y, color);
00219                                 }
00220                         }
00221                 }
00222         }
00223 
00224         // plot the Morse sets in the PNG image
00225         int colorNumber = 0;
00226         for (int n = 0; n < nSets; ++ n)
00227         {
00228                 const CubSetType &theSet = m [n];
00229                 if (theSet. empty ())
00230                         continue;
00231                 int color = col [colorNumber ++];
00232                 int setSize = theSet. size ();
00233                 for (int i = 0; i < setSize; ++ i)
00234                 {
00235                         const CubeType &q = theSet [i];
00236                         int d = q. dim ();
00237                         if (d != dim)
00238                                 throw "Varying dim of Morse sets.";
00239                         q. coord (coord);
00240                         img. putPixel (coord [0] - xoffset, height -
00241                                 coord [1] + yoffset - 1 - bottomHeight,
00242                                 color);
00243                 }
00244         }
00245         delete [] coord;
00246 
00247         // save the PNG file
00248         img. save (filename);
00249 
00250         return;
00251 } /* plotMorseDecomposition */
00252 
00253 
00254 #endif // _CMGRAPHS_PLOTMDEC_H_
00255 

Generated on Sun Mar 28 17:47:58 2010 for The Conley-Morse Graphs Software by  doxygen 1.5.3