00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <time.h>
00004
00005 #include <assert.h>
00006 #include <string>
00007
00008 #include <ARToolKitPlus/arBitFieldPattern.h>
00009 #include <ARToolKitPlus/TrackerImpl.h>
00010
00011 #include "PN/ImageTool.h"
00012
00013
00014 PN::Image* createImageFromPattern(ARToolKitPlus::IDPATTERN nPattern);
00015
00016
00017 void
00018 generatePattern(bool nBCH, int nID, ARToolKitPlus::IDPATTERN& nPattern)
00019 {
00020 if(nBCH)
00021 ARToolKitPlus::generatePatternBCH(nID, nPattern);
00022 else
00023 ARToolKitPlus::generatePatternSimple(nID, nPattern);
00024 }
00025
00026
00027 int main(int argc, char **argv)
00028 {
00029 if(argc<3)
00030 {
00031 printf("IdPatGen 1.1\n\n");
00032 printf("IdPatGen [-noborder] [-all] [-board] [id-number] filename\n\n");
00033 printf(" -noborder create no border around the pattern image\n");
00034 printf(" -thinborder creates thin border (1 pixel) around the pattern image\n");
00035 printf(" instead of a normal border (3 pixels)\n");
00036 printf(" -all creates all markers (no id-number required)\n");
00037 printf(" will add id to the filename\n");
00038 printf(" -board creates a large image with all markers\n");
00039 printf(" counts as: first rows, then columns\n");
00040 printf(" -bch creates marker for the BCH system rather than\n");
00041 printf(" the simple (original ARToolKitPlus marker system\n");
00042 printf(" id-number id of the marker to create\n");
00043 printf(" do not apply this option when using board\n");
00044 printf(" filename filename of the marker or board\n");
00045 printf(" do not pass an extension\n");
00046 return 0;
00047 }
00048
00049 bool _border=true, _all=false, _board=false, _thinborder=false, _bch=false;
00050 int idnumber=0, numOpts=argc-1;
00051 std::string basefilename;
00052 char filename[256];
00053 int idMax;
00054
00055 for(int optid=1; optid<numOpts; optid++)
00056 {
00057 std::string opt = argv[optid];
00058
00059 if(opt=="-noborder")
00060 _border = false;
00061 else
00062 if(opt=="-thinborder")
00063 _thinborder = true;
00064 else
00065 if(opt=="-all")
00066 _all = true;
00067 else
00068 if(opt=="-board")
00069 _board = true;
00070 else
00071 if(opt=="-bch")
00072 _bch = true;
00073 else
00074 {
00075 if(!_all && optid==argc-2)
00076 break;
00077
00078 printf("ERROR: unknown parameter '%s'\n", opt.c_str());
00079 return -1;
00080 }
00081 }
00082
00083 idMax = _bch ? ARToolKitPlus::idMaxBCH : ARToolKitPlus::idMax;
00084
00085 printf("Creating marker(s) for %s id-system\n", _bch ? "BCH" : "SIMPLE");
00086
00087 if(!_all)
00088 idnumber = atoi(argv[argc-2]);
00089 basefilename = argv[argc-1];
00090
00091 if(idnumber<0 || idnumber>idMax)
00092 {
00093 printf("ERROR: invalid id-number %d. must be in rage [0,%d]\n", idnumber, idMax);
00094 return -1;
00095 }
00096
00097
00098 int borderWidth = _thinborder ? 1 : 3;
00099 int markerSize = ARToolKitPlus::idPattWidth+2*borderWidth;
00100 PN::Image* markerImg = NULL;
00101 ARToolKitPlus::IDPATTERN pat;
00102
00103
00104 if(_board || _border)
00105 {
00106 markerImg = PN::Image::createFromPixelBuffer(markerSize, markerSize, new unsigned short[markerSize*markerSize], true);
00107 markerImg->clear(0);
00108 }
00109
00110 if(!_board)
00111 {
00112 if(_all)
00113 {
00114 for(int i=0; i<=idMax; i++)
00115 {
00116 generatePattern(_bch, i, pat);
00117
00118 sprintf(filename, "%s_%04d.tga", basefilename.c_str(), i);
00119
00120
00121 PN::Image* patImg = createImageFromPattern(pat);
00122
00123 if(_border)
00124 {
00125 markerImg->drawImage(borderWidth,borderWidth, patImg);
00126 PN::ImageTool::saveAsTGA(markerImg, filename);
00127 }
00128 else
00129 PN::ImageTool::saveAsTGA(patImg, filename);
00130
00131 delete patImg;
00132 }
00133 delete markerImg;
00134 }
00135 else
00136 {
00137 generatePattern(_bch, idnumber, pat);
00138
00139 sprintf(filename, "%s_%04d.tga", basefilename.c_str(), idnumber);
00140
00141
00142 PN::Image* patImg = createImageFromPattern(pat);
00143
00144 if(_border)
00145 {
00146 markerImg->drawImage(borderWidth,borderWidth, patImg);
00147 PN::ImageTool::saveAsTGA(markerImg, filename);
00148 delete markerImg;
00149 }
00150 else
00151 PN::ImageTool::saveAsTGA(patImg, filename);
00152
00153 delete patImg;
00154 }
00155 }
00156 else
00157 {
00158
00159
00160 int imgW = _bch ? 260*4 : 260,
00161 imgH = _bch ? 516*2 : 516;
00162 int markersPerRow = _bch ? 16*4 : 16,
00163 markersRows = _bch ? 32*2 : 32;
00164
00165 PN::Image* board = PN::Image::createFromPixelBuffer(imgW, imgH, new unsigned short[imgW*imgH], true);
00166 board->clear(0xffff);
00167
00168 int x=0,y=0;
00169
00170 for(y=0; y<markersRows; y++)
00171 for(x=0; x<markersPerRow; x++)
00172 {
00173 int id = y*markersPerRow+x;
00174 generatePattern(_bch, id, pat);
00175
00176 PN::Image* pattern = createImageFromPattern(pat);
00177
00178 markerImg->drawImage(borderWidth,borderWidth, pattern);
00179 board->drawImage(4+x*(markerSize+4), 4+y*(markerSize+4), markerImg);
00180
00181 delete pattern;
00182 }
00183
00184 sprintf(filename, "%s.tga", basefilename.c_str());
00185
00186 PN::ImageTool::saveAsTGA(board, filename);
00187 delete markerImg;
00188 }
00189
00190
00191 return 0;
00192 }
00193
00194
00195 PN::Image*
00196 createImageFromPattern(ARToolKitPlus::IDPATTERN nPattern)
00197 {
00198 unsigned short* pixels = new unsigned short[ARToolKitPlus::idPattWidth*ARToolKitPlus::idPattHeight];
00199
00200 for(int i=0; i<ARToolKitPlus::pattBits; i++)
00201 {
00202 if(ARToolKitPlus::isBitSet(nPattern, i))
00203 pixels[ARToolKitPlus::pattBits-1-i] = 0xffff;
00204 else
00205 pixels[ARToolKitPlus::pattBits-1-i] = 0;
00206 }
00207
00208 return PN::Image::createFromPixelBuffer(ARToolKitPlus::idPattWidth, ARToolKitPlus::idPattHeight, pixels, true);
00209 }
00210