home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / newsprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  56.7 KB  |  2,033 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * newsprint plug-in
  5.  * Copyright (C) 1997-1998 Austin Donnelly <austin@gimp.org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * Portions of this plug-in are copyright 1991-1992 Adobe Systems
  22.  * Incorporated.  See the spot_PS*() functions for details.
  23.  */
  24.  
  25. /*
  26.  * version 0.52
  27.  * This version requires gtk-1.0.4 or above.
  28.  *
  29.  * This plug-in puts an image through a screen at a particular angle
  30.  * and lines per inch, to arrive at a newspaper-style effect.
  31.  *
  32.  * Austin Donnelly <austin@greenend.org.uk>
  33.  * http://www.cl.cam.ac.uk/~and1000/newsprint/
  34.  *
  35.  * Richard Mortier <rmm1002@cam.ac.uk> did the spot_round() function
  36.  * with correct tonal balance.
  37.  *
  38.  * Tim Harris <tim.harris@acm.org> provided valuable feedback on
  39.  * pre-press issues.
  40.  *
  41.  *
  42.  * 0.52: 10 Jan 1999  <austin@greenend.org.uk>
  43.  *    gtk_label_set() -> gtk_label_set_text()
  44.  */
  45.  
  46. #include "config.h"
  47.  
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51.  
  52. #include <gtk/gtk.h>
  53.  
  54. #include <libgimp/gimp.h>
  55. #include <libgimp/gimpui.h>
  56.  
  57. #include "libgimp/stdplugins-intl.h"
  58.  
  59.  
  60. #ifdef RCSID
  61. static char rcsid[] = "$Id: newsprint.c,v 1.27 2000/08/22 01:26:54 neo Exp $";
  62. #endif
  63.  
  64. #define VERSION "v0.52"
  65.  
  66. /* Some useful macros */
  67. #ifdef DEBUG
  68. #define DEBUG_PRINT(x) printf x
  69. #else
  70. #define DEBUG_PRINT(x)
  71. #endif
  72.  
  73. /* HACK so we compile with old gtks.  Won't work on machines where the
  74.  * size of an int is different from the size of a void*, eg Alphas */
  75. #ifndef GINT_TO_POINTER
  76. #warning glib did not define GINT_TO_POINTER, assuming same size as int
  77. #define GINT_TO_POINTER(x) ((gpointer)(x))
  78. #endif
  79.  
  80. #ifndef GPOINTER_TO_INT
  81. #warning glib did not define GPOINTER_TO_INT, assuming same size as int
  82. #define GPOINTER_TO_INT(x) ((int)(x))
  83. #endif
  84.  
  85.  
  86. /*#define TIMINGS*/
  87.  
  88. #ifdef TIMINGS
  89. #include <sys/time.h>
  90. #include <unistd.h>
  91. #define TM_INIT()    struct timeval tm_start, tm_end
  92. #define TM_START()    gettimeofday(&tm_start, NULL)
  93. #define TM_END()                             \
  94. do {                                     \
  95.     gettimeofday(&tm_end, NULL);                     \
  96.     tm_end.tv_sec  -= tm_start.tv_sec;                     \
  97.     tm_end.tv_usec -= tm_start.tv_usec;                     \
  98.     if (tm_end.tv_usec < 0)                         \
  99.     {                                     \
  100.     tm_end.tv_usec += 1000000;                     \
  101.     tm_end.tv_sec  -= 1;                         \
  102.     }                                     \
  103.     printf("operation took %ld.%06ld\n", tm_end.tv_sec, tm_end.tv_usec); \
  104. } while(0)
  105. #else
  106. #define TM_INIT()
  107. #define TM_START()
  108. #define TM_END()
  109. #endif
  110.  
  111. #define TILE_CACHE_SIZE     16
  112. #define SCALE_WIDTH        125
  113. #define DEF_OVERSAMPLE         1 /* default for how much to oversample by */
  114. #define SPOT_PREVIEW_SZ        16
  115. #define PREVIEW_OVERSAMPLE   3
  116.  
  117.  
  118. #define ISNEG(x)    (((x) < 0)? 1 : 0)
  119. #define DEG2RAD(d)    ((d) * G_PI / 180)
  120. #define VALID_BOOL(x)    ((x) == TRUE || (x) == FALSE)
  121. #define CLAMPED_ADD(a, b) (((a)+(b) > 0xff)? 0xff : (a) + (b))
  122.  
  123.  
  124. /* Bartlett window supersampling weight function.  See table 4.1, page
  125.  * 123 of Alan Watt and Mark Watt, Advanced Animation and Rendering
  126.  * Techniques, theory and practice. Addison-Wesley, 1992. ISBN
  127.  * 0-201-54412-1 */
  128. #define BARTLETT(x,y)    (((oversample/2)+1-ABS(x)) * ((oversample/2)+1-ABS(y)))
  129. #define WGT(x,y)    wgt[((y+oversample/2)*oversample) + x+oversample/2]
  130.  
  131. /* colourspaces we can separate to: */
  132. #define CS_GREY        0
  133. #define CS_RGB        1
  134. #define CS_CMYK        2
  135. #define CS_INTENSITY    3
  136. #define NUM_CS        4
  137. #define VALID_CS(x)    ((x) >= 0 && (x) <= NUM_CS-1)
  138.  
  139. /* Spot function related types and definitions */
  140.  
  141. typedef gdouble spotfn_t (gdouble x, gdouble y);
  142.  
  143. /* forward declaration of the functions themselves */
  144. static spotfn_t spot_round;
  145. static spotfn_t spot_line;
  146. static spotfn_t spot_diamond;
  147. static spotfn_t spot_PSsquare;
  148. static spotfn_t spot_PSdiamond;
  149.  
  150. typedef struct
  151. {
  152.   const gchar *name;        /* function's name */
  153.   spotfn_t    *fn;          /* function itself */
  154.   guchar      *thresh;      /* cached threshold matrix */
  155.   gdouble      prev_lvl[3]; /* intensities to preview */
  156.   guchar      *prev_thresh; /* preview-sized threshold matrix */
  157.   gint         balanced;    /* TRUE if spot fn is already balanced */
  158. } spot_info_t;
  159.  
  160.  
  161. /* This is all the info needed per spot function.  Functions are refered to
  162.  * by their index into this array. */
  163. static spot_info_t spotfn_list[] =
  164. {
  165. #define SPOTFN_DOT 0
  166.   {
  167.     N_("Round"),
  168.     spot_round,
  169.     NULL,
  170.     { .22, .50, .90 },
  171.     NULL,
  172.     FALSE
  173.   },
  174.  
  175.   {
  176.     N_("Line"),
  177.     spot_line,
  178.     NULL,
  179.     { .15, .50, .80 },
  180.     NULL,
  181.     FALSE
  182.   },
  183.  
  184.   {
  185.     N_("Diamond"),
  186.     spot_diamond,
  187.     NULL,
  188.     { .15, .50, .80 },
  189.     NULL,
  190.     TRUE
  191.   },
  192.  
  193.   { N_("PS Square (Euclidean Dot)"),
  194.     spot_PSsquare,
  195.     NULL,
  196.     { .15, .50, .90 },
  197.     NULL,
  198.     FALSE
  199.   },
  200.  
  201.   {
  202.     N_("PS Diamond"),
  203.     spot_PSdiamond,
  204.     NULL,
  205.     { .15, .50, .90 },
  206.     NULL,
  207.     FALSE
  208.   },
  209.  
  210.     /* NULL-name terminates */
  211.   { NULL,
  212.     NULL,
  213.     NULL,
  214.     { 0.0, 0.0, 0.0 },
  215.     NULL,
  216.     FALSE
  217.   }
  218. };
  219.  
  220. #define NUM_SPOTFN    ((sizeof(spotfn_list) / sizeof(spot_info_t)) - 1)
  221. #define VALID_SPOTFN(x)    ((x) >= 0 && (x) < NUM_SPOTFN)
  222. #define THRESH(x,y)    (thresh[(y)*width + (x)])
  223. #define THRESHn(n,x,y)    ((thresh[n])[(y)*width + (x)])
  224.     
  225.  
  226. /* Arguments to filter */
  227.  
  228. /* Some of these are here merely to save them across calls.  They are
  229.  * marked as "UI use".  Everything else is a valid arg. */
  230. typedef struct
  231. {
  232.   /* resolution section: */
  233.   gint    cell_width;
  234.  
  235.   /* screening section: */
  236.   gint    colourspace;  /* 0: RGB, 1: CMYK, 2: Intensity */
  237.   gint    k_pullout;    /* percentage of black to pull out */
  238.  
  239.   /* grey screen (only used if greyscale drawable) */
  240.   gdouble gry_ang;
  241.   gint    gry_spotfn;
  242.  
  243.   /* red / cyan screen */
  244.   gdouble red_ang;
  245.   gint    red_spotfn;
  246.  
  247.   /* green / magenta screen */
  248.   gdouble grn_ang;
  249.   gint    grn_spotfn;
  250.  
  251.   /* blue / yellow screen */
  252.   gdouble blu_ang;
  253.   gint    blu_spotfn;
  254.  
  255.   /* anti-alias section */
  256.   gint    oversample;   /* 1 == no anti-aliasing, else small odd int */
  257. } NewsprintValues;
  258.  
  259. /* bits of state used by the UI, but not visible from the PDB */
  260. typedef struct
  261. {
  262.   gint      input_spi;     /* input samples per inch */
  263.   gdouble output_lpi;    /* desired output lines per inch */
  264.   gint      lock_channels; /* changes to one channel affect all */
  265. } NewsprintUIValues;
  266.  
  267. typedef struct
  268. {
  269.   gint run;
  270. } NewsprintInterface;
  271.  
  272.  
  273. /* state for the preview widgets */
  274. typedef struct
  275. {
  276.   GtkWidget *widget;    /* preview widget itself */
  277.   GtkWidget *label;     /* the label below it */
  278. } preview_st;
  279.  
  280. /* state for the channel notebook pages */
  281. typedef struct _channel_st channel_st;
  282.  
  283. struct _channel_st
  284. {
  285.   GtkWidget   *vbox;             /* vbox of this channel */
  286.   gint        *spotfn_num;       /* which spotfn the menu is controlling */
  287.   preview_st   prev[3];          /* state for 3 preview widgets */
  288.   GtkObject   *angle_adj;        /* angle adjustment */
  289.   GtkWidget   *option_menu;      /* popup for spot function */
  290.   GtkWidget   *menuitem[NUM_SPOTFN]; /* menuitems for each spot function */
  291.   GtkWidget   *ch_menuitem;      /* menuitem for the channel selector */
  292.   gint         ch_menu_num;      /* this channel's position in the selector */
  293.   channel_st  *next;             /* circular list of channels in locked group */
  294. };
  295.  
  296.  
  297. /* State associated with the configuration dialog and passed to its
  298.  * callback functions */
  299. typedef struct
  300. {
  301.   GtkWidget  *dlg;             /* main dialog itself */
  302.   GtkWidget  *pull_table;
  303.   GtkObject  *pull;            /* black pullout percentage */
  304.   GtkObject  *input_spi;
  305.   GtkObject  *output_lpi;
  306.   GtkObject  *cellsize;
  307.   GtkWidget  *vbox;            /* container for screen info */    
  308.  
  309.   /* Notebook for the channels (one per colorspace) */
  310.   GtkWidget  *channel_notebook[NUM_CS];
  311.  
  312.   /* room for up to 4 channels per colourspace */
  313.   channel_st *chst[NUM_CS][4];
  314. } NewsprintDialog_st;
  315.  
  316.  
  317. /***** Local vars *****/
  318.  
  319. /* defaults */
  320. /* fixed copy so we can reset them */
  321. static const NewsprintValues factory_defaults =
  322. {
  323.   /* resolution stuff */
  324.   10,          /* cell width */
  325.  
  326.   /* screen setup (default is the classic rosette pattern) */
  327.   CS_RGB,      /* use RGB, not CMYK or Intensity */
  328.   100, /* max pullout */
  329.  
  330.   /* grey/black */
  331.   45.0,
  332.   SPOTFN_DOT,
  333.  
  334.   /* red/cyan */
  335.   15.0,
  336.   SPOTFN_DOT,
  337.  
  338.   /* green/magenta */
  339.   75.0,
  340.   SPOTFN_DOT,
  341.  
  342.   /* blue/yellow */
  343.   0.0,
  344.   SPOTFN_DOT,
  345.  
  346.   /* anti-alias control */
  347.   DEF_OVERSAMPLE
  348. };
  349.  
  350. static const NewsprintUIValues factory_defaults_ui =
  351. {
  352.   72,    /* input spi */
  353.   7.2,   /* output lpi */
  354.   FALSE  /* lock channels */
  355. };
  356.  
  357. /* Mutable copy for normal use.  Initialised in run(). */
  358. static NewsprintValues   pvals;
  359. static NewsprintUIValues pvals_ui;
  360.  
  361. static NewsprintInterface pint =
  362. {
  363.   FALSE      /* run */
  364. };
  365.  
  366.  
  367. /* channel templates */
  368. typedef struct
  369. {
  370.   const gchar   *name;
  371.   /* pointers to the variables this channel updates */
  372.   gdouble       *angle;
  373.   gint          *spotfn;
  374.   /* factory defaults for the angle and spot function (as pointers so
  375.    * the silly compiler can see they're really constants) */
  376.   const gdouble *factory_angle;
  377.   const gint    *factory_spotfn;
  378. } chan_tmpl;
  379.  
  380. static const chan_tmpl grey_tmpl[] =
  381. {
  382.   {
  383.     N_("Grey"),
  384.     &pvals.gry_ang,
  385.     &pvals.gry_spotfn,
  386.     &factory_defaults.gry_ang,
  387.     &factory_defaults.gry_spotfn
  388.   },
  389.  
  390.   { NULL, NULL, NULL, NULL, NULL }
  391. };
  392.  
  393. static const chan_tmpl rgb_tmpl[] =
  394. {
  395.   {
  396.     N_("Red"),
  397.     &pvals.red_ang,
  398.     &pvals.red_spotfn,
  399.     &factory_defaults.red_ang,
  400.     &factory_defaults.red_spotfn
  401.   },
  402.  
  403.   {
  404.     N_("Green"),
  405.     &pvals.grn_ang,
  406.     &pvals.grn_spotfn,
  407.     &factory_defaults.grn_ang,
  408.     &factory_defaults.grn_spotfn
  409.   },
  410.  
  411.   {
  412.     N_("Blue"),
  413.     &pvals.blu_ang,
  414.     &pvals.blu_spotfn,
  415.     &factory_defaults.blu_ang,
  416.     &factory_defaults.blu_spotfn
  417.   },
  418.  
  419.   { NULL, NULL, NULL, NULL, NULL }
  420. };
  421.  
  422. static const chan_tmpl cmyk_tmpl[] =
  423. {
  424.   {
  425.     N_("Cyan"),
  426.     &pvals.red_ang,
  427.     &pvals.red_spotfn,
  428.     &factory_defaults.red_ang,
  429.     &factory_defaults.red_spotfn
  430.   },
  431.  
  432.   {
  433.     N_("Magenta"),
  434.     &pvals.grn_ang,
  435.     &pvals.grn_spotfn,
  436.     &factory_defaults.grn_ang,
  437.     &factory_defaults.grn_spotfn
  438.   },
  439.  
  440.   {
  441.     N_("Yellow"),
  442.     &pvals.blu_ang,
  443.     &pvals.blu_spotfn,
  444.     &factory_defaults.blu_ang,
  445.     &factory_defaults.blu_spotfn
  446.   },
  447.  
  448.   {
  449.     N_("Black"),
  450.     &pvals.gry_ang,
  451.     &pvals.gry_spotfn,
  452.     &factory_defaults.gry_ang,
  453.     &factory_defaults.gry_spotfn
  454.   },
  455.  
  456.   { NULL, NULL, NULL, NULL, NULL }
  457. };
  458.  
  459. static const chan_tmpl intensity_tmpl[] =
  460. {
  461.   {
  462.     N_("Intensity"),
  463.     &pvals.gry_ang,
  464.     &pvals.gry_spotfn,
  465.     &factory_defaults.gry_ang,
  466.     &factory_defaults.gry_spotfn
  467.   },
  468.  
  469.   { NULL, NULL, NULL, NULL, NULL }
  470. };
  471.  
  472. /* cspace_chan_tmpl is indexed by colourspace, and gives an array of
  473.  * channel templates for that colourspace */
  474. static const chan_tmpl *cspace_chan_tmpl[] =
  475. {
  476.   grey_tmpl,
  477.   rgb_tmpl,
  478.   cmyk_tmpl,
  479.   intensity_tmpl
  480. };
  481.  
  482. #define NCHANS(x) ((sizeof(x) / sizeof(chan_tmpl)) - 1)
  483.  
  484. /* cspace_nchans gives a quick way of finding the number of channels
  485.  * in a colourspace.  Alternatively, if you're walking the channel
  486.  * template, you can use the NULL entry at the end to stop. */
  487. static const gint cspace_nchans[] =
  488. {
  489.   NCHANS (grey_tmpl),
  490.   NCHANS (rgb_tmpl),
  491.   NCHANS (cmyk_tmpl),
  492.   NCHANS (intensity_tmpl)
  493. };
  494.  
  495.  
  496. /* Declare local functions.  */
  497. static void    query    (void);
  498. static void    run    (gchar     *name,
  499.              gint     nparams,
  500.              GimpParam     *param,
  501.              gint     *nreturn_vals,
  502.              GimpParam     **return_vals);
  503.  
  504. static gint    newsprint_dialog        (GimpDrawable *drawable);
  505. static void    newsprint_ok_callback   (GtkWidget *widget,
  506.                      gpointer   data);
  507. static void    newsprint_cspace_update (GtkWidget *widget,
  508.                      gpointer   data);
  509.  
  510. static void    newsprint    (GimpDrawable *drawable);
  511. static guchar *    spot2thresh    (gint       type,
  512.                  gint       width);
  513.  
  514.  
  515. GimpPlugInInfo PLUG_IN_INFO =
  516. {
  517.   NULL,     /* init_proc  */
  518.   NULL,     /* quit_proc  */
  519.   query, /* query_proc */
  520.   run     /* run_proc   */
  521. };
  522.  
  523.  
  524. /***** Functions *****/
  525.  
  526. MAIN ()
  527.  
  528. static void
  529. query (void)
  530. {
  531.   static GimpParamDef args[]=
  532.   {
  533.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  534.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  535.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  536.  
  537.     { GIMP_PDB_INT32, "cell_width", "screen cell width, in pixels" },
  538.  
  539.     { GIMP_PDB_INT32, "colorspace", "separate to 0:RGB, 1:CMYK, 2:Intensity" },
  540.     { GIMP_PDB_INT32, "k_pullout", "Percentage of black to pullout (CMYK only)" },
  541.  
  542.     { GIMP_PDB_FLOAT, "gry_ang", "Grey/black screen angle (degrees)" },
  543.     { GIMP_PDB_INT32, "gry_spotfn", "Grey/black spot function (0=dots, 1=lines, 2=diamonds, 3=euclidean dot, 4=PS diamond)" },
  544.     { GIMP_PDB_FLOAT, "red_ang", "Red/cyan screen angle (degrees)" },
  545.     { GIMP_PDB_INT32, "red_spotfn", "Red/cyan spot function (values as gry_spotfn)" },
  546.     { GIMP_PDB_FLOAT, "grn_ang", "Green/magenta screen angle (degrees)" },
  547.     { GIMP_PDB_INT32, "grn_spotfn", "Green/magenta spot function (values as gry_spotfn)" },
  548.     { GIMP_PDB_FLOAT, "blu_ang", "Blue/yellow screen angle (degrees)" },
  549.     { GIMP_PDB_INT32, "blu_spotfn", "Blue/yellow spot function (values as gry_spotfn)" },
  550.  
  551.     { GIMP_PDB_INT32, "oversample", "how many times to oversample spot fn" }
  552.     /* 15 args */
  553.   };
  554.   static gint nargs = sizeof (args) / sizeof (args[0]);
  555.  
  556.   gimp_install_procedure ("plug_in_newsprint",
  557.               "Re-sample the image to give a newspaper-like effect",
  558.               "Halftone the image, trading off resolution to "
  559.               "represent colors or grey levels using the process "
  560.               "described both in the PostScript language "
  561.               "definition, and also by Robert Ulichney, \"Digital "
  562.               "halftoning\", MIT Press, 1987.",
  563.               "Austin Donnelly",
  564.               "Austin Donnelly",
  565.               "1998 (" VERSION ")",
  566.               N_("<Image>/Filters/Distorts/Newsprint..."),
  567.               "RGB*, GRAY*",
  568.               GIMP_PLUGIN,
  569.               nargs, 0,
  570.               args, NULL);
  571. }
  572.  
  573. static void
  574. run (gchar   *name,
  575.      gint    nparams,
  576.      GimpParam  *param,
  577.      gint    *nreturn_vals,
  578.      GimpParam  **return_vals)
  579. {
  580.   static GimpParam values[1];
  581.   GimpDrawable *drawable;
  582.   GimpRunModeType run_mode;
  583.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  584.  
  585.   run_mode = param[0].data.d_int32;
  586.  
  587.   *nreturn_vals = 1;
  588.   *return_vals = values;
  589.  
  590.   values[0].type = GIMP_PDB_STATUS;
  591.   values[0].data.d_status = status;
  592.  
  593.   /* basic defaults */
  594.   pvals    = factory_defaults;
  595.   pvals_ui = factory_defaults_ui;
  596.  
  597.   /*  Get the specified drawable  */
  598.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  599.  
  600.   switch (run_mode)
  601.     {
  602.     case GIMP_RUN_INTERACTIVE:
  603.       INIT_I18N_UI();
  604.       /*  Possibly retrieve data  */
  605.       gimp_get_data ("plug_in_newsprint", &pvals);
  606.       gimp_get_data ("plug_in_newsprint_ui", &pvals_ui);
  607.  
  608.       /*  First acquire information with a dialog  */
  609.       if (! newsprint_dialog (drawable))
  610.     {
  611.       gimp_drawable_detach (drawable);
  612.       return;
  613.     }
  614.       break;
  615.  
  616.     case GIMP_RUN_NONINTERACTIVE:
  617.       INIT_I18N();
  618.       /*  Make sure all the arguments are there!  */
  619.       if (nparams != 15)
  620.     {
  621.       status = GIMP_PDB_CALLING_ERROR;
  622.       break;
  623.     }
  624.  
  625.       pvals.cell_width  = param[3].data.d_int32;
  626.       pvals.colourspace = param[4].data.d_int32;
  627.       pvals.k_pullout   = param[5].data.d_int32;
  628.       pvals.gry_ang     = param[6].data.d_float;
  629.       pvals.gry_spotfn  = param[7].data.d_int32;
  630.       pvals.red_ang     = param[8].data.d_float;
  631.       pvals.red_spotfn  = param[9].data.d_int32;
  632.       pvals.grn_ang     = param[10].data.d_float;
  633.       pvals.grn_spotfn  = param[11].data.d_int32;
  634.       pvals.blu_ang     = param[12].data.d_float;
  635.       pvals.blu_spotfn  = param[13].data.d_int32;
  636.       pvals.oversample  = param[14].data.d_int32;
  637.  
  638.       /* check values are within permitted ranges */
  639.       if (!VALID_SPOTFN (pvals.gry_spotfn) ||
  640.       !VALID_SPOTFN (pvals.red_spotfn) ||
  641.       !VALID_SPOTFN (pvals.grn_spotfn) ||
  642.       !VALID_SPOTFN (pvals.blu_spotfn) ||
  643.       !VALID_CS (pvals.colourspace) ||
  644.       pvals.k_pullout < 0 || pvals.k_pullout > 100)
  645.     {
  646.       status = GIMP_PDB_CALLING_ERROR;
  647.     }
  648.       break;
  649.  
  650.     case GIMP_RUN_WITH_LAST_VALS:
  651.       INIT_I18N();
  652.       /*  Possibly retrieve data  */
  653.       gimp_get_data ("plug_in_newsprint", &pvals);
  654.       break;
  655.  
  656.     default:
  657.       break;
  658.     }
  659.  
  660.   if (status == GIMP_PDB_SUCCESS)
  661.     {
  662.       /*  Make sure that the drawable is gray or RGB color  */
  663.       if (gimp_drawable_is_rgb (drawable->id) ||
  664.       gimp_drawable_is_gray (drawable->id))
  665.     {
  666.       gimp_progress_init (_("Newsprintifing..."));
  667.  
  668.       /*  set the tile cache size  */
  669.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  670.  
  671.       /*  run the newsprint effect  */
  672.       newsprint (drawable);
  673.  
  674.       if (run_mode != GIMP_RUN_NONINTERACTIVE)
  675.         gimp_displays_flush ();
  676.  
  677.       /*  Store data  */
  678.       if (run_mode == GIMP_RUN_INTERACTIVE)
  679.         {
  680.           gimp_set_data ("plug_in_newsprint",
  681.                  &pvals, sizeof (NewsprintValues));
  682.           gimp_set_data ("plug_in_newsprint_ui",
  683.                  &pvals_ui, sizeof (NewsprintUIValues));
  684.         }
  685.     }
  686.       else
  687.     {
  688.       /*gimp_message ("newsprint: cannot operate on indexed images");*/
  689.       status = GIMP_PDB_EXECUTION_ERROR;
  690.     }
  691.     }
  692.  
  693.   values[0].data.d_status = status;
  694.  
  695.   gimp_drawable_detach (drawable);
  696. }
  697.  
  698.  
  699. /* create new menu state, and the preview widgets for it */
  700. static channel_st *
  701. new_preview (gint *sfn)
  702. {
  703.   channel_st *st;
  704.   GtkWidget  *preview;
  705.   GtkWidget  *label;
  706.   gint        i;
  707.  
  708.   st = g_new (channel_st, 1);
  709.  
  710.   st->spotfn_num = sfn;
  711.  
  712.   /* make the preview widgets */
  713.   for (i = 0; i < 3; i++)
  714.     {
  715.       preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  716.       gtk_preview_size (GTK_PREVIEW (preview),
  717.             SPOT_PREVIEW_SZ*2 + 1, SPOT_PREVIEW_SZ*2 + 1);
  718.       gtk_widget_show (preview);
  719.  
  720.       label = gtk_label_new ("");
  721.       gtk_widget_show (label);
  722.  
  723.       st->prev[i].widget = preview;
  724.       st->prev[i].label  = label;
  725.       /* st->prev[i].value changed by preview_update */
  726.     }
  727.  
  728.   return st;
  729. }
  730.  
  731.  
  732. /* the popup menu "st" has changed, so the previews associated with it 
  733.  * need re-calculation */
  734. static void
  735. preview_update (channel_st *st)
  736. {
  737.   gint        sfn = *(st->spotfn_num);
  738.   preview_st *prev;
  739.   gint        i;
  740.   gint        x;
  741.   gint        y;
  742.   gint        width = SPOT_PREVIEW_SZ * PREVIEW_OVERSAMPLE;
  743.   gint        oversample = PREVIEW_OVERSAMPLE;
  744.   guchar     *thresh;
  745.   gchar       pct[12];
  746.   guchar      value;
  747.   guchar      row[3 * (2 * SPOT_PREVIEW_SZ + 1)];
  748.  
  749.   /* If we don't yet have a preview threshold matrix for this spot
  750.    * function, generate one now. */
  751.   if (!spotfn_list[sfn].prev_thresh)
  752.     {
  753.       spotfn_list[sfn].prev_thresh =
  754.     spot2thresh (sfn, SPOT_PREVIEW_SZ*PREVIEW_OVERSAMPLE);
  755.     }
  756.  
  757.   thresh = spotfn_list[sfn].prev_thresh;
  758.  
  759.   for (i = 0; i < 3; i++)
  760.     {
  761.       prev = &st->prev[i];
  762.  
  763.       value = spotfn_list[sfn].prev_lvl[i] * 0xff;
  764.  
  765.       for (y = 0; y <= SPOT_PREVIEW_SZ*2; y++)
  766.     {
  767.       for (x = 0; x <= SPOT_PREVIEW_SZ*2; x++)
  768.         {
  769.           guint32 sum = 0;
  770.           gint sx, sy;
  771.           gint tx, ty;
  772.           gint rx, ry;
  773.  
  774.           rx = x * PREVIEW_OVERSAMPLE;
  775.           ry = y * PREVIEW_OVERSAMPLE;
  776.  
  777.           for (sy = -oversample/2; sy <= oversample/2; sy++)
  778.         for (sx = -oversample/2; sx <= oversample/2; sx++)
  779.           {
  780.             tx = rx+sx;
  781.             ty = ry+sy;
  782.             while (tx < 0)  tx += width;
  783.             while (ty < 0)  ty += width;
  784.             while (tx >= width)  tx -= width;
  785.             while (ty >= width)  ty -= width;
  786.  
  787.             if (value > THRESH (tx, ty))
  788.               sum += 0xff * BARTLETT (sx, sy);
  789.           }
  790.           sum /= BARTLETT (0, 0) * BARTLETT (0, 0);
  791.  
  792.           /* blue lines on cell boundaries */
  793.           if ((x % SPOT_PREVIEW_SZ) == 0 ||
  794.           (y % SPOT_PREVIEW_SZ) == 0)
  795.         {
  796.           row[x*3+0] = 0;
  797.           row[x*3+1] = 0;
  798.           row[x*3+2] = 0xff;
  799.         }
  800.           else
  801.         {
  802.           row[x*3+0] = sum;
  803.           row[x*3+1] = sum;
  804.           row[x*3+2] = sum;
  805.         }
  806.         }
  807.  
  808.       gtk_preview_draw_row (GTK_PREVIEW (prev->widget),
  809.                 row, 0, y, SPOT_PREVIEW_SZ*2+1);
  810.     }
  811.  
  812.       /* redraw preview widget */
  813.       gtk_widget_draw (prev->widget, NULL);
  814.  
  815.       g_snprintf (pct, sizeof (pct), "%2d%%",
  816.           (int) RINT (spotfn_list[sfn].prev_lvl[i] * 100));
  817.       gtk_label_set_text (GTK_LABEL(prev->label), pct);
  818.     }
  819.  
  820.   gdk_flush ();
  821. }
  822.  
  823.     
  824. static void
  825. newsprint_menu_callback (GtkWidget *widget,
  826.              gpointer   data)
  827. {
  828.   channel_st  *st = data;
  829.   gpointer     ud;
  830.   gint         menufn;
  831.   static gint  in_progress = FALSE;
  832.  
  833.   /* we shouldn't need recursion protection, but if lock_channels is
  834.    * set, and gtk_option_menu_set_history ever generates an
  835.    * "activated" signal, then we'll get back here.  So we've defensive. */
  836.   if (in_progress)
  837.     {
  838.       printf ("newsprint_menu_callback: unexpected recursion: can't happen\n");
  839.       return;
  840.     }
  841.  
  842.   in_progress = TRUE;
  843.  
  844.   ud = gtk_object_get_user_data (GTK_OBJECT (widget));
  845.   menufn = GPOINTER_TO_INT (ud);
  846.  
  847.   *(st->spotfn_num) = menufn;
  848.  
  849.   preview_update (st);
  850.  
  851.   /* ripple the change to the other popups if the channels are
  852.    * locked together. */
  853.   if (pvals_ui.lock_channels)
  854.     {
  855.       channel_st *c = st->next;
  856.       gint        oldfn;
  857.  
  858.       while (c != st)
  859.     {
  860.       gtk_option_menu_set_history (GTK_OPTION_MENU (c->option_menu),
  861.                        menufn);
  862.       oldfn = *(c->spotfn_num);
  863.       *(c->spotfn_num) = menufn;
  864.       if (oldfn != menufn)
  865.         preview_update (c);
  866.       c = c->next;
  867.     }
  868.     }
  869.  
  870.   in_progress = FALSE;
  871. }
  872.  
  873.  
  874. static void
  875. angle_callback (GtkAdjustment *adjustment,
  876.         gpointer       data)
  877. {
  878.   channel_st *st = data;
  879.   channel_st *c;
  880.   gdouble    *angle_ptr;
  881.   static gint in_progress = FALSE;
  882.  
  883.   angle_ptr = gtk_object_get_user_data (GTK_OBJECT (adjustment));
  884.  
  885.   gimp_double_adjustment_update (adjustment, angle_ptr);
  886.  
  887.   if (pvals_ui.lock_channels && !in_progress)
  888.     {
  889.       in_progress = TRUE;
  890.  
  891.       c = st->next;
  892.  
  893.       while (c != st)
  894.     {
  895.       gtk_adjustment_set_value (GTK_ADJUSTMENT (c->angle_adj), *angle_ptr);
  896.       c = c->next;
  897.     }
  898.  
  899.       in_progress = FALSE;
  900.     }
  901. }
  902.  
  903. static void
  904. spi_callback (GtkAdjustment *adjustment,
  905.           gpointer       data)
  906. {
  907.   NewsprintDialog_st *st = data;
  908.  
  909.   gimp_double_adjustment_update (adjustment, &pvals_ui.input_spi);
  910.  
  911.   gtk_signal_handler_block_by_data (GTK_OBJECT (st->output_lpi), data);
  912.  
  913.   gtk_adjustment_set_value (GTK_ADJUSTMENT (st->output_lpi),
  914.                 pvals_ui.input_spi / pvals.cell_width);
  915.  
  916.   gtk_signal_handler_unblock_by_data (GTK_OBJECT (st->output_lpi), data);
  917. }
  918.  
  919. static void
  920. lpi_callback (GtkAdjustment *adjustment,
  921.           gpointer       data)
  922. {
  923.   NewsprintDialog_st *st = data;
  924.  
  925.   gimp_double_adjustment_update (adjustment, &pvals_ui.output_lpi);
  926.  
  927.   gtk_signal_handler_block_by_data (GTK_OBJECT (st->cellsize), data);
  928.  
  929.   gtk_adjustment_set_value (GTK_ADJUSTMENT (st->cellsize),
  930.                 pvals_ui.input_spi / pvals_ui.output_lpi);
  931.  
  932.   gtk_signal_handler_unblock_by_data (GTK_OBJECT (st->cellsize), data);
  933. }
  934.  
  935. static void
  936. cellsize_callback (GtkAdjustment *adjustment,
  937.            gpointer       data)
  938. {
  939.   NewsprintDialog_st *st = data;
  940.  
  941.   gimp_int_adjustment_update (adjustment, &pvals.cell_width);
  942.  
  943.   gtk_signal_handler_block_by_data (GTK_OBJECT (st->output_lpi), data);
  944.  
  945.   gtk_adjustment_set_value (GTK_ADJUSTMENT (st->output_lpi),
  946.                 pvals_ui.input_spi / pvals.cell_width);
  947.  
  948.   gtk_signal_handler_unblock_by_data (GTK_OBJECT (st->output_lpi), data);
  949. }
  950.  
  951. static void
  952. newsprint_defaults_callback (GtkWidget *widget,
  953.                  gpointer   data)
  954. {
  955.   NewsprintDialog_st  *st = data;
  956.   gint                 saved_lock;
  957.   gint                 cspace;
  958.   channel_st         **chst;
  959.   const chan_tmpl     *ct;
  960.   gint                 spotfn;
  961.   gint                 c;
  962.  
  963.   /* temporarily turn off channel lock */
  964.   saved_lock = pvals_ui.lock_channels;
  965.   pvals_ui.lock_channels = FALSE;
  966.  
  967.   /* for each colourspace, reset its channel info */
  968.   for (cspace = 0; cspace < NUM_CS; cspace++)
  969.     {
  970.       chst = st->chst[cspace];
  971.       ct = cspace_chan_tmpl[cspace];
  972.  
  973.       /* skip this colourspace if we haven't used it yet */
  974.       if (!chst[0])
  975.     continue;
  976.  
  977.       c = 0;
  978.       while (ct->name)
  979.     {
  980.       gtk_adjustment_set_value (GTK_ADJUSTMENT (chst[c]->angle_adj),
  981.                     *(ct->factory_angle));
  982.  
  983.       /* change the popup menu and also activate the menuitem in
  984.        * question, in order to run the handler that re-computes
  985.        * the preview area */
  986.       spotfn = *(ct->factory_spotfn);
  987.       gtk_option_menu_set_history (GTK_OPTION_MENU (chst[c]->option_menu),
  988.                        spotfn);
  989.       gtk_menu_item_activate (GTK_MENU_ITEM (chst[c]->menuitem[spotfn]));
  990.  
  991.       c++;
  992.       ct++;
  993.     }
  994.     }
  995.  
  996.   pvals_ui.lock_channels = saved_lock;
  997. }
  998.  
  999. /* Create (but don't yet show) a new vbox for a channel 'widget'.
  1000.  * Return the channel state, so caller can find the vbox and place it
  1001.  * in the notebook. */
  1002. static channel_st *
  1003. new_channel (const chan_tmpl *ct)
  1004. {
  1005.   GtkWidget   *table;
  1006.   GtkWidget   *hbox;
  1007.   GtkWidget   *hbox2;
  1008.   GtkWidget   *abox;
  1009.   GtkWidget   *label;
  1010.   GtkWidget   *menu;
  1011.   spot_info_t *sf;
  1012.   channel_st  *chst;
  1013.   gint         i;    
  1014.  
  1015.   /* create the channel state record */
  1016.   chst = new_preview (ct->spotfn);
  1017.  
  1018.   chst->vbox = gtk_vbox_new (FALSE, 4);
  1019.   gtk_container_set_border_width (GTK_CONTAINER (chst->vbox), 4);
  1020.  
  1021.   table = gtk_table_new (1, 3, FALSE);
  1022.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1023.   gtk_box_pack_start (GTK_BOX (chst->vbox), table, FALSE, FALSE, 0);
  1024.   gtk_widget_show (table);
  1025.  
  1026.   /* angle slider */
  1027.   chst->angle_adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
  1028.                       _("Angle:"), SCALE_WIDTH, 0,
  1029.                       *ct->angle,
  1030.                       -90, 90, 1, 15, 1,
  1031.                       TRUE, 0, 0,
  1032.                       NULL, NULL);
  1033.   gtk_object_set_user_data (chst->angle_adj, ct->angle);
  1034.   gtk_signal_connect (GTK_OBJECT (chst->angle_adj), "value_changed",
  1035.               GTK_SIGNAL_FUNC (angle_callback),
  1036.               chst);
  1037.  
  1038.   /* spot function popup */
  1039.   hbox = gtk_hbox_new (FALSE, 6);
  1040.   gtk_box_pack_start (GTK_BOX (chst->vbox), hbox, FALSE, FALSE, 0);
  1041.   gtk_widget_show (hbox);
  1042.   
  1043.   abox = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
  1044.   gtk_box_pack_start (GTK_BOX (hbox), abox, FALSE, FALSE, 0);
  1045.   gtk_widget_show (abox);
  1046.  
  1047.   hbox2 = gtk_hbox_new (FALSE, 4);
  1048.   gtk_container_add (GTK_CONTAINER (abox), hbox2);
  1049.   gtk_widget_show (hbox2);
  1050.  
  1051.   label = gtk_label_new( _("Spot Function:"));
  1052.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  1053.   gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
  1054.   gtk_widget_show (label);
  1055.  
  1056.   chst->option_menu = gtk_option_menu_new ();
  1057.   gtk_box_pack_start (GTK_BOX (hbox2), chst->option_menu, FALSE, FALSE, 0);
  1058.   gtk_widget_show (chst->option_menu);
  1059.  
  1060.   menu = gtk_menu_new ();
  1061.  
  1062.   sf = spotfn_list;
  1063.   i = 0;
  1064.   while (sf->name)
  1065.     {
  1066.       chst->menuitem[i] = gtk_menu_item_new_with_label( gettext(sf->name));
  1067.       gtk_signal_connect (GTK_OBJECT (chst->menuitem[i]), "activate",
  1068.               GTK_SIGNAL_FUNC (newsprint_menu_callback),
  1069.               chst);
  1070.       gtk_object_set_user_data (GTK_OBJECT (chst->menuitem[i]),
  1071.                 GINT_TO_POINTER (i));
  1072.       gtk_widget_show (chst->menuitem[i]);
  1073.       gtk_menu_append (GTK_MENU (menu), GTK_WIDGET (chst->menuitem[i]));
  1074.       sf++;
  1075.       i++;
  1076.     }
  1077.  
  1078.   gtk_menu_set_active (GTK_MENU (menu), *ct->spotfn);
  1079.  
  1080.   gtk_option_menu_set_menu (GTK_OPTION_MENU (chst->option_menu), menu);
  1081.   gtk_widget_show (chst->option_menu);
  1082.  
  1083.   /* spot function previews */
  1084.   {
  1085.     GtkWidget *sub;
  1086.  
  1087.     sub = gtk_table_new (2, 3, FALSE);
  1088.     gtk_table_set_col_spacings (GTK_TABLE (sub), 6);
  1089.     gtk_table_set_row_spacings (GTK_TABLE (sub), 1);
  1090.     gtk_box_pack_start (GTK_BOX (hbox), sub, FALSE, FALSE, 0);
  1091.  
  1092.     for (i = 0; i < 3; i++)
  1093.       {
  1094.     gtk_table_attach (GTK_TABLE (sub), chst->prev[i].widget,
  1095.               i, i+1, 0, 1,
  1096.               GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
  1097.  
  1098.     gtk_table_attach (GTK_TABLE (sub), chst->prev[i].label,
  1099.               i, i+1, 1, 2,
  1100.               GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
  1101.       }
  1102.  
  1103.     gtk_widget_show (sub);
  1104.   }
  1105.  
  1106.   /* fire the update once to make sure we start with something
  1107.    * in the preview windows */
  1108.   preview_update (chst);
  1109.  
  1110.   gtk_widget_show (table);
  1111.  
  1112.   /* create the menuitem used to select this channel for editing */
  1113.   chst->ch_menuitem = gtk_menu_item_new_with_label (gettext (ct->name));
  1114.   gtk_object_set_user_data (GTK_OBJECT (chst->ch_menuitem), chst);
  1115.   /* signal attachment and showing left to caller */
  1116.  
  1117.   /* deliberately don't show the chst->frame, leave that up to
  1118.    * the caller */
  1119.  
  1120.   return chst;
  1121. }
  1122.  
  1123.  
  1124. /* Make all the channels needed for "colourspace", and fill in
  1125.  * the respective channel state fields in "st". */
  1126. static void
  1127. gen_channels (NewsprintDialog_st *st,
  1128.           gint                colourspace)
  1129. {
  1130.   const chan_tmpl  *ct;
  1131.   channel_st      **chst;
  1132.   channel_st       *base = NULL;
  1133.   gint              i;
  1134.  
  1135.   chst = st->chst[colourspace];
  1136.   ct   = cspace_chan_tmpl[colourspace];
  1137.   i    = 0;
  1138.  
  1139.   st->channel_notebook[colourspace] = gtk_notebook_new ();
  1140.   gtk_box_pack_start (GTK_BOX (st->vbox), st->channel_notebook[colourspace],
  1141.               FALSE, FALSE, 0);
  1142.   gtk_box_reorder_child (GTK_BOX (st->vbox),
  1143.              st->channel_notebook[colourspace], 3);
  1144.   gtk_widget_show (st->channel_notebook[colourspace]);
  1145.  
  1146.   while (ct->name)
  1147.     {
  1148.       chst[i] = new_channel (ct);
  1149.  
  1150.       if (i)
  1151.     chst[i-1]->next = chst[i];
  1152.       else
  1153.     base = chst[i];
  1154.  
  1155.       gtk_notebook_append_page (GTK_NOTEBOOK (st->channel_notebook[colourspace]),
  1156.                 chst[i]->vbox,
  1157.                 gtk_label_new (gettext (ct->name)));
  1158.       gtk_widget_show (chst[i]->vbox);
  1159.  
  1160.       i++;
  1161.       ct++;
  1162.     }
  1163.  
  1164.   /* make the list circular */
  1165.   chst[i-1]->next = base;
  1166. }
  1167.  
  1168.  
  1169. static gint
  1170. newsprint_dialog (GimpDrawable *drawable)
  1171. {
  1172.   /* widgets we need from callbacks stored here */
  1173.   NewsprintDialog_st st;
  1174.   GtkWidget *main_vbox;
  1175.   GtkWidget *frame;
  1176.   GtkWidget *table;
  1177.   GtkObject *adj;
  1178.   GSList *group = NULL;
  1179.   gint    bpp;
  1180.   gint    i;
  1181.  
  1182.   gimp_ui_init ("newsprint", TRUE);
  1183.  
  1184.   /* flag values to say we haven't filled these channel
  1185.    * states in yet */
  1186.   for(i=0; i<NUM_CS; i++)
  1187.     st.chst[i][0] = NULL;
  1188.  
  1189.   /* need to know the bpp, so we can tell if we're doing 
  1190.    * RGB/CMYK or grey style of dialog box */
  1191.   bpp = gimp_drawable_bpp (drawable->id);
  1192.   if (gimp_drawable_has_alpha (drawable->id))
  1193.     bpp--;
  1194.  
  1195.   /* force greyscale if it's the only thing we can do */
  1196.   if (bpp == 1)
  1197.     {
  1198.       pvals.colourspace = CS_GREY;
  1199.     }
  1200.   else
  1201.     {
  1202.       if (pvals.colourspace == CS_GREY)
  1203.     pvals.colourspace = CS_RGB;
  1204.     }
  1205.  
  1206.   st.dlg = gimp_dialog_new (_("Newsprint"), "newsprint",
  1207.                 gimp_standard_help_func, "filters/newsprint.html",
  1208.                 GTK_WIN_POS_MOUSE,
  1209.                 FALSE, TRUE, FALSE,
  1210.  
  1211.                 _("OK"), newsprint_ok_callback,
  1212.                 NULL, NULL, NULL, TRUE, FALSE,
  1213.                 _("Cancel"), gtk_widget_destroy,
  1214.                 NULL, 1, NULL, FALSE, TRUE,
  1215.  
  1216.                 NULL);
  1217.  
  1218.   gtk_signal_connect (GTK_OBJECT (st.dlg), "destroy",
  1219.               GTK_SIGNAL_FUNC (gtk_main_quit),
  1220.               NULL);
  1221.  
  1222.   main_vbox = gtk_vbox_new (FALSE, 4);
  1223.   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
  1224.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (st.dlg)->vbox), main_vbox,
  1225.               TRUE, TRUE, 0);
  1226.   gtk_widget_show (main_vbox);
  1227.  
  1228.   /* resolution settings  */
  1229.   frame = gtk_frame_new (_("Resolution"));
  1230.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1231.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  1232.  
  1233.   table = gtk_table_new (3, 3, FALSE);
  1234.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1235.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  1236.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  1237.   gtk_container_add (GTK_CONTAINER (frame), table);
  1238.  
  1239. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1240.   {
  1241.     double xres, yres;
  1242.     gimp_image_get_resolution (gimp_drawable_image_id( drawable->id),
  1243.                    &xres, &yres);
  1244.     /* XXX hack: should really note both resolutions, and use
  1245.      * rectangular cells, not square cells.  But I'm being lazy,
  1246.      * and the majority of the world works with xres == yres */
  1247.     pvals_ui.input_spi = xres;
  1248.   }
  1249. #endif
  1250.  
  1251.   st.input_spi =
  1252.     gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
  1253.               _("Input SPI:"), SCALE_WIDTH, 0,
  1254.               pvals_ui.input_spi,
  1255.               1.0, 1200.0, 1.0, 10.0, 0,
  1256.               FALSE, GIMP_MIN_RESOLUTION, GIMP_MAX_RESOLUTION,
  1257.               NULL, NULL);
  1258.   gtk_signal_connect (GTK_OBJECT (st.input_spi), "value_changed",
  1259.               GTK_SIGNAL_FUNC (spi_callback),
  1260.               &st);
  1261.  
  1262.   st.output_lpi =
  1263.     gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
  1264.               _("Output LPI:"), SCALE_WIDTH, 0,
  1265.               pvals_ui.output_lpi,
  1266.               1.0, 1200.0, 1.0, 10.0, 1,
  1267.               FALSE, GIMP_MIN_RESOLUTION, GIMP_MAX_RESOLUTION,
  1268.                     NULL, NULL);
  1269.   gtk_signal_connect (GTK_OBJECT (st.output_lpi), "value_changed",
  1270.               GTK_SIGNAL_FUNC (lpi_callback),
  1271.               &st);
  1272.  
  1273.   st.cellsize = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
  1274.                       _("Cell Size:"), SCALE_WIDTH, 0,
  1275.                       pvals.cell_width,
  1276.                       3.0, 100.0, 1.0, 5.0, 0,
  1277.                       FALSE, 3.0, GIMP_MAX_IMAGE_SIZE,
  1278.                       NULL, NULL);
  1279.   gtk_signal_connect (GTK_OBJECT (st.cellsize), "value_changed",
  1280.               GTK_SIGNAL_FUNC (cellsize_callback),
  1281.               &st);
  1282.  
  1283.   gtk_widget_show (table);
  1284.   gtk_widget_show (frame);
  1285.  
  1286.   /* screen settings */
  1287.   frame = gtk_frame_new (_("Screen"));
  1288.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1289.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  1290.  
  1291.   st.vbox = gtk_vbox_new (FALSE, 4);
  1292.   gtk_container_set_border_width (GTK_CONTAINER (st.vbox), 4);
  1293.   gtk_container_add (GTK_CONTAINER (frame), st.vbox);
  1294.  
  1295.   /* optional portion begins */
  1296.   if (bpp != 1)
  1297.     {
  1298.       GtkWidget *hbox;
  1299.       GtkWidget *label;
  1300.       GtkWidget *sep;
  1301.       GtkWidget *button;
  1302.       GtkWidget *toggle;
  1303.  
  1304.       st.pull_table = gtk_table_new (1, 3, FALSE);
  1305.       gtk_table_set_col_spacings (GTK_TABLE (st.pull_table), 4);
  1306.       gtk_table_set_row_spacings (GTK_TABLE (st.pull_table), 2);
  1307.  
  1308.       /* black pullout */
  1309.       st.pull = gimp_scale_entry_new (GTK_TABLE (st.pull_table), 0, 0,
  1310.                       _("Black Pullout (%):"), SCALE_WIDTH, 0,
  1311.                       pvals.k_pullout,
  1312.                       0, 100, 1, 10, 0,
  1313.                       TRUE, 0, 0,
  1314.                       NULL, NULL);
  1315.       gtk_signal_connect (GTK_OBJECT (st.pull), "value_changed",
  1316.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  1317.               &pvals.k_pullout);
  1318.       gtk_widget_set_sensitive (st.pull_table, (pvals.colourspace == CS_CMYK));
  1319.  
  1320.       gtk_widget_show (st.pull_table);
  1321.  
  1322.       /* RGB / CMYK / Intensity select */
  1323.       hbox = gtk_hbox_new (FALSE, 6);
  1324.       gtk_box_pack_start (GTK_BOX (st.vbox), hbox, FALSE, FALSE, 0);
  1325.  
  1326.       sep = gtk_hseparator_new ();
  1327.       gtk_box_pack_start (GTK_BOX (st.vbox), sep, FALSE, FALSE, 0);
  1328.       gtk_widget_show (sep);
  1329.  
  1330.       /*  pack the scaleentry table  */
  1331.       gtk_box_pack_start (GTK_BOX (st.vbox), st.pull_table, FALSE, FALSE, 0);
  1332.  
  1333.       label = gtk_label_new( _("Separate to:"));
  1334.       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  1335.       gtk_widget_show(label);
  1336.  
  1337.       toggle = gtk_radio_button_new_with_label(group, _("RGB"));
  1338.       group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  1339.       gtk_box_pack_start (GTK_BOX (hbox), toggle, TRUE, TRUE, 0);
  1340.       gtk_object_set_user_data(GTK_OBJECT(toggle), &st);
  1341.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  1342.                                     (pvals.colourspace == CS_RGB));
  1343.       gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1344.                           (GtkSignalFunc) newsprint_cspace_update,
  1345.                           GINT_TO_POINTER(CS_RGB));
  1346.       gtk_widget_show (toggle);
  1347.  
  1348.       toggle = gtk_radio_button_new_with_label (group, _("CMYK"));
  1349.       group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  1350.       gtk_box_pack_start (GTK_BOX (hbox), toggle, TRUE, TRUE, 0);
  1351.       gtk_object_set_user_data(GTK_OBJECT(toggle), &st);
  1352.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  1353.                                     (pvals.colourspace == CS_CMYK));
  1354.       gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1355.                           (GtkSignalFunc) newsprint_cspace_update,
  1356.                           GINT_TO_POINTER(CS_CMYK));
  1357.       gtk_widget_show (toggle);
  1358.  
  1359.       toggle = gtk_radio_button_new_with_label (group, _("Intensity"));
  1360.       group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  1361.       gtk_box_pack_start (GTK_BOX (hbox), toggle, TRUE, TRUE, 0);
  1362.       gtk_object_set_user_data(GTK_OBJECT(toggle), &st);
  1363.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  1364.                                     (pvals.colourspace == CS_INTENSITY));
  1365.       gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1366.                           (GtkSignalFunc) newsprint_cspace_update,
  1367.                           GINT_TO_POINTER(CS_INTENSITY));
  1368.       gtk_widget_show (toggle);
  1369.  
  1370.       gtk_widget_show (hbox);
  1371.  
  1372.       /* channel lock & factory defaults button */
  1373.       hbox = gtk_hbutton_box_new ();
  1374.       gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbox), 10);
  1375.       gtk_box_pack_start (GTK_BOX (st.vbox), hbox, FALSE, FALSE, 0);
  1376.       gtk_widget_show (hbox);
  1377.  
  1378.       toggle = gtk_check_button_new_with_label (_("Lock Channels"));
  1379.       gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1380.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1381.               &pvals_ui.lock_channels);
  1382.       gtk_object_set_user_data (GTK_OBJECT (toggle), NULL);
  1383.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  1384.                     pvals_ui.lock_channels);
  1385.       gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
  1386.       gtk_widget_show (toggle);
  1387.  
  1388.       button = gtk_button_new_with_label (_("Factory Defaults"));
  1389.       gtk_signal_connect(GTK_OBJECT (button), "clicked",
  1390.              GTK_SIGNAL_FUNC (newsprint_defaults_callback),
  1391.              &st);
  1392.       gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  1393.       gtk_widget_show (button);
  1394.     }
  1395.  
  1396.   /*  Make the channels appropriate for this colourspace and
  1397.    *  currently selected defaults.  They may have already been
  1398.    *  created as a result of callbacks to cspace_update from
  1399.    *  gtk_toggle_button_set_active().
  1400.    */
  1401.   if (!st.chst[pvals.colourspace][0])
  1402.     {
  1403.       gen_channels (&st, pvals.colourspace);
  1404.     }
  1405.  
  1406.   gtk_widget_show (st.vbox);
  1407.   gtk_widget_show (frame);
  1408.  
  1409.   /* anti-alias control */
  1410.   frame = gtk_frame_new (_("Antialiasing"));
  1411.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1412.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  1413.  
  1414.   table = gtk_table_new (1, 3, FALSE);
  1415.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1416.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  1417.   gtk_container_add (GTK_CONTAINER (frame), table);
  1418.  
  1419.   adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
  1420.                   _("Oversample:"), SCALE_WIDTH, 0,
  1421.                   pvals.oversample,
  1422.                   1.0, 15.0, 1.0, 5.0, 0,
  1423.                   TRUE, 0, 0,
  1424.                   NULL, NULL);
  1425.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  1426.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  1427.               &pvals.oversample);
  1428.  
  1429.   gtk_widget_show (table);
  1430.   gtk_widget_show (frame);
  1431.  
  1432.   gtk_widget_show (st.dlg);
  1433.  
  1434.   gtk_main ();
  1435.   gdk_flush ();
  1436.  
  1437.   return pint.run;
  1438. }
  1439.  
  1440. /*  Newsprint interface functions  */
  1441.  
  1442. static void
  1443. newsprint_ok_callback (GtkWidget *widget,
  1444.                gpointer   data)
  1445. {
  1446.   pint.run = TRUE;
  1447.  
  1448.   gtk_widget_destroy (GTK_WIDGET (data));
  1449. }
  1450.  
  1451. static void
  1452. newsprint_cspace_update (GtkWidget *widget,
  1453.              gpointer   data)
  1454. {
  1455.   NewsprintDialog_st  *st;
  1456.   gint new_cs = GPOINTER_TO_INT (data);
  1457.   gint old_cs = pvals.colourspace;
  1458.  
  1459.   st = gtk_object_get_user_data (GTK_OBJECT (widget));
  1460.  
  1461.   if (!st)
  1462.     printf ("newsprint: cspace_update: no state, can't happen!\n");
  1463.  
  1464.   if (st)
  1465.     {
  1466.       /* the CMYK widget looks after the black pullout widget */
  1467.       if (new_cs == CS_CMYK)
  1468.     {
  1469.       gtk_widget_set_sensitive (st->pull_table,
  1470.                     GTK_TOGGLE_BUTTON (widget)->active);
  1471.     }
  1472.  
  1473.       /* if we're not activate, then there's nothing more to do */
  1474.       if (!GTK_TOGGLE_BUTTON (widget)->active)
  1475.     return;
  1476.  
  1477.       pvals.colourspace = new_cs;
  1478.  
  1479.       /* make sure we have the necessary channels for the new
  1480.        * colourspace */
  1481.       if (!st->chst[new_cs][0])
  1482.     gen_channels (st, new_cs);
  1483.  
  1484.       /* hide the old channels (if any) */
  1485.       if (st->channel_notebook[old_cs])
  1486.     {
  1487.       gtk_widget_hide (st->channel_notebook[old_cs]);
  1488.     }
  1489.  
  1490.       /* show the new channels */
  1491.       gtk_widget_show (st->channel_notebook[new_cs]);
  1492.  
  1493.       gtk_notebook_set_page (GTK_NOTEBOOK (st->channel_notebook[new_cs]), 0);
  1494.     }
  1495. }
  1496.  
  1497.  
  1498. /*
  1499.  * Newsprint Effect
  1500.  */
  1501.  
  1502. /*************************************************************/
  1503. /* Spot functions */
  1504.  
  1505.  
  1506. /* Spot functions define the order in which pixels should be whitened
  1507.  * as a cell lightened in colour.  They are defined over the entire
  1508.  * cell, and are called over each pixel in the cell.  The cell
  1509.  * co-ordinate space ranges from -1.0 .. +1.0 inclusive, in both x- and
  1510.  * y-axes.
  1511.  *
  1512.  * This means the spot function f(x, y) must be defined for:
  1513.  *     -1 <= x <= +1, where x is a real number,   and
  1514.  *     -1 <= y <= +1, where y is a real number.
  1515.  *
  1516.  * The function f's range is -1.0 .. +1.0 inclusive, but it is
  1517.  * permissible for f to return values outside this range: the nearest
  1518.  * valid value will be used instead.  NOTE: this is in contrast with
  1519.  * PostScript spot functions, where it is a RangeError for the spot
  1520.  * function to go outside these limits.
  1521.  *
  1522.  * An initially black cell is filled from lowest spot function value
  1523.  * to highest.  The actual values returned do not matter - it is their
  1524.  * relative orderings that count.  This means that spot functions do
  1525.  * not need to be tonally balanced.  A tonally balanced spot function
  1526.  * is one which for all slices though the function (eg say at z), the
  1527.  * area of the slice = 4z.  In particular, almost all PostScript spot
  1528.  * functions are _not_ tonally balanced.
  1529.  */
  1530.  
  1531.  
  1532. /* The classic growing dot spot function.  This one isn't tonally
  1533.  * balanced.  It can be made so, but it's _really_ ugly.  Thanks to
  1534.  * Richard Mortier for this one:
  1535.  *
  1536.  * #define a(r) \
  1537.  *     ((r<=1)? G_PI * (r*r) : \
  1538.  *    4 * sqrt(r*r - 1)  +  G_PI*r*r  -  4*r*r*acos(1/r))
  1539.  *
  1540.  *   radius = sqrt(x*x + y*y);
  1541.  *
  1542.  * return a(radius) / 4; */
  1543. static gdouble
  1544. spot_round (gdouble x,
  1545.         gdouble y)
  1546. {
  1547.   return 1 - (x * x + y * y);
  1548. }
  1549.  
  1550. /* Another commonly seen spot function is the v-shaped wedge. Tonally
  1551.  * balanced. */
  1552. static gdouble
  1553. spot_line (gdouble x,
  1554.        gdouble y)
  1555. {
  1556.   return ABS (y);
  1557. }
  1558.  
  1559. /* Square/Diamond dot that never becomes round.  Tonally balanced. */
  1560. static gdouble
  1561. spot_diamond (gdouble x,
  1562.           gdouble y)
  1563. {
  1564.   gdouble xy = ABS (x) + ABS (y);
  1565.   /* spot only valid for 0 <= xy <= 2 */
  1566.   return ((xy <= 1) ?
  1567.       2 * xy * xy :
  1568.       2 * xy * xy - 4 * (xy - 1) * (xy - 1)) / 4;
  1569. }
  1570.  
  1571. /* The following functions were derived from a peice of PostScript by
  1572.  * Peter Fink and published in his book, "PostScript Screening: Adobe
  1573.  * Accurate Screens" (Adobe Press, 1992).  Adobe Systems Incorporated
  1574.  * allow its use, provided the following copyright message is present:
  1575.  *
  1576.  *  % Film Test Pages for Screenset Development
  1577.  *  % Copyright (c) 1991 and 1992 Adobe Systems Incorporated
  1578.  *  % All rights reserved.
  1579.  *  %
  1580.  *  % NOTICE: This code is copyrighted by Adobe Systems Incorporated, and
  1581.  *  % may not be reproduced for sale except by permission of Adobe Systems
  1582.  *  % Incorporated. Adobe Systems Incorporated grants permission to use
  1583.  *  % this code for the development of screen sets for use with Adobe
  1584.  *  % Accurate Screens software, as long as the copyright notice remains
  1585.  *  % intact.
  1586.  *  %
  1587.  *  % By Peter Fink 1991/1992
  1588.  */
  1589.  
  1590. /* Square (or Euclidean) dot.  Also very common. */
  1591. static gdouble
  1592. spot_PSsquare (gdouble x,
  1593.            gdouble y)
  1594. {
  1595.   gdouble ax = ABS (x);
  1596.   gdouble ay = ABS (y);
  1597.  
  1598.   return ((ax + ay) > 1 ?
  1599.       ((ay - 1) * (ay - 1) + (ax - 1) * (ax - 1)) - 1 :
  1600.       1 - (ay * ay + ax * ax));
  1601. }
  1602.  
  1603. /* Diamond spot function, again from Peter Fink's PostScript
  1604.  * original.  Copyright as for previous function. */
  1605. static gdouble
  1606. spot_PSdiamond (gdouble x,
  1607.         gdouble y)
  1608. {
  1609.   gdouble ax = ABS (x);
  1610.   gdouble ay = ABS (y);
  1611.  
  1612.   return ((ax + ay) <= 0.75 ? 1 - (ax * ax + ay * ay) :     /* dot */
  1613.       ((ax + ay) <= 1.23 ?  1 - ((ay * 0.76) + ax) :    /* to diamond (0.76 distort) */
  1614.        ((ay - 1) * (ay - 1) + (ax - 1) * (ax - 1)) -1)); /* back to dot */
  1615. }
  1616.  
  1617. /* end of Adobe Systems Incorporated copyrighted functions */
  1618.  
  1619.  
  1620. /*************************************************************/
  1621. /* Spot function to threshold matrix conversion */
  1622.  
  1623.  
  1624. /* Each call of the spot function results in one of these */
  1625. typedef struct
  1626. {
  1627.   gint    index;  /* (y * width) + x */
  1628.   gdouble value;  /* return value of the spot function */
  1629. } order_t;
  1630.  
  1631. /* qsort(3) compare function */
  1632. static gint
  1633. order_cmp (const void *va,
  1634.        const void *vb)
  1635. {
  1636.   const order_t *a = va;
  1637.   const order_t *b = vb;
  1638.  
  1639.   return (a->value < b->value) ? -1 : ((a->value > b->value)? + 1 : 0);
  1640. }
  1641.  
  1642. /* Convert spot function "type" to a threshold matrix of size "width"
  1643.  * times "width".  Returns newly allocated threshold matrix.  The
  1644.  * reason for qsort()ing the results rather than just using the spot
  1645.  * function's value directly as the threshold value is that we want to
  1646.  * ensure that the threshold matrix is tonally balanced - that is, for
  1647.  * a threshold value of x%, x% of the values in the matrix are < x%.
  1648.  *
  1649.  * Actually, it turns out that qsort()ing a function which is already
  1650.  * balanced can quite significantly detract from the quality of the
  1651.  * final result.  This is particularly noticable with the line or
  1652.  * diamond spot functions at 45 degrees.  This is because if the spot
  1653.  * function has multiple locations with the same value, qsort may use
  1654.  * them in any order.  Often, there is quite clearly an optimal order
  1655.  * however.  By marking functions as pre-balanced, this random
  1656.  * shuffling is avoided.  WARNING: a non-balanced spot function marked
  1657.  * as pre-balanced is bad: you'll end up with dark areas becoming too
  1658.  * dark or too light, and vice versa for light areas.  This is most
  1659.  * easily checked by halftoning an area, then bluring it back - you
  1660.  * should get the same colour back again.  The only way of getting a
  1661.  * correctly balanced function is by getting a formula for the spot's
  1662.  * area as a function of x and y - this can be fairly tough (ie
  1663.  * possiblly an integral in two dimensions that must be solved
  1664.  * analytically).
  1665.  *
  1666.  * The threshold matrix is used to compare against image values.  If
  1667.  * the image value is greater than the threshold value, then the
  1668.  * output pixel is illuminated.  This means that a threshold matrix
  1669.  * entry of 0 never causes output pixels to be illuminated.  */
  1670. static guchar *
  1671. spot2thresh (gint type,
  1672.          gint width)
  1673. {
  1674.   gdouble   sx, sy;
  1675.   gdouble   val;
  1676.   spotfn_t *spotfn;
  1677.   guchar   *thresh;
  1678.   order_t  *order;
  1679.   gint      x, y;
  1680.   gint      i;
  1681.   gint      wid2 = width * width;
  1682.   gint      balanced = spotfn_list[type].balanced;
  1683.  
  1684.   thresh = g_new (guchar, wid2);
  1685.   spotfn = spotfn_list[type].fn;
  1686.  
  1687.   order = g_new (order_t, wid2);
  1688.  
  1689.   i = 0;
  1690.   for (y = 0; y < width; y++)
  1691.     {
  1692.       for (x = 0; x < width; x++)
  1693.     {
  1694.       /* scale x & y to -1 ... +1 inclusive */
  1695.       sx = (((gdouble)x) / (width-1) - 0.5) * 2;
  1696.       sy = (((gdouble)y) / (width-1) - 0.5) * 2;
  1697.       val = spotfn(sx, sy);
  1698.       val = CLAMP (val, -1, 1);  /* interval is inclusive */
  1699.  
  1700.       order[i].index = i;
  1701.       order[i].value = val;
  1702.       i++;
  1703.     }
  1704.     }
  1705.  
  1706.   if (!balanced)
  1707.     {
  1708.       /* now sort array of (point, value) pairs in ascending order */
  1709.       qsort (order, wid2, sizeof (order_t), order_cmp);
  1710.     }
  1711.  
  1712.   /* compile threshold matrix in order from darkest to lightest */
  1713.   for (i = 0; i < wid2; i++)
  1714.     {
  1715.       /* thresh[] contains values from 0 .. 254.  The reason for not
  1716.        * including 255 is so that an image value of 255 remains
  1717.        * unmolested.  It would be bad to filter a completely white
  1718.        * image and end up with black speckles.  */
  1719.       if (balanced)
  1720.     thresh[order[i].index] = order[i].value * 0xfe;
  1721.       else
  1722.     thresh[order[i].index] = i * 0xff / wid2;
  1723.     }
  1724.  
  1725.   g_free (order);
  1726.  
  1727.   /* TODO: this is where the code to apply a transfer or dot gain
  1728.    * function to the threshold matrix would go. */
  1729.  
  1730.   return thresh;
  1731. }
  1732.  
  1733.  
  1734. /**************************************************************/
  1735. /* Main loop */
  1736.  
  1737.  
  1738. /* This function operates on the image, striding across it in tiles. */
  1739. static void
  1740. newsprint (GimpDrawable *drawable)
  1741. {
  1742.   GimpPixelRgn  src_rgn, dest_rgn;
  1743.   guchar    *src_row, *dest_row;
  1744.   guchar    *src, *dest;
  1745.   guchar    *thresh[4];
  1746.   gdouble    r;
  1747.   gdouble    theta;
  1748.   gdouble    rot[4];
  1749.   gdouble    k_pullout;
  1750.   gint       bpp, colour_bpp;
  1751.   gint       has_alpha;
  1752.   gint       b;
  1753.   gint       tile_width;
  1754.   gint       width;
  1755.   gint       row, col;
  1756.   gint       x, y, x_step, y_step;
  1757.   gint       x1, y1, x2, y2;
  1758.   gint       rx, ry;
  1759.   gint       progress, max_progress;
  1760.   gint       oversample;
  1761.   gint       colourspace;
  1762.   gpointer   pr;
  1763.   gint       w002;
  1764.  
  1765.   TM_INIT ();
  1766.  
  1767.   width = pvals.cell_width;
  1768.  
  1769.   if (width < 0)
  1770.     width = -width;
  1771.   if (width < 1)
  1772.     width = 1;
  1773.  
  1774.   oversample = pvals.oversample;
  1775.   k_pullout = ((gdouble) pvals.k_pullout) / 100.0;
  1776.  
  1777.   width *= oversample;
  1778.  
  1779.   tile_width = gimp_tile_width ();
  1780.  
  1781.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  1782.  
  1783.   bpp        = gimp_drawable_bpp (drawable->id);
  1784.   has_alpha  = gimp_drawable_has_alpha (drawable->id);
  1785.   colour_bpp = has_alpha ? bpp-1 : bpp;
  1786.   colourspace= pvals.colourspace;
  1787.   if (bpp == 1)
  1788.     {
  1789.       colourspace = CS_GREY;
  1790.     }
  1791.   else
  1792.     {
  1793.       if (colourspace == CS_GREY)
  1794.     colourspace = CS_RGB;
  1795.     }
  1796.  
  1797.   /* Bartlett window matrix optimisation */
  1798.   w002 = BARTLETT (0, 0) * BARTLETT (0, 0);
  1799. #if 0
  1800.   /* It turns out to be slightly slower to cache a pre-computed
  1801.    * bartlett matrix!   I put it down to d-cache pollution *shrug* */
  1802.   wgt = g_new (gint, oversample * oversample);
  1803.   for (y = -oversample / 2; y <= oversample / 2; y++)
  1804.     for (x = -oversample / 2; x<=oversample / 2; x++)
  1805.       WGT (x, y) = BARTLETT (x, y);
  1806. #endif /* 0 */
  1807.  
  1808. #define ASRT(_x)                        \
  1809. do {                                \
  1810.     if (!VALID_SPOTFN(_x))                    \
  1811.     {                                \
  1812.     printf("newsprint: %d is not a valid spot type\n", _x);    \
  1813.     _x = SPOTFN_DOT;                    \
  1814.     }                                \
  1815. } while(0)
  1816.  
  1817.   /* calculate the RGB / CMYK rotations and threshold matrices */
  1818.   if (bpp == 1 || colourspace == CS_INTENSITY)
  1819.     {
  1820.       rot[0]    = DEG2RAD (pvals.gry_ang);    
  1821.       thresh[0] = spot2thresh (pvals.gry_spotfn, width);
  1822.     }
  1823.   else
  1824.     {
  1825.       gint rf = pvals.red_spotfn;
  1826.       gint gf = pvals.grn_spotfn;
  1827.       gint bf = pvals.blu_spotfn;
  1828.  
  1829.       rot[0] = DEG2RAD (pvals.red_ang);
  1830.       rot[1] = DEG2RAD (pvals.grn_ang);
  1831.       rot[2] = DEG2RAD (pvals.blu_ang);
  1832.  
  1833.       /* always need at least one threshold matrix */
  1834.       ASRT (rf);
  1835.       spotfn_list[rf].thresh = spot2thresh (rf, width);
  1836.       thresh[0] = spotfn_list[rf].thresh;
  1837.  
  1838.       /* optimisation: only use separate threshold matrices if the
  1839.        * spot functions are actually different */
  1840.       ASRT (gf);
  1841.       if (!spotfn_list[gf].thresh)
  1842.     spotfn_list[gf].thresh = spot2thresh (gf, width);
  1843.       thresh[1] = spotfn_list[gf].thresh;
  1844.  
  1845.       ASRT (bf);
  1846.       if (!spotfn_list[bf].thresh)
  1847.     spotfn_list[bf].thresh = spot2thresh (bf, width);
  1848.       thresh[2] = spotfn_list[bf].thresh;
  1849.  
  1850.       if (colourspace == CS_CMYK)
  1851.     {
  1852.       rot[3] = DEG2RAD (pvals.gry_ang);
  1853.       gf = pvals.gry_spotfn;
  1854.       ASRT (gf);
  1855.       if (!spotfn_list[gf].thresh)
  1856.         spotfn_list[gf].thresh = spot2thresh (gf, width);    
  1857.       thresh[3] = spotfn_list[gf].thresh;
  1858.     }
  1859.     }
  1860.  
  1861.   /* Initialize progress */
  1862.   progress     = 0;
  1863.   max_progress = (x2 - x1) * (y2 - y1);
  1864.  
  1865.   TM_START ();
  1866.  
  1867.   for (y = y1; y < y2; y += tile_width - (y % tile_width))
  1868.     {
  1869.       for (x = x1; x < x2; x += tile_width - (x % tile_width))
  1870.     {
  1871.       /* snap to tile boundary */
  1872.       x_step = tile_width - (x % tile_width);
  1873.       y_step = tile_width - (y % tile_width);
  1874.       /* don't step off the end of the image */
  1875.       x_step = MIN (x_step, x2-x);
  1876.       y_step = MIN (y_step, y2-y);
  1877.  
  1878.       /* set up the source and dest regions */
  1879.       gimp_pixel_rgn_init (&src_rgn, drawable, x, y, x_step, y_step,
  1880.                    FALSE/*dirty*/, FALSE/*shadow*/);
  1881.  
  1882.       gimp_pixel_rgn_init (&dest_rgn, drawable, x, y, x_step, y_step,
  1883.                    TRUE/*dirty*/, TRUE/*shadow*/);
  1884.  
  1885.       /* page in the image, one tile at a time */
  1886.       for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
  1887.            pr != NULL;
  1888.            pr = gimp_pixel_rgns_process (pr))
  1889.         {
  1890.           src_row  = src_rgn.data;
  1891.           dest_row = dest_rgn.data;
  1892.  
  1893.           for (row = 0; row < src_rgn.h; row++)
  1894.         {
  1895.           src  = src_row;
  1896.           dest = dest_row;
  1897.           for (col = 0; col < src_rgn.w; col++)
  1898.             {
  1899.               guchar data[4];
  1900.  
  1901.               rx = (x + col) * oversample;
  1902.               ry = (y + row) * oversample;
  1903.  
  1904.               /* convert rx and ry to polar (r, theta) */
  1905.               r = sqrt (((double)rx)*rx + ((double)ry)*ry);
  1906.               theta = atan2 (((gdouble)ry), ((gdouble)rx));
  1907.  
  1908.               for (b = 0; b < colour_bpp; b++)
  1909.             data[b] = src[b];
  1910.  
  1911.               /* do colour space conversion */
  1912.               switch (colourspace)
  1913.             {
  1914.             case CS_CMYK:
  1915.               data[3] = 0xff;
  1916.  
  1917.               data[0] = 0xff - data[0];
  1918.               data[3] = MIN (data[3], data[0]);
  1919.               data[1] = 0xff - data[1];
  1920.               data[3] = MIN (data[3], data[1]);
  1921.               data[2] = 0xff - data[2];
  1922.               data[3] = MIN (data[3], data[2]);
  1923.  
  1924.               data[3] = ((gdouble)data[3]) * k_pullout;
  1925.  
  1926.               data[0] -= data[3];
  1927.               data[1] -= data[3];
  1928.               data[2] -= data[3];
  1929.               break;
  1930.  
  1931.             case CS_INTENSITY:
  1932.               data[3] = data[0]; /* save orig for later */
  1933.               data[0] = INTENSITY (data[0], data[1], data[2]);
  1934.               break;
  1935.  
  1936.             default:
  1937.               break;
  1938.             }
  1939.             
  1940.               for (b = 0; b < cspace_nchans[colourspace]; b++)
  1941.             {
  1942.               rx = RINT (r * cos (theta + rot[b]));
  1943.               ry = RINT (r * sin (theta + rot[b]));
  1944.  
  1945.               /* Make sure rx and ry are positive and within
  1946.                * the range 0 .. width-1 (incl).  Can't use %
  1947.                * operator, since its definition on negative
  1948.                * numbers is not helpful.  Can't use ABS(),
  1949.                * since that would cause reflection about the
  1950.                * x- and y-axes.  Relies on integer division
  1951.                * rounding towards zero. */
  1952.               rx -= ((rx - ISNEG (rx) * (width-1)) / width) * width;
  1953.               ry -= ((ry - ISNEG (ry) * (width-1)) / width) * width;
  1954.  
  1955.               {
  1956.                 guint32 sum = 0;
  1957.                 gint sx, sy;
  1958.                 gint tx, ty;
  1959.                 for (sy = -oversample/2; sy <= oversample/2; sy++)
  1960.                   for (sx = -oversample/2; sx <= oversample/2; sx++)
  1961.                 {
  1962.                   tx = rx+sx;
  1963.                   ty = ry+sy;
  1964.                   while (tx < 0)  tx += width;
  1965.                   while (ty < 0)  ty += width;
  1966.                   while (tx >= width)  tx -= width;
  1967.                   while (ty >= width)  ty -= width;
  1968.                   if (data[b] > THRESHn(b, tx, ty))
  1969.                     sum += 0xff * BARTLETT(sx, sy);
  1970.                 }
  1971.                 sum /= w002;
  1972.                 data[b] = sum;
  1973.               }
  1974.             }
  1975.               if (has_alpha)
  1976.             dest[colour_bpp] = src[colour_bpp];
  1977.  
  1978.               /* re-pack the colours into RGB */
  1979.               switch (colourspace)
  1980.             {
  1981.             case CS_CMYK:
  1982.               data[0] = CLAMPED_ADD (data[0], data[3]);
  1983.               data[1] = CLAMPED_ADD (data[1], data[3]);
  1984.               data[2] = CLAMPED_ADD (data[2], data[3]);
  1985.               data[0] = 0xff - data[0];
  1986.               data[1] = 0xff - data[1];
  1987.               data[2] = 0xff - data[2];
  1988.               break;
  1989.  
  1990.             case CS_INTENSITY:
  1991.               if (has_alpha)
  1992.                 {
  1993.                   dest[colour_bpp] = data[0];
  1994.                   data[0] = 0xff;
  1995.                 }
  1996.               data[1] = data[1] * data[0] / 0xff;
  1997.               data[2] = data[2] * data[0] / 0xff;
  1998.               data[0] = data[3] * data[0] / 0xff;
  1999.               break;
  2000.  
  2001.             default:
  2002.               /* no other special cases */
  2003.               break;
  2004.             }
  2005.  
  2006.               for (b = 0; b < colour_bpp; b++)
  2007.             dest[b] = data[b];
  2008.  
  2009.               src  += src_rgn.bpp;
  2010.               dest += dest_rgn.bpp;
  2011.             }
  2012.           src_row  += src_rgn.rowstride;
  2013.           dest_row += dest_rgn.rowstride;
  2014.         }
  2015.  
  2016.           /* Update progress */
  2017.           progress += src_rgn.w * src_rgn.h;
  2018.           gimp_progress_update ((double) progress / (double) max_progress);
  2019.         }
  2020.     }
  2021.     }
  2022.  
  2023.   TM_END ();
  2024.  
  2025.   /* We don't free the threshold matrices, since we're about to
  2026.    * exit, and the OS should clean up after us. */
  2027.  
  2028.   /* update the affected region */
  2029.   gimp_drawable_flush (drawable);
  2030.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  2031.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  2032. }
  2033.