📄 headerencoder.java
字号:
/* * CVS identifier: * * $Id: HeaderEncoder.java,v 1.35 2001/03/02 10:10:30 grosbois Exp $ * * Class: HeaderEncoder * * Description: Write codestream headers. * * * * COPYRIGHT: * * This software module was originally developed by Rapha雔 Grosbois and * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel * Askel鰂 (Ericsson Radio Systems AB); and Bertrand Berthelot, David * Bouchard, F閘ix Henry, Gerard Mozelle and Patrice Onno (Canon Research * Centre France S.A) in the course of development of the JPEG2000 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This * software module is an implementation of a part of the JPEG 2000 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio * Systems AB and Canon Research Centre France S.A (collectively JJ2000 * Partners) agree not to assert against ISO/IEC and users of the JPEG * 2000 Standard (Users) any of their rights under the copyright, not * including other intellectual property rights, for this software module * with respect to the usage by ISO/IEC and Users of this software module * or modifications thereof for use in hardware or software products * claiming conformance to the JPEG 2000 Standard. Those intending to use * this software module in hardware or software products are advised that * their use may infringe existing patents. The original developers of * this software module, JJ2000 Partners and ISO/IEC assume no liability * for use of this software module or modifications thereof. No license * or right to this software module is granted for non JPEG 2000 Standard * conforming products. JJ2000 Partners have full right to use this * software module for his/her own purpose, assign or donate this * software module to any third party and to inhibit third parties from * using this software module for non JPEG 2000 Standard conforming * products. This copyright notice must be included in all copies or * derivative works of this software module. * * Copyright (c) 1999/2000 JJ2000 Partners. * */package jj2000.j2k.codestream.writer;import jj2000.j2k.quantization.quantizer.*;import jj2000.j2k.wavelet.analysis.*;import jj2000.j2k.entropy.encoder.*;import jj2000.j2k.quantization.*;import jj2000.j2k.image.input.*;import jj2000.j2k.roi.encoder.*;import jj2000.j2k.codestream.*;import jj2000.j2k.wavelet.*;import jj2000.j2k.encoder.*;import jj2000.j2k.entropy.*;import jj2000.j2k.image.*;import jj2000.j2k.util.*;import jj2000.j2k.io.*;import jj2000.j2k.*;import java.util.*;import java.io.*;/** * This class writes almost of the markers and marker segments in main header * and in tile-part headers. It is created by the run() method of the Encoder * instance. * * <p>A marker segment includes a marker and eventually marker segment * parameters. It is designed by the three letter code of the marker * associated with the marker segment. JPEG 2000 part I defines 6 types of * markers: <ul> <li> Delimiting : SOC,SOT,SOD,EOC (written in * FileCodestreamWriter).</li> <li> Fixed information: SIZ.</li> <li> * Functional: COD,COC,RGN,QCD,QCC,POC.</li> <li> In bit-stream: SOP,EPH.</li> * <li> Pointer: TLM,PLM,PLT,PPM,PPT.</li> <li> Informational: * CRG,COM.</li></ul> * * <p>Main Header is written when Encoder instance calls encodeMainHeader * whereas tile-part headers are written when the EBCOTRateAllocator instance * calls encodeTilePartHeader. * * @see Encoder * @see Markers * @see EBCOTRateAllocator * */public class HeaderEncoder implements Markers, StdEntropyCoderOptions { /** Nominal range bit of the component defining default values in QCD for * main header */ private int defimgn; /** Nominal range bit of the component defining default values in QCD for * tile headers */ private int deftilenr; /** The number of components in the image */ protected int nComp; /** The ByteArrayOutputStream to store header data. This handler * is kept in order to use methods not accessible from a general * DataOutputStream. For the other methods, it's better to use * variable hbuf. * * @see #hbuf */ protected ByteArrayOutputStream baos; /** The DataOutputStream to store header data. This kind of object * is useful to write short, int, .... It's constructor takes * baos as parameter. * * @see #baos **/ protected DataOutputStream hbuf; /** The image data reader. Source of original data info */ protected ImgData origSrc; /** An array specifying, for each component,if the data was signed or not */ protected boolean isOrigSig[]; /** Reference to the rate allocator */ protected PostCompRateAllocator ralloc; /** Reference to the DWT module */ protected ForwardWT dwt; /** Reference to the tiler module */ protected Tiler tiler; /** Reference to the ROI module */ protected ROIScaler roiSc; /** The encoder specifications */ protected EncoderSpecs encSpec; /** * Initializes the header writer with the references to the coding chain. * * @param origsrc The original image data (before any component mixing, * tiling, etc.) * * @param isorigsig An array specifying for each component if it was * originally signed or not. * * @param dwt The discrete wavelet transform module. * * @param tiler The tiler module. * * @param encSpec The encoder specifications * * @param roiSc The ROI scaler module. * * @param ralloc The post compression rate allocator. * */ public HeaderEncoder(ImgData origsrc, boolean isorigsig[], ForwardWT dwt, Tiler tiler,EncoderSpecs encSpec, ROIScaler roiSc, PostCompRateAllocator ralloc) { if (origsrc.getNumComps() != isorigsig.length) { throw new IllegalArgumentException(); } this.origSrc = origsrc; this.isOrigSig = isorigsig; this.dwt = dwt; this.tiler = tiler; this.encSpec = encSpec; this.roiSc = roiSc; this.ralloc = ralloc; baos = new ByteArrayOutputStream(); hbuf = new DataOutputStream(baos); nComp = origsrc.getNumComps(); } /** * Resets the contents of this HeaderEncoder to its initial state. It * erases all the data in the header buffer and reactualizes the * headerLength field of the bit stream writer. * */ public void reset() { baos.reset(); hbuf = new DataOutputStream(baos); } /** * Returns the byte-buffer used to store the codestream header. * * @return A byte array countaining codestream header * */ protected byte[] getBuffer(){ return baos.toByteArray(); } /** * Returns the length of the header. * * @return The length of the header in bytes * */ public int getLength() { return hbuf.size(); } /** * Writes the header to the specified BinaryDataOutput. * * @param out Where to write the header. * */ public void writeTo(BinaryDataOutput out) throws IOException { int i,len; byte buf[]; buf = getBuffer(); len = getLength(); for (i=0; i<len; i++) { out.writeByte(buf[i]); } } /** * Returns the number of bytes used in the codestream header's * buffer. * * @return Header length in buffer (without any header * overhead) * */ protected int getBufferLength(){ return baos.size(); } /** * Writes the header to the specified OutputStream. * * @param out Where to write the header. * */ public void writeTo(OutputStream out) throws IOException { out.write(getBuffer(),0,getBufferLength()); } /** * Start Of Codestream marker (SOC) signalling the beginning of a * codestream. * */ private void writeSOC() throws IOException { hbuf.writeShort(SOC); } /** * Writes SIZ marker segment of the codestream header. It is a fixed * information marker segment containing informations about image and tile * sizes. It is required in the main header immediately after SOC marker * segment. * */ private void writeSIZ() throws IOException { int tmp; // SIZ marker hbuf.writeShort(SIZ); // Lsiz (Marker length) corresponding to // Lsiz(2 bytes)+Rsiz(2)+Xsiz(4)+Ysiz(4)+XOsiz(4)+YOsiz(4)+ // XTsiz(4)+YTsiz(4)+XTOsiz(4)+YTOsiz(4)+Csiz(2)+ // (Ssiz(1)+XRsiz(1)+YRsiz(1))*nComp // markSegLen = 38 + 3*nComp; int markSegLen = 38 + 3*nComp; hbuf.writeShort(markSegLen); // Rsiz (codestream capabilities) hbuf.writeShort(0); // JPEG 2000 - Part I // Xsiz (original image width) hbuf.writeInt(tiler.getImgWidth()+tiler.getImgULX()); // Ysiz (original image height) hbuf.writeInt(tiler.getImgHeight()+tiler.getImgULY()); // XOsiz (horizontal offset from the origin of the reference // grid to the left side of the image area) hbuf.writeInt(tiler.getImgULX()); // YOsiz (vertical offset from the origin of the reference // grid to the top side of the image area) hbuf.writeInt(tiler.getImgULY()); // XTsiz (nominal tile width) hbuf.writeInt(tiler.getNomTileWidth()); // YTsiz (nominal tile height) hbuf.writeInt(tiler.getNomTileHeight()); Coord torig = tiler.getTilingOrigin(null); // XTOsiz (Horizontal offset from the origin of the reference // grid to the left side of the first tile) hbuf.writeInt(torig.x); // YTOsiz (Vertical offset from the origin of the reference // grid to the top side of the first tile) hbuf.writeInt(torig.y); // Csiz (number of components) hbuf.writeShort(nComp); // Bit-depth and downsampling factors. for(int c=0; c<nComp; c++){ // Loop on each component // Ssiz bit-depth before mixing tmp = origSrc.getNomRangeBits(c)-1; tmp |= ( (isOrigSig[c]?1:0)<<SSIZ_DEPTH_BITS ); hbuf.write(tmp); // XRsiz (component sub-sampling value x-wise) hbuf.write(tiler.getCompSubsX(c)); // YRsiz (component sub-sampling value y-wise) hbuf.write(tiler.getCompSubsY(c)); } // End loop on each component } /** * Writes COD marker segment. COD is a functional marker segment * containing the code style default (coding style, decomposition, * layering) used for compressing all the components in an image. * * <p>The values can be overriden for an individual component by a COC * marker in either the main or the tile header. * * @param mh Flag indicating whether this marker belongs to the main * header * * @param tileIdx Tile index if the marker belongs to a tile-part header * * @see #writeCOC * */ protected void writeCOD(boolean mh, int tileIdx) throws IOException { AnWTFilter[][] filt; boolean precinctPartitionUsed; int tmp; int mrl=0,a=0; int ppx=0, ppy=0; Progression[] prog; if (mh) { mrl = ((Integer)encSpec.dls.getDefault()).intValue(); // get default precinct size ppx = encSpec.pss.getPPX(-1,-1,mrl); ppy = encSpec.pss.getPPY(-1,-1,mrl); prog = (Progression[])(encSpec.ps.getDefault()); } else { mrl = ((Integer)encSpec.dls.getTileDef(tileIdx)).intValue(); // get precinct size for specified tile ppx = encSpec.pss.getPPX(tileIdx,-1,mrl); ppy = encSpec.pss.getPPY(tileIdx,-1,mrl); prog = (Progression[])(encSpec.ps.getTileDef(tileIdx)); } if ( ppx != PRECINCT_PARTITION_DEF_SIZE || ppy != PRECINCT_PARTITION_DEF_SIZE ) { precinctPartitionUsed = true; } else { precinctPartitionUsed = false; } if ( precinctPartitionUsed ) { // If precinct partition is used we add one byte per resolution // level i.e. mrl+1 (+1 for resolution 0). a = mrl+1; } // Write COD marker hbuf.writeShort(COD); // Lcod (marker segment length (in bytes)) Basic : Lcod(2 // bytes)+Scod(1)+SGcod(4)+SPcod(5+a) where: // a=0 if no precinct partition is used // a=mrl+1 if precinct partition used int markSegLen = 12+a; hbuf.writeShort(markSegLen); // Scod (coding style parameter) tmp=0; if ( precinctPartitionUsed ) tmp=SCOX_PRECINCT_PARTITION; // Are SOP markers used ? if (mh) { if( ((String)encSpec.sops.getDefault().toString()) .equalsIgnoreCase("on") ) { tmp |= SCOX_USE_SOP; } } else { if ( ((String)encSpec.sops.getTileDef(tileIdx).toString()) .equalsIgnoreCase("on") ) { tmp |= SCOX_USE_SOP; } } // Are EPH markers used ? if(mh){ if ( ((String)encSpec.ephs.getDefault().toString()) .equalsIgnoreCase("on") ) { tmp |= SCOX_USE_EPH; } } else{ if ( ((String)encSpec.ephs.getTileDef(tileIdx).toString()) .equalsIgnoreCase("on") ) { tmp |= SCOX_USE_EPH; } } hbuf.write(tmp); // SGcod // Progression order hbuf.write(prog[0].type); // Number of layers hbuf.writeShort(ralloc.getNumLayers()); // Multiple component transform // CSsiz (Color transform) String str = null; if(mh) str = (String)encSpec.cts.getDefault(); else str = (String)encSpec.cts.getTileDef(tileIdx); if(str.equals("none")) hbuf.write(0); else hbuf.write(1); // SPcod // Number of decomposition levels hbuf.write(mrl); // Code-block width and height if ( mh ) { // main header, get default values tmp = encSpec.cblks. getCBlkWidth(ModuleSpec.SPEC_DEF,-1,-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -