📄 jpc_dec.c
字号:
/* * Copyright (c) 1999-2000 Image Power, Inc. and the University of * British Columbia. * Copyright (c) 2001-2002 Michael David Adams. * All rights reserved. *//* __START_OF_JASPER_LICENSE__ * * JasPer Software License * * IMAGE POWER JPEG-2000 PUBLIC LICENSE * ************************************ * * GRANT: * * Permission is hereby granted, free of charge, to any person (the "User") * obtaining a copy of this software and associated documentation, to deal * in the JasPer Software without restriction, including without limitation * the right to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the JasPer Software (in source and binary forms), * and to permit persons to whom the JasPer Software is furnished to do so, * provided further that the License Conditions below are met. * * License Conditions * ****************** * * A. Redistributions of source code must retain the above copyright notice, * and this list of conditions, and the following disclaimer. * * B. Redistributions in binary form must reproduce the above copyright * notice, and this list of conditions, and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * C. Neither the name of Image Power, Inc. nor any other contributor * (including, but not limited to, the University of British Columbia and * Michael David Adams) may be used to endorse or promote products derived * from this software without specific prior written permission. * * D. User agrees that it shall not commence any action against Image Power, * Inc., the University of British Columbia, Michael David Adams, or any * other contributors (collectively "Licensors") for infringement of any * intellectual property rights ("IPR") held by the User in respect of any * technology that User owns or has a right to license or sublicense and * which is an element required in order to claim compliance with ISO/IEC * 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property * rights worldwide arising under statutory or common law, and whether * or not perfected, including, without limitation, all (i) patents and * patent applications owned or licensable by User; (ii) rights associated * with works of authorship including copyrights, copyright applications, * copyright registrations, mask work rights, mask work applications, * mask work registrations; (iii) rights relating to the protection of * trade secrets and confidential information; (iv) any right analogous * to those set forth in subsections (i), (ii), or (iii) and any other * proprietary rights relating to intangible property (other than trademark, * trade dress, or service mark rights); and (v) divisions, continuations, * renewals, reissues and extensions of the foregoing (as and to the extent * applicable) now existing, hereafter filed, issued or acquired. * * E. If User commences an infringement action against any Licensor(s) then * such Licensor(s) shall have the right to terminate User's license and * all sublicenses that have been granted hereunder by User to other parties. * * F. This software is for use only in hardware or software products that * are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license * or right to this Software is granted for products that do not comply * with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased * from the ISO. * * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. * NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER * THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND * CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, * WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, * IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING * TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE * OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING * PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. * THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE * IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE * IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY * OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY * OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, * REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, * WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE * INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., * MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE * JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO * THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR * CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, * DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR * MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF * SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY * OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT * FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR * RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING * FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT * LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE * JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY * TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE * ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS * OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT * KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING * TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS * CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE * NOTICE SPECIFIED IN THIS SECTION. * * __END_OF_JASPER_LICENSE__ *//* * $Id$ *//******************************************************************************\* Includes.\******************************************************************************/#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "jasper/jas_types.h"#include "jasper/jas_math.h"#include "jasper/jas_tvp.h"#include "jasper/jas_malloc.h"#include "jasper/jas_debug.h"#include "jpc_fix.h"#include "jpc_dec.h"#include "jpc_cs.h"#include "jpc_mct.h"#include "jpc_t2dec.h"#include "jpc_t1dec.h"#include "jpc_math.h"/******************************************************************************\*\******************************************************************************/#define JPC_MHSOC 0x0001 /* In the main header, expecting a SOC marker segment. */#define JPC_MHSIZ 0x0002 /* In the main header, expecting a SIZ marker segment. */#define JPC_MH 0x0004 /* In the main header, expecting "other" marker segments. */#define JPC_TPHSOT 0x0008 /* In a tile-part header, expecting a SOT marker segment. */#define JPC_TPH 0x0010 /* In a tile-part header, expecting "other" marker segments. */#define JPC_MT 0x0020 /* In the main trailer. */typedef struct { uint_fast16_t id; /* The marker segment type. */ int validstates; /* The states in which this type of marker segment can be validly encountered. */ int (*action)(jpc_dec_t *dec, jpc_ms_t *ms); /* The action to take upon encountering this type of marker segment. */} jpc_dec_mstabent_t;/******************************************************************************\*\******************************************************************************//* COD/COC parameters have been specified. */#define JPC_CSET 0x0001/* QCD/QCC parameters have been specified. */#define JPC_QSET 0x0002/* COD/COC parameters set from a COC marker segment. */#define JPC_COC 0x0004/* QCD/QCC parameters set from a QCC marker segment. */#define JPC_QCC 0x0008/******************************************************************************\* Local function prototypes.\******************************************************************************/static int jpc_dec_dump(jpc_dec_t *dec, FILE *out);jpc_ppxstab_t *jpc_ppxstab_create(void);void jpc_ppxstab_destroy(jpc_ppxstab_t *tab);int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents);int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent);jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab);int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab);jpc_ppxstabent_t *jpc_ppxstabent_create(void);void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent);int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist);jpc_streamlist_t *jpc_streamlist_create(void);int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, jas_stream_t *stream);jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno);void jpc_streamlist_destroy(jpc_streamlist_t *streamlist);jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno);static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp);static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps);static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp);static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp);static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod);static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc);static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, jpc_coxcp_t *compparms, int flags);static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd);static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc);static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, jpc_qcxcp_t *compparms, int flags);static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn);static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp);static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp);static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset);static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc);static int jpc_dec_decode(jpc_dec_t *dec);static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in);static void jpc_dec_destroy(jpc_dec_t *dec);static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize);static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps);static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits);static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile);static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile);static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile);static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms);static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts);/******************************************************************************\* Global data.\******************************************************************************/jpc_dec_mstabent_t jpc_dec_mstab[] = { {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc}, {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot}, {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod}, {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc}, {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz}, {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod}, {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc}, {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn}, {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd}, {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc}, {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc}, {JPC_MS_TLM, JPC_MH, 0}, {JPC_MS_PLM, JPC_MH, 0}, {JPC_MS_PLT, JPC_TPH, 0}, {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm}, {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt}, {JPC_MS_SOP, 0, 0}, {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg}, {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com}, {0, JPC_MH | JPC_TPH, jpc_dec_process_unk}};/******************************************************************************\* The main entry point for the JPEG-2000 decoder.\******************************************************************************/jas_image_t *jpc_decode(jas_stream_t *in, char *optstr){ jpc_dec_importopts_t opts; jpc_dec_t *dec; jas_image_t *image; dec = 0; if (jpc_dec_parseopts(optstr, &opts)) { goto error; } jpc_initluts(); if (!(dec = jpc_dec_create(&opts, in))) { goto error; } /* Do most of the work. */ if (jpc_dec_decode(dec)) { goto error; } if (jas_image_numcmpts(dec->image) >= 3) { jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_RGB); jas_image_setcmpttype(dec->image, 0, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); jas_image_setcmpttype(dec->image, 1, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); jas_image_setcmpttype(dec->image, 2, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); } else { jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_GRAY); jas_image_setcmpttype(dec->image, 0, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); } /* Save the return value. */ image = dec->image; /* Stop the image from being discarded. */ dec->image = 0; /* Destroy decoder. */ jpc_dec_destroy(dec); return image;error: if (dec) { jpc_dec_destroy(dec); } return 0;}typedef enum { OPT_MAXLYRS, OPT_MAXPKTS, OPT_DEBUG} optid_t;jas_taginfo_t decopts[] = { {OPT_MAXLYRS, "maxlyrs"}, {OPT_MAXPKTS, "maxpkts"}, {OPT_DEBUG, "debug"}, {-1, 0}};static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts){ jas_tvparser_t *tvp; opts->debug = 0; opts->maxlyrs = JPC_MAXLYRS; opts->maxpkts = -1; if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { return -1; } while (!jas_tvparser_next(tvp)) { switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts, jas_tvparser_gettag(tvp)))->id) { case OPT_MAXLYRS: opts->maxlyrs = atoi(jas_tvparser_getval(tvp)); break; case OPT_DEBUG: opts->debug = atoi(jas_tvparser_getval(tvp)); break; case OPT_MAXPKTS: opts->maxpkts = atoi(jas_tvparser_getval(tvp)); break; default: fprintf(stderr, "warning: ignoring invalid option %s\n", jas_tvparser_gettag(tvp)); break; } } jas_tvparser_destroy(tvp); return 0;}/******************************************************************************\* Code for table-driven code stream decoder.\******************************************************************************/static jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id){ jpc_dec_mstabent_t *mstabent; for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) { if (mstabent->id == id) { break; } } return mstabent;}static int jpc_dec_decode(jpc_dec_t *dec){ jpc_ms_t *ms; jpc_dec_mstabent_t *mstabent; int ret; jpc_cstate_t *cstate; if (!(cstate = jpc_cstate_create())) { return -1; } dec->cstate = cstate; /* Initially, we should expect to encounter a SOC marker segment. */ dec->state = JPC_MHSOC; for (;;) { /* Get the next marker segment in the code stream. */ if (!(ms = jpc_getms(dec->in, cstate))) { fprintf(stderr, "cannot get marker segment\n"); return -1; } mstabent = jpc_dec_mstab_lookup(ms->id); assert(mstabent); /* Ensure that this type of marker segment is permitted at this point in the code stream. */ if (!(dec->state & mstabent->validstates)) { fprintf(stderr, "unexpected marker segment type\n"); jpc_ms_destroy(ms); return -1; } /* Process the marker segment. */ if (mstabent->action) { ret = (*mstabent->action)(dec, ms); } else { /* No explicit action is required. */ ret = 0; } /* Destroy the marker segment. */ jpc_ms_destroy(ms); if (ret < 0) { return -1; } else if (ret > 0) { break; } } return 0;}static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms){ uint_fast16_t cmptno; jpc_dec_cmpt_t *cmpt; jpc_crg_t *crg; crg = &ms->parms.crg; for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno, ++cmpt) { /* Ignore the information in the CRG marker segment for now. This information serves no useful purpose for decoding anyhow.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -