app-font2c.C

Go to the documentation of this file.
00001 /*!@file AppMedia/app-font2c.C Takes a .pgm image with ASCII chars written in it, and
00002   transforms it into C source code
00003 */
00004 
00005 // //////////////////////////////////////////////////////////////////// //
00006 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00007 // University of Southern California (USC) and the iLab at USC.         //
00008 // See http://iLab.usc.edu for information about this project.          //
00009 // //////////////////////////////////////////////////////////////////// //
00010 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00011 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00012 // in Visual Environments, and Applications'' by Christof Koch and      //
00013 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00014 // pending; application number 09/912,225 filed July 23, 2001; see      //
00015 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00016 // //////////////////////////////////////////////////////////////////// //
00017 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00018 //                                                                      //
00019 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00020 // redistribute it and/or modify it under the terms of the GNU General  //
00021 // Public License as published by the Free Software Foundation; either  //
00022 // version 2 of the License, or (at your option) any later version.     //
00023 //                                                                      //
00024 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00025 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00026 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00027 // PURPOSE.  See the GNU General Public License for more details.       //
00028 //                                                                      //
00029 // You should have received a copy of the GNU General Public License    //
00030 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00031 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00032 // Boston, MA 02111-1307 USA.                                           //
00033 // //////////////////////////////////////////////////////////////////// //
00034 //
00035 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00036 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppMedia/app-font2c.C $
00037 // $Id: app-font2c.C 8077 2007-03-08 19:38:18Z rjpeters $
00038 //
00039 
00040 #include "Image/Image.H"
00041 #include "Util/log.H"
00042 #include "Raster/Raster.H"
00043 
00044 #include <cstdio>
00045 #include <cstdlib>
00046 
00047 // number of characters in image:
00048 #define NBCHARS 95
00049 // image should be:
00050 //  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
00051 // in one row of 95 chars
00052 
00053 /*! font2c.C -- takes a .pgm image with ASCII chars written in it, and
00054   transforms it into C source code
00055 */
00056 int main(int argc, char **argv)
00057 {
00058   if (argc != 3)
00059     { printf("USAGE: %s <image.pgm> <fontarrayname>\n", argv[0]); exit(0); }
00060 
00061   Image<byte> img = Raster::ReadGray(argv[1]);
00062   const char* const fontarrayname = argv[2];
00063 
00064   printf("// created by font2c; source image: %s\n", argv[1]);
00065   int w = img.getWidth(), h = img.getHeight();
00066   printf("// Image Width = %d, Height = %d\n", w, h);
00067   int w2 = w / NBCHARS;  // NBCHARS chars in image
00068   printf("// Letter size = %d x %d\n", w2, h);
00069   printf("#define WW 0x00\n");
00070   printf("#define o  0xff\n");
00071   printf("static unsigned char %s[%d][%d] = {\n",
00072          fontarrayname, NBCHARS, w2 * h);
00073 
00074   for (int i = 0; i < NBCHARS; i ++) // loop over chars
00075     {
00076       printf("  {\n    ");
00077       for (int j = 0; j < h; j ++)  // scan y one char
00078         {
00079           const bool lastrow = (j == h - 1);
00080           for (int ii = i * w2; ii < (i + 1) * w2; ii ++)  // scan x one char
00081             {
00082               const bool lastcol = (ii == (i + 1) * w2 - 1);
00083 
00084               if (img.getVal(ii, j) != 0) printf(lastrow && lastcol ? "o" : "o ");
00085               else                        printf("WW");
00086 
00087               if (!lastcol) printf(",");
00088             }
00089           if (!lastrow) printf(",\n    "); else printf("\n");
00090         }
00091       if (i != NBCHARS - 1) printf("  },\n"); else printf("  }\n");
00092     }
00093   printf("};\n");
00094   printf("#undef WW\n");
00095   printf("#undef o\n");
00096 }
00097 
00098 // ######################################################################
00099 /* So things look consistent in everyone's emacs... */
00100 /* Local Variables: */
00101 /* indent-tabs-mode: nil */
00102 /* End: */
Generated on Sun May 8 08:40:07 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3