graphlcd-base/tools/convpic/convpic.c
changeset 4 df6a40031aa5
equal deleted inserted replaced
3:d0e62fc47285 4:df6a40031aa5
       
     1 /**
       
     2  *  convpic.c  -  a tool to convert images to
       
     3  *                own proprietary format of the logos and pictures
       
     4  *                for graphlcd plugin
       
     5  *
       
     6  *  (C) 2004 Andreas Brachold <vdr04 AT deltab de>
       
     7  *  (C) 2001-2003 by Carsten Siebholz <c.siebholz AT t-online.de>
       
     8  **/
       
     9 
       
    10 /***************************************************************************
       
    11  *                                                                         *
       
    12  *   This program is free software; you can redistribute it and/or modify  *
       
    13  *   it under the terms of the GNU General Public License as published by  *
       
    14  *   the Free Software Foundation; either version 2 of the License, or     *
       
    15  *   (at your option) any later version.                                   *
       
    16  *                                                                         *
       
    17  *   This program is distributed in the hope that it will be useful,       *
       
    18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
       
    19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
       
    20  *   GNU General Public License for more details.                          *
       
    21  *                                                                         *
       
    22  *   You should have received a copy of the GNU General Public License     *
       
    23  *   along with this program;                                              *
       
    24  *   if not, write to the Free Software Foundation, Inc.,                  *
       
    25  *   59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *
       
    26  *                                                                         *
       
    27  ***************************************************************************/
       
    28 
       
    29 #include <getopt.h>
       
    30 #include <stdio.h>
       
    31 #include <stdint.h>
       
    32 #include <string.h>
       
    33 #include <stdlib.h>
       
    34 
       
    35 #include <string>
       
    36 
       
    37 #include <glcdgraphics/bitmap.h>
       
    38 #include <glcdgraphics/image.h>
       
    39 #include <glcdgraphics/imagefile.h>
       
    40 #include <glcdgraphics/glcd.h>
       
    41 #include <glcdgraphics/pbm.h>
       
    42 
       
    43 #include "bmp.h"
       
    44 #include "tiff.h"
       
    45 #include "tuxbox.h"
       
    46 
       
    47 static const char *prgname = "convpic";
       
    48 static const char *VERSION = "0.1.1";
       
    49 
       
    50 unsigned int delay = 250;
       
    51 
       
    52 
       
    53 enum ePicFormat
       
    54 {
       
    55     pfUndefined,
       
    56     pfTIFF,
       
    57     pfBMP,
       
    58     pfGLCD,
       
    59     pfPBM,
       
    60     pfTUXBOX
       
    61 };
       
    62 
       
    63 void usage(void);
       
    64 
       
    65 ePicFormat getFormat(const char* szFile)
       
    66 {
       
    67     static const struct tagformats {const char* szExt; ePicFormat picformat;} formats[] =
       
    68     {
       
    69         {".tiff", pfTIFF  },
       
    70         {".tif",  pfTIFF  },
       
    71         {".bmp",  pfBMP   },
       
    72         {".glcd", pfGLCD  },
       
    73         {".pbm",  pfPBM   },
       
    74         {".ani",  pfTUXBOX}
       
    75     };
       
    76     ePicFormat pf = pfUndefined;
       
    77 
       
    78     if (szFile)
       
    79     {
       
    80         for (int i = strlen(szFile) - 1; i >= 0; i--)
       
    81         {
       
    82             if (*(szFile+i) == '.' && strlen(szFile + i + 1))
       
    83             {
       
    84                 for (unsigned int n = 0; n < sizeof(formats)/sizeof(*formats); n++)
       
    85                 {
       
    86                     if (!strcasecmp((szFile+i), formats[n].szExt))
       
    87                     {
       
    88                         return formats[n].picformat;
       
    89                     }
       
    90                 }
       
    91             }
       
    92         }
       
    93     }
       
    94     return pf;
       
    95 }
       
    96 
       
    97 GLCD::cImageFile * GetFileTranslator(ePicFormat Format)
       
    98 {
       
    99     switch (Format)
       
   100     {
       
   101         case pfGLCD:
       
   102             return new GLCD::cGLCDFile();
       
   103 
       
   104         case pfPBM:
       
   105             return new GLCD::cPBMFile();
       
   106 
       
   107         case pfBMP:
       
   108             return new cBMPFile();
       
   109 
       
   110         case pfTIFF:
       
   111             return new cTIFFFile();
       
   112 
       
   113         case pfTUXBOX:
       
   114             return new cTuxBoxFile();
       
   115 
       
   116         default:
       
   117             return NULL;
       
   118     }
       
   119 
       
   120 }
       
   121 
       
   122 int main(int argc, char *argv[]) {
       
   123     ePicFormat  inFormat = pfUndefined;
       
   124     ePicFormat  outFormat = pfUndefined;
       
   125     std::string  inFile = "";
       
   126     std::string  outFile = "";
       
   127     GLCD::cImage  image;
       
   128     GLCD::cImage  nextImage;
       
   129     GLCD::cImageFile *  pInBitmap = NULL;
       
   130     GLCD::cImageFile *  pOutBitmap = NULL;
       
   131     bool  bError = false;
       
   132     bool  bInvert = false;
       
   133     bool  bDelay = false;
       
   134 
       
   135 
       
   136     static struct option long_options[] =
       
   137     {
       
   138         {"invert",         no_argument, NULL, 'n'},
       
   139         {"infile",   required_argument, NULL, 'i'},
       
   140         {"outfile",  required_argument, NULL, 'o'},
       
   141         {"delay",    required_argument, NULL, 'd'},
       
   142         { NULL}
       
   143     };
       
   144 
       
   145     int c, option_index = 0;
       
   146     while ((c=getopt_long(argc,argv,"ni:o:d:",long_options, &option_index))!=-1) {
       
   147         switch (c) {
       
   148             case 'n':
       
   149                 bInvert = true;
       
   150                 break;
       
   151 
       
   152             case 'i':
       
   153                 inFile = optarg;
       
   154                 break;
       
   155 
       
   156             case 'o':
       
   157                 outFile = optarg;
       
   158                 break;
       
   159 
       
   160             case 'd':
       
   161                 delay = atoi(optarg);
       
   162                 bDelay = true;
       
   163                 if (delay < 10)
       
   164                 {
       
   165                     fprintf(stderr, "Warning: You have specify a to short delay, minimum are 10 ms\n");
       
   166                     delay = 10;
       
   167                 }
       
   168                 break;
       
   169 
       
   170             default:
       
   171                 return 1;
       
   172         }
       
   173     }
       
   174 
       
   175     if (inFile.length() == 0)
       
   176     {
       
   177         fprintf(stderr, "ERROR: You have to specify the infile (-i filename)\n");
       
   178         bError = true;
       
   179     }
       
   180 
       
   181     if (pfUndefined == (inFormat = getFormat(inFile.c_str())))
       
   182     {
       
   183         fprintf(stderr, "ERROR: You have to specify a correct extension for the %s\n", inFile.c_str());
       
   184         bError = true;
       
   185     }
       
   186 
       
   187     if (outFile.length() == 0)
       
   188     {
       
   189         fprintf(stderr, "ERROR: You have to specify the outfile (-o filename)\n");
       
   190         bError = true;
       
   191     }
       
   192 
       
   193     if (pfUndefined == (outFormat = getFormat(outFile.c_str())))
       
   194     {
       
   195         fprintf(stderr, "ERROR: You have to specify a correct extension for the %s \n", outFile.c_str());
       
   196         bError = true;
       
   197     }
       
   198 
       
   199     if (bError)
       
   200     {
       
   201         usage();
       
   202         return 1;
       
   203     }
       
   204 
       
   205 
       
   206     pInBitmap = GetFileTranslator(inFormat);
       
   207     if (!pInBitmap)
       
   208         return 2;
       
   209 
       
   210     pOutBitmap = GetFileTranslator(outFormat);
       
   211     if (!pOutBitmap)
       
   212         return 3;
       
   213 
       
   214     // Load Picture
       
   215     fprintf(stdout, "loading %s\n", inFile.c_str());
       
   216     bError = !pInBitmap->Load(image, inFile);
       
   217     if (!bError)
       
   218     {
       
   219         // Load more in files
       
   220         while (optind < argc && !bError)
       
   221         {
       
   222             inFile = argv[optind++];
       
   223             inFormat = getFormat(inFile.c_str());
       
   224             if (inFormat == pfUndefined)
       
   225             {
       
   226                 fprintf(stderr, "ERROR: You have to specify a correct extension for the %s\n", inFile.c_str());
       
   227                 bError = true;
       
   228                 break;
       
   229             }
       
   230             pInBitmap = GetFileTranslator(inFormat);
       
   231             if (!pInBitmap)
       
   232                 break;
       
   233 
       
   234             fprintf(stdout, "loading %s\n", inFile.c_str());
       
   235             if (pInBitmap->Load(nextImage, inFile))
       
   236             {
       
   237                 uint16_t i;
       
   238                 for (i = 0; i < nextImage.Count(); i++)
       
   239                 {
       
   240                     image.AddBitmap(new GLCD::cBitmap(*nextImage.GetBitmap(i)));
       
   241                 }
       
   242             }
       
   243         }
       
   244         if (bDelay)
       
   245             image.SetDelay(delay);
       
   246         if (bInvert)
       
   247         {
       
   248             uint16_t i;
       
   249             for (i = 0; i < image.Count(); i++)
       
   250             {
       
   251                 image.GetBitmap(i)->Invert();
       
   252             }
       
   253         }
       
   254         fprintf(stdout, "saving %s\n", outFile.c_str());
       
   255         bError = !pOutBitmap->Save(image, outFile);
       
   256     }
       
   257     if (bError) {
       
   258         return 4;
       
   259     }
       
   260 
       
   261     fprintf(stdout, "conversion compeleted successfully.\n\n");
       
   262 
       
   263     return 0;
       
   264 }
       
   265 
       
   266 void usage(void)
       
   267 {
       
   268     fprintf(stdout, "\n");
       
   269     fprintf(stdout, "%s v%s\n", prgname, VERSION);
       
   270     fprintf(stdout, "%s is a tool to convert images to a simple format (*.glcd)\n", prgname);
       
   271     fprintf(stdout, "        that is used by the graphlcd plugin for VDR.\n\n");
       
   272     fprintf(stdout, "  Usage: %s [-n] -i file[s...] -o outfile \n\n", prgname);
       
   273     fprintf(stdout, "  -n  --invert      inverts the output (default: none)\n");
       
   274     fprintf(stdout, "  -i  --infile      specifies the name of the input file[s]\n");
       
   275     fprintf(stdout, "  -o  --outfile     specifies the name of the output file\n");
       
   276     fprintf(stdout, "  -d  --delay       specifies the delay between multiple images [Default: %d ms] \n",delay);
       
   277     fprintf(stdout, "\n" );
       
   278     fprintf(stdout, "  example: %s -i vdr-logo.bmp -o vdr-logo.glcd \n", prgname );
       
   279 }