tools/IdPatGen/src/main.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <time.h>
4 //#include <conio.h>
5 #include <assert.h>
6 #include <string>
7 
10 
11 #include "PN/ImageTool.h"
12 
13 
15 
16 
17 void
18 generatePattern(bool nBCH, int nID, ARToolKitPlus::IDPATTERN& nPattern)
19 {
20  if(nBCH)
21  ARToolKitPlus::generatePatternBCH(nID, nPattern);
22  else
24 }
25 
26 
27 int main(int argc, char **argv)
28 {
29  if(argc<3)
30  {
31  printf("IdPatGen 1.1\n\n");
32  printf("IdPatGen [-noborder] [-all] [-board] [id-number] filename\n\n");
33  printf(" -noborder create no border around the pattern image\n");
34  printf(" -thinborder creates thin border (1 pixel) around the pattern image\n");
35  printf(" instead of a normal border (3 pixels)\n");
36  printf(" -all creates all markers (no id-number required)\n");
37  printf(" will add id to the filename\n");
38  printf(" -board creates a large image with all markers\n");
39  printf(" counts as: first rows, then columns\n");
40  printf(" -bch creates marker for the BCH system rather than\n");
41  printf(" the simple (original ARToolKitPlus marker system\n");
42  printf(" id-number id of the marker to create\n");
43  printf(" do not apply this option when using board\n");
44  printf(" filename filename of the marker or board\n");
45  printf(" do not pass an extension\n");
46  return 0;
47  }
48 
49  bool _border=true, _all=false, _board=false, _thinborder=false, _bch=false;
50  int idnumber=0, numOpts=argc-1; // skip last two argv since this should be the id-number & filename
51  std::string basefilename;
52  char filename[256];
53  int idMax;
54 
55  for(int optid=1; optid<numOpts; optid++)
56  {
57  std::string opt = argv[optid];
58 
59  if(opt=="-noborder")
60  _border = false;
61  else
62  if(opt=="-thinborder")
63  _thinborder = true;
64  else
65  if(opt=="-all")
66  _all = true;
67  else
68  if(opt=="-board")
69  _board = true;
70  else
71  if(opt=="-bch")
72  _bch = true;
73  else
74  {
75  if(!_all && optid==argc-2)
76  break;
77 
78  printf("ERROR: unknown parameter '%s'\n", opt.c_str());
79  return -1;
80  }
81  }
82 
84 
85  printf("Creating marker(s) for %s id-system\n", _bch ? "BCH" : "SIMPLE");
86 
87  if(!_all)
88  idnumber = atoi(argv[argc-2]);
89  basefilename = argv[argc-1];
90 
91  if(idnumber<0 || idnumber>idMax)
92  {
93  printf("ERROR: invalid id-number %d. must be in rage [0,%d]\n", idnumber, idMax);
94  return -1;
95  }
96 
97 
98  int borderWidth = _thinborder ? 1 : 3;
99  int markerSize = ARToolKitPlus::idPattWidth+2*borderWidth;
100  PN::Image* markerImg = NULL;
102 
103 
104  if(_board || _border)
105  {
106  markerImg = PN::Image::createFromPixelBuffer(markerSize, markerSize, new unsigned short[markerSize*markerSize], true);
107  markerImg->clear(0);
108  }
109 
110  if(!_board)
111  {
112  if(_all) // creates all idMax markers
113  {
114  for(int i=0; i<=idMax; i++)
115  {
116  generatePattern(_bch, i, pat);
117 
118  sprintf(filename, "%s_%04d.tga", basefilename.c_str(), i);
119  //filename.set("%s_%04d.tga", basefilename.c_str(), i);
120 
121  PN::Image* patImg = createImageFromPattern(pat);
122 
123  if(_border)
124  {
125  markerImg->drawImage(borderWidth,borderWidth, patImg);
126  PN::ImageTool::saveAsTGA(markerImg, filename);
127  }
128  else
129  PN::ImageTool::saveAsTGA(patImg, filename);
130 
131  delete patImg;
132  }
133  delete markerImg;
134  }
135  else // creates one marker (idnumber)
136  {
137  generatePattern(_bch, idnumber, pat);
138 
139  sprintf(filename, "%s_%04d.tga", basefilename.c_str(), idnumber);
140  //filename.set("%s_%04d.tga", basefilename.c_str(), idnumber);
141 
142  PN::Image* patImg = createImageFromPattern(pat);
143 
144  if(_border)
145  {
146  markerImg->drawImage(borderWidth,borderWidth, patImg);
147  PN::ImageTool::saveAsTGA(markerImg, filename);
148  delete markerImg;
149  }
150  else
151  PN::ImageTool::saveAsTGA(patImg, filename);
152 
153  delete patImg;
154  }
155  }
156  else
157  {
158  // create a board full with idMax markers...
159  //
160  int imgW = _bch ? 260*4 : 260,
161  imgH = _bch ? 516*2 : 516;
162  int markersPerRow = _bch ? 16*4 : 16,
163  markersRows = _bch ? 32*2 : 32;
164 
165  PN::Image* board = PN::Image::createFromPixelBuffer(imgW, imgH, new unsigned short[imgW*imgH], true);
166  board->clear(0xffff);
167 
168  int x=0,y=0;
169 
170  for(y=0; y<markersRows; y++)
171  for(x=0; x<markersPerRow; x++)
172  {
173  int id = y*markersPerRow+x;
174  generatePattern(_bch, id, pat);
175 
176  PN::Image* pattern = createImageFromPattern(pat);
177 
178  markerImg->drawImage(borderWidth,borderWidth, pattern);
179  board->drawImage(4+x*(markerSize+4), 4+y*(markerSize+4), markerImg);
180 
181  delete pattern;
182  }
183 
184  sprintf(filename, "%s.tga", basefilename.c_str());
185  //filename.set("%s.tga", basefilename.c_str());
186  PN::ImageTool::saveAsTGA(board, filename);
187  delete markerImg;
188  }
189 
190 
191  return 0;
192 }
193 
194 
195 PN::Image*
197 {
198  unsigned short* pixels = new unsigned short[ARToolKitPlus::idPattWidth*ARToolKitPlus::idPattHeight];
199 
200  for(int i=0; i<ARToolKitPlus::pattBits; i++)
201  {
202  if(ARToolKitPlus::isBitSet(nPattern, i))
203  pixels[ARToolKitPlus::pattBits-1-i] = 0xffff;
204  else
205  pixels[ARToolKitPlus::pattBits-1-i] = 0;
206  }
207 
209 }
210 
const unsigned int idMaxBCH
filename
PN::Image * createImageFromPattern(ARToolKitPlus::IDPATTERN nPattern)
void drawImage(int nX, int nY, const Image *nImage, int nSx0, int nSy0, int nSx1, int nSy1, bool nTransparent=false)
Draws another bitmap inside this bitmap.
Definition: Image.cpp:88
int main(int argc, char **argv)
static Image * createFromPixelBuffer(int nWidth, int nHeight, unsigned short *nPixels, bool nOwner)
Creates an Image object directly from a pixel buffer.
Definition: Image.cpp:288
bool saveAsTGA(Image *nImage, const char *nFileName)
Save an Image into a TGA file.
Definition: ImageTool.cpp:96
static void generatePatternSimple(int nID, IDPATTERN &nPattern)
TFSIMD_FORCE_INLINE const tfScalar & y() const
void clear(int nRed, int nGreen, int nBlue)
Clears the bitmap with the given RGB color.
Definition: Image.cpp:81
TFSIMD_FORCE_INLINE const tfScalar & x() const
void generatePattern(bool nBCH, int nID, ARToolKitPlus::IDPATTERN &nPattern)
#define NULL
Definition: PocketKnife.h:38
The Image class provides basic RGB565 image handing capabilities.
Definition: Image.h:42
static bool isBitSet(IDPATTERN pat, int which)
unsigned long long IDPATTERN
static void generatePatternBCH(int nID, IDPATTERN &nPattern)


tuw_artoolkitplus
Author(s): Markus Bader
autogenerated on Sun Sep 4 2016 03:24:33