diff options
Diffstat (limited to 'glcdgraphics')
-rw-r--r-- | glcdgraphics/bitmap.c | 19 | ||||
-rw-r--r-- | glcdgraphics/pbm.c | 41 |
2 files changed, 52 insertions, 8 deletions
diff --git a/glcdgraphics/bitmap.c b/glcdgraphics/bitmap.c index 1c5e4ed..dbeb27d 100644 --- a/glcdgraphics/bitmap.c +++ b/glcdgraphics/bitmap.c @@ -97,8 +97,10 @@ cBitmap::cBitmap(int width, int height, uint32_t initcol) printf("%s:%s(%d) cBitmap Size %03d * %03d\n", __FILE__, __FUNCTION__, __LINE__, width, height); #endif - bitmap = new uint32_t[width * height]; - Clear(initcol); + if (width > 0 && height > 0) { + bitmap = new uint32_t[width * height]; + Clear(initcol); + } } cBitmap::cBitmap(const cBitmap & b) @@ -117,7 +119,9 @@ cBitmap::cBitmap(const cBitmap & b) cBitmap::~cBitmap() { - delete[] bitmap; + if (bitmap) + delete[] bitmap; + bitmap = NULL; } void cBitmap::Clear(uint32_t color) @@ -726,6 +730,11 @@ int cBitmap::DrawCharacter(int x, int y, int xmax, uint32_t c, const cFont * fon uint32_t cBitmap::GetPixel(int x, int y) const { + if (x < 0 || x > width - 1) + return cColor::Transparent; + if (y < 0 || y > height - 1) + return cColor::Transparent; + uint32_t value; value = bitmap[y * width + x]; return value; @@ -756,6 +765,7 @@ cBitmap * cBitmap::SubBitmap(int x1, int y1, int x2, int y2) const if (!bmp || !bmp->Data()) return NULL; bmp->Clear(); + bmp->SetMonochrome(this->IsMonochrome()); for (yt = 0; yt < h; yt++) { @@ -848,7 +858,8 @@ bool cBitmap::LoadPBM(const std::string & fileName) str[i] = 0; h = atoi(str); - delete[] bitmap; + if (bitmap) + delete[] bitmap; width = w; height = h; bitmap = new uint32_t [width * height]; diff --git a/glcdgraphics/pbm.c b/glcdgraphics/pbm.c index 18b2ee3..2c83752 100644 --- a/glcdgraphics/pbm.c +++ b/glcdgraphics/pbm.c @@ -162,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) { @@ -169,14 +172,29 @@ 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::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(bitmap->Data(), bitmap->Width() * bitmap->Height(), 1, fp); + fwrite(rawdata, rawdata_size, 1, fp); } fclose(fp); + delete[] rawdata; + rawdata = NULL; } } else @@ -191,14 +209,29 @@ bool cPBMFile::Save(cImage & image, const std::string & fileName) 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(bitmap->Data(), bitmap->Width() * bitmap->Height(), 1, fp); + fwrite(rawdata, rawdata_size, 1, fp); } fclose(fp); + delete[] rawdata; + rawdata = NULL; } } } |