📄 jpeg2ktopam.c
字号:
/***************************************************************************** jpeg2kopam****************************************************************************** Convert a JPEG-2000 code stream image to a PNM or PAM By Bryan Henderson, San Jose CA 2002.10.26*****************************************************************************/#define _BSD_SOURCE 1 /* Make sure strdup() is in string.h */#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */#include <string.h>#include "pam.h"#include "shhopt.h"#include "nstring.h"#include "mallocvar.h"#include <jasper/jasper.h>#include "libjasper_compat.h"enum compmode {COMPMODE_INTEGER, COMPMODE_REAL};enum progression {PROG_LRCP, PROG_RLCP, PROG_RPCL, PROG_PCRL, PROG_CPRL};struct cmdlineInfo { /* All the information the user supplied in the command line, in a form easy for the program to use. */ char * inputFilename; unsigned int debuglevel; /* Jasper library debug level */ unsigned int verbose;};static voidparseCommandLine(int argc, char ** argv, struct cmdlineInfo * const cmdlineP) {/*---------------------------------------------------------------------------- Note that many of the strings that this function returns in the *cmdline_p structure are actually in the supplied argv array. And sometimes, one of these strings is actually just a suffix of an entry in argv!-----------------------------------------------------------------------------*/ optEntry *option_def = malloc(100*sizeof(optEntry)); /* Instructions to OptParseOptions3 on how to parse our options. */ optStruct3 opt; unsigned int debuglevelSpec; unsigned int option_def_index; option_def_index = 0; /* incremented by OPTENTRY */ OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); OPTENT3(0, "debuglevel", OPT_UINT, &cmdlineP->debuglevel, &debuglevelSpec, 0); opt.opt_table = option_def; opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ optParseOptions3(&argc, argv, opt, sizeof(opt), 0); if (!debuglevelSpec) cmdlineP->debuglevel = 0; if (argc - 1 == 0) cmdlineP->inputFilename = strdup("-"); /* he wants stdin */ else if (argc - 1 == 1) cmdlineP->inputFilename = strdup(argv[1]); else pm_error("Too many arguments. The only argument accepted\n" "is the input file specificaton");}static voidreadJpc(const char * const inputFilename, jas_image_t ** const jasperPP) { jas_image_t * jasperP; jas_stream_t *instream; const char * options; if ( strcmp(inputFilename, "-") == 0) { /* The input image is to be read from standard input. */ instream = jas_stream_fdopen(fileno(stdin), "rb"); if (instream == NULL) pm_error("error: cannot reopen standard input"); } else { instream = jas_stream_fopen(inputFilename, "rb"); if (instream == NULL ) pm_error("cannot open input image file '%s'", inputFilename); } if (jas_image_getfmt(instream) != jas_image_strtofmt((char*)"jpc")) pm_error("Input is not JPEG-2000 code stream"); options = ""; jasperP = jas_image_decode(instream, jas_image_strtofmt((char*)"jpc"), (char*)options); if (jasperP == NULL) pm_error("Unable to interpret JPEG-2000 input. " "The Jasper library jas_image_decode() subroutine failed."); jas_stream_close(instream); *jasperPP = jasperP;}static voidgetRgbComponents(int jasperCmpnt[], jas_image_t * const jasperP) { { int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); if (rc < 0) pm_error("Input says it has RGB color space, but contains " "no red component"); else jasperCmpnt[PAM_RED_PLANE] = rc; if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the red intensities."); } { int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); if (rc < 0) pm_error("Input says it has RGB color space, but contains " "no green component"); else jasperCmpnt[PAM_GRN_PLANE] = rc; if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the green intensities."); } { int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); if (rc < 0) pm_error("Input says it has RGB color space, but contains " "no blue component"); else jasperCmpnt[PAM_BLU_PLANE] = rc; if (jas_image_cmptsgnd(jasperP, rc)) pm_error("Input image says it is RGB, but has signed values " "for what should be the blue intensities."); }} static voidgetGrayComponent(int * jasperCmptP, jas_image_t * const jasperP) { int const rc = jas_image_getcmptbytype(jasperP, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); if (rc < 0) pm_error("Input says it has Grayscale color space, but contains " "no gray intensity component"); else *jasperCmptP = rc; if (jas_image_cmptsgnd(jasperP, 0)) pm_error("Input image says it is grayscale, but has signed values " "for what should be the gray levels.");}static voidvalidateComponentsAlike(jas_image_t * const jasperP) {/*---------------------------------------------------------------------------- JPC allows each component to have its own width and height. But PAM requires all planes to the same shape. So we validate now that all the channels are the same, and abort the program if not.-----------------------------------------------------------------------------*/ int cmptNo; for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) { if (jas_image_cmptwidth(jasperP, cmptNo) != jas_image_cmptwidth(jasperP, 0)) pm_message("Input image does not have components all the same " "width."); if (jas_image_cmptheight(jasperP, cmptNo) != jas_image_cmptheight(jasperP, 0)) pm_message("Input image does not have components all the same " "height."); }}static unsigned intmaxJasperComponentPrecision(jas_image_t * const jasperP) { int cmptNo; unsigned int max; max = 1; for (cmptNo = 0; cmptNo < jas_image_numcmpts(jasperP); ++cmptNo) max = MAX(max, jas_image_cmptprec(jasperP, cmptNo)); return max;}static voidcomputeOutputParm(jas_image_t * const jasperP, struct pam * const outpamP, int ** const jasperCmptNoP) { int * jasperCmptNo; /* Malloc'ed array. jaspercmptNo[P] is the component number for use with the Jasper library that corresponds to Plane P of the PAM. */ switch (jas_clrspc_fam(jas_image_clrspc(jasperP))) { case JAS_CLRSPC_FAM_GRAY: outpamP->depth = 1; MALLOCARRAY_NOFAIL(jasperCmptNo, 1); getGrayComponent(&jasperCmptNo[0], jasperP); if (jas_image_cmptprec(jasperP, jasperCmptNo[0]) == 1) { outpamP->format = RPBM_FORMAT; strcpy(outpamP->tuple_type, PAM_PBM_TUPLETYPE); } else { outpamP->format = RPGM_FORMAT; strcpy(outpamP->tuple_type, PAM_PGM_TUPLETYPE); } break; case JAS_CLRSPC_FAM_RGB: outpamP->depth = 3; MALLOCARRAY_NOFAIL(jasperCmptNo, 3); getRgbComponents(jasperCmptNo, jasperP); outpamP->format = RPPM_FORMAT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -