00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ScribbleUtils.h"
00036 #include "ScribbleLog.h"
00037
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include <time.h>
00043 #include <unistd.h>
00044
00045
00047
00048
00049
00050 static int COLORVALUE[4]={0xFFFF,0xAAAA,0x5555,0x0000};
00051
00052 int Util_GdkColorFromHWCOLOR(GdkColor *pGdkColor,HW_COLOR iHwColor)
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 }
00062
00063
00064
00065 HW_COLOR Util_HWCOLORFromGdkColor(GdkColor *pGdkColor)
00066 {
00067 HW_COLOR iHwColor=COLOR_BLACK;
00068
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 }
00086
00087
00088 #define GET_256COLOR(gdkcolor) ((unsigned int)gdkcolor/256)
00089
00090
00091 #define GET_GDKCOLOR(color256) ((color256)*256)
00092
00093
00094 int Util_GdkColorFromHtmlColor(GdkColor *pGdkColor,const char* sHtmlColor)
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
00108
00109
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 }
00116
00117
00118 int Util_HtmlColorFromGdkColor(char* sHtmlColor,int iSize,const GdkColor *pGdkColor)
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 }
00134 gboolean FileExist(const char* filename)
00135 {
00136 struct stat buf;
00137
00138 if(NULL==filename) return FALSE;
00139
00140 return ( 0==stat(filename, &buf) );
00141 }
00142
00143
00144 gboolean FileExist2(const char* filename,const char* dir)
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 }
00155
00156
00157 int FileDel(const char* filename)
00158 {
00159 return unlink(filename);
00160 }
00161
00162
00163 int FileMove(const char* sDestFile,const char *sSrcFile)
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;
00172 }
00173 }
00174 if( 0!=rename(sSrcFile,sDestFile) )
00175 {
00176 SB_ERRORPRINTF("\nrename [%s]\nto[%s] fail\n",sSrcFile,sDestFile);
00177 return -1;
00178 }
00179
00180 return 0;
00181 }
00182
00183 int CreateDir(const char* sPath)
00184 {
00185 return mkdir(sPath, 700);
00186 }
00187
00188
00189 void GetFileNameAndDir(char* sFileName,int iNameSize,
00190 char* sDir,int iDirSize,
00191 const char* sFullName)
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
00202 memset(sDir,0,iDirSize);
00203 if(iDestLen==iLen)
00204 {
00205 strcpy(sDir,"./");
00206 }
00207 else
00208 {
00209 strncpy(sDir,sFullName,iLen-iDestLen);
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 }
00221
00222
00223 char* GetFileNameFromFullName(char *sFileName,int iSize,
00224 const char* sFullName)
00225 {
00226 char sDir[512+1];
00227 GetFileNameAndDir(sFileName,iSize,sDir,sizeof(sDir),sFullName);
00228 return sFileName;
00229 }
00230
00231
00232
00233 char* GetDirFromFullName(char *sDir, int iSize,const char* sFullName)
00234 {
00235 char sFileName[512+1];
00236 GetFileNameAndDir(sFileName,sizeof(sFileName),sDir,iSize,sFullName);
00237 return sDir;
00238 }
00239
00241
00242
00243 int GetStrFromTime(char* sTime,int iSize,time_t iTime)
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
00255
00256
00257
00258
00259
00260
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
00268 return 0;
00269 }
00270
00271
00273
00274 gboolean isStrEmpty(const char* s)
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;
00283 }
00284 return TRUE;
00285 }
00286
00287
00289 void dumpPixbufInfo(GdkPixbuf* pixbuf)
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 }
00305
00306 int util_loadImage(GdkPixbuf** buf, const char* filename)
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
00322 SB_FMPRINTF("__Loaded...\n");
00323 return 0;
00324 }
00325
00326
00327 int util_loadImage_bysize(GdkPixbuf** buf,const char* filename,int width,int height)
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
00355 SB_FMPRINTF("__Loaded...\n");
00356 }
00357 else
00358 {
00359 SB_ERRORPRINTF("unknow image format of %s", filename);
00360 }
00361 return 0;
00362 }
00363
00364 char* getFileExtension(IMGTYPE filetype)
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 }
00378
00379 int util_savePixbuf(GdkPixbuf * pixbuf,
00380 char* filename,
00381 IMGTYPE filetype)
00382 {
00383 GError *error = NULL;
00384
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 }
00394
00395
00396 int util_saveImage(GdkPixmap * pixmap,
00397 char* filename,
00398 IMGTYPE filetype)
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();
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 }