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.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-10  |  59.8 KB  |  1,924 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.   gboolean      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 gboolean save_image                (gchar            *filename,
  223.                        gint32            image_ID,
  224.                        gint32            drawable_ID,
  225.                        gint32            orig_image_ID,
  226.                        gboolean          preview);
  227.  
  228. static gboolean 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.   gboolean              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.  
  634.   return *datasrc->next_input_byte++;
  635. }
  636.  
  637. /* We need our own marker parser, since early versions of libjpeg
  638.  * don't keep a conventient list of the marker's that have been
  639.  * seen. */
  640.  
  641. /*
  642.  * Marker processor for COM markers.
  643.  * This replaces the library's built-in processor, which just skips the marker.
  644.  * Note this code relies on a non-suspending data source.
  645.  */
  646. static GString *local_image_comments = NULL;
  647.  
  648. static boolean
  649. COM_handler (j_decompress_ptr cinfo)
  650. {
  651.   gint  length;
  652.   guint ch;
  653.  
  654.   length = jpeg_getc (cinfo) << 8;
  655.   length += jpeg_getc (cinfo);
  656.   if (length < 2)
  657.     return FALSE;
  658.   length -= 2;            /* discount the length word itself */
  659.  
  660.   if (!local_image_comments)
  661.     local_image_comments = g_string_new (NULL);
  662.   else
  663.     g_string_append_c (local_image_comments, '\n');
  664.  
  665.   while (length-- > 0)
  666.     {
  667.       ch = jpeg_getc (cinfo);
  668.       g_string_append_c (local_image_comments, ch);
  669.     }
  670.  
  671.   return TRUE;
  672. }
  673.  
  674. typedef struct my_error_mgr
  675. {
  676.   struct jpeg_error_mgr pub;            /* "public" fields */
  677.  
  678.   jmp_buf               setjmp_buffer;  /* for return to caller */
  679. } *my_error_ptr;
  680.  
  681. /*
  682.  * Here's the routine that will replace the standard error_exit method:
  683.  */
  684.  
  685. static void
  686. my_error_exit (j_common_ptr cinfo)
  687. {
  688.   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
  689.   my_error_ptr myerr = (my_error_ptr) cinfo->err;
  690.  
  691.   /* Always display the message. */
  692.   /* We could postpone this until after returning, if we chose. */
  693.   (*cinfo->err->output_message) (cinfo);
  694.  
  695.   /* Return control to the setjmp point */
  696.   longjmp (myerr->setjmp_buffer, 1);
  697. }
  698.  
  699. static gint32
  700. load_image (gchar           *filename, 
  701.         GimpRunModeType  runmode, 
  702.         gboolean         preview)
  703. {
  704.   GimpPixelRgn     pixel_rgn;
  705.   GimpDrawable    *drawable;
  706.   gint32 volatile  image_ID;
  707.   gint32           layer_ID;
  708.   struct jpeg_decompress_struct cinfo;
  709.   struct my_error_mgr           jerr;
  710.   FILE    *infile;
  711.   guchar  *buf;
  712.   guchar  * volatile padded_buf = NULL;
  713.   guchar **rowbuf;
  714.   gchar   *name;
  715.   gint     image_type;
  716.   gint     layer_type;
  717.   gint     tile_height;
  718.   gint     scanlines;
  719.   gint     i, start, end;
  720.  
  721. #ifdef GIMP_HAVE_PARASITES
  722.   JpegSaveVals local_save_vals;
  723.   GimpParasite * volatile comment_parasite = NULL;
  724.   GimpParasite * volatile vals_parasite    = NULL;
  725. #endif /* GIMP_HAVE_PARASITES */
  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.     {
  783.       /* if we had any comments then make a parasite for them */
  784.       if (local_image_comments && local_image_comments->len) 
  785.     {
  786.       gchar *string = local_image_comments->str;
  787.       g_string_free (local_image_comments, FALSE);
  788.       local_image_comments = NULL;
  789.       comment_parasite = gimp_parasite_new ("gimp-comment",
  790.                         GIMP_PARASITE_PERSISTENT,
  791.                         strlen (string) + 1, string);
  792.     } 
  793.       else 
  794.     {
  795.       comment_parasite = NULL;
  796.     }
  797.       
  798.       /* pw - figuring out what the saved values were is non-trivial.
  799.        * They don't seem to be in the cinfo structure. For now, I will
  800.        * just use the defaults, but if someone figures out how to derive
  801.        * them this is the place to store them. */
  802.       
  803.       local_save_vals.quality     = DEFAULT_QUALITY;
  804.       local_save_vals.smoothing   = DEFAULT_SMOOTHING;
  805.       local_save_vals.optimize    = DEFAULT_OPTIMIZE;
  806.       
  807. #ifdef HAVE_PROGRESSIVE_JPEG 
  808.       local_save_vals.progressive = cinfo.progressive_mode;
  809. #else
  810.       local_save_vals.progressive = 0;
  811. #endif /* HAVE_PROGRESSIVE_JPEG */
  812.       local_save_vals.baseline    = DEFAULT_BASELINE;
  813.       local_save_vals.subsmp      = DEFAULT_SUBSMP;   /* sg - this _is_ there, but I'm too lazy */ 
  814.       local_save_vals.restart     = DEFAULT_RESTART;
  815.       local_save_vals.dct         = DEFAULT_DCT;
  816.       local_save_vals.preview     = DEFAULT_PREVIEW;
  817.       
  818.       vals_parasite = gimp_parasite_new ("jpeg-save-options", 0,
  819.                      sizeof (local_save_vals),
  820.                      &local_save_vals);
  821.     } 
  822. #endif /* GIMP_HAVE_PARASITES */
  823.   
  824.   
  825.   /* Step 4: set parameters for decompression */
  826.  
  827.   /* In this example, we don't need to change any of the defaults set by
  828.    * jpeg_read_header(), so we do nothing here.
  829.    */
  830.  
  831.   /* Step 5: Start decompressor */
  832.  
  833.   jpeg_start_decompress (&cinfo);
  834.  
  835.   /* We may need to do some setup of our own at this point before reading
  836.    * the data.  After jpeg_start_decompress() we have the correct scaled
  837.    * output image dimensions available, as well as the output colormap
  838.    * if we asked for color quantization.
  839.    * In this example, we need to make an output work buffer of the right size.
  840.    */
  841.   /* temporary buffer */
  842.   tile_height = gimp_tile_height ();
  843.   buf = g_new (guchar, 
  844.            tile_height * cinfo.output_width * cinfo.output_components);
  845.   
  846.   if (preview)
  847.     padded_buf = g_new (guchar, tile_height * cinfo.output_width *
  848.             (cinfo.output_components + 1));
  849.  
  850.   rowbuf = g_new (guchar*, tile_height);
  851.  
  852.   for (i = 0; i < tile_height; i++)
  853.     rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
  854.  
  855.   /* Create a new image of the proper size and associate the filename with it.
  856.  
  857.      Preview layers, not being on the bottom of a layer stack, MUST HAVE
  858.      AN ALPHA CHANNEL!
  859.    */
  860.   switch (cinfo.output_components)
  861.     {
  862.     case 1:
  863.       image_type = GIMP_GRAY;
  864.       layer_type = preview ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
  865.       break;
  866.  
  867.     case 3:
  868.       image_type = GIMP_RGB;
  869.       layer_type = preview ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
  870.       break;
  871.  
  872.     default:
  873.       g_message ("don't know how to load JPEGs\nwith %d color channels",
  874.          cinfo.output_components);
  875.       gimp_quit ();
  876.       break;
  877.     }
  878.  
  879.   if (preview) 
  880.     {
  881.       image_ID = image_ID_global;
  882.     } 
  883.   else 
  884.     {
  885.       image_ID = gimp_image_new (cinfo.output_width, cinfo.output_height,
  886.                  image_type);
  887.       gimp_image_set_filename (image_ID, filename);
  888.     }
  889.  
  890.   if (preview) 
  891.     {
  892.       layer_ID_global = layer_ID = 
  893.     gimp_layer_new (image_ID, _("JPEG preview"),
  894.             cinfo.output_width,
  895.             cinfo.output_height,
  896.             layer_type, 100, GIMP_NORMAL_MODE);
  897.     }
  898.   else 
  899.     {
  900.       layer_ID = gimp_layer_new (image_ID, _("Background"),
  901.                  cinfo.output_width,
  902.                  cinfo.output_height,
  903.                  layer_type, 100, GIMP_NORMAL_MODE);
  904.     }
  905.  
  906.   drawable_global = drawable = gimp_drawable_get (layer_ID);
  907.   gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
  908.                drawable->width, drawable->height, TRUE, FALSE);
  909.  
  910. #ifdef GIMP_HAVE_RESOLUTION_INFO
  911.   /* Step 5.1: if the file had resolution information, set it on the image */
  912.   if (!preview && cinfo.saw_JFIF_marker)
  913.     {
  914.       gdouble xresolution;
  915.       gdouble yresolution;
  916.       gdouble asymmetry;
  917.  
  918.       xresolution = cinfo.X_density;
  919.       yresolution = cinfo.Y_density;
  920.  
  921.       switch (cinfo.density_unit)
  922.     {
  923.     case 0: /* unknown -> set the aspect ratio but use the default
  924.         *  image resolution
  925.         */
  926.       asymmetry = xresolution / yresolution;
  927.       gimp_image_get_resolution (image_ID, &xresolution, &yresolution);
  928.       xresolution *= asymmetry;
  929.       break;
  930.  
  931.     case 1: /* dots per inch */
  932.       break;
  933.  
  934.     case 2: /* dots per cm */
  935.       xresolution *= 2.54;
  936.       yresolution *= 2.54;
  937.       gimp_image_set_unit (image_ID, GIMP_UNIT_MM);
  938.       break;
  939.  
  940.     default:
  941.       g_message ("unknown density unit %d\nassuming dots per inch",
  942.              cinfo.density_unit);
  943.       break;
  944.     }
  945.  
  946.       gimp_image_set_resolution (image_ID, xresolution, yresolution);
  947.     }
  948. #endif /* GIMP_HAVE_RESOLUTION_INFO */
  949.  
  950.   /* Step 6: while (scan lines remain to be read) */
  951.   /*           jpeg_read_scanlines(...); */
  952.  
  953.   /* Here we use the library's state variable cinfo.output_scanline as the
  954.    * loop counter, so that we don't have to keep track ourselves.
  955.    */
  956.   while (cinfo.output_scanline < cinfo.output_height)
  957.     {
  958.       start = cinfo.output_scanline;
  959.       end   = cinfo.output_scanline + tile_height;
  960.       end   = MIN (end, cinfo.output_height);
  961.       scanlines = end - start;
  962.  
  963.       for (i = 0; i < scanlines; i++)
  964.     jpeg_read_scanlines (&cinfo, (JSAMPARRAY) &rowbuf[i], 1);
  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.  
  982.         case 3:
  983.           for (i=0; i<num; i++)
  984.         {
  985.           *(dest++) = *(src++);
  986.           *(dest++) = *(src++);
  987.           *(dest++) = *(src++);
  988.           *(dest++) = 255;
  989.         }
  990.           break;
  991.  
  992.         default:
  993.           g_warning ("JPEG - shouldn't have gotten here.  Report to adam@gimp.org");
  994.           break;
  995.         }
  996.     }
  997.  
  998.       gimp_pixel_rgn_set_rect (&pixel_rgn, preview ? padded_buf : buf,
  999.                    0, start, drawable->width, scanlines);
  1000.  
  1001.       if (runmode != GIMP_RUN_NONINTERACTIVE)
  1002.     {
  1003.       gimp_progress_update ((gdouble) cinfo.output_scanline / 
  1004.                 (gdouble) cinfo.output_height);
  1005.     }
  1006.     }
  1007.  
  1008.   /* Step 7: Finish decompression */
  1009.  
  1010.   jpeg_finish_decompress (&cinfo);
  1011.   /* We can ignore the return value since suspension is not possible
  1012.    * with the stdio data source.
  1013.    */
  1014.  
  1015.   /* Step 8: Release JPEG decompression object */
  1016.  
  1017.   /* This is an important step since it will release a good deal of memory. */
  1018.   jpeg_destroy_decompress (&cinfo);
  1019.  
  1020.   /* free up the temporary buffers */
  1021.   g_free (rowbuf);
  1022.   g_free (buf);
  1023.   if (preview)
  1024.     g_free (padded_buf);
  1025.  
  1026.   /* After finish_decompress, we can close the input file.
  1027.    * Here we postpone it until after no more JPEG errors are possible,
  1028.    * so as to simplify the setjmp error logic above.  (Actually, I don't
  1029.    * think that jpeg_destroy can do an error exit, but why assume anything...)
  1030.    */
  1031.   fclose (infile);
  1032.  
  1033.   /* At this point you may want to check to see whether any corrupt-data
  1034.    * warnings occurred (test whether jerr.num_warnings is nonzero).
  1035.    */
  1036.  
  1037.   /* Tell the GIMP to display the image.
  1038.    */
  1039.   gimp_image_add_layer (image_ID, layer_ID, 0);
  1040.   gimp_drawable_flush (drawable);
  1041.  
  1042.   /* pw - Last of all, attach the parasites (couldn't do it earlier -
  1043.      there was no image. */
  1044.  
  1045. #ifdef GIMP_HAVE_PARASITES
  1046.   if (!preview) 
  1047.     {
  1048.       if (comment_parasite)
  1049.     {
  1050.       gimp_image_parasite_attach (image_ID, comment_parasite);
  1051.       gimp_parasite_free (comment_parasite);
  1052.     }
  1053.  
  1054.       if (vals_parasite)
  1055.     {
  1056.       gimp_image_parasite_attach (image_ID, vals_parasite);
  1057.       gimp_parasite_free (vals_parasite);
  1058.     }   
  1059.     }
  1060. #endif /* GIMP_HAVE_PARASITES */
  1061.  
  1062.   return image_ID;
  1063. }
  1064.  
  1065. /*
  1066.  * sg - This is the best I can do, I'm afraid... I think it will fail
  1067.  * if something bad really happens (but it might not). If you have a
  1068.  * better solution, send it ;-)
  1069.  */ 
  1070. static void
  1071. background_error_exit (j_common_ptr cinfo)
  1072. {
  1073.   if (abort_me) 
  1074.     *abort_me = TRUE;
  1075.   (*cinfo->err->output_message) (cinfo);
  1076. }
  1077.  
  1078. static gboolean
  1079. background_jpeg_save (PreviewPersistent *pp)
  1080. {
  1081.   guchar *t;
  1082.   guchar *s;
  1083.   gint    i, j;
  1084.   gint    yend;
  1085.  
  1086.   if (pp->abort_me || (pp->cinfo.next_scanline >= pp->cinfo.image_height))
  1087.     {
  1088.       /* clean up... */
  1089.       if (pp->abort_me)
  1090.     {
  1091.       jpeg_abort_compress (&(pp->cinfo));
  1092.     }
  1093.       else
  1094.     {
  1095.       jpeg_finish_compress (&(pp->cinfo));
  1096.     }
  1097.  
  1098.       fclose (pp->outfile);
  1099.       jpeg_destroy_compress (&(pp->cinfo));
  1100.  
  1101.       g_free (pp->temp);
  1102.       g_free (pp->data);
  1103.  
  1104.       if (pp->drawable) 
  1105.     gimp_drawable_detach (pp->drawable);
  1106.  
  1107.       /* display the preview stuff */
  1108.       if (!pp->abort_me) 
  1109.     {
  1110.       struct stat buf;
  1111.       gchar       temp[128];
  1112.  
  1113.       stat (pp->file_name, &buf);
  1114.       g_snprintf (temp, sizeof (temp),
  1115.               _("Size: %lu bytes (%02.01f kB)"),
  1116.               buf.st_size, (float) (buf.st_size) / 1024.0);
  1117.       gtk_label_set_text (GTK_LABEL (preview_size), temp);
  1118.     
  1119.       /* and load the preview */
  1120.       load_image (pp->file_name, GIMP_RUN_NONINTERACTIVE, TRUE);
  1121.     }
  1122.  
  1123.       /* we cleanup here (load_image doesn't run in the background) */
  1124.       unlink (pp->file_name);
  1125.       g_free (pp->file_name);
  1126.  
  1127.       if (abort_me == &(pp->abort_me)) 
  1128.     abort_me = NULL;
  1129.  
  1130.       g_free (pp);
  1131.  
  1132.       gimp_displays_flush ();
  1133.       gdk_flush ();
  1134.  
  1135.       return FALSE;
  1136.     }
  1137.   else
  1138.     {
  1139.       if ((pp->cinfo.next_scanline % pp->tile_height) == 0)
  1140.     {
  1141.       yend = pp->cinfo.next_scanline + pp->tile_height;
  1142.       yend = MIN (yend, pp->cinfo.image_height);
  1143.       gimp_pixel_rgn_get_rect (&pp->pixel_rgn, pp->data, 0, 
  1144.                    pp->cinfo.next_scanline,
  1145.                    pp->cinfo.image_width,
  1146.                    (yend - pp->cinfo.next_scanline));
  1147.       pp->src = pp->data;
  1148.     }
  1149.  
  1150.       t = pp->temp;
  1151.       s = pp->src;
  1152.       i = pp->cinfo.image_width;
  1153.  
  1154.       while (i--)
  1155.     {
  1156.       for (j = 0; j < pp->cinfo.input_components; j++)
  1157.         *t++ = *s++;
  1158.       if (pp->has_alpha)  /* ignore alpha channel */
  1159.         s++;
  1160.     }
  1161.  
  1162.       pp->src += pp->rowstride;
  1163.       jpeg_write_scanlines (&(pp->cinfo), (JSAMPARRAY) &(pp->temp), 1);
  1164.  
  1165.       return TRUE;
  1166.     }
  1167. }
  1168.  
  1169. static gboolean
  1170. save_image (gchar    *filename,
  1171.         gint32    image_ID,
  1172.         gint32    drawable_ID,
  1173.         gint32    orig_image_ID,
  1174.         gboolean  preview)
  1175. {
  1176.   GimpPixelRgn   pixel_rgn;
  1177.   GimpDrawable  *drawable;
  1178.   GimpImageType  drawable_type;
  1179.   struct jpeg_compress_struct cinfo;
  1180.   struct my_error_mgr         jerr;
  1181.   FILE     * volatile outfile;
  1182.   guchar   *temp, *t;
  1183.   guchar   *data;
  1184.   guchar   *src, *s;
  1185.   gchar    *name;
  1186.   gboolean  has_alpha;
  1187.   gint      rowstride, yend;
  1188.   gint      i, j;
  1189.  
  1190.   drawable = gimp_drawable_get (drawable_ID);
  1191.   drawable_type = gimp_drawable_type (drawable_ID);
  1192.   gimp_pixel_rgn_init (&pixel_rgn, drawable,
  1193.                0, 0, drawable->width, drawable->height, FALSE, FALSE);
  1194.  
  1195.   if (!preview) 
  1196.     {
  1197.       name = g_strdup_printf (_("Saving %s:"), filename);
  1198.       gimp_progress_init (name);
  1199.       g_free (name);
  1200.     }
  1201.  
  1202.   /* Step 1: allocate and initialize JPEG compression object */
  1203.  
  1204.   /* We have to set up the error handler first, in case the initialization
  1205.    * step fails.  (Unlikely, but it could happen if you are out of memory.)
  1206.    * This routine fills in the contents of struct jerr, and returns jerr's
  1207.    * address which we place into the link field in cinfo.
  1208.    */
  1209.   cinfo.err = jpeg_std_error (&jerr.pub);
  1210.   jerr.pub.error_exit = my_error_exit;
  1211.  
  1212.   outfile = NULL;
  1213.   /* Establish the setjmp return context for my_error_exit to use. */
  1214.   if (setjmp (jerr.setjmp_buffer))
  1215.     {
  1216.       /* If we get here, the JPEG code has signaled an error.
  1217.        * We need to clean up the JPEG object, close the input file, and return.
  1218.        */
  1219.       jpeg_destroy_compress (&cinfo);
  1220.       if (outfile)
  1221.     fclose (outfile);
  1222.       if (drawable)
  1223.     gimp_drawable_detach (drawable);
  1224.  
  1225.       return FALSE;
  1226.     }
  1227.  
  1228.   /* Now we can initialize the JPEG compression object. */
  1229.   jpeg_create_compress (&cinfo);
  1230.  
  1231.   /* Step 2: specify data destination (eg, a file) */
  1232.   /* Note: steps 2 and 3 can be done in either order. */
  1233.  
  1234.   /* Here we use the library-supplied code to send compressed data to a
  1235.    * stdio stream.  You can also write your own code to do something else.
  1236.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  1237.    * requires it in order to write binary files.
  1238.    */
  1239.   if ((outfile = fopen (filename, "wb")) == NULL)
  1240.     {
  1241.       g_message ("can't open %s\n", filename);
  1242.       return FALSE;
  1243.     }
  1244.   jpeg_stdio_dest (&cinfo, outfile);
  1245.  
  1246.   /* Get the input image and a pointer to its data.
  1247.    */
  1248.   switch (drawable_type)
  1249.     {
  1250.     case GIMP_RGB_IMAGE:
  1251.     case GIMP_GRAY_IMAGE:
  1252.       /* # of color components per pixel */
  1253.       cinfo.input_components = drawable->bpp;
  1254.       has_alpha = FALSE;
  1255.       break;
  1256.  
  1257.     case GIMP_RGBA_IMAGE:
  1258.     case GIMP_GRAYA_IMAGE:
  1259.       /*gimp_message ("jpeg: image contains a-channel info which will be lost");*/
  1260.       /* # of color components per pixel (minus the GIMP alpha channel) */
  1261.       cinfo.input_components = drawable->bpp - 1;
  1262.       has_alpha = TRUE;
  1263.       break;
  1264.  
  1265.     case GIMP_INDEXED_IMAGE:
  1266.       /*gimp_message ("jpeg: cannot operate on indexed color images");*/
  1267.       return FALSE;
  1268.       break;
  1269.  
  1270.     default:
  1271.       /*gimp_message ("jpeg: cannot operate on unknown image types");*/
  1272.       return FALSE;
  1273.       break;
  1274.     }
  1275.  
  1276.   /* Step 3: set parameters for compression */
  1277.  
  1278.   /* First we supply a description of the input image.
  1279.    * Four fields of the cinfo struct must be filled in:
  1280.    */
  1281.   /* image width and height, in pixels */
  1282.   cinfo.image_width  = drawable->width;
  1283.   cinfo.image_height = drawable->height;
  1284.   /* colorspace of input image */
  1285.   cinfo.in_color_space = (drawable_type == GIMP_RGB_IMAGE ||
  1286.               drawable_type == GIMP_RGBA_IMAGE)
  1287.     ? JCS_RGB : JCS_GRAYSCALE;
  1288.   /* Now use the library's routine to set default compression parameters.
  1289.    * (You must set at least cinfo.in_color_space before calling this,
  1290.    * since the defaults depend on the source color space.)
  1291.    */
  1292.   jpeg_set_defaults (&cinfo);
  1293.  
  1294.   jpeg_set_quality (&cinfo, (gint) (jsvals.quality * 100), jsvals.baseline);
  1295.   cinfo.smoothing_factor = (gint) (jsvals.smoothing * 100);
  1296.   cinfo.optimize_coding = jsvals.optimize;
  1297.  
  1298. #ifdef HAVE_PROGRESSIVE_JPEG
  1299.   if (jsvals.progressive) 
  1300.     {
  1301.       jpeg_simple_progression (&cinfo);
  1302.     }
  1303. #endif /* HAVE_PROGRESSIVE_JPEG */
  1304.  
  1305.   switch (jsvals.subsmp) 
  1306.     {
  1307.     case 0:
  1308.     default:
  1309.       cinfo.comp_info[0].h_samp_factor = 2;
  1310.       cinfo.comp_info[0].v_samp_factor = 2;
  1311.       cinfo.comp_info[1].h_samp_factor = 1;
  1312.       cinfo.comp_info[1].v_samp_factor = 1;
  1313.       cinfo.comp_info[2].h_samp_factor = 1;
  1314.       cinfo.comp_info[2].v_samp_factor = 1;
  1315.       break;
  1316.  
  1317.     case 1:
  1318.       cinfo.comp_info[0].h_samp_factor = 2;
  1319.       cinfo.comp_info[0].v_samp_factor = 1;
  1320.       cinfo.comp_info[1].h_samp_factor = 1;
  1321.       cinfo.comp_info[1].v_samp_factor = 1;
  1322.       cinfo.comp_info[2].h_samp_factor = 1;
  1323.       cinfo.comp_info[2].v_samp_factor = 1;
  1324.       break;
  1325.  
  1326.     case 2:
  1327.       cinfo.comp_info[0].h_samp_factor = 1;
  1328.       cinfo.comp_info[0].v_samp_factor = 1;
  1329.       cinfo.comp_info[1].h_samp_factor = 1;
  1330.       cinfo.comp_info[1].v_samp_factor = 1;
  1331.       cinfo.comp_info[2].h_samp_factor = 1;
  1332.       cinfo.comp_info[2].v_samp_factor = 1;
  1333.       break;
  1334.     }
  1335.   
  1336.   cinfo.restart_interval = 0;
  1337.   cinfo.restart_in_rows = jsvals.restart;
  1338.  
  1339.   switch (jsvals.dct) 
  1340.     {
  1341.     case 0:
  1342.     default:
  1343.       cinfo.dct_method = JDCT_ISLOW;
  1344.       break;
  1345.  
  1346.     case 1:
  1347.       cinfo.dct_method = JDCT_IFAST;
  1348.       break;
  1349.  
  1350.     case 2:
  1351.       cinfo.dct_method = JDCT_FLOAT;
  1352.       break;
  1353.     }
  1354.  
  1355. #ifdef GIMP_HAVE_RESOLUTION_INFO
  1356.   {
  1357.     gdouble xresolution;
  1358.     gdouble yresolution;
  1359.  
  1360.     gimp_image_get_resolution (orig_image_ID, &xresolution, &yresolution);
  1361.  
  1362.     if (xresolution > 1e-5 && yresolution > 1e-5)
  1363.       {
  1364.     gdouble factor;
  1365.  
  1366.     factor = gimp_unit_get_factor (gimp_image_get_unit (orig_image_ID));
  1367.  
  1368.     if (factor == 2.54 /* cm */ ||
  1369.         factor == 25.4 /* mm */)
  1370.       {
  1371.         cinfo.density_unit = 2;  /* dots per cm */
  1372.  
  1373.         xresolution /= 2.54;
  1374.         yresolution /= 2.54;
  1375.       }
  1376.     else
  1377.       {
  1378.         cinfo.density_unit = 1;  /* dots per inch */
  1379.       }
  1380.  
  1381.     cinfo.X_density = xresolution;
  1382.     cinfo.Y_density = yresolution;
  1383.       }
  1384.   }
  1385. #endif /* GIMP_HAVE_RESOLUTION_INFO */
  1386.  
  1387.   /* Step 4: Start compressor */
  1388.  
  1389.   /* TRUE ensures that we will write a complete interchange-JPEG file.
  1390.    * Pass TRUE unless you are very sure of what you're doing.
  1391.    */
  1392.   jpeg_start_compress (&cinfo, TRUE);
  1393.  
  1394.   /* Step 4.1: Write the comment out - pw */
  1395.   if (image_comment && *image_comment)
  1396.     {
  1397.       jpeg_write_marker (&cinfo,
  1398.              JPEG_COM,
  1399.              image_comment,
  1400.              strlen (image_comment));
  1401.     }
  1402.     
  1403.   /* Step 5: while (scan lines remain to be written) */
  1404.   /*           jpeg_write_scanlines(...); */
  1405.  
  1406.   /* Here we use the library's state variable cinfo.next_scanline as the
  1407.    * loop counter, so that we don't have to keep track ourselves.
  1408.    * To keep things simple, we pass one scanline per call; you can pass
  1409.    * more if you wish, though.
  1410.    */
  1411.   /* JSAMPLEs per row in image_buffer */
  1412.   rowstride = drawable->bpp * drawable->width;
  1413.   temp = g_new (guchar, cinfo.image_width * cinfo.input_components);
  1414.   data = g_new (guchar, rowstride * gimp_tile_height ());
  1415.  
  1416.   /* fault if cinfo.next_scanline isn't initially a multiple of
  1417.    * gimp_tile_height */
  1418.   src = NULL;
  1419.  
  1420.   /*
  1421.    * sg - if we preview, we want this to happen in the background -- do
  1422.    * not duplicate code in the future; for now, it's OK
  1423.    */
  1424.  
  1425.   if (preview) 
  1426.     {
  1427.       PreviewPersistent *pp = g_new (PreviewPersistent, 1);
  1428.       
  1429.       /* pass all the information we need */
  1430.       pp->cinfo       = cinfo;
  1431.       pp->tile_height = gimp_tile_height();
  1432.       pp->data        = data;
  1433.       pp->outfile     = outfile;
  1434.       pp->has_alpha   = has_alpha;
  1435.       pp->rowstride   = rowstride;
  1436.       pp->temp        = temp;
  1437.       pp->data        = data;
  1438.       pp->drawable    = drawable;
  1439.       pp->pixel_rgn   = pixel_rgn;
  1440.       pp->src         = NULL;
  1441.       pp->file_name   = filename;
  1442.       
  1443.       pp->abort_me    = FALSE;
  1444.       abort_me = &(pp->abort_me);
  1445.       
  1446.       jerr.pub.error_exit = background_error_exit;
  1447.  
  1448.       gtk_idle_add ((GtkFunction) background_jpeg_save, (gpointer) pp);
  1449.       
  1450.       /* background_jpeg_save() will cleanup as needed */
  1451.       return TRUE;
  1452.     } 
  1453.  
  1454.   while (cinfo.next_scanline < cinfo.image_height)
  1455.     {
  1456.       if ((cinfo.next_scanline % gimp_tile_height ()) == 0)
  1457.     {
  1458.       yend = cinfo.next_scanline + gimp_tile_height ();
  1459.       yend = MIN (yend, cinfo.image_height);
  1460.       gimp_pixel_rgn_get_rect (&pixel_rgn, data, 
  1461.                    0, cinfo.next_scanline, 
  1462.                    cinfo.image_width,
  1463.                    (yend - cinfo.next_scanline));
  1464.       src = data;
  1465.     }
  1466.  
  1467.       t = temp;
  1468.       s = src;
  1469.       i = cinfo.image_width;
  1470.  
  1471.       while (i--)
  1472.     {
  1473.       for (j = 0; j < cinfo.input_components; j++)
  1474.         *t++ = *s++;
  1475.       if (has_alpha)  /* ignore alpha channel */
  1476.         s++;
  1477.     }
  1478.  
  1479.       src += rowstride;
  1480.       jpeg_write_scanlines (&cinfo, (JSAMPARRAY) &temp, 1);
  1481.  
  1482.       if ((cinfo.next_scanline % 5) == 0)
  1483.     gimp_progress_update ((gdouble) cinfo.next_scanline / 
  1484.                   (gdouble) cinfo.image_height);
  1485.     }
  1486.  
  1487.   /* Step 6: Finish compression */
  1488.   jpeg_finish_compress (&cinfo);
  1489.   /* After finish_compress, we can close the output file. */
  1490.   fclose (outfile);
  1491.  
  1492.   /* Step 7: release JPEG compression object */
  1493.  
  1494.   /* This is an important step since it will release a good deal of memory. */
  1495.   jpeg_destroy_compress (&cinfo);
  1496.   /* free the temporary buffer */
  1497.   g_free (temp);
  1498.   g_free (data);
  1499.  
  1500.   /* And we're done! */
  1501.   /*gimp_do_progress (1, 1);*/
  1502.  
  1503.   gimp_drawable_detach (drawable);
  1504.  
  1505.   return TRUE;
  1506. }
  1507.  
  1508. static void
  1509. make_preview (void)
  1510. {
  1511.   gchar *tn;
  1512.  
  1513.   destroy_preview ();
  1514.  
  1515.   if (jsvals.preview) 
  1516.     {
  1517.       tn = gimp_temp_name ("jpeg");
  1518.       save_image (tn, 
  1519.           image_ID_global,
  1520.           drawable_ID_global, 
  1521.           orig_image_ID_global, 
  1522.           TRUE);
  1523.     }
  1524.   else 
  1525.     {
  1526.       gtk_label_set_text (GTK_LABEL (preview_size), _("Size: unknown"));
  1527.       gtk_widget_draw (preview_size, NULL);
  1528.       
  1529.       gimp_displays_flush ();
  1530.       gdk_flush();
  1531.     }
  1532. }
  1533.  
  1534. static void
  1535. destroy_preview (void)
  1536. {
  1537.   if (abort_me) 
  1538.     {
  1539.       *abort_me = TRUE;   /* signal the background save to stop */
  1540.     }
  1541.  
  1542.   if (drawable_global) 
  1543.     {
  1544.       gimp_drawable_detach (drawable_global);
  1545.       drawable_global = NULL;
  1546.     }
  1547.  
  1548.   if (layer_ID_global != -1 && image_ID_global != -1) 
  1549.     {
  1550.       /*  assuming that reference counting is working correctly, 
  1551.       we do not need to delete the layer, removing it from
  1552.       the image should be sufficient  */
  1553.       gimp_image_remove_layer (image_ID_global, layer_ID_global);
  1554.       
  1555.       layer_ID_global = -1;
  1556.     }
  1557. }
  1558.  
  1559. static gboolean
  1560. save_dialog (void)
  1561. {
  1562.   GtkWidget *dlg;
  1563.   GtkWidget *vbox;
  1564.   GtkWidget *main_vbox;
  1565.   GtkWidget *label;
  1566.   GtkWidget *scale;
  1567.   GtkWidget *frame;
  1568.   GtkWidget *table;
  1569.   GtkWidget *toggle;
  1570.   GtkWidget *abox;
  1571.   GtkObject *scale_data;
  1572.  
  1573.   GtkWidget *progressive;
  1574.   GtkWidget *baseline;
  1575.   GtkWidget *restart;
  1576.  
  1577.   GtkWidget *preview;
  1578.   /* GtkWidget *preview_size; -- global */
  1579.  
  1580.   GtkWidget *menu;
  1581.   
  1582.   GtkWidget *text;
  1583.   GtkWidget *com_frame;
  1584.   GtkWidget *com_table;
  1585.   GtkWidget *vscrollbar;
  1586.   
  1587.   GtkWidget *prv_frame;
  1588.   GimpImageType dtype;
  1589.  
  1590.   dlg = gimp_dialog_new (_("Save as JPEG"), "jpeg",
  1591.              gimp_standard_help_func, "filters/jpeg.html",
  1592.              GTK_WIN_POS_MOUSE,
  1593.              FALSE, TRUE, FALSE,
  1594.  
  1595.              _("OK"), save_ok_callback,
  1596.              NULL, NULL, NULL, TRUE, FALSE,
  1597.              _("Cancel"), gtk_widget_destroy,
  1598.              NULL, 1, NULL, FALSE, TRUE,
  1599.  
  1600.              NULL);
  1601.  
  1602.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  1603.               GTK_SIGNAL_FUNC (save_close_callback),
  1604.               NULL);
  1605.  
  1606.   main_vbox = gtk_vbox_new (FALSE, 4);
  1607.   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
  1608.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), main_vbox,
  1609.               TRUE, TRUE, 0);
  1610.   gtk_widget_show (main_vbox);
  1611.  
  1612.   /* sg - preview */
  1613.   prv_frame = gtk_frame_new (_("Image Preview"));
  1614.   gtk_frame_set_shadow_type (GTK_FRAME (prv_frame), GTK_SHADOW_ETCHED_IN);
  1615.   gtk_box_pack_start (GTK_BOX (main_vbox), prv_frame, FALSE, FALSE, 0);
  1616.  
  1617.   vbox = gtk_vbox_new (FALSE, 2);
  1618.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  1619.   gtk_container_add (GTK_CONTAINER (prv_frame), vbox);
  1620.   gtk_widget_show (vbox);
  1621.  
  1622.   preview = gtk_check_button_new_with_label (_("Preview (in image window)"));
  1623.   gtk_box_pack_start (GTK_BOX (vbox), preview, FALSE, FALSE, 0);
  1624.   gtk_signal_connect (GTK_OBJECT (preview), "toggled",
  1625.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1626.               &jsvals.preview);
  1627.   gtk_signal_connect (GTK_OBJECT (preview), "toggled",
  1628.               GTK_SIGNAL_FUNC (make_preview),
  1629.               NULL);
  1630.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (preview), jsvals.preview);
  1631.   gtk_widget_show (preview);
  1632.  
  1633.   preview_size = gtk_label_new (_("Size: unknown"));
  1634.   gtk_misc_set_alignment (GTK_MISC (preview_size), 0.0, 0.5);
  1635.   gtk_box_pack_start (GTK_BOX (vbox), preview_size, FALSE, FALSE, 0);
  1636.   gtk_widget_show (preview_size);
  1637.  
  1638.   gtk_widget_show (prv_frame);
  1639.  
  1640.   make_preview ();
  1641.  
  1642.   /*  parameter settings  */
  1643.   frame = gtk_frame_new (_("Parameter Settings"));
  1644.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  1645.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  1646.  
  1647.   table = gtk_table_new (9, 3, FALSE);
  1648.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  1649.   gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  1650.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  1651.   gtk_container_add (GTK_CONTAINER (frame), table);
  1652.  
  1653.   label = gtk_label_new (_("Quality:"));
  1654.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  1655.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
  1656.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1657.   gtk_widget_show (label);
  1658.  
  1659.   scale_data = gtk_adjustment_new (jsvals.quality, 0.0, 1.0, 0.01, 0.01, 0.0);
  1660.   scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1661.   gtk_widget_set_usize (scale, SCALE_WIDTH, 0);
  1662.   gtk_table_attach (GTK_TABLE (table), scale, 1, 3, 0, 1,
  1663.             GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
  1664.   gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  1665.   gtk_scale_set_digits (GTK_SCALE (scale), 2);
  1666.   gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  1667.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1668.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  1669.               &jsvals.quality);
  1670.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1671.               GTK_SIGNAL_FUNC (make_preview),
  1672.               NULL);
  1673.   gtk_widget_show (scale);
  1674.  
  1675.   label = gtk_label_new (_("Smoothing:"));
  1676.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 1.0);
  1677.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
  1678.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1679.   gtk_widget_show (label);
  1680.  
  1681.   scale_data = gtk_adjustment_new (jsvals.smoothing, 0.0, 1.0, 0.01, 0.01, 0.0);
  1682.   scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1683.   gtk_widget_set_usize (scale, SCALE_WIDTH, 0);
  1684.   gtk_table_attach (GTK_TABLE (table), scale, 1, 3, 1, 2,
  1685.             GTK_FILL | GTK_EXPAND, GTK_FILL, 0, 0);
  1686.   gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_TOP);
  1687.   gtk_scale_set_digits (GTK_SCALE (scale), 2);
  1688.   gtk_range_set_update_policy (GTK_RANGE (scale), GTK_UPDATE_DELAYED);
  1689.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1690.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  1691.               &jsvals.smoothing);
  1692.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1693.               GTK_SIGNAL_FUNC (make_preview),
  1694.               NULL);
  1695.   gtk_widget_show (scale);
  1696.  
  1697.   /* sg - have to init scale here */
  1698.   scale_data = gtk_adjustment_new ((jsvals.restart == 0) ? 1 : jsvals.restart,
  1699.                    1, 64, 1, 1, 0.0);
  1700.   restart_markers_scale = gtk_hscale_new (GTK_ADJUSTMENT (scale_data));
  1701.  
  1702.   restart = gtk_check_button_new_with_label (_("Restart markers"));
  1703.   gtk_table_attach (GTK_TABLE (table), restart, 0, 1, 2, 3,
  1704.             GTK_FILL, 0, 0, 0);
  1705.   gtk_signal_connect (GTK_OBJECT (restart), "toggled",
  1706.               GTK_SIGNAL_FUNC (save_restart_toggle_update),
  1707.               scale_data);
  1708.   gtk_widget_show (restart);
  1709.  
  1710.   restart_markers_label = gtk_label_new (_("Restart frequency (rows):"));
  1711.   gtk_misc_set_alignment (GTK_MISC (restart_markers_label), 1.0, 1.0);
  1712.   gtk_table_attach (GTK_TABLE (table), restart_markers_label, 0, 1, 3, 4,
  1713.             GTK_FILL | GTK_SHRINK, GTK_FILL, 0, 0);
  1714.   gtk_widget_show (restart_markers_label);
  1715.  
  1716.   abox = gtk_alignment_new (0.5, 1.0, 1.0, 0.0);
  1717.   gtk_table_attach (GTK_TABLE (table), abox, 1, 3, 2, 4,
  1718.             GTK_EXPAND | GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);
  1719.   gtk_widget_show (abox);
  1720.  
  1721.   gtk_widget_set_usize (restart_markers_scale, SCALE_WIDTH, 0);
  1722.   gtk_container_add (GTK_CONTAINER (abox), restart_markers_scale);
  1723.   gtk_scale_set_value_pos (GTK_SCALE (restart_markers_scale), GTK_POS_TOP);
  1724.   gtk_scale_set_digits (GTK_SCALE (restart_markers_scale), 0);
  1725.   gtk_range_set_update_policy (GTK_RANGE (restart_markers_scale),
  1726.                    GTK_UPDATE_DELAYED);
  1727.  
  1728.   gtk_signal_connect (GTK_OBJECT (scale_data), "value_changed",
  1729.                       GTK_SIGNAL_FUNC (save_restart_update),
  1730.                       restart);
  1731.  
  1732.   gtk_widget_set_sensitive (restart_markers_label, 
  1733.                 (jsvals.restart ? TRUE : FALSE));
  1734.   gtk_widget_set_sensitive (restart_markers_scale,
  1735.                 (jsvals.restart ? TRUE : FALSE));
  1736.  
  1737.   gtk_widget_show (restart_markers_scale);
  1738.  
  1739.   toggle = gtk_check_button_new_with_label (_("Optimize"));
  1740.   gtk_table_attach (GTK_TABLE (table), toggle, 0, 3, 4, 5,
  1741.             GTK_FILL, 0, 0, 0);
  1742.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1743.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1744.               &jsvals.optimize);
  1745.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  1746.               GTK_SIGNAL_FUNC (make_preview),
  1747.               NULL);
  1748.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), jsvals.optimize);
  1749.   gtk_widget_show (toggle);
  1750.  
  1751.   progressive = gtk_check_button_new_with_label (_("Progressive"));
  1752.   gtk_table_attach (GTK_TABLE (table), progressive, 0, 3, 5, 6,
  1753.             GTK_FILL, 0, 0, 0);
  1754.   gtk_signal_connect (GTK_OBJECT (progressive), "toggled",
  1755.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1756.               &jsvals.progressive);
  1757.   gtk_signal_connect (GTK_OBJECT (progressive), "toggled",
  1758.               GTK_SIGNAL_FUNC (make_preview),
  1759.               NULL);
  1760.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (progressive),
  1761.                 jsvals.progressive);
  1762.   gtk_widget_show (progressive);
  1763.  
  1764. #ifndef HAVE_PROGRESSIVE_JPEG
  1765.   gtk_widget_set_sensitive (progressive, FALSE);
  1766. #endif
  1767.   
  1768.   baseline = gtk_check_button_new_with_label (_("Force baseline JPEG (Readable by all decoders)"));
  1769.   gtk_table_attach (GTK_TABLE (table), baseline, 0, 3, 6, 7,
  1770.             GTK_FILL, 0, 0, 0);
  1771.   gtk_signal_connect (GTK_OBJECT (baseline), "toggled",
  1772.               GTK_SIGNAL_FUNC (gimp_toggle_button_update),
  1773.               &jsvals.baseline);
  1774.   gtk_signal_connect (GTK_OBJECT (baseline), "toggled",
  1775.               GTK_SIGNAL_FUNC (make_preview),
  1776.               NULL);
  1777.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (baseline),
  1778.                 jsvals.baseline);
  1779.   gtk_widget_show (baseline);
  1780.  
  1781.   /* Subsampling */
  1782.   menu = 
  1783.     gimp_option_menu_new2 (FALSE, 
  1784.                menu_callback, 
  1785.                &jsvals.subsmp, GINT_TO_POINTER (jsvals.subsmp),
  1786.                "2x2,1x1,1x1",  GINT_TO_POINTER (0), NULL, 
  1787.                "2x1,1x1,1x1 (4:2:2)", GINT_TO_POINTER (1), NULL,
  1788.                "1x1,1x1,1x1",  GINT_TO_POINTER (2), NULL,
  1789.                NULL);
  1790.  
  1791.   gimp_table_attach_aligned (GTK_TABLE (table), 1, 7, 
  1792.                  _("Subsampling:"),
  1793.                  1.0, 0.5,
  1794.                  menu, 1, FALSE);
  1795.  
  1796.   /* DCT method */
  1797.   menu = 
  1798.     gimp_option_menu_new2 (FALSE, 
  1799.                menu_callback, 
  1800.                &jsvals.dct, GINT_TO_POINTER (jsvals.dct),
  1801.                _("Fast Integer"),   GINT_TO_POINTER (1), NULL, 
  1802.                _("Integer"),        GINT_TO_POINTER (0), NULL,
  1803.                _("Floating-Point"), GINT_TO_POINTER (2), NULL,
  1804.                NULL);
  1805.  
  1806.   gimp_table_attach_aligned (GTK_TABLE (table), 1, 8, 
  1807.                  _("DCT method (Speed/quality tradeoff):"),
  1808.                  1.0, 0.5,
  1809.                  menu, 1, FALSE);
  1810.  
  1811.   dtype = gimp_drawable_type (drawable_ID_global);
  1812.   if (dtype != GIMP_RGB_IMAGE && dtype != GIMP_RGBA_IMAGE) 
  1813.     gtk_widget_set_sensitive (menu, FALSE);
  1814.  
  1815.   com_frame = gtk_frame_new (_("Image comments"));
  1816.   gtk_frame_set_shadow_type (GTK_FRAME (com_frame), GTK_SHADOW_ETCHED_IN);
  1817.   gtk_box_pack_start (GTK_BOX (main_vbox), com_frame, TRUE, TRUE, 0);
  1818.  
  1819.   com_table = gtk_table_new (1, 2, FALSE);
  1820.   gtk_container_set_border_width (GTK_CONTAINER (com_table), 4);
  1821.   gtk_container_add (GTK_CONTAINER (com_frame), com_table);
  1822.  
  1823.   text = gtk_text_new (NULL, NULL);
  1824.   gtk_text_set_editable (GTK_TEXT (text), TRUE);
  1825.   gtk_widget_set_usize (text, -1, 3); /* HB: restrict to 3 line height 
  1826.                        * to allow 800x600 mode */
  1827.   if (image_comment) 
  1828.     gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, image_comment, -1);
  1829.   gtk_table_attach (GTK_TABLE (com_table), text, 0, 1, 0, 1,
  1830.                     GTK_EXPAND | GTK_SHRINK | GTK_FILL,
  1831.                     GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  1832.   
  1833.   /* Add a vertical scrollbar to the GtkText widget */
  1834.   vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
  1835.   gtk_table_attach (GTK_TABLE (com_table), vscrollbar, 1, 2, 0, 1,
  1836.                     GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
  1837.   gtk_widget_show (vscrollbar);
  1838.  
  1839.   /* pw - mild hack here.  I didn't like redoing the comment string
  1840.    * each time a character was typed, so I associated the text area
  1841.    * with the dialog.  That way, just before the dialog destroys
  1842.    * itself (once the ok button is hit) it can save whatever was in
  1843.    * the comment text area to the comment string.  See the
  1844.    * save-ok-callback for more details.  */
  1845.   
  1846.   gtk_object_set_data (GTK_OBJECT (dlg), "text", text); 
  1847.   
  1848.   gtk_widget_show (text);
  1849.   gtk_widget_show (com_frame);
  1850.   gtk_widget_show (com_table);
  1851.  
  1852.   gtk_widget_show (frame);
  1853.   gtk_widget_show (table);
  1854.   gtk_widget_show (dlg);
  1855.  
  1856.   gtk_main ();
  1857.   gdk_flush ();
  1858.  
  1859.   return jsint.run;
  1860. }
  1861.  
  1862. /*  Save interface functions  */
  1863.  
  1864. static void
  1865. save_close_callback (GtkWidget *widget,
  1866.              gpointer   data)
  1867. {
  1868.   destroy_preview ();
  1869.   gimp_displays_flush ();
  1870.   gtk_main_quit ();
  1871. }
  1872.  
  1873. static void
  1874. save_ok_callback (GtkWidget *widget,
  1875.           gpointer   data)
  1876. {
  1877.   GtkWidget *text;
  1878.  
  1879.   jsint.run = TRUE;
  1880.  
  1881.   /* pw - get the comment text object and grab it's data */
  1882.   text = gtk_object_get_data (GTK_OBJECT (data), "text");
  1883.   
  1884.   /* pw - gtk_editable_get_chars allocates a copy of the string, so
  1885.    * don't worry about the gtk_widget_destroy killing it.  */
  1886.  
  1887.   g_free (image_comment);
  1888.   image_comment = NULL;
  1889.  
  1890.   if (text)
  1891.     image_comment = gtk_editable_get_chars (GTK_EDITABLE (text), 0, -1);
  1892.  
  1893.   gtk_widget_destroy (GTK_WIDGET (data));
  1894. }
  1895.  
  1896. static void
  1897. save_restart_toggle_update (GtkWidget     *widget,
  1898.                 GtkAdjustment *adjustment)
  1899. {
  1900.   save_restart_update (adjustment, widget);
  1901. }
  1902.  
  1903. static void
  1904. save_restart_update (GtkAdjustment *adjustment,
  1905.              GtkWidget     *toggle)
  1906. {
  1907.   jsvals.restart = GTK_TOGGLE_BUTTON (toggle)->active ? adjustment->value : 0;
  1908.  
  1909.   gtk_widget_set_sensitive (restart_markers_label,
  1910.                 (jsvals.restart ? TRUE : FALSE));
  1911.   gtk_widget_set_sensitive (restart_markers_scale,
  1912.                 (jsvals.restart ? TRUE : FALSE));
  1913.           
  1914.   make_preview ();
  1915. }
  1916.  
  1917. static void
  1918. menu_callback (GtkWidget *widget,
  1919.            gpointer   data)
  1920. {
  1921.   gimp_menu_item_update (widget, data);
  1922.   make_preview ();
  1923. }
  1924.