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
|
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2010 Mike Laughton. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact: Mike Laughton <mike@dragonflylogic.com>
*
* \file dmtxsobel.c
*/
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <assert.h>
#include "../../dmtx.h"
#include "multi_test.h"
/**
*
*
*/
DmtxSobel *
SobelCreate(DmtxImage *img)
{
int sWidth, sHeight;
DmtxSobel *sobel;
sobel = (DmtxSobel *)calloc(1, sizeof(DmtxSobel));
if(sobel == NULL)
return NULL;
sWidth = dmtxImageGetProp(img, DmtxPropWidth) - 2;
sHeight = dmtxImageGetProp(img, DmtxPropHeight) - 2;
sobel->v = dmtxValueGridCreate(sWidth, sHeight, DmtxEdgeVertical, NULL);
sobel->b = dmtxValueGridCreate(sWidth, sHeight, DmtxEdgeBackslash, NULL);
sobel->h = dmtxValueGridCreate(sWidth, sHeight, DmtxEdgeHorizontal, NULL);
sobel->s = dmtxValueGridCreate(sWidth, sHeight, DmtxEdgeSlash, NULL);
if(sobel->v == NULL || sobel->b == NULL || sobel->h == NULL || sobel->s == NULL)
{
SobelDestroy(&sobel);
return NULL;
}
return sobel;
}
/**
*
*
*/
DmtxPassFail
SobelDestroy(DmtxSobel **sobel)
{
if(sobel == NULL || *sobel == NULL)
return DmtxFail;
dmtxValueGridDestroy(&((*sobel)->s));
dmtxValueGridDestroy(&((*sobel)->h));
dmtxValueGridDestroy(&((*sobel)->b));
dmtxValueGridDestroy(&((*sobel)->v));
free(*sobel);
*sobel = NULL;
return DmtxPass;
}
/**
* 3x3 Sobel Kernel
*
*/
DmtxPassFail
SobelPopulate(DmtxDecode2 *dec)
{
int bytesPerPixel, rowSizeBytes, colorPlane;
int sx, sy;
int py, pOffset;
int vMag, bMag, hMag, sMag;
int colorLoLf, colorLoMd, colorLoRt;
int colorMdRt, colorHiRt, colorHiMd;
int colorHiLf, colorMdLf, colorMdMd;
int idx;
int sWidth, sHeight;
DmtxSobel *sobel = dec->sobel;
DmtxImage *img = dec->image;
assert(dec != NULL);
sobel = dec->sobel;
img = dec->image;
assert(sobel != NULL && img != NULL);
sWidth = dmtxImageGetProp(img, DmtxPropWidth) - 2;
sHeight = dmtxImageGetProp(img, DmtxPropHeight) - 2;
rowSizeBytes = dmtxImageGetProp(img, DmtxPropRowSizeBytes);
bytesPerPixel = dmtxImageGetProp(img, DmtxPropBytesPerPixel);
colorPlane = 1; /* XXX need to make some decisions here */
for(sy = 0; sy < sHeight; sy++)
{
py = sHeight - sy;
pOffset = py * rowSizeBytes + colorPlane;
colorHiLf = img->pxl[pOffset - rowSizeBytes];
colorMdLf = img->pxl[pOffset];
colorLoLf = img->pxl[pOffset + rowSizeBytes];
pOffset += bytesPerPixel;
colorHiMd = img->pxl[pOffset - rowSizeBytes];
colorMdMd = img->pxl[pOffset];
colorLoMd = img->pxl[pOffset + rowSizeBytes];
pOffset += bytesPerPixel;
colorHiRt = img->pxl[pOffset - rowSizeBytes];
colorMdRt = img->pxl[pOffset];
colorLoRt = img->pxl[pOffset + rowSizeBytes];
for(sx = 0; sx < sWidth; sx++)
{
/**
* -1 0 1
* -2 0 2
* -1 0 1
*/
vMag = colorHiRt;
vMag += colorMdRt * 2;
vMag += colorLoRt;
vMag -= colorHiLf;
vMag -= colorMdLf * 2;
vMag -= colorLoLf;
/**
* 0 1 2
* -1 0 1
* -2 -1 0
*/
bMag = colorMdLf;
bMag += colorLoLf * 2;
bMag += colorLoMd;
bMag -= colorMdRt;
bMag -= colorHiRt * 2;
bMag -= colorHiMd;
/**
* 1 2 1
* 0 0 0
* -1 -2 -1
*/
hMag = colorHiLf;
hMag += colorHiMd * 2;
hMag += colorHiRt;
hMag -= colorLoLf;
hMag -= colorLoMd * 2;
hMag -= colorLoRt;
/**
* -2 -1 0
* -1 0 1
* 0 1 2
*/
sMag = colorLoMd;
sMag += colorLoRt * 2;
sMag += colorMdRt;
sMag -= colorHiMd;
sMag -= colorHiLf * 2;
sMag -= colorMdLf;
/**
* If implementing these operations using MMX, can load 2
* registers with 4 doubleword values and subtract (PSUBD).
*/
idx = sy * sWidth + sx;
sobel->v->value[idx] = vMag;
sobel->b->value[idx] = bMag;
sobel->h->value[idx] = hMag;
sobel->s->value[idx] = sMag;
colorHiLf = colorHiMd;
colorMdLf = colorMdMd;
colorLoLf = colorLoMd;
colorHiMd = colorHiRt;
colorMdMd = colorMdRt;
colorLoMd = colorLoRt;
pOffset += bytesPerPixel;
colorHiRt = img->pxl[pOffset - rowSizeBytes];
colorMdRt = img->pxl[pOffset];
colorLoRt = img->pxl[pOffset + rowSizeBytes];
}
}
dec->fn.dmtxValueGridCallback(sobel->v, 0);
dec->fn.dmtxValueGridCallback(sobel->b, 1);
dec->fn.dmtxValueGridCallback(sobel->h, 2);
dec->fn.dmtxValueGridCallback(sobel->s, 3);
return DmtxPass;
}
|