📄 ltkimagewriter.cpp
字号:
/*****************************************************************************************
* Copyright (c) 2007 Hewlett-Packard Development Company, L.P.
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************************/
/************************************************************************
* SVN MACROS
*
* $LastChangedDate: 2007-10-08 22:10:54 +0530 (Mon, 08 Oct 2007) $
* $Revision: 252 $
* $Author: bharatha $
*
************************************************************************/
/************************************************************************
* FILE DESCR: Definitions of Image Writer module
*
* CONTENTS:
* drawLTKTraceGroupToImage
* drawRawInkFileToImage
* drawUnipenFileToImage
* drawUnipenFileToImageWithBB
* showStartingPoint
* setColor
* setAlternateColor
* setOffstet
* normalizeSize
* getBoundingBox
* findMinXOfTrace
* findMaxXOfTrace
* drawPoint
* drawLine
* drawRectangle
* fillRectangle
* createTraceOrderInTraceGroup
*
* AUTHOR: Bharath A
*
* DATE: February 22, 2005
* CHANGE HISTORY:
* Author Date Description of change
************************************************************************/
#include "LTKImageWriter.h"
#include "LTKChannel.h"
#include "LTKTraceFormat.h"
#include "LTKTrace.h"
#include "LTKTraceGroup.h"
#include "LTKInkFileReader.h"
#include "LTKCaptureDevice.h"
#include "LTKPreprocessorInterface.h"
#include "LTKErrors.h"
#include "LTKErrorsList.h"
#include "LTKScreenContext.h"
#include "LTKLoggerUtil.h"
#include "LTKException.h"
using namespace std;
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : LTKImageWriter
* DESCRIPTION : Default Constructor
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
LTKImageWriter::LTKImageWriter():
m_width(0),
m_height(0),
m_pixels(NULL),
m_showBB(false),
m_showSP(false),
m_red(255),
m_green(0),
m_blue(0),
m_altRed(0),
m_altGreen(0),
m_altBlue(255),
m_offset(0)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
"Entering: LTKImageWriter::LTKImageWriter()" <<endl;
xChannelstr = "X";
yChannelstr = "Y";
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
"Exiting: LTKImageWriter::LTKImageWriter()" <<endl;
}
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : ~LTKImageWriter
* DESCRIPTION : Destructor deletes the pixels array
* ARGUMENTS :
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
LTKImageWriter::~LTKImageWriter()
{
if(m_pixels!=NULL){
delete [] m_pixels;
}
}
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : drawLTKTraceGroupToImage
* DESCRIPTION : The function draws LTKTraceGroup to image file with specified width,height,color and offset.
If offset not equal to zero, the image would be in the trace order.
* ARGUMENTS :
* traceGroup the trace group that is to be drawn
* imgFileName name of the file that is to be created to draw the image
* imgWidth width of the image
* imgHeight height of the image
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup& traceGroup,
const string imgFileName,int imgWidth,int imgHeight)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKImageWriter::drawLTKTraceGroupToImage()" << endl;
m_width=imgWidth+5;
m_height=imgHeight+5;
delete [] m_pixels;
m_pixels=new unsigned char[3*m_width*m_height];
for(int c=0;c<(3*m_width*m_height);c++)
{
m_pixels[c]=0xff;
}
LTKTraceGroup normTraceGroup;
if(m_offset)
{
createTraceOrderInTraceGroup(traceGroup,normTraceGroup);
}
else
{
normTraceGroup=traceGroup;
}
normTraceGroup.getBoundingBox(m_xMin,m_yMin,m_xMax,m_yMax);
float newXScaleFactor = (float) imgWidth/((float)(m_xMax-m_xMin)/normTraceGroup.getXScaleFactor());
float newYScaleFactor = (float)imgHeight / (float) ((m_yMax-m_yMin)/normTraceGroup.getYScaleFactor());
float origin = 0;
normTraceGroup.affineTransform(newXScaleFactor,newYScaleFactor,origin,origin,XMIN_YMIN);
int numOfTracesToDraw=normTraceGroup.getNumTraces();
if(m_showBB)
{
numOfTracesToDraw = normTraceGroup.getNumTraces() - 1;
LTKTrace trace ;
normTraceGroup.getTraceAt(numOfTracesToDraw, trace);
for(int j=1; j< trace.getNumberOfPoints(); j++)
{
floatVector fromPoint, toPoint;
trace.getPointAt(j-1, fromPoint);
trace.getPointAt(j, toPoint);
drawLine((int)fromPoint[0]+2, (int)fromPoint[1]+2,
(int)toPoint[0]+2, (int)toPoint[1]+2, 255, 255, 0);
}
}
for(int i=0;i<numOfTracesToDraw;i++)
{
LTKTrace trace;
normTraceGroup.getTraceAt(i, trace);
for(int j=1 ; j < trace.getNumberOfPoints() ; j++)
{
floatVector fromPoint, toPoint;
trace.getPointAt(j-1, fromPoint);
trace.getPointAt(j, toPoint);
if((i%2)==0)
{
if(j == 1)
{
if(m_showSP)
{
drawRectangle((int)fromPoint[0]+2-2,(int)fromPoint[1]+2+2,(int)fromPoint[0]+2+2,(int)fromPoint[1]+2-2,0,0,0);
}
}
drawLine((int)fromPoint[0]+2,(int)fromPoint[1]+2,(int)toPoint[0]+2,(int)toPoint[1]+2,m_red,m_green,m_blue);
}
else
{
if(j==1)
{
if(m_showSP)
{
drawRectangle((int)fromPoint[0]+2-2,(int)fromPoint[1]+2+2,(int)fromPoint[0]+2+2,(int)fromPoint[1]+2-2,0,0,0);
}
}
drawLine((int)fromPoint[0]+2,(int)fromPoint[1]+2,(int)toPoint[0]+2,(int)toPoint[1]+2,m_altRed,m_altGreen,m_altBlue);
}
}
}
drawBMPImage(imgFileName,m_pixels,m_width,m_height);
delete [] m_pixels;
m_pixels=NULL;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKImageWriter::drawLTKTraceGroupToImage()" << endl;
}
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : drawLTKTraceGroupToImage
* DESCRIPTION : The function draws LTKTraceGroup to image file with specified color and offset. The width and height would be determined from the trace group. If offset not equal to zero, the image would be in the trace order.
* ARGUMENTS :
* traceGroup the trace group that is to be drawn
* imgFileName name of the file that is to be created to draw the image
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup& traceGroup,const string imgFileName)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup&,const string)" << endl;
traceGroup.getBoundingBox(m_xMin,m_yMin,m_xMax,m_yMax);
drawLTKTraceGroupToImage(traceGroup,imgFileName,(int)(m_xMax-m_xMin),(int)(m_yMax-m_yMin));
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup&,const string)" << endl;
}
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : drawLTKTraceGroupToImage
* DESCRIPTION : The function draws LTKTraceGroup to image file with specified color and offset. The size specified is the bound on larger dimension and the trace group is normalized to maintain the aspect ratio.
* ARGUMENTS :
* traceGroup the trace group that is to be drawn
* imgFileName name of the file that is to be created to draw the image
* size bound on larger dimension
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup& traceGroup,
const string imgFileName, int size)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup&,const string,size)" << endl;
LTKTraceGroup normTraceGroup = traceGroup;
normTraceGroup.getBoundingBox(m_xMin,m_yMin,m_xMax,m_yMax);
float xScale = ((float)fabs(m_xMax - m_xMin))/normTraceGroup.getXScaleFactor();
float yScale = ((float)fabs(m_yMax - m_yMin))/normTraceGroup.getYScaleFactor();
if(yScale > xScale)
{
xScale = yScale;
}
else
{
yScale = xScale;
}
float xScaleFactor = size / xScale ;
float yScaleFactor = size / yScale;
float origin = 0.0;
normTraceGroup.affineTransform(xScaleFactor,yScaleFactor,origin,origin,XMIN_YMIN);
drawLTKTraceGroupToImage(normTraceGroup,imgFileName);
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKImageWriter::drawLTKTraceGroupToImage(const LTKTraceGroup&,const string,size)" << endl;
}
/**********************************************************************************
* AUTHOR : Bharath A
* DATE : 22-FEB-2005
* NAME : drawLTKTraceGroupToImage
* DESCRIPTION : The function draws LTKTraceGroup to image file with specified color and offset. The size specified is the bound on larger dimension and the trace group is normalized to maintain the aspect ratio.The bounding box of the image is also drawn to show relative position.
* ARGUMENTS :
* traceGroup the trace group that is to be drawn
* screenContext screenContext the reference to screen Context for determining the bounding box
* imgFileName name of the file that is to be created to draw the image
* size bound on larger dimension
*
* RETURNS :
* NOTES :
* CHANGE HISTROY
* Author Date Description of change
*************************************************************************************/
void LTKImageWriter::drawLTKTraceGroupToImageWithBB(const LTKTraceGroup& traceGroup,const LTKScreenContext& screenContext,
const string imgFileName,int size)
{
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Entering: LTKImageWriter::drawLTKTraceGroupToImageWithBB()" << endl;
LTKTraceGroup newTG;
newTG=traceGroup;
float x1=screenContext.getBboxLeft();
float y1=screenContext.getBboxBottom();
float x2=screenContext.getBboxRight();
float y2=screenContext.getBboxTop();
vector<LTKChannel> channels;
LTKChannel xChannel("X", DT_FLOAT, true);
LTKChannel yChannel("Y", DT_FLOAT, true);
channels.push_back(xChannel);
channels.push_back(yChannel);
LTKTraceFormat traceFormat(channels);
LTKTrace trace(traceFormat);
vector<float> point1;
point1.push_back(x1);
point1.push_back(y1);
trace.addPoint(point1);
vector<float> point2;
point2.push_back(x2);
point2.push_back(y1);
trace.addPoint(point2);
vector<float> point3;
point3.push_back(x2);
point3.push_back(y2);
trace.addPoint(point3);
vector<float> point4;
point4.push_back(x1);
point4.push_back(y2);
trace.addPoint(point4);
vector<float> point5;
point5.push_back(x1);
point5.push_back(y1);
trace.addPoint(point5);
newTG.addTrace(trace);
m_showBB=true;
drawLTKTraceGroupToImage(newTG,imgFileName,size);
m_showBB=false;
LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
" Exiting: LTKImageWriter::drawLTKTraceGroupToImageWithBB()" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -