Exemplo n.º 1
0
void spAnimation_mix (const spAnimation* self, spSkeleton* skeleton, float lastTime, float time, int loop, spEvent** events,
		int* eventsCount, float alpha) {
	int i, n = self->timelinesCount;

	if (loop && self->duration) {
		time = FMOD(time, self->duration);
		lastTime = FMOD(lastTime, self->duration);
	}

	for (i = 0; i < n; ++i)
		spTimeline_apply(self->timelines[i], skeleton, lastTime, time, events, eventsCount, alpha);
}
Exemplo n.º 2
0
/**
 * Write a 64-bit integer in memory.
 * @param memory	Memory to write in.
 * @param address	Address to write integer to.
 * @param val		Integer to write.
 * @ingroup memory
 */
void gliss_mem_write64(gliss_memory_t *mem, gliss_address_t address, uint64_t val)
{
    gliss_address_t offset;
    uint64_t* q;

	/* compute address */
    page_entry_t *pte;
    pte = mem_get_page(mem, address);
    offset = FMOD(address, MEM_PAGE_SIZE);

#   if HOST_ENDIANNESS != TARGET_ENDIANNESS
    q = (uint64_t *)(pte->storage + MEM_PAGE_SIZE-8 - offset);
#   else
    q = (uint64_t *)(pte->storage + offset);
#   endif

    // aligned or cross-page?
    if(!((offset & 0x7) | ((offset + 7) & MEM_PAGE_SIZE)))
        *q = val;
    else {
		union val_t {
			uint8_t bytes[8];
			uint64_t dword;
		} *p = (union val_t *)&val;
        gliss_mem_write(mem, address, p->bytes, 8);
	}

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, sizeof(val), gliss_access_write, mem->spy_data);
#	endif
}
Exemplo n.º 3
0
/**
 * Read a 64-bit integer.
 * @param memory	Memory to work with.
 * @param address	Address of integer to read.
 * @return			Read integer.
 * @ingroup memory
 */
uint64_t gliss_mem_read64(gliss_memory_t *mem, gliss_address_t address) {

	/* get page */
    page_entry_t* pte    = mem_get_page(mem, address);
    gliss_address_t offset = FMOD(address, MEM_PAGE_SIZE);
    uint64_t r;

#   if HOST_ENDIANNESS != TARGET_ENDIANNESS
    	uint8_t* p = pte->storage + (MEM_PAGE_SIZE-8 - offset);
	#   else
    uint8_t* p = pte->storage + offset;
#endif

    // aligned or cross-page ?
    if(!((offset & 0x7) | ((offset + 7) & MEM_PAGE_SIZE)))
        r = *(uint64_t *)p;
    // unaligned !
    else
    {
		union {
			uint8_t bytes[8];
			uint64_t dword;
		} val;
        gliss_mem_read(mem, address, val.bytes, 8);
        r = val.dword;
    }

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, sizeof(r), gliss_access_read, mem->spy_data);
#	endif
    return r;
}
Exemplo n.º 4
0
/**
 * Read a 16-bit integer.
 * @param memory	Memory to work with.
 * @param address	Address of integer to read.
 * @return			Read integer.
 * @ingroup memory
 */
uint16_t gliss_mem_read16(gliss_memory_t *mem, gliss_address_t address) {

	/* get page */
    gliss_address_t offset = FMOD(address, MEM_PAGE_SIZE);
    page_entry_t*   pte    = mem_get_page(mem, address);
    uint16_t r;

#   if HOST_ENDIANNESS != TARGET_ENDIANNESS
    	uint8_t* p = pte->storage + (MEM_PAGE_SIZE-2 - offset);
#   else
    	uint8_t* p = pte->storage + offset;
#   endif

    // aligned or cross-page ?
    if(!((offset & 0x1) | ((offset + 1) & MEM_PAGE_SIZE)))
        r = *(uint16_t *)p;
	else {
		union {
			uint8_t bytes[2];
			uint16_t half;
		} val;
        gliss_mem_read(mem, address, val.bytes, 2);
        r = val.half;
    }

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, sizeof(r), gliss_access_read, mem->spy_data);
#	endif
    return r;
}
Exemplo n.º 5
0
/**
 * Write a buffer into memory.
 * @param memory	Memory to write into.
 * @param address	Address in memory to write to.
 * @param buffer	Buffer address in host memory.
 * @param size		Size of the buffer to write.
 * @ingroup memory
 */
void gliss_mem_write(gliss_memory_t* memory, gliss_address_t address, void* buffer, size_t size)
{
    assert(size > 0);
	int i;
    uint32_t      offset = FMOD(address , MEM_PAGE_SIZE);
    uint32_t      sz     = MEM_PAGE_SIZE - offset;
    memory_64_t*  mem    = (memory_64_t *)memory;
    page_entry_t* pte    = mem_get_page(mem, address);;

    if(size > sz)
    {
#       if HOST_ENDIANNESS == TARGET_ENDIANNESS
        memcpy(pte->storage+offset, buffer, sz);
        size    -= sz;
        address += sz;
        buffer   = (uint8_t *)buffer + sz;

        while(size >= MEM_PAGE_SIZE)
        {
            pte = mem_get_page(mem, address);
            memcpy(pte->storage, buffer, MEM_PAGE_SIZE);
            size    -= MEM_PAGE_SIZE;
            address += MEM_PAGE_SIZE;
            buffer   = (uint8_t *)buffer + MEM_PAGE_SIZE;
        }

        if(size > 0)
        {
            pte = mem_get_page(mem, address);
            memcpy(pte->storage, buffer, size);
        }
#       else
		for(i=0; i<size; i++)
        {
            sz--;
            *((uint8_t*)(pte->storage) + sz) = *((uint8_t*)buffer + i);
            address += 1;

            if( sz == 0)
            {
                pte = mem_get_page(mem, address);
                sz  = MEM_PAGE_SIZE;
            }
        }
#       endif
    }
    else
    {
#       if HOST_ENDIANNESS == TARGET_ENDIANNESS
        memcpy(pte->storage + offset, buffer, size);
#       else
        for(i=0; i<size; i++)
            *((uint8_t*)(pte->storage)+sz-1-i) = *((uint8_t*)buffer + i);
#       endif
    }

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, size, gliss_access_write, mem->spy_data);
#	endif
}
Exemplo n.º 6
0
/*
 * Convert a line to tunings; returns -1 if it ok
 */
int Microtonal::linetotunings(unsigned int nline, const char *line)
{
    int      x1 = -1, x2 = -1, type = -1;
    REALTYPE x  = -1.0, tmp, tuning = 1.0;
    if(strstr(line, "/") == NULL) {
        if(strstr(line, ".") == NULL) { // M case (M=M/1)
            sscanf(line, "%d", &x1);
            x2   = 1;
            type = 2; //division
        }
        else {  // float number case
            sscanf(line, "%f", &x);
            if(x < 0.000001)
                return 1;
            type = 1; //float type(cents)
        }
    }
    else {  // M/N case
        sscanf(line, "%d/%d", &x1, &x2);
        if((x1 < 0) || (x2 < 0))
            return 1;
        if(x2 == 0)
            x2 = 1;
        type = 2; //division
    }

    if(x1 <= 0)
        x1 = 1;     //not allow zero frequency sounds (consider 0 as 1)

    //convert to float if the number are too big
    if((type == 2)
       && ((x1 > (128 * 128 * 128 - 1)) || (x2 > (128 * 128 * 128 - 1)))) {
        type = 1;
        x    = ((REALTYPE) x1) / x2;
    }
    switch(type) {
    case 1:
        x1     = (int) floor(x);
        tmp    = FMOD(x, 1.0);
        x2     = (int) (floor(tmp * 1e6));
        tuning = pow(2.0, x / 1200.0);
        break;
    case 2:
        x      = ((REALTYPE)x1) / x2;
        tuning = x;
        break;
    }

    tmpoctave[nline].tuning = tuning;
    tmpoctave[nline].type   = type;
    tmpoctave[nline].x1     = x1;
    tmpoctave[nline].x2     = x2;

    return -1; //ok
}
Exemplo n.º 7
0
static int paint_radials_around_ants(doublemap* map, doublemap* radial, PyObject* ants) {
	PyObject *iterator, *item;
	int row, col, row2, col2;
	int stride, stride2;
	int rows2, cols2;

	rows2 = radial->rows/2;
	cols2 = radial->cols/2;

	iterator = PyObject_GetIter(ants);
	if (iterator == NULL) {
		return -1;
	}

	while ((item = PyIter_Next(iterator))) {
		if (!PyArg_ParseTuple(item, "ii", &row, &col)) {
			Py_DECREF(item);
			Py_DECREF(iterator);
			return -1;
		}
		Py_DECREF(item);
		if (row >= g_rows || col >= g_cols || row < 0 || col < 0) {
			RAISE("Invalid input data - one of the ants is out od map bounds");
			Py_DECREF(iterator);
			return -1;
		}
		for (row2=0; row2<radial->rows; row2++) {
			stride = FMOD((row2 + row - rows2), map->rows) * map->cols;
			stride2 = row2 * radial->cols;
			for (col2=0; col2<radial->cols; col2++) {
				map->values[stride+FMOD((col2 + col - cols2), map->cols)] += radial->values[stride2+col2];
			}
		}
	}

	Py_DECREF(iterator);

	if (PyErr_Occurred()) {
		return -1;
	}
	return 0;
}
Exemplo n.º 8
0
DOUBLE
REMAINDER (DOUBLE x, DOUBLE y)
{
  if (isfinite (x) && isfinite (y) && y != L_(0.0))
    {
      if (x == L_(0.0))
        /* Return x, regardless of the sign of y.  */
        return x;

      {
        int negate = ((!signbit (x)) ^ (!signbit (y)));
        DOUBLE r;

        /* Take the absolute value of x and y.  */
        x = FABS (x);
        y = FABS (y);

        /* Trivial case that requires no computation.  */
        if (x <= L_(0.5) * y)
          return (negate ? - x : x);

        /* With a fixed y, the function x -> remainder(x,y) has a period 2*y.
           Therefore we can reduce the argument x modulo 2*y.  And it's no
           problem if 2*y overflows, since fmod(x,Inf) = x.  */
        x = FMOD (x, L_(2.0) * y);

        /* Consider the 3 cases:
             0 <= x <= 0.5 * y
             0.5 * y < x < 1.5 * y
             1.5 * y <= x <= 2.0 * y  */
        if (x <= L_(0.5) * y)
          r = x;
        else
          {
            r = x - y;
            if (r > L_(0.5) * y)
              r = x - L_(2.0) * y;
          }
        return (negate ? - r : r);
      }
    }
  else
    {
      if (ISNAN (x) || ISNAN (y))
        return x + y; /* NaN */
      else if (isinf (y))
        return x;
      else
        /* x infinite or y zero */
        return NAN;
    }
}
Exemplo n.º 9
0
/**
 * Read an 8-bit integer.
 * @param memory	Memory to work with.
 * @param address	Address of integer to read.
 * @return			Read integer.
 * @ingroup memory
 */
