/* ** NAME ** DFputelement -- write an entire data element ** USAGE ** int DFputelement(dfile, tag, ref, ptr, len) ** DF *dfile; IN: pointer to open DF file ** uint16 tag; IN: tag of data element ** uint16 ref; IN: ref number of data element ** char *ptr; IN: pointer to data element ** int32 len; IN: length of data element ** RETURNS ** Number of bytes written on success, -1 on failure ** DESCRIPTION ** Write an entire data element to HDF file. ** GLOBAL VARIABLES ** COMMENTS, BUGS, ASSUMPTIONS ** EXAMPLES ** REVISION LOG */ int32 DFputelement(DF * dfile, uint16 tag, uint16 ref, char *ptr, int32 len) { if (DFIcheck(dfile) != 0) { DFerror = DFE_NOTOPEN; return (-1); } else DFerror = DFE_NONE; /* test if (DFelstat == DFEL_RESIDENT) { Hputelement(DFid, acc_tag, acc_ref, DFelement, DFelsize); HDfree(DFelement); DFIclearacc(); } test */ if (Hputelement(DFid, tag, ref, (unsigned char *) ptr, len) == FAIL) { DFerror = (int)HEvalue(1); return (-1); } else return (Hlength(DFid, tag, ref)); }
/*-------------------------------------------------------------------------- NAME DFPputpal -- Write palette to file USAGE intn DFPputpal(filename,palette,overwrite,filemode) char *filename; IN: name of HDF file void * palette; IN: ptr to the buffer retrieve the palette from intn overwrite; IN: whether to (1) overwrite last palette written, or (0) write it as a fresh palette char *filemode; IN: if "a" append palette to file, "w" create new file RETURNS SUCCEED on success, FAIL on failure. DESCRIPTION Stores a palette in an HDF file, with options for creating new file or appending, and overwriting last palette written. GLOBAL VARIABLES COMMENTS, BUGS, ASSUMPTIONS To overwrite, the filename must be the same as for the previous call. EXAMPLES REVISION LOG --------------------------------------------------------------------------*/ intn DFPputpal(const char *filename, const void * palette, intn overwrite, const char *filemode) { CONSTR(FUNC, "DFPputpal"); int32 file_id; intn ret_value = SUCCEED; HEclear(); if (!palette) HGOTO_ERROR(DFE_ARGS, FAIL); if (overwrite && HDstrcmp(filename, Lastfile)) HGOTO_ERROR(DFE_BADCALL, FAIL); file_id = DFPIopen(filename, (*filemode == 'w') ? DFACC_CREATE : DFACC_WRITE); if (file_id == FAIL) HGOTO_ERROR(DFE_BADOPEN, FAIL); /* if we want to overwrite, Lastref is the ref to write. If not, if Writeref is set, we use that ref. If not we get a fresh ref. The ref to write is placed in Lastref */ if (!overwrite) Lastref = (uint16) (Writeref ? Writeref : Htagnewref(file_id,DFTAG_IP8)); if (Lastref == 0) HGOTO_ERROR(DFE_NOREF, FAIL); Writeref = 0; /* don't know ref to write after this */ /* write out palette */ if (Hputelement(file_id, DFTAG_IP8, Lastref, (const uint8 *) palette, (int32) 768) < 0) { ret_value = (HDerr(file_id)); goto done; } /* Check for the tag/ref before creating it willy-nilly */ if(Hexist(file_id,DFTAG_LUT,Lastref)==FAIL) Hdupdd(file_id, DFTAG_LUT, Lastref, DFTAG_IP8, Lastref); ret_value = (Hclose(file_id)); done: if(ret_value == FAIL) { /* Error condition cleanup */ } /* end if */ /* Normal function cleanup */ return ret_value; } /* end DFPputpal() */
/* ** NAME ** DFclose -- close HDF file ** USAGE ** int DFclose(dfile) ** DF *dfile; IN: pointer to an open DF file ** RETURNS ** 0 on success, -1 on failure with DFerror set ** DESCRIPTION ** Write out updated DDs; close DF file. ** GLOBAL VARIABLES ** COMMENTS, BUGS, ASSUMPTIONS ** EXAMPLES ** REVISION LOG */ int DFclose(DF * dfile) { int ret; if (DFIcheck(dfile) != 0) { DFerror = DFE_NOTOPEN; return (FAIL); } else DFerror = DFE_NONE; if (DFelstat == DFEL_RESIDENT) { Hputelement(DFid, acc_tag, acc_ref, (unsigned char *) DFelement, DFelsize); HDfree(DFelement); } else Hendaccess(DFaid); if (search_stat == DFSRCH_OLD) { Hendaccess(search_aid); search_aid = 0; } ret = Hclose(DFid); if (ret == 0) { dfile = 0; DFlist = (DF *) NULL; DFid = 0; DFaccmode = 0; } else { DFerror = (int)HEvalue(1); } return (ret); }
intn DFputcomp(int32 file_id, uint16 tag, uint16 ref, const uint8 *image, int32 xdim, int32 ydim, uint8 *palette, uint8 *newpal, int16 scheme, comp_info * cinfo) { CONSTR(FUNC, "DFputcomp"); uint8 *buffer; /* buffer to hold compressed image */ const uint8 *in; /* pointer to input for compression */ uint8 *out; /* pointer to space for compressed output */ int32 cisize; /* maximum size of compressed image */ int32 crowsize; /* maximum size of compressed row */ intn buftype; /* buftype = 1: buffer enough for whole image */ /* buftype = 2: buffer holds 1 row */ int32 n; /* number of compressed bytes produced */ int32 total; /* total compressed bytes produced so far */ int32 i; int32 ret = 0; int32 aid = 0; if (!HDvalidfid(file_id) || !tag || !ref || xdim <= 0 || ydim <= 0 || !image) HRETURN_ERROR(DFE_ARGS, FAIL); switch (scheme) { case DFTAG_RLE: /* RLE compression (8-bit or 24-bit(?) images */ cisize = ydim * (xdim * 121 / 120 + 1); /* 120 chars can compress to 121! */ crowsize = xdim * 121 / 120 + 128; /* allocate buffer for compression */ buffer = (uint8 *) HDmalloc((uint32) cisize); if (!buffer) { buffer = (uint8 *) HDmalloc((uint32) crowsize); if (!buffer) HRETURN_ERROR(DFE_NOSPACE, FAIL); buftype = 2; /* compress and write out row by row */ } else /* can hold whole image, then write */ buftype = 1; in = image; out = buffer; n = total = 0; /* no bytes compressed so far */ if (buftype == 2) { int32 num_blocks; int32 block_length; num_blocks = (ydim > (int32) R8_MAX_BLOCKS) ? (int32) R8_MAX_BLOCKS : ydim; block_length = (xdim > (int32) R8_MAX_LENGTH) ? (int32) R8_MAX_LENGTH : xdim; aid = HLcreate(file_id, tag, ref, block_length, num_blocks); if (aid == FAIL) return FAIL; } /* compress row by row */ for (i = 0; i < ydim; i++) { n = DFCIrle(in, out, xdim); /* compress row */ in += xdim; /* move input pointer */ total += n; /* keep running total */ if (buftype == 1) /* can hold whole image */ out = &buffer[total]; /* move out buffer pointer */ else { /* buffer too small, */ /* write out what was produced */ if (Hwrite(aid, n, buffer) == FAIL) { ret = FAIL; /* flag value */ break; } out = buffer; /* reset output pointer */ } } if (buftype == 1) { /* write out entire image */ ret = Hputelement(file_id, tag, ref, buffer, total); HDfree((VOIDP) buffer); } break; case DFTAG_IMC: /* IMCOMP compression (8-bit images) */ if (!palette || !newpal) /* need palette and newpal */ HRETURN_ERROR(DFE_ARGS, FAIL); cisize = xdim * ydim / 4; /* IMCOMP always cuts to 1/4 */ buffer = (uint8 *) HDmalloc((uint32) cisize); if (!buffer) HRETURN_ERROR(DFE_NOSPACE, FAIL); DFCIimcomp(xdim, ydim, image, buffer, palette, newpal, 0); ret = Hputelement(file_id, tag, ref, buffer, cisize); HDfree((VOIDP) buffer); break; case DFTAG_JPEG5: /* JPEG compression (for 24-bit images) */ case DFTAG_GREYJPEG5: /* JPEG compression (for 8-bit images) */ ret = DFCIjpeg(file_id, tag, ref, xdim, ydim, image, scheme, cinfo); break; default: /* unknown compression scheme */ HRETURN_ERROR(DFE_BADSCHEME, FAIL) } return ((intn) ret); } /* end DFputcomp() */
int32 vimakecompat(HFILEID f) { VGROUP *vg; VDATA *vs; uint8 *buf = NULL; /* to store an old vdata or vgroup descriptor */ int32 old_bsize = 0, bsize=0; int32 aid; int32 ret; uintn u; uint16 tag=DFTAG_NULL, ref=DFTAG_NULL; CONSTR(FUNC, "vimakecompat"); /* ============================================= */ /* --- read all vgs and convert each --- */ /* allocate space for vg */ if (NULL == (vg =VIget_vgroup_node())) HRETURN_ERROR(DFE_NOSPACE, 0); ret = aid = Hstartread(f, (uint16) OLD_VGDESCTAG, DFREF_WILDCARD); while (ret != FAIL) { HQuerytagref(aid, &tag, &ref); HQuerylength(aid, &bsize); if (buf == NULL || bsize > old_bsize) { if (buf != NULL) HDfree((VOIDP) buf); if ((buf = (uint8 *) HDmalloc(bsize)) == NULL) HRETURN_ERROR(DFE_NOSPACE, 0); old_bsize = bsize; } /* end if */ ret = Hgetelement(f, (uint16) OLD_VGDESCTAG, ref, (uint8 *) buf); if (ret == FAIL) { HDfree((VOIDP) buf); HRETURN_ERROR(DFE_READERROR, 0) } /* end if */ oldunpackvg(vg, buf, &bsize); /* add new items */ vg->vgclass = NULL; /* vg->vgclass[0] = '\0'; */ vg->extag = 0; vg->exref = 0; vg->version = 2; /* version 2 */ vg->more = 0; /* inside each vgroup, change the old tags to new */ for (u = 0; u < (uintn)vg->nvelt; u++) if (vg->tag[u] == OLD_VGDESCTAG) vg->tag[u] = NEW_VGDESCTAG; else if (vg->tag[u] == OLD_VSDESCTAG) vg->tag[u] = NEW_VSDESCTAG; else /* BAD */ HERROR(DFE_NOTINSET); vpackvg(vg, buf, &bsize); ret = Hputelement(f, VGDESCTAG, ref, (uint8 *) buf, bsize); HDfree((VOIDP) buf); if (ret == FAIL) HRETURN_ERROR(DFE_WRITEERROR, 0); ret = Hnextread(aid, (uint16) OLD_VGDESCTAG, DFREF_WILDCARD, DF_CURRENT); } /* while */