2 * GraphLCD graphics library
4 * pbm.c - PBM file loading and saving
6 * This file is released under the GNU General Public License. Refer
7 * to the COPYING file distributed with this package.
9 * (c) 2006 Andreas Regel <andreas.regel AT powarman.de>
34 bool cPBMFile::Load(cImage & image, const std::string & fileName)
43 pbmFile = fopen(fileName.c_str(), "rb");
48 while ((ch = getc(pbmFile)) != EOF && i < 31)
50 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
61 if (strcmp(str, "P4") != 0)
64 while ((ch = getc(pbmFile)) == '#')
66 while ((ch = getc(pbmFile)) != EOF)
68 if (ch == '\n' || ch == '\r')
80 while ((ch = getc(pbmFile)) != EOF && i < 31)
82 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
96 while ((ch = getc(pbmFile)) != EOF && i < 31)
98 if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
115 unsigned char * bmpdata = new unsigned char[h * ((w + 7) / 8)];
118 if (fread(bmpdata, h * ((w + 7) / 8), 1, pbmFile) != 1)
125 image.AddBitmap(new cBitmap(w, h, bmpdata));
130 syslog(LOG_ERR, "glcdgraphics: malloc failed (cPBMFile::Load).");
135 syslog(LOG_DEBUG, "glcdgraphics: image %s loaded.", fileName.c_str());
140 bool cPBMFile::Save(cImage & image, const std::string & fileName)
144 const cBitmap * bitmap;
146 if (image.Count() == 1)
148 fp = fopen(fileName.c_str(), "wb");
151 bitmap = image.GetBitmap(0);
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);
166 for (i = 0; i < image.Count(); i++)
168 sprintf(tmpStr, "%.248s.%05d", fileName.c_str(), i);
169 fp = fopen(tmpStr, "wb");
172 bitmap = image.GetBitmap(i);
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);
186 } // end of namespace