graphlcd-base/glcdgraphics/pbm.c
changeset 4 df6a40031aa5
equal deleted inserted replaced
3:d0e62fc47285 4:df6a40031aa5
       
     1 /*
       
     2  * GraphLCD graphics library
       
     3  *
       
     4  * pbm.c  -  PBM file loading and saving
       
     5  *
       
     6  * This file is released under the GNU General Public License. Refer
       
     7  * to the COPYING file distributed with this package.
       
     8  *
       
     9  * (c) 2006 Andreas Regel <andreas.regel AT powarman.de>
       
    10  */
       
    11 
       
    12 #include <stdio.h>
       
    13 #include <stdint.h>
       
    14 #include <syslog.h>
       
    15 
       
    16 #include <string>
       
    17 
       
    18 #include "bitmap.h"
       
    19 #include "pbm.h"
       
    20 #include "image.h"
       
    21 
       
    22 
       
    23 namespace GLCD
       
    24 {
       
    25 
       
    26 cPBMFile::cPBMFile()
       
    27 {
       
    28 }
       
    29 
       
    30 cPBMFile::~cPBMFile()
       
    31 {
       
    32 }
       
    33 
       
    34 bool cPBMFile::Load(cImage & image, const std::string & fileName)
       
    35 {
       
    36     FILE * pbmFile;
       
    37     char str[32];
       
    38     int i;
       
    39     int ch;
       
    40     int w;
       
    41     int h;
       
    42 
       
    43     pbmFile = fopen(fileName.c_str(), "rb");
       
    44     if (!pbmFile)
       
    45         return false;
       
    46 
       
    47     i = 0;
       
    48     while ((ch = getc(pbmFile)) != EOF && i < 31)
       
    49     {
       
    50         if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
       
    51             break;
       
    52         str[i] = ch;
       
    53         i++;
       
    54     }
       
    55     if (ch == EOF)
       
    56     {
       
    57         fclose(pbmFile);
       
    58         return false;
       
    59     }
       
    60     str[i] = 0;
       
    61     if (strcmp(str, "P4") != 0)
       
    62         return false;
       
    63 
       
    64     while ((ch = getc(pbmFile)) == '#')
       
    65     {
       
    66         while ((ch = getc(pbmFile)) != EOF)
       
    67         {
       
    68             if (ch == '\n' || ch == '\r')
       
    69                 break;
       
    70         }
       
    71     }
       
    72     if (ch == EOF)
       
    73     {
       
    74         fclose(pbmFile);
       
    75         return false;
       
    76     }
       
    77     i = 0;
       
    78     str[i] = ch;
       
    79     i += 1;
       
    80     while ((ch = getc(pbmFile)) != EOF && i < 31)
       
    81     {
       
    82         if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
       
    83             break;
       
    84         str[i] = ch;
       
    85         i++;
       
    86     }
       
    87     if (ch == EOF)
       
    88     {
       
    89         fclose(pbmFile);
       
    90         return false;
       
    91     }
       
    92     str[i] = 0;
       
    93     w = atoi(str);
       
    94 
       
    95     i = 0;
       
    96     while ((ch = getc(pbmFile)) != EOF && i < 31)
       
    97     {
       
    98         if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
       
    99             break;
       
   100         str[i] = ch;
       
   101         i++;
       
   102     }
       
   103     if (ch == EOF)
       
   104     {
       
   105         fclose(pbmFile);
       
   106         return false;
       
   107     }
       
   108     str[i] = 0;
       
   109     h = atoi(str);
       
   110 
       
   111     image.Clear();
       
   112     image.SetWidth(w);
       
   113     image.SetHeight(h);
       
   114     image.SetDelay(100);
       
   115     unsigned char * bmpdata = new unsigned char[h * ((w + 7) / 8)];
       
   116     if (bmpdata)
       
   117     {
       
   118         if (fread(bmpdata, h * ((w + 7) / 8), 1, pbmFile) != 1)
       
   119         {
       
   120             delete[] bmpdata;
       
   121             fclose(pbmFile);
       
   122             image.Clear();
       
   123             return false;
       
   124         }
       
   125         image.AddBitmap(new cBitmap(w, h, bmpdata));
       
   126         delete[] bmpdata;
       
   127     }
       
   128     else
       
   129     {
       
   130         syslog(LOG_ERR, "glcdgraphics: malloc failed (cPBMFile::Load).");
       
   131         fclose(pbmFile);
       
   132         return false;
       
   133     }
       
   134     fclose(pbmFile);
       
   135     syslog(LOG_DEBUG, "glcdgraphics: image %s loaded.", fileName.c_str());
       
   136 
       
   137     return true;
       
   138 }
       
   139 
       
   140 bool cPBMFile::Save(cImage & image, const std::string & fileName)
       
   141 {
       
   142     FILE * fp;
       
   143     char str[32];
       
   144     const cBitmap * bitmap;
       
   145 
       
   146     if (image.Count() == 1)
       
   147     {
       
   148         fp = fopen(fileName.c_str(), "wb");
       
   149         if (fp)
       
   150         {
       
   151             bitmap = image.GetBitmap(0);
       
   152             if (bitmap)
       
   153             {
       
   154                 sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height());
       
   155                 fwrite(str, strlen(str), 1, fp);
       
   156                 fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp);
       
   157             }
       
   158             fclose(fp);
       
   159         }
       
   160     }
       
   161     else
       
   162     {
       
   163         uint16_t i;
       
   164         char tmpStr[256];
       
   165 
       
   166         for (i = 0; i < image.Count(); i++)
       
   167         {
       
   168             sprintf(tmpStr, "%.248s.%05d", fileName.c_str(), i);
       
   169             fp = fopen(tmpStr, "wb");
       
   170             if (fp)
       
   171             {
       
   172                 bitmap = image.GetBitmap(i);
       
   173                 if (bitmap)
       
   174                 {
       
   175                     sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height());
       
   176                     fwrite(str, strlen(str), 1, fp);
       
   177                     fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp);
       
   178                 }
       
   179                 fclose(fp);
       
   180             }
       
   181         }
       
   182     }
       
   183     return true;
       
   184 }
       
   185 
       
   186 } // end of namespace
       
   187