Example a/colour_transform: Uses colour_lookup_spectrum to adjust brightness and contrast.

Screenshot of example a/colour_transform


The comfile run by this example is as follows:

# Example a/colour_transform:  Use a spectrum to update the brightness and contrast of an image.

{

sub set_colour_lookup
{
  #An offset applied to the colour values after the contrast
  my $brightness = shift;  #Sensible values from -1 to +1

  #Apply a sigmoid filter of the form x/sqrt(1+x^2)
  my $contrast = shift;    #Sensible values from -1 to +1

  #Controls the centre of the contrast curve by doing an offset
  #before the contrast whereas the brightness is applied afterwards.
  my $offset = shift

  if (defined $brightness)
	 {
 		gfx define field brightness constant $brightness;
	 }
  else
	 {
 		gfx define field brightness constant 0.0;
	 }
  if (defined $contrast)
	 {
 		gfx define field contrast constant $contrast;
	 }
  else
	 {
 		gfx define field contrast constant 0.0;
	 }
  if (defined $offset)
	 {
 		gfx define field offset constant $offset;
	 }
  else
	 {
 		gfx define field offset constant 0.0;
	 }

  #We shouldn't have to modify the spectrum to get it to update,
  #it should register for computed field change events.
  gfx modify spectrum colour_lookup;

}

sub define_colour_lookup_fields
{
  gfx define field colour constant 0.5
  
  gfx define field brightness constant 0.0
  
  gfx define field contrast constant 0.0
  
  gfx define field offset constant 0.0
  
  gfx define field one constant 1
  
  gfx define field minus_one constant -1.0
  
  gfx define field two constant 2

  gfx define field offset_colour add fields colour offset 

  #Apply a sigmoid filter x sqrt(1 -c)/sqrt( 1 - c x^2)

  #Make an alias for the input = 2 x -1
  gfx define field two_offset_colour multiply fields two offset_colour
  
  gfx define field contrast_colour_input add fields two_offset_colour one scale_factors 1 -1
  
  gfx define field contrast_exp exp field contrast
  
  #If e^c > 1 then we can get imaginary solutions which we need to filter out
  gfx define field contrast_exp_imaginary greater_than fields contrast_exp one

  #If we can get imaginary solutions then they are input greater than
  # sqrt(1 / (e^c - 1)) and less than -sqrt(1 / (e^c - 1))
  gfx define field contrast_exp_minus_one add fields contrast_exp one scale_factors 1 -1
  
  gfx define field contrast_exp_minus_one_divided divide fields one contrast_exp_minus_one
  
  gfx define field contrast_exp_C sqrt field contrast_exp_minus_one_divided


  gfx define field contrast_exp_C sqrt field contrast_exp_minus_one_divided
  
  gfx define field contrast_colour_imaginary_A greater_than fields contrast_colour_input contrast_exp_C
  
  gfx define field minus_contrast_exp_C multiply fields contrast_exp_C minus_one
  
  gfx define field contrast_colour_imaginary_B less_than fields contrast_colour_input minus_contrast_exp_C

  #Make x sqrt(e^c)/sqrt( 1 - c x^2)
  
  gfx define field contrast_colour_input_sq multiply fields contrast_colour_input contrast_colour_input
  
  gfx define field contrast_colour_input_sq_contrast multiply fields contrast contrast_colour_input_sq
  
  gfx define field one_minus_contrast_colour_input_sq_contrast add fields one contrast_colour_input_sq_contrast scale_factors 1 -1
  
  gfx define field sqrt_one_minus_contrast_colour_input_sq_contrast sqrt field one_minus_contrast_colour_input_sq_contrast
  
  gfx define field sqrt_contrast_exp sqrt field contrast_exp
  
  gfx define field sqrt_contrast_exp_divided divide fields sqrt_contrast_exp sqrt_one_minus_contrast_colour_input_sq_contrast
  
  gfx define field contrast_colour_H multiply fields contrast_colour_input sqrt_contrast_exp_divided
  
  gfx define field contrast_colour_H_plus_one add fields contrast_colour_H one
  
  gfx define field contrast_colour_output_B divide fields contrast_colour_H_plus_one two
  
  gfx define field contrast_colour_output_E if fields contrast_colour_imaginary_A one contrast_colour_output_B
  
  gfx define field contrast_colour_output_F if fields contrast_colour_imaginary_B minus_one contrast_colour_output_E
  
  gfx define field contrast_colour if fields contrast_exp_imaginary contrast_colour_output_F contrast_colour_output_B

  gfx define field brightness_colour add fields contrast_colour brightness

  gfx create spectrum colour_lookup field monochrome input colour output brightness_colour;

}

# Read in some geometry to hold our texture and define coordinates.
gfx read node $example/SamplePlane_1.exnode;
gfx read element $example/Slice.exelem;

# Read in the first texture
gfx create texture input_image image "$example/torso_gray.png" width 1 height 1;
gfx create material input_image texture input_image;

#Set up the spectrum.  Initially don't change the colour.
define_colour_lookup_fields();
set_colour_lookup(0, 0);

#Specifying a 1 component spectrum with 3 input components will apply
#the same lookup independently to each colour component.
#As this image is gray we could have just looked up one of the colours
#however this material will also work for colour images.
gfx modify material input_image colour_lookup_spectrum colour_lookup per_pixel_mode colour_lookup_red colour_lookup_green colour_lookup_blue;

gfx modify g_element slice general clear circle_discretization 6 default_coordinate coordinates element_discretization "4*4*4" native_discretization none;
gfx modify g_element slice lines select_on material default selected_material default_selected;
gfx modify g_element slice surfaces select_on material input_image selected_material default_selected render_shaded texture_coordinates xi;

gfx create window 1 double_buffer;
gfx modify window 1 image scene default infinite_viewer_lighting two_sided_lighting;
gfx modify window 1 image add_light default;
gfx modify window 1 image add_light default_ambient;
gfx modify window 1 layout 2d ortho_axes z -y eye_spacing 0.25 width 400 height 400;
gfx modify window 1 set current_pane 1;
gfx modify window 1 background colour 0 0 0 texture none;
gfx modify window 1 view parallel eye_point 25 25 155.782 interest_point 25 25 0 up_vector -0 1 0 view_angle 28.8189 near_clipping_plane 1.55782 far_clipping_plane 556.712 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;
gfx modify window 1 set transform_tool current_pane 1 std_view_angle 40 normal_lines no_antialias fast_transparency blend_normal;

#Change the brightness
set_colour_lookup(0.1, 0.1, 0.0);

if ($TESTING)
  {
	 #Force the computed fields to be evaluated over and over
	 #to give us a small stress test for field evaluations.
	 gfx create texture output_image;
	 for (my $i = 0 ; $i < 20 ; $i++)
		{
		  set_colour_lookup(0.1, $i/20, 0.0);
		  gfx modify texture output_image linear width 1 height 1 evaluate_image field input_image spectrum colour_lookup texture_coord xi element_group slice;
		}
  }
}

