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 / jpeg.orig.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  59.5 KB  |  1,906 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. /* JPEG loading and saving file filter for the GIMP
  20.  *  -Peter Mattis
  21.  *
  22.  * This filter is heavily based upon the "example.c" file in libjpeg.
  23.  * In fact most of the loading and saving code was simply cut-and-pasted
  24.  *  from that file. The filter, therefore, also uses libjpeg.
  25.  */
  26.  
  27. /* 11-JAN-99 - Added support for JPEG comments and Progressive saves.
  28.  *  -pete whiting <pwhiting@sprint.net>
  29.  *
  30.  * Comments of size up to 32k can be stored in the header of jpeg
  31.  * files.  (They are not compressed.)  The JPEG specs and libraries
  32.  * support the storing of multiple comments.  The behavior of this
  33.  * code is to merge all comments in a loading image into a single
  34.  * comment (putting \n between each) and attach that string as a
  35.  * parasite - gimp-comment - to the image.  When saving, the image is
  36.  * checked to see if it has the gimp-comment parasite - if so, that is
  37.  * used as the default comment in the save dialog.  Further, the other
  38.  * jpeg parameters (quaility, smoothing, compression and progressive)
  39.  * are attached to the image as a parasite.  This allows the
  40.  * parameters to remain consistent between saves.  I was not able to
  41.  * figure out how to determine the quaility, smoothing or compression
  42.  * values of an image as it is being loaded, but the code is there to
  43.  * support it if anyone knows how.  Progressive mode is a method of
  44.  * saving the image such that as a browser (or other app supporting
  45.  * progressive loads - gimp doesn't) loads the image it first gets a
  46.  * low res version displayed and then the image is progressively
  47.  * enhanced until you get the final version.  It doesn't add any size
  48.  * to the image (actually it often results in smaller file size) - the
  49.  * only draw backs are that progressive jpegs are not supported by some
  50.  * older viewers/browsers, and some might find it annoying.
  51.  */
  52.  
  53. /* 
  54.  * 21-AUG-99 - Added support for JPEG previews, subsampling,
  55.  *             non-baseline JPEGs, restart markers and DCT method choice
  56.  * - Steinar H. Gunderson <sgunderson@bigfoot.com>
  57.  *
  58.  * A small preview appears and changes according to the changes to the
  59.  * compression options. The image itself works as a much bigger preview.
  60.  * For slower machines, the save operation (not the load operation) is
  61.  * done in the background, with a standard GTK+ idle loop, which turned
  62.  * out to be the most portable way. Win32 porters shouldn't have much
  63.  * difficulty porting my changes (at least I hope so...).
  64.  *
  65.  * Subsampling is a pretty obscure feature, but I thought it might be nice
  66.  * to have it in anyway, for people to play with :-) Does anybody have
  67.  * any better suggestions than the ones I've put in the menu? (See wizard.doc
  68.  * from the libjpeg distribution for a tiny bit of information on subsampling.)
  69.  *
  70.  * A baseline JPEG is often larger and/or of worse quality than a non-baseline
  71.  * one (especially at low quality settings), but all decoders are guaranteed
  72.  * to read baseline JPEGs (I think). Not all will read a non-baseline one.
  73.  *
  74.  * Restart markers are useful if you are transmitting the image over an
  75.  * unreliable network. If a file gets corrupted, it will only be corrupted
  76.  * up to the next restart marker. Of course, if there are no restart markers,
  77.  * the rest of the image will be corrupted. Restart markers take up extra
  78.  * space. The libjpeg developers recommend a restart every 1 row for
  79.  * transmitting images over unreliable networks, such as Usenet.
  80.  *
  81.  * The DCT method is said by the libjpeg docs to _only_ influence quality vs.
  82.  * speed, and nothing else. However, I've found that there _are_ size
  83.  * differences. Fast integer, on the other hand, is faster than integer,
  84.  * which in turn is faster than FP. According to the libjpeg docs (and I
  85.  * believe it's true), FP has only a theoretical advantage in quality, and
  86.  * will be slower than the two other methods, unless you're blessed with
  87.  * very a fast FPU. (In addition, images might not be identical on
  88.  * different types of FP hardware.)
  89.  *
  90.  * ...and thus ends my additions to the JPEG plug-in. I hope. *sigh* :-)
  91.  */
  92.  
  93. /* 
  94.  * 21-AUG-99 - Bunch O' Fixes.
  95.  * - Adam D. Moss <adam@gimp.org>
  96.  *
  97.  * We now correctly create an alpha-padded layer for our preview --
  98.  * having a non-background non-alpha layer is a no-no in GIMP.
  99.  *
  100.  * I've also tweaked the GIMP undo API a little and changed the JPEG
  101.  * plugin to use gimp_image_{freeze,thaw}_undo so that it doesn't store
  102.  * up a whole load of superfluous tile data every time the preview is
  103.  * changed.
  104.  */
  105.  
  106. /*
  107.  * 22-DEC-99 - volatiles added
  108.  * - Austin Donnelly <austin@gimp.org>
  109.  *
  110.  * When gcc complains a variable may be clobbered by a longjmp or
  111.  * vfork, it means the following: setjmp() was called by the JPEG
  112.  * library for error recovery purposes, but gcc is keeping some
  113.  * variables in registers without updating the memory locations on the
  114.  * stack consistently.  If JPEG error recovery is every invoked, the
  115.  * values of these variable will be inconsistent.  This is almost
  116.  * always a bug, but not one that's commonly seen unless JPEG recovery
  117.  * code is exercised frequently.  The correct solution is to tell gcc
  118.  * to keep the stack version of the affected variables up to date, by
  119.  * using the "volatile" keyword.   Here endeth the lesson.
  120.  */
  121.  
  122.  
  123. #include "config.h"   /* configure cares about HAVE_PROGRESSIVE_JPEG */
  124.  
  125. #include <glib.h>     /* We want glib.h first because of some
  126.                * pretty obscure Win32 compilation issues.
  127.                */
  128. #include <setjmp.h>
  129. #include <signal.h>
  130. #include <stdio.h>
  131. #include <stdlib.h>
  132. #include <string.h>
  133. #include <sys/types.h>
  134. #include <sys/stat.h>
  135. #ifdef HAVE_UNISTD_H
  136. #include <unistd.h>
  137. #endif
  138. #include <jpeglib.h>
  139. #include <jerror.h>
  140.  
  141. #include <libgimp/gimp.h>
  142. #include <libgimp/gimpui.h>
  143.  
  144. #include "libgimp/stdplugins-intl.h"
  145.  
  146.  
  147. #define SCALE_WIDTH         125
  148.  
  149. /* if you are not compiling this from inside the gimp tree, you have to  */
  150. /* take care yourself if your JPEG library supports progressive mode     */
  151. /* #undef HAVE_PROGRESSIVE_JPEG   if your library doesn't support it     */
  152. /* #define HAVE_PROGRESSIVE_JPEG  if your library knows how to handle it */
  153.  
  154. #define DEFAULT_QUALITY     0.75
  155. #define DEFAULT_SMOOTHING   0.0
  156. #define DEFAULT_OPTIMIZE    1
  157. #define DEFAULT_PROGRESSIVE 0
  158. #define DEFAULT_BASELINE    1
  159. #define DEFAULT_SUBSMP      0
  160. #define DEFAULT_RESTART     0
  161. #define DEFAULT_DCT         0
  162. #define DEFAULT_PREVIEW     1
  163.  
  164. #define DEFAULT_COMMENT     "Created with The GIMP"
  165.  
  166. /* sg - these should not be global... */
  167. static gint32 volatile  image_ID_global       = -1;
  168. static gint32           orig_image_ID_global  = -1;
  169. static gint32           drawable_ID_global    = -1;
  170. static gint32           layer_ID_global       = -1;
  171. static GtkWidget       *preview_size          = NULL;
  172. static GimpDrawable    *drawable_global       = NULL;
  173.  
  174. typedef struct
  175. {
  176.   gdouble quality;
  177.   gdouble smoothing;
  178.   gint    optimize;
  179.   gint    progressive;
  180.   gint    baseline;
  181.   gint    subsmp;
  182.   gint    restart;
  183.   gint    dct;
  184.   gint    preview;
  185. } JpegSaveVals;
  186.  
  187. typedef struct
  188. {
  189.   gint  run;
  190. } JpegSaveInterface;
  191.  
  192. typedef struct 
  193. {
  194.   struct jpeg_compress_struct cinfo;
  195.   gint          tile_height;
  196.   FILE         *outfile;
  197.   gint          has_alpha;
  198.   gint          rowstride;
  199.   guchar       *temp;
  200.   guchar       *data;
  201.   guchar       *src;
  202.   GimpDrawable *drawable;
  203.   GimpPixelRgn  pixel_rgn;
  204.   gchar        *file_name;
  205.   gboolean      abort_me;
  206. } PreviewPersistent;
  207.  
  208. static gboolean *abort_me = NULL;
  209.  
  210.  
  211. /* Declare local functions.
  212.  */
  213. static void   query                     (void);
  214. static void   run                       (gchar            *name,
  215.                      gint              nparams,
  216.                      GimpParam        *param,
  217.                      gint             *nreturn_vals,
  218.                      GimpParam       **return_vals);
  219. static gint32 load_image                (gchar            *filename, 
  220.                      GimpRunModeType   runmode, 
  221.                      gboolean          preview);
  222. static gint   save_image                (gchar            *filename,
  223.                      gint32            image_ID,
  224.                      gint32            drawable_ID,
  225.                      gint32            orig_image_ID,
  226.                      gboolean          preview);
  227.  
  228. static gint   save_dialog                (void);
  229.  
  230. static void   save_close_callback        (GtkWidget       *widget,
  231.                       gpointer         data);
  232. static void   save_ok_callback           (GtkWidget       *widget,
  233.                       gpointer         data);
  234. static void   save_restart_toggle_update (GtkWidget       *toggle,
  235.                       GtkAdjustment   *adjustment);
  236. static void   save_restart_update        (GtkAdjustment   *adjustment,
  237.                       GtkWidget       *toggle);
  238.  
  239. static void   make_preview               (void);
  240. static void   destroy_preview            (void);
  241.  
  242. static void   menu_callback              (GtkWidget       *widget, 
  243.                       gpointer         data);
  244.  
  245.  
  246. GimpPlugInInfo PLUG_IN_INFO =
  247. {
  248.   NULL,  /* init_proc  */
  249.   NULL,  /* quit_proc  */
  250.   query, /* query_proc */
  251.   run,   /* run_proc   */
  252. };
  253.  
  254. static JpegSaveVals jsvals =
  255. {
  256.   DEFAULT_QUALITY,
  257.   DEFAULT_SMOOTHING,
  258.   DEFAULT_OPTIMIZE,
  259.   DEFAULT_PROGRESSIVE,
  260.   DEFAULT_BASELINE,
  261.   DEFAULT_SUBSMP,
  262.   DEFAULT_RESTART,
  263.   DEFAULT_DCT,
  264.   DEFAULT_PREVIEW
  265. };
  266.  
  267. static JpegSaveInterface jsint =
  268. {
  269.   FALSE   /*  run  */
  270. };
  271.  
  272.  
  273. static gchar     *image_comment         = NULL;
  274. static GtkWidget *restart_markers_scale = NULL;
  275. static GtkWidget *restart_markers_label = NULL;
  276.  
  277.  
  278. MAIN ()
  279.  
  280. static void
  281. query (void)
  282. {
  283.   static GimpParamDef load_args[] =
  284.   {
  285.     { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
  286.     { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
  287.     { GIMP_PDB_STRING,   "raw_filename", "The name of the file to load" }
  288.   };
  289.   static GimpParamDef load_return_vals[] =
  290.   {
  291.     { GIMP_PDB_IMAGE,   "image",         "Output image" }
  292.   };
  293.   static gint nload_args        = sizeof (load_args) / sizeof (load_args[0]);
  294.   static gint nload_return_vals = (sizeof (load_return_vals) /
  295.                    sizeof (load_return_vals[0]));
  296.  
  297.   static GimpParamDef save_args[] =
  298.   {
  299.     { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
  300.     { GIMP_PDB_IMAGE,    "image",        "Input image" },
  301.     { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
  302.     { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
  303.     { GIMP_PDB_STRING,   "raw_filename", "The name of the file to save the image in" },
  304.     { GIMP_PDB_FLOAT,    "quality",      "Quality of saved image (0 <= quality <= 1)" },
  305.     { GIMP_PDB_FLOAT,    "smoothing",    "Smoothing factor for saved image (0 <= smoothing <= 1)" },
  306.     { GIMP_PDB_INT32,    "optimize",     "Optimization of entropy encoding parameters (0/1)" },
  307.     { GIMP_PDB_INT32,    "progressive",  "Enable progressive jpeg image loading - ignored if not compiled with HAVE_PROGRESSIVE_JPEG (0/1)" },
  308.     { GIMP_PDB_STRING,   "comment",      "Image comment" },
  309.     { GIMP_PDB_INT32,    "subsmp",       "The subsampling option number" },
  310.     { GIMP_PDB_INT32,    "baseline",     "Force creation of a baseline JPEG (non-baseline JPEGs can't be read by all decoders) (0/1)" },
  311.     { GIMP_PDB_INT32,    "restart",      "Frequency of restart markers (in rows, 0 = no restart markers)" },
  312.     { GIMP_PDB_INT32,    "dct",          "DCT algorithm to use (speed/quality tradeoff)" }
  313.   };
  314.   static gint nsave_args = sizeof (save_args) / sizeof (save_args[0]);
  315.  
  316.   gimp_install_procedure ("file_jpeg_load",
  317.                           "loads files in the JPEG file format",
  318.                           "loads files in the JPEG file format",
  319.                           "Spencer Kimball, Peter Mattis & others",
  320.                           "Spencer Kimball & Peter Mattis",
  321.                           "1995-1999",
  322.               "<Load>/Jpeg",
  323.               NULL,
  324.                           GIMP_PLUGIN,
  325.                           nload_args, nload_return_vals,
  326.                           load_args, load_return_vals);
  327.  
  328.   gimp_install_procedure ("file_jpeg_save",
  329.                           "saves files in the JPEG file format",
  330.                           "saves files in the lossy, widely supported JPEG format",
  331.                           "Spencer Kimball, Peter Mattis & others",
  332.                           "Spencer Kimball & Peter Mattis",
  333.                           "1995-1999",
  334.                           "<Save>/JPEG",
  335.               "RGB*, GRAY*",
  336.                           GIMP_PLUGIN,
  337.                           nsave_args, 0,
  338.                           save_args, NULL);
  339.  
  340.   gimp_register_magic_load_handler ("file_jpeg_load",
  341.                     "jpg,jpeg",
  342.                     "",
  343.                     "6,string,JFIF,6,string,Exif");
  344.   gimp_register_save_handler       ("file_jpeg_save",
  345.                     "jpg,jpeg",
  346.                     "");
  347. }
  348.  
  349. static void
  350. run (gchar   *name,
  351.      gint     nparams,
  352.      GimpParam  *param,
  353.      gint    *nreturn_vals,
  354.      GimpParam **return_vals)
  355. {
  356.   static GimpParam values[2];
  357.   GimpRunModeType  run_mode;
  358.   GimpPDBStatusType   status = GIMP_PDB_SUCCESS;
  359.   gint32  image_ID;
  360.   gint32  drawable_ID;
  361.   gint32  orig_image_ID;
  362.   gint32  display_ID = -1;
  363. #ifdef GIMP_HAVE_PARASITES
  364.   GimpParasite *parasite;
  365. #endif /* GIMP_HAVE_PARASITES */
  366.   gint          err;
  367.   GimpExportReturnType export = GIMP_EXPORT_CANCEL;
  368.  
  369.   run_mode = param[0].data.d_int32;
  370.  
  371.   *nreturn_vals = 1;
  372.   *return_vals  = values;
  373.   values[0].type          = GIMP_PDB_STATUS;
  374.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  375.  
  376.   if (strcmp (name, "file_jpeg_load") == 0)
  377.     {
  378.       INIT_I18N();
  379.  
  380.       image_ID = load_image (param[1].data.d_string, run_mode, FALSE);
  381.  
  382.       if (image_ID != -1)
  383.     {
  384.       *nreturn_vals = 2;
  385.       values[1].type         = GIMP_PDB_IMAGE;
  386.       values[1].data.d_image = image_ID;
  387.     }
  388.       else
  389.     {
  390.       status = GIMP_PDB_EXECUTION_ERROR;
  391.     }
  392.     }
  393.   else if (strcmp (name, "file_jpeg_save") == 0)
  394.     {
  395.       INIT_I18N_UI();
  396.  
  397.       image_ID = orig_image_ID = param[1].data.d_int32;
  398.       drawable_ID = param[2].data.d_int32;
  399.  
  400.        /*  eventually export the image */ 
  401.       switch (run_mode)
  402.     {
  403.     case GIMP_RUN_INTERACTIVE:
  404.     case GIMP_RUN_WITH_LAST_VALS:
  405.       gimp_ui_init ("jpeg", FALSE);
  406.       export = gimp_export_image (&image_ID, &drawable_ID, "JPEG", 
  407.                       (GIMP_EXPORT_CAN_HANDLE_RGB |
  408.                        GIMP_EXPORT_CAN_HANDLE_GRAY));
  409.       switch (export)
  410.         {
  411.         case GIMP_EXPORT_EXPORT: 
  412.           display_ID = gimp_display_new (image_ID);
  413.           gimp_image_set_filename (image_ID, _("Export Preview"));
  414.           gimp_displays_flush ();
  415.           break;
  416.         case GIMP_EXPORT_IGNORE:
  417.           break;
  418.         case GIMP_EXPORT_CANCEL:
  419.           values[0].data.d_status = GIMP_PDB_CANCEL;
  420.           return;
  421.           break;
  422.         }
  423.       break;
  424.     default:
  425.       break;
  426.     }
  427.       
  428.       g_free (image_comment);
  429.       image_comment = NULL;
  430.     
  431. #ifdef GIMP_HAVE_PARASITES
  432.       parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
  433.       if (parasite) 
  434.     {
  435.       image_comment = g_strdup (parasite->data);
  436.       gimp_parasite_free (parasite);
  437.     }
  438. #endif /* GIMP_HAVE_PARASITES */
  439.       if (!image_comment) 
  440.     image_comment = g_strdup (DEFAULT_COMMENT);
  441.  
  442.       jsvals.quality     = DEFAULT_QUALITY;
  443.       jsvals.smoothing   = DEFAULT_SMOOTHING;
  444.       jsvals.optimize    = DEFAULT_OPTIMIZE;
  445.       jsvals.progressive = DEFAULT_PROGRESSIVE;
  446.       jsvals.baseline    = DEFAULT_BASELINE;
  447.       jsvals.subsmp      = DEFAULT_SUBSMP;
  448.       jsvals.restart     = DEFAULT_RESTART;
  449.       jsvals.dct         = DEFAULT_DCT;
  450.       jsvals.preview     = DEFAULT_PREVIEW;
  451.  
  452.       switch (run_mode)
  453.     {
  454.     case GIMP_RUN_INTERACTIVE:
  455.       /*  Possibly retrieve data  */
  456.       gimp_get_data ("file_jpeg_save", &jsvals);
  457.  
  458. #ifdef GIMP_HAVE_PARASITES
  459.       /* load up the previously used values */
  460.       parasite = gimp_image_parasite_find (orig_image_ID,
  461.                            "jpeg-save-options");
  462.       if (parasite)
  463.         {
  464.           jsvals.quality     = ((JpegSaveVals *)parasite->data)->quality;
  465.           jsvals.smoothing   = ((JpegSaveVals *)parasite->data)->smoothing;
  466.           jsvals.optimize    = ((JpegSaveVals *)parasite->data)->optimize;
  467.           jsvals.progressive = ((JpegSaveVals *)parasite->data)->progressive;
  468.           jsvals.baseline    = ((JpegSaveVals *)parasite->data)->baseline;
  469.           jsvals.subsmp      = ((JpegSaveVals *)parasite->data)->subsmp;
  470.           jsvals.restart     = ((JpegSaveVals *)parasite->data)->restart;
  471.           jsvals.dct         = ((JpegSaveVals *)parasite->data)->dct;
  472.           jsvals.preview     = ((JpegSaveVals *)parasite->data)->preview;
  473.           gimp_parasite_free (parasite);
  474.         }
  475. #endif /* GIMP_HAVE_PARASITES */
  476.  
  477.       /* we start an undo_group and immediately freeze undo saving
  478.          so that we can avoid sucking up tile cache with our unneeded
  479.          preview steps. */
  480.             gimp_undo_push_group_start (image_ID);
  481.             gimp_image_undo_freeze (image_ID);
  482.  
  483.       /* prepare for the preview */
  484.       image_ID_global = image_ID;
  485.       orig_image_ID_global = orig_image_ID;
  486.       drawable_ID_global = drawable_ID;
  487.  
  488.       /*  First acquire information with a dialog  */
  489.       err = save_dialog ();
  490.  
  491.       /* thaw undo saving and end the undo_group. */
  492.       gimp_image_undo_thaw (image_ID);
  493.       gimp_undo_push_group_end (image_ID); 
  494.  
  495.           if (!err)
  496.         status = GIMP_PDB_CANCEL;
  497.       break;
  498.  
  499.     case GIMP_RUN_NONINTERACTIVE:
  500.       /*  Make sure all the arguments are there!  */
  501.       /*  pw - added two more progressive and comment */
  502.       /*  sg - added subsampling, preview, baseline, restarts and DCT */
  503.       if (nparams != 14)
  504.         {
  505.           status = GIMP_PDB_CALLING_ERROR;
  506.         }
  507.       else
  508.         {
  509.           jsvals.quality     = param[5].data.d_float;
  510.           jsvals.smoothing   = param[6].data.d_float;
  511.           jsvals.optimize    = param[7].data.d_int32;
  512. #ifdef HAVE_PROGRESSIVE_JPEG
  513.           jsvals.progressive = param[8].data.d_int32;
  514. #endif /* HAVE_PROGRESSIVE_JPEG */
  515.           jsvals.baseline    = param[11].data.d_int32;
  516.           jsvals.subsmp      = param[10].data.d_int32;
  517.           jsvals.restart     = param[12].data.d_int32;
  518.           jsvals.dct         = param[13].data.d_int32;
  519.           jsvals.preview     = FALSE;
  520.  
  521.           /* free up the default -- wasted some effort earlier */
  522.           g_free (image_comment);
  523.           image_comment = g_strdup (param[9].data.d_string);
  524.  
  525.           if (jsvals.quality < 0.0 || jsvals.quality > 1.0)
  526.         status = GIMP_PDB_CALLING_ERROR;
  527.           else if (jsvals.smoothing < 0.0 || jsvals.smoothing > 1.0)
  528.         status = GIMP_PDB_CALLING_ERROR;
  529.           else if (jsvals.subsmp < 0 || jsvals.subsmp > 2)
  530.         status = GIMP_PDB_CALLING_ERROR;
  531.           else if (jsvals.dct < 0 || jsvals.dct > 2)
  532.         status = GIMP_PDB_CALLING_ERROR;
  533.         }
  534.       break;
  535.  
  536.     case GIMP_RUN_WITH_LAST_VALS:
  537.       /*  Possibly retrieve data  */
  538.       gimp_get_data ("file_jpeg_save", &jsvals);
  539. #ifdef GIMP_HAVE_PARASITES
  540.       parasite = gimp_image_parasite_find (orig_image_ID,
  541.                            "jpeg-save-options");
  542.       if (parasite)
  543.         {
  544.           jsvals.quality     = ((JpegSaveVals *)parasite->data)->quality;
  545.           jsvals.smoothing   = ((JpegSaveVals *)parasite->data)->smoothing;
  546.           jsvals.optimize    = ((JpegSaveVals *)parasite->data)->optimize;
  547.           jsvals.progressive = ((JpegSaveVals *)parasite->data)->progressive;
  548.           jsvals.baseline    = ((JpegSaveVals *)parasite->data)->baseline;
  549.           jsvals.subsmp      = ((JpegSaveVals *)parasite->data)->subsmp;
  550.           jsvals.restart     = ((JpegSaveVals *)parasite->data)->restart;
  551.           jsvals.dct         = ((JpegSaveVals *)parasite->data)->dct;
  552.           jsvals.preview     = FALSE;
  553.           gimp_parasite_free (parasite);
  554.         }
  555. #endif /* GIMP_HAVE_PARASITES */
  556.       break;
  557.  
  558.     default:
  559.       break;
  560.     }
  561.  
  562.       if (status == GIMP_PDB_SUCCESS)
  563.     {
  564.       if (save_image (param[3].data.d_string,
  565.               image_ID,
  566.               drawable_ID,
  567.               orig_image_ID,
  568.               FALSE))
  569.         {
  570.           /*  Store mvals data  */
  571.           gimp_set_data ("file_jpeg_save", &jsvals, sizeof (JpegSaveVals));
  572.         }
  573.       else
  574.         {
  575.           status = GIMP_PDB_EXECUTION_ERROR;
  576.         }
  577.     }
  578.  
  579.       if (export == GIMP_EXPORT_EXPORT)
  580.     {
  581.       /* If the image was exported, delete the new display. */
  582.       /* This also deletes the image.                       */
  583.  
  584.       if (display_ID > -1)
  585.         gimp_display_delete (display_ID);
  586.       else
  587.         gimp_image_delete (image_ID);
  588.     }
  589.  
  590. #ifdef GIMP_HAVE_PARASITES
  591.       /* pw - now we need to change the defaults to be whatever
  592.        * was used to save this image.  Dump the old parasites
  593.        * and add new ones. */
  594.       
  595.       gimp_image_parasite_detach (orig_image_ID, "gimp-comment");
  596.       if (image_comment && strlen (image_comment)) 
  597.     {
  598.       parasite = gimp_parasite_new ("gimp-comment",
  599.                     GIMP_PARASITE_PERSISTENT,
  600.                     strlen (image_comment) + 1,
  601.                     image_comment);
  602.       gimp_image_parasite_attach (orig_image_ID, parasite);
  603.       gimp_parasite_free (parasite);
  604.     }
  605.       gimp_image_parasite_detach (orig_image_ID, "jpeg-save-options");
  606.       
  607.       parasite = gimp_parasite_new ("jpeg-save-options",
  608.                     0, sizeof (jsvals), &jsvals);
  609.       gimp_image_parasite_attach (orig_image_ID, parasite);
  610.       gimp_parasite_free (parasite);
  611. #endif /* Have Parasites */  
  612.     }
  613.   else
  614.     {
  615.       status = GIMP_PDB_CALLING_ERROR;
  616.     }
  617.  
  618.   values[0].data.d_status = status;
  619. }
  620.  
  621. /* Read next byte */
  622. static guint
  623. jpeg_getc (j_decompress_ptr cinfo)
  624. {
  625.   struct jpeg_source_mgr * datasrc = cinfo->src;
  626.  
  627.   if (datasrc->bytes_in_buffer == 0) 
  628.     {
  629.       if (! (*datasrc->fill_input_buffer) (cinfo))
  630.     ERREXIT (cinfo, JERR_CANT_SUSPEND);
  631.     }
  632.   datasrc->bytes_in_buffer--;
  633.   return *datasrc->next_input_byte++;
  634. }
  635.  
  636. /* We need our own marker parser, since early versions of libjpeg
  637.  * don't keep a conventient list of the marker's that have been
  638.  * seen. */
  639.  
  640. /*
  641.  * Marker processor for COM markers.
  642.  * This replaces the library's built-in processor, which just skips the marker.
  643.  * Note this code relies on a non-suspending data source.
  644.  */
  645. static GString *local_image_comments = NULL;
  646.  
  647. static boolean
  648. COM_handler (j_decompress_ptr cinfo)
  649. {
  650.   int length;
  651.   unsigned int ch;
  652.  
  653.   length = jpeg_getc (cinfo) << 8;
  654.   length += jpeg_getc (cinfo);
  655.   if (length < 2)
  656.     return FALSE;
  657.   length -= 2;            /* discount the length word itself */
  658.  
  659.   if (!local_image_comments)
  660.     local_image_comments = g_string_new (NULL);
  661.   else
  662.     g_string_append_c (local_image_comments, '\n');
  663.  
  664.   while (length-- > 0)
  665.     {
  666.       ch = jpeg_getc (cinfo);
  667.       g_string_append_c (local_image_comments, ch);
  668.     }
  669.  
  670.   return TRUE;
  671. }
  672.  
  673. typedef struct my_error_mgr
  674. {
  675.   struct jpeg_error_mgr pub;            /* "public" fields */
  676.  
  677.   jmp_buf               setjmp_buffer;  /* for return to caller */
  678. } *my_error_ptr;
  679.  
  680. /*
  681.  * Here's the routine that will replace the standard error_exit method:
  682.  */
  683.  
  684. static void
  685. my_error_exit (j_common_ptr cinfo)
  686. {
  687.   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  688.   my_error_ptr myerr = (my_error_ptr) cinfo->err;
  689.  
  690.   /* Always display the message. */
  691.   /* We could postpone this until after returning, if we chose. */
  692.   (*cinfo->err->output_message) (cinfo);
  693.  
  694.   /* Return control to the setjmp point */
  695.   longjmp (myerr->setjmp_buffer, 1);
  696. }
  697.  
  698. static gint32
  699. load_image (gchar           *filename, 
  700.         GimpRunModeType  runmode, 
  701.         gboolean         preview)
  702. {
  703.   GimpPixelRgn pixel_rgn;
  704.   GimpDrawable *drawable;
  705.   gint32 volatile image_ID;
  706.   gint32 layer_ID;
  707.   struct jpeg_decompress_struct cinfo;
  708.   struct my_error_mgr jerr;
  709.   FILE *infile;
  710.   guchar *buf;
  711.   guchar * volatile padded_buf = NULL;
  712.   guchar **rowbuf;
  713.   gchar *name;
  714.   gint image_type;
  715.   gint layer_type;
  716.   gint tile_height;
  717.   gint scanlines;
  718.   gint i, start, end;
  719.  
  720. #ifdef GIMP_HAVE_PARASITES
  721.   JpegSaveVals local_save_vals;
  722.   GimpParasite * volatile comment_parasite = NULL;
  723.   GimpParasite * volatile vals_parasite    = NULL;
  724. #endif /* GIMP_HAVE_PARASITES */
  725.  
  726.   
  727.   /* We set up the normal JPEG error routines. */
  728.   cinfo.err = jpeg_std_error (&jerr.pub);
  729.   jerr.pub.error_exit = my_error_exit;
  730.  
  731.   if ((infile = fopen (filename, "rb")) == NULL)
  732.     {
  733.       g_message (_("can't open \"%s\"\n"), filename);
  734.       gimp_quit ();
  735.     }
  736.  
  737.   if (runmode != GIMP_RUN_NONINTERACTIVE)
  738.     {
  739.       name = g_strdup_printf (_("Loading %s:"), filename);
  740.       gimp_progress_init (name);
  741.       g_free (name);
  742.     }
  743.  
  744.   image_ID = -1;
  745.   /* Establish the setjmp return context for my_error_exit to use. */
  746.   if (setjmp (jerr.setjmp_buffer))
  747.     {
  748.       /* If we get here, the JPEG code has signaled an error.
  749.        * We need to clean up the JPEG object, close the input file, and return.
  750.        */
  751.       jpeg_destroy_decompress (&cinfo);
  752.       if (infile)
  753.     fclose (infile);
  754.       if (image_ID != -1 && !preview)
  755.     gimp_image_delete (image_ID);
  756.       if (preview)
  757.     destroy_preview();
  758.       gimp_quit ();
  759.     }
  760.   /* Now we can initialize the JPEG decompression object. */
  761.   jpeg_create_decompress (&cinfo);
  762.  
  763.   /* Step 2: specify data source (eg, a file) */
  764.  
  765.   jpeg_stdio_src (&cinfo, infile);
  766.  
  767.   /* pw - step 2.1 let the lib know we want the comments. */
  768.  
  769.   jpeg_set_marker_processor (&cinfo, JPEG_COM, COM_handler);
  770.  
  771.   /* Step 3: read file parameters with jpeg_read_header() */
  772.  
  773.   (void) jpeg_read_header (&cinfo, TRUE);
  774.   /* We can ignore the return value from jpeg_read_header since
  775.    *   (a) suspension is not possible with the stdio data source, and
  776.    *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
  777.    * See libjpeg.doc for more info.
  778.    */
  779.  
  780. #ifdef GIMP_HAVE_PARASITES
  781.   if (!preview) {
  782.     /* if we had any comments then make a parasite for them */
  783.     if (local_image_comments && local_image_comments->len) 
  784.       {
  785.     gchar *string = local_image_comments->str;
  786.     g_string_free (local_image_comments, FALSE);
  787.     local_image_comments = NULL;
  788.     comment_parasite = gimp_parasite_new ("gimp-comment",
  789.                           GIMP_PARASITE_PERSISTENT,
  790.                           strlen (string) + 1, string);
  791.       } 
  792.     else 
  793.       {
  794.     comment_parasite = NULL;
  795.       }
  796.   
  797.     /* pw - figuring out what the saved values were is non-trivial.
  798.      * They don't seem to be in the cinfo structure. For now, I will
  799.      * just use the defaults, but if someone figures out how to derive
  800.      * them this is the place to store them. */
  801.  
  802.     local_save_vals.quality     = DEFAULT_QUALITY;
  803.     local_save_vals.smoothing   = DEFAULT_SMOOTHING;
  804.     local_save_vals.optimize    = DEFAULT_OPTIMIZE;
  805.   
  806. #ifdef HAVE_PROGRESSIVE_JPEG 
  807.     local_save_vals.progressive = cinfo.progressive_mode;
  808. #else
  809.     local_save_vals.progressive = 0;
  810. #endif /* HAVE_PROGRESSIVE_JPEG */
  811.     local_save_vals.baseline    = DEFAULT_BASELINE;
  812.     local_save_vals.subsmp      = DEFAULT_SUBSMP;   /* sg - this _is_ there, but I'm too lazy */ 
  813.     local_save_vals.restart     = DEFAULT_RESTART;
  814.     local_save_vals.dct         = DEFAULT_DCT;
  815.     local_save_vals.preview     = DEFAULT_PREVIEW;
  816.   
  817.     vals_parasite = gimp_parasite_new ("jpeg-save-options", 0,
  818.                        sizeof (local_save_vals),
  819.                        &local_save_vals);
  820.   } 
  821. #endif /* GIMP_HAVE_PARASITES */
  822.   
  823.   
  824.   /* Step 4: set parameters for decompression */
  825.  
  826.   /* In this example, we don't need to change any of the defaults set by
  827.    * jpeg_read_header(), so we do nothing here.
  828.    */
  829.  
  830.   /* Step 5: Start decompressor */
  831.  
  832.   jpeg_start_decompress (&cinfo);
  833.  
  834.   /* We may need to do some setup of our own at this point before reading
  835.    * the data.  After jpeg_start_decompress() we have the correct scaled
  836.    * output image dimensions available, as well as the output colormap
  837.    * if we asked for color quantization.
  838.    * In this example, we need to make an output work buffer of the right size.
  839.    */
  840.   /* temporary buffer */
  841.   tile_height = gimp_tile_height ();
  842.   buf = g_new (guchar, 
  843.            tile_height * cinfo.output_width * cinfo.output_components);
  844.   
  845.   if (preview)
  846.     padded_buf = g_new (guchar, tile_height * cinfo.output_width *
  847.             (cinfo.output_components + 1));
  848.  
  849.   rowbuf = g_new (guchar*, tile_height);
  850.  
  851.   for (i = 0; i < tile_height; i++)
  852.     rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
  853.  
  854.   /* Create a new image of the proper size and associate the filename with it.
  855.  
  856.      Preview layers, not being on the bottom of a layer stack, MUST HAVE
  857.      AN ALPHA CHANNEL!
  858.    */
  859.   switch (cinfo.output_components)
  860.     {
  861.     case 1:
  862.       image_type = GIMP_GRAY;
  863.       layer_type = preview ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
  864.       break;
  865.     case 3:
  866.       image_type = GIMP_RGB;
  867.       layer_type = preview ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
  868.       break;
  869.     default:
  870.       g_message ("don't know how to load JPEGs\nwith %d color channels",
  871.                    cinfo.output_components);
  872.       gimp_quit ();
  873.     }
  874.  
  875.   if (preview) 
  876.     {
  877.       image_ID = image_ID_global;
  878.     } 
  879.   else 
  880.     {
  881.       image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
  882.                  image_type);
  883.       gimp_image_set_filename (image_ID, filename);
  884.     }
  885.  
  886.   if (preview) 
  887.     {
  888.       layer_ID_global = layer_ID = gimp_layer_new (image_ID, _("JPEG preview"),
  889.                            cinfo.output_width,
  890.                            cinfo.output_height,
  891.                            layer_type, 100, GIMP_NORMAL_MODE);
  892.     }
  893.   else 
  894.     {
  895.       layer_ID = gimp_layer_new (image_ID, _("Background"),
  896.                  cinfo.output_width,
  897.                  cinfo.output_height,
  898.                  layer_type, 100, GIMP_NORMAL_MODE);
  899.     }
  900.  
  901.   drawable_global = drawable = gimp_drawable_get (layer_ID);
  902.   gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
  903.                drawable->width, drawable->height, TRUE, FALSE);
  904.  
  905. #ifdef GIMP_HAVE_RESOLUTION_INFO
  906.   /* Step 5.1: if the file had resolution information, set it on the image */
  907.   if (!preview && cinfo.saw_JFIF_marker)
  908.     {
  909.       gdouble xresolution;
  910.       gdouble yresolution;
  911.       gdouble asymmetry;
  912.  
  913.       xresolution = cinfo.X_density;
  914.       yresolution = cinfo.Y_density;
  915.  
  916.       switch (cinfo.density_unit)
  917.     {
  918.     case 0: /* unknown -> set the aspect ratio but use the default
  919.         *  image resolution
  920.         */
  921.       asymmetry = xresolution / yresolution;
  922.       gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
  923.       xresolution *= asymmetry;
  924.       break;
  925.  
  926.     case 1: /* dots per inch */
  927.       break;
  928.  
  929.     case 2: /* dots per cm */
  930.       xresolution *= 2.54;
  931.       yresolution *= 2.54;
  932.       gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
  933.       break;
  934.  
  935.     default:
  936.       g_message ("unknown density unit %d\nassuming dots per inch",
  937.              cinfo.density_unit);
  938.       break;
  939.     }
  940.  
  941.       gimp_image_set_resolution (image_ID, xresolution, yresolution);
  942.     }
  943. #endif /* GIMP_HAVE_RESOLUTION_INFO */
  944.  
  945.   /* Step 6: while (scan lines remain to be read) */
  946.   /*           jpeg_read_scanlines(...); */
  947.  
  948.   /* Here we use the library's state variable cinfo.output_scanline as the
  949.    * loop counter, so that we don't have to keep track ourselves.
  950.    */
  951.   while (cinfo.output_scanline < cinfo.output_height)
  952.     {
  953.       start = cinfo.output_scanline;
  954.       end = cinfo.output_scanline + tile_height;
  955.       end = MIN (end, cinfo.output_height);
  956.       scanlines = end - start;
  957.  
  958.       for (i = 0; i < scanlines; i++)
  959.     jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
  960.  
  961.       /*
  962.       for (i = start; i < end; i++)
  963.     gimp_pixel_rgn_set_row (&pixel_rgn, tilerow[i - start], 0, i, drawable->width);
  964.     */
  965.  
  966.       if (preview) /* Add a dummy alpha channel -- convert buf to padded_buf */
  967.     {
  968.       guchar *dest = padded_buf;
  969.       guchar *src  = buf;
  970.       gint num = drawable->width * scanlines;
  971.  
  972.       switch (cinfo.output_components)
  973.         {
  974.         case 1:
  975.           for (i=0; i<num; i++)
  976.         {
  977.           *(dest++) = *(src++);
  978.           *(dest++) = 255;
  979.         }
  980.           break;
  981.         case 3:
  982.           for (i=0; i<num; i++)
  983.         {
  984.           *(dest++) = *(src++);
  985.           *(dest++) = *(src++);
  986.           *(dest++) = *(src++);
  987.           *(dest++) = 255;
  988.         }
  989.           break;
  990.         default:
  991.           g_warning ("JPEG - shouldn't have gotten here.  Report to adam@gimp.org");
  992.         }
  993.     }
  994.  
  995.       gimp_pixel_rgn_set_rect (&pixel_rgn, preview ? padded_buf : buf,
  996.                    0, start, drawable->width, scanlines);
  997.  
  998.       if (runmode != GIMP_RUN_NONINTERACTIVE)
  999.     {
  1000.       gimp_progress_update ((double) cinfo.output_scanline / (double) cinfo.output_height);
  1001.     }
  1002.     }
  1003.  
  1004.   /* Step 7: Finish decompression */
  1005.  
  1006.   jpeg_finish_decompress (&cinfo);
  1007.   /* We can ignore the return value since suspension is not possible
  1008.    * with the stdio data source.
  1009.    */
  1010.  
  1011.   /* Step 8: Release JPEG decompression object */
  1012.  
  1013.   /* This is an important step since it will release a good deal of memory. */
  1014.   jpeg_destroy_decompress (&cinfo);
  1015.  
  1016.   /* free up the temporary buffers */
  1017.   g_free (rowbuf);
  1018.   g_free (buf);
  1019.   if (preview)
  1020.     g_free (padded_buf);
  1021.  
  1022.   /* After finish_decompress, we can close the input file.
  1023.    * Here we postpone it until after no more JPEG errors are possible,
  1024.    * so as to simplify the setjmp error logic above.  (Actually, I don't
  1025.    * think that jpeg_destroy can do an error exit, but why assume anything...)
  1026.    */
  1027.   fclose (infile);
  1028.  
  1029.   /* At this point you may want to check to see whether any corrupt-data
  1030.    * warnings occurred (test whether jerr.num_warnings is nonzero).
  1031.    */
  1032.  
  1033.   /* Tell the GIMP to display the image.
  1034.    */
  1035.   gimp_image_add_layer (image_ID, layer_ID, 0);
  1036.   gimp_drawable_flush (drawable);
  1037.  
  1038.   /* pw - Last of all, attach the parasites (couldn't do it earlier -
  1039.      there was no image. */
  1040.  
  1041. #ifdef GIMP_HAVE_PARASITES
  1042.   if (!preview) 
  1043.     {
  1044.       if (comment_parasite)
  1045.     {
  1046.       gimp_image_parasite_attach (image_ID, comment_parasite);
  1047.       gimp_parasite_free (comment_parasite);
  1048.     }
  1049.       if (vals_parasite)
  1050.     {
  1051.       gimp_image_parasite_attach (image_ID, vals_parasite);
  1052.       gimp_parasite_free (vals_parasite);
  1053.     }   
  1054.     }
  1055. #endif /* GIMP_HAVE_PARASITES */
  1056.  
  1057.   return image_ID;
  1058. }
  1059.  
  1060. /*
  1061.  * sg - This is the best I can do, I'm afraid... I think it will fail
  1062.  * if something bad really happens (but it might not). If you have a
  1063.  * better solution, send it ;-)
  1064.  */ 
  1065. static void
  1066. background_error_exit (j_common_ptr cinfo)
  1067. {
  1068.   if (abort_me) 
  1069.     *abort_me = TRUE;
  1070.   (*cinfo->err->output_message) (cinfo);
  1071. }
  1072.  
  1073. static gint
  1074. background_jpeg_save (PreviewPersistent *pp)
  1075. {
  1076.   guchar *t;
  1077.   guchar *s;
  1078.   gint i, j;
  1079.   gint yend;
  1080.  
  1081.   if (pp->abort_me || (pp->cinfo.next_scanline >= pp->cinfo.image_height))
  1082.     {
  1083.       struct stat buf;
  1084.       gchar temp[256];
  1085.  
  1086.       /* clean up... */
  1087.       if (pp->abort_me)
  1088.     {
  1089.       jpeg_abort_compress (&(pp->cinfo));
  1090.     }
  1091.       else
  1092.     {
  1093.       jpeg_finish_compress (&(pp->cinfo));
  1094.     }
  1095.  
  1096.       fclose (pp->outfile);
  1097.       jpeg_destroy_compress (&(pp->cinfo));
  1098.  
  1099.       g_free (pp->temp);
  1100.       g_free (pp->data);
  1101.  
  1102.       if (pp->drawable) gimp_drawable_detach (pp->drawable);
  1103.  
  1104.       /* display the preview stuff */
  1105.       if (!pp->abort_me) 
  1106.     {
  1107.       stat (pp->file_name, &buf);
  1108.       g_snprintf (temp, sizeof (temp),
  1109.               _("Size: %lu bytes (%02.01f kB)"),
  1110.               buf.st_size, (float) (buf.st_size) / 1024.0);
  1111.       gtk_label_set_text (GTK_LABEL (preview_size), temp);
  1112.     
  1113.       /* and load the preview */
  1114.       load_image (pp->file_name, GIMP_RUN_NONINTERACTIVE, TRUE);
  1115.     }
  1116.  
  1117.       /* we cleanup here (load_image doesn't run in the background) */
  1118.       unlink (pp->file_name);
  1119.       g_free (pp->file_name);
  1120.  
  1121.       if (abort_me == &(pp->abort_me)) 
  1122.     abort_me = NULL;
  1123.  
  1124.       g_free (pp);
  1125.  
  1126.       gimp_displays_flush ();
  1127.       gdk_flush ();
  1128.  
  1129.       return FALSE;
  1130.     }
  1131.   else
  1132.     {
  1133.       if ((pp->cinfo.next_scanline % pp->tile_height) == 0)
  1134.     {
  1135.       yend = pp->cinfo.next_scanline + pp->tile_height;
  1136.       yend = MIN (yend, pp->cinfo.image_height);
  1137.       gimp_pixel_rgn_get_rect (&pp->pixel_rgn, pp->data, 0, 
  1138.                    pp->cinfo.next_scanline,
  1139.                    pp->cinfo.image_width,
  1140.                    (yend - pp->cinfo.next_scanline));
  1141.       pp->src = pp->data;
  1142.     }
  1143.  
  1144.       t = pp->temp;
  1145.       s = pp->src;
  1146.       i = pp->cinfo.image_width;
  1147.  
  1148.       while (i--)
  1149.     {
  1150.       for (j = 0; j < pp->cinfo.input_components; j++)
  1151.         *t++ = *s++;
  1152.       if (pp->has_alpha)  /* ignore alpha channel */
  1153.         s++;
  1154.     }
  1155.  
  1156.       pp->src += pp->rowstride;
  1157.       jpeg_write_scanlines (&(pp->cinfo), (JSAMPARRAY) &(pp->temp), 1);
  1158.  
  1159.       return TRUE;
  1160.     }
  1161. }
  1162.  
  1163. static gint
  1164. save_image (gchar    *filename,
  1165.         gint32    image_ID,
  1166.         gint32    drawable_ID,
  1167.         gint32    orig_image_ID,
  1168.         gboolean  preview)
  1169. {
  1170.   GimpPixelRgn pixel_rgn;
  1171.   GimpDrawable *drawable;
  1172.   GimpImageType drawable_type;
  1173.   struct jpeg_compress_struct cinfo;
  1174.   struct my_error_mgr jerr;
  1175.   FILE * volatile outfile;
  1176.   guchar *temp, *t;
  1177.   guchar *data;
  1178.   guchar *src, *s;
  1179.   gchar *name;
  1180.   gint has_alpha;
  1181.   gint rowstride, yend;
  1182.   gint i, j;
  1183.  
  1184.   drawable = gimp_drawable_get (drawable_ID);
  1185.   drawable_type = gimp_drawable_type (drawable_ID);
  1186.   gimp_pixel_rgn_init (&pixel_rgn, drawable,
  1187.                0, 0, drawable->width, drawable->height, FALSE, FALSE);
  1188.  
  1189.   if (!preview) 
  1190.     {
  1191.       name = g_strdup_printf (_("Saving %s:"), filename);
  1192.       gimp_progress_init (name);
  1193.       g_free (name);
  1194.     }
  1195.  
  1196.   /* Step 1: allocate and initialize JPEG compression object */
  1197.  
  1198.   /* We have to set up the error handler first, in case the initialization
  1199.    * step fails.  (Unlikely, but it could happen if you are out of memory.)
  1200.    * This routine fills in the contents of struct jerr, and returns jerr's
  1201.    * address which we place into the link field in cinfo.
  1202.    */
  1203.   cinfo.err = jpeg_std_error (&jerr.pub);
  1204.   jerr.pub.error_exit = my_error_exit;
  1205.  
  1206.   outfile = NULL;
  1207.   /* Establish the setjmp return context for my_error_exit to use. */
  1208.   if (setjmp (jerr.setjmp_buffer))
  1209.     {
  1210.       /* If we get here, the JPEG code has signaled an error.
  1211.        * We need to clean up the JPEG object, close the input file, and return.
  1212.        */
  1213.       jpeg_destroy_compress (&cinfo);
  1214.       if (outfile)
  1215.     fclose (outfile);
  1216.       if (drawable)
  1217.     gimp_drawable_detach (drawable);
  1218.  
  1219.       return FALSE;
  1220.     }
  1221.  
  1222.   /* Now we can initialize the JPEG compression object. */
  1223.   jpeg_create_compress (&cinfo);
  1224.  
  1225.   /* Step 2: specify data destination (eg, a file) */
  1226.   /* Note: steps 2 and 3 can be done in either order. */
  1227.  
  1228.   /* Here we use the library-supplied code to send compressed data to a
  1229.    * stdio stream.  You can also write your own code to do something else.
  1230.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  1231.    * requires it in order to write binary files.
  1232.    */
  1233.   if ((outfile = fopen (filename, "wb")) == NULL)
  1234.     {
  1235.       g_message ("can't open %s\n", filename);
  1236.       return FALSE;
  1237.     }
  1238.   jpeg_stdio_dest (&cinfo, outfile);
  1239.  
  1240.   /* Get the input image and a pointer to its data.
  1241.    */
  1242.   switch (drawable_type)
  1243.     {
  1244.     case GIMP_RGB_IMAGE:
  1245.     case GIMP_GRAY_IMAGE:
  1246.       /* # of color components per pixel */
  1247.       cinfo.input_components = drawable->bpp;
  1248.       has_alpha = 0;
  1249.       break;
  1250.     case GIMP_RGBA_IMAGE:
  1251.     case GIMP_GRAYA_IMAGE:
  1252.       /*gimp_message ("jpeg: image contains a-channel info which will be lost");*/
  1253.       /* # of color components per pixel (minus the GIMP alpha channel) */
  1254.       cinfo.input_components = drawable->bpp - 1;
  1255.       has_alpha = 1;
  1256.       break;
  1257.     case GIMP_INDEXED_IMAGE:
  1258.       /*gimp_message ("jpeg: cannot operate on indexed color images");*/
  1259.       return FALSE;
  1260.       break;
  1261.     default:
  1262.       /*gimp_message ("jpeg: cannot operate on unknown image types");*/
  1263.       return FALSE;
  1264.       break;
  1265.     }
  1266.  
  1267.   /* Step 3: set parameters for compression */
  1268.  
  1269.   /* First we supply a description of the input image.
  1270.    * Four fields of the cinfo struct must be filled in:
  1271.    */
  1272.   /* image width and height, in pixels */
  1273.   cinfo.image_width  = drawable->width;
  1274.   cinfo.image_height = drawable->height;
  1275.   /* colorspace of input image */
  1276.   cinfo.in_color_space = (drawable_type == GIMP_RGB_IMAGE ||
  1277.               drawable_type == GIMP_RGBA_IMAGE)
  1278.     ? JCS_RGB : JCS_GRAYSCALE;
  1279.   /* Now use the library's routine to set default compression parameters.
  1280.    * (You must set at least cinfo.in_color_space before calling this,
  1281.    * since the defaults depend on the source color space.)
  1282.    */
  1283.   jpeg_set_defaults (&cinfo);
  1284.  
  1285.   jpeg_set_quality (&cinfo, (int) (jsvals.quality * 100), jsvals.baseline);
  1286.   cinfo.smoothing_factor = (int) (jsvals.smoothing * 100);
  1287.   cinfo.optimize_coding = jsvals.optimize;
  1288.  
  1289. #ifdef HAVE_PROGRESSIVE_JPEG
  1290.   if (jsvals.progressive) 
  1291.     {
  1292.       jpeg_simple_progression (&cinfo);
  1293.     }
  1294. #endif /* HAVE_PROGRESSIVE_JPEG */
  1295.  
  1296.   switch (jsvals.subsmp) 
  1297.     {
  1298.     case 0:
  1299.     default:
  1300.       cinfo.comp_info[0].h_samp_factor = 2;
  1301.       cinfo.comp_info[0].v_samp_factor = 2;
  1302.       cinfo.comp_info[1].h_samp_factor = 1;
  1303.       cinfo.comp_info[1].v_samp_factor = 1;
  1304.       cinfo.comp_info[2].h_samp_factor = 1;
  1305.       cinfo.comp_info[2].v_samp_factor = 1;
  1306.       break;
  1307.     case 1:
  1308.       cinfo.comp_info[0].h_samp_factor = 2;
  1309.       cinfo.comp_info[0].v_samp_factor = 1;
  1310.       cinfo.comp_info[1].h_samp_factor = 1;
  1311.       cinfo.comp_info[1].v_samp_factor = 1;
  1312.       cinfo.comp_info[2].h_samp_factor = 1;
  1313.       cinfo.comp_info[2].v_samp_factor = 1;
  1314.       break;
  1315.     case 2:
  1316.       cinfo.comp_info[0].h_samp_factor = 1;
  1317.       cinfo.comp_info[0].v_samp_factor = 1;
  1318.       cinfo.comp_info[1].h_samp_factor = 1;
  1319.       cinfo.comp_info[1].v_samp_factor = 1;
  1320.       cinfo.comp_info[2].h_samp_factor = 1;
  1321.       cinfo.comp_info[2].v_samp_factor = 1;
  1322.       break;
  1323.     }
  1324.   
  1325.   cinfo.restart_interval = 0;
  1326.   cinfo.restart_in_rows = jsvals.restart;
  1327.  
  1328.   switch (jsvals.dct) 
  1329.     {
  1330.     case 0:
  1331.     default:
  1332.       cinfo.dct_method = JDCT_ISLOW;
  1333.       break;
  1334.     case 1:
  1335.       cinfo.dct_method = JDCT_IFAST;
  1336.       break;
  1337.     case 2:
  1338.       cinfo.dct_method = JDCT_FLOAT;
  1339.       break;
  1340.     }
  1341.  
  1342. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1343.   {
  1344.     gdouble xresolution;
  1345.     gdouble yresolution;
  1346.  
  1347.     gimp_image_get_resolution (orig_image_ID, &xresolution, &yresolution);
  1348.  
  1349.     if (xresolution > 1e-5 && yresolution > 1e-5)
  1350.       {
  1351.     gdouble factor;
  1352.  
  1353.     factor = gimp_unit_get_factor (gimp_image_get_unit (orig_image_ID));
  1354.  
  1355.     if (factor == 2.54 /* cm */ ||
  1356.         factor == 25.4 /* mm */)
  1357.       {
  1358.         cinfo.density_unit = 2;  /* dots per cm */
  1359.  
  1360.         xresolution /= 2.54;
  1361.         yresolution /= 2.54;
  1362.       }
  1363.     else
  1364.       {
  1365.         cinfo.density_unit = 1;  /* dots per inch */
  1366.       }
  1367.  
  1368.     cinfo.X_density = xresolution;
  1369.     cinfo.Y_density = yresolution;
  1370.       }
  1371.   }
  1372. #endif /* GIMP_HAVE_RESOLUTION_INFO */
  1373.  
  1374.   /* Step 4: Start compressor */
  1375.  
  1376.   /* TRUE ensures that we will write a complete interchange-JPEG file.
  1377.    * Pass TRUE unless you are very sure of what you're doing.
  1378.    */
  1379.   jpeg_start_compress (&cinfo, TRUE);
  1380.  
  1381.   /* Step 4.1: Write the comment out - pw */
  1382.   if (image_comment && *image_comment)
  1383.     {
  1384.       jpeg_write_marker (&cinfo,
  1385.              JPEG_COM,
  1386.              image_comment,
  1387.              strlen (image_comment));
  1388.     }
  1389.     
  1390.   /* Step 5: while (scan lines remain to be written) */
  1391.   /*           jpeg_write_scanlines(...); */
  1392.  
  1393.   /* Here we use the library's state variable cinfo.next_scanline as the
  1394.    * loop counter, so that we don't have to keep track ourselves.
  1395.    * To keep things simple, we pass one scanline per call; you can pass
  1396.    * more if you wish, though.
  1397.    */
  1398.   /* JSAMPLEs per row in image_buffer */
  1399.   rowstride = drawable->bpp * drawable->width;
  1400.   temp = g_new (guchar, cinfo.image_width * cinfo.input_components);
  1401.   data = g_new (guchar, rowstride * gimp_tile_height ());
  1402.  
  1403.   /* fault if cinfo.next_scanline isn't initially a multiple of
  1404.    * gimp_tile_height */
  1405.   src = NULL;
  1406.  
  1407.   /*
  1408.    * sg - if we preview, we want this to happen in the background -- do
  1409.    * not duplicate code in the future; for now, it's OK
  1410.    */
  1411.  
  1412.   if (preview) 
  1413.     {
  1414.       PreviewPersistent *pp = g_new (PreviewPersistent, 1);
  1415.       
  1416.       /* pass all the information we need */
  1417.       pp->cinfo       = cinfo;
  1418.       pp->tile_height = gimp_tile_height();
  1419.       pp->data        = data;
  1420.       pp->outfile     = outfile;
  1421.       pp->has_alpha   = has_alpha;
  1422.       pp->rowstride   = rowstride;
  1423.       pp->temp        = temp;
  1424.       pp->data        = data;
  1425.       pp->drawable    = drawable;
  1426.       pp->pixel_rgn   = pixel_rgn;
  1427.       pp->src         = NULL;
  1428.       pp->file_name   = filename;
  1429.       
  1430.       pp->abort_me    = FALSE;
  1431.       abort_me = &(pp->abort_me);
  1432.       
  1433.       jerr.pub.error_exit = background_error_exit;
  1434.  
  1435.       gtk_idle_add ((GtkFunction) background_jpeg_save, (gpointer *)pp);
  1436.       
  1437.       /* background_jpeg_save() will cleanup as needed */
  1438.       return TRUE;
  1439.     } 
  1440.  
  1441.   while (cinfo.next_scanline < cinfo.image_height)
  1442.     {
  1443.       if ((cinfo.next_scanline % gimp_tile_height ()) == 0)
  1444.     {
  1445.       yend = cinfo.next_scanline + gimp_tile_height ();
  1446.       yend = MIN (yend, cinfo.image_height);
  1447.       gimp_pixel_rgn_get_rect (&pixel_rgn, data, 
  1448.                    0, cinfo.next_scanline, 
  1449.                    cinfo.image_width,
  1450.                    (yend - cinfo.next_scanline));
  1451.       src = data;
  1452.     }
  1453.  
  1454.       t = temp;
  1455.       s = src;
  1456.       i = cinfo.image_width;
  1457.  
  1458.       while (i--)
  1459.     {
  1460.       for (j = 0; j < cinfo.input_components; j++)
  1461.         *t++ = *s++;
  1462.       if (has_alpha)  /* ignore alpha channel */
  1463.         s++;
  1464.     }
  1465.  
  1466.       src += rowstride;
  1467.       jpeg_write_scanlines (&cinfo, (JSAMPARRAY) &temp, 1);
  1468.  
  1469.       if ((cinfo.next_scanline % 5) == 0)
  1470.     gimp_progress_update ((gdouble) cinfo.next_scanline / 
  1471.                   (gdouble) cinfo.image_height);
  1472.     }
  1473.  
  1474.   /* Step 6: Finish compression */
  1475.   jpeg_finish_compress (&cinfo);
  1476.   /* After finish_compress, we can close the output file. */
  1477.   fclose (outfile);
  1478.  
  1479.   /* Step 7: release JPEG compression object */
  1480.  
  1481.   /* This is an important step since it will release a good deal of memory. */
  1482.   jpeg_destroy_compress (&cinfo);
  1483.   /* free the temporary buffer */
  1484.   g_free (temp);
  1485.   g_free (data);
  1486.  
  1487.   /* And we're done! */
  1488.   /*gimp_do_progress (1, 1);*/
  1489.  
  1490.   gimp_drawable_detach (drawable);
  1491.  
  1492.   return TRUE;
  1493. }
  1494.  
  1495. static void
  1496. make_preview (void)
  1497. {
  1498.   gchar *tn;
  1499.  
  1500.   destroy_preview ();
  1501.  
  1502.   if (jsvals.preview) 
  1503.     {
  1504.       tn = gimp_temp_name ("jpeg");
  1505.       save_image (tn, 
  1506.           image_ID_global,
  1507.           drawable_ID_global, 
  1508.           orig_image_ID_global, 
  1509.           TRUE);
  1510.     }
  1511.   else 
  1512.     {
  1513.       gtk_label_set_text (GTK_LABEL (preview_size), _("Size: unknown"));
  1514.       gtk_widget_draw (preview_size, NULL);
  1515.       
  1516.       gimp_displays_flush ();
  1517.       gdk_flush();
  1518.     }
  1519. }
  1520.  
  1521. static void
  1522. destroy_preview (void)
  1523. {
  1524.   if (abort_me) 
  1525.     {
  1526.       *abort_me = TRUE;   /* signal the background save to stop */
  1527.     }
  1528.   if (drawable_global) 
  1529.     {
  1530.       gimp_drawable_detach (drawable_global);
  1531.       drawable_global = NULL;
  1532.     }
  1533.   if (layer_ID_global != -1 && image_ID_global != -1) 
  1534.     {
  1535.       gimp_image_remove_layer (image_ID_global, layer_ID_global);
  1536.       
  1537.       /* gimp_layer_delete(layer_ID_global); */
  1538.       layer_ID_global = -1;
  1539.     }
  1540. }
  1541.  
  1542. static gint
  1543. save_dialog (void)
  1544. {
  1545.   GtkWidget *dlg;
  1546.   GtkWidget *vbox;
  1547.   GtkWidget *main_vbox;
  1548.   GtkWidget *label;
  1549.   GtkWidget *scale;
  1550.   GtkWidget *frame;
  1551.   GtkWidget *table;
  1552.   GtkWidget *toggle;
  1553.   GtkWidget *abox;
  1554.   GtkObject *scale_data;
  1555.  
  1556.   GtkWidget *progressive;
  1557.   GtkWidget *baseline;
  1558.   GtkWidget *restart;
  1559.  
  1560.   GtkWidget *preview;
  1561.   /* GtkWidget *preview_size; -- global */
  1562.  
  1563.   GtkWidget *menu;
  1564.   
  1565.   GtkWidget *text;
  1566.   GtkWidget *com_frame;
  1567.   GtkWidget *com_table;
  1568.   GtkWidget *vscrollbar;
  1569.   
  1570.   GtkWidget *prv_frame;
  1571.   GimpImageType dtype;
  1572.  
  1573.   dlg = gimp_dialog_new (_("Save as JPEG"), "jpeg",
  1574.              gimp_standard_help_func, "filters/jpeg.html",
  1575.              GTK_WIN_POS_MOUSE,
  1576.              FALSE, TRUE, FALSE,
  1577.  
  1578.              _("OK"), save_ok_callback,
  1579.              NULL, NULL, NULL, TRUE, FALSE,
  1580.              _("Cancel"), gtk_widget_destroy,
  1581.              NULL, 1, NULL, FALSE, TRUE,
  1582.  
  1583.              NULL);
  1584.  
  1585.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  1586.               GTK_SIGNAL_FUNC (save_close_callback),
  1587.               NULL);
  1588.  
  1589.   main_vbox = gtk_vbox_new (FALSE, 4);
  1590.   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
  1591.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), main_vbox,
  1592.               TRUE, TRUE, 0);
  1593.   gtk_widget_show (main_vbox);
  1594.  
  1595.   /* sg - preview */
  1596.   prv_frame = gtk_frame_new (_("Image Preview"));
  1597.   gtk_frame_set_shadow_type (GTK_FRAME (prv_frame), GTK_SHADOW_ETCHED_IN);
  1598.   gtk_box_pack_start (GTK_BOX (main_vbox), prv_frame, FALSE, FALSE, 0);
  1599.  
  1600.   vbox = gtk_vbox_new (FALSE, 2);
  1601.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  1602.   gtk_container_add (GTK_CONTAINER (prv_frame), vbox);
  1603.   gtk_widget_show (vbox);
  1604.  
  1605.   preview = gtk_check_button_new_with_label (_("Preview (in image window)"));
  1606.   gtk_box_pack_start (GTK_BOX (vbox), preview, FALSE, FALSE, 0);
  1607.   gtk_signal_connect (GTK_OBJECT (preview), "toggled",
  1608.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1609.               &jsvals.preview);
  1610.   gtk_signal_connect (GTK_OBJECT (preview), "toggled",
  1611.               GTK_SIGNAL_FUNC (make_preview),
  1612.               NULL);
  1613.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (preview), jsvals.preview);
  1614.   gtk_widget_show (preview);
  1615.  
  1616.   preview_size = gtk_label_new (_("Size: unknown"));
  1617.   gtk_misc_set_alignment (GTK_MISC (preview_size), 0.0, 0.5);
  1618.   gtk_box_pack_start (GTK_BOX (vbox), preview_size, FALSE, FALSE, 0);
  1619.   gtk_widget_show (preview_size);
  1620.  
  1621.   gtk_widget_show (prv_frame);
  1622.  
  1623.   make_preview ();
  1624.  
  1625.   /*  parameter settings  */
  1626.   frame = gtk_frame_new (_("Parameter Settings"));
  1627.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1628.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  1629.  
  1630.   table = gtk_table_new (9, 3, FALSE);
  1631.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1632.   gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  1633.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  1634.   gtk_container_add (GTK_CONTAINER (frame), table);
  1635.  
  1636.   label = gtk_label_new (_("Quality:"));
  1637.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  1638.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
  1639.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1640.   gtk_widget_show (label);
  1641.  
  1642.   scale_data = gtk_adjustment_new (jsvals.quality, 0.0, 1.0, 0.01, 0.01, 0.0);
  1643.   scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1644.   gtk_widget_set_usize (scale, SCALE_WIDTH, 0);
  1645.   gtk_table_attach (GTK_TABLE (table), scale, 1, 3, 0, 1,
  1646.             GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
  1647.   gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  1648.   gtk_scale_set_digits (GTK_SCALE (scale), 2);
  1649.   gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  1650.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1651.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  1652.               &jsvals.quality);
  1653.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1654.               GTK_SIGNAL_FUNC (make_preview),
  1655.               NULL);
  1656.   gtk_widget_show (scale);
  1657.  
  1658.   label = gtk_label_new (_("Smoothing:"));
  1659.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  1660.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
  1661.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1662.   gtk_widget_show (label);
  1663.  
  1664.   scale_data = gtk_adjustment_new (jsvals.smoothing, 0.0, 1.0, 0.01, 0.01, 0.0);
  1665.   scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1666.   gtk_widget_set_usize (scale, SCALE_WIDTH, 0);
  1667.   gtk_table_attach (GTK_TABLE (table), scale, 1, 3, 1, 2,
  1668.             GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
  1669.   gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  1670.   gtk_scale_set_digits (GTK_SCALE (scale), 2);
  1671.   gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  1672.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1673.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  1674.               &jsvals.smoothing);
  1675.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1676.               GTK_SIGNAL_FUNC (make_preview),
  1677.               NULL);
  1678.   gtk_widget_show (scale);
  1679.  
  1680.   /* sg - have to init scale here */
  1681.   scale_data = gtk_adjustment_new ((jsvals.restart == 0) ? 1 : jsvals.restart,
  1682.                    1, 64, 1, 1, 0.0);
  1683.   restart_markers_scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1684.  
  1685.   restart = gtk_check_button_new_with_label (_("Restart markers"));
  1686.   gtk_table_attach (GTK_TABLE (table), restart, 0, 1, 2, 3,
  1687.             GTK_FILL, 0, 0, 0);
  1688.   gtk_signal_connect (GTK_OBJECT (restart), "toggled",
  1689.               GTK_SIGNAL_FUNC (save_restart_toggle_update),
  1690.               scale_data);
  1691.   gtk_widget_show (restart);
  1692.  
  1693.   restart_markers_label = gtk_label_new (_("Restart frequency (rows):"));
  1694.   gtk_misc_set_alignment (GTK_MISC (restart_markers_label), 1.0, 1.0);
  1695.   gtk_table_attach (GTK_TABLE (table), restart_markers_label, 0, 1, 3, 4,
  1696.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1697.   gtk_widget_show (restart_markers_label);
  1698.  
  1699.   abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  1700.   gtk_table_attach (GTK_TABLE (table), abox, 1, 3, 2, 4,
  1701.             GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  1702.   gtk_widget_show (abox);
  1703.  
  1704.   gtk_widget_set_usize (restart_markers_scale, SCALE_WIDTH, 0);
  1705.   gtk_container_add (GTK_CONTAINER (abox), restart_markers_scale);
  1706.   gtk_scale_set_value_pos (GTK_SCALE (restart_markers_scale), GTK_POS_TOP);
  1707.   gtk_scale_set_digits (GTK_SCALE (restart_markers_scale), 0);
  1708.   gtk_range_set_update_policy (GTK_RANGE (restart_markers_scale),
  1709.                    GTK_UPDATE_DELAYED);
  1710.  
  1711.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1712.                       GTK_SIGNAL_FUNC (save_restart_update),
  1713.                       restart);
  1714.  
  1715.   gtk_widget_set_sensitive (restart_markers_label, 
  1716.                 (jsvals.restart ? TRUE : FALSE));
  1717.   gtk_widget_set_sensitive (restart_markers_scale,
  1718.                 (jsvals.restart ? TRUE : FALSE));
  1719.  
  1720.   gtk_widget_show (restart_markers_scale);
  1721.  
  1722.   toggle = gtk_check_button_new_with_label (_("Optimize"));
  1723.   gtk_table_attach (GTK_TABLE (table), toggle, 0, 3, 4, 5,
  1724.             GTK_FILL, 0, 0, 0);
  1725.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1726.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1727.               &jsvals.optimize);
  1728.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1729.               GTK_SIGNAL_FUNC (make_preview),
  1730.               NULL);
  1731.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), jsvals.optimize);
  1732.   gtk_widget_show (toggle);
  1733.  
  1734.   progressive = gtk_check_button_new_with_label (_("Progressive"));
  1735.   gtk_table_attach (GTK_TABLE (table), progressive, 0, 3, 5, 6,
  1736.             GTK_FILL, 0, 0, 0);
  1737.   gtk_signal_connect (GTK_OBJECT (progressive), "toggled",
  1738.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1739.               &jsvals.progressive);
  1740.   gtk_signal_connect (GTK_OBJECT (progressive), "toggled",
  1741.               GTK_SIGNAL_FUNC (make_preview),
  1742.               NULL);
  1743.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (progressive),
  1744.                 jsvals.progressive);
  1745.   gtk_widget_show (progressive);
  1746.  
  1747. #ifndef HAVE_PROGRESSIVE_JPEG
  1748.   gtk_widget_set_sensitive (progressive, FALSE);
  1749. #endif
  1750.   
  1751.   baseline = gtk_check_button_new_with_label (_("Force baseline JPEG (Readable by all decoders)"));
  1752.   gtk_table_attach (GTK_TABLE (table), baseline, 0, 3, 6, 7,
  1753.             GTK_FILL, 0, 0, 0);
  1754.   gtk_signal_connect (GTK_OBJECT (baseline), "toggled",
  1755.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1756.               &jsvals.baseline);
  1757.   gtk_signal_connect (GTK_OBJECT (baseline), "toggled",
  1758.               GTK_SIGNAL_FUNC (make_preview),
  1759.               NULL);
  1760.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (baseline),
  1761.                 jsvals.baseline);
  1762.   gtk_widget_show (baseline);
  1763.  
  1764.   /* Subsampling */
  1765.   menu = 
  1766.     gimp_option_menu_new2 (FALSE, 
  1767.                menu_callback, 
  1768.                &jsvals.subsmp, GINT_TO_POINTER (jsvals.subsmp),
  1769.                "2x2,1x1,1x1",  GINT_TO_POINTER (0), NULL, 
  1770.                "2x1,1x1,1x1 (4:2:2)", GINT_TO_POINTER (1), NULL,
  1771.                "1x1,1x1,1x1",  GINT_TO_POINTER (2), NULL,
  1772.                NULL);
  1773.  
  1774.   gimp_table_attach_aligned (GTK_TABLE (table), 1, 7, 
  1775.                  _("Subsampling:"),
  1776.                  1.0, 0.5,
  1777.                  menu, 1, FALSE);
  1778.  
  1779.   /* DCT method */
  1780.   menu = 
  1781.     gimp_option_menu_new2 (FALSE, 
  1782.                menu_callback, 
  1783.                &jsvals.dct, GINT_TO_POINTER (jsvals.dct),
  1784.                _("Fast Integer"),   GINT_TO_POINTER (1), NULL, 
  1785.                _("Integer"),        GINT_TO_POINTER (0), NULL,
  1786.                _("Floating-Point"), GINT_TO_POINTER (2), NULL,
  1787.                NULL);
  1788.  
  1789.   gimp_table_attach_aligned (GTK_TABLE (table), 1, 8, 
  1790.                  _("DCT method (Speed/quality tradeoff):"),
  1791.                  1.0, 0.5,
  1792.                  menu, 1, FALSE);
  1793.  
  1794.   dtype = gimp_drawable_type (drawable_ID_global);
  1795.   if (dtype != GIMP_RGB_IMAGE && dtype != GIMP_RGBA_IMAGE) 
  1796.     gtk_widget_set_sensitive (menu, FALSE);
  1797.  
  1798.   com_frame = gtk_frame_new (_("Image comments"));
  1799.   gtk_frame_set_shadow_type (GTK_FRAME (com_frame), GTK_SHADOW_ETCHED_IN);
  1800.   gtk_box_pack_start (GTK_BOX (main_vbox), com_frame, TRUE, TRUE, 0);
  1801.  
  1802.   com_table = gtk_table_new (1, 2, FALSE);
  1803.   gtk_container_set_border_width (GTK_CONTAINER (com_table), 4);
  1804.   gtk_container_add (GTK_CONTAINER (com_frame), com_table);
  1805.  
  1806.   text = gtk_text_new (NULL, NULL);
  1807.   gtk_text_set_editable (GTK_TEXT (text), TRUE);
  1808.   gtk_widget_set_usize (text, -1, 3); /* HB: restrict to 3 line height 
  1809.                        * to allow 800x600 mode */
  1810.   if (image_comment) 
  1811.     gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, image_comment, -1);
  1812.   gtk_table_attach (GTK_TABLE (com_table), text, 0, 1, 0, 1,
  1813.                     GTK_EXPAND | GTK_SHRINK | GTK_FILL,
  1814.                     GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  1815.   
  1816.   /* Add a vertical scrollbar to the GtkText widget */
  1817.   vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
  1818.   gtk_table_attach (GTK_TABLE (com_table), vscrollbar, 1, 2, 0, 1,
  1819.                     GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  1820.   gtk_widget_show (vscrollbar);
  1821.  
  1822.   /* pw - mild hack here.  I didn't like redoing the comment string
  1823.    * each time a character was typed, so I associated the text area
  1824.    * with the dialog.  That way, just before the dialog destroys
  1825.    * itself (once the ok button is hit) it can save whatever was in
  1826.    * the comment text area to the comment string.  See the
  1827.    * save-ok-callback for more details.  */
  1828.   
  1829.   gtk_object_set_data (GTK_OBJECT (dlg), "text", text); 
  1830.   
  1831.   gtk_widget_show (text);
  1832.   gtk_widget_show (com_frame);
  1833.   gtk_widget_show (com_table);
  1834.  
  1835.   gtk_widget_show (frame);
  1836.   gtk_widget_show (table);
  1837.   gtk_widget_show (dlg);
  1838.  
  1839.   gtk_main ();
  1840.   gdk_flush ();
  1841.  
  1842.   return jsint.run;
  1843. }
  1844.  
  1845. /*  Save interface functions  */
  1846.  
  1847. static void
  1848. save_close_callback (GtkWidget *widget,
  1849.              gpointer   data)
  1850. {
  1851.   destroy_preview ();
  1852.   gimp_displays_flush ();
  1853.   gtk_main_quit ();
  1854. }
  1855.  
  1856. static void
  1857. save_ok_callback (GtkWidget *widget,
  1858.           gpointer   data)
  1859. {
  1860.   GtkWidget *text;
  1861.   jsint.run = TRUE;
  1862.  
  1863.   /* pw - get the comment text object and grab it's data */
  1864.   text = gtk_object_get_data (GTK_OBJECT (data), "text");
  1865.   
  1866.   /* pw - gtk_editable_get_chars allocates a copy of the string, so
  1867.    * don't worry about the gtk_widget_destroy killing it.  */
  1868.  
  1869.   g_free (image_comment);
  1870.   image_comment = NULL;
  1871.  
  1872.   if (text)
  1873.     image_comment = gtk_editable_get_chars (GTK_EDITABLE (text), 0, -1);
  1874.  
  1875.   gtk_widget_destroy (GTK_WIDGET (data));
  1876. }
  1877.  
  1878. static void
  1879. save_restart_toggle_update (GtkWidget     *widget,
  1880.                 GtkAdjustment *adjustment)
  1881. {
  1882.   save_restart_update (adjustment, widget);
  1883. }
  1884.  
  1885. static void
  1886. save_restart_update (GtkAdjustment *adjustment,
  1887.              GtkWidget     *toggle)
  1888. {
  1889.   jsvals.restart = GTK_TOGGLE_BUTTON (toggle)->active ? adjustment->value : 0;
  1890.  
  1891.   gtk_widget_set_sensitive (restart_markers_label,
  1892.                 (jsvals.restart ? TRUE : FALSE));
  1893.   gtk_widget_set_sensitive (restart_markers_scale,
  1894.                 (jsvals.restart ? TRUE : FALSE));
  1895.           
  1896.   make_preview ();
  1897. }
  1898.  
  1899. static void
  1900. menu_callback (GtkWidget *widget,
  1901.           gpointer   data)
  1902. {
  1903.   gimp_menu_item_update (widget, data);
  1904.   make_preview ();
  1905. }
  1906.