scribble/src/ScribbleUtils.c File Reference

#include "ScribbleUtils.h"
#include "ScribbleLog.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

Go to the source code of this file.

Defines

#define GET_256COLOR(gdkcolor)   ((unsigned int)gdkcolor/256)
#define GET_GDKCOLOR(color256)   ((color256)*256)

Functions

int Util_GdkColorFromHWCOLOR (GdkColor *pGdkColor, HW_COLOR iHwColor)
HW_COLOR Util_HWCOLORFromGdkColor (GdkColor *pGdkColor)
int Util_GdkColorFromHtmlColor (GdkColor *pGdkColor, const char *sHtmlColor)
int Util_HtmlColorFromGdkColor (char *sHtmlColor, int iSize, const GdkColor *pGdkColor)
gboolean FileExist (const char *filename)
gboolean FileExist2 (const char *filename, const char *dir)
int FileDel (const char *filename)
int FileMove (const char *sDestFile, const char *sSrcFile)
int CreateDir (const char *sPath)
void GetFileNameAndDir (char *sFileName, int iNameSize, char *sDir, int iDirSize, const char *sFullName)
char * GetFileNameFromFullName (char *sFileName, int iSize, const char *sFullName)
char * GetDirFromFullName (char *sDir, int iSize, const char *sFullName)
int GetStrFromTime (char *sTime, int iSize, time_t iTime)
gboolean isStrEmpty (const char *s)
void dumpPixbufInfo (GdkPixbuf *pixbuf)
int util_loadImage (GdkPixbuf **buf, const char *filename)
int util_loadImage_bysize (GdkPixbuf **buf, const char *filename, int width, int height)
char * getFileExtension (IMGTYPE filetype)
int util_savePixbuf (GdkPixbuf *pixbuf, char *filename, IMGTYPE filetype)
int util_saveImage (GdkPixmap *pixmap, char *filename, IMGTYPE filetype)

Variables

static int COLORVALUE [4] = {0xFFFF,0xAAAA,0x5555,0x0000}


Define Documentation

#define GET_256COLOR ( gdkcolor   )     ((unsigned int)gdkcolor/256)

Definition at line 88 of file ScribbleUtils.c.

#define GET_GDKCOLOR ( color256   )     ((color256)*256)

Definition at line 91 of file ScribbleUtils.c.


Function Documentation

int CreateDir ( const char *  sPath  ) 

Definition at line 183 of file ScribbleUtils.c.

00184 {
00185     return  mkdir(sPath, 700);//mode_t,default to root only
00186 }

void dumpPixbufInfo ( GdkPixbuf *  pixbuf  ) 

Definition at line 289 of file ScribbleUtils.c.

00290 {
00291     printf("==========pixbuf info===================\n");
00292     printf("colorsapce=%d,n_channels=%d,has_alpha=%d,per_sample=%d,\n"
00293             "width=%d,height=%d,rowstride=%d,colormap_size=%d\n",
00294     gdk_pixbuf_get_colorspace (pixbuf),
00295     gdk_pixbuf_get_n_channels (pixbuf),
00296     gdk_pixbuf_get_has_alpha(pixbuf),
00297     gdk_pixbuf_get_bits_per_sample(pixbuf),
00298     gdk_pixbuf_get_width(pixbuf),
00299     gdk_pixbuf_get_height(pixbuf),
00300     gdk_pixbuf_get_rowstride(pixbuf),
00301     gdk_colormap_get_system_size () );
00302     printf("========================================\n");
00303             
00304 }

int FileDel ( const char *  filename  ) 

Definition at line 157 of file ScribbleUtils.c.

00158 {
00159     return unlink(filename);
00160 }

gboolean FileExist ( const char *  filename  ) 

Definition at line 134 of file ScribbleUtils.c.

00135 {
00136     struct stat buf;
00137 
00138     if(NULL==filename) return FALSE;
00139 
00140     return ( 0==stat(filename, &buf) );
00141 }

gboolean FileExist2 ( const char *  filename,
const char *  dir 
)

Definition at line 144 of file ScribbleUtils.c.

00145 {
00146     if(NULL==filename && NULL==dir) return FALSE;
00147     
00148     char sFullPathName[512];
00149     memset(sFullPathName,0,sizeof(sFullPathName));    
00150     snprintf(sFullPathName,sizeof(sFullPathName),
00151              "%s%s",dir,filename);
00152     
00153     return FileExist(sFullPathName);
00154 }

