VIS2 SS2013 CVD DVR
 All Classes Namespaces Functions Enumerations Properties
Image.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.Graphics;
7 using visLU.Effects;
8 using visLU.CVD;
9 using System.IO;
10 using Microsoft.Xna.Framework.Graphics.PackedVector;
11 using Microsoft.Xna.Framework.Content;
12 using Microsoft.Xna.Framework.GamerServices;
13 
14 namespace visLU.CVD
15 {
16  class Image
17  {
18  #region variables
19 
20  private GraphicsDevice device;
21  SpriteBatch spriteBatch;
22 
23  private Matrix rotationFromLMS;
24  private Matrix rotationToLMS;
25  private float angle;
26  private string colorVisionDeficency;
27  private int width;
28  private int height;
29 
30  private RenderTarget2D target_lms;
31 
32  private RenderTarget2D target_rgbToLab;
33  private RenderTarget2D target_contrastLoss;
34  private RenderTarget2D target_predominantComponent;
35  private RenderTarget2D target_eigenVector;
36  private RenderTarget2D target_projection;
37  private RenderTarget2D target_maxProjectionEffect;
38  private RenderTarget2D target_enhanceContrast;
39  private RenderTarget2D target_labToRGB;
40 
41  private RecolorShader shader_lms;
42 
43  private RecolorShader shader_rgbToLab;
44  private RecolorShader shader_contrastLoss;
45  private RecolorShader shader_predominantComponent;
46  private RecolorShader shader_eigenVector;
47  private RecolorShader shader_projection;
48  private RecolorShader shader_maxProjectionEffect;
49  private RecolorShader shader_enhanceContrast;
50  private RecolorShader shader_labToRGB;
51 
52  private Texture2D toSimulate;
53  private Texture2D referenceImageRGB; //rendered image
54 
55  private Texture2D noiseTexture; //contains neighbourhood information for contrast loss shader
56 
57  private Texture2D tex_lab;
58  private Texture2D tex_contrastLost;
59  private Texture2D tex_predominantComponent;
60  private Texture2D tex_eigenVectors;
61  private Texture2D tex_projected;
62  private Texture2D tex_maxProjectedEffect;
63  private Texture2D tex_enhancedContrast;
64 
65  private Texture2D recoloredImage;
66 
67  #region debug stuff
68  bool saveDebugTextures = false;
69  bool debugOutput = false; // console output
70  #endregion
71 
72  #endregion
73 
74  #region properties
75 
76  public Matrix RotationFromLMS
77  {
78  set { rotationFromLMS = value; }
79  get { return rotationFromLMS; }
80 
81  }
82  public Matrix RotationToLMS
83  {
84  set { rotationToLMS = value; }
85  get { return rotationToLMS; }
86 
87  }
88 
89  public Texture2D ImageToSimulate
90  {
91  set {
92  toSimulate = value;
93  width = toSimulate.Width;
94  height = toSimulate.Height;
95  }
96  get { return toSimulate; }
97  }
98 
102  public Texture2D RecoloredImage
103  {
104  set { recoloredImage = value; }
105  get { return recoloredImage; }
106  }
107 
108  public string ColorVisionDeficency
109  {
110  set { colorVisionDeficency = value; SetAngle(); SetMatrix(); }
111  get { return colorVisionDeficency; }
112  }
113 
114  public Texture2D ReferenceImageRGB
115  {
116  set {
117  referenceImageRGB = value;
118  //recoloredImage = referenceImageRGB; // TODO: only for debug!
119  width = referenceImageRGB.Width;
120  height = referenceImageRGB.Height;
121  //ExtractColors();
122  }
123  get { return referenceImageRGB; }
124  }
125 
126  public Texture2D LabImage
127  {
128  set { tex_lab = value; }
129  get { return tex_lab; }
130  }
131 
132  public Texture2D NoiseTexture
133  {
134  set { noiseTexture = value; Console.WriteLine("noise texture initialized"); }
135  get { return noiseTexture; }
136  }
137 
138  public RecolorShader LMSShader
139  {
140  set { shader_lms = value; }
141  get { return shader_lms; }
142  }
143 
144  public RecolorShader RGBtoLab_Shader
145  {
146  set { shader_rgbToLab = value; }
147  get { return shader_rgbToLab; }
148  }
149 
150  public RecolorShader ContrastLoss_Shader
151  {
152  set { shader_contrastLoss = value; }
153  get { return shader_contrastLoss; }
154  }
155 
156  public RecolorShader PredominantComponent_Shader
157  {
158  set { shader_predominantComponent = value; }
159  get { return shader_predominantComponent; }
160  }
161 
162  public RecolorShader EigenVector_Shader
163  {
164  set { shader_eigenVector = value; }
165  get { return shader_eigenVector; }
166  }
167 
168  public RecolorShader Projection_Shader
169  {
170  set { shader_projection = value; }
171  get { return shader_projection; }
172  }
173 
174  public RecolorShader MaxProjectionEffect_Shader
175  {
176  set { shader_maxProjectionEffect = value; }
177  get { return shader_predominantComponent; }
178  }
179 
180  public RecolorShader EnhanceContrast_Shader
181  {
182  set { shader_enhanceContrast = value; }
183  get { return shader_enhanceContrast; }
184  }
185 
186  public RecolorShader LabToRGB_Shader
187  {
188  set { shader_labToRGB = value; }
189  get { return shader_labToRGB; }
190  }
191 
192  #endregion
193 
194  #region constructor
195 
196  public Image(GraphicsDevice _device, string _colorVisionDeficency)
197  {
198  if (!saveDebugTextures)
199  {
200  saveDebugTextures = GameProperties.Instance.shaderDebugMode;
201  }
202 
203  device = _device;
204 
205  this.colorVisionDeficency = _colorVisionDeficency;
206  SetAngle();
207  SetMatrix();
208 
209  #region create render targets
210  PresentationParameters pp = device.PresentationParameters;
211  //SurfaceFormat format = pp.BackBufferFormat;
212  SurfaceFormat format = SurfaceFormat.Rgba64;
213  int bbwidth = pp.BackBufferWidth;
214  int bbheight = pp.BackBufferHeight;
215  DepthFormat depthStecilFormat = pp.DepthStencilFormat;
216  int multiSample = pp.MultiSampleCount;
217 
218  target_lms = new RenderTarget2D(device, bbwidth, bbheight, false, format,
219  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
220 
221 
222  target_rgbToLab = new RenderTarget2D(device, bbwidth, bbheight, false, format,
223  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
224 
225  target_contrastLoss = new RenderTarget2D(device, bbwidth, bbheight, false, format,
226  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
227 
228  target_predominantComponent = new RenderTarget2D(device, bbwidth, bbheight, false, format,
229  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
230 
231  target_maxProjectionEffect = new RenderTarget2D(device, bbwidth, bbheight, false, format,
232  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
233 
234  target_eigenVector = new RenderTarget2D(device, bbwidth, bbheight, false, format,
235  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
236 
237  target_projection = new RenderTarget2D(device, bbwidth, bbheight, false, format,
238  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
239 
240  target_enhanceContrast = new RenderTarget2D(device, bbwidth, bbheight, false, format,
241  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
242 
243  target_labToRGB = new RenderTarget2D(device, bbwidth, bbheight, false, format,
244  depthStecilFormat, multiSample, RenderTargetUsage.DiscardContents);
245  #endregion
246 
247  #region create textures
248  //referenceImageRGB = new Texture2D(device, width, height, true, SurfaceFormat.HdrBlendable); //rendered image
249  //labImage = new Texture2D(device, width, height, true, SurfaceFormat.HdrBlendable);
250  //noiseTexture = new Texture2D(device, width, height, true, SurfaceFormat.Rgba64);
251  //pcaImage;
252  //pingPongTex;
253  //eigenImage;
254  //projectedImage;
255  //projSumImage;
256  //resultColorImage = new Texture2D(device, width, height, true, SurfaceFormat.HdrBlendable);
257  //recoloredImage = new Texture2D(device, width, height, true, SurfaceFormat.HdrBlendable);
258 
259 
260  #endregion
261 
262  if (debugOutput)
263  {
264  Console.WriteLine("Image class instanciated");
265  }
266  }
267 
268  #endregion
269 
270  #region methods
271 
272  #region initialization stuff
273 
274  #region set angle
275  private void SetAngle()
276  {
277  switch (colorVisionDeficency)
278  {
279  case "protanopy":
280  angle = -11.47f; //-11.4783f;
281  break;
282 
283  case "deuteranopy":
284  angle = -8.1f; //-8.10959f;
285  break;
286 
287  case "tritanopy":
288  angle = 46.37f;
289  break;
290 
291  default: //Deuteranopy
292  angle = -8.1f;
293  break;
294  }
295 
296  if (debugOutput)
297  {
298  Console.WriteLine("angle set to " + angle);
299  }
300  }
301  #endregion
302 
303  #region set matrix
304  private void SetMatrix()
305  {
306  rotationFromLMS = Matrix.CreateRotationX(MathHelper.ToRadians(angle));
307  rotationToLMS = Matrix.CreateRotationX(MathHelper.ToRadians(-1.0f * angle));
308 
309  if (debugOutput)
310  {
311  Console.WriteLine("rotation matrix set to: " +rotationFromLMS);
312  }
313  }
314  #endregion
315 
316  #region noise texture
317  private void SetNoiseTexture() //gaussian pairing.
321  {
322  if (debugOutput)
323  {
324  Console.WriteLine("setting noise texture");
325  }
326 
327  Color[] noise = new Color[width * height];
328 
329  double variance = Math.Sqrt(2.0f*Math.Min(width,height));
330  float scale = (float)(variance*(2.0f/Math.PI));
331 
332  //debug:
333  Color old = new Color(0.0f, 0.0f, 0.0f, 1.0f); //debug
334  int count = 0; //debug
335 
336  for (int x=0; x<width; x++) {
337  for (int y = 0; y < height; y++)
338  {
339  #region create random gauss
340  // for rx
341  Random randx = new Random(); //reuse this if you are generating many
342  double u1x = randx.NextDouble(); //these are uniform(0,1) random doubles
343  double u2x = randx.NextDouble();
344  double randStdNormalx = Math.Sqrt(-2.0 * Math.Log(u1x)) * Math.Sin(2.0 * Math.PI * u2x); //random normal(0,1)
345 
346  //for ry
347  Random randy = new Random(); //reuse this if you are generating many
348  double u1y = randy.NextDouble(); //these are uniform(0,1) random doubles
349  double u2y = randy.NextDouble();
350  double randStdNormaly = Math.Sqrt(-2.0 * Math.Log(u1y)) * Math.Sin(2.0 * Math.PI * u2y); //random normal(0,1)
351  #endregion
352 
353  int index = y*width+x;
354  int rx = (int)Math.Floor(Math.Max(Math.Min(x + scale*randStdNormalx,width-1.0f),0.0f));
355  /*double c = y + scale * randStdNormal;
356  double b = Math.Min(c, height - 1.0f);
357  double a = Math.Max(b, 0.0f);
358  int ry = (int)Math.Floor(a);*/
359  int ry = (int)Math.Floor(Math.Max(Math.Min(y + scale * randStdNormaly, height - 1.0f), 0.0f));
360 
361  try
362  {
363  Vector4 currentColor = new Vector4((rx + 0.5f)/width, (ry + 0.5f)/height, 0.0f, 1.0f);
364 
365  noise[index] = new Color(currentColor);
366 
367  if (saveDebugTextures)
368  {
369  Color newC = noise[index];
370  if (newC.R > old.R) { old.R = newC.R; }
371  if (newC.G > old.G) { old.G = newC.G; }
372  if (newC.B > old.B) { old.B = newC.B; }
373 
374  if (newC.R >= 255 && newC.G >= 255 && newC.B >= 255)
375  {
376  count++;
377  }
378  }
379  }
380  catch (Exception e)
381  {
382  Console.WriteLine("index="+index+" EXCEPTION!!!!!!!!!!!!!!!!!!!!!! " + e);
383  throw;
384  }
385  }
386  }
387  try
388  {
389  if (debugOutput)
390  {
391  Console.WriteLine("max =" + old);
392  Console.WriteLine("count =" + count);
393  Console.WriteLine("set values to texture");
394  }
395  noiseTexture.SetData<Color>(noise);
396  }
397  catch (Exception e)
398  {
399 
400  Console.WriteLine(" EXCEPTION!!!!!!!!!!!!!!!!!!!!!! " + e);
401  throw;
402  }
403  if (debugOutput)
404  {
405  Console.WriteLine("noise texture set");
406  }
407  }
408  #endregion
409 
410  #endregion
411 
412  #region Simulate Perception
413  public void SimulatePerception(GraphicsDevice _device)
414  {
415  if (debugOutput)
416  {
417  Console.WriteLine("start simulatin perception");
418  }
419  device = _device;
420  device.DepthStencilState = DepthStencilState.None;
421 
422  colorVisionDeficency = GameProperties.Instance.CVD;
423  SetAngle();
424  SetMatrix();
425 
426  spriteBatch = new SpriteBatch(device);
427 
428  ConvertRGBtoLab(toSimulate);
429 
430  ProjectToReducedLMS();
431 
432  ConvertLabToRGB();
433  }
434  #endregion
435 
436  #region Recolor
437  public void Recolor(GraphicsDevice _device)
438  {
439  if (debugOutput)
440  {
441  Console.WriteLine("start recoloring");
442  }
443  device = _device;
444  device.DepthStencilState = DepthStencilState.None;
445 
446  colorVisionDeficency = GameProperties.Instance.CVD;
447  SetAngle();
448  SetMatrix();
449 
450  spriteBatch = new SpriteBatch(device);
451 
452  ConvertRGBtoLab(referenceImageRGB);
453 
454  ContrastLoss();
455 
456  PredominantComponent();
457 
458  ComputeEigenVectors();
459 
460  ProjectOntoLMSplane();
461 
462  MaxProjectionEffect();
463 
464  EnhanceContrast();
465 
466  ConvertLabToRGB();
467 
468  // ---------------------------------------------------------------------------------------
469 
470  device.DepthStencilState = DepthStencilState.Default;
471  }
472  #endregion
473 
474  #region methods for each recoloring/simulation step
475 
476  #region Simulate Perception
477  private void ProjectToReducedLMS()
484  {
485  tex_lab = target_rgbToLab;
486 
487  InitLMSShader();
488 
489  device.SetRenderTarget(target_lms);
490  DrawSprite(tex_lab, shader_lms);
491  device.SetRenderTarget(null);
492 
493  tex_enhancedContrast = target_lms; // pass directly to Lab->RGB shader
494 
495  if (saveDebugTextures)
496  {
497  saveScreenshot(tex_enhancedContrast, "0_simulated", "png"); // for debug
498  }
499 
500  if (debugOutput)
501  {
502  Console.WriteLine("ProjectToReducedLMS done");
503  }
504  }
505  #endregion
506 
507  #region RGB -> Lab
508  private void ConvertRGBtoLab(Texture2D _imageToConvert)
519  {
520  InitRGBToLab_Shader(_imageToConvert);
521 
522  device.SetRenderTarget(target_rgbToLab);
523  DrawSprite3(_imageToConvert, shader_rgbToLab);
524  device.SetRenderTarget(null);
525 
526  tex_lab = target_rgbToLab;
527 
528  if (saveDebugTextures)
529  {
530  saveScreenshot(_imageToConvert, "1_reference", "png");
531  saveScreenshot(tex_lab, "2_rgbToLab", "png"); //for debug
532  }
533 
534  if (debugOutput)
535  {
536  Console.WriteLine("ConvertRGBtoLab done");
537  }
538 
539  }
540  #endregion
541 
542  #region contrast loss
543  private void ContrastLoss()
563  {
564  noiseTexture = new Texture2D(device, width, height, true, SurfaceFormat.Color);
565  SetNoiseTexture();
566 
567  InitContrastLoss_Shader();
568 
569  device.SetRenderTarget(target_contrastLoss);
570  DrawSprite3(tex_lab, shader_contrastLoss);
571  device.SetRenderTarget(null);
572 
573  tex_contrastLost = target_contrastLoss;
574 
575  /*Rgba64[] testColros = new Rgba64[pca1Target.Height * pca1Target.Width];
576  pca1Target.GetData<Rgba64>(testColros);
577 
578  Texture2D noiseTexture2 = new Texture2D(device, width, height, true, SurfaceFormat.Rgba64);
579 
580  noiseTexture2.SetData<Rgba64>(testColros);
581  saveScreenshot(noiseTexture2, "noiseTexture2", "png");*/
582 
583  if (saveDebugTextures)
584  {
585  saveScreenshot(tex_contrastLost, "3_ContrastLoss", "png"); // for debug
586  saveScreenshot(noiseTexture, "noiseTexture", "png"); // for debug
587  }
588 
589  if (debugOutput)
590  {
591  Console.WriteLine("ContrastLoss done");
592  }
593  }
594  #endregion
595 
596  #region PCA: predominant component
597  private void PredominantComponent()
605  {
606  initPredominantComponenet_Shader();
607 
608  device.SetRenderTarget(target_predominantComponent);
609  DrawSprite3(tex_contrastLost, shader_predominantComponent);
610  device.SetRenderTarget(null);
611 
612  tex_predominantComponent = target_predominantComponent;
613 
614  if (saveDebugTextures)
615  {
616  saveScreenshot(tex_predominantComponent, "4_PredominantComponent", "png"); // for debug
617  }
618 
619  if (debugOutput)
620  {
621  Console.WriteLine("PCAPredominantComponent done");
622  }
623  }
624  #endregion
625 
626  #region eigen vectors
627  private void ComputeEigenVectors()
635  {
636  InitEigenVector_Shader();
637 
638  device.SetRenderTarget(target_eigenVector);
639  DrawSprite(tex_predominantComponent, shader_eigenVector); //TODO: check shader
640  device.SetRenderTarget(null);
641 
642  tex_eigenVectors = target_eigenVector;
643 
644  if (saveDebugTextures)
645  {
646  saveScreenshot(tex_eigenVectors, "5_EigenVectors", "png"); // for debug
647  }
648 
649  if (debugOutput)
650  {
651  Console.WriteLine("ComputeEigenVectors done");
652  }
653  }
654  #endregion
655 
656  #region project onto LMS plane
657  private void ProjectOntoLMSplane()
668  {
669  InitProjection_Shader();
670 
671  device.SetRenderTarget(target_projection);
672  DrawSprite(tex_eigenVectors, Projection_Shader);
673  device.SetRenderTarget(null);
674 
675  tex_projected = target_projection;
676 
677  if (saveDebugTextures)
678  {
679  saveScreenshot(tex_projected, "6_Projections", "png"); // for debug
680  }
681  if (debugOutput)
682  {
683  Console.WriteLine("ProjectOntoLMSplane done");
684  }
685  }
686  #endregion
687 
688  #region max projection effect
689  private void MaxProjectionEffect()
695  {
696  initMaxProjectionEffect_Shader();
697 
698  device.SetRenderTarget(target_maxProjectionEffect);
699  DrawSprite3(tex_projected, shader_maxProjectionEffect);
700  device.SetRenderTarget(null);
701 
702  tex_maxProjectedEffect = target_maxProjectionEffect;
703 
704  if (saveDebugTextures)
705  {
706  saveScreenshot(tex_maxProjectedEffect, "7_MaxProjectionEffect", "png"); // for debug
707  }
708 
709  if (debugOutput)
710  {
711  Console.WriteLine("MaxProjectionEffect done");
712  }
713  }
714  #endregion
715 
716  #region enhance contrast
717  private void EnhanceContrast()
724  {
725  InitEnhanceContrast_Shader();
726 
727  device.SetRenderTarget(target_enhanceContrast);
728  DrawSprite3(tex_maxProjectedEffect, shader_enhanceContrast);
729  device.SetRenderTarget(null);
730 
731  tex_enhancedContrast = target_enhanceContrast;
732 
733  if (saveDebugTextures)
734  {
735  saveScreenshot(tex_enhancedContrast, "8_EnhanceContrast", "png"); // for debug
736  }
737 
738  if (debugOutput)
739  {
740  Console.WriteLine("EnhanceContrast done");
741  }
742  }
743  #endregion
744 
745  #region Lab -> RGB
746  private void ConvertLabToRGB()
756  {
757  InitLabtoRGB_Shader();
758 
759  device.SetRenderTarget(target_labToRGB);
760  DrawSprite3(tex_enhancedContrast, shader_labToRGB);
761  device.SetRenderTarget(null);
762 
763  recoloredImage = target_labToRGB;
764 
765  if (saveDebugTextures)
766  {
767  if (GameProperties.Instance.simulate)
768  {
769  saveScreenshot(recoloredImage, "9_SimulatedImage", "png");
770  }
771  else
772  {
773  saveScreenshot(recoloredImage, "9_RecoloredImage", "png");
774  }
775  }
776 
777  if (debugOutput)
778  {
779  Console.WriteLine("ConvertLabToRGB done");
780  }
781  }
782  #endregion
783 
784  #endregion
785 
786  #region set effect parameters for shader
787 
791  private void InitLabtoRGB_Shader()
792  {
793  if (debugOutput)
794  {
795  Console.WriteLine("initializing L*a*b* to RGB shader");
796  }
797 
798  shader_labToRGB.EnhancedContrast_tex = tex_enhancedContrast;
799  shader_labToRGB.RotationMatrix = rotationFromLMS;
800 
801  shader_labToRGB.SetEffectParameter_labToRGB();
802  }
803 
807  private void InitEnhanceContrast_Shader()
808  {
809  if (debugOutput)
810  {
811  Console.WriteLine("initializing Result Color shader");
812  }
813 
814  shader_enhanceContrast.SetEffectParameter_EnhanceContrast(tex_maxProjectedEffect, tex_projected, tex_lab);
815  }
816 
820  private void initMaxProjectionEffect_Shader()
821  {
822  if (debugOutput)
823  {
824  Console.WriteLine("initializing PCA4 Reduction shader");
825  }
826 
827  shader_maxProjectionEffect.SetEffectParameter_MaxProjectionEffect(tex_projected);
828  }
829 
833  private void InitProjection_Shader()
834  {
835  if (debugOutput)
836  {
837  Console.WriteLine("initializing Projection shader");
838  }
839 
840  shader_projection.SetEffectParameter_Projection(tex_lab, tex_eigenVectors);
841  }
842 
846  private void InitEigenVector_Shader()
847  {
848  if (debugOutput)
849  {
850  Console.WriteLine("initializing EigenVector shader");
851  }
852 
853  shader_eigenVector.SetEffectParameter_EigenVectors(tex_predominantComponent);
854  }
855 
861  private void initPredominantComponenet_Shader()
862  {
863  if (debugOutput)
864  {
865  Console.WriteLine("initializing Predominant Component shader");
866  }
867 
868  shader_predominantComponent.SetEffectParameter_PredominantComponenet(tex_contrastLost);
869  }
870 
874  private void InitContrastLoss_Shader()
875  {
876  if (debugOutput)
877  {
878  Console.WriteLine("initializing Contrast Loss shader");
879  }
880 
881  //pca1Shader.SetEffectParameter_PCA1(referenceImageRGB, noiseTexture);
882  shader_contrastLoss.SetEffectParameter_ContrastLoss(tex_lab, noiseTexture);
883  }
884 
888  private void InitRGBToLab_Shader(Texture2D _imageToConvert)
889  {
890  if (debugOutput)
891  {
892  Console.WriteLine("initializing RGB to lab shader");
893  }
894 
895  shader_rgbToLab.RGBTexture = _imageToConvert;
896  shader_rgbToLab.RotationMatrix = rotationToLMS;
897 
898  shader_rgbToLab.SetEffectParameter_RGBtoLab();
899  }
900 
904  private void InitLMSShader()
905  {
906  if (debugOutput)
907  {
908  Console.WriteLine("initializing LMS shader");
909  }
910  shader_lms.SetEffectParameter_LMS(tex_lab);
911  }
912 
913  #endregion
914 
915  #region draw sprite
916 
922  public void DrawSprite(Texture2D tex, RecolorShader shader)
923  {
924  //Console.WriteLine("drawing sprite: " + tex.Name);
925 
926  device.Clear(Color.Black);
927  device.BlendState = BlendState.NonPremultiplied;
928 
929  spriteBatch.Begin(0, BlendState.NonPremultiplied, null, null, null, shader);
930 
931  spriteBatch.Draw(tex, new Rectangle(0, 0, width, height), Color.Black);
932 
933  spriteBatch.End();
934  }
935 
941  private void DrawSprite3(Texture2D tex, RecolorShader shader)
942  {
943  Console.WriteLine("drawing sprite: " + tex.Name);
944 
945  device.Clear(Color.Black);
946  device.BlendState = BlendState.AlphaBlend;
947 
948  try
949  {
950  spriteBatch.Begin(0, BlendState.AlphaBlend, null, null, null, shader);
951  }
952  catch (Exception e)
953  {
954  Console.WriteLine("EXCEPTION!!!!!!!!!!!!!!!!" + e);
955  throw;
956  }
957 
958  spriteBatch.Draw(tex, new Rectangle(0, 0, width, height), Color.White);
959 
960  spriteBatch.End();
961  }
962 
963  #endregion
964 
965  #endregion
966 
967  #region save screenshot
968  private void saveScreenshot(Texture2D texture, String name, String ext)
974  {
975  Console.WriteLine("saving " + name);
976  try
977  {
978  if (!name.ElementAt(name.Length - 4).Equals(".")) name = name + "." + ext;
979  FileStream stream = File.OpenWrite(name);
980  switch (ext)
981  {
982  case "png":
983  texture.SaveAsPng(stream, texture.Width, texture.Height);
984  break;
985  case "jpg":
986  texture.SaveAsJpeg(stream, texture.Width, texture.Height);
987  break;
988  default:
989  if (name.ElementAt(name.Length - 4).Equals(".")) name = name.Substring(0, name.Length - 4);
990  name = name + ".png";
991  texture.SaveAsPng(stream, texture.Width, texture.Height);
992  break;
993  }
994  }
995  catch (Exception e)
996  {
997  Console.WriteLine("Save Screenshot: ");
998  Console.WriteLine(e);
999  }
1000  }
1001  #endregion
1002  }
1003 }