unsigned char *HSI_Values(unsigned char *inputbits, char inc, LONG input_width, LONG input_height) { static unsigned char *outputbits; long int i; static char start = 1; signed long int r, g, b, max, min, intensity, hue, saturation, temp; double angle; if (start) { outputbits = malloc(3*input_width*input_height*sizeof(unsigned char)); start = 0; } for (i=0; i < (input_width*input_height*3); i+=3) { b = inputbits[i]; g = inputbits[i+1]; r = inputbits[i+2]; max = Maximum(r,g,b); /* compute Intensity */ min = Minimum(r,g,b); intensity = (min + max) / 2; outputbits[i+2] = (unsigned char) intensity; if (max == min) /* achromatic case */ { outputbits[i] = 0; // hue is undefined outputbits[i+1] = 0; // saturation = 0 } else if (intensity < 20) /* too dark (ADDED) */ { outputbits[i] = 0; // hue is undefined outputbits[i+1] = 0; // saturation = 0 } else if (intensity > 235) /* too bright (ADDED) */ { outputbits[i] = 0; // hue is undefined outputbits[i+1] = 0; // saturation = 0 } else /* chromatic case */ { if (intensity <= 127) /* bottom cone (dark) */ saturation = (255 * (max - min)) / (max + min); else /* top cone (bright) */ saturation = (255 * (max - min)) / (512 - max - min); if ((saturation < 0) || (saturation > 255)) OutputText("Problem with saturation\n"); outputbits[i+1] = (unsigned char) saturation; if (r == max) hue = (60 *(g -b))/(max - min); /* color is between yellow and magenta */ else if (g == max) hue = 60*2 + (60*(b-r))/(max-min); /* color is between cyan and yellow */ else hue = 60*4 + (60*(r-g))/(max-min); /* color is between magenta and cyan */ if (hue < 0) hue = hue + 360; if ((hue < 0) || (hue > 360)) OutputText("Problem with hue\n"); temp = (255 * hue) / 360; outputbits[i] = (unsigned char) temp; } } return(outputbits); }