📄 mcraw_image_reader.c
字号:
/*****************************************************************************/
/* Copyright 2000, Eastman Kodak Company */
/* All rights reserved */
/* File: "mcraw_image_reader.c" */
/* Description: Multi-component raw image implementation of the */
/* `image_reader' object */
/* Author: Austin Lan */
/* Affiliation: Eastman Kodak Company */
/* Version: VM9.0 */
/* Last Revised: 18 April, 2001 */
/*****************************************************************************/
#include <image_io.h>
#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "mcraw_image_reader_local.h"
#include "mcraw_common.h"
/* ========================================================================= */
/* ---------------------------- Internal Functions ------------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC read_bit_depths */
/*****************************************************************************/
static void
read_bit_depths(char *bps_fname, int *bitdepths, int *is_signed, int num)
{
int n;
FILE *fp;
if ((fp = fopen(bps_fname, "r")) == NULL) {
local_error("Unable to open the specified bit depth file, %s", bps_fname);
}
for (n=0; n<num; n++) {
int val1, val2;
if (fscanf(fp, "%d %d", &val1, &val2) != 2) {
local_error("Unable to read bitdepth and sign value from file, %s", bps_fname);
}
if ((val1 < 0) || (val2 < 0)) {
local_error("Invalid bitdepth and/or sign value found in file, %s", bps_fname);
}
bitdepths[n] = val1;
is_signed[n] = val2;
}
fclose(fp);
}
/* ========================================================================= */
/* ------------------------- Interface Implementation ---------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC __initialize */
/*****************************************************************************/
static void
__initialize(image_reader_ref base, int *num_components,
cmdl_ref cmdl)
{
mcraw_image_reader_ref self = (mcraw_image_reader_ref) base;
char *bps_fname = NULL;
int bps = -1, is_signed = 1;
int max_bitdepth;
int n, p;
char **params;
/* Set up some defaults before parsing command line:
data type = signed short
bits per sample = 16
data organization = BIL
skip bytes = 0
no byte swapping */
self->filename = NULL;
self->rows = self->cols = self->num_components = 0;
self->pack_bytes = 2;
self->reverse_byte_order = 0;
self->data_org = BIL;
self->skip_bytes = 0;
/* EKC mct begin */
if ((p = cmdl->extract(cmdl,"-i",-1,¶ms)) == 1) {
self->filename = (char *) local_malloc(IMAGE_IO_MEM_KEY, strlen(params[0])+1);
strcpy(self->filename, params[0]);
} else {
local_error("The `-i' argument is invalid.");
}
/* EKC mct end */
if ((p = cmdl->extract(cmdl,"-rcb",-1,¶ms)) == 3) {
self->rows = atoi(params[0]);
self->cols = atoi(params[1]);
self->num_components = atoi(params[2]);
} else {
local_error("The `-rcb' argument is invalid.");
}
if ((p = cmdl->extract(cmdl,"-dt",-1,¶ms)) == 1) {
if (strcmp(params[0], "uchar") == 0) {
is_signed = 0;
self->pack_bytes = 1;
} else if (strcmp(params[0], "char") == 0) {
is_signed = 1;
self->pack_bytes = 1;
} else if (strcmp(params[0], "ushort") == 0) {
is_signed = 0;
self->pack_bytes = 2;
} else if (strcmp(params[0], "short") == 0) {
is_signed = 1;
self->pack_bytes = 2;
} else if (strcmp(params[0], "ulong") == 0) {
is_signed = 0;
self->pack_bytes = 4;
} else if (strcmp(params[0], "long") == 0) {
is_signed = 1;
self->pack_bytes = 4;
} else {
local_error("Valid values for the `-dt' argument are \"uchar\", \"char\", "
"\"ushort\", \"short\", \"ulong\", \"long\"");
}
}
if ((p = cmdl->extract(cmdl,"-bps",-1,¶ms)) == 1) {
if (isdigit((int)params[0][0])) {
bps = atoi(params[0]);
} else {
bps_fname = params[0];
}
}
if ((p = cmdl->extract(cmdl,"-do",-1,¶ms)) == 1) {
if (strcmp(params[0], "bsq") == 0) {
self->data_org = BSQ;
} else if (strcmp(params[0], "bil") == 0) {
self->data_org = BIL;
} else if (strcmp(params[0], "bip") == 0) {
self->data_org = BIP;
} else {
local_error("Valid values for the `-do' argument are \"bsq\", \"bil\", \"bip\"");
}
}
if ((p = cmdl->extract(cmdl,"-skip",-1,¶ms)) == 1) {
self->skip_bytes = atoi(params[0]);
}
if ((p = cmdl->extract(cmdl,"-swap",-1,¶ms)) == 0) {
self->reverse_byte_order = 1;
}
/* Error checking */
/* EKC mct begin */
if (self->filename == NULL)
local_error("Must supply an input file via the `-i' argument!");
/* EKC mct end */
if ((self->rows <= 0) || (self->cols <= 0) || (self->num_components <= 0))
local_error("Must supply valid dimensions and number of components via the "
"`-rcb' argument!");
if ((self->reverse_byte_order) && (self->pack_bytes == 1)) {
local_printf(0,76,"\nWARNING");
local_printf(6,70, "No byte swapping will be applied since the input file byte "
"depth is only a single byte.");
self->reverse_byte_order = 0;
}
/* Set up the `bitdepths' and `is_signed' fields of mcraw_image_reader_obj */
self->bitdepths =
(int *) local_malloc(IMAGE_IO_MEM_KEY, (self->num_components * sizeof(int)));
self->is_signed =
(int *) local_malloc(IMAGE_IO_MEM_KEY, (self->num_components * sizeof(int)));
if (bps_fname != NULL) {
read_bit_depths(bps_fname, self->bitdepths, self->is_signed, self->num_components);
} else {
/* Take either default or input value for `is_signed' (from "-dt") */
for (n=0; n<self->num_components; n++) {
self->is_signed[n] = is_signed;
}
if (bps > 0) {
for (n=0; n<self->num_components; n++) {
self->bitdepths[n] = bps;
}
} else {
/* Calculate a default bit depth according to data type specified */
for (n=0; n<self->num_components; n++) {
self->bitdepths[n] = (8 * self->pack_bytes);
}
}
}
/* More error checking */
max_bitdepth = -1;
for (n=0; n<self->num_components; n++) {
if (self->bitdepths[n] > max_bitdepth)
max_bitdepth = self->bitdepths[n];
}
if (max_bitdepth > (self->pack_bytes * 8)) {
char data_type[80];
if (self->pack_bytes == 1) {
sprintf(data_type, "%s", (is_signed) ? "char" : "uchar");
} else if (self->pack_bytes == 2) {
sprintf(data_type, "%s", (is_signed) ? "short" : "ushort");
} else if (self->pack_bytes == 4) {
sprintf(data_type, "%s", (is_signed) ? "long" : "ulong");
}
local_error("The bit depth (max %d) and input file data type (\"%s\") "
"parameters are incompatible", max_bitdepth, data_type);
}
/* Open the user-specified image file */
if ((self->fp = fopen(self->filename, "rb")) == NULL) {
local_error("Unable to open the specified image file, %s", self->filename);
}
/* Initialize the data buffer */
self->current_row_idx = -1;
self->comp_read_so_far = self->num_components;
self->line_buf =
(void **) local_malloc(IMAGE_IO_MEM_KEY, (self->num_components * sizeof(void *)));
self->line_buf[0] =
(void *) local_malloc(IMAGE_IO_MEM_KEY,
(self->num_components * self->cols * self->pack_bytes));
for (n=1; n<self->num_components; n++) {
self->line_buf[n] = (char *)self->line_buf[0] + (n * self->cols * self->pack_bytes);
}
/* Print input image information to the screen for the user */
local_printf(0,76,"\nInformation on input multi-component raw image:");
local_printf(6,70,"(rows, cols, components) = (%d, %d, %d)",
self->rows, self->cols, self->num_components);
local_printf(6,70,"Signed: %s", (is_signed) ? "YES" : "NO");
local_printf(6,70,"Data storage: %d byte%s", self->pack_bytes,
(self->pack_bytes > 1) ? "s" : "");
local_printf(6,70,"Data organization: %s", (self->data_org == BSQ) ? "Band-sequential" :
(self->data_org == BIL) ? "Band-interleaved-by-line" :
"Band-interleaved-by-pixel");
local_printf(6,70,"Swap bytes: %s", (self->reverse_byte_order) ? "YES" : "NO");
local_printf(6,70,"Skip bytes: %d", self->skip_bytes);
local_printf(6,70,"Bit depths: %d...%d",
self->bitdepths[0], self->bitdepths[self->num_components-1]);
local_printf(0,76,"\n");
*num_components = self->num_components;
}
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -