Example a_backup/testing/cmiss_field_image_api: Using the C API to work with Cmiss_field_image.

This routine builds a program which links against the cmgui libraries to test manipulating fields based on image data.


The comfile run by this example is as follows:

extern "C"
{
#include "api/cmiss_context.h"
#include "api/cmiss_core.h"
#include "api/cmiss_region.h"
#include "api/cmiss_field.h"
#include "api/cmiss_field_arithmetic_operators.h"
#include "api/cmiss_field_composite.h"
#include "api/cmiss_field_image.h"
#include "api/cmiss_field_image_processing.h"
#include "api/cmiss_field_module.h"
#include "api/cmiss_stream.h"
}
#include <stdio.h>

int main(void)
{
	Cmiss_context_id context = Cmiss_context_create("example");
	Cmiss_region_id region = Cmiss_context_get_default_region(context);
	Cmiss_field_module_id field_module =  Cmiss_region_get_field_module(region);
	/* Create a field to be our domain for these images, as we aren't
	 * using it as a field we can just use anything. */

	/* Create first field */
	Cmiss_field_id field_one = Cmiss_field_module_create_image(field_module, NULL, NULL);
	Cmiss_field_image_id image_field_one = Cmiss_field_cast_image(field_one);
	/* Read image data from a file */
	Cmiss_field_image_read_file(image_field_one, "sunset.jpg");

	/* Write image data to a memory block */
 	Cmiss_stream_information_id stream_information = NULL;
	Cmiss_stream_resource_id stream = NULL;
	Cmiss_stream_information_image_id image_stream_information = NULL;

 	stream_information = Cmiss_field_image_create_stream_information(image_field_one);
	image_stream_information = Cmiss_stream_information_cast_image(stream_information);
	Cmiss_stream_information_image_set_file_format(image_stream_information,
		CMISS_STREAM_INFORMATION_IMAGE_FILE_FORMAT_PNG);
	stream = Cmiss_stream_information_create_resource_memory(stream_information);
	Cmiss_field_image_write(image_field_one, stream_information);
	void *memory_buffer = NULL;
	unsigned int memory_buffer_size = 0;
	Cmiss_stream_resource_memory_id memory_resource = Cmiss_stream_resource_cast_memory(stream);
	Cmiss_stream_resource_memory_get_buffer_copy(memory_resource, &memory_buffer, &memory_buffer_size);
	FILE *memory_file;
	memory_file = fopen("memory.png", "w");
	fwrite(memory_buffer, memory_buffer_size, 1, memory_file);
	fclose(memory_file);
	Cmiss_stream_resource_memory_destroy(&memory_resource);
	Cmiss_stream_resource_destroy(&stream);
	Cmiss_stream_information_image_destroy(&image_stream_information);
	Cmiss_stream_information_destroy(&stream_information);
  
	/* Create a second field */
	Cmiss_field_id field_two = Cmiss_field_module_create_image(field_module, NULL, NULL);
	Cmiss_field_image_id image_field_two = Cmiss_field_cast_image(field_two);
	
	/* Read the memory block data into new field, Cmiss_field_image_read
	 * should be able to determine the type of a dicom image. */
	stream_information = Cmiss_field_image_create_stream_information(image_field_two);
	stream = Cmiss_stream_information_create_resource_memory_buffer(stream_information,
		memory_buffer, memory_buffer_size);
	Cmiss_field_image_read(image_field_two, stream_information);
	Cmiss_stream_resource_destroy(&stream);
	Cmiss_stream_information_destroy(&stream_information);
	Cmiss_deallocate(memory_buffer);
	
	/* Write out to a file */
	Cmiss_field_image_write_file(image_field_two, "test_output.png");
	
	/* Write a reduced size copy of image_field_two */
	stream_information = Cmiss_field_image_create_stream_information(image_field_one);
	image_stream_information =	Cmiss_stream_information_cast_image(stream_information);
	stream = Cmiss_stream_information_create_resource_file(stream_information,
		"thumbnail_output.png");
	Cmiss_stream_information_image_set_file_format(image_stream_information,
		CMISS_STREAM_INFORMATION_IMAGE_FILE_FORMAT_PNG);
	Cmiss_stream_information_image_set_attribute_integer(image_stream_information,
		CMISS_STREAM_INFORMATION_IMAGE_ATTRIBUTE_RAW_WIDTH_PIXELS, 100);
	Cmiss_field_image_write(image_field_one, stream_information);
	Cmiss_stream_resource_destroy(&stream);
	Cmiss_stream_information_image_destroy(&image_stream_information);
	Cmiss_stream_information_destroy(&stream_information);

	/* Create a field for each of the three component of field one */
	Cmiss_field_id red_component = Cmiss_field_module_create_component(field_module, field_one,0);
	Cmiss_field_id green_component = Cmiss_field_module_create_component(field_module, field_one,1);
	Cmiss_field_id blue_component = Cmiss_field_module_create_component(field_module, field_one,2);
	/* filter out the green component */
	Cmiss_field_id filtered_green =	Cmiss_field_module_create_threshold_image_filter(
		field_module, green_component, GENERAL_THRESHOLD_FILTER_MODE_BELOW, 
		(double)0.0, (double)1.0, (double)1.0);

	/* Create an array of fields */
	Cmiss_field_id image_components[3];
	image_components[0] = red_component;
	image_components[1] = filtered_green;
	image_components[2] = blue_component;

	/* Compose the three components into one single field */
	Cmiss_field_id filtered = Cmiss_field_module_create_concatenate(field_module, 3, image_components);

	/* Create an image field based on the filtered field */
	Cmiss_field_id output = Cmiss_field_module_create_image(field_module, NULL, filtered);

	/* Get the texture from image field "output" */

	Cmiss_field_image_id image_output = Cmiss_field_cast_image(output);
	Cmiss_field_image_write_file(image_output, "filtered_sunset.jpg");

	Cmiss_field_image_destroy(&image_output);
	Cmiss_field_destroy(&output);
	Cmiss_field_destroy(&filtered);

	Cmiss_field_destroy(&filtered_green);
	Cmiss_field_destroy(&red_component);
	Cmiss_field_destroy(&green_component);
	Cmiss_field_destroy(&blue_component);

	Cmiss_field_image_destroy(&image_field_two);
	Cmiss_field_destroy(&field_two);
	Cmiss_field_image_destroy(&image_field_one);
	Cmiss_field_destroy(&field_one);
	Cmiss_field_module_destroy(&field_module);
	Cmiss_region_destroy(®ion);
	Cmiss_context_destroy(&context);

}


Files used by this example are:

Name                       Modified     Size

cmiss_field_image_api.cpp 20-Apr-2012 5.6k Makefile 20-Apr-2012 2.5k sunset.jpg 20-Apr-2012 151k

Download the entire example:

Name                                                    Modified     Size

examples_a_backup_testing_cmiss_field_image_api.tar.gz 12-Aug-2014 3.4M

Testing status by version:

StatusTestedReal time (s)
x86-linux
cmgui-gtk-debugFailureSun Mar 6 00:06:45 20160
last breakThu Aug 7 16:40:00 20140
last successFri Jan 11 00:24:00 201315

Testing status by file:


Html last generated: Sun Mar 6 05:51:01 2016

Input last modified: Fri Apr 20 16:02:23 2012


CMISS Help / Examples / a_backup / testing / cmiss_field_image_api