Class: DataMatrix::Encoder

Inherits:
Object
  • Object
show all
Defined in:
ext/semacode.c

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Object

Initialize the semacode. This function is called after a semacode is created. Ruby objects are created using a new method, and then initialized via the ‘initialize’ method once they have been allocated.

The initializer takes a single argument, which can be anything that responds to the ‘to_s’ method - that is, anything string like.

The string in the argument is encoded and the semacode is returned initialized and ready for use.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/semacode.c', line 120

static VALUE
semacode_init(VALUE self, VALUE message)
{
  semacode_t *semacode;
  
  if (!rb_respond_to(message, rb_intern ("to_s")))
      rb_raise(rb_eRuntimeError, "target must respond to 'to_s'");

  Data_Get_Struct(self, semacode_t, semacode);
  encode_string(semacode, StringValueLen(message), StringValuePtr(message));
  
  return self;
}

Instance Method Details

#dataObject

This function gives the encoding organized by rows and columns.

It returns the semacode matrix as an array of arrays of boolean. The first element in the array is the top row, the last is the bottom row.

Each row is also an array, containing boolean values. The length of each row is the same as the semacode width, and the number of rows is the same as the semacode height.



259
260
261
262
263
264
265
266
267
268
269
# File 'ext/semacode.c', line 259

static VALUE
semacode_data(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  if(semacode->data == NULL)
    return Qnil;
  else
    return semacode_grid(semacode);
}

#ecc_bytesObject

This returns the number of bytes that are being devoted to error correction.



353
354
355
356
357
358
359
360
# File 'ext/semacode.c', line 353

static VALUE
semacode_ecc_bytes(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->ecc_bytes);
}

#encode(message) ⇒ Object

After creating a semacode, it is possible to reuse the semacode object if you want to encode another URL. You should call the ‘encode’ method at any time to create a replacement semecode for the current object.

It returns the semacode matrix as an array of arrays of boolean. The first element in the array is the top row, the last is the bottom row.

Each row is also an array, containing boolean values. The length of each row is the same as the semacode width, and the number of rows is the same as the semacode height.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/semacode.c', line 226

static VALUE
semacode_encode(VALUE self, VALUE message)
{
  semacode_t *semacode;
  
  if (!rb_respond_to(message, rb_intern ("to_s")))
      rb_raise(rb_eRuntimeError, "target must respond to 'to_s'");
  
  Data_Get_Struct(self, semacode_t, semacode);
  
  /* free previous string if that exists */
  if(semacode->data != NULL) {
    free(semacode->data);
    semacode->data == NULL;
  }
  
  /* do a new encoding */
  DATA_PTR(self) = encode_string(semacode, StringValueLen(message), StringValuePtr(message));

  return semacode_grid(semacode);
}

#encodingObject

This returns the encoding string used to create the semacode.



274
275
276
277
278
279
280
281
# File 'ext/semacode.c', line 274

static VALUE
semacode_encoded(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return rb_str_new2(semacode->encoding);
}

#heightObject

This returns the height of the semacode.



298
299
300
301
302
303
304
305
# File 'ext/semacode.c', line 298

static VALUE
semacode_height(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->height);
}

#lengthObject

This returns the length of the semacode. It is the same as the product of the height and the width.



311
312
313
314
315
316
317
318
# File 'ext/semacode.c', line 311

static VALUE
semacode_length(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->height * semacode->width);
}

#raw_encoded_lengthObject

This returns the length of the raw underlying encoding representing the data, before padding, error correction or any other operations on the raw encoding.



325
326
327
328
329
330
331
332
# File 'ext/semacode.c', line 325

static VALUE
semacode_raw_encoded_length(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->raw_encoded_length);
}

#sizeObject

This returns the length of the semacode. It is the same as the product of the height and the width.



311
312
313
314
315
316
317
318
# File 'ext/semacode.c', line 311

static VALUE
semacode_length(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->height * semacode->width);
}

#symbol_sizeObject

This returns the maximum number of characters that can be stored in a symbol of the given width and height. You can use this to decide if it is worth packing in extra information while keeping the symbol size the same.



340
341
342
343
344
345
346
347
# File 'ext/semacode.c', line 340

static VALUE
semacode_symbol_size(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->symbol_capacity);
}

#to_aObject

This function gives the encoding organized by rows and columns.

It returns the semacode matrix as an array of arrays of boolean. The first element in the array is the top row, the last is the bottom row.

Each row is also an array, containing boolean values. The length of each row is the same as the semacode width, and the number of rows is the same as the semacode height.



259
260
261
262
263
264
265
266
267
268
269
# File 'ext/semacode.c', line 259

static VALUE
semacode_data(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  if(semacode->data == NULL)
    return Qnil;
  else
    return semacode_grid(semacode);
}

#to_sObject

This function turns the raw output from an encoding into a string representation.

It returns the semacode matrix as a comma-separated list of character vectors (sequence of characters). The top row is the first vector and the bottow row is the last vector.

Each vector is a sequence of characters, either ‘1’ or ‘0’, to represent the bits of the semacode pattern. The length of a vector is the semacode width, and the number of vectors is the same as the semacode height.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/semacode.c', line 182

static VALUE
semacode_to_s(VALUE self)
{
  semacode_t *semacode;
  VALUE str;
  int x, y;  
  int w, h;
  
  Data_Get_Struct(self, semacode_t, semacode);
  
  if(semacode == NULL || semacode->data == NULL)
    return Qnil;
  
  w = semacode->width;
  h = semacode->height;
  
  str = rb_str_new2("");
  
	for (y = h - 1; y >= 0; y--) {
		for (x = 0; x < w; x++) {
		  if(semacode->data[y * w + x])
		    rb_str_cat(str, "1", 1);
		  else
		    rb_str_cat(str, "0", 1);
		}
		rb_str_cat(str, ",", 1);
	}
	
	return str;
}

#to_strObject

This function turns the raw output from an encoding into a string representation.

It returns the semacode matrix as a comma-separated list of character vectors (sequence of characters). The top row is the first vector and the bottow row is the last vector.

Each vector is a sequence of characters, either ‘1’ or ‘0’, to represent the bits of the semacode pattern. The length of a vector is the semacode width, and the number of vectors is the same as the semacode height.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'ext/semacode.c', line 182

static VALUE
semacode_to_s(VALUE self)
{
  semacode_t *semacode;
  VALUE str;
  int x, y;  
  int w, h;
  
  Data_Get_Struct(self, semacode_t, semacode);
  
  if(semacode == NULL || semacode->data == NULL)
    return Qnil;
  
  w = semacode->width;
  h = semacode->height;
  
  str = rb_str_new2("");
  
	for (y = h - 1; y >= 0; y--) {
		for (x = 0; x < w; x++) {
		  if(semacode->data[y * w + x])
		    rb_str_cat(str, "1", 1);
		  else
		    rb_str_cat(str, "0", 1);
		}
		rb_str_cat(str, ",", 1);
	}
	
	return str;
}

#widthObject

This returns the width of the semacode.



286
287
288
289
290
291
292
293
# File 'ext/semacode.c', line 286

static VALUE
semacode_width(VALUE self)
{
  semacode_t *semacode;
  Data_Get_Struct(self, semacode_t, semacode);

  return INT2FIX(semacode->width);
}