12132

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

void main(){
    int i,j,k,m;
    int width,height,start,world;
    int *bmp,*Lcount;
    bool *Lflag;


    FILE *fp; 

    if((fp=fopen("in1.bmp","rb"))==NULL){
        printf("文件打开失败");
        return;
    }

    fseek(fp,10L,0);
    fscanf(fp,"%4c",&start);   // 4c表示该数据占4个字节
//    printf("start = %d\n",start);

    fseek(fp,18,0);
    fscanf(fp,"%4c",&width);
//    printf("width = %d\n",width);

    fseek(fp,22,0);
    fscanf(fp,"%4c",&height); 
//    printf("height = %d\n",height);

    bmp = (int*)malloc((width+2)*sizeof(int));
    memset(bmp,0,(width+2)*sizeof(int));

    Lcount = (int*)malloc(width*sizeof(int));
    memset(Lcount,0,width*sizeof(int));

    Lflag = (bool*)malloc(width*sizeof(bool));
    memset(Lflag,0,width*sizeof(bool));

    Lcount--;
    Lflag--;

    fseek(fp,start,0);

    world = ( width%32 ? width/32+1 : width/32 )*4;

    int last,i1,i2,i3;
    int eCount = 0 ;
    for(i=0 ; i<height ; i++ ){
        char c;
        k=1;
        last=0;
        for(j=0 ; j<world ; j++){
            fscanf(fp,"%c",&c);
            for(m = 7 ; m >= 0 && k<=width ; m-- ){
                if( !( 1<<m & c ) ){
//printf("*");
                    if(bmp[k]){
                        last = bmp[k];
                        Lcount[last]++;
                        Lflag[last] = true ;
                    }
                    else{
                        i1 = last ? last : bmp[k-1] ;
                        i3 = bmp[k+1] ;
                        last = 0;

                        if( i1 || i3){                            
                            if( i1 && i3 && ( i1 != i3 ) ){//确定需要连接
                                Lcount[i1] += Lcount[i3] ;
                                Lcount[i3]=0;
                                for(i2=1;i2<=width ; i2++){
                                    if(bmp[i2]==i3)
                                        bmp[i2] = i1;
                                }
                            }
                            else{
                                if(!i1)
                                    i1=i3;
                            }
                            bmp[k] = i1 ;
                            Lcount[i1]++;
                            Lflag[i1] = true ;
                        }
                        else{//插入
                            for(i2=1;Lcount[i2];i2++);
                            Lcount[i2]=1;
                            bmp[k] = i2 ;
                            Lflag[i2] = true ;
                        }
                    }
                }
                else{
//printf(" ");
                    last = bmp[k] ;
                    bmp[k] = 0 ;
                }
                k++;
            }
        }
//printf("\n");
        for(i2=1;i2<=width;i2++){
            if(Lcount[i2] && !Lflag[i2] ){
                printf("%d\n",Lcount[i2]);
                Lcount[i2] = 0 ;
                eCount++;
            }
            Lflag[i2]=false;
        }
    }
    fclose(fp);

    free(Lflag+1);
    free(Lcount+1);
    free(bmp);

    printf("count=%d\n",eCount);
}

 

posted on 2013-07-09 23:06  wwjyt  阅读(972)  评论(0编辑  收藏  举报