home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / lib / get_pic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-10  |  5.8 KB  |  180 lines

  1. /*
  2. #    GET_PIC . C
  3. %
  4. %    Read an image from the input device and display it.
  5. %
  6. %    Copyright (c)    See included header files
  7. %
  8. % AUTHOR:    Jin Guojun - LBL    4/1/92
  9. */
  10.  
  11. #include "function.h"
  12.  
  13. static    char    *def_title[] = {"image_title", "IMAGE_TITLE", "title", "TITLE"};
  14. bool    shared_cmap, multi_hd;    /* for multi_frame RLE */
  15.  
  16. #ifndef    MINIMUM_COLORs
  17. #define    MINIMUM_COLORs    32
  18. #endif
  19. #define ROW_STORAGE    16
  20.  
  21. #define    STILL_WIN    (!prev_img || prev_img==img)
  22.  
  23.  
  24. get_pic(cur_img, win_geom, prev_img, img_info, img_win_bg)
  25. image_information    *prev_img, **img_info;
  26. char *win_geom;
  27. {
  28. register image_information    *img=img_info[cur_img];
  29. byte    *save_scan[3], *read_scan[3];
  30. int    max_h, icon_f, next_y, end_y, row_mode,
  31.     sort_colors = STILL_WIN && (shared_cmap && cur_img || !tuner_flag &&
  32.         (VCTEntry < 1 || VCTEntry+MINIMUM_COLORs > specified_levels));
  33. register int    i, image_y, inc_y=1, rows_buffered;
  34.  
  35.     /*    Read setup info. from files    */
  36.  
  37.     if ( (i = -feof(img->IN_FP)) ||
  38.         ((STILL_WIN || img->in_type==RLE) && (i=(*img->header_handle)
  39.         (HEADER_READ, img, 0, multi_hd, OsameI | sort_colors)) < 0))
  40.         return    i;
  41.  
  42.     if (img->in_type==RLE)
  43.         max_h = rle_dflt_hdr.ymax;
  44.     else    max_h = img->h-1;
  45.  
  46.     if (!img->title)
  47.     for (i=sizeof(def_title)/sizeof(def_title[0]); i--;)
  48.         if (img->title=rle_getcom(def_title[i], &rle_dflt_hdr ))
  49.             break;
  50.  
  51.     if (prev_img==img &&
  52.         (img->w+1 != img->image->width || img->h != img->image->height))
  53.         DestroyColorImage(img),    img->map_scanline = NULL;
  54.  
  55.     if (!prev_img)    /* better after img->????_color set up    */
  56.         find_appropriate_visual(img);
  57.     else    img->RGB++;
  58.     sort_colors &= img->dpy_depth < 24 && img->img_num < 2;
  59.  
  60.     /*
  61.     * If we are going to display a 2 or 3 channel image in 1 channel (-w),
  62.     * we have to copy the data any way, so we will always read it into the
  63.     * same spot and map it into a saved_data array with only one color
  64.     * per scanline; this saves memory.
  65.     *
  66.     * read_scan[] is the scan data that we got from rle_getrow.
  67.     * If the number of image channels is equal to which is displayed,
  68.     * we will not allocate new memory for it, but we will move it
  69.     * throughout the saved_data along with save_scan.
  70.     *
  71.     * save_scan[] is the pointer to the current line in img->saved_data
  72.     * that we are saving rle_getrows into.
  73.     *
  74.     * If we can't malloc the memory for the save_scan[], or we are doing
  75.     * flip_book, then malloc the memory is not needed.
  76.     */
  77.  
  78.     if ((!prev_img || tuner_flag) &&
  79.         !(img->scan_data=nzalloc(SAVED_RLE_ROW(img, img->h), 1, No)))
  80.         prgmerr(tuner_flag, "alloc for %s scanline buffer", img->name);
  81.  
  82.     /*    not True when scan_data is not allocated.    */
  83.     if (!img->scan_data || tuner_flag)    {
  84.         img->data = NZALLOC(img->w * img->h, img->img_channels, No);
  85.         if (!img->scan_data)    img->scan_data = img->data;
  86.     }
  87.     if (! img->data)    img->data = img->scan_data;
  88.  
  89.     if (! (row_mode=ccs_get_row_ok(img)) )    {
  90.     /* multiple-frame is applied to HIPS and FITS */
  91.     byte    *pchange = img->data;
  92.     if ((*img->std_swif)(FI_LOAD_FILE, img, 0, img->in_type==FITS ||
  93.         img->img_num > 1 && img->color_form != CFM_SCF ? img->RGB :
  94.             (OsameI | sort_colors)) < 1)
  95.         return    RLE_EOF;
  96.     if (OsameI & sort_colors && img->img_channels > 1)    {
  97.         To_8(img, reg_cmap, 0, MaxColors);
  98.         img->dpy_channels = img->img_channels;
  99.         if (tuner_flag)    img->lvls = specified_levels;
  100.     }
  101.     if (img->mono_img && !tuner_flag && img->in_type != RLE &&
  102.         img->scan_data==0 || pchange != img->data)
  103.         img->scan_data = img->data;
  104.  
  105.     if (tuner_flag | sort_colors)
  106.         img->histp = img->hist = nzalloc(HistoSize*img->dpy_channels,
  107.             sizeof(*(img->hist)), "hist"),
  108.         Find_min_max(img, histinfo.histp=img->hist, img->data, True, True);
  109.     if (sort_colors && reg_cmap[0] && img->color_form==CFM_SCF)
  110.         calibrate_colors(img);
  111.     }
  112.  
  113.     i = img->in_type != RLE;
  114.     if (reg_cmap[0] && (sort_colors | (i || img->color_form != CFM_SCF)))
  115.         regmap_to_rlemap(reg_cmap, img->cmaplen, img->cmaplen &&
  116.             img->img_channels < 2 && (i || sort_colors) ? 3 : 0,
  117.             &rle_dflt_hdr);
  118.  
  119.     if (img->img_channels < img->dpy_channels)
  120.         prgmerr(0, "_get_pic: dpy_channels %d > img_channels %d\n",
  121.             img->dpy_channels, img->img_channels);
  122.  
  123.     i = BuildColorImage(img, prev_img, win_geom, &icon_f);
  124.     XSetWindowBackground(img->dpy, img->window, img_win_bg);
  125.     if (i != SUCCESS)
  126.         return    i;
  127.  
  128. /*    For each scan line, read, save, dither, and display it.    */
  129. #define    ShowRows()    {    \
  130.     XPutImage(img->dpy, img->pixmap?img->pixmap:img->window, img->gc,    \
  131.     img->image, 0, next_y, 0, next_y, img->w, rows_buffered);    \
  132.     if (img->pixmap) XCopyArea(img->dpy, img->pixmap, img->window, img->gc,\
  133.         0, next_y, img->w, rows_buffered, 0, next_y);    }
  134.  
  135.     if (row_mode < 0)
  136.         end_y = inc_y = row_mode;
  137.     else    end_y = img->h;
  138.     next_y = image_y = /* end_y */    inc_y<0 ? img->h + inc_y : 0;
  139.  
  140.     for (rows_buffered=0; image_y!=end_y; image_y+=inc_y)    {
  141.  
  142.     if (img->scan_data) {    /* move scan up one line */
  143.         save_scan[0] = SAVED_RLE_ROW(img, image_y);
  144.         for (i=1; i < img->img_channels; i++)
  145.             save_scan[i] = save_scan[i-1] + img->w;
  146.     }
  147.     if (tuner_flag) {
  148.         read_scan[0] = ORIG_RLE_ROW(img, image_y);
  149.         for (i=1; i<img->img_channels; i++)
  150.         read_scan[i] = read_scan[i-1] + img->w;
  151.     } else
  152.         read_scan[0] = save_scan[0],
  153.         read_scan[1] = save_scan[1],
  154.         read_scan[2] = save_scan[2];
  155.     if (row_mode && (*img->std_swif)(FI_LOAD_ROW, img, image_y, 0) != img->w)
  156.         return    EOF;
  157.     MapRGB(cur_img, prev_img, img_info, read_scan, save_scan,
  158.         image_y, img->w, max_h-image_y, icon_f);
  159.  
  160.     if (++rows_buffered >= ROW_STORAGE || row_mode) {
  161.         ShowRows();
  162.         if (inc_y < 0)    next_y = image_y + inc_y;
  163.         else    next_y += rows_buffered;
  164.         rows_buffered = 0;
  165.     }
  166.     }
  167.     if (rows_buffered++ > 0)    ShowRows();
  168.     if (!prev_img && img->icn_pixmap) {
  169.     XPutImage(img->dpy, img->icn_pixmap, img->icn_gc, img->icn_image,
  170.           0, 0, 0, 0, img->icn_w, img->icn_h);
  171.     XClearWindow(img->dpy, img->icn_window);
  172.     }
  173.  
  174.     set_window_cursor(img->window, XCreateFontCursor(img->dpy, XC_umbrella));
  175.     Draw_ImageScrollBars(img);
  176.     superimpose_images(img, -1, 0, 0, img->w, max_h);
  177.  
  178. return    SUCCESS;
  179. }
  180.