File: simple_test.c

package info (click to toggle)
libdmtx 0.7.7-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,100 kB
  • sloc: ansic: 10,519; sh: 253; makefile: 154; perl: 28
file content (123 lines) | stat: -rw-r--r-- 3,573 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
/**
 * libdmtx - Data Matrix Encoding/Decoding Library
 * Copyright 2008, 2009 Mike Laughton. All rights reserved.
 * Copyright 2010-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
 * Copyright 2016 Tim Zaman. All rights reserved.
 *
 * See LICENSE file in the main project directory for full
 * terms of use and distribution.
 *
 * Contact:
 * Vadim A. Misbakh-Soloviov <dmtx@mva.name>
 * Mike Laughton <mike@dragonflylogic.com>
 *
 * \file simple_test.c
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "../../dmtx.h"

int
main(int argc, char *argv[])
{
   size_t          width, height, bytesPerPixel;
   unsigned char   str[] = "30Q324343430794<OQQ";
   unsigned char  *pxl;
   DmtxEncode     *enc;
   DmtxImage      *img;
   DmtxDecode     *dec;
   DmtxRegion     *reg;
   DmtxMessage    *msg;

   fprintf(stdout, "input:  \"%s\"\n", str);

   /* 1) ENCODE a new Data Matrix barcode image (in memory only) */

   enc = dmtxEncodeCreate();

   /*
    dmtxEncodeSetProp( enc, DmtxPropPixelPacking, DmtxPack16bppRGB );
    dmtxEncodeSetProp( enc, DmtxPropPixelPacking, DmtxPack32bppRGB );
    dmtxEncodeSetProp( enc, DmtxPropWidth, 160 );
    dmtxEncodeSetProp( enc, DmtxPropHeight, 160 );
   */

   assert(enc != NULL);
   dmtxEncodeDataMatrix(enc, strlen((const char *)str), str);

   /* 2) COPY the new image data before releasing encoding memory */

   width = dmtxImageGetProp(enc->image, DmtxPropWidth);
   height = dmtxImageGetProp(enc->image, DmtxPropHeight);
   bytesPerPixel = dmtxImageGetProp(enc->image, DmtxPropBytesPerPixel);

   pxl = (unsigned char *)malloc(width * height * bytesPerPixel);
   assert(pxl != NULL);
   memcpy(pxl, enc->image->pxl, width * height * bytesPerPixel);

   dmtxEncodeDestroy(&enc);

   fprintf(stdout, "width:  \"%zd\"\n", width);
   fprintf(stdout, "height: \"%zd\"\n", height);
   fprintf(stdout, "bpp:    \"%zd\"\n", bytesPerPixel);

   for (int i=0; i<width*height; i++){
      fprintf(stdout, "%d", (pxl[i*3])==0);
      if (i%width==width-1){
         fprintf(stdout, "\n");
      }
   }

   /* 3) DECODE the Data Matrix barcode from the copied image */

   img = dmtxImageCreate(pxl, width, height, DmtxPack24bppRGB);
   assert(img != NULL);

   dec = dmtxDecodeCreate(img, 1);
   assert(dec != NULL);

   reg = dmtxRegionFindNext(dec, NULL);
   if(reg != NULL) {
      msg = dmtxDecodeMatrixRegion(dec, reg, DmtxUndefined);

      fprintf(stdout, "msg->arraySize :  \"%zd\"\n", msg->arraySize );
      fprintf(stdout, "msg->codeSize  :  \"%zd\"\n", msg->codeSize  );
      fprintf(stdout, "msg->outputSize:  \"%zd\"\n", msg->outputSize);
      int oned = sqrt(msg->arraySize);
      for (int i=0; i<msg->arraySize; i++){
         fprintf(stdout, " %c.", msg->array[i]);
         if (i%oned==oned-1){
            fprintf(stdout, "\n");
         }
      }
      fprintf(stdout, "\n\n");
      for (int j=0; j<msg->codeSize; j++){
         fprintf(stdout, " %c.", msg->code[j]);
      }
      fprintf(stdout, "\n\n");
      for (int k=0; k<msg->outputSize; k++){
         fprintf(stdout, " %c.", msg->output[k]);
      }
      fprintf(stdout, "\n\n");

      if(msg != NULL) {
         fputs("output: \"", stdout);
         fwrite(msg->output, sizeof(unsigned char), msg->outputIdx, stdout);
         fputs("\"\n", stdout);
         dmtxMessageDestroy(&msg);
      }
      dmtxRegionDestroy(&reg);
   }

   dmtxDecodeDestroy(&dec);
   dmtxImageDestroy(&img);
   free(pxl);

   fprintf(stdout, "%d\n", getSizeIdxFromSymbolDimension(12, 12));

   exit(0);
}