⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hpbit_stream_in.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
      YRsiz->buf[c] = *(bp++);
    }
  cp->accesses = 1;
  return(1);
}

/* Begin Aerospace MCT mods (TSW) */
/*****************************************************************************/
/* STATIC                      mct_to_pre_marker                             */
/*****************************************************************************/
static int
mct_to_pre_marker(hpbit_stream_in_ref self,hpbit_markers_ptr markers,
				  hpbit_codestream_marker_ptr cp)
{
	/* Returns 1 if successful in translating marker; 0 otherwise. */
	mct_marker mct;
	hpbit_pre_marker_ptr pm;
	hpbit_marker_elt_ptr elt;
	std_byte *bp;
	int instance, length;
	
	if (markers != &(self->global_markers))
		return(0);
	assert(cp->accesses == 0);
	bp = cp->buf;
	copy_from_big_endian(bp,&(mct.MCT),1,2); bp+=1*2;
	copy_from_big_endian(bp,&(mct.Lmct),1,4); bp+=1*4;	/* Length is 4 bytes */
	length = mct.Lmct;
	mct.Smct = *(bp++);	/* Matrix index, matrix type, data type */
	assert(mct.MCT==MARKER_MCT);/* Verify that this is an MCT marker segment */

	pm = (hpbit_pre_marker_ptr)
		local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_pre_marker));
	pm->next = markers->pre_markers;
	markers->pre_markers = pm;
	pm->id = MARKER_MCT & 0x00FF;	
	instance = mct.Smct & 0x3F;	/* Instance formed from matrix index & type */
	pm->instance = instance;

	/* The lower nibble of Smct contains the matrix index. Bits 4 and 5 */
	/* contain the type of matrix (i.e. xfm vs offset and decorrelation vs */
	/* dependency). Bits 6 and 7 describe the data type (integer-8, integer-16, */
	/* float-32, and float-64). */
	elt = add_elt(pm,MARKER_MCT_IX,1); elt->buf[0] = mct.Smct & 0x0F;
	elt = add_elt(pm,MARKER_MCT_TYP,1); elt->buf[0] = (mct.Smct >> 4) & 0x03;
	elt = add_elt(pm,MARKER_MCT_DT,1); elt->buf[0] = (mct.Smct >> 6) & 0x03;
	/* This routine should handle the conversion of data stored in the MCT
	   marker to floating point if needed. If data is already floating point
	   in the marker, then indirect allocation can be used. Also, note that
	   the data must be copied to an allocated buffer to prevent byte 
	   problems from occurring. */
	elt = add_elt_indirect(pm,MARKER_MCT_SP,length-5); 
	elt->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,length-5);

	/* Right now, we only deal with 32-bit floating point. One must */
	/* view the buffer allocated above as a raw byte buffer. Note that */
	/* all compilers allocate memory such that we can freely coerce */
	/* pointers, thus we can safely coerce the buffer to float* later. */
	if(((mct.Smct>>6)&0x03)==2) 
		memcpy(elt->buf,bp,length-5); 

	/* Since we're trying to minimize the amount of memory consumed by the
	   transform matrices, free up the marker data buffer, since we've now
	   processed the marker and we won't need to do it again. */
	local_free(cp->buf);
	cp->buf = NULL;
	
	cp->accesses = 1;
	return(1);
}

