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

📄 xccxct.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
	else
		big_kahuna->depend_offs = NULL;

	/*--------------- Dependency Transform Matrix -------------- */
	/* Next we read N2, the number of dependency transform matrices. */
	my_sscanf(buffer,"%d",&(big_kahuna->n_depend_xfm),fp,filename);

	if(big_kahuna->n_depend_xfm > 0){
		big_kahuna->depend_xfms =
			(matrix_data *) local_malloc(COMPONENT_COLLECTION_MEM_KEY,
				(big_kahuna->n_depend_xfm+1)*sizeof(matrix_data));
		memset(big_kahuna->depend_xfms,0,
            (big_kahuna->n_depend_xfm+1)*sizeof(matrix_data));

		for(i=1;i<=big_kahuna->n_depend_xfm;i++){
			my_sscanf(buffer,"%d",&rows,fp,filename);
			my_sscanf(buffer,"%d",&cols,fp,filename);
			my_sscanf(buffer,"%d",&data_type,fp,filename);
			if(data_type != 2){
				local_error("Matrix data type must equal 2 (floating point) "
					"file: %s (read_cc_file)",filename);
			}
			/* Allocate storage for matrix */
			big_kahuna->depend_xfms[i].height = rows;
			big_kahuna->depend_xfms[i].width = cols;
			temp_flt_ptr = get_matrix_data(big_kahuna->depend_xfms[i].height,
					big_kahuna->depend_xfms[i].width,DEPEND_XFM,buffer,fp,
					filename);
			/* We need to generate row pointers for 2D matrix access */
			/* so we allocate space for float*, one per row and generate */
			/* our own pointers into the data array returned by */
			/* get_matrix_data. It is possible that get_matrix_data returned */
			/* NULL, so we must check for this. */
			if(temp_flt_ptr != NULL){
				big_kahuna->depend_xfms[i].matrix_values =
					(float **)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
						big_kahuna->depend_xfms[i].height*sizeof(float*));
				big_kahuna->depend_xfms[i].matrix_values[0] = temp_flt_ptr;
				for(j=1;j<big_kahuna->depend_xfms[i].height;j++)
					big_kahuna->depend_xfms[i].matrix_values[j] =
						big_kahuna->depend_xfms[i].matrix_values[j-1]+
						big_kahuna->depend_xfms[i].width;
			}
			else
				big_kahuna->depend_xfms[i].matrix_values = NULL;
		}
	}
	else
		big_kahuna->depend_xfms = NULL;

	/*--------------- Decorrelation Offset Matrix -------------- */
	/* Next we read N3, the number of decorrelation offset matrices. */
	my_sscanf(buffer,"%d",&(big_kahuna->n_decorrel_off),fp,filename);

	if(big_kahuna->n_decorrel_off > 0){
		big_kahuna->decorrel_offs =
			(vector_data *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
				(big_kahuna->n_decorrel_off+1)*sizeof(vector_data));
		memset(big_kahuna->decorrel_offs,0,
            (big_kahuna->n_decorrel_off+1)*sizeof(vector_data));

		for(i=1;i<=big_kahuna->n_decorrel_off;i++){
			my_sscanf(buffer,"%d",&rows,fp,filename);
			my_sscanf(buffer,"%d",&cols,fp,filename);
			my_sscanf(buffer,"%d",&data_type,fp,filename);
			if(data_type != 2){
				local_error("Matrix data type must equal 2 (floating point) "
					"file: %s (read_cc_file)",filename);
			}
			/* Allocate storage for vector */
			big_kahuna->decorrel_offs[i].length = MAX(rows,cols);
			big_kahuna->decorrel_offs[i].vector_values =
				get_matrix_data(1,big_kahuna->decorrel_offs[i].length,
					DECORREL_OFF,buffer,fp,filename);
		}
	}
	else
		big_kahuna->decorrel_offs = NULL;

	/*-------------- Decorrelation Transform Matrix ------------ */
	/* Next we read N4, the number of dependency transform matrices. */
	my_sscanf(buffer,"%d",&(big_kahuna->n_decorrel_xfm),fp,filename);

	if(big_kahuna->n_decorrel_xfm > 0){
		big_kahuna->decorrel_xfms =
			(matrix_data *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
				(big_kahuna->n_decorrel_xfm+1)*sizeof(matrix_data));
		memset(big_kahuna->decorrel_xfms,0,
            (big_kahuna->n_decorrel_xfm+1)*sizeof(matrix_data));

		for(i=1;i<=big_kahuna->n_decorrel_xfm;i++){
			my_sscanf(buffer,"%d",&rows,fp,filename);
			my_sscanf(buffer,"%d",&cols,fp,filename);
			my_sscanf(buffer,"%d",&data_type,fp,filename);
			if(data_type != 2){
				local_error("Matrix data type must equal 2 (floating point) "
					"file: %s (read_cc_file)",filename);
			}
			/* Allocate storage for matrix */
			big_kahuna->decorrel_xfms[i].height = rows;
			big_kahuna->decorrel_xfms[i].width = cols;
			temp_flt_ptr = get_matrix_data(big_kahuna->decorrel_xfms[i].height,
					big_kahuna->decorrel_xfms[i].width,DECORREL_XFM,buffer,
					fp,filename);
			/* We need to generate row pointers for 2D matrix access */
			/* so we allocate space for float*, one per row and generate */
			/* our own pointers into the data array returned by */
			/* get_matrix_data. It is possible that get_matrix_data returned */
			/* NULL, so we must check for this. */
			if(temp_flt_ptr != NULL){
				big_kahuna->decorrel_xfms[i].matrix_values =
					(float **)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
						big_kahuna->decorrel_xfms[i].height*sizeof(float*));
				big_kahuna->decorrel_xfms[i].matrix_values[0] = temp_flt_ptr;
				for(j=1;j<big_kahuna->decorrel_xfms[i].height;j++)
					big_kahuna->decorrel_xfms[i].matrix_values[j] =
						big_kahuna->decorrel_xfms[i].matrix_values[j-1]+
						big_kahuna->decorrel_xfms[i].width;
			}
			else
				big_kahuna->decorrel_xfms[i].matrix_values = NULL;
		}
	}
	else
		big_kahuna->decorrel_xfms = NULL;

	return(big_kahuna);
}
#undef MAX
/* end read_ct_file */

/*****************************************************************************
 * EXTERN                        read_cbd_file                               *
 *****************************************************************************/
/* Reads in a CBD file which contains the bitdepths of the reconstructed image */
/* components coming out of a multiple component transformation process. An */
/* example of a CBD file is given below: */
/* */
/*   /* This is a CBD input file. It gives the bitdepths of  */
/*   /* the reconstructed image components that come out of */
/*   /* the multiple component transformation processing. */
/*   /* This example follows that presented in Annex I of */
/*   /* Part II of the JPEG 2000 standard. */
/*   [0] 10      /* The first component (greyscale band) is */
/*               /* 10-bit unsigned */
/*   [1-3] 8     /* The RGB color composite is 8-bit unsigned */
/*   [4-6,8] -13 /* The 4-band decorrelation data is 13-bit */
/*               /* signed */
/*   [9,7] 12 [10] 6  /* The prediction bands are 12-bit unsigned */
/*                    /* The last band (dark & uncorrelated) is */
/*                    /* 6-bit unsigned */
int *read_cbd_file(char *filename,int *no_components)
{
	char suffix[5],buffer[BUF_LEN];
	char temp_str[255],*collection_list;
	uint length,temp_index,i,list_length = 512;
	int OK,*bitdepth_array = NULL,j;
	BitDepth *BDList_top = NULL;
	BitDepth *the_bitdepths,*temp_ptr;
	Group *temp_collect_ptr;
	FILE *fp;

	/* Check for suffix '.cbd'. If it's not present, throw an error. */
	/* Note we do not care about case. If the case is wrong and we */
	/* are on an operating system that cares, then the file will */
	/* fail to open. */
	length = strlen(filename);
	strncpy(suffix,filename+length-4,5);
	convert_to_lower(suffix);
	if(strcmp(suffix,".fcb")!=0){
		if(strcmp(suffix,".icb")!=0)
			OK=0;
		else
			OK=1;
	}
	else
		OK=1;
    if(!OK){
		local_error("Invalid component transform matrix filename: %s"
			" (read_cbd_file).\nFile name must end in '.fcb' or '.icb'",
			filename);
    }

	fp = fopen(filename,"rt");
	if(fp == NULL){
		local_error("Unable to open file: %s (read_cbd_file)",filename);
	}

	/* The collection_list is used to collect all collection strings */
	/* into one large string before processing by parse_collections. */
	/* This is done due to the behavior of the strtok function. You */
	/* cannot switch between different strings using strtok, it's */
	/* internal static variables become corrupt. So we save all of the */
	/* collection strings then process them. */
	collection_list = (char *)
        local_malloc(COMPONENT_COLLECTION_MEM_KEY,list_length*sizeof(char));
	strcpy(collection_list,"");

	/* Initialize the my_sscanf function */
	my_sscanf(NULL,NULL,NULL,NULL,NULL);
	for(;;){
		/* Read in the first bitdepth collection string */
		OK = my_sscanf(buffer,"%s",temp_str,fp,filename);
		if(OK != 1){
			if(OK == EOF){
				if(feof(fp))
					break;	/* Hit end of file, normal termination */
				else{
					/* A sscanf error occurred during conversion */
					local_error("File I/O error in %s "
                        "(read_cbd_file)",filename);
				}
			}
			else if(OK == 0){
				/* Something wrong with file's format, no conversion performed */
				local_error("File format error in %s "
                    "(read_cbd_file)",filename);
			}
			else{
				/* Really messed up to be in here, should never get here */
				local_error("File error in %s (read_cbd_file)",filename);
			}
		}

		/* Append the collection string list */
		if(strlen(collection_list)+strlen(temp_str)+1 > list_length){
			list_length += 512;
			collection_list = local_realloc(collection_list,list_length);
			if(collection_list == NULL){
				local_error("Component string memory block reallocation"
					" failure (read_cbd_file)");
			}
		}
		strcat(collection_list,temp_str);

		/* Build linked-list and get bitdepths */
		if(BDList_top == NULL){
			/* This is the first bitdepth collection, we are at the top of list */
			BDList_top = the_bitdepths = (BitDepth *)
                local_malloc(COMPONENT_COLLECTION_MEM_KEY,sizeof(BitDepth));
			the_bitdepths->next = NULL;
			/* Read in the bitdepth associated with this collection */
			my_sscanf(buffer,"%d\n",&(the_bitdepths->bitdepth),fp,filename);
		}
		else{
			/* If we have already started the list... */
			the_bitdepths->next = (BitDepth *)
                local_malloc(COMPONENT_COLLECTION_MEM_KEY,sizeof(BitDepth));
			the_bitdepths = the_bitdepths->next;
			the_bitdepths->next = NULL;
			/* Read in the bitdepth associated with this collection */
			my_sscanf(buffer,"%d",&(the_bitdepths->bitdepth),fp,filename);
		}
	}

	/* Parse the collection list string */
	temp_collect_ptr = parse_collection(collection_list);
	/* Now count up the number of components, so we can allocate array */
	*no_components = 0;
	the_bitdepths = BDList_top;
	i = 0;
	do{
		the_bitdepths->bdcollect = temp_collect_ptr->collections+i;
		(*no_components) += the_bitdepths->bdcollect->no_components;
		the_bitdepths = the_bitdepths->next;
		i++;
	}while(the_bitdepths != NULL);
	bitdepth_array = (int *)local_malloc(COMPONENT_COLLECTION_MEM_KEY,
        (*no_components)*sizeof(int));
	/* Free the Group data structure. */
	local_free((void *) temp_collect_ptr);

	/* Finally we can store the bitdepths into the array */
	the_bitdepths = BDList_top;
	do{
		for(j=0;j<the_bitdepths->bdcollect->no_components;j++){
			temp_index = the_bitdepths->bdcollect->indices[j];
			if(bitdepth_array[temp_index] == 0)
				bitdepth_array[temp_index] = the_bitdepths->bitdepth;
			else{
				local_error("Component %d bitdepth set multiple\n times"
					" in file %s (read_cbd_file)",temp_index,filename);
			}
		}
		the_bitdepths = the_bitdepths->next;
	}while(the_bitdepths != NULL);

	/* Check to make sure that all component bitdepths were set. We */
	/* do not allow for components to be skipped. The union of all */
	/* collections must be complete (no holes). */
	OK = 1;
	for(j=0;j<(*no_components);j++){
		if(bitdepth_array[j] == 0){
			local_printf(4,78,"No bitdepth found for component %d out %d\n"
				" in file %s (read_cbd_file)",j,*no_components,filename);
			OK = 0;
		}
	}

	if(!OK)
		local_error("Exiting program (read_cbd_file)");

	/* Time to free some memory */
	the_bitdepths = temp_ptr = BDList_top;
	do{
		if(the_bitdepths->bdcollect != NULL){
			if(the_bitdepths->bdcollect->indices != NULL)
				local_free((void *) the_bitdepths->bdcollect->indices);
		}
		the_bitdepths = the_bitdepths->next;
	}while(the_bitdepths != NULL);

	the_bitdepths = temp_ptr = BDList_top;
	local_free((void *) the_bitdepths->bdcollect);
	do{
		temp_ptr = the_bitdepths->next;
		local_free((void *) the_bitdepths);
		the_bitdepths = temp_ptr;
	}while(the_bitdepths != NULL);

	return(bitdepth_array);
}
/* end read_cbd_file */

#undef ushort
#undef uchar
#undef ulong
#undef uint
#undef BUF_LEN

⌨️ 快捷键说明

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