// Path: src/main/java/bdsup2sub/bitmap/ColorSpaceUtils.java
// public static int[] RGB2YCbCr(int r, int g, int b, boolean useBT601) {
//     int[] yCbCr = new int[3];
//     double y,cb,cr;
// 
//     if (useBT601) {
//         /* BT.601 for RGB 0..255 (PC) -> YCbCr 16..235 */
//         y  =  r * 0.299 * 219 / 255    + g * 0.587 * 219 / 255    + b * 0.114 * 219 / 255;
//         cb = -r * 0.168736 * 224 / 255 - g * 0.331264 * 224 / 255 + b * 0.5 * 224 / 255;
//         cr =  r * 0.5 * 224 / 255      - g * 0.418688 * 224 / 255 - b * 0.081312 * 224 / 255;
//     } else {
//         /* BT.709 for RGB 0..255 (PC) -> YCbCr 16..235 */
//         y  =  r * 0.2126 * 219 / 255          + g * 0.7152 * 219 / 255          + b * 0.0722 * 219 / 255;
//         cb = -r * 0.2126 / 1.8556 * 224 / 255 - g * 0.7152 / 1.8556 * 224 / 255 + b * 0.5 * 224 / 255;
//         cr =  r * 0.5 * 224 / 255		      - g * 0.7152 / 1.5748 * 224 / 255 - b * 0.0722 / 1.5748 * 224 / 255;
//     }
//     yCbCr[0] =  16 + (int)(y  + 0.5);
//     yCbCr[1] = 128 + (int)(cb + 0.5);
//     yCbCr[2] = 128 + (int)(cr + 0.5);
//     for (int i = 0; i < 3; i++) {
//         if (yCbCr[i] < 16) {
//             yCbCr[i] = 16;
//         } else {
//             if (i == 0) {
//                 if (yCbCr[i] > 235)
//                     yCbCr[i] = 235;
//             } else {
//                 if (yCbCr[i] > 240)
//                     yCbCr[i] = 240;
//             }
//         }
//     }
//     return yCbCr;
// }


import java.awt.*;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.util.Arrays;
import static bdsup2sub.bitmap.ColorSpaceUtils.RGB2YCbCr;
/*
 * Copyright 2014 Volker Oth (0xdeadbeef) / Miklos Juhasz (mjuhasz)
 *
 * 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.
 */
package bdsup2sub.bitmap;



/**
 * Palette class for mixed representation of RGB/YCbCr palettes with alpha information.
 */
public class Palette {
    /** Number of palette entries */
    private final int size;
    /** Byte buffer for RED info */
    private final byte[] r;
    /** Byte buffer for GREEN info */
    private final byte[] g;
    /** Byte buffer for BLUE info */
    private final byte[] b;
    /** Byte buffer for alpha info */
    private final byte[] a;
    /** Byte buffer for Y (luminance) info */
    private final byte[] y;
    /** Byte buffer for Cb (chrominance blue) info */
    private final byte[] cb;
    /** Byte buffer for Cr (chrominance red) info */
    private final byte[] cr;
    /** Use BT.601 color model instead of BT.709 */
    private final boolean useBT601;

    /**
     * Initializes palette with transparent black (RGBA: 0x00000000)
     * @param size Number of palette entries
     */
    public Palette(int size, boolean useBT601) {
        this.size = size;
        this.useBT601 = useBT601;
        r  = new byte[size];
        g  = new byte[size];
        b  = new byte[size];
        a  = new byte[size];
        y  = new byte[size];
        cb = new byte[size];
        cr = new byte[size];

        // set at least all alpha values to invisible
// Path: src/main/java/bdsup2sub/bitmap/ColorSpaceUtils.java
// public static int[] RGB2YCbCr(int r, int g, int b, boolean useBT601) {
//     int[] yCbCr = new int[3];
//     double y,cb,cr;
// 
//     if (useBT601) {
//         /* BT.601 for RGB 0..255 (PC) -> YCbCr 16..235 */
//         y  =  r * 0.299 * 219 / 255    + g * 0.587 * 219 / 255    + b * 0.114 * 219 / 255;
//         cb = -r * 0.168736 * 224 / 255 - g * 0.331264 * 224 / 255 + b * 0.5 * 224 / 255;
//         cr =  r * 0.5 * 224 / 255      - g * 0.418688 * 224 / 255 - b * 0.081312 * 224 / 255;
//     } else {
//         /* BT.709 for RGB 0..255 (PC) -> YCbCr 16..235 */
//         y  =  r * 0.2126 * 219 / 255          + g * 0.7152 * 219 / 255          + b * 0.0722 * 219 / 255;
//         cb = -r * 0.2126 / 1.8556 * 224 / 255 - g * 0.7152 / 1.8556 * 224 / 255 + b * 0.5 * 224 / 255;
//         cr =  r * 0.5 * 224 / 255		      - g * 0.7152 / 1.5748 * 224 / 255 - b * 0.0722 / 1.5748 * 224 / 255;
//     }
//     yCbCr[0] =  16 + (int)(y  + 0.5);
//     yCbCr[1] = 128 + (int)(cb + 0.5);
//     yCbCr[2] = 128 + (int)(cr + 0.5);
//     for (int i = 0; i < 3; i++) {
//         if (yCbCr[i] < 16) {
//             yCbCr[i] = 16;
//         } else {
//             if (i == 0) {
//                 if (yCbCr[i] > 235)
//                     yCbCr[i] = 235;
//             } else {
//                 if (yCbCr[i] > 240)
//                     yCbCr[i] = 240;
//             }
//         }
//     }
//     return yCbCr;
// }


// Path: src/main/java/bdsup2sub/bitmap/Palette.java
import java.awt.*;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.util.Arrays;
import static bdsup2sub.bitmap.ColorSpaceUtils.RGB2YCbCr;

/*
 * Copyright 2014 Volker Oth (0xdeadbeef) / Miklos Juhasz (mjuhasz)
 *
 * 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.
 */
package bdsup2sub.bitmap;



/**
 * Palette class for mixed representation of RGB/YCbCr palettes with alpha information.
 */
public class Palette {
    /** Number of palette entries */
    private final int size;
    /** Byte buffer for RED info */
    private final byte[] r;
    /** Byte buffer for GREEN info */
    private final byte[] g;
    /** Byte buffer for BLUE info */
    private final byte[] b;
    /** Byte buffer for alpha info */
    private final byte[] a;
    /** Byte buffer for Y (luminance) info */
    private final byte[] y;
    /** Byte buffer for Cb (chrominance blue) info */
    private final byte[] cb;
    /** Byte buffer for Cr (chrominance red) info */
    private final byte[] cr;
    /** Use BT.601 color model instead of BT.709 */
    private final boolean useBT601;

    /**
     * Initializes palette with transparent black (RGBA: 0x00000000)
     * @param size Number of palette entries
     */
    public Palette(int size, boolean useBT601) {
        this.size = size;
        this.useBT601 = useBT601;
        r  = new byte[size];
        g  = new byte[size];
        b  = new byte[size];
        a  = new byte[size];
        y  = new byte[size];
        cb = new byte[size];
        cr = new byte[size];

        // set at least all alpha values to invisible
        int[] yCbCr = RGB2YCbCr(0, 0, 0, useBT601);