
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:
- A subclass of DK_Tool. This component handles your tool's data, rendering, and associated methods.
- A subclass of DK_ToolGUI. This component controls the tool editor window.
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:
- A constructor method: this should set to NULL all the parameters defined in your private section.
- A destructor method: this should delete the parameters defined in your private section.
- A copy method: this should copy your private data members. It is called when users choose Copy from the Tool menu. It is also used to restore previous values if users click Cancel in the tool editor. Many of the DKit data classes have predefined equality operators that you can use.
- An init method: this should create all parameters and set them to their default values. It should also create all required target legs. You do not need to create the OptionalBackground target leg.
- A classID method: this returns the class name of the tool. This should be the same as the .so file name, the .icon file name, and the third argument passed to the DK_DSOFUNC macro in the DK_ToolGUI subclass. It is also the default name for instances of the tool in the Eddie workspace.
- A defaultRect method: this returns the preferred position and size for rendering the image. Eddie can override this area by asking your tool to render a specific area, such as the Camera.
- A render method: this applies the effect to the image, and returns the rendering status. For an overview of the basic steps your render method should follow, see the next section, Rendering.
- A write method: this should write all variables that are needed to preserve the state of the tool to a specified ASCII file (.fn3 function file). It is called when users choose Write from the Tool menu or Write All from the Workspace menu.
- A read method: this should read variables from an ASCII file (.fn3 function file) in the order they were written by the write method. It is called when users choose Read Function from the Workspace menu.
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:
- frame is the frame number to render. It is a floating-point number because Time Control effects may make it necessary to render subframes.
- outim is a pointer to the DK_Image object in which your tool should store the result of your effect.
- area_to_do is the rectangular area that Eddie wants your tool to render, for example, the camera or Processing Region. DK_IRect2 is a class of integer rectangles defined in DK_Rect.h.
All render methods should return RenderStatus. This data type is enumerated in DK_Tool.h as follows:
- FAILURE: did not render successfully.
- DID_REQUEST: rendered the area_to_do argument passed to the render method.
- DID_ALL: rendered the entire image and ignore the area_to_do argument.
Your render method will deal with at least three DK_Image objects:
- The source image connected to a target leg. There may be several images connected to different legs.
- A temporary image to hold the results as they are computed.
- The final rendered image (outim). Typically, you would copy the temporary image to outim once you have finished rendering.
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.
- An Input Editor tab.
- A tool canvas showing the rendered image of the current frame, together with its component toggles and View menu. However, you must create any extra, custom canvases if you want them.
- A local Effect Canvas, together with a View menu.
- A local Playback Control Panel.
- Key, Previous/Next, OK, Apply, and Cancel buttons.
- Freeze and Thaw toggles.
- Animation status display.
Required Methods
You must provide the following:
- A create method: this is called once to create the window when the tool editor is opened.
- An update method: this redraws the window and updates the parameter values.
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:
- DK_ColourChannelToggles lets you display as set of RGBA toggles such as those found in the ColourChannels and Mix tools.
- DK_IconBrowser lets you display an icon browser, such as the list of brushes in the Impressionist tool or the list of effects in PainterlyEffects.
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:
- DK_ColourChannelToggles has one event: valueChangedCallback. callData is the new value.
- DK_ColourPalette has one event: colourSelectedCallback. callData is the index number of the selected colour.
- DK_IconBrowser has two events: iconSelectCallback corresponds to a single-click and iconAcceptCallback corresponds to a double-click. In both cases, callData is the icon's file name.
- DK_ColourSliders has one event: valueChangedCallback. callData is the new colour value.
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
