PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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