001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.formats.jpeg.segments;
018
019import java.io.PrintWriter;
020
021import org.apache.commons.imaging.common.BinaryFileParser;
022
023public abstract class AbstractSegment extends BinaryFileParser {
024    public final int marker;
025    public final int length;
026
027    public AbstractSegment(final int marker, final int length) {
028        // super();
029
030        this.marker = marker;
031        this.length = length;
032    }
033
034    public void dump(final PrintWriter pw) {
035        // empty
036    }
037
038    public abstract String getDescription();
039
040    public String getSegmentType() {
041
042        switch (marker) {
043        case 0xffc0:
044            return "Start Of Frame, Baseline Dct, Huffman coding";
045        case 0xffc1:
046            return "Start Of Frame, Extended sequential Dct, Huffman coding";
047        case 0xffc2:
048            return "Start Of Frame, Progressive Dct, Huffman coding";
049        case 0xffc3:
050            return "Start Of Frame, Lossless (sequential), Huffman coding";
051
052        case 0xffc5:
053            return "Start Of Frame, Differential sequential Dct, Huffman coding";
054        case 0xffc6:
055            return "Start Of Frame, Differential progressive Dct, Huffman coding";
056        case 0xffc7:
057            return "Start Of Frame, Differential lossless (sequential), Huffman coding";
058
059        case 0xffc8:
060            return "Start Of Frame, Reserved for JPEG extensions, arithmetic coding";
061        case 0xffc9:
062            return "Start Of Frame, Extended sequential Dct, arithmetic coding";
063        case 0xffca:
064            return "Start Of Frame, Progressive Dct, arithmetic coding";
065        case 0xffcb:
066            return "Start Of Frame, Lossless (sequential), arithmetic coding";
067
068        case 0xffcd:
069            return "Start Of Frame, Differential sequential Dct, arithmetic coding";
070        case 0xffce:
071            return "Start Of Frame, Differential progressive Dct, arithmetic coding";
072        case 0xffcf:
073            return "Start Of Frame, Differential lossless (sequential), arithmetic coding";
074
075        case 0xffc4:
076            return "Define Huffman table(s)";
077        case 0xffcc:
078            return "Define arithmetic coding conditioning(s)";
079
080        case 0xffd0:
081            return "Restart with modulo 8 count 0";
082        case 0xffd1:
083            return "Restart with modulo 8 count 1";
084        case 0xffd2:
085            return "Restart with modulo 8 count 2";
086        case 0xffd3:
087            return "Restart with modulo 8 count 3";
088        case 0xffd4:
089            return "Restart with modulo 8 count 4";
090        case 0xffd5:
091            return "Restart with modulo 8 count 5";
092        case 0xffd6:
093            return "Restart with modulo 8 count 6";
094        case 0xffd7:
095            return "Restart with modulo 8 count 7";
096
097        case 0xffd8:
098            return "Start of image";
099        case 0xffd9:
100            return "End of image";
101        case 0xffda:
102            return "Start of scan";
103        case 0xffdb:
104            return "Define quantization table(s)";
105        case 0xffdc:
106            return "Define number of lines";
107        case 0xffdd:
108            return "Define restart interval";
109        case 0xffde:
110            return "Define hierarchical progression";
111        case 0xffdf:
112            return "Expand reference component(s)";
113        // case 0xffd8 :
114        // return "Reserved for application segments";
115        // case 0xffd8 :
116        // return "Reserved for JPEG extensions";
117        case 0xfffe:
118            return "Comment";
119        case 0xff01:
120            return "For temporary private use in arithmetic coding";
121        // case 0xffd8 :
122        // return "Reserved";
123
124        default:
125        }
126
127        if (marker >= 0xff02 && marker <= 0xffbf) {
128            return "Reserved";
129        }
130        if (marker >= 0xffe0 && marker <= 0xffef) {
131            return "APP" + (marker - 0xffe0);
132        }
133        if (marker >= 0xfff0 && marker <= 0xfffd) {
134            return "JPG" + (marker - 0xffe0);
135        }
136
137        return "Unknown";
138
139    }
140
141    @Override
142    public String toString() {
143        return "[Segment: " + getDescription() + "]";
144    }
145
146}