VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
RecolorShader.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9 
10 namespace visLU.Effects
11 {
16  class RecolorShader: Effect
17  {
18  GraphicsDevice device;
19  Effect effect;
20 
21  #region EffectParameters
22 
23  // convert from RGB to L*a*b*
24  private EffectParameter ep_rotationMatrix;
25  private EffectParameter ep_rgbTexture;
26  private EffectParameter ep_RGBtoXYZmatrix;
27  //private EffectParameter ep_whiteRef;
28 
29  // PCA1
30  private EffectParameter ep_labTexture; // also for LMS
31  private EffectParameter ep_noiseTexture;
32 
33  // PCA2 and PCA4 reduction
34  private EffectParameter ep_reductionTexture;
35  private EffectParameter ep_viewport; //for PCA4
36 
37  // EigenVectors
38  private EffectParameter ep_pcaTexture;
39 
40  // Project onto LMS plane
41  private EffectParameter ep_eigenTexture;
42  //and reuses ep_labTexture
43 
44  // ResultColor
45  private EffectParameter ep_projSumTexture;
46  private EffectParameter ep_projTexture;
47 
48  // convert from L*a*b* back to RGB
49  // reuses ep_rotationMatrix;
50  private EffectParameter ep_resCTexture;
51  private EffectParameter ep_XYZtoRGBmatrix;
52 
53  #endregion
54 
55  #region member variables
56 
57  // convert from RGB to L*a*b*
58  private Matrix rotation;
59  private Texture2D rgbtex;
60  private Matrix rgbToXYZ;
61 
62  // PCA1
63  private Texture2D labTexture; // also for LMS
64  private Texture2D noiseTexture;
65 
66  // PCA2 and PCA4 reduction
67  private Texture2D reductionTexture; //does not have get and set methods
68 
69  // EigenVectors
70  private Texture2D pcaTexture;
71 
72  // Project onto LMS plane
73  private Texture2D eigenTexture;
74  // and reuses labTexture
75 
76  // ResultColor
77  private Texture2D projSumTexture;
78  private Texture2D projTexture;
79  // and reuses labTexture
80 
81  // convert from RGB to L*a*b*
82  // reuses rotation;
83  private Texture2D resultCTex;
84  private Matrix xyzToRGB;
85 
86 
87  #endregion
88 
89  #region properties
90 
91  public Matrix RotationMatrix
92  {
93  get { return rotation; }
94  set { rotation = value; }
95  }
96 
97  public Texture2D RGBTexture
98  {
99  get { return rgbtex; }
100  set { rgbtex = value; }
101  }
102 
103  public Matrix RGBtoXYZ
104  {
105  get { return rgbToXYZ; }
106  set { rgbToXYZ = value; }
107  }
108 
109  /*public Vector3 WhiteReference
110  {
111  get { return whiteReference; }
112  set { whiteReference = value; }
113  }*/
114 
115  public Texture2D LabTexture
116  {
117  get { return labTexture; }
118  set { labTexture = value; }
119  }
120 
121  public Texture2D NoiseTexture
122  {
123  get { return noiseTexture; }
124  set { noiseTexture = value; }
125  }
126 
127  public Texture2D PCATexture
128  {
129  get { return pcaTexture; }
130  set { pcaTexture = value; }
131  }
132 
133  public Texture2D EigenTexture
134  {
135  get { return eigenTexture; }
136  set { eigenTexture = value; }
137  }
138 
139  public Texture2D EnhancedContrast_tex
140  {
141  get { return resultCTex; }
142  set { resultCTex = value; }
143  }
144  #endregion
145 
146 
153  public RecolorShader(GraphicsDevice _device, Effect _effect, string step)
154  : base(_effect)
155  {
156  Console.WriteLine("creating recolorshader");
157  device = _device;
158  effect = _effect;
159 
160  //if(step == "convertToLab")
161 
162  switch (step)
163  {
164  #region LMS
165 
166  case "LMS":
167  ep_labTexture = this.Parameters["labTexture"];
168  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
169  break;
170 
171  #endregion
172 
173  #region convertToLab
174 
175  case "convertToLab":
176  //Adobe RGB (1998) D50:
177  rgbToXYZ = new Matrix(0.6097559f, 0.2052401f, 0.1492240f, 0.0f,
178  0.3111242f, 0.6256560f, 0.0632197f, 0.0f,
179  0.0194811f, 0.0608902f, 0.7448387f, 0.0f,
180  0.0f, 0.0f, 0.0f, 1.0f);
181 
182  #region delete
183 
184  /*xyzToRGB = new Matrix(1.9624274f, - 0.6105343f, - 0.3413404f, 0.0f,
185  -0.9787684f, 1.9161415f, 0.0334540f, 0.0f,
186  0.0286869f, -0.1406752f, 1.3487655f, 0.0f,
187  0.0f, 0.0f, 0.0f, 1.0f);
188 
189  //almost:
190  rgbToXYZ = new Matrix(0.41242400f, 0.21265600f, 0.01933240f, 0.0f,
191  0.35757900f, 0.71515800f, 0.11919300f, 0.0f,
192  0.18046400f, 0.07218560f, 0.95044400f, 0.0f,
193  0.0f, 0.0f, 0.0f, 1.0f);
194 
195  xyzToRGB = new Matrix(3.24070846f, -0.96925735f, 0.05563507f, 0.0f,
196  -1.53725917f, 1.87599516f, -0.20399580f, 0.0f,
197  -0.49857039f, 0.04155555f, 1.05706957f, 0.0f,
198  0.0f, 0.0f, 0.0f, 1.0f);
199 
200  //Lindbloom D50:
201  rgbToXYZ = new Matrix(0.4360747f, 0.3850649f, 0.1430804f, 0.0f,
202  0.2225045f, 0.7168786f, 0.0606169f, 0.0f,
203  0.0139322f, 0.0971045f, 0.7141733f, 0.0f,
204  0.0f, 0.0f, 0.0f, 1.0f);
205 
206  xyzToRGB = new Matrix(3.1338561f, - 1.6168667f, - 0.4906146f, 0.0f,
207  -0.9787684f, 1.9161415f, 0.0334540f, 0.0f,
208  0.0719453f, -0.2289914f, 1.4052427f, 0.0f,
209  0.0f, 0.0f, 0.0f, 1.0f);
210 
211  //Lindbloom D65:
212  rgbToXYZ = new Matrix(0.4124564f, 0.3575761f, 0.1804375f, 0.0f,
213  0.2126729f, 0.7151522f, 0.0721750f, 0.0f,
214  0.0193339f, 0.1191920f, 0.9503041f, 0.0f,
215  0.0f, 0.0f, 0.0f, 1.0f);
216 
217  xyzToRGB = new Matrix(3.2404542f, -1.5371385f, -0.4985314f, 0.0f,
218  -0.9692660f, 1.8760108f, 0.0415560f, 0.0f,
219  0.0556434f, -0.2040259f, 1.0572252f, 0.0f,
220  0.0f, 0.0f, 0.0f, 1.0f);*/
221 
222  #endregion
223 
224  ep_rotationMatrix = this.Parameters["rotmat"];
225  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
226  ep_rgbTexture = this.Parameters["RGBtexture"];
227  ep_RGBtoXYZmatrix = this.Parameters["mRGBtoXYZ"];
228  break;
229 
230  #endregion
231 
232  #region ContrastLoss
233 
234  case "ContrastLoss":
235  ep_labTexture = this.Parameters["labTexture"];
236  ep_noiseTexture = this.Parameters["noiseTexture"];
237  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
238  break;
239 
240  #endregion
241 
242  #region PredominantComponent
243 
244  case "PredominantComponent":
245  ep_reductionTexture = this.Parameters["redTexture"];
246  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
247  break;
248 
249  #endregion
250 
251  #region EigenVector
252 
253  case "EigenVector":
254  ep_pcaTexture = this.Parameters["pcaTexture"];
255  break;
256 
257  #endregion
258 
259  #region Project
260 
261  case "Project":
262  ep_labTexture = this.Parameters["labTexture"];
263  ep_eigenTexture = this.Parameters["eigenTexture"];
264  break;
265 
266  #endregion
267 
268  #region MaxProjectionEffect
269 
270  case "MaxProjectionEffect":
271  ep_reductionTexture = this.Parameters["redTexture"];
272  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
273  break;
274 
275  #endregion
276 
277  #region EnhanceContrast
278 
279  case "EnhanceContrast":
280  ep_projSumTexture = this.Parameters["projEffectSumTexture"];
281  ep_projTexture = this.Parameters["projEffectTexture"];
282  ep_labTexture = this.Parameters["labTexture"];
283  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
284  break;
285 
286  #endregion
287 
288  #region convertToRGB
289 
290  case "convertToRGB":
291 
292  //Adobe RGB (1998) D50:
293  xyzToRGB = new Matrix(1.9624274f, -0.6105343f, -0.3413404f, 0.0f,
294  -0.9787684f, 1.9161415f, 0.0334540f, 0.0f,
295  0.0286869f, -0.1406752f, 1.3487655f, 0.0f,
296  0.0f, 0.0f, 0.0f, 1.0f);
297 
298  #region delete
299 
300  //lindbloom D50:
301  /*xyzToRGB = new Matrix(3.1338561f, -1.6168667f, -0.4906146f, 0.0f,
302  -0.9787684f, 1.9161415f, 0.0334540f, 0.0f,
303  0.0719453f, -0.2289914f, 1.4052427f, 0.0f,
304  0.0f, 0.0f, 0.0f, 1.0f);*/
305 
306  //Lindbloom D65:
307  /*xyzToRGB = new Matrix(3.2404542f, -1.5371385f, -0.4985314f, 0.0f,
308  -0.9692660f, 1.8760108f, 0.0415560f, 0.0f,
309  0.0556434f, -0.2040259f, 1.0572252f, 0.0f,
310  0.0f, 0.0f, 0.0f, 1.0f);
311 
312  //almost
313  xyzToRGB = new Matrix(3.24070846f, -0.96925735f, 0.05563507f, 0.0f,
314  -1.53725917f, 1.87599516f, -0.20399580f, 0.0f,
315  -0.49857039f, 0.04155555f, 1.05706957f, 0.0f,
316  0.0f, 0.0f, 0.0f, 1.0f);*/
317 
318  #endregion
319 
320  ep_rotationMatrix = this.Parameters["rotmat"];
321  ep_viewport = this.Parameters["viewport"]; // needed for shader model 3.0
322  ep_resCTexture = this.Parameters["ResCtexture"];
323  ep_XYZtoRGBmatrix = this.Parameters["mXYZtoRGB"];
324  break;
325 
326  #endregion
327  }
328 
329 
330  Console.WriteLine("RECOLORSHADER CREATED");
331  }
332 
333  #region SetEffectParameter
334 
335  #region LMS
336  public void SetEffectParameter_LMS(Texture2D _labTexture)
337  {
338  labTexture = _labTexture;
339 
340  this.ep_labTexture.SetValue(_labTexture);
341  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
342 
343  Console.WriteLine("effect parameters for lms shader set");
344  }
345  #endregion
346 
347  #region convert RGB to L*a*b*
348  public void SetEffectParameter_RGBtoLab()
352  {
353  this.ep_rotationMatrix.SetValue(rotation);
354  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
355  this.ep_rgbTexture.SetValue(rgbtex);
356  this.ep_RGBtoXYZmatrix.SetValue(rgbToXYZ);
357  //this.ep_whiteRef.SetValue(whiteReference);
358  Console.WriteLine("effect parameters for rgb to lab shader set");
359  }
360  #endregion
361 
362  #region PCA1
363  public void SetEffectParameter_ContrastLoss(Texture2D _labTexture, Texture2D _noiseTexture)
364  {
365  labTexture = _labTexture;
366  noiseTexture = _noiseTexture;
367 
368  this.ep_labTexture.SetValue(_labTexture);
369  this.ep_noiseTexture.SetValue(_noiseTexture);
370  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
371  }
372 
373  public void SetEffectParameter_PCA1()
374  {
375  this.ep_labTexture.SetValue(labTexture);
376  this.ep_noiseTexture.SetValue(noiseTexture);
377  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
378  }
379  #endregion
380 
381  #region PCA2
382  public void SetEffectParameter_PredominantComponenet(Texture2D _reductionTexture/*, Vector2 _dimension*/)
383  {
384  reductionTexture = _reductionTexture;
385  //dimension = _dimension;
386 
387  this.ep_reductionTexture.SetValue(reductionTexture);
388  //this.ep_dimension.SetValue(dimension);
389  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
390  }
391  #endregion
392 
393  #region EigenVectors
394  public void SetEffectParameter_EigenVectors(Texture2D _pcaTexture)
395  {
396  pcaTexture = _pcaTexture;
397  this.ep_pcaTexture.SetValue(pcaTexture);
398  }
399 
400  public void SetEffectParameter_EigenVec()
401  {
402  this.ep_pcaTexture.SetValue(pcaTexture);
403  }
404  #endregion
405 
406  #region Project onto LMS plane
407  public void SetEffectParameter_Projection(Texture2D _labTexture, Texture2D _eigenTexture)
408  {
409  labTexture = _labTexture;
410  eigenTexture = _eigenTexture;
411 
412  this.ep_labTexture.SetValue(labTexture);
413  this.ep_eigenTexture.SetValue(eigenTexture);
414  }
415  #endregion
416 
417  #region PCA4
418  public void SetEffectParameter_MaxProjectionEffect(Texture2D _reductionTexture/*, Vector2 _dimension*/)
419  {
420  reductionTexture = _reductionTexture;
421  //dimension = _dimension;
422 
423  this.ep_reductionTexture.SetValue(reductionTexture);
424  //this.ep_dimension.SetValue(dimension);
425  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
426  }
427  #endregion
428 
429  #region result color
430  public void SetEffectParameter_EnhanceContrast(Texture2D _projSumImage, Texture2D _projectedImage, Texture2D _labImage)
431  {
432  projSumTexture = _projSumImage;
433  projTexture = _projectedImage;
434  labTexture = _labImage;
435 
436  this.ep_projSumTexture.SetValue(projSumTexture);
437  this.ep_projTexture.SetValue(projTexture);
438  this.ep_labTexture.SetValue(labTexture);
439  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
440  }
441  #endregion
442 
443  #region convert L*a*b* back to RGB
444  internal void SetEffectParameter_labToRGB()
445  {
446  this.ep_rotationMatrix.SetValue(rotation);
447  this.ep_viewport.SetValue(new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height));
448  this.ep_resCTexture.SetValue(resultCTex);
449  this.ep_XYZtoRGBmatrix.SetValue(xyzToRGB);
450 
451  Console.WriteLine("effect parameters for lab to rgb shader set");
452  }
453  #endregion
454 
455  #endregion
456 
457 
458 
459 
460  }
461 }