/*****************************************************************************/
/* STATIC                      mcc_to_pre_marker                             */
/*****************************************************************************/
static int
mcc_to_pre_marker(hpbit_stream_in_ref self,hpbit_markers_ptr markers,
				  hpbit_codestream_marker_ptr cp)
{
	/* Returns 1 if successful in translating marker; 0 otherwise. */
	hpbit_pre_marker_ptr pm;
	hpbit_marker_elt_ptr elt,wdmo,xmx,nmcs,nmcn,mmcs,mmcn,tmco,tmcx,cmc,wmc;
	std_byte *bp,*bpstop;
	int index,nseg=0,nin=0,nout=0,i,*cint,*wint;
	std_ushort sh,nbytes,nentries,j;
	long int length;

	/* for now, forcing the MCC to appear in the main header. This
	   should be easily extended to appearing in tile part headers when
	   ready, though we will have to be careful about the instance number.
	   Also, many of the fields are omitted if index != 0 in a tile part
	   header. */

	if (markers != &(self->global_markers))
		return(0);
	assert(cp->accesses == 0);
	bp = cp->buf;
	copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
	assert(sh==MARKER_MCC);		/* Verify that this is an MCC marker segment */
	copy_from_big_endian(bp,&length,1,4); bp += 1*4;	/* Length is 4 bytes */
	index = (int) *(bp++);
	bpstop = cp->buf+length+2;	/* Compute number of bytes in marker */
	/* This loop determines the number of collections and */
	/* the total number of input and output components across */
	/* all of the collections */
	while(bp<bpstop){
		nseg++; bp++; /* skip the xmx and xwdo fields */
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
		nbytes = ( (sh & 0xC000)>>14 ) + 1;
		nentries = (sh & 0x3FFF);
		nin += nentries;
		bp += (nentries * nbytes);
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
		nbytes = ( (sh & 0xC000)>>14 ) + 1;
		nentries = (sh & 0x3FFF);
		nout += nentries;
		bp += (nentries * nbytes);
		bp++; /* skip the tmcx\tmco field */
	}

	pm = (hpbit_pre_marker_ptr)
		local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_pre_marker));
	pm->next = markers->pre_markers;
	markers->pre_markers = pm;
	pm->id = MARKER_MCC & 0x00FF;
	pm->instance = index;

	elt = add_elt(pm,MARKER_MCC_IX,1);
	elt->buf[0] = (std_uint) index;
	wdmo = add_elt(pm,MARKER_MCC_XWDO,nseg);    
	xmx = add_elt(pm,MARKER_MCC_XMX,nseg);
	nmcs = add_elt(pm,MARKER_MCC_NMCS,nseg);
	nmcn = add_elt(pm,MARKER_MCC_NMCN,nseg);
	mmcs = add_elt(pm,MARKER_MCC_MMCS,nseg);
	mmcn = add_elt(pm,MARKER_MCC_MMCN,nseg);
	tmco = add_elt(pm,MARKER_MCC_TMCO,nseg);
	tmcx = add_elt(pm,MARKER_MCC_TMCX,nseg);
	cmc = add_elt_indirect(pm,MARKER_MCC_CMC,nin*sizeof(int));
	wmc = add_elt_indirect(pm,MARKER_MCC_WMC,nout*sizeof(int));
	cmc->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,nin*sizeof(int));
	wmc->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,nout*sizeof(int));
	cint = (int *) cmc->buf;
	wint = (int *) wmc->buf;

	/* The following loop reads through the data of the MCC marker. */
	/* Note that the lists of component indices (cint and wint) span */
	/* all component collections. They are formed as contiguous integer */
	/* arrays. */
	for(i=0,nin=0,nout=0,bp=cp->buf+7;i<nseg;i++){ /* Point to first Xmcc */
		wdmo->buf[i]=(std_uint)((*bp&0xF0)>>4);	/* Wavelet offset matrix index */
		xmx->buf[i]=(std_uint)(*(bp++)&0x0F);	/* Decorrelation xfm type */
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;	/* Read Nmcc */
		nmcs->buf[i]=(std_uint)((sh&0xC000)>>14);	/* Input index byte depth */
		nmcn->buf[i]=(std_uint)(sh&0x3FFF);		/* Number of input components */
		for(j=0;j<nmcn->buf[i];j++){
			if(nmcs->buf[i]==0)		/* Component indices are one byte long */
				cint[nin++] = (int) *(bp++);
			else{					/* Component indices are two bytes long */
				copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
				cint[nin++] = (int) sh;
			}
		}
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;	/* Read Mmcc */
		mmcs->buf[i]=(std_uint)((sh&0xC000)>>14);	/* Output index byte depth */
		mmcn->buf[i]=(std_uint)(sh&0x3FFF);		/* Number of output components */
		for(j=0;j<mmcn->buf[i];j++){
			if(mmcs->buf[i]==0)		/* Component indices are one byte long */
				wint[nout++] = (int) *(bp++);
			else{					/* Component indices are two bytes long */
				copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
				wint[nout++] = (int) sh;
			}
		}
		tmco->buf[i]=(std_uint)((*bp&0xF0)>>4);	/* Offset matrix or ADS index */
		tmcx->buf[i]=(std_uint)(*(bp++)&0x0F);	/* Xfm matrix or ATK index */
	}

	assert(bp == bpstop);
	/* Since we're trying to minimize the amount of memory consumed by the
	   transform matrices, free up the marker data buffer, since we've now
	   processed the marker and we won't need to do it again. */
	local_free(cp->buf);
	cp->buf = NULL;

	cp->accesses = 1;
	return(1);
}

/*****************************************************************************/
/* STATIC                      mic_to_pre_marker                             */
/*****************************************************************************/
static int
mic_to_pre_marker(hpbit_stream_in_ref self,hpbit_markers_ptr markers,
				  hpbit_codestream_marker_ptr cp)
{
	/* Returns 1 if successful in translating marker; 0 otherwise. */
	hpbit_pre_marker_ptr pm;
	hpbit_marker_elt_ptr elt,nmcs,nmcn,mmcs,mmcn,tmco,tmcx,cmc,wmc;
	std_byte *bp,*bpstop;
	int index,nseg=0,nin=0,nout=0,i,*cint,*wint;
	std_ushort sh,nbytes,nentries,j;
	long int length;

	/* for now, forcing the MIC to appear in the main header. This
	   should be easily extended to appearing in tile part headers when
	   ready, though we will have to be careful about the instance number.
	   Also, many of the fields are omitted if index != 0 in a tile part
	   header. */

	if (markers != &(self->global_markers))
		return(0);
	assert(cp->accesses == 0);
	bp = cp->buf;
	copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
	assert(sh==MARKER_MIC);		/* Verify that this is an MCC marker segment */
	copy_from_big_endian(bp,&length,1,4); bp += 1*4;	/* Length is 4 bytes */
	index = (int) *(bp++);
	bpstop = cp->buf+length+2;	/* Compute number of bytes in marker */
	/* This loop determines the number of collections and */
	/* the total number of input and output components across */
	/* all of the collections */
	while(bp<bpstop){
		nseg++;		/* There is no Xmic parameter */
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
		nbytes = ( (sh & 0xC000)>>14 ) + 1;
		nentries = (sh & 0x3FFF);
		nin += nentries;
		bp += (nentries * nbytes);
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
		nbytes = ( (sh & 0xC000)>>14 ) + 1;
		nentries = (sh & 0x3FFF);
		nout += nentries;
		bp += (nentries * nbytes);
		bp++; /* skip the tcmx, tmco field */
	}

	pm = (hpbit_pre_marker_ptr)
		local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_pre_marker));
	pm->next = markers->pre_markers;
	markers->pre_markers = pm;
	pm->id = MARKER_MIC & 0x00FF;
	pm->instance = index;

	elt = add_elt(pm,MARKER_MIC_IX,1);
	elt->buf[0] = (std_uint) index;
	nmcs = add_elt(pm,MARKER_MIC_NMCS,nseg);
	nmcn = add_elt(pm,MARKER_MIC_NMCN,nseg);
	mmcs = add_elt(pm,MARKER_MIC_MMCS,nseg);
	mmcn = add_elt(pm,MARKER_MIC_MMCN,nseg);
	tmco = add_elt(pm,MARKER_MIC_TMCO,nseg);
	tmcx = add_elt(pm,MARKER_MIC_TMCX,nseg);
	cmc = add_elt_indirect(pm,MARKER_MIC_CMC,nin*sizeof(int));
	wmc = add_elt_indirect(pm,MARKER_MIC_WMC,nout*sizeof(int));
	cmc->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,nin*sizeof(int));
	wmc->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,nout*sizeof(int));
	cint = (int *) cmc->buf;
	wint = (int *) wmc->buf;

	/* The following loop reads through the data of the MIC marker. */
	/* Note that the lists of component indices (cint and wint) span */
	/* all component collections. They are formed as contiguous integer */
	/* arrays. */
	for(i=0,nin=0,nout=0,bp=cp->buf+7;i<nseg;i++){	/* Point to first Nmic */
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;	/* Read Nmic */
		nmcs->buf[i]=(std_uint)((sh&0xC000)>>14);	/* Input index byte depth */
		nmcn->buf[i]=(std_uint)(sh&0x3FFF);		/* Number of input components */
		for(j=0;j<nmcn->buf[i];j++){
			if(nmcs->buf[i]==0)		/* Component indices are one byte long */
				cint[nin++] = (int) *(bp++);
			else {					/* Component indices are two bytes long */
				copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
				cint[nin++] = (int) sh;
			}
		}
		copy_from_big_endian(bp,&sh,1,2); bp += 1*2;	/* Read Mmic */
		mmcs->buf[i]=(std_uint)((sh&0xC000)>>14);	/* Output index byte depth */
		mmcn->buf[i]=(std_uint)(sh&0x3FFF);		/* Number of output components */
		for(j=0;j<mmcn->buf[i];j++){
			if(mmcs->buf[i]==0)		/* Component indices are one byte long */
				wint[nout++] = (int) *(bp++);
			else{					/* Component indices are two bytes long */
				copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
				wint[nout++] = (int) sh;
			}
		}
		tmco->buf[i]=(std_uint)((*bp&0xF0)>>4);	/* Offset matrix index */
		tmcx->buf[i]=(std_uint)(*(bp++)&0x0F);	/* Xfm matrix index */
	}

	assert(bp == bpstop);
	/* Since we're trying to minimize the amount of memory consumed by the
	   transform matrices, free up the marker data buffer, since we've now
	   processed the marker and we won't need to do it again. */
	local_free(cp->buf);
	cp->buf = NULL;

	cp->accesses = 1;
	return(1);
}

/*****************************************************************************/
/* STATIC                      cbd_to_pre_marker                             */
/*****************************************************************************/
static int
cbd_to_pre_marker(hpbit_stream_in_ref self,hpbit_markers_ptr markers,
				  hpbit_codestream_marker_ptr cp)
{
	/* Returns 1 if successful in translating marker; 0 otherwise. */
	hpbit_pre_marker_ptr pm;
	hpbit_marker_elt_ptr elt, bd;
	std_byte *bp;
	int i, *bint, sgn, dpth;
	std_ushort sh, length, ncomp, allsame, ninmarker;

	/* The CBD must appear in the main header. */
	if (markers != &(self->global_markers))
		return(0);
	assert(cp->accesses == 0);
	bp = cp->buf;
	copy_from_big_endian(bp,&sh,1,2); bp += 1*2;
	assert(sh==MARKER_CBD);		/* Verify that this is an CBD marker segment */
	copy_from_big_endian(bp,&length,1,2); bp += 1*2;	/* Length is 2 bytes */
	copy_from_big_endian(bp,&ncomp,1,2); bp += 1*2;		/* Number of components */
	/* allsame == 1 means one bitdepth given applies to all components */
	/* This is determined by the most significant bit in ncomp */
	allsame = (ncomp & 0x8000) >> 15;
	/* The rest of ncomp contains the number of components - 1 */
	ncomp = ncomp & 0x7FFF; ncomp++;
	/* if allsame == 1, one value in marker, else ncomp of them */
	ninmarker = (allsame)?1:ncomp;
	assert(ninmarker == (length - 4));	/* Make sure length is correct */

	pm = (hpbit_pre_marker_ptr)
		local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_pre_marker));
	pm->next = markers->pre_markers;
	markers->pre_markers = pm;
	pm->id = MARKER_CBD & 0x00FF;
	pm->instance = 0;

	elt = add_elt(pm,MARKER_CBD_N,1);
	elt->buf[0] = (std_uint) ncomp;
	bd = add_elt_indirect(pm,MARKER_CBD_BD,ncomp*sizeof(int));
	bd->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,ncomp*sizeof(int));
	bint = (int *) bd->buf;
	if(allsame){				/* All components have same bitdepth */
		sgn = (*bp&0x80)>>7;	/* If MSB is set, bitdepth is negative */
		dpth = ((*(bp++)&0x3F)+1)*((sgn)?-1:1);
		for(i=0;i<ncomp;i++)
			bint[i] = dpth;		/* Store the same value for each component */
	}
	else{						/* Each component has bitdepth specified */
		for(i=0;i<ncomp;i++){
			sgn=(*bp&0x80)>>7;
			dpth=((*(bp++)&0x3F)+1)*((sgn)?-1:1);
			bint[i] = dpth;
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -