Source for gnu.javax.imageio.jpeg.YCbCr_ColorSpace

   1: /* YCbCr_ColorSpace.java --
   2:    Copyright (C)  2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.javax.imageio.jpeg;
  39: 
  40: import java.awt.color.ColorSpace;
  41: 
  42: public class YCbCr_ColorSpace extends ColorSpace {
  43:   public YCbCr_ColorSpace() {
  44:     super(ColorSpace.TYPE_YCbCr, 3);
  45:   }
  46: 
  47:   public float[] fromCIEXYZ(float[] data) {
  48:     return(new float[data.length]);
  49:   }
  50: 
  51:   public float[] toCIEXYZ(float[] data) {
  52:     return(new float[data.length]);
  53:   }
  54: 
  55:   public float[] fromRGB(float[] data) {
  56:     return(new float[data.length]);
  57:   }
  58: 
  59:   /* YCbCr to RGB range 0 to 1 */
  60:   public float[] toRGB(float[] data) {
  61:     float[] dest = new float[3];
  62: 
  63:     data[0] *= 255;
  64:     data[1] *= 255;
  65:     data[2] *= 255;
  66: 
  67:     dest[0] = (float)data[0] + (float)1.402*((float)data[2]-(float)128);
  68:     dest[1] = (float)data[0] - (float)0.34414*((float)data[1]-(float)128) - (float)0.71414*((float)data[2]-(float)128);
  69:     dest[2] = (float)data[0] + (float)1.772*((float)data[1]-(float)128);
  70: 
  71:     dest[0] /= 255;
  72:     dest[1] /= 255;
  73:     dest[2] /= 255;
  74: 
  75:     //dest[0] = ((float)1.164*((float)data[0]*(float)255 - (float)16) + (float)1.596*((float)data[2]*(float)255 - (float)128))/(float)255;
  76:     //dest[1] = ((float)1.164*((float)data[0]*(float)255 - (float)16) - (float)0.813*((float)data[2]*(float)255 - (float)128) - (float)0.392*(data[1]*255 - 128))/(float)255;
  77:     //dest[2] = ((float)1.164*((float)data[0]*(float)255 - (float)16) + (float)2.017*((float)data[1]*(float)255 - (float)128))/(float)255;
  78: 
  79:     //System.err.println("toRGB values received: 0: "+data[0]+" 1: "+data[1]+" 2: "+data[2]+" sent: 0: "+dest[0]+" 1: "+dest[1]+" 2: "+dest[2]);
  80:     if(dest[0] < (float)0)
  81:       dest[0] = 0;
  82:     if(dest[1] < (float)0)
  83:       dest[1] = 0;
  84:     if(dest[2] < (float)0)
  85:       dest[2] = 0;
  86: 
  87:     if(dest[0] > (float)1)
  88:       dest[0] = 1;
  89:     if(dest[1] > (float)1)
  90:       dest[1] = 1;
  91:     if(dest[2] > (float)1)
  92:       dest[2] = 1;
  93: 
  94: 
  95:     return(dest);
  96:   }
  97: 
  98:   /* RGB to YCbCr range 0-255 */
  99:   public static float[] toYCbCr(float[] data) {
 100:     float[] dest = new float[3];
 101:     //dest[0] = (float)0.257*data[0] + (float)0.504*data[1] + (float)0.098*data[2] + 16;
 102:     //dest[1] = (float)-0.148*data[0] - (float)0.291*data[1] + (float)0.439*data[2] + 128;
 103:     //dest[2] = (float)0.439*data[0] - (float)0.368*data[1] - (float)0.071*data[2] + 128;
 104: 
 105:     dest[0] = (float)((0.299 * (float)data[0] + 0.587 * (float)data[1] + 0.114 * (float)data[2]));
 106:     dest[1] = 128 + (float)((-0.16874 * (float)data[0] - 0.33126 * (float)data[1] + 0.5 * (float)data[2]));
 107:     dest[2] = 128 + (float)((0.5 * (float)data[0] - 0.41869 * (float)data[1] - 0.08131 * (float)data[2]));
 108: 
 109: 
 110:     return(dest);
 111: 
 112:   }
 113: }