/************************************************************************************
libsj
Copyright (c) 2010, Segway-Japan, Ltd.
All rights reserved.

Contact us if you use this software for sell.
If you use this software not for sell, you can use this software under BSD lisence.
See the files LICENSE.TXT and LICENSE-BSD.TXT for more details.                     
************************************************************************************/
#ifndef __MISC_H__
#define __MISC_H__




inline void drawcolor(unsigned char* dstbuf, int w, int h, int x, int y, 
	       unsigned char r, unsigned char g, unsigned char b)
{
    if (x < 0 || x > w-1 || y < 0 || y > h-1) {
	return;
    }
    int index = (4*w) * y + 4 * x;
    dstbuf[index]   = 0xFF;
    dstbuf[index+1] = r;
    dstbuf[index+2] = g;
    dstbuf[index+3] = b;
}


inline void drawline(unsigned char* dstbuf, int w, int h, int x0, int y0, int x1, int y1, 
	      unsigned char r, unsigned char g, unsigned char b)
{
    int E,x,y;
    int dx,dy,sx,sy;
    
    sx = ( x1 > x0 ) ? 1 : -1;
    dx = ( x1 > x0 ) ? x1 - x0 : x0 - x1;
    sy = ( y1 > y0 ) ? 1 : -1;
    dy = ( y1 > y0 ) ? y1 - y0 : y0 - y1;

    x = x0;
    y = y0;
    if ( dx >= dy ) {
	E = -dx;
	for ( int i = 0; i <= dx; i++ ) {
	    drawcolor(dstbuf, w, h, x, y, r, g, b );
	    x += sx;
	    E += 2 * dy;
	    if ( E >= 0 ) {
		y += sy;
		E -= 2 * dx;
	    }
	}
    } 
    else {
	E = -dy;
	for (int i = 0; i <= dy; i++ ) {
	    drawcolor(dstbuf, w, h, x, y, r, g, b );
	    y += sy;
	    E += 2 * dx;
	    if( E >= 0 ) {
		x += sx;
		E -= 2 * dy;
	    }
	}
    }
}




#endif
