diff options
Diffstat (limited to 'glcdgraphics/pbm.c')
-rw-r--r-- | glcdgraphics/pbm.c | 93 |
1 files changed, 76 insertions, 17 deletions
diff --git a/glcdgraphics/pbm.c b/glcdgraphics/pbm.c index 2bca3ef..3bf94c1 100644 --- a/glcdgraphics/pbm.c +++ b/glcdgraphics/pbm.c @@ -6,7 +6,9 @@ * This file is released under the GNU General Public License. Refer * to the COPYING file distributed with this package. * - * (c) 2006 Andreas Regel <andreas.regel AT powarman.de> + * (c) 2006-2010 Andreas Regel <andreas.regel AT powarman.de> + * (c) 2010-2013 Wolfgang Astleitner <mrwastl AT users sourceforge net> + * Andreas 'randy' Weinberger */ #include <stdio.h> @@ -113,18 +115,35 @@ bool cPBMFile::Load(cImage & image, const std::string & fileName) image.SetWidth(w); image.SetHeight(h); image.SetDelay(100); - unsigned char * bmpdata = new unsigned char[h * ((w + 7) / 8)]; - if (bmpdata) + + unsigned char * bmpdata_raw = new unsigned char[h * ((w + 7) / 8)]; + uint32_t * bmpdata = new uint32_t[h * w]; + if (bmpdata && bmpdata_raw) { - if (fread(bmpdata, h * ((w + 7) / 8), 1, pbmFile) != 1) - { + if (fread(bmpdata_raw, h * ((w + 7) / 8), 1, pbmFile) != 1) + { + delete[] bmpdata; + fclose(pbmFile); + image.Clear(); + return false; + } + int colsize = (w+7)/8; + for (int j = 0; j < h; j++) { + for (int i = 0; i < w; i++) { + if ( bmpdata_raw[j*colsize + (i>>3)] & (1 << (7-(i%8))) ) { + bmpdata[j*w+i] = cColor::Black; + } else { + bmpdata[j*w+i] = cColor::White; + } + } + } + delete [] bmpdata_raw; + + cBitmap * b = new cBitmap(w, h, bmpdata); + b->SetMonochrome(true); + //image.AddBitmap(new cBitmap(width, height, bmpdata)); + image.AddBitmap(b); delete[] bmpdata; - fclose(pbmFile); - image.Clear(); - return false; - } - image.AddBitmap(new cBitmap(w, h, bmpdata)); - delete[] bmpdata; } else { @@ -133,7 +152,7 @@ bool cPBMFile::Load(cImage & image, const std::string & fileName) return false; } fclose(pbmFile); - syslog(LOG_DEBUG, "glcdgraphics: image %s loaded.", fileName.c_str()); + syslog(LOG_DEBUG, "glcdgraphics: image '%s' loaded.", fileName.c_str()); return true; } @@ -143,6 +162,9 @@ bool cPBMFile::Save(cImage & image, const std::string & fileName) FILE * fp; char str[32]; const cBitmap * bitmap; + unsigned char* rawdata = NULL; + int rawdata_size = 0; + const uint32_t * bmpdata = NULL; if (image.Count() == 1) { @@ -150,34 +172,71 @@ bool cPBMFile::Save(cImage & image, const std::string & fileName) if (fp) { bitmap = image.GetBitmap(0); - if (bitmap) + rawdata_size = ((bitmap->Width() + 7) / 8) * bitmap->Height(); + rawdata = new unsigned char[ rawdata_size ]; + bmpdata = bitmap->Data(); + + if (bitmap && rawdata && bmpdata) { + memset(rawdata, 0, rawdata_size ); + for (int y = 0; y < bitmap->Height(); y++) { + int startpos = y * ((bitmap->Width() + 7) / 8); + for (int x = 0; x < bitmap->Width(); x++) { + if (bmpdata[ y * bitmap->Width() + x ] == cColor::White) { + rawdata[ startpos + (x / 8) ] |= (1 << ( 7 - ( x % 8 ) )); + } + } + } sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height()); fwrite(str, strlen(str), 1, fp); - fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp); + fwrite(rawdata, rawdata_size, 1, fp); } fclose(fp); + delete[] rawdata; + rawdata = NULL; } } else { uint16_t i; char tmpStr[256]; + size_t pos = fileName.find_last_of('.'); + std::string fileExt = ""; + std::string fileBase = fileName; + if (pos != std::string::npos) { + fileExt = fileName.substr(pos); + fileBase = fileName.substr(0, fileName.length() - fileExt.length()); + } for (i = 0; i < image.Count(); i++) { - sprintf(tmpStr, "%.248s.%05d", fileName.c_str(), i); + sprintf(tmpStr, "%.244s-%05d%s", fileBase.c_str(), i, fileExt.c_str()); fp = fopen(tmpStr, "wb"); if (fp) { bitmap = image.GetBitmap(i); - if (bitmap) + rawdata_size = ((bitmap->Width() + 7) / 8) * bitmap->Height(); + rawdata = new unsigned char[ rawdata_size ]; + bmpdata = bitmap->Data(); + + if (bitmap && rawdata && bmpdata) { + memset(rawdata, 0, rawdata_size ); + for (int y = 0; y < bitmap->Height(); y++) { + int startpos = y * ((bitmap->Width() + 7) / 8); + for (int x = 0; x < bitmap->Width(); x++) { + if (bmpdata[ y * bitmap->Width() + x ] == cColor::Black) { + rawdata[ startpos + (x / 8) ] |= (1 << ( 7 - ( x % 8 ) )); + } + } + } sprintf(str, "P4\n%d %d\n", bitmap->Width(), bitmap->Height()); fwrite(str, strlen(str), 1, fp); - fwrite(bitmap->Data(), bitmap->LineSize() * bitmap->Height(), 1, fp); + fwrite(rawdata, rawdata_size, 1, fp); } fclose(fp); + delete[] rawdata; + rawdata = NULL; } } } |