/* --- winn-m.c  ver1.4 */
/*This is a sample program for reading monochrome data in file
              to do something on it and disply it.*/
/* --- coded by Masaki Oshima  1st release(ver1.0)  1999.10.01  */
/*                             ver1.1 released      1999.10.13  */
/*                             last modification    2001.06.18  */

/* usage example winn-m ~/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;
  /*    unsigned char *vdata, *vdata2, *vdisp;*/
  int width, height;
  XColor *color; int pixs;
  Window win1;
  int x, y, fxy, points, depth, gray=1, flg;
  XImage *image;
  Colormap cmap; /* 2000.08.10 */
  XEvent event;  /* 2001.06.18 */

  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 = rasin->color;           /* colorはXColor形式のカラーデータ        */
  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;
  }

  /* 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(x < width/2) fxy =2*fxy/3;   /* 各画素の処理(テスト用)        */
      f_xy(vd2,x,y) = (unsigned char) fxy; /* 各画素をvdata2に書く */

      /*             fxy = *(vdata + y*width + x) この形式でも使える、ただし上記
のマクロの方が速い */
      /*             *(vdata2 + y*width + x) = (unsigned char) fxy;  */
    }
  }

  /*      sub( vd2);                  関数に処理結果を渡したいときの呼び出し例 *
/
  /*      sub( unisigned char *vd2[]) 呼び出される側。上記のfor文と同様にアクセ
ス可能 */

  vdisp = vdata2; /* 表示用データvdispとしてvdata2を使うよう設定 */
  rasin->vdisp = vdisp;
  color = mono_cmp(rasin); /* make color data for mono chrome 2001.06.09 */
  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 */
  XFree(image);                        /* 使用したメモリを解放する */
  free(vdata); free(vdisp);
  return SUCCESS;
  /* エラー時の処理 */
 err:    printf("error in main\n");
  return FAIL;
}
/* End of main */


戻る