uint8_t gliss_mem_read8(gliss_memory_t *mem, gliss_address_t address)
{
    page_entry_t* pte    = mem_get_page(mem, address);
    gliss_address_t offset = FMOD(address, MEM_PAGE_SIZE);
    uint8_t r;

#   if HOST_ENDIANNESS != TARGET_ENDIANNESS
    	r = pte->storage[MEM_PAGE_SIZE-1 - offset];
#   else
    	r = pte->storage[offset];
#	endif

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, sizeof(r), gliss_access_read, mem->spy_data);
#	endif
    return r;
}
Exemplo n.º 10
0
/**
 * Write an 8-bit integer in memory.
 * @param memory	Memory to write in.
 * @param address	Address to write integer to.
 * @param val		Integer to write.
 * @ingroup memory
 */
void gliss_mem_write8(gliss_memory_t* mem, gliss_address_t address, uint8_t val)
{
    gliss_address_t offset;
    page_entry_t*   pte;
    pte    = mem_get_page(mem, address);
    offset = FMOD(address, MEM_PAGE_SIZE);


#   if HOST_ENDIANNESS != TARGET_ENDIANNESS
    	pte->storage[MEM_PAGE_SIZE-1 - offset] = val;
#   else
    	pte->storage[offset] = val;
#   endif

#	ifdef GLISS_MEM_SPY
    	mem->spy_fun(mem, address, sizeof(val), gliss_access_write, mem->spy_data);
#	endif
}
Exemplo n.º 11
0
void rgb_to_hsv(int r, int g, int b, float *h, float *s, float *v) {	
    float maxc = (float)MAX(MAX(r, g), b);
    float minc = (float)MIN(MIN(r, g), b);
    *v = maxc;
    if(minc == maxc) {
		*h = 0;
		*s = 0;
		return;
	}

	*s = (maxc-minc) / maxc;
	float rc = (maxc-r) / (maxc-minc);
	float gc = (maxc-g) / (maxc-minc);
	float bc = (maxc-b) / (maxc-minc);
	if(r == maxc) *h = bc-gc;
	else if(g == maxc) *h = 2.0f+rc-bc;
	else *h = 4.0f+gc-rc;

	*h = *h/6.0f;
	FMOD(*h, 1.0f);
}
Exemplo n.º 12
0
/*
 *---------------------------------------------------------------------------
 *
 * Blt_RotateBitmap --
 *
 *	Creates a new bitmap containing the rotated image of the given
 *	bitmap.  We also need a special GC of depth 1, so that we do
 *	not need to rotate more than one plane of the bitmap.
 *
 * Results:
 *	Returns a new bitmap containing the rotated image.
 *
 *---------------------------------------------------------------------------
 */
