Grafische Datenverarbeitung
Digitale Bildverarbeitung

Ergänzende Quelltexte

#include <stdio.h>
#include <math.h>
#define PI 3,141592653589793238

//RGB zu SchwarzWeiß
void getGrauWert(double *rgb) {
   double& R = rgb[0];
   double& G = rgb[1];
   double& B = rgb[2];

      R = R*0.3+G*0.5+B*0.2;
      G = R;
      B = R;
   }
}

void rgb2hsi(double *hsi, double *rgb) {
   double& R = rgb[0];
   double& G = rgb[1];
   double& B = rgb[2];
   double& H = hsi[0];
   double& S = hsi[1];
   double& I = hsi[2];

   double min = R;
   if (G < min) min = G;
   if (B < min) min = B;

   I = (R+G+B)/3.0;
   S = 1 - min/I;
   if (S == 0.0) {
      H = 0.0;
   } else {
      H = ((R-G)+(R-B))/2.0;
      H = H/sqrt((R-G)*(R-G) + (R-B)*(G-B));
      H = acos(H);
      if (B > G) {
         H = 2*PI - H;
      }
      H = H/(2*PI);
   }
}

void hsi2rgb(double* rgb, double* hsi) {
   double& R = rgb[0];
   double& G = rgb[1];
   double& B = rgb[2];
   double& H = hsi[0];
   double& S = hsi[1];
   double& I = hsi[2];

   double r;
   double g;
   double b;

   if (H < 1.0/3.0) {
      b = (1-S)/3;
      r = (1+S*cos(2*PI*H)/cos(PI/3-2*PI*H))/3.0;
      g = 1 - (b + r);
   } else if (H < 2.0/3.0) {
      H = H - 1.0/3.0;
      r = (1-S)/3;
      g = (1+S*cos(2*PI*H)/cos(PI/3-2*PI*H))/3.0;
      b = 1 - (r+g);
   } else {
      H = H - 2.0/3.0;
      g = (1-S)/3;
      b = (1+S*cos(2*PI*H)/cos(PI/3-2*PI*H))/3.0;
      r = 1 - (g+b);
   }

   R = I * r * 3;
   G = I * g * 3;
   B = I * b * 3;
}