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 Segment extends BinaryFileParser {
024    public final int marker;
025    public final int length;
026
027    public Segment(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    @Override
041    public String toString() {
042        return "[Segment: " + getDescription() + "]";
043    }
044
045    public String getSegmentType() {
046
047        switch (marker) {
048        case 0xffc0:
049            return "Start Of Frame, Baseline Dct, Huffman coding";
050        case 0xffc1:
051            return "Start Of Frame, Extended sequential Dct, Huffman coding";
052        case 0xffc2:
053            return "Start Of Frame, Progressive Dct, Huffman coding";
054        case 0xffc3:
055            return "Start Of Frame, Lossless (sequential), Huffman coding";
056
057        case 0xffc5:
058            return "Start Of Frame, Differential sequential Dct, Huffman coding";
059        case 0xffc6:
060            return "Start Of Frame, Differential progressive Dct, Huffman coding";
061        case 0xffc7:
062            return "Start Of Frame, Differential lossless (sequential), Huffman coding";
063
064        case 0xffc8:
065            return "Start Of Frame, Reserved for JPEG extensions, arithmetic coding";
066        case 0xffc9:
067            return "Start Of Frame, Extended sequential Dct, arithmetic coding";
068        case 0xffca:
069            return "Start Of Frame, Progressive Dct, arithmetic coding";
070        case 0xffcb:
071            return "Start Of Frame, Lossless (sequential), arithmetic coding";
072
073        case 0xffcd:
074            return "Start Of Frame, Differential sequential Dct, arithmetic coding";
075        case 0xffce:
076            return "Start Of Frame, Differential progressive Dct, arithmetic coding";
077        case 0xffcf:
078            return "Start Of Frame, Differential lossless (sequential), arithmetic coding";
079
080        case 0xffc4:
081            return "Define Huffman table(s)";
082        case 0xffcc:
083            return "Define arithmetic coding conditioning(s)";
084
085        case 0xffd0:
086            return "Restart with modulo 8 count 0";
087        case 0xffd1:
088            return "Restart with modulo 8 count 1";
089        case 0xffd2:
090            return "Restart with modulo 8 count 2";
091        case 0xffd3:
092            return "Restart with modulo 8 count 3";
093        case 0xffd4:
094            return "Restart with modulo 8 count 4";
095        case 0xffd5:
096            return "Restart with modulo 8 count 5";
097        case 0xffd6:
098            return "Restart with modulo 8 count 6";
099        case 0xffd7:
100            return "Restart with modulo 8 count 7";
101
102        case 0xffd8:
103            return "Start of image";
104        case 0xffd9:
105            return "End of image";
106        case 0xffda:
107            return "Start of scan";
108        case 0xffdb:
109            return "Define quantization table(s)";
110        case 0xffdc:
111            return "Define number of lines";
112        case 0xffdd:
113            return "Define restart interval";
114        case 0xffde:
115            return "Define hierarchical progression";
116        case 0xffdf:
117            return "Expand reference component(s)";
118            // case 0xffd8 :
119            // return "Reserved for application segments";
120            // case 0xffd8 :
121            // return "Reserved for JPEG extensions";
122        case 0xfffe:
123            return "Comment";
124        case 0xff01:
125            return "For temporary private use in arithmetic coding";
126            // case 0xffd8 :
127            // return "Reserved";
128
129        default:
130        }
131
132        if ((marker >= 0xff02) && (marker <= 0xffbf)) {
133            return "Reserved";
134        }
135        if ((marker >= 0xffe0) && (marker <= 0xffef)) {
136            return "APP" + (marker - 0xffe0);
137        }
138        if ((marker >= 0xfff0) && (marker <= 0xfffd)) {
139            return "JPG" + (marker - 0xffe0);
140        }
141
142        return "Unknown";
143
144    }
145
146}