/*
 * Copyright 2015 Rockchip Electronics Co. LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __CAMERA2_HELPER_H__
#define __CAMERA2_HELPER_H__


template <typename T>
class RangeValue {
public:
	T min_, max_;
	/**
     * return absolute value from relative value
     * value: in percent (50 for 50%)
     */
	T value(int percent) {
		return static_cast<T>(min_ + (max_ - min_) * percent / 100);
	}
	RangeValue() { min_ = max_ = static_cast<T>(0); }

	bool Supported(void) const { return (min_ != max_); }
};


struct ImageFormat {
	int32_t width;
	int32_t height;

	int32_t format;  // Through out this demo, the format is fixed to
	// YUV_420 format
};

 /**
  * A helper class to assist image size comparison, by comparing the absolute
  * size
  * regardless of the portrait or landscape mode.
  */
class DisplayDimension {
public:
	DisplayDimension(int32_t w, int32_t h) : w_(w), h_(h), portrait_(false) {
		if (h > w) {
			// make it landscape
			w_ = h;
			h_ = w;
			portrait_ = true;
		}
	}
	DisplayDimension(const DisplayDimension& other) {
		w_ = other.w_;
		h_ = other.h_;
		portrait_ = other.portrait_;
	}

	DisplayDimension(void) {
		w_ = 0;
		h_ = 0;
		portrait_ = false;
	}
	DisplayDimension& operator=(const DisplayDimension& other) {
		w_ = other.w_;
		h_ = other.h_;
		portrait_ = other.portrait_;

		return (*this);
	}

	bool IsSameRatio(DisplayDimension& other) {
		return (w_ * other.h_ == h_ * other.w_);
	}
	bool operator>(DisplayDimension& other) {
		return (w_ >= other.w_ & h_ >= other.h_);
	}
	bool operator==(DisplayDimension& other) {
		return (w_ == other.w_ && h_ == other.h_ && portrait_ == other.portrait_);
	}
	DisplayDimension operator-(DisplayDimension& other) {
		DisplayDimension delta(w_ - other.w_, h_ - other.h_);
		return delta;
	}
	void Flip(void) { portrait_ = !portrait_; }
	bool IsPortrait(void) { return portrait_; }
	int32_t width(void) { return w_; }
	int32_t height(void) { return h_; }
	int32_t org_width(void) { return (portrait_ ? h_ : w_); }
	int32_t org_height(void) { return (portrait_ ? w_ : h_); }

private:
	int32_t w_, h_;
	bool portrait_;
};


#endif /* __CAMERA2_HELPER_H__ */
