⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpc_cs.c

📁 用C语言实现的JPEG编码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * 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__ *//* * JPEG-2000 Code Stream Library * * $Id$ *//******************************************************************************\* Includes.\******************************************************************************/#include <stdlib.h>#include <assert.h>#include <ctype.h>#include "jasper/jas_malloc.h"#include "jasper/jas_debug.h"#include "jpc_cs.h"/******************************************************************************\* Types.\******************************************************************************//* Marker segment table entry. */typedef struct {	int id;	char *name;	jpc_msops_t ops;} jpc_mstabent_t;/******************************************************************************\* Local prototypes.\******************************************************************************/static jpc_mstabent_t *jpc_mstab_lookup(int id);static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static void jpc_poc_destroyparms(jpc_ms_t *ms);static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out);static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out);static void jpc_siz_destroyparms(jpc_ms_t *ms);static void jpc_qcd_destroyparms(jpc_ms_t *ms);static void jpc_qcc_destroyparms(jpc_ms_t *ms);static void jpc_cod_destroyparms(jpc_ms_t *ms);static void jpc_coc_destroyparms(jpc_ms_t *ms);static void jpc_unk_destroyparms(jpc_ms_t *ms);static void jpc_ppm_destroyparms(jpc_ms_t *ms);static void jpc_ppt_destroyparms(jpc_ms_t *ms);static void jpc_crg_destroyparms(jpc_ms_t *ms);static void jpc_com_destroyparms(jpc_ms_t *ms);static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms);static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,  jas_stream_t *in, uint_fast16_t len);static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate,  jas_stream_t *out);static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms);static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,  jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms);static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate,  jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms);/******************************************************************************\* Global data.\******************************************************************************/static jpc_mstabent_t jpc_mstab[] = {	{JPC_MS_SOC, "SOC", {0, 0, 0, 0}},	{JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms,	  jpc_sot_dumpparms}},	{JPC_MS_SOD, "SOD", {0, 0, 0, 0}},	{JPC_MS_EOC, "EOC", {0, 0, 0, 0}},	{JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms,	  jpc_siz_putparms, jpc_siz_dumpparms}},	{JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms,	  jpc_cod_putparms, jpc_cod_dumpparms}},	{JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms,	  jpc_coc_putparms, jpc_coc_dumpparms}},	{JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms,	  jpc_rgn_dumpparms}},	{JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms,	  jpc_qcd_putparms, jpc_qcd_dumpparms}},	{JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms,	  jpc_qcc_putparms, jpc_qcc_dumpparms}},	{JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms,	  jpc_poc_putparms, jpc_poc_dumpparms}},	{JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},	{JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}},	{JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms,	  jpc_ppm_putparms, jpc_ppm_dumpparms}},	{JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms,	  jpc_ppt_putparms, jpc_ppt_dumpparms}},	{JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms,	  jpc_sop_dumpparms}},	{JPC_MS_EPH, "EPH", {0, 0, 0, 0}},	{JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms,	  jpc_crg_dumpparms}},	{JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms,	  jpc_com_putparms, jpc_com_dumpparms}},	{-1, "UNKNOWN",  {jpc_unk_destroyparms, jpc_unk_getparms,	  jpc_unk_putparms, jpc_unk_dumpparms}}};/******************************************************************************\* Code stream manipulation functions.\******************************************************************************//* Create a code stream state object. */jpc_cstate_t *jpc_cstate_create(){	jpc_cstate_t *cstate;	if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) {		return 0;	}	cstate->numcomps = 0;	return cstate;}/* Destroy a code stream state object. */void jpc_cstate_destroy(jpc_cstate_t *cstate){	jas_free(cstate);}/* Read a marker segment from a stream. */jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate){	jpc_ms_t *ms;	jpc_mstabent_t *mstabent;	jas_stream_t *tmpstream;	if (!(ms = jpc_ms_create(0))) {		return 0;	}	/* Get the marker type. */	if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN ||	  ms->id > JPC_MS_MAX) {		jpc_ms_destroy(ms);		return 0;	}	mstabent = jpc_mstab_lookup(ms->id);	ms->ops = &mstabent->ops;	/* Get the marker segment length and parameters if present. */	/* Note: It is tacitly assumed that a marker segment cannot have	  parameters unless it has a length field.  That is, there cannot	  be a parameters field without a length field and vice versa. */	if (JPC_MS_HASPARMS(ms->id)) {		/* Get the length of the marker segment. */		if (jpc_getuint16(in, &ms->len) || ms->len < 3) {			jpc_ms_destroy(ms);			return 0;		}		/* Calculate the length of the marker segment parameters. */		ms->len -= 2;		/* Create and prepare a temporary memory stream from which to		  read the marker segment parameters. */		/* Note: This approach provides a simple way of ensuring that		  we never read beyond the end of the marker segment (even if		  the marker segment length is errantly set too small). */		if (!(tmpstream = jas_stream_memopen(0, 0))) {			jpc_ms_destroy(ms);			return 0;		}		if (jas_stream_copy(tmpstream, in, ms->len) ||		  jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) {			jas_stream_close(tmpstream);			jpc_ms_destroy(ms);			return 0;		}		/* Get the marker segment parameters. */		if ((*ms->ops->getparms)(ms, cstate, tmpstream)) {			ms->ops = 0;			jpc_ms_destroy(ms);			jas_stream_close(tmpstream);			return 0;		}		if (jas_getdbglevel() > 0) {			jpc_ms_dump(ms, stderr);		}		if (JAS_CAST(ulong, jas_stream_tell(tmpstream)) != ms->len) {			fprintf(stderr,			  "warning: trailing garbage in marker segment (%ld bytes)\n",			  ms->len - jas_stream_tell(tmpstream));		}		/* Close the temporary stream. */		jas_stream_close(tmpstream);	} else {		/* There are no marker segment parameters. */		ms->len = 0;		if (jas_getdbglevel() > 0) {			jpc_ms_dump(ms, stderr);		}	}	/* Update the code stream state information based on the type of	  marker segment read. */	/* Note: This is a bit of a hack, but I'm not going to define another	  type of virtual function for this one special case. */	if (ms->id == JPC_MS_SIZ) {		cstate->numcomps = ms->parms.siz.numcomps;	}	return ms;}/* Write a marker segment to a stream. */int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms){	jas_stream_t *tmpstream;	int len;	/* Output the marker segment type. */	if (jpc_putuint16(out, ms->id)) {		return -1;	}	/* Output the marker segment length and parameters if necessary. */	if (ms->ops->putparms) {		/* Create a temporary stream in which to buffer the		  parameter data. */		if (!(tmpstream = jas_stream_memopen(0, 0))) {			return -1;		}		if ((*ms->ops->putparms)(ms, cstate, tmpstream)) {			jas_stream_close(tmpstream);			return -1;		}		/* Get the number of bytes of parameter data written. */		if ((len = jas_stream_tell(tmpstream)) < 0) {			jas_stream_close(tmpstream);			return -1;		}		ms->len = len;		/* Write the marker segment length and parameter data to		  the output stream. */		if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 ||		  jpc_putuint16(out, ms->len + 2) ||		  jas_stream_copy(out, tmpstream, ms->len) < 0) {			jas_stream_close(tmpstream);			return -1;		}		/* Close the temporary stream. */		jas_stream_close(tmpstream);	}	/* This is a bit of a hack, but I'm not going to define another	  type of virtual function for this one special case. */	if (ms->id == JPC_MS_SIZ) {		cstate->numcomps = ms->parms.siz.numcomps;	}	if (jas_getdbglevel() > 0) {		jpc_ms_dump(ms, stderr);	}	return 0;}/******************************************************************************\* Marker segment operations.\******************************************************************************/

⌨️ 快捷键说明

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