Here is the call graph for this function:

int FileMove ( const char *  sDestFile,
const char *  sSrcFile 
)

Definition at line 163 of file ScribbleUtils.c.

00164 {
00165     if( NULL==sDestFile || NULL==sSrcFile ) return -1;
00166     if( FileExist(sDestFile) )
00167     {
00168         if( 0!=FileDel(sDestFile) )
00169         {
00170             SB_ERRORPRINTF("del [%s] fail\n",sDestFile);
00171             return -1;//delete bakfile
00172         }
00173     }
00174     if( 0!=rename(sSrcFile,sDestFile) )
00175     {
00176         SB_ERRORPRINTF("\nrename [%s]\nto[%s] fail\n",sSrcFile,sDestFile);
00177         return -1;//delete bakfile
00178     }
00179     
00180     return 0;
00181 }

Here is the call graph for this function:

char* GetDirFromFullName ( char *  sDir,
int  iSize,
const char *  sFullName 
)

Definition at line 233 of file ScribbleUtils.c.

00234 {
00235     char sFileName[512+1];
00236     GetFileNameAndDir(sFileName,sizeof(sFileName),sDir,iSize,sFullName);
00237     return sDir;
00238 }

Here is the call graph for this function:

char* getFileExtension ( IMGTYPE  filetype  ) 

Definition at line 364 of file ScribbleUtils.c.

00365 {
00366     switch(filetype) 
00367     {
00368         case TYPE_BMP:
00369             return "bmp";
00370         case TYPE_PNG:
00371             return "png";
00372         case TYPE_JPG:
00373             return "jpg";
00374         default:
00375             return "";
00376     }
00377 }

void GetFileNameAndDir ( char *  sFileName,
int  iNameSize,
char *  sDir,
int  iDirSize,
const char *  sFullName 
)

Definition at line 189 of file ScribbleUtils.c.

00192 {
00193     int iLen=strlen(sFullName);
00194     int iDestLen=0;
00195     SB_FMPRINTF("__fullname=%s\n",sFullName);
00196     for(iDestLen=0;iDestLen<iLen;iDestLen++)
00197     {
00198         if(sFullName[iLen-1-iDestLen] == '/')
00199             break;
00200     }
00201     //fetch dir
00202     memset(sDir,0,iDirSize);
00203     if(iDestLen==iLen) //no path char 
00204     {
00205         strcpy(sDir,"./");//current directory
00206     }
00207     else
00208     {
00209         strncpy(sDir,sFullName,iLen-iDestLen);//keep last"/"
00210     }
00211     
00212     if(iDestLen>0)
00213     {
00214         if(iDestLen>iDirSize-1) iDestLen=iDirSize-1;
00215         strncpy(sFileName,sFullName+iLen-iDestLen,iDestLen);
00216     }
00217     
00218     sFileName[iDestLen]=0;
00219     SB_FMPRINTF("\n__dir=%s,filename=%s\n",sDir,sFileName);
00220 }

char* GetFileNameFromFullName ( char *  sFileName,
int  iSize,
const char *  sFullName 
)

Definition at line 223 of file ScribbleUtils.c.

00225 {
00226     char sDir[512+1];
00227     GetFileNameAndDir(sFileName,iSize,sDir,sizeof(sDir),sFullName);
00228     return sFileName;
00229 }

Here is the call graph for this function:

int GetStrFromTime ( char *  sTime,
int  iSize,
time_t  iTime 
)

Definition at line 243 of file ScribbleUtils.c.

00244 {
00245     struct tm *tmp;
00246     tmp = (struct tm *)localtime(&iTime);
00247     if ( NULL==tmp )
00248     {
00249         SB_ERRORPRINTF("error get localtime");
00250         return -1;
00251     }
00252     memset(sTime,0,sizeof(iSize));
00253 /*
00254     printf("time is:%02d,%02d,%02d,%02d,%2d,%02d\n",
00255     tmp->tm_sec,   // Seconds.     [0-60] (1 leap second)
00256     tmp->tm_min,   // Minutes.     [0-59]
00257     tmp->tm_hour,   // Hours.       [0-23] 
00258     tmp->tm_mday,   // Day.         [1-31] 
00259     tmp->tm_mon,    // Month.       [0-11] 
00260     tmp->tm_year-100); //year-1900
00261 */
00262     if (strftime(sTime, iSize, "%Y-%m-%d__%H-%M-%S", tmp) == 0) 
00263     {
00264         SB_ERRORPRINTF("strftime returned 0");
00265         return -1;
00266     }
00267     //SB_LOGPRINTF("time str:%s\n",sTime);
00268     return 0;
00269 }

gboolean isStrEmpty ( const char *  s  ) 

Definition at line 274 of file ScribbleUtils.c.

00275 {
00276     if ( NULL==s ) return TRUE;
00277     
00278     int i;
00279     
00280     for( i=0; i<strlen(s); i++ )
00281     {
00282         if( ' '!=s[i] ) return FALSE;//find a meaningful char.
00283     }
00284     return TRUE;
00285 }

int Util_GdkColorFromHtmlColor ( GdkColor *  pGdkColor,
const char *  sHtmlColor 
)

Definition at line 94 of file ScribbleUtils.c.

00095 {       
00096     if( NULL==pGdkColor || NULL==sHtmlColor)
00097     {
00098         return -1;
00099     }
00100     if( '#'!=sHtmlColor[0] && strlen(sHtmlColor)!=HTMLCOLOR_LEN )
00101     {
00102         return -1;
00103     }
00104     
00105     unsigned int rgbValue;
00106     sscanf(sHtmlColor+1,"%x",&rgbValue);
00107     //printf("color=%s,rgbvalue=%08x\n",sHtmlColor,rgbValue);
00108     
00109     //first compute the max-255 color,then transform to max-0xffff.      
00110     pGdkColor->red=   GET_GDKCOLOR( (rgbValue>>16)& 0xff );
00111     pGdkColor->green= GET_GDKCOLOR( (rgbValue&0xff00)>>8 );
00112     pGdkColor->blue=  GET_GDKCOLOR( rgbValue & 0x00ff );
00113 
00114     return 0;
00115 }

int Util_GdkColorFromHWCOLOR ( GdkColor *  pGdkColor,
HW_COLOR  iHwColor 
)

Definition at line 52 of file ScribbleUtils.c.

00053 {
00054     if(iHwColor<0 || iHwColor>3) iHwColor=COLOR_BLACK;
00055     
00056     if( NULL==pGdkColor) return -1;
00057     
00058     pGdkColor->red=pGdkColor->green=pGdkColor->blue
00059                                    =COLORVALUE[iHwColor];
00060     return 0;
00061 }

int Util_HtmlColorFromGdkColor ( char *  sHtmlColor,
int  iSize,
const GdkColor *  pGdkColor 
)

Definition at line 118 of file ScribbleUtils.c.

00119 {
00120     if( NULL==pGdkColor || NULL==sHtmlColor || iSize<(HTMLCOLOR_LEN+1) )
00121     {
00122         return -1;
00123     }
00124     
00125     unsigned int rgbValue= (GET_256COLOR(pGdkColor->red) & 0xff)<<16;
00126     rgbValue |= (GET_256COLOR(pGdkColor->green) &0xff)<<8;
00127     rgbValue |= GET_256COLOR(pGdkColor->blue) & 0xff;
00128     
00129     snprintf(sHtmlColor,iSize,"#%06x",rgbValue);
00130     
00131     return 0;   
00132 }

HW_COLOR Util_HWCOLORFromGdkColor ( GdkColor *  pGdkColor  ) 

Definition at line 65 of file ScribbleUtils.c.

00066 {
00067     HW_COLOR iHwColor=COLOR_BLACK;
00068     //compute the average value
00069     int iCurrColor=
00070             (pGdkColor->red+pGdkColor->green+pGdkColor->blue)/3;
00071     int i=0;
00072     int iDiff=INT_MAX;
00073     int iCurrDiff;
00074     for(i=0; i<ARRAYSIZE(COLORVALUE); i++)
00075     {
00076         iCurrDiff=abs(COLORVALUE[i]-iCurrColor);
00077         if(iCurrDiff<iDiff)
00078         {
00079             iDiff= iCurrDiff;
00080             iHwColor=i;
00081         }
00082     }
00083 
00084     return iHwColor;    
00085 }

int util_loadImage ( GdkPixbuf **  buf,
const char *  filename 
)

Definition at line 306 of file ScribbleUtils.c.

00307 {
00308     GError *error = NULL;
00309 
00310     SB_FMPRINTF("__Loading(%s)...\n",filename);
00311     (*buf) = gdk_pixbuf_new_from_file(filename, &error);
00312     if ((*buf)==NULL)
00313     {        
00314         SB_ERRORPRINTF("Error loading bitmap(%s)\n", filename);
00315         if( NULL!= error )
00316         {
00317             SB_ERRORPRINTF("Error msg\n(%s)\n",error->message);
00318         }
00319         return -1;
00320     }
00321     //dumpPixbufInfo(*buf);
00322     SB_FMPRINTF("__Loaded...\n");
00323     return 0;
00324 }

int util_loadImage_bysize ( GdkPixbuf **  buf,
const char *  filename,
int  width,
int  height 
)

Definition at line 327 of file ScribbleUtils.c.

00328 {
00329     int w, h;
00330     GdkPixbufFormat* format;
00331     GError *error = NULL;
00332 
00333     format = gdk_pixbuf_get_file_info(filename, &w, &h);
00334     if (format)
00335     {
00336         SB_FMPRINTF("__Loading(%s)...\n",filename);
00337         if ((w <= width) && (h <= height))
00338         {
00339             (*buf) = gdk_pixbuf_new_from_file_at_size(filename, w, h, &error);
00340         }
00341         else
00342         {
00343             (*buf) = gdk_pixbuf_new_from_file_at_size(filename, width, height, &error);
00344         }
00345         if ((*buf)==NULL)
00346         {        
00347             SB_ERRORPRINTF("Error loading bitmap\n(%s)\n", filename);
00348             if( NULL!= error )
00349             {
00350                 SB_ERRORPRINTF("Error msg\n(%s)\n",error->message);
00351             }
00352             return -1;
00353         }
00354         //dumpPixbufInfo(*buf);
00355         SB_FMPRINTF("__Loaded...\n");
00356     }
00357     else
00358     {
00359         SB_ERRORPRINTF("unknow image format of %s", filename);
00360     }
00361     return 0;
00362 }

int util_saveImage ( GdkPixmap *  pixmap,
char *  filename,
IMGTYPE  filetype 
)

Definition at line 396 of file ScribbleUtils.c.

00399 {
00400     GdkPixbuf *pixbuf;
00401 
00402     SB_TIMEDISPLAY("__Saving-->\n%s\n",filename);
00403     gdk_threads_enter();    
00404     pixbuf = gdk_pixbuf_get_from_drawable(NULL,(GdkDrawable *)pixmap,
00405                                           NULL,0,0,0,0,-1,-1);
00406     gdk_flush();//make sure all x command has sent to X server.
00407     gdk_threads_leave();
00408     int iRet=util_savePixbuf(pixbuf,filename,filetype);
00409     
00410     if(pixbuf)
00411     {
00412         g_object_unref(pixbuf);
00413     }
00414     return iRet;
00415 }

Here is the call graph for this function:

int util_savePixbuf ( GdkPixbuf *  pixbuf,
char *  filename,
IMGTYPE  filetype 
)

Definition at line 379 of file ScribbleUtils.c.

00382 {
00383     GError *error = NULL;
00384     //dumpPixbufInfo(pixbuf);
00385     SB_TIMEDISPLAY("--start save \n%s\n",filename);
00386     if (!gdk_pixbuf_save(pixbuf, filename,getFileExtension(filetype), &error, NULL))
00387     {
00388         SB_ERRORPRINTF("Error saving image.msg(%s)\n",error->message);
00389         return -1;
00390     }
00391     SB_TIMEDISPLAY("__Saved Done\n");
00392     return 0;
00393 }

Here is the call graph for this function:


Variable Documentation

int COLORVALUE[4] = {0xFFFF,0xAAAA,0x5555,0x0000} [static]

Copyright (C) 2005-2008 iRex Technologies B.V. All rights reserved.

Definition at line 50 of file ScribbleUtils.c.


Generated on Sun Dec 14 17:14:42 2008 by  doxygen 1.5.6