Exemple #1
0
int16_t Alik_define_cut_points(
    pchar raster_frag,
    struct own_cut *ans,
    int16_t dx,                   /* рабочая ширина растра          */
    int16_t dy,                   /* рабочая высота растра          */
    int16_t row
)

{
    int16_t    hor_byte, ver_byte, nshort, CP, i, j, bl_up, bl_dw, tret_h;
    pint16_t   penalty, cut_points, adr_cut_points, my_penalty;
    pchar  adrw, adrw_two, product, product_two, trace, adr_raster, stek, adr_ras_two,
    SourceRaster;
    puchar  IntBuf, CountCut, UpBlackPoint;
    struct own_cut *ans_ptr;
#ifdef AlikBl
    char snap[380], *buf = snap;
#endif
    Z = Z;
    bl_up = bbs2;
    bl_dw = bbs3;
    adr_cut_points = (pint16_t)RCUTP_ForRaster3;
    ans_ptr = ans;
    hor_byte = (dx + 7) >> 3;
    ver_byte = (dy + 7) >> 3;
    CP = ver_byte * dx;
    i = hor_byte * dy;
    tret_h = (dy << 1) / 3;
    adr_raster = (pchar)RCUTP_MemForCutPoints;       /* под исходный транспонированный растр */
    adrw = adr_raster + CP;             /* под обработанный транспонированный растр */
    trace = adrw + (CP > i ? CP : i);  /* под обход  */
    penalty = (pint16_t)trace + dx;       /* под штрафы */
    product = (pchar)(penalty + dx);   /* под произведения последовательных столбцов */
    cut_points = (pint16_t)(product + dx); /* под точки разрезания */
    adrw_two = (pchar)RCUTP_MemForCutPointsTwo;
    adr_ras_two = adrw_two + (CP > i ? CP : i);
    product_two = adr_ras_two + (CP > i ? CP : i); /* под произведения последовательных столбцов слева и справа*/
    UpBlackPoint = (puchar)(product_two + dx);
    CountCut    = (puchar)(UpBlackPoint + dx);
    SourceRaster = (pchar)(CountCut + dx);
    my_penalty  = (pint16_t)(SourceRaster + (CP > i ? CP : i));
    IntBuf      = (puchar)(my_penalty + dx);
    CP = (dx < 3 || dy < 3) ? 0 : 1;   /* резать не будем, если растр мал по одному из направлений */

    if (CP) {
        memcpy(SourceRaster, raster_frag, hor_byte*dy);
        memset((puchar)adr_cut_points, 0, R_S);
        memset(CountCut, 0, dx);
        Alik_tr_bit_matr(ver_byte, dy, raster_frag, adr_raster, hor_byte, dx); /* транспонирование исходного растра */
        memset((pint16_t)trace, 0, sizeof(int16_t)*dx);    /* зануление двух байтовых массивов */
        memset(penalty, 0, sizeof(int16_t)*dx);        /* зануление массива штрафов */
        CP = Alik_del_detail(raster_frag, dx, (int16_t)(dy * hor_byte), penalty);

        if (!CP && dx > 20 && dx <= 128) CP = 1;   //10-09-96 03:24pm  Alik  cten33.tif

        if (!CP) {                     /* растр распался */
            memset(penalty, 0, sizeof(int16_t)*dx);       /* зануление массива штрафов */
            memcpy(adr_raster, adrw, ver_byte*dx);
        }

        else
            Alik_tr_bit_matr(ver_byte, dy, raster_frag, adrw, hor_byte, dx);   /* транспонирование битовой матрицы */

        if (CP) CP = Alik_search_bound_point_in_raster(adrw, ver_byte, dx);

        if (CP) {    /*  растр непуст */
            Alik_form_bound(adrw, dx, dy, ver_byte, trace, 0);
            memcpy(adr_ras_two, adrw, ver_byte*dx);
            memcpy(adrw_two, adrw, ver_byte*dx);
            memcpy(my_penalty, penalty, sizeof(int16_t)*dx);
            Alik_CountCut(adrw, dy, dx, CountCut, IntBuf);
            Alik_UpBlackPoint(adrw, dy, dx, UpBlackPoint);
            Alik_cut_short(adrw, adrw_two, dx, ver_byte, product, penalty, cut_points, product_two);
            nshort = *cut_points;
            Alik_cut_hole(trace, dx, cut_points, nshort, dy, product, penalty, 0x00);
            Alik_del_equal_hole(cut_points, product, (pint16_t)penalty, dx, dy, nshort);
#ifdef AlikBl

            if ( db_status && snap_activity('j') && snap_activity('a'))
                Alik_snap_show_raster(raster_frag, SourceRaster, "raster mode", dy, dx,
                                      product, product_two, penalty);

#endif

            if (language == LANGUAGE_RUSSIAN)
                Alik_find_brus(raster_frag, SourceRaster, adr_ras_two, (int16_t)(bl_dw - bl_up), dy, dx,
                               product, product_two, penalty, cut_points, CountCut,
                               UpBlackPoint);

#ifdef AlikBl
            buf = snap;
            buf += sprintf(buf, "bl_up=%2d,bl_dw=%2d   ", bl_up, bl_dw);
#endif
            Alik_my_correct_base_lines(&bl_up, &bl_dw, dy, dx, row, (puchar)adr_ras_two,
                                       (uint16_t*)penalty, IntBuf);
#ifdef AlikBl
            buf += sprintf(buf, "bl_up=%2d,bl_dw=%2d   ", bl_up, bl_dw);
#endif
            Alik_correct_base_lines(trace, dx, dy, &bl_up, &bl_dw);
#ifdef AlikBl
            buf += sprintf(buf, "bl_up=%2d,bl_dw=%2d \n", bl_up, bl_dw); //,up_position); // Bound
            buf += sprintf(buf, "bbs1=%2d, bbs2=%2d, bbs3=%2d, bbs4=%2d,  minrow=%2d,  row=%2d \n",
                           bbs1, bbs2, bbs3, bbs4, minrow, row);
            buf += sprintf(buf, "Nb1=%2d,  Nb2=%2d,  Nb3=%2d,  Nb4=%2d,  up=%2d,  dw=%2d",
                           Nb1, Nb2, Nb3, Nb4, up_position, dw_position);

            if ( db_status && snap_activity('j'))
                Alik_snap_show_raster(raster_frag, SourceRaster, snap, dy, dx,
                                      product, product_two, penalty);

#endif

            if (bl_up >= 0 && bl_dw <= dy) {
                Alik_double_serif(cut_points, trace, product, dx, dy, bl_up, bl_dw, penalty,
                                  (puchar)adr_ras_two, IntBuf);
                Alik_cut_d(trace + dx, cut_points, product, dx, dy, bl_up, bl_dw, (pchar)penalty);
                Alik_defis(trace, cut_points, dx, dy, bl_up, bl_dw, penalty);

                if (language == LANGUAGE_ENGLISH)
                    Alik_cut_l(cut_points, trace, dx, bl_dw, penalty);
            }

            if (language == LANGUAGE_RUSSIAN)
                Alik_cut_y(raster_frag, SourceRaster, adr_ras_two, dy, dx, cut_points,
                           IntBuf, row, product, product_two, penalty, CountCut);

            if (language == LANGUAGE_RUSSIAN)
                Alik_rus_bad_c(raster_frag, SourceRaster, dy, dx, product, product_two,
                               penalty, cut_points, CountCut, UpBlackPoint);

            Alik_del_equal(cut_points);
            Alik_del_3_cut(adrw, cut_points, nshort, ver_byte, dx, CountCut);

            if (abs((row + dy) - (minrow + bbs3)) > 3)
                Alik_del_p_cut(adrw, cut_points, dx, dy, nshort, (char)(bl_dw - bl_up - 3));

            if (language == LANGUAGE_RUSSIAN)
                Alik_Check_Rus_D(raster_frag, SourceRaster, dy, dx, cut_points, IntBuf, row,
                                 product, product_two, penalty, CountCut);

            Alik_del_equal(cut_points);
            CP = *cut_points;

            if (CP) {
                CP = Alik_sort(cut_points, (pint16_t)adr_cut_points, dx);

                if (CP)  CP = Alik_del_doubl_cut((pint16_t)adr_cut_points, product, penalty, CP);
            }

            CP = MIN(STK_H - 2, CP);
        }

        if (!CP)  Alik_tr_bit_matr(hor_byte, dx, adr_raster, raster_frag, ver_byte, dy);

        if (CP) {
            Alik_cor_pnt((pint16_t)adr_cut_points, penalty, cut_points, CP, dx, dy, ver_byte, (pchar)adrw, (pchar)trace);
            Alik_form_bound(adr_raster, dx, dy, ver_byte, trace, 1);
            stek = (pchar)(penalty + ((CP + 1) << 2) - (CP + 1));  /* переписываем фрагменты трассы обхода */
            cut_points = penalty + 3 * (CP - 1) + 1;

            for (j = CP; j > 0; j--, cut_points -= 4, stek++) {
                *stek = (char) * cut_points++;

                if (*stek) (*stek)--;

                *++stek = dy - *cut_points;

                if (*stek) (*stek)--;
            }

            trace = (pchar)(penalty + ((CP + 1) << 2) - (CP + 1));

            for (j = CP - 1, i = 0; j >= 0; j--, i += 2) {
                ans_ptr->x   = (char) * ((pint16_t)adr_cut_points + j);
                ans_ptr->h   = dy - *(trace + i);
                ans_ptr->dh  = dy + 1 - *(trace + i + 1) - *(trace + i);
                ans_ptr->dh  = (char)MIN(ans_ptr->dh, dy);
                ans_ptr->dh  = MIN(ans_ptr->dh, ans_ptr->h);

                if (ans_ptr->dh < tret_h)   ans_ptr->var = 1;

                else                        ans_ptr->var = 8;

                ans_ptr++;
            }

            ans_ptr->x = 127;
            qsort((void *)(ans), CP, sizeof(struct own_cut), Alik_sort_function);
            Alik_set_method_for_cut_points(ans, dy, dx, CountCut);
            Alik_cor_height_and_var(CP, ans, my_penalty, dx, dy);
            Alik_new_points(&CP, ans, ans_ptr, dy, dx, my_penalty, product);
            //AK! posible stack crash there when ans pointed to last vector element!
            qsort((void *)(ans), CP, sizeof(struct own_cut), Alik_sort_function);
        }
    }

    return CP;
}
int16_t Alik_define_cut_points(
  pchar raster_frag,
  struct own_cut *ans,
  int16_t dx,                  /* ࠡ��� �ਭ� ����          */
  int16_t dy,                   /* ࠡ��� ���� ����          */
  int16_t row
)

