home *** CD-ROM | disk | FTP | other *** search
- /* HeaDer_HANDLE. C
- #
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
- This software is copyright (C) by the Lawrence Berkeley Laboratory.
- Permission is granted to reproduce this software for non-commercial
- purposes provided that this notice is left intact.
-
- It is acknowledged that the U.S. Government has rights to this software
- under Contract DE-AC03-765F00098 between the U.S. Department of Energy
- and the University of California.
-
- This software is provided as a professional and academic contribution
- for joint exchange. Thus, it is experimental, and is provided ``as is'',
- with no warranties of any kind whatsoever, no support, no promise of
- updates, or printed documentation. By using this software, you
- acknowledge that the Lawrence Berkeley Laboratory and Regents of the
- University of California shall have no liability with respect to the
- infringement of other copyrights by any part of this software.
-
- For further information about this notice, contact William Johnston,
- Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- (wejohnston@lbl.gov)
-
- For further information about this software, contact:
- Jin Guojun
- Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
- g_jin@lbl.gov
-
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % AUTHOR: Jin Guojun - LBL 7/25/91
- */
-
- #ifdef COMMON_TOOL
-
- #ifndef MaxColors
- #define MaxColors 256
- #endif
-
- ICC_HEADER icchd;
-
- icc_header_handle(job, img, ac, av, assist)
- U_IMAGE *img;
- int ac;
- char **av;
- VType *assist;
- {
- int num;
-
- switch (job) {
- case HEADER_READ:
- case HEADER_FREAD:
- if (!av)
- fseek(img->IN_FP, 0, 0);
- fread(&num, sizeof(num), 1, img->IN_FP);
- if (num != ICC_MAGIC)
- other_hd: return EOF;
- fread(&icchd.hd_len, sizeof(icchd.hd_len), 1, img->IN_FP);
- fread(&icchd.H_W, icchd.hd_len - sizeof(icchd.hd_len), 1, img->IN_FP);
- fread(&icchd.img_hd.len, sizeof(icchd.img_hd.len), 1, img->IN_FP);
- fread(&icchd.img_hd.length, icchd.img_hd.len-sizeof(icchd.img_hd.len),
- 1, img->IN_FP);
- if (icchd.img_hd.length - icchd.img_hd.len -
- icchd.img_hd.x_size * icchd.img_hd.y_size)
- goto other_hd;
- img->width = icchd.img_hd.orig_width;
- img->height = icchd.img_hd.orig_height;
- if (!(img->width | img->height) || img->height != icchd.img_hd.y_size
- || ((img->width + 7) & 0xfffffff8L != icchd.img_hd.x_size))
- img->width = icchd.img_hd.x_size,
- img->height = icchd.img_hd.y_size;
- img->pxl_in = img->frames = 1;
- img->channels = img->dpy_channels = icchd.img_hd.planes;
- img->in_type = ICC;
- img->in_color = img->color_form = CFM_SGF;
- if (img->channels==3)
- img->in_form = IFMT_SEPLANE,
- img->in_color = CFM_SEPLANE;
- else img->in_form = IFMT_SGF,
- img->mono_img = True;
- break;
- default: message("unknown icc job %d\n", job);
- return job;
- }
- return 0;
- }
-
-
- static int format, maxval;
- static xel **xel24;
- static colorhash_table cht;
-
-
- static cmap_t*
- pnmmap_to_regmap(rg_cmap, chv, colors)
- cmap_t *rg_cmap[3];
- colorhist_vector chv;
- int colors;
- {
- register int i=3, j=1<<min_bits(colors-1);
- register cmap_t *cmap[3];
-
- verify_buffer_size(rg_cmap, j, i*sizeof(*cmap), "pnm-rcmap");
- cmap[0] = rg_cmap[0];
- cmap[1] = rg_cmap[1] = rg_cmap[0] + j;
- cmap[2] = rg_cmap[2] = rg_cmap[1] + j;
-
- for (i=0; i < colors; ++i) {
- cmap[0][i] = PPM_GETR(chv[i].color);
- cmap[1][i] = PPM_GETG(chv[i].color);
- cmap[2][i] = PPM_GETB(chv[i].color);
- }
- return cmap[0];
- }
-
- pnm_header_handle(job, img, ac, av, assist)
- U_IMAGE *img;
- int ac;
- char *av[];
- VType *assist;
- {
- gray gmaxval; /* byte */
- colorhist_vector chv;
- xel *xelrow, p;
- int depth, colors;
- register int r;
-
- switch (job) {
- case HEADER_READ:
- case HEADER_FREAD:
- if (img->in_type > RLE && img->in_type != PNM) goto pnm_df;
- if (!av)
- fseek(img->IN_FP, 0, 0);
- format = pbm_readmagicnumber(img->IN_FP); /* check magic # */
- img->pxl_in = 1;
- switch (PNM_FORMAT_TYPE(format)) {
- case PPM_TYPE:
- maxval = ppm_readppminitrest(img->IN_FP, &img->width, &img->height);
- xel24 = (xel**)alloc_2d_discrete(img->width, img->height, sizeof(xel));
- for (r=0; r < img->height; r++)
- pnm_readpnmrow(img->IN_FP, xel24[r], img->width, maxval, format);
- /* Figure out the proper depth and colormap */
- chv = ppm_computecolorhist(xel24, img->width, img->height,
- MaxColors, &colors);
- if (chv) {
- DEBUGMESSAGE("%d colors found", colors);
- if (maxval != 255)
- for (r=0; r < colors; r++)
- PPM_DEPTH(chv[r].color, chv[r].color, maxval, 255);
-
- /* Force white to slot 0 and black to slot 1, if possible. */
- PPM_ASSIGN(p, 255, 255, 255);
- ppm_addtocolorhist(chv, &colors, MaxColors, &p, 0, 0);
- PPM_ASSIGN(p, 0, 0, 0);
- ppm_addtocolorhist(chv, &colors, MaxColors, &p, 0, 1);
-
- if (colors != 2) {
- /* convert the ppm colormap into the RLE colormap. */
- depth = 8;
- img->cmaplen = colors;
- img->in_color = CFM_SCF;
- img->in_form = IFMT_SCF;
- pnmmap_to_regmap(reg_cmap, chv, colors);
- }
- if (cht) ppm_freecolorhash(cht);
- cht = ppm_colorhisttocolorhash(chv, colors);
- ppm_freecolorhist(chv);
- img->channels = img->mono_img = 1;
- img->in_type = PNM;
- if (colors==2) { /* Monochrome. */
- depth = 1;
- goto pbm_tp;
- }
- }
- else img->channels = 3,
- img->in_type = PPM,
- img->in_form = IFMT_ILC,
- img->in_color = CFM_ILC;
- break;
- case PGM_TYPE:
- pgm_readpgminitrest(img->IN_FP, &img->width, &img->height, &gmaxval);
- maxval = (xelval) gmaxval;
- if (maxval > 255)
- # ifdef BIGGRAYS
- img->in_form = IFMT_SHORT,
- img->pxl_in = sizeof(short);
- # else
- return prgmerr(0, "maxval (%d) is greater than 255", maxval);
- # endif
- img->channels = img->mono_img = 1;
- img->in_type = PGM;
- img->in_color = CFM_SGF;
- break;
- case PBM_TYPE:
- pbm_readpbminitrest(img->IN_FP, &img->width, &img->height);
- maxval = img->channels = img->mono_img = 1;
- img->in_type = PBM;
- pbm_tp: img->in_form = IFMT_BITMAP;
- img->in_color = CFM_BITMAP;
- break;
- default:
- pnm_df: return EOF;
- }
- if (maxval > 255 && depth != 1)
- message("maxval is not 255 - automatically rescaling colors");
-
- break;
- case HEADER_WRITE:
- default: return prgmerr(0, "not a pnm job %d\n", job);
- }
- return 0;
- }
-
-
- static bool TiffRGB;
- TIFF *TIFFin;
- unsigned short samplesperpixel, bitspersample;
-
- tiff_header_handle(job, img, ac, av, assist)
- U_IMAGE *img;
- int ac;
- char **av;
- VType *assist;
- {
- unsigned short maxval;
-
- switch (job) {
- case HEADER_READ:
- case HEADER_FREAD:
- if (!av)
- lseek(fileno(img->IN_FP), 0, 0);
- TIFFin = TIFFFdOpen(fileno(img->IN_FP), img->name ? img->name:"", "r");
- if (!TIFFin)
- return EOF;
- TIFFGetField(TIFFin, TIFFTAG_BITSPERSAMPLE, &bitspersample);
- TIFFGetField(TIFFin, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
- if (samplesperpixel > 1)
- TiffRGB++;
- switch (samplesperpixel) {
- default:message("handle %d samples.\n", samplesperpixel);
- case 1:
- img->channels = 1;
- if (img->color_dpy && !assist)
- img->dpy_channels = 3;
- else img->dpy_channels = img->mono_img = 1;
- if (TIFFin->tif_dir.td_photometric==PHOTOMETRIC_PALETTE) {
- register int i = MIN(MaxColors, (maxval=1<<bitspersample));
-
- img->in_color = CFM_SCF;
- img->in_form = IFMT_SCF;
- img->cmaplen = i;
- if (verify_buffer_size(reg_cmap, i, 3*sizeof(cmap_t), "tif_cmap")){
- reg_cmap[1] = reg_cmap[0] + i;
- reg_cmap[2] = reg_cmap[1] + i;
- }
- while (i--) {
- reg_cmap[0][i] = TIFFin->tif_dir.td_colormap[0][i] >> 8;
- reg_cmap[1][i] = TIFFin->tif_dir.td_colormap[1][i] >> 8;
- reg_cmap[2][i] = TIFFin->tif_dir.td_colormap[2][i] >> 8;
- }
- }
- else if (bitspersample==1) /* always convert to SGF */
- img->in_color = CFM_SGF,
- img->in_form = IFMT_BITMAP;
- else prgmerr(0, "TIFF format-8");
- break;
- case 3:
- if (TIFFin->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE)
- img->in_color = CFM_ILL,
- img->in_form = IFMT_ILL;
- else img->in_color = CFM_ILC,
- img->in_form = IFMT_ILC;
- goto next34;
- case 4: img->in_color = CFM_ALPHA;
- img->in_form = IFMT_ALPHA;
- next34: img->dpy_channels = img->channels = 3;
- if (!img->color_dpy)
- img->dpy_channels = 1;
- break;
- }
-
- TIFFGetField(TIFFin, TIFFTAG_IMAGEWIDTH, &img->width);
- TIFFGetField(TIFFin, TIFFTAG_IMAGELENGTH, &img->height);
-
- img->in_type = TiFF;
- img->pxl_in = sizeof(byte);
- break;
- case HEADER_WRITE:
- default: return prgmerr(0, "no such a tiff job %d\n", job);
- }
- return 0;
- }
-
- #endif COMMON_TOOL
-
- int row_dpy_mode;
-
- ccs_get_row_ok(img)
- register U_IMAGE* img;
- {
- register int type=img->in_type, r_mode=row_dpy_mode && img->IN_FP==stdin;
-
- if (type==RLE && r_mode) return -1; /* decreasing mode */
-
- r_mode |= type==HIPS && img->color_form==CFM_SGF;
-
- #ifdef STREAM_IMAGE
- if ((r_mode &= !get_sinfo(True, 0)) && /* not in compressed type */
- (type=get_sinfo(False, 0)) > 0) /* is in stream mode. */
- for (type*=img->width; type--;)
- fgetc(img->IN_FP);
- #endif
- return r_mode;
- }
-
- ccs_get_row(img, row, no_buf)
- U_IMAGE *img;
- register int row;
- {
- register int w = img->width;
- register char* buf = (char*)img->src + (no_buf ? 0 : w * row * img->channels);
- switch (img->in_type) {
- case HIPS:
- return fread(buf, 1, w, img->IN_FP);
- #ifdef RLE_IMAGE
- case RLE: {
- char* scan[3];
- scan[0] = buf; scan[1] = buf + w; scan[2] = buf + (w << 1);
- rle_getrow(&rle_dflt_hdr, scan);
- return w;
- }
- #endif
- default:
- return prgmerr(0, "cannot get_row for %s", ITypeName[img->in_type]);
- }
- }
-
- #ifdef HIPS_IMAGE
-
- #define si_elems "si_elems"
- #define si_draws "si_draws"
- #define si_texts "si_texts"
- #define si_str "sit%03d"
-
- static char sbuf[8];
-
- put_superimpose_param(img, hhd)
- U_IMAGE* img;
- struct header* hhd;
- {
- if (img->superimpose || img->draws | img->texts) {
- register superimpose_elems* si_ep=img->superimpose[1];
- if (findparam(hhd, si_elems) != NULLPAR)
- hhd->paramdealloc = False;
- setparam(hhd, si_elems, PFSHORT, 2, &img->draws);
- if (img->draws)
- setparam(hhd, si_draws, PFBYTE, img->draws * sizeof(*si_ep),
- img->superimpose[0]);
- if (img->texts) {
- register int i=img->texts;
- setparam(hhd, si_texts, PFBYTE, img->texts * sizeof(*si_ep),
- si_ep);
- while (i--) {
- sprintf(sbuf, si_str, i);
- setparam(hhd, sbuf, PFBYTE, strlen(si_ep[i].elem.text.content),
- si_ep[i].elem.text.content);
- }
- }
- }
- }
-
- get_superimpose_param(img, hhd)
- U_IMAGE* img;
- struct header* hhd;
- {
- int n=2;
- short *boo;
- if (!findparam(hhd, si_elems)) return False;
- img->superimpose = zalloc(n, sizeof(superimpose_elems), "");
- if (getparam(hhd, si_elems, PFSHORT, &n, &boo) != HIPS_OK)
- return EOF;
- img->draws = boo[0]; n = 2;
- if ((findparam(hhd, si_draws) != NULLPAR | n) &&
- getparam(hhd, si_draws, PFBYTE, &n, img->superimpose) != HIPS_OK)
- img->draws = 0;
- img->texts = boo[1]; n = 2;
- if (findparam(hhd, si_texts) != NULLPAR &&
- getparam(hhd, si_texts, PFBYTE, &n, img->superimpose+1) == HIPS_OK) {
- register superimpose_elems* si_ep=img->superimpose[1];
- register int i=img->texts;
- while (i--) {
- sprintf(sbuf, si_str, i); n = 2; /* non-single char */
- getparam(hhd, sbuf, PFBYTE, &n, &si_ep[i].elem.text.content);
- }
- } else img->texts = 0;
- return img->texts | img->draws;
- }
-
- #endif
-