/*
 * Copyright 2002-2017 Drew Noakes
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 * More information about this project is available at:
 *
 *    https://drewnoakes.com/code/exif/
 *    https://github.com/drewnoakes/metadata-extractor
 */
package com.drew.metadata.mp4.boxes;

import com.drew.lang.SequentialReader;
import com.drew.metadata.mp4.Mp4Directory;
import com.drew.metadata.mp4.Mp4HandlerFactory;

import java.awt.*;
import java.io.IOException;
import java.util.Arrays;

/**
 * ISO/IED 14496-12:2015 pg.29
 */
public class TrackHeaderBox extends FullBox
{
    long creationTime;
    long modificationTime;
    long trackID;
    long duration;
    int layer;
    int alternateGroup;
    int volume;
    int[] matrix = new int[9];
    long width;
    long height;

    public TrackHeaderBox(SequentialReader reader, Box box) throws IOException
    {
        super(reader, box);

        if (version == 1) {
            creationTime = reader.getInt64();
            modificationTime = reader.getInt64();
            trackID = reader.getInt32();
            reader.skip(4); // reserved
            duration = reader.getInt64();
        } else {
            creationTime = reader.getUInt32();
            modificationTime = reader.getUInt32();
            trackID = reader.getUInt32();
            reader.skip(4);
            duration = reader.getUInt32();
        }
        reader.skip(8); // reserved
        layer = reader.getInt16();
        alternateGroup = reader.getInt16();
        volume = reader.getInt16();
        reader.skip(2); // reserved
        for (int i = 0; i < 9; i++) {
            matrix[i] = reader.getInt32();
        }
        width = reader.getInt32();
        height = reader.getInt32();
    }

    public void addMetadata(Mp4Directory directory)
    {
        if (width != 0 && height != 0 && directory.getDoubleObject(Mp4Directory.TAG_ROTATION) == null) {
            Point p = new Point(matrix[1] + matrix[4], matrix[0] + matrix[3]);
            double theta = Math.atan2(p.y, p.x);
            double degree = Math.toDegrees(theta);
            degree -= 45;
            directory.setDouble(Mp4Directory.TAG_ROTATION, Math.abs(degree));
        }
    }
}

@@ -900,56 +900,8 @@
 ory;
-%0Aimport com.drew.metadata.mp4.Mp4HandlerFactory;
 %0A%0Aim
@@ -948,33 +948,8 @@
 ion;
-%0Aimport java.util.Arrays;
 %0A%0A/*
@@ -976,16 +976,19 @@
 2:20
-1
+0
 5 pg.
-29
+17-18
 %0A */

update class comment