diff options
author | mrwastl <mrwastl@users.sourceforge.net> | 2011-06-29 01:17:41 +0200 |
---|---|---|
committer | mrwastl <mrwastl@users.sourceforge.net> | 2011-06-29 01:17:41 +0200 |
commit | 0178c698712832d4d19e9303edacbe7e8a034891 (patch) | |
tree | 4283c7e775abff7d2d4530ca530954d2516bcf62 | |
parent | 861d73246c73ae3201a9be96483d04f3cc5ba741 (diff) | |
download | graphlcd-base-0178c698712832d4d19e9303edacbe7e8a034891.tar.gz graphlcd-base-0178c698712832d4d19e9303edacbe7e8a034891.tar.bz2 |
fix saving of PBMs, fix crtfont and genfont; remove cBitmap::LoadPBM()/::SavePBM (use cPMFile::Load()/Save()) instead; fixed cBitmap::SubBitmap(); add static methods for converting from and to 1BPP
-rw-r--r-- | glcdgraphics/bitmap.c | 59 | ||||
-rw-r--r-- | glcdgraphics/bitmap.h | 5 | ||||
-rw-r--r-- | glcdgraphics/font.c | 19 | ||||
-rw-r--r-- | tools/crtfont/crtfont.c | 30 | ||||
-rw-r--r-- | tools/genfont/genfont.c | 32 |
5 files changed, 125 insertions, 20 deletions
diff --git a/glcdgraphics/bitmap.c b/glcdgraphics/bitmap.c index dbeb27d..0ab27b6 100644 --- a/glcdgraphics/bitmap.c +++ b/glcdgraphics/bitmap.c @@ -771,13 +771,69 @@ cBitmap * cBitmap::SubBitmap(int x1, int y1, int x2, int y2) const { for (xt = 0; xt < w; xt++) { - cl = bitmap[(w*yt+y1)+xt+x1]; + cl = this->GetPixel(xt+x1, yt+y1); bmp->DrawPixel(xt,yt, cl); } } return bmp; } + +// convert a new 32 bpp bitmap to an old style 1bpp bitmap (bit order: from least to most sig. bit: byte: [ 0, 1, 2, 3, 4, 5, 6, 7 ]) +// if IsMonochrome(): ignore threshold +const unsigned char* cBitmap::ConvertTo1BPP(const cBitmap & bitmap, int threshold) +{ + if (bitmap.Width() <= 0 || bitmap.Height() <= 0) + return NULL; + + int cols = (bitmap.Width() + 7 ) / 8; + unsigned char* monobmp = new unsigned char[ cols * bitmap.Height() ]; + if (!monobmp) + return NULL; + + memset(monobmp, 0, cols * bitmap.Height()); + + uint32_t col; + unsigned char greyval = 0; + bool ismono = bitmap.IsMonochrome(); + + for (int y = 0; y < bitmap.Height(); y++) { + for (int x = 0; x < bitmap.Width(); x++) { + col = bitmap.GetPixel(x, y); + if (! ismono) { + // col -> grey level + greyval = (((0x00FF0000 & col) >> 16) * 77 + ((0x0000FF00 * col) >> 8) * 150 + (0x000000FF & col) * 28) / 255; + col = (greyval >= threshold) ? cColor::White : cColor::Black; + } + + if ( col == cColor::Black) + monobmp[ y * cols + (x >> 3) ] |= ( 1 << ( 7 - (x % 8) ) ); + } + } + return monobmp; +} + + +// convert an old style 1 bpp bitmap to new 32 bpp bitmap (bit order: from least to most sig. bit: byte: [ 0, 1, 2, 3, 4, 5, 6, 7 ]) +const cBitmap* cBitmap::ConvertFrom1BPP(const unsigned char* monobmp, int w, int h, uint32_t fg, uint32_t bg) +{ + if (w <= 0 || h <= 0 || !monobmp) + return NULL; + + cBitmap* bmp = new cBitmap(w, h, bg); + if (bmp == NULL) + return NULL; + + int cols = (w + 7 ) / 8; + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + bmp->DrawPixel(x, y, ( monobmp[ y * cols + (x >> 3) ] & ( 1 << (7 - (x % 8)) ) ) ? fg : bg ); + } + } + return bmp; +} + +#if 0 bool cBitmap::LoadPBM(const std::string & fileName) { #ifdef DEBUG @@ -890,5 +946,6 @@ void cBitmap::SavePBM(const std::string & fileName) fclose(fp); } } +#endif } // end of namespace diff --git a/glcdgraphics/bitmap.h b/glcdgraphics/bitmap.h index 8067cc3..dcfa26d 100644 --- a/glcdgraphics/bitmap.h +++ b/glcdgraphics/bitmap.h @@ -133,9 +133,14 @@ public: void SetProcessAlpha(bool procAlpha) { processAlpha = procAlpha; } bool IsProcessAlpha(void) const { return processAlpha; } + + static const unsigned char* ConvertTo1BPP(const cBitmap & bitmap, int threshold = 127); + static const cBitmap* ConvertFrom1BPP(const unsigned char* monobmp, int w, int h, uint32_t fg = cColor::White, uint32_t bg = cColor::Black); +#if 0 bool LoadPBM(const std::string & fileName); void SavePBM(const std::string & fileName); +#endif }; } // end of namespace diff --git a/glcdgraphics/font.c b/glcdgraphics/font.c index ff42514..24a44a0 100644 --- a/glcdgraphics/font.c +++ b/glcdgraphics/font.c @@ -176,6 +176,7 @@ bool cFont::LoadFNT(const std::string & fileName, const std::string & encoding) int num = 0; uint dot; uint b; cBitmap * charBitmap = new cBitmap(charWidth, fontHeight); + charBitmap->SetMonochrome(true); charBitmap->Clear(); for (num=0; num<fontHeight * ((charWidth + 7) / 8);num++) { y = (charWidth + 7) / 8; @@ -220,7 +221,7 @@ bool cFont::SaveFNT(const std::string & fileName) const numChars = 0; for (i = 0; i < 256; i++) { - if (characters[i]) + if (GetCharacter(i)) { numChars++; } @@ -243,17 +244,20 @@ bool cFont::SaveFNT(const std::string & fileName) const // write font file header fwrite(fhdr, kFontHeaderSize, 1, fontFile); + const cBitmap* charbmp = NULL; for (i = 0; i < 256; i++) { - if (characters[i]) + charbmp = GetCharacter(i); + if (charbmp) { chdr[0] = (uint8_t) i; chdr[1] = (uint8_t) (i >> 8); - chdr[2] = (uint8_t) characters[i]->Width(); - chdr[3] = (uint8_t) (characters[i]->Width() >> 8); + chdr[2] = (uint8_t) charbmp->Width(); + chdr[3] = (uint8_t) (charbmp->Width() >> 8); fwrite(chdr, kCharHeaderSize, 1, fontFile); -// fwrite(characters[i]->Data(), totalHeight * characters[i]->LineSize(), 1, fontFile); - fwrite(characters[i]->Data(), totalHeight * characters[i]->Width(), 1, fontFile); + const unsigned char* monobmp = cBitmap::ConvertTo1BPP(*charbmp); + fwrite(monobmp /*characters[i]->Data()*/, totalHeight * ((charbmp->Width() + 7) / 8), 1, fontFile); + delete[] monobmp; } } @@ -467,7 +471,8 @@ const cBitmap * cFont::GetCharacter(uint32_t ch) const } else { // now, fill our pixel data cBitmap *charBitmap = new cBitmap(face->glyph->advance.x >> 6, TotalHeight()); - charBitmap->Clear(); + charBitmap->Clear(cColor::White); + charBitmap->SetMonochrome(true); unsigned char * bufPtr = face->glyph->bitmap.buffer; unsigned char pixel; for (int y = 0; y < face->glyph->bitmap.rows; y++) diff --git a/tools/crtfont/crtfont.c b/tools/crtfont/crtfont.c index 97e57fe..fc7beef 100644 --- a/tools/crtfont/crtfont.c +++ b/tools/crtfont/crtfont.c @@ -10,7 +10,9 @@ * This file is released under the GNU General Public License. Refer * to the COPYING file distributed with this package. * - * (c) 2004 Andreas Regel <andreas.regel AT powarman.de> + * (c) 2004 Andreas Regel <andreas.regel AT powarman.de> + * (c) 2010-2011 Wolfgang Astleitner <mrwastl AT users sourceforge net> + * Andreas 'randy' Weinberger */ #include <getopt.h> @@ -20,6 +22,8 @@ #include <string.h> #include <glcdgraphics/bitmap.h> #include <glcdgraphics/font.h> +#include <glcdgraphics/image.h> +#include <glcdgraphics/pbm.h> static const char *prgname = "crtfont"; static const char *version = "0.1.6"; @@ -151,15 +155,25 @@ int main(int argc, char *argv[]) // Load Picture switch (picFormat) { - case PBM: - bitmap = new GLCD::cBitmap(0, 0); - bitmap->LoadPBM(picName); - if (!bitmap) - { + case PBM: { + GLCD::cPBMFile pbm; + GLCD::cImage* image = new GLCD::cImage(); + + if (!image) + return 3; + + if (pbm.Load(*image, picName) == false) { fprintf(stderr, "Cannot open file: %s\n",picName); + delete image; return 2; } - break; + + const GLCD::cBitmap * pbmbm = image->GetBitmap(); + bitmap = new GLCD::cBitmap(*pbmbm); + delete image; + pbmbm = NULL; + } + break; default: return 2; @@ -296,7 +310,7 @@ void usage(void) fprintf(stdout, " graphlcd plugin for VDR.\n\n"); fprintf(stdout, " Usage: %s -f <format> -b bmpfile -d descfile -o outfile\n\n", prgname); fprintf(stdout, " -f --format specifies the format of the bitmap. Possible values are:\n"); - fprintf(stdout, " PBM : file is an binary PBM file\n" ); + fprintf(stdout, " PBM : file is a binary PBM file\n" ); fprintf(stdout, " -b --bmpfile specifies the name of the bitmap file (*.pbm)\n"); fprintf(stdout, " -d --descfile specifies the name of the description file (*.desc)\n"); fprintf(stdout, " -o --outfile specifies the name of the output file (*.fnt)\n"); diff --git a/tools/genfont/genfont.c b/tools/genfont/genfont.c index ce9698d..980c55b 100644 --- a/tools/genfont/genfont.c +++ b/tools/genfont/genfont.c @@ -19,6 +19,8 @@ #include <glcdgraphics/bitmap.h> #include <glcdgraphics/font.h> +#include <glcdgraphics/image.h> +#include <glcdgraphics/pbm.h> static const char *prgname = "genfont"; static const char *version = "0.0.2"; @@ -138,9 +140,10 @@ int main(int argc, char *argv[]) fprintf(descFile, "spacebetween:%d\n", 0); fprintf(descFile, "spacewidth:%d\n", 0); - for (unsigned int i = 0; i < 256; i++) + for (uint32_t i = 0; i < 256; i++) { - const GLCD::cBitmap * charBitmap = font.GetCharacter((char) i); + const GLCD::cBitmap * charBitmap = font.GetCharacter(i); + if (charBitmap == NULL) continue; @@ -155,12 +158,33 @@ int main(int argc, char *argv[]) } } + // invert image + for(int y=0; y < bitmap->Height(); y++) { + for(int x=0; x < bitmap->Width(); x++) { + bitmap->DrawPixel(x, y, GLCD::cColor(bitmap->GetPixel(x, y)).Invert()); + } + } + if (posX > 0) // write last end marker fprintf(descFile, "%d\n", posX); fileName = outputName + ".pbm"; - bitmap->SavePBM(fileName); - delete bitmap; + + GLCD::cPBMFile pbm; + GLCD::cImage* image = new GLCD::cImage(); + + if (!image) + return 3; + + image->AddBitmap(bitmap); + + if (pbm.Save(*image, fileName) == false) { + fprintf(stderr, "Cannot save file: %s\n",fileName.c_str()); + delete image; + return 2; + } + fclose(descFile); + delete image; } fprintf(stdout, "Font successfully generated.\n"); |