Writing the Code

Structure of the Code

The supplied TemplateTool.C file gives a good idea of how you should structure the code. Your custom tool requires two main components:

DK_Tool Subclass

The DK_Tool subclass of your custom tool is responsible for holding the parameter values, as well as for providing methods for setting up, copying, answering queries, rendering, reading and writing function files, displaying progress, and so on.

Required Methods

You should provide the following:

For a description of other methods you can write, see Virtual Methods.

Rendering

The render method is where your custom effect is actually applied to an image. The basic implementation of your effect depends on what you are trying to accomplish; however, there are some basic steps all render methods should follow.

The render method is prototyped as follows:

virtual RenderStatus render
                    (float     frame,
                     DK_Image  &outim,
                     DK_IRect2 &area_to_do) = 0;
In this prototype:

All render methods should return RenderStatus. This data type is enumerated in DK_Tool.h as follows:

Your render method will deal with at least three DK_Image objects:

The basic steps your render method should follow are:

1. Check to see that the child tools are connected properly; for example:

  if ( !isConnected(LegNumber) )
    return FAILURE;
2. Define your local variables.

3. Get the images from the target legs at the proper frame; for example:

  DK_Image *img = 
    (DK_Image*) getChildImage( fr, LegNumber );
4. Check to make sure that the rendering of the child image was not interrupted, such as by the user clicking Cancel. In the following example, sptr is a pointer that holds all the RGBA data for the image connected to the target leg (img).

  if ((sptr = img->getDataPtr()) == NULL) 
  { delete img;
    return FAILURE;
  }
5. Get the size of the child image:

  u_int width  = img->getWidth; 
  u_int height = img->getHeight;

  /* validate width and height of image */
  if (width == 0 || height == 0) 
    return FAILURE;
6. If necessary, get the Global Scale. Depending on your tool, it may be necessary to adjust the size of brushes and so on based on the Global Scale value. To get the Global Scale:

  float gs = getGlobalScale();
7. Create a temporary buffer to store your results as they are computed; for example:

  DK_Image *tempImage =  
                   new DK_Image(img->getRect());
8. Apply the effect to the source image and store the results in the temporary buffer. The DK_Image class has a number of convenience functions you can use, including getDataArray, getDataPtr, getLine, and getPixel. For pixel components, you should use the DK_PIXEL* data type. This lets you easily create both 8 and 16-bit versions of your tool. For more information, see Writing 8 and 16-Bit Versions.

9. Copy your temporary image to outim once your effect is applied:

  outim.copyRect(outim.getRect().getMin(),               *tempImage, tempImage->getRect());
10. Before returning the appropriate RenderStatus value, delete any objects you created, including the source image and temporary buffer:

  delete img;
  delete tempImage;

Storing Data

You normally store your data (parameter settings and so on) in the private section of the DK_Tool subclass. For animatable parameters and colours, use members of DK_AnimParam and DK_AnimColour.

Each animatable parameter you define in your tool's DK_Tool section should have its related widget in the DK_ToolGUI section. For instance:

class MyTool : Public DK_Tool
{ ...
  private :
    DK_AnimParam *_animParam;
}
class MyToolGUI : public DK_ToolGUI
{...
  private :
    DK_APScroller *_animParamGUI;
    MyTool *_ptr;
}
Widget MyToolGUI::create(...)
{
  _animPAramGUI = new 
    DK_APScroller(*_ptr->_animParam,...);
  ...
}

DK_ToolGUI Subclass

The DK_ToolGUI subclass of your tool is responsible for displaying widgets for your parameters on the tool shelf, as well as for managing and drawing any custom canvases and graphics.

GUI Elements Provided Automatically

You do not have to provide any of the following items. They are provided automatically by Eddie for every custom tool.

Required Methods

You must provide the following:

Building Your Tool Shelf

In the private section of the DK_ToolGUI component, you must include a widget for every parameter that you want your users to set, and link the widget with the corresponding parameter in the DK_Tool section (see Storing Data).

For animated parameters, you can use the widgets in DK_AnimParamI. For animated colours, use the widgets in DK_ColourPalette or DK_ColourSliders. For non-animated parameters, you can use the standard ViewKit or X Window widgets.

To display a browser, add a button and link it to a DK_FileBrowser object.

In addition, there are some other special widgets you can use:

Note that some of the GUI classes are derived from SGI ViewKit base classes, such as VkComponent. This means that you can use the VkComponent baseWidget member function to retrieve the GUI's base widget for the purpose of setting resources. For more information, refer to the on-line IRIS ViewKit Programmer's Guide.

Interacting with ViewKit

Several GUI classes make use of the callback feature of the SGI ViewKit. They include events that VKCallbackObjects can register themselves for:

For more information, refer to the on-line IRIS ViewKit Programmer's Guide.

Custom Canvases

Every custom tool automatically has an output canvas showing the result of the effect on the current frame. If desired, you can add extra canvases to your tool. For example, the Impressionist and PainterlyEffects tools have an extra canvas that allows you to set the Preview Region.

To add an image canvas, use a DK_ImageCanvas object to create the canvas itself, then use a DK_ImageGraphic to associate the canvas with a DK_Image object.

To add a time spline canvas such as the one found in the Animation Editor, use a DK_TimeSplineCanvas object to create the canvas itself, then draw a DK_TimeSplineGraphic in it.

For any other type of canvas, you can create your own subclass of DK_Canvas. If desired, you can use a DK_ScreenMap object to control how image space is mapped to screen space.

Another type of graphic you can draw in canvases is the DK_RectGraphic class. These let you draw rectangular areas, such as the Processing Region or the Preview Region of Impressionist and PainterlyEffects.

To create your own graphics such as custom brushes, create a subclass of DK_UserGraphic. You are responsible for any corresponding data class for your custom graphics.

DK_Tool Subclass
Required Methods
Rendering
Storing Data
DK_ToolGUI Subclass
GUI Elements Provided Automatically
Required Methods
Building Your Tool Shelf
Interacting with ViewKit
Custom Canvases

SOFTIMAGE Eddie 3.5 - 10 JAN 1997