/ocr/ocrservice/jni/common/utils.h

http://eyes-free.googlecode.com/ · C Header · 106 lines · 81 code · 20 blank · 5 comment · 6 complexity · 13201edc01cbee1ed185cca7384ac9e5 MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. // Author: andrewharp@google.com (Andrew Harp)
  3. #ifndef JAVA_COM_GOOGLE_ANDROID_APPS_UNVEIL_JNI_COMMON_UTILS_H_
  4. #define JAVA_COM_GOOGLE_ANDROID_APPS_UNVEIL_JNI_COMMON_UTILS_H_
  5. #include <android/log.h>
  6. #include <stdlib.h>
  7. #ifdef HAVE_ARMEABI_V7A
  8. #include <cpu-features.h>
  9. #include <arm_neon.h>
  10. #endif
  11. #include <math.h>
  12. #include "types.h"
  13. #define SAFE_DELETE(pointer) {\
  14. if ((pointer) != NULL) {\
  15. LOGV("Safe deleting pointer: %s", #pointer);\
  16. delete (pointer);\
  17. (pointer) = NULL;\
  18. } else {\
  19. LOGV("Pointer already null: %s", #pointer);\
  20. }\
  21. }
  22. #ifdef VERBOSE_LOGGING
  23. #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
  24. #else
  25. #define LOGV(...) {}
  26. #endif
  27. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
  28. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
  29. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
  30. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
  31. #define LOG_TAG "goggles"
  32. #ifdef SANITY_CHECKS
  33. #define CHECK(condition, ...) {\
  34. if (!(condition)) {\
  35. LOGE("CHECK FAILED: (%s) @ %s:%u\n", #condition, __FILE__, __LINE__);\
  36. LOGE(__VA_ARGS__);\
  37. }\
  38. }
  39. #else
  40. #define CHECK(...) {}
  41. #endif
  42. #ifdef HAVE_ARMEABI_V7A
  43. // Runtime check for NEON support. Only call on devices that support at least
  44. // armeabi-v7a.
  45. inline bool supportsNeon() {
  46. return (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
  47. }
  48. #endif
  49. #ifndef max
  50. #define max(a, b) (((a) > (b)) ? (a) : (b))
  51. #endif
  52. #ifndef min
  53. #define min(a, b) (((a) > (b)) ? (b) : (a))
  54. #endif
  55. template<typename T>
  56. inline static T square(const T a) {
  57. return a * a;
  58. }
  59. template<typename T>
  60. inline static T clip(const T a, const T floor, const T ceil) {
  61. return min(ceil, max(a, floor));
  62. }
  63. template<typename T>
  64. inline static int32 floor(const T a) {
  65. return static_cast<int32>(a);
  66. }
  67. template<typename T>
  68. inline static int32 ceil(const T a) {
  69. return floor(a) + 1;
  70. }
  71. template<typename T>
  72. inline static bool inRange(const T a, const T min, const T max) {
  73. return (a >= min) && (a <= max);
  74. }
  75. template<typename T>
  76. inline static int32 round(const float a) {
  77. return (a - (float) floor(a) > 0.5f) ? ceil(a) : floor(a);
  78. }
  79. template<typename T>
  80. inline static void swap(T* const a, T* const b) {
  81. // Cache out the VALUE of what's at a.
  82. T tmp = *a;
  83. *a = *b;
  84. *b = tmp;
  85. }
  86. #endif // JAVA_COM_GOOGLE_ANDROID_APPS_UNVEIL_JNI_COMMON_UTILS_H_