text2image.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "Component/ModelManager.H"
00038 #include "Component/ComponentOpts.H"
00039 #include "Image/Image.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/Pixels.H"
00042 #include "Image/SimpleFont.H"
00043 #include "Util/Types.H"
00044 #include "Util/StringConversions.H"
00045 #include "Util/StringUtil.H"
00046 #include "Media/FrameSeries.H"
00047 #include "Transport/FrameInfo.H"
00048
00049 #include <fstream>
00050
00051 #define HDEG 54.9
00052
00053 extern "C" int main(const int argc, char** argv)
00054 {
00055 MYLOGVERB = LOG_INFO;
00056
00057
00058 ModelManager manager("Create Images");
00059
00060 nub::soft_ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00061 manager.addSubComponent(ofs);
00062
00063
00064 if (manager.parseCommandLine(argc, argv,
00065 "<textfile> visual-angle-of-single-character",
00066 1, 2)==false)
00067 return(1);
00068
00069 double fontsize = fromStr<double>(manager.getExtraArg(1));
00070
00071
00072 manager.start();
00073
00074
00075
00076
00077
00078
00079
00080 std::ifstream *itsFile;
00081 itsFile = new std::ifstream(manager.getExtraArg(0).c_str());
00082
00083
00084 if (itsFile->is_open() == false)
00085 LFATAL("Cannot open '%s' for reading",manager.getExtraArg(0).c_str());
00086
00087
00088 std::string line;
00089 std::vector<std::vector<std::string> > lines;
00090 std::vector<uint> itsType;
00091 uint scount = 0;
00092
00093
00094 while (!itsFile->eof())
00095 {
00096 getline(*itsFile, line);
00097
00098 std::vector<std::string> temp;
00099
00100 if (line[0] == '#')
00101 {
00102 line.erase(0,1);
00103 temp.push_back(line);
00104 lines.push_back(temp);
00105 itsType.push_back(1);
00106 scount++;
00107 }
00108 else if (line[0] =='!')
00109 {
00110 line.erase(0,1);
00111 temp.push_back(line);
00112 lines.push_back(temp);
00113 itsType.push_back(0);
00114 scount++;
00115 }
00116 else
00117 {
00118 if (line.size() > 1)
00119 {
00120 scount--;
00121 lines[scount].push_back(line);
00122 scount++;
00123 }
00124 }
00125 }
00126 itsFile->close();
00127
00128
00129 int w = 1920;
00130 int h = 1080;
00131 uint fontwidth = uint(fontsize * w / HDEG);
00132 SimpleFont fnt = SimpleFont::fixedMaxWidth(fontwidth);
00133 std::vector<Image<PixRGB<byte> > > itsImage;
00134
00135 for (uint i = 0; i < lines.size(); i++)
00136 {
00137 int space = 0;
00138 int hanchor = int(h/2) - int(fnt.h()/2);
00139 Image<PixRGB<byte> > timage(w,h,ZEROS);
00140 PixRGB<byte> gr(128,128,128);
00141 timage += gr;
00142
00143 for (uint j = 0; j < lines[i].size(); j++)
00144 {
00145 if (j < 1)
00146 space = int( double(w - fnt.w() * lines[i][j].size()) / 2.0 );
00147 if (j > 0)
00148 hanchor = hanchor + fnt.h();
00149 Point2D<int> tanchor(space, hanchor);
00150 writeText(timage,tanchor,lines[i][j].c_str(),
00151 PixRGB<byte>(0,0,0),
00152 gr,
00153 fnt);
00154 }
00155
00156 itsImage.push_back(timage);
00157 }
00158
00159
00160
00161 for (uint i = 0; i < itsImage.size(); i ++)
00162 {
00163
00164
00165 if (ofs->becameVoid())
00166 {
00167 LINFO("quitting because output stream was closed or became void");
00168 return 0;
00169 }
00170
00171
00172 const FrameState os = ofs->updateNext();
00173
00174
00175 ofs->writeRGB(itsImage[i], "output",
00176 FrameInfo("Text imbeded image",SRC_POS));
00177
00178
00179 if (os == FRAME_FINAL)
00180 break;
00181
00182 }
00183
00184
00185
00186 manager.stop();
00187
00188
00189 return 0;
00190 }
00191
00192
00193
00194
00195
00196