pamtojpeg2k.c
来自「linux下将各类格式图片转换工具」· C语言 代码 · 共 525 行 · 第 1/2 页
C
525 行
/***************************************************************************** pamtojpeg2k****************************************************************************** Convert a PNM image to JPEG-2000 code stream image 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 <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 imgareatlx; unsigned int imgareatly; unsigned int tilegrdtlx; unsigned int tilegrdtly; unsigned int tilewidth; unsigned int tileheight; unsigned int prcwidth; unsigned int prcheight; unsigned int cblkwidth; unsigned int cblkheight; enum compmode compmode; float compressionRatio; char * ilyrrates; enum progression progression; unsigned int numrlvls; unsigned int numgbits; unsigned int nomct; unsigned int sop; unsigned int eph; unsigned int lazy; unsigned int termall; unsigned int segsym; unsigned int vcausal; unsigned int pterm; unsigned int resetprob; 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 imgareatlxSpec, imgareatlySpec; unsigned int tilegrdtlxSpec, tilegrdtlySpec; unsigned int tilewidthSpec, tileheightSpec; unsigned int prcwidthSpec, prcheightSpec; unsigned int cblkwidthSpec, cblkheightSpec; unsigned int modeSpec, compressionSpec, ilyrratesSpec; unsigned int progressionSpec, numrlvlsSpec, numgbitsSpec; unsigned int debuglevelSpec; char * progressionOpt; char * modeOpt; unsigned int option_def_index; option_def_index = 0; /* incremented by OPTENTRY */ OPTENT3(0, "imgareatlx", OPT_UINT, &cmdlineP->imgareatlx, &imgareatlxSpec, 0); OPTENT3(0, "imgareatly", OPT_UINT, &cmdlineP->imgareatly, &imgareatlySpec, 0); OPTENT3(0, "tilegrdtlx", OPT_UINT, &cmdlineP->tilegrdtlx, &tilegrdtlxSpec, 0); OPTENT3(0, "tilegrdtly", OPT_UINT, &cmdlineP->tilegrdtly, &tilegrdtlySpec, 0); OPTENT3(0, "tilewidth", OPT_UINT, &cmdlineP->tilewidth, &tilewidthSpec, 0); OPTENT3(0, "tileheight", OPT_UINT, &cmdlineP->tileheight, &tileheightSpec, 0); OPTENT3(0, "prcwidth", OPT_UINT, &cmdlineP->prcwidth, &prcwidthSpec, 0); OPTENT3(0, "prcheight", OPT_UINT, &cmdlineP->prcheight, &prcheightSpec, 0); OPTENT3(0, "cblkwidth", OPT_UINT, &cmdlineP->cblkwidth, &cblkwidthSpec, 0); OPTENT3(0, "cblkheight", OPT_UINT, &cmdlineP->cblkheight, &cblkheightSpec, 0); OPTENT3(0, "mode", OPT_STRING, &modeOpt, &modeSpec, 0); OPTENT3(0, "compression", OPT_FLOAT, &cmdlineP->compressionRatio, &compressionSpec, 0); OPTENT3(0, "ilyrrates", OPT_STRING, &cmdlineP->ilyrrates, &ilyrratesSpec, 0); OPTENT3(0, "progression", OPT_STRING, &progressionOpt, &progressionSpec, 0); OPTENT3(0, "numrlvls", OPT_UINT, &cmdlineP->numrlvls, &numrlvlsSpec, 0); OPTENT3(0, "numgbits", OPT_UINT, &cmdlineP->numgbits, &numgbitsSpec, 0); OPTENT3(0, "nomct", OPT_FLAG, NULL, &cmdlineP->nomct, 0); OPTENT3(0, "sop", OPT_FLAG, NULL, &cmdlineP->sop, 0); OPTENT3(0, "eph", OPT_FLAG, NULL, &cmdlineP->eph, 0); OPTENT3(0, "lazy", OPT_FLAG, NULL, &cmdlineP->lazy, 0); OPTENT3(0, "termall", OPT_FLAG, NULL, &cmdlineP->termall, 0); OPTENT3(0, "segsym", OPT_FLAG, NULL, &cmdlineP->segsym, 0); OPTENT3(0, "vcausal", OPT_FLAG, NULL, &cmdlineP->vcausal, 0); OPTENT3(0, "pterm", OPT_FLAG, NULL, &cmdlineP->pterm, 0); OPTENT3(0, "resetprob", OPT_FLAG, NULL, &cmdlineP->resetprob, 0); 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 (!imgareatlxSpec) cmdlineP->imgareatlx = 0; if (!imgareatlySpec) cmdlineP->imgareatly = 0; if (!tilegrdtlxSpec) cmdlineP->tilegrdtlx = 0; if (!tilegrdtlySpec) cmdlineP->tilegrdtly = 0; if (!tilewidthSpec) cmdlineP->tilewidth = 0; if (!tileheightSpec) cmdlineP->tileheight = 0; if (!prcwidthSpec) cmdlineP->prcwidth = 32768; if (!prcheightSpec) cmdlineP->prcheight = 32768; if (!cblkwidthSpec) cmdlineP->cblkwidth = 64; if (!cblkheightSpec) cmdlineP->cblkheight = 64; if (modeSpec) { if (strcmp(modeOpt, "integer") == 0 || strcmp(modeOpt, "int") == 0) cmdlineP->compmode = COMPMODE_INTEGER; else if (strcmp(modeOpt, "real") == 0) cmdlineP->compmode = COMPMODE_REAL; else pm_error("Invalid value for 'mode' option: '%s'. " "valid values are 'INTEGER' and 'REAL'", modeOpt); } else cmdlineP->compmode = COMPMODE_INTEGER; if (compressionSpec) { if (cmdlineP->compressionRatio < 1.0) pm_error("Compression ratio less than 1 does not make sense."); } else cmdlineP->compressionRatio = 1.0; if (!ilyrratesSpec) cmdlineP->ilyrrates = (char*) ""; if (progressionSpec) { if (strcmp(progressionOpt, "lrcp") == 0) cmdlineP->progression = PROG_LRCP; if (strcmp(progressionOpt, "rlcp") == 0) cmdlineP->progression = PROG_RLCP; if (strcmp(progressionOpt, "rpcl") == 0) cmdlineP->progression = PROG_RPCL; if (strcmp(progressionOpt, "pcrl") == 0) cmdlineP->progression = PROG_PCRL; if (strcmp(progressionOpt, "cprl") == 0) cmdlineP->progression = PROG_CPRL; else pm_error("Invalid value for -progression: '%s'. " "Valid values are lrcp, rlcp, rpcl, pcrl, and cprl.", progressionOpt); } else cmdlineP->progression = PROG_LRCP; if (!numrlvlsSpec) cmdlineP->numrlvls = 6; if (!numgbitsSpec) cmdlineP->numgbits = 2; 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 voidcreateJasperRaster(struct pam * const inpamP, jas_image_t * const jasperP) { jas_matrix_t ** matrix; /* malloc'ed */ /* matrix[X] is the data for Plane X of the current row */ unsigned int plane; unsigned int row; tuple * tuplerow; bool oddMaxval; sample jasperMaxval; matrix = malloc(inpamP->depth * sizeof(jas_matrix_t *)); if (matrix == NULL) pm_error("Out of memory"); for (plane = 0; plane < inpamP->depth; ++plane) { matrix[plane] = jas_matrix_create(1, inpamP->width); if (matrix[plane] == NULL) pm_error("Unable to create matrix for plane %u. " "jas_matrix_create() failed.", plane); } tuplerow = pnm_allocpamrow(inpamP); jasperMaxval = pm_bitstomaxval(pm_maxvaltobits(inpamP->maxval)); oddMaxval = jasperMaxval != inpamP->maxval; for (row = 0; row < inpamP->height; ++row) { unsigned int col; pnm_readpamrow(inpamP, tuplerow); for (col = 0; col < inpamP->width; ++col) { unsigned int plane; for (plane = 0; plane < inpamP->depth; ++plane) { unsigned int jasperSample; if (oddMaxval)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?