Tags.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013 by Wayne C. Gramlich. All rights reserved.
2 
3 #include <assert.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include "CRC.hpp"
8 #include "FEC.hpp"
9 #include "String.hpp"
10 #include "SVG.hpp"
11 
12 extern void SVG__tag_bit(SVG *svg,
13  double cell_width, unsigned int row, unsigned int column, bool border);
14 extern void SVG__tag_write(/* Extractor extractor, */
15  unsigned int tag_id, unsigned int tag_size, bool border);
16 
17 int main(int arguments_size, char * arguments[]) {
18  if (arguments_size <= 1) {
19  File__format(stderr, "Usage: tag_id...\n");
20  } else {
21  bool border = 1;
22  unsigned int tag_size = 160;
23  for (unsigned int index = 1; index < arguments_size; index++) {
24  String tag_name = arguments[index];
25  if (String__equal(tag_name, "-L")) {
26  tag_size = (unsigned int)(160.0 * 11.0 / 8.5);
27  } else {
28  unsigned int tag_number = String__to_unsigned(tag_name);
29  //File__format(stdout,
30  // "[%d]: '%s' %d\n", index, tag_name, tag_number);
31  SVG__tag_write(tag_number, tag_size, border);
32  }
33  }
34  }
35  return 0;
36 }
37 
51 
52 void SVG__tag_bits(SVG *svg, double cell_width, unsigned int first_column,
53  unsigned int first_row, unsigned int last_column, unsigned int last_row, bool border) {
54 
55  // Deal with *border*:
56  unsigned int delta = 0;
57  if (!border) {
58  delta = 1;
59  }
60 
61  // Deal with SVG using left-hand (rather than right-hand) Cartesian
62  // coordinate system:
63  first_row = 11 - first_row;
64  last_row = 11 - last_row;
65 
66  // Make sure *first_column* < *last_column*:
67  if (first_column > last_column) {
68  unsigned int temporary_column = first_column;
69  first_column = last_column;
70  last_column = temporary_column;
71  }
72 
73  // Make sure *first_row* < *last_row*:
74  if (first_row > last_row) {
75  unsigned int temporary_row = first_row;
76  first_row = last_row;
77  last_row = temporary_row;
78  }
79 
80  // Output the appropriate rectangle to *svg*:
81  String_Const color = "black";
82  svg->rectangle(
83  (double)(first_column - delta) * cell_width,
84  (double)(first_row - delta) * cell_width,
85  (double)(last_column - first_column + 1) * cell_width,
86  (double)(last_row - first_row + 1) * cell_width,
87  color, color);
88 }
89 
100 
101 void SVG__tag_bit(SVG *svg,
102  double cell_width, unsigned int row, unsigned int column, bool border) {
103  SVG__tag_bits(svg, cell_width, row, column, row, column, border);
104 }
105 
106 // This routine will write out an SVG file for {tag_id} that is
107 // {tag_size} millimeters square. {border} specifies whether there
108 // is a black line drawn around the "white" border of the tag.
109 
110 void SVG__tag_write(/* Extractor extractor, */
111  unsigned int tag_id, unsigned int tag_size, bool border) {
112 
113  double cell_width = (double)(tag_size) / 10.0;
114  //double offset = cell_width / 2.0;
115  double offset = 5.0;
116  double length = 10.0 * cell_width;
117  double length_plus = length + 5.0 * cell_width;
118 
119  // Open the file for writing:
120  String base_name = String__format("tag%d", tag_id);
121  SVG * svg = new SVG(base_name, length + 3.0 * cell_width, length_plus,
122  1.0, 1.0, "mm");
123  assert (svg != NULL);
124  svg->setOffsets(offset, offset + cell_width);
125 
126  // Initialize {tag_bytes} to contain 8 bytes of 0:
127  unsigned int tag_bytes[8];
128  for (unsigned int index = 0; index < 8; index++) {
129  tag_bytes[index] = 0;
130  }
131 
132  // Place the tag id into the tag id buffer.
133  unsigned int id = tag_id;
134  tag_bytes[1] = (id >> 8) & 0xff;
135  tag_bytes[0] = id & 0xff;
136 
137  // Calculate the 16-bit CCITT CRC portion of the tag id buffer:
138  unsigned int crc = CRC__compute(tag_bytes, 2);
139  tag_bytes[3] = (crc >> 8) & 0xff;
140  tag_bytes[2] = crc & 0xff;
141 
142  // Calculate the FEC portion of the tag id buffer:
143  FEC fec = FEC__create(8, 4, 4);
144  FEC__parity(fec, tag_bytes, 8);
145 
146  // Print a line border around everything:
147  if (border) {
148  double x_or_y = length + 2.0 * cell_width;
149  double d = 2.0;
150 
151  String_Const color = "black";
152  double x1 = 0.0;
153  double x2 = x_or_y;
154 
155  // +-- --+
156  double y = -cell_width;
157  svg->line(x1, y, x1 + d, y, color);
158  svg->line(x2, y, x2 - d, y, color);
159 
160  // +-- --+
161  // | |
162  y = 0.0;
163  svg->line(x1, y, x1 + d, y, color);
164  svg->line(x1, y, x1, y + d, color);
165  svg->line(x2, y, x2 - d, y, color);
166  svg->line(x2, y, x2, y + d, color);
167 
168  // | |
169  // +-- --+
170  y = x_or_y;
171  svg->line(x1, y, x1 + d, y, color);
172  svg->line(x1, y, x1, y - d, color);
173  svg->line(x2, y, x2 - d, y, color);
174  svg->line(x2, y, x2, y - d, color);
175 
176  // +-- --+
177  y = x_or_y + cell_width;
178  svg->line(x1, y, x1 + d, y, color);
179  svg->line(x2, y, x2 - d, y, color);
180  }
181 
182  // Print the bit border:
183  // Lower row:
184  SVG__tag_bits(svg, cell_width, 1, 1, 9, 1, border);
185  // Right column:
186  SVG__tag_bits(svg, cell_width, 10, 1, 10, 9, border);
187  // Upper row:
188  SVG__tag_bits(svg, cell_width, 2, 10, 10, 10, border);
189  // Left column:
190  SVG__tag_bits(svg, cell_width, 1, 2, 1, 10, border);
191 
192  // Print the tag data:
193  for (unsigned int index = 0; index < 64; index++) {
194  unsigned int row = (index >> 3) & 7;
195  unsigned int column = index & 7;
196  unsigned int tag_byte = tag_bytes[row];
197  if ((tag_byte & (1 << column)) != 0) {
198  SVG__tag_bit(svg, cell_width, column + 2, row + 2, border);
199  }
200  }
201 
202  // Put some text on the page:
203  String tag_name = String__format("%d", tag_id);
204  if (border) {
205  svg->text(tag_name,
206  6.0 * cell_width, 12.25 * cell_width,
207  "ariel", (unsigned int)(cell_width) / 2);
208  } else {
209  svg->text(tag_name,
210  5.0 * cell_width, 12.25 * cell_width,
211  "ariel", (unsigned int)(cell_width) / 2);
212  }
213  String__free(tag_name);
214 
215  // Close *svg*:
216  delete svg;
217 }
218 
SVG_Struct is the data structure for representing Scalable Vector Graphics.
Definition: SVG.hpp:15
void SVG__tag_bit(SVG *svg, double cell_width, unsigned int row, unsigned int column, bool border)
Draw a bit at row and column.
Definition: Tags.cpp:101
void String__free(String_Const string)
will free memory assciated with string.
Definition: String.cpp:65
Definition: FEC.hpp:44
void line(double x1, double y1, double x2, double y2, String_Const stroke)
Draw a line from (x1, y1) to (x2, y2) using stroke.
Definition: SVG.cpp:91
int main(int arguments_size, char *arguments[])
Definition: Tags.cpp:17
void SVG__tag_bits(SVG *svg, double cell_width, unsigned int first_column, unsigned int first_row, unsigned int last_column, unsigned int last_row, bool border)
Draw a rectangular section of bits:
Definition: Tags.cpp:52
void File__format(File file, String_Const format,...)
will write format out to file with all patterns that start with "%" replaced by formatted versions of...
Definition: File.cpp:107
char * String
Copyright (c) 2013-2014 by Wayne C. Gramlich. All rights reserved.
Definition: String.hpp:8
void text(String_Const message, double x, double y, String_Const font_family, unsigned int font_size)
draw message at (x, y).
Definition: SVG.cpp:196
void setOffsets(double x_offset, double y_offset)
Definition: SVG.cpp:207
FEC FEC__create(unsigned int symbol_size, unsigned int data_size, unsigned int parity_size)
Definition: FEC.cpp:653
unsigned int CRC__compute(unsigned int *buffer, unsigned int size)
Returns a 16-bit CRC of the size bytes in buffer.
Definition: CRC.cpp:58
void FEC__parity(FEC fec, unsigned int *data, unsigned int size)
Definition: FEC.cpp:630
void SVG__tag_write(unsigned int tag_id, unsigned int tag_size, bool border)
Definition: Tags.cpp:110
unsigned int String__to_unsigned(String_Const string)
Converts from decimal string into a number and return it.
Definition: String.cpp:90
const char * String_Const
Definition: String.hpp:9
bool String__equal(String_Const string1, String_Const string2)
Returns true if string1 equals string2.
Definition: String.cpp:31
void rectangle(double x, double y, double width, double height, String_Const stroke_color, String_Const fill_color)
Draw a width by height rectangle at (x, y).
Definition: SVG.cpp:171
String String__format(String_Const format,...)
Return a formatted version of format.
Definition: String.cpp:43


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53