Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Bitmap.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "macros.h"
00028 #include "debug.h"
00029 #include "Bases.h"
00030 #include "Locale.h"
00031 #include "Common.h"
00032 #include "Memory.h"
00033 #include "Gui.h"
00034 #include "Window.h"
00035 #include "Net.h"
00036 #include "Bitmap.h"
00037 
00038 
00039 static u32 (*call_vector[BITMAP_FUNC_DONE-BITMAP_FUNCBASE+1])(tag *);
00040 
00041 
00042 struct Bitmap_s
00043 {
00044   int width;
00045   int height;
00046   int size;
00047   int bytewidth;
00048   u8 *data;
00049   int locked;
00050 };
00051 
00052 
00060 u32 bitmap_Create(u32 firsttag, ...)
00061 {
00062   return(bitmap_CreateTL((tag *)&firsttag));
00063 }
00064 u32 bitmap_CreateTL(tag *taglist)
00065 {
00066   int width,height,inittype;
00067   u32 value;
00068   u8 *vector;
00069 
00070   Bitmap_t *ret=NULL;
00071   u8 val;
00072 
00073   debug_Begin();
00074 
00075   width=(int)tag_GetTagData(taglist,TAG_BMP_WIDTH,0L);
00076   height=(int)tag_GetTagData(taglist,TAG_BMP_HEIGHT,0L);
00077   inittype=(u8)tag_GetTagData(taglist,TAG_BMP_INITTYPE,0L);
00078   vector=(u8 *)tag_GetTagData(taglist,TAG_BMP_VECTOR,0L);
00079   value=(u32)tag_GetTagData(taglist,TAG_BMP_VALUE,0L);
00080 
00081   debug_Message("w:%d h:%d",width,height);
00082 
00083   val=(u8)value;
00084   if(width>0&&height>0)
00085   {
00086     if(NULL!=(ret=mem_malloc(sizeof(Bitmap_t))))
00087     {
00088       if(0!=(width%8)) ret->bytewidth=(width/8)+1;
00089       else ret->bytewidth=width/8;
00090       ret->size=ret->bytewidth*height;
00091       ret->locked=0;
00092       if(NULL!=(ret->data=mem_malloc(ret->size)))
00093       {
00094         ret->width=width;
00095         ret->height=height;
00096         memset(ret->data,0xff,ret->size);
00097         switch(inittype)
00098         {
00099           case BMP_ITY_BITMAP:
00100           {
00101             memcpy(ret->data,vector,ret->size);
00102             break;
00103           }
00104           case BMP_ITY_BYTES:
00105           {
00106             int x,y,b;
00107             u8 *p1,*p2,*src;
00108             for(src=vector,p1=ret->data,y=0;y<ret->height;y++)
00109             {
00110               p2=p1;
00111               for(x=0,b=7;x<ret->width;x++,b--)
00112               {
00113                 if(b<0) { b=7; p2++; *p2=0; }
00114                 *p2|=((!!*src++)<<b);
00115               }
00116               p1+=ret->bytewidth;
00117             }
00118             break;
00119           }
00120           case BMP_ITY_FILL:
00121           {
00122             bitmap_Fill(TAG_BMP_OBJECT,ret,TAG_BMP_VALUE,val,TAG_DONE);
00123             break;
00124           }
00125           case BMP_ITY_ALPHA:
00126           {
00127             debug_Error("BMP_ITY_ALPHA unimplemented yet");
00128             break;
00129           }
00130           case BMP_ITY_RGB:
00131           {
00132             int x,y,b;
00133             u8 *p1,*p2,*src;
00134             u32 curr;
00135 
00136             value&=0x00ffffff;
00137             for(src=vector,p1=ret->data,y=0;y<ret->height;y++)
00138             {
00139               p2=p1;
00140               for(x=0,b=7;x<ret->width;x++,b--)
00141               {
00142                 if(b<0) { b=7; p2++; }
00143                 curr=0L;
00144                 src++;
00145                 curr|=*src++;
00146                 curr<<=8;
00147                 curr|=*src++;
00148                 curr<<=8;
00149                 curr|=*src++;
00150                 if(curr==value) *p2&=~(1<<b);
00151               }
00152               p1+=ret->bytewidth;
00153             }
00154             break;
00155           }
00156           default:
00157           {
00158             debug_Error("Bad inittype value");
00159             mem_free(ret);
00160             ret=NULL;
00161             break;
00162           }
00163         }
00164       }
00165       else
00166       {
00167         mem_free(ret);
00168         ret=NULL;
00169       }
00170     }
00171   }
00172 
00173 //  bitmap_Print(ret,__FUNCTION__);
00174 
00175   debug_End();
00176 
00177   return((u32)ret);
00178 }
00179 
00180 
00181 void bitmap_Print(Bitmap_t *bmp, char *msg)
00182 {
00183   if(bmp!=NULL)
00184   {
00185     int x,y,b;
00186     u8 *p1,*p2;
00187 
00188     printf("BITMAP: %dx%d '%s'\n  ",bmp->width,bmp->height,msg);
00189     for(p1=bmp->data,y=0;y<bmp->height;y++)
00190     {
00191       p2=p1;
00192       for(x=0,b=7;x<bmp->width;x++,b--)
00193       {
00194          if(b<0) { b=7; p2++; }
00195          if(((((*p2)&(1<<b)))&0xff)==0) printf(".");
00196          else printf("x");
00197       }
00198       p1+=bmp->bytewidth;
00199       printf("\n  ");
00200     }
00201     printf("-----\n");
00202   }
00203 }
00204 
00205 
00209 u32 bitmap_Delete(u32 firsttag, ...)
00210 {
00211   return(bitmap_DeleteTL((tag *)&firsttag));
00212 }
00213 u32 bitmap_DeleteTL(tag *taglist)
00214 {
00215   Bitmap_t *bmp;
00216 
00217   u32 ret=~0L;
00218 
00219   debug_Begin();
00220 
00221   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00222 
00223   if(bmp!=NULL)
00224   {
00225     if(bmp->locked==0)
00226     {
00227       if(bmp->data!=NULL) mem_free(bmp->data);
00228       mem_free(bmp);
00229       ret=0L;
00230     }
00231     else debug_Error("Unable to delete, Bitmap locked!");
00232   }
00233 
00234   debug_End();
00235 
00236   return(ret);
00237 }
00238 
00239 
00246 u32 bitmap_Copy(u32 firsttag, ...)
00247 {
00248   return(bitmap_CopyTL((tag *)&firsttag));
00249 }
00250 u32 bitmap_CopyTL(tag *taglist)
00251 {
00252   Bitmap_t *bmp;
00253   int x;
00254   int y;
00255   Bitmap_t *source;
00256 
00257   u32 ret=~0L;
00258 
00259   debug_Begin();
00260 
00261   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00262   x=(int)tag_GetTagData(taglist,TAG_BMP_X,0L);
00263   y=(int)tag_GetTagData(taglist,TAG_BMP_Y,0L);
00264   source=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_SOURCE,0L);
00265 
00266   if(bmp!=NULL&&source!=NULL&&x>=0&&y>=0&&x<bmp->width&&y<bmp->height)
00267   {
00268     int ymax,xmax,x1,y1,addr,bit,saddr,sbit;
00269 
00270     // source bitmap-et bemasolni a masikba, a megfelelo helyre
00271     ymax=min(source->height,(bmp->height-y));
00272     xmax=min(source->width,(bmp->width-x));
00273 
00274     debug_Message("src.width=%d src.height=%d x=%d y=%d dest.width=%d dest.height=%d xmax=%d ymax=%d",source->width,source->height,x,y,bmp->width,bmp->height,xmax,ymax);
00275 
00276     for(y1=0;y1<ymax;y1++)
00277     {
00278       for(x1=0;x1<xmax;x1++)
00279       {
00280         addr=(y1+y)*bmp->bytewidth+((x1+x)/8);
00281         bit=1<<(7-((x1+x)%8));
00282         bmp->data[addr]&=~bit;
00283         saddr=y1*source->bytewidth+(x1/8);
00284         sbit=1<<(7-(x1%8));
00285         if(((source->data[saddr]&sbit)&0xff)!=0) bmp->data[addr]|=bit;
00286       }
00287     }
00288     ret=0L;
00289   }
00290 
00291   debug_End();
00292 
00293   return(ret);
00294 }
00295 
00296 
00304 u32 bitmap_ObtainVector(u32 firsttag, ...)
00305 {
00306   return(bitmap_ObtainVectorTL((tag *)&firsttag));
00307 }
00308 u32 bitmap_ObtainVectorTL(tag *taglist)
00309 {
00310   Bitmap_t *bmp;
00311   u8 **vector;
00312   int *width,*height,*bytewidth;
00313 
00314   u32 ret=~0L;
00315 
00316   debug_Begin();
00317 
00318   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00319   vector=(u8 **)tag_GetTagData(taglist,TAG_BMP_VECTOR,0L);
00320   width=(int *)tag_GetTagData(taglist,TAG_BMP_WIDTH,0L);
00321   height=(int *)tag_GetTagData(taglist,TAG_BMP_HEIGHT,0L);
00322   bytewidth=(int *)tag_GetTagData(taglist,TAG_BMP_BYTEWIDTH,0L);
00323 
00324   if(bmp!=NULL&&vector!=NULL)
00325   {
00326     bmp->locked=1;
00327     if(vector!=NULL) *vector=bmp->data;
00328     if(width!=NULL) *width=bmp->width;
00329     if(height!=NULL) *height=bmp->height;
00330     if(bytewidth!=NULL) *bytewidth=bmp->bytewidth;
00331     ret=0L;
00332   }
00333 
00334   debug_End();
00335 
00336   return(ret);
00337 }
00338 
00342 u32 bitmap_ReleaseVector(u32 firsttag, ...)
00343 {
00344   return(bitmap_ReleaseVectorTL((tag *)&firsttag));
00345 }
00346 u32 bitmap_ReleaseVectorTL(tag *taglist)
00347 {
00348   Bitmap_t *bmp;
00349 
00350   u32 ret=~0L;
00351 
00352   debug_Begin();
00353 
00354   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00355 
00356   if(bmp!=NULL)
00357   {
00358     bmp->locked=0;
00359     ret=0L;
00360   }
00361 
00362   debug_End();
00363 
00364   return(ret);
00365 }
00366 
00367 
00372 u32 bitmap_Fill(u32 firsttag, ...)
00373 {
00374   return(bitmap_FillTL((tag *)&firsttag));
00375 }
00376 u32 bitmap_FillTL(tag *taglist)
00377 {
00378   Bitmap_t *bmp;
00379   u8 value;
00380 
00381   u32 ret=~0L;
00382 
00383   debug_Begin();
00384 
00385   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00386   value=(u8)tag_GetTagData(taglist,TAG_BMP_VALUE,0L);
00387 
00388   if(bmp!=NULL)
00389   {
00390     int i;
00391     u8 mask=0,*p;
00392 
00393     if(value!=0) mask=0xff;
00394     for(p=bmp->data,i=0;i<bmp->size;i++) *p++=mask;
00395     ret=0L;
00396   }
00397 
00398   debug_End();
00399 
00400   return(ret);
00401 }
00402 
00403 
00408 u32 bitmap_IsSolid(u32 firsttag, ...)
00409 {
00410   return(bitmap_IsSolidTL((tag *)&firsttag));
00411 }
00412 u32 bitmap_IsSolidTL(tag *taglist)
00413 {
00414   Bitmap_t *bmp;
00415   u8 value;
00416 
00417   u32 ret=~0L;
00418 
00419   debug_Begin();
00420 
00421   bmp=(Bitmap_t *)tag_GetTagData(taglist,TAG_BMP_OBJECT,0L);
00422   value=(u8)tag_GetTagData(taglist,TAG_BMP_VALUE,0L);
00423 
00424   if(bmp!=NULL)
00425   {
00426     ret=0L;
00427   }
00428 
00429   debug_End();
00430 
00431   return(ret);
00432 }
00433 
00434 
00436 
00437 
00438 u32 bitmap_Call(int function, u32 firsttag, ...)
00439 {
00440   return(bitmap_CallTL(function,(tag *)&firsttag));
00441 }
00442 u32 bitmap_CallTL(int function, tag *taglist)
00443 {
00444   u32 ret=~0L;
00445 
00446   debug_Begin();
00447 
00448   debug_Message("function ID : %d",function-BITMAP_FUNCBASE);
00449 
00450   if(function>=BITMAP_FUNCBASE&&function<BITMAP_FUNC_DONE)
00451   {
00452     ret=call_vector[function-BITMAP_FUNCBASE](taglist);
00453   }
00454   else debug_Warning("%s(): Function code out of range! code=0x%04x",__FUNCTION__,function);
00455 
00456   debug_End();
00457 
00458   return(ret);
00459 }
00460 
00461 
00462 int bitmap_Init(void)
00463 {
00464   int ret=-1;
00465 
00466   debug_Begin();
00467 
00468 //Initialize call vector BEGIN - put this into the init function of this module
00469   call_vector[BITMAP_CREATE-BITMAP_FUNCBASE]=bitmap_CreateTL;
00470   call_vector[BITMAP_DELETE-BITMAP_FUNCBASE]=bitmap_DeleteTL;
00471   call_vector[BITMAP_COPY-BITMAP_FUNCBASE]=bitmap_CopyTL;
00472   call_vector[BITMAP_OBTAINVECTOR-BITMAP_FUNCBASE]=bitmap_ObtainVectorTL;
00473   call_vector[BITMAP_RELEASEVECTOR-BITMAP_FUNCBASE]=bitmap_ReleaseVectorTL;
00474   call_vector[BITMAP_FILL-BITMAP_FUNCBASE]=bitmap_FillTL;
00475   call_vector[BITMAP_ISSOLID-BITMAP_FUNCBASE]=bitmap_IsSolidTL;
00476 //Initialize call vector END
00477   bases_modules.bitmap_Call=bitmap_Call;
00478   bases_modules.bitmap_CallTL=bitmap_CallTL;
00479 
00480   ret=0;
00481 
00482   debug_End();
00483 
00484   return(ret);
00485 }
00486 
00487 
00488 void bitmap_CleanUp(void)
00489 {
00490   debug_Begin();
00491   debug_End();
00492 
00493   return;
00494 }

Generated on Tue Jan 7 12:11:18 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002