Class FileDicomJPEG


  • public class FileDicomJPEG
    extends java.lang.Object
    Class that reads a lossless compressed JPEG file. These are sometimes encapsulated in DICOM. The lossless decoder uses a Huffman table to interpret values. For color images this file will have to be modified. Some code for color is here but it isn't universal. There is a private class within this file that does the bit reading and packing. For more information see the comments in front of that class. This code was ported from Huang and Smith's Cornell software.
     Copyright (c) 1993 Cornell University, Kongji Huang
     All rights reserved.
     
     Permission to use, copy, modify, and distribute this software and its
     documentation for research purposes, without fee, and without written
     agreement is hereby granted, provided that the above copyright notice
     and the following two paragraphs appear in all copies of this
     software.
     
     IN NO EVENT SHALL THE CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR
     DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
     OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
     UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
     DAMAGE.
     
     THE CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE
     PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
     CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     ENHANCEMENTS, OR MODIFICATIONS.
     

     Copyright (c) 1993 The Regents of the University of California, Brian
     C. Smith All rights reserved.
     
     Permission to use, copy, modify, and distribute this software and its
     documentation for any purpose, without fee, and without written
     agreement is hereby granted, provided that the above copyright notice
     and the following two paragraphs appear in all copies of this
     software.
     
     IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
     FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
     ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
     THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
     SUCH DAMAGE.
     
     THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE
     PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
     CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     ENHANCEMENTS, OR MODIFICATIONS.
     

     IJG Copyright
     
     The authors make NO WARRANTY or representation, either express or
     implied, with respect to this software, its quality, accuracy,
     merchantability, or fitness for a particular purpose.  This software is
     provided "AS IS", and you, its user, assume the entire risk as to its
     quality and accuracy.
     
     This software is copyright (C) 1991, 1992, Thomas G. Lane.  All Rights
     Reserved except as specified below.
     
     Permission is hereby granted to use, copy, modify, and distribute this
     software (or portions thereof) for any purpose, without fee, subject to
     these conditions:  (1) If any part of the source code for this software
     is distributed, then this README file must be included, with this
     copyright and no-warranty notice unaltered; and any additions,
     deletions, or changes to the original files must be clearly indicated
     in accompanying documentation.  (2) If only executable code is
     distributed, then the accompanying documentation must state that "this
     software is based in part on the work of the Independent JPEG Group".
     (3) Permission for use of this software is granted only if the user
     accepts full responsibility for any undesirable consequences; the
     authors accept NO LIABILITY for damages of any kind.
     
     Permission is NOT granted for the use of any IJG author's name or
     company name in advertising or publicity relating to this software or
     products derived from it.  This software may be referred to only as
     "the Independent JPEG Group's software".
     
     We specifically permit and encourage the use of this software as the
     basis of commercial products, provided that all warranty or liability
     claims are assumed by the product vendor.
     
    Version:
    1.0 May 14, 2002
    Author:
    Neva Cherniavsky
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) class  FileDicomJPEG.HuffTable
      This class reads and packs the bits appropriately for interpreting the huffman codes.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[] bitMask
      Useful bit mask for getting appropriate bits out of number.
      private byte[][] bits
      Read in DHT header.
      private int[] componentId
      Needed for color images.
      private short[] componentIndex
      Needed for color images.
      private int compsInScan
      Number of components in scan; 1 for black and white, 4 for color.
      private byte dataPrecision
      Data precision.
      private int dicomH
      Height read in from DICOM file; only use if imageHeight isn't defined.
      private int dicomW
      Width read in from DICOM file; only use if imageWidth isn't defined.
      private static int[] extendOffset
      Entry n is (-1
      private static int[] extendTest
      Entry n is 2**(n-1); used to test whether we need to make a number negative.
      private int[] hSampFactor
      Horizontal sample factor.
      private byte[][] huffval
      Read in DHT header.
      private byte[] image
      Image data we're reading.
      private int imageHeight
      Image height.
      private int imageWidth
      Image width.
      private static byte M_APP0
      Required marker in this standard.
      private static byte M_DHT
      DOCUMENT ME!
      private static byte M_DQT
      DOCUMENT ME!
      private static byte M_DRI
      DOCUMENT ME!
      private static byte M_EOI
      DOCUMENT ME!
      private static byte M_JPG
      DOCUMENT ME!
      private static byte M_RST0
      DOCUMENT ME!
      private static byte M_RST1
      DOCUMENT ME!
      private static byte M_RST2
      DOCUMENT ME!
      private static byte M_RST3
      DOCUMENT ME!
      private static byte M_RST4
      DOCUMENT ME!
      private static byte M_RST5
      DOCUMENT ME!
      private static byte M_RST6
      DOCUMENT ME!
      private static byte M_RST7
      DOCUMENT ME!
      private static byte M_SOF0
      Marker tags in JPEG header.
      private static byte M_SOF1
      DOCUMENT ME!
      private static byte M_SOF10
      DOCUMENT ME!
      private static byte M_SOF11
      DOCUMENT ME!
      private static byte M_SOF13
      DOCUMENT ME!
      private static byte M_SOF14
      DOCUMENT ME!
      private static byte M_SOF15
      DOCUMENT ME!
      private static byte M_SOF2
      DOCUMENT ME!
      private static byte M_SOF3
      DOCUMENT ME!
      private static byte M_SOF5
      DOCUMENT ME!
      private static byte M_SOF6
      DOCUMENT ME!
      private static byte M_SOF7
      DOCUMENT ME!
      private static byte M_SOF9
      DOCUMENT ME!
      private static byte M_SOI
      DOCUMENT ME!
      private static byte M_SOS
      DOCUMENT ME!
      private static byte M_TEM
      DOCUMENT ME!
      private int[][] maxcode
      mincode[index] = minimum legal code at that index.
      private short[][] mincode
      mincode[index] = minimum legal code at that index.
      private int nextRestartNum
      Numbers needed for restart.
      private int[][] numbits
      number of bits needed for code at index.
      private int numComponents
      Number of components - 1 for black and white, 4 for color.
      private int Pt
      Shift by this when putting into int buffer.
      private int restartInRows
      Numbers needed for restart.
      private int restartInterval
      Restart interval.
      private int restartRowsToGo
      Numbers needed for restart.
      private byte Ss
      Byte indicating which location to use as the predictor.
      private int[] tableNo
      Table number - always 0 for non-color images.
      private java.util.Vector<byte[]> v  
      private java.util.Vector<int[]> v2  
      private short[][] valptr
      valptr[index] = pointer to where legal code values start.
      private int[][] value
      huffman value for code.
      private int vIndex  
      private int[] vSampFactor
      Vertical sample factor.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileDicomJPEG​(byte[] imageBuffer, int width, int height)
      Assigns image buffer and creates the other arrays needed to read in image.
      FileDicomJPEG​(java.util.Vector<byte[]> v, java.util.Vector<int[]> v2, int width, int height)
      Assigns image buffer and creates the other arrays needed to read in image.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void decodeFirstRow​(FileDicomJPEG.HuffTable table, short[][] curRowBuf)
      Decode the first raster line of samples at the start of the scan and at the beginning of each restart interval.
      private int[] decodeImage​(int index)
      Decode the input stream.
      int[] extractJPEGImage()
      Extracts the JPEG image by processing the byte image array.
      void extractMultiJPEGImage()  
      private void fixHuffmanTable​(int tableNum)
      Sets up Huffman table based on values read in header.
      private int getAPPO​(int index)
      Get APPO part of header.
      private int getDHT​(int index)
      Get the Huffman table.
      private int getDRI​(int index)
      Get DRI part of header.
      private int getSOF​(int index)
      Get the SOF part of header.
      private int getSOS​(int index)
      Get the SOS part of header.
      private int nextMarker​(int index)
      Gets the next marker in the header.
      private int processRestart​(int index)
      Check for a restart marker & resynchronize decoder.
      private int processTables​(int index)
      Process the header markers.
      private int skipVariable​(int index)
      Skip this variable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • M_APP0

        private static final byte M_APP0
        Required marker in this standard.
        See Also:
        Constant Field Values
      • extendTest

        private static int[] extendTest
        Entry n is 2**(n-1); used to test whether we need to make a number negative.
      • extendOffset

        private static int[] extendOffset
        Entry n is (-1
        • bitMask

          private final int[] bitMask
          Useful bit mask for getting appropriate bits out of number.
        • bits

          private final byte[][] bits
          Read in DHT header. Number of codes with bitlength of index. Thus, bits[2] = 1 means one code represented by 2 bits. bits[3] = 4 means four codes represented by 3 bits. 2D array necessary in case of color image - component #.
        • componentId

          private final int[] componentId
          Needed for color images.
        • componentIndex

          private final short[] componentIndex
          Needed for color images.
        • compsInScan

          private int compsInScan
          Number of components in scan; 1 for black and white, 4 for color.
        • dataPrecision

          private byte dataPrecision
          Data precision.
        • dicomH

          private final int dicomH
          Height read in from DICOM file; only use if imageHeight isn't defined.
        • dicomW

          private final int dicomW
          Width read in from DICOM file; only use if imageWidth isn't defined.
        • hSampFactor

          private final int[] hSampFactor
          Horizontal sample factor.
        • huffval

          private final byte[][] huffval
          Read in DHT header. Huffman code values, used to determine Huffman table (value array). Also directly accessed when numbits > 8, since fast lookup is not possible.
        • image

          private byte[] image
          Image data we're reading.
        • imageHeight

          private int imageHeight
          Image height.
        • imageWidth

          private int imageWidth
          Image width.
        • maxcode

          private final int[][] maxcode
          mincode[index] = minimum legal code at that index.
        • mincode

          private final short[][] mincode
          mincode[index] = minimum legal code at that index.
        • nextRestartNum

          private int nextRestartNum
          Numbers needed for restart. Hasn't been tested!
        • numbits

          private final int[][] numbits
          number of bits needed for code at index.
        • numComponents

          private int numComponents
          Number of components - 1 for black and white, 4 for color.
        • Pt

          private int Pt
          Shift by this when putting into int buffer.
        • restartInRows

          private int restartInRows
          Numbers needed for restart. Hasn't been tested!
        • restartInterval

          private int restartInterval
          Restart interval.
        • restartRowsToGo

          private int restartRowsToGo
          Numbers needed for restart. Hasn't been tested!
        • Ss

          private byte Ss
          Byte indicating which location to use as the predictor.
        • tableNo

          private final int[] tableNo
          Table number - always 0 for non-color images.
        • valptr

          private final short[][] valptr
          valptr[index] = pointer to where legal code values start.
        • value

          private final int[][] value
          huffman value for code.
        • vSampFactor

          private final int[] vSampFactor
          Vertical sample factor.
        • v

          private java.util.Vector<byte[]> v
        • vIndex

          private int vIndex
        • v2

          private java.util.Vector<int[]> v2
        • Constructor Detail

          • FileDicomJPEG

            public FileDicomJPEG​(byte[] imageBuffer,
                                 int width,
                                 int height)
            Assigns image buffer and creates the other arrays needed to read in image.
            Parameters:
            imageBuffer - Image buffer.
            width - DOCUMENT ME!
            height - DOCUMENT ME!
          • FileDicomJPEG

            public FileDicomJPEG​(java.util.Vector<byte[]> v,
                                 java.util.Vector<int[]> v2,
                                 int width,
                                 int height)
            Assigns image buffer and creates the other arrays needed to read in image.
            Parameters:
            imageBuffer - Image buffer.
            width - DOCUMENT ME!
            height - DOCUMENT ME!
        • Method Detail

          • extractMultiJPEGImage

            public void extractMultiJPEGImage()
                                       throws java.io.IOException
            Throws:
            java.io.IOException
          • extractJPEGImage

            public int[] extractJPEGImage()
                                   throws java.io.IOException
            Extracts the JPEG image by processing the byte image array.
            Returns:
            image buffer - the JPEG image
            Throws:
            java.io.IOException - DOCUMENT ME!
          • decodeFirstRow

            private void decodeFirstRow​(FileDicomJPEG.HuffTable table,
                                        short[][] curRowBuf)
            Decode the first raster line of samples at the start of the scan and at the beginning of each restart interval. This includes modifying the component value so the real value, not the difference is returned.
            Parameters:
            table - Huffman table.
            curRowBuf - Current row buffer; 2D because if image is color, there are 4 components.
          • decodeImage

            private int[] decodeImage​(int index)
            Decode the input stream. This includes modifying the component value so the real value, not the difference is returned.
            Parameters:
            index - Index where we currently are in the image.
            Returns:
            Buffer holding decompressed image.
          • fixHuffmanTable

            private void fixHuffmanTable​(int tableNum)
            Sets up Huffman table based on values read in header.
            Parameters:
            tableNum - Usually 0, haven't tested when it isn't.
          • getAPPO

            private int getAPPO​(int index)
            Get APPO part of header.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • getDHT

            private int getDHT​(int index)
            Get the Huffman table.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • getDRI

            private int getDRI​(int index)
            Get DRI part of header.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • getSOF

            private int getSOF​(int index)
            Get the SOF part of header.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • getSOS

            private int getSOS​(int index)
            Get the SOS part of header.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • nextMarker

            private int nextMarker​(int index)
            Gets the next marker in the header.
            Parameters:
            index - Index that we're at in the image array.
            Returns:
            Last accessed index.
          • processRestart

            private int processRestart​(int index)
            Check for a restart marker & resynchronize decoder. Haven't tested this at all.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • processTables

            private int processTables​(int index)
            Process the header markers.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.
          • skipVariable

            private int skipVariable​(int index)
            Skip this variable.
            Parameters:
            index - Index we're at in the image array.
            Returns:
            Last accessed index.