Tags.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2013 by Wayne C. Gramlich.  All rights reserved.
00002 
00003 #include <assert.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 
00007 #include "CRC.hpp"
00008 #include "FEC.hpp"
00009 #include "String.hpp"
00010 #include "SVG.hpp"
00011 
00012 extern void SVG__tag_bit(SVG *svg,
00013   double cell_width, unsigned int row, unsigned int column, bool border);
00014 extern void SVG__tag_write(/* Extractor extractor, */
00015   unsigned int tag_id, unsigned int tag_size, bool border);
00016 
00017 int main(int arguments_size, char * arguments[]) {
00018     if (arguments_size <= 1) {
00019         File__format(stderr, "Usage: tag_id...\n");
00020     } else {
00021         bool border = 1;
00022         unsigned int tag_size = 160;
00023         for (unsigned int index = 1; index < arguments_size; index++) {
00024             String tag_name = arguments[index];
00025             if (String__equal(tag_name, "-L")) {
00026               tag_size = (unsigned int)(160.0 * 11.0 / 8.5);
00027             } else {
00028                 unsigned int tag_number = String__to_unsigned(tag_name);
00029                 //File__format(stdout,
00030                 //  "[%d]: '%s' %d\n", index, tag_name, tag_number);
00031                 SVG__tag_write(tag_number, tag_size, border);
00032             }
00033         }
00034     }
00035     return 0;
00036 }
00037 
00051 
00052 void SVG__tag_bits(SVG *svg, double cell_width, unsigned int first_column,
00053   unsigned int first_row, unsigned int last_column, unsigned int last_row, bool border) {
00054 
00055     // Deal with *border*:
00056     unsigned int delta = 0;
00057     if (!border) {
00058         delta = 1;
00059     }
00060 
00061     // Deal with SVG using left-hand (rather than right-hand) Cartesian
00062     // coordinate system:
00063     first_row = 11 - first_row;
00064     last_row = 11 - last_row;
00065 
00066     // Make sure *first_column* < *last_column*:
00067     if (first_column > last_column) {
00068         unsigned int temporary_column = first_column;
00069         first_column = last_column;
00070         last_column = temporary_column;
00071         }
00072 
00073     // Make sure *first_row* < *last_row*:
00074     if (first_row > last_row) {
00075         unsigned int temporary_row = first_row;
00076         first_row = last_row;
00077         last_row = temporary_row;
00078     }
00079 
00080     // Output the appropriate rectangle to *svg*:
00081     String_Const color = "black";
00082     svg->rectangle(
00083       (double)(first_column - delta) * cell_width,
00084       (double)(first_row - delta) * cell_width,
00085       (double)(last_column - first_column + 1) * cell_width,
00086       (double)(last_row - first_row + 1) * cell_width,
00087       color, color);
00088 }
00089 
00100 
00101 void SVG__tag_bit(SVG *svg,
00102   double cell_width, unsigned int row, unsigned int column, bool border) {
00103     SVG__tag_bits(svg, cell_width, row, column, row, column, border);
00104 }
00105 
00106 // This routine will write out an SVG file for {tag_id} that is
00107 // {tag_size} millimeters square.  {border} specifies whether there
00108 // is a black line drawn around the "white" border of the tag.
00109 
00110 void SVG__tag_write(/* Extractor extractor, */
00111   unsigned int tag_id, unsigned int tag_size, bool border) {
00112 
00113     double cell_width = (double)(tag_size) / 10.0;
00114     //double offset = cell_width / 2.0;
00115     double offset = 5.0;
00116     double length = 10.0 * cell_width;
00117     double length_plus = length + 5.0 * cell_width;
00118 
00119     // Open the file for writing:
00120     String base_name = String__format("tag%d", tag_id);
00121     SVG * svg = new SVG(base_name, length + 3.0 * cell_width, length_plus,
00122         1.0, 1.0, "mm");
00123     assert (svg != NULL);
00124     svg->setOffsets(offset, offset + cell_width);
00125 
00126     // Initialize {tag_bytes} to contain 8 bytes of 0:
00127     unsigned int tag_bytes[8];
00128     for (unsigned int index = 0; index < 8; index++) {
00129         tag_bytes[index] = 0;
00130     }
00131 
00132     // Place the tag id into the tag id buffer.
00133     unsigned int id = tag_id;
00134     tag_bytes[1] = (id >> 8) & 0xff;
00135     tag_bytes[0] = id & 0xff;
00136 
00137     // Calculate the 16-bit CCITT CRC portion of the tag id buffer:
00138     unsigned int crc = CRC__compute(tag_bytes, 2);
00139     tag_bytes[3] = (crc >> 8) & 0xff;
00140     tag_bytes[2] = crc & 0xff;
00141 
00142     // Calculate the FEC portion of the tag id buffer:
00143     FEC fec = FEC__create(8, 4, 4);
00144     FEC__parity(fec, tag_bytes, 8);
00145 
00146     // Print a line border around everything:
00147     if (border) {
00148         double x_or_y = length + 2.0 * cell_width;
00149         double d = 2.0;
00150 
00151         String_Const color = "black";
00152         double x1 = 0.0;
00153         double x2 = x_or_y;
00154 
00155         //  +--                                   --+
00156         double y = -cell_width;
00157         svg->line(x1, y, x1 + d, y,     color);
00158         svg->line(x2, y, x2 - d, y,     color);
00159 
00160         //  +--                                   --+
00161         //  |                                       |
00162         y = 0.0;
00163         svg->line(x1, y, x1 + d, y,     color);
00164         svg->line(x1, y, x1,     y + d, color);
00165         svg->line(x2, y, x2 - d, y,     color);
00166         svg->line(x2, y, x2,     y + d, color);
00167 
00168         //  |                                       |
00169         //  +--                                   --+
00170         y = x_or_y;
00171         svg->line(x1, y, x1 + d, y,     color);
00172         svg->line(x1, y, x1,     y - d, color);
00173         svg->line(x2, y, x2 - d, y,     color);
00174         svg->line(x2, y, x2,     y - d, color);
00175 
00176         //  +--                                   --+
00177         y = x_or_y + cell_width;
00178         svg->line(x1, y, x1 + d, y,     color);
00179         svg->line(x2, y, x2 - d, y,     color);
00180     }
00181 
00182     // Print the bit border:
00183     // Lower row:
00184     SVG__tag_bits(svg, cell_width, 1, 1, 9, 1, border);
00185     // Right column:
00186     SVG__tag_bits(svg, cell_width, 10, 1, 10, 9, border);
00187     // Upper row:
00188     SVG__tag_bits(svg, cell_width, 2, 10, 10, 10, border);
00189     // Left column:
00190     SVG__tag_bits(svg, cell_width, 1, 2, 1, 10, border);
00191 
00192     // Print the tag data:
00193     for (unsigned int index = 0; index < 64; index++) {
00194         unsigned int row = (index >> 3) & 7;
00195         unsigned int column  = index & 7;
00196         unsigned int tag_byte = tag_bytes[row];
00197         if ((tag_byte & (1 << column)) != 0) {
00198             SVG__tag_bit(svg, cell_width, column + 2, row + 2, border);
00199         }
00200     }
00201 
00202     // Put some text on the page:
00203     String tag_name = String__format("%d", tag_id);
00204     if (border) {
00205         svg->text(tag_name,
00206           6.0 * cell_width, 12.25 * cell_width,
00207           "ariel", (unsigned int)(cell_width) / 2);
00208     } else {
00209         svg->text(tag_name,
00210           5.0 * cell_width, 12.25 * cell_width,
00211           "ariel", (unsigned int)(cell_width) / 2);
00212     }
00213     String__free(tag_name);
00214 
00215     // Close *svg*:
00216     delete svg;
00217 }
00218 


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Jun 6 2019 18:08:04