View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.jpeg.segments;
18  
19  import java.io.PrintWriter;
20  
21  import org.apache.commons.imaging.common.BinaryFileParser;
22  
23  /**
24   * Abstracts segment implementations.
25   */
26  public abstract class AbstractSegment extends BinaryFileParser {
27  
28      /** Segment marker. */
29      public final int marker;
30  
31      /** Segment length. */
32      public final int length;
33  
34      /**
35       * Constructs a new instance.
36       *
37       * @param marker segment marker.
38       * @param length segment length.
39       */
40      public AbstractSegment(final int marker, final int length) {
41          this.marker = marker;
42          this.length = length;
43      }
44  
45      public void dump(final PrintWriter pw) {
46          // empty
47      }
48  
49      /**
50       * Gets the description.
51       *
52       * @return the description.
53       */
54      public abstract String getDescription();
55  
56      /**
57       * Gets the type.
58       *
59       * @return the type.
60       */
61      public String getSegmentType() {
62          switch (marker) {
63          case 0xffc0:
64              return "Start Of Frame, Baseline Dct, Huffman coding";
65          case 0xffc1:
66              return "Start Of Frame, Extended sequential Dct, Huffman coding";
67          case 0xffc2:
68              return "Start Of Frame, Progressive Dct, Huffman coding";
69          case 0xffc3:
70              return "Start Of Frame, Lossless (sequential), Huffman coding";
71          case 0xffc5:
72              return "Start Of Frame, Differential sequential Dct, Huffman coding";
73          case 0xffc6:
74              return "Start Of Frame, Differential progressive Dct, Huffman coding";
75          case 0xffc7:
76              return "Start Of Frame, Differential lossless (sequential), Huffman coding";
77          case 0xffc8:
78              return "Start Of Frame, Reserved for JPEG extensions, arithmetic coding";
79          case 0xffc9:
80              return "Start Of Frame, Extended sequential Dct, arithmetic coding";
81          case 0xffca:
82              return "Start Of Frame, Progressive Dct, arithmetic coding";
83          case 0xffcb:
84              return "Start Of Frame, Lossless (sequential), arithmetic coding";
85          case 0xffcd:
86              return "Start Of Frame, Differential sequential Dct, arithmetic coding";
87          case 0xffce:
88              return "Start Of Frame, Differential progressive Dct, arithmetic coding";
89          case 0xffcf:
90              return "Start Of Frame, Differential lossless (sequential), arithmetic coding";
91          case 0xffc4:
92              return "Define Huffman table(s)";
93          case 0xffcc:
94              return "Define arithmetic coding conditioning(s)";
95          case 0xffd0:
96              return "Restart with modulo 8 count 0";
97          case 0xffd1:
98              return "Restart with modulo 8 count 1";
99          case 0xffd2:
100             return "Restart with modulo 8 count 2";
101         case 0xffd3:
102             return "Restart with modulo 8 count 3";
103         case 0xffd4:
104             return "Restart with modulo 8 count 4";
105         case 0xffd5:
106             return "Restart with modulo 8 count 5";
107         case 0xffd6:
108             return "Restart with modulo 8 count 6";
109         case 0xffd7:
110             return "Restart with modulo 8 count 7";
111         case 0xffd8:
112             return "Start of image";
113         case 0xffd9:
114             return "End of image";
115         case 0xffda:
116             return "Start of scan";
117         case 0xffdb:
118             return "Define quantization table(s)";
119         case 0xffdc:
120             return "Define number of lines";
121         case 0xffdd:
122             return "Define restart interval";
123         case 0xffde:
124             return "Define hierarchical progression";
125         case 0xffdf:
126             return "Expand reference component(s)";
127         // case 0xffd8 :
128         // return "Reserved for application segments";
129         // case 0xffd8 :
130         // return "Reserved for JPEG extensions";
131         case 0xfffe:
132             return "Comment";
133         case 0xff01:
134             return "For temporary private use in arithmetic coding";
135         // case 0xffd8 :
136         // return "Reserved";
137         default:
138         }
139         if (marker >= 0xff02 && marker <= 0xffbf) {
140             return "Reserved";
141         }
142         if (marker >= 0xffe0 && marker <= 0xffef) {
143             return "APP" + (marker - 0xffe0);
144         }
145         if (marker >= 0xfff0 && marker <= 0xfffd) {
146             return "JPG" + (marker - 0xffe0);
147         }
148         return "Unknown";
149     }
150 
151     @Override
152     public String toString() {
153         return "[Segment: " + getDescription() + "]";
154     }
155 
156 }