{
 int16_t    hor_byte,ver_byte,nshort,CP,i,j,bl_up,bl_dw,tret_h;
 pint16_t   penalty,cut_points,adr_cut_points,my_penalty;
 pchar  adrw,adrw_two,product,product_two,trace,adr_raster,stek,adr_ras_two,
        SourceRaster;
 puchar  IntBuf,CountCut,UpBlackPoint;
 struct own_cut *ans_ptr;
#ifdef AlikBl
char snap[380],*buf=snap;
#endif

 Z=Z;
 bl_up=bbs2;
 bl_dw=bbs3;
 adr_cut_points=(pint16_t)ForRaster3;
 ans_ptr=ans;
 hor_byte=(dx+7)>>3;
 ver_byte=(dy+7)>>3;
 CP=ver_byte*dx;
 i=hor_byte*dy;
 tret_h = (dy<<1)/3;

 adr_raster=(pchar)MemForCutPoints;        /* ��� ��室�� �࠭ᯮ��஢���� ���� */
 adrw=adr_raster+CP;                /* ��� ��ࠡ�⠭�� �࠭ᯮ��஢���� ���� */
 trace=adrw+(CP>i?CP:i);            /* ��� ��室  */
 penalty=(pint16_t)trace+dx;            /* ��� ����� */
 product=(pchar)(penalty+dx);       /* ��� �ந�������� ��᫥����⥫��� �⮫�殢 */
 cut_points=(pint16_t)(product+dx);     /* ��� �窨 ࠧ१���� */

 adrw_two=(pchar)MemForCutPointsTwo;
 adr_ras_two = adrw_two+(CP>i?CP:i);
 product_two = adr_ras_two+(CP>i?CP:i); /* ��� �ந�������� ��᫥����⥫��� �⮫�殢 ᫥�� � �ࠢ�*/
 UpBlackPoint= (puchar)(product_two+dx);
 CountCut    = (puchar)(UpBlackPoint+dx);
 SourceRaster= (pchar)(CountCut+dx);
 my_penalty  = (pint16_t)(SourceRaster+(CP>i?CP:i));
 IntBuf      = (puchar)(my_penalty+dx);

 CP = (dx<3||dy<3)? 0 : 1;          /* १��� �� �㤥�, �᫨ ���� ��� �� ������ �� ���ࠢ����� */

if(CP)
 {
   memcpy(SourceRaster,raster_frag,hor_byte*dy);
   memset((puchar)adr_cut_points,0,R_S);
   memset(CountCut,0,dx);

   Alik_tr_bit_matr(ver_byte,dy,raster_frag,adr_raster,hor_byte,dx); /* �࠭ᯮ��஢���� ��室���� ���� */
   memset((pint16_t)trace,0,sizeof(int16_t)*dx);      /* ���㫥��� ���� ���⮢�� ���ᨢ�� */
   memset(penalty,0,sizeof(int16_t)*dx);          /* ���㫥��� ���ᨢ� ���䮢 */
   CP=Alik_del_detail(raster_frag,dx,(int16_t)(dy*hor_byte),penalty);
   if(!CP && dx>20 && dx<=128) CP=1;   //10-09-96 03:24pm  Alik  cten33.tif
   if(!CP)                        /* ���� �ᯠ��� */
    {
     memset(penalty,0,sizeof(int16_t)*dx);        /* ���㫥��� ���ᨢ� ���䮢 */
     memcpy(adr_raster,adrw,ver_byte*dx);
    }
   else
     Alik_tr_bit_matr(ver_byte,dy,raster_frag,adrw,hor_byte,dx);    /* �࠭ᯮ��஢���� ��⮢�� ������ */

   if(CP) CP=Alik_search_bound_point_in_raster(adrw,ver_byte,dx);

   if(CP)       /*  ���� ������ */
    {
     Alik_form_bound(adrw,dx,dy,ver_byte,trace,0);
     memcpy(adr_ras_two,adrw,ver_byte*dx);
     memcpy(adrw_two,adrw,ver_byte*dx);
     memcpy(my_penalty,penalty,sizeof(int16_t)*dx);

     Alik_CountCut(adrw,dy,dx,CountCut,IntBuf);
     Alik_UpBlackPoint(adrw,dy,dx,UpBlackPoint);
     Alik_cut_short(adrw,adrw_two,dx,ver_byte,product,penalty,cut_points,product_two);
     nshort=*cut_points;
     Alik_cut_hole(trace,dx,cut_points,nshort,dy,product,penalty,0x00);
     Alik_del_equal_hole(cut_points,product,(pint16_t)penalty,dx,dy,nshort);

#ifdef AlikBl
     if( db_status && snap_activity('j') && snap_activity('a'))
      Alik_snap_show_raster(raster_frag,SourceRaster,"raster mode",dy,dx,
                           product,product_two,penalty);
#endif
     if(language==LANG_RUSSIAN)
       Alik_find_brus(raster_frag,SourceRaster,adr_ras_two,(int16_t)(bl_dw-bl_up),dy,dx,
                      product,product_two,penalty,cut_points,CountCut,
		      UpBlackPoint);
#ifdef AlikBl
     buf=snap;
     buf+=sprintf(buf,"bl_up=%2d,bl_dw=%2d   ",bl_up,bl_dw);
#endif
     Alik_my_correct_base_lines(&bl_up,&bl_dw,dy,dx,row,(puchar)adr_ras_two,
                                (PWORD)penalty,IntBuf);
#ifdef AlikBl
     buf+=sprintf(buf,"bl_up=%2d,bl_dw=%2d   ",bl_up,bl_dw);
#endif
     Alik_correct_base_lines(trace,dx,dy,&bl_up,&bl_dw);
#ifdef AlikBl
     buf+=sprintf(buf,"bl_up=%2d,bl_dw=%2d \n",bl_up,bl_dw); //,up_position); // Bound
     buf+=sprintf(buf,"bbs1=%2d, bbs2=%2d, bbs3=%2d, bbs4=%2d,  minrow=%2d,  row=%2d \n",
                  bbs1,bbs2,bbs3,bbs4,minrow,row);
     buf+=sprintf(buf,"Nb1=%2d,  Nb2=%2d,  Nb3=%2d,  Nb4=%2d,  up=%2d,  dw=%2d",
                  Nb1,Nb2,Nb3,Nb4,up_position,dw_position);
     if( db_status && snap_activity('j'))
      Alik_snap_show_raster(raster_frag,SourceRaster,snap,dy,dx,
                           product,product_two,penalty);
#endif

     if(bl_up>=0 && bl_dw<=dy)
      {
       Alik_double_serif(cut_points,trace,product,dx,dy,bl_up,bl_dw,penalty,
                         (puchar)adr_ras_two,IntBuf);
       Alik_cut_d(trace+dx,cut_points,product,dx,dy,bl_up,bl_dw,(pchar)penalty);
       Alik_defis(trace,cut_points,dx,dy,bl_up,bl_dw,penalty);
    if(language==LANG_ENGLISH)
       Alik_cut_l(cut_points,trace,dx,bl_dw,penalty);
      }

    if(language==LANG_RUSSIAN)
      Alik_cut_y(raster_frag,SourceRaster,adr_ras_two,dy,dx,cut_points,
                 IntBuf,row,product,product_two,penalty,CountCut);
    if(language==LANG_RUSSIAN)
      Alik_rus_bad_c(raster_frag,SourceRaster,dy,dx,product,product_two,
                     penalty,cut_points,CountCut,UpBlackPoint);

     Alik_del_equal(cut_points);
     Alik_del_3_cut(adrw,cut_points,nshort,ver_byte,dx,CountCut);
    if(abs((row+dy)-(minrow+bbs3))>3)
     Alik_del_p_cut(adrw,cut_points,dx,dy,nshort,(char)(bl_dw-bl_up-3));
    if(language==LANG_RUSSIAN)
     Alik_Check_Rus_D(raster_frag,SourceRaster,dy,dx,cut_points,IntBuf,row,
                      product,product_two,penalty,CountCut);
     Alik_del_equal(cut_points);
     CP=*cut_points;

     if(CP)
      {
       CP=Alik_sort(cut_points,(pint16_t)adr_cut_points,dx);
       if(CP)  CP=Alik_del_doubl_cut((pint16_t)adr_cut_points,product,penalty,CP);
      }
     CP=MIN(STK_H-2,CP);

    }
   if (!CP)  Alik_tr_bit_matr(hor_byte,dx,adr_raster,raster_frag,ver_byte,dy);

 if(CP)
  {
   Alik_cor_pnt((pint16_t)adr_cut_points,penalty,cut_points,CP,dx,dy,ver_byte,(pchar)adrw,(pchar)trace);
   Alik_form_bound(adr_raster,dx,dy,ver_byte,trace,1);
 	 stek=(pchar)(penalty+((CP+1)<<2)-(CP+1));  /* ��९��뢠�� �ࠣ����� ����� ��室� */
 	 cut_points=penalty+3*(CP-1)+1;
 	 for (j=CP; j>0; j--, cut_points-=4,stek++)
      {
       *stek=(char)*cut_points++;
       if (*stek) (*stek)--;
       *++stek=dy-*cut_points;
       if (*stek) (*stek)--;
      }
   trace=(pchar)(penalty+((CP+1)<<2)-(CP+1));

   for(j=CP-1,i=0; j>=0; j--,i+=2)
    {
     ans_ptr->x   = (char)*((pint16_t)adr_cut_points+j);
     ans_ptr->h   = dy - *(trace+i);
     ans_ptr->dh  = dy+1 - *(trace+i+1)-*(trace+i);
     ans_ptr->dh  = (char)MIN(ans_ptr->dh,dy);
     ans_ptr->dh  = MIN(ans_ptr->dh,ans_ptr->h);
     if (ans_ptr->dh < tret_h)   ans_ptr->var=1;
     else                        ans_ptr->var=8;
     ans_ptr++;
    }
   ans_ptr->x=127;

   qsort((void *)(ans),CP,sizeof(struct own_cut),Alik_sort_function);
   Alik_set_method_for_cut_points(ans,dy,dx,CountCut);
   Alik_cor_height_and_var(CP,ans,my_penalty,dx,dy);
   Alik_new_points(&CP,ans,ans_ptr,dy,dx,my_penalty,product);
   //AK! posible stack crash there when ans pointed to last vector element!
   qsort((void *)(ans),CP,sizeof(struct own_cut),Alik_sort_function);
  }
 }
 return CP;
}