Package com.google.code.appengine.imageio.plugins.jpeg

Source Code of com.google.code.appengine.imageio.plugins.jpeg.JPEGQTable

/*
*  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You 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.
*/
/**
* @author Rustem V. Rafikov
*/
package com.google.code.appengine.imageio.plugins.jpeg;


import org.apache.harmony.x.imageio.internal.nls.Messages;

import com.google.code.appengine.imageio.plugins.jpeg.JPEGQTable;

public class JPEGQTable {

    private final static int SIZE = 64;
    private final static int BASELINE_MAX = 255;
    private final static int MAX = 32767;


    private int[] theTable;

    /*
     * K1 & K2 tables can be found in the JPEG format specification
     * at http://www.w3.org/Graphics/JPEG/itu-t81.pdf
     */

    private static final int[] K1LumTable = new int[] {
        1611101624405161,
        1212141926586055,
        1413162440576956,
        1417222951878062,
        1822375668109, 103, 77,
        2435556481104, 113, 92,
        49647887103, 121, 120, 101,
        72929598112, 100, 103, 99
    };

    private static final int[] K2ChrTable = new int[] {
        1718244799999999,
        1821266699999999,
        2426569999999999,
        4766999999999999,
        9999999999999999,
        9999999999999999,
        9999999999999999,
        9999999999999999
    };

    public static final JPEGQTable K1Luminance = new JPEGQTable(K1LumTable);
    public static final JPEGQTable K1Div2Luminance = K1Luminance.getScaledInstance(0.5f, true);
    public static final JPEGQTable K2Chrominance = new JPEGQTable(K2ChrTable);
    public static final JPEGQTable K2Div2Chrominance = K2Chrominance.getScaledInstance(0.5f, true);;


    public JPEGQTable(int[] table) {
        if (table == null) {
            throw new IllegalArgumentException(Messages.getString("imageio.42"));
        }
        if (table.length != SIZE) {
                throw new IllegalArgumentException(Messages.getString("imageio.93", table.length));
        }
        theTable = table.clone();
    }

    public int[] getTable() {
        return theTable.clone();
    }

    public JPEGQTable getScaledInstance(float scaleFactor, boolean forceBaseline) {
        int table[] = new int[SIZE];

        int maxValue = forceBaseline ? BASELINE_MAX : MAX;

        for (int i = 0; i < theTable.length; i++) {
            int rounded = Math.round(theTable[i] * scaleFactor);
            if (rounded < 1) {
                rounded = 1;
            }
            if (rounded > maxValue) {
                rounded = maxValue;
            }
            table[i] = rounded;
        }
        return new JPEGQTable(table);
    }

    @Override
    public String toString() {
      StringBuilder sb = new StringBuilder();
     
      sb.append("JPEGQTable:\n");
      for (int i = 0; i < 8; i++) {
        sb.append('\t');
        for (int j = 0; j < 8; j++) {
          sb.append(theTable[i*8+j]);
          sb.append(' ');
        }
        sb.append('\n');
      }
     
        return sb.toString();
    }
}
TOP

Related Classes of com.google.code.appengine.imageio.plugins.jpeg.JPEGQTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.