Moved to java 1.8. Code cleanup and formatting.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Color.java
1 /*
2  * Imagesqueeze - Image codec optimized for photos.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.imagesqueeze.codec;
11
12 /**
13  * Helper class to convert between RGB and YUV
14  */
15 class Color {
16
17     int r;
18     int g;
19     int b;
20
21     int y;
22     int u;
23     int v;
24
25     public void RGB2YUV() {
26
27         y = (int) ((r * 0.299000) + (g * 0.587000) + (b * 0.114000));
28         u = (int) ((r * -0.168736) + (g * -0.331264) + (b * 0.500000) + 128);
29         v = (int) ((r * 0.500000) + (g * -0.418688) + (b * -0.081312) + 128);
30
31         if (y < 0)
32             y = 0;
33         if (u < 0)
34             u = 0;
35         if (v < 0)
36             v = 0;
37
38         if (y > 255)
39             y = 255;
40         if (u > 255)
41             u = 255;
42         if (v > 255)
43             v = 255;
44
45     }
46
47     public void YUV2RGB() {
48
49         b = (int) (y + (1.4075 * (v - 128)));
50         g = (int) (y - (0.3455 * (u - 128)) - (0.7169 * (v - 128)));
51         r = (int) (y + (1.7790 * (u - 128)));
52
53         if (r < 0)
54             r = 0;
55         if (g < 0)
56             g = 0;
57         if (b < 0)
58             b = 0;
59
60         if (r > 255)
61             r = 255;
62         if (g > 255)
63             g = 255;
64         if (b > 255)
65             b = 255;
66
67     }
68
69 }