/* --- winn-c.c ver1.4 */
/*   This is a sample program for reading 24bit etc. color data in
         file, to do something on it and disply it.*/
/*   Because this prog. is dedicated for color data, something bad
     will be appered for other data. */
/* --- 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-c ~/Video_data/amano/Fune9-c.ras */

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

int main(int argc, char 
*argv[])/*==============================================
=*/
{
  char data_name[100];
  unsigned char *vdata, *vdata2, *vdisp, *vdisp2, r, g, b;
  unsigned char *vd[MAX_HEIGHT], *vd2[MAX_HEIGHT];
  int width, height, depth, points, gray=0, bwrev;
  XColor *color; int pixs;
  struct rasin *rasin;
  static Window win1, win2;
  int x, y, flg;
  XImage *image; XStandardColormap *scmap;
  Colormap cmap; /* 2000.08.10 */
  XEvent event;


  if( argc<2 ){
    printf("Usage: [Program][data_name]\n");
    return (int)NULL;
  }
  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と同サイズ)の確保 */
  /* 各点ごとに3(4)バイト使用 */
  vdata2 = (unsigned char *)malloc(points *rasin->pix_span);
  /*    vdata2 = (unsigned char *)malloc(points *3);*//* 各点ごとに3バイト使用*/
  if(vdata2 == NULL) goto err;
  /* 2次元配列類似アクセス(f_xyc(data,x,y,c))の準備*/

  /* y行目先頭の点のvdata, vdata2中の位置を設定 */
  for(y = 0; y < height; y++){
    vd[y]  = vdata  + rasin->pix_span*y*width;
    vd2[y] = vdata2 + rasin->pix_span*y*width;
  }
  /*        {vd[y] = vdata + 3*y*width; vd2[y] = vdata2 + 3*y*width;}*/

  /* vdataから読んで(処理の上)vdata2に書く(ラスター走査) */
  for(y = 0; y < height; y++){
    for(x = 0; x < width; x++){
      r = f_xyc(vd, x, y, 'r');     /* vdata各画素rの読み出し、2次元配列類似アク
セスマクロ使用(visio.h参照)*/ /* 注意ras_type3のデータのときf_xycrを使うこと 
*/
      g = f_xyc(vd, x, y, 'g');
      b = f_xyc(vd, x, y, 'b');
      if( y > x && y < x+30) b = 2*b/3;       /* 各画素の処理(テスト用) */
      f_xyc(vd2, x, y, 'r') = r;    /* 各画素のrをvdata2に書く  */
      f_xyc(vd2, x, y, 'g') = g;
      f_xyc(vd2, x, y, 'b') = b;
      /*               r = *(vdata + (y*width + x)*3 + 2 );*/  /*  この形式で読
み書きしてもよい、ただしマクロの方が速い */
      /*               g = *(vdata + (y*width + x)*3 + 1 );
                       b = *(vdata + (y*width + x)*3     );
                       if( y > height/2) b = 0;
                       *(vdata2 + (y*width + x)*3 + 2 ) = (unsigned char) r;
                       *(vdata2 + (y*width + x)*3 + 1 ) = (unsigned char) g;
                       *(vdata2 + (y*width + x)*3     ) = (unsigned char) b;*/
    }
  }

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

  if(rasin->dir_col == True){ /* ifが成立するときダイレクトカラー画像 */
    scmap = rasin->scmap; /* scmapはカラー画像のとき用いる標準カラー
                             マップ形式のデータ */
    /* vdataからscmapに従って表示用の形式vdispを作る */
    vdisp = data_to_disp(vdata, points, scmap, pixs, rasin);
  }else{
    vdisp = vdata; /* カラーマップのあるデータ(非ダイレクトカラー)
                             のときはvdispはvdataと同じでよい */
  }

  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);
  XClearWindow(disp,win1);
  XFlush(disp);
  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 */


戻る