00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019
00020
00021 #include "_gocr.h"
00022 #include <string.h>
00023
00024 00025 00026
00027 gocrBlock *currentblock = NULL;
00028
00029 00030 00031
00032 List blocklist;
00033 static HashTable blocktypelist;
00034 static gocrblockType currentblocktype = 1;
00035
00036 00037 00038
00039 int _gocr_initBlock ( void ) {
00040 char *basetypes[3] = { "TEXT", "PICTURE", "MATH_EXPRESSION" };
00041 int i;
00042
00043 hash_init(&blocktypelist, 0xFF, NULL);
00044
00045 for ( i = 0; i < sizeof(basetypes)/sizeof(char *); i++ )
00046 gocr_blockTypeRegister(basetypes[i]);
00047
00048 return 0;
00049 }
00050
00051 void _gocr_endBlock ( void ) {
00052
00053 hash_free(&blocktypelist, NULL);
00054
00055 }
00056
00057 00058 00059
00060
00061 00062
00063
00072 gocrblockType gocr_blockTypeRegister ( char *name ) {
00073 _gocr_debug(3, fprintf(_data.error, "gocr_blockTypeRegister(%s): %d\n", name, currentblocktype);)
00074
00075 if ( !name ) {
00076 _gocr_debug(1, fprintf(_data.error, "Error: gocr_blockTypeRegister(NULL).\n");)
00077 return -1;
00078 }
00079
00080 /* insert */
00081 switch ( hash_insert(&blocktypelist, name, (void *)currentblocktype) ) {
00082 case -1:
00083 _gocr_debug(1, fprintf(_data.error, "Error: hash_insert returned -1.\n");)
00084 return -1;
00085 case -2:
00086 _gocr_debug(1, fprintf(_data.error, "Error: block already registered. Ignoring.\n");)
00087 return -1;
00088 default:
00089 break;
00090 }
00091
00092 return currentblocktype++;
00093 }
00094
00103 gocrblockType gocr_blockTypeGetByName ( char *name ) {
00104 void *ret;
00105
00106 _gocr_debug(3, fprintf(_data.error, "gocr_blockTypeGetByName(%s): %d\n", name,
00107 currentblocktype);)
00108
00109 ret = hash_data(&blocktypelist, name);
00110 if ( ret == NULL )
00111 return -1;
00112 return (gocrblockType) ret;
00113 }
00114
00123 char *gocr_blockTypeGetNameByType ( gocrblockType t ) {
00124 _gocr_debug(3, fprintf(_data.error, "gocr_blockTypeGetNameByType(%d)\n", t);)
00125
00126 return hash_key(&blocktypelist, (void *)t);
00127 }
00128
00137 int gocr_blockAdd ( gocrBlock *b ) {
00138 int i, j;
00139
00140 _gocr_debug(3, fprintf(_data.error, "gocr_blockAdd(%p)\n", b);)
00141
00142 /* checks if blocktype is registered */
00143 if ( b->t >= currentblocktype ) {
00144 _gocr_debug(1, fprintf(_data.error, "Block type not registered, ignoring block\n");)
00145 return -1;
00146 }
00147
00148 /* check boundaries */
00149 _gocr_fixParameters ( &b->x0, &b->y0, &b->x1, &b->y1 );
00150 if ( b->x0 < 0 || b->y0 < 0 || b->x1 >= currentimage->x || b->y1 >= currentimage->y ) {
00151 _gocr_debug(1, fprintf(_data.error, "Block is out of bounds\n");)
00152 return -1;
00153 }
00154
00155 /* check for overlap */
00156 if ( _data.block_overlap == 0 ) {
00157 gocrBlock *t;
00158 int ret = 0;
00159
00160 for_each_data(&blocklist) {
00161 t = (gocrBlock *)list_get_current(&blocklist);
00162 if ( t->x0 < b->x0 && t->x1 > b->x0 && t->y0 < b->y0 && t->y1 > b->y0 ) {
00163 ret = 1;
00164 break;
00165 }
00166 if ( t->x0 < b->x1 && t->x1 > b->x1 && t->y0 < b->y1 && t->y1 > b->y1 ) {
00167 ret = 1;
00168 break;
00169 }
00170 } end_for_each(&blocklist);
00171 if ( ret == 1 ) {
00172 _gocr_debug(2, fprintf(_data.error, "Block overlap\n");)
00173 return -2;
00174 }
00175 }
00176
00177 /* set image */
00178 if ( _gocr_imageSharedCopy(currentimage, b->x0, b->x1, b->y0, b->y1,
00179 &b->image) == -1 ) {
00180 _gocr_debug(2, fprintf(_data.error, "Image share error\n");)
00181 return -1;
00182 }
00183
00184 /* fill the isblock field */
00185 for ( i = b->x0; i <= b->x1; i++ )
00186 for ( j = b->x0; j <= b->x1; j++ )
00187 currentimage->data[i][j].isblock = 1;
00188
00189 /* append to block list */
00190 if ( list_app(&blocklist, b) ) {
00191 _gocr_debug(2, fprintf(_data.error, "List error\n");)
00192 return -1;
00193 }
00194
00195 return 0;
00196 }