Files used by this example are:

Name                  Modified     Size

colour_transform.com 09-Mar-2016 6.5k COPYRIGHT 17-Mar-2014 504 SamplePlane_1.exnode 17-Mar-2014 351 Slice.exelem 17-Mar-2014 1.5k torso_gray.png 17-Mar-2014 15k

Download the entire example:

Name                                Modified     Size

examples_a_colour_transform.tar.gz 09-Mar-2016 99k

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxFailureSun Mar 6 00:38:03 201624
last breakTue Feb 24 03:37:00 201521
cmgui-wx-debugFailureSun Mar 6 00:57:08 201636
last breakTue Feb 24 03:46:00 201537
cmgui-wx-debug-memorycheckFailureSun Mar 6 01:14:16 201654
last breakTue Feb 24 03:49:00 201555
cmgui-wx-debug-valgrindFailureSun Mar 6 03:27:23 20161745
last breakSun Mar 6 02:58:00 20161745
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:28 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debugFailureSun Mar 6 00:01:29 20161
last breakSun Mar 6 00:01:00 20161
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:29 20161
last breakSun Mar 6 00:01:00 20161
cmgui-wx-debug-valgrindFailureSun Mar 6 00:03:15 20169
last breakSun Mar 6 00:03:00 20169

Testing status by file:


Html last generated: Wed Mar 9 16:01:31 2016

Input last modified: Wed Mar 9 15:49:43 2016


CMISS Help / Examples / a / colour_transform