Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * digitprep1.c 00018 * 00019 * Extract barcode digits and put in a pixaa (a resource file for 00020 * readnum.c). 00021 */ 00022 00023 #include <stdio.h> 00024 #include <stdlib.h> 00025 #include "allheaders.h" 00026 00027 static const l_int32 HEIGHT = 32; /* pixels */ 00028 00029 main(int argc, 00030 char **argv) 00031 { 00032 char buf[8]; 00033 l_int32 i, n, h; 00034 l_float32 scalefact; 00035 BOXA *boxa; 00036 PIX *pixs, *pix, *pixt1, *pixt2; 00037 PIXA *pixa, *pixas, *pixad; 00038 PIXAA *pixaa; 00039 static char mainName[] = "digitprep1"; 00040 00041 if (argc != 1) { 00042 ERROR_INT(" Syntax: digitprep1", mainName, 1); 00043 return 1; 00044 } 00045 00046 if ((pixs = pixRead("barcode-digits.png")) == NULL) 00047 return ERROR_INT("pixs not read", mainName, 1); 00048 00049 /* Extract the digits and scale to HEIGHT */ 00050 boxa = pixConnComp(pixs, &pixa, 8); 00051 pixas = pixaSort(pixa, L_SORT_BY_X, L_SORT_INCREASING, NULL, L_CLONE); 00052 n = pixaGetCount(pixas); 00053 00054 /* Move the last ("0") to the first position */ 00055 pixt1 = pixaGetPix(pixas, n - 1, L_CLONE); 00056 pixaInsertPix(pixas, 0, pixt1, NULL); 00057 pixaRemovePix(pixas, n); 00058 00059 /* Make the output scaled pixa */ 00060 pixad = pixaCreate(n); 00061 for (i = 0; i < n; i++) { 00062 pixt1 = pixaGetPix(pixas, i, L_CLONE); 00063 pixGetDimensions(pixt1, NULL, &h, NULL); 00064 scalefact = HEIGHT / (l_float32)h; 00065 pixt2 = pixScale(pixt1, scalefact, scalefact); 00066 if (pixGetHeight(pixt2) != 32) 00067 return ERROR_INT("height not 32!", mainName, 1); 00068 sprintf(buf, "%d", i); 00069 pixSetText(pixt2, buf); 00070 pixaAddPix(pixad, pixt2, L_INSERT); 00071 pixDestroy(&pixt1); 00072 } 00073 00074 /* Save in a pixaa, with 1 pix in each pixa */ 00075 pixaa = pixaaCreateFromPixa(pixad, 1, L_CHOOSE_CONSECUTIVE, L_CLONE); 00076 pixaaWrite("junkdigits.pixaa", pixaa); 00077 00078 /* Show result */ 00079 pixt1 = pixaaDisplayByPixa(pixaa, 20, 20, 1000); 00080 pixDisplay(pixt1, 100, 100); 00081 pixDestroy(&pixt1); 00082 00083 boxaDestroy(&boxa); 00084 pixaDestroy(&pixa); 00085 pixaDestroy(&pixas); 00086 pixaDestroy(&pixad); 00087 pixaaDestroy(&pixaa); 00088 return 0; 00089 } 00090 00091