/*
  Define declarations.
*/
#define MinPanSize  96
#define MaxStacksize  (1 << 15)
#define Push(up,left,right,delta) \
  if ((p < (segment_stack+MaxStacksize)) && (((up)+(delta)) >= 0) && \
      (((up)+(delta)) < image->rows)) \
    { \
      p->y1=(up); \
      p->x1=(left); \
      p->x2=(right); \
      p->y2=(delta); \
      p++; \
    }
#define RoiDelta  8
#define LoadImageText  "  Loading image...  "

/*
  State declarations.
*/
#define DefaultState  0x0000
#define EscapeState  0x0001
#define ExitState  0x0002
#define FormerImageState  0x0004
#define ModifierState  0x0008
#define MontageImageState  0x0010
#define NextImageState  0x0020
#define UpdateConfigurationState  0x0040
#define UpdateRegionState  0x0080

/*
  Page size declarations.
*/
static char
  *PageSizes[]=
  {
    "Letter",
    "Tabloid",
    "Ledger",
    "Legal",
    "Statement",
    "Executive",
    "A3",
    "A4",
    "A5",
    "B4",
    "B5",
    "Folio",
    "Quarto",
    "10x14",
    (char *) NULL
  };

/*
  Help widget declarations.
*/
static char
  *ImageAnnotateHelp[]=
  {
    "In annotate mode, the Command widget has these options:",
    "",
    "    Font Name",
    "      fixed",
    "      variable",
    "      5x8",
    "      6x10",
    "      7x13bold",
    "      8x13bold",
    "      9x15bold",
    "      10x20",
    "      12x24",
    "      Browser...",
    "    Font Color",
    "      black",
    "      blue",
    "      cyan",
    "      green",
    "      gray",
    "      red",
    "      magenta",
    "      yellow",
    "      white",
    "      Browser...",
    "    Rotate Text",
    "      45",
    "      90",
    "      135",
    "      180",
    "      225",
    "      270",
    "      315",
    "      Dialog...",
    "    Help",
    "    Return",
    "",
    "Choose a font name from the Font Name sub-menu.  Additional",
    "font names can be specified with the font browser.  You can",
    "change the menu names by setting the X resources font1",
    "through font9.",
    "",
    "Choose a font color from the Font Color sub-menu.",
    "Additional font colors can be specified with the color",
    "browser.  You can change the menu colors by setting the X",
    "resources pen1 through pen9.",
    "",
    "If you select the color browser and press Grab, you can",
    "choose the font color by moving the pointer to the desired",
    "color on the screen and press any button.",
    "",
    "If you choose to rotate the text, choose Rotate Text from the",
    "menu and select an angle.  Typically you will only want to",
    "rotate one line of text at a time.  Depending on the angle you",
    "choose, subsequent lines may end up overwriting each other.",
    "",
    "Choosing a font and its color is optional.  The default font",
    "is fixed and the default color is black.  However, you must",
    "choose a location to begin entering text and press a button.",
    "An underscore character will appear at the location of the",
    "pointer.  The cursor changes to a pencil to indicate you are",
    "in text mode.  To exit immediately, press Return.",
    "",
    "In text mode, any key presses will display the character at",
    "the location of the underscore and advance the underscore",
    "cursor.  Enter your text and once completed press Return to",
    "finish your image annotation.  To correct errors press BACK",
    "SPACE.  To delete an entire line of text, press DELETE.  Any",
    "text that exceeds the boundaries of the image window is",
    "automatically continued onto the next line.",
    "",
    "The actual color you request for the font is saved in the",
    "image.  However, the color that appears in your image window",
    "may be different.  For example, on a monochrome screen the",
    "text will appear black or white even if you choose the color",
    "red as the font color.  However, the image saved to a file",
    "with -write is written with red lettering.  To assure the",
    "correct color text in the final image, any PseudoClass image",
    "is promoted to DirectClass (see miff(5)).  To force a",
    "PseudoClass image to remain PseudoClass, use -colors.",
    (char *) NULL,
  },
  *ImageChopHelp[]=
  {
    "In chop mode, the Command widget has these options:",
    "",
    "    Direction",
    "      horizontal",
    "      vertical",
    "    Help",
    "    Return",
    "",
    "If the you choose the horizontal direction (this is the",
    "default), the area of the image between the two horizontal",
    "endpoints of the chop line is removed.  Otherwise, the area",
    "of the image between the two vertical endpoints of the chop",
    "line is removed.",
    "",
    "Select a location within the image window to begin your chop,",
    "press and hold any button.  Next, move the pointer to",
    "another location in the image.  As you move a line will",
    "connect the initial location and the pointer.  When you",
    "release the button, the area within the image to chop is",
    "determined by which direction you choose from the Command",
    "widget.",
    "",
    "To cancel the image chopping, move the pointer back to the",
    "starting point of the line and release the button.",
    (char *) NULL,
  },
  *ImageColorEditHelp[]=
  {
    "In color edit mode, the Command widget has these options:",
    "",
    "    Pixel Color",
    "      black",
    "      blue",
    "      cyan",
    "      green",
    "      gray",
    "      red",
    "      magenta",
    "      yellow",
    "      white",
    "      Browser...",
    "    Method",
    "      point",
    "      replace",
    "      floodfill",
    "    Undo",
    "    Help",
    "    Return",
    "",
    "Choose a pixel color from the Pixel Color sub-menu.",
    "Additional pixel colors can be specified with the color",
    "browser.  You can change the menu colors by setting the X",
    "resources pen1 through pen9.",
    "",
    "Next, choose a color editing method from the Method sub-menu",
    "of the Command widget.  The point method recolors any pixel",
    "selected with the pointer until the button is released.  The",
    "replace method recolors any pixel that matches the color of",
    "the pixel you select with a button press.  Floodfill recolors",
    "any pixel that matches the color of the pixel you select with",
    "a button press and is a neighbor.",
    "",
    "Now press a button to select a pixel within the image window",
    "to change its color.  Additional pixels may be recolored as",
    "prescribed by the method you choose.  If the Magnify widget",
    "is mapped, it can be helpful in positioning your pointer",
    "within the image (refer to button 2).",
    "",
    "The actual color you request for the pixels is saved in the",
    "image.  However, the color that appears in your image window",
    "may be different.  For example, on a monochrome screen the",
    "pixel will appear black or white even if you choose the",
    "color red as the pixel color.  However, the image saved to a",
    "file with -write is written with red pixels.  To assure the",
    "correct color text in the final image, any PseudoClass image",
    "is promoted to DirectClass (see miff(5)).  To force a",
    "PseudoClass image to remain PseudoClass, use -colors.",
    (char *) NULL,
  },
  *ImageCompositeHelp[]=
  {
    "First a widget window is displayed requesting you to enter an",
    "image name. Press Composite, Grab or type a file name.",
    "Press Cancel if you choose not to create a composite image.",
    "When you choose Grab, move the pointer to the desired window",
    "and press any button.",
    "",
    "A small window appears showing the location of the cursor in",
    "the image window. You are now in composite mode.  To exit",
    "immediately, press Return.  In composite mode, the Command",
    "widget has these options:",
    "",
    "    Operators",
    "      over",
    "      in",
    "      out",
    "      atop",
    "      xor",
    "      plus",
    "      minus",
    "      add",
    "      subtract",
    "      difference",
    "      replace",
    "    Help",
    "    Return",
    "",
    "Choose a composite operation from the Operators sub-menu of",
    "the Command widget.  How each operator behaves is described",
    "below.  Image window is the image currently displayed on",
    "your X server and image is the image obtained with the File",
    "Browser widget.",
    "",
    "over     The result is the union of the two image shapes,",
    "         with image obscuring image window in the region of",
    "         overlap.",
    "",
    "in       The result is simply image cut by the shape of",
    "         image window.  None of the image data of image",
    "         window is in the result.",
    "",
    "out      The resulting image is image with the shape of",
    "         image window cut out.",
    "",
    "atop     The result is the same shape as image image window,",
    "         with image obscuring image window where the image",
    "         shapes overlap.  Note this differs from over",
    "         because the portion of image outside image window's",
    "         shape does not appear in the result.",
    "",
    "xor      The result is the image data from both image and",
    "         image window that is outside the overlap region.",
    "         The overlap region is blank.",
    "",
    "plus     The result is just the sum of the image data.",
    "         Output values are cropped to 255 (no overflow).",
    "         This operation is independent of the matte",
    "         channels.",
    "",
    "minus    The result of image - image window, with underflow",
    "         cropped to zero.  The matte channel is ignored (set",
    "         to 255, full coverage).",
    "",
    "add      The result of image + image window, with overflow",
    "         wrapping around (mod 256).",
    "",
    "subtract The result of image - image window, with underflow",
    "         wrapping around (mod 256).  The add and subtract",
    "         operators can be used to perform reversible",
    "         transformations.",
    "",
    "difference",
    "         The result of abs(image - image window).  This is",
    "         useful for comparing two very similar images.",
    "",
    "replace  The resulting image is image window replaced with",
    "         image.  Here the matte information is ignored.",
    "",
    "The image compositor requires a matte, or alpha channel in",
    "the image for some operations.  This extra channel usually",
    "defines a mask which represents a sort of a cookie-cutter",
    "for the image.  This is the case when matte is 255 (full",
    "coverage) for pixels inside the shape, zero outside, and",
    "between zero and 255 on the boundary.  If image does not",
    "have a matte channel, it is initialized with 0 for any pixel",
    "matching in color to pixel location (0,0), otherwise 255.",
    "",
    "Note that matte information for image window is not retained",
    "for colormapped X server visuals (e.g. StaticColor,",
    "StaticColor, GrayScale, PseudoColor).  Correct compositing",
    "behavior may require a TrueColor or DirectColor visual or a",
    "Standard Colormap.",
    "",
    "Choosing a composite operator is optional.  The default",
    "operator is replace.  However, you must choose a location to",
    "composite your image and press a button.  Press and hold the",
    "button before releasing and an outline of the image will",
    "appear to help you identify your location.",
    "",
    "The actual colors of the composite image is saved.  However,",
    "the color that appears in image window may be different.",
    "For example, on a monochrome screen image window will appear",
    "black or white even though your composited image may have",
    "many colors.  If the image is saved to a file it is written",
    "with the correct colors.  To assure the correct colors are",
    "saved in the final image, any PseudoClass image is promoted",
    "to DirectClass (see miff(5)).  To force a PseudoClass image",
    "to remain PseudoClass, use -colors.",
    (char *) NULL,
  },
  *ImageCutHelp[]=
  {
    "In cut mode, the Command widget has these options:",
    "",
    "    Help",
    "    Return",
    "",
    "To define a cut region, press button 1 and drag.  The",
    "cut region is defined by a highlighted rectangle that",
    "expands or contracts as it follows the pointer.  Once you",
    "are satisfied with the cut region, release the button.",
    "You are now in rectify mode.  In rectify mode, the Command",
    "widget has these options:",
    "",
    "    Cut",
    "    Help",
    "    Return",
    "",
    "You can make adjustments by moving the pointer to one of the",
    "cut rectangle corners, pressing a button, and dragging.",
    "Finally, press Cut to commit your copy region.  To",
    "exit without cutting the image, press Return.",
    (char *) NULL,
  },
  *ImageCopyHelp[]=
  {
    "In copy mode, the Command widget has these options:",
    "",
    "    Help",
    "    Return",
    "",
    "To define a copy region, press button 1 and drag.  The",
    "copy region is defined by a highlighted rectangle that",
    "expands or contracts as it follows the pointer.  Once you",
    "are satisfied with the copy region, release the button.",
    "You are now in rectify mode.  In rectify mode, the Command",
    "widget has these options:",
    "",
    "    Copy",
    "    Help",
    "    Return",
    "",
    "You can make adjustments by moving the pointer to one of the",
    "copy rectangle corners, pressing a button, and dragging.",
    "Finally, press Copy to commit your copy region.  To",
    "exit without copying the image, press Return.",
    (char *) NULL,
  },
  *ImageCropHelp[]=
  {
    "In crop mode, the Command widget has these options:",
    "",
    "    Help",
    "    Return",
    "",
    "To define a cropping region, press button 1 and drag.  The",
    "cropping region is defined by a highlighted rectangle that",
    "expands or contracts as it follows the pointer.  Once you",
    "are satisfied with the cropping region, release the button.",
    "You are now in rectify mode.  In rectify mode, the Command",
    "widget has these options:",
    "",
    "    Crop",
    "    Help",
    "    Return",
    "",
    "You can make adjustments by moving the pointer to one of the",
    "cropping rectangle corners, pressing a button, and dragging.",
    "Finally, press Crop to commit your cropping region.  To",
    "exit without cropping the image, press Return.",
    (char *) NULL,
  },
  *ImageDrawHelp[]=
  {
    "The cursor changes to a crosshair to indicate you are in",
    "draw mode.  To exit immediately, press Return.  In draw mode,",
    "the Command widget has these options:",
    "",
    "    Primitive",
    "      line",
    "      rectangle",
    "      fill rectangle",
    "    Color",
    "      black",
    "      blue",
    "      cyan",
    "      green",
    "      gray",
    "      red",
    "      magenta",
    "      yellow",
    "      white",
    "      Browser...",
    "    Width",
    "      1",
    "      2",
    "      4",
    "      6",
    "      8",
    "      Dialog...",
    "    Undo",
    "    Help",
    "    Return",
    "",
    "Choose a drawing primitive from the Primitive sub-menu.",
    "",
    "Choose a color from the Color sub-menu.  Additional",
    "colors can be specified with the color browser.",
    "",
    "If you choose the color browser and press Grab, you can",
    "select the color by moving the pointer to the desired",
    "color on the screen and press any button.",
    "",
    "Choose a width from the Width sub-menu.  To choose a",
    "specific width select the Dialog widget.",
    "",
    "Choose a point in the Image window and press a button and",
    "hold.  Next, move the pointer to another location in the",
    "image.  As you move, a line connects the initial location and",
    "the pointer.  When you release the button, the image is",
    "updated with the primitive you just drew.",
    "",
    "To cancel image drawing, move the pointer back to the",
    "starting point of the line and release the button.",
    (char *) NULL,
  },
  *ImageMagickHelp[]=
  {
    "BUTTONS",
    "  The effects of each button press is described below.  Three",
    "  buttons are required.  If you have a two button mouse,",
    "  button 1 and 3 are returned.  Press ALT and button 3 to",
    "  simulate button 2.",
    "",
    "  1    Press this button to map or unmap the Command widget.",
    "",
    "  2    Press and drag to define a region of the image to",
    "       magnify.",
    "",
    "  3    Choose a particular tile of the Visual Image Directory",
    "       and press this button and drag to select a command from",
    "       a pop-up menu.  Choose from these menu items:",
    "",
    "           Load",
    "           Delete",
    "",
    "       If you choose Delete, the image represented by the tile",
    "       is deleted.  Otherwise, it is displayed.  To return to",
    "       the visual image directory, choose Next from the",
    "       Command widget.",
    "",
    "",
    "COMMAND WIDGET",
    "  The Command widget lists a number of sub-menus and commands.",
    "  They are",
    "",
    "      File",
    "        Load...",
    "        Next",
    "        Former",
    "        Select...",
    "        Write...",
    "        Print...",
    "        Delete...",
    "        Canvas...",
    "        Slide Show",
    "        Visual Directory...",
    "      Edit",
    "        Undo",
    "        Cut",
    "        Copy",
    "        Paste",
    "        Refresh",
    "      Size",
    "        Half Size",
    "        Original Size",
    "        Double Size",
    "        Resize...",
    "        Restore",
    "        Apply",
    "      Pixel Transform",
    "        Crop",
    "        Chop",
    "        Flop",
    "        Flip",
    "        Rotate Right",
    "        Rotate Left",
    "        Rotate...",
    "        Shear...",
    "        Trim Edges",
    "      Color Enhance",
    "        Brightness...",
    "        Saturation...",
    "        Hue...",
    "        Gamma...",
    "        Sharpen...",
    "        Dull",
    "        Equalize",
    "        Normalize",
    "        Negate",
    "        Toggle Colormap",
    "      Effects",
    "        Despeckle",
    "        Peak Noise",
    "        Sharpen",
    "        Blur",
    "        Edge Detect",
    "        Emboss",
    "        Oil Painting",
    "        Segment",
    "        Grayscale",
    "        Quantize...",
    "      Image Edit",
    "        Annotate",
    "        Draw",
    "        Color",
    "        Matte",
    "        Composite...",
    "        Add Border...",
    "        Comment",
    "        Region of Interest",
    "      Info",
    "        Image Info",
    "        Version",
    "        Help",
    "      Quit",
    "",
    "  Menu items with a indented triangle have a sub-menu.  They",
    "  are represented above as the indented items.  To access a",
    "  sub-menu item, move the pointer to the appropriate menu and",
    "  press a button and drag.  When you find the desired sub-menu",
    "  item, release the button and the command is executed.  Move",
    "  the pointer away from the sub-menu if you decide not to",
    "  execute a particular command.",
    "",
    "KEYBOARD ACCELERATORS",
    "  Accelerators are one or two key presses that effect a",
    "  particular command.  The keyboard accelerators that",
    "  display(1) understands is:",
    "",
    "  l    Press to load an image from a file.",
    "",
    "  n    Press to display the next image.",
    "",
    "  f    Press to display the former image.",
    "",
    "  F2   Press to select an image from the command line.",
    "",
    "  w    Press to write the image to a file.",
    "",
    "  p    Press to print the image to a Postscript printer.",
    "",
    "  ^    Press to delete an image file.",
    "",
    "  C    Press to create a blank canvas.",
    "",
    "  ,    Press to display the next image after pausing.",
    "",
    "  V    Press to create a Visual Image Directory.",
    "",
    "  u    Press to undo last image transformation.",
    "",
    "  F3   Press to cut a region of the image.",
    "",
    "  F4   Press to copy a region of the image.",
    "",
    "  F5   Press to paste a region to the image.",
    "",
    "  r    Press to restore the image to its original size.",
    "",
    "  @    Press to refresh the image window.",
    "",
    "  <    Press to half the image size.",
    "",
    "  o    Press to return to the original image size.",
    "",
    "  >    Press to double the image size.",
    "",
    "  %    Press to resize the image to a width and height you",
    "       specify.",
    "",
    "  [    Press to crop the image.",
    "",
    "  ]    Press to chop the image.",
    "",
    "  |    Press to flop image in the horizontal direction.",
    "",
    "  -    Press to flip image in the vertical direction.",
    "",
    "  /    Press to rotate the image 90 degrees clockwise.",
    "",
    " \\    Press to rotate the image 90 degrees counter-clockwise.",
    "",
    "  *    Press to rotate the image the number of degrees you",
    "       specify.",
    "",
    "  s    Press to shear the image the number of degrees you",
    "       specify.",
    "",
    "  t    Press to trim the image edges.",
    "",
    "  F7   Press to vary the color brightness.",
    "",
    "  F8   Press to vary the color saturation.",
    "",
    "  F9   Press to vary the image hue.",
    "",
    "  g    Press to gamma correct the image.",
    "",
    "  F10  Press to sharpen the image contrast.",
    "",
    "  F11  Press to dull the image contrast.",
    "",
    "  =    Press to perform histogram equalization on the image.",
    "",
    "  N    Press to perform histogram normalization on the image.",
    "",
    "  ~    Press to negate the colors of the image.",
    "",
    "  F12  toggle the colormap type: : Shared or Private",
    "",
    "  D    Press to reduce the speckles in an image.",
    "",
    "  P    Press to eliminate peak noise from an image.",
    "",
    "  S    Press to sharpen an image.",
    "",
    "  B    Press to delete an image file.",
    "",
    "  E    Press to detect edges within an image.",
    "",
    "  M    Press to emboss an image.",
    "",
    "  O    Press to oil paint an image.",
    "",
    "  Z    Press to segment the image by color.",
    "",
    "  G    Press to convert the image colors to gray.",
    "",
    "  #    Press to set the maximum number of unique colors in the",
    "       image.",
    "",
    "  a    Press to annotate the image with text.",
    "",
    "  d    Press to draw on an image.",
    "",
    "  c    Press to edit an image pixel color.",
    "",
    "  m    Press to edit the image matte information.",
    "",
    "  x    Press to composite the image with another.",
    "",
    "  b    Press to add a border to the image.",
    "",
    "  !    Press to add an image comment.",
    "",
    "  R    Press to apply image processing techniques to a region",
    "       of interest.",
    "",
    "  i    Press to display information about the image.",
    "",
    "  v    Press to display the version number of display(1).",
    "",
    "  h    Press to display helpful information about display(1).",
    "",
    "       Function keys HELP or F1 are synonymous with the h key.",
    "",
    "  q    Press to discard all images and exit program.",
    "",
    "  1-9  Press to change the level of magnification.",
    "",
    "  Use the arrow keys to move the image one pixel up, down,",
    "  left, or right within the magnify window.  Be sure to first",
    "  map the magnify window by pressing button 2.",
    "",
    "  Press ALT and one of the arrow keys to trim off one pixel",
    "  from any side of the image.",
    (char *) NULL,
  },
  *ImageMatteEditHelp[]=
  {
    "Matte information within an image is useful for some",
    "operations such as image compositing (See IMAGE",
    "COMPOSITING).  This extra channel usually defines a mask",
    "which represents a sort of a cookie-cutter for the image.",
    "This is the case when matte is 255 (full coverage) for",
    "pixels inside the shape, zero outside, and between zero and",
    "255 on the boundary.",
    "",
    "A small window appears showing the location of the cursor in",
    "the image window. You are now in matte edit mode.  To exit",
    "immediately, press Return.  In matte edit mode, the Command",
    "widget has these options:",
    "",
    "    Method",
    "      point",
    "      replace",
    "      floodfill",
    "    Matte",
    "    Undo",
    "    Help",
    "    Return",
    "",
    "Choose a matte editing method from the Method sub-menu of",
    "the Command widget.  The point method changes the matte value",
    "of any pixel selected with the pointer until the button is",
    "is released.  The replace method changes the matte value of",
    "any pixel that matches the color of the pixel you select with",
    "a button press.  Floodfill changes the matte value of any pixel",
    "that matches the color of the pixel you select with a button",
    "press and is a neighbor.",
    "",
    "Choose Matte Value and a dialog appears requesting a matte",
    "value.  Enter a value between 0 and 255.  This value is",
    "assigned as the matte value of the selected pixel or pixels.",
    "",
    "Now, press any button to select a pixel within the image",
    "window to change its matte value.  If the Magnify widget is",
    "mapped, it can be helpful in positioning your pointer within",
    "the image (refer to button 2).",
    "",
    "Matte information is only valid in a DirectClass image.",
    "Therefore, any PseudoClass image is promoted to DirectClass",
    "(see miff(5)).  Note that matte information for PseudoClass",
    "is not retained for colormapped X server visuals (e.g.",
    "StaticColor, StaticColor, GrayScale, PseudoColor) unless you",
    "immediately save your image to a file (refer to Write).",
    "Correct matte editing behavior may require a TrueColor or",
    "DirectColor visual or a Standard Colormap.",
    (char *) NULL,
  },
  *ImagePanHelp[]=
  {
    "When an image exceeds the width or height of the X server",
    "screen, display maps a small panning icon.  The rectangle",
    "within the panning icon shows the area that is currently",
    "displayed in the the image window.  To pan about the image,",
    "press any button and drag the pointer within the panning",
    "icon.  The pan rectangle moves with the pointer and the",
    "image window is updated to reflect the location of the",
    "rectangle within the panning icon.  When you have selected",
    "the area of the image you wish to view, release the button.",
    "",
    "Use the arrow keys to pan the image one pixel up, down,",
    "left, or right within the image window.",
    "",
    "The panning icon is withdrawn if the image becomes smaller",
    "than the dimensions of the X server screen.",
    (char *) NULL,
  },
  *ImagePasteHelp[]=
  {
    "A small window appears showing the location of the cursor in",
    "the image window. You are now in paste mode.  To exit",
    "immediately, press Return.  In paste mode, the Command",
    "widget has these options:",
    "",
    "    Operators",
    "      over",
    "      in",
    "      out",
    "      atop",
    "      xor",
    "      plus",
    "      minus",
    "      add",
    "      subtract",
    "      difference",
    "      replace",
    "    Help",
    "    Return",
    "",
    "Choose a composite operation from the Operators sub-menu of",
    "the Command widget.  How each operator behaves is described",
    "below.  Image window is the image currently displayed on",
    "your X server and image is the image obtained with the File",
    "Browser widget.",
    "",
    "over     The result is the union of the two image shapes,",
    "         with image obscuring image window in the region of",
    "         overlap.",
    "",
    "in       The result is simply image cut by the shape of",
    "         image window.  None of the image data of image",
    "         window is in the result.",
    "",
    "out      The resulting image is image with the shape of",
    "         image window cut out.",
    "",
    "atop     The result is the same shape as image image window,",
    "         with image obscuring image window where the image",
    "         shapes overlap.  Note this differs from over",
    "         because the portion of image outside image window's",
    "         shape does not appear in the result.",
    "",
    "xor      The result is the image data from both image and",
    "         image window that is outside the overlap region.",
    "         The overlap region is blank.",
    "",
    "plus     The result is just the sum of the image data.",
    "         Output values are cropped to 255 (no overflow).",
    "         This operation is independent of the matte",
    "         channels.",
    "",
    "minus    The result of image - image window, with underflow",
    "         cropped to zero.  The matte channel is ignored (set",
    "         to 255, full coverage).",
    "",
    "add      The result of image + image window, with overflow",
    "         wrapping around (mod 256).",
    "",
    "subtract The result of image - image window, with underflow",
    "         wrapping around (mod 256).  The add and subtract",
    "         operators can be used to perform reversible",
    "         transformations.",
    "",
    "difference",
    "         The result of abs(image - image window).  This is",
    "         useful for comparing two very similar images.",
    "",
    "replace  The resulting image is image window replaced with",
    "         image.  Here the matte information is ignored.",
    "",
    "The image compositor requires a matte, or alpha channel in",
    "the image for some operations.  This extra channel usually",
    "defines a mask which represents a sort of a cookie-cutter",
    "for the image.  This is the case when matte is 255 (full",
    "coverage) for pixels inside the shape, zero outside, and",
    "between zero and 255 on the boundary.  If image does not",
    "have a matte channel, it is initialized with 0 for any pixel",
    "matching in color to pixel location (0,0), otherwise 255.",
    "",
    "Note that matte information for image window is not retained",
    "for colormapped X server visuals (e.g. StaticColor,",
    "StaticColor, GrayScale, PseudoColor).  Correct compositing",
    "behavior may require a TrueColor or DirectColor visual or a",
    "Standard Colormap.",
    "",
    "Choosing a composite operator is optional.  The default",
    "operator is replace.  However, you must choose a location to",
    "paste your image and press a button.  Press and hold the",
    "button before releasing and an outline of the image will",
    "appear to help you identify your location.",
    "",
    "The actual colors of the pasted image is saved.  However,",
    "the color that appears in image window may be different.",
    "For example, on a monochrome screen image window will appear",
    "black or white even though your pasted image may have",
    "many colors.  If the image is saved to a file it is written",
    "with the correct colors.  To assure the correct colors are",
    "saved in the final image, any PseudoClass image is promoted",
    "to DirectClass (see miff(5)).  To force a PseudoClass image",
    "to remain PseudoClass, use -colors.",
    (char *) NULL,
  },
  *ImageROIHelp[]=
  {
    "In region of interest mode, the Command widget has these",
    "options:",
    "",
    "    Help",
    "    Return",
    "",
    "To define a region of interest, press button 1 and drag.",
    "The region of interest is defined by a highlighted rectangle",
    "that expands or contracts as it follows the pointer.  Once",
    "you are satisfied with the region of interest, release the",
    "button.  You are now in apply mode.  In apply mode the",
    "Command widget has these options:",
    "",
    "    File",
    "      Image Info",
    "    Edit",
    "      Undo",
    "    Pixel Transform",
    "      Flop",
    "      Flip",
    "      Rotate Right",
    "      Rotate Left",
    "    Color Enhance",
    "      Brightness...",
    "      Saturation...",
    "      Hue...",
    "      Gamma...",
    "      Sharpen...",
    "      Dull",
    "      Equalize",
    "      Normalize",
    "      Negate",
    "    Effects",
    "      Despeckle",
    "      Peak Noise",
    "      Sharpen",
    "      Blur",
    "      Edge Detect",
    "      Emboss",
    "      Oil Painting",
    "      Segment",
    "      Grayscale",
    "      Quantize...",
    "    Help",
    "    Return",
    "",
    "You can make adjustments to the region of interest by moving",
    "the pointer to one of the rectangle corners, pressing a",
    "button, and dragging.  Finally, choose an image processing",
    "technique from the Command widget.  You can choose more than",
    "one image processing technique to apply to an area.",
    "Alternatively, you can move the region of interest before",
    "applying another image processing technique.  To exit, press",
    "Return.",
    (char *) NULL,
  },
  *ImageRotateHelp[]=
  {
    "In rotate mode, the Command widget has these options:",
    "",
    "    Pixel Color",
    "      black",
    "      blue",
    "      cyan",
    "      green",
    "      gray",
    "      red",
    "      magenta",
    "      yellow",
    "      white",
    "      Browser...",
    "    Direction",
    "      horizontal",
    "      vertical",
    "    Help",
    "    Return",
    "",
    "Choose a background color from the Pixel Color sub-menu.",
    "Additional background colors can be specified with the color",
    "browser.  You can change the menu colors by setting the X",
    "resources pen1 through pen9.",
    "",
    "If you choose the color browser and press Grab, you can",
    "select the background color by moving the pointer to the",
    "desired color on the screen and press any button.",
    "",
    "Choose a point in the image window and press this button and",
    "hold.  Next, move the pointer to another location in the",
    "image.  As you move a line connects the initial location and",
    "the pointer.  When you release the button, the degree of",
    "image rotation is determined by the slope of the line you",
    "just drew.  The slope is relative to the direction you",
    "choose from the Direction sub-menu of the Command widget.",
    "",
    "To cancel the image rotation, move the pointer back to the",
    "starting point of the line and release the button.",
    (char *) NULL,
  };

