forked from dmtx/libdmtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmtximage.c
432 lines (388 loc) · 11.4 KB
/
dmtximage.c
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
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2008, 2009 Mike Laughton. All rights reserved.
* Copyright 2012-2016 Vadim A. Misbakh-Soloviov. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact:
* Vadim A. Misbakh-Soloviov <[email protected]>
* Mike Laughton <[email protected]>
*
* \file dmtximage.c
* \brief Image handling
*/
/**
* libdmtx stores image data as a large one-dimensional array of packed pixels,
* reading from the array when scanning barcodes and writing to it when creating
* a barcode. Beyond this interaction the calling program is responsible for
* populating and dispatching pixels between the image array and the outside
* world, whether that means loading an image from a file, acquiring camera
* input, displaying output to a screen, saving to disk, etc...
*
* By default, libdmtx treats the first pixel of an image array as the top-left
* corner of the physical image, with the final pixel landing at the bottom-
* right. However, if mapping a pixel buffer this way produces an inverted
* image the calling program can specify DmtxFlipY at image creation time to
* remove the inversion. This has a negligible effect on performance since it
* only modifies the pixel mapping math, and does not alter any pixel data.
*
* Regardless of how an image is stored internally, all libdmtx functions
* consider coordinate (0,0) to mathematically represent the bottom-left pixel
* location of an image using a right-handed coordinate system.
*
* (0,HEIGHT-1) (WIDTH-1,HEIGHT-1)
*
* array pos = 0,1,2,3,...-----------+
* | |
* | |
* | libdmtx |
* | image |
* | coordinates |
* | |
* | |
* +---------...,N-2,N-1,N = array pos
*
* (0,0) (WIDTH-1,0)
*
* Notes:
* - OpenGL pixel arrays obtained with glReadPixels() are stored
* bottom-to-top; use DmtxFlipY
* - Many popular image formats (e.g., PNG, GIF) store rows
* top-to-bottom; use DmtxFlipNone
*/
/**
* \brief XXX
* \param XXX
* \return XXX
*/
extern DmtxImage *
dmtxImageCreate(unsigned char *pxl, int width, int height, int pack)
{
// DmtxPassFail err;
DmtxImage *img;
if(pxl == NULL || width < 1 || height < 1)
return NULL;
img = (DmtxImage *)calloc(1, sizeof(DmtxImage));
if(img == NULL)
return NULL;
img->pxl = pxl;
img->width = width;
img->height = height;
img->pixelPacking = pack;
img->bitsPerPixel = GetBitsPerPixel(pack);
img->bytesPerPixel = img->bitsPerPixel/8;
img->rowPadBytes = 0;
img->rowSizeBytes = img->width * img->bytesPerPixel + img->rowPadBytes;
img->imageFlip = DmtxFlipNone;
/* Leave channelStart[] and bitsPerChannel[] with zeros from calloc */
img->channelCount = 0;
switch(pack) {
case DmtxPackCustom:
break;
case DmtxPack1bppK:
dmtxImageSetChannel(img, 0, 1);
return NULL; /* unsupported packing order */
/* break; */
case DmtxPack8bppK:
dmtxImageSetChannel(img, 0, 8);
break;
case DmtxPack16bppRGB:
case DmtxPack16bppBGR:
case DmtxPack16bppYCbCr:
dmtxImageSetChannel(img, 0, 5);
dmtxImageSetChannel(img, 5, 5);
dmtxImageSetChannel(img, 10, 5);
break;
case DmtxPack24bppRGB:
case DmtxPack24bppBGR:
case DmtxPack24bppYCbCr:
case DmtxPack32bppRGBX:
case DmtxPack32bppBGRX:
dmtxImageSetChannel(img, 0, 8);
dmtxImageSetChannel(img, 8, 8);
dmtxImageSetChannel(img, 16, 8);
break;
case DmtxPack16bppRGBX:
case DmtxPack16bppBGRX:
dmtxImageSetChannel(img, 0, 5);
dmtxImageSetChannel(img, 5, 5);
dmtxImageSetChannel(img, 10, 5);
break;
case DmtxPack16bppXRGB:
case DmtxPack16bppXBGR:
dmtxImageSetChannel(img, 1, 5);
dmtxImageSetChannel(img, 6, 5);
dmtxImageSetChannel(img, 11, 5);
break;
case DmtxPack32bppXRGB:
case DmtxPack32bppXBGR:
dmtxImageSetChannel(img, 8, 8);
dmtxImageSetChannel(img, 16, 8);
dmtxImageSetChannel(img, 24, 8);
break;
case DmtxPack32bppCMYK:
dmtxImageSetChannel(img, 0, 8);
dmtxImageSetChannel(img, 8, 8);
dmtxImageSetChannel(img, 16, 8);
dmtxImageSetChannel(img, 24, 8);
break;
default:
return NULL;
}
return img;
}
/**
* \brief Free libdmtx image memory
* \param img pointer to img location
* \return DmtxFail | DmtxPass
*/
extern DmtxPassFail
dmtxImageDestroy(DmtxImage **img)
{
if(img == NULL || *img == NULL)
return DmtxFail;
free(*img);
*img = NULL;
return DmtxPass;
}
/**
*
*
*/
extern DmtxPassFail
dmtxImageSetChannel(DmtxImage *img, int channelStart, int bitsPerChannel)
{
if(img->channelCount >= 4) /* IMAGE_MAX_CHANNEL */
return DmtxFail;
/* New channel extends beyond pixel data */
/* if(channelStart + bitsPerChannel > img->bitsPerPixel)
return DmtxFail; */
img->bitsPerChannel[img->channelCount] = bitsPerChannel;
img->channelStart[img->channelCount] = channelStart;
(img->channelCount)++;
return DmtxPass;
}
/**
* \brief Set image property
* \param img pointer to image
* \return image width
*/
extern DmtxPassFail
dmtxImageSetProp(DmtxImage *img, int prop, int value)
{
if(img == NULL)
return DmtxFail;
switch(prop) {
case DmtxPropRowPadBytes:
img->rowPadBytes = value;
img->rowSizeBytes = img->width * (img->bitsPerPixel/8) + img->rowPadBytes;
break;
case DmtxPropImageFlip:
img->imageFlip = value;
break;
default:
break;
}
return DmtxPass;
}
/**
* \brief Get image width
* \param img pointer to image
* \return image width
*/
extern int
dmtxImageGetProp(DmtxImage *img, int prop)
{
if(img == NULL)
return DmtxUndefined;
switch(prop) {
case DmtxPropWidth:
return img->width;
case DmtxPropHeight:
return img->height;
case DmtxPropPixelPacking:
return img->pixelPacking;
case DmtxPropBitsPerPixel:
return img->bitsPerPixel;
case DmtxPropBytesPerPixel:
return img->bytesPerPixel;
case DmtxPropRowPadBytes:
return img->rowPadBytes;
case DmtxPropRowSizeBytes:
return img->rowSizeBytes;
case DmtxPropImageFlip:
return img->imageFlip;
case DmtxPropChannelCount:
return img->channelCount;
default:
break;
}
return DmtxUndefined;
}
/**
* \brief Returns pixel offset for image
* \param img
* \param x coordinate
* \param y coordinate
* \return pixel byte offset
*/
extern int
dmtxImageGetByteOffset(DmtxImage *img, int x, int y)
{
assert(img != NULL);
assert(!(img->imageFlip & DmtxFlipX)); /* DmtxFlipX is not an option */
if(dmtxImageContainsInt(img, 0, x, y) == DmtxFalse)
return DmtxUndefined;
if(img->imageFlip & DmtxFlipY)
return (y * img->rowSizeBytes + x * img->bytesPerPixel);
return ((img->height - y - 1) * img->rowSizeBytes + x * img->bytesPerPixel);
}
/**
*
*
*/
extern DmtxPassFail
dmtxImageGetPixelValue(DmtxImage *img, int x, int y, int channel, int *value)
{
int offset;
/* unsigned char *pixelPtr;
int pixelValue;
int mask;
int bitShift; */
assert(img != NULL);
assert(channel < img->channelCount);
offset = dmtxImageGetByteOffset(img, x, y);
if(offset == DmtxUndefined)
return DmtxFail;
switch(img->bitsPerChannel[channel]) {
case 1:
/* assert(img->bitsPerPixel == 1);
mask = 0x01 << (7 - offset%8);
*value = (img->pxl[offset/8] & mask) ? 255 : 0; */
break;
case 5:
/* XXX might be expensive if we want to scale perfect 0-255 range */
/* assert(img->bitsPerPixel == 16);
pixelPtr = img->pxl + (offset * (img->bitsPerPixel/8));
pixelValue = (*pixelPtr << 8) | (*(pixelPtr+1));
bitShift = img->bitsPerPixel - 5 - img->channelStart[channel];
mask = 0x1f << bitShift;
*value = (((pixelValue & mask) >> bitShift) << 3); */
break;
case 8:
assert(img->channelStart[channel] % 8 == 0);
assert(img->bitsPerPixel % 8 == 0);
*value = img->pxl[offset + channel];
break;
}
return DmtxPass;
}
/**
*
*
*/
extern DmtxPassFail
dmtxImageSetPixelValue(DmtxImage *img, int x, int y, int channel, int value)
{
int offset;
/* unsigned char *pixelPtr; */
/* int pixelValue; */
/* int mask; */
/* int bitShift; */
assert(img != NULL);
assert(channel < img->channelCount);
offset = dmtxImageGetByteOffset(img, x, y);
if(offset == DmtxUndefined)
return DmtxFail;
switch(img->bitsPerChannel[channel]) {
case 1:
/* assert(img->bitsPerPixel == 1);
mask = 0x01 << (7 - offset%8);
*value = (img->pxl[offset/8] & mask) ? 255 : 0; */
break;
case 5:
/* XXX might be expensive if we want to scale perfect 0-255 range */
/* assert(img->bitsPerPixel == 16);
pixelPtr = img->pxl + (offset * (img->bitsPerPixel/8));
pixelValue = (*pixelPtr << 8) | (*(pixelPtr+1));
bitShift = img->bitsPerPixel - 5 - img->channelStart[channel];
mask = 0x1f << bitShift;
*value = (((pixelValue & mask) >> bitShift) << 3); */
break;
case 8:
assert(img->channelStart[channel] % 8 == 0);
assert(img->bitsPerPixel % 8 == 0);
img->pxl[offset + channel] = value;
break;
}
return DmtxPass;
}
/**
* \brief Test whether image contains a coordinate expressed in integers
* \param img
* \param margin width
* \param x coordinate
* \param y coordinate
* \return DmtxTrue | DmtxFalse
*/
extern DmtxBoolean
dmtxImageContainsInt(DmtxImage *img, int margin, int x, int y)
{
assert(img != NULL);
if(x - margin >= 0 && x + margin < img->width &&
y - margin >= 0 && y + margin < img->height)
return DmtxTrue;
return DmtxFalse;
}
/**
* \brief Test whether image contains a coordinate expressed in floating points
* \param img
* \param x coordinate
* \param y coordinate
* \return DmtxTrue | DmtxFalse
*/
extern DmtxBoolean
dmtxImageContainsFloat(DmtxImage *img, double x, double y)
{
assert(img != NULL);
if(x >= 0.0 && x < (double)img->width && y >= 0.0 && y < (double)img->height)
return DmtxTrue;
return DmtxFalse;
}
/**
*
*
*/
static int
GetBitsPerPixel(int pack)
{
switch(pack) {
case DmtxPack1bppK:
return 1;
case DmtxPack8bppK:
return 8;
case DmtxPack16bppRGB:
case DmtxPack16bppRGBX:
case DmtxPack16bppXRGB:
case DmtxPack16bppBGR:
case DmtxPack16bppBGRX:
case DmtxPack16bppXBGR:
case DmtxPack16bppYCbCr:
return 16;
case DmtxPack24bppRGB:
case DmtxPack24bppBGR:
case DmtxPack24bppYCbCr:
return 24;
case DmtxPack32bppRGBX:
case DmtxPack32bppXRGB:
case DmtxPack32bppBGRX:
case DmtxPack32bppXBGR:
case DmtxPack32bppCMYK:
return 32;
default:
break;
}
return DmtxUndefined;
}