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

📄 simple_image_reader.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
		if (replacement_chars == NULL)
			open_image_file(params[p],self->components+p);
		else {
			sprintf(fname, "%s%s%s", params_cpy, replacement_chars[p], 
				params_cpy+pound_offset+1);
			open_image_file(fname,self->components+p);
		}
	if (params_cpy != NULL) local_free(params_cpy);
	if (subset_params_cpy != NULL) local_free(subset_params_cpy);

	if (replacement_chars != NULL) {
		local_free(fname);
		local_free(replacement_chars[0]);
		local_free(replacement_chars);
	}
	/* New Multi-component Input End Mods */

	if (pound_pos != NULL)
		params[0][pound_offset] = '#';
	if (colon_pos != NULL)
		params[0][colon_offset] = ':';
	*num_components = self->num_components;
#undef MAX_COMPONENTS
}

/*****************************************************************************/
/* STATIC                         __get_usage                                */
/*****************************************************************************/

static char **
__get_usage(image_reader_ref base)
{
	static char *args[] = {
		"-i <image file> [<image file> [...]]",
			"Mandatory argument, identifying one or more image files; with "
			"one file for each component.  The simple image reader supports binary "
			"PGM and PGX files only.  PGX is a trivial monochrome file format "
			"defined just to enable simple testing of JPEG2000 with image data "
			"having unusual bit-depths.  The file consists of a single text header "
			"line of the form \"PG <byte order> [+|-]<bit-depth> <cols> <rows>\", "
			"with the binary data appearing immediately after the newline "
			"character, packed into 1, 2 or 4 bytes per sample, depending upon the "
			"value of <bit-depth>.  The <byte order> field must be one of the fixed "
			"strings, \"LM\" (LSB's first) or \"ML\" (MSB's first), while the "
			"optional `+' (default) or `-' character preceding the <bit-depth> "
			"field indicates whether the data is signed or not.  Any bit-depth "
			"from 1 to 32 is acceptable.  Each component can be specified "
			"separately through this command line argument.  Alternatively, the "
			"wild card character `#' can be used to allow replacement with a "
			"one-based component index.  If the file name presented to this "
			"argument includes the `#' character, that filename is first checked "
			"for existence.  If not found, the file names with `#' replacement "
			"character `1' to '1024' are checked for existence.  The actual "
			"replacement characters used as indices can also be controlled with "
			"the `-i_subset' argument.  All consequtive component file names "
			"generated with the replacement character list will be used in "
			"compression.",
			"-i_subset <component_subset>", 
			"This argument can control the list of replacement characters used for "
			"the `#' character of the filename specified with the `-i' argument.  "
			"This list of replacement characters defaults to the range `1' to `256',"
			"but this argument can specify individual component indices (as in "
			"`-i_subset 2 4 6 8' or `-i_subset r g b') or an inclusive range of "
			"component indices (as in `-i_subset 1:10').",
			NULL};
		
		return(args);
}

/*****************************************************************************/
/* STATIC                         __get_dims                                 */
/*****************************************************************************/

static void
__get_dims(image_reader_ref base, int component_idx, canvas_dims_ptr dims)
{
	simple_image_reader_ref self = (simple_image_reader_ref) base;
	
	assert(component_idx < self->num_components);
	dims->rows = self->components[component_idx].rows;
	dims->cols = self->components[component_idx].cols;
}

/*****************************************************************************/
/* STATIC                      __get_dynamic_range                           */
/*****************************************************************************/

static int
__get_dynamic_range(image_reader_ref base, int component_idx)
{
	simple_image_reader_ref self = (simple_image_reader_ref) base;
	image_component_ptr comp;
	int result;
	
	assert(component_idx < self->num_components);
	comp = self->components + component_idx;
	result = comp->bitdepth;
	if (comp->is_signed)
		result = -result;
	return(result);
}

/*****************************************************************************/
/* STATIC                         __pull_line                                */
/*****************************************************************************/

static void
__pull_line(image_reader_ref base, ifc_int *line_buf, int component_idx,
			int width)
{
	simple_image_reader_ref self = (simple_image_reader_ref) base;
	image_component_ptr comp;
	ifc_int *dp, offset, mask, val;
	int n;
	
	comp = self->components + component_idx;
	assert((component_idx < self->num_components) &&
		(width == comp->cols) && (comp->unread_rows > 0));
	if (fread(line_buf,1,(size_t)(comp->line_bytes),comp->fp) !=
		(size_t)(comp->line_bytes))
		local_error("Image input file, \"%s\", terminated unexpectedly!",
		comp->filename);
	comp->unread_rows--;
	
	/* See if we need to reverse the byte order. */
	
	if (comp->reverse_byte_order)
    {
		if (comp->pack_bytes == 2)
        {
			std_short *sp, val;
			
			for (sp=(std_short *) line_buf, n=width; n > 0; n--)
            {
				val = *sp;
				val = ((val >> 8) & 0x00FF) | (val << 8);
				*(sp++) = val;
            }
        }
		else if (comp->pack_bytes == 4)
        {
			std_int *sp, val;
			
			for (sp=(std_int *) line_buf, n=width; n > 0; n--)
            {
				val = *sp;
				val = ((val>>24) & 0x000000FF) | ((val>>8) & 0x0000FF00) |
                    ((val<<8)  & 0x00FF0000) | (val<<24);
				*(sp++) = val;
            }
        }
		else
			assert(0);
    }
	
	/* Unpack and sign extend source words. */
	
	offset = 0;
	mask = -1; 
	if (comp->bitdepth < 32) {
		mask <<= (ifc_int)(comp->bitdepth); 
		mask = ~mask;
	}
	if (comp->is_signed)
    {
		offset = 1;
		offset <<= (comp->bitdepth-1);
        /* Add offset to input words, mask off unused MSB's and subract
		offset again to get correct signed representation, regardless
		of what the most significant bits used to be. */
    }
	
	switch (comp->pack_bytes) {
    case 1:
		{
			std_byte *sp;
			
			for (dp=line_buf, sp=(std_byte *) dp, n=width, sp+=n, dp+=n; n>0; n--)
			{
				val = (ifc_int)(*(--sp));
				val += offset;
				val &= mask;
				val -= offset;
				*(--dp) = val;
			}
		} break;
    case 2:
		{
			std_short *sp;
			
			for (dp=line_buf, sp=(std_short *) dp, n=width, sp+=n, dp+=n; n>0; n--)
			{
				val = (ifc_int)(*(--sp));
				val += offset;
				val &= mask;
				val -= offset;
				*(--dp) = val;
			}
		} break;
#if (IMPLEMENTATION_PRECISION >= 32)
    case 4:
		{
			std_int *sp;
			
			for (dp=line_buf, sp=(std_int *) dp, n=width, sp+=n, dp+=n; n>0; n--)
			{
				val = (ifc_int)(*(--sp));
				val += offset;
				val &= mask;
				val -= offset;
				*(--dp) = val;
			}
		} break;
#endif /* IMPLEMENTATION_PRECISION >= 32 */
    default:
		assert(0);
	}
}

/*****************************************************************************/
/* STATIC                         __terminate                                */
/*****************************************************************************/

static void
__terminate(image_reader_ref base)
{
	simple_image_reader_ref self = (simple_image_reader_ref) base;
	image_component_ptr comp;
	int n;
	
	if (self->components != NULL)
    {
		for (n=0; n < self->num_components; n++)
        {
			comp = self->components + n;
			fclose(comp->fp);
			local_free(comp->filename);
        }
		local_free(self->components);
    }
	local_free(self);
}

/*****************************************************************************/
/* EXTERN                   create_simple_image_reader                       */
/*****************************************************************************/

image_reader_ref
create_simple_image_reader(void)
{
	simple_image_reader_ref result;
	
	result = (simple_image_reader_ref)
		local_malloc(IMAGE_IO_MEM_KEY,sizeof(simple_image_reader_obj));
	result->base.initialize = __initialize;
	result->base.get_usage = __get_usage;
	result->base.get_dims = __get_dims;
	result->base.get_dynamic_range = __get_dynamic_range;
	result->base.pull_line = __pull_line;
	result->base.terminate = __terminate;
	return((image_reader_ref) result);
}

⌨️ 快捷键说明

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