
#include "ax_test_utils.h"

#ifdef __EOS__
	#include <rtthread.h>
#else
	#ifdef _USE_LOMBO_MALLOC_
	#include "lombo_malloc.h"
	#else
	#include "mi_mbase.h"
	#endif
#endif

#ifndef u8
typedef unsigned char u8;
#endif

#ifndef u16
typedef unsigned short u16;
#endif

#define NN_LOMBO_COLOR_A(color)         (((color) >> 24) & 0xff)
#define NN_LOMBO_COLOR_R(color)         (((color) >> 16) & 0xff)
#define NN_LOMBO_COLOR_G(color)         (((color) >> 8) & 0xff)
#define NN_LOMBO_COLOR_B(color)         ((color) & 0xff)

static void rgb_to_yuv(int r, int g, int b, u8 *py, u8 *pu, u8 *pv)
{
    int result;
    u8 y, u, v;

    /* y:[16, 235] u/v:[16, 240] */
    result = (r * 77 + g * 150 + b * 29) >> 8; // rgb to y
    if (result < 16) {
        y = 16;
    } else if (result > 235) {
        y = 235;
    } else {
        y = result;
    }

    result = ((r * (-44) - g * 87 + b * 131) >> 8) + 128; // rgb to u
    if (result < 16) {
        u = 16;
    } else if (result > 240) {
        u = 240;
    } else {
        u = result;
    }

    result = ((r * 131 - g * 110 - b * 21) >> 8) + 128; // rgb to v
    if (result < 16) {
        v = 16;
    } else if (result > 240) {
        v = 240;
    } else {
        v = result;
    }

    *py = y;
    *pu = u;
    *pv = v;
}

int drawrectback(char *y, int w, ezax_rt_t *pRect)
{
	int i = 0;
	int j = 0;
	int line_w = 4;

	for (j = pRect->y0; j < pRect->y0 + line_w; j++)
		memset(y + j*w + pRect->x0, 255, pRect->x1 - pRect->x0);

	for (j = pRect->y1; j > pRect->y1 - line_w; j--)
		memset(y + j*w + pRect->x0, 255, pRect->x1 - pRect->x0);

	for (i = pRect->y0; i < pRect->y1; i++) {
		for (j = 0; j < line_w; j++) {
			*(y + i*w + pRect->x0 + j) = 255;
			*(y + i*w + pRect->x1 - 1 -j) = 255;
		}
	}


	return 0;
}

int drawrectback_c(char *y, int w, ezax_rt_t *pRect, int color)
{
	int i = 0;
	int j = 0;
	int line_w = 4;

	int value_y = 0;
	//int value_u = 0;
	//int value_v = 0;

	if (color == 0) {
		value_y = 255;
		//value_u = 255;
		//value_v = 255;	
	}

	if (color == 1) {
		value_y = 0;
		//value_u = 0;
		//value_v = 0;	
	}

	for (j = pRect->y0; j < pRect->y0 + line_w; j++)
		memset(y + j*w + pRect->x0, value_y, pRect->x1 - pRect->x0);

	for (j = pRect->y1; j > pRect->y1 - line_w; j--)
		memset(y + j*w + pRect->x0, value_y, pRect->x1 - pRect->x0);

	for (i = pRect->y0; i < pRect->y1; i++) {
		for (j = 0; j < line_w; j++) {
			*(y + i*w + pRect->x0 + j) = value_y;
			*(y + i*w + pRect->x1 - 1 -j) = value_y;
		}
	}


	return 0;
}

int draw_rect_with_color(char *y, int w, int h, ezax_rt_t *pRect, int color)
{
	int i = 0;
	int j = 0;
	int line_w = 4;

	int r, g, b;

	u8 value_y, value_u, value_v;
	u16 uv;
	u8 *uv_ptr;
	u16 *temp16;

	r = NN_LOMBO_COLOR_R(color);
	g = NN_LOMBO_COLOR_G(color);
	b = NN_LOMBO_COLOR_B(color);
	rgb_to_yuv(r, g, b, &value_y, &value_u, &value_v);
	uv = (value_v << 8) + value_u;

	uv_ptr = (u8 *)(y + w * h);
	//printf("rgb[%d %d %d]", r, g, b);
	//printf("yuv[%d %d %d]", value_y, value_u, value_v);

	for (i = pRect->y0; i < pRect->y0 + line_w; i++) {
		memset(y + i*w + pRect->x0, value_y, pRect->x1 - pRect->x0);
		temp16 = (u16 *)(uv_ptr + i / 2 * w);
		for (j = pRect->x0 / 2; j < pRect->x1 / 2; j++) {
			temp16[j] = uv;
		}
	}

	for (i = pRect->y1; i > pRect->y1 - line_w; i--) {
		memset(y + i * w + pRect->x0, value_y, pRect->x1 - pRect->x0);
		temp16 = (u16 *)(uv_ptr + i / 2 * w);
		for (j = pRect->x0 / 2; j < pRect->x1 / 2; j++) {
			temp16[j] = uv;
		}
	}

	for (i = pRect->y0; i < pRect->y1; i++) {
		for (j = 0; j < line_w; j++) {
			*(y + i * w + pRect->x0 + j) = value_y;
			*(y + i * w + pRect->x1 - 1 -j) = value_y;
		}
		temp16 = (u16 *)(uv_ptr + i / 2 * w);
		for (j = pRect->x0 / 2; j < (pRect->x0 + line_w) / 2; j++) {
			temp16[j] = uv;
		}
		for (j = (pRect->x1 - line_w) / 2; j < pRect->x1 / 2; j++) {
			temp16[j] = uv;
		}
	}

	return 0;
}

void *ax_malloc_uncache(unsigned int *phy_addr, int size)
{
	void *vir_addr;
#ifdef __EOS__
	vir_addr = rt_zalloc_unca_align(size, 4096);
	*phy_addr = unca_to_phys(vir_addr);
#else
	#ifdef _USE_LOMBO_MALLOC_
		vir_addr = lombo_malloc(size, MEM_UC, (unsigned long *)phy_addr, __FILE__, __LINE__);
	#else
		EI_MI_MBASE_MemAlloc((unsigned long long *)phy_addr, &vir_addr, "pepdet_test", NULL, size);
	#endif
#endif
	return vir_addr;
}


int ax_free_uncache(unsigned int phy_addr, void *viraddr)
{
#ifdef __EOS__
	rt_free_unca_align(viraddr);
#else
	#ifdef _USE_LOMBO_MALLOC_
		lombo_free(viraddr, MEM_UC);
	#else
		EI_MI_MBASE_MemFree(phy_addr, viraddr);
	#endif
#endif
	return 0;
}


void *ax_realloc_virt(void *prt, int size)
{
	return realloc(prt, size);
}



void *ax_malloc_virt(int size)
{
	return malloc(size);
}



int ax_free_virt(void *prt)
{
	free(prt);
	return 0;
}

