📄 simple_image_reader.c
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/* File: "simple_image_reader.c" */
/* Description: Simple implementation of the `image_reader' object */
/* Author: David Taubman */
/* Affiliation: Hewlett-Packard and */
/* The University of New South Wales, Australia */
/* Version: VM9.0 */
/* Last Revised: 18 April, 2001 */
/*****************************************************************************/
#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <image_io.h>
#include "simple_image_reader_local.h"
/* ========================================================================= */
/* ---------------------------- Internal Functions ------------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC eat_white */
/*****************************************************************************/
static void
eat_white(FILE *fp)
{
int ch;
while ((ch=getc(fp)) != EOF)
if (ch == '#')
do {
ch = getc(fp);
} while ((ch != '\n') && (ch != EOF));
else if ((ch != ' ') && (ch != '\t') && (ch != '\n') && (ch != '\r'))
{
ungetc(ch,fp);
return;
}
}
/*****************************************************************************/
/* STATIC open_image_file */
/*****************************************************************************/
static void
open_image_file(char *filename, image_component_ptr comp)
{
FILE *fp;
char magic[3];
int cols, rows, bitdepth, is_signed = 0, lsb_first = 0;
comp->filename = (char *)
local_malloc(IMAGE_IO_MEM_KEY,strlen(filename)+1);
strcpy(comp->filename,filename);
comp->fp = fp = fopen(filename,"rb");
if (fp == NULL)
local_error("Unable to open input image file, \"%s\"!",filename);
magic[0] = magic[1] = magic[2] = '\0';
fread(magic,1,2,fp);
if (strcmp(magic,"PG") == 0)
{ /* PGX file format. */
char header[256], *ch;
if ((fgets(header,255,fp) == NULL) || (strchr(header,'\n') == NULL))
local_error("Invalid PGX header in input image, \"%s\"!",filename);
ch = header;
while ((*ch == ' ') || (*ch == '\t'))
ch++;
if (strncmp(ch,"LM",2) == 0)
lsb_first = 1;
else if (strncmp(ch,"ML",2) == 0)
lsb_first = 0;
else
local_error("Cannot find valid byte order specifier in the "
"PGX image file, \"%s\"! Header line is\n\"%s\"",
filename,header);
ch += 2;
while ((*ch == ' ') || (*ch == '\t'))
ch++;
is_signed = 0;
if (*ch == '+')
ch++;
else if (*ch == '-')
{ is_signed = 1; ch++; }
while ((*ch == ' ') || (*ch == '\t'))
ch++;
if ((sscanf(ch,"%d %d %d",&bitdepth,&cols,&rows) != 3) ||
(bitdepth < 1) || (bitdepth > 32) || (cols < 1) || (rows < 1))
local_error("Cannot find valid bit-depth, width and/or height "
"specifiers in the PGX image file, \"%s\"! Header "
"line is\n\"%s\"",filename,header);
}
else if (strcmp(magic,"P5") == 0)
{ /* PGM file format. */
eat_white(fp);
if ((fscanf(fp,"%d",&cols) != 1) || (cols < 1))
local_error("Invalid PNM header in input image, \"%s\"!",filename);
eat_white(fp);
if ((fscanf(fp,"%d",&rows) != 1) || (rows < 1))
local_error("Invalid PNM header in input image, \"%s\"!",filename);
eat_white(fp);
if ((fscanf(fp,"%d",&bitdepth) < 1) ||
(bitdepth > 255) || (bitdepth < 0))
local_error("Invalid PNM header in input image, \"%s\"!",filename);
fgetc(fp);
bitdepth = 8;
is_signed = 0;
lsb_first = 0;
}
else
local_error("Unrecognized image file format encountered while "
"trying to open input image, \"%s\"!",filename);
comp->rows = comp->unread_rows = rows;
comp->cols = cols;
comp->bitdepth = bitdepth;
comp->is_signed = is_signed;
if (bitdepth <= 8)
comp->pack_bytes = 1;
else if (bitdepth <= 16)
comp->pack_bytes = 2;
#if (IMPLEMENTATION_PRECISION >= 32)
else if (bitdepth <= 32)
comp->pack_bytes = 4;
#endif /* IMPLEMENTATION_PRECISION >= 32 */
else
local_error("The input image, \"%s\", contains %d-bit sample "
"values!! Cannot support such high bit-depths "
"with current implementation precision. Try changing the "
"IMPLEMENTATION_PRECISION macro in \"ifc.h\" and "
"recompiling!",filename,bitdepth);
comp->line_bytes = cols*comp->pack_bytes;
comp->reverse_byte_order = 0;
if (comp->pack_bytes > 1)
{
int native_lsb_first;
native_lsb_first = 0;
((std_byte *)(&native_lsb_first))[0] = 1;
native_lsb_first = (native_lsb_first==1)?1:0;
comp->reverse_byte_order = (lsb_first != native_lsb_first);
}
}
/* ========================================================================= */
/* ------------------------- Interface Implementation ---------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC __initialize */
/*****************************************************************************/
static void
__initialize(image_reader_ref base, int *num_components,
cmdl_ref cmdl)
{
/* Begin Aerospace mods (JHK) */
#define MAX_COMPONENTS 1024
/* End Aerospace mods (JHK) */
simple_image_reader_ref self = (simple_image_reader_ref) base;
char **params;
int p;
/* New Multi-component Input Begin */
int pound_offset=0, colon_offset=0; /* TJF: =0 to avoid warnings */
int list_size = 0;
int n, subset_p;
char **subset_params, *subset_params_cpy = NULL, *params_cpy = NULL;
char *pound_pos = NULL, *colon_pos = NULL, *fname = NULL;
char **replacement_chars = NULL;
FILE *fp;
/* New Multi-component Input End */
if ((p = cmdl->extract(cmdl,"-i",-1,¶ms)) <= 0)
local_error("Must supply one or more input files via the"
" `-i' argument!");
self->num_components = p;
/* New Multi-component Input Begin */
if ((p == 1) && ((pound_pos = strchr(params[0], '#')) != NULL)) {
if ((fp = fopen(params[0], "rb")) != NULL) {
fclose(fp);
}
else {
/* Set up possible list of replacement characters for #'s */
replacement_chars = (char **)
local_malloc(IMAGE_IO_MEM_KEY,MAX_COMPONENTS*sizeof(char *));
replacement_chars[0] = (char *)
local_malloc(IMAGE_IO_MEM_KEY,MAX_COMPONENTS*5*sizeof(char));
for (n=1;n<MAX_COMPONENTS;n++)
replacement_chars[n] = replacement_chars[0]+5*n;
if ((subset_p=cmdl->extract(cmdl,"-i_subset",-1,&subset_params))>0){
if ((subset_p == 1) &&
((colon_pos=strchr(subset_params[0],':'))!=NULL)){
subset_params_cpy = (char *) local_malloc(IMAGE_IO_MEM_KEY,
strlen(subset_params[0])+1);
memcpy(subset_params_cpy,subset_params[0],
strlen(subset_params[0]));
colon_offset = colon_pos - subset_params[0];
subset_params_cpy[colon_offset] = '\0';
for (n=atoi(subset_params_cpy);
n<=atoi(subset_params_cpy+colon_offset+1);n++){
sprintf(replacement_chars[n-atoi(subset_params_cpy)],
"%d",n);
list_size++;
}
}
else{
list_size=subset_p;
for (n=0;n<subset_p;n++){
strcpy(replacement_chars[n], subset_params[n]);
}
}
}
else{
list_size = MAX_COMPONENTS;
for (n=0;n<MAX_COMPONENTS;n++){
/* Begin Aerospace modification for zero-based indexing (JHK) */
#ifdef ZERO_INDEXING
sprintf(replacement_chars[n],"%d",n);
#else
sprintf(replacement_chars[n],"%d",n+1);
#endif
/* End Aerospace modification for zero-based indexing (JHK) */
}
}
/* Check out all files with pound character replaced with
replacement_chars */
fname=(char *)local_malloc(IMAGE_IO_MEM_KEY,strlen(params[0])+5);
params_cpy=(char *)local_malloc(IMAGE_IO_MEM_KEY,
strlen(params[0])+1);
memcpy(params_cpy,params[0],strlen(params[0]));
pound_offset=pound_pos-params[0];
params_cpy[pound_offset]='\0';
for (n=0;n<list_size;n++) {
sprintf(fname,"%s%s%s",params_cpy,replacement_chars[n],
params_cpy+pound_offset+1);
if ((fp = fopen(fname, "rb")) != NULL) {
fclose(fp);
}
else {
break;
}
}
self->num_components = n;
if (self->num_components == 0)
local_error("Metacharacter replacement of `#' for `-i' "
"cannot open any files!");
}
}
/* New Multi-component Input End */
self->components = (image_component_ptr)local_malloc(IMAGE_IO_MEM_KEY,
sizeof(image_component)*self->num_components);
/* New Multi-component Input Begin Mods */
for (p=0; p < self->num_components; p++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -