Class FileDicomJPEG

java.lang.Object
gov.nih.mipav.model.file.FileDicomJPEG

public class FileDicomJPEG extends 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 
    This class reads and packs the bits appropriately for interpreting the huffman codes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int[]
    Useful bit mask for getting appropriate bits out of number.
    private final byte[][]
    Read in DHT header.
    private final int[]
    Needed for color images.
    private final short[]
    Needed for color images.
    private int
    Number of components in scan; 1 for black and white, 4 for color.
    private byte
    Data precision.
    private final int
    Height read in from DICOM file; only use if imageHeight isn't defined.
    private final int
    Width read in from DICOM file; only use if imageWidth isn't defined.
    private static int[]
    Entry n is (-1 invalid input: '<'invalid input: '<' n) + 1; used to make a number negative.
    private static int[]
    Entry n is 2**(n-1); used to test whether we need to make a number negative.
    private final int[]
    Horizontal sample factor.
    private final byte[][]
    Read in DHT header.
    private byte[]
    Image data we're reading.
    private int
    Image height.
    private int
    Image width.
    private static final byte
    Required marker in this standard.
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    Marker tags in JPEG header.
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private static final byte
    DOCUMENT ME!
    private final int[][]
    mincode[index] = minimum legal code at that index.
    private final short[][]
    mincode[index] = minimum legal code at that index.
    private int
    Numbers needed for restart.
    private final int[][]
    number of bits needed for code at index.
    private int
    Number of components - 1 for black and white, 4 for color.
    private int
    Shift by this when putting into int buffer.
    private int
    Numbers needed for restart.
    private int
    Restart interval.
    private int
    Numbers needed for restart.
    private byte
    Byte indicating which location to use as the predictor.
    private final int[]
    Table number - always 0 for non-color images.
    private Vector<byte[]>
     
    private Vector<int[]>
     
    private final short[][]
    valptr[index] = pointer to where legal code values start.
    private final int[][]
    huffman value for code.
    private int
     
    private final int[]
    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(Vector<byte[]> v, Vector<int[]> v2, int width, int height)
    Assigns image buffer and creates the other arrays needed to read in image.
  • Method Summary

    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[]
    Extracts the JPEG image by processing the byte image array.
    void
     
    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 invalid input: '&' 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 Details

    • M_SOF0

      private static final byte M_SOF0
      Marker tags in JPEG header.
      See Also:
    • M_SOF1

      private static final byte M_SOF1
      DOCUMENT ME!
      See Also:
    • M_SOF2

      private static final byte M_SOF2
      DOCUMENT ME!
      See Also:
    • M_SOF3

      private static final byte M_SOF3
      DOCUMENT ME!
      See Also:
    • M_SOF5

      private static final byte M_SOF5
      DOCUMENT ME!
      See Also:
    • M_SOF6

      private static final byte M_SOF6
      DOCUMENT ME!
      See Also:
    • M_SOF7

      private static final byte M_SOF7
      DOCUMENT ME!
      See Also:
    • M_JPG

      private static final byte M_JPG
      DOCUMENT ME!
      See Also:
    • M_SOF9

      private static final byte M_SOF9
      DOCUMENT ME!
      See Also:
    • M_SOF10

      private static final byte M_SOF10
      DOCUMENT ME!
      See Also:
    • M_SOF11

      private static final byte M_SOF11
      DOCUMENT ME!
      See Also:
    • M_SOF13

      private static final byte M_SOF13
      DOCUMENT ME!
      See Also:
    • M_SOF14

      private static final byte M_SOF14
      DOCUMENT ME!
      See Also:
    • M_SOF15

      private static final byte M_SOF15
      DOCUMENT ME!
      See Also:
    • M_DHT

      private static final byte M_DHT
      DOCUMENT ME!
      See Also:
    • M_RST0

      private static final byte M_RST0
      DOCUMENT ME!
      See Also:
    • M_RST1

      private static final byte M_RST1
      DOCUMENT ME!
      See Also:
    • M_RST2

      private static final byte M_RST2
      DOCUMENT ME!
      See Also:
    • M_RST3

      private static final byte M_RST3
      DOCUMENT ME!
      See Also:
    • M_RST4

      private static final byte M_RST4
      DOCUMENT ME!
      See Also:
    • M_RST5

      private static final byte M_RST5
      DOCUMENT ME!
      See Also:
    • M_RST6

      private static final byte M_RST6
      DOCUMENT ME!
      See Also:
    • M_RST7

      private static final byte M_RST7
      DOCUMENT ME!
      See Also:
    • M_SOI

      private static final byte M_SOI
      DOCUMENT ME!
      See Also:
    • M_EOI

      private static final byte M_EOI
      DOCUMENT ME!
      See Also:
    • M_SOS

      private static final byte M_SOS
      DOCUMENT ME!
      See Also:
    • M_DQT

      private static final byte M_DQT
      DOCUMENT ME!
      See Also:
    • M_DRI

      private static final byte M_DRI
      DOCUMENT ME!
      See Also:
    • M_APP0

      private static final byte M_APP0
      Required marker in this standard.
      See Also:
    • M_TEM

      private static final byte M_TEM
      DOCUMENT ME!
      See Also:
    • 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 invalid input: '<'invalid input: '<' n) + 1; used to make a number negative.
    • 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 Vector<byte[]> v
    • vIndex

      private int vIndex
    • v2

      private Vector<int[]> v2
  • Constructor Details

    • 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(Vector<byte[]> v, Vector<int[]> v2, int width, int height)
      Assigns image buffer and creates the other arrays needed to read in image.
      Parameters:
      width - DOCUMENT ME!
      height - DOCUMENT ME!
      imageBuffer - Image buffer.
  • Method Details

    • extractMultiJPEGImage

      public void extractMultiJPEGImage() throws IOException
      Throws:
      IOException
    • extractJPEGImage

      public int[] extractJPEGImage() throws IOException
      Extracts the JPEG image by processing the byte image array.
      Returns:
      image buffer - the JPEG image
      Throws:
      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 invalid input: '&' 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.