⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ltkimagewriter.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 3 页
字号:
*		 red,green,blue RGB values of the color of the point
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void LTKImageWriter::drawPoint(int x,int y,unsigned char red,unsigned char green,unsigned char blue){
		
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::drawPoint()" << endl;


		m_pixels[(3*((x)+y*m_width))]=red;
		m_pixels[(3*((x)+y*m_width))+1]=green;
		m_pixels[(3*((x)+y*m_width))+2]=blue;

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::drawPoint()" << endl;
	}




/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: drawLine
* DESCRIPTION	: The function draws line between the specified end points.
* ARGUMENTS		:
*		x1,y1,x2,y2 coordinates of the end points of the line to be drawn
*		red,green,blue RGB values of the color of the line
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void LTKImageWriter::drawLine(int x1, int y1, int x2, int y2,unsigned char red,unsigned char green,unsigned char blue)
	{
	
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
	  " Entering: LTKImageWriter::drawLine()" << endl;

	  int x, y;
	  double k, s;

	  if (y1==y2) {
		if (x1>x2)
		 swap(x1,x2);
		for (x=x1; x<=x2; x++)
		  drawPoint(x, y1,red,green,blue);
	  } else {
		k = (double)(y2-y1)/(x2-x1);
		if (-1<=k && k<=1) {
		  if (x1>x2) {
		swap(x1, x2);
		swap(y1, y2);
		  }
		  for (x=x1, s=y1; x<=x2; x++, s+=k)
		drawPoint(x, (int)(s+.5),red,green,blue);
		} else {
		  if (y1>y2) {
		swap(x1, x2);
		swap(y1, y2);
		  }
		  k = 1/k;
		  for (s=x1, y=y1; y<=y2; s+=k, y++)
		drawPoint((int)(s+.5), y,red,green,blue);
		}
	  }

	  LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting:LTKImageWriter::drawLine()" << endl;
	}




/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: drawRectangle
* DESCRIPTION	: The function draws hollow rectange with specified color and diagonal end points.
* ARGUMENTS		:
*		x1,y1,x2,y2 coordinates of the end points of the diagonal of the rectangle
*		red,green,blue RGB values of the color of the lines
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void LTKImageWriter::drawRectangle(int x1,int y1,int x2,int y2,unsigned char red,unsigned char green,unsigned char blue)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::drawRectangle()" << endl;


		int xmin,ymin,xmax,ymax;

		if(x1<x2){

			xmin=x1;xmax=x2;
		}
		else{
			xmin=x2;xmax=x1;
		}

		if(y1<y2){

			ymin=y1;ymax=y2;
		}
		else{
			ymin=y2;ymax=y1;
		}

		drawLine(xmin,ymin,xmin,ymax,red,green,blue);
		drawLine(xmin,ymin,xmax,ymin,red,green,blue);
		drawLine(xmin,ymax,xmax,ymax,red,green,blue);
		drawLine(xmax,ymin,xmax,ymax,red,green,blue);

		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::drawRectangle()" << endl;
	}




/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: drawRectangle
* DESCRIPTION	: The function draws a rectange with specified diagonal end points and fills it with the specified color.
* ARGUMENTS		:
*		x1,y1,x2,y2 coordinates of the end points of the diagonal of the rectangle
*		red,green,blue RGB values of the color of the lines
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
	void LTKImageWriter::fillRectangle(int x1,int y1,int x2,int y2,unsigned char red,unsigned char green,unsigned char blue)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::fillRectangle()" << endl;

		int xmin,ymin,xmax,ymax;

		if(x1<x2){

			xmin=x1;xmax=x2;
		}
		else{
			xmin=x2;xmax=x1;
		}

		if(y1<y2){

			ymin=y1;ymax=y2;
		}
		else{
			ymin=y2;ymax=y1;
		}


		for(int i=xmin;i<=xmax;i++)
		{
			drawLine(i,ymin,i,ymax,red,green,blue);
		}

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		 " Exiting: LTKImageWriter::fillRectangle()" << endl;
	}



/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: createTraceOrderInTraceGroup
* DESCRIPTION	: The function offsets the input trace group by specified offset value and in the original trace order.
* ARGUMENTS		:
*		traceGroup input trace group
*		offsetTraceGroup trace group with offset
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void  LTKImageWriter::createTraceOrderInTraceGroup(const LTKTraceGroup& traceGroup,LTKTraceGroup& offsetTraceGroup)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::createTraceOrderInTraceGroup()" << endl;


		float maxOfX=0.0;
		LTKTrace tempTrace;
		traceGroup.getTraceAt(0, tempTrace);

		findMaxXOfTrace(tempTrace,maxOfX);

		maxOfX += m_offset;


		offsetTraceGroup.addTrace(tempTrace);

		for(int i = 1 ; i < traceGroup.getNumTraces(); i++)
		{
			LTKTrace trace;
			traceGroup.getTraceAt(i, trace);

			float minOfTrace =0;

			findMinXOfTrace(trace,minOfTrace);

			LTKTrace offsetTrace=trace;
			int pointToShift=0;
			vector<float> newXChannel;
			for(int j=0;j<trace.getNumberOfPoints();j++){
				vector<float> point;

				trace.getPointAt(j, point);
				point[0]+=fabs(maxOfX-minOfTrace);
				newXChannel.push_back(point[0]);
			}

			offsetTrace.reassignChannelValues(xChannelstr,newXChannel);
			offsetTraceGroup.addTrace(offsetTrace);

			findMaxXOfTrace(offsetTrace,maxOfX);
			maxOfX += m_offset;

		}
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::createTraceOrderInTraceGroup()" << endl;
	}


