/***************************************************************************************** * 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()" < 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 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 point1; point1.push_back(x1); point1.push_back(y1); trace.addPoint(point1); vector point2; point2.push_back(x2); point2.push_back(y1); trace.addPoint(point2); vector point3; point3.push_back(x2); point3.push_back(y2); trace.addPoint(point3); vector point4; point4.push_back(x1); point4.push_back(y2); trace.addPoint(point4); vector 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; } /********************************************************************************** * AUTHOR : Bharath A * DATE : 22-FEB-2005 * NAME : drawRawInkFileToImage * DESCRIPTION : The function creates LTKTraceGroup from the specified data file and draws it 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 : * fileName path to the datafile * 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::drawRawInkFileToImage(const string fileName, const string imgFileName, int imgWidth,int imgHeight) { int errorCode; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKImageWriter::drawRawInkFileToImage(const string, const string,int,int)" << endl; LTKTraceGroup traceGroup; LTKCaptureDevice captureDevice; LTKScreenContext screenContext; string strFileName(fileName); try { LTKInkFileReader::readRawInkFile(strFileName,traceGroup,captureDevice,screenContext); } catch(LTKException e) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EINK_FILE_OPEN <<":"<< getErrorMessage(EINK_FILE_OPEN) <<"LTKImageWriter::drawRawInkFileToImage(const string, const string,int,int)" <::infinity(); for(int i=0;i point; trace.getPointAt(i, point); if(point[0] point; trace.getPointAt(i, point); if(point[0]>maxX){ maxX=point[0]; } } LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKImageWriter::findMaxXOfTrace(const LTKTrace&,float&)" << endl; return SUCCESS; } /********************************************************************************** * AUTHOR : Bharath A * DATE : 22-FEB-2005 * NAME : drawPoint * DESCRIPTION : The function makes entries in the pixels array for the specified x,y and color. * ARGUMENTS : * x,y coordinates of the point to be drawn * 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 newXChannel; for(int j=0;j 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; }