/*1* Copyright (c) 1999-2000 Image Power, Inc. and the University of2* British Columbia.3* Copyright (c) 2001-2002 Michael David Adams.4* All rights reserved.5*/67/* __START_OF_JASPER_LICENSE__8*9* JasPer License Version 2.010*11* Copyright (c) 2001-2006 Michael David Adams12* Copyright (c) 1999-2000 Image Power, Inc.13* Copyright (c) 1999-2000 The University of British Columbia14*15* All rights reserved.16*17* Permission is hereby granted, free of charge, to any person (the18* "User") obtaining a copy of this software and associated documentation19* files (the "Software"), to deal in the Software without restriction,20* including without limitation the rights to use, copy, modify, merge,21* publish, distribute, and/or sell copies of the Software, and to permit22* persons to whom the Software is furnished to do so, subject to the23* following conditions:24*25* 1. The above copyright notices and this permission notice (which26* includes the disclaimer below) shall be included in all copies or27* substantial portions of the Software.28*29* 2. The name of a copyright holder shall not be used to endorse or30* promote products derived from the Software without specific prior31* written permission.32*33* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS34* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER35* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS36* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING37* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A38* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO39* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL40* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING41* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,42* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION43* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE44* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE45* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.46* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS47* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL48* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS49* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE50* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE51* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL52* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,53* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL54* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH55* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,56* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH57* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY58* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.59*60* __END_OF_JASPER_LICENSE__61*/6263/*64* JPEG-2000 Code Stream Library65*66* $Id: jpc_cs.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $67*/6869#ifndef JPC_CS_H70#define JPC_CS_H7172/******************************************************************************\73* Includes.74\******************************************************************************/7576#include "jasper/jas_image.h"77#include "jasper/jas_stream.h"7879#include "jpc_cod.h"8081/******************************************************************************\82* Constants and Types.83\******************************************************************************/8485/* The maximum number of resolution levels. */86#define JPC_MAXRLVLS 338788/* The maximum number of bands. */89#define JPC_MAXBANDS (3 * JPC_MAXRLVLS + 1)9091/* The maximum number of layers. */92#define JPC_MAXLYRS 163849394/**************************************\95* Code stream.96\**************************************/9798/*99* Code stream states.100*/101102/* Initial. */103#define JPC_CS_INIT 0104/* Main header. */105#define JPC_CS_MHDR 1106/* Tile-part header. */107#define JPC_CS_THDR 2108/* Main trailer. */109#define JPC_CS_MTLR 3110/* Tile-part data. */111#define JPC_CS_TDATA 4112113/*114* Unfortunately, the code stream syntax was not designed in such a way that115* any given marker segment can be correctly decoded without additional state116* derived from previously decoded marker segments.117* For example, a RGN/COC/QCC marker segment cannot be decoded unless the118* number of components is known.119*/120121/*122* Code stream state information.123*/124125typedef struct {126127/* The number of components. */128uint_fast16_t numcomps;129130} jpc_cstate_t;131132/**************************************\133* SOT marker segment parameters.134\**************************************/135136typedef struct {137138/* The tile number. */139uint_fast16_t tileno;140141/* The combined length of the marker segment and its auxilary data142(i.e., packet data). */143uint_fast32_t len;144145/* The tile-part instance. */146uint_fast8_t partno;147148/* The number of tile-parts. */149uint_fast8_t numparts;150151} jpc_sot_t;152153/**************************************\154* SIZ marker segment parameters.155\**************************************/156157/* Per component information. */158159typedef struct {160161/* The precision of the samples. */162uint_fast8_t prec;163164/* The signedness of the samples. */165uint_fast8_t sgnd;166167/* The horizontal separation of samples with respect to the reference168grid. */169uint_fast8_t hsamp;170171/* The vertical separation of samples with respect to the reference172grid. */173uint_fast8_t vsamp;174175} jpc_sizcomp_t;176177/* SIZ marker segment parameters. */178179typedef struct {180181/* The code stream capabilities. */182uint_fast16_t caps;183184/* The width of the image in units of the reference grid. */185uint_fast32_t width;186187/* The height of the image in units of the reference grid. */188uint_fast32_t height;189190/* The horizontal offset from the origin of the reference grid to the191left side of the image area. */192uint_fast32_t xoff;193194/* The vertical offset from the origin of the reference grid to the195top side of the image area. */196uint_fast32_t yoff;197198/* The nominal width of a tile in units of the reference grid. */199uint_fast32_t tilewidth;200201/* The nominal height of a tile in units of the reference grid. */202uint_fast32_t tileheight;203204/* The horizontal offset from the origin of the reference grid to the205left side of the first tile. */206uint_fast32_t tilexoff;207208/* The vertical offset from the origin of the reference grid to the209top side of the first tile. */210uint_fast32_t tileyoff;211212/* The number of components. */213uint_fast16_t numcomps;214215/* The per-component information. */216jpc_sizcomp_t *comps;217218} jpc_siz_t;219220/**************************************\221* COD marker segment parameters.222\**************************************/223224/*225* Coding style constants.226*/227228/* Precincts may be used. */229#define JPC_COX_PRT 0x01230/* SOP marker segments may be used. */231#define JPC_COD_SOP 0x02232/* EPH marker segments may be used. */233#define JPC_COD_EPH 0x04234235/*236* Progression order constants.237*/238239/* Layer-resolution-component-precinct progressive240(i.e., progressive by fidelity). */241#define JPC_COD_LRCPPRG 0242/* Resolution-layer-component-precinct progressive243(i.e., progressive by resolution). */244#define JPC_COD_RLCPPRG 1245/* Resolution-precinct-component-layer progressive. */246#define JPC_COD_RPCLPRG 2247/* Precinct-component-resolution-layer progressive. */248#define JPC_COD_PCRLPRG 3249/* Component-position-resolution-layer progressive. */250#define JPC_COD_CPRLPRG 4251252/*253* Code block style constants.254*/255256#define JPC_COX_LAZY 0x01 /* Selective arithmetic coding bypass. */257#define JPC_COX_RESET 0x02 /* Reset context probabilities. */258#define JPC_COX_TERMALL 0x04 /* Terminate all coding passes. */259#define JPC_COX_VSC 0x08 /* Vertical stripe causal context formation. */260#define JPC_COX_PTERM 0x10 /* Predictable termination. */261#define JPC_COX_SEGSYM 0x20 /* Use segmentation symbols. */262263/* Transform constants. */264#define JPC_COX_INS 0x00 /* Irreversible 9/7. */265#define JPC_COX_RFT 0x01 /* Reversible 5/3. */266267/* Multicomponent transform constants. */268#define JPC_COD_NOMCT 0x00 /* No multicomponent transform. */269#define JPC_COD_MCT 0x01 /* Multicomponent transform. */270271/* Get the code block size value from the code block size exponent. */272#define JPC_COX_CBLKSIZEEXPN(x) ((x) - 2)273/* Get the code block size exponent from the code block size value. */274#define JPC_COX_GETCBLKSIZEEXPN(x) ((x) + 2)275276/* Per resolution-level information. */277278typedef struct {279280/* The packet partition width. */281uint_fast8_t parwidthval;282283/* The packet partition height. */284uint_fast8_t parheightval;285286} jpc_coxrlvl_t;287288/* Per component information. */289290typedef struct {291292/* The coding style. */293uint_fast8_t csty;294295/* The number of decomposition levels. */296uint_fast8_t numdlvls;297298/* The nominal code block width specifier. */299uint_fast8_t cblkwidthval;300301/* The nominal code block height specifier. */302uint_fast8_t cblkheightval;303304/* The style of coding passes. */305uint_fast8_t cblksty;306307/* The QMFB employed. */308uint_fast8_t qmfbid;309310/* The number of resolution levels. */311int numrlvls;312313/* The per-resolution-level information. */314jpc_coxrlvl_t rlvls[JPC_MAXRLVLS];315316} jpc_coxcp_t;317318/* COD marker segment parameters. */319320typedef struct {321322/* The general coding style. */323uint_fast8_t csty;324325/* The progression order. */326uint_fast8_t prg;327328/* The number of layers. */329uint_fast16_t numlyrs;330331/* The multicomponent transform. */332uint_fast8_t mctrans;333334/* Component-related parameters. */335jpc_coxcp_t compparms;336337} jpc_cod_t;338339/* COC marker segment parameters. */340341typedef struct {342343/* The component number. */344uint_fast16_t compno;345346/* Component-related parameters. */347jpc_coxcp_t compparms;348349} jpc_coc_t;350351/**************************************\352* RGN marker segment parameters.353\**************************************/354355/* The maxshift ROI style. */356#define JPC_RGN_MAXSHIFT 0x00357358typedef struct {359360/* The component to which the marker applies. */361uint_fast16_t compno;362363/* The ROI style. */364uint_fast8_t roisty;365366/* The ROI shift value. */367uint_fast8_t roishift;368369} jpc_rgn_t;370371/**************************************\372* QCD/QCC marker segment parameters.373\**************************************/374375/*376* Quantization style constants.377*/378379#define JPC_QCX_NOQNT 0 /* No quantization. */380#define JPC_QCX_SIQNT 1 /* Scalar quantization, implicit. */381#define JPC_QCX_SEQNT 2 /* Scalar quantization, explicit. */382383/*384* Stepsize manipulation macros.385*/386387#define JPC_QCX_GETEXPN(x) ((x) >> 11)388#define JPC_QCX_GETMANT(x) ((x) & 0x07ff)389#define JPC_QCX_EXPN(x) (assert(!((x) & (~0x1f))), (((x) & 0x1f) << 11))390#define JPC_QCX_MANT(x) (assert(!((x) & (~0x7ff))), ((x) & 0x7ff))391392/* Per component information. */393394typedef struct {395396/* The quantization style. */397uint_fast8_t qntsty;398399/* The number of step sizes. */400int numstepsizes;401402/* The step sizes. */403uint_fast16_t *stepsizes;404405/* The number of guard bits. */406uint_fast8_t numguard;407408} jpc_qcxcp_t;409410/* QCC marker segment parameters. */411412typedef struct {413414/* The component associated with this marker segment. */415uint_fast16_t compno;416417/* The parameters. */418jpc_qcxcp_t compparms;419420} jpc_qcc_t;421422/* QCD marker segment parameters. */423424typedef struct {425426/* The parameters. */427jpc_qcxcp_t compparms;428429} jpc_qcd_t;430431/**************************************\432* POD marker segment parameters.433\**************************************/434435typedef struct {436437/* The progression order. */438uint_fast8_t prgord;439440/* The lower bound (inclusive) on the resolution level for the441progression order volume. */442uint_fast8_t rlvlnostart;443444/* The upper bound (exclusive) on the resolution level for the445progression order volume. */446uint_fast8_t rlvlnoend;447448/* The lower bound (inclusive) on the component for the progression449order volume. */450uint_fast16_t compnostart;451452/* The upper bound (exclusive) on the component for the progression453order volume. */454uint_fast16_t compnoend;455456/* The upper bound (exclusive) on the layer for the progression457order volume. */458uint_fast16_t lyrnoend;459460} jpc_pocpchg_t;461462/* An alias for the above type. */463typedef jpc_pocpchg_t jpc_pchg_t;464465/* POC marker segment parameters. */466467typedef struct {468469/* The number of progression order changes. */470int numpchgs;471472/* The per-progression-order-change information. */473jpc_pocpchg_t *pchgs;474475} jpc_poc_t;476477/**************************************\478* PPM/PPT marker segment parameters.479\**************************************/480481/* PPM marker segment parameters. */482483typedef struct {484485/* The index. */486uint_fast8_t ind;487488/* The length. */489uint_fast16_t len;490491/* The data. */492uchar *data;493494} jpc_ppm_t;495496/* PPT marker segment parameters. */497498typedef struct {499500/* The index. */501uint_fast8_t ind;502503/* The length. */504uint_fast32_t len;505506/* The data. */507unsigned char *data;508509} jpc_ppt_t;510511/**************************************\512* COM marker segment parameters.513\**************************************/514515/*516* Registration IDs.517*/518519#define JPC_COM_BIN 0x00520#define JPC_COM_LATIN 0x01521522typedef struct {523524/* The registration ID. */525uint_fast16_t regid;526527/* The length of the data in bytes. */528uint_fast16_t len;529530/* The data. */531uchar *data;532533} jpc_com_t;534535/**************************************\536* SOP marker segment parameters.537\**************************************/538539typedef struct {540541/* The sequence number. */542uint_fast16_t seqno;543544} jpc_sop_t;545546/**************************************\547* CRG marker segment parameters.548\**************************************/549550/* Per component information. */551552typedef struct {553554/* The horizontal offset. */555uint_fast16_t hoff;556557/* The vertical offset. */558uint_fast16_t voff;559560} jpc_crgcomp_t;561562typedef struct {563564/* The number of components. */565int numcomps;566567/* Per component information. */568jpc_crgcomp_t *comps;569570} jpc_crg_t;571572/**************************************\573* Marker segment parameters for unknown marker type.574\**************************************/575576typedef struct {577578/* The data. */579uchar *data;580581/* The length. */582uint_fast16_t len;583584} jpc_unk_t;585586/**************************************\587* Generic marker segment parameters.588\**************************************/589590typedef union {591int soc; /* unused */592jpc_sot_t sot;593int sod; /* unused */594int eoc; /* unused */595jpc_siz_t siz;596jpc_cod_t cod;597jpc_coc_t coc;598jpc_rgn_t rgn;599jpc_qcd_t qcd;600jpc_qcc_t qcc;601jpc_poc_t poc;602/* jpc_plm_t plm; */603/* jpc_plt_t plt; */604jpc_ppm_t ppm;605jpc_ppt_t ppt;606jpc_sop_t sop;607int eph; /* unused */608jpc_com_t com;609jpc_crg_t crg;610jpc_unk_t unk;611} jpc_msparms_t;612613/**************************************\614* Marker segment.615\**************************************/616617/* Marker segment IDs. */618619/* The smallest valid marker value. */620#define JPC_MS_MIN 0xff00621622/* The largest valid marker value. */623#define JPC_MS_MAX 0xffff624625/* The minimum marker value that cannot occur within packet data. */626#define JPC_MS_INMIN 0xff80627/* The maximum marker value that cannot occur within packet data. */628#define JPC_MS_INMAX 0xffff629630/* Delimiting marker segments. */631#define JPC_MS_SOC 0xff4f /* Start of code stream (SOC). */632#define JPC_MS_SOT 0xff90 /* Start of tile-part (SOT). */633#define JPC_MS_SOD 0xff93 /* Start of data (SOD). */634#define JPC_MS_EOC 0xffd9 /* End of code stream (EOC). */635636/* Fixed information marker segments. */637#define JPC_MS_SIZ 0xff51 /* Image and tile size (SIZ). */638639/* Functional marker segments. */640#define JPC_MS_COD 0xff52 /* Coding style default (COD). */641#define JPC_MS_COC 0xff53 /* Coding style component (COC). */642#define JPC_MS_RGN 0xff5e /* Region of interest (RGN). */643#define JPC_MS_QCD 0xff5c /* Quantization default (QCD). */644#define JPC_MS_QCC 0xff5d /* Quantization component (QCC). */645#define JPC_MS_POC 0xff5f /* Progression order default (POC). */646647/* Pointer marker segments. */648#define JPC_MS_TLM 0xff55 /* Tile-part lengths, main header (TLM). */649#define JPC_MS_PLM 0xff57 /* Packet length, main header (PLM). */650#define JPC_MS_PLT 0xff58 /* Packet length, tile-part header (PLT). */651#define JPC_MS_PPM 0xff60 /* Packed packet headers, main header (PPM). */652#define JPC_MS_PPT 0xff61 /* Packet packet headers, tile-part header (PPT). */653654/* In bit stream marker segments. */655#define JPC_MS_SOP 0xff91 /* Start of packet (SOP). */656#define JPC_MS_EPH 0xff92 /* End of packet header (EPH). */657658/* Informational marker segments. */659#define JPC_MS_CRG 0xff63 /* Component registration (CRG). */660#define JPC_MS_COM 0xff64 /* Comment (COM). */661662/* Forward declaration. */663struct jpc_msops_s;664665/* Generic marker segment class. */666667typedef struct {668669/* The type of marker segment. */670uint_fast16_t id;671672/* The length of the marker segment. */673uint_fast16_t len;674675/* The starting offset within the stream. */676uint_fast32_t off;677678/* The parameters of the marker segment. */679jpc_msparms_t parms;680681/* The marker segment operations. */682struct jpc_msops_s *ops;683684} jpc_ms_t;685686/* Marker segment operations (which depend on the marker segment type). */687688typedef struct jpc_msops_s {689690/* Destroy the marker segment parameters. */691void (*destroyparms)(jpc_ms_t *ms);692693/* Get the marker segment parameters from a stream. */694int (*getparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in);695696/* Put the marker segment parameters to a stream. */697int (*putparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out);698699/* Dump the marker segment parameters (for debugging). */700int (*dumpparms)(jpc_ms_t *ms, FILE *out);701702} jpc_msops_t;703704/******************************************************************************\705* Macros/Functions.706\******************************************************************************/707708/* Create a code-stream state object. */709jpc_cstate_t *jpc_cstate_create(void);710711/* Destroy a code-stream state object. */712void jpc_cstate_destroy(jpc_cstate_t *cstate);713714/* Create a marker segment. */715jpc_ms_t *jpc_ms_create(int type);716717/* Destroy a marker segment. */718void jpc_ms_destroy(jpc_ms_t *ms);719720/* Does a marker segment have parameters? */721#define JPC_MS_HASPARMS(x) \722(!((x) == JPC_MS_SOC || (x) == JPC_MS_SOD || (x) == JPC_MS_EOC || \723(x) == JPC_MS_EPH || ((x) >= 0xff30 && (x) <= 0xff3f)))724725/* Get the marker segment type. */726#define jpc_ms_gettype(ms) \727((ms)->id)728729/* Read a marker segment from a stream. */730jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate);731732/* Write a marker segment to a stream. */733int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms);734735/* Copy code stream data from one stream to another. */736int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long n);737738/* Copy code stream data from one stream to another. */739int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long n);740741/* Dump a marker segment (for debugging). */742void jpc_ms_dump(jpc_ms_t *ms, FILE *out);743744/* Read a 8-bit unsigned integer from a stream. */745int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val);746747/* Read a 16-bit unsigned integer from a stream. */748int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val);749750/* Read a 32-bit unsigned integer from a stream. */751int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val);752753/* Write a 8-bit unsigned integer to a stream. */754int jpc_putuint8(jas_stream_t *out, uint_fast8_t val);755756/* Write a 16-bit unsigned integer to a stream. */757int jpc_putuint16(jas_stream_t *out, uint_fast16_t val);758759/* Write a 32-bit unsigned integer to a stream. */760int jpc_putuint32(jas_stream_t *out, uint_fast32_t val);761762#endif763764765