pamtojpeg2k.c

来自「linux下将各类格式图片转换工具」· C语言 代码 · 共 525 行 · 第 1/2 页

C
525
字号
                    jasperSample = tuplerow[col][plane] *                         jasperMaxval / inpamP->maxval;                else                    jasperSample = tuplerow[col][plane];                jas_matrix_set(matrix[plane], 0, col, jasperSample);            }        }        {             unsigned int plane;            for (plane = 0; plane < inpamP->depth; ++plane) {                int rc;                rc = jas_image_writecmpt(jasperP, plane, 0, row,                                          inpamP->width, 1,                                         matrix[plane]);                if (rc != 0)                    pm_error("jas_image_writecmpt() of plane %u failed.",                              plane);            }        }    }    pnm_freepamrow(tuplerow);    for (plane = 0; plane < inpamP->depth; ++plane)        jas_matrix_destroy(matrix[plane]);        free(matrix);}static voidcreateJasperImage(struct pam *   const inpamP,                   jas_image_t ** const jasperPP) {	jas_image_cmptparm_t * cmptparms;    unsigned int plane;    cmptparms = malloc(sizeof(jas_image_cmptparm_t) * inpamP->depth);    if (cmptparms == NULL)        pm_error("Unable to allocate storage for cmptparms");    for (plane = 0; plane < inpamP->depth; ++plane) {        cmptparms[plane].tlx = 0;        cmptparms[plane].tly = 0;        cmptparms[plane].hstep = 1;        cmptparms[plane].vstep = 1;        cmptparms[plane].width = inpamP->width;        cmptparms[plane].height = inpamP->height;        cmptparms[plane].prec = pm_maxvaltobits(inpamP->maxval);        cmptparms[plane].sgnd = 0;    }    *jasperPP =         jas_image_create(inpamP->depth, cmptparms, JAS_CLRSPC_UNKNOWN);    if (*jasperPP == NULL)        pm_error("Unable to create jasper image structure.  "                 "jas_image_create() failed.");    free(cmptparms);}static voidconvertToJasperImage(struct pam *   const inpamP,                     jas_image_t ** const jasperPP) {    jas_image_t * jasperP;    createJasperImage(inpamP, &jasperP);    if (strncmp(inpamP->tuple_type, "RGB", 3) == 0) {        if (inpamP->depth < 3)            pm_error("Input tuple type is RGB*, but depth is only %d.  "                     "It should be at least 3.", inpamP->depth);        else {            jas_image_setclrspc(jasperP, JAS_CLRSPC_GENRGB);            jas_image_setcmpttype(jasperP, 0,                                  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R));            jas_image_setcmpttype(jasperP, 1,                                  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G));            jas_image_setcmpttype(jasperP, 2,                                  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B));        }    } else {        if (strncmp(inpamP->tuple_type, "GRAYSCALE", 9 == 0) ||            strncmp(inpamP->tuple_type, "BLACKANDWHITE", 13) == 0) {            jas_image_setclrspc(jasperP, JAS_CLRSPC_GENGRAY);            jas_image_setcmpttype(jasperP, 0,                                  JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y));        }    }    createJasperRaster(inpamP, jasperP);    *jasperPP = jasperP;}static voidwriteJpc(jas_image_t *      const jasperP,          struct cmdlineInfo const cmdline) {    jas_stream_t * outStreamP;    const char * options;    const char * ilyrratesOpt;    const char * prgValue;    char rateOpt[20+1];/* Note: ilyrrates is a hack because we're too lazy to properly parse   command line options to get the information and then compose   a proper input to Jasper.  So the user can screw things up by    specifying garbage for the -ilyrrates option */    if (strlen(cmdline.ilyrrates) > 0)        asprintfN(&ilyrratesOpt, "ilyrrates=%s", cmdline.ilyrrates);    else        ilyrratesOpt = strdup("");    switch(cmdline.progression) {    case PROG_LRCP: prgValue = "lrcp"; break;    case PROG_RLCP: prgValue = "rlcp"; break;    case PROG_RPCL: prgValue = "rcpc"; break;    case PROG_PCRL: prgValue = "pcrl"; break;    case PROG_CPRL: prgValue = "cprl"; break;    }    /* Note that asprintfN() doesn't understand %f, but sprintf() does */    sprintf(rateOpt, "%1.9f", 1.0/cmdline.compressionRatio);    asprintfN(&options,               "imgareatlx=%u "              "imgareatly=%u "              "tilegrdtlx=%u "              "tilegrdtly=%u "              "tilewidth=%u "              "tileheight=%u "              "prcwidth=%u "              "prcheight=%u "              "cblkwidth=%u "              "cblkheight=%u "              "mode=%s "              "rate=%s "              "%s "              "prg=%s "              "numrlvls=%u "              "numgbits=%u "              "%s %s %s %s %s %s %s %s %s",              cmdline.imgareatlx,              cmdline.imgareatly,              cmdline.tilegrdtlx,              cmdline.tilegrdtlx,              cmdline.tilewidth,              cmdline.tileheight,              cmdline.prcwidth,              cmdline.prcheight,              cmdline.cblkwidth,              cmdline.cblkheight,              cmdline.compmode == COMPMODE_INTEGER ? "int" : "real",              rateOpt,              ilyrratesOpt,              prgValue,              cmdline.numrlvls,              cmdline.numgbits,              cmdline.nomct     ? "nomct"     : "",              cmdline.sop       ? "sop"       : "",              cmdline.eph       ? "eph"       : "",              cmdline.lazy      ? "lazy"      : "",              cmdline.termall   ? "termall"   : "",              cmdline.segsym    ? "segsym"    : "",              cmdline.vcausal   ? "vcausal"   : "",              cmdline.pterm     ? "pterm"     : "",              cmdline.resetprob ? "resetprob" : ""        );    strfree(ilyrratesOpt);    /* Open the output image file (Standard Output) */    outStreamP = jas_stream_fdopen(fileno(stdout), "w+b");    if (outStreamP == NULL)        pm_error("Unable to open output stream.  jas_stream_fdopen() "                 "of stdout failed");    {        int rc;        if (cmdline.verbose)            pm_message("Using Jasper to encode to 'jpc' format with options "                       "'%s'", options);        rc = jas_image_encode(jasperP, outStreamP,                               jas_image_strtofmt((char*)"jpc"),                               (char*)options);        if (rc != 0)            pm_error("jas_image_encode() failed to encode the JPEG 2000 "                     "image.  Rc=%d", rc);    }	jas_stream_flush(outStreamP);    {        int rc;        rc = jas_stream_close(outStreamP);                    if (rc != 0)            pm_error("Failed to close output stream, "                     "jas_stream_close() rc = %d", rc);    }                     	jas_image_clearfmts();    strfree(options);}intmain(int argc, char **argv){    struct cmdlineInfo cmdline;    FILE *ifP;    struct pam inpam;    jas_image_t * jasperP;    pnm_init(&argc, argv);        parseCommandLine(argc, argv, &cmdline);        {         int rc;                rc = jas_init();        if ( rc != 0 )            pm_error("Failed to initialize Jasper library.  "                     "jas_init() returns rc %d", rc );    }        jas_setdbglevel(cmdline.debuglevel);        ifP = pm_openr(cmdline.inputFilename);        pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));        convertToJasperImage(&inpam, &jasperP);        writeJpc(jasperP, cmdline);    	jas_image_destroy(jasperP);    pm_close(ifP);    pm_close(stdout);        exit(0);}

⌨️ 快捷键说明

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