util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Various useful definitions.
  2. *
  3. * J.Cupitt, 8/4/93
  4. * 15/7/96 JC
  5. * - C++ stuff added
  6. */
  7. /*
  8. This file is part of VIPS.
  9. VIPS is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU Lesser General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. 02110-1301 USA
  21. */
  22. /*
  23. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  24. */
  25. #ifndef VIPS_UTIL_H
  26. #define VIPS_UTIL_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif /*__cplusplus*/
  30. #include <stdio.h>
  31. #include <math.h>
  32. /* Some platforms don't have M_PI :-(
  33. */
  34. #define VIPS_PI (3.14159265358979323846)
  35. /* Convert degrees->rads and vice-versa.
  36. */
  37. #define VIPS_RAD( R ) (((R) / 360.0) * 2.0 * VIPS_PI)
  38. #define VIPS_DEG( A ) (((A) / (2.0 * VIPS_PI)) * 360.0)
  39. #define VIPS_MAX( A, B ) ((A) > (B) ? (A) : (B))
  40. #define VIPS_MIN( A, B ) ((A) < (B) ? (A) : (B))
  41. #define VIPS_CLIP( A, V, B ) VIPS_MAX( (A), VIPS_MIN( (B), (V) ) )
  42. #define VIPS_FCLIP( A, V, B ) VIPS_FMAX( (A), VIPS_FMIN( (B), (V) ) )
  43. #define VIPS_NUMBER( R ) ((int) (sizeof(R) / sizeof(R[0])))
  44. #define VIPS_ABS( X ) (((X) >= 0) ? (X) : -(X))
  45. /* The built-in isnan and isinf functions provided by gcc 4+ and clang are
  46. * up to 7x faster than their libc equivalent included from <math.h>.
  47. */
  48. #if defined(__clang__) || (__GNUC__ >= 4)
  49. #define VIPS_ISNAN( V ) __builtin_isnan( V )
  50. #define VIPS_ISINF( V ) __builtin_isinf( V )
  51. #define VIPS_FLOOR( V ) __builtin_floor( V )
  52. #define VIPS_CEIL( V ) __builtin_ceil( V )
  53. #define VIPS_RINT( V ) __builtin_rint( V )
  54. #define VIPS_ROUND( V ) __builtin_round( V )
  55. #define VIPS_FABS( V ) __builtin_fabs( V )
  56. #define VIPS_FMAX( A, B ) __builtin_fmax( A, B )
  57. #define VIPS_FMIN( A, B ) __builtin_fmin( A, B )
  58. #else
  59. #define VIPS_ISNAN( V ) isnan( V )
  60. #define VIPS_ISINF( V ) isinf( V )
  61. #define VIPS_FLOOR( V ) floor( V )
  62. #define VIPS_CEIL( V ) ceil( V )
  63. #define VIPS_RINT( V ) rint( V )
  64. #define VIPS_ROUND( V ) round( V )
  65. #define VIPS_FABS( V ) VIPS_ABS( V )
  66. #define VIPS_FMAX( A, B ) VIPS_MAX( A, B )
  67. #define VIPS_FMIN( A, B ) VIPS_MIN( A, B )
  68. #endif
  69. /* Testing status before the function call saves a lot of time.
  70. */
  71. #define VIPS_ONCE( ONCE, FUNC, CLIENT ) \
  72. G_STMT_START { \
  73. if( G_UNLIKELY( (ONCE)->status != G_ONCE_STATUS_READY ) ) \
  74. (void) g_once( ONCE, FUNC, CLIENT ); \
  75. } G_STMT_END
  76. /* VIPS_RINT() does "bankers rounding", it rounds to the nerarest even integer.
  77. * For things like image geometry, we want strict nearest int.
  78. *
  79. * If you know it's unsigned, _UINT is a little faster.
  80. */
  81. #define VIPS_ROUND_INT( R ) ((int) ((R) > 0 ? ((R) + 0.5) : ((R) - 0.5)))
  82. #define VIPS_ROUND_UINT( R ) ((int) ((R) + 0.5))
  83. /* Round N down and up to the nearest multiple of P.
  84. */
  85. #define VIPS_ROUND_DOWN( N, P ) ((N) - ((N) % (P)))
  86. #define VIPS_ROUND_UP( N, P ) (VIPS_ROUND_DOWN( (N) + (P) - 1, (P) ))
  87. #define VIPS_SWAP( TYPE, A, B ) \
  88. G_STMT_START { \
  89. TYPE t = (A); \
  90. (A) = (B); \
  91. (B) = t; \
  92. } G_STMT_END
  93. /* Duff's device. Do OPERation N times in a 16-way unrolled loop.
  94. */
  95. #define VIPS_UNROLL( N, OPER ) \
  96. G_STMT_START { \
  97. if( (N) ) { \
  98. int duff_count = ((N) + 15) / 16; \
  99. \
  100. switch( (N) % 16 ) { \
  101. case 0: do { OPER; \
  102. case 15: OPER; \
  103. case 14: OPER; \
  104. case 13: OPER; \
  105. case 12: OPER; \
  106. case 11: OPER; \
  107. case 10: OPER; \
  108. case 9: OPER; \
  109. case 8: OPER; \
  110. case 7: OPER; \
  111. case 6: OPER; \
  112. case 5: OPER; \
  113. case 4: OPER; \
  114. case 3: OPER; \
  115. case 2: OPER; \
  116. case 1: OPER; \
  117. } while( --duff_count > 0 ); \
  118. } \
  119. } \
  120. } G_STMT_END
  121. /* The g_info() macro was added in 2.40.
  122. */
  123. #ifndef g_info
  124. /* Hopefully we have varargs macros. Maybe revisit this.
  125. */
  126. #define g_info(...) \
  127. g_log( G_LOG_DOMAIN, G_LOG_LEVEL_INFO, __VA_ARGS__ )
  128. #endif
  129. /* Various integer range clips. Record over/under flows.
  130. */
  131. #define VIPS_CLIP_UCHAR( V, SEQ ) \
  132. G_STMT_START { \
  133. if( (V) < 0 ) { \
  134. (SEQ)->underflow++; \
  135. (V) = 0; \
  136. } \
  137. else if( (V) > UCHAR_MAX ) { \
  138. (SEQ)->overflow++; \
  139. (V) = UCHAR_MAX; \
  140. } \
  141. } G_STMT_END
  142. #define VIPS_CLIP_CHAR( V, SEQ ) \
  143. G_STMT_START { \
  144. if( (V) < SCHAR_MIN ) { \
  145. (SEQ)->underflow++; \
  146. (V) = SCHAR_MIN; \
  147. } \
  148. else if( (V) > SCHAR_MAX ) { \
  149. (SEQ)->overflow++; \
  150. (V) = SCHAR_MAX; \
  151. } \
  152. } G_STMT_END
  153. #define VIPS_CLIP_USHORT( V, SEQ ) \
  154. G_STMT_START { \
  155. if( (V) < 0 ) { \
  156. (SEQ)->underflow++; \
  157. (V) = 0; \
  158. } \
  159. else if( (V) > USHRT_MAX ) { \
  160. (SEQ)->overflow++; \
  161. (V) = USHRT_MAX; \
  162. } \
  163. } G_STMT_END
  164. #define VIPS_CLIP_SHORT( V, SEQ ) \
  165. G_STMT_START { \
  166. if( (V) < SHRT_MIN ) { \
  167. (SEQ)->underflow++; \
  168. (V) = SHRT_MIN; \
  169. } \
  170. else if( (V) > SHRT_MAX ) { \
  171. (SEQ)->overflow++; \
  172. (V) = SHRT_MAX; \
  173. } \
  174. } G_STMT_END
  175. #define VIPS_CLIP_UINT( V, SEQ ) \
  176. G_STMT_START { \
  177. if( (V) < 0 ) { \
  178. (SEQ)->underflow++; \
  179. (V) = 0; \
  180. } \
  181. } G_STMT_END
  182. #define VIPS_CLIP_NONE( V, SEQ ) {}
  183. /* Not all platforms have PATH_MAX (eg. Hurd) and we don't need a platform one
  184. * anyway, just a static buffer big enough for almost any path.
  185. */
  186. #define VIPS_PATH_MAX (4096)
  187. const char *vips_enum_string( GType enm, int value );
  188. const char *vips_enum_nick( GType enm, int value );
  189. int vips_enum_from_nick( const char *domain, GType type, const char *str );
  190. int vips_flags_from_nick( const char *domain, GType type, const char *nick );
  191. gboolean vips_slist_equal( GSList *l1, GSList *l2 );
  192. void *vips_slist_map2( GSList *list, VipsSListMap2Fn fn, void *a, void *b );
  193. void *vips_slist_map2_rev( GSList *list, VipsSListMap2Fn fn, void *a, void *b );
  194. void *vips_slist_map4( GSList *list,
  195. VipsSListMap4Fn fn, void *a, void *b, void *c, void *d );
  196. void *vips_slist_fold2( GSList *list, void *start,
  197. VipsSListFold2Fn fn, void *a, void *b );
  198. GSList *vips_slist_filter( GSList *list, VipsSListMap2Fn fn, void *a, void *b );
  199. void vips_slist_free_all( GSList *list );
  200. void *vips_map_equal( void *a, void *b );
  201. void *vips_hash_table_map( GHashTable *hash,
  202. VipsSListMap2Fn fn, void *a, void *b );
  203. char *vips_strncpy( char *dest, const char *src, int n );
  204. char *vips_strrstr( const char *haystack, const char *needle );
  205. gboolean vips_ispostfix( const char *a, const char *b );
  206. gboolean vips_iscasepostfix( const char *a, const char *b );
  207. gboolean vips_isprefix( const char *a, const char *b );
  208. char *vips_break_token( char *str, const char *brk );
  209. void vips__chomp( char *str );
  210. int vips_vsnprintf( char *str, size_t size, const char *format, va_list ap );
  211. int vips_snprintf( char *str, size_t size, const char *format, ... )
  212. __attribute__((format(printf, 3, 4)));
  213. int vips_filename_suffix_match( const char *path, const char *suffixes[] );
  214. gint64 vips_file_length( int fd );
  215. int vips__write( int fd, const void *buf, size_t count );
  216. int vips__open( const char *filename, int flags, ... );
  217. int vips__open_read( const char *filename );
  218. FILE *vips__fopen( const char *filename, const char *mode );
  219. FILE *vips__file_open_read( const char *filename,
  220. const char *fallback_dir, gboolean text_mode );
  221. FILE *vips__file_open_write( const char *filename,
  222. gboolean text_mode );
  223. char *vips__file_read( FILE *fp, const char *name, size_t *length_out );
  224. char *vips__file_read_name( const char *name, const char *fallback_dir,
  225. size_t *length_out );
  226. int vips__file_write( void *data, size_t size, size_t nmemb, FILE *stream );
  227. guint64 vips__get_bytes( const char *filename,
  228. unsigned char buf[], guint64 len );
  229. int vips__fgetc( FILE *fp );
  230. GValue *vips__gvalue_ref_string_new( const char *text );
  231. void vips__gslist_gvalue_free( GSList *list );
  232. GSList *vips__gslist_gvalue_copy( const GSList *list );
  233. GSList *vips__gslist_gvalue_merge( GSList *a, const GSList *b );
  234. char *vips__gslist_gvalue_get( const GSList *list );
  235. gint64 vips__seek_no_error( int fd, gint64 pos, int whence );
  236. gint64 vips__seek( int fd, gint64 pos, int whence );
  237. int vips__ftruncate( int fd, gint64 pos );
  238. int vips_existsf( const char *name, ... )
  239. __attribute__((format(printf, 1, 2)));
  240. int vips_mkdirf( const char *name, ... )
  241. __attribute__((format(printf, 1, 2)));
  242. int vips_rmdirf( const char *name, ... )
  243. __attribute__((format(printf, 1, 2)));
  244. int vips_rename( const char *old_name, const char *new_name );
  245. FILE *vips_popenf( const char *fmt, const char *mode, ... )
  246. __attribute__((format(printf, 1, 3)));
  247. /**
  248. * VipsToken:
  249. * @VIPS_TOKEN_LEFT: left bracket
  250. * @VIPS_TOKEN_RIGHT: right bracket
  251. * @VIPS_TOKEN_STRING: string constant
  252. * @VIPS_TOKEN_EQUALS: equals sign
  253. * @VIPS_TOKEN_COMMA: comma
  254. *
  255. * Tokens returned by the vips lexical analyzer, see vips__token_get(). This
  256. * is used to parse option strings for arguments.
  257. *
  258. * Left and right brackets can be any of (, {, [, <.
  259. *
  260. * Strings may be in double quotes, and may contain escaped quote characters,
  261. * for example string, "string" and "str\"ing".
  262. *
  263. */
  264. typedef enum {
  265. VIPS_TOKEN_LEFT = 1,
  266. VIPS_TOKEN_RIGHT,
  267. VIPS_TOKEN_STRING,
  268. VIPS_TOKEN_EQUALS,
  269. VIPS_TOKEN_COMMA
  270. } VipsToken;
  271. const char *vips__token_get( const char *buffer,
  272. VipsToken *token, char *string, int size );
  273. const char *vips__token_must( const char *buffer, VipsToken *token,
  274. char *string, int size );
  275. const char *vips__token_need( const char *buffer, VipsToken need_token,
  276. char *string, int size );
  277. const char *vips__token_segment( const char *p, VipsToken *token,
  278. char *string, int size );
  279. const char *vips__token_segment_need( const char *p, VipsToken need_token,
  280. char *string, int size );
  281. const char *vips__find_rightmost_brackets( const char *p );
  282. void vips__filename_split8( const char *name,
  283. char *filename, char *option_string );
  284. int vips_ispoweroftwo( int p );
  285. int vips_amiMSBfirst( void );
  286. char *vips__temp_name( const char *format );
  287. void vips__change_suffix( const char *name, char *out, int mx,
  288. const char *new_suff, const char **olds, int nolds );
  289. char *vips_realpath( const char *path );
  290. guint32 vips__random( guint32 seed );
  291. guint32 vips__random_add( guint32 seed, int value );
  292. const char *vips__icc_dir( void );
  293. const char *vips__windows_prefix( void );
  294. char *vips__get_iso8601( void );
  295. #ifdef __cplusplus
  296. }
  297. #endif /*__cplusplus*/
  298. #endif /*VIPS_UTIL_H*/