home *** CD-ROM | disk | FTP | other *** search
- /*
- # GET_PIC . C
- %
- % Read an image from the input device and display it.
- %
- % Copyright (c) See included header files
- %
- % AUTHOR: Jin Guojun - LBL 4/1/92
- */
-
- #include "function.h"
-
- static char *def_title[] = {"image_title", "IMAGE_TITLE", "title", "TITLE"};
- bool shared_cmap, multi_hd; /* for multi_frame RLE */
-
- #ifndef MINIMUM_COLORs
- #define MINIMUM_COLORs 32
- #endif
- #define ROW_STORAGE 16
-
- #define STILL_WIN (!prev_img || prev_img==img)
-
-
- get_pic(cur_img, win_geom, prev_img, img_info, img_win_bg)
- image_information *prev_img, **img_info;
- char *win_geom;
- {
- register image_information *img=img_info[cur_img];
- byte *save_scan[3], *read_scan[3];
- int max_h, icon_f, next_y, end_y, row_mode,
- sort_colors = STILL_WIN && (shared_cmap && cur_img || !tuner_flag &&
- (VCTEntry < 1 || VCTEntry+MINIMUM_COLORs > specified_levels));
- register int i, image_y, inc_y=1, rows_buffered;
-
- /* Read setup info. from files */
-
- if ( (i = -feof(img->IN_FP)) ||
- ((STILL_WIN || img->in_type==RLE) && (i=(*img->header_handle)
- (HEADER_READ, img, 0, multi_hd, OsameI | sort_colors)) < 0))
- return i;
-
- if (img->in_type==RLE)
- max_h = rle_dflt_hdr.ymax;
- else max_h = img->h-1;
-
- if (!img->title)
- for (i=sizeof(def_title)/sizeof(def_title[0]); i--;)
- if (img->title=rle_getcom(def_title[i], &rle_dflt_hdr ))
- break;
-
- if (prev_img==img &&
- (img->w+1 != img->image->width || img->h != img->image->height))
- DestroyColorImage(img), img->map_scanline = NULL;
-
- if (!prev_img) /* better after img->????_color set up */
- find_appropriate_visual(img);
- else img->RGB++;
- sort_colors &= img->dpy_depth < 24 && img->img_num < 2;
-
- /*
- * If we are going to display a 2 or 3 channel image in 1 channel (-w),
- * we have to copy the data any way, so we will always read it into the
- * same spot and map it into a saved_data array with only one color
- * per scanline; this saves memory.
- *
- * read_scan[] is the scan data that we got from rle_getrow.
- * If the number of image channels is equal to which is displayed,
- * we will not allocate new memory for it, but we will move it
- * throughout the saved_data along with save_scan.
- *
- * save_scan[] is the pointer to the current line in img->saved_data
- * that we are saving rle_getrows into.
- *
- * If we can't malloc the memory for the save_scan[], or we are doing
- * flip_book, then malloc the memory is not needed.
- */
-
- if ((!prev_img || tuner_flag) &&
- !(img->scan_data=nzalloc(SAVED_RLE_ROW(img, img->h), 1, No)))
- prgmerr(tuner_flag, "alloc for %s scanline buffer", img->name);
-
- /* not True when scan_data is not allocated. */
- if (!img->scan_data || tuner_flag) {
- img->data = NZALLOC(img->w * img->h, img->img_channels, No);
- if (!img->scan_data) img->scan_data = img->data;
- }
- if (! img->data) img->data = img->scan_data;
-
- if (! (row_mode=ccs_get_row_ok(img)) ) {
- /* multiple-frame is applied to HIPS and FITS */
- byte *pchange = img->data;
- if ((*img->std_swif)(FI_LOAD_FILE, img, 0, img->in_type==FITS ||
- img->img_num > 1 && img->color_form != CFM_SCF ? img->RGB :
- (OsameI | sort_colors)) < 1)
- return RLE_EOF;
- if (OsameI & sort_colors && img->img_channels > 1) {
- To_8(img, reg_cmap, 0, MaxColors);
- img->dpy_channels = img->img_channels;
- if (tuner_flag) img->lvls = specified_levels;
- }
- if (img->mono_img && !tuner_flag && img->in_type != RLE &&
- img->scan_data==0 || pchange != img->data)
- img->scan_data = img->data;
-
- if (tuner_flag | sort_colors)
- img->histp = img->hist = nzalloc(HistoSize*img->dpy_channels,
- sizeof(*(img->hist)), "hist"),
- Find_min_max(img, histinfo.histp=img->hist, img->data, True, True);
- if (sort_colors && reg_cmap[0] && img->color_form==CFM_SCF)
- calibrate_colors(img);
- }
-
- i = img->in_type != RLE;
- if (reg_cmap[0] && (sort_colors | (i || img->color_form != CFM_SCF)))
- regmap_to_rlemap(reg_cmap, img->cmaplen, img->cmaplen &&
- img->img_channels < 2 && (i || sort_colors) ? 3 : 0,
- &rle_dflt_hdr);
-
- if (img->img_channels < img->dpy_channels)
- prgmerr(0, "_get_pic: dpy_channels %d > img_channels %d\n",
- img->dpy_channels, img->img_channels);
-
- i = BuildColorImage(img, prev_img, win_geom, &icon_f);
- XSetWindowBackground(img->dpy, img->window, img_win_bg);
- if (i != SUCCESS)
- return i;
-
- /* For each scan line, read, save, dither, and display it. */
- #define ShowRows() { \
- XPutImage(img->dpy, img->pixmap?img->pixmap:img->window, img->gc, \
- img->image, 0, next_y, 0, next_y, img->w, rows_buffered); \
- if (img->pixmap) XCopyArea(img->dpy, img->pixmap, img->window, img->gc,\
- 0, next_y, img->w, rows_buffered, 0, next_y); }
-
- if (row_mode < 0)
- end_y = inc_y = row_mode;
- else end_y = img->h;
- next_y = image_y = /* end_y */ inc_y<0 ? img->h + inc_y : 0;
-
- for (rows_buffered=0; image_y!=end_y; image_y+=inc_y) {
-
- if (img->scan_data) { /* move scan up one line */
- save_scan[0] = SAVED_RLE_ROW(img, image_y);
- for (i=1; i < img->img_channels; i++)
- save_scan[i] = save_scan[i-1] + img->w;
- }
- if (tuner_flag) {
- read_scan[0] = ORIG_RLE_ROW(img, image_y);
- for (i=1; i<img->img_channels; i++)
- read_scan[i] = read_scan[i-1] + img->w;
- } else
- read_scan[0] = save_scan[0],
- read_scan[1] = save_scan[1],
- read_scan[2] = save_scan[2];
- if (row_mode && (*img->std_swif)(FI_LOAD_ROW, img, image_y, 0) != img->w)
- return EOF;
- MapRGB(cur_img, prev_img, img_info, read_scan, save_scan,
- image_y, img->w, max_h-image_y, icon_f);
-
- if (++rows_buffered >= ROW_STORAGE || row_mode) {
- ShowRows();
- if (inc_y < 0) next_y = image_y + inc_y;
- else next_y += rows_buffered;
- rows_buffered = 0;
- }
- }
- if (rows_buffered++ > 0) ShowRows();
- if (!prev_img && img->icn_pixmap) {
- XPutImage(img->dpy, img->icn_pixmap, img->icn_gc, img->icn_image,
- 0, 0, 0, 0, img->icn_w, img->icn_h);
- XClearWindow(img->dpy, img->icn_window);
- }
-
- set_window_cursor(img->window, XCreateFontCursor(img->dpy, XC_umbrella));
- Draw_ImageScrollBars(img);
- superimpose_images(img, -1, 0, 0, img->w, max_h);
-
- return SUCCESS;
- }
-