/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: setColor
* DESCRIPTION	: Setter method for color of the starting stroke and subsequent alternate strokes
* ARGUMENTS		:
*		red value of Red in RGB combination
*		green value of Green in RGB combination
*		blue value of Blue in RGB combination
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void LTKImageWriter::setColor(unsigned char red,unsigned char green,unsigned char blue)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::setColor()" << endl;

         m_red=red;
         m_green=green;
         m_blue=blue;

		 LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::setColor()" << endl;
    }


/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: setAlternateColor
* DESCRIPTION	: Setter method for color of alternate strokes
* ARGUMENTS		:
*		altRed value of Red in RGB combination
*		altGreen value of Green in RGB combination
*		altBlue value of Blue in RGB combination
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
	void LTKImageWriter::setAlternateColor(unsigned char altRed,unsigned char altGreen,unsigned char altBlue)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::setAlternateColor()" << endl;

         m_altRed=altRed;
         m_altGreen=altGreen;
         m_altBlue=altBlue;

		 LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::setAlternateColor()" << endl;
    }


/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 22-FEB-2005
* NAME			: setOffstet
* DESCRIPTION	: Setter method for offset value between strokes
* ARGUMENTS		:
*		offset value of offset
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/

	void LTKImageWriter::setOffset(int offset)
    {
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Entering: LTKImageWriter::setOffset()" << endl;

		if(offset < 0)
		{
			m_offset=0;
		}
		else
		{
			m_offset=offset;
		}
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::setOffset()" << endl;
    }



/**********************************************************************************
* AUTHOR		: Bharath A
* DATE			: 19-MAY-2005
* NAME			: drawBMPImage
* DESCRIPTION	: The function writes the given pixel array to the specified BMP file
* ARGUMENTS		:
*			fileName name of output bmp file name with extension as 'bmp'
*			pixelArray pixel array
*			width width of the image
*			height height of the image
*
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
	void LTKImageWriter::drawBMPImage(string fileName,unsigned char* pixelArray,int width,int height)
	{
			LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
			  " Entering: LTKImageWriter::drawBMPImage()" << endl;

			short type;
			int size;
			short reserved1;
			short reserved2;
			int offset;
			int i,j;
			int biSize,biWidth,biHeight;
			short biPlanes, biBitCount;
			int biCompression,biSizeImage,biXPelsPerMeter,biYPelsPerMeter,biClrUsed,biClrImportant;
			FILE *bmp = fopen(fileName.c_str(),"wb");

			offset = 54;
			biSize = 40;

			biWidth=width;
			biHeight=height;
			biPlanes = 1;
			biBitCount = 24;
			biCompression = 0;
			biSizeImage =0;
			biXPelsPerMeter = 0;
			biYPelsPerMeter = 0;
			biClrUsed = 0;
			biClrImportant = 0;
			type = 19778;

			int padding = ( 4 - ( ( 3 * width ) % 4 ) ) % 4;

			size = 54 + ( ( 3 * width ) + padding ) *  height;
			reserved1 = 0;
			reserved2 = 0;
			fwrite(&type,sizeof(short),1,bmp);
			fwrite(&size,sizeof(int),1,bmp);
			fwrite(&reserved1,sizeof(short),1,bmp);
			fwrite(&reserved1,sizeof(short),1,bmp);
			fwrite(&offset,sizeof(int),1,bmp);
			fwrite(&biSize,sizeof(int),1,bmp);
			fwrite(&biWidth,sizeof(int),1,bmp);
			fwrite(&biHeight,sizeof(int),1,bmp);
			fwrite(&biPlanes,sizeof(short),1,bmp);
			fwrite(&biBitCount,sizeof(short),1,bmp);
			fwrite(&biCompression,sizeof(int),1,bmp);
			fwrite(&biSizeImage,sizeof(int),1,bmp);
			fwrite(&biXPelsPerMeter,sizeof(int),1,bmp);
			fwrite(&biYPelsPerMeter,sizeof(int),1,bmp);
			fwrite(&biClrUsed,sizeof(int),1,bmp);
			fwrite(&biClrImportant,sizeof(int),1,bmp);

			unsigned char zeroValue=0x00;
			//if(height%4 ==0) ++height;
			for ( i = height-1; i >=0 ; --i )
			{
				for ( j = 0; j < width ; ++j )
				{

				  fwrite(&m_pixels[(3*((i*m_width)+j))+2],sizeof(char),1,bmp);
				  fwrite(&m_pixels[(3*((i*m_width)+j))+1],sizeof(char),1,bmp);
				  fwrite(&m_pixels[(3*((i*m_width)+j))],sizeof(char),1,bmp);

				}


				for ( int k = 0; k < padding; k++ )
				{

				 fwrite(&zeroValue,sizeof(char),1,bmp);
				}
			}

			fclose(bmp);
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
		  " Exiting: LTKImageWriter::drawBMPImage()" << endl;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -