    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
    023/**
    024 * Abstracts segment implementations.
    025 */
    026public abstract class AbstractSegment extends BinaryFileParser {
    027
    028    /** Segment marker. */
    029    public final int marker;
    030
    031    /** Segment length. */
    032    public final int length;
    033
    034    /**
    035     * Constructs a new instance.
    036     *
    037     * @param marker segment marker.
    038     * @param length segment length.
    039     */
    040    public AbstractSegment(final int marker, final int length) {
    041        this.marker = marker;
    042        this.length = length;
    043    }
    044
    045    public void dump(final PrintWriter pw) {
    046        // empty
    047    }
    048
    049    /**
    050     * Gets the description.
    051     *
    052     * @return the description.
    053     */
    054    public abstract String getDescription();
    055
    056    /**
    057     * Gets the type.
    058     *
    059     * @return the type.
    060     */
    061    public String getSegmentType() {
    062        switch (marker) {
    063        case 0xffc0:
    064            return "Start Of Frame, Baseline Dct, Huffman coding";
    065        case 0xffc1:
    066            return "Start Of Frame, Extended sequential Dct, Huffman coding";
    067        case 0xffc2:
    068            return "Start Of Frame, Progressive Dct, Huffman coding";
    069        case 0xffc3:
    070            return "Start Of Frame, Lossless (sequential), Huffman coding";
    071        case 0xffc5:
    072            return "Start Of Frame, Differential sequential Dct, Huffman coding";
    073        case 0xffc6:
    074            return "Start Of Frame, Differential progressive Dct, Huffman coding";
    075        case 0xffc7:
    076            return "Start Of Frame, Differential lossless (sequential), Huffman coding";
    077        case 0xffc8:
    078            return "Start Of Frame, Reserved for JPEG extensions, arithmetic coding";
    079        case 0xffc9:
    080            return "Start Of Frame, Extended sequential Dct, arithmetic coding";
    081        case 0xffca:
    082            return "Start Of Frame, Progressive Dct, arithmetic coding";
    083        case 0xffcb:
    084            return "Start Of Frame, Lossless (sequential), arithmetic coding";
    085        case 0xffcd:
    086            return "Start Of Frame, Differential sequential Dct, arithmetic coding";
    087        case 0xffce:
    088            return "Start Of Frame, Differential progressive Dct, arithmetic coding";
    089        case 0xffcf:
    090            return "Start Of Frame, Differential lossless (sequential), arithmetic coding";
    091        case 0xffc4:
    092            return "Define Huffman table(s)";
    093        case 0xffcc:
    094            return "Define arithmetic coding conditioning(s)";
    095        case 0xffd0:
    096            return "Restart with modulo 8 count 0";
    097        case 0xffd1:
    098            return "Restart with modulo 8 count 1";
    099        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}