/* ** NAME ** DFread -- read a portion of a data element ** USAGE ** int32 DFread(dfile, ptr, len) ** DF *dfile; IN: pointer to open DF file ** char *ptr; IN: pointer to space to read data into ** int32 len; IN: number of bytes to read ** RETURNS ** number of bytes read on success, -1 on failure ** DESCRIPTION ** Read bytes from a DF file (part of element specified by DFaccess) ** GLOBAL VARIABLES ** COMMENTS, BUGS, ASSUMPTIONS ** Space for data is assumed to be pre-allocated. ** EXAMPLES ** REVISION LOG */ int32 DFread(DF * dfile, char *ptr, int32 len) { int32 ret; if (DFIcheck(dfile) != 0) { DFerror = DFE_NOTOPEN; return (-1); } else DFerror = DFE_NONE; DFaid = Hstartread(DFid, acc_tag, acc_ref); ret = Hseek(DFaid, DFelseekpos, 0); if (ret == FAIL) { Hendaccess(DFaid); DFerror = (int)HEvalue(1); return (-1); } ret = Hread(DFaid, len, (unsigned char *) ptr); Hendaccess(DFaid); if (ret == FAIL) { DFerror = (int)HEvalue(1); return (-1); } else { DFelseekpos += ret; return (ret); } }
/*-------------------------------------------------------------------------- NAME HCIcszip_decode -- Decode SZIP compressed data into a buffer. USAGE int32 HCIcszip_decode(info,length,buf) compinfo_t *info; IN: the info about the compressed element int32 length; IN: number of bytes to read into the buffer uint8 *buf; OUT: buffer to store the bytes read RETURNS Returns SUCCEED or FAIL DESCRIPTION Common code called to decode SZIP data from the file. GLOBAL VARIABLES COMMENTS, BUGS, ASSUMPTIONS EXAMPLES REVISION LOG --------------------------------------------------------------------------*/ PRIVATE int32 HCIcszip_decode(compinfo_t * info, int32 length, uint8 *buf) { CONSTR(FUNC, "HCIcszip_decode"); #ifdef H4_HAVE_LIBSZ accrec_t *access_rec; comp_coder_szip_info_t *szip_info; /* ptr to SZIP info */ uint8 *in_buffer; uint8 *out_buffer; int32 in_length; int32 out_length; int bytes_per_pixel; int32 rbytes; uint16 tag,ref; int32 len1; int32 aid; int32 status; size_t size_out; uint8 *cp; int32 good_bytes; int32 old_way; SZ_com_t sz_param; #endif #ifdef H4_HAVE_LIBSZ szip_info = &(info->cinfo.coder_info.szip_info); if (szip_info->szip_state == SZIP_INIT) { /* Load from disk, decode the data */ if ((access_rec = HAatom_object(info->aid)) == NULL) /* get the access_rec pointer */ HRETURN_ERROR(DFE_ARGS, FAIL); /* Discover how much data must be read */ if(HTPinquire(access_rec->ddid,&tag,&ref,NULL,&in_length)==FAIL) HRETURN_ERROR(DFE_INTERNAL, FAIL); if (in_length == -1) HRETURN_ERROR(DFE_INTERNAL, FAIL); if (tag & 0x4000) { /* this is linked list -- get the length of the data */ aid = Hstartread(access_rec->file_id, tag, ref); if (HDinqblockinfo(aid, &len1, NULL, NULL, NULL) == FAIL) { Hendaccess(aid); HRETURN_ERROR(DFE_INTERNAL, FAIL); } in_length = len1; Hendaccess(aid); } old_way = (int)(szip_info->options_mask & SZ_H4_REV_2); if (old_way == 0) { /* special case: read data encoded in V4.2r0 */ old_way = 1; good_bytes = in_length; in_length = in_length+5; if ((in_buffer = (uint8 *) HDmalloc(in_length)) == NULL) HRETURN_ERROR(DFE_NOSPACE, FAIL); cp = in_buffer; *cp = 0; cp++; INT32ENCODE(cp, good_bytes); } else { /* V4.2r1: in_length is correct */ old_way = 0; if ((in_buffer = (uint8 *) HDmalloc(in_length)) == NULL) HRETURN_ERROR(DFE_NOSPACE, FAIL); } /* Allocate memory for the uncompressed data */ bytes_per_pixel = (szip_info->bits_per_pixel + 7) >> 3; if (bytes_per_pixel == 3) bytes_per_pixel++; out_length = szip_info->pixels * bytes_per_pixel; if ((out_buffer = (uint8 *) HDmalloc(out_length)) == NULL) HRETURN_ERROR(DFE_NOSPACE, FAIL); /* Read the unompressed data */ if (old_way == 1) { /* this is encoded in V4.2r0 */ /* the preamble isn't in the file, so read only the data */ if ((rbytes = Hread(info->aid, in_length-5, in_buffer+5)) == FAIL) { HDfree(out_buffer); HDfree(in_buffer); HRETURN_ERROR(DFE_READERROR, FAIL); } if (rbytes == 0 || rbytes != (in_length - 5)) { /* is this possible? */ HDfree(out_buffer); HDfree(in_buffer); HRETURN_ERROR(DFE_READERROR, FAIL); } } else { /* HDF4.2R1: read the data plus preamble */ if ((rbytes = Hread(info->aid, in_length, in_buffer)) == FAIL) { HDfree(out_buffer); HDfree(in_buffer); HRETURN_ERROR(DFE_READERROR, FAIL); } if (rbytes == 0 || rbytes != in_length) { /* is this possible? */ HDfree(out_buffer); HDfree(in_buffer); HRETURN_ERROR(DFE_READERROR, FAIL); } } cp = in_buffer; cp++; INT32DECODE(cp, good_bytes); if (in_buffer[0] == 1) { /* This byte means the data was not compressed -- just copy out */ szip_info->szip_state = SZIP_RUN; HDmemcpy(out_buffer, in_buffer+5, good_bytes); szip_info->buffer = out_buffer; szip_info->buffer_pos = 0; szip_info->buffer_size = good_bytes; szip_info->offset = 0; if (good_bytes > length) { /* partial read */ HDmemcpy(buf, in_buffer+5, length); szip_info->buffer_pos += length; szip_info->buffer_size -= length; } else { /* read the whole data block to the user buffer */ HDmemcpy(buf, in_buffer+5, good_bytes); szip_info->buffer_pos += good_bytes; szip_info->buffer_size -= good_bytes; } szip_info->offset = szip_info->buffer_pos; HDfree(in_buffer); if (szip_info->buffer_size == 0) { if (szip_info->buffer != NULL) { HDfree(szip_info->buffer); szip_info->buffer = NULL; } } return (SUCCEED); } /* Decompress the data */ /* set up the parameters */ sz_param.options_mask = (szip_info->options_mask & ~SZ_H4_REV_2); sz_param.bits_per_pixel = szip_info->bits_per_pixel; sz_param.pixels_per_block = szip_info->pixels_per_block; sz_param.pixels_per_scanline = szip_info->pixels_per_scanline; size_out = out_length; if(SZ_OK!= (status = SZ_BufftoBuffDecompress(out_buffer, &size_out, (in_buffer+5), good_bytes, &sz_param))) { HDfree(out_buffer); HDfree(in_buffer); HRETURN_ERROR(DFE_CDECODE, FAIL); } if ((int32)size_out != out_length) { /* This should never happen?? */ printf("status: %d ??bytes != out_length %d != %d\n",status,size_out,out_length); } /* The data is successfully decompressed. Put into the szip struct */ HDfree(in_buffer); szip_info->szip_state = SZIP_RUN; szip_info->buffer = out_buffer; szip_info->buffer_pos = 0; szip_info->buffer_size = out_length; szip_info->offset = 0; } /* copy the data into the return buffer */ if (length > szip_info->buffer_size) { /* can't happen?? panic?? */ if (szip_info->buffer != NULL) { HDfree(szip_info->buffer); szip_info->buffer = NULL; } return (FAIL); } HDmemcpy(buf, szip_info->buffer + szip_info->buffer_pos, length); szip_info->buffer_pos += length; szip_info->buffer_size -= length; szip_info->offset = szip_info->buffer_pos; if (szip_info->buffer_size == 0) { if (szip_info->buffer != NULL) { HDfree(szip_info->buffer); szip_info->buffer = NULL; } } return (SUCCEED); #else /* ifdef H4_HAVE_LIBSZ */ HRETURN_ERROR(DFE_CANTCOMP, FAIL); #endif /* H4_HAVE_LIBSZ */ } /* end HCIcszip_decode() */
/*-------------------------------------------------------------------------- NAME DFPgetpal -- get next palette from file USAGE intn DFPgetpal(filename,palette) char *filename; IN: name of HDF file void * palette; OUT: ptr to the buffer to store the palette in RETURNS SUCCEED on success, FAIL on failure. DESCRIPTION Gets the next palette from the file specified. GLOBAL VARIABLES Lastref, Refset COMMENTS, BUGS, ASSUMPTIONS EXAMPLES REVISION LOG --------------------------------------------------------------------------*/ intn DFPgetpal(const char *filename, void * palette) { CONSTR(FUNC, "DFPgetpal"); int32 file_id; int32 aid; int32 length; intn ret_value = SUCCEED; HEclear(); if (!palette) HGOTO_ERROR(DFE_ARGS, FAIL); if ((file_id = DFPIopen(filename, DFACC_READ)) == FAIL) HGOTO_ERROR(DFE_BADOPEN, FAIL); if (Refset) { aid = Hstartread(file_id, DFTAG_IP8, Refset); if (aid == FAIL) aid = Hstartread(file_id, DFTAG_LUT, Refset); } /* end if */ else if (Readref) { aid = Hstartread(file_id, DFTAG_IP8, Readref); if (aid == FAIL) aid = Hstartread(file_id, DFTAG_LUT, Readref); if (aid != FAIL && (Hnextread(aid, DFTAG_IP8, DFREF_WILDCARD, DF_CURRENT) == FAIL)) { if (Hnextread(aid, DFTAG_LUT, DFREF_WILDCARD, DF_CURRENT) == FAIL) { Hendaccess(aid); aid = FAIL; } /* end if */ } /* end if */ } /* end if */ else { aid = Hstartread(file_id, DFTAG_IP8, DFREF_WILDCARD); if (aid == FAIL) aid = Hstartread(file_id, DFTAG_LUT, DFREF_WILDCARD); } /* end else */ Refset = 0; /* on error, close file and return -1 */ if (aid == FAIL) { ret_value = (HDerr(file_id)); goto done; } if (Hinquire(aid, (int32 *) NULL, (uint16 *) NULL, &Readref, &length, (int32 *) NULL, (int32 *) NULL, (int16 *) NULL, (int16 *) NULL) == FAIL) { Hendaccess(aid); ret_value = HDerr(file_id); goto done; } /* end if */ /* read palette */ if (Hread(aid, length, (uint8 *) palette) == FAIL) { Hendaccess(aid); ret_value = (HDerr(file_id)); goto done; } /* end if */ Hendaccess(aid); Lastref = Readref; ret_value = Hclose(file_id); done: if(ret_value == FAIL) { /* Error condition cleanup */ } /* end if */ /* Normal function cleanup */ return ret_value; } /* end DFPgetpal() */