Moved bit input and output streams into Svjatoslav Commons library.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Color.java
index 1d8b7a6..08535a2 100755 (executable)
@@ -1,3 +1,12 @@
+/*
+ * Imagesqueeze - Image codec optimized for photos.
+ * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
 package eu.svjatoslav.imagesqueeze.codec;
 
 /**
@@ -13,37 +22,48 @@ public class Color {
        int u;
        int v;
 
-       public void YUV2RGB(){
+       public void RGB2YUV() {
 
-               b = (int)(y + 1.4075 * (v - 128));
-               g = (int)(y - 0.3455 * (u - 128) - (0.7169 * (v - 128)));
-               r = (int)(y + 1.7790 * (u - 128));
+               y = (int) ((r * 0.299000) + (g * 0.587000) + (b * 0.114000));
+               u = (int) ((r * -0.168736) + (g * -0.331264) + (b * 0.500000) + 128);
+               v = (int) ((r * 0.500000) + (g * -0.418688) + (b * -0.081312) + 128);
 
-               if (r < 0) r = 0;
-               if (g < 0) g = 0;
-               if (b < 0) b = 0;
+               if (y < 0)
+                       y = 0;
+               if (u < 0)
+                       u = 0;
+               if (v < 0)
+                       v = 0;
 
-               if (r > 255) r = 255;
-               if (g > 255) g = 255;
-               if (b > 255) b = 255;
+               if (y > 255)
+                       y = 255;
+               if (u > 255)
+                       u = 255;
+               if (v > 255)
+                       v = 255;
 
        }
 
-       public void RGB2YUV(){
+       public void YUV2RGB() {
+
+               b = (int) (y + (1.4075 * (v - 128)));
+               g = (int) (y - (0.3455 * (u - 128)) - (0.7169 * (v - 128)));
+               r = (int) (y + (1.7790 * (u - 128)));
 
-               y = (int)(r * 0.299000 + g * 0.587000 + b * 0.114000);
-               u = (int)(r * -0.168736 + g * -0.331264 + b * 0.500000 + 128);
-               v = (int)(r *  0.500000 + g * -0.418688 + b * -0.081312 + 128);
+               if (r < 0)
+                       r = 0;
+               if (g < 0)
+                       g = 0;
+               if (b < 0)
+                       b = 0;
 
-               if (y < 0) y = 0;
-               if (u < 0) u = 0;
-               if (v < 0) v = 0;
+               if (r > 255)
+                       r = 255;
+               if (g > 255)
+                       g = 255;
+               if (b > 255)
+                       b = 255;
 
-               if (y > 255) y = 255;
-               if (u > 255) u = 255;
-               if (v > 255) v = 255;
-               
        }
-       
-};
 
+};