00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00032 #include "operator/ConversionOperators.hpp"
00033 #include <iostream>
00034
00035 using namespace Millie;
00036
00037 void Millie::ModuleImageComplexToVisibleImage(Image & out, const ImageComplex& in)
00038 {
00039 if(out.getNumComponents() != in.getNumComponents())
00040 throw IllegalArgument("ModuleImageComplexToVisibleImage");
00041
00042 float max = 0;
00043
00044 out.resize(in.getWidth(), in.getHeight());
00045
00046 for(int canal = 0; canal< in.getNumComponents(); canal++)
00047 {
00048 for(int j = 0; j< in.getHeight(); j++)
00049 for(int i=0; i < in.getWidth(); i++)
00050 {
00051 std::complex<float> tempo = in.getPixel(i,j, canal);
00052 if(log(1+std::norm(tempo)) > max)
00053 max = log(1+std::norm(tempo));
00054 out.setPixel(i, j, canal, log(1+std::norm(tempo)));
00055 }
00056
00057 for(int j = 0; j< out.getHeight(); j++)
00058 for(int i=0; i < out.getWidth(); i++)
00059 {
00060 out.setPixel(i, j, canal, 255 / max * out.getPixel(i,j, canal));
00061 }
00062
00063 }
00064
00065
00066 }
00067
00068
00069
00070 void Millie::RGBToHSL(ImageHSL& out, const ImageRGB& in)
00071 {
00072 out.resize(in.getWidth(), in.getHeight());
00073
00074 for(int j=0; j<in.getHeight(); j++)
00075 for(int i = 0; i<in.getWidth(); i++)
00076 {
00077 float r = in.getRedPixel(i,j)/ 255.0f;
00078 float g = in.getGreenPixel(i,j)/ 255.0f;
00079 float b = in.getBluePixel(i,j)/ 255.0f;
00080 float max = r;
00081 float min = r;
00082 if(max<g)
00083 max = g;
00084 if(max<b)
00085 max = b;
00086 if(min>g)
00087 min = g;
00088 if(min>b)
00089 min = b;
00090 float h;
00091 float s;
00092 float l;
00093
00094 if(max==min)
00095 h = 0.0f;
00096 else if(max==r && g>=b)
00097 h = 60 * (g-b)/(max-min);
00098 else if(max==r && g<b)
00099 h = 60 * (g-b)/(max-min) + 360;
00100 else if(max==g)
00101 h = 60 * (b-r)/(max-min) + 120;
00102 else
00103 h = 60 * (r-g) /(max-min) +240;
00104
00105 l = 0.5 * (max+min);
00106
00107 if(l==0 || max==min)
00108 s = 0;
00109 else
00110 if(l>0 && l<= 0.5)
00111 s = (max-min)/(2*l);
00112 else
00113 s = (max - min)/(2-2*l);
00114
00115 out.setHuePixel(i,j,h);
00116 out.setSaturationPixel(i,j, s);
00117 out.setLightnessPixel(i,j, l);
00118
00119 }
00120 }
00121
00122 void Millie::HSLToRGB(ImageRGB& out, const ImageHSL& in)
00123 {
00124 out.resize(in.getWidth(), in.getHeight());
00125
00126 for(int j=0; j<in.getHeight(); j++)
00127 for(int i = 0; i<in.getWidth(); i++)
00128 {
00129
00130 float h = in.getHuePixel(i,j);
00131 float s = in.getSaturationPixel(i,j);
00132 float l = in.getLightnessPixel(i,j);
00133
00134 float r;
00135 float g;
00136 float b;
00137
00138 float q;
00139 if(l<0.5f)
00140 q = l * (1.0f+s);
00141 else
00142 q = l + s - (l*s);
00143
00144 float p = 2.0f * l -q;
00145 float hk = h/360.0f;
00146
00147 float tr = hk + 1.0f/3.0f;
00148 float tg = hk;
00149 float tb = hk - 1.0f/3.0f;
00150
00151 if(tr<0)
00152 tr++;
00153 if(tr>1)
00154 tr--;
00155
00156 if(tg<0)
00157 tg++;
00158 if(tg>1)
00159 tg--;
00160
00161 if(tb<0)
00162 tb++;
00163 if(tb>1)
00164 tb--;
00165
00166 if(tr< 1.0f/6.0f)
00167 r = p + ((q-p) * 6.0f * tr);
00168 else if(tr<0.5f)
00169 r = q;
00170 else if(tr<2.0f/3.0f)
00171 r = p + ((q-p) * (2.0f / 3.0f - tr) * 6.0f);
00172 else
00173 r = p;
00174
00175 if(tg< 1.0f/6.0f)
00176 g = p + ((q-p) * 6.0f * tg);
00177 else if(tg<0.5f)
00178 g = q;
00179 else if(tg<2.0f/3.0f)
00180 g = p + ((q-p) * (2.0f / 3.0f - tg) * 6.0f);
00181 else
00182 g = p;
00183
00184 if(tb< 1.0f/6.0f)
00185 b = p + ((q-p) * 6.0f * tb);
00186 else if(tb<0.5f)
00187 b = q;
00188 else if(tb<2.0f/3.0f)
00189 b = p + ((q-p) * (2.0f / 3.0f - tb) * 6.0f);
00190 else
00191 b = p;
00192
00193 out.setRedPixel(i,j,255 * r);
00194 out.setGreenPixel(i,j,255 * g);
00195 out.setBluePixel(i,j,255 * b);
00196
00197
00198
00199 }
00200 }
00201
00202 void Millie::RGBToYUV(ImageYUV& out, const ImageRGB& in)
00203 {
00204
00205 out.resize(in.getWidth(), in.getHeight());
00206
00207 for(int j=0; j<in.getHeight(); j++)
00208 for(int i = 0; i<in.getWidth(); i++)
00209 {
00210 float R = in.getRedPixel(i,j);
00211 float G = in.getGreenPixel(i,j);
00212 float B = in.getBluePixel(i,j);
00213
00214 float Y = 0.299 * R + 0.587 * G + 0.114 * B;
00215 float U = -0.14713*R - 0.2886*G + 0.436 * B;
00216 float V = 0.615 * R - 0.51499 * G - 0.10001 * B;
00217
00218 out.setYPixel(i,j, Y);
00219 out.setUPixel(i,j, U);
00220 out.setVPixel(i,j, V);
00221
00222 }
00223 }
00224
00225 void Millie::YUVToRGB(ImageRGB& out, const ImageYUV& in)
00226 {
00227
00228 out.resize(in.getWidth(), in.getHeight());
00229
00230 for(int j=0; j<in.getHeight(); j++)
00231 for(int i = 0; i<in.getWidth(); i++)
00232 {
00233 float Y = in.getYPixel(i,j);
00234 float U = in.getUPixel(i,j);
00235 float V = in.getVPixel(i,j);
00236
00237 float R = Y + 1.13983*V;
00238 float G = Y - 0.39466*U - 0.58060*V;
00239 float B = Y + 2.03211*U;
00240
00241 out.setRedPixel(i,j, R);
00242 out.setBluePixel(i,j, B);
00243 out.setGreenPixel(i,j, G);
00244
00245 }
00246 }
00247