SilentEye 0.4.1

modules/seformatjpegold/seformatjpeg/ycbcr.cpp

Go to the documentation of this file.
00001 //  This file is part of SilentEye.
00002 //
00003 //  SilentEye is free software: you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation, either version 3 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  SilentEye is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with SilentEye.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 #include "ycbcr.h"
00017 
00018 #include "math.h"
00019 
00020 namespace SEFormatJPEG {
00021 
00022     YCbCr::YCbCr(const float y, const float cb, const float cr, QObject* parent) : QObject(parent)
00023     {
00024         m_y = y;
00025         m_cb = cb;
00026         m_cr = cr;
00027     }
00028 
00029     YCbCr::YCbCr(const QRgb& rgb, QObject* parent) : QObject(parent)
00030     {        
00031         int r = qRed(rgb);
00032         int g = qGreen(rgb);
00033         int b = qBlue(rgb);
00034 
00035         m_y = (kr * r) + ((1 - kr - kb) * g) + (kb * b);
00036         m_cb = 128 - (0.1687 * r) - (0.3313 * g) + (0.5 * b);
00037         m_cr = 128 + (0.5 * r) - (0.4187 * g) - (0.0813 * b);
00038     }
00039 
00040     YCbCr::~YCbCr()
00041     {
00042         // NOTHING TO DO
00043     }
00044 
00045     float YCbCr::y() const
00046     {
00047         return m_y;
00048     }
00049     
00050     void YCbCr::setY(float value)
00051     {
00052         m_y = value;
00053     }
00054 
00055     float YCbCr::cb() const
00056     {
00057         return m_cb;
00058     }
00059 
00060     void YCbCr::setCb(float value)
00061     {
00062         m_cb = value;
00063     }
00064     
00065     float YCbCr::cr() const
00066     {
00067         return m_cr;
00068     }
00069 
00070     void YCbCr::setCr(float value)
00071     {
00072         m_cr = value;
00073     }
00074     
00075     QRgb YCbCr::toRGB()
00076     {
00077         int red = round(m_y + 1.402 * (m_cr - 128));
00078         int green = round(m_y - (0.34414 * (m_cb - 128)) - (0.71414 * (m_cr-128)));
00079         int blue = round(m_y + (1.772 * (m_cb - 128)));
00080 
00081         /*int red = (int)(2 * (1 - kr) * m_cr + m_y);
00082         int green = (int)(m_y - 2 * ((1 - kr) * kr * m_cr + (1 - kb) * kb * m_cb) / (1 - kr - kb));
00083         int blue = (int)(2 * (1 - kb) * m_cb + m_y);*/
00084 
00085         // After the compacting process some computed value could be invalid.
00086         if (red > 255) red = 255;
00087         else if (red < 0) red = 0;
00088         if (green > 255) green = 255;
00089         else if (green < 0) green = 0;
00090         if (blue > 255) blue = 255;
00091         else if (blue < 0) blue = 0;
00092 
00093         return qRgb(red, green, blue);
00094     }
00095 
00096     QString YCbCr::toString()
00097     {
00098         QString str = "Y:" + QString::number(m_y) + ",";
00099         str += "Cb:" + QString::number(m_cb) + ",";
00100         str += "Cr:" + QString::number(m_cr);
00101         return str;
00102     }
00103 
00104     bool YCbCr::operator==(const YCbCr& color)
00105     {
00106         return round(color.y()) == round(m_y);
00107     }
00108 
00109 }