File: dmtx_mod.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (462 lines) | stat: -rw-r--r-- 12,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 *  Falcon DataMatrix - Internal
 */

#include "dmtx_mod.h"

#include <stdio.h>//debug..
#include <string.h>

#include <falcon/engine.h>
#include <falcon/garbagelock.h>

#include <dmtx.h>


namespace Falcon
{
namespace Dmtx
{

/*******************************************************************************
    DataMatrix class
*******************************************************************************/

DataMatrix::DataMatrix( const Falcon::CoreClass* cls )
    :
    CoreObject( cls ),
    mData( 0 ),
    mContext( 0 )
{
    // initialize options struct
    resetOptions();
}

DataMatrix::DataMatrix( const DataMatrix& other )
    :
    CoreObject( other ),
    mData( 0 ),
    mContext( 0 )
{
    // copy options struct
    memcpy( &options, &other.options, sizeof( DataMatrixOptions ) );
    // copy data
    if ( other.mData )
        mData = new Falcon::GarbageLock( other.mData->item() );
    // copy context
    if ( other.mContext )
        mContext = new Falcon::GarbageLock( other.mContext->item() );
}

DataMatrix::~DataMatrix()
{
    if ( mData )
        delete mData;
    if ( mContext )
        delete mContext;
}

bool
DataMatrix::getProperty( const String& nm,
                       Item& it ) const
{
    if ( nm == "module_size" )
        it.setInteger( options.module_size );
    else
    if ( nm == "margin_size" )
        it.setInteger( options.margin_size );
    else
    if ( nm == "gap_size" )
        it.setInteger( options.gap_size );
    else
    if ( nm == "scheme" )
        it.setInteger( options.scheme );
    else
    if ( nm == "shape" )
        it.setInteger( options.shape );
    else
    if ( nm == "timeout" )
        it.setInteger( options.timeout );
    else
    if ( nm == "shrink" )
        it.setInteger( options.shrink );
    else
    if ( nm == "deviation" )
        it.setInteger( options.deviation );
    else
    if ( nm == "threshold" )
        it.setInteger( options.threshold );
    else
    if ( nm == "min_edge" )
        it.setInteger( options.min_edge );
    else
    if ( nm == "max_edge" )
        it.setInteger( options.max_edge );
    else
    if ( nm == "corrections" )
        it.setInteger( options.corrections );
    else
    if ( nm == "max_count" )
        it.setInteger( options.max_count );
    else
        return defaultProperty( nm, it );
}

bool
DataMatrix::setProperty( const String& nm,
                         const Item& it )
{
    if ( !it.isInteger() )
        return false;

    if ( nm == "module_size" )
        options.module_size = it.asInteger();
    else
    if ( nm == "margin_size" )
        options.margin_size = it.asInteger();
    else
    if ( nm == "gap_size" )
        options.gap_size = it.asInteger();
    else
    if ( nm == "scheme" )
        options.scheme = it.asInteger();
    else
    if ( nm == "shape" )
        options.shape = it.asInteger();
    else
    if ( nm == "timeout" )
        options.timeout = it.asInteger();
    else
    if ( nm == "shrink" )
        options.shrink = it.asInteger();
    else
    if ( nm == "deviation" )
        options.deviation = it.asInteger();
    else
    if ( nm == "threshold" )
        options.threshold  = it.asInteger();
    else
    if ( nm == "min_edge" )
        options.min_edge = it.asInteger();
    else
    if ( nm == "max_edge" )
        options.max_edge = it.asInteger();
    else
    if ( nm == "corrections" )
        options.corrections = it.asInteger();
    else
    if ( nm == "max_count" )
        options.max_count = it.asInteger();
    else
        return false;
    return true;
}

Falcon::CoreObject*
DataMatrix::clone() const
{
    return new DataMatrix( *this );
}

Falcon::CoreObject*
DataMatrix::factory( const CoreClass* cls, void*, bool )
{
    return new DataMatrix( cls );
}

void
DataMatrix::resetOptions()
{
    memset( &options, DmtxUndefined, sizeof( DataMatrixOptions ) );
    options.shrink = 1;
}

Falcon::Item*
DataMatrix::data() const
{
    return mData ? &mData->item(): 0;
}

bool
DataMatrix::data( const Falcon::Item& itm )
{
    if ( !( itm.isString() || itm.isMemBuf() ) )
        return false;
    if ( mData )
        delete mData;
    mData = new Falcon::GarbageLock( itm );
    return true;
}

Falcon::Item*
DataMatrix::context() const
{
    return mContext ? &mContext->item(): 0;
}

bool
DataMatrix::context( const Falcon::Item& itm )
{
    if ( !itm.isObject() )
        return false;

    CoreObject* obj = itm.asObject();
    Falcon::Item tmp;
    if ( !obj->getMethod( "plot", tmp ) )
        return false;

    if ( mContext )
        delete mContext;
    mContext = new Falcon::GarbageLock( itm );
    return true;
}

bool
DataMatrix::encode( const Falcon::Item& itm,
                    const Falcon::Item& ctxt )
{
    if ( !data( itm ) )
        return false;

    if ( !context( ctxt ) )
        return false;

    switch ( itm.type() )
    {
    case FLC_ITEM_STRING:
        return encode( *itm.asString() );
    case FLC_ITEM_MEMBUF:
        return encode( *itm.asMemBuf() );
    default: // not reached
        fassert( 0 );
    }
}

bool
DataMatrix::encode( const Falcon::String& data )
{
    return internalEncode( (const char*) data.getRawStorage(), data.size() );
}

bool
DataMatrix::encode( const Falcon::MemBuf& data )
{
    return internalEncode( (const char*) data.data(), data.size() );
}

bool
DataMatrix::internalEncode( const char* data,
                            const uint32 sz )
{
    fassert( mContext );

    DmtxEncode* enc;
    CoreObject* ctxt = mContext->item().asObjectSafe();
    Falcon::Item meth;
    VMachine* vm = VMachine::getCurrent();
    int row, col;
    int rgb[3];

    // create the dmtx encoder
    enc = dmtxEncodeCreate();
    if ( !enc )
        return false;

    dmtxEncodeSetProp( enc, DmtxPropPixelPacking, DmtxPack24bppRGB );
    dmtxEncodeSetProp( enc, DmtxPropImageFlip, DmtxFlipNone );

    if ( options.scheme != DmtxUndefined )
        dmtxEncodeSetProp( enc, DmtxPropScheme, options.scheme );

    if ( options.shape != DmtxUndefined )
        dmtxEncodeSetProp( enc, DmtxPropSizeRequest, options.shape );

    if ( options.margin_size != DmtxUndefined )
        dmtxEncodeSetProp( enc, DmtxPropMarginSize, options.margin_size );

    if ( options.module_size != DmtxUndefined )
        dmtxEncodeSetProp( enc, DmtxPropModuleSize, options.module_size );

    dmtxEncodeDataMatrix( enc, sz, (unsigned char*) data );

    // call context start( width, height )
    if ( ctxt->getMethod( "start", meth ) )
    {
        fassert( meth.isCallable() );
        vm->pushParam( enc->image->width );
        vm->pushParam( enc->image->height );
        vm->callItem( meth, 2 );
    }

    // call context plot( row, col, red, green, blue )
    ctxt->getMethod( "plot", meth );
    fassert( meth.isCallable() );

    for ( row = 0; row < enc->image->height; ++row )
    {
        for ( col = 0; col < enc->image->width; ++col )
        {
            dmtxImageGetPixelValue( enc->image, col, row, 0, &rgb[0] );
            dmtxImageGetPixelValue( enc->image, col, row, 1, &rgb[1] );
            dmtxImageGetPixelValue( enc->image, col, row, 2, &rgb[2] );
            vm->pushParam( row );
            vm->pushParam( col );
            vm->pushParam( rgb[0] );
            vm->pushParam( rgb[1] );
            vm->pushParam( rgb[2] );
            vm->callItem( meth, 5 );
        }
    }

    // call context finish()
    if ( ctxt->getMethod( "finish", meth ) )
    {
        fassert( meth.isCallable() );
        vm->callItem( meth, 0 );
    }

    // finished
    dmtxEncodeDestroy( &enc );
    return true;
}

bool
DataMatrix::decode( const Falcon::Item& dat,
                    int width,
                    int height,
                    Falcon::CoreArray** output )
{
    switch ( dat.type() )
    {
    case FLC_ITEM_STRING:
        data( dat );
        return decode( *dat.asString(), width, height, output );
    case FLC_ITEM_MEMBUF:
        data( dat );
        return decode( *dat.asMemBuf(), width, height, output );
    default:
        return false;
    }
}

bool
DataMatrix::decode( const Falcon::String& data,
                    int width,
                    int height,
                    Falcon::CoreArray** output )
{
    return internalDecode( (const char*) data.getRawStorage(), data.size(),
                           width, height, output );
}

bool DataMatrix::decode( const Falcon::MemBuf& data,
                         int width,
                         int height,
                         Falcon::CoreArray** output )
{
    return internalDecode( (const char*) data.data(), data.size(),
                           width, height, output );
}

bool
DataMatrix::internalDecode( const char* data,
                            const uint32 sz,
                            int width,
                            int height,
                            Falcon::CoreArray** output )
{
    DmtxImage*  img;
    DmtxDecode* dec;
    DmtxTime dmtx_timeout;
    DmtxRegion* reg;
    DmtxMessage* msg;
    DmtxVector2 p00, p10, p11, p01;
    int found = 0;

    // reset timeout for each new page
    if ( options.timeout != DmtxUndefined )
        dmtx_timeout = dmtxTimeAdd( dmtxTimeNow(), options.timeout );

    img = dmtxImageCreate( (unsigned char*)data, width, height, DmtxPack24bppRGB );
    if ( !img )
        return false;

    dec = dmtxDecodeCreate( img, options.shrink );
    if ( !dec )
    {
        dmtxImageDestroy( &img );
        return false;
    }

    if ( options.gap_size != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropScanGap, options.gap_size );

    if ( options.shape != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropSymbolSize, options.shape );

    if ( options.deviation != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropSquareDevn, options.deviation );

    if ( options.threshold != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropEdgeThresh, options.threshold );

    if ( options.min_edge != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropEdgeMin, options.min_edge );

    if ( options.max_edge != DmtxUndefined )
        dmtxDecodeSetProp( dec, DmtxPropEdgeMax, options.max_edge );

    // initialize output array
    *output = new Falcon::CoreArray;

    for ( int count=1; ;count++ )
    {
        if ( options.timeout == DmtxUndefined )
            reg = dmtxRegionFindNext( dec, NULL );
        else
            reg = dmtxRegionFindNext( dec, &dmtx_timeout );

        // finished file or ran out of time before finding another region
        if ( !reg )
            break;

        msg = dmtxDecodeMatrixRegion( dec, reg, options.corrections );
        if ( msg )
        {
            p00.X = p00.Y = p10.Y = p01.X = 0.0;
            p10.X = p01.Y = p11.X = p11.Y = 1.0;
            dmtxMatrix3VMultiplyBy( &p00, reg->fit2raw );
            dmtxMatrix3VMultiplyBy( &p10, reg->fit2raw );
            dmtxMatrix3VMultiplyBy( &p11, reg->fit2raw );
            dmtxMatrix3VMultiplyBy( &p01, reg->fit2raw );

            CoreArray* res = new Falcon::CoreArray( 9 );
            res->append( String( (const char*)msg->output ) );
            res->append( (int)((options.shrink * p00.X) + 0.5) );
            res->append( height - 1 - (int)((options.shrink * p00.Y) + 0.5) );
            res->append( (int)((options.shrink * p10.X) + 0.5) );
            res->append( height - 1 - (int)((options.shrink * p10.Y) + 0.5) );
            res->append( (int)((options.shrink * p11.X) + 0.5) );
            res->append( height - 1 - (int)((options.shrink * p11.Y) + 0.5) );
            res->append( (int)((options.shrink * p01.X) + 0.5) );
            res->append( height - 1 - (int)((options.shrink * p01.Y) + 0.5) );
            (*output)->append( res );

            dmtxMessageDestroy( &msg );
            ++found;
        }
        dmtxRegionDestroy( &reg );

        // stop if we've reached maximum count
        if ( options.max_count != DmtxUndefined )
            if ( found >= options.max_count )
                break;
    }

    dmtxDecodeDestroy( &dec );
    dmtxImageDestroy( &img );
    return true;
}

} // !namespace Dmtx
} // !namespace Falcon