예제 #1
0
파일: cgi.c 프로젝트: odi2/test
/*********************************************************************//*!
 * @brief Query the current state of the application and see what else
 * we need to get from it
 *
 * Depending on the current state of the application, other additional
 * parameters may be queried.
 *
 * @return SUCCESS or an appropriate error code otherwise
 *//*********************************************************************/
static OSC_ERR QueryApp()
{
	OSC_ERR err;
	struct OSC_PICTURE pic;

	/* First, get the current state of the algorithm. */
	err = OscIpcGetParam(cgi.ipcChan, &cgi.appState, GET_APP_STATE, sizeof(struct APPLICATION_STATE));
	if (err != SUCCESS)
	{
		/* This request is defined in all states, and thus must succeed. */
		OscLog(ERROR, "CGI: Error querying application! (%d)\n", err);
		return err;
	}

	switch(cgi.appState.enAppMode)
	{
	case APP_OFF:
		/* Algorithm is off, nothing else to do. */
		break;
	case APP_CAPTURE_ON:
		if (cgi.appState.bNewImageReady)
		{
			/* If there is a new image ready, request it from the application. */
			err = OscIpcGetParam(cgi.ipcChan, cgi.imgBuf, GET_NEW_IMG, OSC_CAM_MAX_IMAGE_WIDTH/2*OSC_CAM_MAX_IMAGE_HEIGHT/2);
			if (err != SUCCESS)
			{
				OscLog(DEBUG, "CGI: Getting new image failed! (%d)\n", err);
				return err;
			}

			/* Write the image to the RAM file system where it can be picked
			 * up by the webserver on request from the browser. */
			pic.width = OSC_CAM_MAX_IMAGE_WIDTH/2;
			pic.height = OSC_CAM_MAX_IMAGE_HEIGHT/2;
			pic.type = OSC_PICTURE_GREYSCALE;
			pic.data = (void*)cgi.imgBuf;

			return OscBmpWrite(&pic, IMG_FN);
		}
		break;
	default:
		OscLog(ERROR, "%s: Invalid application mode (%d)!\n", __func__, cgi.appState.enAppMode);
		break;
	}
	return SUCCESS;
}
예제 #2
0
/*********************************************************************//*!
 * @brief Query the current state of the application and see what else
 * we need to get from it
 *
 * Depending on the current state of the application, other additional
 * parameters may be queried.
 *
 * @return SUCCESS or an appropriate error code otherwise
 *//*********************************************************************/
