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

📄 imgwriterpgx.java

📁 jpeg2000算法实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * CVS identifier: * * $Id: ImgWriterPGX.java,v 1.10 2000/11/16 11:30:45 dsanta Exp $ * * Class:                   ImgWriterPGX * * Description:             Image Writer for PGX files (custom file format *                          for VM3A) * * * * 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.image.output;import jj2000.j2k.image.*;import jj2000.j2k.util.*;import java.io.*;/** * This class extends the ImgWriter abstract class for writing PGX files. * PGX is a custom monochrome file format invented specifically to simplify  * the use of VM3A with images of different bit-depths in the range 1 to 31  * bits per pixel. * * <P>The file consists of a one line text header followed by the binary datas. * <P><u>Header:</u><br> * <tt>"PG"+ <i>ws</i> +&lt;<i>endianess</i>&gt;+ <i>ws</i> +<i>[sign]</i>+  * <i>ws</i> + &lt;<i>bit-depth</i>&gt;+" "+&lt;<i>width</i>&gt;+" "+ * &lt;<i>height</i>&gt;+'\n'</tt><br> * where:<br> * - <i>ws</i>(white-spaces) is any combination of characters <tt>' '</tt> and  * <tt>'\t'</tt>.<br> * - <i>endianess</i> equals <tt>"LM"</tt> or <tt>"ML"</tt>(resp. little-endian * or big-endian).<br> * - <i>sign</i> equals "+" or "-" (resp. unsigned or signed). If ommited,  * default value is unsigned.<br> * - <i>bit-depth</i> can be any number between 1 and 31.<br> * - <i>width</i> and <i>height</i> are the image dimensions (in pixels).<br> * <u>Datas:</u><br> * The image binary datas appear one after the other(in raster order)  * immediatly after the last header character('\n') and are byte-aligned (they * are packed into 1,2 or 4 bytes per sample, depending upon the  * <i>bit-depth</i> value). * * <P> If the data is unsigned, level shifting is applied adding * 2^(bit depth - 1) * * <P>NOTE: This class is not thread safe, for reasons of internal buffering. * * @see ImgWriter * * @see BlkImgDataSrc * * */public class ImgWriterPGX extends ImgWriter {    /** Used during saturation (2^bitdepth-1 if unsigned,     * 2^(bitdepth-1)-1 if signed)*/    int maxVal;    /** Used during saturation (0 if unsigned, -2^(bitdepth-1) if signed) */    int minVal;    /** Used with level-shiting */    int levShift;    /** Whether the data must be signed when writing or not. In the     * latter case inverse level shifting must be applied */    boolean isSigned;    /** The bit-depth of the input file (must be between 1 and 31)*/    private int bitDepth;    /** Where to write the data */    private RandomAccessFile out;        /** The offset of the raw pixel data in the PGX file */    private int offset;    /** A DataBlk, just used to avoid allocating a new one each time        it is needed */    private DataBlkInt db = new DataBlkInt();    /** The number of fractional bits in the source data */    private int fb;    /** The index of the component from where to get the data */    private int c;    /** The pack length of one sample (in bytes, according to the        output bit-depth */    private int packBytes;    /** The line buffer. */    // This makes the class not thrad safe    // (but it is not the only one making it so)    private byte buf[];    /**     * Creates a new writer to the specified File object, to write data from     * the specified component.     *     * <P>The size of the image that is written to the file is the     * size of the component from which to get the data, specified by     * b, not the size of the source image (they differ if there is     * some sub-sampling).     *     * <P>All the header informations are given by the BlkImgDataSrc source     * (component width, component height, bit-depth) and sign flag, which are     * provided to the constructor. The endianness is always big-endian (MSB     * first).     *     * @param out The file where to write the data     *     * @param imgSrc The source from where to get the image data to     * write.     *     * @param n The index of the component from where to get the data.     *     * @param isSigned Whether the datas are signed or not (needed only     *                 when writing header).     *     * @see DataBlk     *     *     * */    public ImgWriterPGX(File out, 			BlkImgDataSrc imgSrc, 			int n, boolean isSigned) throws IOException{        //Initialize        this.c = n;        if (out.exists() && !out.delete()) {            throw new IOException("Could not reset file");        }        this.out = new RandomAccessFile(out,"rw");        this.isSigned = isSigned;        src = imgSrc;        w = src.getCompImgWidth(n);        h = src.getCompImgHeight(n);        fb = imgSrc.getFixedPoint(n);                bitDepth = src.getNomRangeBits(this.c);        if((bitDepth<=0)||(bitDepth>31))            throw new IOException("PGX supports only bit-depth between "+                                  "1 and 31");        if(bitDepth<=8)            packBytes = 1;        else if(bitDepth<=16)            packBytes = 2;        else // <= 31            packBytes = 4;        // Writes PGX header        String tmpString =             "PG "            + "ML " // Always writing big-endian            + ((this.isSigned) ? "- " : "+ ") // signed/unsigned            + bitDepth + " " // bit-depth            + w + " " // component width            + h + "\n"; // component height        byte[] tmpByte = tmpString.getBytes();        for(int i=0; i<tmpByte.length; i++)            this.out.write(tmpByte[i]);        offset = tmpByte.length;        maxVal =             (this.isSigned)?            (( 1<<(src.getNomRangeBits(n)-1) )-1):            ((1<<src.getNomRangeBits(n))-1);        minVal =            (this.isSigned)?            (-1 * ( 1<<(src.getNomRangeBits(n)-1) )):            0;                    levShift =             (this.isSigned)?            0:            1<<(src.getNomRangeBits(n)-1);    }    /**     * Creates a new writer to the specified file, to write data from the     * specified component.     *     * <P>The size of the image that is written to the file is the     * size of the component from which to get the data, specified by     * b, not the size of the source image (they differ if there is     * some sub-sampling).     *     * <P>All the header informations are given by the BlkImgDataSrc source     * (component width, component height, bit-depth) and sign flag, which are     * provided to the constructor. The endianness is always big-endian (MSB     * first).     *     * @param fname The name of the file where to write the data     *     * @param imgSrc The source from where to get the image data to     * write.     *     * @param n The index of the component from where to get the data.     *     * @param isSigned Whether the datas are signed or not (needed only     *                 when writing header).     *     * @see DataBlk     *     *     * */    public ImgWriterPGX(String fname, 			BlkImgDataSrc imgSrc, 			int n, boolean isSigned) throws IOException{        this(new File(fname),imgSrc,n,isSigned);

⌨️ 快捷键说明

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