⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pixel.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2005 by VizIR project.
 * Please contact Horst Eidenberger (hme@ims.tuwien.ac.at)
 * for more information.
 *
 * Mathias Lux, mathias@juggle.at, http://www.juggle.at
 */

package org.vizir.media;

/**
 * Description: A class representing a pixel with three (color) components.
 *
 * @author flo ledermann ledermann@ims.tuwien.ac.at
 * @version $Id: Pixel.java,v 1.2 2005/05/17 22:00:47 motte Exp $
 */
public class Pixel {

    /**
     * Internal storage of pixel data.
     */
    int[] data = {0, 0, 0};

    /**
     * No conversion.
     */
    public static final int NO_CONVERSION = 0;
    /**
     * RGB to YUV color space conversion.
     */
    public static final int RGB2YUV = 1;
    /**
     * YUV to RGB color space converison.
     */
    public static final int YUV2RGB = 2;

    /**
     * Create one Pixel object of one pixel represented as int value in RGB.
     *
     * @param data    pixel-data in RGB
     * @param convert Color space conversion to apply (RGB2YUV, YUV2RGB or NO_CONVERSION)
     * @param rMask   Masking bits for red component.
     * @param gMask   Masking bits for green component.
     * @param bMask   Masking bits for blue component.
     * @return
     */
    public static Pixel createFromInt(int data, int convert, int rMask, int gMask, int bMask) {
        int rShift = 0, gShift = 0, bShift = 0;
        if (rMask != 0) while ((rMask >> rShift) % 2 == 0) rShift++;
        if (gMask != 0) while ((gMask >> gShift) % 2 == 0) gShift++;
        if (bMask != 0) while ((bMask >> bShift) % 2 == 0) bShift++;

        Pixel p = new Pixel((data & rMask) >> rShift, (data & gMask) >> gShift, (data & bMask) >> bShift);
        if (convert == RGB2YUV)
            p.RGB2YUV();
        else if (convert == YUV2RGB) p.YUV2RGB();
        return p;
    }

    /**
     * Convert the internal data from RGB to YUV.
     */
    void RGB2YUV() {
        float y = 0.299f * data[0] + 0.587f * data[1] + 0.114f * data[2];
        data[1] = norm((data[2] - y) * 0.565f);
        data[2] = norm((data[0] - y) * 0.713f);
        data[0] = norm(y);
    }

    /**
     * Convert the internal data from YUV to RGB.
     */
    void YUV2RGB() {
        float y = data[0];
        float u = data[1];
        data[0] = norm(y + 1.403f * data[2]);
        data[1] = norm(y - 0.344f * u - 0.714f * data[2]);
        data[2] = norm(y + 1.770f * u);
    }

    /**
     * Helper method to cast & normalize the components to
     * integers in the range of 0..255
     *
     * @param num Float value to normalize.
     * @return Normalized value.
     */
    int norm(float num) {
        if (num < 0) return 0;
        if (num > 255) return 255;
        return (int) num;
    }

    /**
     * Constructor, takes three components.
     *
     * @param a R or Y component.
     * @param b G or U component.
     * @param c B or V component.
     */
    public Pixel(int a, int b, int c) {
        data[0] = a;
        data[1] = b;
        data[2] = c;
    }

    /**
     * Returns the i'th color component.
     *
     * @param i 0 = R/Y; 1 = G/U; 2 = B/V.
     * @return The desired color component.
     */
    public int getComponent(int i) {
        return data[i];
    }

    /**
     * Check for equality. A Pixel is equal to another pixel if all
     * three components are equal.
     *
     * @param obj Object to compare to.
     * @return true, if Pixels are equal.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Pixel) {
            if (((Pixel) obj).getComponent(0) == getComponent(0) &&
                    ((Pixel) obj).getComponent(1) == getComponent(1) &&
                    ((Pixel) obj).getComponent(2) == getComponent(2))
                return true;
        }
        return false;
    }

    /**
     * Returns a string representation of the Pixel.
     *
     * @return The three component values, concatenated by colons.
     */
    public String toString() {
        return data[0] + ":" + data[1] + ":" + data[2];
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -