Re: [AD] True Colour font loading fix

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Saturday 02 April 2005 19:06, Evert Glebbeek wrote:
> The attached patch fixes problems loading high and true colour font
> bitmaps. 

Seems I once again forgot to attach the patch. Never mind, this one is 
better anyway :)
It should not affect 8bit fonts in any way, but it affects high/true colour 
bitmap fonts in the following way: they are converted to the current 
colourdepth when they are loaded, just as bitmaps are. The user is not 
able to override this because the font actually needs to be in the current 
colordepth to be useable at all.
That said, there is currently no API to convert a font to a selected 
colourdepth. This is a shame, but to add it now is quite a large change - 
too large in my opinion. Therefor, the glyph renderer will convert each 
glyph on the fly (using blit) before drawing it (using masked_blit with 
the new bitmap). This has obvious performance repercussions, which can be 
slightly reduced by making the bitmap static and reusing it, but I haven't 
bothered. A note in the docs that for optimal performance true colour 
fonts should be loaded after set_color_depth() has been called should 
suffice. This is not really different from bitmaps anyway.

Documentation to follow.

Evert
Index: src/font.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/font.c,v
retrieving revision 1.17
diff -u -r1.17 font.c
--- src/font.c	1 Apr 2005 13:45:17 -0000	1.17
+++ src/font.c	3 Apr 2005 19:21:57 -0000
@@ -904,7 +904,26 @@
 	    }
         }
         else {
-	    masked_blit(g, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
+            if (bitmap_color_depth(g) == bitmap_color_depth(bmp)) {
+	       masked_blit(g, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
+            }
+            else {
+               int color_conv_mode;
+               BITMAP *tbmp;
+               /* We need to do colour conversion - which is slow... */
+               
+               color_conv_mode = get_color_conversion();
+               set_color_conversion(COLORCONV_MOST | COLORCONV_KEEP_TRANS);
+               
+               tbmp = create_bitmap_ex(bitmap_color_depth(bmp), g->w, g->h);
+               blit(g, tbmp, 0, 0, 0, 0, g->w, g->h);
+               
+               set_color_conversion(color_conv_mode);
+               
+               masked_blit(tbmp, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
+               
+               destroy_bitmap(tbmp);
+            }
         }        
 
 	w = g->w;
Index: src/fontbmp.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/fontbmp.c,v
retrieving revision 1.4
diff -u -r1.4 fontbmp.c
--- src/fontbmp.c	1 Apr 2005 08:01:47 -0000	1.4
+++ src/fontbmp.c	3 Apr 2005 19:21:57 -0000
@@ -30,24 +30,20 @@
 /* splits bitmaps into sub-sprites, using regions bounded by col #255 */
 static void font_find_character(BITMAP *bmp, int *x, int *y, int *w, int *h)
 {
-   int c1;
-   int c2;
+   int c;
 
    if (bitmap_color_depth(bmp) == 8) {
-      c1 = 255;
-      c2 = 255;
+      c = 255;
    }
    else {
-      c1 = makecol_depth(bitmap_color_depth(bmp), 255, 255, 0);
-      c2 = makecol_depth(bitmap_color_depth(bmp), 0, 255, 255);
+      c = makecol_depth(bitmap_color_depth(bmp), 255, 255, 0);
    }
 
    /* look for top left corner of character */
-   while ((getpixel(bmp, *x, *y) != c1) || 
-	  (getpixel(bmp, *x+1, *y) != c2) ||
-	  (getpixel(bmp, *x, *y+1) != c2) ||
-	  (getpixel(bmp, *x+1, *y+1) == c1) ||
-	  (getpixel(bmp, *x+1, *y+1) == c2)) {
+   while ((getpixel(bmp, *x, *y) != c) || 
+	  (getpixel(bmp, *x+1, *y) != c) ||
+	  (getpixel(bmp, *x, *y+1) != c) ||
+	  (getpixel(bmp, *x+1, *y+1) == c)) {
       (*x)++;
       if (*x >= bmp->w) {
 	 *x = 0;
@@ -62,15 +58,15 @@
 
    /* look for right edge of character */
    *w = 0;
-   while ((getpixel(bmp, *x+*w+1, *y) == c2) &&
-	  (getpixel(bmp, *x+*w+1, *y+1) != c2) &&
+   while ((getpixel(bmp, *x+*w+1, *y) == c) &&
+	  (getpixel(bmp, *x+*w+1, *y+1) != c) &&
 	  (*x+*w+1 <= bmp->w))
       (*w)++;
 
    /* look for bottom edge of character */
    *h = 0;
-   while ((getpixel(bmp, *x, *y+*h+1) == c2) &&
-	  (getpixel(bmp, *x+1, *y+*h+1) != c2) &&
+   while ((getpixel(bmp, *x, *y+*h+1) == c) &&
+	  (getpixel(bmp, *x+1, *y+*h+1) != c) &&
 	  (*y+*h+1 <= bmp->h))
       (*h)++;
 }
@@ -129,12 +125,12 @@
    for(i = 0; i < num; i++) {
       if(w > 0 && h > 0) font_find_character(import_bmp, &import_x, &import_y, &w, &h);
       if(w <= 0 || h <= 0) {
-	 bits[i] = create_bitmap_ex(8, 8, 8);
+	 bits[i] = create_bitmap_ex(bitmap_color_depth(import_bmp), 8, 8);
 	 if(!bits[i]) return -1;
 	 clear_to_color(bits[i], 255);
       }
       else {
-	 bits[i] = create_bitmap_ex(8, w, h);
+	 bits[i] = create_bitmap_ex(bitmap_color_depth(import_bmp), w, h);
 	 if(!bits[i]) return -1;
 	 blit(import_bmp, bits[i], import_x + 1, import_y + 1, 0, 0, w, h);
 	 import_x += w;
@@ -197,21 +193,15 @@
    FONT *f;
    ASSERT(fname);
 
-   /* Don't change the colourdepth of the bitmap */
+   /* Don't change the colourdepth of the bitmap if it is 8 bit */
    color_conv_mode = get_color_conversion();
-   set_color_conversion(COLORCONV_NONE);
+   set_color_conversion(COLORCONV_MOST | COLORCONV_KEEP_TRANS);
    import_bmp = load_bitmap(fname, pal);
    set_color_conversion(color_conv_mode);
 
    if(!import_bmp) 
      return NULL;
 
-   if(bitmap_color_depth(import_bmp) != 8) {
-      destroy_bitmap(import_bmp);
-      import_bmp = NULL;
-      return NULL;
-   }
-
    f = grab_font_from_bitmap(import_bmp);
 
    destroy_bitmap(import_bmp);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/