static OSC_ERR QueryApp()
{
	OSC_ERR err;

	/* First, get the current state of the algorithm. */
	err = OscIpcGetParam(cgi.ipcChan, &cgi.appState, GET_APP_STATE, sizeof(struct APPLICATION_STATE));
	if (err != SUCCESS)
	{
		/* This request is defined in all states, and thus must succeed. */
		OscLog(ERROR, "CGI: Error querying application! (%d)\n", err);
		return err;
	}

	switch(cgi.appState.enAppMode)
	{
	case APP_OFF:
		/* Algorithm is off, nothing else to do. */
		break;
	case APP_CAPTURE_ON:
		if (cgi.appState.bNewImageReady)
		{
			FILE* F;
			uint16 r,c;
			uint8* pData;
			uint32 dataSiz, i;
			uint16 oType;

			/* If there is a new image ready, request it from the application. */
			/* We get TWICE the size of an image because metadata might be available */
			err = OscIpcGetParam(cgi.ipcChan, cgi.imgBuf, GET_NEW_IMG, NUM_COLORS*nc*nr*2);
			if (err != SUCCESS)
			{
				OscLog(DEBUG, "CGI: Getting new image failed! (%d)\n", err);
				return err;
			}
//we have to take care of the different ways gdlib treats gray and color data
#if NUM_COLORS == 1
			//create gd image and ...
			gdImagePtr im_out =  gdImageCreate(nc, nr);
			//initialize with sensor image
			for(r = 0; r < nr; r++)
			{
				//in case the original image should not be modified replace the following loop by the memcpy statement
				//memcpy(im_out->pixels[r], cgi.imgBuf+r*nc, nc*sizeof(uint8));
				for(c = 0; c < nc; c++)
				{
					im_out->pixels[r][c] = (*(cgi.imgBuf+r*nc+c) & 0xfe);//mask out first bit -> only even gray values
				}
			}
			//allocate color palette (255 is red -> we did not change the sensor image!! should rather use a LUT)
			for(c = 0; c < 256; c++)
			{
				if((c%2) && c > 255-2*MAX_NUM_COLORS){
					i = (255-c)/2;
					gdImageColorAllocate (im_out, colorLUT[i][0], colorLUT[i][1], colorLUT[i][2]);
				} else {
					gdImageColorAllocate (im_out, c, c, c);
				}
			}
#else
			//create gd image and ...
			gdImagePtr im_out =  gdImageCreateTrueColor(nc, nr);
			//initialize with sensor image
			for(r = 0; r < nr; r++)
			{
				for(c = 0; c < nc; c++)
				{
					uint8* p = (cgi.imgBuf+r*3*nc+3*c);
					im_out->tpixels[r][c] = gdTrueColor(p[2], p[1], p[0]);
				}
			}


#endif
			//there might be additional data to be written to image
			pData = (uint8*) (cgi.imgBuf+NUM_COLORS*nc*nr);
			memcpy(&dataSiz, pData, sizeof(uint32));
			//OscLog(DEBUG, "received %d number of bytes\n", dataSiz);
			pData += sizeof(uint32);//skip dataSiz
			if(dataSiz)
			{
				i = 0;
				while(i < dataSiz)
				{
					memcpy(&oType, pData+i, sizeof(uint16));
					i += sizeof(uint16);
					switch(oType) {
						case OBJ_LINE:
						{
							struct IMG_LINE imgLine;
							memcpy(&imgLine, pData+i, sizLine);
							i += sizLine;
							//OscLog(DEBUG, "received line (%d,%d)-(%d,%d), color(%d)\n", imgLine.x1, imgLine.y1, imgLine.x2, imgLine.y2, (int) imgLine.color);
							gdImageLine(im_out, imgLine.x1, imgLine.y1, imgLine.x2, imgLine.y2, colorLoolUp(imgLine.color));
							break;
						}
						case OBJ_RECT:
						{
							struct IMG_RECT imgRect;
							memcpy(&imgRect, pData+i, sizRect);
							i += sizRect;
							//OscLog(DEBUG, "received rect (%d,%d)-(%d,%d), %s, color(%d)\n", imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, imgRect.recFill ? "fill" : "not fill", (int) imgRect.color);
							if(imgRect.recFill) {
								gdImageFilledRectangle(im_out, imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, colorLoolUp(imgRect.color));
							} else {
								gdImageRectangle(im_out, imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, colorLoolUp(imgRect.color));
							}
							break;
						}
						case OBJ_STRING:
						{
							gdFontPtr font = gdFontSmall;
							struct IMG_STRING imgString;
							memcpy(&imgString, pData+i, sizString);
							i += sizString;
							//OscLog(DEBUG, "received string (%d,%d), font %d, %s, color(%d)\n", imgString.xPos, imgString.yPos, imgString.font, pData+i, imgString.color);
							switch(imgString.font)
							{
								case GIANT:
									font = gdFontGiant;
									break;
								case LARGE:
									font = gdFontLarge;
									break;
								case MEDIUMBOLD:
									font = gdFontMediumBold;
									break;
								case SMALL:
									font = gdFontSmall;
									break;
								case TINY:
									font = gdFontTiny;
									break;
								default:
									break;//set in definition of font
							}
							gdImageString(im_out, font, imgString.xPos, imgString.yPos, pData+i, colorLoolUp(imgString.color));
							i += imgString.len;
						}
					}
				}
			}

			F = fopen(IMG_FN, "wb");
			//gdImageGif(im_out, F);
			gdImageJpeg(im_out, F, 80);
			fclose(F);
			gdImageDestroy(im_out);

			return SUCCESS;
		}
		break;
	default:
		OscLog(ERROR, "%s: Invalid application mode (%d)!\n", __func__, cgi.appState.enAppMode);
		break;
	}
	return SUCCESS;
}