Pixmap
Blt_RotateBitmap(
    Tk_Window tkwin,
    Pixmap srcBitmap,		/* Source bitmap to be rotated */
    int srcWidth, int srcHeight, /* Width and height of the source bitmap */
    float angle,		/* # of degrees to rotate the bitmap. */
    int *destWidthPtr, 
    int *destHeightPtr)
{
    Display *display;		/* X display */
    GC bitmapGC;
    Pixmap destBitmap;
    Window root;		/* Root window drawable */
    XImage *srcImgPtr, *destImgPtr;
    double rotWidth, rotHeight;
    int destWidth, destHeight;

    display = Tk_Display(tkwin);
    root = Tk_RootWindow(tkwin);

#ifdef notdef
    /* Create a bitmap and image big enough to contain the rotated text */
    Blt_GetBoundingBox(srcWidth, srcHeight, angle, &rotWidth, &rotHeight,
	(Point2d *)NULL);
    destWidth = ROUND(rotWidth);
    destHeight = ROUND(rotHeight);
    destBitmap = Tk_GetPixmap(display, root, destWidth, destHeight, 1);
    bitmapGC = Blt_GetBitmapGC(tkwin);
    XSetForeground(display, bitmapGC, 0x0);
    XFillRectangle(display, destBitmap, bitmapGC, 0, 0, destWidth, destHeight);

    srcImgPtr = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, 
	ZPixmap);
    destImgPtr = XGetImage(display, destBitmap, 0, 0, destWidth, destHeight, 
	1, ZPixmap);
    angle = FMOD(angle, 360.0);
    if (FMOD(angle, 90.0) == 0.0) {
	int quadrant;
	int y;

	/* Handle right-angle rotations specifically */

	quadrant = (int)(angle / 90.0);
	switch (quadrant) {
	case ROTATE_270:	/* 270 degrees */
	    for (y = 0; y < destHeight; y++) {
		int x, sx;

		sx = y;
		for (x = 0; x < destWidth; x++) {
		    int sy;
		    unsigned long pixel;
		    
		    sy = destWidth - x - 1;
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_180:	/* 180 degrees */
	    for (y = 0; y < destHeight; y++) {
		int x, sy;

		sy = destHeight - y - 1;
		for (x = 0; x < destWidth; x++) {
		    int sx;
		    unsigned long pixel;

		    sx = destWidth - x - 1, 
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_90:		/* 90 degrees */
	    for (y = 0; y < destHeight; y++) {
		int x, sx;

		sx = destHeight - y - 1;
		for (x = 0; x < destWidth; x++) {
		    int sy;
		    unsigned long pixel;

		    sy = x;
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_0:		/* 0 degrees */
	    for (y = 0; y < destHeight; y++) {
		int x;

		for (x = 0; x < destWidth; x++) {
		    unsigned long pixel;

		    pixel = XGetPixel(srcImgPtr, x, y);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	default:
	    /* The calling routine should never let this happen. */
	    break;
	}
    } else {
	double radians, sinTheta, cosTheta;
	double sox, soy;	/* Offset from the center of
				 * the source rectangle. */
	double destCX, destCY;	/* Offset to the center of the destination
				 * rectangle. */
	int y;

	radians = (angle / 180.0) * M_PI;
	sinTheta = sin(radians), cosTheta = cos(radians);

	/*
	 * Coordinates of the centers of the source and destination rectangles
	 */
	sox = srcWidth * 0.5;
	soy = srcHeight * 0.5;
	destCX = destWidth * 0.5;
	destCY = destHeight * 0.5;

	/* For each pixel of the destination image, transform back to the
	 * associated pixel in the source image. */

	for (y = 0; y < destHeight; y++) {
	    double ty;
	    int x;

	    ty = y - destCY;
	    for (x = 0; x < destWidth; x++) {
		double tx, rx, ry, sx, sy;
		unsigned long pixel;

		/* Translate origin to center of destination image. */
		tx = x - destCX;

		/* Rotate the coordinates about the origin. */
		rx = (tx * cosTheta) - (ty * sinTheta);
		ry = (tx * sinTheta) + (ty * cosTheta);

		/* Translate back to the center of the source image. */
		rx += sox;
		ry += soy;

		sx = ROUND(rx);
		sy = ROUND(ry);

		/*
		 * Verify the coordinates, since the destination image can be
		 * bigger than the source.
		 */

		if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) ||
		    (sy < 0)) {
		    continue;
		}
		pixel = XGetPixel(srcImgPtr, sx, sy);
		if (pixel) {
		    XPutPixel(destImgPtr, x, y, pixel);
		}
	    }
	}
    }
    /* Write the rotated image into the destination bitmap. */
    XPutImage(display, destBitmap, bitmapGC, destImgPtr, 0, 0, 0, 0, 
	destWidth, destHeight);

    /* Clean up the temporary resources used. */
    XDestroyImage(srcImgPtr), XDestroyImage(destImgPtr);
    *destWidthPtr = destWidth;
    *destHeightPtr = destHeight;
#endif
    return destBitmap;
}
Exemplo n.º 13
0
    //------------------------------------------------------------------------
    void bezier_arc::init(real x,  real y, 
                          real rx, real ry, 
                          real start_angle, 
                          real sweep_angle)
    {
        start_angle = FMOD(start_angle, 2.0f * pi);
        if(sweep_angle >=  2.0f * pi) sweep_angle =  2.0f * pi;
        if(sweep_angle <= -2.0f * pi) sweep_angle = -2.0f * pi;

        if(FABS(sweep_angle) < 1e-10)
        {
            m_num_vertices = 4;
            m_cmd = path_cmd_line_to;
            m_vertices[0] = x + rx * (real)cos(start_angle);
            m_vertices[1] = y + ry * (real)sin(start_angle);
            m_vertices[2] = x + rx * (real)cos(start_angle + sweep_angle);
            m_vertices[3] = y + ry * (real)sin(start_angle + sweep_angle);
            return;
        }

        real total_sweep = 0.0f;
        real local_sweep = 0.0f;
        real prev_sweep;
        m_num_vertices = 2;
        m_cmd = path_cmd_curve4;
        bool done = false;
        do
        {
            if(sweep_angle < 0.0f)
            {
                prev_sweep  = total_sweep;
                local_sweep = -pi * 0.5f;
                total_sweep -= pi * 0.5f;
                if(total_sweep <= sweep_angle + bezier_arc_angle_epsilon)
                {
                    local_sweep = sweep_angle - prev_sweep;
                    done = true;
                }
            }
            else
            {
                prev_sweep  = total_sweep;
                local_sweep =  pi * 0.5f;
                total_sweep += pi * 0.5f;
                if(total_sweep >= sweep_angle - bezier_arc_angle_epsilon)
                {
                    local_sweep = sweep_angle - prev_sweep;
                    done = true;
                }
            }

            arc_to_bezier(x, y, rx, ry, 
                          start_angle, 
                          local_sweep, 
                          m_vertices + m_num_vertices - 2);

            m_num_vertices += 6;
            start_angle += local_sweep;
        }
        while(!done && m_num_vertices < 26);
    }
Exemplo n.º 14
0
/*
 *---------------------------------------------------------------------------
 *
 * Blt_RotateScaleBitmapArea --
 *
 *	Creates a scaled and rotated bitmap from a given bitmap.  The
 *	caller also provides (offsets and dimensions) the region of
 *	interest in the destination bitmap.  This saves having to
 *	process the entire destination bitmap is only part of it is
 *	showing in the viewport.
 *
 *	This uses a simple rotation/scaling of each pixel in the 
 *	destination image.  For each pixel, the corresponding 
 *	pixel in the source bitmap is used.  This means that 
 *	destination coordinates are first scaled to the size of 
 *	the rotated source bitmap.  These coordinates are then
 *	rotated back to their original orientation in the source.
 *
 * Results:
 *	The new rotated and scaled bitmap is returned.
 *
 * Side Effects:
 *	A new pixmap is allocated. The caller must release this.
 *
 *---------------------------------------------------------------------------
 */
Pixmap
Blt_ScaleRotateBitmapArea(
    Tk_Window tkwin,
    Pixmap srcBitmap,		/* Source bitmap. */
    unsigned int srcWidth, 
    unsigned int srcHeight,	/* Size of source bitmap */
    int regionX, int regionY,	/* Offset of region in virtual
				 * destination bitmap. */
    unsigned int regionWidth, 
    unsigned int regionHeight,	/* Desire size of bitmap region. */
    unsigned int destWidth,		
    unsigned int destHeight,	/* Virtual size of destination bitmap. */
    float angle)		/* Angle to rotate bitmap.  */
{
    Display *display;		/* X display */
    Window root;		/* Root window drawable */
    Pixmap destBitmap;
    XImage *srcImgPtr, *destImgPtr;
    double xScale, yScale;
    double rotWidth, rotHeight;
    GC bitmapGC;

    display = Tk_Display(tkwin);
    root = Tk_RootWindow(tkwin);

#ifdef notdef
    /* Create a bitmap and image big enough to contain the rotated text */
    bitmapGC = Blt_GetBitmapGC(tkwin);
    destBitmap = Tk_GetPixmap(display, root, regionWidth, regionHeight, 1);
    XSetForeground(display, bitmapGC, 0x0);
    XFillRectangle(display, destBitmap, bitmapGC, 0, 0, regionWidth, 
	regionHeight);

    srcImgPtr = XGetImage(display, srcBitmap, 0, 0, srcWidth, srcHeight, 1, 
	ZPixmap);
    destImgPtr = XGetImage(display, destBitmap, 0, 0, regionWidth, 
	regionHeight, 1, ZPixmap);
    angle = FMOD(angle, 360.0);

    Blt_GetBoundingBox(srcWidth, srcHeight, angle, &rotWidth, &rotHeight,
	(Point2d *)NULL);

    xScale = rotWidth / (double)destWidth;
    yScale = rotHeight / (double)destHeight;

    if (FMOD(angle, (double)90.0) == 0.0) {
	int quadrant;
	int x, y;

	/* Handle right-angle rotations specifically */

	quadrant = (int)(angle / 90.0);
	switch (quadrant) {
	case ROTATE_270:	/* 270 degrees */
	    for (y = 0; y < regionHeight; y++) {
		int sx;	

		sx = (int)(yScale * (double)(y + regionY));
		for (x = 0; x < regionWidth; x++) {
		    int sy;	
		    unsigned long pixel;

		    sy = (int)(xScale *(double)(destWidth - (x + regionX) - 1));
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_180:	/* 180 degrees */
	    for (y = 0; y < regionHeight; y++) {
		int sy;	

		sy = (int)(yScale * (double)(destHeight - (y + regionY) - 1));
		for (x = 0; x < regionWidth; x++) {
		    int sx;	
		    unsigned long pixel;

		    sx = (int)(xScale *(double)(destWidth - (x + regionX) - 1));
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_90:		/* 90 degrees */
	    for (y = 0; y < regionHeight; y++) {
		int sx;	

		sx = (int)(yScale * (double)(destHeight - (y + regionY) - 1));
		for (x = 0; x < regionWidth; x++) {
		    int sy;	
		    unsigned long pixel;

		    sy = (int)(xScale * (double)(x + regionX));
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	case ROTATE_0:		/* 0 degrees */
	    for (y = 0; y < regionHeight; y++) {
		int sy;

		sy = (int)(yScale * (double)(y + regionY));
		for (x = 0; x < regionWidth; x++) {
		    int sx;	
		    unsigned long pixel;

		    sx = (int)(xScale * (double)(x + regionX));
		    pixel = XGetPixel(srcImgPtr, sx, sy);
		    if (pixel) {
			XPutPixel(destImgPtr, x, y, pixel);
		    }
		}
	    }
	    break;

	default:
	    /* The calling routine should never let this happen. */
	    break;
	}
    } else {
	double radians, sinTheta, cosTheta;
	double sox, soy; 	/* Offset from the center of the
				 * source rectangle. */
	double rox, roy; 	/* Offset to the center of the
				 * rotated rectangle. */
	int x, y;

	radians = (angle / 180.0) * M_PI;
	sinTheta = sin(radians), cosTheta = cos(radians);

	/*
	 * Coordinates of the centers of the source and destination rectangles
	 */
	sox = srcWidth * 0.5;
	soy = srcHeight * 0.5;
	rox = rotWidth * 0.5;
	roy = rotHeight * 0.5;

	/* For each pixel of the destination image, transform back to the
	 * associated pixel in the source image. */

	for (y = 0; y < regionHeight; y++) {
	    double ty;

	    ty = (yScale * (double)(y + regionY)) - roy;
	    for (x = 0; x < regionWidth; x++) {
		double tx, rx, ry;
		int sx, sy;	
		unsigned long pixel;

		/* Translate origin to center of destination image. */
		tx = (xScale * (double)(x + regionX)) - rox;

		/* Rotate the coordinates about the origin. */
		rx = (tx * cosTheta) - (ty * sinTheta);
		ry = (tx * sinTheta) + (ty * cosTheta);

		/* Translate back to the center of the source image. */
		rx += sox;
		ry += soy;

		sx = ROUND(rx);
		sy = ROUND(ry);

		/*
		 * Verify the coordinates, since the destination image can be
		 * bigger than the source.
		 */

		if ((sx >= srcWidth) || (sx < 0) || (sy >= srcHeight) ||
		    (sy < 0)) {
		    continue;
		}
		pixel = XGetPixel(srcImgPtr, sx, sy);
		if (pixel) {
		    XPutPixel(destImgPtr, x, y, pixel);
		}
	    }
	}
    }
    /* Write the rotated image into the destination bitmap. */
    XPutImage(display, destBitmap, bitmapGC, destImgPtr, 0, 0, 0, 0, 
	regionWidth, regionHeight);

    /* Clean up the temporary resources used. */
    XDestroyImage(srcImgPtr), XDestroyImage(destImgPtr);
#endif
    return destBitmap;
}
Exemplo n.º 15
0
static PyObject* cstuff_DirectionMap_fill_near(cstuff_DirectionMap* self, PyObject *args) {
	PyObject *iterator, *item, *obj, *with_value_obj;
	int row, col, row2, col2, stride, pos;
	double value, value2, value3, limit;
	int with_value, parse_result;

	queue_d queue = TAILQ_HEAD_INITIALIZER(queue);
	entry_d *entry, **waiting;

	if (!PyArg_ParseTuple(args, "OdO", &obj, &limit, &with_value_obj)) {
		return NULL;
	}
	with_value = PyObject_IsTrue(with_value_obj) == 1;

	iterator = PyObject_GetIter(obj);
	if (iterator == NULL) {
		return NULL;
	}

	if (!(waiting = (entry_d**)malloc(g_rows*g_cols*sizeof(entry_d*)))) {
		RAISE("Failed to allocate waiting nodes array");
		return NULL;
	}
	memset(waiting, 0, g_rows*g_cols*sizeof(entry_d*));

	value = 0.0;

	while ((item = PyIter_Next(iterator))) {
		if (with_value) {
			parse_result = PyArg_ParseTuple(item, "iid", &row, &col, &value);
		} else {
			parse_result = PyArg_ParseTuple(item, "ii", &row, &col);
		}
		Py_DECREF(item);
		if (!parse_result) {
			Py_DECREF(iterator);
			cleanup_d(&queue);
			free(waiting);
			return NULL;
		}
		if (row >= g_rows || col >= g_cols || row < 0 || col < 0) {
			RAISE("Invalid input data - one of the targets is out od map bounds");
			Py_DECREF(iterator);
			cleanup_d(&queue);
			free(waiting);
			return NULL;
		}
		if (with_value) {
			put_entry_d_sorted(row, col, value, row*g_cols+col, &queue, self, waiting);
		} else {
			put_entry_d(row, col, 0.0, row*g_cols+col, &queue, self, waiting);
		}
	}
	Py_DECREF(iterator);

	if (PyErr_Occurred()) {
		cleanup_d(&queue);
		free(waiting);
		return NULL;
	}
	while (!TAILQ_EMPTY(&queue)) {
		entry = TAILQ_FIRST(&queue);
		TAILQ_REMOVE(&queue, entry, hook);
		TAILQ_INSERT_TAIL(&free_entries_d, entry, hook);

		row = entry->row;
		col = entry->col;
		value = entry->value;
		stride = row*g_cols;
		waiting[stride+col] = NULL;
		value += 1.0;

		if ((limit > 0) && (value > limit)) continue;

		col2 = FMOD((col-1), g_cols);
		pos = stride+col2;
		DO_STUFF_NEAR(row, col2)

		col2 = FMOD((col+1), g_cols);
		pos = stride+col2;
		DO_STUFF_NEAR(row, col2)

		row2 = FMOD((row-1), g_rows);
		pos = row2*g_cols+col;
		DO_STUFF_NEAR(row2, col)

		row2 = FMOD((row+1), g_rows);
		pos = row2*g_cols+col;
		DO_STUFF_NEAR(row2, col)
	}

	cleanup_d(&queue);
	free(waiting);
	Py_RETURN_NONE;
}
Exemplo n.º 16
0
static PyObject* cstuff_DirectionMap_fill(cstuff_DirectionMap* self, PyObject *args) {
	PyObject *iterator, *item, *obj;
	int row, col, row2, col2, stride, pos;
	double value, limit;

	queue_s queue = STAILQ_HEAD_INITIALIZER(queue);
	entry_s *entry;

	if (!PyArg_ParseTuple(args, "Od", &obj, &limit)) {
		return NULL;
	}

	iterator = PyObject_GetIter(obj);
	if (iterator == NULL) {
		return NULL;
	}

	while ((item = PyIter_Next(iterator))) {
		if (!PyArg_ParseTuple(item, "ii", &row, &col)) {
			Py_DECREF(item);
			Py_DECREF(iterator);
			cleanup_s(&queue);
			return NULL;
		}
		if (row >= g_rows || col >= g_cols || row < 0 || col < 0) {
			RAISE("Invalid input data - one of the targets is out od map bounds");
			Py_DECREF(iterator);
			cleanup_s(&queue);
			return NULL;
		}
		Py_DECREF(item);
		put_entry_s(row, col, 0.0, row*g_cols+col, &queue, self);
	}
	Py_DECREF(iterator);

	if (PyErr_Occurred()) {
		cleanup_s(&queue);
		return NULL;
	}

	while (!STAILQ_EMPTY(&queue)) {
		entry = STAILQ_FIRST(&queue);
		STAILQ_REMOVE_HEAD(&queue, hook);
		STAILQ_INSERT_TAIL(&free_entries_s, entry, hook);

		row = entry->row;
		col = entry->col;
		value = entry->value;
		stride = row*g_cols;
		value += 1.0;

		if ((limit > 0) && (value > limit)) continue;

		col2 = FMOD((col-1), g_cols);
		pos = stride+col2;
		DO_STUFF(row, col2);

		col2 = FMOD((col+1), g_cols);
		pos = stride+col2;
		DO_STUFF(row, col2);

		row2 = FMOD((row-1), g_rows);
		pos = row2*g_cols+col;
		DO_STUFF(row2, col);

		row2 = FMOD((row+1), g_rows);
		pos = row2*g_cols+col;
		DO_STUFF(row2, col);
	}

	cleanup_s(&queue);
	Py_RETURN_NONE;
}
Exemplo n.º 17
0
/******************************************************************************************
	Process real-time game lights
*******************************************************************************************/
void ProcessRTLights( void )
{
    int j;
    RT_LIGHT *light;
    XLIGHT *xlight;
    RT_FIXED_LIGHT *fixed;
    RT_PULSING_LIGHT *pulse;
    RT_FLICKERING_LIGHT *flicker;
    RT_SPOT_LIGHT *spot;
    float chance;
    MATRIX rotmat;

    for ( j = 0; j < rt_lights; j++ )
    {
        light = &rt_light[ j ];
        if ( light->xlight == (u_int16_t) -1 )
            continue; // only happens if run out of XLIGHTs
        xlight = &XLights[ light->xlight ];
        if ( light->delay > 0.0F )
        {
            light->delay -= framelag;
            if ( light->delay <= 0.0F )
            {
                light->enabled = true;
            }
        }
        if ( light->enabled )
        {
            switch ( light->type )
            {
            case LIGHT_FIXED:
                fixed = &light->fixed;
                switch ( light->state )
                {
                case STATE_OFF:
                    light->now_time = 0.0F;
                    xlight->Visible = false;
                    light->intensity = 0.0F;
                    break;
                case STATE_TURNING_ON:
                    if ( light->delay < 0.0F )
                    {
                        light->now_time += -light->delay;
                        light->delay = 0.0F;
                    }
                    else
                        light->now_time += framelag;
                    xlight->Visible = true;
                    if ( light->now_time < fixed->on_time )
                    {
                        InterpLightOn( light, light->now_time / fixed->on_time, fixed->on_type );
                    }
                    else
                    {
                        xlight->r = light->r;
                        xlight->g = light->g;
                        xlight->b = light->b;
                        xlight->Visible = true;
                        light->state = STATE_ON;
                    }
                    break;
                case STATE_ON:
                    xlight->r = light->r;
                    xlight->g = light->g;
                    xlight->b = light->b;
                    xlight->Visible = true;
                    break;
                case STATE_TURNING_OFF:
                    light->now_time += framelag;
                    xlight->Visible = true;
                    if ( light->now_time < fixed->off_time )
                    {
                        InterpLightOff( light, light->now_time / fixed->off_time, fixed->off_type );
                    }
                    else
                    {
                        light->state = STATE_OFF;
                        light->now_time = 0.0F;
                        xlight->Visible = false;
                    }
                    break;
                }
                break;
            case LIGHT_PULSING:
                pulse = &light->pulse;
                if ( light->delay < 0.0F )
                {
                    light->now_time += -light->delay;
                    light->delay = 0.0F;
                }
                else
                    light->now_time += framelag;
                if ( light->now_time > pulse->total_time )
                {
                    light->now_time = FMOD( light->now_time, pulse->total_time );
                }
                if ( light->now_time < pulse->on_time )
                {
                    // light is turning on
                    light->state = STATE_TURNING_ON;
                    InterpLightOn( light, light->now_time / pulse->on_time, pulse->type );
                    xlight->Visible = true;
                }
                else if ( light->now_time < pulse->stay_on_point )
                {
                    // light is staying on
                    light->state = STATE_ON;
                    xlight->r = light->r;
                    xlight->g = light->g;
                    xlight->b = light->b;
                    xlight->Visible = true;
                }
                else if ( light->now_time < pulse->off_point )
                {
                    // light is turning off
                    light->state = STATE_TURNING_OFF;
                    InterpLightOff( light, ( light->now_time - pulse->stay_on_point ) / pulse->off_time, pulse->type );
                    xlight->Visible = true;
                }
                else // light->now_time < pulse->total_time
                {
                    // light is staying off
                    light->state = STATE_OFF;
                    xlight->Visible = false;
                }
                break;
            case LIGHT_FLICKERING:
                flicker = &light->flicker;
                chance = RANDOM();
                if ( light->delay < 0.0F )
                {
                    light->now_time += -light->delay;
                    light->delay = 0.0F;
                }
                else
                    light->now_time += framelag;
                if ( light->state == STATE_ON )
                {
                    // check chance of switching off
                    if ( light->now_time > flicker->stay_on_time &&
                            chance > flicker->stay_on_chance )
                    {
                        light->state = STATE_OFF;
                        light->now_time = 0.0F;
                        xlight->Visible = false;
                    }
                }
                else // light is off
                {
                    // check chance of switching on
                    if ( light->now_time > flicker->stay_off_time &&
                            chance > flicker->stay_off_chance )
                    {
                        light->state = STATE_ON;
                        light->now_time = 0.0F;
                        xlight->Visible = true;
                    }
                }
                break;
            case LIGHT_SPOT:
                spot = &light->spot;
                if ( spot->rotation_speed )
                {
                    // rotate spotlight beam
                    if ( light->delay < 0.0F )
                    {
                        light->now_time += -light->delay;
                        light->delay = 0.0F;
                    }
                    else
                        light->now_time += framelag;
                    spot->angle = light->now_time * spot->rotation_speed;
                    if ( spot->angle > TWO_PI )
                        spot->angle = FMOD( spot->angle, TWO_PI );
                    MatrixFromAxisAndAngle( spot->angle, &spot->up, &rotmat );
                    ApplyMatrix( &rotmat, &spot->dir, &xlight->Dir );
                    NormaliseVector( &xlight->Dir );
                }
                light->state = STATE_ON;
                xlight->Visible = true;
                break;
            }
        }
        else
        {
            // light is disabled, check if turning off
            if ( light->state == STATE_TURNING_OFF )
            {
                switch ( light->type )
                {
                case LIGHT_FIXED:
                    fixed = &light->fixed;
                    light->now_time += framelag;
                    if ( light->now_time < fixed->off_time )
                    {
                        InterpLightOff( light, light->now_time / fixed->off_time, fixed->off_type );
                        xlight->Visible = true;
                    }
                    else
                    {
                        light->state = STATE_OFF;
                        light->now_time = 0.0F;
                        xlight->Visible = false;
                    }
                    break;
                case LIGHT_PULSING:
                    pulse = &light->pulse;
                    light->now_time += framelag;
                    if ( light->now_time < pulse->off_time )
                    {
                        InterpLightOff( light, light->now_time / pulse->off_time, pulse->type );
                        xlight->Visible = true;
                    }
                    else
                    {
                        light->state = STATE_OFF;
                        light->now_time = 0.0F;
                        xlight->Visible = false;
                    }
                    break;
                case LIGHT_FLICKERING:
                    light->state = STATE_OFF;
                    xlight->Visible = false;
                    break;
                case LIGHT_SPOT:
                    light->state = STATE_OFF;
                    xlight->Visible = false;
                    break;
                }
            }
            else if ( light->delay <= 0.0F )
            {
                light->state = STATE_OFF;
                xlight->Visible = false;
            }
        }
    }
}
Exemplo n.º 18
0
static int print_f(void (*printchar_handler)(struct printchar_handler_data *d, int c),
		struct printchar_handler_data *printchar_data, long double r, int width,
		int precision, unsigned int ops, int base, int with_exp, int is_shortened) {
	char buff[PRINT_F_BUFF_SZ], *str, *end, *prefix, *postfix;
	DOUBLE ip, fp, ep;
	int pc, i, ch, len, prefix_len, postfix_len, pad_count, sign_count, zero_left, letter_base;

	assert(printchar_handler != NULL);
	assert(width >= 0);
	assert(precision >= 0);

	postfix = end = str = &buff[0] + sizeof buff / sizeof buff[0] - 1;
	*end = '\0';
	prefix = signbit(r) ? (r = -r, base == 16)
				? ops & OPS_SPEC_UPPER_CASE ? "-0X" : "-0x"
				: "-"
			: ops & OPS_FLAG_WITH_SIGN ? base == 16
				? ops & OPS_SPEC_UPPER_CASE ? "+0X" : "+0x"
				: "+"
			: ops & OPS_FLAG_EXTRA_SPACE ? base == 16
				? ops & OPS_SPEC_UPPER_CASE ? " 0X" : " 0x"
				: " "
			: base == 16 ? ops & OPS_SPEC_UPPER_CASE ? "0X" : "0x"
			: "";
	sign_count = i = pc = 0;
	prefix_len = strlen(prefix);
	letter_base = ops & OPS_SPEC_UPPER_CASE ? 'A' : 'a';
	precision = ops & OPS_PREC_IS_GIVEN ? is_shortened ?
				max(precision, 1) : precision
			: base == 16 ? 12 : PRINT_F_PREC_DEFAULT;

	fp = MODF(r, &ip);
	if (with_exp || is_shortened) {
		ep = 0.0L;
		while (ip >= base) fp = MODF((ip + fp) / base, &ip), ep += 1.0L;
		if (fp != 0.0L) while (ip == 0.0L) fp = MODF((ip + fp) * base, &ip), ep -= 1.0L;
		if ((ep < -4) || (ep >= precision)) with_exp = 1;
	}
	fp = with_exp ? fp : MODF(r, &ip);
	precision -= is_shortened ? ceill(LOG10(ip)) + (ip != 0.0L) : 0;
	assert(precision >= 0);
	for (; (sign_count < precision) && (FMOD(fp, 1.0L) != 0.0L); ++sign_count) fp *= base;
	fp = roundl(fp);
	ip = precision ? fp != POW(base, sign_count)
			? ip : ip + 1.0L : roundl(ip + fp);
	fp = fp != POW(base, sign_count) ? fp : 0.0L;
	if (with_exp && (ip >= base)) fp = MODF((ip + fp) / base, &ip), ep += 1.0L;

	if (with_exp) {
		do {
			ch = FMOD(FABS(ep), base);
			assert((ch >= 0) && (ch < base));
			if (ch >= 10) ch += letter_base - 10 - '0';
			*--postfix = ch + '0';
			MODF(ep / base, &ep);
		} while (ep != 0.0L);
		if ((strlen(postfix) == 1) && (base != 16)) *--postfix = '0';
		*--postfix = signbit(ep) ? '-' : '+';
		*--postfix = base == 16 ? ops & OPS_SPEC_UPPER_CASE ?
					'P' : 'p'
				: ops & OPS_SPEC_UPPER_CASE ? 'E' : 'e';
		str = end = postfix - 1;
		*end = '\0';
	}

	for (; i < sign_count; ++i) {
		ch = FMOD(fp, base);
		assert((ch >= 0) && (ch < base));
		if (ch >= 10) ch += letter_base - 10 - '0';
		*--str = ch + '0';
		MODF(fp / base, &fp);
	}

	if ((precision && !is_shortened) || sign_count
			|| (ops & OPS_FLAG_WITH_SPEC)) {
		*--str = '.';
	}

	do {
		ch = (int)FMOD(ip, (long double)base);
		assert((ch >= 0) && (ch < base));
		if (ch >= 10) ch += letter_base - 10 - '0';
		*--str = ch + '0';
		MODF(ip / base, &ip);
	} while (ip != 0.0L);

	len = end - str;
	postfix_len = strlen(postfix);
	zero_left = is_shortened ? 0 : precision - sign_count;
	pad_count = max(width - prefix_len - len - zero_left - postfix_len, 0);

	if (!(ops & (OPS_FLAG_ZERO_PAD | OPS_FLAG_LEFT_ALIGN))) {
		pc += pad_count;
		while (pad_count--) printchar_handler(printchar_data, ' ');
	}

	pc += prefix_len;
	while (prefix_len--) printchar_handler(printchar_data, *prefix++);

	if (ops & OPS_FLAG_ZERO_PAD) {
		pc += pad_count;
		while (pad_count--) printchar_handler(printchar_data, '0');
	}

	pc += len;
	while (len--) printchar_handler(printchar_data, *str++);

	pc += zero_left;
	while (zero_left--) printchar_handler(printchar_data, '0');

	pc += postfix_len;
	while (postfix_len--) printchar_handler(printchar_data, *postfix++);

	if (ops & OPS_FLAG_LEFT_ALIGN) {
		pc += pad_count;
		while (pad_count--) printchar_handler(printchar_data, ' ');
	}

	return pc;
}