/* --- winn-th.c  ver1.4 */
/*This is a sample program for thresholding */
/*                      based on winn-m.c   */
/* --- coded by Masaki Oshima  1st release(ver1.4)  2001.06.14  */
/*                                               last modification    2001.06.18  */

/* usage example winn-th ~/Video_data/gazou/hirosue.ras */

#include <stdio.h>
#include "vismain.h"

int main(int argc, char 
*argv[])/*==============================================
=*/
{
  char data_name[100];
  struct rasin *rasin;
  unsigned char *vdata, *vdata2, *vd[MAX_HEIGHT], *vd2[MAX_HEIGHT], *vdisp;
  int width, height;
  XColor *color; int pixs;
  Window win1;
  int x, y, fxy, points, depth, gray=1;
  XImage *image;
  Colormap cmap; /*2000.08.10*/
  int th, flg;
  XEvent event;

  if( argc<2 ){
    printf("Usage: [Program][data_name]\n");
    return FAIL;
  }
  sprintf(data_name,"%s",argv[1]); /*データ名の設定*/

  /*画像ファイルデータの読み込み  rasinはデータ入力のための領域  visio.h参照 */
  rasin = get_image(data_name, &width, &height, &pixs);
  if(rasin == NULL) goto err;
  vdata = rasin->vdata;           /* vdataは入力画像の入っている領域  */
  color = mono_cmp(rasin); /* 2001.06.09 */
  pixs  =  rasin->pixs;
  points = width * height;        /* 画素点の数を横幅と縦幅から計算 */

  /* 画像領域vdata2(処理結果用、vdataと同サイズ)の確保 */
  vdata2 = (unsigned char *)malloc(points);
  if(vdata2 == NULL) goto err;
  /* 2次元配列類似アクセス(f_xy(data,x,y))の準備*/
  for(y = 0; y < height; y++){
    /* y行目先頭の点のvdata, vdata2中の位置を設定 */
    vd[y] = vdata + y*width; vd2[y] = vdata2 + y*width;
  }

  while(th <= 255){
    fprintf(stderr, "enter th (999=end)\n");
    scanf("%d",&th);
    if(th > 255) continue;
    /* vdataから読んで(処理の上)vdata2に書く(ラスター走査) */
    for(y = 0; y < height; y++){
      for(x = 0; x < width; x++){
        fxy = f_xy(vd,x,y);       /* 各画素データをvdataから読み出し、2次元配列
類似アクセスマクロ使用(visio.h参照) */
        if(fxy > th){
          fxy = 255;
        }else{
          fxy =0;
        }
        f_xy(vd2,x,y) = (unsigned char) fxy; /* 各画素をvdata2に書く */
      }
    }

    vdisp = vdata2; /* 表示用データvdispとしてvdata2を使うよう設定 */
    rasin->vdisp = vdisp;
    cmap = set_cmap(color, pixs, rasin);         /* カラーマップの設定 */
    win1  = win_set(width, height, "output data", cmap);      /*ウインドウの設定
*/

    depth =8;
    /* 表示用データvdispをcolorに従って作成 */
    image = mk_image (vdisp, width, height, depth, win1, color, pixs, gray);

    XSelectInput(disp, win1, ExposureMask | ButtonPressMask );

    flg =1;
    while(flg){ /*   event loop starts. to out the loop, click the window */
      XNextEvent( disp, &event );
      switch( event.type ){
      case Expose :
        fprintf(stderr, "Expose event\n");
        XPutImage(disp,win1,gc,image,0,0,0,0,
                  width,height);
        XFlush(disp);
        break;
      case ButtonPress :
        fprintf(stderr, "ButtonPress event\n");
        flg = 0;
      }
    }
    fprintf(stderr, "event loop out\n"); /* exit the event loop, goto th loop */
  }
 thlpout:
  fprintf(stderr, "lpout\n");
  XFree(image);                  /* 使用したメモリを解放する */
  free(vdata); free(vdisp);
  return SUCCESS;
  /* エラー時の処理 */
 err:    printf("error in main\n");
  return FAIL;
}
/* End of main */



戻る