static Display
  *display;

static Image
  *copy_image = (Image *) NULL;

static XWindows
  *windows;

/*
  Forward declarations.
*/
static Image
  *XLoadImage _Declare((Display *,XResourceInfo *,XWindows *,unsigned int,
    Image *)),
  *XMagickCommand _Declare((Display *,XResourceInfo *,XWindows *,unsigned int,
    KeySym,Image **)),
  *XTileImage _Declare((Display *,XResourceInfo *,XWindows *,Image *,XEvent *)),
  *XVisualDirectoryImage _Declare((Display *,XResourceInfo *,XWindows *));

static int
  XScreenEvent _Declare((Display *,XEvent *,char *));

static unsigned int
  XConfigureImage _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  XMatteEditImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XNoisyImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XOilPaintImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XPasteImage _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  XPrintImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XRotateImage _Declare((Display *,XResourceInfo *,XWindows *,double,Image **)),
  XROIImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XSharpenImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XShearImage _Declare((Display *,XResourceInfo *,XWindows *,Image **)),
  XTrimImage _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  XWriteImage _Declare((Display *,XResourceInfo *,XWindows *,Image **));

static void
  XConfigureImageColormap _Declare((Display *,XResourceInfo *,XWindows *,
    Image *)),
  XDrawPanRectangle _Declare((Display *,XWindows *)),
  XMagnifyImage _Declare((Display *,XWindows *,XEvent *)),
  XMakePanImage _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  XPanImage _Declare((Display *,XWindows *,XEvent *)),
  XMagnifyWindowCommand _Declare((Display *,XWindows *,KeySym)),
  XSetCropGeometry _Declare((Display *,XWindows *,RectangleInfo *,Image *)),
  XTranslateImage _Declare((Display *,XWindows *,Image *,KeySym)),
  XWarning _Declare((char *,char *));
