1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
12 #include "colortools.h"
14 ColorTools::ColorTools()
20 QImage ColorTools::yuvColorWheel(const QSize &size, const unsigned char &Y, const float &scaling, const bool &modifiedVersion, const bool &circleOnly)
22 QImage wheel(size, QImage::Format_ARGB32);
23 if (size.width() == 0 || size.height() == 0) {
24 qCritical("ERROR: Size of the color wheel must not be 0!");
27 wheel.fill(qRgba(0,0,0,0));
29 double dr, dg, db, du, dv, dmax;
31 const int w = size.width();
32 const int h = size.height();
33 const float w2 = (float)w/2;
34 const float h2 = (float)h/2;
36 for (int u = 0; u < w; u++) {
37 // Transform u from {0,...,w} to [-1,1]
38 du = (double) 2*u/(w-1) - 1;
41 for (int v = 0; v < h; v++) {
42 dv = (double) 2*v/(h-1) - 1;
46 // Ellipsis equation: x²/a² + y²/b² = 1
47 // Here: x=ru, y=rv, a=w/2, b=h/2, 1=rr
48 // For rr > 1, the point lies outside. Don't draw it.
51 rr = ru*ru/(w2*w2) + rv*rv/(h2*h2);
57 // Calculate the RGB values from YUV
59 dg = Y - 100.6*du - 148*dv;
62 if (modifiedVersion) {
63 // Scale the RGB values down, or up, to max 255
65 if (fabs(dg) > dmax) dmax = fabs(dg);
66 if (fabs(db) > dmax) dmax = fabs(db);
74 // Avoid overflows (which would generate intersting patterns).
75 // Note that not all possible (y,u,v) values with u,v \in [-1,1]
76 // have a correct RGB representation, therefore some RGB values
77 // may exceed {0,...,255}.
81 if (dr > 255) dr = 255;
82 if (dg > 255) dg = 255;
83 if (db > 255) db = 255;
85 wheel.setPixel(u, (h-v-1), qRgba(dr, dg, db, 255));
89 emit signalWheelCalculationFinished();
93 QImage ColorTools::yuvVerticalPlane(const QSize &size, const float &angle, const float &scaling)
95 QImage plane(size, QImage::Format_ARGB32);
96 if (size.width() == 0 || size.height() == 0) {
97 qCritical("ERROR: Size of the color plane must not be 0!");
101 double dr, dg, db, du, dv, Y;
102 const int w = size.width();
103 const int h = size.height();
104 const double uscaling = scaling*cos(M_PI*angle/180);
105 const double vscaling = scaling*sin(M_PI*angle/180);
107 for (int uv = 0; uv < w; uv++) {
108 du = uscaling*((double)2*uv/w - 1);//(double)?
109 dv = vscaling*((double)2*uv/w - 1);
111 for (int y = 0; y < h; y++) {
114 // See yuv2rgb, yuvColorWheel
116 dg = Y - 100.6*du - 148*dv;
121 if (dr > 255) dr = 255;
122 if (dg > 255) dg = 255;
123 if (db > 255) db = 255;
125 plane.setPixel(uv, (h-y-1), qRgba(dr, dg, db, 255));
135 QImage ColorTools::rgbCurvePlane(const QSize &size, const ColorsRGB &color)
137 QImage plane(size, QImage::Format_ARGB32);
138 if (size.width() == 0 || size.height() == 0) {
139 qCritical("ERROR: Size of the color plane must not be 0!");
143 const int w = size.width();
144 const int h = size.height();
148 for (int x = 0; x < w; x++) {
149 dval = (double)255*x/w;
151 for (int y = 0; y < h; y++) {
152 dcol = (double)255*y/h;
154 if (color == ColorTools::COL_R) {
155 plane.setPixel(x, (h-y-1), qRgb(dcol, dval, dval));
156 } else if (color == ColorTools::COL_G) {
157 plane.setPixel(x, (h-y-1), qRgb(dval, dcol, dval));
159 plane.setPixel(x, (h-y-1), qRgb(dval, dval, dcol));