-
Notifications
You must be signed in to change notification settings - Fork 0
/
AUCR.c
2161 lines (1931 loc) · 75.3 KB
/
AUCR.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//AUCR.c
#include "AUCR.h"
/* ==============================================
FUNCTION IMPLEMENTATIONS
============================================== */
/*
* Name:
* AUCR_CHARACTER_Init()
* Description:
* Initializes a new AUCR_Character instance by allocating appropriate
* memories and setting member values.
* Parameters:
* i_unicode -> the unicode value to be represented by the structure.
* i_num_directional_codes -> i_number of directional codes to allocate memory for.
* i_num_activity_regions -> i_number of activity regions for to allocate
* activity measure memories for.
* o_character_ptr <- memory to be populated with the new AUCR_Character.
* Preconditions:
* i_num_directional_codes and i_num_activity_regions must be positive,
* non-zero values. i_unicode should not be 0x0000 which is reserved as an
* error character. o_character_ptr must be NULL.
* Postconditions:
* o_character_ptr is the address of a newly allocated and initialized
* AUCR_Character. Its directional_codes is a new array of
* (num_directional_codes + 1) elements. Its activity_measures is a new
* array of num_activity_regions elements
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_NO_MEMORY if the structure or array memories could not be allocated
*/
AUCR_Error AUCR_CHARACTER_Init(
wchar_t i_unicode,
int i_num_directional_codes,
int i_num_activity_regions,
AUCR_Character_Ptr * o_character_ptr_ptr )
{
//check for valid input
if( ( i_unicode == 0x0000 ) || ( i_num_directional_codes <= 0 ) ||
( i_num_activity_regions <= 0 ) || ( o_character_ptr_ptr == NULL ) ||
( ( * o_character_ptr_ptr ) != NULL ) )
{
return( AUCR_ERR_FAILED );
}
//malloc memory for character struct
( * o_character_ptr_ptr ) = (AUCR_Character_Ptr)malloc( sizeof( AUCR_Character ) );
if( ( * o_character_ptr_ptr ) == NULL )
{
return( AUCR_ERR_NO_MEMORY );
}
//malloc memory for directional codes
( * o_character_ptr_ptr )->directional_codes =
(int *)malloc( (i_num_directional_codes ) * sizeof( int ) );
if( ( * o_character_ptr_ptr )->directional_codes == NULL )
{
free( * o_character_ptr_ptr );
return( AUCR_ERR_NO_MEMORY );
}
//malloc memory for activity codes
( * o_character_ptr_ptr )->activity_measures =
(int *)malloc( i_num_activity_regions * sizeof( int ) );
if( ( * o_character_ptr_ptr )->activity_measures == NULL )
{
free( * o_character_ptr_ptr );
free( ( * o_character_ptr_ptr )->directional_codes );
return( AUCR_ERR_NO_MEMORY );
}
//memory allocations successful
//now fill in fields
( * o_character_ptr_ptr )->unicode = i_unicode;
( * o_character_ptr_ptr )->num_directional_codes = i_num_directional_codes;
( * o_character_ptr_ptr )->num_activity_regions = i_num_activity_regions;
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_INTERPOLATED_CHARACTER_Release()
* Description:
* Frees all dynamically allocated memories for the AUCR_Interpolated_Character and NULLs
* the AUCR_Interpolated_Character_Ptr.
* Parameters:
* io__interpolatedcharacter_ptr <-> address of the AUCR_Interpolated_Character memory to be freed.
* Preconditions:
* io__interpolatedcharacter_ptr is a valid address of an AUCR_Interpolated_Character.
* Postconditions:
* The character's coordinate array is freed followed by the memory for the
* character as well. io_interpolated_character_ptr is NULL.
* Returns:
* none
*/
void AUCR_CHARACTER_Release(
AUCR_Character_Ptr * io_character_ptr_ptr )
{
//check for valid input
if( ( io_character_ptr_ptr == NULL ) || ( * io_character_ptr_ptr == NULL ) )
{
return;
}
//free directional code array
free( ( * io_character_ptr_ptr )->directional_codes );
//free activity measure array
free( ( * io_character_ptr_ptr )->activity_measures );
//free character structure
free( * io_character_ptr_ptr );
//set pointer to null
io_character_ptr_ptr = NULL;
return;
}
/*
* Name:
* AUCR_CHARACTER_From_Interpolated()
* Description:
* takes in an interpolated character and an alphabet and forms the character
* representation of the interpolated character based on the alphabet.
* Parameters:
* i_interpolated_character_ptr -> a pointer to an interpolated character
* i_alphabet_ptr -> a pointer to an alphabet
* io_character_ptr <-> a pointer to a character that we will fill in
* Preconditions:
* i_interpolated_character_ptr is non NULL and is a pointer to a valid
* interpolated character, i_alphabet_ptr is non NULL and is a pointer to
* a valid alphabet, io_character_ptr is non NULL and is a pointer to a valid
* character, num_directional_codes between the alphabet and the interpolated
* are in agreement
* Postconditions:
* io_character_ptr is populated with the character version of the
* interpolated character
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_CONFLICTING_PARAMETERS if all preconditions except the agreement of
* num_directional_codes are met
*/
AUCR_Error AUCR_CHARACTER_From_Interpolated(
AUCR_Interpolated_Character_Ptr i_interpolated_character_ptr,
AUCR_Alphabet_Ptr i_alphabet_ptr,
AUCR_Character_Ptr io_character_ptr )
{
//local variables
int i, j;
int code, dx, dy, begin, end, max;
int * hits;
//check for valid input
if( i_interpolated_character_ptr == NULL ||
i_alphabet_ptr == NULL || io_character_ptr == NULL )
{
return( AUCR_ERR_FAILED );
}
else if( i_interpolated_character_ptr->num_directional_codes
!= i_alphabet_ptr->num_directional_codes ||
i_interpolated_character_ptr->num_directional_codes
!= io_character_ptr->num_directional_codes ||
io_character_ptr->num_directional_codes
!= i_alphabet_ptr->num_directional_codes )
{
return( AUCR_ERR_CONFLICTING_PARAMETERS );
}
hits = (int *)malloc( sizeof( int ) *
( i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes ) );
if( hits == NULL )
{
return( AUCR_ERR_NO_MEMORY );
}
//find all directional codes
for( i = 0; i < i_alphabet_ptr->num_directional_codes; i++ )
{
//intialize code to invalid value
code = -1;
//find change in x
dx = i_interpolated_character_ptr->coordinates[i + 1].x -
i_interpolated_character_ptr->coordinates[i].x;
//find change in y
dy = i_interpolated_character_ptr->coordinates[i + 1].y -
i_interpolated_character_ptr->coordinates[i].y;
//setup for search in right halfplane
if( dx > 0 )
{
if( i_alphabet_ptr->directional_code_map_ptr->positive_begin == -1 )
{
code = i_alphabet_ptr->directional_code_map_ptr->negative_end;
}
begin = i_alphabet_ptr->directional_code_map_ptr->positive_begin;
end = i_alphabet_ptr->directional_code_map_ptr->positive_end;
}
//setup for search in left halfplane
else
{
if( i_alphabet_ptr->directional_code_map_ptr->negative_begin == -1 )
{
code = i_alphabet_ptr->directional_code_map_ptr->positive_end;
}
begin = i_alphabet_ptr->directional_code_map_ptr->negative_begin;
end = i_alphabet_ptr->directional_code_map_ptr->negative_end;
}
if( code == -1 )
{
//start at the directional code that begins the quadrant, and advance thru the directional
//codes until you find one that has slope greater than the slope of the present vector,
//or until you reach the end of the quadrant
for ( j = begin; j != (( end + 1 ) %
( i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes ) );
j = (( j + 1 ) %
( i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes ) ) )
{
if ( dy * i_alphabet_ptr->directional_code_map_ptr->x[j] <
dx * i_alphabet_ptr->directional_code_map_ptr->y[j] )
{
code =
( ( j + i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes - 1 ) %
( i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes ) );
break;
}
}
}
//if for loop went until the end of the quadrant,
//point must be in the last area of the quadrant
if ( code == -1 )
{
code = end;
}
//direction is now in code
//store directional code i
io_character_ptr->directional_codes[i] = code;
}
//zero out counts for all directions
for( i = 0; i <
i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes; i++ )
{
hits[i] = 0;
}
//find activity for all regions
for( i = 0; i < i_alphabet_ptr->num_activity_regions; i++ )
{
for( j = i_alphabet_ptr->activity_regions[i].start;
j <= i_alphabet_ptr->activity_regions[i].stop; j++ )
{
hits[io_character_ptr->directional_codes[j]]++;
}
max = 0;
for( j = 0; j <
i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes; j++ )
{
if( hits[j] > max )
{
max = hits[j];
}
hits[j] = 0;
}
io_character_ptr->activity_measures[i] =
AUCR_Rounding_Divide( ( AUCR_SCALE *
( 1 + i_alphabet_ptr->activity_regions[i].stop
- i_alphabet_ptr->activity_regions[i].start ) ),
( max ) );
}
free( hits );
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_INTERPOLATED_CHARACTER_Init()
* Description:
* Initializes a new AUCR_Interpolated_Character instance by allocating appropriate
* memories and setting member values.
* Parameters:
* i_unicode -> the unicode value to be represented by the structure.
* i_num_directional_codes -> (i_num_directional_codes + 1) elements of the
* coordinates vector must have memory allocated.
* o_interpolated_character_ptr_ptr <- address of the AUCR_Interpolated_Character_Ptr
* to be populated with the new AUCR_Interpolated_Character.
* Preconditions:
* i_num_directional_codes must be positive and non-zero. i_unicode should not be
* 0x0000 which is reserved as an error character. The AUCR_Interpolated_Character_Ptr
* at o_interpolated_character_ptr_ptr must be NULL.
* Postconditions:
* o_character_ptr_Ptr is the address of a newly allocated and initialized
* AUCR_Interpolated_Character_Ptr. Its coordinates is a new array of
* (num_directional_codes + 1) "AUCR_Coordinate"s.
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_NO_MEMORY if the structure or array memories could not be allocated
*/
AUCR_Error AUCR_INTERPOLATED_CHARACTER_Init(
wchar_t i_unicode,
int i_num_directional_codes,
AUCR_Interpolated_Character_Ptr * o_interpolated_character_ptr_ptr )
{
//check for valid input
if( ( i_unicode == 0x0000 ) || ( i_num_directional_codes < 1 ) ||
( o_interpolated_character_ptr_ptr == NULL ) ||
( ( * o_interpolated_character_ptr_ptr ) != NULL ) )
{
return( AUCR_ERR_FAILED );
}
//malloc memory for character struct
( * o_interpolated_character_ptr_ptr ) =
( AUCR_Interpolated_Character_Ptr )malloc( sizeof( AUCR_Interpolated_Character ) );
if( ( * o_interpolated_character_ptr_ptr ) == NULL )
{
return( AUCR_ERR_NO_MEMORY );
}
//malloc memory for coordinates
( * o_interpolated_character_ptr_ptr )->coordinates =
( AUCR_Coordinate_Ptr )malloc( (i_num_directional_codes + 1 )
* sizeof( AUCR_Coordinate ) );
if( ( * o_interpolated_character_ptr_ptr )->coordinates == NULL )
{
free( * o_interpolated_character_ptr_ptr );
return( AUCR_ERR_NO_MEMORY );
}
//memory allocations successful
//now fill in fields
( * o_interpolated_character_ptr_ptr )->unicode = i_unicode;
( * o_interpolated_character_ptr_ptr )->num_directional_codes =
i_num_directional_codes;
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_INTERPOLATED_CHARACTER_Release()
* Description:
* Frees all dynamically allocated memories for the AUCR_Interpolated_Character and NULLs
* the AUCR_Interpolated_Character_Ptr.
* Parameters:
* io__interpolatedcharacter_ptr <-> address of the AUCR_Interpolated_Character memory to be freed.
* Preconditions:
* io__interpolatedcharacter_ptr is a valid address of an AUCR_Interpolated_Character.
* Postconditions:
* The character's coordinate array is freed followed by the memory for the
* character as well. io_interpolated_character_ptr is NULL.
* Returns:
* none
*/
void AUCR_INTERPOLATED_CHARACTER_Release(
AUCR_Interpolated_Character_Ptr * io_interpolated_character_ptr_ptr )
{
//check for valid input
if( ( io_interpolated_character_ptr_ptr == NULL ) ||
( ( * io_interpolated_character_ptr_ptr ) == NULL ) )
{
return;
}
//free coordinate array
free( ( * io_interpolated_character_ptr_ptr )->coordinates );
//free character structure
free( ( * io_interpolated_character_ptr_ptr ) );
//set pointer to null
( * io_interpolated_character_ptr_ptr ) = NULL;
return;
}
/*
* Name:
* AUCR_INTERPOLATED_CHARACTER_From_Raw()
* Description:
* Converts an array of AUCR_Coordinates (captured by a GUI) to an AUCR_Interpolated_Character.
* Parameters:
* i_num_coordinates -> i_number of coordinates in the i_coordinates array.
* i_coordinates -> array of raw pen coordinates
* io_interpolated_character_ptr <-> address of the AUCR_Interpolated_Character
* to be populated from the raw data
* Preconditions:
* io_interpolated_character_ptr is a valid address of an initialized AUCR_Interpolated_Character.
* Postconditions:
* io_interpolated_character_ptr is populated with the interpolated version if the raw
* coordinates.
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_NO_MEMORY if the structure or array memories could not be allocated
*/
AUCR_Error AUCR_INTERPOLATED_CHARACTER_From_Raw(
int i_num_coordinates,
AUCR_Coordinate * i_coordinates,
AUCR_Interpolated_Character_Ptr io_interpolated_character_ptr )
{
//declare variables
//array of distances for each point
unsigned int * distances;
//holds scale factor if needed
int tempscalar;
//holds length all segments will be
int seglength;
//index of point currently being looked at
int currentcoord = 1;
//parameter in parametric equations for doing interpolations
int t;
//loop index
int i;
//check for valid input
if( io_interpolated_character_ptr == NULL ||
i_num_coordinates < 1 )
{
return( AUCR_ERR_FAILED );
}
//get memory for distance array
distances = (unsigned int *)malloc( i_num_coordinates * sizeof( unsigned int ) );
if( distances == NULL )
{
return( AUCR_ERR_NO_MEMORY );
}
distances[0] = 0;
//loop to calculate distances
for(i = 1; i < i_num_coordinates; i++ )
{
distances[i] = distances[i - 1] + AUCR_Integer_Square_Root(
( ( i_coordinates[i].x - i_coordinates[i - 1].x ) *
( i_coordinates[i].x - i_coordinates[i - 1].x ) ) +
( ( i_coordinates[i].y - i_coordinates[i - 1].y ) *
( i_coordinates[i].y - i_coordinates[i - 1].y ) ) );
}
//case 1: input has no distance
if( distances[i_num_coordinates - 1] < 1 )
{
//fill in entire coordinate array with zeros
for( i = 0; i <= io_interpolated_character_ptr->num_directional_codes; i++ )
{
io_interpolated_character_ptr->coordinates[i].x = 0;
io_interpolated_character_ptr->coordinates[i].y = 0;
}
}
//case 2: input has distance
else
{
//make sure input has long enough distance
//
//the ( io_interpolated_character_ptr->num_directional_codes * AUCR_SCALE * 2 )
//term is the scaling cut off factor and should have a noticeable impact on
//the efficiency and accuracy of the interpolation algorithm.
//
//for smaller input devices, it may be better to go ahead and just scale
//everything without even checking how much total information we read in.
//
//the larger the term is (i.e. increasing the 2 to a 5) should give us a
//better interpolation and make the resulting segmented curve smoother,
//but on the other hand making it too large makes us run the risk of
//overflow in longer cases and will also force us to rescale more often
//which may not have adequate run time performance
//
if(distances[i_num_coordinates - 1] <
(unsigned int)( io_interpolated_character_ptr->num_directional_codes * AUCR_SCALE * 10 ) )
{
//too short, so we will scale
//scale factor
tempscalar = AUCR_Rounding_Divide(
io_interpolated_character_ptr->num_directional_codes * AUCR_SCALE * 20,
distances[i_num_coordinates - 1] );
//scale first raw coordinate
i_coordinates[0].x = tempscalar * i_coordinates[0].x;
i_coordinates[0].y = tempscalar * i_coordinates[0].y;
//scale all subsequent raw coordinates and distances
for( i = 1; i < i_num_coordinates; i++ )
{
i_coordinates[i].x = tempscalar * i_coordinates[i].x;
i_coordinates[i].y = tempscalar * i_coordinates[i].y;
distances[i] = tempscalar * distances[i];
}
}
//first interpolated point is the same as first raw point
io_interpolated_character_ptr->coordinates[0].x = i_coordinates[0].x;
io_interpolated_character_ptr->coordinates[0].y = i_coordinates[0].y;
//find segment length
seglength = AUCR_Rounding_Divide( distances[i_num_coordinates - 1],
io_interpolated_character_ptr->num_directional_codes );
//interpolate middle points
for( i = 1; i < io_interpolated_character_ptr->num_directional_codes; i++ )
{
//while the total distance from the start to the current coordinate
//is less than the current multiple of the segment length
while( ( distances[currentcoord] ) < (unsigned int)( i * seglength ) )
{
//move to the next coordinate
currentcoord++;
}
//calculate parameter t that will give us the interpolated point
//when used in parametric equations for the current segment
t = AUCR_Rounding_Divide( (unsigned int) ( ( i * seglength ) -
distances[currentcoord - 1] ), AUCR_Rounding_Divide(
( distances[currentcoord] -
distances[currentcoord - 1] ), AUCR_SCALE ) );
//parametric equation for x
io_interpolated_character_ptr->coordinates[i].x =
i_coordinates[currentcoord - 1].x + AUCR_Rounding_Divide(
( ( i_coordinates[currentcoord].x -
i_coordinates[currentcoord - 1].x ) * t ), AUCR_SCALE );
//parametric equation for y
io_interpolated_character_ptr->coordinates[i].y =
i_coordinates[currentcoord - 1].y + AUCR_Rounding_Divide(
( ( i_coordinates[currentcoord].y -
i_coordinates[currentcoord - 1].y ) * t ), AUCR_SCALE );
}
//last interpolated point is the same as last raw point
io_interpolated_character_ptr->coordinates[i].x =
i_coordinates[i_num_coordinates - 1].x;
io_interpolated_character_ptr->coordinates[i].y =
i_coordinates[i_num_coordinates - 1].y;
}
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_INTERPOLATED_CHARACTER_From_Character()
* Description:
* takes in a character and the alphabet that formed the character, as
* well as a rectangle (in pixels) and the border inside the rectangle
* and returns an interpolated character whose coordinates fill a
* rectangle of the given size while always staying a distance of
* border pixels from the edge of the rectangle.
* note: this function is primarily intended to be used as a mechanism
* to get points that can be written to screen, such that a character
* may be displayed. it does not return an interpolated character
* that is identical to the interpolated character that was used to
* create the character in the alphabet.
* Parameters:
* i_character_ptr -> a pointer to a character
* i_alphabet_ptr -> a pointer to the alphabet that was used to create the character
* i_rectangle_width -> the width in pixels of the bounding rectangle for the points
* in the interpolated character
* i_rectangle_height -> the height in pixels of the bounding rectangle for the points
* in the interpolated character
* i_rectangle_border -> the closest distance in pixels that any point in the
* interpolated character can come to the edge of the
* bounding rectangle
* io_interpolated charactercharacter_ptr <-> a pointer to an interpolated character
* that will be filled in by extrapolating information from the character
* Preconditions:
* io_interpolated_character_ptr is non NULL and is a pointer to a valid
* interpolated character, i_alphabet_ptr is non NULL and is a pointer to
* a valid alphabet, io_character_ptr is non NULL and is a pointer to a valid
* character, i_rectangle_width is positive, i_rectangle_height is positive,
* i_rectangle_border is non negative, and num_directional_codes between
* the alphabet, character, and the interpolated character are in agreement
* Postconditions:
* io_interpolated_character_ptr is populated with the interpolated version of the
* character in i_character_ptr
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_CONFLICTING_PARAMETERS if all preconditions except the agreement of
* num_directional_codes are met
*/
AUCR_Error AUCR_INTERPOLATED_CHARACTER_From_Character(
AUCR_Character_Ptr i_character_ptr,
AUCR_Alphabet_Ptr i_alphabet_ptr,
int i_rectangle_width,
int i_rectangle_height,
int i_rectangle_border,
AUCR_Interpolated_Character_Ptr io_interpolated_character_ptr )
{
//local variables
int i, xoff, yoff, scale, div, nextDirCode;
int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
//check for valid input
if( io_interpolated_character_ptr == NULL ||
i_alphabet_ptr == NULL || i_character_ptr == NULL )
{
return( AUCR_ERR_FAILED );
}
else if( io_interpolated_character_ptr->num_directional_codes
!= i_alphabet_ptr->num_directional_codes ||
io_interpolated_character_ptr->num_directional_codes
!= i_character_ptr->num_directional_codes ||
i_character_ptr->num_directional_codes
!= i_alphabet_ptr->num_directional_codes )
{
return( AUCR_ERR_CONFLICTING_PARAMETERS );
}
//start at ( 0, 0 )
io_interpolated_character_ptr->coordinates[0].x = 0;
io_interpolated_character_ptr->coordinates[0].y = 0;
//calculate remaining points, using a scale of AUCR_SCALE
//and an offset of ( 0, 0 )
for( i = 0; i < i_alphabet_ptr->num_directional_codes; i++ )
{
if( i_character_ptr->directional_codes[i] ==
( i_alphabet_ptr->directional_code_map_ptr->mappable_directional_codes - 1 ) )
{
nextDirCode = 0;
}
else
{
nextDirCode = i_character_ptr->directional_codes[i] + 1;
}
div = AUCR_Integer_Square_Root(
( ( i_alphabet_ptr->directional_code_map_ptr->x[i_character_ptr->directional_codes[i]] +
i_alphabet_ptr->directional_code_map_ptr->x[nextDirCode] ) *
( i_alphabet_ptr->directional_code_map_ptr->x[i_character_ptr->directional_codes[i]] +
i_alphabet_ptr->directional_code_map_ptr->x[nextDirCode] ) ) +
( ( i_alphabet_ptr->directional_code_map_ptr->y[i_character_ptr->directional_codes[i]] +
i_alphabet_ptr->directional_code_map_ptr->y[nextDirCode] ) *
( i_alphabet_ptr->directional_code_map_ptr->y[i_character_ptr->directional_codes[i]] +
i_alphabet_ptr->directional_code_map_ptr->y[nextDirCode] ) ) );
io_interpolated_character_ptr->coordinates[i + 1].x = ( AUCR_Rounding_Divide(
( i_alphabet_ptr->directional_code_map_ptr->x[i_character_ptr->directional_codes[i]]
+ i_alphabet_ptr->directional_code_map_ptr->x[nextDirCode] ) * AUCR_SCALE,
div ) + io_interpolated_character_ptr->coordinates[i].x );
io_interpolated_character_ptr->coordinates[i + 1].y = ( AUCR_Rounding_Divide(
( i_alphabet_ptr->directional_code_map_ptr->y[i_character_ptr->directional_codes[i]]
+ i_alphabet_ptr->directional_code_map_ptr->y[nextDirCode] ) * AUCR_SCALE,
div ) + io_interpolated_character_ptr->coordinates[i].y );
if( io_interpolated_character_ptr->coordinates[i + 1].x > xmax )
{
xmax = io_interpolated_character_ptr->coordinates[i + 1].x;
}
if( io_interpolated_character_ptr->coordinates[i + 1].x < xmin )
{
xmin = io_interpolated_character_ptr->coordinates[i + 1].x;
}
if( io_interpolated_character_ptr->coordinates[i + 1].y > ymax )
{
ymax = io_interpolated_character_ptr->coordinates[i + 1].y;
}
if( io_interpolated_character_ptr->coordinates[i + 1].y < ymin )
{
ymin = io_interpolated_character_ptr->coordinates[i + 1].y;
}
}
//calculate scale and offsets to map points to rectangle
if( ( ( ymax - ymin ) * i_rectangle_width ) > ( ( xmax - xmin ) * i_rectangle_height ) )
{
//y is the constraining direction
if( ( ymax - ymin ) < 10 * ( i_rectangle_height - ( 2 * i_rectangle_border ) ) )
{
scale = AUCR_Rounding_Divide(
20 * ( i_rectangle_height - ( 2 * i_rectangle_border ) ), ( ymax - ymin ) );
xmax = xmax * scale;
ymax = ymax * scale;
xmin = xmin * scale;
ymin = ymin * scale;
for( i = 0; i <= i_alphabet_ptr->num_directional_codes; i++ )
{
io_interpolated_character_ptr->coordinates[i].x =
io_interpolated_character_ptr->coordinates[i].x * scale;
io_interpolated_character_ptr->coordinates[i].y =
io_interpolated_character_ptr->coordinates[i].y * scale;
}
}
scale = ( i_rectangle_height - ( 2 * i_rectangle_border ) );
div = ( ymax - ymin );
yoff = i_rectangle_border - AUCR_Rounding_Divide( ymin * scale, div );
xoff = i_rectangle_border - AUCR_Rounding_Divide( xmin * scale, div )
+ AUCR_Rounding_Divide( i_rectangle_width - ( 2 *
i_rectangle_border ) - AUCR_Rounding_Divide(
( xmax - xmin ) * scale, div ), 2 );
}
else
{
//x is the constraining direction
if( ( xmax - xmin ) < 10 * ( i_rectangle_width - ( 2 * i_rectangle_border ) ) )
{
scale = AUCR_Rounding_Divide(
20 * ( i_rectangle_width - ( 2 * i_rectangle_border ) ), ( xmax - xmin ) );
xmax = xmax * scale;
ymax = ymax * scale;
xmin = xmin * scale;
ymin = ymin * scale;
for( i = 0; i <= i_alphabet_ptr->num_directional_codes; i++ )
{
io_interpolated_character_ptr->coordinates[i].x =
io_interpolated_character_ptr->coordinates[i].x * scale;
io_interpolated_character_ptr->coordinates[i].y =
io_interpolated_character_ptr->coordinates[i].y * scale;
}
}
scale = ( i_rectangle_width - ( 2 * i_rectangle_border ) );
div = ( xmax - xmin );
xoff = i_rectangle_border - AUCR_Rounding_Divide( xmin * scale, div );
yoff = i_rectangle_border - AUCR_Rounding_Divide( ymin * scale, div )
+ AUCR_Rounding_Divide( i_rectangle_height - ( 2 *
i_rectangle_border ) - AUCR_Rounding_Divide(
( ymax - ymin ) * scale, div ), 2 );
}
//loop through all points, scaling, then offsetting them
for( i = 0; i <= i_alphabet_ptr->num_directional_codes; i++ )
{
io_interpolated_character_ptr->coordinates[i].x = xoff +
AUCR_Rounding_Divide(
io_interpolated_character_ptr->coordinates[i].x * scale, div );
io_interpolated_character_ptr->coordinates[i].y = yoff +
AUCR_Rounding_Divide(
io_interpolated_character_ptr->coordinates[i].y * scale, div );
}
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_DIRECTIONAL_CODE_MAP_Init()
* Description:
* Initializes a new AUCR_Directional_Code_Map instance by allocating appropriate
* memories and setting member values.
* Parameters:
* i_mappable_directional_codes -> i_number of unique directional codes to allocate memory
* for their mappings.
* o_directional_code_map_ptr_ptr <- memory address of the AUCR_Directional_Code_Map_Ptr
* to be populated with the new AUCR_Directional_Code_Map.
* Preconditions:
* i_mappable_directional_codes must be a positive, non-zero values.
* The AUCR_Directional_Code_Map_Ptr at o_directional_code_map_ptr_ptr must be NULL.
* Postconditions:
* o_directional_code_map_ptr_ptr is the address of a newly allocated and initialized
* AUCR_Directional_Code_Map_Ptr. Its "x" and "y" variables are newly allocated arrays
* of (mappable_directional_codes) elements... each element set to zero.
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
* AUCR_ERR_NO_MEMORY if the structure or array memories could not be allocated
*/
AUCR_Error AUCR_DIRECTIONAL_CODE_MAP_Init(
int i_mappable_directional_codes,
AUCR_Directional_Code_Map_Ptr * o_directional_code_map_ptr_ptr )
{
//declare local variables
int i;
//check for valid input
if( ( i_mappable_directional_codes < 1 ) || ( o_directional_code_map_ptr_ptr == NULL )
|| ( ( * o_directional_code_map_ptr_ptr ) != NULL ) )
{
return( AUCR_ERR_FAILED );
}
//malloc space for Directional_Code_Map
( * o_directional_code_map_ptr_ptr ) =
( AUCR_Directional_Code_Map_Ptr )malloc( sizeof( AUCR_Directional_Code_Map ) );
if( ( * o_directional_code_map_ptr_ptr ) == NULL )
{
return( AUCR_ERR_NO_MEMORY );
}
//malloc space for x array
( * o_directional_code_map_ptr_ptr )->x =
(int *)malloc( sizeof( int ) * ( i_mappable_directional_codes ) );
if( ( * o_directional_code_map_ptr_ptr )->x == NULL )
{
free( * o_directional_code_map_ptr_ptr );
return( AUCR_ERR_NO_MEMORY );
}
//malloc space for y array
( * o_directional_code_map_ptr_ptr )->y =
(int *)malloc( sizeof( int ) * ( i_mappable_directional_codes ) );
if( ( * o_directional_code_map_ptr_ptr )->y == NULL )
{
free( * o_directional_code_map_ptr_ptr );
free( ( * o_directional_code_map_ptr_ptr )->x );
return( AUCR_ERR_NO_MEMORY );
}
//memory allocation all good
//fill in structure
( * o_directional_code_map_ptr_ptr )->mappable_directional_codes =
i_mappable_directional_codes;
//zero out array entries
for( i = 0; i < ( i_mappable_directional_codes ); i++ )
{
( * o_directional_code_map_ptr_ptr )->x[i] = 0;
( * o_directional_code_map_ptr_ptr )->y[i] = 0;
}
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_DIRECTIONAL_CODE_MAP_Release()
* Description:
* Frees all dynamically allocated memories for the AUCR_Directional_Code_Map
* and NULLs the AUCR_Directional_Code_Map_Ptr.
* Parameters:
* io_directional_code_map_ptr_ptr <-> address of the AUCR_Directional_Code_Map memory to be freed.
* Preconditions:
* The AUCR_Directional_Code_Map_Ptr at io_directional_code_map_ptr_ptr
* is a valid address of an AUCR_Directional_Code_Map.
* Postconditions:
* The AUCR_Directional_Code_Map's "x" and "y" arrays are freed
* followed by the memory for the AUCR_Directional_Code_Map as well.
* The AUCR_Directional_Code_Map_Ptr at io_directional_code_map_ptr_ptr
* is NULL.
* Returns:
* none
*/
void AUCR_DIRECTIONAL_CODE_MAP_Release(
AUCR_Directional_Code_Map_Ptr * io_directional_code_map_ptr_ptr )
{
//check for valid input
if( ( io_directional_code_map_ptr_ptr == NULL ) ||
( ( * io_directional_code_map_ptr_ptr ) == NULL ) )
{
return;
}
//free y array
free( ( * io_directional_code_map_ptr_ptr )->y );
//free x array
free( ( * io_directional_code_map_ptr_ptr )->x );
//free Directional_Code_Map structure
free( * io_directional_code_map_ptr_ptr );
//NULL the structure pointer
( * io_directional_code_map_ptr_ptr ) = NULL;
return;
}
/*
* Name:
* AUCR_DIRECTIONAL_CODE_MAP_Finalize()
* Description:
* Prepares a directional code map instance for use in converting from interpolated
* characters to characters in the alphabet by setting up data structures required for
* arbitrary directional code lookup from arbitrary vectors.
* Parameters:
* io_directional_code_map_ptr <- pointer to AUCR_Directional_Code_Map that has been
* filled in completely with mappable_direction_codes (x, y) pairs.
* Preconditions:
* io_directional_code_map_ptr is non NULL and points to a valid directional code map
* that has mappable_directional_codes (x,y) pairs that each describe the vector that
* the begginning of a directional region, and all directional vectors, are in
* counterclockwise order, starting from the zeroeth direcional code
* Postconditions:
* positive_begin, positive_end, negative_begin, and negative_end have all been set
* to their appropriate values.
* Returns:
* AUCR_ERR_SUCCESS
* AUCR_ERR_FAILED if preconditions are unmet.
*/
AUCR_Error AUCR_DIRECTIONAL_CODE_MAP_Finalize(
AUCR_Directional_Code_Map_Ptr io_directional_code_map_ptr )
{
//declare local variables
int i;
int posBeginX = 0;
int posBeginY = 1;
int posEndX = 0;
int posEndY = -1;
int negBeginX = 0;
int negBeginY = -1;
int negEndX = 0;
int negEndY = 1;
//check for valid input
if( io_directional_code_map_ptr == NULL )
{
return( AUCR_ERR_FAILED );
}
//intitialize structure variables to their not found state
io_directional_code_map_ptr->positive_begin = -1;
io_directional_code_map_ptr->positive_end = -1;
io_directional_code_map_ptr->negative_begin = -1;
io_directional_code_map_ptr->negative_end = -1;
//find correct values for structure variables, if they exist
for( i = 0; i < io_directional_code_map_ptr->mappable_directional_codes; i++ )
{
if( io_directional_code_map_ptr->x[i] > 0 )
{
if( ( io_directional_code_map_ptr->x[i] * posBeginY ) >= ( io_directional_code_map_ptr->y[i] * posBeginX ) )
{
io_directional_code_map_ptr->positive_begin = i;
posBeginX = io_directional_code_map_ptr->x[i];
posBeginY = io_directional_code_map_ptr->y[i];
}
if( ( io_directional_code_map_ptr->x[i] * posEndY ) <= ( io_directional_code_map_ptr->y[i] * posEndX ) )
{
io_directional_code_map_ptr->positive_end = i;
posEndX = io_directional_code_map_ptr->x[i];
posEndY = io_directional_code_map_ptr->y[i];
}
}
else
{
if( ( io_directional_code_map_ptr->x[i] * negBeginY ) >= ( io_directional_code_map_ptr->y[i] * negBeginX ) )
{
io_directional_code_map_ptr->negative_begin = i;
negBeginX = io_directional_code_map_ptr->x[i];
negBeginY = io_directional_code_map_ptr->y[i];
}
if( ( io_directional_code_map_ptr->x[i] * negEndY ) <= ( io_directional_code_map_ptr->y[i] * negEndX ) )
{
io_directional_code_map_ptr->negative_end = i;
negEndX = io_directional_code_map_ptr->x[i];
negEndY = io_directional_code_map_ptr->y[i];
}
}
}
//return success
return( AUCR_ERR_SUCCESS );
}
/*
* Name:
* AUCR_ALPHABET_Init()
* Description:
* Initializes a new AUCR_ALPHABET instance by allocating appropriate
* memories and setting member values.
* Parameters:
* i_directional_code_map_ptr -> the memory address of a directional code
* map structure that is used to fill in the directional
* code map used by the alphabet.
* i_num_activity_regions -> number of activity regions for to allocate
* activity measure memories for.
* i_num_directional_codes -> number of directional codes to allocate memory
* for.
* o_alphabet_ptr_ptr <- memory address of the AUCR_Alphabet_Ptr to be
* populated with the new AUCR_Alphabet_Ptr which points to
* new AUCR_Alphabet.
* Preconditions:
* The *AUCR_Alphabet_Ptr at o_alphabet_ptr_ptr
* is a valid address of a AUCR_Alphabet_Prt that
* is currently set to NULL. i_directional_code_map_ptr
* is a valide pointer. i_num_activity_regions,
* and i_num_directional_codes, are positive, nonzero numbers.
* Postconditions:
* the structure pointed to by i_directional_code_map_ptr has been copied,
* the values held by i_num_activity_regions and i_num_directional_codes
* have been stored, bias is new array of biases(length i_num_activity_regions),
* activity_regions is a new array of activity regions (length i_num_activity_regions),
* characters_ptr_ptr is set to NULL (to prepare it for realloc), num_characters