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  public abstract class AbstractSegment extends BinaryFileParser {
24      public final int marker;
25      public final int length;
26  
27      public AbstractSegment(final int marker, final int length) {
28          // super();
29  
30          this.marker = marker;
31          this.length = length;
32      }
33  
34      public void dump(final PrintWriter pw) {
35          // empty
36      }
37  
38      public abstract String getDescription();
39  
40      public String getSegmentType() {
41  
42          switch (marker) {
43          case 0xffc0:
44              return "Start Of Frame, Baseline Dct, Huffman coding";
45          case 0xffc1:
46              return "Start Of Frame, Extended sequential Dct, Huffman coding";
47          case 0xffc2:
48              return "Start Of Frame, Progressive Dct, Huffman coding";
49          case 0xffc3:
50              return "Start Of Frame, Lossless (sequential), Huffman coding";
51  
52          case 0xffc5:
53              return "Start Of Frame, Differential sequential Dct, Huffman coding";
54          case 0xffc6:
55              return "Start Of Frame, Differential progressive Dct, Huffman coding";
56          case 0xffc7:
57              return "Start Of Frame, Differential lossless (sequential), Huffman coding";
58  
59          case 0xffc8:
60              return "Start Of Frame, Reserved for JPEG extensions, arithmetic coding";
61          case 0xffc9:
62              return "Start Of Frame, Extended sequential Dct, arithmetic coding";
63          case 0xffca:
64              return "Start Of Frame, Progressive Dct, arithmetic coding";
65          case 0xffcb:
66              return "Start Of Frame, Lossless (sequential), arithmetic coding";
67  
68          case 0xffcd:
69              return "Start Of Frame, Differential sequential Dct, arithmetic coding";
70          case 0xffce:
71              return "Start Of Frame, Differential progressive Dct, arithmetic coding";
72          case 0xffcf:
73              return "Start Of Frame, Differential lossless (sequential), arithmetic coding";
74  
75          case 0xffc4:
76              return "Define Huffman table(s)";
77          case 0xffcc:
78              return "Define arithmetic coding conditioning(s)";
79  
80          case 0xffd0:
81              return "Restart with modulo 8 count 0";
82          case 0xffd1:
83              return "Restart with modulo 8 count 1";
84          case 0xffd2:
85              return "Restart with modulo 8 count 2";
86          case 0xffd3:
87              return "Restart with modulo 8 count 3";
88          case 0xffd4:
89              return "Restart with modulo 8 count 4";
90          case 0xffd5:
91              return "Restart with modulo 8 count 5";
92          case 0xffd6:
93              return "Restart with modulo 8 count 6";
94          case 0xffd7:
95              return "Restart with modulo 8 count 7";
96  
97          case 0xffd8:
98              return "Start of image";
99          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 }