Fixed some prototype defination
[sudos/9s12.git] / sources / app / image / image_prc.c
blob6e7a0a170efb597b75cded69bb172412dda0a9a6
1 #include "image_prc.h"
3 #define DEBUG
5 unsigned char black=255;
6 unsigned char white =0;
8 struct track itrack[80];
10 int image_prc(char *data, int v, int h){
11 int t=0;
12 int i=0;
13 int ix,iy;
14 int tt=0;
16 t=image_head(data, v, h,itrack);
18 if(t<2){
19 itrack[0].x = -1;
20 itrack[0].y = -1;
21 //itrack[0].vector = 0;
22 return 0;
23 }else{
24 tt=image_trail(data,v,h,t,itrack);
25 itrack[tt+1].x=-1;
26 itrack[tt+1].y=-1;
29 #ifdef DEBUG
30 for(i=0;i<=tt; i++){
31 ix=itrack[i].x;
32 iy=itrack[i].y;
33 *(data+iy*h+ix)=255;
35 #endif
37 return tt;
43 int image_head(char *data, int v, int h,struct track *itrack){
45 int temp;
46 int i=0;
47 int pf=0,nf=0,j=0,k=0,t=0,offset=0,f=0,po=0,flag=0;
48 int left=h, right=0,it=0,local=0,crital=0;
52 local=12;
53 offset=80;
54 for(po=v-1; po>9; po--){
55 flag=0;
56 temp = h*(po-1);
57 pf=0;
58 left=h;
59 right =0;
60 nf=0;
62 if(po==40) {
63 offset=60;
64 crital=0;
66 if(po==35) offset=40;
67 if(po==25) offset=30;
68 if(po== 15) offset=10;
69 if(po<=40 && it==0) return 0;
71 for(i=local-12; i< h-6; i++){
72 if(i+5<h-1 && i>0){
73 j=(unsigned char)*(data+temp+i) ;
74 k=(unsigned char)*(data+temp+i+5) ;
75 if(j-k>offset && flag==0){
77 left=i+5;
78 i=i+5;
79 flag=1;
81 if(k-j>offset && flag==1){
82 right=i;
83 //*(data+i+temp)=255;
84 if(right-left<20){ //Add start line searching here
86 it++;
87 itrack[it].x = i+1;
88 itrack[it].y = po;
89 //itrack[it].vector = i - itrack[it-1].x;
90 local=i;
91 crital=0;
93 break;
94 }else {
95 flag=0;
100 if(i==h-6) crital++;
101 if(crital>=2 && po<40) return it;
102 po--;
105 return it;
108 int image_trail(char *data, int v, int h, int offset,struct track *itrack){
109 int i=0;
110 unsigned char j=0,k=0;
111 int xo,yo,vo;
112 int temp;
113 int step;
114 int crital=0,flag=0;
116 xo=itrack[offset].x;
117 yo=itrack[offset].y;
118 temp=itrack[offset-2].x;
120 if(yo<8) return offset;
121 if(xo-temp >-3 && xo-temp<3) return offset;
123 if(yo>10) step=12;
124 if(yo>20) step = 15;
125 if(yo>30) step=40;
126 if(xo-temp<-1){
127 while(xo>1){
128 xo--;
129 xo--;
130 //flag=0;
131 for(i=3; i>-5; i--){
132 if(yo+i-2>6){
133 j=*(data+(yo+i)*h+xo);
134 k=*(data+(yo+i-2)*h+xo);
135 if(j-k>step){
136 offset++;
137 itrack[offset].x=xo;
138 itrack[offset].y=yo+i-2;
139 yo=itrack[offset].y;
140 flag=1;
141 break;
145 if(flag==0) { crital++;
146 printf("cri:%d\n",crital);
147 printf("flag==1 crital:%d\n",crital);
148 if(crital>2){
149 return offset;
155 //if(crital>15) return offset;
157 }else{
158 while(xo<h-2){
159 xo++;
160 xo++;
161 flag=0;
162 for(i=3; i>-5; i--){
163 if(yo+i-2>6){
164 j=*(data+(yo+i)*h+xo);
165 k=*(data+(yo+i-2)*h+xo);
166 if(j-k>step){
167 offset++;
168 itrack[offset].x=xo;
169 itrack[offset].y=yo+i-2;
170 yo=itrack[offset].y;
171 flag=1;
172 //break;
176 if(flag==0){
177 printf("cri:%d\n",crital);
178 crital++;
179 if(crital>2){
180 return offset;
185 //if(crital>15) return offset;
189 return offset;
193 int image_body(char *data, int v, int h, char start){
195 int i=0,j=0,k=0,t=0,offset=0,f=0,po=0,flag=0,sline=0;
196 int scale=0,temp=0,vo=0,pos=0,neg=0,toggle=0,q=0,m=0;
197 int inthere=0;
199 po=itrack[start].x;
200 sline=itrack[start].y-1;
201 vo=itrack[start].vector;
203 offset=80;
204 f=start+1;
205 scale=18;
207 po+=vo;
209 for(t=sline; t>5; t--){
210 flag=0;
211 neg=0;
212 pos=0;
213 temp = h*t;
215 if(t==35) {
216 scale=16;
217 offset-=20;
219 if(t==50){
220 scale=14;
221 offset-=20;
223 for(i=0; i<scale; i++){
224 for(q=0; q<2; q++){
225 if(q==0) toggle=1;
226 else toggle=-1;
228 if(po+i*toggle+3<h && po+i*toggle>0) {
231 j=(unsigned char)*(data+temp+po+i*toggle) ;
232 k=(unsigned char)*(data+temp+po+i*toggle+2) ;
233 if(j-k>offset){
234 pos=1;
235 m=0;
237 while(i*toggle+po+3+m<h && po+i*toggle+m>0){
238 m=m+toggle;
239 j=(unsigned char)*(data+temp+po+i*toggle+m) ;
240 k=(unsigned char)*(data+temp+po+i*toggle+m+2) ;
242 if(k-j>offset){
243 //if(m<20 && m>-20){
244 //po=(pos+i*toggle+po)/2;
245 itrack[f].x=po+i*toggle+m;
246 itrack[f].y=t;
247 itrack[f].vector=i*toggle+m;
248 //*(data+temp+po+i)=255;
249 po=2*(i*toggle+m)+po;
250 flag=1;
251 break;
256 pos=0;
259 if(flag==1) break;
261 if(k-j>offset){
262 neg=1;
263 inthere = i*toggle;
264 //toggle=0-toggle;
265 m=0;
266 while(inthere+po+3+m<h && po+inthere+m>0){
267 m=m-toggle;
268 j=(unsigned char)*(data+temp+po+inthere+m) ;
269 k=(unsigned char)*(data+temp+po+inthere+m+2) ;
271 if(j-k>offset){
272 //if(m<20 && m>-20){
273 //po=(pos+inthere+po)/2;
274 itrack[f].x=po+inthere+m;
275 itrack[f].y=t;
276 itrack[f].vector=inthere+m;
277 //*(data+temp+po+i)=255;
278 po=2*(inthere+m)+po;
279 flag=1;
280 break;
284 neg=0;
287 if (flag==1) break;
291 if (flag==1) break;
294 if(i==scale) {
295 itrack[f].x=-1;
296 itrack[f].y=-1;
297 itrack[f].vector=0;
298 return f;
300 f++;
301 t--;
304 return f;
312 int image_trail_old_new(char *data, int v, int h, int offset){
314 int vo=0, po=0,line=0;
315 int pos=0, neg=0;
316 int i=0, j=0;
317 int temp=0;
318 int step=0,flag=0;
319 int pf=0,nf=0;
320 unsigned char m,n;
322 vo = itrack[offset].vector;
323 po = itrack[offset].x;
324 line = itrack[offset].y;
326 step=25;
327 for(line=offset; line>5; line--){
328 temp=h*line;
329 flag=0;
330 for(i=0; i<h-3; i++){
331 m=*(data+temp+i);
332 n=*(data+temp+i+2);
333 if(m-n>step && pf==0) {
335 pos=i;
336 pf=1;
339 if(n-m>step && pf==1){
340 neg=i;
341 nf=1;
342 if(neg-pos>1 && neg-pos<10){
343 itrack[offset++].x = (neg+pos)/2;
344 itrack[offset].y = line;
345 itrack[offset].vector=itrack[offset].x - itrack[offset-1].x;
347 break;
352 flag++;
353 if(flag==4){
354 itrack[offset++].x=-1;
355 itrack[offset].y=-1;
356 itrack[offset].vector=0;
358 return offset;
366 int image_body_old(char *data, int v, int h, char start){
368 int i=0,j=0;
370 int base=0;
371 int px=0;
372 int py=0;
373 unsigned char val=0;
374 char flag = 0;
375 int cry=0;
377 int cal = 0;
378 int temp=0;
379 int remote=0;
380 int iblack=0;
381 int li=0;
382 int k=0;
385 itrack[0].x = start;
386 itrack[0].y = v-1;
387 itrack[0].vector = 0;
388 black +=50;
389 //iblack=black;
391 for(i =0; i<50; i++){
393 //black = black+40;
394 //black=iblack;
395 //white=50;
396 //if(black >white) black=white-20;
397 //if(white>220) white=black;
398 flag=0;
399 remote=0;
400 li=6;
401 base = itrack[i].vector;
402 px = itrack[i].x;
403 py = itrack[i].y;
404 // printf("i%d: px=%d py=%d\n",i,px,py);
405 if(py<20 || px<2 || px>h-1) {
406 // itrack[i+1].x=-1;
407 // itrack[i+1].y=-1;
408 // itrack[i+1].vector=itrack[i].vector;
409 return i;
411 if(py<35){
412 py=py+2;
413 remote=1;
414 //black+=10;
415 li=6;
417 for(cal=0; cal < li; cal++){
418 for(j=0; j<2; j++){
419 if(j==0) temp = base+cal;
420 else temp = base -cal;
421 //printf("-%d",temp);
422 k=0;
423 switch (temp){
424 case 0:
426 if(py-4<0) break;
427 val = (unsigned char)*(data+(py-4)*h+px);
429 if(val < black) {
430 while(k<10){
431 if(*(data+(py-4)*h+px-k)>black) break;
432 //printf("--%d",*(data+(py-4)*h+px-k));
433 k++;
435 if(k<10){
436 *(data+(py-4)*h+px-k)=255;
437 //black=val;
438 itrack[i+1].x = px;
439 itrack[i+1].y = py-4;
440 itrack[i+1].vector =0;
441 flag =1;
444 break;
445 case 1:
446 if(py-4 <0 || px +2>h) break;
447 val = (unsigned char)*(data+(py-4)*h+px+1);
448 // printf("in case 1: black is %d, val is %d\n",black,val);
449 if(val < black) {
450 while(k<10){
451 if(*(data+(py-4)*h+px+1-k)>black) break;
452 k++;
454 if(k<10){
455 *(data+(py-4)*h+px+1-k)=255;//black=val;
456 itrack[i+1].x = px+1-k;
457 itrack[i+1].y = py-4;
458 itrack[i+1].vector =1;
459 flag =1;
462 break;
463 case -1:
464 if(py-4 <0 || px-1<0) break;
465 val = (unsigned char)*(data+(py-4)*h+px-1);
466 if(val < black) {
467 while(k<10){
468 if(*(data+(py-4)*h+px-1+k)>black) break;
469 k++;
471 if(k<10){
472 *(data+(py-4)*h+px-1+k)=255;//black=val;
473 itrack[i+1].x = px-1+k;
474 itrack[i+1].y = py-4;
475 itrack[i+1].vector =-1;
476 flag =1;
479 break;
480 case 2:
481 if(py-4 <0 || px+3>h) break;
482 val = (unsigned char)*(data+(py-4)*h+px+2);
483 if(val < black) {
484 while(k<10){
485 if(*(data+(py-4)*h+px+2-k)>black) break;
486 k++;
488 if(k<10){
489 *(data+(py-4)*h+px+2-k)=255;//black=val;
490 itrack[i+1].x = px+2-k;
491 itrack[i+1].y = py-4;
492 itrack[i+1].vector =2;
493 flag =1;
496 break;
497 case -2:
498 if(py-4 <0 || px -2<0) break;
499 val = (unsigned char)*(data+(py-4)*h+px-2);
500 if(val < black) {
501 while(k<10){
502 if(*(data+(py-4)*h+px-2+k)>black) break;
503 k++;
505 if(k<10){
506 *(data+(py-4)*h+px-2+k)=255;//black=val;
507 itrack[i+1].x = px-2+k;
508 itrack[i+1].y = py-4;
509 itrack[i+1].vector =-2;
510 flag =1;
513 break;
514 case 3:
515 if(py-4 <0 || px+3>h) break;
516 val = (unsigned char)*(data+(py-3)*h+px+2);
517 if(val < black) {
518 while(k<10){
519 if(*(data+(py-3)*h+px+2-k)>black) break;
520 k++;
522 if(k<10){
523 *(data+(py-3)*h+px+2-k)=255;//black=val;
524 itrack[i+1].x = px+2-k;
525 itrack[i+1].y = py-3;
526 itrack[i+1].vector =3;
527 flag =1;
530 break;
531 case -3:
532 if(py-4 <0 || px -2<0) break;
533 val = (unsigned char)*(data+(py-3)*h+px-2);
534 if(val < black) {
535 while(k<10){
536 if(*(data+(py-3)*h+px-2+k)>black) break;
537 k++;
539 if(k<10){
540 *(data+(py-3)*h+px-2+k)=255;//black=val;
541 itrack[i+1].x = px-2+k;
542 itrack[i+1].y = py-3;
543 itrack[i+1].vector =-3;
544 flag =1;
547 break;
548 case 4:
549 if(py-4 <0 || px+3>h) break;
550 val = (unsigned char)*(data+(py-2)*h+px+2);
551 if(val < black) {
552 while(k<10){
553 if(*(data+(py-2)*h+px+2-k)>black) break;
554 k++;
556 if(k<10){
557 *(data+(py-2)*h+px+2-k)=255;//black=val;
558 itrack[i+1].x = px+2-k;
559 itrack[i+1].y = py-2;
560 itrack[i+1].vector =4;
561 flag =1;
564 break;
565 case -4:
566 if(py-4 <0 || px -2<0) break;
567 val = (unsigned char)*(data+(py-2)*h+px-2);
568 if(val < black) {
569 while(k<10){
570 if(*(data+(py-2)*h+px-2+k)>black) break;
571 k++;
573 if(k<10){
574 *(data+(py-2)*h+px-2+k)=255;//black=val;
575 itrack[i+1].x = px-2+k;
576 itrack[i+1].y = py-2;
577 itrack[i+1].vector =-4;
578 flag =1;
581 break;
583 case 5:
584 if(py-4 <0 || px+3>h) break;
585 val = (unsigned char)*(data+(py-1)*h+px+2);
586 if(val < black) {
587 while(k<10){
588 if(*(data+(py-1)*h+px+2-k)>black) break;
589 k++;
591 if(k<10){
593 *(data+(py-1)*h+px+2+k)=255;//black=val;
594 itrack[i+1].x = px+2;
595 itrack[i+1].y = py-1;
596 itrack[i+1].vector =5;
597 flag =1;
600 break;
601 case -5:
602 if(py-4 <0 || px -2<0) break;
603 val = (unsigned char)*(data+(py-1)*h+px-2);
604 if(val < black) {
605 while(k<10){
606 if(*(data+(py-2)*h+px-2+k)>black) break;
607 k++;
609 if(k<10){
610 *(data+(py-1)*h+px-2)=255;//black=val;
611 itrack[i+1].x = px-2;
612 itrack[i+1].y = py-1;
613 itrack[i+1].vector =-5;
614 flag =1;
617 break;
618 case 6:
619 if(px+3>h ) break;
620 val = (unsigned char)*(data+(py)*h+px+2);
621 if(val < black) {
622 while(k<10){
623 if(*(data+(py)*h+px+2-k)>black) break;
624 k++;
626 if(k<10){
628 *(data+(py)*h+px+2-k)=255;//black=val;
629 itrack[i+1].x = px+2;
630 itrack[i+1].y = py;
631 itrack[i+1].vector =6;
632 flag =1;
635 break;
636 case -6:
637 if(px -2<0) break;
638 val = (unsigned char)*(data+(py)*h+px-2);
639 if(val < black) {
640 while(k<10){
641 if(*(data+(py-2)*h+px-2+k)>black) break;
642 k++;
644 if(k<10){
645 *(data+(py)*h+px-2)=255;//black=val;
646 itrack[i+1].x = px-2;
647 itrack[i+1].y = py;
648 itrack[i+1].vector =-6;
649 flag =1;
652 break;
654 case 7:
655 if(py+5> v ||px+3>h) break;
656 val = (unsigned char)*(data+(py+1)*h+px+2);
657 if(val < black) {
658 *(data+(py+1)*h+px+2)=255;//black=val;
659 itrack[i+1].x = px+2;
660 itrack[i+1].y = py+1;
661 itrack[i+1].vector =7;
662 flag =1;
664 break;
665 case -7:
666 if(py+5> v || px -2<0) break;
667 val = (unsigned char)*(data+(py+1)*h+px-2);
668 if(val < black) {
669 *(data+(py+1)*h+px-2)=255;//black=val;
670 itrack[i+1].x = px-2;
671 itrack[i+1].y = py+1;
672 itrack[i+1].vector =-7;
673 flag =1;
675 break;
676 case 8:
677 if(py+5> v || px+3>h) break;
678 val = (unsigned char)*(data+(py+2)*h+px+2);
679 if(val < black) {
680 *(data+(py+2)*h+px+2)=255;//black=val;
681 itrack[i+1].x = px+2;
682 itrack[i+1].y = py+2;
683 itrack[i+1].vector =8;
684 flag =1;
686 break;
687 case -8:
688 if(py+5> v|| px -2<0) break;
689 val = (unsigned char)*(data+(py+2)*h+px-2);
690 if(val < black) {
691 *(data+(py+2)*h+px-2)=255;//black=val;
692 itrack[i+1].x = px-2;
693 itrack[i+1].y = py+2;
694 itrack[i+1].vector =-8;
695 flag =1;
697 break;
698 case 9:
699 if(py+5> v || px+3>h) break;
700 val = (unsigned char)*(data+(py+3)*h+px+2);
701 if(val < black) {
702 *(data+(py+3)*h+px+2)=255;//black=val;
703 itrack[i+1].x = px+2;
704 itrack[i+1].y = py+3;
705 itrack[i+1].vector =9;
706 flag =1;
708 break;
709 case -9:
710 if(py+5> v ||px -2<0) break;
711 val = (unsigned char)*(data+(py+3)*h+px-2);
712 if(val < black) {
713 *(data+(py+3)*h+px-2)=255;//black=val;
714 itrack[i+1].x = px-2;
715 itrack[i+1].y = py+3;
716 itrack[i+1].vector =-9;
717 flag =1;
719 break;
720 case 10:
721 if(py+5> v || px+3>h ) break;
722 val = (unsigned char)*(data+(py+4)*h+px+2);
723 if(val < black) {
724 *(data+(py+4)*h+px+2)=255;//black=val;
725 itrack[i+1].x = px+2;
726 itrack[i+1].y = py+4;
727 itrack[i+1].vector =10;
728 flag =1;
730 break;
731 case -10:
732 if(py+5> v || px -2<0) break;
733 val = (unsigned char)*(data+(py+4)*h+px-2);
734 if(val < black) {
735 *(data+(py+4)*h+px-2)=255;//black=val;
736 itrack[i+1].x = px-2;
737 itrack[i+1].y = py+4;
738 itrack[i+1].vector =-10;
739 flag =1;
741 break;
742 case 11:
743 if(py+5> v || px+3>h) break;
744 val = (unsigned char)*(data+(py+4)*h+px+1);
745 if(val < black) {
746 *(data+(py+4)*h+px+1)=255;//black=val;
747 itrack[i+1].x = px+1;
748 itrack[i+1].y = py+4;
749 itrack[i+1].vector =11;
750 flag =1;
752 break;
753 case -11:
754 if(py+5> v || px -2<0) break;
755 val = (unsigned char)*(data+(py+4)*h+px-1);
756 if(val < black) {
757 *(data+(py+4)*h+px-1)=255;//black=val;
758 itrack[i+1].x = px-1;
759 itrack[i+1].y = py+4;
760 itrack[i+1].vector =-11;
761 flag =1;
763 break;
764 case 12:
765 case -12:
766 if(py+5> v) break;
767 val = (unsigned char)*(data+(py+4)*h+px);
768 if(val < black) {
769 *(data+(py+4)*h+px)=255;//black=val;
770 itrack[i+1].x = px;
771 itrack[i+1].y = py+4;
772 itrack[i+1].vector =12;
773 flag =1;
775 break;
777 default:
778 break;
782 if(py<35&& flag==1) {
783 if(itrack[i+1].vector<-1 || itrack[i+1].vector>1)
784 itrack[i+1].vector -=2;
785 break;
789 if(flag==1) break;
794 if(py>20&& flag==0){
795 itrack[i+1].x=-1;
796 itrack[i+1].y=-1;
797 itrack[i+1].vector=itrack[i].vector;
798 return i;
799 }else{
800 if(flag==0) {
801 if(cry>=3) return i;
802 else cry++;
803 if(remote==1) py=py-2;
804 if(py<10 || px<1 || px>h-2) {
805 itrack[i+1].x=-1;
806 itrack[i+1].y=-1;
807 itrack[i+1].vector=itrack[i].vector;
808 return i;
809 }else
811 if((unsigned char)*(data+(py-1)*h+px) < black) {
812 black = (unsigned char)*(data+(py-1)*h+px);
813 itrack[i+1].x=px;
814 itrack[i+1].y=py-1;
815 itrack[i+1].vector=itrack[i].vector;
817 }else
818 if((unsigned char)*(data+(py-1)*h+px+1) < black) {
819 black = (unsigned char)*(data+(py-1)*h+px+1);
820 itrack[i+1].x=px-1;
821 itrack[i+1].y=py+1;
822 itrack[i+1].vector=itrack[i].vector;
824 }else
825 if((unsigned char)*(data+(py-1)*h+px-1) < black) {
826 black = (unsigned char)*(data+(py-1)*h+px-1);
827 itrack[i+1].x=px-1;
828 itrack[i+1].y=py-1;
829 itrack[i+1].vector=itrack[i].vector;
831 }else
832 if((unsigned char)*(data+py*h+px+1) < black) {
833 black = (unsigned char)*(data+py*h+px+1);
834 itrack[i+1].x=px+2;
835 itrack[i+1].y=py;
836 itrack[i+1].vector=itrack[i].vector;
838 }else
839 if((unsigned char)*(data+py*h+px-1) < black) {
840 black = (unsigned char)*(data+py*h+px-1);
841 itrack[i+1].x=px-1;
842 itrack[i+1].y=py;
843 itrack[i+1].vector=itrack[i].vector;
845 } else{
846 itrack[i+1].x=-1;
847 itrack[i+1].y=-1;
848 itrack[i+1].vector=itrack[i].vector;
849 return i;
852 }else cry=0;
858 itrack[i+1].x=-1;
859 itrack[i+1].y=-1;
860 itrack[i+1].vector=itrack[i].vector;
861 return i;
864 int image_trail_old(char *data, int v, int h, int offset){
866 int x=0,y=0,vt=0;
867 int i=0,j=0,left=0,right=0,last=0;
870 while(j<10){
871 x=itrack[offset+j].x;
872 y=itrack[offset+j].y;
873 vt=itrack[offset+j].vector;
875 if(y<5 || x<1 || x>=h-1 || j+offset>49) return j;
876 //if(y<20) return j;
878 j++;
880 left=*(data+y*h+x-1)+*(data+(y-1)*h+x-1);
881 right=*(data+y*h+x+1)+*(data+(y-1)*h+x+1);
884 if(left==right){
885 if(last<0) right+=1;
886 else if(last>0) left+=1;
887 else return offset+j;
888 }else if(left<right){
889 last=-1;
890 if(*(data+y*h+x-1) < *(data+(y-1)*h+x-1)){
891 itrack[offset+j].x=x-1;
892 itrack[offset+j].y=y;
893 itrack[offset+j].vector=-6;
894 *(data+y*h+x-1)=255;
895 }else{
896 itrack[offset+j].x=x-1;
897 itrack[offset+j].y=y-1;
898 itrack[offset+j].vector=-5;
899 *(data+(y-1)*h+x-1)=255;
901 }else if(left>right){
902 last=1;
903 if(*(data+y*h+x+1) < *(data+(y-1)*h+x+1)){
904 itrack[offset+j].x=x+1;
905 itrack[offset+j].y=y;
906 itrack[offset+j].vector=6;
907 *(data+y*h+x+1)=255;
908 }else{
909 itrack[offset+j].x=x+1;
910 itrack[offset+j].y=y-1;
911 itrack[offset+j].vector=5;
912 *(data+(y-1)*h+x+1)=255;
917 return j;