#include "gc.h" /* clip functions */ static int gClipIfLessThan( tPoint *p, tPoint *q ) { int clip = 0; if( p->x < q->x ) { p->x = q->x; clip++; } if( p->y < q->y ) { p->y = q->y; clip++; } return clip; } static int gClipIfGreaterThan( tPoint *p, tPoint *q ) { int clip = 0; if( p->x > q->x ) { p->x = q->x; clip++; } if( p->y > q->y ) { p->y = q->y; clip++; } return clip; } static int gClipPointAgainstRect( tPoint *p, tRect *dst ) { int clip = 0; clip += gClipIfLessThan( p, &dst->UpperLeft ); clip += gClipIfGreaterThan( p, &dst->BottomRight ); return clip; } static int gClipRectAgainstRect( tRect *src, tRect *dst ) { int clip = 0; clip += gClipPointAgainstRect( &src->UpperLeft, dst ); clip += gClipPointAgainstRect( &src->BottomRight, dst ); return clip; } /* return value : 0=invisible, 1=visible */ int ClipPointAgainstRect( tPoint *p, tRect *dst ) { if( p->x < dst->UpperLeft.x ) return 0; if( p->x > dst->BottomRight.x ) return 0; if( p->y < dst->UpperLeft.y ) return 0; if( p->y > dst->BottomRight.y ) return 0; return 1; } /* return value : 0=invisible, other=visible edges coded by bits 0xf=all */ int ClipRectAgainstRect( tRect *src, tRect *dst ) { int rv = 0; /* visible ? */ if( src->BottomRight.x < dst->UpperLeft.x ) return 0; if( src->UpperLeft.x > dst->BottomRight.x ) return 0; if( src->BottomRight.y < dst->UpperLeft.y ) return 0; if( src->UpperLeft.y > dst->BottomRight.y ) return 0; /* patrially visible */ if( src->BottomRight.x <= dst->BottomRight.x ) rv |= GC_EDGE_RIGHT; else src->BottomRight.x = dst->BottomRight.x; if( src->UpperLeft.x >= dst->UpperLeft.x ) rv |= GC_EDGE_LEFT; else src->UpperLeft.x = dst->UpperLeft.x; if( src->BottomRight.y <= dst->BottomRight.y ) rv |= GC_EDGE_BOTTOM; else src->BottomRight.y = dst->BottomRight.y; if( src->UpperLeft.y >= dst->UpperLeft.y ) rv |= GC_EDGE_TOP; else src->UpperLeft.y = dst->UpperLeft.y; return rv; } void NormRect( tRect *r ) { if( r->UpperLeft.x > r->BottomRight.x ) r->BottomRight.x = r->UpperLeft.x; if( r->UpperLeft.y > r->BottomRight.y ) r->BottomRight.y = r->UpperLeft.y; } void CenterRectH( tRect *a, tRect *b ) { tCoord ca = (a->UpperLeft.x + a->BottomRight.x)>>1; tCoord cb = (b->UpperLeft.x + b->BottomRight.x)>>1; a->UpperLeft.x += cb-ca; a->BottomRight.x += cb-ca; } void CenterRectV( tRect *a, tRect *b ) { tCoord ca = (a->UpperLeft.y + a->BottomRight.y)>>1; tCoord cb = (b->UpperLeft.y + b->BottomRight.y)>>1; a->UpperLeft.y += cb-ca; a->BottomRight.y += cb-ca; } void CenterRectToPoint( tRect *a, tPoint *b ) { tRect r = { {b->x, b->y}, {b->x, b->y} }; CenterRectH(a,&r); CenterRectV(a,&r); } void CenterRect( tRect *a, tRect *b ) { CenterRectH(a,b); CenterRectV(a,b); } void ExtendRect( tRect *d, tRect *s, tCoord v ) { d->UpperLeft.x = s->UpperLeft.x -v; d->UpperLeft.y = s->UpperLeft.y -v; d->BottomRight.x = s->BottomRight.x +v; d->BottomRight.y = s->BottomRight.y +v; } /* color functions */ tColor SetColorRGB ( int R, int G, int B ) { return SetColorRGBA(R,G,B,255); } tColor SetColorRGBA( int R, int G, int B, int A ) { return ((R&0xff)<<24) | ((G&0xff)<<16) | ((B&0xff)<<8) | (A&0xff); } int GetColorRed ( tColor color ) { return (color>>24) & 0xff; } int GetColorGreen( tColor color ) { return (color>>16) & 0xff; } int GetColorBlue ( tColor color ) { return (color>>8) & 0xff; } int GetColorAlpha( tColor color ) { return color & 0xff; } tColor RGBToYUV( tColor rgb ) { int R = GetColorRed(rgb); int G = GetColorGreen(rgb); int B = GetColorBlue(rgb); int Y = (( R*299 + G*587 + B*114 )+500 )/1000 ; int U = (( - R*168736 - G*331264 + B*500000 )+500000*256)/1000000; int V = (( R*500000 - G*418688 - B* 81312 )+500000*256)/1000000; return SetColorRGB(Y,U,V); } tColor YUVToRGB( tColor yuv ) { int Y = GetColorRed(yuv); int U = GetColorGreen(yuv)-128; int V = GetColorBlue(yuv)-128; int r = (( Y*1000000 - U* 1 + V*1402000)+500000)/1000000; int g = (( Y*1000000 - U* 344136 - V* 714136)+500000)/1000000; int b = (( Y*1000000 + U*1772000 + V* 0)+500000)/1000000; int R = r<0 ? 0 : r>255 ? 255 : r; int G = g<0 ? 0 : g>255 ? 255 : g; int B = b<0 ? 0 : b>255 ? 255 : b; return SetColorRGB(R,G,B); } tColor RGBToBW( tColor rgb ) { int R = GetColorRed(rgb); int G = GetColorGreen(rgb); int B = GetColorBlue(rgb); int Y = (( R*299 + G*587 + B*114 )+500 )/1000 ; return SetColorRGB(Y,Y,Y); }